diff mbox series

[v2,2/2] hwmon: powr1220: Add support for Lattice's POWR1014 power manager IC

Message ID 20220111173239.21006-3-michaelsh@nvidia.com (mailing list archive)
State Changes Requested
Headers show
Series hwmon: powr1220: add powr104 support | expand

Commit Message

Michael Shych Jan. 11, 2022, 5:32 p.m. UTC
From: Michael Shych <michaelsh@nvidia.com>

This patch adds support for Lattice's POWR1014 power manager IC.
Read access to all the ADCs on the chip are supported through
the "hwmon" "sysfs" files.

The main difference of POWR1014 compared to POWR1220 is amount
of VMON input lines: 10 on POWR1014 and 12 lines on POWR1220.

Extend wait time for conversion to complete, since for POWR1014 it
could be longer.

Signed-off-by: Michael Shych <michaelsh@nvidia.com>
Reviewed-by: Vadim Pasternak <vadimp@nvidia.com>
---
v1->v2
Fix added by Michael: Fix incorrect device id.
---
 drivers/hwmon/powr1220.c | 27 +++++++++++++++++++++------
 1 file changed, 21 insertions(+), 6 deletions(-)

Comments

Guenter Roeck Jan. 14, 2022, 6:44 p.m. UTC | #1
On 1/11/22 9:32 AM, michaelsh@nvidia.com wrote:
> From: Michael Shych <michaelsh@nvidia.com>
> 
> This patch adds support for Lattice's POWR1014 power manager IC.
> Read access to all the ADCs on the chip are supported through
> the "hwmon" "sysfs" files.
> 
> The main difference of POWR1014 compared to POWR1220 is amount
> of VMON input lines: 10 on POWR1014 and 12 lines on POWR1220.
> 
> Extend wait time for conversion to complete, since for POWR1014 it
> could be longer.
> 

That is not correct, according to the POWR1014 datasheet, and
the change is wrong anyway. More on that see below.

> Signed-off-by: Michael Shych <michaelsh@nvidia.com>
> Reviewed-by: Vadim Pasternak <vadimp@nvidia.com>
> ---
> v1->v2
> Fix added by Michael: Fix incorrect device id.
> ---
>   drivers/hwmon/powr1220.c | 27 +++++++++++++++++++++------
>   1 file changed, 21 insertions(+), 6 deletions(-)
> 
> diff --git a/drivers/hwmon/powr1220.c b/drivers/hwmon/powr1220.c
> index 1b833781e89d..84f1508f1cbd 100644
> --- a/drivers/hwmon/powr1220.c
> +++ b/drivers/hwmon/powr1220.c
> @@ -22,6 +22,8 @@
>   #define ADC_STEP_MV			2
>   #define ADC_MAX_LOW_MEASUREMENT_MV	2000
>   
> +enum powr1xxx_chips { powr1220, powr1014 };

Please put these into (alpha)numberic order, ie powr1014 first.
Same everywhere below.

> +
>   enum powr1220_regs {
>   	VMON_STATUS0,
>   	VMON_STATUS1,
> @@ -74,6 +76,7 @@ enum powr1220_adc_values {
>   struct powr1220_data {
>   	struct i2c_client *client;
>   	struct mutex update_lock;
> +	u8 max_channels;
>   	bool adc_valid[MAX_POWR1220_ADC_VALUES];
>   	 /* the next value is in jiffies */
>   	unsigned long adc_last_updated[MAX_POWR1220_ADC_VALUES];
> @@ -128,11 +131,8 @@ static int powr1220_read_adc(struct device *dev, int ch_num)
>   		if (result)
>   			goto exit;
>   
> -		/*
> -		 * wait at least Tconvert time (200 us) for the
> -		 * conversion to complete
> -		 */
> -		usleep(200);
> +		/* wait Tconvert time (200us - 400us) for the conversion to complete */
> +		usleep_range(200, 400);

This is confusing. I don't mind using usleep_range(), but usleep_range means
that it is the kernel's choice how long to wait, and it may return after
200uS. If the chip specification states that the conversion may take up to
400uS to complete, the above may return before the conversion is complete.

So please clarify what the chip actually specifies. If the conversion time for
POWR1014 can indeed be 400uS but is only 200uS for POWR1220, the minimum sleep
time would have to be 400uS. If the above is just a personal preference change
to relax sleep time, please make it a separate patch.

Follow-up: I looked up the datasheets. The maximum conversion time for POWR1014
is 100uS, and the maximum conversion time for POWR1220 is 200uS. The above really
doesn't make sense in this context. Tconvert is never "200us - 400us" for any
of the supported chips. Please don't do that as part of an unrelated patch.
If you want to relax wait time, please submit a separate patch that includes
a rationale (and one that is correct and backed up by a datasheet).

>   
>   		/* get the ADC reading */
>   		result = i2c_smbus_read_byte_data(data->client, ADC_VALUE_LOW);
> @@ -170,6 +170,9 @@ static umode_t
>   powr1220_is_visible(const void *data, enum hwmon_sensor_types type, u32
>   		    attr, int channel)
>   {
> +	if (((struct powr1220_data *)data)->max_channels <= channel)
> +		return 0;
> +

I would prefer to have a separate variable for struct powr1220_data *
here, and please don't use Joda programming. Something like

	struct powr1220_data *data = _data;

	if (channel >= data->max_channels)
		return 0;

>   	switch (type) {
>   	case hwmon_in:
>   		switch (attr) {
> @@ -270,6 +273,8 @@ static const struct hwmon_chip_info powr1220_chip_info = {
>   	.info = powr1220_info,
>   };
>   
> +static const struct i2c_device_id powr1220_ids[];
> +
>   static int powr1220_probe(struct i2c_client *client)
>   {
>   	struct powr1220_data *data;
> @@ -282,6 +287,15 @@ static int powr1220_probe(struct i2c_client *client)
>   	if (!data)
>   		return -ENOMEM;
>   
> +	switch (i2c_match_id(powr1220_ids, client)->driver_data) {
> +	case powr1014:
> +		data->max_channels = 10;
> +		break;
> +	default:
> +		data->max_channels = 12;
> +		break;
> +	}
> +
>   	mutex_init(&data->update_lock);
>   	data->client = client;
>   
> @@ -293,7 +307,8 @@ static int powr1220_probe(struct i2c_client *client)
>   }
>   
>   static const struct i2c_device_id powr1220_ids[] = {
> -	{ "powr1220", 0, },
> +	{ "powr1220", powr1220, },
> +	{ "powr1014", powr1014, },
>   	{ }
>   };
>   
>
diff mbox series

Patch

diff --git a/drivers/hwmon/powr1220.c b/drivers/hwmon/powr1220.c
index 1b833781e89d..84f1508f1cbd 100644
--- a/drivers/hwmon/powr1220.c
+++ b/drivers/hwmon/powr1220.c
@@ -22,6 +22,8 @@ 
 #define ADC_STEP_MV			2
 #define ADC_MAX_LOW_MEASUREMENT_MV	2000
 
+enum powr1xxx_chips { powr1220, powr1014 };
+
 enum powr1220_regs {
 	VMON_STATUS0,
 	VMON_STATUS1,
@@ -74,6 +76,7 @@  enum powr1220_adc_values {
 struct powr1220_data {
 	struct i2c_client *client;
 	struct mutex update_lock;
+	u8 max_channels;
 	bool adc_valid[MAX_POWR1220_ADC_VALUES];
 	 /* the next value is in jiffies */
 	unsigned long adc_last_updated[MAX_POWR1220_ADC_VALUES];
@@ -128,11 +131,8 @@  static int powr1220_read_adc(struct device *dev, int ch_num)
 		if (result)
 			goto exit;
 
-		/*
-		 * wait at least Tconvert time (200 us) for the
-		 * conversion to complete
-		 */
-		usleep(200);
+		/* wait Tconvert time (200us - 400us) for the conversion to complete */
+		usleep_range(200, 400);
 
 		/* get the ADC reading */
 		result = i2c_smbus_read_byte_data(data->client, ADC_VALUE_LOW);
@@ -170,6 +170,9 @@  static umode_t
 powr1220_is_visible(const void *data, enum hwmon_sensor_types type, u32
 		    attr, int channel)
 {
+	if (((struct powr1220_data *)data)->max_channels <= channel)
+		return 0;
+
 	switch (type) {
 	case hwmon_in:
 		switch (attr) {
@@ -270,6 +273,8 @@  static const struct hwmon_chip_info powr1220_chip_info = {
 	.info = powr1220_info,
 };
 
+static const struct i2c_device_id powr1220_ids[];
+
 static int powr1220_probe(struct i2c_client *client)
 {
 	struct powr1220_data *data;
@@ -282,6 +287,15 @@  static int powr1220_probe(struct i2c_client *client)
 	if (!data)
 		return -ENOMEM;
 
+	switch (i2c_match_id(powr1220_ids, client)->driver_data) {
+	case powr1014:
+		data->max_channels = 10;
+		break;
+	default:
+		data->max_channels = 12;
+		break;
+	}
+
 	mutex_init(&data->update_lock);
 	data->client = client;
 
@@ -293,7 +307,8 @@  static int powr1220_probe(struct i2c_client *client)
 }
 
 static const struct i2c_device_id powr1220_ids[] = {
-	{ "powr1220", 0, },
+	{ "powr1220", powr1220, },
+	{ "powr1014", powr1014, },
 	{ }
 };