11ccc42b25a6014b8523493036b201c6a19eb131
[deliverable/linux.git] / drivers / iio / adc / ad7923.c
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/regulator/consumer.h>
16 #include <linux/err.h>
17 #include <linux/delay.h>
18 #include <linux/module.h>
19 #include <linux/interrupt.h>
20
21 #include <linux/iio/iio.h>
22 #include <linux/iio/sysfs.h>
23 #include <linux/iio/buffer.h>
24 #include <linux/iio/trigger_consumer.h>
25 #include <linux/iio/triggered_buffer.h>
26
27 #define AD7923_WRITE_CR (1 << 11) /* write control register */
28 #define AD7923_RANGE (1 << 1) /* range to REFin */
29 #define AD7923_CODING (1 << 0) /* coding is straight binary */
30 #define AD7923_PM_MODE_AS (1) /* auto shutdown */
31 #define AD7923_PM_MODE_FS (2) /* full shutdown */
32 #define AD7923_PM_MODE_OPS (3) /* normal operation */
33 #define AD7923_CHANNEL_0 (0) /* analog input 0 */
34 #define AD7923_CHANNEL_1 (1) /* analog input 1 */
35 #define AD7923_CHANNEL_2 (2) /* analog input 2 */
36 #define AD7923_CHANNEL_3 (3) /* analog input 3 */
37 #define AD7923_SEQUENCE_OFF (0) /* no sequence fonction */
38 #define AD7923_SEQUENCE_PROTECT (2) /* no interrupt write cycle */
39 #define AD7923_SEQUENCE_ON (3) /* continuous sequence */
40
41 #define AD7923_MAX_CHAN 4
42
43 #define AD7923_PM_MODE_WRITE(mode) (mode << 4) /* write mode */
44 #define AD7923_CHANNEL_WRITE(channel) (channel << 6) /* write channel */
45 #define AD7923_SEQUENCE_WRITE(sequence) (((sequence & 1) << 3) \
46 + ((sequence & 2) << 9))
47 /* write sequence fonction */
48 /* left shift for CR : bit 11 transmit in first */
49 #define AD7923_SHIFT_REGISTER 4
50
51 /* val = value, dec = left shift, bits = number of bits of the mask */
52 #define EXTRACT(val, dec, bits) ((val >> dec) & ((1 << bits) - 1))
53
54 struct ad7923_state {
55 struct spi_device *spi;
56 struct spi_transfer ring_xfer[5];
57 struct spi_transfer scan_single_xfer[2];
58 struct spi_message ring_msg;
59 struct spi_message scan_single_msg;
60
61 struct regulator *reg;
62
63 unsigned int settings;
64
65 /*
66 * DMA (thus cache coherency maintenance) requires the
67 * transfer buffers to live in their own cache lines.
68 */
69 __be16 rx_buf[4] ____cacheline_aligned;
70 __be16 tx_buf[4];
71 };
72
73 #define AD7923_V_CHAN(index) \
74 { \
75 .type = IIO_VOLTAGE, \
76 .indexed = 1, \
77 .channel = index, \
78 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \
79 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE), \
80 .address = index, \
81 .scan_index = index, \
82 .scan_type = { \
83 .sign = 'u', \
84 .realbits = 12, \
85 .storagebits = 16, \
86 .endianness = IIO_BE, \
87 }, \
88 }
89
90 static const struct iio_chan_spec ad7923_channels[] = {
91 AD7923_V_CHAN(0),
92 AD7923_V_CHAN(1),
93 AD7923_V_CHAN(2),
94 AD7923_V_CHAN(3),
95 IIO_CHAN_SOFT_TIMESTAMP(4),
96 };
97
98 /**
99 * ad7923_update_scan_mode() setup the spi transfer buffer for the new scan mask
100 **/
101 static int ad7923_update_scan_mode(struct iio_dev *indio_dev,
102 const unsigned long *active_scan_mask)
103 {
104 struct ad7923_state *st = iio_priv(indio_dev);
105 int i, cmd, len;
106
107 len = 0;
108 for_each_set_bit(i, active_scan_mask, AD7923_MAX_CHAN) {
109 cmd = AD7923_WRITE_CR | AD7923_CHANNEL_WRITE(i) |
110 AD7923_SEQUENCE_WRITE(AD7923_SEQUENCE_OFF) |
111 st->settings;
112 cmd <<= AD7923_SHIFT_REGISTER;
113 st->tx_buf[len++] = cpu_to_be16(cmd);
114 }
115 /* build spi ring message */
116 st->ring_xfer[0].tx_buf = &st->tx_buf[0];
117 st->ring_xfer[0].len = len;
118 st->ring_xfer[0].cs_change = 1;
119
120 spi_message_init(&st->ring_msg);
121 spi_message_add_tail(&st->ring_xfer[0], &st->ring_msg);
122
123 for (i = 0; i < len; i++) {
124 st->ring_xfer[i + 1].rx_buf = &st->rx_buf[i];
125 st->ring_xfer[i + 1].len = 2;
126 st->ring_xfer[i + 1].cs_change = 1;
127 spi_message_add_tail(&st->ring_xfer[i + 1], &st->ring_msg);
128 }
129 /* make sure last transfer cs_change is not set */
130 st->ring_xfer[i + 1].cs_change = 0;
131
132 return 0;
133 }
134
135 /**
136 * ad7923_trigger_handler() bh of trigger launched polling to ring buffer
137 *
138 * Currently there is no option in this driver to disable the saving of
139 * timestamps within the ring.
140 **/
141 static irqreturn_t ad7923_trigger_handler(int irq, void *p)
142 {
143 struct iio_poll_func *pf = p;
144 struct iio_dev *indio_dev = pf->indio_dev;
145 struct ad7923_state *st = iio_priv(indio_dev);
146 s64 time_ns = 0;
147 int b_sent;
148
149 b_sent = spi_sync(st->spi, &st->ring_msg);
150 if (b_sent)
151 goto done;
152
153 if (indio_dev->scan_timestamp) {
154 time_ns = iio_get_time_ns();
155 memcpy((u8 *)st->rx_buf + indio_dev->scan_bytes - sizeof(s64),
156 &time_ns, sizeof(time_ns));
157 }
158
159 iio_push_to_buffers(indio_dev, (u8 *)st->rx_buf);
160
161 done:
162 iio_trigger_notify_done(indio_dev->trig);
163
164 return IRQ_HANDLED;
165 }
166
167 static int ad7923_scan_direct(struct ad7923_state *st, unsigned ch)
168 {
169 int ret, cmd;
170
171 cmd = AD7923_WRITE_CR | AD7923_CHANNEL_WRITE(ch) |
172 AD7923_SEQUENCE_WRITE(AD7923_SEQUENCE_OFF) |
173 st->settings;
174 cmd <<= AD7923_SHIFT_REGISTER;
175 st->tx_buf[0] = cpu_to_be16(cmd);
176
177 ret = spi_sync(st->spi, &st->scan_single_msg);
178 if (ret)
179 return ret;
180
181 return be16_to_cpu(st->rx_buf[0]);
182 }
183
184 static int ad7923_get_range(struct ad7923_state *st)
185 {
186 int vref;
187
188 vref = regulator_get_voltage(st->reg);
189 if (vref < 0)
190 return vref;
191
192 vref /= 1000;
193
194 if (!(st->settings & AD7923_RANGE))
195 vref *= 2;
196
197 return vref;
198 }
199
200 static int ad7923_read_raw(struct iio_dev *indio_dev,
201 struct iio_chan_spec const *chan,
202 int *val,
203 int *val2,
204 long m)
205 {
206 int ret;
207 struct ad7923_state *st = iio_priv(indio_dev);
208
209 switch (m) {
210 case IIO_CHAN_INFO_RAW:
211 mutex_lock(&indio_dev->mlock);
212 if (iio_buffer_enabled(indio_dev))
213 ret = -EBUSY;
214 else
215 ret = ad7923_scan_direct(st, chan->address);
216 mutex_unlock(&indio_dev->mlock);
217
218 if (ret < 0)
219 return ret;
220
221 if (chan->address == EXTRACT(ret, 12, 4))
222 *val = EXTRACT(ret, 0, 12);
223 else
224 return -EIO;
225
226 return IIO_VAL_INT;
227 case IIO_CHAN_INFO_SCALE:
228 ret = ad7923_get_range(st);
229 if (ret < 0)
230 return ret;
231 *val = ret;
232 *val2 = chan->scan_type.realbits;
233 return IIO_VAL_FRACTIONAL_LOG2;
234 }
235 return -EINVAL;
236 }
237
238 static const struct iio_info ad7923_info = {
239 .read_raw = &ad7923_read_raw,
240 .update_scan_mode = ad7923_update_scan_mode,
241 .driver_module = THIS_MODULE,
242 };
243
244 static int ad7923_probe(struct spi_device *spi)
245 {
246 struct ad7923_state *st;
247 struct iio_dev *indio_dev = iio_device_alloc(sizeof(*st));
248 int ret;
249
250 if (indio_dev == NULL)
251 return -ENOMEM;
252
253 st = iio_priv(indio_dev);
254
255 spi_set_drvdata(spi, indio_dev);
256
257 st->spi = spi;
258 st->settings = AD7923_CODING | AD7923_RANGE |
259 AD7923_PM_MODE_WRITE(AD7923_PM_MODE_OPS);
260
261 indio_dev->name = spi_get_device_id(spi)->name;
262 indio_dev->dev.parent = &spi->dev;
263 indio_dev->modes = INDIO_DIRECT_MODE;
264 indio_dev->channels = ad7923_channels;
265 indio_dev->num_channels = ARRAY_SIZE(ad7923_channels);
266 indio_dev->info = &ad7923_info;
267
268 /* Setup default message */
269
270 st->scan_single_xfer[0].tx_buf = &st->tx_buf[0];
271 st->scan_single_xfer[0].len = 2;
272 st->scan_single_xfer[0].cs_change = 1;
273 st->scan_single_xfer[1].rx_buf = &st->rx_buf[0];
274 st->scan_single_xfer[1].len = 2;
275
276 spi_message_init(&st->scan_single_msg);
277 spi_message_add_tail(&st->scan_single_xfer[0], &st->scan_single_msg);
278 spi_message_add_tail(&st->scan_single_xfer[1], &st->scan_single_msg);
279
280 st->reg = regulator_get(&spi->dev, "refin");
281 if (IS_ERR(st->reg)) {
282 ret = PTR_ERR(st->reg);
283 goto error_free;
284 }
285 ret = regulator_enable(st->reg);
286 if (ret)
287 goto error_put_reg;
288
289 ret = iio_triggered_buffer_setup(indio_dev, NULL,
290 &ad7923_trigger_handler, NULL);
291 if (ret)
292 goto error_disable_reg;
293
294 ret = iio_device_register(indio_dev);
295 if (ret)
296 goto error_cleanup_ring;
297
298 return 0;
299
300 error_cleanup_ring:
301 iio_triggered_buffer_cleanup(indio_dev);
302 error_disable_reg:
303 regulator_disable(st->reg);
304 error_put_reg:
305 regulator_put(st->reg);
306 error_free:
307 iio_device_free(indio_dev);
308
309 return ret;
310 }
311
312 static int ad7923_remove(struct spi_device *spi)
313 {
314 struct iio_dev *indio_dev = spi_get_drvdata(spi);
315 struct ad7923_state *st = iio_priv(indio_dev);
316
317 iio_device_unregister(indio_dev);
318 iio_triggered_buffer_cleanup(indio_dev);
319 regulator_disable(st->reg);
320 regulator_put(st->reg);
321 iio_device_free(indio_dev);
322
323 return 0;
324 }
325
326 static const struct spi_device_id ad7923_id[] = {
327 {"ad7923", 0},
328 {}
329 };
330 MODULE_DEVICE_TABLE(spi, ad7923_id);
331
332 static struct spi_driver ad7923_driver = {
333 .driver = {
334 .name = "ad7923",
335 .owner = THIS_MODULE,
336 },
337 .probe = ad7923_probe,
338 .remove = ad7923_remove,
339 .id_table = ad7923_id,
340 };
341 module_spi_driver(ad7923_driver);
342
343 MODULE_AUTHOR("Michael Hennerich <hennerich@blackfin.uclinux.org>");
344 MODULE_AUTHOR("Patrick Vasseur <patrick.vasseur@c-s.fr>");
345 MODULE_DESCRIPTION("Analog Devices AD7923 ADC");
346 MODULE_LICENSE("GPL v2");
This page took 0.055366 seconds and 4 git commands to generate.