Zephyr EC PWM Configuration

Overview

PWM provides support for PWM setup and control on the platform.

Kconfig Options

Kconfig OptionDefaultDocumentation
CONFIG_PWMnPWM (Pulse Width Modulation) Drivers
CONFIG_PWM_<platform>nPlatform specific PWM driver
Kconfig sub-optionDefaultDocumentation
CONFIG_PLATFORM_EC_PWM_INIT_PRIORITY51Init priority of the PWM module

Devicetree Nodes

PWM channels are configured by enabling the PWM subsystem in the project prj.conf, setting a phandle reference to higher level driver pwms property, and enabling and configuring the corresponding PWM controller device node.

For example for a keyboard backlight device:

kblight {
        compatible = "cros-ec,kblight-pwm";
        pwms = <&pwm3 0 PWM_HZ(2400) PWM_POLARITY_NORMAL>;
};
PropertyDescriptionSettings
pwmsPWM phandle, identifies the controller (X), channel (Y) and flags.<&pwmX Y flags>
frequencyPWM frequency, in Hzinteger (32 bit unsigned)

The flags cell of pwms defines the PWM signal properties, valid options are listed in the dt-bindings/pwm/pwm.h, commonly used ones are PWM_POLARITY_NORMAL and PWM_POLARITY_INVERTED.

Any used platform PWM device has to be explicitly enabled and configured, for example:

&pwm3 {
	status = "okay";
	clock-bus = "NPCX_CLOCK_BUS_LFCLK"; /* Keep active during low power mode. */
};

The device node may include any platform specific property, see the corresponding bindings files for more details (nuvoton,npcx-pwm, ite,it8xxx2-pwm).

Board Specific Code

None required.

Threads

PWM support does not enable any thread.

Testing and Debugging

Shell Command

Zephyr defines a pwm shell command that can be used to change the current PWM duty cycle and period.

The command is enabled by setting CONFIG_PWM_SHELL=y (CONFIG_PWM_SHELL) in the project prj.conf file.

CommandDescriptionUsage
pwmPWM shell commandspwm <cycles or usec or nsec> <device> <pwm> <period> <pulse> [flags]
ParameterDescription
cycles/usec/nsecThe unit of period and pulse (cycles, microseconds or nanoseconds).
deviceThe PWM device label (for example PWM_1).
pwmThe PWM channel number.
periodRequested period of the PWM signal.
pulseRequested active cycle duration of the PWM signal.
flagsPWM flags (for example 1 for inverted polarity).

For example to set the keyboard backlight (connected to PWM3) to 90% at 250Hz on Volteer:

$ pwm usec PWM_3 0 250 225

Features using PWM directly

Keyboard backlight

Kconfig OptionDefaultDocumentation
PLATFORM_EC_PWM_KBLIGHTnPWM keyboard backlight

This requires defining a node with compatible = "cros-ec,kblight-pwm", normally in a separate overlay keyboard.dts file, for example on the Volteer reference board:

PWM Example

The keyboard backlight line is connected to PWM3 channel 0 (there's only one channel per PWM on NPCX), normal polarity:

/ {
        kblight {
                compatible = "cros-ec,kblight-pwm";
                pwms = <&pwm3 0 PWM_HZ(2400) PWM_POLARITY_NORMAL>;
        };
};

&pwm3 {
        status = "okay";
};