staging:iio: rename ring_generic.h -> buffer_generic.h
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / staging / iio / adc / ad7780.c
blob4b6f8c6640ead903ace3d60cb0350449851fdf92
1 /*
2 * AD7780/AD7781 SPI ADC driver
4 * Copyright 2011 Analog Devices Inc.
6 * Licensed under the GPL-2.
7 */
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 "../iio.h"
22 #include "../sysfs.h"
24 #include "ad7780.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;
39 struct ad7780_state {
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;
45 bool done;
46 u16 int_vref_mv;
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 {
57 ID_AD7780,
58 ID_AD7781,
61 static int ad7780_read(struct ad7780_state *st, int *val)
63 int ret;
65 spi_bus_lock(st->spi->master);
67 enable_irq(st->spi->irq);
68 st->done = false;
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);
73 if (ret)
74 goto out;
76 ret = spi_sync_locked(st->spi, &st->msg);
77 *val = be32_to_cpu(st->data);
78 out:
79 gpio_set_value(st->pdata->gpio_pdrst, 0);
80 spi_bus_unlock(st->spi->master);
82 return ret;
85 static int ad7780_read_raw(struct iio_dev *indio_dev,
86 struct iio_chan_spec const *chan,
87 int *val,
88 int *val2,
89 long m)
91 struct ad7780_state *st = iio_priv(indio_dev);
92 struct iio_chan_spec channel = st->chip_info->channel;
93 int ret, smpl = 0;
94 unsigned long scale_uv;
96 switch (m) {
97 case 0:
98 mutex_lock(&indio_dev->mlock);
99 ret = ad7780_read(st, &smpl);
100 mutex_unlock(&indio_dev->mlock);
102 if (ret < 0)
103 return ret;
105 if ((smpl & AD7780_ERR) ||
106 !((smpl & AD7780_PAT0) && !(smpl & AD7780_PAT1)))
107 return -EIO;
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))
114 *val *= 128;
116 return IIO_VAL_INT;
117 case (1 << IIO_CHAN_INFO_SCALE_SHARED):
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;
124 return -EINVAL;
127 static const struct ad7780_chip_info ad7780_chip_info_tbl[] = {
128 [ID_AD7780] = {
129 .channel = IIO_CHAN(IIO_IN, 0, 1, 0, NULL, 0, 0,
130 (1 << IIO_CHAN_INFO_SCALE_SHARED),
131 0, 0, IIO_ST('s', 24, 32, 8), 0),
133 [ID_AD7781] = {
134 .channel = IIO_CHAN(IIO_IN, 0, 1, 0, NULL, 0, 0,
135 (1 << IIO_CHAN_INFO_SCALE_SHARED),
136 0, 0, IIO_ST('s', 20, 32, 12), 0),
141 * Interrupt handler
143 static irqreturn_t ad7780_interrupt(int irq, void *dev_id)
145 struct ad7780_state *st = dev_id;
147 st->done = true;
148 wake_up_interruptible(&st->wq_data_avail);
150 return IRQ_HANDLED;
153 static const struct iio_info ad7780_info = {
154 .read_raw = &ad7780_read_raw,
155 .driver_module = THIS_MODULE,
158 static int __devinit ad7780_probe(struct spi_device *spi)
160 struct ad7780_platform_data *pdata = spi->dev.platform_data;
161 struct ad7780_state *st;
162 struct iio_dev *indio_dev;
163 int ret, voltage_uv = 0;
165 if (!pdata) {
166 dev_dbg(&spi->dev, "no platform data?\n");
167 return -ENODEV;
170 indio_dev = iio_allocate_device(sizeof(*st));
171 if (indio_dev == NULL)
172 return -ENOMEM;
174 st = iio_priv(indio_dev);
176 st->reg = regulator_get(&spi->dev, "vcc");
177 if (!IS_ERR(st->reg)) {
178 ret = regulator_enable(st->reg);
179 if (ret)
180 goto error_put_reg;
182 voltage_uv = regulator_get_voltage(st->reg);
185 st->chip_info =
186 &ad7780_chip_info_tbl[spi_get_device_id(spi)->driver_data];
188 st->pdata = pdata;
190 if (pdata && pdata->vref_mv)
191 st->int_vref_mv = pdata->vref_mv;
192 else if (voltage_uv)
193 st->int_vref_mv = voltage_uv / 1000;
194 else
195 dev_warn(&spi->dev, "reference voltage unspecified\n");
197 spi_set_drvdata(spi, indio_dev);
198 st->spi = spi;
200 indio_dev->dev.parent = &spi->dev;
201 indio_dev->name = spi_get_device_id(spi)->name;
202 indio_dev->modes = INDIO_DIRECT_MODE;
203 indio_dev->channels = &st->chip_info->channel;
204 indio_dev->num_channels = 1;
205 indio_dev->info = &ad7780_info;
207 init_waitqueue_head(&st->wq_data_avail);
209 /* Setup default message */
211 st->xfer.rx_buf = &st->data;
212 st->xfer.len = st->chip_info->channel.scan_type.storagebits / 8;
214 spi_message_init(&st->msg);
215 spi_message_add_tail(&st->xfer, &st->msg);
217 ret = gpio_request_one(st->pdata->gpio_pdrst, GPIOF_OUT_INIT_LOW,
218 "AD7780 /PDRST");
219 if (ret) {
220 dev_err(&spi->dev, "failed to request GPIO PDRST\n");
221 goto error_disable_reg;
224 ret = request_irq(spi->irq, ad7780_interrupt,
225 IRQF_TRIGGER_FALLING, spi_get_device_id(spi)->name, st);
226 if (ret)
227 goto error_free_gpio;
229 disable_irq(spi->irq);
231 ret = iio_device_register(indio_dev);
232 if (ret)
233 goto error_free_irq;
235 return 0;
237 error_free_irq:
238 free_irq(spi->irq, st);
239 error_free_gpio:
240 gpio_free(st->pdata->gpio_pdrst);
241 error_disable_reg:
242 if (!IS_ERR(st->reg))
243 regulator_disable(st->reg);
244 error_put_reg:
245 if (!IS_ERR(st->reg))
246 regulator_put(st->reg);
248 iio_free_device(indio_dev);
250 return ret;
253 static int ad7780_remove(struct spi_device *spi)
255 struct iio_dev *indio_dev = spi_get_drvdata(spi);
256 struct ad7780_state *st = iio_priv(indio_dev);
258 free_irq(spi->irq, st);
259 gpio_free(st->pdata->gpio_pdrst);
260 if (!IS_ERR(st->reg)) {
261 regulator_disable(st->reg);
262 regulator_put(st->reg);
264 iio_device_unregister(indio_dev);
266 return 0;
269 static const struct spi_device_id ad7780_id[] = {
270 {"ad7780", ID_AD7780},
271 {"ad7781", ID_AD7781},
275 static struct spi_driver ad7780_driver = {
276 .driver = {
277 .name = "ad7780",
278 .bus = &spi_bus_type,
279 .owner = THIS_MODULE,
281 .probe = ad7780_probe,
282 .remove = __devexit_p(ad7780_remove),
283 .id_table = ad7780_id,
286 static int __init ad7780_init(void)
288 return spi_register_driver(&ad7780_driver);
290 module_init(ad7780_init);
292 static void __exit ad7780_exit(void)
294 spi_unregister_driver(&ad7780_driver);
296 module_exit(ad7780_exit);
298 MODULE_AUTHOR("Michael Hennerich <hennerich@blackfin.uclinux.org>");
299 MODULE_DESCRIPTION("Analog Devices AD7780/1 ADC");
300 MODULE_LICENSE("GPL v2");