From 3c535bca9a43eb04d64537b55994c88e399a9981 Mon Sep 17 00:00:00 2001 From: Krzysztof Kozlowski Date: Mon, 5 Jan 2015 09:57:55 +0100 Subject: [PATCH 01/19] hwmon: (nct7802) Constify struct regmap_config The regmap_config struct may be const because it is not modified by the driver and regmap_init() accepts pointer to const. Signed-off-by: Krzysztof Kozlowski Signed-off-by: Guenter Roeck --- drivers/hwmon/nct7802.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/drivers/hwmon/nct7802.c b/drivers/hwmon/nct7802.c index ec5678289e4a..55765790907b 100644 --- a/drivers/hwmon/nct7802.c +++ b/drivers/hwmon/nct7802.c @@ -779,7 +779,7 @@ static bool nct7802_regmap_is_volatile(struct device *dev, unsigned int reg) return reg != REG_BANK && reg <= 0x20; } -static struct regmap_config nct7802_regmap_config = { +static const struct regmap_config nct7802_regmap_config = { .reg_bits = 8, .val_bits = 8, .cache_type = REGCACHE_RBTREE, From 509416a8e741d11d65c027f510b79573f69f6de8 Mon Sep 17 00:00:00 2001 From: Bartosz Golaszewski Date: Mon, 5 Jan 2015 15:20:52 +0100 Subject: [PATCH 02/19] hwmon: (ina2xx) reinitialize the chip in case it's been reset Chips from the ina family don't like to be uninitialized. In case the power is cut-off and restored again the calibration register will be reset to 0 and both the power and current registers will remain at 0. Check the calibration register in ina2xx_update_device() and reinitialize the chip if needed. Signed-off-by: Bartosz Golaszewski Signed-off-by: Guenter Roeck --- drivers/hwmon/ina2xx.c | 148 ++++++++++++++++++++++++++++------------- 1 file changed, 101 insertions(+), 47 deletions(-) diff --git a/drivers/hwmon/ina2xx.c b/drivers/hwmon/ina2xx.c index e01feba909c3..ffbd60f52619 100644 --- a/drivers/hwmon/ina2xx.c +++ b/drivers/hwmon/ina2xx.c @@ -35,6 +35,7 @@ #include #include #include +#include #include @@ -64,6 +65,9 @@ /* worst case is 68.10 ms (~14.6Hz, ina219) */ #define INA2XX_CONVERSION_RATE 15 +#define INA2XX_MAX_DELAY 69 /* worst case delay in ms */ + +#define INA2XX_RSHUNT_DEFAULT 10000 enum ina2xx_ids { ina219, ina226 }; @@ -81,6 +85,8 @@ struct ina2xx_data { struct i2c_client *client; const struct ina2xx_config *config; + long rshunt; + struct mutex update_lock; bool valid; unsigned long last_updated; @@ -110,34 +116,96 @@ static const struct ina2xx_config ina2xx_config[] = { }, }; -static struct ina2xx_data *ina2xx_update_device(struct device *dev) +/* + * Initialize the configuration and calibration registers. + */ +static int ina2xx_init(struct ina2xx_data *data) +{ + struct i2c_client *client = data->client; + int ret; + + /* device configuration */ + ret = i2c_smbus_write_word_swapped(client, INA2XX_CONFIG, + data->config->config_default); + if (ret < 0) + return ret; + + /* + * Set current LSB to 1mA, shunt is in uOhms + * (equation 13 in datasheet). + */ + return i2c_smbus_write_word_swapped(client, INA2XX_CALIBRATION, + data->config->calibration_factor / data->rshunt); +} + +static int ina2xx_do_update(struct device *dev) { struct ina2xx_data *data = dev_get_drvdata(dev); struct i2c_client *client = data->client; + int i, rv, retry; + + dev_dbg(&client->dev, "Starting ina2xx update\n"); + + for (retry = 5; retry; retry--) { + /* Read all registers */ + for (i = 0; i < data->config->registers; i++) { + rv = i2c_smbus_read_word_swapped(client, i); + if (rv < 0) + return rv; + data->regs[i] = rv; + } + + /* + * If the current value in the calibration register is 0, the + * power and current registers will also remain at 0. In case + * the chip has been reset let's check the calibration + * register and reinitialize if needed. + */ + if (data->regs[INA2XX_CALIBRATION] == 0) { + dev_warn(dev, "chip not calibrated, reinitializing\n"); + + rv = ina2xx_init(data); + if (rv < 0) + return rv; + + /* + * Let's make sure the power and current registers + * have been updated before trying again. + */ + msleep(INA2XX_MAX_DELAY); + continue; + } + + data->last_updated = jiffies; + data->valid = 1; + + return 0; + } + + /* + * If we're here then although all write operations succeeded, the + * chip still returns 0 in the calibration register. Nothing more we + * can do here. + */ + dev_err(dev, "unable to reinitialize the chip\n"); + return -ENODEV; +} + +static struct ina2xx_data *ina2xx_update_device(struct device *dev) +{ + struct ina2xx_data *data = dev_get_drvdata(dev); struct ina2xx_data *ret = data; + int rv; mutex_lock(&data->update_lock); if (time_after(jiffies, data->last_updated + HZ / INA2XX_CONVERSION_RATE) || !data->valid) { - - int i; - - dev_dbg(&client->dev, "Starting ina2xx update\n"); - - /* Read all registers */ - for (i = 0; i < data->config->registers; i++) { - int rv = i2c_smbus_read_word_swapped(client, i); - if (rv < 0) { - ret = ERR_PTR(rv); - goto abort; - } - data->regs[i] = rv; - } - data->last_updated = jiffies; - data->valid = 1; + rv = ina2xx_do_update(dev); + if (rv < 0) + ret = ERR_PTR(rv); } -abort: + mutex_unlock(&data->update_lock); return ret; } @@ -221,7 +289,6 @@ static int ina2xx_probe(struct i2c_client *client, struct device *dev = &client->dev; struct ina2xx_data *data; struct device *hwmon_dev; - long shunt = 10000; /* default shunt value 10mOhms */ u32 val; int ret; @@ -234,41 +301,28 @@ static int ina2xx_probe(struct i2c_client *client, if (dev_get_platdata(dev)) { pdata = dev_get_platdata(dev); - shunt = pdata->shunt_uohms; + data->rshunt = pdata->shunt_uohms; } else if (!of_property_read_u32(dev->of_node, "shunt-resistor", &val)) { - shunt = val; + data->rshunt = val; + } else { + data->rshunt = INA2XX_RSHUNT_DEFAULT; } - if (shunt <= 0) - return -ENODEV; - /* set the device type */ data->kind = id->driver_data; data->config = &ina2xx_config[data->kind]; - - /* device configuration */ - ret = i2c_smbus_write_word_swapped(client, INA2XX_CONFIG, - data->config->config_default); - if (ret < 0) { - dev_err(dev, - "error writing to the config register: %d", ret); - return -ENODEV; - } - - /* - * Set current LSB to 1mA, shunt is in uOhms - * (equation 13 in datasheet). - */ - ret = i2c_smbus_write_word_swapped(client, INA2XX_CALIBRATION, - data->config->calibration_factor / shunt); - if (ret < 0) { - dev_err(dev, - "error writing to the calibration register: %d", ret); - return -ENODEV; - } - data->client = client; + + if (data->rshunt <= 0) + return -ENODEV; + + ret = ina2xx_init(data); + if (ret < 0) { + dev_err(dev, "error configuring the device: %d\n", ret); + return -ENODEV; + } + mutex_init(&data->update_lock); hwmon_dev = devm_hwmon_device_register_with_groups(dev, client->name, @@ -277,7 +331,7 @@ static int ina2xx_probe(struct i2c_client *client, return PTR_ERR(hwmon_dev); dev_info(dev, "power monitor %s (Rshunt = %li uOhm)\n", - id->name, shunt); + id->name, data->rshunt); return 0; } From f4fe902717f3a3fa10e30544cef97b2ff2811db8 Mon Sep 17 00:00:00 2001 From: Bartosz Golaszewski Date: Mon, 5 Jan 2015 15:20:53 +0100 Subject: [PATCH 03/19] hwmon: (ina2xx) remove a stray new line Signed-off-by: Bartosz Golaszewski Signed-off-by: Guenter Roeck --- drivers/hwmon/ina2xx.c | 1 - 1 file changed, 1 deletion(-) diff --git a/drivers/hwmon/ina2xx.c b/drivers/hwmon/ina2xx.c index ffbd60f52619..39e017bf92fd 100644 --- a/drivers/hwmon/ina2xx.c +++ b/drivers/hwmon/ina2xx.c @@ -52,7 +52,6 @@ #define INA226_ALERT_LIMIT 0x07 #define INA226_DIE_ID 0xFF - /* register count */ #define INA219_REGISTERS 6 #define INA226_REGISTERS 8 From e794704000cf7954039b8daab9dfae2a74142f07 Mon Sep 17 00:00:00 2001 From: Bartosz Golaszewski Date: Mon, 5 Jan 2015 15:20:54 +0100 Subject: [PATCH 04/19] hwmon: (ina2xx) don't accept shunt values greater than the calibration factor Shunt resistance values greater than the chip's calibration factor make no sense since the actual value written to the register equals: / Bail-out from ina2xx_probe() if the configured value is greater than the calibration factor. Signed-off-by: Bartosz Golaszewski Signed-off-by: Guenter Roeck --- drivers/hwmon/ina2xx.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/drivers/hwmon/ina2xx.c b/drivers/hwmon/ina2xx.c index 39e017bf92fd..3234e571805c 100644 --- a/drivers/hwmon/ina2xx.c +++ b/drivers/hwmon/ina2xx.c @@ -313,7 +313,8 @@ static int ina2xx_probe(struct i2c_client *client, data->config = &ina2xx_config[data->kind]; data->client = client; - if (data->rshunt <= 0) + if (data->rshunt <= 0 || + data->rshunt > data->config->calibration_factor) return -ENODEV; ret = ina2xx_init(data); From 8a5fc79513afe325fd755026299d0bfdb47e42de Mon Sep 17 00:00:00 2001 From: Bartosz Golaszewski Date: Mon, 5 Jan 2015 15:20:55 +0100 Subject: [PATCH 05/19] hwmon: (ina2xx) make shunt resistance configurable at run-time The shunt resistance can only be set via platform_data or device tree. This isn't suitable for devices in which the shunt resistance can change/isn't known at boot-time. Add a sysfs attribute that allows to read and set the shunt resistance. Signed-off-by: Bartosz Golaszewski Signed-off-by: Guenter Roeck --- Documentation/hwmon/ina2xx | 5 ++-- drivers/hwmon/ina2xx.c | 48 ++++++++++++++++++++++++++++++++++++-- 2 files changed, 49 insertions(+), 4 deletions(-) diff --git a/Documentation/hwmon/ina2xx b/Documentation/hwmon/ina2xx index 4223c2d3b508..320dd69fb5e6 100644 --- a/Documentation/hwmon/ina2xx +++ b/Documentation/hwmon/ina2xx @@ -44,6 +44,7 @@ The INA226 monitors both a shunt voltage drop and bus supply voltage. The INA230 is a high or low side current shunt and power monitor with an I2C interface. The INA230 monitors both a shunt voltage drop and bus supply voltage. -The shunt value in micro-ohms can be set via platform data or device tree. -Please refer to the Documentation/devicetree/bindings/i2c/ina2xx.txt for bindings +The shunt value in micro-ohms can be set via platform data or device tree at +compile-time or via the shunt_resistor attribute in sysfs at run-time. Please +refer to the Documentation/devicetree/bindings/i2c/ina2xx.txt for bindings if the device tree is used. diff --git a/drivers/hwmon/ina2xx.c b/drivers/hwmon/ina2xx.c index 3234e571805c..49537ea80748 100644 --- a/drivers/hwmon/ina2xx.c +++ b/drivers/hwmon/ina2xx.c @@ -115,6 +115,12 @@ static const struct ina2xx_config ina2xx_config[] = { }, }; +static int ina2xx_calibrate(struct ina2xx_data *data) +{ + return i2c_smbus_write_word_swapped(data->client, INA2XX_CALIBRATION, + data->config->calibration_factor / data->rshunt); +} + /* * Initialize the configuration and calibration registers. */ @@ -133,8 +139,7 @@ static int ina2xx_init(struct ina2xx_data *data) * Set current LSB to 1mA, shunt is in uOhms * (equation 13 in datasheet). */ - return i2c_smbus_write_word_swapped(client, INA2XX_CALIBRATION, - data->config->calibration_factor / data->rshunt); + return ina2xx_calibrate(data); } static int ina2xx_do_update(struct device *dev) @@ -231,6 +236,9 @@ static int ina2xx_get_value(struct ina2xx_data *data, u8 reg) /* signed register, LSB=1mA (selected), in mA */ val = (s16)data->regs[reg]; break; + case INA2XX_CALIBRATION: + val = data->config->calibration_factor / data->regs[reg]; + break; default: /* programmer goofed */ WARN_ON_ONCE(1); @@ -254,6 +262,36 @@ static ssize_t ina2xx_show_value(struct device *dev, ina2xx_get_value(data, attr->index)); } +static ssize_t ina2xx_set_shunt(struct device *dev, + struct device_attribute *da, + const char *buf, size_t count) +{ + struct ina2xx_data *data = ina2xx_update_device(dev); + unsigned long val; + int status; + + if (IS_ERR(data)) + return PTR_ERR(data); + + status = kstrtoul(buf, 10, &val); + if (status < 0) + return status; + + if (val == 0 || + /* Values greater than the calibration factor make no sense. */ + val > data->config->calibration_factor) + return -EINVAL; + + mutex_lock(&data->update_lock); + data->rshunt = val; + status = ina2xx_calibrate(data); + mutex_unlock(&data->update_lock); + if (status < 0) + return status; + + return count; +} + /* shunt voltage */ static SENSOR_DEVICE_ATTR(in0_input, S_IRUGO, ina2xx_show_value, NULL, INA2XX_SHUNT_VOLTAGE); @@ -270,12 +308,18 @@ static SENSOR_DEVICE_ATTR(curr1_input, S_IRUGO, ina2xx_show_value, NULL, static SENSOR_DEVICE_ATTR(power1_input, S_IRUGO, ina2xx_show_value, NULL, INA2XX_POWER); +/* shunt resistance */ +static SENSOR_DEVICE_ATTR(shunt_resistor, S_IRUGO | S_IWUSR, + ina2xx_show_value, ina2xx_set_shunt, + INA2XX_CALIBRATION); + /* pointers to created device attributes */ static struct attribute *ina2xx_attrs[] = { &sensor_dev_attr_in0_input.dev_attr.attr, &sensor_dev_attr_in1_input.dev_attr.attr, &sensor_dev_attr_curr1_input.dev_attr.attr, &sensor_dev_attr_power1_input.dev_attr.attr, + &sensor_dev_attr_shunt_resistor.dev_attr.attr, NULL, }; ATTRIBUTE_GROUPS(ina2xx); From 72a87a47a81e062fc27b7675db33cf29458bc6d2 Mon Sep 17 00:00:00 2001 From: Bartosz Golaszewski Date: Fri, 9 Jan 2015 17:03:42 +0100 Subject: [PATCH 06/19] hwmon: (ina2xx) implement update_interval attribute for ina226 This attribute allows to configure the update interval of ina226. Although the bus and shunt voltage conversion times remain hardcoded to 1.1 ms, we can now modify said interval by changing the averaging rate. While we're at it - add an additional variable to ina2xx_data, which holds the current configuration settings - this way we'll be able to restore the configuration in case of an unexpected chip reset. Signed-off-by: Bartosz Golaszewski Signed-off-by: Guenter Roeck --- Documentation/hwmon/ina2xx | 7 ++ drivers/hwmon/ina2xx.c | 164 +++++++++++++++++++++++++++++++++++-- 2 files changed, 165 insertions(+), 6 deletions(-) diff --git a/Documentation/hwmon/ina2xx b/Documentation/hwmon/ina2xx index 320dd69fb5e6..450e3ccd983d 100644 --- a/Documentation/hwmon/ina2xx +++ b/Documentation/hwmon/ina2xx @@ -48,3 +48,10 @@ The shunt value in micro-ohms can be set via platform data or device tree at compile-time or via the shunt_resistor attribute in sysfs at run-time. Please refer to the Documentation/devicetree/bindings/i2c/ina2xx.txt for bindings if the device tree is used. + +Additionally ina226 supports update_interval attribute as described in +Documentation/hwmon/sysfs-interface. Internally the interval is the sum of +bus and shunt voltage conversion times multiplied by the averaging rate. We +don't touch the conversion times and only modify the number of averages. The +lower limit of the update_interval is 2 ms, the upper limit is 2253 ms. +The actual programmed interval may vary from the desired value. diff --git a/drivers/hwmon/ina2xx.c b/drivers/hwmon/ina2xx.c index 49537ea80748..a16d6a283286 100644 --- a/drivers/hwmon/ina2xx.c +++ b/drivers/hwmon/ina2xx.c @@ -68,6 +68,21 @@ #define INA2XX_RSHUNT_DEFAULT 10000 +/* bit mask for reading the averaging setting in the configuration register */ +#define INA226_AVG_RD_MASK 0x0E00 + +#define INA226_READ_AVG(reg) (((reg) & INA226_AVG_RD_MASK) >> 9) +#define INA226_SHIFT_AVG(val) ((val) << 9) + +/* common attrs, ina226 attrs and NULL */ +#define INA2XX_MAX_ATTRIBUTE_GROUPS 3 + +/* + * Both bus voltage and shunt voltage conversion times for ina226 are set + * to 0b0100 on POR, which translates to 2200 microseconds in total. + */ +#define INA226_TOTAL_CONV_TIME_DEFAULT 2200 + enum ina2xx_ids { ina219, ina226 }; struct ina2xx_config { @@ -85,12 +100,15 @@ struct ina2xx_data { const struct ina2xx_config *config; long rshunt; + u16 curr_config; struct mutex update_lock; bool valid; unsigned long last_updated; + int update_interval; /* in jiffies */ int kind; + const struct attribute_group *groups[INA2XX_MAX_ATTRIBUTE_GROUPS]; u16 regs[INA2XX_MAX_REGISTERS]; }; @@ -115,6 +133,57 @@ static const struct ina2xx_config ina2xx_config[] = { }, }; +/* + * Available averaging rates for ina226. The indices correspond with + * the bit values expected by the chip (according to the ina226 datasheet, + * table 3 AVG bit settings, found at + * http://www.ti.com/lit/ds/symlink/ina226.pdf. + */ +static const int ina226_avg_tab[] = { 1, 4, 16, 64, 128, 256, 512, 1024 }; + +static int ina226_avg_bits(int avg) +{ + int i; + + /* Get the closest average from the tab. */ + for (i = 0; i < ARRAY_SIZE(ina226_avg_tab) - 1; i++) { + if (avg <= (ina226_avg_tab[i] + ina226_avg_tab[i + 1]) / 2) + break; + } + + return i; /* Return 0b0111 for values greater than 1024. */ +} + +static int ina226_reg_to_interval(u16 config) +{ + int avg = ina226_avg_tab[INA226_READ_AVG(config)]; + + /* + * Multiply the total conversion time by the number of averages. + * Return the result in milliseconds. + */ + return DIV_ROUND_CLOSEST(avg * INA226_TOTAL_CONV_TIME_DEFAULT, 1000); +} + +static u16 ina226_interval_to_reg(int interval, u16 config) +{ + int avg, avg_bits; + + avg = DIV_ROUND_CLOSEST(interval * 1000, + INA226_TOTAL_CONV_TIME_DEFAULT); + avg_bits = ina226_avg_bits(avg); + + return (config & ~INA226_AVG_RD_MASK) | INA226_SHIFT_AVG(avg_bits); +} + +static void ina226_set_update_interval(struct ina2xx_data *data) +{ + int ms; + + ms = ina226_reg_to_interval(data->curr_config); + data->update_interval = msecs_to_jiffies(ms); +} + static int ina2xx_calibrate(struct ina2xx_data *data) { return i2c_smbus_write_word_swapped(data->client, INA2XX_CALIBRATION, @@ -131,7 +200,7 @@ static int ina2xx_init(struct ina2xx_data *data) /* device configuration */ ret = i2c_smbus_write_word_swapped(client, INA2XX_CONFIG, - data->config->config_default); + data->curr_config); if (ret < 0) return ret; @@ -199,12 +268,13 @@ static struct ina2xx_data *ina2xx_update_device(struct device *dev) { struct ina2xx_data *data = dev_get_drvdata(dev); struct ina2xx_data *ret = data; + unsigned long after; int rv; mutex_lock(&data->update_lock); - if (time_after(jiffies, data->last_updated + - HZ / INA2XX_CONVERSION_RATE) || !data->valid) { + after = data->last_updated + data->update_interval; + if (time_after(jiffies, after) || !data->valid) { rv = ina2xx_do_update(dev); if (rv < 0) ret = ERR_PTR(rv); @@ -292,6 +362,58 @@ static ssize_t ina2xx_set_shunt(struct device *dev, return count; } +static ssize_t ina226_set_interval(struct device *dev, + struct device_attribute *da, + const char *buf, size_t count) +{ + struct ina2xx_data *data = dev_get_drvdata(dev); + unsigned long val; + int status; + + if (IS_ERR(data)) + return PTR_ERR(data); + + status = kstrtoul(buf, 10, &val); + if (status < 0) + return status; + + if (val > INT_MAX || val == 0) + return -EINVAL; + + mutex_lock(&data->update_lock); + data->curr_config = ina226_interval_to_reg(val, + data->regs[INA2XX_CONFIG]); + status = i2c_smbus_write_word_swapped(data->client, + INA2XX_CONFIG, + data->curr_config); + + ina226_set_update_interval(data); + /* Make sure the next access re-reads all registers. */ + data->valid = 0; + mutex_unlock(&data->update_lock); + if (status < 0) + return status; + + return count; +} + +static ssize_t ina226_show_interval(struct device *dev, + struct device_attribute *da, char *buf) +{ + struct ina2xx_data *data = ina2xx_update_device(dev); + + if (IS_ERR(data)) + return PTR_ERR(data); + + /* + * We don't use data->update_interval here as we want to display + * the actual interval used by the chip and jiffies_to_msecs() + * doesn't seem to be accurate enough. + */ + return snprintf(buf, PAGE_SIZE, "%d\n", + ina226_reg_to_interval(data->regs[INA2XX_CONFIG])); +} + /* shunt voltage */ static SENSOR_DEVICE_ATTR(in0_input, S_IRUGO, ina2xx_show_value, NULL, INA2XX_SHUNT_VOLTAGE); @@ -313,6 +435,10 @@ static SENSOR_DEVICE_ATTR(shunt_resistor, S_IRUGO | S_IWUSR, ina2xx_show_value, ina2xx_set_shunt, INA2XX_CALIBRATION); +/* update interval (ina226 only) */ +static SENSOR_DEVICE_ATTR(update_interval, S_IRUGO | S_IWUSR, + ina226_show_interval, ina226_set_interval, 0); + /* pointers to created device attributes */ static struct attribute *ina2xx_attrs[] = { &sensor_dev_attr_in0_input.dev_attr.attr, @@ -322,7 +448,19 @@ static struct attribute *ina2xx_attrs[] = { &sensor_dev_attr_shunt_resistor.dev_attr.attr, NULL, }; -ATTRIBUTE_GROUPS(ina2xx); + +static const struct attribute_group ina2xx_group = { + .attrs = ina2xx_attrs, +}; + +static struct attribute *ina226_attrs[] = { + &sensor_dev_attr_update_interval.dev_attr.attr, + NULL, +}; + +static const struct attribute_group ina226_group = { + .attrs = ina226_attrs, +}; static int ina2xx_probe(struct i2c_client *client, const struct i2c_device_id *id) @@ -333,7 +471,7 @@ static int ina2xx_probe(struct i2c_client *client, struct ina2xx_data *data; struct device *hwmon_dev; u32 val; - int ret; + int ret, group = 0; if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_WORD_DATA)) return -ENODEV; @@ -355,8 +493,18 @@ static int ina2xx_probe(struct i2c_client *client, /* set the device type */ data->kind = id->driver_data; data->config = &ina2xx_config[data->kind]; + data->curr_config = data->config->config_default; data->client = client; + /* + * Ina226 has a variable update_interval. For ina219 we + * use a constant value. + */ + if (data->kind == ina226) + ina226_set_update_interval(data); + else + data->update_interval = HZ / INA2XX_CONVERSION_RATE; + if (data->rshunt <= 0 || data->rshunt > data->config->calibration_factor) return -ENODEV; @@ -369,8 +517,12 @@ static int ina2xx_probe(struct i2c_client *client, mutex_init(&data->update_lock); + data->groups[group++] = &ina2xx_group; + if (data->kind == ina226) + data->groups[group++] = &ina226_group; + hwmon_dev = devm_hwmon_device_register_with_groups(dev, client->name, - data, ina2xx_groups); + data, data->groups); if (IS_ERR(hwmon_dev)) return PTR_ERR(hwmon_dev); From 71eb7c4c7e6219a484c5185919962a99fb0ddabb Mon Sep 17 00:00:00 2001 From: Bartosz Golaszewski Date: Mon, 12 Jan 2015 14:47:21 +0100 Subject: [PATCH 07/19] hwmon: (ina2xx) remove an unnecessary dev_get_drvdata() result check Signed-off-by: Bartosz Golaszewski Signed-off-by: Guenter Roeck --- drivers/hwmon/ina2xx.c | 3 --- 1 file changed, 3 deletions(-) diff --git a/drivers/hwmon/ina2xx.c b/drivers/hwmon/ina2xx.c index a16d6a283286..ae110c5386ae 100644 --- a/drivers/hwmon/ina2xx.c +++ b/drivers/hwmon/ina2xx.c @@ -370,9 +370,6 @@ static ssize_t ina226_set_interval(struct device *dev, unsigned long val; int status; - if (IS_ERR(data)) - return PTR_ERR(data); - status = kstrtoul(buf, 10, &val); if (status < 0) return status; From b721fe2a3a92b896ba1b41e338471dfef672052a Mon Sep 17 00:00:00 2001 From: Bartosz Golaszewski Date: Mon, 12 Jan 2015 14:47:22 +0100 Subject: [PATCH 08/19] hwmon: (ina2xx) use DIV_ROUND_CLOSEST() to avoid rounding errors Use DIV_ROUND_CLOSEST() when dealing with the calibration values to make the calculations less error prone. Signed-off-by: Bartosz Golaszewski Signed-off-by: Guenter Roeck --- drivers/hwmon/ina2xx.c | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/drivers/hwmon/ina2xx.c b/drivers/hwmon/ina2xx.c index ae110c5386ae..611ec4edd979 100644 --- a/drivers/hwmon/ina2xx.c +++ b/drivers/hwmon/ina2xx.c @@ -186,8 +186,11 @@ static void ina226_set_update_interval(struct ina2xx_data *data) static int ina2xx_calibrate(struct ina2xx_data *data) { - return i2c_smbus_write_word_swapped(data->client, INA2XX_CALIBRATION, - data->config->calibration_factor / data->rshunt); + u16 val = DIV_ROUND_CLOSEST(data->config->calibration_factor, + data->rshunt); + + return i2c_smbus_write_word_swapped(data->client, + INA2XX_CALIBRATION, val); } /* @@ -307,7 +310,8 @@ static int ina2xx_get_value(struct ina2xx_data *data, u8 reg) val = (s16)data->regs[reg]; break; case INA2XX_CALIBRATION: - val = data->config->calibration_factor / data->regs[reg]; + val = DIV_ROUND_CLOSEST(data->config->calibration_factor, + data->regs[reg]); break; default: /* programmer goofed */ From add513be1c47b3b2765c3f618a69c9db26d8dc27 Mon Sep 17 00:00:00 2001 From: Kevin Hilman Date: Wed, 14 Jan 2015 17:34:58 -0800 Subject: [PATCH 09/19] hwmon: (ina2xx) Add ina231 compatible string Add support for "ina231" as compatible string, and update Documentation and Kconfig accordingly. Tested with the Exynos5422-based odroid-xu3 board which has on-board INA231 sensors. Signed-off-by: Kevin Hilman Signed-off-by: Guenter Roeck --- Documentation/hwmon/ina2xx | 11 +++++++++-- drivers/hwmon/Kconfig | 4 ++-- drivers/hwmon/ina2xx.c | 1 + 3 files changed, 12 insertions(+), 4 deletions(-) diff --git a/Documentation/hwmon/ina2xx b/Documentation/hwmon/ina2xx index 450e3ccd983d..cfd31d94c872 100644 --- a/Documentation/hwmon/ina2xx +++ b/Documentation/hwmon/ina2xx @@ -26,6 +26,12 @@ Supported chips: Datasheet: Publicly available at the Texas Instruments website http://www.ti.com/ + * Texas Instruments INA231 + Prefix: 'ina231' + Addresses: I2C 0x40 - 0x4f + Datasheet: Publicly available at the Texas Instruments website + http://www.ti.com/ + Author: Lothar Felten Description @@ -41,8 +47,9 @@ interface. The INA220 monitors both shunt drop and supply voltage. The INA226 is a current shunt and power monitor with an I2C interface. The INA226 monitors both a shunt voltage drop and bus supply voltage. -The INA230 is a high or low side current shunt and power monitor with an I2C -interface. The INA230 monitors both a shunt voltage drop and bus supply voltage. +INA230 and INA231 are high or low side current shunt and power monitors +with an I2C interface. The chips monitor both a shunt voltage drop and +bus supply voltage. The shunt value in micro-ohms can be set via platform data or device tree at compile-time or via the shunt_resistor attribute in sysfs at run-time. Please diff --git a/drivers/hwmon/Kconfig b/drivers/hwmon/Kconfig index a7de26d1ac80..c24394086b40 100644 --- a/drivers/hwmon/Kconfig +++ b/drivers/hwmon/Kconfig @@ -1430,8 +1430,8 @@ config SENSORS_INA2XX tristate "Texas Instruments INA219 and compatibles" depends on I2C help - If you say yes here you get support for INA219, INA220, INA226, and - INA230 power monitor chips. + If you say yes here you get support for INA219, INA220, INA226, + INA230, and INA231 power monitor chips. The INA2xx driver is configured for the default configuration of the part as described in the datasheet. diff --git a/drivers/hwmon/ina2xx.c b/drivers/hwmon/ina2xx.c index 611ec4edd979..d1542b7d4bc3 100644 --- a/drivers/hwmon/ina2xx.c +++ b/drivers/hwmon/ina2xx.c @@ -538,6 +538,7 @@ static const struct i2c_device_id ina2xx_id[] = { { "ina220", ina219 }, { "ina226", ina226 }, { "ina230", ina226 }, + { "ina231", ina226 }, { } }; MODULE_DEVICE_TABLE(i2c, ina2xx_id); From bca6a1ada0464b7179f34f9af62bcdfd60c223e7 Mon Sep 17 00:00:00 2001 From: Guenter Roeck Date: Sun, 18 Jan 2015 17:27:55 -0800 Subject: [PATCH 10/19] hwmon: (jc42) Use sign_extend32 for sign extension Despite the name, sign_extend32 works just fine for 16 bit variables, so it is safe to use. Cc: Martin Kepplinger Reviewed-by: Jean Delvare Signed-off-by: Guenter Roeck --- drivers/hwmon/jc42.c | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/drivers/hwmon/jc42.c b/drivers/hwmon/jc42.c index 388f8bcd898e..262d8086822f 100644 --- a/drivers/hwmon/jc42.c +++ b/drivers/hwmon/jc42.c @@ -213,11 +213,7 @@ static u16 jc42_temp_to_reg(int temp, bool extended) static int jc42_temp_from_reg(s16 reg) { - reg &= 0x1fff; - - /* sign extend register */ - if (reg & 0x1000) - reg |= 0xf000; + reg = sign_extend32(reg, 12); /* convert from 0.0625 to 0.001 resolution */ return reg * 125 / 2; From 3a05633b042fb4aea65b09ce2d0d43689df6eb9a Mon Sep 17 00:00:00 2001 From: Guenter Roeck Date: Sun, 18 Jan 2015 17:29:32 -0800 Subject: [PATCH 11/19] hwmon: (jc42) Fix integer overflow Mixed use of long and int caused an integer overflow when writing large limits. Signed-off-by: Guenter Roeck Reviewed-by: Jean Delvare --- drivers/hwmon/jc42.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/drivers/hwmon/jc42.c b/drivers/hwmon/jc42.c index 262d8086822f..f8e3bbbf019c 100644 --- a/drivers/hwmon/jc42.c +++ b/drivers/hwmon/jc42.c @@ -201,7 +201,7 @@ struct jc42_data { #define JC42_TEMP_MIN 0 #define JC42_TEMP_MAX 125000 -static u16 jc42_temp_to_reg(int temp, bool extended) +static u16 jc42_temp_to_reg(long temp, bool extended) { int ntemp = clamp_val(temp, extended ? JC42_TEMP_MIN_EXTENDED : From e2c26f058e31619b548046b84fe2c2116cb5016f Mon Sep 17 00:00:00 2001 From: Guenter Roeck Date: Mon, 19 Jan 2015 09:16:53 -0800 Subject: [PATCH 12/19] hwmon: (jc42) Fix integer overflow when writing hysteresis value Subtracting an unsigned long from a signed value causes an overflow with large values. Use clamp_val() to reduce the number range prior to subtracting it from the temperature limit. Signed-off-by: Guenter Roeck Reviewed-by: Jean Delvare --- drivers/hwmon/jc42.c | 2 ++ 1 file changed, 2 insertions(+) diff --git a/drivers/hwmon/jc42.c b/drivers/hwmon/jc42.c index f8e3bbbf019c..a46cb65cacb5 100644 --- a/drivers/hwmon/jc42.c +++ b/drivers/hwmon/jc42.c @@ -312,7 +312,9 @@ static ssize_t set_temp_crit_hyst(struct device *dev, if (kstrtoul(buf, 10, &val) < 0) return -EINVAL; + val = clamp_val(val, 0, JC42_TEMP_MAX); diff = jc42_temp_from_reg(data->temp[t_crit]) - val; + hyst = 0; if (diff > 0) { if (diff < 2250) From a14c70729c46c740f37572879e8e7f3de43f6aa7 Mon Sep 17 00:00:00 2001 From: Asaf Vertz Date: Wed, 21 Jan 2015 10:03:22 +0200 Subject: [PATCH 13/19] hwmon: (abx500) Fix format string warnings Fixed the following warnings (reported by cppcheck): [drivers/hwmon/abx500.c:224]: (warning) %ld in format string (no. 1) requires 'long' but the argument type is 'unsigned long'. [drivers/hwmon/abx500.c:233]: (warning) %ld in format string (no. 1) requires 'long' but the argument type is 'unsigned long'. [drivers/hwmon/abx500.c:242]: (warning) %ld in format string (no. 1) requires 'long' but the argument type is 'unsigned long'. Signed-off-by: Asaf Vertz Signed-off-by: Guenter Roeck --- drivers/hwmon/abx500.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/drivers/hwmon/abx500.c b/drivers/hwmon/abx500.c index 13875968c844..6cb89c0ebab6 100644 --- a/drivers/hwmon/abx500.c +++ b/drivers/hwmon/abx500.c @@ -221,7 +221,7 @@ static ssize_t show_min(struct device *dev, struct abx500_temp *data = dev_get_drvdata(dev); struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr); - return sprintf(buf, "%ld\n", data->min[attr->index]); + return sprintf(buf, "%lu\n", data->min[attr->index]); } static ssize_t show_max(struct device *dev, @@ -230,7 +230,7 @@ static ssize_t show_max(struct device *dev, struct abx500_temp *data = dev_get_drvdata(dev); struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr); - return sprintf(buf, "%ld\n", data->max[attr->index]); + return sprintf(buf, "%lu\n", data->max[attr->index]); } static ssize_t show_max_hyst(struct device *dev, @@ -239,7 +239,7 @@ static ssize_t show_max_hyst(struct device *dev, struct abx500_temp *data = dev_get_drvdata(dev); struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr); - return sprintf(buf, "%ld\n", data->max_hyst[attr->index]); + return sprintf(buf, "%lu\n", data->max_hyst[attr->index]); } static ssize_t show_min_alarm(struct device *dev, From 984faa1fb9175720828bf6604f936dff3f17b995 Mon Sep 17 00:00:00 2001 From: Rasmus Villemoes Date: Thu, 22 Jan 2015 23:44:13 +0100 Subject: [PATCH 14/19] hwmon: (ad7314) Do proper sign extension The comment above (data << 2) >> 2 explains what the intention is: To use bit 13 of the 14-bit value data as the sign bit. However, this doesn't work due to C's promotion rules. data has type s16, but data << 2 has type int. To get sign extension, that expression would have to be cast back to an s16 before being shifted (at which point C's promotion rules would then kick in again and promote the left operand to int). As it stands, both expressions are no-ops for any value of data. Avoid these subtleties by using the existing API for this. sign_extend32 works equally well for 8 and 16 bit types. Signed-off-by: Rasmus Villemoes Signed-off-by: Guenter Roeck --- drivers/hwmon/ad7314.c | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/drivers/hwmon/ad7314.c b/drivers/hwmon/ad7314.c index f4f9b219bf16..11955467fc0f 100644 --- a/drivers/hwmon/ad7314.c +++ b/drivers/hwmon/ad7314.c @@ -16,6 +16,7 @@ #include #include #include +#include /* * AD7314 temperature masks @@ -67,7 +68,7 @@ static ssize_t ad7314_show_temperature(struct device *dev, switch (spi_get_device_id(chip->spi_dev)->driver_data) { case ad7314: data = (ret & AD7314_TEMP_MASK) >> AD7314_TEMP_SHIFT; - data = (data << 6) >> 6; + data = sign_extend32(data, 9); return sprintf(buf, "%d\n", 250 * data); case adt7301: @@ -78,7 +79,7 @@ static ssize_t ad7314_show_temperature(struct device *dev, * register. 1lsb - 31.25 milli degrees centigrade */ data = ret & ADT7301_TEMP_MASK; - data = (data << 2) >> 2; + data = sign_extend32(data, 13); return sprintf(buf, "%d\n", DIV_ROUND_CLOSEST(data * 3125, 100)); From 2c3b1189fdca2f512187a670aeb295f901c10d87 Mon Sep 17 00:00:00 2001 From: Rasmus Villemoes Date: Thu, 22 Jan 2015 23:44:14 +0100 Subject: [PATCH 15/19] hwmon: (adc128d818) Do proper sign extension data->temp[index] has type s16. Because of C's promotion rules, (data->temp[index] << 7) >> 7 is exactly the same as data->temp[index]. The intention was to use bit 8 as a sign bit, so do that using the existing API. Signed-off-by: Rasmus Villemoes Signed-off-by: Guenter Roeck --- drivers/hwmon/adc128d818.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/drivers/hwmon/adc128d818.c b/drivers/hwmon/adc128d818.c index 0625e50d7a6e..ad2b47e40345 100644 --- a/drivers/hwmon/adc128d818.c +++ b/drivers/hwmon/adc128d818.c @@ -27,6 +27,7 @@ #include #include #include +#include /* Addresses to scan * The chip also supports addresses 0x35..0x37. Don't scan those addresses @@ -189,7 +190,7 @@ static ssize_t adc128_show_temp(struct device *dev, if (IS_ERR(data)) return PTR_ERR(data); - temp = (data->temp[index] << 7) >> 7; /* sign extend */ + temp = sign_extend32(data->temp[index], 8); return sprintf(buf, "%d\n", temp * 500);/* 0.5 degrees C resolution */ } From 9130880a1c2de115d4a3af0f78b2a437a77a1b7e Mon Sep 17 00:00:00 2001 From: Jean Delvare Date: Fri, 23 Jan 2015 10:27:03 +0100 Subject: [PATCH 16/19] hwmon: (jc42) Allow negative hysteresis temperatures The driver supports negative high and critical limits, it can return negative hysteresis values, so there is no good reason to not let the user write negative hysteresis values. Signed-off-by: Jean Delvare Cc: Guenter Roeck Signed-off-by: Guenter Roeck --- drivers/hwmon/jc42.c | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/drivers/hwmon/jc42.c b/drivers/hwmon/jc42.c index a46cb65cacb5..996bdfd5cf25 100644 --- a/drivers/hwmon/jc42.c +++ b/drivers/hwmon/jc42.c @@ -304,15 +304,16 @@ static ssize_t set_temp_crit_hyst(struct device *dev, const char *buf, size_t count) { struct jc42_data *data = dev_get_drvdata(dev); - unsigned long val; + long val; int diff, hyst; int err; int ret = count; - if (kstrtoul(buf, 10, &val) < 0) + if (kstrtol(buf, 10, &val) < 0) return -EINVAL; - val = clamp_val(val, 0, JC42_TEMP_MAX); + val = clamp_val(val, (data->extended ? JC42_TEMP_MIN_EXTENDED : + JC42_TEMP_MIN) - 6000, JC42_TEMP_MAX); diff = jc42_temp_from_reg(data->temp[t_crit]) - val; hyst = 0; From bea0bab0fcf5a0b97f3545ba35647f0f37dc2475 Mon Sep 17 00:00:00 2001 From: Guenter Roeck Date: Fri, 16 Jan 2015 10:08:27 -0800 Subject: [PATCH 17/19] hwmon: (ads2828) Convert to use regmap Simplify code and reduce code size by using regmap to access i2c registers. Reviewed-and-Tested-by: Robert Rosengren Signed-off-by: Guenter Roeck --- drivers/hwmon/Kconfig | 1 + drivers/hwmon/ads7828.c | 70 +++++++++++++++-------------------------- 2 files changed, 27 insertions(+), 44 deletions(-) diff --git a/drivers/hwmon/Kconfig b/drivers/hwmon/Kconfig index c24394086b40..d931cbbed240 100644 --- a/drivers/hwmon/Kconfig +++ b/drivers/hwmon/Kconfig @@ -1389,6 +1389,7 @@ config SENSORS_ADS1015 config SENSORS_ADS7828 tristate "Texas Instruments ADS7828 and compatibles" depends on I2C + select REGMAP_I2C help If you say yes here you get support for Texas Instruments ADS7828 and ADS7830 8-channel A/D converters. ADS7828 resolution is 12-bit, while diff --git a/drivers/hwmon/ads7828.c b/drivers/hwmon/ads7828.c index a622d40eec17..071f779d5e48 100644 --- a/drivers/hwmon/ads7828.c +++ b/drivers/hwmon/ads7828.c @@ -30,14 +30,12 @@ #include #include #include -#include #include -#include #include +#include #include /* The ADS7828 registers */ -#define ADS7828_NCH 8 /* 8 channels supported */ #define ADS7828_CMD_SD_SE 0x80 /* Single ended inputs */ #define ADS7828_CMD_PD1 0x04 /* Internal vref OFF && A/D ON */ #define ADS7828_CMD_PD3 0x0C /* Internal vref ON && A/D ON */ @@ -50,17 +48,12 @@ enum ads7828_chips { ads7828, ads7830 }; /* Client specific data */ struct ads7828_data { - struct i2c_client *client; - struct mutex update_lock; /* Mutex protecting updates */ - unsigned long last_updated; /* Last updated time (in jiffies) */ - u16 adc_input[ADS7828_NCH]; /* ADS7828_NCH samples */ - bool valid; /* Validity flag */ + struct regmap *regmap; bool diff_input; /* Differential input */ bool ext_vref; /* External voltage reference */ unsigned int vref_mv; /* voltage reference value */ u8 cmd_byte; /* Command byte without channel bits */ unsigned int lsb_resol; /* Resolution of the ADC sample LSB */ - s32 (*read_channel)(const struct i2c_client *client, u8 command); }; /* Command byte C2,C1,C0 - see datasheet */ @@ -69,42 +62,22 @@ static inline u8 ads7828_cmd_byte(u8 cmd, int ch) return cmd | (((ch >> 1) | (ch & 0x01) << 2) << 4); } -/* Update data for the device (all 8 channels) */ -static struct ads7828_data *ads7828_update_device(struct device *dev) -{ - struct ads7828_data *data = dev_get_drvdata(dev); - struct i2c_client *client = data->client; - - mutex_lock(&data->update_lock); - - if (time_after(jiffies, data->last_updated + HZ + HZ / 2) - || !data->valid) { - unsigned int ch; - dev_dbg(&client->dev, "Starting ads7828 update\n"); - - for (ch = 0; ch < ADS7828_NCH; ch++) { - u8 cmd = ads7828_cmd_byte(data->cmd_byte, ch); - data->adc_input[ch] = data->read_channel(client, cmd); - } - data->last_updated = jiffies; - data->valid = true; - } - - mutex_unlock(&data->update_lock); - - return data; -} - /* sysfs callback function */ static ssize_t ads7828_show_in(struct device *dev, struct device_attribute *da, char *buf) { struct sensor_device_attribute *attr = to_sensor_dev_attr(da); - struct ads7828_data *data = ads7828_update_device(dev); - unsigned int value = DIV_ROUND_CLOSEST(data->adc_input[attr->index] * - data->lsb_resol, 1000); + struct ads7828_data *data = dev_get_drvdata(dev); + u8 cmd = ads7828_cmd_byte(data->cmd_byte, attr->index); + unsigned int regval; + int err; - return sprintf(buf, "%d\n", value); + err = regmap_read(data->regmap, cmd, ®val); + if (err < 0) + return err; + + return sprintf(buf, "%d\n", + DIV_ROUND_CLOSEST(regval * data->lsb_resol, 1000)); } static SENSOR_DEVICE_ATTR(in0_input, S_IRUGO, ads7828_show_in, NULL, 0); @@ -130,6 +103,16 @@ static struct attribute *ads7828_attrs[] = { ATTRIBUTE_GROUPS(ads7828); +static const struct regmap_config ads2828_regmap_config = { + .reg_bits = 8, + .val_bits = 16, +}; + +static const struct regmap_config ads2830_regmap_config = { + .reg_bits = 8, + .val_bits = 8, +}; + static int ads7828_probe(struct i2c_client *client, const struct i2c_device_id *id) { @@ -160,19 +143,18 @@ static int ads7828_probe(struct i2c_client *client, /* ADS7828 uses 12-bit samples, while ADS7830 is 8-bit */ if (id->driver_data == ads7828) { data->lsb_resol = DIV_ROUND_CLOSEST(data->vref_mv * 1000, 4096); - data->read_channel = i2c_smbus_read_word_swapped; + data->regmap = devm_regmap_init_i2c(client, + &ads2828_regmap_config); } else { data->lsb_resol = DIV_ROUND_CLOSEST(data->vref_mv * 1000, 256); - data->read_channel = i2c_smbus_read_byte_data; + data->regmap = devm_regmap_init_i2c(client, + &ads2830_regmap_config); } data->cmd_byte = data->ext_vref ? ADS7828_CMD_PD1 : ADS7828_CMD_PD3; if (!data->diff_input) data->cmd_byte |= ADS7828_CMD_SD_SE; - data->client = client; - mutex_init(&data->update_lock); - hwmon_dev = devm_hwmon_device_register_with_groups(dev, client->name, data, ads7828_groups); From 7f444bf0a28c030d40925c5d0073d3e5ed8ca1e3 Mon Sep 17 00:00:00 2001 From: Guenter Roeck Date: Fri, 16 Jan 2015 10:12:50 -0800 Subject: [PATCH 18/19] hwmon: (ads2828) Only keep data in device data structure if needed The variables diff_input, ext_vref, and vref_mv are only used in the probe function and therefore don't need to be kept in the device data structure. Reviewed-and-Tested-by: Robert Rosengren Signed-off-by: Guenter Roeck --- drivers/hwmon/ads7828.c | 32 ++++++++++++++------------------ 1 file changed, 14 insertions(+), 18 deletions(-) diff --git a/drivers/hwmon/ads7828.c b/drivers/hwmon/ads7828.c index 071f779d5e48..bce4e9ff21bf 100644 --- a/drivers/hwmon/ads7828.c +++ b/drivers/hwmon/ads7828.c @@ -49,9 +49,6 @@ enum ads7828_chips { ads7828, ads7830 }; /* Client specific data */ struct ads7828_data { struct regmap *regmap; - bool diff_input; /* Differential input */ - bool ext_vref; /* External voltage reference */ - unsigned int vref_mv; /* voltage reference value */ u8 cmd_byte; /* Command byte without channel bits */ unsigned int lsb_resol; /* Resolution of the ADC sample LSB */ }; @@ -120,39 +117,38 @@ static int ads7828_probe(struct i2c_client *client, struct ads7828_platform_data *pdata = dev_get_platdata(dev); struct ads7828_data *data; struct device *hwmon_dev; + unsigned int vref_mv = ADS7828_INT_VREF_MV; + bool diff_input = false; + bool ext_vref = false; data = devm_kzalloc(dev, sizeof(struct ads7828_data), GFP_KERNEL); if (!data) return -ENOMEM; if (pdata) { - data->diff_input = pdata->diff_input; - data->ext_vref = pdata->ext_vref; - if (data->ext_vref) - data->vref_mv = pdata->vref_mv; + diff_input = pdata->diff_input; + ext_vref = pdata->ext_vref; + if (ext_vref && pdata->vref_mv) + vref_mv = pdata->vref_mv; } - /* Bound Vref with min/max values if it was provided */ - if (data->vref_mv) - data->vref_mv = clamp_val(data->vref_mv, - ADS7828_EXT_VREF_MV_MIN, - ADS7828_EXT_VREF_MV_MAX); - else - data->vref_mv = ADS7828_INT_VREF_MV; + /* Bound Vref with min/max values */ + vref_mv = clamp_val(vref_mv, ADS7828_EXT_VREF_MV_MIN, + ADS7828_EXT_VREF_MV_MAX); /* ADS7828 uses 12-bit samples, while ADS7830 is 8-bit */ if (id->driver_data == ads7828) { - data->lsb_resol = DIV_ROUND_CLOSEST(data->vref_mv * 1000, 4096); + data->lsb_resol = DIV_ROUND_CLOSEST(vref_mv * 1000, 4096); data->regmap = devm_regmap_init_i2c(client, &ads2828_regmap_config); } else { - data->lsb_resol = DIV_ROUND_CLOSEST(data->vref_mv * 1000, 256); + data->lsb_resol = DIV_ROUND_CLOSEST(vref_mv * 1000, 256); data->regmap = devm_regmap_init_i2c(client, &ads2830_regmap_config); } - data->cmd_byte = data->ext_vref ? ADS7828_CMD_PD1 : ADS7828_CMD_PD3; - if (!data->diff_input) + data->cmd_byte = ext_vref ? ADS7828_CMD_PD1 : ADS7828_CMD_PD3; + if (!diff_input) data->cmd_byte |= ADS7828_CMD_SD_SE; hwmon_dev = devm_hwmon_device_register_with_groups(dev, client->name, From dd378b1bcaa0ef5b14cca1e52b58ef9a3279fd8b Mon Sep 17 00:00:00 2001 From: Grygorii Strashko Date: Tue, 3 Feb 2015 17:01:58 +0200 Subject: [PATCH 19/19] hwmon: (tmp102) add hibernation callbacks Setting a dev_pm_ops suspend/resume pair but not a set of hibernation functions means those pm functions will not be called upon hibernation. Fix this by using SIMPLE_DEV_PM_OPS, which appropriately assigns the suspend and hibernation handlers and move mp102_suspend/tmp102_resume under CONFIG_PM_SLEEP to avoid build warnings. Signed-off-by: Grygorii Strashko [groeck: Declare tmp102_dev_pm_ops as static variable] Signed-off-by: Guenter Roeck --- drivers/hwmon/tmp102.c | 15 ++++----------- 1 file changed, 4 insertions(+), 11 deletions(-) diff --git a/drivers/hwmon/tmp102.c b/drivers/hwmon/tmp102.c index ba9f478f64ee..9da2735f1424 100644 --- a/drivers/hwmon/tmp102.c +++ b/drivers/hwmon/tmp102.c @@ -253,7 +253,7 @@ static int tmp102_remove(struct i2c_client *client) return 0; } -#ifdef CONFIG_PM +#ifdef CONFIG_PM_SLEEP static int tmp102_suspend(struct device *dev) { struct i2c_client *client = to_i2c_client(dev); @@ -279,17 +279,10 @@ static int tmp102_resume(struct device *dev) config &= ~TMP102_CONF_SD; return i2c_smbus_write_word_swapped(client, TMP102_CONF_REG, config); } - -static const struct dev_pm_ops tmp102_dev_pm_ops = { - .suspend = tmp102_suspend, - .resume = tmp102_resume, -}; - -#define TMP102_DEV_PM_OPS (&tmp102_dev_pm_ops) -#else -#define TMP102_DEV_PM_OPS NULL #endif /* CONFIG_PM */ +static SIMPLE_DEV_PM_OPS(tmp102_dev_pm_ops, tmp102_suspend, tmp102_resume); + static const struct i2c_device_id tmp102_id[] = { { "tmp102", 0 }, { } @@ -298,7 +291,7 @@ MODULE_DEVICE_TABLE(i2c, tmp102_id); static struct i2c_driver tmp102_driver = { .driver.name = DRIVER_NAME, - .driver.pm = TMP102_DEV_PM_OPS, + .driver.pm = &tmp102_dev_pm_ops, .probe = tmp102_probe, .remove = tmp102_remove, .id_table = tmp102_id,