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/*
2 * Driver for Texas Instruments INA219, INA226 power monitor chips
3 *
4 * INA219:
5 * Zero Drift Bi-Directional Current/Power Monitor with I2C Interface
6 * Datasheet: http://www.ti.com/product/ina219
7 *
8 * INA220:
9 * Bi-Directional Current/Power Monitor with I2C Interface
10 * Datasheet: http://www.ti.com/product/ina220
11 *
12 * INA226:
13 * Bi-Directional Current/Power Monitor with I2C Interface
14 * Datasheet: http://www.ti.com/product/ina226
15 *
16 * INA230:
17 * Bi-directional Current/Power Monitor with I2C Interface
18 * Datasheet: http://www.ti.com/product/ina230
19 *
20 * Copyright (C) 2012 Lothar Felten <l-felten@ti.com>
21 * Thanks to Jan Volkering
22 *
23 * This program is free software; you can redistribute it and/or modify
24 * it under the terms of the GNU General Public License as published by
25 * the Free Software Foundation; version 2 of the License.
26 */
27
28#include <linux/kernel.h>
29#include <linux/module.h>
30#include <linux/init.h>
31#include <linux/err.h>
32#include <linux/slab.h>
33#include <linux/i2c.h>
34#include <linux/hwmon.h>
35#include <linux/hwmon-sysfs.h>
36#include <linux/jiffies.h>
37#include <linux/of_device.h>
38#include <linux/of.h>
39#include <linux/delay.h>
40#include <linux/util_macros.h>
41#include <linux/regmap.h>
42
43#include <linux/platform_data/ina2xx.h>
44
45/* common register definitions */
46#define INA2XX_CONFIG 0x00
47#define INA2XX_SHUNT_VOLTAGE 0x01 /* readonly */
48#define INA2XX_BUS_VOLTAGE 0x02 /* readonly */
49#define INA2XX_POWER 0x03 /* readonly */
50#define INA2XX_CURRENT 0x04 /* readonly */
51#define INA2XX_CALIBRATION 0x05
52
53/* INA226 register definitions */
54#define INA226_MASK_ENABLE 0x06
55#define INA226_ALERT_LIMIT 0x07
56#define INA226_DIE_ID 0xFF
57
58/* register count */
59#define INA219_REGISTERS 6
60#define INA226_REGISTERS 8
61
62#define INA2XX_MAX_REGISTERS 8
63
64/* settings - depend on use case */
65#define INA219_CONFIG_DEFAULT 0x399F /* PGA=8 */
66#define INA226_CONFIG_DEFAULT 0x4527 /* averages=16 */
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
74/* bit mask for reading the averaging setting in the configuration register */
75#define INA226_AVG_RD_MASK 0x0E00
76
77#define INA226_READ_AVG(reg) (((reg) & INA226_AVG_RD_MASK) >> 9)
78#define INA226_SHIFT_AVG(val) ((val) << 9)
79
80/* common attrs, ina226 attrs and NULL */
81#define INA2XX_MAX_ATTRIBUTE_GROUPS 3
82
83/*
84 * Both bus voltage and shunt voltage conversion times for ina226 are set
85 * to 0b0100 on POR, which translates to 2200 microseconds in total.
86 */
87#define INA226_TOTAL_CONV_TIME_DEFAULT 2200
88
89static struct regmap_config ina2xx_regmap_config = {
90 .reg_bits = 8,
91 .val_bits = 16,
92};
93
94enum ina2xx_ids { ina219, ina226 };
95
96struct ina2xx_config {
97 u16 config_default;
98 int calibration_value;
99 int registers;
100 int shunt_div;
101 int bus_voltage_shift;
102 int bus_voltage_lsb; /* uV */
103 int power_lsb_factor;
104};
105
106struct ina2xx_data {
107 const struct ina2xx_config *config;
108
109 long rshunt;
110 long current_lsb_uA;
111 long power_lsb_uW;
112 struct mutex config_lock;
113 struct regmap *regmap;
114
115 const struct attribute_group *groups[INA2XX_MAX_ATTRIBUTE_GROUPS];
116};
117
118static const struct ina2xx_config ina2xx_config[] = {
119 [ina219] = {
120 .config_default = INA219_CONFIG_DEFAULT,
121 .calibration_value = 4096,
122 .registers = INA219_REGISTERS,
123 .shunt_div = 100,
124 .bus_voltage_shift = 3,
125 .bus_voltage_lsb = 4000,
126 .power_lsb_factor = 20,
127 },
128 [ina226] = {
129 .config_default = INA226_CONFIG_DEFAULT,
130 .calibration_value = 2048,
131 .registers = INA226_REGISTERS,
132 .shunt_div = 400,
133 .bus_voltage_shift = 0,
134 .bus_voltage_lsb = 1250,
135 .power_lsb_factor = 25,
136 },
137};
138
139/*
140 * Available averaging rates for ina226. The indices correspond with
141 * the bit values expected by the chip (according to the ina226 datasheet,
142 * table 3 AVG bit settings, found at
143 * http://www.ti.com/lit/ds/symlink/ina226.pdf.
144 */
145static const int ina226_avg_tab[] = { 1, 4, 16, 64, 128, 256, 512, 1024 };
146
147static int ina226_reg_to_interval(u16 config)
148{
149 int avg = ina226_avg_tab[INA226_READ_AVG(config)];
150
151 /*
152 * Multiply the total conversion time by the number of averages.
153 * Return the result in milliseconds.
154 */
155 return DIV_ROUND_CLOSEST(avg * INA226_TOTAL_CONV_TIME_DEFAULT, 1000);
156}
157
158/*
159 * Return the new, shifted AVG field value of CONFIG register,
160 * to use with regmap_update_bits
161 */
162static u16 ina226_interval_to_reg(int interval)
163{
164 int avg, avg_bits;
165
166 avg = DIV_ROUND_CLOSEST(interval * 1000,
167 INA226_TOTAL_CONV_TIME_DEFAULT);
168 avg_bits = find_closest(avg, ina226_avg_tab,
169 ARRAY_SIZE(ina226_avg_tab));
170
171 return INA226_SHIFT_AVG(avg_bits);
172}
173
174/*
175 * Calibration register is set to the best value, which eliminates
176 * truncation errors on calculating current register in hardware.
177 * According to datasheet (eq. 3) the best values are 2048 for
178 * ina226 and 4096 for ina219. They are hardcoded as calibration_value.
179 */
180static int ina2xx_calibrate(struct ina2xx_data *data)
181{
182 return regmap_write(data->regmap, INA2XX_CALIBRATION,
183 data->config->calibration_value);
184}
185
186/*
187 * Initialize the configuration and calibration registers.
188 */
189static int ina2xx_init(struct ina2xx_data *data)
190{
191 int ret = regmap_write(data->regmap, INA2XX_CONFIG,
192 data->config->config_default);
193 if (ret < 0)
194 return ret;
195
196 return ina2xx_calibrate(data);
197}
198
199static int ina2xx_read_reg(struct device *dev, int reg, unsigned int *regval)
200{
201 struct ina2xx_data *data = dev_get_drvdata(dev);
202 int ret, retry;
203
204 dev_dbg(dev, "Starting register %d read\n", reg);
205
206 for (retry = 5; retry; retry--) {
207
208 ret = regmap_read(data->regmap, reg, regval);
209 if (ret < 0)
210 return ret;
211
212 dev_dbg(dev, "read %d, val = 0x%04x\n", reg, *regval);
213
214 /*
215 * If the current value in the calibration register is 0, the
216 * power and current registers will also remain at 0. In case
217 * the chip has been reset let's check the calibration
218 * register and reinitialize if needed.
219 * We do that extra read of the calibration register if there
220 * is some hint of a chip reset.
221 */
222 if (*regval == 0) {
223 unsigned int cal;
224
225 ret = regmap_read(data->regmap, INA2XX_CALIBRATION,
226 &cal);
227 if (ret < 0)
228 return ret;
229
230 if (cal == 0) {
231 dev_warn(dev, "chip not calibrated, reinitializing\n");
232
233 ret = ina2xx_init(data);
234 if (ret < 0)
235 return ret;
236 /*
237 * Let's make sure the power and current
238 * registers have been updated before trying
239 * again.
240 */
241 msleep(INA2XX_MAX_DELAY);
242 continue;
243 }
244 }
245 return 0;
246 }
247
248 /*
249 * If we're here then although all write operations succeeded, the
250 * chip still returns 0 in the calibration register. Nothing more we
251 * can do here.
252 */
253 dev_err(dev, "unable to reinitialize the chip\n");
254 return -ENODEV;
255}
256
257static int ina2xx_get_value(struct ina2xx_data *data, u8 reg,
258 unsigned int regval)
259{
260 int val;
261
262 switch (reg) {
263 case INA2XX_SHUNT_VOLTAGE:
264 /* signed register */
265 val = DIV_ROUND_CLOSEST((s16)regval, data->config->shunt_div);
266 break;
267 case INA2XX_BUS_VOLTAGE:
268 val = (regval >> data->config->bus_voltage_shift)
269 * data->config->bus_voltage_lsb;
270 val = DIV_ROUND_CLOSEST(val, 1000);
271 break;
272 case INA2XX_POWER:
273 val = regval * data->power_lsb_uW;
274 break;
275 case INA2XX_CURRENT:
276 /* signed register, result in mA */
277 val = regval * data->current_lsb_uA;
278 val = DIV_ROUND_CLOSEST(val, 1000);
279 break;
280 case INA2XX_CALIBRATION:
281 val = regval;
282 break;
283 default:
284 /* programmer goofed */
285 WARN_ON_ONCE(1);
286 val = 0;
287 break;
288 }
289
290 return val;
291}
292
293static ssize_t ina2xx_show_value(struct device *dev,
294 struct device_attribute *da, char *buf)
295{
296 struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
297 struct ina2xx_data *data = dev_get_drvdata(dev);
298 unsigned int regval;
299
300 int err = ina2xx_read_reg(dev, attr->index, ®val);
301
302 if (err < 0)
303 return err;
304
305 return snprintf(buf, PAGE_SIZE, "%d\n",
306 ina2xx_get_value(data, attr->index, regval));
307}
308
309/*
310 * In order to keep calibration register value fixed, the product
311 * of current_lsb and shunt_resistor should also be fixed and equal
312 * to shunt_voltage_lsb = 1 / shunt_div multiplied by 10^9 in order
313 * to keep the scale.
314 */
315static int ina2xx_set_shunt(struct ina2xx_data *data, long val)
316{
317 unsigned int dividend = DIV_ROUND_CLOSEST(1000000000,
318 data->config->shunt_div);
319 if (val <= 0 || val > dividend)
320 return -EINVAL;
321
322 mutex_lock(&data->config_lock);
323 data->rshunt = val;
324 data->current_lsb_uA = DIV_ROUND_CLOSEST(dividend, val);
325 data->power_lsb_uW = data->config->power_lsb_factor *
326 data->current_lsb_uA;
327 mutex_unlock(&data->config_lock);
328
329 return 0;
330}
331
332static ssize_t ina2xx_store_shunt(struct device *dev,
333 struct device_attribute *da,
334 const char *buf, size_t count)
335{
336 unsigned long val;
337 int status;
338 struct ina2xx_data *data = dev_get_drvdata(dev);
339
340 status = kstrtoul(buf, 10, &val);
341 if (status < 0)
342 return status;
343
344 status = ina2xx_set_shunt(data, val);
345 if (status < 0)
346 return status;
347 return count;
348}
349
350static ssize_t ina226_set_interval(struct device *dev,
351 struct device_attribute *da,
352 const char *buf, size_t count)
353{
354 struct ina2xx_data *data = dev_get_drvdata(dev);
355 unsigned long val;
356 int status;
357
358 status = kstrtoul(buf, 10, &val);
359 if (status < 0)
360 return status;
361
362 if (val > INT_MAX || val == 0)
363 return -EINVAL;
364
365 status = regmap_update_bits(data->regmap, INA2XX_CONFIG,
366 INA226_AVG_RD_MASK,
367 ina226_interval_to_reg(val));
368 if (status < 0)
369 return status;
370
371 return count;
372}
373
374static ssize_t ina226_show_interval(struct device *dev,
375 struct device_attribute *da, char *buf)
376{
377 struct ina2xx_data *data = dev_get_drvdata(dev);
378 int status;
379 unsigned int regval;
380
381 status = regmap_read(data->regmap, INA2XX_CONFIG, ®val);
382 if (status)
383 return status;
384
385 return snprintf(buf, PAGE_SIZE, "%d\n", ina226_reg_to_interval(regval));
386}
387
388/* shunt voltage */
389static SENSOR_DEVICE_ATTR(in0_input, S_IRUGO, ina2xx_show_value, NULL,
390 INA2XX_SHUNT_VOLTAGE);
391
392/* bus voltage */
393static SENSOR_DEVICE_ATTR(in1_input, S_IRUGO, ina2xx_show_value, NULL,
394 INA2XX_BUS_VOLTAGE);
395
396/* calculated current */
397static SENSOR_DEVICE_ATTR(curr1_input, S_IRUGO, ina2xx_show_value, NULL,
398 INA2XX_CURRENT);
399
400/* calculated power */
401static SENSOR_DEVICE_ATTR(power1_input, S_IRUGO, ina2xx_show_value, NULL,
402 INA2XX_POWER);
403
404/* shunt resistance */
405static SENSOR_DEVICE_ATTR(shunt_resistor, S_IRUGO | S_IWUSR,
406 ina2xx_show_value, ina2xx_store_shunt,
407 INA2XX_CALIBRATION);
408
409/* update interval (ina226 only) */
410static SENSOR_DEVICE_ATTR(update_interval, S_IRUGO | S_IWUSR,
411 ina226_show_interval, ina226_set_interval, 0);
412
413/* pointers to created device attributes */
414static struct attribute *ina2xx_attrs[] = {
415 &sensor_dev_attr_in0_input.dev_attr.attr,
416 &sensor_dev_attr_in1_input.dev_attr.attr,
417 &sensor_dev_attr_curr1_input.dev_attr.attr,
418 &sensor_dev_attr_power1_input.dev_attr.attr,
419 &sensor_dev_attr_shunt_resistor.dev_attr.attr,
420 NULL,
421};
422
423static const struct attribute_group ina2xx_group = {
424 .attrs = ina2xx_attrs,
425};
426
427static struct attribute *ina226_attrs[] = {
428 &sensor_dev_attr_update_interval.dev_attr.attr,
429 NULL,
430};
431
432static const struct attribute_group ina226_group = {
433 .attrs = ina226_attrs,
434};
435
436static int ina2xx_probe(struct i2c_client *client,
437 const struct i2c_device_id *id)
438{
439 struct device *dev = &client->dev;
440 struct ina2xx_data *data;
441 struct device *hwmon_dev;
442 u32 val;
443 int ret, group = 0;
444 enum ina2xx_ids chip;
445
446 if (client->dev.of_node)
447 chip = (enum ina2xx_ids)of_device_get_match_data(&client->dev);
448 else
449 chip = id->driver_data;
450
451 data = devm_kzalloc(dev, sizeof(*data), GFP_KERNEL);
452 if (!data)
453 return -ENOMEM;
454
455 /* set the device type */
456 data->config = &ina2xx_config[chip];
457 mutex_init(&data->config_lock);
458
459 if (of_property_read_u32(dev->of_node, "shunt-resistor", &val) < 0) {
460 struct ina2xx_platform_data *pdata = dev_get_platdata(dev);
461
462 if (pdata)
463 val = pdata->shunt_uohms;
464 else
465 val = INA2XX_RSHUNT_DEFAULT;
466 }
467
468 ina2xx_set_shunt(data, val);
469
470 ina2xx_regmap_config.max_register = data->config->registers;
471
472 data->regmap = devm_regmap_init_i2c(client, &ina2xx_regmap_config);
473 if (IS_ERR(data->regmap)) {
474 dev_err(dev, "failed to allocate register map\n");
475 return PTR_ERR(data->regmap);
476 }
477
478 ret = ina2xx_init(data);
479 if (ret < 0) {
480 dev_err(dev, "error configuring the device: %d\n", ret);
481 return -ENODEV;
482 }
483
484 data->groups[group++] = &ina2xx_group;
485 if (id->driver_data == ina226)
486 data->groups[group++] = &ina226_group;
487
488 hwmon_dev = devm_hwmon_device_register_with_groups(dev, client->name,
489 data, data->groups);
490 if (IS_ERR(hwmon_dev))
491 return PTR_ERR(hwmon_dev);
492
493 dev_info(dev, "power monitor %s (Rshunt = %li uOhm)\n",
494 id->name, data->rshunt);
495
496 return 0;
497}
498
499static const struct i2c_device_id ina2xx_id[] = {
500 { "ina219", ina219 },
501 { "ina220", ina219 },
502 { "ina226", ina226 },
503 { "ina230", ina226 },
504 { "ina231", ina226 },
505 { }
506};
507MODULE_DEVICE_TABLE(i2c, ina2xx_id);
508
509static const struct of_device_id ina2xx_of_match[] = {
510 {
511 .compatible = "ti,ina219",
512 .data = (void *)ina219
513 },
514 {
515 .compatible = "ti,ina220",
516 .data = (void *)ina219
517 },
518 {
519 .compatible = "ti,ina226",
520 .data = (void *)ina226
521 },
522 {
523 .compatible = "ti,ina230",
524 .data = (void *)ina226
525 },
526 {
527 .compatible = "ti,ina231",
528 .data = (void *)ina226
529 },
530 { },
531};
532MODULE_DEVICE_TABLE(of, ina2xx_of_match);
533
534static struct i2c_driver ina2xx_driver = {
535 .driver = {
536 .name = "ina2xx",
537 .of_match_table = of_match_ptr(ina2xx_of_match),
538 },
539 .probe = ina2xx_probe,
540 .id_table = ina2xx_id,
541};
542
543module_i2c_driver(ina2xx_driver);
544
545MODULE_AUTHOR("Lothar Felten <l-felten@ti.com>");
546MODULE_DESCRIPTION("ina2xx driver");
547MODULE_LICENSE("GPL");