2 * AD7298 SPI ADC driver
4 * Copyright 2011 Analog Devices Inc.
6 * Licensed under the GPL-2.
9 #include <linux/interrupt.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 #include <linux/spi/spi.h>
18 #include "../ring_generic.h"
19 #include "../ring_sw.h"
20 #include "../trigger.h"
25 static IIO_SCAN_EL_C(in0
, 0, 0, NULL
);
26 static IIO_SCAN_EL_C(in1
, 1, 0, NULL
);
27 static IIO_SCAN_EL_C(in2
, 2, 0, NULL
);
28 static IIO_SCAN_EL_C(in3
, 3, 0, NULL
);
29 static IIO_SCAN_EL_C(in4
, 4, 0, NULL
);
30 static IIO_SCAN_EL_C(in5
, 5, 0, NULL
);
31 static IIO_SCAN_EL_C(in6
, 6, 0, NULL
);
32 static IIO_SCAN_EL_C(in7
, 7, 0, NULL
);
34 static IIO_SCAN_EL_TIMESTAMP(8);
35 static IIO_CONST_ATTR_SCAN_EL_TYPE(timestamp
, s
, 64, 64);
37 static IIO_CONST_ATTR(in_type
, "u12/16") ;
39 static struct attribute
*ad7298_scan_el_attrs
[] = {
40 &iio_scan_el_in0
.dev_attr
.attr
,
41 &iio_const_attr_in0_index
.dev_attr
.attr
,
42 &iio_scan_el_in1
.dev_attr
.attr
,
43 &iio_const_attr_in1_index
.dev_attr
.attr
,
44 &iio_scan_el_in2
.dev_attr
.attr
,
45 &iio_const_attr_in2_index
.dev_attr
.attr
,
46 &iio_scan_el_in3
.dev_attr
.attr
,
47 &iio_const_attr_in3_index
.dev_attr
.attr
,
48 &iio_scan_el_in4
.dev_attr
.attr
,
49 &iio_const_attr_in4_index
.dev_attr
.attr
,
50 &iio_scan_el_in5
.dev_attr
.attr
,
51 &iio_const_attr_in5_index
.dev_attr
.attr
,
52 &iio_scan_el_in6
.dev_attr
.attr
,
53 &iio_const_attr_in6_index
.dev_attr
.attr
,
54 &iio_scan_el_in7
.dev_attr
.attr
,
55 &iio_const_attr_in7_index
.dev_attr
.attr
,
56 &iio_const_attr_timestamp_index
.dev_attr
.attr
,
57 &iio_scan_el_timestamp
.dev_attr
.attr
,
58 &iio_const_attr_timestamp_type
.dev_attr
.attr
,
59 &iio_const_attr_in_type
.dev_attr
.attr
,
63 static struct attribute_group ad7298_scan_el_group
= {
64 .name
= "scan_elements",
65 .attrs
= ad7298_scan_el_attrs
,
68 int ad7298_scan_from_ring(struct ad7298_state
*st
, long ch
)
70 struct iio_ring_buffer
*ring
= st
->indio_dev
->ring
;
74 if (!(ring
->scan_mask
& (1 << ch
))) {
79 ring_data
= kmalloc(ring
->access
.get_bytes_per_datum(ring
), GFP_KERNEL
);
80 if (ring_data
== NULL
) {
84 ret
= ring
->access
.read_last(ring
, (u8
*) ring_data
);
86 goto error_free_ring_data
;
88 ret
= be16_to_cpu(ring_data
[ch
]);
97 * ad7298_ring_preenable() setup the parameters of the ring before enabling
99 * The complex nature of the setting of the number of bytes per datum is due
100 * to this driver currently ensuring that the timestamp is stored at an 8
103 static int ad7298_ring_preenable(struct iio_dev
*indio_dev
)
105 struct ad7298_state
*st
= indio_dev
->dev_data
;
106 struct iio_ring_buffer
*ring
= indio_dev
->ring
;
109 unsigned short command
;
111 d_size
= ring
->scan_count
* (AD7298_STORAGE_BITS
/ 8);
113 if (ring
->scan_timestamp
) {
114 d_size
+= sizeof(s64
);
116 if (d_size
% sizeof(s64
))
117 d_size
+= sizeof(s64
) - (d_size
% sizeof(s64
));
120 if (ring
->access
.set_bytes_per_datum
)
121 ring
->access
.set_bytes_per_datum(ring
, d_size
);
125 command
= AD7298_WRITE
| st
->ext_ref
;
127 for (i
= 0, m
= AD7298_CH(0); i
< AD7298_MAX_CHAN
; i
++, m
>>= 1)
128 if (ring
->scan_mask
& (1 << i
))
131 st
->tx_buf
[0] = cpu_to_be16(command
);
133 /* build spi ring message */
134 st
->ring_xfer
[0].tx_buf
= &st
->tx_buf
[0];
135 st
->ring_xfer
[0].len
= 2;
136 st
->ring_xfer
[0].cs_change
= 1;
137 st
->ring_xfer
[1].tx_buf
= &st
->tx_buf
[1];
138 st
->ring_xfer
[1].len
= 2;
139 st
->ring_xfer
[1].cs_change
= 1;
141 spi_message_init(&st
->ring_msg
);
142 spi_message_add_tail(&st
->ring_xfer
[0], &st
->ring_msg
);
143 spi_message_add_tail(&st
->ring_xfer
[1], &st
->ring_msg
);
145 for (i
= 0; i
< ring
->scan_count
; i
++) {
146 st
->ring_xfer
[i
+ 2].rx_buf
= &st
->rx_buf
[i
];
147 st
->ring_xfer
[i
+ 2].len
= 2;
148 st
->ring_xfer
[i
+ 2].cs_change
= 1;
149 spi_message_add_tail(&st
->ring_xfer
[i
+ 2], &st
->ring_msg
);
151 /* make sure last transfer cs_change is not set */
152 st
->ring_xfer
[i
+ 1].cs_change
= 0;
158 * ad7298_poll_func_th() th of trigger launched polling to ring buffer
160 * As sampling only occurs on spi comms occurring, leave timestamping until
161 * then. Some triggers will generate their own time stamp. Currently
162 * there is no way of notifying them when no one cares.
164 static void ad7298_poll_func_th(struct iio_dev
*indio_dev
, s64 time
)
166 struct ad7298_state
*st
= indio_dev
->dev_data
;
168 schedule_work(&st
->poll_work
);
173 * ad7298_poll_bh_to_ring() bh of trigger launched polling to ring buffer
174 * @work_s: the work struct through which this was scheduled
176 * Currently there is no option in this driver to disable the saving of
177 * timestamps within the ring.
178 * I think the one copy of this at a time was to avoid problems if the
179 * trigger was set far too high and the reads then locked up the computer.
181 static void ad7298_poll_bh_to_ring(struct work_struct
*work_s
)
183 struct ad7298_state
*st
= container_of(work_s
, struct ad7298_state
,
185 struct iio_dev
*indio_dev
= st
->indio_dev
;
186 struct iio_sw_ring_buffer
*sw_ring
= iio_to_sw_ring(indio_dev
->ring
);
187 struct iio_ring_buffer
*ring
= indio_dev
->ring
;
192 /* Ensure only one copy of this function running at a time */
193 if (atomic_inc_return(&st
->protect_ring
) > 1)
196 b_sent
= spi_sync(st
->spi
, &st
->ring_msg
);
200 if (ring
->scan_timestamp
) {
201 time_ns
= iio_get_time_ns();
202 memcpy((u8
*)buf
+ st
->d_size
- sizeof(s64
),
203 &time_ns
, sizeof(time_ns
));
206 for (i
= 0; i
< ring
->scan_count
; i
++)
207 buf
[i
] = be16_to_cpu(st
->rx_buf
[i
]);
209 indio_dev
->ring
->access
.store_to(&sw_ring
->buf
, (u8
*)buf
, time_ns
);
211 atomic_dec(&st
->protect_ring
);
214 int ad7298_register_ring_funcs_and_init(struct iio_dev
*indio_dev
)
216 struct ad7298_state
*st
= indio_dev
->dev_data
;
219 indio_dev
->ring
= iio_sw_rb_allocate(indio_dev
);
220 if (!indio_dev
->ring
) {
224 /* Effectively select the ring buffer implementation */
225 iio_ring_sw_register_funcs(&indio_dev
->ring
->access
);
226 ret
= iio_alloc_pollfunc(indio_dev
, NULL
, &ad7298_poll_func_th
);
228 goto error_deallocate_sw_rb
;
230 /* Ring buffer functions - here trigger setup related */
232 indio_dev
->ring
->preenable
= &ad7298_ring_preenable
;
233 indio_dev
->ring
->postenable
= &iio_triggered_ring_postenable
;
234 indio_dev
->ring
->predisable
= &iio_triggered_ring_predisable
;
235 indio_dev
->ring
->scan_el_attrs
= &ad7298_scan_el_group
;
236 indio_dev
->ring
->scan_timestamp
= true;
238 INIT_WORK(&st
->poll_work
, &ad7298_poll_bh_to_ring
);
240 /* Flag that polled ring buffering is possible */
241 indio_dev
->modes
|= INDIO_RING_TRIGGERED
;
243 error_deallocate_sw_rb
:
244 iio_sw_rb_free(indio_dev
->ring
);
249 void ad7298_ring_cleanup(struct iio_dev
*indio_dev
)
251 if (indio_dev
->trig
) {
252 iio_put_trigger(indio_dev
->trig
);
253 iio_trigger_dettach_poll_func(indio_dev
->trig
,
254 indio_dev
->pollfunc
);
256 kfree(indio_dev
->pollfunc
);
257 iio_sw_rb_free(indio_dev
->ring
);