iio:adc:ad799x: Drop I2C access helper functions
[firefly-linux-kernel-4.4.55.git] / drivers / iio / adc / ad799x.c
1 /*
2  * iio/adc/ad799x.c
3  * Copyright (C) 2010-2011 Michael Hennerich, Analog Devices Inc.
4  *
5  * based on iio/adc/max1363
6  * Copyright (C) 2008-2010 Jonathan Cameron
7  *
8  * based on linux/drivers/i2c/chips/max123x
9  * Copyright (C) 2002-2004 Stefan Eletzhofer
10  *
11  * based on linux/drivers/acron/char/pcf8583.c
12  * Copyright (C) 2000 Russell King
13  *
14  * This program is free software; you can redistribute it and/or modify
15  * it under the terms of the GNU General Public License version 2 as
16  * published by the Free Software Foundation.
17  *
18  * ad799x.c
19  *
20  * Support for ad7991, ad7995, ad7999, ad7992, ad7993, ad7994, ad7997,
21  * ad7998 and similar chips.
22  *
23  */
24
25 #include <linux/interrupt.h>
26 #include <linux/device.h>
27 #include <linux/kernel.h>
28 #include <linux/sysfs.h>
29 #include <linux/i2c.h>
30 #include <linux/regulator/consumer.h>
31 #include <linux/slab.h>
32 #include <linux/types.h>
33 #include <linux/err.h>
34 #include <linux/module.h>
35
36 #include <linux/iio/iio.h>
37 #include <linux/iio/sysfs.h>
38 #include <linux/iio/events.h>
39 #include <linux/iio/buffer.h>
40 #include <linux/iio/trigger_consumer.h>
41 #include <linux/iio/triggered_buffer.h>
42
43 #define AD799X_CHANNEL_SHIFT                    4
44 #define AD799X_STORAGEBITS                      16
45 /*
46  * AD7991, AD7995 and AD7999 defines
47  */
48
49 #define AD7991_REF_SEL                          0x08
50 #define AD7991_FLTR                             0x04
51 #define AD7991_BIT_TRIAL_DELAY                  0x02
52 #define AD7991_SAMPLE_DELAY                     0x01
53
54 /*
55  * AD7992, AD7993, AD7994, AD7997 and AD7998 defines
56  */
57
58 #define AD7998_FLTR                             0x08
59 #define AD7998_ALERT_EN                         0x04
60 #define AD7998_BUSY_ALERT                       0x02
61 #define AD7998_BUSY_ALERT_POL                   0x01
62
63 #define AD7998_CONV_RES_REG                     0x0
64 #define AD7998_ALERT_STAT_REG                   0x1
65 #define AD7998_CONF_REG                         0x2
66 #define AD7998_CYCLE_TMR_REG                    0x3
67
68 #define AD7998_DATALOW_REG(x)                   ((x) * 3 + 0x4)
69 #define AD7998_DATAHIGH_REG(x)                  ((x) * 3 + 0x5)
70 #define AD7998_HYST_REG(x)                      ((x) * 3 + 0x6)
71
72 #define AD7998_CYC_MASK                         0x7
73 #define AD7998_CYC_DIS                          0x0
74 #define AD7998_CYC_TCONF_32                     0x1
75 #define AD7998_CYC_TCONF_64                     0x2
76 #define AD7998_CYC_TCONF_128                    0x3
77 #define AD7998_CYC_TCONF_256                    0x4
78 #define AD7998_CYC_TCONF_512                    0x5
79 #define AD7998_CYC_TCONF_1024                   0x6
80 #define AD7998_CYC_TCONF_2048                   0x7
81
82 #define AD7998_ALERT_STAT_CLEAR                 0xFF
83
84 /*
85  * AD7997 and AD7997 defines
86  */
87
88 #define AD7997_8_READ_SINGLE                    0x80
89 #define AD7997_8_READ_SEQUENCE                  0x70
90 /* TODO: move this into a common header */
91 #define RES_MASK(bits)  ((1 << (bits)) - 1)
92
93 enum {
94         ad7991,
95         ad7995,
96         ad7999,
97         ad7992,
98         ad7993,
99         ad7994,
100         ad7997,
101         ad7998
102 };
103
104 /**
105  * struct ad799x_chip_info - chip specific information
106  * @channel:            channel specification
107  * @num_channels:       number of channels
108  * @default_config:     device default configuration
109  * @info:               pointer to iio_info struct
110  */
111 struct ad799x_chip_info {
112         struct iio_chan_spec            channel[9];
113         int                             num_channels;
114         u16                             default_config;
115         const struct iio_info           *info;
116 };
117
118 struct ad799x_state {
119         struct i2c_client               *client;
120         const struct ad799x_chip_info   *chip_info;
121         struct regulator                *reg;
122         struct regulator                *vref;
123         unsigned                        id;
124         u16                             config;
125
126         u8                              *rx_buf;
127         unsigned int                    transfer_size;
128 };
129
130 /**
131  * ad799x_trigger_handler() bh of trigger launched polling to ring buffer
132  *
133  * Currently there is no option in this driver to disable the saving of
134  * timestamps within the ring.
135  **/
136 static irqreturn_t ad799x_trigger_handler(int irq, void *p)
137 {
138         struct iio_poll_func *pf = p;
139         struct iio_dev *indio_dev = pf->indio_dev;
140         struct ad799x_state *st = iio_priv(indio_dev);
141         int b_sent;
142         u8 cmd;
143
144         switch (st->id) {
145         case ad7991:
146         case ad7995:
147         case ad7999:
148                 cmd = st->config |
149                         (*indio_dev->active_scan_mask << AD799X_CHANNEL_SHIFT);
150                 break;
151         case ad7992:
152         case ad7993:
153         case ad7994:
154                 cmd = (*indio_dev->active_scan_mask << AD799X_CHANNEL_SHIFT) |
155                         AD7998_CONV_RES_REG;
156                 break;
157         case ad7997:
158         case ad7998:
159                 cmd = AD7997_8_READ_SEQUENCE | AD7998_CONV_RES_REG;
160                 break;
161         default:
162                 cmd = 0;
163         }
164
165         b_sent = i2c_smbus_read_i2c_block_data(st->client,
166                         cmd, st->transfer_size, st->rx_buf);
167         if (b_sent < 0)
168                 goto out;
169
170         iio_push_to_buffers_with_timestamp(indio_dev, st->rx_buf,
171                         iio_get_time_ns());
172 out:
173         iio_trigger_notify_done(indio_dev->trig);
174
175         return IRQ_HANDLED;
176 }
177
178 static int ad7997_8_update_scan_mode(struct iio_dev *indio_dev,
179         const unsigned long *scan_mask)
180 {
181         struct ad799x_state *st = iio_priv(indio_dev);
182
183         kfree(st->rx_buf);
184         st->rx_buf = kmalloc(indio_dev->scan_bytes, GFP_KERNEL);
185         if (!st->rx_buf)
186                 return -ENOMEM;
187
188         st->transfer_size = bitmap_weight(scan_mask, indio_dev->masklength) * 2;
189
190         switch (st->id) {
191         case ad7997:
192         case ad7998:
193                 return i2c_smbus_write_word_swapped(st->client, AD7998_CONF_REG,
194                         st->config | (*scan_mask << AD799X_CHANNEL_SHIFT));
195         default:
196                 break;
197         }
198
199         return 0;
200 }
201
202 static int ad799x_scan_direct(struct ad799x_state *st, unsigned ch)
203 {
204         u8 cmd;
205
206         switch (st->id) {
207         case ad7991:
208         case ad7995:
209         case ad7999:
210                 cmd = st->config | ((1 << ch) << AD799X_CHANNEL_SHIFT);
211                 break;
212         case ad7992:
213         case ad7993:
214         case ad7994:
215                 cmd = (1 << ch) << AD799X_CHANNEL_SHIFT;
216                 break;
217         case ad7997:
218         case ad7998:
219                 cmd = (ch << AD799X_CHANNEL_SHIFT) | AD7997_8_READ_SINGLE;
220                 break;
221         default:
222                 return -EINVAL;
223         }
224
225         return i2c_smbus_read_word_swapped(st->client, cmd);
226 }
227
228 static int ad799x_read_raw(struct iio_dev *indio_dev,
229                            struct iio_chan_spec const *chan,
230                            int *val,
231                            int *val2,
232                            long m)
233 {
234         int ret;
235         struct ad799x_state *st = iio_priv(indio_dev);
236
237         switch (m) {
238         case IIO_CHAN_INFO_RAW:
239                 mutex_lock(&indio_dev->mlock);
240                 if (iio_buffer_enabled(indio_dev))
241                         ret = -EBUSY;
242                 else
243                         ret = ad799x_scan_direct(st, chan->scan_index);
244                 mutex_unlock(&indio_dev->mlock);
245
246                 if (ret < 0)
247                         return ret;
248                 *val = (ret >> chan->scan_type.shift) &
249                         RES_MASK(chan->scan_type.realbits);
250                 return IIO_VAL_INT;
251         case IIO_CHAN_INFO_SCALE:
252                 ret = regulator_get_voltage(st->vref);
253                 if (ret < 0)
254                         return ret;
255                 *val = ret / 1000;
256                 *val2 = chan->scan_type.realbits;
257                 return IIO_VAL_FRACTIONAL_LOG2;
258         }
259         return -EINVAL;
260 }
261 static const unsigned int ad7998_frequencies[] = {
262         [AD7998_CYC_DIS]        = 0,
263         [AD7998_CYC_TCONF_32]   = 15625,
264         [AD7998_CYC_TCONF_64]   = 7812,
265         [AD7998_CYC_TCONF_128]  = 3906,
266         [AD7998_CYC_TCONF_512]  = 976,
267         [AD7998_CYC_TCONF_1024] = 488,
268         [AD7998_CYC_TCONF_2048] = 244,
269 };
270
271 static ssize_t ad799x_read_frequency(struct device *dev,
272                                         struct device_attribute *attr,
273                                         char *buf)
274 {
275         struct iio_dev *indio_dev = dev_to_iio_dev(dev);
276         struct ad799x_state *st = iio_priv(indio_dev);
277
278         int ret = i2c_smbus_read_byte_data(st->client, AD7998_CYCLE_TMR_REG);
279         if (ret < 0)
280                 return ret;
281
282         return sprintf(buf, "%u\n", ad7998_frequencies[ret & AD7998_CYC_MASK]);
283 }
284
285 static ssize_t ad799x_write_frequency(struct device *dev,
286                                          struct device_attribute *attr,
287                                          const char *buf,
288                                          size_t len)
289 {
290         struct iio_dev *indio_dev = dev_to_iio_dev(dev);
291         struct ad799x_state *st = iio_priv(indio_dev);
292
293         long val;
294         int ret, i;
295
296         ret = kstrtol(buf, 10, &val);
297         if (ret)
298                 return ret;
299
300         mutex_lock(&indio_dev->mlock);
301         ret = i2c_smbus_read_byte_data(st->client, AD7998_CYCLE_TMR_REG);
302         if (ret < 0)
303                 goto error_ret_mutex;
304         /* Wipe the bits clean */
305         ret &= ~AD7998_CYC_MASK;
306
307         for (i = 0; i < ARRAY_SIZE(ad7998_frequencies); i++)
308                 if (val == ad7998_frequencies[i])
309                         break;
310         if (i == ARRAY_SIZE(ad7998_frequencies)) {
311                 ret = -EINVAL;
312                 goto error_ret_mutex;
313         }
314
315         ret = i2c_smbus_write_byte_data(st->client, AD7998_CYCLE_TMR_REG,
316                 ret | i);
317         if (ret < 0)
318                 goto error_ret_mutex;
319         ret = len;
320
321 error_ret_mutex:
322         mutex_unlock(&indio_dev->mlock);
323
324         return ret;
325 }
326
327 static int ad799x_read_event_config(struct iio_dev *indio_dev,
328                                     const struct iio_chan_spec *chan,
329                                     enum iio_event_type type,
330                                     enum iio_event_direction dir)
331 {
332         return 1;
333 }
334
335 static unsigned int ad799x_threshold_reg(const struct iio_chan_spec *chan,
336                                          enum iio_event_direction dir,
337                                          enum iio_event_info info)
338 {
339         switch (info) {
340         case IIO_EV_INFO_VALUE:
341                 if (dir == IIO_EV_DIR_FALLING)
342                         return AD7998_DATALOW_REG(chan->channel);
343                 else
344                         return AD7998_DATAHIGH_REG(chan->channel);
345         case IIO_EV_INFO_HYSTERESIS:
346                 return AD7998_HYST_REG(chan->channel);
347         default:
348                 return -EINVAL;
349         }
350
351         return 0;
352 }
353
354 static int ad799x_write_event_value(struct iio_dev *indio_dev,
355                                     const struct iio_chan_spec *chan,
356                                     enum iio_event_type type,
357                                     enum iio_event_direction dir,
358                                     enum iio_event_info info,
359                                     int val, int val2)
360 {
361         int ret;
362         struct ad799x_state *st = iio_priv(indio_dev);
363
364         if (val < 0 || val > RES_MASK(chan->scan_type.realbits))
365                 return -EINVAL;
366
367         mutex_lock(&indio_dev->mlock);
368         ret = i2c_smbus_write_word_swapped(st->client,
369                 ad799x_threshold_reg(chan, dir, info),
370                 val << chan->scan_type.shift);
371         mutex_unlock(&indio_dev->mlock);
372
373         return ret;
374 }
375
376 static int ad799x_read_event_value(struct iio_dev *indio_dev,
377                                     const struct iio_chan_spec *chan,
378                                     enum iio_event_type type,
379                                     enum iio_event_direction dir,
380                                     enum iio_event_info info,
381                                     int *val, int *val2)
382 {
383         int ret;
384         struct ad799x_state *st = iio_priv(indio_dev);
385
386         mutex_lock(&indio_dev->mlock);
387         ret = i2c_smbus_read_word_swapped(st->client,
388                 ad799x_threshold_reg(chan, dir, info));
389         mutex_unlock(&indio_dev->mlock);
390         if (ret < 0)
391                 return ret;
392         *val = (ret >> chan->scan_type.shift) &
393                 RES_MASK(chan->scan_type.realbits);
394
395         return IIO_VAL_INT;
396 }
397
398 static irqreturn_t ad799x_event_handler(int irq, void *private)
399 {
400         struct iio_dev *indio_dev = private;
401         struct ad799x_state *st = iio_priv(private);
402         int i, ret;
403
404         ret = i2c_smbus_read_byte_data(st->client, AD7998_ALERT_STAT_REG);
405         if (ret <= 0)
406                 goto done;
407
408         if (i2c_smbus_write_byte_data(st->client, AD7998_ALERT_STAT_REG,
409                 AD7998_ALERT_STAT_CLEAR) < 0)
410                 goto done;
411
412         for (i = 0; i < 8; i++) {
413                 if (ret & (1 << i))
414                         iio_push_event(indio_dev,
415                                        i & 0x1 ?
416                                        IIO_UNMOD_EVENT_CODE(IIO_VOLTAGE,
417                                                             (i >> 1),
418                                                             IIO_EV_TYPE_THRESH,
419                                                             IIO_EV_DIR_RISING) :
420                                        IIO_UNMOD_EVENT_CODE(IIO_VOLTAGE,
421                                                             (i >> 1),
422                                                             IIO_EV_TYPE_THRESH,
423                                                             IIO_EV_DIR_FALLING),
424                                        iio_get_time_ns());
425         }
426
427 done:
428         return IRQ_HANDLED;
429 }
430
431 static IIO_DEV_ATTR_SAMP_FREQ(S_IWUSR | S_IRUGO,
432                               ad799x_read_frequency,
433                               ad799x_write_frequency);
434 static IIO_CONST_ATTR_SAMP_FREQ_AVAIL("15625 7812 3906 1953 976 488 244 0");
435
436 static struct attribute *ad799x_event_attributes[] = {
437         &iio_dev_attr_sampling_frequency.dev_attr.attr,
438         &iio_const_attr_sampling_frequency_available.dev_attr.attr,
439         NULL,
440 };
441
442 static struct attribute_group ad799x_event_attrs_group = {
443         .attrs = ad799x_event_attributes,
444         .name = "events",
445 };
446
447 static const struct iio_info ad7991_info = {
448         .read_raw = &ad799x_read_raw,
449         .driver_module = THIS_MODULE,
450 };
451
452 static const struct iio_info ad7993_4_7_8_info = {
453         .read_raw = &ad799x_read_raw,
454         .event_attrs = &ad799x_event_attrs_group,
455         .read_event_config = &ad799x_read_event_config,
456         .read_event_value = &ad799x_read_event_value,
457         .write_event_value = &ad799x_write_event_value,
458         .driver_module = THIS_MODULE,
459         .update_scan_mode = ad7997_8_update_scan_mode,
460 };
461
462 static const struct iio_event_spec ad799x_events[] = {
463         {
464                 .type = IIO_EV_TYPE_THRESH,
465                 .dir = IIO_EV_DIR_RISING,
466                 .mask_separate = BIT(IIO_EV_INFO_VALUE) |
467                         BIT(IIO_EV_INFO_ENABLE),
468         }, {
469                 .type = IIO_EV_TYPE_THRESH,
470                 .dir = IIO_EV_DIR_FALLING,
471                 .mask_separate = BIT(IIO_EV_INFO_VALUE) |
472                         BIT(IIO_EV_INFO_ENABLE),
473         }, {
474                 .type = IIO_EV_TYPE_THRESH,
475                 .dir = IIO_EV_DIR_EITHER,
476                 .mask_separate = BIT(IIO_EV_INFO_HYSTERESIS),
477         },
478 };
479
480 #define _AD799X_CHANNEL(_index, _realbits, _ev_spec, _num_ev_spec) { \
481         .type = IIO_VOLTAGE, \
482         .indexed = 1, \
483         .channel = (_index), \
484         .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \
485         .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE), \
486         .scan_index = (_index), \
487         .scan_type = { \
488                 .sign = 'u', \
489                 .realbits = (_realbits), \
490                 .storagebits = 16, \
491                 .shift = 12 - (_realbits), \
492                 .endianness = IIO_BE, \
493         }, \
494         .event_spec = _ev_spec, \
495         .num_event_specs = _num_ev_spec, \
496 }
497
498 #define AD799X_CHANNEL(_index, _realbits) \
499         _AD799X_CHANNEL(_index, _realbits, NULL, 0)
500
501 #define AD799X_CHANNEL_WITH_EVENTS(_index, _realbits) \
502         _AD799X_CHANNEL(_index, _realbits, ad799x_events, \
503                 ARRAY_SIZE(ad799x_events))
504
505 static const struct ad799x_chip_info ad799x_chip_info_tbl[] = {
506         [ad7991] = {
507                 .channel = {
508                         AD799X_CHANNEL(0, 12),
509                         AD799X_CHANNEL(1, 12),
510                         AD799X_CHANNEL(2, 12),
511                         AD799X_CHANNEL(3, 12),
512                         IIO_CHAN_SOFT_TIMESTAMP(4),
513                 },
514                 .num_channels = 5,
515                 .info = &ad7991_info,
516         },
517         [ad7995] = {
518                 .channel = {
519                         AD799X_CHANNEL(0, 10),
520                         AD799X_CHANNEL(1, 10),
521                         AD799X_CHANNEL(2, 10),
522                         AD799X_CHANNEL(3, 10),
523                         IIO_CHAN_SOFT_TIMESTAMP(4),
524                 },
525                 .num_channels = 5,
526                 .info = &ad7991_info,
527         },
528         [ad7999] = {
529                 .channel = {
530                         AD799X_CHANNEL(0, 8),
531                         AD799X_CHANNEL(1, 8),
532                         AD799X_CHANNEL(2, 8),
533                         AD799X_CHANNEL(3, 8),
534                         IIO_CHAN_SOFT_TIMESTAMP(4),
535                 },
536                 .num_channels = 5,
537                 .info = &ad7991_info,
538         },
539         [ad7992] = {
540                 .channel = {
541                         AD799X_CHANNEL_WITH_EVENTS(0, 12),
542                         AD799X_CHANNEL_WITH_EVENTS(1, 12),
543                         IIO_CHAN_SOFT_TIMESTAMP(3),
544                 },
545                 .num_channels = 3,
546                 .default_config = AD7998_ALERT_EN,
547                 .info = &ad7993_4_7_8_info,
548         },
549         [ad7993] = {
550                 .channel = {
551                         AD799X_CHANNEL_WITH_EVENTS(0, 10),
552                         AD799X_CHANNEL_WITH_EVENTS(1, 10),
553                         AD799X_CHANNEL_WITH_EVENTS(2, 10),
554                         AD799X_CHANNEL_WITH_EVENTS(3, 10),
555                         IIO_CHAN_SOFT_TIMESTAMP(4),
556                 },
557                 .num_channels = 5,
558                 .default_config = AD7998_ALERT_EN,
559                 .info = &ad7993_4_7_8_info,
560         },
561         [ad7994] = {
562                 .channel = {
563                         AD799X_CHANNEL_WITH_EVENTS(0, 12),
564                         AD799X_CHANNEL_WITH_EVENTS(1, 12),
565                         AD799X_CHANNEL_WITH_EVENTS(2, 12),
566                         AD799X_CHANNEL_WITH_EVENTS(3, 12),
567                         IIO_CHAN_SOFT_TIMESTAMP(4),
568                 },
569                 .num_channels = 5,
570                 .default_config = AD7998_ALERT_EN,
571                 .info = &ad7993_4_7_8_info,
572         },
573         [ad7997] = {
574                 .channel = {
575                         AD799X_CHANNEL_WITH_EVENTS(0, 10),
576                         AD799X_CHANNEL_WITH_EVENTS(1, 10),
577                         AD799X_CHANNEL_WITH_EVENTS(2, 10),
578                         AD799X_CHANNEL_WITH_EVENTS(3, 10),
579                         AD799X_CHANNEL(4, 10),
580                         AD799X_CHANNEL(5, 10),
581                         AD799X_CHANNEL(6, 10),
582                         AD799X_CHANNEL(7, 10),
583                         IIO_CHAN_SOFT_TIMESTAMP(8),
584                 },
585                 .num_channels = 9,
586                 .default_config = AD7998_ALERT_EN,
587                 .info = &ad7993_4_7_8_info,
588         },
589         [ad7998] = {
590                 .channel = {
591                         AD799X_CHANNEL_WITH_EVENTS(0, 12),
592                         AD799X_CHANNEL_WITH_EVENTS(1, 12),
593                         AD799X_CHANNEL_WITH_EVENTS(2, 12),
594                         AD799X_CHANNEL_WITH_EVENTS(3, 12),
595                         AD799X_CHANNEL(4, 12),
596                         AD799X_CHANNEL(5, 12),
597                         AD799X_CHANNEL(6, 12),
598                         AD799X_CHANNEL(7, 12),
599                         IIO_CHAN_SOFT_TIMESTAMP(8),
600                 },
601                 .num_channels = 9,
602                 .default_config = AD7998_ALERT_EN,
603                 .info = &ad7993_4_7_8_info,
604         },
605 };
606
607 static int ad799x_probe(struct i2c_client *client,
608                                    const struct i2c_device_id *id)
609 {
610         int ret;
611         struct ad799x_state *st;
612         struct iio_dev *indio_dev;
613
614         indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*st));
615         if (indio_dev == NULL)
616                 return -ENOMEM;
617
618         st = iio_priv(indio_dev);
619         /* this is only used for device removal purposes */
620         i2c_set_clientdata(client, indio_dev);
621
622         st->id = id->driver_data;
623         st->chip_info = &ad799x_chip_info_tbl[st->id];
624         st->config = st->chip_info->default_config;
625
626         /* TODO: Add pdata options for filtering and bit delay */
627
628         st->reg = devm_regulator_get(&client->dev, "vcc");
629         if (IS_ERR(st->reg))
630                 return PTR_ERR(st->reg);
631         ret = regulator_enable(st->reg);
632         if (ret)
633                 return ret;
634         st->vref = devm_regulator_get(&client->dev, "vref");
635         if (IS_ERR(st->vref)) {
636                 ret = PTR_ERR(st->vref);
637                 goto error_disable_reg;
638         }
639         ret = regulator_enable(st->vref);
640         if (ret)
641                 goto error_disable_reg;
642
643         st->client = client;
644
645         indio_dev->dev.parent = &client->dev;
646         indio_dev->name = id->name;
647         indio_dev->info = st->chip_info->info;
648
649         indio_dev->modes = INDIO_DIRECT_MODE;
650         indio_dev->channels = st->chip_info->channel;
651         indio_dev->num_channels = st->chip_info->num_channels;
652
653         ret = iio_triggered_buffer_setup(indio_dev, NULL,
654                 &ad799x_trigger_handler, NULL);
655         if (ret)
656                 goto error_disable_vref;
657
658         if (client->irq > 0) {
659                 ret = devm_request_threaded_irq(&client->dev,
660                                                 client->irq,
661                                                 NULL,
662                                                 ad799x_event_handler,
663                                                 IRQF_TRIGGER_FALLING |
664                                                 IRQF_ONESHOT,
665                                                 client->name,
666                                                 indio_dev);
667                 if (ret)
668                         goto error_cleanup_ring;
669         }
670         ret = iio_device_register(indio_dev);
671         if (ret)
672                 goto error_cleanup_ring;
673
674         return 0;
675
676 error_cleanup_ring:
677         iio_triggered_buffer_cleanup(indio_dev);
678 error_disable_vref:
679         regulator_disable(st->vref);
680 error_disable_reg:
681         regulator_disable(st->reg);
682
683         return ret;
684 }
685
686 static int ad799x_remove(struct i2c_client *client)
687 {
688         struct iio_dev *indio_dev = i2c_get_clientdata(client);
689         struct ad799x_state *st = iio_priv(indio_dev);
690
691         iio_device_unregister(indio_dev);
692
693         iio_triggered_buffer_cleanup(indio_dev);
694         regulator_disable(st->vref);
695         regulator_disable(st->reg);
696         kfree(st->rx_buf);
697
698         return 0;
699 }
700
701 static const struct i2c_device_id ad799x_id[] = {
702         { "ad7991", ad7991 },
703         { "ad7995", ad7995 },
704         { "ad7999", ad7999 },
705         { "ad7992", ad7992 },
706         { "ad7993", ad7993 },
707         { "ad7994", ad7994 },
708         { "ad7997", ad7997 },
709         { "ad7998", ad7998 },
710         {}
711 };
712
713 MODULE_DEVICE_TABLE(i2c, ad799x_id);
714
715 static struct i2c_driver ad799x_driver = {
716         .driver = {
717                 .name = "ad799x",
718         },
719         .probe = ad799x_probe,
720         .remove = ad799x_remove,
721         .id_table = ad799x_id,
722 };
723 module_i2c_driver(ad799x_driver);
724
725 MODULE_AUTHOR("Michael Hennerich <hennerich@blackfin.uclinux.org>");
726 MODULE_DESCRIPTION("Analog Devices AD799x ADC");
727 MODULE_LICENSE("GPL v2");