一、测试环境:Android 9 系统
二、驱动文件在\kernel\drivers\leds\leds-gpio.c
三、dts的官方配置文档 kernel\Documentation\devicetree\bindings\leds\leds-gpio.txt
LEDs connected to GPIO lines
Required properties:
- compatible : should be "gpio-leds".
Each LED is represented as a sub-node of the gpio-leds device. Each
node's name represents the name of the corresponding LED.
LED sub-node properties:
- gpios : Should specify the LED's GPIO, see "gpios property" in
Documentation/devicetree/bindings/gpio/gpio.txt. Active low LEDs should be
indicated using flags in the GPIO specifier.
- label : (optional)
see Documentation/devicetree/bindings/leds/common.txt
- linux,default-trigger : (optional)
see Documentation/devicetree/bindings/leds/common.txt
- default-state: (optional) The initial state of the LED. Valid
values are "on", "off", and "keep". If the LED is already on or off
and the default-state property is set the to same value, then no
glitch should be produced where the LED momentarily turns off (or
on). The "keep" setting will keep the LED at whatever its current
state is, without producing a glitch. The default is off if this
property is not present.
- retain-state-suspended: (optional) The suspend state can be retained.Such
as charge-led gpio.
Examples:
#include <dt-bindings/gpio/gpio.h>
leds {
compatible = "gpio-leds";
hdd {
label = "IDE Activity";
gpios = <&mcu_pio 0 GPIO_ACTIVE_LOW>;
linux,default-trigger = "ide-disk";
};
fault {
gpios = <&mcu_pio 1 GPIO_ACTIVE_HIGH>;
/* Keep LED on if BIOS detected hardware fault */
default-state = "keep";
};
};
run-control {
compatible = "gpio-leds";
red {
gpios = <&mpc8572 6 GPIO_ACTIVE_HIGH>;
default-state = "off";
};
green {
gpios = <&mpc8572 7 GPIO_ACTIVE_HIGH>;
default-state = "on";
};
};
leds {
compatible = "gpio-leds";
charger-led {
gpios = <&gpio1 2 GPIO_ACTIVE_HIGH>;
linux,default-trigger = "max8903-charger-charging";
retain-state-suspended;
};
};
四、我这里的dts配置是
leds {
compatible = "gpio-leds";
topdon_led {
default-state = "off";
gpios = <&gpio0 12 GPIO_ACTIVE_HIGH>;
pinctrl-names = "default";
linux,default-trigger = "heartbeat";
pinctrl-0 = <&led1_ctl>;
status = "okay";
};
topdon_buzzer {
default-state = "on";
gpios = <&gpio4 22 GPIO_ACTIVE_LOW>;
pinctrl-names = "default";
pinctrl-0 = <&beep_gpio>;
status = "okay";
};
};
五、测试中发现如果配置的gpio已经被占用了,在/sys/class/leds不会生成相应的节点。
六、运行后可以看到相应的gpio被注册申请。
七、这个时候执行echo 0 >/sys/bus/platform/devices/leds/leds/topdon_led/brightness 命令led灯会有变化
八、这里有个问题,设置default-trigger = "heartbeat"没有效果,执行cat trigger也看不到相关选项。
8.1、cat trigger 没有看到有heartbeat。
8.2、解决方法:勾选相应的选项
8.3、按上面修改之后,就ok了
九、定时器timer的使用, echo timer >trigger切换到定时器功能,然后通过写delay_on和delay_off来修改频率,我这样修改后led以10HZ的频率在闪烁。
十、有的平台在/sys/devices/platform/gpio-leds/leds这个路径
十一、参考文章
(686条消息) linux驱动之LED实验(基于系统自带leds-gpio.c驱动)_金城孤客的博客-CSDN博客https://blog.csdn.net/fengweibo112/article/details/102744366(685条消息) Linux驱动开发学习笔记【12】:Linux自带LED灯驱动_hurryddd的博客-CSDN博客_linux,default-triggerhttps://blog.csdn.net/m0_37845735/article/details/107328682?spm=1001.2101.3001.6650.1&utm_medium=distribute.pc_relevant.none-task-blog-2~default~CTRLIST~Rate-1-107328682-blog-102744366.pc_relevant_aa&depth_1-utm_source=distribute.pc_relevant.none-task-blog-2~default~CTRLIST~Rate-1-107328682-blog-102744366.pc_relevant_aa&utm_relevant_index=1