blob: 3212eb4c0f2512d429b4aa725049bbea413796e7 [file] [log] [blame]
Thomas Gleixnerfda8d262019-05-28 09:57:06 -07001// SPDX-License-Identifier: GPL-2.0-only
Christophe Leroy0eac2592013-02-13 06:47:00 +00002/*
Lars-Peter Clausenf2f7a442013-03-04 19:30:00 +00003 * AD7904/AD7914/AD7923/AD7924 SPI ADC driver
Christophe Leroy0eac2592013-02-13 06:47:00 +00004 *
5 * Copyright 2011 Analog Devices Inc (from AD7923 Driver)
6 * Copyright 2012 CS Systemes d'Information
Christophe Leroy0eac2592013-02-13 06:47:00 +00007 */
8
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>
Lars-Peter Clausenecf6ca22013-03-04 19:30:00 +000014#include <linux/regulator/consumer.h>
Christophe Leroy0eac2592013-02-13 06:47:00 +000015#include <linux/err.h>
16#include <linux/delay.h>
17#include <linux/module.h>
18#include <linux/interrupt.h>
19
20#include <linux/iio/iio.h>
21#include <linux/iio/sysfs.h>
22#include <linux/iio/buffer.h>
23#include <linux/iio/trigger_consumer.h>
24#include <linux/iio/triggered_buffer.h>
25
Bárbara Fernandesc1fc8bb2019-02-22 17:31:57 -030026#define AD7923_WRITE_CR BIT(11) /* write control register */
27#define AD7923_RANGE BIT(1) /* range to REFin */
28#define AD7923_CODING BIT(0) /* coding is straight binary */
Christophe Leroy0eac2592013-02-13 06:47:00 +000029#define AD7923_PM_MODE_AS (1) /* auto shutdown */
30#define AD7923_PM_MODE_FS (2) /* full shutdown */
31#define AD7923_PM_MODE_OPS (3) /* normal operation */
32#define AD7923_CHANNEL_0 (0) /* analog input 0 */
33#define AD7923_CHANNEL_1 (1) /* analog input 1 */
34#define AD7923_CHANNEL_2 (2) /* analog input 2 */
35#define AD7923_CHANNEL_3 (3) /* analog input 3 */
36#define AD7923_SEQUENCE_OFF (0) /* no sequence fonction */
37#define AD7923_SEQUENCE_PROTECT (2) /* no interrupt write cycle */
38#define AD7923_SEQUENCE_ON (3) /* continuous sequence */
39
40#define AD7923_MAX_CHAN 4
41
Bárbara Fernandes385c3012019-02-22 17:31:58 -030042#define AD7923_PM_MODE_WRITE(mode) ((mode) << 4) /* write mode */
43#define AD7923_CHANNEL_WRITE(channel) ((channel) << 6) /* write channel */
44#define AD7923_SEQUENCE_WRITE(sequence) ((((sequence) & 1) << 3) \
45 + (((sequence) & 2) << 9))
Christophe Leroy0eac2592013-02-13 06:47:00 +000046 /* write sequence fonction */
47/* left shift for CR : bit 11 transmit in first */
48#define AD7923_SHIFT_REGISTER 4
49
50/* val = value, dec = left shift, bits = number of bits of the mask */
Bárbara Fernandes385c3012019-02-22 17:31:58 -030051#define EXTRACT(val, dec, bits) (((val) >> (dec)) & ((1 << (bits)) - 1))
Christophe Leroy0eac2592013-02-13 06:47:00 +000052
53struct ad7923_state {
54 struct spi_device *spi;
55 struct spi_transfer ring_xfer[5];
56 struct spi_transfer scan_single_xfer[2];
57 struct spi_message ring_msg;
58 struct spi_message scan_single_msg;
Lars-Peter Clausenecf6ca22013-03-04 19:30:00 +000059
60 struct regulator *reg;
61
62 unsigned int settings;
63
Christophe Leroy0eac2592013-02-13 06:47:00 +000064 /*
65 * DMA (thus cache coherency maintenance) requires the
66 * transfer buffers to live in their own cache lines.
67 */
68 __be16 rx_buf[4] ____cacheline_aligned;
69 __be16 tx_buf[4];
70};
71
Lars-Peter Clausenf2f7a442013-03-04 19:30:00 +000072struct ad7923_chip_info {
73 const struct iio_chan_spec *channels;
74 unsigned int num_channels;
75};
76
77enum ad7923_id {
78 AD7904,
79 AD7914,
80 AD7924,
81};
82
83#define AD7923_V_CHAN(index, bits) \
Christophe Leroy0eac2592013-02-13 06:47:00 +000084 { \
85 .type = IIO_VOLTAGE, \
86 .indexed = 1, \
87 .channel = index, \
Jonathan Cameron76577192013-03-03 12:26:47 +000088 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \
89 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE), \
Christophe Leroy0eac2592013-02-13 06:47:00 +000090 .address = index, \
91 .scan_index = index, \
92 .scan_type = { \
93 .sign = 'u', \
Lars-Peter Clausenf2f7a442013-03-04 19:30:00 +000094 .realbits = (bits), \
Christophe Leroy0eac2592013-02-13 06:47:00 +000095 .storagebits = 16, \
96 .endianness = IIO_BE, \
97 }, \
98 }
99
Lars-Peter Clausenf2f7a442013-03-04 19:30:00 +0000100#define DECLARE_AD7923_CHANNELS(name, bits) \
101const struct iio_chan_spec name ## _channels[] = { \
102 AD7923_V_CHAN(0, bits), \
103 AD7923_V_CHAN(1, bits), \
104 AD7923_V_CHAN(2, bits), \
105 AD7923_V_CHAN(3, bits), \
106 IIO_CHAN_SOFT_TIMESTAMP(4), \
107}
108
109static DECLARE_AD7923_CHANNELS(ad7904, 8);
110static DECLARE_AD7923_CHANNELS(ad7914, 10);
111static DECLARE_AD7923_CHANNELS(ad7924, 12);
112
113static const struct ad7923_chip_info ad7923_chip_info[] = {
114 [AD7904] = {
115 .channels = ad7904_channels,
116 .num_channels = ARRAY_SIZE(ad7904_channels),
117 },
118 [AD7914] = {
119 .channels = ad7914_channels,
120 .num_channels = ARRAY_SIZE(ad7914_channels),
121 },
122 [AD7924] = {
123 .channels = ad7924_channels,
124 .num_channels = ARRAY_SIZE(ad7924_channels),
125 },
Christophe Leroy0eac2592013-02-13 06:47:00 +0000126};
127
128/**
129 * ad7923_update_scan_mode() setup the spi transfer buffer for the new scan mask
130 **/
131static int ad7923_update_scan_mode(struct iio_dev *indio_dev,
Bárbara Fernandes66deb5c2019-02-22 17:31:56 -0300132 const unsigned long *active_scan_mask)
Christophe Leroy0eac2592013-02-13 06:47:00 +0000133{
134 struct ad7923_state *st = iio_priv(indio_dev);
135 int i, cmd, len;
136
137 len = 0;
138 for_each_set_bit(i, active_scan_mask, AD7923_MAX_CHAN) {
Lars-Peter Clausenecf6ca22013-03-04 19:30:00 +0000139 cmd = AD7923_WRITE_CR | AD7923_CHANNEL_WRITE(i) |
Christophe Leroy0eac2592013-02-13 06:47:00 +0000140 AD7923_SEQUENCE_WRITE(AD7923_SEQUENCE_OFF) |
Lars-Peter Clausenecf6ca22013-03-04 19:30:00 +0000141 st->settings;
Christophe Leroy0eac2592013-02-13 06:47:00 +0000142 cmd <<= AD7923_SHIFT_REGISTER;
143 st->tx_buf[len++] = cpu_to_be16(cmd);
144 }
145 /* build spi ring message */
146 st->ring_xfer[0].tx_buf = &st->tx_buf[0];
147 st->ring_xfer[0].len = len;
148 st->ring_xfer[0].cs_change = 1;
149
150 spi_message_init(&st->ring_msg);
151 spi_message_add_tail(&st->ring_xfer[0], &st->ring_msg);
152
153 for (i = 0; i < len; i++) {
154 st->ring_xfer[i + 1].rx_buf = &st->rx_buf[i];
155 st->ring_xfer[i + 1].len = 2;
156 st->ring_xfer[i + 1].cs_change = 1;
157 spi_message_add_tail(&st->ring_xfer[i + 1], &st->ring_msg);
158 }
159 /* make sure last transfer cs_change is not set */
160 st->ring_xfer[i + 1].cs_change = 0;
161
162 return 0;
163}
164
165/**
166 * ad7923_trigger_handler() bh of trigger launched polling to ring buffer
167 *
168 * Currently there is no option in this driver to disable the saving of
169 * timestamps within the ring.
170 **/
171static irqreturn_t ad7923_trigger_handler(int irq, void *p)
172{
173 struct iio_poll_func *pf = p;
174 struct iio_dev *indio_dev = pf->indio_dev;
175 struct ad7923_state *st = iio_priv(indio_dev);
Christophe Leroy0eac2592013-02-13 06:47:00 +0000176 int b_sent;
177
178 b_sent = spi_sync(st->spi, &st->ring_msg);
179 if (b_sent)
180 goto done;
181
Lars-Peter Clausen9f4fa4f2013-09-19 13:59:00 +0100182 iio_push_to_buffers_with_timestamp(indio_dev, st->rx_buf,
Bárbara Fernandes66deb5c2019-02-22 17:31:56 -0300183 iio_get_time_ns(indio_dev));
Christophe Leroy0eac2592013-02-13 06:47:00 +0000184
185done:
186 iio_trigger_notify_done(indio_dev->trig);
187
188 return IRQ_HANDLED;
189}
190
191static int ad7923_scan_direct(struct ad7923_state *st, unsigned ch)
192{
193 int ret, cmd;
194
Lars-Peter Clausenecf6ca22013-03-04 19:30:00 +0000195 cmd = AD7923_WRITE_CR | AD7923_CHANNEL_WRITE(ch) |
196 AD7923_SEQUENCE_WRITE(AD7923_SEQUENCE_OFF) |
197 st->settings;
Christophe Leroy0eac2592013-02-13 06:47:00 +0000198 cmd <<= AD7923_SHIFT_REGISTER;
199 st->tx_buf[0] = cpu_to_be16(cmd);
200
201 ret = spi_sync(st->spi, &st->scan_single_msg);
202 if (ret)
203 return ret;
204
205 return be16_to_cpu(st->rx_buf[0]);
206}
207
Lars-Peter Clausenecf6ca22013-03-04 19:30:00 +0000208static int ad7923_get_range(struct ad7923_state *st)
209{
210 int vref;
211
212 vref = regulator_get_voltage(st->reg);
213 if (vref < 0)
214 return vref;
215
216 vref /= 1000;
217
218 if (!(st->settings & AD7923_RANGE))
219 vref *= 2;
220
221 return vref;
222}
223
Christophe Leroy0eac2592013-02-13 06:47:00 +0000224static int ad7923_read_raw(struct iio_dev *indio_dev,
225 struct iio_chan_spec const *chan,
226 int *val,
227 int *val2,
228 long m)
229{
230 int ret;
231 struct ad7923_state *st = iio_priv(indio_dev);
232
233 switch (m) {
234 case IIO_CHAN_INFO_RAW:
Alison Schofield9f57e062016-05-24 12:19:49 -0700235 ret = iio_device_claim_direct_mode(indio_dev);
236 if (ret)
237 return ret;
238 ret = ad7923_scan_direct(st, chan->address);
239 iio_device_release_direct_mode(indio_dev);
Christophe Leroy0eac2592013-02-13 06:47:00 +0000240
241 if (ret < 0)
242 return ret;
243
244 if (chan->address == EXTRACT(ret, 12, 4))
245 *val = EXTRACT(ret, 0, 12);
Lars-Peter Clausen135f0642013-03-04 19:30:00 +0000246 else
247 return -EIO;
Christophe Leroy0eac2592013-02-13 06:47:00 +0000248
249 return IIO_VAL_INT;
Lars-Peter Clausenecf6ca22013-03-04 19:30:00 +0000250 case IIO_CHAN_INFO_SCALE:
251 ret = ad7923_get_range(st);
252 if (ret < 0)
253 return ret;
254 *val = ret;
255 *val2 = chan->scan_type.realbits;
256 return IIO_VAL_FRACTIONAL_LOG2;
Christophe Leroy0eac2592013-02-13 06:47:00 +0000257 }
258 return -EINVAL;
259}
260
261static const struct iio_info ad7923_info = {
262 .read_raw = &ad7923_read_raw,
263 .update_scan_mode = ad7923_update_scan_mode,
Christophe Leroy0eac2592013-02-13 06:47:00 +0000264};
265
266static int ad7923_probe(struct spi_device *spi)
267{
268 struct ad7923_state *st;
Sachin Kamate59576d2013-07-23 09:58:00 +0100269 struct iio_dev *indio_dev;
Lars-Peter Clausenf2f7a442013-03-04 19:30:00 +0000270 const struct ad7923_chip_info *info;
Christophe Leroy0eac2592013-02-13 06:47:00 +0000271 int ret;
272
Sachin Kamate59576d2013-07-23 09:58:00 +0100273 indio_dev = devm_iio_device_alloc(&spi->dev, sizeof(*st));
Bárbara Fernandes0a39ac22019-02-22 17:31:59 -0300274 if (!indio_dev)
Christophe Leroy0eac2592013-02-13 06:47:00 +0000275 return -ENOMEM;
276
277 st = iio_priv(indio_dev);
278
279 spi_set_drvdata(spi, indio_dev);
280
281 st->spi = spi;
Lars-Peter Clausenecf6ca22013-03-04 19:30:00 +0000282 st->settings = AD7923_CODING | AD7923_RANGE |
283 AD7923_PM_MODE_WRITE(AD7923_PM_MODE_OPS);
Christophe Leroy0eac2592013-02-13 06:47:00 +0000284
Lars-Peter Clausenf2f7a442013-03-04 19:30:00 +0000285 info = &ad7923_chip_info[spi_get_device_id(spi)->driver_data];
286
Christophe Leroy0eac2592013-02-13 06:47:00 +0000287 indio_dev->name = spi_get_device_id(spi)->name;
288 indio_dev->dev.parent = &spi->dev;
Matt Ranostayb541eaf2016-07-02 17:26:33 -0700289 indio_dev->dev.of_node = spi->dev.of_node;
Christophe Leroy0eac2592013-02-13 06:47:00 +0000290 indio_dev->modes = INDIO_DIRECT_MODE;
Lars-Peter Clausenf2f7a442013-03-04 19:30:00 +0000291 indio_dev->channels = info->channels;
292 indio_dev->num_channels = info->num_channels;
Christophe Leroy0eac2592013-02-13 06:47:00 +0000293 indio_dev->info = &ad7923_info;
294
295 /* Setup default message */
296
297 st->scan_single_xfer[0].tx_buf = &st->tx_buf[0];
298 st->scan_single_xfer[0].len = 2;
299 st->scan_single_xfer[0].cs_change = 1;
300 st->scan_single_xfer[1].rx_buf = &st->rx_buf[0];
301 st->scan_single_xfer[1].len = 2;
302
303 spi_message_init(&st->scan_single_msg);
304 spi_message_add_tail(&st->scan_single_xfer[0], &st->scan_single_msg);
305 spi_message_add_tail(&st->scan_single_xfer[1], &st->scan_single_msg);
306
Sachin Kamate59576d2013-07-23 09:58:00 +0100307 st->reg = devm_regulator_get(&spi->dev, "refin");
308 if (IS_ERR(st->reg))
309 return PTR_ERR(st->reg);
310
Lars-Peter Clausenecf6ca22013-03-04 19:30:00 +0000311 ret = regulator_enable(st->reg);
312 if (ret)
Sachin Kamate59576d2013-07-23 09:58:00 +0100313 return ret;
Lars-Peter Clausenecf6ca22013-03-04 19:30:00 +0000314
Christophe Leroy0eac2592013-02-13 06:47:00 +0000315 ret = iio_triggered_buffer_setup(indio_dev, NULL,
Bárbara Fernandes66deb5c2019-02-22 17:31:56 -0300316 &ad7923_trigger_handler, NULL);
Christophe Leroy0eac2592013-02-13 06:47:00 +0000317 if (ret)
Lars-Peter Clausenecf6ca22013-03-04 19:30:00 +0000318 goto error_disable_reg;
Christophe Leroy0eac2592013-02-13 06:47:00 +0000319
320 ret = iio_device_register(indio_dev);
321 if (ret)
322 goto error_cleanup_ring;
323
324 return 0;
325
326error_cleanup_ring:
327 iio_triggered_buffer_cleanup(indio_dev);
Lars-Peter Clausenecf6ca22013-03-04 19:30:00 +0000328error_disable_reg:
329 regulator_disable(st->reg);
Christophe Leroy0eac2592013-02-13 06:47:00 +0000330
331 return ret;
332}
333
334static int ad7923_remove(struct spi_device *spi)
335{
336 struct iio_dev *indio_dev = spi_get_drvdata(spi);
Lars-Peter Clausenecf6ca22013-03-04 19:30:00 +0000337 struct ad7923_state *st = iio_priv(indio_dev);
Christophe Leroy0eac2592013-02-13 06:47:00 +0000338
339 iio_device_unregister(indio_dev);
340 iio_triggered_buffer_cleanup(indio_dev);
Lars-Peter Clausenecf6ca22013-03-04 19:30:00 +0000341 regulator_disable(st->reg);
Christophe Leroy0eac2592013-02-13 06:47:00 +0000342
343 return 0;
344}
345
346static const struct spi_device_id ad7923_id[] = {
Lars-Peter Clausenf2f7a442013-03-04 19:30:00 +0000347 {"ad7904", AD7904},
348 {"ad7914", AD7914},
349 {"ad7923", AD7924},
350 {"ad7924", AD7924},
Christophe Leroy0eac2592013-02-13 06:47:00 +0000351 {}
352};
353MODULE_DEVICE_TABLE(spi, ad7923_id);
354
355static struct spi_driver ad7923_driver = {
356 .driver = {
357 .name = "ad7923",
Christophe Leroy0eac2592013-02-13 06:47:00 +0000358 },
359 .probe = ad7923_probe,
360 .remove = ad7923_remove,
361 .id_table = ad7923_id,
362};
363module_spi_driver(ad7923_driver);
364
Michael Hennerich9920ed22018-08-14 13:23:17 +0200365MODULE_AUTHOR("Michael Hennerich <michael.hennerich@analog.com>");
Christophe Leroy0eac2592013-02-13 06:47:00 +0000366MODULE_AUTHOR("Patrick Vasseur <patrick.vasseur@c-s.fr>");
Lars-Peter Clausenf2f7a442013-03-04 19:30:00 +0000367MODULE_DESCRIPTION("Analog Devices AD7904/AD7914/AD7923/AD7924 ADC");
Christophe Leroy0eac2592013-02-13 06:47:00 +0000368MODULE_LICENSE("GPL v2");