blob: b7ecf9aab90fa56ecf3e51584ce24c4bc423211b [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,
Gregor Boiriebc2b7da2016-03-09 19:05:49 +010073 iio_get_time_ns(indio_dev));
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:
Alison Schofielda52f2382016-05-24 12:18:06 -0700109 ret = iio_device_claim_direct_mode(indio_dev);
110 if (ret)
111 return ret;
112 ret = ad7476_scan_direct(st);
113 iio_device_release_direct_mode(indio_dev);
Michael Hennerich349282d2010-10-11 18:05:37 +0200114
Jonathan Cameronc5e08192011-05-18 14:41:24 +0100115 if (ret < 0)
116 return ret;
117 *val = (ret >> st->chip_info->channel[0].scan_type.shift) &
Peter Meerwaldbc74fb82014-12-06 06:00:00 +0000118 GENMASK(st->chip_info->channel[0].scan_type.realbits - 1, 0);
Jonathan Cameronc5e08192011-05-18 14:41:24 +0100119 return IIO_VAL_INT;
Jonathan Cameronc8a9f802011-10-26 17:41:36 +0100120 case IIO_CHAN_INFO_SCALE:
Lars-Peter Clausencb093e42012-09-10 09:34:00 +0100121 if (!st->chip_info->int_vref_uv) {
122 scale_uv = regulator_get_voltage(st->reg);
123 if (scale_uv < 0)
124 return scale_uv;
125 } else {
126 scale_uv = st->chip_info->int_vref_uv;
127 }
Lars-Peter Clausend88c89d2013-09-28 10:31:00 +0100128 *val = scale_uv / 1000;
129 *val2 = chan->scan_type.realbits;
130 return IIO_VAL_FRACTIONAL_LOG2;
Jonathan Cameronc5e08192011-05-18 14:41:24 +0100131 }
132 return -EINVAL;
Michael Hennerich349282d2010-10-11 18:05:37 +0200133}
Michael Hennerich349282d2010-10-11 18:05:37 +0200134
Jonathan Cameron8c1033f2013-02-27 19:05:34 +0000135#define _AD7476_CHAN(bits, _shift, _info_mask_sep) \
Jonathan Cameron85871cd2012-04-13 10:42:55 +0100136 { \
137 .type = IIO_VOLTAGE, \
138 .indexed = 1, \
Jonathan Cameron8c1033f2013-02-27 19:05:34 +0000139 .info_mask_separate = _info_mask_sep, \
140 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE), \
Jonathan Cameron85871cd2012-04-13 10:42:55 +0100141 .scan_type = { \
142 .sign = 'u', \
Lars-Peter Clausen4c337de2012-09-10 09:34:00 +0100143 .realbits = (bits), \
Jonathan Cameron85871cd2012-04-13 10:42:55 +0100144 .storagebits = 16, \
Lars-Peter Clausen4c337de2012-09-10 09:34:00 +0100145 .shift = (_shift), \
146 .endianness = IIO_BE, \
Jonathan Cameron85871cd2012-04-13 10:42:55 +0100147 }, \
148}
149
Lars-Peter Clausen87c5b102012-09-17 13:26:00 +0100150#define AD7476_CHAN(bits) _AD7476_CHAN((bits), 13 - (bits), \
Jonathan Cameron8c1033f2013-02-27 19:05:34 +0000151 BIT(IIO_CHAN_INFO_RAW))
Lars-Peter Clausen87c5b102012-09-17 13:26:00 +0100152#define AD7940_CHAN(bits) _AD7476_CHAN((bits), 15 - (bits), \
Jonathan Cameron8c1033f2013-02-27 19:05:34 +0000153 BIT(IIO_CHAN_INFO_RAW))
Lars-Peter Clausen87c5b102012-09-17 13:26:00 +0100154#define AD7091R_CHAN(bits) _AD7476_CHAN((bits), 16 - (bits), 0)
Lars-Peter Clausen4c337de2012-09-10 09:34:00 +0100155
Michael Hennerich349282d2010-10-11 18:05:37 +0200156static const struct ad7476_chip_info ad7476_chip_info_tbl[] = {
Lars-Peter Clausen87c5b102012-09-17 13:26:00 +0100157 [ID_AD7091R] = {
158 .channel[0] = AD7091R_CHAN(12),
159 .channel[1] = IIO_CHAN_SOFT_TIMESTAMP(1),
160 .reset = ad7091_reset,
161 },
Lars-Peter Clausenc26cc892012-09-10 10:33:00 +0100162 [ID_AD7276] = {
163 .channel[0] = AD7940_CHAN(12),
164 .channel[1] = IIO_CHAN_SOFT_TIMESTAMP(1),
165 },
166 [ID_AD7277] = {
167 .channel[0] = AD7940_CHAN(10),
168 .channel[1] = IIO_CHAN_SOFT_TIMESTAMP(1),
169 },
170 [ID_AD7278] = {
171 .channel[0] = AD7940_CHAN(8),
172 .channel[1] = IIO_CHAN_SOFT_TIMESTAMP(1),
173 },
Michael Hennerich349282d2010-10-11 18:05:37 +0200174 [ID_AD7466] = {
Jonathan Cameron85871cd2012-04-13 10:42:55 +0100175 .channel[0] = AD7476_CHAN(12),
Jonathan Cameronc5e08192011-05-18 14:41:24 +0100176 .channel[1] = IIO_CHAN_SOFT_TIMESTAMP(1),
Michael Hennerich349282d2010-10-11 18:05:37 +0200177 },
178 [ID_AD7467] = {
Jonathan Cameron85871cd2012-04-13 10:42:55 +0100179 .channel[0] = AD7476_CHAN(10),
Jonathan Cameronc5e08192011-05-18 14:41:24 +0100180 .channel[1] = IIO_CHAN_SOFT_TIMESTAMP(1),
Michael Hennerich349282d2010-10-11 18:05:37 +0200181 },
182 [ID_AD7468] = {
Jonathan Cameron85871cd2012-04-13 10:42:55 +0100183 .channel[0] = AD7476_CHAN(8),
Jonathan Cameronc5e08192011-05-18 14:41:24 +0100184 .channel[1] = IIO_CHAN_SOFT_TIMESTAMP(1),
Michael Hennerich349282d2010-10-11 18:05:37 +0200185 },
Michael Hennerich349282d2010-10-11 18:05:37 +0200186 [ID_AD7495] = {
Jonathan Cameron85871cd2012-04-13 10:42:55 +0100187 .channel[0] = AD7476_CHAN(12),
Jonathan Cameronc5e08192011-05-18 14:41:24 +0100188 .channel[1] = IIO_CHAN_SOFT_TIMESTAMP(1),
Lars-Peter Clausencb093e42012-09-10 09:34:00 +0100189 .int_vref_uv = 2500000,
Michael Hennerich349282d2010-10-11 18:05:37 +0200190 },
Lars-Peter Clausen4c337de2012-09-10 09:34:00 +0100191 [ID_AD7940] = {
192 .channel[0] = AD7940_CHAN(14),
193 .channel[1] = IIO_CHAN_SOFT_TIMESTAMP(1),
194 },
Michael Hennerich349282d2010-10-11 18:05:37 +0200195};
196
Jonathan Cameron6fe81352011-05-18 14:42:37 +0100197static const struct iio_info ad7476_info = {
198 .driver_module = THIS_MODULE,
199 .read_raw = &ad7476_read_raw,
200};
201
Greg Kroah-Hartmanfc526922012-12-21 13:21:43 -0800202static int ad7476_probe(struct spi_device *spi)
Michael Hennerich349282d2010-10-11 18:05:37 +0200203{
Michael Hennerich349282d2010-10-11 18:05:37 +0200204 struct ad7476_state *st;
Jonathan Cameron67688102011-06-27 13:07:18 +0100205 struct iio_dev *indio_dev;
Lars-Peter Clausencb093e42012-09-10 09:34:00 +0100206 int ret;
Michael Hennerich349282d2010-10-11 18:05:37 +0200207
Sachin Kamat4ea454d2013-07-23 09:58:00 +0100208 indio_dev = devm_iio_device_alloc(&spi->dev, sizeof(*st));
209 if (!indio_dev)
210 return -ENOMEM;
211
Jonathan Cameron67688102011-06-27 13:07:18 +0100212 st = iio_priv(indio_dev);
Michael Hennerich349282d2010-10-11 18:05:37 +0200213 st->chip_info =
214 &ad7476_chip_info_tbl[spi_get_device_id(spi)->driver_data];
215
Sachin Kamat4ea454d2013-07-23 09:58:00 +0100216 st->reg = devm_regulator_get(&spi->dev, "vcc");
217 if (IS_ERR(st->reg))
218 return PTR_ERR(st->reg);
Lars-Peter Clausencb093e42012-09-10 09:34:00 +0100219
220 ret = regulator_enable(st->reg);
221 if (ret)
Sachin Kamat4ea454d2013-07-23 09:58:00 +0100222 return ret;
Michael Hennerich349282d2010-10-11 18:05:37 +0200223
Jonathan Cameron38f71aa2011-07-13 13:42:47 +0100224 spi_set_drvdata(spi, indio_dev);
Michael Hennerich349282d2010-10-11 18:05:37 +0200225
Michael Hennerich349282d2010-10-11 18:05:37 +0200226 st->spi = spi;
227
Roland Stiggeae639832011-01-29 16:27:17 +0100228 /* Establish that the iio_dev is a child of the spi device */
Jonathan Cameron67688102011-06-27 13:07:18 +0100229 indio_dev->dev.parent = &spi->dev;
Matt Ranostayb541eaf2016-07-02 17:26:33 -0700230 indio_dev->dev.of_node = spi->dev.of_node;
Jonathan Cameron67688102011-06-27 13:07:18 +0100231 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 },
306 .probe = ad7476_probe,
Greg Kroah-Hartmanfc526922012-12-21 13:21:43 -0800307 .remove = ad7476_remove,
Michael Hennerich349282d2010-10-11 18:05:37 +0200308 .id_table = ad7476_id,
309};
Lars-Peter Clausenae6ae6f2011-11-16 10:13:39 +0100310module_spi_driver(ad7476_driver);
Michael Hennerich349282d2010-10-11 18:05:37 +0200311
312MODULE_AUTHOR("Michael Hennerich <hennerich@blackfin.uclinux.org>");
Lars-Peter Clausenac5332b2012-09-10 09:34:00 +0100313MODULE_DESCRIPTION("Analog Devices AD7476 and similar 1-channel ADCs");
Michael Hennerich349282d2010-10-11 18:05:37 +0200314MODULE_LICENSE("GPL v2");