RK3399 PWM

PWM 调光

Posted by LXG on March 23, 2023

PWM 使用-Firefly

PWM 概念

PWM,英文名Pulse Width Modulation,是脉冲宽度调制缩写,也就是一种周期一定而高低电平可调的方波信号,在一个信号周期中,高电平持续时间的为T1,低电平持续时间的为T2

占空比:在一个信号周期中,高电平持续时间与信号周期的比值,即T1/Time

pwm_brightness

RK3399 PWM

RK3399 开发板上有 4 路 PWM 输出,分别为 PWM0 ~ PWM3,4 路 PWM 分别使用在 EDP 背光、MIPI 背光、VDDLOG 供电、红外 IR。

RK3399 的 PWM 驱动为:kernel/drivers/pwm/pwm-rockchip.c

kernel/arch/arm64/boot/dts/rockchip/rk3399.dtsi


/ {
	compatible = "rockchip,rk3399";

        // MIPI backlight
	pwm0: pwm@ff420000 {
		compatible = "rockchip,rk3399-pwm", "rockchip,rk3288-pwm";
		reg = <0x0 0xff420000 0x0 0x10>;
		#pwm-cells = <3>;
		pinctrl-names = "active";
		pinctrl-0 = <&pwm0_pin>;
		clocks = <&pmucru PCLK_RKPWM_PMU>;
		clock-names = "pwm";
		status = "disabled";
	};

        // EDP backlight
	pwm1: pwm@ff420010 {
		compatible = "rockchip,rk3399-pwm", "rockchip,rk3288-pwm";
		reg = <0x0 0xff420010 0x0 0x10>;
		#pwm-cells = <3>;
		pinctrl-names = "active";
		pinctrl-0 = <&pwm1_pin>;
		clocks = <&pmucru PCLK_RKPWM_PMU>;
		clock-names = "pwm";
		status = "disabled";
	};

        // VDDLOG 供电
	pwm2: pwm@ff420020 {
		compatible = "rockchip,rk3399-pwm", "rockchip,rk3288-pwm";
		reg = <0x0 0xff420020 0x0 0x10>;
		#pwm-cells = <3>;
		pinctrl-names = "active";
		pinctrl-0 = <&pwm2_pin>;
		clocks = <&pmucru PCLK_RKPWM_PMU>;
		clock-names = "pwm";
		status = "disabled";
	};

	pwm3: pwm@ff420030 {
		compatible = "rockchip,rk3399-pwm", "rockchip,rk3288-pwm";
		reg = <0x0 0xff420030 0x0 0x10>;
		#pwm-cells = <3>;
		pinctrl-names = "active";
		pinctrl-0 = <&pwm3a_pin>;
		clocks = <&pmucru PCLK_RKPWM_PMU>;
		clock-names = "pwm";
		status = "disabled";
	};

};

rk3399-evb.dtsi


/ {
	compatible = "rockchip,rk3399-evb", "rockchip,rk3399";

	backlight: backlight {
		compatible = "pwm-backlight";
		pwms = <&pwm0 0 25000 0>;
		brightness-levels = <
		    255 254 253 252 251 250 249 248 247 246 245 244 243 242 241 240
		    239 238 237 236 235 234 233 232 231 230 229 228 227 226 225 224 223 222 221 220
		    219 218 217 216 215 214 213 212 211 210 209 208 207 206 205 204 203 202 201 200
		    199 198 197 196 195 194 193 192 191 190 189 188 187 186 185 184 183 182 181 180
		    179 178 177 176 175 174 173 172 171 170 169 168 167 166 165 164 163 162 161 160
		    159 158 157 156 155 154 153 152 151 150 149 148 147 146 145 144 143 142 141 140
		    139 138 137 136 135 134 133 132 131 130 129 128 127 126 125 124 123 122 121 120
		    119 118 117 116 115 114 113 112 111 110 109 108 107 106 105 104 103 102 101 100
		    99 98 97 96 95 94 93 92 91 90 89 88 87 86 85 84 83 82 81 80 79 78 77 76 75 74 73 72 71 70
		    69 68 67 66 65 64 63 62 61 60 59 58 57 56 55 54 53 52 51 50 49 48 47 46 45 44 43 42 41 40
		    39 38 37 36 35 34 33 32 31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16 15 14 13 12 11 10
		    9 8 7 6 5 4 3 2 1 0
                >;

		default-brightness-level = <200>;
	};

};

&pwm0 {
        status = "okay";
};

rk3399-evb-rev3.dtsi


/ {
	compatible = "rockchip,rk3399-evb-rev3", "rockchip,rk3399";

	vdd_center: vdd-center {
		compatible = "pwm-regulator";
		rockchip,pwm_id = <2>;
		rockchip,pwm_voltage = <900000>;
		pwms = <&pwm2 0 25000 1>;
		regulator-name = "vdd_center";
		regulator-min-microvolt = <800000>;
		regulator-max-microvolt = <1400000>;
		regulator-always-on;
		regulator-boot-on;
	};

	rockchip_suspend: rockchip-suspend {
		compatible = "rockchip,pm-rk3399";
		status = "okay";
		rockchip,sleep-debug-en = <1>;
		rockchip,wakeup-config = <
			(0
			| RKPM_GPIO_WKUP_EN
			)
		>;
		rockchip,pwm-regulator-config = <
			(0
			| PWM2_REGULATOR_EN
			)
		>;
		rockchip,power-ctrl =
			<&gpio1 17 GPIO_ACTIVE_HIGH>;
	};

        // LED 调光,可自定义
        pwmleds {
	        status = "okay";
	        compatible = "pwm-leds";
	        front {
	                //active-low;
	                label = "lamp_led";
	                max-brightness = <255>;
	                pwms = <&pwm3 0 25000 0>;
	        };
        };

};

&pwm2 {
	status = "okay";
	pinctrl-names = "active";
	pinctrl-0 = <&pwm2_pin_pull_down>;
};

&pwm3 {
        status = "okay";
};

rk3399-wif-edp-1366x768.dtsi


&pwm1 {
        status = "okay";
};

&backlight {
        status = "okay";
        pwms = <&pwm1 0 25000 0>;
};

驱动参数含义

pwms = <&pwm0 0 25000 0>;

  • 第一个参数 表示此背光接在 pwm0 上;
  • 第二个参数 表示 index 为 0,pwm0 下只有 1个 pwm,所以填 0
  • 第三个参数 表示周期为 25000ns,即 频率 为 40k
  • 第四个参数 表示极性,0 正极性,1 负极性, 正极性 0 表示 背光为正极 0~255 ,占空比从 0~100% 变化, 负极性 1 表示 背光为负极 255~0 ,占空比从 100~0% 变化

调试

通过内核丰富的debug接口查看pwm注册状态


$ cat sys/kernel/debug/pwm                                                                                                                                                                                  
platform/ff420030.pwm, 1 PWM device
 pwm-0   (front               ): requested period: 24997 ns duty: 0 ns polarity: normal

platform/ff420020.pwm, 1 PWM device
 pwm-0   (vdd-center          ): requested enabled period: 24997 ns duty: 4246 ns polarity: inverse

platform/ff420000.pwm, 1 PWM device
 pwm-0   (backlight           ): requested enabled period: 24997 ns duty: 13234 ns polarity: normal

使用PWM3 实现 LED 灯调光

原理图

rk3399_pwm3

led_pwm_hardware

LED_PWM: 输出可调节占空比的方波信号

LED+ : 根据不同占空比输出不同的直流电压信号,接 3(并联) x 4(串联)LED灯组负载时,最大亮度(255)输出电压约为12.5v, 最小亮度(1) 输出电压为 10.4V,亮度为0时输出为 5V (此时无法驱动LED灯)

驱动

CONFIG_LEDS_PWM=y

kernel/drivers/leds/leds-pwm.c

波形

rk3399_pwm

调节亮度节点

echo 20 > sys/class/leds/lamp_led/brightness

RY3730 恒流源 驱动 LED 灯

恒流源是指在功率范围内,对外输出的电流基本是恒定的

  • 不因负载(输出电压)变化而改变;
  • 不因环境温度变化而改变;
  • 内阻为无限大 (以使其电流可以全部流出到外面);
  • 能够提供恒定电流的驱动电路即为恒流源驱动电路。

在LED驱动电路中常见恒流源驱动。 由于在电路中给LED供电的电源电压不同, 以及LED正向电压的不同,为了确保LED最佳的性能和长久的工作寿命,就需要一个有效的恒流源驱动电路,而不是传统DC/DC 的恒压控制。 通常可采用一个电流检测电阻反馈LED电流来实现其恒流控制,从而使LED的正向电流保持一定。

RY3730是一款升压转换器