iio.tmpl 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697
  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" []>
  4. <book id="iioid">
  5. <bookinfo>
  6. <title>Industrial I/O driver developer's guide </title>
  7. <authorgroup>
  8. <author>
  9. <firstname>Daniel</firstname>
  10. <surname>Baluta</surname>
  11. <affiliation>
  12. <address>
  13. <email>daniel.baluta@intel.com</email>
  14. </address>
  15. </affiliation>
  16. </author>
  17. </authorgroup>
  18. <copyright>
  19. <year>2015</year>
  20. <holder>Intel Corporation</holder>
  21. </copyright>
  22. <legalnotice>
  23. <para>
  24. This documentation is free software; you can redistribute
  25. it and/or modify it under the terms of the GNU General Public
  26. License version 2.
  27. </para>
  28. </legalnotice>
  29. </bookinfo>
  30. <toc></toc>
  31. <chapter id="intro">
  32. <title>Introduction</title>
  33. <para>
  34. The main purpose of the Industrial I/O subsystem (IIO) is to provide
  35. support for devices that in some sense perform either analog-to-digital
  36. conversion (ADC) or digital-to-analog conversion (DAC) or both. The aim
  37. is to fill the gap between the somewhat similar hwmon and input
  38. subsystems.
  39. Hwmon is directed at low sample rate sensors used to monitor and
  40. control the system itself, like fan speed control or temperature
  41. measurement. Input is, as its name suggests, focused on human interaction
  42. input devices (keyboard, mouse, touchscreen). In some cases there is
  43. considerable overlap between these and IIO.
  44. </para>
  45. <para>
  46. Devices that fall into this category include:
  47. <itemizedlist>
  48. <listitem>
  49. analog to digital converters (ADCs)
  50. </listitem>
  51. <listitem>
  52. accelerometers
  53. </listitem>
  54. <listitem>
  55. capacitance to digital converters (CDCs)
  56. </listitem>
  57. <listitem>
  58. digital to analog converters (DACs)
  59. </listitem>
  60. <listitem>
  61. gyroscopes
  62. </listitem>
  63. <listitem>
  64. inertial measurement units (IMUs)
  65. </listitem>
  66. <listitem>
  67. color and light sensors
  68. </listitem>
  69. <listitem>
  70. magnetometers
  71. </listitem>
  72. <listitem>
  73. pressure sensors
  74. </listitem>
  75. <listitem>
  76. proximity sensors
  77. </listitem>
  78. <listitem>
  79. temperature sensors
  80. </listitem>
  81. </itemizedlist>
  82. Usually these sensors are connected via SPI or I2C. A common use case of the
  83. sensors devices is to have combined functionality (e.g. light plus proximity
  84. sensor).
  85. </para>
  86. </chapter>
  87. <chapter id='iiosubsys'>
  88. <title>Industrial I/O core</title>
  89. <para>
  90. The Industrial I/O core offers:
  91. <itemizedlist>
  92. <listitem>
  93. a unified framework for writing drivers for many different types of
  94. embedded sensors.
  95. </listitem>
  96. <listitem>
  97. a standard interface to user space applications manipulating sensors.
  98. </listitem>
  99. </itemizedlist>
  100. The implementation can be found under <filename>
  101. drivers/iio/industrialio-*</filename>
  102. </para>
  103. <sect1 id="iiodevice">
  104. <title> Industrial I/O devices </title>
  105. !Finclude/linux/iio/iio.h iio_dev
  106. !Fdrivers/iio/industrialio-core.c iio_device_alloc
  107. !Fdrivers/iio/industrialio-core.c iio_device_free
  108. !Fdrivers/iio/industrialio-core.c iio_device_register
  109. !Fdrivers/iio/industrialio-core.c iio_device_unregister
  110. <para>
  111. An IIO device usually corresponds to a single hardware sensor and it
  112. provides all the information needed by a driver handling a device.
  113. Let's first have a look at the functionality embedded in an IIO
  114. device then we will show how a device driver makes use of an IIO
  115. device.
  116. </para>
  117. <para>
  118. There are two ways for a user space application to interact
  119. with an IIO driver.
  120. <itemizedlist>
  121. <listitem>
  122. <filename>/sys/bus/iio/iio:deviceX/</filename>, this
  123. represents a hardware sensor and groups together the data
  124. channels of the same chip.
  125. </listitem>
  126. <listitem>
  127. <filename>/dev/iio:deviceX</filename>, character device node
  128. interface used for buffered data transfer and for events information
  129. retrieval.
  130. </listitem>
  131. </itemizedlist>
  132. </para>
  133. A typical IIO driver will register itself as an I2C or SPI driver and will
  134. create two routines, <function> probe </function> and <function> remove
  135. </function>. At <function>probe</function>:
  136. <itemizedlist>
  137. <listitem>call <function>iio_device_alloc</function>, which allocates memory
  138. for an IIO device.
  139. </listitem>
  140. <listitem> initialize IIO device fields with driver specific information
  141. (e.g. device name, device channels).
  142. </listitem>
  143. <listitem>call <function> iio_device_register</function>, this registers the
  144. device with the IIO core. After this call the device is ready to accept
  145. requests from user space applications.
  146. </listitem>
  147. </itemizedlist>
  148. At <function>remove</function>, we free the resources allocated in
  149. <function>probe</function> in reverse order:
  150. <itemizedlist>
  151. <listitem><function>iio_device_unregister</function>, unregister the device
  152. from the IIO core.
  153. </listitem>
  154. <listitem><function>iio_device_free</function>, free the memory allocated
  155. for the IIO device.
  156. </listitem>
  157. </itemizedlist>
  158. <sect2 id="iioattr"> <title> IIO device sysfs interface </title>
  159. <para>
  160. Attributes are sysfs files used to expose chip info and also allowing
  161. applications to set various configuration parameters. For device
  162. with index X, attributes can be found under
  163. <filename>/sys/bus/iio/iio:deviceX/ </filename> directory.
  164. Common attributes are:
  165. <itemizedlist>
  166. <listitem><filename>name</filename>, description of the physical
  167. chip.
  168. </listitem>
  169. <listitem><filename>dev</filename>, shows the major:minor pair
  170. associated with <filename>/dev/iio:deviceX</filename> node.
  171. </listitem>
  172. <listitem><filename>sampling_frequency_available</filename>,
  173. available discrete set of sampling frequency values for
  174. device.
  175. </listitem>
  176. </itemizedlist>
  177. Available standard attributes for IIO devices are described in the
  178. <filename>Documentation/ABI/testing/sysfs-bus-iio </filename> file
  179. in the Linux kernel sources.
  180. </para>
  181. </sect2>
  182. <sect2 id="iiochannel"> <title> IIO device channels </title>
  183. !Finclude/linux/iio/iio.h iio_chan_spec structure.
  184. <para>
  185. An IIO device channel is a representation of a data channel. An
  186. IIO device can have one or multiple channels. For example:
  187. <itemizedlist>
  188. <listitem>
  189. a thermometer sensor has one channel representing the
  190. temperature measurement.
  191. </listitem>
  192. <listitem>
  193. a light sensor with two channels indicating the measurements in
  194. the visible and infrared spectrum.
  195. </listitem>
  196. <listitem>
  197. an accelerometer can have up to 3 channels representing
  198. acceleration on X, Y and Z axes.
  199. </listitem>
  200. </itemizedlist>
  201. An IIO channel is described by the <type> struct iio_chan_spec
  202. </type>. A thermometer driver for the temperature sensor in the
  203. example above would have to describe its channel as follows:
  204. <programlisting>
  205. static const struct iio_chan_spec temp_channel[] = {
  206. {
  207. .type = IIO_TEMP,
  208. .info_mask_separate = BIT(IIO_CHAN_INFO_PROCESSED),
  209. },
  210. };
  211. </programlisting>
  212. Channel sysfs attributes exposed to userspace are specified in
  213. the form of <emphasis>bitmasks</emphasis>. Depending on their
  214. shared info, attributes can be set in one of the following masks:
  215. <itemizedlist>
  216. <listitem><emphasis>info_mask_separate</emphasis>, attributes will
  217. be specific to this channel</listitem>
  218. <listitem><emphasis>info_mask_shared_by_type</emphasis>,
  219. attributes are shared by all channels of the same type</listitem>
  220. <listitem><emphasis>info_mask_shared_by_dir</emphasis>, attributes
  221. are shared by all channels of the same direction </listitem>
  222. <listitem><emphasis>info_mask_shared_by_all</emphasis>,
  223. attributes are shared by all channels</listitem>
  224. </itemizedlist>
  225. When there are multiple data channels per channel type we have two
  226. ways to distinguish between them:
  227. <itemizedlist>
  228. <listitem> set <emphasis> .modified</emphasis> field of <type>
  229. iio_chan_spec</type> to 1. Modifiers are specified using
  230. <emphasis>.channel2</emphasis> field of the same
  231. <type>iio_chan_spec</type> structure and are used to indicate a
  232. physically unique characteristic of the channel such as its direction
  233. or spectral response. For example, a light sensor can have two channels,
  234. one for infrared light and one for both infrared and visible light.
  235. </listitem>
  236. <listitem> set <emphasis>.indexed </emphasis> field of
  237. <type>iio_chan_spec</type> to 1. In this case the channel is
  238. simply another instance with an index specified by the
  239. <emphasis>.channel</emphasis> field.
  240. </listitem>
  241. </itemizedlist>
  242. Here is how we can make use of the channel's modifiers:
  243. <programlisting>
  244. static const struct iio_chan_spec light_channels[] = {
  245. {
  246. .type = IIO_INTENSITY,
  247. .modified = 1,
  248. .channel2 = IIO_MOD_LIGHT_IR,
  249. .info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
  250. .info_mask_shared = BIT(IIO_CHAN_INFO_SAMP_FREQ),
  251. },
  252. {
  253. .type = IIO_INTENSITY,
  254. .modified = 1,
  255. .channel2 = IIO_MOD_LIGHT_BOTH,
  256. .info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
  257. .info_mask_shared = BIT(IIO_CHAN_INFO_SAMP_FREQ),
  258. },
  259. {
  260. .type = IIO_LIGHT,
  261. .info_mask_separate = BIT(IIO_CHAN_INFO_PROCESSED),
  262. .info_mask_shared = BIT(IIO_CHAN_INFO_SAMP_FREQ),
  263. },
  264. }
  265. </programlisting>
  266. This channel's definition will generate two separate sysfs files
  267. for raw data retrieval:
  268. <itemizedlist>
  269. <listitem>
  270. <filename>/sys/bus/iio/iio:deviceX/in_intensity_ir_raw</filename>
  271. </listitem>
  272. <listitem>
  273. <filename>/sys/bus/iio/iio:deviceX/in_intensity_both_raw</filename>
  274. </listitem>
  275. </itemizedlist>
  276. one file for processed data:
  277. <itemizedlist>
  278. <listitem>
  279. <filename>/sys/bus/iio/iio:deviceX/in_illuminance_input
  280. </filename>
  281. </listitem>
  282. </itemizedlist>
  283. and one shared sysfs file for sampling frequency:
  284. <itemizedlist>
  285. <listitem>
  286. <filename>/sys/bus/iio/iio:deviceX/sampling_frequency.
  287. </filename>
  288. </listitem>
  289. </itemizedlist>
  290. </para>
  291. <para>
  292. Here is how we can make use of the channel's indexing:
  293. <programlisting>
  294. static const struct iio_chan_spec light_channels[] = {
  295. {
  296. .type = IIO_VOLTAGE,
  297. .indexed = 1,
  298. .channel = 0,
  299. .info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
  300. },
  301. {
  302. .type = IIO_VOLTAGE,
  303. .indexed = 1,
  304. .channel = 1,
  305. .info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
  306. },
  307. }
  308. </programlisting>
  309. This will generate two separate attributes files for raw data
  310. retrieval:
  311. <itemizedlist>
  312. <listitem>
  313. <filename>/sys/bus/iio/devices/iio:deviceX/in_voltage0_raw</filename>,
  314. representing voltage measurement for channel 0.
  315. </listitem>
  316. <listitem>
  317. <filename>/sys/bus/iio/devices/iio:deviceX/in_voltage1_raw</filename>,
  318. representing voltage measurement for channel 1.
  319. </listitem>
  320. </itemizedlist>
  321. </para>
  322. </sect2>
  323. </sect1>
  324. <sect1 id="iiobuffer"> <title> Industrial I/O buffers </title>
  325. !Finclude/linux/iio/buffer.h iio_buffer
  326. !Edrivers/iio/industrialio-buffer.c
  327. <para>
  328. The Industrial I/O core offers a way for continuous data capture
  329. based on a trigger source. Multiple data channels can be read at once
  330. from <filename>/dev/iio:deviceX</filename> character device node,
  331. thus reducing the CPU load.
  332. </para>
  333. <sect2 id="iiobuffersysfs">
  334. <title>IIO buffer sysfs interface </title>
  335. <para>
  336. An IIO buffer has an associated attributes directory under <filename>
  337. /sys/bus/iio/iio:deviceX/buffer/</filename>. Here are the existing
  338. attributes:
  339. <itemizedlist>
  340. <listitem>
  341. <emphasis>length</emphasis>, the total number of data samples
  342. (capacity) that can be stored by the buffer.
  343. </listitem>
  344. <listitem>
  345. <emphasis>enable</emphasis>, activate buffer capture.
  346. </listitem>
  347. </itemizedlist>
  348. </para>
  349. </sect2>
  350. <sect2 id="iiobuffersetup"> <title> IIO buffer setup </title>
  351. <para>The meta information associated with a channel reading
  352. placed in a buffer is called a <emphasis> scan element </emphasis>.
  353. The important bits configuring scan elements are exposed to
  354. userspace applications via the <filename>
  355. /sys/bus/iio/iio:deviceX/scan_elements/</filename> directory. This
  356. file contains attributes of the following form:
  357. <itemizedlist>
  358. <listitem><emphasis>enable</emphasis>, used for enabling a channel.
  359. If and only if its attribute is non zero, then a triggered capture
  360. will contain data samples for this channel.
  361. </listitem>
  362. <listitem><emphasis>type</emphasis>, description of the scan element
  363. data storage within the buffer and hence the form in which it is
  364. read from user space. Format is <emphasis>
  365. [be|le]:[s|u]bits/storagebitsXrepeat[>>shift] </emphasis>.
  366. <itemizedlist>
  367. <listitem> <emphasis>be</emphasis> or <emphasis>le</emphasis>, specifies
  368. big or little endian.
  369. </listitem>
  370. <listitem>
  371. <emphasis>s </emphasis>or <emphasis>u</emphasis>, specifies if
  372. signed (2's complement) or unsigned.
  373. </listitem>
  374. <listitem><emphasis>bits</emphasis>, is the number of valid data
  375. bits.
  376. </listitem>
  377. <listitem><emphasis>storagebits</emphasis>, is the number of bits
  378. (after padding) that it occupies in the buffer.
  379. </listitem>
  380. <listitem>
  381. <emphasis>shift</emphasis>, if specified, is the shift that needs
  382. to be applied prior to masking out unused bits.
  383. </listitem>
  384. <listitem>
  385. <emphasis>repeat</emphasis>, specifies the number of bits/storagebits
  386. repetitions. When the repeat element is 0 or 1, then the repeat
  387. value is omitted.
  388. </listitem>
  389. </itemizedlist>
  390. </listitem>
  391. </itemizedlist>
  392. For example, a driver for a 3-axis accelerometer with 12 bit
  393. resolution where data is stored in two 8-bits registers as
  394. follows:
  395. <programlisting>
  396. 7 6 5 4 3 2 1 0
  397. +---+---+---+---+---+---+---+---+
  398. |D3 |D2 |D1 |D0 | X | X | X | X | (LOW byte, address 0x06)
  399. +---+---+---+---+---+---+---+---+
  400. 7 6 5 4 3 2 1 0
  401. +---+---+---+---+---+---+---+---+
  402. |D11|D10|D9 |D8 |D7 |D6 |D5 |D4 | (HIGH byte, address 0x07)
  403. +---+---+---+---+---+---+---+---+
  404. </programlisting>
  405. will have the following scan element type for each axis:
  406. <programlisting>
  407. $ cat /sys/bus/iio/devices/iio:device0/scan_elements/in_accel_y_type
  408. le:s12/16>>4
  409. </programlisting>
  410. A user space application will interpret data samples read from the
  411. buffer as two byte little endian signed data, that needs a 4 bits
  412. right shift before masking out the 12 valid bits of data.
  413. </para>
  414. <para>
  415. For implementing buffer support a driver should initialize the following
  416. fields in <type>iio_chan_spec</type> definition:
  417. <programlisting>
  418. struct iio_chan_spec {
  419. /* other members */
  420. int scan_index
  421. struct {
  422. char sign;
  423. u8 realbits;
  424. u8 storagebits;
  425. u8 shift;
  426. u8 repeat;
  427. enum iio_endian endianness;
  428. } scan_type;
  429. };
  430. </programlisting>
  431. The driver implementing the accelerometer described above will
  432. have the following channel definition:
  433. <programlisting>
  434. struct struct iio_chan_spec accel_channels[] = {
  435. {
  436. .type = IIO_ACCEL,
  437. .modified = 1,
  438. .channel2 = IIO_MOD_X,
  439. /* other stuff here */
  440. .scan_index = 0,
  441. .scan_type = {
  442. .sign = 's',
  443. .realbits = 12,
  444. .storgebits = 16,
  445. .shift = 4,
  446. .endianness = IIO_LE,
  447. },
  448. }
  449. /* similar for Y (with channel2 = IIO_MOD_Y, scan_index = 1)
  450. * and Z (with channel2 = IIO_MOD_Z, scan_index = 2) axis
  451. */
  452. }
  453. </programlisting>
  454. </para>
  455. <para>
  456. Here <emphasis> scan_index </emphasis> defines the order in which
  457. the enabled channels are placed inside the buffer. Channels with a lower
  458. scan_index will be placed before channels with a higher index. Each
  459. channel needs to have a unique scan_index.
  460. </para>
  461. <para>
  462. Setting scan_index to -1 can be used to indicate that the specific
  463. channel does not support buffered capture. In this case no entries will
  464. be created for the channel in the scan_elements directory.
  465. </para>
  466. </sect2>
  467. </sect1>
  468. <sect1 id="iiotrigger"> <title> Industrial I/O triggers </title>
  469. !Finclude/linux/iio/trigger.h iio_trigger
  470. !Edrivers/iio/industrialio-trigger.c
  471. <para>
  472. In many situations it is useful for a driver to be able to
  473. capture data based on some external event (trigger) as opposed
  474. to periodically polling for data. An IIO trigger can be provided
  475. by a device driver that also has an IIO device based on hardware
  476. generated events (e.g. data ready or threshold exceeded) or
  477. provided by a separate driver from an independent interrupt
  478. source (e.g. GPIO line connected to some external system, timer
  479. interrupt or user space writing a specific file in sysfs). A
  480. trigger may initiate data capture for a number of sensors and
  481. also it may be completely unrelated to the sensor itself.
  482. </para>
  483. <sect2 id="iiotrigsysfs"> <title> IIO trigger sysfs interface </title>
  484. There are two locations in sysfs related to triggers:
  485. <itemizedlist>
  486. <listitem><filename>/sys/bus/iio/devices/triggerY</filename>,
  487. this file is created once an IIO trigger is registered with
  488. the IIO core and corresponds to trigger with index Y. Because
  489. triggers can be very different depending on type there are few
  490. standard attributes that we can describe here:
  491. <itemizedlist>
  492. <listitem>
  493. <emphasis>name</emphasis>, trigger name that can be later
  494. used for association with a device.
  495. </listitem>
  496. <listitem>
  497. <emphasis>sampling_frequency</emphasis>, some timer based
  498. triggers use this attribute to specify the frequency for
  499. trigger calls.
  500. </listitem>
  501. </itemizedlist>
  502. </listitem>
  503. <listitem>
  504. <filename>/sys/bus/iio/devices/iio:deviceX/trigger/</filename>, this
  505. directory is created once the device supports a triggered
  506. buffer. We can associate a trigger with our device by writing
  507. the trigger's name in the <filename>current_trigger</filename> file.
  508. </listitem>
  509. </itemizedlist>
  510. </sect2>
  511. <sect2 id="iiotrigattr"> <title> IIO trigger setup</title>
  512. <para>
  513. Let's see a simple example of how to setup a trigger to be used
  514. by a driver.
  515. <programlisting>
  516. struct iio_trigger_ops trigger_ops = {
  517. .set_trigger_state = sample_trigger_state,
  518. .validate_device = sample_validate_device,
  519. }
  520. struct iio_trigger *trig;
  521. /* first, allocate memory for our trigger */
  522. trig = iio_trigger_alloc(dev, "trig-%s-%d", name, idx);
  523. /* setup trigger operations field */
  524. trig->ops = &amp;trigger_ops;
  525. /* now register the trigger with the IIO core */
  526. iio_trigger_register(trig);
  527. </programlisting>
  528. </para>
  529. </sect2>
  530. <sect2 id="iiotrigsetup"> <title> IIO trigger ops</title>
  531. !Finclude/linux/iio/trigger.h iio_trigger_ops
  532. <para>
  533. Notice that a trigger has a set of operations attached:
  534. <itemizedlist>
  535. <listitem>
  536. <function>set_trigger_state</function>, switch the trigger on/off
  537. on demand.
  538. </listitem>
  539. <listitem>
  540. <function>validate_device</function>, function to validate the
  541. device when the current trigger gets changed.
  542. </listitem>
  543. </itemizedlist>
  544. </para>
  545. </sect2>
  546. </sect1>
  547. <sect1 id="iiotriggered_buffer">
  548. <title> Industrial I/O triggered buffers </title>
  549. <para>
  550. Now that we know what buffers and triggers are let's see how they
  551. work together.
  552. </para>
  553. <sect2 id="iiotrigbufsetup"> <title> IIO triggered buffer setup</title>
  554. !Edrivers/iio/buffer/industrialio-triggered-buffer.c
  555. !Finclude/linux/iio/iio.h iio_buffer_setup_ops
  556. <para>
  557. A typical triggered buffer setup looks like this:
  558. <programlisting>
  559. const struct iio_buffer_setup_ops sensor_buffer_setup_ops = {
  560. .preenable = sensor_buffer_preenable,
  561. .postenable = sensor_buffer_postenable,
  562. .postdisable = sensor_buffer_postdisable,
  563. .predisable = sensor_buffer_predisable,
  564. };
  565. irqreturn_t sensor_iio_pollfunc(int irq, void *p)
  566. {
  567. pf->timestamp = iio_get_time_ns();
  568. return IRQ_WAKE_THREAD;
  569. }
  570. irqreturn_t sensor_trigger_handler(int irq, void *p)
  571. {
  572. u16 buf[8];
  573. int i = 0;
  574. /* read data for each active channel */
  575. for_each_set_bit(bit, active_scan_mask, masklength)
  576. buf[i++] = sensor_get_data(bit)
  577. iio_push_to_buffers_with_timestamp(indio_dev, buf, timestamp);
  578. iio_trigger_notify_done(trigger);
  579. return IRQ_HANDLED;
  580. }
  581. /* setup triggered buffer, usually in probe function */
  582. iio_triggered_buffer_setup(indio_dev, sensor_iio_polfunc,
  583. sensor_trigger_handler,
  584. sensor_buffer_setup_ops);
  585. </programlisting>
  586. </para>
  587. The important things to notice here are:
  588. <itemizedlist>
  589. <listitem><function> iio_buffer_setup_ops</function>, the buffer setup
  590. functions to be called at predefined points in the buffer configuration
  591. sequence (e.g. before enable, after disable). If not specified, the
  592. IIO core uses the default <type>iio_triggered_buffer_setup_ops</type>.
  593. </listitem>
  594. <listitem><function>sensor_iio_pollfunc</function>, the function that
  595. will be used as top half of poll function. It should do as little
  596. processing as possible, because it runs in interrupt context. The most
  597. common operation is recording of the current timestamp and for this reason
  598. one can use the IIO core defined <function>iio_pollfunc_store_time
  599. </function> function.
  600. </listitem>
  601. <listitem><function>sensor_trigger_handler</function>, the function that
  602. will be used as bottom half of the poll function. This runs in the
  603. context of a kernel thread and all the processing takes place here.
  604. It usually reads data from the device and stores it in the internal
  605. buffer together with the timestamp recorded in the top half.
  606. </listitem>
  607. </itemizedlist>
  608. </sect2>
  609. </sect1>
  610. </chapter>
  611. <chapter id='iioresources'>
  612. <title> Resources </title>
  613. IIO core may change during time so the best documentation to read is the
  614. source code. There are several locations where you should look:
  615. <itemizedlist>
  616. <listitem>
  617. <filename>drivers/iio/</filename>, contains the IIO core plus
  618. and directories for each sensor type (e.g. accel, magnetometer,
  619. etc.)
  620. </listitem>
  621. <listitem>
  622. <filename>include/linux/iio/</filename>, contains the header
  623. files, nice to read for the internal kernel interfaces.
  624. </listitem>
  625. <listitem>
  626. <filename>include/uapi/linux/iio/</filename>, contains files to be
  627. used by user space applications.
  628. </listitem>
  629. <listitem>
  630. <filename>tools/iio/</filename>, contains tools for rapidly
  631. testing buffers, events and device creation.
  632. </listitem>
  633. <listitem>
  634. <filename>drivers/staging/iio/</filename>, contains code for some
  635. drivers or experimental features that are not yet mature enough
  636. to be moved out.
  637. </listitem>
  638. </itemizedlist>
  639. <para>
  640. Besides the code, there are some good online documentation sources:
  641. <itemizedlist>
  642. <listitem>
  643. <ulink url="http://marc.info/?l=linux-iio"> Industrial I/O mailing
  644. list </ulink>
  645. </listitem>
  646. <listitem>
  647. <ulink url="http://wiki.analog.com/software/linux/docs/iio/iio">
  648. Analog Device IIO wiki page </ulink>
  649. </listitem>
  650. <listitem>
  651. <ulink url="https://fosdem.org/2015/schedule/event/iiosdr/">
  652. Using the Linux IIO framework for SDR, Lars-Peter Clausen's
  653. presentation at FOSDEM </ulink>
  654. </listitem>
  655. </itemizedlist>
  656. </para>
  657. </chapter>
  658. </book>
  659. <!--
  660. vim: softtabstop=2:shiftwidth=2:expandtab:textwidth=72
  661. -->