Skip to contentSkip to conversation

hwmon: (ltc4151) Make shunt-resistor configurable

PATCH Superseded Daniel Golle 5 participants 12 messages Jul 24, 2016 – Aug 1, 2016 5 messages not in the archive

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 hwmon: (ltc4151) Make shunt-resistor configurable

Allow to specify the resistance of the attached shunt via DT by adding the shunt-resistor property. Fall-back to the previous default (1 mOhm) if unset.

---
 drivers/hwmon/ltc4151.c | 26 ++++++++++++++++++++++++--
 1 file changed, 24 insertions(+), 2 deletions(-)
drivers/hwmon/ltc4151.c +24 -2
index c86a184..f856e44 100644
@@ -30,6 +30,8 @@
 
 #include <linux/kernel.h>
 #include <linux/module.h>
+#include <linux/of.h>
+#include <linux/of_device.h>
 #include <linux/init.h>
 #include <linux/err.h>
 #include <linux/slab.h>
@@ -52,6 +54,7 @@ struct ltc4151_data {
 	struct mutex update_lock;
 	bool valid;
 	unsigned long last_updated; /* in jiffies */
+	unsigned int shunt; /* in micro ohms */
 
 	/* Registers */
 	u8 regs[6];
@@ -111,9 +114,9 @@ static int ltc4151_get_value(struct ltc4151_data *data, u8 reg)
 	case LTC4151_SENSE_H:
 		/*
 		 * 20uV resolution. Convert to current as measured with
-		 * an 1 mOhm sense resistor, in mA.
+		 * a given sense resistor, in mA.
 		 */
-		val = val * 20;
+		val = val * 20 * 1000 / data->shunt;
 		break;
 	case LTC4151_VIN_H:
 		/* 25 mV per increment */
@@ -176,6 +179,7 @@ static int ltc4151_probe(struct i2c_client *client,
 	struct device *dev = &client->dev;
 	struct ltc4151_data *data;
 	struct device *hwmon_dev;
+	u32 shunt;
 
 	if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
 		return -ENODEV;
@@ -184,6 +188,14 @@ static int ltc4151_probe(struct i2c_client *client,
 	if (!data)
 		return -ENOMEM;
 
+#ifdef CONFIG_OF
+	if (!of_property_read_u32(client->dev.of_node, "shunt-resistor", &shunt)
+		&& shunt > 0 )
+		data->shunt = shunt;
+	else
+#endif
+		data->shunt = 1000; /* 1 mOhm if not set */
+
 	data->client = client;
 	mutex_init(&data->update_lock);
 
@@ -199,10 +211,20 @@ static const struct i2c_device_id ltc4151_id[] = {
 };
 MODULE_DEVICE_TABLE(i2c, ltc4151_id);
 
+#ifdef CONFIG_OF
+static const struct of_device_id ltc4151_match[] = {
+	{ .compatible = "lltc,ltc4151" },
+	{},
+};
+#endif
+
 /* This is the driver that will be inserted */
 static struct i2c_driver ltc4151_driver = {
+	.class		= I2C_CLASS_HWMON,
 	.driver = {
 		.name	= "ltc4151",
+		.owner  = THIS_MODULE,
+		.of_match_table = of_match_ptr(ltc4151_match),
 	},
 	.probe		= ltc4151_probe,
 	.id_table	= ltc4151_id,
Signature · 1 line
2.9.0
maintainer
#1 lore raw
Quoted 14 lines · Daniel Golle

Allow to specify the resistance of the attached shunt via DT by adding the shunt-resistor property. Fall-back to the previous default (1 mOhm) if unset.

---
  drivers/hwmon/ltc4151.c | 26 ++++++++++++++++++++++++--
  1 file changed, 24 insertions(+), 2 deletions(-)
drivers/hwmon/ltc4151.c +0 -0
index c86a184..f856e44 100644

drivers/hwmon/ltc4151.c line 34

Quoted 5 lines
drivers/hwmon/ltc4151.c +2 -0
@@ -30,6 +30,8 @@
 
  #include <linux/kernel.h>
  #include <linux/module.h>
+#include <linux/of.h>
+#include <linux/of_device.h>

I don't immediately see why this include file would be necessary.

Quoted 32 lines
drivers/hwmon/ltc4151.c +4 -2
  #include <linux/init.h>
  #include <linux/err.h>
  #include <linux/slab.h>
@@ -52,6 +54,7 @@ struct ltc4151_data {
  	struct mutex update_lock;
  	bool valid;
  	unsigned long last_updated; /* in jiffies */
+	unsigned int shunt; /* in micro ohms */
 
  	/* Registers */
  	u8 regs[6];
@@ -111,9 +114,9 @@ static int ltc4151_get_value(struct ltc4151_data *data, u8 reg)
  	case LTC4151_SENSE_H:
  		/*
  		 * 20uV resolution. Convert to current as measured with
-		 * an 1 mOhm sense resistor, in mA.
+		 * a given sense resistor, in mA.
  		 */
-		val = val * 20;
+		val = val * 20 * 1000 / data->shunt;
  		break;
  	case LTC4151_VIN_H:
  		/* 25 mV per increment */
@@ -176,6 +179,7 @@ static int ltc4151_probe(struct i2c_client *client,
  	struct device *dev = &client->dev;
  	struct ltc4151_data *data;
  	struct device *hwmon_dev;
+	u32 shunt;
 
  	if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
  		return -ENODEV;

drivers/hwmon/ltc4151.c static int ltc4151_probe(struct i2c_client *client, · line 191

Quoted 4 lines
drivers/hwmon/ltc4151.c +1 -0
@@ -184,6 +188,14 @@ static int ltc4151_probe(struct i2c_client *client,
  	if (!data)
  		return -ENOMEM;
 
+#ifdef CONFIG_OF

#ifdef is unnecessary. Worse, it results in

drivers/hwmon/ltc4151.c: In function ‘ltc4151_probe’:
drivers/hwmon/ltc4151.c:182:6: warning: unused variable ‘shunt’

if CONFIG_OF is not defined.

drivers/hwmon/ltc4151.c static int ltc4151_probe(struct i2c_client *client, · line 193

Quoted 2 lines
drivers/hwmon/ltc4151.c +2 -0
+	if (!of_property_read_u32(client->dev.of_node, "shunt-resistor", &shunt)
+		&& shunt > 0 )

checkpatch error

drivers/hwmon/ltc4151.c static int ltc4151_probe(struct i2c_client *client, · line 194

Quoted 1 line
drivers/hwmon/ltc4151.c +1 -0
+		data->shunt = shunt;

shunt == 0 is silently accepted and replaced with 1000. This is quite unusual. Any reason ? If not, return -EINVAL might be more appropriate for shunt == 0.

drivers/hwmon/ltc4151.c static const struct i2c_device_id ltc4151_id[] = { · line 214

Quoted 12 lines
drivers/hwmon/ltc4151.c +5 -0
+	else
+#endif
+		data->shunt = 1000; /* 1 mOhm if not set */
+
  	data->client = client;
  	mutex_init(&data->update_lock);
 
@@ -199,10 +211,20 @@ static const struct i2c_device_id ltc4151_id[] = {
  };
  MODULE_DEVICE_TABLE(i2c, ltc4151_id);
 
+#ifdef CONFIG_OF

#ifdef is unnecessary.

drivers/hwmon/ltc4151.c static const struct i2c_device_id ltc4151_id[] = { · line 216

Quoted 2 lines
drivers/hwmon/ltc4151.c +2 -0
+static const struct of_device_id ltc4151_match[] = {
+	{ .compatible = "lltc,ltc4151" },

This as well as the property need to be documented.

drivers/hwmon/ltc4151.c static const struct i2c_device_id ltc4151_id[] = { · line 223

Quoted 7 lines
drivers/hwmon/ltc4151.c +5 -0
+	{},
+};
+#endif
+
  /* This is the driver that will be inserted */
  static struct i2c_driver ltc4151_driver = {
+	.class		= I2C_CLASS_HWMON,

This is an unrelated change, and appears to be quite pointless, since the driver does not have a detect function.
Any reason for adding it ? If so, please explain, and submit as separate patch.

drivers/hwmon/ltc4151.c static const struct i2c_device_id ltc4151_id[] = { · line 226

Quoted 3 lines
drivers/hwmon/ltc4151.c +1 -0
  	.driver = {
  		.name	= "ltc4151",
+		.owner  = THIS_MODULE,

Is this now necessary again ? If so, please explain, and submit as separate patch.

drivers/hwmon/ltc4151.c static const struct i2c_device_id ltc4151_id[] = { · line 230

Quoted 5 lines
drivers/hwmon/ltc4151.c +1 -0
+		.of_match_table = of_match_ptr(ltc4151_match),
  	},
  	.probe		= ltc4151_probe,
  	.id_table	= ltc4151_id,

--
To unsubscribe from this list: send the line "unsubscribe linux-hwmon" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html

A message that is not in the archive (sent off-list or not yet received): [email protected]

#3 lore raw

PATCH v2 1/2 hwmon: (ltc4151) Make shunt-resistor configurable

Allow to specify the resistance of the attached shunt via DT by adding the shunt-resistor property. Fall-back to the previous default (1 mOhm) if unset.

---
v2: removed unneeded things and throw -EINVAL for shunt==0

 drivers/hwmon/ltc4151.c | 21 +++++++++++++++++++--
 1 file changed, 19 insertions(+), 2 deletions(-)
drivers/hwmon/ltc4151.c +19 -2
index c86a184..419c6f7 100644
@@ -30,6 +30,7 @@
 
 #include <linux/kernel.h>
 #include <linux/module.h>
+#include <linux/of.h>
 #include <linux/init.h>
 #include <linux/err.h>
 #include <linux/slab.h>
@@ -52,6 +53,7 @@ struct ltc4151_data {
 	struct mutex update_lock;
 	bool valid;
 	unsigned long last_updated; /* in jiffies */
+	unsigned int shunt; /* in micro ohms */
 
 	/* Registers */
 	u8 regs[6];
@@ -111,9 +113,9 @@ static int ltc4151_get_value(struct ltc4151_data *data, u8 reg)
 	case LTC4151_SENSE_H:
 		/*
 		 * 20uV resolution. Convert to current as measured with
-		 * an 1 mOhm sense resistor, in mA.
+		 * a given sense resistor, in mA.
 		 */
-		val = val * 20;
+		val = val * 20 * 1000 / data->shunt;
 		break;
 	case LTC4151_VIN_H:
 		/* 25 mV per increment */
@@ -176,6 +178,7 @@ static int ltc4151_probe(struct i2c_client *client,
 	struct device *dev = &client->dev;
 	struct ltc4151_data *data;
 	struct device *hwmon_dev;
+	u32 shunt;
 
 	if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
 		return -ENODEV;
@@ -184,6 +187,14 @@ static int ltc4151_probe(struct i2c_client *client,
 	if (!data)
 		return -ENOMEM;
 
+	if (of_property_read_u32(client->dev.of_node, "shunt-resistor", &shunt))
+		shunt = 1000; /* 1 mOhm if not set via DT */
+
+	if (shunt == 0)
+		return -EINVAL;
+
+	data->shunt = shunt;
+
 	data->client = client;
 	mutex_init(&data->update_lock);
 
@@ -199,10 +210,16 @@ static const struct i2c_device_id ltc4151_id[] = {
 };
 MODULE_DEVICE_TABLE(i2c, ltc4151_id);
 
+static const struct of_device_id ltc4151_match[] = {
+	{ .compatible = "lltc,ltc4151" },
+	{},
+};
+
 /* This is the driver that will be inserted */
 static struct i2c_driver ltc4151_driver = {
 	.driver = {
 		.name	= "ltc4151",
+		.of_match_table = of_match_ptr(ltc4151_match),
 	},
 	.probe		= ltc4151_probe,
 	.id_table	= ltc4151_id,
Signature · 6 lines
2.9.0

--
To unsubscribe from this list: send the line "unsubscribe devicetree" in
the body of a message to [email protected]
More majordomo info at  http://vger.kernel.org/majordomo-info.html

A message that is not in the archive (sent off-list or not yet received): [email protected]

#5 lore raw

Daniel,

Quoted 5 lines · Daniel Golle

Allow to specify the resistance of the attached shunt via DT by adding the shunt-resistor property. Fall-back to the previous default (1 mOhm) if unset.

Please resend with the property name changed as asked for by Rob.

Thanks,
Guenter

Quoted 69 lines

---
v2: removed unneeded things and throw -EINVAL for shunt==0

  drivers/hwmon/ltc4151.c | 21 +++++++++++++++++++--
  1 file changed, 19 insertions(+), 2 deletions(-)
drivers/hwmon/ltc4151.c +16 -2
index c86a184..419c6f7 100644
@@ -30,6 +30,7 @@
 
  #include <linux/kernel.h>
  #include <linux/module.h>
+#include <linux/of.h>
  #include <linux/init.h>
  #include <linux/err.h>
  #include <linux/slab.h>
@@ -52,6 +53,7 @@ struct ltc4151_data {
  	struct mutex update_lock;
  	bool valid;
  	unsigned long last_updated; /* in jiffies */
+	unsigned int shunt; /* in micro ohms */
 
  	/* Registers */
  	u8 regs[6];
@@ -111,9 +113,9 @@ static int ltc4151_get_value(struct ltc4151_data *data, u8 reg)
  	case LTC4151_SENSE_H:
  		/*
  		 * 20uV resolution. Convert to current as measured with
-		 * an 1 mOhm sense resistor, in mA.
+		 * a given sense resistor, in mA.
  		 */
-		val = val * 20;
+		val = val * 20 * 1000 / data->shunt;
  		break;
  	case LTC4151_VIN_H:
  		/* 25 mV per increment */
@@ -176,6 +178,7 @@ static int ltc4151_probe(struct i2c_client *client,
  	struct device *dev = &client->dev;
  	struct ltc4151_data *data;
  	struct device *hwmon_dev;
+	u32 shunt;
 
  	if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
  		return -ENODEV;
@@ -184,6 +187,14 @@ static int ltc4151_probe(struct i2c_client *client,
  	if (!data)
  		return -ENOMEM;
 
+	if (of_property_read_u32(client->dev.of_node, "shunt-resistor", &shunt))
+		shunt = 1000; /* 1 mOhm if not set via DT */
+
+	if (shunt == 0)
+		return -EINVAL;
+
+	data->shunt = shunt;
+
  	data->client = client;
  	mutex_init(&data->update_lock);
 
@@ -199,10 +210,16 @@ static const struct i2c_device_id ltc4151_id[] = {
  };
  MODULE_DEVICE_TABLE(i2c, ltc4151_id);
 
+static const struct of_device_id ltc4151_match[] = {
+	{ .compatible = "lltc,ltc4151" },
+	{},

drivers/hwmon/ltc4151.c static const struct i2c_device_id ltc4151_id[] = { · line 225

Quoted 10 lines
drivers/hwmon/ltc4151.c +3 -0
+};
+
  /* This is the driver that will be inserted */
  static struct i2c_driver ltc4151_driver = {
  	.driver = {
  		.name	= "ltc4151",
+		.of_match_table = of_match_ptr(ltc4151_match),
  	},
  	.probe		= ltc4151_probe,
  	.id_table	= ltc4151_id,

--
To unsubscribe from this list: send the line "unsubscribe devicetree" in the body of a message to [email protected] More majordomo info at http://vger.kernel.org/majordomo-info.html

A message that is not in the archive (sent off-list or not yet received): [email protected]

#7 lore raw

Hi Guenter,

Quoted 10 lines · Guenter Roeck

Daniel,

Quoted 5 lines · Daniel Golle

Allow to specify the resistance of the attached shunt via DT by adding the shunt-resistor property. Fall-back to the previous default (1 mOhm) if unset.

Please resend with the property name changed as asked for by Rob.

Ok, will do. I originally copy-pasted the property name from ina209, so I reckon it's supposed to be changed there as well...?

Cheers

Daniel

Quoted 69 lines

Thanks,
Guenter

Quoted 65 lines

---
v2: removed unneeded things and throw -EINVAL for shunt==0

  drivers/hwmon/ltc4151.c | 21 +++++++++++++++++++--
  1 file changed, 19 insertions(+), 2 deletions(-)
drivers/hwmon/ltc4151.c +13 -2
index c86a184..419c6f7 100644
@@ -30,6 +30,7 @@
 
  #include <linux/kernel.h>
  #include <linux/module.h>
+#include <linux/of.h>
  #include <linux/init.h>
  #include <linux/err.h>
  #include <linux/slab.h>
@@ -52,6 +53,7 @@ struct ltc4151_data {
  	struct mutex update_lock;
  	bool valid;
  	unsigned long last_updated; /* in jiffies */
+	unsigned int shunt; /* in micro ohms */
 
  	/* Registers */
  	u8 regs[6];
@@ -111,9 +113,9 @@ static int ltc4151_get_value(struct ltc4151_data *data, u8 reg)
  	case LTC4151_SENSE_H:
  		/*
  		 * 20uV resolution. Convert to current as measured with
-		 * an 1 mOhm sense resistor, in mA.
+		 * a given sense resistor, in mA.
  		 */
-		val = val * 20;
+		val = val * 20 * 1000 / data->shunt;
  		break;
  	case LTC4151_VIN_H:
  		/* 25 mV per increment */
@@ -176,6 +178,7 @@ static int ltc4151_probe(struct i2c_client *client,
  	struct device *dev = &client->dev;
  	struct ltc4151_data *data;
  	struct device *hwmon_dev;
+	u32 shunt;
 
  	if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
  		return -ENODEV;
@@ -184,6 +187,14 @@ static int ltc4151_probe(struct i2c_client *client,
  	if (!data)
  		return -ENOMEM;
 
+	if (of_property_read_u32(client->dev.of_node, "shunt-resistor", &shunt))
+		shunt = 1000; /* 1 mOhm if not set via DT */
+
+	if (shunt == 0)
+		return -EINVAL;
+
+	data->shunt = shunt;
+
  	data->client = client;
  	mutex_init(&data->update_lock);
 
@@ -199,10 +210,16 @@ static const struct i2c_device_id ltc4151_id[] = {
  };
  MODULE_DEVICE_TABLE(i2c, ltc4151_id);
 
Quoted 15 lines
Quoted 4 lines
drivers/hwmon/ltc4151.c +3 -0
+static const struct of_device_id ltc4151_match[] = {
+	{ .compatible = "lltc,ltc4151" },
+	{},

drivers/hwmon/ltc4151.c static const struct i2c_device_id ltc4151_id[] = { · line 225

Quoted 10 lines
drivers/hwmon/ltc4151.c +3 -0
+};
+
  /* This is the driver that will be inserted */
  static struct i2c_driver ltc4151_driver = {
  	.driver = {
  		.name	= "ltc4151",
+		.of_match_table = of_match_ptr(ltc4151_match),
  	},
  	.probe		= ltc4151_probe,
  	.id_table	= ltc4151_id,

--
To unsubscribe from this list: send the line "unsubscribe devicetree" in the body of a message to [email protected] More majordomo info at http://vger.kernel.org/majordomo-info.html

A message that is not in the archive (sent off-list or not yet received): [email protected]

#9 lore raw
Quoted 10 lines · Daniel Golle

Hi Guenter,

Quoted 7 lines · Guenter Roeck

Daniel,

Quoted 4 lines · Daniel Golle

Allow to specify the resistance of the attached shunt via DT by adding the shunt-resistor property. Fall-back to the previous default (1 mOhm) if unset.

Quoted 7 lines
Quoted 3 lines
Quoted 1 line

Please resend with the property name changed as asked for by Rob.

Ok, will do. I originally copy-pasted the property name from ina209, so I reckon it's supposed to be changed there as well...?

One can not simply change an existing property. We would have to deprecate the old one, define a replacement, and have the driver support both. For ina2xx, it would have to be done in the iio driver as well.

Sure, if you feel up to it, feel free to prepare a set of patches to do that.

Note that Documentation/devicetree/bindings/property-units.txt documents that -micro-ohms should be used.

Thanks,
Guenter

--
To unsubscribe from this list: send the line "unsubscribe devicetree" in the body of a message to [email protected] More majordomo info at http://vger.kernel.org/majordomo-info.html

#10 lore raw

PATCH 2/2 hwmon: (ltc4151) Add devicetree binding for ltc4151

In reply to a message not in the archive
---
 Documentation/devicetree/bindings/hwmon/ltc4151.txt | 18 ++++++++++++++++++
 1 file changed, 18 insertions(+)
 create mode 100644 Documentation/devicetree/bindings/hwmon/ltc4151.txt
Documentation/devicetree/bindings/hwmon/ltc4151.txt new file +18 -0
new file mode 100644
index 0000000..aec69ed
@@ -0,0 +1,18 @@
+ltc4151 properties
+
+Required properties:
+- compatible: Must be "lltc,ltc4151"
+- reg: I2C address
+
+Optional properties:
+
+- shunt-resistor
+	Shunt resistor value in micro-Ohm
+
+Example:
+
+ltc4151@6e {
+	compatible = "lltc,ltc4151";
+	reg = <0x6e>;
+	shunt-resistor = <1500>;
+};
Signature · 6 lines
2.9.0

--
To unsubscribe from this list: send the line "unsubscribe devicetree" in
the body of a message to [email protected]
More majordomo info at  http://vger.kernel.org/majordomo-info.html

A message that is not in the archive (sent off-list or not yet received): [email protected]

#12 lore raw

Re: [PATCH 2/2] hwmon: (ltc4151) Add devicetree binding for ltc4151

Documentation/devicetree/bindings/hwmon/ltc4151.txt line 1

Quoted 13 lines · Daniel Golle
---
 Documentation/devicetree/bindings/hwmon/ltc4151.txt | 18 ++++++++++++++++++
 1 file changed, 18 insertions(+)
 create mode 100644 Documentation/devicetree/bindings/hwmon/ltc4151.txt
Documentation/devicetree/bindings/hwmon/ltc4151.txt new file +1 -0
new file mode 100644
index 0000000..aec69ed
@@ -0,0 +1,18 @@
+ltc4151 properties

Add a brief description of what the chip does please.

Documentation/devicetree/bindings/hwmon/ltc4151.txt line 10

Quoted 9 lines
Documentation/devicetree/bindings/hwmon/ltc4151.txt new file +9 -0
+
+Required properties:
+- compatible: Must be "lltc,ltc4151"
+- reg: I2C address
+
+Optional properties:
+
+- shunt-resistor
+	Shunt resistor value in micro-Ohm

Add a unit suffix (-micro-ohms).

Quoted 15 lines
Documentation/devicetree/bindings/hwmon/ltc4151.txt new file +8 -0
+
+Example:
+
+ltc4151@6e {
+	compatible = "lltc,ltc4151";
+	reg = <0x6e>;
+	shunt-resistor = <1500>;
+};
Signature · 6 lines
2.9.0

--
To unsubscribe from this list: send the line "unsubscribe devicetree" in
the body of a message to [email protected]
More majordomo info at  http://vger.kernel.org/majordomo-info.html

--
To unsubscribe from this list: send the line "unsubscribe devicetree" in the body of a message to [email protected] More majordomo info at http://vger.kernel.org/majordomo-info.html

#13 lore raw

PATCH v2 1/2 hwmon: (ltc4151) Make shunt-resistor configurable

Allow to specify the resistance of the attached shunt via DT by adding the shunt-resistor property. Fall-back to the previous default (1 mOhm) if unset.

---
v2: add -micro-ohms unit suffix

 drivers/hwmon/ltc4151.c | 21 +++++++++++++++++++--
 1 file changed, 19 insertions(+), 2 deletions(-)
drivers/hwmon/ltc4151.c +19 -2
index c86a184..d36e5e1 100644
@@ -30,6 +30,7 @@
 
 #include <linux/kernel.h>
 #include <linux/module.h>
+#include <linux/of.h>
 #include <linux/init.h>
 #include <linux/err.h>
 #include <linux/slab.h>
@@ -52,6 +53,7 @@ struct ltc4151_data {
 	struct mutex update_lock;
 	bool valid;
 	unsigned long last_updated; /* in jiffies */
+	unsigned int shunt; /* in micro ohms */
 
 	/* Registers */
 	u8 regs[6];
@@ -111,9 +113,9 @@ static int ltc4151_get_value(struct ltc4151_data *data, u8 reg)
 	case LTC4151_SENSE_H:
 		/*
 		 * 20uV resolution. Convert to current as measured with
-		 * an 1 mOhm sense resistor, in mA.
+		 * a given sense resistor, in mA.
 		 */
-		val = val * 20;
+		val = val * 20 * 1000 / data->shunt;
 		break;
 	case LTC4151_VIN_H:
 		/* 25 mV per increment */
@@ -176,6 +178,7 @@ static int ltc4151_probe(struct i2c_client *client,
 	struct device *dev = &client->dev;
 	struct ltc4151_data *data;
 	struct device *hwmon_dev;
+	u32 shunt;
 
 	if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
 		return -ENODEV;
@@ -184,6 +187,14 @@ static int ltc4151_probe(struct i2c_client *client,
 	if (!data)
 		return -ENOMEM;
 
+	if (of_property_read_u32(client->dev.of_node, "shunt-resistor-micro-ohms", &shunt))
+		shunt = 1000; /* 1 mOhm if not set via DT */
+
+	if (shunt == 0)
+		return -EINVAL;
+
+	data->shunt = shunt;
+
 	data->client = client;
 	mutex_init(&data->update_lock);
 
@@ -199,10 +210,16 @@ static const struct i2c_device_id ltc4151_id[] = {
 };
 MODULE_DEVICE_TABLE(i2c, ltc4151_id);
 
+static const struct of_device_id ltc4151_match[] = {
+	{ .compatible = "lltc,ltc4151" },
+	{},
+};
+
 /* This is the driver that will be inserted */
 static struct i2c_driver ltc4151_driver = {
 	.driver = {
 		.name	= "ltc4151",
+		.of_match_table = of_match_ptr(ltc4151_match),
 	},
 	.probe		= ltc4151_probe,
 	.id_table	= ltc4151_id,
Signature · 1 line
2.9.2
#14 lore raw

PATCH 2/2 hwmon: (ltc4151) Add devicetree binding for ltc4151

In reply to Rob Herring

---
v2: add description and -micro-ohms unit suffix to property

 Documentation/devicetree/bindings/hwmon/ltc4151.txt | 18 ++++++++++++++++++
 1 file changed, 18 insertions(+)
 create mode 100644 Documentation/devicetree/bindings/hwmon/ltc4151.txt
Documentation/devicetree/bindings/hwmon/ltc4151.txt new file +18 -0
new file mode 100644
index 0000000..d008a5e
@@ -0,0 +1,18 @@
+LTC4151 High Voltage I2C Current and Voltage Monitor
+
+Required properties:
+- compatible: Must be "lltc,ltc4151"
+- reg: I2C address
+
+Optional properties:
+- shunt-resistor-micro-ohms
+	Shunt resistor value in micro-Ohms
+	Defaults to <1000> if unset.
+
+Example:
+
+ltc4151@6e {
+	compatible = "lltc,ltc4151";
+	reg = <0x6e>;
+	shunt-resistor-micro-ohms = <1500>;
+};
Signature · 1 line
2.9.2
#15 lore raw

PATCH v2 1/2 hwmon: (ltc4151) Make shunt-resistor configurable

In reply to Guenter Roeck

Allow to specify the resistance of the attached shunt via DT by adding the shunt-resistor property. Fall-back to the previous default (1 mOhm) if unset.

---
v2: removed unneeded things and throw -EINVAL for shunt==0 (resent to linux-hwmon which by accident wasn't among the receipients)

 drivers/hwmon/ltc4151.c | 21 +++++++++++++++++++--
 1 file changed, 19 insertions(+), 2 deletions(-)
drivers/hwmon/ltc4151.c +19 -2
index c86a184..419c6f7 100644
@@ -30,6 +30,7 @@
 
 #include <linux/kernel.h>
 #include <linux/module.h>
+#include <linux/of.h>
 #include <linux/init.h>
 #include <linux/err.h>
 #include <linux/slab.h>
@@ -52,6 +53,7 @@ struct ltc4151_data {
 	struct mutex update_lock;
 	bool valid;
 	unsigned long last_updated; /* in jiffies */
+	unsigned int shunt; /* in micro ohms */
 
 	/* Registers */
 	u8 regs[6];
@@ -111,9 +113,9 @@ static int ltc4151_get_value(struct ltc4151_data *data, u8 reg)
 	case LTC4151_SENSE_H:
 		/*
 		 * 20uV resolution. Convert to current as measured with
-		 * an 1 mOhm sense resistor, in mA.
+		 * a given sense resistor, in mA.
 		 */
-		val = val * 20;
+		val = val * 20 * 1000 / data->shunt;
 		break;
 	case LTC4151_VIN_H:
 		/* 25 mV per increment */
@@ -176,6 +178,7 @@ static int ltc4151_probe(struct i2c_client *client,
 	struct device *dev = &client->dev;
 	struct ltc4151_data *data;
 	struct device *hwmon_dev;
+	u32 shunt;
 
 	if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
 		return -ENODEV;
@@ -184,6 +187,14 @@ static int ltc4151_probe(struct i2c_client *client,
 	if (!data)
 		return -ENOMEM;
 
+	if (of_property_read_u32(client->dev.of_node, "shunt-resistor", &shunt))
+		shunt = 1000; /* 1 mOhm if not set via DT */
+
+	if (shunt == 0)
+		return -EINVAL;
+
+	data->shunt = shunt;
+
 	data->client = client;
 	mutex_init(&data->update_lock);
 
@@ -199,10 +210,16 @@ static const struct i2c_device_id ltc4151_id[] = {
 };
 MODULE_DEVICE_TABLE(i2c, ltc4151_id);
 
+static const struct of_device_id ltc4151_match[] = {
+	{ .compatible = "lltc,ltc4151" },
+	{},
+};
+
 /* This is the driver that will be inserted */
 static struct i2c_driver ltc4151_driver = {
 	.driver = {
 		.name	= "ltc4151",
+		.of_match_table = of_match_ptr(ltc4151_match),
 	},
 	.probe		= ltc4151_probe,
 	.id_table	= ltc4151_id,
Signature · 1 line
2.9.0
#16 lore raw

PATCH 2/2 hwmon: (ltc4151) Add devicetree binding for ltc4151

In reply to Guenter Roeck

---
(resent to linux-hwmon which by accident wasn't among the receipients)

 Documentation/devicetree/bindings/hwmon/ltc4151.txt | 18 ++++++++++++++++++
 1 file changed, 18 insertions(+)
 create mode 100644 Documentation/devicetree/bindings/hwmon/ltc4151.txt
Documentation/devicetree/bindings/hwmon/ltc4151.txt new file +18 -0
new file mode 100644
index 0000000..aec69ed
@@ -0,0 +1,18 @@
+ltc4151 properties
+
+Required properties:
+- compatible: Must be "lltc,ltc4151"
+- reg: I2C address
+
+Optional properties:
+
+- shunt-resistor
+	Shunt resistor value in micro-Ohm
+
+Example:
+
+ltc4151@6e {
+	compatible = "lltc,ltc4151";
+	reg = <0x6e>;
+	shunt-resistor = <1500>;
+};
Signature · 1 line
2.9.0