Fix common misspellings
[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{
bf32963c 33 struct iio_ring_buffer *ring = st->indio_dev->ring;
82020b0e
JC
34 int count = 0, ret;
35 u8 *ring_data;
36 if (!(st->current_mode->modemask & mask)) {
37 ret = -EBUSY;
38 goto error_ret;
39 }
0a1231df 40
e1517c00 41 ring_data = kmalloc(ring->access.get_bytes_per_datum(ring), GFP_KERNEL);
0a1231df
JC
42 if (ring_data == NULL) {
43 ret = -ENOMEM;
44 goto error_ret;
45 }
bf32963c 46 ret = ring->access.read_last(ring, ring_data);
0a1231df
JC
47 if (ret)
48 goto error_free_ring_data;
82020b0e
JC
49 /* Need a count of channels prior to this one */
50 mask >>= 1;
51 while (mask) {
81b77f94 52 if (mask & st->current_mode->modemask)
82020b0e
JC
53 count++;
54 mask >>= 1;
55 }
3bf877c1 56 if (st->chip_info->bits != 8)
81b77f94 57 ret = ((int)(ring_data[count*2 + 0] & 0x0F) << 8)
3bf877c1
JC
58 + (int)(ring_data[count*2 + 1]);
59 else
81b77f94 60 ret = ring_data[count];
0a1231df
JC
61
62error_free_ring_data:
63 kfree(ring_data);
64error_ret:
65 return ret;
66}
67
68/**
c40ab874 69 * max1363_ring_preenable() - setup the parameters of the ring before enabling
0a1231df
JC
70 *
71 * The complex nature of the setting of the nuber of bytes per datum is due
72 * to this driver currently ensuring that the timestamp is stored at an 8
73 * byte boundary.
74 **/
75static int max1363_ring_preenable(struct iio_dev *indio_dev)
76{
77 struct max1363_state *st = indio_dev->dev_data;
bf32963c 78 struct iio_ring_buffer *ring = indio_dev->ring;
0a1231df 79 size_t d_size;
82020b0e
JC
80 unsigned long numvals;
81
82 /*
83 * Need to figure out the current mode based upon the requested
84 * scan mask in iio_dev
85 */
bf32963c 86 st->current_mode = max1363_match_mode(ring->scan_mask,
82020b0e
JC
87 st->chip_info);
88 if (!st->current_mode)
89 return -EINVAL;
90
91 max1363_set_scan_mode(st);
0a1231df 92
82020b0e 93 numvals = hweight_long(st->current_mode->modemask);
bf32963c 94 if (ring->access.set_bytes_per_datum) {
3bf877c1
JC
95 if (st->chip_info->bits != 8)
96 d_size = numvals*2 + sizeof(s64);
97 else
98 d_size = numvals + sizeof(s64);
0a1231df
JC
99 if (d_size % 8)
100 d_size += 8 - (d_size % 8);
bf32963c 101 ring->access.set_bytes_per_datum(ring, d_size);
0a1231df
JC
102 }
103
104 return 0;
105}
106
0a1231df
JC
107
108/**
c40ab874 109 * max1363_poll_func_th() - th of trigger launched polling to ring buffer
0a1231df 110 *
25985edc 111 * As sampling only occurs on i2c comms occurring, leave timestamping until
0a1231df
JC
112 * then. Some triggers will generate their own time stamp. Currently
113 * there is no way of notifying them when no one cares.
114 **/
7b2c33b1 115static void max1363_poll_func_th(struct iio_dev *indio_dev, s64 time)
0a1231df
JC
116{
117 struct max1363_state *st = indio_dev->dev_data;
118
119 schedule_work(&st->poll_work);
120
121 return;
122}
123/**
c40ab874 124 * max1363_poll_bh_to_ring() - bh of trigger launched polling to ring buffer
0a1231df
JC
125 * @work_s: the work struct through which this was scheduled
126 *
127 * Currently there is no option in this driver to disable the saving of
128 * timestamps within the ring.
129 * I think the one copy of this at a time was to avoid problems if the
130 * trigger was set far too high and the reads then locked up the computer.
131 **/
132static void max1363_poll_bh_to_ring(struct work_struct *work_s)
133{
134 struct max1363_state *st = container_of(work_s, struct max1363_state,
135 poll_work);
136 struct iio_dev *indio_dev = st->indio_dev;
bf32963c 137 struct iio_sw_ring_buffer *sw_ring = iio_to_sw_ring(indio_dev->ring);
0a1231df
JC
138 s64 time_ns;
139 __u8 *rxbuf;
140 int b_sent;
141 size_t d_size;
82020b0e 142 unsigned long numvals = hweight_long(st->current_mode->modemask);
0a1231df
JC
143
144 /* Ensure the timestamp is 8 byte aligned */
3bf877c1
JC
145 if (st->chip_info->bits != 8)
146 d_size = numvals*2 + sizeof(s64);
147 else
148 d_size = numvals + sizeof(s64);
0a1231df
JC
149 if (d_size % sizeof(s64))
150 d_size += sizeof(s64) - (d_size % sizeof(s64));
151
152 /* Ensure only one copy of this function running at a time */
153 if (atomic_inc_return(&st->protect_ring) > 1)
154 return;
155
156 /* Monitor mode prevents reading. Whilst not currently implemented
157 * might as well have this test in here in the meantime as it does
158 * no harm.
159 */
82020b0e 160 if (numvals == 0)
0a1231df
JC
161 return;
162
163 rxbuf = kmalloc(d_size, GFP_KERNEL);
164 if (rxbuf == NULL)
165 return;
3bf877c1
JC
166 if (st->chip_info->bits != 8)
167 b_sent = i2c_master_recv(st->client, rxbuf, numvals*2);
168 else
169 b_sent = i2c_master_recv(st->client, rxbuf, numvals);
0a1231df
JC
170 if (b_sent < 0)
171 goto done;
172
173 time_ns = iio_get_time_ns();
174
175 memcpy(rxbuf + d_size - sizeof(s64), &time_ns, sizeof(time_ns));
176
bf32963c 177 indio_dev->ring->access.store_to(&sw_ring->buf, rxbuf, time_ns);
0a1231df
JC
178done:
179 kfree(rxbuf);
180 atomic_dec(&st->protect_ring);
181}
182
183
184int max1363_register_ring_funcs_and_init(struct iio_dev *indio_dev)
185{
186 struct max1363_state *st = indio_dev->dev_data;
187 int ret = 0;
188
189 indio_dev->ring = iio_sw_rb_allocate(indio_dev);
190 if (!indio_dev->ring) {
191 ret = -ENOMEM;
192 goto error_ret;
193 }
194 /* Effectively select the ring buffer implementation */
bf32963c 195 iio_ring_sw_register_funcs(&indio_dev->ring->access);
15744090
JC
196 ret = iio_alloc_pollfunc(indio_dev, NULL, &max1363_poll_func_th);
197 if (ret)
0a1231df 198 goto error_deallocate_sw_rb;
0a1231df
JC
199
200 /* Ring buffer functions - here trigger setup related */
bf32963c 201 indio_dev->ring->scan_el_attrs = st->chip_info->scan_attrs;
c3db00cc 202 indio_dev->ring->postenable = &iio_triggered_ring_postenable;
0a1231df 203 indio_dev->ring->preenable = &max1363_ring_preenable;
c3db00cc 204 indio_dev->ring->predisable = &iio_triggered_ring_predisable;
0a1231df
JC
205 INIT_WORK(&st->poll_work, &max1363_poll_bh_to_ring);
206
207 /* Flag that polled ring buffering is possible */
208 indio_dev->modes |= INDIO_RING_TRIGGERED;
209 return 0;
210error_deallocate_sw_rb:
211 iio_sw_rb_free(indio_dev->ring);
212error_ret:
213 return ret;
214}
215
216void max1363_ring_cleanup(struct iio_dev *indio_dev)
217{
218 /* ensure that the trigger has been detached */
219 if (indio_dev->trig) {
220 iio_put_trigger(indio_dev->trig);
221 iio_trigger_dettach_poll_func(indio_dev->trig,
222 indio_dev->pollfunc);
223 }
224 kfree(indio_dev->pollfunc);
225 iio_sw_rb_free(indio_dev->ring);
226}
This page took 0.170895 seconds and 5 git commands to generate.