2 * Copyright 2010-2011 Analog Devices Inc.
3 * Copyright (C) 2008 Jonathan Cameron
5 * Licensed under the GPL-2.
10 #include <linux/interrupt.h>
11 #include <linux/kernel.h>
12 #include <linux/slab.h>
13 #include <linux/spi/spi.h>
16 #include "../ring_generic.h"
17 #include "../ring_sw.h"
18 #include "../trigger_consumer.h"
22 int ad7887_scan_from_ring(struct ad7887_state
*st
, int channum
)
24 struct iio_ring_buffer
*ring
= iio_priv_to_dev(st
)->ring
;
28 if (!(test_bit(channum
, ring
->scan_mask
))) {
33 ring_data
= kmalloc(ring
->access
->get_bytes_per_datum(ring
),
35 if (ring_data
== NULL
) {
39 ret
= ring
->access
->read_last(ring
, (u8
*) ring_data
);
41 goto error_free_ring_data
;
43 /* for single channel scan the result is stored with zero offset */
44 if ((test_bit(1, ring
->scan_mask
) || test_bit(0, ring
->scan_mask
)) &&
48 ret
= be16_to_cpu(ring_data
[count
]);
57 * ad7887_ring_preenable() setup the parameters of the ring before enabling
59 * The complex nature of the setting of the nuber of bytes per datum is due
60 * to this driver currently ensuring that the timestamp is stored at an 8
63 static int ad7887_ring_preenable(struct iio_dev
*indio_dev
)
65 struct ad7887_state
*st
= iio_priv(indio_dev
);
66 struct iio_ring_buffer
*ring
= indio_dev
->ring
;
68 st
->d_size
= ring
->scan_count
*
69 st
->chip_info
->channel
[0].scan_type
.storagebits
/ 8;
71 if (ring
->scan_timestamp
) {
72 st
->d_size
+= sizeof(s64
);
74 if (st
->d_size
% sizeof(s64
))
75 st
->d_size
+= sizeof(s64
) - (st
->d_size
% sizeof(s64
));
78 if (indio_dev
->ring
->access
->set_bytes_per_datum
)
79 indio_dev
->ring
->access
->set_bytes_per_datum(indio_dev
->ring
,
82 /* We know this is a single long so can 'cheat' */
83 switch (*ring
->scan_mask
) {
85 st
->ring_msg
= &st
->msg
[AD7887_CH0
];
88 st
->ring_msg
= &st
->msg
[AD7887_CH1
];
89 /* Dummy read: push CH1 setting down to hardware */
90 spi_sync(st
->spi
, st
->ring_msg
);
92 case ((1 << 1) | (1 << 0)):
93 st
->ring_msg
= &st
->msg
[AD7887_CH0_CH1
];
100 static int ad7887_ring_postdisable(struct iio_dev
*indio_dev
)
102 struct ad7887_state
*st
= iio_priv(indio_dev
);
104 /* dummy read: restore default CH0 settin */
105 return spi_sync(st
->spi
, &st
->msg
[AD7887_CH0
]);
109 * ad7887_trigger_handler() bh of trigger launched polling to ring buffer
111 * Currently there is no option in this driver to disable the saving of
112 * timestamps within the ring.
114 static irqreturn_t
ad7887_trigger_handler(int irq
, void *p
)
116 struct iio_poll_func
*pf
= p
;
117 struct iio_dev
*indio_dev
= pf
->indio_dev
;
118 struct ad7887_state
*st
= iio_priv(indio_dev
);
119 struct iio_ring_buffer
*ring
= indio_dev
->ring
;
124 unsigned int bytes
= ring
->scan_count
*
125 st
->chip_info
->channel
[0].scan_type
.storagebits
/ 8;
127 buf
= kzalloc(st
->d_size
, GFP_KERNEL
);
131 b_sent
= spi_sync(st
->spi
, st
->ring_msg
);
135 time_ns
= iio_get_time_ns();
137 memcpy(buf
, st
->data
, bytes
);
138 if (ring
->scan_timestamp
)
139 memcpy(buf
+ st
->d_size
- sizeof(s64
),
140 &time_ns
, sizeof(time_ns
));
142 indio_dev
->ring
->access
->store_to(indio_dev
->ring
, buf
, time_ns
);
145 iio_trigger_notify_done(indio_dev
->trig
);
150 static const struct iio_ring_setup_ops ad7887_ring_setup_ops
= {
151 .preenable
= &ad7887_ring_preenable
,
152 .postenable
= &iio_triggered_buffer_postenable
,
153 .predisable
= &iio_triggered_buffer_predisable
,
154 .postdisable
= &ad7887_ring_postdisable
,
157 int ad7887_register_ring_funcs_and_init(struct iio_dev
*indio_dev
)
161 indio_dev
->ring
= iio_sw_rb_allocate(indio_dev
);
162 if (!indio_dev
->ring
) {
166 /* Effectively select the ring buffer implementation */
167 indio_dev
->ring
->access
= &ring_sw_access_funcs
;
168 indio_dev
->pollfunc
= iio_alloc_pollfunc(&iio_pollfunc_store_time
,
169 &ad7887_trigger_handler
,
174 if (indio_dev
->pollfunc
== NULL
) {
176 goto error_deallocate_sw_rb
;
178 /* Ring buffer functions - here trigger setup related */
179 indio_dev
->ring
->setup_ops
= &ad7887_ring_setup_ops
;
181 /* Flag that polled ring buffering is possible */
182 indio_dev
->modes
|= INDIO_RING_TRIGGERED
;
185 error_deallocate_sw_rb
:
186 iio_sw_rb_free(indio_dev
->ring
);
191 void ad7887_ring_cleanup(struct iio_dev
*indio_dev
)
193 iio_dealloc_pollfunc(indio_dev
->pollfunc
);
194 iio_sw_rb_free(indio_dev
->ring
);