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/kernel.h>
11 #include <linux/slab.h>
12 #include <linux/spi/spi.h>
15 #include "../buffer_generic.h"
16 #include "../ring_sw.h"
17 #include "../trigger_consumer.h"
21 int ad7298_scan_from_ring(struct iio_dev
*dev_info
, long ch
)
23 struct iio_buffer
*ring
= dev_info
->buffer
;
27 if (!(test_bit(ch
, ring
->scan_mask
))) {
32 ring_data
= kmalloc(ring
->access
->get_bytes_per_datum(ring
),
34 if (ring_data
== NULL
) {
38 ret
= ring
->access
->read_last(ring
, (u8
*) ring_data
);
40 goto error_free_ring_data
;
42 ret
= be16_to_cpu(ring_data
[ch
]);
51 * ad7298_ring_preenable() setup the parameters of the ring before enabling
53 * The complex nature of the setting of the number of bytes per datum is due
54 * to this driver currently ensuring that the timestamp is stored at an 8
57 static int ad7298_ring_preenable(struct iio_dev
*indio_dev
)
59 struct ad7298_state
*st
= iio_priv(indio_dev
);
60 struct iio_buffer
*ring
= indio_dev
->buffer
;
63 unsigned short command
;
65 d_size
= ring
->scan_count
* (AD7298_STORAGE_BITS
/ 8);
67 if (ring
->scan_timestamp
) {
68 d_size
+= sizeof(s64
);
70 if (d_size
% sizeof(s64
))
71 d_size
+= sizeof(s64
) - (d_size
% sizeof(s64
));
74 if (ring
->access
->set_bytes_per_datum
)
75 ring
->access
->set_bytes_per_datum(ring
, d_size
);
79 command
= AD7298_WRITE
| st
->ext_ref
;
81 for (i
= 0, m
= AD7298_CH(0); i
< AD7298_MAX_CHAN
; i
++, m
>>= 1)
82 if (test_bit(i
, ring
->scan_mask
))
85 st
->tx_buf
[0] = cpu_to_be16(command
);
87 /* build spi ring message */
88 st
->ring_xfer
[0].tx_buf
= &st
->tx_buf
[0];
89 st
->ring_xfer
[0].len
= 2;
90 st
->ring_xfer
[0].cs_change
= 1;
91 st
->ring_xfer
[1].tx_buf
= &st
->tx_buf
[1];
92 st
->ring_xfer
[1].len
= 2;
93 st
->ring_xfer
[1].cs_change
= 1;
95 spi_message_init(&st
->ring_msg
);
96 spi_message_add_tail(&st
->ring_xfer
[0], &st
->ring_msg
);
97 spi_message_add_tail(&st
->ring_xfer
[1], &st
->ring_msg
);
99 for (i
= 0; i
< ring
->scan_count
; i
++) {
100 st
->ring_xfer
[i
+ 2].rx_buf
= &st
->rx_buf
[i
];
101 st
->ring_xfer
[i
+ 2].len
= 2;
102 st
->ring_xfer
[i
+ 2].cs_change
= 1;
103 spi_message_add_tail(&st
->ring_xfer
[i
+ 2], &st
->ring_msg
);
105 /* make sure last transfer cs_change is not set */
106 st
->ring_xfer
[i
+ 1].cs_change
= 0;
112 * ad7298_trigger_handler() bh of trigger launched polling to ring buffer
114 * Currently there is no option in this driver to disable the saving of
115 * timestamps within the ring.
117 static irqreturn_t
ad7298_trigger_handler(int irq
, void *p
)
119 struct iio_poll_func
*pf
= p
;
120 struct iio_dev
*indio_dev
= pf
->indio_dev
;
121 struct ad7298_state
*st
= iio_priv(indio_dev
);
122 struct iio_buffer
*ring
= indio_dev
->buffer
;
127 b_sent
= spi_sync(st
->spi
, &st
->ring_msg
);
131 if (ring
->scan_timestamp
) {
132 time_ns
= iio_get_time_ns();
133 memcpy((u8
*)buf
+ st
->d_size
- sizeof(s64
),
134 &time_ns
, sizeof(time_ns
));
137 for (i
= 0; i
< ring
->scan_count
; i
++)
138 buf
[i
] = be16_to_cpu(st
->rx_buf
[i
]);
140 indio_dev
->buffer
->access
->store_to(ring
, (u8
*)buf
, time_ns
);
141 iio_trigger_notify_done(indio_dev
->trig
);
146 static const struct iio_buffer_setup_ops ad7298_ring_setup_ops
= {
147 .preenable
= &ad7298_ring_preenable
,
148 .postenable
= &iio_triggered_buffer_postenable
,
149 .predisable
= &iio_triggered_buffer_predisable
,
152 int ad7298_register_ring_funcs_and_init(struct iio_dev
*indio_dev
)
156 indio_dev
->buffer
= iio_sw_rb_allocate(indio_dev
);
157 if (!indio_dev
->buffer
) {
161 /* Effectively select the ring buffer implementation */
162 indio_dev
->buffer
->access
= &ring_sw_access_funcs
;
164 indio_dev
->pollfunc
= iio_alloc_pollfunc(NULL
,
165 &ad7298_trigger_handler
,
171 if (indio_dev
->pollfunc
== NULL
) {
173 goto error_deallocate_sw_rb
;
176 /* Ring buffer functions - here trigger setup related */
177 indio_dev
->buffer
->setup_ops
= &ad7298_ring_setup_ops
;
178 indio_dev
->buffer
->scan_timestamp
= true;
180 /* Flag that polled ring buffering is possible */
181 indio_dev
->modes
|= INDIO_BUFFER_TRIGGERED
;
184 error_deallocate_sw_rb
:
185 iio_sw_rb_free(indio_dev
->buffer
);
190 void ad7298_ring_cleanup(struct iio_dev
*indio_dev
)
192 iio_dealloc_pollfunc(indio_dev
->pollfunc
);
193 iio_sw_rb_free(indio_dev
->buffer
);