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 "../buffer.h"
19 #include "../ring_sw.h"
20 #include "../trigger_consumer.h"
24 int max1363_update_scan_mode(struct iio_dev *indio_dev,
25 const unsigned long *scan_mask)
27 struct max1363_state *st = iio_priv(indio_dev);
30 * Need to figure out the current mode based upon the requested
31 * scan mask in iio_dev
33 st->current_mode = max1363_match_mode(scan_mask, st->chip_info);
34 if (!st->current_mode)
36 max1363_set_scan_mode(st);
40 static irqreturn_t max1363_trigger_handler(int irq, void *p)
42 struct iio_poll_func *pf = p;
43 struct iio_dev *indio_dev = pf->indio_dev;
44 struct max1363_state *st = iio_priv(indio_dev);
49 unsigned long numvals = bitmap_weight(st->current_mode->modemask,
50 MAX1363_MAX_CHANNELS);
52 /* Ensure the timestamp is 8 byte aligned */
53 if (st->chip_info->bits != 8)
57 if (indio_dev->buffer->scan_timestamp) {
58 d_size += sizeof(s64);
59 if (d_size % sizeof(s64))
60 d_size += sizeof(s64) - (d_size % sizeof(s64));
62 /* Monitor mode prevents reading. Whilst not currently implemented
63 * might as well have this test in here in the meantime as it does
69 rxbuf = kmalloc(d_size, GFP_KERNEL);
72 if (st->chip_info->bits != 8)
73 b_sent = i2c_master_recv(st->client, rxbuf, numvals*2);
75 b_sent = i2c_master_recv(st->client, rxbuf, numvals);
79 time_ns = iio_get_time_ns();
81 if (indio_dev->buffer->scan_timestamp)
82 memcpy(rxbuf + d_size - sizeof(s64), &time_ns, sizeof(time_ns));
83 iio_push_to_buffer(indio_dev->buffer, rxbuf, time_ns);
86 iio_trigger_notify_done(indio_dev->trig);
92 static const struct iio_buffer_setup_ops max1363_ring_setup_ops = {
93 .postenable = &iio_triggered_buffer_postenable,
94 .preenable = &iio_sw_buffer_preenable,
95 .predisable = &iio_triggered_buffer_predisable,
98 int max1363_register_ring_funcs_and_init(struct iio_dev *indio_dev)
100 struct max1363_state *st = iio_priv(indio_dev);
103 indio_dev->buffer = iio_sw_rb_allocate(indio_dev);
104 if (!indio_dev->buffer) {
108 indio_dev->pollfunc = iio_alloc_pollfunc(NULL,
109 &max1363_trigger_handler,
115 if (indio_dev->pollfunc == NULL) {
117 goto error_deallocate_sw_rb;
119 /* Effectively select the ring buffer implementation */
120 indio_dev->buffer->access = &ring_sw_access_funcs;
121 /* Ring buffer functions - here trigger setup related */
122 indio_dev->setup_ops = &max1363_ring_setup_ops;
124 /* Flag that polled ring buffering is possible */
125 indio_dev->modes |= INDIO_BUFFER_TRIGGERED;
129 error_deallocate_sw_rb:
130 iio_sw_rb_free(indio_dev->buffer);
135 void max1363_ring_cleanup(struct iio_dev *indio_dev)
137 /* ensure that the trigger has been detached */
138 iio_dealloc_pollfunc(indio_dev->pollfunc);
139 iio_sw_rb_free(indio_dev->buffer);