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
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;
149 /* data buffer needs space for channel data and timestamp */
156 static const struct ina2xx_config ina2xx_config[] = {
158 .config_default = INA219_CONFIG_DEFAULT,
159 .calibration_value = 4096,
160 .shunt_voltage_lsb = 10000,
161 .bus_voltage_shift = INA219_BUS_VOLTAGE_SHIFT,
162 .bus_voltage_lsb = 4000,
163 .power_lsb_factor = 20,
167 .config_default = INA226_CONFIG_DEFAULT,
168 .calibration_value = 2048,
169 .shunt_voltage_lsb = 2500,
170 .bus_voltage_shift = 0,
171 .bus_voltage_lsb = 1250,
172 .power_lsb_factor = 25,
177 static int ina2xx_read_raw(struct iio_dev *indio_dev,
178 struct iio_chan_spec const *chan,
179 int *val, int *val2, long mask)
182 struct ina2xx_chip_info *chip = iio_priv(indio_dev);
186 case IIO_CHAN_INFO_RAW:
187 ret = regmap_read(chip->regmap, chan->address, ®val);
191 if (is_signed_reg(chan->address))
196 if (chan->address == INA2XX_BUS_VOLTAGE)
197 *val >>= chip->config->bus_voltage_shift;
201 case IIO_CHAN_INFO_OVERSAMPLING_RATIO:
205 case IIO_CHAN_INFO_INT_TIME:
207 if (chan->address == INA2XX_SHUNT_VOLTAGE)
208 *val2 = chip->int_time_vshunt;
210 *val2 = chip->int_time_vbus;
212 return IIO_VAL_INT_PLUS_MICRO;
214 case IIO_CHAN_INFO_SAMP_FREQ:
216 * Sample freq is read only, it is a consequence of
217 * 1/AVG*(CT_bus+CT_shunt).
219 *val = DIV_ROUND_CLOSEST(1000000, SAMPLING_PERIOD(chip));
223 case IIO_CHAN_INFO_SCALE:
224 switch (chan->address) {
225 case INA2XX_SHUNT_VOLTAGE:
226 /* processed (mV) = raw * lsb(nV) / 1000000 */
227 *val = chip->config->shunt_voltage_lsb;
229 return IIO_VAL_FRACTIONAL;
231 case INA2XX_BUS_VOLTAGE:
232 /* processed (mV) = raw * lsb (uV) / 1000 */
233 *val = chip->config->bus_voltage_lsb;
235 return IIO_VAL_FRACTIONAL;
239 * processed (mA) = raw * current_lsb (mA)
240 * current_lsb (mA) = shunt_voltage_lsb (nV) /
241 * shunt_resistor (uOhm)
243 *val = chip->config->shunt_voltage_lsb;
244 *val2 = chip->shunt_resistor_uohm;
245 return IIO_VAL_FRACTIONAL;
249 * processed (mW) = raw * power_lsb (mW)
250 * power_lsb (mW) = power_lsb_factor (mW/mA) *
253 *val = chip->config->power_lsb_factor *
254 chip->config->shunt_voltage_lsb;
255 *val2 = chip->shunt_resistor_uohm;
256 return IIO_VAL_FRACTIONAL;
260 case IIO_CHAN_INFO_HARDWAREGAIN:
261 switch (chan->address) {
262 case INA2XX_SHUNT_VOLTAGE:
263 *val = chip->pga_gain_vshunt;
265 return IIO_VAL_FRACTIONAL;
267 case INA2XX_BUS_VOLTAGE:
268 *val = chip->range_vbus == 32 ? 1 : 2;
278 * Available averaging rates for ina226. The indices correspond with
279 * the bit values expected by the chip (according to the ina226 datasheet,
280 * table 3 AVG bit settings, found at
281 * https://www.ti.com/lit/ds/symlink/ina226.pdf.
283 static const int ina226_avg_tab[] = { 1, 4, 16, 64, 128, 256, 512, 1024 };
285 static int ina226_set_average(struct ina2xx_chip_info *chip, unsigned int val,
286 unsigned int *config)
290 if (val > 1024 || val < 1)
293 bits = find_closest(val, ina226_avg_tab,
294 ARRAY_SIZE(ina226_avg_tab));
296 chip->avg = ina226_avg_tab[bits];
298 *config &= ~INA226_AVG_MASK;
299 *config |= INA226_SHIFT_AVG(bits) & INA226_AVG_MASK;
304 /* Conversion times in uS */
305 static const int ina226_conv_time_tab[] = { 140, 204, 332, 588, 1100,
308 static int ina226_set_int_time_vbus(struct ina2xx_chip_info *chip,
309 unsigned int val_us, unsigned int *config)
313 if (val_us > 8244 || val_us < 140)
316 bits = find_closest(val_us, ina226_conv_time_tab,
317 ARRAY_SIZE(ina226_conv_time_tab));
319 chip->int_time_vbus = ina226_conv_time_tab[bits];
321 *config &= ~INA226_ITB_MASK;
322 *config |= INA226_SHIFT_ITB(bits) & INA226_ITB_MASK;
327 static int ina226_set_int_time_vshunt(struct ina2xx_chip_info *chip,
328 unsigned int val_us, unsigned int *config)
332 if (val_us > 8244 || val_us < 140)
335 bits = find_closest(val_us, ina226_conv_time_tab,
336 ARRAY_SIZE(ina226_conv_time_tab));
338 chip->int_time_vshunt = ina226_conv_time_tab[bits];
340 *config &= ~INA226_ITS_MASK;
341 *config |= INA226_SHIFT_ITS(bits) & INA226_ITS_MASK;
346 /* Conversion times in uS. */
347 static const int ina219_conv_time_tab_subsample[] = { 84, 148, 276, 532 };
348 static const int ina219_conv_time_tab_average[] = { 532, 1060, 2130, 4260,
349 8510, 17020, 34050, 68100};
351 static int ina219_lookup_int_time(unsigned int *val_us, int *bits)
353 if (*val_us > 68100 || *val_us < 84)
356 if (*val_us <= 532) {
357 *bits = find_closest(*val_us, ina219_conv_time_tab_subsample,
358 ARRAY_SIZE(ina219_conv_time_tab_subsample));
359 *val_us = ina219_conv_time_tab_subsample[*bits];
361 *bits = find_closest(*val_us, ina219_conv_time_tab_average,
362 ARRAY_SIZE(ina219_conv_time_tab_average));
363 *val_us = ina219_conv_time_tab_average[*bits];
370 static int ina219_set_int_time_vbus(struct ina2xx_chip_info *chip,
371 unsigned int val_us, unsigned int *config)
374 unsigned int val_us_best = val_us;
376 ret = ina219_lookup_int_time(&val_us_best, &bits);
380 chip->int_time_vbus = val_us_best;
382 *config &= ~INA219_ITB_MASK;
383 *config |= INA219_SHIFT_ITB(bits) & INA219_ITB_MASK;
388 static int ina219_set_int_time_vshunt(struct ina2xx_chip_info *chip,
389 unsigned int val_us, unsigned int *config)
392 unsigned int val_us_best = val_us;
394 ret = ina219_lookup_int_time(&val_us_best, &bits);
398 chip->int_time_vshunt = val_us_best;
400 *config &= ~INA219_ITS_MASK;
401 *config |= INA219_SHIFT_ITS(bits) & INA219_ITS_MASK;
406 static const int ina219_vbus_range_tab[] = { 1, 2 };
407 static int ina219_set_vbus_range_denom(struct ina2xx_chip_info *chip,
409 unsigned int *config)
412 chip->range_vbus = 32;
414 chip->range_vbus = 16;
418 *config &= ~INA219_BRNG_MASK;
419 *config |= INA219_SHIFT_BRNG(range == 1 ? 1 : 0) & INA219_BRNG_MASK;
424 static const int ina219_vshunt_gain_tab[] = { 125, 250, 500, 1000 };
425 static const int ina219_vshunt_gain_frac[] = {
426 125, 1000, 250, 1000, 500, 1000, 1000, 1000 };
428 static int ina219_set_vshunt_pga_gain(struct ina2xx_chip_info *chip,
430 unsigned int *config)
434 if (gain < 125 || gain > 1000)
437 bits = find_closest(gain, ina219_vshunt_gain_tab,
438 ARRAY_SIZE(ina219_vshunt_gain_tab));
440 chip->pga_gain_vshunt = ina219_vshunt_gain_tab[bits];
443 *config &= ~INA219_PGA_MASK;
444 *config |= INA219_SHIFT_PGA(bits) & INA219_PGA_MASK;
449 static int ina2xx_read_avail(struct iio_dev *indio_dev,
450 struct iio_chan_spec const *chan,
451 const int **vals, int *type, int *length,
455 case IIO_CHAN_INFO_HARDWAREGAIN:
456 switch (chan->address) {
457 case INA2XX_SHUNT_VOLTAGE:
458 *type = IIO_VAL_FRACTIONAL;
459 *length = sizeof(ina219_vshunt_gain_frac) / sizeof(int);
460 *vals = ina219_vshunt_gain_frac;
461 return IIO_AVAIL_LIST;
463 case INA2XX_BUS_VOLTAGE:
465 *length = sizeof(ina219_vbus_range_tab) / sizeof(int);
466 *vals = ina219_vbus_range_tab;
467 return IIO_AVAIL_LIST;
474 static int ina2xx_write_raw(struct iio_dev *indio_dev,
475 struct iio_chan_spec const *chan,
476 int val, int val2, long mask)
478 struct ina2xx_chip_info *chip = iio_priv(indio_dev);
479 unsigned int config, tmp;
482 if (iio_buffer_enabled(indio_dev))
485 mutex_lock(&chip->state_lock);
487 ret = regmap_read(chip->regmap, INA2XX_CONFIG, &config);
494 case IIO_CHAN_INFO_OVERSAMPLING_RATIO:
495 ret = ina226_set_average(chip, val, &tmp);
498 case IIO_CHAN_INFO_INT_TIME:
499 if (chip->config->chip_id == ina226) {
500 if (chan->address == INA2XX_SHUNT_VOLTAGE)
501 ret = ina226_set_int_time_vshunt(chip, val2,
504 ret = ina226_set_int_time_vbus(chip, val2,
507 if (chan->address == INA2XX_SHUNT_VOLTAGE)
508 ret = ina219_set_int_time_vshunt(chip, val2,
511 ret = ina219_set_int_time_vbus(chip, val2,
516 case IIO_CHAN_INFO_HARDWAREGAIN:
517 if (chan->address == INA2XX_SHUNT_VOLTAGE)
518 ret = ina219_set_vshunt_pga_gain(chip, val * 1000 +
521 ret = ina219_set_vbus_range_denom(chip, val, &tmp);
528 if (!ret && (tmp != config))
529 ret = regmap_write(chip->regmap, INA2XX_CONFIG, tmp);
531 mutex_unlock(&chip->state_lock);
536 static ssize_t ina2xx_allow_async_readout_show(struct device *dev,
537 struct device_attribute *attr,
540 struct ina2xx_chip_info *chip = iio_priv(dev_to_iio_dev(dev));
542 return sysfs_emit(buf, "%d\n", chip->allow_async_readout);
545 static ssize_t ina2xx_allow_async_readout_store(struct device *dev,
546 struct device_attribute *attr,
547 const char *buf, size_t len)
549 struct ina2xx_chip_info *chip = iio_priv(dev_to_iio_dev(dev));
553 ret = kstrtobool(buf, &val);
557 chip->allow_async_readout = val;
563 * Calibration register is set to the best value, which eliminates
564 * truncation errors on calculating current register in hardware.
565 * According to datasheet (INA 226: eq. 3, INA219: eq. 4) the best values
566 * are 2048 for ina226 and 4096 for ina219. They are hardcoded as
569 static int ina2xx_set_calibration(struct ina2xx_chip_info *chip)
571 return regmap_write(chip->regmap, INA2XX_CALIBRATION,
572 chip->config->calibration_value);
575 static int set_shunt_resistor(struct ina2xx_chip_info *chip, unsigned int val)
577 if (val == 0 || val > INT_MAX)
580 chip->shunt_resistor_uohm = val;
585 static ssize_t ina2xx_shunt_resistor_show(struct device *dev,
586 struct device_attribute *attr,
589 struct ina2xx_chip_info *chip = iio_priv(dev_to_iio_dev(dev));
590 int vals[2] = { chip->shunt_resistor_uohm, 1000000 };
592 return iio_format_value(buf, IIO_VAL_FRACTIONAL, 1, vals);
595 static ssize_t ina2xx_shunt_resistor_store(struct device *dev,
596 struct device_attribute *attr,
597 const char *buf, size_t len)
599 struct ina2xx_chip_info *chip = iio_priv(dev_to_iio_dev(dev));
600 int val, val_fract, ret;
602 ret = iio_str_to_fixpoint(buf, 100000, &val, &val_fract);
606 ret = set_shunt_resistor(chip, val * 1000000 + val_fract);
613 #define INA219_CHAN(_type, _index, _address) { \
615 .address = (_address), \
617 .channel = (_index), \
618 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | \
619 BIT(IIO_CHAN_INFO_SCALE), \
620 .info_mask_shared_by_dir = BIT(IIO_CHAN_INFO_SAMP_FREQ), \
621 .scan_index = (_index), \
626 .endianness = IIO_CPU, \
630 #define INA226_CHAN(_type, _index, _address) { \
632 .address = (_address), \
634 .channel = (_index), \
635 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | \
636 BIT(IIO_CHAN_INFO_SCALE), \
637 .info_mask_shared_by_dir = BIT(IIO_CHAN_INFO_SAMP_FREQ) | \
638 BIT(IIO_CHAN_INFO_OVERSAMPLING_RATIO), \
639 .scan_index = (_index), \
644 .endianness = IIO_CPU, \
649 * Sampling Freq is a consequence of the integration times of
650 * the Voltage channels.
652 #define INA219_CHAN_VOLTAGE(_index, _address, _shift) { \
653 .type = IIO_VOLTAGE, \
654 .address = (_address), \
656 .channel = (_index), \
657 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | \
658 BIT(IIO_CHAN_INFO_SCALE) | \
659 BIT(IIO_CHAN_INFO_INT_TIME) | \
660 BIT(IIO_CHAN_INFO_HARDWAREGAIN), \
661 .info_mask_separate_available = \
662 BIT(IIO_CHAN_INFO_HARDWAREGAIN), \
663 .info_mask_shared_by_dir = BIT(IIO_CHAN_INFO_SAMP_FREQ), \
664 .scan_index = (_index), \
668 .realbits = 16 - _shift, \
670 .endianness = IIO_LE, \
674 #define INA226_CHAN_VOLTAGE(_index, _address) { \
675 .type = IIO_VOLTAGE, \
676 .address = (_address), \
678 .channel = (_index), \
679 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | \
680 BIT(IIO_CHAN_INFO_SCALE) | \
681 BIT(IIO_CHAN_INFO_INT_TIME), \
682 .info_mask_shared_by_dir = BIT(IIO_CHAN_INFO_SAMP_FREQ) | \
683 BIT(IIO_CHAN_INFO_OVERSAMPLING_RATIO), \
684 .scan_index = (_index), \
689 .endianness = IIO_LE, \
694 static const struct iio_chan_spec ina226_channels[] = {
695 INA226_CHAN_VOLTAGE(0, INA2XX_SHUNT_VOLTAGE),
696 INA226_CHAN_VOLTAGE(1, INA2XX_BUS_VOLTAGE),
697 INA226_CHAN(IIO_POWER, 2, INA2XX_POWER),
698 INA226_CHAN(IIO_CURRENT, 3, INA2XX_CURRENT),
699 IIO_CHAN_SOFT_TIMESTAMP(4),
702 static const struct iio_chan_spec ina219_channels[] = {
703 INA219_CHAN_VOLTAGE(0, INA2XX_SHUNT_VOLTAGE, 0),
704 INA219_CHAN_VOLTAGE(1, INA2XX_BUS_VOLTAGE, INA219_BUS_VOLTAGE_SHIFT),
705 INA219_CHAN(IIO_POWER, 2, INA2XX_POWER),
706 INA219_CHAN(IIO_CURRENT, 3, INA2XX_CURRENT),
707 IIO_CHAN_SOFT_TIMESTAMP(4),
710 static int ina2xx_conversion_ready(struct iio_dev *indio_dev)
712 struct ina2xx_chip_info *chip = iio_priv(indio_dev);
717 * Because the timer thread and the chip conversion clock
718 * are asynchronous, the period difference will eventually
719 * result in reading V[k-1] again, or skip V[k] at time Tk.
720 * In order to resync the timer with the conversion process
721 * we check the ConVersionReadyFlag.
722 * On hardware that supports using the ALERT pin to toggle a
723 * GPIO a triggered buffer could be used instead.
724 * For now, we do an extra read of the MASK_ENABLE register (INA226)
725 * resp. the BUS_VOLTAGE register (INA219).
727 if (chip->config->chip_id == ina226) {
728 ret = regmap_read(chip->regmap,
729 INA226_MASK_ENABLE, &alert);
730 alert &= INA226_CVRF;
732 ret = regmap_read(chip->regmap,
733 INA2XX_BUS_VOLTAGE, &alert);
734 alert &= INA219_CNVR;
743 static int ina2xx_work_buffer(struct iio_dev *indio_dev)
745 struct ina2xx_chip_info *chip = iio_priv(indio_dev);
749 time = iio_get_time_ns(indio_dev);
752 * Single register reads: bulk_read will not work with ina226/219
753 * as there is no auto-increment of the register pointer.
755 for_each_set_bit(bit, indio_dev->active_scan_mask,
756 indio_dev->masklength) {
759 ret = regmap_read(chip->regmap,
760 INA2XX_SHUNT_VOLTAGE + bit, &val);
764 chip->scan.chan[i++] = val;
767 iio_push_to_buffers_with_timestamp(indio_dev, &chip->scan, time);
772 static int ina2xx_capture_thread(void *data)
774 struct iio_dev *indio_dev = data;
775 struct ina2xx_chip_info *chip = iio_priv(indio_dev);
776 int sampling_us = SAMPLING_PERIOD(chip);
778 struct timespec64 next, now, delta;
782 * Poll a bit faster than the chip internal Fs, in case
783 * we wish to sync with the conversion ready flag.
785 if (!chip->allow_async_readout)
788 ktime_get_ts64(&next);
791 while (!chip->allow_async_readout) {
792 ret = ina2xx_conversion_ready(indio_dev);
797 * If the conversion was not yet finished,
798 * reset the reference timestamp.
801 ktime_get_ts64(&next);
806 ret = ina2xx_work_buffer(indio_dev);
810 ktime_get_ts64(&now);
813 * Advance the timestamp for the next poll by one sampling
814 * interval, and sleep for the remainder (next - now)
815 * In case "next" has already passed, the interval is added
816 * multiple times, i.e. samples are dropped.
819 timespec64_add_ns(&next, 1000 * sampling_us);
820 delta = timespec64_sub(next, now);
821 delay_us = div_s64(timespec64_to_ns(&delta), 1000);
822 } while (delay_us <= 0);
824 usleep_range(delay_us, (delay_us * 3) >> 1);
826 } while (!kthread_should_stop());
831 static int ina2xx_buffer_enable(struct iio_dev *indio_dev)
833 struct ina2xx_chip_info *chip = iio_priv(indio_dev);
834 unsigned int sampling_us = SAMPLING_PERIOD(chip);
835 struct task_struct *task;
837 dev_dbg(&indio_dev->dev, "Enabling buffer w/ scan_mask %02x, freq = %d, avg =%u\n",
838 (unsigned int)(*indio_dev->active_scan_mask),
839 1000000 / sampling_us, chip->avg);
841 dev_dbg(&indio_dev->dev, "Expected work period: %u us\n", sampling_us);
842 dev_dbg(&indio_dev->dev, "Async readout mode: %d\n",
843 chip->allow_async_readout);
845 task = kthread_run(ina2xx_capture_thread, (void *)indio_dev,
846 "%s:%d-%uus", indio_dev->name,
847 iio_device_id(indio_dev),
850 return PTR_ERR(task);
857 static int ina2xx_buffer_disable(struct iio_dev *indio_dev)
859 struct ina2xx_chip_info *chip = iio_priv(indio_dev);
862 kthread_stop(chip->task);
869 static const struct iio_buffer_setup_ops ina2xx_setup_ops = {
870 .postenable = &ina2xx_buffer_enable,
871 .predisable = &ina2xx_buffer_disable,
874 static int ina2xx_debug_reg(struct iio_dev *indio_dev,
875 unsigned reg, unsigned writeval, unsigned *readval)
877 struct ina2xx_chip_info *chip = iio_priv(indio_dev);
880 return regmap_write(chip->regmap, reg, writeval);
882 return regmap_read(chip->regmap, reg, readval);
885 /* Possible integration times for vshunt and vbus */
886 static IIO_CONST_ATTR_NAMED(ina219_integration_time_available,
887 integration_time_available,
888 "0.000084 0.000148 0.000276 0.000532 0.001060 0.002130 0.004260 0.008510 0.017020 0.034050 0.068100");
890 static IIO_CONST_ATTR_NAMED(ina226_integration_time_available,
891 integration_time_available,
892 "0.000140 0.000204 0.000332 0.000588 0.001100 0.002116 0.004156 0.008244");
894 static IIO_DEVICE_ATTR(in_allow_async_readout, S_IRUGO | S_IWUSR,
895 ina2xx_allow_async_readout_show,
896 ina2xx_allow_async_readout_store, 0);
898 static IIO_DEVICE_ATTR(in_shunt_resistor, S_IRUGO | S_IWUSR,
899 ina2xx_shunt_resistor_show,
900 ina2xx_shunt_resistor_store, 0);
902 static struct attribute *ina219_attributes[] = {
903 &iio_dev_attr_in_allow_async_readout.dev_attr.attr,
904 &iio_const_attr_ina219_integration_time_available.dev_attr.attr,
905 &iio_dev_attr_in_shunt_resistor.dev_attr.attr,
909 static struct attribute *ina226_attributes[] = {
910 &iio_dev_attr_in_allow_async_readout.dev_attr.attr,
911 &iio_const_attr_ina226_integration_time_available.dev_attr.attr,
912 &iio_dev_attr_in_shunt_resistor.dev_attr.attr,
916 static const struct attribute_group ina219_attribute_group = {
917 .attrs = ina219_attributes,
920 static const struct attribute_group ina226_attribute_group = {
921 .attrs = ina226_attributes,
924 static const struct iio_info ina219_info = {
925 .attrs = &ina219_attribute_group,
926 .read_raw = ina2xx_read_raw,
927 .read_avail = ina2xx_read_avail,
928 .write_raw = ina2xx_write_raw,
929 .debugfs_reg_access = ina2xx_debug_reg,
932 static const struct iio_info ina226_info = {
933 .attrs = &ina226_attribute_group,
934 .read_raw = ina2xx_read_raw,
935 .write_raw = ina2xx_write_raw,
936 .debugfs_reg_access = ina2xx_debug_reg,
939 /* Initialize the configuration and calibration registers. */
940 static int ina2xx_init(struct ina2xx_chip_info *chip, unsigned int config)
942 int ret = regmap_write(chip->regmap, INA2XX_CONFIG, config);
946 return ina2xx_set_calibration(chip);
949 static int ina2xx_probe(struct i2c_client *client)
951 const struct i2c_device_id *id = i2c_client_get_device_id(client);
952 struct ina2xx_chip_info *chip;
953 struct iio_dev *indio_dev;
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 = (uintptr_t)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;
1018 if (id->driver_data == ina226) {
1019 indio_dev->channels = ina226_channels;
1020 indio_dev->num_channels = ARRAY_SIZE(ina226_channels);
1021 indio_dev->info = &ina226_info;
1023 indio_dev->channels = ina219_channels;
1024 indio_dev->num_channels = ARRAY_SIZE(ina219_channels);
1025 indio_dev->info = &ina219_info;
1027 indio_dev->name = id->name;
1029 ret = devm_iio_kfifo_buffer_setup(&client->dev, indio_dev,
1034 return iio_device_register(indio_dev);
1037 static void ina2xx_remove(struct i2c_client *client)
1039 struct iio_dev *indio_dev = i2c_get_clientdata(client);
1040 struct ina2xx_chip_info *chip = iio_priv(indio_dev);
1043 iio_device_unregister(indio_dev);
1046 ret = regmap_update_bits(chip->regmap, INA2XX_CONFIG,
1047 INA2XX_MODE_MASK, 0);
1049 dev_warn(&client->dev, "Failed to power down device (%pe)\n",
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_new = ina2xx_probe,
1094 .remove = ina2xx_remove,
1095 .id_table = ina2xx_id,
1097 module_i2c_driver(ina2xx_driver);
1100 MODULE_DESCRIPTION("Texas Instruments INA2XX ADC driver");
1101 MODULE_LICENSE("GPL v2");