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/kernel.h>
16 #include <linux/sysfs.h>
17 #include <linux/list.h>
18 #include <linux/i2c.h>
21 #include "../ring_generic.h"
22 #include "../ring_sw.h"
23 #include "../trigger.h"
28 ssize_t max1363_scan_from_ring(struct device *dev,
29 struct device_attribute *attr,
32 struct iio_dev *dev_info = dev_get_drvdata(dev);
33 struct max1363_state *info = dev_info->dev_data;
37 ring_data = kmalloc(info->current_mode->numvals*2, GFP_KERNEL);
38 if (ring_data == NULL) {
42 ret = dev_info->ring->access.read_last(dev_info->ring, ring_data);
44 goto error_free_ring_data;
45 len += sprintf(buf+len, "ring ");
46 for (i = 0; i < info->current_mode->numvals; i++)
47 len += sprintf(buf + len, "%d ",
48 ((int)(ring_data[i*2 + 0] & 0x0F) << 8)
49 + ((int)(ring_data[i*2 + 1])));
50 len += sprintf(buf + len, "\n");
62 * max1363_ring_preenable() setup the parameters of the ring before enabling
64 * The complex nature of the setting of the nuber of bytes per datum is due
65 * to this driver currently ensuring that the timestamp is stored at an 8
68 static int max1363_ring_preenable(struct iio_dev *indio_dev)
70 struct max1363_state *st = indio_dev->dev_data;
73 if (indio_dev->ring->access.set_bpd) {
74 d_size = st->current_mode->numvals*2 + sizeof(s64);
76 d_size += 8 - (d_size % 8);
77 indio_dev->ring->access.set_bpd(indio_dev->ring, d_size);
84 * max1363_ring_postenable() typical ring post enable
86 * Only not moved into the core for the hardware ring buffer cases
87 * that are more sophisticated.
89 static int max1363_ring_postenable(struct iio_dev *indio_dev)
91 if (indio_dev->trig == NULL)
93 return iio_trigger_attach_poll_func(indio_dev->trig,
98 * max1363_ring_predisable() runs just prior to ring buffer being disabled
100 * Typical predisable function which ensures that no trigger events can
101 * occur before we disable the ring buffer (and hence would have no idea
102 * what to do with them)
104 static int max1363_ring_predisable(struct iio_dev *indio_dev)
107 return iio_trigger_dettach_poll_func(indio_dev->trig,
108 indio_dev->pollfunc);
114 * max1363_poll_func_th() th of trigger launched polling to ring buffer
116 * As sampling only occurs on i2c comms occuring, leave timestamping until
117 * then. Some triggers will generate their own time stamp. Currently
118 * there is no way of notifying them when no one cares.
120 void max1363_poll_func_th(struct iio_dev *indio_dev)
122 struct max1363_state *st = indio_dev->dev_data;
124 schedule_work(&st->poll_work);
129 * max1363_poll_bh_to_ring() bh of trigger launched polling to ring buffer
130 * @work_s: the work struct through which this was scheduled
132 * Currently there is no option in this driver to disable the saving of
133 * timestamps within the ring.
134 * I think the one copy of this at a time was to avoid problems if the
135 * trigger was set far too high and the reads then locked up the computer.
137 static void max1363_poll_bh_to_ring(struct work_struct *work_s)
139 struct max1363_state *st = container_of(work_s, struct max1363_state,
141 struct iio_dev *indio_dev = st->indio_dev;
142 struct iio_sw_ring_buffer *ring = iio_to_sw_ring(indio_dev->ring);
148 /* Ensure the timestamp is 8 byte aligned */
149 d_size = st->current_mode->numvals*2 + sizeof(s64);
150 if (d_size % sizeof(s64))
151 d_size += sizeof(s64) - (d_size % sizeof(s64));
153 /* Ensure only one copy of this function running at a time */
154 if (atomic_inc_return(&st->protect_ring) > 1)
157 /* Monitor mode prevents reading. Whilst not currently implemented
158 * might as well have this test in here in the meantime as it does
161 if (st->current_mode->numvals == 0)
164 rxbuf = kmalloc(d_size, GFP_KERNEL);
168 b_sent = i2c_master_recv(st->client,
170 st->current_mode->numvals*2);
174 time_ns = iio_get_time_ns();
176 memcpy(rxbuf + d_size - sizeof(s64), &time_ns, sizeof(time_ns));
178 indio_dev->ring->access.store_to(&ring->buf, rxbuf, time_ns);
181 atomic_dec(&st->protect_ring);
185 int max1363_register_ring_funcs_and_init(struct iio_dev *indio_dev)
187 struct max1363_state *st = indio_dev->dev_data;
190 indio_dev->ring = iio_sw_rb_allocate(indio_dev);
191 if (!indio_dev->ring) {
195 /* Effectively select the ring buffer implementation */
196 iio_ring_sw_register_funcs(&st->indio_dev->ring->access);
197 indio_dev->pollfunc = kzalloc(sizeof(*indio_dev->pollfunc), GFP_KERNEL);
198 if (indio_dev->pollfunc == NULL) {
200 goto error_deallocate_sw_rb;
202 /* Configure the polling function called on trigger interrupts */
203 indio_dev->pollfunc->poll_func_main = &max1363_poll_func_th;
204 indio_dev->pollfunc->private_data = indio_dev;
206 /* Ring buffer functions - here trigger setup related */
207 indio_dev->ring->postenable = &max1363_ring_postenable;
208 indio_dev->ring->preenable = &max1363_ring_preenable;
209 indio_dev->ring->predisable = &max1363_ring_predisable;
210 INIT_WORK(&st->poll_work, &max1363_poll_bh_to_ring);
212 /* Flag that polled ring buffering is possible */
213 indio_dev->modes |= INDIO_RING_TRIGGERED;
215 error_deallocate_sw_rb:
216 iio_sw_rb_free(indio_dev->ring);
221 void max1363_ring_cleanup(struct iio_dev *indio_dev)
223 /* ensure that the trigger has been detached */
224 if (indio_dev->trig) {
225 iio_put_trigger(indio_dev->trig);
226 iio_trigger_dettach_poll_func(indio_dev->trig,
227 indio_dev->pollfunc);
229 kfree(indio_dev->pollfunc);
230 iio_sw_rb_free(indio_dev->ring);
233 void max1363_uninitialize_ring(struct iio_ring_buffer *ring)
235 iio_ring_buffer_unregister(ring);
238 int max1363_initialize_ring(struct iio_ring_buffer *ring)
240 return iio_ring_buffer_register(ring);