3 * Copyright (C) 2010-1011 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/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>
38 #include "../ring_generic.h"
43 * ad799x register access by I2C
45 static int ad799x_i2c_read16(struct ad799x_state
*st
, u8 reg
, u16
*data
)
47 struct i2c_client
*client
= st
->client
;
50 ret
= i2c_smbus_read_word_data(client
, reg
);
52 dev_err(&client
->dev
, "I2C read error\n");
56 *data
= swab16((u16
)ret
);
61 static int ad799x_i2c_read8(struct ad799x_state
*st
, u8 reg
, u8
*data
)
63 struct i2c_client
*client
= st
->client
;
66 ret
= i2c_smbus_read_byte_data(client
, reg
);
68 dev_err(&client
->dev
, "I2C read error\n");
77 static int ad799x_i2c_write16(struct ad799x_state
*st
, u8 reg
, u16 data
)
79 struct i2c_client
*client
= st
->client
;
82 ret
= i2c_smbus_write_word_data(client
, reg
, swab16(data
));
84 dev_err(&client
->dev
, "I2C write error\n");
89 static int ad799x_i2c_write8(struct ad799x_state
*st
, u8 reg
, u8 data
)
91 struct i2c_client
*client
= st
->client
;
94 ret
= i2c_smbus_write_byte_data(client
, reg
, data
);
96 dev_err(&client
->dev
, "I2C write error\n");
101 int ad7997_8_set_scan_mode(struct ad799x_state
*st
, unsigned mask
)
103 return ad799x_i2c_write16(st
, AD7998_CONF_REG
,
104 st
->config
| (mask
<< AD799X_CHANNEL_SHIFT
));
107 static int ad799x_scan_direct(struct ad799x_state
*st
, unsigned ch
)
117 cmd
= st
->config
| ((1 << ch
) << AD799X_CHANNEL_SHIFT
);
122 cmd
= (1 << ch
) << AD799X_CHANNEL_SHIFT
;
126 cmd
= (ch
<< AD799X_CHANNEL_SHIFT
) | AD7997_8_READ_SINGLE
;
132 ret
= ad799x_i2c_read16(st
, cmd
, &rxbuf
);
139 static int ad799x_read_raw(struct iio_dev
*dev_info
,
140 struct iio_chan_spec
const *chan
,
146 struct ad799x_state
*st
= iio_priv(dev_info
);
147 unsigned int scale_uv
;
151 mutex_lock(&dev_info
->mlock
);
152 if (iio_ring_enabled(dev_info
))
153 ret
= ad799x_single_channel_from_ring(st
,
156 ret
= ad799x_scan_direct(st
, chan
->address
);
157 mutex_unlock(&dev_info
->mlock
);
161 *val
= (ret
>> st
->chip_info
->channel
[0].scan_type
.shift
) &
162 RES_MASK(st
->chip_info
->channel
[0].scan_type
.realbits
);
164 case (1 << IIO_CHAN_INFO_SCALE_SHARED
):
165 scale_uv
= (st
->int_vref_mv
* 1000)
166 >> st
->chip_info
->channel
[0].scan_type
.realbits
;
167 *val
= scale_uv
/ 1000;
168 *val2
= (scale_uv
% 1000) * 1000;
169 return IIO_VAL_INT_PLUS_MICRO
;
174 static ssize_t
ad799x_read_frequency(struct device
*dev
,
175 struct device_attribute
*attr
,
178 struct iio_dev
*dev_info
= dev_get_drvdata(dev
);
179 struct ad799x_state
*st
= iio_priv(dev_info
);
183 ret
= ad799x_i2c_read8(st
, AD7998_CYCLE_TMR_REG
, &val
);
187 val
&= AD7998_CYC_MASK
;
191 len
= sprintf(buf
, "0\n");
193 case AD7998_CYC_TCONF_32
:
194 len
= sprintf(buf
, "15625\n");
196 case AD7998_CYC_TCONF_64
:
197 len
= sprintf(buf
, "7812\n");
199 case AD7998_CYC_TCONF_128
:
200 len
= sprintf(buf
, "3906\n");
202 case AD7998_CYC_TCONF_256
:
203 len
= sprintf(buf
, "1953\n");
205 case AD7998_CYC_TCONF_512
:
206 len
= sprintf(buf
, "976\n");
208 case AD7998_CYC_TCONF_1024
:
209 len
= sprintf(buf
, "488\n");
211 case AD7998_CYC_TCONF_2048
:
212 len
= sprintf(buf
, "244\n");
218 static ssize_t
ad799x_write_frequency(struct device
*dev
,
219 struct device_attribute
*attr
,
223 struct iio_dev
*dev_info
= dev_get_drvdata(dev
);
224 struct ad799x_state
*st
= iio_priv(dev_info
);
230 ret
= strict_strtol(buf
, 10, &val
);
234 mutex_lock(&dev_info
->mlock
);
235 ret
= ad799x_i2c_read8(st
, AD7998_CYCLE_TMR_REG
, &t
);
237 goto error_ret_mutex
;
238 /* Wipe the bits clean */
239 t
&= ~AD7998_CYC_MASK
;
243 t
|= AD7998_CYC_TCONF_32
;
246 t
|= AD7998_CYC_TCONF_64
;
249 t
|= AD7998_CYC_TCONF_128
;
252 t
|= AD7998_CYC_TCONF_256
;
255 t
|= AD7998_CYC_TCONF_512
;
258 t
|= AD7998_CYC_TCONF_1024
;
261 t
|= AD7998_CYC_TCONF_2048
;
268 goto error_ret_mutex
;
271 ret
= ad799x_i2c_write8(st
, AD7998_CYCLE_TMR_REG
, t
);
274 mutex_unlock(&dev_info
->mlock
);
276 return ret
? ret
: len
;
279 static ssize_t
ad799x_read_channel_config(struct device
*dev
,
280 struct device_attribute
*attr
,
283 struct iio_dev
*dev_info
= dev_get_drvdata(dev
);
284 struct ad799x_state
*st
= iio_priv(dev_info
);
285 struct iio_dev_attr
*this_attr
= to_iio_dev_attr(attr
);
289 ret
= ad799x_i2c_read16(st
, this_attr
->address
, &val
);
293 return sprintf(buf
, "%d\n", val
);
296 static ssize_t
ad799x_write_channel_config(struct device
*dev
,
297 struct device_attribute
*attr
,
301 struct iio_dev
*dev_info
= dev_get_drvdata(dev
);
302 struct ad799x_state
*st
= iio_priv(dev_info
);
303 struct iio_dev_attr
*this_attr
= to_iio_dev_attr(attr
);
308 ret
= strict_strtol(buf
, 10, &val
);
312 mutex_lock(&dev_info
->mlock
);
313 ret
= ad799x_i2c_write16(st
, this_attr
->address
, val
);
314 mutex_unlock(&dev_info
->mlock
);
316 return ret
? ret
: len
;
319 static irqreturn_t
ad799x_event_handler(int irq
, void *private)
321 struct iio_dev
*indio_dev
= private;
322 struct ad799x_state
*st
= iio_priv(private);
326 ret
= ad799x_i2c_read8(st
, AD7998_ALERT_STAT_REG
, &status
);
333 ad799x_i2c_write8(st
, AD7998_ALERT_STAT_REG
, AD7998_ALERT_STAT_CLEAR
);
335 for (i
= 0; i
< 8; i
++) {
336 if (status
& (1 << i
))
337 iio_push_event(indio_dev
, 0,
339 IIO_EVENT_CODE_IN_HIGH_THRESH(i
>> 1) :
340 IIO_EVENT_CODE_IN_LOW_THRESH(i
>> 1),
347 static IIO_DEVICE_ATTR(in0_thresh_low_value
,
349 ad799x_read_channel_config
,
350 ad799x_write_channel_config
,
351 AD7998_DATALOW_CH1_REG
);
353 static IIO_DEVICE_ATTR(in0_thresh_high_value
,
355 ad799x_read_channel_config
,
356 ad799x_write_channel_config
,
357 AD7998_DATAHIGH_CH1_REG
);
359 static IIO_DEVICE_ATTR(in0_thresh_both_hyst_raw
,
361 ad799x_read_channel_config
,
362 ad799x_write_channel_config
,
363 AD7998_HYST_CH1_REG
);
365 static IIO_DEVICE_ATTR(in1_thresh_low_value
,
367 ad799x_read_channel_config
,
368 ad799x_write_channel_config
,
369 AD7998_DATALOW_CH2_REG
);
371 static IIO_DEVICE_ATTR(in1_thresh_high_value
,
373 ad799x_read_channel_config
,
374 ad799x_write_channel_config
,
375 AD7998_DATAHIGH_CH2_REG
);
377 static IIO_DEVICE_ATTR(in1_thresh_both_hyst_raw
,
379 ad799x_read_channel_config
,
380 ad799x_write_channel_config
,
381 AD7998_HYST_CH2_REG
);
383 static IIO_DEVICE_ATTR(in2_thresh_low_value
,
385 ad799x_read_channel_config
,
386 ad799x_write_channel_config
,
387 AD7998_DATALOW_CH3_REG
);
389 static IIO_DEVICE_ATTR(in2_thresh_high_value
,
391 ad799x_read_channel_config
,
392 ad799x_write_channel_config
,
393 AD7998_DATAHIGH_CH3_REG
);
395 static IIO_DEVICE_ATTR(in2_thresh_both_hyst_raw
,
397 ad799x_read_channel_config
,
398 ad799x_write_channel_config
,
399 AD7998_HYST_CH3_REG
);
401 static IIO_DEVICE_ATTR(in3_thresh_low_value
,
403 ad799x_read_channel_config
,
404 ad799x_write_channel_config
,
405 AD7998_DATALOW_CH4_REG
);
407 static IIO_DEVICE_ATTR(in3_thresh_high_value
,
409 ad799x_read_channel_config
,
410 ad799x_write_channel_config
,
411 AD7998_DATAHIGH_CH4_REG
);
413 static IIO_DEVICE_ATTR(in3_thresh_both_hyst_raw
,
415 ad799x_read_channel_config
,
416 ad799x_write_channel_config
,
417 AD7998_HYST_CH4_REG
);
419 static IIO_DEV_ATTR_SAMP_FREQ(S_IWUSR
| S_IRUGO
,
420 ad799x_read_frequency
,
421 ad799x_write_frequency
);
422 static IIO_CONST_ATTR_SAMP_FREQ_AVAIL("15625 7812 3906 1953 976 488 244 0");
424 static struct attribute
*ad7993_4_7_8_event_attributes
[] = {
425 &iio_dev_attr_in0_thresh_low_value
.dev_attr
.attr
,
426 &iio_dev_attr_in0_thresh_high_value
.dev_attr
.attr
,
427 &iio_dev_attr_in0_thresh_both_hyst_raw
.dev_attr
.attr
,
428 &iio_dev_attr_in1_thresh_low_value
.dev_attr
.attr
,
429 &iio_dev_attr_in1_thresh_high_value
.dev_attr
.attr
,
430 &iio_dev_attr_in1_thresh_both_hyst_raw
.dev_attr
.attr
,
431 &iio_dev_attr_in2_thresh_low_value
.dev_attr
.attr
,
432 &iio_dev_attr_in2_thresh_high_value
.dev_attr
.attr
,
433 &iio_dev_attr_in2_thresh_both_hyst_raw
.dev_attr
.attr
,
434 &iio_dev_attr_in3_thresh_low_value
.dev_attr
.attr
,
435 &iio_dev_attr_in3_thresh_high_value
.dev_attr
.attr
,
436 &iio_dev_attr_in3_thresh_both_hyst_raw
.dev_attr
.attr
,
437 &iio_dev_attr_sampling_frequency
.dev_attr
.attr
,
438 &iio_const_attr_sampling_frequency_available
.dev_attr
.attr
,
442 static struct attribute_group ad7993_4_7_8_event_attrs_group
= {
443 .attrs
= ad7993_4_7_8_event_attributes
,
446 static struct attribute
*ad7992_event_attributes
[] = {
447 &iio_dev_attr_in0_thresh_low_value
.dev_attr
.attr
,
448 &iio_dev_attr_in0_thresh_high_value
.dev_attr
.attr
,
449 &iio_dev_attr_in0_thresh_both_hyst_raw
.dev_attr
.attr
,
450 &iio_dev_attr_in1_thresh_low_value
.dev_attr
.attr
,
451 &iio_dev_attr_in1_thresh_high_value
.dev_attr
.attr
,
452 &iio_dev_attr_in1_thresh_both_hyst_raw
.dev_attr
.attr
,
453 &iio_dev_attr_sampling_frequency
.dev_attr
.attr
,
454 &iio_const_attr_sampling_frequency_available
.dev_attr
.attr
,
458 static struct attribute_group ad7992_event_attrs_group
= {
459 .attrs
= ad7992_event_attributes
,
462 static const struct iio_info ad7991_info
= {
463 .read_raw
= &ad799x_read_raw
,
464 .driver_module
= THIS_MODULE
,
467 static const struct iio_info ad7992_info
= {
468 .read_raw
= &ad799x_read_raw
,
469 .num_interrupt_lines
= 1,
470 .event_attrs
= &ad7992_event_attrs_group
,
471 .driver_module
= THIS_MODULE
,
474 static const struct iio_info ad7993_4_7_8_info
= {
475 .read_raw
= &ad799x_read_raw
,
476 .num_interrupt_lines
= 1,
477 .event_attrs
= &ad7993_4_7_8_event_attrs_group
,
478 .driver_module
= THIS_MODULE
,
481 static const struct ad799x_chip_info ad799x_chip_info_tbl
[] = {
483 .channel
[0] = IIO_CHAN(IIO_IN
, 0, 1, 0, NULL
, 0, 0,
484 (1 << IIO_CHAN_INFO_SCALE_SHARED
),
485 0, 0, IIO_ST('u', 12, 16, 0), 0),
486 .channel
[1] = IIO_CHAN(IIO_IN
, 0, 1, 0, NULL
, 1, 0,
487 (1 << IIO_CHAN_INFO_SCALE_SHARED
),
488 1, 1, IIO_ST('u', 12, 16, 0), 0),
489 .channel
[2] = IIO_CHAN(IIO_IN
, 0, 1, 0, NULL
, 2, 0,
490 (1 << IIO_CHAN_INFO_SCALE_SHARED
),
491 2, 2, IIO_ST('u', 12, 16, 0), 0),
492 .channel
[3] = IIO_CHAN(IIO_IN
, 0, 1, 0, NULL
, 3, 0,
493 (1 << IIO_CHAN_INFO_SCALE_SHARED
),
494 3, 3, IIO_ST('u', 12, 16, 0), 0),
495 .channel
[4] = IIO_CHAN_SOFT_TIMESTAMP(4),
498 .info
= &ad7991_info
,
501 .channel
[0] = IIO_CHAN(IIO_IN
, 0, 1, 0, NULL
, 0, 0,
502 (1 << IIO_CHAN_INFO_SCALE_SHARED
),
503 0, 0, IIO_ST('u', 10, 16, 0), 0),
504 .channel
[1] = IIO_CHAN(IIO_IN
, 0, 1, 0, NULL
, 1, 0,
505 (1 << IIO_CHAN_INFO_SCALE_SHARED
),
506 1, 1, IIO_ST('u', 10, 16, 0), 0),
507 .channel
[2] = IIO_CHAN(IIO_IN
, 0, 1, 0, NULL
, 2, 0,
508 (1 << IIO_CHAN_INFO_SCALE_SHARED
),
509 2, 2, IIO_ST('u', 10, 16, 0), 0),
510 .channel
[3] = IIO_CHAN(IIO_IN
, 0, 1, 0, NULL
, 3, 0,
511 (1 << IIO_CHAN_INFO_SCALE_SHARED
),
512 3, 3, IIO_ST('u', 10, 16, 0), 0),
513 .channel
[4] = IIO_CHAN_SOFT_TIMESTAMP(4),
516 .info
= &ad7991_info
,
519 .channel
[0] = IIO_CHAN(IIO_IN
, 0, 1, 0, NULL
, 0, 0,
520 (1 << IIO_CHAN_INFO_SCALE_SHARED
),
521 0, 0, IIO_ST('u', 10, 16, 0), 0),
522 .channel
[1] = IIO_CHAN(IIO_IN
, 0, 1, 0, NULL
, 1, 0,
523 (1 << IIO_CHAN_INFO_SCALE_SHARED
),
524 1, 1, IIO_ST('u', 10, 16, 0), 0),
525 .channel
[2] = IIO_CHAN(IIO_IN
, 0, 1, 0, NULL
, 2, 0,
526 (1 << IIO_CHAN_INFO_SCALE_SHARED
),
527 2, 2, IIO_ST('u', 10, 16, 0), 0),
528 .channel
[3] = IIO_CHAN(IIO_IN
, 0, 1, 0, NULL
, 3, 0,
529 (1 << IIO_CHAN_INFO_SCALE_SHARED
),
530 3, 3, IIO_ST('u', 10, 16, 0), 0),
531 .channel
[4] = IIO_CHAN_SOFT_TIMESTAMP(4),
534 .info
= &ad7991_info
,
537 .channel
[0] = IIO_CHAN(IIO_IN
, 0, 1, 0, NULL
, 0, 0,
538 (1 << IIO_CHAN_INFO_SCALE_SHARED
),
539 0, 0, IIO_ST('u', 12, 16, 0), 0),
540 .channel
[1] = IIO_CHAN(IIO_IN
, 0, 1, 0, NULL
, 1, 0,
541 (1 << IIO_CHAN_INFO_SCALE_SHARED
),
542 1, 1, IIO_ST('u', 12, 16, 0), 0),
543 .channel
[2] = IIO_CHAN_SOFT_TIMESTAMP(2),
546 .default_config
= AD7998_ALERT_EN
,
547 .info
= &ad7992_info
,
550 .channel
[0] = IIO_CHAN(IIO_IN
, 0, 1, 0, NULL
, 0, 0,
551 (1 << IIO_CHAN_INFO_SCALE_SHARED
),
552 0, 0, IIO_ST('u', 10, 16, 0), 0),
553 .channel
[1] = IIO_CHAN(IIO_IN
, 0, 1, 0, NULL
, 1, 0,
554 (1 << IIO_CHAN_INFO_SCALE_SHARED
),
555 1, 1, IIO_ST('u', 10, 16, 0), 0),
556 .channel
[2] = IIO_CHAN(IIO_IN
, 0, 1, 0, NULL
, 2, 0,
557 (1 << IIO_CHAN_INFO_SCALE_SHARED
),
558 2, 2, IIO_ST('u', 10, 16, 0), 0),
559 .channel
[3] = IIO_CHAN(IIO_IN
, 0, 1, 0, NULL
, 3, 0,
560 (1 << IIO_CHAN_INFO_SCALE_SHARED
),
561 3, 3, IIO_ST('u', 10, 16, 0), 0),
562 .channel
[4] = IIO_CHAN_SOFT_TIMESTAMP(4),
565 .default_config
= AD7998_ALERT_EN
,
566 .info
= &ad7993_4_7_8_info
,
569 .channel
[0] = IIO_CHAN(IIO_IN
, 0, 1, 0, NULL
, 0, 0,
570 (1 << IIO_CHAN_INFO_SCALE_SHARED
),
571 0, 0, IIO_ST('u', 12, 16, 0), 0),
572 .channel
[1] = IIO_CHAN(IIO_IN
, 0, 1, 0, NULL
, 1, 0,
573 (1 << IIO_CHAN_INFO_SCALE_SHARED
),
574 1, 1, IIO_ST('u', 12, 16, 0), 0),
575 .channel
[2] = IIO_CHAN(IIO_IN
, 0, 1, 0, NULL
, 2, 0,
576 (1 << IIO_CHAN_INFO_SCALE_SHARED
),
577 2, 2, IIO_ST('u', 12, 16, 0), 0),
578 .channel
[3] = IIO_CHAN(IIO_IN
, 0, 1, 0, NULL
, 3, 0,
579 (1 << IIO_CHAN_INFO_SCALE_SHARED
),
580 3, 3, IIO_ST('u', 12, 16, 0), 0),
581 .channel
[4] = IIO_CHAN_SOFT_TIMESTAMP(4),
584 .default_config
= AD7998_ALERT_EN
,
585 .info
= &ad7993_4_7_8_info
,
588 .channel
[0] = IIO_CHAN(IIO_IN
, 0, 1, 0, NULL
, 0, 0,
589 (1 << IIO_CHAN_INFO_SCALE_SHARED
),
590 0, 0, IIO_ST('u', 10, 16, 0), 0),
591 .channel
[1] = IIO_CHAN(IIO_IN
, 0, 1, 0, NULL
, 1, 0,
592 (1 << IIO_CHAN_INFO_SCALE_SHARED
),
593 1, 1, IIO_ST('u', 10, 16, 0), 0),
594 .channel
[2] = IIO_CHAN(IIO_IN
, 0, 1, 0, NULL
, 2, 0,
595 (1 << IIO_CHAN_INFO_SCALE_SHARED
),
596 2, 2, IIO_ST('u', 10, 16, 0), 0),
597 .channel
[3] = IIO_CHAN(IIO_IN
, 0, 1, 0, NULL
, 3, 0,
598 (1 << IIO_CHAN_INFO_SCALE_SHARED
),
599 3, 3, IIO_ST('u', 10, 16, 0), 0),
600 .channel
[4] = IIO_CHAN(IIO_IN
, 0, 1, 0, NULL
, 4, 0,
601 (1 << IIO_CHAN_INFO_SCALE_SHARED
),
602 4, 4, IIO_ST('u', 10, 16, 0), 0),
603 .channel
[5] = IIO_CHAN(IIO_IN
, 0, 1, 0, NULL
, 5, 0,
604 (1 << IIO_CHAN_INFO_SCALE_SHARED
),
605 5, 5, IIO_ST('u', 10, 16, 0), 0),
606 .channel
[6] = IIO_CHAN(IIO_IN
, 0, 1, 0, NULL
, 6, 0,
607 (1 << IIO_CHAN_INFO_SCALE_SHARED
),
608 6, 6, IIO_ST('u', 10, 16, 0), 0),
609 .channel
[7] = IIO_CHAN(IIO_IN
, 0, 1, 0, NULL
, 7, 0,
610 (1 << IIO_CHAN_INFO_SCALE_SHARED
),
611 7, 7, IIO_ST('u', 10, 16, 0), 0),
612 .channel
[8] = IIO_CHAN_SOFT_TIMESTAMP(8),
615 .default_config
= AD7998_ALERT_EN
,
616 .info
= &ad7993_4_7_8_info
,
619 .channel
[0] = IIO_CHAN(IIO_IN
, 0, 1, 0, NULL
, 0, 0,
620 (1 << IIO_CHAN_INFO_SCALE_SHARED
),
621 0, 0, IIO_ST('u', 12, 16, 0), 0),
622 .channel
[1] = IIO_CHAN(IIO_IN
, 0, 1, 0, NULL
, 1, 0,
623 (1 << IIO_CHAN_INFO_SCALE_SHARED
),
624 1, 1, IIO_ST('u', 12, 16, 0), 0),
625 .channel
[2] = IIO_CHAN(IIO_IN
, 0, 1, 0, NULL
, 2, 0,
626 (1 << IIO_CHAN_INFO_SCALE_SHARED
),
627 2, 2, IIO_ST('u', 12, 16, 0), 0),
628 .channel
[3] = IIO_CHAN(IIO_IN
, 0, 1, 0, NULL
, 3, 0,
629 (1 << IIO_CHAN_INFO_SCALE_SHARED
),
630 3, 3, IIO_ST('u', 12, 16, 0), 0),
631 .channel
[4] = IIO_CHAN(IIO_IN
, 0, 1, 0, NULL
, 4, 0,
632 (1 << IIO_CHAN_INFO_SCALE_SHARED
),
633 4, 4, IIO_ST('u', 12, 16, 0), 0),
634 .channel
[5] = IIO_CHAN(IIO_IN
, 0, 1, 0, NULL
, 5, 0,
635 (1 << IIO_CHAN_INFO_SCALE_SHARED
),
636 5, 5, IIO_ST('u', 12, 16, 0), 0),
637 .channel
[6] = IIO_CHAN(IIO_IN
, 0, 1, 0, NULL
, 6, 0,
638 (1 << IIO_CHAN_INFO_SCALE_SHARED
),
639 6, 6, IIO_ST('u', 12, 16, 0), 0),
640 .channel
[7] = IIO_CHAN(IIO_IN
, 0, 1, 0, NULL
, 7, 0,
641 (1 << IIO_CHAN_INFO_SCALE_SHARED
),
642 7, 7, IIO_ST('u', 12, 16, 0), 0),
643 .channel
[8] = IIO_CHAN_SOFT_TIMESTAMP(8),
646 .default_config
= AD7998_ALERT_EN
,
647 .info
= &ad7993_4_7_8_info
,
651 static int __devinit
ad799x_probe(struct i2c_client
*client
,
652 const struct i2c_device_id
*id
)
654 int ret
, regdone
= 0;
655 struct ad799x_platform_data
*pdata
= client
->dev
.platform_data
;
656 struct ad799x_state
*st
;
657 struct iio_dev
*indio_dev
= iio_allocate_device(sizeof(*st
));
659 if (indio_dev
== NULL
)
662 st
= iio_priv(indio_dev
);
663 /* this is only used for device removal purposes */
664 i2c_set_clientdata(client
, indio_dev
);
666 st
->id
= id
->driver_data
;
667 st
->chip_info
= &ad799x_chip_info_tbl
[st
->id
];
668 st
->config
= st
->chip_info
->default_config
;
670 /* TODO: Add pdata options for filtering and bit delay */
673 st
->int_vref_mv
= pdata
->vref_mv
;
675 st
->int_vref_mv
= st
->chip_info
->int_vref_mv
;
677 st
->reg
= regulator_get(&client
->dev
, "vcc");
678 if (!IS_ERR(st
->reg
)) {
679 ret
= regulator_enable(st
->reg
);
685 indio_dev
->dev
.parent
= &client
->dev
;
686 indio_dev
->name
= id
->name
;
687 indio_dev
->info
= st
->chip_info
->info
;
688 indio_dev
->name
= id
->name
;
690 indio_dev
->modes
= INDIO_DIRECT_MODE
;
691 indio_dev
->channels
= st
->chip_info
->channel
;
692 indio_dev
->num_channels
= st
->chip_info
->num_channels
;
694 ret
= ad799x_register_ring_funcs_and_init(indio_dev
);
696 goto error_disable_reg
;
698 ret
= iio_device_register(indio_dev
);
700 goto error_cleanup_ring
;
703 ret
= iio_ring_buffer_register_ex(indio_dev
->ring
, 0,
705 indio_dev
->num_channels
);
707 goto error_cleanup_ring
;
709 if (client
->irq
> 0) {
710 ret
= request_threaded_irq(client
->irq
,
712 ad799x_event_handler
,
713 IRQF_TRIGGER_FALLING
|
718 goto error_cleanup_ring
;
724 ad799x_ring_cleanup(indio_dev
);
726 if (!IS_ERR(st
->reg
))
727 regulator_disable(st
->reg
);
729 if (!IS_ERR(st
->reg
))
730 regulator_put(st
->reg
);
732 iio_device_unregister(indio_dev
);
734 iio_free_device(indio_dev
);
739 static __devexit
int ad799x_remove(struct i2c_client
*client
)
741 struct iio_dev
*indio_dev
= i2c_get_clientdata(client
);
742 struct ad799x_state
*st
= iio_priv(indio_dev
);
745 free_irq(client
->irq
, indio_dev
);
747 iio_ring_buffer_unregister(indio_dev
->ring
);
748 ad799x_ring_cleanup(indio_dev
);
749 if (!IS_ERR(st
->reg
)) {
750 regulator_disable(st
->reg
);
751 regulator_put(st
->reg
);
753 iio_device_unregister(indio_dev
);
758 static const struct i2c_device_id ad799x_id
[] = {
759 { "ad7991", ad7991
},
760 { "ad7995", ad7995
},
761 { "ad7999", ad7999
},
762 { "ad7992", ad7992
},
763 { "ad7993", ad7993
},
764 { "ad7994", ad7994
},
765 { "ad7997", ad7997
},
766 { "ad7998", ad7998
},
770 MODULE_DEVICE_TABLE(i2c
, ad799x_id
);
772 static struct i2c_driver ad799x_driver
= {
776 .probe
= ad799x_probe
,
777 .remove
= __devexit_p(ad799x_remove
),
778 .id_table
= ad799x_id
,
781 static __init
int ad799x_init(void)
783 return i2c_add_driver(&ad799x_driver
);
786 static __exit
void ad799x_exit(void)
788 i2c_del_driver(&ad799x_driver
);
791 MODULE_AUTHOR("Michael Hennerich <hennerich@blackfin.uclinux.org>");
792 MODULE_DESCRIPTION("Analog Devices AD799x ADC");
793 MODULE_LICENSE("GPL v2");
794 MODULE_ALIAS("i2c:ad799x");
796 module_init(ad799x_init
);
797 module_exit(ad799x_exit
);