blob: 48d43cb0f932cf48b32aaf4e21b2616152dfb45e [file] [log] [blame]
Thomas Gleixnerfda8d262019-05-28 09:57:06 -07001// SPDX-License-Identifier: GPL-2.0-only
Michael Hennerich7c31b982011-02-24 12:32:45 +01002/*
3 * AD7298 SPI ADC driver
4 *
5 * Copyright 2011 Analog Devices Inc.
Michael Hennerich7c31b982011-02-24 12:32:45 +01006 */
7
Michael Hennerich7c31b982011-02-24 12:32:45 +01008#include <linux/device.h>
9#include <linux/kernel.h>
10#include <linux/slab.h>
11#include <linux/sysfs.h>
12#include <linux/spi/spi.h>
13#include <linux/regulator/consumer.h>
14#include <linux/err.h>
15#include <linux/delay.h>
Paul Gortmaker99c97852011-07-03 15:49:50 -040016#include <linux/module.h>
Lars-Peter Clausendc4871a2012-11-15 13:15:00 +000017#include <linux/interrupt.h>
Peter Meerwald48898052014-12-06 06:00:00 +000018#include <linux/bitops.h>
Michael Hennerich7c31b982011-02-24 12:32:45 +010019
Jonathan Cameron06458e22012-04-25 15:54:58 +010020#include <linux/iio/iio.h>
21#include <linux/iio/sysfs.h>
22#include <linux/iio/buffer.h>
Lars-Peter Clausendc4871a2012-11-15 13:15:00 +000023#include <linux/iio/trigger_consumer.h>
24#include <linux/iio/triggered_buffer.h>
Michael Hennerich7c31b982011-02-24 12:32:45 +010025
Lars-Peter Clausen709ab362012-11-17 11:42:59 +000026#include <linux/platform_data/ad7298.h>
Michael Hennerich7c31b982011-02-24 12:32:45 +010027
Peter Meerwald48898052014-12-06 06:00:00 +000028#define AD7298_WRITE BIT(15) /* write to the control register */
29#define AD7298_REPEAT BIT(14) /* repeated conversion enable */
30#define AD7298_CH(x) BIT(13 - (x)) /* channel select */
31#define AD7298_TSENSE BIT(5) /* temperature conversion enable */
32#define AD7298_EXTREF BIT(2) /* external reference enable */
33#define AD7298_TAVG BIT(1) /* temperature sensor averaging enable */
34#define AD7298_PDD BIT(0) /* partial power down enable */
Lars-Peter Clausendc4871a2012-11-15 13:15:00 +000035
36#define AD7298_MAX_CHAN 8
Lars-Peter Clausendc4871a2012-11-15 13:15:00 +000037#define AD7298_INTREF_mV 2500
38
39#define AD7298_CH_TEMP 9
40
Lars-Peter Clausendc4871a2012-11-15 13:15:00 +000041struct ad7298_state {
42 struct spi_device *spi;
43 struct regulator *reg;
44 unsigned ext_ref;
45 struct spi_transfer ring_xfer[10];
46 struct spi_transfer scan_single_xfer[3];
47 struct spi_message ring_msg;
48 struct spi_message scan_single_msg;
49 /*
50 * DMA (thus cache coherency maintenance) requires the
51 * transfer buffers to live in their own cache lines.
52 */
Jonathan Cameronbe7fd3b2012-11-21 18:24:26 +000053 __be16 rx_buf[12] ____cacheline_aligned;
54 __be16 tx_buf[2];
Lars-Peter Clausendc4871a2012-11-15 13:15:00 +000055};
56
Jonathan Cameron95e48f72012-04-13 10:42:54 +010057#define AD7298_V_CHAN(index) \
58 { \
59 .type = IIO_VOLTAGE, \
60 .indexed = 1, \
61 .channel = index, \
Jonathan Cameron40dd6762013-02-27 19:04:29 +000062 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \
63 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE), \
Jonathan Cameron95e48f72012-04-13 10:42:54 +010064 .address = index, \
65 .scan_index = index, \
66 .scan_type = { \
67 .sign = 'u', \
68 .realbits = 12, \
69 .storagebits = 16, \
Lars-Peter Clausenca6546382012-11-15 13:15:00 +000070 .endianness = IIO_BE, \
Jonathan Cameron95e48f72012-04-13 10:42:54 +010071 }, \
72 }
73
Lars-Peter Clausenf4e4b952012-08-09 08:51:00 +010074static const struct iio_chan_spec ad7298_channels[] = {
Jonathan Cameron95e48f72012-04-13 10:42:54 +010075 {
76 .type = IIO_TEMP,
77 .indexed = 1,
78 .channel = 0,
Jonathan Cameron40dd6762013-02-27 19:04:29 +000079 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
80 BIT(IIO_CHAN_INFO_SCALE) |
81 BIT(IIO_CHAN_INFO_OFFSET),
Lars-Peter Clausend045b9d2012-06-18 18:33:51 +020082 .address = AD7298_CH_TEMP,
83 .scan_index = -1,
Jonathan Cameron95e48f72012-04-13 10:42:54 +010084 .scan_type = {
85 .sign = 's',
86 .realbits = 32,
87 .storagebits = 32,
88 },
89 },
90 AD7298_V_CHAN(0),
91 AD7298_V_CHAN(1),
92 AD7298_V_CHAN(2),
93 AD7298_V_CHAN(3),
94 AD7298_V_CHAN(4),
95 AD7298_V_CHAN(5),
96 AD7298_V_CHAN(6),
97 AD7298_V_CHAN(7),
Michael Hennerich01a99e12011-05-18 14:41:55 +010098 IIO_CHAN_SOFT_TIMESTAMP(8),
99};
100
Lee Jonesb5d214d2020-07-16 14:59:02 +0100101/*
Lars-Peter Clausendc4871a2012-11-15 13:15:00 +0000102 * ad7298_update_scan_mode() setup the spi transfer buffer for the new scan mask
Lee Jonesb5d214d2020-07-16 14:59:02 +0100103 */
Lars-Peter Clausendc4871a2012-11-15 13:15:00 +0000104static int ad7298_update_scan_mode(struct iio_dev *indio_dev,
105 const unsigned long *active_scan_mask)
106{
107 struct ad7298_state *st = iio_priv(indio_dev);
108 int i, m;
109 unsigned short command;
110 int scan_count;
111
112 /* Now compute overall size */
113 scan_count = bitmap_weight(active_scan_mask, indio_dev->masklength);
114
115 command = AD7298_WRITE | st->ext_ref;
116
117 for (i = 0, m = AD7298_CH(0); i < AD7298_MAX_CHAN; i++, m >>= 1)
118 if (test_bit(i, active_scan_mask))
119 command |= m;
120
121 st->tx_buf[0] = cpu_to_be16(command);
122
123 /* build spi ring message */
124 st->ring_xfer[0].tx_buf = &st->tx_buf[0];
125 st->ring_xfer[0].len = 2;
126 st->ring_xfer[0].cs_change = 1;
127 st->ring_xfer[1].tx_buf = &st->tx_buf[1];
128 st->ring_xfer[1].len = 2;
129 st->ring_xfer[1].cs_change = 1;
130
131 spi_message_init(&st->ring_msg);
132 spi_message_add_tail(&st->ring_xfer[0], &st->ring_msg);
133 spi_message_add_tail(&st->ring_xfer[1], &st->ring_msg);
134
135 for (i = 0; i < scan_count; i++) {
136 st->ring_xfer[i + 2].rx_buf = &st->rx_buf[i];
137 st->ring_xfer[i + 2].len = 2;
138 st->ring_xfer[i + 2].cs_change = 1;
139 spi_message_add_tail(&st->ring_xfer[i + 2], &st->ring_msg);
140 }
141 /* make sure last transfer cs_change is not set */
142 st->ring_xfer[i + 1].cs_change = 0;
143
144 return 0;
145}
146
Lee Jonesb5d214d2020-07-16 14:59:02 +0100147/*
Lars-Peter Clausendc4871a2012-11-15 13:15:00 +0000148 * ad7298_trigger_handler() bh of trigger launched polling to ring buffer
149 *
150 * Currently there is no option in this driver to disable the saving of
151 * timestamps within the ring.
Lee Jonesb5d214d2020-07-16 14:59:02 +0100152 */
Lars-Peter Clausendc4871a2012-11-15 13:15:00 +0000153static irqreturn_t ad7298_trigger_handler(int irq, void *p)
154{
155 struct iio_poll_func *pf = p;
156 struct iio_dev *indio_dev = pf->indio_dev;
157 struct ad7298_state *st = iio_priv(indio_dev);
Lars-Peter Clausendc4871a2012-11-15 13:15:00 +0000158 int b_sent;
159
160 b_sent = spi_sync(st->spi, &st->ring_msg);
161 if (b_sent)
162 goto done;
163
Lars-Peter Clausen85ec2372013-09-19 13:59:00 +0100164 iio_push_to_buffers_with_timestamp(indio_dev, st->rx_buf,
Gregor Boiriebc2b7da2016-03-09 19:05:49 +0100165 iio_get_time_ns(indio_dev));
Lars-Peter Clausendc4871a2012-11-15 13:15:00 +0000166
167done:
168 iio_trigger_notify_done(indio_dev->trig);
169
170 return IRQ_HANDLED;
171}
172
Michael Hennerich7c31b982011-02-24 12:32:45 +0100173static int ad7298_scan_direct(struct ad7298_state *st, unsigned ch)
174{
175 int ret;
176 st->tx_buf[0] = cpu_to_be16(AD7298_WRITE | st->ext_ref |
177 (AD7298_CH(0) >> ch));
178
179 ret = spi_sync(st->spi, &st->scan_single_msg);
180 if (ret)
181 return ret;
182
183 return be16_to_cpu(st->rx_buf[0]);
184}
185
Michael Hennerich01a99e12011-05-18 14:41:55 +0100186static int ad7298_scan_temp(struct ad7298_state *st, int *val)
Michael Hennerich7c31b982011-02-24 12:32:45 +0100187{
Lars-Peter Clausen01a10e02012-11-15 13:15:00 +0000188 int ret;
Dan Carpenterc1a75282011-11-22 10:39:15 +0300189 __be16 buf;
Michael Hennerich7c31b982011-02-24 12:32:45 +0100190
Dan Carpenterc1a75282011-11-22 10:39:15 +0300191 buf = cpu_to_be16(AD7298_WRITE | AD7298_TSENSE |
Michael Hennerich7c31b982011-02-24 12:32:45 +0100192 AD7298_TAVG | st->ext_ref);
193
Dan Carpenterc1a75282011-11-22 10:39:15 +0300194 ret = spi_write(st->spi, (u8 *)&buf, 2);
Michael Hennerich01a99e12011-05-18 14:41:55 +0100195 if (ret)
196 return ret;
197
Dan Carpenterc1a75282011-11-22 10:39:15 +0300198 buf = cpu_to_be16(0);
Michael Hennerich01a99e12011-05-18 14:41:55 +0100199
Dan Carpenterc1a75282011-11-22 10:39:15 +0300200 ret = spi_write(st->spi, (u8 *)&buf, 2);
Michael Hennerich01a99e12011-05-18 14:41:55 +0100201 if (ret)
202 return ret;
203
Michael Hennerich7c31b982011-02-24 12:32:45 +0100204 usleep_range(101, 1000); /* sleep > 100us */
Michael Hennerich01a99e12011-05-18 14:41:55 +0100205
Dan Carpenterc1a75282011-11-22 10:39:15 +0300206 ret = spi_read(st->spi, (u8 *)&buf, 2);
Michael Hennerich01a99e12011-05-18 14:41:55 +0100207 if (ret)
208 return ret;
Michael Hennerich7c31b982011-02-24 12:32:45 +0100209
Lars-Peter Clausen01a10e02012-11-15 13:15:00 +0000210 *val = sign_extend32(be16_to_cpu(buf), 11);
Michael Hennerich01a99e12011-05-18 14:41:55 +0100211
212 return 0;
Michael Hennerich7c31b982011-02-24 12:32:45 +0100213}
214
Lars-Peter Clausen01a10e02012-11-15 13:15:00 +0000215static int ad7298_get_ref_voltage(struct ad7298_state *st)
216{
217 int vref;
218
219 if (st->ext_ref) {
220 vref = regulator_get_voltage(st->reg);
221 if (vref < 0)
222 return vref;
223
224 return vref / 1000;
225 } else {
226 return AD7298_INTREF_mV;
227 }
228}
229
Jonathan Cameron84f79ec2011-10-06 17:14:37 +0100230static int ad7298_read_raw(struct iio_dev *indio_dev,
Michael Hennerich01a99e12011-05-18 14:41:55 +0100231 struct iio_chan_spec const *chan,
232 int *val,
233 int *val2,
234 long m)
Michael Hennerich7c31b982011-02-24 12:32:45 +0100235{
Michael Hennerich01a99e12011-05-18 14:41:55 +0100236 int ret;
Jonathan Cameron84f79ec2011-10-06 17:14:37 +0100237 struct ad7298_state *st = iio_priv(indio_dev);
Michael Hennerich7c31b982011-02-24 12:32:45 +0100238
Michael Hennerich01a99e12011-05-18 14:41:55 +0100239 switch (m) {
Jonathan Cameronb11f98f2012-04-15 17:41:18 +0100240 case IIO_CHAN_INFO_RAW:
Alison Schofieldd02ec002016-07-18 12:34:17 -0700241 ret = iio_device_claim_direct_mode(indio_dev);
242 if (ret)
243 return ret;
244
245 if (chan->address == AD7298_CH_TEMP)
246 ret = ad7298_scan_temp(st, val);
247 else
248 ret = ad7298_scan_direct(st, chan->address);
249
250 iio_device_release_direct_mode(indio_dev);
Michael Hennerich01a99e12011-05-18 14:41:55 +0100251
252 if (ret < 0)
253 return ret;
254
255 if (chan->address != AD7298_CH_TEMP)
Peter Meerwald48898052014-12-06 06:00:00 +0000256 *val = ret & GENMASK(chan->scan_type.realbits - 1, 0);
Michael Hennerich01a99e12011-05-18 14:41:55 +0100257
258 return IIO_VAL_INT;
Jonathan Cameronc8a9f802011-10-26 17:41:36 +0100259 case IIO_CHAN_INFO_SCALE:
260 switch (chan->type) {
261 case IIO_VOLTAGE:
Lars-Peter Clausen01a10e02012-11-15 13:15:00 +0000262 *val = ad7298_get_ref_voltage(st);
Lars-Peter Clausen2e334602012-11-15 13:15:00 +0000263 *val2 = chan->scan_type.realbits;
264 return IIO_VAL_FRACTIONAL_LOG2;
Jonathan Cameronc8a9f802011-10-26 17:41:36 +0100265 case IIO_TEMP:
Lars-Peter Clausen01a10e02012-11-15 13:15:00 +0000266 *val = ad7298_get_ref_voltage(st);
267 *val2 = 10;
268 return IIO_VAL_FRACTIONAL;
Jonathan Cameronc8a9f802011-10-26 17:41:36 +0100269 default:
270 return -EINVAL;
271 }
Lars-Peter Clausen01a10e02012-11-15 13:15:00 +0000272 case IIO_CHAN_INFO_OFFSET:
273 *val = 1093 - 2732500 / ad7298_get_ref_voltage(st);
274 return IIO_VAL_INT;
Michael Hennerich01a99e12011-05-18 14:41:55 +0100275 }
276 return -EINVAL;
Michael Hennerich7c31b982011-02-24 12:32:45 +0100277}
Michael Hennerich7c31b982011-02-24 12:32:45 +0100278
Jonathan Cameron6fe81352011-05-18 14:42:37 +0100279static const struct iio_info ad7298_info = {
280 .read_raw = &ad7298_read_raw,
Lars-Peter Clausend045b9d2012-06-18 18:33:51 +0200281 .update_scan_mode = ad7298_update_scan_mode,
Jonathan Cameron6fe81352011-05-18 14:42:37 +0100282};
283
Greg Kroah-Hartmanfc526922012-12-21 13:21:43 -0800284static int ad7298_probe(struct spi_device *spi)
Michael Hennerich7c31b982011-02-24 12:32:45 +0100285{
286 struct ad7298_platform_data *pdata = spi->dev.platform_data;
287 struct ad7298_state *st;
Sachin Kamat7abe9002013-07-23 09:58:00 +0100288 struct iio_dev *indio_dev;
Lars-Peter Clausen2e334602012-11-15 13:15:00 +0000289 int ret;
Michael Hennerich7c31b982011-02-24 12:32:45 +0100290
Sachin Kamat7abe9002013-07-23 09:58:00 +0100291 indio_dev = devm_iio_device_alloc(&spi->dev, sizeof(*st));
Michael Hennerichcc4a48e2011-05-18 14:42:25 +0100292 if (indio_dev == NULL)
293 return -ENOMEM;
294
295 st = iio_priv(indio_dev);
Michael Hennerich7c31b982011-02-24 12:32:45 +0100296
Lars-Peter Clausen2e334602012-11-15 13:15:00 +0000297 if (pdata && pdata->ext_ref)
298 st->ext_ref = AD7298_EXTREF;
299
300 if (st->ext_ref) {
Sachin Kamat7abe9002013-07-23 09:58:00 +0100301 st->reg = devm_regulator_get(&spi->dev, "vref");
302 if (IS_ERR(st->reg))
303 return PTR_ERR(st->reg);
304
Michael Hennerich7c31b982011-02-24 12:32:45 +0100305 ret = regulator_enable(st->reg);
306 if (ret)
Sachin Kamat7abe9002013-07-23 09:58:00 +0100307 return ret;
Michael Hennerich7c31b982011-02-24 12:32:45 +0100308 }
309
Michael Hennerichcc4a48e2011-05-18 14:42:25 +0100310 spi_set_drvdata(spi, indio_dev);
Michael Hennerich7c31b982011-02-24 12:32:45 +0100311
Michael Hennerich7c31b982011-02-24 12:32:45 +0100312 st->spi = spi;
313
Michael Hennerichcc4a48e2011-05-18 14:42:25 +0100314 indio_dev->name = spi_get_device_id(spi)->name;
Michael Hennerichcc4a48e2011-05-18 14:42:25 +0100315 indio_dev->modes = INDIO_DIRECT_MODE;
316 indio_dev->channels = ad7298_channels;
317 indio_dev->num_channels = ARRAY_SIZE(ad7298_channels);
Jonathan Cameron6fe81352011-05-18 14:42:37 +0100318 indio_dev->info = &ad7298_info;
Michael Hennerich7c31b982011-02-24 12:32:45 +0100319
320 /* Setup default message */
321
322 st->scan_single_xfer[0].tx_buf = &st->tx_buf[0];
323 st->scan_single_xfer[0].len = 2;
324 st->scan_single_xfer[0].cs_change = 1;
325 st->scan_single_xfer[1].tx_buf = &st->tx_buf[1];
326 st->scan_single_xfer[1].len = 2;
327 st->scan_single_xfer[1].cs_change = 1;
328 st->scan_single_xfer[2].rx_buf = &st->rx_buf[0];
329 st->scan_single_xfer[2].len = 2;
330
331 spi_message_init(&st->scan_single_msg);
332 spi_message_add_tail(&st->scan_single_xfer[0], &st->scan_single_msg);
333 spi_message_add_tail(&st->scan_single_xfer[1], &st->scan_single_msg);
334 spi_message_add_tail(&st->scan_single_xfer[2], &st->scan_single_msg);
335
Lars-Peter Clausendc4871a2012-11-15 13:15:00 +0000336 ret = iio_triggered_buffer_setup(indio_dev, NULL,
337 &ad7298_trigger_handler, NULL);
Michael Hennerich7c31b982011-02-24 12:32:45 +0100338 if (ret)
Michael Hennerichcc4a48e2011-05-18 14:42:25 +0100339 goto error_disable_reg;
Michael Hennerich7c31b982011-02-24 12:32:45 +0100340
Jonathan Cameron26d25ae2011-09-02 17:14:40 +0100341 ret = iio_device_register(indio_dev);
342 if (ret)
Lars-Peter Clausend045b9d2012-06-18 18:33:51 +0200343 goto error_cleanup_ring;
Michael Hennerichcc4a48e2011-05-18 14:42:25 +0100344
Michael Hennerich7c31b982011-02-24 12:32:45 +0100345 return 0;
346
347error_cleanup_ring:
Lars-Peter Clausendc4871a2012-11-15 13:15:00 +0000348 iio_triggered_buffer_cleanup(indio_dev);
Michael Hennerich7c31b982011-02-24 12:32:45 +0100349error_disable_reg:
Lars-Peter Clausen2e334602012-11-15 13:15:00 +0000350 if (st->ext_ref)
Michael Hennerich7c31b982011-02-24 12:32:45 +0100351 regulator_disable(st->reg);
Michael Hennerichcc4a48e2011-05-18 14:42:25 +0100352
Michael Hennerich7c31b982011-02-24 12:32:45 +0100353 return ret;
354}
355
Greg Kroah-Hartmanfc526922012-12-21 13:21:43 -0800356static int ad7298_remove(struct spi_device *spi)
Michael Hennerich7c31b982011-02-24 12:32:45 +0100357{
Michael Hennerichcc4a48e2011-05-18 14:42:25 +0100358 struct iio_dev *indio_dev = spi_get_drvdata(spi);
359 struct ad7298_state *st = iio_priv(indio_dev);
Michael Hennerich7c31b982011-02-24 12:32:45 +0100360
Jonathan Camerond2fffd62011-10-14 14:46:58 +0100361 iio_device_unregister(indio_dev);
Lars-Peter Clausendc4871a2012-11-15 13:15:00 +0000362 iio_triggered_buffer_cleanup(indio_dev);
Sachin Kamat7abe9002013-07-23 09:58:00 +0100363 if (st->ext_ref)
Michael Hennerich7c31b982011-02-24 12:32:45 +0100364 regulator_disable(st->reg);
Michael Hennerichcc4a48e2011-05-18 14:42:25 +0100365
Michael Hennerich7c31b982011-02-24 12:32:45 +0100366 return 0;
367}
368
369static const struct spi_device_id ad7298_id[] = {
370 {"ad7298", 0},
371 {}
372};
Lars-Peter Clausen55e43902011-11-16 08:53:31 +0100373MODULE_DEVICE_TABLE(spi, ad7298_id);
Michael Hennerich7c31b982011-02-24 12:32:45 +0100374
375static struct spi_driver ad7298_driver = {
376 .driver = {
377 .name = "ad7298",
Michael Hennerich7c31b982011-02-24 12:32:45 +0100378 },
379 .probe = ad7298_probe,
Greg Kroah-Hartmanfc526922012-12-21 13:21:43 -0800380 .remove = ad7298_remove,
Michael Hennerich7c31b982011-02-24 12:32:45 +0100381 .id_table = ad7298_id,
382};
Lars-Peter Clausenae6ae6f2011-11-16 10:13:39 +0100383module_spi_driver(ad7298_driver);
Michael Hennerich7c31b982011-02-24 12:32:45 +0100384
Michael Hennerich9920ed22018-08-14 13:23:17 +0200385MODULE_AUTHOR("Michael Hennerich <michael.hennerich@analog.com>");
Michael Hennerich7c31b982011-02-24 12:32:45 +0100386MODULE_DESCRIPTION("Analog Devices AD7298 ADC");
387MODULE_LICENSE("GPL v2");