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