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, 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
,
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
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
,
128 struct lis3l02dq_state
*st
= iio_dev_get_devdata(dev_info
);
129 struct iio_trigger
*trig
= st
->trig
;
131 iio_trigger_poll(trig
, timestamp
);
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
,
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
);
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
)
160 if (!dev_info
->scan_el_attrs
->attrs
[i
]) {
164 /* If this element is in the scan mask */
165 ret
= iio_scan_mask_query(dev_info
, el
->number
);
169 data
= kmalloc(dev_info
->ring
->access
.get_bpd(dev_info
->ring
),
173 ret
= dev_info
->ring
->access
.read_last(dev_info
->ring
,
176 goto error_free_data
;
181 len
= iio_scan_mask_count_to_right(dev_info
, el
->number
);
184 goto error_free_data
;
186 len
= sprintf(buf
, "ring %d\n", data
[len
]);
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
;
215 xfers
= kzalloc((st
->indio_dev
->scan_count
) * 2
216 * sizeof(*xfers
), GFP_KERNEL
);
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
)) {
225 xfers
[j
].tx_buf
= st
->tx
+ 2*j
;
226 st
->tx
[2*j
] = read_all_tx_array
[i
*4];
229 xfers
[j
].rx_buf
= rx_array
+ j
*2;
230 xfers
[j
].bits_per_word
= 8;
232 xfers
[j
].cs_change
= 1;
236 xfers
[j
].tx_buf
= st
->tx
+ 2*j
;
237 st
->tx
[2*j
] = read_all_tx_array
[i
*4 + 2];
240 xfers
[j
].rx_buf
= rx_array
+ j
*2;
241 xfers
[j
].bits_per_word
= 8;
243 xfers
[j
].cs_change
= 1;
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
);
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
);
274 size_t datasize
= st
->indio_dev
275 ->ring
->access
.get_bpd(st
->indio_dev
->ring
);
277 data
= kmalloc(datasize
, GFP_KERNEL
);
279 dev_err(&st
->us
->dev
, "memory alloc failed in ring bh");
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");
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.
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],
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
,
312 iio_trigger_notify_done(st
->indio_dev
->trig
);
319 /* Caller responsible for locking as necessary. */
321 __lis3l02dq_write_data_ready_config(struct device
*dev
,
322 struct iio_event_handler_list
*list
,
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
,
336 /* Find out if data ready is already on */
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
,
350 ret
= lis3l02dq_spi_write_reg_8(dev
,
351 LIS3L02DQ_REG_CTRL_2_ADDR
,
356 iio_remove_event_from_list(list
,
357 &indio_dev
->interrupts
[0]
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
,
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
,
387 struct lis3l02dq_state
*st
= trig
->private_data
;
390 __lis3l02dq_write_data_ready_config(&st
->indio_dev
->dev
,
391 &iio_event_data_rdy_trig
,
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
,
405 static DEVICE_ATTR(name
, S_IRUGO
, iio_trigger_read_name
, NULL
);
407 static struct attribute
*lis3l02dq_trigger_attrs
[] = {
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
440 enable_irq(st
->us
->irq
);
445 /* irq reenabled so success! */
449 int lis3l02dq_probe_trigger(struct iio_dev
*indio_dev
)
452 struct lis3l02dq_state
*state
= indio_dev
->dev_data
;
454 state
->trig
= iio_allocate_trigger();
458 state
->trig
->name
= kasprintf(GFP_KERNEL
,
461 if (!state
->trig
->name
) {
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
);
474 goto error_free_trig_name
;
478 error_free_trig_name
:
479 kfree(state
->trig
->name
);
481 iio_free_trigger(state
->trig
);
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
)
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
);
521 indio_dev
->ring
= ring
;
522 /* Effectively select the ring buffer implementation */
523 iio_ring_sw_register_funcs(&ring
->access
);
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
);
532 goto error_iio_sw_rb_free
;;
533 indio_dev
->modes
|= INDIO_RING_TRIGGERED
;
536 error_iio_sw_rb_free
:
537 iio_sw_rb_free(indio_dev
->ring
);