Skip to content

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:

/sys/bus/i2c/devices/1-0048/temp_input

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:

/sys/class/hwmon/hwmonX/
    ├── name
    ├── temp1_input
    ├── temp1_max
    ├── in1_input
    └── ...

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

HWMON_CHANNEL_INFO(temp, HWMON_T_INPUT)

Defines: - one temperature sensor - exposes temp1_input


2. Visibility Control

.is_visible = myi2c_sensor_hwmon_is_visible

Controls: - which attributes exist - permissions (0444 = read-only)


3. Read Callback

.read = myi2c_sensor_hwmon_read

Maps:

hwmon attr driver function
temp1_input myi2c_sensor_read_temp_mc

4. hwmon Registration

devm_hwmon_device_register_with_info(...)

Creates: - /sys/class/hwmon/hwmonX/ - standard attributes automatically


Data Flow

userspace
temp1_input
hwmon_read()
myi2c_sensor_read_temp_mc()
I2C register access

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:

extra_groups

Result:

/sys/class/hwmon/hwmonX/
    ├── temp1_input   (standard)
    ├── temp_raw      (custom)
    ├── config
    └── ...

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