2 * AD7466/7/8 AD7476/5/7/8 (A) SPI ADC driver
4 * Copyright 2010 Analog Devices Inc.
6 * Licensed under the GPL-2 or later.
9 #include <linux/device.h>
10 #include <linux/kernel.h>
11 #include <linux/slab.h>
12 #include <linux/sysfs.h>
13 #include <linux/spi/spi.h>
14 #include <linux/regulator/consumer.h>
15 #include <linux/err.h>
16 #include <linux/module.h>
20 #include "../buffer.h"
24 static int ad7476_scan_direct(struct ad7476_state *st)
28 ret = spi_sync(st->spi, &st->msg);
32 return (st->data[0] << 8) | st->data[1];
35 static int ad7476_read_raw(struct iio_dev *indio_dev,
36 struct iio_chan_spec const *chan,
42 struct ad7476_state *st = iio_priv(indio_dev);
43 unsigned int scale_uv;
47 mutex_lock(&indio_dev->mlock);
48 if (iio_buffer_enabled(indio_dev))
51 ret = ad7476_scan_direct(st);
52 mutex_unlock(&indio_dev->mlock);
56 *val = (ret >> st->chip_info->channel[0].scan_type.shift) &
57 RES_MASK(st->chip_info->channel[0].scan_type.realbits);
59 case IIO_CHAN_INFO_SCALE:
60 scale_uv = (st->int_vref_mv * 1000)
61 >> st->chip_info->channel[0].scan_type.realbits;
63 *val2 = (scale_uv%1000)*1000;
64 return IIO_VAL_INT_PLUS_MICRO;
69 static const struct ad7476_chip_info ad7476_chip_info_tbl[] = {
71 .channel[0] = IIO_CHAN(IIO_VOLTAGE, 0, 1, 0, NULL, 0, 0,
72 IIO_CHAN_INFO_SCALE_SHARED_BIT,
73 0, 0, IIO_ST('u', 12, 16, 0), 0),
74 .channel[1] = IIO_CHAN_SOFT_TIMESTAMP(1),
77 .channel[0] = IIO_CHAN(IIO_VOLTAGE, 0, 1, 0, NULL, 0, 0,
78 IIO_CHAN_INFO_SCALE_SHARED_BIT,
79 0, 0, IIO_ST('u', 10, 16, 2), 0),
80 .channel[1] = IIO_CHAN_SOFT_TIMESTAMP(1),
83 .channel[0] = IIO_CHAN(IIO_VOLTAGE, 0, 1 , 0, NULL, 0, 0,
84 IIO_CHAN_INFO_SCALE_SHARED_BIT,
85 0, 0, IIO_ST('u', 8, 16, 4), 0),
86 .channel[1] = IIO_CHAN_SOFT_TIMESTAMP(1),
89 .channel[0] = IIO_CHAN(IIO_VOLTAGE, 0, 1, 0, NULL, 0, 0,
90 IIO_CHAN_INFO_SCALE_SHARED_BIT,
91 0, 0, IIO_ST('u', 12, 16, 0), 0),
92 .channel[1] = IIO_CHAN_SOFT_TIMESTAMP(1),
95 .channel[0] = IIO_CHAN(IIO_VOLTAGE, 0, 1, 0, NULL, 0, 0,
96 IIO_CHAN_INFO_SCALE_SHARED_BIT,
97 0, 0, IIO_ST('u', 12, 16, 0), 0),
98 .channel[1] = IIO_CHAN_SOFT_TIMESTAMP(1),
101 .channel[0] = IIO_CHAN(IIO_VOLTAGE, 0, 1, 0, NULL, 0, 0,
102 IIO_CHAN_INFO_SCALE_SHARED_BIT,
103 0, 0, IIO_ST('u', 10, 16, 2), 0),
104 .channel[1] = IIO_CHAN_SOFT_TIMESTAMP(1),
107 .channel[0] = IIO_CHAN(IIO_VOLTAGE, 0, 1, 0, NULL, 0, 0,
108 IIO_CHAN_INFO_SCALE_SHARED_BIT,
109 0, 0, IIO_ST('u', 8, 16, 4), 0),
110 .channel[1] = IIO_CHAN_SOFT_TIMESTAMP(1),
113 .channel[0] = IIO_CHAN(IIO_VOLTAGE, 0, 1, 0, NULL, 0, 0,
114 IIO_CHAN_INFO_SCALE_SHARED_BIT,
115 0, 0, IIO_ST('u', 12, 16, 0), 0),
116 .channel[1] = IIO_CHAN_SOFT_TIMESTAMP(1),
121 static const struct iio_info ad7476_info = {
122 .driver_module = THIS_MODULE,
123 .read_raw = &ad7476_read_raw,
126 static int __devinit ad7476_probe(struct spi_device *spi)
128 struct ad7476_platform_data *pdata = spi->dev.platform_data;
129 struct ad7476_state *st;
130 struct iio_dev *indio_dev;
131 int ret, voltage_uv = 0;
133 indio_dev = iio_allocate_device(sizeof(*st));
134 if (indio_dev == NULL) {
138 st = iio_priv(indio_dev);
139 st->reg = regulator_get(&spi->dev, "vcc");
140 if (!IS_ERR(st->reg)) {
141 ret = regulator_enable(st->reg);
145 voltage_uv = regulator_get_voltage(st->reg);
148 &ad7476_chip_info_tbl[spi_get_device_id(spi)->driver_data];
150 if (st->chip_info->int_vref_mv)
151 st->int_vref_mv = st->chip_info->int_vref_mv;
152 else if (pdata && pdata->vref_mv)
153 st->int_vref_mv = pdata->vref_mv;
155 st->int_vref_mv = voltage_uv / 1000;
157 dev_warn(&spi->dev, "reference voltage unspecified\n");
159 spi_set_drvdata(spi, indio_dev);
163 /* Establish that the iio_dev is a child of the spi device */
164 indio_dev->dev.parent = &spi->dev;
165 indio_dev->name = spi_get_device_id(spi)->name;
166 indio_dev->modes = INDIO_DIRECT_MODE;
167 indio_dev->channels = st->chip_info->channel;
168 indio_dev->num_channels = 2;
169 indio_dev->info = &ad7476_info;
170 /* Setup default message */
172 st->xfer.rx_buf = &st->data;
173 st->xfer.len = st->chip_info->channel[0].scan_type.storagebits / 8;
175 spi_message_init(&st->msg);
176 spi_message_add_tail(&st->xfer, &st->msg);
178 ret = ad7476_register_ring_funcs_and_init(indio_dev);
180 goto error_disable_reg;
182 ret = iio_buffer_register(indio_dev,
183 st->chip_info->channel,
184 ARRAY_SIZE(st->chip_info->channel));
186 goto error_cleanup_ring;
188 ret = iio_device_register(indio_dev);
190 goto error_ring_unregister;
193 error_ring_unregister:
194 iio_buffer_unregister(indio_dev);
196 ad7476_ring_cleanup(indio_dev);
198 if (!IS_ERR(st->reg))
199 regulator_disable(st->reg);
201 if (!IS_ERR(st->reg))
202 regulator_put(st->reg);
203 iio_free_device(indio_dev);
209 static int ad7476_remove(struct spi_device *spi)
211 struct iio_dev *indio_dev = spi_get_drvdata(spi);
212 struct ad7476_state *st = iio_priv(indio_dev);
214 iio_device_unregister(indio_dev);
215 iio_buffer_unregister(indio_dev);
216 ad7476_ring_cleanup(indio_dev);
217 if (!IS_ERR(st->reg)) {
218 regulator_disable(st->reg);
219 regulator_put(st->reg);
221 iio_free_device(indio_dev);
226 static const struct spi_device_id ad7476_id[] = {
227 {"ad7466", ID_AD7466},
228 {"ad7467", ID_AD7467},
229 {"ad7468", ID_AD7468},
230 {"ad7475", ID_AD7475},
231 {"ad7476", ID_AD7476},
232 {"ad7476a", ID_AD7476},
233 {"ad7477", ID_AD7477},
234 {"ad7477a", ID_AD7477},
235 {"ad7478", ID_AD7478},
236 {"ad7478a", ID_AD7478},
237 {"ad7495", ID_AD7495},
240 MODULE_DEVICE_TABLE(spi, ad7476_id);
242 static struct spi_driver ad7476_driver = {
245 .owner = THIS_MODULE,
247 .probe = ad7476_probe,
248 .remove = __devexit_p(ad7476_remove),
249 .id_table = ad7476_id,
251 module_spi_driver(ad7476_driver);
253 MODULE_AUTHOR("Michael Hennerich <hennerich@blackfin.uclinux.org>");
254 MODULE_DESCRIPTION("Analog Devices AD7475/6/7/8(A) AD7466/7/8 ADC");
255 MODULE_LICENSE("GPL v2");