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>
20 #include "../ring_generic.h"
25 static struct iio_chan_spec ad7298_channels[] = {
26 IIO_CHAN(IIO_TEMP, 0, 1, 0, NULL, 0, 0,
27 (1 << IIO_CHAN_INFO_SCALE_SEPARATE),
28 9, AD7298_CH_TEMP, IIO_ST('s', 32, 32, 0), 0),
29 IIO_CHAN(IIO_IN, 0, 1, 0, NULL, 0, 0,
30 (1 << IIO_CHAN_INFO_SCALE_SHARED),
31 0, 0, IIO_ST('u', 12, 16, 0), 0),
32 IIO_CHAN(IIO_IN, 0, 1, 0, NULL, 1, 0,
33 (1 << IIO_CHAN_INFO_SCALE_SHARED),
34 1, 1, IIO_ST('u', 12, 16, 0), 0),
35 IIO_CHAN(IIO_IN, 0, 1, 0, NULL, 2, 0,
36 (1 << IIO_CHAN_INFO_SCALE_SHARED),
37 2, 2, IIO_ST('u', 12, 16, 0), 0),
38 IIO_CHAN(IIO_IN, 0, 1, 0, NULL, 3, 0,
39 (1 << IIO_CHAN_INFO_SCALE_SHARED),
40 3, 3, IIO_ST('u', 12, 16, 0), 0),
41 IIO_CHAN(IIO_IN, 0, 1, 0, NULL, 4, 0,
42 (1 << IIO_CHAN_INFO_SCALE_SHARED),
43 4, 4, IIO_ST('u', 12, 16, 0), 0),
44 IIO_CHAN(IIO_IN, 0, 1, 0, NULL, 5, 0,
45 (1 << IIO_CHAN_INFO_SCALE_SHARED),
46 5, 5, IIO_ST('u', 12, 16, 0), 0),
47 IIO_CHAN(IIO_IN, 0, 1, 0, NULL, 6, 0,
48 (1 << IIO_CHAN_INFO_SCALE_SHARED),
49 6, 6, IIO_ST('u', 12, 16, 0), 0),
50 IIO_CHAN(IIO_IN, 0, 1, 0, NULL, 7, 0,
51 (1 << IIO_CHAN_INFO_SCALE_SHARED),
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)
73 tmp = cpu_to_be16(AD7298_WRITE | AD7298_TSENSE |
74 AD7298_TAVG | st->ext_ref);
76 ret = spi_write(st->spi, (u8 *)&tmp, 2);
82 ret = spi_write(st->spi, (u8 *)&tmp, 2);
86 usleep_range(101, 1000); /* sleep > 100us */
88 ret = spi_read(st->spi, (u8 *)&tmp, 2);
92 tmp = be16_to_cpu(tmp) & RES_MASK(AD7298_BITS);
95 * One LSB of the ADC corresponds to 0.25 deg C.
96 * The temperature reading is in 12-bit twos complement format
99 if (tmp & (1 << (AD7298_BITS - 1))) {
100 tmp = (4096 - tmp) * 250;
104 tmp *= 250; /* temperature in milli degrees Celsius */
112 static int ad7298_read_raw(struct iio_dev *dev_info,
113 struct iio_chan_spec const *chan,
119 struct ad7298_state *st = dev_info->dev_data;
120 unsigned int scale_uv;
124 mutex_lock(&dev_info->mlock);
125 if (iio_ring_enabled(dev_info)) {
126 if (chan->address == AD7298_CH_TEMP)
129 ret = ad7298_scan_from_ring(st, chan->address);
131 if (chan->address == AD7298_CH_TEMP)
132 ret = ad7298_scan_temp(st, val);
134 ret = ad7298_scan_direct(st, chan->address);
136 mutex_unlock(&dev_info->mlock);
141 if (chan->address != AD7298_CH_TEMP)
142 *val = ret & RES_MASK(AD7298_BITS);
145 case (1 << IIO_CHAN_INFO_SCALE_SHARED):
146 scale_uv = (st->int_vref_mv * 1000) >> AD7298_BITS;
147 *val = scale_uv / 1000;
148 *val2 = (scale_uv % 1000) * 1000;
149 return IIO_VAL_INT_PLUS_MICRO;
150 case (1 << IIO_CHAN_INFO_SCALE_SEPARATE):
153 return IIO_VAL_INT_PLUS_MICRO;
158 static int __devinit ad7298_probe(struct spi_device *spi)
160 struct ad7298_platform_data *pdata = spi->dev.platform_data;
161 struct ad7298_state *st;
164 st = kzalloc(sizeof(*st), GFP_KERNEL);
170 st->reg = regulator_get(&spi->dev, "vcc");
171 if (!IS_ERR(st->reg)) {
172 ret = regulator_enable(st->reg);
177 spi_set_drvdata(spi, st);
181 st->indio_dev = iio_allocate_device(0);
182 if (st->indio_dev == NULL) {
184 goto error_disable_reg;
187 st->indio_dev->name = spi_get_device_id(spi)->name;
188 st->indio_dev->dev.parent = &spi->dev;
189 st->indio_dev->dev_data = (void *)(st);
190 st->indio_dev->driver_module = THIS_MODULE;
191 st->indio_dev->modes = INDIO_DIRECT_MODE;
192 st->indio_dev->channels = ad7298_channels;
193 st->indio_dev->num_channels = ARRAY_SIZE(ad7298_channels);
194 st->indio_dev->read_raw = &ad7298_read_raw;
196 /* Setup default message */
198 st->scan_single_xfer[0].tx_buf = &st->tx_buf[0];
199 st->scan_single_xfer[0].len = 2;
200 st->scan_single_xfer[0].cs_change = 1;
201 st->scan_single_xfer[1].tx_buf = &st->tx_buf[1];
202 st->scan_single_xfer[1].len = 2;
203 st->scan_single_xfer[1].cs_change = 1;
204 st->scan_single_xfer[2].rx_buf = &st->rx_buf[0];
205 st->scan_single_xfer[2].len = 2;
207 spi_message_init(&st->scan_single_msg);
208 spi_message_add_tail(&st->scan_single_xfer[0], &st->scan_single_msg);
209 spi_message_add_tail(&st->scan_single_xfer[1], &st->scan_single_msg);
210 spi_message_add_tail(&st->scan_single_xfer[2], &st->scan_single_msg);
212 if (pdata && pdata->vref_mv) {
213 st->int_vref_mv = pdata->vref_mv;
214 st->ext_ref = AD7298_EXTREF;
216 st->int_vref_mv = AD7298_INTREF_mV;
219 ret = ad7298_register_ring_funcs_and_init(st->indio_dev);
221 goto error_free_device;
223 ret = iio_device_register(st->indio_dev);
225 goto error_free_device;
227 ret = iio_ring_buffer_register_ex(st->indio_dev->ring, 0,
228 &ad7298_channels[1], /* skip temp0 */
229 ARRAY_SIZE(ad7298_channels) - 1);
231 goto error_cleanup_ring;
235 ad7298_ring_cleanup(st->indio_dev);
236 iio_device_unregister(st->indio_dev);
238 iio_free_device(st->indio_dev);
240 if (!IS_ERR(st->reg))
241 regulator_disable(st->reg);
243 if (!IS_ERR(st->reg))
244 regulator_put(st->reg);
250 static int __devexit ad7298_remove(struct spi_device *spi)
252 struct ad7298_state *st = spi_get_drvdata(spi);
253 struct iio_dev *indio_dev = st->indio_dev;
255 iio_ring_buffer_unregister(indio_dev->ring);
256 ad7298_ring_cleanup(indio_dev);
257 iio_device_unregister(indio_dev);
258 if (!IS_ERR(st->reg)) {
259 regulator_disable(st->reg);
260 regulator_put(st->reg);
266 static const struct spi_device_id ad7298_id[] = {
271 static struct spi_driver ad7298_driver = {
274 .bus = &spi_bus_type,
275 .owner = THIS_MODULE,
277 .probe = ad7298_probe,
278 .remove = __devexit_p(ad7298_remove),
279 .id_table = ad7298_id,
282 static int __init ad7298_init(void)
284 return spi_register_driver(&ad7298_driver);
286 module_init(ad7298_init);
288 static void __exit ad7298_exit(void)
290 spi_unregister_driver(&ad7298_driver);
292 module_exit(ad7298_exit);
294 MODULE_AUTHOR("Michael Hennerich <hennerich@blackfin.uclinux.org>");
295 MODULE_DESCRIPTION("Analog Devices AD7298 ADC");
296 MODULE_LICENSE("GPL v2");
297 MODULE_ALIAS("spi:ad7298");