162306a36Sopenharmony_ci=================
262306a36Sopenharmony_ciTriggered Buffers
362306a36Sopenharmony_ci=================
462306a36Sopenharmony_ci
562306a36Sopenharmony_ciNow that we know what buffers and triggers are let's see how they work together.
662306a36Sopenharmony_ci
762306a36Sopenharmony_ciIIO triggered buffer setup
862306a36Sopenharmony_ci==========================
962306a36Sopenharmony_ci
1062306a36Sopenharmony_ci* :c:func:`iio_triggered_buffer_setup` — Setup triggered buffer and pollfunc
1162306a36Sopenharmony_ci* :c:func:`iio_triggered_buffer_cleanup` — Free resources allocated by
1262306a36Sopenharmony_ci  :c:func:`iio_triggered_buffer_setup`
1362306a36Sopenharmony_ci* struct iio_buffer_setup_ops — buffer setup related callbacks
1462306a36Sopenharmony_ci
1562306a36Sopenharmony_ciA typical triggered buffer setup looks like this::
1662306a36Sopenharmony_ci
1762306a36Sopenharmony_ci    const struct iio_buffer_setup_ops sensor_buffer_setup_ops = {
1862306a36Sopenharmony_ci      .preenable    = sensor_buffer_preenable,
1962306a36Sopenharmony_ci      .postenable   = sensor_buffer_postenable,
2062306a36Sopenharmony_ci      .postdisable  = sensor_buffer_postdisable,
2162306a36Sopenharmony_ci      .predisable   = sensor_buffer_predisable,
2262306a36Sopenharmony_ci    };
2362306a36Sopenharmony_ci
2462306a36Sopenharmony_ci    irqreturn_t sensor_iio_pollfunc(int irq, void *p)
2562306a36Sopenharmony_ci    {
2662306a36Sopenharmony_ci        pf->timestamp = iio_get_time_ns((struct indio_dev *)p);
2762306a36Sopenharmony_ci        return IRQ_WAKE_THREAD;
2862306a36Sopenharmony_ci    }
2962306a36Sopenharmony_ci
3062306a36Sopenharmony_ci    irqreturn_t sensor_trigger_handler(int irq, void *p)
3162306a36Sopenharmony_ci    {
3262306a36Sopenharmony_ci        u16 buf[8];
3362306a36Sopenharmony_ci        int i = 0;
3462306a36Sopenharmony_ci
3562306a36Sopenharmony_ci        /* read data for each active channel */
3662306a36Sopenharmony_ci        for_each_set_bit(bit, active_scan_mask, masklength)
3762306a36Sopenharmony_ci            buf[i++] = sensor_get_data(bit)
3862306a36Sopenharmony_ci
3962306a36Sopenharmony_ci        iio_push_to_buffers_with_timestamp(indio_dev, buf, timestamp);
4062306a36Sopenharmony_ci
4162306a36Sopenharmony_ci        iio_trigger_notify_done(trigger);
4262306a36Sopenharmony_ci        return IRQ_HANDLED;
4362306a36Sopenharmony_ci    }
4462306a36Sopenharmony_ci
4562306a36Sopenharmony_ci    /* setup triggered buffer, usually in probe function */
4662306a36Sopenharmony_ci    iio_triggered_buffer_setup(indio_dev, sensor_iio_polfunc,
4762306a36Sopenharmony_ci                               sensor_trigger_handler,
4862306a36Sopenharmony_ci                               sensor_buffer_setup_ops);
4962306a36Sopenharmony_ci
5062306a36Sopenharmony_ciThe important things to notice here are:
5162306a36Sopenharmony_ci
5262306a36Sopenharmony_ci* :c:type:`iio_buffer_setup_ops`, the buffer setup functions to be called at
5362306a36Sopenharmony_ci  predefined points in the buffer configuration sequence (e.g. before enable,
5462306a36Sopenharmony_ci  after disable). If not specified, the IIO core uses the default
5562306a36Sopenharmony_ci  iio_triggered_buffer_setup_ops.
5662306a36Sopenharmony_ci* **sensor_iio_pollfunc**, the function that will be used as top half of poll
5762306a36Sopenharmony_ci  function. It should do as little processing as possible, because it runs in
5862306a36Sopenharmony_ci  interrupt context. The most common operation is recording of the current
5962306a36Sopenharmony_ci  timestamp and for this reason one can use the IIO core defined
6062306a36Sopenharmony_ci  :c:func:`iio_pollfunc_store_time` function.
6162306a36Sopenharmony_ci* **sensor_trigger_handler**, the function that will be used as bottom half of
6262306a36Sopenharmony_ci  the poll function. This runs in the context of a kernel thread and all the
6362306a36Sopenharmony_ci  processing takes place here. It usually reads data from the device and
6462306a36Sopenharmony_ci  stores it in the internal buffer together with the timestamp recorded in the
6562306a36Sopenharmony_ci  top half.
6662306a36Sopenharmony_ci
6762306a36Sopenharmony_ciMore details
6862306a36Sopenharmony_ci============
6962306a36Sopenharmony_ci.. kernel-doc:: drivers/iio/buffer/industrialio-triggered-buffer.c
70