]> Pileus Git - ~andy/linux/blob - drivers/staging/iio/adc/ad799x_ring.c
staging:iio:pollfunc: Make explicit that private data is always pointer to a struct...
[~andy/linux] / 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 "../iio.h"
20 #include "../ring_generic.h"
21 #include "../ring_sw.h"
22 #include "../trigger.h"
23
24 #include "ad799x.h"
25
26 int ad799x_single_channel_from_ring(struct ad799x_state *st, long mask)
27 {
28         struct iio_ring_buffer *ring = iio_priv_to_dev(st)->ring;
29         int count = 0, ret;
30         u16 *ring_data;
31
32         if (!(ring->scan_mask & mask)) {
33                 ret = -EBUSY;
34                 goto error_ret;
35         }
36
37         ring_data = kmalloc(ring->access->get_bytes_per_datum(ring),
38                             GFP_KERNEL);
39         if (ring_data == NULL) {
40                 ret = -ENOMEM;
41                 goto error_ret;
42         }
43         ret = ring->access->read_last(ring, (u8 *) ring_data);
44         if (ret)
45                 goto error_free_ring_data;
46         /* Need a count of channels prior to this one */
47         mask >>= 1;
48         while (mask) {
49                 if (mask & ring->scan_mask)
50                         count++;
51                 mask >>= 1;
52         }
53
54         ret = be16_to_cpu(ring_data[count]);
55
56 error_free_ring_data:
57         kfree(ring_data);
58 error_ret:
59         return ret;
60 }
61
62 /**
63  * ad799x_ring_preenable() setup the parameters of the ring before enabling
64  *
65  * The complex nature of the setting of the nuber of bytes per datum is due
66  * to this driver currently ensuring that the timestamp is stored at an 8
67  * byte boundary.
68  **/
69 static int ad799x_ring_preenable(struct iio_dev *indio_dev)
70 {
71         struct iio_ring_buffer *ring = indio_dev->ring;
72         struct ad799x_state *st = iio_priv(indio_dev);
73
74         /*
75          * Need to figure out the current mode based upon the requested
76          * scan mask in iio_dev
77          */
78
79         if (st->id == ad7997 || st->id == ad7998)
80                 ad7997_8_set_scan_mode(st, ring->scan_mask);
81
82         st->d_size = ring->scan_count * 2;
83
84         if (ring->scan_timestamp) {
85                 st->d_size += sizeof(s64);
86
87                 if (st->d_size % sizeof(s64))
88                         st->d_size += sizeof(s64) - (st->d_size % sizeof(s64));
89         }
90
91         if (indio_dev->ring->access->set_bytes_per_datum)
92                 indio_dev->ring->access->set_bytes_per_datum(indio_dev->ring,
93                                                             st->d_size);
94
95         return 0;
96 }
97
98 /**
99  * ad799x_trigger_handler() bh of trigger launched polling to ring buffer
100  *
101  * Currently there is no option in this driver to disable the saving of
102  * timestamps within the ring.
103  **/
104
105 static irqreturn_t ad799x_trigger_handler(int irq, void *p)
106 {
107         struct iio_poll_func *pf = p;
108         struct iio_dev *indio_dev = pf->indio_dev;
109         struct ad799x_state *st = iio_priv(indio_dev);
110         struct iio_ring_buffer *ring = indio_dev->ring;
111         s64 time_ns;
112         __u8 *rxbuf;
113         int b_sent;
114         u8 cmd;
115
116         rxbuf = kmalloc(st->d_size, GFP_KERNEL);
117         if (rxbuf == NULL)
118                 goto out;
119
120         switch (st->id) {
121         case ad7991:
122         case ad7995:
123         case ad7999:
124                 cmd = st->config | (ring->scan_mask << AD799X_CHANNEL_SHIFT);
125                 break;
126         case ad7992:
127         case ad7993:
128         case ad7994:
129                 cmd = (ring->scan_mask << AD799X_CHANNEL_SHIFT) |
130                         AD7998_CONV_RES_REG;
131                 break;
132         case ad7997:
133         case ad7998:
134                 cmd = AD7997_8_READ_SEQUENCE | AD7998_CONV_RES_REG;
135                 break;
136         default:
137                 cmd = 0;
138         }
139
140         b_sent = i2c_smbus_read_i2c_block_data(st->client,
141                         cmd, ring->scan_count * 2, rxbuf);
142         if (b_sent < 0)
143                 goto done;
144
145         time_ns = iio_get_time_ns();
146
147         if (ring->scan_timestamp)
148                 memcpy(rxbuf + st->d_size - sizeof(s64),
149                         &time_ns, sizeof(time_ns));
150
151         ring->access->store_to(indio_dev->ring, rxbuf, time_ns);
152 done:
153         kfree(rxbuf);
154         if (b_sent < 0)
155                 return b_sent;
156 out:
157         iio_trigger_notify_done(indio_dev->trig);
158
159         return IRQ_HANDLED;
160 }
161
162 static const struct iio_ring_setup_ops ad799x_buf_setup_ops = {
163         .preenable = &ad799x_ring_preenable,
164         .postenable = &iio_triggered_ring_postenable,
165         .predisable = &iio_triggered_ring_predisable,
166 };
167
168 int ad799x_register_ring_funcs_and_init(struct iio_dev *indio_dev)
169 {
170         int ret = 0;
171
172         indio_dev->ring = iio_sw_rb_allocate(indio_dev);
173         if (!indio_dev->ring) {
174                 ret = -ENOMEM;
175                 goto error_ret;
176         }
177         /* Effectively select the ring buffer implementation */
178         indio_dev->ring->access = &ring_sw_access_funcs;
179         indio_dev->pollfunc = iio_alloc_pollfunc(NULL,
180                                                  &ad799x_trigger_handler,
181                                                  IRQF_ONESHOT,
182                                                  indio_dev,
183                                                  "%s_consumer%d",
184                                                  indio_dev->name,
185                                                  indio_dev->id);
186         if (indio_dev->pollfunc == NULL) {
187                 ret = -ENOMEM;
188                 goto error_deallocate_sw_rb;
189         }
190
191         /* Ring buffer functions - here trigger setup related */
192         indio_dev->ring->setup_ops = &ad799x_buf_setup_ops;
193         indio_dev->ring->scan_timestamp = true;
194
195         /* Flag that polled ring buffering is possible */
196         indio_dev->modes |= INDIO_RING_TRIGGERED;
197         return 0;
198
199 error_deallocate_sw_rb:
200         iio_sw_rb_free(indio_dev->ring);
201 error_ret:
202         return ret;
203 }
204
205 void ad799x_ring_cleanup(struct iio_dev *indio_dev)
206 {
207         /* ensure that the trigger has been detached */
208         if (indio_dev->trig) {
209                 iio_put_trigger(indio_dev->trig);
210                 iio_trigger_dettach_poll_func(indio_dev->trig,
211                                               indio_dev->pollfunc);
212         }
213         iio_dealloc_pollfunc(indio_dev->pollfunc);
214         iio_sw_rb_free(indio_dev->ring);
215 }