Skip to contentSkip to conversation

hwmon: (pmbus) Add support for custom register to data conversion

PATCH In review Vadim Pasternak 2 participants 6 messages Feb 24, 2020 Hardware monitoring

lore
Share & tools
lore
#0 lore raw

cover hwmon: (pmbus) Add support for custom register to data conversion

Currently there is an assumption that 'input', 'limits' and 'history' registers use the same conversion format within the sensor class. However some devices could be configured with different format for reading 'input' and 'limits' or 'history' registers.

Patch#1 adds reg2data() callback.
Patch#2 utilizes this callback for 'xdpe12284' driver.

Vadim Pasternak (2):
hwmon: (pmbus/core) Add callback for register to data conversion
hwmon: (pmbus/xdpe12284) Add custom format for vout limits conversion

 drivers/hwmon/pmbus/pmbus.h      |  1 +
 drivers/hwmon/pmbus/pmbus_core.c |  4 ++++
 drivers/hwmon/pmbus/xdpe12284.c  | 23 +++++++++++++++++++++++
 3 files changed, 28 insertions(+)
Signature · 1 line
2.11.0
#1 lore raw

PATCH 1/2 hwmon: (pmbus/core) Add callback for register to data conversion

Add callback reg2data() to allow device specific formatting for particular registers.
Currently there is an assumption that 'limits' and 'history' registers are inherited conversion format from the relevant sensor class. But there are some devices, which for example, provides 'vout' register reading in 'vid' format, while 'limits' or 'history' registers for 'vout' are provided in linear format.

---
 drivers/hwmon/pmbus/pmbus.h      | 1 +
 drivers/hwmon/pmbus/pmbus_core.c | 4 ++++
 2 files changed, 5 insertions(+)
drivers/hwmon/pmbus/pmbus.h +1 -0
index 18e06fc6c53f..7acd75303c7f 100644
@@ -427,6 +427,7 @@ struct pmbus_driver_info {
 	int (*write_word_data)(struct i2c_client *client, int page, int reg,
 			       u16 word);
 	int (*write_byte)(struct i2c_client *client, int page, u8 value);
+	int (*reg2data)(u16 reg, int data, long *val);
 	/*
 	 * The identify function determines supported PMBus functionality.
 	 * This function is only necessary if a chip driver supports multiple
drivers/hwmon/pmbus/pmbus_core.c +4 -0
index 8d321bf7d15b..e1f88d362549 100644
@@ -747,6 +747,10 @@ static long pmbus_reg2data(struct pmbus_data *data, struct pmbus_sensor *sensor)
 	if (!sensor->convert)
 		return sensor->data;
 
+	if (data->info->reg2data &&
+	    data->info->reg2data(sensor->reg, sensor->data, &val) != -ENODATA)
+		return val;
+
 	switch (data->info->format[sensor->class]) {
 	case direct:
 		val = pmbus_reg2data_direct(data, sensor);
Signature · 1 line
2.11.0
#2 lore raw

PATCH 2/2 hwmon: (pmbus/xdpe12284) Add custom format for vout limits conversion

In reply to Vadim Pasternak

Provide callback for overvoltage and undervoltage output readouts conversion. These registers are presented in 'slinear11' format, while default conversion for 'vout' class for the devices is 'vid'. It is resulted in wrong conversion in pmbus_reg2data() for in{3-4}_lcrit and in{3-4}_crit attributes.

---
 drivers/hwmon/pmbus/xdpe12284.c | 23 +++++++++++++++++++++++
 1 file changed, 23 insertions(+)
drivers/hwmon/pmbus/xdpe12284.c +23 -0
index ecd9b65627ec..751c8e18c881 100644
@@ -18,6 +18,28 @@
 #define XDPE122_AMD_625MV		0x10 /* AMD mode 6.25mV */
 #define XDPE122_PAGE_NUM		2
 
+static int xdpe122_reg2data(u16 reg, int data, long *val)
+{
+	s16 exponent;
+	s32 mantissa;
+
+	switch (reg) {
+	case PMBUS_VOUT_OV_FAULT_LIMIT:
+	case PMBUS_VOUT_UV_FAULT_LIMIT:
+		/* Convert to LINEAR11. */
+		exponent = ((s16)data) >> 11;
+		mantissa = ((s16)((data & GENMASK(10, 0)) << 5)) >> 5;
+		*val = mantissa * 1000L;
+		if (exponent >= 0)
+			*val <<= exponent;
+		else
+			*val >>= -exponent;
+		return 0;
+	default:
+		return -ENODATA;
+	}
+}
+
 static int xdpe122_identify(struct i2c_client *client,
 			    struct pmbus_driver_info *info)
 {
@@ -70,6 +92,7 @@ static struct pmbus_driver_info xdpe122_info = {
 		PMBUS_HAVE_TEMP | PMBUS_HAVE_STATUS_TEMP |
 		PMBUS_HAVE_POUT | PMBUS_HAVE_PIN | PMBUS_HAVE_STATUS_INPUT,
 	.identify = xdpe122_identify,
+	.reg2data = xdpe122_reg2data,
 };
 
 static int xdpe122_probe(struct i2c_client *client,
Signature · 1 line
2.11.0
maintainer
#3 lore raw

Re: [hwmon-next v1 2/2] hwmon: (pmbus/xdpe12284) Add custom format for vout limits conversion

Quoted 17 lines · Vadim Pasternak

Provide callback for overvoltage and undervoltage output readouts conversion. These registers are presented in 'slinear11' format, while default conversion for 'vout' class for the devices is 'vid'. It is resulted in wrong conversion in pmbus_reg2data() for in{3-4}_lcrit and in{3-4}_crit attributes.

---
  drivers/hwmon/pmbus/xdpe12284.c | 23 +++++++++++++++++++++++
  1 file changed, 23 insertions(+)
drivers/hwmon/pmbus/xdpe12284.c +0 -0
index ecd9b65627ec..751c8e18c881 100644
@@ -18,6 +18,28 @@
  #define XDPE122_AMD_625MV		0x10 /* AMD mode 6.25mV */

drivers/hwmon/pmbus/xdpe12284.c line 28

Quoted 10 lines
drivers/hwmon/pmbus/xdpe12284.c +8 -0
  #define XDPE122_PAGE_NUM		2
 
+static int xdpe122_reg2data(u16 reg, int data, long *val)
+{
+	s16 exponent;
+	s32 mantissa;
+
+	switch (reg) {
+	case PMBUS_VOUT_OV_FAULT_LIMIT:
+	case PMBUS_VOUT_UV_FAULT_LIMIT:

Another situation where having a datasheet would be quite valuable.

I would suggest to implement reading those two registers locally and convert them to the expected data format. That seems to be more straightforward than re-implementing slinear conversion.

Thanks,
Guenter

Quoted 26 lines
drivers/hwmon/pmbus/xdpe12284.c +15 -0
+		/* Convert to LINEAR11. */
+		exponent = ((s16)data) >> 11;
+		mantissa = ((s16)((data & GENMASK(10, 0)) << 5)) >> 5;
+		*val = mantissa * 1000L;
+		if (exponent >= 0)
+			*val <<= exponent;
+		else
+			*val >>= -exponent;
+		return 0;
+	default:
+		return -ENODATA;
+	}
+}
+
  static int xdpe122_identify(struct i2c_client *client,
  			    struct pmbus_driver_info *info)
  {
@@ -70,6 +92,7 @@ static struct pmbus_driver_info xdpe122_info = {
  		PMBUS_HAVE_TEMP | PMBUS_HAVE_STATUS_TEMP |
  		PMBUS_HAVE_POUT | PMBUS_HAVE_PIN | PMBUS_HAVE_STATUS_INPUT,
  	.identify = xdpe122_identify,
+	.reg2data = xdpe122_reg2data,
  };
 
  static int xdpe122_probe(struct i2c_client *client,
#4 lore raw

RE: [hwmon-next v1 2/2] hwmon: (pmbus/xdpe12284) Add custom format for vout limits conversion

Quoted 41 lines

-----Original Message-----
From: Guenter Roeck <[email protected]> On Behalf Of Guenter Roeck
Sent: Monday, February 24, 2020 4:27 PM
To: Vadim Pasternak <[email protected]>
Cc: [email protected]
Subject: Re: [hwmon-next v1 2/2] hwmon: (pmbus/xdpe12284) Add custom format for vout limits conversion

Quoted 17 lines · Vadim Pasternak

Provide callback for overvoltage and undervoltage output readouts conversion. These registers are presented in 'slinear11' format, while default conversion for 'vout' class for the devices is 'vid'. It is resulted in wrong conversion in pmbus_reg2data() for in{3-4}_lcrit and in{3-4}_crit attributes.

---
  drivers/hwmon/pmbus/xdpe12284.c | 23 +++++++++++++++++++++++
  1 file changed, 23 insertions(+)
(quoted diff) +0 -0

b/drivers/hwmon/pmbus/xdpe12284.c index ecd9b65627ec..751c8e18c881 100644

drivers/hwmon/pmbus/xdpe12284.c line 27

Quoted 10 lines
drivers/hwmon/pmbus/xdpe12284.c +7 -0
@@ -18,6 +18,28 @@
  #define XDPE122_AMD_625MV		0x10 /* AMD mode 6.25mV */
  #define XDPE122_PAGE_NUM		2
 
+static int xdpe122_reg2data(u16 reg, int data, long *val) {
+	s16 exponent;
+	s32 mantissa;
+
+	switch (reg) {
+	case PMBUS_VOUT_OV_FAULT_LIMIT:
+	case PMBUS_VOUT_UV_FAULT_LIMIT:

Another situation where having a datasheet would be quite valuable.

Quoted 3 lines

I would suggest to implement reading those two registers locally and convert them to the expected data format. That seems to be more straightforward than re-implementing slinear conversion.

Hi Guenter,

Do you mean to implement them locally through the virtual registers?
case PMBUS_VIRT_VMON_OV_FAULT_LIMIT:
case PMBUS_VIRT_VMON_UV_FAULT_LIMIT:

Thanks,
Vadim.

Quoted 31 lines

Thanks,
Guenter

Quoted 18 lines
drivers/hwmon/pmbus/xdpe12284.c +14 -0
+		/* Convert to LINEAR11. */
+		exponent = ((s16)data) >> 11;
+		mantissa = ((s16)((data & GENMASK(10, 0)) << 5)) >> 5;
+		*val = mantissa * 1000L;
+		if (exponent >= 0)
+			*val <<= exponent;
+		else
+			*val >>= -exponent;
+		return 0;
+	default:
+		return -ENODATA;
+	}
+}
+
  static int xdpe122_identify(struct i2c_client *client,
  			    struct pmbus_driver_info *info)
  {

drivers/hwmon/pmbus/xdpe12284.c static struct pmbus_driver_info xdpe122_info = { · line 93

Quoted 2 lines
drivers/hwmon/pmbus/xdpe12284.c +0 -0
@@ -70,6 +92,7 @@ static struct pmbus_driver_info xdpe122_info = {
  		PMBUS_HAVE_TEMP | PMBUS_HAVE_STATUS_TEMP |
  		PMBUS_HAVE_POUT | PMBUS_HAVE_PIN |

PMBUS_HAVE_STATUS_INPUT,

Quoted 6 lines
drivers/hwmon/pmbus/xdpe12284.c +1 -0
  	.identify = xdpe122_identify,
+	.reg2data = xdpe122_reg2data,
  };
 
  static int xdpe122_probe(struct i2c_client *client,
maintainer
#5 lore raw

Re: [hwmon-next v1 2/2] hwmon: (pmbus/xdpe12284) Add custom format for vout limits conversion

Quoted 45 lines · Vadim Pasternak
Quoted 41 lines

-----Original Message-----
From: Guenter Roeck <[email protected]> On Behalf Of Guenter Roeck
Sent: Monday, February 24, 2020 4:27 PM
To: Vadim Pasternak <[email protected]>
Cc: [email protected]
Subject: Re: [hwmon-next v1 2/2] hwmon: (pmbus/xdpe12284) Add custom format for vout limits conversion

Quoted 17 lines · Vadim Pasternak

Provide callback for overvoltage and undervoltage output readouts conversion. These registers are presented in 'slinear11' format, while default conversion for 'vout' class for the devices is 'vid'. It is resulted in wrong conversion in pmbus_reg2data() for in{3-4}_lcrit and in{3-4}_crit attributes.

---
   drivers/hwmon/pmbus/xdpe12284.c | 23 +++++++++++++++++++++++
   1 file changed, 23 insertions(+)
(quoted diff) +0 -0

b/drivers/hwmon/pmbus/xdpe12284.c index ecd9b65627ec..751c8e18c881 100644

drivers/hwmon/pmbus/xdpe12284.c line 27

Quoted 10 lines
drivers/hwmon/pmbus/xdpe12284.c +7 -0
@@ -18,6 +18,28 @@
   #define XDPE122_AMD_625MV		0x10 /* AMD mode 6.25mV */
   #define XDPE122_PAGE_NUM		2
 
+static int xdpe122_reg2data(u16 reg, int data, long *val) {
+	s16 exponent;
+	s32 mantissa;
+
+	switch (reg) {
+	case PMBUS_VOUT_OV_FAULT_LIMIT:
+	case PMBUS_VOUT_UV_FAULT_LIMIT:

Another situation where having a datasheet would be quite valuable.

Quoted 3 lines

I would suggest to implement reading those two registers locally and convert them to the expected data format. That seems to be more straightforward than re-implementing slinear conversion.

Quoted 6 lines

Hi Guenter,

Do you mean to implement them locally through the virtual registers?
case PMBUS_VIRT_VMON_OV_FAULT_LIMIT:
case PMBUS_VIRT_VMON_UV_FAULT_LIMIT:

No, just implement reading PMBUS_VOUT_OV_FAULT_LIMIT and PMBUS_VOUT_UV_FAULT_LIMIT locally, or in other words intercept it in a read_word_data function.

Thanks,
Guenter

Quoted 35 lines

Thanks,
Vadim.

Quoted 31 lines

Thanks,
Guenter

Quoted 18 lines
drivers/hwmon/pmbus/xdpe12284.c +14 -0
+		/* Convert to LINEAR11. */
+		exponent = ((s16)data) >> 11;
+		mantissa = ((s16)((data & GENMASK(10, 0)) << 5)) >> 5;
+		*val = mantissa * 1000L;
+		if (exponent >= 0)
+			*val <<= exponent;
+		else
+			*val >>= -exponent;
+		return 0;
+	default:
+		return -ENODATA;
+	}
+}
+
   static int xdpe122_identify(struct i2c_client *client,
   			    struct pmbus_driver_info *info)
   {

drivers/hwmon/pmbus/xdpe12284.c static struct pmbus_driver_info xdpe122_info = { · line 93

Quoted 2 lines
drivers/hwmon/pmbus/xdpe12284.c +0 -0
@@ -70,6 +92,7 @@ static struct pmbus_driver_info xdpe122_info = {
   		PMBUS_HAVE_TEMP | PMBUS_HAVE_STATUS_TEMP |
   		PMBUS_HAVE_POUT | PMBUS_HAVE_PIN |

PMBUS_HAVE_STATUS_INPUT,

Quoted 6 lines
drivers/hwmon/pmbus/xdpe12284.c +1 -0
   	.identify = xdpe122_identify,
+	.reg2data = xdpe122_reg2data,
   };
 
   static int xdpe122_probe(struct i2c_client *client,