PWM Backlight 驱动详解
1. 概述
pwm_bl.c是 Linux 内核中基于 PWM 的背光控制驱动程序。该驱动通过 PWM(脉冲宽度调制)信号来控制 LCD 屏幕的背光亮度,是嵌入式系统中常用的背光控制方案。
文件位置:linux-4.19.125/drivers/video/backlight/pwm_bl.c
主要功能:
- 通过 PWM 占空比控制背光亮度
- 支持设备树(Device Tree)配置
- 支持 CIE 1931 亮度曲线算法
- 支持亮度等级线性插值
- 支持电源管理和休眠/唤醒
2. 驱动架构
2.1 核心数据结构
structpwm_bl_data{structpwm_device*pwm;// PWM 设备structdevice*dev;// 设备指针unsignedintperiod;// PWM 周期(纳秒)unsignedintlth_brightness;// 最低亮度阈值unsignedint*levels;// 亮度等级表bool enabled;// 使能状态structregulator*power_supply;// 电源 regulatorstructgpio_desc*enable_gpio;// 使能 GPIOunsignedintscale;// 亮度缩放因子bool legacy;// 旧版 API 标志unsignedintpost_pwm_on_delay;// PWM 开启后延迟unsignedintpwm_off_delay;// PWM 关闭前延迟int(*notify)(structdevice*,intbrightness);// 亮度变化前通知void(*notify_after)(structdevice*,intbrightness);// 亮度变化后通知int(*check_fb)(structdevice*,structfb_info*);// framebuffer 检查void(*exit)(structdevice*);// 退出回调};2.2 背光操作接口
staticconststructbacklight_opspwm_backlight_ops={.update_status=pwm_backlight_update_status,// 更新背光状态.check_fb=pwm_backlight_check_fb,// 检查 framebuffer};3. 核心功能实现
3.1 电源控制
开启背光 (pwm_backlight_power_on)
staticvoidpwm_backlight_power_on(structpwm_bl_data*pb,intbrightness){// 1. 检查是否已开启if(pb->enabled)return;// 2. 使能电源 regulatorregulator_enable(pb->power_supply);// 3. 启用 PWMpwm_enable(pb->pwm);// 4. 延迟(如果配置)if(pb->post_pwm_on_delay)msleep(pb->post_pwm_on_delay);// 5. 设置使能 GPIOif(pb->enable_gpio)gpiod_set_value_cansleep(pb->enable_gpio,1);pb->enabled=true;}关闭背光 (pwm_backlight_power_off)
staticvoidpwm_backlight_power_off(structpwm_bl_data*pb){if(!pb->enabled)return;// 1. 关闭使能 GPIOif(pb->enable_gpio)gpiod_set_value_cansleep(pb->enable_gpio,0);// 2. 延迟(如果配置)if(pb->pwm_off_delay)msleep(pb->pwm_off_delay);// 3. 配置 PWM 并禁用pwm_config(pb->pwm,0,pb->period);pwm_disable(pb->pwm);// 4. 禁用电源 regulatorregulator_disable(pb->power_supply);pb->enabled=false;}3.2 占空比计算
staticintcompute_duty_cycle(structpwm_bl_data