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