Commit | Line | Data |
---|---|---|
b9618c0c MH |
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> | |
b9618c0c MH |
10 | #include <linux/device.h> |
11 | #include <linux/kernel.h> | |
12 | #include <linux/slab.h> | |
b9618c0c MH |
13 | |
14 | #include "../iio.h" | |
3811cd62 | 15 | #include "../buffer_generic.h" |
b9618c0c | 16 | #include "../ring_sw.h" |
3f72395e | 17 | #include "../trigger_consumer.h" |
b9618c0c MH |
18 | |
19 | #include "ad7606.h" | |
20 | ||
44039a67 | 21 | int ad7606_scan_from_ring(struct iio_dev *indio_dev, unsigned ch) |
b9618c0c | 22 | { |
14555b14 | 23 | struct iio_buffer *ring = indio_dev->buffer; |
b9618c0c MH |
24 | int ret; |
25 | u16 *ring_data; | |
26 | ||
5565a450 | 27 | ring_data = kmalloc(ring->access->get_bytes_per_datum(ring), |
e61181d0 | 28 | GFP_KERNEL); |
b9618c0c MH |
29 | if (ring_data == NULL) { |
30 | ret = -ENOMEM; | |
31 | goto error_ret; | |
32 | } | |
5565a450 | 33 | ret = ring->access->read_last(ring, (u8 *) ring_data); |
b9618c0c MH |
34 | if (ret) |
35 | goto error_free_ring_data; | |
36 | ||
37 | ret = ring_data[ch]; | |
38 | ||
39 | error_free_ring_data: | |
40 | kfree(ring_data); | |
41 | error_ret: | |
42 | return ret; | |
43 | } | |
44 | ||
45 | /** | |
46 | * ad7606_ring_preenable() setup the parameters of the ring before enabling | |
47 | * | |
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 | |
50 | * byte boundary. | |
51 | **/ | |
52 | static int ad7606_ring_preenable(struct iio_dev *indio_dev) | |
53 | { | |
44039a67 | 54 | struct ad7606_state *st = iio_priv(indio_dev); |
14555b14 | 55 | struct iio_buffer *ring = indio_dev->buffer; |
b9618c0c MH |
56 | size_t d_size; |
57 | ||
58 | d_size = st->chip_info->num_channels * | |
1caf7cb4 | 59 | st->chip_info->channels[0].scan_type.storagebits / 8; |
dcfb0863 MH |
60 | |
61 | if (ring->scan_timestamp) { | |
62 | d_size += sizeof(s64); | |
b9618c0c | 63 | |
dcfb0863 MH |
64 | if (d_size % sizeof(s64)) |
65 | d_size += sizeof(s64) - (d_size % sizeof(s64)); | |
66 | } | |
b9618c0c | 67 | |
5565a450 JC |
68 | if (ring->access->set_bytes_per_datum) |
69 | ring->access->set_bytes_per_datum(ring, d_size); | |
b9618c0c MH |
70 | |
71 | st->d_size = d_size; | |
72 | ||
73 | return 0; | |
74 | } | |
75 | ||
76 | /** | |
1caf7cb4 | 77 | * ad7606_trigger_handler_th() th/bh of trigger launched polling to ring buffer |
b9618c0c MH |
78 | * |
79 | **/ | |
1caf7cb4 | 80 | static irqreturn_t ad7606_trigger_handler_th_bh(int irq, void *p) |
b9618c0c | 81 | { |
13cfac20 | 82 | struct iio_poll_func *pf = p; |
e65bc6ac | 83 | struct ad7606_state *st = iio_priv(pf->indio_dev); |
1caf7cb4 | 84 | |
b9618c0c MH |
85 | gpio_set_value(st->pdata->gpio_convst, 1); |
86 | ||
13cfac20 | 87 | return IRQ_HANDLED; |
b9618c0c | 88 | } |
13cfac20 | 89 | |
b9618c0c MH |
90 | /** |
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 | |
93 | * | |
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. | |
98 | **/ | |
99 | static void ad7606_poll_bh_to_ring(struct work_struct *work_s) | |
100 | { | |
101 | struct ad7606_state *st = container_of(work_s, struct ad7606_state, | |
102 | poll_work); | |
e61181d0 | 103 | struct iio_dev *indio_dev = iio_priv_to_dev(st); |
14555b14 | 104 | struct iio_buffer *ring = indio_dev->buffer; |
b9618c0c MH |
105 | s64 time_ns; |
106 | __u8 *buf; | |
107 | int ret; | |
108 | ||
b9618c0c MH |
109 | buf = kzalloc(st->d_size, GFP_KERNEL); |
110 | if (buf == NULL) | |
111 | return; | |
112 | ||
68b41aef | 113 | if (gpio_is_valid(st->pdata->gpio_frstdata)) { |
b9618c0c MH |
114 | ret = st->bops->read_block(st->dev, 1, buf); |
115 | if (ret) | |
116 | goto done; | |
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. | |
122 | */ | |
123 | ad7606_reset(st); | |
124 | goto done; | |
125 | } | |
126 | ret = st->bops->read_block(st->dev, | |
127 | st->chip_info->num_channels - 1, buf + 2); | |
128 | if (ret) | |
129 | goto done; | |
130 | } else { | |
131 | ret = st->bops->read_block(st->dev, | |
132 | st->chip_info->num_channels, buf); | |
133 | if (ret) | |
134 | goto done; | |
135 | } | |
136 | ||
137 | time_ns = iio_get_time_ns(); | |
dcfb0863 MH |
138 | |
139 | if (ring->scan_timestamp) | |
140 | memcpy(buf + st->d_size - sizeof(s64), | |
141 | &time_ns, sizeof(time_ns)); | |
b9618c0c | 142 | |
14555b14 | 143 | ring->access->store_to(indio_dev->buffer, buf, time_ns); |
b9618c0c MH |
144 | done: |
145 | gpio_set_value(st->pdata->gpio_convst, 0); | |
1caf7cb4 | 146 | iio_trigger_notify_done(indio_dev->trig); |
b9618c0c | 147 | kfree(buf); |
b9618c0c MH |
148 | } |
149 | ||
14555b14 | 150 | static const struct iio_buffer_setup_ops ad7606_ring_setup_ops = { |
5565a450 | 151 | .preenable = &ad7606_ring_preenable, |
3b99fb76 JC |
152 | .postenable = &iio_triggered_buffer_postenable, |
153 | .predisable = &iio_triggered_buffer_predisable, | |
5565a450 JC |
154 | }; |
155 | ||
b9618c0c MH |
156 | int ad7606_register_ring_funcs_and_init(struct iio_dev *indio_dev) |
157 | { | |
44039a67 | 158 | struct ad7606_state *st = iio_priv(indio_dev); |
b9618c0c MH |
159 | int ret; |
160 | ||
14555b14 JC |
161 | indio_dev->buffer = iio_sw_rb_allocate(indio_dev); |
162 | if (!indio_dev->buffer) { | |
b9618c0c MH |
163 | ret = -ENOMEM; |
164 | goto error_ret; | |
165 | } | |
166 | ||
167 | /* Effectively select the ring buffer implementation */ | |
14555b14 | 168 | indio_dev->buffer->access = &ring_sw_access_funcs; |
0ed731d2 JC |
169 | indio_dev->pollfunc = iio_alloc_pollfunc(&ad7606_trigger_handler_th_bh, |
170 | &ad7606_trigger_handler_th_bh, | |
171 | 0, | |
172 | indio_dev, | |
173 | "%s_consumer%d", | |
174 | indio_dev->name, | |
175 | indio_dev->id); | |
13cfac20 JC |
176 | if (indio_dev->pollfunc == NULL) { |
177 | ret = -ENOMEM; | |
b9618c0c | 178 | goto error_deallocate_sw_rb; |
13cfac20 | 179 | } |
1caf7cb4 | 180 | |
b9618c0c MH |
181 | /* Ring buffer functions - here trigger setup related */ |
182 | ||
14555b14 JC |
183 | indio_dev->buffer->setup_ops = &ad7606_ring_setup_ops; |
184 | indio_dev->buffer->scan_timestamp = true ; | |
b9618c0c MH |
185 | |
186 | INIT_WORK(&st->poll_work, &ad7606_poll_bh_to_ring); | |
187 | ||
188 | /* Flag that polled ring buffering is possible */ | |
ec3afa40 | 189 | indio_dev->modes |= INDIO_BUFFER_TRIGGERED; |
b9618c0c | 190 | return 0; |
0ed731d2 | 191 | |
b9618c0c | 192 | error_deallocate_sw_rb: |
14555b14 | 193 | iio_sw_rb_free(indio_dev->buffer); |
b9618c0c MH |
194 | error_ret: |
195 | return ret; | |
196 | } | |
197 | ||
198 | void ad7606_ring_cleanup(struct iio_dev *indio_dev) | |
199 | { | |
0ed731d2 | 200 | iio_dealloc_pollfunc(indio_dev->pollfunc); |
14555b14 | 201 | iio_sw_rb_free(indio_dev->buffer); |
b9618c0c | 202 | } |