PWM (Linux)
Introduction
The Toradex SoM families define some pin-compatible PWM channels:
- Colibri: 4 PWMs,
PWM <A>
throughPWM <D>
. ThePWM <A>
is traditionally used for display backlight brightness control. - Apalis: 4 PWMs,
PWM1
throughPWM4
. The internal hardware that generates the signal forPWM4
might not be available for general use if it is used for the backlight brightness control pinBKL1_PWM
. Learn more on Backlight PWM (Linux). - Verdin: up-to 3 PWMs,
PWM_1
,PWM_2
andPWM_3_DSI
. ThePWM_1
is on an always compatible pin, thePWM_2
andPWM_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.
# cd /sys/class/pwm/pwmchip1/
# echo 0 > export
Select the period of the PWM signal. Value is in nanoseconds.
# echo 1000000 > pwm0/period
Select the duty cycle. Value is in nanoseconds and must be less than the period.
# echo 500000 > pwm0/duty_cycle
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
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:
/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
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)
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/ | - |
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/ | - |
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
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 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).
Verdin iMX8M Plus
Toradex Name | NXP/Freescale Name | sysfs path | Note |
---|---|---|---|
PWM_1 | PWM1 | /sys/class/pwm/pwmchip0/ | - |
PWM_2 | PWM2 | /sys/class/pwm/pwmchip1/ | - |
PWM_3_DSI | PWM3 | /sys/class/pwm/pwmchip2/ | Dedicated PWM for display backlight |