linux-hwmon.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] hwmon: (adc128d818) Fix advanced configuration register init
@ 2020-07-28 15:18 Roy van Doormaal
  2020-08-04 21:28 ` Guenter Roeck
  0 siblings, 1 reply; 2+ messages in thread
From: Roy van Doormaal @ 2020-07-28 15:18 UTC (permalink / raw)
  To: linux-hwmon; +Cc: Roy van Doormaal, Jean Delvare, Guenter Roeck, open list

If the operation mode is non-zero and an external reference voltage is set,
first the operation mode is written to the advanced configuration register,
followed by the externel reference enable bit,
resetting the configuration mode to 0.

To fix this, first compose the value of the advanced configuration register
based on the configuration mode and the external reference voltage.
The advanced configuration register is then written to the device,
if it is different from the default register value (0x0).

Signed-off-by: Roy van Doormaal <roy.van.doormaal@prodrive-technologies.com>
---
 drivers/hwmon/adc128d818.c | 24 ++++++++++++------------
 1 file changed, 12 insertions(+), 12 deletions(-)

diff --git a/drivers/hwmon/adc128d818.c b/drivers/hwmon/adc128d818.c
index f9edec195c35..571d5454c6b2 100644
--- a/drivers/hwmon/adc128d818.c
+++ b/drivers/hwmon/adc128d818.c
@@ -393,6 +393,7 @@ static int adc128_init_client(struct adc128_data *data)
 {
 	struct i2c_client *client = data->client;
 	int err;
+	u8 regval = 0x0;
 
 	/*
 	 * Reset chip to defaults.
@@ -403,10 +404,17 @@ static int adc128_init_client(struct adc128_data *data)
 		return err;
 
 	/* Set operation mode, if non-default */
-	if (data->mode != 0) {
-		err = i2c_smbus_write_byte_data(client,
-						ADC128_REG_CONFIG_ADV,
-						data->mode << 1);
+	if (data->mode != 0)
+		regval |= data->mode << 1;
+
+	/* If external vref is selected, configure the chip to use it */
+	if (data->regulator)
+		regval |= 0x01;
+
+	/* Write advanced configuration register */
+	if (regval != 0x0) {
+		err = i2c_smbus_write_byte_data(client, ADC128_REG_CONFIG_ADV,
+						regval);
 		if (err)
 			return err;
 	}
@@ -416,14 +424,6 @@ static int adc128_init_client(struct adc128_data *data)
 	if (err)
 		return err;
 
-	/* If external vref is selected, configure the chip to use it */
-	if (data->regulator) {
-		err = i2c_smbus_write_byte_data(client,
-						ADC128_REG_CONFIG_ADV, 0x01);
-		if (err)
-			return err;
-	}
-
 	return 0;
 }
 
-- 
2.20.1


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

* Re: [PATCH] hwmon: (adc128d818) Fix advanced configuration register init
  2020-07-28 15:18 [PATCH] hwmon: (adc128d818) Fix advanced configuration register init Roy van Doormaal
@ 2020-08-04 21:28 ` Guenter Roeck
  0 siblings, 0 replies; 2+ messages in thread
From: Guenter Roeck @ 2020-08-04 21:28 UTC (permalink / raw)
  To: Roy van Doormaal; +Cc: linux-hwmon, Jean Delvare, open list

On Tue, Jul 28, 2020 at 05:18:45PM +0200, Roy van Doormaal wrote:
> If the operation mode is non-zero and an external reference voltage is set,
> first the operation mode is written to the advanced configuration register,
> followed by the externel reference enable bit,
> resetting the configuration mode to 0.
> 
> To fix this, first compose the value of the advanced configuration register
> based on the configuration mode and the external reference voltage.
> The advanced configuration register is then written to the device,
> if it is different from the default register value (0x0).
> 
> Signed-off-by: Roy van Doormaal <roy.van.doormaal@prodrive-technologies.com>

Applied.

Thanks,
Guenter

> ---
>  drivers/hwmon/adc128d818.c | 24 ++++++++++++------------
>  1 file changed, 12 insertions(+), 12 deletions(-)
> 
> diff --git a/drivers/hwmon/adc128d818.c b/drivers/hwmon/adc128d818.c
> index f9edec195c35..571d5454c6b2 100644
> --- a/drivers/hwmon/adc128d818.c
> +++ b/drivers/hwmon/adc128d818.c
> @@ -393,6 +393,7 @@ static int adc128_init_client(struct adc128_data *data)
>  {
>  	struct i2c_client *client = data->client;
>  	int err;
> +	u8 regval = 0x0;
>  
>  	/*
>  	 * Reset chip to defaults.
> @@ -403,10 +404,17 @@ static int adc128_init_client(struct adc128_data *data)
>  		return err;
>  
>  	/* Set operation mode, if non-default */
> -	if (data->mode != 0) {
> -		err = i2c_smbus_write_byte_data(client,
> -						ADC128_REG_CONFIG_ADV,
> -						data->mode << 1);
> +	if (data->mode != 0)
> +		regval |= data->mode << 1;
> +
> +	/* If external vref is selected, configure the chip to use it */
> +	if (data->regulator)
> +		regval |= 0x01;
> +
> +	/* Write advanced configuration register */
> +	if (regval != 0x0) {
> +		err = i2c_smbus_write_byte_data(client, ADC128_REG_CONFIG_ADV,
> +						regval);
>  		if (err)
>  			return err;
>  	}
> @@ -416,14 +424,6 @@ static int adc128_init_client(struct adc128_data *data)
>  	if (err)
>  		return err;
>  
> -	/* If external vref is selected, configure the chip to use it */
> -	if (data->regulator) {
> -		err = i2c_smbus_write_byte_data(client,
> -						ADC128_REG_CONFIG_ADV, 0x01);
> -		if (err)
> -			return err;
> -	}
> -
>  	return 0;
>  }
>  

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

end of thread, other threads:[~2020-08-04 21:28 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-07-28 15:18 [PATCH] hwmon: (adc128d818) Fix advanced configuration register init Roy van Doormaal
2020-08-04 21:28 ` 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).