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