linux设备驱动 -- RK3568 led驱动 (led子系统&设备树)

查看原理图


查看原理图,根据引脚分配表,找到led的gpio控制引脚。

led子系统

LED驱动可以直接采用gpio子系统或led子系统两种方式控制。此处采用led子系统,要确保内核启用了led子系统。

配置使能LED子系统:Device Drivers --> LEDs support

内核中设备树参考文档:kernel/Documentation/devicetree/bindings/leds/leds-gpio.txt

编写设备树节点

复制代码
leds: leds {
	compatible = "gpio-leds";
	work_led: work {
		label = "work_led";
		gpios = <&gpio0 RK_PC0 GPIO_ACTIVE_HIGH>;
		linux,default-trigger = "heartbeat";
	};
	run_led: run {
		label = "run_led";
		gpios = <&gpio0 RK_PA5 GPIO_ACTIVE_HIGH>;
		linux,default-trigger = "heartbeat";
	};
};
	
gv_leds: gv-leds {
	compatible = "gpio-leds";
	pinctrl-names = "default";
	pinctrl-0 = <&gv_leds_gpio_fun>;
	status = "okay";
	
	led1_u: led1-u {
		label = "led1_up";
		gpios = <&gpio0 RK_PB0 GPIO_ACTIVE_HIGH>;
		default-state = "off";
	};

	led1_d: led1-d {
		label = "led1_down";
		gpios = <&gpio0 RK_PB5 GPIO_ACTIVE_HIGH>;
		/* Keep LED on if BIOS detected hardware fault */
		default-state = "off";
	};

	led2_u: led2-u {
		label = "led2_up";
		gpios = <&gpio0 RK_PB6 GPIO_ACTIVE_HIGH>;
		/* Keep LED on if BIOS detected hardware fault */
		default-state = "off";
	};

	 led2_d: led2-d {
		label = "led2_down";
		gpios = <&gpio0 RK_PC5 GPIO_ACTIVE_HIGH>;
		/* Keep LED on if BIOS detected hardware fault */
		default-state = "off";
	};

	led_rgb_1: led-rgb-1 {
		label = "led_rgb_1";
		gpios = <&gpio1 RK_PB0 GPIO_ACTIVE_HIGH>;
		/* Keep LED on if BIOS detected hardware fault */
		default-state = "off";
	};

	led_rgb_2: led-rgb-2 {
		label = "led_rgb_2";
		gpios = <&gpio1 RK_PB1 GPIO_ACTIVE_HIGH>;
		/* Keep LED on if BIOS detected hardware fault */
		default-state = "off";
	};

	led_rgb_3: led-rgb-3 {
		label = "led_rgb_3";
		gpios = <&gpio1 RK_PB2 GPIO_ACTIVE_HIGH>;
		/* Keep LED on if BIOS detected hardware fault */
		default-state = "off";
	};
};
  • compatible属性:设备树和led子系统匹配的属性,表示这些led使用led子系统。

  • pinctrl-names属性:指定在不同状态下使用不同的引脚配置,这里是default,也就是pinctrl-0指定的属性。

  • pinctrl-0:pinctrl-names为default值时的引脚配置,这里值是gv_leds_gpio_fun。

  • status:表示使能该节点,若想禁用设置修改okay为disable即可。

  • label属性:系统将根据led子节点中的label属性在sysfs中生成相应的目录和设备文件。

  • gpios属性: 该属性指定了控制此led的是哪一个引脚。

  • 更多属性配置参考内核中文档。

在pinctl节点增加gpio的配置:

c 复制代码
gv-leds {
	gv_leds_gpio_fun: gv-leds-gpio-fun {
		rockchip,pins =
			<0 RK_PB0 RK_FUNC_GPIO &pcfg_pull_up>,
			<0 RK_PB5 RK_FUNC_GPIO &pcfg_pull_up>,
			<0 RK_PB6 RK_FUNC_GPIO &pcfg_pull_up>,
			<0 RK_PC5 RK_FUNC_GPIO &pcfg_pull_up>,
			<1 RK_PB0 RK_FUNC_GPIO &pcfg_pull_up>,
			<1 RK_PB1 RK_FUNC_GPIO &pcfg_pull_up>,
			<1 RK_PB2 RK_FUNC_GPIO &pcfg_pull_up>; 
	};
};

如果后续不需要使用只需要将gv_leds设备节点status状态设置为disable。

在实现过程中,部分引脚被占用找到没有生成led的sysfs文件,需要解除引脚占用:

a) 配置内核禁用TouchScreen

b) 禁用以下设备树节点

c 复制代码
vcc3v3_lcd1_n: vcc3v3-lcd1-n {
		compatible = "regulator-fixed";
		regulator-name = "vcc3v3_lcd1_n";
		regulator-boot-on;
		regulator-min-microvolt = <3300000>;
		regulator-max-microvolt = <3300000>;
		enable-active-high;
		gpio = <&gpio0 RK_PC5 GPIO_ACTIVE_HIGH>;
		vin-supply = <&vcc3v3_sys>;
		status = "disable";
		regulator-state-mem {
			regulator-off-in-suspend;
		};
	};

&wireless_bluetooth {
	compatible = "bluetooth-platdata";
	clocks = <&rk809 1>;
	clock-names = "ext_clock";
	//wifi-bt-power-toggle;
	uart_rts_gpios = <&gpio2 RK_PB1 GPIO_ACTIVE_LOW>;
	pinctrl-names = "default", "rts_gpio";
	pinctrl-0 = <&uart8m0_rtsn>;
	pinctrl-1 = <&uart8_gpios>;
	BT,reset_gpio    = <&gpio3 RK_PA0 GPIO_ACTIVE_HIGH>;
	BT,wake_gpio     = <&gpio3 RK_PA2 GPIO_ACTIVE_HIGH>;
	BT,wake_host_irq = <&gpio3 RK_PA1 GPIO_ACTIVE_HIGH>;
	status = "disable";
};
       
&pcie3x2 {
	reset-gpios = <&gpio2 RK_PD6 GPIO_ACTIVE_HIGH>;
	vpcie3v3-supply = <&vcc3v3_pcie>;
status = "disable";
};	              

将这些节点的status属性设置为"disable"。

用户空间访问

shell 复制代码
root@M3:~# ls /sys/class/leds/
led_rgb_1/ led_rgb_2/ led_rgb_3/ mmc0::/    run_led/   work_led/


root@M3:~# ls /sys/class/leds/led_rgb_1/
brightness  device  max_brightness  power  subsystem  trigger  uevent
root@M3:~# ls /sys/class/leds/led_rgb_2/
brightness  device  max_brightness  power  subsystem  trigger  uevent
root@M3:~# ls /sys/class/leds/led_rgb_3/
brightness  device  max_brightness  power  subsystem  trigger  uevent

通过读写brightness属性就可以控led点亮/关闭。

rightness power subsystem trigger uevent

root@M3:~# ls /sys/class/leds/led_rgb_3/

brightness device max_brightness power subsystem trigger uevent

复制代码
通过读写brightness属性就可以控led点亮/关闭。
相关推荐
XIAOHEZIcode3 小时前
Ubuntu 终端美化全栈指南:Bash 到 Kitty 踩坑实录
linux·ubuntu·命令行
唐青枫5 小时前
别再只会用 cron:Linux systemd Timer 定时任务实战详解
linux
AlfredZhao2 天前
生产环境里,为什么不建议把普通端口直接暴露到公网?
linux·https·443·80
戴为沐3 天前
Linux内存扩容指南
linux
zylyehuo4 天前
Linux 彻底且安全地删除文件
linux
用户805533698034 天前
主线 U-Boot 上 RK3506:和闭源 rkbin 拔河的三个隐性契约
linux·嵌入式
用户034095297914 天前
linux fcitx 5 雾凇拼音 设置在中文输入法下仍然输入英文标点
linux
Web3探索者6 天前
可视化服务器管理和传统命令行区别是什么?新手教程:Linux 运维到底该用图形界面还是 SSH 命令行?
linux·ssh
zylyehuo6 天前
Linux系统中网线与USB网络共享冲突
linux
Sokach10157 天前
Linux Shell 脚本从零到能用:一个新手的一天学习总结
linux