linux驱动开发 - 12_platform 平台驱动模型


链接: C/C++Linux服务器开发/后台架构师【零声教育】-学习视频教程-腾讯课堂

platform 平台驱动模型

根据总线-驱动-设备驱动模型,IIC、SPI、USB这样实实在在的总线是完全匹配的,但是要有一些外设是没法归结为具体的总线:比如定时器、RTC、LCD等。为此linux内核创造了一个虚拟的总线:platform总线。

  • 方便开发,linux提出了驱动分离与分层。

  • 进一步引出了驱动-总线-设备驱动模型,或者框架。

  • 对于SOC内部的RTC,timer等等不好归结为具体的总线,为此linux内核提出了一个虚拟总线:platform总线,platform设备和platform驱动。

1 platform 总线

Linux系统内核使用bus_type 结构体表示总线,此结构体定义在文件 include/linux/device.h,bus_type 结构体内容如下

struct bus_type {
	const char		*name;						/* 总线名字 */
	const char		*dev_name;
	struct device		*dev_root;
	struct device_attribute	*dev_attrs;	/* use dev_groups instead */
	const struct attribute_group **bus_groups;	/* 总线属性 */
	const struct attribute_group **dev_groups;	/* 设备属性 */
	const struct attribute_group **drv_groups;	/* 驱动属性 */

	int (*match)(struct device *dev, struct device_driver *drv);
	int (*uevent)(struct device *dev, struct kobj_uevent_env *env);
	int (*probe)(struct device *dev);
	int (*remove)(struct device *dev);
	void (*shutdown)(struct device *dev);

	int (*online)(struct device *dev);
	int (*offline)(struct device *dev);

	int (*suspend)(struct device *dev, pm_message_t state);
	int (*resume)(struct device *dev);

	const struct dev_pm_ops *pm;

	const struct iommu_ops *iommu_ops;

	struct subsys_private *p;
	struct lock_class_key lock_key;
};
  • 第 10 行, match 函数,此函数就是完成设备和驱动之间匹配的总使用 match 函数来根据注册的设备来查找对应的驱动,或者根据注册的驱动来查找相应的设备,因此每一条总线都必须实现此函数。 match 函数有两个参数: dev 和 drv,这两个参数分别为 device 和 device_driver 类型,也就是设备和驱动。

platform 总线是 bus_type 的一个具体实例,定义在文件 drivers/base/platform.c, platform 总线定义如下:

struct bus_type platform_bus_type = {
	.name		= "platform",
	.dev_groups	= platform_dev_groups,
	.match		= platform_match,
	.uevent		= platform_uevent,
	.pm		= &platform_dev_pm_ops,
};

platform_bus_type 就是 platform 平台总线,其中 platform_match 就是匹配函数。

驱动和设备是如何匹配的, platform_match 函数定义在文件 drivers/base/platform.c 中,函数内容如下所示

static int platform_match(struct device *dev, 
                          struct device_driver *drv)
{
	struct platform_device *pdev = to_platform_device(dev);
	struct platform_driver *pdrv = to_platform_driver(drv);

	/* When driver_override is set, only bind to the matching driver */
	if (pdev->driver_override)
		return !strcmp(pdev->driver_override, drv->name);

	/* Attempt an OF style match first */
	if (of_driver_match_device(dev, drv))
		return 1;

	/* Then try ACPI style match */
	if (acpi_driver_match_device(dev, drv))
		return 1;

	/* Then try to match against the id table */
	if (pdrv->id_table)
		return platform_match_id(pdrv->id_table, pdev) != NULL;

	/* fall-back to driver name match */
	return (strcmp(pdev->name, drv->name) == 0);
}

驱动和设备的匹配有四种方法 :

  • 第一种匹配方式, OF 类型的匹配,也就是设备树采用的匹配方式,of_driver_match_device 函数 。
  • 第四种匹配方式,如果第三种匹配方式的 id_table 不存在的话就直接比较驱动和设备的 name 字段,看看是不是相等,如果相等的话就匹配成功。

platform匹配过程

根据前面的分析,驱动和设备匹配是通过bus->match函数,platform总线下的match函数就是:platform_match。

platform_match	
	-> of_driver_match_device,设备树
	-> acpi_driver_match_device ACPI类型的
	-> platform_match_id 根据platform_driver-> id_table 
	-> strcmp(pdev->name, drv->name)  //最终的就是比较字符串,就是platform_device->name,和platform_driver->driver->name。无设备树情况下使用。

	1、有设备树的时候:
	of_driver_match_device
		-> of_match_device(drv->of_match_table, dev)  //of_match_table非常重要,
类型为of_device_id。
//compatible属性

2 platform 驱动

platform_driver 结构体表示platform驱 动,此结构体定义在文件include/linux/platform_device.h中

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;
};
  • 第 2 行, probe 函数,当驱动与设备匹配成功以后 probe 函数就会执行

  • 第 7 行, driver 成员,为 device_driver 结构体变量, Linux 内核里面大量使用到了面向对象的思维, device_driver 相当于基类,提供了最基础的驱动框架。 plaform_driver 继承了这个基类,然后在此基础上又添加了一些特有的成员变量。

使用platform_driver_register向内核注册platform驱动。
	platform_driver_register
		-> __platform_driver_register   (platform_driver)
			-> 设置driver的probe为platform_drv_probe, //如果platform_driver的
														// probe函数有效的话。
			-> driver_register
				->执行device_drive->probe,对于platform总线,也就是platform_drv_probe函数。而platform_drv_probe函数会执行platform_driver下的probe函数。
	结论:向内核注册platform驱动的时候,如果驱动和设备匹配成功,最终会执行platform_driver的probe函数。

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

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

platform_driver_register 函数原型如下所示:

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("kaka");

3 platform 设备

platform_device 这个结构体表示 platform 设备,如果内核支持设备树的话就不要再使用 platform_device 来描述设备了,改用设备树去描述 。

platform_device 结构体定义在文件include/linux/platform_device.h 中,结构体内容如下:

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 资源的大小。

  • 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;
};
1、无设备树的时候,此时需要驱动开发人员编写设备注册文件,使用platform_device_register函数注册设备。
2,有设备树,修改设备树的设备节点即可。

当设备与platform的驱动匹配以后,就会执行platform_driver->probe函数。

使用 platform_device_register 函数将设备信息注册到 Linux 内核中,此函数原型如下所示:

int platform_device_register(struct platform_device *pdev)
函数参数和返回值含义如下:
    pdev:要注册的 platform 设备。
    返回值: 负数,失败; 0,成功。

通过 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 

/* 资源 
数组 xxx_resources 表示设备资源,一共有两个资源,分别为设备外设 1 和外设 2 的寄存器信息。
*/
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",	// name 字段要和所使用的驱动中的 name 字段一致
	.id = -1,
	.num_resources = ARRAY_SIZE(xxx_resources),	//num_resources 表示资源大小,其实就是数组 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("zuozhongkai");

当 Linux 内核支持了设备树以后就不需要用户手动去注册 platform 设备了。因为设备信息都放到了设备树中去描述,Linux 内核启动的时候会从设备树中读取设备信息,然后将其组织成 platform_device 形式

4 程序编写

1 LED灯的 platform 设备文件

#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/init.h>
#include <linux/fs.h>
#include <linux/slab.h>
#include <linux/uaccess.h>
#include <linux/io.h>
#include <linux/cdev.h>
#include <linux/device.h>
#include <linux/of.h>
#include <linux/of_address.h>
#include <linux/of_irq.h>
#include <linux/gpio.h>
#include <linux/of_gpio.h>
#include <linux/string.h>
#include <linux/irq.h>
#include <asm/mach/map.h>
#include <asm/uaccess.h>
#include <asm/io.h>
#include <linux/interrupt.h>
#include <linux/poll.h>
#include <linux/fcntl.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,   // 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,
    .dev = {
        .release  = leddevice_release,
    },
    .num_resources = ARRAY_SIZE(led_resources),
    .resource = led_resources,  // led_resources 数组,也就是设备资源,描述了 LED 所要使用到的寄存器信息,
};

/*
 设备加载
 */
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("zuozhongkai");

2 LED 灯的 platform 的驱动文件

#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/init.h>
#include <linux/fs.h>
#include <linux/slab.h>
#include <linux/uaccess.h>
#include <linux/io.h>
#include <linux/cdev.h>
#include <linux/device.h>
#include <linux/of.h>
#include <linux/of_address.h>
#include <linux/of_irq.h>
#include <linux/gpio.h>
#include <linux/of_gpio.h>
#include <linux/string.h>
#include <linux/irq.h>
#include <asm/mach/map.h>
#include <asm/uaccess.h>
#include <asm/io.h>
#include <linux/interrupt.h>
#include <linux/poll.h>
#include <linux/fcntl.h>
#include <linux/ide.h>
#include <linux/platform_device.h>

#define PLATFORM_NAME  "platled"
#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_DR;
static void __iomem *GPIO1_GDIR;

#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设备 */

/* LED灯打开/关闭 */
static void led_switch(u8 sta)
{
    u32 val = 0;

    if(sta == LEDON) {
        val = readl(GPIO1_DR);
        val &= ~(1 << 3);            /* bit3清零,打开LED灯 */
        writel(val, GPIO1_DR); 
    } else if(sta == LEDOFF) {
        val = readl(GPIO1_DR);
        val |= (1 << 3);            /* bit3清零,打开LED灯 */
        writel(val, GPIO1_DR);
    }
}

static int newchrled_open(struct inode *inode, struct file *filp)
{
    filp->private_data = &newchrled;
    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 ssize_t newchrled_write(struct file *filp, const char __user *buf,
			 size_t count, loff_t *ppos)
{
    int retvalue;
    unsigned char databuf[1];

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

    /* 判断是开灯还是关灯 */
    led_switch(databuf[0]);

    return 0;
}

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

// probe 函数,当设备和驱动匹配以后此函数就会执行
static int led_probe(struct platform_device *dev)
{
  int i = 0;
  struct resource *ledsource[5];
  int ret = 0;
  unsigned int val = 0;

  //printk("led driver proe\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;
  }

    /* 内存映射 */
    /* 1,初始化LED灯,地址映射 */
    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,初始化 */
    val = readl(IMX6U_CCM_CCGR1);
    val &= ~(3 << 26);  /* 先清除以前的配置bit26,27 */
    val |= 3 << 26;     /* bit26,27置1 */
    writel(val, IMX6U_CCM_CCGR1);
 
    writel(0x5, SW_MUX_GPIO1_IO03);     /* 设置复用 */
    writel(0X10B0, SW_PAD_GPIO1_IO03);  /* 设置电气属性 */

    val = readl(GPIO1_GDIR);
    val |= 1 << 3;              /* bit3置1,设置为输出 */
    writel(val, GPIO1_GDIR);

    val = readl(GPIO1_DR);
    val |= (1 << 3);            /* bit3置1,关闭LED灯 */
    writel(val, GPIO1_DR);

    newchrled.major = 0;    /* 设置为0,表示由系统申请设备号 */

    /* 2,注册字符设备 */
    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; 
}

//remobe 函数,当卸载 platform 驱动的时候此函数就会执行
static int led_remove(struct platform_device *dev)
{

  unsigned int val = 0;

  printk("led driver remove\r\n");
  val = readl(GPIO1_DR);
  val |= (1 << 3);            /* bit3清零,打开LED灯 */
  writel(val, GPIO1_DR);

  /* 1,取消地址映射 */
  iounmap(IMX6U_CCM_CCGR1);
  iounmap(SW_MUX_GPIO1_IO03);
  iounmap(SW_PAD_GPIO1_IO03);
  iounmap(GPIO1_DR);
  iounmap(GPIO1_GDIR);

  /* 1,删除字符设备 */
  cdev_del(&newchrled.cdev);

  /* 2,注销设备号 */
  unregister_chrdev_region(newchrled.devid, PLATFORM_COUNT);

  /* 3,摧毁设备 */
  device_destroy(newchrled.class, newchrled.devid);
  
  /* 4,摧毁类 */
  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("zuozhongkai");

3 编写测试 APP

#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>


/*
 *argc:应用程序参数个数
 *argv[]:具体的参数内容,字符串形式 
 *./platledApp  <filename>  <0:1> 0表示关灯,1表示开灯
 * ./platledApp /dev/platled 0    关灯
 * ./platledApp /dev/platled 1    开灯
 */

#define LEDOFF 0
#define LEDON 1

int main(int argc, char *argv[])
{
    int fd, retvalue;
    char *filename;
    unsigned char databuf[1];


    if(argc != 3) {
        printf("Error Usage!\r\n");
        return -1;
    }

    filename = argv[1];

    fd = open(filename, O_RDWR);
    if(fd < 0) {
        printf("file %s open failed!\r\n", filename);
        return -1;
    }

    databuf[0] = atoi(argv[2]); /* 将字符转换为数字 */

    retvalue = write(fd, databuf, sizeof(databuf));
    if(retvalue < 0) {
        printf("LED Control Failed!\r\n");
        close(fd);
        return -1;
    }

    close(fd);

    return 0;
}

4 编译驱动程序和测试 APP

①、编译驱动程序

KERNELDIR := /home/kaka/linux/IMX6ULL/linux-imx-rel_imx_4.1.15_2.1.0_ga_alientek
CURRENT_PATH := $(shell pwd)

obj-m := leddevice.o leddriver.o

build: kernel_modules

kernel_modules:
	$(MAKE) -C $(KERNELDIR) M=$(CURRENT_PATH) modules
clean:
	$(MAKE) -C $(KERNELDIR) M=$(CURRENT_PATH) clean

输入如下命令编译出驱动模块文件:

make -j32

编译成功以后就会生成一个名为“leddevice.ko leddriver.ko”的驱动模块文件。

②、编译测试 APP

arm-linux-gnueabihf-gcc ledApp.c -o ledApp

4、运行测试

将编译出来的leddevice.ko 、 leddriver.ko 和 ledApp 拷贝到rootfs/lib/modules/4.1.15目录中 ,输入如下命令
加载 leddevice.ko 设备模块和 leddriver.ko 这个驱动模块。

depmod //第一次加载驱动的时候需要运行此命令
modprobe leddevice.ko //加载设备模块
modprobe leddriver.ko //加载驱动模块

根文件系统中/sys/bus/platform/目录下保存着当前板子 platform 总线下的设备和驱动,其中devices 子目录为 platform 设备, drivers 子目录为 plartofm 驱动。

查看/sys/bus/platform/devices/目录,看一下驱动是否存在
/lib/modules/4.1.15 # ls /sys/bus/platform/devices/imx6ul
imx6ul-pinctrl/  imx6ul-tsc/      imx6ull-led/

查看/sys/bus/platform/drivers/目录,看一下驱动是否存在
/lib/modules/4.1.15 # ls /sys/bus/platform/drivers/imx6ul
imx6ul-pinctrl/  imx6ul-tsc/      imx6ull-led/

驱动和设备匹配成功以后测试 LED 灯驱动了,输入如下命令打开 LED 灯:

./ledApp /dev/platled 1 / /打开 LED 灯 

在输入如下命令关闭 LED 灯:

./ledApp /dev/platled 0 //关闭 LED 灯

观察一下 LED 灯能否打开和关闭,如果可以的话就说明驱动工作正常,如果要卸载驱动的话输入如下命令即可:

rmmod leddevice.ko
rmmod leddriver.ko

设备树下的 platform 驱动

Linux 下的驱动分离与分层,以及总线、设备和驱动这样的驱动框架。基于总线、设备和驱动这样的驱动框架, Linux 内核提出来 platform 这个虚拟总线,相应的也有 platform 设备和 platform 驱动。

最新的 Linux 内核已经支持了设备树,如何在设备树下编写 platform 驱动?

1 设备树下的 platform 驱动简介

  • platform 驱动框架分为总线、设备和驱动,其中总线不需要去管理,这个是 Linux 内核提供的,编写驱动的时候只要关注于设备和驱动的具体实现即可。

  • 在没有设备树的 Linux 内核下,需要分别编写并注册 platform_device 和 platform_driver,分别代表设备和驱动。

  • 在使用设备树的时候,设备的描述被放到了设备树中,因此 platform_device 就不需要去编写了,只需要实现 platform_driver 即可。

1.1 在设备树中创建设备节点

要先在设备树中创建设备节点来描述设备信息,重点是要设置好 compatible属性的值,因为 platform 总线需要通过设备节点的 compatible 属性值来匹配驱动

编写如下设备节点来描述 LED 这个设备:

gpioled {
    #address-cells = <1>;
    #size-cells = <1>;
    compatible = "atkalpha-gpioled";
    pinctrl-names = "default";
    pinctrl-0 = <&pinctrl_led>;
    led-gpio = <&gpio1 3 GPIO_ACTIVE_LOW>;
    status = "okay";
};

compatible 属性值为“atkalpha-gpioled”,写 platform驱动的时of_match_table 属性表中要有“atkalpha-gpioled”

1.2 编写 platform 驱动的时候要注意兼容属性

用设备树的时候 platform 驱动会通过 of_match_table 来保存兼容性值,platform 驱动中 platform_driver 就可以按照如下所示设置:

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

MODULE_DEVICE_TABLE(of, led_of_match);

struct platform_driver led_driver = {
    .driver = {
        .name = "imx6ull-led",  /* 无设备树和设备进行匹配,驱动名字 */
        .of_match_table = led_of_match, /* 设备树匹配表 */ 

    },
    .probe = led_probe,
    .remove = led_remove,
};
  • compatible 值为“atkalpha-gpioled”,驱动中的 compatible 属性和设备中的 compatible 属性相匹配,因此驱动中对应的 probe 函数就会执行。在编写 of_device_id 的时候最后一个元素一定要为空!

  • 通过 MODULE_DEVICE_TABLE 声明一下 leds_of_match 这个设备匹配表。

  • 设置 platform_driver 中的 of_match_table 匹配表为上面创建的 leds_of_match

1.3 编写 platform 驱动

当驱动和设备匹配成功以后就会执行 probe 函数。需要在 probe 函数里面执行字符设备驱动初始化,当注销驱动模块的时候 remove 函数就会执行。

2 程序编写

2.1 platform 驱动程序编写

#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/init.h>
#include <linux/fs.h>
#include <linux/slab.h>
#include <linux/uaccess.h>
#include <linux/io.h>
#include <linux/cdev.h>
#include <linux/device.h>
#include <linux/of.h>
#include <linux/of_address.h>
#include <linux/of_irq.h>
#include <linux/gpio.h>
#include <linux/of_gpio.h>
#include <linux/string.h>
#include <linux/irq.h>
#include <asm/mach/map.h>
#include <asm/uaccess.h>
#include <asm/io.h>
#include <linux/interrupt.h>
#include <linux/poll.h>
#include <linux/fcntl.h>
#include <linux/ide.h>
#include <linux/platform_device.h>

#define GPIOLED_CNT     1
#define GPIOLED_NAME    "dtsplatled"
#define LEDOFF          0
#define LEDON           1

/* gpioled设备结构体 */
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 int led_release(struct inode *inode, struct file *filp)
{
    
    return 0;
}

static ssize_t led_write(struct file *filp, const char __user *buf,
			 size_t count, loff_t *ppos)
{
    int ret;
    unsigned char databuf[1];
    struct gpioled_dev *dev = filp->private_data;

    ret = copy_from_user(databuf, buf, count);
    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 const struct file_operations led_fops = {
    .owner		=	THIS_MODULE,
	.write		=	led_write,
	.open		=	led_open,
	.release	=	led_release,
};

static int led_probe(struct platform_device *dev)
{
    printk("led probe\r\n");

     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, minor = %d\r\n", gpioled.major, gpioled.minor);

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

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

    /* 4、创建类 */
    gpioled.class = class_create(THIS_MODULE, GPIOLED_NAME);
    if(IS_ERR(gpioled.class)) {
        return PTR_ERR(gpioled.class);
    }

    /* 5,创建设备  */
    gpioled.device = device_create(gpioled.class, NULL, gpioled.devid, NULL, GPIOLED_NAME);
    if(IS_ERR(gpioled.device)) {
        return PTR_ERR(gpioled.device);
    }

    /* 1,获取设备节点 */
#if 0
    gpioled.nd = of_find_node_by_path("/gpioled");
    if(gpioled.nd == NULL) {
        ret = -EINVAL;
        goto fail_findnode;
    }
#endif
    gpioled.nd = dev->dev.of_node; 
  
    /* 2, 获取LED所对应的GPIO */
    gpioled.led_gpio = of_get_named_gpio(gpioled.nd, "led-gpios", 0);
    if(gpioled.led_gpio < 0) {
        printk("can't find led gpio\r\n");
        ret = -EINVAL;
        goto fail_findnode;
    }
    printk("led gpio num = %d\r\n", gpioled.led_gpio);

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

    /* 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_findnode:
    return ret;
}

static int led_remove(struct platform_device *dev)
{
    printk("led remove\r\n");

        /* 关灯 */
    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("zuozhongkai");

2.2 编写测试 APP

#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>


/*
 *argc:应用程序参数个数
 *argv[]:具体的参数内容,字符串形式 
 *./platledApp  <filename>  <0:1> 0表示关灯,1表示开灯
 * ./platledApp /dev/platled 0    关灯
 * ./platledApp /dev/platled 1    开灯
 */

#define LEDOFF 0
#define LEDON 1

int main(int argc, char *argv[])
{
    int fd, retvalue;
    char *filename;
    unsigned char databuf[1];


    if(argc != 3) {
        printf("Error Usage!\r\n");
        return -1;
    }

    filename = argv[1];

    fd = open(filename, O_RDWR);
    if(fd < 0) {
        printf("file %s open failed!\r\n", filename);
        return -1;
    }

    databuf[0] = atoi(argv[2]); /* 将字符转换为数字 */

    retvalue = write(fd, databuf, sizeof(databuf));
    if(retvalue < 0) {
        printf("LED Control Failed!\r\n");
        close(fd);
        return -1;
    }

    close(fd);

    return 0;
}

2.3 编译驱动程序和测试 APP

①、编译驱动程序

KERNELDIR := /home/kaka/linux/IMX6ULL/linux-imx-rel_imx_4.1.15_2.1.0_ga_alientek
CURRENT_PATH := $(shell pwd)

obj-m := leddriver.o

build: kernel_modules

kernel_modules:
	$(MAKE) -C $(KERNELDIR) M=$(CURRENT_PATH) modules
clean:
	$(MAKE) -C $(KERNELDIR) M=$(CURRENT_PATH) clean

输入如下命令编译出驱动模块文件:

make -j32

编译成功以后就会生成一个名为“leddriver.ko”的驱动模块文件。

②、编译测试 APP

arm-linux-gnueabihf-gcc ledApp.c -o ledApp

2.4 运行测试

将编译出来的 leddriver.ko 和 ledApp 拷贝到rootfs/lib/modules/4.1.15目录中 ,输入如下命令
加载 leddriver.ko 这个驱动模块。

depmod //第一次加载驱动的时候需要运行此命令
modprobe leddriver.ko //加载驱动模块

根文件系统中/sys/bus/platform/目录下保存着当前板子 platform 总线下的设备和驱动,其中devices 子目录为 platform 设备, drivers 子目录为 plartofm 驱动。

查看/sys/bus/platform/devices/目录,看一下驱动是否存在
/lib/modules/4.1.15 # ls /sys/bus/platform/devices/imx6ul
imx6ul-pinctrl/  imx6ul-tsc/      imx6ull-led/

查看/sys/bus/platform/drivers/目录,看一下驱动是否存在
/lib/modules/4.1.15 # ls /sys/bus/platform/drivers/imx6ul
imx6ul-pinctrl/  imx6ul-tsc/      imx6ull-led/

驱动和设备匹配成功以后测试 LED 灯驱动了,输入如下命令打开 LED 灯:

./ledApp /dev/platled 1 / /打开 LED 灯 

在输入如下命令关闭 LED 灯:

./ledApp /dev/platled 0 //关闭 LED 灯

观察一下 LED 灯能否打开和关闭,如果可以的话就说明驱动工作正常,如果要卸载驱动的话输入如下命令即可:

rmmod leddevice.ko
rmmod leddriver.ko
  • 1
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值