]> git.karo-electronics.de Git - karo-tx-linux.git/blob - drivers/staging/iio/adc/ad799x_core.c
94bd8080f69d7ce0b563b9c28e4c2eb397933a52
[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/workqueue.h>
27 #include <linux/device.h>
28 #include <linux/kernel.h>
29 #include <linux/sysfs.h>
30 #include <linux/list.h>
31 #include <linux/i2c.h>
32 #include <linux/regulator/consumer.h>
33 #include <linux/slab.h>
34 #include <linux/types.h>
35 #include <linux/err.h>
36
37 #include "../iio.h"
38 #include "../sysfs.h"
39
40 #include "../ring_generic.h"
41 #include "adc.h"
42 #include "ad799x.h"
43
44 /*
45  * ad799x register access by I2C
46  */
47 static int ad799x_i2c_read16(struct ad799x_state *st, u8 reg, u16 *data)
48 {
49         struct i2c_client *client = st->client;
50         int ret = 0;
51
52         ret = i2c_smbus_read_word_data(client, reg);
53         if (ret < 0) {
54                 dev_err(&client->dev, "I2C read error\n");
55                 return ret;
56         }
57
58         *data = swab16((u16)ret);
59
60         return 0;
61 }
62
63 static int ad799x_i2c_read8(struct ad799x_state *st, u8 reg, u8 *data)
64 {
65         struct i2c_client *client = st->client;
66         int ret = 0;
67
68         ret = i2c_smbus_read_byte_data(client, reg);
69         if (ret < 0) {
70                 dev_err(&client->dev, "I2C read error\n");
71                 return ret;
72         }
73
74         *data = (u8)ret;
75
76         return 0;
77 }
78
79 static int ad799x_i2c_write16(struct ad799x_state *st, u8 reg, u16 data)
80 {
81         struct i2c_client *client = st->client;
82         int ret = 0;
83
84         ret = i2c_smbus_write_word_data(client, reg, swab16(data));
85         if (ret < 0)
86                 dev_err(&client->dev, "I2C write error\n");
87
88         return ret;
89 }
90
91 static int ad799x_i2c_write8(struct ad799x_state *st, u8 reg, u8 data)
92 {
93         struct i2c_client *client = st->client;
94         int ret = 0;
95
96         ret = i2c_smbus_write_byte_data(client, reg, data);
97         if (ret < 0)
98                 dev_err(&client->dev, "I2C write error\n");
99
100         return ret;
101 }
102
103 int ad7997_8_set_scan_mode(struct ad799x_state *st, unsigned mask)
104 {
105         return ad799x_i2c_write16(st, AD7998_CONF_REG,
106                 st->config | (mask << AD799X_CHANNEL_SHIFT));
107 }
108
109 static int ad799x_scan_direct(struct ad799x_state *st, unsigned ch)
110 {
111         u16 rxbuf;
112         u8 cmd;
113         int ret;
114
115         switch (st->id) {
116         case ad7991:
117         case ad7995:
118         case ad7999:
119                 cmd = st->config | ((1 << ch) << AD799X_CHANNEL_SHIFT);
120                 break;
121         case ad7992:
122         case ad7993:
123         case ad7994:
124                 cmd = (1 << ch) << AD799X_CHANNEL_SHIFT;
125                 break;
126         case ad7997:
127         case ad7998:
128                 cmd = (ch << AD799X_CHANNEL_SHIFT) | AD7997_8_READ_SINGLE;
129                 break;
130         default:
131                 return -EINVAL;
132         }
133
134         ret = ad799x_i2c_read16(st, cmd, &rxbuf);
135         if (ret < 0)
136                 return ret;
137
138         return rxbuf;
139 }
140
141 static int ad799x_read_raw(struct iio_dev *dev_info,
142                            struct iio_chan_spec const *chan,
143                            int *val,
144                            int *val2,
145                            long m)
146 {
147         int ret;
148         struct ad799x_state *st = dev_info->dev_data;
149         unsigned int scale_uv;
150
151         switch (m) {
152         case 0:
153                 mutex_lock(&dev_info->mlock);
154                 if (iio_ring_enabled(dev_info))
155                         ret = ad799x_single_channel_from_ring(st,
156                                 1 << chan->address);
157                 else
158                         ret = ad799x_scan_direct(st, chan->address);
159                 mutex_unlock(&dev_info->mlock);
160
161                 if (ret < 0)
162                         return ret;
163                 *val = (ret >> st->chip_info->channel[0].scan_type.shift) &
164                         RES_MASK(st->chip_info->channel[0].scan_type.realbits);
165                 return IIO_VAL_INT;
166         case (1 << IIO_CHAN_INFO_SCALE_SHARED):
167                 scale_uv = (st->int_vref_mv * 1000)
168                         >> st->chip_info->channel[0].scan_type.realbits;
169                 *val =  scale_uv / 1000;
170                 *val2 = (scale_uv % 1000) * 1000;
171                 return IIO_VAL_INT_PLUS_MICRO;
172         }
173         return -EINVAL;
174 }
175
176 static ssize_t ad799x_read_frequency(struct device *dev,
177                                         struct device_attribute *attr,
178                                         char *buf)
179 {
180         struct iio_dev *dev_info = dev_get_drvdata(dev);
181         struct ad799x_state *st = iio_dev_get_devdata(dev_info);
182
183         int ret, len = 0;
184         u8 val;
185         ret = ad799x_i2c_read8(st, AD7998_CYCLE_TMR_REG, &val);
186         if (ret)
187                 return ret;
188
189         val &= AD7998_CYC_MASK;
190
191         switch (val) {
192         case AD7998_CYC_DIS:
193                 len = sprintf(buf, "0\n");
194                 break;
195         case AD7998_CYC_TCONF_32:
196                 len = sprintf(buf, "15625\n");
197                 break;
198         case AD7998_CYC_TCONF_64:
199                 len = sprintf(buf, "7812\n");
200                 break;
201         case AD7998_CYC_TCONF_128:
202                 len = sprintf(buf, "3906\n");
203                 break;
204         case AD7998_CYC_TCONF_256:
205                 len = sprintf(buf, "1953\n");
206                 break;
207         case AD7998_CYC_TCONF_512:
208                 len = sprintf(buf, "976\n");
209                 break;
210         case AD7998_CYC_TCONF_1024:
211                 len = sprintf(buf, "488\n");
212                 break;
213         case AD7998_CYC_TCONF_2048:
214                 len = sprintf(buf, "244\n");
215                 break;
216         }
217         return len;
218 }
219
220 static ssize_t ad799x_write_frequency(struct device *dev,
221                                          struct device_attribute *attr,
222                                          const char *buf,
223                                          size_t len)
224 {
225         struct iio_dev *dev_info = dev_get_drvdata(dev);
226         struct ad799x_state *st = iio_dev_get_devdata(dev_info);
227
228         long val;
229         int ret;
230         u8 t;
231
232         ret = strict_strtol(buf, 10, &val);
233         if (ret)
234                 return ret;
235
236         mutex_lock(&dev_info->mlock);
237         ret = ad799x_i2c_read8(st, AD7998_CYCLE_TMR_REG, &t);
238         if (ret)
239                 goto error_ret_mutex;
240         /* Wipe the bits clean */
241         t &= ~AD7998_CYC_MASK;
242
243         switch (val) {
244         case 15625:
245                 t |= AD7998_CYC_TCONF_32;
246                 break;
247         case 7812:
248                 t |= AD7998_CYC_TCONF_64;
249                 break;
250         case 3906:
251                 t |= AD7998_CYC_TCONF_128;
252                 break;
253         case 1953:
254                 t |= AD7998_CYC_TCONF_256;
255                 break;
256         case 976:
257                 t |= AD7998_CYC_TCONF_512;
258                 break;
259         case 488:
260                 t |= AD7998_CYC_TCONF_1024;
261                 break;
262         case 244:
263                 t |= AD7998_CYC_TCONF_2048;
264                 break;
265         case  0:
266                 t |= AD7998_CYC_DIS;
267                 break;
268         default:
269                 ret = -EINVAL;
270                 goto error_ret_mutex;
271         }
272
273         ret = ad799x_i2c_write8(st, AD7998_CYCLE_TMR_REG, t);
274
275 error_ret_mutex:
276         mutex_unlock(&dev_info->mlock);
277
278         return ret ? ret : len;
279 }
280
281 static ssize_t ad799x_read_channel_config(struct device *dev,
282                                         struct device_attribute *attr,
283                                         char *buf)
284 {
285         struct iio_dev *dev_info = dev_get_drvdata(dev);
286         struct ad799x_state *st = iio_dev_get_devdata(dev_info);
287         struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
288
289         int ret;
290         u16 val;
291         ret = ad799x_i2c_read16(st, this_attr->address, &val);
292         if (ret)
293                 return ret;
294
295         return sprintf(buf, "%d\n", val);
296 }
297
298 static ssize_t ad799x_write_channel_config(struct device *dev,
299                                          struct device_attribute *attr,
300                                          const char *buf,
301                                          size_t len)
302 {
303         struct iio_dev *dev_info = dev_get_drvdata(dev);
304         struct ad799x_state *st = iio_dev_get_devdata(dev_info);
305         struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
306
307         long val;
308         int ret;
309
310         ret = strict_strtol(buf, 10, &val);
311         if (ret)
312                 return ret;
313
314         mutex_lock(&dev_info->mlock);
315         ret = ad799x_i2c_write16(st, this_attr->address, val);
316         mutex_unlock(&dev_info->mlock);
317
318         return ret ? ret : len;
319 }
320
321 static irqreturn_t ad799x_event_handler(int irq, void *private)
322 {
323         struct iio_dev *indio_dev = private;
324         struct ad799x_state *st = iio_dev_get_devdata(private);
325         u8 status;
326         int i, ret;
327
328         ret = ad799x_i2c_read8(st, AD7998_ALERT_STAT_REG, &status);
329         if (ret)
330                 return ret;
331
332         if (!status)
333                 return -EIO;
334
335         ad799x_i2c_write8(st, AD7998_ALERT_STAT_REG, AD7998_ALERT_STAT_CLEAR);
336
337         for (i = 0; i < 8; i++) {
338                 if (status & (1 << i))
339                         iio_push_event(indio_dev, 0,
340                                        i & 0x1 ?
341                                        IIO_EVENT_CODE_IN_HIGH_THRESH(i >> 1) :
342                                        IIO_EVENT_CODE_IN_LOW_THRESH(i >> 1),
343                                        iio_get_time_ns());
344         }
345
346         return IRQ_HANDLED;
347 }
348
349 static IIO_DEVICE_ATTR(in0_thresh_low_value,
350                        S_IRUGO | S_IWUSR,
351                        ad799x_read_channel_config,
352                        ad799x_write_channel_config,
353                        AD7998_DATALOW_CH1_REG);
354
355 static IIO_DEVICE_ATTR(in0_thresh_high_value,
356                        S_IRUGO | S_IWUSR,
357                        ad799x_read_channel_config,
358                        ad799x_write_channel_config,
359                        AD7998_DATAHIGH_CH1_REG);
360
361 static IIO_DEVICE_ATTR(in0_thresh_both_hyst_raw,
362                        S_IRUGO | S_IWUSR,
363                        ad799x_read_channel_config,
364                        ad799x_write_channel_config,
365                        AD7998_HYST_CH1_REG);
366
367 static IIO_DEVICE_ATTR(in1_thresh_low_value,
368                        S_IRUGO | S_IWUSR,
369                        ad799x_read_channel_config,
370                        ad799x_write_channel_config,
371                        AD7998_DATALOW_CH2_REG);
372
373 static IIO_DEVICE_ATTR(in1_thresh_high_value,
374                        S_IRUGO | S_IWUSR,
375                        ad799x_read_channel_config,
376                        ad799x_write_channel_config,
377                        AD7998_DATAHIGH_CH2_REG);
378
379 static IIO_DEVICE_ATTR(in1_thresh_both_hyst_raw,
380                        S_IRUGO | S_IWUSR,
381                        ad799x_read_channel_config,
382                        ad799x_write_channel_config,
383                        AD7998_HYST_CH2_REG);
384
385 static IIO_DEVICE_ATTR(in2_thresh_low_value,
386                        S_IRUGO | S_IWUSR,
387                        ad799x_read_channel_config,
388                        ad799x_write_channel_config,
389                        AD7998_DATALOW_CH3_REG);
390
391 static IIO_DEVICE_ATTR(in2_thresh_high_value,
392                        S_IRUGO | S_IWUSR,
393                        ad799x_read_channel_config,
394                        ad799x_write_channel_config,
395                        AD7998_DATAHIGH_CH3_REG);
396
397 static IIO_DEVICE_ATTR(in2_thresh_both_hyst_raw,
398                        S_IRUGO | S_IWUSR,
399                        ad799x_read_channel_config,
400                        ad799x_write_channel_config,
401                        AD7998_HYST_CH3_REG);
402
403 static IIO_DEVICE_ATTR(in3_thresh_low_value,
404                        S_IRUGO | S_IWUSR,
405                        ad799x_read_channel_config,
406                        ad799x_write_channel_config,
407                        AD7998_DATALOW_CH4_REG);
408
409 static IIO_DEVICE_ATTR(in3_thresh_high_value,
410                        S_IRUGO | S_IWUSR,
411                        ad799x_read_channel_config,
412                        ad799x_write_channel_config,
413                        AD7998_DATAHIGH_CH4_REG);
414
415 static IIO_DEVICE_ATTR(in3_thresh_both_hyst_raw,
416                        S_IRUGO | S_IWUSR,
417                        ad799x_read_channel_config,
418                        ad799x_write_channel_config,
419                        AD7998_HYST_CH4_REG);
420
421 static IIO_DEV_ATTR_SAMP_FREQ(S_IWUSR | S_IRUGO,
422                               ad799x_read_frequency,
423                               ad799x_write_frequency);
424 static IIO_CONST_ATTR_SAMP_FREQ_AVAIL("15625 7812 3906 1953 976 488 244 0");
425
426 static struct attribute *ad7993_4_7_8_event_attributes[] = {
427         &iio_dev_attr_in0_thresh_low_value.dev_attr.attr,
428         &iio_dev_attr_in0_thresh_high_value.dev_attr.attr,
429         &iio_dev_attr_in0_thresh_both_hyst_raw.dev_attr.attr,
430         &iio_dev_attr_in1_thresh_low_value.dev_attr.attr,
431         &iio_dev_attr_in1_thresh_high_value.dev_attr.attr,
432         &iio_dev_attr_in1_thresh_both_hyst_raw.dev_attr.attr,
433         &iio_dev_attr_in2_thresh_low_value.dev_attr.attr,
434         &iio_dev_attr_in2_thresh_high_value.dev_attr.attr,
435         &iio_dev_attr_in2_thresh_both_hyst_raw.dev_attr.attr,
436         &iio_dev_attr_in3_thresh_low_value.dev_attr.attr,
437         &iio_dev_attr_in3_thresh_high_value.dev_attr.attr,
438         &iio_dev_attr_in3_thresh_both_hyst_raw.dev_attr.attr,
439         &iio_dev_attr_sampling_frequency.dev_attr.attr,
440         &iio_const_attr_sampling_frequency_available.dev_attr.attr,
441         NULL,
442 };
443
444 static struct attribute_group ad7993_4_7_8_event_attrs_group = {
445         .attrs = ad7993_4_7_8_event_attributes,
446 };
447
448 static struct attribute *ad7992_event_attributes[] = {
449         &iio_dev_attr_in0_thresh_low_value.dev_attr.attr,
450         &iio_dev_attr_in0_thresh_high_value.dev_attr.attr,
451         &iio_dev_attr_in0_thresh_both_hyst_raw.dev_attr.attr,
452         &iio_dev_attr_in1_thresh_low_value.dev_attr.attr,
453         &iio_dev_attr_in1_thresh_high_value.dev_attr.attr,
454         &iio_dev_attr_in1_thresh_both_hyst_raw.dev_attr.attr,
455         &iio_dev_attr_sampling_frequency.dev_attr.attr,
456         &iio_const_attr_sampling_frequency_available.dev_attr.attr,
457         NULL,
458 };
459
460 static struct attribute_group ad7992_event_attrs_group = {
461         .attrs = ad7992_event_attributes,
462 };
463
464 static const struct ad799x_chip_info ad799x_chip_info_tbl[] = {
465         [ad7991] = {
466                 .channel[0] = IIO_CHAN(IIO_IN, 0, 1, 0, NULL, 0, 0,
467                                        (1 << IIO_CHAN_INFO_SCALE_SHARED),
468                                        0, 0, IIO_ST('u', 12, 16, 0), 0),
469                 .channel[1] = IIO_CHAN(IIO_IN, 0, 1, 0, NULL, 1, 0,
470                                        (1 << IIO_CHAN_INFO_SCALE_SHARED),
471                                        1, 1, IIO_ST('u', 12, 16, 0), 0),
472                 .channel[2] = IIO_CHAN(IIO_IN, 0, 1, 0, NULL, 2, 0,
473                                        (1 << IIO_CHAN_INFO_SCALE_SHARED),
474                                        2, 2, IIO_ST('u', 12, 16, 0), 0),
475                 .channel[3] = IIO_CHAN(IIO_IN, 0, 1, 0, NULL, 3, 0,
476                                        (1 << IIO_CHAN_INFO_SCALE_SHARED),
477                                        3, 3, IIO_ST('u', 12, 16, 0), 0),
478                 .channel[4] = IIO_CHAN_SOFT_TIMESTAMP(4),
479                 .num_channels = 5,
480                 .int_vref_mv = 4096,
481         },
482         [ad7995] = {
483                 .channel[0] = IIO_CHAN(IIO_IN, 0, 1, 0, NULL, 0, 0,
484                                        (1 << IIO_CHAN_INFO_SCALE_SHARED),
485                                        0, 0, IIO_ST('u', 10, 16, 0), 0),
486                 .channel[1] = IIO_CHAN(IIO_IN, 0, 1, 0, NULL, 1, 0,
487                                        (1 << IIO_CHAN_INFO_SCALE_SHARED),
488                                        1, 1, IIO_ST('u', 10, 16, 0), 0),
489                 .channel[2] = IIO_CHAN(IIO_IN, 0, 1, 0, NULL, 2, 0,
490                                        (1 << IIO_CHAN_INFO_SCALE_SHARED),
491                                        2, 2, IIO_ST('u', 10, 16, 0), 0),
492                 .channel[3] = IIO_CHAN(IIO_IN, 0, 1, 0, NULL, 3, 0,
493                                        (1 << IIO_CHAN_INFO_SCALE_SHARED),
494                                        3, 3, IIO_ST('u', 10, 16, 0), 0),
495                 .channel[4] = IIO_CHAN_SOFT_TIMESTAMP(4),
496                 .num_channels = 5,
497                 .int_vref_mv = 1024,
498         },
499         [ad7999] = {
500                 .channel[0] = IIO_CHAN(IIO_IN, 0, 1, 0, NULL, 0, 0,
501                                        (1 << IIO_CHAN_INFO_SCALE_SHARED),
502                                        0, 0, IIO_ST('u', 10, 16, 0), 0),
503                 .channel[1] = IIO_CHAN(IIO_IN, 0, 1, 0, NULL, 1, 0,
504                                        (1 << IIO_CHAN_INFO_SCALE_SHARED),
505                                        1, 1, IIO_ST('u', 10, 16, 0), 0),
506                 .channel[2] = IIO_CHAN(IIO_IN, 0, 1, 0, NULL, 2, 0,
507                                        (1 << IIO_CHAN_INFO_SCALE_SHARED),
508                                        2, 2, IIO_ST('u', 10, 16, 0), 0),
509                 .channel[3] = IIO_CHAN(IIO_IN, 0, 1, 0, NULL, 3, 0,
510                                        (1 << IIO_CHAN_INFO_SCALE_SHARED),
511                                        3, 3, IIO_ST('u', 10, 16, 0), 0),
512                 .channel[4] = IIO_CHAN_SOFT_TIMESTAMP(4),
513                 .num_channels = 5,
514                 .int_vref_mv = 1024,
515         },
516         [ad7992] = {
517                 .channel[0] = IIO_CHAN(IIO_IN, 0, 1, 0, NULL, 0, 0,
518                                        (1 << IIO_CHAN_INFO_SCALE_SHARED),
519                                        0, 0, IIO_ST('u', 12, 16, 0), 0),
520                 .channel[1] = IIO_CHAN(IIO_IN, 0, 1, 0, NULL, 1, 0,
521                                        (1 << IIO_CHAN_INFO_SCALE_SHARED),
522                                        1, 1, IIO_ST('u', 12, 16, 0), 0),
523                 .channel[2] = IIO_CHAN_SOFT_TIMESTAMP(2),
524                 .num_channels = 3,
525                 .int_vref_mv = 4096,
526                 .monitor_mode = true,
527                 .default_config = AD7998_ALERT_EN,
528                 .event_attrs = &ad7992_event_attrs_group,
529         },
530         [ad7993] = {
531                 .channel[0] = IIO_CHAN(IIO_IN, 0, 1, 0, NULL, 0, 0,
532                                        (1 << IIO_CHAN_INFO_SCALE_SHARED),
533                                        0, 0, IIO_ST('u', 10, 16, 0), 0),
534                 .channel[1] = IIO_CHAN(IIO_IN, 0, 1, 0, NULL, 1, 0,
535                                        (1 << IIO_CHAN_INFO_SCALE_SHARED),
536                                        1, 1, IIO_ST('u', 10, 16, 0), 0),
537                 .channel[2] = IIO_CHAN(IIO_IN, 0, 1, 0, NULL, 2, 0,
538                                        (1 << IIO_CHAN_INFO_SCALE_SHARED),
539                                        2, 2, IIO_ST('u', 10, 16, 0), 0),
540                 .channel[3] = IIO_CHAN(IIO_IN, 0, 1, 0, NULL, 3, 0,
541                                        (1 << IIO_CHAN_INFO_SCALE_SHARED),
542                                        3, 3, IIO_ST('u', 10, 16, 0), 0),
543                 .channel[4] = IIO_CHAN_SOFT_TIMESTAMP(4),
544                 .num_channels = 5,
545                 .int_vref_mv = 1024,
546                 .monitor_mode = true,
547                 .default_config = AD7998_ALERT_EN,
548                 .event_attrs = &ad7993_4_7_8_event_attrs_group,
549         },
550         [ad7994] = {
551                 .channel[0] = IIO_CHAN(IIO_IN, 0, 1, 0, NULL, 0, 0,
552                                        (1 << IIO_CHAN_INFO_SCALE_SHARED),
553                                        0, 0, IIO_ST('u', 12, 16, 0), 0),
554                 .channel[1] = IIO_CHAN(IIO_IN, 0, 1, 0, NULL, 1, 0,
555                                        (1 << IIO_CHAN_INFO_SCALE_SHARED),
556                                        1, 1, IIO_ST('u', 12, 16, 0), 0),
557                 .channel[2] = IIO_CHAN(IIO_IN, 0, 1, 0, NULL, 2, 0,
558                                        (1 << IIO_CHAN_INFO_SCALE_SHARED),
559                                        2, 2, IIO_ST('u', 12, 16, 0), 0),
560                 .channel[3] = IIO_CHAN(IIO_IN, 0, 1, 0, NULL, 3, 0,
561                                        (1 << IIO_CHAN_INFO_SCALE_SHARED),
562                                        3, 3, IIO_ST('u', 12, 16, 0), 0),
563                 .channel[4] = IIO_CHAN_SOFT_TIMESTAMP(4),
564                 .num_channels = 5,
565                 .int_vref_mv = 4096,
566                 .monitor_mode = true,
567                 .default_config = AD7998_ALERT_EN,
568                 .event_attrs = &ad7993_4_7_8_event_attrs_group,
569         },
570         [ad7997] = {
571                 .channel[0] = IIO_CHAN(IIO_IN, 0, 1, 0, NULL, 0, 0,
572                                           (1 << IIO_CHAN_INFO_SCALE_SHARED),
573                                           0, 0, IIO_ST('u', 10, 16, 0), 0),
574                 .channel[1] = IIO_CHAN(IIO_IN, 0, 1, 0, NULL, 1, 0,
575                                           (1 << IIO_CHAN_INFO_SCALE_SHARED),
576                                           1, 1, IIO_ST('u', 10, 16, 0), 0),
577                 .channel[2] = IIO_CHAN(IIO_IN, 0, 1, 0, NULL, 2, 0,
578                                           (1 << IIO_CHAN_INFO_SCALE_SHARED),
579                                           2, 2, IIO_ST('u', 10, 16, 0), 0),
580                 .channel[3] = IIO_CHAN(IIO_IN, 0, 1, 0, NULL, 3, 0,
581                                           (1 << IIO_CHAN_INFO_SCALE_SHARED),
582                                           3, 3, IIO_ST('u', 10, 16, 0), 0),
583                 .channel[4] = IIO_CHAN(IIO_IN, 0, 1, 0, NULL, 4, 0,
584                                           (1 << IIO_CHAN_INFO_SCALE_SHARED),
585                                           4, 4, IIO_ST('u', 10, 16, 0), 0),
586                 .channel[5] = IIO_CHAN(IIO_IN, 0, 1, 0, NULL, 5, 0,
587                                           (1 << IIO_CHAN_INFO_SCALE_SHARED),
588                                           5, 5, IIO_ST('u', 10, 16, 0), 0),
589                 .channel[6] = IIO_CHAN(IIO_IN, 0, 1, 0, NULL, 6, 0,
590                                           (1 << IIO_CHAN_INFO_SCALE_SHARED),
591                                           6, 6, IIO_ST('u', 10, 16, 0), 0),
592                 .channel[7] = IIO_CHAN(IIO_IN, 0, 1, 0, NULL, 7, 0,
593                                           (1 << IIO_CHAN_INFO_SCALE_SHARED),
594                                           7, 7, IIO_ST('u', 10, 16, 0), 0),
595                 .channel[8] = IIO_CHAN_SOFT_TIMESTAMP(8),
596                 .num_channels = 9,
597                 .int_vref_mv = 1024,
598                 .monitor_mode = true,
599                 .default_config = AD7998_ALERT_EN,
600                 .event_attrs = &ad7993_4_7_8_event_attrs_group,
601         },
602         [ad7998] = {
603                 .channel[0] = IIO_CHAN(IIO_IN, 0, 1, 0, NULL, 0, 0,
604                                        (1 << IIO_CHAN_INFO_SCALE_SHARED),
605                                        0, 0, IIO_ST('u', 12, 16, 0), 0),
606                 .channel[1] = IIO_CHAN(IIO_IN, 0, 1, 0, NULL, 1, 0,
607                                        (1 << IIO_CHAN_INFO_SCALE_SHARED),
608                                        1, 1, IIO_ST('u', 12, 16, 0), 0),
609                 .channel[2] = IIO_CHAN(IIO_IN, 0, 1, 0, NULL, 2, 0,
610                                        (1 << IIO_CHAN_INFO_SCALE_SHARED),
611                                        2, 2, IIO_ST('u', 12, 16, 0), 0),
612                 .channel[3] = IIO_CHAN(IIO_IN, 0, 1, 0, NULL, 3, 0,
613                                        (1 << IIO_CHAN_INFO_SCALE_SHARED),
614                                        3, 3, IIO_ST('u', 12, 16, 0), 0),
615                 .channel[4] = IIO_CHAN(IIO_IN, 0, 1, 0, NULL, 4, 0,
616                                        (1 << IIO_CHAN_INFO_SCALE_SHARED),
617                                        4, 4, IIO_ST('u', 12, 16, 0), 0),
618                 .channel[5] = IIO_CHAN(IIO_IN, 0, 1, 0, NULL, 5, 0,
619                                        (1 << IIO_CHAN_INFO_SCALE_SHARED),
620                                        5, 5, IIO_ST('u', 12, 16, 0), 0),
621                 .channel[6] = IIO_CHAN(IIO_IN, 0, 1, 0, NULL, 6, 0,
622                                        (1 << IIO_CHAN_INFO_SCALE_SHARED),
623                                        6, 6, IIO_ST('u', 12, 16, 0), 0),
624                 .channel[7] = IIO_CHAN(IIO_IN, 0, 1, 0, NULL, 7, 0,
625                                           (1 << IIO_CHAN_INFO_SCALE_SHARED),
626                                           7, 7, IIO_ST('u', 12, 16, 0), 0),
627                 .channel[8] = IIO_CHAN_SOFT_TIMESTAMP(8),
628                 .num_channels = 9,
629                 .int_vref_mv = 4096,
630                 .monitor_mode = true,
631                 .default_config = AD7998_ALERT_EN,
632                 .event_attrs = &ad7993_4_7_8_event_attrs_group,
633         },
634 };
635
636 static int __devinit ad799x_probe(struct i2c_client *client,
637                                    const struct i2c_device_id *id)
638 {
639         int ret, regdone = 0;
640         struct ad799x_platform_data *pdata = client->dev.platform_data;
641         struct ad799x_state *st;
642         struct iio_dev *indio_dev = iio_allocate_device(sizeof(*st));
643
644         if (indio_dev == NULL)
645                 return -ENOMEM;
646
647         st = iio_priv(indio_dev);
648         /* this is only used for device removal purposes */
649         i2c_set_clientdata(client, indio_dev);
650
651         st->id = id->driver_data;
652         st->chip_info = &ad799x_chip_info_tbl[st->id];
653         st->config = st->chip_info->default_config;
654
655         /* TODO: Add pdata options for filtering and bit delay */
656
657         if (pdata)
658                 st->int_vref_mv = pdata->vref_mv;
659         else
660                 st->int_vref_mv = st->chip_info->int_vref_mv;
661
662         st->reg = regulator_get(&client->dev, "vcc");
663         if (!IS_ERR(st->reg)) {
664                 ret = regulator_enable(st->reg);
665                 if (ret)
666                         goto error_put_reg;
667         }
668         st->client = client;
669
670         indio_dev->dev.parent = &client->dev;
671         indio_dev->name = id->name;
672         indio_dev->event_attrs = st->chip_info->event_attrs;
673         indio_dev->name = id->name;
674         indio_dev->dev_data = (void *)(st);
675         indio_dev->driver_module = THIS_MODULE;
676         indio_dev->modes = INDIO_DIRECT_MODE;
677         indio_dev->num_interrupt_lines = 1;
678         indio_dev->channels = st->chip_info->channel;
679         indio_dev->num_channels = st->chip_info->num_channels;
680         indio_dev->read_raw = &ad799x_read_raw;
681
682         ret = ad799x_register_ring_funcs_and_init(indio_dev);
683         if (ret)
684                 goto error_disable_reg;
685
686         ret = iio_device_register(indio_dev);
687         if (ret)
688                 goto error_cleanup_ring;
689         regdone = 1;
690
691         ret = iio_ring_buffer_register_ex(indio_dev->ring, 0,
692                                           indio_dev->channels,
693                                           indio_dev->num_channels);
694         if (ret)
695                 goto error_cleanup_ring;
696
697         if (client->irq > 0 && st->chip_info->monitor_mode) {
698                 ret = request_threaded_irq(client->irq,
699                                            NULL,
700                                            ad799x_event_handler,
701                                            IRQF_TRIGGER_FALLING |
702                                            IRQF_ONESHOT,
703                                            client->name,
704                                            indio_dev);
705                 if (ret)
706                         goto error_cleanup_ring;
707         }
708
709         return 0;
710
711 error_cleanup_ring:
712         ad799x_ring_cleanup(indio_dev);
713 error_disable_reg:
714         if (!IS_ERR(st->reg))
715                 regulator_disable(st->reg);
716 error_put_reg:
717         if (!IS_ERR(st->reg))
718                 regulator_put(st->reg);
719         if (regdone)
720                 iio_device_unregister(indio_dev);
721         else
722                 iio_free_device(indio_dev);
723
724         return ret;
725 }
726
727 static __devexit int ad799x_remove(struct i2c_client *client)
728 {
729         struct iio_dev *indio_dev = i2c_get_clientdata(client);
730         struct ad799x_state *st = iio_priv(indio_dev);
731
732         if (client->irq > 0 && st->chip_info->monitor_mode)
733                 free_irq(client->irq, indio_dev);
734
735         iio_ring_buffer_unregister(indio_dev->ring);
736         ad799x_ring_cleanup(indio_dev);
737         if (!IS_ERR(st->reg)) {
738                 regulator_disable(st->reg);
739                 regulator_put(st->reg);
740         }
741         iio_device_unregister(indio_dev);
742
743         return 0;
744 }
745
746 static const struct i2c_device_id ad799x_id[] = {
747         { "ad7991", ad7991 },
748         { "ad7995", ad7995 },
749         { "ad7999", ad7999 },
750         { "ad7992", ad7992 },
751         { "ad7993", ad7993 },
752         { "ad7994", ad7994 },
753         { "ad7997", ad7997 },
754         { "ad7998", ad7998 },
755         {}
756 };
757
758 MODULE_DEVICE_TABLE(i2c, ad799x_id);
759
760 static struct i2c_driver ad799x_driver = {
761         .driver = {
762                 .name = "ad799x",
763         },
764         .probe = ad799x_probe,
765         .remove = __devexit_p(ad799x_remove),
766         .id_table = ad799x_id,
767 };
768
769 static __init int ad799x_init(void)
770 {
771         return i2c_add_driver(&ad799x_driver);
772 }
773
774 static __exit void ad799x_exit(void)
775 {
776         i2c_del_driver(&ad799x_driver);
777 }
778
779 MODULE_AUTHOR("Michael Hennerich <hennerich@blackfin.uclinux.org>");
780 MODULE_DESCRIPTION("Analog Devices AD799x ADC");
781 MODULE_LICENSE("GPL v2");
782 MODULE_ALIAS("i2c:ad799x");
783
784 module_init(ad799x_init);
785 module_exit(ad799x_exit);