1<html><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8"><title>Chapter 2. Industrial I/O core</title><meta name="generator" content="DocBook XSL Stylesheets V1.78.1"><link rel="home" href="index.html" title="Industrial I/O driver developer's guide"><link rel="up" href="index.html" title="Industrial I/O driver developer's guide"><link rel="prev" href="intro.html" title="Chapter 1. Introduction"><link rel="next" href="API-struct-iio-dev.html" title="struct iio_dev"></head><body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF"><div class="navheader"><table width="100%" summary="Navigation header"><tr><th colspan="3" align="center">Chapter 2. Industrial I/O core</th></tr><tr><td width="20%" align="left"><a accesskey="p" href="intro.html">Prev</a> </td><th width="60%" align="center"> </th><td width="20%" align="right"> <a accesskey="n" href="API-struct-iio-dev.html">Next</a></td></tr></table><hr></div><div class="chapter"><div class="titlepage"><div><div><h1 class="title"><a name="iiosubsys"></a>Chapter 2. Industrial I/O core</h1></div></div></div><div class="toc"><p><b>Table of Contents</b></p><dl class="toc"><dt><span class="sect1"><a href="iiosubsys.html#iiodevice"> Industrial I/O devices </a></span></dt><dd><dl><dt><span class="sect2"><a href="iiosubsys.html#iioattr"> IIO device sysfs interface </a></span></dt><dt><span class="sect2"><a href="iiosubsys.html#iiochannel"> IIO device channels </a></span></dt></dl></dd><dt><span class="sect1"><a href="iiobuffer.html"> Industrial I/O buffers </a></span></dt><dd><dl><dt><span class="sect2"><a href="iiobuffer.html#iiobuffersysfs">IIO buffer sysfs interface </a></span></dt><dt><span class="sect2"><a href="iiobuffer.html#iiobuffersetup"> IIO buffer setup </a></span></dt></dl></dd><dt><span class="sect1"><a href="iiotrigger.html"> Industrial I/O triggers </a></span></dt><dd><dl><dt><span class="sect2"><a href="iiotrigger.html#iiotrigsysfs"> IIO trigger sysfs interface </a></span></dt><dt><span class="sect2"><a href="iiotrigger.html#iiotrigattr"> IIO trigger setup</a></span></dt><dt><span class="sect2"><a href="iiotrigger.html#iiotrigsetup"> IIO trigger ops</a></span></dt></dl></dd><dt><span class="sect1"><a href="iiotriggered_buffer.html"> Industrial I/O triggered buffers </a></span></dt><dd><dl><dt><span class="sect2"><a href="iiotriggered_buffer.html#iiotrigbufsetup"> IIO triggered buffer setup</a></span></dt></dl></dd></dl></div><p> 2 The Industrial I/O core offers: 3 </p><div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: disc; "><li class="listitem"> 4 a unified framework for writing drivers for many different types of 5 embedded sensors. 6 </li><li class="listitem"> 7 a standard interface to user space applications manipulating sensors. 8 </li></ul></div><p> 9 The implementation can be found under <code class="filename"> 10 drivers/iio/industrialio-*</code> 11 </p><div class="sect1"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a name="iiodevice"></a> Industrial I/O devices </h2></div></div></div><div class="toc"><dl class="toc"><dt><span class="refentrytitle"><a href="API-struct-iio-dev.html"><span class="phrase">struct iio_dev</span></a></span><span class="refpurpose"> — 12 industrial I/O device 13 </span></dt><dt><span class="refentrytitle"><a href="API-iio-device-alloc.html"><span class="phrase">iio_device_alloc</span></a></span><span class="refpurpose"> — 14 allocate an iio_dev from a driver 15 </span></dt><dt><span class="refentrytitle"><a href="API-iio-device-free.html"><span class="phrase">iio_device_free</span></a></span><span class="refpurpose"> — 16 free an iio_dev from a driver 17 </span></dt><dt><span class="refentrytitle"><a href="API-iio-device-register.html"><span class="phrase">iio_device_register</span></a></span><span class="refpurpose"> — 18 register a device with the IIO subsystem 19 </span></dt><dt><span class="refentrytitle"><a href="API-iio-device-unregister.html"><span class="phrase">iio_device_unregister</span></a></span><span class="refpurpose"> — 20 unregister a device from the IIO subsystem 21 </span></dt><dt><span class="sect2"><a href="iiosubsys.html#iioattr"> IIO device sysfs interface </a></span></dt><dt><span class="sect2"><a href="iiosubsys.html#iiochannel"> IIO device channels </a></span></dt></dl></div><p> 22 An IIO device usually corresponds to a single hardware sensor and it 23 provides all the information needed by a driver handling a device. 24 Let's first have a look at the functionality embedded in an IIO 25 device then we will show how a device driver makes use of an IIO 26 device. 27 </p><p> 28 There are two ways for a user space application to interact 29 with an IIO driver. 30 </p><div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: disc; "><li class="listitem"><code class="filename">/sys/bus/iio/iio:deviceX/</code>, this 31 represents a hardware sensor and groups together the data 32 channels of the same chip. 33 </li><li class="listitem"><code class="filename">/dev/iio:deviceX</code>, character device node 34 interface used for buffered data transfer and for events information 35 retrieval. 36 </li></ul></div><p> 37 </p> 38 A typical IIO driver will register itself as an I2C or SPI driver and will 39 create two routines, <code class="function"> probe </code> and <code class="function"> remove 40 </code>. At <code class="function">probe</code>: 41 <div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: disc; "><li class="listitem">call <code class="function"><a class="link" href="API-iio-device-alloc.html" title="iio_device_alloc">iio_device_alloc</a></code>, which allocates memory 42 for an IIO device. 43 </li><li class="listitem"> initialize IIO device fields with driver specific information 44 (e.g. device name, device channels). 45 </li><li class="listitem">call <code class="function"><a class="link" href="API-iio-device-register.html" title="iio_device_register"> iio_device_register</a></code>, this registers the 46 device with the IIO core. After this call the device is ready to accept 47 requests from user space applications. 48 </li></ul></div> 49 At <code class="function">remove</code>, we free the resources allocated in 50 <code class="function">probe</code> in reverse order: 51 <div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: disc; "><li class="listitem"><code class="function"><a class="link" href="API-iio-device-unregister.html" title="iio_device_unregister">iio_device_unregister</a></code>, unregister the device 52 from the IIO core. 53 </li><li class="listitem"><code class="function"><a class="link" href="API-iio-device-free.html" title="iio_device_free">iio_device_free</a></code>, free the memory allocated 54 for the IIO device. 55 </li></ul></div><div class="sect2"><div class="titlepage"><div><div><h3 class="title"><a name="iioattr"></a> IIO device sysfs interface </h3></div></div></div><p> 56 Attributes are sysfs files used to expose chip info and also allowing 57 applications to set various configuration parameters. For device 58 with index X, attributes can be found under 59 <code class="filename">/sys/bus/iio/iio:deviceX/ </code> directory. 60 Common attributes are: 61 </p><div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: disc; "><li class="listitem"><code class="filename">name</code>, description of the physical 62 chip. 63 </li><li class="listitem"><code class="filename">dev</code>, shows the major:minor pair 64 associated with <code class="filename">/dev/iio:deviceX</code> node. 65 </li><li class="listitem"><code class="filename">sampling_frequency_available</code>, 66 available discrete set of sampling frequency values for 67 device. 68 </li></ul></div><p> 69 Available standard attributes for IIO devices are described in the 70 <code class="filename">Documentation/ABI/testing/sysfs-bus-iio </code> file 71 in the Linux kernel sources. 72 </p></div><div class="sect2"><div class="titlepage"><div><div><h3 class="title"><a name="iiochannel"></a> IIO device channels </h3></div></div></div><div class="toc"><dl class="toc"><dt><span class="refentrytitle"><a href="API-struct-iio-chan-spec.html"><span class="phrase">struct iio_chan_spec</span></a></span><span class="refpurpose"> — 73 specification of a single channel 74 </span></dt></dl></div><p> 75 An IIO device channel is a representation of a data channel. An 76 IIO device can have one or multiple channels. For example: 77 </p><div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: disc; "><li class="listitem"> 78 a thermometer sensor has one channel representing the 79 temperature measurement. 80 </li><li class="listitem"> 81 a light sensor with two channels indicating the measurements in 82 the visible and infrared spectrum. 83 </li><li class="listitem"> 84 an accelerometer can have up to 3 channels representing 85 acceleration on X, Y and Z axes. 86 </li></ul></div><p> 87 An IIO channel is described by the <span class="type"> struct iio_chan_spec 88 </span>. A thermometer driver for the temperature sensor in the 89 example above would have to describe its channel as follows: 90 </p><pre class="programlisting"> 91 static const struct iio_chan_spec temp_channel[] = { 92 { 93 .type = IIO_TEMP, 94 .info_mask_separate = BIT(IIO_CHAN_INFO_PROCESSED), 95 }, 96 }; 97 98 </pre><p> 99 Channel sysfs attributes exposed to userspace are specified in 100 the form of <span class="emphasis"><em>bitmasks</em></span>. Depending on their 101 shared info, attributes can be set in one of the following masks: 102 </p><div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: disc; "><li class="listitem"><span class="emphasis"><em>info_mask_separate</em></span>, attributes will 103 be specific to this channel</li><li class="listitem"><span class="emphasis"><em>info_mask_shared_by_type</em></span>, 104 attributes are shared by all channels of the same type</li><li class="listitem"><span class="emphasis"><em>info_mask_shared_by_dir</em></span>, attributes 105 are shared by all channels of the same direction </li><li class="listitem"><span class="emphasis"><em>info_mask_shared_by_all</em></span>, 106 attributes are shared by all channels</li></ul></div><p> 107 When there are multiple data channels per channel type we have two 108 ways to distinguish between them: 109 </p><div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: disc; "><li class="listitem"> set <span class="emphasis"><em> .modified</em></span> field of <span class="type"> 110 iio_chan_spec</span> to 1. Modifiers are specified using 111 <span class="emphasis"><em>.channel2</em></span> field of the same 112 <span class="type">iio_chan_spec</span> structure and are used to indicate a 113 physically unique characteristic of the channel such as its direction 114 or spectral response. For example, a light sensor can have two channels, 115 one for infrared light and one for both infrared and visible light. 116 </li><li class="listitem"> set <span class="emphasis"><em>.indexed </em></span> field of 117 <span class="type">iio_chan_spec</span> to 1. In this case the channel is 118 simply another instance with an index specified by the 119 <span class="emphasis"><em>.channel</em></span> field. 120 </li></ul></div><p> 121 Here is how we can make use of the channel's modifiers: 122 </p><pre class="programlisting"> 123 static const struct iio_chan_spec light_channels[] = { 124 { 125 .type = IIO_INTENSITY, 126 .modified = 1, 127 .channel2 = IIO_MOD_LIGHT_IR, 128 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), 129 .info_mask_shared = BIT(IIO_CHAN_INFO_SAMP_FREQ), 130 }, 131 { 132 .type = IIO_INTENSITY, 133 .modified = 1, 134 .channel2 = IIO_MOD_LIGHT_BOTH, 135 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), 136 .info_mask_shared = BIT(IIO_CHAN_INFO_SAMP_FREQ), 137 }, 138 { 139 .type = IIO_LIGHT, 140 .info_mask_separate = BIT(IIO_CHAN_INFO_PROCESSED), 141 .info_mask_shared = BIT(IIO_CHAN_INFO_SAMP_FREQ), 142 }, 143 144 } 145 </pre><p> 146 This channel's definition will generate two separate sysfs files 147 for raw data retrieval: 148 </p><div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: disc; "><li class="listitem"><code class="filename">/sys/bus/iio/iio:deviceX/in_intensity_ir_raw</code></li><li class="listitem"><code class="filename">/sys/bus/iio/iio:deviceX/in_intensity_both_raw</code></li></ul></div><p> 149 one file for processed data: 150 </p><div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: disc; "><li class="listitem"><code class="filename">/sys/bus/iio/iio:deviceX/in_illuminance_input 151 </code></li></ul></div><p> 152 and one shared sysfs file for sampling frequency: 153 </p><div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: disc; "><li class="listitem"><code class="filename">/sys/bus/iio/iio:deviceX/sampling_frequency. 154 </code></li></ul></div><p> 155 </p><p> 156 Here is how we can make use of the channel's indexing: 157 </p><pre class="programlisting"> 158 static const struct iio_chan_spec light_channels[] = { 159 { 160 .type = IIO_VOLTAGE, 161 .indexed = 1, 162 .channel = 0, 163 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), 164 }, 165 { 166 .type = IIO_VOLTAGE, 167 .indexed = 1, 168 .channel = 1, 169 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), 170 }, 171 } 172 </pre><p> 173 This will generate two separate attributes files for raw data 174 retrieval: 175 </p><div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: disc; "><li class="listitem"><code class="filename">/sys/bus/iio/devices/iio:deviceX/in_voltage0_raw</code>, 176 representing voltage measurement for channel 0. 177 </li><li class="listitem"><code class="filename">/sys/bus/iio/devices/iio:deviceX/in_voltage1_raw</code>, 178 representing voltage measurement for channel 1. 179 </li></ul></div><p> 180 </p></div></div></div><div class="navfooter"><hr><table width="100%" summary="Navigation footer"><tr><td width="40%" align="left"><a accesskey="p" href="intro.html">Prev</a> </td><td width="20%" align="center"> </td><td width="40%" align="right"> <a accesskey="n" href="API-struct-iio-dev.html">Next</a></td></tr><tr><td width="40%" align="left" valign="top">Chapter 1. Introduction </td><td width="20%" align="center"><a accesskey="h" href="index.html">Home</a></td><td width="40%" align="right" valign="top"> <span class="phrase">struct iio_dev</span></td></tr></table></div></body></html> 181