blob: faedd0e165f6e85dbf495b4a32b232d3f26ab907 [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>
Michael Hennerich2b4756a2010-11-22 14:35:32 +010018
Jonathan Cameron06458e22012-04-25 15:54:58 +010019#include <linux/iio/iio.h>
20#include <linux/iio/sysfs.h>
21#include <linux/iio/buffer.h>
Jonathan Cameroncdf38702011-08-12 17:08:43 +010022
Lars-Peter Clausen65dd3d32012-11-05 09:56:00 +000023#include <linux/iio/trigger_consumer.h>
24#include <linux/iio/triggered_buffer.h>
Michael Hennerich2b4756a2010-11-22 14:35:32 +010025
Lars-Peter Clausen4eb3ccf2012-11-05 09:56:00 +000026#include <linux/platform_data/ad7887.h>
Michael Hennerich2b4756a2010-11-22 14:35:32 +010027
Lars-Peter Clausen65dd3d32012-11-05 09:56:00 +000028#define AD7887_REF_DIS (1 << 5) /* on-chip reference disable */
29#define AD7887_DUAL (1 << 4) /* dual-channel mode */
30#define AD7887_CH_AIN1 (1 << 3) /* convert on channel 1, DUAL=1 */
31#define AD7887_CH_AIN0 (0 << 3) /* convert on channel 0, DUAL=0,1 */
32#define AD7887_PM_MODE1 (0) /* CS based shutdown */
33#define AD7887_PM_MODE2 (1) /* full on */
34#define AD7887_PM_MODE3 (2) /* auto shutdown after conversion */
35#define AD7887_PM_MODE4 (3) /* standby mode */
36
37enum ad7887_channels {
38 AD7887_CH0,
39 AD7887_CH0_CH1,
40 AD7887_CH1,
41};
42
43#define RES_MASK(bits) ((1 << (bits)) - 1)
44
45/**
46 * struct ad7887_chip_info - chip specifc information
47 * @int_vref_mv: the internal reference voltage
48 * @channel: channel specification
49 */
50struct ad7887_chip_info {
51 u16 int_vref_mv;
52 struct iio_chan_spec channel[3];
53};
54
55struct ad7887_state {
56 struct spi_device *spi;
57 const struct ad7887_chip_info *chip_info;
58 struct regulator *reg;
59 struct spi_transfer xfer[4];
60 struct spi_message msg[3];
61 struct spi_message *ring_msg;
Lars-Peter Clausenfce7c3e2012-11-05 09:56:00 +000062 unsigned char tx_cmd_buf[4];
Lars-Peter Clausen65dd3d32012-11-05 09:56:00 +000063
64 /*
65 * DMA (thus cache coherency maintenance) requires the
66 * transfer buffers to live in their own cache lines.
67 * Buffer needs to be large enough to hold two 16 bit samples and a
68 * 64 bit aligned 64 bit timestamp.
69 */
70 unsigned char data[ALIGN(4, sizeof(s64)) + sizeof(s64)]
71 ____cacheline_aligned;
72};
73
74enum ad7887_supported_device_ids {
75 ID_AD7887
76};
77
78static int ad7887_ring_preenable(struct iio_dev *indio_dev)
79{
80 struct ad7887_state *st = iio_priv(indio_dev);
81 int ret;
82
83 ret = iio_sw_buffer_preenable(indio_dev);
84 if (ret < 0)
85 return ret;
86
87 /* We know this is a single long so can 'cheat' */
88 switch (*indio_dev->active_scan_mask) {
89 case (1 << 0):
90 st->ring_msg = &st->msg[AD7887_CH0];
91 break;
92 case (1 << 1):
93 st->ring_msg = &st->msg[AD7887_CH1];
94 /* Dummy read: push CH1 setting down to hardware */
95 spi_sync(st->spi, st->ring_msg);
96 break;
97 case ((1 << 1) | (1 << 0)):
98 st->ring_msg = &st->msg[AD7887_CH0_CH1];
99 break;
100 }
101
102 return 0;
103}
104
105static int ad7887_ring_postdisable(struct iio_dev *indio_dev)
106{
107 struct ad7887_state *st = iio_priv(indio_dev);
108
109 /* dummy read: restore default CH0 settin */
110 return spi_sync(st->spi, &st->msg[AD7887_CH0]);
111}
112
113/**
114 * ad7887_trigger_handler() bh of trigger launched polling to ring buffer
115 *
116 * Currently there is no option in this driver to disable the saving of
117 * timestamps within the ring.
118 **/
119static irqreturn_t ad7887_trigger_handler(int irq, void *p)
120{
121 struct iio_poll_func *pf = p;
122 struct iio_dev *indio_dev = pf->indio_dev;
123 struct ad7887_state *st = iio_priv(indio_dev);
Lars-Peter Clausen65dd3d32012-11-05 09:56:00 +0000124 int b_sent;
125
126 b_sent = spi_sync(st->spi, st->ring_msg);
127 if (b_sent)
128 goto done;
129
Lars-Peter Clausen5afd6022013-09-19 13:59:00 +0100130 iio_push_to_buffers_with_timestamp(indio_dev, st->data,
131 iio_get_time_ns());
Lars-Peter Clausen65dd3d32012-11-05 09:56:00 +0000132done:
133 iio_trigger_notify_done(indio_dev->trig);
134
135 return IRQ_HANDLED;
136}
137
138static const struct iio_buffer_setup_ops ad7887_ring_setup_ops = {
139 .preenable = &ad7887_ring_preenable,
140 .postenable = &iio_triggered_buffer_postenable,
141 .predisable = &iio_triggered_buffer_predisable,
142 .postdisable = &ad7887_ring_postdisable,
143};
144
Michael Hennerich2b4756a2010-11-22 14:35:32 +0100145static int ad7887_scan_direct(struct ad7887_state *st, unsigned ch)
146{
147 int ret = spi_sync(st->spi, &st->msg[ch]);
148 if (ret)
149 return ret;
150
151 return (st->data[(ch * 2)] << 8) | st->data[(ch * 2) + 1];
152}
153
Jonathan Cameron84f79ec2011-10-06 17:14:37 +0100154static int ad7887_read_raw(struct iio_dev *indio_dev,
Michael Hennerich596d0602011-05-18 14:41:50 +0100155 struct iio_chan_spec const *chan,
156 int *val,
157 int *val2,
158 long m)
Michael Hennerich2b4756a2010-11-22 14:35:32 +0100159{
Michael Hennerich2b4756a2010-11-22 14:35:32 +0100160 int ret;
Jonathan Cameron84f79ec2011-10-06 17:14:37 +0100161 struct ad7887_state *st = iio_priv(indio_dev);
Michael Hennerich2b4756a2010-11-22 14:35:32 +0100162
Michael Hennerich596d0602011-05-18 14:41:50 +0100163 switch (m) {
Jonathan Cameronb11f98f2012-04-15 17:41:18 +0100164 case IIO_CHAN_INFO_RAW:
Jonathan Cameron84f79ec2011-10-06 17:14:37 +0100165 mutex_lock(&indio_dev->mlock);
166 if (iio_buffer_enabled(indio_dev))
Jonathan Cameron790d8752011-12-05 22:18:23 +0000167 ret = -EBUSY;
Michael Hennerich596d0602011-05-18 14:41:50 +0100168 else
169 ret = ad7887_scan_direct(st, chan->address);
Jonathan Cameron84f79ec2011-10-06 17:14:37 +0100170 mutex_unlock(&indio_dev->mlock);
Michael Hennerich2b4756a2010-11-22 14:35:32 +0100171
Michael Hennerich596d0602011-05-18 14:41:50 +0100172 if (ret < 0)
173 return ret;
Lars-Peter Clausen98efb702012-11-05 09:56:00 +0000174 *val = ret >> chan->scan_type.shift;
175 *val &= RES_MASK(chan->scan_type.realbits);
Michael Hennerich596d0602011-05-18 14:41:50 +0100176 return IIO_VAL_INT;
Jonathan Cameronc8a9f802011-10-26 17:41:36 +0100177 case IIO_CHAN_INFO_SCALE:
Lars-Peter Clausenbf5d2612012-11-05 09:56:00 +0000178 if (st->reg) {
179 *val = regulator_get_voltage(st->reg);
180 if (*val < 0)
181 return *val;
182 *val /= 1000;
183 } else {
184 *val = st->chip_info->int_vref_mv;
185 }
186
Lars-Peter Clausen98efb702012-11-05 09:56:00 +0000187 *val2 = chan->scan_type.realbits;
Lars-Peter Clausenbf5d2612012-11-05 09:56:00 +0000188
189 return IIO_VAL_FRACTIONAL_LOG2;
Michael Hennerich596d0602011-05-18 14:41:50 +0100190 }
191 return -EINVAL;
Michael Hennerich2b4756a2010-11-22 14:35:32 +0100192}
193
Michael Hennerich2b4756a2010-11-22 14:35:32 +0100194
195static const struct ad7887_chip_info ad7887_chip_info_tbl[] = {
196 /*
197 * More devices added in future
198 */
199 [ID_AD7887] = {
Jonathan Cameron31bf47d2011-09-30 10:05:44 +0100200 .channel[0] = {
201 .type = IIO_VOLTAGE,
202 .indexed = 1,
203 .channel = 1,
Jonathan Cameron16d186b2013-02-27 19:05:59 +0000204 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
205 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE),
Jonathan Cameron31bf47d2011-09-30 10:05:44 +0100206 .address = 1,
207 .scan_index = 1,
208 .scan_type = IIO_ST('u', 12, 16, 0),
209 },
210 .channel[1] = {
211 .type = IIO_VOLTAGE,
212 .indexed = 1,
213 .channel = 0,
Jonathan Cameron16d186b2013-02-27 19:05:59 +0000214 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
215 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE),
Jonathan Cameron31bf47d2011-09-30 10:05:44 +0100216 .address = 0,
217 .scan_index = 0,
218 .scan_type = IIO_ST('u', 12, 16, 0),
219 },
Michael Hennerich596d0602011-05-18 14:41:50 +0100220 .channel[2] = IIO_CHAN_SOFT_TIMESTAMP(2),
Michael Hennerich2b4756a2010-11-22 14:35:32 +0100221 .int_vref_mv = 2500,
222 },
223};
224
Jonathan Cameron6fe81352011-05-18 14:42:37 +0100225static const struct iio_info ad7887_info = {
226 .read_raw = &ad7887_read_raw,
227 .driver_module = THIS_MODULE,
228};
229
Greg Kroah-Hartmanfc526922012-12-21 13:21:43 -0800230static int ad7887_probe(struct spi_device *spi)
Michael Hennerich2b4756a2010-11-22 14:35:32 +0100231{
232 struct ad7887_platform_data *pdata = spi->dev.platform_data;
233 struct ad7887_state *st;
Sachin Kamat82429e02013-07-23 09:58:00 +0100234 struct iio_dev *indio_dev;
Lars-Peter Clausenfce7c3e2012-11-05 09:56:00 +0000235 uint8_t mode;
Lars-Peter Clausenbf5d2612012-11-05 09:56:00 +0000236 int ret;
Michael Hennerich2b4756a2010-11-22 14:35:32 +0100237
Sachin Kamat82429e02013-07-23 09:58:00 +0100238 indio_dev = devm_iio_device_alloc(&spi->dev, sizeof(*st));
Michael Hennerichf39e0862011-05-18 14:41:51 +0100239 if (indio_dev == NULL)
240 return -ENOMEM;
241
242 st = iio_priv(indio_dev);
Michael Hennerich2b4756a2010-11-22 14:35:32 +0100243
Lars-Peter Clausenbf5d2612012-11-05 09:56:00 +0000244 if (!pdata || !pdata->use_onchip_ref) {
Sachin Kamat82429e02013-07-23 09:58:00 +0100245 st->reg = devm_regulator_get(&spi->dev, "vref");
246 if (IS_ERR(st->reg))
247 return PTR_ERR(st->reg);
Lars-Peter Clausenbf5d2612012-11-05 09:56:00 +0000248
Michael Hennerich2b4756a2010-11-22 14:35:32 +0100249 ret = regulator_enable(st->reg);
250 if (ret)
Sachin Kamat82429e02013-07-23 09:58:00 +0100251 return ret;
Michael Hennerich2b4756a2010-11-22 14:35:32 +0100252 }
253
254 st->chip_info =
255 &ad7887_chip_info_tbl[spi_get_device_id(spi)->driver_data];
256
Michael Hennerichf39e0862011-05-18 14:41:51 +0100257 spi_set_drvdata(spi, indio_dev);
Michael Hennerich2b4756a2010-11-22 14:35:32 +0100258 st->spi = spi;
259
Michael Hennerich2b4756a2010-11-22 14:35:32 +0100260 /* Estabilish that the iio_dev is a child of the spi device */
Michael Hennerichf39e0862011-05-18 14:41:51 +0100261 indio_dev->dev.parent = &spi->dev;
262 indio_dev->name = spi_get_device_id(spi)->name;
Jonathan Cameron6fe81352011-05-18 14:42:37 +0100263 indio_dev->info = &ad7887_info;
Michael Hennerichf39e0862011-05-18 14:41:51 +0100264 indio_dev->modes = INDIO_DIRECT_MODE;
Michael Hennerich2b4756a2010-11-22 14:35:32 +0100265
266 /* Setup default message */
267
Lars-Peter Clausenfce7c3e2012-11-05 09:56:00 +0000268 mode = AD7887_PM_MODE4;
269 if (!pdata || !pdata->use_onchip_ref)
270 mode |= AD7887_REF_DIS;
271 if (pdata && pdata->en_dual)
272 mode |= AD7887_DUAL;
273
274 st->tx_cmd_buf[0] = AD7887_CH_AIN0 | mode;
Michael Hennerich2b4756a2010-11-22 14:35:32 +0100275
276 st->xfer[0].rx_buf = &st->data[0];
277 st->xfer[0].tx_buf = &st->tx_cmd_buf[0];
278 st->xfer[0].len = 2;
279
280 spi_message_init(&st->msg[AD7887_CH0]);
281 spi_message_add_tail(&st->xfer[0], &st->msg[AD7887_CH0]);
282
283 if (pdata && pdata->en_dual) {
Lars-Peter Clausenfce7c3e2012-11-05 09:56:00 +0000284 st->tx_cmd_buf[2] = AD7887_CH_AIN1 | mode;
Michael Hennerich2b4756a2010-11-22 14:35:32 +0100285
286 st->xfer[1].rx_buf = &st->data[0];
287 st->xfer[1].tx_buf = &st->tx_cmd_buf[2];
288 st->xfer[1].len = 2;
289
290 st->xfer[2].rx_buf = &st->data[2];
Lars-Peter Clausenfce7c3e2012-11-05 09:56:00 +0000291 st->xfer[2].tx_buf = &st->tx_cmd_buf[0];
Michael Hennerich2b4756a2010-11-22 14:35:32 +0100292 st->xfer[2].len = 2;
293
294 spi_message_init(&st->msg[AD7887_CH0_CH1]);
295 spi_message_add_tail(&st->xfer[1], &st->msg[AD7887_CH0_CH1]);
296 spi_message_add_tail(&st->xfer[2], &st->msg[AD7887_CH0_CH1]);
297
Lars-Peter Clausenfce7c3e2012-11-05 09:56:00 +0000298 st->xfer[3].rx_buf = &st->data[2];
299 st->xfer[3].tx_buf = &st->tx_cmd_buf[2];
Michael Hennerich2b4756a2010-11-22 14:35:32 +0100300 st->xfer[3].len = 2;
301
302 spi_message_init(&st->msg[AD7887_CH1]);
303 spi_message_add_tail(&st->xfer[3], &st->msg[AD7887_CH1]);
304
Michael Hennerichf39e0862011-05-18 14:41:51 +0100305 indio_dev->channels = st->chip_info->channel;
306 indio_dev->num_channels = 3;
Michael Hennerich2b4756a2010-11-22 14:35:32 +0100307 } else {
Michael Hennerichf39e0862011-05-18 14:41:51 +0100308 indio_dev->channels = &st->chip_info->channel[1];
309 indio_dev->num_channels = 2;
Michael Hennerich596d0602011-05-18 14:41:50 +0100310 }
Michael Hennerich2b4756a2010-11-22 14:35:32 +0100311
Lars-Peter Clausen65dd3d32012-11-05 09:56:00 +0000312 ret = iio_triggered_buffer_setup(indio_dev, &iio_pollfunc_store_time,
313 &ad7887_trigger_handler, &ad7887_ring_setup_ops);
Michael Hennerich2b4756a2010-11-22 14:35:32 +0100314 if (ret)
Michael Hennerichf39e0862011-05-18 14:41:51 +0100315 goto error_disable_reg;
Michael Hennerich2b4756a2010-11-22 14:35:32 +0100316
Jonathan Cameron26d25ae2011-09-02 17:14:40 +0100317 ret = iio_device_register(indio_dev);
318 if (ret)
319 goto error_unregister_ring;
320
321 return 0;
322error_unregister_ring:
Lars-Peter Clausen65dd3d32012-11-05 09:56:00 +0000323 iio_triggered_buffer_cleanup(indio_dev);
Michael Hennerich2b4756a2010-11-22 14:35:32 +0100324error_disable_reg:
Lars-Peter Clausenbf5d2612012-11-05 09:56:00 +0000325 if (st->reg)
Michael Hennerich2b4756a2010-11-22 14:35:32 +0100326 regulator_disable(st->reg);
Michael Hennerichf39e0862011-05-18 14:41:51 +0100327
Michael Hennerich2b4756a2010-11-22 14:35:32 +0100328 return ret;
329}
330
Greg Kroah-Hartmanfc526922012-12-21 13:21:43 -0800331static int ad7887_remove(struct spi_device *spi)
Michael Hennerich2b4756a2010-11-22 14:35:32 +0100332{
Michael Hennerichf39e0862011-05-18 14:41:51 +0100333 struct iio_dev *indio_dev = spi_get_drvdata(spi);
334 struct ad7887_state *st = iio_priv(indio_dev);
335
Jonathan Camerond2fffd62011-10-14 14:46:58 +0100336 iio_device_unregister(indio_dev);
Lars-Peter Clausen65dd3d32012-11-05 09:56:00 +0000337 iio_triggered_buffer_cleanup(indio_dev);
Sachin Kamat82429e02013-07-23 09:58:00 +0100338 if (st->reg)
Michael Hennerich2b4756a2010-11-22 14:35:32 +0100339 regulator_disable(st->reg);
Michael Hennerichf39e0862011-05-18 14:41:51 +0100340
Michael Hennerich2b4756a2010-11-22 14:35:32 +0100341 return 0;
342}
343
344static const struct spi_device_id ad7887_id[] = {
345 {"ad7887", ID_AD7887},
346 {}
347};
Lars-Peter Clausen55e43902011-11-16 08:53:31 +0100348MODULE_DEVICE_TABLE(spi, ad7887_id);
Michael Hennerich2b4756a2010-11-22 14:35:32 +0100349
350static struct spi_driver ad7887_driver = {
351 .driver = {
352 .name = "ad7887",
Michael Hennerich2b4756a2010-11-22 14:35:32 +0100353 .owner = THIS_MODULE,
354 },
355 .probe = ad7887_probe,
Greg Kroah-Hartmanfc526922012-12-21 13:21:43 -0800356 .remove = ad7887_remove,
Michael Hennerich2b4756a2010-11-22 14:35:32 +0100357 .id_table = ad7887_id,
358};
Lars-Peter Clausenae6ae6f2011-11-16 10:13:39 +0100359module_spi_driver(ad7887_driver);
Michael Hennerich2b4756a2010-11-22 14:35:32 +0100360
361MODULE_AUTHOR("Michael Hennerich <hennerich@blackfin.uclinux.org>");
362MODULE_DESCRIPTION("Analog Devices AD7887 ADC");
363MODULE_LICENSE("GPL v2");