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/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>
21 #include "../ring_generic.h"
22 #include "../ring_sw.h"
23 #include "../trigger.h"
28 int max1363_single_channel_from_ring(long mask, struct max1363_state *st)
30 struct iio_ring_buffer *ring = iio_priv_to_dev(st)->ring;
33 if (!(st->current_mode->modemask & mask)) {
38 ring_data = kmalloc(ring->access->get_bytes_per_datum(ring),
40 if (ring_data == NULL) {
44 ret = ring->access->read_last(ring, ring_data);
46 goto error_free_ring_data;
47 /* Need a count of channels prior to this one */
50 if (mask & st->current_mode->modemask)
54 if (st->chip_info->bits != 8)
55 ret = ((int)(ring_data[count*2 + 0] & 0x0F) << 8)
56 + (int)(ring_data[count*2 + 1]);
58 ret = ring_data[count];
68 * max1363_ring_preenable() - setup the parameters of the ring before enabling
70 * The complex nature of the setting of the nuber of bytes per datum is due
71 * to this driver currently ensuring that the timestamp is stored at an 8
74 static int max1363_ring_preenable(struct iio_dev *indio_dev)
76 struct max1363_state *st = iio_priv(indio_dev);
77 struct iio_ring_buffer *ring = indio_dev->ring;
79 unsigned long numvals;
82 * Need to figure out the current mode based upon the requested
83 * scan mask in iio_dev
85 st->current_mode = max1363_match_mode(ring->scan_mask,
87 if (!st->current_mode)
90 max1363_set_scan_mode(st);
92 numvals = hweight_long(st->current_mode->modemask);
93 if (ring->access->set_bytes_per_datum) {
94 if (ring->scan_timestamp)
95 d_size += sizeof(s64);
96 if (st->chip_info->bits != 8)
100 if (ring->scan_timestamp && (d_size % 8))
101 d_size += 8 - (d_size % 8);
102 ring->access->set_bytes_per_datum(ring, d_size);
108 static irqreturn_t max1363_trigger_handler(int irq, void *p)
110 struct iio_poll_func *pf = p;
111 struct iio_dev *indio_dev = pf->private_data;
112 struct max1363_state *st = iio_priv(indio_dev);
117 unsigned long numvals = hweight_long(st->current_mode->modemask);
119 /* Ensure the timestamp is 8 byte aligned */
120 if (st->chip_info->bits != 8)
121 d_size = numvals*2 + sizeof(s64);
123 d_size = numvals + sizeof(s64);
124 if (d_size % sizeof(s64))
125 d_size += sizeof(s64) - (d_size % sizeof(s64));
127 /* Monitor mode prevents reading. Whilst not currently implemented
128 * might as well have this test in here in the meantime as it does
134 rxbuf = kmalloc(d_size, GFP_KERNEL);
137 if (st->chip_info->bits != 8)
138 b_sent = i2c_master_recv(st->client, rxbuf, numvals*2);
140 b_sent = i2c_master_recv(st->client, rxbuf, numvals);
144 time_ns = iio_get_time_ns();
146 memcpy(rxbuf + d_size - sizeof(s64), &time_ns, sizeof(time_ns));
148 indio_dev->ring->access->store_to(indio_dev->ring, rxbuf, time_ns);
150 iio_trigger_notify_done(indio_dev->trig);
156 static const struct iio_ring_setup_ops max1363_ring_setup_ops = {
157 .postenable = &iio_triggered_ring_postenable,
158 .preenable = &max1363_ring_preenable,
159 .predisable = &iio_triggered_ring_predisable,
162 int max1363_register_ring_funcs_and_init(struct iio_dev *indio_dev)
164 struct max1363_state *st = iio_priv(indio_dev);
167 indio_dev->ring = iio_sw_rb_allocate(indio_dev);
168 if (!indio_dev->ring) {
172 indio_dev->pollfunc = iio_alloc_pollfunc(NULL,
173 &max1363_trigger_handler,
179 if (indio_dev->pollfunc == NULL) {
181 goto error_deallocate_sw_rb;
183 /* Effectively select the ring buffer implementation */
184 indio_dev->ring->access = &ring_sw_access_funcs;
185 /* Ring buffer functions - here trigger setup related */
186 indio_dev->ring->setup_ops = &max1363_ring_setup_ops;
188 /* Flag that polled ring buffering is possible */
189 indio_dev->modes |= INDIO_RING_TRIGGERED;
193 error_deallocate_sw_rb:
194 iio_sw_rb_free(indio_dev->ring);
199 void max1363_ring_cleanup(struct iio_dev *indio_dev)
201 /* ensure that the trigger has been detached */
202 if (indio_dev->trig) {
203 iio_put_trigger(indio_dev->trig);
204 iio_trigger_dettach_poll_func(indio_dev->trig,
205 indio_dev->pollfunc);
207 iio_dealloc_pollfunc(indio_dev->pollfunc);
208 iio_sw_rb_free(indio_dev->ring);