1 #include <linux/interrupt.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>
15 #include "../ring_sw.h"
17 #include "../trigger.h"
18 #include "lis3l02dq.h"
21 * combine_8_to_16() utility function to munge to u8s into u16
23 static inline u16 combine_8_to_16(u8 lower, u8 upper)
27 return _lower | (_upper << 8);
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.
38 static int lis3l02dq_scan_el_set_state(struct iio_scan_el *scan_el,
39 struct iio_dev *indio_dev,
45 ret = lis3l02dq_spi_read_reg_8(&indio_dev->dev,
46 LIS3L02DQ_REG_CTRL_1_ADDR,
50 switch (scan_el->label) {
51 case LIS3L02DQ_REG_OUT_X_L_ADDR:
52 mask = LIS3L02DQ_REG_CTRL_1_AXES_X_ENABLE;
54 case LIS3L02DQ_REG_OUT_Y_L_ADDR:
55 mask = LIS3L02DQ_REG_CTRL_1_AXES_Y_ENABLE;
57 case LIS3L02DQ_REG_OUT_Z_L_ADDR:
58 mask = LIS3L02DQ_REG_CTRL_1_AXES_Z_ENABLE;
65 if (!(mask & t) == state) {
70 ret = lis3l02dq_spi_write_reg_8(&indio_dev->dev,
71 LIS3L02DQ_REG_CTRL_1_ADDR,
78 static IIO_SCAN_EL_C(accel_x, 0,
79 LIS3L02DQ_REG_OUT_X_L_ADDR,
80 &lis3l02dq_scan_el_set_state);
81 static IIO_SCAN_EL_C(accel_y, 1,
82 LIS3L02DQ_REG_OUT_Y_L_ADDR,
83 &lis3l02dq_scan_el_set_state);
84 static IIO_SCAN_EL_C(accel_z, 2,
85 LIS3L02DQ_REG_OUT_Z_L_ADDR,
86 &lis3l02dq_scan_el_set_state);
87 static IIO_CONST_ATTR_SCAN_EL_TYPE(accel, s, 12, 16);
88 static IIO_SCAN_EL_TIMESTAMP(3);
89 static IIO_CONST_ATTR_SCAN_EL_TYPE(timestamp, s, 64, 64);
91 static struct attribute *lis3l02dq_scan_el_attrs[] = {
92 &iio_scan_el_accel_x.dev_attr.attr,
93 &iio_const_attr_accel_x_index.dev_attr.attr,
94 &iio_scan_el_accel_y.dev_attr.attr,
95 &iio_const_attr_accel_y_index.dev_attr.attr,
96 &iio_scan_el_accel_z.dev_attr.attr,
97 &iio_const_attr_accel_z_index.dev_attr.attr,
98 &iio_const_attr_accel_type.dev_attr.attr,
99 &iio_scan_el_timestamp.dev_attr.attr,
100 &iio_const_attr_timestamp_index.dev_attr.attr,
101 &iio_const_attr_timestamp_type.dev_attr.attr,
105 static struct attribute_group lis3l02dq_scan_el_group = {
106 .attrs = lis3l02dq_scan_el_attrs,
107 .name = "scan_elements",
111 * lis3l02dq_poll_func_th() top half interrupt handler called by trigger
112 * @private_data: iio_dev
114 static void lis3l02dq_poll_func_th(struct iio_dev *indio_dev, s64 time)
116 struct iio_sw_ring_helper_state *h
117 = iio_dev_get_devdata(indio_dev);
118 struct lis3l02dq_state *st = lis3l02dq_h_to_s(h);
119 /* in this case we need to slightly extend the helper function */
120 iio_sw_poll_func_th(indio_dev, time);
122 /* Indicate that this interrupt is being handled */
123 /* Technically this is trigger related, but without this
124 * handler running there is currently now way for the interrupt
131 * lis3l02dq_data_rdy_trig_poll() the event handler for the data rdy trig
133 static int lis3l02dq_data_rdy_trig_poll(struct iio_dev *indio_dev,
138 struct iio_sw_ring_helper_state *h
139 = iio_dev_get_devdata(indio_dev);
140 struct lis3l02dq_state *st = lis3l02dq_h_to_s(h);
142 iio_trigger_poll(st->trig, timestamp);
147 /* This is an event as it is a response to a physical interrupt */
148 IIO_EVENT_SH(data_rdy_trig, &lis3l02dq_data_rdy_trig_poll);
151 * lis3l02dq_read_accel_from_ring() individual acceleration read from ring
153 ssize_t lis3l02dq_read_accel_from_ring(struct device *dev,
154 struct device_attribute *attr,
157 struct iio_scan_el *el = NULL;
158 int ret, len = 0, i = 0;
159 struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
160 struct iio_dev *dev_info = dev_get_drvdata(dev);
161 struct iio_ring_buffer *ring = dev_info->ring;
162 struct attribute_group *scan_el_attrs = ring->scan_el_attrs;
165 while (scan_el_attrs->attrs[i]) {
166 el = to_iio_scan_el((struct device_attribute *)
167 (scan_el_attrs->attrs[i]));
168 /* label is in fact the address */
169 if (el->label == this_attr->address)
173 if (!scan_el_attrs->attrs[i]) {
177 /* If this element is in the scan mask */
178 ret = iio_scan_mask_query(ring, el->number);
182 data = kmalloc(ring->access.get_bytes_per_datum(ring),
186 ret = ring->access.read_last(ring,
189 goto error_free_data;
194 len = iio_scan_mask_count_to_right(ring, el->number);
197 goto error_free_data;
199 len = sprintf(buf, "ring %d\n", data[len]);
203 return ret ? ret : len;
207 static const u8 read_all_tx_array[] = {
208 LIS3L02DQ_READ_REG(LIS3L02DQ_REG_OUT_X_L_ADDR), 0,
209 LIS3L02DQ_READ_REG(LIS3L02DQ_REG_OUT_X_H_ADDR), 0,
210 LIS3L02DQ_READ_REG(LIS3L02DQ_REG_OUT_Y_L_ADDR), 0,
211 LIS3L02DQ_READ_REG(LIS3L02DQ_REG_OUT_Y_H_ADDR), 0,
212 LIS3L02DQ_READ_REG(LIS3L02DQ_REG_OUT_Z_L_ADDR), 0,
213 LIS3L02DQ_READ_REG(LIS3L02DQ_REG_OUT_Z_H_ADDR), 0,
217 * lis3l02dq_read_all() Reads all channels currently selected
218 * @st: device specific state
219 * @rx_array: (dma capable) recieve array, must be at least
220 * 4*number of channels
222 static int lis3l02dq_read_all(struct lis3l02dq_state *st, u8 *rx_array)
224 struct iio_ring_buffer *ring = st->help.indio_dev->ring;
225 struct spi_transfer *xfers;
226 struct spi_message msg;
229 xfers = kzalloc((ring->scan_count) * 2
230 * sizeof(*xfers), GFP_KERNEL);
234 mutex_lock(&st->buf_lock);
236 for (i = 0; i < ARRAY_SIZE(read_all_tx_array)/4; i++) {
237 if (ring->scan_mask & (1 << i)) {
239 xfers[j].tx_buf = st->tx + 2*j;
240 st->tx[2*j] = read_all_tx_array[i*4];
243 xfers[j].rx_buf = rx_array + j*2;
244 xfers[j].bits_per_word = 8;
246 xfers[j].cs_change = 1;
250 xfers[j].tx_buf = st->tx + 2*j;
251 st->tx[2*j] = read_all_tx_array[i*4 + 2];
254 xfers[j].rx_buf = rx_array + j*2;
255 xfers[j].bits_per_word = 8;
257 xfers[j].cs_change = 1;
261 /* After these are transmitted, the rx_buff should have
262 * values in alternate bytes
264 spi_message_init(&msg);
265 for (j = 0; j < ring->scan_count * 2; j++)
266 spi_message_add_tail(&xfers[j], &msg);
268 ret = spi_sync(st->us, &msg);
269 mutex_unlock(&st->buf_lock);
275 static void lis3l02dq_trigger_bh_to_ring(struct work_struct *work_s)
277 struct iio_sw_ring_helper_state *h
278 = container_of(work_s, struct iio_sw_ring_helper_state,
279 work_trigger_to_ring);
280 struct lis3l02dq_state *st = lis3l02dq_h_to_s(h);
283 iio_sw_trigger_bh_to_ring(work_s);
286 static int lis3l02dq_get_ring_element(struct iio_sw_ring_helper_state *h,
291 s16 *data = (s16 *)buf;
293 rx_array = kzalloc(4 * (h->indio_dev->ring->scan_count), GFP_KERNEL);
294 if (rx_array == NULL)
296 ret = lis3l02dq_read_all(lis3l02dq_h_to_s(h), rx_array);
299 for (i = 0; i < h->indio_dev->ring->scan_count; i++)
300 data[i] = combine_8_to_16(rx_array[i*4+1],
304 return i*sizeof(data[0]);
307 /* Caller responsible for locking as necessary. */
309 __lis3l02dq_write_data_ready_config(struct device *dev,
310 struct iio_event_handler_list *list,
316 struct iio_dev *indio_dev = dev_get_drvdata(dev);
318 /* Get the current event mask register */
319 ret = lis3l02dq_spi_read_reg_8(dev,
320 LIS3L02DQ_REG_CTRL_2_ADDR,
324 /* Find out if data ready is already on */
326 = valold & LIS3L02DQ_REG_CTRL_2_ENABLE_DATA_READY_GENERATION;
328 /* Disable requested */
329 if (!state && currentlyset) {
331 valold &= ~LIS3L02DQ_REG_CTRL_2_ENABLE_DATA_READY_GENERATION;
332 /* The double write is to overcome a hardware bug?*/
333 ret = lis3l02dq_spi_write_reg_8(dev,
334 LIS3L02DQ_REG_CTRL_2_ADDR,
338 ret = lis3l02dq_spi_write_reg_8(dev,
339 LIS3L02DQ_REG_CTRL_2_ADDR,
344 iio_remove_event_from_list(list,
345 &indio_dev->interrupts[0]
348 /* Enable requested */
349 } else if (state && !currentlyset) {
350 /* if not set, enable requested */
351 valold |= LIS3L02DQ_REG_CTRL_2_ENABLE_DATA_READY_GENERATION;
352 iio_add_event_to_list(list, &indio_dev->interrupts[0]->ev_list);
353 ret = lis3l02dq_spi_write_reg_8(dev,
354 LIS3L02DQ_REG_CTRL_2_ADDR,
366 * lis3l02dq_data_rdy_trigger_set_state() set datardy interrupt state
368 * If disabling the interrupt also does a final read to ensure it is clear.
369 * This is only important in some cases where the scan enable elements are
370 * switched before the ring is reenabled.
372 static int lis3l02dq_data_rdy_trigger_set_state(struct iio_trigger *trig,
375 struct lis3l02dq_state *st = trig->private_data;
378 __lis3l02dq_write_data_ready_config(&st->help.indio_dev->dev,
379 &iio_event_data_rdy_trig,
381 if (state == false) {
382 /* possible quirk with handler currently worked around
383 by ensuring the work queue is empty */
384 flush_scheduled_work();
385 /* Clear any outstanding ready events */
386 ret = lis3l02dq_read_all(st, NULL);
388 lis3l02dq_spi_read_reg_8(&st->help.indio_dev->dev,
389 LIS3L02DQ_REG_WAKE_UP_SRC_ADDR,
394 static IIO_TRIGGER_NAME_ATTR;
396 static struct attribute *lis3l02dq_trigger_attrs[] = {
401 static const struct attribute_group lis3l02dq_trigger_attr_group = {
402 .attrs = lis3l02dq_trigger_attrs,
406 * lis3l02dq_trig_try_reen() try renabling irq for data rdy trigger
407 * @trig: the datardy trigger
409 * As the trigger may occur on any data element being updated it is
410 * really rather likely to occur during the read from the previous
411 * trigger event. The only way to discover if this has occured on
412 * boards not supporting level interrupts is to take a look at the line.
413 * If it is indicating another interrupt and we don't seem to have a
414 * handler looking at it, then we need to notify the core that we need
415 * to tell the triggering core to try reading all these again.
417 static int lis3l02dq_trig_try_reen(struct iio_trigger *trig)
419 struct lis3l02dq_state *st = trig->private_data;
420 enable_irq(st->us->irq);
421 /* If gpio still high (or high again) */
422 if (gpio_get_value(irq_to_gpio(st->us->irq)))
423 if (st->inter == 0) {
424 /* already interrupt handler dealing with it */
425 disable_irq_nosync(st->us->irq);
426 if (st->inter == 1) {
427 /* interrupt handler snuck in between test
429 enable_irq(st->us->irq);
434 /* irq reenabled so success! */
438 int lis3l02dq_probe_trigger(struct iio_dev *indio_dev)
441 struct lis3l02dq_state *state = indio_dev->dev_data;
443 state->trig = iio_allocate_trigger();
447 state->trig->name = kasprintf(GFP_KERNEL,
450 if (!state->trig->name) {
452 goto error_free_trig;
455 state->trig->dev.parent = &state->us->dev;
456 state->trig->owner = THIS_MODULE;
457 state->trig->private_data = state;
458 state->trig->set_trigger_state = &lis3l02dq_data_rdy_trigger_set_state;
459 state->trig->try_reenable = &lis3l02dq_trig_try_reen;
460 state->trig->control_attrs = &lis3l02dq_trigger_attr_group;
461 ret = iio_trigger_register(state->trig);
463 goto error_free_trig_name;
467 error_free_trig_name:
468 kfree(state->trig->name);
470 iio_free_trigger(state->trig);
475 void lis3l02dq_remove_trigger(struct iio_dev *indio_dev)
477 struct lis3l02dq_state *state = indio_dev->dev_data;
479 iio_trigger_unregister(state->trig);
480 kfree(state->trig->name);
481 iio_free_trigger(state->trig);
484 void lis3l02dq_unconfigure_ring(struct iio_dev *indio_dev)
486 kfree(indio_dev->pollfunc);
487 iio_sw_rb_free(indio_dev->ring);
490 int lis3l02dq_configure_ring(struct iio_dev *indio_dev)
493 struct iio_sw_ring_helper_state *h = iio_dev_get_devdata(indio_dev);
494 struct iio_ring_buffer *ring;
495 INIT_WORK(&h->work_trigger_to_ring, lis3l02dq_trigger_bh_to_ring);
496 h->get_ring_element = &lis3l02dq_get_ring_element;
498 ring = iio_sw_rb_allocate(indio_dev);
502 indio_dev->ring = ring;
503 /* Effectively select the ring buffer implementation */
504 iio_ring_sw_register_funcs(&ring->access);
506 ring->scan_el_attrs = &lis3l02dq_scan_el_group;
507 ring->scan_timestamp = true;
508 ring->preenable = &iio_sw_ring_preenable;
509 ring->postenable = &iio_triggered_ring_postenable;
510 ring->predisable = &iio_triggered_ring_predisable;
511 ring->owner = THIS_MODULE;
513 /* Set default scan mode */
514 iio_scan_mask_set(ring, iio_scan_el_accel_x.number);
515 iio_scan_mask_set(ring, iio_scan_el_accel_y.number);
516 iio_scan_mask_set(ring, iio_scan_el_accel_z.number);
518 ret = iio_alloc_pollfunc(indio_dev, NULL, &lis3l02dq_poll_func_th);
520 goto error_iio_sw_rb_free;;
521 indio_dev->modes |= INDIO_RING_TRIGGERED;
524 error_iio_sw_rb_free:
525 iio_sw_rb_free(indio_dev->ring);