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>
20 #include "../buffer.h"
25 static int ad7887_scan_direct(struct ad7887_state *st, unsigned ch)
27 int ret = spi_sync(st->spi, &st->msg[ch]);
31 return (st->data[(ch * 2)] << 8) | st->data[(ch * 2) + 1];
34 static int ad7887_read_raw(struct iio_dev *indio_dev,
35 struct iio_chan_spec const *chan,
41 struct ad7887_state *st = iio_priv(indio_dev);
42 unsigned int scale_uv;
46 mutex_lock(&indio_dev->mlock);
47 if (iio_buffer_enabled(indio_dev))
48 ret = ad7887_scan_from_ring(st, 1 << chan->address);
50 ret = ad7887_scan_direct(st, chan->address);
51 mutex_unlock(&indio_dev->mlock);
55 *val = (ret >> st->chip_info->channel[0].scan_type.shift) &
56 RES_MASK(st->chip_info->channel[0].scan_type.realbits);
58 case IIO_CHAN_INFO_SCALE:
59 scale_uv = (st->int_vref_mv * 1000)
60 >> st->chip_info->channel[0].scan_type.realbits;
62 *val2 = (scale_uv%1000)*1000;
63 return IIO_VAL_INT_PLUS_MICRO;
69 static const struct ad7887_chip_info ad7887_chip_info_tbl[] = {
71 * More devices added in future
78 .info_mask = IIO_CHAN_INFO_SCALE_SHARED_BIT,
81 .scan_type = IIO_ST('u', 12, 16, 0),
87 .info_mask = IIO_CHAN_INFO_SCALE_SHARED_BIT,
90 .scan_type = IIO_ST('u', 12, 16, 0),
92 .channel[2] = IIO_CHAN_SOFT_TIMESTAMP(2),
97 static const struct iio_info ad7887_info = {
98 .read_raw = &ad7887_read_raw,
99 .driver_module = THIS_MODULE,
102 static int __devinit ad7887_probe(struct spi_device *spi)
104 struct ad7887_platform_data *pdata = spi->dev.platform_data;
105 struct ad7887_state *st;
106 int ret, voltage_uv = 0;
107 struct iio_dev *indio_dev = iio_allocate_device(sizeof(*st));
109 if (indio_dev == NULL)
112 st = iio_priv(indio_dev);
114 st->reg = regulator_get(&spi->dev, "vcc");
115 if (!IS_ERR(st->reg)) {
116 ret = regulator_enable(st->reg);
120 voltage_uv = regulator_get_voltage(st->reg);
124 &ad7887_chip_info_tbl[spi_get_device_id(spi)->driver_data];
126 spi_set_drvdata(spi, indio_dev);
129 /* Estabilish that the iio_dev is a child of the spi device */
130 indio_dev->dev.parent = &spi->dev;
131 indio_dev->name = spi_get_device_id(spi)->name;
132 indio_dev->info = &ad7887_info;
133 indio_dev->modes = INDIO_DIRECT_MODE;
135 /* Setup default message */
137 st->tx_cmd_buf[0] = AD7887_CH_AIN0 | AD7887_PM_MODE4 |
138 ((pdata && pdata->use_onchip_ref) ?
141 st->xfer[0].rx_buf = &st->data[0];
142 st->xfer[0].tx_buf = &st->tx_cmd_buf[0];
145 spi_message_init(&st->msg[AD7887_CH0]);
146 spi_message_add_tail(&st->xfer[0], &st->msg[AD7887_CH0]);
148 if (pdata && pdata->en_dual) {
149 st->tx_cmd_buf[0] |= AD7887_DUAL | AD7887_REF_DIS;
151 st->tx_cmd_buf[2] = AD7887_CH_AIN1 | AD7887_DUAL |
152 AD7887_REF_DIS | AD7887_PM_MODE4;
153 st->tx_cmd_buf[4] = AD7887_CH_AIN0 | AD7887_DUAL |
154 AD7887_REF_DIS | AD7887_PM_MODE4;
155 st->tx_cmd_buf[6] = AD7887_CH_AIN1 | AD7887_DUAL |
156 AD7887_REF_DIS | AD7887_PM_MODE4;
158 st->xfer[1].rx_buf = &st->data[0];
159 st->xfer[1].tx_buf = &st->tx_cmd_buf[2];
162 st->xfer[2].rx_buf = &st->data[2];
163 st->xfer[2].tx_buf = &st->tx_cmd_buf[4];
166 spi_message_init(&st->msg[AD7887_CH0_CH1]);
167 spi_message_add_tail(&st->xfer[1], &st->msg[AD7887_CH0_CH1]);
168 spi_message_add_tail(&st->xfer[2], &st->msg[AD7887_CH0_CH1]);
170 st->xfer[3].rx_buf = &st->data[0];
171 st->xfer[3].tx_buf = &st->tx_cmd_buf[6];
174 spi_message_init(&st->msg[AD7887_CH1]);
175 spi_message_add_tail(&st->xfer[3], &st->msg[AD7887_CH1]);
177 if (pdata && pdata->vref_mv)
178 st->int_vref_mv = pdata->vref_mv;
180 st->int_vref_mv = voltage_uv / 1000;
182 dev_warn(&spi->dev, "reference voltage unspecified\n");
184 indio_dev->channels = st->chip_info->channel;
185 indio_dev->num_channels = 3;
187 if (pdata && pdata->vref_mv)
188 st->int_vref_mv = pdata->vref_mv;
189 else if (pdata && pdata->use_onchip_ref)
190 st->int_vref_mv = st->chip_info->int_vref_mv;
192 dev_warn(&spi->dev, "reference voltage unspecified\n");
194 indio_dev->channels = &st->chip_info->channel[1];
195 indio_dev->num_channels = 2;
198 ret = ad7887_register_ring_funcs_and_init(indio_dev);
200 goto error_disable_reg;
202 ret = iio_buffer_register(indio_dev,
204 indio_dev->num_channels);
206 goto error_cleanup_ring;
208 ret = iio_device_register(indio_dev);
210 goto error_unregister_ring;
213 error_unregister_ring:
214 iio_buffer_unregister(indio_dev);
216 ad7887_ring_cleanup(indio_dev);
218 if (!IS_ERR(st->reg))
219 regulator_disable(st->reg);
221 if (!IS_ERR(st->reg))
222 regulator_put(st->reg);
223 iio_free_device(indio_dev);
228 static int ad7887_remove(struct spi_device *spi)
230 struct iio_dev *indio_dev = spi_get_drvdata(spi);
231 struct ad7887_state *st = iio_priv(indio_dev);
233 iio_device_unregister(indio_dev);
234 iio_buffer_unregister(indio_dev);
235 ad7887_ring_cleanup(indio_dev);
236 if (!IS_ERR(st->reg)) {
237 regulator_disable(st->reg);
238 regulator_put(st->reg);
240 iio_free_device(indio_dev);
245 static const struct spi_device_id ad7887_id[] = {
246 {"ad7887", ID_AD7887},
249 MODULE_DEVICE_TABLE(spi, ad7887_id);
251 static struct spi_driver ad7887_driver = {
254 .bus = &spi_bus_type,
255 .owner = THIS_MODULE,
257 .probe = ad7887_probe,
258 .remove = __devexit_p(ad7887_remove),
259 .id_table = ad7887_id,
262 static int __init ad7887_init(void)
264 return spi_register_driver(&ad7887_driver);
266 module_init(ad7887_init);
268 static void __exit ad7887_exit(void)
270 spi_unregister_driver(&ad7887_driver);
272 module_exit(ad7887_exit);
274 MODULE_AUTHOR("Michael Hennerich <hennerich@blackfin.uclinux.org>");
275 MODULE_DESCRIPTION("Analog Devices AD7887 ADC");
276 MODULE_LICENSE("GPL v2");