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