Linux驱动开发学习笔记——GPIO(一)

本文详细介绍了如何在AM4379平台上,从设备树配置到驱动程序编写,再到内核编译和加载,实现自定义GPIO驱动的完整过程。通过配置设备树新增测试节点和引脚,编写包含驱动注册和销毁的驱动程序,并在Makefile和Kconfig中进行编译设置,最终成功加载驱动并输出相关信息。
摘要由CSDN通过智能技术生成

平台: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
至此驱动第一步成功。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

C bug专业户

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值