HWMON Core 注册与生命周期

HWMON Core 注册与生命周期

1. Class

hwmon_class

  • name:hwmon
  • 默认属性:name、可选 label
  • release:释放自动属性、groups、label 和 hwmon_device

IDA 动态分配 hwmonN,编号会随 probe 顺序变化。

2. 现代 API

推荐:

1
2
devm_hwmon_device_register_with_info(parent, name, drvdata,
chip_info, extra_groups);

非 devm:

1
2
hwmon_device_register_with_info();
hwmon_device_unregister();

hwmon_device_register() 已 deprecated。

3. 参数

  • parent:真实 I2C/SPI/platform device;
  • name:name 属性,不允许 - * 空白
  • drvdata:传给 ops;
  • hwmon_chip_info:ops + channel info;
  • extra groups:非标准属性,应该极少使用。

4. 注册链

1
2
3
4
5
6
7
8
9
10
11
12
13
14
devm_hwmon_device_register_with_info()
-> allocate devres
-> hwmon_device_register_with_info()
-> __hwmon_device_register()
-> ida_alloc()
-> allocate hwmon_device
-> __hwmon_create_attrs()
-> walk channel info/config bits
-> is_visible()
-> create attribute metadata
-> inherit OF node
-> device_register(hwmonN)
-> optional thermal sensor registration
-> attach devres cleanup

5. 属性生成

hwmon_channel_info.config[] 以 0 结尾。每个 channel 的 bit mask决定候选属性;is_visible() 最终决定:

  • 0:不创建;
  • 0444:只读;
  • 0644:root 可写;
  • 其它合理 sysfs mode。

Core 根据 type/attr 选择标准文件名模板。

6. 读写

1
2
3
4
5
6
show -> hwmon_attr_show()
-> ops->read()
-> sysfs_emit("%ld\n")

store -> kstrtol()
-> ops->write()

字符串属性(主要 label)走 read_string()

7. label

若 parent firmware property 有 label,Core复制并暴露顶层 label。各 channel label 则由 read_string(type, *_label, channel) 提供。

8. Thermal bridge

满足以下条件才尝试:

  • OF node;
  • chip info;
  • ops->read;
  • 第一个 info 是 hwmon_chip
  • config 含 HWMON_C_REGISTER_TZ

Core按温度 channel 调 devm_thermal_of_zone_register()hwmon_notify_event() 也会更新关联 thermal zone。

9. 卸载

devm 在 parent detach 时调用 hwmon_device_unregister()

1
2
3
4
5
device_unregister(hwmonN)
-> sysfs removed
-> thermal devres cleanup
-> class release
-> ida_free

driver 的 worker/timer/IRQ 必须先停止,避免 sysfs 消失后仍访问 drvdata。

10. 错误

  • -EINVAL:name/chip/ops/info 无效;
  • -ENOMEM:属性或 device 分配失败;
  • -ENOENTis_visible 隐藏属性时内部跳过;
  • thermal -ENODEV:没有匹配 OF zone,通常不使 hwmon 注册失败;
  • probe defer:底层 regulator/I2C/PWM provider 未就绪。

文章互动

阅读 --

留言

0 条留言

正在加载留言…