Staging: iio: pull in slab.h for kmalloc funcs
[pandora-kernel.git] / drivers / staging / iio / imu / adis16350_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 "adis16350.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, ADIS16350_SCAN_SUPPLY, IIO_UNSIGNED(12),
31                 ADIS16350_SUPPLY_OUT, NULL);
32
33 static IIO_SCAN_EL_C(gyro_x, ADIS16350_SCAN_GYRO_X, IIO_SIGNED(14),
34                 ADIS16350_XGYRO_OUT, NULL);
35 static IIO_SCAN_EL_C(gyro_y, ADIS16350_SCAN_GYRO_Y, IIO_SIGNED(14),
36                 ADIS16350_YGYRO_OUT, NULL);
37 static IIO_SCAN_EL_C(gyro_z, ADIS16350_SCAN_GYRO_Z, IIO_SIGNED(14),
38                 ADIS16350_ZGYRO_OUT, NULL);
39
40 static IIO_SCAN_EL_C(accel_x, ADIS16350_SCAN_ACC_X, IIO_SIGNED(14),
41                 ADIS16350_XACCL_OUT, NULL);
42 static IIO_SCAN_EL_C(accel_y, ADIS16350_SCAN_ACC_Y, IIO_SIGNED(14),
43                 ADIS16350_YACCL_OUT, NULL);
44 static IIO_SCAN_EL_C(accel_z, ADIS16350_SCAN_ACC_Z, IIO_SIGNED(14),
45                 ADIS16350_ZACCL_OUT, NULL);
46
47 static IIO_SCAN_EL_C(temp_x, ADIS16350_SCAN_TEMP_X, IIO_SIGNED(12),
48                 ADIS16350_XTEMP_OUT, NULL);
49 static IIO_SCAN_EL_C(temp_y, ADIS16350_SCAN_TEMP_Y, IIO_SIGNED(12),
50                 ADIS16350_YTEMP_OUT, NULL);
51 static IIO_SCAN_EL_C(temp_z, ADIS16350_SCAN_TEMP_Z, IIO_SIGNED(12),
52                 ADIS16350_ZTEMP_OUT, NULL);
53
54 static IIO_SCAN_EL_C(adc_0, ADIS16350_SCAN_ADC_0, IIO_UNSIGNED(12),
55                 ADIS16350_AUX_ADC, NULL);
56
57 static IIO_SCAN_EL_TIMESTAMP(11);
58
59 static struct attribute *adis16350_scan_el_attrs[] = {
60         &iio_scan_el_supply.dev_attr.attr,
61         &iio_scan_el_gyro_x.dev_attr.attr,
62         &iio_scan_el_gyro_y.dev_attr.attr,
63         &iio_scan_el_gyro_z.dev_attr.attr,
64         &iio_scan_el_accel_x.dev_attr.attr,
65         &iio_scan_el_accel_y.dev_attr.attr,
66         &iio_scan_el_accel_z.dev_attr.attr,
67         &iio_scan_el_temp_x.dev_attr.attr,
68         &iio_scan_el_temp_y.dev_attr.attr,
69         &iio_scan_el_temp_z.dev_attr.attr,
70         &iio_scan_el_adc_0.dev_attr.attr,
71         &iio_scan_el_timestamp.dev_attr.attr,
72         NULL,
73 };
74
75 static struct attribute_group adis16350_scan_el_group = {
76         .attrs = adis16350_scan_el_attrs,
77         .name = "scan_elements",
78 };
79
80 /**
81  * adis16350_poll_func_th() top half interrupt handler called by trigger
82  * @private_data:       iio_dev
83  **/
84 static void adis16350_poll_func_th(struct iio_dev *indio_dev)
85 {
86         struct adis16350_state *st = iio_dev_get_devdata(indio_dev);
87         st->last_timestamp = indio_dev->trig->timestamp;
88         schedule_work(&st->work_trigger_to_ring);
89 }
90
91 /**
92  * adis16350_spi_read_burst() - read all data registers
93  * @dev: device associated with child of actual device (iio_dev or iio_trig)
94  * @rx: somewhere to pass back the value read (min size is 24 bytes)
95  **/
96 static int adis16350_spi_read_burst(struct device *dev, u8 *rx)
97 {
98         struct spi_message msg;
99         struct iio_dev *indio_dev = dev_get_drvdata(dev);
100         struct adis16350_state *st = iio_dev_get_devdata(indio_dev);
101         u32 old_speed_hz = st->us->max_speed_hz;
102         int ret;
103
104         struct spi_transfer xfers[] = {
105                 {
106                         .tx_buf = st->tx,
107                         .bits_per_word = 8,
108                         .len = 2,
109                         .cs_change = 0,
110                 }, {
111                         .rx_buf = rx,
112                         .bits_per_word = 8,
113                         .len = 22,
114                         .cs_change = 0,
115                 },
116         };
117
118         mutex_lock(&st->buf_lock);
119         st->tx[0] = ADIS16350_READ_REG(ADIS16350_GLOB_CMD);
120         st->tx[1] = 0;
121
122         spi_message_init(&msg);
123         spi_message_add_tail(&xfers[0], &msg);
124         spi_message_add_tail(&xfers[1], &msg);
125
126         st->us->max_speed_hz = ADIS16350_SPI_BURST;
127         spi_setup(st->us);
128
129         ret = spi_sync(st->us, &msg);
130         if (ret)
131                 dev_err(&st->us->dev, "problem when burst reading");
132
133         st->us->max_speed_hz = old_speed_hz;
134         spi_setup(st->us);
135         mutex_unlock(&st->buf_lock);
136         return ret;
137 }
138
139 /* Whilst this makes a lot of calls to iio_sw_ring functions - it is to device
140  * specific to be rolled into the core.
141  */
142 static void adis16350_trigger_bh_to_ring(struct work_struct *work_s)
143 {
144         struct adis16350_state *st
145                 = container_of(work_s, struct adis16350_state,
146                                work_trigger_to_ring);
147
148         int i = 0;
149         s16 *data;
150         size_t datasize = st->indio_dev
151                 ->ring->access.get_bpd(st->indio_dev->ring);
152
153         data = kmalloc(datasize , GFP_KERNEL);
154         if (data == NULL) {
155                 dev_err(&st->us->dev, "memory alloc failed in ring bh");
156                 return;
157         }
158
159         if (st->indio_dev->scan_count)
160                 if (adis16350_spi_read_burst(&st->indio_dev->dev, st->rx) >= 0)
161                         for (; i < st->indio_dev->scan_count; i++) {
162                                 data[i] = combine_8_to_16(st->rx[i*2+1],
163                                                           st->rx[i*2]);
164                         }
165
166         /* Guaranteed to be aligned with 8 byte boundary */
167         if (st->indio_dev->scan_timestamp)
168                 *((s64 *)(data + ((i + 3)/4)*4)) = st->last_timestamp;
169
170         st->indio_dev->ring->access.store_to(st->indio_dev->ring,
171                                             (u8 *)data,
172                                             st->last_timestamp);
173
174         iio_trigger_notify_done(st->indio_dev->trig);
175         kfree(data);
176
177         return;
178 }
179
180 static int adis16350_data_rdy_ring_preenable(struct iio_dev *indio_dev)
181 {
182         size_t size;
183         dev_dbg(&indio_dev->dev, "%s\n", __func__);
184         /* Check if there are any scan elements enabled, if not fail*/
185         if (!(indio_dev->scan_count || indio_dev->scan_timestamp))
186                 return -EINVAL;
187
188         if (indio_dev->ring->access.set_bpd) {
189                 if (indio_dev->scan_timestamp)
190                         if (indio_dev->scan_count)
191                                 /* Timestamp (aligned sizeof(s64) and data */
192                                 size = (((indio_dev->scan_count * sizeof(s16))
193                                                 + sizeof(s64) - 1)
194                                         & ~(sizeof(s64) - 1))
195                                         + sizeof(s64);
196                         else /* Timestamp only  */
197                                 size = sizeof(s64);
198                 else /* Data only */
199                         size = indio_dev->scan_count*sizeof(s16);
200                 indio_dev->ring->access.set_bpd(indio_dev->ring, size);
201         }
202
203         return 0;
204 }
205
206 static int adis16350_data_rdy_ring_postenable(struct iio_dev *indio_dev)
207 {
208         return indio_dev->trig
209                 ? iio_trigger_attach_poll_func(indio_dev->trig,
210                                                indio_dev->pollfunc)
211                 : 0;
212 }
213
214 static int adis16350_data_rdy_ring_predisable(struct iio_dev *indio_dev)
215 {
216         return indio_dev->trig
217                 ? iio_trigger_dettach_poll_func(indio_dev->trig,
218                                                 indio_dev->pollfunc)
219                 : 0;
220 }
221
222 void adis16350_unconfigure_ring(struct iio_dev *indio_dev)
223 {
224         kfree(indio_dev->pollfunc);
225         iio_sw_rb_free(indio_dev->ring);
226 }
227
228 int adis16350_configure_ring(struct iio_dev *indio_dev)
229 {
230         int ret = 0;
231         struct adis16350_state *st = indio_dev->dev_data;
232         struct iio_ring_buffer *ring;
233         INIT_WORK(&st->work_trigger_to_ring, adis16350_trigger_bh_to_ring);
234         /* Set default scan mode */
235
236         iio_scan_mask_set(indio_dev, iio_scan_el_supply.number);
237         iio_scan_mask_set(indio_dev, iio_scan_el_gyro_x.number);
238         iio_scan_mask_set(indio_dev, iio_scan_el_gyro_y.number);
239         iio_scan_mask_set(indio_dev, iio_scan_el_gyro_z.number);
240         iio_scan_mask_set(indio_dev, iio_scan_el_accel_x.number);
241         iio_scan_mask_set(indio_dev, iio_scan_el_accel_y.number);
242         iio_scan_mask_set(indio_dev, iio_scan_el_accel_z.number);
243         iio_scan_mask_set(indio_dev, iio_scan_el_temp_x.number);
244         iio_scan_mask_set(indio_dev, iio_scan_el_temp_y.number);
245         iio_scan_mask_set(indio_dev, iio_scan_el_temp_z.number);
246         iio_scan_mask_set(indio_dev, iio_scan_el_adc_0.number);
247         indio_dev->scan_timestamp = true;
248
249         indio_dev->scan_el_attrs = &adis16350_scan_el_group;
250
251         ring = iio_sw_rb_allocate(indio_dev);
252         if (!ring) {
253                 ret = -ENOMEM;
254                 return ret;
255         }
256         indio_dev->ring = ring;
257         /* Effectively select the ring buffer implementation */
258         iio_ring_sw_register_funcs(&ring->access);
259         ring->preenable = &adis16350_data_rdy_ring_preenable;
260         ring->postenable = &adis16350_data_rdy_ring_postenable;
261         ring->predisable = &adis16350_data_rdy_ring_predisable;
262         ring->owner = THIS_MODULE;
263
264         indio_dev->pollfunc = kzalloc(sizeof(*indio_dev->pollfunc), GFP_KERNEL);
265         if (indio_dev->pollfunc == NULL) {
266                 ret = -ENOMEM;
267                 goto error_iio_sw_rb_free;;
268         }
269         indio_dev->pollfunc->poll_func_main = &adis16350_poll_func_th;
270         indio_dev->pollfunc->private_data = indio_dev;
271         indio_dev->modes |= INDIO_RING_TRIGGERED;
272         return 0;
273
274 error_iio_sw_rb_free:
275         iio_sw_rb_free(indio_dev->ring);
276         return ret;
277 }
278
279 int adis16350_initialize_ring(struct iio_ring_buffer *ring)
280 {
281         return iio_ring_buffer_register(ring, 0);
282 }
283
284 void adis16350_uninitialize_ring(struct iio_ring_buffer *ring)
285 {
286         iio_ring_buffer_unregister(ring);
287 }