blob: 4a8c0a2f49b6959f5528f80124f4d10a2d9ae9f9 [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>
Peter Meerwald48898052014-12-06 06:00:00 +000019#include <linux/bitops.h>
Michael Hennerich7c31b982011-02-24 12:32:45 +010020
Jonathan Cameron06458e22012-04-25 15:54:58 +010021#include <linux/iio/iio.h>
22#include <linux/iio/sysfs.h>
23#include <linux/iio/buffer.h>
Lars-Peter Clausendc4871a2012-11-15 13:15:00 +000024#include <linux/iio/trigger_consumer.h>
25#include <linux/iio/triggered_buffer.h>
Michael Hennerich7c31b982011-02-24 12:32:45 +010026
Lars-Peter Clausen709ab362012-11-17 11:42:59 +000027#include <linux/platform_data/ad7298.h>
Michael Hennerich7c31b982011-02-24 12:32:45 +010028
Peter Meerwald48898052014-12-06 06:00:00 +000029#define AD7298_WRITE BIT(15) /* write to the control register */
30#define AD7298_REPEAT BIT(14) /* repeated conversion enable */
31#define AD7298_CH(x) BIT(13 - (x)) /* channel select */
32#define AD7298_TSENSE BIT(5) /* temperature conversion enable */
33#define AD7298_EXTREF BIT(2) /* external reference enable */
34#define AD7298_TAVG BIT(1) /* temperature sensor averaging enable */
35#define AD7298_PDD BIT(0) /* partial power down enable */
Lars-Peter Clausendc4871a2012-11-15 13:15:00 +000036
37#define AD7298_MAX_CHAN 8
Lars-Peter Clausendc4871a2012-11-15 13:15:00 +000038#define AD7298_INTREF_mV 2500
39
40#define AD7298_CH_TEMP 9
41
Lars-Peter Clausendc4871a2012-11-15 13:15:00 +000042struct ad7298_state {
43 struct spi_device *spi;
44 struct regulator *reg;
45 unsigned ext_ref;
46 struct spi_transfer ring_xfer[10];
47 struct spi_transfer scan_single_xfer[3];
48 struct spi_message ring_msg;
49 struct spi_message scan_single_msg;
50 /*
51 * DMA (thus cache coherency maintenance) requires the
52 * transfer buffers to live in their own cache lines.
53 */
Jonathan Cameronbe7fd3b2012-11-21 18:24:26 +000054 __be16 rx_buf[12] ____cacheline_aligned;
55 __be16 tx_buf[2];
Lars-Peter Clausendc4871a2012-11-15 13:15:00 +000056};
57
Jonathan Cameron95e48f72012-04-13 10:42:54 +010058#define AD7298_V_CHAN(index) \
59 { \
60 .type = IIO_VOLTAGE, \
61 .indexed = 1, \
62 .channel = index, \
Jonathan Cameron40dd6762013-02-27 19:04:29 +000063 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \
64 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE), \
Jonathan Cameron95e48f72012-04-13 10:42:54 +010065 .address = index, \
66 .scan_index = index, \
67 .scan_type = { \
68 .sign = 'u', \
69 .realbits = 12, \
70 .storagebits = 16, \
Lars-Peter Clausenca6546382012-11-15 13:15:00 +000071 .endianness = IIO_BE, \
Jonathan Cameron95e48f72012-04-13 10:42:54 +010072 }, \
73 }
74
Lars-Peter Clausenf4e4b952012-08-09 08:51:00 +010075static const struct iio_chan_spec ad7298_channels[] = {
Jonathan Cameron95e48f72012-04-13 10:42:54 +010076 {
77 .type = IIO_TEMP,
78 .indexed = 1,
79 .channel = 0,
Jonathan Cameron40dd6762013-02-27 19:04:29 +000080 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
81 BIT(IIO_CHAN_INFO_SCALE) |
82 BIT(IIO_CHAN_INFO_OFFSET),
Lars-Peter Clausend045b9d2012-06-18 18:33:51 +020083 .address = AD7298_CH_TEMP,
84 .scan_index = -1,
Jonathan Cameron95e48f72012-04-13 10:42:54 +010085 .scan_type = {
86 .sign = 's',
87 .realbits = 32,
88 .storagebits = 32,
89 },
90 },
91 AD7298_V_CHAN(0),
92 AD7298_V_CHAN(1),
93 AD7298_V_CHAN(2),
94 AD7298_V_CHAN(3),
95 AD7298_V_CHAN(4),
96 AD7298_V_CHAN(5),
97 AD7298_V_CHAN(6),
98 AD7298_V_CHAN(7),
Michael Hennerich01a99e12011-05-18 14:41:55 +010099 IIO_CHAN_SOFT_TIMESTAMP(8),
100};
101
Lars-Peter Clausendc4871a2012-11-15 13:15:00 +0000102/**
103 * ad7298_update_scan_mode() setup the spi transfer buffer for the new scan mask
104 **/
105static int ad7298_update_scan_mode(struct iio_dev *indio_dev,
106 const unsigned long *active_scan_mask)
107{
108 struct ad7298_state *st = iio_priv(indio_dev);
109 int i, m;
110 unsigned short command;
111 int scan_count;
112
113 /* Now compute overall size */
114 scan_count = bitmap_weight(active_scan_mask, indio_dev->masklength);
115
116 command = AD7298_WRITE | st->ext_ref;
117
118 for (i = 0, m = AD7298_CH(0); i < AD7298_MAX_CHAN; i++, m >>= 1)
119 if (test_bit(i, active_scan_mask))
120 command |= m;
121
122 st->tx_buf[0] = cpu_to_be16(command);
123
124 /* build spi ring message */
125 st->ring_xfer[0].tx_buf = &st->tx_buf[0];
126 st->ring_xfer[0].len = 2;
127 st->ring_xfer[0].cs_change = 1;
128 st->ring_xfer[1].tx_buf = &st->tx_buf[1];
129 st->ring_xfer[1].len = 2;
130 st->ring_xfer[1].cs_change = 1;
131
132 spi_message_init(&st->ring_msg);
133 spi_message_add_tail(&st->ring_xfer[0], &st->ring_msg);
134 spi_message_add_tail(&st->ring_xfer[1], &st->ring_msg);
135
136 for (i = 0; i < scan_count; i++) {
137 st->ring_xfer[i + 2].rx_buf = &st->rx_buf[i];
138 st->ring_xfer[i + 2].len = 2;
139 st->ring_xfer[i + 2].cs_change = 1;
140 spi_message_add_tail(&st->ring_xfer[i + 2], &st->ring_msg);
141 }
142 /* make sure last transfer cs_change is not set */
143 st->ring_xfer[i + 1].cs_change = 0;
144
145 return 0;
146}
147
148/**
149 * ad7298_trigger_handler() bh of trigger launched polling to ring buffer
150 *
151 * Currently there is no option in this driver to disable the saving of
152 * timestamps within the ring.
153 **/
154static irqreturn_t ad7298_trigger_handler(int irq, void *p)
155{
156 struct iio_poll_func *pf = p;
157 struct iio_dev *indio_dev = pf->indio_dev;
158 struct ad7298_state *st = iio_priv(indio_dev);
Lars-Peter Clausendc4871a2012-11-15 13:15:00 +0000159 int b_sent;
160
161 b_sent = spi_sync(st->spi, &st->ring_msg);
162 if (b_sent)
163 goto done;
164
Lars-Peter Clausen85ec2372013-09-19 13:59:00 +0100165 iio_push_to_buffers_with_timestamp(indio_dev, st->rx_buf,
166 iio_get_time_ns());
Lars-Peter Clausendc4871a2012-11-15 13:15:00 +0000167
168done:
169 iio_trigger_notify_done(indio_dev->trig);
170
171 return IRQ_HANDLED;
172}
173
Michael Hennerich7c31b982011-02-24 12:32:45 +0100174static int ad7298_scan_direct(struct ad7298_state *st, unsigned ch)
175{
176 int ret;
177 st->tx_buf[0] = cpu_to_be16(AD7298_WRITE | st->ext_ref |
178 (AD7298_CH(0) >> ch));
179
180 ret = spi_sync(st->spi, &st->scan_single_msg);
181 if (ret)
182 return ret;
183
184 return be16_to_cpu(st->rx_buf[0]);
185}
186
Michael Hennerich01a99e12011-05-18 14:41:55 +0100187static int ad7298_scan_temp(struct ad7298_state *st, int *val)
Michael Hennerich7c31b982011-02-24 12:32:45 +0100188{
Lars-Peter Clausen01a10e02012-11-15 13:15:00 +0000189 int ret;
Dan Carpenterc1a75282011-11-22 10:39:15 +0300190 __be16 buf;
Michael Hennerich7c31b982011-02-24 12:32:45 +0100191
Dan Carpenterc1a75282011-11-22 10:39:15 +0300192 buf = cpu_to_be16(AD7298_WRITE | AD7298_TSENSE |
Michael Hennerich7c31b982011-02-24 12:32:45 +0100193 AD7298_TAVG | st->ext_ref);
194
Dan Carpenterc1a75282011-11-22 10:39:15 +0300195 ret = spi_write(st->spi, (u8 *)&buf, 2);
Michael Hennerich01a99e12011-05-18 14:41:55 +0100196 if (ret)
197 return ret;
198
Dan Carpenterc1a75282011-11-22 10:39:15 +0300199 buf = cpu_to_be16(0);
Michael Hennerich01a99e12011-05-18 14:41:55 +0100200
Dan Carpenterc1a75282011-11-22 10:39:15 +0300201 ret = spi_write(st->spi, (u8 *)&buf, 2);
Michael Hennerich01a99e12011-05-18 14:41:55 +0100202 if (ret)
203 return ret;
204
Michael Hennerich7c31b982011-02-24 12:32:45 +0100205 usleep_range(101, 1000); /* sleep > 100us */
Michael Hennerich01a99e12011-05-18 14:41:55 +0100206
Dan Carpenterc1a75282011-11-22 10:39:15 +0300207 ret = spi_read(st->spi, (u8 *)&buf, 2);
Michael Hennerich01a99e12011-05-18 14:41:55 +0100208 if (ret)
209 return ret;
Michael Hennerich7c31b982011-02-24 12:32:45 +0100210
Lars-Peter Clausen01a10e02012-11-15 13:15:00 +0000211 *val = sign_extend32(be16_to_cpu(buf), 11);
Michael Hennerich01a99e12011-05-18 14:41:55 +0100212
213 return 0;
Michael Hennerich7c31b982011-02-24 12:32:45 +0100214}
215
Lars-Peter Clausen01a10e02012-11-15 13:15:00 +0000216static int ad7298_get_ref_voltage(struct ad7298_state *st)
217{
218 int vref;
219
220 if (st->ext_ref) {
221 vref = regulator_get_voltage(st->reg);
222 if (vref < 0)
223 return vref;
224
225 return vref / 1000;
226 } else {
227 return AD7298_INTREF_mV;
228 }
229}
230
Jonathan Cameron84f79ec2011-10-06 17:14:37 +0100231static int ad7298_read_raw(struct iio_dev *indio_dev,
Michael Hennerich01a99e12011-05-18 14:41:55 +0100232 struct iio_chan_spec const *chan,
233 int *val,
234 int *val2,
235 long m)
Michael Hennerich7c31b982011-02-24 12:32:45 +0100236{
Michael Hennerich01a99e12011-05-18 14:41:55 +0100237 int ret;
Jonathan Cameron84f79ec2011-10-06 17:14:37 +0100238 struct ad7298_state *st = iio_priv(indio_dev);
Michael Hennerich7c31b982011-02-24 12:32:45 +0100239
Michael Hennerich01a99e12011-05-18 14:41:55 +0100240 switch (m) {
Jonathan Cameronb11f98f2012-04-15 17:41:18 +0100241 case IIO_CHAN_INFO_RAW:
Jonathan Cameron84f79ec2011-10-06 17:14:37 +0100242 mutex_lock(&indio_dev->mlock);
Jonathan Cameron389ac482011-12-05 22:18:19 +0000243 if (indio_dev->currentmode == INDIO_BUFFER_TRIGGERED) {
244 ret = -EBUSY;
Michael Hennerich01a99e12011-05-18 14:41:55 +0100245 } else {
246 if (chan->address == AD7298_CH_TEMP)
247 ret = ad7298_scan_temp(st, val);
248 else
249 ret = ad7298_scan_direct(st, chan->address);
250 }
Jonathan Cameron84f79ec2011-10-06 17:14:37 +0100251 mutex_unlock(&indio_dev->mlock);
Michael Hennerich01a99e12011-05-18 14:41:55 +0100252
253 if (ret < 0)
254 return ret;
255
256 if (chan->address != AD7298_CH_TEMP)
Peter Meerwald48898052014-12-06 06:00:00 +0000257 *val = ret & GENMASK(chan->scan_type.realbits - 1, 0);
Michael Hennerich01a99e12011-05-18 14:41:55 +0100258
259 return IIO_VAL_INT;
Jonathan Cameronc8a9f802011-10-26 17:41:36 +0100260 case IIO_CHAN_INFO_SCALE:
261 switch (chan->type) {
262 case IIO_VOLTAGE:
Lars-Peter Clausen01a10e02012-11-15 13:15:00 +0000263 *val = ad7298_get_ref_voltage(st);
Lars-Peter Clausen2e334602012-11-15 13:15:00 +0000264 *val2 = chan->scan_type.realbits;
265 return IIO_VAL_FRACTIONAL_LOG2;
Jonathan Cameronc8a9f802011-10-26 17:41:36 +0100266 case IIO_TEMP:
Lars-Peter Clausen01a10e02012-11-15 13:15:00 +0000267 *val = ad7298_get_ref_voltage(st);
268 *val2 = 10;
269 return IIO_VAL_FRACTIONAL;
Jonathan Cameronc8a9f802011-10-26 17:41:36 +0100270 default:
271 return -EINVAL;
272 }
Lars-Peter Clausen01a10e02012-11-15 13:15:00 +0000273 case IIO_CHAN_INFO_OFFSET:
274 *val = 1093 - 2732500 / ad7298_get_ref_voltage(st);
275 return IIO_VAL_INT;
Michael Hennerich01a99e12011-05-18 14:41:55 +0100276 }
277 return -EINVAL;
Michael Hennerich7c31b982011-02-24 12:32:45 +0100278}
Michael Hennerich7c31b982011-02-24 12:32:45 +0100279
Jonathan Cameron6fe81352011-05-18 14:42:37 +0100280static const struct iio_info ad7298_info = {
281 .read_raw = &ad7298_read_raw,
Lars-Peter Clausend045b9d2012-06-18 18:33:51 +0200282 .update_scan_mode = ad7298_update_scan_mode,
Jonathan Cameron6fe81352011-05-18 14:42:37 +0100283 .driver_module = THIS_MODULE,
284};
285
Greg Kroah-Hartmanfc526922012-12-21 13:21:43 -0800286static int ad7298_probe(struct spi_device *spi)
Michael Hennerich7c31b982011-02-24 12:32:45 +0100287{
288 struct ad7298_platform_data *pdata = spi->dev.platform_data;
289 struct ad7298_state *st;
Sachin Kamat7abe9002013-07-23 09:58:00 +0100290 struct iio_dev *indio_dev;
Lars-Peter Clausen2e334602012-11-15 13:15:00 +0000291 int ret;
Michael Hennerich7c31b982011-02-24 12:32:45 +0100292
Sachin Kamat7abe9002013-07-23 09:58:00 +0100293 indio_dev = devm_iio_device_alloc(&spi->dev, sizeof(*st));
Michael Hennerichcc4a48e2011-05-18 14:42:25 +0100294 if (indio_dev == NULL)
295 return -ENOMEM;
296
297 st = iio_priv(indio_dev);
Michael Hennerich7c31b982011-02-24 12:32:45 +0100298
Lars-Peter Clausen2e334602012-11-15 13:15:00 +0000299 if (pdata && pdata->ext_ref)
300 st->ext_ref = AD7298_EXTREF;
301
302 if (st->ext_ref) {
Sachin Kamat7abe9002013-07-23 09:58:00 +0100303 st->reg = devm_regulator_get(&spi->dev, "vref");
304 if (IS_ERR(st->reg))
305 return PTR_ERR(st->reg);
306
Michael Hennerich7c31b982011-02-24 12:32:45 +0100307 ret = regulator_enable(st->reg);
308 if (ret)
Sachin Kamat7abe9002013-07-23 09:58:00 +0100309 return ret;
Michael Hennerich7c31b982011-02-24 12:32:45 +0100310 }
311
Michael Hennerichcc4a48e2011-05-18 14:42:25 +0100312 spi_set_drvdata(spi, indio_dev);
Michael Hennerich7c31b982011-02-24 12:32:45 +0100313
Michael Hennerich7c31b982011-02-24 12:32:45 +0100314 st->spi = spi;
315
Michael Hennerichcc4a48e2011-05-18 14:42:25 +0100316 indio_dev->name = spi_get_device_id(spi)->name;
317 indio_dev->dev.parent = &spi->dev;
Michael Hennerichcc4a48e2011-05-18 14:42:25 +0100318 indio_dev->modes = INDIO_DIRECT_MODE;
319 indio_dev->channels = ad7298_channels;
320 indio_dev->num_channels = ARRAY_SIZE(ad7298_channels);
Jonathan Cameron6fe81352011-05-18 14:42:37 +0100321 indio_dev->info = &ad7298_info;
Michael Hennerich7c31b982011-02-24 12:32:45 +0100322
323 /* Setup default message */
324
325 st->scan_single_xfer[0].tx_buf = &st->tx_buf[0];
326 st->scan_single_xfer[0].len = 2;
327 st->scan_single_xfer[0].cs_change = 1;
328 st->scan_single_xfer[1].tx_buf = &st->tx_buf[1];
329 st->scan_single_xfer[1].len = 2;
330 st->scan_single_xfer[1].cs_change = 1;
331 st->scan_single_xfer[2].rx_buf = &st->rx_buf[0];
332 st->scan_single_xfer[2].len = 2;
333
334 spi_message_init(&st->scan_single_msg);
335 spi_message_add_tail(&st->scan_single_xfer[0], &st->scan_single_msg);
336 spi_message_add_tail(&st->scan_single_xfer[1], &st->scan_single_msg);
337 spi_message_add_tail(&st->scan_single_xfer[2], &st->scan_single_msg);
338
Lars-Peter Clausendc4871a2012-11-15 13:15:00 +0000339 ret = iio_triggered_buffer_setup(indio_dev, NULL,
340 &ad7298_trigger_handler, NULL);
Michael Hennerich7c31b982011-02-24 12:32:45 +0100341 if (ret)
Michael Hennerichcc4a48e2011-05-18 14:42:25 +0100342 goto error_disable_reg;
Michael Hennerich7c31b982011-02-24 12:32:45 +0100343
Jonathan Cameron26d25ae2011-09-02 17:14:40 +0100344 ret = iio_device_register(indio_dev);
345 if (ret)
Lars-Peter Clausend045b9d2012-06-18 18:33:51 +0200346 goto error_cleanup_ring;
Michael Hennerichcc4a48e2011-05-18 14:42:25 +0100347
Michael Hennerich7c31b982011-02-24 12:32:45 +0100348 return 0;
349
350error_cleanup_ring:
Lars-Peter Clausendc4871a2012-11-15 13:15:00 +0000351 iio_triggered_buffer_cleanup(indio_dev);
Michael Hennerich7c31b982011-02-24 12:32:45 +0100352error_disable_reg:
Lars-Peter Clausen2e334602012-11-15 13:15:00 +0000353 if (st->ext_ref)
Michael Hennerich7c31b982011-02-24 12:32:45 +0100354 regulator_disable(st->reg);
Michael Hennerichcc4a48e2011-05-18 14:42:25 +0100355
Michael Hennerich7c31b982011-02-24 12:32:45 +0100356 return ret;
357}
358
Greg Kroah-Hartmanfc526922012-12-21 13:21:43 -0800359static int ad7298_remove(struct spi_device *spi)
Michael Hennerich7c31b982011-02-24 12:32:45 +0100360{
Michael Hennerichcc4a48e2011-05-18 14:42:25 +0100361 struct iio_dev *indio_dev = spi_get_drvdata(spi);
362 struct ad7298_state *st = iio_priv(indio_dev);
Michael Hennerich7c31b982011-02-24 12:32:45 +0100363
Jonathan Camerond2fffd62011-10-14 14:46:58 +0100364 iio_device_unregister(indio_dev);
Lars-Peter Clausendc4871a2012-11-15 13:15:00 +0000365 iio_triggered_buffer_cleanup(indio_dev);
Sachin Kamat7abe9002013-07-23 09:58:00 +0100366 if (st->ext_ref)
Michael Hennerich7c31b982011-02-24 12:32:45 +0100367 regulator_disable(st->reg);
Michael Hennerichcc4a48e2011-05-18 14:42:25 +0100368
Michael Hennerich7c31b982011-02-24 12:32:45 +0100369 return 0;
370}
371
372static const struct spi_device_id ad7298_id[] = {
373 {"ad7298", 0},
374 {}
375};
Lars-Peter Clausen55e43902011-11-16 08:53:31 +0100376MODULE_DEVICE_TABLE(spi, ad7298_id);
Michael Hennerich7c31b982011-02-24 12:32:45 +0100377
378static struct spi_driver ad7298_driver = {
379 .driver = {
380 .name = "ad7298",
Michael Hennerich7c31b982011-02-24 12:32:45 +0100381 .owner = THIS_MODULE,
382 },
383 .probe = ad7298_probe,
Greg Kroah-Hartmanfc526922012-12-21 13:21:43 -0800384 .remove = ad7298_remove,
Michael Hennerich7c31b982011-02-24 12:32:45 +0100385 .id_table = ad7298_id,
386};
Lars-Peter Clausenae6ae6f2011-11-16 10:13:39 +0100387module_spi_driver(ad7298_driver);
Michael Hennerich7c31b982011-02-24 12:32:45 +0100388
389MODULE_AUTHOR("Michael Hennerich <hennerich@blackfin.uclinux.org>");
390MODULE_DESCRIPTION("Analog Devices AD7298 ADC");
391MODULE_LICENSE("GPL v2");