Modern smartphones are packed with hardware sensors: Accelerometers to detect screen rotation, gyroscopes for gaming, magnetometers for the compass, and ambient light sensors to adjust screen brightness.
Historically, Android sensor drivers were messy. Manufacturers implemented them as custom character drivers or abused the evdev input subsystem. Today, the Linux kernel has standardized sensor management via the IIO (Industrial I/O) subsystem.
The IIO Subsystem
The IIO subsystem was designed specifically for Analog-to-Digital Converters (ADCs), Digital-to-Analog Converters (DACs), and various environmental sensors.
It provides a unified sysfs interface. Instead of writing complex ioctl C code, the Android sensor HAL can often read sensor data by simply reading standard text files generated by the kernel.
# Explore the IIO devices on an Android phone
adb shell ls /sys/class/iio:device0/
# You can often read the raw sensor value directly
adb shell cat /sys/class/iio:device0/in_accel_x_raw
Polling vs. Interrupt-Driven Sensors
There are two primary ways the kernel retrieves data from a sensor chip over the I2C or SPI bus:
1. Polling Mode
In polling mode, the CPU sets a timer and constantly wakes up to ask the sensor, "Do you have new data?" This is incredibly inefficient. If the phone is sitting perfectly still on a desk, the CPU is wasting battery power constantly asking the accelerometer for data that hasn't changed.
2. Interrupt-Driven Mode (Data Ready)
Modern sensors use hardware interrupts. The kernel puts the sensor to sleep. When the accelerometer detects physical movement, it pulls a GPIO pin low.
The CPU interrupt handler fires, the IIO driver wakes up, reads the new data over I2C, and pushes it to a ring buffer for the user-space Android SensorService to consume.
Sensor Hubs (Context Hub)
Because reading sensors hundreds of times a second keeps the main CPU awake and drains battery, modern Android architectures offload this work to a Sensor Hub (or Context Hub).
A Sensor Hub is a tiny, ultra-low-power micro-controller (like an ARM Cortex-M4) dedicated entirely to managing sensors.
- The main Android CPU goes to sleep.
- The Sensor Hub continually reads the accelerometer and gyroscope.
- It performs "Sensor Fusion" (combining data to determine orientation) or step counting.
- It only wakes up the main, power-hungry Android CPU when something important happens (e.g., the user picks up the phone).
In this modern architecture, the Linux kernel driver doesn't talk to the sensors directly; it acts as an IPC bridge, communicating exclusively with the Sensor Hub firmware.