I.MX6Q GPIO驱动的编写
2019-07-12 13:37发布
生成海报
I.MX6Q GPIO驱动的编写
作者:卢浩
时间:2017.2.20
转载请注明出处
嵌入式爱好者开发群:122879839
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include "linux/version.h"
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
/*
Set the DEVICE_NAME,you can change it
*/
#define DEVICE_NAME "GPIO_DEVICE"
/*
OUT level control
1:high
0:low
Isn't it?You can read the reference mentual
*/
#define IOCTL_GPIO_HIGH 1
#define IOCTL_GPIO_LOW 0
#define NUM_5_16 IMX_GPIO_NR(5, 16)
#define NUM_5_17 IMX_GPIO_NR(5, 17)
//#define NUM_4_28 IMX_GPIO_NR(4, 28)
//#define NUM_4_29 IMX_GPIO_NR(4, 29)
//#define NUM_4_30 IMX_GPIO_NR(4, 30)
static unsigned int num[] = {
NUM_5_16,
NUM_5_17,
// NUM_4_28,
// NUM_4_29,
// NUM_4_30,
};
static long gpio_ioctl(
struct file *file,
unsigned int cmd,
unsigned long arg)
{
if (arg > 3)
{
return -EINVAL;
}
switch(cmd)
{
case IOCTL_GPIO_HIGH:
gpio_set_value(num[arg], 1);
return 0;
case IOCTL_GPIO_LOW:
gpio_set_value(num[arg], 0);
return 0;
default:
return -EINVAL;
}
}
static struct file_operations gpio_fops = {
.owner = THIS_MODULE,
.unlocked_ioctl = gpio_ioctl,
// .release = gpio_release,
// .read = gpio_read,
// .write = gpio_write,
};
static struct miscdevice misc = {
.minor = MISC_DYNAMIC_MINOR,
.name = DEVICE_NAME,
.fops = &gpio_fops,
};
static int __init gpio_init(void)
{
int ret;
int i;
for(i = 0 ; i<2; i++){
gpio_request(num[i],"fsl_gpio");
gpio_direction_output(num[i],1);
gpio_set_value(num[i], 0);
}
ret = misc_register(&misc);
printk (DEVICE_NAME" initialized.
");
return ret;
}
static void __exit gpio_exit(void)
{
misc_deregister(&misc);
}
MODULE_LICENSE("GPL");
MODULE_AUTHOR("luhao");
module_init(gpio_init);
module_exit(gpio_exit);
打开微信“扫一扫”,打开网页后点击屏幕右上角分享按钮