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>
20 #include <linux/bitops.h>
23 #include "../ring_generic.h"
24 #include "../ring_sw.h"
25 #include "../trigger.h"
31 int max1363_single_channel_from_ring(long mask
, struct max1363_state
*st
)
33 struct iio_ring_buffer
*ring
= st
->indio_dev
->ring
;
36 if (!(st
->current_mode
->modemask
& mask
)) {
41 ring_data
= kmalloc(ring
->access
.get_bytes_per_datum(ring
), GFP_KERNEL
);
42 if (ring_data
== NULL
) {
46 ret
= ring
->access
.read_last(ring
, ring_data
);
48 goto error_free_ring_data
;
49 /* Need a count of channels prior to this one */
52 if (mask
& st
->current_mode
->modemask
)
56 if (st
->chip_info
->bits
!= 8)
57 ret
= ((int)(ring_data
[count
*2 + 0] & 0x0F) << 8)
58 + (int)(ring_data
[count
*2 + 1]);
60 ret
= ring_data
[count
];
69 * max1363_ring_preenable() - setup the parameters of the ring before enabling
71 * The complex nature of the setting of the nuber of bytes per datum is due
72 * to this driver currently ensuring that the timestamp is stored at an 8
75 static int max1363_ring_preenable(struct iio_dev
*indio_dev
)
77 struct max1363_state
*st
= indio_dev
->dev_data
;
78 struct iio_ring_buffer
*ring
= indio_dev
->ring
;
80 unsigned long numvals
;
83 * Need to figure out the current mode based upon the requested
84 * scan mask in iio_dev
86 st
->current_mode
= max1363_match_mode(ring
->scan_mask
,
88 if (!st
->current_mode
)
91 max1363_set_scan_mode(st
);
93 numvals
= hweight_long(st
->current_mode
->modemask
);
94 if (ring
->access
.set_bytes_per_datum
) {
95 if (ring
->scan_timestamp
)
96 d_size
+= sizeof(s64
);
97 if (st
->chip_info
->bits
!= 8)
101 if (ring
->scan_timestamp
&& (d_size
% 8))
102 d_size
+= 8 - (d_size
% 8);
103 ring
->access
.set_bytes_per_datum(ring
, d_size
);
111 * max1363_poll_func_th() - th of trigger launched polling to ring buffer
113 * As sampling only occurs on i2c comms occurring, leave timestamping until
114 * then. Some triggers will generate their own time stamp. Currently
115 * there is no way of notifying them when no one cares.
117 static void max1363_poll_func_th(struct iio_dev
*indio_dev
, s64 time
)
119 struct max1363_state
*st
= indio_dev
->dev_data
;
121 schedule_work(&st
->poll_work
);
126 * max1363_poll_bh_to_ring() - bh of trigger launched polling to ring buffer
127 * @work_s: the work struct through which this was scheduled
129 * Currently there is no option in this driver to disable the saving of
130 * timestamps within the ring.
131 * I think the one copy of this at a time was to avoid problems if the
132 * trigger was set far too high and the reads then locked up the computer.
134 static void max1363_poll_bh_to_ring(struct work_struct
*work_s
)
136 struct max1363_state
*st
= container_of(work_s
, struct max1363_state
,
138 struct iio_dev
*indio_dev
= st
->indio_dev
;
139 struct iio_sw_ring_buffer
*sw_ring
= iio_to_sw_ring(indio_dev
->ring
);
144 unsigned long numvals
= hweight_long(st
->current_mode
->modemask
);
146 /* Ensure the timestamp is 8 byte aligned */
147 if (st
->chip_info
->bits
!= 8)
148 d_size
= numvals
*2 + sizeof(s64
);
150 d_size
= numvals
+ 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
165 rxbuf
= kmalloc(d_size
, GFP_KERNEL
);
168 if (st
->chip_info
->bits
!= 8)
169 b_sent
= i2c_master_recv(st
->client
, rxbuf
, numvals
*2);
171 b_sent
= i2c_master_recv(st
->client
, rxbuf
, numvals
);
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(&sw_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(&indio_dev
->ring
->access
);
198 ret
= iio_alloc_pollfunc(indio_dev
, NULL
, &max1363_poll_func_th
);
200 goto error_deallocate_sw_rb
;
202 /* Ring buffer functions - here trigger setup related */
203 indio_dev
->ring
->scan_el_attrs
= st
->chip_info
->scan_attrs
;
204 indio_dev
->ring
->postenable
= &iio_triggered_ring_postenable
;
205 indio_dev
->ring
->preenable
= &max1363_ring_preenable
;
206 indio_dev
->ring
->predisable
= &iio_triggered_ring_predisable
;
207 INIT_WORK(&st
->poll_work
, &max1363_poll_bh_to_ring
);
209 /* Flag that polled ring buffering is possible */
210 indio_dev
->modes
|= INDIO_RING_TRIGGERED
;
212 error_deallocate_sw_rb
:
213 iio_sw_rb_free(indio_dev
->ring
);
218 void max1363_ring_cleanup(struct iio_dev
*indio_dev
)
220 /* ensure that the trigger has been detached */
221 if (indio_dev
->trig
) {
222 iio_put_trigger(indio_dev
->trig
);
223 iio_trigger_dettach_poll_func(indio_dev
->trig
,
224 indio_dev
->pollfunc
);
226 kfree(indio_dev
->pollfunc
);
227 iio_sw_rb_free(indio_dev
->ring
);