1 // SPDX-License-Identifier: GPL-2.0-only
4 * Copyright (C) 2010-2011 Michael Hennerich, Analog Devices Inc.
6 * based on iio/adc/max1363
7 * Copyright (C) 2008-2010 Jonathan Cameron
9 * based on linux/drivers/i2c/chips/max123x
10 * Copyright (C) 2002-2004 Stefan Eletzhofer
12 * based on linux/drivers/acron/char/pcf8583.c
13 * Copyright (C) 2000 Russell King
17 * Support for ad7991, ad7995, ad7999, ad7992, ad7993, ad7994, ad7997,
18 * ad7998 and similar chips.
21 #include <linux/interrupt.h>
22 #include <linux/device.h>
23 #include <linux/kernel.h>
24 #include <linux/sysfs.h>
25 #include <linux/i2c.h>
26 #include <linux/regulator/consumer.h>
27 #include <linux/slab.h>
28 #include <linux/types.h>
29 #include <linux/err.h>
30 #include <linux/module.h>
31 #include <linux/bitops.h>
33 #include <linux/iio/iio.h>
34 #include <linux/iio/sysfs.h>
35 #include <linux/iio/events.h>
36 #include <linux/iio/buffer.h>
37 #include <linux/iio/trigger_consumer.h>
38 #include <linux/iio/triggered_buffer.h>
40 #define AD799X_CHANNEL_SHIFT 4
43 * AD7991, AD7995 and AD7999 defines
46 #define AD7991_REF_SEL 0x08
47 #define AD7991_FLTR 0x04
48 #define AD7991_BIT_TRIAL_DELAY 0x02
49 #define AD7991_SAMPLE_DELAY 0x01
52 * AD7992, AD7993, AD7994, AD7997 and AD7998 defines
55 #define AD7998_FLTR BIT(3)
56 #define AD7998_ALERT_EN BIT(2)
57 #define AD7998_BUSY_ALERT BIT(1)
58 #define AD7998_BUSY_ALERT_POL BIT(0)
60 #define AD7998_CONV_RES_REG 0x0
61 #define AD7998_ALERT_STAT_REG 0x1
62 #define AD7998_CONF_REG 0x2
63 #define AD7998_CYCLE_TMR_REG 0x3
65 #define AD7998_DATALOW_REG(x) ((x) * 3 + 0x4)
66 #define AD7998_DATAHIGH_REG(x) ((x) * 3 + 0x5)
67 #define AD7998_HYST_REG(x) ((x) * 3 + 0x6)
69 #define AD7998_CYC_MASK GENMASK(2, 0)
70 #define AD7998_CYC_DIS 0x0
71 #define AD7998_CYC_TCONF_32 0x1
72 #define AD7998_CYC_TCONF_64 0x2
73 #define AD7998_CYC_TCONF_128 0x3
74 #define AD7998_CYC_TCONF_256 0x4
75 #define AD7998_CYC_TCONF_512 0x5
76 #define AD7998_CYC_TCONF_1024 0x6
77 #define AD7998_CYC_TCONF_2048 0x7
79 #define AD7998_ALERT_STAT_CLEAR 0xFF
82 * AD7997 and AD7997 defines
85 #define AD7997_8_READ_SINGLE BIT(7)
86 #define AD7997_8_READ_SEQUENCE (BIT(6) | BIT(5) | BIT(4))
100 * struct ad799x_chip_config - chip specific information
101 * @channel: channel specification
102 * @default_config: device default configuration
103 * @info: pointer to iio_info struct
105 struct ad799x_chip_config
{
106 const struct iio_chan_spec channel
[9];
108 const struct iio_info
*info
;
112 * struct ad799x_chip_info - chip specific information
113 * @num_channels: number of channels
114 * @noirq_config: device configuration w/o IRQ
115 * @irq_config: device configuration w/IRQ
117 struct ad799x_chip_info
{
119 const struct ad799x_chip_config noirq_config
;
120 const struct ad799x_chip_config irq_config
;
123 struct ad799x_state
{
124 struct i2c_client
*client
;
125 const struct ad799x_chip_config
*chip_config
;
126 struct regulator
*reg
;
127 struct regulator
*vref
;
132 unsigned int transfer_size
;
135 static int ad799x_write_config(struct ad799x_state
*st
, u16 val
)
140 return i2c_smbus_write_word_swapped(st
->client
, AD7998_CONF_REG
,
145 return i2c_smbus_write_byte_data(st
->client
, AD7998_CONF_REG
,
148 /* Will be written when doing a conversion */
154 static int ad799x_read_config(struct ad799x_state
*st
)
159 return i2c_smbus_read_word_swapped(st
->client
, AD7998_CONF_REG
);
163 return i2c_smbus_read_byte_data(st
->client
, AD7998_CONF_REG
);
165 /* No readback support */
170 static int ad799x_update_config(struct ad799x_state
*st
, u16 config
)
174 ret
= ad799x_write_config(st
, config
);
177 ret
= ad799x_read_config(st
);
186 * ad799x_trigger_handler() bh of trigger launched polling to ring buffer
188 * Currently there is no option in this driver to disable the saving of
189 * timestamps within the ring.
191 static irqreturn_t
ad799x_trigger_handler(int irq
, void *p
)
193 struct iio_poll_func
*pf
= p
;
194 struct iio_dev
*indio_dev
= pf
->indio_dev
;
195 struct ad799x_state
*st
= iio_priv(indio_dev
);
204 (*indio_dev
->active_scan_mask
<< AD799X_CHANNEL_SHIFT
);
209 cmd
= (*indio_dev
->active_scan_mask
<< AD799X_CHANNEL_SHIFT
) |
214 cmd
= AD7997_8_READ_SEQUENCE
| AD7998_CONV_RES_REG
;
220 b_sent
= i2c_smbus_read_i2c_block_data(st
->client
,
221 cmd
, st
->transfer_size
, st
->rx_buf
);
225 iio_push_to_buffers_with_timestamp(indio_dev
, st
->rx_buf
,
226 iio_get_time_ns(indio_dev
));
228 iio_trigger_notify_done(indio_dev
->trig
);
233 static int ad799x_update_scan_mode(struct iio_dev
*indio_dev
,
234 const unsigned long *scan_mask
)
236 struct ad799x_state
*st
= iio_priv(indio_dev
);
239 st
->rx_buf
= kmalloc(indio_dev
->scan_bytes
, GFP_KERNEL
);
243 st
->transfer_size
= bitmap_weight(scan_mask
, indio_dev
->masklength
) * 2;
251 st
->config
&= ~(GENMASK(7, 0) << AD799X_CHANNEL_SHIFT
);
252 st
->config
|= (*scan_mask
<< AD799X_CHANNEL_SHIFT
);
253 return ad799x_write_config(st
, st
->config
);
259 static int ad799x_scan_direct(struct ad799x_state
*st
, unsigned ch
)
267 cmd
= st
->config
| (BIT(ch
) << AD799X_CHANNEL_SHIFT
);
272 cmd
= BIT(ch
) << AD799X_CHANNEL_SHIFT
;
276 cmd
= (ch
<< AD799X_CHANNEL_SHIFT
) | AD7997_8_READ_SINGLE
;
282 return i2c_smbus_read_word_swapped(st
->client
, cmd
);
285 static int ad799x_read_raw(struct iio_dev
*indio_dev
,
286 struct iio_chan_spec
const *chan
,
292 struct ad799x_state
*st
= iio_priv(indio_dev
);
295 case IIO_CHAN_INFO_RAW
:
296 ret
= iio_device_claim_direct_mode(indio_dev
);
299 ret
= ad799x_scan_direct(st
, chan
->scan_index
);
300 iio_device_release_direct_mode(indio_dev
);
304 *val
= (ret
>> chan
->scan_type
.shift
) &
305 GENMASK(chan
->scan_type
.realbits
- 1, 0);
307 case IIO_CHAN_INFO_SCALE
:
308 ret
= regulator_get_voltage(st
->vref
);
312 *val2
= chan
->scan_type
.realbits
;
313 return IIO_VAL_FRACTIONAL_LOG2
;
317 static const unsigned int ad7998_frequencies
[] = {
318 [AD7998_CYC_DIS
] = 0,
319 [AD7998_CYC_TCONF_32
] = 15625,
320 [AD7998_CYC_TCONF_64
] = 7812,
321 [AD7998_CYC_TCONF_128
] = 3906,
322 [AD7998_CYC_TCONF_512
] = 976,
323 [AD7998_CYC_TCONF_1024
] = 488,
324 [AD7998_CYC_TCONF_2048
] = 244,
327 static ssize_t
ad799x_read_frequency(struct device
*dev
,
328 struct device_attribute
*attr
,
331 struct iio_dev
*indio_dev
= dev_to_iio_dev(dev
);
332 struct ad799x_state
*st
= iio_priv(indio_dev
);
334 int ret
= i2c_smbus_read_byte_data(st
->client
, AD7998_CYCLE_TMR_REG
);
338 return sprintf(buf
, "%u\n", ad7998_frequencies
[ret
& AD7998_CYC_MASK
]);
341 static ssize_t
ad799x_write_frequency(struct device
*dev
,
342 struct device_attribute
*attr
,
346 struct iio_dev
*indio_dev
= dev_to_iio_dev(dev
);
347 struct ad799x_state
*st
= iio_priv(indio_dev
);
352 ret
= kstrtol(buf
, 10, &val
);
356 mutex_lock(&indio_dev
->mlock
);
357 ret
= i2c_smbus_read_byte_data(st
->client
, AD7998_CYCLE_TMR_REG
);
359 goto error_ret_mutex
;
360 /* Wipe the bits clean */
361 ret
&= ~AD7998_CYC_MASK
;
363 for (i
= 0; i
< ARRAY_SIZE(ad7998_frequencies
); i
++)
364 if (val
== ad7998_frequencies
[i
])
366 if (i
== ARRAY_SIZE(ad7998_frequencies
)) {
368 goto error_ret_mutex
;
371 ret
= i2c_smbus_write_byte_data(st
->client
, AD7998_CYCLE_TMR_REG
,
374 goto error_ret_mutex
;
378 mutex_unlock(&indio_dev
->mlock
);
383 static int ad799x_read_event_config(struct iio_dev
*indio_dev
,
384 const struct iio_chan_spec
*chan
,
385 enum iio_event_type type
,
386 enum iio_event_direction dir
)
388 struct ad799x_state
*st
= iio_priv(indio_dev
);
390 if (!(st
->config
& AD7998_ALERT_EN
))
393 if ((st
->config
>> AD799X_CHANNEL_SHIFT
) & BIT(chan
->scan_index
))
399 static int ad799x_write_event_config(struct iio_dev
*indio_dev
,
400 const struct iio_chan_spec
*chan
,
401 enum iio_event_type type
,
402 enum iio_event_direction dir
,
405 struct ad799x_state
*st
= iio_priv(indio_dev
);
408 ret
= iio_device_claim_direct_mode(indio_dev
);
413 st
->config
|= BIT(chan
->scan_index
) << AD799X_CHANNEL_SHIFT
;
415 st
->config
&= ~(BIT(chan
->scan_index
) << AD799X_CHANNEL_SHIFT
);
417 if (st
->config
>> AD799X_CHANNEL_SHIFT
)
418 st
->config
|= AD7998_ALERT_EN
;
420 st
->config
&= ~AD7998_ALERT_EN
;
422 ret
= ad799x_write_config(st
, st
->config
);
423 iio_device_release_direct_mode(indio_dev
);
427 static unsigned int ad799x_threshold_reg(const struct iio_chan_spec
*chan
,
428 enum iio_event_direction dir
,
429 enum iio_event_info info
)
432 case IIO_EV_INFO_VALUE
:
433 if (dir
== IIO_EV_DIR_FALLING
)
434 return AD7998_DATALOW_REG(chan
->channel
);
436 return AD7998_DATAHIGH_REG(chan
->channel
);
437 case IIO_EV_INFO_HYSTERESIS
:
438 return AD7998_HYST_REG(chan
->channel
);
446 static int ad799x_write_event_value(struct iio_dev
*indio_dev
,
447 const struct iio_chan_spec
*chan
,
448 enum iio_event_type type
,
449 enum iio_event_direction dir
,
450 enum iio_event_info info
,
454 struct ad799x_state
*st
= iio_priv(indio_dev
);
456 if (val
< 0 || val
> GENMASK(chan
->scan_type
.realbits
- 1, 0))
459 mutex_lock(&indio_dev
->mlock
);
460 ret
= i2c_smbus_write_word_swapped(st
->client
,
461 ad799x_threshold_reg(chan
, dir
, info
),
462 val
<< chan
->scan_type
.shift
);
463 mutex_unlock(&indio_dev
->mlock
);
468 static int ad799x_read_event_value(struct iio_dev
*indio_dev
,
469 const struct iio_chan_spec
*chan
,
470 enum iio_event_type type
,
471 enum iio_event_direction dir
,
472 enum iio_event_info info
,
476 struct ad799x_state
*st
= iio_priv(indio_dev
);
478 mutex_lock(&indio_dev
->mlock
);
479 ret
= i2c_smbus_read_word_swapped(st
->client
,
480 ad799x_threshold_reg(chan
, dir
, info
));
481 mutex_unlock(&indio_dev
->mlock
);
484 *val
= (ret
>> chan
->scan_type
.shift
) &
485 GENMASK(chan
->scan_type
.realbits
- 1, 0);
490 static irqreturn_t
ad799x_event_handler(int irq
, void *private)
492 struct iio_dev
*indio_dev
= private;
493 struct ad799x_state
*st
= iio_priv(private);
496 ret
= i2c_smbus_read_byte_data(st
->client
, AD7998_ALERT_STAT_REG
);
500 if (i2c_smbus_write_byte_data(st
->client
, AD7998_ALERT_STAT_REG
,
501 AD7998_ALERT_STAT_CLEAR
) < 0)
504 for (i
= 0; i
< 8; i
++) {
506 iio_push_event(indio_dev
,
508 IIO_UNMOD_EVENT_CODE(IIO_VOLTAGE
,
512 IIO_UNMOD_EVENT_CODE(IIO_VOLTAGE
,
516 iio_get_time_ns(indio_dev
));
523 static IIO_DEV_ATTR_SAMP_FREQ(S_IWUSR
| S_IRUGO
,
524 ad799x_read_frequency
,
525 ad799x_write_frequency
);
526 static IIO_CONST_ATTR_SAMP_FREQ_AVAIL("15625 7812 3906 1953 976 488 244 0");
528 static struct attribute
*ad799x_event_attributes
[] = {
529 &iio_dev_attr_sampling_frequency
.dev_attr
.attr
,
530 &iio_const_attr_sampling_frequency_available
.dev_attr
.attr
,
534 static const struct attribute_group ad799x_event_attrs_group
= {
535 .attrs
= ad799x_event_attributes
,
538 static const struct iio_info ad7991_info
= {
539 .read_raw
= &ad799x_read_raw
,
540 .update_scan_mode
= ad799x_update_scan_mode
,
543 static const struct iio_info ad7993_4_7_8_noirq_info
= {
544 .read_raw
= &ad799x_read_raw
,
545 .update_scan_mode
= ad799x_update_scan_mode
,
548 static const struct iio_info ad7993_4_7_8_irq_info
= {
549 .read_raw
= &ad799x_read_raw
,
550 .event_attrs
= &ad799x_event_attrs_group
,
551 .read_event_config
= &ad799x_read_event_config
,
552 .write_event_config
= &ad799x_write_event_config
,
553 .read_event_value
= &ad799x_read_event_value
,
554 .write_event_value
= &ad799x_write_event_value
,
555 .update_scan_mode
= ad799x_update_scan_mode
,
558 static const struct iio_event_spec ad799x_events
[] = {
560 .type
= IIO_EV_TYPE_THRESH
,
561 .dir
= IIO_EV_DIR_RISING
,
562 .mask_separate
= BIT(IIO_EV_INFO_VALUE
) |
563 BIT(IIO_EV_INFO_ENABLE
),
565 .type
= IIO_EV_TYPE_THRESH
,
566 .dir
= IIO_EV_DIR_FALLING
,
567 .mask_separate
= BIT(IIO_EV_INFO_VALUE
) |
568 BIT(IIO_EV_INFO_ENABLE
),
570 .type
= IIO_EV_TYPE_THRESH
,
571 .dir
= IIO_EV_DIR_EITHER
,
572 .mask_separate
= BIT(IIO_EV_INFO_HYSTERESIS
),
576 #define _AD799X_CHANNEL(_index, _realbits, _ev_spec, _num_ev_spec) { \
577 .type = IIO_VOLTAGE, \
579 .channel = (_index), \
580 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \
581 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE), \
582 .scan_index = (_index), \
585 .realbits = (_realbits), \
587 .shift = 12 - (_realbits), \
588 .endianness = IIO_BE, \
590 .event_spec = _ev_spec, \
591 .num_event_specs = _num_ev_spec, \
594 #define AD799X_CHANNEL(_index, _realbits) \
595 _AD799X_CHANNEL(_index, _realbits, NULL, 0)
597 #define AD799X_CHANNEL_WITH_EVENTS(_index, _realbits) \
598 _AD799X_CHANNEL(_index, _realbits, ad799x_events, \
599 ARRAY_SIZE(ad799x_events))
601 static const struct ad799x_chip_info ad799x_chip_info_tbl
[] = {
606 AD799X_CHANNEL(0, 12),
607 AD799X_CHANNEL(1, 12),
608 AD799X_CHANNEL(2, 12),
609 AD799X_CHANNEL(3, 12),
610 IIO_CHAN_SOFT_TIMESTAMP(4),
612 .info
= &ad7991_info
,
619 AD799X_CHANNEL(0, 10),
620 AD799X_CHANNEL(1, 10),
621 AD799X_CHANNEL(2, 10),
622 AD799X_CHANNEL(3, 10),
623 IIO_CHAN_SOFT_TIMESTAMP(4),
625 .info
= &ad7991_info
,
632 AD799X_CHANNEL(0, 8),
633 AD799X_CHANNEL(1, 8),
634 AD799X_CHANNEL(2, 8),
635 AD799X_CHANNEL(3, 8),
636 IIO_CHAN_SOFT_TIMESTAMP(4),
638 .info
= &ad7991_info
,
645 AD799X_CHANNEL(0, 12),
646 AD799X_CHANNEL(1, 12),
647 IIO_CHAN_SOFT_TIMESTAMP(3),
649 .info
= &ad7993_4_7_8_noirq_info
,
653 AD799X_CHANNEL_WITH_EVENTS(0, 12),
654 AD799X_CHANNEL_WITH_EVENTS(1, 12),
655 IIO_CHAN_SOFT_TIMESTAMP(3),
657 .default_config
= AD7998_ALERT_EN
| AD7998_BUSY_ALERT
,
658 .info
= &ad7993_4_7_8_irq_info
,
665 AD799X_CHANNEL(0, 10),
666 AD799X_CHANNEL(1, 10),
667 AD799X_CHANNEL(2, 10),
668 AD799X_CHANNEL(3, 10),
669 IIO_CHAN_SOFT_TIMESTAMP(4),
671 .info
= &ad7993_4_7_8_noirq_info
,
675 AD799X_CHANNEL_WITH_EVENTS(0, 10),
676 AD799X_CHANNEL_WITH_EVENTS(1, 10),
677 AD799X_CHANNEL_WITH_EVENTS(2, 10),
678 AD799X_CHANNEL_WITH_EVENTS(3, 10),
679 IIO_CHAN_SOFT_TIMESTAMP(4),
681 .default_config
= AD7998_ALERT_EN
| AD7998_BUSY_ALERT
,
682 .info
= &ad7993_4_7_8_irq_info
,
689 AD799X_CHANNEL(0, 12),
690 AD799X_CHANNEL(1, 12),
691 AD799X_CHANNEL(2, 12),
692 AD799X_CHANNEL(3, 12),
693 IIO_CHAN_SOFT_TIMESTAMP(4),
695 .info
= &ad7993_4_7_8_noirq_info
,
699 AD799X_CHANNEL_WITH_EVENTS(0, 12),
700 AD799X_CHANNEL_WITH_EVENTS(1, 12),
701 AD799X_CHANNEL_WITH_EVENTS(2, 12),
702 AD799X_CHANNEL_WITH_EVENTS(3, 12),
703 IIO_CHAN_SOFT_TIMESTAMP(4),
705 .default_config
= AD7998_ALERT_EN
| AD7998_BUSY_ALERT
,
706 .info
= &ad7993_4_7_8_irq_info
,
713 AD799X_CHANNEL(0, 10),
714 AD799X_CHANNEL(1, 10),
715 AD799X_CHANNEL(2, 10),
716 AD799X_CHANNEL(3, 10),
717 AD799X_CHANNEL(4, 10),
718 AD799X_CHANNEL(5, 10),
719 AD799X_CHANNEL(6, 10),
720 AD799X_CHANNEL(7, 10),
721 IIO_CHAN_SOFT_TIMESTAMP(8),
723 .info
= &ad7993_4_7_8_noirq_info
,
727 AD799X_CHANNEL_WITH_EVENTS(0, 10),
728 AD799X_CHANNEL_WITH_EVENTS(1, 10),
729 AD799X_CHANNEL_WITH_EVENTS(2, 10),
730 AD799X_CHANNEL_WITH_EVENTS(3, 10),
731 AD799X_CHANNEL(4, 10),
732 AD799X_CHANNEL(5, 10),
733 AD799X_CHANNEL(6, 10),
734 AD799X_CHANNEL(7, 10),
735 IIO_CHAN_SOFT_TIMESTAMP(8),
737 .default_config
= AD7998_ALERT_EN
| AD7998_BUSY_ALERT
,
738 .info
= &ad7993_4_7_8_irq_info
,
745 AD799X_CHANNEL(0, 12),
746 AD799X_CHANNEL(1, 12),
747 AD799X_CHANNEL(2, 12),
748 AD799X_CHANNEL(3, 12),
749 AD799X_CHANNEL(4, 12),
750 AD799X_CHANNEL(5, 12),
751 AD799X_CHANNEL(6, 12),
752 AD799X_CHANNEL(7, 12),
753 IIO_CHAN_SOFT_TIMESTAMP(8),
755 .info
= &ad7993_4_7_8_noirq_info
,
759 AD799X_CHANNEL_WITH_EVENTS(0, 12),
760 AD799X_CHANNEL_WITH_EVENTS(1, 12),
761 AD799X_CHANNEL_WITH_EVENTS(2, 12),
762 AD799X_CHANNEL_WITH_EVENTS(3, 12),
763 AD799X_CHANNEL(4, 12),
764 AD799X_CHANNEL(5, 12),
765 AD799X_CHANNEL(6, 12),
766 AD799X_CHANNEL(7, 12),
767 IIO_CHAN_SOFT_TIMESTAMP(8),
769 .default_config
= AD7998_ALERT_EN
| AD7998_BUSY_ALERT
,
770 .info
= &ad7993_4_7_8_irq_info
,
775 static int ad799x_probe(struct i2c_client
*client
,
776 const struct i2c_device_id
*id
)
779 struct ad799x_state
*st
;
780 struct iio_dev
*indio_dev
;
781 const struct ad799x_chip_info
*chip_info
=
782 &ad799x_chip_info_tbl
[id
->driver_data
];
784 indio_dev
= devm_iio_device_alloc(&client
->dev
, sizeof(*st
));
785 if (indio_dev
== NULL
)
788 st
= iio_priv(indio_dev
);
789 /* this is only used for device removal purposes */
790 i2c_set_clientdata(client
, indio_dev
);
792 st
->id
= id
->driver_data
;
793 if (client
->irq
> 0 && chip_info
->irq_config
.info
)
794 st
->chip_config
= &chip_info
->irq_config
;
796 st
->chip_config
= &chip_info
->noirq_config
;
798 /* TODO: Add pdata options for filtering and bit delay */
800 st
->reg
= devm_regulator_get(&client
->dev
, "vcc");
802 return PTR_ERR(st
->reg
);
803 ret
= regulator_enable(st
->reg
);
806 st
->vref
= devm_regulator_get(&client
->dev
, "vref");
807 if (IS_ERR(st
->vref
)) {
808 ret
= PTR_ERR(st
->vref
);
809 goto error_disable_reg
;
811 ret
= regulator_enable(st
->vref
);
813 goto error_disable_reg
;
817 indio_dev
->name
= id
->name
;
818 indio_dev
->info
= st
->chip_config
->info
;
820 indio_dev
->modes
= INDIO_DIRECT_MODE
;
821 indio_dev
->channels
= st
->chip_config
->channel
;
822 indio_dev
->num_channels
= chip_info
->num_channels
;
824 ret
= ad799x_update_config(st
, st
->chip_config
->default_config
);
826 goto error_disable_vref
;
828 ret
= iio_triggered_buffer_setup(indio_dev
, NULL
,
829 &ad799x_trigger_handler
, NULL
);
831 goto error_disable_vref
;
833 if (client
->irq
> 0) {
834 ret
= devm_request_threaded_irq(&client
->dev
,
837 ad799x_event_handler
,
838 IRQF_TRIGGER_FALLING
|
843 goto error_cleanup_ring
;
845 ret
= iio_device_register(indio_dev
);
847 goto error_cleanup_ring
;
852 iio_triggered_buffer_cleanup(indio_dev
);
854 regulator_disable(st
->vref
);
856 regulator_disable(st
->reg
);
861 static int ad799x_remove(struct i2c_client
*client
)
863 struct iio_dev
*indio_dev
= i2c_get_clientdata(client
);
864 struct ad799x_state
*st
= iio_priv(indio_dev
);
866 iio_device_unregister(indio_dev
);
868 iio_triggered_buffer_cleanup(indio_dev
);
869 regulator_disable(st
->vref
);
870 regulator_disable(st
->reg
);
876 static int __maybe_unused
ad799x_suspend(struct device
*dev
)
878 struct iio_dev
*indio_dev
= i2c_get_clientdata(to_i2c_client(dev
));
879 struct ad799x_state
*st
= iio_priv(indio_dev
);
881 regulator_disable(st
->vref
);
882 regulator_disable(st
->reg
);
887 static int __maybe_unused
ad799x_resume(struct device
*dev
)
889 struct iio_dev
*indio_dev
= i2c_get_clientdata(to_i2c_client(dev
));
890 struct ad799x_state
*st
= iio_priv(indio_dev
);
893 ret
= regulator_enable(st
->reg
);
895 dev_err(dev
, "Unable to enable vcc regulator\n");
898 ret
= regulator_enable(st
->vref
);
900 regulator_disable(st
->reg
);
901 dev_err(dev
, "Unable to enable vref regulator\n");
906 ret
= ad799x_update_config(st
, st
->config
);
908 regulator_disable(st
->vref
);
909 regulator_disable(st
->reg
);
916 static SIMPLE_DEV_PM_OPS(ad799x_pm_ops
, ad799x_suspend
, ad799x_resume
);
918 static const struct i2c_device_id ad799x_id
[] = {
919 { "ad7991", ad7991
},
920 { "ad7995", ad7995
},
921 { "ad7999", ad7999
},
922 { "ad7992", ad7992
},
923 { "ad7993", ad7993
},
924 { "ad7994", ad7994
},
925 { "ad7997", ad7997
},
926 { "ad7998", ad7998
},
930 MODULE_DEVICE_TABLE(i2c
, ad799x_id
);
932 static struct i2c_driver ad799x_driver
= {
935 .pm
= &ad799x_pm_ops
,
937 .probe
= ad799x_probe
,
938 .remove
= ad799x_remove
,
939 .id_table
= ad799x_id
,
941 module_i2c_driver(ad799x_driver
);
943 MODULE_AUTHOR("Michael Hennerich <michael.hennerich@analog.com>");
944 MODULE_DESCRIPTION("Analog Devices AD799x ADC");
945 MODULE_LICENSE("GPL v2");