Linux Kernel  3.7.1
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
ad799x_core.c
Go to the documentation of this file.
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>
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:
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] = {
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) ==
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) ==
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 ?
365  (i >> 1),
369  (i >> 1),
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,
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,
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,
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,
402 
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 = IIO_CHAN_INFO_RAW_SEPARATE_BIT,
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 = IIO_CHAN_INFO_RAW_SEPARATE_BIT,
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 = IIO_CHAN_INFO_RAW_SEPARATE_BIT,
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 = IIO_CHAN_INFO_RAW_SEPARATE_BIT,
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  .int_vref_mv = 4096,
502  .info = &ad7991_info,
503  },
504  [ad7995] = {
505  .channel = {
506  [0] = {
507  .type = IIO_VOLTAGE,
508  .indexed = 1,
509  .channel = 0,
510  .info_mask = IIO_CHAN_INFO_RAW_SEPARATE_BIT,
511  .scan_index = 0,
512  .scan_type = IIO_ST('u', 10, 16, 2),
513  },
514  [1] = {
515  .type = IIO_VOLTAGE,
516  .indexed = 1,
517  .channel = 1,
518  .info_mask = IIO_CHAN_INFO_RAW_SEPARATE_BIT,
519  .scan_index = 1,
520  .scan_type = IIO_ST('u', 10, 16, 2),
521  },
522  [2] = {
523  .type = IIO_VOLTAGE,
524  .indexed = 1,
525  .channel = 2,
526  .info_mask = IIO_CHAN_INFO_RAW_SEPARATE_BIT,
527  .scan_index = 2,
528  .scan_type = IIO_ST('u', 10, 16, 2),
529  },
530  [3] = {
531  .type = IIO_VOLTAGE,
532  .indexed = 1,
533  .channel = 3,
534  .info_mask = IIO_CHAN_INFO_RAW_SEPARATE_BIT,
535  .scan_index = 3,
536  .scan_type = IIO_ST('u', 10, 16, 2),
537  },
538  [4] = IIO_CHAN_SOFT_TIMESTAMP(4),
539  },
540  .num_channels = 5,
541  .int_vref_mv = 1024,
542  .info = &ad7991_info,
543  },
544  [ad7999] = {
545  .channel = {
546  [0] = {
547  .type = IIO_VOLTAGE,
548  .indexed = 1,
549  .channel = 0,
550  .info_mask = IIO_CHAN_INFO_RAW_SEPARATE_BIT,
551  .scan_index = 0,
552  .scan_type = IIO_ST('u', 8, 16, 4),
553  },
554  [1] = {
555  .type = IIO_VOLTAGE,
556  .indexed = 1,
557  .channel = 1,
558  .info_mask = IIO_CHAN_INFO_RAW_SEPARATE_BIT,
559  .scan_index = 1,
560  .scan_type = IIO_ST('u', 8, 16, 4),
561  },
562  [2] = {
563  .type = IIO_VOLTAGE,
564  .indexed = 1,
565  .channel = 2,
566  .info_mask = IIO_CHAN_INFO_RAW_SEPARATE_BIT,
567  .scan_index = 2,
568  .scan_type = IIO_ST('u', 8, 16, 4),
569  },
570  [3] = {
571  .type = IIO_VOLTAGE,
572  .indexed = 1,
573  .channel = 3,
574  .info_mask = IIO_CHAN_INFO_RAW_SEPARATE_BIT,
575  .scan_index = 3,
576  .scan_type = IIO_ST('u', 8, 16, 4),
577  },
578  [4] = IIO_CHAN_SOFT_TIMESTAMP(4),
579  },
580  .num_channels = 5,
581  .int_vref_mv = 1024,
582  .info = &ad7991_info,
583  },
584  [ad7992] = {
585  .channel = {
586  [0] = {
587  .type = IIO_VOLTAGE,
588  .indexed = 1,
589  .channel = 0,
590  .info_mask = IIO_CHAN_INFO_RAW_SEPARATE_BIT,
591  .scan_index = 0,
592  .scan_type = IIO_ST('u', 12, 16, 0),
593  .event_mask = AD799X_EV_MASK,
594  },
595  [1] = {
596  .type = IIO_VOLTAGE,
597  .indexed = 1,
598  .channel = 1,
599  .info_mask = IIO_CHAN_INFO_RAW_SEPARATE_BIT,
600  .scan_index = 1,
601  .scan_type = IIO_ST('u', 12, 16, 0),
602  .event_mask = AD799X_EV_MASK,
603  },
604  [2] = IIO_CHAN_SOFT_TIMESTAMP(2),
605  },
606  .num_channels = 3,
607  .int_vref_mv = 4096,
608  .default_config = AD7998_ALERT_EN,
609  .info = &ad7992_info,
610  },
611  [ad7993] = {
612  .channel = {
613  [0] = {
614  .type = IIO_VOLTAGE,
615  .indexed = 1,
616  .channel = 0,
617  .info_mask = IIO_CHAN_INFO_RAW_SEPARATE_BIT,
618  .scan_index = 0,
619  .scan_type = IIO_ST('u', 10, 16, 2),
620  .event_mask = AD799X_EV_MASK,
621  },
622  [1] = {
623  .type = IIO_VOLTAGE,
624  .indexed = 1,
625  .channel = 1,
626  .scan_index = 1,
627  .info_mask = IIO_CHAN_INFO_RAW_SEPARATE_BIT,
628  .scan_type = IIO_ST('u', 10, 16, 2),
629  .event_mask = AD799X_EV_MASK,
630  },
631  [2] = {
632  .type = IIO_VOLTAGE,
633  .indexed = 1,
634  .channel = 2,
635  .info_mask = IIO_CHAN_INFO_RAW_SEPARATE_BIT,
636  .scan_index = 2,
637  .scan_type = IIO_ST('u', 10, 16, 2),
638  .event_mask = AD799X_EV_MASK,
639  },
640  [3] = {
641  .type = IIO_VOLTAGE,
642  .indexed = 1,
643  .channel = 3,
644  .info_mask = IIO_CHAN_INFO_RAW_SEPARATE_BIT,
645  .scan_index = 3,
646  .scan_type = IIO_ST('u', 10, 16, 2),
647  .event_mask = AD799X_EV_MASK,
648  },
649  [4] = IIO_CHAN_SOFT_TIMESTAMP(4),
650  },
651  .num_channels = 5,
652  .int_vref_mv = 1024,
653  .default_config = AD7998_ALERT_EN,
654  .info = &ad7993_4_7_8_info,
655  },
656  [ad7994] = {
657  .channel = {
658  [0] = {
659  .type = IIO_VOLTAGE,
660  .indexed = 1,
661  .channel = 0,
662  .info_mask = IIO_CHAN_INFO_RAW_SEPARATE_BIT,
663  .scan_index = 0,
664  .scan_type = IIO_ST('u', 12, 16, 0),
665  .event_mask = AD799X_EV_MASK,
666  },
667  [1] = {
668  .type = IIO_VOLTAGE,
669  .indexed = 1,
670  .channel = 1,
671  .info_mask = IIO_CHAN_INFO_RAW_SEPARATE_BIT,
672  .scan_index = 1,
673  .scan_type = IIO_ST('u', 12, 16, 0),
674  .event_mask = AD799X_EV_MASK,
675  },
676  [2] = {
677  .type = IIO_VOLTAGE,
678  .indexed = 1,
679  .channel = 2,
680  .info_mask = IIO_CHAN_INFO_RAW_SEPARATE_BIT,
681  .scan_index = 2,
682  .scan_type = IIO_ST('u', 12, 16, 0),
683  .event_mask = AD799X_EV_MASK,
684  },
685  [3] = {
686  .type = IIO_VOLTAGE,
687  .indexed = 1,
688  .channel = 3,
689  .info_mask = IIO_CHAN_INFO_RAW_SEPARATE_BIT,
690  .scan_index = 3,
691  .scan_type = IIO_ST('u', 12, 16, 0),
692  .event_mask = AD799X_EV_MASK,
693  },
694  [4] = IIO_CHAN_SOFT_TIMESTAMP(4),
695  },
696  .num_channels = 5,
697  .int_vref_mv = 4096,
698  .default_config = AD7998_ALERT_EN,
699  .info = &ad7993_4_7_8_info,
700  },
701  [ad7997] = {
702  .channel = {
703  [0] = {
704  .type = IIO_VOLTAGE,
705  .indexed = 1,
706  .channel = 0,
707  .info_mask = IIO_CHAN_INFO_RAW_SEPARATE_BIT,
708  .scan_index = 0,
709  .scan_type = IIO_ST('u', 10, 16, 2),
710  .event_mask = AD799X_EV_MASK,
711  },
712  [1] = {
713  .type = IIO_VOLTAGE,
714  .indexed = 1,
715  .channel = 1,
716  .info_mask = IIO_CHAN_INFO_RAW_SEPARATE_BIT,
717  .scan_index = 1,
718  .scan_type = IIO_ST('u', 10, 16, 2),
719  .event_mask = AD799X_EV_MASK,
720  },
721  [2] = {
722  .type = IIO_VOLTAGE,
723  .indexed = 1,
724  .channel = 2,
725  .info_mask = IIO_CHAN_INFO_RAW_SEPARATE_BIT,
726  .scan_index = 2,
727  .scan_type = IIO_ST('u', 10, 16, 2),
728  .event_mask = AD799X_EV_MASK,
729  },
730  [3] = {
731  .type = IIO_VOLTAGE,
732  .indexed = 1,
733  .channel = 3,
734  .info_mask = IIO_CHAN_INFO_RAW_SEPARATE_BIT,
735  .scan_index = 3,
736  .scan_type = IIO_ST('u', 10, 16, 2),
737  .event_mask = AD799X_EV_MASK,
738  },
739  [4] = {
740  .type = IIO_VOLTAGE,
741  .indexed = 1,
742  .channel = 4,
743  .info_mask = IIO_CHAN_INFO_RAW_SEPARATE_BIT,
744  .scan_index = 4,
745  .scan_type = IIO_ST('u', 10, 16, 2),
746  },
747  [5] = {
748  .type = IIO_VOLTAGE,
749  .indexed = 1,
750  .channel = 5,
751  .info_mask = IIO_CHAN_INFO_RAW_SEPARATE_BIT,
752  .scan_index = 5,
753  .scan_type = IIO_ST('u', 10, 16, 2),
754  },
755  [6] = {
756  .type = IIO_VOLTAGE,
757  .indexed = 1,
758  .channel = 6,
759  .info_mask = IIO_CHAN_INFO_RAW_SEPARATE_BIT,
760  .scan_index = 6,
761  .scan_type = IIO_ST('u', 10, 16, 2),
762  },
763  [7] = {
764  .type = IIO_VOLTAGE,
765  .indexed = 1,
766  .channel = 7,
767  .info_mask = IIO_CHAN_INFO_RAW_SEPARATE_BIT,
768  .scan_index = 7,
769  .scan_type = IIO_ST('u', 10, 16, 2),
770  },
771  [8] = IIO_CHAN_SOFT_TIMESTAMP(8),
772  },
773  .num_channels = 9,
774  .int_vref_mv = 1024,
775  .default_config = AD7998_ALERT_EN,
776  .info = &ad7993_4_7_8_info,
777  },
778  [ad7998] = {
779  .channel = {
780  [0] = {
781  .type = IIO_VOLTAGE,
782  .indexed = 1,
783  .channel = 0,
784  .info_mask = IIO_CHAN_INFO_RAW_SEPARATE_BIT,
785  .scan_index = 0,
786  .scan_type = IIO_ST('u', 12, 16, 0),
787  .event_mask = AD799X_EV_MASK,
788  },
789  [1] = {
790  .type = IIO_VOLTAGE,
791  .indexed = 1,
792  .channel = 1,
793  .info_mask = IIO_CHAN_INFO_RAW_SEPARATE_BIT,
794  .scan_index = 1,
795  .scan_type = IIO_ST('u', 12, 16, 0),
796  .event_mask = AD799X_EV_MASK,
797  },
798  [2] = {
799  .type = IIO_VOLTAGE,
800  .indexed = 1,
801  .channel = 2,
802  .info_mask = IIO_CHAN_INFO_RAW_SEPARATE_BIT,
803  .scan_index = 2,
804  .scan_type = IIO_ST('u', 12, 16, 0),
805  .event_mask = AD799X_EV_MASK,
806  },
807  [3] = {
808  .type = IIO_VOLTAGE,
809  .indexed = 1,
810  .channel = 3,
811  .info_mask = IIO_CHAN_INFO_RAW_SEPARATE_BIT,
812  .scan_index = 3,
813  .scan_type = IIO_ST('u', 12, 16, 0),
814  .event_mask = AD799X_EV_MASK,
815  },
816  [4] = {
817  .type = IIO_VOLTAGE,
818  .indexed = 1,
819  .channel = 4,
820  .info_mask = IIO_CHAN_INFO_RAW_SEPARATE_BIT,
821  .scan_index = 4,
822  .scan_type = IIO_ST('u', 12, 16, 0),
823  },
824  [5] = {
825  .type = IIO_VOLTAGE,
826  .indexed = 1,
827  .channel = 5,
828  .info_mask = IIO_CHAN_INFO_RAW_SEPARATE_BIT,
829  .scan_index = 5,
830  .scan_type = IIO_ST('u', 12, 16, 0),
831  },
832  [6] = {
833  .type = IIO_VOLTAGE,
834  .indexed = 1,
835  .channel = 6,
836  .info_mask = IIO_CHAN_INFO_RAW_SEPARATE_BIT,
837  .scan_index = 6,
838  .scan_type = IIO_ST('u', 12, 16, 0),
839  },
840  [7] = {
841  .type = IIO_VOLTAGE,
842  .indexed = 1,
843  .channel = 7,
844  .info_mask = IIO_CHAN_INFO_RAW_SEPARATE_BIT,
845  .scan_index = 7,
846  .scan_type = IIO_ST('u', 12, 16, 0),
847  },
848  [8] = IIO_CHAN_SOFT_TIMESTAMP(8),
849  },
850  .num_channels = 9,
851  .int_vref_mv = 4096,
852  .default_config = AD7998_ALERT_EN,
853  .info = &ad7993_4_7_8_info,
854  },
855 };
856 
857 static int __devinit ad799x_probe(struct i2c_client *client,
858  const struct i2c_device_id *id)
859 {
860  int ret;
861  struct ad799x_platform_data *pdata = client->dev.platform_data;
862  struct ad799x_state *st;
863  struct iio_dev *indio_dev = iio_device_alloc(sizeof(*st));
864 
865  if (indio_dev == NULL)
866  return -ENOMEM;
867 
868  st = iio_priv(indio_dev);
869  /* this is only used for device removal purposes */
870  i2c_set_clientdata(client, indio_dev);
871 
872  st->id = id->driver_data;
873  st->chip_info = &ad799x_chip_info_tbl[st->id];
874  st->config = st->chip_info->default_config;
875 
876  /* TODO: Add pdata options for filtering and bit delay */
877 
878  if (pdata)
879  st->int_vref_mv = pdata->vref_mv;
880  else
881  st->int_vref_mv = st->chip_info->int_vref_mv;
882 
883  st->reg = regulator_get(&client->dev, "vcc");
884  if (!IS_ERR(st->reg)) {
885  ret = regulator_enable(st->reg);
886  if (ret)
887  goto error_put_reg;
888  }
889  st->client = client;
890 
891  indio_dev->dev.parent = &client->dev;
892  indio_dev->name = id->name;
893  indio_dev->info = st->chip_info->info;
894 
895  indio_dev->modes = INDIO_DIRECT_MODE;
896  indio_dev->channels = st->chip_info->channel;
897  indio_dev->num_channels = st->chip_info->num_channels;
898 
899  ret = ad799x_register_ring_funcs_and_init(indio_dev);
900  if (ret)
901  goto error_disable_reg;
902 
903  if (client->irq > 0) {
904  ret = request_threaded_irq(client->irq,
905  NULL,
906  ad799x_event_handler,
908  IRQF_ONESHOT,
909  client->name,
910  indio_dev);
911  if (ret)
912  goto error_cleanup_ring;
913  }
914  ret = iio_device_register(indio_dev);
915  if (ret)
916  goto error_free_irq;
917 
918  return 0;
919 
920 error_free_irq:
921  free_irq(client->irq, indio_dev);
922 error_cleanup_ring:
923  ad799x_ring_cleanup(indio_dev);
924 error_disable_reg:
925  if (!IS_ERR(st->reg))
926  regulator_disable(st->reg);
927 error_put_reg:
928  if (!IS_ERR(st->reg))
929  regulator_put(st->reg);
930  iio_device_free(indio_dev);
931 
932  return ret;
933 }
934 
935 static __devexit int ad799x_remove(struct i2c_client *client)
936 {
937  struct iio_dev *indio_dev = i2c_get_clientdata(client);
938  struct ad799x_state *st = iio_priv(indio_dev);
939 
940  iio_device_unregister(indio_dev);
941  if (client->irq > 0)
942  free_irq(client->irq, indio_dev);
943 
944  ad799x_ring_cleanup(indio_dev);
945  if (!IS_ERR(st->reg)) {
946  regulator_disable(st->reg);
947  regulator_put(st->reg);
948  }
949  iio_device_free(indio_dev);
950 
951  return 0;
952 }
953 
954 static const struct i2c_device_id ad799x_id[] = {
955  { "ad7991", ad7991 },
956  { "ad7995", ad7995 },
957  { "ad7999", ad7999 },
958  { "ad7992", ad7992 },
959  { "ad7993", ad7993 },
960  { "ad7994", ad7994 },
961  { "ad7997", ad7997 },
962  { "ad7998", ad7998 },
963  {}
964 };
965 
966 MODULE_DEVICE_TABLE(i2c, ad799x_id);
967 
968 static struct i2c_driver ad799x_driver = {
969  .driver = {
970  .name = "ad799x",
971  },
972  .probe = ad799x_probe,
973  .remove = __devexit_p(ad799x_remove),
974  .id_table = ad799x_id,
975 };
976 module_i2c_driver(ad799x_driver);
977 
978 MODULE_AUTHOR("Michael Hennerich <[email protected]>");
979 MODULE_DESCRIPTION("Analog Devices AD799x ADC");
980 MODULE_LICENSE("GPL v2");