[PATCH 3/3] hwmon: (pmbus/tps25990): add initial support

Jerome Brunet posted 3 patches 2 months, 3 weeks ago
There is a newer version of this series
[PATCH 3/3] hwmon: (pmbus/tps25990): add initial support
Posted by Jerome Brunet 2 months, 3 weeks ago
Add initial support for the Texas Instruments TPS25990 eFuse.
This adds the basic PMBUS telemetry support for the device.

Signed-off-by: Jerome Brunet <jbrunet@baylibre.com>
---
 Documentation/hwmon/tps25990.rst | 141 ++++++++++++
 drivers/hwmon/pmbus/Kconfig      |  17 ++
 drivers/hwmon/pmbus/Makefile     |   1 +
 drivers/hwmon/pmbus/tps25990.c   | 474 +++++++++++++++++++++++++++++++++++++++
 4 files changed, 633 insertions(+)

diff --git a/Documentation/hwmon/tps25990.rst b/Documentation/hwmon/tps25990.rst
new file mode 100644
index 000000000000..7b3ef724008a
--- /dev/null
+++ b/Documentation/hwmon/tps25990.rst
@@ -0,0 +1,141 @@
+.. SPDX-License-Identifier: GPL-2.0
+
+Kernel driver tps25990
+======================
+
+Supported chips:
+
+  * TI TPS25990
+
+    Prefix: 'tps25990'
+
+  * Datasheet
+
+    Publicly available at Texas Instruments website: https://www.ti.com/lit/gpn/tps25990
+
+Author:
+
+	Jerome Brunet <jbrunet@baylibre.com>
+
+Description
+-----------
+
+This driver implements support for TI TPS25990 eFuse.
+This is an integrated, high-current circuit protection and power
+management device with PMBUS interface
+
+Device compliant with:
+
+- PMBus rev 1.3 interface.
+
+Device supports direct format for reading input voltages,
+output voltage, input current, input power and temperature.
+
+The driver exports the following attributes via the 'sysfs' files
+for input current:
+
+**curr1_average**
+
+**curr1_crit**
+
+**curr1_crit_alarm**
+
+**curr1_highest**
+
+**curr1_input**
+
+**curr1_label**
+
+**curr1_max**
+
+**curr1_max_alarm**
+
+The driver provides the following attributes for main input voltage:
+
+**in1_average**
+
+**in1_crit**
+
+**in1_crit_alarm**
+
+**in1_highest**
+
+**in1_input**
+
+**in1_label**
+
+**in1_lcrit**
+
+**in1_lcrit_alarm**
+
+**in1_lowest**
+
+**in1_max**
+
+**in1_max_alarm**
+
+**in1_min**
+
+**in1_min_alarm**
+
+The driver provides the following attributes for auxiliary input voltage:
+
+**in2_input**
+
+**in2_label**
+
+The driver provides the following attributes for output voltage:
+
+**in3_average**
+
+**in3_good_off**
+
+**in3_input**
+
+**in3_label**
+
+**in3_lowest**
+
+**in3_min**
+
+**in3_min_alarm**
+
+The driver provides the following attributes for input power:
+
+**power1_alarm**
+
+**power1_average**
+
+**power1_input**
+
+**power1_input_highest**
+
+**power1_label**
+
+**power1_max**
+
+The driver provides the following attributes for temperature:
+
+**temp1_average**
+
+**temp1_crit**
+
+**temp1_crit_alarm**
+
+**temp1_highest**
+
+**temp1_input**
+
+**temp1_max**
+
+**temp1_max_alarm**
+
+The driver provides the following attributes for history:
+
+**samples**
+
+**average_history_reset**
+
+**highest_history_reset**
+
+**lowest_history_reset**
diff --git a/drivers/hwmon/pmbus/Kconfig b/drivers/hwmon/pmbus/Kconfig
index a4f02cad92fd..3559864e232d 100644
--- a/drivers/hwmon/pmbus/Kconfig
+++ b/drivers/hwmon/pmbus/Kconfig
@@ -510,6 +510,23 @@ config SENSORS_TDA38640_REGULATOR
 	  If you say yes here you get regulator support for Infineon
 	  TDA38640 as regulator.
 
+config SENSORS_TPS25990
+	tristate "TI TPS25990"
+	help
+	  If you say yes here you get hardware monitoring support for TI
+	  TPS25990.
+
+	  This driver can also be built as a module. If so, the module will
+	  be called tps25990.
+
+config SENSORS_TPS25990_REGULATOR
+	bool "Regulator support for TPS25990 and compatibles"
+	depends on SENSORS_TPS25990 && REGULATOR
+	default SENSORS_TPS2599
+	help
+	  If you say yes here you get regulator support for Texas Instruments
+	  TPS25990.
+
 config SENSORS_TPS40422
 	tristate "TI TPS40422"
 	help
diff --git a/drivers/hwmon/pmbus/Makefile b/drivers/hwmon/pmbus/Makefile
index d00bcc758b97..3d3183f8d2a7 100644
--- a/drivers/hwmon/pmbus/Makefile
+++ b/drivers/hwmon/pmbus/Makefile
@@ -51,6 +51,7 @@ obj-$(CONFIG_SENSORS_PXE1610)	+= pxe1610.o
 obj-$(CONFIG_SENSORS_Q54SJ108A2)	+= q54sj108a2.o
 obj-$(CONFIG_SENSORS_STPDDC60)	+= stpddc60.o
 obj-$(CONFIG_SENSORS_TDA38640)	+= tda38640.o
+obj-$(CONFIG_SENSORS_TPS25990)	+= tps25990.o
 obj-$(CONFIG_SENSORS_TPS40422)	+= tps40422.o
 obj-$(CONFIG_SENSORS_TPS53679)	+= tps53679.o
 obj-$(CONFIG_SENSORS_TPS546D24)	+= tps546d24.o
diff --git a/drivers/hwmon/pmbus/tps25990.c b/drivers/hwmon/pmbus/tps25990.c
new file mode 100644
index 000000000000..14290c4c71dd
--- /dev/null
+++ b/drivers/hwmon/pmbus/tps25990.c
@@ -0,0 +1,474 @@
+// SPDX-License-Identifier: GPL-2.0
+//
+// Copyright (c) 2024 BayLibre, SAS.
+// Author: Jerome Brunet <jbrunet@baylibre.com>
+
+#include <linux/debugfs.h>
+#include <linux/err.h>
+#include <linux/hwmon-sysfs.h>
+#include <linux/i2c.h>
+#include <linux/init.h>
+#include <linux/kernel.h>
+#include <linux/module.h>
+
+#include "pmbus.h"
+
+#define TPS25990_READ_VAUX		0xd0
+#define TPS25990_READ_VIN_MIN		0xd1
+#define TPS25990_READ_VIN_PEAK		0xd2
+#define TPS25990_READ_IIN_PEAK		0xd4
+#define TPS25990_READ_PIN_PEAK		0xd5
+#define TPS25990_READ_TEMP_AVG		0xd6
+#define TPS25990_READ_TEMP_PEAK		0xd7
+#define TPS25990_READ_VOUT_MIN		0xda
+#define TPS25990_READ_VIN_AVG		0xdc
+#define TPS25990_READ_VOUT_AVG		0xdd
+#define TPS25990_READ_IIN_AVG		0xde
+#define TPS25990_READ_PIN_AVG		0xdf
+#define TPS25990_VIREF			0xe0
+#define TPS25990_PK_MIN_AVG		0xea
+#define  PK_MIN_AVG_RST_PEAK		BIT(7)
+#define  PK_MIN_AVG_RST_AVG		BIT(6)
+#define  PK_MIN_AVG_RST_MIN		BIT(5)
+#define  PK_MIN_AVG_AVG_CNT		GENMASK(2, 0)
+#define TPS25990_MFR_WRITE_PROTECT	0xf8
+#define  TPS25990_UNLOCKED		BIT(7)
+
+#define TPS25990_8B_SHIFT		2
+#define TPS25990_VIN_OVF_NUM		525100
+#define TPS25990_VIN_OVF_DIV		10163
+#define TPS25990_VIN_OVF_OFF		155
+#define TPS25990_IIN_OCF_NUM		953800
+#define TPS25990_IIN_OCF_DIV		129278
+#define TPS25990_IIN_OCF_OFF		157
+
+#define TPS25990_DEFAULT_RIMON		910000
+
+static int tps25990_mfr_write_protect(struct i2c_client *client, bool protect)
+{
+	return pmbus_write_byte_data(client, -1, TPS25990_MFR_WRITE_PROTECT,
+				     protect ? 0x0 : 0xa2);
+}
+
+static int tps25990_mfr_write_protect_active(struct i2c_client *client)
+{
+	int ret = pmbus_read_byte_data(client, -1, TPS25990_MFR_WRITE_PROTECT);
+
+	if (ret < 0)
+		return ret;
+
+	return !(ret & TPS25990_UNLOCKED);
+}
+
+#if IS_ENABLED(CONFIG_DEBUG_FS)
+static int tps25990_write_protect_get(void *data, u64 *val)
+{
+	struct i2c_client *client = data;
+
+	return tps25990_mfr_write_protect_active(client);
+}
+
+static int tps25990_write_protect_set(void *data, u64 val)
+{
+	struct i2c_client *client = data;
+
+	if (val > 1)
+		return -EINVAL;
+
+	return tps25990_mfr_write_protect(client, val);
+}
+
+DEFINE_DEBUGFS_ATTRIBUTE(tps25990_write_protect_fops,
+			 tps25990_write_protect_get,
+			 tps25990_write_protect_set,
+			 "%llu\n");
+
+static int tps25990_init_debugfs(struct i2c_client *client)
+{
+	struct dentry *dir;
+
+	dir = pmbus_get_debugfs_dir(client);
+	if (!dir)
+		return -ENOENT;
+
+	debugfs_create_file("write_protect", 0644, dir,
+			    client, &tps25990_write_protect_fops);
+
+	return 0;
+}
+
+#else
+static inline int tps25990_init_debugfs(struct i2c_client *client)
+{
+	return 0;
+}
+#endif
+
+/*
+ * TPS25990 has history reset based on MIN/AVG/PEAK instead of per sensor type
+ * Emulate the behaviour a pmbus limit_attr would have for consistency
+ *  - Read: Do nothing and emit 0
+ *  - Write: Check the input is a number and reset
+ */
+static ssize_t tps25990_history_reset_show(struct device *dev,
+					   struct device_attribute *devattr,
+					   char *buf)
+{
+	return sysfs_emit(buf, "0\n");
+}
+
+static ssize_t tps25990_history_reset_store(struct device *dev,
+					    struct device_attribute *devattr,
+					    const char *buf, size_t count)
+{
+	struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
+	struct i2c_client *client = to_i2c_client(dev->parent);
+	s64 val;
+	int ret;
+
+	if (kstrtos64(buf, 10, &val) < 0)
+		return -EINVAL;
+
+	ret = pmbus_update_byte_data(client, 0, TPS25990_PK_MIN_AVG,
+				     BIT(attr->index), BIT(attr->index));
+	if (ret < 0)
+		return ret;
+
+	return count;
+}
+
+static SENSOR_DEVICE_ATTR_RW(highest_history_reset, tps25990_history_reset, 7);
+static SENSOR_DEVICE_ATTR_RW(average_history_reset, tps25990_history_reset, 6);
+static SENSOR_DEVICE_ATTR_RW(lowest_history_reset,  tps25990_history_reset, 5);
+
+static struct attribute *tps25990_attrs[] = {
+	&sensor_dev_attr_highest_history_reset.dev_attr.attr,
+	&sensor_dev_attr_average_history_reset.dev_attr.attr,
+	&sensor_dev_attr_lowest_history_reset.dev_attr.attr,
+	NULL,
+};
+
+ATTRIBUTE_GROUPS(tps25990);
+
+static int tps25990_get_addr(int reg)
+{
+	switch (reg) {
+	case PMBUS_SMBALERT_MASK:
+		/*
+		 * Note: PMBUS_SMBALERT_MASK is not implemented on this chip
+		 * Writing to this address raises CML errors.
+		 * Instead it provides ALERT_MASK which allows to set the mask
+		 * for each of the status registers, but not the specific bits
+		 * in them.
+		 * The default setup assert SMBA# if any bit is set in any of the
+		 * status registers the chip has. This is as close as we can get
+		 * to what pmbus_irq_setup() would set, sooo ... do nothing.
+		 */
+		return -ENXIO;
+	case PMBUS_IIN_OC_FAULT_LIMIT:
+		/*
+		 * VIREF directly sets the over-current limit at which the eFuse
+		 * will turn the FET off and trigger a fault. Expose it through
+		 * this generic property instead of a manufacturer specific one.
+		 */
+		return TPS25990_VIREF;
+	case PMBUS_VIRT_READ_VIN_MAX:
+		return TPS25990_READ_VIN_PEAK;
+	case PMBUS_VIRT_READ_VIN_MIN:
+		return TPS25990_READ_VIN_MIN;
+	case PMBUS_VIRT_READ_VIN_AVG:
+		return TPS25990_READ_VIN_AVG;
+	case PMBUS_VIRT_READ_VOUT_MIN:
+		return TPS25990_READ_VOUT_MIN;
+	case PMBUS_VIRT_READ_VOUT_AVG:
+		return TPS25990_READ_VOUT_AVG;
+	case PMBUS_VIRT_READ_IIN_AVG:
+		return TPS25990_READ_IIN_AVG;
+	case PMBUS_VIRT_READ_IIN_MAX:
+		return TPS25990_READ_IIN_PEAK;
+	case PMBUS_VIRT_READ_TEMP_AVG:
+		return TPS25990_READ_TEMP_AVG;
+	case PMBUS_VIRT_READ_TEMP_MAX:
+		return TPS25990_READ_TEMP_PEAK;
+	case PMBUS_VIRT_READ_PIN_AVG:
+		return TPS25990_READ_PIN_AVG;
+	case PMBUS_VIRT_READ_PIN_MAX:
+		return TPS25990_READ_PIN_PEAK;
+	case PMBUS_VIRT_READ_VMON:
+		return TPS25990_READ_VAUX;
+	case PMBUS_VIRT_SAMPLES:
+		return TPS25990_PK_MIN_AVG;
+	}
+
+	/* Let the register check do its job */
+	if (reg < PMBUS_VIRT_BASE)
+		return reg;
+
+	return -ENXIO;
+}
+
+/*
+ * Some registers use a different scale than the one registered with
+ * pmbus_driver_info. An extra conversion step is necessary to adapt
+ * the register value to the conversion on the sensor type
+ */
+static int tps25990_read_adapt_value(int reg, int val)
+{
+	switch (reg) {
+	case PMBUS_VIN_UV_WARN_LIMIT:
+	case PMBUS_VIN_UV_FAULT_LIMIT:
+	case PMBUS_VIN_OV_WARN_LIMIT:
+	case PMBUS_VOUT_UV_WARN_LIMIT:
+	case PMBUS_IIN_OC_WARN_LIMIT:
+	case PMBUS_OT_WARN_LIMIT:
+	case PMBUS_OT_FAULT_LIMIT:
+	case PMBUS_PIN_OP_WARN_LIMIT:
+	case PMBUS_POWER_GOOD_OFF:
+		/*
+		 * These registers provide an 8 bits value instead of a
+		 * 10bits one. Just shifting twice the register value is
+		 * enough to make the sensor type conversion work, even
+		 * if the datasheet provides different m, b and R for
+		 * those.
+		 */
+		val <<= TPS25990_8B_SHIFT;
+		break;
+
+	case PMBUS_VIN_OV_FAULT_LIMIT:
+		val = DIV_ROUND_CLOSEST(val * TPS25990_VIN_OVF_NUM, TPS25990_VIN_OVF_DIV);
+		val += TPS25990_VIN_OVF_OFF;
+		break;
+
+	case PMBUS_IIN_OC_FAULT_LIMIT:
+		val = DIV_ROUND_CLOSEST(val * TPS25990_IIN_OCF_NUM, TPS25990_IIN_OCF_DIV);
+		val += TPS25990_IIN_OCF_OFF;
+		break;
+
+	case PMBUS_VIRT_SAMPLES:
+		val = 1 << val;
+		break;
+	}
+
+	return val;
+}
+
+static int tps25990_read_word(struct i2c_client *client,
+			      int page, int phase, int reg)
+{
+	int ret, addr;
+
+	addr = tps25990_get_addr(reg);
+	if (addr < 0)
+		return addr;
+
+	switch (reg) {
+	case PMBUS_VIRT_SAMPLES:
+		ret = pmbus_read_byte_data(client, page, addr);
+		ret = FIELD_GET(PK_MIN_AVG_AVG_CNT, ret);
+		break;
+
+	case PMBUS_IIN_OC_FAULT_LIMIT:
+		ret = pmbus_read_byte_data(client, page, addr);
+		break;
+
+	default:
+		ret = pmbus_read_word_data(client, page, -1, addr);
+		break;
+	}
+
+	if (ret >= 0)
+		ret = tps25990_read_adapt_value(reg, ret);
+
+	return ret;
+}
+
+static int tps25990_write_adapt_value(int reg, int val)
+{
+	switch (reg) {
+	case PMBUS_VIN_UV_WARN_LIMIT:
+	case PMBUS_VIN_UV_FAULT_LIMIT:
+	case PMBUS_VIN_OV_WARN_LIMIT:
+	case PMBUS_VOUT_UV_WARN_LIMIT:
+	case PMBUS_IIN_OC_WARN_LIMIT:
+	case PMBUS_OT_WARN_LIMIT:
+	case PMBUS_OT_FAULT_LIMIT:
+	case PMBUS_PIN_OP_WARN_LIMIT:
+	case PMBUS_POWER_GOOD_OFF:
+		val >>= TPS25990_8B_SHIFT;
+		val = clamp(val, 0, 0xff);
+		break;
+
+	case PMBUS_VIN_OV_FAULT_LIMIT:
+		val -= TPS25990_VIN_OVF_OFF;
+		val = DIV_ROUND_CLOSEST(val * TPS25990_VIN_OVF_DIV, TPS25990_VIN_OVF_NUM);
+		val = clamp_val(val, 0, 0xf);
+		break;
+
+	case PMBUS_IIN_OC_FAULT_LIMIT:
+		val -= TPS25990_IIN_OCF_OFF;
+		val = DIV_ROUND_CLOSEST(val * TPS25990_IIN_OCF_DIV, TPS25990_IIN_OCF_NUM);
+		val = clamp_val(val, 0, 0x3f);
+		break;
+
+	case PMBUS_VIRT_SAMPLES:
+		val = clamp_val(val, 1, 1 << PK_MIN_AVG_AVG_CNT);
+		val = ilog2(val);
+		break;
+	}
+
+	return val;
+}
+
+static int tps25990_write_word(struct i2c_client *client,
+			       int page, int reg, u16 value)
+{
+	int addr, ret;
+
+	addr = tps25990_get_addr(reg);
+	if (addr < 0)
+		return addr;
+
+	value = tps25990_write_adapt_value(reg, value);
+
+	switch (reg) {
+	case PMBUS_VIRT_SAMPLES:
+		ret = pmbus_update_byte_data(client, page, addr,
+					     PK_MIN_AVG_AVG_CNT,
+					     FIELD_PREP(PK_MIN_AVG_AVG_CNT, value));
+		break;
+
+	case PMBUS_IIN_OC_FAULT_LIMIT:
+		ret = pmbus_write_byte_data(client, page, addr,
+					    value);
+		break;
+
+	default:
+		ret = pmbus_write_word_data(client, page, addr, value);
+		break;
+	}
+
+	return ret;
+}
+
+#if IS_ENABLED(CONFIG_SENSORS_TPS25990_REGULATOR)
+static const struct regulator_desc tps25990_reg_desc[] = {
+	PMBUS_REGULATOR_ONE("vout"),
+};
+#endif
+
+static const struct pmbus_driver_info tps25990_base_info = {
+	.pages = 1,
+	.format[PSC_VOLTAGE_IN] = direct,
+	.m[PSC_VOLTAGE_IN] = 5251,
+	.b[PSC_VOLTAGE_IN] = 0,
+	.R[PSC_VOLTAGE_IN] = -2,
+	.format[PSC_VOLTAGE_OUT] = direct,
+	.m[PSC_VOLTAGE_OUT] = 5251,
+	.b[PSC_VOLTAGE_OUT] = 0,
+	.R[PSC_VOLTAGE_OUT] = -2,
+	.format[PSC_TEMPERATURE] = direct,
+	.m[PSC_TEMPERATURE] = 140,
+	.b[PSC_TEMPERATURE] = 32100,
+	.R[PSC_TEMPERATURE] = -2,
+	/*
+	 * Current and Power measurement depends on the ohm value
+	 * of Rimon. m is multiplied by 1000 below to have an integer
+	 * and -3 is added to R to compensate.
+	 */
+	.format[PSC_CURRENT_IN] = direct,
+	.m[PSC_CURRENT_IN] = 9538,
+	.b[PSC_CURRENT_IN] = 0,
+	.R[PSC_CURRENT_IN] = -6,
+	.format[PSC_POWER] = direct,
+	.m[PSC_POWER] = 4901,
+	.b[PSC_POWER] = 0,
+	.R[PSC_POWER] = -7,
+	.func[0] = (PMBUS_HAVE_VIN |
+		    PMBUS_HAVE_VOUT |
+		    PMBUS_HAVE_VMON |
+		    PMBUS_HAVE_IIN |
+		    PMBUS_HAVE_PIN |
+		    PMBUS_HAVE_TEMP |
+		    PMBUS_HAVE_STATUS_VOUT |
+		    PMBUS_HAVE_STATUS_IOUT |
+		    PMBUS_HAVE_STATUS_INPUT |
+		    PMBUS_HAVE_STATUS_TEMP |
+		    PMBUS_HAVE_SAMPLES),
+	.read_word_data = tps25990_read_word,
+	.write_word_data = tps25990_write_word,
+	.groups = tps25990_groups,
+
+#if IS_ENABLED(CONFIG_SENSORS_TPS25990_REGULATOR)
+	.reg_desc = tps25990_reg_desc,
+	.num_regulators = ARRAY_SIZE(tps25990_reg_desc),
+#endif
+};
+
+static const struct i2c_device_id tps25990_i2c_id[] = {
+	{ "tps25990" },
+	{}
+};
+MODULE_DEVICE_TABLE(i2c, tps25990_i2c_id);
+
+static const struct of_device_id tps25990_of_match[] = {
+	{ .compatible = "ti,tps25990" },
+	{}
+};
+MODULE_DEVICE_TABLE(of, tps25990_of_match);
+
+static int tps25990_probe(struct i2c_client *client)
+{
+	struct device *dev = &client->dev;
+	struct pmbus_driver_info *info;
+	u32 rimon = TPS25990_DEFAULT_RIMON;
+	int ret;
+
+	ret = device_property_read_u32(dev, "ti,rimon-milli-ohms", &rimon);
+	if (ret == -EINVAL) {
+		dev_warn(dev,
+			 "using default rimon: current and power scale possibly wrong\n");
+	} else if (ret < 0) {
+		return dev_err_probe(dev, ret, "failed get rimon\n");
+	}
+
+	/*
+	 * TPS25990 may be stacked with several TPS25895, allowing a higher
+	 * current. The higher the allowed current is, the lower rimon
+	 * will be. How low it can realistically get is unknown.
+	 * To avoid problems with precision later on, rimon is provided in
+	 * milli Ohms. This is a precaution to keep a stable ABI.
+	 * At the moment, doing the calculation with rimon in milli Ohms
+	 * would overflow the s32 'm' in the direct conversion. Convert it
+	 * back to Ohms until greater precision is actually needed.
+	 */
+	rimon /= 1000;
+
+	info = devm_kmemdup(dev, &tps25990_base_info, sizeof(*info), GFP_KERNEL);
+	if (!info)
+		return -ENOMEM;
+
+	/* Adapt the current and power scale for each instance */
+	info->m[PSC_CURRENT_IN] *= rimon;
+	info->m[PSC_POWER] *= rimon;
+
+	ret = pmbus_do_probe(client, info);
+	if (ret < 0)
+		return ret;
+
+	return tps25990_init_debugfs(client);
+}
+
+static struct i2c_driver tps25990_driver = {
+	.driver = {
+		.name = "tps25990",
+		.of_match_table = tps25990_of_match,
+	},
+	.probe = tps25990_probe,
+	.id_table = tps25990_i2c_id,
+};
+module_i2c_driver(tps25990_driver);
+
+MODULE_AUTHOR("Jerome Brunet <jbrunet@baylibre.com>");
+MODULE_DESCRIPTION("PMBUS driver for TPS25990 eFuse");
+MODULE_LICENSE("GPL");
+MODULE_IMPORT_NS(PMBUS);

-- 
2.45.2
Re: [PATCH 3/3] hwmon: (pmbus/tps25990): add initial support
Posted by kernel test robot 2 months, 3 weeks ago
Hi Jerome,

kernel test robot noticed the following build errors:

[auto build test ERROR on d22bd451d5606411895ef55cb105277e4f4f6e54]

url:    https://github.com/intel-lab-lkp/linux/commits/Jerome-Brunet/dt-bindings-hwmon-pmbus-add-ti-tps25990-documentation/20240909-234152
base:   d22bd451d5606411895ef55cb105277e4f4f6e54
patch link:    https://lore.kernel.org/r/20240909-tps25990-v1-3-39b37e43e795%40baylibre.com
patch subject: [PATCH 3/3] hwmon: (pmbus/tps25990): add initial support
config: hexagon-allyesconfig (https://download.01.org/0day-ci/archive/20240910/202409101807.GjHADcvQ-lkp@intel.com/config)
compiler: clang version 20.0.0git (https://github.com/llvm/llvm-project 05f5a91d00b02f4369f46d076411c700755ae041)
reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20240910/202409101807.GjHADcvQ-lkp@intel.com/reproduce)

If you fix the issue in a separate patch/commit (i.e. not just a new version of
the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <lkp@intel.com>
| Closes: https://lore.kernel.org/oe-kbuild-all/202409101807.GjHADcvQ-lkp@intel.com/

All errors (new ones prefixed by >>):

   In file included from drivers/hwmon/pmbus/tps25990.c:9:
   In file included from include/linux/i2c.h:19:
   In file included from include/linux/regulator/consumer.h:35:
   In file included from include/linux/suspend.h:5:
   In file included from include/linux/swap.h:9:
   In file included from include/linux/memcontrol.h:13:
   In file included from include/linux/cgroup.h:25:
   In file included from include/linux/kernel_stat.h:8:
   In file included from include/linux/interrupt.h:11:
   In file included from include/linux/hardirq.h:11:
   In file included from ./arch/hexagon/include/generated/asm/hardirq.h:1:
   In file included from include/asm-generic/hardirq.h:17:
   In file included from include/linux/irq.h:20:
   In file included from include/linux/io.h:14:
   In file included from arch/hexagon/include/asm/io.h:328:
   include/asm-generic/io.h:548:31: warning: performing pointer arithmetic on a null pointer has undefined behavior [-Wnull-pointer-arithmetic]
     548 |         val = __raw_readb(PCI_IOBASE + addr);
         |                           ~~~~~~~~~~ ^
   include/asm-generic/io.h:561:61: warning: performing pointer arithmetic on a null pointer has undefined behavior [-Wnull-pointer-arithmetic]
     561 |         val = __le16_to_cpu((__le16 __force)__raw_readw(PCI_IOBASE + addr));
         |                                                         ~~~~~~~~~~ ^
   include/uapi/linux/byteorder/little_endian.h:37:51: note: expanded from macro '__le16_to_cpu'
      37 | #define __le16_to_cpu(x) ((__force __u16)(__le16)(x))
         |                                                   ^
   In file included from drivers/hwmon/pmbus/tps25990.c:9:
   In file included from include/linux/i2c.h:19:
   In file included from include/linux/regulator/consumer.h:35:
   In file included from include/linux/suspend.h:5:
   In file included from include/linux/swap.h:9:
   In file included from include/linux/memcontrol.h:13:
   In file included from include/linux/cgroup.h:25:
   In file included from include/linux/kernel_stat.h:8:
   In file included from include/linux/interrupt.h:11:
   In file included from include/linux/hardirq.h:11:
   In file included from ./arch/hexagon/include/generated/asm/hardirq.h:1:
   In file included from include/asm-generic/hardirq.h:17:
   In file included from include/linux/irq.h:20:
   In file included from include/linux/io.h:14:
   In file included from arch/hexagon/include/asm/io.h:328:
   include/asm-generic/io.h:574:61: warning: performing pointer arithmetic on a null pointer has undefined behavior [-Wnull-pointer-arithmetic]
     574 |         val = __le32_to_cpu((__le32 __force)__raw_readl(PCI_IOBASE + addr));
         |                                                         ~~~~~~~~~~ ^
   include/uapi/linux/byteorder/little_endian.h:35:51: note: expanded from macro '__le32_to_cpu'
      35 | #define __le32_to_cpu(x) ((__force __u32)(__le32)(x))
         |                                                   ^
   In file included from drivers/hwmon/pmbus/tps25990.c:9:
   In file included from include/linux/i2c.h:19:
   In file included from include/linux/regulator/consumer.h:35:
   In file included from include/linux/suspend.h:5:
   In file included from include/linux/swap.h:9:
   In file included from include/linux/memcontrol.h:13:
   In file included from include/linux/cgroup.h:25:
   In file included from include/linux/kernel_stat.h:8:
   In file included from include/linux/interrupt.h:11:
   In file included from include/linux/hardirq.h:11:
   In file included from ./arch/hexagon/include/generated/asm/hardirq.h:1:
   In file included from include/asm-generic/hardirq.h:17:
   In file included from include/linux/irq.h:20:
   In file included from include/linux/io.h:14:
   In file included from arch/hexagon/include/asm/io.h:328:
   include/asm-generic/io.h:585:33: warning: performing pointer arithmetic on a null pointer has undefined behavior [-Wnull-pointer-arithmetic]
     585 |         __raw_writeb(value, PCI_IOBASE + addr);
         |                             ~~~~~~~~~~ ^
   include/asm-generic/io.h:595:59: warning: performing pointer arithmetic on a null pointer has undefined behavior [-Wnull-pointer-arithmetic]
     595 |         __raw_writew((u16 __force)cpu_to_le16(value), PCI_IOBASE + addr);
         |                                                       ~~~~~~~~~~ ^
   include/asm-generic/io.h:605:59: warning: performing pointer arithmetic on a null pointer has undefined behavior [-Wnull-pointer-arithmetic]
     605 |         __raw_writel((u32 __force)cpu_to_le32(value), PCI_IOBASE + addr);
         |                                                       ~~~~~~~~~~ ^
   In file included from drivers/hwmon/pmbus/tps25990.c:9:
   In file included from include/linux/i2c.h:19:
   In file included from include/linux/regulator/consumer.h:35:
   In file included from include/linux/suspend.h:5:
   In file included from include/linux/swap.h:9:
   In file included from include/linux/memcontrol.h:21:
   In file included from include/linux/mm.h:2228:
   include/linux/vmstat.h:514:36: warning: arithmetic between different enumeration types ('enum node_stat_item' and 'enum lru_list') [-Wenum-enum-conversion]
     514 |         return node_stat_name(NR_LRU_BASE + lru) + 3; // skip "nr_"
         |                               ~~~~~~~~~~~ ^ ~~~
>> drivers/hwmon/pmbus/tps25990.c:267:9: error: call to undeclared function 'FIELD_GET'; ISO C99 and later do not support implicit function declarations [-Wimplicit-function-declaration]
     267 |                 ret = FIELD_GET(PK_MIN_AVG_AVG_CNT, ret);
         |                       ^
>> drivers/hwmon/pmbus/tps25990.c:337:11: error: call to undeclared function 'FIELD_PREP'; ISO C99 and later do not support implicit function declarations [-Wimplicit-function-declaration]
     337 |                                              FIELD_PREP(PK_MIN_AVG_AVG_CNT, value));
         |                                              ^
   7 warnings and 2 errors generated.


vim +/FIELD_GET +267 drivers/hwmon/pmbus/tps25990.c

   254	
   255	static int tps25990_read_word(struct i2c_client *client,
   256				      int page, int phase, int reg)
   257	{
   258		int ret, addr;
   259	
   260		addr = tps25990_get_addr(reg);
   261		if (addr < 0)
   262			return addr;
   263	
   264		switch (reg) {
   265		case PMBUS_VIRT_SAMPLES:
   266			ret = pmbus_read_byte_data(client, page, addr);
 > 267			ret = FIELD_GET(PK_MIN_AVG_AVG_CNT, ret);
   268			break;
   269	
   270		case PMBUS_IIN_OC_FAULT_LIMIT:
   271			ret = pmbus_read_byte_data(client, page, addr);
   272			break;
   273	
   274		default:
   275			ret = pmbus_read_word_data(client, page, -1, addr);
   276			break;
   277		}
   278	
   279		if (ret >= 0)
   280			ret = tps25990_read_adapt_value(reg, ret);
   281	
   282		return ret;
   283	}
   284	
   285	static int tps25990_write_adapt_value(int reg, int val)
   286	{
   287		switch (reg) {
   288		case PMBUS_VIN_UV_WARN_LIMIT:
   289		case PMBUS_VIN_UV_FAULT_LIMIT:
   290		case PMBUS_VIN_OV_WARN_LIMIT:
   291		case PMBUS_VOUT_UV_WARN_LIMIT:
   292		case PMBUS_IIN_OC_WARN_LIMIT:
   293		case PMBUS_OT_WARN_LIMIT:
   294		case PMBUS_OT_FAULT_LIMIT:
   295		case PMBUS_PIN_OP_WARN_LIMIT:
   296		case PMBUS_POWER_GOOD_OFF:
   297			val >>= TPS25990_8B_SHIFT;
   298			val = clamp(val, 0, 0xff);
   299			break;
   300	
   301		case PMBUS_VIN_OV_FAULT_LIMIT:
   302			val -= TPS25990_VIN_OVF_OFF;
   303			val = DIV_ROUND_CLOSEST(val * TPS25990_VIN_OVF_DIV, TPS25990_VIN_OVF_NUM);
   304			val = clamp_val(val, 0, 0xf);
   305			break;
   306	
   307		case PMBUS_IIN_OC_FAULT_LIMIT:
   308			val -= TPS25990_IIN_OCF_OFF;
   309			val = DIV_ROUND_CLOSEST(val * TPS25990_IIN_OCF_DIV, TPS25990_IIN_OCF_NUM);
   310			val = clamp_val(val, 0, 0x3f);
   311			break;
   312	
   313		case PMBUS_VIRT_SAMPLES:
   314			val = clamp_val(val, 1, 1 << PK_MIN_AVG_AVG_CNT);
   315			val = ilog2(val);
   316			break;
   317		}
   318	
   319		return val;
   320	}
   321	
   322	static int tps25990_write_word(struct i2c_client *client,
   323				       int page, int reg, u16 value)
   324	{
   325		int addr, ret;
   326	
   327		addr = tps25990_get_addr(reg);
   328		if (addr < 0)
   329			return addr;
   330	
   331		value = tps25990_write_adapt_value(reg, value);
   332	
   333		switch (reg) {
   334		case PMBUS_VIRT_SAMPLES:
   335			ret = pmbus_update_byte_data(client, page, addr,
   336						     PK_MIN_AVG_AVG_CNT,
 > 337						     FIELD_PREP(PK_MIN_AVG_AVG_CNT, value));
   338			break;
   339	
   340		case PMBUS_IIN_OC_FAULT_LIMIT:
   341			ret = pmbus_write_byte_data(client, page, addr,
   342						    value);
   343			break;
   344	
   345		default:
   346			ret = pmbus_write_word_data(client, page, addr, value);
   347			break;
   348		}
   349	
   350		return ret;
   351	}
   352	

-- 
0-DAY CI Kernel Test Service
https://github.com/intel/lkp-tests/wiki
Re: [PATCH 3/3] hwmon: (pmbus/tps25990): add initial support
Posted by kernel test robot 2 months, 3 weeks ago
Hi Jerome,

kernel test robot noticed the following build errors:

[auto build test ERROR on d22bd451d5606411895ef55cb105277e4f4f6e54]

url:    https://github.com/intel-lab-lkp/linux/commits/Jerome-Brunet/dt-bindings-hwmon-pmbus-add-ti-tps25990-documentation/20240909-234152
base:   d22bd451d5606411895ef55cb105277e4f4f6e54
patch link:    https://lore.kernel.org/r/20240909-tps25990-v1-3-39b37e43e795%40baylibre.com
patch subject: [PATCH 3/3] hwmon: (pmbus/tps25990): add initial support
config: m68k-allmodconfig (https://download.01.org/0day-ci/archive/20240910/202409101522.ZoP360wM-lkp@intel.com/config)
compiler: m68k-linux-gcc (GCC) 14.1.0
reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20240910/202409101522.ZoP360wM-lkp@intel.com/reproduce)

If you fix the issue in a separate patch/commit (i.e. not just a new version of
the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <lkp@intel.com>
| Closes: https://lore.kernel.org/oe-kbuild-all/202409101522.ZoP360wM-lkp@intel.com/

All errors (new ones prefixed by >>):

   drivers/hwmon/pmbus/tps25990.c: In function 'tps25990_read_word':
>> drivers/hwmon/pmbus/tps25990.c:267:23: error: implicit declaration of function 'FIELD_GET' [-Wimplicit-function-declaration]
     267 |                 ret = FIELD_GET(PK_MIN_AVG_AVG_CNT, ret);
         |                       ^~~~~~~~~
   drivers/hwmon/pmbus/tps25990.c: In function 'tps25990_write_word':
>> drivers/hwmon/pmbus/tps25990.c:337:46: error: implicit declaration of function 'FIELD_PREP' [-Wimplicit-function-declaration]
     337 |                                              FIELD_PREP(PK_MIN_AVG_AVG_CNT, value));
         |                                              ^~~~~~~~~~


vim +/FIELD_GET +267 drivers/hwmon/pmbus/tps25990.c

   254	
   255	static int tps25990_read_word(struct i2c_client *client,
   256				      int page, int phase, int reg)
   257	{
   258		int ret, addr;
   259	
   260		addr = tps25990_get_addr(reg);
   261		if (addr < 0)
   262			return addr;
   263	
   264		switch (reg) {
   265		case PMBUS_VIRT_SAMPLES:
   266			ret = pmbus_read_byte_data(client, page, addr);
 > 267			ret = FIELD_GET(PK_MIN_AVG_AVG_CNT, ret);
   268			break;
   269	
   270		case PMBUS_IIN_OC_FAULT_LIMIT:
   271			ret = pmbus_read_byte_data(client, page, addr);
   272			break;
   273	
   274		default:
   275			ret = pmbus_read_word_data(client, page, -1, addr);
   276			break;
   277		}
   278	
   279		if (ret >= 0)
   280			ret = tps25990_read_adapt_value(reg, ret);
   281	
   282		return ret;
   283	}
   284	
   285	static int tps25990_write_adapt_value(int reg, int val)
   286	{
   287		switch (reg) {
   288		case PMBUS_VIN_UV_WARN_LIMIT:
   289		case PMBUS_VIN_UV_FAULT_LIMIT:
   290		case PMBUS_VIN_OV_WARN_LIMIT:
   291		case PMBUS_VOUT_UV_WARN_LIMIT:
   292		case PMBUS_IIN_OC_WARN_LIMIT:
   293		case PMBUS_OT_WARN_LIMIT:
   294		case PMBUS_OT_FAULT_LIMIT:
   295		case PMBUS_PIN_OP_WARN_LIMIT:
   296		case PMBUS_POWER_GOOD_OFF:
   297			val >>= TPS25990_8B_SHIFT;
   298			val = clamp(val, 0, 0xff);
   299			break;
   300	
   301		case PMBUS_VIN_OV_FAULT_LIMIT:
   302			val -= TPS25990_VIN_OVF_OFF;
   303			val = DIV_ROUND_CLOSEST(val * TPS25990_VIN_OVF_DIV, TPS25990_VIN_OVF_NUM);
   304			val = clamp_val(val, 0, 0xf);
   305			break;
   306	
   307		case PMBUS_IIN_OC_FAULT_LIMIT:
   308			val -= TPS25990_IIN_OCF_OFF;
   309			val = DIV_ROUND_CLOSEST(val * TPS25990_IIN_OCF_DIV, TPS25990_IIN_OCF_NUM);
   310			val = clamp_val(val, 0, 0x3f);
   311			break;
   312	
   313		case PMBUS_VIRT_SAMPLES:
   314			val = clamp_val(val, 1, 1 << PK_MIN_AVG_AVG_CNT);
   315			val = ilog2(val);
   316			break;
   317		}
   318	
   319		return val;
   320	}
   321	
   322	static int tps25990_write_word(struct i2c_client *client,
   323				       int page, int reg, u16 value)
   324	{
   325		int addr, ret;
   326	
   327		addr = tps25990_get_addr(reg);
   328		if (addr < 0)
   329			return addr;
   330	
   331		value = tps25990_write_adapt_value(reg, value);
   332	
   333		switch (reg) {
   334		case PMBUS_VIRT_SAMPLES:
   335			ret = pmbus_update_byte_data(client, page, addr,
   336						     PK_MIN_AVG_AVG_CNT,
 > 337						     FIELD_PREP(PK_MIN_AVG_AVG_CNT, value));
   338			break;
   339	
   340		case PMBUS_IIN_OC_FAULT_LIMIT:
   341			ret = pmbus_write_byte_data(client, page, addr,
   342						    value);
   343			break;
   344	
   345		default:
   346			ret = pmbus_write_word_data(client, page, addr, value);
   347			break;
   348		}
   349	
   350		return ret;
   351	}
   352	

-- 
0-DAY CI Kernel Test Service
https://github.com/intel/lkp-tests/wiki
Re: [PATCH 3/3] hwmon: (pmbus/tps25990): add initial support
Posted by Guenter Roeck 2 months, 3 weeks ago
On 9/9/24 08:39, Jerome Brunet wrote:
> Add initial support for the Texas Instruments TPS25990 eFuse.
> This adds the basic PMBUS telemetry support for the device.
> 
> Signed-off-by: Jerome Brunet <jbrunet@baylibre.com>
> ---
>   Documentation/hwmon/tps25990.rst | 141 ++++++++++++
>   drivers/hwmon/pmbus/Kconfig      |  17 ++
>   drivers/hwmon/pmbus/Makefile     |   1 +
>   drivers/hwmon/pmbus/tps25990.c   | 474 +++++++++++++++++++++++++++++++++++++++
>   4 files changed, 633 insertions(+)
> 
> diff --git a/Documentation/hwmon/tps25990.rst b/Documentation/hwmon/tps25990.rst
> new file mode 100644
> index 000000000000..7b3ef724008a
> --- /dev/null
> +++ b/Documentation/hwmon/tps25990.rst
> @@ -0,0 +1,141 @@
> +.. SPDX-License-Identifier: GPL-2.0
> +
> +Kernel driver tps25990
> +======================
> +
> +Supported chips:
> +
> +  * TI TPS25990
> +
> +    Prefix: 'tps25990'
> +
> +  * Datasheet
> +
> +    Publicly available at Texas Instruments website: https://www.ti.com/lit/gpn/tps25990
> +
> +Author:
> +
> +	Jerome Brunet <jbrunet@baylibre.com>
> +
> +Description
> +-----------
> +
> +This driver implements support for TI TPS25990 eFuse.
> +This is an integrated, high-current circuit protection and power
> +management device with PMBUS interface
> +
> +Device compliant with:
> +
> +- PMBus rev 1.3 interface.
> +
> +Device supports direct format for reading input voltages,
> +output voltage, input current, input power and temperature.
> +
> +The driver exports the following attributes via the 'sysfs' files
> +for input current:
> +
> +**curr1_average**
> +
> +**curr1_crit**
> +
> +**curr1_crit_alarm**
> +
> +**curr1_highest**
> +
> +**curr1_input**
> +
> +**curr1_label**
> +
> +**curr1_max**
> +
> +**curr1_max_alarm**
> +
> +The driver provides the following attributes for main input voltage:
> +
> +**in1_average**
> +
> +**in1_crit**
> +
> +**in1_crit_alarm**
> +
> +**in1_highest**
> +
> +**in1_input**
> +
> +**in1_label**
> +
> +**in1_lcrit**
> +
> +**in1_lcrit_alarm**
> +
> +**in1_lowest**
> +
> +**in1_max**
> +
> +**in1_max_alarm**
> +
> +**in1_min**
> +
> +**in1_min_alarm**
> +
> +The driver provides the following attributes for auxiliary input voltage:
> +
> +**in2_input**
> +
> +**in2_label**
> +
> +The driver provides the following attributes for output voltage:
> +
> +**in3_average**
> +
> +**in3_good_off**


As mentioned in the other patch, those mnon-standard attributes are not acceptable.

> +
> +**in3_input**
> +
> +**in3_label**
> +
> +**in3_lowest**
> +
> +**in3_min**
> +
> +**in3_min_alarm**
> +
> +The driver provides the following attributes for input power:
> +
> +**power1_alarm**
> +
> +**power1_average**
> +
> +**power1_input**
> +
> +**power1_input_highest**
> +
> +**power1_label**
> +
> +**power1_max**
> +
> +The driver provides the following attributes for temperature:
> +
> +**temp1_average**
> +
> +**temp1_crit**
> +
> +**temp1_crit_alarm**
> +
> +**temp1_highest**
> +
> +**temp1_input**
> +
> +**temp1_max**
> +
> +**temp1_max_alarm**
> +
> +The driver provides the following attributes for history:
> +
> +**samples**
> +
> +**average_history_reset**
> +
> +**highest_history_reset**
> +
> +**lowest_history_reset**
> diff --git a/drivers/hwmon/pmbus/Kconfig b/drivers/hwmon/pmbus/Kconfig
> index a4f02cad92fd..3559864e232d 100644
> --- a/drivers/hwmon/pmbus/Kconfig
> +++ b/drivers/hwmon/pmbus/Kconfig
> @@ -510,6 +510,23 @@ config SENSORS_TDA38640_REGULATOR
>   	  If you say yes here you get regulator support for Infineon
>   	  TDA38640 as regulator.
>   
> +config SENSORS_TPS25990
> +	tristate "TI TPS25990"
> +	help
> +	  If you say yes here you get hardware monitoring support for TI
> +	  TPS25990.
> +
> +	  This driver can also be built as a module. If so, the module will
> +	  be called tps25990.
> +
> +config SENSORS_TPS25990_REGULATOR
> +	bool "Regulator support for TPS25990 and compatibles"
> +	depends on SENSORS_TPS25990 && REGULATOR
> +	default SENSORS_TPS2599
> +	help
> +	  If you say yes here you get regulator support for Texas Instruments
> +	  TPS25990.
> +
>   config SENSORS_TPS40422
>   	tristate "TI TPS40422"
>   	help
> diff --git a/drivers/hwmon/pmbus/Makefile b/drivers/hwmon/pmbus/Makefile
> index d00bcc758b97..3d3183f8d2a7 100644
> --- a/drivers/hwmon/pmbus/Makefile
> +++ b/drivers/hwmon/pmbus/Makefile
> @@ -51,6 +51,7 @@ obj-$(CONFIG_SENSORS_PXE1610)	+= pxe1610.o
>   obj-$(CONFIG_SENSORS_Q54SJ108A2)	+= q54sj108a2.o
>   obj-$(CONFIG_SENSORS_STPDDC60)	+= stpddc60.o
>   obj-$(CONFIG_SENSORS_TDA38640)	+= tda38640.o
> +obj-$(CONFIG_SENSORS_TPS25990)	+= tps25990.o
>   obj-$(CONFIG_SENSORS_TPS40422)	+= tps40422.o
>   obj-$(CONFIG_SENSORS_TPS53679)	+= tps53679.o
>   obj-$(CONFIG_SENSORS_TPS546D24)	+= tps546d24.o
> diff --git a/drivers/hwmon/pmbus/tps25990.c b/drivers/hwmon/pmbus/tps25990.c
> new file mode 100644
> index 000000000000..14290c4c71dd
> --- /dev/null
> +++ b/drivers/hwmon/pmbus/tps25990.c
> @@ -0,0 +1,474 @@
> +// SPDX-License-Identifier: GPL-2.0
> +//
> +// Copyright (c) 2024 BayLibre, SAS.
> +// Author: Jerome Brunet <jbrunet@baylibre.com>
> +
> +#include <linux/debugfs.h>
> +#include <linux/err.h>
> +#include <linux/hwmon-sysfs.h>
> +#include <linux/i2c.h>
> +#include <linux/init.h>
> +#include <linux/kernel.h>
> +#include <linux/module.h>
> +
> +#include "pmbus.h"
> +
> +#define TPS25990_READ_VAUX		0xd0
> +#define TPS25990_READ_VIN_MIN		0xd1
> +#define TPS25990_READ_VIN_PEAK		0xd2
> +#define TPS25990_READ_IIN_PEAK		0xd4
> +#define TPS25990_READ_PIN_PEAK		0xd5
> +#define TPS25990_READ_TEMP_AVG		0xd6
> +#define TPS25990_READ_TEMP_PEAK		0xd7
> +#define TPS25990_READ_VOUT_MIN		0xda
> +#define TPS25990_READ_VIN_AVG		0xdc
> +#define TPS25990_READ_VOUT_AVG		0xdd
> +#define TPS25990_READ_IIN_AVG		0xde
> +#define TPS25990_READ_PIN_AVG		0xdf
> +#define TPS25990_VIREF			0xe0
> +#define TPS25990_PK_MIN_AVG		0xea
> +#define  PK_MIN_AVG_RST_PEAK		BIT(7)
> +#define  PK_MIN_AVG_RST_AVG		BIT(6)
> +#define  PK_MIN_AVG_RST_MIN		BIT(5)
> +#define  PK_MIN_AVG_AVG_CNT		GENMASK(2, 0)
> +#define TPS25990_MFR_WRITE_PROTECT	0xf8
> +#define  TPS25990_UNLOCKED		BIT(7)
> +
> +#define TPS25990_8B_SHIFT		2
> +#define TPS25990_VIN_OVF_NUM		525100
> +#define TPS25990_VIN_OVF_DIV		10163
> +#define TPS25990_VIN_OVF_OFF		155
> +#define TPS25990_IIN_OCF_NUM		953800
> +#define TPS25990_IIN_OCF_DIV		129278
> +#define TPS25990_IIN_OCF_OFF		157
> +
> +#define TPS25990_DEFAULT_RIMON		910000

If this is the default, why would it make sense to even specify the
property in milli-ohm ? I mean, sure, the devices can be stacked,
but it seems unrealistic to assume that there are hundreds or even
dozens of devices in parallel.
> +
> +static int tps25990_mfr_write_protect(struct i2c_client *client, bool protect)
> +{
> +	return pmbus_write_byte_data(client, -1, TPS25990_MFR_WRITE_PROTECT,
> +				     protect ? 0x0 : 0xa2);
> +}
> +
> +static int tps25990_mfr_write_protect_active(struct i2c_client *client)
> +{
> +	int ret = pmbus_read_byte_data(client, -1, TPS25990_MFR_WRITE_PROTECT);
> +
> +	if (ret < 0)
> +		return ret;
> +
> +	return !(ret & TPS25990_UNLOCKED);
> +}
> +
> +#if IS_ENABLED(CONFIG_DEBUG_FS)
> +static int tps25990_write_protect_get(void *data, u64 *val)
> +{
> +	struct i2c_client *client = data;
> +
> +	return tps25990_mfr_write_protect_active(client);
> +}
> +
> +static int tps25990_write_protect_set(void *data, u64 val)
> +{
> +	struct i2c_client *client = data;
> +
> +	if (val > 1)
> +		return -EINVAL;
> +
> +	return tps25990_mfr_write_protect(client, val);
> +}
> +
> +DEFINE_DEBUGFS_ATTRIBUTE(tps25990_write_protect_fops,
> +			 tps25990_write_protect_get,
> +			 tps25990_write_protect_set,
> +			 "%llu\n");
> +
> +static int tps25990_init_debugfs(struct i2c_client *client)
> +{
> +	struct dentry *dir;
> +
> +	dir = pmbus_get_debugfs_dir(client);
> +	if (!dir)
> +		return -ENOENT;
> +
> +	debugfs_create_file("write_protect", 0644, dir,
> +			    client, &tps25990_write_protect_fops);
> +
> +	return 0;
> +}
> +
> +#else
> +static inline int tps25990_init_debugfs(struct i2c_client *client)
> +{
> +	return 0;
> +}
> +#endif
> +

In general it is extremely undesirable to overwrite write protection.
Many chips support such attributes. If write protection is enabled,
it means that the board vendor does not want to have them changed.
Granted, that can be overwritten with direct i2c commands, but that
is what it should be. Anyone who really wants to disable write protection
should have to dig deeper than just writing into a debugfs or sysfs attribute.
Otherwise the protection becomes worthless. If this is, for example, needed
for production to write initial settings, the production scripts should
disable (or enable) write protection by writing directly into command
registers.

> +/*
> + * TPS25990 has history reset based on MIN/AVG/PEAK instead of per sensor type
> + * Emulate the behaviour a pmbus limit_attr would have for consistency
> + *  - Read: Do nothing and emit 0
> + *  - Write: Check the input is a number and reset
> + */
> +static ssize_t tps25990_history_reset_show(struct device *dev,
> +					   struct device_attribute *devattr,
> +					   char *buf)
> +{
> +	return sysfs_emit(buf, "0\n");
> +}
> +
> +static ssize_t tps25990_history_reset_store(struct device *dev,
> +					    struct device_attribute *devattr,
> +					    const char *buf, size_t count)
> +{
> +	struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
> +	struct i2c_client *client = to_i2c_client(dev->parent);
> +	s64 val;
> +	int ret;
> +
> +	if (kstrtos64(buf, 10, &val) < 0)
> +		return -EINVAL;
> +
> +	ret = pmbus_update_byte_data(client, 0, TPS25990_PK_MIN_AVG,
> +				     BIT(attr->index), BIT(attr->index));
> +	if (ret < 0)
> +		return ret;
> +
> +	return count;
> +}
> +
> +static SENSOR_DEVICE_ATTR_RW(highest_history_reset, tps25990_history_reset, 7);
> +static SENSOR_DEVICE_ATTR_RW(average_history_reset, tps25990_history_reset, 6);
> +static SENSOR_DEVICE_ATTR_RW(lowest_history_reset,  tps25990_history_reset, 5);

That is not a unique problem, and not a reason to introduce non-standard attributes.
Just attach the attribute to the first channel and document that it resets all
channels.

> +
> +static struct attribute *tps25990_attrs[] = {
> +	&sensor_dev_attr_highest_history_reset.dev_attr.attr,
> +	&sensor_dev_attr_average_history_reset.dev_attr.attr,
> +	&sensor_dev_attr_lowest_history_reset.dev_attr.attr,
> +	NULL,
> +};
> +
> +ATTRIBUTE_GROUPS(tps25990);
> +
> +static int tps25990_get_addr(int reg)
> +{
> +	switch (reg) {
> +	case PMBUS_SMBALERT_MASK:
> +		/*
> +		 * Note: PMBUS_SMBALERT_MASK is not implemented on this chip
> +		 * Writing to this address raises CML errors.
> +		 * Instead it provides ALERT_MASK which allows to set the mask
> +		 * for each of the status registers, but not the specific bits
> +		 * in them.
> +		 * The default setup assert SMBA# if any bit is set in any of the
> +		 * status registers the chip has. This is as close as we can get
> +		 * to what pmbus_irq_setup() would set, sooo ... do nothing.
> +		 */
> +		return -ENXIO;

Many chips have that problem. The core code ignores errors, and attempts to write
the command are limited to initialization. This is not a reason to overwrite
the command like this. If this does cause a real a problem wit hthe chip (other
than setting CML errors, which many chips not supporting the command do),
we should define a flag in include/linux/pmbus.h and explain its need.

> +	case PMBUS_IIN_OC_FAULT_LIMIT:
> +		/*
> +		 * VIREF directly sets the over-current limit at which the eFuse
> +		 * will turn the FET off and trigger a fault. Expose it through
> +		 * this generic property instead of a manufacturer specific one.
> +		 */
> +		return TPS25990_VIREF;

I don't see the value in this override. See below.

> +	case PMBUS_VIRT_READ_VIN_MAX:
> +		return TPS25990_READ_VIN_PEAK;
> +	case PMBUS_VIRT_READ_VIN_MIN:
> +		return TPS25990_READ_VIN_MIN;
> +	case PMBUS_VIRT_READ_VIN_AVG:
> +		return TPS25990_READ_VIN_AVG;
> +	case PMBUS_VIRT_READ_VOUT_MIN:
> +		return TPS25990_READ_VOUT_MIN;
> +	case PMBUS_VIRT_READ_VOUT_AVG:
> +		return TPS25990_READ_VOUT_AVG;
> +	case PMBUS_VIRT_READ_IIN_AVG:
> +		return TPS25990_READ_IIN_AVG;
> +	case PMBUS_VIRT_READ_IIN_MAX:
> +		return TPS25990_READ_IIN_PEAK;
> +	case PMBUS_VIRT_READ_TEMP_AVG:
> +		return TPS25990_READ_TEMP_AVG;
> +	case PMBUS_VIRT_READ_TEMP_MAX:
> +		return TPS25990_READ_TEMP_PEAK;
> +	case PMBUS_VIRT_READ_PIN_AVG:
> +		return TPS25990_READ_PIN_AVG;
> +	case PMBUS_VIRT_READ_PIN_MAX:
> +		return TPS25990_READ_PIN_PEAK;
> +	case PMBUS_VIRT_READ_VMON:
> +		return TPS25990_READ_VAUX;
> +	case PMBUS_VIRT_SAMPLES:
> +		return TPS25990_PK_MIN_AVG;

default: missing.

> +	}
> +
> +	/* Let the register check do its job */
> +	if (reg < PMBUS_VIRT_BASE)
> +		return reg;
> +
> +	return -ENXIO;

This needs to be more specific: The code should only return -ENXIO
if auto-detection can not handle the case. "Return -ENXIO for all
unsupported virtual registers" is unexpected. That situation should
be handled by the PMBus core.

> +}
> +
> +/*
> + * Some registers use a different scale than the one registered with
> + * pmbus_driver_info. An extra conversion step is necessary to adapt
> + * the register value to the conversion on the sensor type
> + */
> +static int tps25990_read_adapt_value(int reg, int val)
> +{
> +	switch (reg) {
> +	case PMBUS_VIN_UV_WARN_LIMIT:
> +	case PMBUS_VIN_UV_FAULT_LIMIT:
> +	case PMBUS_VIN_OV_WARN_LIMIT:
> +	case PMBUS_VOUT_UV_WARN_LIMIT:
> +	case PMBUS_IIN_OC_WARN_LIMIT:
> +	case PMBUS_OT_WARN_LIMIT:
> +	case PMBUS_OT_FAULT_LIMIT:
> +	case PMBUS_PIN_OP_WARN_LIMIT:
> +	case PMBUS_POWER_GOOD_OFF:
> +		/*
> +		 * These registers provide an 8 bits value instead of a
> +		 * 10bits one. Just shifting twice the register value is
> +		 * enough to make the sensor type conversion work, even
> +		 * if the datasheet provides different m, b and R for
> +		 * those.
> +		 */
> +		val <<= TPS25990_8B_SHIFT;
> +		break;
> +
> +	case PMBUS_VIN_OV_FAULT_LIMIT:
> +		val = DIV_ROUND_CLOSEST(val * TPS25990_VIN_OVF_NUM, TPS25990_VIN_OVF_DIV);
> +		val += TPS25990_VIN_OVF_OFF;
> +		break;
> +
> +	case PMBUS_IIN_OC_FAULT_LIMIT:
> +		val = DIV_ROUND_CLOSEST(val * TPS25990_IIN_OCF_NUM, TPS25990_IIN_OCF_DIV);
> +		val += TPS25990_IIN_OCF_OFF;
> +		break;
> +
> +	case PMBUS_VIRT_SAMPLES:
> +		val = 1 << val;
> +		break;

default case missing.

> +	}
> +
> +	return val;
> +}
> +
> +static int tps25990_read_word(struct i2c_client *client,
> +			      int page, int phase, int reg)
> +{
> +	int ret, addr;
> +
> +	addr = tps25990_get_addr(reg);
> +	if (addr < 0)
> +		return addr;
> +
> +	switch (reg) {
> +	case PMBUS_VIRT_SAMPLES:
> +		ret = pmbus_read_byte_data(client, page, addr);

Mapping the register name in tps25990_get_addr() is unnecessary
and misleading. It is well known that TPS25990_PK_MIN_AVG is to be
used. Do it here.

> +		ret = FIELD_GET(PK_MIN_AVG_AVG_CNT, ret);
> +		break;
> +
> +	case PMBUS_IIN_OC_FAULT_LIMIT:
> +		ret = pmbus_read_byte_data(client, page, addr);
> +		break;
> +
Same here.

> +	default:
> +		ret = pmbus_read_word_data(client, page, -1, addr);

This is unexpected for registers not handled locally. Expectation is
that -ENODATA is returned for those, to be handled in the core.

> +		break;
> +	}
> +
> +	if (ret >= 0)
> +		ret = tps25990_read_adapt_value(reg, ret);
> +
> +	return ret;
> +}
> +
> +static int tps25990_write_adapt_value(int reg, int val)
> +{
> +	switch (reg) {
> +	case PMBUS_VIN_UV_WARN_LIMIT:
> +	case PMBUS_VIN_UV_FAULT_LIMIT:
> +	case PMBUS_VIN_OV_WARN_LIMIT:
> +	case PMBUS_VOUT_UV_WARN_LIMIT:
> +	case PMBUS_IIN_OC_WARN_LIMIT:
> +	case PMBUS_OT_WARN_LIMIT:
> +	case PMBUS_OT_FAULT_LIMIT:
> +	case PMBUS_PIN_OP_WARN_LIMIT:
> +	case PMBUS_POWER_GOOD_OFF:
> +		val >>= TPS25990_8B_SHIFT;
> +		val = clamp(val, 0, 0xff);

Why clamp() here but clamp_val() elsewhere ?

> +		break;
> +
> +	case PMBUS_VIN_OV_FAULT_LIMIT:
> +		val -= TPS25990_VIN_OVF_OFF;
> +		val = DIV_ROUND_CLOSEST(val * TPS25990_VIN_OVF_DIV, TPS25990_VIN_OVF_NUM);
> +		val = clamp_val(val, 0, 0xf);
> +		break;
> +
> +	case PMBUS_IIN_OC_FAULT_LIMIT:
> +		val -= TPS25990_IIN_OCF_OFF;
> +		val = DIV_ROUND_CLOSEST(val * TPS25990_IIN_OCF_DIV, TPS25990_IIN_OCF_NUM);
> +		val = clamp_val(val, 0, 0x3f);
> +		break;
> +
> +	case PMBUS_VIRT_SAMPLES:
> +		val = clamp_val(val, 1, 1 << PK_MIN_AVG_AVG_CNT);
> +		val = ilog2(val);
> +		break;

default: missing.

> +	}
> +
> +	return val;
> +}
> +
> +static int tps25990_write_word(struct i2c_client *client,
> +			       int page, int reg, u16 value)
> +{
> +	int addr, ret;
> +
> +	addr = tps25990_get_addr(reg);
> +	if (addr < 0)
> +		return addr;
> +
> +	value = tps25990_write_adapt_value(reg, value);
> +
> +	switch (reg) {
> +	case PMBUS_VIRT_SAMPLES:
> +		ret = pmbus_update_byte_data(client, page, addr,
> +					     PK_MIN_AVG_AVG_CNT,
> +					     FIELD_PREP(PK_MIN_AVG_AVG_CNT, value));
> +		break;
> +
> +	case PMBUS_IIN_OC_FAULT_LIMIT:
> +		ret = pmbus_write_byte_data(client, page, addr,
> +					    value);
> +		break;
> +
> +	default:
> +		ret = pmbus_write_word_data(client, page, addr, value);
> +		break;

Same comments as for read functions.

> +	}
> +
> +	return ret;
> +}
> +
> +#if IS_ENABLED(CONFIG_SENSORS_TPS25990_REGULATOR)
> +static const struct regulator_desc tps25990_reg_desc[] = {
> +	PMBUS_REGULATOR_ONE("vout"),
> +};
> +#endif
> +
> +static const struct pmbus_driver_info tps25990_base_info = {
> +	.pages = 1,
> +	.format[PSC_VOLTAGE_IN] = direct,
> +	.m[PSC_VOLTAGE_IN] = 5251,
> +	.b[PSC_VOLTAGE_IN] = 0,
> +	.R[PSC_VOLTAGE_IN] = -2,
> +	.format[PSC_VOLTAGE_OUT] = direct,
> +	.m[PSC_VOLTAGE_OUT] = 5251,
> +	.b[PSC_VOLTAGE_OUT] = 0,
> +	.R[PSC_VOLTAGE_OUT] = -2,
> +	.format[PSC_TEMPERATURE] = direct,
> +	.m[PSC_TEMPERATURE] = 140,
> +	.b[PSC_TEMPERATURE] = 32100,
> +	.R[PSC_TEMPERATURE] = -2,
> +	/*
> +	 * Current and Power measurement depends on the ohm value
> +	 * of Rimon. m is multiplied by 1000 below to have an integer
> +	 * and -3 is added to R to compensate.
> +	 */
> +	.format[PSC_CURRENT_IN] = direct,
> +	.m[PSC_CURRENT_IN] = 9538,
> +	.b[PSC_CURRENT_IN] = 0,
> +	.R[PSC_CURRENT_IN] = -6,
> +	.format[PSC_POWER] = direct,
> +	.m[PSC_POWER] = 4901,
> +	.b[PSC_POWER] = 0,
> +	.R[PSC_POWER] = -7,
> +	.func[0] = (PMBUS_HAVE_VIN |
> +		    PMBUS_HAVE_VOUT |
> +		    PMBUS_HAVE_VMON |
> +		    PMBUS_HAVE_IIN |
> +		    PMBUS_HAVE_PIN |
> +		    PMBUS_HAVE_TEMP |
> +		    PMBUS_HAVE_STATUS_VOUT |
> +		    PMBUS_HAVE_STATUS_IOUT |
> +		    PMBUS_HAVE_STATUS_INPUT |
> +		    PMBUS_HAVE_STATUS_TEMP |
> +		    PMBUS_HAVE_SAMPLES),
> +	.read_word_data = tps25990_read_word,
> +	.write_word_data = tps25990_write_word,
> +	.groups = tps25990_groups,
> +
> +#if IS_ENABLED(CONFIG_SENSORS_TPS25990_REGULATOR)
> +	.reg_desc = tps25990_reg_desc,
> +	.num_regulators = ARRAY_SIZE(tps25990_reg_desc),
> +#endif
> +};
> +
> +static const struct i2c_device_id tps25990_i2c_id[] = {
> +	{ "tps25990" },
> +	{}
> +};
> +MODULE_DEVICE_TABLE(i2c, tps25990_i2c_id);
> +
> +static const struct of_device_id tps25990_of_match[] = {
> +	{ .compatible = "ti,tps25990" },
> +	{}
> +};
> +MODULE_DEVICE_TABLE(of, tps25990_of_match);
> +
> +static int tps25990_probe(struct i2c_client *client)
> +{
> +	struct device *dev = &client->dev;
> +	struct pmbus_driver_info *info;
> +	u32 rimon = TPS25990_DEFAULT_RIMON;
> +	int ret;
> +
> +	ret = device_property_read_u32(dev, "ti,rimon-milli-ohms", &rimon);
> +	if (ret == -EINVAL) {
> +		dev_warn(dev,
> +			 "using default rimon: current and power scale possibly wrong\n");

This is not an appropriate warning. It is perfectly fine to load the driver
if there is no ti,rimon-milli-ohms property.

> +	} else if (ret < 0) {
> +		return dev_err_probe(dev, ret, "failed get rimon\n");
> +	}
> +
> +	/*
> +	 * TPS25990 may be stacked with several TPS25895, allowing a higher
> +	 * current. The higher the allowed current is, the lower rimon
> +	 * will be. How low it can realistically get is unknown.
> +	 * To avoid problems with precision later on, rimon is provided in
> +	 * milli Ohms. This is a precaution to keep a stable ABI.
> +	 * At the moment, doing the calculation with rimon in milli Ohms
> +	 * would overflow the s32 'm' in the direct conversion. Convert it
> +	 * back to Ohms until greater precision is actually needed.
> +	 */
> +	rimon /= 1000;
> +

Seems to me it would make more sense to limit the valid range of ti,rimon-milli-ohms
to avoid the overflow. But then I really don't understand the reasoning to provide
the property in milli-ohm, given the default value of 910 Ohm. What is a realistic
lowest value that would make sense ? But even if it is less than 1 Ohm I don't
understand why it would make sense to completely ignore it.

> +	info = devm_kmemdup(dev, &tps25990_base_info, sizeof(*info), GFP_KERNEL);
> +	if (!info)
> +		return -ENOMEM;
> +
> +	/* Adapt the current and power scale for each instance */
> +	info->m[PSC_CURRENT_IN] *= rimon;
> +	info->m[PSC_POWER] *= rimon;

Any rimon value < 1000 mOhm will result in m values of 0.

> +
> +	ret = pmbus_do_probe(client, info);
> +	if (ret < 0)
> +		return ret;
> +
> +	return tps25990_init_debugfs(client);

debugfs initialization is not expected to fail.

> +}
> +
> +static struct i2c_driver tps25990_driver = {
> +	.driver = {
> +		.name = "tps25990",
> +		.of_match_table = tps25990_of_match,
> +	},
> +	.probe = tps25990_probe,
> +	.id_table = tps25990_i2c_id,
> +};
> +module_i2c_driver(tps25990_driver);
> +
> +MODULE_AUTHOR("Jerome Brunet <jbrunet@baylibre.com>");
> +MODULE_DESCRIPTION("PMBUS driver for TPS25990 eFuse");
> +MODULE_LICENSE("GPL");
> +MODULE_IMPORT_NS(PMBUS);
>
Re: [PATCH 3/3] hwmon: (pmbus/tps25990): add initial support
Posted by Jerome Brunet 2 months, 3 weeks ago
On Mon 09 Sep 2024 at 15:52, Guenter Roeck <linux@roeck-us.net> wrote:

[...]

>> +
>> +#define TPS25990_DEFAULT_RIMON		910000
>
> If this is the default, why would it make sense to even specify the
> property in milli-ohm ? I mean, sure, the devices can be stacked,
> but it seems unrealistic to assume that there are hundreds or even
> dozens of devices in parallel.

Indeed not hundreds, I suppose :)
8 would already land you in a position where milli-Ohms could be useful.
Is this realistic, I honestly don't know.

More on this below.

>> +
>> +static int tps25990_mfr_write_protect(struct i2c_client *client, bool protect)
>> +{
>> +	return pmbus_write_byte_data(client, -1, TPS25990_MFR_WRITE_PROTECT,
>> +				     protect ? 0x0 : 0xa2);
>> +}
>> +
>> +static int tps25990_mfr_write_protect_active(struct i2c_client *client)
>> +{
>> +	int ret = pmbus_read_byte_data(client, -1, TPS25990_MFR_WRITE_PROTECT);
>> +
>> +	if (ret < 0)
>> +		return ret;
>> +
>> +	return !(ret & TPS25990_UNLOCKED);
>> +}
>> +
>> +#if IS_ENABLED(CONFIG_DEBUG_FS)
>> +static int tps25990_write_protect_get(void *data, u64 *val)
>> +{
>> +	struct i2c_client *client = data;
>> +
>> +	return tps25990_mfr_write_protect_active(client);
>> +}
>> +
>> +static int tps25990_write_protect_set(void *data, u64 val)
>> +{
>> +	struct i2c_client *client = data;
>> +
>> +	if (val > 1)
>> +		return -EINVAL;
>> +
>> +	return tps25990_mfr_write_protect(client, val);
>> +}
>> +
>> +DEFINE_DEBUGFS_ATTRIBUTE(tps25990_write_protect_fops,
>> +			 tps25990_write_protect_get,
>> +			 tps25990_write_protect_set,
>> +			 "%llu\n");
>> +
>> +static int tps25990_init_debugfs(struct i2c_client *client)
>> +{
>> +	struct dentry *dir;
>> +
>> +	dir = pmbus_get_debugfs_dir(client);
>> +	if (!dir)
>> +		return -ENOENT;
>> +
>> +	debugfs_create_file("write_protect", 0644, dir,
>> +			    client, &tps25990_write_protect_fops);
>> +
>> +	return 0;
>> +}
>> +
>> +#else
>> +static inline int tps25990_init_debugfs(struct i2c_client *client)
>> +{
>> +	return 0;
>> +}
>> +#endif
>> +
>
> In general it is extremely undesirable to overwrite write protection.
> Many chips support such attributes. If write protection is enabled,
> it means that the board vendor does not want to have them changed.

According to documentation, it protects against "unintented" writes,
not 'wrong' or 'malicious'. If one goes in debugfs and write just '0' to
a file, there is an intent at least.

> Granted, that can be overwritten with direct i2c commands, but that
> is what it should be. Anyone who really wants to disable write protection
> should have to dig deeper than just writing into a debugfs or sysfs attribute.
> Otherwise the protection becomes worthless.
> If this is, for example, needed
> for production to write initial settings, the production scripts should
> disable (or enable) write protection by writing directly into command
> registers.

As I wrote in the cover letter, the write protection is always active on
chip startup and it locks down almost everything, including things you may
need to write past production, in the field. The history reset below is
an example of such thing.

To 'safely' remove the protection by writing i2c commands from
userspace:
 * the device will need be unbinded first,
 * call i2cset
 * bind the device again

That seems really cumbersome to do something like an history
reset. Is this what you are suggesting ?

bind/unbind could be skipped by forcing i2cset but that would add danger
where we certainly don't want it.

>
>> +/*
>> + * TPS25990 has history reset based on MIN/AVG/PEAK instead of per sensor type
>> + * Emulate the behaviour a pmbus limit_attr would have for consistency
>> + *  - Read: Do nothing and emit 0
>> + *  - Write: Check the input is a number and reset
>> + */
>> +static ssize_t tps25990_history_reset_show(struct device *dev,
>> +					   struct device_attribute *devattr,
>> +					   char *buf)
>> +{
>> +	return sysfs_emit(buf, "0\n");
>> +}
>> +
>> +static ssize_t tps25990_history_reset_store(struct device *dev,
>> +					    struct device_attribute *devattr,
>> +					    const char *buf, size_t count)
>> +{
>> +	struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
>> +	struct i2c_client *client = to_i2c_client(dev->parent);
>> +	s64 val;
>> +	int ret;
>> +
>> +	if (kstrtos64(buf, 10, &val) < 0)
>> +		return -EINVAL;
>> +
>> +	ret = pmbus_update_byte_data(client, 0, TPS25990_PK_MIN_AVG,
>> +				     BIT(attr->index), BIT(attr->index));
>> +	if (ret < 0)
>> +		return ret;
>> +
>> +	return count;
>> +}
>> +
>> +static SENSOR_DEVICE_ATTR_RW(highest_history_reset, tps25990_history_reset, 7);
>> +static SENSOR_DEVICE_ATTR_RW(average_history_reset, tps25990_history_reset, 6);
>> +static SENSOR_DEVICE_ATTR_RW(lowest_history_reset,  tps25990_history_reset, 5);
>
> That is not a unique problem, and not a reason to introduce non-standard attributes.
> Just attach the attribute to the first channel and document that it resets all
> channels.

Not sure I got this right so I'll rephrase. I should:
* Pick a channel, say vin
* Map the virtual reset register to hit the 3 resets above
* Put in the documentation that it resets the other channels as well
* Not allow independent resets of min/max/avg, just all 3 together ?

>
>> +
>> +static struct attribute *tps25990_attrs[] = {
>> +	&sensor_dev_attr_highest_history_reset.dev_attr.attr,
>> +	&sensor_dev_attr_average_history_reset.dev_attr.attr,
>> +	&sensor_dev_attr_lowest_history_reset.dev_attr.attr,
>> +	NULL,
>> +};
>> +
>> +ATTRIBUTE_GROUPS(tps25990);
>> +
>> +static int tps25990_get_addr(int reg)
>> +{
>> +	switch (reg) {
>> +	case PMBUS_SMBALERT_MASK:
>> +		/*
>> +		 * Note: PMBUS_SMBALERT_MASK is not implemented on this chip
>> +		 * Writing to this address raises CML errors.
>> +		 * Instead it provides ALERT_MASK which allows to set the mask
>> +		 * for each of the status registers, but not the specific bits
>> +		 * in them.
>> +		 * The default setup assert SMBA# if any bit is set in any of the
>> +		 * status registers the chip has. This is as close as we can get
>> +		 * to what pmbus_irq_setup() would set, sooo ... do nothing.
>> +		 */
>> +		return -ENXIO;
>
> Many chips have that problem. The core code ignores errors, and attempts to write
> the command are limited to initialization. This is not a reason to overwrite
> the command like this. If this does cause a real a problem wit hthe chip (other
> than setting CML errors, which many chips not supporting the command do),
> we should define a flag in include/linux/pmbus.h and explain its need.

CML is error is the problem. Following pmbus_irq_setup() there is an
uncleared fault because there is no register check on PMBUS_SMBALERT_MASK.

When pmbus_core then gets here:
https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/drivers/hwmon/pmbus/pmbus_core.c?h=v6.11-rc7#n3386

pmbus_check_block_register() fails because of the uncleared fault and
the 'mfr_id' property is silently not registered, eventhough the
register is supported by the chip. This is how I noticed the problem.

So, should I add flag in include/linux/pmbus.h to skip
PMBUS_SMBALERT_MASK setup ?

Another possibility is to call register_check()
on it before using PMBUS_SMBALERT_MASK in pmbus_core.

>
>> +	case PMBUS_IIN_OC_FAULT_LIMIT:
>> +		/*
>> +		 * VIREF directly sets the over-current limit at which the eFuse
>> +		 * will turn the FET off and trigger a fault. Expose it through
>> +		 * this generic property instead of a manufacturer specific one.
>> +		 */
>> +		return TPS25990_VIREF;
>
> I don't see the value in this override. See below.
>
>> +	case PMBUS_VIRT_READ_VIN_MAX:
>> +		return TPS25990_READ_VIN_PEAK;
>> +	case PMBUS_VIRT_READ_VIN_MIN:
>> +		return TPS25990_READ_VIN_MIN;
>> +	case PMBUS_VIRT_READ_VIN_AVG:
>> +		return TPS25990_READ_VIN_AVG;
>> +	case PMBUS_VIRT_READ_VOUT_MIN:
>> +		return TPS25990_READ_VOUT_MIN;
>> +	case PMBUS_VIRT_READ_VOUT_AVG:
>> +		return TPS25990_READ_VOUT_AVG;
>> +	case PMBUS_VIRT_READ_IIN_AVG:
>> +		return TPS25990_READ_IIN_AVG;
>> +	case PMBUS_VIRT_READ_IIN_MAX:
>> +		return TPS25990_READ_IIN_PEAK;
>> +	case PMBUS_VIRT_READ_TEMP_AVG:
>> +		return TPS25990_READ_TEMP_AVG;
>> +	case PMBUS_VIRT_READ_TEMP_MAX:
>> +		return TPS25990_READ_TEMP_PEAK;
>> +	case PMBUS_VIRT_READ_PIN_AVG:
>> +		return TPS25990_READ_PIN_AVG;
>> +	case PMBUS_VIRT_READ_PIN_MAX:
>> +		return TPS25990_READ_PIN_PEAK;
>> +	case PMBUS_VIRT_READ_VMON:
>> +		return TPS25990_READ_VAUX;
>> +	case PMBUS_VIRT_SAMPLES:
>> +		return TPS25990_PK_MIN_AVG;
>
> default: missing.
>
>> +	}
>> +
>> +	/* Let the register check do its job */
>> +	if (reg < PMBUS_VIRT_BASE)
>> +		return reg;
>> +
>> +	return -ENXIO;
>
> This needs to be more specific: The code should only return -ENXIO
> if auto-detection can not handle the case. "Return -ENXIO for all
> unsupported virtual registers" is unexpected. That situation should
> be handled by the PMBus core.
>
>> +}
>> +
>> +/*
>> + * Some registers use a different scale than the one registered with
>> + * pmbus_driver_info. An extra conversion step is necessary to adapt
>> + * the register value to the conversion on the sensor type
>> + */
>> +static int tps25990_read_adapt_value(int reg, int val)
>> +{
>> +	switch (reg) {
>> +	case PMBUS_VIN_UV_WARN_LIMIT:
>> +	case PMBUS_VIN_UV_FAULT_LIMIT:
>> +	case PMBUS_VIN_OV_WARN_LIMIT:
>> +	case PMBUS_VOUT_UV_WARN_LIMIT:
>> +	case PMBUS_IIN_OC_WARN_LIMIT:
>> +	case PMBUS_OT_WARN_LIMIT:
>> +	case PMBUS_OT_FAULT_LIMIT:
>> +	case PMBUS_PIN_OP_WARN_LIMIT:
>> +	case PMBUS_POWER_GOOD_OFF:
>> +		/*
>> +		 * These registers provide an 8 bits value instead of a
>> +		 * 10bits one. Just shifting twice the register value is
>> +		 * enough to make the sensor type conversion work, even
>> +		 * if the datasheet provides different m, b and R for
>> +		 * those.
>> +		 */
>> +		val <<= TPS25990_8B_SHIFT;
>> +		break;
>> +
>> +	case PMBUS_VIN_OV_FAULT_LIMIT:
>> +		val = DIV_ROUND_CLOSEST(val * TPS25990_VIN_OVF_NUM, TPS25990_VIN_OVF_DIV);
>> +		val += TPS25990_VIN_OVF_OFF;
>> +		break;
>> +
>> +	case PMBUS_IIN_OC_FAULT_LIMIT:
>> +		val = DIV_ROUND_CLOSEST(val * TPS25990_IIN_OCF_NUM, TPS25990_IIN_OCF_DIV);
>> +		val += TPS25990_IIN_OCF_OFF;
>> +		break;
>> +
>> +	case PMBUS_VIRT_SAMPLES:
>> +		val = 1 << val;
>> +		break;
>
> default case missing.
>
>> +	}
>> +
>> +	return val;
>> +}
>> +
>> +static int tps25990_read_word(struct i2c_client *client,
>> +			      int page, int phase, int reg)
>> +{
>> +	int ret, addr;
>> +
>> +	addr = tps25990_get_addr(reg);
>> +	if (addr < 0)
>> +		return addr;
>> +
>> +	switch (reg) {
>> +	case PMBUS_VIRT_SAMPLES:
>> +		ret = pmbus_read_byte_data(client, page, addr);
>
> Mapping the register name in tps25990_get_addr() is unnecessary
> and misleading. It is well known that TPS25990_PK_MIN_AVG is to be
> used. Do it here.

My intent was to do the mapping in one place instead of repeating for
both read and write, not be misleading. I'll change it.

>
>> +		ret = FIELD_GET(PK_MIN_AVG_AVG_CNT, ret);
>> +		break;
>> +
>> +	case PMBUS_IIN_OC_FAULT_LIMIT:
>> +		ret = pmbus_read_byte_data(client, page, addr);
>> +		break;
>> +
> Same here.
>
>> +	default:
>> +		ret = pmbus_read_word_data(client, page, -1, addr);
>
> This is unexpected for registers not handled locally. Expectation is
> that -ENODATA is returned for those, to be handled in the core.

Got it. Thanks.

>
>> +		break;
>> +	}
>> +
>> +	if (ret >= 0)
>> +		ret = tps25990_read_adapt_value(reg, ret);
>> +
>> +	return ret;
>> +}
>> +
>> +static int tps25990_write_adapt_value(int reg, int val)
>> +{
>> +	switch (reg) {
>> +	case PMBUS_VIN_UV_WARN_LIMIT:
>> +	case PMBUS_VIN_UV_FAULT_LIMIT:
>> +	case PMBUS_VIN_OV_WARN_LIMIT:
>> +	case PMBUS_VOUT_UV_WARN_LIMIT:
>> +	case PMBUS_IIN_OC_WARN_LIMIT:
>> +	case PMBUS_OT_WARN_LIMIT:
>> +	case PMBUS_OT_FAULT_LIMIT:
>> +	case PMBUS_PIN_OP_WARN_LIMIT:
>> +	case PMBUS_POWER_GOOD_OFF:
>> +		val >>= TPS25990_8B_SHIFT;
>> +		val = clamp(val, 0, 0xff);
>
> Why clamp() here but clamp_val() elsewhere ?
>
>> +		break;
>> +
>> +	case PMBUS_VIN_OV_FAULT_LIMIT:
>> +		val -= TPS25990_VIN_OVF_OFF;
>> +		val = DIV_ROUND_CLOSEST(val * TPS25990_VIN_OVF_DIV, TPS25990_VIN_OVF_NUM);
>> +		val = clamp_val(val, 0, 0xf);
>> +		break;
>> +
>> +	case PMBUS_IIN_OC_FAULT_LIMIT:
>> +		val -= TPS25990_IIN_OCF_OFF;
>> +		val = DIV_ROUND_CLOSEST(val * TPS25990_IIN_OCF_DIV, TPS25990_IIN_OCF_NUM);
>> +		val = clamp_val(val, 0, 0x3f);
>> +		break;
>> +
>> +	case PMBUS_VIRT_SAMPLES:
>> +		val = clamp_val(val, 1, 1 << PK_MIN_AVG_AVG_CNT);
>> +		val = ilog2(val);
>> +		break;
>
> default: missing.
>
>> +	}
>> +
>> +	return val;
>> +}
>> +
>> +static int tps25990_write_word(struct i2c_client *client,
>> +			       int page, int reg, u16 value)
>> +{
>> +	int addr, ret;
>> +
>> +	addr = tps25990_get_addr(reg);
>> +	if (addr < 0)
>> +		return addr;
>> +
>> +	value = tps25990_write_adapt_value(reg, value);
>> +
>> +	switch (reg) {
>> +	case PMBUS_VIRT_SAMPLES:
>> +		ret = pmbus_update_byte_data(client, page, addr,
>> +					     PK_MIN_AVG_AVG_CNT,
>> +					     FIELD_PREP(PK_MIN_AVG_AVG_CNT, value));
>> +		break;
>> +
>> +	case PMBUS_IIN_OC_FAULT_LIMIT:
>> +		ret = pmbus_write_byte_data(client, page, addr,
>> +					    value);
>> +		break;
>> +
>> +	default:
>> +		ret = pmbus_write_word_data(client, page, addr, value);
>> +		break;
>
> Same comments as for read functions.
>
>> +	}
>> +
>> +	return ret;
>> +}
>> +
>> +#if IS_ENABLED(CONFIG_SENSORS_TPS25990_REGULATOR)
>> +static const struct regulator_desc tps25990_reg_desc[] = {
>> +	PMBUS_REGULATOR_ONE("vout"),
>> +};
>> +#endif
>> +
>> +static const struct pmbus_driver_info tps25990_base_info = {
>> +	.pages = 1,
>> +	.format[PSC_VOLTAGE_IN] = direct,
>> +	.m[PSC_VOLTAGE_IN] = 5251,
>> +	.b[PSC_VOLTAGE_IN] = 0,
>> +	.R[PSC_VOLTAGE_IN] = -2,
>> +	.format[PSC_VOLTAGE_OUT] = direct,
>> +	.m[PSC_VOLTAGE_OUT] = 5251,
>> +	.b[PSC_VOLTAGE_OUT] = 0,
>> +	.R[PSC_VOLTAGE_OUT] = -2,
>> +	.format[PSC_TEMPERATURE] = direct,
>> +	.m[PSC_TEMPERATURE] = 140,
>> +	.b[PSC_TEMPERATURE] = 32100,
>> +	.R[PSC_TEMPERATURE] = -2,
>> +	/*
>> +	 * Current and Power measurement depends on the ohm value
>> +	 * of Rimon. m is multiplied by 1000 below to have an integer
>> +	 * and -3 is added to R to compensate.
>> +	 */
>> +	.format[PSC_CURRENT_IN] = direct,
>> +	.m[PSC_CURRENT_IN] = 9538,
>> +	.b[PSC_CURRENT_IN] = 0,
>> +	.R[PSC_CURRENT_IN] = -6,
>> +	.format[PSC_POWER] = direct,
>> +	.m[PSC_POWER] = 4901,
>> +	.b[PSC_POWER] = 0,
>> +	.R[PSC_POWER] = -7,
>> +	.func[0] = (PMBUS_HAVE_VIN |
>> +		    PMBUS_HAVE_VOUT |
>> +		    PMBUS_HAVE_VMON |
>> +		    PMBUS_HAVE_IIN |
>> +		    PMBUS_HAVE_PIN |
>> +		    PMBUS_HAVE_TEMP |
>> +		    PMBUS_HAVE_STATUS_VOUT |
>> +		    PMBUS_HAVE_STATUS_IOUT |
>> +		    PMBUS_HAVE_STATUS_INPUT |
>> +		    PMBUS_HAVE_STATUS_TEMP |
>> +		    PMBUS_HAVE_SAMPLES),
>> +	.read_word_data = tps25990_read_word,
>> +	.write_word_data = tps25990_write_word,
>> +	.groups = tps25990_groups,
>> +
>> +#if IS_ENABLED(CONFIG_SENSORS_TPS25990_REGULATOR)
>> +	.reg_desc = tps25990_reg_desc,
>> +	.num_regulators = ARRAY_SIZE(tps25990_reg_desc),
>> +#endif
>> +};
>> +
>> +static const struct i2c_device_id tps25990_i2c_id[] = {
>> +	{ "tps25990" },
>> +	{}
>> +};
>> +MODULE_DEVICE_TABLE(i2c, tps25990_i2c_id);
>> +
>> +static const struct of_device_id tps25990_of_match[] = {
>> +	{ .compatible = "ti,tps25990" },
>> +	{}
>> +};
>> +MODULE_DEVICE_TABLE(of, tps25990_of_match);
>> +
>> +static int tps25990_probe(struct i2c_client *client)
>> +{
>> +	struct device *dev = &client->dev;
>> +	struct pmbus_driver_info *info;
>> +	u32 rimon = TPS25990_DEFAULT_RIMON;
>> +	int ret;
>> +
>> +	ret = device_property_read_u32(dev, "ti,rimon-milli-ohms", &rimon);
>> +	if (ret == -EINVAL) {
>> +		dev_warn(dev,
>> +			 "using default rimon: current and power scale possibly wrong\n");
>
> This is not an appropriate warning. It is perfectly fine to load the driver
> if there is no ti,rimon-milli-ohms property.

I should have commented more on the default value. It is meant for the
case where the device is instanciated through i2c sys 'new_device',
which is meant for debugging purpose. In that particular case, it does
not really matter if the current and power scale are wrong.

There is no way to pass device properties when instanciating device
through that interface, as far as I know. 

In every other cases, a correct Rimon value is expected.
I could turn the above to an error. It means loading through i2c sys
would not possible for this driver.

Would it be better ?

>
>> +	} else if (ret < 0) {
>> +		return dev_err_probe(dev, ret, "failed get rimon\n");
>> +	}
>> +
>> +	/*
>> +	 * TPS25990 may be stacked with several TPS25895, allowing a higher
>> +	 * current. The higher the allowed current is, the lower rimon
>> +	 * will be. How low it can realistically get is unknown.
>> +	 * To avoid problems with precision later on, rimon is provided in
>> +	 * milli Ohms. This is a precaution to keep a stable ABI.
>> +	 * At the moment, doing the calculation with rimon in milli Ohms
>> +	 * would overflow the s32 'm' in the direct conversion. Convert it
>> +	 * back to Ohms until greater precision is actually needed.
>> +	 */
>> +	rimon /= 1000;
>> +
>
> Seems to me it would make more sense to limit the valid range of ti,rimon-milli-ohms
> to avoid the overflow. But then I really don't understand the reasoning to provide
> the property in milli-ohm, given the default value of 910 Ohm. What is a realistic
> lowest value that would make sense ?

The highest value I've seen, when the tps25990 is alone, is 1370
Ohms. That means a 30A overcurrent fault limit.

With one TPS25895, I've seen 608 Ohms (110A limit)

I have no idea what the realistic low limit is. To get to ~100 Ohms, you'd
need 8 devices (not hundreds ;) ) If one gets there, it might be
desirable to have 3 digits to play with, and not be limited by the unit.

The DT folks really don't like when a property changes. Going with
milli-Ohms is way to anticipate the problem.

The other way could be to use Ohms now, and if we ever get to point
where milli-Ohms precision is needed, add it then. The downside is that
the driver will need to support both properties.

Would you prefer this ?


> But even if it is less than 1 Ohm I don't
> understand why it would make sense to completely ignore it.

It would not make sense to ignore it.

>
>> +	info = devm_kmemdup(dev, &tps25990_base_info, sizeof(*info), GFP_KERNEL);
>> +	if (!info)
>> +		return -ENOMEM;
>> +
>> +	/* Adapt the current and power scale for each instance */
>> +	info->m[PSC_CURRENT_IN] *= rimon;
>> +	info->m[PSC_POWER] *= rimon;
>
> Any rimon value < 1000 mOhm will result in m values of 0.

Indeed. Such Rimon value would mean an over current limit > 50kA. I admit
I did really think much about such value.

The idea was more keep some precision if we get somewhere near a 100 Ohms.

>
>> +
>> +	ret = pmbus_do_probe(client, info);
>> +	if (ret < 0)
>> +		return ret;
>> +
>> +	return tps25990_init_debugfs(client);
>
> debugfs initialization is not expected to fail.
>
>> +}
>> +
>> +static struct i2c_driver tps25990_driver = {
>> +	.driver = {
>> +		.name = "tps25990",
>> +		.of_match_table = tps25990_of_match,
>> +	},
>> +	.probe = tps25990_probe,
>> +	.id_table = tps25990_i2c_id,
>> +};
>> +module_i2c_driver(tps25990_driver);
>> +
>> +MODULE_AUTHOR("Jerome Brunet <jbrunet@baylibre.com>");
>> +MODULE_DESCRIPTION("PMBUS driver for TPS25990 eFuse");
>> +MODULE_LICENSE("GPL");
>> +MODULE_IMPORT_NS(PMBUS);
>> 

-- 
Jerome
Re: [PATCH 3/3] hwmon: (pmbus/tps25990): add initial support
Posted by Guenter Roeck 2 months, 2 weeks ago
On Tue, Sep 10, 2024 at 11:07:57AM +0200, Jerome Brunet wrote:
> On Mon 09 Sep 2024 at 15:52, Guenter Roeck <linux@roeck-us.net> wrote:
> 
> [...]
> 
Unrelated to the other comments:

 Documentation/hwmon/tps25990.rst | 141 ++++++++++++

Needs to be added to Documentation/hwmon/index.rst.

+config SENSORS_TPS25990_REGULATOR
+	bool "Regulator support for TPS25990 and compatibles"
+	depends on SENSORS_TPS25990 && REGULATOR
+	default SENSORS_TPS2599
                ^^^^^^^^^^^^^^^ TPS2599 ???

> >> +
> >> +#define TPS25990_DEFAULT_RIMON		910000

Where does the default come from anyway ? I don't immediately see the number
in the datasheet.

> >> +static int tps25990_write_protect_get(void *data, u64 *val)
> >> +{
> >> +	struct i2c_client *client = data;
> >> +
> >> +	return tps25990_mfr_write_protect_active(client);
> >> +}
> >> +
> >> +static int tps25990_write_protect_set(void *data, u64 val)
> >> +{
> >> +	struct i2c_client *client = data;
> >> +
> >> +	if (val > 1)
> >> +		return -EINVAL;
> >> +
> >> +	return tps25990_mfr_write_protect(client, val);
> >> +}
> >> +
> >> +DEFINE_DEBUGFS_ATTRIBUTE(tps25990_write_protect_fops,
> >> +			 tps25990_write_protect_get,
> >> +			 tps25990_write_protect_set,
> >> +			 "%llu\n");
> >> +
> >> +static int tps25990_init_debugfs(struct i2c_client *client)
> >> +{
> >> +	struct dentry *dir;
> >> +
> >> +	dir = pmbus_get_debugfs_dir(client);
> >> +	if (!dir)
> >> +		return -ENOENT;
> >> +
> >> +	debugfs_create_file("write_protect", 0644, dir,
> >> +			    client, &tps25990_write_protect_fops);
> >> +
> >> +	return 0;
> >> +}
> >> +
> >> +#else
> >> +static inline int tps25990_init_debugfs(struct i2c_client *client)
> >> +{
> >> +	return 0;
> >> +}
> >> +#endif
> >> +
> >
> > In general it is extremely undesirable to overwrite write protection.
> > Many chips support such attributes. If write protection is enabled,
> > it means that the board vendor does not want to have them changed.
> 
> According to documentation, it protects against "unintented" writes,
> not 'wrong' or 'malicious'. If one goes in debugfs and write just '0' to
> a file, there is an intent at least.
> 
> > Granted, that can be overwritten with direct i2c commands, but that
> > is what it should be. Anyone who really wants to disable write protection
> > should have to dig deeper than just writing into a debugfs or sysfs attribute.
> > Otherwise the protection becomes worthless.
> > If this is, for example, needed
> > for production to write initial settings, the production scripts should
> > disable (or enable) write protection by writing directly into command
> > registers.
> 
> As I wrote in the cover letter, the write protection is always active on
> chip startup and it locks down almost everything, including things you may
> need to write past production, in the field. The history reset below is
> an example of such thing.
> 
> To 'safely' remove the protection by writing i2c commands from
> userspace:
>  * the device will need be unbinded first,
>  * call i2cset
>  * bind the device again
> 
> That seems really cumbersome to do something like an history
> reset. Is this what you are suggesting ?
> 
> bind/unbind could be skipped by forcing i2cset but that would add danger
> where we certainly don't want it.
> 

Not sure I understand the "danger" part. Either case, the problem is deeper.
The driver enables regulator support, which includes enabling and disabling
the output voltage. But that doesn't work unles write protect is disabled.
debugfs doesn't help there; that is way too late.

> >
> >> +/*
> >> + * TPS25990 has history reset based on MIN/AVG/PEAK instead of per sensor type
> >> + * Emulate the behaviour a pmbus limit_attr would have for consistency
> >> + *  - Read: Do nothing and emit 0
> >> + *  - Write: Check the input is a number and reset
> >> + */
> >> +static ssize_t tps25990_history_reset_show(struct device *dev,
> >> +					   struct device_attribute *devattr,
> >> +					   char *buf)
> >> +{
> >> +	return sysfs_emit(buf, "0\n");
> >> +}
> >> +
> >> +static ssize_t tps25990_history_reset_store(struct device *dev,
> >> +					    struct device_attribute *devattr,
> >> +					    const char *buf, size_t count)
> >> +{
> >> +	struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
> >> +	struct i2c_client *client = to_i2c_client(dev->parent);
> >> +	s64 val;
> >> +	int ret;
> >> +
> >> +	if (kstrtos64(buf, 10, &val) < 0)
> >> +		return -EINVAL;
> >> +
> >> +	ret = pmbus_update_byte_data(client, 0, TPS25990_PK_MIN_AVG,
> >> +				     BIT(attr->index), BIT(attr->index));
> >> +	if (ret < 0)
> >> +		return ret;
> >> +
> >> +	return count;
> >> +}
> >> +
> >> +static SENSOR_DEVICE_ATTR_RW(highest_history_reset, tps25990_history_reset, 7);
> >> +static SENSOR_DEVICE_ATTR_RW(average_history_reset, tps25990_history_reset, 6);
> >> +static SENSOR_DEVICE_ATTR_RW(lowest_history_reset,  tps25990_history_reset, 5);
> >
> > That is not a unique problem, and not a reason to introduce non-standard attributes.
> > Just attach the attribute to the first channel and document that it resets all
> > channels.
> 
> Not sure I got this right so I'll rephrase. I should:
> * Pick a channel, say vin
> * Map the virtual reset register to hit the 3 resets above
> * Put in the documentation that it resets the other channels as well
> * Not allow independent resets of min/max/avg, just all 3 together ?
> 
Correct. It is amazing what hardware designers come up with (here:
resetting history based on min/max/average instead of the sensor type
is novel), but I really don't want to introduce new attributes to
accommodate each variant. I'd be open to introducing a global
PMBUS_VIRT_RESET_HISTORY virtual register and reset_history attribute
if you want to go there, but that would have to be in the PMBus core.

> >
> >> +
> >> +static struct attribute *tps25990_attrs[] = {
> >> +	&sensor_dev_attr_highest_history_reset.dev_attr.attr,
> >> +	&sensor_dev_attr_average_history_reset.dev_attr.attr,
> >> +	&sensor_dev_attr_lowest_history_reset.dev_attr.attr,
> >> +	NULL,
> >> +};
> >> +
> >> +ATTRIBUTE_GROUPS(tps25990);
> >> +
> >> +static int tps25990_get_addr(int reg)
> >> +{
> >> +	switch (reg) {
> >> +	case PMBUS_SMBALERT_MASK:
> >> +		/*
> >> +		 * Note: PMBUS_SMBALERT_MASK is not implemented on this chip
> >> +		 * Writing to this address raises CML errors.
> >> +		 * Instead it provides ALERT_MASK which allows to set the mask
> >> +		 * for each of the status registers, but not the specific bits
> >> +		 * in them.
> >> +		 * The default setup assert SMBA# if any bit is set in any of the
> >> +		 * status registers the chip has. This is as close as we can get
> >> +		 * to what pmbus_irq_setup() would set, sooo ... do nothing.
> >> +		 */
> >> +		return -ENXIO;
> >
> > Many chips have that problem. The core code ignores errors, and attempts to write
> > the command are limited to initialization. This is not a reason to overwrite
> > the command like this. If this does cause a real a problem wit hthe chip (other
> > than setting CML errors, which many chips not supporting the command do),
> > we should define a flag in include/linux/pmbus.h and explain its need.
> 
> CML is error is the problem. Following pmbus_irq_setup() there is an
> uncleared fault because there is no register check on PMBUS_SMBALERT_MASK.
> 
> When pmbus_core then gets here:
> https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/drivers/hwmon/pmbus/pmbus_core.c?h=v6.11-rc7#n3386
> 
> pmbus_check_block_register() fails because of the uncleared fault and
> the 'mfr_id' property is silently not registered, eventhough the
> register is supported by the chip. This is how I noticed the problem.
> 
> So, should I add flag in include/linux/pmbus.h to skip
> PMBUS_SMBALERT_MASK setup ?
> 
> Another possibility is to call register_check()
> on it before using PMBUS_SMBALERT_MASK in pmbus_core.
> 

The problem, as you point out, is in pmbus_irq_setup(). Since the function
explicitly ignores errors from accessing PMBUS_SMBALERT_MASK, it should
either clear faults after it is done. I don't think we can rely on
register_check() because the register might exist but be read-only.

> >
> >> +	case PMBUS_IIN_OC_FAULT_LIMIT:
> >> +		/*
> >> +		 * VIREF directly sets the over-current limit at which the eFuse
> >> +		 * will turn the FET off and trigger a fault. Expose it through
> >> +		 * this generic property instead of a manufacturer specific one.
> >> +		 */
> >> +		return TPS25990_VIREF;
> >
> > I don't see the value in this override. See below.
> >
> >> +	case PMBUS_VIRT_READ_VIN_MAX:
> >> +		return TPS25990_READ_VIN_PEAK;
> >> +	case PMBUS_VIRT_READ_VIN_MIN:
> >> +		return TPS25990_READ_VIN_MIN;
> >> +	case PMBUS_VIRT_READ_VIN_AVG:
> >> +		return TPS25990_READ_VIN_AVG;
> >> +	case PMBUS_VIRT_READ_VOUT_MIN:
> >> +		return TPS25990_READ_VOUT_MIN;
> >> +	case PMBUS_VIRT_READ_VOUT_AVG:
> >> +		return TPS25990_READ_VOUT_AVG;
> >> +	case PMBUS_VIRT_READ_IIN_AVG:
> >> +		return TPS25990_READ_IIN_AVG;
> >> +	case PMBUS_VIRT_READ_IIN_MAX:
> >> +		return TPS25990_READ_IIN_PEAK;
> >> +	case PMBUS_VIRT_READ_TEMP_AVG:
> >> +		return TPS25990_READ_TEMP_AVG;
> >> +	case PMBUS_VIRT_READ_TEMP_MAX:
> >> +		return TPS25990_READ_TEMP_PEAK;
> >> +	case PMBUS_VIRT_READ_PIN_AVG:
> >> +		return TPS25990_READ_PIN_AVG;
> >> +	case PMBUS_VIRT_READ_PIN_MAX:
> >> +		return TPS25990_READ_PIN_PEAK;
> >> +	case PMBUS_VIRT_READ_VMON:
> >> +		return TPS25990_READ_VAUX;
> >> +	case PMBUS_VIRT_SAMPLES:
> >> +		return TPS25990_PK_MIN_AVG;
> >
> > default: missing.
> >
> >> +	}
> >> +
> >> +	/* Let the register check do its job */
> >> +	if (reg < PMBUS_VIRT_BASE)
> >> +		return reg;
> >> +
> >> +	return -ENXIO;
> >
> > This needs to be more specific: The code should only return -ENXIO
> > if auto-detection can not handle the case. "Return -ENXIO for all
> > unsupported virtual registers" is unexpected. That situation should
> > be handled by the PMBus core.
> >
> >> +}
> >> +
> >> +/*
> >> + * Some registers use a different scale than the one registered with
> >> + * pmbus_driver_info. An extra conversion step is necessary to adapt
> >> + * the register value to the conversion on the sensor type
> >> + */
> >> +static int tps25990_read_adapt_value(int reg, int val)
> >> +{
> >> +	switch (reg) {
> >> +	case PMBUS_VIN_UV_WARN_LIMIT:
> >> +	case PMBUS_VIN_UV_FAULT_LIMIT:
> >> +	case PMBUS_VIN_OV_WARN_LIMIT:
> >> +	case PMBUS_VOUT_UV_WARN_LIMIT:
> >> +	case PMBUS_IIN_OC_WARN_LIMIT:
> >> +	case PMBUS_OT_WARN_LIMIT:
> >> +	case PMBUS_OT_FAULT_LIMIT:
> >> +	case PMBUS_PIN_OP_WARN_LIMIT:
> >> +	case PMBUS_POWER_GOOD_OFF:
> >> +		/*
> >> +		 * These registers provide an 8 bits value instead of a
> >> +		 * 10bits one. Just shifting twice the register value is
> >> +		 * enough to make the sensor type conversion work, even
> >> +		 * if the datasheet provides different m, b and R for
> >> +		 * those.
> >> +		 */
> >> +		val <<= TPS25990_8B_SHIFT;
> >> +		break;
> >> +
> >> +	case PMBUS_VIN_OV_FAULT_LIMIT:
> >> +		val = DIV_ROUND_CLOSEST(val * TPS25990_VIN_OVF_NUM, TPS25990_VIN_OVF_DIV);
> >> +		val += TPS25990_VIN_OVF_OFF;
> >> +		break;
> >> +
> >> +	case PMBUS_IIN_OC_FAULT_LIMIT:
> >> +		val = DIV_ROUND_CLOSEST(val * TPS25990_IIN_OCF_NUM, TPS25990_IIN_OCF_DIV);
> >> +		val += TPS25990_IIN_OCF_OFF;
> >> +		break;
> >> +
> >> +	case PMBUS_VIRT_SAMPLES:
> >> +		val = 1 << val;
> >> +		break;
> >
> > default case missing.
> >
> >> +	}
> >> +
> >> +	return val;
> >> +}
> >> +
> >> +static int tps25990_read_word(struct i2c_client *client,
> >> +			      int page, int phase, int reg)
> >> +{
> >> +	int ret, addr;
> >> +
> >> +	addr = tps25990_get_addr(reg);
> >> +	if (addr < 0)
> >> +		return addr;
> >> +
> >> +	switch (reg) {
> >> +	case PMBUS_VIRT_SAMPLES:
> >> +		ret = pmbus_read_byte_data(client, page, addr);
> >
> > Mapping the register name in tps25990_get_addr() is unnecessary
> > and misleading. It is well known that TPS25990_PK_MIN_AVG is to be
> > used. Do it here.
> 
> My intent was to do the mapping in one place instead of repeating for
> both read and write, not be misleading. I'll change it.
> 

Mapping is only needed in the default: case below, and should be limited
to that.

> >
> >> +		ret = FIELD_GET(PK_MIN_AVG_AVG_CNT, ret);
> >> +		break;
> >> +
> >> +	case PMBUS_IIN_OC_FAULT_LIMIT:
> >> +		ret = pmbus_read_byte_data(client, page, addr);
> >> +		break;
> >> +
> > Same here.
> >
> >> +	default:
> >> +		ret = pmbus_read_word_data(client, page, -1, addr);
> >
> > This is unexpected for registers not handled locally. Expectation is
> > that -ENODATA is returned for those, to be handled in the core.
> 
> Got it. Thanks.
> 
> >
> >> +		break;
> >> +	}
> >> +
> >> +	if (ret >= 0)
> >> +		ret = tps25990_read_adapt_value(reg, ret);
> >> +
> >> +	return ret;
> >> +}
> >> +
> >> +static int tps25990_write_adapt_value(int reg, int val)
> >> +{
> >> +	switch (reg) {
> >> +	case PMBUS_VIN_UV_WARN_LIMIT:
> >> +	case PMBUS_VIN_UV_FAULT_LIMIT:
> >> +	case PMBUS_VIN_OV_WARN_LIMIT:
> >> +	case PMBUS_VOUT_UV_WARN_LIMIT:
> >> +	case PMBUS_IIN_OC_WARN_LIMIT:
> >> +	case PMBUS_OT_WARN_LIMIT:
> >> +	case PMBUS_OT_FAULT_LIMIT:
> >> +	case PMBUS_PIN_OP_WARN_LIMIT:
> >> +	case PMBUS_POWER_GOOD_OFF:
> >> +		val >>= TPS25990_8B_SHIFT;
> >> +		val = clamp(val, 0, 0xff);
> >
> > Why clamp() here but clamp_val() elsewhere ?
> >
> >> +		break;
> >> +
> >> +	case PMBUS_VIN_OV_FAULT_LIMIT:
> >> +		val -= TPS25990_VIN_OVF_OFF;
> >> +		val = DIV_ROUND_CLOSEST(val * TPS25990_VIN_OVF_DIV, TPS25990_VIN_OVF_NUM);
> >> +		val = clamp_val(val, 0, 0xf);
> >> +		break;
> >> +
> >> +	case PMBUS_IIN_OC_FAULT_LIMIT:
> >> +		val -= TPS25990_IIN_OCF_OFF;
> >> +		val = DIV_ROUND_CLOSEST(val * TPS25990_IIN_OCF_DIV, TPS25990_IIN_OCF_NUM);
> >> +		val = clamp_val(val, 0, 0x3f);
> >> +		break;
> >> +
> >> +	case PMBUS_VIRT_SAMPLES:
> >> +		val = clamp_val(val, 1, 1 << PK_MIN_AVG_AVG_CNT);
> >> +		val = ilog2(val);
> >> +		break;
> >
> > default: missing.
> >
> >> +	}
> >> +
> >> +	return val;
> >> +}
> >> +
> >> +static int tps25990_write_word(struct i2c_client *client,
> >> +			       int page, int reg, u16 value)
> >> +{
> >> +	int addr, ret;
> >> +
> >> +	addr = tps25990_get_addr(reg);
> >> +	if (addr < 0)
> >> +		return addr;
> >> +
> >> +	value = tps25990_write_adapt_value(reg, value);
> >> +
> >> +	switch (reg) {
> >> +	case PMBUS_VIRT_SAMPLES:
> >> +		ret = pmbus_update_byte_data(client, page, addr,
> >> +					     PK_MIN_AVG_AVG_CNT,
> >> +					     FIELD_PREP(PK_MIN_AVG_AVG_CNT, value));
> >> +		break;
> >> +
> >> +	case PMBUS_IIN_OC_FAULT_LIMIT:
> >> +		ret = pmbus_write_byte_data(client, page, addr,
> >> +					    value);
> >> +		break;
> >> +
> >> +	default:
> >> +		ret = pmbus_write_word_data(client, page, addr, value);
> >> +		break;
> >
> > Same comments as for read functions.
> >
> >> +	}
> >> +
> >> +	return ret;
> >> +}
> >> +
> >> +#if IS_ENABLED(CONFIG_SENSORS_TPS25990_REGULATOR)
> >> +static const struct regulator_desc tps25990_reg_desc[] = {
> >> +	PMBUS_REGULATOR_ONE("vout"),
> >> +};
> >> +#endif
> >> +
> >> +static const struct pmbus_driver_info tps25990_base_info = {
> >> +	.pages = 1,
> >> +	.format[PSC_VOLTAGE_IN] = direct,
> >> +	.m[PSC_VOLTAGE_IN] = 5251,
> >> +	.b[PSC_VOLTAGE_IN] = 0,
> >> +	.R[PSC_VOLTAGE_IN] = -2,
> >> +	.format[PSC_VOLTAGE_OUT] = direct,
> >> +	.m[PSC_VOLTAGE_OUT] = 5251,
> >> +	.b[PSC_VOLTAGE_OUT] = 0,
> >> +	.R[PSC_VOLTAGE_OUT] = -2,
> >> +	.format[PSC_TEMPERATURE] = direct,
> >> +	.m[PSC_TEMPERATURE] = 140,
> >> +	.b[PSC_TEMPERATURE] = 32100,
> >> +	.R[PSC_TEMPERATURE] = -2,
> >> +	/*
> >> +	 * Current and Power measurement depends on the ohm value
> >> +	 * of Rimon. m is multiplied by 1000 below to have an integer
> >> +	 * and -3 is added to R to compensate.
> >> +	 */
> >> +	.format[PSC_CURRENT_IN] = direct,
> >> +	.m[PSC_CURRENT_IN] = 9538,
> >> +	.b[PSC_CURRENT_IN] = 0,
> >> +	.R[PSC_CURRENT_IN] = -6,
> >> +	.format[PSC_POWER] = direct,
> >> +	.m[PSC_POWER] = 4901,
> >> +	.b[PSC_POWER] = 0,
> >> +	.R[PSC_POWER] = -7,
> >> +	.func[0] = (PMBUS_HAVE_VIN |
> >> +		    PMBUS_HAVE_VOUT |
> >> +		    PMBUS_HAVE_VMON |
> >> +		    PMBUS_HAVE_IIN |
> >> +		    PMBUS_HAVE_PIN |
> >> +		    PMBUS_HAVE_TEMP |
> >> +		    PMBUS_HAVE_STATUS_VOUT |
> >> +		    PMBUS_HAVE_STATUS_IOUT |
> >> +		    PMBUS_HAVE_STATUS_INPUT |
> >> +		    PMBUS_HAVE_STATUS_TEMP |
> >> +		    PMBUS_HAVE_SAMPLES),
> >> +	.read_word_data = tps25990_read_word,
> >> +	.write_word_data = tps25990_write_word,
> >> +	.groups = tps25990_groups,
> >> +
> >> +#if IS_ENABLED(CONFIG_SENSORS_TPS25990_REGULATOR)
> >> +	.reg_desc = tps25990_reg_desc,
> >> +	.num_regulators = ARRAY_SIZE(tps25990_reg_desc),
> >> +#endif
> >> +};
> >> +
> >> +static const struct i2c_device_id tps25990_i2c_id[] = {
> >> +	{ "tps25990" },
> >> +	{}
> >> +};
> >> +MODULE_DEVICE_TABLE(i2c, tps25990_i2c_id);
> >> +
> >> +static const struct of_device_id tps25990_of_match[] = {
> >> +	{ .compatible = "ti,tps25990" },
> >> +	{}
> >> +};
> >> +MODULE_DEVICE_TABLE(of, tps25990_of_match);
> >> +
> >> +static int tps25990_probe(struct i2c_client *client)
> >> +{
> >> +	struct device *dev = &client->dev;
> >> +	struct pmbus_driver_info *info;
> >> +	u32 rimon = TPS25990_DEFAULT_RIMON;
> >> +	int ret;
> >> +
> >> +	ret = device_property_read_u32(dev, "ti,rimon-milli-ohms", &rimon);
> >> +	if (ret == -EINVAL) {
> >> +		dev_warn(dev,
> >> +			 "using default rimon: current and power scale possibly wrong\n");
> >
> > This is not an appropriate warning. It is perfectly fine to load the driver
> > if there is no ti,rimon-milli-ohms property.
> 
> I should have commented more on the default value. It is meant for the
> case where the device is instanciated through i2c sys 'new_device',
> which is meant for debugging purpose. In that particular case, it does
> not really matter if the current and power scale are wrong.
> 
> There is no way to pass device properties when instanciating device
> through that interface, as far as I know. 
> 
> In every other cases, a correct Rimon value is expected.
> I could turn the above to an error. It means loading through i2c sys
> would not possible for this driver.
> 
> Would it be better ?
> 

We use default values for pretty much all drivers, so I don't see why
this one should be different. The driver should still be usable on a
system without devicetree support. There is a reason for the sensors
configuration file.

> >
> >> +	} else if (ret < 0) {
> >> +		return dev_err_probe(dev, ret, "failed get rimon\n");
> >> +	}
> >> +
> >> +	/*
> >> +	 * TPS25990 may be stacked with several TPS25895, allowing a higher
> >> +	 * current. The higher the allowed current is, the lower rimon
> >> +	 * will be. How low it can realistically get is unknown.
> >> +	 * To avoid problems with precision later on, rimon is provided in
> >> +	 * milli Ohms. This is a precaution to keep a stable ABI.
> >> +	 * At the moment, doing the calculation with rimon in milli Ohms
> >> +	 * would overflow the s32 'm' in the direct conversion. Convert it
> >> +	 * back to Ohms until greater precision is actually needed.
> >> +	 */
> >> +	rimon /= 1000;
> >> +
> >
> > Seems to me it would make more sense to limit the valid range of ti,rimon-milli-ohms
> > to avoid the overflow. But then I really don't understand the reasoning to provide
> > the property in milli-ohm, given the default value of 910 Ohm. What is a realistic
> > lowest value that would make sense ?
> 
> The highest value I've seen, when the tps25990 is alone, is 1370
> Ohms. That means a 30A overcurrent fault limit.
> 
> With one TPS25895, I've seen 608 Ohms (110A limit)
> 
> I have no idea what the realistic low limit is. To get to ~100 Ohms, you'd
> need 8 devices (not hundreds ;) ) If one gets there, it might be
> desirable to have 3 digits to play with, and not be limited by the unit.
> 
> The DT folks really don't like when a property changes. Going with
> milli-Ohms is way to anticipate the problem.
> 
> The other way could be to use Ohms now, and if we ever get to point
> where milli-Ohms precision is needed, add it then. The downside is that
> the driver will need to support both properties.
> 
> Would you prefer this ?
> 

In practice the driver, as submitted, does _not_ support milli-Ohms
to start with. It only supports Ohms. Worse, it doesn't range check the
value, causing bad behavior (everything will be reported 0) if a value
below 1,000 is provided, and still overflows if the value gets close to
UINT_MAX.

> 
> > But even if it is less than 1 Ohm I don't
> > understand why it would make sense to completely ignore it.
> 
> It would not make sense to ignore it.
> 
But you do ... by setting m to 0 in that case.

> >
> >> +	info = devm_kmemdup(dev, &tps25990_base_info, sizeof(*info), GFP_KERNEL);
> >> +	if (!info)
> >> +		return -ENOMEM;
> >> +
> >> +	/* Adapt the current and power scale for each instance */
> >> +	info->m[PSC_CURRENT_IN] *= rimon;
> >> +	info->m[PSC_POWER] *= rimon;
> >
> > Any rimon value < 1000 mOhm will result in m values of 0.
> 
> Indeed. Such Rimon value would mean an over current limit > 50kA. I admit
> I did really think much about such value.
> 
> The idea was more keep some precision if we get somewhere near a 100 Ohms.
> 

It doesn't, though, since the provided milli-Ohm value is divided by 1,000
(and the division doesn't even use DIV_ROUND_CLOSEST). Even though certain
values don't make sense, there still needs to be a range check. And that
missing range check triggers the next question: Why not just limit the upper
range instead of ignoring the milli-part of the value ?

Note that you might have used micro-Ohm (which is a standard devicetree
resolution) and divide it by 1,000. That would have been perfectly fine.
It would result in an upper resistor limit of 4,294 Ohm, which I'd assume
should be acceptable. Overflows due to large values of m could have been
avoided by adjusting .R if .m gets otherwise too large.

Guenter
Re: [PATCH 3/3] hwmon: (pmbus/tps25990): add initial support
Posted by Jerome Brunet 2 months, 2 weeks ago
On Tue 10 Sep 2024 at 10:07, Guenter Roeck <linux@roeck-us.net> wrote:

> On Tue, Sep 10, 2024 at 11:07:57AM +0200, Jerome Brunet wrote:
>> On Mon 09 Sep 2024 at 15:52, Guenter Roeck <linux@roeck-us.net> wrote:
>> 
>> [...]
>> 
> Unrelated to the other comments:
>
>  Documentation/hwmon/tps25990.rst | 141 ++++++++++++
>
> Needs to be added to Documentation/hwmon/index.rst.
>
> +config SENSORS_TPS25990_REGULATOR
> +	bool "Regulator support for TPS25990 and compatibles"
> +	depends on SENSORS_TPS25990 && REGULATOR
> +	default SENSORS_TPS2599
>                 ^^^^^^^^^^^^^^^ TPS2599 ???
>
>> >> +
>> >> +#define TPS25990_DEFAULT_RIMON		910000
>
> Where does the default come from anyway ? I don't immediately see the number
> in the datasheet.

It is Rimon value for the  maximum current supported when the TPS25990
is alone (60A) with Viref on its default value: 1V - Section 8.3.4.2.

There is no reason for it beside that. 

>
>> >> +static int tps25990_write_protect_get(void *data, u64 *val)
>> >> +{
>> >> +	struct i2c_client *client = data;
>> >> +
>> >> +	return tps25990_mfr_write_protect_active(client);
>> >> +}
>> >> +
>> >> +static int tps25990_write_protect_set(void *data, u64 val)
>> >> +{
>> >> +	struct i2c_client *client = data;
>> >> +
>> >> +	if (val > 1)
>> >> +		return -EINVAL;
>> >> +
>> >> +	return tps25990_mfr_write_protect(client, val);
>> >> +}
>> >> +
>> >> +DEFINE_DEBUGFS_ATTRIBUTE(tps25990_write_protect_fops,
>> >> +			 tps25990_write_protect_get,
>> >> +			 tps25990_write_protect_set,
>> >> +			 "%llu\n");
>> >> +
>> >> +static int tps25990_init_debugfs(struct i2c_client *client)
>> >> +{
>> >> +	struct dentry *dir;
>> >> +
>> >> +	dir = pmbus_get_debugfs_dir(client);
>> >> +	if (!dir)
>> >> +		return -ENOENT;
>> >> +
>> >> +	debugfs_create_file("write_protect", 0644, dir,
>> >> +			    client, &tps25990_write_protect_fops);
>> >> +
>> >> +	return 0;
>> >> +}
>> >> +
>> >> +#else
>> >> +static inline int tps25990_init_debugfs(struct i2c_client *client)
>> >> +{
>> >> +	return 0;
>> >> +}
>> >> +#endif
>> >> +
>> >
>> > In general it is extremely undesirable to overwrite write protection.
>> > Many chips support such attributes. If write protection is enabled,
>> > it means that the board vendor does not want to have them changed.
>> 
>> According to documentation, it protects against "unintented" writes,
>> not 'wrong' or 'malicious'. If one goes in debugfs and write just '0' to
>> a file, there is an intent at least.
>> 
>> > Granted, that can be overwritten with direct i2c commands, but that
>> > is what it should be. Anyone who really wants to disable write protection
>> > should have to dig deeper than just writing into a debugfs or sysfs attribute.
>> > Otherwise the protection becomes worthless.
>> > If this is, for example, needed
>> > for production to write initial settings, the production scripts should
>> > disable (or enable) write protection by writing directly into command
>> > registers.
>> 
>> As I wrote in the cover letter, the write protection is always active on
>> chip startup and it locks down almost everything, including things you may
>> need to write past production, in the field. The history reset below is
>> an example of such thing.
>> 
>> To 'safely' remove the protection by writing i2c commands from
>> userspace:
>>  * the device will need be unbinded first,
>>  * call i2cset
>>  * bind the device again
>> 
>> That seems really cumbersome to do something like an history
>> reset. Is this what you are suggesting ?
>> 
>> bind/unbind could be skipped by forcing i2cset but that would add danger
>> where we certainly don't want it.
>> 
>
> Not sure I understand the "danger" part. Either case, the problem is
> deeper.

If the driver is bound, i2cset will require the '-f' flag. Man page says
it is dangerous do so, if 2 i2c commands happens at the same time I suppose.

> The driver enables regulator support, which includes enabling and disabling
> the output voltage. But that doesn't work unles write protect is disabled.
> debugfs doesn't help there; that is way too late.

Indeed OPERATION command is locked as well, I missed that.
I'll drop that from the initial submission.

The fact that is comes too late is also why I did not add extra features
yet, things like GPIO support, GPDAC regulators, NVMEM blackbox, etc...

I know we are not supposed to (and never will) support all the shiny
features HW designers can think of, but it would be nice to unlock some
of its potential. 

Do you have an idea ? (for later I mean)
A module parm to do the unlock might work but seems a bit extreme.

>
>> >
>> >> +/*
>> >> + * TPS25990 has history reset based on MIN/AVG/PEAK instead of per sensor type
>> >> + * Emulate the behaviour a pmbus limit_attr would have for consistency
>> >> + *  - Read: Do nothing and emit 0
>> >> + *  - Write: Check the input is a number and reset
>> >> + */
>> >> +static ssize_t tps25990_history_reset_show(struct device *dev,
>> >> +					   struct device_attribute *devattr,
>> >> +					   char *buf)
>> >> +{
>> >> +	return sysfs_emit(buf, "0\n");
>> >> +}
>> >> +
>> >> +static ssize_t tps25990_history_reset_store(struct device *dev,
>> >> +					    struct device_attribute *devattr,
>> >> +					    const char *buf, size_t count)
>> >> +{
>> >> +	struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
>> >> +	struct i2c_client *client = to_i2c_client(dev->parent);
>> >> +	s64 val;
>> >> +	int ret;
>> >> +
>> >> +	if (kstrtos64(buf, 10, &val) < 0)
>> >> +		return -EINVAL;
>> >> +
>> >> +	ret = pmbus_update_byte_data(client, 0, TPS25990_PK_MIN_AVG,
>> >> +				     BIT(attr->index), BIT(attr->index));
>> >> +	if (ret < 0)
>> >> +		return ret;
>> >> +
>> >> +	return count;
>> >> +}
>> >> +
>> >> +static SENSOR_DEVICE_ATTR_RW(highest_history_reset, tps25990_history_reset, 7);
>> >> +static SENSOR_DEVICE_ATTR_RW(average_history_reset, tps25990_history_reset, 6);
>> >> +static SENSOR_DEVICE_ATTR_RW(lowest_history_reset,  tps25990_history_reset, 5);
>> >
>> > That is not a unique problem, and not a reason to introduce non-standard attributes.
>> > Just attach the attribute to the first channel and document that it resets all
>> > channels.
>> 
>> Not sure I got this right so I'll rephrase. I should:
>> * Pick a channel, say vin
>> * Map the virtual reset register to hit the 3 resets above
>> * Put in the documentation that it resets the other channels as well
>> * Not allow independent resets of min/max/avg, just all 3 together ?
>> 
> Correct. It is amazing what hardware designers come up with (here:
> resetting history based on min/max/average instead of the sensor type
> is novel), but I really don't want to introduce new attributes to
> accommodate each variant.

Sure. Make sense

> I'd be open to introducing a global
> PMBUS_VIRT_RESET_HISTORY virtual register and reset_history attribute
> if you want to go there, but that would have to be in the PMBus core.

Both solutions are fine by me.
Do you have a preference ?

>
>> >
>> >> +
>> >> +static struct attribute *tps25990_attrs[] = {
>> >> +	&sensor_dev_attr_highest_history_reset.dev_attr.attr,
>> >> +	&sensor_dev_attr_average_history_reset.dev_attr.attr,
>> >> +	&sensor_dev_attr_lowest_history_reset.dev_attr.attr,
>> >> +	NULL,
>> >> +};
>> >> +
>> >> +ATTRIBUTE_GROUPS(tps25990);
>> >> +
>> >> +static int tps25990_get_addr(int reg)
>> >> +{
>> >> +	switch (reg) {
>> >> +	case PMBUS_SMBALERT_MASK:
>> >> +		/*
>> >> +		 * Note: PMBUS_SMBALERT_MASK is not implemented on this chip
>> >> +		 * Writing to this address raises CML errors.
>> >> +		 * Instead it provides ALERT_MASK which allows to set the mask
>> >> +		 * for each of the status registers, but not the specific bits
>> >> +		 * in them.
>> >> +		 * The default setup assert SMBA# if any bit is set in any of the
>> >> +		 * status registers the chip has. This is as close as we can get
>> >> +		 * to what pmbus_irq_setup() would set, sooo ... do nothing.
>> >> +		 */
>> >> +		return -ENXIO;
>> >
>> > Many chips have that problem. The core code ignores errors, and attempts to write
>> > the command are limited to initialization. This is not a reason to overwrite
>> > the command like this. If this does cause a real a problem wit hthe chip (other
>> > than setting CML errors, which many chips not supporting the command do),
>> > we should define a flag in include/linux/pmbus.h and explain its need.
>> 
>> CML is error is the problem. Following pmbus_irq_setup() there is an
>> uncleared fault because there is no register check on PMBUS_SMBALERT_MASK.
>> 
>> When pmbus_core then gets here:
>> https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/drivers/hwmon/pmbus/pmbus_core.c?h=v6.11-rc7#n3386
>> 
>> pmbus_check_block_register() fails because of the uncleared fault and
>> the 'mfr_id' property is silently not registered, eventhough the
>> register is supported by the chip. This is how I noticed the problem.
>> 
>> So, should I add flag in include/linux/pmbus.h to skip
>> PMBUS_SMBALERT_MASK setup ?
>> 
>> Another possibility is to call register_check()
>> on it before using PMBUS_SMBALERT_MASK in pmbus_core.
>> 
>
> The problem, as you point out, is in pmbus_irq_setup(). Since the function
> explicitly ignores errors from accessing PMBUS_SMBALERT_MASK, it should
> either clear faults after it is done. I don't think we can rely on
> register_check() because the register might exist but be read-only.
>

Noted. I'll add the fault clearing.

>> >
>> >> +	case PMBUS_IIN_OC_FAULT_LIMIT:

[...]

>> >> +static int tps25990_probe(struct i2c_client *client)
>> >> +{
>> >> +	struct device *dev = &client->dev;
>> >> +	struct pmbus_driver_info *info;
>> >> +	u32 rimon = TPS25990_DEFAULT_RIMON;
>> >> +	int ret;
>> >> +
>> >> +	ret = device_property_read_u32(dev, "ti,rimon-milli-ohms", &rimon);
>> >> +	if (ret == -EINVAL) {
>> >> +		dev_warn(dev,
>> >> +			 "using default rimon: current and power scale possibly wrong\n");
>> >
>> > This is not an appropriate warning. It is perfectly fine to load the driver
>> > if there is no ti,rimon-milli-ohms property.
>> 
>> I should have commented more on the default value. It is meant for the
>> case where the device is instanciated through i2c sys 'new_device',
>> which is meant for debugging purpose. In that particular case, it does
>> not really matter if the current and power scale are wrong.
>> 
>> There is no way to pass device properties when instanciating device
>> through that interface, as far as I know. 
>> 
>> In every other cases, a correct Rimon value is expected.
>> I could turn the above to an error. It means loading through i2c sys
>> would not possible for this driver.
>> 
>> Would it be better ?
>> 
>
> We use default values for pretty much all drivers, so I don't see why
> this one should be different. The driver should still be usable on a
> system without devicetree support. There is a reason for the sensors
> configuration file.
>

Supporting more than DT is a concern. That is why I did not use the DT
specific API. In theory, the one used should support other sources, such
as ACPI, I think.

Thanks for pointing out the sensor configuration file. I did not know
calculation were possible, and acceptable, at that stage.

So, IIUC, I could just drop the device property, then the device would
be used in the same way on all the platform, DT or not ?
I like that a lot.

All I would have to do is add something in the documentation about it, I
guess. With default value of 1000, instead 910, the range would still be
good in sysfs and calculation simple in userspace.

That would solve the unit discussion as well, a nice bonus ;)

>> >
>> >> +	} else if (ret < 0) {
>> >> +		return dev_err_probe(dev, ret, "failed get rimon\n");
>> >> +	}
>> >> +
>> >> +	/*
>> >> +	 * TPS25990 may be stacked with several TPS25895, allowing a higher
>> >> +	 * current. The higher the allowed current is, the lower rimon
>> >> +	 * will be. How low it can realistically get is unknown.
>> >> +	 * To avoid problems with precision later on, rimon is provided in
>> >> +	 * milli Ohms. This is a precaution to keep a stable ABI.
>> >> +	 * At the moment, doing the calculation with rimon in milli Ohms
>> >> +	 * would overflow the s32 'm' in the direct conversion. Convert it
>> >> +	 * back to Ohms until greater precision is actually needed.
>> >> +	 */
>> >> +	rimon /= 1000;
>> >> +
>> >
>> > Seems to me it would make more sense to limit the valid range of ti,rimon-milli-ohms
>> > to avoid the overflow. But then I really don't understand the reasoning to provide
>> > the property in milli-ohm, given the default value of 910 Ohm. What is a realistic
>> > lowest value that would make sense ?
>> 
>> The highest value I've seen, when the tps25990 is alone, is 1370
>> Ohms. That means a 30A overcurrent fault limit.
>> 
>> With one TPS25895, I've seen 608 Ohms (110A limit)
>> 
>> I have no idea what the realistic low limit is. To get to ~100 Ohms, you'd
>> need 8 devices (not hundreds ;) ) If one gets there, it might be
>> desirable to have 3 digits to play with, and not be limited by the unit.
>> 
>> The DT folks really don't like when a property changes. Going with
>> milli-Ohms is way to anticipate the problem.
>> 
>> The other way could be to use Ohms now, and if we ever get to point
>> where milli-Ohms precision is needed, add it then. The downside is that
>> the driver will need to support both properties.
>> 
>> Would you prefer this ?
>> 
>
> In practice the driver, as submitted, does _not_ support milli-Ohms
> to start with. It only supports Ohms. Worse, it doesn't range check the
> value, causing bad behavior (everything will be reported 0) if a value
> below 1,000 is provided, and still overflows if the value gets close to
> UINT_MAX.

Yes I relied on the value being sane-ish. 4 MOhms (or even 2) is not. 

>
>> 
>> > But even if it is less than 1 Ohm I don't
>> > understand why it would make sense to completely ignore it.
>> 
>> It would not make sense to ignore it.
>> 
> But you do ... by setting m to 0 in that case.

I did not intentionally set 0. Let's just call it for what it is: a
bug that needs fixing, if the property stays.

>
>> >
>> >> +	info = devm_kmemdup(dev, &tps25990_base_info, sizeof(*info), GFP_KERNEL);
>> >> +	if (!info)
>> >> +		return -ENOMEM;
>> >> +
>> >> +	/* Adapt the current and power scale for each instance */
>> >> +	info->m[PSC_CURRENT_IN] *= rimon;
>> >> +	info->m[PSC_POWER] *= rimon;
>> >
>> > Any rimon value < 1000 mOhm will result in m values of 0.
>> 
>> Indeed. Such Rimon value would mean an over current limit > 50kA. I admit
>> I did really think much about such value.
>> 
>> The idea was more keep some precision if we get somewhere near a 100 Ohms.
>> 
>
> It doesn't, though, since the provided milli-Ohm value is divided by 1,000
> (and the division doesn't even use DIV_ROUND_CLOSEST). Even though certain
> values don't make sense, there still needs to be a range check. And that
> missing range check triggers the next question: Why not just limit the upper
> range instead of ignoring the milli-part of the value ?

If there was an actual range in the documentation, I'd be happy to check
it, but there is not one. I don't think I should make a range out of
thin air.

That being said, we are not going get into Mega-Ohms or Micro-Ohms
territory with this chip, that's for sure. I was trying to strike a
balance in between.

The upper limit of Rimon would be set by what you consider the lower
acceptable limit for over current protection (Iocp). Isn't 10A ? or 5A
or even 1A ? I don't know. I'm not sure the driver should disallow any
sane value if the HW can do it.

What is sane then ...

>
> Note that you might have used micro-Ohm (which is a standard devicetree
> resolution) and divide it by 1,000.

Take Rimon = 1370 Ohms, which provide an Iocp of 30A.
Micro-Ohms divided by 1000: 13700000000 / 1000 = 1370000.
For current: m = 9538 * 1370000 = 13067060000

This will overflow m on 32bits systems for struct pmbus_driver_info.
In pmbus_reg2data_direct, m is an s32 so it would overflow there as
well regardless of the arch.

Micro or milli-Ohms, the matter is the same. Solution might simply be to
do m calculation over 64bits then divide result so it fits the 32bits
type used in pmbus_core.

> That would have been perfectly fine.
> It would result in an upper resistor limit of 4,294 Ohm, which I'd assume
> should be acceptable.

Means the driver will not allow a Iocp lower that 234mA.
It is probably realistic.

> Overflows due to large values of m could have been
> avoided by adjusting .R if .m gets otherwise too large.

I think we are down 2 solutions then:
1) Drop the device property completely, use a sane default and rely on
   libsensor for the final calculation.
2) Property in micro-ohms, with some calculation to fit m in 32bits.
   Probably need to expose Rimon in debugfs too, so the users may check
   the value used and revert back to lib-sensor calc if necessary.

I tend to prefer 1) for its simplicity and lack of added constraints.
Is it Ok with you ?

>
> Guenter

-- 
Jerome