Day22 - I2C hwmon Driver with Polling Mode¶
Objective¶
Enhance the Day21 hwmon I2C sensor driver by adding:
- background polling with delayed work
- cache-only hwmon read path when polling is enabled
- sysfs controls for polling
- fallback to on-demand mode when polling is disabled
Environment¶
- Target board: Raspberry Pi 5
- Kernel: Raspberry Pi Linux 6.12.x
- Driver:
myi2c_sensor - Sensor side: STM32 fake I2C sensor device
- Interface: I2C + hwmon
Features Implemented¶
Polling worker¶
A delayed workqueue periodically:
- sends update command to sensor
- reads raw temperature registers
- converts raw value to milli-degree Celsius
- updates internal cache
hwmon read path¶
temp1_input behavior:
- polling enabled:
- read cached value only
- return
-EAGAINif cache is not ready - polling disabled:
- fallback to on-demand update logic
Sysfs attributes¶
Driver-specific attributes:
chip_idtemp_rawconfigstatusupdate_periodupdate_oncecache_intervalpolling_enablepolling_interval
Standard hwmon attribute:
temp1_input
Implementation Notes¶
New driver fields¶
Added to struct myi2c_sensor_data:
Background polling callback¶
Polling worker:
- locks driver state
- performs sensor update
- updates cache
- reschedules itself if polling remains enabled
Read path¶
The hwmon read callback uses myi2c_sensor_read_temp_mc().
Behavior:
- polling enabled -> cache-only
- polling disabled -> update if needed
Build¶
Example:
If needed:
Load Driver¶
Example:
Expected result:
- probe succeeds
- chip ID check passes
- hwmon device is registered
- first polling work is scheduled
Find hwmon Node¶
Example:
Find the hwmon node whose name is:
Basic Read Test¶
Example:
Possible early result:
This means cache is not valid yet and the driver returned -EAGAIN.
Retry after a short delay:
Check Polling Controls¶
Example:
Enable Polling¶
Example:
Disable Polling¶
Example:
Expected behavior:
- background updates stop
temp1_inputfalls back to on-demand update logic
Change Polling Interval¶
Example:
echo 1000 | sudo tee /sys/class/hwmon/hwmonX/polling_interval
echo 3000 | sudo tee /sys/class/hwmon/hwmonX/polling_interval
Expected behavior:
- shorter interval -> more frequent refresh
- longer interval -> slower cache updates
Debug Raw Temperature Path¶
Example:
This path performs direct access to the sensor and is intended for debugging.
Manual Update¶
Example:
This is useful for:
- debug
- manual refresh
- verifying device response
Test Script¶
A helper script was used to verify:
- polling enable/disable
- polling interval effect
- cache-only hwmon behavior
Example:
The script checks:
- enable polling
- set polling interval
- read
temp1_inputrepeatedly - disable polling
- enable polling again
Observed Result¶
Observed behavior matched expectation:
- polling mode updated cached values periodically
- changing polling interval affected update frequency
- disabling polling stopped periodic updates
- re-enabling polling resumed updates
- hwmon path operated correctly
Conclusion¶
Day22 successfully migrated the driver from a purely read-triggered update model to a polling-based cached model.
This completes the second active update model in the learning path:
- Day21: on-demand
- Day22: polling
- Day23: interrupt-driven (planned)