blob: 7a483bfbd70cc66947588a42f10d585b7eea519b [file] [log] [blame]
Michael Hennerich2b4756a2010-11-22 14:35:32 +01001/*
2 * AD7887 SPI ADC driver
3 *
Michael Hennerich596d0602011-05-18 14:41:50 +01004 * Copyright 2010-2011 Analog Devices Inc.
Michael Hennerich2b4756a2010-11-22 14:35:32 +01005 *
Michael Hennerich596d0602011-05-18 14:41:50 +01006 * Licensed under the GPL-2.
Michael Hennerich2b4756a2010-11-22 14:35:32 +01007 */
8
Michael Hennerich2b4756a2010-11-22 14:35:32 +01009#include <linux/device.h>
10#include <linux/kernel.h>
11#include <linux/slab.h>
12#include <linux/sysfs.h>
Michael Hennerich2b4756a2010-11-22 14:35:32 +010013#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>
Lars-Peter Clausen65dd3d32012-11-05 09:56:00 +000017#include <linux/interrupt.h>
Peter Meerwald09a17372014-12-06 06:00:00 +000018#include <linux/bitops.h>
Michael Hennerich2b4756a2010-11-22 14:35:32 +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>
Jonathan Cameroncdf38702011-08-12 17:08:43 +010023
Lars-Peter Clausen65dd3d32012-11-05 09:56:00 +000024#include <linux/iio/trigger_consumer.h>
25#include <linux/iio/triggered_buffer.h>
Michael Hennerich2b4756a2010-11-22 14:35:32 +010026
Lars-Peter Clausen4eb3ccf2012-11-05 09:56:00 +000027#include <linux/platform_data/ad7887.h>
Michael Hennerich2b4756a2010-11-22 14:35:32 +010028
Peter Meerwald09a17372014-12-06 06:00:00 +000029#define AD7887_REF_DIS BIT(5) /* on-chip reference disable */
30#define AD7887_DUAL BIT(4) /* dual-channel mode */
31#define AD7887_CH_AIN1 BIT(3) /* convert on channel 1, DUAL=1 */
32#define AD7887_CH_AIN0 0 /* convert on channel 0, DUAL=0,1 */
33#define AD7887_PM_MODE1 0 /* CS based shutdown */
34#define AD7887_PM_MODE2 1 /* full on */
35#define AD7887_PM_MODE3 2 /* auto shutdown after conversion */
36#define AD7887_PM_MODE4 3 /* standby mode */
Lars-Peter Clausen65dd3d32012-11-05 09:56:00 +000037
38enum ad7887_channels {
39 AD7887_CH0,
40 AD7887_CH0_CH1,
41 AD7887_CH1,
42};
43
Lars-Peter Clausen65dd3d32012-11-05 09:56:00 +000044/**
45 * struct ad7887_chip_info - chip specifc information
46 * @int_vref_mv: the internal reference voltage
47 * @channel: channel specification
48 */
49struct ad7887_chip_info {
50 u16 int_vref_mv;
51 struct iio_chan_spec channel[3];
52};
53
54struct ad7887_state {
55 struct spi_device *spi;
56 const struct ad7887_chip_info *chip_info;
57 struct regulator *reg;
58 struct spi_transfer xfer[4];
59 struct spi_message msg[3];
60 struct spi_message *ring_msg;
Lars-Peter Clausenfce7c3e2012-11-05 09:56:00 +000061 unsigned char tx_cmd_buf[4];
Lars-Peter Clausen65dd3d32012-11-05 09:56:00 +000062
63 /*
64 * DMA (thus cache coherency maintenance) requires the
65 * transfer buffers to live in their own cache lines.
66 * Buffer needs to be large enough to hold two 16 bit samples and a
67 * 64 bit aligned 64 bit timestamp.
68 */
69 unsigned char data[ALIGN(4, sizeof(s64)) + sizeof(s64)]
70 ____cacheline_aligned;
71};
72
73enum ad7887_supported_device_ids {
74 ID_AD7887
75};
76
77static int ad7887_ring_preenable(struct iio_dev *indio_dev)
78{
79 struct ad7887_state *st = iio_priv(indio_dev);
Lars-Peter Clausen65dd3d32012-11-05 09:56:00 +000080
81 /* We know this is a single long so can 'cheat' */
82 switch (*indio_dev->active_scan_mask) {
83 case (1 << 0):
84 st->ring_msg = &st->msg[AD7887_CH0];
85 break;
86 case (1 << 1):
87 st->ring_msg = &st->msg[AD7887_CH1];
88 /* Dummy read: push CH1 setting down to hardware */
89 spi_sync(st->spi, st->ring_msg);
90 break;
91 case ((1 << 1) | (1 << 0)):
92 st->ring_msg = &st->msg[AD7887_CH0_CH1];
93 break;
94 }
95
96 return 0;
97}
98
99static int ad7887_ring_postdisable(struct iio_dev *indio_dev)
100{
101 struct ad7887_state *st = iio_priv(indio_dev);
102
103 /* dummy read: restore default CH0 settin */
104 return spi_sync(st->spi, &st->msg[AD7887_CH0]);
105}
106
107/**
108 * ad7887_trigger_handler() bh of trigger launched polling to ring buffer
109 *
110 * Currently there is no option in this driver to disable the saving of
111 * timestamps within the ring.
112 **/
113static irqreturn_t ad7887_trigger_handler(int irq, void *p)
114{
115 struct iio_poll_func *pf = p;
116 struct iio_dev *indio_dev = pf->indio_dev;
117 struct ad7887_state *st = iio_priv(indio_dev);
Lars-Peter Clausen65dd3d32012-11-05 09:56:00 +0000118 int b_sent;
119
120 b_sent = spi_sync(st->spi, st->ring_msg);
121 if (b_sent)
122 goto done;
123
Lars-Peter Clausen5afd6022013-09-19 13:59:00 +0100124 iio_push_to_buffers_with_timestamp(indio_dev, st->data,
Gregor Boiriebc2b7da2016-03-09 19:05:49 +0100125 iio_get_time_ns(indio_dev));
Lars-Peter Clausen65dd3d32012-11-05 09:56:00 +0000126done:
127 iio_trigger_notify_done(indio_dev->trig);
128
129 return IRQ_HANDLED;
130}
131
132static const struct iio_buffer_setup_ops ad7887_ring_setup_ops = {
133 .preenable = &ad7887_ring_preenable,
134 .postenable = &iio_triggered_buffer_postenable,
135 .predisable = &iio_triggered_buffer_predisable,
136 .postdisable = &ad7887_ring_postdisable,
137};
138
Michael Hennerich2b4756a2010-11-22 14:35:32 +0100139static int ad7887_scan_direct(struct ad7887_state *st, unsigned ch)
140{
141 int ret = spi_sync(st->spi, &st->msg[ch]);
142 if (ret)
143 return ret;
144
145 return (st->data[(ch * 2)] << 8) | st->data[(ch * 2) + 1];
146}
147
Jonathan Cameron84f79ec2011-10-06 17:14:37 +0100148static int ad7887_read_raw(struct iio_dev *indio_dev,
Michael Hennerich596d0602011-05-18 14:41:50 +0100149 struct iio_chan_spec const *chan,
150 int *val,
151 int *val2,
152 long m)
Michael Hennerich2b4756a2010-11-22 14:35:32 +0100153{
Michael Hennerich2b4756a2010-11-22 14:35:32 +0100154 int ret;
Jonathan Cameron84f79ec2011-10-06 17:14:37 +0100155 struct ad7887_state *st = iio_priv(indio_dev);
Michael Hennerich2b4756a2010-11-22 14:35:32 +0100156
Michael Hennerich596d0602011-05-18 14:41:50 +0100157 switch (m) {
Jonathan Cameronb11f98f2012-04-15 17:41:18 +0100158 case IIO_CHAN_INFO_RAW:
Alison Schofield6fea8a42016-05-24 12:18:43 -0700159 ret = iio_device_claim_direct_mode(indio_dev);
160 if (ret)
161 return ret;
162 ret = ad7887_scan_direct(st, chan->address);
163 iio_device_release_direct_mode(indio_dev);
Michael Hennerich2b4756a2010-11-22 14:35:32 +0100164
Michael Hennerich596d0602011-05-18 14:41:50 +0100165 if (ret < 0)
166 return ret;
Lars-Peter Clausen98efb702012-11-05 09:56:00 +0000167 *val = ret >> chan->scan_type.shift;
Peter Meerwald09a17372014-12-06 06:00:00 +0000168 *val &= GENMASK(chan->scan_type.realbits - 1, 0);
Michael Hennerich596d0602011-05-18 14:41:50 +0100169 return IIO_VAL_INT;
Jonathan Cameronc8a9f802011-10-26 17:41:36 +0100170 case IIO_CHAN_INFO_SCALE:
Lars-Peter Clausenbf5d2612012-11-05 09:56:00 +0000171 if (st->reg) {
172 *val = regulator_get_voltage(st->reg);
173 if (*val < 0)
174 return *val;
175 *val /= 1000;
176 } else {
177 *val = st->chip_info->int_vref_mv;
178 }
179
Lars-Peter Clausen98efb702012-11-05 09:56:00 +0000180 *val2 = chan->scan_type.realbits;
Lars-Peter Clausenbf5d2612012-11-05 09:56:00 +0000181
182 return IIO_VAL_FRACTIONAL_LOG2;
Michael Hennerich596d0602011-05-18 14:41:50 +0100183 }
184 return -EINVAL;
Michael Hennerich2b4756a2010-11-22 14:35:32 +0100185}
186
Michael Hennerich2b4756a2010-11-22 14:35:32 +0100187
188static const struct ad7887_chip_info ad7887_chip_info_tbl[] = {
189 /*
190 * More devices added in future
191 */
192 [ID_AD7887] = {
Jonathan Cameron31bf47d2011-09-30 10:05:44 +0100193 .channel[0] = {
194 .type = IIO_VOLTAGE,
195 .indexed = 1,
196 .channel = 1,
Jonathan Cameron16d186b2013-02-27 19:05:59 +0000197 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
198 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE),
Jonathan Cameron31bf47d2011-09-30 10:05:44 +0100199 .address = 1,
200 .scan_index = 1,
Jonathan Camerone39d9902013-12-11 18:45:00 +0000201 .scan_type = {
202 .sign = 'u',
203 .realbits = 12,
204 .storagebits = 16,
205 .shift = 0,
206 .endianness = IIO_BE,
207 },
Jonathan Cameron31bf47d2011-09-30 10:05:44 +0100208 },
209 .channel[1] = {
210 .type = IIO_VOLTAGE,
211 .indexed = 1,
212 .channel = 0,
Jonathan Cameron16d186b2013-02-27 19:05:59 +0000213 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
214 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE),
Jonathan Cameron31bf47d2011-09-30 10:05:44 +0100215 .address = 0,
216 .scan_index = 0,
Jonathan Camerone39d9902013-12-11 18:45:00 +0000217 .scan_type = {
218 .sign = 'u',
219 .realbits = 12,
220 .storagebits = 16,
221 .shift = 0,
222 .endianness = IIO_BE,
223 },
Jonathan Cameron31bf47d2011-09-30 10:05:44 +0100224 },
Michael Hennerich596d0602011-05-18 14:41:50 +0100225 .channel[2] = IIO_CHAN_SOFT_TIMESTAMP(2),
Michael Hennerich2b4756a2010-11-22 14:35:32 +0100226 .int_vref_mv = 2500,
227 },
228};
229
Jonathan Cameron6fe81352011-05-18 14:42:37 +0100230static const struct iio_info ad7887_info = {
231 .read_raw = &ad7887_read_raw,
232 .driver_module = THIS_MODULE,
233};
234
Greg Kroah-Hartmanfc526922012-12-21 13:21:43 -0800235static int ad7887_probe(struct spi_device *spi)
Michael Hennerich2b4756a2010-11-22 14:35:32 +0100236{
237 struct ad7887_platform_data *pdata = spi->dev.platform_data;
238 struct ad7887_state *st;
Sachin Kamat82429e02013-07-23 09:58:00 +0100239 struct iio_dev *indio_dev;
Lars-Peter Clausenfce7c3e2012-11-05 09:56:00 +0000240 uint8_t mode;
Lars-Peter Clausenbf5d2612012-11-05 09:56:00 +0000241 int ret;
Michael Hennerich2b4756a2010-11-22 14:35:32 +0100242
Sachin Kamat82429e02013-07-23 09:58:00 +0100243 indio_dev = devm_iio_device_alloc(&spi->dev, sizeof(*st));
Michael Hennerichf39e0862011-05-18 14:41:51 +0100244 if (indio_dev == NULL)
245 return -ENOMEM;
246
247 st = iio_priv(indio_dev);
Michael Hennerich2b4756a2010-11-22 14:35:32 +0100248
Lars-Peter Clausenbf5d2612012-11-05 09:56:00 +0000249 if (!pdata || !pdata->use_onchip_ref) {
Sachin Kamat82429e02013-07-23 09:58:00 +0100250 st->reg = devm_regulator_get(&spi->dev, "vref");
251 if (IS_ERR(st->reg))
252 return PTR_ERR(st->reg);
Lars-Peter Clausenbf5d2612012-11-05 09:56:00 +0000253
Michael Hennerich2b4756a2010-11-22 14:35:32 +0100254 ret = regulator_enable(st->reg);
255 if (ret)
Sachin Kamat82429e02013-07-23 09:58:00 +0100256 return ret;
Michael Hennerich2b4756a2010-11-22 14:35:32 +0100257 }
258
259 st->chip_info =
260 &ad7887_chip_info_tbl[spi_get_device_id(spi)->driver_data];
261
Michael Hennerichf39e0862011-05-18 14:41:51 +0100262 spi_set_drvdata(spi, indio_dev);
Michael Hennerich2b4756a2010-11-22 14:35:32 +0100263 st->spi = spi;
264
Michael Hennerich2b4756a2010-11-22 14:35:32 +0100265 /* Estabilish that the iio_dev is a child of the spi device */
Michael Hennerichf39e0862011-05-18 14:41:51 +0100266 indio_dev->dev.parent = &spi->dev;
Matt Ranostayb541eaf2016-07-02 17:26:33 -0700267 indio_dev->dev.of_node = spi->dev.of_node;
Michael Hennerichf39e0862011-05-18 14:41:51 +0100268 indio_dev->name = spi_get_device_id(spi)->name;
Jonathan Cameron6fe81352011-05-18 14:42:37 +0100269 indio_dev->info = &ad7887_info;
Michael Hennerichf39e0862011-05-18 14:41:51 +0100270 indio_dev->modes = INDIO_DIRECT_MODE;
Michael Hennerich2b4756a2010-11-22 14:35:32 +0100271
272 /* Setup default message */
273
Lars-Peter Clausenfce7c3e2012-11-05 09:56:00 +0000274 mode = AD7887_PM_MODE4;
275 if (!pdata || !pdata->use_onchip_ref)
276 mode |= AD7887_REF_DIS;
277 if (pdata && pdata->en_dual)
278 mode |= AD7887_DUAL;
279
280 st->tx_cmd_buf[0] = AD7887_CH_AIN0 | mode;
Michael Hennerich2b4756a2010-11-22 14:35:32 +0100281
282 st->xfer[0].rx_buf = &st->data[0];
283 st->xfer[0].tx_buf = &st->tx_cmd_buf[0];
284 st->xfer[0].len = 2;
285
286 spi_message_init(&st->msg[AD7887_CH0]);
287 spi_message_add_tail(&st->xfer[0], &st->msg[AD7887_CH0]);
288
289 if (pdata && pdata->en_dual) {
Lars-Peter Clausenfce7c3e2012-11-05 09:56:00 +0000290 st->tx_cmd_buf[2] = AD7887_CH_AIN1 | mode;
Michael Hennerich2b4756a2010-11-22 14:35:32 +0100291
292 st->xfer[1].rx_buf = &st->data[0];
293 st->xfer[1].tx_buf = &st->tx_cmd_buf[2];
294 st->xfer[1].len = 2;
295
296 st->xfer[2].rx_buf = &st->data[2];
Lars-Peter Clausenfce7c3e2012-11-05 09:56:00 +0000297 st->xfer[2].tx_buf = &st->tx_cmd_buf[0];
Michael Hennerich2b4756a2010-11-22 14:35:32 +0100298 st->xfer[2].len = 2;
299
300 spi_message_init(&st->msg[AD7887_CH0_CH1]);
301 spi_message_add_tail(&st->xfer[1], &st->msg[AD7887_CH0_CH1]);
302 spi_message_add_tail(&st->xfer[2], &st->msg[AD7887_CH0_CH1]);
303
Lars-Peter Clausenfce7c3e2012-11-05 09:56:00 +0000304 st->xfer[3].rx_buf = &st->data[2];
305 st->xfer[3].tx_buf = &st->tx_cmd_buf[2];
Michael Hennerich2b4756a2010-11-22 14:35:32 +0100306 st->xfer[3].len = 2;
307
308 spi_message_init(&st->msg[AD7887_CH1]);
309 spi_message_add_tail(&st->xfer[3], &st->msg[AD7887_CH1]);
310
Michael Hennerichf39e0862011-05-18 14:41:51 +0100311 indio_dev->channels = st->chip_info->channel;
312 indio_dev->num_channels = 3;
Michael Hennerich2b4756a2010-11-22 14:35:32 +0100313 } else {
Michael Hennerichf39e0862011-05-18 14:41:51 +0100314 indio_dev->channels = &st->chip_info->channel[1];
315 indio_dev->num_channels = 2;
Michael Hennerich596d0602011-05-18 14:41:50 +0100316 }
Michael Hennerich2b4756a2010-11-22 14:35:32 +0100317
Lars-Peter Clausen65dd3d32012-11-05 09:56:00 +0000318 ret = iio_triggered_buffer_setup(indio_dev, &iio_pollfunc_store_time,
319 &ad7887_trigger_handler, &ad7887_ring_setup_ops);
Michael Hennerich2b4756a2010-11-22 14:35:32 +0100320 if (ret)
Michael Hennerichf39e0862011-05-18 14:41:51 +0100321 goto error_disable_reg;
Michael Hennerich2b4756a2010-11-22 14:35:32 +0100322
Jonathan Cameron26d25ae2011-09-02 17:14:40 +0100323 ret = iio_device_register(indio_dev);
324 if (ret)
325 goto error_unregister_ring;
326
327 return 0;
328error_unregister_ring:
Lars-Peter Clausen65dd3d32012-11-05 09:56:00 +0000329 iio_triggered_buffer_cleanup(indio_dev);
Michael Hennerich2b4756a2010-11-22 14:35:32 +0100330error_disable_reg:
Lars-Peter Clausenbf5d2612012-11-05 09:56:00 +0000331 if (st->reg)
Michael Hennerich2b4756a2010-11-22 14:35:32 +0100332 regulator_disable(st->reg);
Michael Hennerichf39e0862011-05-18 14:41:51 +0100333
Michael Hennerich2b4756a2010-11-22 14:35:32 +0100334 return ret;
335}
336
Greg Kroah-Hartmanfc526922012-12-21 13:21:43 -0800337static int ad7887_remove(struct spi_device *spi)
Michael Hennerich2b4756a2010-11-22 14:35:32 +0100338{
Michael Hennerichf39e0862011-05-18 14:41:51 +0100339 struct iio_dev *indio_dev = spi_get_drvdata(spi);
340 struct ad7887_state *st = iio_priv(indio_dev);
341
Jonathan Camerond2fffd62011-10-14 14:46:58 +0100342 iio_device_unregister(indio_dev);
Lars-Peter Clausen65dd3d32012-11-05 09:56:00 +0000343 iio_triggered_buffer_cleanup(indio_dev);
Sachin Kamat82429e02013-07-23 09:58:00 +0100344 if (st->reg)
Michael Hennerich2b4756a2010-11-22 14:35:32 +0100345 regulator_disable(st->reg);
Michael Hennerichf39e0862011-05-18 14:41:51 +0100346
Michael Hennerich2b4756a2010-11-22 14:35:32 +0100347 return 0;
348}
349
350static const struct spi_device_id ad7887_id[] = {
351 {"ad7887", ID_AD7887},
352 {}
353};
Lars-Peter Clausen55e43902011-11-16 08:53:31 +0100354MODULE_DEVICE_TABLE(spi, ad7887_id);
Michael Hennerich2b4756a2010-11-22 14:35:32 +0100355
356static struct spi_driver ad7887_driver = {
357 .driver = {
358 .name = "ad7887",
Michael Hennerich2b4756a2010-11-22 14:35:32 +0100359 },
360 .probe = ad7887_probe,
Greg Kroah-Hartmanfc526922012-12-21 13:21:43 -0800361 .remove = ad7887_remove,
Michael Hennerich2b4756a2010-11-22 14:35:32 +0100362 .id_table = ad7887_id,
363};
Lars-Peter Clausenae6ae6f2011-11-16 10:13:39 +0100364module_spi_driver(ad7887_driver);
Michael Hennerich2b4756a2010-11-22 14:35:32 +0100365
366MODULE_AUTHOR("Michael Hennerich <hennerich@blackfin.uclinux.org>");
367MODULE_DESCRIPTION("Analog Devices AD7887 ADC");
368MODULE_LICENSE("GPL v2");