]> Pileus Git - ~andy/linux/blob - drivers/staging/iio/adc/ad7887_core.c
Merge tag 'sound-3.5' of git://git.kernel.org/pub/scm/linux/kernel/git/tiwai/sound
[~andy/linux] / drivers / staging / iio / adc / ad7887_core.c
1 /*
2  * AD7887 SPI ADC driver
3  *
4  * Copyright 2010-2011 Analog Devices Inc.
5  *
6  * Licensed under the GPL-2.
7  */
8
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>
17
18 #include <linux/iio/iio.h>
19 #include <linux/iio/sysfs.h>
20 #include <linux/iio/buffer.h>
21
22
23 #include "ad7887.h"
24
25 static 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
34 static int ad7887_read_raw(struct iio_dev *indio_dev,
35                            struct iio_chan_spec const *chan,
36                            int *val,
37                            int *val2,
38                            long m)
39 {
40         int ret;
41         struct ad7887_state *st = iio_priv(indio_dev);
42         unsigned int scale_uv;
43
44         switch (m) {
45         case IIO_CHAN_INFO_RAW:
46                 mutex_lock(&indio_dev->mlock);
47                 if (iio_buffer_enabled(indio_dev))
48                         ret = -EBUSY;
49                 else
50                         ret = ad7887_scan_direct(st, chan->address);
51                 mutex_unlock(&indio_dev->mlock);
52
53                 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 IIO_CHAN_INFO_SCALE:
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;
66 }
67
68
69 static const struct ad7887_chip_info ad7887_chip_info_tbl[] = {
70         /*
71          * More devices added in future
72          */
73         [ID_AD7887] = {
74                 .channel[0] = {
75                         .type = IIO_VOLTAGE,
76                         .indexed = 1,
77                         .channel = 1,
78                         .info_mask = IIO_CHAN_INFO_RAW_SEPARATE_BIT |
79                         IIO_CHAN_INFO_SCALE_SHARED_BIT,
80                         .address = 1,
81                         .scan_index = 1,
82                         .scan_type = IIO_ST('u', 12, 16, 0),
83                 },
84                 .channel[1] = {
85                         .type = IIO_VOLTAGE,
86                         .indexed = 1,
87                         .channel = 0,
88                         .info_mask = IIO_CHAN_INFO_RAW_SEPARATE_BIT |
89                         IIO_CHAN_INFO_SCALE_SHARED_BIT,
90                         .address = 0,
91                         .scan_index = 0,
92                         .scan_type = IIO_ST('u', 12, 16, 0),
93                 },
94                 .channel[2] = IIO_CHAN_SOFT_TIMESTAMP(2),
95                 .int_vref_mv = 2500,
96         },
97 };
98
99 static const struct iio_info ad7887_info = {
100         .read_raw = &ad7887_read_raw,
101         .driver_module = THIS_MODULE,
102 };
103
104 static int __devinit ad7887_probe(struct spi_device *spi)
105 {
106         struct ad7887_platform_data *pdata = spi->dev.platform_data;
107         struct ad7887_state *st;
108         int ret, voltage_uv = 0;
109         struct iio_dev *indio_dev = iio_device_alloc(sizeof(*st));
110
111         if (indio_dev == NULL)
112                 return -ENOMEM;
113
114         st = iio_priv(indio_dev);
115
116         st->reg = regulator_get(&spi->dev, "vcc");
117         if (!IS_ERR(st->reg)) {
118                 ret = regulator_enable(st->reg);
119                 if (ret)
120                         goto error_put_reg;
121
122                 voltage_uv = regulator_get_voltage(st->reg);
123         }
124
125         st->chip_info =
126                 &ad7887_chip_info_tbl[spi_get_device_id(spi)->driver_data];
127
128         spi_set_drvdata(spi, indio_dev);
129         st->spi = spi;
130
131         /* Estabilish that the iio_dev is a child of the spi device */
132         indio_dev->dev.parent = &spi->dev;
133         indio_dev->name = spi_get_device_id(spi)->name;
134         indio_dev->info = &ad7887_info;
135         indio_dev->modes = INDIO_DIRECT_MODE;
136
137         /* Setup default message */
138
139         st->tx_cmd_buf[0] = AD7887_CH_AIN0 | AD7887_PM_MODE4 |
140                             ((pdata && pdata->use_onchip_ref) ?
141                             0 : AD7887_REF_DIS);
142
143         st->xfer[0].rx_buf = &st->data[0];
144         st->xfer[0].tx_buf = &st->tx_cmd_buf[0];
145         st->xfer[0].len = 2;
146
147         spi_message_init(&st->msg[AD7887_CH0]);
148         spi_message_add_tail(&st->xfer[0], &st->msg[AD7887_CH0]);
149
150         if (pdata && pdata->en_dual) {
151                 st->tx_cmd_buf[0] |= AD7887_DUAL | AD7887_REF_DIS;
152
153                 st->tx_cmd_buf[2] = AD7887_CH_AIN1 | AD7887_DUAL |
154                                     AD7887_REF_DIS | AD7887_PM_MODE4;
155                 st->tx_cmd_buf[4] = AD7887_CH_AIN0 | AD7887_DUAL |
156                                     AD7887_REF_DIS | AD7887_PM_MODE4;
157                 st->tx_cmd_buf[6] = AD7887_CH_AIN1 | AD7887_DUAL |
158                                     AD7887_REF_DIS | AD7887_PM_MODE4;
159
160                 st->xfer[1].rx_buf = &st->data[0];
161                 st->xfer[1].tx_buf = &st->tx_cmd_buf[2];
162                 st->xfer[1].len = 2;
163
164                 st->xfer[2].rx_buf = &st->data[2];
165                 st->xfer[2].tx_buf = &st->tx_cmd_buf[4];
166                 st->xfer[2].len = 2;
167
168                 spi_message_init(&st->msg[AD7887_CH0_CH1]);
169                 spi_message_add_tail(&st->xfer[1], &st->msg[AD7887_CH0_CH1]);
170                 spi_message_add_tail(&st->xfer[2], &st->msg[AD7887_CH0_CH1]);
171
172                 st->xfer[3].rx_buf = &st->data[0];
173                 st->xfer[3].tx_buf = &st->tx_cmd_buf[6];
174                 st->xfer[3].len = 2;
175
176                 spi_message_init(&st->msg[AD7887_CH1]);
177                 spi_message_add_tail(&st->xfer[3], &st->msg[AD7887_CH1]);
178
179                 if (pdata && pdata->vref_mv)
180                         st->int_vref_mv = pdata->vref_mv;
181                 else if (voltage_uv)
182                         st->int_vref_mv = voltage_uv / 1000;
183                 else
184                         dev_warn(&spi->dev, "reference voltage unspecified\n");
185
186                 indio_dev->channels = st->chip_info->channel;
187                 indio_dev->num_channels = 3;
188         } else {
189                 if (pdata && pdata->vref_mv)
190                         st->int_vref_mv = pdata->vref_mv;
191                 else if (pdata && pdata->use_onchip_ref)
192                         st->int_vref_mv = st->chip_info->int_vref_mv;
193                 else
194                         dev_warn(&spi->dev, "reference voltage unspecified\n");
195
196                 indio_dev->channels = &st->chip_info->channel[1];
197                 indio_dev->num_channels = 2;
198         }
199
200         ret = ad7887_register_ring_funcs_and_init(indio_dev);
201         if (ret)
202                 goto error_disable_reg;
203
204         ret = iio_buffer_register(indio_dev,
205                                   indio_dev->channels,
206                                   indio_dev->num_channels);
207         if (ret)
208                 goto error_cleanup_ring;
209
210         ret = iio_device_register(indio_dev);
211         if (ret)
212                 goto error_unregister_ring;
213
214         return 0;
215 error_unregister_ring:
216         iio_buffer_unregister(indio_dev);
217 error_cleanup_ring:
218         ad7887_ring_cleanup(indio_dev);
219 error_disable_reg:
220         if (!IS_ERR(st->reg))
221                 regulator_disable(st->reg);
222 error_put_reg:
223         if (!IS_ERR(st->reg))
224                 regulator_put(st->reg);
225         iio_device_free(indio_dev);
226
227         return ret;
228 }
229
230 static int ad7887_remove(struct spi_device *spi)
231 {
232         struct iio_dev *indio_dev = spi_get_drvdata(spi);
233         struct ad7887_state *st = iio_priv(indio_dev);
234
235         iio_device_unregister(indio_dev);
236         iio_buffer_unregister(indio_dev);
237         ad7887_ring_cleanup(indio_dev);
238         if (!IS_ERR(st->reg)) {
239                 regulator_disable(st->reg);
240                 regulator_put(st->reg);
241         }
242         iio_device_free(indio_dev);
243
244         return 0;
245 }
246
247 static const struct spi_device_id ad7887_id[] = {
248         {"ad7887", ID_AD7887},
249         {}
250 };
251 MODULE_DEVICE_TABLE(spi, ad7887_id);
252
253 static struct spi_driver ad7887_driver = {
254         .driver = {
255                 .name   = "ad7887",
256                 .owner  = THIS_MODULE,
257         },
258         .probe          = ad7887_probe,
259         .remove         = __devexit_p(ad7887_remove),
260         .id_table       = ad7887_id,
261 };
262 module_spi_driver(ad7887_driver);
263
264 MODULE_AUTHOR("Michael Hennerich <hennerich@blackfin.uclinux.org>");
265 MODULE_DESCRIPTION("Analog Devices AD7887 ADC");
266 MODULE_LICENSE("GPL v2");