linux-hwmon.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] hwmon: adm1275: Make sure we are reading enough data for different chips
@ 2020-07-08 23:53 Chu Lin
  2020-07-09  0:33 ` Guenter Roeck
  0 siblings, 1 reply; 4+ messages in thread
From: Chu Lin @ 2020-07-08 23:53 UTC (permalink / raw)
  To: linux
  Cc: belgaied, jasonling, jdelvare, linchuyuan, linux-hwmon,
	linux-kernel, zhongqil

Issue:
When binding adm1272 to the adm1275 driver, PEC error is reported.
See blow:
adm1275: probe of xxxx failed with error -74

Diagnosis:
Per the datasheet of adm1272 and adm1278 (www.analog.com),
PMON_CONFIG (0xd4) is 16bits wide. On the other hand,
PMON_CONFIG (0xd4) for adm1275 is 8bits wide.
The driver should not assume everything is 8bits wide and read only
8bits from it.

Solution:
if it is adm1272 or adm1278, use i2c_read_word. Else, use i2c_read_byte

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

diff --git a/drivers/hwmon/pmbus/adm1275.c b/drivers/hwmon/pmbus/adm1275.c
index e25f541227da..a0d547d20358 100644
--- a/drivers/hwmon/pmbus/adm1275.c
+++ b/drivers/hwmon/pmbus/adm1275.c
@@ -472,6 +472,7 @@ static int adm1275_probe(struct i2c_client *client,
 	struct adm1275_data *data;
 	const struct i2c_device_id *mid;
 	const struct coefficients *coefficients;
+	s32 (*config_read_fn_ptr)(const struct i2c_client *client, u8 reg);
 	int vindex = -1, voindex = -1, cindex = -1, pindex = -1;
 	int tindex = -1;
 	u32 shunt;
@@ -510,14 +511,6 @@ static int adm1275_probe(struct i2c_client *client,
 			   "Device mismatch: Configured %s, detected %s\n",
 			   id->name, mid->name);
 
-	config = i2c_smbus_read_byte_data(client, ADM1275_PMON_CONFIG);
-	if (config < 0)
-		return config;
-
-	device_config = i2c_smbus_read_byte_data(client, ADM1275_DEVICE_CONFIG);
-	if (device_config < 0)
-		return device_config;
-
 	data = devm_kzalloc(&client->dev, sizeof(struct adm1275_data),
 			    GFP_KERNEL);
 	if (!data)
@@ -532,6 +525,21 @@ static int adm1275_probe(struct i2c_client *client,
 
 	data->id = mid->driver_data;
 
+	/* adm1272 and adm1278 supports temperature monitoring.  The config and device config
+	 * are 16bits wide for these two devices. On the other hand, other devices are 8 bits wide
+	 */
+	if (data->id == adm1272 || data->id == adm1278)
+		config_read_fn_ptr = &i2c_smbus_read_word_data;
+	else
+		config_read_fn_ptr = &i2c_smbus_read_byte_data;
+	config = config_read_fn_ptr(client, ADM1275_PMON_CONFIG);
+	if (config < 0)
+		return config;
+
+	device_config = config_read_fn_ptr(client, ADM1275_DEVICE_CONFIG);
+	if (device_config < 0)
+		return device_config;
+
 	info = &data->info;
 
 	info->pages = 1;
-- 
2.27.0.383.g050319c2ae-goog


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

* Re: [PATCH] hwmon: adm1275: Make sure we are reading enough data for different chips
  2020-07-08 23:53 [PATCH] hwmon: adm1275: Make sure we are reading enough data for different chips Chu Lin
@ 2020-07-09  0:33 ` Guenter Roeck
  2020-07-09  3:30   ` Chu Lin
  0 siblings, 1 reply; 4+ messages in thread
From: Guenter Roeck @ 2020-07-09  0:33 UTC (permalink / raw)
  To: Chu Lin
  Cc: belgaied, jasonling, jdelvare, linux-hwmon, linux-kernel, zhongqil

On 7/8/20 4:53 PM, Chu Lin wrote:
> Issue:
> When binding adm1272 to the adm1275 driver, PEC error is reported.
> See blow:

s/blow/below/

Also, unless I am missing something, the error is only seen if PEC is
enabled.

> adm1275: probe of xxxx failed with error -74
> 
> Diagnosis:
> Per the datasheet of adm1272 and adm1278 (www.analog.com),
> PMON_CONFIG (0xd4) is 16bits wide. On the other hand,
> PMON_CONFIG (0xd4) for adm1275 is 8bits wide.
> The driver should not assume everything is 8bits wide and read only
> 8bits from it.
> 
> Solution:
> if it is adm1272 or adm1278, use i2c_read_word. Else, use i2c_read_byte
> 

See below for more chips with 16-bit configuration registers.

> Signed-off-by: Chu Lin <linchuyuan@google.com>
> ---
>  drivers/hwmon/pmbus/adm1275.c | 24 ++++++++++++++++--------
>  1 file changed, 16 insertions(+), 8 deletions(-)
> 
> diff --git a/drivers/hwmon/pmbus/adm1275.c b/drivers/hwmon/pmbus/adm1275.c
> index e25f541227da..a0d547d20358 100644
> --- a/drivers/hwmon/pmbus/adm1275.c
> +++ b/drivers/hwmon/pmbus/adm1275.c
> @@ -472,6 +472,7 @@ static int adm1275_probe(struct i2c_client *client,
>  	struct adm1275_data *data;
>  	const struct i2c_device_id *mid;
>  	const struct coefficients *coefficients;
> +	s32 (*config_read_fn_ptr)(const struct i2c_client *client, u8 reg);

_ptr is really not needed here and redundant. A variable pointing to a function
is always a pointer.

>  	int vindex = -1, voindex = -1, cindex = -1, pindex = -1;
>  	int tindex = -1;
>  	u32 shunt;
> @@ -510,14 +511,6 @@ static int adm1275_probe(struct i2c_client *client,
>  			   "Device mismatch: Configured %s, detected %s\n",
>  			   id->name, mid->name);
>  
> -	config = i2c_smbus_read_byte_data(client, ADM1275_PMON_CONFIG);
> -	if (config < 0)
> -		return config;
> -
> -	device_config = i2c_smbus_read_byte_data(client, ADM1275_DEVICE_CONFIG);
> -	if (device_config < 0)
> -		return device_config;
> -

The reason for doing this here was to avoid the memory allocation failure
on error. I don't really see the point of moving the code block.

>  	data = devm_kzalloc(&client->dev, sizeof(struct adm1275_data),
>  			    GFP_KERNEL);
>  	if (!data)
> @@ -532,6 +525,21 @@ static int adm1275_probe(struct i2c_client *client,
>  
>  	data->id = mid->driver_data;
>  
> +	/* adm1272 and adm1278 supports temperature monitoring.  The config and device config
> +	 * are 16bits wide for these two devices. On the other hand, other devices are 8 bits wide
> +	 */

Standard multi-line comments, please. Also, the destinction is really
based on have_power_sampling, though that is set only later.

> +	if (data->id == adm1272 || data->id == adm1278)
> +		config_read_fn_ptr = &i2c_smbus_read_word_data;

Function pointers don't need &.
For ADM1293 and ADM1294, the registers are also 16 bit wide
(again, see use of have_power_sampling).

Thanks,
Guenter

> +	else
> +		config_read_fn_ptr = &i2c_smbus_read_byte_data;
> +	config = config_read_fn_ptr(client, ADM1275_PMON_CONFIG);
> +	if (config < 0)
> +		return config;
> +
> +	device_config = config_read_fn_ptr(client, ADM1275_DEVICE_CONFIG);
> +	if (device_config < 0)
> +		return device_config;
> +
>  	info = &data->info;
>  
>  	info->pages = 1;
> 


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

* Re: [PATCH] hwmon: adm1275: Make sure we are reading enough data for different chips
  2020-07-09  0:33 ` Guenter Roeck
@ 2020-07-09  3:30   ` Chu Lin
  2020-07-09  3:57     ` Guenter Roeck
  0 siblings, 1 reply; 4+ messages in thread
From: Chu Lin @ 2020-07-09  3:30 UTC (permalink / raw)
  To: Guenter Roeck
  Cc: Kais Belgaied, Jason Ling, jdelvare, linux-hwmon, linux-kernel,
	Zhongqi Li

On Wed, Jul 8, 2020 at 5:33 PM Guenter Roeck <linux@roeck-us.net> wrote:
>
> On 7/8/20 4:53 PM, Chu Lin wrote:
> > Issue:
> > When binding adm1272 to the adm1275 driver, PEC error is reported.
> > See blow:
>
> s/blow/below/
>
> Also, unless I am missing something, the error is only seen if PEC is
> enabled.

This is correct.

Regards,
Chu

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

* Re: [PATCH] hwmon: adm1275: Make sure we are reading enough data for different chips
  2020-07-09  3:30   ` Chu Lin
@ 2020-07-09  3:57     ` Guenter Roeck
  0 siblings, 0 replies; 4+ messages in thread
From: Guenter Roeck @ 2020-07-09  3:57 UTC (permalink / raw)
  To: Chu Lin
  Cc: Kais Belgaied, Jason Ling, jdelvare, linux-hwmon, linux-kernel,
	Zhongqi Li

On 7/8/20 8:30 PM, Chu Lin wrote:
> On Wed, Jul 8, 2020 at 5:33 PM Guenter Roeck <linux@roeck-us.net> wrote:
>>
>> On 7/8/20 4:53 PM, Chu Lin wrote:
>>> Issue:
>>> When binding adm1272 to the adm1275 driver, PEC error is reported.
>>> See blow:
>>
>> s/blow/below/
>>
>> Also, unless I am missing something, the error is only seen if PEC is
>> enabled.
> 
> This is correct.
> 

That should be mentioned in the description.

Thanks,
Guenter

> Regards,
> Chu
> 


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

end of thread, other threads:[~2020-07-09  3:57 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-07-08 23:53 [PATCH] hwmon: adm1275: Make sure we are reading enough data for different chips Chu Lin
2020-07-09  0:33 ` Guenter Roeck
2020-07-09  3:30   ` Chu Lin
2020-07-09  3:57     ` Guenter Roeck

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).