^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) =================
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) Triggered Buffers
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) =================
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) Now that we know what buffers and triggers are let's see how they work together.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) IIO triggered buffer setup
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) ==========================
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) * :c:func:`iio_triggered_buffer_setup` — Setup triggered buffer and pollfunc
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) * :c:func:`iio_triggered_buffer_cleanup` — Free resources allocated by
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) :c:func:`iio_triggered_buffer_setup`
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) * struct iio_buffer_setup_ops — buffer setup related callbacks
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) A typical triggered buffer setup looks like this::
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) const struct iio_buffer_setup_ops sensor_buffer_setup_ops = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) .preenable = sensor_buffer_preenable,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) .postenable = sensor_buffer_postenable,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) .postdisable = sensor_buffer_postdisable,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) .predisable = sensor_buffer_predisable,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) irqreturn_t sensor_iio_pollfunc(int irq, void *p)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) pf->timestamp = iio_get_time_ns((struct indio_dev *)p);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) return IRQ_WAKE_THREAD;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) irqreturn_t sensor_trigger_handler(int irq, void *p)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) u16 buf[8];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) int i = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) /* read data for each active channel */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) for_each_set_bit(bit, active_scan_mask, masklength)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) buf[i++] = sensor_get_data(bit)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) iio_push_to_buffers_with_timestamp(indio_dev, buf, timestamp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) iio_trigger_notify_done(trigger);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) return IRQ_HANDLED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) /* setup triggered buffer, usually in probe function */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) iio_triggered_buffer_setup(indio_dev, sensor_iio_polfunc,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) sensor_trigger_handler,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) sensor_buffer_setup_ops);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) The important things to notice here are:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) * :c:type:`iio_buffer_setup_ops`, the buffer setup functions to be called at
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) predefined points in the buffer configuration sequence (e.g. before enable,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) after disable). If not specified, the IIO core uses the default
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) iio_triggered_buffer_setup_ops.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) * **sensor_iio_pollfunc**, the function that will be used as top half of poll
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) function. It should do as little processing as possible, because it runs in
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) interrupt context. The most common operation is recording of the current
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) timestamp and for this reason one can use the IIO core defined
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) :c:func:`iio_pollfunc_store_time` function.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) * **sensor_trigger_handler**, the function that will be used as bottom half of
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) the poll function. This runs in the context of a kernel thread and all the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) processing takes place here. It usually reads data from the device and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) stores it in the internal buffer together with the timestamp recorded in the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) top half.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) More details
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) ============
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) .. kernel-doc:: drivers/iio/buffer/industrialio-triggered-buffer.c