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> | |
10 | #include <linux/workqueue.h> | |
11 | #include <linux/device.h> | |
12 | #include <linux/kernel.h> | |
13 | #include <linux/slab.h> | |
14 | #include <linux/sysfs.h> | |
15 | ||
16 | #include "../iio.h" | |
17 | #include "../ring_generic.h" | |
18 | #include "../ring_sw.h" | |
19 | #include "../trigger.h" | |
20 | #include "../sysfs.h" | |
21 | ||
22 | #include "ad7606.h" | |
23 | ||
24 | static IIO_SCAN_EL_C(in0, 0, 0, NULL); | |
25 | static IIO_SCAN_EL_C(in1, 1, 0, NULL); | |
26 | static IIO_SCAN_EL_C(in2, 2, 0, NULL); | |
27 | static IIO_SCAN_EL_C(in3, 3, 0, NULL); | |
28 | static IIO_SCAN_EL_C(in4, 4, 0, NULL); | |
29 | static IIO_SCAN_EL_C(in5, 5, 0, NULL); | |
30 | static IIO_SCAN_EL_C(in6, 6, 0, NULL); | |
31 | static IIO_SCAN_EL_C(in7, 7, 0, NULL); | |
32 | ||
33 | static ssize_t ad7606_show_type(struct device *dev, | |
34 | struct device_attribute *attr, | |
35 | char *buf) | |
36 | { | |
37 | struct iio_ring_buffer *ring = dev_get_drvdata(dev); | |
38 | struct iio_dev *indio_dev = ring->indio_dev; | |
39 | struct ad7606_state *st = indio_dev->dev_data; | |
40 | ||
41 | return sprintf(buf, "%c%d/%d\n", st->chip_info->sign, | |
42 | st->chip_info->bits, st->chip_info->bits); | |
43 | } | |
44 | static IIO_DEVICE_ATTR(in_type, S_IRUGO, ad7606_show_type, NULL, 0); | |
45 | ||
46 | static struct attribute *ad7606_scan_el_attrs[] = { | |
47 | &iio_scan_el_in0.dev_attr.attr, | |
48 | &iio_const_attr_in0_index.dev_attr.attr, | |
49 | &iio_scan_el_in1.dev_attr.attr, | |
50 | &iio_const_attr_in1_index.dev_attr.attr, | |
51 | &iio_scan_el_in2.dev_attr.attr, | |
52 | &iio_const_attr_in2_index.dev_attr.attr, | |
53 | &iio_scan_el_in3.dev_attr.attr, | |
54 | &iio_const_attr_in3_index.dev_attr.attr, | |
55 | &iio_scan_el_in4.dev_attr.attr, | |
56 | &iio_const_attr_in4_index.dev_attr.attr, | |
57 | &iio_scan_el_in5.dev_attr.attr, | |
58 | &iio_const_attr_in5_index.dev_attr.attr, | |
59 | &iio_scan_el_in6.dev_attr.attr, | |
60 | &iio_const_attr_in6_index.dev_attr.attr, | |
61 | &iio_scan_el_in7.dev_attr.attr, | |
62 | &iio_const_attr_in7_index.dev_attr.attr, | |
63 | &iio_dev_attr_in_type.dev_attr.attr, | |
64 | NULL, | |
65 | }; | |
66 | ||
67 | static mode_t ad7606_scan_el_attr_is_visible(struct kobject *kobj, | |
68 | struct attribute *attr, int n) | |
69 | { | |
70 | struct device *dev = container_of(kobj, struct device, kobj); | |
71 | struct iio_ring_buffer *ring = dev_get_drvdata(dev); | |
72 | struct iio_dev *indio_dev = ring->indio_dev; | |
73 | struct ad7606_state *st = indio_dev->dev_data; | |
74 | ||
75 | mode_t mode = attr->mode; | |
76 | ||
77 | if (st->chip_info->num_channels <= 6 && | |
78 | (attr == &iio_scan_el_in7.dev_attr.attr || | |
79 | attr == &iio_const_attr_in7_index.dev_attr.attr || | |
80 | attr == &iio_scan_el_in6.dev_attr.attr || | |
81 | attr == &iio_const_attr_in6_index.dev_attr.attr)) | |
82 | mode = 0; | |
83 | else if (st->chip_info->num_channels <= 4 && | |
84 | (attr == &iio_scan_el_in5.dev_attr.attr || | |
85 | attr == &iio_const_attr_in5_index.dev_attr.attr || | |
86 | attr == &iio_scan_el_in4.dev_attr.attr || | |
87 | attr == &iio_const_attr_in4_index.dev_attr.attr)) | |
88 | mode = 0; | |
89 | ||
90 | return mode; | |
91 | } | |
92 | ||
93 | static struct attribute_group ad7606_scan_el_group = { | |
94 | .name = "scan_elements", | |
95 | .attrs = ad7606_scan_el_attrs, | |
96 | .is_visible = ad7606_scan_el_attr_is_visible, | |
97 | }; | |
98 | ||
99 | int ad7606_scan_from_ring(struct ad7606_state *st, unsigned ch) | |
100 | { | |
101 | struct iio_ring_buffer *ring = st->indio_dev->ring; | |
102 | int ret; | |
103 | u16 *ring_data; | |
104 | ||
105 | ring_data = kmalloc(ring->access.get_bytes_per_datum(ring), GFP_KERNEL); | |
106 | if (ring_data == NULL) { | |
107 | ret = -ENOMEM; | |
108 | goto error_ret; | |
109 | } | |
110 | ret = ring->access.read_last(ring, (u8 *) ring_data); | |
111 | if (ret) | |
112 | goto error_free_ring_data; | |
113 | ||
114 | ret = ring_data[ch]; | |
115 | ||
116 | error_free_ring_data: | |
117 | kfree(ring_data); | |
118 | error_ret: | |
119 | return ret; | |
120 | } | |
121 | ||
122 | /** | |
123 | * ad7606_ring_preenable() setup the parameters of the ring before enabling | |
124 | * | |
125 | * The complex nature of the setting of the nuber of bytes per datum is due | |
126 | * to this driver currently ensuring that the timestamp is stored at an 8 | |
127 | * byte boundary. | |
128 | **/ | |
129 | static int ad7606_ring_preenable(struct iio_dev *indio_dev) | |
130 | { | |
131 | struct ad7606_state *st = indio_dev->dev_data; | |
132 | struct iio_ring_buffer *ring = indio_dev->ring; | |
133 | size_t d_size; | |
134 | ||
135 | d_size = st->chip_info->num_channels * | |
136 | st->chip_info->bits / 8 + sizeof(s64); | |
137 | ||
138 | if (d_size % sizeof(s64)) | |
139 | d_size += sizeof(s64) - (d_size % sizeof(s64)); | |
140 | ||
141 | if (ring->access.set_bytes_per_datum) | |
142 | ring->access.set_bytes_per_datum(ring, d_size); | |
143 | ||
144 | st->d_size = d_size; | |
145 | ||
146 | return 0; | |
147 | } | |
148 | ||
149 | /** | |
150 | * ad7606_poll_func_th() th of trigger launched polling to ring buffer | |
151 | * | |
152 | **/ | |
153 | static void ad7606_poll_func_th(struct iio_dev *indio_dev, s64 time) | |
154 | { | |
155 | struct ad7606_state *st = indio_dev->dev_data; | |
156 | gpio_set_value(st->pdata->gpio_convst, 1); | |
157 | ||
158 | return; | |
159 | } | |
160 | /** | |
161 | * ad7606_poll_bh_to_ring() bh of trigger launched polling to ring buffer | |
162 | * @work_s: the work struct through which this was scheduled | |
163 | * | |
164 | * Currently there is no option in this driver to disable the saving of | |
165 | * timestamps within the ring. | |
166 | * I think the one copy of this at a time was to avoid problems if the | |
167 | * trigger was set far too high and the reads then locked up the computer. | |
168 | **/ | |
169 | static void ad7606_poll_bh_to_ring(struct work_struct *work_s) | |
170 | { | |
171 | struct ad7606_state *st = container_of(work_s, struct ad7606_state, | |
172 | poll_work); | |
173 | struct iio_dev *indio_dev = st->indio_dev; | |
174 | struct iio_sw_ring_buffer *sw_ring = iio_to_sw_ring(indio_dev->ring); | |
175 | struct iio_ring_buffer *ring = indio_dev->ring; | |
176 | s64 time_ns; | |
177 | __u8 *buf; | |
178 | int ret; | |
179 | ||
180 | /* Ensure only one copy of this function running at a time */ | |
181 | if (atomic_inc_return(&st->protect_ring) > 1) | |
182 | return; | |
183 | ||
184 | buf = kzalloc(st->d_size, GFP_KERNEL); | |
185 | if (buf == NULL) | |
186 | return; | |
187 | ||
188 | if (st->have_frstdata) { | |
189 | ret = st->bops->read_block(st->dev, 1, buf); | |
190 | if (ret) | |
191 | goto done; | |
192 | if (!gpio_get_value(st->pdata->gpio_frstdata)) { | |
193 | /* This should never happen. However | |
194 | * some signal glitch caused by bad PCB desgin or | |
195 | * electrostatic discharge, could cause an extra read | |
196 | * or clock. This allows recovery. | |
197 | */ | |
198 | ad7606_reset(st); | |
199 | goto done; | |
200 | } | |
201 | ret = st->bops->read_block(st->dev, | |
202 | st->chip_info->num_channels - 1, buf + 2); | |
203 | if (ret) | |
204 | goto done; | |
205 | } else { | |
206 | ret = st->bops->read_block(st->dev, | |
207 | st->chip_info->num_channels, buf); | |
208 | if (ret) | |
209 | goto done; | |
210 | } | |
211 | ||
212 | time_ns = iio_get_time_ns(); | |
213 | memcpy(buf + st->d_size - sizeof(s64), &time_ns, sizeof(time_ns)); | |
214 | ||
215 | ring->access.store_to(&sw_ring->buf, buf, time_ns); | |
216 | done: | |
217 | gpio_set_value(st->pdata->gpio_convst, 0); | |
218 | kfree(buf); | |
219 | atomic_dec(&st->protect_ring); | |
220 | } | |
221 | ||
222 | int ad7606_register_ring_funcs_and_init(struct iio_dev *indio_dev) | |
223 | { | |
224 | struct ad7606_state *st = indio_dev->dev_data; | |
225 | int ret; | |
226 | ||
227 | indio_dev->ring = iio_sw_rb_allocate(indio_dev); | |
228 | if (!indio_dev->ring) { | |
229 | ret = -ENOMEM; | |
230 | goto error_ret; | |
231 | } | |
232 | ||
233 | /* Effectively select the ring buffer implementation */ | |
234 | iio_ring_sw_register_funcs(&indio_dev->ring->access); | |
235 | ret = iio_alloc_pollfunc(indio_dev, NULL, &ad7606_poll_func_th); | |
236 | if (ret) | |
237 | goto error_deallocate_sw_rb; | |
238 | ||
239 | /* Ring buffer functions - here trigger setup related */ | |
240 | ||
241 | indio_dev->ring->preenable = &ad7606_ring_preenable; | |
242 | indio_dev->ring->postenable = &iio_triggered_ring_postenable; | |
243 | indio_dev->ring->predisable = &iio_triggered_ring_predisable; | |
244 | indio_dev->ring->scan_el_attrs = &ad7606_scan_el_group; | |
245 | ||
246 | INIT_WORK(&st->poll_work, &ad7606_poll_bh_to_ring); | |
247 | ||
248 | /* Flag that polled ring buffering is possible */ | |
249 | indio_dev->modes |= INDIO_RING_TRIGGERED; | |
250 | return 0; | |
251 | error_deallocate_sw_rb: | |
252 | iio_sw_rb_free(indio_dev->ring); | |
253 | error_ret: | |
254 | return ret; | |
255 | } | |
256 | ||
257 | void ad7606_ring_cleanup(struct iio_dev *indio_dev) | |
258 | { | |
259 | if (indio_dev->trig) { | |
260 | iio_put_trigger(indio_dev->trig); | |
261 | iio_trigger_dettach_poll_func(indio_dev->trig, | |
262 | indio_dev->pollfunc); | |
263 | } | |
264 | kfree(indio_dev->pollfunc); | |
265 | iio_sw_rb_free(indio_dev->ring); | |
266 | } |