]> bbs.cooldavid.org Git - net-next-2.6.git/blob - drivers/staging/iio/accel/adis16209_ring.c
staging: iio: adis16209 driver
[net-next-2.6.git] / drivers / staging / iio / accel / adis16209_ring.c
1 #include <linux/interrupt.h>
2 #include <linux/irq.h>
3 #include <linux/gpio.h>
4 #include <linux/workqueue.h>
5 #include <linux/mutex.h>
6 #include <linux/device.h>
7 #include <linux/kernel.h>
8 #include <linux/spi/spi.h>
9 #include <linux/sysfs.h>
10 #include <linux/list.h>
11
12 #include "../iio.h"
13 #include "../sysfs.h"
14 #include "../ring_sw.h"
15 #include "accel.h"
16 #include "../trigger.h"
17 #include "adis16209.h"
18
19 /**
20  * combine_8_to_16() utility function to munge to u8s into u16
21  **/
22 static inline u16 combine_8_to_16(u8 lower, u8 upper)
23 {
24         u16 _lower = lower;
25         u16 _upper = upper;
26         return _lower | (_upper << 8);
27 }
28
29 static IIO_SCAN_EL_C(supply, ADIS16209_SCAN_SUPPLY, IIO_UNSIGNED(14),
30                      ADIS16209_SUPPLY_OUT, NULL);
31 static IIO_SCAN_EL_C(accel_x, ADIS16209_SCAN_ACC_X, IIO_SIGNED(14),
32                      ADIS16209_XACCL_OUT, NULL);
33 static IIO_SCAN_EL_C(accel_y, ADIS16209_SCAN_ACC_Y, IIO_SIGNED(14),
34                      ADIS16209_YACCL_OUT, NULL);
35 static IIO_SCAN_EL_C(aux_adc, ADIS16209_SCAN_AUX_ADC, IIO_UNSIGNED(12),
36                      ADIS16209_AUX_ADC, NULL);
37 static IIO_SCAN_EL_C(temp, ADIS16209_SCAN_TEMP, IIO_UNSIGNED(12),
38                      ADIS16209_TEMP_OUT, NULL);
39 static IIO_SCAN_EL_C(incli_x, ADIS16209_SCAN_INCLI_X, IIO_SIGNED(14),
40                      ADIS16209_XINCL_OUT, NULL);
41 static IIO_SCAN_EL_C(incli_y, ADIS16209_SCAN_INCLI_Y, IIO_SIGNED(14),
42                      ADIS16209_YINCL_OUT, NULL);
43 static IIO_SCAN_EL_C(rot, ADIS16209_SCAN_ROT, IIO_SIGNED(14),
44                      ADIS16209_ROT_OUT, NULL);
45
46 static IIO_SCAN_EL_TIMESTAMP(8);
47
48 static struct attribute *adis16209_scan_el_attrs[] = {
49         &iio_scan_el_supply.dev_attr.attr,
50         &iio_scan_el_accel_x.dev_attr.attr,
51         &iio_scan_el_accel_y.dev_attr.attr,
52         &iio_scan_el_aux_adc.dev_attr.attr,
53         &iio_scan_el_temp.dev_attr.attr,
54         &iio_scan_el_incli_x.dev_attr.attr,
55         &iio_scan_el_incli_y.dev_attr.attr,
56         &iio_scan_el_rot.dev_attr.attr,
57         &iio_scan_el_timestamp.dev_attr.attr,
58         NULL,
59 };
60
61 static struct attribute_group adis16209_scan_el_group = {
62         .attrs = adis16209_scan_el_attrs,
63         .name = "scan_elements",
64 };
65
66 /**
67  * adis16209_poll_func_th() top half interrupt handler called by trigger
68  * @private_data:       iio_dev
69  **/
70 static void adis16209_poll_func_th(struct iio_dev *indio_dev)
71 {
72         struct adis16209_state *st = iio_dev_get_devdata(indio_dev);
73         st->last_timestamp = indio_dev->trig->timestamp;
74         schedule_work(&st->work_trigger_to_ring);
75 }
76
77 /**
78  * adis16209_read_ring_data() read data registers which will be placed into ring
79  * @dev: device associated with child of actual device (iio_dev or iio_trig)
80  * @rx: somewhere to pass back the value read
81  **/
82 static int adis16209_read_ring_data(struct device *dev, u8 *rx)
83 {
84         struct spi_message msg;
85         struct iio_dev *indio_dev = dev_get_drvdata(dev);
86         struct adis16209_state *st = iio_dev_get_devdata(indio_dev);
87         struct spi_transfer xfers[ADIS16209_OUTPUTS + 1];
88         int ret;
89         int i;
90
91         mutex_lock(&st->buf_lock);
92
93         spi_message_init(&msg);
94
95         memset(xfers, 0, sizeof(xfers));
96         for (i = 0; i <= ADIS16209_OUTPUTS; i++) {
97                 xfers[i].bits_per_word = 8;
98                 xfers[i].cs_change = 1;
99                 xfers[i].len = 2;
100                 xfers[i].delay_usecs = 20;
101                 xfers[i].tx_buf = st->tx + 2 * i;
102                 st->tx[2 * i]
103                         = ADIS16209_READ_REG(ADIS16209_SUPPLY_OUT + 2 * i);
104                 st->tx[2 * i + 1] = 0;
105                 if (i >= 1)
106                         xfers[i].rx_buf = rx + 2 * (i - 1);
107                 spi_message_add_tail(&xfers[i], &msg);
108         }
109
110         ret = spi_sync(st->us, &msg);
111         if (ret)
112                 dev_err(&st->us->dev, "problem when burst reading");
113
114         mutex_unlock(&st->buf_lock);
115
116         return ret;
117 }
118
119 /* Whilst this makes a lot of calls to iio_sw_ring functions - it is to device
120  * specific to be rolled into the core.
121  */
122 static void adis16209_trigger_bh_to_ring(struct work_struct *work_s)
123 {
124         struct adis16209_state *st
125                 = container_of(work_s, struct adis16209_state,
126                                work_trigger_to_ring);
127
128         int i = 0;
129         s16 *data;
130         size_t datasize = st->indio_dev
131                 ->ring->access.get_bpd(st->indio_dev->ring);
132
133         data = kmalloc(datasize , GFP_KERNEL);
134         if (data == NULL) {
135                 dev_err(&st->us->dev, "memory alloc failed in ring bh");
136                 return;
137         }
138
139         if (st->indio_dev->scan_count)
140                 if (adis16209_read_ring_data(&st->indio_dev->dev, st->rx) >= 0)
141                         for (; i < st->indio_dev->scan_count; i++) {
142                                 data[i] = combine_8_to_16(st->rx[i*2+1],
143                                                           st->rx[i*2]);
144                         }
145
146         /* Guaranteed to be aligned with 8 byte boundary */
147         if (st->indio_dev->scan_timestamp)
148                 *((s64 *)(data + ((i + 3)/4)*4)) = st->last_timestamp;
149
150         st->indio_dev->ring->access.store_to(st->indio_dev->ring,
151                                             (u8 *)data,
152                                             st->last_timestamp);
153
154         iio_trigger_notify_done(st->indio_dev->trig);
155         kfree(data);
156
157         return;
158 }
159
160 /* in these circumstances is it better to go with unaligned packing and
161  * deal with the cost?*/
162 static int adis16209_data_rdy_ring_preenable(struct iio_dev *indio_dev)
163 {
164         size_t size;
165         dev_dbg(&indio_dev->dev, "%s\n", __func__);
166         /* Check if there are any scan elements enabled, if not fail*/
167         if (!(indio_dev->scan_count || indio_dev->scan_timestamp))
168                 return -EINVAL;
169
170         if (indio_dev->ring->access.set_bpd) {
171                 if (indio_dev->scan_timestamp)
172                         if (indio_dev->scan_count)
173                                 /* Timestamp (aligned to s64) and data */
174                                 size = (((indio_dev->scan_count * sizeof(s16))
175                                          + sizeof(s64) - 1)
176                                         & ~(sizeof(s64) - 1))
177                                         + sizeof(s64);
178                         else /* Timestamp only  */
179                                 size = sizeof(s64);
180                 else /* Data only */
181                         size = indio_dev->scan_count*sizeof(s16);
182                 indio_dev->ring->access.set_bpd(indio_dev->ring, size);
183         }
184
185         return 0;
186 }
187
188 static int adis16209_data_rdy_ring_postenable(struct iio_dev *indio_dev)
189 {
190         return indio_dev->trig
191                 ? iio_trigger_attach_poll_func(indio_dev->trig,
192                                                indio_dev->pollfunc)
193                 : 0;
194 }
195
196 static int adis16209_data_rdy_ring_predisable(struct iio_dev *indio_dev)
197 {
198         return indio_dev->trig
199                 ? iio_trigger_dettach_poll_func(indio_dev->trig,
200                                                 indio_dev->pollfunc)
201                 : 0;
202 }
203
204 void adis16209_unconfigure_ring(struct iio_dev *indio_dev)
205 {
206         kfree(indio_dev->pollfunc);
207         iio_sw_rb_free(indio_dev->ring);
208 }
209
210 int adis16209_configure_ring(struct iio_dev *indio_dev)
211 {
212         int ret = 0;
213         struct adis16209_state *st = indio_dev->dev_data;
214         struct iio_ring_buffer *ring;
215         INIT_WORK(&st->work_trigger_to_ring, adis16209_trigger_bh_to_ring);
216         /* Set default scan mode */
217
218         iio_scan_mask_set(indio_dev, iio_scan_el_supply.number);
219         iio_scan_mask_set(indio_dev, iio_scan_el_rot.number);
220         iio_scan_mask_set(indio_dev, iio_scan_el_accel_x.number);
221         iio_scan_mask_set(indio_dev, iio_scan_el_accel_y.number);
222         iio_scan_mask_set(indio_dev, iio_scan_el_temp.number);
223         iio_scan_mask_set(indio_dev, iio_scan_el_aux_adc.number);
224         iio_scan_mask_set(indio_dev, iio_scan_el_incli_x.number);
225         iio_scan_mask_set(indio_dev, iio_scan_el_incli_y.number);
226         indio_dev->scan_timestamp = true;
227
228         indio_dev->scan_el_attrs = &adis16209_scan_el_group;
229
230         ring = iio_sw_rb_allocate(indio_dev);
231         if (!ring) {
232                 ret = -ENOMEM;
233                 return ret;
234         }
235         indio_dev->ring = ring;
236         /* Effectively select the ring buffer implementation */
237         iio_ring_sw_register_funcs(&ring->access);
238         ring->preenable = &adis16209_data_rdy_ring_preenable;
239         ring->postenable = &adis16209_data_rdy_ring_postenable;
240         ring->predisable = &adis16209_data_rdy_ring_predisable;
241         ring->owner = THIS_MODULE;
242
243         indio_dev->pollfunc = kzalloc(sizeof(*indio_dev->pollfunc), GFP_KERNEL);
244         if (indio_dev->pollfunc == NULL) {
245                 ret = -ENOMEM;
246                 goto error_iio_sw_rb_free;;
247         }
248         indio_dev->pollfunc->poll_func_main = &adis16209_poll_func_th;
249         indio_dev->pollfunc->private_data = indio_dev;
250         indio_dev->modes |= INDIO_RING_TRIGGERED;
251         return 0;
252
253 error_iio_sw_rb_free:
254         iio_sw_rb_free(indio_dev->ring);
255         return ret;
256 }
257
258 int adis16209_initialize_ring(struct iio_ring_buffer *ring)
259 {
260         return iio_ring_buffer_register(ring, 0);
261 }
262
263 void adis16209_uninitialize_ring(struct iio_ring_buffer *ring)
264 {
265         iio_ring_buffer_unregister(ring);
266 }