staging:iio:ad7291: Remove unnecessary dev_info() from probe()
[firefly-linux-kernel-4.4.55.git] / drivers / staging / iio / adc / ad7291.c
1 /*
2  * AD7291 8-Channel, I2C, 12-Bit SAR ADC with Temperature Sensor
3  *
4  * Copyright 2010-2011 Analog Devices Inc.
5  *
6  * Licensed under the GPL-2 or later.
7  */
8
9 #include <linux/interrupt.h>
10 #include <linux/device.h>
11 #include <linux/kernel.h>
12 #include <linux/slab.h>
13 #include <linux/sysfs.h>
14 #include <linux/i2c.h>
15 #include <linux/module.h>
16 #include <linux/mutex.h>
17 #include <linux/regulator/consumer.h>
18 #include <linux/err.h>
19
20 #include <linux/iio/iio.h>
21 #include <linux/iio/sysfs.h>
22 #include <linux/iio/events.h>
23
24 /*
25  * Simplified handling
26  *
27  * If no events enabled - single polled channel read
28  * If event enabled direct reads disable unless channel
29  * is in the read mask.
30  *
31  * The noise-delayed bit as per datasheet suggestion is always enabled.
32  *
33  */
34
35 /*
36  * AD7291 registers definition
37  */
38 #define AD7291_COMMAND                  0x00
39 #define AD7291_VOLTAGE                  0x01
40 #define AD7291_T_SENSE                  0x02
41 #define AD7291_T_AVERAGE                0x03
42 #define AD7291_CH0_DATA_HIGH            0x04
43 #define AD7291_CH0_DATA_LOW             0x05
44 #define AD7291_CH0_HYST                 0x06
45 #define AD7291_CH1_DATA_HIGH            0x07
46 #define AD7291_CH1_DATA_LOW             0x08
47 #define AD7291_CH1_HYST                 0x09
48 #define AD7291_CH2_DATA_HIGH            0x0A
49 #define AD7291_CH2_DATA_LOW             0x0B
50 #define AD7291_CH2_HYST                 0x0C
51 #define AD7291_CH3_DATA_HIGH            0x0D
52 #define AD7291_CH3_DATA_LOW             0x0E
53 #define AD7291_CH3_HYST                 0x0F
54 #define AD7291_CH4_DATA_HIGH            0x10
55 #define AD7291_CH4_DATA_LOW             0x11
56 #define AD7291_CH4_HYST                 0x12
57 #define AD7291_CH5_DATA_HIGH            0x13
58 #define AD7291_CH5_DATA_LOW             0x14
59 #define AD7291_CH5_HYST                 0x15
60 #define AD7291_CH6_DATA_HIGH            0x16
61 #define AD7291_CH6_DATA_LOW             0x17
62 #define AD7291_CH6_HYST                 0x18
63 #define AD7291_CH7_DATA_HIGH            0x19
64 #define AD7291_CH7_DATA_LOW             0x1A
65 #define AD7291_CH7_HYST                 0x2B
66 #define AD7291_T_SENSE_HIGH             0x1C
67 #define AD7291_T_SENSE_LOW              0x1D
68 #define AD7291_T_SENSE_HYST             0x1E
69 #define AD7291_VOLTAGE_ALERT_STATUS     0x1F
70 #define AD7291_T_ALERT_STATUS           0x20
71
72 #define AD7291_VOLTAGE_LIMIT_COUNT      8
73
74
75 /*
76  * AD7291 command
77  */
78 #define AD7291_AUTOCYCLE                (1 << 0)
79 #define AD7291_RESET                    (1 << 1)
80 #define AD7291_ALERT_CLEAR              (1 << 2)
81 #define AD7291_ALERT_POLARITY           (1 << 3)
82 #define AD7291_EXT_REF                  (1 << 4)
83 #define AD7291_NOISE_DELAY              (1 << 5)
84 #define AD7291_T_SENSE_MASK             (1 << 7)
85 #define AD7291_VOLTAGE_MASK             0xFF00
86 #define AD7291_VOLTAGE_OFFSET           0x8
87
88 /*
89  * AD7291 value masks
90  */
91 #define AD7291_CHANNEL_MASK             0xF000
92 #define AD7291_BITS                     12
93 #define AD7291_VALUE_MASK               0xFFF
94 #define AD7291_T_VALUE_SIGN             0x400
95 #define AD7291_T_VALUE_FLOAT_OFFSET     2
96 #define AD7291_T_VALUE_FLOAT_MASK       0x2
97
98 #define AD7291_BITS                     12
99
100 struct ad7291_chip_info {
101         struct i2c_client       *client;
102         struct regulator        *reg;
103         u16                     int_vref_mv;
104         u16                     command;
105         u16                     c_mask; /* Active voltage channels for events */
106         struct mutex            state_lock;
107 };
108
109 static int ad7291_i2c_read(struct ad7291_chip_info *chip, u8 reg, u16 *data)
110 {
111         struct i2c_client *client = chip->client;
112         int ret = 0;
113
114         ret = i2c_smbus_read_word_data(client, reg);
115         if (ret < 0) {
116                 dev_err(&client->dev, "I2C read error\n");
117                 return ret;
118         }
119
120         *data = swab16((u16)ret);
121
122         return 0;
123 }
124
125 static int ad7291_i2c_write(struct ad7291_chip_info *chip, u8 reg, u16 data)
126 {
127         return i2c_smbus_write_word_data(chip->client, reg, swab16(data));
128 }
129
130 static irqreturn_t ad7291_event_handler(int irq, void *private)
131 {
132         struct iio_dev *indio_dev = private;
133         struct ad7291_chip_info *chip = iio_priv(private);
134         u16 t_status, v_status;
135         u16 command;
136         int i;
137         s64 timestamp = iio_get_time_ns();
138
139         if (ad7291_i2c_read(chip, AD7291_T_ALERT_STATUS, &t_status))
140                 return IRQ_HANDLED;
141
142         if (ad7291_i2c_read(chip, AD7291_VOLTAGE_ALERT_STATUS, &v_status))
143                 return IRQ_HANDLED;
144
145         if (!(t_status || v_status))
146                 return IRQ_HANDLED;
147
148         command = chip->command | AD7291_ALERT_CLEAR;
149         ad7291_i2c_write(chip, AD7291_COMMAND, command);
150
151         command = chip->command & ~AD7291_ALERT_CLEAR;
152         ad7291_i2c_write(chip, AD7291_COMMAND, command);
153
154         /* For now treat t_sense and t_sense_average the same */
155         if ((t_status & (1 << 0)) || (t_status & (1 << 2)))
156                 iio_push_event(indio_dev,
157                                IIO_UNMOD_EVENT_CODE(IIO_TEMP,
158                                                     0,
159                                                     IIO_EV_TYPE_THRESH,
160                                                     IIO_EV_DIR_FALLING),
161                                timestamp);
162         if ((t_status & (1 << 1)) || (t_status & (1 << 3)))
163                 iio_push_event(indio_dev,
164                                IIO_UNMOD_EVENT_CODE(IIO_TEMP,
165                                                     0,
166                                                     IIO_EV_TYPE_THRESH,
167                                                     IIO_EV_DIR_RISING),
168                                timestamp);
169
170         for (i = 0; i < AD7291_VOLTAGE_LIMIT_COUNT*2; i += 2) {
171                 if (v_status & (1 << i))
172                         iio_push_event(indio_dev,
173                                        IIO_UNMOD_EVENT_CODE(IIO_VOLTAGE,
174                                                             i/2,
175                                                             IIO_EV_TYPE_THRESH,
176                                                             IIO_EV_DIR_FALLING),
177                                        timestamp);
178                 if (v_status & (1 << (i + 1)))
179                         iio_push_event(indio_dev,
180                                        IIO_UNMOD_EVENT_CODE(IIO_VOLTAGE,
181                                                             i/2,
182                                                             IIO_EV_TYPE_THRESH,
183                                                             IIO_EV_DIR_RISING),
184                                        timestamp);
185         }
186
187         return IRQ_HANDLED;
188 }
189
190 static inline ssize_t ad7291_show_hyst(struct device *dev,
191                 struct device_attribute *attr,
192                 char *buf)
193 {
194         struct iio_dev *indio_dev = dev_to_iio_dev(dev);
195         struct ad7291_chip_info *chip = iio_priv(indio_dev);
196         struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
197         u16 data;
198         int ret;
199
200         ret = ad7291_i2c_read(chip, this_attr->address, &data);
201         if (ret < 0)
202                 return ret;
203
204         return sprintf(buf, "%d\n", data & AD7291_VALUE_MASK);
205 }
206
207 static inline ssize_t ad7291_set_hyst(struct device *dev,
208                                       struct device_attribute *attr,
209                                       const char *buf,
210                                       size_t len)
211 {
212         struct iio_dev *indio_dev = dev_to_iio_dev(dev);
213         struct ad7291_chip_info *chip = iio_priv(indio_dev);
214         struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
215         u16 data;
216         int ret;
217
218         ret = kstrtou16(buf, 10, &data);
219
220         if (ret < 0)
221                 return ret;
222         if (data > AD7291_VALUE_MASK)
223                 return -EINVAL;
224
225         ret = ad7291_i2c_write(chip, this_attr->address, data);
226         if (ret < 0)
227                 return ret;
228
229         return len;
230 }
231
232 static IIO_DEVICE_ATTR(in_temp0_thresh_both_hyst_raw,
233                        S_IRUGO | S_IWUSR,
234                        ad7291_show_hyst, ad7291_set_hyst,
235                        AD7291_T_SENSE_HYST);
236 static IIO_DEVICE_ATTR(in_voltage0_thresh_both_hyst_raw,
237                        S_IRUGO | S_IWUSR,
238                        ad7291_show_hyst, ad7291_set_hyst, AD7291_CH0_HYST);
239 static IIO_DEVICE_ATTR(in_voltage1_thresh_both_hyst_raw,
240                        S_IRUGO | S_IWUSR,
241                        ad7291_show_hyst, ad7291_set_hyst, AD7291_CH1_HYST);
242 static IIO_DEVICE_ATTR(in_voltage2_thresh_both_hyst_raw,
243                        S_IRUGO | S_IWUSR,
244                        ad7291_show_hyst, ad7291_set_hyst, AD7291_CH2_HYST);
245 static IIO_DEVICE_ATTR(in_voltage3_thresh_both_hyst_raw,
246                        S_IRUGO | S_IWUSR,
247                        ad7291_show_hyst, ad7291_set_hyst, AD7291_CH3_HYST);
248 static IIO_DEVICE_ATTR(in_voltage4_thresh_both_hyst_raw,
249                        S_IRUGO | S_IWUSR,
250                        ad7291_show_hyst, ad7291_set_hyst, AD7291_CH4_HYST);
251 static IIO_DEVICE_ATTR(in_voltage5_thresh_both_hyst_raw,
252                        S_IRUGO | S_IWUSR,
253                        ad7291_show_hyst, ad7291_set_hyst, AD7291_CH5_HYST);
254 static IIO_DEVICE_ATTR(in_voltage6_thresh_both_hyst_raw,
255                        S_IRUGO | S_IWUSR,
256                        ad7291_show_hyst, ad7291_set_hyst, AD7291_CH6_HYST);
257 static IIO_DEVICE_ATTR(in_voltage7_thresh_both_hyst_raw,
258                        S_IRUGO | S_IWUSR,
259                        ad7291_show_hyst, ad7291_set_hyst, AD7291_CH7_HYST);
260
261 static struct attribute *ad7291_event_attributes[] = {
262         &iio_dev_attr_in_temp0_thresh_both_hyst_raw.dev_attr.attr,
263         &iio_dev_attr_in_voltage0_thresh_both_hyst_raw.dev_attr.attr,
264         &iio_dev_attr_in_voltage1_thresh_both_hyst_raw.dev_attr.attr,
265         &iio_dev_attr_in_voltage2_thresh_both_hyst_raw.dev_attr.attr,
266         &iio_dev_attr_in_voltage3_thresh_both_hyst_raw.dev_attr.attr,
267         &iio_dev_attr_in_voltage4_thresh_both_hyst_raw.dev_attr.attr,
268         &iio_dev_attr_in_voltage5_thresh_both_hyst_raw.dev_attr.attr,
269         &iio_dev_attr_in_voltage6_thresh_both_hyst_raw.dev_attr.attr,
270         &iio_dev_attr_in_voltage7_thresh_both_hyst_raw.dev_attr.attr,
271         NULL,
272 };
273
274 /* high / low */
275 static u8 ad7291_limit_regs[9][2] = {
276         { AD7291_CH0_DATA_HIGH, AD7291_CH0_DATA_LOW },
277         { AD7291_CH1_DATA_HIGH, AD7291_CH1_DATA_LOW },
278         { AD7291_CH2_DATA_HIGH, AD7291_CH2_DATA_LOW },
279         { AD7291_CH3_DATA_HIGH, AD7291_CH3_DATA_LOW }, /* FIXME: ? */
280         { AD7291_CH4_DATA_HIGH, AD7291_CH4_DATA_LOW },
281         { AD7291_CH5_DATA_HIGH, AD7291_CH5_DATA_LOW },
282         { AD7291_CH6_DATA_HIGH, AD7291_CH6_DATA_LOW },
283         { AD7291_CH7_DATA_HIGH, AD7291_CH7_DATA_LOW },
284         /* temp */
285         { AD7291_T_SENSE_HIGH, AD7291_T_SENSE_LOW },
286 };
287
288 static int ad7291_read_event_value(struct iio_dev *indio_dev,
289                                    u64 event_code,
290                                    int *val)
291 {
292         struct ad7291_chip_info *chip = iio_priv(indio_dev);
293
294         int ret;
295         u8 reg;
296         u16 uval;
297         s16 signval;
298
299         switch (IIO_EVENT_CODE_EXTRACT_CHAN_TYPE(event_code)) {
300         case IIO_VOLTAGE:
301                 reg = ad7291_limit_regs[IIO_EVENT_CODE_EXTRACT_CHAN(event_code)]
302                         [!(IIO_EVENT_CODE_EXTRACT_DIR(event_code) ==
303                            IIO_EV_DIR_RISING)];
304
305                 ret = ad7291_i2c_read(chip, reg, &uval);
306                 if (ret < 0)
307                         return ret;
308                 *val = uval & AD7291_VALUE_MASK;
309                 return 0;
310
311         case IIO_TEMP:
312                 reg = ad7291_limit_regs[8]
313                         [!(IIO_EVENT_CODE_EXTRACT_DIR(event_code) ==
314                            IIO_EV_DIR_RISING)];
315
316                 ret = ad7291_i2c_read(chip, reg, &signval);
317                 if (ret < 0)
318                         return ret;
319                 signval = (s16)((signval & AD7291_VALUE_MASK) << 4) >> 4;
320                 *val = signval;
321                 return 0;
322         default:
323                 return -EINVAL;
324         };
325 }
326
327 static int ad7291_write_event_value(struct iio_dev *indio_dev,
328                                     u64 event_code,
329                                     int val)
330 {
331         struct ad7291_chip_info *chip = iio_priv(indio_dev);
332         u8 reg;
333         s16 signval;
334
335         switch (IIO_EVENT_CODE_EXTRACT_CHAN_TYPE(event_code)) {
336         case IIO_VOLTAGE:
337                 if (val > AD7291_VALUE_MASK || val < 0)
338                         return -EINVAL;
339                 reg = ad7291_limit_regs[IIO_EVENT_CODE_EXTRACT_CHAN(event_code)]
340                         [!(IIO_EVENT_CODE_EXTRACT_DIR(event_code) ==
341                            IIO_EV_DIR_RISING)];
342                 return ad7291_i2c_write(chip, reg, val);
343         case IIO_TEMP:
344                 if (val > 2047 || val < -2048)
345                         return -EINVAL;
346                 reg = ad7291_limit_regs[8]
347                         [!(IIO_EVENT_CODE_EXTRACT_DIR(event_code) ==
348                            IIO_EV_DIR_RISING)];
349                 signval = val;
350                 return ad7291_i2c_write(chip, reg, *(u16 *)&signval);
351         default:
352                 return -EINVAL;
353         };
354 }
355
356 static int ad7291_read_event_config(struct iio_dev *indio_dev,
357                                     u64 event_code)
358 {
359         struct ad7291_chip_info *chip = iio_priv(indio_dev);
360         /* To be enabled the channel must simply be on. If any are enabled
361            we are in continuous sampling mode */
362
363         switch (IIO_EVENT_CODE_EXTRACT_CHAN_TYPE(event_code)) {
364         case IIO_VOLTAGE:
365                 if (chip->c_mask &
366                     (1 << (15 - IIO_EVENT_CODE_EXTRACT_CHAN(event_code))))
367                         return 1;
368                 else
369                         return 0;
370         case IIO_TEMP:
371                 /* always on */
372                 return 1;
373         default:
374                 return -EINVAL;
375         }
376
377 }
378
379 static int ad7291_write_event_config(struct iio_dev *indio_dev,
380                                      u64 event_code,
381                                      int state)
382 {
383         int ret = 0;
384         struct ad7291_chip_info *chip = iio_priv(indio_dev);
385         u16 regval;
386
387         mutex_lock(&chip->state_lock);
388         regval = chip->command;
389         /*
390          * To be enabled the channel must simply be on. If any are enabled
391          * use continuous sampling mode.
392          * Possible to disable temp as well but that makes single read tricky.
393          */
394
395         switch (IIO_EVENT_CODE_EXTRACT_TYPE(event_code)) {
396         case IIO_VOLTAGE:
397                 if ((!state) && (chip->c_mask & (1 << (15 -
398                                 IIO_EVENT_CODE_EXTRACT_CHAN(event_code)))))
399                         chip->c_mask &= ~(1 << (15 - IIO_EVENT_CODE_EXTRACT_CHAN
400                                                         (event_code)));
401                 else if (state && (!(chip->c_mask & (1 << (15 -
402                                 IIO_EVENT_CODE_EXTRACT_CHAN(event_code))))))
403                         chip->c_mask |= (1 << (15 - IIO_EVENT_CODE_EXTRACT_CHAN
404                                                         (event_code)));
405                 else
406                         break;
407
408                 regval &= ~AD7291_AUTOCYCLE;
409                 regval |= chip->c_mask;
410                 if (chip->c_mask) /* Enable autocycle? */
411                         regval |= AD7291_AUTOCYCLE;
412
413                 ret = ad7291_i2c_write(chip, AD7291_COMMAND, regval);
414                 if (ret < 0)
415                         goto error_ret;
416
417                 chip->command = regval;
418                 break;
419         default:
420                 ret = -EINVAL;
421         }
422
423 error_ret:
424         mutex_unlock(&chip->state_lock);
425         return ret;
426 }
427
428 static int ad7291_read_raw(struct iio_dev *indio_dev,
429                            struct iio_chan_spec const *chan,
430                            int *val,
431                            int *val2,
432                            long mask)
433 {
434         int ret;
435         struct ad7291_chip_info *chip = iio_priv(indio_dev);
436         u16 regval;
437         s16 signval;
438
439         switch (mask) {
440         case IIO_CHAN_INFO_RAW:
441                 switch (chan->type) {
442                 case IIO_VOLTAGE:
443                         mutex_lock(&chip->state_lock);
444                         /* If in autocycle mode drop through */
445                         if (chip->command & AD7291_AUTOCYCLE) {
446                                 mutex_unlock(&chip->state_lock);
447                                 return -EBUSY;
448                         }
449                         /* Enable this channel alone */
450                         regval = chip->command & (~AD7291_VOLTAGE_MASK);
451                         regval |= 1 << (15 - chan->channel);
452                         ret = ad7291_i2c_write(chip, AD7291_COMMAND, regval);
453                         if (ret < 0) {
454                                 mutex_unlock(&chip->state_lock);
455                                 return ret;
456                         }
457                         /* Read voltage */
458                         ret = i2c_smbus_read_word_data(chip->client,
459                                                        AD7291_VOLTAGE);
460                         if (ret < 0) {
461                                 mutex_unlock(&chip->state_lock);
462                                 return ret;
463                         }
464                         *val = swab16((u16)ret) & AD7291_VALUE_MASK;
465                         mutex_unlock(&chip->state_lock);
466                         return IIO_VAL_INT;
467                 case IIO_TEMP:
468                         /* Assumes tsense bit of command register always set */
469                         ret = i2c_smbus_read_word_data(chip->client,
470                                                        AD7291_T_SENSE);
471                         if (ret < 0)
472                                 return ret;
473                         signval = (s16)((swab16((u16)ret) &
474                                 AD7291_VALUE_MASK) << 4) >> 4;
475                         *val = signval;
476                         return IIO_VAL_INT;
477                 default:
478                         return -EINVAL;
479                 }
480         case IIO_CHAN_INFO_AVERAGE_RAW:
481                 ret = i2c_smbus_read_word_data(chip->client,
482                                                AD7291_T_AVERAGE);
483                         if (ret < 0)
484                                 return ret;
485                         signval = (s16)((swab16((u16)ret) &
486                                 AD7291_VALUE_MASK) << 4) >> 4;
487                         *val = signval;
488                         return IIO_VAL_INT;
489         case IIO_CHAN_INFO_SCALE:
490                 switch (chan->type) {
491                 case IIO_VOLTAGE:
492                         *val = chip->int_vref_mv;
493                         *val2 = AD7291_BITS;
494                         return IIO_VAL_FRACTIONAL_LOG2;
495                 case IIO_TEMP:
496                         /*
497                          * One LSB of the ADC corresponds to 0.25 deg C.
498                          * The temperature reading is in 12-bit twos
499                          * complement format
500                          */
501                         *val = 250;
502                         return IIO_VAL_INT;
503                 default:
504                         return -EINVAL;
505                 }
506         default:
507                 return -EINVAL;
508         }
509 }
510
511 #define AD7291_VOLTAGE_CHAN(_chan)                                      \
512 {                                                                       \
513         .type = IIO_VOLTAGE,                                            \
514         .info_mask_separate = BIT(IIO_CHAN_INFO_RAW),                   \
515         .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE),           \
516         .indexed = 1,                                                   \
517         .channel = _chan,                                               \
518         .event_mask = IIO_EV_BIT(IIO_EV_TYPE_THRESH, IIO_EV_DIR_RISING)|\
519         IIO_EV_BIT(IIO_EV_TYPE_THRESH, IIO_EV_DIR_FALLING)              \
520 }
521
522 static const struct iio_chan_spec ad7291_channels[] = {
523         AD7291_VOLTAGE_CHAN(0),
524         AD7291_VOLTAGE_CHAN(1),
525         AD7291_VOLTAGE_CHAN(2),
526         AD7291_VOLTAGE_CHAN(3),
527         AD7291_VOLTAGE_CHAN(4),
528         AD7291_VOLTAGE_CHAN(5),
529         AD7291_VOLTAGE_CHAN(6),
530         AD7291_VOLTAGE_CHAN(7),
531         {
532                 .type = IIO_TEMP,
533                 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
534                                 BIT(IIO_CHAN_INFO_AVERAGE_RAW) |
535                                 BIT(IIO_CHAN_INFO_SCALE),
536                 .indexed = 1,
537                 .channel = 0,
538                 .event_mask =
539                 IIO_EV_BIT(IIO_EV_TYPE_THRESH, IIO_EV_DIR_RISING)|
540                 IIO_EV_BIT(IIO_EV_TYPE_THRESH, IIO_EV_DIR_FALLING)
541         }
542 };
543
544 static struct attribute_group ad7291_event_attribute_group = {
545         .attrs = ad7291_event_attributes,
546 };
547
548 static const struct iio_info ad7291_info = {
549         .read_raw = &ad7291_read_raw,
550         .read_event_config = &ad7291_read_event_config,
551         .write_event_config = &ad7291_write_event_config,
552         .read_event_value = &ad7291_read_event_value,
553         .write_event_value = &ad7291_write_event_value,
554         .event_attrs = &ad7291_event_attribute_group,
555 };
556
557 static int ad7291_probe(struct i2c_client *client,
558                 const struct i2c_device_id *id)
559 {
560         struct ad7291_chip_info *chip;
561         struct iio_dev *indio_dev;
562         int ret = 0, voltage_uv = 0;
563
564         indio_dev = iio_device_alloc(sizeof(*chip));
565         if (indio_dev == NULL) {
566                 ret = -ENOMEM;
567                 goto error_ret;
568         }
569         chip = iio_priv(indio_dev);
570
571         chip->reg = regulator_get(&client->dev, "vcc");
572         if (!IS_ERR(chip->reg)) {
573                 ret = regulator_enable(chip->reg);
574                 if (ret)
575                         goto error_put_reg;
576                 voltage_uv = regulator_get_voltage(chip->reg);
577         }
578
579         mutex_init(&chip->state_lock);
580         /* this is only used for device removal purposes */
581         i2c_set_clientdata(client, indio_dev);
582
583         chip->client = client;
584
585         chip->command = AD7291_NOISE_DELAY |
586                         AD7291_T_SENSE_MASK | /* Tsense always enabled */
587                         AD7291_ALERT_POLARITY; /* set irq polarity low level */
588
589         if (voltage_uv) {
590                 chip->int_vref_mv = voltage_uv / 1000;
591                 chip->command |= AD7291_EXT_REF;
592         } else {
593                 chip->int_vref_mv = 2500; /* Build-in ref */
594         }
595
596         indio_dev->name = id->name;
597         indio_dev->channels = ad7291_channels;
598         indio_dev->num_channels = ARRAY_SIZE(ad7291_channels);
599
600         indio_dev->dev.parent = &client->dev;
601         indio_dev->info = &ad7291_info;
602         indio_dev->modes = INDIO_DIRECT_MODE;
603
604         ret = ad7291_i2c_write(chip, AD7291_COMMAND, AD7291_RESET);
605         if (ret) {
606                 ret = -EIO;
607                 goto error_disable_reg;
608         }
609
610         ret = ad7291_i2c_write(chip, AD7291_COMMAND, chip->command);
611         if (ret) {
612                 ret = -EIO;
613                 goto error_disable_reg;
614         }
615
616         if (client->irq > 0) {
617                 ret = request_threaded_irq(client->irq,
618                                            NULL,
619                                            &ad7291_event_handler,
620                                            IRQF_TRIGGER_LOW | IRQF_ONESHOT,
621                                            id->name,
622                                            indio_dev);
623                 if (ret)
624                         goto error_disable_reg;
625         }
626
627         ret = iio_device_register(indio_dev);
628         if (ret)
629                 goto error_unreg_irq;
630
631         return 0;
632
633 error_unreg_irq:
634         if (client->irq)
635                 free_irq(client->irq, indio_dev);
636 error_disable_reg:
637         if (!IS_ERR(chip->reg))
638                 regulator_disable(chip->reg);
639 error_put_reg:
640         if (!IS_ERR(chip->reg))
641                 regulator_put(chip->reg);
642
643         iio_device_free(indio_dev);
644 error_ret:
645         return ret;
646 }
647
648 static int ad7291_remove(struct i2c_client *client)
649 {
650         struct iio_dev *indio_dev = i2c_get_clientdata(client);
651         struct ad7291_chip_info *chip = iio_priv(indio_dev);
652
653         iio_device_unregister(indio_dev);
654
655         if (client->irq)
656                 free_irq(client->irq, indio_dev);
657
658         if (!IS_ERR(chip->reg)) {
659                 regulator_disable(chip->reg);
660                 regulator_put(chip->reg);
661         }
662
663         iio_device_free(indio_dev);
664
665         return 0;
666 }
667
668 static const struct i2c_device_id ad7291_id[] = {
669         { "ad7291", 0 },
670         {}
671 };
672
673 MODULE_DEVICE_TABLE(i2c, ad7291_id);
674
675 static struct i2c_driver ad7291_driver = {
676         .driver = {
677                 .name = KBUILD_MODNAME,
678         },
679         .probe = ad7291_probe,
680         .remove = ad7291_remove,
681         .id_table = ad7291_id,
682 };
683 module_i2c_driver(ad7291_driver);
684
685 MODULE_AUTHOR("Sonic Zhang <sonic.zhang@analog.com>");
686 MODULE_DESCRIPTION("Analog Devices AD7291 ADC driver");
687 MODULE_LICENSE("GPL v2");