1
2
3
4
5
6
7
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>
13
14#include "../iio.h"
15#include "../buffer.h"
16#include "../ring_sw.h"
17#include "../trigger_consumer.h"
18
19#include "ad7606.h"
20
21
22
23
24
25static irqreturn_t ad7606_trigger_handler_th_bh(int irq, void *p)
26{
27 struct iio_poll_func *pf = p;
28 struct ad7606_state *st = iio_priv(pf->indio_dev);
29
30 gpio_set_value(st->pdata->gpio_convst, 1);
31
32 return IRQ_HANDLED;
33}
34
35
36
37
38
39
40
41
42
43
44static void ad7606_poll_bh_to_ring(struct work_struct *work_s)
45{
46 struct ad7606_state *st = container_of(work_s, struct ad7606_state,
47 poll_work);
48 struct iio_dev *indio_dev = iio_priv_to_dev(st);
49 struct iio_buffer *ring = indio_dev->buffer;
50 s64 time_ns;
51 __u8 *buf;
52 int ret;
53
54 buf = kzalloc(ring->access->get_bytes_per_datum(ring),
55 GFP_KERNEL);
56 if (buf == NULL)
57 return;
58
59 if (gpio_is_valid(st->pdata->gpio_frstdata)) {
60 ret = st->bops->read_block(st->dev, 1, buf);
61 if (ret)
62 goto done;
63 if (!gpio_get_value(st->pdata->gpio_frstdata)) {
64
65
66
67
68
69 ad7606_reset(st);
70 goto done;
71 }
72 ret = st->bops->read_block(st->dev,
73 st->chip_info->num_channels - 1, buf + 2);
74 if (ret)
75 goto done;
76 } else {
77 ret = st->bops->read_block(st->dev,
78 st->chip_info->num_channels, buf);
79 if (ret)
80 goto done;
81 }
82
83 time_ns = iio_get_time_ns();
84
85 if (ring->scan_timestamp)
86 *((s64 *)(buf + ring->access->get_bytes_per_datum(ring) -
87 sizeof(s64))) = time_ns;
88
89 ring->access->store_to(indio_dev->buffer, buf, time_ns);
90done:
91 gpio_set_value(st->pdata->gpio_convst, 0);
92 iio_trigger_notify_done(indio_dev->trig);
93 kfree(buf);
94}
95
96static const struct iio_buffer_setup_ops ad7606_ring_setup_ops = {
97 .preenable = &iio_sw_buffer_preenable,
98 .postenable = &iio_triggered_buffer_postenable,
99 .predisable = &iio_triggered_buffer_predisable,
100};
101
102int ad7606_register_ring_funcs_and_init(struct iio_dev *indio_dev)
103{
104 struct ad7606_state *st = iio_priv(indio_dev);
105 int ret;
106
107 indio_dev->buffer = iio_sw_rb_allocate(indio_dev);
108 if (!indio_dev->buffer) {
109 ret = -ENOMEM;
110 goto error_ret;
111 }
112
113 indio_dev->pollfunc = iio_alloc_pollfunc(&ad7606_trigger_handler_th_bh,
114 &ad7606_trigger_handler_th_bh,
115 0,
116 indio_dev,
117 "%s_consumer%d",
118 indio_dev->name,
119 indio_dev->id);
120 if (indio_dev->pollfunc == NULL) {
121 ret = -ENOMEM;
122 goto error_deallocate_sw_rb;
123 }
124
125
126
127 indio_dev->setup_ops = &ad7606_ring_setup_ops;
128 indio_dev->buffer->scan_timestamp = true ;
129
130 INIT_WORK(&st->poll_work, &ad7606_poll_bh_to_ring);
131
132
133 indio_dev->modes |= INDIO_BUFFER_TRIGGERED;
134 return 0;
135
136error_deallocate_sw_rb:
137 iio_sw_rb_free(indio_dev->buffer);
138error_ret:
139 return ret;
140}
141
142void ad7606_ring_cleanup(struct iio_dev *indio_dev)
143{
144 iio_dealloc_pollfunc(indio_dev->pollfunc);
145 iio_sw_rb_free(indio_dev->buffer);
146}
147