PWM

Introduction

This chapter describes how to configure PWM.

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

DTS configure

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

Configure PWM DTS node

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

pwm_demo: pwm_demo {
   status = "okay";
   compatible = "firefly,rk3399-pwm";
   pwm_id = <1>;
   min_period = <0>;
   max_period = <10000>;
   duty_ns = <5000>;
};
  • pwm_id : number of PWM channels to be applied.

  • min_period : minimum period length.

  • max_period : maximum period length.

  • duty_ns : duty cycle activation time of PWM, unit ns.

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 *pwm_request(int pwm_id, const char *label);
```

Function to apply for PWM. Such as:

```
struct pwm_device * pwm1 = NULL;pwm0 = pwm_request(1, “firefly-pwm”);
```
  • (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/drivers/pwm/pwm-firefly.c.

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.

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.