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/util_macros.h>
35 #include <linux/platform_data/ina2xx.h>
37 /* INA2XX registers definition */
38 #define INA2XX_CONFIG 0x00
39 #define INA2XX_SHUNT_VOLTAGE 0x01 /* readonly */
40 #define INA2XX_BUS_VOLTAGE 0x02 /* readonly */
41 #define INA2XX_POWER 0x03 /* readonly */
42 #define INA2XX_CURRENT 0x04 /* readonly */
43 #define INA2XX_CALIBRATION 0x05
45 #define INA226_MASK_ENABLE 0x06
46 #define INA226_CVRF BIT(3)
48 #define INA2XX_MAX_REGISTERS 8
50 /* settings - depend on use case */
51 #define INA219_CONFIG_DEFAULT 0x399F /* PGA=1/8, BRNG=32V */
52 #define INA219_DEFAULT_IT 532
53 #define INA219_DEFAULT_BRNG 1 /* 32V */
54 #define INA219_DEFAULT_PGA 125 /* 1000/8 */
55 #define INA226_CONFIG_DEFAULT 0x4327
56 #define INA226_DEFAULT_AVG 4
57 #define INA226_DEFAULT_IT 1110
59 #define INA2XX_RSHUNT_DEFAULT 10000
62 * bit masks for reading the settings in the configuration register
63 * FIXME: use regmap_fields.
65 #define INA2XX_MODE_MASK GENMASK(3, 0)
67 /* Gain for VShunt: 1/8 (default), 1/4, 1/2, 1 */
68 #define INA219_PGA_MASK GENMASK(12, 11)
69 #define INA219_SHIFT_PGA(val) ((val) << 11)
71 /* VBus range: 32V (default), 16V */
72 #define INA219_BRNG_MASK BIT(13)
73 #define INA219_SHIFT_BRNG(val) ((val) << 13)
75 /* Averaging for VBus/VShunt/Power */
76 #define INA226_AVG_MASK GENMASK(11, 9)
77 #define INA226_SHIFT_AVG(val) ((val) << 9)
79 /* Integration time for VBus */
80 #define INA219_ITB_MASK GENMASK(10, 7)
81 #define INA219_SHIFT_ITB(val) ((val) << 7)
82 #define INA226_ITB_MASK GENMASK(8, 6)
83 #define INA226_SHIFT_ITB(val) ((val) << 6)
85 /* Integration time for VShunt */
86 #define INA219_ITS_MASK GENMASK(6, 3)
87 #define INA219_SHIFT_ITS(val) ((val) << 3)
88 #define INA226_ITS_MASK GENMASK(5, 3)
89 #define INA226_SHIFT_ITS(val) ((val) << 3)
91 /* INA219 Bus voltage register, low bits are flags */
92 #define INA219_OVF BIT(0)
93 #define INA219_CNVR BIT(1)
94 #define INA219_BUS_VOLTAGE_SHIFT 3
96 /* Cosmetic macro giving the sampling period for a full P=UxI cycle */
97 #define SAMPLING_PERIOD(c) ((c->int_time_vbus + c->int_time_vshunt) \
100 static bool ina2xx_is_writeable_reg(struct device
*dev
, unsigned int reg
)
102 return (reg
== INA2XX_CONFIG
) || (reg
> INA2XX_CURRENT
);
105 static bool ina2xx_is_volatile_reg(struct device
*dev
, unsigned int reg
)
107 return (reg
!= INA2XX_CONFIG
);
110 static inline bool is_signed_reg(unsigned int reg
)
112 return (reg
== INA2XX_SHUNT_VOLTAGE
) || (reg
== INA2XX_CURRENT
);
115 static const struct regmap_config ina2xx_regmap_config
= {
118 .max_register
= INA2XX_MAX_REGISTERS
,
119 .writeable_reg
= ina2xx_is_writeable_reg
,
120 .volatile_reg
= ina2xx_is_volatile_reg
,
123 enum ina2xx_ids
{ ina219
, ina226
};
125 struct ina2xx_config
{
127 int calibration_value
;
128 int shunt_voltage_lsb
; /* nV */
129 int bus_voltage_shift
; /* position of lsb */
130 int bus_voltage_lsb
; /* uV */
131 /* fixed relation between current and power lsb, uW/uA */
132 int power_lsb_factor
;
133 enum ina2xx_ids chip_id
;
136 struct ina2xx_chip_info
{
137 struct regmap
*regmap
;
138 struct task_struct
*task
;
139 const struct ina2xx_config
*config
;
140 struct mutex state_lock
;
141 unsigned int shunt_resistor_uohm
;
143 int int_time_vbus
; /* Bus voltage integration time uS */
144 int int_time_vshunt
; /* Shunt voltage integration time uS */
145 int range_vbus
; /* Bus voltage maximum in V */
146 int pga_gain_vshunt
; /* Shunt voltage PGA gain */
147 bool allow_async_readout
;
150 static const struct ina2xx_config ina2xx_config
[] = {
152 .config_default
= INA219_CONFIG_DEFAULT
,
153 .calibration_value
= 4096,
154 .shunt_voltage_lsb
= 10000,
155 .bus_voltage_shift
= INA219_BUS_VOLTAGE_SHIFT
,
156 .bus_voltage_lsb
= 4000,
157 .power_lsb_factor
= 20,
161 .config_default
= INA226_CONFIG_DEFAULT
,
162 .calibration_value
= 2048,
163 .shunt_voltage_lsb
= 2500,
164 .bus_voltage_shift
= 0,
165 .bus_voltage_lsb
= 1250,
166 .power_lsb_factor
= 25,
171 static int ina2xx_read_raw(struct iio_dev
*indio_dev
,
172 struct iio_chan_spec
const *chan
,
173 int *val
, int *val2
, long mask
)
176 struct ina2xx_chip_info
*chip
= iio_priv(indio_dev
);
180 case IIO_CHAN_INFO_RAW
:
181 ret
= regmap_read(chip
->regmap
, chan
->address
, ®val
);
185 if (is_signed_reg(chan
->address
))
190 if (chan
->address
== INA2XX_BUS_VOLTAGE
)
191 *val
>>= chip
->config
->bus_voltage_shift
;
195 case IIO_CHAN_INFO_OVERSAMPLING_RATIO
:
199 case IIO_CHAN_INFO_INT_TIME
:
201 if (chan
->address
== INA2XX_SHUNT_VOLTAGE
)
202 *val2
= chip
->int_time_vshunt
;
204 *val2
= chip
->int_time_vbus
;
206 return IIO_VAL_INT_PLUS_MICRO
;
208 case IIO_CHAN_INFO_SAMP_FREQ
:
210 * Sample freq is read only, it is a consequence of
211 * 1/AVG*(CT_bus+CT_shunt).
213 *val
= DIV_ROUND_CLOSEST(1000000, SAMPLING_PERIOD(chip
));
217 case IIO_CHAN_INFO_SCALE
:
218 switch (chan
->address
) {
219 case INA2XX_SHUNT_VOLTAGE
:
220 /* processed (mV) = raw * lsb(nV) / 1000000 */
221 *val
= chip
->config
->shunt_voltage_lsb
;
223 return IIO_VAL_FRACTIONAL
;
225 case INA2XX_BUS_VOLTAGE
:
226 /* processed (mV) = raw * lsb (uV) / 1000 */
227 *val
= chip
->config
->bus_voltage_lsb
;
229 return IIO_VAL_FRACTIONAL
;
233 * processed (mA) = raw * current_lsb (mA)
234 * current_lsb (mA) = shunt_voltage_lsb (nV) /
235 * shunt_resistor (uOhm)
237 *val
= chip
->config
->shunt_voltage_lsb
;
238 *val2
= chip
->shunt_resistor_uohm
;
239 return IIO_VAL_FRACTIONAL
;
243 * processed (mW) = raw * power_lsb (mW)
244 * power_lsb (mW) = power_lsb_factor (mW/mA) *
247 *val
= chip
->config
->power_lsb_factor
*
248 chip
->config
->shunt_voltage_lsb
;
249 *val2
= chip
->shunt_resistor_uohm
;
250 return IIO_VAL_FRACTIONAL
;
253 case IIO_CHAN_INFO_HARDWAREGAIN
:
254 switch (chan
->address
) {
255 case INA2XX_SHUNT_VOLTAGE
:
256 *val
= chip
->pga_gain_vshunt
;
258 return IIO_VAL_FRACTIONAL
;
260 case INA2XX_BUS_VOLTAGE
:
261 *val
= chip
->range_vbus
== 32 ? 1 : 2;
270 * Available averaging rates for ina226. The indices correspond with
271 * the bit values expected by the chip (according to the ina226 datasheet,
272 * table 3 AVG bit settings, found at
273 * http://www.ti.com/lit/ds/symlink/ina226.pdf.
275 static const int ina226_avg_tab
[] = { 1, 4, 16, 64, 128, 256, 512, 1024 };
277 static int ina226_set_average(struct ina2xx_chip_info
*chip
, unsigned int val
,
278 unsigned int *config
)
282 if (val
> 1024 || val
< 1)
285 bits
= find_closest(val
, ina226_avg_tab
,
286 ARRAY_SIZE(ina226_avg_tab
));
288 chip
->avg
= ina226_avg_tab
[bits
];
290 *config
&= ~INA226_AVG_MASK
;
291 *config
|= INA226_SHIFT_AVG(bits
) & INA226_AVG_MASK
;
296 /* Conversion times in uS */
297 static const int ina226_conv_time_tab
[] = { 140, 204, 332, 588, 1100,
300 static int ina226_set_int_time_vbus(struct ina2xx_chip_info
*chip
,
301 unsigned int val_us
, unsigned int *config
)
305 if (val_us
> 8244 || val_us
< 140)
308 bits
= find_closest(val_us
, ina226_conv_time_tab
,
309 ARRAY_SIZE(ina226_conv_time_tab
));
311 chip
->int_time_vbus
= ina226_conv_time_tab
[bits
];
313 *config
&= ~INA226_ITB_MASK
;
314 *config
|= INA226_SHIFT_ITB(bits
) & INA226_ITB_MASK
;
319 static int ina226_set_int_time_vshunt(struct ina2xx_chip_info
*chip
,
320 unsigned int val_us
, unsigned int *config
)
324 if (val_us
> 8244 || val_us
< 140)
327 bits
= find_closest(val_us
, ina226_conv_time_tab
,
328 ARRAY_SIZE(ina226_conv_time_tab
));
330 chip
->int_time_vshunt
= ina226_conv_time_tab
[bits
];
332 *config
&= ~INA226_ITS_MASK
;
333 *config
|= INA226_SHIFT_ITS(bits
) & INA226_ITS_MASK
;
338 /* Conversion times in uS. */
339 static const int ina219_conv_time_tab_subsample
[] = { 84, 148, 276, 532 };
340 static const int ina219_conv_time_tab_average
[] = { 532, 1060, 2130, 4260,
341 8510, 17020, 34050, 68100};
343 static int ina219_lookup_int_time(unsigned int *val_us
, int *bits
)
345 if (*val_us
> 68100 || *val_us
< 84)
348 if (*val_us
<= 532) {
349 *bits
= find_closest(*val_us
, ina219_conv_time_tab_subsample
,
350 ARRAY_SIZE(ina219_conv_time_tab_subsample
));
351 *val_us
= ina219_conv_time_tab_subsample
[*bits
];
353 *bits
= find_closest(*val_us
, ina219_conv_time_tab_average
,
354 ARRAY_SIZE(ina219_conv_time_tab_average
));
355 *val_us
= ina219_conv_time_tab_average
[*bits
];
362 static int ina219_set_int_time_vbus(struct ina2xx_chip_info
*chip
,
363 unsigned int val_us
, unsigned int *config
)
366 unsigned int val_us_best
= val_us
;
368 ret
= ina219_lookup_int_time(&val_us_best
, &bits
);
372 chip
->int_time_vbus
= val_us_best
;
374 *config
&= ~INA219_ITB_MASK
;
375 *config
|= INA219_SHIFT_ITB(bits
) & INA219_ITB_MASK
;
380 static int ina219_set_int_time_vshunt(struct ina2xx_chip_info
*chip
,
381 unsigned int val_us
, unsigned int *config
)
384 unsigned int val_us_best
= val_us
;
386 ret
= ina219_lookup_int_time(&val_us_best
, &bits
);
390 chip
->int_time_vshunt
= val_us_best
;
392 *config
&= ~INA219_ITS_MASK
;
393 *config
|= INA219_SHIFT_ITS(bits
) & INA219_ITS_MASK
;
398 static const int ina219_vbus_range_tab
[] = { 1, 2 };
399 static int ina219_set_vbus_range_denom(struct ina2xx_chip_info
*chip
,
401 unsigned int *config
)
404 chip
->range_vbus
= 32;
406 chip
->range_vbus
= 16;
410 *config
&= ~INA219_BRNG_MASK
;
411 *config
|= INA219_SHIFT_BRNG(range
== 1 ? 1 : 0) & INA219_BRNG_MASK
;
416 static const int ina219_vshunt_gain_tab
[] = { 125, 250, 500, 1000 };
417 static const int ina219_vshunt_gain_frac
[] = {
418 125, 1000, 250, 1000, 500, 1000, 1000, 1000 };
420 static int ina219_set_vshunt_pga_gain(struct ina2xx_chip_info
*chip
,
422 unsigned int *config
)
426 if (gain
< 125 || gain
> 1000)
429 bits
= find_closest(gain
, ina219_vshunt_gain_tab
,
430 ARRAY_SIZE(ina219_vshunt_gain_tab
));
432 chip
->pga_gain_vshunt
= ina219_vshunt_gain_tab
[bits
];
435 *config
&= ~INA219_PGA_MASK
;
436 *config
|= INA219_SHIFT_PGA(bits
) & INA219_PGA_MASK
;
441 static int ina2xx_read_avail(struct iio_dev
*indio_dev
,
442 struct iio_chan_spec
const *chan
,
443 const int **vals
, int *type
, int *length
,
447 case IIO_CHAN_INFO_HARDWAREGAIN
:
448 switch (chan
->address
) {
449 case INA2XX_SHUNT_VOLTAGE
:
450 *type
= IIO_VAL_FRACTIONAL
;
451 *length
= sizeof(ina219_vshunt_gain_frac
) / sizeof(int);
452 *vals
= ina219_vshunt_gain_frac
;
453 return IIO_AVAIL_LIST
;
455 case INA2XX_BUS_VOLTAGE
:
457 *length
= sizeof(ina219_vbus_range_tab
) / sizeof(int);
458 *vals
= ina219_vbus_range_tab
;
459 return IIO_AVAIL_LIST
;
466 static int ina2xx_write_raw(struct iio_dev
*indio_dev
,
467 struct iio_chan_spec
const *chan
,
468 int val
, int val2
, long mask
)
470 struct ina2xx_chip_info
*chip
= iio_priv(indio_dev
);
471 unsigned int config
, tmp
;
474 if (iio_buffer_enabled(indio_dev
))
477 mutex_lock(&chip
->state_lock
);
479 ret
= regmap_read(chip
->regmap
, INA2XX_CONFIG
, &config
);
486 case IIO_CHAN_INFO_OVERSAMPLING_RATIO
:
487 ret
= ina226_set_average(chip
, val
, &tmp
);
490 case IIO_CHAN_INFO_INT_TIME
:
491 if (chip
->config
->chip_id
== ina226
) {
492 if (chan
->address
== INA2XX_SHUNT_VOLTAGE
)
493 ret
= ina226_set_int_time_vshunt(chip
, val2
,
496 ret
= ina226_set_int_time_vbus(chip
, val2
,
499 if (chan
->address
== INA2XX_SHUNT_VOLTAGE
)
500 ret
= ina219_set_int_time_vshunt(chip
, val2
,
503 ret
= ina219_set_int_time_vbus(chip
, val2
,
508 case IIO_CHAN_INFO_HARDWAREGAIN
:
509 if (chan
->address
== INA2XX_SHUNT_VOLTAGE
)
510 ret
= ina219_set_vshunt_pga_gain(chip
, val
* 1000 +
513 ret
= ina219_set_vbus_range_denom(chip
, val
, &tmp
);
520 if (!ret
&& (tmp
!= config
))
521 ret
= regmap_write(chip
->regmap
, INA2XX_CONFIG
, tmp
);
523 mutex_unlock(&chip
->state_lock
);
528 static ssize_t
ina2xx_allow_async_readout_show(struct device
*dev
,
529 struct device_attribute
*attr
,
532 struct ina2xx_chip_info
*chip
= iio_priv(dev_to_iio_dev(dev
));
534 return sprintf(buf
, "%d\n", chip
->allow_async_readout
);
537 static ssize_t
ina2xx_allow_async_readout_store(struct device
*dev
,
538 struct device_attribute
*attr
,
539 const char *buf
, size_t len
)
541 struct ina2xx_chip_info
*chip
= iio_priv(dev_to_iio_dev(dev
));
545 ret
= strtobool((const char *) buf
, &val
);
549 chip
->allow_async_readout
= val
;
555 * Calibration register is set to the best value, which eliminates
556 * truncation errors on calculating current register in hardware.
557 * According to datasheet (INA 226: eq. 3, INA219: eq. 4) the best values
558 * are 2048 for ina226 and 4096 for ina219. They are hardcoded as
561 static int ina2xx_set_calibration(struct ina2xx_chip_info
*chip
)
563 return regmap_write(chip
->regmap
, INA2XX_CALIBRATION
,
564 chip
->config
->calibration_value
);
567 static int set_shunt_resistor(struct ina2xx_chip_info
*chip
, unsigned int val
)
569 if (val
== 0 || val
> INT_MAX
)
572 chip
->shunt_resistor_uohm
= val
;
577 static ssize_t
ina2xx_shunt_resistor_show(struct device
*dev
,
578 struct device_attribute
*attr
,
581 struct ina2xx_chip_info
*chip
= iio_priv(dev_to_iio_dev(dev
));
582 int vals
[2] = { chip
->shunt_resistor_uohm
, 1000000 };
584 return iio_format_value(buf
, IIO_VAL_FRACTIONAL
, 1, vals
);
587 static ssize_t
ina2xx_shunt_resistor_store(struct device
*dev
,
588 struct device_attribute
*attr
,
589 const char *buf
, size_t len
)
591 struct ina2xx_chip_info
*chip
= iio_priv(dev_to_iio_dev(dev
));
592 int val
, val_fract
, ret
;
594 ret
= iio_str_to_fixpoint(buf
, 100000, &val
, &val_fract
);
598 ret
= set_shunt_resistor(chip
, val
* 1000000 + val_fract
);
605 #define INA219_CHAN(_type, _index, _address) { \
607 .address = (_address), \
609 .channel = (_index), \
610 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | \
611 BIT(IIO_CHAN_INFO_SCALE), \
612 .info_mask_shared_by_dir = BIT(IIO_CHAN_INFO_SAMP_FREQ), \
613 .scan_index = (_index), \
618 .endianness = IIO_CPU, \
622 #define INA226_CHAN(_type, _index, _address) { \
624 .address = (_address), \
626 .channel = (_index), \
627 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | \
628 BIT(IIO_CHAN_INFO_SCALE), \
629 .info_mask_shared_by_dir = BIT(IIO_CHAN_INFO_SAMP_FREQ) | \
630 BIT(IIO_CHAN_INFO_OVERSAMPLING_RATIO), \
631 .scan_index = (_index), \
636 .endianness = IIO_CPU, \
641 * Sampling Freq is a consequence of the integration times of
642 * the Voltage channels.
644 #define INA219_CHAN_VOLTAGE(_index, _address, _shift) { \
645 .type = IIO_VOLTAGE, \
646 .address = (_address), \
648 .channel = (_index), \
649 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | \
650 BIT(IIO_CHAN_INFO_SCALE) | \
651 BIT(IIO_CHAN_INFO_INT_TIME) | \
652 BIT(IIO_CHAN_INFO_HARDWAREGAIN), \
653 .info_mask_separate_available = \
654 BIT(IIO_CHAN_INFO_HARDWAREGAIN), \
655 .info_mask_shared_by_dir = BIT(IIO_CHAN_INFO_SAMP_FREQ), \
656 .scan_index = (_index), \
660 .realbits = 16 - _shift, \
662 .endianness = IIO_LE, \
666 #define INA226_CHAN_VOLTAGE(_index, _address) { \
667 .type = IIO_VOLTAGE, \
668 .address = (_address), \
670 .channel = (_index), \
671 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | \
672 BIT(IIO_CHAN_INFO_SCALE) | \
673 BIT(IIO_CHAN_INFO_INT_TIME), \
674 .info_mask_shared_by_dir = BIT(IIO_CHAN_INFO_SAMP_FREQ) | \
675 BIT(IIO_CHAN_INFO_OVERSAMPLING_RATIO), \
676 .scan_index = (_index), \
681 .endianness = IIO_LE, \
686 static const struct iio_chan_spec ina226_channels
[] = {
687 INA226_CHAN_VOLTAGE(0, INA2XX_SHUNT_VOLTAGE
),
688 INA226_CHAN_VOLTAGE(1, INA2XX_BUS_VOLTAGE
),
689 INA226_CHAN(IIO_POWER
, 2, INA2XX_POWER
),
690 INA226_CHAN(IIO_CURRENT
, 3, INA2XX_CURRENT
),
691 IIO_CHAN_SOFT_TIMESTAMP(4),
694 static const struct iio_chan_spec ina219_channels
[] = {
695 INA219_CHAN_VOLTAGE(0, INA2XX_SHUNT_VOLTAGE
, 0),
696 INA219_CHAN_VOLTAGE(1, INA2XX_BUS_VOLTAGE
, INA219_BUS_VOLTAGE_SHIFT
),
697 INA219_CHAN(IIO_POWER
, 2, INA2XX_POWER
),
698 INA219_CHAN(IIO_CURRENT
, 3, INA2XX_CURRENT
),
699 IIO_CHAN_SOFT_TIMESTAMP(4),
702 static int ina2xx_conversion_ready(struct iio_dev
*indio_dev
)
704 struct ina2xx_chip_info
*chip
= iio_priv(indio_dev
);
709 * Because the timer thread and the chip conversion clock
710 * are asynchronous, the period difference will eventually
711 * result in reading V[k-1] again, or skip V[k] at time Tk.
712 * In order to resync the timer with the conversion process
713 * we check the ConVersionReadyFlag.
714 * On hardware that supports using the ALERT pin to toggle a
715 * GPIO a triggered buffer could be used instead.
716 * For now, we do an extra read of the MASK_ENABLE register (INA226)
717 * resp. the BUS_VOLTAGE register (INA219).
719 if (chip
->config
->chip_id
== ina226
) {
720 ret
= regmap_read(chip
->regmap
,
721 INA226_MASK_ENABLE
, &alert
);
722 alert
&= INA226_CVRF
;
724 ret
= regmap_read(chip
->regmap
,
725 INA2XX_BUS_VOLTAGE
, &alert
);
726 alert
&= INA219_CNVR
;
735 static int ina2xx_work_buffer(struct iio_dev
*indio_dev
)
737 struct ina2xx_chip_info
*chip
= iio_priv(indio_dev
);
738 /* data buffer needs space for channel data and timestap */
739 unsigned short data
[4 + sizeof(s64
)/sizeof(short)];
743 time
= iio_get_time_ns(indio_dev
);
746 * Single register reads: bulk_read will not work with ina226/219
747 * as there is no auto-increment of the register pointer.
749 for_each_set_bit(bit
, indio_dev
->active_scan_mask
,
750 indio_dev
->masklength
) {
753 ret
= regmap_read(chip
->regmap
,
754 INA2XX_SHUNT_VOLTAGE
+ bit
, &val
);
761 iio_push_to_buffers_with_timestamp(indio_dev
, data
, time
);
766 static int ina2xx_capture_thread(void *data
)
768 struct iio_dev
*indio_dev
= data
;
769 struct ina2xx_chip_info
*chip
= iio_priv(indio_dev
);
770 int sampling_us
= SAMPLING_PERIOD(chip
);
772 struct timespec64 next
, now
, delta
;
776 * Poll a bit faster than the chip internal Fs, in case
777 * we wish to sync with the conversion ready flag.
779 if (!chip
->allow_async_readout
)
782 ktime_get_ts64(&next
);
785 while (!chip
->allow_async_readout
) {
786 ret
= ina2xx_conversion_ready(indio_dev
);
791 * If the conversion was not yet finished,
792 * reset the reference timestamp.
795 ktime_get_ts64(&next
);
800 ret
= ina2xx_work_buffer(indio_dev
);
804 ktime_get_ts64(&now
);
807 * Advance the timestamp for the next poll by one sampling
808 * interval, and sleep for the remainder (next - now)
809 * In case "next" has already passed, the interval is added
810 * multiple times, i.e. samples are dropped.
813 timespec64_add_ns(&next
, 1000 * sampling_us
);
814 delta
= timespec64_sub(next
, now
);
815 delay_us
= div_s64(timespec64_to_ns(&delta
), 1000);
816 } while (delay_us
<= 0);
818 usleep_range(delay_us
, (delay_us
* 3) >> 1);
820 } while (!kthread_should_stop());
825 static int ina2xx_buffer_enable(struct iio_dev
*indio_dev
)
827 struct ina2xx_chip_info
*chip
= iio_priv(indio_dev
);
828 unsigned int sampling_us
= SAMPLING_PERIOD(chip
);
830 dev_dbg(&indio_dev
->dev
, "Enabling buffer w/ scan_mask %02x, freq = %d, avg =%u\n",
831 (unsigned int)(*indio_dev
->active_scan_mask
),
832 1000000 / sampling_us
, chip
->avg
);
834 dev_dbg(&indio_dev
->dev
, "Expected work period: %u us\n", sampling_us
);
835 dev_dbg(&indio_dev
->dev
, "Async readout mode: %d\n",
836 chip
->allow_async_readout
);
838 chip
->task
= kthread_run(ina2xx_capture_thread
, (void *)indio_dev
,
839 "%s:%d-%uus", indio_dev
->name
, indio_dev
->id
,
842 return PTR_ERR_OR_ZERO(chip
->task
);
845 static int ina2xx_buffer_disable(struct iio_dev
*indio_dev
)
847 struct ina2xx_chip_info
*chip
= iio_priv(indio_dev
);
850 kthread_stop(chip
->task
);
857 static const struct iio_buffer_setup_ops ina2xx_setup_ops
= {
858 .postenable
= &ina2xx_buffer_enable
,
859 .predisable
= &ina2xx_buffer_disable
,
862 static int ina2xx_debug_reg(struct iio_dev
*indio_dev
,
863 unsigned reg
, unsigned writeval
, unsigned *readval
)
865 struct ina2xx_chip_info
*chip
= iio_priv(indio_dev
);
868 return regmap_write(chip
->regmap
, reg
, writeval
);
870 return regmap_read(chip
->regmap
, reg
, readval
);
873 /* Possible integration times for vshunt and vbus */
874 static IIO_CONST_ATTR_NAMED(ina219_integration_time_available
,
875 integration_time_available
,
876 "0.000084 0.000148 0.000276 0.000532 0.001060 0.002130 0.004260 0.008510 0.017020 0.034050 0.068100");
878 static IIO_CONST_ATTR_NAMED(ina226_integration_time_available
,
879 integration_time_available
,
880 "0.000140 0.000204 0.000332 0.000588 0.001100 0.002116 0.004156 0.008244");
882 static IIO_DEVICE_ATTR(in_allow_async_readout
, S_IRUGO
| S_IWUSR
,
883 ina2xx_allow_async_readout_show
,
884 ina2xx_allow_async_readout_store
, 0);
886 static IIO_DEVICE_ATTR(in_shunt_resistor
, S_IRUGO
| S_IWUSR
,
887 ina2xx_shunt_resistor_show
,
888 ina2xx_shunt_resistor_store
, 0);
890 static struct attribute
*ina219_attributes
[] = {
891 &iio_dev_attr_in_allow_async_readout
.dev_attr
.attr
,
892 &iio_const_attr_ina219_integration_time_available
.dev_attr
.attr
,
893 &iio_dev_attr_in_shunt_resistor
.dev_attr
.attr
,
897 static struct attribute
*ina226_attributes
[] = {
898 &iio_dev_attr_in_allow_async_readout
.dev_attr
.attr
,
899 &iio_const_attr_ina226_integration_time_available
.dev_attr
.attr
,
900 &iio_dev_attr_in_shunt_resistor
.dev_attr
.attr
,
904 static const struct attribute_group ina219_attribute_group
= {
905 .attrs
= ina219_attributes
,
908 static const struct attribute_group ina226_attribute_group
= {
909 .attrs
= ina226_attributes
,
912 static const struct iio_info ina219_info
= {
913 .attrs
= &ina219_attribute_group
,
914 .read_raw
= ina2xx_read_raw
,
915 .read_avail
= ina2xx_read_avail
,
916 .write_raw
= ina2xx_write_raw
,
917 .debugfs_reg_access
= ina2xx_debug_reg
,
920 static const struct iio_info ina226_info
= {
921 .attrs
= &ina226_attribute_group
,
922 .read_raw
= ina2xx_read_raw
,
923 .write_raw
= ina2xx_write_raw
,
924 .debugfs_reg_access
= ina2xx_debug_reg
,
927 /* Initialize the configuration and calibration registers. */
928 static int ina2xx_init(struct ina2xx_chip_info
*chip
, unsigned int config
)
930 int ret
= regmap_write(chip
->regmap
, INA2XX_CONFIG
, config
);
934 return ina2xx_set_calibration(chip
);
937 static int ina2xx_probe(struct i2c_client
*client
,
938 const struct i2c_device_id
*id
)
940 struct ina2xx_chip_info
*chip
;
941 struct iio_dev
*indio_dev
;
942 struct iio_buffer
*buffer
;
944 enum ina2xx_ids type
;
947 indio_dev
= devm_iio_device_alloc(&client
->dev
, sizeof(*chip
));
951 chip
= iio_priv(indio_dev
);
953 /* This is only used for device removal purposes. */
954 i2c_set_clientdata(client
, indio_dev
);
956 chip
->regmap
= devm_regmap_init_i2c(client
, &ina2xx_regmap_config
);
957 if (IS_ERR(chip
->regmap
)) {
958 dev_err(&client
->dev
, "failed to allocate register map\n");
959 return PTR_ERR(chip
->regmap
);
962 if (client
->dev
.of_node
)
963 type
= (enum ina2xx_ids
)of_device_get_match_data(&client
->dev
);
965 type
= id
->driver_data
;
966 chip
->config
= &ina2xx_config
[type
];
968 mutex_init(&chip
->state_lock
);
970 if (of_property_read_u32(client
->dev
.of_node
,
971 "shunt-resistor", &val
) < 0) {
972 struct ina2xx_platform_data
*pdata
=
973 dev_get_platdata(&client
->dev
);
976 val
= pdata
->shunt_uohms
;
978 val
= INA2XX_RSHUNT_DEFAULT
;
981 ret
= set_shunt_resistor(chip
, val
);
985 /* Patch the current config register with default. */
986 val
= chip
->config
->config_default
;
988 if (id
->driver_data
== ina226
) {
989 ina226_set_average(chip
, INA226_DEFAULT_AVG
, &val
);
990 ina226_set_int_time_vbus(chip
, INA226_DEFAULT_IT
, &val
);
991 ina226_set_int_time_vshunt(chip
, INA226_DEFAULT_IT
, &val
);
994 ina219_set_int_time_vbus(chip
, INA219_DEFAULT_IT
, &val
);
995 ina219_set_int_time_vshunt(chip
, INA219_DEFAULT_IT
, &val
);
996 ina219_set_vbus_range_denom(chip
, INA219_DEFAULT_BRNG
, &val
);
997 ina219_set_vshunt_pga_gain(chip
, INA219_DEFAULT_PGA
, &val
);
1000 ret
= ina2xx_init(chip
, val
);
1002 dev_err(&client
->dev
, "error configuring the device\n");
1006 indio_dev
->modes
= INDIO_DIRECT_MODE
| INDIO_BUFFER_SOFTWARE
;
1007 indio_dev
->dev
.parent
= &client
->dev
;
1008 indio_dev
->dev
.of_node
= client
->dev
.of_node
;
1009 if (id
->driver_data
== ina226
) {
1010 indio_dev
->channels
= ina226_channels
;
1011 indio_dev
->num_channels
= ARRAY_SIZE(ina226_channels
);
1012 indio_dev
->info
= &ina226_info
;
1014 indio_dev
->channels
= ina219_channels
;
1015 indio_dev
->num_channels
= ARRAY_SIZE(ina219_channels
);
1016 indio_dev
->info
= &ina219_info
;
1018 indio_dev
->name
= id
->name
;
1019 indio_dev
->setup_ops
= &ina2xx_setup_ops
;
1021 buffer
= devm_iio_kfifo_allocate(&indio_dev
->dev
);
1025 iio_device_attach_buffer(indio_dev
, buffer
);
1027 return iio_device_register(indio_dev
);
1030 static int ina2xx_remove(struct i2c_client
*client
)
1032 struct iio_dev
*indio_dev
= i2c_get_clientdata(client
);
1033 struct ina2xx_chip_info
*chip
= iio_priv(indio_dev
);
1035 iio_device_unregister(indio_dev
);
1038 return regmap_update_bits(chip
->regmap
, INA2XX_CONFIG
,
1039 INA2XX_MODE_MASK
, 0);
1042 static const struct i2c_device_id ina2xx_id
[] = {
1050 MODULE_DEVICE_TABLE(i2c
, ina2xx_id
);
1052 static const struct of_device_id ina2xx_of_match
[] = {
1054 .compatible
= "ti,ina219",
1055 .data
= (void *)ina219
1058 .compatible
= "ti,ina220",
1059 .data
= (void *)ina219
1062 .compatible
= "ti,ina226",
1063 .data
= (void *)ina226
1066 .compatible
= "ti,ina230",
1067 .data
= (void *)ina226
1070 .compatible
= "ti,ina231",
1071 .data
= (void *)ina226
1075 MODULE_DEVICE_TABLE(of
, ina2xx_of_match
);
1077 static struct i2c_driver ina2xx_driver
= {
1079 .name
= KBUILD_MODNAME
,
1080 .of_match_table
= ina2xx_of_match
,
1082 .probe
= ina2xx_probe
,
1083 .remove
= ina2xx_remove
,
1084 .id_table
= ina2xx_id
,
1086 module_i2c_driver(ina2xx_driver
);
1088 MODULE_AUTHOR("Marc Titinger <marc.titinger@baylibre.com>");
1089 MODULE_DESCRIPTION("Texas Instruments INA2XX ADC driver");
1090 MODULE_LICENSE("GPL v2");