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>
14 #include <linux/iio/iio.h>
15 #include <linux/iio/buffer.h>
16 #include "../ring_sw.h"
17 #include <linux/iio/trigger_consumer.h>
22 * ad7606_trigger_handler_th() th/bh of trigger launched polling to ring buffer
25 static irqreturn_t
ad7606_trigger_handler_th_bh(int irq
, void *p
)
27 struct iio_poll_func
*pf
= p
;
28 struct ad7606_state
*st
= iio_priv(pf
->indio_dev
);
30 gpio_set_value(st
->pdata
->gpio_convst
, 1);
36 * ad7606_poll_bh_to_ring() bh of trigger launched polling to ring buffer
37 * @work_s: the work struct through which this was scheduled
39 * Currently there is no option in this driver to disable the saving of
40 * timestamps within the ring.
41 * I think the one copy of this at a time was to avoid problems if the
42 * trigger was set far too high and the reads then locked up the computer.
44 static void ad7606_poll_bh_to_ring(struct work_struct
*work_s
)
46 struct ad7606_state
*st
= container_of(work_s
, struct ad7606_state
,
48 struct iio_dev
*indio_dev
= iio_priv_to_dev(st
);
49 struct iio_buffer
*ring
= indio_dev
->buffer
;
54 buf
= kzalloc(indio_dev
->scan_bytes
, GFP_KERNEL
);
58 if (gpio_is_valid(st
->pdata
->gpio_frstdata
)) {
59 ret
= st
->bops
->read_block(st
->dev
, 1, buf
);
62 if (!gpio_get_value(st
->pdata
->gpio_frstdata
)) {
63 /* This should never happen. However
64 * some signal glitch caused by bad PCB desgin or
65 * electrostatic discharge, could cause an extra read
66 * or clock. This allows recovery.
71 ret
= st
->bops
->read_block(st
->dev
,
72 st
->chip_info
->num_channels
- 1, buf
+ 2);
76 ret
= st
->bops
->read_block(st
->dev
,
77 st
->chip_info
->num_channels
, buf
);
82 time_ns
= iio_get_time_ns();
84 if (indio_dev
->scan_timestamp
)
85 *((s64
*)(buf
+ indio_dev
->scan_bytes
- sizeof(s64
))) = time_ns
;
87 ring
->access
->store_to(indio_dev
->buffer
, buf
, time_ns
);
89 gpio_set_value(st
->pdata
->gpio_convst
, 0);
90 iio_trigger_notify_done(indio_dev
->trig
);
94 static const struct iio_buffer_setup_ops ad7606_ring_setup_ops
= {
95 .preenable
= &iio_sw_buffer_preenable
,
96 .postenable
= &iio_triggered_buffer_postenable
,
97 .predisable
= &iio_triggered_buffer_predisable
,
100 int ad7606_register_ring_funcs_and_init(struct iio_dev
*indio_dev
)
102 struct ad7606_state
*st
= iio_priv(indio_dev
);
105 indio_dev
->buffer
= iio_sw_rb_allocate(indio_dev
);
106 if (!indio_dev
->buffer
) {
111 indio_dev
->pollfunc
= iio_alloc_pollfunc(&ad7606_trigger_handler_th_bh
,
112 &ad7606_trigger_handler_th_bh
,
118 if (indio_dev
->pollfunc
== NULL
) {
120 goto error_deallocate_sw_rb
;
123 /* Ring buffer functions - here trigger setup related */
125 indio_dev
->setup_ops
= &ad7606_ring_setup_ops
;
126 indio_dev
->buffer
->scan_timestamp
= true ;
128 INIT_WORK(&st
->poll_work
, &ad7606_poll_bh_to_ring
);
130 /* Flag that polled ring buffering is possible */
131 indio_dev
->modes
|= INDIO_BUFFER_TRIGGERED
;
134 error_deallocate_sw_rb
:
135 iio_sw_rb_free(indio_dev
->buffer
);
140 void ad7606_ring_cleanup(struct iio_dev
*indio_dev
)
142 iio_dealloc_pollfunc(indio_dev
->pollfunc
);
143 iio_sw_rb_free(indio_dev
->buffer
);