162306a36Sopenharmony_ci=======
262306a36Sopenharmony_ciBuffers
362306a36Sopenharmony_ci=======
462306a36Sopenharmony_ci
562306a36Sopenharmony_ci* struct iio_buffer — general buffer structure
662306a36Sopenharmony_ci* :c:func:`iio_validate_scan_mask_onehot` — Validates that exactly one channel
762306a36Sopenharmony_ci  is selected
862306a36Sopenharmony_ci* :c:func:`iio_buffer_get` — Grab a reference to the buffer
962306a36Sopenharmony_ci* :c:func:`iio_buffer_put` — Release the reference to the buffer
1062306a36Sopenharmony_ci
1162306a36Sopenharmony_ciThe Industrial I/O core offers a way for continuous data capture based on a
1262306a36Sopenharmony_citrigger source. Multiple data channels can be read at once from
1362306a36Sopenharmony_ci:file:`/dev/iio:device{X}` character device node, thus reducing the CPU load.
1462306a36Sopenharmony_ci
1562306a36Sopenharmony_ciIIO buffer sysfs interface
1662306a36Sopenharmony_ci==========================
1762306a36Sopenharmony_ciAn IIO buffer has an associated attributes directory under
1862306a36Sopenharmony_ci:file:`/sys/bus/iio/iio:device{X}/buffer/*`. Here are some of the existing
1962306a36Sopenharmony_ciattributes:
2062306a36Sopenharmony_ci
2162306a36Sopenharmony_ci* :file:`length`, the total number of data samples (capacity) that can be
2262306a36Sopenharmony_ci  stored by the buffer.
2362306a36Sopenharmony_ci* :file:`enable`, activate buffer capture.
2462306a36Sopenharmony_ci
2562306a36Sopenharmony_ciIIO buffer setup
2662306a36Sopenharmony_ci================
2762306a36Sopenharmony_ci
2862306a36Sopenharmony_ciThe meta information associated with a channel reading placed in a buffer is
2962306a36Sopenharmony_cicalled a scan element. The important bits configuring scan elements are
3062306a36Sopenharmony_ciexposed to userspace applications via the
3162306a36Sopenharmony_ci:file:`/sys/bus/iio/iio:device{X}/scan_elements/` directory. This directory contains
3262306a36Sopenharmony_ciattributes of the following form:
3362306a36Sopenharmony_ci
3462306a36Sopenharmony_ci* :file:`enable`, used for enabling a channel. If and only if its attribute
3562306a36Sopenharmony_ci  is non *zero*, then a triggered capture will contain data samples for this
3662306a36Sopenharmony_ci  channel.
3762306a36Sopenharmony_ci* :file:`index`, the scan_index of the channel.
3862306a36Sopenharmony_ci* :file:`type`, description of the scan element data storage within the buffer
3962306a36Sopenharmony_ci  and hence the form in which it is read from user space.
4062306a36Sopenharmony_ci  Format is [be|le]:[s|u]bits/storagebits[Xrepeat][>>shift] .
4162306a36Sopenharmony_ci
4262306a36Sopenharmony_ci  * *be* or *le*, specifies big or little endian.
4362306a36Sopenharmony_ci  * *s* or *u*, specifies if signed (2's complement) or unsigned.
4462306a36Sopenharmony_ci  * *bits*, is the number of valid data bits.
4562306a36Sopenharmony_ci  * *storagebits*, is the number of bits (after padding) that it occupies in the
4662306a36Sopenharmony_ci    buffer.
4762306a36Sopenharmony_ci  * *repeat*, specifies the number of bits/storagebits repetitions. When the
4862306a36Sopenharmony_ci    repeat element is 0 or 1, then the repeat value is omitted.
4962306a36Sopenharmony_ci  * *shift*, if specified, is the shift that needs to be applied prior to
5062306a36Sopenharmony_ci    masking out unused bits.
5162306a36Sopenharmony_ci
5262306a36Sopenharmony_ciFor example, a driver for a 3-axis accelerometer with 12 bit resolution where
5362306a36Sopenharmony_cidata is stored in two 8-bits registers as follows::
5462306a36Sopenharmony_ci
5562306a36Sopenharmony_ci        7   6   5   4   3   2   1   0
5662306a36Sopenharmony_ci      +---+---+---+---+---+---+---+---+
5762306a36Sopenharmony_ci      |D3 |D2 |D1 |D0 | X | X | X | X | (LOW byte, address 0x06)
5862306a36Sopenharmony_ci      +---+---+---+---+---+---+---+---+
5962306a36Sopenharmony_ci
6062306a36Sopenharmony_ci        7   6   5   4   3   2   1   0
6162306a36Sopenharmony_ci      +---+---+---+---+---+---+---+---+
6262306a36Sopenharmony_ci      |D11|D10|D9 |D8 |D7 |D6 |D5 |D4 | (HIGH byte, address 0x07)
6362306a36Sopenharmony_ci      +---+---+---+---+---+---+---+---+
6462306a36Sopenharmony_ci
6562306a36Sopenharmony_ciwill have the following scan element type for each axis::
6662306a36Sopenharmony_ci
6762306a36Sopenharmony_ci      $ cat /sys/bus/iio/devices/iio:device0/scan_elements/in_accel_y_type
6862306a36Sopenharmony_ci      le:s12/16>>4
6962306a36Sopenharmony_ci
7062306a36Sopenharmony_ciA user space application will interpret data samples read from the buffer as
7162306a36Sopenharmony_citwo byte little endian signed data, that needs a 4 bits right shift before
7262306a36Sopenharmony_cimasking out the 12 valid bits of data.
7362306a36Sopenharmony_ci
7462306a36Sopenharmony_ciFor implementing buffer support a driver should initialize the following
7562306a36Sopenharmony_cifields in iio_chan_spec definition::
7662306a36Sopenharmony_ci
7762306a36Sopenharmony_ci   struct iio_chan_spec {
7862306a36Sopenharmony_ci   /* other members */
7962306a36Sopenharmony_ci           int scan_index
8062306a36Sopenharmony_ci           struct {
8162306a36Sopenharmony_ci                   char sign;
8262306a36Sopenharmony_ci                   u8 realbits;
8362306a36Sopenharmony_ci                   u8 storagebits;
8462306a36Sopenharmony_ci                   u8 shift;
8562306a36Sopenharmony_ci                   u8 repeat;
8662306a36Sopenharmony_ci                   enum iio_endian endianness;
8762306a36Sopenharmony_ci                  } scan_type;
8862306a36Sopenharmony_ci          };
8962306a36Sopenharmony_ci
9062306a36Sopenharmony_ciThe driver implementing the accelerometer described above will have the
9162306a36Sopenharmony_cifollowing channel definition::
9262306a36Sopenharmony_ci
9362306a36Sopenharmony_ci   struct iio_chan_spec accel_channels[] = {
9462306a36Sopenharmony_ci           {
9562306a36Sopenharmony_ci                   .type = IIO_ACCEL,
9662306a36Sopenharmony_ci		   .modified = 1,
9762306a36Sopenharmony_ci		   .channel2 = IIO_MOD_X,
9862306a36Sopenharmony_ci		   /* other stuff here */
9962306a36Sopenharmony_ci		   .scan_index = 0,
10062306a36Sopenharmony_ci		   .scan_type = {
10162306a36Sopenharmony_ci		           .sign = 's',
10262306a36Sopenharmony_ci			   .realbits = 12,
10362306a36Sopenharmony_ci			   .storagebits = 16,
10462306a36Sopenharmony_ci			   .shift = 4,
10562306a36Sopenharmony_ci			   .endianness = IIO_LE,
10662306a36Sopenharmony_ci		   },
10762306a36Sopenharmony_ci           }
10862306a36Sopenharmony_ci           /* similar for Y (with channel2 = IIO_MOD_Y, scan_index = 1)
10962306a36Sopenharmony_ci            * and Z (with channel2 = IIO_MOD_Z, scan_index = 2) axis
11062306a36Sopenharmony_ci            */
11162306a36Sopenharmony_ci    }
11262306a36Sopenharmony_ci
11362306a36Sopenharmony_ciHere **scan_index** defines the order in which the enabled channels are placed
11462306a36Sopenharmony_ciinside the buffer. Channels with a lower **scan_index** will be placed before
11562306a36Sopenharmony_cichannels with a higher index. Each channel needs to have a unique
11662306a36Sopenharmony_ci**scan_index**.
11762306a36Sopenharmony_ci
11862306a36Sopenharmony_ciSetting **scan_index** to -1 can be used to indicate that the specific channel
11962306a36Sopenharmony_cidoes not support buffered capture. In this case no entries will be created for
12062306a36Sopenharmony_cithe channel in the scan_elements directory.
12162306a36Sopenharmony_ci
12262306a36Sopenharmony_ciMore details
12362306a36Sopenharmony_ci============
12462306a36Sopenharmony_ci.. kernel-doc:: include/linux/iio/buffer.h
12562306a36Sopenharmony_ci.. kernel-doc:: drivers/iio/industrialio-buffer.c
12662306a36Sopenharmony_ci   :export:
127