PWM Usage
Introduction
PWM (Pulse Width Modulation) is commonly used for backlight adjustment, fan speed control, buzzers and servos. The PWM driver of Firefly-R3572A is the pwm-rockchip driver in the SDK kernel. PWM signals are available on the LCD_BL_PWM pin of J18 (the MIPI-DSI connector) and on the J1 double-row pin header.
Hardware Connection
| Connector | Signal | Level |
|---|---|---|
| J18 pin 12 | LCD_BL_PWM | 3.3 V |
| J1 double-row pin header | PWM | Depends on the actual configuration |
DTS Configuration
The Firefly-R3572A SDK already uses a 6-channel PWM controller as the MIPI panel backlight, defined in kernel-6.12/arch/arm64/boot/dts/rockchip/rk3572-firefly-r3572a-mipi101-BSD1218-A101KL68.dtsi:
&backlight {
compatible = "pwm-backlight";
pwms = <&pwm1_6ch_5 0 50000 1>;
};
&pwm1_6ch_5 {
status = "okay";
pinctrl-names = "default";
pinctrl-0 = <&pwm1m0_ch5_pins>;
};To add your own PWM device, request a channel in the board-level DTS in the same way (the controller, channel number and pinctrl macro depend on the SDK code):
pwm_demo: pwm_demo {
compatible = "firefly,pwm-demo";
status = "okay";
pwms = <&pwmX 0 25000 0>;
duty-cycle-ratio = <50>;
pinctrl-names = "default";
pinctrl-0 = <&pwmXmX_pins>;
};Parameters:
&pwmX: the PWM controller node (such as&pwm1_6ch_5above)0: the channel number25000: the period in ns (corresponding to 40 kHz)duty-cycle-ratio: the duty cycle in percent
sysfs Usage
After the system boots, control PWM through the sysfs interface:
echo 0 > /sys/class/pwm/pwmchipX/export # Request channel 0
echo 25000 > /sys/class/pwm/pwmchipX/pwm0/period # Set the period to 25 us
echo 12500 > /sys/class/pwm/pwmchipX/pwm0/duty_cycle # Set the duty time to 12.5 us
echo normal > /sys/class/pwm/pwmchipX/pwm0/polarity # Set the polarity
echo 1 > /sys/class/pwm/pwmchipX/pwm0/enable # Enable the outputThe number of pwmchipX depends on the system enumeration. Check with ls /sys/class/pwm/.
Disable the output:
echo 0 > /sys/class/pwm/pwmchipX/pwm0/enableDebugging
Measure the pin with an oscilloscope or a logic analyzer and confirm that the frequency and duty cycle match the configuration. If there is no output, check in order:
- Whether the PWM node
statusin the DTS isokay. - Whether pinctrl multiplexes the pin to the PWM function.
- Whether
duty_cycleis smaller thanperiod.
Interface Description
You can use the PWM node generated by the steps above in other driver files. The steps are as follows:
(1) Include the following header file in the driver file of the device controlled by PWM:
#include <linux/pwm.h>This header file contains the PWM function interfaces.
(2) Request a PWM
Use
struct pwm_device *devm_pwm_get(struct device *dev, const char *con_id);to request a PWM. For example:
struct pwm_device *pwm0 = devm_pwm_get(&pdev->dev, NULL);(3) Configure the PWM
Use
int pwm_config(struct pwm_device *pwm, int duty_ns, int period_ns);to configure the duty cycle, for example:
pwm_config(pwm0, 500000, 1000000);(4) Enable the PWM
int pwm_enable(struct pwm_device *pwm);is used to enable the PWM, for example:
pwm_enable(pwm0);(5) The main interface functions for controlling the PWM output are:
- Function: request a PWM
struct pwm_device *devm_pwm_get(struct device *dev, const char *con_id);- Function: free the requested PWM
void pwm_free(struct pwm_device *pwm);- Function: configure the duty cycle of the PWM
int pwm_config(struct pwm_device *pwm, int duty_ns, int period_ns);- Function: enable the PWM
int pwm_enable(struct pwm_device *pwm);- Function: disable the PWM
void pwm_disable(struct pwm_device *pwm);Reference example: kernel-6.12/drivers/pwm/pwm-firefly-demo.c
FAQs
The PWM fails to register:
- Check whether the corresponding PWM is enabled in the dts configuration file.
- Check whether the IO port of the PWM is occupied by other resources; check the error return value for the reason.

