1 // SPDX-License-Identifier: GPL-2.0-only
3 * Driver for Texas Instruments INA219, INA226 power monitor chips
6 * Zero Drift Bi-Directional Current/Power Monitor with I2C Interface
7 * Datasheet: https://www.ti.com/product/ina219
10 * Bi-Directional Current/Power Monitor with I2C Interface
11 * Datasheet: https://www.ti.com/product/ina220
14 * Bi-Directional Current/Power Monitor with I2C Interface
15 * Datasheet: https://www.ti.com/product/ina226
18 * Bi-directional Current/Power Monitor with I2C Interface
19 * Datasheet: https://www.ti.com/product/ina230
21 * Copyright (C) 2012 Lothar Felten <lothar.felten@gmail.com>
22 * Thanks to Jan Volkering
25 #include <linux/kernel.h>
26 #include <linux/module.h>
27 #include <linux/init.h>
28 #include <linux/err.h>
29 #include <linux/slab.h>
30 #include <linux/i2c.h>
31 #include <linux/hwmon.h>
32 #include <linux/hwmon-sysfs.h>
33 #include <linux/jiffies.h>
34 #include <linux/of_device.h>
36 #include <linux/delay.h>
37 #include <linux/util_macros.h>
38 #include <linux/regmap.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
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 /* bit mask for reading the averaging setting in the configuration register */
72 #define INA226_AVG_RD_MASK 0x0E00
74 #define INA226_READ_AVG(reg) (((reg) & INA226_AVG_RD_MASK) >> 9)
75 #define INA226_SHIFT_AVG(val) ((val) << 9)
77 /* bit number of alert functions in Mask/Enable Register */
78 #define INA226_SHUNT_OVER_VOLTAGE_BIT 15
79 #define INA226_SHUNT_UNDER_VOLTAGE_BIT 14
80 #define INA226_BUS_OVER_VOLTAGE_BIT 13
81 #define INA226_BUS_UNDER_VOLTAGE_BIT 12
82 #define INA226_POWER_OVER_LIMIT_BIT 11
84 /* bit mask for alert config bits of Mask/Enable Register */
85 #define INA226_ALERT_CONFIG_MASK 0xFC00
86 #define INA226_ALERT_FUNCTION_FLAG BIT(4)
88 /* common attrs, ina226 attrs and NULL */
89 #define INA2XX_MAX_ATTRIBUTE_GROUPS 3
92 * Both bus voltage and shunt voltage conversion times for ina226 are set
93 * to 0b0100 on POR, which translates to 2200 microseconds in total.
95 #define INA226_TOTAL_CONV_TIME_DEFAULT 2200
97 static struct regmap_config ina2xx_regmap_config = {
102 enum ina2xx_ids { ina219, ina226 };
104 struct ina2xx_config {
106 int calibration_value;
109 int bus_voltage_shift;
110 int bus_voltage_lsb; /* uV */
111 int power_lsb_factor;
115 const struct ina2xx_config *config;
120 struct mutex config_lock;
121 struct regmap *regmap;
123 const struct attribute_group *groups[INA2XX_MAX_ATTRIBUTE_GROUPS];
126 static const struct ina2xx_config ina2xx_config[] = {
128 .config_default = INA219_CONFIG_DEFAULT,
129 .calibration_value = 4096,
130 .registers = INA219_REGISTERS,
132 .bus_voltage_shift = 3,
133 .bus_voltage_lsb = 4000,
134 .power_lsb_factor = 20,
137 .config_default = INA226_CONFIG_DEFAULT,
138 .calibration_value = 2048,
139 .registers = INA226_REGISTERS,
141 .bus_voltage_shift = 0,
142 .bus_voltage_lsb = 1250,
143 .power_lsb_factor = 25,
148 * Available averaging rates for ina226. The indices correspond with
149 * the bit values expected by the chip (according to the ina226 datasheet,
150 * table 3 AVG bit settings, found at
151 * https://www.ti.com/lit/ds/symlink/ina226.pdf.
153 static const int ina226_avg_tab[] = { 1, 4, 16, 64, 128, 256, 512, 1024 };
155 static int ina226_reg_to_interval(u16 config)
157 int avg = ina226_avg_tab[INA226_READ_AVG(config)];
160 * Multiply the total conversion time by the number of averages.
161 * Return the result in milliseconds.
163 return DIV_ROUND_CLOSEST(avg * INA226_TOTAL_CONV_TIME_DEFAULT, 1000);
167 * Return the new, shifted AVG field value of CONFIG register,
168 * to use with regmap_update_bits
170 static u16 ina226_interval_to_reg(int interval)
174 avg = DIV_ROUND_CLOSEST(interval * 1000,
175 INA226_TOTAL_CONV_TIME_DEFAULT);
176 avg_bits = find_closest(avg, ina226_avg_tab,
177 ARRAY_SIZE(ina226_avg_tab));
179 return INA226_SHIFT_AVG(avg_bits);
183 * Calibration register is set to the best value, which eliminates
184 * truncation errors on calculating current register in hardware.
185 * According to datasheet (eq. 3) the best values are 2048 for
186 * ina226 and 4096 for ina219. They are hardcoded as calibration_value.
188 static int ina2xx_calibrate(struct ina2xx_data *data)
190 return regmap_write(data->regmap, INA2XX_CALIBRATION,
191 data->config->calibration_value);
195 * Initialize the configuration and calibration registers.
197 static int ina2xx_init(struct ina2xx_data *data)
199 int ret = regmap_write(data->regmap, INA2XX_CONFIG,
200 data->config->config_default);
204 return ina2xx_calibrate(data);
207 static int ina2xx_read_reg(struct device *dev, int reg, unsigned int *regval)
209 struct ina2xx_data *data = dev_get_drvdata(dev);
212 dev_dbg(dev, "Starting register %d read\n", reg);
214 for (retry = 5; retry; retry--) {
216 ret = regmap_read(data->regmap, reg, regval);
220 dev_dbg(dev, "read %d, val = 0x%04x\n", reg, *regval);
223 * If the current value in the calibration register is 0, the
224 * power and current registers will also remain at 0. In case
225 * the chip has been reset let's check the calibration
226 * register and reinitialize if needed.
227 * We do that extra read of the calibration register if there
228 * is some hint of a chip reset.
233 ret = regmap_read(data->regmap, INA2XX_CALIBRATION,
239 dev_warn(dev, "chip not calibrated, reinitializing\n");
241 ret = ina2xx_init(data);
245 * Let's make sure the power and current
246 * registers have been updated before trying
249 msleep(INA2XX_MAX_DELAY);
257 * If we're here then although all write operations succeeded, the
258 * chip still returns 0 in the calibration register. Nothing more we
261 dev_err(dev, "unable to reinitialize the chip\n");
265 static int ina2xx_get_value(struct ina2xx_data *data, u8 reg,
271 case INA2XX_SHUNT_VOLTAGE:
272 /* signed register */
273 val = DIV_ROUND_CLOSEST((s16)regval, data->config->shunt_div);
275 case INA2XX_BUS_VOLTAGE:
276 val = (regval >> data->config->bus_voltage_shift)
277 * data->config->bus_voltage_lsb;
278 val = DIV_ROUND_CLOSEST(val, 1000);
281 val = regval * data->power_lsb_uW;
284 /* signed register, result in mA */
285 val = (s16)regval * data->current_lsb_uA;
286 val = DIV_ROUND_CLOSEST(val, 1000);
288 case INA2XX_CALIBRATION:
292 /* programmer goofed */
301 static ssize_t ina2xx_value_show(struct device *dev,
302 struct device_attribute *da, char *buf)
304 struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
305 struct ina2xx_data *data = dev_get_drvdata(dev);
308 int err = ina2xx_read_reg(dev, attr->index, ®val);
313 return snprintf(buf, PAGE_SIZE, "%d\n",
314 ina2xx_get_value(data, attr->index, regval));
317 static int ina226_reg_to_alert(struct ina2xx_data *data, u8 bit, u16 regval)
322 case INA226_SHUNT_OVER_VOLTAGE_BIT:
323 case INA226_SHUNT_UNDER_VOLTAGE_BIT:
324 reg = INA2XX_SHUNT_VOLTAGE;
326 case INA226_BUS_OVER_VOLTAGE_BIT:
327 case INA226_BUS_UNDER_VOLTAGE_BIT:
328 reg = INA2XX_BUS_VOLTAGE;
330 case INA226_POWER_OVER_LIMIT_BIT:
334 /* programmer goofed */
339 return ina2xx_get_value(data, reg, regval);
343 * Turns alert limit values into register values.
344 * Opposite of the formula in ina2xx_get_value().
346 static s16 ina226_alert_to_reg(struct ina2xx_data *data, u8 bit, int val)
349 case INA226_SHUNT_OVER_VOLTAGE_BIT:
350 case INA226_SHUNT_UNDER_VOLTAGE_BIT:
351 val *= data->config->shunt_div;
352 return clamp_val(val, SHRT_MIN, SHRT_MAX);
353 case INA226_BUS_OVER_VOLTAGE_BIT:
354 case INA226_BUS_UNDER_VOLTAGE_BIT:
355 val = (val * 1000) << data->config->bus_voltage_shift;
356 val = DIV_ROUND_CLOSEST(val, data->config->bus_voltage_lsb);
357 return clamp_val(val, 0, SHRT_MAX);
358 case INA226_POWER_OVER_LIMIT_BIT:
359 val = DIV_ROUND_CLOSEST(val, data->power_lsb_uW);
360 return clamp_val(val, 0, USHRT_MAX);
362 /* programmer goofed */
368 static ssize_t ina226_alert_show(struct device *dev,
369 struct device_attribute *da, char *buf)
371 struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
372 struct ina2xx_data *data = dev_get_drvdata(dev);
377 mutex_lock(&data->config_lock);
378 ret = regmap_read(data->regmap, INA226_MASK_ENABLE, ®val);
382 if (regval & BIT(attr->index)) {
383 ret = regmap_read(data->regmap, INA226_ALERT_LIMIT, ®val);
386 val = ina226_reg_to_alert(data, attr->index, regval);
389 ret = snprintf(buf, PAGE_SIZE, "%d\n", val);
391 mutex_unlock(&data->config_lock);
395 static ssize_t ina226_alert_store(struct device *dev,
396 struct device_attribute *da,
397 const char *buf, size_t count)
399 struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
400 struct ina2xx_data *data = dev_get_drvdata(dev);
404 ret = kstrtoul(buf, 10, &val);
409 * Clear all alerts first to avoid accidentally triggering ALERT pin
410 * due to register write sequence. Then, only enable the alert
411 * if the value is non-zero.
413 mutex_lock(&data->config_lock);
414 ret = regmap_update_bits(data->regmap, INA226_MASK_ENABLE,
415 INA226_ALERT_CONFIG_MASK, 0);
419 ret = regmap_write(data->regmap, INA226_ALERT_LIMIT,
420 ina226_alert_to_reg(data, attr->index, val));
425 ret = regmap_update_bits(data->regmap, INA226_MASK_ENABLE,
426 INA226_ALERT_CONFIG_MASK,
434 mutex_unlock(&data->config_lock);
438 static ssize_t ina226_alarm_show(struct device *dev,
439 struct device_attribute *da, char *buf)
441 struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
442 struct ina2xx_data *data = dev_get_drvdata(dev);
447 ret = regmap_read(data->regmap, INA226_MASK_ENABLE, ®val);
451 alarm = (regval & BIT(attr->index)) &&
452 (regval & INA226_ALERT_FUNCTION_FLAG);
453 return snprintf(buf, PAGE_SIZE, "%d\n", alarm);
457 * In order to keep calibration register value fixed, the product
458 * of current_lsb and shunt_resistor should also be fixed and equal
459 * to shunt_voltage_lsb = 1 / shunt_div multiplied by 10^9 in order
462 static int ina2xx_set_shunt(struct ina2xx_data *data, long val)
464 unsigned int dividend = DIV_ROUND_CLOSEST(1000000000,
465 data->config->shunt_div);
466 if (val <= 0 || val > dividend)
469 mutex_lock(&data->config_lock);
471 data->current_lsb_uA = DIV_ROUND_CLOSEST(dividend, val);
472 data->power_lsb_uW = data->config->power_lsb_factor *
473 data->current_lsb_uA;
474 mutex_unlock(&data->config_lock);
479 static ssize_t ina2xx_shunt_show(struct device *dev,
480 struct device_attribute *da, char *buf)
482 struct ina2xx_data *data = dev_get_drvdata(dev);
484 return snprintf(buf, PAGE_SIZE, "%li\n", data->rshunt);
487 static ssize_t ina2xx_shunt_store(struct device *dev,
488 struct device_attribute *da,
489 const char *buf, size_t count)
493 struct ina2xx_data *data = dev_get_drvdata(dev);
495 status = kstrtoul(buf, 10, &val);
499 status = ina2xx_set_shunt(data, val);
505 static ssize_t ina226_interval_store(struct device *dev,
506 struct device_attribute *da,
507 const char *buf, size_t count)
509 struct ina2xx_data *data = dev_get_drvdata(dev);
513 status = kstrtoul(buf, 10, &val);
517 if (val > INT_MAX || val == 0)
520 status = regmap_update_bits(data->regmap, INA2XX_CONFIG,
522 ina226_interval_to_reg(val));
529 static ssize_t ina226_interval_show(struct device *dev,
530 struct device_attribute *da, char *buf)
532 struct ina2xx_data *data = dev_get_drvdata(dev);
536 status = regmap_read(data->regmap, INA2XX_CONFIG, ®val);
540 return snprintf(buf, PAGE_SIZE, "%d\n", ina226_reg_to_interval(regval));
544 static SENSOR_DEVICE_ATTR_RO(in0_input, ina2xx_value, INA2XX_SHUNT_VOLTAGE);
545 /* shunt voltage over/under voltage alert setting and alarm */
546 static SENSOR_DEVICE_ATTR_RW(in0_crit, ina226_alert,
547 INA226_SHUNT_OVER_VOLTAGE_BIT);
548 static SENSOR_DEVICE_ATTR_RW(in0_lcrit, ina226_alert,
549 INA226_SHUNT_UNDER_VOLTAGE_BIT);
550 static SENSOR_DEVICE_ATTR_RO(in0_crit_alarm, ina226_alarm,
551 INA226_SHUNT_OVER_VOLTAGE_BIT);
552 static SENSOR_DEVICE_ATTR_RO(in0_lcrit_alarm, ina226_alarm,
553 INA226_SHUNT_UNDER_VOLTAGE_BIT);
556 static SENSOR_DEVICE_ATTR_RO(in1_input, ina2xx_value, INA2XX_BUS_VOLTAGE);
557 /* bus voltage over/under voltage alert setting and alarm */
558 static SENSOR_DEVICE_ATTR_RW(in1_crit, ina226_alert,
559 INA226_BUS_OVER_VOLTAGE_BIT);
560 static SENSOR_DEVICE_ATTR_RW(in1_lcrit, ina226_alert,
561 INA226_BUS_UNDER_VOLTAGE_BIT);
562 static SENSOR_DEVICE_ATTR_RO(in1_crit_alarm, ina226_alarm,
563 INA226_BUS_OVER_VOLTAGE_BIT);
564 static SENSOR_DEVICE_ATTR_RO(in1_lcrit_alarm, ina226_alarm,
565 INA226_BUS_UNDER_VOLTAGE_BIT);
567 /* calculated current */
568 static SENSOR_DEVICE_ATTR_RO(curr1_input, ina2xx_value, INA2XX_CURRENT);
570 /* calculated power */
571 static SENSOR_DEVICE_ATTR_RO(power1_input, ina2xx_value, INA2XX_POWER);
572 /* over-limit power alert setting and alarm */
573 static SENSOR_DEVICE_ATTR_RW(power1_crit, ina226_alert,
574 INA226_POWER_OVER_LIMIT_BIT);
575 static SENSOR_DEVICE_ATTR_RO(power1_crit_alarm, ina226_alarm,
576 INA226_POWER_OVER_LIMIT_BIT);
578 /* shunt resistance */
579 static SENSOR_DEVICE_ATTR_RW(shunt_resistor, ina2xx_shunt, INA2XX_CALIBRATION);
581 /* update interval (ina226 only) */
582 static SENSOR_DEVICE_ATTR_RW(update_interval, ina226_interval, 0);
584 /* pointers to created device attributes */
585 static struct attribute *ina2xx_attrs[] = {
586 &sensor_dev_attr_in0_input.dev_attr.attr,
587 &sensor_dev_attr_in1_input.dev_attr.attr,
588 &sensor_dev_attr_curr1_input.dev_attr.attr,
589 &sensor_dev_attr_power1_input.dev_attr.attr,
590 &sensor_dev_attr_shunt_resistor.dev_attr.attr,
594 static const struct attribute_group ina2xx_group = {
595 .attrs = ina2xx_attrs,
598 static struct attribute *ina226_attrs[] = {
599 &sensor_dev_attr_in0_crit.dev_attr.attr,
600 &sensor_dev_attr_in0_lcrit.dev_attr.attr,
601 &sensor_dev_attr_in0_crit_alarm.dev_attr.attr,
602 &sensor_dev_attr_in0_lcrit_alarm.dev_attr.attr,
603 &sensor_dev_attr_in1_crit.dev_attr.attr,
604 &sensor_dev_attr_in1_lcrit.dev_attr.attr,
605 &sensor_dev_attr_in1_crit_alarm.dev_attr.attr,
606 &sensor_dev_attr_in1_lcrit_alarm.dev_attr.attr,
607 &sensor_dev_attr_power1_crit.dev_attr.attr,
608 &sensor_dev_attr_power1_crit_alarm.dev_attr.attr,
609 &sensor_dev_attr_update_interval.dev_attr.attr,
613 static const struct attribute_group ina226_group = {
614 .attrs = ina226_attrs,
617 static const struct i2c_device_id ina2xx_id[];
619 static int ina2xx_probe(struct i2c_client *client)
621 struct device *dev = &client->dev;
622 struct ina2xx_data *data;
623 struct device *hwmon_dev;
626 enum ina2xx_ids chip;
628 if (client->dev.of_node)
629 chip = (enum ina2xx_ids)of_device_get_match_data(&client->dev);
631 chip = i2c_match_id(ina2xx_id, client)->driver_data;
633 data = devm_kzalloc(dev, sizeof(*data), GFP_KERNEL);
637 /* set the device type */
638 data->config = &ina2xx_config[chip];
639 mutex_init(&data->config_lock);
641 if (of_property_read_u32(dev->of_node, "shunt-resistor", &val) < 0) {
642 struct ina2xx_platform_data *pdata = dev_get_platdata(dev);
645 val = pdata->shunt_uohms;
647 val = INA2XX_RSHUNT_DEFAULT;
650 ina2xx_set_shunt(data, val);
652 ina2xx_regmap_config.max_register = data->config->registers;
654 data->regmap = devm_regmap_init_i2c(client, &ina2xx_regmap_config);
655 if (IS_ERR(data->regmap)) {
656 dev_err(dev, "failed to allocate register map\n");
657 return PTR_ERR(data->regmap);
660 ret = ina2xx_init(data);
662 dev_err(dev, "error configuring the device: %d\n", ret);
666 data->groups[group++] = &ina2xx_group;
668 data->groups[group++] = &ina226_group;
670 hwmon_dev = devm_hwmon_device_register_with_groups(dev, client->name,
672 if (IS_ERR(hwmon_dev))
673 return PTR_ERR(hwmon_dev);
675 dev_info(dev, "power monitor %s (Rshunt = %li uOhm)\n",
676 client->name, data->rshunt);
681 static const struct i2c_device_id ina2xx_id[] = {
682 { "ina219", ina219 },
683 { "ina220", ina219 },
684 { "ina226", ina226 },
685 { "ina230", ina226 },
686 { "ina231", ina226 },
689 MODULE_DEVICE_TABLE(i2c, ina2xx_id);
691 static const struct of_device_id __maybe_unused ina2xx_of_match[] = {
693 .compatible = "ti,ina219",
694 .data = (void *)ina219
697 .compatible = "ti,ina220",
698 .data = (void *)ina219
701 .compatible = "ti,ina226",
702 .data = (void *)ina226
705 .compatible = "ti,ina230",
706 .data = (void *)ina226
709 .compatible = "ti,ina231",
710 .data = (void *)ina226
714 MODULE_DEVICE_TABLE(of, ina2xx_of_match);
716 static struct i2c_driver ina2xx_driver = {
719 .of_match_table = of_match_ptr(ina2xx_of_match),
721 .probe_new = ina2xx_probe,
722 .id_table = ina2xx_id,
725 module_i2c_driver(ina2xx_driver);
727 MODULE_AUTHOR("Lothar Felten <l-felten@ti.com>");
728 MODULE_DESCRIPTION("ina2xx driver");
729 MODULE_LICENSE("GPL");