Linux Thermal 是 Linux 系统下温度控制相关的模块,主要用来控制系统运行过程中芯片产生的热量,使芯片温度和设备外壳温度维持在一个安全、舒适的范围。
那下面我们就来一起看看对于温度控制这样一个需求,Linux 内核是怎么实现的。
Thermal 的主要框架
要实现一个温度控制的需求,试想一下我们是不是最少要有获取温度的设备和控制温度的设备这两个最基本的东西?当然附带的也会产生一些使用温度控制设备的策略。
那上面这些东西在 Linux Thermal 框架中怎么体现呢?通过阅读源码我们发现代码中对上面的东西进行了一些抽象。获取温度的设备:在 Thermal 框架中被抽象为 Thermal Zone Device;
控制温度的设备:在 Thermal 框架中被抽象为 Thermal Cooling Device;
控制温度策略:在 Thermal 框架中被抽象为 Thermal Governor;
Thermal Zone Device
上面说到 Thermal Zone Device 是获取温度设备的抽象,怎么抽象的?终究我们还是要 RTFSC。
struct thermal_zone_device {
int id;
char type[THERMAL_NAME_LENGTH];
struct device device;
struct thermal_attr *trip_temp_attrs;
struct thermal_attr *trip_type_attrs;
struct thermal_attr *trip_hyst_attrs;
void *devdata;
int trips;
/* 轮询时间 */
int passive_delay;
int polling_delay;
int temperature;
int last_temperature;
int emul_temperature;
int passive;
unsigned int forced_passive;
/* 设备的操作函数 */
struct thermal_zone_device_ops *ops;
const struct thermal_zone_params *tzp;
struct thermal_governor *governor;
struct list_head thermal_instances;
struct idr idr;
struct mutex lock;
struct list_head node;
/* 用来循环处理的 delayed_work */
struct delayed_work poll_queue;
};
struct thermal_zone_device_ops {
/* 绑定函数 */
int (*bind) (struct thermal_zone_device *,
struct thermal_cooling_device *);
int (*unbind