platform驱动的简单介绍
前几篇介绍了最简单的LED驱动写法,在linux2.6中提出了platform驱动,具体为什么要这么做后面博客再解释吧。
这里的platform驱动只是一个框架,并没有体现platform驱动的意义,因为在driver中没有使用device定义的资源。后面会再写一个真正的platfom。本篇算是入门练手,体会下吧。
pltform机制本身使用并不复杂,由两部分组成:platform_device和platform_driver。通过platform机制开发底层驱动的大致流程为:定义platform_deive->注册platform_device->定义platform_driver->注册platform_driver
好了,按照要求我们如果想编写一个platform驱动就需要编写两个模块了。一个设备模块dev。一个驱动模块driver。另外再编写一个应用程序来验证下。
platform_device
//my2416PlatformLedDev.c
#include <linux/miscdevice.h>
#include <linux/delay.h>
#include <asm/irq.h>
#include <mach/regs-gpio.h>
#include <mach/hardware.h>
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/init.h>
#include <linux/mm.h>
#include <linux/fs.h>
#include <linux/types.h>
#include <linux/delay.h>
#include <linux/moduleparam.h>
#include <linux/slab.h>
#include <linux/errno.h>
#include <linux/ioctl.h>
#include <linux/cdev.h>
#include <linux/string.h>
#include <linux/list.h>
#include <linux/pci.h>
#include <asm/uaccess.h>
#include <asm/atomic.h>
#include <asm/unistd.h>
#include <linux/gpio.h> //2.6.32.2内核版本要求
#include <linux/poll.h>
#include <linux/platform_device.h>
#include <linux/gpio.h> //2.6.32.2内核版本要求
/* 参考arch/arm/plat-s3c24xx/devs.c */
/*1. 根据芯片手册来获取资源*/
static struct resource led_resource[] = {
[0] = {
.start = S3C2410_GPBCON,//使用2416开发板的GPB1,暂时我只是定义了资源,但是在driver中并没有使用定义的资源(因为第一次写还不熟悉,后面会再写一个led的platform驱动来使用device定义的资源,这样才能发挥platform驱动的真正意义:platform机制将设备本身的资源注册进内核,由内核统一管理,在驱动中使用这些资源时通过platform device提供的标准结构进行申请并使用。这样提高了驱动和资源的独立性,并且具有较好的可移植性和安全性(这些标准接口是安全的))
.end = S3C2410_GPBDAT,
.flags = IORESOURCE_MEM,
},
//[1] = {
// .start = 5,
// .end = 5,
// .flags = IORESOURCE_IRQ,
//},
};
void led_release(struct device *dev)
{
}
/*1.构建平台设备结构体,将平台资源加入进来*/
struct platform_device led_device = {
.name = "myplatformled", /* 使用名为"myplatformled"的平台驱动 ,加载驱动之后在sys/bus/platform/devices/目录下生成myplatformled设备*/
.id = -