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/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 int ad7887_scan_from_ring(struct ad7887_state
*st
, long mask
)
27 struct iio_ring_buffer
*ring
= iio_priv_to_dev(st
)->ring
;
31 if (!(ring
->scan_mask
& mask
)) {
36 ring_data
= kmalloc(ring
->access
->get_bytes_per_datum(ring
),
38 if (ring_data
== NULL
) {
42 ret
= ring
->access
->read_last(ring
, (u8
*) ring_data
);
44 goto error_free_ring_data
;
46 /* for single channel scan the result is stored with zero offset */
47 if ((ring
->scan_mask
== ((1 << 1) | (1 << 0))) && (mask
== (1 << 1)))
50 ret
= be16_to_cpu(ring_data
[count
]);
59 * ad7887_ring_preenable() setup the parameters of the ring before enabling
61 * The complex nature of the setting of the nuber of bytes per datum is due
62 * to this driver currently ensuring that the timestamp is stored at an 8
65 static int ad7887_ring_preenable(struct iio_dev
*indio_dev
)
67 struct ad7887_state
*st
= iio_priv(indio_dev
);
68 struct iio_ring_buffer
*ring
= indio_dev
->ring
;
70 st
->d_size
= ring
->scan_count
*
71 st
->chip_info
->channel
[0].scan_type
.storagebits
/ 8;
73 if (ring
->scan_timestamp
) {
74 st
->d_size
+= sizeof(s64
);
76 if (st
->d_size
% sizeof(s64
))
77 st
->d_size
+= sizeof(s64
) - (st
->d_size
% sizeof(s64
));
80 if (indio_dev
->ring
->access
->set_bytes_per_datum
)
81 indio_dev
->ring
->access
->set_bytes_per_datum(indio_dev
->ring
,
84 switch (ring
->scan_mask
) {
86 st
->ring_msg
= &st
->msg
[AD7887_CH0
];
89 st
->ring_msg
= &st
->msg
[AD7887_CH1
];
90 /* Dummy read: push CH1 setting down to hardware */
91 spi_sync(st
->spi
, st
->ring_msg
);
93 case ((1 << 1) | (1 << 0)):
94 st
->ring_msg
= &st
->msg
[AD7887_CH0_CH1
];
101 static int ad7887_ring_postdisable(struct iio_dev
*indio_dev
)
103 struct ad7887_state
*st
= iio_priv(indio_dev
);
105 /* dummy read: restore default CH0 settin */
106 return spi_sync(st
->spi
, &st
->msg
[AD7887_CH0
]);
110 * ad7887_trigger_handler() bh of trigger launched polling to ring buffer
112 * Currently there is no option in this driver to disable the saving of
113 * timestamps within the ring.
115 static irqreturn_t
ad7887_trigger_handler(int irq
, void *p
)
117 struct iio_poll_func
*pf
= p
;
118 struct iio_dev
*indio_dev
= pf
->private_data
;
119 struct ad7887_state
*st
= iio_priv(indio_dev
);
120 struct iio_ring_buffer
*ring
= indio_dev
->ring
;
125 unsigned int bytes
= ring
->scan_count
*
126 st
->chip_info
->channel
[0].scan_type
.storagebits
/ 8;
128 buf
= kzalloc(st
->d_size
, GFP_KERNEL
);
132 b_sent
= spi_sync(st
->spi
, st
->ring_msg
);
136 time_ns
= iio_get_time_ns();
138 memcpy(buf
, st
->data
, bytes
);
139 if (ring
->scan_timestamp
)
140 memcpy(buf
+ st
->d_size
- sizeof(s64
),
141 &time_ns
, sizeof(time_ns
));
143 indio_dev
->ring
->access
->store_to(indio_dev
->ring
, buf
, time_ns
);
146 iio_trigger_notify_done(indio_dev
->trig
);
151 static const struct iio_ring_setup_ops ad7887_ring_setup_ops
= {
152 .preenable
= &ad7887_ring_preenable
,
153 .postenable
= &iio_triggered_ring_postenable
,
154 .predisable
= &iio_triggered_ring_predisable
,
155 .postdisable
= &ad7887_ring_postdisable
,
158 int ad7887_register_ring_funcs_and_init(struct iio_dev
*indio_dev
)
162 indio_dev
->ring
= iio_sw_rb_allocate(indio_dev
);
163 if (!indio_dev
->ring
) {
167 /* Effectively select the ring buffer implementation */
168 indio_dev
->ring
->access
= &ring_sw_access_funcs
;
169 indio_dev
->pollfunc
= iio_alloc_pollfunc(&iio_pollfunc_store_time
,
170 &ad7887_trigger_handler
,
175 if (indio_dev
->pollfunc
== NULL
) {
177 goto error_deallocate_sw_rb
;
179 /* Ring buffer functions - here trigger setup related */
180 indio_dev
->ring
->setup_ops
= &ad7887_ring_setup_ops
;
182 /* Flag that polled ring buffering is possible */
183 indio_dev
->modes
|= INDIO_RING_TRIGGERED
;
186 error_deallocate_sw_rb
:
187 iio_sw_rb_free(indio_dev
->ring
);
192 void ad7887_ring_cleanup(struct iio_dev
*indio_dev
)
194 /* ensure that the trigger has been detached */
195 if (indio_dev
->trig
) {
196 iio_put_trigger(indio_dev
->trig
);
197 iio_trigger_dettach_poll_func(indio_dev
->trig
,
198 indio_dev
->pollfunc
);
200 iio_dealloc_pollfunc(indio_dev
->pollfunc
);
201 iio_sw_rb_free(indio_dev
->ring
);