aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMichael Hennerich <michael.hennerich@analog.com>2011-02-24 06:32:45 -0500
committerGreg Kroah-Hartman <gregkh@suse.de>2011-02-28 16:39:54 -0500
commit7c31b984c4d119d0e32a1696fd4ca6b506a73d10 (patch)
tree472e752709dec123aafb7fb02fd4ba51268e7a54
parent5bfcf90bfb1889885211e8a853c6b70500065fce (diff)
IIO: ADC: New driver for the AD7298 8-channel SPI ADC
This patch adds support for the AD7298: 8-Channel, 1MSPS, 12-Bit SAR ADC with Temperature Sensor via SPI bus. This patch replaces the existing ad7298.c driver completely. It was necessary since, the old driver did not comply with the IIO ABI for such devices. Changes since V1: IIO: ADC: New driver for the AD7298 8-channel SPI ADC Add documentation for new sysfs file tempX_input. Simplify bit defines. Remove outdated comments. Fix indention style. ad7298_show_temp(): Add locking. Simplify temperature calculation. Change temperature result from degrees into milli degrees Celsius. Rename file according to new sysfs ABI documentation Add timestamp attributes. Revise timestamp handling accordingly. Preset timestamp generation. Signed-off-by: Michael Hennerich <michael.hennerich@analog.com> Acked-by: Jonathan Cameron <jic23@cam.ac.uk> Reviewed-by: Shubhrajyoti <Shubhrajyoti@ti.com> Signed-off-by: Greg Kroah-Hartman <gregkh@suse.de>
-rw-r--r--drivers/staging/iio/Documentation/sysfs-bus-iio6
-rw-r--r--drivers/staging/iio/adc/Kconfig7
-rw-r--r--drivers/staging/iio/adc/Makefile5
-rw-r--r--drivers/staging/iio/adc/ad7298.c501
-rw-r--r--drivers/staging/iio/adc/ad7298.h80
-rw-r--r--drivers/staging/iio/adc/ad7298_core.c287
-rw-r--r--drivers/staging/iio/adc/ad7298_ring.c258
7 files changed, 640 insertions, 504 deletions
diff --git a/drivers/staging/iio/Documentation/sysfs-bus-iio b/drivers/staging/iio/Documentation/sysfs-bus-iio
index da25bc73983..8058fcbb87d 100644
--- a/drivers/staging/iio/Documentation/sysfs-bus-iio
+++ b/drivers/staging/iio/Documentation/sysfs-bus-iio
@@ -111,6 +111,12 @@ Description:
111 sensor is associated with one part of a compound device (e.g. 111 sensor is associated with one part of a compound device (e.g.
112 a gyroscope axis). 112 a gyroscope axis).
113 113
114What: /sys/bus/iio/devices/deviceX/tempX_input
115KernelVersion: 2.6.38
116Contact: linux-iio@vger.kernel.org
117Description:
118 Scaled temperature measurement in milli degrees Celsius.
119
114What: /sys/bus/iio/devices/deviceX/accel_x_raw 120What: /sys/bus/iio/devices/deviceX/accel_x_raw
115What: /sys/bus/iio/devices/deviceX/accel_y_raw 121What: /sys/bus/iio/devices/deviceX/accel_y_raw
116What: /sys/bus/iio/devices/deviceX/accel_z_raw 122What: /sys/bus/iio/devices/deviceX/accel_z_raw
diff --git a/drivers/staging/iio/adc/Kconfig b/drivers/staging/iio/adc/Kconfig
index 5613b30bc66..6692a3d87f2 100644
--- a/drivers/staging/iio/adc/Kconfig
+++ b/drivers/staging/iio/adc/Kconfig
@@ -49,11 +49,14 @@ config AD7291
49 temperature sensors. 49 temperature sensors.
50 50
51config AD7298 51config AD7298
52 tristate "Analog Devices AD7298 temperature sensor and ADC driver" 52 tristate "Analog Devices AD7298 ADC driver"
53 depends on SPI 53 depends on SPI
54 help 54 help
55 Say yes here to build support for Analog Devices AD7298 55 Say yes here to build support for Analog Devices AD7298
56 temperature sensors and ADC. 56 8 Channel ADC with temperature sensor.
57
58 To compile this driver as a module, choose M here: the
59 module will be called ad7298.
57 60
58config AD7314 61config AD7314
59 tristate "Analog Devices AD7314 temperature sensor driver" 62 tristate "Analog Devices AD7314 temperature sensor driver"
diff --git a/drivers/staging/iio/adc/Makefile b/drivers/staging/iio/adc/Makefile
index cb73346d6d9..31067defd79 100644
--- a/drivers/staging/iio/adc/Makefile
+++ b/drivers/staging/iio/adc/Makefile
@@ -25,10 +25,13 @@ ad7887-y := ad7887_core.o
25ad7887-$(CONFIG_IIO_RING_BUFFER) += ad7887_ring.o 25ad7887-$(CONFIG_IIO_RING_BUFFER) += ad7887_ring.o
26obj-$(CONFIG_AD7887) += ad7887.o 26obj-$(CONFIG_AD7887) += ad7887.o
27 27
28ad7298-y := ad7298_core.o
29ad7298-$(CONFIG_IIO_RING_BUFFER) += ad7298_ring.o
30obj-$(CONFIG_AD7298) += ad7298.o
31
28obj-$(CONFIG_AD7150) += ad7150.o 32obj-$(CONFIG_AD7150) += ad7150.o
29obj-$(CONFIG_AD7152) += ad7152.o 33obj-$(CONFIG_AD7152) += ad7152.o
30obj-$(CONFIG_AD7291) += ad7291.o 34obj-$(CONFIG_AD7291) += ad7291.o
31obj-$(CONFIG_AD7298) += ad7298.o
32obj-$(CONFIG_AD7314) += ad7314.o 35obj-$(CONFIG_AD7314) += ad7314.o
33obj-$(CONFIG_AD7745) += ad7745.o 36obj-$(CONFIG_AD7745) += ad7745.o
34obj-$(CONFIG_AD7816) += ad7816.o 37obj-$(CONFIG_AD7816) += ad7816.o
diff --git a/drivers/staging/iio/adc/ad7298.c b/drivers/staging/iio/adc/ad7298.c
deleted file mode 100644
index 1a080c97763..00000000000
--- a/drivers/staging/iio/adc/ad7298.c
+++ /dev/null
@@ -1,501 +0,0 @@
1/*
2 * AD7298 digital temperature sensor driver supporting AD7298
3 *
4 * Copyright 2010 Analog Devices Inc.
5 *
6 * Licensed under the GPL-2 or later.
7 */
8
9#include <linux/interrupt.h>
10#include <linux/gpio.h>
11#include <linux/workqueue.h>
12#include <linux/device.h>
13#include <linux/kernel.h>
14#include <linux/slab.h>
15#include <linux/sysfs.h>
16#include <linux/list.h>
17#include <linux/spi/spi.h>
18#include <linux/rtc.h>
19
20#include "../iio.h"
21#include "../sysfs.h"
22
23/*
24 * AD7298 command
25 */
26#define AD7298_PD 0x1
27#define AD7298_T_AVG_MASK 0x2
28#define AD7298_EXT_REF 0x4
29#define AD7298_T_SENSE_MASK 0x20
30#define AD7298_VOLTAGE_MASK 0x3fc0
31#define AD7298_VOLTAGE_OFFSET 0x6
32#define AD7298_VOLTAGE_LIMIT_COUNT 8
33#define AD7298_REPEAT 0x40
34#define AD7298_WRITE 0x80
35
36/*
37 * AD7298 value masks
38 */
39#define AD7298_CHANNEL_MASK 0xf000
40#define AD7298_VALUE_MASK 0xfff
41#define AD7298_T_VALUE_SIGN 0x400
42#define AD7298_T_VALUE_FLOAT_OFFSET 2
43#define AD7298_T_VALUE_FLOAT_MASK 0x2
44
45/*
46 * struct ad7298_chip_info - chip specifc information
47 */
48
49struct ad7298_chip_info {
50 const char *name;
51 struct spi_device *spi_dev;
52 struct iio_dev *indio_dev;
53 u16 command;
54 u16 busy_pin;
55 u8 channels; /* Active voltage channels */
56};
57
58/*
59 * ad7298 register access by SPI
60 */
61static int ad7298_spi_write(struct ad7298_chip_info *chip, u16 data)
62{
63 struct spi_device *spi_dev = chip->spi_dev;
64 int ret = 0;
65
66 data |= AD7298_WRITE;
67 data = cpu_to_be16(data);
68 ret = spi_write(spi_dev, (u8 *)&data, sizeof(data));
69 if (ret < 0)
70 dev_err(&spi_dev->dev, "SPI write error\n");
71
72 return ret;
73}
74
75static int ad7298_spi_read(struct ad7298_chip_info *chip, u16 mask, u16 *data)
76{
77 struct spi_device *spi_dev = chip->spi_dev;
78 int ret = 0;
79 u8 count = chip->channels;
80 u16 command;
81 int i;
82
83 if (mask & AD7298_T_SENSE_MASK) {
84 command = chip->command & ~(AD7298_T_AVG_MASK | AD7298_VOLTAGE_MASK);
85 command |= AD7298_T_SENSE_MASK;
86 count = 1;
87 } else if (mask & AD7298_T_AVG_MASK) {
88 command = chip->command & ~AD7298_VOLTAGE_MASK;
89 command |= AD7298_T_SENSE_MASK | AD7298_T_AVG_MASK;
90 count = 2;
91 } else if (mask & AD7298_VOLTAGE_MASK) {
92 command = chip->command & ~(AD7298_T_AVG_MASK | AD7298_T_SENSE_MASK);
93 count = chip->channels;
94 }
95
96 ret = ad7298_spi_write(chip, chip->command);
97 if (ret < 0) {
98 dev_err(&spi_dev->dev, "SPI write command error\n");
99 return ret;
100 }
101
102 ret = spi_read(spi_dev, (u8 *)&command, sizeof(command));
103 if (ret < 0) {
104 dev_err(&spi_dev->dev, "SPI read error\n");
105 return ret;
106 }
107
108 i = 10000;
109 while (i && gpio_get_value(chip->busy_pin)) {
110 cpu_relax();
111 i--;
112 }
113 if (!i) {
114 dev_err(&spi_dev->dev, "Always in busy convertion.\n");
115 return -EBUSY;
116 }
117
118 for (i = 0; i < count; i++) {
119 ret = spi_read(spi_dev, (u8 *)&data[i], sizeof(data[i]));
120 if (ret < 0) {
121 dev_err(&spi_dev->dev, "SPI read error\n");
122 return ret;
123 }
124 *data = be16_to_cpu(data[i]);
125 }
126
127 return 0;
128}
129
130static ssize_t ad7298_show_mode(struct device *dev,
131 struct device_attribute *attr,
132 char *buf)
133{
134 struct iio_dev *dev_info = dev_get_drvdata(dev);
135 struct ad7298_chip_info *chip = dev_info->dev_data;
136
137 if (chip->command & AD7298_REPEAT)
138 return sprintf(buf, "repeat\n");
139 else
140 return sprintf(buf, "normal\n");
141}
142
143static ssize_t ad7298_store_mode(struct device *dev,
144 struct device_attribute *attr,
145 const char *buf,
146 size_t len)
147{
148 struct iio_dev *dev_info = dev_get_drvdata(dev);
149 struct ad7298_chip_info *chip = dev_info->dev_data;
150
151 if (strcmp(buf, "repeat"))
152 chip->command |= AD7298_REPEAT;
153 else
154 chip->command &= (~AD7298_REPEAT);
155
156 return 1;
157}
158
159static IIO_DEVICE_ATTR(mode, S_IRUGO | S_IWUSR,
160 ad7298_show_mode,
161 ad7298_store_mode,
162 0);
163
164static ssize_t ad7298_show_available_modes(struct device *dev,
165 struct device_attribute *attr,
166 char *buf)
167{
168 return sprintf(buf, "normal\nrepeat\n");
169}
170
171static IIO_DEVICE_ATTR(available_modes, S_IRUGO, ad7298_show_available_modes, NULL, 0);
172
173static ssize_t ad7298_store_reset(struct device *dev,
174 struct device_attribute *attr,
175 const char *buf,
176 size_t len)
177{
178 struct iio_dev *dev_info = dev_get_drvdata(dev);
179 struct ad7298_chip_info *chip = dev_info->dev_data;
180 u16 command;
181 int ret;
182
183 command = chip->command & ~AD7298_PD;
184
185 ret = ad7298_spi_write(chip, command);
186 if (ret)
187 return -EIO;
188
189 command = chip->command | AD7298_PD;
190
191 ret = ad7298_spi_write(chip, command);
192 if (ret)
193 return -EIO;
194
195 return len;
196}
197
198static IIO_DEVICE_ATTR(reset, S_IWUSR,
199 NULL,
200 ad7298_store_reset,
201 0);
202
203static ssize_t ad7298_show_ext_ref(struct device *dev,
204 struct device_attribute *attr,
205 char *buf)
206{
207 struct iio_dev *dev_info = dev_get_drvdata(dev);
208 struct ad7298_chip_info *chip = dev_info->dev_data;
209
210 return sprintf(buf, "%d\n", !!(chip->command & AD7298_EXT_REF));
211}
212
213static ssize_t ad7298_store_ext_ref(struct device *dev,
214 struct device_attribute *attr,
215 const char *buf,
216 size_t len)
217{
218 struct iio_dev *dev_info = dev_get_drvdata(dev);
219 struct ad7298_chip_info *chip = dev_info->dev_data;
220 u16 command;
221 int ret;
222
223 command = chip->command & (~AD7298_EXT_REF);
224 if (strcmp(buf, "1"))
225 command |= AD7298_EXT_REF;
226
227 ret = ad7298_spi_write(chip, command);
228 if (ret)
229 return -EIO;
230
231 chip->command = command;
232
233 return len;
234}
235
236static IIO_DEVICE_ATTR(ext_ref, S_IRUGO | S_IWUSR,
237 ad7298_show_ext_ref,
238 ad7298_store_ext_ref,
239 0);
240
241static ssize_t ad7298_show_t_sense(struct device *dev,
242 struct device_attribute *attr,
243 char *buf)
244{
245 struct iio_dev *dev_info = dev_get_drvdata(dev);
246 struct ad7298_chip_info *chip = dev_info->dev_data;
247 u16 data;
248 char sign = ' ';
249 int ret;
250
251 ret = ad7298_spi_read(chip, AD7298_T_SENSE_MASK, &data);
252 if (ret)
253 return -EIO;
254
255 if (data & AD7298_T_VALUE_SIGN) {
256 /* convert supplement to positive value */
257 data = (AD7298_T_VALUE_SIGN << 1) - data;
258 sign = '-';
259 }
260
261 return sprintf(buf, "%c%d.%.2d\n", sign,
262 (data >> AD7298_T_VALUE_FLOAT_OFFSET),
263 (data & AD7298_T_VALUE_FLOAT_MASK) * 25);
264}
265
266static IIO_DEVICE_ATTR(t_sense, S_IRUGO, ad7298_show_t_sense, NULL, 0);
267
268static ssize_t ad7298_show_t_average(struct device *dev,
269 struct device_attribute *attr,
270 char *buf)
271{
272 struct iio_dev *dev_info = dev_get_drvdata(dev);
273 struct ad7298_chip_info *chip = dev_info->dev_data;
274 u16 data[2];
275 char sign = ' ';
276 int ret;
277
278 ret = ad7298_spi_read(chip, AD7298_T_AVG_MASK, data);
279 if (ret)
280 return -EIO;
281
282 if (data[1] & AD7298_T_VALUE_SIGN) {
283 /* convert supplement to positive value */
284 data[1] = (AD7298_T_VALUE_SIGN << 1) - data[1];
285 sign = '-';
286 }
287
288 return sprintf(buf, "%c%d.%.2d\n", sign,
289 (data[1] >> AD7298_T_VALUE_FLOAT_OFFSET),
290 (data[1] & AD7298_T_VALUE_FLOAT_MASK) * 25);
291}
292
293static IIO_DEVICE_ATTR(t_average, S_IRUGO, ad7298_show_t_average, NULL, 0);
294
295static ssize_t ad7298_show_voltage(struct device *dev,
296 struct device_attribute *attr,
297 char *buf)
298{
299 struct iio_dev *dev_info = dev_get_drvdata(dev);
300 struct ad7298_chip_info *chip = dev_info->dev_data;
301 u16 data[AD7298_VOLTAGE_LIMIT_COUNT];
302 int i, size, ret;
303
304 ret = ad7298_spi_read(chip, AD7298_VOLTAGE_MASK, data);
305 if (ret)
306 return -EIO;
307
308 for (i = 0; i < AD7298_VOLTAGE_LIMIT_COUNT; i++) {
309 if (chip->command & (AD7298_T_SENSE_MASK << i)) {
310 ret = sprintf(buf, "channel[%d]=%d\n", i,
311 data[i] & AD7298_VALUE_MASK);
312 if (ret < 0)
313 break;
314 buf += ret;
315 size += ret;
316 }
317 }
318
319 return size;
320}
321
322static IIO_DEVICE_ATTR(voltage, S_IRUGO, ad7298_show_voltage, NULL, 0);
323
324static ssize_t ad7298_show_channel_mask(struct device *dev,
325 struct device_attribute *attr,
326 char *buf)
327{
328 struct iio_dev *dev_info = dev_get_drvdata(dev);
329 struct ad7298_chip_info *chip = dev_info->dev_data;
330
331 return sprintf(buf, "0x%x\n", (chip->command & AD7298_VOLTAGE_MASK) >>
332 AD7298_VOLTAGE_OFFSET);
333}
334
335static ssize_t ad7298_store_channel_mask(struct device *dev,
336 struct device_attribute *attr,
337 const char *buf,
338 size_t len)
339{
340 struct iio_dev *dev_info = dev_get_drvdata(dev);
341 struct ad7298_chip_info *chip = dev_info->dev_data;
342 unsigned long data;
343 int i, ret;
344
345 ret = strict_strtoul(buf, 16, &data);
346 if (ret || data > 0xff)
347 return -EINVAL;
348
349 chip->command &= (~AD7298_VOLTAGE_MASK);
350 chip->command |= data << AD7298_VOLTAGE_OFFSET;
351
352 for (i = 0, chip->channels = 0; i < AD7298_VOLTAGE_LIMIT_COUNT; i++) {
353 if (chip->command & (AD7298_T_SENSE_MASK << i))
354 chip->channels++;
355 }
356
357 return ret;
358}
359
360static IIO_DEVICE_ATTR(channel_mask, S_IRUGO | S_IWUSR,
361 ad7298_show_channel_mask,
362 ad7298_store_channel_mask,
363 0);
364
365static ssize_t ad7298_show_name(struct device *dev,
366 struct device_attribute *attr,
367 char *buf)
368{
369 struct iio_dev *dev_info = dev_get_drvdata(dev);
370 struct ad7298_chip_info *chip = dev_info->dev_data;
371 return sprintf(buf, "%s\n", chip->name);
372}
373
374static IIO_DEVICE_ATTR(name, S_IRUGO, ad7298_show_name, NULL, 0);
375
376static struct attribute *ad7298_attributes[] = {
377 &iio_dev_attr_available_modes.dev_attr.attr,
378 &iio_dev_attr_mode.dev_attr.attr,
379 &iio_dev_attr_reset.dev_attr.attr,
380 &iio_dev_attr_ext_ref.dev_attr.attr,
381 &iio_dev_attr_t_sense.dev_attr.attr,
382 &iio_dev_attr_t_average.dev_attr.attr,
383 &iio_dev_attr_voltage.dev_attr.attr,
384 &iio_dev_attr_channel_mask.dev_attr.attr,
385 &iio_dev_attr_name.dev_attr.attr,
386 NULL,
387};
388
389static const struct attribute_group ad7298_attribute_group = {
390 .attrs = ad7298_attributes,
391};
392
393/*
394 * device probe and remove
395 */
396static int __devinit ad7298_probe(struct spi_device *spi_dev)
397{
398 struct ad7298_chip_info *chip;
399 unsigned short *pins = spi_dev->dev.platform_data;
400 int ret = 0;
401
402 chip = kzalloc(sizeof(struct ad7298_chip_info), GFP_KERNEL);
403
404 if (chip == NULL)
405 return -ENOMEM;
406
407 /* this is only used for device removal purposes */
408 dev_set_drvdata(&spi_dev->dev, chip);
409
410 chip->spi_dev = spi_dev;
411 chip->name = spi_dev->modalias;
412 chip->busy_pin = pins[0];
413
414 ret = gpio_request(chip->busy_pin, chip->name);
415 if (ret) {
416 dev_err(&spi_dev->dev, "Fail to request busy gpio PIN %d.\n",
417 chip->busy_pin);
418 goto error_free_chip;
419 }
420 gpio_direction_input(chip->busy_pin);
421
422 chip->indio_dev = iio_allocate_device();
423 if (chip->indio_dev == NULL) {
424 ret = -ENOMEM;
425 goto error_free_gpio;
426 }
427
428 chip->indio_dev->dev.parent = &spi_dev->dev;
429 chip->indio_dev->attrs = &ad7298_attribute_group;
430 chip->indio_dev->dev_data = (void *)chip;
431 chip->indio_dev->driver_module = THIS_MODULE;
432 chip->indio_dev->modes = INDIO_DIRECT_MODE;
433
434 ret = iio_device_register(chip->indio_dev);
435 if (ret)
436 goto error_free_dev;
437
438 dev_info(&spi_dev->dev, "%s temperature sensor and ADC registered.\n",
439 chip->name);
440
441 return 0;
442
443error_free_dev:
444 iio_free_device(chip->indio_dev);
445error_free_gpio:
446 gpio_free(chip->busy_pin);
447error_free_chip:
448 kfree(chip);
449
450 return ret;
451}
452
453static int __devexit ad7298_remove(struct spi_device *spi_dev)
454{
455 struct ad7298_chip_info *chip = dev_get_drvdata(&spi_dev->dev);
456 struct iio_dev *indio_dev = chip->indio_dev;
457
458 dev_set_drvdata(&spi_dev->dev, NULL);
459 iio_device_unregister(indio_dev);
460 iio_free_device(chip->indio_dev);
461 gpio_free(chip->busy_pin);
462 kfree(chip);
463
464 return 0;
465}
466
467static const struct spi_device_id ad7298_id[] = {
468 { "ad7298", 0 },
469 {}
470};
471
472MODULE_DEVICE_TABLE(spi, ad7298_id);
473
474static struct spi_driver ad7298_driver = {
475 .driver = {
476 .name = "ad7298",
477 .bus = &spi_bus_type,
478 .owner = THIS_MODULE,
479 },
480 .probe = ad7298_probe,
481 .remove = __devexit_p(ad7298_remove),
482 .id_table = ad7298_id,
483};
484
485static __init int ad7298_init(void)
486{
487 return spi_register_driver(&ad7298_driver);
488}
489
490static __exit void ad7298_exit(void)
491{
492 spi_unregister_driver(&ad7298_driver);
493}
494
495MODULE_AUTHOR("Sonic Zhang <sonic.zhang@analog.com>");
496MODULE_DESCRIPTION("Analog Devices AD7298 digital"
497 " temperature sensor and ADC driver");
498MODULE_LICENSE("GPL v2");
499
500module_init(ad7298_init);
501module_exit(ad7298_exit);
diff --git a/drivers/staging/iio/adc/ad7298.h b/drivers/staging/iio/adc/ad7298.h
new file mode 100644
index 00000000000..fe7ed77d638
--- /dev/null
+++ b/drivers/staging/iio/adc/ad7298.h
@@ -0,0 +1,80 @@
1/*
2 * AD7298 SPI ADC driver
3 *
4 * Copyright 2011 Analog Devices Inc.
5 *
6 * Licensed under the GPL-2.
7 */
8
9#ifndef IIO_ADC_AD7298_H_
10#define IIO_ADC_AD7298_H_
11
12#define AD7298_WRITE (1 << 15) /* write to the control register */
13#define AD7298_REPEAT (1 << 14) /* repeated conversion enable */
14#define AD7298_CH(x) (1 << (13 - (x))) /* channel select */
15#define AD7298_TSENSE (1 << 5) /* temperature conversion enable */
16#define AD7298_EXTREF (1 << 2) /* external reference enable */
17#define AD7298_TAVG (1 << 1) /* temperature sensor averaging enable */
18#define AD7298_PDD (1 << 0) /* partial power down enable */
19
20#define AD7298_CH_MASK (AD7298_CH0 | AD7298_CH1 | AD7298_CH2 | AD7298_CH3 | \
21 AD7298_CH4 | AD7298_CH5 | AD7298_CH6 | AD7298_CH7)
22
23#define AD7298_MAX_CHAN 8
24#define AD7298_BITS 12
25#define AD7298_STORAGE_BITS 16
26#define AD7298_INTREF_mV 2500
27
28#define RES_MASK(bits) ((1 << (bits)) - 1)
29
30/*
31 * TODO: struct ad7298_platform_data needs to go into include/linux/iio
32 */
33
34struct ad7298_platform_data {
35 /* External Vref voltage applied */
36 u16 vref_mv;
37};
38
39struct ad7298_state {
40 struct iio_dev *indio_dev;
41 struct spi_device *spi;
42 struct regulator *reg;
43 struct work_struct poll_work;
44 atomic_t protect_ring;
45 size_t d_size;
46 u16 int_vref_mv;
47 unsigned ext_ref;
48 struct spi_transfer ring_xfer[10];
49 struct spi_transfer scan_single_xfer[3];
50 struct spi_message ring_msg;
51 struct spi_message scan_single_msg;
52 /*
53 * DMA (thus cache coherency maintenance) requires the
54 * transfer buffers to live in their own cache lines.
55 */
56 unsigned short rx_buf[8] ____cacheline_aligned;
57 unsigned short tx_buf[2];
58};
59
60#ifdef CONFIG_IIO_RING_BUFFER
61int ad7298_scan_from_ring(struct ad7298_state *st, long ch);
62int ad7298_register_ring_funcs_and_init(struct iio_dev *indio_dev);
63void ad7298_ring_cleanup(struct iio_dev *indio_dev);
64#else /* CONFIG_IIO_RING_BUFFER */
65static inline int ad7298_scan_from_ring(struct ad7298_state *st, long ch)
66{
67 return 0;
68}
69
70static inline int
71ad7298_register_ring_funcs_and_init(struct iio_dev *indio_dev)
72{
73 return 0;
74}
75
76static inline void ad7298_ring_cleanup(struct iio_dev *indio_dev)
77{
78}
79#endif /* CONFIG_IIO_RING_BUFFER */
80#endif /* IIO_ADC_AD7298_H_ */
diff --git a/drivers/staging/iio/adc/ad7298_core.c b/drivers/staging/iio/adc/ad7298_core.c
new file mode 100644
index 00000000000..2e9154e7d88
--- /dev/null
+++ b/drivers/staging/iio/adc/ad7298_core.c
@@ -0,0 +1,287 @@
1/*
2 * AD7298 SPI ADC driver
3 *
4 * Copyright 2011 Analog Devices Inc.
5 *
6 * Licensed under the GPL-2.
7 */
8
9#include <linux/workqueue.h>
10#include <linux/device.h>
11#include <linux/kernel.h>
12#include <linux/slab.h>
13#include <linux/sysfs.h>
14#include <linux/spi/spi.h>
15#include <linux/regulator/consumer.h>
16#include <linux/err.h>
17#include <linux/delay.h>
18
19#include "../iio.h"
20#include "../sysfs.h"
21#include "../ring_generic.h"
22#include "adc.h"
23
24#include "ad7298.h"
25
26static int ad7298_scan_direct(struct ad7298_state *st, unsigned ch)
27{
28 int ret;
29 st->tx_buf[0] = cpu_to_be16(AD7298_WRITE | st->ext_ref |
30 (AD7298_CH(0) >> ch));
31
32 ret = spi_sync(st->spi, &st->scan_single_msg);
33 if (ret)
34 return ret;
35
36 return be16_to_cpu(st->rx_buf[0]);
37}
38
39static ssize_t ad7298_scan(struct device *dev,
40 struct device_attribute *attr,
41 char *buf)
42{
43 struct iio_dev *dev_info = dev_get_drvdata(dev);
44 struct ad7298_state *st = dev_info->dev_data;
45 struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
46 int ret;
47
48 mutex_lock(&dev_info->mlock);
49 if (iio_ring_enabled(dev_info))
50 ret = ad7298_scan_from_ring(st, this_attr->address);
51 else
52 ret = ad7298_scan_direct(st, this_attr->address);
53 mutex_unlock(&dev_info->mlock);
54
55 if (ret < 0)
56 return ret;
57
58 return sprintf(buf, "%d\n", ret & RES_MASK(AD7298_BITS));
59}
60
61static IIO_DEV_ATTR_IN_RAW(0, ad7298_scan, 0);
62static IIO_DEV_ATTR_IN_RAW(1, ad7298_scan, 1);
63static IIO_DEV_ATTR_IN_RAW(2, ad7298_scan, 2);
64static IIO_DEV_ATTR_IN_RAW(3, ad7298_scan, 3);
65static IIO_DEV_ATTR_IN_RAW(4, ad7298_scan, 4);
66static IIO_DEV_ATTR_IN_RAW(5, ad7298_scan, 5);
67static IIO_DEV_ATTR_IN_RAW(6, ad7298_scan, 6);
68static IIO_DEV_ATTR_IN_RAW(7, ad7298_scan, 7);
69
70static ssize_t ad7298_show_temp(struct device *dev,
71 struct device_attribute *attr,
72 char *buf)
73{
74 struct iio_dev *dev_info = dev_get_drvdata(dev);
75 struct ad7298_state *st = iio_dev_get_devdata(dev_info);
76 int tmp;
77
78 tmp = cpu_to_be16(AD7298_WRITE | AD7298_TSENSE |
79 AD7298_TAVG | st->ext_ref);
80
81 mutex_lock(&dev_info->mlock);
82 spi_write(st->spi, (u8 *)&tmp, 2);
83 tmp = 0;
84 spi_write(st->spi, (u8 *)&tmp, 2);
85 usleep_range(101, 1000); /* sleep > 100us */
86 spi_read(st->spi, (u8 *)&tmp, 2);
87 mutex_unlock(&dev_info->mlock);
88
89 tmp = be16_to_cpu(tmp) & RES_MASK(AD7298_BITS);
90
91 /*
92 * One LSB of the ADC corresponds to 0.25 deg C.
93 * The temperature reading is in 12-bit twos complement format
94 */
95
96 if (tmp & (1 << (AD7298_BITS - 1))) {
97 tmp = (4096 - tmp) * 250;
98 tmp -= (2 * tmp);
99
100 } else {
101 tmp *= 250; /* temperature in milli degrees Celsius */
102 }
103
104 return sprintf(buf, "%d\n", tmp);
105}
106
107static IIO_DEVICE_ATTR(temp0_input, S_IRUGO, ad7298_show_temp, NULL, 0);
108
109static ssize_t ad7298_show_scale(struct device *dev,
110 struct device_attribute *attr,
111 char *buf)
112{
113 struct iio_dev *dev_info = dev_get_drvdata(dev);
114 struct ad7298_state *st = iio_dev_get_devdata(dev_info);
115 /* Corresponds to Vref / 2^(bits) */
116 unsigned int scale_uv = (st->int_vref_mv * 1000) >> AD7298_BITS;
117
118 return sprintf(buf, "%d.%03d\n", scale_uv / 1000, scale_uv % 1000);
119}
120static IIO_DEVICE_ATTR(in_scale, S_IRUGO, ad7298_show_scale, NULL, 0);
121
122static ssize_t ad7298_show_name(struct device *dev,
123 struct device_attribute *attr,
124 char *buf)
125{
126 struct iio_dev *dev_info = dev_get_drvdata(dev);
127 struct ad7298_state *st = iio_dev_get_devdata(dev_info);
128
129 return sprintf(buf, "%s\n", spi_get_device_id(st->spi)->name);
130}
131static IIO_DEVICE_ATTR(name, S_IRUGO, ad7298_show_name, NULL, 0);
132
133static struct attribute *ad7298_attributes[] = {
134 &iio_dev_attr_in0_raw.dev_attr.attr,
135 &iio_dev_attr_in1_raw.dev_attr.attr,
136 &iio_dev_attr_in2_raw.dev_attr.attr,
137 &iio_dev_attr_in3_raw.dev_attr.attr,
138 &iio_dev_attr_in4_raw.dev_attr.attr,
139 &iio_dev_attr_in5_raw.dev_attr.attr,
140 &iio_dev_attr_in6_raw.dev_attr.attr,
141 &iio_dev_attr_in7_raw.dev_attr.attr,
142 &iio_dev_attr_in_scale.dev_attr.attr,
143 &iio_dev_attr_temp0_input.dev_attr.attr,
144 &iio_dev_attr_name.dev_attr.attr,
145 NULL,
146};
147
148static const struct attribute_group ad7298_attribute_group = {
149 .attrs = ad7298_attributes,
150};
151
152static int __devinit ad7298_probe(struct spi_device *spi)
153{
154 struct ad7298_platform_data *pdata = spi->dev.platform_data;
155 struct ad7298_state *st;
156 int ret;
157
158 st = kzalloc(sizeof(*st), GFP_KERNEL);
159 if (st == NULL) {
160 ret = -ENOMEM;
161 goto error_ret;
162 }
163
164 st->reg = regulator_get(&spi->dev, "vcc");
165 if (!IS_ERR(st->reg)) {
166 ret = regulator_enable(st->reg);
167 if (ret)
168 goto error_put_reg;
169 }
170
171 spi_set_drvdata(spi, st);
172
173 atomic_set(&st->protect_ring, 0);
174 st->spi = spi;
175
176 st->indio_dev = iio_allocate_device();
177 if (st->indio_dev == NULL) {
178 ret = -ENOMEM;
179 goto error_disable_reg;
180 }
181
182 st->indio_dev->dev.parent = &spi->dev;
183 st->indio_dev->attrs = &ad7298_attribute_group;
184 st->indio_dev->dev_data = (void *)(st);
185 st->indio_dev->driver_module = THIS_MODULE;
186 st->indio_dev->modes = INDIO_DIRECT_MODE;
187
188 /* Setup default message */
189
190 st->scan_single_xfer[0].tx_buf = &st->tx_buf[0];
191 st->scan_single_xfer[0].len = 2;
192 st->scan_single_xfer[0].cs_change = 1;
193 st->scan_single_xfer[1].tx_buf = &st->tx_buf[1];
194 st->scan_single_xfer[1].len = 2;
195 st->scan_single_xfer[1].cs_change = 1;
196 st->scan_single_xfer[2].rx_buf = &st->rx_buf[0];
197 st->scan_single_xfer[2].len = 2;
198
199 spi_message_init(&st->scan_single_msg);
200 spi_message_add_tail(&st->scan_single_xfer[0], &st->scan_single_msg);
201 spi_message_add_tail(&st->scan_single_xfer[1], &st->scan_single_msg);
202 spi_message_add_tail(&st->scan_single_xfer[2], &st->scan_single_msg);
203
204 if (pdata && pdata->vref_mv) {
205 st->int_vref_mv = pdata->vref_mv;
206 st->ext_ref = AD7298_EXTREF;
207 } else {
208 st->int_vref_mv = AD7298_INTREF_mV;
209 }
210
211 ret = ad7298_register_ring_funcs_and_init(st->indio_dev);
212 if (ret)
213 goto error_free_device;
214
215 ret = iio_device_register(st->indio_dev);
216 if (ret)
217 goto error_free_device;
218
219 ret = iio_ring_buffer_register(st->indio_dev->ring, 0);
220 if (ret)
221 goto error_cleanup_ring;
222 return 0;
223
224error_cleanup_ring:
225 ad7298_ring_cleanup(st->indio_dev);
226 iio_device_unregister(st->indio_dev);
227error_free_device:
228 iio_free_device(st->indio_dev);
229error_disable_reg:
230 if (!IS_ERR(st->reg))
231 regulator_disable(st->reg);
232error_put_reg:
233 if (!IS_ERR(st->reg))
234 regulator_put(st->reg);
235 kfree(st);
236error_ret:
237 return ret;
238}
239
240static int __devexit ad7298_remove(struct spi_device *spi)
241{
242 struct ad7298_state *st = spi_get_drvdata(spi);
243 struct iio_dev *indio_dev = st->indio_dev;
244
245 iio_ring_buffer_unregister(indio_dev->ring);
246 ad7298_ring_cleanup(indio_dev);
247 iio_device_unregister(indio_dev);
248 if (!IS_ERR(st->reg)) {
249 regulator_disable(st->reg);
250 regulator_put(st->reg);
251 }
252 kfree(st);
253 return 0;
254}
255
256static const struct spi_device_id ad7298_id[] = {
257 {"ad7298", 0},
258 {}
259};
260
261static struct spi_driver ad7298_driver = {
262 .driver = {
263 .name = "ad7298",
264 .bus = &spi_bus_type,
265 .owner = THIS_MODULE,
266 },
267 .probe = ad7298_probe,
268 .remove = __devexit_p(ad7298_remove),
269 .id_table = ad7298_id,
270};
271
272static int __init ad7298_init(void)
273{
274 return spi_register_driver(&ad7298_driver);
275}
276module_init(ad7298_init);
277
278static void __exit ad7298_exit(void)
279{
280 spi_unregister_driver(&ad7298_driver);
281}
282module_exit(ad7298_exit);
283
284MODULE_AUTHOR("Michael Hennerich <hennerich@blackfin.uclinux.org>");
285MODULE_DESCRIPTION("Analog Devices AD7298 ADC");
286MODULE_LICENSE("GPL v2");
287MODULE_ALIAS("spi:ad7298");
diff --git a/drivers/staging/iio/adc/ad7298_ring.c b/drivers/staging/iio/adc/ad7298_ring.c
new file mode 100644
index 00000000000..19d1aced1e6
--- /dev/null
+++ b/drivers/staging/iio/adc/ad7298_ring.c
@@ -0,0 +1,258 @@
1/*
2 * AD7298 SPI ADC driver
3 *
4 * Copyright 2011 Analog Devices Inc.
5 *
6 * Licensed under the GPL-2.
7 */
8
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/spi/spi.h>
16
17#include "../iio.h"
18#include "../ring_generic.h"
19#include "../ring_sw.h"
20#include "../trigger.h"
21#include "../sysfs.h"
22
23#include "ad7298.h"
24
25static IIO_SCAN_EL_C(in0, 0, 0, NULL);
26static IIO_SCAN_EL_C(in1, 1, 0, NULL);
27static IIO_SCAN_EL_C(in2, 2, 0, NULL);
28static IIO_SCAN_EL_C(in3, 3, 0, NULL);
29static IIO_SCAN_EL_C(in4, 4, 0, NULL);
30static IIO_SCAN_EL_C(in5, 5, 0, NULL);
31static IIO_SCAN_EL_C(in6, 6, 0, NULL);
32static IIO_SCAN_EL_C(in7, 7, 0, NULL);
33
34static IIO_SCAN_EL_TIMESTAMP(8);
35static IIO_CONST_ATTR_SCAN_EL_TYPE(timestamp, s, 64, 64);
36
37static IIO_CONST_ATTR(in_type, "u12/16") ;
38
39static struct attribute *ad7298_scan_el_attrs[] = {
40 &iio_scan_el_in0.dev_attr.attr,
41 &iio_const_attr_in0_index.dev_attr.attr,
42 &iio_scan_el_in1.dev_attr.attr,
43 &iio_const_attr_in1_index.dev_attr.attr,
44 &iio_scan_el_in2.dev_attr.attr,
45 &iio_const_attr_in2_index.dev_attr.attr,
46 &iio_scan_el_in3.dev_attr.attr,
47 &iio_const_attr_in3_index.dev_attr.attr,
48 &iio_scan_el_in4.dev_attr.attr,
49 &iio_const_attr_in4_index.dev_attr.attr,
50 &iio_scan_el_in5.dev_attr.attr,
51 &iio_const_attr_in5_index.dev_attr.attr,
52 &iio_scan_el_in6.dev_attr.attr,
53 &iio_const_attr_in6_index.dev_attr.attr,
54 &iio_scan_el_in7.dev_attr.attr,
55 &iio_const_attr_in7_index.dev_attr.attr,
56 &iio_const_attr_timestamp_index.dev_attr.attr,
57 &iio_scan_el_timestamp.dev_attr.attr,
58 &iio_const_attr_timestamp_type.dev_attr.attr,
59 &iio_const_attr_in_type.dev_attr.attr,
60 NULL,
61};
62
63static struct attribute_group ad7298_scan_el_group = {
64 .name = "scan_elements",
65 .attrs = ad7298_scan_el_attrs,
66};
67
68int ad7298_scan_from_ring(struct ad7298_state *st, long ch)
69{
70 struct iio_ring_buffer *ring = st->indio_dev->ring;
71 int ret;
72 u16 *ring_data;
73
74 if (!(ring->scan_mask & (1 << ch))) {
75 ret = -EBUSY;
76 goto error_ret;
77 }
78
79 ring_data = kmalloc(ring->access.get_bytes_per_datum(ring), GFP_KERNEL);
80 if (ring_data == NULL) {
81 ret = -ENOMEM;
82 goto error_ret;
83 }
84 ret = ring->access.read_last(ring, (u8 *) ring_data);
85 if (ret)
86 goto error_free_ring_data;
87
88 ret = be16_to_cpu(ring_data[ch]);
89
90error_free_ring_data:
91 kfree(ring_data);
92error_ret:
93 return ret;
94}
95
96/**
97 * ad7298_ring_preenable() setup the parameters of the ring before enabling
98 *
99 * The complex nature of the setting of the number of bytes per datum is due
100 * to this driver currently ensuring that the timestamp is stored at an 8
101 * byte boundary.
102 **/
103static int ad7298_ring_preenable(struct iio_dev *indio_dev)
104{
105 struct ad7298_state *st = indio_dev->dev_data;
106 struct iio_ring_buffer *ring = indio_dev->ring;
107 size_t d_size;
108 int i, m;
109 unsigned short command;
110
111 d_size = ring->scan_count * (AD7298_STORAGE_BITS / 8);
112
113 if (ring->scan_timestamp) {
114 d_size += sizeof(s64);
115
116 if (d_size % sizeof(s64))
117 d_size += sizeof(s64) - (d_size % sizeof(s64));
118 }
119
120 if (ring->access.set_bytes_per_datum)
121 ring->access.set_bytes_per_datum(ring, d_size);
122
123 st->d_size = d_size;
124
125 command = AD7298_WRITE | st->ext_ref;
126
127 for (i = 0, m = AD7298_CH(0); i < AD7298_MAX_CHAN; i++, m >>= 1)
128 if (ring->scan_mask & (1 << i))
129 command |= m;
130
131 st->tx_buf[0] = cpu_to_be16(command);
132
133 /* build spi ring message */
134 st->ring_xfer[0].tx_buf = &st->tx_buf[0];
135 st->ring_xfer[0].len = 2;
136 st->ring_xfer[0].cs_change = 1;
137 st->ring_xfer[1].tx_buf = &st->tx_buf[1];
138 st->ring_xfer[1].len = 2;
139 st->ring_xfer[1].cs_change = 1;
140
141 spi_message_init(&st->ring_msg);
142 spi_message_add_tail(&st->ring_xfer[0], &st->ring_msg);
143 spi_message_add_tail(&st->ring_xfer[1], &st->ring_msg);
144
145 for (i = 0; i < ring->scan_count; i++) {
146 st->ring_xfer[i + 2].rx_buf = &st->rx_buf[i];
147 st->ring_xfer[i + 2].len = 2;
148 st->ring_xfer[i + 2].cs_change = 1;
149 spi_message_add_tail(&st->ring_xfer[i + 2], &st->ring_msg);
150 }
151 /* make sure last transfer cs_change is not set */
152 st->ring_xfer[i + 1].cs_change = 0;
153
154 return 0;
155}
156
157/**
158 * ad7298_poll_func_th() th of trigger launched polling to ring buffer
159 *
160 * As sampling only occurs on spi comms occuring, leave timestamping until
161 * then. Some triggers will generate their own time stamp. Currently
162 * there is no way of notifying them when no one cares.
163 **/
164static void ad7298_poll_func_th(struct iio_dev *indio_dev, s64 time)
165{
166 struct ad7298_state *st = indio_dev->dev_data;
167
168 schedule_work(&st->poll_work);
169 return;
170}
171
172/**
173 * ad7298_poll_bh_to_ring() bh of trigger launched polling to ring buffer
174 * @work_s: the work struct through which this was scheduled
175 *
176 * Currently there is no option in this driver to disable the saving of
177 * timestamps within the ring.
178 * I think the one copy of this at a time was to avoid problems if the
179 * trigger was set far too high and the reads then locked up the computer.
180 **/
181static void ad7298_poll_bh_to_ring(struct work_struct *work_s)
182{
183 struct ad7298_state *st = container_of(work_s, struct ad7298_state,
184 poll_work);
185 struct iio_dev *indio_dev = st->indio_dev;
186 struct iio_sw_ring_buffer *sw_ring = iio_to_sw_ring(indio_dev->ring);
187 struct iio_ring_buffer *ring = indio_dev->ring;
188 s64 time_ns;
189 __u16 buf[16];
190 int b_sent, i;
191
192 /* Ensure only one copy of this function running at a time */
193 if (atomic_inc_return(&st->protect_ring) > 1)
194 return;
195
196 b_sent = spi_sync(st->spi, &st->ring_msg);
197 if (b_sent)
198 goto done;
199
200 if (ring->scan_timestamp) {
201 time_ns = iio_get_time_ns();
202 memcpy((u8 *)buf + st->d_size - sizeof(s64),
203 &time_ns, sizeof(time_ns));
204 }
205
206 for (i = 0; i < ring->scan_count; i++)
207 buf[i] = be16_to_cpu(st->rx_buf[i]);
208
209 indio_dev->ring->access.store_to(&sw_ring->buf, (u8 *)buf, time_ns);
210done:
211 atomic_dec(&st->protect_ring);
212}
213
214int ad7298_register_ring_funcs_and_init(struct iio_dev *indio_dev)
215{
216 struct ad7298_state *st = indio_dev->dev_data;
217 int ret;
218
219 indio_dev->ring = iio_sw_rb_allocate(indio_dev);
220 if (!indio_dev->ring) {
221 ret = -ENOMEM;
222 goto error_ret;
223 }
224 /* Effectively select the ring buffer implementation */
225 iio_ring_sw_register_funcs(&indio_dev->ring->access);
226 ret = iio_alloc_pollfunc(indio_dev, NULL, &ad7298_poll_func_th);
227 if (ret)
228 goto error_deallocate_sw_rb;
229
230 /* Ring buffer functions - here trigger setup related */
231
232 indio_dev->ring->preenable = &ad7298_ring_preenable;
233 indio_dev->ring->postenable = &iio_triggered_ring_postenable;
234 indio_dev->ring->predisable = &iio_triggered_ring_predisable;
235 indio_dev->ring->scan_el_attrs = &ad7298_scan_el_group;
236 indio_dev->ring->scan_timestamp = true;
237
238 INIT_WORK(&st->poll_work, &ad7298_poll_bh_to_ring);
239
240 /* Flag that polled ring buffering is possible */
241 indio_dev->modes |= INDIO_RING_TRIGGERED;
242 return 0;
243error_deallocate_sw_rb:
244 iio_sw_rb_free(indio_dev->ring);
245error_ret:
246 return ret;
247}
248
249void ad7298_ring_cleanup(struct iio_dev *indio_dev)
250{
251 if (indio_dev->trig) {
252 iio_put_trigger(indio_dev->trig);
253 iio_trigger_dettach_poll_func(indio_dev->trig,
254 indio_dev->pollfunc);
255 }
256 kfree(indio_dev->pollfunc);
257 iio_sw_rb_free(indio_dev->ring);
258}