staging:iio: remove broken support for multiple event interfaces.
[pandora-kernel.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 "../ring_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_ring_enabled(dev_info))
153                         ret = ad799x_single_channel_from_ring(st,
154                                 1 << chan->address);
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 >> st->chip_info->channel[0].scan_type.shift) &
162                         RES_MASK(st->chip_info->channel[0].scan_type.realbits);
163                 return IIO_VAL_INT;
164         case (1 << IIO_CHAN_INFO_SCALE_SHARED):
165                 scale_uv = (st->int_vref_mv * 1000)
166                         >> st->chip_info->channel[0].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
174 static ssize_t ad799x_read_frequency(struct device *dev,
175                                         struct device_attribute *attr,
176                                         char *buf)
177 {
178         struct iio_dev *dev_info = dev_get_drvdata(dev);
179         struct ad799x_state *st = iio_priv(dev_info);
180
181         int ret, len = 0;
182         u8 val;
183         ret = ad799x_i2c_read8(st, AD7998_CYCLE_TMR_REG, &val);
184         if (ret)
185                 return ret;
186
187         val &= AD7998_CYC_MASK;
188
189         switch (val) {
190         case AD7998_CYC_DIS:
191                 len = sprintf(buf, "0\n");
192                 break;
193         case AD7998_CYC_TCONF_32:
194                 len = sprintf(buf, "15625\n");
195                 break;
196         case AD7998_CYC_TCONF_64:
197                 len = sprintf(buf, "7812\n");
198                 break;
199         case AD7998_CYC_TCONF_128:
200                 len = sprintf(buf, "3906\n");
201                 break;
202         case AD7998_CYC_TCONF_256:
203                 len = sprintf(buf, "1953\n");
204                 break;
205         case AD7998_CYC_TCONF_512:
206                 len = sprintf(buf, "976\n");
207                 break;
208         case AD7998_CYC_TCONF_1024:
209                 len = sprintf(buf, "488\n");
210                 break;
211         case AD7998_CYC_TCONF_2048:
212                 len = sprintf(buf, "244\n");
213                 break;
214         }
215         return len;
216 }
217
218 static ssize_t ad799x_write_frequency(struct device *dev,
219                                          struct device_attribute *attr,
220                                          const char *buf,
221                                          size_t len)
222 {
223         struct iio_dev *dev_info = dev_get_drvdata(dev);
224         struct ad799x_state *st = iio_priv(dev_info);
225
226         long val;
227         int ret;
228         u8 t;
229
230         ret = strict_strtol(buf, 10, &val);
231         if (ret)
232                 return ret;
233
234         mutex_lock(&dev_info->mlock);
235         ret = ad799x_i2c_read8(st, AD7998_CYCLE_TMR_REG, &t);
236         if (ret)
237                 goto error_ret_mutex;
238         /* Wipe the bits clean */
239         t &= ~AD7998_CYC_MASK;
240
241         switch (val) {
242         case 15625:
243                 t |= AD7998_CYC_TCONF_32;
244                 break;
245         case 7812:
246                 t |= AD7998_CYC_TCONF_64;
247                 break;
248         case 3906:
249                 t |= AD7998_CYC_TCONF_128;
250                 break;
251         case 1953:
252                 t |= AD7998_CYC_TCONF_256;
253                 break;
254         case 976:
255                 t |= AD7998_CYC_TCONF_512;
256                 break;
257         case 488:
258                 t |= AD7998_CYC_TCONF_1024;
259                 break;
260         case 244:
261                 t |= AD7998_CYC_TCONF_2048;
262                 break;
263         case  0:
264                 t |= AD7998_CYC_DIS;
265                 break;
266         default:
267                 ret = -EINVAL;
268                 goto error_ret_mutex;
269         }
270
271         ret = ad799x_i2c_write8(st, AD7998_CYCLE_TMR_REG, t);
272
273 error_ret_mutex:
274         mutex_unlock(&dev_info->mlock);
275
276         return ret ? ret : len;
277 }
278
279 static ssize_t ad799x_read_channel_config(struct device *dev,
280                                         struct device_attribute *attr,
281                                         char *buf)
282 {
283         struct iio_dev *dev_info = dev_get_drvdata(dev);
284         struct ad799x_state *st = iio_priv(dev_info);
285         struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
286
287         int ret;
288         u16 val;
289         ret = ad799x_i2c_read16(st, this_attr->address, &val);
290         if (ret)
291                 return ret;
292
293         return sprintf(buf, "%d\n", val);
294 }
295
296 static ssize_t ad799x_write_channel_config(struct device *dev,
297                                          struct device_attribute *attr,
298                                          const char *buf,
299                                          size_t len)
300 {
301         struct iio_dev *dev_info = dev_get_drvdata(dev);
302         struct ad799x_state *st = iio_priv(dev_info);
303         struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
304
305         long val;
306         int ret;
307
308         ret = strict_strtol(buf, 10, &val);
309         if (ret)
310                 return ret;
311
312         mutex_lock(&dev_info->mlock);
313         ret = ad799x_i2c_write16(st, this_attr->address, val);
314         mutex_unlock(&dev_info->mlock);
315
316         return ret ? ret : len;
317 }
318
319 static irqreturn_t ad799x_event_handler(int irq, void *private)
320 {
321         struct iio_dev *indio_dev = private;
322         struct ad799x_state *st = iio_priv(private);
323         u8 status;
324         int i, ret;
325
326         ret = ad799x_i2c_read8(st, AD7998_ALERT_STAT_REG, &status);
327         if (ret)
328                 return ret;
329
330         if (!status)
331                 return -EIO;
332
333         ad799x_i2c_write8(st, AD7998_ALERT_STAT_REG, AD7998_ALERT_STAT_CLEAR);
334
335         for (i = 0; i < 8; i++) {
336                 if (status & (1 << i))
337                         iio_push_event(indio_dev,
338                                        i & 0x1 ?
339                                        IIO_UNMOD_EVENT_CODE(IIO_IN,
340                                                             (i >> 1),
341                                                             IIO_EV_TYPE_THRESH,
342                                                             IIO_EV_DIR_RISING) :
343                                        IIO_UNMOD_EVENT_CODE(IIO_IN,
344                                                             (i >> 1),
345                                                             IIO_EV_TYPE_THRESH,
346                                                             IIO_EV_DIR_FALLING),
347                                        iio_get_time_ns());
348         }
349
350         return IRQ_HANDLED;
351 }
352
353 static IIO_DEVICE_ATTR(in0_thresh_low_value,
354                        S_IRUGO | S_IWUSR,
355                        ad799x_read_channel_config,
356                        ad799x_write_channel_config,
357                        AD7998_DATALOW_CH1_REG);
358
359 static IIO_DEVICE_ATTR(in0_thresh_high_value,
360                        S_IRUGO | S_IWUSR,
361                        ad799x_read_channel_config,
362                        ad799x_write_channel_config,
363                        AD7998_DATAHIGH_CH1_REG);
364
365 static IIO_DEVICE_ATTR(in0_thresh_both_hyst_raw,
366                        S_IRUGO | S_IWUSR,
367                        ad799x_read_channel_config,
368                        ad799x_write_channel_config,
369                        AD7998_HYST_CH1_REG);
370
371 static IIO_DEVICE_ATTR(in1_thresh_low_value,
372                        S_IRUGO | S_IWUSR,
373                        ad799x_read_channel_config,
374                        ad799x_write_channel_config,
375                        AD7998_DATALOW_CH2_REG);
376
377 static IIO_DEVICE_ATTR(in1_thresh_high_value,
378                        S_IRUGO | S_IWUSR,
379                        ad799x_read_channel_config,
380                        ad799x_write_channel_config,
381                        AD7998_DATAHIGH_CH2_REG);
382
383 static IIO_DEVICE_ATTR(in1_thresh_both_hyst_raw,
384                        S_IRUGO | S_IWUSR,
385                        ad799x_read_channel_config,
386                        ad799x_write_channel_config,
387                        AD7998_HYST_CH2_REG);
388
389 static IIO_DEVICE_ATTR(in2_thresh_low_value,
390                        S_IRUGO | S_IWUSR,
391                        ad799x_read_channel_config,
392                        ad799x_write_channel_config,
393                        AD7998_DATALOW_CH3_REG);
394
395 static IIO_DEVICE_ATTR(in2_thresh_high_value,
396                        S_IRUGO | S_IWUSR,
397                        ad799x_read_channel_config,
398                        ad799x_write_channel_config,
399                        AD7998_DATAHIGH_CH3_REG);
400
401 static IIO_DEVICE_ATTR(in2_thresh_both_hyst_raw,
402                        S_IRUGO | S_IWUSR,
403                        ad799x_read_channel_config,
404                        ad799x_write_channel_config,
405                        AD7998_HYST_CH3_REG);
406
407 static IIO_DEVICE_ATTR(in3_thresh_low_value,
408                        S_IRUGO | S_IWUSR,
409                        ad799x_read_channel_config,
410                        ad799x_write_channel_config,
411                        AD7998_DATALOW_CH4_REG);
412
413 static IIO_DEVICE_ATTR(in3_thresh_high_value,
414                        S_IRUGO | S_IWUSR,
415                        ad799x_read_channel_config,
416                        ad799x_write_channel_config,
417                        AD7998_DATAHIGH_CH4_REG);
418
419 static IIO_DEVICE_ATTR(in3_thresh_both_hyst_raw,
420                        S_IRUGO | S_IWUSR,
421                        ad799x_read_channel_config,
422                        ad799x_write_channel_config,
423                        AD7998_HYST_CH4_REG);
424
425 static IIO_DEV_ATTR_SAMP_FREQ(S_IWUSR | S_IRUGO,
426                               ad799x_read_frequency,
427                               ad799x_write_frequency);
428 static IIO_CONST_ATTR_SAMP_FREQ_AVAIL("15625 7812 3906 1953 976 488 244 0");
429
430 static struct attribute *ad7993_4_7_8_event_attributes[] = {
431         &iio_dev_attr_in0_thresh_low_value.dev_attr.attr,
432         &iio_dev_attr_in0_thresh_high_value.dev_attr.attr,
433         &iio_dev_attr_in0_thresh_both_hyst_raw.dev_attr.attr,
434         &iio_dev_attr_in1_thresh_low_value.dev_attr.attr,
435         &iio_dev_attr_in1_thresh_high_value.dev_attr.attr,
436         &iio_dev_attr_in1_thresh_both_hyst_raw.dev_attr.attr,
437         &iio_dev_attr_in2_thresh_low_value.dev_attr.attr,
438         &iio_dev_attr_in2_thresh_high_value.dev_attr.attr,
439         &iio_dev_attr_in2_thresh_both_hyst_raw.dev_attr.attr,
440         &iio_dev_attr_in3_thresh_low_value.dev_attr.attr,
441         &iio_dev_attr_in3_thresh_high_value.dev_attr.attr,
442         &iio_dev_attr_in3_thresh_both_hyst_raw.dev_attr.attr,
443         &iio_dev_attr_sampling_frequency.dev_attr.attr,
444         &iio_const_attr_sampling_frequency_available.dev_attr.attr,
445         NULL,
446 };
447
448 static struct attribute_group ad7993_4_7_8_event_attrs_group = {
449         .attrs = ad7993_4_7_8_event_attributes,
450         .name = "events",
451 };
452
453 static struct attribute *ad7992_event_attributes[] = {
454         &iio_dev_attr_in0_thresh_low_value.dev_attr.attr,
455         &iio_dev_attr_in0_thresh_high_value.dev_attr.attr,
456         &iio_dev_attr_in0_thresh_both_hyst_raw.dev_attr.attr,
457         &iio_dev_attr_in1_thresh_low_value.dev_attr.attr,
458         &iio_dev_attr_in1_thresh_high_value.dev_attr.attr,
459         &iio_dev_attr_in1_thresh_both_hyst_raw.dev_attr.attr,
460         &iio_dev_attr_sampling_frequency.dev_attr.attr,
461         &iio_const_attr_sampling_frequency_available.dev_attr.attr,
462         NULL,
463 };
464
465 static struct attribute_group ad7992_event_attrs_group = {
466         .attrs = ad7992_event_attributes,
467         .name = "events",
468 };
469
470 static const struct iio_info ad7991_info = {
471         .read_raw = &ad799x_read_raw,
472         .driver_module = THIS_MODULE,
473 };
474
475 static const struct iio_info ad7992_info = {
476         .read_raw = &ad799x_read_raw,
477         .event_attrs = &ad7992_event_attrs_group,
478         .driver_module = THIS_MODULE,
479 };
480
481 static const struct iio_info ad7993_4_7_8_info = {
482         .read_raw = &ad799x_read_raw,
483         .event_attrs = &ad7993_4_7_8_event_attrs_group,
484         .driver_module = THIS_MODULE,
485 };
486
487 static const struct ad799x_chip_info ad799x_chip_info_tbl[] = {
488         [ad7991] = {
489                 .channel[0] = IIO_CHAN(IIO_IN, 0, 1, 0, NULL, 0, 0,
490                                        (1 << IIO_CHAN_INFO_SCALE_SHARED),
491                                        0, 0, IIO_ST('u', 12, 16, 0), 0),
492                 .channel[1] = IIO_CHAN(IIO_IN, 0, 1, 0, NULL, 1, 0,
493                                        (1 << IIO_CHAN_INFO_SCALE_SHARED),
494                                        1, 1, IIO_ST('u', 12, 16, 0), 0),
495                 .channel[2] = IIO_CHAN(IIO_IN, 0, 1, 0, NULL, 2, 0,
496                                        (1 << IIO_CHAN_INFO_SCALE_SHARED),
497                                        2, 2, IIO_ST('u', 12, 16, 0), 0),
498                 .channel[3] = IIO_CHAN(IIO_IN, 0, 1, 0, NULL, 3, 0,
499                                        (1 << IIO_CHAN_INFO_SCALE_SHARED),
500                                        3, 3, IIO_ST('u', 12, 16, 0), 0),
501                 .channel[4] = IIO_CHAN_SOFT_TIMESTAMP(4),
502                 .num_channels = 5,
503                 .int_vref_mv = 4096,
504                 .info = &ad7991_info,
505         },
506         [ad7995] = {
507                 .channel[0] = IIO_CHAN(IIO_IN, 0, 1, 0, NULL, 0, 0,
508                                        (1 << IIO_CHAN_INFO_SCALE_SHARED),
509                                        0, 0, IIO_ST('u', 10, 16, 0), 0),
510                 .channel[1] = IIO_CHAN(IIO_IN, 0, 1, 0, NULL, 1, 0,
511                                        (1 << IIO_CHAN_INFO_SCALE_SHARED),
512                                        1, 1, IIO_ST('u', 10, 16, 0), 0),
513                 .channel[2] = IIO_CHAN(IIO_IN, 0, 1, 0, NULL, 2, 0,
514                                        (1 << IIO_CHAN_INFO_SCALE_SHARED),
515                                        2, 2, IIO_ST('u', 10, 16, 0), 0),
516                 .channel[3] = IIO_CHAN(IIO_IN, 0, 1, 0, NULL, 3, 0,
517                                        (1 << IIO_CHAN_INFO_SCALE_SHARED),
518                                        3, 3, IIO_ST('u', 10, 16, 0), 0),
519                 .channel[4] = IIO_CHAN_SOFT_TIMESTAMP(4),
520                 .num_channels = 5,
521                 .int_vref_mv = 1024,
522                 .info = &ad7991_info,
523         },
524         [ad7999] = {
525                 .channel[0] = IIO_CHAN(IIO_IN, 0, 1, 0, NULL, 0, 0,
526                                        (1 << IIO_CHAN_INFO_SCALE_SHARED),
527                                        0, 0, IIO_ST('u', 10, 16, 0), 0),
528                 .channel[1] = IIO_CHAN(IIO_IN, 0, 1, 0, NULL, 1, 0,
529                                        (1 << IIO_CHAN_INFO_SCALE_SHARED),
530                                        1, 1, IIO_ST('u', 10, 16, 0), 0),
531                 .channel[2] = IIO_CHAN(IIO_IN, 0, 1, 0, NULL, 2, 0,
532                                        (1 << IIO_CHAN_INFO_SCALE_SHARED),
533                                        2, 2, IIO_ST('u', 10, 16, 0), 0),
534                 .channel[3] = IIO_CHAN(IIO_IN, 0, 1, 0, NULL, 3, 0,
535                                        (1 << IIO_CHAN_INFO_SCALE_SHARED),
536                                        3, 3, IIO_ST('u', 10, 16, 0), 0),
537                 .channel[4] = IIO_CHAN_SOFT_TIMESTAMP(4),
538                 .num_channels = 5,
539                 .int_vref_mv = 1024,
540                 .info = &ad7991_info,
541         },
542         [ad7992] = {
543                 .channel[0] = IIO_CHAN(IIO_IN, 0, 1, 0, NULL, 0, 0,
544                                        (1 << IIO_CHAN_INFO_SCALE_SHARED),
545                                        0, 0, IIO_ST('u', 12, 16, 0), 0),
546                 .channel[1] = IIO_CHAN(IIO_IN, 0, 1, 0, NULL, 1, 0,
547                                        (1 << IIO_CHAN_INFO_SCALE_SHARED),
548                                        1, 1, IIO_ST('u', 12, 16, 0), 0),
549                 .channel[2] = IIO_CHAN_SOFT_TIMESTAMP(2),
550                 .num_channels = 3,
551                 .int_vref_mv = 4096,
552                 .default_config = AD7998_ALERT_EN,
553                 .info = &ad7992_info,
554         },
555         [ad7993] = {
556                 .channel[0] = IIO_CHAN(IIO_IN, 0, 1, 0, NULL, 0, 0,
557                                        (1 << IIO_CHAN_INFO_SCALE_SHARED),
558                                        0, 0, IIO_ST('u', 10, 16, 0), 0),
559                 .channel[1] = IIO_CHAN(IIO_IN, 0, 1, 0, NULL, 1, 0,
560                                        (1 << IIO_CHAN_INFO_SCALE_SHARED),
561                                        1, 1, IIO_ST('u', 10, 16, 0), 0),
562                 .channel[2] = IIO_CHAN(IIO_IN, 0, 1, 0, NULL, 2, 0,
563                                        (1 << IIO_CHAN_INFO_SCALE_SHARED),
564                                        2, 2, IIO_ST('u', 10, 16, 0), 0),
565                 .channel[3] = IIO_CHAN(IIO_IN, 0, 1, 0, NULL, 3, 0,
566                                        (1 << IIO_CHAN_INFO_SCALE_SHARED),
567                                        3, 3, IIO_ST('u', 10, 16, 0), 0),
568                 .channel[4] = IIO_CHAN_SOFT_TIMESTAMP(4),
569                 .num_channels = 5,
570                 .int_vref_mv = 1024,
571                 .default_config = AD7998_ALERT_EN,
572                 .info = &ad7993_4_7_8_info,
573         },
574         [ad7994] = {
575                 .channel[0] = IIO_CHAN(IIO_IN, 0, 1, 0, NULL, 0, 0,
576                                        (1 << IIO_CHAN_INFO_SCALE_SHARED),
577                                        0, 0, IIO_ST('u', 12, 16, 0), 0),
578                 .channel[1] = IIO_CHAN(IIO_IN, 0, 1, 0, NULL, 1, 0,
579                                        (1 << IIO_CHAN_INFO_SCALE_SHARED),
580                                        1, 1, IIO_ST('u', 12, 16, 0), 0),
581                 .channel[2] = IIO_CHAN(IIO_IN, 0, 1, 0, NULL, 2, 0,
582                                        (1 << IIO_CHAN_INFO_SCALE_SHARED),
583                                        2, 2, IIO_ST('u', 12, 16, 0), 0),
584                 .channel[3] = IIO_CHAN(IIO_IN, 0, 1, 0, NULL, 3, 0,
585                                        (1 << IIO_CHAN_INFO_SCALE_SHARED),
586                                        3, 3, IIO_ST('u', 12, 16, 0), 0),
587                 .channel[4] = IIO_CHAN_SOFT_TIMESTAMP(4),
588                 .num_channels = 5,
589                 .int_vref_mv = 4096,
590                 .default_config = AD7998_ALERT_EN,
591                 .info = &ad7993_4_7_8_info,
592         },
593         [ad7997] = {
594                 .channel[0] = IIO_CHAN(IIO_IN, 0, 1, 0, NULL, 0, 0,
595                                           (1 << IIO_CHAN_INFO_SCALE_SHARED),
596                                           0, 0, IIO_ST('u', 10, 16, 0), 0),
597                 .channel[1] = IIO_CHAN(IIO_IN, 0, 1, 0, NULL, 1, 0,
598                                           (1 << IIO_CHAN_INFO_SCALE_SHARED),
599                                           1, 1, IIO_ST('u', 10, 16, 0), 0),
600                 .channel[2] = IIO_CHAN(IIO_IN, 0, 1, 0, NULL, 2, 0,
601                                           (1 << IIO_CHAN_INFO_SCALE_SHARED),
602                                           2, 2, IIO_ST('u', 10, 16, 0), 0),
603                 .channel[3] = IIO_CHAN(IIO_IN, 0, 1, 0, NULL, 3, 0,
604                                           (1 << IIO_CHAN_INFO_SCALE_SHARED),
605                                           3, 3, IIO_ST('u', 10, 16, 0), 0),
606                 .channel[4] = IIO_CHAN(IIO_IN, 0, 1, 0, NULL, 4, 0,
607                                           (1 << IIO_CHAN_INFO_SCALE_SHARED),
608                                           4, 4, IIO_ST('u', 10, 16, 0), 0),
609                 .channel[5] = IIO_CHAN(IIO_IN, 0, 1, 0, NULL, 5, 0,
610                                           (1 << IIO_CHAN_INFO_SCALE_SHARED),
611                                           5, 5, IIO_ST('u', 10, 16, 0), 0),
612                 .channel[6] = IIO_CHAN(IIO_IN, 0, 1, 0, NULL, 6, 0,
613                                           (1 << IIO_CHAN_INFO_SCALE_SHARED),
614                                           6, 6, IIO_ST('u', 10, 16, 0), 0),
615                 .channel[7] = IIO_CHAN(IIO_IN, 0, 1, 0, NULL, 7, 0,
616                                           (1 << IIO_CHAN_INFO_SCALE_SHARED),
617                                           7, 7, IIO_ST('u', 10, 16, 0), 0),
618                 .channel[8] = IIO_CHAN_SOFT_TIMESTAMP(8),
619                 .num_channels = 9,
620                 .int_vref_mv = 1024,
621                 .default_config = AD7998_ALERT_EN,
622                 .info = &ad7993_4_7_8_info,
623         },
624         [ad7998] = {
625                 .channel[0] = IIO_CHAN(IIO_IN, 0, 1, 0, NULL, 0, 0,
626                                        (1 << IIO_CHAN_INFO_SCALE_SHARED),
627                                        0, 0, IIO_ST('u', 12, 16, 0), 0),
628                 .channel[1] = IIO_CHAN(IIO_IN, 0, 1, 0, NULL, 1, 0,
629                                        (1 << IIO_CHAN_INFO_SCALE_SHARED),
630                                        1, 1, IIO_ST('u', 12, 16, 0), 0),
631                 .channel[2] = IIO_CHAN(IIO_IN, 0, 1, 0, NULL, 2, 0,
632                                        (1 << IIO_CHAN_INFO_SCALE_SHARED),
633                                        2, 2, IIO_ST('u', 12, 16, 0), 0),
634                 .channel[3] = IIO_CHAN(IIO_IN, 0, 1, 0, NULL, 3, 0,
635                                        (1 << IIO_CHAN_INFO_SCALE_SHARED),
636                                        3, 3, IIO_ST('u', 12, 16, 0), 0),
637                 .channel[4] = IIO_CHAN(IIO_IN, 0, 1, 0, NULL, 4, 0,
638                                        (1 << IIO_CHAN_INFO_SCALE_SHARED),
639                                        4, 4, IIO_ST('u', 12, 16, 0), 0),
640                 .channel[5] = IIO_CHAN(IIO_IN, 0, 1, 0, NULL, 5, 0,
641                                        (1 << IIO_CHAN_INFO_SCALE_SHARED),
642                                        5, 5, IIO_ST('u', 12, 16, 0), 0),
643                 .channel[6] = IIO_CHAN(IIO_IN, 0, 1, 0, NULL, 6, 0,
644                                        (1 << IIO_CHAN_INFO_SCALE_SHARED),
645                                        6, 6, IIO_ST('u', 12, 16, 0), 0),
646                 .channel[7] = IIO_CHAN(IIO_IN, 0, 1, 0, NULL, 7, 0,
647                                           (1 << IIO_CHAN_INFO_SCALE_SHARED),
648                                           7, 7, IIO_ST('u', 12, 16, 0), 0),
649                 .channel[8] = IIO_CHAN_SOFT_TIMESTAMP(8),
650                 .num_channels = 9,
651                 .int_vref_mv = 4096,
652                 .default_config = AD7998_ALERT_EN,
653                 .info = &ad7993_4_7_8_info,
654         },
655 };
656
657 static int __devinit ad799x_probe(struct i2c_client *client,
658                                    const struct i2c_device_id *id)
659 {
660         int ret, regdone = 0;
661         struct ad799x_platform_data *pdata = client->dev.platform_data;
662         struct ad799x_state *st;
663         struct iio_dev *indio_dev = iio_allocate_device(sizeof(*st));
664
665         if (indio_dev == NULL)
666                 return -ENOMEM;
667
668         st = iio_priv(indio_dev);
669         /* this is only used for device removal purposes */
670         i2c_set_clientdata(client, indio_dev);
671
672         st->id = id->driver_data;
673         st->chip_info = &ad799x_chip_info_tbl[st->id];
674         st->config = st->chip_info->default_config;
675
676         /* TODO: Add pdata options for filtering and bit delay */
677
678         if (pdata)
679                 st->int_vref_mv = pdata->vref_mv;
680         else
681                 st->int_vref_mv = st->chip_info->int_vref_mv;
682
683         st->reg = regulator_get(&client->dev, "vcc");
684         if (!IS_ERR(st->reg)) {
685                 ret = regulator_enable(st->reg);
686                 if (ret)
687                         goto error_put_reg;
688         }
689         st->client = client;
690
691         indio_dev->dev.parent = &client->dev;
692         indio_dev->name = id->name;
693         indio_dev->info = st->chip_info->info;
694         indio_dev->name = id->name;
695
696         indio_dev->modes = INDIO_DIRECT_MODE;
697         indio_dev->channels = st->chip_info->channel;
698         indio_dev->num_channels = st->chip_info->num_channels;
699
700         ret = ad799x_register_ring_funcs_and_init(indio_dev);
701         if (ret)
702                 goto error_disable_reg;
703
704         ret = iio_device_register(indio_dev);
705         if (ret)
706                 goto error_cleanup_ring;
707         regdone = 1;
708
709         ret = iio_ring_buffer_register_ex(indio_dev, 0,
710                                           indio_dev->channels,
711                                           indio_dev->num_channels);
712         if (ret)
713                 goto error_cleanup_ring;
714
715         if (client->irq > 0) {
716                 ret = request_threaded_irq(client->irq,
717                                            NULL,
718                                            ad799x_event_handler,
719                                            IRQF_TRIGGER_FALLING |
720                                            IRQF_ONESHOT,
721                                            client->name,
722                                            indio_dev);
723                 if (ret)
724                         goto error_cleanup_ring;
725         }
726
727         return 0;
728
729 error_cleanup_ring:
730         ad799x_ring_cleanup(indio_dev);
731 error_disable_reg:
732         if (!IS_ERR(st->reg))
733                 regulator_disable(st->reg);
734 error_put_reg:
735         if (!IS_ERR(st->reg))
736                 regulator_put(st->reg);
737         if (regdone)
738                 iio_device_unregister(indio_dev);
739         else
740                 iio_free_device(indio_dev);
741
742         return ret;
743 }
744
745 static __devexit int ad799x_remove(struct i2c_client *client)
746 {
747         struct iio_dev *indio_dev = i2c_get_clientdata(client);
748         struct ad799x_state *st = iio_priv(indio_dev);
749
750         if (client->irq > 0)
751                 free_irq(client->irq, indio_dev);
752
753         iio_ring_buffer_unregister(indio_dev);
754         ad799x_ring_cleanup(indio_dev);
755         if (!IS_ERR(st->reg)) {
756                 regulator_disable(st->reg);
757                 regulator_put(st->reg);
758         }
759         iio_device_unregister(indio_dev);
760
761         return 0;
762 }
763
764 static const struct i2c_device_id ad799x_id[] = {
765         { "ad7991", ad7991 },
766         { "ad7995", ad7995 },
767         { "ad7999", ad7999 },
768         { "ad7992", ad7992 },
769         { "ad7993", ad7993 },
770         { "ad7994", ad7994 },
771         { "ad7997", ad7997 },
772         { "ad7998", ad7998 },
773         {}
774 };
775
776 MODULE_DEVICE_TABLE(i2c, ad799x_id);
777
778 static struct i2c_driver ad799x_driver = {
779         .driver = {
780                 .name = "ad799x",
781         },
782         .probe = ad799x_probe,
783         .remove = __devexit_p(ad799x_remove),
784         .id_table = ad799x_id,
785 };
786
787 static __init int ad799x_init(void)
788 {
789         return i2c_add_driver(&ad799x_driver);
790 }
791
792 static __exit void ad799x_exit(void)
793 {
794         i2c_del_driver(&ad799x_driver);
795 }
796
797 MODULE_AUTHOR("Michael Hennerich <hennerich@blackfin.uclinux.org>");
798 MODULE_DESCRIPTION("Analog Devices AD799x ADC");
799 MODULE_LICENSE("GPL v2");
800 MODULE_ALIAS("i2c:ad799x");
801
802 module_init(ad799x_init);
803 module_exit(ad799x_exit);