Linux-Hwmon Archive on lore.kernel.org
 help / color / Atom feed
* [PATCH V1 1/2] hwmon: (powr1220) Add support for Lattice's POWR1014 power manager IC
@ 2020-01-27 10:25 Oleksij Rempel
  2020-01-27 10:25 ` [PATCH V1 2/2] hwmon: (powr1220) add scaled voltage support Oleksij Rempel
  2020-01-27 13:55 ` [PATCH V1 1/2] hwmon: (powr1220) Add support for Lattice's POWR1014 power manager IC Guenter Roeck
  0 siblings, 2 replies; 4+ messages in thread
From: Oleksij Rempel @ 2020-01-27 10:25 UTC (permalink / raw)
  To: Jean Delvare, Markus Pietrek, Guenter Roeck
  Cc: Oleksij Rempel, kernel, linux-hwmon, linux-kernel

From: Markus Pietrek <mpie@msc-ge.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.

Signed-off-by: Markus Pietrek <mpie@msc-ge.com>
Signed-off-by: Oleksij Rempel <o.rempel@pengutronix.de>
---
 drivers/hwmon/powr1220.c | 65 ++++++++++++++++++++++++++++++++++++++--
 1 file changed, 63 insertions(+), 2 deletions(-)

diff --git a/drivers/hwmon/powr1220.c b/drivers/hwmon/powr1220.c
index 65997421ee3c..ad7a82f132e6 100644
--- a/drivers/hwmon/powr1220.c
+++ b/drivers/hwmon/powr1220.c
@@ -19,6 +19,9 @@
 #include <linux/mutex.h>
 #include <linux/delay.h>
 
+#define I2C_CLIENT_DATA_1014 1014
+#define I2C_CLIENT_DATA_1220 1220
+
 #define ADC_STEP_MV			2
 #define ADC_MAX_LOW_MEASUREMENT_MV	2000
 
@@ -246,6 +249,51 @@ static SENSOR_DEVICE_ATTR_RO(in11_label, powr1220_label, VMON12);
 static SENSOR_DEVICE_ATTR_RO(in12_label, powr1220_label, VCCA);
 static SENSOR_DEVICE_ATTR_RO(in13_label, powr1220_label, VCCINP);
 
+static struct attribute *powr1014_attrs[] = {
+	&sensor_dev_attr_in0_input.dev_attr.attr,
+	&sensor_dev_attr_in1_input.dev_attr.attr,
+	&sensor_dev_attr_in2_input.dev_attr.attr,
+	&sensor_dev_attr_in3_input.dev_attr.attr,
+	&sensor_dev_attr_in4_input.dev_attr.attr,
+	&sensor_dev_attr_in5_input.dev_attr.attr,
+	&sensor_dev_attr_in6_input.dev_attr.attr,
+	&sensor_dev_attr_in7_input.dev_attr.attr,
+	&sensor_dev_attr_in8_input.dev_attr.attr,
+	&sensor_dev_attr_in9_input.dev_attr.attr,
+	&sensor_dev_attr_in12_input.dev_attr.attr,
+	&sensor_dev_attr_in13_input.dev_attr.attr,
+
+	&sensor_dev_attr_in0_highest.dev_attr.attr,
+	&sensor_dev_attr_in1_highest.dev_attr.attr,
+	&sensor_dev_attr_in2_highest.dev_attr.attr,
+	&sensor_dev_attr_in3_highest.dev_attr.attr,
+	&sensor_dev_attr_in4_highest.dev_attr.attr,
+	&sensor_dev_attr_in5_highest.dev_attr.attr,
+	&sensor_dev_attr_in6_highest.dev_attr.attr,
+	&sensor_dev_attr_in7_highest.dev_attr.attr,
+	&sensor_dev_attr_in8_highest.dev_attr.attr,
+	&sensor_dev_attr_in9_highest.dev_attr.attr,
+	&sensor_dev_attr_in12_highest.dev_attr.attr,
+	&sensor_dev_attr_in13_highest.dev_attr.attr,
+
+	&sensor_dev_attr_in0_label.dev_attr.attr,
+	&sensor_dev_attr_in1_label.dev_attr.attr,
+	&sensor_dev_attr_in2_label.dev_attr.attr,
+	&sensor_dev_attr_in3_label.dev_attr.attr,
+	&sensor_dev_attr_in4_label.dev_attr.attr,
+	&sensor_dev_attr_in5_label.dev_attr.attr,
+	&sensor_dev_attr_in6_label.dev_attr.attr,
+	&sensor_dev_attr_in7_label.dev_attr.attr,
+	&sensor_dev_attr_in8_label.dev_attr.attr,
+	&sensor_dev_attr_in9_label.dev_attr.attr,
+	&sensor_dev_attr_in12_label.dev_attr.attr,
+	&sensor_dev_attr_in13_label.dev_attr.attr,
+
+	NULL
+};
+
+ATTRIBUTE_GROUPS(powr1014);
+
 static struct attribute *powr1220_attrs[] = {
 	&sensor_dev_attr_in0_input.dev_attr.attr,
 	&sensor_dev_attr_in1_input.dev_attr.attr,
@@ -300,9 +348,21 @@ ATTRIBUTE_GROUPS(powr1220);
 static int powr1220_probe(struct i2c_client *client,
 		const struct i2c_device_id *id)
 {
+	const struct attribute_group **attr_groups = NULL;
 	struct powr1220_data *data;
 	struct device *hwmon_dev;
 
+	switch (id->driver_data) {
+	case I2C_CLIENT_DATA_1014:
+		attr_groups = powr1014_groups;
+		break;
+	case I2C_CLIENT_DATA_1220:
+		attr_groups = powr1220_groups;
+		break;
+	default:
+		return -EINVAL;
+	}
+
 	if (!i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_BYTE_DATA))
 		return -ENODEV;
 
@@ -314,13 +374,14 @@ static int powr1220_probe(struct i2c_client *client,
 	data->client = client;
 
 	hwmon_dev = devm_hwmon_device_register_with_groups(&client->dev,
-			client->name, data, powr1220_groups);
+			client->name, data, attr_groups);
 
 	return PTR_ERR_OR_ZERO(hwmon_dev);
 }
 
 static const struct i2c_device_id powr1220_ids[] = {
-	{ "powr1220", 0, },
+	{ "powr1014", I2C_CLIENT_DATA_1014, },
+	{ "powr1220", I2C_CLIENT_DATA_1220, },
 	{ }
 };
 
-- 
2.25.0


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

* [PATCH V1 2/2] hwmon: (powr1220) add scaled voltage support
  2020-01-27 10:25 [PATCH V1 1/2] hwmon: (powr1220) Add support for Lattice's POWR1014 power manager IC Oleksij Rempel
@ 2020-01-27 10:25 ` Oleksij Rempel
  2020-01-27 13:52   ` Guenter Roeck
  2020-01-27 13:55 ` [PATCH V1 1/2] hwmon: (powr1220) Add support for Lattice's POWR1014 power manager IC Guenter Roeck
  1 sibling, 1 reply; 4+ messages in thread
From: Oleksij Rempel @ 2020-01-27 10:25 UTC (permalink / raw)
  To: Jean Delvare, Markus Pietrek, Guenter Roeck
  Cc: Oleksij Rempel, kernel, linux-hwmon, linux-kernel

From: Markus Pietrek <mpie@msc-ge.com>

On some (or may be all?) boards, voltage measured by powr1220 do not
reflect real system configuration. This patch provides scale option to
set board specific configuration.

Signed-off-by: Markus Pietrek <mpie@msc-ge.com>
Signed-off-by: Oleksij Rempel <o.rempel@pengutronix.de>
---
 drivers/hwmon/powr1220.c | 155 +++++++++++++++++++++++++++++++++++++++
 1 file changed, 155 insertions(+)

diff --git a/drivers/hwmon/powr1220.c b/drivers/hwmon/powr1220.c
index ad7a82f132e6..7cb2a6d3b3d8 100644
--- a/drivers/hwmon/powr1220.c
+++ b/drivers/hwmon/powr1220.c
@@ -74,6 +74,14 @@ enum powr1220_adc_values {
 	MAX_POWR1220_ADC_VALUES
 };
 
+/* Real value = (measured-value * factor) / divisor.
+ * A divisor of 0 disables scaling and is identical to factor==1 && divisor==1.
+ */
+struct adc_scale {
+	int factor;
+	int divisor;
+};
+
 struct powr1220_data {
 	struct i2c_client *client;
 	struct mutex update_lock;
@@ -84,6 +92,7 @@ struct powr1220_data {
 	/* values */
 	int adc_maxes[MAX_POWR1220_ADC_VALUES];
 	int adc_values[MAX_POWR1220_ADC_VALUES];
+	struct adc_scale adc_scales[MAX_POWR1220_ADC_VALUES];
 };
 
 static const char * const input_names[] = {
@@ -184,6 +193,29 @@ static ssize_t powr1220_voltage_show(struct device *dev,
 	return sprintf(buf, "%d\n", adc_val);
 }
 
+static ssize_t powr1220_scaled_voltage_show(struct device *dev,
+	struct device_attribute *dev_attr, char *buf)
+{
+	struct sensor_device_attribute *attr = to_sensor_dev_attr(dev_attr);
+	struct powr1220_data *data = dev_get_drvdata(dev);
+	int adc_val = powr1220_read_adc(dev, attr->index);
+	struct adc_scale *scale;
+
+	if (adc_val < 0)
+		return adc_val;
+
+	scale = &data->adc_scales[attr->index];
+	if (scale->divisor) {
+		int64_t lscaled = adc_val;
+
+		lscaled *= scale->factor;
+		lscaled /= scale->divisor;
+		adc_val = (int) lscaled;
+	}
+
+	return sprintf(buf, "%d\n", adc_val);
+}
+
 /* Shows the maximum setting associated with the specified ADC channel */
 static ssize_t powr1220_max_show(struct device *dev,
 				 struct device_attribute *dev_attr, char *buf)
@@ -204,6 +236,38 @@ static ssize_t powr1220_label_show(struct device *dev,
 	return sprintf(buf, "%s\n", input_names[attr->index]);
 }
 
+/* Shows the scale for the read value.
+ * real value = (measured value * factor) / divisor. n/0 means scaling disabled
+ * and raw values are printed.
+ */
+static ssize_t powr1220_scale_show(struct device *dev,
+	struct device_attribute *dev_attr, char *buf)
+{
+	struct sensor_device_attribute *attr = to_sensor_dev_attr(dev_attr);
+	struct powr1220_data *data = dev_get_drvdata(dev);
+
+	return sprintf(buf, "%d/%d\n", data->adc_scales[attr->index].factor,
+		       data->adc_scales[attr->index].divisor);
+}
+
+static ssize_t powr1220_scale_store(struct device *dev,
+				    struct device_attribute *dev_attr,
+				    const char *buf, size_t count)
+{
+	struct sensor_device_attribute *attr = to_sensor_dev_attr(dev_attr);
+	struct powr1220_data *data = dev_get_drvdata(dev);
+	int factor;
+	int divisor;
+
+	if (sscanf(buf, "%d/%d", &factor, &divisor) != 2)
+		return -EINVAL;
+
+	data->adc_scales[attr->index].factor = factor;
+	data->adc_scales[attr->index].divisor = divisor;
+
+	return count;
+}
+
 static SENSOR_DEVICE_ATTR_RO(in0_input, powr1220_voltage, VMON1);
 static SENSOR_DEVICE_ATTR_RO(in1_input, powr1220_voltage, VMON2);
 static SENSOR_DEVICE_ATTR_RO(in2_input, powr1220_voltage, VMON3);
@@ -219,6 +283,26 @@ static SENSOR_DEVICE_ATTR_RO(in11_input, powr1220_voltage, VMON12);
 static SENSOR_DEVICE_ATTR_RO(in12_input, powr1220_voltage, VCCA);
 static SENSOR_DEVICE_ATTR_RO(in13_input, powr1220_voltage, VCCINP);
 
+static SENSOR_DEVICE_ATTR_RO(in0_scaled_input, powr1220_scaled_voltage, VMON1);
+static SENSOR_DEVICE_ATTR_RO(in1_scaled_input, powr1220_scaled_voltage, VMON2);
+static SENSOR_DEVICE_ATTR_RO(in2_scaled_input, powr1220_scaled_voltage, VMON3);
+static SENSOR_DEVICE_ATTR_RO(in3_scaled_input, powr1220_scaled_voltage, VMON4);
+static SENSOR_DEVICE_ATTR_RO(in4_scaled_input, powr1220_scaled_voltage, VMON5);
+static SENSOR_DEVICE_ATTR_RO(in5_scaled_input, powr1220_scaled_voltage, VMON6);
+static SENSOR_DEVICE_ATTR_RO(in6_scaled_input, powr1220_scaled_voltage, VMON7);
+static SENSOR_DEVICE_ATTR_RO(in7_scaled_input, powr1220_scaled_voltage, VMON8);
+static SENSOR_DEVICE_ATTR_RO(in8_scaled_input, powr1220_scaled_voltage, VMON9);
+static SENSOR_DEVICE_ATTR_RO(in9_scaled_input, powr1220_scaled_voltage,
+			     VMON10);
+static SENSOR_DEVICE_ATTR_RO(in10_scaled_input, powr1220_scaled_voltage,
+			     VMON11);
+static SENSOR_DEVICE_ATTR_RO(in11_scaled_input, powr1220_scaled_voltage,
+			     VMON12);
+static SENSOR_DEVICE_ATTR_RO(in12_scaled_input, powr1220_scaled_voltage,
+			     VCCA);
+static SENSOR_DEVICE_ATTR_RO(in13_scaled_input, powr1220_scaled_voltage,
+			     VCCINP);
+
 static SENSOR_DEVICE_ATTR_RO(in0_highest, powr1220_max, VMON1);
 static SENSOR_DEVICE_ATTR_RO(in1_highest, powr1220_max, VMON2);
 static SENSOR_DEVICE_ATTR_RO(in2_highest, powr1220_max, VMON3);
@@ -249,6 +333,21 @@ static SENSOR_DEVICE_ATTR_RO(in11_label, powr1220_label, VMON12);
 static SENSOR_DEVICE_ATTR_RO(in12_label, powr1220_label, VCCA);
 static SENSOR_DEVICE_ATTR_RO(in13_label, powr1220_label, VCCINP);
 
+static SENSOR_DEVICE_ATTR_RW(in0_scale, powr1220_scale, VMON1);
+static SENSOR_DEVICE_ATTR_RW(in1_scale, powr1220_scale, VMON2);
+static SENSOR_DEVICE_ATTR_RW(in2_scale, powr1220_scale, VMON3);
+static SENSOR_DEVICE_ATTR_RW(in3_scale, powr1220_scale, VMON4);
+static SENSOR_DEVICE_ATTR_RW(in4_scale, powr1220_scale, VMON5);
+static SENSOR_DEVICE_ATTR_RW(in5_scale, powr1220_scale, VMON6);
+static SENSOR_DEVICE_ATTR_RW(in6_scale, powr1220_scale, VMON7);
+static SENSOR_DEVICE_ATTR_RW(in7_scale, powr1220_scale, VMON8);
+static SENSOR_DEVICE_ATTR_RW(in8_scale, powr1220_scale, VMON9);
+static SENSOR_DEVICE_ATTR_RW(in9_scale, powr1220_scale, VMON10);
+static SENSOR_DEVICE_ATTR_RW(in10_scale, powr1220_scale, VMON11);
+static SENSOR_DEVICE_ATTR_RW(in11_scale, powr1220_scale, VMON12);
+static SENSOR_DEVICE_ATTR_RW(in12_scale, powr1220_scale, VCCA);
+static SENSOR_DEVICE_ATTR_RW(in13_scale, powr1220_scale, VCCINP);
+
 static struct attribute *powr1014_attrs[] = {
 	&sensor_dev_attr_in0_input.dev_attr.attr,
 	&sensor_dev_attr_in1_input.dev_attr.attr,
@@ -263,6 +362,19 @@ static struct attribute *powr1014_attrs[] = {
 	&sensor_dev_attr_in12_input.dev_attr.attr,
 	&sensor_dev_attr_in13_input.dev_attr.attr,
 
+	&sensor_dev_attr_in0_scaled_input.dev_attr.attr,
+	&sensor_dev_attr_in1_scaled_input.dev_attr.attr,
+	&sensor_dev_attr_in2_scaled_input.dev_attr.attr,
+	&sensor_dev_attr_in3_scaled_input.dev_attr.attr,
+	&sensor_dev_attr_in4_scaled_input.dev_attr.attr,
+	&sensor_dev_attr_in5_scaled_input.dev_attr.attr,
+	&sensor_dev_attr_in6_scaled_input.dev_attr.attr,
+	&sensor_dev_attr_in7_scaled_input.dev_attr.attr,
+	&sensor_dev_attr_in8_scaled_input.dev_attr.attr,
+	&sensor_dev_attr_in9_scaled_input.dev_attr.attr,
+	&sensor_dev_attr_in12_scaled_input.dev_attr.attr,
+	&sensor_dev_attr_in13_scaled_input.dev_attr.attr,
+
 	&sensor_dev_attr_in0_highest.dev_attr.attr,
 	&sensor_dev_attr_in1_highest.dev_attr.attr,
 	&sensor_dev_attr_in2_highest.dev_attr.attr,
@@ -289,6 +401,19 @@ static struct attribute *powr1014_attrs[] = {
 	&sensor_dev_attr_in12_label.dev_attr.attr,
 	&sensor_dev_attr_in13_label.dev_attr.attr,
 
+	&sensor_dev_attr_in0_scale.dev_attr.attr,
+	&sensor_dev_attr_in1_scale.dev_attr.attr,
+	&sensor_dev_attr_in2_scale.dev_attr.attr,
+	&sensor_dev_attr_in3_scale.dev_attr.attr,
+	&sensor_dev_attr_in4_scale.dev_attr.attr,
+	&sensor_dev_attr_in5_scale.dev_attr.attr,
+	&sensor_dev_attr_in6_scale.dev_attr.attr,
+	&sensor_dev_attr_in7_scale.dev_attr.attr,
+	&sensor_dev_attr_in8_scale.dev_attr.attr,
+	&sensor_dev_attr_in9_scale.dev_attr.attr,
+	&sensor_dev_attr_in12_scale.dev_attr.attr,
+	&sensor_dev_attr_in13_scale.dev_attr.attr,
+
 	NULL
 };
 
@@ -310,6 +435,21 @@ static struct attribute *powr1220_attrs[] = {
 	&sensor_dev_attr_in12_input.dev_attr.attr,
 	&sensor_dev_attr_in13_input.dev_attr.attr,
 
+	&sensor_dev_attr_in0_scaled_input.dev_attr.attr,
+	&sensor_dev_attr_in1_scaled_input.dev_attr.attr,
+	&sensor_dev_attr_in2_scaled_input.dev_attr.attr,
+	&sensor_dev_attr_in3_scaled_input.dev_attr.attr,
+	&sensor_dev_attr_in4_scaled_input.dev_attr.attr,
+	&sensor_dev_attr_in5_scaled_input.dev_attr.attr,
+	&sensor_dev_attr_in6_scaled_input.dev_attr.attr,
+	&sensor_dev_attr_in7_scaled_input.dev_attr.attr,
+	&sensor_dev_attr_in8_scaled_input.dev_attr.attr,
+	&sensor_dev_attr_in9_scaled_input.dev_attr.attr,
+	&sensor_dev_attr_in10_scaled_input.dev_attr.attr,
+	&sensor_dev_attr_in11_scaled_input.dev_attr.attr,
+	&sensor_dev_attr_in12_scaled_input.dev_attr.attr,
+	&sensor_dev_attr_in13_scaled_input.dev_attr.attr,
+
 	&sensor_dev_attr_in0_highest.dev_attr.attr,
 	&sensor_dev_attr_in1_highest.dev_attr.attr,
 	&sensor_dev_attr_in2_highest.dev_attr.attr,
@@ -340,6 +480,21 @@ static struct attribute *powr1220_attrs[] = {
 	&sensor_dev_attr_in12_label.dev_attr.attr,
 	&sensor_dev_attr_in13_label.dev_attr.attr,
 
+	&sensor_dev_attr_in0_scale.dev_attr.attr,
+	&sensor_dev_attr_in1_scale.dev_attr.attr,
+	&sensor_dev_attr_in2_scale.dev_attr.attr,
+	&sensor_dev_attr_in3_scale.dev_attr.attr,
+	&sensor_dev_attr_in4_scale.dev_attr.attr,
+	&sensor_dev_attr_in5_scale.dev_attr.attr,
+	&sensor_dev_attr_in6_scale.dev_attr.attr,
+	&sensor_dev_attr_in7_scale.dev_attr.attr,
+	&sensor_dev_attr_in8_scale.dev_attr.attr,
+	&sensor_dev_attr_in9_scale.dev_attr.attr,
+	&sensor_dev_attr_in10_scale.dev_attr.attr,
+	&sensor_dev_attr_in11_scale.dev_attr.attr,
+	&sensor_dev_attr_in12_scale.dev_attr.attr,
+	&sensor_dev_attr_in13_scale.dev_attr.attr,
+
 	NULL
 };
 
-- 
2.25.0


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

* Re: [PATCH V1 2/2] hwmon: (powr1220) add scaled voltage support
  2020-01-27 10:25 ` [PATCH V1 2/2] hwmon: (powr1220) add scaled voltage support Oleksij Rempel
@ 2020-01-27 13:52   ` Guenter Roeck
  0 siblings, 0 replies; 4+ messages in thread
From: Guenter Roeck @ 2020-01-27 13:52 UTC (permalink / raw)
  To: Oleksij Rempel, Jean Delvare, Markus Pietrek
  Cc: kernel, linux-hwmon, linux-kernel

On 1/27/20 2:25 AM, Oleksij Rempel wrote:
> From: Markus Pietrek <mpie@msc-ge.com>
> 
> On some (or may be all?) boards, voltage measured by powr1220 do not
> reflect real system configuration. This patch provides scale option to
> set board specific configuration.
> 
> Signed-off-by: Markus Pietrek <mpie@msc-ge.com>
> Signed-off-by: Oleksij Rempel <o.rempel@pengutronix.de>
> ---

NACK; scaling is what sensors3.conf is for. Also, we most definintely
won't permit out-of-ABI attributes for something like this.

Guenter

>   drivers/hwmon/powr1220.c | 155 +++++++++++++++++++++++++++++++++++++++
>   1 file changed, 155 insertions(+)
> 
> diff --git a/drivers/hwmon/powr1220.c b/drivers/hwmon/powr1220.c
> index ad7a82f132e6..7cb2a6d3b3d8 100644
> --- a/drivers/hwmon/powr1220.c
> +++ b/drivers/hwmon/powr1220.c
> @@ -74,6 +74,14 @@ enum powr1220_adc_values {
>   	MAX_POWR1220_ADC_VALUES
>   };
>   
> +/* Real value = (measured-value * factor) / divisor.
> + * A divisor of 0 disables scaling and is identical to factor==1 && divisor==1.
> + */
> +struct adc_scale {
> +	int factor;
> +	int divisor;
> +};
> +
>   struct powr1220_data {
>   	struct i2c_client *client;
>   	struct mutex update_lock;
> @@ -84,6 +92,7 @@ struct powr1220_data {
>   	/* values */
>   	int adc_maxes[MAX_POWR1220_ADC_VALUES];
>   	int adc_values[MAX_POWR1220_ADC_VALUES];
> +	struct adc_scale adc_scales[MAX_POWR1220_ADC_VALUES];
>   };
>   
>   static const char * const input_names[] = {
> @@ -184,6 +193,29 @@ static ssize_t powr1220_voltage_show(struct device *dev,
>   	return sprintf(buf, "%d\n", adc_val);
>   }
>   
> +static ssize_t powr1220_scaled_voltage_show(struct device *dev,
> +	struct device_attribute *dev_attr, char *buf)
> +{
> +	struct sensor_device_attribute *attr = to_sensor_dev_attr(dev_attr);
> +	struct powr1220_data *data = dev_get_drvdata(dev);
> +	int adc_val = powr1220_read_adc(dev, attr->index);
> +	struct adc_scale *scale;
> +
> +	if (adc_val < 0)
> +		return adc_val;
> +
> +	scale = &data->adc_scales[attr->index];
> +	if (scale->divisor) {
> +		int64_t lscaled = adc_val;
> +
> +		lscaled *= scale->factor;
> +		lscaled /= scale->divisor;
> +		adc_val = (int) lscaled;
> +	}
> +
> +	return sprintf(buf, "%d\n", adc_val);
> +}
> +
>   /* Shows the maximum setting associated with the specified ADC channel */
>   static ssize_t powr1220_max_show(struct device *dev,
>   				 struct device_attribute *dev_attr, char *buf)
> @@ -204,6 +236,38 @@ static ssize_t powr1220_label_show(struct device *dev,
>   	return sprintf(buf, "%s\n", input_names[attr->index]);
>   }
>   
> +/* Shows the scale for the read value.
> + * real value = (measured value * factor) / divisor. n/0 means scaling disabled
> + * and raw values are printed.
> + */
> +static ssize_t powr1220_scale_show(struct device *dev,
> +	struct device_attribute *dev_attr, char *buf)
> +{
> +	struct sensor_device_attribute *attr = to_sensor_dev_attr(dev_attr);
> +	struct powr1220_data *data = dev_get_drvdata(dev);
> +
> +	return sprintf(buf, "%d/%d\n", data->adc_scales[attr->index].factor,
> +		       data->adc_scales[attr->index].divisor);
> +}
> +
> +static ssize_t powr1220_scale_store(struct device *dev,
> +				    struct device_attribute *dev_attr,
> +				    const char *buf, size_t count)
> +{
> +	struct sensor_device_attribute *attr = to_sensor_dev_attr(dev_attr);
> +	struct powr1220_data *data = dev_get_drvdata(dev);
> +	int factor;
> +	int divisor;
> +
> +	if (sscanf(buf, "%d/%d", &factor, &divisor) != 2)
> +		return -EINVAL;
> +
> +	data->adc_scales[attr->index].factor = factor;
> +	data->adc_scales[attr->index].divisor = divisor;
> +
> +	return count;
> +}
> +
>   static SENSOR_DEVICE_ATTR_RO(in0_input, powr1220_voltage, VMON1);
>   static SENSOR_DEVICE_ATTR_RO(in1_input, powr1220_voltage, VMON2);
>   static SENSOR_DEVICE_ATTR_RO(in2_input, powr1220_voltage, VMON3);
> @@ -219,6 +283,26 @@ static SENSOR_DEVICE_ATTR_RO(in11_input, powr1220_voltage, VMON12);
>   static SENSOR_DEVICE_ATTR_RO(in12_input, powr1220_voltage, VCCA);
>   static SENSOR_DEVICE_ATTR_RO(in13_input, powr1220_voltage, VCCINP);
>   
> +static SENSOR_DEVICE_ATTR_RO(in0_scaled_input, powr1220_scaled_voltage, VMON1);
> +static SENSOR_DEVICE_ATTR_RO(in1_scaled_input, powr1220_scaled_voltage, VMON2);
> +static SENSOR_DEVICE_ATTR_RO(in2_scaled_input, powr1220_scaled_voltage, VMON3);
> +static SENSOR_DEVICE_ATTR_RO(in3_scaled_input, powr1220_scaled_voltage, VMON4);
> +static SENSOR_DEVICE_ATTR_RO(in4_scaled_input, powr1220_scaled_voltage, VMON5);
> +static SENSOR_DEVICE_ATTR_RO(in5_scaled_input, powr1220_scaled_voltage, VMON6);
> +static SENSOR_DEVICE_ATTR_RO(in6_scaled_input, powr1220_scaled_voltage, VMON7);
> +static SENSOR_DEVICE_ATTR_RO(in7_scaled_input, powr1220_scaled_voltage, VMON8);
> +static SENSOR_DEVICE_ATTR_RO(in8_scaled_input, powr1220_scaled_voltage, VMON9);
> +static SENSOR_DEVICE_ATTR_RO(in9_scaled_input, powr1220_scaled_voltage,
> +			     VMON10);
> +static SENSOR_DEVICE_ATTR_RO(in10_scaled_input, powr1220_scaled_voltage,
> +			     VMON11);
> +static SENSOR_DEVICE_ATTR_RO(in11_scaled_input, powr1220_scaled_voltage,
> +			     VMON12);
> +static SENSOR_DEVICE_ATTR_RO(in12_scaled_input, powr1220_scaled_voltage,
> +			     VCCA);
> +static SENSOR_DEVICE_ATTR_RO(in13_scaled_input, powr1220_scaled_voltage,
> +			     VCCINP);
> +
>   static SENSOR_DEVICE_ATTR_RO(in0_highest, powr1220_max, VMON1);
>   static SENSOR_DEVICE_ATTR_RO(in1_highest, powr1220_max, VMON2);
>   static SENSOR_DEVICE_ATTR_RO(in2_highest, powr1220_max, VMON3);
> @@ -249,6 +333,21 @@ static SENSOR_DEVICE_ATTR_RO(in11_label, powr1220_label, VMON12);
>   static SENSOR_DEVICE_ATTR_RO(in12_label, powr1220_label, VCCA);
>   static SENSOR_DEVICE_ATTR_RO(in13_label, powr1220_label, VCCINP);
>   
> +static SENSOR_DEVICE_ATTR_RW(in0_scale, powr1220_scale, VMON1);
> +static SENSOR_DEVICE_ATTR_RW(in1_scale, powr1220_scale, VMON2);
> +static SENSOR_DEVICE_ATTR_RW(in2_scale, powr1220_scale, VMON3);
> +static SENSOR_DEVICE_ATTR_RW(in3_scale, powr1220_scale, VMON4);
> +static SENSOR_DEVICE_ATTR_RW(in4_scale, powr1220_scale, VMON5);
> +static SENSOR_DEVICE_ATTR_RW(in5_scale, powr1220_scale, VMON6);
> +static SENSOR_DEVICE_ATTR_RW(in6_scale, powr1220_scale, VMON7);
> +static SENSOR_DEVICE_ATTR_RW(in7_scale, powr1220_scale, VMON8);
> +static SENSOR_DEVICE_ATTR_RW(in8_scale, powr1220_scale, VMON9);
> +static SENSOR_DEVICE_ATTR_RW(in9_scale, powr1220_scale, VMON10);
> +static SENSOR_DEVICE_ATTR_RW(in10_scale, powr1220_scale, VMON11);
> +static SENSOR_DEVICE_ATTR_RW(in11_scale, powr1220_scale, VMON12);
> +static SENSOR_DEVICE_ATTR_RW(in12_scale, powr1220_scale, VCCA);
> +static SENSOR_DEVICE_ATTR_RW(in13_scale, powr1220_scale, VCCINP);
> +
>   static struct attribute *powr1014_attrs[] = {
>   	&sensor_dev_attr_in0_input.dev_attr.attr,
>   	&sensor_dev_attr_in1_input.dev_attr.attr,
> @@ -263,6 +362,19 @@ static struct attribute *powr1014_attrs[] = {
>   	&sensor_dev_attr_in12_input.dev_attr.attr,
>   	&sensor_dev_attr_in13_input.dev_attr.attr,
>   
> +	&sensor_dev_attr_in0_scaled_input.dev_attr.attr,
> +	&sensor_dev_attr_in1_scaled_input.dev_attr.attr,
> +	&sensor_dev_attr_in2_scaled_input.dev_attr.attr,
> +	&sensor_dev_attr_in3_scaled_input.dev_attr.attr,
> +	&sensor_dev_attr_in4_scaled_input.dev_attr.attr,
> +	&sensor_dev_attr_in5_scaled_input.dev_attr.attr,
> +	&sensor_dev_attr_in6_scaled_input.dev_attr.attr,
> +	&sensor_dev_attr_in7_scaled_input.dev_attr.attr,
> +	&sensor_dev_attr_in8_scaled_input.dev_attr.attr,
> +	&sensor_dev_attr_in9_scaled_input.dev_attr.attr,
> +	&sensor_dev_attr_in12_scaled_input.dev_attr.attr,
> +	&sensor_dev_attr_in13_scaled_input.dev_attr.attr,
> +
>   	&sensor_dev_attr_in0_highest.dev_attr.attr,
>   	&sensor_dev_attr_in1_highest.dev_attr.attr,
>   	&sensor_dev_attr_in2_highest.dev_attr.attr,
> @@ -289,6 +401,19 @@ static struct attribute *powr1014_attrs[] = {
>   	&sensor_dev_attr_in12_label.dev_attr.attr,
>   	&sensor_dev_attr_in13_label.dev_attr.attr,
>   
> +	&sensor_dev_attr_in0_scale.dev_attr.attr,
> +	&sensor_dev_attr_in1_scale.dev_attr.attr,
> +	&sensor_dev_attr_in2_scale.dev_attr.attr,
> +	&sensor_dev_attr_in3_scale.dev_attr.attr,
> +	&sensor_dev_attr_in4_scale.dev_attr.attr,
> +	&sensor_dev_attr_in5_scale.dev_attr.attr,
> +	&sensor_dev_attr_in6_scale.dev_attr.attr,
> +	&sensor_dev_attr_in7_scale.dev_attr.attr,
> +	&sensor_dev_attr_in8_scale.dev_attr.attr,
> +	&sensor_dev_attr_in9_scale.dev_attr.attr,
> +	&sensor_dev_attr_in12_scale.dev_attr.attr,
> +	&sensor_dev_attr_in13_scale.dev_attr.attr,
> +
>   	NULL
>   };
>   
> @@ -310,6 +435,21 @@ static struct attribute *powr1220_attrs[] = {
>   	&sensor_dev_attr_in12_input.dev_attr.attr,
>   	&sensor_dev_attr_in13_input.dev_attr.attr,
>   
> +	&sensor_dev_attr_in0_scaled_input.dev_attr.attr,
> +	&sensor_dev_attr_in1_scaled_input.dev_attr.attr,
> +	&sensor_dev_attr_in2_scaled_input.dev_attr.attr,
> +	&sensor_dev_attr_in3_scaled_input.dev_attr.attr,
> +	&sensor_dev_attr_in4_scaled_input.dev_attr.attr,
> +	&sensor_dev_attr_in5_scaled_input.dev_attr.attr,
> +	&sensor_dev_attr_in6_scaled_input.dev_attr.attr,
> +	&sensor_dev_attr_in7_scaled_input.dev_attr.attr,
> +	&sensor_dev_attr_in8_scaled_input.dev_attr.attr,
> +	&sensor_dev_attr_in9_scaled_input.dev_attr.attr,
> +	&sensor_dev_attr_in10_scaled_input.dev_attr.attr,
> +	&sensor_dev_attr_in11_scaled_input.dev_attr.attr,
> +	&sensor_dev_attr_in12_scaled_input.dev_attr.attr,
> +	&sensor_dev_attr_in13_scaled_input.dev_attr.attr,
> +
>   	&sensor_dev_attr_in0_highest.dev_attr.attr,
>   	&sensor_dev_attr_in1_highest.dev_attr.attr,
>   	&sensor_dev_attr_in2_highest.dev_attr.attr,
> @@ -340,6 +480,21 @@ static struct attribute *powr1220_attrs[] = {
>   	&sensor_dev_attr_in12_label.dev_attr.attr,
>   	&sensor_dev_attr_in13_label.dev_attr.attr,
>   
> +	&sensor_dev_attr_in0_scale.dev_attr.attr,
> +	&sensor_dev_attr_in1_scale.dev_attr.attr,
> +	&sensor_dev_attr_in2_scale.dev_attr.attr,
> +	&sensor_dev_attr_in3_scale.dev_attr.attr,
> +	&sensor_dev_attr_in4_scale.dev_attr.attr,
> +	&sensor_dev_attr_in5_scale.dev_attr.attr,
> +	&sensor_dev_attr_in6_scale.dev_attr.attr,
> +	&sensor_dev_attr_in7_scale.dev_attr.attr,
> +	&sensor_dev_attr_in8_scale.dev_attr.attr,
> +	&sensor_dev_attr_in9_scale.dev_attr.attr,
> +	&sensor_dev_attr_in10_scale.dev_attr.attr,
> +	&sensor_dev_attr_in11_scale.dev_attr.attr,
> +	&sensor_dev_attr_in12_scale.dev_attr.attr,
> +	&sensor_dev_attr_in13_scale.dev_attr.attr,
> +
>   	NULL
>   };
>   
> 


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

* Re: [PATCH V1 1/2] hwmon: (powr1220) Add support for Lattice's POWR1014 power manager IC
  2020-01-27 10:25 [PATCH V1 1/2] hwmon: (powr1220) Add support for Lattice's POWR1014 power manager IC Oleksij Rempel
  2020-01-27 10:25 ` [PATCH V1 2/2] hwmon: (powr1220) add scaled voltage support Oleksij Rempel
@ 2020-01-27 13:55 ` Guenter Roeck
  1 sibling, 0 replies; 4+ messages in thread
From: Guenter Roeck @ 2020-01-27 13:55 UTC (permalink / raw)
  To: Oleksij Rempel, Jean Delvare, Markus Pietrek
  Cc: kernel, linux-hwmon, linux-kernel

On 1/27/20 2:25 AM, Oleksij Rempel wrote:
> From: Markus Pietrek <mpie@msc-ge.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.
> 

Please use an is_visible function instead of duplicating the attribute
pointer arrays.

Thanks,
Guenter

> Signed-off-by: Markus Pietrek <mpie@msc-ge.com>
> Signed-off-by: Oleksij Rempel <o.rempel@pengutronix.de>
> ---
>   drivers/hwmon/powr1220.c | 65 ++++++++++++++++++++++++++++++++++++++--
>   1 file changed, 63 insertions(+), 2 deletions(-)
> 
> diff --git a/drivers/hwmon/powr1220.c b/drivers/hwmon/powr1220.c
> index 65997421ee3c..ad7a82f132e6 100644
> --- a/drivers/hwmon/powr1220.c
> +++ b/drivers/hwmon/powr1220.c
> @@ -19,6 +19,9 @@
>   #include <linux/mutex.h>
>   #include <linux/delay.h>
>   
> +#define I2C_CLIENT_DATA_1014 1014
> +#define I2C_CLIENT_DATA_1220 1220
> +
>   #define ADC_STEP_MV			2
>   #define ADC_MAX_LOW_MEASUREMENT_MV	2000
>   
> @@ -246,6 +249,51 @@ static SENSOR_DEVICE_ATTR_RO(in11_label, powr1220_label, VMON12);
>   static SENSOR_DEVICE_ATTR_RO(in12_label, powr1220_label, VCCA);
>   static SENSOR_DEVICE_ATTR_RO(in13_label, powr1220_label, VCCINP);
>   
> +static struct attribute *powr1014_attrs[] = {
> +	&sensor_dev_attr_in0_input.dev_attr.attr,
> +	&sensor_dev_attr_in1_input.dev_attr.attr,
> +	&sensor_dev_attr_in2_input.dev_attr.attr,
> +	&sensor_dev_attr_in3_input.dev_attr.attr,
> +	&sensor_dev_attr_in4_input.dev_attr.attr,
> +	&sensor_dev_attr_in5_input.dev_attr.attr,
> +	&sensor_dev_attr_in6_input.dev_attr.attr,
> +	&sensor_dev_attr_in7_input.dev_attr.attr,
> +	&sensor_dev_attr_in8_input.dev_attr.attr,
> +	&sensor_dev_attr_in9_input.dev_attr.attr,
> +	&sensor_dev_attr_in12_input.dev_attr.attr,
> +	&sensor_dev_attr_in13_input.dev_attr.attr,
> +
> +	&sensor_dev_attr_in0_highest.dev_attr.attr,
> +	&sensor_dev_attr_in1_highest.dev_attr.attr,
> +	&sensor_dev_attr_in2_highest.dev_attr.attr,
> +	&sensor_dev_attr_in3_highest.dev_attr.attr,
> +	&sensor_dev_attr_in4_highest.dev_attr.attr,
> +	&sensor_dev_attr_in5_highest.dev_attr.attr,
> +	&sensor_dev_attr_in6_highest.dev_attr.attr,
> +	&sensor_dev_attr_in7_highest.dev_attr.attr,
> +	&sensor_dev_attr_in8_highest.dev_attr.attr,
> +	&sensor_dev_attr_in9_highest.dev_attr.attr,
> +	&sensor_dev_attr_in12_highest.dev_attr.attr,
> +	&sensor_dev_attr_in13_highest.dev_attr.attr,
> +
> +	&sensor_dev_attr_in0_label.dev_attr.attr,
> +	&sensor_dev_attr_in1_label.dev_attr.attr,
> +	&sensor_dev_attr_in2_label.dev_attr.attr,
> +	&sensor_dev_attr_in3_label.dev_attr.attr,
> +	&sensor_dev_attr_in4_label.dev_attr.attr,
> +	&sensor_dev_attr_in5_label.dev_attr.attr,
> +	&sensor_dev_attr_in6_label.dev_attr.attr,
> +	&sensor_dev_attr_in7_label.dev_attr.attr,
> +	&sensor_dev_attr_in8_label.dev_attr.attr,
> +	&sensor_dev_attr_in9_label.dev_attr.attr,
> +	&sensor_dev_attr_in12_label.dev_attr.attr,
> +	&sensor_dev_attr_in13_label.dev_attr.attr,
> +
> +	NULL
> +};
> +
> +ATTRIBUTE_GROUPS(powr1014);
> +
>   static struct attribute *powr1220_attrs[] = {
>   	&sensor_dev_attr_in0_input.dev_attr.attr,
>   	&sensor_dev_attr_in1_input.dev_attr.attr,
> @@ -300,9 +348,21 @@ ATTRIBUTE_GROUPS(powr1220);
>   static int powr1220_probe(struct i2c_client *client,
>   		const struct i2c_device_id *id)
>   {
> +	const struct attribute_group **attr_groups = NULL;
>   	struct powr1220_data *data;
>   	struct device *hwmon_dev;
>   
> +	switch (id->driver_data) {
> +	case I2C_CLIENT_DATA_1014:
> +		attr_groups = powr1014_groups;
> +		break;
> +	case I2C_CLIENT_DATA_1220:
> +		attr_groups = powr1220_groups;
> +		break;
> +	default:
> +		return -EINVAL;
> +	}
> +
>   	if (!i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_BYTE_DATA))
>   		return -ENODEV;
>   
> @@ -314,13 +374,14 @@ static int powr1220_probe(struct i2c_client *client,
>   	data->client = client;
>   
>   	hwmon_dev = devm_hwmon_device_register_with_groups(&client->dev,
> -			client->name, data, powr1220_groups);
> +			client->name, data, attr_groups);
>   
>   	return PTR_ERR_OR_ZERO(hwmon_dev);
>   }
>   
>   static const struct i2c_device_id powr1220_ids[] = {
> -	{ "powr1220", 0, },
> +	{ "powr1014", I2C_CLIENT_DATA_1014, },
> +	{ "powr1220", I2C_CLIENT_DATA_1220, },
>   	{ }
>   };
>   
> 


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

end of thread, back to index

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-01-27 10:25 [PATCH V1 1/2] hwmon: (powr1220) Add support for Lattice's POWR1014 power manager IC Oleksij Rempel
2020-01-27 10:25 ` [PATCH V1 2/2] hwmon: (powr1220) add scaled voltage support Oleksij Rempel
2020-01-27 13:52   ` Guenter Roeck
2020-01-27 13:55 ` [PATCH V1 1/2] hwmon: (powr1220) Add support for Lattice's POWR1014 power manager IC Guenter Roeck

Linux-Hwmon Archive on lore.kernel.org

Archives are clonable:
	git clone --mirror https://lore.kernel.org/linux-hwmon/0 linux-hwmon/git/0.git

	# If you have public-inbox 1.1+ installed, you may
	# initialize and index your mirror using the following commands:
	public-inbox-init -V2 linux-hwmon linux-hwmon/ https://lore.kernel.org/linux-hwmon \
		linux-hwmon@vger.kernel.org
	public-inbox-index linux-hwmon

Example config snippet for mirrors

Newsgroup available over NNTP:
	nntp://nntp.lore.kernel.org/org.kernel.vger.linux-hwmon


AGPL code for this site: git clone https://public-inbox.org/public-inbox.git