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/gpio.h>
13 #include <linux/workqueue.h>
14 #include <linux/device.h>
15 #include <linux/slab.h>
16 #include <linux/kernel.h>
17 #include <linux/sysfs.h>
18 #include <linux/list.h>
19 #include <linux/i2c.h>
22 #include "../ring_generic.h"
23 #include "../ring_sw.h"
24 #include "../trigger.h"
29 ssize_t max1363_scan_from_ring(struct device *dev,
30 struct device_attribute *attr,
33 struct iio_dev *dev_info = dev_get_drvdata(dev);
34 struct max1363_state *info = dev_info->dev_data;
38 ring_data = kmalloc(info->current_mode->numvals*2, GFP_KERNEL);
39 if (ring_data == NULL) {
43 ret = dev_info->ring->access.read_last(dev_info->ring, ring_data);
45 goto error_free_ring_data;
46 len += sprintf(buf+len, "ring ");
47 for (i = 0; i < info->current_mode->numvals; i++)
48 len += sprintf(buf + len, "%d ",
49 ((int)(ring_data[i*2 + 0] & 0x0F) << 8)
50 + ((int)(ring_data[i*2 + 1])));
51 len += sprintf(buf + len, "\n");
63 * max1363_ring_preenable() setup the parameters of the ring before enabling
65 * The complex nature of the setting of the nuber of bytes per datum is due
66 * to this driver currently ensuring that the timestamp is stored at an 8
69 static int max1363_ring_preenable(struct iio_dev *indio_dev)
71 struct max1363_state *st = indio_dev->dev_data;
74 if (indio_dev->ring->access.set_bpd) {
75 d_size = st->current_mode->numvals*2 + sizeof(s64);
77 d_size += 8 - (d_size % 8);
78 indio_dev->ring->access.set_bpd(indio_dev->ring, d_size);
85 * max1363_ring_postenable() typical ring post enable
87 * Only not moved into the core for the hardware ring buffer cases
88 * that are more sophisticated.
90 static int max1363_ring_postenable(struct iio_dev *indio_dev)
92 if (indio_dev->trig == NULL)
94 return iio_trigger_attach_poll_func(indio_dev->trig,
99 * max1363_ring_predisable() runs just prior to ring buffer being disabled
101 * Typical predisable function which ensures that no trigger events can
102 * occur before we disable the ring buffer (and hence would have no idea
103 * what to do with them)
105 static int max1363_ring_predisable(struct iio_dev *indio_dev)
108 return iio_trigger_dettach_poll_func(indio_dev->trig,
109 indio_dev->pollfunc);
115 * max1363_poll_func_th() th of trigger launched polling to ring buffer
117 * As sampling only occurs on i2c comms occuring, leave timestamping until
118 * then. Some triggers will generate their own time stamp. Currently
119 * there is no way of notifying them when no one cares.
121 void max1363_poll_func_th(struct iio_dev *indio_dev)
123 struct max1363_state *st = indio_dev->dev_data;
125 schedule_work(&st->poll_work);
130 * max1363_poll_bh_to_ring() bh of trigger launched polling to ring buffer
131 * @work_s: the work struct through which this was scheduled
133 * Currently there is no option in this driver to disable the saving of
134 * timestamps within the ring.
135 * I think the one copy of this at a time was to avoid problems if the
136 * trigger was set far too high and the reads then locked up the computer.
138 static void max1363_poll_bh_to_ring(struct work_struct *work_s)
140 struct max1363_state *st = container_of(work_s, struct max1363_state,
142 struct iio_dev *indio_dev = st->indio_dev;
143 struct iio_sw_ring_buffer *ring = iio_to_sw_ring(indio_dev->ring);
149 /* Ensure the timestamp is 8 byte aligned */
150 d_size = st->current_mode->numvals*2 + sizeof(s64);
151 if (d_size % sizeof(s64))
152 d_size += sizeof(s64) - (d_size % sizeof(s64));
154 /* Ensure only one copy of this function running at a time */
155 if (atomic_inc_return(&st->protect_ring) > 1)
158 /* Monitor mode prevents reading. Whilst not currently implemented
159 * might as well have this test in here in the meantime as it does
162 if (st->current_mode->numvals == 0)
165 rxbuf = kmalloc(d_size, GFP_KERNEL);
169 b_sent = i2c_master_recv(st->client,
171 st->current_mode->numvals*2);
175 time_ns = iio_get_time_ns();
177 memcpy(rxbuf + d_size - sizeof(s64), &time_ns, sizeof(time_ns));
179 indio_dev->ring->access.store_to(&ring->buf, rxbuf, time_ns);
182 atomic_dec(&st->protect_ring);
186 int max1363_register_ring_funcs_and_init(struct iio_dev *indio_dev)
188 struct max1363_state *st = indio_dev->dev_data;
191 indio_dev->ring = iio_sw_rb_allocate(indio_dev);
192 if (!indio_dev->ring) {
196 /* Effectively select the ring buffer implementation */
197 iio_ring_sw_register_funcs(&st->indio_dev->ring->access);
198 indio_dev->pollfunc = kzalloc(sizeof(*indio_dev->pollfunc), GFP_KERNEL);
199 if (indio_dev->pollfunc == NULL) {
201 goto error_deallocate_sw_rb;
203 /* Configure the polling function called on trigger interrupts */
204 indio_dev->pollfunc->poll_func_main = &max1363_poll_func_th;
205 indio_dev->pollfunc->private_data = indio_dev;
207 /* Ring buffer functions - here trigger setup related */
208 indio_dev->ring->postenable = &max1363_ring_postenable;
209 indio_dev->ring->preenable = &max1363_ring_preenable;
210 indio_dev->ring->predisable = &max1363_ring_predisable;
211 INIT_WORK(&st->poll_work, &max1363_poll_bh_to_ring);
213 /* Flag that polled ring buffering is possible */
214 indio_dev->modes |= INDIO_RING_TRIGGERED;
216 error_deallocate_sw_rb:
217 iio_sw_rb_free(indio_dev->ring);
222 void max1363_ring_cleanup(struct iio_dev *indio_dev)
224 /* ensure that the trigger has been detached */
225 if (indio_dev->trig) {
226 iio_put_trigger(indio_dev->trig);
227 iio_trigger_dettach_poll_func(indio_dev->trig,
228 indio_dev->pollfunc);
230 kfree(indio_dev->pollfunc);
231 iio_sw_rb_free(indio_dev->ring);
234 void max1363_uninitialize_ring(struct iio_ring_buffer *ring)
236 iio_ring_buffer_unregister(ring);
239 int max1363_initialize_ring(struct iio_ring_buffer *ring)
241 return iio_ring_buffer_register(ring);