staging:iio:adc:ad7887 move to irqchip based trigger handling.
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / staging / iio / adc / ad7887_core.c
blob42e9cd7c60a30a5b071ba63c2288a7852025c010
1 /*
2 * AD7887 SPI ADC driver
4 * Copyright 2010 Analog Devices Inc.
6 * Licensed under the GPL-2 or later.
7 */
9 #include <linux/interrupt.h>
10 #include <linux/workqueue.h>
11 #include <linux/device.h>
12 #include <linux/kernel.h>
13 #include <linux/slab.h>
14 #include <linux/sysfs.h>
15 #include <linux/list.h>
16 #include <linux/spi/spi.h>
17 #include <linux/regulator/consumer.h>
18 #include <linux/err.h>
20 #include "../iio.h"
21 #include "../sysfs.h"
22 #include "../ring_generic.h"
23 #include "adc.h"
25 #include "ad7887.h"
27 static int ad7887_scan_direct(struct ad7887_state *st, unsigned ch)
29 int ret = spi_sync(st->spi, &st->msg[ch]);
30 if (ret)
31 return ret;
33 return (st->data[(ch * 2)] << 8) | st->data[(ch * 2) + 1];
36 static ssize_t ad7887_scan(struct device *dev,
37 struct device_attribute *attr,
38 char *buf)
40 struct iio_dev *dev_info = dev_get_drvdata(dev);
41 struct ad7887_state *st = dev_info->dev_data;
42 struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
43 int ret;
45 mutex_lock(&dev_info->mlock);
46 if (iio_ring_enabled(dev_info))
47 ret = ad7887_scan_from_ring(st, 1 << this_attr->address);
48 else
49 ret = ad7887_scan_direct(st, this_attr->address);
50 mutex_unlock(&dev_info->mlock);
52 if (ret < 0)
53 return ret;
55 return sprintf(buf, "%d\n", (ret >> st->chip_info->left_shift) &
56 RES_MASK(st->chip_info->bits));
58 static IIO_DEV_ATTR_IN_RAW(0, ad7887_scan, 0);
59 static IIO_DEV_ATTR_IN_RAW(1, ad7887_scan, 1);
61 static ssize_t ad7887_show_scale(struct device *dev,
62 struct device_attribute *attr,
63 char *buf)
65 /* Driver currently only support internal vref */
66 struct iio_dev *dev_info = dev_get_drvdata(dev);
67 struct ad7887_state *st = iio_dev_get_devdata(dev_info);
68 /* Corresponds to Vref / 2^(bits) */
69 unsigned int scale_uv = (st->int_vref_mv * 1000) >> st->chip_info->bits;
71 return sprintf(buf, "%d.%03d\n", scale_uv / 1000, scale_uv % 1000);
73 static IIO_DEVICE_ATTR(in_scale, S_IRUGO, ad7887_show_scale, NULL, 0);
75 static ssize_t ad7887_show_name(struct device *dev,
76 struct device_attribute *attr,
77 char *buf)
79 struct iio_dev *dev_info = dev_get_drvdata(dev);
80 struct ad7887_state *st = iio_dev_get_devdata(dev_info);
82 return sprintf(buf, "%s\n", spi_get_device_id(st->spi)->name);
84 static IIO_DEVICE_ATTR(name, S_IRUGO, ad7887_show_name, NULL, 0);
86 static struct attribute *ad7887_attributes[] = {
87 &iio_dev_attr_in0_raw.dev_attr.attr,
88 &iio_dev_attr_in1_raw.dev_attr.attr,
89 &iio_dev_attr_in_scale.dev_attr.attr,
90 &iio_dev_attr_name.dev_attr.attr,
91 NULL,
94 static mode_t ad7887_attr_is_visible(struct kobject *kobj,
95 struct attribute *attr, int n)
97 struct device *dev = container_of(kobj, struct device, kobj);
98 struct iio_dev *dev_info = dev_get_drvdata(dev);
99 struct ad7887_state *st = iio_dev_get_devdata(dev_info);
101 mode_t mode = attr->mode;
103 if ((attr == &iio_dev_attr_in1_raw.dev_attr.attr) && !st->en_dual)
104 mode = 0;
106 return mode;
109 static const struct attribute_group ad7887_attribute_group = {
110 .attrs = ad7887_attributes,
111 .is_visible = ad7887_attr_is_visible,
114 static const struct ad7887_chip_info ad7887_chip_info_tbl[] = {
116 * More devices added in future
118 [ID_AD7887] = {
119 .bits = 12,
120 .storagebits = 16,
121 .left_shift = 0,
122 .sign = IIO_SCAN_EL_TYPE_UNSIGNED,
123 .int_vref_mv = 2500,
127 static int __devinit ad7887_probe(struct spi_device *spi)
129 struct ad7887_platform_data *pdata = spi->dev.platform_data;
130 struct ad7887_state *st;
131 int ret, voltage_uv = 0;
133 st = kzalloc(sizeof(*st), GFP_KERNEL);
134 if (st == NULL) {
135 ret = -ENOMEM;
136 goto error_ret;
139 st->reg = regulator_get(&spi->dev, "vcc");
140 if (!IS_ERR(st->reg)) {
141 ret = regulator_enable(st->reg);
142 if (ret)
143 goto error_put_reg;
145 voltage_uv = regulator_get_voltage(st->reg);
148 st->chip_info =
149 &ad7887_chip_info_tbl[spi_get_device_id(spi)->driver_data];
151 spi_set_drvdata(spi, st);
153 st->spi = spi;
155 st->indio_dev = iio_allocate_device(0);
156 if (st->indio_dev == NULL) {
157 ret = -ENOMEM;
158 goto error_disable_reg;
161 /* Estabilish that the iio_dev is a child of the spi device */
162 st->indio_dev->dev.parent = &spi->dev;
163 st->indio_dev->attrs = &ad7887_attribute_group;
164 st->indio_dev->dev_data = (void *)(st);
165 st->indio_dev->driver_module = THIS_MODULE;
166 st->indio_dev->modes = INDIO_DIRECT_MODE;
168 /* Setup default message */
170 st->tx_cmd_buf[0] = AD7887_CH_AIN0 | AD7887_PM_MODE4 |
171 ((pdata && pdata->use_onchip_ref) ?
172 0 : AD7887_REF_DIS);
174 st->xfer[0].rx_buf = &st->data[0];
175 st->xfer[0].tx_buf = &st->tx_cmd_buf[0];
176 st->xfer[0].len = 2;
178 spi_message_init(&st->msg[AD7887_CH0]);
179 spi_message_add_tail(&st->xfer[0], &st->msg[AD7887_CH0]);
181 if (pdata && pdata->en_dual) {
182 st->tx_cmd_buf[0] |= AD7887_DUAL | AD7887_REF_DIS;
184 st->tx_cmd_buf[2] = AD7887_CH_AIN1 | AD7887_DUAL |
185 AD7887_REF_DIS | AD7887_PM_MODE4;
186 st->tx_cmd_buf[4] = AD7887_CH_AIN0 | AD7887_DUAL |
187 AD7887_REF_DIS | AD7887_PM_MODE4;
188 st->tx_cmd_buf[6] = AD7887_CH_AIN1 | AD7887_DUAL |
189 AD7887_REF_DIS | AD7887_PM_MODE4;
191 st->xfer[1].rx_buf = &st->data[0];
192 st->xfer[1].tx_buf = &st->tx_cmd_buf[2];
193 st->xfer[1].len = 2;
195 st->xfer[2].rx_buf = &st->data[2];
196 st->xfer[2].tx_buf = &st->tx_cmd_buf[4];
197 st->xfer[2].len = 2;
199 spi_message_init(&st->msg[AD7887_CH0_CH1]);
200 spi_message_add_tail(&st->xfer[1], &st->msg[AD7887_CH0_CH1]);
201 spi_message_add_tail(&st->xfer[2], &st->msg[AD7887_CH0_CH1]);
203 st->xfer[3].rx_buf = &st->data[0];
204 st->xfer[3].tx_buf = &st->tx_cmd_buf[6];
205 st->xfer[3].len = 2;
207 spi_message_init(&st->msg[AD7887_CH1]);
208 spi_message_add_tail(&st->xfer[3], &st->msg[AD7887_CH1]);
210 st->en_dual = true;
212 if (pdata && pdata->vref_mv)
213 st->int_vref_mv = pdata->vref_mv;
214 else if (voltage_uv)
215 st->int_vref_mv = voltage_uv / 1000;
216 else
217 dev_warn(&spi->dev, "reference voltage unspecified\n");
219 } else {
220 if (pdata && pdata->vref_mv)
221 st->int_vref_mv = pdata->vref_mv;
222 else if (pdata && pdata->use_onchip_ref)
223 st->int_vref_mv = st->chip_info->int_vref_mv;
224 else
225 dev_warn(&spi->dev, "reference voltage unspecified\n");
229 ret = ad7887_register_ring_funcs_and_init(st->indio_dev);
230 if (ret)
231 goto error_free_device;
233 ret = iio_device_register(st->indio_dev);
234 if (ret)
235 goto error_free_device;
237 ret = iio_ring_buffer_register(st->indio_dev->ring, 0);
238 if (ret)
239 goto error_cleanup_ring;
240 return 0;
242 error_cleanup_ring:
243 ad7887_ring_cleanup(st->indio_dev);
244 iio_device_unregister(st->indio_dev);
245 error_free_device:
246 iio_free_device(st->indio_dev);
247 error_disable_reg:
248 if (!IS_ERR(st->reg))
249 regulator_disable(st->reg);
250 error_put_reg:
251 if (!IS_ERR(st->reg))
252 regulator_put(st->reg);
253 kfree(st);
254 error_ret:
255 return ret;
258 static int ad7887_remove(struct spi_device *spi)
260 struct ad7887_state *st = spi_get_drvdata(spi);
261 struct iio_dev *indio_dev = st->indio_dev;
262 iio_ring_buffer_unregister(indio_dev->ring);
263 ad7887_ring_cleanup(indio_dev);
264 iio_device_unregister(indio_dev);
265 if (!IS_ERR(st->reg)) {
266 regulator_disable(st->reg);
267 regulator_put(st->reg);
269 kfree(st);
270 return 0;
273 static const struct spi_device_id ad7887_id[] = {
274 {"ad7887", ID_AD7887},
278 static struct spi_driver ad7887_driver = {
279 .driver = {
280 .name = "ad7887",
281 .bus = &spi_bus_type,
282 .owner = THIS_MODULE,
284 .probe = ad7887_probe,
285 .remove = __devexit_p(ad7887_remove),
286 .id_table = ad7887_id,
289 static int __init ad7887_init(void)
291 return spi_register_driver(&ad7887_driver);
293 module_init(ad7887_init);
295 static void __exit ad7887_exit(void)
297 spi_unregister_driver(&ad7887_driver);
299 module_exit(ad7887_exit);
301 MODULE_AUTHOR("Michael Hennerich <hennerich@blackfin.uclinux.org>");
302 MODULE_DESCRIPTION("Analog Devices AD7887 ADC");
303 MODULE_LICENSE("GPL v2");
304 MODULE_ALIAS("spi:ad7887");