]> git.kernelconcepts.de Git - karo-tx-linux.git/blob - drivers/staging/iio/adc/ad7887_core.c
staging:iio: Add missing MODULE_DEVICE_TABLE and MODULE_ALIAS
[karo-tx-linux.git] / 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 "../iio.h"
19 #include "../sysfs.h"
20 #include "../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 0:
46                 mutex_lock(&indio_dev->mlock);
47                 if (iio_buffer_enabled(indio_dev))
48                         ret = ad7887_scan_from_ring(st, 1 << chan->address);
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_SCALE_SHARED_BIT,
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 = IIO_CHAN_INFO_SCALE_SHARED_BIT,
88                         .address = 0,
89                         .scan_index = 0,
90                         .scan_type = IIO_ST('u', 12, 16, 0),
91                 },
92                 .channel[2] = IIO_CHAN_SOFT_TIMESTAMP(2),
93                 .int_vref_mv = 2500,
94         },
95 };
96
97 static const struct iio_info ad7887_info = {
98         .read_raw = &ad7887_read_raw,
99         .driver_module = THIS_MODULE,
100 };
101
102 static int __devinit ad7887_probe(struct spi_device *spi)
103 {
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));
108
109         if (indio_dev == NULL)
110                 return -ENOMEM;
111
112         st = iio_priv(indio_dev);
113
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
126         spi_set_drvdata(spi, indio_dev);
127         st->spi = spi;
128
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;
134
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
177                 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
184                 indio_dev->channels = st->chip_info->channel;
185                 indio_dev->num_channels = 3;
186         } 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");
193
194                 indio_dev->channels = &st->chip_info->channel[1];
195                 indio_dev->num_channels = 2;
196         }
197
198         ret = ad7887_register_ring_funcs_and_init(indio_dev);
199         if (ret)
200                 goto error_disable_reg;
201
202         ret = iio_buffer_register(indio_dev,
203                                   indio_dev->channels,
204                                   indio_dev->num_channels);
205         if (ret)
206                 goto error_cleanup_ring;
207
208         ret = iio_device_register(indio_dev);
209         if (ret)
210                 goto error_unregister_ring;
211
212         return 0;
213 error_unregister_ring:
214         iio_buffer_unregister(indio_dev);
215 error_cleanup_ring:
216         ad7887_ring_cleanup(indio_dev);
217 error_disable_reg:
218         if (!IS_ERR(st->reg))
219                 regulator_disable(st->reg);
220 error_put_reg:
221         if (!IS_ERR(st->reg))
222                 regulator_put(st->reg);
223         iio_free_device(indio_dev);
224
225         return ret;
226 }
227
228 static int ad7887_remove(struct spi_device *spi)
229 {
230         struct iio_dev *indio_dev = spi_get_drvdata(spi);
231         struct ad7887_state *st = iio_priv(indio_dev);
232
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);
239         }
240         iio_free_device(indio_dev);
241
242         return 0;
243 }
244
245 static const struct spi_device_id ad7887_id[] = {
246         {"ad7887", ID_AD7887},
247         {}
248 };
249 MODULE_DEVICE_TABLE(spi, ad7887_id);
250
251 static struct spi_driver ad7887_driver = {
252         .driver = {
253                 .name   = "ad7887",
254                 .bus    = &spi_bus_type,
255                 .owner  = THIS_MODULE,
256         },
257         .probe          = ad7887_probe,
258         .remove         = __devexit_p(ad7887_remove),
259         .id_table       = ad7887_id,
260 };
261
262 static int __init ad7887_init(void)
263 {
264         return spi_register_driver(&ad7887_driver);
265 }
266 module_init(ad7887_init);
267
268 static void __exit ad7887_exit(void)
269 {
270         spi_unregister_driver(&ad7887_driver);
271 }
272 module_exit(ad7887_exit);
273
274 MODULE_AUTHOR("Michael Hennerich <hennerich@blackfin.uclinux.org>");
275 MODULE_DESCRIPTION("Analog Devices AD7887 ADC");
276 MODULE_LICENSE("GPL v2");