staging:iio:max1363 use device_id instead of searching on name again
[deliverable/linux.git] / drivers / staging / iio / adc / max1363_ring.c
CommitLineData
0a1231df
JC
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/gpio.h>
13#include <linux/workqueue.h>
14#include <linux/device.h>
5a0e3ad6 15#include <linux/slab.h>
0a1231df
JC
16#include <linux/kernel.h>
17#include <linux/sysfs.h>
18#include <linux/list.h>
19#include <linux/i2c.h>
82020b0e 20#include <linux/bitops.h>
0a1231df
JC
21
22#include "../iio.h"
23#include "../ring_generic.h"
24#include "../ring_sw.h"
25#include "../trigger.h"
26#include "../sysfs.h"
27
28#include "max1363.h"
29
82020b0e
JC
30/* Todo: test this */
31int max1363_single_channel_from_ring(long mask, struct max1363_state *st)
0a1231df 32{
82020b0e
JC
33 unsigned long numvals;
34 int count = 0, ret;
35 u8 *ring_data;
36 if (!(st->current_mode->modemask & mask)) {
37 ret = -EBUSY;
38 goto error_ret;
39 }
40 numvals = hweight_long(st->current_mode->modemask);
0a1231df 41
82020b0e 42 ring_data = kmalloc(numvals*2, GFP_KERNEL);
0a1231df
JC
43 if (ring_data == NULL) {
44 ret = -ENOMEM;
45 goto error_ret;
46 }
82020b0e
JC
47 ret = st->indio_dev->ring->access.read_last(st->indio_dev->ring,
48 ring_data);
0a1231df
JC
49 if (ret)
50 goto error_free_ring_data;
82020b0e
JC
51 /* Need a count of channels prior to this one */
52 mask >>= 1;
53 while (mask) {
81b77f94 54 if (mask & st->current_mode->modemask)
82020b0e
JC
55 count++;
56 mask >>= 1;
57 }
3bf877c1 58 if (st->chip_info->bits != 8)
81b77f94 59 ret = ((int)(ring_data[count*2 + 0] & 0x0F) << 8)
3bf877c1
JC
60 + (int)(ring_data[count*2 + 1]);
61 else
81b77f94 62 ret = ring_data[count];
0a1231df
JC
63
64error_free_ring_data:
65 kfree(ring_data);
66error_ret:
67 return ret;
68}
69
70/**
71 * max1363_ring_preenable() setup the parameters of the ring before enabling
72 *
73 * The complex nature of the setting of the nuber of bytes per datum is due
74 * to this driver currently ensuring that the timestamp is stored at an 8
75 * byte boundary.
76 **/
77static int max1363_ring_preenable(struct iio_dev *indio_dev)
78{
79 struct max1363_state *st = indio_dev->dev_data;
80 size_t d_size;
82020b0e
JC
81 unsigned long numvals;
82
83 /*
84 * Need to figure out the current mode based upon the requested
85 * scan mask in iio_dev
86 */
87 st->current_mode = max1363_match_mode(st->indio_dev->scan_mask,
88 st->chip_info);
89 if (!st->current_mode)
90 return -EINVAL;
91
92 max1363_set_scan_mode(st);
0a1231df 93
82020b0e 94 numvals = hweight_long(st->current_mode->modemask);
0a1231df 95 if (indio_dev->ring->access.set_bpd) {
3bf877c1
JC
96 if (st->chip_info->bits != 8)
97 d_size = numvals*2 + sizeof(s64);
98 else
99 d_size = numvals + sizeof(s64);
0a1231df
JC
100 if (d_size % 8)
101 d_size += 8 - (d_size % 8);
102 indio_dev->ring->access.set_bpd(indio_dev->ring, d_size);
103 }
104
105 return 0;
106}
107
108/**
109 * max1363_ring_postenable() typical ring post enable
110 *
111 * Only not moved into the core for the hardware ring buffer cases
112 * that are more sophisticated.
113 **/
114static int max1363_ring_postenable(struct iio_dev *indio_dev)
115{
116 if (indio_dev->trig == NULL)
117 return 0;
118 return iio_trigger_attach_poll_func(indio_dev->trig,
119 indio_dev->pollfunc);
120}
121
122/**
123 * max1363_ring_predisable() runs just prior to ring buffer being disabled
124 *
125 * Typical predisable function which ensures that no trigger events can
126 * occur before we disable the ring buffer (and hence would have no idea
127 * what to do with them)
128 **/
129static int max1363_ring_predisable(struct iio_dev *indio_dev)
130{
131 if (indio_dev->trig)
132 return iio_trigger_dettach_poll_func(indio_dev->trig,
133 indio_dev->pollfunc);
134 else
135 return 0;
136}
137
138/**
139 * max1363_poll_func_th() th of trigger launched polling to ring buffer
140 *
141 * As sampling only occurs on i2c comms occuring, leave timestamping until
142 * then. Some triggers will generate their own time stamp. Currently
143 * there is no way of notifying them when no one cares.
144 **/
acbbfe23 145static void max1363_poll_func_th(struct iio_dev *indio_dev)
0a1231df
JC
146{
147 struct max1363_state *st = indio_dev->dev_data;
148
149 schedule_work(&st->poll_work);
150
151 return;
152}
153/**
154 * max1363_poll_bh_to_ring() bh of trigger launched polling to ring buffer
155 * @work_s: the work struct through which this was scheduled
156 *
157 * Currently there is no option in this driver to disable the saving of
158 * timestamps within the ring.
159 * I think the one copy of this at a time was to avoid problems if the
160 * trigger was set far too high and the reads then locked up the computer.
161 **/
162static void max1363_poll_bh_to_ring(struct work_struct *work_s)
163{
164 struct max1363_state *st = container_of(work_s, struct max1363_state,
165 poll_work);
166 struct iio_dev *indio_dev = st->indio_dev;
167 struct iio_sw_ring_buffer *ring = iio_to_sw_ring(indio_dev->ring);
168 s64 time_ns;
169 __u8 *rxbuf;
170 int b_sent;
171 size_t d_size;
82020b0e 172 unsigned long numvals = hweight_long(st->current_mode->modemask);
0a1231df
JC
173
174 /* Ensure the timestamp is 8 byte aligned */
3bf877c1
JC
175 if (st->chip_info->bits != 8)
176 d_size = numvals*2 + sizeof(s64);
177 else
178 d_size = numvals + sizeof(s64);
0a1231df
JC
179 if (d_size % sizeof(s64))
180 d_size += sizeof(s64) - (d_size % sizeof(s64));
181
182 /* Ensure only one copy of this function running at a time */
183 if (atomic_inc_return(&st->protect_ring) > 1)
184 return;
185
186 /* Monitor mode prevents reading. Whilst not currently implemented
187 * might as well have this test in here in the meantime as it does
188 * no harm.
189 */
82020b0e 190 if (numvals == 0)
0a1231df
JC
191 return;
192
193 rxbuf = kmalloc(d_size, GFP_KERNEL);
194 if (rxbuf == NULL)
195 return;
3bf877c1
JC
196 if (st->chip_info->bits != 8)
197 b_sent = i2c_master_recv(st->client, rxbuf, numvals*2);
198 else
199 b_sent = i2c_master_recv(st->client, rxbuf, numvals);
0a1231df
JC
200 if (b_sent < 0)
201 goto done;
202
203 time_ns = iio_get_time_ns();
204
205 memcpy(rxbuf + d_size - sizeof(s64), &time_ns, sizeof(time_ns));
206
207 indio_dev->ring->access.store_to(&ring->buf, rxbuf, time_ns);
208done:
209 kfree(rxbuf);
210 atomic_dec(&st->protect_ring);
211}
212
213
214int max1363_register_ring_funcs_and_init(struct iio_dev *indio_dev)
215{
216 struct max1363_state *st = indio_dev->dev_data;
217 int ret = 0;
218
219 indio_dev->ring = iio_sw_rb_allocate(indio_dev);
220 if (!indio_dev->ring) {
221 ret = -ENOMEM;
222 goto error_ret;
223 }
224 /* Effectively select the ring buffer implementation */
225 iio_ring_sw_register_funcs(&st->indio_dev->ring->access);
226 indio_dev->pollfunc = kzalloc(sizeof(*indio_dev->pollfunc), GFP_KERNEL);
227 if (indio_dev->pollfunc == NULL) {
228 ret = -ENOMEM;
229 goto error_deallocate_sw_rb;
230 }
231 /* Configure the polling function called on trigger interrupts */
232 indio_dev->pollfunc->poll_func_main = &max1363_poll_func_th;
233 indio_dev->pollfunc->private_data = indio_dev;
234
235 /* Ring buffer functions - here trigger setup related */
236 indio_dev->ring->postenable = &max1363_ring_postenable;
237 indio_dev->ring->preenable = &max1363_ring_preenable;
238 indio_dev->ring->predisable = &max1363_ring_predisable;
239 INIT_WORK(&st->poll_work, &max1363_poll_bh_to_ring);
240
241 /* Flag that polled ring buffering is possible */
242 indio_dev->modes |= INDIO_RING_TRIGGERED;
243 return 0;
244error_deallocate_sw_rb:
245 iio_sw_rb_free(indio_dev->ring);
246error_ret:
247 return ret;
248}
249
250void max1363_ring_cleanup(struct iio_dev *indio_dev)
251{
252 /* ensure that the trigger has been detached */
253 if (indio_dev->trig) {
254 iio_put_trigger(indio_dev->trig);
255 iio_trigger_dettach_poll_func(indio_dev->trig,
256 indio_dev->pollfunc);
257 }
258 kfree(indio_dev->pollfunc);
259 iio_sw_rb_free(indio_dev->ring);
260}
261
262void max1363_uninitialize_ring(struct iio_ring_buffer *ring)
263{
264 iio_ring_buffer_unregister(ring);
c608cb01 265}
0a1231df
JC
266
267int max1363_initialize_ring(struct iio_ring_buffer *ring)
268{
758d988c 269 return iio_ring_buffer_register(ring, 0);
c608cb01 270}
This page took 0.109967 seconds and 5 git commands to generate.