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

PWM (Linux)

Introduction​

The Toradex SoM families define some pin-compatible PWM channels:

  • Colibri: 4 PWMs, PWM <A> through PWM <D>. The PWM <A> is traditionally used for display backlight brightness control.
  • Apalis: 4 PWMs, PWM1 through PWM4. The internal hardware that generates the signal for PWM4 might not be available for general use if it is used for the backlight brightness control pin BKL1_PWM. Learn more on Backlight PWM (Linux).
  • Verdin: up-to 3 PWMs, PWM_1, PWM_2 and PWM_3_DSI. The PWM_1 is on an always compatible pin, the PWM_2 and PWM_3_DSI are on reserved pins. Learn more about this on the Verdin Family Specification.

The Linux kernel supports PWM display backlight brightness using a dedicated driver, hence the PWM channel used for backlight brightness control is made available to user-space in a different manner than the other PWM channels. See Backlight PWM (Linux) for more information about PWM backlight usage.

Up until around Linux Kernel version ~3.6 there has not been a dedicated PWM interface to user-space. Instead, our BSP made use of the PWM LED interface to export the PWM channels to user-space.

Torizon​

The concept from this article also applies to Torizon, since it is based on the Toradex BSP Layers. Nevertheless, there is a dedicated article explaining How to Use PWM With Torizon OS.

How to use PWM in Linux through sysfs​

These instructions were made to work with any Toradex CoM, for specific information about each of them and which interface you will use, please refer to Specific information section.

Using Pwmchip interface​

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

Using Backlight interface​

For information on how to access and use the backlight interface, please refer to Backlight (Linux).

Using PWM LED's interface​

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

Specific information about Toradex Computer on Modules (CoM)​

info

Linux will mount the pwmchip interfaces as they are picked up from the device tree starting at 0 and they could show in a different order than the one shown here. We recommend checking the bus address correspondence between the device tree and in sysfs (You can quickly check the bus address of all PWM devices with ls -l /sys/class/pwm)

Apalis iMX8​

Toradex NameNXP/Freescale Namesysfs pathNote
PWM 1PWM 2/sys/class/pwm/pwmchip2/-
PWM 2PWM 3/sys/class/pwm/pwmchip3/-
PWM 3PWM 0/sys/class/pwm/pwmchip0/-
PWM 4PWM 1/sys/class/pwm/pwmchip1/-

Colibri iMX8X​

Toradex NameNXP/Freescale Namesysfs pathNote
PWM_AADMA_LCD_PWM0/sys/class/pwm/pwmchip0/Used for backlight control (/sys/class/backlight/backlight/)
PWM_BLSIO_PWM0/sys/class/pwm/pwmchip1/-
PWM_CLSIO_PWM1/sys/class/pwm/pwmchip2/-
PWM_DLSIO_PWM2/sys/class/pwm/pwmchip3/-

Colibri iMX7​

Toradex NameNXP/Freescale Namesysfs pathNote
PWM_APWM1/sys/class/pwm/pwmchip0/Used for backlight control (/sys/class/backlight/backlight/)
PWM_BPWM2/sys/class/pwm/pwmchip1/-
PWM_CPWM3/sys/class/pwm/pwmchip2/-
PWM_DPWM4/sys/class/pwm/pwmchip3/-

Note that PWM1 is used by the PWM backlight driver by default. If you want to use that PWM channel as regular PWM, you have to alter the device tree and disable the pwm-backlight driver (see Device Tree Customization). To control the backlight, one can use the backlight sysfs interface.

For more information refer: pwm.txt

Apalis iMX6​

Starting with BSP version V2.7 we use the generic sysfs PWM framework to give access to PWMs from userspace.

Toradex NameNXP/Freescale Namesysfs pathNote
PWM1PWM1/sys/class/pwm/pwmchip0/-
PWM2PWM2/sys/class/pwm/pwmchip1/-
PWM3PWM3/sys/class/pwm/pwmchip2/-
PWM4PWM4/sys/class/backlight/backlight.17/Used for backlight control

Colibri iMX6​

Toradex NameNXP/Freescale Namesysfs pathNote
PWM_APWM3/sys/class/pwm/pwmchip2/Used for backlight control (/sys/class/backlight/backlight/)
PWM_BPWM1/sys/class/pwm/pwmchip0/-
PWM_CPWM4/sys/class/pwm/pwmchip3/-
PWM_DPWM2/sys/class/pwm/pwmchip1/-
info

In older BSP versions the PWM3 backlight sysfs path was as follows: /sys/class/backlight/backlight.15/.

Colibri iMX6ULL​

Toradex NameNXP/Freescale Namesysfs pathNote
PWM_APWM4/sys/class/pwm/pwmchip0/Used for backlight control (/sys/class/backlight/backlight/)
PWM_BPWM5/sys/class/pwm/pwmchip1/-
PWM_CPWM6/sys/class/pwm/pwmchip2/-
PWM_DPWM7/sys/class/pwm/pwmchip3/-

Note that PWM_A is used by the PWM backlight driver by default. If you want to use PWM_A channel as regular PWM, you have to alter the device tree and disable the pwm-backlight driver (see Device Tree Customization). To control the backlight, one can use the backlight sysfs interface.

For more information refer: pwm.txt

info

These values apply to BSP 5. In BSP 3, the (unusable) interfaces are mounted before so PWM_A will be at pwmchip3 (and so on).

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

Note that PWM_3_DSI is used by the PWM backlight driver by default. If you want to use that PWM channel as regular PWM, you have to alter the device tree and disable the pwm-backlight driver (see Device Tree Customization).

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_1EPWM0/sys/class/pwm/pwmchip0/-
PWM_2EPWM1/sys/class/pwm/pwmchip0/-
PWM_3_DSIEPWM2/sys/class/pwm/pwmchip2/Dedicated PWM for display backlight

Aquila AM69​

Toradex NameTI Namesysfs pathNote
PWM_1EPWM0/sys/class/pwm/pwmchip0/-
PWM_2EPWM3/sys/class/pwm/pwmchip4/-
PWM_3_DSIEPWM5/sys/class/pwm/pwmchip6/Dedicated PWM for display backlight
PWM_4_DPEPWM2/sys/class/pwm/pwmchip2/Dedicated PWM for display port


Send Feedback!