2 * 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>
26 #define AD7780_RDY (1 << 7)
27 #define AD7780_FILTER (1 << 6)
28 #define AD7780_ERR (1 << 5)
29 #define AD7780_ID1 (1 << 4)
30 #define AD7780_ID0 (1 << 3)
31 #define AD7780_GAIN (1 << 2)
32 #define AD7780_PAT1 (1 << 1)
33 #define AD7780_PAT0 (1 << 0)
35 struct ad7780_chip_info {
36 struct iio_chan_spec channel;
40 struct spi_device *spi;
41 const struct ad7780_chip_info *chip_info;
42 struct regulator *reg;
43 struct ad7780_platform_data *pdata;
44 wait_queue_head_t wq_data_avail;
47 struct spi_transfer xfer;
48 struct spi_message msg;
50 * DMA (thus cache coherency maintenance) requires the
51 * transfer buffers to live in their own cache lines.
53 unsigned int data ____cacheline_aligned;
56 enum ad7780_supported_device_ids {
61 static int ad7780_read(struct ad7780_state *st, int *val)
65 spi_bus_lock(st->spi->master);
67 enable_irq(st->spi->irq);
69 gpio_set_value(st->pdata->gpio_pdrst, 1);
71 ret = wait_event_interruptible(st->wq_data_avail, st->done);
72 disable_irq_nosync(st->spi->irq);
76 ret = spi_sync_locked(st->spi, &st->msg);
77 *val = be32_to_cpu(st->data);
79 gpio_set_value(st->pdata->gpio_pdrst, 0);
80 spi_bus_unlock(st->spi->master);
85 static int ad7780_read_raw(struct iio_dev *indio_dev,
86 struct iio_chan_spec const *chan,
91 struct ad7780_state *st = iio_priv(indio_dev);
92 struct iio_chan_spec channel = st->chip_info->channel;
94 unsigned long scale_uv;
97 case IIO_CHAN_INFO_RAW:
98 mutex_lock(&indio_dev->mlock);
99 ret = ad7780_read(st, &smpl);
100 mutex_unlock(&indio_dev->mlock);
105 if ((smpl & AD7780_ERR) ||
106 !((smpl & AD7780_PAT0) && !(smpl & AD7780_PAT1)))
109 *val = (smpl >> channel.scan_type.shift) &
110 ((1 << (channel.scan_type.realbits)) - 1);
111 *val -= (1 << (channel.scan_type.realbits - 1));
113 if (!(smpl & AD7780_GAIN))
117 case IIO_CHAN_INFO_SCALE:
118 scale_uv = (st->int_vref_mv * 100000)
119 >> (channel.scan_type.realbits - 1);
120 *val = scale_uv / 100000;
121 *val2 = (scale_uv % 100000) * 10;
122 return IIO_VAL_INT_PLUS_MICRO;
127 static const struct ad7780_chip_info ad7780_chip_info_tbl[] = {
133 .info_mask = IIO_CHAN_INFO_RAW_SEPARATE_BIT |
134 IIO_CHAN_INFO_SCALE_SHARED_BIT,
148 .info_mask = IIO_CHAN_INFO_RAW_SEPARATE_BIT |
149 IIO_CHAN_INFO_SCALE_SHARED_BIT,
163 static irqreturn_t ad7780_interrupt(int irq, void *dev_id)
165 struct ad7780_state *st = dev_id;
168 wake_up_interruptible(&st->wq_data_avail);
173 static const struct iio_info ad7780_info = {
174 .read_raw = &ad7780_read_raw,
175 .driver_module = THIS_MODULE,
178 static int __devinit ad7780_probe(struct spi_device *spi)
180 struct ad7780_platform_data *pdata = spi->dev.platform_data;
181 struct ad7780_state *st;
182 struct iio_dev *indio_dev;
183 int ret, voltage_uv = 0;
186 dev_dbg(&spi->dev, "no platform data?\n");
190 indio_dev = iio_device_alloc(sizeof(*st));
191 if (indio_dev == NULL)
194 st = iio_priv(indio_dev);
196 st->reg = regulator_get(&spi->dev, "vcc");
197 if (!IS_ERR(st->reg)) {
198 ret = regulator_enable(st->reg);
202 voltage_uv = regulator_get_voltage(st->reg);
206 &ad7780_chip_info_tbl[spi_get_device_id(spi)->driver_data];
210 if (pdata && pdata->vref_mv)
211 st->int_vref_mv = pdata->vref_mv;
213 st->int_vref_mv = voltage_uv / 1000;
215 dev_warn(&spi->dev, "reference voltage unspecified\n");
217 spi_set_drvdata(spi, indio_dev);
220 indio_dev->dev.parent = &spi->dev;
221 indio_dev->name = spi_get_device_id(spi)->name;
222 indio_dev->modes = INDIO_DIRECT_MODE;
223 indio_dev->channels = &st->chip_info->channel;
224 indio_dev->num_channels = 1;
225 indio_dev->info = &ad7780_info;
227 init_waitqueue_head(&st->wq_data_avail);
229 /* Setup default message */
231 st->xfer.rx_buf = &st->data;
232 st->xfer.len = st->chip_info->channel.scan_type.storagebits / 8;
234 spi_message_init(&st->msg);
235 spi_message_add_tail(&st->xfer, &st->msg);
237 ret = gpio_request_one(st->pdata->gpio_pdrst, GPIOF_OUT_INIT_LOW,
240 dev_err(&spi->dev, "failed to request GPIO PDRST\n");
241 goto error_disable_reg;
244 ret = request_irq(spi->irq, ad7780_interrupt,
245 IRQF_TRIGGER_FALLING, spi_get_device_id(spi)->name, st);
247 goto error_free_gpio;
249 disable_irq(spi->irq);
251 ret = iio_device_register(indio_dev);
258 free_irq(spi->irq, st);
260 gpio_free(st->pdata->gpio_pdrst);
262 if (!IS_ERR(st->reg))
263 regulator_disable(st->reg);
265 if (!IS_ERR(st->reg))
266 regulator_put(st->reg);
268 iio_device_free(indio_dev);
273 static int ad7780_remove(struct spi_device *spi)
275 struct iio_dev *indio_dev = spi_get_drvdata(spi);
276 struct ad7780_state *st = iio_priv(indio_dev);
278 iio_device_unregister(indio_dev);
279 free_irq(spi->irq, st);
280 gpio_free(st->pdata->gpio_pdrst);
281 if (!IS_ERR(st->reg)) {
282 regulator_disable(st->reg);
283 regulator_put(st->reg);
285 iio_device_free(indio_dev);
290 static const struct spi_device_id ad7780_id[] = {
291 {"ad7780", ID_AD7780},
292 {"ad7781", ID_AD7781},
295 MODULE_DEVICE_TABLE(spi, ad7780_id);
297 static struct spi_driver ad7780_driver = {
300 .owner = THIS_MODULE,
302 .probe = ad7780_probe,
303 .remove = __devexit_p(ad7780_remove),
304 .id_table = ad7780_id,
306 module_spi_driver(ad7780_driver);
308 MODULE_AUTHOR("Michael Hennerich <hennerich@blackfin.uclinux.org>");
309 MODULE_DESCRIPTION("Analog Devices AD7780/1 ADC");
310 MODULE_LICENSE("GPL v2");