As part of recent projects, we had to dig into the Linux kernel Industrial I/O (IIO) subsystem with the goals of supporting a new ADC and adding new features to an existing driver. These tasks involved quite a few discussions between our engineering team and the IIO maintainers and reviewers. The aim of this blog post is to summarize the substance of these explanations to help others understand how an IIO kernel driver works and interacts with the core IIO subsystem.
Disclaimer: The IIO core is huge and keeps evolving. The aim of this article is not to cover it entirely, but at least explain our knowledge of how to use its basic features for common situations.
What is IIO?
The Industrial I/O subsystem covers any type of device that is commonly called as a “sensor”: ADCs, IMUs, temperature sensors, accelerometers, pressure sensors, potentiometers, light sensors, proximity sensors, etc (as well as few actuators, which I will on purpose disregard in this blog post). All these devices, besides measuring truly different physical components of our three dimensional world, end-up sharing quite a few properties. Any of these sensors must first be configured in order to know what must be measured and possibly how. When adequate, the device must be triggered in order to start converting. When the requested samples are ready, there must be some kind of signaling involved in order for the user to retrieve and process the data.
When thinking about the generic interfaces which could be needed by all these devices, it is quite straightforward to list:
- The configuration before sampling
- The triggering mechanism
- The signaling for an end of conversion situation
- The reading of the samples
- The advertisement of the data
Registering an IIO device
The IIO core manipulates struct iio_dev *
objects which inherits from struct device
. This object should be allocated by the device driver with devm_iio_device_alloc()
, providing the size of the driver’s internal structure as second argument. The allocated area dedicated for this internal pointer can be then retrieved with iio_priv()
.
This iio_dev
structure must then be filled with a number of information:
- The name of the device
- A set of
struct iio_info
operations, typically a hook to read one or multiple samples on demand, optionally be able to write to the device, etc. - A set of supported modes, such as
INDIO_DIRECT_MODE
, which is used when samples can be retrieved at any time by the user from sysfs. - A scan mask, namely
available_scan_masks
which defines what are the possible/impossible scan combinations when requesting a read. Typically, a device might be configured to scan all of its internal channels from 1 to N. This can be described with a list ofGENMASK(X, 0)
, with X ranging from 0 to the maximum number of channels. When the user will request a given set of channels, the IIO core will go through all the available masks registered by the driver and pick the first one that contains the desired channels. The selected mask will be available to the driver through theactive_scan_mask
entry of theiio_dev
structure. If ‘anything goes’ and the devices has no restriction regarding which channel(s) can be scanned, this field should be skipped. - A definition of all the possible channels, including the type of physical measurements the device is able to perform (
IIO_VOLTAGE
,IIO_CURRENT
,IIO_TEMPERATURE
,IIO_STEPS
,IIO_ROT
, etc), the channel index and the data format.
Here is an example of channel description and below the meaning of these fields.
struct iio_chan_spec chan1 = { .type = IIO_VOLTAGE, .indexed = 0, .channel = index, .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE), .scan_index = 1, .scan_type = { .sign = 'u', .realbits = 10, .storagebits = 16, .shift = 2, .endianness = IIO_BE, }, }
.info_mask_separate
indicates an entry in sysfs that will be present for this the channel. In this case,IIO_CHAN_INFO_RAW
indicates it is going to be the raw value of the sample..info_mask_shared_by_type
indicates an entry in sysfs that will be shared by all channels of the same type.IIO_CHAN_INFO_SCALE
means that there will be a common voltage scale sysfs entry shared by all the voltage raw entries. If the device was also able to read a temperature, we would also have had a single file indicating the scale for all the temperature samples.- The
.scan_type
field in the example indicates that values are provided as 16-bit big-endian samples that must be shifted by two bits. The full scale range is 0-1023. This conversion only applies to the buffer reading path: raw values directly read from sysfs and returned by the->read_raw()
hook (see below) should be converted by the driver itself.
Once the device fully described (and initialized, of course), the driver must register it with devm_iio_device_register()
.
Scaling factors
The int (*read_raw)(struct iio_dev *indio_dev, struct iio_chan_spec const *chan, int *val, int *val2, long mask)
callback will be executed when reading either of the ‘raw value’ or ‘scale’ files from sysfs.
The type of data that must be returned is provided in the mask parameter: IIO_CHAN_INFO_RAW
to retrieve the raw measurement or IIO_CHAN_INFO_SCALE
to retrieve the scaling parameters, based on the scale information available in the iio_chan_spec
structure that describes the channel.
For the IIO_CHAN_INFO_RAW
case, most drivers return an IIO_VAL_INT type which can be simply “returned” into the *val
argument. It is however possible to return a fixed point number, in this case the logic explained right after applies.
For the IIO_CHAN_INFO_SCALE
case, the return value indicates what type of scaling should be done. In most cases here a fixed point value will be used so *val
and *val2
will carry the scaling parameters. Here are two examples:
IIO_TEMP
example:*val = 1; *val2 = 8; ret = IIO_VAL_FRACTIONAL;
The full scale sample value should be multiplied by 1/8 in order to get Celcius degrees.
-
IIO_VOLTAGE
example:*val = 2500; *val2 = 10; ret = IIO_VAL_FRACTIONAL_LOG2;
The full scale range is a 10-bit value mapped to a 0-2500mV input level, said otherwise the scaling factor should be 2500 / 1024. The core will automatically do the computation of this factor and return 2,44140625 to the user in order to get milli-Volts.
Sampling
There are two basic common cases here.
In simple situations, a “single” on-demand read was issued by user-space directly by reading /sys/bus/devices/iio:device
. In this case the ->read_raw()
callback should handle basically all the steps necessary to get a measurement, as detailed in our introduction.
However, user-space can also pick a more advanced way of interacting with the measurement device, called triggers.
A trigger is a specific configuration of the device which will sample a number of channels upon a specific event. This event might be the user requesting it from userspace with a so called software trigger, it might also be an external hardware event, or a periodic signal, or an internal continuous read mode… There are many ways of triggering a sensor and they are all covered by the subsystem.
Many devices cannot handle both modes at the same time. The only situation where this might work smoothly is when a device provides a hardware FIFO where you can read from (or a ‘latest value’ register) while not disrupting the FIFO read back. Otherwise, it will be needed, in order to avoid collisions between these two modes, to verify that exclusive access to the device is granted with a call to iio_device_claim_direct_mode()
when starting a direct mode operation. As this helper grabs a mutex, it should be only called from process context and always be balanced with a call to iio_device_release_direct_mode()
.
IIO interoperability model
In the IIO core these four concepts are used:
- IIO device: the hardware part which produces samples
- IIO trigger: the signaling capability to request a conversion start
- IIO buffers: where to store the samples
- IIO events: threshold detectors
Even though a single hardware device might have hardware support for all these features, they must be described and handled separately so that, when applicable, other IIO devices might use them as well, eg. IIO device 2 could start a conversion upon IIO device 1 trigger state change. In practice it is not always possible but the way the API is built should lead us to keep things well separated anyway.
Registering a trigger
A struct iio_trigger
must be allocated by devm_iio_trigger_alloc()
, giving the new trigger a name.
The trigger should then receive a set of operations (struct iio_trigger_ops
) with at least ->set_trigger_state()
implemented, in order to switch on and off the trigger. One can use iio_trigger_set_drvdata()
in order to link private data with the trigger and get this pointer back from the trigger callbacks.
Once initialized, devm_iio_trigger_register()
will register the IIO trigger. This trigger will appear as a dedicated IIO device in sysfs.
It is likely that an IRQ will need to be registered as part of the trigger initialization step: the driver must be notified somehow that the trigger was toggled. If the asynchronous signaling is tied to a “trigger change” condition, which is the easiest situation, then it is advised to provide iio_trigger_generic_data_rdy_poll()
as hard IRQ handler. This helper will just call iio_trigger_poll()
and return.
You may of course want to handle more than this but in any case the rule is clear, triggers, buffers and devices should be fully separated. Hence, do not directly handle any data from this handler: an IIO trigger is only supposed to indicate a hardware transition, no more.
The call to iio_trigger_poll()
will effectively go through the IIO internal interrupt tree, find the device that is connected to the trigger which fired and call the relevant handler in order to request the waiting device to process the data (which may be identical or different than the triggering device).
In the case of the device being limited to, for instance, an End Of Conversion (EOC) interrupt, you should still consider this signal as being suitable for being registered as a trigger. Yes, this might imply an additional delay between the hardware toggling and the IRQ being fired which is not ideal, but from a software point of view, the split between driver code and core logic will let other IIO devices use this IRQ as a trigger with no additional change needed to your code.
Note: There is one exception here. When a device does not provide any visible per-scan interrupt and the software has only access to some kind of FIFO watermark events, the whole trigger + buffer representation is swapped with a pure buffer-only implementation.
Registering triggered buffers
If the device itself is able to provide fast samples, the driver should also register a buffer, with iio_triggered_buffer_setup()
. Both a hard IRQ handler and a threaded IRQ handler can be registered, as well as additional callbacks called before and after enabling and disabling the buffers in order to eg. configure the requested channels based on the current ->active_scan_mask
.
Upon a trigger condition, these are the handlers that might be chosen by the core if the trigger is connected to your device!
The hard IRQ handler might be used to eg. save timestamps. The threaded IRQ handler is dedicated to the data processing. Depending of the type of trigger (iio_trigger_using_own()
) the driver must decide whether it should start a conversion manually or if the data is waiting somewhere in a hardware FIFO, ready to be retrieved.
The final step is to push the samples into the core’s buffers. This should not be done manually. Let’s say that the user requested channels 0, 1 and 3 while the selected scan mask was including all channels from 0 to 4. Just calling iio_push_to_buffers()
is the solution: the core knows that it will receive five samples of 16 bits, it also knows that the user only requested three of them and will automatically pick the right ones.
With all these IIO objects registered, you should be able to properly interact with the core and the other drivers, providing trigger capabilities to third party devices, or benefiting from other’s triggers.
What if my design lacks trigger capabilities?
You can still use triggers by enabling IIO_CONFIGFS
(enables the configuration interface) and IIO_SW_TRIGGER
. Then, you can either choose to trigger your scans from userspace with a simple file write, thanks to CONFIG_IIO_SYSFS_TRIGGER
, or leverage timers to get periodic scans with CONFIG_IIO_HRTIMER_TRIGGER
.
As an example, here is how to create a sysfs trigger:
# echo 0 > /sys/bus/iio/devices/iio_sysfs_trigger/add_trigger # cat /sys/bus/iio/devices/iio_sysfs_trigger/trigger0/name sysfstrig0
And here is how to create a timer based software trigger:
# mkdir -p /config # mount -t configfs none /config # mkdir /config/iio/triggers/hrtimer/my_5ms_trigger # cat /sys/bus/iio/devices/trigger0/name my_5ms_trigger # echo 200 > /sys/bus/iio/devices/trigger0/sampling_frequency
How to use triggers and buffers from userspace
Just for the reference, linking an IIO trigger to an IIO device is as simple as:
# cat /sys/bus/iio/devices/trigger0/name > /sys/bus/iio/devices/iio:device0/trigger/current_trigger
The next step is to configure the channels that should be scanned:
# echo 1 > /sys/bus/iio/devices/iio:device0/scan_elements/in_voltage0_en # echo 1 > /sys/bus/iio/devices/iio:device0/scan_elements/in_voltage1_en # echo 1 > /sys/bus/iio/devices/iio:device0/scan_elements/in_voltage3_en
Starting the sampling process is managed with:
# echo 1 > /sys/bus/iio/devices/iio:device0/buffer/enable
In the case of a sysfs software trigger, it is the user’s responsibility to timely run:
# echo 1 > /sys/bus/iio/devices/trigger0/trigger_now
The samples are available to be read in /dev/iio\:device0
.
The decoding process before the scaling operation must be performed by the userspace, following the content of:
# cat /sys/bus/iio/devices/iio:device0/scan_elements/in_voltage0_type be:u12/16>>2
Which in this case would mean that each sample is 16 bits wide, values should be considered big-endian, shifted twice before being considered as an unsigned 12-bit value.
# od -t x1 /dev/iio\:device0 0000000 08 06
Should be interpreted as 0x806 >> 2 = 0x201
, which should be then multipled by the scaling factor in order to get the final mV value.
Conclusion
While contributing to this (relatively new) subsystem, we discovered a number of interesting features and design choices which would really benefit from a much tougher in-kernel documentation as most of the available information explains how to use IIO (with libiio
or configfs
) more than how to write a decent and properly shaped IIO device driver. As the subsystem is still pretty recent, it is valid to look at existing drivers to make design choices, but that is not a magic solution as no device never fully matches any software API anyway, and sensors unfortunately do not escape from that sticky rule.
We want to warmly thank Jonathan Cameron, IIO founder and maintainer, for his precious feedback on the mailing list, as well as his valuable review and contribution to this blog post.
We hope this article will help you go through this API and if it does, please mind letting us know by dropping a comment in the section down below!
Awesome article!
A very helpful document!
Exactly what I have been looking for! Thank you!
Really good.
very good blog
The examples of IIO_VAL_FRACTIONAL and IIO_VAL_FRACTIONAL_LOG2 are golden. Please, send a patch which adds them as kerneldoc to the IIO headers.
I remember I spent way too long reading the __iio_format_value(). It was teodious trying to figure out what those formats actually are by reading the code which formats those values for sysfs output.
Mhh my understanding would be that `info_mask_separate` only concerns the single channel it is applied to, while `info_mask_shared_by_all` would be for all channels.
Indeed the raw value of a channel only concerns that specific channel.
That’s right, I just corrected the sentence (and the one right after). Thanks!