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