2 * Driver for Texas Instruments INA219, INA226 power monitor chips
5 * Zero Drift Bi-Directional Current/Power Monitor with I2C Interface
6 * Datasheet: http://www.ti.com/product/ina219
9 * Bi-Directional Current/Power Monitor with I2C Interface
10 * Datasheet: http://www.ti.com/product/ina220
13 * Bi-Directional Current/Power Monitor with I2C Interface
14 * Datasheet: http://www.ti.com/product/ina226
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>
38 #include <linux/delay.h>
39 #include <linux/util_macros.h>
40 #include <linux/regmap.h>
42 #include <linux/platform_data/ina2xx.h>
44 /* common register definitions */
45 #define INA2XX_CONFIG 0x00
46 #define INA2XX_SHUNT_VOLTAGE 0x01 /* readonly */
47 #define INA2XX_BUS_VOLTAGE 0x02 /* readonly */
48 #define INA2XX_POWER 0x03 /* readonly */
49 #define INA2XX_CURRENT 0x04 /* readonly */
50 #define INA2XX_CALIBRATION 0x05
52 /* INA226 register definitions */
53 #define INA226_MASK_ENABLE 0x06
54 #define INA226_ALERT_LIMIT 0x07
55 #define INA226_DIE_ID 0xFF
58 #define INA219_REGISTERS 6
59 #define INA226_REGISTERS 8
61 #define INA2XX_MAX_REGISTERS 8
63 /* settings - depend on use case */
64 #define INA219_CONFIG_DEFAULT 0x399F /* PGA=8 */
65 #define INA226_CONFIG_DEFAULT 0x4527 /* averages=16 */
67 /* worst case is 68.10 ms (~14.6Hz, ina219) */
68 #define INA2XX_CONVERSION_RATE 15
69 #define INA2XX_MAX_DELAY 69 /* worst case delay in ms */
71 #define INA2XX_RSHUNT_DEFAULT 10000
73 /* bit mask for reading the averaging setting in the configuration register */
74 #define INA226_AVG_RD_MASK 0x0E00
76 #define INA226_READ_AVG(reg) (((reg) & INA226_AVG_RD_MASK) >> 9)
77 #define INA226_SHIFT_AVG(val) ((val) << 9)
79 /* common attrs, ina226 attrs and NULL */
80 #define INA2XX_MAX_ATTRIBUTE_GROUPS 3
83 * Both bus voltage and shunt voltage conversion times for ina226 are set
84 * to 0b0100 on POR, which translates to 2200 microseconds in total.
86 #define INA226_TOTAL_CONV_TIME_DEFAULT 2200
88 static struct regmap_config ina2xx_regmap_config = {
93 enum ina2xx_ids { ina219, ina226 };
95 struct ina2xx_config {
97 int calibration_factor;
100 int bus_voltage_shift;
101 int bus_voltage_lsb; /* uV */
102 int power_lsb; /* uW */
106 const struct ina2xx_config *config;
109 struct mutex config_lock;
110 struct regmap *regmap;
113 const struct attribute_group *groups[INA2XX_MAX_ATTRIBUTE_GROUPS];
116 static const struct ina2xx_config ina2xx_config[] = {
118 .config_default = INA219_CONFIG_DEFAULT,
119 .calibration_factor = 40960000,
120 .registers = INA219_REGISTERS,
122 .bus_voltage_shift = 3,
123 .bus_voltage_lsb = 4000,
127 .config_default = INA226_CONFIG_DEFAULT,
128 .calibration_factor = 5120000,
129 .registers = INA226_REGISTERS,
131 .bus_voltage_shift = 0,
132 .bus_voltage_lsb = 1250,
138 * Available averaging rates for ina226. The indices correspond with
139 * the bit values expected by the chip (according to the ina226 datasheet,
140 * table 3 AVG bit settings, found at
141 * http://www.ti.com/lit/ds/symlink/ina226.pdf.
143 static const int ina226_avg_tab[] = { 1, 4, 16, 64, 128, 256, 512, 1024 };
145 static int ina226_reg_to_interval(u16 config)
147 int avg = ina226_avg_tab[INA226_READ_AVG(config)];
150 * Multiply the total conversion time by the number of averages.
151 * Return the result in milliseconds.
153 return DIV_ROUND_CLOSEST(avg * INA226_TOTAL_CONV_TIME_DEFAULT, 1000);
157 * Return the new, shifted AVG field value of CONFIG register,
158 * to use with regmap_update_bits
160 static u16 ina226_interval_to_reg(int interval)
164 avg = DIV_ROUND_CLOSEST(interval * 1000,
165 INA226_TOTAL_CONV_TIME_DEFAULT);
166 avg_bits = find_closest(avg, ina226_avg_tab,
167 ARRAY_SIZE(ina226_avg_tab));
169 return INA226_SHIFT_AVG(avg_bits);
172 static int ina2xx_calibrate(struct ina2xx_data *data)
174 u16 val = DIV_ROUND_CLOSEST(data->config->calibration_factor,
177 return regmap_write(data->regmap, INA2XX_CALIBRATION, val);
181 * Initialize the configuration and calibration registers.
183 static int ina2xx_init(struct ina2xx_data *data)
185 int ret = regmap_write(data->regmap, INA2XX_CONFIG,
186 data->config->config_default);
191 * Set current LSB to 1mA, shunt is in uOhms
192 * (equation 13 in datasheet).
194 return ina2xx_calibrate(data);
197 static int ina2xx_read_reg(struct device *dev, int reg, unsigned int *regval)
199 struct ina2xx_data *data = dev_get_drvdata(dev);
202 dev_dbg(dev, "Starting register %d read\n", reg);
204 for (retry = 5; retry; retry--) {
206 ret = regmap_read(data->regmap, reg, regval);
210 dev_dbg(dev, "read %d, val = 0x%04x\n", reg, *regval);
213 * If the current value in the calibration register is 0, the
214 * power and current registers will also remain at 0. In case
215 * the chip has been reset let's check the calibration
216 * register and reinitialize if needed.
217 * We do that extra read of the calibration register if there
218 * is some hint of a chip reset.
223 ret = regmap_read(data->regmap, INA2XX_CALIBRATION,
229 dev_warn(dev, "chip not calibrated, reinitializing\n");
231 ret = ina2xx_init(data);
235 * Let's make sure the power and current
236 * registers have been updated before trying
239 msleep(INA2XX_MAX_DELAY);
247 * If we're here then although all write operations succeeded, the
248 * chip still returns 0 in the calibration register. Nothing more we
251 dev_err(dev, "unable to reinitialize the chip\n");
255 static int ina2xx_get_value(struct ina2xx_data *data, u8 reg,
261 case INA2XX_SHUNT_VOLTAGE:
262 /* signed register */
263 val = DIV_ROUND_CLOSEST((s16)regval, data->config->shunt_div);
265 case INA2XX_BUS_VOLTAGE:
266 val = (regval >> data->config->bus_voltage_shift)
267 * data->config->bus_voltage_lsb;
268 val = DIV_ROUND_CLOSEST(val, 1000);
271 val = regval * data->config->power_lsb;
274 /* signed register, LSB=1mA (selected), in mA */
277 case INA2XX_CALIBRATION:
278 val = DIV_ROUND_CLOSEST(data->config->calibration_factor,
282 /* programmer goofed */
291 static ssize_t ina2xx_show_value(struct device *dev,
292 struct device_attribute *da, char *buf)
294 struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
295 struct ina2xx_data *data = dev_get_drvdata(dev);
298 int err = ina2xx_read_reg(dev, attr->index, ®val);
303 return snprintf(buf, PAGE_SIZE, "%d\n",
304 ina2xx_get_value(data, attr->index, regval));
307 static ssize_t ina2xx_set_shunt(struct device *dev,
308 struct device_attribute *da,
309 const char *buf, size_t count)
313 struct ina2xx_data *data = dev_get_drvdata(dev);
315 status = kstrtoul(buf, 10, &val);
320 /* Values greater than the calibration factor make no sense. */
321 val > data->config->calibration_factor)
324 mutex_lock(&data->config_lock);
326 status = ina2xx_calibrate(data);
327 mutex_unlock(&data->config_lock);
334 static ssize_t ina226_set_interval(struct device *dev,
335 struct device_attribute *da,
336 const char *buf, size_t count)
338 struct ina2xx_data *data = dev_get_drvdata(dev);
342 status = kstrtoul(buf, 10, &val);
346 if (val > INT_MAX || val == 0)
349 status = regmap_update_bits(data->regmap, INA2XX_CONFIG,
351 ina226_interval_to_reg(val));
358 static ssize_t ina226_show_interval(struct device *dev,
359 struct device_attribute *da, char *buf)
361 struct ina2xx_data *data = dev_get_drvdata(dev);
365 status = regmap_read(data->regmap, INA2XX_CONFIG, ®val);
369 return snprintf(buf, PAGE_SIZE, "%d\n", ina226_reg_to_interval(regval));
373 static SENSOR_DEVICE_ATTR(in0_input, S_IRUGO, ina2xx_show_value, NULL,
374 INA2XX_SHUNT_VOLTAGE);
377 static SENSOR_DEVICE_ATTR(in1_input, S_IRUGO, ina2xx_show_value, NULL,
380 /* calculated current */
381 static SENSOR_DEVICE_ATTR(curr1_input, S_IRUGO, ina2xx_show_value, NULL,
384 /* calculated power */
385 static SENSOR_DEVICE_ATTR(power1_input, S_IRUGO, ina2xx_show_value, NULL,
388 /* shunt resistance */
389 static SENSOR_DEVICE_ATTR(shunt_resistor, S_IRUGO | S_IWUSR,
390 ina2xx_show_value, ina2xx_set_shunt,
393 /* update interval (ina226 only) */
394 static SENSOR_DEVICE_ATTR(update_interval, S_IRUGO | S_IWUSR,
395 ina226_show_interval, ina226_set_interval, 0);
397 /* pointers to created device attributes */
398 static struct attribute *ina2xx_attrs[] = {
399 &sensor_dev_attr_in0_input.dev_attr.attr,
400 &sensor_dev_attr_in1_input.dev_attr.attr,
401 &sensor_dev_attr_curr1_input.dev_attr.attr,
402 &sensor_dev_attr_power1_input.dev_attr.attr,
403 &sensor_dev_attr_shunt_resistor.dev_attr.attr,
407 static const struct attribute_group ina2xx_group = {
408 .attrs = ina2xx_attrs,
411 static struct attribute *ina226_attrs[] = {
412 &sensor_dev_attr_update_interval.dev_attr.attr,
416 static const struct attribute_group ina226_group = {
417 .attrs = ina226_attrs,
420 static int ina2xx_probe(struct i2c_client *client,
421 const struct i2c_device_id *id)
423 struct ina2xx_platform_data *pdata;
424 struct device *dev = &client->dev;
425 struct ina2xx_data *data;
426 struct device *hwmon_dev;
430 data = devm_kzalloc(dev, sizeof(*data), GFP_KERNEL);
434 if (dev_get_platdata(dev)) {
435 pdata = dev_get_platdata(dev);
436 data->rshunt = pdata->shunt_uohms;
437 } else if (!of_property_read_u32(dev->of_node,
438 "shunt-resistor", &val)) {
441 data->rshunt = INA2XX_RSHUNT_DEFAULT;
444 /* set the device type */
445 data->kind = id->driver_data;
446 data->config = &ina2xx_config[data->kind];
448 if (data->rshunt <= 0 ||
449 data->rshunt > data->config->calibration_factor)
452 ina2xx_regmap_config.max_register = data->config->registers;
454 data->regmap = devm_regmap_init_i2c(client, &ina2xx_regmap_config);
455 if (IS_ERR(data->regmap)) {
456 dev_err(dev, "failed to allocate register map\n");
457 return PTR_ERR(data->regmap);
460 ret = ina2xx_init(data);
462 dev_err(dev, "error configuring the device: %d\n", ret);
466 mutex_init(&data->config_lock);
468 data->groups[group++] = &ina2xx_group;
469 if (data->kind == ina226)
470 data->groups[group++] = &ina226_group;
472 hwmon_dev = devm_hwmon_device_register_with_groups(dev, client->name,
474 if (IS_ERR(hwmon_dev))
475 return PTR_ERR(hwmon_dev);
477 dev_info(dev, "power monitor %s (Rshunt = %li uOhm)\n",
478 id->name, data->rshunt);
483 static const struct i2c_device_id ina2xx_id[] = {
484 { "ina219", ina219 },
485 { "ina220", ina219 },
486 { "ina226", ina226 },
487 { "ina230", ina226 },
488 { "ina231", ina226 },
491 MODULE_DEVICE_TABLE(i2c, ina2xx_id);
493 static struct i2c_driver ina2xx_driver = {
497 .probe = ina2xx_probe,
498 .id_table = ina2xx_id,
501 module_i2c_driver(ina2xx_driver);
503 MODULE_AUTHOR("Lothar Felten <l-felten@ti.com>");
504 MODULE_DESCRIPTION("ina2xx driver");
505 MODULE_LICENSE("GPL");