编写MTK6737平台的GPIO驱动例程(一)

1、在dts文件中创建节点,在/kernel-3.18/arch/arm64/boot/dts/mt6735.dtsi

创建一个名mygpio的设备节点,匹配名称为“mykgpio”。

2、别写驱动框架

#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 

#include "mt-plat/mtgpio.h"
#include 
#include 
#include 
#include 

/* 生命函数定义 */
static int mygpio_probe(struct platform_device *pdev);
static int mygpio_remove(struct platform_device *pdev);


static const struct of_device_id mygpio_of_match[] = {
	{ .compatible = "mykgpio", },
	{},
};

static struct platform_driver mygpio_driver = {
	.remove = mygpio_remove,
	.probe = mygpio_probe,
	.driver = {
			.name = "myGPIO",
			.owner = THIS_MODULE,
			.of_match_table = mygpio_of_match,
	},
};

static int mygpio_misc_open(struct inode *inode, struct file *file)
{
	printk("MyGPIO OPen. \r\n");
	
	return 0;
}


static int mygpio_misc_release(struct inode *inode, struct file *file)
{
	printk("MyGPIO Release. \r\n");
	
	return 0;
}

static long mygpio_unlocked_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
{
	printk("MyGPIO Ioctl. \r\n");
	
	printk("MyGPIO cmd=%d \r\n", cmd);
	
	return 0;
}

static const struct file_operations mygpio_fops = {
 /* .owner = THIS_MODULE, */
	.open = mygpio_misc_open,
	.release = mygpio_misc_release,
	.unlocked_ioctl = mygpio_unlocked_ioctl,
};


static struct miscdevice mygpio_misc_device = {
	.minor = MISC_DYNAMIC_MINOR,		//动态设备号
	.name = "myGPIO",
	.fops = &mygpio_fops,
};



/* My GPIO probe */
static int mygpio_probe(struct platform_device *pdev)
{
	int ret = 0;
	
	printk("MyGPIO Probe. \r\n");
	
	ret = misc_register(&mygpio_misc_device);
	if (ret != 0 )
		printk("myGPIO: mygpio_device register failed\n");
	
	return ret;
}


static int mygpio_remove(struct platform_device *pdev)
{
	int err;
	
	printk("MyGPIO remove. \r\n");

	err = misc_deregister(&mygpio_misc_device);
	if (err)
		printk("deregister gpio\n");
	
	return err;
}

static int __init my_gpio_init(void)
{
	int ret = 0;

	printk("Register MyGPIO platform_driver. \r\n");

	ret = platform_driver_register(&mygpio_driver);
	if(ret != 0 )
		printk("unable to register MyGPIO driver.\n");
	
	return ret;
}

/*---------------------------------------------------------------------------*/
static void __exit my_gpio_exit(void)
{
	platform_driver_unregister(&mygpio_driver);
}

subsys_initcall(my_gpio_init);
/*module_init(my_gpio_init);*/
module_exit(my_gpio_exit);

MODULE_AUTHOR("zue");
MODULE_DESCRIPTION("MY General Purpose Driver (GPIO)");
MODULE_LICENSE("GPL v2");

这个过程很简单,首先注册一个设备驱动,当DTS中匹配到名为"mykgpio"的设备时,调用Probe函数,在probe函数中,注册一个名为"myGPIO"的杂项设备驱动。

将驱动文件放入kernel,进行编译并少写编译出的镜像。

待开发板启动后,即可在/dev目录下看到注册的设备。这样一个驱动框架就OK了,下面就可以编写实质的GPIO驱动程序了。

 

你可能感兴趣的:(Android,MTK)