(安卓)linux常用驱动

news/2024/7/21 8:56:29 标签: kernel, 设备节点, GPIO控制, Android

Kernel: 3.10.49

平台:RK3399

要求:提供一个节点,可以控制GPIO的输出状态。


RK平台GPIO号的计算如:

gpio1_A7 :1x32+0x8+7=39
gpio0_B5 : 0x32+1x8+5=13
gpio3_C3 : 3x32+2x8+3=115
gpio_direction_output(115,1);

MTKGPIO号的计算

gpio31::31+902 = 933
gpio_direction_output(933,1);

方法一:(在sys/class/目录下生成节点,在对节点写入对应的字符,驱动力再进行相关的操作,注意函数:gpio1a7_store


下面代码实现了创建节点sys/class/dev_class/gpio1a7

当执行echo 0 >  sys/class/dev_class/gpio1a7GPIO1_A7  拉低

当执行echo 1 >   sys/class/dev_class/gpio1a7GPIO1_A7  拉高

(代码适用所有内核)      

#include<linux/init.h>
#include<linux/module.h>
#include<linux/fs.h>
#include<linux/device.h>
#include <linux/cdev.h>
#include <linux/gpio.h>
#include <linux/of_gpio.h>
#include <asm/uaccess.h>
#include <linux/input.h>
#include <linux/interrupt.h>
#include <linux/slab.h>
#include <linux/irq.h>
#include <linux/delay.h>
#include <linux/hrtimer.h>
#include <linux/platform_device.h>
#include <linux/kobject.h>
#include<linux/timer.h>
#include <linux/hrtimer.h>

static unsigned int dev_major=100;
static dev_t devno;
static struct class  *dev_class=NULL;
static struct cdev   *dev_cdev = NULL;

static long ben_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
{
	long ret = 0;        
	return ret;
}
static int dev_open(struct inode *inode, struct file *file)
{
	printk(KERN_INFO"%s()-%d\n", __func__, __LINE__);
	return 0;
}
static ssize_t dev_read(struct file *file, char __user *buf, size_t count, loff_t *offset)
{
	printk(KERN_INFO"%s()-%d\n", __func__, __LINE__);
	return count;
}

static	ssize_t dev_write(struct file *file, const char __user *buf, size_t count, loff_t *offset)
{
	printk(KERN_INFO"%s()-%d\n", __func__, __LINE__);
	return count;
}

static ssize_t gpio1a7_store(struct class *cls,struct class_attribute *attr, const char __user *buf,size_t count)
{	
	int ret = -1;
	if(!strncmp(buf,"1",strlen("1")))//
	{ 
		gpio_free(39);
		ret = gpio_request(39, NULL);
		gpio_direction_output(39,1);
		printk("-------nbpt----the gpio1_a7 id high---- ret = %d--------------\n",gpio_get_value(39));
	
		return count;

	}
	else if(!strncmp(buf,"0",strlen("0")))//
	{

		gpio_free(39);
		ret = gpio_request(39, NULL);
		gpio_direction_output(39,0);
		printk("-------nbpt----the gpio1_a7 id high---- ret = %d--------------\n",gpio_get_value(39));
		return count;

	}

	return 0;
}

static CLASS_ATTR(gpio1a7, 0655, NULL, gpio1a7_store);

static struct file_operations dev_fops={
	.owner	= THIS_MODULE,
	.open	= dev_open,
	.read	= dev_read,
	.unlocked_ioctl   = ben_ioctl,      //
	.write	= dev_write,
};
static int __init dev_init(void)
{	
	int ret,ret1;
	devno = MKDEV(dev_major,0);
	printk("-----nbpt------dev_init! \n");
	ret = register_chrdev_region(devno,1,"xxx");
	if (ret < 0) {
		printk(KERN_ERR "unable to get major \n");
		return -EINVAL;
	}
		
	gpio_free(39);
	ret = gpio_request(39, NULL);
	gpio_direction_output(39,1);

	dev_cdev=cdev_alloc();

	cdev_init(dev_cdev,&dev_fops);

	cdev_add(dev_cdev,devno,1);

	dev_class=class_create(THIS_MODULE,"dev_class");
	if (IS_ERR(dev_class)) {
		printk(KERN_ERR "class_create() failed for dev_class\n");
		ret = -EFAULT;
		goto out_err_1;
	}	

	ret1 = class_create_file(dev_class,&class_attr_gpio1a7);
	if(ret1 <0){
		goto out_err_2;
	}

	return 0;

out_err_2:
	class_destroy(dev_class);
out_err_1:
	unregister_chrdev_region(MKDEV(dev_major,0),1);
	cdev_del(dev_cdev);
	return ret;


}
static void __exit dev_exit(void)
{


	unregister_chrdev_region(MKDEV(dev_major,0),1);
	device_destroy(dev_class,MKDEV(dev_major,0));

	class_destroy(dev_class);
}
module_init(dev_init);
module_exit(dev_exit);
MODULE_LICENSE("GPL");

2.获取GPIO的状态,返回不同字段。

生成dev/cdevdemo节点。cat  dev/cdevdemo 会返回不同字段。来判断gpio的状态。

#include <linux/module.h>  
#include <linux/types.h>  
#include <linux/fs.h>  
#include <linux/errno.h>  
#include <linux/mm.h>  
#include <linux/sched.h>  
#include <linux/init.h>  
#include <linux/cdev.h>  
#include <asm/io.h>  
#include <asm/uaccess.h>  
#include <linux/timer.h>  
#include <asm/atomic.h>  
#include <linux/slab.h>  
#include <linux/device.h> 

#include <linux/of_gpio.h>
#include <linux/gpio.h>
#define CDEVDEMO_MAJOR 255  /*预设cdevdemo的主设备号*/  
  
static int cdevdemo_major = CDEVDEMO_MAJOR;
static int flag;
  
struct cdevdemo_dev   
{  
	    struct cdev cdev;  
};  
  
struct cdevdemo_dev *cdevdemo_devp; /*设备结构体指针*/  
  
int cdevdemo_open(struct inode *inode, struct file *filp)     
{  
	    flag = 1;
	    printk(KERN_NOTICE "======== cdevdemo_open ");  
	        return 0;  
} 

int cdevdemo_release(struct inode *inode, struct file *filp)      
{  
	    printk(KERN_NOTICE "======== cdevdemo_release ");     
	        return 0;  
}  

#define BUF_LEN 50
static char kbuf[BUF_LEN];

static ssize_t cdevdemo_read(struct file *filp, char __user *ubuf, size_t count, loff_t *ppos)  
{  
	        int ret;
		
	    	if(flag == 0)
		return 0;
		
		gpio_free(902+52);
		gpio_request(902+52,NULL);
		gpio_direction_input(902+52);
		ret = gpio_get_value(902+52);
		
		if (ret == 1){ 
	    	        strcpy(kbuf, "hdmi state is 1");
		}else {
			strcpy(kbuf, "");
		}
			
			if (count < 0)
				return -EINVAL;
			if (count > strlen(kbuf))
			count = (strlen(kbuf));
			ret = copy_to_user(ubuf, kbuf, count); //拷贝给用户 //copy_from_user(to, from, size);
			if (ret) {
			printk(KERN_WARNING "copy_to_user failed! \n");
			return ret;
			}
			flag = 0;
	    return (ssize_t)count;
}  
  
static const struct file_operations cdevdemo_fops =  
{  
	    .owner = THIS_MODULE,  
	    .open = cdevdemo_open,  
		.release = cdevdemo_release,  
		.read = cdevdemo_read,  
};

static void cdevdemo_setup_cdev(struct cdevdemo_dev *dev, int index)  
{  
	        int err, devno = MKDEV(cdevdemo_major, index);
 
		    cdev_init(&dev->cdev, &cdevdemo_fops);  
			dev->cdev.owner = THIS_MODULE;  
		    dev->cdev.ops = &cdevdemo_fops;  
		    err = cdev_add(&dev->cdev, devno, 1);  
 
	        if(err) 
	     	{  
			printk(KERN_NOTICE "Error %d add cdevdemo %d", err, index);   
			}  
}

int cdevdemo_init(void)  
{  
	    int ret;  
		struct class *cdevdemo_class;  
		dev_t devno = MKDEV(cdevdemo_major, 0);  
		      
	    printk(KERN_NOTICE "======== cdevdemo_init ");    
			    /*申请设备号,如果申请失败采用动态申请方式*/  
		if(cdevdemo_major)  
		{  
		ret = register_chrdev_region(devno, 1, "cdevdemo");  
		}else{  
		ret = alloc_chrdev_region(&devno,0,1,"cdevdemo");  
		cdevdemo_major = MAJOR(devno);  
		}	

		if(ret < 0)  
	    {  
		return ret;  
		}  
    		/*动态申请设备结构体内存*/  
    	cdevdemo_devp = kmalloc(sizeof(struct cdevdemo_dev), GFP_KERNEL);  
        if(!cdevdemo_devp)  /*申请失败*/  
	    {	  
		ret = -ENOMEM;  
		printk(KERN_NOTICE "Error add cdevdemo");     
		goto fail_malloc;  
		}

		memset(cdevdemo_devp,0,sizeof(struct cdevdemo_dev));  
        cdevdemo_setup_cdev(cdevdemo_devp, 0);  
	  
	    	/*下面两行是创建了一个总线类型,会在/sys/class下生成cdevdemo目录 
	     	*       这里的还有一个主要作用是执行device_create后会在/dev/下自动生成 
	     	*             cdevdemo设备节点。而如果不调用此函数,如果想通过设备节点访问设备 
	     	*                   需要手动mknod来创建设备节点后再访问。*/  
	    cdevdemo_class = class_create(THIS_MODULE, "cdevdemo");  
	    device_create(cdevdemo_class, NULL, MKDEV(cdevdemo_major, 0), NULL, "cdevdemo");  
		return 0;  
			  
fail_malloc:  
	unregister_chrdev_region(devno,1);
      	return 0;	
}

void cdevdemo_exit(void)    /*模块卸载*/  
{  
	    printk(KERN_NOTICE "End cdevdemo");   
	    cdev_del(&cdevdemo_devp->cdev);  /*注销cdev*/  
	    kfree(cdevdemo_devp);       /*释放设备结构体内存*/  
	    unregister_chrdev_region(MKDEV(cdevdemo_major,0),1);    //释放设备号  
}  
  
MODULE_LICENSE("Dual BSD/GPL");  
module_param(cdevdemo_major, int, S_IRUGO);  
module_init(cdevdemo_init);  
module_exit(cdevdemo_exit); 

3.基于设备树的GPIO驱动(通过系统节点控制)

节点gpio_22可以进行cat和echo操作。






1、dts文件
    gpio_ldo_power {
        compatible = "xcz,gpio_ldo_power";
        qcom,gpio_ldo_pin = <&msm_gpio 22 0>;
    };

2、驱动
比较简单,直接看源码
#include <linux/types.h>
#include <linux/pm.h>
#include <linux/gpio.h>
#include <linux/slab.h>
#include <linux/init.h>
#include <linux/platform_device.h>
#include <linux/fsl_devices.h>
#include <asm/setup.h>
#include <linux/of.h>
#include <linux/of_gpio.h>
#include <linux/stat.h>
#include <linux/module.h>
#include <linux/err.h>
#include <linux/spinlock.h>
#include <linux/err.h>
#include <linux/regulator/consumer.h>

int gpio_ldo_pin = -1;
int gpio_flag = -1;
static struct class *gpio_ldo_power_class = NULL;
static struct device *gpio_ldo_power_dev = NULL;

#define CTL_POWER_ON    "1"
#define CTL_POWER_OFF   "0"

static ssize_t gpio_22_show(struct device *dev,
        struct device_attribute *attr, char *buf)
{
    printk("%s\n", __func__);
    sprintf(buf, "gpio_22 is %d\n", gpio_flag);
    return strlen(buf);
}

static ssize_t gpio_22_store(struct device *dev,
        struct device_attribute *attr, const char *buf,
        size_t count)
{
    if(!strncmp(buf, CTL_POWER_ON, strlen(CTL_POWER_ON))) {
        printk("%s: to enable gpio_22\n", __func__);
        gpio_set_value(gpio_ldo_pin, 1);
        gpio_flag = 1;

    } else if(!strncmp(buf, CTL_POWER_OFF, strlen(CTL_POWER_OFF))) {
        printk("%s: to disable gpio_22\n", __func__);
        gpio_set_value(gpio_ldo_pin, 0);
        gpio_flag = 0;
    }

    return count;
}

static struct device_attribute gpio_22_dev_attr = {
    .attr = {
        .name = "gpio_22",
        .mode = S_IRWXU|S_IRWXG|S_IRWXO,
    },
    .show = gpio_22_show,
    .store = gpio_22_store,
};



static int gpio_ldo_power_probe(struct platform_device *pdev)
{
    int ret = 0;

    printk("xcz enter gpio_ldo_power_probe \n");

    gpio_ldo_pin = of_get_named_gpio(pdev->dev.of_node, "qcom,gpio_ldo_pin", 0);
    if (gpio_ldo_pin < 0)
        printk("xcz gpio_ldo_pin is not available \n");

    ret = gpio_request(gpio_ldo_pin, "gpio_ldo_pin");
    if(0 != ret) {
        printk("xcz gpio request %d failed.", gpio_ldo_pin);
        goto fail1;
    }
    
    gpio_direction_output(gpio_ldo_pin, 0);

    gpio_set_value(gpio_ldo_pin, 0);
    gpio_flag = 0;

    gpio_ldo_power_class = class_create(THIS_MODULE, "gpio_ldo_power");
    if(IS_ERR(gpio_ldo_power_class))
    {
        ret = PTR_ERR(gpio_ldo_power_class);
        printk("Failed to create class.\n");
        return ret;
    }

    gpio_ldo_power_dev = device_create(gpio_ldo_power_class, NULL, 0, NULL, "gpio_gpio_22");
    if (IS_ERR(gpio_ldo_power_dev))
    {
        ret = PTR_ERR(gpio_ldo_power_class);
        printk("Failed to create device(gpio_ldo_power_dev)!\n");
        return ret;
    }

    ret = device_create_file(gpio_ldo_power_dev, &gpio_22_dev_attr);
    if(ret)
    {
        pr_err("%s: gpio_22 creat sysfs failed\n",__func__);
        return ret;
    }

    printk("xcz enter gpio_ldo_power_probe, ok \n");

fail1:
    return ret;
}

static int gpio_ldo_power_remove(struct platform_device *pdev)
{
    device_destroy(gpio_ldo_power_class, 0);
    class_destroy(gpio_ldo_power_class);
    device_remove_file(gpio_ldo_power_dev, &gpio_22_dev_attr);

    return 0;
}

static int gpio_ldo_power_suspend(struct platform_device *pdev,pm_message_t state)
{
    return 0;
}

static int gpio_ldo_power_resume(struct platform_device *pdev)
{
    return 0;
}

static struct of_device_id gpio_ldo_power_dt_match[] = {
    { .compatible = "xcz,gpio_ldo_power",},
    { },
};
MODULE_DEVICE_TABLE(of, gpio_ldo_power_dt_match);

static struct platform_driver gpio_power_driver = {
    .driver = {
        .name = "gpio_ldo_power",
        .owner = THIS_MODULE,
        .of_match_table = of_match_ptr(gpio_ldo_power_dt_match),
    },
    .probe = gpio_ldo_power_probe,
    .remove = gpio_ldo_power_remove,
    .suspend = gpio_ldo_power_suspend,
    .resume = gpio_ldo_power_resume,
};

static __init int gpio_power_init(void)
{
    return platform_driver_register(&gpio_power_driver);
}

static void __exit gpio_power_exit(void)
{
    platform_driver_unregister(&gpio_power_driver);
}

module_init(gpio_power_init);
module_exit(gpio_power_exit);
MODULE_AUTHOR("GPIO_LDO_POWER, Inc.");
MODULE_DESCRIPTION("XCZ GPIO_LDO_POWER");
MODULE_LICENSE("GPL");


4. Sample kobject implementation:

    linux内核学习笔记之设备与模块 - CSDN博客:
    https://blog.csdn.net/brand_j/article/details/79844766




http://www.niftyadmin.cn/n/1567995.html

相关文章

Android 命令:工作中常用到的命令都在这里

前言&#xff1a;本文主要记录了自己工作中使用频率比较高的命令。 objdump ,readelf:&#xff1a;可以查看目标文件&#xff0c;如二进制可执行文件.so文件&#xff0c; objdump -T XXX.so 检查库是否加密成功 链接&#xff1a;https://www.jianshu.com/p/863b279c941e ad…

C++基础(1) - 文件操作

1. 文件的概念 对于用户来说&#xff0c;常用到的文件有两大类&#xff1a;程序文件和数据文件。而根据文件中数据的组织方式&#xff0c;则可以将文件分为ASCII 文件和二进制文件。 数字 64 在内存中表示为 0100 0000&#xff0c;若将其保存为 ASCII 文件&#xff0c;则要分别…

图像按钮ImageButton

在Android App应用中&#xff0c;默认的Button按钮尽管我们可以通过样式变成圆角&#xff0c;但有时感觉仍然不够美观&#xff0c;我们可以通过采用图像按钮ImageButton改善这种现状&#xff0c;今天我们就一起学习一下图像按钮的使用。 知识点&#xff1a;图像按钮ImageButto…

Android按钮单击事件的四种常用写法

这篇文章主要介绍了Android按钮单击事件的四种常用写法总结,比较了常见的四种写法的优劣,有不错的参考借鉴价值,需要的朋友可以参考下很多学习Android程序设计的人都会发现每个人对代码的写法都有不同的偏好&#xff0c;比较明显的就是对控件响应事件的写法的不同。因此本文就把…

安卓SystemProperties详解及使用

platform &#xff1a;msm8916 OS:android5.1安卓系统属性是以键值对的形式存在&#xff0c;系统属性&#xff0c;一般放在system.prop&#xff0c;build.prop&#xff0c;default.prop等文件中&#xff0c; 应用通过函数String key SystemProperties.get("ro.android.my…

安卓屏幕的那些事

1、屏幕方向参数&#xff1a; ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED,//未指定&#xff0c;此为默认值。由Android系统自己选择合适的方向。 ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE,//横屏 ActivityInfo.SCREEN_ORIENTATION_PORTRAIT,//竖屏…

普通IO模拟i2c总线

相关文章&#xff1a; 一 . i2c总线协议 二. 普通IO模拟i2c总线 三. 简单i2c设备驱动实例 下面简单的单片机程序&#xff0c;理解之后会让你对I2c总线有更深的认识&#xff0c; 思路 1.向EEPROM中通过I2C总线写入一个字节 2.通过I2C总线读出写入的字节 3.如果写入和读…

简单i2c设备驱动实例

相关文章&#xff1a; 一 . i2c总线协议 二. 普通IO模拟i2c总线 三. 简单i2c设备驱动实例 平台&#xff1a; msm8916 OS&#xff1a;安卓5.1 usb4604在设备上作用是切换设备为主从模式。 设备树文件如下&#xff1a; microchip2d {compatible "microchip,usb460…