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>
19 #include "../ring_generic.h"
24 static int ad7887_scan_direct(struct ad7887_state *st, unsigned ch)
26 int ret = spi_sync(st->spi, &st->msg[ch]);
30 return (st->data[(ch * 2)] << 8) | st->data[(ch * 2) + 1];
33 static int ad7887_read_raw(struct iio_dev *dev_info,
34 struct iio_chan_spec const *chan,
40 struct ad7887_state *st = dev_info->dev_data;
41 unsigned int scale_uv;
45 mutex_lock(&dev_info->mlock);
46 if (iio_ring_enabled(dev_info))
47 ret = ad7887_scan_from_ring(st, 1 << chan->address);
49 ret = ad7887_scan_direct(st, chan->address);
50 mutex_unlock(&dev_info->mlock);
54 *val = (ret >> st->chip_info->channel[0].scan_type.shift) &
55 RES_MASK(st->chip_info->channel[0].scan_type.realbits);
57 case (1 << IIO_CHAN_INFO_SCALE_SHARED):
58 scale_uv = (st->int_vref_mv * 1000)
59 >> st->chip_info->channel[0].scan_type.realbits;
61 *val2 = (scale_uv%1000)*1000;
62 return IIO_VAL_INT_PLUS_MICRO;
68 static const struct ad7887_chip_info ad7887_chip_info_tbl[] = {
70 * More devices added in future
73 .channel[0] = IIO_CHAN(IIO_IN, 0, 1, 0, NULL, 1, 0,
74 (1 << IIO_CHAN_INFO_SCALE_SHARED),
75 1, 1, IIO_ST('u', 12, 16, 0), 0),
77 .channel[1] = IIO_CHAN(IIO_IN, 0, 1, 0, NULL, 0, 0,
78 (1 << IIO_CHAN_INFO_SCALE_SHARED),
79 0, 0, IIO_ST('u', 12, 16, 0), 0),
81 .channel[2] = IIO_CHAN_SOFT_TIMESTAMP(2),
86 static int __devinit ad7887_probe(struct spi_device *spi)
88 struct ad7887_platform_data *pdata = spi->dev.platform_data;
89 struct ad7887_state *st;
90 int ret, voltage_uv = 0, regdone = 0;
91 struct iio_dev *indio_dev = iio_allocate_device(sizeof(*st));
93 if (indio_dev == NULL)
96 st = iio_priv(indio_dev);
98 st->reg = regulator_get(&spi->dev, "vcc");
99 if (!IS_ERR(st->reg)) {
100 ret = regulator_enable(st->reg);
104 voltage_uv = regulator_get_voltage(st->reg);
108 &ad7887_chip_info_tbl[spi_get_device_id(spi)->driver_data];
110 spi_set_drvdata(spi, indio_dev);
113 /* Estabilish that the iio_dev is a child of the spi device */
114 indio_dev->dev.parent = &spi->dev;
115 indio_dev->name = spi_get_device_id(spi)->name;
116 indio_dev->dev_data = (void *)(st);
117 indio_dev->read_raw = &ad7887_read_raw;
118 indio_dev->driver_module = THIS_MODULE;
119 indio_dev->modes = INDIO_DIRECT_MODE;
121 /* Setup default message */
123 st->tx_cmd_buf[0] = AD7887_CH_AIN0 | AD7887_PM_MODE4 |
124 ((pdata && pdata->use_onchip_ref) ?
127 st->xfer[0].rx_buf = &st->data[0];
128 st->xfer[0].tx_buf = &st->tx_cmd_buf[0];
131 spi_message_init(&st->msg[AD7887_CH0]);
132 spi_message_add_tail(&st->xfer[0], &st->msg[AD7887_CH0]);
134 if (pdata && pdata->en_dual) {
135 st->tx_cmd_buf[0] |= AD7887_DUAL | AD7887_REF_DIS;
137 st->tx_cmd_buf[2] = AD7887_CH_AIN1 | AD7887_DUAL |
138 AD7887_REF_DIS | AD7887_PM_MODE4;
139 st->tx_cmd_buf[4] = AD7887_CH_AIN0 | AD7887_DUAL |
140 AD7887_REF_DIS | AD7887_PM_MODE4;
141 st->tx_cmd_buf[6] = AD7887_CH_AIN1 | AD7887_DUAL |
142 AD7887_REF_DIS | AD7887_PM_MODE4;
144 st->xfer[1].rx_buf = &st->data[0];
145 st->xfer[1].tx_buf = &st->tx_cmd_buf[2];
148 st->xfer[2].rx_buf = &st->data[2];
149 st->xfer[2].tx_buf = &st->tx_cmd_buf[4];
152 spi_message_init(&st->msg[AD7887_CH0_CH1]);
153 spi_message_add_tail(&st->xfer[1], &st->msg[AD7887_CH0_CH1]);
154 spi_message_add_tail(&st->xfer[2], &st->msg[AD7887_CH0_CH1]);
156 st->xfer[3].rx_buf = &st->data[0];
157 st->xfer[3].tx_buf = &st->tx_cmd_buf[6];
160 spi_message_init(&st->msg[AD7887_CH1]);
161 spi_message_add_tail(&st->xfer[3], &st->msg[AD7887_CH1]);
163 if (pdata && pdata->vref_mv)
164 st->int_vref_mv = pdata->vref_mv;
166 st->int_vref_mv = voltage_uv / 1000;
168 dev_warn(&spi->dev, "reference voltage unspecified\n");
170 indio_dev->channels = st->chip_info->channel;
171 indio_dev->num_channels = 3;
173 if (pdata && pdata->vref_mv)
174 st->int_vref_mv = pdata->vref_mv;
175 else if (pdata && pdata->use_onchip_ref)
176 st->int_vref_mv = st->chip_info->int_vref_mv;
178 dev_warn(&spi->dev, "reference voltage unspecified\n");
180 indio_dev->channels = &st->chip_info->channel[1];
181 indio_dev->num_channels = 2;
184 ret = ad7887_register_ring_funcs_and_init(indio_dev);
186 goto error_disable_reg;
188 ret = iio_device_register(indio_dev);
190 goto error_disable_reg;
193 ret = iio_ring_buffer_register_ex(indio_dev->ring, 0,
195 indio_dev->num_channels);
197 goto error_cleanup_ring;
201 ad7887_ring_cleanup(indio_dev);
203 if (!IS_ERR(st->reg))
204 regulator_disable(st->reg);
206 if (!IS_ERR(st->reg))
207 regulator_put(st->reg);
209 iio_device_unregister(indio_dev);
211 iio_free_device(indio_dev);
216 static int ad7887_remove(struct spi_device *spi)
218 struct iio_dev *indio_dev = spi_get_drvdata(spi);
219 struct ad7887_state *st = iio_priv(indio_dev);
221 iio_ring_buffer_unregister(indio_dev->ring);
222 ad7887_ring_cleanup(indio_dev);
223 if (!IS_ERR(st->reg)) {
224 regulator_disable(st->reg);
225 regulator_put(st->reg);
227 iio_device_unregister(indio_dev);
232 static const struct spi_device_id ad7887_id[] = {
233 {"ad7887", ID_AD7887},
237 static struct spi_driver ad7887_driver = {
240 .bus = &spi_bus_type,
241 .owner = THIS_MODULE,
243 .probe = ad7887_probe,
244 .remove = __devexit_p(ad7887_remove),
245 .id_table = ad7887_id,
248 static int __init ad7887_init(void)
250 return spi_register_driver(&ad7887_driver);
252 module_init(ad7887_init);
254 static void __exit ad7887_exit(void)
256 spi_unregister_driver(&ad7887_driver);
258 module_exit(ad7887_exit);
260 MODULE_AUTHOR("Michael Hennerich <hennerich@blackfin.uclinux.org>");
261 MODULE_DESCRIPTION("Analog Devices AD7887 ADC");
262 MODULE_LICENSE("GPL v2");
263 MODULE_ALIAS("spi:ad7887");