
Recall first from The hwmon subsystem
- The hwmon core generates standard sysfs files from channel masks and visibility callbacks.
- Callback channels are zero-based, while temperature filenames start at temp1 and voltage filenames start at in0.
- Ordinary temperature readings are reported in millidegrees Celsius and voltage readings in millivolts.
Describe the measurement channels
2 min readhwmon from the last lesson gives a fixed set of attributes. This lesson answers the more general IIO framework, its channel spec and channel types, and how read_raw encodes values like IIO_VAL_INT.
Consider a program that needs the raw X-axis reading from an accelerometer. The sensor manual tells the driver which hardware register to read and how to interpret its bits. The program needs a Linux name that identifies the axis and the requested quantity. The Industrial I/O framework, usually called IIO, defines that naming through channel descriptions. The Linux IIO documentation shows how a modified acceleration channel identifies an axis and how the information masks define attributes such as raw readings and scale. Linux IIO core documentation
A measurement or output described by one struct iio_chan_spec entry has a .type that identifies the quantity. Use IIO_TEMP for temperature, IIO_VOLTAGE for voltage, and IIO_ACCEL for acceleration. For an input channel, these types produce the beginnings in_temp, in_voltage, and in_accel. The remaining fields say which input or axis the entry describes and which properties it exposes. A single device can describe several channels, including channels of different types.
For an indexed voltage input, set .indexed = 1 and put the logical input number in .channel. With .channel = 0, the channel begins in_voltage0. An unindexed, unmodified temperature input begins in_temp. For an X-axis acceleration input, set .modified = 1 and .channel2 = IIO_MOD_X. That gives in_accel_x. The channel number presented to user space can differ from the hardware register address. The driver can use .address to carry a hardware selector if it needs one.
The information masks declare what is available. Setting BIT(IIO_CHAN_INFO_RAW) in .info_mask_separate requests a separate _raw attribute for that channel. In the examples above, this gives in_voltage0_raw, in_temp_raw, or in_accel_x_raw. A driver can expose both raw readings and scale. If every acceleration axis on that device has the same scale, set BIT(IIO_CHAN_INFO_SCALE) in .info_mask_shared_by_type for those channels. That creates one in_accel_scale attribute for the acceleration type. With separate scale bits, each axis has its own scale attribute.
Decide which quantities the hardware really shares before choosing a mask. If one configuration field controls scale for all axes, a shared attribute describes that relationship. If each axis can have its own scale, separate attributes let the driver report the value for the requested axis. The raw bit belongs in the separate mask when each axis has its own reading. These choices determine what user space can ask for. The driver still has to implement each property it declares and return a value for the channel and property selected by the core.
Register the device
2 min readstruct iio_dev represents an instance of an IIO device. In the physical driver's probe function, allocate it with devm_iio_device_alloc(), passing the parent device and the number of bytes required for private state. Check for NULL and return -ENOMEM if allocation fails. Use iio_priv() to obtain the private state when you initialize it in probe or retrieve it from a callback. For example, that state can hold the register map the callback needs to read a sample.
Set indio_dev->name to the device name and indio_dev->info to the driver's struct iio_info table. The table contains .read_raw and, when the device supports suitable writable properties, .write_raw. Set indio_dev->channels to the channel table and indio_dev->num_channels to its number of entries. For a driver providing direct access, set indio_dev->modes = INDIO_DIRECT_MODE. The channel and callback tables must remain valid for the whole time the device is registered. Static constant tables are suitable when all instances use the same description.
Once the callbacks can run safely, call iio_device_register(indio_dev). It returns zero on success or a negative errno on failure. Propagate a registration failure from probe. If registration succeeds, pair it with iio_device_unregister() during teardown. Alternatively, register with devm_iio_device_register() and let device-managed cleanup handle unregistration. Managed allocation and managed registration are separate choices. Allocating with devm_iio_device_alloc() and registering with iio_device_register() still requires explicit unregistration after a successful registration.
This order matters because users can attempt to read the attributes once registration publishes them. If a callback depends on a register map or on the sensor being configured, establish those prerequisites before registering. Keep the hardware and callback state usable until the device has been unregistered. The channel table and callback table also have to agree. If a channel advertises raw and scale, its read callback must handle both requests.
After registration, the device appears under /sys/bus/iio/devices/ as iio:deviceN, with N assigned by the core. The name attribute identifies the device by the name the driver supplied. During bring-up, read name to find a candidate device directory, then inspect its channel attributes. Check the declared type, index or modifier, and information masks if the expected filename is missing. If the file exists but reading it fails, examine the callback path for that property and the hardware operation it performs. An Ubuntu VM or WSL2 instance may have no registered IIO devices to inspect.
Encode direct reads and writes
4 min readWhen a program reads in_voltage0_raw, the IIO core calls the driver's read_raw with the iio_dev, a pointer to the voltage channel, two output pointers named val and val2, and a long mask identifying the requested property. Here the mask argument is IIO_CHAN_INFO_RAW. It is the property selector itself. In the channel declaration, BIT(IIO_CHAN_INFO_RAW) is the bit used to advertise that selector. For in_voltage0_scale, if declared, the property selector is IIO_CHAN_INFO_SCALE. Both requests can reach the same function.
For a raw voltage reading, use the channel information to select the ADC input, get the conversion result, and check the hardware operation before using the sample. When the operation succeeds, store the sample in *val and return IIO_VAL_INT. If the hardware returns a negative errno, propagate that error. A signed measurement can also legitimately be negative. Decode it according to the sensor's sample format and put that signed result in *val, with IIO_VAL_INT as the return value. This keeps a negative sample distinguishable from a failed read. For an unsupported property, return -EINVAL.
read_raw can report a non-integer property such as scale. Choose the format according to the numeric representation your driver has available. The IIO core interprets the two output fields according to the return code and formats the result for sysfs.
IIO_VAL_INTuses*valalone. Set it to the integer sample or property value.*val2is unused for this format.IIO_VAL_INT_PLUS_MICROuses*valfor the whole units and*val2for the fractional magnitude in millionths. For the positive value0.125, set*val = 0and*val2 = 125000. For a negative magnitude smaller than one, such as-0.125, set*val = 0and*val2 = -125000. For-1.125, use*val = -1and*val2 = 125000.IIO_VAL_FRACTIONALuses*valas the numerator and*val2as the denominator. For the ratio1/8, set them to1and8. Use a nonzero denominator and keep it positive in driver code, putting any sign on the numerator. The core formats the ratio as decimal text.
The decimal values here illustrate arithmetic. On real hardware, the sample and any scale components come from the device and its documented behavior. Notice that the positive 0.125 and 1/8 use very different pairs of integers for the same quantity. If a driver fills the fractional pair and returns IIO_VAL_INT_PLUS_MICRO, the core will interpret it as one whole unit and eight millionths. Review an assignment to *val and *val2 together with the return statement that selects their meaning.
For an exposed property that supports writing, the IIO sysfs path can call write_raw. Its val and val2 arguments are integers passed by value. The core parses the text before calling the driver. For these standard attribute writes, the default input format is IIO_VAL_INT_PLUS_MICRO. The optional .write_raw_get_fmt callback can select another supported input format. For example, with the default format a positive input of 0.125 reaches write_raw as val = 0 and val2 = 125000. A read of the same property can still return IIO_VAL_FRACTIONAL. The read format and the write parser are selected independently.
Use mask to decide which property the write targets, and use the channel to identify the hardware it applies to. Check that the requested value corresponds to a setting the hardware supports. For example, for a device with a discrete set of scale settings, match the parsed value against that set before updating the register. Return -EINVAL for an unsupported property or setting and propagate any hardware error. After a successful update, return zero. The sysfs layer reports the bytes written to user space. The callback itself uses a status return.
IIO buffers and triggers follows this lesson by asking how a registered IIO device delivers a sequence of samples when an application needs more than individual attribute reads.
Key takeaways
- An iio_dev references a channel table and an iio_info callback table.
- Channel information masks declare which attributes are exposed and how they are shared.
- read_raw fills output integers and returns a format code or a negative errno.
- write_raw receives parsed integers and returns zero on success or a negative errno on failure.
Reflect
- Explain to a colleague, in two sentences: identify the device, channel, and callback structures used to register an IIO device.
- Which objective are you least sure about? Re-read that section, then do its exercise in the practice pack.
- What surprised you when you ran the commands yourself?
On a real board
Repeat the key commands on your Raspberry Pi or lab board over SSH or the serial console. Note anything that behaves differently from your PC, such as missing tools, a BusyBox shell or a read-only filesystem.
References and further reading
- Kernel documentationIndustrial I/O core: devices, channels, and registration
- Kernel documentationIndustrial I/O ABI: sysfs attributes for IIO devices
Official documentation first. Links checked when the lesson was published.