Skip to main content

7. PWM

7.1. Introduction

This chapter describes how to configure PWM.

The PWM driver of ROC-RK3506J-CC 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/arm/boot/dts/rk3506b-firefly-roc-rk3506b-cc-mipi101-BSD1218-A101KL68.dtsi, as shown below:

/{
backlight: backlight {
compatible = "pwm-backlight";
enable-gpios = <&pca9555 PCA_IO0_1 GPIO_ACTIVE_HIGH>;
pwms = <&pwm0_4ch_2 0 50000 1>; //pwm0_4ch_2:PWM number 0 50000:PWM period in nanoseconds 1:polarity
...
&#125;;

&pwm0_4ch_2 &#123;
pinctrl-names = "active";
pinctrl-0 = <&rm_io3_pwm0_ch2>;
status = "okay";
&#125;;

PWM parameter analysis:

&pwm0_4ch_2: defined in rk3502.dtsi.

0: indicates chip index, usually 0, because there is only one rockchip PWM chip per chip.

50000: one cycle time is 100000000 nanoseconds, there are 10 100000000 nanoseconds in one second, so the PWM output cycle is 20000Hz.

1: indicates polarity, 0 is normal polarity, 1 is reverse polarity.

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);

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.