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>
18 #include <linux/iio/iio.h>
19 #include <linux/iio/sysfs.h>
20 #include <linux/iio/buffer.h>
21 #include <linux/iio/trigger_consumer.h>
22 #include <linux/iio/triggered_buffer.h>
24 #define RES_MASK(bits) ((1 << (bits)) - 1)
28 struct ad7476_chip_info {
29 unsigned int int_vref_uv;
30 struct iio_chan_spec channel[2];
31 void (*reset)(struct ad7476_state *);
35 struct spi_device *spi;
36 const struct ad7476_chip_info *chip_info;
37 struct regulator *reg;
38 struct spi_transfer xfer;
39 struct spi_message msg;
41 * DMA (thus cache coherency maintenance) requires the
42 * transfer buffers to live in their own cache lines.
43 * Make the buffer large enough for one 16 bit sample and one 64 bit
44 * aligned 64 bit timestamp.
46 unsigned char data[ALIGN(2, sizeof(s64)) + sizeof(s64)]
47 ____cacheline_aligned;
50 enum ad7476_supported_device_ids {
62 static irqreturn_t ad7476_trigger_handler(int irq, void *p)
64 struct iio_poll_func *pf = p;
65 struct iio_dev *indio_dev = pf->indio_dev;
66 struct ad7476_state *st = iio_priv(indio_dev);
70 b_sent = spi_sync(st->spi, &st->msg);
74 time_ns = iio_get_time_ns();
76 if (indio_dev->scan_timestamp)
77 ((s64 *)st->data)[1] = time_ns;
79 iio_push_to_buffers(indio_dev, st->data);
81 iio_trigger_notify_done(indio_dev->trig);
86 static void ad7091_reset(struct ad7476_state *st)
88 /* Any transfers with 8 scl cycles will reset the device */
89 spi_read(st->spi, st->data, 1);
92 static int ad7476_scan_direct(struct ad7476_state *st)
96 ret = spi_sync(st->spi, &st->msg);
100 return be16_to_cpup((__be16 *)st->data);
103 static int ad7476_read_raw(struct iio_dev *indio_dev,
104 struct iio_chan_spec const *chan,
110 struct ad7476_state *st = iio_priv(indio_dev);
114 case IIO_CHAN_INFO_RAW:
115 mutex_lock(&indio_dev->mlock);
116 if (iio_buffer_enabled(indio_dev))
119 ret = ad7476_scan_direct(st);
120 mutex_unlock(&indio_dev->mlock);
124 *val = (ret >> st->chip_info->channel[0].scan_type.shift) &
125 RES_MASK(st->chip_info->channel[0].scan_type.realbits);
127 case IIO_CHAN_INFO_SCALE:
128 if (!st->chip_info->int_vref_uv) {
129 scale_uv = regulator_get_voltage(st->reg);
133 scale_uv = st->chip_info->int_vref_uv;
135 scale_uv >>= chan->scan_type.realbits;
136 *val = scale_uv / 1000;
137 *val2 = (scale_uv % 1000) * 1000;
138 return IIO_VAL_INT_PLUS_MICRO;
143 #define _AD7476_CHAN(bits, _shift, _info_mask_sep) \
145 .type = IIO_VOLTAGE, \
147 .info_mask_separate = _info_mask_sep, \
148 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE), \
151 .realbits = (bits), \
154 .endianness = IIO_BE, \
158 #define AD7476_CHAN(bits) _AD7476_CHAN((bits), 13 - (bits), \
159 BIT(IIO_CHAN_INFO_RAW))
160 #define AD7940_CHAN(bits) _AD7476_CHAN((bits), 15 - (bits), \
161 BIT(IIO_CHAN_INFO_RAW))
162 #define AD7091R_CHAN(bits) _AD7476_CHAN((bits), 16 - (bits), 0)
164 static const struct ad7476_chip_info ad7476_chip_info_tbl[] = {
166 .channel[0] = AD7091R_CHAN(12),
167 .channel[1] = IIO_CHAN_SOFT_TIMESTAMP(1),
168 .reset = ad7091_reset,
171 .channel[0] = AD7940_CHAN(12),
172 .channel[1] = IIO_CHAN_SOFT_TIMESTAMP(1),
175 .channel[0] = AD7940_CHAN(10),
176 .channel[1] = IIO_CHAN_SOFT_TIMESTAMP(1),
179 .channel[0] = AD7940_CHAN(8),
180 .channel[1] = IIO_CHAN_SOFT_TIMESTAMP(1),
183 .channel[0] = AD7476_CHAN(12),
184 .channel[1] = IIO_CHAN_SOFT_TIMESTAMP(1),
187 .channel[0] = AD7476_CHAN(10),
188 .channel[1] = IIO_CHAN_SOFT_TIMESTAMP(1),
191 .channel[0] = AD7476_CHAN(8),
192 .channel[1] = IIO_CHAN_SOFT_TIMESTAMP(1),
195 .channel[0] = AD7476_CHAN(12),
196 .channel[1] = IIO_CHAN_SOFT_TIMESTAMP(1),
197 .int_vref_uv = 2500000,
200 .channel[0] = AD7940_CHAN(14),
201 .channel[1] = IIO_CHAN_SOFT_TIMESTAMP(1),
205 static const struct iio_info ad7476_info = {
206 .driver_module = THIS_MODULE,
207 .read_raw = &ad7476_read_raw,
210 static int ad7476_probe(struct spi_device *spi)
212 struct ad7476_state *st;
213 struct iio_dev *indio_dev;
216 indio_dev = iio_device_alloc(sizeof(*st));
217 if (indio_dev == NULL) {
221 st = iio_priv(indio_dev);
223 &ad7476_chip_info_tbl[spi_get_device_id(spi)->driver_data];
225 st->reg = regulator_get(&spi->dev, "vcc");
226 if (IS_ERR(st->reg)) {
227 ret = PTR_ERR(st->reg);
231 ret = regulator_enable(st->reg);
235 spi_set_drvdata(spi, indio_dev);
239 /* Establish that the iio_dev is a child of the spi device */
240 indio_dev->dev.parent = &spi->dev;
241 indio_dev->name = spi_get_device_id(spi)->name;
242 indio_dev->modes = INDIO_DIRECT_MODE;
243 indio_dev->channels = st->chip_info->channel;
244 indio_dev->num_channels = 2;
245 indio_dev->info = &ad7476_info;
246 /* Setup default message */
248 st->xfer.rx_buf = &st->data;
249 st->xfer.len = st->chip_info->channel[0].scan_type.storagebits / 8;
251 spi_message_init(&st->msg);
252 spi_message_add_tail(&st->xfer, &st->msg);
254 ret = iio_triggered_buffer_setup(indio_dev, NULL,
255 &ad7476_trigger_handler, NULL);
257 goto error_disable_reg;
259 if (st->chip_info->reset)
260 st->chip_info->reset(st);
262 ret = iio_device_register(indio_dev);
264 goto error_ring_unregister;
267 error_ring_unregister:
268 iio_triggered_buffer_cleanup(indio_dev);
270 regulator_disable(st->reg);
272 regulator_put(st->reg);
274 iio_device_free(indio_dev);
280 static int ad7476_remove(struct spi_device *spi)
282 struct iio_dev *indio_dev = spi_get_drvdata(spi);
283 struct ad7476_state *st = iio_priv(indio_dev);
285 iio_device_unregister(indio_dev);
286 iio_triggered_buffer_cleanup(indio_dev);
287 regulator_disable(st->reg);
288 regulator_put(st->reg);
289 iio_device_free(indio_dev);
294 static const struct spi_device_id ad7476_id[] = {
295 {"ad7091r", ID_AD7091R},
296 {"ad7273", ID_AD7277},
297 {"ad7274", ID_AD7276},
298 {"ad7276", ID_AD7276},
299 {"ad7277", ID_AD7277},
300 {"ad7278", ID_AD7278},
301 {"ad7466", ID_AD7466},
302 {"ad7467", ID_AD7467},
303 {"ad7468", ID_AD7468},
304 {"ad7475", ID_AD7466},
305 {"ad7476", ID_AD7466},
306 {"ad7476a", ID_AD7466},
307 {"ad7477", ID_AD7467},
308 {"ad7477a", ID_AD7467},
309 {"ad7478", ID_AD7468},
310 {"ad7478a", ID_AD7468},
311 {"ad7495", ID_AD7495},
312 {"ad7910", ID_AD7467},
313 {"ad7920", ID_AD7466},
314 {"ad7940", ID_AD7940},
317 MODULE_DEVICE_TABLE(spi, ad7476_id);
319 static struct spi_driver ad7476_driver = {
322 .owner = THIS_MODULE,
324 .probe = ad7476_probe,
325 .remove = ad7476_remove,
326 .id_table = ad7476_id,
328 module_spi_driver(ad7476_driver);
330 MODULE_AUTHOR("Michael Hennerich <hennerich@blackfin.uclinux.org>");
331 MODULE_DESCRIPTION("Analog Devices AD7476 and similar 1-channel ADCs");
332 MODULE_LICENSE("GPL v2");