Search by Tags

PWM (Linux)

 

Article updated at 29 Oct 2021
Compare with Revision




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

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.

# cd /sys/class/pwm/pwmchip1/
# echo 0 > export

Select the period of the PWM signal. Value is in nanoseconds.

# echo 1000000 > pwmchip1/pwm0/period

Select the duty cycle. Value is in nanoseconds and must be less than the period.

# echo 500000 > pwm0/duty_cycle

Enable/disable the PWM signal, use 1 or 0 respectively:

# echo 1 > pwm0/enable

Change 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

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 Colibri VF61, PWM <D> was configured adding the following node to the device tree (see Device Tree Customization for more information on device trees):

    pwmleds {
        compatible = "pwm-leds";
        led1 {
            label = "vf610:green:led1";
            pwms = <&pwm1 1 5000000 0>;
            max-brightness = <255>;
        };
    };

The brightness can then be configured using sysfs:

echo 120 > /sys/class/leds/vf610\:green\:led1/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/vf610\:green\:led1/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).

Here is an example of how to access and manage brightness through LEDs interface:

# cd /sys/class/leds
# ls
PWM1    PWM2    PWM3    mmc0::  mmc1::  mmc2::
# cd PWM1/
# ls
brightness      device          max_brightness  power           subsystem       uevent
# echo 127 > brightness  

Specific information about Toradex Computer on Modules (CoM)

Note: 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 Name NXP/Freescale Name sysfs path Note
PWM 1 PWM 2 /sys/class/pwm/pwmchip2/ -
PWM 2 PWM 3 /sys/class/pwm/pwmchip3/ -
PWM 3 PWM 0 /sys/class/pwm/pwmchip0/ -
PWM 4 PWM 1 /sys/class/pwm/pwmchip1/ -

Apalis iMX8X

Note: Apalis iMX8X is phased out, and it is not available for purchase anymore. The latest supported BSP and TorizonCore version is 5.4.0.

Toradex Name NXP/Freescale Name sysfs path Note
PWM 1 PWM 2 /sys/class/pwm/pwmchip2/ -
PWM 2 MIPI_DSI0_PWM0 /sys/class/pwm/pwmchip0/ -
PWM 3 n.A. - No PWM available
PWM 4 n.A. - No PWM available

Colibri iMX8X

Toradex Name NXP/Freescale Name sysfs path Note
PWM_A ADMA_LCD_PWM0 /sys/class/pwm/pwmchip0/ Used for backlight control (/sys/class/backlight/backlight/)
PWM_B LSIO_PWM0 /sys/class/pwm/pwmchip1/ -
PWM_C LSIO_PWM1 /sys/class/pwm/pwmchip2/ -
PWM_D LSIO_PWM2 /sys/class/pwm/pwmchip3/ -

Colibri iMX7

Toradex Name NXP/Freescale Name sysfs path Note
PWM_A PWM1 /sys/class/pwm/pwmchip0/ Used for backlight control (/sys/class/backlight/backlight/)
PWM_B PWM2 /sys/class/pwm/pwmchip1/ -
PWM_C PWM3 /sys/class/pwm/pwmchip2/ -
PWM_D PWM4 /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 Name NXP/Freescale Name sysfs path Note
PWM1 PWM1 /sys/class/pwm/pwmchip0/ -
PWM2 PWM2 /sys/class/pwm/pwmchip1/ -
PWM3 PWM3 /sys/class/pwm/pwmchip2/ -
PWM4 PWM4 /sys/class/backlight/backlight.17/ Used for backlight control

Colibri iMX6

Toradex Name NXP/Freescale Name sysfs path Note
PWM_A PWM3 /sys/class/pwm/pwmchip2/ Used for backlight control (/sys/class/backlight/backlight/)
PWM_B PWM1 /sys/class/pwm/pwmchip0/ -
PWM_C PWM4 /sys/class/pwm/pwmchip3/ -
PWM_D PWM2 /sys/class/pwm/pwmchip1/ -

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

Colibri iMX6ULL

Toradex Name NXP/Freescale Name sysfs path Note
PWM_A PWM4 /sys/class/pwm/pwmchip0/ Used for backlight control (/sys/class/backlight/backlight/)
PWM_B PWM5 /sys/class/pwm/pwmchip1/ -
PWM_C PWM6 /sys/class/pwm/pwmchip2/ -
PWM_D PWM7 /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

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

Apalis TK1

On TK1 all PWM instances are covered by one single PWM chip abstraction.

Toradex Name Nvidia TK1 Name sysfs path Note
PWM1 PWM0 /sys/class/pwm/pwmchip0/ -
PWM2 PWM1 /sys/class/pwm/pwmchip0/ -
PWM3 PWM2 /sys/class/pwm/pwmchip0/ -
PWM4 PWM3 /sys/class/pwm/pwmchip0/ Used for backlight control (sys/class/backlight/pwm-backlight/)

Note that PWM3 is used by the PWM backlight driver by default. If you want to use PWM3 instance 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:

Colibri T20/T30 and Apalis T30

For our Tegra based modules, the PWM LED interface is used to export the PWM channels PWM<B> through PWM<D>, located at /sys/class/leds/.

To control the duty cycle, the sysfs brightness file can be used. Value is within the range 0 to 255, where 0 means 0% duty cycle while 255 means 100% duty cycle.

echo [value] > /sys/class/leds/[pwm_instance]/brightness
e.g.:
echo 127 > /sys/class/leds/PWM<B>/brightness

Note: leds-pwm driver has only support for controlling the duty cycle through sysfs, period is fixed and cannot be configured through sysfs.

Colibri VFxx

The Vybrid BSP is making use of the kernel's PWM framework. PWMs are exposed at /sys/class/pwm/

Toradex Name NXP/Freescale Name sysfs path Note
PWM_A FTM0_0 /sys/class/backlight/backlight/ Used for backlight control
PWM_B FTM1_0 /sys/class/pwm/pwmchip8/pwm0/ -
PWM_C FTM0_1 /sys/class/pwm/pwmchip0/pwm1/ -
PWM_D FTM1_1 /sys/class/pwm/pwmchip8/pwm1/ -

Note: By default PWM_A is consumed by the backlight driver, hence exporting PWM0_0 fails with an error:

root@colibri-vf:~# echo 0 > export
-sh: echo: write error: Device or resource busy

If you don't use the backlight capabilities and want to use PWM_A through the PWM interface, you have to remove the backlight node in the board level device tree (see also the Device Tree Customization article). For temporarily purposes, you can unbind the driver using

root@colibri-vf:~# echo backlight > /sys/bus/platform/drivers/pwm-backlight/unbind

Note 2: The NXP/Freescale FTM controller supports only a single period for all PWM channels, we cannot use different period values for channels on same FTM controller.

By default, the backlight PWM channel (PWM_A, FTM0_0) is used with a period of 1000000 nsec, so PWM_C (FTM0_1) cannot be configured with a different period.

For more information refer: pwm.txt

Verdin iMX8M Mini

Toradex Name NXP/Freescale Name sysfs path Note
PWM_1 PWM2 /sys/class/pwm/pwmchip1/ -
PWM_2 PWM3 /sys/class/pwm/pwmchip2/ -
PWM_3_DSI PWM1 /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).

Legacy

BSP version older than V2.7

The PWM LED interface is used to export the PWM channels.

To control the duty cycle, the sysfs brightness file can be used. Value is within the range 0 to 255, where 0 means 0% duty cycle while 255 means 100% duty cycle.

Apalis iMX6

Toradex Name NXP/Freescale Name sysfs path Note
PWM1 PWM1 /sys/class/leds/PWM1/ -
PWM2 PWM2 /sys/class/leds/PWM2/ -
PWM3 PWM3 /sys/class/leds/PWM3/ -
PWM4 PWM4 /sys/class/backlight/backlight.17/ Used for backlight control

Colibri iMX6

Toradex Name NXP/Freescale Name sysfs path Note
PWM_A PWM3 /sys/class/backlight/backlight.15/ Used for backlight control
PWM_B PWM1 /sys/class/leds/PWM<B>/ -
PWM_C PWM4 /sys/class/leds/PWM<C>/ -
PWM_D PWM2 /sys/class/leds/PWM<D>/ -

Note: leds-pwm driver has only support for controlling the duty cycle through sysfs, period is fixed and cannot be configured through sysfs.