Linux 驱动开发基础知识——LED 模板驱动程序的改造:设备树
#include <linux/platform_device.h>
#include <linux/of.h>
#include <linux/of_gpio.h>
#include <linux/leds.h>
/* 假设在设备树中定义了LED相关属性 */
static struct of_device_id my_led_of_match[] = {
{ .compatible = "my,led", },
{ /* Sentinel */ }
};
MODULE_DEVICE_TABLE(of, my_led_of_match);
/* 平台设备注册函数 */
static int my_led_probe(struct platform_device *pdev)
{
struct device *dev = &pdev->dev;
struct device_node *np = dev->of_node;
struct led_classdev *led_cdev;
int led_gpio;
int ret;
/* 获取GPIO编号 */
led_gpio = of_get_named_gpio(np, "led-gpio", 0);
if (led_gpio < 0) {
dev_err(dev, "Failed to get LED GPIO: %d\n", led_gpio);
return led_gpio;
}
/* 分配和初始化led_classdev结构体 */
led_cdev = kzalloc(sizeof(*led_cdev), GFP_KERNEL);
if (!led_cdev) {
dev_err(dev, "Failed to allocate memory for LED\n");
return -ENOMEM;
}
/* 设置 led_classdev 的属性和回调函数 */
led_cdev->brightness_set = my_led_set;
led_cdev->brightness_get = my_led_get;
led_cdev->default_trigger = "none";
led_cdev->flags |= LED_CORE_SUSPENDRESUME;
/* 注册到LED子系统 */
ret = led_classdev_register(dev, led_cdev);
if (ret < 0) {
dev_err(dev, "LED register failed with error %d\n", ret);
goto err_led_register;
}
platform_set_drvdata(pdev, led_cdev);
return 0;
err_led_register:
kfree(led_cdev);
return ret;
}
/* 平台设备移除函数 */
static int my_led_remove(struct platform_device *pdev)
{
struct led_classdev *led_cdev = platform_get_drvdata(pdev);
led_classdev_unregister(led_cdev);
kfree(led_cdev);
return 0;
}
/* 平台驱动结构体 */
static struct platform_driver my_led_driver = {
.probe = my_led_probe,
.remove = my_led_remove,
.driver = {
.name = "my-led",
.of_match_table = my_led_of_match,
},
};
module_platform_driver(my_led_driver);
这个示例代码展示了如何使用设备树来改造一个简单的LED驱动程序。它首先定义了一个与设备树中LED节点相匹配的结构体数组,然后在设备树中找到LED节点并获取GPIO相关信息。接着,它初始化了一个led_classdev
结构体,并设置了亮度设置和获取函数,并注册到LED子系统中。最后,它提供了平台驱动的注册函数。这个过程是驱动开发中常见的一个步骤,展示了如何将设备树用于驱动的配置。
评论已关闭