platform相关函数及驱动框架

 platform_driver结构体表示platform驱动,内容如下:

struct platform_driver {
 int (*probe)(struct platform_device *);
 int (*remove)(struct platform_device *);
 void (*shutdown)(struct platform_device *);
 int (*suspend)(struct platform_device *, pm_message_t state);
 int (*resume)(struct platform_device *);
 struct device_driver driver;
 const struct platform_device_id *id_table;
 bool prevent_deferred_probe;
 };

1、probe 函数,当驱动与设备匹配成功以后 probe 函数就会执行。

2、remove函数,当驱动卸载时会执行remove函数。

2、driver 成员,为 device_driver 结构体变量。

device_driver结构体内容如下:

struct device_driver {
 const char *name;
 struct bus_type *bus;
 
 struct module *owner;
 const char *mod_name; /* used for built-in modules */
 
 bool suppress_bind_attrs; /* disables bind/unbind via sysfs */
 
 const struct of_device_id *of_match_table;
 const struct acpi_device_id *acpi_match_table;

 int (*probe) (struct device *dev);
 int (*remove) (struct device *dev);
 void (*shutdown) (struct device *dev);
 int (*suspend) (struct device *dev, pm_message_t state);
 int (*resume) (struct device *dev);
 const struct attribute_group **groups;

 const struct dev_pm_ops *pm;

 struct driver_private *p;
 };

其中of_match_table就是采用设备树的时候驱动使用的匹配表,同样是数组,每个匹 配项都为 of_device_id 结构体类型,of_device_id内容如下:

struct of_device_id {
 char name[32];
 char type[32];
 char compatible[128];
 const void *data;
 };

compatible:对于设备树而言,就是通过设备节点的 compatible 属 性值和 of_match_table 中每个项目的 compatible 成员变量进行比较,如果有相等的就表示设备 和此驱动匹配成功。

         在编写 platform 驱动的时候,首先定义一个 platform_driver 结构体变量,然后实现结构体 中的各个成员变量,重点是实现匹配方法以及 probe 函数。当驱动和设备匹配成功以后 probe 函数就会执行,具体的驱动程序在 probe 函数里面编写,比如字符设备驱动等等。

         当我们定义并初始化好 platform_driver 结构体变量以后,需要在驱动入口函数里面调用 platform_driver_register 函数向 Linux 内核注册一个 platform 驱动,函数原型如下:

int platform_driver_register (struct platform_driver *driver)

driver:要注册的platform驱动

返回值:负数失败、0成功

        还需要在驱动卸载函数中通过 platform_driver_unregister 函数卸载 platform 驱动, platform_driver_unregister 函数原型如下:

void platform_driver_unregister(struct platform_driver *drv)

drv:要卸载的platform驱动

platform驱动框架如下:

/* 设备结构体 */
 struct xxx_dev{
 struct cdev cdev;
 /* 设备结构体其他具体内容 */
 };
 
 struct xxx_dev xxxdev; /* 定义个设备结构体变量 */
 
 static int xxx_open(struct inode *inode, struct file *filp)
 { 
 /* 函数具体内容 */
 return 0;
 }

 static ssize_t xxx_write(struct file *filp, const char __user *buf,size_t cnt, loff_t *offt)
 {
 /* 函数具体内容 */
 return 0;
 }
/*
 字符设备驱动操作集
 */
 static struct file_operations xxx_fops = {
 .owner = THIS_MODULE,
 .open = xxx_open,
 .write = xxx_write,
 };

 /*
 * platform 驱动的 probe 函数
 * 驱动与设备匹配成功以后此函数就会执行
 */
 static int xxx_probe(struct platform_device *dev)
 { 
 ......
 cdev_init(&xxxdev.cdev, &xxx_fops); /* 注册字符设备驱动 */
 /* 函数具体内容 */
 return 0;
 }

 static int xxx_remove(struct platform_device *dev)
 {
 ......
 cdev_del(&xxxdev.cdev);/* 删除 cdev */
 /* 函数具体内容 */
 return 0;
 }

 /* 匹配列表 */
 static const struct of_device_id xxx_of_match[] = {
 { .compatible = "xxx-gpio" },
 { /* Sentinel */ }
 };

 /* 
 * platform 平台驱动结构体
 */
 static struct platform_driver xxx_driver = {
 .driver = {
 .name = "xxx",
 .of_match_table = xxx_of_match,
 },
 .probe = xxx_probe,
.remove = xxx_remove,
 };
 
 /* 驱动模块加载 */
 static int __init xxxdriver_init(void)
 {
 return platform_driver_register(&xxx_driver);
 }

 /* 驱动模块卸载 */
 static void __exit xxxdriver_exit(void)
 {
 platform_driver_unregister(&xxx_driver);
 }

 module_init(xxxdriver_init);
 module_exit(xxxdriver_exit);
 MODULE_LICENSE("GPL");
 MODULE_AUTHOR("qqq");

 设备和驱动的匹配主要是通过以下来实现的:

有设备树:xxx_of_match 匹配表,如果使用设备树的话将通过此匹配表进行驱动和设备的匹配。compatible设置了一个匹配项,此匹配项的 compatible 值为“xxx-gpio”,因此当设备树中设备节点的 compatible 属性值为“xxx-gpio”的时候此设备就会与此驱动匹配。of_device_id 表最后一个匹配项必须是空的。

无设备树:platform_driver 结构体变量 xxx_driver,表示 platform 驱动, paltform_driver 中的 device_driver 成员变量的 name 和 of_match_table 这两个属性。其中 name 属性用于传统的驱动与设备匹配,也就是检查驱动和设备的 name 字段是不是相同。 of_match_table 属性就是用于设备树下的驱动与设备检查。对于一个完整的驱动程序,必须提供 有设备树和无设备树两种匹配方法。

platform设备:

       platform_device 这个结构体表示 platform 设备,这里我们要注意,如果内核支持设备树 的话就不要再使用 platform_device 来描述设备了,因为改用设备树去描述了。结构体的内容如下:

struct platform_device {
 const char *name; 
 int id; 
 bool id_auto;
 struct device dev;
 u32 num_resources; 
 struct resource *resource;

 const struct platform_device_id *id_entry;
 char *driver_override; /* Driver name to force a match */

 /* MFD cell pointer */
 struct mfd_cell *mfd_cell;

 /* arch specific additions */
 struct pdev_archdata archdata;
 };

name :表示设备名字,要和所使用的 platform 驱动的 name 字段相同,否则的话设 备就无法匹配到对应的驱动。比如对应的 platform 驱动的 name 字段为“xxx-gpio”,那么此 name 字段也要设置为“xxx-gpio”。

num_resources:表示资源数量。

resource:表示资源,也就是设备信息,比如外设寄存器等。Linux 内核使用 resource 结构体表示资源,resource 结构体内容如下:

struct resource {
 resource_size_t start;
 resource_size_t end;
 const char *name;
 unsigned long flags;
 struct resource *parent, *sibling, *child;
 };

start 和 end 分别表示资源的起始和终止信息,对于内存类的资源,就表示内存起始和终止 地址,name 表示资源名字,flags 表示资源类型。

        在以前不支持设备树的Linux版本中,用户需要编写platform_device变量来描述设备信息, 然后使用 platform_device_register 函数将设备信息注册到 Linux 内核中,此函数原型如下所示:

int platform_device_register(struct platform_device *pdev)

pdev:要注册的platform设备

返回值:负数失败,0成功

        如果不再使用 platform 的话可以通过 platform_device_unregister 函数注销掉相应的 platform 设备,platform_device_unregister 函数原型如下:

void platform_device_unregister(struct platform_device *pdev)

pdev:要注销的platform设备

platform 设备信息框架如下所示:

/* 寄存器地址定义*/
 #define PERIPH1_REGISTER_BASE (0X20000000) /* 外设 1 寄存器首地址 */ 
 #define PERIPH2_REGISTER_BASE (0X020E0068) /* 外设 2 寄存器首地址 */
 #define REGISTER_LENGTH 4
 
 /* 资源 */
 static struct resource xxx_resources[] = {
 [0] = {
 .start = PERIPH1_REGISTER_BASE,
 .end = (PERIPH1_REGISTER_BASE + REGISTER_LENGTH - 1),
 .flags = IORESOURCE_MEM,
 }, 
 [1] = {
.start = PERIPH2_REGISTER_BASE,
 .end = (PERIPH2_REGISTER_BASE + REGISTER_LENGTH - 1),
 .flags = IORESOURCE_MEM,
 },
 };

 /* platform 设备结构体 */
 static struct platform_device xxxdevice = {
 .name = "xxx-gpio",
 .id = -1,
 .num_resources = ARRAY_SIZE(xxx_resources),
 .resource = xxx_resources,
 };
 
 /* 设备模块加载 */
 static int __init xxxdevice_init(void)
 {
 return platform_device_register(&xxxdevice);
 }

 /* 设备模块注销 */
 static void __exit xxx_resourcesdevice_exit(void)
 {
 platform_device_unregister(&xxxdevice);
 }

 module_init(xxxdevice_init);
 module_exit(xxxdevice_exit);
 MODULE_LICENSE("GPL");
 MODULE_AUTHOR("qqq");

xxx_resources:表示设备资源,一共有两个资源,分别为设备外设 1 和外 设 2 的寄存器信息。因此 flags 都为 IORESOURCE_MEM,表示资源为内存类型的。

xxxdevice:platform 设备结构体变量,注意 name 字段要和所使用的驱动中的 name 字段 一致,否则驱动和设备无法匹配成功。num_resources 表示资源大小,其实就是数组 xxx_resources 的元素数量,这里用 ARRAY_SIZE 来测量一个数组的元素个数。

LED灯的无设备树驱动为例进行实验:

LED的platform设备文件程序:

#include <linux/types.h>
#include <linux/kernel.h>
#include <linux/delay.h>
#include <linux/ide.h>
#include <linux/init.h>
#include <linux/module.h>
#include <linux/errno.h>
#include <linux/gpio.h>
#include <linux/of_gpio.h>
#include <linux/cdev.h>
#include <linux/device.h>
#include <linux/of.h>
#include <linux/of_address.h>
#include <asm/mach/map.h>
#include <asm/uaccess.h>
#include <asm/io.h>
#include<linux/atomic.h>
#include<linux/string.h>
#include<linux/interrupt.h>
#include<linux/fcntl.h>
#include<linux/poll.h>
#include<linux/ide.h>
#include<linux/platform_device.h>


/* 寄存器物理地址 */
#define CCM_CCGR1_BASE				(0X020C406C)	
#define SW_MUX_GPIO1_IO03_BASE		(0X020E0068)
#define SW_PAD_GPIO1_IO03_BASE		(0X020E02F4)
#define GPIO1_DR_BASE				(0X0209C000)
#define GPIO1_GDIR_BASE				(0X0209C004)

#define REGISTER_LENGTH             4


void leddevice_release(struct device *dev)
{
    printk("leddevice release\r\n");
}



static struct resource led_resources[] = {
    [0] = {
        .start = CCM_CCGR1_BASE,
        .end = CCM_CCGR1_BASE + REGISTER_LENGTH - 1,
        .flags = IORESOURCE_MEM,
    },
    [1] = {
        .start = SW_MUX_GPIO1_IO03_BASE,
        .end = SW_MUX_GPIO1_IO03_BASE + REGISTER_LENGTH - 1,
        .flags = IORESOURCE_MEM,
    },
    [2] = {
        .start = SW_PAD_GPIO1_IO03_BASE,
        .end = SW_PAD_GPIO1_IO03_BASE + REGISTER_LENGTH - 1,
        .flags = IORESOURCE_MEM,
    },
    [3] = {
        .start = GPIO1_DR_BASE,
        .end = GPIO1_DR_BASE + REGISTER_LENGTH - 1,
        .flags = IORESOURCE_MEM,
    },
    [4] = {
        .start = GPIO1_GDIR_BASE,
        .end = GPIO1_GDIR_BASE + REGISTER_LENGTH - 1,
        .flags = IORESOURCE_MEM,
    },


};

static struct platform_device leddevice = {
    .name = "imx6ull-led",
    .id = -1,   //表示此设备无id
    .dev = {
        .release = leddevice_release,
    },
    .num_resources = ARRAY_SIZE(led_resources),
    .resource = led_resources,

};


/* 设备加载 */
static int __init leddevice_init(void)
{
    /* 注册platform设备 */
    return  platform_device_register(&leddevice);

}

/* 设备卸载 */
static void __exit leddevice_exit(void)
{
    platform_device_unregister(&leddevice);
}


module_init(leddevice_init);
module_exit(leddevice_exit);
MODULE_LICENSE("GPL");
MODULE_AUTHOR("wzh");

LED的驱动文件程序如下:

#include <linux/types.h>
#include <linux/kernel.h>
#include <linux/delay.h>
#include <linux/ide.h>
#include <linux/init.h>
#include <linux/module.h>
#include <linux/errno.h>
#include <linux/gpio.h>
#include <linux/of_gpio.h>
#include <linux/cdev.h>
#include <linux/device.h>
#include <linux/of.h>
#include <linux/of_address.h>
#include <asm/mach/map.h>
#include <asm/uaccess.h>
#include <asm/io.h>
#include<linux/atomic.h>
#include<linux/string.h>
#include<linux/interrupt.h>
#include<linux/fcntl.h>
#include<linux/poll.h>
#include<linux/ide.h>
#include<linux/platform_device.h>

#define PLATFORM_NAME  "platformled"
#define PLATFORM_COUNT    1

//地址映射后的虚拟地址指针
static void  __iomem * IMX6U_CCM_CCGR1;
static void  __iomem * SW_MUX_GPIO1_IO03;
static void  __iomem * SW_PAD_GPIO1_IO03;
static void  __iomem * GPIO1_GDIR;
static void  __iomem * GPIO1_DR;

#define LEDOFF 0    //关闭
#define LEDON  1    //打开



//led设备结构体
struct newchrled_dev
{
    struct cdev cdev;   //字符设备
    dev_t devid;       //设备号
    struct class * class;    //类
    struct device * device;  //设备
    int major;
    int minor;
};

struct newchrled_dev newchrled;     //led设备


void led_switch(u8 sta)
{
	u32 val = 0;
	if(sta == LEDON) {
		val = readl(GPIO1_DR);
		val &= ~(1 << 3);	
		writel(val, GPIO1_DR);
	}else if(sta == LEDOFF) {
		val = readl(GPIO1_DR);
		val|= (1 << 3);	
		writel(val, GPIO1_DR);
	}	
}

static int newchrled_open(struct inode *inode, struct file *filp)
{
    filp->private_data = &newchrled;
	return 0;
}

static ssize_t newchrled_write(struct file *filp, const char __user *buf, size_t cnt, loff_t *offt)
{
	int retvalue;
	unsigned char databuf[1];
	unsigned char ledstat;

	retvalue = copy_from_user(databuf, buf, cnt);
	if(retvalue < 0) {
		printk("kernel write failed!\r\n");
		return -EFAULT;
	}

	ledstat = databuf[0];		/* 获取状态值 */

	if(ledstat == LEDON) {	
		led_switch(LEDON);		/* 打开LED灯 */
	} else if(ledstat == LEDOFF) {
		led_switch(LEDOFF);	/* 关闭LED灯 */
	}
	return 0;
}

static int newchrled_release(struct inode *inode, struct file *filp)
{
    struct newchrled_dev *dev = (struct newchrled_dev *)filp->private_data;
    
	return 0;
}

static const struct file_operations newchrled_fops=
{
    .owner = THIS_MODULE,
    .write = newchrled_write,
    .open = newchrled_open,
    .release = newchrled_release,
};


static int led_probe(struct platform_device *dev)
{
    int ret = 0;
    u32 val = 0;
    int i = 0;
    struct resource *ledsource[5];

    //printk("led driver probe\r\n");
    /* 初始化LED,字符设备驱动 */
    /*1、从设备中获取资源*/
    for(i = 0;i<5;i++){
        ledsource[i] = platform_get_resource(dev,IORESOURCE_MEM, i);
        if(ledsource[i] == NULL){
            return -EINVAL;
        }
        //resource_size = ledsource[0]->end - ledsource[0]->start +1;
    }
    /* 内存映射 */
    //1、初始化led
	/* 1、寄存器地址映射 */
  	IMX6U_CCM_CCGR1 = ioremap(ledsource[0]->start, resource_size(ledsource[0]));
	SW_MUX_GPIO1_IO03 = ioremap(ledsource[1]->start, resource_size(ledsource[1]));
  	SW_PAD_GPIO1_IO03 = ioremap(ledsource[2]->start, resource_size(ledsource[2]));
	GPIO1_DR = ioremap(ledsource[3]->start, resource_size(ledsource[3]));
	GPIO1_GDIR = ioremap(ledsource[4]->start, resource_size(ledsource[4]));

	/* 2、使能GPIO1时钟 */
	val = readl(IMX6U_CCM_CCGR1);
	val &= ~(3 << 26);	/* 清楚以前的设置 */
	val |= (3 << 26);	/* 设置新值 */
	writel(val, IMX6U_CCM_CCGR1);

	/* 3、设置GPIO1_IO03的复用功能,将其复用为
	 *    GPIO1_IO03,最后设置IO属性。
	 */
	writel(5, SW_MUX_GPIO1_IO03);
	
	writel(0x10B0, SW_PAD_GPIO1_IO03);

	/* 4、设置GPIO1_IO03为输出功能 */
	val = readl(GPIO1_GDIR);
	val &= ~(1 << 3);	/* 清除以前的设置 */
	val |= (1 << 3);	/* 设置为输出 */
	writel(val, GPIO1_GDIR);

	/* 5、默认关闭LED */
	val = readl(GPIO1_DR);
	val |= (1 << 3);	
	writel(val, GPIO1_DR);
    //2、分配设备号
    newchrled.major = 0;    //表示由系统申请设备号
    if(newchrled.major)
    {
        newchrled.devid = MKDEV(newchrled.major,0);
        ret = register_chrdev_region(newchrled.devid,PLATFORM_COUNT,PLATFORM_NAME);
    }
    else
    {
        ret = alloc_chrdev_region(&newchrled.devid, 0, PLATFORM_COUNT,PLATFORM_NAME);
        newchrled.major = MAJOR(newchrled.devid);
        newchrled.minor = MINOR(newchrled.devid);
    }

    if(ret < 0)
    {
        printk("newchrled chrdev_region err!\r\n");
        goto fail_devid;
    }
    printk("newchrled major = %d,minor = %d\r\n",newchrled.major,newchrled.minor);

    
    //3、注册字符设备
    newchrled.cdev.owner = THIS_MODULE;
    cdev_init(&newchrled.cdev,&newchrled_fops);

    ret = cdev_add(&newchrled.cdev, newchrled.devid, PLATFORM_COUNT);
    if(ret < 0)
        goto fail_cdev;

    //4、自动创建设备节点
	newchrled.class = class_create(THIS_MODULE, PLATFORM_NAME);
	if (IS_ERR(newchrled.class))
    {
        ret = PTR_ERR(newchrled.class);
        goto fail_class;  
    }
  

    newchrled.device = device_create(newchrled.class, NULL, newchrled.devid, NULL, PLATFORM_NAME);
	if (IS_ERR(newchrled.device))
    {
    	ret = PTR_ERR(newchrled.device); 
        goto fail_device;
    }

    return 0;
fail_device:
    class_destroy(newchrled.class);
fail_class:
    cdev_del(&newchrled.cdev);
fail_cdev:
	unregister_chrdev_region(newchrled.devid, PLATFORM_COUNT);
fail_devid:
    return ret;

}

static int led_remove(struct platform_device *dev)
{
    //printk("led driver remove\r\n");
    u32 val = 0;
    val = readl(GPIO1_DR);
	val |= (1 << 3);	
	writel(val, GPIO1_DR);

	/* 取消映射 */
	iounmap(IMX6U_CCM_CCGR1);
	iounmap(SW_MUX_GPIO1_IO03);
	iounmap(SW_PAD_GPIO1_IO03);
	iounmap(GPIO1_DR);
	iounmap(GPIO1_GDIR);
    printk("newchrled exit\r\n");

    //删除字符设备
    cdev_del(&newchrled.cdev);
    //1、注销设备号
    unregister_chrdev_region(newchrled.devid, PLATFORM_COUNT);
    //设备摧毁
    device_destroy(newchrled.class, newchrled.devid);
    //类的删除
    class_destroy(newchrled.class); 

    return 0;
}

/* platform驱动结构体 */
static struct platform_driver led_driver = {
    .driver = {
        .name = "imx6ull-led",  //驱动名字,用于和设备匹配
    },
    .probe = led_probe,
    .remove = led_remove,

};



/* 驱动加载 */
static int __init leddriver_init(void)
{
    /* 注册platform驱动*/
    return  platform_driver_register(&led_driver);

}

/* 驱动卸载 */
static void __exit leddriver_exit(void)
{
   platform_driver_unregister(&led_driver);
}


module_init(leddriver_init);
module_exit(leddriver_exit);
MODULE_LICENSE("GPL");
MODULE_AUTHOR("wzh");

LED灯的有设备驱动为例进行实验:(常用)

#include <linux/types.h>
#include <linux/kernel.h>
#include <linux/delay.h>
#include <linux/ide.h>
#include <linux/init.h>
#include <linux/module.h>
#include <linux/errno.h>
#include <linux/gpio.h>
#include <linux/of_gpio.h>
#include <linux/cdev.h>
#include <linux/device.h>
#include <linux/of.h>
#include <linux/of_address.h>
#include <asm/mach/map.h>
#include <asm/uaccess.h>
#include <asm/io.h>
#include<linux/atomic.h>
#include<linux/string.h>
#include<linux/interrupt.h>
#include<linux/fcntl.h>
#include<linux/poll.h>
#include<linux/ide.h>
#include<linux/platform_device.h>



#define GPIOLED_CNT 1
#define GPIOLED_NAME "dtsplatled"

#define LEDOFF  0
#define LEDON   1

/*gpio设备结构体*/
struct gpioled_dev{
    dev_t devid;
    int major;
    int minor;
    struct cdev cdev;
    struct class * class;
    struct device *device;
    struct device_node *nd;
    int led_gpio;
};

struct gpioled_dev gpioled; //led


static int led_open(struct inode *inode, struct file *filp)
{
	filp->private_data = &gpioled; /* 设置私有数据 */
	return 0;
}

static ssize_t led_read(struct file *filp, char __user *buf, size_t cnt, loff_t *offt)
{
	return 0;
}

static ssize_t led_write(struct file *filp, const char __user *buf, size_t cnt, loff_t *offt)
{
    int ret;
    unsigned char databuf[1];
    struct gpioled_dev *dev = filp->private_data;
    ret = copy_from_user(databuf,buf,cnt);
    if(ret < 0){
        return -EINVAL;
    }

    if(databuf[0] == LEDON){
        gpio_set_value(dev->led_gpio,0);
    }else if(databuf[0] == LEDOFF){
         gpio_set_value(dev->led_gpio,1);
    }


	return 0;
}

static int led_release(struct inode *inode, struct file *filp)
{
	return 0;
}

//操作集
static struct file_operations led_fops = {
	.owner = THIS_MODULE,
	.open = led_open,
	.read = led_read,
	.write = led_write,
	.release = 	led_release,
};

int led_probe(struct platform_device * dev)
{
    int ret = 0;
    /*注册字符设备驱动*/
    gpioled.major = 0;
    if(gpioled.major){      //给定主设备号
        gpioled.devid = MKDEV(gpioled.major,0);
        register_chrdev_region(gpioled.devid,GPIOLED_CNT,GPIOLED_NAME);
    }else{          //没给定主设备号
        alloc_chrdev_region(&gpioled.devid,0,GPIOLED_CNT,GPIOLED_NAME);
        gpioled.major = MAJOR(gpioled.devid);
        gpioled.minor = MINOR(gpioled.devid);
    }
    printk("gpioled major = %d , gpioled minor = %d\r\n",gpioled.major,gpioled.minor);

    /*2、初始化cdev*/
    gpioled.cdev.owner =  THIS_MODULE;
    cdev_init(&gpioled.cdev,&led_fops);

    /*添加CDEV*/
    cdev_add(&gpioled.cdev,gpioled.devid,GPIOLED_CNT);

    /*3、创建类*/
    gpioled.class = class_create(THIS_MODULE,GPIOLED_NAME);
    if(IS_ERR(gpioled.class)){
        return PTR_ERR(gpioled.class);
    }
    /*4、创建设备*/
    gpioled.device = device_create(gpioled.class,NULL,gpioled.devid,NULL,GPIOLED_NAME);
    if(IS_ERR(gpioled.device)){
        return PTR_ERR(gpioled.device);
    }
#if 0
    /* 1、获取设备节点*/
    gpioled.nd = of_find_node_by_path("/gpioled");
    if(gpioled.nd == NULL){
        ret = -EINVAL;
        goto fail_findnd;
    }
#endif
    gpioled.nd = dev->dev.of_node;     //驱动和设备匹配成功以后,设备信息就会从设备树节点转为platform_device结构体

    /*2、获取LED所对应的GPIO*/
    gpioled.led_gpio = of_get_named_gpio(gpioled.nd,"cd-gpios",0);
    if(gpioled.led_gpio < 0){
        printk("can't find led_gpio\r\n");
        ret = -EINVAL;
        goto fail_findnd;
    }
    printk("led_gpio = %d\r\n",gpioled.led_gpio);

    /*3、申请IO*/
    ret = gpio_request(gpioled.led_gpio,"ledgpio");
    if(ret){
        printk("Failed to request the led gpio\r\n");
        ret = -EINVAL;
        goto fail_findnd;
    }

    /*4、使用IO,设置为输出*/
    ret = gpio_direction_output(gpioled.led_gpio,1);
    if(ret){
        goto fail_setoutput;
    }

    /*5、输出低电平,点亮LED灯*/
    gpio_set_value(gpioled.led_gpio,0);

    return 0;

fail_setoutput:
    gpio_free(gpioled.led_gpio);
fail_findnd:
    return ret;
}


int led_remove(struct platform_device * dev)
{
    /*关灯*/
    gpio_set_value(gpioled.led_gpio,1);

    /*注销字符设备驱动*/
    cdev_del(&gpioled.cdev);
    unregister_chrdev_region(gpioled.devid,GPIOLED_CNT);

    device_destroy(gpioled.class, gpioled.devid);
	class_destroy(gpioled.class);

    /*释放IO*/
	gpio_free(gpioled.led_gpio);
    return 0;
}

struct of_device_id led_of_match[] = {
    {.compatible = "alientek,gpioled"},
    {/*sentinel*/},
};

struct platform_driver led_driver = {
    .driver = {
        .name = "imx6ull-led",      //无设备树时进行匹配,名字
        .of_match_table = led_of_match,     //设备树匹配表
    },
    .probe = led_probe,
    .remove = led_remove,
};


/* 驱动加载 */
static int __init leddriver_init(void)
{
    return platform_driver_register(&led_driver);
}

/* 驱动卸载 */
static void __exit leddriver_exit(void)
{
    platform_driver_unregister(&led_driver);
}


module_init(leddriver_init);
module_exit(leddriver_exit);
MODULE_LICENSE("GPL");
MODULE_AUTHOR("wzh");

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值