brintos

brintos / linux-shallow public Read only

0
0
Text · 6.8 KiB · dfe438d Raw
183 lines · plain
1=============2Core elements3=============4 5The Industrial I/O core offers both a unified framework for writing drivers for6many different types of embedded sensors and a standard interface to user space7applications manipulating sensors. The implementation can be found under8:file:`drivers/iio/industrialio-*`9 10Industrial I/O Devices11----------------------12 13* struct iio_dev - industrial I/O device14* iio_device_alloc() - allocate an :c:type:`iio_dev` from a driver15* iio_device_free() - free an :c:type:`iio_dev` from a driver16* iio_device_register() - register a device with the IIO subsystem17* iio_device_unregister() - unregister a device from the IIO18  subsystem19 20An IIO device usually corresponds to a single hardware sensor and it21provides all the information needed by a driver handling a device.22Let's first have a look at the functionality embedded in an IIO device23then 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/devices/iio:device{X}/`, this represents a hardware sensor28   and groups together the data channels of the same chip.292. :file:`/dev/iio:device{X}`, character device node interface used for30   buffered data transfer and for events information retrieval.31 32A typical IIO driver will register itself as an :doc:`I2C <../i2c>` or33: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 the41   IIO core. After this call the device is ready to accept requests from user42   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 interface50==========================51 52Attributes are sysfs files used to expose chip info and also allowing53applications to set various configuration parameters. For device with54index X, attributes can be found under /sys/bus/iio/devices/iio:deviceX/55directory.  Common attributes are:56 57* :file:`name`, description of the physical chip.58* :file:`dev`, shows the major:minor pair associated with59  :file:`/dev/iio:deviceX` node.60* :file:`sampling_frequency_available`, available discrete set of sampling61  frequency values for device.62* Available standard attributes for IIO devices are described in the63  :file:`Documentation/ABI/testing/sysfs-bus-iio` file in the Linux kernel64  sources.65 66IIO device channels67===================68 69struct iio_chan_spec - specification of a single channel70 71An IIO device channel is a representation of a data channel. An IIO device can72have 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 visible76  and infrared spectrum.77* an accelerometer can have up to 3 channels representing acceleration on X, Y78  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 would82have 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 of92bitmasks. Depending on their shared info, attributes can be set in one of the93following masks:94 95* **info_mask_separate**, attributes will be specific to96  this channel97* **info_mask_shared_by_type**, attributes are shared by all channels of the98  same type99* **info_mask_shared_by_dir**, attributes are shared by all channels of the same100  direction101* **info_mask_shared_by_all**, attributes are shared by all channels102 103When there are multiple data channels per channel type we have two ways to104distinguish between them:105 106* set **.modified** field of :c:type:`iio_chan_spec` to 1. Modifiers are107  specified using **.channel2** field of the same :c:type:`iio_chan_spec`108  structure and are used to indicate a physically unique characteristic of the109  channel such as its direction or spectral response. For example, a light110  sensor can have two channels, one for infrared light and one for both111  infrared and visible light.112* set **.indexed** field of :c:type:`iio_chan_spec` to 1. In this case the113  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 data141retrieval:142 143* :file:`/sys/bus/iio/devices/iio:device{X}/in_intensity_ir_raw`144* :file:`/sys/bus/iio/devices/iio:device{X}/in_intensity_both_raw`145 146one file for processed data:147 148* :file:`/sys/bus/iio/devices/iio:device{X}/in_illuminance_input`149 150and one shared sysfs file for sampling frequency:151 152* :file:`/sys/bus/iio/devices/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`, representing174  voltage measurement for channel 0.175* :file:`/sys/bus/iio/devices/iio:device{X}/in_voltage1_raw`, representing176  voltage measurement for channel 1.177 178More details179============180.. kernel-doc:: include/linux/iio/iio.h181.. kernel-doc:: drivers/iio/industrialio-core.c182   :export:183