TS-7970 PWM

From embeddedTS Manuals

The CPU exposes one PWM on HD1_IRQ (HD1 pin 21). By default, this pin is a GPIO which is used as an interrupt for daughter cards. To use this as PWM a modification to the device tree will be required. See the #Compile the Kernel section to get a build environment working. The file "arch/arm/boot/dts/imx6qdl-ts7970.dtsi" will need to be modified.

First the pin must be configured to be PWM and not a GPIO. Find the line:

 MX6QDL_PAD_DISP0_DAT9__GPIO4_IO30	0x1b088 /* HD1_IRQ */

and remove it.

Add the "pinctrl_pwm2" block below as a new section in the "imx6-ts7970" section:

iomuxc {
	pinctrl-names = "default";
	pinctrl-0 = <&pinctrl_hog>;

	imx6-ts7970 {
		pinctrl_pwm2: pwm2grp {
			fsl,pins = <
				MX6QDL_PAD_DISP0_DAT9__PWM2_OUT		0x1b088

The "MX6QDL_PAD_DISP0_DAT9__PWM2_OUT" pad name comes from "imx6q-pinfunc.h" or "imx6dl-pinfunc.h" files. The format follows "MX6QDL_PAD_<CPU Pad Name>__<Function of that pad>". This specifies that the pad "DISP0_DAT9" to be used as PWM2's output.

At the end of the file, add the following block which enables the PWM controller and connects the iomuxc pinctrl created above:

&pwm2 {
	pinctrl-names = "default";
	pinctrl-0 = <&pinctrl_pwm2>;
	status = "okay";

After rebuilding and installing this updated device tree, the directory "/sys/class/pwm/pwmchip0/" will be available to control the PWM as outlined below.

# Each PWM controller has "1" pwm which will be pwm channel 0
echo 0 > /sys/class/pwm/pwmchip0/export

This will create a "pwm0/" directory under "pwmchip0/" which has the following files:

period Total period, inactive and active time in the PWM cycle specified in nanoseconds.
duty_cycle Active time of the PWM signal specified in nanoseconds. Must be less than the period.
enable Write 1 to enable, 0 to disable
polarity When the pwm is disabled this can be written as "normal" for default behavior or "inversed" to invert the signal.

As an example, this will set a 50 khz signal with a 50% duty cycle.

# 20us is the period for 50khz
echo 20000 > /sys/class/pwm/pwmchip0/pwm0/period
echo 10000 > /sys/class/pwm/pwmchip0/pwm0/duty_cycle
echo 1 > /sys/class/pwm/pwmchip0/pwm0/enable

A common use of the PWM is for backlight control which is specified in the device tree. Our TS-TPC-8390 baseboard device tree can be used as an example.

In addition to setting up the PWM as demonstrated above, the PWM is connected to the pwm-backlight driver.

	backlight_lcd {
		compatible = "pwm-backlight";
		pwms = <&pwm3 0 5000000>;
		brightness-levels = <0 128 140 160 180 200 220 240 255>;
		default-brightness-level = <8>;
		power-supply = <&reg_3p3v>;

This specifies PWM3 to run with 5000000 ns periods (200hz) with duty cycles specified in brightness-levels up to 255. This example creates 8 levels of brightness for the backlight, but more duty cycles can be added if more levels are needed.