xref: /OK3568_Linux_fs/kernel/Documentation/driver-api/iio/buffers.rst (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun=======
2*4882a593SmuzhiyunBuffers
3*4882a593Smuzhiyun=======
4*4882a593Smuzhiyun
5*4882a593Smuzhiyun* struct iio_buffer — general buffer structure
6*4882a593Smuzhiyun* :c:func:`iio_validate_scan_mask_onehot` — Validates that exactly one channel
7*4882a593Smuzhiyun  is selected
8*4882a593Smuzhiyun* :c:func:`iio_buffer_get` — Grab a reference to the buffer
9*4882a593Smuzhiyun* :c:func:`iio_buffer_put` — Release the reference to the buffer
10*4882a593Smuzhiyun
11*4882a593SmuzhiyunThe Industrial I/O core offers a way for continuous data capture based on a
12*4882a593Smuzhiyuntrigger source. Multiple data channels can be read at once from
13*4882a593Smuzhiyun:file:`/dev/iio:device{X}` character device node, thus reducing the CPU load.
14*4882a593Smuzhiyun
15*4882a593SmuzhiyunIIO buffer sysfs interface
16*4882a593Smuzhiyun==========================
17*4882a593SmuzhiyunAn IIO buffer has an associated attributes directory under
18*4882a593Smuzhiyun:file:`/sys/bus/iio/iio:device{X}/buffer/*`. Here are some of the existing
19*4882a593Smuzhiyunattributes:
20*4882a593Smuzhiyun
21*4882a593Smuzhiyun* :file:`length`, the total number of data samples (capacity) that can be
22*4882a593Smuzhiyun  stored by the buffer.
23*4882a593Smuzhiyun* :file:`enable`, activate buffer capture.
24*4882a593Smuzhiyun
25*4882a593SmuzhiyunIIO buffer setup
26*4882a593Smuzhiyun================
27*4882a593Smuzhiyun
28*4882a593SmuzhiyunThe meta information associated with a channel reading placed in a buffer is
29*4882a593Smuzhiyuncalled a scan element. The important bits configuring scan elements are
30*4882a593Smuzhiyunexposed to userspace applications via the
31*4882a593Smuzhiyun:file:`/sys/bus/iio/iio:device{X}/scan_elements/*` directory. This file contains
32*4882a593Smuzhiyunattributes of the following form:
33*4882a593Smuzhiyun
34*4882a593Smuzhiyun* :file:`enable`, used for enabling a channel. If and only if its attribute
35*4882a593Smuzhiyun  is non *zero*, then a triggered capture will contain data samples for this
36*4882a593Smuzhiyun  channel.
37*4882a593Smuzhiyun* :file:`type`, description of the scan element data storage within the buffer
38*4882a593Smuzhiyun  and hence the form in which it is read from user space.
39*4882a593Smuzhiyun  Format is [be|le]:[s|u]bits/storagebitsXrepeat[>>shift] .
40*4882a593Smuzhiyun  * *be* or *le*, specifies big or little endian.
41*4882a593Smuzhiyun  * *s* or *u*, specifies if signed (2's complement) or unsigned.
42*4882a593Smuzhiyun  * *bits*, is the number of valid data bits.
43*4882a593Smuzhiyun  * *storagebits*, is the number of bits (after padding) that it occupies in the
44*4882a593Smuzhiyun  buffer.
45*4882a593Smuzhiyun  * *shift*, if specified, is the shift that needs to be applied prior to
46*4882a593Smuzhiyun  masking out unused bits.
47*4882a593Smuzhiyun  * *repeat*, specifies the number of bits/storagebits repetitions. When the
48*4882a593Smuzhiyun  repeat element is 0 or 1, then the repeat value is omitted.
49*4882a593Smuzhiyun
50*4882a593SmuzhiyunFor example, a driver for a 3-axis accelerometer with 12 bit resolution where
51*4882a593Smuzhiyundata is stored in two 8-bits registers as follows::
52*4882a593Smuzhiyun
53*4882a593Smuzhiyun        7   6   5   4   3   2   1   0
54*4882a593Smuzhiyun      +---+---+---+---+---+---+---+---+
55*4882a593Smuzhiyun      |D3 |D2 |D1 |D0 | X | X | X | X | (LOW byte, address 0x06)
56*4882a593Smuzhiyun      +---+---+---+---+---+---+---+---+
57*4882a593Smuzhiyun
58*4882a593Smuzhiyun        7   6   5   4   3   2   1   0
59*4882a593Smuzhiyun      +---+---+---+---+---+---+---+---+
60*4882a593Smuzhiyun      |D11|D10|D9 |D8 |D7 |D6 |D5 |D4 | (HIGH byte, address 0x07)
61*4882a593Smuzhiyun      +---+---+---+---+---+---+---+---+
62*4882a593Smuzhiyun
63*4882a593Smuzhiyunwill have the following scan element type for each axis::
64*4882a593Smuzhiyun
65*4882a593Smuzhiyun      $ cat /sys/bus/iio/devices/iio:device0/scan_elements/in_accel_y_type
66*4882a593Smuzhiyun      le:s12/16>>4
67*4882a593Smuzhiyun
68*4882a593SmuzhiyunA user space application will interpret data samples read from the buffer as
69*4882a593Smuzhiyuntwo byte little endian signed data, that needs a 4 bits right shift before
70*4882a593Smuzhiyunmasking out the 12 valid bits of data.
71*4882a593Smuzhiyun
72*4882a593SmuzhiyunFor implementing buffer support a driver should initialize the following
73*4882a593Smuzhiyunfields in iio_chan_spec definition::
74*4882a593Smuzhiyun
75*4882a593Smuzhiyun   struct iio_chan_spec {
76*4882a593Smuzhiyun   /* other members */
77*4882a593Smuzhiyun           int scan_index
78*4882a593Smuzhiyun           struct {
79*4882a593Smuzhiyun                   char sign;
80*4882a593Smuzhiyun                   u8 realbits;
81*4882a593Smuzhiyun                   u8 storagebits;
82*4882a593Smuzhiyun                   u8 shift;
83*4882a593Smuzhiyun                   u8 repeat;
84*4882a593Smuzhiyun                   enum iio_endian endianness;
85*4882a593Smuzhiyun                  } scan_type;
86*4882a593Smuzhiyun          };
87*4882a593Smuzhiyun
88*4882a593SmuzhiyunThe driver implementing the accelerometer described above will have the
89*4882a593Smuzhiyunfollowing channel definition::
90*4882a593Smuzhiyun
91*4882a593Smuzhiyun   struct iio_chan_spec accel_channels[] = {
92*4882a593Smuzhiyun           {
93*4882a593Smuzhiyun                   .type = IIO_ACCEL,
94*4882a593Smuzhiyun		   .modified = 1,
95*4882a593Smuzhiyun		   .channel2 = IIO_MOD_X,
96*4882a593Smuzhiyun		   /* other stuff here */
97*4882a593Smuzhiyun		   .scan_index = 0,
98*4882a593Smuzhiyun		   .scan_type = {
99*4882a593Smuzhiyun		           .sign = 's',
100*4882a593Smuzhiyun			   .realbits = 12,
101*4882a593Smuzhiyun			   .storagebits = 16,
102*4882a593Smuzhiyun			   .shift = 4,
103*4882a593Smuzhiyun			   .endianness = IIO_LE,
104*4882a593Smuzhiyun		   },
105*4882a593Smuzhiyun           }
106*4882a593Smuzhiyun           /* similar for Y (with channel2 = IIO_MOD_Y, scan_index = 1)
107*4882a593Smuzhiyun            * and Z (with channel2 = IIO_MOD_Z, scan_index = 2) axis
108*4882a593Smuzhiyun            */
109*4882a593Smuzhiyun    }
110*4882a593Smuzhiyun
111*4882a593SmuzhiyunHere **scan_index** defines the order in which the enabled channels are placed
112*4882a593Smuzhiyuninside the buffer. Channels with a lower **scan_index** will be placed before
113*4882a593Smuzhiyunchannels with a higher index. Each channel needs to have a unique
114*4882a593Smuzhiyun**scan_index**.
115*4882a593Smuzhiyun
116*4882a593SmuzhiyunSetting **scan_index** to -1 can be used to indicate that the specific channel
117*4882a593Smuzhiyundoes not support buffered capture. In this case no entries will be created for
118*4882a593Smuzhiyunthe channel in the scan_elements directory.
119*4882a593Smuzhiyun
120*4882a593SmuzhiyunMore details
121*4882a593Smuzhiyun============
122*4882a593Smuzhiyun.. kernel-doc:: include/linux/iio/buffer.h
123*4882a593Smuzhiyun.. kernel-doc:: drivers/iio/industrialio-buffer.c
124*4882a593Smuzhiyun   :export:
125*4882a593Smuzhiyun
126