Skip to main content
Version: BSP 7.x.y

PWM (Linux)

Introduction

This article will guide you on how to use the sysfs linux interface to control PWM signals on Toradex's System on Modules (SoMs). It also provides guidance on working with GPIO-related drivers and explains how to access GPIO within U-Boot.

Keep in mind that Torizon OS is preferred to control PWM due to its ease of use and smoother application development experience. For controlling PWM pins using Torizon, refer to How to Use PWM on Torizon OS.

tip

The Linux kernel provides an alternative, easier-to-use driver to control the Display Backlight Brightness PWM control signal. See Backlight PWM (Linux) for more information about PWM backlight usage.

Control PWM through Pwmchip

The following instructions apply to all Toradex modules; for specific information on how to use each module's PWM interfaces, please refer to Pwmchip paths on Toradex System on Modules (SoMs).

This section provides generic information on how to use PWM in Linux with the Pwmchip interface.

  1. Go to your desired PWM pin's pwmchip directory. Refer to Specific information about Toradex Computer on Modules (CoM) for information about each pin's directory. This example uses /sys/class/pwm/pwmchip1/.
# cd /sys/class/pwm/pwmchip1/
  1. Write to the export file to select your pin's PWM channel. This example uses channel 0
# echo 0 > export

A pwm<n> directory should appear inside /sys/class/pwm/pwmchip1/, where <n> is the number of the channel you selected.

info

Each PWM controller in the SoC includes multiple channels, each mapped to specific pins. To configure PWM output correctly, you must identify the appropriate channel for your target pin, according to the channel specification for Toradex SoMs:

  • For NXP iMX-based devices: The channel is always 0
  • For TI AMx-based devices: Each pin has a TI equivalent signal name with the format EHRPWMx_A or EHRPWMx_B. You must check the signal name in your device's datasheet and choose the channel with the following criteria:
    • EHRPWMx_A: Channel 0
    • EHRPWMx_B: Channel 1
  1. Select the period of the PWM signal. The period's value is in nanoseconds.
# echo 1000000 > pwm0/period
  1. Select the duty cycle of the PWM signal. The duty cicle's value is in nanoseconds and must be less than the period.
# echo 500000 > pwm0/duty_cycle
  1. Select the polarity of the PWM signal (you can use "normal" or "inversed"). The polarity can only be changed if the PWM is not enabled.
# echo "normal" > pwm0/polarity
  1. Enable/disable the PWM signal, use 1 or 0 respectively:
# echo 1 > pwm0/enable

Control PWM through the LED driver

You can use the PWM LED driver which allows controlling LED's brightness through PWM. The LED framework has also the advantage that triggers can be configured. Triggers allow to let a LED be controlled by kernel events such as NAND flash access.

Make sure the kernel configuration CONFIG_LEDS_PWM resp. CONFIG_LEDS_GPIO is enabled.

In the example below using a Verdin iMX8M Plus, PWM_1 was configured adding the following device tree overlay:

verdin-imx8mp_pwm_led.dts
/dts-v1/;
/plugin/;

/ {
compatible = "toradex,verdin-imx8mp";
};

&pwm1 {
status = "okay";
};

&{/} {
led-controller {
compatible = "pwm-leds";
led-1 {
label = "verdin_status_led";
pwms = <&pwm1 0 5000000 0>;
max-brightness = <255>;
};
};
};

The brightness can then be configured using sysfs:

echo 120 > /sys/class/leds/verdin_status_led/brightness

To let the LED be controlled by the kernel itself, triggers can be used to control the LED:

echo nand-disk > /sys/class/leds/verdin_status_led/trigger
note

There is also a GPIO LED driver, which might be more appropriate for LED's which are only used in an on/off configuration (e.g. triggers).

Pwmchip paths on Toradex System on Modules (SoMs)

The tables below list the the /sys/class/pwm/pwmchip<n> paths of each PWM pin for Toradex SoMs, check the correspondent path for your own device.

The values shown here are only valid for Toradex's unmodified Device Trees. If you create a custom Device Tree for your module or apply a Device Tree Overlay, the pwmchip<n> numbers could be different. This happnes because the kernel mounts the PWM interfaces in order as the Device Tree is applied at the system startup.

If you modify your module's Device Tree, and the PWM numbers change, run the command below to list the memory addresses of the pwmchip<n> interfaces, and compare them with the addresses of the PWM nodes of the device tree.

$ ls -l /sys/class/pwm
warning

The Display Backlight dedicated PWM pins cannot be controlled via the pwmchip interface by default, since they are used by the backlight driver. To use the dedicated pins as regular PWM sources, disable the pwm-backlight functionality on the device tree (refer to Device Tree Customization).

Verdin iMX8M Mini

Toradex NameNXP/Freescale Namesysfs pathNote
PWM_1PWM2/sys/class/pwm/pwmchip1/-
PWM_2PWM3/sys/class/pwm/pwmchip2/-
PWM_3_DSIPWM1/sys/class/pwm/pwmchip0/Dedicated PWM for display backlight

Verdin iMX8M Plus

Toradex NameNXP/Freescale Namesysfs pathNote
PWM_1PWM1/sys/class/pwm/pwmchip0/-
PWM_2PWM2/sys/class/pwm/pwmchip1/-
PWM_3_DSIPWM3/sys/class/pwm/pwmchip2/Dedicated PWM for display backlight

Verdin AM62

Toradex NameTI Namesysfs pathNote
PWM_1EHRPWM0_A/sys/class/pwm/pwmchip0/-
PWM_2EHRPWM0_B/sys/class/pwm/pwmchip0/-
PWM_3_DSIEHRPWM1_A/sys/class/pwm/pwmchip2/Dedicated PWM for display backlight

Verdin AM62P

Toradex NameTI Namesysfs pathNote
PWM_1EHRPWM2_B/sys/class/pwm/pwmchip0/-
PWM_2EHRPWM2_A/sys/class/pwm/pwmchip0/-
PWM_3_DSIEHRPWM0_B/sys/class/pwm/pwmchip2/Dedicated PWM for display backlight
Send Feedback!