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 ssize_t
ad799x_show_precision(struct device
*dev
,
127 struct device_attribute
*attr
,
130 struct iio_dev
*dev_info
= dev_get_drvdata(dev
);
131 struct ad799x_state
*st
= iio_dev_get_devdata(dev_info
);
132 return sprintf(buf
, "%d\n", st
->chip_info
->bits
);
135 static IIO_DEVICE_ATTR(in_precision
, S_IRUGO
, ad799x_show_precision
,
138 static int ad7991_5_9_set_scan_mode(struct ad799x_state
*st
, unsigned mask
)
140 return i2c_smbus_write_byte(st
->client
,
141 st
->config
| (mask
<< AD799X_CHANNEL_SHIFT
));
144 static int ad7992_3_4_set_scan_mode(struct ad799x_state
*st
, unsigned mask
)
146 return ad799x_i2c_write8(st
, AD7998_CONF_REG
,
147 st
->config
| (mask
<< AD799X_CHANNEL_SHIFT
));
150 static int ad7997_8_set_scan_mode(struct ad799x_state
*st
, unsigned mask
)
152 return ad799x_i2c_write16(st
, AD7998_CONF_REG
,
153 st
->config
| (mask
<< AD799X_CHANNEL_SHIFT
));
156 int ad799x_set_scan_mode(struct ad799x_state
*st
, unsigned mask
)
160 if (st
->chip_info
->ad799x_set_scan_mode
!= NULL
) {
161 ret
= st
->chip_info
->ad799x_set_scan_mode(st
, mask
);
162 return (ret
> 0) ? 0 : ret
;
168 static ssize_t
ad799x_read_single_channel(struct device
*dev
,
169 struct device_attribute
*attr
,
172 struct iio_dev
*dev_info
= dev_get_drvdata(dev
);
173 struct ad799x_state
*st
= iio_dev_get_devdata(dev_info
);
174 struct iio_dev_attr
*this_attr
= to_iio_dev_attr(attr
);
175 int ret
= 0, len
= 0;
181 mutex_lock(&dev_info
->mlock
);
182 mask
= 1 << this_attr
->address
;
183 /* If ring buffer capture is occuring, query the buffer */
184 if (iio_ring_enabled(dev_info
)) {
185 data
= ret
= ad799x_single_channel_from_ring(st
, mask
);
194 cmd
= st
->config
| (mask
<< AD799X_CHANNEL_SHIFT
);
199 cmd
= mask
<< AD799X_CHANNEL_SHIFT
;
203 cmd
= (this_attr
->address
<<
204 AD799X_CHANNEL_SHIFT
) | AD7997_8_READ_SINGLE
;
210 ret
= ad799x_i2c_read16(st
, cmd
, rxbuf
);
214 data
= rxbuf
[0] & 0xFFF;
217 /* Pretty print the result */
218 len
= sprintf(buf
, "%u\n", data
);
221 mutex_unlock(&dev_info
->mlock
);
222 return ret
? ret
: len
;
225 static ssize_t
ad799x_read_frequency(struct device
*dev
,
226 struct device_attribute
*attr
,
229 struct iio_dev
*dev_info
= dev_get_drvdata(dev
);
230 struct ad799x_state
*st
= iio_dev_get_devdata(dev_info
);
234 ret
= ad799x_i2c_read8(st
, AD7998_CYCLE_TMR_REG
, &val
);
238 val
&= AD7998_CYC_MASK
;
242 len
= sprintf(buf
, "0\n");
244 case AD7998_CYC_TCONF_32
:
245 len
= sprintf(buf
, "15625\n");
247 case AD7998_CYC_TCONF_64
:
248 len
= sprintf(buf
, "7812\n");
250 case AD7998_CYC_TCONF_128
:
251 len
= sprintf(buf
, "3906\n");
253 case AD7998_CYC_TCONF_256
:
254 len
= sprintf(buf
, "1953\n");
256 case AD7998_CYC_TCONF_512
:
257 len
= sprintf(buf
, "976\n");
259 case AD7998_CYC_TCONF_1024
:
260 len
= sprintf(buf
, "488\n");
262 case AD7998_CYC_TCONF_2048
:
263 len
= sprintf(buf
, "244\n");
269 static ssize_t
ad799x_write_frequency(struct device
*dev
,
270 struct device_attribute
*attr
,
274 struct iio_dev
*dev_info
= dev_get_drvdata(dev
);
275 struct ad799x_state
*st
= iio_dev_get_devdata(dev_info
);
281 ret
= strict_strtol(buf
, 10, &val
);
285 mutex_lock(&dev_info
->mlock
);
286 ret
= ad799x_i2c_read8(st
, AD7998_CYCLE_TMR_REG
, &t
);
288 goto error_ret_mutex
;
289 /* Wipe the bits clean */
290 t
&= ~AD7998_CYC_MASK
;
294 t
|= AD7998_CYC_TCONF_32
;
297 t
|= AD7998_CYC_TCONF_64
;
300 t
|= AD7998_CYC_TCONF_128
;
303 t
|= AD7998_CYC_TCONF_256
;
306 t
|= AD7998_CYC_TCONF_512
;
309 t
|= AD7998_CYC_TCONF_1024
;
312 t
|= AD7998_CYC_TCONF_2048
;
319 goto error_ret_mutex
;
322 ret
= ad799x_i2c_write8(st
, AD7998_CYCLE_TMR_REG
, t
);
325 mutex_unlock(&dev_info
->mlock
);
327 return ret
? ret
: len
;
331 static ssize_t
ad799x_read_channel_config(struct device
*dev
,
332 struct device_attribute
*attr
,
335 struct iio_dev
*dev_info
= dev_get_drvdata(dev
);
336 struct ad799x_state
*st
= iio_dev_get_devdata(dev_info
);
337 struct iio_event_attr
*this_attr
= to_iio_event_attr(attr
);
341 ret
= ad799x_i2c_read16(st
, this_attr
->mask
, &val
);
345 return sprintf(buf
, "%d\n", val
);
348 static ssize_t
ad799x_write_channel_config(struct device
*dev
,
349 struct device_attribute
*attr
,
353 struct iio_dev
*dev_info
= dev_get_drvdata(dev
);
354 struct ad799x_state
*st
= iio_dev_get_devdata(dev_info
);
355 struct iio_event_attr
*this_attr
= to_iio_event_attr(attr
);
360 ret
= strict_strtol(buf
, 10, &val
);
364 mutex_lock(&dev_info
->mlock
);
365 ret
= ad799x_i2c_write16(st
, this_attr
->mask
, val
);
366 mutex_unlock(&dev_info
->mlock
);
368 return ret
? ret
: len
;
371 static void ad799x_interrupt_bh(struct work_struct
*work_s
)
373 struct ad799x_state
*st
= container_of(work_s
,
374 struct ad799x_state
, work_thresh
);
378 if (ad799x_i2c_read8(st
, AD7998_ALERT_STAT_REG
, &status
))
384 ad799x_i2c_write8(st
, AD7998_ALERT_STAT_REG
, AD7998_ALERT_STAT_CLEAR
);
386 for (i
= 0; i
< 8; i
++) {
387 if (status
& (1 << i
))
388 iio_push_event(st
->indio_dev
, 0,
390 IIO_EVENT_CODE_IN_HIGH_THRESH(i
>> 1) :
391 IIO_EVENT_CODE_IN_LOW_THRESH(i
>> 1),
396 enable_irq(st
->client
->irq
);
399 static int ad799x_interrupt(struct iio_dev
*dev_info
,
404 struct ad799x_state
*st
= dev_info
->dev_data
;
406 st
->last_timestamp
= timestamp
;
407 schedule_work(&st
->work_thresh
);
411 IIO_EVENT_SH(ad799x
, &ad799x_interrupt
);
413 /* Direct read attribtues */
414 static IIO_DEV_ATTR_IN_RAW(0, ad799x_read_single_channel
, 0);
415 static IIO_DEV_ATTR_IN_RAW(1, ad799x_read_single_channel
, 1);
416 static IIO_DEV_ATTR_IN_RAW(2, ad799x_read_single_channel
, 2);
417 static IIO_DEV_ATTR_IN_RAW(3, ad799x_read_single_channel
, 3);
418 static IIO_DEV_ATTR_IN_RAW(4, ad799x_read_single_channel
, 4);
419 static IIO_DEV_ATTR_IN_RAW(5, ad799x_read_single_channel
, 5);
420 static IIO_DEV_ATTR_IN_RAW(6, ad799x_read_single_channel
, 6);
421 static IIO_DEV_ATTR_IN_RAW(7, ad799x_read_single_channel
, 7);
423 static ssize_t
ad799x_show_scale(struct device
*dev
,
424 struct device_attribute
*attr
,
427 /* Driver currently only support internal vref */
428 struct iio_dev
*dev_info
= dev_get_drvdata(dev
);
429 struct ad799x_state
*st
= iio_dev_get_devdata(dev_info
);
430 /* Corresponds to Vref / 2^(bits) */
432 if ((1 << (st
->chip_info
->bits
+ 1))
434 return sprintf(buf
, "0.5\n");
436 return sprintf(buf
, "%d\n",
437 st
->int_vref_mv
>> st
->chip_info
->bits
);
440 static IIO_DEVICE_ATTR(in_scale
, S_IRUGO
, ad799x_show_scale
, NULL
, 0);
442 static ssize_t
ad799x_show_name(struct device
*dev
,
443 struct device_attribute
*attr
,
446 struct iio_dev
*dev_info
= dev_get_drvdata(dev
);
447 struct ad799x_state
*st
= iio_dev_get_devdata(dev_info
);
448 return sprintf(buf
, "%s\n", st
->client
->name
);
451 static IIO_DEVICE_ATTR(name
, S_IRUGO
, ad799x_show_name
, NULL
, 0);
453 static struct attribute
*ad7991_5_9_3_4_device_attrs
[] = {
454 &iio_dev_attr_in0_raw
.dev_attr
.attr
,
455 &iio_dev_attr_in1_raw
.dev_attr
.attr
,
456 &iio_dev_attr_in2_raw
.dev_attr
.attr
,
457 &iio_dev_attr_in3_raw
.dev_attr
.attr
,
458 &iio_dev_attr_name
.dev_attr
.attr
,
459 &iio_dev_attr_in_scale
.dev_attr
.attr
,
463 static struct attribute_group ad7991_5_9_3_4_dev_attr_group
= {
464 .attrs
= ad7991_5_9_3_4_device_attrs
,
467 static struct attribute
*ad7991_5_9_3_4_scan_el_attrs
[] = {
468 &iio_scan_el_in0
.dev_attr
.attr
,
469 &iio_const_attr_in0_index
.dev_attr
.attr
,
470 &iio_scan_el_in1
.dev_attr
.attr
,
471 &iio_const_attr_in1_index
.dev_attr
.attr
,
472 &iio_scan_el_in2
.dev_attr
.attr
,
473 &iio_const_attr_in2_index
.dev_attr
.attr
,
474 &iio_scan_el_in3
.dev_attr
.attr
,
475 &iio_const_attr_in3_index
.dev_attr
.attr
,
476 &iio_dev_attr_in_precision
.dev_attr
.attr
,
480 static struct attribute_group ad7991_5_9_3_4_scan_el_group
= {
481 .name
= "scan_elements",
482 .attrs
= ad7991_5_9_3_4_scan_el_attrs
,
485 static struct attribute
*ad7992_device_attrs
[] = {
486 &iio_dev_attr_in0_raw
.dev_attr
.attr
,
487 &iio_dev_attr_in1_raw
.dev_attr
.attr
,
488 &iio_dev_attr_name
.dev_attr
.attr
,
489 &iio_dev_attr_in_scale
.dev_attr
.attr
,
493 static struct attribute_group ad7992_dev_attr_group
= {
494 .attrs
= ad7992_device_attrs
,
497 static struct attribute
*ad7992_scan_el_attrs
[] = {
498 &iio_scan_el_in0
.dev_attr
.attr
,
499 &iio_const_attr_in0_index
.dev_attr
.attr
,
500 &iio_scan_el_in1
.dev_attr
.attr
,
501 &iio_const_attr_in1_index
.dev_attr
.attr
,
502 &iio_dev_attr_in_precision
.dev_attr
.attr
,
506 static struct attribute_group ad7992_scan_el_group
= {
507 .name
= "scan_elements",
508 .attrs
= ad7992_scan_el_attrs
,
511 static struct attribute
*ad7997_8_device_attrs
[] = {
512 &iio_dev_attr_in0_raw
.dev_attr
.attr
,
513 &iio_dev_attr_in1_raw
.dev_attr
.attr
,
514 &iio_dev_attr_in2_raw
.dev_attr
.attr
,
515 &iio_dev_attr_in3_raw
.dev_attr
.attr
,
516 &iio_dev_attr_in4_raw
.dev_attr
.attr
,
517 &iio_dev_attr_in5_raw
.dev_attr
.attr
,
518 &iio_dev_attr_in6_raw
.dev_attr
.attr
,
519 &iio_dev_attr_in7_raw
.dev_attr
.attr
,
520 &iio_dev_attr_name
.dev_attr
.attr
,
521 &iio_dev_attr_in_scale
.dev_attr
.attr
,
525 static struct attribute_group ad7997_8_dev_attr_group
= {
526 .attrs
= ad7997_8_device_attrs
,
529 static struct attribute
*ad7997_8_scan_el_attrs
[] = {
530 &iio_scan_el_in0
.dev_attr
.attr
,
531 &iio_const_attr_in0_index
.dev_attr
.attr
,
532 &iio_scan_el_in1
.dev_attr
.attr
,
533 &iio_const_attr_in1_index
.dev_attr
.attr
,
534 &iio_scan_el_in2
.dev_attr
.attr
,
535 &iio_const_attr_in2_index
.dev_attr
.attr
,
536 &iio_scan_el_in3
.dev_attr
.attr
,
537 &iio_const_attr_in3_index
.dev_attr
.attr
,
538 &iio_scan_el_in4
.dev_attr
.attr
,
539 &iio_const_attr_in4_index
.dev_attr
.attr
,
540 &iio_scan_el_in5
.dev_attr
.attr
,
541 &iio_const_attr_in5_index
.dev_attr
.attr
,
542 &iio_scan_el_in6
.dev_attr
.attr
,
543 &iio_const_attr_in6_index
.dev_attr
.attr
,
544 &iio_scan_el_in7
.dev_attr
.attr
,
545 &iio_const_attr_in7_index
.dev_attr
.attr
,
546 &iio_dev_attr_in_precision
.dev_attr
.attr
,
550 static struct attribute_group ad7997_8_scan_el_group
= {
551 .name
= "scan_elements",
552 .attrs
= ad7997_8_scan_el_attrs
,
555 IIO_EVENT_ATTR_SH(in0_thresh_low_value
,
557 ad799x_read_channel_config
,
558 ad799x_write_channel_config
,
559 AD7998_DATALOW_CH1_REG
);
561 IIO_EVENT_ATTR_SH(in0_thresh_high_value
,
563 ad799x_read_channel_config
,
564 ad799x_write_channel_config
,
565 AD7998_DATAHIGH_CH1_REG
);
567 IIO_EVENT_ATTR_SH(in0_thresh_both_hyst_raw
,
569 ad799x_read_channel_config
,
570 ad799x_write_channel_config
,
571 AD7998_HYST_CH1_REG
);
573 IIO_EVENT_ATTR_SH(in1_thresh_low_value
,
575 ad799x_read_channel_config
,
576 ad799x_write_channel_config
,
577 AD7998_DATALOW_CH2_REG
);
579 IIO_EVENT_ATTR_SH(in1_thresh_high_value
,
581 ad799x_read_channel_config
,
582 ad799x_write_channel_config
,
583 AD7998_DATAHIGH_CH2_REG
);
585 IIO_EVENT_ATTR_SH(in1_thresh_both_hyst_raw
,
587 ad799x_read_channel_config
,
588 ad799x_write_channel_config
,
589 AD7998_HYST_CH2_REG
);
591 IIO_EVENT_ATTR_SH(in2_thresh_low_value
,
593 ad799x_read_channel_config
,
594 ad799x_write_channel_config
,
595 AD7998_DATALOW_CH3_REG
);
597 IIO_EVENT_ATTR_SH(in2_thresh_high_value
,
599 ad799x_read_channel_config
,
600 ad799x_write_channel_config
,
601 AD7998_DATAHIGH_CH3_REG
);
603 IIO_EVENT_ATTR_SH(in2_thresh_both_hyst_raw
,
605 ad799x_read_channel_config
,
606 ad799x_write_channel_config
,
607 AD7998_HYST_CH3_REG
);
609 IIO_EVENT_ATTR_SH(in3_thresh_low_value
,
611 ad799x_read_channel_config
,
612 ad799x_write_channel_config
,
613 AD7998_DATALOW_CH4_REG
);
615 IIO_EVENT_ATTR_SH(in3_thresh_high_value
,
617 ad799x_read_channel_config
,
618 ad799x_write_channel_config
,
619 AD7998_DATAHIGH_CH4_REG
);
621 IIO_EVENT_ATTR_SH(in3_thresh_both_hyst_raw
,
623 ad799x_read_channel_config
,
624 ad799x_write_channel_config
,
625 AD7998_HYST_CH4_REG
);
627 static IIO_DEV_ATTR_SAMP_FREQ(S_IWUSR
| S_IRUGO
,
628 ad799x_read_frequency
,
629 ad799x_write_frequency
);
630 static IIO_CONST_ATTR_SAMP_FREQ_AVAIL("15625 7812 3906 1953 976 488 244 0");
632 static struct attribute
*ad7993_4_7_8_event_attributes
[] = {
633 &iio_event_attr_in0_thresh_low_value
.dev_attr
.attr
,
634 &iio_event_attr_in0_thresh_high_value
.dev_attr
.attr
,
635 &iio_event_attr_in0_thresh_both_hyst_raw
.dev_attr
.attr
,
636 &iio_event_attr_in1_thresh_low_value
.dev_attr
.attr
,
637 &iio_event_attr_in1_thresh_high_value
.dev_attr
.attr
,
638 &iio_event_attr_in1_thresh_both_hyst_raw
.dev_attr
.attr
,
639 &iio_event_attr_in2_thresh_low_value
.dev_attr
.attr
,
640 &iio_event_attr_in2_thresh_high_value
.dev_attr
.attr
,
641 &iio_event_attr_in2_thresh_both_hyst_raw
.dev_attr
.attr
,
642 &iio_event_attr_in3_thresh_low_value
.dev_attr
.attr
,
643 &iio_event_attr_in3_thresh_high_value
.dev_attr
.attr
,
644 &iio_event_attr_in3_thresh_both_hyst_raw
.dev_attr
.attr
,
645 &iio_dev_attr_sampling_frequency
.dev_attr
.attr
,
646 &iio_const_attr_sampling_frequency_available
.dev_attr
.attr
,
650 static struct attribute_group ad7993_4_7_8_event_attrs_group
= {
651 .attrs
= ad7993_4_7_8_event_attributes
,
654 static struct attribute
*ad7992_event_attributes
[] = {
655 &iio_event_attr_in0_thresh_low_value
.dev_attr
.attr
,
656 &iio_event_attr_in0_thresh_high_value
.dev_attr
.attr
,
657 &iio_event_attr_in0_thresh_both_hyst_raw
.dev_attr
.attr
,
658 &iio_event_attr_in1_thresh_low_value
.dev_attr
.attr
,
659 &iio_event_attr_in1_thresh_high_value
.dev_attr
.attr
,
660 &iio_event_attr_in1_thresh_both_hyst_raw
.dev_attr
.attr
,
661 &iio_dev_attr_sampling_frequency
.dev_attr
.attr
,
662 &iio_const_attr_sampling_frequency_available
.dev_attr
.attr
,
666 static struct attribute_group ad7992_event_attrs_group
= {
667 .attrs
= ad7992_event_attributes
,
670 static const struct ad799x_chip_info ad799x_chip_info_tbl
[] = {
675 .dev_attrs
= &ad7991_5_9_3_4_dev_attr_group
,
676 .scan_attrs
= &ad7991_5_9_3_4_scan_el_group
,
677 .ad799x_set_scan_mode
= ad7991_5_9_set_scan_mode
,
683 .dev_attrs
= &ad7991_5_9_3_4_dev_attr_group
,
684 .scan_attrs
= &ad7991_5_9_3_4_scan_el_group
,
685 .ad799x_set_scan_mode
= ad7991_5_9_set_scan_mode
,
691 .dev_attrs
= &ad7991_5_9_3_4_dev_attr_group
,
692 .scan_attrs
= &ad7991_5_9_3_4_scan_el_group
,
693 .ad799x_set_scan_mode
= ad7991_5_9_set_scan_mode
,
699 .monitor_mode
= true,
700 .default_config
= AD7998_ALERT_EN
,
701 .dev_attrs
= &ad7992_dev_attr_group
,
702 .scan_attrs
= &ad7992_scan_el_group
,
703 .event_attrs
= &ad7992_event_attrs_group
,
704 .ad799x_set_scan_mode
= ad7992_3_4_set_scan_mode
,
710 .monitor_mode
= true,
711 .default_config
= AD7998_ALERT_EN
,
712 .dev_attrs
= &ad7991_5_9_3_4_dev_attr_group
,
713 .scan_attrs
= &ad7991_5_9_3_4_scan_el_group
,
714 .event_attrs
= &ad7993_4_7_8_event_attrs_group
,
715 .ad799x_set_scan_mode
= ad7992_3_4_set_scan_mode
,
721 .monitor_mode
= true,
722 .default_config
= AD7998_ALERT_EN
,
723 .dev_attrs
= &ad7991_5_9_3_4_dev_attr_group
,
724 .scan_attrs
= &ad7991_5_9_3_4_scan_el_group
,
725 .event_attrs
= &ad7993_4_7_8_event_attrs_group
,
726 .ad799x_set_scan_mode
= ad7992_3_4_set_scan_mode
,
732 .monitor_mode
= true,
733 .default_config
= AD7998_ALERT_EN
,
734 .dev_attrs
= &ad7997_8_dev_attr_group
,
735 .scan_attrs
= &ad7997_8_scan_el_group
,
736 .event_attrs
= &ad7993_4_7_8_event_attrs_group
,
737 .ad799x_set_scan_mode
= ad7997_8_set_scan_mode
,
743 .monitor_mode
= true,
744 .default_config
= AD7998_ALERT_EN
,
745 .dev_attrs
= &ad7997_8_dev_attr_group
,
746 .scan_attrs
= &ad7997_8_scan_el_group
,
747 .event_attrs
= &ad7993_4_7_8_event_attrs_group
,
748 .ad799x_set_scan_mode
= ad7997_8_set_scan_mode
,
752 static int __devinit
ad799x_probe(struct i2c_client
*client
,
753 const struct i2c_device_id
*id
)
755 int ret
, regdone
= 0;
756 struct ad799x_platform_data
*pdata
= client
->dev
.platform_data
;
757 struct ad799x_state
*st
= kzalloc(sizeof(*st
), GFP_KERNEL
);
763 /* this is only used for device removal purposes */
764 i2c_set_clientdata(client
, st
);
766 atomic_set(&st
->protect_ring
, 0);
767 st
->id
= id
->driver_data
;
768 st
->chip_info
= &ad799x_chip_info_tbl
[st
->id
];
769 st
->config
= st
->chip_info
->default_config
;
771 /* TODO: Add pdata options for filtering and bit delay */
774 st
->int_vref_mv
= pdata
->vref_mv
;
776 st
->int_vref_mv
= st
->chip_info
->int_vref_mv
;
778 st
->reg
= regulator_get(&client
->dev
, "vcc");
779 if (!IS_ERR(st
->reg
)) {
780 ret
= regulator_enable(st
->reg
);
786 st
->indio_dev
= iio_allocate_device();
787 if (st
->indio_dev
== NULL
) {
789 goto error_disable_reg
;
792 /* Estabilish that the iio_dev is a child of the i2c device */
793 st
->indio_dev
->dev
.parent
= &client
->dev
;
794 st
->indio_dev
->attrs
= st
->chip_info
->dev_attrs
;
795 st
->indio_dev
->event_attrs
= st
->chip_info
->event_attrs
;
797 st
->indio_dev
->dev_data
= (void *)(st
);
798 st
->indio_dev
->driver_module
= THIS_MODULE
;
799 st
->indio_dev
->modes
= INDIO_DIRECT_MODE
;
800 st
->indio_dev
->num_interrupt_lines
= 1;
802 ret
= ad799x_set_scan_mode(st
, 0);
804 goto error_free_device
;
806 ret
= ad799x_register_ring_funcs_and_init(st
->indio_dev
);
808 goto error_free_device
;
810 ret
= iio_device_register(st
->indio_dev
);
812 goto error_cleanup_ring
;
815 ret
= iio_ring_buffer_register(st
->indio_dev
->ring
, 0);
817 goto error_cleanup_ring
;
819 if (client
->irq
> 0 && st
->chip_info
->monitor_mode
) {
820 INIT_WORK(&st
->work_thresh
, ad799x_interrupt_bh
);
822 ret
= iio_register_interrupt_line(client
->irq
,
825 IRQF_TRIGGER_FALLING
,
828 goto error_cleanup_ring
;
831 * The event handler list element refer to iio_event_ad799x.
832 * All event attributes bind to the same event handler.
833 * So, only register event handler once.
835 iio_add_event_to_list(&iio_event_ad799x
,
836 &st
->indio_dev
->interrupts
[0]->ev_list
);
841 ad799x_ring_cleanup(st
->indio_dev
);
844 iio_free_device(st
->indio_dev
);
846 iio_device_unregister(st
->indio_dev
);
848 if (!IS_ERR(st
->reg
))
849 regulator_disable(st
->reg
);
851 if (!IS_ERR(st
->reg
))
852 regulator_put(st
->reg
);
858 static __devexit
int ad799x_remove(struct i2c_client
*client
)
860 struct ad799x_state
*st
= i2c_get_clientdata(client
);
861 struct iio_dev
*indio_dev
= st
->indio_dev
;
863 if (client
->irq
> 0 && st
->chip_info
->monitor_mode
)
864 iio_unregister_interrupt_line(indio_dev
, 0);
866 iio_ring_buffer_unregister(indio_dev
->ring
);
867 ad799x_ring_cleanup(indio_dev
);
868 iio_device_unregister(indio_dev
);
869 if (!IS_ERR(st
->reg
)) {
870 regulator_disable(st
->reg
);
871 regulator_put(st
->reg
);
878 static const struct i2c_device_id ad799x_id
[] = {
879 { "ad7991", ad7991
},
880 { "ad7995", ad7995
},
881 { "ad7999", ad7999
},
882 { "ad7992", ad7992
},
883 { "ad7993", ad7993
},
884 { "ad7994", ad7994
},
885 { "ad7997", ad7997
},
886 { "ad7998", ad7998
},
890 MODULE_DEVICE_TABLE(i2c
, ad799x_id
);
892 static struct i2c_driver ad799x_driver
= {
896 .probe
= ad799x_probe
,
897 .remove
= __devexit_p(ad799x_remove
),
898 .id_table
= ad799x_id
,
901 static __init
int ad799x_init(void)
903 return i2c_add_driver(&ad799x_driver
);
906 static __exit
void ad799x_exit(void)
908 i2c_del_driver(&ad799x_driver
);
911 MODULE_AUTHOR("Michael Hennerich <hennerich@blackfin.uclinux.org>");
912 MODULE_DESCRIPTION("Analog Devices AD799x ADC");
913 MODULE_LICENSE("GPL v2");
914 MODULE_ALIAS("i2c:ad799x");
916 module_init(ad799x_init
);
917 module_exit(ad799x_exit
);