staging:iio: treewide rename iio_triggered_ring_* to iio_triggered_buffer_*
[pandora-kernel.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/slab.h>
13 #include <linux/kernel.h>
14 #include <linux/i2c.h>
15 #include <linux/bitops.h>
16
17 #include "../iio.h"
18 #include "../ring_generic.h"
19 #include "../ring_sw.h"
20 #include "../trigger_consumer.h"
21
22 #include "max1363.h"
23
24 int max1363_single_channel_from_ring(const long *mask, struct max1363_state *st)
25 {
26         struct iio_ring_buffer *ring = iio_priv_to_dev(st)->ring;
27         int count = 0, ret, index;
28         u8 *ring_data;
29         index = find_first_bit(mask, MAX1363_MAX_CHANNELS);
30
31         if (!(test_bit(index, st->current_mode->modemask))) {
32                 ret = -EBUSY;
33                 goto error_ret;
34         }
35
36         ring_data = kmalloc(ring->access->get_bytes_per_datum(ring),
37                             GFP_KERNEL);
38         if (ring_data == NULL) {
39                 ret = -ENOMEM;
40                 goto error_ret;
41         }
42         ret = ring->access->read_last(ring, ring_data);
43         if (ret)
44                 goto error_free_ring_data;
45         /* Need a count of channels prior to this one */
46
47         count = bitmap_weight(mask, index - 1);
48         if (st->chip_info->bits != 8)
49                 ret = ((int)(ring_data[count*2 + 0] & 0x0F) << 8)
50                         + (int)(ring_data[count*2 + 1]);
51         else
52                 ret = ring_data[count];
53
54 error_free_ring_data:
55         kfree(ring_data);
56 error_ret:
57         return ret;
58 }
59
60
61 /**
62  * max1363_ring_preenable() - setup the parameters of the ring before enabling
63  *
64  * The complex nature of the setting of the nuber of bytes per datum is due
65  * to this driver currently ensuring that the timestamp is stored at an 8
66  * byte boundary.
67  **/
68 static int max1363_ring_preenable(struct iio_dev *indio_dev)
69 {
70         struct max1363_state *st = iio_priv(indio_dev);
71         struct iio_ring_buffer *ring = indio_dev->ring;
72         size_t d_size = 0;
73         unsigned long numvals;
74
75         /*
76          * Need to figure out the current mode based upon the requested
77          * scan mask in iio_dev
78          */
79         st->current_mode = max1363_match_mode(ring->scan_mask,
80                                         st->chip_info);
81         if (!st->current_mode)
82                 return -EINVAL;
83
84         max1363_set_scan_mode(st);
85
86         numvals = bitmap_weight(st->current_mode->modemask,
87                                 indio_dev->masklength);
88         if (ring->access->set_bytes_per_datum) {
89                 if (ring->scan_timestamp)
90                         d_size += sizeof(s64);
91                 if (st->chip_info->bits != 8)
92                         d_size += numvals*2;
93                 else
94                         d_size += numvals;
95                 if (ring->scan_timestamp && (d_size % 8))
96                         d_size += 8 - (d_size % 8);
97                 ring->access->set_bytes_per_datum(ring, d_size);
98         }
99
100         return 0;
101 }
102
103 static irqreturn_t max1363_trigger_handler(int irq, void *p)
104 {
105         struct iio_poll_func *pf = p;
106         struct iio_dev *indio_dev = pf->indio_dev;
107         struct max1363_state *st = iio_priv(indio_dev);
108         s64 time_ns;
109         __u8 *rxbuf;
110         int b_sent;
111         size_t d_size;
112         unsigned long numvals = bitmap_weight(st->current_mode->modemask,
113                                               MAX1363_MAX_CHANNELS);
114
115         /* Ensure the timestamp is 8 byte aligned */
116         if (st->chip_info->bits != 8)
117                 d_size = numvals*2 + sizeof(s64);
118         else
119                 d_size = numvals + sizeof(s64);
120         if (d_size % sizeof(s64))
121                 d_size += sizeof(s64) - (d_size % sizeof(s64));
122
123         /* Monitor mode prevents reading. Whilst not currently implemented
124          * might as well have this test in here in the meantime as it does
125          * no harm.
126          */
127         if (numvals == 0)
128                 return IRQ_HANDLED;
129
130         rxbuf = kmalloc(d_size, GFP_KERNEL);
131         if (rxbuf == NULL)
132                 return -ENOMEM;
133         if (st->chip_info->bits != 8)
134                 b_sent = i2c_master_recv(st->client, rxbuf, numvals*2);
135         else
136                 b_sent = i2c_master_recv(st->client, rxbuf, numvals);
137         if (b_sent < 0)
138                 goto done;
139
140         time_ns = iio_get_time_ns();
141
142         memcpy(rxbuf + d_size - sizeof(s64), &time_ns, sizeof(time_ns));
143
144         indio_dev->ring->access->store_to(indio_dev->ring, rxbuf, time_ns);
145 done:
146         iio_trigger_notify_done(indio_dev->trig);
147         kfree(rxbuf);
148
149         return IRQ_HANDLED;
150 }
151
152 static const struct iio_ring_setup_ops max1363_ring_setup_ops = {
153         .postenable = &iio_triggered_buffer_postenable,
154         .preenable = &max1363_ring_preenable,
155         .predisable = &iio_triggered_buffer_predisable,
156 };
157
158 int max1363_register_ring_funcs_and_init(struct iio_dev *indio_dev)
159 {
160         struct max1363_state *st = iio_priv(indio_dev);
161         int ret = 0;
162
163         indio_dev->ring = iio_sw_rb_allocate(indio_dev);
164         if (!indio_dev->ring) {
165                 ret = -ENOMEM;
166                 goto error_ret;
167         }
168         indio_dev->pollfunc = iio_alloc_pollfunc(NULL,
169                                                  &max1363_trigger_handler,
170                                                  IRQF_ONESHOT,
171                                                  indio_dev,
172                                                  "%s_consumer%d",
173                                                  st->client->name,
174                                                  indio_dev->id);
175         if (indio_dev->pollfunc == NULL) {
176                 ret = -ENOMEM;
177                 goto error_deallocate_sw_rb;
178         }
179         /* Effectively select the ring buffer implementation */
180         indio_dev->ring->access = &ring_sw_access_funcs;
181         /* Ring buffer functions - here trigger setup related */
182         indio_dev->ring->setup_ops = &max1363_ring_setup_ops;
183
184         /* Flag that polled ring buffering is possible */
185         indio_dev->modes |= INDIO_RING_TRIGGERED;
186
187         return 0;
188
189 error_deallocate_sw_rb:
190         iio_sw_rb_free(indio_dev->ring);
191 error_ret:
192         return ret;
193 }
194
195 void max1363_ring_cleanup(struct iio_dev *indio_dev)
196 {
197         /* ensure that the trigger has been detached */
198         iio_dealloc_pollfunc(indio_dev->pollfunc);
199         iio_sw_rb_free(indio_dev->ring);
200 }