2 * AD7887 SPI ADC driver
4 * Copyright 2010-2011 Analog Devices Inc.
6 * Licensed under the GPL-2.
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>
14 #include <linux/regulator/consumer.h>
15 #include <linux/err.h>
16 #include <linux/module.h>
17 #include <linux/interrupt.h>
19 #include <linux/iio/iio.h>
20 #include <linux/iio/sysfs.h>
21 #include <linux/iio/buffer.h>
23 #include <linux/iio/trigger_consumer.h>
24 #include <linux/iio/triggered_buffer.h>
26 #include <linux/platform_data/ad7887.h>
28 #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 */
37 enum ad7887_channels
{
43 #define RES_MASK(bits) ((1 << (bits)) - 1)
46 * struct ad7887_chip_info - chip specifc information
47 * @int_vref_mv: the internal reference voltage
48 * @channel: channel specification
50 struct ad7887_chip_info
{
52 struct iio_chan_spec channel
[3];
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
;
62 unsigned char tx_cmd_buf
[4];
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.
70 unsigned char data
[ALIGN(4, sizeof(s64
)) + sizeof(s64
)]
71 ____cacheline_aligned
;
74 enum ad7887_supported_device_ids
{
78 static int ad7887_ring_preenable(struct iio_dev
*indio_dev
)
80 struct ad7887_state
*st
= iio_priv(indio_dev
);
83 ret
= iio_sw_buffer_preenable(indio_dev
);
87 /* We know this is a single long so can 'cheat' */
88 switch (*indio_dev
->active_scan_mask
) {
90 st
->ring_msg
= &st
->msg
[AD7887_CH0
];
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
);
97 case ((1 << 1) | (1 << 0)):
98 st
->ring_msg
= &st
->msg
[AD7887_CH0_CH1
];
105 static int ad7887_ring_postdisable(struct iio_dev
*indio_dev
)
107 struct ad7887_state
*st
= iio_priv(indio_dev
);
109 /* dummy read: restore default CH0 settin */
110 return spi_sync(st
->spi
, &st
->msg
[AD7887_CH0
]);
114 * ad7887_trigger_handler() bh of trigger launched polling to ring buffer
116 * Currently there is no option in this driver to disable the saving of
117 * timestamps within the ring.
119 static irqreturn_t
ad7887_trigger_handler(int irq
, void *p
)
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
);
127 b_sent
= spi_sync(st
->spi
, st
->ring_msg
);
131 time_ns
= iio_get_time_ns();
133 if (indio_dev
->scan_timestamp
)
134 memcpy(st
->data
+ indio_dev
->scan_bytes
- sizeof(s64
),
135 &time_ns
, sizeof(time_ns
));
137 iio_push_to_buffers(indio_dev
, st
->data
);
139 iio_trigger_notify_done(indio_dev
->trig
);
144 static const struct iio_buffer_setup_ops ad7887_ring_setup_ops
= {
145 .preenable
= &ad7887_ring_preenable
,
146 .postenable
= &iio_triggered_buffer_postenable
,
147 .predisable
= &iio_triggered_buffer_predisable
,
148 .postdisable
= &ad7887_ring_postdisable
,
151 static int ad7887_scan_direct(struct ad7887_state
*st
, unsigned ch
)
153 int ret
= spi_sync(st
->spi
, &st
->msg
[ch
]);
157 return (st
->data
[(ch
* 2)] << 8) | st
->data
[(ch
* 2) + 1];
160 static int ad7887_read_raw(struct iio_dev
*indio_dev
,
161 struct iio_chan_spec
const *chan
,
167 struct ad7887_state
*st
= iio_priv(indio_dev
);
170 case IIO_CHAN_INFO_RAW
:
171 mutex_lock(&indio_dev
->mlock
);
172 if (iio_buffer_enabled(indio_dev
))
175 ret
= ad7887_scan_direct(st
, chan
->address
);
176 mutex_unlock(&indio_dev
->mlock
);
180 *val
= ret
>> chan
->scan_type
.shift
;
181 *val
&= RES_MASK(chan
->scan_type
.realbits
);
183 case IIO_CHAN_INFO_SCALE
:
185 *val
= regulator_get_voltage(st
->reg
);
190 *val
= st
->chip_info
->int_vref_mv
;
193 *val2
= chan
->scan_type
.realbits
;
195 return IIO_VAL_FRACTIONAL_LOG2
;
201 static const struct ad7887_chip_info ad7887_chip_info_tbl
[] = {
203 * More devices added in future
210 .info_mask_separate
= BIT(IIO_CHAN_INFO_RAW
),
211 .info_mask_shared_by_type
= BIT(IIO_CHAN_INFO_SCALE
),
219 .endianness
= IIO_BE
,
226 .info_mask_separate
= BIT(IIO_CHAN_INFO_RAW
),
227 .info_mask_shared_by_type
= BIT(IIO_CHAN_INFO_SCALE
),
235 .endianness
= IIO_BE
,
238 .channel
[2] = IIO_CHAN_SOFT_TIMESTAMP(2),
243 static const struct iio_info ad7887_info
= {
244 .read_raw
= &ad7887_read_raw
,
245 .driver_module
= THIS_MODULE
,
248 static int ad7887_probe(struct spi_device
*spi
)
250 struct ad7887_platform_data
*pdata
= spi
->dev
.platform_data
;
251 struct ad7887_state
*st
;
252 struct iio_dev
*indio_dev
;
256 indio_dev
= devm_iio_device_alloc(&spi
->dev
, sizeof(*st
));
257 if (indio_dev
== NULL
)
260 st
= iio_priv(indio_dev
);
262 if (!pdata
|| !pdata
->use_onchip_ref
) {
263 st
->reg
= devm_regulator_get(&spi
->dev
, "vref");
265 return PTR_ERR(st
->reg
);
267 ret
= regulator_enable(st
->reg
);
273 &ad7887_chip_info_tbl
[spi_get_device_id(spi
)->driver_data
];
275 spi_set_drvdata(spi
, indio_dev
);
278 /* Estabilish that the iio_dev is a child of the spi device */
279 indio_dev
->dev
.parent
= &spi
->dev
;
280 indio_dev
->name
= spi_get_device_id(spi
)->name
;
281 indio_dev
->info
= &ad7887_info
;
282 indio_dev
->modes
= INDIO_DIRECT_MODE
;
284 /* Setup default message */
286 mode
= AD7887_PM_MODE4
;
287 if (!pdata
|| !pdata
->use_onchip_ref
)
288 mode
|= AD7887_REF_DIS
;
289 if (pdata
&& pdata
->en_dual
)
292 st
->tx_cmd_buf
[0] = AD7887_CH_AIN0
| mode
;
294 st
->xfer
[0].rx_buf
= &st
->data
[0];
295 st
->xfer
[0].tx_buf
= &st
->tx_cmd_buf
[0];
298 spi_message_init(&st
->msg
[AD7887_CH0
]);
299 spi_message_add_tail(&st
->xfer
[0], &st
->msg
[AD7887_CH0
]);
301 if (pdata
&& pdata
->en_dual
) {
302 st
->tx_cmd_buf
[2] = AD7887_CH_AIN1
| mode
;
304 st
->xfer
[1].rx_buf
= &st
->data
[0];
305 st
->xfer
[1].tx_buf
= &st
->tx_cmd_buf
[2];
308 st
->xfer
[2].rx_buf
= &st
->data
[2];
309 st
->xfer
[2].tx_buf
= &st
->tx_cmd_buf
[0];
312 spi_message_init(&st
->msg
[AD7887_CH0_CH1
]);
313 spi_message_add_tail(&st
->xfer
[1], &st
->msg
[AD7887_CH0_CH1
]);
314 spi_message_add_tail(&st
->xfer
[2], &st
->msg
[AD7887_CH0_CH1
]);
316 st
->xfer
[3].rx_buf
= &st
->data
[2];
317 st
->xfer
[3].tx_buf
= &st
->tx_cmd_buf
[2];
320 spi_message_init(&st
->msg
[AD7887_CH1
]);
321 spi_message_add_tail(&st
->xfer
[3], &st
->msg
[AD7887_CH1
]);
323 indio_dev
->channels
= st
->chip_info
->channel
;
324 indio_dev
->num_channels
= 3;
326 indio_dev
->channels
= &st
->chip_info
->channel
[1];
327 indio_dev
->num_channels
= 2;
330 ret
= iio_triggered_buffer_setup(indio_dev
, &iio_pollfunc_store_time
,
331 &ad7887_trigger_handler
, &ad7887_ring_setup_ops
);
333 goto error_disable_reg
;
335 ret
= iio_device_register(indio_dev
);
337 goto error_unregister_ring
;
340 error_unregister_ring
:
341 iio_triggered_buffer_cleanup(indio_dev
);
344 regulator_disable(st
->reg
);
349 static int ad7887_remove(struct spi_device
*spi
)
351 struct iio_dev
*indio_dev
= spi_get_drvdata(spi
);
352 struct ad7887_state
*st
= iio_priv(indio_dev
);
354 iio_device_unregister(indio_dev
);
355 iio_triggered_buffer_cleanup(indio_dev
);
357 regulator_disable(st
->reg
);
362 static const struct spi_device_id ad7887_id
[] = {
363 {"ad7887", ID_AD7887
},
366 MODULE_DEVICE_TABLE(spi
, ad7887_id
);
368 static struct spi_driver ad7887_driver
= {
371 .owner
= THIS_MODULE
,
373 .probe
= ad7887_probe
,
374 .remove
= ad7887_remove
,
375 .id_table
= ad7887_id
,
377 module_spi_driver(ad7887_driver
);
379 MODULE_AUTHOR("Michael Hennerich <hennerich@blackfin.uclinux.org>");
380 MODULE_DESCRIPTION("Analog Devices AD7887 ADC");
381 MODULE_LICENSE("GPL v2");