Day 20 - hwmon Subsystem (Hardware Monitoring)¶
Overview¶
In this lab, we migrate a custom I2C sensor driver from a manual sysfs interface to the Linux hwmon subsystem.
This introduces: - Standardized sensor interface - Kernel-managed sysfs structure - Cleaner separation between standard data and debug controls
Why hwmon?¶
Previously (Day19), we implemented:
This approach has limitations:
- Not standardized
- Not compatible with tools like
sensors - Hard to scale for multiple sensor types
hwmon Design¶
hwmon provides a standard interface for sensors:
Key Concepts¶
| Concept | Description |
|---|---|
| hwmon device | Logical sensor device |
| channel | Sensor type (temp, voltage, fan...) |
| attribute | sysfs entry (temp1_input) |
| hwmon_ops | callback interface |
| hwmon_chip_info | driver descriptor |
Driver Architecture¶
1. Channel Definition¶
Defines:
- one temperature sensor
- exposes temp1_input
2. Visibility Control¶
Controls: - which attributes exist - permissions (0444 = read-only)
3. Read Callback¶
Maps:
| hwmon attr | driver function |
|---|---|
| temp1_input | myi2c_sensor_read_temp_mc |
4. hwmon Registration¶
Creates:
- /sys/class/hwmon/hwmonX/
- standard attributes automatically
Data Flow¶
Unit Convention¶
hwmon requires:
- temperature = milli-degree Celsius
- integer only (no float)
Example:
| Raw | Celsius | hwmon |
|---|---|---|
| 2650 | 26.50°C | 26500 |
Custom Attributes (extra_groups)¶
Non-standard attributes:
- chip_id
- temp_raw
- config
- status
- update_period
- update_once
Are attached via:
Result:
Migration Summary¶
| Feature | Day19 | Day20 |
|---|---|---|
| temp interface | temp_input | temp1_input |
| sysfs location | i2c device | hwmon |
| standardization | ❌ | ✅ |
| extensibility | low | high |
Key Design Insight¶
Do not reinvent interfaces — use subsystem standards
- Standard → hwmon
- Custom → extra_groups
When to use hwmon¶
Use hwmon when your device provides:
- temperature
- voltage
- current
- fan speed
- power
Do NOT use hwmon for:
- arbitrary device control
- non-sensor devices
Takeaway¶
- hwmon separates data plane (sensor values) from control/debug plane
- kernel provides structure → driver provides data
- consistent interface improves ecosystem compatibility