2 * Copyright 2011 Analog Devices Inc.
4 * Licensed under the GPL-2.
8 #include <linux/interrupt.h>
9 #include <linux/gpio.h>
10 #include <linux/device.h>
11 #include <linux/kernel.h>
12 #include <linux/slab.h>
15 #include "../ring_generic.h"
16 #include "../ring_sw.h"
17 #include "../trigger_consumer.h"
21 int ad7606_scan_from_ring(struct iio_dev
*indio_dev
, unsigned ch
)
23 struct iio_ring_buffer
*ring
= indio_dev
->ring
;
27 ring_data
= kmalloc(ring
->access
->get_bytes_per_datum(ring
),
29 if (ring_data
== NULL
) {
33 ret
= ring
->access
->read_last(ring
, (u8
*) ring_data
);
35 goto error_free_ring_data
;
46 * ad7606_ring_preenable() setup the parameters of the ring before enabling
48 * The complex nature of the setting of the nuber of bytes per datum is due
49 * to this driver currently ensuring that the timestamp is stored at an 8
52 static int ad7606_ring_preenable(struct iio_dev
*indio_dev
)
54 struct ad7606_state
*st
= iio_priv(indio_dev
);
55 struct iio_ring_buffer
*ring
= indio_dev
->ring
;
58 d_size
= st
->chip_info
->num_channels
*
59 st
->chip_info
->channels
[0].scan_type
.storagebits
/ 8;
61 if (ring
->scan_timestamp
) {
62 d_size
+= sizeof(s64
);
64 if (d_size
% sizeof(s64
))
65 d_size
+= sizeof(s64
) - (d_size
% sizeof(s64
));
68 if (ring
->access
->set_bytes_per_datum
)
69 ring
->access
->set_bytes_per_datum(ring
, d_size
);
77 * ad7606_trigger_handler_th() th/bh of trigger launched polling to ring buffer
80 static irqreturn_t
ad7606_trigger_handler_th_bh(int irq
, void *p
)
82 struct iio_poll_func
*pf
= p
;
83 struct ad7606_state
*st
= iio_priv(pf
->indio_dev
);
85 gpio_set_value(st
->pdata
->gpio_convst
, 1);
91 * ad7606_poll_bh_to_ring() bh of trigger launched polling to ring buffer
92 * @work_s: the work struct through which this was scheduled
94 * Currently there is no option in this driver to disable the saving of
95 * timestamps within the ring.
96 * I think the one copy of this at a time was to avoid problems if the
97 * trigger was set far too high and the reads then locked up the computer.
99 static void ad7606_poll_bh_to_ring(struct work_struct
*work_s
)
101 struct ad7606_state
*st
= container_of(work_s
, struct ad7606_state
,
103 struct iio_dev
*indio_dev
= iio_priv_to_dev(st
);
104 struct iio_ring_buffer
*ring
= indio_dev
->ring
;
109 buf
= kzalloc(st
->d_size
, GFP_KERNEL
);
113 if (st
->have_frstdata
) {
114 ret
= st
->bops
->read_block(st
->dev
, 1, buf
);
117 if (!gpio_get_value(st
->pdata
->gpio_frstdata
)) {
118 /* This should never happen. However
119 * some signal glitch caused by bad PCB desgin or
120 * electrostatic discharge, could cause an extra read
121 * or clock. This allows recovery.
126 ret
= st
->bops
->read_block(st
->dev
,
127 st
->chip_info
->num_channels
- 1, buf
+ 2);
131 ret
= st
->bops
->read_block(st
->dev
,
132 st
->chip_info
->num_channels
, buf
);
137 time_ns
= iio_get_time_ns();
139 if (ring
->scan_timestamp
)
140 memcpy(buf
+ st
->d_size
- sizeof(s64
),
141 &time_ns
, sizeof(time_ns
));
143 ring
->access
->store_to(indio_dev
->ring
, buf
, time_ns
);
145 gpio_set_value(st
->pdata
->gpio_convst
, 0);
146 iio_trigger_notify_done(indio_dev
->trig
);
150 static const struct iio_ring_setup_ops ad7606_ring_setup_ops
= {
151 .preenable
= &ad7606_ring_preenable
,
152 .postenable
= &iio_triggered_buffer_postenable
,
153 .predisable
= &iio_triggered_buffer_predisable
,
156 int ad7606_register_ring_funcs_and_init(struct iio_dev
*indio_dev
)
158 struct ad7606_state
*st
= iio_priv(indio_dev
);
161 indio_dev
->ring
= iio_sw_rb_allocate(indio_dev
);
162 if (!indio_dev
->ring
) {
167 /* Effectively select the ring buffer implementation */
168 indio_dev
->ring
->access
= &ring_sw_access_funcs
;
169 indio_dev
->pollfunc
= iio_alloc_pollfunc(&ad7606_trigger_handler_th_bh
,
170 &ad7606_trigger_handler_th_bh
,
176 if (indio_dev
->pollfunc
== NULL
) {
178 goto error_deallocate_sw_rb
;
181 /* Ring buffer functions - here trigger setup related */
183 indio_dev
->ring
->setup_ops
= &ad7606_ring_setup_ops
;
184 indio_dev
->ring
->scan_timestamp
= true ;
186 INIT_WORK(&st
->poll_work
, &ad7606_poll_bh_to_ring
);
188 /* Flag that polled ring buffering is possible */
189 indio_dev
->modes
|= INDIO_BUFFER_TRIGGERED
;
192 error_deallocate_sw_rb
:
193 iio_sw_rb_free(indio_dev
->ring
);
198 void ad7606_ring_cleanup(struct iio_dev
*indio_dev
)
200 iio_dealloc_pollfunc(indio_dev
->pollfunc
);
201 iio_sw_rb_free(indio_dev
->ring
);