平台:AM4379
目标:将自己编写的驱动编译到内核,并成功加载至内核。
1、配置设备树
新建一个测试节点:
mygpio_test {
compatible = "mygpiotest";
pinctrl-names = "default";
pinctrl-0 = <&user_gpio_out>;
gpios = <&gpio0 2 GPIO_ACTIVE_HIGH>;
default-state = "on";
};
配置引脚:
在节点&am43xx_pinmux{… …}内添加:
user_gpio_out: user_gpio_out {
pinctrl-single,pins = <
AM4372_IOPAD(0x9a8, PIN_OUTPUT_PULLUP | MUX_MODE9) /* (m25)gpio0_2 */
>;
};
2、编写驱动程序
#include <linux/module.h>
#include <linux/init.h>
#include <linux/err.h>
#include <linux/gpio.h>
#include <linux/gpio/consumer.h>
#include <linux/kernel.h>
#include <linux/of.h>
#include <linux/of_platform.h>
#include <linux/platform_device.h>
#include <linux/property.h>
#include <linux/of_gpio.h>
#include <linux/workqueue.h>
#include <linux/cdev.h>
#include "mygpio-out.h"
//驱动注册
static int my_gpio_out_probe(struct platform_device *pdev)
{
MGIO_INFO("my gpio out driver probe\n");
return 0;
}
//驱动销毁
static int my_gpio_out_remove(struct platform_device *pdev)
{
MGIO_INFO("my gpio out driver remove\n");
return 0;
}
//设备树关联
static const struct of_device_id my_gpio_out_of_match[] = {
{ .compatible = "mygpiotest", },
{ },
};
MODULE_DEVICE_TABLE(of, my_gpio_out_of_match);
//驱动配置,驱动注册函数及驱动销毁函数接口指定,设备树关联
static struct platform_driver my_gpio_out_driver = {
.probe = my_gpio_out_probe,
.remove = my_gpio_out_remove,
.driver = {
.name = "mygpio_test",
.of_match_table = of_match_ptr(my_gpio_out_of_match),
}
};
//驱动注册
static int __init my_gpio_init(void)
{
MGIO_INFO("my gpio out driver installing...\n");
return platform_driver_register(&my_gpio_out_driver);
}
//驱动销毁
static void __exit my_gpio_exit(void)
{
MGIO_INFO("my gpio out driver exited.\n");
platform_driver_unregister(&my_gpio_out_driver);
}
module_init(my_gpio_init);
module_exit(my_gpio_exit);
MODULE_LICENSE("GPL");
MODULE_DESCRIPTION("GPIO OUT driver for GPIOs");
3、编译相关文件更改
在Makefile文件内加入:
obj-$(CONFIG_GPIO_OUT) += mygpio-out.o
在Kconfig文件内加入,便于直接通过编译内核到镜像中:
config GPIO_OUT
tristate "GPIO_OUT support"
default y if ARCH_GPIO_OUT
depends on OF_GPIO
help
Select this option to enable GPIO driver for
GPIO_OUT devices.
配置驱动编译至内核:
4、编译内核、运行
编译内核,编译设备树,将生成的镜像和设备树文件拷贝到开发板,运行。
输出信息:
[ 0.167312] <> my gpio out driver installing…
[ 0.167312]
[ 0.167487] <> my gpio out driver probe
至此驱动第一步成功。