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;
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;
259 case IIO_CHAN_INFO_HARDWAREGAIN:
260 switch (chan->address) {
261 case INA2XX_SHUNT_VOLTAGE:
262 *val = chip->pga_gain_vshunt;
264 return IIO_VAL_FRACTIONAL;
266 case INA2XX_BUS_VOLTAGE:
267 *val = chip->range_vbus == 32 ? 1 : 2;
276 * Available averaging rates for ina226. The indices correspond with
277 * the bit values expected by the chip (according to the ina226 datasheet,
278 * table 3 AVG bit settings, found at
279 * http://www.ti.com/lit/ds/symlink/ina226.pdf.
281 static const int ina226_avg_tab[] = { 1, 4, 16, 64, 128, 256, 512, 1024 };
283 static int ina226_set_average(struct ina2xx_chip_info *chip, unsigned int val,
284 unsigned int *config)
288 if (val > 1024 || val < 1)
291 bits = find_closest(val, ina226_avg_tab,
292 ARRAY_SIZE(ina226_avg_tab));
294 chip->avg = ina226_avg_tab[bits];
296 *config &= ~INA226_AVG_MASK;
297 *config |= INA226_SHIFT_AVG(bits) & INA226_AVG_MASK;
302 /* Conversion times in uS */
303 static const int ina226_conv_time_tab[] = { 140, 204, 332, 588, 1100,
306 static int ina226_set_int_time_vbus(struct ina2xx_chip_info *chip,
307 unsigned int val_us, unsigned int *config)
311 if (val_us > 8244 || val_us < 140)
314 bits = find_closest(val_us, ina226_conv_time_tab,
315 ARRAY_SIZE(ina226_conv_time_tab));
317 chip->int_time_vbus = ina226_conv_time_tab[bits];
319 *config &= ~INA226_ITB_MASK;
320 *config |= INA226_SHIFT_ITB(bits) & INA226_ITB_MASK;
325 static int ina226_set_int_time_vshunt(struct ina2xx_chip_info *chip,
326 unsigned int val_us, unsigned int *config)
330 if (val_us > 8244 || val_us < 140)
333 bits = find_closest(val_us, ina226_conv_time_tab,
334 ARRAY_SIZE(ina226_conv_time_tab));
336 chip->int_time_vshunt = ina226_conv_time_tab[bits];
338 *config &= ~INA226_ITS_MASK;
339 *config |= INA226_SHIFT_ITS(bits) & INA226_ITS_MASK;
344 /* Conversion times in uS. */
345 static const int ina219_conv_time_tab_subsample[] = { 84, 148, 276, 532 };
346 static const int ina219_conv_time_tab_average[] = { 532, 1060, 2130, 4260,
347 8510, 17020, 34050, 68100};
349 static int ina219_lookup_int_time(unsigned int *val_us, int *bits)
351 if (*val_us > 68100 || *val_us < 84)
354 if (*val_us <= 532) {
355 *bits = find_closest(*val_us, ina219_conv_time_tab_subsample,
356 ARRAY_SIZE(ina219_conv_time_tab_subsample));
357 *val_us = ina219_conv_time_tab_subsample[*bits];
359 *bits = find_closest(*val_us, ina219_conv_time_tab_average,
360 ARRAY_SIZE(ina219_conv_time_tab_average));
361 *val_us = ina219_conv_time_tab_average[*bits];
368 static int ina219_set_int_time_vbus(struct ina2xx_chip_info *chip,
369 unsigned int val_us, unsigned int *config)
372 unsigned int val_us_best = val_us;
374 ret = ina219_lookup_int_time(&val_us_best, &bits);
378 chip->int_time_vbus = val_us_best;
380 *config &= ~INA219_ITB_MASK;
381 *config |= INA219_SHIFT_ITB(bits) & INA219_ITB_MASK;
386 static int ina219_set_int_time_vshunt(struct ina2xx_chip_info *chip,
387 unsigned int val_us, unsigned int *config)
390 unsigned int val_us_best = val_us;
392 ret = ina219_lookup_int_time(&val_us_best, &bits);
396 chip->int_time_vshunt = val_us_best;
398 *config &= ~INA219_ITS_MASK;
399 *config |= INA219_SHIFT_ITS(bits) & INA219_ITS_MASK;
404 static const int ina219_vbus_range_tab[] = { 1, 2 };
405 static int ina219_set_vbus_range_denom(struct ina2xx_chip_info *chip,
407 unsigned int *config)
410 chip->range_vbus = 32;
412 chip->range_vbus = 16;
416 *config &= ~INA219_BRNG_MASK;
417 *config |= INA219_SHIFT_BRNG(range == 1 ? 1 : 0) & INA219_BRNG_MASK;
422 static const int ina219_vshunt_gain_tab[] = { 125, 250, 500, 1000 };
423 static const int ina219_vshunt_gain_frac[] = {
424 125, 1000, 250, 1000, 500, 1000, 1000, 1000 };
426 static int ina219_set_vshunt_pga_gain(struct ina2xx_chip_info *chip,
428 unsigned int *config)
432 if (gain < 125 || gain > 1000)
435 bits = find_closest(gain, ina219_vshunt_gain_tab,
436 ARRAY_SIZE(ina219_vshunt_gain_tab));
438 chip->pga_gain_vshunt = ina219_vshunt_gain_tab[bits];
441 *config &= ~INA219_PGA_MASK;
442 *config |= INA219_SHIFT_PGA(bits) & INA219_PGA_MASK;
447 static int ina2xx_read_avail(struct iio_dev *indio_dev,
448 struct iio_chan_spec const *chan,
449 const int **vals, int *type, int *length,
453 case IIO_CHAN_INFO_HARDWAREGAIN:
454 switch (chan->address) {
455 case INA2XX_SHUNT_VOLTAGE:
456 *type = IIO_VAL_FRACTIONAL;
457 *length = sizeof(ina219_vshunt_gain_frac) / sizeof(int);
458 *vals = ina219_vshunt_gain_frac;
459 return IIO_AVAIL_LIST;
461 case INA2XX_BUS_VOLTAGE:
463 *length = sizeof(ina219_vbus_range_tab) / sizeof(int);
464 *vals = ina219_vbus_range_tab;
465 return IIO_AVAIL_LIST;
472 static int ina2xx_write_raw(struct iio_dev *indio_dev,
473 struct iio_chan_spec const *chan,
474 int val, int val2, long mask)
476 struct ina2xx_chip_info *chip = iio_priv(indio_dev);
477 unsigned int config, tmp;
480 if (iio_buffer_enabled(indio_dev))
483 mutex_lock(&chip->state_lock);
485 ret = regmap_read(chip->regmap, INA2XX_CONFIG, &config);
492 case IIO_CHAN_INFO_OVERSAMPLING_RATIO:
493 ret = ina226_set_average(chip, val, &tmp);
496 case IIO_CHAN_INFO_INT_TIME:
497 if (chip->config->chip_id == ina226) {
498 if (chan->address == INA2XX_SHUNT_VOLTAGE)
499 ret = ina226_set_int_time_vshunt(chip, val2,
502 ret = ina226_set_int_time_vbus(chip, val2,
505 if (chan->address == INA2XX_SHUNT_VOLTAGE)
506 ret = ina219_set_int_time_vshunt(chip, val2,
509 ret = ina219_set_int_time_vbus(chip, val2,
514 case IIO_CHAN_INFO_HARDWAREGAIN:
515 if (chan->address == INA2XX_SHUNT_VOLTAGE)
516 ret = ina219_set_vshunt_pga_gain(chip, val * 1000 +
519 ret = ina219_set_vbus_range_denom(chip, val, &tmp);
526 if (!ret && (tmp != config))
527 ret = regmap_write(chip->regmap, INA2XX_CONFIG, tmp);
529 mutex_unlock(&chip->state_lock);
534 static ssize_t ina2xx_allow_async_readout_show(struct device *dev,
535 struct device_attribute *attr,
538 struct ina2xx_chip_info *chip = iio_priv(dev_to_iio_dev(dev));
540 return sprintf(buf, "%d\n", chip->allow_async_readout);
543 static ssize_t ina2xx_allow_async_readout_store(struct device *dev,
544 struct device_attribute *attr,
545 const char *buf, size_t len)
547 struct ina2xx_chip_info *chip = iio_priv(dev_to_iio_dev(dev));
551 ret = strtobool((const char *) buf, &val);
555 chip->allow_async_readout = val;
561 * Calibration register is set to the best value, which eliminates
562 * truncation errors on calculating current register in hardware.
563 * According to datasheet (INA 226: eq. 3, INA219: eq. 4) the best values
564 * are 2048 for ina226 and 4096 for ina219. They are hardcoded as
567 static int ina2xx_set_calibration(struct ina2xx_chip_info *chip)
569 return regmap_write(chip->regmap, INA2XX_CALIBRATION,
570 chip->config->calibration_value);
573 static int set_shunt_resistor(struct ina2xx_chip_info *chip, unsigned int val)
575 if (val == 0 || val > INT_MAX)
578 chip->shunt_resistor_uohm = val;
583 static ssize_t ina2xx_shunt_resistor_show(struct device *dev,
584 struct device_attribute *attr,
587 struct ina2xx_chip_info *chip = iio_priv(dev_to_iio_dev(dev));
588 int vals[2] = { chip->shunt_resistor_uohm, 1000000 };
590 return iio_format_value(buf, IIO_VAL_FRACTIONAL, 1, vals);
593 static ssize_t ina2xx_shunt_resistor_store(struct device *dev,
594 struct device_attribute *attr,
595 const char *buf, size_t len)
597 struct ina2xx_chip_info *chip = iio_priv(dev_to_iio_dev(dev));
598 int val, val_fract, ret;
600 ret = iio_str_to_fixpoint(buf, 100000, &val, &val_fract);
604 ret = set_shunt_resistor(chip, val * 1000000 + val_fract);
611 #define INA219_CHAN(_type, _index, _address) { \
613 .address = (_address), \
615 .channel = (_index), \
616 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | \
617 BIT(IIO_CHAN_INFO_SCALE), \
618 .info_mask_shared_by_dir = BIT(IIO_CHAN_INFO_SAMP_FREQ), \
619 .scan_index = (_index), \
624 .endianness = IIO_CPU, \
628 #define INA226_CHAN(_type, _index, _address) { \
630 .address = (_address), \
632 .channel = (_index), \
633 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | \
634 BIT(IIO_CHAN_INFO_SCALE), \
635 .info_mask_shared_by_dir = BIT(IIO_CHAN_INFO_SAMP_FREQ) | \
636 BIT(IIO_CHAN_INFO_OVERSAMPLING_RATIO), \
637 .scan_index = (_index), \
642 .endianness = IIO_CPU, \
647 * Sampling Freq is a consequence of the integration times of
648 * the Voltage channels.
650 #define INA219_CHAN_VOLTAGE(_index, _address, _shift) { \
651 .type = IIO_VOLTAGE, \
652 .address = (_address), \
654 .channel = (_index), \
655 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | \
656 BIT(IIO_CHAN_INFO_SCALE) | \
657 BIT(IIO_CHAN_INFO_INT_TIME) | \
658 BIT(IIO_CHAN_INFO_HARDWAREGAIN), \
659 .info_mask_separate_available = \
660 BIT(IIO_CHAN_INFO_HARDWAREGAIN), \
661 .info_mask_shared_by_dir = BIT(IIO_CHAN_INFO_SAMP_FREQ), \
662 .scan_index = (_index), \
666 .realbits = 16 - _shift, \
668 .endianness = IIO_LE, \
672 #define INA226_CHAN_VOLTAGE(_index, _address) { \
673 .type = IIO_VOLTAGE, \
674 .address = (_address), \
676 .channel = (_index), \
677 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | \
678 BIT(IIO_CHAN_INFO_SCALE) | \
679 BIT(IIO_CHAN_INFO_INT_TIME), \
680 .info_mask_shared_by_dir = BIT(IIO_CHAN_INFO_SAMP_FREQ) | \
681 BIT(IIO_CHAN_INFO_OVERSAMPLING_RATIO), \
682 .scan_index = (_index), \
687 .endianness = IIO_LE, \
692 static const struct iio_chan_spec ina226_channels[] = {
693 INA226_CHAN_VOLTAGE(0, INA2XX_SHUNT_VOLTAGE),
694 INA226_CHAN_VOLTAGE(1, INA2XX_BUS_VOLTAGE),
695 INA226_CHAN(IIO_POWER, 2, INA2XX_POWER),
696 INA226_CHAN(IIO_CURRENT, 3, INA2XX_CURRENT),
697 IIO_CHAN_SOFT_TIMESTAMP(4),
700 static const struct iio_chan_spec ina219_channels[] = {
701 INA219_CHAN_VOLTAGE(0, INA2XX_SHUNT_VOLTAGE, 0),
702 INA219_CHAN_VOLTAGE(1, INA2XX_BUS_VOLTAGE, INA219_BUS_VOLTAGE_SHIFT),
703 INA219_CHAN(IIO_POWER, 2, INA2XX_POWER),
704 INA219_CHAN(IIO_CURRENT, 3, INA2XX_CURRENT),
705 IIO_CHAN_SOFT_TIMESTAMP(4),
708 static int ina2xx_conversion_ready(struct iio_dev *indio_dev)
710 struct ina2xx_chip_info *chip = iio_priv(indio_dev);
715 * Because the timer thread and the chip conversion clock
716 * are asynchronous, the period difference will eventually
717 * result in reading V[k-1] again, or skip V[k] at time Tk.
718 * In order to resync the timer with the conversion process
719 * we check the ConVersionReadyFlag.
720 * On hardware that supports using the ALERT pin to toggle a
721 * GPIO a triggered buffer could be used instead.
722 * For now, we do an extra read of the MASK_ENABLE register (INA226)
723 * resp. the BUS_VOLTAGE register (INA219).
725 if (chip->config->chip_id == ina226) {
726 ret = regmap_read(chip->regmap,
727 INA226_MASK_ENABLE, &alert);
728 alert &= INA226_CVRF;
730 ret = regmap_read(chip->regmap,
731 INA2XX_BUS_VOLTAGE, &alert);
732 alert &= INA219_CNVR;
741 static int ina2xx_work_buffer(struct iio_dev *indio_dev)
743 struct ina2xx_chip_info *chip = iio_priv(indio_dev);
747 time = iio_get_time_ns(indio_dev);
750 * Single register reads: bulk_read will not work with ina226/219
751 * as there is no auto-increment of the register pointer.
753 for_each_set_bit(bit, indio_dev->active_scan_mask,
754 indio_dev->masklength) {
757 ret = regmap_read(chip->regmap,
758 INA2XX_SHUNT_VOLTAGE + bit, &val);
762 chip->scan.chan[i++] = val;
765 iio_push_to_buffers_with_timestamp(indio_dev, &chip->scan, time);
770 static int ina2xx_capture_thread(void *data)
772 struct iio_dev *indio_dev = data;
773 struct ina2xx_chip_info *chip = iio_priv(indio_dev);
774 int sampling_us = SAMPLING_PERIOD(chip);
776 struct timespec64 next, now, delta;
780 * Poll a bit faster than the chip internal Fs, in case
781 * we wish to sync with the conversion ready flag.
783 if (!chip->allow_async_readout)
786 ktime_get_ts64(&next);
789 while (!chip->allow_async_readout) {
790 ret = ina2xx_conversion_ready(indio_dev);
795 * If the conversion was not yet finished,
796 * reset the reference timestamp.
799 ktime_get_ts64(&next);
804 ret = ina2xx_work_buffer(indio_dev);
808 ktime_get_ts64(&now);
811 * Advance the timestamp for the next poll by one sampling
812 * interval, and sleep for the remainder (next - now)
813 * In case "next" has already passed, the interval is added
814 * multiple times, i.e. samples are dropped.
817 timespec64_add_ns(&next, 1000 * sampling_us);
818 delta = timespec64_sub(next, now);
819 delay_us = div_s64(timespec64_to_ns(&delta), 1000);
820 } while (delay_us <= 0);
822 usleep_range(delay_us, (delay_us * 3) >> 1);
824 } while (!kthread_should_stop());
829 static int ina2xx_buffer_enable(struct iio_dev *indio_dev)
831 struct ina2xx_chip_info *chip = iio_priv(indio_dev);
832 unsigned int sampling_us = SAMPLING_PERIOD(chip);
833 struct task_struct *task;
835 dev_dbg(&indio_dev->dev, "Enabling buffer w/ scan_mask %02x, freq = %d, avg =%u\n",
836 (unsigned int)(*indio_dev->active_scan_mask),
837 1000000 / sampling_us, chip->avg);
839 dev_dbg(&indio_dev->dev, "Expected work period: %u us\n", sampling_us);
840 dev_dbg(&indio_dev->dev, "Async readout mode: %d\n",
841 chip->allow_async_readout);
843 task = kthread_create(ina2xx_capture_thread, (void *)indio_dev,
844 "%s:%d-%uus", indio_dev->name, indio_dev->id,
847 return PTR_ERR(task);
849 get_task_struct(task);
850 wake_up_process(task);
856 static int ina2xx_buffer_disable(struct iio_dev *indio_dev)
858 struct ina2xx_chip_info *chip = iio_priv(indio_dev);
861 kthread_stop(chip->task);
862 put_task_struct(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,
950 const struct i2c_device_id *id)
952 struct ina2xx_chip_info *chip;
953 struct iio_dev *indio_dev;
954 struct iio_buffer *buffer;
956 enum ina2xx_ids type;
959 indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*chip));
963 chip = iio_priv(indio_dev);
965 /* This is only used for device removal purposes. */
966 i2c_set_clientdata(client, indio_dev);
968 chip->regmap = devm_regmap_init_i2c(client, &ina2xx_regmap_config);
969 if (IS_ERR(chip->regmap)) {
970 dev_err(&client->dev, "failed to allocate register map\n");
971 return PTR_ERR(chip->regmap);
974 if (client->dev.of_node)
975 type = (enum ina2xx_ids)of_device_get_match_data(&client->dev);
977 type = id->driver_data;
978 chip->config = &ina2xx_config[type];
980 mutex_init(&chip->state_lock);
982 if (of_property_read_u32(client->dev.of_node,
983 "shunt-resistor", &val) < 0) {
984 struct ina2xx_platform_data *pdata =
985 dev_get_platdata(&client->dev);
988 val = pdata->shunt_uohms;
990 val = INA2XX_RSHUNT_DEFAULT;
993 ret = set_shunt_resistor(chip, val);
997 /* Patch the current config register with default. */
998 val = chip->config->config_default;
1000 if (id->driver_data == ina226) {
1001 ina226_set_average(chip, INA226_DEFAULT_AVG, &val);
1002 ina226_set_int_time_vbus(chip, INA226_DEFAULT_IT, &val);
1003 ina226_set_int_time_vshunt(chip, INA226_DEFAULT_IT, &val);
1006 ina219_set_int_time_vbus(chip, INA219_DEFAULT_IT, &val);
1007 ina219_set_int_time_vshunt(chip, INA219_DEFAULT_IT, &val);
1008 ina219_set_vbus_range_denom(chip, INA219_DEFAULT_BRNG, &val);
1009 ina219_set_vshunt_pga_gain(chip, INA219_DEFAULT_PGA, &val);
1012 ret = ina2xx_init(chip, val);
1014 dev_err(&client->dev, "error configuring the device\n");
1018 indio_dev->modes = INDIO_DIRECT_MODE | INDIO_BUFFER_SOFTWARE;
1019 indio_dev->dev.parent = &client->dev;
1020 indio_dev->dev.of_node = client->dev.of_node;
1021 if (id->driver_data == ina226) {
1022 indio_dev->channels = ina226_channels;
1023 indio_dev->num_channels = ARRAY_SIZE(ina226_channels);
1024 indio_dev->info = &ina226_info;
1026 indio_dev->channels = ina219_channels;
1027 indio_dev->num_channels = ARRAY_SIZE(ina219_channels);
1028 indio_dev->info = &ina219_info;
1030 indio_dev->name = id->name;
1031 indio_dev->setup_ops = &ina2xx_setup_ops;
1033 buffer = devm_iio_kfifo_allocate(&indio_dev->dev);
1037 iio_device_attach_buffer(indio_dev, buffer);
1039 return iio_device_register(indio_dev);
1042 static int ina2xx_remove(struct i2c_client *client)
1044 struct iio_dev *indio_dev = i2c_get_clientdata(client);
1045 struct ina2xx_chip_info *chip = iio_priv(indio_dev);
1047 iio_device_unregister(indio_dev);
1050 return regmap_update_bits(chip->regmap, INA2XX_CONFIG,
1051 INA2XX_MODE_MASK, 0);
1054 static const struct i2c_device_id ina2xx_id[] = {
1062 MODULE_DEVICE_TABLE(i2c, ina2xx_id);
1064 static const struct of_device_id ina2xx_of_match[] = {
1066 .compatible = "ti,ina219",
1067 .data = (void *)ina219
1070 .compatible = "ti,ina220",
1071 .data = (void *)ina219
1074 .compatible = "ti,ina226",
1075 .data = (void *)ina226
1078 .compatible = "ti,ina230",
1079 .data = (void *)ina226
1082 .compatible = "ti,ina231",
1083 .data = (void *)ina226
1087 MODULE_DEVICE_TABLE(of, ina2xx_of_match);
1089 static struct i2c_driver ina2xx_driver = {
1091 .name = KBUILD_MODNAME,
1092 .of_match_table = ina2xx_of_match,
1094 .probe = ina2xx_probe,
1095 .remove = ina2xx_remove,
1096 .id_table = ina2xx_id,
1098 module_i2c_driver(ina2xx_driver);
1100 MODULE_AUTHOR("Marc Titinger <marc.titinger@baylibre.com>");
1101 MODULE_DESCRIPTION("Texas Instruments INA2XX ADC driver");
1102 MODULE_LICENSE("GPL v2");