Staging: iio: pull in slab.h for kmalloc funcs
[pandora-kernel.git] / drivers / staging / iio / imu / adis16300_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/slab.h>
10 #include <linux/sysfs.h>
11 #include <linux/list.h>
12
13 #include "../iio.h"
14 #include "../sysfs.h"
15 #include "../ring_sw.h"
16 #include "../accel/accel.h"
17 #include "../trigger.h"
18 #include "adis16300.h"
19
20 /**
21  * combine_8_to_16() utility function to munge to u8s into u16
22  **/
23 static inline u16 combine_8_to_16(u8 lower, u8 upper)
24 {
25         u16 _lower = lower;
26         u16 _upper = upper;
27         return _lower | (_upper << 8);
28 }
29
30 static IIO_SCAN_EL_C(supply, ADIS16300_SCAN_SUPPLY, IIO_SIGNED(14),
31                      ADIS16300_SUPPLY_OUT, NULL);
32
33 static IIO_SCAN_EL_C(gyro_x, ADIS16300_SCAN_GYRO_X, IIO_SIGNED(14),
34                      ADIS16300_XGYRO_OUT, NULL);
35
36 static IIO_SCAN_EL_C(accel_x, ADIS16300_SCAN_ACC_X, IIO_SIGNED(14),
37                      ADIS16300_XACCL_OUT, NULL);
38 static IIO_SCAN_EL_C(accel_y, ADIS16300_SCAN_ACC_Y, IIO_SIGNED(14),
39                      ADIS16300_YACCL_OUT, NULL);
40 static IIO_SCAN_EL_C(accel_z, ADIS16300_SCAN_ACC_Z, IIO_SIGNED(14),
41                      ADIS16300_ZACCL_OUT, NULL);
42
43 static IIO_SCAN_EL_C(temp, ADIS16300_SCAN_TEMP, IIO_SIGNED(12),
44                      ADIS16300_TEMP_OUT, NULL);
45 static IIO_SCAN_EL_C(adc_0, ADIS16300_SCAN_ADC_0, IIO_SIGNED(12),
46                      ADIS16300_AUX_ADC, NULL);
47
48 static IIO_SCAN_EL_C(incli_x, ADIS16300_SCAN_INCLI_X, IIO_SIGNED(12),
49                      ADIS16300_XINCLI_OUT, NULL);
50 static IIO_SCAN_EL_C(incli_y, ADIS16300_SCAN_INCLI_Y, IIO_SIGNED(12),
51                      ADIS16300_YINCLI_OUT, NULL);
52
53 static IIO_SCAN_EL_TIMESTAMP(9);
54
55 static struct attribute *adis16300_scan_el_attrs[] = {
56         &iio_scan_el_supply.dev_attr.attr,
57         &iio_scan_el_gyro_x.dev_attr.attr,
58         &iio_scan_el_temp.dev_attr.attr,
59         &iio_scan_el_accel_x.dev_attr.attr,
60         &iio_scan_el_accel_y.dev_attr.attr,
61         &iio_scan_el_accel_z.dev_attr.attr,
62         &iio_scan_el_incli_x.dev_attr.attr,
63         &iio_scan_el_incli_y.dev_attr.attr,
64         &iio_scan_el_adc_0.dev_attr.attr,
65         &iio_scan_el_timestamp.dev_attr.attr,
66         NULL,
67 };
68
69 static struct attribute_group adis16300_scan_el_group = {
70         .attrs = adis16300_scan_el_attrs,
71         .name = "scan_elements",
72 };
73
74 /**
75  * adis16300_poll_func_th() top half interrupt handler called by trigger
76  * @private_data:       iio_dev
77  **/
78 static void adis16300_poll_func_th(struct iio_dev *indio_dev)
79 {
80         struct adis16300_state *st = iio_dev_get_devdata(indio_dev);
81         st->last_timestamp = indio_dev->trig->timestamp;
82         schedule_work(&st->work_trigger_to_ring);
83         /* Indicate that this interrupt is being handled */
84
85         /* Technically this is trigger related, but without this
86          * handler running there is currently no way for the interrupt
87          * to clear.
88          */
89 }
90
91 /* Whilst this makes a lot of calls to iio_sw_ring functions - it is to device
92  * specific to be rolled into the core.
93  */
94 static void adis16300_trigger_bh_to_ring(struct work_struct *work_s)
95 {
96         struct adis16300_state *st
97                 = container_of(work_s, struct adis16300_state,
98                                work_trigger_to_ring);
99
100         int i = 0;
101         s16 *data;
102         size_t datasize = st->indio_dev
103                 ->ring->access.get_bpd(st->indio_dev->ring);
104
105         data = kmalloc(datasize , GFP_KERNEL);
106         if (data == NULL) {
107                 dev_err(&st->us->dev, "memory alloc failed in ring bh");
108                 return;
109         }
110
111         if (st->indio_dev->scan_count)
112                 if (adis16300_spi_read_burst(&st->indio_dev->dev, st->rx) >= 0)
113                         for (; i < st->indio_dev->scan_count; i++) {
114                                 data[i] = combine_8_to_16(st->rx[i*2+1],
115                                                           st->rx[i*2]);
116                         }
117
118         /* Guaranteed to be aligned with 8 byte boundary */
119         if (st->indio_dev->scan_timestamp)
120                 *((s64 *)(data + ((i + 3)/4)*4)) = st->last_timestamp;
121
122         st->indio_dev->ring->access.store_to(st->indio_dev->ring,
123                                             (u8 *)data,
124                                             st->last_timestamp);
125
126         iio_trigger_notify_done(st->indio_dev->trig);
127         kfree(data);
128
129         return;
130 }
131 /* in these circumstances is it better to go with unaligned packing and
132  * deal with the cost?*/
133 static int adis16300_data_rdy_ring_preenable(struct iio_dev *indio_dev)
134 {
135         size_t size;
136         dev_dbg(&indio_dev->dev, "%s\n", __func__);
137         /* Check if there are any scan elements enabled, if not fail*/
138         if (!(indio_dev->scan_count || indio_dev->scan_timestamp))
139                 return -EINVAL;
140
141         if (indio_dev->ring->access.set_bpd) {
142                 if (indio_dev->scan_timestamp)
143                         if (indio_dev->scan_count) /* Timestamp and data */
144                                 size = 4*sizeof(s64);
145                         else /* Timestamp only  */
146                                 size = sizeof(s64);
147                 else /* Data only */
148                         size = indio_dev->scan_count*sizeof(s16);
149                 indio_dev->ring->access.set_bpd(indio_dev->ring, size);
150         }
151
152         return 0;
153 }
154
155 static int adis16300_data_rdy_ring_postenable(struct iio_dev *indio_dev)
156 {
157         return indio_dev->trig
158                 ? iio_trigger_attach_poll_func(indio_dev->trig,
159                                                indio_dev->pollfunc)
160                 : 0;
161 }
162
163 static int adis16300_data_rdy_ring_predisable(struct iio_dev *indio_dev)
164 {
165         return indio_dev->trig
166                 ? iio_trigger_dettach_poll_func(indio_dev->trig,
167                                                 indio_dev->pollfunc)
168                 : 0;
169 }
170
171 void adis16300_unconfigure_ring(struct iio_dev *indio_dev)
172 {
173         kfree(indio_dev->pollfunc);
174         iio_sw_rb_free(indio_dev->ring);
175 }
176
177 int adis16300_configure_ring(struct iio_dev *indio_dev)
178 {
179         int ret = 0;
180         struct adis16300_state *st = indio_dev->dev_data;
181         struct iio_ring_buffer *ring;
182         INIT_WORK(&st->work_trigger_to_ring, adis16300_trigger_bh_to_ring);
183         /* Set default scan mode */
184
185         iio_scan_mask_set(indio_dev, iio_scan_el_supply.number);
186         iio_scan_mask_set(indio_dev, iio_scan_el_gyro_x.number);
187         iio_scan_mask_set(indio_dev, iio_scan_el_accel_x.number);
188         iio_scan_mask_set(indio_dev, iio_scan_el_accel_y.number);
189         iio_scan_mask_set(indio_dev, iio_scan_el_accel_z.number);
190         iio_scan_mask_set(indio_dev, iio_scan_el_temp.number);
191         iio_scan_mask_set(indio_dev, iio_scan_el_adc_0.number);
192         iio_scan_mask_set(indio_dev, iio_scan_el_incli_x.number);
193         iio_scan_mask_set(indio_dev, iio_scan_el_incli_y.number);
194         indio_dev->scan_timestamp = true;
195
196         indio_dev->scan_el_attrs = &adis16300_scan_el_group;
197
198         ring = iio_sw_rb_allocate(indio_dev);
199         if (!ring) {
200                 ret = -ENOMEM;
201                 return ret;
202         }
203         indio_dev->ring = ring;
204         /* Effectively select the ring buffer implementation */
205         iio_ring_sw_register_funcs(&ring->access);
206         ring->preenable = &adis16300_data_rdy_ring_preenable;
207         ring->postenable = &adis16300_data_rdy_ring_postenable;
208         ring->predisable = &adis16300_data_rdy_ring_predisable;
209         ring->owner = THIS_MODULE;
210
211         indio_dev->pollfunc = kzalloc(sizeof(*indio_dev->pollfunc), GFP_KERNEL);
212         if (indio_dev->pollfunc == NULL) {
213                 ret = -ENOMEM;
214                 goto error_iio_sw_rb_free;;
215         }
216         indio_dev->pollfunc->poll_func_main = &adis16300_poll_func_th;
217         indio_dev->pollfunc->private_data = indio_dev;
218         indio_dev->modes |= INDIO_RING_TRIGGERED;
219         return 0;
220
221 error_iio_sw_rb_free:
222         iio_sw_rb_free(indio_dev->ring);
223         return ret;
224 }
225
226 int adis16300_initialize_ring(struct iio_ring_buffer *ring)
227 {
228         return iio_ring_buffer_register(ring, 0);
229 }
230
231 void adis16300_uninitialize_ring(struct iio_ring_buffer *ring)
232 {
233         iio_ring_buffer_unregister(ring);
234 }