blob: ce400ec176f13e34fddd799deaa2caf595114b03 [file] [log] [blame]
Michael Hennerich349282d2010-10-11 18:05:37 +02001/*
2 * AD7466/7/8 AD7476/5/7/8 (A) SPI ADC driver
3 *
4 * Copyright 2010 Analog Devices Inc.
5 *
6 * Licensed under the GPL-2 or later.
7 */
8
Michael Hennerich349282d2010-10-11 18:05:37 +02009#include <linux/device.h>
10#include <linux/kernel.h>
11#include <linux/slab.h>
12#include <linux/sysfs.h>
Michael Hennerich349282d2010-10-11 18:05:37 +020013#include <linux/spi/spi.h>
14#include <linux/regulator/consumer.h>
15#include <linux/err.h>
Paul Gortmaker99c97852011-07-03 15:49:50 -040016#include <linux/module.h>
Peter Meerwaldbc74fb82014-12-06 06:00:00 +000017#include <linux/bitops.h>
Michael Hennerich349282d2010-10-11 18:05:37 +020018
Jonathan Cameron06458e22012-04-25 15:54:58 +010019#include <linux/iio/iio.h>
20#include <linux/iio/sysfs.h>
21#include <linux/iio/buffer.h>
Lars-Peter Clausen7a28fe32012-09-10 09:34:00 +010022#include <linux/iio/trigger_consumer.h>
23#include <linux/iio/triggered_buffer.h>
Michael Hennerich349282d2010-10-11 18:05:37 +020024
Lars-Peter Clausen87c5b102012-09-17 13:26:00 +010025struct ad7476_state;
26
Lars-Peter Clausen7a28fe32012-09-10 09:34:00 +010027struct ad7476_chip_info {
28 unsigned int int_vref_uv;
29 struct iio_chan_spec channel[2];
Lars-Peter Clausen87c5b102012-09-17 13:26:00 +010030 void (*reset)(struct ad7476_state *);
Lars-Peter Clausen7a28fe32012-09-10 09:34:00 +010031};
32
33struct ad7476_state {
34 struct spi_device *spi;
35 const struct ad7476_chip_info *chip_info;
36 struct regulator *reg;
37 struct spi_transfer xfer;
38 struct spi_message msg;
39 /*
40 * DMA (thus cache coherency maintenance) requires the
41 * transfer buffers to live in their own cache lines.
42 * Make the buffer large enough for one 16 bit sample and one 64 bit
43 * aligned 64 bit timestamp.
44 */
45 unsigned char data[ALIGN(2, sizeof(s64)) + sizeof(s64)]
46 ____cacheline_aligned;
47};
48
49enum ad7476_supported_device_ids {
Lars-Peter Clausen87c5b102012-09-17 13:26:00 +010050 ID_AD7091R,
Lars-Peter Clausenc26cc892012-09-10 10:33:00 +010051 ID_AD7276,
52 ID_AD7277,
53 ID_AD7278,
Lars-Peter Clausen7a28fe32012-09-10 09:34:00 +010054 ID_AD7466,
55 ID_AD7467,
56 ID_AD7468,
Lars-Peter Clausen4c337de2012-09-10 09:34:00 +010057 ID_AD7495,
58 ID_AD7940,
Lars-Peter Clausen7a28fe32012-09-10 09:34:00 +010059};
60
61static irqreturn_t ad7476_trigger_handler(int irq, void *p)
62{
63 struct iio_poll_func *pf = p;
64 struct iio_dev *indio_dev = pf->indio_dev;
65 struct ad7476_state *st = iio_priv(indio_dev);
Lars-Peter Clausen7a28fe32012-09-10 09:34:00 +010066 int b_sent;
67
68 b_sent = spi_sync(st->spi, &st->msg);
69 if (b_sent < 0)
70 goto done;
71
Lars-Peter Clausenb05583a2013-09-19 13:59:00 +010072 iio_push_to_buffers_with_timestamp(indio_dev, st->data,
73 iio_get_time_ns());
Lars-Peter Clausen7a28fe32012-09-10 09:34:00 +010074done:
75 iio_trigger_notify_done(indio_dev->trig);
76
77 return IRQ_HANDLED;
78}
Michael Hennerich349282d2010-10-11 18:05:37 +020079
Lars-Peter Clausen87c5b102012-09-17 13:26:00 +010080static void ad7091_reset(struct ad7476_state *st)
81{
82 /* Any transfers with 8 scl cycles will reset the device */
83 spi_read(st->spi, st->data, 1);
84}
85
Michael Hennerich349282d2010-10-11 18:05:37 +020086static int ad7476_scan_direct(struct ad7476_state *st)
87{
Michael Hennerich349282d2010-10-11 18:05:37 +020088 int ret;
89
Michael Hennerich668413e2010-10-11 18:05:56 +020090 ret = spi_sync(st->spi, &st->msg);
Michael Hennerich349282d2010-10-11 18:05:37 +020091 if (ret)
92 return ret;
93
Lars-Peter Clausen610a4072012-09-10 09:34:00 +010094 return be16_to_cpup((__be16 *)st->data);
Michael Hennerich349282d2010-10-11 18:05:37 +020095}
96
Jonathan Cameron84f79ec2011-10-06 17:14:37 +010097static int ad7476_read_raw(struct iio_dev *indio_dev,
Jonathan Cameronc5e08192011-05-18 14:41:24 +010098 struct iio_chan_spec const *chan,
99 int *val,
100 int *val2,
101 long m)
Michael Hennerich349282d2010-10-11 18:05:37 +0200102{
Michael Hennerich349282d2010-10-11 18:05:37 +0200103 int ret;
Jonathan Cameron84f79ec2011-10-06 17:14:37 +0100104 struct ad7476_state *st = iio_priv(indio_dev);
Lars-Peter Clausencb093e42012-09-10 09:34:00 +0100105 int scale_uv;
Michael Hennerich349282d2010-10-11 18:05:37 +0200106
Jonathan Cameronc5e08192011-05-18 14:41:24 +0100107 switch (m) {
Jonathan Cameronb11f98f2012-04-15 17:41:18 +0100108 case IIO_CHAN_INFO_RAW:
Jonathan Cameron84f79ec2011-10-06 17:14:37 +0100109 mutex_lock(&indio_dev->mlock);
110 if (iio_buffer_enabled(indio_dev))
Jonathan Cameron78c32ed2011-12-05 22:18:20 +0000111 ret = -EBUSY;
Jonathan Cameronc5e08192011-05-18 14:41:24 +0100112 else
113 ret = ad7476_scan_direct(st);
Jonathan Cameron84f79ec2011-10-06 17:14:37 +0100114 mutex_unlock(&indio_dev->mlock);
Michael Hennerich349282d2010-10-11 18:05:37 +0200115
Jonathan Cameronc5e08192011-05-18 14:41:24 +0100116 if (ret < 0)
117 return ret;
118 *val = (ret >> st->chip_info->channel[0].scan_type.shift) &
Peter Meerwaldbc74fb82014-12-06 06:00:00 +0000119 GENMASK(st->chip_info->channel[0].scan_type.realbits - 1, 0);
Jonathan Cameronc5e08192011-05-18 14:41:24 +0100120 return IIO_VAL_INT;
Jonathan Cameronc8a9f802011-10-26 17:41:36 +0100121 case IIO_CHAN_INFO_SCALE:
Lars-Peter Clausencb093e42012-09-10 09:34:00 +0100122 if (!st->chip_info->int_vref_uv) {
123 scale_uv = regulator_get_voltage(st->reg);
124 if (scale_uv < 0)
125 return scale_uv;
126 } else {
127 scale_uv = st->chip_info->int_vref_uv;
128 }
Lars-Peter Clausend88c89d2013-09-28 10:31:00 +0100129 *val = scale_uv / 1000;
130 *val2 = chan->scan_type.realbits;
131 return IIO_VAL_FRACTIONAL_LOG2;
Jonathan Cameronc5e08192011-05-18 14:41:24 +0100132 }
133 return -EINVAL;
Michael Hennerich349282d2010-10-11 18:05:37 +0200134}
Michael Hennerich349282d2010-10-11 18:05:37 +0200135
Jonathan Cameron8c1033f2013-02-27 19:05:34 +0000136#define _AD7476_CHAN(bits, _shift, _info_mask_sep) \
Jonathan Cameron85871cd2012-04-13 10:42:55 +0100137 { \
138 .type = IIO_VOLTAGE, \
139 .indexed = 1, \
Jonathan Cameron8c1033f2013-02-27 19:05:34 +0000140 .info_mask_separate = _info_mask_sep, \
141 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE), \
Jonathan Cameron85871cd2012-04-13 10:42:55 +0100142 .scan_type = { \
143 .sign = 'u', \
Lars-Peter Clausen4c337de2012-09-10 09:34:00 +0100144 .realbits = (bits), \
Jonathan Cameron85871cd2012-04-13 10:42:55 +0100145 .storagebits = 16, \
Lars-Peter Clausen4c337de2012-09-10 09:34:00 +0100146 .shift = (_shift), \
147 .endianness = IIO_BE, \
Jonathan Cameron85871cd2012-04-13 10:42:55 +0100148 }, \
149}
150
Lars-Peter Clausen87c5b102012-09-17 13:26:00 +0100151#define AD7476_CHAN(bits) _AD7476_CHAN((bits), 13 - (bits), \
Jonathan Cameron8c1033f2013-02-27 19:05:34 +0000152 BIT(IIO_CHAN_INFO_RAW))
Lars-Peter Clausen87c5b102012-09-17 13:26:00 +0100153#define AD7940_CHAN(bits) _AD7476_CHAN((bits), 15 - (bits), \
Jonathan Cameron8c1033f2013-02-27 19:05:34 +0000154 BIT(IIO_CHAN_INFO_RAW))
Lars-Peter Clausen87c5b102012-09-17 13:26:00 +0100155#define AD7091R_CHAN(bits) _AD7476_CHAN((bits), 16 - (bits), 0)
Lars-Peter Clausen4c337de2012-09-10 09:34:00 +0100156
Michael Hennerich349282d2010-10-11 18:05:37 +0200157static const struct ad7476_chip_info ad7476_chip_info_tbl[] = {
Lars-Peter Clausen87c5b102012-09-17 13:26:00 +0100158 [ID_AD7091R] = {
159 .channel[0] = AD7091R_CHAN(12),
160 .channel[1] = IIO_CHAN_SOFT_TIMESTAMP(1),
161 .reset = ad7091_reset,
162 },
Lars-Peter Clausenc26cc892012-09-10 10:33:00 +0100163 [ID_AD7276] = {
164 .channel[0] = AD7940_CHAN(12),
165 .channel[1] = IIO_CHAN_SOFT_TIMESTAMP(1),
166 },
167 [ID_AD7277] = {
168 .channel[0] = AD7940_CHAN(10),
169 .channel[1] = IIO_CHAN_SOFT_TIMESTAMP(1),
170 },
171 [ID_AD7278] = {
172 .channel[0] = AD7940_CHAN(8),
173 .channel[1] = IIO_CHAN_SOFT_TIMESTAMP(1),
174 },
Michael Hennerich349282d2010-10-11 18:05:37 +0200175 [ID_AD7466] = {
Jonathan Cameron85871cd2012-04-13 10:42:55 +0100176 .channel[0] = AD7476_CHAN(12),
Jonathan Cameronc5e08192011-05-18 14:41:24 +0100177 .channel[1] = IIO_CHAN_SOFT_TIMESTAMP(1),
Michael Hennerich349282d2010-10-11 18:05:37 +0200178 },
179 [ID_AD7467] = {
Jonathan Cameron85871cd2012-04-13 10:42:55 +0100180 .channel[0] = AD7476_CHAN(10),
Jonathan Cameronc5e08192011-05-18 14:41:24 +0100181 .channel[1] = IIO_CHAN_SOFT_TIMESTAMP(1),
Michael Hennerich349282d2010-10-11 18:05:37 +0200182 },
183 [ID_AD7468] = {
Jonathan Cameron85871cd2012-04-13 10:42:55 +0100184 .channel[0] = AD7476_CHAN(8),
Jonathan Cameronc5e08192011-05-18 14:41:24 +0100185 .channel[1] = IIO_CHAN_SOFT_TIMESTAMP(1),
Michael Hennerich349282d2010-10-11 18:05:37 +0200186 },
Michael Hennerich349282d2010-10-11 18:05:37 +0200187 [ID_AD7495] = {
Jonathan Cameron85871cd2012-04-13 10:42:55 +0100188 .channel[0] = AD7476_CHAN(12),
Jonathan Cameronc5e08192011-05-18 14:41:24 +0100189 .channel[1] = IIO_CHAN_SOFT_TIMESTAMP(1),
Lars-Peter Clausencb093e42012-09-10 09:34:00 +0100190 .int_vref_uv = 2500000,
Michael Hennerich349282d2010-10-11 18:05:37 +0200191 },
Lars-Peter Clausen4c337de2012-09-10 09:34:00 +0100192 [ID_AD7940] = {
193 .channel[0] = AD7940_CHAN(14),
194 .channel[1] = IIO_CHAN_SOFT_TIMESTAMP(1),
195 },
Michael Hennerich349282d2010-10-11 18:05:37 +0200196};
197
Jonathan Cameron6fe81352011-05-18 14:42:37 +0100198static const struct iio_info ad7476_info = {
199 .driver_module = THIS_MODULE,
200 .read_raw = &ad7476_read_raw,
201};
202
Greg Kroah-Hartmanfc526922012-12-21 13:21:43 -0800203static int ad7476_probe(struct spi_device *spi)
Michael Hennerich349282d2010-10-11 18:05:37 +0200204{
Michael Hennerich349282d2010-10-11 18:05:37 +0200205 struct ad7476_state *st;
Jonathan Cameron67688102011-06-27 13:07:18 +0100206 struct iio_dev *indio_dev;
Lars-Peter Clausencb093e42012-09-10 09:34:00 +0100207 int ret;
Michael Hennerich349282d2010-10-11 18:05:37 +0200208
Sachin Kamat4ea454d2013-07-23 09:58:00 +0100209 indio_dev = devm_iio_device_alloc(&spi->dev, sizeof(*st));
210 if (!indio_dev)
211 return -ENOMEM;
212
Jonathan Cameron67688102011-06-27 13:07:18 +0100213 st = iio_priv(indio_dev);
Michael Hennerich349282d2010-10-11 18:05:37 +0200214 st->chip_info =
215 &ad7476_chip_info_tbl[spi_get_device_id(spi)->driver_data];
216
Sachin Kamat4ea454d2013-07-23 09:58:00 +0100217 st->reg = devm_regulator_get(&spi->dev, "vcc");
218 if (IS_ERR(st->reg))
219 return PTR_ERR(st->reg);
Lars-Peter Clausencb093e42012-09-10 09:34:00 +0100220
221 ret = regulator_enable(st->reg);
222 if (ret)
Sachin Kamat4ea454d2013-07-23 09:58:00 +0100223 return ret;
Michael Hennerich349282d2010-10-11 18:05:37 +0200224
Jonathan Cameron38f71aa2011-07-13 13:42:47 +0100225 spi_set_drvdata(spi, indio_dev);
Michael Hennerich349282d2010-10-11 18:05:37 +0200226
Michael Hennerich349282d2010-10-11 18:05:37 +0200227 st->spi = spi;
228
Roland Stiggeae639832011-01-29 16:27:17 +0100229 /* Establish that the iio_dev is a child of the spi device */
Jonathan Cameron67688102011-06-27 13:07:18 +0100230 indio_dev->dev.parent = &spi->dev;
231 indio_dev->name = spi_get_device_id(spi)->name;
232 indio_dev->modes = INDIO_DIRECT_MODE;
233 indio_dev->channels = st->chip_info->channel;
234 indio_dev->num_channels = 2;
235 indio_dev->info = &ad7476_info;
Michael Hennerich349282d2010-10-11 18:05:37 +0200236 /* Setup default message */
237
238 st->xfer.rx_buf = &st->data;
Jonathan Cameronc5e08192011-05-18 14:41:24 +0100239 st->xfer.len = st->chip_info->channel[0].scan_type.storagebits / 8;
Michael Hennerich349282d2010-10-11 18:05:37 +0200240
241 spi_message_init(&st->msg);
242 spi_message_add_tail(&st->xfer, &st->msg);
243
Lars-Peter Clausen7a28fe32012-09-10 09:34:00 +0100244 ret = iio_triggered_buffer_setup(indio_dev, NULL,
245 &ad7476_trigger_handler, NULL);
Michael Hennerich349282d2010-10-11 18:05:37 +0200246 if (ret)
Jonathan Cameron67688102011-06-27 13:07:18 +0100247 goto error_disable_reg;
Michael Hennerich349282d2010-10-11 18:05:37 +0200248
Lars-Peter Clausen87c5b102012-09-17 13:26:00 +0100249 if (st->chip_info->reset)
250 st->chip_info->reset(st);
251
Jonathan Cameron26d25ae2011-09-02 17:14:40 +0100252 ret = iio_device_register(indio_dev);
253 if (ret)
254 goto error_ring_unregister;
Michael Hennerich349282d2010-10-11 18:05:37 +0200255 return 0;
256
Jonathan Cameron26d25ae2011-09-02 17:14:40 +0100257error_ring_unregister:
Lars-Peter Clausen7a28fe32012-09-10 09:34:00 +0100258 iio_triggered_buffer_cleanup(indio_dev);
Michael Hennerich349282d2010-10-11 18:05:37 +0200259error_disable_reg:
Lars-Peter Clausencb093e42012-09-10 09:34:00 +0100260 regulator_disable(st->reg);
Jonathan Cameron26d25ae2011-09-02 17:14:40 +0100261
Michael Hennerich349282d2010-10-11 18:05:37 +0200262 return ret;
263}
264
Greg Kroah-Hartmanfc526922012-12-21 13:21:43 -0800265static int ad7476_remove(struct spi_device *spi)
Michael Hennerich349282d2010-10-11 18:05:37 +0200266{
Jonathan Cameron67688102011-06-27 13:07:18 +0100267 struct iio_dev *indio_dev = spi_get_drvdata(spi);
268 struct ad7476_state *st = iio_priv(indio_dev);
Jonathan Cameron67688102011-06-27 13:07:18 +0100269
Jonathan Camerond2fffd62011-10-14 14:46:58 +0100270 iio_device_unregister(indio_dev);
Lars-Peter Clausen7a28fe32012-09-10 09:34:00 +0100271 iio_triggered_buffer_cleanup(indio_dev);
Lars-Peter Clausencb093e42012-09-10 09:34:00 +0100272 regulator_disable(st->reg);
Jonathan Cameron67688102011-06-27 13:07:18 +0100273
Michael Hennerich349282d2010-10-11 18:05:37 +0200274 return 0;
275}
276
277static const struct spi_device_id ad7476_id[] = {
Lars-Peter Clausen87c5b102012-09-17 13:26:00 +0100278 {"ad7091r", ID_AD7091R},
Lars-Peter Clausenc26cc892012-09-10 10:33:00 +0100279 {"ad7273", ID_AD7277},
280 {"ad7274", ID_AD7276},
281 {"ad7276", ID_AD7276},
282 {"ad7277", ID_AD7277},
283 {"ad7278", ID_AD7278},
Michael Hennerich349282d2010-10-11 18:05:37 +0200284 {"ad7466", ID_AD7466},
285 {"ad7467", ID_AD7467},
286 {"ad7468", ID_AD7468},
Lars-Peter Clausenfcc78002012-09-10 09:34:00 +0100287 {"ad7475", ID_AD7466},
288 {"ad7476", ID_AD7466},
289 {"ad7476a", ID_AD7466},
290 {"ad7477", ID_AD7467},
291 {"ad7477a", ID_AD7467},
292 {"ad7478", ID_AD7468},
293 {"ad7478a", ID_AD7468},
Michael Hennerich349282d2010-10-11 18:05:37 +0200294 {"ad7495", ID_AD7495},
Lars-Peter Clausenac5332b2012-09-10 09:34:00 +0100295 {"ad7910", ID_AD7467},
296 {"ad7920", ID_AD7466},
Lars-Peter Clausen4c337de2012-09-10 09:34:00 +0100297 {"ad7940", ID_AD7940},
Michael Hennerich349282d2010-10-11 18:05:37 +0200298 {}
299};
Lars-Peter Clausen55e43902011-11-16 08:53:31 +0100300MODULE_DEVICE_TABLE(spi, ad7476_id);
Michael Hennerich349282d2010-10-11 18:05:37 +0200301
302static struct spi_driver ad7476_driver = {
303 .driver = {
304 .name = "ad7476",
Michael Hennerich349282d2010-10-11 18:05:37 +0200305 .owner = THIS_MODULE,
306 },
307 .probe = ad7476_probe,
Greg Kroah-Hartmanfc526922012-12-21 13:21:43 -0800308 .remove = ad7476_remove,
Michael Hennerich349282d2010-10-11 18:05:37 +0200309 .id_table = ad7476_id,
310};
Lars-Peter Clausenae6ae6f2011-11-16 10:13:39 +0100311module_spi_driver(ad7476_driver);
Michael Hennerich349282d2010-10-11 18:05:37 +0200312
313MODULE_AUTHOR("Michael Hennerich <hennerich@blackfin.uclinux.org>");
Lars-Peter Clausenac5332b2012-09-10 09:34:00 +0100314MODULE_DESCRIPTION("Analog Devices AD7476 and similar 1-channel ADCs");
Michael Hennerich349282d2010-10-11 18:05:37 +0200315MODULE_LICENSE("GPL v2");