]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blob - drivers/staging/iio/adc/ad7887_core.c
staging: iio: push the main buffer chrdev down to the top level.
[mirror_ubuntu-bionic-kernel.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 "../ring_generic.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 *dev_info,
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(dev_info);
42 unsigned int scale_uv;
43
44 switch (m) {
45 case 0:
46 mutex_lock(&dev_info->mlock);
47 if (iio_ring_enabled(dev_info))
48 ret = ad7887_scan_from_ring(st, 1 << chan->address);
49 else
50 ret = ad7887_scan_direct(st, chan->address);
51 mutex_unlock(&dev_info->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 (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;
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] = IIO_CHAN(IIO_IN, 0, 1, 0, NULL, 1, 0,
75 (1 << IIO_CHAN_INFO_SCALE_SHARED),
76 1, 1, IIO_ST('u', 12, 16, 0), 0),
77
78 .channel[1] = IIO_CHAN(IIO_IN, 0, 1, 0, NULL, 0, 0,
79 (1 << IIO_CHAN_INFO_SCALE_SHARED),
80 0, 0, IIO_ST('u', 12, 16, 0), 0),
81
82 .channel[2] = IIO_CHAN_SOFT_TIMESTAMP(2),
83 .int_vref_mv = 2500,
84 },
85 };
86
87 static const struct iio_info ad7887_info = {
88 .read_raw = &ad7887_read_raw,
89 .driver_module = THIS_MODULE,
90 };
91
92 static int __devinit ad7887_probe(struct spi_device *spi)
93 {
94 struct ad7887_platform_data *pdata = spi->dev.platform_data;
95 struct ad7887_state *st;
96 int ret, voltage_uv = 0, regdone = 0;
97 struct iio_dev *indio_dev = iio_allocate_device(sizeof(*st));
98
99 if (indio_dev == NULL)
100 return -ENOMEM;
101
102 st = iio_priv(indio_dev);
103
104 st->reg = regulator_get(&spi->dev, "vcc");
105 if (!IS_ERR(st->reg)) {
106 ret = regulator_enable(st->reg);
107 if (ret)
108 goto error_put_reg;
109
110 voltage_uv = regulator_get_voltage(st->reg);
111 }
112
113 st->chip_info =
114 &ad7887_chip_info_tbl[spi_get_device_id(spi)->driver_data];
115
116 spi_set_drvdata(spi, indio_dev);
117 st->spi = spi;
118
119 /* Estabilish that the iio_dev is a child of the spi device */
120 indio_dev->dev.parent = &spi->dev;
121 indio_dev->name = spi_get_device_id(spi)->name;
122 indio_dev->info = &ad7887_info;
123 indio_dev->modes = INDIO_DIRECT_MODE;
124
125 /* Setup default message */
126
127 st->tx_cmd_buf[0] = AD7887_CH_AIN0 | AD7887_PM_MODE4 |
128 ((pdata && pdata->use_onchip_ref) ?
129 0 : AD7887_REF_DIS);
130
131 st->xfer[0].rx_buf = &st->data[0];
132 st->xfer[0].tx_buf = &st->tx_cmd_buf[0];
133 st->xfer[0].len = 2;
134
135 spi_message_init(&st->msg[AD7887_CH0]);
136 spi_message_add_tail(&st->xfer[0], &st->msg[AD7887_CH0]);
137
138 if (pdata && pdata->en_dual) {
139 st->tx_cmd_buf[0] |= AD7887_DUAL | AD7887_REF_DIS;
140
141 st->tx_cmd_buf[2] = AD7887_CH_AIN1 | AD7887_DUAL |
142 AD7887_REF_DIS | AD7887_PM_MODE4;
143 st->tx_cmd_buf[4] = AD7887_CH_AIN0 | AD7887_DUAL |
144 AD7887_REF_DIS | AD7887_PM_MODE4;
145 st->tx_cmd_buf[6] = AD7887_CH_AIN1 | AD7887_DUAL |
146 AD7887_REF_DIS | AD7887_PM_MODE4;
147
148 st->xfer[1].rx_buf = &st->data[0];
149 st->xfer[1].tx_buf = &st->tx_cmd_buf[2];
150 st->xfer[1].len = 2;
151
152 st->xfer[2].rx_buf = &st->data[2];
153 st->xfer[2].tx_buf = &st->tx_cmd_buf[4];
154 st->xfer[2].len = 2;
155
156 spi_message_init(&st->msg[AD7887_CH0_CH1]);
157 spi_message_add_tail(&st->xfer[1], &st->msg[AD7887_CH0_CH1]);
158 spi_message_add_tail(&st->xfer[2], &st->msg[AD7887_CH0_CH1]);
159
160 st->xfer[3].rx_buf = &st->data[0];
161 st->xfer[3].tx_buf = &st->tx_cmd_buf[6];
162 st->xfer[3].len = 2;
163
164 spi_message_init(&st->msg[AD7887_CH1]);
165 spi_message_add_tail(&st->xfer[3], &st->msg[AD7887_CH1]);
166
167 if (pdata && pdata->vref_mv)
168 st->int_vref_mv = pdata->vref_mv;
169 else if (voltage_uv)
170 st->int_vref_mv = voltage_uv / 1000;
171 else
172 dev_warn(&spi->dev, "reference voltage unspecified\n");
173
174 indio_dev->channels = st->chip_info->channel;
175 indio_dev->num_channels = 3;
176 } else {
177 if (pdata && pdata->vref_mv)
178 st->int_vref_mv = pdata->vref_mv;
179 else if (pdata && pdata->use_onchip_ref)
180 st->int_vref_mv = st->chip_info->int_vref_mv;
181 else
182 dev_warn(&spi->dev, "reference voltage unspecified\n");
183
184 indio_dev->channels = &st->chip_info->channel[1];
185 indio_dev->num_channels = 2;
186 }
187
188 ret = ad7887_register_ring_funcs_and_init(indio_dev);
189 if (ret)
190 goto error_disable_reg;
191
192 ret = iio_device_register(indio_dev);
193 if (ret)
194 goto error_disable_reg;
195 regdone = 1;
196
197 ret = iio_ring_buffer_register_ex(indio_dev, 0,
198 indio_dev->channels,
199 indio_dev->num_channels);
200 if (ret)
201 goto error_cleanup_ring;
202 return 0;
203
204 error_cleanup_ring:
205 ad7887_ring_cleanup(indio_dev);
206 error_disable_reg:
207 if (!IS_ERR(st->reg))
208 regulator_disable(st->reg);
209 error_put_reg:
210 if (!IS_ERR(st->reg))
211 regulator_put(st->reg);
212 if (regdone)
213 iio_device_unregister(indio_dev);
214 else
215 iio_free_device(indio_dev);
216
217 return ret;
218 }
219
220 static int ad7887_remove(struct spi_device *spi)
221 {
222 struct iio_dev *indio_dev = spi_get_drvdata(spi);
223 struct ad7887_state *st = iio_priv(indio_dev);
224
225 iio_ring_buffer_unregister(indio_dev);
226 ad7887_ring_cleanup(indio_dev);
227 if (!IS_ERR(st->reg)) {
228 regulator_disable(st->reg);
229 regulator_put(st->reg);
230 }
231 iio_device_unregister(indio_dev);
232
233 return 0;
234 }
235
236 static const struct spi_device_id ad7887_id[] = {
237 {"ad7887", ID_AD7887},
238 {}
239 };
240
241 static struct spi_driver ad7887_driver = {
242 .driver = {
243 .name = "ad7887",
244 .bus = &spi_bus_type,
245 .owner = THIS_MODULE,
246 },
247 .probe = ad7887_probe,
248 .remove = __devexit_p(ad7887_remove),
249 .id_table = ad7887_id,
250 };
251
252 static int __init ad7887_init(void)
253 {
254 return spi_register_driver(&ad7887_driver);
255 }
256 module_init(ad7887_init);
257
258 static void __exit ad7887_exit(void)
259 {
260 spi_unregister_driver(&ad7887_driver);
261 }
262 module_exit(ad7887_exit);
263
264 MODULE_AUTHOR("Michael Hennerich <hennerich@blackfin.uclinux.org>");
265 MODULE_DESCRIPTION("Analog Devices AD7887 ADC");
266 MODULE_LICENSE("GPL v2");
267 MODULE_ALIAS("spi:ad7887");