2 * INA2XX Current and Power Monitors
4 * Copyright 2015 Baylibre SAS.
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 as
8 * published by the Free Software Foundation.
10 * Based on linux/drivers/iio/adc/ad7291.c
11 * Copyright 2010-2011 Analog Devices Inc.
13 * Based on linux/drivers/hwmon/ina2xx.c
14 * Copyright 2012 Lothar Felten <l-felten@ti.com>
16 * Licensed under the GPL-2 or later.
18 * IIO driver for INA219-220-226-230-231
20 * Configurable 7-bit I2C slave address from 0x40 to 0x4F
23 #include <linux/delay.h>
24 #include <linux/i2c.h>
25 #include <linux/iio/iio.h>
26 #include <linux/iio/buffer.h>
27 #include <linux/iio/kfifo_buf.h>
28 #include <linux/iio/sysfs.h>
29 #include <linux/kthread.h>
30 #include <linux/module.h>
31 #include <linux/of_device.h>
32 #include <linux/regmap.h>
33 #include <linux/sched/task.h>
34 #include <linux/util_macros.h>
36 #include <linux/platform_data/ina2xx.h>
38 /* INA2XX registers definition */
39 #define INA2XX_CONFIG 0x00
40 #define INA2XX_SHUNT_VOLTAGE 0x01 /* readonly */
41 #define INA2XX_BUS_VOLTAGE 0x02 /* readonly */
42 #define INA2XX_POWER 0x03 /* readonly */
43 #define INA2XX_CURRENT 0x04 /* readonly */
44 #define INA2XX_CALIBRATION 0x05
46 #define INA226_MASK_ENABLE 0x06
47 #define INA226_CVRF BIT(3)
49 #define INA2XX_MAX_REGISTERS 8
51 /* settings - depend on use case */
52 #define INA219_CONFIG_DEFAULT 0x399F /* PGA=1/8, BRNG=32V */
53 #define INA219_DEFAULT_IT 532
54 #define INA219_DEFAULT_BRNG 1 /* 32V */
55 #define INA219_DEFAULT_PGA 125 /* 1000/8 */
56 #define INA226_CONFIG_DEFAULT 0x4327
57 #define INA226_DEFAULT_AVG 4
58 #define INA226_DEFAULT_IT 1110
60 #define INA2XX_RSHUNT_DEFAULT 10000
63 * bit masks for reading the settings in the configuration register
64 * FIXME: use regmap_fields.
66 #define INA2XX_MODE_MASK GENMASK(3, 0)
68 /* Gain for VShunt: 1/8 (default), 1/4, 1/2, 1 */
69 #define INA219_PGA_MASK GENMASK(12, 11)
70 #define INA219_SHIFT_PGA(val) ((val) << 11)
72 /* VBus range: 32V (default), 16V */
73 #define INA219_BRNG_MASK BIT(13)
74 #define INA219_SHIFT_BRNG(val) ((val) << 13)
76 /* Averaging for VBus/VShunt/Power */
77 #define INA226_AVG_MASK GENMASK(11, 9)
78 #define INA226_SHIFT_AVG(val) ((val) << 9)
80 /* Integration time for VBus */
81 #define INA219_ITB_MASK GENMASK(10, 7)
82 #define INA219_SHIFT_ITB(val) ((val) << 7)
83 #define INA226_ITB_MASK GENMASK(8, 6)
84 #define INA226_SHIFT_ITB(val) ((val) << 6)
86 /* Integration time for VShunt */
87 #define INA219_ITS_MASK GENMASK(6, 3)
88 #define INA219_SHIFT_ITS(val) ((val) << 3)
89 #define INA226_ITS_MASK GENMASK(5, 3)
90 #define INA226_SHIFT_ITS(val) ((val) << 3)
92 /* INA219 Bus voltage register, low bits are flags */
93 #define INA219_OVF BIT(0)
94 #define INA219_CNVR BIT(1)
95 #define INA219_BUS_VOLTAGE_SHIFT 3
97 /* Cosmetic macro giving the sampling period for a full P=UxI cycle */
98 #define SAMPLING_PERIOD(c) ((c->int_time_vbus + c->int_time_vshunt) \
101 static bool ina2xx_is_writeable_reg(struct device
*dev
, unsigned int reg
)
103 return (reg
== INA2XX_CONFIG
) || (reg
> INA2XX_CURRENT
);
106 static bool ina2xx_is_volatile_reg(struct device
*dev
, unsigned int reg
)
108 return (reg
!= INA2XX_CONFIG
);
111 static inline bool is_signed_reg(unsigned int reg
)
113 return (reg
== INA2XX_SHUNT_VOLTAGE
) || (reg
== INA2XX_CURRENT
);
116 static const struct regmap_config ina2xx_regmap_config
= {
119 .max_register
= INA2XX_MAX_REGISTERS
,
120 .writeable_reg
= ina2xx_is_writeable_reg
,
121 .volatile_reg
= ina2xx_is_volatile_reg
,
124 enum ina2xx_ids
{ ina219
, ina226
};
126 struct ina2xx_config
{
128 int calibration_value
;
129 int shunt_voltage_lsb
; /* nV */
130 int bus_voltage_shift
; /* position of lsb */
131 int bus_voltage_lsb
; /* uV */
132 /* fixed relation between current and power lsb, uW/uA */
133 int power_lsb_factor
;
134 enum ina2xx_ids chip_id
;
137 struct ina2xx_chip_info
{
138 struct regmap
*regmap
;
139 struct task_struct
*task
;
140 const struct ina2xx_config
*config
;
141 struct mutex state_lock
;
142 unsigned int shunt_resistor_uohm
;
144 int int_time_vbus
; /* Bus voltage integration time uS */
145 int int_time_vshunt
; /* Shunt voltage integration time uS */
146 int range_vbus
; /* Bus voltage maximum in V */
147 int pga_gain_vshunt
; /* Shunt voltage PGA gain */
148 bool allow_async_readout
;
151 static const struct ina2xx_config ina2xx_config
[] = {
153 .config_default
= INA219_CONFIG_DEFAULT
,
154 .calibration_value
= 4096,
155 .shunt_voltage_lsb
= 10000,
156 .bus_voltage_shift
= INA219_BUS_VOLTAGE_SHIFT
,
157 .bus_voltage_lsb
= 4000,
158 .power_lsb_factor
= 20,
162 .config_default
= INA226_CONFIG_DEFAULT
,
163 .calibration_value
= 2048,
164 .shunt_voltage_lsb
= 2500,
165 .bus_voltage_shift
= 0,
166 .bus_voltage_lsb
= 1250,
167 .power_lsb_factor
= 25,
172 static int ina2xx_read_raw(struct iio_dev
*indio_dev
,
173 struct iio_chan_spec
const *chan
,
174 int *val
, int *val2
, long mask
)
177 struct ina2xx_chip_info
*chip
= iio_priv(indio_dev
);
181 case IIO_CHAN_INFO_RAW
:
182 ret
= regmap_read(chip
->regmap
, chan
->address
, ®val
);
186 if (is_signed_reg(chan
->address
))
191 if (chan
->address
== INA2XX_BUS_VOLTAGE
)
192 *val
>>= chip
->config
->bus_voltage_shift
;
196 case IIO_CHAN_INFO_OVERSAMPLING_RATIO
:
200 case IIO_CHAN_INFO_INT_TIME
:
202 if (chan
->address
== INA2XX_SHUNT_VOLTAGE
)
203 *val2
= chip
->int_time_vshunt
;
205 *val2
= chip
->int_time_vbus
;
207 return IIO_VAL_INT_PLUS_MICRO
;
209 case IIO_CHAN_INFO_SAMP_FREQ
:
211 * Sample freq is read only, it is a consequence of
212 * 1/AVG*(CT_bus+CT_shunt).
214 *val
= DIV_ROUND_CLOSEST(1000000, SAMPLING_PERIOD(chip
));
218 case IIO_CHAN_INFO_SCALE
:
219 switch (chan
->address
) {
220 case INA2XX_SHUNT_VOLTAGE
:
221 /* processed (mV) = raw * lsb(nV) / 1000000 */
222 *val
= chip
->config
->shunt_voltage_lsb
;
224 return IIO_VAL_FRACTIONAL
;
226 case INA2XX_BUS_VOLTAGE
:
227 /* processed (mV) = raw * lsb (uV) / 1000 */
228 *val
= chip
->config
->bus_voltage_lsb
;
230 return IIO_VAL_FRACTIONAL
;
234 * processed (mA) = raw * current_lsb (mA)
235 * current_lsb (mA) = shunt_voltage_lsb (nV) /
236 * shunt_resistor (uOhm)
238 *val
= chip
->config
->shunt_voltage_lsb
;
239 *val2
= chip
->shunt_resistor_uohm
;
240 return IIO_VAL_FRACTIONAL
;
244 * processed (mW) = raw * power_lsb (mW)
245 * power_lsb (mW) = power_lsb_factor (mW/mA) *
248 *val
= chip
->config
->power_lsb_factor
*
249 chip
->config
->shunt_voltage_lsb
;
250 *val2
= chip
->shunt_resistor_uohm
;
251 return IIO_VAL_FRACTIONAL
;
255 case IIO_CHAN_INFO_HARDWAREGAIN
:
256 switch (chan
->address
) {
257 case INA2XX_SHUNT_VOLTAGE
:
258 *val
= chip
->pga_gain_vshunt
;
260 return IIO_VAL_FRACTIONAL
;
262 case INA2XX_BUS_VOLTAGE
:
263 *val
= chip
->range_vbus
== 32 ? 1 : 2;
273 * Available averaging rates for ina226. The indices correspond with
274 * the bit values expected by the chip (according to the ina226 datasheet,
275 * table 3 AVG bit settings, found at
276 * http://www.ti.com/lit/ds/symlink/ina226.pdf.
278 static const int ina226_avg_tab
[] = { 1, 4, 16, 64, 128, 256, 512, 1024 };
280 static int ina226_set_average(struct ina2xx_chip_info
*chip
, unsigned int val
,
281 unsigned int *config
)
285 if (val
> 1024 || val
< 1)
288 bits
= find_closest(val
, ina226_avg_tab
,
289 ARRAY_SIZE(ina226_avg_tab
));
291 chip
->avg
= ina226_avg_tab
[bits
];
293 *config
&= ~INA226_AVG_MASK
;
294 *config
|= INA226_SHIFT_AVG(bits
) & INA226_AVG_MASK
;
299 /* Conversion times in uS */
300 static const int ina226_conv_time_tab
[] = { 140, 204, 332, 588, 1100,
303 static int ina226_set_int_time_vbus(struct ina2xx_chip_info
*chip
,
304 unsigned int val_us
, unsigned int *config
)
308 if (val_us
> 8244 || val_us
< 140)
311 bits
= find_closest(val_us
, ina226_conv_time_tab
,
312 ARRAY_SIZE(ina226_conv_time_tab
));
314 chip
->int_time_vbus
= ina226_conv_time_tab
[bits
];
316 *config
&= ~INA226_ITB_MASK
;
317 *config
|= INA226_SHIFT_ITB(bits
) & INA226_ITB_MASK
;
322 static int ina226_set_int_time_vshunt(struct ina2xx_chip_info
*chip
,
323 unsigned int val_us
, unsigned int *config
)
327 if (val_us
> 8244 || val_us
< 140)
330 bits
= find_closest(val_us
, ina226_conv_time_tab
,
331 ARRAY_SIZE(ina226_conv_time_tab
));
333 chip
->int_time_vshunt
= ina226_conv_time_tab
[bits
];
335 *config
&= ~INA226_ITS_MASK
;
336 *config
|= INA226_SHIFT_ITS(bits
) & INA226_ITS_MASK
;
341 /* Conversion times in uS. */
342 static const int ina219_conv_time_tab_subsample
[] = { 84, 148, 276, 532 };
343 static const int ina219_conv_time_tab_average
[] = { 532, 1060, 2130, 4260,
344 8510, 17020, 34050, 68100};
346 static int ina219_lookup_int_time(unsigned int *val_us
, int *bits
)
348 if (*val_us
> 68100 || *val_us
< 84)
351 if (*val_us
<= 532) {
352 *bits
= find_closest(*val_us
, ina219_conv_time_tab_subsample
,
353 ARRAY_SIZE(ina219_conv_time_tab_subsample
));
354 *val_us
= ina219_conv_time_tab_subsample
[*bits
];
356 *bits
= find_closest(*val_us
, ina219_conv_time_tab_average
,
357 ARRAY_SIZE(ina219_conv_time_tab_average
));
358 *val_us
= ina219_conv_time_tab_average
[*bits
];
365 static int ina219_set_int_time_vbus(struct ina2xx_chip_info
*chip
,
366 unsigned int val_us
, unsigned int *config
)
369 unsigned int val_us_best
= val_us
;
371 ret
= ina219_lookup_int_time(&val_us_best
, &bits
);
375 chip
->int_time_vbus
= val_us_best
;
377 *config
&= ~INA219_ITB_MASK
;
378 *config
|= INA219_SHIFT_ITB(bits
) & INA219_ITB_MASK
;
383 static int ina219_set_int_time_vshunt(struct ina2xx_chip_info
*chip
,
384 unsigned int val_us
, unsigned int *config
)
387 unsigned int val_us_best
= val_us
;
389 ret
= ina219_lookup_int_time(&val_us_best
, &bits
);
393 chip
->int_time_vshunt
= val_us_best
;
395 *config
&= ~INA219_ITS_MASK
;
396 *config
|= INA219_SHIFT_ITS(bits
) & INA219_ITS_MASK
;
401 static const int ina219_vbus_range_tab
[] = { 1, 2 };
402 static int ina219_set_vbus_range_denom(struct ina2xx_chip_info
*chip
,
404 unsigned int *config
)
407 chip
->range_vbus
= 32;
409 chip
->range_vbus
= 16;
413 *config
&= ~INA219_BRNG_MASK
;
414 *config
|= INA219_SHIFT_BRNG(range
== 1 ? 1 : 0) & INA219_BRNG_MASK
;
419 static const int ina219_vshunt_gain_tab
[] = { 125, 250, 500, 1000 };
420 static const int ina219_vshunt_gain_frac
[] = {
421 125, 1000, 250, 1000, 500, 1000, 1000, 1000 };
423 static int ina219_set_vshunt_pga_gain(struct ina2xx_chip_info
*chip
,
425 unsigned int *config
)
429 if (gain
< 125 || gain
> 1000)
432 bits
= find_closest(gain
, ina219_vshunt_gain_tab
,
433 ARRAY_SIZE(ina219_vshunt_gain_tab
));
435 chip
->pga_gain_vshunt
= ina219_vshunt_gain_tab
[bits
];
438 *config
&= ~INA219_PGA_MASK
;
439 *config
|= INA219_SHIFT_PGA(bits
) & INA219_PGA_MASK
;
444 static int ina2xx_read_avail(struct iio_dev
*indio_dev
,
445 struct iio_chan_spec
const *chan
,
446 const int **vals
, int *type
, int *length
,
450 case IIO_CHAN_INFO_HARDWAREGAIN
:
451 switch (chan
->address
) {
452 case INA2XX_SHUNT_VOLTAGE
:
453 *type
= IIO_VAL_FRACTIONAL
;
454 *length
= sizeof(ina219_vshunt_gain_frac
) / sizeof(int);
455 *vals
= ina219_vshunt_gain_frac
;
456 return IIO_AVAIL_LIST
;
458 case INA2XX_BUS_VOLTAGE
:
460 *length
= sizeof(ina219_vbus_range_tab
) / sizeof(int);
461 *vals
= ina219_vbus_range_tab
;
462 return IIO_AVAIL_LIST
;
469 static int ina2xx_write_raw(struct iio_dev
*indio_dev
,
470 struct iio_chan_spec
const *chan
,
471 int val
, int val2
, long mask
)
473 struct ina2xx_chip_info
*chip
= iio_priv(indio_dev
);
474 unsigned int config
, tmp
;
477 if (iio_buffer_enabled(indio_dev
))
480 mutex_lock(&chip
->state_lock
);
482 ret
= regmap_read(chip
->regmap
, INA2XX_CONFIG
, &config
);
489 case IIO_CHAN_INFO_OVERSAMPLING_RATIO
:
490 ret
= ina226_set_average(chip
, val
, &tmp
);
493 case IIO_CHAN_INFO_INT_TIME
:
494 if (chip
->config
->chip_id
== ina226
) {
495 if (chan
->address
== INA2XX_SHUNT_VOLTAGE
)
496 ret
= ina226_set_int_time_vshunt(chip
, val2
,
499 ret
= ina226_set_int_time_vbus(chip
, val2
,
502 if (chan
->address
== INA2XX_SHUNT_VOLTAGE
)
503 ret
= ina219_set_int_time_vshunt(chip
, val2
,
506 ret
= ina219_set_int_time_vbus(chip
, val2
,
511 case IIO_CHAN_INFO_HARDWAREGAIN
:
512 if (chan
->address
== INA2XX_SHUNT_VOLTAGE
)
513 ret
= ina219_set_vshunt_pga_gain(chip
, val
* 1000 +
516 ret
= ina219_set_vbus_range_denom(chip
, val
, &tmp
);
523 if (!ret
&& (tmp
!= config
))
524 ret
= regmap_write(chip
->regmap
, INA2XX_CONFIG
, tmp
);
526 mutex_unlock(&chip
->state_lock
);
531 static ssize_t
ina2xx_allow_async_readout_show(struct device
*dev
,
532 struct device_attribute
*attr
,
535 struct ina2xx_chip_info
*chip
= iio_priv(dev_to_iio_dev(dev
));
537 return sprintf(buf
, "%d\n", chip
->allow_async_readout
);
540 static ssize_t
ina2xx_allow_async_readout_store(struct device
*dev
,
541 struct device_attribute
*attr
,
542 const char *buf
, size_t len
)
544 struct ina2xx_chip_info
*chip
= iio_priv(dev_to_iio_dev(dev
));
548 ret
= strtobool((const char *) buf
, &val
);
552 chip
->allow_async_readout
= val
;
558 * Calibration register is set to the best value, which eliminates
559 * truncation errors on calculating current register in hardware.
560 * According to datasheet (INA 226: eq. 3, INA219: eq. 4) the best values
561 * are 2048 for ina226 and 4096 for ina219. They are hardcoded as
564 static int ina2xx_set_calibration(struct ina2xx_chip_info
*chip
)
566 return regmap_write(chip
->regmap
, INA2XX_CALIBRATION
,
567 chip
->config
->calibration_value
);
570 static int set_shunt_resistor(struct ina2xx_chip_info
*chip
, unsigned int val
)
572 if (val
== 0 || val
> INT_MAX
)
575 chip
->shunt_resistor_uohm
= val
;
580 static ssize_t
ina2xx_shunt_resistor_show(struct device
*dev
,
581 struct device_attribute
*attr
,
584 struct ina2xx_chip_info
*chip
= iio_priv(dev_to_iio_dev(dev
));
585 int vals
[2] = { chip
->shunt_resistor_uohm
, 1000000 };
587 return iio_format_value(buf
, IIO_VAL_FRACTIONAL
, 1, vals
);
590 static ssize_t
ina2xx_shunt_resistor_store(struct device
*dev
,
591 struct device_attribute
*attr
,
592 const char *buf
, size_t len
)
594 struct ina2xx_chip_info
*chip
= iio_priv(dev_to_iio_dev(dev
));
595 int val
, val_fract
, ret
;
597 ret
= iio_str_to_fixpoint(buf
, 100000, &val
, &val_fract
);
601 ret
= set_shunt_resistor(chip
, val
* 1000000 + val_fract
);
608 #define INA219_CHAN(_type, _index, _address) { \
610 .address = (_address), \
612 .channel = (_index), \
613 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | \
614 BIT(IIO_CHAN_INFO_SCALE), \
615 .info_mask_shared_by_dir = BIT(IIO_CHAN_INFO_SAMP_FREQ), \
616 .scan_index = (_index), \
621 .endianness = IIO_CPU, \
625 #define INA226_CHAN(_type, _index, _address) { \
627 .address = (_address), \
629 .channel = (_index), \
630 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | \
631 BIT(IIO_CHAN_INFO_SCALE), \
632 .info_mask_shared_by_dir = BIT(IIO_CHAN_INFO_SAMP_FREQ) | \
633 BIT(IIO_CHAN_INFO_OVERSAMPLING_RATIO), \
634 .scan_index = (_index), \
639 .endianness = IIO_CPU, \
644 * Sampling Freq is a consequence of the integration times of
645 * the Voltage channels.
647 #define INA219_CHAN_VOLTAGE(_index, _address, _shift) { \
648 .type = IIO_VOLTAGE, \
649 .address = (_address), \
651 .channel = (_index), \
652 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | \
653 BIT(IIO_CHAN_INFO_SCALE) | \
654 BIT(IIO_CHAN_INFO_INT_TIME) | \
655 BIT(IIO_CHAN_INFO_HARDWAREGAIN), \
656 .info_mask_separate_available = \
657 BIT(IIO_CHAN_INFO_HARDWAREGAIN), \
658 .info_mask_shared_by_dir = BIT(IIO_CHAN_INFO_SAMP_FREQ), \
659 .scan_index = (_index), \
663 .realbits = 16 - _shift, \
665 .endianness = IIO_LE, \
669 #define INA226_CHAN_VOLTAGE(_index, _address) { \
670 .type = IIO_VOLTAGE, \
671 .address = (_address), \
673 .channel = (_index), \
674 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | \
675 BIT(IIO_CHAN_INFO_SCALE) | \
676 BIT(IIO_CHAN_INFO_INT_TIME), \
677 .info_mask_shared_by_dir = BIT(IIO_CHAN_INFO_SAMP_FREQ) | \
678 BIT(IIO_CHAN_INFO_OVERSAMPLING_RATIO), \
679 .scan_index = (_index), \
684 .endianness = IIO_LE, \
689 static const struct iio_chan_spec ina226_channels
[] = {
690 INA226_CHAN_VOLTAGE(0, INA2XX_SHUNT_VOLTAGE
),
691 INA226_CHAN_VOLTAGE(1, INA2XX_BUS_VOLTAGE
),
692 INA226_CHAN(IIO_POWER
, 2, INA2XX_POWER
),
693 INA226_CHAN(IIO_CURRENT
, 3, INA2XX_CURRENT
),
694 IIO_CHAN_SOFT_TIMESTAMP(4),
697 static const struct iio_chan_spec ina219_channels
[] = {
698 INA219_CHAN_VOLTAGE(0, INA2XX_SHUNT_VOLTAGE
, 0),
699 INA219_CHAN_VOLTAGE(1, INA2XX_BUS_VOLTAGE
, INA219_BUS_VOLTAGE_SHIFT
),
700 INA219_CHAN(IIO_POWER
, 2, INA2XX_POWER
),
701 INA219_CHAN(IIO_CURRENT
, 3, INA2XX_CURRENT
),
702 IIO_CHAN_SOFT_TIMESTAMP(4),
705 static int ina2xx_conversion_ready(struct iio_dev
*indio_dev
)
707 struct ina2xx_chip_info
*chip
= iio_priv(indio_dev
);
712 * Because the timer thread and the chip conversion clock
713 * are asynchronous, the period difference will eventually
714 * result in reading V[k-1] again, or skip V[k] at time Tk.
715 * In order to resync the timer with the conversion process
716 * we check the ConVersionReadyFlag.
717 * On hardware that supports using the ALERT pin to toggle a
718 * GPIO a triggered buffer could be used instead.
719 * For now, we do an extra read of the MASK_ENABLE register (INA226)
720 * resp. the BUS_VOLTAGE register (INA219).
722 if (chip
->config
->chip_id
== ina226
) {
723 ret
= regmap_read(chip
->regmap
,
724 INA226_MASK_ENABLE
, &alert
);
725 alert
&= INA226_CVRF
;
727 ret
= regmap_read(chip
->regmap
,
728 INA2XX_BUS_VOLTAGE
, &alert
);
729 alert
&= INA219_CNVR
;
738 static int ina2xx_work_buffer(struct iio_dev
*indio_dev
)
740 struct ina2xx_chip_info
*chip
= iio_priv(indio_dev
);
741 /* data buffer needs space for channel data and timestap */
742 unsigned short data
[4 + sizeof(s64
)/sizeof(short)];
746 time
= iio_get_time_ns(indio_dev
);
749 * Single register reads: bulk_read will not work with ina226/219
750 * as there is no auto-increment of the register pointer.
752 for_each_set_bit(bit
, indio_dev
->active_scan_mask
,
753 indio_dev
->masklength
) {
756 ret
= regmap_read(chip
->regmap
,
757 INA2XX_SHUNT_VOLTAGE
+ bit
, &val
);
764 iio_push_to_buffers_with_timestamp(indio_dev
, data
, time
);
769 static int ina2xx_capture_thread(void *data
)
771 struct iio_dev
*indio_dev
= data
;
772 struct ina2xx_chip_info
*chip
= iio_priv(indio_dev
);
773 int sampling_us
= SAMPLING_PERIOD(chip
);
775 struct timespec64 next
, now
, delta
;
779 * Poll a bit faster than the chip internal Fs, in case
780 * we wish to sync with the conversion ready flag.
782 if (!chip
->allow_async_readout
)
785 ktime_get_ts64(&next
);
788 while (!chip
->allow_async_readout
) {
789 ret
= ina2xx_conversion_ready(indio_dev
);
794 * If the conversion was not yet finished,
795 * reset the reference timestamp.
798 ktime_get_ts64(&next
);
803 ret
= ina2xx_work_buffer(indio_dev
);
807 ktime_get_ts64(&now
);
810 * Advance the timestamp for the next poll by one sampling
811 * interval, and sleep for the remainder (next - now)
812 * In case "next" has already passed, the interval is added
813 * multiple times, i.e. samples are dropped.
816 timespec64_add_ns(&next
, 1000 * sampling_us
);
817 delta
= timespec64_sub(next
, now
);
818 delay_us
= div_s64(timespec64_to_ns(&delta
), 1000);
819 } while (delay_us
<= 0);
821 usleep_range(delay_us
, (delay_us
* 3) >> 1);
823 } while (!kthread_should_stop());
828 static int ina2xx_buffer_enable(struct iio_dev
*indio_dev
)
830 struct ina2xx_chip_info
*chip
= iio_priv(indio_dev
);
831 unsigned int sampling_us
= SAMPLING_PERIOD(chip
);
832 struct task_struct
*task
;
834 dev_dbg(&indio_dev
->dev
, "Enabling buffer w/ scan_mask %02x, freq = %d, avg =%u\n",
835 (unsigned int)(*indio_dev
->active_scan_mask
),
836 1000000 / sampling_us
, chip
->avg
);
838 dev_dbg(&indio_dev
->dev
, "Expected work period: %u us\n", sampling_us
);
839 dev_dbg(&indio_dev
->dev
, "Async readout mode: %d\n",
840 chip
->allow_async_readout
);
842 task
= kthread_create(ina2xx_capture_thread
, (void *)indio_dev
,
843 "%s:%d-%uus", indio_dev
->name
, indio_dev
->id
,
846 return PTR_ERR(task
);
848 get_task_struct(task
);
849 wake_up_process(task
);
855 static int ina2xx_buffer_disable(struct iio_dev
*indio_dev
)
857 struct ina2xx_chip_info
*chip
= iio_priv(indio_dev
);
860 kthread_stop(chip
->task
);
861 put_task_struct(chip
->task
);
868 static const struct iio_buffer_setup_ops ina2xx_setup_ops
= {
869 .postenable
= &ina2xx_buffer_enable
,
870 .predisable
= &ina2xx_buffer_disable
,
873 static int ina2xx_debug_reg(struct iio_dev
*indio_dev
,
874 unsigned reg
, unsigned writeval
, unsigned *readval
)
876 struct ina2xx_chip_info
*chip
= iio_priv(indio_dev
);
879 return regmap_write(chip
->regmap
, reg
, writeval
);
881 return regmap_read(chip
->regmap
, reg
, readval
);
884 /* Possible integration times for vshunt and vbus */
885 static IIO_CONST_ATTR_NAMED(ina219_integration_time_available
,
886 integration_time_available
,
887 "0.000084 0.000148 0.000276 0.000532 0.001060 0.002130 0.004260 0.008510 0.017020 0.034050 0.068100");
889 static IIO_CONST_ATTR_NAMED(ina226_integration_time_available
,
890 integration_time_available
,
891 "0.000140 0.000204 0.000332 0.000588 0.001100 0.002116 0.004156 0.008244");
893 static IIO_DEVICE_ATTR(in_allow_async_readout
, S_IRUGO
| S_IWUSR
,
894 ina2xx_allow_async_readout_show
,
895 ina2xx_allow_async_readout_store
, 0);
897 static IIO_DEVICE_ATTR(in_shunt_resistor
, S_IRUGO
| S_IWUSR
,
898 ina2xx_shunt_resistor_show
,
899 ina2xx_shunt_resistor_store
, 0);
901 static struct attribute
*ina219_attributes
[] = {
902 &iio_dev_attr_in_allow_async_readout
.dev_attr
.attr
,
903 &iio_const_attr_ina219_integration_time_available
.dev_attr
.attr
,
904 &iio_dev_attr_in_shunt_resistor
.dev_attr
.attr
,
908 static struct attribute
*ina226_attributes
[] = {
909 &iio_dev_attr_in_allow_async_readout
.dev_attr
.attr
,
910 &iio_const_attr_ina226_integration_time_available
.dev_attr
.attr
,
911 &iio_dev_attr_in_shunt_resistor
.dev_attr
.attr
,
915 static const struct attribute_group ina219_attribute_group
= {
916 .attrs
= ina219_attributes
,
919 static const struct attribute_group ina226_attribute_group
= {
920 .attrs
= ina226_attributes
,
923 static const struct iio_info ina219_info
= {
924 .attrs
= &ina219_attribute_group
,
925 .read_raw
= ina2xx_read_raw
,
926 .read_avail
= ina2xx_read_avail
,
927 .write_raw
= ina2xx_write_raw
,
928 .debugfs_reg_access
= ina2xx_debug_reg
,
931 static const struct iio_info ina226_info
= {
932 .attrs
= &ina226_attribute_group
,
933 .read_raw
= ina2xx_read_raw
,
934 .write_raw
= ina2xx_write_raw
,
935 .debugfs_reg_access
= ina2xx_debug_reg
,
938 /* Initialize the configuration and calibration registers. */
939 static int ina2xx_init(struct ina2xx_chip_info
*chip
, unsigned int config
)
941 int ret
= regmap_write(chip
->regmap
, INA2XX_CONFIG
, config
);
945 return ina2xx_set_calibration(chip
);
948 static int ina2xx_probe(struct i2c_client
*client
,
949 const struct i2c_device_id
*id
)
951 struct ina2xx_chip_info
*chip
;
952 struct iio_dev
*indio_dev
;
953 struct iio_buffer
*buffer
;
955 enum ina2xx_ids type
;
958 indio_dev
= devm_iio_device_alloc(&client
->dev
, sizeof(*chip
));
962 chip
= iio_priv(indio_dev
);
964 /* This is only used for device removal purposes. */
965 i2c_set_clientdata(client
, indio_dev
);
967 chip
->regmap
= devm_regmap_init_i2c(client
, &ina2xx_regmap_config
);
968 if (IS_ERR(chip
->regmap
)) {
969 dev_err(&client
->dev
, "failed to allocate register map\n");
970 return PTR_ERR(chip
->regmap
);
973 if (client
->dev
.of_node
)
974 type
= (enum ina2xx_ids
)of_device_get_match_data(&client
->dev
);
976 type
= id
->driver_data
;
977 chip
->config
= &ina2xx_config
[type
];
979 mutex_init(&chip
->state_lock
);
981 if (of_property_read_u32(client
->dev
.of_node
,
982 "shunt-resistor", &val
) < 0) {
983 struct ina2xx_platform_data
*pdata
=
984 dev_get_platdata(&client
->dev
);
987 val
= pdata
->shunt_uohms
;
989 val
= INA2XX_RSHUNT_DEFAULT
;
992 ret
= set_shunt_resistor(chip
, val
);
996 /* Patch the current config register with default. */
997 val
= chip
->config
->config_default
;
999 if (id
->driver_data
== ina226
) {
1000 ina226_set_average(chip
, INA226_DEFAULT_AVG
, &val
);
1001 ina226_set_int_time_vbus(chip
, INA226_DEFAULT_IT
, &val
);
1002 ina226_set_int_time_vshunt(chip
, INA226_DEFAULT_IT
, &val
);
1005 ina219_set_int_time_vbus(chip
, INA219_DEFAULT_IT
, &val
);
1006 ina219_set_int_time_vshunt(chip
, INA219_DEFAULT_IT
, &val
);
1007 ina219_set_vbus_range_denom(chip
, INA219_DEFAULT_BRNG
, &val
);
1008 ina219_set_vshunt_pga_gain(chip
, INA219_DEFAULT_PGA
, &val
);
1011 ret
= ina2xx_init(chip
, val
);
1013 dev_err(&client
->dev
, "error configuring the device\n");
1017 indio_dev
->modes
= INDIO_DIRECT_MODE
| INDIO_BUFFER_SOFTWARE
;
1018 indio_dev
->dev
.parent
= &client
->dev
;
1019 indio_dev
->dev
.of_node
= client
->dev
.of_node
;
1020 if (id
->driver_data
== ina226
) {
1021 indio_dev
->channels
= ina226_channels
;
1022 indio_dev
->num_channels
= ARRAY_SIZE(ina226_channels
);
1023 indio_dev
->info
= &ina226_info
;
1025 indio_dev
->channels
= ina219_channels
;
1026 indio_dev
->num_channels
= ARRAY_SIZE(ina219_channels
);
1027 indio_dev
->info
= &ina219_info
;
1029 indio_dev
->name
= id
->name
;
1030 indio_dev
->setup_ops
= &ina2xx_setup_ops
;
1032 buffer
= devm_iio_kfifo_allocate(&indio_dev
->dev
);
1036 iio_device_attach_buffer(indio_dev
, buffer
);
1038 return iio_device_register(indio_dev
);
1041 static int ina2xx_remove(struct i2c_client
*client
)
1043 struct iio_dev
*indio_dev
= i2c_get_clientdata(client
);
1044 struct ina2xx_chip_info
*chip
= iio_priv(indio_dev
);
1046 iio_device_unregister(indio_dev
);
1049 return regmap_update_bits(chip
->regmap
, INA2XX_CONFIG
,
1050 INA2XX_MODE_MASK
, 0);
1053 static const struct i2c_device_id ina2xx_id
[] = {
1061 MODULE_DEVICE_TABLE(i2c
, ina2xx_id
);
1063 static const struct of_device_id ina2xx_of_match
[] = {
1065 .compatible
= "ti,ina219",
1066 .data
= (void *)ina219
1069 .compatible
= "ti,ina220",
1070 .data
= (void *)ina219
1073 .compatible
= "ti,ina226",
1074 .data
= (void *)ina226
1077 .compatible
= "ti,ina230",
1078 .data
= (void *)ina226
1081 .compatible
= "ti,ina231",
1082 .data
= (void *)ina226
1086 MODULE_DEVICE_TABLE(of
, ina2xx_of_match
);
1088 static struct i2c_driver ina2xx_driver
= {
1090 .name
= KBUILD_MODNAME
,
1091 .of_match_table
= ina2xx_of_match
,
1093 .probe
= ina2xx_probe
,
1094 .remove
= ina2xx_remove
,
1095 .id_table
= ina2xx_id
,
1097 module_i2c_driver(ina2xx_driver
);
1099 MODULE_AUTHOR("Marc Titinger <marc.titinger@baylibre.com>");
1100 MODULE_DESCRIPTION("Texas Instruments INA2XX ADC driver");
1101 MODULE_LICENSE("GPL v2");