linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] hwmon: pmbus: max20730: adjust the vout reading given voltage divider
@ 2020-09-10 17:08 Chu Lin
  2020-09-10 19:05 ` Guenter Roeck
  0 siblings, 1 reply; 4+ messages in thread
From: Chu Lin @ 2020-09-10 17:08 UTC (permalink / raw)
  To: linchuyuan, jdelvare, linux, linux-kernel, linux-hwmon,
	qiongwang, zhongqil, jasonling, belgaied

Problem:
We use voltage dividers so that the voltage presented at the voltage
sense pins is confusing. We might need to convert these readings to more
meaningful readings given the voltage divider.

Solution:
Read the voltage divider resistance from dts and convert the voltage
reading to a more meaningful reading.

Testing:
max20730 with voltage divider

Signed-off-by: Chu Lin <linchuyuan@google.com>
---
 drivers/hwmon/pmbus/max20730.c | 16 ++++++++++++++++
 1 file changed, 16 insertions(+)

diff --git a/drivers/hwmon/pmbus/max20730.c b/drivers/hwmon/pmbus/max20730.c
index c0bb05487e0e..4b56810fa894 100644
--- a/drivers/hwmon/pmbus/max20730.c
+++ b/drivers/hwmon/pmbus/max20730.c
@@ -29,6 +29,7 @@ struct max20730_data {
 	struct pmbus_driver_info info;
 	struct mutex lock;	/* Used to protect against parallel writes */
 	u16 mfr_devset1;
+	u32 vout_voltage_divider[2];
 };
 
 #define to_max20730_data(x)  container_of(x, struct max20730_data, info)
@@ -111,6 +112,12 @@ static int max20730_read_word_data(struct i2c_client *client, int page,
 		max_c = max_current[data->id][(data->mfr_devset1 >> 5) & 0x3];
 		ret = val_to_direct(max_c, PSC_CURRENT_OUT, info);
 		break;
+	case PMBUS_READ_VOUT:
+		ret = pmbus_read_word_data(client, page, phase, reg);
+		if (data->vout_voltage_divider[0] && data->vout_voltage_divider[1])
+			ret = DIV_ROUND_CLOSEST(ret * data->vout_voltage_divider[1],
+						data->vout_voltage_divider[0]);
+		break;
 	default:
 		ret = -ENODATA;
 		break;
@@ -329,6 +336,15 @@ static int max20730_probe(struct i2c_client *client,
 	data->id = chip_id;
 	mutex_init(&data->lock);
 	memcpy(&data->info, &max20730_info[chip_id], sizeof(data->info));
+	if (of_property_read_u32_array(client->dev.of_node, "vout-voltage-divider",
+				       data->vout_voltage_divider,
+				       ARRAY_SIZE(data->vout_voltage_divider)) != 0)
+		memset(data->vout_voltage_divider, 0, sizeof(data->vout_voltage_divider));
+	if (data->vout_voltage_divider[1] < data->vout_voltage_divider[0]) {
+		dev_err(dev,
+			"The total resistance of voltage divider is less than output resistance\n");
+		return -ENODEV;
+	}
 
 	ret = i2c_smbus_read_word_data(client, MAX20730_MFR_DEVSET1);
 	if (ret < 0)
-- 
2.28.0.526.ge36021eeef-goog


^ permalink raw reply related	[flat|nested] 4+ messages in thread

* Re: [PATCH] hwmon: pmbus: max20730: adjust the vout reading given voltage divider
  2020-09-10 17:08 [PATCH] hwmon: pmbus: max20730: adjust the vout reading given voltage divider Chu Lin
@ 2020-09-10 19:05 ` Guenter Roeck
  0 siblings, 0 replies; 4+ messages in thread
From: Guenter Roeck @ 2020-09-10 19:05 UTC (permalink / raw)
  To: Chu Lin, jdelvare, linux-kernel, linux-hwmon, qiongwang,
	zhongqil, jasonling, belgaied

On 9/10/20 10:08 AM, Chu Lin wrote:
> Problem:
> We use voltage dividers so that the voltage presented at the voltage
> sense pins is confusing. We might need to convert these readings to more
> meaningful readings given the voltage divider.
> 
> Solution:
> Read the voltage divider resistance from dts and convert the voltage
> reading to a more meaningful reading.
> 
> Testing:
> max20730 with voltage divider
> 
> Signed-off-by: Chu Lin <linchuyuan@google.com>
> ---
>  drivers/hwmon/pmbus/max20730.c | 16 ++++++++++++++++
>  1 file changed, 16 insertions(+)
> 
> diff --git a/drivers/hwmon/pmbus/max20730.c b/drivers/hwmon/pmbus/max20730.c
> index c0bb05487e0e..4b56810fa894 100644
> --- a/drivers/hwmon/pmbus/max20730.c
> +++ b/drivers/hwmon/pmbus/max20730.c
> @@ -29,6 +29,7 @@ struct max20730_data {
>  	struct pmbus_driver_info info;
>  	struct mutex lock;	/* Used to protect against parallel writes */
>  	u16 mfr_devset1;
> +	u32 vout_voltage_divider[2];
>  };
>  
>  #define to_max20730_data(x)  container_of(x, struct max20730_data, info)
> @@ -111,6 +112,12 @@ static int max20730_read_word_data(struct i2c_client *client, int page,
>  		max_c = max_current[data->id][(data->mfr_devset1 >> 5) & 0x3];
>  		ret = val_to_direct(max_c, PSC_CURRENT_OUT, info);
>  		break;
> +	case PMBUS_READ_VOUT:
> +		ret = pmbus_read_word_data(client, page, phase, reg);

		if (ret < 0)
			return ret;

> +		if (data->vout_voltage_divider[0] && data->vout_voltage_divider[1])
> +			ret = DIV_ROUND_CLOSEST(ret * data->vout_voltage_divider[1],
> +						data->vout_voltage_divider[0]);

With no range checks, ret * data->vout_voltage_divider[1] can
easily overflow if vout_voltage_divider[] is larger than 0xffff.
Also, the pmbus core does not expect register values larger than 0xffff
(it typecasts the returned value explicitly to u16).

[ Grumble. The PMBus specification has VOUT_SCALE commands to handle
  this situation. It is quite annoying that this chip does not support it. ]

Guenter

> +		break;
>  	default:
>  		ret = -ENODATA;
>  		break;
> @@ -329,6 +336,15 @@ static int max20730_probe(struct i2c_client *client,
>  	data->id = chip_id;
>  	mutex_init(&data->lock);
>  	memcpy(&data->info, &max20730_info[chip_id], sizeof(data->info));
> +	if (of_property_read_u32_array(client->dev.of_node, "vout-voltage-divider",
> +				       data->vout_voltage_divider,
> +				       ARRAY_SIZE(data->vout_voltage_divider)) != 0)
> +		memset(data->vout_voltage_divider, 0, sizeof(data->vout_voltage_divider));
> +	if (data->vout_voltage_divider[1] < data->vout_voltage_divider[0]) {
> +		dev_err(dev,
> +			"The total resistance of voltage divider is less than output resistance\n");
> +		return -ENODEV;
> +	}
>  
>  	ret = i2c_smbus_read_word_data(client, MAX20730_MFR_DEVSET1);
>  	if (ret < 0)
> 


^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH] hwmon: pmbus: max20730: adjust the vout reading given voltage divider
  2020-09-10 17:09 Chu Lin
@ 2020-09-10 17:20 ` Chu Lin
  0 siblings, 0 replies; 4+ messages in thread
From: Chu Lin @ 2020-09-10 17:20 UTC (permalink / raw)
  To: Chu Lin, jdelvare, Guenter Roeck, linux-kernel, linux-hwmon,
	Qiong Wang, Zhongqi Li, Jason Ling, Kais Belgaied

The same patch got sent twice. Please, discard this one.

Thanks,
Chu

On Thu, Sep 10, 2020 at 10:09 AM Chu Lin <linchuyuan@google.com> wrote:
>
> Problem:
> We use voltage dividers so that the voltage presented at the voltage
> sense pins is confusing. We might need to convert these readings to more
> meaningful readings given the voltage divider.
>
> Solution:
> Read the voltage divider resistance from dts and convert the voltage
> reading to a more meaningful reading.
>
> Testing:
> max20730 with voltage divider
>
> Signed-off-by: Chu Lin <linchuyuan@google.com>
> ---
>  drivers/hwmon/pmbus/max20730.c | 16 ++++++++++++++++
>  1 file changed, 16 insertions(+)
>
> diff --git a/drivers/hwmon/pmbus/max20730.c b/drivers/hwmon/pmbus/max20730.c
> index c0bb05487e0e..4b56810fa894 100644
> --- a/drivers/hwmon/pmbus/max20730.c
> +++ b/drivers/hwmon/pmbus/max20730.c
> @@ -29,6 +29,7 @@ struct max20730_data {
>         struct pmbus_driver_info info;
>         struct mutex lock;      /* Used to protect against parallel writes */
>         u16 mfr_devset1;
> +       u32 vout_voltage_divider[2];
>  };
>
>  #define to_max20730_data(x)  container_of(x, struct max20730_data, info)
> @@ -111,6 +112,12 @@ static int max20730_read_word_data(struct i2c_client *client, int page,
>                 max_c = max_current[data->id][(data->mfr_devset1 >> 5) & 0x3];
>                 ret = val_to_direct(max_c, PSC_CURRENT_OUT, info);
>                 break;
> +       case PMBUS_READ_VOUT:
> +               ret = pmbus_read_word_data(client, page, phase, reg);
> +               if (data->vout_voltage_divider[0] && data->vout_voltage_divider[1])
> +                       ret = DIV_ROUND_CLOSEST(ret * data->vout_voltage_divider[1],
> +                                               data->vout_voltage_divider[0]);
> +               break;
>         default:
>                 ret = -ENODATA;
>                 break;
> @@ -329,6 +336,15 @@ static int max20730_probe(struct i2c_client *client,
>         data->id = chip_id;
>         mutex_init(&data->lock);
>         memcpy(&data->info, &max20730_info[chip_id], sizeof(data->info));
> +       if (of_property_read_u32_array(client->dev.of_node, "vout-voltage-divider",
> +                                      data->vout_voltage_divider,
> +                                      ARRAY_SIZE(data->vout_voltage_divider)) != 0)
> +               memset(data->vout_voltage_divider, 0, sizeof(data->vout_voltage_divider));
> +       if (data->vout_voltage_divider[1] < data->vout_voltage_divider[0]) {
> +               dev_err(dev,
> +                       "The total resistance of voltage divider is less than output resistance\n");
> +               return -ENODEV;
> +       }
>
>         ret = i2c_smbus_read_word_data(client, MAX20730_MFR_DEVSET1);
>         if (ret < 0)
> --
> 2.28.0.526.ge36021eeef-goog
>

^ permalink raw reply	[flat|nested] 4+ messages in thread

* [PATCH] hwmon: pmbus: max20730: adjust the vout reading given voltage divider
@ 2020-09-10 17:09 Chu Lin
  2020-09-10 17:20 ` Chu Lin
  0 siblings, 1 reply; 4+ messages in thread
From: Chu Lin @ 2020-09-10 17:09 UTC (permalink / raw)
  To: linchuyuan, jdelvare, linux, linux-kernel, linux-hwmon,
	qiongwang, zhongqil, jasonling, belgaied

Problem:
We use voltage dividers so that the voltage presented at the voltage
sense pins is confusing. We might need to convert these readings to more
meaningful readings given the voltage divider.

Solution:
Read the voltage divider resistance from dts and convert the voltage
reading to a more meaningful reading.

Testing:
max20730 with voltage divider

Signed-off-by: Chu Lin <linchuyuan@google.com>
---
 drivers/hwmon/pmbus/max20730.c | 16 ++++++++++++++++
 1 file changed, 16 insertions(+)

diff --git a/drivers/hwmon/pmbus/max20730.c b/drivers/hwmon/pmbus/max20730.c
index c0bb05487e0e..4b56810fa894 100644
--- a/drivers/hwmon/pmbus/max20730.c
+++ b/drivers/hwmon/pmbus/max20730.c
@@ -29,6 +29,7 @@ struct max20730_data {
 	struct pmbus_driver_info info;
 	struct mutex lock;	/* Used to protect against parallel writes */
 	u16 mfr_devset1;
+	u32 vout_voltage_divider[2];
 };
 
 #define to_max20730_data(x)  container_of(x, struct max20730_data, info)
@@ -111,6 +112,12 @@ static int max20730_read_word_data(struct i2c_client *client, int page,
 		max_c = max_current[data->id][(data->mfr_devset1 >> 5) & 0x3];
 		ret = val_to_direct(max_c, PSC_CURRENT_OUT, info);
 		break;
+	case PMBUS_READ_VOUT:
+		ret = pmbus_read_word_data(client, page, phase, reg);
+		if (data->vout_voltage_divider[0] && data->vout_voltage_divider[1])
+			ret = DIV_ROUND_CLOSEST(ret * data->vout_voltage_divider[1],
+						data->vout_voltage_divider[0]);
+		break;
 	default:
 		ret = -ENODATA;
 		break;
@@ -329,6 +336,15 @@ static int max20730_probe(struct i2c_client *client,
 	data->id = chip_id;
 	mutex_init(&data->lock);
 	memcpy(&data->info, &max20730_info[chip_id], sizeof(data->info));
+	if (of_property_read_u32_array(client->dev.of_node, "vout-voltage-divider",
+				       data->vout_voltage_divider,
+				       ARRAY_SIZE(data->vout_voltage_divider)) != 0)
+		memset(data->vout_voltage_divider, 0, sizeof(data->vout_voltage_divider));
+	if (data->vout_voltage_divider[1] < data->vout_voltage_divider[0]) {
+		dev_err(dev,
+			"The total resistance of voltage divider is less than output resistance\n");
+		return -ENODEV;
+	}
 
 	ret = i2c_smbus_read_word_data(client, MAX20730_MFR_DEVSET1);
 	if (ret < 0)
-- 
2.28.0.526.ge36021eeef-goog


^ permalink raw reply related	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2020-09-10 19:08 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-09-10 17:08 [PATCH] hwmon: pmbus: max20730: adjust the vout reading given voltage divider Chu Lin
2020-09-10 19:05 ` Guenter Roeck
2020-09-10 17:09 Chu Lin
2020-09-10 17:20 ` Chu Lin

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).