64ac31666739ab7e8a61e8d3813199d4c6155009
[firefly-linux-kernel-4.4.55.git] / drivers / staging / iio / adc / ad799x_core.c
1 /*
2  * iio/adc/ad799x.c
3  * Copyright (C) 2010-1011 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 "../iio.h"
37 #include "../sysfs.h"
38 #include "../events.h"
39 #include "../buffer.h"
40
41 #include "ad799x.h"
42
43 /*
44  * ad799x register access by I2C
45  */
46 static int ad799x_i2c_read16(struct ad799x_state *st, u8 reg, u16 *data)
47 {
48         struct i2c_client *client = st->client;
49         int ret = 0;
50
51         ret = i2c_smbus_read_word_data(client, reg);
52         if (ret < 0) {
53                 dev_err(&client->dev, "I2C read error\n");
54                 return ret;
55         }
56
57         *data = swab16((u16)ret);
58
59         return 0;
60 }
61
62 static int ad799x_i2c_read8(struct ad799x_state *st, u8 reg, u8 *data)
63 {
64         struct i2c_client *client = st->client;
65         int ret = 0;
66
67         ret = i2c_smbus_read_byte_data(client, reg);
68         if (ret < 0) {
69                 dev_err(&client->dev, "I2C read error\n");
70                 return ret;
71         }
72
73         *data = (u8)ret;
74
75         return 0;
76 }
77
78 static int ad799x_i2c_write16(struct ad799x_state *st, u8 reg, u16 data)
79 {
80         struct i2c_client *client = st->client;
81         int ret = 0;
82
83         ret = i2c_smbus_write_word_data(client, reg, swab16(data));
84         if (ret < 0)
85                 dev_err(&client->dev, "I2C write error\n");
86
87         return ret;
88 }
89
90 static int ad799x_i2c_write8(struct ad799x_state *st, u8 reg, u8 data)
91 {
92         struct i2c_client *client = st->client;
93         int ret = 0;
94
95         ret = i2c_smbus_write_byte_data(client, reg, data);
96         if (ret < 0)
97                 dev_err(&client->dev, "I2C write error\n");
98
99         return ret;
100 }
101
102 int ad7997_8_set_scan_mode(struct ad799x_state *st, unsigned mask)
103 {
104         return ad799x_i2c_write16(st, AD7998_CONF_REG,
105                 st->config | (mask << AD799X_CHANNEL_SHIFT));
106 }
107
108 static int ad799x_scan_direct(struct ad799x_state *st, unsigned ch)
109 {
110         u16 rxbuf;
111         u8 cmd;
112         int ret;
113
114         switch (st->id) {
115         case ad7991:
116         case ad7995:
117         case ad7999:
118                 cmd = st->config | ((1 << ch) << AD799X_CHANNEL_SHIFT);
119                 break;
120         case ad7992:
121         case ad7993:
122         case ad7994:
123                 cmd = (1 << ch) << AD799X_CHANNEL_SHIFT;
124                 break;
125         case ad7997:
126         case ad7998:
127                 cmd = (ch << AD799X_CHANNEL_SHIFT) | AD7997_8_READ_SINGLE;
128                 break;
129         default:
130                 return -EINVAL;
131         }
132
133         ret = ad799x_i2c_read16(st, cmd, &rxbuf);
134         if (ret < 0)
135                 return ret;
136
137         return rxbuf;
138 }
139
140 static int ad799x_read_raw(struct iio_dev *indio_dev,
141                            struct iio_chan_spec const *chan,
142                            int *val,
143                            int *val2,
144                            long m)
145 {
146         int ret;
147         struct ad799x_state *st = iio_priv(indio_dev);
148         unsigned int scale_uv;
149
150         switch (m) {
151         case 0:
152                 mutex_lock(&indio_dev->mlock);
153                 if (iio_buffer_enabled(indio_dev))
154                         ret = ad799x_single_channel_from_ring(indio_dev,
155                                                               chan->scan_index);
156                 else
157                         ret = ad799x_scan_direct(st, chan->scan_index);
158                 mutex_unlock(&indio_dev->mlock);
159
160                 if (ret < 0)
161                         return ret;
162                 *val = (ret >> chan->scan_type.shift) &
163                         RES_MASK(chan->scan_type.realbits);
164                 return IIO_VAL_INT;
165         case (1 << IIO_CHAN_INFO_SCALE_SHARED):
166                 scale_uv = (st->int_vref_mv * 1000) >> chan->scan_type.realbits;
167                 *val =  scale_uv / 1000;
168                 *val2 = (scale_uv % 1000) * 1000;
169                 return IIO_VAL_INT_PLUS_MICRO;
170         }
171         return -EINVAL;
172 }
173 static const unsigned int ad7998_frequencies[] = {
174         [AD7998_CYC_DIS]        = 0,
175         [AD7998_CYC_TCONF_32]   = 15625,
176         [AD7998_CYC_TCONF_64]   = 7812,
177         [AD7998_CYC_TCONF_128]  = 3906,
178         [AD7998_CYC_TCONF_512]  = 976,
179         [AD7998_CYC_TCONF_1024] = 488,
180         [AD7998_CYC_TCONF_2048] = 244,
181 };
182 static ssize_t ad799x_read_frequency(struct device *dev,
183                                         struct device_attribute *attr,
184                                         char *buf)
185 {
186         struct iio_dev *indio_dev = dev_get_drvdata(dev);
187         struct ad799x_state *st = iio_priv(indio_dev);
188
189         int ret;
190         u8 val;
191         ret = ad799x_i2c_read8(st, AD7998_CYCLE_TMR_REG, &val);
192         if (ret)
193                 return ret;
194
195         val &= AD7998_CYC_MASK;
196
197         return sprintf(buf, "%u\n", ad7998_frequencies[val]);
198 }
199
200 static ssize_t ad799x_write_frequency(struct device *dev,
201                                          struct device_attribute *attr,
202                                          const char *buf,
203                                          size_t len)
204 {
205         struct iio_dev *indio_dev = dev_get_drvdata(dev);
206         struct ad799x_state *st = iio_priv(indio_dev);
207
208         long val;
209         int ret, i;
210         u8 t;
211
212         ret = strict_strtol(buf, 10, &val);
213         if (ret)
214                 return ret;
215
216         mutex_lock(&indio_dev->mlock);
217         ret = ad799x_i2c_read8(st, AD7998_CYCLE_TMR_REG, &t);
218         if (ret)
219                 goto error_ret_mutex;
220         /* Wipe the bits clean */
221         t &= ~AD7998_CYC_MASK;
222
223         for (i = 0; i < ARRAY_SIZE(ad7998_frequencies); i++)
224                 if (val == ad7998_frequencies[i])
225                         break;
226         if (i == ARRAY_SIZE(ad7998_frequencies)) {
227                 ret = -EINVAL;
228                 goto error_ret_mutex;
229         }
230         t |= i;
231         ret = ad799x_i2c_write8(st, AD7998_CYCLE_TMR_REG, t);
232
233 error_ret_mutex:
234         mutex_unlock(&indio_dev->mlock);
235
236         return ret ? ret : len;
237 }
238
239 static int ad799x_read_event_config(struct iio_dev *indio_dev,
240                                     u64 event_code)
241 {
242         return 1;
243 }
244
245 static const u8 ad799x_threshold_addresses[][2] = {
246         { AD7998_DATALOW_CH1_REG, AD7998_DATAHIGH_CH1_REG },
247         { AD7998_DATALOW_CH2_REG, AD7998_DATAHIGH_CH2_REG },
248         { AD7998_DATALOW_CH3_REG, AD7998_DATAHIGH_CH3_REG },
249         { AD7998_DATALOW_CH4_REG, AD7998_DATAHIGH_CH4_REG },
250 };
251
252 static int ad799x_write_event_value(struct iio_dev *indio_dev,
253                                     u64 event_code,
254                                     int val)
255 {
256         int ret;
257         struct ad799x_state *st = iio_priv(indio_dev);
258         int direction = !!(IIO_EVENT_CODE_EXTRACT_DIR(event_code) ==
259                            IIO_EV_DIR_FALLING);
260         int number = IIO_EVENT_CODE_EXTRACT_NUM(event_code);
261
262         mutex_lock(&indio_dev->mlock);
263         ret = ad799x_i2c_write16(st,
264                                  ad799x_threshold_addresses[number][direction],
265                                  val);
266         mutex_unlock(&indio_dev->mlock);
267
268         return ret;
269 }
270
271 static int ad799x_read_event_value(struct iio_dev *indio_dev,
272                                     u64 event_code,
273                                     int *val)
274 {
275         int ret;
276         struct ad799x_state *st = iio_priv(indio_dev);
277         int direction = !!(IIO_EVENT_CODE_EXTRACT_DIR(event_code) ==
278                            IIO_EV_DIR_FALLING);
279         int number = IIO_EVENT_CODE_EXTRACT_NUM(event_code);
280         u16 valin;
281
282         mutex_lock(&indio_dev->mlock);
283         ret = ad799x_i2c_read16(st,
284                                 ad799x_threshold_addresses[number][direction],
285                                 &valin);
286         mutex_unlock(&indio_dev->mlock);
287         if (ret < 0)
288                 return ret;
289         *val = valin;
290
291         return 0;
292 }
293
294 static ssize_t ad799x_read_channel_config(struct device *dev,
295                                         struct device_attribute *attr,
296                                         char *buf)
297 {
298         struct iio_dev *indio_dev = dev_get_drvdata(dev);
299         struct ad799x_state *st = iio_priv(indio_dev);
300         struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
301
302         int ret;
303         u16 val;
304         ret = ad799x_i2c_read16(st, this_attr->address, &val);
305         if (ret)
306                 return ret;
307
308         return sprintf(buf, "%d\n", val);
309 }
310
311 static ssize_t ad799x_write_channel_config(struct device *dev,
312                                          struct device_attribute *attr,
313                                          const char *buf,
314                                          size_t len)
315 {
316         struct iio_dev *indio_dev = dev_get_drvdata(dev);
317         struct ad799x_state *st = iio_priv(indio_dev);
318         struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
319
320         long val;
321         int ret;
322
323         ret = strict_strtol(buf, 10, &val);
324         if (ret)
325                 return ret;
326
327         mutex_lock(&indio_dev->mlock);
328         ret = ad799x_i2c_write16(st, this_attr->address, val);
329         mutex_unlock(&indio_dev->mlock);
330
331         return ret ? ret : len;
332 }
333
334 static irqreturn_t ad799x_event_handler(int irq, void *private)
335 {
336         struct iio_dev *indio_dev = private;
337         struct ad799x_state *st = iio_priv(private);
338         u8 status;
339         int i, ret;
340
341         ret = ad799x_i2c_read8(st, AD7998_ALERT_STAT_REG, &status);
342         if (ret)
343                 return ret;
344
345         if (!status)
346                 return -EIO;
347
348         ad799x_i2c_write8(st, AD7998_ALERT_STAT_REG, AD7998_ALERT_STAT_CLEAR);
349
350         for (i = 0; i < 8; i++) {
351                 if (status & (1 << i))
352                         iio_push_event(indio_dev,
353                                        i & 0x1 ?
354                                        IIO_UNMOD_EVENT_CODE(IIO_VOLTAGE,
355                                                             (i >> 1),
356                                                             IIO_EV_TYPE_THRESH,
357                                                             IIO_EV_DIR_RISING) :
358                                        IIO_UNMOD_EVENT_CODE(IIO_VOLTAGE,
359                                                             (i >> 1),
360                                                             IIO_EV_TYPE_THRESH,
361                                                             IIO_EV_DIR_FALLING),
362                                        iio_get_time_ns());
363         }
364
365         return IRQ_HANDLED;
366 }
367
368 static IIO_DEVICE_ATTR(in_voltage0_thresh_both_hyst_raw,
369                        S_IRUGO | S_IWUSR,
370                        ad799x_read_channel_config,
371                        ad799x_write_channel_config,
372                        AD7998_HYST_CH1_REG);
373
374 static IIO_DEVICE_ATTR(in_voltage1_thresh_both_hyst_raw,
375                        S_IRUGO | S_IWUSR,
376                        ad799x_read_channel_config,
377                        ad799x_write_channel_config,
378                        AD7998_HYST_CH2_REG);
379
380 static IIO_DEVICE_ATTR(in_voltage2_thresh_both_hyst_raw,
381                        S_IRUGO | S_IWUSR,
382                        ad799x_read_channel_config,
383                        ad799x_write_channel_config,
384                        AD7998_HYST_CH3_REG);
385
386 static IIO_DEVICE_ATTR(in_voltage3_thresh_both_hyst_raw,
387                        S_IRUGO | S_IWUSR,
388                        ad799x_read_channel_config,
389                        ad799x_write_channel_config,
390                        AD7998_HYST_CH4_REG);
391
392 static IIO_DEV_ATTR_SAMP_FREQ(S_IWUSR | S_IRUGO,
393                               ad799x_read_frequency,
394                               ad799x_write_frequency);
395 static IIO_CONST_ATTR_SAMP_FREQ_AVAIL("15625 7812 3906 1953 976 488 244 0");
396
397 static struct attribute *ad7993_4_7_8_event_attributes[] = {
398         &iio_dev_attr_in_voltage0_thresh_both_hyst_raw.dev_attr.attr,
399         &iio_dev_attr_in_voltage1_thresh_both_hyst_raw.dev_attr.attr,
400         &iio_dev_attr_in_voltage2_thresh_both_hyst_raw.dev_attr.attr,
401         &iio_dev_attr_in_voltage3_thresh_both_hyst_raw.dev_attr.attr,
402         &iio_dev_attr_sampling_frequency.dev_attr.attr,
403         &iio_const_attr_sampling_frequency_available.dev_attr.attr,
404         NULL,
405 };
406
407 static struct attribute_group ad7993_4_7_8_event_attrs_group = {
408         .attrs = ad7993_4_7_8_event_attributes,
409         .name = "events",
410 };
411
412 static struct attribute *ad7992_event_attributes[] = {
413         &iio_dev_attr_in_voltage0_thresh_both_hyst_raw.dev_attr.attr,
414         &iio_dev_attr_in_voltage1_thresh_both_hyst_raw.dev_attr.attr,
415         &iio_dev_attr_sampling_frequency.dev_attr.attr,
416         &iio_const_attr_sampling_frequency_available.dev_attr.attr,
417         NULL,
418 };
419
420 static struct attribute_group ad7992_event_attrs_group = {
421         .attrs = ad7992_event_attributes,
422         .name = "events",
423 };
424
425 static const struct iio_info ad7991_info = {
426         .read_raw = &ad799x_read_raw,
427         .driver_module = THIS_MODULE,
428 };
429
430 static const struct iio_info ad7992_info = {
431         .read_raw = &ad799x_read_raw,
432         .event_attrs = &ad7992_event_attrs_group,
433         .read_event_config = &ad799x_read_event_config,
434         .read_event_value = &ad799x_read_event_value,
435         .write_event_value = &ad799x_write_event_value,
436         .driver_module = THIS_MODULE,
437 };
438
439 static const struct iio_info ad7993_4_7_8_info = {
440         .read_raw = &ad799x_read_raw,
441         .event_attrs = &ad7993_4_7_8_event_attrs_group,
442         .read_event_config = &ad799x_read_event_config,
443         .read_event_value = &ad799x_read_event_value,
444         .write_event_value = &ad799x_write_event_value,
445         .driver_module = THIS_MODULE,
446 };
447
448 #define AD799X_EV_MASK (IIO_EV_BIT(IIO_EV_TYPE_THRESH, IIO_EV_DIR_RISING) | \
449                         IIO_EV_BIT(IIO_EV_TYPE_THRESH, IIO_EV_DIR_FALLING))
450
451 static const struct ad799x_chip_info ad799x_chip_info_tbl[] = {
452         [ad7991] = {
453                 .channel = {
454                         [0] = {
455                                 .type = IIO_VOLTAGE,
456                                 .indexed = 1,
457                                 .channel = 0,
458                                 .scan_index = 0,
459                                 .scan_type = IIO_ST('u', 12, 16, 0),
460                         },
461                         [1] = {
462                                 .type = IIO_VOLTAGE,
463                                 .indexed = 1,
464                                 .channel = 1,
465                                 .scan_index = 1,
466                                 .scan_type = IIO_ST('u', 12, 16, 0),
467                         },
468                         [2] = {
469                                 .type = IIO_VOLTAGE,
470                                 .indexed = 1,
471                                 .channel = 2,
472                                 .scan_index = 2,
473                                 .scan_type = IIO_ST('u', 12, 16, 0),
474                         },
475                         [3] = {
476                                 .type = IIO_VOLTAGE,
477                                 .indexed = 1,
478                                 .channel = 3,
479                                 .scan_index = 3,
480                                 .scan_type = IIO_ST('u', 12, 16, 0),
481                         },
482                         [4] = IIO_CHAN_SOFT_TIMESTAMP(4),
483                 },
484                 .num_channels = 5,
485                 .int_vref_mv = 4096,
486                 .info = &ad7991_info,
487         },
488         [ad7995] = {
489                 .channel = {
490                         [0] = {
491                                 .type = IIO_VOLTAGE,
492                                 .indexed = 1,
493                                 .channel = 0,
494                                 .scan_index = 0,
495                                 .scan_type = IIO_ST('u', 10, 16, 2),
496                         },
497                         [1] = {
498                                 .type = IIO_VOLTAGE,
499                                 .indexed = 1,
500                                 .channel = 1,
501                                 .scan_index = 1,
502                                 .scan_type = IIO_ST('u', 10, 16, 2),
503                         },
504                         [2] = {
505                                 .type = IIO_VOLTAGE,
506                                 .indexed = 1,
507                                 .channel = 2,
508                                 .scan_index = 2,
509                                 .scan_type = IIO_ST('u', 10, 16, 2),
510                         },
511                         [3] = {
512                                 .type = IIO_VOLTAGE,
513                                 .indexed = 1,
514                                 .channel = 3,
515                                 .scan_index = 3,
516                                 .scan_type = IIO_ST('u', 10, 16, 2),
517                         },
518                         [4] = IIO_CHAN_SOFT_TIMESTAMP(4),
519                 },
520                 .num_channels = 5,
521                 .int_vref_mv = 1024,
522                 .info = &ad7991_info,
523         },
524         [ad7999] = {
525                 .channel = {
526                         [0] = {
527                                 .type = IIO_VOLTAGE,
528                                 .indexed = 1,
529                                 .channel = 0,
530                                 .scan_index = 0,
531                                 .scan_type = IIO_ST('u', 8, 16, 4),
532                         },
533                         [1] = {
534                                 .type = IIO_VOLTAGE,
535                                 .indexed = 1,
536                                 .channel = 1,
537                                 .scan_index = 1,
538                                 .scan_type = IIO_ST('u', 8, 16, 4),
539                         },
540                         [2] = {
541                                 .type = IIO_VOLTAGE,
542                                 .indexed = 1,
543                                 .channel = 2,
544                                 .scan_index = 2,
545                                 .scan_type = IIO_ST('u', 8, 16, 4),
546                         },
547                         [3] = {
548                                 .type = IIO_VOLTAGE,
549                                 .indexed = 1,
550                                 .channel = 3,
551                                 .scan_index = 3,
552                                 .scan_type = IIO_ST('u', 8, 16, 4),
553                         },
554                         [4] = IIO_CHAN_SOFT_TIMESTAMP(4),
555                 },
556                 .num_channels = 5,
557                 .int_vref_mv = 1024,
558                 .info = &ad7991_info,
559         },
560         [ad7992] = {
561                 .channel = {
562                         [0] = {
563                                 .type = IIO_VOLTAGE,
564                                 .indexed = 1,
565                                 .channel = 0,
566                                 .scan_index = 0,
567                                 .scan_type = IIO_ST('u', 12, 16, 0),
568                                 .event_mask = AD799X_EV_MASK,
569                         },
570                         [1] = {
571                                 .type = IIO_VOLTAGE,
572                                 .indexed = 1,
573                                 .channel = 1,
574                                 .scan_index = 1,
575                                 .scan_type = IIO_ST('u', 12, 16, 0),
576                                 .event_mask = AD799X_EV_MASK,
577                         },
578                         [2] = IIO_CHAN_SOFT_TIMESTAMP(2),
579                 },
580                 .num_channels = 3,
581                 .int_vref_mv = 4096,
582                 .default_config = AD7998_ALERT_EN,
583                 .info = &ad7992_info,
584         },
585         [ad7993] = {
586                 .channel = {
587                         [0] = {
588                                 .type = IIO_VOLTAGE,
589                                 .indexed = 1,
590                                 .channel = 0,
591                                 .scan_index = 0,
592                                 .scan_type = IIO_ST('u', 10, 16, 2),
593                                 .event_mask = AD799X_EV_MASK,
594                         },
595                         [1] = {
596                                 .type = IIO_VOLTAGE,
597                                 .indexed = 1,
598                                 .channel = 1,
599                                 .scan_index = 1,
600                                 .scan_type = IIO_ST('u', 10, 16, 2),
601                                 .event_mask = AD799X_EV_MASK,
602                         },
603                         [2] = {
604                                 .type = IIO_VOLTAGE,
605                                 .indexed = 1,
606                                 .channel = 2,
607                                 .scan_index = 2,
608                                 .scan_type = IIO_ST('u', 10, 16, 2),
609                                 .event_mask = AD799X_EV_MASK,
610                         },
611                         [3] = {
612                                 .type = IIO_VOLTAGE,
613                                 .indexed = 1,
614                                 .channel = 3,
615                                 .scan_index = 3,
616                                 .scan_type = IIO_ST('u', 10, 16, 2),
617                                 .event_mask = AD799X_EV_MASK,
618                         },
619                         [4] = IIO_CHAN_SOFT_TIMESTAMP(4),
620                 },
621                 .num_channels = 5,
622                 .int_vref_mv = 1024,
623                 .default_config = AD7998_ALERT_EN,
624                 .info = &ad7993_4_7_8_info,
625         },
626         [ad7994] = {
627                 .channel = {
628                         [0] = {
629                                 .type = IIO_VOLTAGE,
630                                 .indexed = 1,
631                                 .channel = 0,
632                                 .scan_index = 0,
633                                 .scan_type = IIO_ST('u', 12, 16, 0),
634                                 .event_mask = AD799X_EV_MASK,
635                         },
636                         [1] = {
637                                 .type = IIO_VOLTAGE,
638                                 .indexed = 1,
639                                 .channel = 1,
640                                 .scan_index = 1,
641                                 .scan_type = IIO_ST('u', 12, 16, 0),
642                                 .event_mask = AD799X_EV_MASK,
643                         },
644                         [2] = {
645                                 .type = IIO_VOLTAGE,
646                                 .indexed = 1,
647                                 .channel = 2,
648                                 .scan_index = 2,
649                                 .scan_type = IIO_ST('u', 12, 16, 0),
650                                 .event_mask = AD799X_EV_MASK,
651                         },
652                         [3] = {
653                                 .type = IIO_VOLTAGE,
654                                 .indexed = 1,
655                                 .channel = 3,
656                                 .scan_index = 3,
657                                 .scan_type = IIO_ST('u', 12, 16, 0),
658                                 .event_mask = AD799X_EV_MASK,
659                         },
660                         [4] = IIO_CHAN_SOFT_TIMESTAMP(4),
661                 },
662                 .num_channels = 5,
663                 .int_vref_mv = 4096,
664                 .default_config = AD7998_ALERT_EN,
665                 .info = &ad7993_4_7_8_info,
666         },
667         [ad7997] = {
668                 .channel = {
669                         [0] = {
670                                 .type = IIO_VOLTAGE,
671                                 .indexed = 1,
672                                 .channel = 0,
673                                 .scan_index = 0,
674                                 .scan_type = IIO_ST('u', 10, 16, 2),
675                                 .event_mask = AD799X_EV_MASK,
676                         },
677                         [1] = {
678                                 .type = IIO_VOLTAGE,
679                                 .indexed = 1,
680                                 .channel = 1,
681                                 .scan_index = 1,
682                                 .scan_type = IIO_ST('u', 10, 16, 2),
683                                 .event_mask = AD799X_EV_MASK,
684                         },
685                         [2] = {
686                                 .type = IIO_VOLTAGE,
687                                 .indexed = 1,
688                                 .channel = 2,
689                                 .scan_index = 2,
690                                 .scan_type = IIO_ST('u', 10, 16, 2),
691                                 .event_mask = AD799X_EV_MASK,
692                         },
693                         [3] = {
694                                 .type = IIO_VOLTAGE,
695                                 .indexed = 1,
696                                 .channel = 3,
697                                 .scan_index = 3,
698                                 .scan_type = IIO_ST('u', 10, 16, 2),
699                                 .event_mask = AD799X_EV_MASK,
700                         },
701                         [4] = {
702                                 .type = IIO_VOLTAGE,
703                                 .indexed = 1,
704                                 .channel = 4,
705                                 .scan_index = 4,
706                                 .scan_type = IIO_ST('u', 10, 16, 2),
707                         },
708                         [5] = {
709                                 .type = IIO_VOLTAGE,
710                                 .indexed = 1,
711                                 .channel = 5,
712                                 .scan_index = 5,
713                                 .scan_type = IIO_ST('u', 10, 16, 2),
714                         },
715                         [6] = {
716                                 .type = IIO_VOLTAGE,
717                                 .indexed = 1,
718                                 .channel = 6,
719                                 .scan_index = 6,
720                                 .scan_type = IIO_ST('u', 10, 16, 2),
721                         },
722                         [7] = {
723                                 .type = IIO_VOLTAGE,
724                                 .indexed = 1,
725                                 .channel = 7,
726                                 .scan_index = 7,
727                                 .scan_type = IIO_ST('u', 10, 16, 2),
728                         },
729                         [8] = IIO_CHAN_SOFT_TIMESTAMP(8),
730                 },
731                 .num_channels = 9,
732                 .int_vref_mv = 1024,
733                 .default_config = AD7998_ALERT_EN,
734                 .info = &ad7993_4_7_8_info,
735         },
736         [ad7998] = {
737                 .channel = {
738                         [0] = {
739                                 .type = IIO_VOLTAGE,
740                                 .indexed = 1,
741                                 .channel = 0,
742                                 .scan_index = 0,
743                                 .scan_type = IIO_ST('u', 12, 16, 0),
744                                 .event_mask = AD799X_EV_MASK,
745                         },
746                         [1] = {
747                                 .type = IIO_VOLTAGE,
748                                 .indexed = 1,
749                                 .channel = 1,
750                                 .scan_index = 1,
751                                 .scan_type = IIO_ST('u', 12, 16, 0),
752                                 .event_mask = AD799X_EV_MASK,
753                         },
754                         [2] = {
755                                 .type = IIO_VOLTAGE,
756                                 .indexed = 1,
757                                 .channel = 2,
758                                 .scan_index = 2,
759                                 .scan_type = IIO_ST('u', 12, 16, 0),
760                                 .event_mask = AD799X_EV_MASK,
761                         },
762                         [3] = {
763                                 .type = IIO_VOLTAGE,
764                                 .indexed = 1,
765                                 .channel = 3,
766                                 .scan_index = 3,
767                                 .scan_type = IIO_ST('u', 12, 16, 0),
768                                 .event_mask = AD799X_EV_MASK,
769                         },
770                         [4] = {
771                                 .type = IIO_VOLTAGE,
772                                 .indexed = 1,
773                                 .channel = 4,
774                                 .scan_index = 4,
775                                 .scan_type = IIO_ST('u', 12, 16, 0),
776                         },
777                         [5] = {
778                                 .type = IIO_VOLTAGE,
779                                 .indexed = 1,
780                                 .channel = 5,
781                                 .scan_index = 5,
782                                 .scan_type = IIO_ST('u', 12, 16, 0),
783                         },
784                         [6] = {
785                                 .type = IIO_VOLTAGE,
786                                 .indexed = 1,
787                                 .channel = 6,
788                                 .scan_index = 6,
789                                 .scan_type = IIO_ST('u', 12, 16, 0),
790                         },
791                         [7] = {
792                                 .type = IIO_VOLTAGE,
793                                 .indexed = 1,
794                                 .channel = 7,
795                                 .scan_index = 7,
796                                 .scan_type = IIO_ST('u', 12, 16, 0),
797                         },
798                         [8] = IIO_CHAN_SOFT_TIMESTAMP(8),
799                 },
800                 .num_channels = 9,
801                 .int_vref_mv = 4096,
802                 .default_config = AD7998_ALERT_EN,
803                 .info = &ad7993_4_7_8_info,
804         },
805 };
806
807 static int __devinit ad799x_probe(struct i2c_client *client,
808                                    const struct i2c_device_id *id)
809 {
810         int ret;
811         struct ad799x_platform_data *pdata = client->dev.platform_data;
812         struct ad799x_state *st;
813         struct iio_dev *indio_dev = iio_allocate_device(sizeof(*st));
814
815         if (indio_dev == NULL)
816                 return -ENOMEM;
817
818         st = iio_priv(indio_dev);
819         /* this is only used for device removal purposes */
820         i2c_set_clientdata(client, indio_dev);
821
822         st->id = id->driver_data;
823         st->chip_info = &ad799x_chip_info_tbl[st->id];
824         st->config = st->chip_info->default_config;
825
826         /* TODO: Add pdata options for filtering and bit delay */
827
828         if (pdata)
829                 st->int_vref_mv = pdata->vref_mv;
830         else
831                 st->int_vref_mv = st->chip_info->int_vref_mv;
832
833         st->reg = regulator_get(&client->dev, "vcc");
834         if (!IS_ERR(st->reg)) {
835                 ret = regulator_enable(st->reg);
836                 if (ret)
837                         goto error_put_reg;
838         }
839         st->client = client;
840
841         indio_dev->dev.parent = &client->dev;
842         indio_dev->name = id->name;
843         indio_dev->info = st->chip_info->info;
844
845         indio_dev->modes = INDIO_DIRECT_MODE;
846         indio_dev->channels = st->chip_info->channel;
847         indio_dev->num_channels = st->chip_info->num_channels;
848
849         ret = ad799x_register_ring_funcs_and_init(indio_dev);
850         if (ret)
851                 goto error_disable_reg;
852
853         ret = iio_buffer_register(indio_dev,
854                                   indio_dev->channels,
855                                   indio_dev->num_channels);
856         if (ret)
857                 goto error_cleanup_ring;
858
859         if (client->irq > 0) {
860                 ret = request_threaded_irq(client->irq,
861                                            NULL,
862                                            ad799x_event_handler,
863                                            IRQF_TRIGGER_FALLING |
864                                            IRQF_ONESHOT,
865                                            client->name,
866                                            indio_dev);
867                 if (ret)
868                         goto error_cleanup_ring;
869         }
870         ret = iio_device_register(indio_dev);
871         if (ret)
872                 goto error_free_irq;
873
874         return 0;
875
876 error_free_irq:
877         free_irq(client->irq, indio_dev);
878 error_cleanup_ring:
879         ad799x_ring_cleanup(indio_dev);
880 error_disable_reg:
881         if (!IS_ERR(st->reg))
882                 regulator_disable(st->reg);
883 error_put_reg:
884         if (!IS_ERR(st->reg))
885                 regulator_put(st->reg);
886         iio_free_device(indio_dev);
887
888         return ret;
889 }
890
891 static __devexit int ad799x_remove(struct i2c_client *client)
892 {
893         struct iio_dev *indio_dev = i2c_get_clientdata(client);
894         struct ad799x_state *st = iio_priv(indio_dev);
895
896         iio_device_unregister(indio_dev);
897         if (client->irq > 0)
898                 free_irq(client->irq, indio_dev);
899
900         iio_buffer_unregister(indio_dev);
901         ad799x_ring_cleanup(indio_dev);
902         if (!IS_ERR(st->reg)) {
903                 regulator_disable(st->reg);
904                 regulator_put(st->reg);
905         }
906         iio_free_device(indio_dev);
907
908         return 0;
909 }
910
911 static const struct i2c_device_id ad799x_id[] = {
912         { "ad7991", ad7991 },
913         { "ad7995", ad7995 },
914         { "ad7999", ad7999 },
915         { "ad7992", ad7992 },
916         { "ad7993", ad7993 },
917         { "ad7994", ad7994 },
918         { "ad7997", ad7997 },
919         { "ad7998", ad7998 },
920         {}
921 };
922
923 MODULE_DEVICE_TABLE(i2c, ad799x_id);
924
925 static struct i2c_driver ad799x_driver = {
926         .driver = {
927                 .name = "ad799x",
928         },
929         .probe = ad799x_probe,
930         .remove = __devexit_p(ad799x_remove),
931         .id_table = ad799x_id,
932 };
933
934 static __init int ad799x_init(void)
935 {
936         return i2c_add_driver(&ad799x_driver);
937 }
938
939 static __exit void ad799x_exit(void)
940 {
941         i2c_del_driver(&ad799x_driver);
942 }
943
944 MODULE_AUTHOR("Michael Hennerich <hennerich@blackfin.uclinux.org>");
945 MODULE_DESCRIPTION("Analog Devices AD799x ADC");
946 MODULE_LICENSE("GPL v2");
947 MODULE_ALIAS("i2c:ad799x");
948
949 module_init(ad799x_init);
950 module_exit(ad799x_exit);