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);
93 unsigned long scale_uv;
96 case IIO_CHAN_INFO_RAW:
97 return ad_sigma_delta_single_conversion(indio_dev, chan, val);
98 case IIO_CHAN_INFO_SCALE:
99 scale_uv = (st->int_vref_mv * 100000 * st->gain)
100 >> (chan->scan_type.realbits - 1);
101 *val = scale_uv / 100000;
102 *val2 = (scale_uv % 100000) * 10;
103 return IIO_VAL_INT_PLUS_MICRO;
104 case IIO_CHAN_INFO_OFFSET:
105 *val -= (1 << (chan->scan_type.realbits - 1));
112 static int ad7780_postprocess_sample(struct ad_sigma_delta *sigma_delta,
113 unsigned int raw_sample)
115 struct ad7780_state *st = ad_sigma_delta_to_ad7780(sigma_delta);
116 const struct ad7780_chip_info *chip_info = st->chip_info;
118 if ((raw_sample & AD7780_ERR) ||
119 ((raw_sample & chip_info->pattern_mask) != chip_info->pattern))
122 if (raw_sample & AD7780_GAIN)
130 static const struct ad_sigma_delta_info ad7780_sigma_delta_info = {
131 .set_mode = ad7780_set_mode,
132 .postprocess_sample = ad7780_postprocess_sample,
133 .has_registers = false,
136 #define AD7780_CHANNEL(bits, wordsize) \
137 AD_SD_CHANNEL(1, 0, 0, bits, 32, wordsize - bits)
139 static const struct ad7780_chip_info ad7780_chip_info_tbl[] = {
141 .channel = AD7780_CHANNEL(12, 24),
146 .channel = AD7780_CHANNEL(16, 24),
151 .channel = AD7780_CHANNEL(24, 32),
156 .channel = AD7780_CHANNEL(20, 32),
162 static const struct iio_info ad7780_info = {
163 .read_raw = &ad7780_read_raw,
164 .driver_module = THIS_MODULE,
167 static int ad7780_probe(struct spi_device *spi)
169 struct ad7780_platform_data *pdata = spi->dev.platform_data;
170 struct ad7780_state *st;
171 struct iio_dev *indio_dev;
172 int ret, voltage_uv = 0;
174 indio_dev = iio_device_alloc(sizeof(*st));
175 if (indio_dev == NULL)
178 st = iio_priv(indio_dev);
181 ad_sd_init(&st->sd, indio_dev, spi, &ad7780_sigma_delta_info);
183 st->reg = regulator_get(&spi->dev, "vcc");
184 if (!IS_ERR(st->reg)) {
185 ret = regulator_enable(st->reg);
189 voltage_uv = regulator_get_voltage(st->reg);
193 &ad7780_chip_info_tbl[spi_get_device_id(spi)->driver_data];
195 if (pdata && pdata->vref_mv)
196 st->int_vref_mv = pdata->vref_mv;
198 st->int_vref_mv = voltage_uv / 1000;
200 dev_warn(&spi->dev, "reference voltage unspecified\n");
202 spi_set_drvdata(spi, indio_dev);
204 indio_dev->dev.parent = &spi->dev;
205 indio_dev->name = spi_get_device_id(spi)->name;
206 indio_dev->modes = INDIO_DIRECT_MODE;
207 indio_dev->channels = &st->chip_info->channel;
208 indio_dev->num_channels = 1;
209 indio_dev->info = &ad7780_info;
211 if (pdata && gpio_is_valid(pdata->gpio_pdrst)) {
213 ret = gpio_request_one(pdata->gpio_pdrst, GPIOF_OUT_INIT_LOW,
216 dev_err(&spi->dev, "failed to request GPIO PDRST\n");
217 goto error_disable_reg;
219 st->powerdown_gpio = pdata->gpio_pdrst;
221 st->powerdown_gpio = -1;
224 ret = ad_sd_setup_buffer_and_trigger(indio_dev);
226 goto error_free_gpio;
228 ret = iio_device_register(indio_dev);
230 goto error_cleanup_buffer_and_trigger;
234 error_cleanup_buffer_and_trigger:
235 ad_sd_cleanup_buffer_and_trigger(indio_dev);
237 if (pdata && gpio_is_valid(pdata->gpio_pdrst))
238 gpio_free(pdata->gpio_pdrst);
240 if (!IS_ERR(st->reg))
241 regulator_disable(st->reg);
243 if (!IS_ERR(st->reg))
244 regulator_put(st->reg);
246 iio_device_free(indio_dev);
251 static int __devexit ad7780_remove(struct spi_device *spi)
253 struct iio_dev *indio_dev = spi_get_drvdata(spi);
254 struct ad7780_state *st = iio_priv(indio_dev);
256 iio_device_unregister(indio_dev);
257 ad_sd_cleanup_buffer_and_trigger(indio_dev);
259 if (gpio_is_valid(st->powerdown_gpio))
260 gpio_free(st->powerdown_gpio);
262 if (!IS_ERR(st->reg)) {
263 regulator_disable(st->reg);
264 regulator_put(st->reg);
266 iio_device_free(indio_dev);
271 static const struct spi_device_id ad7780_id[] = {
272 {"ad7170", ID_AD7170},
273 {"ad7171", ID_AD7171},
274 {"ad7780", ID_AD7780},
275 {"ad7781", ID_AD7781},
278 MODULE_DEVICE_TABLE(spi, ad7780_id);
280 static struct spi_driver ad7780_driver = {
283 .owner = THIS_MODULE,
285 .probe = ad7780_probe,
286 .remove = __devexit_p(ad7780_remove),
287 .id_table = ad7780_id,
289 module_spi_driver(ad7780_driver);
291 MODULE_AUTHOR("Michael Hennerich <hennerich@blackfin.uclinux.org>");
292 MODULE_DESCRIPTION("Analog Devices AD7780 and similar ADCs");
293 MODULE_LICENSE("GPL v2");