系统加载后会寻找与设备树相关的驱动进行匹配。
开发板上的LED灯在设备树中的leds节点中描述
leds {
compatible = "gpio-leds";
work {
label = "firefly:blue:power";
linux,default-trigger = "ir-power-click";
gpios = <&gpio2 27 GPIO_ACTIVE_HIGH>;
pinctrl-names = "default";
pinctrl-0 = <&led_power>;
default-state = "on";
};
user {
label = "firefly:yellow:user";
linux,default-trigger = "ir-user-click";
gpios = <&gpio0 13 GPIO_ACTIVE_HIGH>;
pinctrl-names = "default";
pinctrl-0 = <&led_user>;
default-state = "off";
};
};
系统根据compatible 属性与驱动的of_gpio_leds_match进行匹配,匹配成功后执行probe函数
static const struct of_device_id of_gpio_leds_match[] = {
{ .compatible = "gpio-leds", },
{},
};
static struct platform_driver gpio_led_driver = {
.probe = gpio_led_probe,
.remove = gpio_led_remove,
.shutdown = gpio_led_shutdown,
.driver = {
.name = "leds-gpio",
.of_match_table = of_gpio_leds_match,
},
};
probe中调用gpio_leds_create函数,在gpio_leds_create函数中会从设备从获取GPIO信息并初始化struct gpio_led led结构体,
然后调用create_gpio_led函数申请并配置GPIO并初始化 struct gpio_led_data *led_dat结构体,并调用led_classdev_register注册到LED系统。
其中led_dat->cdev.brightness_set指针在用户操作GPIO时会被调用,该指针指向static void gpio_led_set(struct led_classdev *led_cdev, enum led_brightness value)函数。
gpio_led_set函数中会根据传参调用gpiod_set_value函数对GPIO进行设置。