1=============
2Core elements
3=============
4
5The Industrial I/O core offers both a unified framework for writing drivers for
6many different types of embedded sensors and a standard interface to user space
7applications manipulating sensors. The implementation can be found under
8:file:`drivers/iio/industrialio-*`
9
10Industrial I/O Devices
11----------------------
12
13* struct iio_dev - industrial I/O device
14* iio_device_alloc() - allocate an :c:type:`iio_dev` from a driver
15* iio_device_free() - free an :c:type:`iio_dev` from a driver
16* iio_device_register() - register a device with the IIO subsystem
17* iio_device_unregister() - unregister a device from the IIO
18  subsystem
19
20An IIO device usually corresponds to a single hardware sensor and it
21provides all the information needed by a driver handling a device.
22Let's first have a look at the functionality embedded in an IIO device
23then we will show how a device driver makes use of an IIO device.
24
25There are two ways for a user space application to interact with an IIO driver.
26
271. :file:`/sys/bus/iio/iio:device{X}/`, this represents a hardware sensor
28   and groups together the data channels of the same chip.
292. :file:`/dev/iio:device{X}`, character device node interface used for
30   buffered data transfer and for events information retrieval.
31
32A typical IIO driver will register itself as an :doc:`I2C <../i2c>` or
33:doc:`SPI <../spi>` driver and will create two routines, probe and remove.
34
35At probe:
36
371. Call iio_device_alloc(), which allocates memory for an IIO device.
382. Initialize IIO device fields with driver specific information (e.g.
39   device name, device channels).
403. Call iio_device_register(), this registers the device with the
41   IIO core. After this call the device is ready to accept requests from user
42   space applications.
43
44At remove, we free the resources allocated in probe in reverse order:
45
461. iio_device_unregister(), unregister the device from the IIO core.
472. iio_device_free(), free the memory allocated for the IIO device.
48
49IIO device sysfs interface
50==========================
51
52Attributes are sysfs files used to expose chip info and also allowing
53applications to set various configuration parameters. For device with
54index X, attributes can be found under /sys/bus/iio/iio:deviceX/ directory.
55Common attributes are:
56
57* :file:`name`, description of the physical chip.
58* :file:`dev`, shows the major:minor pair associated with
59  :file:`/dev/iio:deviceX` node.
60* :file:`sampling_frequency_available`, available discrete set of sampling
61  frequency values for device.
62* Available standard attributes for IIO devices are described in the
63  :file:`Documentation/ABI/testing/sysfs-bus-iio` file in the Linux kernel
64  sources.
65
66IIO device channels
67===================
68
69struct iio_chan_spec - specification of a single channel
70
71An IIO device channel is a representation of a data channel. An IIO device can
72have one or multiple channels. For example:
73
74* a thermometer sensor has one channel representing the temperature measurement.
75* a light sensor with two channels indicating the measurements in the visible
76  and infrared spectrum.
77* an accelerometer can have up to 3 channels representing acceleration on X, Y
78  and Z axes.
79
80An IIO channel is described by the struct iio_chan_spec.
81A thermometer driver for the temperature sensor in the example above would
82have to describe its channel as follows::
83
84   static const struct iio_chan_spec temp_channel[] = {
85        {
86            .type = IIO_TEMP,
87            .info_mask_separate = BIT(IIO_CHAN_INFO_PROCESSED),
88        },
89   };
90
91Channel sysfs attributes exposed to userspace are specified in the form of
92bitmasks. Depending on their shared info, attributes can be set in one of the
93following masks:
94
95* **info_mask_separate**, attributes will be specific to
96  this channel
97* **info_mask_shared_by_type**, attributes are shared by all channels of the
98  same type
99* **info_mask_shared_by_dir**, attributes are shared by all channels of the same
100  direction
101* **info_mask_shared_by_all**, attributes are shared by all channels
102
103When there are multiple data channels per channel type we have two ways to
104distinguish between them:
105
106* set **.modified** field of :c:type:`iio_chan_spec` to 1. Modifiers are
107  specified using **.channel2** field of the same :c:type:`iio_chan_spec`
108  structure and are used to indicate a physically unique characteristic of the
109  channel such as its direction or spectral response. For example, a light
110  sensor can have two channels, one for infrared light and one for both
111  infrared and visible light.
112* set **.indexed** field of :c:type:`iio_chan_spec` to 1. In this case the
113  channel is simply another instance with an index specified by the **.channel**
114  field.
115
116Here is how we can make use of the channel's modifiers::
117
118   static const struct iio_chan_spec light_channels[] = {
119           {
120                   .type = IIO_INTENSITY,
121                   .modified = 1,
122                   .channel2 = IIO_MOD_LIGHT_IR,
123                   .info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
124                   .info_mask_shared = BIT(IIO_CHAN_INFO_SAMP_FREQ),
125           },
126           {
127                   .type = IIO_INTENSITY,
128                   .modified = 1,
129                   .channel2 = IIO_MOD_LIGHT_BOTH,
130                   .info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
131                   .info_mask_shared = BIT(IIO_CHAN_INFO_SAMP_FREQ),
132           },
133           {
134                   .type = IIO_LIGHT,
135                   .info_mask_separate = BIT(IIO_CHAN_INFO_PROCESSED),
136                   .info_mask_shared = BIT(IIO_CHAN_INFO_SAMP_FREQ),
137           },
138      }
139
140This channel's definition will generate two separate sysfs files for raw data
141retrieval:
142
143* :file:`/sys/bus/iio/iio:device{X}/in_intensity_ir_raw`
144* :file:`/sys/bus/iio/iio:device{X}/in_intensity_both_raw`
145
146one file for processed data:
147
148* :file:`/sys/bus/iio/iio:device{X}/in_illuminance_input`
149
150and one shared sysfs file for sampling frequency:
151
152* :file:`/sys/bus/iio/iio:device{X}/sampling_frequency`.
153
154Here is how we can make use of the channel's indexing::
155
156   static const struct iio_chan_spec light_channels[] = {
157           {
158                   .type = IIO_VOLTAGE,
159		   .indexed = 1,
160		   .channel = 0,
161		   .info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
162	   },
163           {
164	           .type = IIO_VOLTAGE,
165                   .indexed = 1,
166                   .channel = 1,
167                   .info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
168           },
169   }
170
171This will generate two separate attributes files for raw data retrieval:
172
173* :file:`/sys/bus/iio/devices/iio:device{X}/in_voltage0_raw`, representing
174  voltage measurement for channel 0.
175* :file:`/sys/bus/iio/devices/iio:device{X}/in_voltage1_raw`, representing
176  voltage measurement for channel 1.
177
178More details
179============
180.. kernel-doc:: include/linux/iio/iio.h
181.. kernel-doc:: drivers/iio/industrialio-core.c
182   :export:
183