]> git.kernelconcepts.de Git - karo-tx-linux.git/blob - drivers/iio/adc/ad799x.c
Linux 3.16-rc4
[karo-tx-linux.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  * @monitor_mode:       whether the chip supports monitor interrupts
109  * @default_config:     device default configuration
110  * @event_attrs:        pointer to the monitor event attribute group
111  */
112 struct ad799x_chip_info {
113         struct iio_chan_spec            channel[9];
114         int                             num_channels;
115         u16                             default_config;
116         const struct iio_info           *info;
117 };
118
119 struct ad799x_state {
120         struct i2c_client               *client;
121         const struct ad799x_chip_info   *chip_info;
122         struct regulator                *reg;
123         struct regulator                *vref;
124         unsigned                        id;
125         u16                             config;
126
127         u8                              *rx_buf;
128         unsigned int                    transfer_size;
129 };
130
131 /**
132  * ad799x_trigger_handler() bh of trigger launched polling to ring buffer
133  *
134  * Currently there is no option in this driver to disable the saving of
135  * timestamps within the ring.
136  **/
137 static irqreturn_t ad799x_trigger_handler(int irq, void *p)
138 {
139         struct iio_poll_func *pf = p;
140         struct iio_dev *indio_dev = pf->indio_dev;
141         struct ad799x_state *st = iio_priv(indio_dev);
142         int b_sent;
143         u8 cmd;
144
145         switch (st->id) {
146         case ad7991:
147         case ad7995:
148         case ad7999:
149                 cmd = st->config |
150                         (*indio_dev->active_scan_mask << AD799X_CHANNEL_SHIFT);
151                 break;
152         case ad7992:
153         case ad7993:
154         case ad7994:
155                 cmd = (*indio_dev->active_scan_mask << AD799X_CHANNEL_SHIFT) |
156                         AD7998_CONV_RES_REG;
157                 break;
158         case ad7997:
159         case ad7998:
160                 cmd = AD7997_8_READ_SEQUENCE | AD7998_CONV_RES_REG;
161                 break;
162         default:
163                 cmd = 0;
164         }
165
166         b_sent = i2c_smbus_read_i2c_block_data(st->client,
167                         cmd, st->transfer_size, st->rx_buf);
168         if (b_sent < 0)
169                 goto out;
170
171         iio_push_to_buffers_with_timestamp(indio_dev, st->rx_buf,
172                         iio_get_time_ns());
173 out:
174         iio_trigger_notify_done(indio_dev->trig);
175
176         return IRQ_HANDLED;
177 }
178
179 /*
180  * ad799x register access by I2C
181  */
182 static int ad799x_i2c_read16(struct ad799x_state *st, u8 reg, u16 *data)
183 {
184         struct i2c_client *client = st->client;
185         int ret = 0;
186
187         ret = i2c_smbus_read_word_swapped(client, reg);
188         if (ret < 0) {
189                 dev_err(&client->dev, "I2C read error\n");
190                 return ret;
191         }
192
193         *data = (u16)ret;
194
195         return 0;
196 }
197
198 static int ad799x_i2c_read8(struct ad799x_state *st, u8 reg, u8 *data)
199 {
200         struct i2c_client *client = st->client;
201         int ret = 0;
202
203         ret = i2c_smbus_read_byte_data(client, reg);
204         if (ret < 0) {
205                 dev_err(&client->dev, "I2C read error\n");
206                 return ret;
207         }
208
209         *data = (u8)ret;
210
211         return 0;
212 }
213
214 static int ad799x_i2c_write16(struct ad799x_state *st, u8 reg, u16 data)
215 {
216         struct i2c_client *client = st->client;
217         int ret = 0;
218
219         ret = i2c_smbus_write_word_swapped(client, reg, data);
220         if (ret < 0)
221                 dev_err(&client->dev, "I2C write error\n");
222
223         return ret;
224 }
225
226 static int ad799x_i2c_write8(struct ad799x_state *st, u8 reg, u8 data)
227 {
228         struct i2c_client *client = st->client;
229         int ret = 0;
230
231         ret = i2c_smbus_write_byte_data(client, reg, data);
232         if (ret < 0)
233                 dev_err(&client->dev, "I2C write error\n");
234
235         return ret;
236 }
237
238 static int ad7997_8_update_scan_mode(struct iio_dev *indio_dev,
239         const unsigned long *scan_mask)
240 {
241         struct ad799x_state *st = iio_priv(indio_dev);
242
243         kfree(st->rx_buf);
244         st->rx_buf = kmalloc(indio_dev->scan_bytes, GFP_KERNEL);
245         if (!st->rx_buf)
246                 return -ENOMEM;
247
248         st->transfer_size = bitmap_weight(scan_mask, indio_dev->masklength) * 2;
249
250         switch (st->id) {
251         case ad7997:
252         case ad7998:
253                 return ad799x_i2c_write16(st, AD7998_CONF_REG,
254                         st->config | (*scan_mask << AD799X_CHANNEL_SHIFT));
255         default:
256                 break;
257         }
258
259         return 0;
260 }
261
262 static int ad799x_scan_direct(struct ad799x_state *st, unsigned ch)
263 {
264         u16 rxbuf;
265         u8 cmd;
266         int ret;
267
268         switch (st->id) {
269         case ad7991:
270         case ad7995:
271         case ad7999:
272                 cmd = st->config | ((1 << ch) << AD799X_CHANNEL_SHIFT);
273                 break;
274         case ad7992:
275         case ad7993:
276         case ad7994:
277                 cmd = (1 << ch) << AD799X_CHANNEL_SHIFT;
278                 break;
279         case ad7997:
280         case ad7998:
281                 cmd = (ch << AD799X_CHANNEL_SHIFT) | AD7997_8_READ_SINGLE;
282                 break;
283         default:
284                 return -EINVAL;
285         }
286
287         ret = ad799x_i2c_read16(st, cmd, &rxbuf);
288         if (ret < 0)
289                 return ret;
290
291         return rxbuf;
292 }
293
294 static int ad799x_read_raw(struct iio_dev *indio_dev,
295                            struct iio_chan_spec const *chan,
296                            int *val,
297                            int *val2,
298                            long m)
299 {
300         int ret;
301         struct ad799x_state *st = iio_priv(indio_dev);
302
303         switch (m) {
304         case IIO_CHAN_INFO_RAW:
305                 mutex_lock(&indio_dev->mlock);
306                 if (iio_buffer_enabled(indio_dev))
307                         ret = -EBUSY;
308                 else
309                         ret = ad799x_scan_direct(st, chan->scan_index);
310                 mutex_unlock(&indio_dev->mlock);
311
312                 if (ret < 0)
313                         return ret;
314                 *val = (ret >> chan->scan_type.shift) &
315                         RES_MASK(chan->scan_type.realbits);
316                 return IIO_VAL_INT;
317         case IIO_CHAN_INFO_SCALE:
318                 ret = regulator_get_voltage(st->vref);
319                 if (ret < 0)
320                         return ret;
321                 *val = ret / 1000;
322                 *val2 = chan->scan_type.realbits;
323                 return IIO_VAL_FRACTIONAL_LOG2;
324         }
325         return -EINVAL;
326 }
327 static const unsigned int ad7998_frequencies[] = {
328         [AD7998_CYC_DIS]        = 0,
329         [AD7998_CYC_TCONF_32]   = 15625,
330         [AD7998_CYC_TCONF_64]   = 7812,
331         [AD7998_CYC_TCONF_128]  = 3906,
332         [AD7998_CYC_TCONF_512]  = 976,
333         [AD7998_CYC_TCONF_1024] = 488,
334         [AD7998_CYC_TCONF_2048] = 244,
335 };
336 static ssize_t ad799x_read_frequency(struct device *dev,
337                                         struct device_attribute *attr,
338                                         char *buf)
339 {
340         struct iio_dev *indio_dev = dev_to_iio_dev(dev);
341         struct ad799x_state *st = iio_priv(indio_dev);
342
343         int ret;
344         u8 val;
345         ret = ad799x_i2c_read8(st, AD7998_CYCLE_TMR_REG, &val);
346         if (ret)
347                 return ret;
348
349         val &= AD7998_CYC_MASK;
350
351         return sprintf(buf, "%u\n", ad7998_frequencies[val]);
352 }
353
354 static ssize_t ad799x_write_frequency(struct device *dev,
355                                          struct device_attribute *attr,
356                                          const char *buf,
357                                          size_t len)
358 {
359         struct iio_dev *indio_dev = dev_to_iio_dev(dev);
360         struct ad799x_state *st = iio_priv(indio_dev);
361
362         long val;
363         int ret, i;
364         u8 t;
365
366         ret = kstrtol(buf, 10, &val);
367         if (ret)
368                 return ret;
369
370         mutex_lock(&indio_dev->mlock);
371         ret = ad799x_i2c_read8(st, AD7998_CYCLE_TMR_REG, &t);
372         if (ret)
373                 goto error_ret_mutex;
374         /* Wipe the bits clean */
375         t &= ~AD7998_CYC_MASK;
376
377         for (i = 0; i < ARRAY_SIZE(ad7998_frequencies); i++)
378                 if (val == ad7998_frequencies[i])
379                         break;
380         if (i == ARRAY_SIZE(ad7998_frequencies)) {
381                 ret = -EINVAL;
382                 goto error_ret_mutex;
383         }
384         t |= i;
385         ret = ad799x_i2c_write8(st, AD7998_CYCLE_TMR_REG, t);
386
387 error_ret_mutex:
388         mutex_unlock(&indio_dev->mlock);
389
390         return ret ? ret : len;
391 }
392
393 static int ad799x_read_event_config(struct iio_dev *indio_dev,
394                                     const struct iio_chan_spec *chan,
395                                     enum iio_event_type type,
396                                     enum iio_event_direction dir)
397 {
398         return 1;
399 }
400
401 static unsigned int ad799x_threshold_reg(const struct iio_chan_spec *chan,
402                                          enum iio_event_direction dir,
403                                          enum iio_event_info info)
404 {
405         switch (info) {
406         case IIO_EV_INFO_VALUE:
407                 if (dir == IIO_EV_DIR_FALLING)
408                         return AD7998_DATALOW_REG(chan->channel);
409                 else
410                         return AD7998_DATAHIGH_REG(chan->channel);
411         case IIO_EV_INFO_HYSTERESIS:
412                 return AD7998_HYST_REG(chan->channel);
413         default:
414                 return -EINVAL;
415         }
416
417         return 0;
418 }
419
420 static int ad799x_write_event_value(struct iio_dev *indio_dev,
421                                     const struct iio_chan_spec *chan,
422                                     enum iio_event_type type,
423                                     enum iio_event_direction dir,
424                                     enum iio_event_info info,
425                                     int val, int val2)
426 {
427         int ret;
428         struct ad799x_state *st = iio_priv(indio_dev);
429
430         if (val < 0 || val > RES_MASK(chan->scan_type.realbits))
431                 return -EINVAL;
432
433         mutex_lock(&indio_dev->mlock);
434         ret = ad799x_i2c_write16(st, ad799x_threshold_reg(chan, dir, info),
435                 val << chan->scan_type.shift);
436         mutex_unlock(&indio_dev->mlock);
437
438         return ret;
439 }
440
441 static int ad799x_read_event_value(struct iio_dev *indio_dev,
442                                     const struct iio_chan_spec *chan,
443                                     enum iio_event_type type,
444                                     enum iio_event_direction dir,
445                                     enum iio_event_info info,
446                                     int *val, int *val2)
447 {
448         int ret;
449         struct ad799x_state *st = iio_priv(indio_dev);
450         u16 valin;
451
452         mutex_lock(&indio_dev->mlock);
453         ret = ad799x_i2c_read16(st, ad799x_threshold_reg(chan, dir, info),
454                 &valin);
455         mutex_unlock(&indio_dev->mlock);
456         if (ret < 0)
457                 return ret;
458         *val = (valin >> chan->scan_type.shift) &
459                 RES_MASK(chan->scan_type.realbits);
460
461         return IIO_VAL_INT;
462 }
463
464 static irqreturn_t ad799x_event_handler(int irq, void *private)
465 {
466         struct iio_dev *indio_dev = private;
467         struct ad799x_state *st = iio_priv(private);
468         u8 status;
469         int i, ret;
470
471         ret = ad799x_i2c_read8(st, AD7998_ALERT_STAT_REG, &status);
472         if (ret)
473                 goto done;
474
475         if (!status)
476                 goto done;
477
478         ad799x_i2c_write8(st, AD7998_ALERT_STAT_REG, AD7998_ALERT_STAT_CLEAR);
479
480         for (i = 0; i < 8; i++) {
481                 if (status & (1 << i))
482                         iio_push_event(indio_dev,
483                                        i & 0x1 ?
484                                        IIO_UNMOD_EVENT_CODE(IIO_VOLTAGE,
485                                                             (i >> 1),
486                                                             IIO_EV_TYPE_THRESH,
487                                                             IIO_EV_DIR_RISING) :
488                                        IIO_UNMOD_EVENT_CODE(IIO_VOLTAGE,
489                                                             (i >> 1),
490                                                             IIO_EV_TYPE_THRESH,
491                                                             IIO_EV_DIR_FALLING),
492                                        iio_get_time_ns());
493         }
494
495 done:
496         return IRQ_HANDLED;
497 }
498
499 static IIO_DEV_ATTR_SAMP_FREQ(S_IWUSR | S_IRUGO,
500                               ad799x_read_frequency,
501                               ad799x_write_frequency);
502 static IIO_CONST_ATTR_SAMP_FREQ_AVAIL("15625 7812 3906 1953 976 488 244 0");
503
504 static struct attribute *ad799x_event_attributes[] = {
505         &iio_dev_attr_sampling_frequency.dev_attr.attr,
506         &iio_const_attr_sampling_frequency_available.dev_attr.attr,
507         NULL,
508 };
509
510 static struct attribute_group ad799x_event_attrs_group = {
511         .attrs = ad799x_event_attributes,
512         .name = "events",
513 };
514
515 static const struct iio_info ad7991_info = {
516         .read_raw = &ad799x_read_raw,
517         .driver_module = THIS_MODULE,
518 };
519
520 static const struct iio_info ad7993_4_7_8_info = {
521         .read_raw = &ad799x_read_raw,
522         .event_attrs = &ad799x_event_attrs_group,
523         .read_event_config = &ad799x_read_event_config,
524         .read_event_value = &ad799x_read_event_value,
525         .write_event_value = &ad799x_write_event_value,
526         .driver_module = THIS_MODULE,
527         .update_scan_mode = ad7997_8_update_scan_mode,
528 };
529
530 static const struct iio_event_spec ad799x_events[] = {
531         {
532                 .type = IIO_EV_TYPE_THRESH,
533                 .dir = IIO_EV_DIR_RISING,
534                 .mask_separate = BIT(IIO_EV_INFO_VALUE) |
535                         BIT(IIO_EV_INFO_ENABLE),
536         }, {
537                 .type = IIO_EV_TYPE_THRESH,
538                 .dir = IIO_EV_DIR_FALLING,
539                 .mask_separate = BIT(IIO_EV_INFO_VALUE) |
540                         BIT(IIO_EV_INFO_ENABLE),
541         }, {
542                 .type = IIO_EV_TYPE_THRESH,
543                 .dir = IIO_EV_DIR_EITHER,
544                 .mask_separate = BIT(IIO_EV_INFO_HYSTERESIS),
545         },
546 };
547
548 #define _AD799X_CHANNEL(_index, _realbits, _ev_spec, _num_ev_spec) { \
549         .type = IIO_VOLTAGE, \
550         .indexed = 1, \
551         .channel = (_index), \
552         .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \
553         .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE), \
554         .scan_index = (_index), \
555         .scan_type = { \
556                 .sign = 'u', \
557                 .realbits = (_realbits), \
558                 .storagebits = 16, \
559                 .shift = 12 - (_realbits), \
560                 .endianness = IIO_BE, \
561         }, \
562         .event_spec = _ev_spec, \
563         .num_event_specs = _num_ev_spec, \
564 }
565
566 #define AD799X_CHANNEL(_index, _realbits) \
567         _AD799X_CHANNEL(_index, _realbits, NULL, 0)
568
569 #define AD799X_CHANNEL_WITH_EVENTS(_index, _realbits) \
570         _AD799X_CHANNEL(_index, _realbits, ad799x_events, \
571                 ARRAY_SIZE(ad799x_events))
572
573 static const struct ad799x_chip_info ad799x_chip_info_tbl[] = {
574         [ad7991] = {
575                 .channel = {
576                         AD799X_CHANNEL(0, 12),
577                         AD799X_CHANNEL(1, 12),
578                         AD799X_CHANNEL(2, 12),
579                         AD799X_CHANNEL(3, 12),
580                         IIO_CHAN_SOFT_TIMESTAMP(4),
581                 },
582                 .num_channels = 5,
583                 .info = &ad7991_info,
584         },
585         [ad7995] = {
586                 .channel = {
587                         AD799X_CHANNEL(0, 10),
588                         AD799X_CHANNEL(1, 10),
589                         AD799X_CHANNEL(2, 10),
590                         AD799X_CHANNEL(3, 10),
591                         IIO_CHAN_SOFT_TIMESTAMP(4),
592                 },
593                 .num_channels = 5,
594                 .info = &ad7991_info,
595         },
596         [ad7999] = {
597                 .channel = {
598                         AD799X_CHANNEL(0, 8),
599                         AD799X_CHANNEL(1, 8),
600                         AD799X_CHANNEL(2, 8),
601                         AD799X_CHANNEL(3, 8),
602                         IIO_CHAN_SOFT_TIMESTAMP(4),
603                 },
604                 .num_channels = 5,
605                 .info = &ad7991_info,
606         },
607         [ad7992] = {
608                 .channel = {
609                         AD799X_CHANNEL_WITH_EVENTS(0, 12),
610                         AD799X_CHANNEL_WITH_EVENTS(1, 12),
611                         IIO_CHAN_SOFT_TIMESTAMP(3),
612                 },
613                 .num_channels = 3,
614                 .default_config = AD7998_ALERT_EN,
615                 .info = &ad7993_4_7_8_info,
616         },
617         [ad7993] = {
618                 .channel = {
619                         AD799X_CHANNEL_WITH_EVENTS(0, 10),
620                         AD799X_CHANNEL_WITH_EVENTS(1, 10),
621                         AD799X_CHANNEL_WITH_EVENTS(2, 10),
622                         AD799X_CHANNEL_WITH_EVENTS(3, 10),
623                         IIO_CHAN_SOFT_TIMESTAMP(4),
624                 },
625                 .num_channels = 5,
626                 .default_config = AD7998_ALERT_EN,
627                 .info = &ad7993_4_7_8_info,
628         },
629         [ad7994] = {
630                 .channel = {
631                         AD799X_CHANNEL_WITH_EVENTS(0, 12),
632                         AD799X_CHANNEL_WITH_EVENTS(1, 12),
633                         AD799X_CHANNEL_WITH_EVENTS(2, 12),
634                         AD799X_CHANNEL_WITH_EVENTS(3, 12),
635                         IIO_CHAN_SOFT_TIMESTAMP(4),
636                 },
637                 .num_channels = 5,
638                 .default_config = AD7998_ALERT_EN,
639                 .info = &ad7993_4_7_8_info,
640         },
641         [ad7997] = {
642                 .channel = {
643                         AD799X_CHANNEL_WITH_EVENTS(0, 10),
644                         AD799X_CHANNEL_WITH_EVENTS(1, 10),
645                         AD799X_CHANNEL_WITH_EVENTS(2, 10),
646                         AD799X_CHANNEL_WITH_EVENTS(3, 10),
647                         AD799X_CHANNEL(4, 10),
648                         AD799X_CHANNEL(5, 10),
649                         AD799X_CHANNEL(6, 10),
650                         AD799X_CHANNEL(7, 10),
651                         IIO_CHAN_SOFT_TIMESTAMP(8),
652                 },
653                 .num_channels = 9,
654                 .default_config = AD7998_ALERT_EN,
655                 .info = &ad7993_4_7_8_info,
656         },
657         [ad7998] = {
658                 .channel = {
659                         AD799X_CHANNEL_WITH_EVENTS(0, 12),
660                         AD799X_CHANNEL_WITH_EVENTS(1, 12),
661                         AD799X_CHANNEL_WITH_EVENTS(2, 12),
662                         AD799X_CHANNEL_WITH_EVENTS(3, 12),
663                         AD799X_CHANNEL(4, 12),
664                         AD799X_CHANNEL(5, 12),
665                         AD799X_CHANNEL(6, 12),
666                         AD799X_CHANNEL(7, 12),
667                         IIO_CHAN_SOFT_TIMESTAMP(8),
668                 },
669                 .num_channels = 9,
670                 .default_config = AD7998_ALERT_EN,
671                 .info = &ad7993_4_7_8_info,
672         },
673 };
674
675 static int ad799x_probe(struct i2c_client *client,
676                                    const struct i2c_device_id *id)
677 {
678         int ret;
679         struct ad799x_state *st;
680         struct iio_dev *indio_dev;
681
682         indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*st));
683         if (indio_dev == NULL)
684                 return -ENOMEM;
685
686         st = iio_priv(indio_dev);
687         /* this is only used for device removal purposes */
688         i2c_set_clientdata(client, indio_dev);
689
690         st->id = id->driver_data;
691         st->chip_info = &ad799x_chip_info_tbl[st->id];
692         st->config = st->chip_info->default_config;
693
694         /* TODO: Add pdata options for filtering and bit delay */
695
696         st->reg = devm_regulator_get(&client->dev, "vcc");
697         if (IS_ERR(st->reg))
698                 return PTR_ERR(st->reg);
699         ret = regulator_enable(st->reg);
700         if (ret)
701                 return ret;
702         st->vref = devm_regulator_get(&client->dev, "vref");
703         if (IS_ERR(st->vref)) {
704                 ret = PTR_ERR(st->vref);
705                 goto error_disable_reg;
706         }
707         ret = regulator_enable(st->vref);
708         if (ret)
709                 goto error_disable_reg;
710
711         st->client = client;
712
713         indio_dev->dev.parent = &client->dev;
714         indio_dev->name = id->name;
715         indio_dev->info = st->chip_info->info;
716
717         indio_dev->modes = INDIO_DIRECT_MODE;
718         indio_dev->channels = st->chip_info->channel;
719         indio_dev->num_channels = st->chip_info->num_channels;
720
721         ret = iio_triggered_buffer_setup(indio_dev, NULL,
722                 &ad799x_trigger_handler, NULL);
723         if (ret)
724                 goto error_disable_vref;
725
726         if (client->irq > 0) {
727                 ret = devm_request_threaded_irq(&client->dev,
728                                                 client->irq,
729                                                 NULL,
730                                                 ad799x_event_handler,
731                                                 IRQF_TRIGGER_FALLING |
732                                                 IRQF_ONESHOT,
733                                                 client->name,
734                                                 indio_dev);
735                 if (ret)
736                         goto error_cleanup_ring;
737         }
738         ret = iio_device_register(indio_dev);
739         if (ret)
740                 goto error_cleanup_ring;
741
742         return 0;
743
744 error_cleanup_ring:
745         iio_triggered_buffer_cleanup(indio_dev);
746 error_disable_vref:
747         regulator_disable(st->vref);
748 error_disable_reg:
749         regulator_disable(st->reg);
750
751         return ret;
752 }
753
754 static int ad799x_remove(struct i2c_client *client)
755 {
756         struct iio_dev *indio_dev = i2c_get_clientdata(client);
757         struct ad799x_state *st = iio_priv(indio_dev);
758
759         iio_device_unregister(indio_dev);
760
761         iio_triggered_buffer_cleanup(indio_dev);
762         regulator_disable(st->vref);
763         regulator_disable(st->reg);
764         kfree(st->rx_buf);
765
766         return 0;
767 }
768
769 static const struct i2c_device_id ad799x_id[] = {
770         { "ad7991", ad7991 },
771         { "ad7995", ad7995 },
772         { "ad7999", ad7999 },
773         { "ad7992", ad7992 },
774         { "ad7993", ad7993 },
775         { "ad7994", ad7994 },
776         { "ad7997", ad7997 },
777         { "ad7998", ad7998 },
778         {}
779 };
780
781 MODULE_DEVICE_TABLE(i2c, ad799x_id);
782
783 static struct i2c_driver ad799x_driver = {
784         .driver = {
785                 .name = "ad799x",
786         },
787         .probe = ad799x_probe,
788         .remove = ad799x_remove,
789         .id_table = ad799x_id,
790 };
791 module_i2c_driver(ad799x_driver);
792
793 MODULE_AUTHOR("Michael Hennerich <hennerich@blackfin.uclinux.org>");
794 MODULE_DESCRIPTION("Analog Devices AD799x ADC");
795 MODULE_LICENSE("GPL v2");