staging:iio: rationalization of different buffer implementation hooks.
[linux-2.6-block.git] / drivers / staging / iio / adc / max1363_ring.c
1 /*
2  * Copyright (C) 2008 Jonathan Cameron
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License version 2 as
6  * published by the Free Software Foundation.
7  *
8  * max1363_ring.c
9  */
10
11 #include <linux/interrupt.h>
12 #include <linux/device.h>
13 #include <linux/slab.h>
14 #include <linux/kernel.h>
15 #include <linux/sysfs.h>
16 #include <linux/list.h>
17 #include <linux/i2c.h>
18 #include <linux/bitops.h>
19
20 #include "../iio.h"
21 #include "../ring_generic.h"
22 #include "../ring_sw.h"
23 #include "../trigger.h"
24 #include "../sysfs.h"
25
26 #include "max1363.h"
27
28 int max1363_single_channel_from_ring(long mask, struct max1363_state *st)
29 {
30         struct iio_ring_buffer *ring = iio_priv_to_dev(st)->ring;
31         int count = 0, ret;
32         u8 *ring_data;
33         if (!(st->current_mode->modemask & mask)) {
34                 ret = -EBUSY;
35                 goto error_ret;
36         }
37
38         ring_data = kmalloc(ring->access->get_bytes_per_datum(ring),
39                             GFP_KERNEL);
40         if (ring_data == NULL) {
41                 ret = -ENOMEM;
42                 goto error_ret;
43         }
44         ret = ring->access->read_last(ring, ring_data);
45         if (ret)
46                 goto error_free_ring_data;
47         /* Need a count of channels prior to this one */
48         mask >>= 1;
49         while (mask) {
50                 if (mask & st->current_mode->modemask)
51                         count++;
52                 mask >>= 1;
53         }
54         if (st->chip_info->bits != 8)
55                 ret = ((int)(ring_data[count*2 + 0] & 0x0F) << 8)
56                         + (int)(ring_data[count*2 + 1]);
57         else
58                 ret = ring_data[count];
59
60 error_free_ring_data:
61         kfree(ring_data);
62 error_ret:
63         return ret;
64 }
65
66 /**
67  * max1363_ring_preenable() - setup the parameters of the ring before enabling
68  *
69  * The complex nature of the setting of the nuber of bytes per datum is due
70  * to this driver currently ensuring that the timestamp is stored at an 8
71  * byte boundary.
72  **/
73 static int max1363_ring_preenable(struct iio_dev *indio_dev)
74 {
75         struct max1363_state *st = iio_priv(indio_dev);
76         struct iio_ring_buffer *ring = indio_dev->ring;
77         size_t d_size = 0;
78         unsigned long numvals;
79
80         /*
81          * Need to figure out the current mode based upon the requested
82          * scan mask in iio_dev
83          */
84         st->current_mode = max1363_match_mode(ring->scan_mask,
85                                         st->chip_info);
86         if (!st->current_mode)
87                 return -EINVAL;
88
89         max1363_set_scan_mode(st);
90
91         numvals = hweight_long(st->current_mode->modemask);
92         if (ring->access->set_bytes_per_datum) {
93                 if (ring->scan_timestamp)
94                         d_size += sizeof(s64);
95                 if (st->chip_info->bits != 8)
96                         d_size += numvals*2;
97                 else
98                         d_size += numvals;
99                 if (ring->scan_timestamp && (d_size % 8))
100                         d_size += 8 - (d_size % 8);
101                 ring->access->set_bytes_per_datum(ring, d_size);
102         }
103
104         return 0;
105 }
106
107 static irqreturn_t max1363_trigger_handler(int irq, void *p)
108 {
109         struct iio_poll_func *pf = p;
110         struct iio_dev *indio_dev = pf->private_data;
111         struct max1363_state *st = iio_priv(indio_dev);
112         s64 time_ns;
113         __u8 *rxbuf;
114         int b_sent;
115         size_t d_size;
116         unsigned long numvals = hweight_long(st->current_mode->modemask);
117
118         /* Ensure the timestamp is 8 byte aligned */
119         if (st->chip_info->bits != 8)
120                 d_size = numvals*2 + sizeof(s64);
121         else
122                 d_size = numvals + sizeof(s64);
123         if (d_size % sizeof(s64))
124                 d_size += sizeof(s64) - (d_size % sizeof(s64));
125
126         /* Monitor mode prevents reading. Whilst not currently implemented
127          * might as well have this test in here in the meantime as it does
128          * no harm.
129          */
130         if (numvals == 0)
131                 return IRQ_HANDLED;
132
133         rxbuf = kmalloc(d_size, GFP_KERNEL);
134         if (rxbuf == NULL)
135                 return -ENOMEM;
136         if (st->chip_info->bits != 8)
137                 b_sent = i2c_master_recv(st->client, rxbuf, numvals*2);
138         else
139                 b_sent = i2c_master_recv(st->client, rxbuf, numvals);
140         if (b_sent < 0)
141                 goto done;
142
143         time_ns = iio_get_time_ns();
144
145         memcpy(rxbuf + d_size - sizeof(s64), &time_ns, sizeof(time_ns));
146
147         indio_dev->ring->access->store_to(indio_dev->ring, rxbuf, time_ns);
148 done:
149         iio_trigger_notify_done(indio_dev->trig);
150         kfree(rxbuf);
151
152         return IRQ_HANDLED;
153 }
154
155 static const struct iio_ring_setup_ops max1363_ring_setup_ops = {
156         .postenable = &iio_triggered_ring_postenable,
157         .preenable = &max1363_ring_preenable,
158         .predisable = &iio_triggered_ring_predisable,
159 };
160
161 int max1363_register_ring_funcs_and_init(struct iio_dev *indio_dev)
162 {
163         struct max1363_state *st = iio_priv(indio_dev);
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         indio_dev->pollfunc = kzalloc(sizeof(*indio_dev->pollfunc), GFP_KERNEL);
172         if (indio_dev->pollfunc == NULL) {
173                 ret = -ENOMEM;
174                 goto error_deallocate_sw_rb;
175         }
176         indio_dev->pollfunc->private_data = indio_dev;
177         indio_dev->pollfunc->thread = &max1363_trigger_handler;
178         indio_dev->pollfunc->type = IRQF_ONESHOT;
179         indio_dev->pollfunc->name =
180                 kasprintf(GFP_KERNEL, "%s_consumer%d",
181                           st->client->name, indio_dev->id);
182         if (indio_dev->pollfunc->name == NULL) {
183                 ret = -ENOMEM;
184                 goto error_free_pollfunc;
185         }
186         /* Effectively select the ring buffer implementation */
187         indio_dev->ring->access = &ring_sw_access_funcs;
188         /* Ring buffer functions - here trigger setup related */
189         indio_dev->ring->setup_ops = &max1363_ring_setup_ops;
190
191         /* Flag that polled ring buffering is possible */
192         indio_dev->modes |= INDIO_RING_TRIGGERED;
193
194         return 0;
195 error_free_pollfunc:
196         kfree(indio_dev->pollfunc);
197 error_deallocate_sw_rb:
198         iio_sw_rb_free(indio_dev->ring);
199 error_ret:
200         return ret;
201 }
202
203 void max1363_ring_cleanup(struct iio_dev *indio_dev)
204 {
205         /* ensure that the trigger has been detached */
206         if (indio_dev->trig) {
207                 iio_put_trigger(indio_dev->trig);
208                 iio_trigger_dettach_poll_func(indio_dev->trig,
209                                               indio_dev->pollfunc);
210         }
211         kfree(indio_dev->pollfunc->name);
212         kfree(indio_dev->pollfunc);
213         iio_sw_rb_free(indio_dev->ring);
214 }