IIO: Move core headers to include/linux/iio
[deliverable/linux.git] / drivers / staging / iio / adc / ad799x_ring.c
1 /*
2 * Copyright (C) 2010 Michael Hennerich, Analog Devices Inc.
3 * Copyright (C) 2008-2010 Jonathan Cameron
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 2 as
7 * published by the Free Software Foundation.
8 *
9 * ad799x_ring.c
10 */
11
12 #include <linux/interrupt.h>
13 #include <linux/slab.h>
14 #include <linux/kernel.h>
15 #include <linux/list.h>
16 #include <linux/i2c.h>
17 #include <linux/bitops.h>
18
19 #include <linux/iio/iio.h>
20 #include <linux/iio/buffer.h>
21 #include "../ring_sw.h"
22 #include <linux/iio/trigger_consumer.h>
23
24 #include "ad799x.h"
25
26 /**
27 * ad799x_ring_preenable() setup the parameters of the ring before enabling
28 *
29 * The complex nature of the setting of the number of bytes per datum is due
30 * to this driver currently ensuring that the timestamp is stored at an 8
31 * byte boundary.
32 **/
33 static int ad799x_ring_preenable(struct iio_dev *indio_dev)
34 {
35 struct ad799x_state *st = iio_priv(indio_dev);
36 /*
37 * Need to figure out the current mode based upon the requested
38 * scan mask in iio_dev
39 */
40
41 if (st->id == ad7997 || st->id == ad7998)
42 ad7997_8_set_scan_mode(st, *indio_dev->active_scan_mask);
43
44 return iio_sw_buffer_preenable(indio_dev);
45 }
46
47 /**
48 * ad799x_trigger_handler() bh of trigger launched polling to ring buffer
49 *
50 * Currently there is no option in this driver to disable the saving of
51 * timestamps within the ring.
52 **/
53
54 static irqreturn_t ad799x_trigger_handler(int irq, void *p)
55 {
56 struct iio_poll_func *pf = p;
57 struct iio_dev *indio_dev = pf->indio_dev;
58 struct ad799x_state *st = iio_priv(indio_dev);
59 struct iio_buffer *ring = indio_dev->buffer;
60 s64 time_ns;
61 __u8 *rxbuf;
62 int b_sent;
63 u8 cmd;
64
65 rxbuf = kmalloc(indio_dev->scan_bytes, GFP_KERNEL);
66 if (rxbuf == NULL)
67 goto out;
68
69 switch (st->id) {
70 case ad7991:
71 case ad7995:
72 case ad7999:
73 cmd = st->config |
74 (*indio_dev->active_scan_mask << AD799X_CHANNEL_SHIFT);
75 break;
76 case ad7992:
77 case ad7993:
78 case ad7994:
79 cmd = (*indio_dev->active_scan_mask << AD799X_CHANNEL_SHIFT) |
80 AD7998_CONV_RES_REG;
81 break;
82 case ad7997:
83 case ad7998:
84 cmd = AD7997_8_READ_SEQUENCE | AD7998_CONV_RES_REG;
85 break;
86 default:
87 cmd = 0;
88 }
89
90 b_sent = i2c_smbus_read_i2c_block_data(st->client,
91 cmd, bitmap_weight(indio_dev->active_scan_mask,
92 indio_dev->masklength) * 2, rxbuf);
93 if (b_sent < 0)
94 goto done;
95
96 time_ns = iio_get_time_ns();
97
98 if (indio_dev->scan_timestamp)
99 memcpy(rxbuf + indio_dev->scan_bytes - sizeof(s64),
100 &time_ns, sizeof(time_ns));
101
102 ring->access->store_to(indio_dev->buffer, rxbuf, time_ns);
103 done:
104 kfree(rxbuf);
105 if (b_sent < 0)
106 return b_sent;
107 out:
108 iio_trigger_notify_done(indio_dev->trig);
109
110 return IRQ_HANDLED;
111 }
112
113 static const struct iio_buffer_setup_ops ad799x_buf_setup_ops = {
114 .preenable = &ad799x_ring_preenable,
115 .postenable = &iio_triggered_buffer_postenable,
116 .predisable = &iio_triggered_buffer_predisable,
117 };
118
119 int ad799x_register_ring_funcs_and_init(struct iio_dev *indio_dev)
120 {
121 int ret = 0;
122
123 indio_dev->buffer = iio_sw_rb_allocate(indio_dev);
124 if (!indio_dev->buffer) {
125 ret = -ENOMEM;
126 goto error_ret;
127 }
128 indio_dev->pollfunc = iio_alloc_pollfunc(NULL,
129 &ad799x_trigger_handler,
130 IRQF_ONESHOT,
131 indio_dev,
132 "%s_consumer%d",
133 indio_dev->name,
134 indio_dev->id);
135 if (indio_dev->pollfunc == NULL) {
136 ret = -ENOMEM;
137 goto error_deallocate_sw_rb;
138 }
139
140 /* Ring buffer functions - here trigger setup related */
141 indio_dev->setup_ops = &ad799x_buf_setup_ops;
142 indio_dev->buffer->scan_timestamp = true;
143
144 /* Flag that polled ring buffering is possible */
145 indio_dev->modes |= INDIO_BUFFER_TRIGGERED;
146 return 0;
147
148 error_deallocate_sw_rb:
149 iio_sw_rb_free(indio_dev->buffer);
150 error_ret:
151 return ret;
152 }
153
154 void ad799x_ring_cleanup(struct iio_dev *indio_dev)
155 {
156 iio_dealloc_pollfunc(indio_dev->pollfunc);
157 iio_sw_rb_free(indio_dev->buffer);
158 }
This page took 0.040747 seconds and 5 git commands to generate.