Skip to main content

9. PWM

9.1. Introduction​

This chapter describes how to configure PWM.

The PWM driver of AIO-3588L is kernel-5.10/drivers/pwm/pwm-rockchip.c

9.2. DTS configure​

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

9.2.1. Configure PWM DTS node​

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

/{
pwm_demo: pwm_demo {
compatible = "firefly,rk3588-pwm";
status = "okay";
pwms = <&pwm15 0 100000000 1>;
duty_ns = <50000000>;
pinctrl-names = "default";
pinctrl-0 = <&pwm15m2_pins>;
&#125;;
&#125;;

&pwm15{
status = "okay";
&#125;;
  • pwms : number of PWM channels to be applied.
  • duty_ns : duty cycle activation time of PWM, unit ns.

9.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.

9.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.

9.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.