2 * Copyright (C) 2008 Jonathan Cameron
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.
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>
18 #include "../ring_generic.h"
19 #include "../ring_sw.h"
20 #include "../trigger.h"
24 int max1363_single_channel_from_ring(long mask, struct max1363_state *st)
26 struct iio_ring_buffer *ring = iio_priv_to_dev(st)->ring;
29 if (!(st->current_mode->modemask & mask)) {
34 ring_data = kmalloc(ring->access->get_bytes_per_datum(ring),
36 if (ring_data == NULL) {
40 ret = ring->access->read_last(ring, ring_data);
42 goto error_free_ring_data;
43 /* Need a count of channels prior to this one */
46 if (mask & st->current_mode->modemask)
50 if (st->chip_info->bits != 8)
51 ret = ((int)(ring_data[count*2 + 0] & 0x0F) << 8)
52 + (int)(ring_data[count*2 + 1]);
54 ret = ring_data[count];
64 * max1363_ring_preenable() - setup the parameters of the ring before enabling
66 * The complex nature of the setting of the nuber of bytes per datum is due
67 * to this driver currently ensuring that the timestamp is stored at an 8
70 static int max1363_ring_preenable(struct iio_dev *indio_dev)
72 struct max1363_state *st = iio_priv(indio_dev);
73 struct iio_ring_buffer *ring = indio_dev->ring;
75 unsigned long numvals;
78 * Need to figure out the current mode based upon the requested
79 * scan mask in iio_dev
81 st->current_mode = max1363_match_mode(ring->scan_mask,
83 if (!st->current_mode)
86 max1363_set_scan_mode(st);
88 numvals = hweight_long(st->current_mode->modemask);
89 if (ring->access->set_bytes_per_datum) {
90 if (ring->scan_timestamp)
91 d_size += sizeof(s64);
92 if (st->chip_info->bits != 8)
96 if (ring->scan_timestamp && (d_size % 8))
97 d_size += 8 - (d_size % 8);
98 ring->access->set_bytes_per_datum(ring, d_size);
104 static irqreturn_t max1363_trigger_handler(int irq, void *p)
106 struct iio_poll_func *pf = p;
107 struct iio_dev *indio_dev = pf->private_data;
108 struct max1363_state *st = iio_priv(indio_dev);
113 unsigned long numvals = hweight_long(st->current_mode->modemask);
115 /* Ensure the timestamp is 8 byte aligned */
116 if (st->chip_info->bits != 8)
117 d_size = numvals*2 + sizeof(s64);
119 d_size = numvals + sizeof(s64);
120 if (d_size % sizeof(s64))
121 d_size += sizeof(s64) - (d_size % sizeof(s64));
123 /* Monitor mode prevents reading. Whilst not currently implemented
124 * might as well have this test in here in the meantime as it does
130 rxbuf = kmalloc(d_size, GFP_KERNEL);
133 if (st->chip_info->bits != 8)
134 b_sent = i2c_master_recv(st->client, rxbuf, numvals*2);
136 b_sent = i2c_master_recv(st->client, rxbuf, numvals);
140 time_ns = iio_get_time_ns();
142 memcpy(rxbuf + d_size - sizeof(s64), &time_ns, sizeof(time_ns));
144 indio_dev->ring->access->store_to(indio_dev->ring, rxbuf, time_ns);
146 iio_trigger_notify_done(indio_dev->trig);
152 static const struct iio_ring_setup_ops max1363_ring_setup_ops = {
153 .postenable = &iio_triggered_ring_postenable,
154 .preenable = &max1363_ring_preenable,
155 .predisable = &iio_triggered_ring_predisable,
158 int max1363_register_ring_funcs_and_init(struct iio_dev *indio_dev)
160 struct max1363_state *st = iio_priv(indio_dev);
163 indio_dev->ring = iio_sw_rb_allocate(indio_dev);
164 if (!indio_dev->ring) {
168 indio_dev->pollfunc = iio_alloc_pollfunc(NULL,
169 &max1363_trigger_handler,
175 if (indio_dev->pollfunc == NULL) {
177 goto error_deallocate_sw_rb;
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;
184 /* Flag that polled ring buffering is possible */
185 indio_dev->modes |= INDIO_RING_TRIGGERED;
189 error_deallocate_sw_rb:
190 iio_sw_rb_free(indio_dev->ring);
195 void max1363_ring_cleanup(struct iio_dev *indio_dev)
197 /* ensure that the trigger has been detached */
198 if (indio_dev->trig) {
199 iio_put_trigger(indio_dev->trig);
200 iio_trigger_dettach_poll_func(indio_dev->trig,
201 indio_dev->pollfunc);
203 iio_dealloc_pollfunc(indio_dev->pollfunc);
204 iio_sw_rb_free(indio_dev->ring);