i.MX6ULL驱动开发 | 09 -基于Linux自带的LED驱动点亮LED

一、Linux内核自带的LED驱动

Linux内核已经集成了采用 platform 框架编写的LED驱动,无需我们自己编写,只要按照要求在设备树文件中添加相应的LED节点即可。

1. 如何使能

(1)在内核源码目录中,打开 menuconfig 进行配置:

make menuconfig

(2)打开LED驱动配置项

按下?可以打开此选项的帮助信息:

可以看到,如果此选项打开,.config文件中就会开启LEDS_GPIO:

配置改动后,重新编译内核,下载到开发板。

二、设备树节点编写

1. 设备树节点格式

在文档Documentation/devicetree/bindings/leds/leds-gpio.txt中。

(1)需要的属性

  • compatible:应该是"gpio-leds"

(2)描述

Each LED is represented as a sub-node of the gpio-leds device. Each node’s name represents the name of the corresponding LED.

(3)LED子节点的属性

  • 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.

(4)示例

#include <dt-bindings/gpio/gpio.h>

leds {
    
    
	compatible = "gpio-leds";

	charger-led {
    
    
		gpios = <&gpio1 2 GPIO_ACTIVE_HIGH>;
		linux,default-trigger = "max8903-charger-charging";
		retain-state-suspended;
	};
};

其中 "linux,default-trigger"的属性值在 Documentation/devicetree/bindings/leds/common.txt 文件中给出。

Current triggers are:

扫描二维码关注公众号,回复: 13981165 查看本文章
  • “backlight” - LED will act as a back-light, controlled by the framebuffer system
  • “default-on” - LED will turn on (but for leds-gpio see “default-state” property in Documentation/devicetree/bindings/gpio/led.txt)
  • “heartbeat” - LED “double” flashes at a load average based rate
  • “ide-disk” - LED indicates disk activity
  • “timer” - LED flashes at a fixed, configurable rate

2. 编写LED节点——作为普通led灯

(1)添加设备树节点

在自己的设备树描述文件中修改:

leds {
    
    
	compatible = "gpio-leds";

	red {
    
    
		label = "red";
		gpios = <&gpio1 3 GPIO_ACTIVE_LOW>;
		default-state = "off";
	};
};

(2)编译设备树,使用新的设备树启动内核

编译设备树,使用该设备树启动:

make dtbs

启动后查看是否有对应的设备节点:

(3)操作LED

打开LED:

echo 1 > /sys/class/leds/red/brightness

关闭led:

echo 0 > /sys/class/leds/red/brightness

2. 编写LED节点——作为心跳指示灯

在自己的设备树描述文件中添加:

leds {
    
    
	compatible = "gpio-leds";

	heart-led {
    
    
		gpios = <&gpio1 3 GPIO_ACTIVE_LOW>;
		linux,default-trigger = "heartbeat";
	};
};

编译设备树,使用该设备树启动:

make dtbs

启动后查看是否注册的对应总线设备目录是否存在:

同时可以看到LED已经在闪烁,快闪两下,表示系统正在运行。

三、Linux自带LED驱动浅析

1. 如何找到对应的驱动文件?

搜索该宏定义,找对应的Makefile:

grep -nR "CONFIG_LEDS_GPIO" * | grep Makefile


瞬间找到,LED驱动对应的文件为:drivers/leds/leds-gpio.c

2. platform驱动的注册与删除

3. 驱动probe加载函数


在设备树方式中,核心函数是 gpio_leds_create,同样在该驱动模块中实现。

(1)统计子节点数量

一般在设备树中创建一个节点表示LED灯,该节点下的每个子节点都表示一个LED灯,因此子节点数量就是LED灯的数量。

(2)遍历子节点,获取每个子节点的信息

(3)在遍历到每个子节点时,读取子节点labale属性值,使用label属性作为led的名字

(4)读取"linux,default-trigger"属性值,可以通过此属性设置某个LED灯在Linux系统中的默认功能,比如作为心跳指示灯。

(5)获取"default-state"属性值,设置LED灯的默认状态。

(6)创建LED相关的io。

4. 驱动卸载remove函数

5. 驱动兼容性

猜你喜欢

转载自blog.csdn.net/Mculover666/article/details/123999277