Day 20 - hwmon Subsystem¶
Summary¶
Today I migrated my I2C sensor driver from a custom sysfs interface to the hwmon subsystem.
Key changes: - temp_input → temp1_input - manual sysfs → hwmon framework - added hwmon_ops and chip_info - used extra_groups for debug attributes
What I Learned¶
1. hwmon is a standardized sensor framework¶
Instead of defining custom sysfs files, Linux already provides: - naming convention - structure - user-space compatibility
2. hwmon separates responsibilities¶
- driver → provides data
- subsystem → handles interface
3. data format is strict¶
- temperature must be integer
- unit is milli-degree Celsius
4. extra_groups is important¶
Allows mixing: - standard attributes - custom debug attributes
5. devm API simplifies lifecycle¶
No need for: - manual cleanup - remove() handling
Issues Encountered¶
hwmon name warning¶
Problem:
Fix: - use underscore instead of dash
Different values between interfaces¶
Cause: - update_once is triggered per read
Final Result¶
Reflection¶
This lab clearly shows the importance of:
- using kernel subsystems
- not reinventing interfaces
- designing clean abstraction layers
Next¶
Potential improvements: - caching (update interval) - support multiple channels - integrate with userspace tools (lm-sensors)