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];
67 * max1363_ring_preenable() - setup the parameters of the ring before enabling
69 * The complex nature of the setting of the nuber of bytes per datum is due
70 * to this driver currently ensuring that the timestamp is stored at an 8
73 static int max1363_ring_preenable(struct iio_dev *indio_dev)
75 struct max1363_state *st = iio_priv(indio_dev);
76 struct iio_ring_buffer *ring = indio_dev->ring;
78 unsigned long numvals;
81 * Need to figure out the current mode based upon the requested
82 * scan mask in iio_dev
84 st->current_mode = max1363_match_mode(ring->scan_mask,
86 if (!st->current_mode)
89 max1363_set_scan_mode(st);
91 numvals = hweight_long(st->current_mode->modemask);
92 if (ring->access->set_bytes_per_datum) {
93 if (ring->scan_timestamp)
94 d_size += sizeof(s64);
95 if (st->chip_info->bits != 8)
99 if (ring->scan_timestamp && (d_size % 8))
100 d_size += 8 - (d_size % 8);
101 ring->access->set_bytes_per_datum(ring, d_size);
107 static irqreturn_t max1363_trigger_handler(int irq, void *p)
109 struct iio_poll_func *pf = p;
110 struct iio_dev *indio_dev = pf->private_data;
111 struct max1363_state *st = iio_priv(indio_dev);
116 unsigned long numvals = hweight_long(st->current_mode->modemask);
118 /* Ensure the timestamp is 8 byte aligned */
119 if (st->chip_info->bits != 8)
120 d_size = numvals*2 + sizeof(s64);
122 d_size = numvals + sizeof(s64);
123 if (d_size % sizeof(s64))
124 d_size += sizeof(s64) - (d_size % sizeof(s64));
126 /* Monitor mode prevents reading. Whilst not currently implemented
127 * might as well have this test in here in the meantime as it does
133 rxbuf = kmalloc(d_size, GFP_KERNEL);
136 if (st->chip_info->bits != 8)
137 b_sent = i2c_master_recv(st->client, rxbuf, numvals*2);
139 b_sent = i2c_master_recv(st->client, rxbuf, numvals);
143 time_ns = iio_get_time_ns();
145 memcpy(rxbuf + d_size - sizeof(s64), &time_ns, sizeof(time_ns));
147 indio_dev->ring->access->store_to(indio_dev->ring, rxbuf, time_ns);
149 iio_trigger_notify_done(indio_dev->trig);
155 static const struct iio_ring_setup_ops max1363_ring_setup_ops = {
156 .postenable = &iio_triggered_ring_postenable,
157 .preenable = &max1363_ring_preenable,
158 .predisable = &iio_triggered_ring_predisable,
161 int max1363_register_ring_funcs_and_init(struct iio_dev *indio_dev)
163 struct max1363_state *st = iio_priv(indio_dev);
166 indio_dev->ring = iio_sw_rb_allocate(indio_dev);
167 if (!indio_dev->ring) {
171 indio_dev->pollfunc = kzalloc(sizeof(*indio_dev->pollfunc), GFP_KERNEL);
172 if (indio_dev->pollfunc == NULL) {
174 goto error_deallocate_sw_rb;
176 indio_dev->pollfunc->private_data = indio_dev;
177 indio_dev->pollfunc->thread = &max1363_trigger_handler;
178 indio_dev->pollfunc->type = IRQF_ONESHOT;
179 indio_dev->pollfunc->name =
180 kasprintf(GFP_KERNEL, "%s_consumer%d",
181 st->client->name, indio_dev->id);
182 if (indio_dev->pollfunc->name == NULL) {
184 goto error_free_pollfunc;
186 /* Effectively select the ring buffer implementation */
187 indio_dev->ring->access = &ring_sw_access_funcs;
188 /* Ring buffer functions - here trigger setup related */
189 indio_dev->ring->setup_ops = &max1363_ring_setup_ops;
191 /* Flag that polled ring buffering is possible */
192 indio_dev->modes |= INDIO_RING_TRIGGERED;
196 kfree(indio_dev->pollfunc);
197 error_deallocate_sw_rb:
198 iio_sw_rb_free(indio_dev->ring);
203 void max1363_ring_cleanup(struct iio_dev *indio_dev)
205 /* ensure that the trigger has been detached */
206 if (indio_dev->trig) {
207 iio_put_trigger(indio_dev->trig);
208 iio_trigger_dettach_poll_func(indio_dev->trig,
209 indio_dev->pollfunc);
211 kfree(indio_dev->pollfunc->name);
212 kfree(indio_dev->pollfunc);
213 iio_sw_rb_free(indio_dev->ring);