staging: iio: Make extensive use of iio_sw_ring_preenable
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / staging / iio / accel / lis3l02dq_ring.c
blob2c112093609a914d9c92ea3ab0a58c836bd6387a
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 #include <linux/slab.h>
13 #include "../iio.h"
14 #include "../sysfs.h"
15 #include "../ring_sw.h"
16 #include "accel.h"
17 #include "../trigger.h"
18 #include "lis3l02dq.h"
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)
25 u16 _lower = lower;
26 u16 _upper = upper;
27 return _lower | (_upper << 8);
30 /**
31 * lis3l02dq_scan_el_set_state() set whether a scan contains a given channel
32 * @scan_el: associtate iio scan element attribute
33 * @indio_dev: the device structure
34 * @bool: desired state
36 * mlock already held when this is called.
37 **/
38 static int lis3l02dq_scan_el_set_state(struct iio_scan_el *scan_el,
39 struct iio_dev *indio_dev,
40 bool state)
42 u8 t, mask;
43 int ret;
45 ret = lis3l02dq_spi_read_reg_8(&indio_dev->dev,
46 LIS3L02DQ_REG_CTRL_1_ADDR,
47 &t);
48 if (ret)
49 goto error_ret;
50 switch (scan_el->label) {
51 case LIS3L02DQ_REG_OUT_X_L_ADDR:
52 mask = LIS3L02DQ_REG_CTRL_1_AXES_X_ENABLE;
53 break;
54 case LIS3L02DQ_REG_OUT_Y_L_ADDR:
55 mask = LIS3L02DQ_REG_CTRL_1_AXES_Y_ENABLE;
56 break;
57 case LIS3L02DQ_REG_OUT_Z_L_ADDR:
58 mask = LIS3L02DQ_REG_CTRL_1_AXES_Z_ENABLE;
59 break;
60 default:
61 ret = -EINVAL;
62 goto error_ret;
65 if (!(mask & t) == state) {
66 if (state)
67 t |= mask;
68 else
69 t &= ~mask;
70 ret = lis3l02dq_spi_write_reg_8(&indio_dev->dev,
71 LIS3L02DQ_REG_CTRL_1_ADDR,
72 &t);
74 error_ret:
75 return ret;
78 static IIO_SCAN_EL_C(accel_x, 0, IIO_SIGNED(16),
79 LIS3L02DQ_REG_OUT_X_L_ADDR,
80 &lis3l02dq_scan_el_set_state);
81 static IIO_SCAN_EL_C(accel_y, 1, IIO_SIGNED(16),
82 LIS3L02DQ_REG_OUT_Y_L_ADDR,
83 &lis3l02dq_scan_el_set_state);
84 static IIO_SCAN_EL_C(accel_z, 2, IIO_SIGNED(16),
85 LIS3L02DQ_REG_OUT_Z_L_ADDR,
86 &lis3l02dq_scan_el_set_state);
87 static IIO_SCAN_EL_TIMESTAMP(3);
89 static struct attribute *lis3l02dq_scan_el_attrs[] = {
90 &iio_scan_el_accel_x.dev_attr.attr,
91 &iio_scan_el_accel_y.dev_attr.attr,
92 &iio_scan_el_accel_z.dev_attr.attr,
93 &iio_scan_el_timestamp.dev_attr.attr,
94 NULL,
97 static struct attribute_group lis3l02dq_scan_el_group = {
98 .attrs = lis3l02dq_scan_el_attrs,
99 .name = "scan_elements",
103 * lis3l02dq_poll_func_th() top half interrupt handler called by trigger
104 * @private_data: iio_dev
106 static void lis3l02dq_poll_func_th(struct iio_dev *indio_dev, s64 time)
108 struct lis3l02dq_state *st = iio_dev_get_devdata(indio_dev);
109 st->last_timestamp = time;
110 schedule_work(&st->work_trigger_to_ring);
111 /* Indicate that this interrupt is being handled */
113 /* Technically this is trigger related, but without this
114 * handler running there is currently now way for the interrupt
115 * to clear.
117 st->inter = 1;
121 * lis3l02dq_data_rdy_trig_poll() the event handler for the data rdy trig
123 static int lis3l02dq_data_rdy_trig_poll(struct iio_dev *dev_info,
124 int index,
125 s64 timestamp,
126 int no_test)
128 struct lis3l02dq_state *st = iio_dev_get_devdata(dev_info);
129 struct iio_trigger *trig = st->trig;
131 iio_trigger_poll(trig, timestamp);
133 return IRQ_HANDLED;
136 /* This is an event as it is a response to a physical interrupt */
137 IIO_EVENT_SH(data_rdy_trig, &lis3l02dq_data_rdy_trig_poll);
140 * lis3l02dq_read_accel_from_ring() individual acceleration read from ring
142 ssize_t lis3l02dq_read_accel_from_ring(struct device *dev,
143 struct device_attribute *attr,
144 char *buf)
146 struct iio_scan_el *el = NULL;
147 int ret, len = 0, i = 0;
148 struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
149 struct iio_dev *dev_info = dev_get_drvdata(dev);
150 s16 *data;
152 while (dev_info->scan_el_attrs->attrs[i]) {
153 el = to_iio_scan_el((struct device_attribute *)
154 (dev_info->scan_el_attrs->attrs[i]));
155 /* label is in fact the address */
156 if (el->label == this_attr->address)
157 break;
158 i++;
160 if (!dev_info->scan_el_attrs->attrs[i]) {
161 ret = -EINVAL;
162 goto error_ret;
164 /* If this element is in the scan mask */
165 ret = iio_scan_mask_query(dev_info, el->number);
166 if (ret < 0)
167 goto error_ret;
168 if (ret) {
169 data = kmalloc(dev_info->ring->access.get_bpd(dev_info->ring),
170 GFP_KERNEL);
171 if (data == NULL)
172 return -ENOMEM;
173 ret = dev_info->ring->access.read_last(dev_info->ring,
174 (u8 *)data);
175 if (ret)
176 goto error_free_data;
177 } else {
178 ret = -EINVAL;
179 goto error_ret;
181 len = iio_scan_mask_count_to_right(dev_info, el->number);
182 if (len < 0) {
183 ret = len;
184 goto error_free_data;
186 len = sprintf(buf, "ring %d\n", data[len]);
187 error_free_data:
188 kfree(data);
189 error_ret:
190 return ret ? ret : len;
194 static const u8 read_all_tx_array[] = {
195 LIS3L02DQ_READ_REG(LIS3L02DQ_REG_OUT_X_L_ADDR), 0,
196 LIS3L02DQ_READ_REG(LIS3L02DQ_REG_OUT_X_H_ADDR), 0,
197 LIS3L02DQ_READ_REG(LIS3L02DQ_REG_OUT_Y_L_ADDR), 0,
198 LIS3L02DQ_READ_REG(LIS3L02DQ_REG_OUT_Y_H_ADDR), 0,
199 LIS3L02DQ_READ_REG(LIS3L02DQ_REG_OUT_Z_L_ADDR), 0,
200 LIS3L02DQ_READ_REG(LIS3L02DQ_REG_OUT_Z_H_ADDR), 0,
204 * lis3l02dq_read_all() Reads all channels currently selected
205 * @st: device specific state
206 * @rx_array: (dma capable) recieve array, must be at least
207 * 4*number of channels
209 static int lis3l02dq_read_all(struct lis3l02dq_state *st, u8 *rx_array)
211 struct spi_transfer *xfers;
212 struct spi_message msg;
213 int ret, i, j = 0;
215 xfers = kzalloc((st->indio_dev->scan_count) * 2
216 * sizeof(*xfers), GFP_KERNEL);
217 if (!xfers)
218 return -ENOMEM;
220 mutex_lock(&st->buf_lock);
222 for (i = 0; i < ARRAY_SIZE(read_all_tx_array)/4; i++) {
223 if (st->indio_dev->scan_mask & (1 << i)) {
224 /* lower byte */
225 xfers[j].tx_buf = st->tx + 2*j;
226 st->tx[2*j] = read_all_tx_array[i*4];
227 st->tx[2*j + 1] = 0;
228 if (rx_array)
229 xfers[j].rx_buf = rx_array + j*2;
230 xfers[j].bits_per_word = 8;
231 xfers[j].len = 2;
232 xfers[j].cs_change = 1;
233 j++;
235 /* upper byte */
236 xfers[j].tx_buf = st->tx + 2*j;
237 st->tx[2*j] = read_all_tx_array[i*4 + 2];
238 st->tx[2*j + 1] = 0;
239 if (rx_array)
240 xfers[j].rx_buf = rx_array + j*2;
241 xfers[j].bits_per_word = 8;
242 xfers[j].len = 2;
243 xfers[j].cs_change = 1;
244 j++;
247 /* After these are transmitted, the rx_buff should have
248 * values in alternate bytes
250 spi_message_init(&msg);
251 for (j = 0; j < st->indio_dev->scan_count * 2; j++)
252 spi_message_add_tail(&xfers[j], &msg);
254 ret = spi_sync(st->us, &msg);
255 mutex_unlock(&st->buf_lock);
256 kfree(xfers);
258 return ret;
262 /* Whilst this makes a lot of calls to iio_sw_ring functions - it is to device
263 * specific to be rolled into the core.
265 static void lis3l02dq_trigger_bh_to_ring(struct work_struct *work_s)
267 struct lis3l02dq_state *st
268 = container_of(work_s, struct lis3l02dq_state,
269 work_trigger_to_ring);
271 u8 *rx_array;
272 int i = 0;
273 u16 *data;
274 size_t datasize = st->indio_dev
275 ->ring->access.get_bpd(st->indio_dev->ring);
277 data = kmalloc(datasize , GFP_KERNEL);
278 if (data == NULL) {
279 dev_err(&st->us->dev, "memory alloc failed in ring bh");
280 return;
282 /* Due to interleaved nature of transmission this buffer must be
283 * twice the number of bytes, or 4 times the number of channels
285 rx_array = kmalloc(4 * (st->indio_dev->scan_count), GFP_KERNEL);
286 if (rx_array == NULL) {
287 dev_err(&st->us->dev, "memory alloc failed in ring bh");
288 kfree(data);
289 return;
292 /* whilst trigger specific, if this read does nto occur the data
293 ready interrupt will not be cleared. Need to add a mechanism
294 to provide a dummy read function if this is not triggering on
295 the data ready function but something else is.
297 st->inter = 0;
299 if (st->indio_dev->scan_count)
300 if (lis3l02dq_read_all(st, rx_array) >= 0)
301 for (; i < st->indio_dev->scan_count; i++)
302 data[i] = combine_8_to_16(rx_array[i*4+1],
303 rx_array[i*4+3]);
304 /* Guaranteed to be aligned with 8 byte boundary */
305 if (st->indio_dev->scan_timestamp)
306 *((s64 *)(data + ((i + 3)/4)*4)) = st->last_timestamp;
308 st->indio_dev->ring->access.store_to(st->indio_dev->ring,
309 (u8 *)data,
310 st->last_timestamp);
312 iio_trigger_notify_done(st->indio_dev->trig);
313 kfree(rx_array);
314 kfree(data);
316 return;
319 /* Caller responsible for locking as necessary. */
320 static int
321 __lis3l02dq_write_data_ready_config(struct device *dev,
322 struct iio_event_handler_list *list,
323 bool state)
325 int ret;
326 u8 valold;
327 bool currentlyset;
328 struct iio_dev *indio_dev = dev_get_drvdata(dev);
330 /* Get the current event mask register */
331 ret = lis3l02dq_spi_read_reg_8(dev,
332 LIS3L02DQ_REG_CTRL_2_ADDR,
333 &valold);
334 if (ret)
335 goto error_ret;
336 /* Find out if data ready is already on */
337 currentlyset
338 = valold & LIS3L02DQ_REG_CTRL_2_ENABLE_DATA_READY_GENERATION;
340 /* Disable requested */
341 if (!state && currentlyset) {
343 valold &= ~LIS3L02DQ_REG_CTRL_2_ENABLE_DATA_READY_GENERATION;
344 /* The double write is to overcome a hardware bug?*/
345 ret = lis3l02dq_spi_write_reg_8(dev,
346 LIS3L02DQ_REG_CTRL_2_ADDR,
347 &valold);
348 if (ret)
349 goto error_ret;
350 ret = lis3l02dq_spi_write_reg_8(dev,
351 LIS3L02DQ_REG_CTRL_2_ADDR,
352 &valold);
353 if (ret)
354 goto error_ret;
356 iio_remove_event_from_list(list,
357 &indio_dev->interrupts[0]
358 ->ev_list);
360 /* Enable requested */
361 } else if (state && !currentlyset) {
362 /* if not set, enable requested */
363 valold |= LIS3L02DQ_REG_CTRL_2_ENABLE_DATA_READY_GENERATION;
364 iio_add_event_to_list(list, &indio_dev->interrupts[0]->ev_list);
365 ret = lis3l02dq_spi_write_reg_8(dev,
366 LIS3L02DQ_REG_CTRL_2_ADDR,
367 &valold);
368 if (ret)
369 goto error_ret;
372 return 0;
373 error_ret:
374 return ret;
378 * lis3l02dq_data_rdy_trigger_set_state() set datardy interrupt state
380 * If disabling the interrupt also does a final read to ensure it is clear.
381 * This is only important in some cases where the scan enable elements are
382 * switched before the ring is reenabled.
384 static int lis3l02dq_data_rdy_trigger_set_state(struct iio_trigger *trig,
385 bool state)
387 struct lis3l02dq_state *st = trig->private_data;
388 int ret = 0;
389 u8 t;
390 __lis3l02dq_write_data_ready_config(&st->indio_dev->dev,
391 &iio_event_data_rdy_trig,
392 state);
393 if (state == false) {
394 /* possible quirk with handler currently worked around
395 by ensuring the work queue is empty */
396 flush_scheduled_work();
397 /* Clear any outstanding ready events */
398 ret = lis3l02dq_read_all(st, NULL);
400 lis3l02dq_spi_read_reg_8(&st->indio_dev->dev,
401 LIS3L02DQ_REG_WAKE_UP_SRC_ADDR,
402 &t);
403 return ret;
405 static DEVICE_ATTR(name, S_IRUGO, iio_trigger_read_name, NULL);
407 static struct attribute *lis3l02dq_trigger_attrs[] = {
408 &dev_attr_name.attr,
409 NULL,
412 static const struct attribute_group lis3l02dq_trigger_attr_group = {
413 .attrs = lis3l02dq_trigger_attrs,
417 * lis3l02dq_trig_try_reen() try renabling irq for data rdy trigger
418 * @trig: the datardy trigger
420 * As the trigger may occur on any data element being updated it is
421 * really rather likely to occur during the read from the previous
422 * trigger event. The only way to discover if this has occured on
423 * boards not supporting level interrupts is to take a look at the line.
424 * If it is indicating another interrupt and we don't seem to have a
425 * handler looking at it, then we need to notify the core that we need
426 * to tell the triggering core to try reading all these again.
428 static int lis3l02dq_trig_try_reen(struct iio_trigger *trig)
430 struct lis3l02dq_state *st = trig->private_data;
431 enable_irq(st->us->irq);
432 /* If gpio still high (or high again) */
433 if (gpio_get_value(irq_to_gpio(st->us->irq)))
434 if (st->inter == 0) {
435 /* already interrupt handler dealing with it */
436 disable_irq_nosync(st->us->irq);
437 if (st->inter == 1) {
438 /* interrupt handler snuck in between test
439 * and disable */
440 enable_irq(st->us->irq);
441 return 0;
443 return -EAGAIN;
445 /* irq reenabled so success! */
446 return 0;
449 int lis3l02dq_probe_trigger(struct iio_dev *indio_dev)
451 int ret;
452 struct lis3l02dq_state *state = indio_dev->dev_data;
454 state->trig = iio_allocate_trigger();
455 if (!state->trig)
456 return -ENOMEM;
458 state->trig->name = kasprintf(GFP_KERNEL,
459 "lis3l02dq-dev%d",
460 indio_dev->id);
461 if (!state->trig->name) {
462 ret = -ENOMEM;
463 goto error_free_trig;
466 state->trig->dev.parent = &state->us->dev;
467 state->trig->owner = THIS_MODULE;
468 state->trig->private_data = state;
469 state->trig->set_trigger_state = &lis3l02dq_data_rdy_trigger_set_state;
470 state->trig->try_reenable = &lis3l02dq_trig_try_reen;
471 state->trig->control_attrs = &lis3l02dq_trigger_attr_group;
472 ret = iio_trigger_register(state->trig);
473 if (ret)
474 goto error_free_trig_name;
476 return 0;
478 error_free_trig_name:
479 kfree(state->trig->name);
480 error_free_trig:
481 iio_free_trigger(state->trig);
483 return ret;
486 void lis3l02dq_remove_trigger(struct iio_dev *indio_dev)
488 struct lis3l02dq_state *state = indio_dev->dev_data;
490 iio_trigger_unregister(state->trig);
491 kfree(state->trig->name);
492 iio_free_trigger(state->trig);
495 void lis3l02dq_unconfigure_ring(struct iio_dev *indio_dev)
497 kfree(indio_dev->pollfunc);
498 iio_sw_rb_free(indio_dev->ring);
501 int lis3l02dq_configure_ring(struct iio_dev *indio_dev)
503 int ret = 0;
504 struct lis3l02dq_state *st = indio_dev->dev_data;
505 struct iio_ring_buffer *ring;
506 INIT_WORK(&st->work_trigger_to_ring, lis3l02dq_trigger_bh_to_ring);
507 /* Set default scan mode */
509 iio_scan_mask_set(indio_dev, iio_scan_el_accel_x.number);
510 iio_scan_mask_set(indio_dev, iio_scan_el_accel_y.number);
511 iio_scan_mask_set(indio_dev, iio_scan_el_accel_z.number);
512 indio_dev->scan_timestamp = true;
514 indio_dev->scan_el_attrs = &lis3l02dq_scan_el_group;
516 ring = iio_sw_rb_allocate(indio_dev);
517 if (!ring) {
518 ret = -ENOMEM;
519 return ret;
521 indio_dev->ring = ring;
522 /* Effectively select the ring buffer implementation */
523 iio_ring_sw_register_funcs(&ring->access);
524 ring->bpe = 2;
525 ring->preenable = &iio_sw_ring_preenable;
526 ring->postenable = &iio_triggered_ring_postenable;
527 ring->predisable = &iio_triggered_ring_predisable;
528 ring->owner = THIS_MODULE;
530 ret = iio_alloc_pollfunc(indio_dev, NULL, &lis3l02dq_poll_func_th);
531 if (ret)
532 goto error_iio_sw_rb_free;;
533 indio_dev->modes |= INDIO_RING_TRIGGERED;
534 return 0;
536 error_iio_sw_rb_free:
537 iio_sw_rb_free(indio_dev->ring);
538 return ret;