hwmon: (ina2xx) make shunt resistance configurable at run-time
[linux/fpc-iii.git] / drivers / hwmon / ina2xx.c
blob49537ea8074876793fb22b2c6d6126c01216cee6
1 /*
2 * Driver for Texas Instruments INA219, INA226 power monitor chips
4 * INA219:
5 * Zero Drift Bi-Directional Current/Power Monitor with I2C Interface
6 * Datasheet: http://www.ti.com/product/ina219
8 * INA220:
9 * Bi-Directional Current/Power Monitor with I2C Interface
10 * Datasheet: http://www.ti.com/product/ina220
12 * INA226:
13 * Bi-Directional Current/Power Monitor with I2C Interface
14 * Datasheet: http://www.ti.com/product/ina226
16 * INA230:
17 * Bi-directional Current/Power Monitor with I2C Interface
18 * Datasheet: http://www.ti.com/product/ina230
20 * Copyright (C) 2012 Lothar Felten <l-felten@ti.com>
21 * Thanks to Jan Volkering
23 * This program is free software; you can redistribute it and/or modify
24 * it under the terms of the GNU General Public License as published by
25 * the Free Software Foundation; version 2 of the License.
28 #include <linux/kernel.h>
29 #include <linux/module.h>
30 #include <linux/init.h>
31 #include <linux/err.h>
32 #include <linux/slab.h>
33 #include <linux/i2c.h>
34 #include <linux/hwmon.h>
35 #include <linux/hwmon-sysfs.h>
36 #include <linux/jiffies.h>
37 #include <linux/of.h>
38 #include <linux/delay.h>
40 #include <linux/platform_data/ina2xx.h>
42 /* common register definitions */
43 #define INA2XX_CONFIG 0x00
44 #define INA2XX_SHUNT_VOLTAGE 0x01 /* readonly */
45 #define INA2XX_BUS_VOLTAGE 0x02 /* readonly */
46 #define INA2XX_POWER 0x03 /* readonly */
47 #define INA2XX_CURRENT 0x04 /* readonly */
48 #define INA2XX_CALIBRATION 0x05
50 /* INA226 register definitions */
51 #define INA226_MASK_ENABLE 0x06
52 #define INA226_ALERT_LIMIT 0x07
53 #define INA226_DIE_ID 0xFF
55 /* register count */
56 #define INA219_REGISTERS 6
57 #define INA226_REGISTERS 8
59 #define INA2XX_MAX_REGISTERS 8
61 /* settings - depend on use case */
62 #define INA219_CONFIG_DEFAULT 0x399F /* PGA=8 */
63 #define INA226_CONFIG_DEFAULT 0x4527 /* averages=16 */
65 /* worst case is 68.10 ms (~14.6Hz, ina219) */
66 #define INA2XX_CONVERSION_RATE 15
67 #define INA2XX_MAX_DELAY 69 /* worst case delay in ms */
69 #define INA2XX_RSHUNT_DEFAULT 10000
71 enum ina2xx_ids { ina219, ina226 };
73 struct ina2xx_config {
74 u16 config_default;
75 int calibration_factor;
76 int registers;
77 int shunt_div;
78 int bus_voltage_shift;
79 int bus_voltage_lsb; /* uV */
80 int power_lsb; /* uW */
83 struct ina2xx_data {
84 struct i2c_client *client;
85 const struct ina2xx_config *config;
87 long rshunt;
89 struct mutex update_lock;
90 bool valid;
91 unsigned long last_updated;
93 int kind;
94 u16 regs[INA2XX_MAX_REGISTERS];
97 static const struct ina2xx_config ina2xx_config[] = {
98 [ina219] = {
99 .config_default = INA219_CONFIG_DEFAULT,
100 .calibration_factor = 40960000,
101 .registers = INA219_REGISTERS,
102 .shunt_div = 100,
103 .bus_voltage_shift = 3,
104 .bus_voltage_lsb = 4000,
105 .power_lsb = 20000,
107 [ina226] = {
108 .config_default = INA226_CONFIG_DEFAULT,
109 .calibration_factor = 5120000,
110 .registers = INA226_REGISTERS,
111 .shunt_div = 400,
112 .bus_voltage_shift = 0,
113 .bus_voltage_lsb = 1250,
114 .power_lsb = 25000,
118 static int ina2xx_calibrate(struct ina2xx_data *data)
120 return i2c_smbus_write_word_swapped(data->client, INA2XX_CALIBRATION,
121 data->config->calibration_factor / data->rshunt);
125 * Initialize the configuration and calibration registers.
127 static int ina2xx_init(struct ina2xx_data *data)
129 struct i2c_client *client = data->client;
130 int ret;
132 /* device configuration */
133 ret = i2c_smbus_write_word_swapped(client, INA2XX_CONFIG,
134 data->config->config_default);
135 if (ret < 0)
136 return ret;
139 * Set current LSB to 1mA, shunt is in uOhms
140 * (equation 13 in datasheet).
142 return ina2xx_calibrate(data);
145 static int ina2xx_do_update(struct device *dev)
147 struct ina2xx_data *data = dev_get_drvdata(dev);
148 struct i2c_client *client = data->client;
149 int i, rv, retry;
151 dev_dbg(&client->dev, "Starting ina2xx update\n");
153 for (retry = 5; retry; retry--) {
154 /* Read all registers */
155 for (i = 0; i < data->config->registers; i++) {
156 rv = i2c_smbus_read_word_swapped(client, i);
157 if (rv < 0)
158 return rv;
159 data->regs[i] = rv;
163 * If the current value in the calibration register is 0, the
164 * power and current registers will also remain at 0. In case
165 * the chip has been reset let's check the calibration
166 * register and reinitialize if needed.
168 if (data->regs[INA2XX_CALIBRATION] == 0) {
169 dev_warn(dev, "chip not calibrated, reinitializing\n");
171 rv = ina2xx_init(data);
172 if (rv < 0)
173 return rv;
176 * Let's make sure the power and current registers
177 * have been updated before trying again.
179 msleep(INA2XX_MAX_DELAY);
180 continue;
183 data->last_updated = jiffies;
184 data->valid = 1;
186 return 0;
190 * If we're here then although all write operations succeeded, the
191 * chip still returns 0 in the calibration register. Nothing more we
192 * can do here.
194 dev_err(dev, "unable to reinitialize the chip\n");
195 return -ENODEV;
198 static struct ina2xx_data *ina2xx_update_device(struct device *dev)
200 struct ina2xx_data *data = dev_get_drvdata(dev);
201 struct ina2xx_data *ret = data;
202 int rv;
204 mutex_lock(&data->update_lock);
206 if (time_after(jiffies, data->last_updated +
207 HZ / INA2XX_CONVERSION_RATE) || !data->valid) {
208 rv = ina2xx_do_update(dev);
209 if (rv < 0)
210 ret = ERR_PTR(rv);
213 mutex_unlock(&data->update_lock);
214 return ret;
217 static int ina2xx_get_value(struct ina2xx_data *data, u8 reg)
219 int val;
221 switch (reg) {
222 case INA2XX_SHUNT_VOLTAGE:
223 /* signed register */
224 val = DIV_ROUND_CLOSEST((s16)data->regs[reg],
225 data->config->shunt_div);
226 break;
227 case INA2XX_BUS_VOLTAGE:
228 val = (data->regs[reg] >> data->config->bus_voltage_shift)
229 * data->config->bus_voltage_lsb;
230 val = DIV_ROUND_CLOSEST(val, 1000);
231 break;
232 case INA2XX_POWER:
233 val = data->regs[reg] * data->config->power_lsb;
234 break;
235 case INA2XX_CURRENT:
236 /* signed register, LSB=1mA (selected), in mA */
237 val = (s16)data->regs[reg];
238 break;
239 case INA2XX_CALIBRATION:
240 val = data->config->calibration_factor / data->regs[reg];
241 break;
242 default:
243 /* programmer goofed */
244 WARN_ON_ONCE(1);
245 val = 0;
246 break;
249 return val;
252 static ssize_t ina2xx_show_value(struct device *dev,
253 struct device_attribute *da, char *buf)
255 struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
256 struct ina2xx_data *data = ina2xx_update_device(dev);
258 if (IS_ERR(data))
259 return PTR_ERR(data);
261 return snprintf(buf, PAGE_SIZE, "%d\n",
262 ina2xx_get_value(data, attr->index));
265 static ssize_t ina2xx_set_shunt(struct device *dev,
266 struct device_attribute *da,
267 const char *buf, size_t count)
269 struct ina2xx_data *data = ina2xx_update_device(dev);
270 unsigned long val;
271 int status;
273 if (IS_ERR(data))
274 return PTR_ERR(data);
276 status = kstrtoul(buf, 10, &val);
277 if (status < 0)
278 return status;
280 if (val == 0 ||
281 /* Values greater than the calibration factor make no sense. */
282 val > data->config->calibration_factor)
283 return -EINVAL;
285 mutex_lock(&data->update_lock);
286 data->rshunt = val;
287 status = ina2xx_calibrate(data);
288 mutex_unlock(&data->update_lock);
289 if (status < 0)
290 return status;
292 return count;
295 /* shunt voltage */
296 static SENSOR_DEVICE_ATTR(in0_input, S_IRUGO, ina2xx_show_value, NULL,
297 INA2XX_SHUNT_VOLTAGE);
299 /* bus voltage */
300 static SENSOR_DEVICE_ATTR(in1_input, S_IRUGO, ina2xx_show_value, NULL,
301 INA2XX_BUS_VOLTAGE);
303 /* calculated current */
304 static SENSOR_DEVICE_ATTR(curr1_input, S_IRUGO, ina2xx_show_value, NULL,
305 INA2XX_CURRENT);
307 /* calculated power */
308 static SENSOR_DEVICE_ATTR(power1_input, S_IRUGO, ina2xx_show_value, NULL,
309 INA2XX_POWER);
311 /* shunt resistance */
312 static SENSOR_DEVICE_ATTR(shunt_resistor, S_IRUGO | S_IWUSR,
313 ina2xx_show_value, ina2xx_set_shunt,
314 INA2XX_CALIBRATION);
316 /* pointers to created device attributes */
317 static struct attribute *ina2xx_attrs[] = {
318 &sensor_dev_attr_in0_input.dev_attr.attr,
319 &sensor_dev_attr_in1_input.dev_attr.attr,
320 &sensor_dev_attr_curr1_input.dev_attr.attr,
321 &sensor_dev_attr_power1_input.dev_attr.attr,
322 &sensor_dev_attr_shunt_resistor.dev_attr.attr,
323 NULL,
325 ATTRIBUTE_GROUPS(ina2xx);
327 static int ina2xx_probe(struct i2c_client *client,
328 const struct i2c_device_id *id)
330 struct i2c_adapter *adapter = client->adapter;
331 struct ina2xx_platform_data *pdata;
332 struct device *dev = &client->dev;
333 struct ina2xx_data *data;
334 struct device *hwmon_dev;
335 u32 val;
336 int ret;
338 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_WORD_DATA))
339 return -ENODEV;
341 data = devm_kzalloc(dev, sizeof(*data), GFP_KERNEL);
342 if (!data)
343 return -ENOMEM;
345 if (dev_get_platdata(dev)) {
346 pdata = dev_get_platdata(dev);
347 data->rshunt = pdata->shunt_uohms;
348 } else if (!of_property_read_u32(dev->of_node,
349 "shunt-resistor", &val)) {
350 data->rshunt = val;
351 } else {
352 data->rshunt = INA2XX_RSHUNT_DEFAULT;
355 /* set the device type */
356 data->kind = id->driver_data;
357 data->config = &ina2xx_config[data->kind];
358 data->client = client;
360 if (data->rshunt <= 0 ||
361 data->rshunt > data->config->calibration_factor)
362 return -ENODEV;
364 ret = ina2xx_init(data);
365 if (ret < 0) {
366 dev_err(dev, "error configuring the device: %d\n", ret);
367 return -ENODEV;
370 mutex_init(&data->update_lock);
372 hwmon_dev = devm_hwmon_device_register_with_groups(dev, client->name,
373 data, ina2xx_groups);
374 if (IS_ERR(hwmon_dev))
375 return PTR_ERR(hwmon_dev);
377 dev_info(dev, "power monitor %s (Rshunt = %li uOhm)\n",
378 id->name, data->rshunt);
380 return 0;
383 static const struct i2c_device_id ina2xx_id[] = {
384 { "ina219", ina219 },
385 { "ina220", ina219 },
386 { "ina226", ina226 },
387 { "ina230", ina226 },
390 MODULE_DEVICE_TABLE(i2c, ina2xx_id);
392 static struct i2c_driver ina2xx_driver = {
393 .driver = {
394 .name = "ina2xx",
396 .probe = ina2xx_probe,
397 .id_table = ina2xx_id,
400 module_i2c_driver(ina2xx_driver);
402 MODULE_AUTHOR("Lothar Felten <l-felten@ti.com>");
403 MODULE_DESCRIPTION("ina2xx driver");
404 MODULE_LICENSE("GPL");