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