2 * AD7298 SPI ADC driver
4 * Copyright 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/delay.h>
17 #include <linux/module.h>
21 #include "../buffer.h"
25 static struct iio_chan_spec ad7298_channels[] = {
26 IIO_CHAN(IIO_TEMP, 0, 1, 0, NULL, 0, 0,
27 IIO_CHAN_INFO_SCALE_SEPARATE_BIT,
28 9, AD7298_CH_TEMP, IIO_ST('s', 32, 32, 0), 0),
29 IIO_CHAN(IIO_VOLTAGE, 0, 1, 0, NULL, 0, 0,
30 IIO_CHAN_INFO_SCALE_SHARED_BIT,
31 0, 0, IIO_ST('u', 12, 16, 0), 0),
32 IIO_CHAN(IIO_VOLTAGE, 0, 1, 0, NULL, 1, 0,
33 IIO_CHAN_INFO_SCALE_SHARED_BIT,
34 1, 1, IIO_ST('u', 12, 16, 0), 0),
35 IIO_CHAN(IIO_VOLTAGE, 0, 1, 0, NULL, 2, 0,
36 IIO_CHAN_INFO_SCALE_SHARED_BIT,
37 2, 2, IIO_ST('u', 12, 16, 0), 0),
38 IIO_CHAN(IIO_VOLTAGE, 0, 1, 0, NULL, 3, 0,
39 IIO_CHAN_INFO_SCALE_SHARED_BIT,
40 3, 3, IIO_ST('u', 12, 16, 0), 0),
41 IIO_CHAN(IIO_VOLTAGE, 0, 1, 0, NULL, 4, 0,
42 IIO_CHAN_INFO_SCALE_SHARED_BIT,
43 4, 4, IIO_ST('u', 12, 16, 0), 0),
44 IIO_CHAN(IIO_VOLTAGE, 0, 1, 0, NULL, 5, 0,
45 IIO_CHAN_INFO_SCALE_SHARED_BIT,
46 5, 5, IIO_ST('u', 12, 16, 0), 0),
47 IIO_CHAN(IIO_VOLTAGE, 0, 1, 0, NULL, 6, 0,
48 IIO_CHAN_INFO_SCALE_SHARED_BIT,
49 6, 6, IIO_ST('u', 12, 16, 0), 0),
50 IIO_CHAN(IIO_VOLTAGE, 0, 1, 0, NULL, 7, 0,
51 IIO_CHAN_INFO_SCALE_SHARED_BIT,
52 7, 7, IIO_ST('u', 12, 16, 0), 0),
53 IIO_CHAN_SOFT_TIMESTAMP(8),
56 static int ad7298_scan_direct(struct ad7298_state *st, unsigned ch)
59 st->tx_buf[0] = cpu_to_be16(AD7298_WRITE | st->ext_ref |
60 (AD7298_CH(0) >> ch));
62 ret = spi_sync(st->spi, &st->scan_single_msg);
66 return be16_to_cpu(st->rx_buf[0]);
69 static int ad7298_scan_temp(struct ad7298_state *st, int *val)
74 buf = cpu_to_be16(AD7298_WRITE | AD7298_TSENSE |
75 AD7298_TAVG | st->ext_ref);
77 ret = spi_write(st->spi, (u8 *)&buf, 2);
83 ret = spi_write(st->spi, (u8 *)&buf, 2);
87 usleep_range(101, 1000); /* sleep > 100us */
89 ret = spi_read(st->spi, (u8 *)&buf, 2);
93 tmp = be16_to_cpu(buf) & RES_MASK(AD7298_BITS);
96 * One LSB of the ADC corresponds to 0.25 deg C.
97 * The temperature reading is in 12-bit twos complement format
100 if (tmp & (1 << (AD7298_BITS - 1))) {
101 tmp = (4096 - tmp) * 250;
105 tmp *= 250; /* temperature in milli degrees Celsius */
113 static int ad7298_read_raw(struct iio_dev *indio_dev,
114 struct iio_chan_spec const *chan,
120 struct ad7298_state *st = iio_priv(indio_dev);
121 unsigned int scale_uv;
125 mutex_lock(&indio_dev->mlock);
126 if (iio_buffer_enabled(indio_dev)) {
127 if (chan->address == AD7298_CH_TEMP)
130 ret = ad7298_scan_from_ring(indio_dev,
133 if (chan->address == AD7298_CH_TEMP)
134 ret = ad7298_scan_temp(st, val);
136 ret = ad7298_scan_direct(st, chan->address);
138 mutex_unlock(&indio_dev->mlock);
143 if (chan->address != AD7298_CH_TEMP)
144 *val = ret & RES_MASK(AD7298_BITS);
147 case IIO_CHAN_INFO_SCALE:
148 switch (chan->type) {
150 scale_uv = (st->int_vref_mv * 1000) >> AD7298_BITS;
151 *val = scale_uv / 1000;
152 *val2 = (scale_uv % 1000) * 1000;
153 return IIO_VAL_INT_PLUS_MICRO;
157 return IIO_VAL_INT_PLUS_MICRO;
165 static const struct iio_info ad7298_info = {
166 .read_raw = &ad7298_read_raw,
167 .driver_module = THIS_MODULE,
170 static int __devinit ad7298_probe(struct spi_device *spi)
172 struct ad7298_platform_data *pdata = spi->dev.platform_data;
173 struct ad7298_state *st;
175 struct iio_dev *indio_dev = iio_allocate_device(sizeof(*st));
177 if (indio_dev == NULL)
180 st = iio_priv(indio_dev);
182 st->reg = regulator_get(&spi->dev, "vcc");
183 if (!IS_ERR(st->reg)) {
184 ret = regulator_enable(st->reg);
189 spi_set_drvdata(spi, indio_dev);
193 indio_dev->name = spi_get_device_id(spi)->name;
194 indio_dev->dev.parent = &spi->dev;
195 indio_dev->modes = INDIO_DIRECT_MODE;
196 indio_dev->channels = ad7298_channels;
197 indio_dev->num_channels = ARRAY_SIZE(ad7298_channels);
198 indio_dev->info = &ad7298_info;
200 /* Setup default message */
202 st->scan_single_xfer[0].tx_buf = &st->tx_buf[0];
203 st->scan_single_xfer[0].len = 2;
204 st->scan_single_xfer[0].cs_change = 1;
205 st->scan_single_xfer[1].tx_buf = &st->tx_buf[1];
206 st->scan_single_xfer[1].len = 2;
207 st->scan_single_xfer[1].cs_change = 1;
208 st->scan_single_xfer[2].rx_buf = &st->rx_buf[0];
209 st->scan_single_xfer[2].len = 2;
211 spi_message_init(&st->scan_single_msg);
212 spi_message_add_tail(&st->scan_single_xfer[0], &st->scan_single_msg);
213 spi_message_add_tail(&st->scan_single_xfer[1], &st->scan_single_msg);
214 spi_message_add_tail(&st->scan_single_xfer[2], &st->scan_single_msg);
216 if (pdata && pdata->vref_mv) {
217 st->int_vref_mv = pdata->vref_mv;
218 st->ext_ref = AD7298_EXTREF;
220 st->int_vref_mv = AD7298_INTREF_mV;
223 ret = ad7298_register_ring_funcs_and_init(indio_dev);
225 goto error_disable_reg;
227 ret = iio_buffer_register(indio_dev,
228 &ad7298_channels[1], /* skip temp0 */
229 ARRAY_SIZE(ad7298_channels) - 1);
231 goto error_cleanup_ring;
232 ret = iio_device_register(indio_dev);
234 goto error_unregister_ring;
238 error_unregister_ring:
239 iio_buffer_unregister(indio_dev);
241 ad7298_ring_cleanup(indio_dev);
243 if (!IS_ERR(st->reg))
244 regulator_disable(st->reg);
246 if (!IS_ERR(st->reg))
247 regulator_put(st->reg);
248 iio_free_device(indio_dev);
253 static int __devexit ad7298_remove(struct spi_device *spi)
255 struct iio_dev *indio_dev = spi_get_drvdata(spi);
256 struct ad7298_state *st = iio_priv(indio_dev);
258 iio_device_unregister(indio_dev);
259 iio_buffer_unregister(indio_dev);
260 ad7298_ring_cleanup(indio_dev);
261 if (!IS_ERR(st->reg)) {
262 regulator_disable(st->reg);
263 regulator_put(st->reg);
265 iio_free_device(indio_dev);
270 static const struct spi_device_id ad7298_id[] = {
274 MODULE_DEVICE_TABLE(spi, ad7298_id);
276 static struct spi_driver ad7298_driver = {
279 .bus = &spi_bus_type,
280 .owner = THIS_MODULE,
282 .probe = ad7298_probe,
283 .remove = __devexit_p(ad7298_remove),
284 .id_table = ad7298_id,
287 static int __init ad7298_init(void)
289 return spi_register_driver(&ad7298_driver);
291 module_init(ad7298_init);
293 static void __exit ad7298_exit(void)
295 spi_unregister_driver(&ad7298_driver);
297 module_exit(ad7298_exit);
299 MODULE_AUTHOR("Michael Hennerich <hennerich@blackfin.uclinux.org>");
300 MODULE_DESCRIPTION("Analog Devices AD7298 ADC");
301 MODULE_LICENSE("GPL v2");