GNU Linux-libre 4.9.337-gnu1
[releases.git] / drivers / iio / adc / ina2xx-adc.c
1 /*
2  * INA2XX Current and Power Monitors
3  *
4  * Copyright 2015 Baylibre SAS.
5  *
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.
9  *
10  * Based on linux/drivers/iio/adc/ad7291.c
11  * Copyright 2010-2011 Analog Devices Inc.
12  *
13  * Based on linux/drivers/hwmon/ina2xx.c
14  * Copyright 2012 Lothar Felten <l-felten@ti.com>
15  *
16  * Licensed under the GPL-2 or later.
17  *
18  * IIO driver for INA219-220-226-230-231
19  *
20  * Configurable 7-bit I2C slave address from 0x40 to 0x4F
21  */
22
23 #include <linux/delay.h>
24 #include <linux/i2c.h>
25 #include <linux/iio/kfifo_buf.h>
26 #include <linux/iio/sysfs.h>
27 #include <linux/kthread.h>
28 #include <linux/module.h>
29 #include <linux/regmap.h>
30 #include <linux/util_macros.h>
31
32 #include <linux/platform_data/ina2xx.h>
33
34 /* INA2XX registers definition */
35 #define INA2XX_CONFIG                   0x00
36 #define INA2XX_SHUNT_VOLTAGE            0x01    /* readonly */
37 #define INA2XX_BUS_VOLTAGE              0x02    /* readonly */
38 #define INA2XX_POWER                    0x03    /* readonly */
39 #define INA2XX_CURRENT                  0x04    /* readonly */
40 #define INA2XX_CALIBRATION              0x05
41
42 #define INA226_ALERT_MASK               GENMASK(2, 1)
43 #define INA266_CVRF                     BIT(3)
44
45 #define INA2XX_MAX_REGISTERS            8
46
47 /* settings - depend on use case */
48 #define INA219_CONFIG_DEFAULT           0x399F  /* PGA=8 */
49 #define INA226_CONFIG_DEFAULT           0x4327
50 #define INA226_DEFAULT_AVG              4
51 #define INA226_DEFAULT_IT               1110
52
53 #define INA2XX_RSHUNT_DEFAULT           10000
54
55 /*
56  * bit mask for reading the averaging setting in the configuration register
57  * FIXME: use regmap_fields.
58  */
59 #define INA2XX_MODE_MASK        GENMASK(3, 0)
60
61 #define INA226_AVG_MASK         GENMASK(11, 9)
62 #define INA226_SHIFT_AVG(val)   ((val) << 9)
63
64 /* Integration time for VBus */
65 #define INA226_ITB_MASK         GENMASK(8, 6)
66 #define INA226_SHIFT_ITB(val)   ((val) << 6)
67
68 /* Integration time for VShunt */
69 #define INA226_ITS_MASK         GENMASK(5, 3)
70 #define INA226_SHIFT_ITS(val)   ((val) << 3)
71
72 /* Cosmetic macro giving the sampling period for a full P=UxI cycle */
73 #define SAMPLING_PERIOD(c)      ((c->int_time_vbus + c->int_time_vshunt) \
74                                  * c->avg)
75
76 static bool ina2xx_is_writeable_reg(struct device *dev, unsigned int reg)
77 {
78         return (reg == INA2XX_CONFIG) || (reg > INA2XX_CURRENT);
79 }
80
81 static bool ina2xx_is_volatile_reg(struct device *dev, unsigned int reg)
82 {
83         return (reg != INA2XX_CONFIG);
84 }
85
86 static inline bool is_signed_reg(unsigned int reg)
87 {
88         return (reg == INA2XX_SHUNT_VOLTAGE) || (reg == INA2XX_CURRENT);
89 }
90
91 static const struct regmap_config ina2xx_regmap_config = {
92         .reg_bits = 8,
93         .val_bits = 16,
94         .max_register = INA2XX_MAX_REGISTERS,
95         .writeable_reg = ina2xx_is_writeable_reg,
96         .volatile_reg = ina2xx_is_volatile_reg,
97 };
98
99 enum ina2xx_ids { ina219, ina226 };
100
101 struct ina2xx_config {
102         u16 config_default;
103         int calibration_factor;
104         int shunt_div;
105         int bus_voltage_shift;
106         int bus_voltage_lsb;    /* uV */
107         int power_lsb;          /* uW */
108 };
109
110 struct ina2xx_chip_info {
111         struct regmap *regmap;
112         struct task_struct *task;
113         const struct ina2xx_config *config;
114         struct mutex state_lock;
115         unsigned int shunt_resistor;
116         int avg;
117         int int_time_vbus; /* Bus voltage integration time uS */
118         int int_time_vshunt; /* Shunt voltage integration time uS */
119         bool allow_async_readout;
120         /* data buffer needs space for channel data and timestamp */
121         struct {
122                 u16 chan[4];
123                 u64 ts __aligned(8);
124         } scan;
125 };
126
127 static const struct ina2xx_config ina2xx_config[] = {
128         [ina219] = {
129                 .config_default = INA219_CONFIG_DEFAULT,
130                 .calibration_factor = 40960000,
131                 .shunt_div = 100,
132                 .bus_voltage_shift = 3,
133                 .bus_voltage_lsb = 4000,
134                 .power_lsb = 20000,
135         },
136         [ina226] = {
137                 .config_default = INA226_CONFIG_DEFAULT,
138                 .calibration_factor = 5120000,
139                 .shunt_div = 400,
140                 .bus_voltage_shift = 0,
141                 .bus_voltage_lsb = 1250,
142                 .power_lsb = 25000,
143         },
144 };
145
146 static int ina2xx_read_raw(struct iio_dev *indio_dev,
147                            struct iio_chan_spec const *chan,
148                            int *val, int *val2, long mask)
149 {
150         int ret;
151         struct ina2xx_chip_info *chip = iio_priv(indio_dev);
152         unsigned int regval;
153
154         switch (mask) {
155         case IIO_CHAN_INFO_RAW:
156                 ret = regmap_read(chip->regmap, chan->address, &regval);
157                 if (ret)
158                         return ret;
159
160                 if (is_signed_reg(chan->address))
161                         *val = (s16) regval;
162                 else
163                         *val  = regval;
164
165                 return IIO_VAL_INT;
166
167         case IIO_CHAN_INFO_OVERSAMPLING_RATIO:
168                 *val = chip->avg;
169                 return IIO_VAL_INT;
170
171         case IIO_CHAN_INFO_INT_TIME:
172                 *val = 0;
173                 if (chan->address == INA2XX_SHUNT_VOLTAGE)
174                         *val2 = chip->int_time_vshunt;
175                 else
176                         *val2 = chip->int_time_vbus;
177
178                 return IIO_VAL_INT_PLUS_MICRO;
179
180         case IIO_CHAN_INFO_SAMP_FREQ:
181                 /*
182                  * Sample freq is read only, it is a consequence of
183                  * 1/AVG*(CT_bus+CT_shunt).
184                  */
185                 *val = DIV_ROUND_CLOSEST(1000000, SAMPLING_PERIOD(chip));
186
187                 return IIO_VAL_INT;
188
189         case IIO_CHAN_INFO_SCALE:
190                 switch (chan->address) {
191                 case INA2XX_SHUNT_VOLTAGE:
192                         /* processed (mV) = raw/shunt_div */
193                         *val2 = chip->config->shunt_div;
194                         *val = 1;
195                         return IIO_VAL_FRACTIONAL;
196
197                 case INA2XX_BUS_VOLTAGE:
198                         /* processed (mV) = raw*lsb (uV) / (1000 << shift) */
199                         *val = chip->config->bus_voltage_lsb;
200                         *val2 = 1000 << chip->config->bus_voltage_shift;
201                         return IIO_VAL_FRACTIONAL;
202
203                 case INA2XX_POWER:
204                         /* processed (mW) = raw*lsb (uW) / 1000 */
205                         *val = chip->config->power_lsb;
206                         *val2 = 1000;
207                         return IIO_VAL_FRACTIONAL;
208
209                 case INA2XX_CURRENT:
210                         /* processed (mA) = raw (mA) */
211                         *val = 1;
212                         return IIO_VAL_INT;
213                 }
214         }
215
216         return -EINVAL;
217 }
218
219 /*
220  * Available averaging rates for ina226. The indices correspond with
221  * the bit values expected by the chip (according to the ina226 datasheet,
222  * table 3 AVG bit settings, found at
223  * http://www.ti.com/lit/ds/symlink/ina226.pdf.
224  */
225 static const int ina226_avg_tab[] = { 1, 4, 16, 64, 128, 256, 512, 1024 };
226
227 static int ina226_set_average(struct ina2xx_chip_info *chip, unsigned int val,
228                               unsigned int *config)
229 {
230         int bits;
231
232         if (val > 1024 || val < 1)
233                 return -EINVAL;
234
235         bits = find_closest(val, ina226_avg_tab,
236                             ARRAY_SIZE(ina226_avg_tab));
237
238         chip->avg = ina226_avg_tab[bits];
239
240         *config &= ~INA226_AVG_MASK;
241         *config |= INA226_SHIFT_AVG(bits) & INA226_AVG_MASK;
242
243         return 0;
244 }
245
246 /* Conversion times in uS */
247 static const int ina226_conv_time_tab[] = { 140, 204, 332, 588, 1100,
248                                             2116, 4156, 8244 };
249
250 static int ina226_set_int_time_vbus(struct ina2xx_chip_info *chip,
251                                     unsigned int val_us, unsigned int *config)
252 {
253         int bits;
254
255         if (val_us > 8244 || val_us < 140)
256                 return -EINVAL;
257
258         bits = find_closest(val_us, ina226_conv_time_tab,
259                             ARRAY_SIZE(ina226_conv_time_tab));
260
261         chip->int_time_vbus = ina226_conv_time_tab[bits];
262
263         *config &= ~INA226_ITB_MASK;
264         *config |= INA226_SHIFT_ITB(bits) & INA226_ITB_MASK;
265
266         return 0;
267 }
268
269 static int ina226_set_int_time_vshunt(struct ina2xx_chip_info *chip,
270                                       unsigned int val_us, unsigned int *config)
271 {
272         int bits;
273
274         if (val_us > 8244 || val_us < 140)
275                 return -EINVAL;
276
277         bits = find_closest(val_us, ina226_conv_time_tab,
278                             ARRAY_SIZE(ina226_conv_time_tab));
279
280         chip->int_time_vshunt = ina226_conv_time_tab[bits];
281
282         *config &= ~INA226_ITS_MASK;
283         *config |= INA226_SHIFT_ITS(bits) & INA226_ITS_MASK;
284
285         return 0;
286 }
287
288 static int ina2xx_write_raw(struct iio_dev *indio_dev,
289                             struct iio_chan_spec const *chan,
290                             int val, int val2, long mask)
291 {
292         struct ina2xx_chip_info *chip = iio_priv(indio_dev);
293         unsigned int config, tmp;
294         int ret;
295
296         if (iio_buffer_enabled(indio_dev))
297                 return -EBUSY;
298
299         mutex_lock(&chip->state_lock);
300
301         ret = regmap_read(chip->regmap, INA2XX_CONFIG, &config);
302         if (ret)
303                 goto err;
304
305         tmp = config;
306
307         switch (mask) {
308         case IIO_CHAN_INFO_OVERSAMPLING_RATIO:
309                 ret = ina226_set_average(chip, val, &tmp);
310                 break;
311
312         case IIO_CHAN_INFO_INT_TIME:
313                 if (chan->address == INA2XX_SHUNT_VOLTAGE)
314                         ret = ina226_set_int_time_vshunt(chip, val2, &tmp);
315                 else
316                         ret = ina226_set_int_time_vbus(chip, val2, &tmp);
317                 break;
318
319         default:
320                 ret = -EINVAL;
321         }
322
323         if (!ret && (tmp != config))
324                 ret = regmap_write(chip->regmap, INA2XX_CONFIG, tmp);
325 err:
326         mutex_unlock(&chip->state_lock);
327
328         return ret;
329 }
330
331 static ssize_t ina2xx_allow_async_readout_show(struct device *dev,
332                                            struct device_attribute *attr,
333                                            char *buf)
334 {
335         struct ina2xx_chip_info *chip = iio_priv(dev_to_iio_dev(dev));
336
337         return sprintf(buf, "%d\n", chip->allow_async_readout);
338 }
339
340 static ssize_t ina2xx_allow_async_readout_store(struct device *dev,
341                                 struct device_attribute *attr,
342                                 const char *buf, size_t len)
343 {
344         struct ina2xx_chip_info *chip = iio_priv(dev_to_iio_dev(dev));
345         bool val;
346         int ret;
347
348         ret = strtobool((const char *) buf, &val);
349         if (ret)
350                 return ret;
351
352         chip->allow_async_readout = val;
353
354         return len;
355 }
356
357 /*
358  * Set current LSB to 1mA, shunt is in uOhms
359  * (equation 13 in datasheet). We hardcode a Current_LSB
360  * of 1.0 x10-6. The only remaining parameter is RShunt.
361  * There is no need to expose the CALIBRATION register
362  * to the user for now. But we need to reset this register
363  * if the user updates RShunt after driver init, e.g upon
364  * reading an EEPROM/Probe-type value.
365  */
366 static int ina2xx_set_calibration(struct ina2xx_chip_info *chip)
367 {
368         u16 regval = DIV_ROUND_CLOSEST(chip->config->calibration_factor,
369                                    chip->shunt_resistor);
370
371         return regmap_write(chip->regmap, INA2XX_CALIBRATION, regval);
372 }
373
374 static int set_shunt_resistor(struct ina2xx_chip_info *chip, unsigned int val)
375 {
376         if (val <= 0 || val > chip->config->calibration_factor)
377                 return -EINVAL;
378
379         chip->shunt_resistor = val;
380
381         return 0;
382 }
383
384 static ssize_t ina2xx_shunt_resistor_show(struct device *dev,
385                                           struct device_attribute *attr,
386                                           char *buf)
387 {
388         struct ina2xx_chip_info *chip = iio_priv(dev_to_iio_dev(dev));
389
390         return sprintf(buf, "%d\n", chip->shunt_resistor);
391 }
392
393 static ssize_t ina2xx_shunt_resistor_store(struct device *dev,
394                                            struct device_attribute *attr,
395                                            const char *buf, size_t len)
396 {
397         struct ina2xx_chip_info *chip = iio_priv(dev_to_iio_dev(dev));
398         unsigned long val;
399         int ret;
400
401         ret = kstrtoul((const char *) buf, 10, &val);
402         if (ret)
403                 return ret;
404
405         ret = set_shunt_resistor(chip, val);
406         if (ret)
407                 return ret;
408
409         /* Update the Calibration register */
410         ret = ina2xx_set_calibration(chip);
411         if (ret)
412                 return ret;
413
414         return len;
415 }
416
417 #define INA2XX_CHAN(_type, _index, _address) { \
418         .type = (_type), \
419         .address = (_address), \
420         .indexed = 1, \
421         .channel = (_index), \
422         .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) \
423         | BIT(IIO_CHAN_INFO_SCALE), \
424         .info_mask_shared_by_dir = BIT(IIO_CHAN_INFO_SAMP_FREQ) | \
425                                    BIT(IIO_CHAN_INFO_OVERSAMPLING_RATIO), \
426         .scan_index = (_index), \
427         .scan_type = { \
428                 .sign = 'u', \
429                 .realbits = 16, \
430                 .storagebits = 16, \
431                 .endianness = IIO_CPU, \
432         } \
433 }
434
435 /*
436  * Sampling Freq is a consequence of the integration times of
437  * the Voltage channels.
438  */
439 #define INA2XX_CHAN_VOLTAGE(_index, _address) { \
440         .type = IIO_VOLTAGE, \
441         .address = (_address), \
442         .indexed = 1, \
443         .channel = (_index), \
444         .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | \
445                               BIT(IIO_CHAN_INFO_SCALE) | \
446                               BIT(IIO_CHAN_INFO_INT_TIME), \
447         .scan_index = (_index), \
448         .scan_type = { \
449                 .sign = 'u', \
450                 .realbits = 16, \
451                 .storagebits = 16, \
452                 .endianness = IIO_LE, \
453         } \
454 }
455
456 static const struct iio_chan_spec ina2xx_channels[] = {
457         INA2XX_CHAN_VOLTAGE(0, INA2XX_SHUNT_VOLTAGE),
458         INA2XX_CHAN_VOLTAGE(1, INA2XX_BUS_VOLTAGE),
459         INA2XX_CHAN(IIO_POWER, 2, INA2XX_POWER),
460         INA2XX_CHAN(IIO_CURRENT, 3, INA2XX_CURRENT),
461         IIO_CHAN_SOFT_TIMESTAMP(4),
462 };
463
464 static int ina2xx_work_buffer(struct iio_dev *indio_dev)
465 {
466         struct ina2xx_chip_info *chip = iio_priv(indio_dev);
467         int bit, ret, i = 0;
468         s64 time_a, time_b;
469         unsigned int alert;
470
471         time_a = iio_get_time_ns(indio_dev);
472
473         /*
474          * Because the timer thread and the chip conversion clock
475          * are asynchronous, the period difference will eventually
476          * result in reading V[k-1] again, or skip V[k] at time Tk.
477          * In order to resync the timer with the conversion process
478          * we check the ConVersionReadyFlag.
479          * On hardware that supports using the ALERT pin to toggle a
480          * GPIO a triggered buffer could be used instead.
481          * For now, we pay for that extra read of the ALERT register
482          */
483         if (!chip->allow_async_readout)
484                 do {
485                         ret = regmap_read(chip->regmap, INA226_ALERT_MASK,
486                                           &alert);
487                         if (ret < 0)
488                                 return ret;
489
490                         alert &= INA266_CVRF;
491                 } while (!alert);
492
493         /*
494          * Single register reads: bulk_read will not work with ina226
495          * as there is no auto-increment of the address register for
496          * data length longer than 16bits.
497          */
498         for_each_set_bit(bit, indio_dev->active_scan_mask,
499                          indio_dev->masklength) {
500                 unsigned int val;
501
502                 ret = regmap_read(chip->regmap,
503                                   INA2XX_SHUNT_VOLTAGE + bit, &val);
504                 if (ret < 0)
505                         return ret;
506
507                 chip->scan.chan[i++] = val;
508         }
509
510         time_b = iio_get_time_ns(indio_dev);
511
512         iio_push_to_buffers_with_timestamp(indio_dev, &chip->scan, time_a);
513
514         return (unsigned long)(time_b - time_a) / 1000;
515 };
516
517 static int ina2xx_capture_thread(void *data)
518 {
519         struct iio_dev *indio_dev = data;
520         struct ina2xx_chip_info *chip = iio_priv(indio_dev);
521         unsigned int sampling_us = SAMPLING_PERIOD(chip);
522         int buffer_us;
523
524         /*
525          * Poll a bit faster than the chip internal Fs, in case
526          * we wish to sync with the conversion ready flag.
527          */
528         if (!chip->allow_async_readout)
529                 sampling_us -= 200;
530
531         do {
532                 buffer_us = ina2xx_work_buffer(indio_dev);
533                 if (buffer_us < 0)
534                         return buffer_us;
535
536                 if (sampling_us > buffer_us)
537                         udelay(sampling_us - buffer_us);
538
539         } while (!kthread_should_stop());
540
541         return 0;
542 }
543
544 static int ina2xx_buffer_enable(struct iio_dev *indio_dev)
545 {
546         struct ina2xx_chip_info *chip = iio_priv(indio_dev);
547         unsigned int sampling_us = SAMPLING_PERIOD(chip);
548
549         dev_dbg(&indio_dev->dev, "Enabling buffer w/ scan_mask %02x, freq = %d, avg =%u\n",
550                 (unsigned int)(*indio_dev->active_scan_mask),
551                 1000000 / sampling_us, chip->avg);
552
553         dev_dbg(&indio_dev->dev, "Expected work period: %u us\n", sampling_us);
554         dev_dbg(&indio_dev->dev, "Async readout mode: %d\n",
555                 chip->allow_async_readout);
556
557         chip->task = kthread_run(ina2xx_capture_thread, (void *)indio_dev,
558                                  "%s:%d-%uus", indio_dev->name, indio_dev->id,
559                                  sampling_us);
560
561         return PTR_ERR_OR_ZERO(chip->task);
562 }
563
564 static int ina2xx_buffer_disable(struct iio_dev *indio_dev)
565 {
566         struct ina2xx_chip_info *chip = iio_priv(indio_dev);
567
568         if (chip->task) {
569                 kthread_stop(chip->task);
570                 chip->task = NULL;
571         }
572
573         return 0;
574 }
575
576 static const struct iio_buffer_setup_ops ina2xx_setup_ops = {
577         .postenable = &ina2xx_buffer_enable,
578         .predisable = &ina2xx_buffer_disable,
579 };
580
581 static int ina2xx_debug_reg(struct iio_dev *indio_dev,
582                             unsigned reg, unsigned writeval, unsigned *readval)
583 {
584         struct ina2xx_chip_info *chip = iio_priv(indio_dev);
585
586         if (!readval)
587                 return regmap_write(chip->regmap, reg, writeval);
588
589         return regmap_read(chip->regmap, reg, readval);
590 }
591
592 /* Possible integration times for vshunt and vbus */
593 static IIO_CONST_ATTR_INT_TIME_AVAIL("0.000140 0.000204 0.000332 0.000588 0.001100 0.002116 0.004156 0.008244");
594
595 static IIO_DEVICE_ATTR(in_allow_async_readout, S_IRUGO | S_IWUSR,
596                        ina2xx_allow_async_readout_show,
597                        ina2xx_allow_async_readout_store, 0);
598
599 static IIO_DEVICE_ATTR(in_shunt_resistor, S_IRUGO | S_IWUSR,
600                        ina2xx_shunt_resistor_show,
601                        ina2xx_shunt_resistor_store, 0);
602
603 static struct attribute *ina2xx_attributes[] = {
604         &iio_dev_attr_in_allow_async_readout.dev_attr.attr,
605         &iio_const_attr_integration_time_available.dev_attr.attr,
606         &iio_dev_attr_in_shunt_resistor.dev_attr.attr,
607         NULL,
608 };
609
610 static const struct attribute_group ina2xx_attribute_group = {
611         .attrs = ina2xx_attributes,
612 };
613
614 static const struct iio_info ina2xx_info = {
615         .driver_module = THIS_MODULE,
616         .attrs = &ina2xx_attribute_group,
617         .read_raw = ina2xx_read_raw,
618         .write_raw = ina2xx_write_raw,
619         .debugfs_reg_access = ina2xx_debug_reg,
620 };
621
622 /* Initialize the configuration and calibration registers. */
623 static int ina2xx_init(struct ina2xx_chip_info *chip, unsigned int config)
624 {
625         int ret = regmap_write(chip->regmap, INA2XX_CONFIG, config);
626         if (ret)
627                 return ret;
628
629         return ina2xx_set_calibration(chip);
630 }
631
632 static int ina2xx_probe(struct i2c_client *client,
633                         const struct i2c_device_id *id)
634 {
635         struct ina2xx_chip_info *chip;
636         struct iio_dev *indio_dev;
637         struct iio_buffer *buffer;
638         unsigned int val;
639         int ret;
640
641         indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*chip));
642         if (!indio_dev)
643                 return -ENOMEM;
644
645         chip = iio_priv(indio_dev);
646
647         /* This is only used for device removal purposes. */
648         i2c_set_clientdata(client, indio_dev);
649
650         chip->regmap = devm_regmap_init_i2c(client, &ina2xx_regmap_config);
651         if (IS_ERR(chip->regmap)) {
652                 dev_err(&client->dev, "failed to allocate register map\n");
653                 return PTR_ERR(chip->regmap);
654         }
655
656         chip->config = &ina2xx_config[id->driver_data];
657
658         mutex_init(&chip->state_lock);
659
660         if (of_property_read_u32(client->dev.of_node,
661                                  "shunt-resistor", &val) < 0) {
662                 struct ina2xx_platform_data *pdata =
663                     dev_get_platdata(&client->dev);
664
665                 if (pdata)
666                         val = pdata->shunt_uohms;
667                 else
668                         val = INA2XX_RSHUNT_DEFAULT;
669         }
670
671         ret = set_shunt_resistor(chip, val);
672         if (ret)
673                 return ret;
674
675         /* Patch the current config register with default. */
676         val = chip->config->config_default;
677
678         if (id->driver_data == ina226) {
679                 ina226_set_average(chip, INA226_DEFAULT_AVG, &val);
680                 ina226_set_int_time_vbus(chip, INA226_DEFAULT_IT, &val);
681                 ina226_set_int_time_vshunt(chip, INA226_DEFAULT_IT, &val);
682         }
683
684         ret = ina2xx_init(chip, val);
685         if (ret) {
686                 dev_err(&client->dev, "error configuring the device\n");
687                 return ret;
688         }
689
690         indio_dev->modes = INDIO_DIRECT_MODE | INDIO_BUFFER_SOFTWARE;
691         indio_dev->dev.parent = &client->dev;
692         indio_dev->dev.of_node = client->dev.of_node;
693         indio_dev->channels = ina2xx_channels;
694         indio_dev->num_channels = ARRAY_SIZE(ina2xx_channels);
695         indio_dev->name = id->name;
696         indio_dev->info = &ina2xx_info;
697         indio_dev->setup_ops = &ina2xx_setup_ops;
698
699         buffer = devm_iio_kfifo_allocate(&indio_dev->dev);
700         if (!buffer)
701                 return -ENOMEM;
702
703         iio_device_attach_buffer(indio_dev, buffer);
704
705         return iio_device_register(indio_dev);
706 }
707
708 static int ina2xx_remove(struct i2c_client *client)
709 {
710         struct iio_dev *indio_dev = i2c_get_clientdata(client);
711         struct ina2xx_chip_info *chip = iio_priv(indio_dev);
712
713         iio_device_unregister(indio_dev);
714
715         /* Powerdown */
716         return regmap_update_bits(chip->regmap, INA2XX_CONFIG,
717                                   INA2XX_MODE_MASK, 0);
718 }
719
720 static const struct i2c_device_id ina2xx_id[] = {
721         {"ina219", ina219},
722         {"ina220", ina219},
723         {"ina226", ina226},
724         {"ina230", ina226},
725         {"ina231", ina226},
726         {}
727 };
728 MODULE_DEVICE_TABLE(i2c, ina2xx_id);
729
730 static struct i2c_driver ina2xx_driver = {
731         .driver = {
732                    .name = KBUILD_MODNAME,
733         },
734         .probe = ina2xx_probe,
735         .remove = ina2xx_remove,
736         .id_table = ina2xx_id,
737 };
738 module_i2c_driver(ina2xx_driver);
739
740 MODULE_AUTHOR("Marc Titinger <marc.titinger@baylibre.com>");
741 MODULE_DESCRIPTION("Texas Instruments INA2XX ADC driver");
742 MODULE_LICENSE("GPL v2");