xref: /openbmc/linux/Documentation/driver-api/iio/core.rst (revision cbecf716ca618fd44feda6bd9a64a8179d031fc5)
149b2fd6eSJonathan Cameron=============
249b2fd6eSJonathan CameronCore elements
349b2fd6eSJonathan Cameron=============
449b2fd6eSJonathan Cameron
58c56eebcSTomasz DuszynskiThe Industrial I/O core offers both a unified framework for writing drivers for
68c56eebcSTomasz Duszynskimany different types of embedded sensors and a standard interface to user space
749b2fd6eSJonathan Cameronapplications manipulating sensors. The implementation can be found under
849b2fd6eSJonathan Cameron:file:`drivers/iio/industrialio-*`
949b2fd6eSJonathan Cameron
1049b2fd6eSJonathan CameronIndustrial I/O Devices
1149b2fd6eSJonathan Cameron----------------------
1249b2fd6eSJonathan Cameron
13*9303c9d5SMauro Carvalho Chehab* struct iio_dev - industrial I/O device
14a320274aSPuranjay Mohan* iio_device_alloc() - allocate an :c:type:`iio_dev` from a driver
15a320274aSPuranjay Mohan* iio_device_free() - free an :c:type:`iio_dev` from a driver
16a320274aSPuranjay Mohan* iio_device_register() - register a device with the IIO subsystem
17a320274aSPuranjay Mohan* iio_device_unregister() - unregister a device from the IIO
1849b2fd6eSJonathan Cameron  subsystem
1949b2fd6eSJonathan Cameron
2049b2fd6eSJonathan CameronAn IIO device usually corresponds to a single hardware sensor and it
2149b2fd6eSJonathan Cameronprovides all the information needed by a driver handling a device.
2249b2fd6eSJonathan CameronLet's first have a look at the functionality embedded in an IIO device
2349b2fd6eSJonathan Cameronthen we will show how a device driver makes use of an IIO device.
2449b2fd6eSJonathan Cameron
2549b2fd6eSJonathan CameronThere are two ways for a user space application to interact with an IIO driver.
2649b2fd6eSJonathan Cameron
2749b2fd6eSJonathan Cameron1. :file:`/sys/bus/iio/iio:device{X}/`, this represents a hardware sensor
2849b2fd6eSJonathan Cameron   and groups together the data channels of the same chip.
2949b2fd6eSJonathan Cameron2. :file:`/dev/iio:device{X}`, character device node interface used for
3049b2fd6eSJonathan Cameron   buffered data transfer and for events information retrieval.
3149b2fd6eSJonathan Cameron
3249b2fd6eSJonathan CameronA typical IIO driver will register itself as an :doc:`I2C <../i2c>` or
3349b2fd6eSJonathan Cameron:doc:`SPI <../spi>` driver and will create two routines, probe and remove.
3449b2fd6eSJonathan Cameron
3549b2fd6eSJonathan CameronAt probe:
3649b2fd6eSJonathan Cameron
37a320274aSPuranjay Mohan1. Call iio_device_alloc(), which allocates memory for an IIO device.
3849b2fd6eSJonathan Cameron2. Initialize IIO device fields with driver specific information (e.g.
3949b2fd6eSJonathan Cameron   device name, device channels).
40a320274aSPuranjay Mohan3. Call iio_device_register(), this registers the device with the
4149b2fd6eSJonathan Cameron   IIO core. After this call the device is ready to accept requests from user
4249b2fd6eSJonathan Cameron   space applications.
4349b2fd6eSJonathan Cameron
4449b2fd6eSJonathan CameronAt remove, we free the resources allocated in probe in reverse order:
4549b2fd6eSJonathan Cameron
46a320274aSPuranjay Mohan1. iio_device_unregister(), unregister the device from the IIO core.
47a320274aSPuranjay Mohan2. iio_device_free(), free the memory allocated for the IIO device.
4849b2fd6eSJonathan Cameron
4949b2fd6eSJonathan CameronIIO device sysfs interface
5049b2fd6eSJonathan Cameron==========================
5149b2fd6eSJonathan Cameron
5249b2fd6eSJonathan CameronAttributes are sysfs files used to expose chip info and also allowing
5349b2fd6eSJonathan Cameronapplications to set various configuration parameters. For device with
5449b2fd6eSJonathan Cameronindex X, attributes can be found under /sys/bus/iio/iio:deviceX/ directory.
5549b2fd6eSJonathan CameronCommon attributes are:
5649b2fd6eSJonathan Cameron
5749b2fd6eSJonathan Cameron* :file:`name`, description of the physical chip.
5849b2fd6eSJonathan Cameron* :file:`dev`, shows the major:minor pair associated with
5949b2fd6eSJonathan Cameron  :file:`/dev/iio:deviceX` node.
6049b2fd6eSJonathan Cameron* :file:`sampling_frequency_available`, available discrete set of sampling
6149b2fd6eSJonathan Cameron  frequency values for device.
6249b2fd6eSJonathan Cameron* Available standard attributes for IIO devices are described in the
6349b2fd6eSJonathan Cameron  :file:`Documentation/ABI/testing/sysfs-bus-iio` file in the Linux kernel
6449b2fd6eSJonathan Cameron  sources.
6549b2fd6eSJonathan Cameron
6649b2fd6eSJonathan CameronIIO device channels
6749b2fd6eSJonathan Cameron===================
6849b2fd6eSJonathan Cameron
69*9303c9d5SMauro Carvalho Chehabstruct iio_chan_spec - specification of a single channel
7049b2fd6eSJonathan Cameron
7149b2fd6eSJonathan CameronAn IIO device channel is a representation of a data channel. An IIO device can
7249b2fd6eSJonathan Cameronhave one or multiple channels. For example:
7349b2fd6eSJonathan Cameron
7449b2fd6eSJonathan Cameron* a thermometer sensor has one channel representing the temperature measurement.
7549b2fd6eSJonathan Cameron* a light sensor with two channels indicating the measurements in the visible
7649b2fd6eSJonathan Cameron  and infrared spectrum.
7749b2fd6eSJonathan Cameron* an accelerometer can have up to 3 channels representing acceleration on X, Y
7849b2fd6eSJonathan Cameron  and Z axes.
7949b2fd6eSJonathan Cameron
80*9303c9d5SMauro Carvalho ChehabAn IIO channel is described by the struct iio_chan_spec.
8149b2fd6eSJonathan CameronA thermometer driver for the temperature sensor in the example above would
8249b2fd6eSJonathan Cameronhave to describe its channel as follows::
8349b2fd6eSJonathan Cameron
8449b2fd6eSJonathan Cameron   static const struct iio_chan_spec temp_channel[] = {
8549b2fd6eSJonathan Cameron        {
8649b2fd6eSJonathan Cameron            .type = IIO_TEMP,
8749b2fd6eSJonathan Cameron            .info_mask_separate = BIT(IIO_CHAN_INFO_PROCESSED),
8849b2fd6eSJonathan Cameron        },
8949b2fd6eSJonathan Cameron   };
9049b2fd6eSJonathan Cameron
9149b2fd6eSJonathan CameronChannel sysfs attributes exposed to userspace are specified in the form of
9249b2fd6eSJonathan Cameronbitmasks. Depending on their shared info, attributes can be set in one of the
9349b2fd6eSJonathan Cameronfollowing masks:
9449b2fd6eSJonathan Cameron
9549b2fd6eSJonathan Cameron* **info_mask_separate**, attributes will be specific to
9649b2fd6eSJonathan Cameron  this channel
9749b2fd6eSJonathan Cameron* **info_mask_shared_by_type**, attributes are shared by all channels of the
9849b2fd6eSJonathan Cameron  same type
9949b2fd6eSJonathan Cameron* **info_mask_shared_by_dir**, attributes are shared by all channels of the same
10049b2fd6eSJonathan Cameron  direction
10149b2fd6eSJonathan Cameron* **info_mask_shared_by_all**, attributes are shared by all channels
10249b2fd6eSJonathan Cameron
10349b2fd6eSJonathan CameronWhen there are multiple data channels per channel type we have two ways to
10449b2fd6eSJonathan Camerondistinguish between them:
10549b2fd6eSJonathan Cameron
10649b2fd6eSJonathan Cameron* set **.modified** field of :c:type:`iio_chan_spec` to 1. Modifiers are
10749b2fd6eSJonathan Cameron  specified using **.channel2** field of the same :c:type:`iio_chan_spec`
10849b2fd6eSJonathan Cameron  structure and are used to indicate a physically unique characteristic of the
10949b2fd6eSJonathan Cameron  channel such as its direction or spectral response. For example, a light
11049b2fd6eSJonathan Cameron  sensor can have two channels, one for infrared light and one for both
11149b2fd6eSJonathan Cameron  infrared and visible light.
11249b2fd6eSJonathan Cameron* set **.indexed** field of :c:type:`iio_chan_spec` to 1. In this case the
11349b2fd6eSJonathan Cameron  channel is simply another instance with an index specified by the **.channel**
11449b2fd6eSJonathan Cameron  field.
11549b2fd6eSJonathan Cameron
11649b2fd6eSJonathan CameronHere is how we can make use of the channel's modifiers::
11749b2fd6eSJonathan Cameron
11849b2fd6eSJonathan Cameron   static const struct iio_chan_spec light_channels[] = {
11949b2fd6eSJonathan Cameron           {
12049b2fd6eSJonathan Cameron                   .type = IIO_INTENSITY,
12149b2fd6eSJonathan Cameron                   .modified = 1,
12249b2fd6eSJonathan Cameron                   .channel2 = IIO_MOD_LIGHT_IR,
12349b2fd6eSJonathan Cameron                   .info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
12449b2fd6eSJonathan Cameron                   .info_mask_shared = BIT(IIO_CHAN_INFO_SAMP_FREQ),
12549b2fd6eSJonathan Cameron           },
12649b2fd6eSJonathan Cameron           {
12749b2fd6eSJonathan Cameron                   .type = IIO_INTENSITY,
12849b2fd6eSJonathan Cameron                   .modified = 1,
12949b2fd6eSJonathan Cameron                   .channel2 = IIO_MOD_LIGHT_BOTH,
13049b2fd6eSJonathan Cameron                   .info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
13149b2fd6eSJonathan Cameron                   .info_mask_shared = BIT(IIO_CHAN_INFO_SAMP_FREQ),
13249b2fd6eSJonathan Cameron           },
13349b2fd6eSJonathan Cameron           {
13449b2fd6eSJonathan Cameron                   .type = IIO_LIGHT,
13549b2fd6eSJonathan Cameron                   .info_mask_separate = BIT(IIO_CHAN_INFO_PROCESSED),
13649b2fd6eSJonathan Cameron                   .info_mask_shared = BIT(IIO_CHAN_INFO_SAMP_FREQ),
13749b2fd6eSJonathan Cameron           },
13849b2fd6eSJonathan Cameron      }
13949b2fd6eSJonathan Cameron
14049b2fd6eSJonathan CameronThis channel's definition will generate two separate sysfs files for raw data
14149b2fd6eSJonathan Cameronretrieval:
14249b2fd6eSJonathan Cameron
14349b2fd6eSJonathan Cameron* :file:`/sys/bus/iio/iio:device{X}/in_intensity_ir_raw`
14449b2fd6eSJonathan Cameron* :file:`/sys/bus/iio/iio:device{X}/in_intensity_both_raw`
14549b2fd6eSJonathan Cameron
14649b2fd6eSJonathan Cameronone file for processed data:
14749b2fd6eSJonathan Cameron
14849b2fd6eSJonathan Cameron* :file:`/sys/bus/iio/iio:device{X}/in_illuminance_input`
14949b2fd6eSJonathan Cameron
15049b2fd6eSJonathan Cameronand one shared sysfs file for sampling frequency:
15149b2fd6eSJonathan Cameron
15249b2fd6eSJonathan Cameron* :file:`/sys/bus/iio/iio:device{X}/sampling_frequency`.
15349b2fd6eSJonathan Cameron
15449b2fd6eSJonathan CameronHere is how we can make use of the channel's indexing::
15549b2fd6eSJonathan Cameron
15649b2fd6eSJonathan Cameron   static const struct iio_chan_spec light_channels[] = {
15749b2fd6eSJonathan Cameron           {
15849b2fd6eSJonathan Cameron                   .type = IIO_VOLTAGE,
15949b2fd6eSJonathan Cameron		   .indexed = 1,
16049b2fd6eSJonathan Cameron		   .channel = 0,
16149b2fd6eSJonathan Cameron		   .info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
16249b2fd6eSJonathan Cameron	   },
16349b2fd6eSJonathan Cameron           {
16449b2fd6eSJonathan Cameron	           .type = IIO_VOLTAGE,
16549b2fd6eSJonathan Cameron                   .indexed = 1,
16649b2fd6eSJonathan Cameron                   .channel = 1,
16749b2fd6eSJonathan Cameron                   .info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
16849b2fd6eSJonathan Cameron           },
16949b2fd6eSJonathan Cameron   }
17049b2fd6eSJonathan Cameron
17149b2fd6eSJonathan CameronThis will generate two separate attributes files for raw data retrieval:
17249b2fd6eSJonathan Cameron
17349b2fd6eSJonathan Cameron* :file:`/sys/bus/iio/devices/iio:device{X}/in_voltage0_raw`, representing
17449b2fd6eSJonathan Cameron  voltage measurement for channel 0.
17549b2fd6eSJonathan Cameron* :file:`/sys/bus/iio/devices/iio:device{X}/in_voltage1_raw`, representing
17649b2fd6eSJonathan Cameron  voltage measurement for channel 1.
17749b2fd6eSJonathan Cameron
17849b2fd6eSJonathan CameronMore details
17949b2fd6eSJonathan Cameron============
18049b2fd6eSJonathan Cameron.. kernel-doc:: include/linux/iio/iio.h
18149b2fd6eSJonathan Cameron.. kernel-doc:: drivers/iio/industrialio-core.c
18249b2fd6eSJonathan Cameron   :export:
183