1.gpio 控制驱动
#include <dt-bindings/gpio/gpio.h>
#include <linux/input.h>
#include <linux/module.h>
#include <linux/init.h>
#include <linux/interrupt.h>
#include <linux/kernel.h>
#include <linux/fcntl.h>
#include <linux/delay.h>
#include <linux/device.h>
#include <linux/miscdevice.h>
#include <asm/types.h>
#include <linux/platform_device.h>
#include <asm/uaccess.h>
#include <linux/wait.h>
#include <linux/of_gpio.h>
#include <linux/gpio.h>
#include <linux/slab.h>
#define GPIO_HIGH 1
#define GPIO_LOW 0
int gpio,gpio_value;
static struct class *gpio_class;
static struct of_device_id gpio_match_table[] = {
{ .compatible = "gpio_driver",},
{},
};
static int gpio_open(struct inode *inode, struct file *file)
{
return 0;
}
static int gpio_release(struct inode *inode, struct file *file)
{
return 0;
}
static long gpio_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
{
switch(cmd)
{
case GPIO_HIGH:
gpio_direction_output(gpio, cmd);
break;
case GPIO_LOW:
gpio_direction_output(gpio, cmd);
break;
default:
return -EINVAL;
}
return 0;
}
const struct file_operations gpio_fops = {
.owner = THIS_MODULE,
.open = gpio_open,
.release = gpio_release,
.unlocked_ioctl = gpio_ioctl,
};
static int gpio_probe(struct platform_device *pdev)
{
enum of_gpio_flags flag;
struct device_node *gps_gpio_node = pdev->dev.of_node;
int ret;
ret = register_chrdev(103,"gpio_driver",&gpio_fops); //注册字符设备驱动
if (ret<0) {
printk("failed to register gpio_out device\n");
return -ENODEV;
}
gpio_class = class_create(THIS_MODULE, "gpio_driver"); //注册一个类,可以在/dev/目录下面建立设备节点
if (IS_ERR(gpio_class)) {
return -ENODEV;
}
/* not a big deal if we fail here :-) */
device_create(gpio_class, NULL, MKDEV(103, 0), NULL, "gpio_driver"); //创建一个设备节点,节点名为"gpio_driver"
gpio = of_get_named_gpio_flags(gps_gpio_node, "gpio_out", 0, &flag); //获取gpio号
if (!gpio_is_valid(gpio)) {
printk("gpio_driver can't register major number\n");
return -ENODEV;
}
//申请gpio
if (gpio_request(gpio, "gpio_driver")) {
printk("gpio_driver request failed!\n");
gpio_free(gpio);//释放gpio
return -ENODEV;
}
gpio_value = (flag == GPIO_ACTIVE_HIGH)? 1:0; //输入GPIO_ACTIVE_HIGH为1,赋值给gpio_value
gpio_direction_output(gpio, gpio_value);
printk("gpio_driver putout\n");
return 0;
}
static int gpio_remove(struct platform_device *pdev)
{
gpio_free(gpio);
return 0;
}
static struct platform_driver gpio_driver = {
.driver = {
.name = "gpio_driver",
.owner = THIS_MODULE,
.of_match_table = gpio_match_table,
},
.probe = gpio_probe,
.remove = gpio_remove,
};
module_platform_driver(gpio_driver);
MODULE_DESCRIPTION("gpio_driver control driver");
MODULE_AUTHOR("liuy@armdesigner.com");
MODULE_LICENSE("GPL");
2.gpio应用测试程序
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <sys/ioctl.h>
int main()
{
int fd,i;
while(1){
fd = open("/dev/gpio_driver", O_RDWR); //打开设备
scanf("%d",&i);
if(i==0){
ioctl(fd,0);
}
else if(i==1){
ioctl(fd,1);
}
else if(i==2){
close(fd); //关闭设备
printf("close gpio device !");
break;
}
else {
perror("open device gpio failed !"); // 如果打开设备失败,退出
}
}
return 0;
}
/**************************************************************************************************
1.
$ cp /opt/buildroot/rk3288_linux_v2.1/prebuilts/gcc/linux-x86/arm/gcc-linaro-6.3.1-2017.05-x86_64_arm-linux-gnueabihf/bin
$ ./arm-linux-gnueabihf-gcc gpio_test.c -o gpio_test
2.
# cp udisk/gpio_test /bin/
# gpio_test
*****************************************************************************************************/