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/workqueue.h>
11 #include <linux/device.h>
12 #include <linux/kernel.h>
13 #include <linux/slab.h>
14 #include <linux/sysfs.h>
17 #include "../ring_generic.h"
18 #include "../ring_sw.h"
19 #include "../trigger.h"
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
);
33 static IIO_SCAN_EL_TIMESTAMP(8);
34 static IIO_CONST_ATTR_SCAN_EL_TYPE(timestamp
, s
, 64, 64);
36 static ssize_t
ad7606_show_type(struct device
*dev
,
37 struct device_attribute
*attr
,
40 struct iio_ring_buffer
*ring
= dev_get_drvdata(dev
);
41 struct iio_dev
*indio_dev
= ring
->indio_dev
;
42 struct ad7606_state
*st
= indio_dev
->dev_data
;
44 return sprintf(buf
, "%c%d/%d\n", st
->chip_info
->sign
,
45 st
->chip_info
->bits
, st
->chip_info
->bits
);
47 static IIO_DEVICE_ATTR(in_type
, S_IRUGO
, ad7606_show_type
, NULL
, 0);
49 static struct attribute
*ad7606_scan_el_attrs
[] = {
50 &iio_scan_el_in0
.dev_attr
.attr
,
51 &iio_const_attr_in0_index
.dev_attr
.attr
,
52 &iio_scan_el_in1
.dev_attr
.attr
,
53 &iio_const_attr_in1_index
.dev_attr
.attr
,
54 &iio_scan_el_in2
.dev_attr
.attr
,
55 &iio_const_attr_in2_index
.dev_attr
.attr
,
56 &iio_scan_el_in3
.dev_attr
.attr
,
57 &iio_const_attr_in3_index
.dev_attr
.attr
,
58 &iio_scan_el_in4
.dev_attr
.attr
,
59 &iio_const_attr_in4_index
.dev_attr
.attr
,
60 &iio_scan_el_in5
.dev_attr
.attr
,
61 &iio_const_attr_in5_index
.dev_attr
.attr
,
62 &iio_scan_el_in6
.dev_attr
.attr
,
63 &iio_const_attr_in6_index
.dev_attr
.attr
,
64 &iio_scan_el_in7
.dev_attr
.attr
,
65 &iio_const_attr_in7_index
.dev_attr
.attr
,
66 &iio_const_attr_timestamp_index
.dev_attr
.attr
,
67 &iio_scan_el_timestamp
.dev_attr
.attr
,
68 &iio_const_attr_timestamp_type
.dev_attr
.attr
,
69 &iio_dev_attr_in_type
.dev_attr
.attr
,
73 static mode_t
ad7606_scan_el_attr_is_visible(struct kobject
*kobj
,
74 struct attribute
*attr
, int n
)
76 struct device
*dev
= container_of(kobj
, struct device
, kobj
);
77 struct iio_ring_buffer
*ring
= dev_get_drvdata(dev
);
78 struct iio_dev
*indio_dev
= ring
->indio_dev
;
79 struct ad7606_state
*st
= indio_dev
->dev_data
;
81 mode_t mode
= attr
->mode
;
83 if (st
->chip_info
->num_channels
<= 6 &&
84 (attr
== &iio_scan_el_in7
.dev_attr
.attr
||
85 attr
== &iio_const_attr_in7_index
.dev_attr
.attr
||
86 attr
== &iio_scan_el_in6
.dev_attr
.attr
||
87 attr
== &iio_const_attr_in6_index
.dev_attr
.attr
))
89 else if (st
->chip_info
->num_channels
<= 4 &&
90 (attr
== &iio_scan_el_in5
.dev_attr
.attr
||
91 attr
== &iio_const_attr_in5_index
.dev_attr
.attr
||
92 attr
== &iio_scan_el_in4
.dev_attr
.attr
||
93 attr
== &iio_const_attr_in4_index
.dev_attr
.attr
))
99 static struct attribute_group ad7606_scan_el_group
= {
100 .name
= "scan_elements",
101 .attrs
= ad7606_scan_el_attrs
,
102 .is_visible
= ad7606_scan_el_attr_is_visible
,
105 int ad7606_scan_from_ring(struct ad7606_state
*st
, unsigned ch
)
107 struct iio_ring_buffer
*ring
= st
->indio_dev
->ring
;
111 ring_data
= kmalloc(ring
->access
.get_bytes_per_datum(ring
), GFP_KERNEL
);
112 if (ring_data
== NULL
) {
116 ret
= ring
->access
.read_last(ring
, (u8
*) ring_data
);
118 goto error_free_ring_data
;
122 error_free_ring_data
:
129 * ad7606_ring_preenable() setup the parameters of the ring before enabling
131 * The complex nature of the setting of the nuber of bytes per datum is due
132 * to this driver currently ensuring that the timestamp is stored at an 8
135 static int ad7606_ring_preenable(struct iio_dev
*indio_dev
)
137 struct ad7606_state
*st
= indio_dev
->dev_data
;
138 struct iio_ring_buffer
*ring
= indio_dev
->ring
;
141 d_size
= st
->chip_info
->num_channels
*
142 st
->chip_info
->bits
/ 8;
144 if (ring
->scan_timestamp
) {
145 d_size
+= sizeof(s64
);
147 if (d_size
% sizeof(s64
))
148 d_size
+= sizeof(s64
) - (d_size
% sizeof(s64
));
151 if (ring
->access
.set_bytes_per_datum
)
152 ring
->access
.set_bytes_per_datum(ring
, d_size
);
160 * ad7606_poll_func_th() th of trigger launched polling to ring buffer
163 static void ad7606_poll_func_th(struct iio_dev
*indio_dev
, s64 time
)
165 struct ad7606_state
*st
= indio_dev
->dev_data
;
166 gpio_set_value(st
->pdata
->gpio_convst
, 1);
171 * ad7606_poll_bh_to_ring() bh of trigger launched polling to ring buffer
172 * @work_s: the work struct through which this was scheduled
174 * Currently there is no option in this driver to disable the saving of
175 * timestamps within the ring.
176 * I think the one copy of this at a time was to avoid problems if the
177 * trigger was set far too high and the reads then locked up the computer.
179 static void ad7606_poll_bh_to_ring(struct work_struct
*work_s
)
181 struct ad7606_state
*st
= container_of(work_s
, struct ad7606_state
,
183 struct iio_dev
*indio_dev
= st
->indio_dev
;
184 struct iio_sw_ring_buffer
*sw_ring
= iio_to_sw_ring(indio_dev
->ring
);
185 struct iio_ring_buffer
*ring
= indio_dev
->ring
;
190 /* Ensure only one copy of this function running at a time */
191 if (atomic_inc_return(&st
->protect_ring
) > 1)
194 buf
= kzalloc(st
->d_size
, GFP_KERNEL
);
198 if (st
->have_frstdata
) {
199 ret
= st
->bops
->read_block(st
->dev
, 1, buf
);
202 if (!gpio_get_value(st
->pdata
->gpio_frstdata
)) {
203 /* This should never happen. However
204 * some signal glitch caused by bad PCB desgin or
205 * electrostatic discharge, could cause an extra read
206 * or clock. This allows recovery.
211 ret
= st
->bops
->read_block(st
->dev
,
212 st
->chip_info
->num_channels
- 1, buf
+ 2);
216 ret
= st
->bops
->read_block(st
->dev
,
217 st
->chip_info
->num_channels
, buf
);
222 time_ns
= iio_get_time_ns();
224 if (ring
->scan_timestamp
)
225 memcpy(buf
+ st
->d_size
- sizeof(s64
),
226 &time_ns
, sizeof(time_ns
));
228 ring
->access
.store_to(&sw_ring
->buf
, buf
, time_ns
);
230 gpio_set_value(st
->pdata
->gpio_convst
, 0);
232 atomic_dec(&st
->protect_ring
);
235 int ad7606_register_ring_funcs_and_init(struct iio_dev
*indio_dev
)
237 struct ad7606_state
*st
= indio_dev
->dev_data
;
240 indio_dev
->ring
= iio_sw_rb_allocate(indio_dev
);
241 if (!indio_dev
->ring
) {
246 /* Effectively select the ring buffer implementation */
247 iio_ring_sw_register_funcs(&indio_dev
->ring
->access
);
248 ret
= iio_alloc_pollfunc(indio_dev
, NULL
, &ad7606_poll_func_th
);
250 goto error_deallocate_sw_rb
;
252 /* Ring buffer functions - here trigger setup related */
254 indio_dev
->ring
->preenable
= &ad7606_ring_preenable
;
255 indio_dev
->ring
->postenable
= &iio_triggered_ring_postenable
;
256 indio_dev
->ring
->predisable
= &iio_triggered_ring_predisable
;
257 indio_dev
->ring
->scan_el_attrs
= &ad7606_scan_el_group
;
258 indio_dev
->ring
->scan_timestamp
= true ;
260 INIT_WORK(&st
->poll_work
, &ad7606_poll_bh_to_ring
);
262 /* Flag that polled ring buffering is possible */
263 indio_dev
->modes
|= INDIO_RING_TRIGGERED
;
265 error_deallocate_sw_rb
:
266 iio_sw_rb_free(indio_dev
->ring
);
271 void ad7606_ring_cleanup(struct iio_dev
*indio_dev
)
273 if (indio_dev
->trig
) {
274 iio_put_trigger(indio_dev
->trig
);
275 iio_trigger_dettach_poll_func(indio_dev
->trig
,
276 indio_dev
->pollfunc
);
278 kfree(indio_dev
->pollfunc
);
279 iio_sw_rb_free(indio_dev
->ring
);