Loading...
1// SPDX-License-Identifier: GPL-2.0-only
2/*
3 * Driver for Texas Instruments INA219, INA226 power monitor chips
4 *
5 * INA219:
6 * Zero Drift Bi-Directional Current/Power Monitor with I2C Interface
7 * Datasheet: https://www.ti.com/product/ina219
8 *
9 * INA220:
10 * Bi-Directional Current/Power Monitor with I2C Interface
11 * Datasheet: https://www.ti.com/product/ina220
12 *
13 * INA226:
14 * Bi-Directional Current/Power Monitor with I2C Interface
15 * Datasheet: https://www.ti.com/product/ina226
16 *
17 * INA230:
18 * Bi-directional Current/Power Monitor with I2C Interface
19 * Datasheet: https://www.ti.com/product/ina230
20 *
21 * Copyright (C) 2012 Lothar Felten <lothar.felten@gmail.com>
22 * Thanks to Jan Volkering
23 */
24
25#include <linux/bitfield.h>
26#include <linux/bits.h>
27#include <linux/delay.h>
28#include <linux/device.h>
29#include <linux/err.h>
30#include <linux/hwmon.h>
31#include <linux/i2c.h>
32#include <linux/init.h>
33#include <linux/kernel.h>
34#include <linux/module.h>
35#include <linux/property.h>
36#include <linux/regmap.h>
37#include <linux/slab.h>
38#include <linux/sysfs.h>
39#include <linux/util_macros.h>
40
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
48
49/* INA226 register definitions */
50#define INA226_MASK_ENABLE 0x06
51#define INA226_ALERT_LIMIT 0x07
52#define INA226_DIE_ID 0xFF
53
54/* SY24655 register definitions */
55#define SY24655_EIN 0x0A
56#define SY24655_ACCUM_CONFIG 0x0D
57#define INA2XX_MAX_REGISTERS 0x0D
58
59/* settings - depend on use case */
60#define INA219_CONFIG_DEFAULT 0x399F /* PGA=8 */
61#define INA226_CONFIG_DEFAULT 0x4527 /* averages=16 */
62#define INA260_CONFIG_DEFAULT 0x6527 /* averages=16 */
63#define SY24655_CONFIG_DEFAULT 0x4527 /* averages=16 */
64
65/* (only for sy24655) */
66#define SY24655_ACCUM_CONFIG_DEFAULT 0x044C /* continuous mode, clear after read*/
67
68/* worst case is 68.10 ms (~14.6Hz, ina219) */
69#define INA2XX_CONVERSION_RATE 15
70#define INA2XX_MAX_DELAY 69 /* worst case delay in ms */
71
72#define INA2XX_RSHUNT_DEFAULT 10000
73#define INA260_RSHUNT 2000
74
75/* bit mask for reading the averaging setting in the configuration register */
76#define INA226_AVG_RD_MASK GENMASK(11, 9)
77
78#define INA226_READ_AVG(reg) FIELD_GET(INA226_AVG_RD_MASK, reg)
79
80#define INA226_ALERT_LATCH_ENABLE BIT(0)
81#define INA226_ALERT_POLARITY BIT(1)
82
83/* bit number of alert functions in Mask/Enable Register */
84#define INA226_SHUNT_OVER_VOLTAGE_MASK BIT(15)
85#define INA226_SHUNT_UNDER_VOLTAGE_MASK BIT(14)
86#define INA226_BUS_OVER_VOLTAGE_MASK BIT(13)
87#define INA226_BUS_UNDER_VOLTAGE_MASK BIT(12)
88#define INA226_POWER_OVER_LIMIT_MASK BIT(11)
89
90/* bit mask for alert config bits of Mask/Enable Register */
91#define INA226_ALERT_CONFIG_MASK GENMASK(15, 10)
92#define INA226_ALERT_FUNCTION_FLAG BIT(4)
93
94/*
95 * Both bus voltage and shunt voltage conversion times for ina226 are set
96 * to 0b0100 on POR, which translates to 2200 microseconds in total.
97 */
98#define INA226_TOTAL_CONV_TIME_DEFAULT 2200
99
100static bool ina2xx_writeable_reg(struct device *dev, unsigned int reg)
101{
102 switch (reg) {
103 case INA2XX_CONFIG:
104 case INA2XX_CALIBRATION:
105 case INA226_MASK_ENABLE:
106 case INA226_ALERT_LIMIT:
107 case SY24655_ACCUM_CONFIG:
108 return true;
109 default:
110 return false;
111 }
112}
113
114static bool ina2xx_volatile_reg(struct device *dev, unsigned int reg)
115{
116 switch (reg) {
117 case INA2XX_SHUNT_VOLTAGE:
118 case INA2XX_BUS_VOLTAGE:
119 case INA2XX_POWER:
120 case INA2XX_CURRENT:
121 return true;
122 default:
123 return false;
124 }
125}
126
127static const struct regmap_config ina2xx_regmap_config = {
128 .reg_bits = 8,
129 .val_bits = 16,
130 .use_single_write = true,
131 .use_single_read = true,
132 .max_register = INA2XX_MAX_REGISTERS,
133 .cache_type = REGCACHE_MAPLE,
134 .volatile_reg = ina2xx_volatile_reg,
135 .writeable_reg = ina2xx_writeable_reg,
136};
137
138enum ina2xx_ids { ina219, ina226, ina260, sy24655 };
139
140struct ina2xx_config {
141 u16 config_default;
142 bool has_alerts; /* chip supports alerts and limits */
143 bool has_ishunt; /* chip has internal shunt resistor */
144 bool has_power_average; /* chip has internal shunt resistor */
145 int calibration_value;
146 int shunt_div;
147 int bus_voltage_shift;
148 int bus_voltage_lsb; /* uV */
149 int power_lsb_factor;
150};
151
152struct ina2xx_data {
153 const struct ina2xx_config *config;
154 enum ina2xx_ids chip;
155
156 long rshunt;
157 long current_lsb_uA;
158 long power_lsb_uW;
159 struct mutex config_lock;
160 struct regmap *regmap;
161 struct i2c_client *client;
162};
163
164static const struct ina2xx_config ina2xx_config[] = {
165 [ina219] = {
166 .config_default = INA219_CONFIG_DEFAULT,
167 .calibration_value = 4096,
168 .shunt_div = 100,
169 .bus_voltage_shift = 3,
170 .bus_voltage_lsb = 4000,
171 .power_lsb_factor = 20,
172 .has_alerts = false,
173 .has_ishunt = false,
174 .has_power_average = false,
175 },
176 [ina226] = {
177 .config_default = INA226_CONFIG_DEFAULT,
178 .calibration_value = 2048,
179 .shunt_div = 400,
180 .bus_voltage_shift = 0,
181 .bus_voltage_lsb = 1250,
182 .power_lsb_factor = 25,
183 .has_alerts = true,
184 .has_ishunt = false,
185 .has_power_average = false,
186 },
187 [ina260] = {
188 .config_default = INA260_CONFIG_DEFAULT,
189 .shunt_div = 400,
190 .bus_voltage_shift = 0,
191 .bus_voltage_lsb = 1250,
192 .power_lsb_factor = 8,
193 .has_alerts = true,
194 .has_ishunt = true,
195 .has_power_average = false,
196 },
197 [sy24655] = {
198 .config_default = SY24655_CONFIG_DEFAULT,
199 .calibration_value = 4096,
200 .shunt_div = 400,
201 .bus_voltage_shift = 0,
202 .bus_voltage_lsb = 1250,
203 .power_lsb_factor = 25,
204 .has_alerts = true,
205 .has_ishunt = false,
206 .has_power_average = true,
207 },
208};
209
210/*
211 * Available averaging rates for ina226. The indices correspond with
212 * the bit values expected by the chip (according to the ina226 datasheet,
213 * table 3 AVG bit settings, found at
214 * https://www.ti.com/lit/ds/symlink/ina226.pdf.
215 */
216static const int ina226_avg_tab[] = { 1, 4, 16, 64, 128, 256, 512, 1024 };
217
218static int ina226_reg_to_interval(u16 config)
219{
220 int avg = ina226_avg_tab[INA226_READ_AVG(config)];
221
222 /*
223 * Multiply the total conversion time by the number of averages.
224 * Return the result in milliseconds.
225 */
226 return DIV_ROUND_CLOSEST(avg * INA226_TOTAL_CONV_TIME_DEFAULT, 1000);
227}
228
229/*
230 * Return the new, shifted AVG field value of CONFIG register,
231 * to use with regmap_update_bits
232 */
233static u16 ina226_interval_to_reg(long interval)
234{
235 int avg, avg_bits;
236
237 /*
238 * The maximum supported interval is 1,024 * (2 * 8.244ms) ~= 16.8s.
239 * Clamp to 32 seconds before calculations to avoid overflows.
240 */
241 interval = clamp_val(interval, 0, 32000);
242
243 avg = DIV_ROUND_CLOSEST(interval * 1000,
244 INA226_TOTAL_CONV_TIME_DEFAULT);
245 avg_bits = find_closest(avg, ina226_avg_tab,
246 ARRAY_SIZE(ina226_avg_tab));
247
248 return FIELD_PREP(INA226_AVG_RD_MASK, avg_bits);
249}
250
251static int ina2xx_get_value(struct ina2xx_data *data, u8 reg,
252 unsigned int regval)
253{
254 int val;
255
256 switch (reg) {
257 case INA2XX_SHUNT_VOLTAGE:
258 /* signed register */
259 val = DIV_ROUND_CLOSEST((s16)regval, data->config->shunt_div);
260 break;
261 case INA2XX_BUS_VOLTAGE:
262 val = (regval >> data->config->bus_voltage_shift) *
263 data->config->bus_voltage_lsb;
264 val = DIV_ROUND_CLOSEST(val, 1000);
265 break;
266 case INA2XX_POWER:
267 val = regval * data->power_lsb_uW;
268 break;
269 case INA2XX_CURRENT:
270 /* signed register, result in mA */
271 val = (s16)regval * data->current_lsb_uA;
272 val = DIV_ROUND_CLOSEST(val, 1000);
273 break;
274 case INA2XX_CALIBRATION:
275 val = regval;
276 break;
277 default:
278 /* programmer goofed */
279 WARN_ON_ONCE(1);
280 val = 0;
281 break;
282 }
283
284 return val;
285}
286
287/*
288 * Read and convert register value from chip. If the register value is 0,
289 * check if the chip has been power cycled or reset. If so, re-initialize it.
290 */
291static int ina2xx_read_init(struct device *dev, int reg, long *val)
292{
293 struct ina2xx_data *data = dev_get_drvdata(dev);
294 struct regmap *regmap = data->regmap;
295 unsigned int regval;
296 int ret, retry;
297
298 if (data->config->has_ishunt) {
299 /* No calibration needed */
300 ret = regmap_read(regmap, reg, ®val);
301 if (ret < 0)
302 return ret;
303 *val = ina2xx_get_value(data, reg, regval);
304 return 0;
305 }
306
307 for (retry = 5; retry; retry--) {
308 ret = regmap_read(regmap, reg, ®val);
309 if (ret < 0)
310 return ret;
311
312 /*
313 * If the current value in the calibration register is 0, the
314 * power and current registers will also remain at 0. In case
315 * the chip has been reset let's check the calibration
316 * register and reinitialize if needed.
317 * We do that extra read of the calibration register if there
318 * is some hint of a chip reset.
319 */
320 if (regval == 0) {
321 unsigned int cal;
322
323 ret = regmap_read_bypassed(regmap, INA2XX_CALIBRATION, &cal);
324 if (ret < 0)
325 return ret;
326
327 if (cal == 0) {
328 dev_warn(dev, "chip not calibrated, reinitializing\n");
329
330 regcache_mark_dirty(regmap);
331 regcache_sync(regmap);
332
333 /*
334 * Let's make sure the power and current
335 * registers have been updated before trying
336 * again.
337 */
338 msleep(INA2XX_MAX_DELAY);
339 continue;
340 }
341 }
342 *val = ina2xx_get_value(data, reg, regval);
343 return 0;
344 }
345
346 /*
347 * If we're here then although all write operations succeeded, the
348 * chip still returns 0 in the calibration register. Nothing more we
349 * can do here.
350 */
351 dev_err(dev, "unable to reinitialize the chip\n");
352 return -ENODEV;
353}
354
355/*
356 * Turns alert limit values into register values.
357 * Opposite of the formula in ina2xx_get_value().
358 */
359static u16 ina226_alert_to_reg(struct ina2xx_data *data, int reg, long val)
360{
361 switch (reg) {
362 case INA2XX_SHUNT_VOLTAGE:
363 val = clamp_val(val, 0, SHRT_MAX * data->config->shunt_div);
364 val *= data->config->shunt_div;
365 return clamp_val(val, 0, SHRT_MAX);
366 case INA2XX_BUS_VOLTAGE:
367 val = clamp_val(val, 0, 200000);
368 val = (val * 1000) << data->config->bus_voltage_shift;
369 val = DIV_ROUND_CLOSEST(val, data->config->bus_voltage_lsb);
370 return clamp_val(val, 0, USHRT_MAX);
371 case INA2XX_POWER:
372 val = clamp_val(val, 0, UINT_MAX - data->power_lsb_uW);
373 val = DIV_ROUND_CLOSEST(val, data->power_lsb_uW);
374 return clamp_val(val, 0, USHRT_MAX);
375 case INA2XX_CURRENT:
376 val = clamp_val(val, INT_MIN / 1000, INT_MAX / 1000);
377 /* signed register, result in mA */
378 val = DIV_ROUND_CLOSEST(val * 1000, data->current_lsb_uA);
379 return clamp_val(val, SHRT_MIN, SHRT_MAX);
380 default:
381 /* programmer goofed */
382 WARN_ON_ONCE(1);
383 return 0;
384 }
385}
386
387static int ina226_alert_limit_read(struct ina2xx_data *data, u32 mask, int reg, long *val)
388{
389 struct regmap *regmap = data->regmap;
390 int regval;
391 int ret;
392
393 mutex_lock(&data->config_lock);
394 ret = regmap_read(regmap, INA226_MASK_ENABLE, ®val);
395 if (ret)
396 goto abort;
397
398 if (regval & mask) {
399 ret = regmap_read(regmap, INA226_ALERT_LIMIT, ®val);
400 if (ret)
401 goto abort;
402 *val = ina2xx_get_value(data, reg, regval);
403 } else {
404 *val = 0;
405 }
406abort:
407 mutex_unlock(&data->config_lock);
408 return ret;
409}
410
411static int ina226_alert_limit_write(struct ina2xx_data *data, u32 mask, int reg, long val)
412{
413 struct regmap *regmap = data->regmap;
414 int ret;
415
416 if (val < 0)
417 return -EINVAL;
418
419 /*
420 * Clear all alerts first to avoid accidentally triggering ALERT pin
421 * due to register write sequence. Then, only enable the alert
422 * if the value is non-zero.
423 */
424 mutex_lock(&data->config_lock);
425 ret = regmap_update_bits(regmap, INA226_MASK_ENABLE,
426 INA226_ALERT_CONFIG_MASK, 0);
427 if (ret < 0)
428 goto abort;
429
430 ret = regmap_write(regmap, INA226_ALERT_LIMIT,
431 ina226_alert_to_reg(data, reg, val));
432 if (ret < 0)
433 goto abort;
434
435 if (val)
436 ret = regmap_update_bits(regmap, INA226_MASK_ENABLE,
437 INA226_ALERT_CONFIG_MASK, mask);
438abort:
439 mutex_unlock(&data->config_lock);
440 return ret;
441}
442
443static int ina2xx_chip_read(struct device *dev, u32 attr, long *val)
444{
445 struct ina2xx_data *data = dev_get_drvdata(dev);
446 u32 regval;
447 int ret;
448
449 switch (attr) {
450 case hwmon_chip_update_interval:
451 ret = regmap_read(data->regmap, INA2XX_CONFIG, ®val);
452 if (ret)
453 return ret;
454
455 *val = ina226_reg_to_interval(regval);
456 break;
457 default:
458 return -EOPNOTSUPP;
459 }
460 return 0;
461}
462
463static int ina226_alert_read(struct regmap *regmap, u32 mask, long *val)
464{
465 unsigned int regval;
466 int ret;
467
468 ret = regmap_read_bypassed(regmap, INA226_MASK_ENABLE, ®val);
469 if (ret)
470 return ret;
471
472 *val = (regval & mask) && (regval & INA226_ALERT_FUNCTION_FLAG);
473
474 return 0;
475}
476
477static int ina2xx_in_read(struct device *dev, u32 attr, int channel, long *val)
478{
479 int voltage_reg = channel ? INA2XX_BUS_VOLTAGE : INA2XX_SHUNT_VOLTAGE;
480 u32 under_voltage_mask = channel ? INA226_BUS_UNDER_VOLTAGE_MASK
481 : INA226_SHUNT_UNDER_VOLTAGE_MASK;
482 u32 over_voltage_mask = channel ? INA226_BUS_OVER_VOLTAGE_MASK
483 : INA226_SHUNT_OVER_VOLTAGE_MASK;
484 struct ina2xx_data *data = dev_get_drvdata(dev);
485 struct regmap *regmap = data->regmap;
486 unsigned int regval;
487 int ret;
488
489 switch (attr) {
490 case hwmon_in_input:
491 ret = regmap_read(regmap, voltage_reg, ®val);
492 if (ret)
493 return ret;
494 *val = ina2xx_get_value(data, voltage_reg, regval);
495 break;
496 case hwmon_in_lcrit:
497 return ina226_alert_limit_read(data, under_voltage_mask,
498 voltage_reg, val);
499 case hwmon_in_crit:
500 return ina226_alert_limit_read(data, over_voltage_mask,
501 voltage_reg, val);
502 case hwmon_in_lcrit_alarm:
503 return ina226_alert_read(regmap, under_voltage_mask, val);
504 case hwmon_in_crit_alarm:
505 return ina226_alert_read(regmap, over_voltage_mask, val);
506 default:
507 return -EOPNOTSUPP;
508 }
509 return 0;
510}
511
512/*
513 * Configuring the READ_EIN (bit 10) of the ACCUM_CONFIG register to 1
514 * can clear accumulator and sample_count after reading the EIN register.
515 * This way, the average power between the last read and the current
516 * read can be obtained. By combining with accurate time data from
517 * outside, the energy consumption during that period can be calculated.
518 */
519static int sy24655_average_power_read(struct ina2xx_data *data, u8 reg, long *val)
520{
521 u8 template[6];
522 int ret;
523 long accumulator_24, sample_count;
524
525 /* 48-bit register read */
526 ret = i2c_smbus_read_i2c_block_data(data->client, reg, 6, template);
527 if (ret < 0)
528 return ret;
529 if (ret != 6)
530 return -EIO;
531 accumulator_24 = ((template[3] << 16) |
532 (template[4] << 8) |
533 template[5]);
534 sample_count = ((template[0] << 16) |
535 (template[1] << 8) |
536 template[2]);
537 if (sample_count <= 0) {
538 *val = 0;
539 return 0;
540 }
541
542 *val = DIV_ROUND_CLOSEST(accumulator_24, sample_count) * data->power_lsb_uW;
543
544 return 0;
545}
546
547static int ina2xx_power_read(struct device *dev, u32 attr, long *val)
548{
549 struct ina2xx_data *data = dev_get_drvdata(dev);
550
551 switch (attr) {
552 case hwmon_power_input:
553 return ina2xx_read_init(dev, INA2XX_POWER, val);
554 case hwmon_power_average:
555 return sy24655_average_power_read(data, SY24655_EIN, val);
556 case hwmon_power_crit:
557 return ina226_alert_limit_read(data, INA226_POWER_OVER_LIMIT_MASK,
558 INA2XX_POWER, val);
559 case hwmon_power_crit_alarm:
560 return ina226_alert_read(data->regmap, INA226_POWER_OVER_LIMIT_MASK, val);
561 default:
562 return -EOPNOTSUPP;
563 }
564}
565
566static int ina2xx_curr_read(struct device *dev, u32 attr, long *val)
567{
568 struct ina2xx_data *data = dev_get_drvdata(dev);
569 struct regmap *regmap = data->regmap;
570 unsigned int regval;
571 int ret;
572
573 /*
574 * While the chips supported by this driver do not directly support
575 * current limits, they do support setting shunt voltage limits.
576 * The shunt voltage divided by the shunt resistor value is the current.
577 * On top of that, calibration values are set such that in the shunt
578 * voltage register and the current register report the same values.
579 * That means we can report and configure current limits based on shunt
580 * voltage limits.
581 */
582 switch (attr) {
583 case hwmon_curr_input:
584 /*
585 * Since the shunt voltage and the current register report the
586 * same values when the chip is calibrated, we can calculate
587 * the current directly from the shunt voltage without relying
588 * on chip calibration.
589 */
590 ret = regmap_read(regmap, INA2XX_SHUNT_VOLTAGE, ®val);
591 if (ret)
592 return ret;
593 *val = ina2xx_get_value(data, INA2XX_CURRENT, regval);
594 return 0;
595 case hwmon_curr_lcrit:
596 return ina226_alert_limit_read(data, INA226_SHUNT_UNDER_VOLTAGE_MASK,
597 INA2XX_CURRENT, val);
598 case hwmon_curr_crit:
599 return ina226_alert_limit_read(data, INA226_SHUNT_OVER_VOLTAGE_MASK,
600 INA2XX_CURRENT, val);
601 case hwmon_curr_lcrit_alarm:
602 return ina226_alert_read(regmap, INA226_SHUNT_UNDER_VOLTAGE_MASK, val);
603 case hwmon_curr_crit_alarm:
604 return ina226_alert_read(regmap, INA226_SHUNT_OVER_VOLTAGE_MASK, val);
605 default:
606 return -EOPNOTSUPP;
607 }
608}
609
610static int ina2xx_read(struct device *dev, enum hwmon_sensor_types type,
611 u32 attr, int channel, long *val)
612{
613 switch (type) {
614 case hwmon_chip:
615 return ina2xx_chip_read(dev, attr, val);
616 case hwmon_in:
617 return ina2xx_in_read(dev, attr, channel, val);
618 case hwmon_power:
619 return ina2xx_power_read(dev, attr, val);
620 case hwmon_curr:
621 return ina2xx_curr_read(dev, attr, val);
622 default:
623 return -EOPNOTSUPP;
624 }
625}
626
627static int ina2xx_chip_write(struct device *dev, u32 attr, long val)
628{
629 struct ina2xx_data *data = dev_get_drvdata(dev);
630
631 switch (attr) {
632 case hwmon_chip_update_interval:
633 return regmap_update_bits(data->regmap, INA2XX_CONFIG,
634 INA226_AVG_RD_MASK,
635 ina226_interval_to_reg(val));
636 default:
637 return -EOPNOTSUPP;
638 }
639}
640
641static int ina2xx_in_write(struct device *dev, u32 attr, int channel, long val)
642{
643 struct ina2xx_data *data = dev_get_drvdata(dev);
644
645 switch (attr) {
646 case hwmon_in_lcrit:
647 return ina226_alert_limit_write(data,
648 channel ? INA226_BUS_UNDER_VOLTAGE_MASK : INA226_SHUNT_UNDER_VOLTAGE_MASK,
649 channel ? INA2XX_BUS_VOLTAGE : INA2XX_SHUNT_VOLTAGE,
650 val);
651 case hwmon_in_crit:
652 return ina226_alert_limit_write(data,
653 channel ? INA226_BUS_OVER_VOLTAGE_MASK : INA226_SHUNT_OVER_VOLTAGE_MASK,
654 channel ? INA2XX_BUS_VOLTAGE : INA2XX_SHUNT_VOLTAGE,
655 val);
656 default:
657 return -EOPNOTSUPP;
658 }
659 return 0;
660}
661
662static int ina2xx_power_write(struct device *dev, u32 attr, long val)
663{
664 struct ina2xx_data *data = dev_get_drvdata(dev);
665
666 switch (attr) {
667 case hwmon_power_crit:
668 return ina226_alert_limit_write(data, INA226_POWER_OVER_LIMIT_MASK,
669 INA2XX_POWER, val);
670 default:
671 return -EOPNOTSUPP;
672 }
673 return 0;
674}
675
676static int ina2xx_curr_write(struct device *dev, u32 attr, long val)
677{
678 struct ina2xx_data *data = dev_get_drvdata(dev);
679
680 switch (attr) {
681 case hwmon_curr_lcrit:
682 return ina226_alert_limit_write(data, INA226_SHUNT_UNDER_VOLTAGE_MASK,
683 INA2XX_CURRENT, val);
684 case hwmon_curr_crit:
685 return ina226_alert_limit_write(data, INA226_SHUNT_OVER_VOLTAGE_MASK,
686 INA2XX_CURRENT, val);
687 default:
688 return -EOPNOTSUPP;
689 }
690 return 0;
691}
692
693static int ina2xx_write(struct device *dev, enum hwmon_sensor_types type,
694 u32 attr, int channel, long val)
695{
696 switch (type) {
697 case hwmon_chip:
698 return ina2xx_chip_write(dev, attr, val);
699 case hwmon_in:
700 return ina2xx_in_write(dev, attr, channel, val);
701 case hwmon_power:
702 return ina2xx_power_write(dev, attr, val);
703 case hwmon_curr:
704 return ina2xx_curr_write(dev, attr, val);
705 default:
706 return -EOPNOTSUPP;
707 }
708}
709
710static umode_t ina2xx_is_visible(const void *_data, enum hwmon_sensor_types type,
711 u32 attr, int channel)
712{
713 const struct ina2xx_data *data = _data;
714 bool has_alerts = data->config->has_alerts;
715 bool has_power_average = data->config->has_power_average;
716 enum ina2xx_ids chip = data->chip;
717
718 switch (type) {
719 case hwmon_in:
720 switch (attr) {
721 case hwmon_in_input:
722 return 0444;
723 case hwmon_in_lcrit:
724 case hwmon_in_crit:
725 if (has_alerts)
726 return 0644;
727 break;
728 case hwmon_in_lcrit_alarm:
729 case hwmon_in_crit_alarm:
730 if (has_alerts)
731 return 0444;
732 break;
733 default:
734 break;
735 }
736 break;
737 case hwmon_curr:
738 switch (attr) {
739 case hwmon_curr_input:
740 return 0444;
741 case hwmon_curr_lcrit:
742 case hwmon_curr_crit:
743 if (has_alerts)
744 return 0644;
745 break;
746 case hwmon_curr_lcrit_alarm:
747 case hwmon_curr_crit_alarm:
748 if (has_alerts)
749 return 0444;
750 break;
751 default:
752 break;
753 }
754 break;
755 case hwmon_power:
756 switch (attr) {
757 case hwmon_power_input:
758 return 0444;
759 case hwmon_power_crit:
760 if (has_alerts)
761 return 0644;
762 break;
763 case hwmon_power_crit_alarm:
764 if (has_alerts)
765 return 0444;
766 break;
767 case hwmon_power_average:
768 if (has_power_average)
769 return 0444;
770 break;
771 default:
772 break;
773 }
774 break;
775 case hwmon_chip:
776 switch (attr) {
777 case hwmon_chip_update_interval:
778 if (chip == ina226 || chip == ina260)
779 return 0644;
780 break;
781 default:
782 break;
783 }
784 break;
785 default:
786 break;
787 }
788 return 0;
789}
790
791static const struct hwmon_channel_info * const ina2xx_info[] = {
792 HWMON_CHANNEL_INFO(chip,
793 HWMON_C_UPDATE_INTERVAL),
794 HWMON_CHANNEL_INFO(in,
795 HWMON_I_INPUT | HWMON_I_CRIT | HWMON_I_CRIT_ALARM |
796 HWMON_I_LCRIT | HWMON_I_LCRIT_ALARM,
797 HWMON_I_INPUT | HWMON_I_CRIT | HWMON_I_CRIT_ALARM |
798 HWMON_I_LCRIT | HWMON_I_LCRIT_ALARM
799 ),
800 HWMON_CHANNEL_INFO(curr, HWMON_C_INPUT | HWMON_C_CRIT | HWMON_C_CRIT_ALARM |
801 HWMON_C_LCRIT | HWMON_C_LCRIT_ALARM),
802 HWMON_CHANNEL_INFO(power,
803 HWMON_P_INPUT | HWMON_P_CRIT | HWMON_P_CRIT_ALARM |
804 HWMON_P_AVERAGE),
805 NULL
806};
807
808static const struct hwmon_ops ina2xx_hwmon_ops = {
809 .is_visible = ina2xx_is_visible,
810 .read = ina2xx_read,
811 .write = ina2xx_write,
812};
813
814static const struct hwmon_chip_info ina2xx_chip_info = {
815 .ops = &ina2xx_hwmon_ops,
816 .info = ina2xx_info,
817};
818
819/* shunt resistance */
820
821/*
822 * In order to keep calibration register value fixed, the product
823 * of current_lsb and shunt_resistor should also be fixed and equal
824 * to shunt_voltage_lsb = 1 / shunt_div multiplied by 10^9 in order
825 * to keep the scale.
826 */
827static int ina2xx_set_shunt(struct ina2xx_data *data, unsigned long val)
828{
829 unsigned int dividend = DIV_ROUND_CLOSEST(1000000000,
830 data->config->shunt_div);
831 if (!val || val > dividend)
832 return -EINVAL;
833
834 data->rshunt = val;
835 data->current_lsb_uA = DIV_ROUND_CLOSEST(dividend, val);
836 data->power_lsb_uW = data->config->power_lsb_factor *
837 data->current_lsb_uA;
838
839 return 0;
840}
841
842static ssize_t shunt_resistor_show(struct device *dev,
843 struct device_attribute *da, char *buf)
844{
845 struct ina2xx_data *data = dev_get_drvdata(dev);
846
847 return sysfs_emit(buf, "%li\n", data->rshunt);
848}
849
850static ssize_t shunt_resistor_store(struct device *dev,
851 struct device_attribute *da,
852 const char *buf, size_t count)
853{
854 struct ina2xx_data *data = dev_get_drvdata(dev);
855 unsigned long val;
856 int status;
857
858 status = kstrtoul(buf, 10, &val);
859 if (status < 0)
860 return status;
861
862 mutex_lock(&data->config_lock);
863 status = ina2xx_set_shunt(data, val);
864 mutex_unlock(&data->config_lock);
865 if (status < 0)
866 return status;
867 return count;
868}
869
870static DEVICE_ATTR_RW(shunt_resistor);
871
872/* pointers to created device attributes */
873static struct attribute *ina2xx_attrs[] = {
874 &dev_attr_shunt_resistor.attr,
875 NULL,
876};
877ATTRIBUTE_GROUPS(ina2xx);
878
879/*
880 * Initialize chip
881 */
882static int ina2xx_init(struct device *dev, struct ina2xx_data *data)
883{
884 struct regmap *regmap = data->regmap;
885 u32 shunt;
886 int ret;
887
888 if (data->config->has_ishunt)
889 shunt = INA260_RSHUNT;
890 else if (device_property_read_u32(dev, "shunt-resistor", &shunt) < 0)
891 shunt = INA2XX_RSHUNT_DEFAULT;
892
893 ret = ina2xx_set_shunt(data, shunt);
894 if (ret < 0)
895 return ret;
896
897 ret = regmap_write(regmap, INA2XX_CONFIG, data->config->config_default);
898 if (ret < 0)
899 return ret;
900
901 if (data->config->has_alerts) {
902 bool active_high = device_property_read_bool(dev, "ti,alert-polarity-active-high");
903
904 regmap_update_bits(regmap, INA226_MASK_ENABLE,
905 INA226_ALERT_LATCH_ENABLE | INA226_ALERT_POLARITY,
906 INA226_ALERT_LATCH_ENABLE |
907 FIELD_PREP(INA226_ALERT_POLARITY, active_high));
908 }
909 if (data->config->has_power_average) {
910 if (data->chip == sy24655) {
911 /*
912 * Initialize the power accumulation method to continuous
913 * mode and clear the EIN register after each read of the
914 * EIN register
915 */
916 ret = regmap_write(regmap, SY24655_ACCUM_CONFIG,
917 SY24655_ACCUM_CONFIG_DEFAULT);
918 if (ret < 0)
919 return ret;
920 }
921 }
922
923 if (data->config->has_ishunt)
924 return 0;
925
926 /*
927 * Calibration register is set to the best value, which eliminates
928 * truncation errors on calculating current register in hardware.
929 * According to datasheet (eq. 3) the best values are 2048 for
930 * ina226 and 4096 for ina219. They are hardcoded as calibration_value.
931 */
932 return regmap_write(regmap, INA2XX_CALIBRATION,
933 data->config->calibration_value);
934}
935
936static int ina2xx_probe(struct i2c_client *client)
937{
938 struct device *dev = &client->dev;
939 struct ina2xx_data *data;
940 struct device *hwmon_dev;
941 enum ina2xx_ids chip;
942 int ret;
943
944 chip = (uintptr_t)i2c_get_match_data(client);
945
946 data = devm_kzalloc(dev, sizeof(*data), GFP_KERNEL);
947 if (!data)
948 return -ENOMEM;
949
950 /* set the device type */
951 data->client = client;
952 data->config = &ina2xx_config[chip];
953 data->chip = chip;
954 mutex_init(&data->config_lock);
955
956 data->regmap = devm_regmap_init_i2c(client, &ina2xx_regmap_config);
957 if (IS_ERR(data->regmap)) {
958 dev_err(dev, "failed to allocate register map\n");
959 return PTR_ERR(data->regmap);
960 }
961
962 ret = devm_regulator_get_enable(dev, "vs");
963 if (ret)
964 return dev_err_probe(dev, ret, "failed to enable vs regulator\n");
965
966 ret = ina2xx_init(dev, data);
967 if (ret < 0)
968 return dev_err_probe(dev, ret, "failed to configure device\n");
969
970 hwmon_dev = devm_hwmon_device_register_with_info(dev, client->name,
971 data, &ina2xx_chip_info,
972 data->config->has_ishunt ?
973 NULL : ina2xx_groups);
974 if (IS_ERR(hwmon_dev))
975 return PTR_ERR(hwmon_dev);
976
977 dev_info(dev, "power monitor %s (Rshunt = %li uOhm)\n",
978 client->name, data->rshunt);
979
980 return 0;
981}
982
983static const struct i2c_device_id ina2xx_id[] = {
984 { "ina219", ina219 },
985 { "ina220", ina219 },
986 { "ina226", ina226 },
987 { "ina230", ina226 },
988 { "ina231", ina226 },
989 { "ina260", ina260 },
990 { "sy24655", sy24655 },
991 { }
992};
993MODULE_DEVICE_TABLE(i2c, ina2xx_id);
994
995static const struct of_device_id __maybe_unused ina2xx_of_match[] = {
996 {
997 .compatible = "silergy,sy24655",
998 .data = (void *)sy24655
999 },
1000 {
1001 .compatible = "ti,ina219",
1002 .data = (void *)ina219
1003 },
1004 {
1005 .compatible = "ti,ina220",
1006 .data = (void *)ina219
1007 },
1008 {
1009 .compatible = "ti,ina226",
1010 .data = (void *)ina226
1011 },
1012 {
1013 .compatible = "ti,ina230",
1014 .data = (void *)ina226
1015 },
1016 {
1017 .compatible = "ti,ina231",
1018 .data = (void *)ina226
1019 },
1020 {
1021 .compatible = "ti,ina260",
1022 .data = (void *)ina260
1023 },
1024 { }
1025};
1026MODULE_DEVICE_TABLE(of, ina2xx_of_match);
1027
1028static struct i2c_driver ina2xx_driver = {
1029 .driver = {
1030 .name = "ina2xx",
1031 .of_match_table = of_match_ptr(ina2xx_of_match),
1032 },
1033 .probe = ina2xx_probe,
1034 .id_table = ina2xx_id,
1035};
1036
1037module_i2c_driver(ina2xx_driver);
1038
1039MODULE_AUTHOR("Lothar Felten <l-felten@ti.com>");
1040MODULE_DESCRIPTION("ina2xx driver");
1041MODULE_LICENSE("GPL");
1// SPDX-License-Identifier: GPL-2.0-only
2/*
3 * Driver for Texas Instruments INA219, INA226 power monitor chips
4 *
5 * INA219:
6 * Zero Drift Bi-Directional Current/Power Monitor with I2C Interface
7 * Datasheet: https://www.ti.com/product/ina219
8 *
9 * INA220:
10 * Bi-Directional Current/Power Monitor with I2C Interface
11 * Datasheet: https://www.ti.com/product/ina220
12 *
13 * INA226:
14 * Bi-Directional Current/Power Monitor with I2C Interface
15 * Datasheet: https://www.ti.com/product/ina226
16 *
17 * INA230:
18 * Bi-directional Current/Power Monitor with I2C Interface
19 * Datasheet: https://www.ti.com/product/ina230
20 *
21 * Copyright (C) 2012 Lothar Felten <lothar.felten@gmail.com>
22 * Thanks to Jan Volkering
23 */
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>
33#include <linux/jiffies.h>
34#include <linux/of_device.h>
35#include <linux/of.h>
36#include <linux/delay.h>
37#include <linux/util_macros.h>
38#include <linux/regmap.h>
39
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
55/* 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
67#define INA2XX_MAX_DELAY 69 /* worst case delay in ms */
68
69#define INA2XX_RSHUNT_DEFAULT 10000
70
71/* 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
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
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
88/* 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
97static struct regmap_config ina2xx_regmap_config = {
98 .reg_bits = 8,
99 .val_bits = 16,
100};
101
102enum ina2xx_ids { ina219, ina226 };
103
104struct ina2xx_config {
105 u16 config_default;
106 int calibration_value;
107 int registers;
108 int shunt_div;
109 int bus_voltage_shift;
110 int bus_voltage_lsb; /* uV */
111 int power_lsb_factor;
112};
113
114struct ina2xx_data {
115 const struct ina2xx_config *config;
116
117 long rshunt;
118 long current_lsb_uA;
119 long power_lsb_uW;
120 struct mutex config_lock;
121 struct regmap *regmap;
122
123 const struct attribute_group *groups[INA2XX_MAX_ATTRIBUTE_GROUPS];
124};
125
126static const struct ina2xx_config ina2xx_config[] = {
127 [ina219] = {
128 .config_default = INA219_CONFIG_DEFAULT,
129 .calibration_value = 4096,
130 .registers = INA219_REGISTERS,
131 .shunt_div = 100,
132 .bus_voltage_shift = 3,
133 .bus_voltage_lsb = 4000,
134 .power_lsb_factor = 20,
135 },
136 [ina226] = {
137 .config_default = INA226_CONFIG_DEFAULT,
138 .calibration_value = 2048,
139 .registers = INA226_REGISTERS,
140 .shunt_div = 400,
141 .bus_voltage_shift = 0,
142 .bus_voltage_lsb = 1250,
143 .power_lsb_factor = 25,
144 },
145};
146
147/*
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.
152 */
153static const int ina226_avg_tab[] = { 1, 4, 16, 64, 128, 256, 512, 1024 };
154
155static 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
166/*
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)
171{
172 int avg, avg_bits;
173
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));
178
179 return INA226_SHIFT_AVG(avg_bits);
180}
181
182/*
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 */
188static int ina2xx_calibrate(struct ina2xx_data *data)
189{
190 return regmap_write(data->regmap, INA2XX_CALIBRATION,
191 data->config->calibration_value);
192}
193
194/*
195 * Initialize the configuration and calibration registers.
196 */
197static int ina2xx_init(struct ina2xx_data *data)
198{
199 int ret = regmap_write(data->regmap, INA2XX_CONFIG,
200 data->config->config_default);
201 if (ret < 0)
202 return ret;
203
204 return ina2xx_calibrate(data);
205}
206
207static int ina2xx_read_reg(struct device *dev, int reg, unsigned int *regval)
208{
209 struct ina2xx_data *data = dev_get_drvdata(dev);
210 int ret, retry;
211
212 dev_dbg(dev, "Starting register %d read\n", reg);
213
214 for (retry = 5; retry; retry--) {
215
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);
221
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.
227 * We do that extra read of the calibration register if there
228 * is some hint of a chip reset.
229 */
230 if (*regval == 0) {
231 unsigned int cal;
232
233 ret = regmap_read(data->regmap, INA2XX_CALIBRATION,
234 &cal);
235 if (ret < 0)
236 return ret;
237
238 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 }
252 }
253 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
265static int ina2xx_get_value(struct ina2xx_data *data, u8 reg,
266 unsigned int regval)
267{
268 int val;
269
270 switch (reg) {
271 case INA2XX_SHUNT_VOLTAGE:
272 /* signed register */
273 val = DIV_ROUND_CLOSEST((s16)regval, data->config->shunt_div);
274 break;
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);
279 break;
280 case INA2XX_POWER:
281 val = regval * data->power_lsb_uW;
282 break;
283 case INA2XX_CURRENT:
284 /* signed register, result in mA */
285 val = (s16)regval * data->current_lsb_uA;
286 val = DIV_ROUND_CLOSEST(val, 1000);
287 break;
288 case INA2XX_CALIBRATION:
289 val = regval;
290 break;
291 default:
292 /* programmer goofed */
293 WARN_ON_ONCE(1);
294 val = 0;
295 break;
296 }
297
298 return val;
299}
300
301static ssize_t ina2xx_value_show(struct device *dev,
302 struct device_attribute *da, char *buf)
303{
304 struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
305 struct ina2xx_data *data = dev_get_drvdata(dev);
306 unsigned int regval;
307
308 int err = ina2xx_read_reg(dev, attr->index, ®val);
309
310 if (err < 0)
311 return err;
312
313 return snprintf(buf, PAGE_SIZE, "%d\n",
314 ina2xx_get_value(data, attr->index, regval));
315}
316
317static 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, ®val);
379 if (ret)
380 goto abort;
381
382 if (regval & BIT(attr->index)) {
383 ret = regmap_read(data->regmap, INA226_ALERT_LIMIT, ®val);
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, ®val);
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
456/*
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
479static ssize_t ina2xx_shunt_show(struct device *dev,
480 struct device_attribute *da, char *buf)
481{
482 struct ina2xx_data *data = dev_get_drvdata(dev);
483
484 return snprintf(buf, PAGE_SIZE, "%li\n", data->rshunt);
485}
486
487static ssize_t ina2xx_shunt_store(struct device *dev,
488 struct device_attribute *da,
489 const char *buf, size_t count)
490{
491 unsigned long val;
492 int status;
493 struct ina2xx_data *data = dev_get_drvdata(dev);
494
495 status = kstrtoul(buf, 10, &val);
496 if (status < 0)
497 return status;
498
499 status = ina2xx_set_shunt(data, val);
500 if (status < 0)
501 return status;
502 return count;
503}
504
505static ssize_t ina226_interval_store(struct device *dev,
506 struct device_attribute *da,
507 const char *buf, size_t count)
508{
509 struct ina2xx_data *data = dev_get_drvdata(dev);
510 unsigned long val;
511 int status;
512
513 status = kstrtoul(buf, 10, &val);
514 if (status < 0)
515 return status;
516
517 if (val > INT_MAX || val == 0)
518 return -EINVAL;
519
520 status = regmap_update_bits(data->regmap, INA2XX_CONFIG,
521 INA226_AVG_RD_MASK,
522 ina226_interval_to_reg(val));
523 if (status < 0)
524 return status;
525
526 return count;
527}
528
529static ssize_t ina226_interval_show(struct device *dev,
530 struct device_attribute *da, char *buf)
531{
532 struct ina2xx_data *data = dev_get_drvdata(dev);
533 int status;
534 unsigned int regval;
535
536 status = regmap_read(data->regmap, INA2XX_CONFIG, ®val);
537 if (status)
538 return status;
539
540 return snprintf(buf, PAGE_SIZE, "%d\n", ina226_reg_to_interval(regval));
541}
542
543/* shunt voltage */
544static SENSOR_DEVICE_ATTR_RO(in0_input, ina2xx_value, INA2XX_SHUNT_VOLTAGE);
545/* 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);
554
555/* bus voltage */
556static SENSOR_DEVICE_ATTR_RO(in1_input, ina2xx_value, INA2XX_BUS_VOLTAGE);
557/* 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);
566
567/* calculated current */
568static SENSOR_DEVICE_ATTR_RO(curr1_input, ina2xx_value, INA2XX_CURRENT);
569
570/* calculated power */
571static SENSOR_DEVICE_ATTR_RO(power1_input, ina2xx_value, INA2XX_POWER);
572/* 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);
577
578/* shunt resistance */
579static SENSOR_DEVICE_ATTR_RW(shunt_resistor, ina2xx_shunt, INA2XX_CALIBRATION);
580
581/* update interval (ina226 only) */
582static SENSOR_DEVICE_ATTR_RW(update_interval, ina226_interval, 0);
583
584/* pointers to created device attributes */
585static 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,
591 NULL,
592};
593
594static const struct attribute_group ina2xx_group = {
595 .attrs = ina2xx_attrs,
596};
597
598static 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,
610 NULL,
611};
612
613static const struct attribute_group ina226_group = {
614 .attrs = ina226_attrs,
615};
616
617static int ina2xx_probe(struct i2c_client *client,
618 const struct i2c_device_id *id)
619{
620 struct device *dev = &client->dev;
621 struct ina2xx_data *data;
622 struct device *hwmon_dev;
623 u32 val;
624 int ret, group = 0;
625 enum ina2xx_ids chip;
626
627 if (client->dev.of_node)
628 chip = (enum ina2xx_ids)of_device_get_match_data(&client->dev);
629 else
630 chip = id->driver_data;
631
632 data = devm_kzalloc(dev, sizeof(*data), GFP_KERNEL);
633 if (!data)
634 return -ENOMEM;
635
636 /* set the device type */
637 data->config = &ina2xx_config[chip];
638 mutex_init(&data->config_lock);
639
640 if (of_property_read_u32(dev->of_node, "shunt-resistor", &val) < 0) {
641 struct ina2xx_platform_data *pdata = dev_get_platdata(dev);
642
643 if (pdata)
644 val = pdata->shunt_uohms;
645 else
646 val = INA2XX_RSHUNT_DEFAULT;
647 }
648
649 ina2xx_set_shunt(data, val);
650
651 ina2xx_regmap_config.max_register = data->config->registers;
652
653 data->regmap = devm_regmap_init_i2c(client, &ina2xx_regmap_config);
654 if (IS_ERR(data->regmap)) {
655 dev_err(dev, "failed to allocate register map\n");
656 return PTR_ERR(data->regmap);
657 }
658
659 ret = ina2xx_init(data);
660 if (ret < 0) {
661 dev_err(dev, "error configuring the device: %d\n", ret);
662 return -ENODEV;
663 }
664
665 data->groups[group++] = &ina2xx_group;
666 if (chip == ina226)
667 data->groups[group++] = &ina226_group;
668
669 hwmon_dev = devm_hwmon_device_register_with_groups(dev, client->name,
670 data, data->groups);
671 if (IS_ERR(hwmon_dev))
672 return PTR_ERR(hwmon_dev);
673
674 dev_info(dev, "power monitor %s (Rshunt = %li uOhm)\n",
675 client->name, data->rshunt);
676
677 return 0;
678}
679
680static const struct i2c_device_id ina2xx_id[] = {
681 { "ina219", ina219 },
682 { "ina220", ina219 },
683 { "ina226", ina226 },
684 { "ina230", ina226 },
685 { "ina231", ina226 },
686 { }
687};
688MODULE_DEVICE_TABLE(i2c, ina2xx_id);
689
690static const struct of_device_id __maybe_unused ina2xx_of_match[] = {
691 {
692 .compatible = "ti,ina219",
693 .data = (void *)ina219
694 },
695 {
696 .compatible = "ti,ina220",
697 .data = (void *)ina219
698 },
699 {
700 .compatible = "ti,ina226",
701 .data = (void *)ina226
702 },
703 {
704 .compatible = "ti,ina230",
705 .data = (void *)ina226
706 },
707 {
708 .compatible = "ti,ina231",
709 .data = (void *)ina226
710 },
711 { },
712};
713MODULE_DEVICE_TABLE(of, ina2xx_of_match);
714
715static struct i2c_driver ina2xx_driver = {
716 .driver = {
717 .name = "ina2xx",
718 .of_match_table = of_match_ptr(ina2xx_of_match),
719 },
720 .probe = ina2xx_probe,
721 .id_table = ina2xx_id,
722};
723
724module_i2c_driver(ina2xx_driver);
725
726MODULE_AUTHOR("Lothar Felten <l-felten@ti.com>");
727MODULE_DESCRIPTION("ina2xx driver");
728MODULE_LICENSE("GPL");