diff options
author | Christophe Leroy <christophe.leroy@c-s.fr> | 2013-02-13 01:47:00 -0500 |
---|---|---|
committer | Jonathan Cameron <jic23@kernel.org> | 2013-03-16 06:18:05 -0400 |
commit | 0eac259db28fde88767cab5fd0380f4024e08c02 (patch) | |
tree | 5387c8e29634682f74063c3755d0c29102a6a20a | |
parent | 17d82b47a215ded05ee3fb8d93b7c1269dbe0083 (diff) |
IIO ADC support for AD7923
This patch adds support for Analog Devices AD7923 ADC in the IIO Subsystem.
Signed-off-by: Patrick Vasseur <patrick.vasseur@c-s.fr>
Signed-off-by: Christophe Leroy <christophe.leroy@c-s.fr>
Reviewed-by: Lars-Peter Clausen <lars@metafoo.de>
Signed-off-by: Jonathan Cameron <jic23@kernel.org>
-rw-r--r-- | drivers/iio/adc/Kconfig | 12 | ||||
-rw-r--r-- | drivers/iio/adc/Makefile | 1 | ||||
-rw-r--r-- | drivers/iio/adc/ad7923.c | 298 |
3 files changed, 311 insertions, 0 deletions
diff --git a/drivers/iio/adc/Kconfig b/drivers/iio/adc/Kconfig index e372257a8494..00213eaf8aa9 100644 --- a/drivers/iio/adc/Kconfig +++ b/drivers/iio/adc/Kconfig | |||
@@ -30,6 +30,18 @@ config AD7298 | |||
30 | To compile this driver as a module, choose M here: the | 30 | To compile this driver as a module, choose M here: the |
31 | module will be called ad7298. | 31 | module will be called ad7298. |
32 | 32 | ||
33 | config AD7923 | ||
34 | tristate "Analog Devices AD7923 ADC driver" | ||
35 | depends on SPI | ||
36 | select IIO_BUFFER | ||
37 | select IIO_TRIGGERED_BUFFER | ||
38 | help | ||
39 | Say yes here to build support for Analog Devices AD7923 | ||
40 | 4 Channel ADC with temperature sensor. | ||
41 | |||
42 | To compile this driver as a module, choose M here: the | ||
43 | module will be called ad7923. | ||
44 | |||
33 | config AD7791 | 45 | config AD7791 |
34 | tristate "Analog Devices AD7791 ADC driver" | 46 | tristate "Analog Devices AD7791 ADC driver" |
35 | depends on SPI | 47 | depends on SPI |
diff --git a/drivers/iio/adc/Makefile b/drivers/iio/adc/Makefile index 2d5f10080d8d..ab910ba4664c 100644 --- a/drivers/iio/adc/Makefile +++ b/drivers/iio/adc/Makefile | |||
@@ -5,6 +5,7 @@ | |||
5 | obj-$(CONFIG_AD_SIGMA_DELTA) += ad_sigma_delta.o | 5 | obj-$(CONFIG_AD_SIGMA_DELTA) += ad_sigma_delta.o |
6 | obj-$(CONFIG_AD7266) += ad7266.o | 6 | obj-$(CONFIG_AD7266) += ad7266.o |
7 | obj-$(CONFIG_AD7298) += ad7298.o | 7 | obj-$(CONFIG_AD7298) += ad7298.o |
8 | obj-$(CONFIG_AD7923) += ad7923.o | ||
8 | obj-$(CONFIG_AD7476) += ad7476.o | 9 | obj-$(CONFIG_AD7476) += ad7476.o |
9 | obj-$(CONFIG_AD7791) += ad7791.o | 10 | obj-$(CONFIG_AD7791) += ad7791.o |
10 | obj-$(CONFIG_AD7793) += ad7793.o | 11 | obj-$(CONFIG_AD7793) += ad7793.o |
diff --git a/drivers/iio/adc/ad7923.c b/drivers/iio/adc/ad7923.c new file mode 100644 index 000000000000..28b2bda8f0ec --- /dev/null +++ b/drivers/iio/adc/ad7923.c | |||
@@ -0,0 +1,298 @@ | |||
1 | /* | ||
2 | * AD7923 SPI ADC driver | ||
3 | * | ||
4 | * Copyright 2011 Analog Devices Inc (from AD7923 Driver) | ||
5 | * Copyright 2012 CS Systemes d'Information | ||
6 | * | ||
7 | * Licensed under the GPL-2. | ||
8 | */ | ||
9 | |||
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/err.h> | ||
16 | #include <linux/delay.h> | ||
17 | #include <linux/module.h> | ||
18 | #include <linux/interrupt.h> | ||
19 | |||
20 | #include <linux/iio/iio.h> | ||
21 | #include <linux/iio/sysfs.h> | ||
22 | #include <linux/iio/buffer.h> | ||
23 | #include <linux/iio/trigger_consumer.h> | ||
24 | #include <linux/iio/triggered_buffer.h> | ||
25 | |||
26 | #define AD7923_WRITE_CR (1 << 11) /* write control register */ | ||
27 | #define AD7923_RANGE (1 << 1) /* range to REFin */ | ||
28 | #define AD7923_CODING (1 << 0) /* coding is straight binary */ | ||
29 | #define AD7923_PM_MODE_AS (1) /* auto shutdown */ | ||
30 | #define AD7923_PM_MODE_FS (2) /* full shutdown */ | ||
31 | #define AD7923_PM_MODE_OPS (3) /* normal operation */ | ||
32 | #define AD7923_CHANNEL_0 (0) /* analog input 0 */ | ||
33 | #define AD7923_CHANNEL_1 (1) /* analog input 1 */ | ||
34 | #define AD7923_CHANNEL_2 (2) /* analog input 2 */ | ||
35 | #define AD7923_CHANNEL_3 (3) /* analog input 3 */ | ||
36 | #define AD7923_SEQUENCE_OFF (0) /* no sequence fonction */ | ||
37 | #define AD7923_SEQUENCE_PROTECT (2) /* no interrupt write cycle */ | ||
38 | #define AD7923_SEQUENCE_ON (3) /* continuous sequence */ | ||
39 | |||
40 | #define AD7923_MAX_CHAN 4 | ||
41 | |||
42 | #define AD7923_PM_MODE_WRITE(mode) (mode << 4) /* write mode */ | ||
43 | #define AD7923_CHANNEL_WRITE(channel) (channel << 6) /* write channel */ | ||
44 | #define AD7923_SEQUENCE_WRITE(sequence) (((sequence & 1) << 3) \ | ||
45 | + ((sequence & 2) << 9)) | ||
46 | /* write sequence fonction */ | ||
47 | /* left shift for CR : bit 11 transmit in first */ | ||
48 | #define AD7923_SHIFT_REGISTER 4 | ||
49 | |||
50 | /* val = value, dec = left shift, bits = number of bits of the mask */ | ||
51 | #define EXTRACT(val, dec, bits) ((val >> dec) & ((1 << bits) - 1)) | ||
52 | |||
53 | struct ad7923_state { | ||
54 | struct spi_device *spi; | ||
55 | struct spi_transfer ring_xfer[5]; | ||
56 | struct spi_transfer scan_single_xfer[2]; | ||
57 | struct spi_message ring_msg; | ||
58 | struct spi_message scan_single_msg; | ||
59 | /* | ||
60 | * DMA (thus cache coherency maintenance) requires the | ||
61 | * transfer buffers to live in their own cache lines. | ||
62 | */ | ||
63 | __be16 rx_buf[4] ____cacheline_aligned; | ||
64 | __be16 tx_buf[4]; | ||
65 | }; | ||
66 | |||
67 | #define AD7923_V_CHAN(index) \ | ||
68 | { \ | ||
69 | .type = IIO_VOLTAGE, \ | ||
70 | .indexed = 1, \ | ||
71 | .channel = index, \ | ||
72 | .info_mask = IIO_CHAN_INFO_RAW_SEPARATE_BIT | \ | ||
73 | IIO_CHAN_INFO_SCALE_SHARED_BIT, \ | ||
74 | .address = index, \ | ||
75 | .scan_index = index, \ | ||
76 | .scan_type = { \ | ||
77 | .sign = 'u', \ | ||
78 | .realbits = 12, \ | ||
79 | .storagebits = 16, \ | ||
80 | .endianness = IIO_BE, \ | ||
81 | }, \ | ||
82 | } | ||
83 | |||
84 | static const struct iio_chan_spec ad7923_channels[] = { | ||
85 | AD7923_V_CHAN(0), | ||
86 | AD7923_V_CHAN(1), | ||
87 | AD7923_V_CHAN(2), | ||
88 | AD7923_V_CHAN(3), | ||
89 | IIO_CHAN_SOFT_TIMESTAMP(4), | ||
90 | }; | ||
91 | |||
92 | /** | ||
93 | * ad7923_update_scan_mode() setup the spi transfer buffer for the new scan mask | ||
94 | **/ | ||
95 | static int ad7923_update_scan_mode(struct iio_dev *indio_dev, | ||
96 | const unsigned long *active_scan_mask) | ||
97 | { | ||
98 | struct ad7923_state *st = iio_priv(indio_dev); | ||
99 | int i, cmd, len; | ||
100 | |||
101 | len = 0; | ||
102 | for_each_set_bit(i, active_scan_mask, AD7923_MAX_CHAN) { | ||
103 | cmd = AD7923_WRITE_CR | AD7923_CODING | AD7923_RANGE | | ||
104 | AD7923_PM_MODE_WRITE(AD7923_PM_MODE_OPS) | | ||
105 | AD7923_SEQUENCE_WRITE(AD7923_SEQUENCE_OFF) | | ||
106 | AD7923_CHANNEL_WRITE(i); | ||
107 | cmd <<= AD7923_SHIFT_REGISTER; | ||
108 | st->tx_buf[len++] = cpu_to_be16(cmd); | ||
109 | } | ||
110 | /* build spi ring message */ | ||
111 | st->ring_xfer[0].tx_buf = &st->tx_buf[0]; | ||
112 | st->ring_xfer[0].len = len; | ||
113 | st->ring_xfer[0].cs_change = 1; | ||
114 | |||
115 | spi_message_init(&st->ring_msg); | ||
116 | spi_message_add_tail(&st->ring_xfer[0], &st->ring_msg); | ||
117 | |||
118 | for (i = 0; i < len; i++) { | ||
119 | st->ring_xfer[i + 1].rx_buf = &st->rx_buf[i]; | ||
120 | st->ring_xfer[i + 1].len = 2; | ||
121 | st->ring_xfer[i + 1].cs_change = 1; | ||
122 | spi_message_add_tail(&st->ring_xfer[i + 1], &st->ring_msg); | ||
123 | } | ||
124 | /* make sure last transfer cs_change is not set */ | ||
125 | st->ring_xfer[i + 1].cs_change = 0; | ||
126 | |||
127 | return 0; | ||
128 | } | ||
129 | |||
130 | /** | ||
131 | * ad7923_trigger_handler() bh of trigger launched polling to ring buffer | ||
132 | * | ||
133 | * Currently there is no option in this driver to disable the saving of | ||
134 | * timestamps within the ring. | ||
135 | **/ | ||
136 | static irqreturn_t ad7923_trigger_handler(int irq, void *p) | ||
137 | { | ||
138 | struct iio_poll_func *pf = p; | ||
139 | struct iio_dev *indio_dev = pf->indio_dev; | ||
140 | struct ad7923_state *st = iio_priv(indio_dev); | ||
141 | s64 time_ns = 0; | ||
142 | int b_sent; | ||
143 | |||
144 | b_sent = spi_sync(st->spi, &st->ring_msg); | ||
145 | if (b_sent) | ||
146 | goto done; | ||
147 | |||
148 | if (indio_dev->scan_timestamp) { | ||
149 | time_ns = iio_get_time_ns(); | ||
150 | memcpy((u8 *)st->rx_buf + indio_dev->scan_bytes - sizeof(s64), | ||
151 | &time_ns, sizeof(time_ns)); | ||
152 | } | ||
153 | |||
154 | iio_push_to_buffers(indio_dev, (u8 *)st->rx_buf); | ||
155 | |||
156 | done: | ||
157 | iio_trigger_notify_done(indio_dev->trig); | ||
158 | |||
159 | return IRQ_HANDLED; | ||
160 | } | ||
161 | |||
162 | static int ad7923_scan_direct(struct ad7923_state *st, unsigned ch) | ||
163 | { | ||
164 | int ret, cmd; | ||
165 | |||
166 | cmd = AD7923_WRITE_CR | AD7923_PM_MODE_WRITE(AD7923_PM_MODE_OPS) | | ||
167 | AD7923_SEQUENCE_WRITE(AD7923_SEQUENCE_OFF) | AD7923_CODING | | ||
168 | AD7923_CHANNEL_WRITE(ch) | AD7923_RANGE; | ||
169 | cmd <<= AD7923_SHIFT_REGISTER; | ||
170 | st->tx_buf[0] = cpu_to_be16(cmd); | ||
171 | |||
172 | ret = spi_sync(st->spi, &st->scan_single_msg); | ||
173 | if (ret) | ||
174 | return ret; | ||
175 | |||
176 | return be16_to_cpu(st->rx_buf[0]); | ||
177 | } | ||
178 | |||
179 | static int ad7923_read_raw(struct iio_dev *indio_dev, | ||
180 | struct iio_chan_spec const *chan, | ||
181 | int *val, | ||
182 | int *val2, | ||
183 | long m) | ||
184 | { | ||
185 | int ret; | ||
186 | struct ad7923_state *st = iio_priv(indio_dev); | ||
187 | |||
188 | switch (m) { | ||
189 | case IIO_CHAN_INFO_RAW: | ||
190 | mutex_lock(&indio_dev->mlock); | ||
191 | if (iio_buffer_enabled(indio_dev)) | ||
192 | ret = -EBUSY; | ||
193 | else | ||
194 | ret = ad7923_scan_direct(st, chan->address); | ||
195 | mutex_unlock(&indio_dev->mlock); | ||
196 | |||
197 | if (ret < 0) | ||
198 | return ret; | ||
199 | |||
200 | if (chan->address == EXTRACT(ret, 12, 4)) | ||
201 | *val = EXTRACT(ret, 0, 12); | ||
202 | |||
203 | return IIO_VAL_INT; | ||
204 | } | ||
205 | return -EINVAL; | ||
206 | } | ||
207 | |||
208 | static const struct iio_info ad7923_info = { | ||
209 | .read_raw = &ad7923_read_raw, | ||
210 | .update_scan_mode = ad7923_update_scan_mode, | ||
211 | .driver_module = THIS_MODULE, | ||
212 | }; | ||
213 | |||
214 | static int ad7923_probe(struct spi_device *spi) | ||
215 | { | ||
216 | struct ad7923_state *st; | ||
217 | struct iio_dev *indio_dev = iio_device_alloc(sizeof(*st)); | ||
218 | int ret; | ||
219 | |||
220 | if (indio_dev == NULL) | ||
221 | return -ENOMEM; | ||
222 | |||
223 | st = iio_priv(indio_dev); | ||
224 | |||
225 | spi_set_drvdata(spi, indio_dev); | ||
226 | |||
227 | st->spi = spi; | ||
228 | |||
229 | indio_dev->name = spi_get_device_id(spi)->name; | ||
230 | indio_dev->dev.parent = &spi->dev; | ||
231 | indio_dev->modes = INDIO_DIRECT_MODE; | ||
232 | indio_dev->channels = ad7923_channels; | ||
233 | indio_dev->num_channels = ARRAY_SIZE(ad7923_channels); | ||
234 | indio_dev->info = &ad7923_info; | ||
235 | |||
236 | /* Setup default message */ | ||
237 | |||
238 | st->scan_single_xfer[0].tx_buf = &st->tx_buf[0]; | ||
239 | st->scan_single_xfer[0].len = 2; | ||
240 | st->scan_single_xfer[0].cs_change = 1; | ||
241 | st->scan_single_xfer[1].rx_buf = &st->rx_buf[0]; | ||
242 | st->scan_single_xfer[1].len = 2; | ||
243 | |||
244 | spi_message_init(&st->scan_single_msg); | ||
245 | spi_message_add_tail(&st->scan_single_xfer[0], &st->scan_single_msg); | ||
246 | spi_message_add_tail(&st->scan_single_xfer[1], &st->scan_single_msg); | ||
247 | |||
248 | ret = iio_triggered_buffer_setup(indio_dev, NULL, | ||
249 | &ad7923_trigger_handler, NULL); | ||
250 | if (ret) | ||
251 | goto error_free; | ||
252 | |||
253 | ret = iio_device_register(indio_dev); | ||
254 | if (ret) | ||
255 | goto error_cleanup_ring; | ||
256 | |||
257 | return 0; | ||
258 | |||
259 | error_cleanup_ring: | ||
260 | iio_triggered_buffer_cleanup(indio_dev); | ||
261 | error_free: | ||
262 | iio_device_free(indio_dev); | ||
263 | |||
264 | return ret; | ||
265 | } | ||
266 | |||
267 | static int ad7923_remove(struct spi_device *spi) | ||
268 | { | ||
269 | struct iio_dev *indio_dev = spi_get_drvdata(spi); | ||
270 | |||
271 | iio_device_unregister(indio_dev); | ||
272 | iio_triggered_buffer_cleanup(indio_dev); | ||
273 | iio_device_free(indio_dev); | ||
274 | |||
275 | return 0; | ||
276 | } | ||
277 | |||
278 | static const struct spi_device_id ad7923_id[] = { | ||
279 | {"ad7923", 0}, | ||
280 | {} | ||
281 | }; | ||
282 | MODULE_DEVICE_TABLE(spi, ad7923_id); | ||
283 | |||
284 | static struct spi_driver ad7923_driver = { | ||
285 | .driver = { | ||
286 | .name = "ad7923", | ||
287 | .owner = THIS_MODULE, | ||
288 | }, | ||
289 | .probe = ad7923_probe, | ||
290 | .remove = ad7923_remove, | ||
291 | .id_table = ad7923_id, | ||
292 | }; | ||
293 | module_spi_driver(ad7923_driver); | ||
294 | |||
295 | MODULE_AUTHOR("Michael Hennerich <hennerich@blackfin.uclinux.org>"); | ||
296 | MODULE_AUTHOR("Patrick Vasseur <patrick.vasseur@c-s.fr>"); | ||
297 | MODULE_DESCRIPTION("Analog Devices AD7923 ADC"); | ||
298 | MODULE_LICENSE("GPL v2"); | ||