Khadas Docs

Amazing Khadas, always amazes you!

User Tools

Site Tools


products:sbc:common:applications:gpio:pwm

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revision Previous revision
Next revision
Previous revision
products:sbc:common:applications:gpio:pwm [2022/07/12 06:01]
nick
products:sbc:common:applications:gpio:pwm [2024/08/22 02:40] (current)
nick
Line 3: Line 3:
 ===== Introduction ===== ===== Introduction =====
  
-This document mainly describes the usage of hardware PWM. You will learn how to control the hardware PWM on the [[products:sbc:common:applications:gpio:40pin-header|40-Pin Header]].+This page introduces the usage of hardware PWM, and leads you to control the hardware PWM on the [[products:sbc:common:applications:gpio:40pin-header|40-Pin Header]].
  
-===== PWM Information =====+===== PWM Reference Table =====
  
-<tabbox VIM1>+|          PWM            PIN  ^  GPIO name      ^  DT overlay node  ^  Device node              ^ 
 +^  VIM1   |  ''PWM_AO_A''  |  35    ''GPIOAO_3''    ''pwm_ao_a''      /sys/class/pwm/pwmchip4 
 +^  VIM2    ''PWM_D''      35    ''GPIODV_28''  |  ''pwm_d''        |  /sys/class/pwm/pwmchip1 
 +^  VIM3    ''PWM_F''      35    ''GPIOH_5''    |  ''pwm_f''        |  /sys/class/pwm/pwmchip4 
 +^  VIM3L  |  ''PWM_F''      35    ''GPIOH_5''    |  ''pwm_f''        | /sys/class/pwm/pwmchip0   | 
 +^  VIM4    ''PWM_F''      35    ''GPIOY_8''    |  ''pwm_f''        |  /sys/class/pwm/pwmchip4 
 +^  VIM1S  |  ''PWM_F''      35    ''GPIOZ_6''    |  ''pwm_f''        |  /sys/class/pwm/pwmchip0  |
  
-^  PWM      ^  PIN & GPIO Number        DT Overlays Node  ^  Device Node              ^ 
-| PWM_AO_A  | PIN35 - GPIOAO_3 (#504)  |  pwm_ao_a          |  /sys/class/pwm/pwmchip4  | 
  
-<tabbox VIM2>+===== Enable PWM =====
  
-^  PWM    PIN & GPIO Number        ^  DT Overlays Node  ^  Device Node              ^ +==== Linux ====
-| PWM_D  | PIN35 - GPIODV_28 (#478)  |  pwm_d              /sys/class/pwm/pwmchip1  |+
  
-<tabbox VIM3/3L> 
  
-^  PWM   ^  PIN & GPIO Number      ^  DT Overlays Node  ^  Device Node              ^ +In order to use the PWM, you need to enable the PWM function via [[products:sbc:common:configurations:device-tree-overlay|Device Tree Overlay]]. 
-PWM_F  | PIN35 - GPIOX_7 (#432)   pwm_f              /sys/class/pwm/pwmchip4  |+ 
 +<tabbox VIM1/2/3/Edge1> 
 + 
 +Edit ''/boot/env.txt'' to add the pwm node to ''overlays'' node if it doesn't exist. 
 + 
 +Take VIM3 as an example to enable ''PWM_F'', you need to add ''pwm_f'' node to ''overlays'' node if it doesn't exist. 
 + 
 +```shell 
 +overlays=uart3 pwm_f i2c3 i2s os08a10 watchdog 
 +``` 
 + 
 +After reboot, you will see the pwm device node. 
 + 
 +```shell 
 +$ ls /sys/class/pwm/pwmchip4 
 +device  export  npwm  power  subsystem  uevent  unexport 
 +``` 
 + 
 +<tabbox VIM3L> 
 + 
 +Edit ''/boot/env.txt'' to add the pwm node to ''overlays'' node if it doesn't exist. 
 + 
 +Take VIM3L as an example to enable ''PWM_F'', you need to add ''pwm_f'' node to ''overlays'' node if it doesn't exist. 
 + 
 +```shell 
 +overlays=uart3 pwm_f i2c3 i2s watchdog 
 +``` 
 + 
 +After reboot, you will see the pwm device node. 
 + 
 +```shell 
 +$ ls /sys/class/pwm/pwmchip0 
 +device  export  npwm  power  subsystem  uevent  unexport 
 +``` 
 + 
 + 
 +<tabbox VIM3 with 5.15 kernel> 
 + 
 +Edit ''/boot/dtb/amlogic/kvim3.dtb.overlay.env'' to add pwm node to ''fdt_overlays'' node if it doesn't exist. 
 + 
 + 
 +You need to add ''pwm_f'' to node ''fdt_overlays'' if it doesn't exist to enable ''PWM_F''
 + 
 +```shell 
 +fdt_overlays=pwm_f 
 + 
 +``` 
 + 
 +After reboot, you will see the pwm device node. 
 + 
 +<tabbox VIM3L with 5.15 kernel> 
 + 
 +Edit ''/boot/dtb/amlogic/kvim3l.dtb.overlay.env'' to add pwm node to ''fdt_overlays'' node if it doesn't exist. 
 + 
 + 
 +You need to add ''pwm_f'' to node ''fdt_overlays'' if it doesn't exist to enable ''PWM_F''
 + 
 +```shell 
 +fdt_overlays=pwm_f 
 + 
 +``` 
 + 
 +After reboot, you will see the pwm device node.
  
 <tabbox VIM4> <tabbox VIM4>
  
-^  PWM    PIN & GPIO Number      ^  DT Overlays Node  ^  Device Node              ^ +Edit ''/boot/dtb/amlogic/kvim4.dtb.overlay.env'' to add pwm node to ''fdt_overlays'' node if it doesn't exist.
-| PWM_F  | PIN35 - GPIOY_8 (#482)  |  pwm_f             |  /sys/class/pwm/pwmchip4  |+
  
  
-</tabbox>+You need to add ''pwm_f'' to node ''fdt_overlays'' if it doesn't exist to enable ''PWM_F''.
  
-===== Enable PWM =====+```shell 
 +fdt_overlays=pwm_f 
 + 
 +``` 
 + 
 +After reboot, you will see the pwm device node.
  
-<tabbox VIM1>  
-%%PWM_AO_A:%% 
 ```shell ```shell
-echo 1 | sudo tee /sys/class/pwm/pwmchip4/export +ls /sys/class/pwm/pwmchip4 
-$ echo 1000000 | sudo tee /sys/class/pwm/pwmchip4/pwm1/period +device  export  npwm  power  subsystem  uevent  unexport
-$ echo 500000 | sudo tee /sys/class/pwm/pwmchip4/pwm1/duty_cycle +
-$ echo 1 | sudo tee /sys/class/pwm/pwmchip4/pwm1/enable+
 ``` ```
-<tabbox VIM2>  + 
-PWM_D:+<tabbox VIM1S
 + 
 +Edit ''/boot/dtb/amlogic/kvim1s.dtb.overlay.env'' to add pwm node to ''fdt_overlays'' node if it doesn't exist. 
 + 
 + 
 +You need to add ''pwm_f'' to node ''fdt_overlays'' if it doesn't exist to enable ''PWM_F''
 ```shell ```shell
-$ echo 1 | sudo tee /sys/class/pwm/pwmchip1/export +fdt_overlays=pwm_f 
-$ echo 1000000 | sudo tee /sys/class/pwm/pwmchip1/pwm1/period +
-$ echo 500000 | sudo tee /sys/class/pwm/pwmchip1/pwm1/duty_cycle +
-$ echo 1 | sudo tee /sys/class/pwm/pwmchip1/pwm1/enable+
 ``` ```
-<tabbox VIM3/3L>  + 
-%%PWM_F:%%+After reboot, you will see the pwm device node. 
 ```shell ```shell
-echo 1 | sudo tee /sys/class/pwm/pwmchip4/export +ls /sys/class/pwm/pwmchip0 
-$ echo 1000000 | sudo tee /sys/class/pwm/pwmchip4/pwm1/period +device  export  npwm  power  subsystem  uevent  unexport
-$ echo 500000 | sudo tee /sys/class/pwm/pwmchip4/pwm1/duty_cycle +
-$ echo 1 | sudo tee /sys/class/pwm/pwmchip4/pwm1/enable+
 ``` ```
 +</tabbox>
 +
 +
 +==== Android ====
 +
 +<tabbox VIM4>
 +
 +The ''PWM'' function is disabled by default, you need to modify the dts to enable it.
 +
 +```diff
 +diff --git a/arch/arm64/boot/dts/amlogic/kvim4.dts b/arch/arm64/boot/dts/amlogic/kvim4.dts
 +--- a/arch/arm64/boot/dts/amlogic/kvim4.dts
 ++++ b/arch/arm64/boot/dts/amlogic/kvim4.dts
 + &pwm_ef {
 ++    pinctrl-names = "default";
 ++    pinctrl-0 = <&pwm_f_pins>;
 +        status = "okay";
 + };
 + 
 +diff --git a/arch/arm64/boot/dts/amlogic/kvim4n.dts b/arch/arm64/boot/dts/amlogic/kvim4n.dts
 +--- a/arch/arm64/boot/dts/amlogic/kvim4n.dts
 ++++ b/arch/arm64/boot/dts/amlogic/kvim4n.dts
 + &pwm_ef {
 ++    pinctrl-names = "default";
 ++    pinctrl-0 = <&pwm_f_pins>;
 +        status = "okay";
 + };
 + 
 +diff --git a/arch/arm64/boot/dts/amlogic/mesont7_an400-panel.dtsi b/arch/arm64/boot/dts/amlogic/mesont7_an400-panel.dtsi
 +--- a/arch/arm64/boot/dts/amlogic/mesont7_an400-panel.dtsi
 ++++ b/arch/arm64/boot/dts/amlogic/mesont7_an400-panel.dtsi
 +@@ -1442,13 +1442,13 @@
 + 
 +        backlight1{
 +                compatible = "amlogic, backlight-t7";
 +-               status = "okay";
 ++               status = "disabled";
 +                index = <1>;
 +                key_valid = <0>;
 +                pinctrl-names = "pwm_on",
 +                                "pwm_off";
 +-               pinctrl-0 = <&pwm_f_pins>;
 +-               pinctrl-1 = <&bl1_pwm_off_pins>;
 +                pinctrl_version = <2>; /* for uboot */
 +                interrupts = <0 197 1
 +
 +```
 +
 +<tabbox VIM1S>
 +
 +The ''PWM'' function is disabled by default, you need to modify the dts to enable it.
 +
 +```diff
 +--- a/arch/arm64/boot/dts/amlogic/kvim1s.dts
 ++++ b/arch/arm64/boot/dts/amlogic/kvim1s.dts
 +@@ -1411,8 +1411,8 @@
 + };
 + 
 + &pwm_ef {
 +-       /*pinctrl-0 = <&pwm_f_pins2>;*/
 +-       /*pinctrl-names = "default";*/
 ++       pinctrl-0 = <&pwm_f_pins2>;
 ++       pinctrl-names = "default";
 +        status = "okay";
 + };
 +
 +```
 +</tabbox>
 +
 +===== Control PWM =====
 +
 +**Enable PWM:**
 +
 +<tabbox VIM1/3/4> 
 +
 +```shell
 +echo 1 | sudo tee /sys/class/pwm/pwmchip4/export
 +echo 1000000 | sudo tee /sys/class/pwm/pwmchip4/pwm1/period
 +echo 500000 | sudo tee /sys/class/pwm/pwmchip4/pwm1/duty_cycle
 +echo 1 | sudo tee /sys/class/pwm/pwmchip4/pwm1/enable
 +```
 +
 +<tabbox VIM2> 
 +
 +```shell
 +echo 1 | sudo tee /sys/class/pwm/pwmchip1/export
 +echo 1000000 | sudo tee /sys/class/pwm/pwmchip1/pwm1/period
 +echo 500000 | sudo tee /sys/class/pwm/pwmchip1/pwm1/duty_cycle
 +echo 1 | sudo tee /sys/class/pwm/pwmchip1/pwm1/enable
 +```
 +
 +<tabbox VIM3L/1S> 
  
-<tabbox VIM4>  
-%%PWM_F:%% 
 ```shell ```shell
-echo 1 | sudo tee /sys/class/pwm/pwmchip4/export +echo 1 | sudo tee /sys/class/pwm/pwmchip0/export 
-echo 1000000 | sudo tee /sys/class/pwm/pwmchip4/pwm1/period +echo 1000000 | sudo tee /sys/class/pwm/pwmchip0/pwm1/period 
-echo 500000 | sudo tee /sys/class/pwm/pwmchip4/pwm1/duty_cycle +echo 500000 | sudo tee /sys/class/pwm/pwmchip0/pwm1/duty_cycle 
-echo 1 | sudo tee /sys/class/pwm/pwmchip4/pwm1/enable+echo 1 | sudo tee /sys/class/pwm/pwmchip0/pwm1/enable
 ``` ```
  
Line 76: Line 233:
 </WRAP> </WRAP>
  
-===== Disable PWM =====+**Disable PWM:**
  
-<tabbox VIM1/3/3L/4> +<tabbox VIM1/3/4> 
 ```shell ```shell
-echo 0 | sudo tee /sys/class/pwm/pwmchip4/pwm1/enable+echo 0 | sudo tee /sys/class/pwm/pwmchip4/pwm1/enable
 ``` ```
 <tabbox VIM2>  <tabbox VIM2> 
 ```shell ```shell
-echo 0 | sudo tee /sys/class/pwm/pwmchip1/pwm1/enable+echo 0 | sudo tee /sys/class/pwm/pwmchip1/pwm1/enable
 ``` ```
 +<tabbox VIM3L/1S> 
 +```shell
 +echo 0 | sudo tee /sys/class/pwm/pwmchip0/pwm1/enable
 +```
 +
 </tabbox> </tabbox>
 +
 +===== Disable PWM to Use GPIO =====
 +
 +If you want to use normal GPIO instead of PWM, you can remove the PWM node in [[products:sbc:common:configurations:device-tree-overlay|Device Tree Overlay]].
 +
  
  
Last modified: 2022/07/12 06:01 by nick