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>
35 #include <linux/delay.h>
36 #include <linux/util_macros.h>
37 #include <linux/regmap.h>
39 #include <linux/platform_data/ina2xx.h>
41 /* common register definitions */
42 #define INA2XX_CONFIG 0x00
43 #define INA2XX_SHUNT_VOLTAGE 0x01 /* readonly */
44 #define INA2XX_BUS_VOLTAGE 0x02 /* readonly */
45 #define INA2XX_POWER 0x03 /* readonly */
46 #define INA2XX_CURRENT 0x04 /* readonly */
47 #define INA2XX_CALIBRATION 0x05
49 /* INA226 register definitions */
50 #define INA226_MASK_ENABLE 0x06
51 #define INA226_ALERT_LIMIT 0x07
52 #define INA226_DIE_ID 0xFF
55 #define INA219_REGISTERS 6
56 #define INA226_REGISTERS 8
58 #define INA2XX_MAX_REGISTERS 8
60 /* settings - depend on use case */
61 #define INA219_CONFIG_DEFAULT 0x399F /* PGA=8 */
62 #define INA226_CONFIG_DEFAULT 0x4527 /* averages=16 */
64 /* worst case is 68.10 ms (~14.6Hz, ina219) */
65 #define INA2XX_CONVERSION_RATE 15
66 #define INA2XX_MAX_DELAY 69 /* worst case delay in ms */
68 #define INA2XX_RSHUNT_DEFAULT 10000
70 /* bit mask for reading the averaging setting in the configuration register */
71 #define INA226_AVG_RD_MASK 0x0E00
73 #define INA226_READ_AVG(reg) (((reg) & INA226_AVG_RD_MASK) >> 9)
74 #define INA226_SHIFT_AVG(val) ((val) << 9)
76 /* bit number of alert functions in Mask/Enable Register */
77 #define INA226_SHUNT_OVER_VOLTAGE_BIT 15
78 #define INA226_SHUNT_UNDER_VOLTAGE_BIT 14
79 #define INA226_BUS_OVER_VOLTAGE_BIT 13
80 #define INA226_BUS_UNDER_VOLTAGE_BIT 12
81 #define INA226_POWER_OVER_LIMIT_BIT 11
83 /* bit mask for alert config bits of Mask/Enable Register */
84 #define INA226_ALERT_CONFIG_MASK 0xFC00
85 #define INA226_ALERT_FUNCTION_FLAG BIT(4)
87 /* common attrs, ina226 attrs and NULL */
88 #define INA2XX_MAX_ATTRIBUTE_GROUPS 3
91 * Both bus voltage and shunt voltage conversion times for ina226 are set
92 * to 0b0100 on POR, which translates to 2200 microseconds in total.
94 #define INA226_TOTAL_CONV_TIME_DEFAULT 2200
96 static struct regmap_config ina2xx_regmap_config = {
101 enum ina2xx_ids { ina219, ina226 };
103 struct ina2xx_config {
105 int calibration_value;
108 int bus_voltage_shift;
109 int bus_voltage_lsb; /* uV */
110 int power_lsb_factor;
114 const struct ina2xx_config *config;
119 struct mutex config_lock;
120 struct regmap *regmap;
122 const struct attribute_group *groups[INA2XX_MAX_ATTRIBUTE_GROUPS];
125 static const struct ina2xx_config ina2xx_config[] = {
127 .config_default = INA219_CONFIG_DEFAULT,
128 .calibration_value = 4096,
129 .registers = INA219_REGISTERS,
131 .bus_voltage_shift = 3,
132 .bus_voltage_lsb = 4000,
133 .power_lsb_factor = 20,
136 .config_default = INA226_CONFIG_DEFAULT,
137 .calibration_value = 2048,
138 .registers = INA226_REGISTERS,
140 .bus_voltage_shift = 0,
141 .bus_voltage_lsb = 1250,
142 .power_lsb_factor = 25,
147 * Available averaging rates for ina226. The indices correspond with
148 * the bit values expected by the chip (according to the ina226 datasheet,
149 * table 3 AVG bit settings, found at
150 * https://www.ti.com/lit/ds/symlink/ina226.pdf.
152 static const int ina226_avg_tab[] = { 1, 4, 16, 64, 128, 256, 512, 1024 };
154 static int ina226_reg_to_interval(u16 config)
156 int avg = ina226_avg_tab[INA226_READ_AVG(config)];
159 * Multiply the total conversion time by the number of averages.
160 * Return the result in milliseconds.
162 return DIV_ROUND_CLOSEST(avg * INA226_TOTAL_CONV_TIME_DEFAULT, 1000);
166 * Return the new, shifted AVG field value of CONFIG register,
167 * to use with regmap_update_bits
169 static u16 ina226_interval_to_reg(int interval)
173 avg = DIV_ROUND_CLOSEST(interval * 1000,
174 INA226_TOTAL_CONV_TIME_DEFAULT);
175 avg_bits = find_closest(avg, ina226_avg_tab,
176 ARRAY_SIZE(ina226_avg_tab));
178 return INA226_SHIFT_AVG(avg_bits);
182 * Calibration register is set to the best value, which eliminates
183 * truncation errors on calculating current register in hardware.
184 * According to datasheet (eq. 3) the best values are 2048 for
185 * ina226 and 4096 for ina219. They are hardcoded as calibration_value.
187 static int ina2xx_calibrate(struct ina2xx_data *data)
189 return regmap_write(data->regmap, INA2XX_CALIBRATION,
190 data->config->calibration_value);
194 * Initialize the configuration and calibration registers.
196 static int ina2xx_init(struct ina2xx_data *data)
198 int ret = regmap_write(data->regmap, INA2XX_CONFIG,
199 data->config->config_default);
203 return ina2xx_calibrate(data);
206 static int ina2xx_read_reg(struct device *dev, int reg, unsigned int *regval)
208 struct ina2xx_data *data = dev_get_drvdata(dev);
211 dev_dbg(dev, "Starting register %d read\n", reg);
213 for (retry = 5; retry; retry--) {
215 ret = regmap_read(data->regmap, reg, regval);
219 dev_dbg(dev, "read %d, val = 0x%04x\n", reg, *regval);
222 * If the current value in the calibration register is 0, the
223 * power and current registers will also remain at 0. In case
224 * the chip has been reset let's check the calibration
225 * register and reinitialize if needed.
226 * We do that extra read of the calibration register if there
227 * is some hint of a chip reset.
232 ret = regmap_read(data->regmap, INA2XX_CALIBRATION,
238 dev_warn(dev, "chip not calibrated, reinitializing\n");
240 ret = ina2xx_init(data);
244 * Let's make sure the power and current
245 * registers have been updated before trying
248 msleep(INA2XX_MAX_DELAY);
256 * If we're here then although all write operations succeeded, the
257 * chip still returns 0 in the calibration register. Nothing more we
260 dev_err(dev, "unable to reinitialize the chip\n");
264 static int ina2xx_get_value(struct ina2xx_data *data, u8 reg,
270 case INA2XX_SHUNT_VOLTAGE:
271 /* signed register */
272 val = DIV_ROUND_CLOSEST((s16)regval, data->config->shunt_div);
274 case INA2XX_BUS_VOLTAGE:
275 val = (regval >> data->config->bus_voltage_shift)
276 * data->config->bus_voltage_lsb;
277 val = DIV_ROUND_CLOSEST(val, 1000);
280 val = regval * data->power_lsb_uW;
283 /* signed register, result in mA */
284 val = (s16)regval * data->current_lsb_uA;
285 val = DIV_ROUND_CLOSEST(val, 1000);
287 case INA2XX_CALIBRATION:
291 /* programmer goofed */
300 static ssize_t ina2xx_value_show(struct device *dev,
301 struct device_attribute *da, char *buf)
303 struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
304 struct ina2xx_data *data = dev_get_drvdata(dev);
307 int err = ina2xx_read_reg(dev, attr->index, ®val);
312 return sysfs_emit(buf, "%d\n", ina2xx_get_value(data, attr->index, regval));
315 static int ina226_reg_to_alert(struct ina2xx_data *data, u8 bit, u16 regval)
320 case INA226_SHUNT_OVER_VOLTAGE_BIT:
321 case INA226_SHUNT_UNDER_VOLTAGE_BIT:
322 reg = INA2XX_SHUNT_VOLTAGE;
324 case INA226_BUS_OVER_VOLTAGE_BIT:
325 case INA226_BUS_UNDER_VOLTAGE_BIT:
326 reg = INA2XX_BUS_VOLTAGE;
328 case INA226_POWER_OVER_LIMIT_BIT:
332 /* programmer goofed */
337 return ina2xx_get_value(data, reg, regval);
341 * Turns alert limit values into register values.
342 * Opposite of the formula in ina2xx_get_value().
344 static s16 ina226_alert_to_reg(struct ina2xx_data *data, u8 bit, int val)
347 case INA226_SHUNT_OVER_VOLTAGE_BIT:
348 case INA226_SHUNT_UNDER_VOLTAGE_BIT:
349 val *= data->config->shunt_div;
350 return clamp_val(val, SHRT_MIN, SHRT_MAX);
351 case INA226_BUS_OVER_VOLTAGE_BIT:
352 case INA226_BUS_UNDER_VOLTAGE_BIT:
353 val = (val * 1000) << data->config->bus_voltage_shift;
354 val = DIV_ROUND_CLOSEST(val, data->config->bus_voltage_lsb);
355 return clamp_val(val, 0, SHRT_MAX);
356 case INA226_POWER_OVER_LIMIT_BIT:
357 val = DIV_ROUND_CLOSEST(val, data->power_lsb_uW);
358 return clamp_val(val, 0, USHRT_MAX);
360 /* programmer goofed */
366 static ssize_t ina226_alert_show(struct device *dev,
367 struct device_attribute *da, char *buf)
369 struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
370 struct ina2xx_data *data = dev_get_drvdata(dev);
375 mutex_lock(&data->config_lock);
376 ret = regmap_read(data->regmap, INA226_MASK_ENABLE, ®val);
380 if (regval & BIT(attr->index)) {
381 ret = regmap_read(data->regmap, INA226_ALERT_LIMIT, ®val);
384 val = ina226_reg_to_alert(data, attr->index, regval);
387 ret = sysfs_emit(buf, "%d\n", val);
389 mutex_unlock(&data->config_lock);
393 static ssize_t ina226_alert_store(struct device *dev,
394 struct device_attribute *da,
395 const char *buf, size_t count)
397 struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
398 struct ina2xx_data *data = dev_get_drvdata(dev);
402 ret = kstrtoul(buf, 10, &val);
407 * Clear all alerts first to avoid accidentally triggering ALERT pin
408 * due to register write sequence. Then, only enable the alert
409 * if the value is non-zero.
411 mutex_lock(&data->config_lock);
412 ret = regmap_update_bits(data->regmap, INA226_MASK_ENABLE,
413 INA226_ALERT_CONFIG_MASK, 0);
417 ret = regmap_write(data->regmap, INA226_ALERT_LIMIT,
418 ina226_alert_to_reg(data, attr->index, val));
423 ret = regmap_update_bits(data->regmap, INA226_MASK_ENABLE,
424 INA226_ALERT_CONFIG_MASK,
432 mutex_unlock(&data->config_lock);
436 static ssize_t ina226_alarm_show(struct device *dev,
437 struct device_attribute *da, char *buf)
439 struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
440 struct ina2xx_data *data = dev_get_drvdata(dev);
445 ret = regmap_read(data->regmap, INA226_MASK_ENABLE, ®val);
449 alarm = (regval & BIT(attr->index)) &&
450 (regval & INA226_ALERT_FUNCTION_FLAG);
451 return sysfs_emit(buf, "%d\n", alarm);
455 * In order to keep calibration register value fixed, the product
456 * of current_lsb and shunt_resistor should also be fixed and equal
457 * to shunt_voltage_lsb = 1 / shunt_div multiplied by 10^9 in order
460 static int ina2xx_set_shunt(struct ina2xx_data *data, long val)
462 unsigned int dividend = DIV_ROUND_CLOSEST(1000000000,
463 data->config->shunt_div);
464 if (val <= 0 || val > dividend)
467 mutex_lock(&data->config_lock);
469 data->current_lsb_uA = DIV_ROUND_CLOSEST(dividend, val);
470 data->power_lsb_uW = data->config->power_lsb_factor *
471 data->current_lsb_uA;
472 mutex_unlock(&data->config_lock);
477 static ssize_t ina2xx_shunt_show(struct device *dev,
478 struct device_attribute *da, char *buf)
480 struct ina2xx_data *data = dev_get_drvdata(dev);
482 return sysfs_emit(buf, "%li\n", data->rshunt);
485 static ssize_t ina2xx_shunt_store(struct device *dev,
486 struct device_attribute *da,
487 const char *buf, size_t count)
491 struct ina2xx_data *data = dev_get_drvdata(dev);
493 status = kstrtoul(buf, 10, &val);
497 status = ina2xx_set_shunt(data, val);
503 static ssize_t ina226_interval_store(struct device *dev,
504 struct device_attribute *da,
505 const char *buf, size_t count)
507 struct ina2xx_data *data = dev_get_drvdata(dev);
511 status = kstrtoul(buf, 10, &val);
515 if (val > INT_MAX || val == 0)
518 status = regmap_update_bits(data->regmap, INA2XX_CONFIG,
520 ina226_interval_to_reg(val));
527 static ssize_t ina226_interval_show(struct device *dev,
528 struct device_attribute *da, char *buf)
530 struct ina2xx_data *data = dev_get_drvdata(dev);
534 status = regmap_read(data->regmap, INA2XX_CONFIG, ®val);
538 return sysfs_emit(buf, "%d\n", ina226_reg_to_interval(regval));
542 static SENSOR_DEVICE_ATTR_RO(in0_input, ina2xx_value, INA2XX_SHUNT_VOLTAGE);
543 /* shunt voltage over/under voltage alert setting and alarm */
544 static SENSOR_DEVICE_ATTR_RW(in0_crit, ina226_alert,
545 INA226_SHUNT_OVER_VOLTAGE_BIT);
546 static SENSOR_DEVICE_ATTR_RW(in0_lcrit, ina226_alert,
547 INA226_SHUNT_UNDER_VOLTAGE_BIT);
548 static SENSOR_DEVICE_ATTR_RO(in0_crit_alarm, ina226_alarm,
549 INA226_SHUNT_OVER_VOLTAGE_BIT);
550 static SENSOR_DEVICE_ATTR_RO(in0_lcrit_alarm, ina226_alarm,
551 INA226_SHUNT_UNDER_VOLTAGE_BIT);
554 static SENSOR_DEVICE_ATTR_RO(in1_input, ina2xx_value, INA2XX_BUS_VOLTAGE);
555 /* bus voltage over/under voltage alert setting and alarm */
556 static SENSOR_DEVICE_ATTR_RW(in1_crit, ina226_alert,
557 INA226_BUS_OVER_VOLTAGE_BIT);
558 static SENSOR_DEVICE_ATTR_RW(in1_lcrit, ina226_alert,
559 INA226_BUS_UNDER_VOLTAGE_BIT);
560 static SENSOR_DEVICE_ATTR_RO(in1_crit_alarm, ina226_alarm,
561 INA226_BUS_OVER_VOLTAGE_BIT);
562 static SENSOR_DEVICE_ATTR_RO(in1_lcrit_alarm, ina226_alarm,
563 INA226_BUS_UNDER_VOLTAGE_BIT);
565 /* calculated current */
566 static SENSOR_DEVICE_ATTR_RO(curr1_input, ina2xx_value, INA2XX_CURRENT);
568 /* calculated power */
569 static SENSOR_DEVICE_ATTR_RO(power1_input, ina2xx_value, INA2XX_POWER);
570 /* over-limit power alert setting and alarm */
571 static SENSOR_DEVICE_ATTR_RW(power1_crit, ina226_alert,
572 INA226_POWER_OVER_LIMIT_BIT);
573 static SENSOR_DEVICE_ATTR_RO(power1_crit_alarm, ina226_alarm,
574 INA226_POWER_OVER_LIMIT_BIT);
576 /* shunt resistance */
577 static SENSOR_DEVICE_ATTR_RW(shunt_resistor, ina2xx_shunt, INA2XX_CALIBRATION);
579 /* update interval (ina226 only) */
580 static SENSOR_DEVICE_ATTR_RW(update_interval, ina226_interval, 0);
582 /* pointers to created device attributes */
583 static struct attribute *ina2xx_attrs[] = {
584 &sensor_dev_attr_in0_input.dev_attr.attr,
585 &sensor_dev_attr_in1_input.dev_attr.attr,
586 &sensor_dev_attr_curr1_input.dev_attr.attr,
587 &sensor_dev_attr_power1_input.dev_attr.attr,
588 &sensor_dev_attr_shunt_resistor.dev_attr.attr,
592 static const struct attribute_group ina2xx_group = {
593 .attrs = ina2xx_attrs,
596 static struct attribute *ina226_attrs[] = {
597 &sensor_dev_attr_in0_crit.dev_attr.attr,
598 &sensor_dev_attr_in0_lcrit.dev_attr.attr,
599 &sensor_dev_attr_in0_crit_alarm.dev_attr.attr,
600 &sensor_dev_attr_in0_lcrit_alarm.dev_attr.attr,
601 &sensor_dev_attr_in1_crit.dev_attr.attr,
602 &sensor_dev_attr_in1_lcrit.dev_attr.attr,
603 &sensor_dev_attr_in1_crit_alarm.dev_attr.attr,
604 &sensor_dev_attr_in1_lcrit_alarm.dev_attr.attr,
605 &sensor_dev_attr_power1_crit.dev_attr.attr,
606 &sensor_dev_attr_power1_crit_alarm.dev_attr.attr,
607 &sensor_dev_attr_update_interval.dev_attr.attr,
611 static const struct attribute_group ina226_group = {
612 .attrs = ina226_attrs,
615 static const struct i2c_device_id ina2xx_id[];
617 static int ina2xx_probe(struct i2c_client *client)
619 struct device *dev = &client->dev;
620 struct ina2xx_data *data;
621 struct device *hwmon_dev;
624 enum ina2xx_ids chip;
626 if (client->dev.of_node)
627 chip = (uintptr_t)of_device_get_match_data(&client->dev);
629 chip = i2c_match_id(ina2xx_id, client)->driver_data;
631 data = devm_kzalloc(dev, sizeof(*data), GFP_KERNEL);
635 /* set the device type */
636 data->config = &ina2xx_config[chip];
637 mutex_init(&data->config_lock);
639 if (of_property_read_u32(dev->of_node, "shunt-resistor", &val) < 0) {
640 struct ina2xx_platform_data *pdata = dev_get_platdata(dev);
643 val = pdata->shunt_uohms;
645 val = INA2XX_RSHUNT_DEFAULT;
648 ina2xx_set_shunt(data, val);
650 ina2xx_regmap_config.max_register = data->config->registers;
652 data->regmap = devm_regmap_init_i2c(client, &ina2xx_regmap_config);
653 if (IS_ERR(data->regmap)) {
654 dev_err(dev, "failed to allocate register map\n");
655 return PTR_ERR(data->regmap);
658 ret = devm_regulator_get_enable(dev, "vs");
660 return dev_err_probe(dev, ret, "failed to enable vs regulator\n");
662 ret = ina2xx_init(data);
664 dev_err(dev, "error configuring the device: %d\n", ret);
668 data->groups[group++] = &ina2xx_group;
670 data->groups[group++] = &ina226_group;
672 hwmon_dev = devm_hwmon_device_register_with_groups(dev, client->name,
674 if (IS_ERR(hwmon_dev))
675 return PTR_ERR(hwmon_dev);
677 dev_info(dev, "power monitor %s (Rshunt = %li uOhm)\n",
678 client->name, data->rshunt);
683 static const struct i2c_device_id ina2xx_id[] = {
684 { "ina219", ina219 },
685 { "ina220", ina219 },
686 { "ina226", ina226 },
687 { "ina230", ina226 },
688 { "ina231", ina226 },
691 MODULE_DEVICE_TABLE(i2c, ina2xx_id);
693 static const struct of_device_id __maybe_unused ina2xx_of_match[] = {
695 .compatible = "ti,ina219",
696 .data = (void *)ina219
699 .compatible = "ti,ina220",
700 .data = (void *)ina219
703 .compatible = "ti,ina226",
704 .data = (void *)ina226
707 .compatible = "ti,ina230",
708 .data = (void *)ina226
711 .compatible = "ti,ina231",
712 .data = (void *)ina226
716 MODULE_DEVICE_TABLE(of, ina2xx_of_match);
718 static struct i2c_driver ina2xx_driver = {
721 .of_match_table = of_match_ptr(ina2xx_of_match),
723 .probe = ina2xx_probe,
724 .id_table = ina2xx_id,
727 module_i2c_driver(ina2xx_driver);
729 MODULE_AUTHOR("Lothar Felten <l-felten@ti.com>");
730 MODULE_DESCRIPTION("ina2xx driver");
731 MODULE_LICENSE("GPL");