2 * AD7170/AD7171 and AD7780/AD7781 SPI ADC driver
4 * Copyright 2011 Analog Devices Inc.
6 * Licensed under the GPL-2.
9 #include <linux/interrupt.h>
10 #include <linux/device.h>
11 #include <linux/kernel.h>
12 #include <linux/slab.h>
13 #include <linux/sysfs.h>
14 #include <linux/spi/spi.h>
15 #include <linux/regulator/consumer.h>
16 #include <linux/err.h>
17 #include <linux/sched.h>
18 #include <linux/gpio/consumer.h>
19 #include <linux/module.h>
21 #include <linux/iio/iio.h>
22 #include <linux/iio/sysfs.h>
23 #include <linux/iio/adc/ad_sigma_delta.h>
25 #define AD7780_RDY BIT(7)
26 #define AD7780_FILTER BIT(6)
27 #define AD7780_ERR BIT(5)
28 #define AD7780_ID1 BIT(4)
29 #define AD7780_ID0 BIT(3)
30 #define AD7780_GAIN BIT(2)
31 #define AD7780_PAT1 BIT(1)
32 #define AD7780_PAT0 BIT(0)
34 struct ad7780_chip_info
{
35 struct iio_chan_spec channel
;
36 unsigned int pattern_mask
;
41 const struct ad7780_chip_info
*chip_info
;
42 struct regulator
*reg
;
43 struct gpio_desc
*powerdown_gpio
;
47 struct ad_sigma_delta sd
;
50 enum ad7780_supported_device_ids
{
57 static struct ad7780_state
*ad_sigma_delta_to_ad7780(struct ad_sigma_delta
*sd
)
59 return container_of(sd
, struct ad7780_state
, sd
);
62 static int ad7780_set_mode(struct ad_sigma_delta
*sigma_delta
,
63 enum ad_sigma_delta_mode mode
)
65 struct ad7780_state
*st
= ad_sigma_delta_to_ad7780(sigma_delta
);
69 case AD_SD_MODE_SINGLE
:
70 case AD_SD_MODE_CONTINUOUS
:
78 gpiod_set_value(st
->powerdown_gpio
, val
);
83 static int ad7780_read_raw(struct iio_dev
*indio_dev
,
84 struct iio_chan_spec
const *chan
,
89 struct ad7780_state
*st
= iio_priv(indio_dev
);
92 case IIO_CHAN_INFO_RAW
:
93 return ad_sigma_delta_single_conversion(indio_dev
, chan
, val
);
94 case IIO_CHAN_INFO_SCALE
:
95 *val
= st
->int_vref_mv
* st
->gain
;
96 *val2
= chan
->scan_type
.realbits
- 1;
97 return IIO_VAL_FRACTIONAL_LOG2
;
98 case IIO_CHAN_INFO_OFFSET
:
99 *val
-= (1 << (chan
->scan_type
.realbits
- 1));
106 static int ad7780_postprocess_sample(struct ad_sigma_delta
*sigma_delta
,
107 unsigned int raw_sample
)
109 struct ad7780_state
*st
= ad_sigma_delta_to_ad7780(sigma_delta
);
110 const struct ad7780_chip_info
*chip_info
= st
->chip_info
;
112 if ((raw_sample
& AD7780_ERR
) ||
113 ((raw_sample
& chip_info
->pattern_mask
) != chip_info
->pattern
))
116 if (raw_sample
& AD7780_GAIN
)
124 static const struct ad_sigma_delta_info ad7780_sigma_delta_info
= {
125 .set_mode
= ad7780_set_mode
,
126 .postprocess_sample
= ad7780_postprocess_sample
,
127 .has_registers
= false,
130 #define AD7780_CHANNEL(bits, wordsize) \
131 AD_SD_CHANNEL(1, 0, 0, bits, 32, wordsize - bits)
133 static const struct ad7780_chip_info ad7780_chip_info_tbl
[] = {
135 .channel
= AD7780_CHANNEL(12, 24),
140 .channel
= AD7780_CHANNEL(16, 24),
145 .channel
= AD7780_CHANNEL(24, 32),
150 .channel
= AD7780_CHANNEL(20, 32),
156 static const struct iio_info ad7780_info
= {
157 .read_raw
= &ad7780_read_raw
,
158 .driver_module
= THIS_MODULE
,
161 static int ad7780_probe(struct spi_device
*spi
)
163 struct ad7780_state
*st
;
164 struct iio_dev
*indio_dev
;
165 int ret
, voltage_uv
= 0;
167 indio_dev
= devm_iio_device_alloc(&spi
->dev
, sizeof(*st
));
171 st
= iio_priv(indio_dev
);
174 ad_sd_init(&st
->sd
, indio_dev
, spi
, &ad7780_sigma_delta_info
);
176 st
->reg
= devm_regulator_get(&spi
->dev
, "vcc");
177 if (!IS_ERR(st
->reg
)) {
178 ret
= regulator_enable(st
->reg
);
182 voltage_uv
= regulator_get_voltage(st
->reg
);
186 &ad7780_chip_info_tbl
[spi_get_device_id(spi
)->driver_data
];
189 st
->int_vref_mv
= voltage_uv
/ 1000;
191 dev_warn(&spi
->dev
, "Reference voltage unspecified\n");
193 spi_set_drvdata(spi
, indio_dev
);
195 indio_dev
->dev
.parent
= &spi
->dev
;
196 indio_dev
->name
= spi_get_device_id(spi
)->name
;
197 indio_dev
->modes
= INDIO_DIRECT_MODE
;
198 indio_dev
->channels
= &st
->chip_info
->channel
;
199 indio_dev
->num_channels
= 1;
200 indio_dev
->info
= &ad7780_info
;
202 st
->powerdown_gpio
= devm_gpiod_get_optional(&spi
->dev
,
205 if (IS_ERR(st
->powerdown_gpio
)) {
206 ret
= PTR_ERR(st
->powerdown_gpio
);
207 dev_err(&spi
->dev
, "Failed to request powerdown GPIO: %d\n",
209 goto error_disable_reg
;
212 ret
= ad_sd_setup_buffer_and_trigger(indio_dev
);
214 goto error_disable_reg
;
216 ret
= iio_device_register(indio_dev
);
218 goto error_cleanup_buffer_and_trigger
;
222 error_cleanup_buffer_and_trigger
:
223 ad_sd_cleanup_buffer_and_trigger(indio_dev
);
225 if (!IS_ERR(st
->reg
))
226 regulator_disable(st
->reg
);
231 static int ad7780_remove(struct spi_device
*spi
)
233 struct iio_dev
*indio_dev
= spi_get_drvdata(spi
);
234 struct ad7780_state
*st
= iio_priv(indio_dev
);
236 iio_device_unregister(indio_dev
);
237 ad_sd_cleanup_buffer_and_trigger(indio_dev
);
239 if (!IS_ERR(st
->reg
))
240 regulator_disable(st
->reg
);
245 static const struct spi_device_id ad7780_id
[] = {
246 {"ad7170", ID_AD7170
},
247 {"ad7171", ID_AD7171
},
248 {"ad7780", ID_AD7780
},
249 {"ad7781", ID_AD7781
},
252 MODULE_DEVICE_TABLE(spi
, ad7780_id
);
254 static struct spi_driver ad7780_driver
= {
258 .probe
= ad7780_probe
,
259 .remove
= ad7780_remove
,
260 .id_table
= ad7780_id
,
262 module_spi_driver(ad7780_driver
);
264 MODULE_AUTHOR("Michael Hennerich <hennerich@blackfin.uclinux.org>");
265 MODULE_DESCRIPTION("Analog Devices AD7780 and similar ADCs");
266 MODULE_LICENSE("GPL v2");