blob: 2a3b65c74af9aa32543dc4d248216f31646a1c08 [file] [log] [blame]
Michael Hennerich7c31b982011-02-24 12:32:45 +01001/*
2 * AD7298 SPI ADC driver
3 *
4 * Copyright 2011 Analog Devices Inc.
5 *
6 * Licensed under the GPL-2.
7 */
8
Michael Hennerich7c31b982011-02-24 12:32:45 +01009#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/delay.h>
Paul Gortmaker99c97852011-07-03 15:49:50 -040017#include <linux/module.h>
Lars-Peter Clausendc4871a2012-11-15 13:15:00 +000018#include <linux/interrupt.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
Lars-Peter Clausendc4871a2012-11-15 13:15:00 +000028#define AD7298_WRITE (1 << 15) /* write to the control register */
29#define AD7298_REPEAT (1 << 14) /* repeated conversion enable */
30#define AD7298_CH(x) (1 << (13 - (x))) /* channel select */
31#define AD7298_TSENSE (1 << 5) /* temperature conversion enable */
32#define AD7298_EXTREF (1 << 2) /* external reference enable */
33#define AD7298_TAVG (1 << 1) /* temperature sensor averaging enable */
34#define AD7298_PDD (1 << 0) /* partial power down enable */
35
36#define AD7298_MAX_CHAN 8
37#define AD7298_BITS 12
38#define AD7298_STORAGE_BITS 16
39#define AD7298_INTREF_mV 2500
40
41#define AD7298_CH_TEMP 9
42
43#define RES_MASK(bits) ((1 << (bits)) - 1)
44
45struct ad7298_state {
46 struct spi_device *spi;
47 struct regulator *reg;
48 unsigned ext_ref;
49 struct spi_transfer ring_xfer[10];
50 struct spi_transfer scan_single_xfer[3];
51 struct spi_message ring_msg;
52 struct spi_message scan_single_msg;
53 /*
54 * DMA (thus cache coherency maintenance) requires the
55 * transfer buffers to live in their own cache lines.
56 */
Jonathan Cameronbe7fd3b2012-11-21 18:24:26 +000057 __be16 rx_buf[12] ____cacheline_aligned;
58 __be16 tx_buf[2];
Lars-Peter Clausendc4871a2012-11-15 13:15:00 +000059};
60
Jonathan Cameron95e48f72012-04-13 10:42:54 +010061#define AD7298_V_CHAN(index) \
62 { \
63 .type = IIO_VOLTAGE, \
64 .indexed = 1, \
65 .channel = index, \
Jonathan Cameron40dd6762013-02-27 19:04:29 +000066 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \
67 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE), \
Jonathan Cameron95e48f72012-04-13 10:42:54 +010068 .address = index, \
69 .scan_index = index, \
70 .scan_type = { \
71 .sign = 'u', \
72 .realbits = 12, \
73 .storagebits = 16, \
Lars-Peter Clausenca6546382012-11-15 13:15:00 +000074 .endianness = IIO_BE, \
Jonathan Cameron95e48f72012-04-13 10:42:54 +010075 }, \
76 }
77
Lars-Peter Clausenf4e4b952012-08-09 08:51:00 +010078static const struct iio_chan_spec ad7298_channels[] = {
Jonathan Cameron95e48f72012-04-13 10:42:54 +010079 {
80 .type = IIO_TEMP,
81 .indexed = 1,
82 .channel = 0,
Jonathan Cameron40dd6762013-02-27 19:04:29 +000083 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
84 BIT(IIO_CHAN_INFO_SCALE) |
85 BIT(IIO_CHAN_INFO_OFFSET),
Lars-Peter Clausend045b9d2012-06-18 18:33:51 +020086 .address = AD7298_CH_TEMP,
87 .scan_index = -1,
Jonathan Cameron95e48f72012-04-13 10:42:54 +010088 .scan_type = {
89 .sign = 's',
90 .realbits = 32,
91 .storagebits = 32,
92 },
93 },
94 AD7298_V_CHAN(0),
95 AD7298_V_CHAN(1),
96 AD7298_V_CHAN(2),
97 AD7298_V_CHAN(3),
98 AD7298_V_CHAN(4),
99 AD7298_V_CHAN(5),
100 AD7298_V_CHAN(6),
101 AD7298_V_CHAN(7),
Michael Hennerich01a99e12011-05-18 14:41:55 +0100102 IIO_CHAN_SOFT_TIMESTAMP(8),
103};
104
Lars-Peter Clausendc4871a2012-11-15 13:15:00 +0000105/**
106 * ad7298_update_scan_mode() setup the spi transfer buffer for the new scan mask
107 **/
108static int ad7298_update_scan_mode(struct iio_dev *indio_dev,
109 const unsigned long *active_scan_mask)
110{
111 struct ad7298_state *st = iio_priv(indio_dev);
112 int i, m;
113 unsigned short command;
114 int scan_count;
115
116 /* Now compute overall size */
117 scan_count = bitmap_weight(active_scan_mask, indio_dev->masklength);
118
119 command = AD7298_WRITE | st->ext_ref;
120
121 for (i = 0, m = AD7298_CH(0); i < AD7298_MAX_CHAN; i++, m >>= 1)
122 if (test_bit(i, active_scan_mask))
123 command |= m;
124
125 st->tx_buf[0] = cpu_to_be16(command);
126
127 /* build spi ring message */
128 st->ring_xfer[0].tx_buf = &st->tx_buf[0];
129 st->ring_xfer[0].len = 2;
130 st->ring_xfer[0].cs_change = 1;
131 st->ring_xfer[1].tx_buf = &st->tx_buf[1];
132 st->ring_xfer[1].len = 2;
133 st->ring_xfer[1].cs_change = 1;
134
135 spi_message_init(&st->ring_msg);
136 spi_message_add_tail(&st->ring_xfer[0], &st->ring_msg);
137 spi_message_add_tail(&st->ring_xfer[1], &st->ring_msg);
138
139 for (i = 0; i < scan_count; i++) {
140 st->ring_xfer[i + 2].rx_buf = &st->rx_buf[i];
141 st->ring_xfer[i + 2].len = 2;
142 st->ring_xfer[i + 2].cs_change = 1;
143 spi_message_add_tail(&st->ring_xfer[i + 2], &st->ring_msg);
144 }
145 /* make sure last transfer cs_change is not set */
146 st->ring_xfer[i + 1].cs_change = 0;
147
148 return 0;
149}
150
151/**
152 * ad7298_trigger_handler() bh of trigger launched polling to ring buffer
153 *
154 * Currently there is no option in this driver to disable the saving of
155 * timestamps within the ring.
156 **/
157static irqreturn_t ad7298_trigger_handler(int irq, void *p)
158{
159 struct iio_poll_func *pf = p;
160 struct iio_dev *indio_dev = pf->indio_dev;
161 struct ad7298_state *st = iio_priv(indio_dev);
Lars-Peter Clausendc4871a2012-11-15 13:15:00 +0000162 int b_sent;
163
164 b_sent = spi_sync(st->spi, &st->ring_msg);
165 if (b_sent)
166 goto done;
167
Lars-Peter Clausen85ec2372013-09-19 13:59:00 +0100168 iio_push_to_buffers_with_timestamp(indio_dev, st->rx_buf,
169 iio_get_time_ns());
Lars-Peter Clausendc4871a2012-11-15 13:15:00 +0000170
171done:
172 iio_trigger_notify_done(indio_dev->trig);
173
174 return IRQ_HANDLED;
175}
176
Michael Hennerich7c31b982011-02-24 12:32:45 +0100177static int ad7298_scan_direct(struct ad7298_state *st, unsigned ch)
178{
179 int ret;
180 st->tx_buf[0] = cpu_to_be16(AD7298_WRITE | st->ext_ref |
181 (AD7298_CH(0) >> ch));
182
183 ret = spi_sync(st->spi, &st->scan_single_msg);
184 if (ret)
185 return ret;
186
187 return be16_to_cpu(st->rx_buf[0]);
188}
189
Michael Hennerich01a99e12011-05-18 14:41:55 +0100190static int ad7298_scan_temp(struct ad7298_state *st, int *val)
Michael Hennerich7c31b982011-02-24 12:32:45 +0100191{
Lars-Peter Clausen01a10e02012-11-15 13:15:00 +0000192 int ret;
Dan Carpenterc1a75282011-11-22 10:39:15 +0300193 __be16 buf;
Michael Hennerich7c31b982011-02-24 12:32:45 +0100194
Dan Carpenterc1a75282011-11-22 10:39:15 +0300195 buf = cpu_to_be16(AD7298_WRITE | AD7298_TSENSE |
Michael Hennerich7c31b982011-02-24 12:32:45 +0100196 AD7298_TAVG | st->ext_ref);
197
Dan Carpenterc1a75282011-11-22 10:39:15 +0300198 ret = spi_write(st->spi, (u8 *)&buf, 2);
Michael Hennerich01a99e12011-05-18 14:41:55 +0100199 if (ret)
200 return ret;
201
Dan Carpenterc1a75282011-11-22 10:39:15 +0300202 buf = cpu_to_be16(0);
Michael Hennerich01a99e12011-05-18 14:41:55 +0100203
Dan Carpenterc1a75282011-11-22 10:39:15 +0300204 ret = spi_write(st->spi, (u8 *)&buf, 2);
Michael Hennerich01a99e12011-05-18 14:41:55 +0100205 if (ret)
206 return ret;
207
Michael Hennerich7c31b982011-02-24 12:32:45 +0100208 usleep_range(101, 1000); /* sleep > 100us */
Michael Hennerich01a99e12011-05-18 14:41:55 +0100209
Dan Carpenterc1a75282011-11-22 10:39:15 +0300210 ret = spi_read(st->spi, (u8 *)&buf, 2);
Michael Hennerich01a99e12011-05-18 14:41:55 +0100211 if (ret)
212 return ret;
Michael Hennerich7c31b982011-02-24 12:32:45 +0100213
Lars-Peter Clausen01a10e02012-11-15 13:15:00 +0000214 *val = sign_extend32(be16_to_cpu(buf), 11);
Michael Hennerich01a99e12011-05-18 14:41:55 +0100215
216 return 0;
Michael Hennerich7c31b982011-02-24 12:32:45 +0100217}
218
Lars-Peter Clausen01a10e02012-11-15 13:15:00 +0000219static int ad7298_get_ref_voltage(struct ad7298_state *st)
220{
221 int vref;
222
223 if (st->ext_ref) {
224 vref = regulator_get_voltage(st->reg);
225 if (vref < 0)
226 return vref;
227
228 return vref / 1000;
229 } else {
230 return AD7298_INTREF_mV;
231 }
232}
233
Jonathan Cameron84f79ec2011-10-06 17:14:37 +0100234static int ad7298_read_raw(struct iio_dev *indio_dev,
Michael Hennerich01a99e12011-05-18 14:41:55 +0100235 struct iio_chan_spec const *chan,
236 int *val,
237 int *val2,
238 long m)
Michael Hennerich7c31b982011-02-24 12:32:45 +0100239{
Michael Hennerich01a99e12011-05-18 14:41:55 +0100240 int ret;
Jonathan Cameron84f79ec2011-10-06 17:14:37 +0100241 struct ad7298_state *st = iio_priv(indio_dev);
Michael Hennerich7c31b982011-02-24 12:32:45 +0100242
Michael Hennerich01a99e12011-05-18 14:41:55 +0100243 switch (m) {
Jonathan Cameronb11f98f2012-04-15 17:41:18 +0100244 case IIO_CHAN_INFO_RAW:
Jonathan Cameron84f79ec2011-10-06 17:14:37 +0100245 mutex_lock(&indio_dev->mlock);
Jonathan Cameron389ac482011-12-05 22:18:19 +0000246 if (indio_dev->currentmode == INDIO_BUFFER_TRIGGERED) {
247 ret = -EBUSY;
Michael Hennerich01a99e12011-05-18 14:41:55 +0100248 } else {
249 if (chan->address == AD7298_CH_TEMP)
250 ret = ad7298_scan_temp(st, val);
251 else
252 ret = ad7298_scan_direct(st, chan->address);
253 }
Jonathan Cameron84f79ec2011-10-06 17:14:37 +0100254 mutex_unlock(&indio_dev->mlock);
Michael Hennerich01a99e12011-05-18 14:41:55 +0100255
256 if (ret < 0)
257 return ret;
258
259 if (chan->address != AD7298_CH_TEMP)
260 *val = ret & RES_MASK(AD7298_BITS);
261
262 return IIO_VAL_INT;
Jonathan Cameronc8a9f802011-10-26 17:41:36 +0100263 case IIO_CHAN_INFO_SCALE:
264 switch (chan->type) {
265 case IIO_VOLTAGE:
Lars-Peter Clausen01a10e02012-11-15 13:15:00 +0000266 *val = ad7298_get_ref_voltage(st);
Lars-Peter Clausen2e334602012-11-15 13:15:00 +0000267 *val2 = chan->scan_type.realbits;
268 return IIO_VAL_FRACTIONAL_LOG2;
Jonathan Cameronc8a9f802011-10-26 17:41:36 +0100269 case IIO_TEMP:
Lars-Peter Clausen01a10e02012-11-15 13:15:00 +0000270 *val = ad7298_get_ref_voltage(st);
271 *val2 = 10;
272 return IIO_VAL_FRACTIONAL;
Jonathan Cameronc8a9f802011-10-26 17:41:36 +0100273 default:
274 return -EINVAL;
275 }
Lars-Peter Clausen01a10e02012-11-15 13:15:00 +0000276 case IIO_CHAN_INFO_OFFSET:
277 *val = 1093 - 2732500 / ad7298_get_ref_voltage(st);
278 return IIO_VAL_INT;
Michael Hennerich01a99e12011-05-18 14:41:55 +0100279 }
280 return -EINVAL;
Michael Hennerich7c31b982011-02-24 12:32:45 +0100281}
Michael Hennerich7c31b982011-02-24 12:32:45 +0100282
Jonathan Cameron6fe81352011-05-18 14:42:37 +0100283static const struct iio_info ad7298_info = {
284 .read_raw = &ad7298_read_raw,
Lars-Peter Clausend045b9d2012-06-18 18:33:51 +0200285 .update_scan_mode = ad7298_update_scan_mode,
Jonathan Cameron6fe81352011-05-18 14:42:37 +0100286 .driver_module = THIS_MODULE,
287};
288
Greg Kroah-Hartmanfc526922012-12-21 13:21:43 -0800289static int ad7298_probe(struct spi_device *spi)
Michael Hennerich7c31b982011-02-24 12:32:45 +0100290{
291 struct ad7298_platform_data *pdata = spi->dev.platform_data;
292 struct ad7298_state *st;
Sachin Kamat7abe9002013-07-23 09:58:00 +0100293 struct iio_dev *indio_dev;
Lars-Peter Clausen2e334602012-11-15 13:15:00 +0000294 int ret;
Michael Hennerich7c31b982011-02-24 12:32:45 +0100295
Sachin Kamat7abe9002013-07-23 09:58:00 +0100296 indio_dev = devm_iio_device_alloc(&spi->dev, sizeof(*st));
Michael Hennerichcc4a48e2011-05-18 14:42:25 +0100297 if (indio_dev == NULL)
298 return -ENOMEM;
299
300 st = iio_priv(indio_dev);
Michael Hennerich7c31b982011-02-24 12:32:45 +0100301
Lars-Peter Clausen2e334602012-11-15 13:15:00 +0000302 if (pdata && pdata->ext_ref)
303 st->ext_ref = AD7298_EXTREF;
304
305 if (st->ext_ref) {
Sachin Kamat7abe9002013-07-23 09:58:00 +0100306 st->reg = devm_regulator_get(&spi->dev, "vref");
307 if (IS_ERR(st->reg))
308 return PTR_ERR(st->reg);
309
Michael Hennerich7c31b982011-02-24 12:32:45 +0100310 ret = regulator_enable(st->reg);
311 if (ret)
Sachin Kamat7abe9002013-07-23 09:58:00 +0100312 return ret;
Michael Hennerich7c31b982011-02-24 12:32:45 +0100313 }
314
Michael Hennerichcc4a48e2011-05-18 14:42:25 +0100315 spi_set_drvdata(spi, indio_dev);
Michael Hennerich7c31b982011-02-24 12:32:45 +0100316
Michael Hennerich7c31b982011-02-24 12:32:45 +0100317 st->spi = spi;
318
Michael Hennerichcc4a48e2011-05-18 14:42:25 +0100319 indio_dev->name = spi_get_device_id(spi)->name;
320 indio_dev->dev.parent = &spi->dev;
Michael Hennerichcc4a48e2011-05-18 14:42:25 +0100321 indio_dev->modes = INDIO_DIRECT_MODE;
322 indio_dev->channels = ad7298_channels;
323 indio_dev->num_channels = ARRAY_SIZE(ad7298_channels);
Jonathan Cameron6fe81352011-05-18 14:42:37 +0100324 indio_dev->info = &ad7298_info;
Michael Hennerich7c31b982011-02-24 12:32:45 +0100325
326 /* Setup default message */
327
328 st->scan_single_xfer[0].tx_buf = &st->tx_buf[0];
329 st->scan_single_xfer[0].len = 2;
330 st->scan_single_xfer[0].cs_change = 1;
331 st->scan_single_xfer[1].tx_buf = &st->tx_buf[1];
332 st->scan_single_xfer[1].len = 2;
333 st->scan_single_xfer[1].cs_change = 1;
334 st->scan_single_xfer[2].rx_buf = &st->rx_buf[0];
335 st->scan_single_xfer[2].len = 2;
336
337 spi_message_init(&st->scan_single_msg);
338 spi_message_add_tail(&st->scan_single_xfer[0], &st->scan_single_msg);
339 spi_message_add_tail(&st->scan_single_xfer[1], &st->scan_single_msg);
340 spi_message_add_tail(&st->scan_single_xfer[2], &st->scan_single_msg);
341
Lars-Peter Clausendc4871a2012-11-15 13:15:00 +0000342 ret = iio_triggered_buffer_setup(indio_dev, NULL,
343 &ad7298_trigger_handler, NULL);
Michael Hennerich7c31b982011-02-24 12:32:45 +0100344 if (ret)
Michael Hennerichcc4a48e2011-05-18 14:42:25 +0100345 goto error_disable_reg;
Michael Hennerich7c31b982011-02-24 12:32:45 +0100346
Jonathan Cameron26d25ae2011-09-02 17:14:40 +0100347 ret = iio_device_register(indio_dev);
348 if (ret)
Lars-Peter Clausend045b9d2012-06-18 18:33:51 +0200349 goto error_cleanup_ring;
Michael Hennerichcc4a48e2011-05-18 14:42:25 +0100350
Michael Hennerich7c31b982011-02-24 12:32:45 +0100351 return 0;
352
353error_cleanup_ring:
Lars-Peter Clausendc4871a2012-11-15 13:15:00 +0000354 iio_triggered_buffer_cleanup(indio_dev);
Michael Hennerich7c31b982011-02-24 12:32:45 +0100355error_disable_reg:
Lars-Peter Clausen2e334602012-11-15 13:15:00 +0000356 if (st->ext_ref)
Michael Hennerich7c31b982011-02-24 12:32:45 +0100357 regulator_disable(st->reg);
Michael Hennerichcc4a48e2011-05-18 14:42:25 +0100358
Michael Hennerich7c31b982011-02-24 12:32:45 +0100359 return ret;
360}
361
Greg Kroah-Hartmanfc526922012-12-21 13:21:43 -0800362static int ad7298_remove(struct spi_device *spi)
Michael Hennerich7c31b982011-02-24 12:32:45 +0100363{
Michael Hennerichcc4a48e2011-05-18 14:42:25 +0100364 struct iio_dev *indio_dev = spi_get_drvdata(spi);
365 struct ad7298_state *st = iio_priv(indio_dev);
Michael Hennerich7c31b982011-02-24 12:32:45 +0100366
Jonathan Camerond2fffd62011-10-14 14:46:58 +0100367 iio_device_unregister(indio_dev);
Lars-Peter Clausendc4871a2012-11-15 13:15:00 +0000368 iio_triggered_buffer_cleanup(indio_dev);
Sachin Kamat7abe9002013-07-23 09:58:00 +0100369 if (st->ext_ref)
Michael Hennerich7c31b982011-02-24 12:32:45 +0100370 regulator_disable(st->reg);
Michael Hennerichcc4a48e2011-05-18 14:42:25 +0100371
Michael Hennerich7c31b982011-02-24 12:32:45 +0100372 return 0;
373}
374
375static const struct spi_device_id ad7298_id[] = {
376 {"ad7298", 0},
377 {}
378};
Lars-Peter Clausen55e43902011-11-16 08:53:31 +0100379MODULE_DEVICE_TABLE(spi, ad7298_id);
Michael Hennerich7c31b982011-02-24 12:32:45 +0100380
381static struct spi_driver ad7298_driver = {
382 .driver = {
383 .name = "ad7298",
Michael Hennerich7c31b982011-02-24 12:32:45 +0100384 .owner = THIS_MODULE,
385 },
386 .probe = ad7298_probe,
Greg Kroah-Hartmanfc526922012-12-21 13:21:43 -0800387 .remove = ad7298_remove,
Michael Hennerich7c31b982011-02-24 12:32:45 +0100388 .id_table = ad7298_id,
389};
Lars-Peter Clausenae6ae6f2011-11-16 10:13:39 +0100390module_spi_driver(ad7298_driver);
Michael Hennerich7c31b982011-02-24 12:32:45 +0100391
392MODULE_AUTHOR("Michael Hennerich <hennerich@blackfin.uclinux.org>");
393MODULE_DESCRIPTION("Analog Devices AD7298 ADC");
394MODULE_LICENSE("GPL v2");