1 /*
2 * Copyright 2011 Analog Devices Inc.
3 *
4 * Licensed under the GPL-2.
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 * ad7606_trigger_handler_th() th/bh of trigger launched polling to ring buffer
23 *
24 **/
ad7606_trigger_handler_th_bh(int irq,void * p)25 static 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 * ad7606_poll_bh_to_ring() bh of trigger launched polling to ring buffer
37 * @work_s: the work struct through which this was scheduled
38 *
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.
43 **/
ad7606_poll_bh_to_ring(struct work_struct * work_s)44 static 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 /* This should never happen. However
65 * some signal glitch caused by bad PCB desgin or
66 * electrostatic discharge, could cause an extra read
67 * or clock. This allows recovery.
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);
90 done:
91 gpio_set_value(st->pdata->gpio_convst, 0);
92 iio_trigger_notify_done(indio_dev->trig);
93 kfree(buf);
94 }
95
96 static 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
ad7606_register_ring_funcs_and_init(struct iio_dev * indio_dev)102 int 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 /* Ring buffer functions - here trigger setup related */
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 /* Flag that polled ring buffering is possible */
133 indio_dev->modes |= INDIO_BUFFER_TRIGGERED;
134 return 0;
135
136 error_deallocate_sw_rb:
137 iio_sw_rb_free(indio_dev->buffer);
138 error_ret:
139 return ret;
140 }
141
ad7606_ring_cleanup(struct iio_dev * indio_dev)142 void 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