blob: a7baa9bb0eaa0bfdaa0255545a89798852f1f419 [file] [log] [blame]
Michael Hennerich2b4756a2010-11-22 14:35:32 +01001/*
2 * AD7887 SPI ADC driver
3 *
Michael Hennerich596d0602011-05-18 14:41:50 +01004 * Copyright 2010-2011 Analog Devices Inc.
Michael Hennerich2b4756a2010-11-22 14:35:32 +01005 *
Michael Hennerich596d0602011-05-18 14:41:50 +01006 * Licensed under the GPL-2.
Michael Hennerich2b4756a2010-11-22 14:35:32 +01007 */
8
Michael Hennerich2b4756a2010-11-22 14:35:32 +01009#include <linux/device.h>
10#include <linux/kernel.h>
11#include <linux/slab.h>
12#include <linux/sysfs.h>
Michael Hennerich2b4756a2010-11-22 14:35:32 +010013#include <linux/spi/spi.h>
14#include <linux/regulator/consumer.h>
15#include <linux/err.h>
Paul Gortmaker99c97852011-07-03 15:49:50 -040016#include <linux/module.h>
Michael Hennerich2b4756a2010-11-22 14:35:32 +010017
18#include "../iio.h"
19#include "../sysfs.h"
Jonathan Cameronaf5046a2011-10-26 17:41:32 +010020#include "../buffer.h"
Jonathan Cameroncdf38702011-08-12 17:08:43 +010021
Michael Hennerich2b4756a2010-11-22 14:35:32 +010022
23#include "ad7887.h"
24
25static int ad7887_scan_direct(struct ad7887_state *st, unsigned ch)
26{
27 int ret = spi_sync(st->spi, &st->msg[ch]);
28 if (ret)
29 return ret;
30
31 return (st->data[(ch * 2)] << 8) | st->data[(ch * 2) + 1];
32}
33
Jonathan Cameron84f79ec2011-10-06 17:14:37 +010034static int ad7887_read_raw(struct iio_dev *indio_dev,
Michael Hennerich596d0602011-05-18 14:41:50 +010035 struct iio_chan_spec const *chan,
36 int *val,
37 int *val2,
38 long m)
Michael Hennerich2b4756a2010-11-22 14:35:32 +010039{
Michael Hennerich2b4756a2010-11-22 14:35:32 +010040 int ret;
Jonathan Cameron84f79ec2011-10-06 17:14:37 +010041 struct ad7887_state *st = iio_priv(indio_dev);
Michael Hennerich596d0602011-05-18 14:41:50 +010042 unsigned int scale_uv;
Michael Hennerich2b4756a2010-11-22 14:35:32 +010043
Michael Hennerich596d0602011-05-18 14:41:50 +010044 switch (m) {
45 case 0:
Jonathan Cameron84f79ec2011-10-06 17:14:37 +010046 mutex_lock(&indio_dev->mlock);
47 if (iio_buffer_enabled(indio_dev))
Michael Hennerich596d0602011-05-18 14:41:50 +010048 ret = ad7887_scan_from_ring(st, 1 << chan->address);
49 else
50 ret = ad7887_scan_direct(st, chan->address);
Jonathan Cameron84f79ec2011-10-06 17:14:37 +010051 mutex_unlock(&indio_dev->mlock);
Michael Hennerich2b4756a2010-11-22 14:35:32 +010052
Michael Hennerich596d0602011-05-18 14:41:50 +010053 if (ret < 0)
54 return ret;
55 *val = (ret >> st->chip_info->channel[0].scan_type.shift) &
56 RES_MASK(st->chip_info->channel[0].scan_type.realbits);
57 return IIO_VAL_INT;
58 case (1 << IIO_CHAN_INFO_SCALE_SHARED):
59 scale_uv = (st->int_vref_mv * 1000)
60 >> st->chip_info->channel[0].scan_type.realbits;
61 *val = scale_uv/1000;
62 *val2 = (scale_uv%1000)*1000;
63 return IIO_VAL_INT_PLUS_MICRO;
64 }
65 return -EINVAL;
Michael Hennerich2b4756a2010-11-22 14:35:32 +010066}
67
Michael Hennerich2b4756a2010-11-22 14:35:32 +010068
69static const struct ad7887_chip_info ad7887_chip_info_tbl[] = {
70 /*
71 * More devices added in future
72 */
73 [ID_AD7887] = {
Jonathan Cameron31bf47d2011-09-30 10:05:44 +010074 .channel[0] = {
75 .type = IIO_VOLTAGE,
76 .indexed = 1,
77 .channel = 1,
78 .info_mask = (1 << IIO_CHAN_INFO_SCALE_SHARED),
79 .address = 1,
80 .scan_index = 1,
81 .scan_type = IIO_ST('u', 12, 16, 0),
82 },
83 .channel[1] = {
84 .type = IIO_VOLTAGE,
85 .indexed = 1,
86 .channel = 0,
87 .info_mask = (1 << IIO_CHAN_INFO_SCALE_SHARED),
88 .address = 0,
89 .scan_index = 0,
90 .scan_type = IIO_ST('u', 12, 16, 0),
91 },
Michael Hennerich596d0602011-05-18 14:41:50 +010092 .channel[2] = IIO_CHAN_SOFT_TIMESTAMP(2),
Michael Hennerich2b4756a2010-11-22 14:35:32 +010093 .int_vref_mv = 2500,
94 },
95};
96
Jonathan Cameron6fe81352011-05-18 14:42:37 +010097static const struct iio_info ad7887_info = {
98 .read_raw = &ad7887_read_raw,
99 .driver_module = THIS_MODULE,
100};
101
Michael Hennerich2b4756a2010-11-22 14:35:32 +0100102static int __devinit ad7887_probe(struct spi_device *spi)
103{
104 struct ad7887_platform_data *pdata = spi->dev.platform_data;
105 struct ad7887_state *st;
Jonathan Cameron26d25ae2011-09-02 17:14:40 +0100106 int ret, voltage_uv = 0;
Michael Hennerichf39e0862011-05-18 14:41:51 +0100107 struct iio_dev *indio_dev = iio_allocate_device(sizeof(*st));
Michael Hennerich2b4756a2010-11-22 14:35:32 +0100108
Michael Hennerichf39e0862011-05-18 14:41:51 +0100109 if (indio_dev == NULL)
110 return -ENOMEM;
111
112 st = iio_priv(indio_dev);
Michael Hennerich2b4756a2010-11-22 14:35:32 +0100113
114 st->reg = regulator_get(&spi->dev, "vcc");
115 if (!IS_ERR(st->reg)) {
116 ret = regulator_enable(st->reg);
117 if (ret)
118 goto error_put_reg;
119
120 voltage_uv = regulator_get_voltage(st->reg);
121 }
122
123 st->chip_info =
124 &ad7887_chip_info_tbl[spi_get_device_id(spi)->driver_data];
125
Michael Hennerichf39e0862011-05-18 14:41:51 +0100126 spi_set_drvdata(spi, indio_dev);
Michael Hennerich2b4756a2010-11-22 14:35:32 +0100127 st->spi = spi;
128
Michael Hennerich2b4756a2010-11-22 14:35:32 +0100129 /* Estabilish that the iio_dev is a child of the spi device */
Michael Hennerichf39e0862011-05-18 14:41:51 +0100130 indio_dev->dev.parent = &spi->dev;
131 indio_dev->name = spi_get_device_id(spi)->name;
Jonathan Cameron6fe81352011-05-18 14:42:37 +0100132 indio_dev->info = &ad7887_info;
Michael Hennerichf39e0862011-05-18 14:41:51 +0100133 indio_dev->modes = INDIO_DIRECT_MODE;
Michael Hennerich2b4756a2010-11-22 14:35:32 +0100134
135 /* Setup default message */
136
137 st->tx_cmd_buf[0] = AD7887_CH_AIN0 | AD7887_PM_MODE4 |
138 ((pdata && pdata->use_onchip_ref) ?
139 0 : AD7887_REF_DIS);
140
141 st->xfer[0].rx_buf = &st->data[0];
142 st->xfer[0].tx_buf = &st->tx_cmd_buf[0];
143 st->xfer[0].len = 2;
144
145 spi_message_init(&st->msg[AD7887_CH0]);
146 spi_message_add_tail(&st->xfer[0], &st->msg[AD7887_CH0]);
147
148 if (pdata && pdata->en_dual) {
149 st->tx_cmd_buf[0] |= AD7887_DUAL | AD7887_REF_DIS;
150
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;
157
158 st->xfer[1].rx_buf = &st->data[0];
159 st->xfer[1].tx_buf = &st->tx_cmd_buf[2];
160 st->xfer[1].len = 2;
161
162 st->xfer[2].rx_buf = &st->data[2];
163 st->xfer[2].tx_buf = &st->tx_cmd_buf[4];
164 st->xfer[2].len = 2;
165
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]);
169
170 st->xfer[3].rx_buf = &st->data[0];
171 st->xfer[3].tx_buf = &st->tx_cmd_buf[6];
172 st->xfer[3].len = 2;
173
174 spi_message_init(&st->msg[AD7887_CH1]);
175 spi_message_add_tail(&st->xfer[3], &st->msg[AD7887_CH1]);
176
Michael Hennerich2b4756a2010-11-22 14:35:32 +0100177 if (pdata && pdata->vref_mv)
178 st->int_vref_mv = pdata->vref_mv;
179 else if (voltage_uv)
180 st->int_vref_mv = voltage_uv / 1000;
181 else
182 dev_warn(&spi->dev, "reference voltage unspecified\n");
183
Michael Hennerichf39e0862011-05-18 14:41:51 +0100184 indio_dev->channels = st->chip_info->channel;
185 indio_dev->num_channels = 3;
Michael Hennerich2b4756a2010-11-22 14:35:32 +0100186 } else {
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;
191 else
192 dev_warn(&spi->dev, "reference voltage unspecified\n");
Michael Hennerich2b4756a2010-11-22 14:35:32 +0100193
Michael Hennerichf39e0862011-05-18 14:41:51 +0100194 indio_dev->channels = &st->chip_info->channel[1];
195 indio_dev->num_channels = 2;
Michael Hennerich596d0602011-05-18 14:41:50 +0100196 }
Michael Hennerich2b4756a2010-11-22 14:35:32 +0100197
Michael Hennerichf39e0862011-05-18 14:41:51 +0100198 ret = ad7887_register_ring_funcs_and_init(indio_dev);
Michael Hennerich2b4756a2010-11-22 14:35:32 +0100199 if (ret)
Michael Hennerichf39e0862011-05-18 14:41:51 +0100200 goto error_disable_reg;
Michael Hennerich2b4756a2010-11-22 14:35:32 +0100201
Jonathan Cameron14555b12011-09-21 11:15:57 +0100202 ret = iio_buffer_register(indio_dev,
203 indio_dev->channels,
204 indio_dev->num_channels);
Michael Hennerich2b4756a2010-11-22 14:35:32 +0100205 if (ret)
206 goto error_cleanup_ring;
Michael Hennerich2b4756a2010-11-22 14:35:32 +0100207
Jonathan Cameron26d25ae2011-09-02 17:14:40 +0100208 ret = iio_device_register(indio_dev);
209 if (ret)
210 goto error_unregister_ring;
211
212 return 0;
213error_unregister_ring:
Jonathan Cameron14555b12011-09-21 11:15:57 +0100214 iio_buffer_unregister(indio_dev);
Michael Hennerich2b4756a2010-11-22 14:35:32 +0100215error_cleanup_ring:
Michael Hennerichf39e0862011-05-18 14:41:51 +0100216 ad7887_ring_cleanup(indio_dev);
Michael Hennerich2b4756a2010-11-22 14:35:32 +0100217error_disable_reg:
218 if (!IS_ERR(st->reg))
219 regulator_disable(st->reg);
220error_put_reg:
221 if (!IS_ERR(st->reg))
222 regulator_put(st->reg);
Jonathan Cameron26d25ae2011-09-02 17:14:40 +0100223 iio_free_device(indio_dev);
Michael Hennerichf39e0862011-05-18 14:41:51 +0100224
Michael Hennerich2b4756a2010-11-22 14:35:32 +0100225 return ret;
226}
227
228static int ad7887_remove(struct spi_device *spi)
229{
Michael Hennerichf39e0862011-05-18 14:41:51 +0100230 struct iio_dev *indio_dev = spi_get_drvdata(spi);
231 struct ad7887_state *st = iio_priv(indio_dev);
232
Jonathan Camerond2fffd62011-10-14 14:46:58 +0100233 iio_device_unregister(indio_dev);
Jonathan Cameron14555b12011-09-21 11:15:57 +0100234 iio_buffer_unregister(indio_dev);
Michael Hennerich2b4756a2010-11-22 14:35:32 +0100235 ad7887_ring_cleanup(indio_dev);
Michael Hennerich2b4756a2010-11-22 14:35:32 +0100236 if (!IS_ERR(st->reg)) {
237 regulator_disable(st->reg);
238 regulator_put(st->reg);
239 }
Jonathan Camerond2fffd62011-10-14 14:46:58 +0100240 iio_free_device(indio_dev);
Michael Hennerichf39e0862011-05-18 14:41:51 +0100241
Michael Hennerich2b4756a2010-11-22 14:35:32 +0100242 return 0;
243}
244
245static const struct spi_device_id ad7887_id[] = {
246 {"ad7887", ID_AD7887},
247 {}
248};
249
250static struct spi_driver ad7887_driver = {
251 .driver = {
252 .name = "ad7887",
253 .bus = &spi_bus_type,
254 .owner = THIS_MODULE,
255 },
256 .probe = ad7887_probe,
257 .remove = __devexit_p(ad7887_remove),
258 .id_table = ad7887_id,
259};
260
261static int __init ad7887_init(void)
262{
263 return spi_register_driver(&ad7887_driver);
264}
265module_init(ad7887_init);
266
267static void __exit ad7887_exit(void)
268{
269 spi_unregister_driver(&ad7887_driver);
270}
271module_exit(ad7887_exit);
272
273MODULE_AUTHOR("Michael Hennerich <hennerich@blackfin.uclinux.org>");
274MODULE_DESCRIPTION("Analog Devices AD7887 ADC");
275MODULE_LICENSE("GPL v2");
276MODULE_ALIAS("spi:ad7887");