Skip to content

Day33 - Minimal IIO Driver (Single Channel, Direct Mode)

Overview

This day focuses on implementing a minimal IIO driver to understand:

  • How IIO devices are registered
  • How sysfs attributes are generated automatically
  • How user-space maps to driver callbacks

The driver is intentionally simple:

  • Single channel (voltage0)
  • No buffer
  • No trigger
  • Only read_raw() supported

Key Concepts

1. IIO Device Lifecycle

module load
platform_device created
platform_driver probe()
iio_device_register()
/sys/bus/iio/devices/iio:deviceX appears

Important

  • An IIO device is NOT created at module load
  • It is created inside probe()
  • A matching device must exist for probe to run

2. struct iio_dev

struct iio_dev

Represents an IIO device instance.

Similar to:

  • struct mygpio_dev
  • struct mysensor_data

But managed by the IIO framework.


3. Channel Definition (iio_chan_spec)

.type = IIO_VOLTAGE
.indexed = 1
.channel = 0

This directly generates:

in_voltage0_raw
in_voltage0_scale

Key Insight

sysfs attribute names are derived from channel metadata, not manually created.


4. info_mask

.info_mask_separate =
    BIT(IIO_CHAN_INFO_RAW) |
    BIT(IIO_CHAN_INFO_SCALE);

Controls which attributes appear in sysfs.

Flag Result
RAW in_voltage0_raw
SCALE in_voltage0_scale

5. read_raw()

.read_raw = myiio_read_raw

Handles all data access.

Mapping:

sysfs mask
in_voltage0_raw IIO_CHAN_INFO_RAW
in_voltage0_scale IIO_CHAN_INFO_SCALE

6. Return Value Format

Example:

return IIO_VAL_INT;
return IIO_VAL_INT_PLUS_MICRO;

Meaning:

INT → val
INT_PLUS_MICRO → val + val2 * 1e-6

Example:

val = 0
val2 = 1000
 0.001

Data Flow

sysfs read

cat in_voltage0_raw
IIO core
read_raw(mask = RAW)
driver returns value

Comparison with Previous Work

vs hwmon

hwmon IIO
Manual sysfs Auto-generated sysfs
show() functions read_raw()
Fixed attributes Channel-driven attributes

vs Character Driver

Char driver IIO
read/write read_raw
protocol-based measurement-based
user-defined format framework-defined format

vs TTY

TTY IIO
byte stream structured data
flip buffer IIO buffer (future)
RX-driven trigger-driven

Limitations of Current Implementation

This minimal driver:

  • Does NOT support buffer
  • Does NOT support trigger
  • Does NOT support multi-channel

It behaves like:

single-shot sensor (similar to hwmon)

Key Takeaways

  1. IIO sysfs is automatically generated from channel metadata
  2. read_raw() is the central data path
  3. Device must exist before probe runs
  4. IIO abstraction is higher than hwmon

Next Step (Day34)

  • Multi-channel support
  • Deeper dive into iio_chan_spec
  • Prepare for buffer / scan elements