mbox series

[v5,0/2] hwmon: (adt7475) Added attenuator bypass support

Message ID 20200123220533.2228-1-logan.shaw@alliedtelesis.co.nz
Headers show
Series hwmon: (adt7475) Added attenuator bypass support | expand

Message

Logan Shaw Jan. 23, 2020, 10:05 p.m. UTC
The ADT7473 and ADT7475 support bypassing voltage input attenuators on
voltage input 1 and the ADT7476 and ADT7490 additionally support
bypassing voltage input attenuators on voltage inputs 0, 3, and 4. This
can be useful to improve measurement resolution when measuring voltages
0 V - 2.25 V.

This patch adds 4 optional devicetree properties to the adt7475
driver, each setting the attenuator bypass (or clearing) on a
specific voltage input.

* v5
- modified adt7475.yaml to remove dt_binding_check errors
- made various alignment fixes to adt7475.c to improve style.
- renamed function modify_config_from_dts_prop to modify_config
- changed return type of function modify_config to void
- function modify_config error return code modified no longer override
  the dependent functions i2c_smbus_write_byte_data error.
- renamed function load_all_bypass_attenuators to load_attenuators
- in function load_attenuators the two local variables config2_copy
  and config4_copy have been combined into one: conf_copy.

* v4
- fixed a small error in file adt7475.yaml (duplicate property names).

* v3
- removed the functionality to set the global attenuator bypass.
- added functionality to allow bypassing voltage input 1 on the
	ADT7473 and ADT7475.
- added DTS definition file adt7475.yaml and 4 new properties.
- added the previousely missing newline character to the end of
  	file adt7475.c. 

* v2
- removed sysfs changes from patch
- removed adt7475_write macro from patch and replaced it by using
	the i2c_smbus_write_byte_data function directly in code.
- removed config4_attenuate_index function from patch and replaced it
	by modifying the function  load_individual_bypass_attenuators
	to use hard coded bit values.
- modified function load_individual_bypass_attenuators to use 4 if
	statements, one for each voltage input, replacing the for loop.
- modified function adt7475_probe to check the device is a ADT7476 or
	ADT7490 (other devices do not support bypassing all or
	individual attenuators), and only then set the relevant bits.
- added new file adt7475.txt to document the new devicetree properties.
- removed c++ style comments. 

Logan Shaw (2):
  hwmon: (adt7475) Added attenuator bypass support
  dt-bindings: hwmon: (adt7475) Added missing adt7475 doccumentation

 .../devicetree/bindings/hwmon/adt7475.yaml    | 95 +++++++++++++++++++
 drivers/hwmon/adt7475.c                       | 68 +++++++++++++
 2 files changed, 163 insertions(+)
 create mode 100644 Documentation/devicetree/bindings/hwmon/adt7475.yaml

Comments

Guenter Roeck Jan. 24, 2020, 4:55 a.m. UTC | #1
On 1/23/20 2:05 PM, Logan Shaw wrote:
> Added support for reading DTS properties to set attenuators on
> device probe for the ADT7473, ADT7475, ADT7476, and ADT7490.
> 
> Signed-off-by: Logan Shaw <logan.shaw@alliedtelesis.co.nz>
> ---
> ---
>   drivers/hwmon/adt7475.c | 68 +++++++++++++++++++++++++++++++++++++++++
>   1 file changed, 68 insertions(+)
> 
> diff --git a/drivers/hwmon/adt7475.c b/drivers/hwmon/adt7475.c
> index 6c64d50c9aae..8afc5a89ec92 100644
> --- a/drivers/hwmon/adt7475.c
> +++ b/drivers/hwmon/adt7475.c
> @@ -19,6 +19,7 @@
>   #include <linux/hwmon-vid.h>
>   #include <linux/err.h>
>   #include <linux/jiffies.h>
> +#include <linux/of.h>
>   #include <linux/util_macros.h>
>   
>   /* Indexes for the sysfs hooks */
> @@ -1457,6 +1458,70 @@ static int adt7475_update_limits(struct i2c_client *client)
>   	return 0;
>   }
>   
> +/**

I am still at loss why you think it would make sense to mark those
comments for the kernel documentation. This doesn't have value outside
this driver.

> + * Sets or clears the given bit in the config depending on the value of the
> + * given dts property. Non-zero value sets, zero value clears. No change if
> + * the property is absent.
> + */
> +static void modify_config(const struct i2c_client *client, char *property,
> +				u8 *config, u8 bit_index)
> +{
> +	u32 prop_value = 0;
> +	int ret = of_property_read_u32(client->dev.of_node, property,
> +					&prop_value);
> +
> +	if (!ret) {
> +		if (prop_value)
> +			*config |= (1 << bit_index);
> +		else
> +			*config &= ~(1 << bit_index);
> +	}
> +}
> +
> +/**
> + * Configures the attenuator bypasses for voltage inputs, depening on
> + * properties in the dts.
> +.*
> + * The adt7473 and adt7475 only support bypassing in1.
> + *
> + * Returns a negative error code if there was an error writing to the register.
> + */
> +static int load_attenuators(const struct i2c_client *client, int chip,
> +				u8 *config2, u8 *config4)
> +{
> +	u8 conf_copy;
> +	int ret = 0;

Unnecessary assignment.

> +
> +	if (chip == adt7476 || chip == adt7490) {
> +		conf_copy = *config4;
> +
> +		modify_config(client, "bypass-attenuator-in0", &conf_copy, 4);
> +		modify_config(client, "bypass-attenuator-in1", &conf_copy, 5);
> +		modify_config(client, "bypass-attenuator-in3", &conf_copy, 6);
> +		modify_config(client, "bypass-attenuator-in4", &conf_copy, 7);
> +
> +		ret = i2c_smbus_write_byte_data(client, REG_CONFIG4,
> +			conf_copy);
> +		if (ret < 0)
> +			return ret;
> +
> +		*config4 = conf_copy;
> +	} else if (chip == adt7473 || chip == adt7475) {
> +		conf_copy = *config2;
> +
> +		modify_config(client, "bypass-attenuator-in1", &conf_copy, 5);
> +
> +		ret = i2c_smbus_write_byte_data(client, REG_CONFIG2,
> +			conf_copy);
> +		if (ret < 0)
> +			return ret;
> +
> +		*config2 = conf_copy;
> +	}
> +
> +	return 0;
> +}
> +
>   static int adt7475_probe(struct i2c_client *client,
>   			 const struct i2c_device_id *id)
>   {
> @@ -1546,6 +1611,9 @@ static int adt7475_probe(struct i2c_client *client,
>   
>   	/* Voltage attenuators can be bypassed, globally or individually */
>   	config2 = adt7475_read(REG_CONFIG2);
> +	if (load_attenuators(client, chip, &config2, &(data->config4)) < 0)
> +		dev_warn(&client->dev, "Error setting bypass attenuators\n");
> +
>   	if (config2 & CONFIG2_ATTN) {
>   		data->bypass_attn = (0x3 << 3) | 0x3;
>   	} else {
> 
CHECK: Alignment should match open parenthesis
#45: FILE: drivers/hwmon/adt7475.c:1467:
+static void modify_config(const struct i2c_client *client, char *property,
+				u8 *config, u8 bit_index)

CHECK: Alignment should match open parenthesis
#68: FILE: drivers/hwmon/adt7475.c:1490:
+static int load_attenuators(const struct i2c_client *client, int chip,
+				u8 *config2, u8 *config4)

CHECK: Alignment should match open parenthesis
#82: FILE: drivers/hwmon/adt7475.c:1504:
+		ret = i2c_smbus_write_byte_data(client, REG_CONFIG4,
+			conf_copy);

CHECK: Alignment should match open parenthesis
#93: FILE: drivers/hwmon/adt7475.c:1515:
+		ret = i2c_smbus_write_byte_data(client, REG_CONFIG2,
+			conf_copy);

CHECK: Unnecessary parentheses around data->config4
#110: FILE: drivers/hwmon/adt7475.c:1614:
+	if (load_attenuators(client, chip, &config2, &(data->config4)) < 0)

None of those is beneficial. Please fix.

Thanks,
Guenter