Skip to content

Day38 - IIO Sysfs Control Interface

🎯 Goal

Implement control plane for IIO driver using sysfs interface.

Replace module parameters with runtime configurable attributes:

  • sampling_frequency (standard IIO)
  • fifo_enable (driver private)
  • watermark (driver private)

Keep existing data path unchanged:

IRQ → trigger → pollfunc → FIFO read → unpack → iio_push_to_buffers


🧠 Design Overview

Control Plane vs Data Plane

Type Interface
Control sysfs (/sys/bus/iio/devices/iio:deviceX)
Data buffer device (/dev/iio:deviceX)

🧩 Attribute Design

1. sampling_frequency (Standard IIO)

  • Implemented via:
  • read_raw()
  • write_raw()
  • Declared in channel:
.info_mask_shared_by_all = BIT(IIO_CHAN_INFO_SAMP_FREQ)
  • Exposed as:
/sys/bus/iio/devices/iio:deviceX/sampling_frequency

2. fifo_enable (Private Attribute)

  • Controls:
  • MYADC_CONFIG_FIFO_ENABLE bit

  • Implemented via:

  • IIO_DEVICE_ATTR
  • show/store handler

  • Exposed as:

/sys/bus/iio/devices/iio:deviceX/fifo_enable

3. watermark (Private Attribute)

  • Controls FIFO watermark level
  • Only meaningful when FIFO is enabled

  • Exposed as:

/sys/bus/iio/devices/iio:deviceX/watermark

🔧 Helper Design

All hardware configuration is routed through helper functions:

myadc_set_sample_rate()
myadc_set_fifo_enable()
myadc_set_fifo_watermark()

Design Rules

  • Caller must hold data->lock
  • Write-through cache:
  • Update hardware
  • Then update driver state

🔒 Concurrency Model

  • All control paths use mutex
  • Helper functions assume locked context
mutex_lock(&data->lock);
myadc_set_xxx(...);
mutex_unlock(&data->lock);

🚫 Runtime Protection

Prevent configuration changes during streaming:

if (iio_buffer_enabled(indio_dev))
    return -EBUSY;

Applied to: - sampling_frequency - fifo_enable - watermark


⚙️ Default Configuration

Driver initializes:

MYADC_CONFIG_ENABLE | MYADC_CONFIG_IRQ_ENABLE
  • FIFO disabled by default
  • IRQ enabled for consistency
  • FIFO mode enabled via sysfs only

📌 Design Decision

Why not auto-enable DRDY when FIFO is disabled?

  • fifo_enable only controls FIFO bit
  • Does NOT imply mode switching

Avoid mixing:

  • FIFO mode
  • DRDY mode

Mode switching should be handled explicitly in future design.


🧱 Macro Simplification

Custom macro to reduce boilerplate:

DECLARE_MYADC_IIO_ATTR(name, permission)

Expands to:

IIO_DEVICE_ATTR(name, permission,
                myadc_<name>_show,
                myadc_<name>_store, 0)

✅ Summary

  • Introduced sysfs-based control plane
  • Unified configuration through helper functions
  • Preserved existing FIFO data path
  • Established clean driver state model