Skip to content

Day20 - I2C Driver to hwmon

Goal

Convert custom sysfs-based driver into hwmon-based driver.


Step 1 - Implement temperature helper

static int myi2c_sensor_read_temp_mc(...)
  • triggers update
  • reads raw register
  • converts to milli-degree

Step 2 - Define hwmon channel

HWMON_CHANNEL_INFO(temp, HWMON_T_INPUT)

Step 3 - Implement hwmon callbacks

is_visible

if (attr == hwmon_temp_input)
    return 0444;

read

return myi2c_sensor_read_temp_mc(...)

Step 4 - Register hwmon device

devm_hwmon_device_register_with_info(...)

Step 5 - Attach extra attributes

static const struct attribute_group *extra_groups[]

Step 6 - Remove old sysfs interface

Remove:

temp_input
sysfs_create_group()


Build & Load

make
scp myi2c_sensor.ko pi:~
sudo insmod myi2c_sensor.ko
dmesg | tail

Verify hwmon

ls /sys/class/hwmon/
cat /sys/class/hwmon/hwmonX/name

Read temperature

cat /sys/class/hwmon/hwmonX/temp1_input

Check custom attributes

ls /sys/class/hwmon/hwmonX

Ensure old path removed

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

Should NOT contain: - temp_input


Debug Comparison (Optional)

cat temp1_input
cat temp_raw

Expected Result

  • hwmon interface works
  • standard naming
  • debug attributes preserved
  • no duplicate sysfs