blob: ca97f9e931bc0232445c0c14216756e615d12d98 [file] [log] [blame]
Thomas Gleixnerb886d832019-06-01 10:08:55 +02001// SPDX-License-Identifier: GPL-2.0-only
Felten, Lotharf7c2fe32012-05-12 04:36:38 -04002/*
3 * Driver for Texas Instruments INA219, INA226 power monitor chips
4 *
5 * INA219:
6 * Zero Drift Bi-Directional Current/Power Monitor with I2C Interface
Alexander A. Klimov49dc2fb2020-07-19 20:15:30 +02007 * Datasheet: https://www.ti.com/product/ina219
Felten, Lotharf7c2fe32012-05-12 04:36:38 -04008 *
Guenter Roeckdc92cd02012-05-12 11:33:11 -07009 * INA220:
10 * Bi-Directional Current/Power Monitor with I2C Interface
Alexander A. Klimov49dc2fb2020-07-19 20:15:30 +020011 * Datasheet: https://www.ti.com/product/ina220
Guenter Roeckdc92cd02012-05-12 11:33:11 -070012 *
Felten, Lotharf7c2fe32012-05-12 04:36:38 -040013 * INA226:
14 * Bi-Directional Current/Power Monitor with I2C Interface
Alexander A. Klimov49dc2fb2020-07-19 20:15:30 +020015 * Datasheet: https://www.ti.com/product/ina226
Felten, Lotharf7c2fe32012-05-12 04:36:38 -040016 *
Guenter Roeckdc92cd02012-05-12 11:33:11 -070017 * INA230:
18 * Bi-directional Current/Power Monitor with I2C Interface
Alexander A. Klimov49dc2fb2020-07-19 20:15:30 +020019 * Datasheet: https://www.ti.com/product/ina230
Guenter Roeckdc92cd02012-05-12 11:33:11 -070020 *
Lothar Felten3ad86702018-08-14 09:09:37 +020021 * Copyright (C) 2012 Lothar Felten <lothar.felten@gmail.com>
Felten, Lotharf7c2fe32012-05-12 04:36:38 -040022 * Thanks to Jan Volkering
Felten, Lotharf7c2fe32012-05-12 04:36:38 -040023 */
24
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>
Jean Delvaredcd8f392012-10-10 15:25:56 +020033#include <linux/jiffies.h>
Javier Martinez Canillasbd0ddd42017-02-24 10:13:00 -030034#include <linux/of_device.h>
Tang Yuantian31e7ad72013-06-19 14:50:20 +080035#include <linux/of.h>
Bartosz Golaszewski509416a2015-01-05 15:20:52 +010036#include <linux/delay.h>
Bartosz Golaszewskid38df342015-04-16 12:43:34 -070037#include <linux/util_macros.h>
Marc Titingera0de56c2015-10-28 12:04:53 +010038#include <linux/regmap.h>
Felten, Lotharf7c2fe32012-05-12 04:36:38 -040039
40#include <linux/platform_data/ina2xx.h>
41
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
49
50/* INA226 register definitions */
51#define INA226_MASK_ENABLE 0x06
52#define INA226_ALERT_LIMIT 0x07
53#define INA226_DIE_ID 0xFF
54
Felten, Lotharf7c2fe32012-05-12 04:36:38 -040055/* register count */
56#define INA219_REGISTERS 6
57#define INA226_REGISTERS 8
58
59#define INA2XX_MAX_REGISTERS 8
60
61/* settings - depend on use case */
62#define INA219_CONFIG_DEFAULT 0x399F /* PGA=8 */
63#define INA226_CONFIG_DEFAULT 0x4527 /* averages=16 */
64
65/* worst case is 68.10 ms (~14.6Hz, ina219) */
66#define INA2XX_CONVERSION_RATE 15
Bartosz Golaszewski509416a2015-01-05 15:20:52 +010067#define INA2XX_MAX_DELAY 69 /* worst case delay in ms */
68
69#define INA2XX_RSHUNT_DEFAULT 10000
Felten, Lotharf7c2fe32012-05-12 04:36:38 -040070
Bartosz Golaszewski72a87a42015-01-09 17:03:42 +010071/* bit mask for reading the averaging setting in the configuration register */
72#define INA226_AVG_RD_MASK 0x0E00
73
74#define INA226_READ_AVG(reg) (((reg) & INA226_AVG_RD_MASK) >> 9)
75#define INA226_SHIFT_AVG(val) ((val) << 9)
76
Alex Qiu5a56a392020-05-04 17:59:45 -070077/* 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
83
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)
87
Bartosz Golaszewski72a87a42015-01-09 17:03:42 +010088/* common attrs, ina226 attrs and NULL */
89#define INA2XX_MAX_ATTRIBUTE_GROUPS 3
90
91/*
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.
94 */
95#define INA226_TOTAL_CONV_TIME_DEFAULT 2200
96
Marc Titingera0de56c2015-10-28 12:04:53 +010097static struct regmap_config ina2xx_regmap_config = {
98 .reg_bits = 8,
99 .val_bits = 16,
100};
101
Felten, Lotharf7c2fe32012-05-12 04:36:38 -0400102enum ina2xx_ids { ina219, ina226 };
103
Guenter Roeck6106db22012-05-12 11:21:01 -0700104struct ina2xx_config {
105 u16 config_default;
Maciej Purski5d389b12017-11-22 16:32:15 +0100106 int calibration_value;
Guenter Roeck6106db22012-05-12 11:21:01 -0700107 int registers;
108 int shunt_div;
109 int bus_voltage_shift;
110 int bus_voltage_lsb; /* uV */
Maciej Purski5d389b12017-11-22 16:32:15 +0100111 int power_lsb_factor;
Guenter Roeck6106db22012-05-12 11:21:01 -0700112};
113
Felten, Lotharf7c2fe32012-05-12 04:36:38 -0400114struct ina2xx_data {
Guenter Roeck6106db22012-05-12 11:21:01 -0700115 const struct ina2xx_config *config;
Felten, Lotharf7c2fe32012-05-12 04:36:38 -0400116
Bartosz Golaszewski509416a2015-01-05 15:20:52 +0100117 long rshunt;
Maciej Purski5d389b12017-11-22 16:32:15 +0100118 long current_lsb_uA;
119 long power_lsb_uW;
Marc Titingera0de56c2015-10-28 12:04:53 +0100120 struct mutex config_lock;
121 struct regmap *regmap;
Felten, Lotharf7c2fe32012-05-12 04:36:38 -0400122
Bartosz Golaszewski72a87a42015-01-09 17:03:42 +0100123 const struct attribute_group *groups[INA2XX_MAX_ATTRIBUTE_GROUPS];
Felten, Lotharf7c2fe32012-05-12 04:36:38 -0400124};
125
Guenter Roeck6106db22012-05-12 11:21:01 -0700126static const struct ina2xx_config ina2xx_config[] = {
127 [ina219] = {
128 .config_default = INA219_CONFIG_DEFAULT,
Maciej Purski5d389b12017-11-22 16:32:15 +0100129 .calibration_value = 4096,
Guenter Roeck6106db22012-05-12 11:21:01 -0700130 .registers = INA219_REGISTERS,
131 .shunt_div = 100,
132 .bus_voltage_shift = 3,
133 .bus_voltage_lsb = 4000,
Maciej Purski5d389b12017-11-22 16:32:15 +0100134 .power_lsb_factor = 20,
Guenter Roeck6106db22012-05-12 11:21:01 -0700135 },
136 [ina226] = {
137 .config_default = INA226_CONFIG_DEFAULT,
Maciej Purski5d389b12017-11-22 16:32:15 +0100138 .calibration_value = 2048,
Guenter Roeck6106db22012-05-12 11:21:01 -0700139 .registers = INA226_REGISTERS,
140 .shunt_div = 400,
141 .bus_voltage_shift = 0,
142 .bus_voltage_lsb = 1250,
Maciej Purski5d389b12017-11-22 16:32:15 +0100143 .power_lsb_factor = 25,
Guenter Roeck6106db22012-05-12 11:21:01 -0700144 },
145};
146
Bartosz Golaszewski72a87a42015-01-09 17:03:42 +0100147/*
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
Alexander A. Klimov49dc2fb2020-07-19 20:15:30 +0200151 * https://www.ti.com/lit/ds/symlink/ina226.pdf.
Bartosz Golaszewski72a87a42015-01-09 17:03:42 +0100152 */
153static const int ina226_avg_tab[] = { 1, 4, 16, 64, 128, 256, 512, 1024 };
154
Bartosz Golaszewski72a87a42015-01-09 17:03:42 +0100155static int ina226_reg_to_interval(u16 config)
156{
157 int avg = ina226_avg_tab[INA226_READ_AVG(config)];
158
159 /*
160 * Multiply the total conversion time by the number of averages.
161 * Return the result in milliseconds.
162 */
163 return DIV_ROUND_CLOSEST(avg * INA226_TOTAL_CONV_TIME_DEFAULT, 1000);
164}
165
Marc Titingera0de56c2015-10-28 12:04:53 +0100166/*
167 * Return the new, shifted AVG field value of CONFIG register,
168 * to use with regmap_update_bits
169 */
170static u16 ina226_interval_to_reg(int interval)
Bartosz Golaszewski72a87a42015-01-09 17:03:42 +0100171{
172 int avg, avg_bits;
173
174 avg = DIV_ROUND_CLOSEST(interval * 1000,
175 INA226_TOTAL_CONV_TIME_DEFAULT);
Bartosz Golaszewskid38df342015-04-16 12:43:34 -0700176 avg_bits = find_closest(avg, ina226_avg_tab,
177 ARRAY_SIZE(ina226_avg_tab));
Bartosz Golaszewski72a87a42015-01-09 17:03:42 +0100178
Marc Titingera0de56c2015-10-28 12:04:53 +0100179 return INA226_SHIFT_AVG(avg_bits);
Bartosz Golaszewski72a87a42015-01-09 17:03:42 +0100180}
181
Maciej Purski5d389b12017-11-22 16:32:15 +0100182/*
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.
187 */
Bartosz Golaszewski8a5fc792015-01-05 15:20:55 +0100188static int ina2xx_calibrate(struct ina2xx_data *data)
189{
Maciej Purski5d389b12017-11-22 16:32:15 +0100190 return regmap_write(data->regmap, INA2XX_CALIBRATION,
191 data->config->calibration_value);
Bartosz Golaszewski8a5fc792015-01-05 15:20:55 +0100192}
193
Bartosz Golaszewski509416a2015-01-05 15:20:52 +0100194/*
195 * Initialize the configuration and calibration registers.
196 */
197static int ina2xx_init(struct ina2xx_data *data)
198{
Marc Titingera0de56c2015-10-28 12:04:53 +0100199 int ret = regmap_write(data->regmap, INA2XX_CONFIG,
200 data->config->config_default);
Bartosz Golaszewski509416a2015-01-05 15:20:52 +0100201 if (ret < 0)
202 return ret;
203
Bartosz Golaszewski8a5fc792015-01-05 15:20:55 +0100204 return ina2xx_calibrate(data);
Bartosz Golaszewski509416a2015-01-05 15:20:52 +0100205}
206
Marc Titingera0de56c2015-10-28 12:04:53 +0100207static int ina2xx_read_reg(struct device *dev, int reg, unsigned int *regval)
Felten, Lotharf7c2fe32012-05-12 04:36:38 -0400208{
Guenter Roeck468bf0e2013-09-02 13:16:19 -0700209 struct ina2xx_data *data = dev_get_drvdata(dev);
Marc Titingera0de56c2015-10-28 12:04:53 +0100210 int ret, retry;
Bartosz Golaszewski509416a2015-01-05 15:20:52 +0100211
Marc Titingera0de56c2015-10-28 12:04:53 +0100212 dev_dbg(dev, "Starting register %d read\n", reg);
Bartosz Golaszewski509416a2015-01-05 15:20:52 +0100213
214 for (retry = 5; retry; retry--) {
Marc Titingera0de56c2015-10-28 12:04:53 +0100215
216 ret = regmap_read(data->regmap, reg, regval);
217 if (ret < 0)
218 return ret;
219
220 dev_dbg(dev, "read %d, val = 0x%04x\n", reg, *regval);
Bartosz Golaszewski509416a2015-01-05 15:20:52 +0100221
222 /*
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.
Marc Titingera0de56c2015-10-28 12:04:53 +0100227 * We do that extra read of the calibration register if there
228 * is some hint of a chip reset.
Bartosz Golaszewski509416a2015-01-05 15:20:52 +0100229 */
Marc Titingera0de56c2015-10-28 12:04:53 +0100230 if (*regval == 0) {
231 unsigned int cal;
Bartosz Golaszewski509416a2015-01-05 15:20:52 +0100232
Marc Titingera0de56c2015-10-28 12:04:53 +0100233 ret = regmap_read(data->regmap, INA2XX_CALIBRATION,
234 &cal);
235 if (ret < 0)
236 return ret;
Bartosz Golaszewski509416a2015-01-05 15:20:52 +0100237
Marc Titingera0de56c2015-10-28 12:04:53 +0100238 if (cal == 0) {
239 dev_warn(dev, "chip not calibrated, reinitializing\n");
240
241 ret = ina2xx_init(data);
242 if (ret < 0)
243 return ret;
244 /*
245 * Let's make sure the power and current
246 * registers have been updated before trying
247 * again.
248 */
249 msleep(INA2XX_MAX_DELAY);
250 continue;
251 }
Bartosz Golaszewski509416a2015-01-05 15:20:52 +0100252 }
Bartosz Golaszewski509416a2015-01-05 15:20:52 +0100253 return 0;
254 }
255
256 /*
257 * If we're here then although all write operations succeeded, the
258 * chip still returns 0 in the calibration register. Nothing more we
259 * can do here.
260 */
261 dev_err(dev, "unable to reinitialize the chip\n");
262 return -ENODEV;
263}
264
Marc Titingera0de56c2015-10-28 12:04:53 +0100265static int ina2xx_get_value(struct ina2xx_data *data, u8 reg,
266 unsigned int regval)
Felten, Lotharf7c2fe32012-05-12 04:36:38 -0400267{
Guenter Roeck6106db22012-05-12 11:21:01 -0700268 int val;
Felten, Lotharf7c2fe32012-05-12 04:36:38 -0400269
270 switch (reg) {
271 case INA2XX_SHUNT_VOLTAGE:
Fabio Baltieric0214f92014-06-08 22:06:24 +0100272 /* signed register */
Marc Titingera0de56c2015-10-28 12:04:53 +0100273 val = DIV_ROUND_CLOSEST((s16)regval, data->config->shunt_div);
Felten, Lotharf7c2fe32012-05-12 04:36:38 -0400274 break;
275 case INA2XX_BUS_VOLTAGE:
Marc Titingera0de56c2015-10-28 12:04:53 +0100276 val = (regval >> data->config->bus_voltage_shift)
Guenter Roeck6106db22012-05-12 11:21:01 -0700277 * data->config->bus_voltage_lsb;
278 val = DIV_ROUND_CLOSEST(val, 1000);
Felten, Lotharf7c2fe32012-05-12 04:36:38 -0400279 break;
280 case INA2XX_POWER:
Maciej Purski5d389b12017-11-22 16:32:15 +0100281 val = regval * data->power_lsb_uW;
Felten, Lotharf7c2fe32012-05-12 04:36:38 -0400282 break;
283 case INA2XX_CURRENT:
Maciej Purski5d389b12017-11-22 16:32:15 +0100284 /* signed register, result in mA */
Nicolin Chen38cd989e2018-11-13 19:48:54 -0800285 val = (s16)regval * data->current_lsb_uA;
Maciej Purski5d389b12017-11-22 16:32:15 +0100286 val = DIV_ROUND_CLOSEST(val, 1000);
Felten, Lotharf7c2fe32012-05-12 04:36:38 -0400287 break;
Bartosz Golaszewski8a5fc792015-01-05 15:20:55 +0100288 case INA2XX_CALIBRATION:
Maciej Purski5d389b12017-11-22 16:32:15 +0100289 val = regval;
Bartosz Golaszewski8a5fc792015-01-05 15:20:55 +0100290 break;
Felten, Lotharf7c2fe32012-05-12 04:36:38 -0400291 default:
292 /* programmer goofed */
293 WARN_ON_ONCE(1);
294 val = 0;
295 break;
296 }
297
298 return val;
299}
300
Guenter Roeck6a0f2342018-12-06 11:06:23 -0800301static ssize_t ina2xx_value_show(struct device *dev,
Felten, Lotharf7c2fe32012-05-12 04:36:38 -0400302 struct device_attribute *da, char *buf)
303{
304 struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
Marc Titingera0de56c2015-10-28 12:04:53 +0100305 struct ina2xx_data *data = dev_get_drvdata(dev);
306 unsigned int regval;
Felten, Lotharf7c2fe32012-05-12 04:36:38 -0400307
Marc Titingera0de56c2015-10-28 12:04:53 +0100308 int err = ina2xx_read_reg(dev, attr->index, &regval);
309
310 if (err < 0)
311 return err;
Felten, Lotharf7c2fe32012-05-12 04:36:38 -0400312
Guenter Roeck6106db22012-05-12 11:21:01 -0700313 return snprintf(buf, PAGE_SIZE, "%d\n",
Marc Titingera0de56c2015-10-28 12:04:53 +0100314 ina2xx_get_value(data, attr->index, regval));
Felten, Lotharf7c2fe32012-05-12 04:36:38 -0400315}
316
Alex Qiu5a56a392020-05-04 17:59:45 -0700317static int ina226_reg_to_alert(struct ina2xx_data *data, u8 bit, u16 regval)
318{
319 int reg;
320
321 switch (bit) {
322 case INA226_SHUNT_OVER_VOLTAGE_BIT:
323 case INA226_SHUNT_UNDER_VOLTAGE_BIT:
324 reg = INA2XX_SHUNT_VOLTAGE;
325 break;
326 case INA226_BUS_OVER_VOLTAGE_BIT:
327 case INA226_BUS_UNDER_VOLTAGE_BIT:
328 reg = INA2XX_BUS_VOLTAGE;
329 break;
330 case INA226_POWER_OVER_LIMIT_BIT:
331 reg = INA2XX_POWER;
332 break;
333 default:
334 /* programmer goofed */
335 WARN_ON_ONCE(1);
336 return 0;
337 }
338
339 return ina2xx_get_value(data, reg, regval);
340}
341
342/*
343 * Turns alert limit values into register values.
344 * Opposite of the formula in ina2xx_get_value().
345 */
346static s16 ina226_alert_to_reg(struct ina2xx_data *data, u8 bit, int val)
347{
348 switch (bit) {
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);
361 default:
362 /* programmer goofed */
363 WARN_ON_ONCE(1);
364 return 0;
365 }
366}
367
368static ssize_t ina226_alert_show(struct device *dev,
369 struct device_attribute *da, char *buf)
370{
371 struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
372 struct ina2xx_data *data = dev_get_drvdata(dev);
373 int regval;
374 int val = 0;
375 int ret;
376
377 mutex_lock(&data->config_lock);
378 ret = regmap_read(data->regmap, INA226_MASK_ENABLE, &regval);
379 if (ret)
380 goto abort;
381
382 if (regval & BIT(attr->index)) {
383 ret = regmap_read(data->regmap, INA226_ALERT_LIMIT, &regval);
384 if (ret)
385 goto abort;
386 val = ina226_reg_to_alert(data, attr->index, regval);
387 }
388
389 ret = snprintf(buf, PAGE_SIZE, "%d\n", val);
390abort:
391 mutex_unlock(&data->config_lock);
392 return ret;
393}
394
395static ssize_t ina226_alert_store(struct device *dev,
396 struct device_attribute *da,
397 const char *buf, size_t count)
398{
399 struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
400 struct ina2xx_data *data = dev_get_drvdata(dev);
401 unsigned long val;
402 int ret;
403
404 ret = kstrtoul(buf, 10, &val);
405 if (ret < 0)
406 return ret;
407
408 /*
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.
412 */
413 mutex_lock(&data->config_lock);
414 ret = regmap_update_bits(data->regmap, INA226_MASK_ENABLE,
415 INA226_ALERT_CONFIG_MASK, 0);
416 if (ret < 0)
417 goto abort;
418
419 ret = regmap_write(data->regmap, INA226_ALERT_LIMIT,
420 ina226_alert_to_reg(data, attr->index, val));
421 if (ret < 0)
422 goto abort;
423
424 if (val != 0) {
425 ret = regmap_update_bits(data->regmap, INA226_MASK_ENABLE,
426 INA226_ALERT_CONFIG_MASK,
427 BIT(attr->index));
428 if (ret < 0)
429 goto abort;
430 }
431
432 ret = count;
433abort:
434 mutex_unlock(&data->config_lock);
435 return ret;
436}
437
438static ssize_t ina226_alarm_show(struct device *dev,
439 struct device_attribute *da, char *buf)
440{
441 struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
442 struct ina2xx_data *data = dev_get_drvdata(dev);
443 int regval;
444 int alarm = 0;
445 int ret;
446
447 ret = regmap_read(data->regmap, INA226_MASK_ENABLE, &regval);
448 if (ret)
449 return ret;
450
451 alarm = (regval & BIT(attr->index)) &&
452 (regval & INA226_ALERT_FUNCTION_FLAG);
453 return snprintf(buf, PAGE_SIZE, "%d\n", alarm);
454}
455
Maciej Purski5d389b12017-11-22 16:32:15 +0100456/*
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
460 * to keep the scale.
461 */
462static int ina2xx_set_shunt(struct ina2xx_data *data, long val)
463{
464 unsigned int dividend = DIV_ROUND_CLOSEST(1000000000,
465 data->config->shunt_div);
466 if (val <= 0 || val > dividend)
467 return -EINVAL;
468
469 mutex_lock(&data->config_lock);
470 data->rshunt = val;
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);
475
476 return 0;
477}
478
Guenter Roeck6a0f2342018-12-06 11:06:23 -0800479static ssize_t ina2xx_shunt_show(struct device *dev,
480 struct device_attribute *da, char *buf)
Lothar Felten3ad86702018-08-14 09:09:37 +0200481{
482 struct ina2xx_data *data = dev_get_drvdata(dev);
483
484 return snprintf(buf, PAGE_SIZE, "%li\n", data->rshunt);
485}
486
Guenter Roeck6a0f2342018-12-06 11:06:23 -0800487static ssize_t ina2xx_shunt_store(struct device *dev,
Maciej Purski5d389b12017-11-22 16:32:15 +0100488 struct device_attribute *da,
489 const char *buf, size_t count)
Bartosz Golaszewski8a5fc792015-01-05 15:20:55 +0100490{
Bartosz Golaszewski8a5fc792015-01-05 15:20:55 +0100491 unsigned long val;
492 int status;
Marc Titingera0de56c2015-10-28 12:04:53 +0100493 struct ina2xx_data *data = dev_get_drvdata(dev);
Bartosz Golaszewski8a5fc792015-01-05 15:20:55 +0100494
495 status = kstrtoul(buf, 10, &val);
496 if (status < 0)
497 return status;
498
Maciej Purski5d389b12017-11-22 16:32:15 +0100499 status = ina2xx_set_shunt(data, val);
Bartosz Golaszewski8a5fc792015-01-05 15:20:55 +0100500 if (status < 0)
501 return status;
Bartosz Golaszewski8a5fc792015-01-05 15:20:55 +0100502 return count;
503}
504
Guenter Roeck6a0f2342018-12-06 11:06:23 -0800505static ssize_t ina226_interval_store(struct device *dev,
506 struct device_attribute *da,
507 const char *buf, size_t count)
Bartosz Golaszewski72a87a42015-01-09 17:03:42 +0100508{
509 struct ina2xx_data *data = dev_get_drvdata(dev);
510 unsigned long val;
511 int status;
512
Bartosz Golaszewski72a87a42015-01-09 17:03:42 +0100513 status = kstrtoul(buf, 10, &val);
514 if (status < 0)
515 return status;
516
517 if (val > INT_MAX || val == 0)
518 return -EINVAL;
519
Marc Titingera0de56c2015-10-28 12:04:53 +0100520 status = regmap_update_bits(data->regmap, INA2XX_CONFIG,
521 INA226_AVG_RD_MASK,
522 ina226_interval_to_reg(val));
Bartosz Golaszewski72a87a42015-01-09 17:03:42 +0100523 if (status < 0)
524 return status;
525
526 return count;
527}
528
Guenter Roeck6a0f2342018-12-06 11:06:23 -0800529static ssize_t ina226_interval_show(struct device *dev,
Bartosz Golaszewski72a87a42015-01-09 17:03:42 +0100530 struct device_attribute *da, char *buf)
531{
Marc Titingera0de56c2015-10-28 12:04:53 +0100532 struct ina2xx_data *data = dev_get_drvdata(dev);
533 int status;
534 unsigned int regval;
Bartosz Golaszewski72a87a42015-01-09 17:03:42 +0100535
Marc Titingera0de56c2015-10-28 12:04:53 +0100536 status = regmap_read(data->regmap, INA2XX_CONFIG, &regval);
537 if (status)
538 return status;
Bartosz Golaszewski72a87a42015-01-09 17:03:42 +0100539
Marc Titingera0de56c2015-10-28 12:04:53 +0100540 return snprintf(buf, PAGE_SIZE, "%d\n", ina226_reg_to_interval(regval));
Bartosz Golaszewski72a87a42015-01-09 17:03:42 +0100541}
542
Felten, Lotharf7c2fe32012-05-12 04:36:38 -0400543/* shunt voltage */
Guenter Roeck6a0f2342018-12-06 11:06:23 -0800544static SENSOR_DEVICE_ATTR_RO(in0_input, ina2xx_value, INA2XX_SHUNT_VOLTAGE);
Alex Qiu5a56a392020-05-04 17:59:45 -0700545/* shunt voltage over/under voltage alert setting and alarm */
546static SENSOR_DEVICE_ATTR_RW(in0_crit, ina226_alert,
547 INA226_SHUNT_OVER_VOLTAGE_BIT);
548static SENSOR_DEVICE_ATTR_RW(in0_lcrit, ina226_alert,
549 INA226_SHUNT_UNDER_VOLTAGE_BIT);
550static SENSOR_DEVICE_ATTR_RO(in0_crit_alarm, ina226_alarm,
551 INA226_SHUNT_OVER_VOLTAGE_BIT);
552static SENSOR_DEVICE_ATTR_RO(in0_lcrit_alarm, ina226_alarm,
553 INA226_SHUNT_UNDER_VOLTAGE_BIT);
Felten, Lotharf7c2fe32012-05-12 04:36:38 -0400554
555/* bus voltage */
Guenter Roeck6a0f2342018-12-06 11:06:23 -0800556static SENSOR_DEVICE_ATTR_RO(in1_input, ina2xx_value, INA2XX_BUS_VOLTAGE);
Alex Qiu5a56a392020-05-04 17:59:45 -0700557/* bus voltage over/under voltage alert setting and alarm */
558static SENSOR_DEVICE_ATTR_RW(in1_crit, ina226_alert,
559 INA226_BUS_OVER_VOLTAGE_BIT);
560static SENSOR_DEVICE_ATTR_RW(in1_lcrit, ina226_alert,
561 INA226_BUS_UNDER_VOLTAGE_BIT);
562static SENSOR_DEVICE_ATTR_RO(in1_crit_alarm, ina226_alarm,
563 INA226_BUS_OVER_VOLTAGE_BIT);
564static SENSOR_DEVICE_ATTR_RO(in1_lcrit_alarm, ina226_alarm,
565 INA226_BUS_UNDER_VOLTAGE_BIT);
Felten, Lotharf7c2fe32012-05-12 04:36:38 -0400566
567/* calculated current */
Guenter Roeck6a0f2342018-12-06 11:06:23 -0800568static SENSOR_DEVICE_ATTR_RO(curr1_input, ina2xx_value, INA2XX_CURRENT);
Felten, Lotharf7c2fe32012-05-12 04:36:38 -0400569
570/* calculated power */
Guenter Roeck6a0f2342018-12-06 11:06:23 -0800571static SENSOR_DEVICE_ATTR_RO(power1_input, ina2xx_value, INA2XX_POWER);
Alex Qiu5a56a392020-05-04 17:59:45 -0700572/* over-limit power alert setting and alarm */
573static SENSOR_DEVICE_ATTR_RW(power1_crit, ina226_alert,
574 INA226_POWER_OVER_LIMIT_BIT);
575static SENSOR_DEVICE_ATTR_RO(power1_crit_alarm, ina226_alarm,
576 INA226_POWER_OVER_LIMIT_BIT);
Felten, Lotharf7c2fe32012-05-12 04:36:38 -0400577
Bartosz Golaszewski8a5fc792015-01-05 15:20:55 +0100578/* shunt resistance */
Guenter Roeck6a0f2342018-12-06 11:06:23 -0800579static SENSOR_DEVICE_ATTR_RW(shunt_resistor, ina2xx_shunt, INA2XX_CALIBRATION);
Bartosz Golaszewski8a5fc792015-01-05 15:20:55 +0100580
Bartosz Golaszewski72a87a42015-01-09 17:03:42 +0100581/* update interval (ina226 only) */
Guenter Roeck6a0f2342018-12-06 11:06:23 -0800582static SENSOR_DEVICE_ATTR_RW(update_interval, ina226_interval, 0);
Bartosz Golaszewski72a87a42015-01-09 17:03:42 +0100583
Felten, Lotharf7c2fe32012-05-12 04:36:38 -0400584/* pointers to created device attributes */
Guenter Roeck468bf0e2013-09-02 13:16:19 -0700585static struct attribute *ina2xx_attrs[] = {
Felten, Lotharf7c2fe32012-05-12 04:36:38 -0400586 &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,
Bartosz Golaszewski8a5fc792015-01-05 15:20:55 +0100590 &sensor_dev_attr_shunt_resistor.dev_attr.attr,
Felten, Lotharf7c2fe32012-05-12 04:36:38 -0400591 NULL,
592};
Bartosz Golaszewski72a87a42015-01-09 17:03:42 +0100593
594static const struct attribute_group ina2xx_group = {
595 .attrs = ina2xx_attrs,
596};
597
598static struct attribute *ina226_attrs[] = {
Alex Qiu5a56a392020-05-04 17:59:45 -0700599 &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,
Bartosz Golaszewski72a87a42015-01-09 17:03:42 +0100609 &sensor_dev_attr_update_interval.dev_attr.attr,
610 NULL,
611};
612
613static const struct attribute_group ina226_group = {
614 .attrs = ina226_attrs,
615};
Felten, Lotharf7c2fe32012-05-12 04:36:38 -0400616
Stephen Kitt67487032020-08-13 18:02:22 +0200617static const struct i2c_device_id ina2xx_id[];
618
619static int ina2xx_probe(struct i2c_client *client)
Felten, Lotharf7c2fe32012-05-12 04:36:38 -0400620{
Guenter Roeck468bf0e2013-09-02 13:16:19 -0700621 struct device *dev = &client->dev;
622 struct ina2xx_data *data;
623 struct device *hwmon_dev;
Guenter Roeck468bf0e2013-09-02 13:16:19 -0700624 u32 val;
Bartosz Golaszewski72a87a42015-01-09 17:03:42 +0100625 int ret, group = 0;
Javier Martinez Canillasbd0ddd42017-02-24 10:13:00 -0300626 enum ina2xx_ids chip;
627
628 if (client->dev.of_node)
629 chip = (enum ina2xx_ids)of_device_get_match_data(&client->dev);
630 else
Stephen Kitt67487032020-08-13 18:02:22 +0200631 chip = i2c_match_id(ina2xx_id, client)->driver_data;
Felten, Lotharf7c2fe32012-05-12 04:36:38 -0400632
Guenter Roeck468bf0e2013-09-02 13:16:19 -0700633 data = devm_kzalloc(dev, sizeof(*data), GFP_KERNEL);
Felten, Lotharf7c2fe32012-05-12 04:36:38 -0400634 if (!data)
635 return -ENOMEM;
636
Felten, Lotharf7c2fe32012-05-12 04:36:38 -0400637 /* set the device type */
Javier Martinez Canillasbd0ddd42017-02-24 10:13:00 -0300638 data->config = &ina2xx_config[chip];
Marek Szyprowski0c4c5862018-01-15 14:58:21 +0100639 mutex_init(&data->config_lock);
Bartosz Golaszewski72a87a42015-01-09 17:03:42 +0100640
Marc Titinger001e2e72015-10-27 10:51:08 +0100641 if (of_property_read_u32(dev->of_node, "shunt-resistor", &val) < 0) {
642 struct ina2xx_platform_data *pdata = dev_get_platdata(dev);
643
644 if (pdata)
645 val = pdata->shunt_uohms;
646 else
647 val = INA2XX_RSHUNT_DEFAULT;
648 }
649
Maciej Purski5d389b12017-11-22 16:32:15 +0100650 ina2xx_set_shunt(data, val);
Marc Titinger001e2e72015-10-27 10:51:08 +0100651
Marc Titingera0de56c2015-10-28 12:04:53 +0100652 ina2xx_regmap_config.max_register = data->config->registers;
653
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);
658 }
659
Bartosz Golaszewski509416a2015-01-05 15:20:52 +0100660 ret = ina2xx_init(data);
661 if (ret < 0) {
662 dev_err(dev, "error configuring the device: %d\n", ret);
663 return -ENODEV;
664 }
665
Bartosz Golaszewski72a87a42015-01-09 17:03:42 +0100666 data->groups[group++] = &ina2xx_group;
Nicolin Chen70df9eb2018-11-09 16:42:14 -0800667 if (chip == ina226)
Bartosz Golaszewski72a87a42015-01-09 17:03:42 +0100668 data->groups[group++] = &ina226_group;
669
Guenter Roeck468bf0e2013-09-02 13:16:19 -0700670 hwmon_dev = devm_hwmon_device_register_with_groups(dev, client->name,
Bartosz Golaszewski72a87a42015-01-09 17:03:42 +0100671 data, data->groups);
Guenter Roeck468bf0e2013-09-02 13:16:19 -0700672 if (IS_ERR(hwmon_dev))
673 return PTR_ERR(hwmon_dev);
Felten, Lotharf7c2fe32012-05-12 04:36:38 -0400674
Guenter Roeck468bf0e2013-09-02 13:16:19 -0700675 dev_info(dev, "power monitor %s (Rshunt = %li uOhm)\n",
Nicolin Chen70df9eb2018-11-09 16:42:14 -0800676 client->name, data->rshunt);
Guenter Roeck6106db22012-05-12 11:21:01 -0700677
Felten, Lotharf7c2fe32012-05-12 04:36:38 -0400678 return 0;
Felten, Lotharf7c2fe32012-05-12 04:36:38 -0400679}
680
681static const struct i2c_device_id ina2xx_id[] = {
682 { "ina219", ina219 },
Guenter Roeckdc92cd02012-05-12 11:33:11 -0700683 { "ina220", ina219 },
Felten, Lotharf7c2fe32012-05-12 04:36:38 -0400684 { "ina226", ina226 },
Guenter Roeckdc92cd02012-05-12 11:33:11 -0700685 { "ina230", ina226 },
Kevin Hilmanadd513b2015-01-14 17:34:58 -0800686 { "ina231", ina226 },
Felten, Lotharf7c2fe32012-05-12 04:36:38 -0400687 { }
688};
689MODULE_DEVICE_TABLE(i2c, ina2xx_id);
690
Guenter Roeckdf6b8c72019-04-04 08:00:57 -0700691static const struct of_device_id __maybe_unused ina2xx_of_match[] = {
Javier Martinez Canillasbd0ddd42017-02-24 10:13:00 -0300692 {
693 .compatible = "ti,ina219",
694 .data = (void *)ina219
695 },
696 {
697 .compatible = "ti,ina220",
698 .data = (void *)ina219
699 },
700 {
701 .compatible = "ti,ina226",
702 .data = (void *)ina226
703 },
704 {
705 .compatible = "ti,ina230",
706 .data = (void *)ina226
707 },
708 {
709 .compatible = "ti,ina231",
710 .data = (void *)ina226
711 },
712 { },
713};
714MODULE_DEVICE_TABLE(of, ina2xx_of_match);
715
Felten, Lotharf7c2fe32012-05-12 04:36:38 -0400716static struct i2c_driver ina2xx_driver = {
717 .driver = {
718 .name = "ina2xx",
Javier Martinez Canillasbd0ddd42017-02-24 10:13:00 -0300719 .of_match_table = of_match_ptr(ina2xx_of_match),
Felten, Lotharf7c2fe32012-05-12 04:36:38 -0400720 },
Stephen Kitt67487032020-08-13 18:02:22 +0200721 .probe_new = ina2xx_probe,
Felten, Lotharf7c2fe32012-05-12 04:36:38 -0400722 .id_table = ina2xx_id,
723};
724
Wei Yongjund835ca02012-10-08 20:40:35 +0800725module_i2c_driver(ina2xx_driver);
Felten, Lotharf7c2fe32012-05-12 04:36:38 -0400726
727MODULE_AUTHOR("Lothar Felten <l-felten@ti.com>");
728MODULE_DESCRIPTION("ina2xx driver");
729MODULE_LICENSE("GPL");