Skip to main content

7. PWM

7.1. Introduction​

This chapter describes how to configure PWM.

The PWM driver of ROC-RK3576-PC is kernel/drivers/pwm/pwm-rockchip.c

7.2. DTS configure​

There are three main steps to configure PWM: configure PWM DTS node, configure PWM kernel driver and control PWM device.

7.2.1. Configure PWM DTS node​

Add the PWM DTS configuration in the DTS source file kernel/arch/arm64/boot/dts/rockchip/rk3576-firefly-demo.dtsi, as shown below:

/{
pwm_demo: pwm_demo {
compatible = "firefly,rk3576-pwm";
status = "okay";
pwms = <&pwm1_6ch_1 0 100000000 1>; //pwm1_6ch_1:PWM number 0 100000000:PWM period in nanoseconds 1:polarity
duty_ns = <50000000>; //pwm duty cycle activation time, unit ns
&#125;;
&#125;;

&pwm1_6ch_1 &#123;
status = "okay";
pinctrl-0 = <&pwm1m0_ch1>;
&#125;;
  • &pwm1_6ch_1 : number of PWM channels to be applied.
  • duty_ns : duty cycle activation time of PWM, unit ns.

7.3. Interface specification​

Users can use the PWM nodes generated by the above steps in other driver files. The specific methods are as follows:

  • (1) The following header files are included in the device driver files that need to be controlled by PWM :

    #include <linux/pwm.h>

    This header file mainly contains the PWM function interface.

  • (2) Apply PWM

    Using:

    struct pwm_device *devm_pwm_get(struct device *dev, const char *con_id);

Function to apply for PWM. Such as:

struct pwm_device * pwm0 = devm_pwm_get(&pdev->dev, NULL);
  • (3) Configue PWM

    Using:

    int pwm_config(struct pwm_device *pwm, int duty_ns, int period_ns);

    Configure the duty cycle of PWM, for example:

    pwm_config(pwm0, 500000, 1000000);
  • (4) Enable the PWM function

    int pwm_enable(struct pwm_device *pwm);

    Used to enable PWM, for example:

    pwm_enable(pwm0);
  • (5) Control PWM output mainly uses the following interface functions:

    function: Used to apply for PWM

    struct pwm_device *pwm_request(int pwm_id, const char *label);

    function: Used to release the PWM requested

    void pwm_free(struct pwm_device *pwm);

    function: Used to configure the duty cycle of PWM

    int pwm_config(struct pwm_device *pwm, int duty_ns, int period_ns);

    function: Enable PWM

    int pwm_enable(struct pwm_device *pwm);

    function: Ban PWM

    void pwm_disable(struct pwm_device *pwm);

Refer to the Demo: kernel-5.10/drivers/pwm/pwm-firefly-demo.c.

7.4. Debug method​

Check PWM registration status through the rich debug interface of the kernel, adb shell or serial port to enter the android terminal and execute:

cat /sys/kernel/debug/ PWM

To see if the registration was successful, the interface name and register address are returned.

7.5. FAQs​

Q1: Pwm could not register successfully ?

A1:

  • whether the DTS configuration file opens the corresponding PWM.
  • whether the IO port where PWM is located is occupied by other resources, you can check the reason according to the error return value.