1 <?xml version=
"1.0" encoding=
"UTF-8"?>
2 <!DOCTYPE book PUBLIC
"-//OASIS//DTD DocBook XML V4.1.2//EN"
3 "http://www.oasis-open.org/docbook/xml/4.1.2/docbookx.dtd" []
>
7 <title>Industrial I/O driver developer's guide
</title>
11 <firstname>Daniel
</firstname>
12 <surname>Baluta
</surname>
15 <email>daniel.baluta@intel.com
</email>
23 <holder>Intel Corporation
</holder>
28 This documentation is free software; you can redistribute
29 it and/or modify it under the terms of the GNU General Public
38 <title>Introduction
</title>
40 The main purpose of the Industrial I/O subsystem (IIO) is to provide
41 support for devices that in some sense perform either analog-to-digital
42 conversion (ADC) or digital-to-analog conversion (DAC) or both. The aim
43 is to fill the gap between the somewhat similar hwmon and input
45 Hwmon is directed at low sample rate sensors used to monitor and
46 control the system itself, like fan speed control or temperature
47 measurement. Input is, as its name suggests, focused on human interaction
48 input devices (keyboard, mouse, touchscreen). In some cases there is
49 considerable overlap between these and IIO.
52 Devices that fall into this category include:
55 analog to digital converters (ADCs)
61 capacitance to digital converters (CDCs)
64 digital to analog converters (DACs)
70 inertial measurement units (IMUs)
73 color and light sensors
88 Usually these sensors are connected via SPI or I2C. A common use case of the
89 sensors devices is to have combined functionality (e.g. light plus proximity
93 <chapter id='iiosubsys'
>
94 <title>Industrial I/O core
</title>
96 The Industrial I/O core offers:
99 a unified framework for writing drivers for many different types of
103 a standard interface to user space applications manipulating sensors.
106 The implementation can be found under
<filename>
107 drivers/iio/industrialio-*
</filename>
109 <sect1 id=
"iiodevice">
110 <title> Industrial I/O devices
</title>
112 !Finclude/linux/iio/iio.h iio_dev
113 !Fdrivers/iio/industrialio-core.c iio_device_alloc
114 !Fdrivers/iio/industrialio-core.c iio_device_free
115 !Fdrivers/iio/industrialio-core.c iio_device_register
116 !Fdrivers/iio/industrialio-core.c iio_device_unregister
119 An IIO device usually corresponds to a single hardware sensor and it
120 provides all the information needed by a driver handling a device.
121 Let's first have a look at the functionality embedded in an IIO
122 device then we will show how a device driver makes use of an IIO
126 There are two ways for a user space application to interact
130 <filename>/sys/bus/iio/iio:deviceX/
</filename>, this
131 represents a hardware sensor and groups together the data
132 channels of the same chip.
135 <filename>/dev/iio:deviceX
</filename>, character device node
136 interface used for buffered data transfer and for events information
141 A typical IIO driver will register itself as an I2C or SPI driver and will
142 create two routines,
<function> probe
</function> and
<function> remove
143 </function>. At
<function>probe
</function>:
145 <listitem>call
<function>iio_device_alloc
</function>, which allocates memory
148 <listitem> initialize IIO device fields with driver specific information
149 (e.g. device name, device channels).
151 <listitem>call
<function> iio_device_register
</function>, this registers the
152 device with the IIO core. After this call the device is ready to accept
153 requests from user space applications.
156 At
<function>remove
</function>, we free the resources allocated in
157 <function>probe
</function> in reverse order:
159 <listitem><function>iio_device_unregister
</function>, unregister the device
162 <listitem><function>iio_device_free
</function>, free the memory allocated
167 <sect2 id=
"iioattr"> <title> IIO device sysfs interface
</title>
169 Attributes are sysfs files used to expose chip info and also allowing
170 applications to set various configuration parameters. For device
171 with index X, attributes can be found under
172 <filename>/sys/bus/iio/iio:deviceX/
</filename> directory.
173 Common attributes are:
175 <listitem><filename>name
</filename>, description of the physical
178 <listitem><filename>dev
</filename>, shows the major:minor pair
179 associated with
<filename>/dev/iio:deviceX
</filename> node.
181 <listitem><filename>sampling_frequency_available
</filename>,
182 available discrete set of sampling frequency values for
186 Available standard attributes for IIO devices are described in the
187 <filename>Documentation/ABI/testing/sysfs-bus-iio
</filename> file
188 in the Linux kernel sources.
191 <sect2 id=
"iiochannel"> <title> IIO device channels
</title>
192 !Finclude/linux/iio/iio.h iio_chan_spec structure.
194 An IIO device channel is a representation of a data channel. An
195 IIO device can have one or multiple channels. For example:
198 a thermometer sensor has one channel representing the
199 temperature measurement.
202 a light sensor with two channels indicating the measurements in
203 the visible and infrared spectrum.
206 an accelerometer can have up to
3 channels representing
207 acceleration on X, Y and Z axes.
210 An IIO channel is described by the
<type> struct iio_chan_spec
211 </type>. A thermometer driver for the temperature sensor in the
212 example above would have to describe its channel as follows:
214 static const struct iio_chan_spec temp_channel[] = {
217 .info_mask_separate = BIT(IIO_CHAN_INFO_PROCESSED),
222 Channel sysfs attributes exposed to userspace are specified in
223 the form of
<emphasis>bitmasks
</emphasis>. Depending on their
224 shared info, attributes can be set in one of the following masks:
226 <listitem><emphasis>info_mask_separate
</emphasis>, attributes will
227 be specific to this channel
</listitem>
228 <listitem><emphasis>info_mask_shared_by_type
</emphasis>,
229 attributes are shared by all channels of the same type
</listitem>
230 <listitem><emphasis>info_mask_shared_by_dir
</emphasis>, attributes
231 are shared by all channels of the same direction
</listitem>
232 <listitem><emphasis>info_mask_shared_by_all
</emphasis>,
233 attributes are shared by all channels
</listitem>
235 When there are multiple data channels per channel type we have two
236 ways to distinguish between them:
238 <listitem> set
<emphasis> .modified
</emphasis> field of
<type>
239 iio_chan_spec
</type> to
1. Modifiers are specified using
240 <emphasis>.channel2
</emphasis> field of the same
241 <type>iio_chan_spec
</type> structure and are used to indicate a
242 physically unique characteristic of the channel such as its direction
243 or spectral response. For example, a light sensor can have two channels,
244 one for infrared light and one for both infrared and visible light.
246 <listitem> set
<emphasis>.indexed
</emphasis> field of
247 <type>iio_chan_spec
</type> to
1. In this case the channel is
248 simply another instance with an index specified by the
249 <emphasis>.channel
</emphasis> field.
252 Here is how we can make use of the channel's modifiers:
254 static const struct iio_chan_spec light_channels[] = {
256 .type = IIO_INTENSITY,
258 .channel2 = IIO_MOD_LIGHT_IR,
259 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
260 .info_mask_shared = BIT(IIO_CHAN_INFO_SAMP_FREQ),
263 .type = IIO_INTENSITY,
265 .channel2 = IIO_MOD_LIGHT_BOTH,
266 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
267 .info_mask_shared = BIT(IIO_CHAN_INFO_SAMP_FREQ),
271 .info_mask_separate = BIT(IIO_CHAN_INFO_PROCESSED),
272 .info_mask_shared = BIT(IIO_CHAN_INFO_SAMP_FREQ),
277 This channel's definition will generate two separate sysfs files
278 for raw data retrieval:
281 <filename>/sys/bus/iio/iio:deviceX/in_intensity_ir_raw
</filename>
284 <filename>/sys/bus/iio/iio:deviceX/in_intensity_both_raw
</filename>
287 one file for processed data:
290 <filename>/sys/bus/iio/iio:deviceX/in_illuminance_input
294 and one shared sysfs file for sampling frequency:
297 <filename>/sys/bus/iio/iio:deviceX/sampling_frequency.
303 Here is how we can make use of the channel's indexing:
305 static const struct iio_chan_spec light_channels[] = {
310 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
316 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
320 This will generate two separate attributes files for raw data
324 <filename>/sys/bus/iio/devices/iio:deviceX/in_voltage0_raw
</filename>,
325 representing voltage measurement for channel
0.
328 <filename>/sys/bus/iio/devices/iio:deviceX/in_voltage1_raw
</filename>,
329 representing voltage measurement for channel
1.
336 <sect1 id=
"iiobuffer"> <title> Industrial I/O buffers
</title>
337 !Finclude/linux/iio/buffer.h iio_buffer
338 !Edrivers/iio/industrialio-buffer.c
341 The Industrial I/O core offers a way for continuous data capture
342 based on a trigger source. Multiple data channels can be read at once
343 from
<filename>/dev/iio:deviceX
</filename> character device node,
344 thus reducing the CPU load.
347 <sect2 id=
"iiobuffersysfs">
348 <title>IIO buffer sysfs interface
</title>
350 An IIO buffer has an associated attributes directory under
<filename>
351 /sys/bus/iio/iio:deviceX/buffer/
</filename>. Here are the existing
355 <emphasis>length
</emphasis>, the total number of data samples
356 (capacity) that can be stored by the buffer.
359 <emphasis>enable
</emphasis>, activate buffer capture.
365 <sect2 id=
"iiobuffersetup"> <title> IIO buffer setup
</title>
366 <para>The meta information associated with a channel reading
367 placed in a buffer is called a
<emphasis> scan element
</emphasis>.
368 The important bits configuring scan elements are exposed to
369 userspace applications via the
<filename>
370 /sys/bus/iio/iio:deviceX/scan_elements/
</filename> directory. This
371 file contains attributes of the following form:
373 <listitem><emphasis>enable
</emphasis>, used for enabling a channel.
374 If and only if its attribute is non zero, then a triggered capture
375 will contain data samples for this channel.
377 <listitem><emphasis>type
</emphasis>, description of the scan element
378 data storage within the buffer and hence the form in which it is
379 read from user space. Format is
<emphasis>
380 [be|le]:[s|u]bits/storagebitsXrepeat[
>>shift]
</emphasis>.
382 <listitem> <emphasis>be
</emphasis> or
<emphasis>le
</emphasis>, specifies
383 big or little endian.
386 <emphasis>s
</emphasis>or
<emphasis>u
</emphasis>, specifies if
387 signed (
2's complement) or unsigned.
389 <listitem><emphasis>bits
</emphasis>, is the number of valid data
392 <listitem><emphasis>storagebits
</emphasis>, is the number of bits
393 (after padding) that it occupies in the buffer.
396 <emphasis>shift
</emphasis>, if specified, is the shift that needs
397 to be applied prior to masking out unused bits.
400 <emphasis>repeat
</emphasis>, specifies the number of bits/storagebits
401 repetitions. When the repeat element is
0 or
1, then the repeat
407 For example, a driver for a
3-axis accelerometer with
12 bit
408 resolution where data is stored in two
8-bits registers as
412 +---+---+---+---+---+---+---+---+
413 |D3 |D2 |D1 |D0 | X | X | X | X | (LOW byte, address
0x06)
414 +---+---+---+---+---+---+---+---+
417 +---+---+---+---+---+---+---+---+
418 |D11|D10|D9 |D8 |D7 |D6 |D5 |D4 | (HIGH byte, address
0x07)
419 +---+---+---+---+---+---+---+---+
422 will have the following scan element type for each axis:
424 $ cat /sys/bus/iio/devices/iio:device0/scan_elements/in_accel_y_type
427 A user space application will interpret data samples read from the
428 buffer as two byte little endian signed data, that needs a
4 bits
429 right shift before masking out the
12 valid bits of data.
432 For implementing buffer support a driver should initialize the following
433 fields in
<type>iio_chan_spec
</type> definition:
435 struct iio_chan_spec {
444 enum iio_endian endianness;
448 The driver implementing the accelerometer described above will
449 have the following channel definition:
451 struct struct iio_chan_spec accel_channels[] = {
455 .channel2 = IIO_MOD_X,
456 /* other stuff here */
463 .endianness = IIO_LE,
466 /* similar for Y (with channel2 = IIO_MOD_Y, scan_index =
1)
467 * and Z (with channel2 = IIO_MOD_Z, scan_index =
2) axis
473 Here
<emphasis> scan_index
</emphasis> defines the order in which
474 the enabled channels are placed inside the buffer. Channels with a lower
475 scan_index will be placed before channels with a higher index. Each
476 channel needs to have a unique scan_index.
479 Setting scan_index to -
1 can be used to indicate that the specific
480 channel does not support buffered capture. In this case no entries will
481 be created for the channel in the scan_elements directory.
486 <sect1 id=
"iiotrigger"> <title> Industrial I/O triggers
</title>
487 !Finclude/linux/iio/trigger.h iio_trigger
488 !Edrivers/iio/industrialio-trigger.c
490 In many situations it is useful for a driver to be able to
491 capture data based on some external event (trigger) as opposed
492 to periodically polling for data. An IIO trigger can be provided
493 by a device driver that also has an IIO device based on hardware
494 generated events (e.g. data ready or threshold exceeded) or
495 provided by a separate driver from an independent interrupt
496 source (e.g. GPIO line connected to some external system, timer
497 interrupt or user space writing a specific file in sysfs). A
498 trigger may initiate data capture for a number of sensors and
499 also it may be completely unrelated to the sensor itself.
502 <sect2 id=
"iiotrigsysfs"> <title> IIO trigger sysfs interface
</title>
503 There are two locations in sysfs related to triggers:
505 <listitem><filename>/sys/bus/iio/devices/triggerY
</filename>,
506 this file is created once an IIO trigger is registered with
507 the IIO core and corresponds to trigger with index Y. Because
508 triggers can be very different depending on type there are few
509 standard attributes that we can describe here:
512 <emphasis>name
</emphasis>, trigger name that can be later
513 used for association with a device.
516 <emphasis>sampling_frequency
</emphasis>, some timer based
517 triggers use this attribute to specify the frequency for
523 <filename>/sys/bus/iio/devices/iio:deviceX/trigger/
</filename>, this
524 directory is created once the device supports a triggered
525 buffer. We can associate a trigger with our device by writing
526 the trigger's name in the
<filename>current_trigger
</filename> file.
531 <sect2 id=
"iiotrigattr"> <title> IIO trigger setup
</title>
534 Let's see a simple example of how to setup a trigger to be used
538 struct iio_trigger_ops trigger_ops = {
539 .set_trigger_state = sample_trigger_state,
540 .validate_device = sample_validate_device,
543 struct iio_trigger *trig;
545 /* first, allocate memory for our trigger */
546 trig = iio_trigger_alloc(dev,
"trig-%s-%d", name, idx);
548 /* setup trigger operations field */
549 trig-
>ops =
&trigger_ops;
551 /* now register the trigger with the IIO core */
552 iio_trigger_register(trig);
557 <sect2 id=
"iiotrigsetup"> <title> IIO trigger ops
</title>
558 !Finclude/linux/iio/trigger.h iio_trigger_ops
560 Notice that a trigger has a set of operations attached:
563 <function>set_trigger_state
</function>, switch the trigger on/off
567 <function>validate_device
</function>, function to validate the
568 device when the current trigger gets changed.
574 <sect1 id=
"iiotriggered_buffer">
575 <title> Industrial I/O triggered buffers
</title>
577 Now that we know what buffers and triggers are let's see how they
580 <sect2 id=
"iiotrigbufsetup"> <title> IIO triggered buffer setup
</title>
581 !Edrivers/iio/buffer/industrialio-triggered-buffer.c
582 !Finclude/linux/iio/iio.h iio_buffer_setup_ops
586 A typical triggered buffer setup looks like this:
588 const struct iio_buffer_setup_ops sensor_buffer_setup_ops = {
589 .preenable = sensor_buffer_preenable,
590 .postenable = sensor_buffer_postenable,
591 .postdisable = sensor_buffer_postdisable,
592 .predisable = sensor_buffer_predisable,
595 irqreturn_t sensor_iio_pollfunc(int irq, void *p)
597 pf-
>timestamp = iio_get_time_ns();
598 return IRQ_WAKE_THREAD;
601 irqreturn_t sensor_trigger_handler(int irq, void *p)
606 /* read data for each active channel */
607 for_each_set_bit(bit, active_scan_mask, masklength)
608 buf[i++] = sensor_get_data(bit)
610 iio_push_to_buffers_with_timestamp(indio_dev, buf, timestamp);
612 iio_trigger_notify_done(trigger);
616 /* setup triggered buffer, usually in probe function */
617 iio_triggered_buffer_setup(indio_dev, sensor_iio_polfunc,
618 sensor_trigger_handler,
619 sensor_buffer_setup_ops);
622 The important things to notice here are:
624 <listitem><function> iio_buffer_setup_ops
</function>, the buffer setup
625 functions to be called at predefined points in the buffer configuration
626 sequence (e.g. before enable, after disable). If not specified, the
627 IIO core uses the default
<type>iio_triggered_buffer_setup_ops
</type>.
629 <listitem><function>sensor_iio_pollfunc
</function>, the function that
630 will be used as top half of poll function. It should do as little
631 processing as possible, because it runs in interrupt context. The most
632 common operation is recording of the current timestamp and for this reason
633 one can use the IIO core defined
<function>iio_pollfunc_store_time
634 </function> function.
636 <listitem><function>sensor_trigger_handler
</function>, the function that
637 will be used as bottom half of the poll function. This runs in the
638 context of a kernel thread and all the processing takes place here.
639 It usually reads data from the device and stores it in the internal
640 buffer together with the timestamp recorded in the top half.
646 <chapter id='iioresources'
>
647 <title> Resources
</title>
648 IIO core may change during time so the best documentation to read is the
649 source code. There are several locations where you should look:
652 <filename>drivers/iio/
</filename>, contains the IIO core plus
653 and directories for each sensor type (e.g. accel, magnetometer,
657 <filename>include/linux/iio/
</filename>, contains the header
658 files, nice to read for the internal kernel interfaces.
661 <filename>include/uapi/linux/iio/
</filename>, contains files to be
662 used by user space applications.
665 <filename>tools/iio/
</filename>, contains tools for rapidly
666 testing buffers, events and device creation.
669 <filename>drivers/staging/iio/
</filename>, contains code for some
670 drivers or experimental features that are not yet mature enough
675 Besides the code, there are some good online documentation sources:
678 <ulink url=
"http://marc.info/?l=linux-iio"> Industrial I/O mailing
682 <ulink url=
"http://wiki.analog.com/software/linux/docs/iio/iio">
683 Analog Device IIO wiki page
</ulink>
686 <ulink url=
"https://fosdem.org/2015/schedule/event/iiosdr/">
687 Using the Linux IIO framework for SDR, Lars-Peter Clausen's
688 presentation at FOSDEM
</ulink>
696 vim: softtabstop=2:shiftwidth=2:expandtab:textwidth=72