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.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>
27 #define AD7780_RDY (1 << 7)
28 #define AD7780_FILTER (1 << 6)
29 #define AD7780_ERR (1 << 5)
30 #define AD7780_ID1 (1 << 4)
31 #define AD7780_ID0 (1 << 3)
32 #define AD7780_GAIN (1 << 2)
33 #define AD7780_PAT1 (1 << 1)
34 #define AD7780_PAT0 (1 << 0)
36 struct ad7780_chip_info {
37 struct iio_chan_spec channel;
38 unsigned int pattern_mask;
43 const struct ad7780_chip_info *chip_info;
44 struct regulator *reg;
49 struct ad_sigma_delta sd;
52 enum ad7780_supported_device_ids {
59 static struct ad7780_state *ad_sigma_delta_to_ad7780(struct ad_sigma_delta *sd)
61 return container_of(sd, struct ad7780_state, sd);
64 static int ad7780_set_mode(struct ad_sigma_delta *sigma_delta,
65 enum ad_sigma_delta_mode mode)
67 struct ad7780_state *st = ad_sigma_delta_to_ad7780(sigma_delta);
71 case AD_SD_MODE_SINGLE:
72 case AD_SD_MODE_CONTINUOUS:
80 if (gpio_is_valid(st->powerdown_gpio))
81 gpio_set_value(st->powerdown_gpio, val);
86 static int ad7780_read_raw(struct iio_dev *indio_dev,
87 struct iio_chan_spec const *chan,
92 struct ad7780_state *st = iio_priv(indio_dev);
95 case IIO_CHAN_INFO_RAW:
96 return ad_sigma_delta_single_conversion(indio_dev, chan, val);
97 case IIO_CHAN_INFO_SCALE:
98 *val = st->int_vref_mv * st->gain;
99 *val2 = chan->scan_type.realbits - 1;
100 return IIO_VAL_FRACTIONAL_LOG2;
101 case IIO_CHAN_INFO_OFFSET:
102 *val -= (1 << (chan->scan_type.realbits - 1));
109 static int ad7780_postprocess_sample(struct ad_sigma_delta *sigma_delta,
110 unsigned int raw_sample)
112 struct ad7780_state *st = ad_sigma_delta_to_ad7780(sigma_delta);
113 const struct ad7780_chip_info *chip_info = st->chip_info;
115 if ((raw_sample & AD7780_ERR) ||
116 ((raw_sample & chip_info->pattern_mask) != chip_info->pattern))
119 if (raw_sample & AD7780_GAIN)
127 static const struct ad_sigma_delta_info ad7780_sigma_delta_info = {
128 .set_mode = ad7780_set_mode,
129 .postprocess_sample = ad7780_postprocess_sample,
130 .has_registers = false,
133 #define AD7780_CHANNEL(bits, wordsize) \
134 AD_SD_CHANNEL(1, 0, 0, bits, 32, wordsize - bits)
136 static const struct ad7780_chip_info ad7780_chip_info_tbl[] = {
138 .channel = AD7780_CHANNEL(12, 24),
143 .channel = AD7780_CHANNEL(16, 24),
148 .channel = AD7780_CHANNEL(24, 32),
153 .channel = AD7780_CHANNEL(20, 32),
159 static const struct iio_info ad7780_info = {
160 .read_raw = &ad7780_read_raw,
161 .driver_module = THIS_MODULE,
164 static int ad7780_probe(struct spi_device *spi)
166 struct ad7780_platform_data *pdata = spi->dev.platform_data;
167 struct ad7780_state *st;
168 struct iio_dev *indio_dev;
169 int ret, voltage_uv = 0;
171 indio_dev = devm_iio_device_alloc(&spi->dev, sizeof(*st));
172 if (indio_dev == NULL)
175 st = iio_priv(indio_dev);
178 ad_sd_init(&st->sd, indio_dev, spi, &ad7780_sigma_delta_info);
180 st->reg = devm_regulator_get(&spi->dev, "vcc");
181 if (!IS_ERR(st->reg)) {
182 ret = regulator_enable(st->reg);
186 voltage_uv = regulator_get_voltage(st->reg);
190 &ad7780_chip_info_tbl[spi_get_device_id(spi)->driver_data];
192 if (pdata && pdata->vref_mv)
193 st->int_vref_mv = pdata->vref_mv;
195 st->int_vref_mv = voltage_uv / 1000;
197 dev_warn(&spi->dev, "reference voltage unspecified\n");
199 spi_set_drvdata(spi, indio_dev);
201 indio_dev->dev.parent = &spi->dev;
202 indio_dev->name = spi_get_device_id(spi)->name;
203 indio_dev->modes = INDIO_DIRECT_MODE;
204 indio_dev->channels = &st->chip_info->channel;
205 indio_dev->num_channels = 1;
206 indio_dev->info = &ad7780_info;
208 if (pdata && gpio_is_valid(pdata->gpio_pdrst)) {
210 ret = devm_gpio_request_one(&spi->dev, pdata->gpio_pdrst,
211 GPIOF_OUT_INIT_LOW, "AD7780 /PDRST");
213 dev_err(&spi->dev, "failed to request GPIO PDRST\n");
214 goto error_disable_reg;
216 st->powerdown_gpio = pdata->gpio_pdrst;
218 st->powerdown_gpio = -1;
221 ret = ad_sd_setup_buffer_and_trigger(indio_dev);
223 goto error_disable_reg;
225 ret = iio_device_register(indio_dev);
227 goto error_cleanup_buffer_and_trigger;
231 error_cleanup_buffer_and_trigger:
232 ad_sd_cleanup_buffer_and_trigger(indio_dev);
234 if (!IS_ERR(st->reg))
235 regulator_disable(st->reg);
240 static int ad7780_remove(struct spi_device *spi)
242 struct iio_dev *indio_dev = spi_get_drvdata(spi);
243 struct ad7780_state *st = iio_priv(indio_dev);
245 iio_device_unregister(indio_dev);
246 ad_sd_cleanup_buffer_and_trigger(indio_dev);
248 if (!IS_ERR(st->reg))
249 regulator_disable(st->reg);
254 static const struct spi_device_id ad7780_id[] = {
255 {"ad7170", ID_AD7170},
256 {"ad7171", ID_AD7171},
257 {"ad7780", ID_AD7780},
258 {"ad7781", ID_AD7781},
261 MODULE_DEVICE_TABLE(spi, ad7780_id);
263 static struct spi_driver ad7780_driver = {
266 .owner = THIS_MODULE,
268 .probe = ad7780_probe,
269 .remove = ad7780_remove,
270 .id_table = ad7780_id,
272 module_spi_driver(ad7780_driver);
274 MODULE_AUTHOR("Michael Hennerich <hennerich@blackfin.uclinux.org>");
275 MODULE_DESCRIPTION("Analog Devices AD7780 and similar ADCs");
276 MODULE_LICENSE("GPL v2");