3 * Copyright (C) 2010 Michael Hennerich, Analog Devices Inc.
5 * based on iio/adc/max1363
6 * Copyright (C) 2008-2010 Jonathan Cameron
8 * based on linux/drivers/i2c/chips/max123x
9 * Copyright (C) 2002-2004 Stefan Eletzhofer
11 * based on linux/drivers/acron/char/pcf8583.c
12 * Copyright (C) 2000 Russell King
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.
20 * Support for ad7991, ad7995, ad7999, ad7992, ad7993, ad7994, ad7997,
21 * ad7998 and similar chips.
25 #include <linux/interrupt.h>
26 #include <linux/workqueue.h>
27 #include <linux/device.h>
28 #include <linux/kernel.h>
29 #include <linux/sysfs.h>
30 #include <linux/list.h>
31 #include <linux/i2c.h>
32 #include <linux/regulator/consumer.h>
33 #include <linux/slab.h>
34 #include <linux/types.h>
35 #include <linux/err.h>
40 #include "../ring_generic.h"
45 * ad799x register access by I2C
47 static int ad799x_i2c_read16(struct ad799x_state *st, u8 reg, u16 *data)
49 struct i2c_client *client = st->client;
52 ret = i2c_smbus_read_word_data(client, reg);
54 dev_err(&client->dev, "I2C read error\n");
58 *data = swab16((u16)ret);
63 static int ad799x_i2c_read8(struct ad799x_state *st, u8 reg, u8 *data)
65 struct i2c_client *client = st->client;
68 ret = i2c_smbus_read_byte_data(client, reg);
70 dev_err(&client->dev, "I2C read error\n");
79 static int ad799x_i2c_write16(struct ad799x_state *st, u8 reg, u16 data)
81 struct i2c_client *client = st->client;
84 ret = i2c_smbus_write_word_data(client, reg, swab16(data));
86 dev_err(&client->dev, "I2C write error\n");
91 static int ad799x_i2c_write8(struct ad799x_state *st, u8 reg, u8 data)
93 struct i2c_client *client = st->client;
96 ret = i2c_smbus_write_byte_data(client, reg, data);
98 dev_err(&client->dev, "I2C write error\n");
103 static int ad799x_scan_el_set_state(struct iio_scan_el *scan_el,
104 struct iio_dev *indio_dev,
107 struct ad799x_state *st = indio_dev->dev_data;
108 return ad799x_set_scan_mode(st, st->indio_dev->ring->scan_mask);
111 /* Here we claim all are 16 bits. This currently does no harm and saves
112 * us a lot of scan element listings */
114 #define AD799X_SCAN_EL(number) \
115 IIO_SCAN_EL_C(in##number, number, 0, ad799x_scan_el_set_state);
117 static AD799X_SCAN_EL(0);
118 static AD799X_SCAN_EL(1);
119 static AD799X_SCAN_EL(2);
120 static AD799X_SCAN_EL(3);
121 static AD799X_SCAN_EL(4);
122 static AD799X_SCAN_EL(5);
123 static AD799X_SCAN_EL(6);
124 static AD799X_SCAN_EL(7);
126 static IIO_SCAN_EL_TIMESTAMP(8);
127 static IIO_CONST_ATTR_SCAN_EL_TYPE(timestamp, s, 64, 64)
129 static ssize_t ad799x_show_type(struct device *dev,
130 struct device_attribute *attr,
133 struct iio_ring_buffer *ring = dev_get_drvdata(dev);
134 struct iio_dev *indio_dev = ring->indio_dev;
135 struct ad799x_state *st = indio_dev->dev_data;
137 return sprintf(buf, "%c%d/%d\n", st->chip_info->sign,
138 st->chip_info->bits, AD799X_STORAGEBITS);
140 static IIO_DEVICE_ATTR(in_type, S_IRUGO, ad799x_show_type, NULL, 0);
142 static int ad7991_5_9_set_scan_mode(struct ad799x_state *st, unsigned mask)
144 return i2c_smbus_write_byte(st->client,
145 st->config | (mask << AD799X_CHANNEL_SHIFT));
148 static int ad7992_3_4_set_scan_mode(struct ad799x_state *st, unsigned mask)
150 return ad799x_i2c_write8(st, AD7998_CONF_REG,
151 st->config | (mask << AD799X_CHANNEL_SHIFT));
154 static int ad7997_8_set_scan_mode(struct ad799x_state *st, unsigned mask)
156 return ad799x_i2c_write16(st, AD7998_CONF_REG,
157 st->config | (mask << AD799X_CHANNEL_SHIFT));
160 int ad799x_set_scan_mode(struct ad799x_state *st, unsigned mask)
164 if (st->chip_info->ad799x_set_scan_mode != NULL) {
165 ret = st->chip_info->ad799x_set_scan_mode(st, mask);
166 return (ret > 0) ? 0 : ret;
172 static ssize_t ad799x_read_single_channel(struct device *dev,
173 struct device_attribute *attr,
176 struct iio_dev *dev_info = dev_get_drvdata(dev);
177 struct ad799x_state *st = iio_dev_get_devdata(dev_info);
178 struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
179 int ret = 0, len = 0;
185 mutex_lock(&dev_info->mlock);
186 mask = 1 << this_attr->address;
187 /* If ring buffer capture is occuring, query the buffer */
188 if (iio_ring_enabled(dev_info)) {
189 data = ret = ad799x_single_channel_from_ring(st, mask);
198 cmd = st->config | (mask << AD799X_CHANNEL_SHIFT);
203 cmd = mask << AD799X_CHANNEL_SHIFT;
207 cmd = (this_attr->address <<
208 AD799X_CHANNEL_SHIFT) | AD7997_8_READ_SINGLE;
214 ret = ad799x_i2c_read16(st, cmd, rxbuf);
221 /* Pretty print the result */
222 len = sprintf(buf, "%u\n", data & ((1 << (st->chip_info->bits)) - 1));
225 mutex_unlock(&dev_info->mlock);
226 return ret ? ret : len;
229 static ssize_t ad799x_read_frequency(struct device *dev,
230 struct device_attribute *attr,
233 struct iio_dev *dev_info = dev_get_drvdata(dev);
234 struct ad799x_state *st = iio_dev_get_devdata(dev_info);
238 ret = ad799x_i2c_read8(st, AD7998_CYCLE_TMR_REG, &val);
242 val &= AD7998_CYC_MASK;
246 len = sprintf(buf, "0\n");
248 case AD7998_CYC_TCONF_32:
249 len = sprintf(buf, "15625\n");
251 case AD7998_CYC_TCONF_64:
252 len = sprintf(buf, "7812\n");
254 case AD7998_CYC_TCONF_128:
255 len = sprintf(buf, "3906\n");
257 case AD7998_CYC_TCONF_256:
258 len = sprintf(buf, "1953\n");
260 case AD7998_CYC_TCONF_512:
261 len = sprintf(buf, "976\n");
263 case AD7998_CYC_TCONF_1024:
264 len = sprintf(buf, "488\n");
266 case AD7998_CYC_TCONF_2048:
267 len = sprintf(buf, "244\n");
273 static ssize_t ad799x_write_frequency(struct device *dev,
274 struct device_attribute *attr,
278 struct iio_dev *dev_info = dev_get_drvdata(dev);
279 struct ad799x_state *st = iio_dev_get_devdata(dev_info);
285 ret = strict_strtol(buf, 10, &val);
289 mutex_lock(&dev_info->mlock);
290 ret = ad799x_i2c_read8(st, AD7998_CYCLE_TMR_REG, &t);
292 goto error_ret_mutex;
293 /* Wipe the bits clean */
294 t &= ~AD7998_CYC_MASK;
298 t |= AD7998_CYC_TCONF_32;
301 t |= AD7998_CYC_TCONF_64;
304 t |= AD7998_CYC_TCONF_128;
307 t |= AD7998_CYC_TCONF_256;
310 t |= AD7998_CYC_TCONF_512;
313 t |= AD7998_CYC_TCONF_1024;
316 t |= AD7998_CYC_TCONF_2048;
323 goto error_ret_mutex;
326 ret = ad799x_i2c_write8(st, AD7998_CYCLE_TMR_REG, t);
329 mutex_unlock(&dev_info->mlock);
331 return ret ? ret : len;
335 static ssize_t ad799x_read_channel_config(struct device *dev,
336 struct device_attribute *attr,
339 struct iio_dev *dev_info = dev_get_drvdata(dev);
340 struct ad799x_state *st = iio_dev_get_devdata(dev_info);
341 struct iio_event_attr *this_attr = to_iio_event_attr(attr);
345 ret = ad799x_i2c_read16(st, this_attr->mask, &val);
349 return sprintf(buf, "%d\n", val);
352 static ssize_t ad799x_write_channel_config(struct device *dev,
353 struct device_attribute *attr,
357 struct iio_dev *dev_info = dev_get_drvdata(dev);
358 struct ad799x_state *st = iio_dev_get_devdata(dev_info);
359 struct iio_event_attr *this_attr = to_iio_event_attr(attr);
364 ret = strict_strtol(buf, 10, &val);
368 mutex_lock(&dev_info->mlock);
369 ret = ad799x_i2c_write16(st, this_attr->mask, val);
370 mutex_unlock(&dev_info->mlock);
372 return ret ? ret : len;
375 static void ad799x_interrupt_bh(struct work_struct *work_s)
377 struct ad799x_state *st = container_of(work_s,
378 struct ad799x_state, work_thresh);
382 if (ad799x_i2c_read8(st, AD7998_ALERT_STAT_REG, &status))
388 ad799x_i2c_write8(st, AD7998_ALERT_STAT_REG, AD7998_ALERT_STAT_CLEAR);
390 for (i = 0; i < 8; i++) {
391 if (status & (1 << i))
392 iio_push_event(st->indio_dev, 0,
394 IIO_EVENT_CODE_IN_HIGH_THRESH(i >> 1) :
395 IIO_EVENT_CODE_IN_LOW_THRESH(i >> 1),
400 enable_irq(st->client->irq);
403 static int ad799x_interrupt(struct iio_dev *dev_info,
408 struct ad799x_state *st = dev_info->dev_data;
410 st->last_timestamp = timestamp;
411 schedule_work(&st->work_thresh);
415 IIO_EVENT_SH(ad799x, &ad799x_interrupt);
417 /* Direct read attribtues */
418 static IIO_DEV_ATTR_IN_RAW(0, ad799x_read_single_channel, 0);
419 static IIO_DEV_ATTR_IN_RAW(1, ad799x_read_single_channel, 1);
420 static IIO_DEV_ATTR_IN_RAW(2, ad799x_read_single_channel, 2);
421 static IIO_DEV_ATTR_IN_RAW(3, ad799x_read_single_channel, 3);
422 static IIO_DEV_ATTR_IN_RAW(4, ad799x_read_single_channel, 4);
423 static IIO_DEV_ATTR_IN_RAW(5, ad799x_read_single_channel, 5);
424 static IIO_DEV_ATTR_IN_RAW(6, ad799x_read_single_channel, 6);
425 static IIO_DEV_ATTR_IN_RAW(7, ad799x_read_single_channel, 7);
427 static ssize_t ad799x_show_scale(struct device *dev,
428 struct device_attribute *attr,
431 /* Driver currently only support internal vref */
432 struct iio_dev *dev_info = dev_get_drvdata(dev);
433 struct ad799x_state *st = iio_dev_get_devdata(dev_info);
435 /* Corresponds to Vref / 2^(bits) */
436 unsigned int scale_uv = (st->int_vref_mv * 1000) >> st->chip_info->bits;
438 return sprintf(buf, "%d.%03d\n", scale_uv / 1000, scale_uv % 1000);
441 static IIO_DEVICE_ATTR(in_scale, S_IRUGO, ad799x_show_scale, NULL, 0);
443 static ssize_t ad799x_show_name(struct device *dev,
444 struct device_attribute *attr,
447 struct iio_dev *dev_info = dev_get_drvdata(dev);
448 struct ad799x_state *st = iio_dev_get_devdata(dev_info);
449 return sprintf(buf, "%s\n", st->client->name);
452 static IIO_DEVICE_ATTR(name, S_IRUGO, ad799x_show_name, NULL, 0);
454 static struct attribute *ad7991_5_9_3_4_device_attrs[] = {
455 &iio_dev_attr_in0_raw.dev_attr.attr,
456 &iio_dev_attr_in1_raw.dev_attr.attr,
457 &iio_dev_attr_in2_raw.dev_attr.attr,
458 &iio_dev_attr_in3_raw.dev_attr.attr,
459 &iio_dev_attr_name.dev_attr.attr,
460 &iio_dev_attr_in_scale.dev_attr.attr,
464 static struct attribute_group ad7991_5_9_3_4_dev_attr_group = {
465 .attrs = ad7991_5_9_3_4_device_attrs,
468 static struct attribute *ad7991_5_9_3_4_scan_el_attrs[] = {
469 &iio_scan_el_in0.dev_attr.attr,
470 &iio_const_attr_in0_index.dev_attr.attr,
471 &iio_scan_el_in1.dev_attr.attr,
472 &iio_const_attr_in1_index.dev_attr.attr,
473 &iio_scan_el_in2.dev_attr.attr,
474 &iio_const_attr_in2_index.dev_attr.attr,
475 &iio_scan_el_in3.dev_attr.attr,
476 &iio_const_attr_in3_index.dev_attr.attr,
477 &iio_const_attr_timestamp_index.dev_attr.attr,
478 &iio_scan_el_timestamp.dev_attr.attr,
479 &iio_const_attr_timestamp_type.dev_attr.attr,
480 &iio_dev_attr_in_type.dev_attr.attr,
484 static struct attribute_group ad7991_5_9_3_4_scan_el_group = {
485 .name = "scan_elements",
486 .attrs = ad7991_5_9_3_4_scan_el_attrs,
489 static struct attribute *ad7992_device_attrs[] = {
490 &iio_dev_attr_in0_raw.dev_attr.attr,
491 &iio_dev_attr_in1_raw.dev_attr.attr,
492 &iio_dev_attr_name.dev_attr.attr,
493 &iio_dev_attr_in_scale.dev_attr.attr,
497 static struct attribute_group ad7992_dev_attr_group = {
498 .attrs = ad7992_device_attrs,
501 static struct attribute *ad7992_scan_el_attrs[] = {
502 &iio_scan_el_in0.dev_attr.attr,
503 &iio_const_attr_in0_index.dev_attr.attr,
504 &iio_scan_el_in1.dev_attr.attr,
505 &iio_const_attr_in1_index.dev_attr.attr,
506 &iio_const_attr_timestamp_index.dev_attr.attr,
507 &iio_scan_el_timestamp.dev_attr.attr,
508 &iio_const_attr_timestamp_type.dev_attr.attr,
509 &iio_dev_attr_in_type.dev_attr.attr,
513 static struct attribute_group ad7992_scan_el_group = {
514 .name = "scan_elements",
515 .attrs = ad7992_scan_el_attrs,
518 static struct attribute *ad7997_8_device_attrs[] = {
519 &iio_dev_attr_in0_raw.dev_attr.attr,
520 &iio_dev_attr_in1_raw.dev_attr.attr,
521 &iio_dev_attr_in2_raw.dev_attr.attr,
522 &iio_dev_attr_in3_raw.dev_attr.attr,
523 &iio_dev_attr_in4_raw.dev_attr.attr,
524 &iio_dev_attr_in5_raw.dev_attr.attr,
525 &iio_dev_attr_in6_raw.dev_attr.attr,
526 &iio_dev_attr_in7_raw.dev_attr.attr,
527 &iio_dev_attr_name.dev_attr.attr,
528 &iio_dev_attr_in_scale.dev_attr.attr,
532 static struct attribute_group ad7997_8_dev_attr_group = {
533 .attrs = ad7997_8_device_attrs,
536 static struct attribute *ad7997_8_scan_el_attrs[] = {
537 &iio_scan_el_in0.dev_attr.attr,
538 &iio_const_attr_in0_index.dev_attr.attr,
539 &iio_scan_el_in1.dev_attr.attr,
540 &iio_const_attr_in1_index.dev_attr.attr,
541 &iio_scan_el_in2.dev_attr.attr,
542 &iio_const_attr_in2_index.dev_attr.attr,
543 &iio_scan_el_in3.dev_attr.attr,
544 &iio_const_attr_in3_index.dev_attr.attr,
545 &iio_scan_el_in4.dev_attr.attr,
546 &iio_const_attr_in4_index.dev_attr.attr,
547 &iio_scan_el_in5.dev_attr.attr,
548 &iio_const_attr_in5_index.dev_attr.attr,
549 &iio_scan_el_in6.dev_attr.attr,
550 &iio_const_attr_in6_index.dev_attr.attr,
551 &iio_scan_el_in7.dev_attr.attr,
552 &iio_const_attr_in7_index.dev_attr.attr,
553 &iio_const_attr_timestamp_index.dev_attr.attr,
554 &iio_scan_el_timestamp.dev_attr.attr,
555 &iio_const_attr_timestamp_type.dev_attr.attr,
556 &iio_dev_attr_in_type.dev_attr.attr,
560 static struct attribute_group ad7997_8_scan_el_group = {
561 .name = "scan_elements",
562 .attrs = ad7997_8_scan_el_attrs,
565 IIO_EVENT_ATTR_SH(in0_thresh_low_value,
567 ad799x_read_channel_config,
568 ad799x_write_channel_config,
569 AD7998_DATALOW_CH1_REG);
571 IIO_EVENT_ATTR_SH(in0_thresh_high_value,
573 ad799x_read_channel_config,
574 ad799x_write_channel_config,
575 AD7998_DATAHIGH_CH1_REG);
577 IIO_EVENT_ATTR_SH(in0_thresh_both_hyst_raw,
579 ad799x_read_channel_config,
580 ad799x_write_channel_config,
581 AD7998_HYST_CH1_REG);
583 IIO_EVENT_ATTR_SH(in1_thresh_low_value,
585 ad799x_read_channel_config,
586 ad799x_write_channel_config,
587 AD7998_DATALOW_CH2_REG);
589 IIO_EVENT_ATTR_SH(in1_thresh_high_value,
591 ad799x_read_channel_config,
592 ad799x_write_channel_config,
593 AD7998_DATAHIGH_CH2_REG);
595 IIO_EVENT_ATTR_SH(in1_thresh_both_hyst_raw,
597 ad799x_read_channel_config,
598 ad799x_write_channel_config,
599 AD7998_HYST_CH2_REG);
601 IIO_EVENT_ATTR_SH(in2_thresh_low_value,
603 ad799x_read_channel_config,
604 ad799x_write_channel_config,
605 AD7998_DATALOW_CH3_REG);
607 IIO_EVENT_ATTR_SH(in2_thresh_high_value,
609 ad799x_read_channel_config,
610 ad799x_write_channel_config,
611 AD7998_DATAHIGH_CH3_REG);
613 IIO_EVENT_ATTR_SH(in2_thresh_both_hyst_raw,
615 ad799x_read_channel_config,
616 ad799x_write_channel_config,
617 AD7998_HYST_CH3_REG);
619 IIO_EVENT_ATTR_SH(in3_thresh_low_value,
621 ad799x_read_channel_config,
622 ad799x_write_channel_config,
623 AD7998_DATALOW_CH4_REG);
625 IIO_EVENT_ATTR_SH(in3_thresh_high_value,
627 ad799x_read_channel_config,
628 ad799x_write_channel_config,
629 AD7998_DATAHIGH_CH4_REG);
631 IIO_EVENT_ATTR_SH(in3_thresh_both_hyst_raw,
633 ad799x_read_channel_config,
634 ad799x_write_channel_config,
635 AD7998_HYST_CH4_REG);
637 static IIO_DEV_ATTR_SAMP_FREQ(S_IWUSR | S_IRUGO,
638 ad799x_read_frequency,
639 ad799x_write_frequency);
640 static IIO_CONST_ATTR_SAMP_FREQ_AVAIL("15625 7812 3906 1953 976 488 244 0");
642 static struct attribute *ad7993_4_7_8_event_attributes[] = {
643 &iio_event_attr_in0_thresh_low_value.dev_attr.attr,
644 &iio_event_attr_in0_thresh_high_value.dev_attr.attr,
645 &iio_event_attr_in0_thresh_both_hyst_raw.dev_attr.attr,
646 &iio_event_attr_in1_thresh_low_value.dev_attr.attr,
647 &iio_event_attr_in1_thresh_high_value.dev_attr.attr,
648 &iio_event_attr_in1_thresh_both_hyst_raw.dev_attr.attr,
649 &iio_event_attr_in2_thresh_low_value.dev_attr.attr,
650 &iio_event_attr_in2_thresh_high_value.dev_attr.attr,
651 &iio_event_attr_in2_thresh_both_hyst_raw.dev_attr.attr,
652 &iio_event_attr_in3_thresh_low_value.dev_attr.attr,
653 &iio_event_attr_in3_thresh_high_value.dev_attr.attr,
654 &iio_event_attr_in3_thresh_both_hyst_raw.dev_attr.attr,
655 &iio_dev_attr_sampling_frequency.dev_attr.attr,
656 &iio_const_attr_sampling_frequency_available.dev_attr.attr,
660 static struct attribute_group ad7993_4_7_8_event_attrs_group = {
661 .attrs = ad7993_4_7_8_event_attributes,
664 static struct attribute *ad7992_event_attributes[] = {
665 &iio_event_attr_in0_thresh_low_value.dev_attr.attr,
666 &iio_event_attr_in0_thresh_high_value.dev_attr.attr,
667 &iio_event_attr_in0_thresh_both_hyst_raw.dev_attr.attr,
668 &iio_event_attr_in1_thresh_low_value.dev_attr.attr,
669 &iio_event_attr_in1_thresh_high_value.dev_attr.attr,
670 &iio_event_attr_in1_thresh_both_hyst_raw.dev_attr.attr,
671 &iio_dev_attr_sampling_frequency.dev_attr.attr,
672 &iio_const_attr_sampling_frequency_available.dev_attr.attr,
676 static struct attribute_group ad7992_event_attrs_group = {
677 .attrs = ad7992_event_attributes,
680 static const struct ad799x_chip_info ad799x_chip_info_tbl[] = {
684 .sign = IIO_SCAN_EL_TYPE_UNSIGNED,
686 .dev_attrs = &ad7991_5_9_3_4_dev_attr_group,
687 .scan_attrs = &ad7991_5_9_3_4_scan_el_group,
688 .ad799x_set_scan_mode = ad7991_5_9_set_scan_mode,
693 .sign = IIO_SCAN_EL_TYPE_UNSIGNED,
695 .dev_attrs = &ad7991_5_9_3_4_dev_attr_group,
696 .scan_attrs = &ad7991_5_9_3_4_scan_el_group,
697 .ad799x_set_scan_mode = ad7991_5_9_set_scan_mode,
702 .sign = IIO_SCAN_EL_TYPE_UNSIGNED,
704 .dev_attrs = &ad7991_5_9_3_4_dev_attr_group,
705 .scan_attrs = &ad7991_5_9_3_4_scan_el_group,
706 .ad799x_set_scan_mode = ad7991_5_9_set_scan_mode,
711 .sign = IIO_SCAN_EL_TYPE_UNSIGNED,
713 .monitor_mode = true,
714 .default_config = AD7998_ALERT_EN,
715 .dev_attrs = &ad7992_dev_attr_group,
716 .scan_attrs = &ad7992_scan_el_group,
717 .event_attrs = &ad7992_event_attrs_group,
718 .ad799x_set_scan_mode = ad7992_3_4_set_scan_mode,
723 .sign = IIO_SCAN_EL_TYPE_UNSIGNED,
725 .monitor_mode = true,
726 .default_config = AD7998_ALERT_EN,
727 .dev_attrs = &ad7991_5_9_3_4_dev_attr_group,
728 .scan_attrs = &ad7991_5_9_3_4_scan_el_group,
729 .event_attrs = &ad7993_4_7_8_event_attrs_group,
730 .ad799x_set_scan_mode = ad7992_3_4_set_scan_mode,
735 .sign = IIO_SCAN_EL_TYPE_UNSIGNED,
737 .monitor_mode = true,
738 .default_config = AD7998_ALERT_EN,
739 .dev_attrs = &ad7991_5_9_3_4_dev_attr_group,
740 .scan_attrs = &ad7991_5_9_3_4_scan_el_group,
741 .event_attrs = &ad7993_4_7_8_event_attrs_group,
742 .ad799x_set_scan_mode = ad7992_3_4_set_scan_mode,
747 .sign = IIO_SCAN_EL_TYPE_UNSIGNED,
749 .monitor_mode = true,
750 .default_config = AD7998_ALERT_EN,
751 .dev_attrs = &ad7997_8_dev_attr_group,
752 .scan_attrs = &ad7997_8_scan_el_group,
753 .event_attrs = &ad7993_4_7_8_event_attrs_group,
754 .ad799x_set_scan_mode = ad7997_8_set_scan_mode,
759 .sign = IIO_SCAN_EL_TYPE_UNSIGNED,
761 .monitor_mode = true,
762 .default_config = AD7998_ALERT_EN,
763 .dev_attrs = &ad7997_8_dev_attr_group,
764 .scan_attrs = &ad7997_8_scan_el_group,
765 .event_attrs = &ad7993_4_7_8_event_attrs_group,
766 .ad799x_set_scan_mode = ad7997_8_set_scan_mode,
770 static int __devinit ad799x_probe(struct i2c_client *client,
771 const struct i2c_device_id *id)
773 int ret, regdone = 0;
774 struct ad799x_platform_data *pdata = client->dev.platform_data;
775 struct ad799x_state *st = kzalloc(sizeof(*st), GFP_KERNEL);
781 /* this is only used for device removal purposes */
782 i2c_set_clientdata(client, st);
784 atomic_set(&st->protect_ring, 0);
785 st->id = id->driver_data;
786 st->chip_info = &ad799x_chip_info_tbl[st->id];
787 st->config = st->chip_info->default_config;
789 /* TODO: Add pdata options for filtering and bit delay */
792 st->int_vref_mv = pdata->vref_mv;
794 st->int_vref_mv = st->chip_info->int_vref_mv;
796 st->reg = regulator_get(&client->dev, "vcc");
797 if (!IS_ERR(st->reg)) {
798 ret = regulator_enable(st->reg);
804 st->indio_dev = iio_allocate_device();
805 if (st->indio_dev == NULL) {
807 goto error_disable_reg;
810 /* Estabilish that the iio_dev is a child of the i2c device */
811 st->indio_dev->dev.parent = &client->dev;
812 st->indio_dev->attrs = st->chip_info->dev_attrs;
813 st->indio_dev->event_attrs = st->chip_info->event_attrs;
815 st->indio_dev->dev_data = (void *)(st);
816 st->indio_dev->driver_module = THIS_MODULE;
817 st->indio_dev->modes = INDIO_DIRECT_MODE;
818 st->indio_dev->num_interrupt_lines = 1;
820 ret = ad799x_set_scan_mode(st, 0);
822 goto error_free_device;
824 ret = ad799x_register_ring_funcs_and_init(st->indio_dev);
826 goto error_free_device;
828 ret = iio_device_register(st->indio_dev);
830 goto error_cleanup_ring;
833 ret = iio_ring_buffer_register(st->indio_dev->ring, 0);
835 goto error_cleanup_ring;
837 if (client->irq > 0 && st->chip_info->monitor_mode) {
838 INIT_WORK(&st->work_thresh, ad799x_interrupt_bh);
840 ret = iio_register_interrupt_line(client->irq,
843 IRQF_TRIGGER_FALLING,
846 goto error_cleanup_ring;
849 * The event handler list element refer to iio_event_ad799x.
850 * All event attributes bind to the same event handler.
851 * So, only register event handler once.
853 iio_add_event_to_list(&iio_event_ad799x,
854 &st->indio_dev->interrupts[0]->ev_list);
859 ad799x_ring_cleanup(st->indio_dev);
862 iio_free_device(st->indio_dev);
864 iio_device_unregister(st->indio_dev);
866 if (!IS_ERR(st->reg))
867 regulator_disable(st->reg);
869 if (!IS_ERR(st->reg))
870 regulator_put(st->reg);
876 static __devexit int ad799x_remove(struct i2c_client *client)
878 struct ad799x_state *st = i2c_get_clientdata(client);
879 struct iio_dev *indio_dev = st->indio_dev;
881 if (client->irq > 0 && st->chip_info->monitor_mode)
882 iio_unregister_interrupt_line(indio_dev, 0);
884 iio_ring_buffer_unregister(indio_dev->ring);
885 ad799x_ring_cleanup(indio_dev);
886 iio_device_unregister(indio_dev);
887 if (!IS_ERR(st->reg)) {
888 regulator_disable(st->reg);
889 regulator_put(st->reg);
896 static const struct i2c_device_id ad799x_id[] = {
897 { "ad7991", ad7991 },
898 { "ad7995", ad7995 },
899 { "ad7999", ad7999 },
900 { "ad7992", ad7992 },
901 { "ad7993", ad7993 },
902 { "ad7994", ad7994 },
903 { "ad7997", ad7997 },
904 { "ad7998", ad7998 },
908 MODULE_DEVICE_TABLE(i2c, ad799x_id);
910 static struct i2c_driver ad799x_driver = {
914 .probe = ad799x_probe,
915 .remove = __devexit_p(ad799x_remove),
916 .id_table = ad799x_id,
919 static __init int ad799x_init(void)
921 return i2c_add_driver(&ad799x_driver);
924 static __exit void ad799x_exit(void)
926 i2c_del_driver(&ad799x_driver);
929 MODULE_AUTHOR("Michael Hennerich <hennerich@blackfin.uclinux.org>");
930 MODULE_DESCRIPTION("Analog Devices AD799x ADC");
931 MODULE_LICENSE("GPL v2");
932 MODULE_ALIAS("i2c:ad799x");
934 module_init(ad799x_init);
935 module_exit(ad799x_exit);