Controlling LED brightness via PWM

Hi. I have a LED connected to the GPIO7 pin which is GPIO3_PV.00. The necessary changes have been made to the device tree using the Pinmux Configuration Sheet. The changes have been flashed to the system using the generated .dtsi files.

This is the node for it from the pinmux .dtsi file:

lcd_bl_pwm_pv0 {
				nvidia,pins = "lcd_bl_pwm_pv0";
				nvidia,function = "pwm0";
				nvidia,pull = <TEGRA_PIN_PULL_NONE>;
				nvidia,tristate = <TEGRA_PIN_DISABLE>;
				nvidia,enable-input = <TEGRA_PIN_DISABLE>;
			};

Is it possible to control the PWM pin via userspace with the above changes? What commands should I use?

I also found this: https://www.kernel.org/doc/Documentation/devicetree/bindings/leds/leds-pwm.txt
This binding shows these two nodes:

twl_pwm: pwm {
	/* provides two PWMs (id 0, 1 for PWM1 and PWM2) */
	compatible = "ti,twl6030-pwm";
	#pwm-cells = <2>;
};

twl_pwmled: pwmled {
	/* provides one PWM (id 0 for Charing indicator LED) */
	compatible = "ti,twl6030-pwmled";
	#pwm-cells = <2>;
};

The above two nodes are then used here in pwms property:

pwmleds {
	compatible = "pwm-leds";
	kpad {
		label = "omap4::keypad";
		pwms = <&twl_pwm 0 7812500>;
		max-brightness = <127>;
	};

	charging {
		label = "omap4:green:chrg";
		pwms = <&twl_pwmled 0 7812500>;
		max-brightness = <255>;
	};
};

twl_pwm uses the following compatible string: compatible = "ti,twl6030-pwm";
Whereas, twl_pwmled uses: compatible = "ti,twl6030-pwmled";

I am not quite sure how to integrate this into my device tree though.

hello surfinride,

had you refer to this topic, Topic 178959 for using Nano PWM?
you may also probe the pin to ensure the PWM periods / duty_cycle configurations has applied correctly.
thanks

Hi. Thanks for the reply. The PWM pin works as expected now. Earlier in the pinmux configuration sheet, I had not selected the PWM functionality for the pin. So in the pinmux dtsi, the pin function was reserved nvidia,function = "rsvd3";. But after selecting the PWM functionality, the pinmux dtsi shows nvidia,function = "pwm0";.

I wanted to have the LED light up with a certain brightnedd on boot up. So I added this node to the tegra210-p3448-0002-p3449-0000-b00.dts:

bcled: pwm_ctrl_led {
    status = "okay";
    pwms = <&{/pwm@7000a000} 0 5000000>;
    pwm-names = "pwm_ctrl_led";
};

I added the above node based on these device tree bindings from the kernel: Link 1 and Link 2

But $ cat /sys/kernel/debug/pwm shows:

# cat /sys/kernel/debug/pwm
platform/70110000.pwm, 1 PWM device
 pwm-0   (pwm-regulator       ): requested enabled period: 2500 ns duty: 0 ns polarity: normal

platform/7000a000.pwm, 4 PWM devices
 pwm-0   ((null)              ): period: 0 ns duty: 0 ns polarity: normal
 pwm-1   (pwm-regulator       ): requested period: 8000 ns duty: 1440 ns polarity: normal
 pwm-2   ((null)              ): period: 0 ns duty: 0 ns polarity: normal
 pwm-3   (pwm-fan             ): requested enabled period: 45334 ns duty: 0 ns polarity: normal

As you can see, the period mentioned in the bcled node is not reflected like pwm-fan. Instead I have to use the below commands to make the LED work:

# cd /sys/class/pwm/
# echo 0 > pwmchip0/export
# echo 10000 > pwmchip0/pwm0/period
# echo 2000 > pwmchip0/pwm0/duty_cycle
# echo 1 > pwmchip0/pwm0/enable

Is there a way to have the period and duty-cycle set via the device tree?

hello surfinride,

please check you’re having this driver kernel/kernel-4.9/drivers/leds/leds-pwm.c to make LED works.
if yes, there’re device tree properties to configure the LED.
for example,

160  		of_property_read_u32(child, "max-brightness",
161  				     &led.max_brightness);
162  		of_property_read_u32(child, "default-brightness",
163  				     &led.default_brightness);

Hi Jerry,

I modified the node based on the examples in the device tree bindings to use pwm-leds and added the max_brightness and default_brightness properties to it. I recompiled the image and flashed it to the Nano.
Node:

pwmleds {
    compatible = "pwm-leds";
    status = "okay";

	bc_led {
	    pwms = <&{/pwm@7000a000} 0 5000000>;
    	    pwm-names = "bc_led";
	    max-brightness = <255>;
	    default-brightness = <255>;
	};
};

On boot up the led was lit. $ cat /sys/kernel/debug/pwmshows:

platform/70110000.pwm, 1 PWM device
 pwm-0   (pwm-regulator       ): requested enabled period: 2500 ns duty: 0 ns polarity: normal

platform/7000a000.pwm, 4 PWM devices
 pwm-0   (bc_led   ): requested enabled period: 5000000 ns duty: 5000000 ns polarity: normal
 pwm-1   (pwm-regulator       ): requested period: 8000 ns duty: 1440 ns polarity: normal
 pwm-2   ((null)              ): period: 0 ns duty: 0 ns polarity: normal
 pwm-3   (pwm-fan             ): requested enabled period: 45334 ns duty: 0 ns polarity: normal

I can change the brightness using: /sys/class/leds/bc_led# echo 127 > brightness

Remember that your kernel config should have CONFIG_LEDS_PWM=y. And its probably not required, but CONFIG_LEDS_GPIO=y was also set.

Thanks for the help.

hello surfinride,

thanks for sharing the results, are we able to close this discussion thread?

Hi Jerry,

Please go ahead close the thread.

Thank you!

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.