基于platform驱动模型的LED驱动

上一篇博文《platform设备驱动框架搭建分析》主要是根据内核源码来分析platform驱动模型工作的原理,在实际的驱动开发中如何使用Linux的这么一种模型来管理这种类型的设备呢?把tq2440开发板上的LED1当做是平台设备注册到Linux系统中,让系统可以用这种platform驱动来管理他。

①总线层:代码不用我们自己去写,,内核已经提供了

②设备层:向platform总线层注册硬件相关的资源,一般是寄存器地址、内存空间、中断号(序号的一种代表)等等

led_dev.c

#include <linux/module.h>#include <linux/version.h>#include <linux/init.h>#include <linux/kernel.h>#include <linux/types.h>#include <linux/interrupt.h>#include <linux/list.h>#include <linux/timer.h>#include <linux/init.h>#include <linux/serial_core.h>#include <linux/platform_device.h>static struct resource led_resource[] = {[0] = { //GPBCON.start = 0x56000010,.end = 0x56000050 + 8 – 1,.flags = IORESOURCE_MEM, //一般地址类的操作就用这个flags},[1] = { //中断号.start = 5,.end = 5,.flags = IORESOURCE_IRQ, //在这个例子中和中断并没有半毛钱的关系,纯粹就是表示一种序号的意思}};static void led_release(struct device * dev){}/* 构造platform_device结构体 */static struct platform_device led_dev = {.name= "myled",.id= -1,.num_resources = ARRAY_SIZE(led_resource),.resource= led_resource,.dev = {.release = led_release, },};/* 向platform_bus注册一个设置好的platform_device */static int led_dev_init(void){platform_device_register(&led_dev);return 0;}static void led_dev_exit(void){platform_device_unregister(&led_dev);}module_init(led_dev_init);module_exit(led_dev_exit);MODULE_LICENSE("GPL");

③驱动层:从总线那里获取需要的硬件资源,根据要求用它来完成一些硬件相关的操作,然后把驱动注册到总线,同时还需要完成probe成员函数–这是驱动和设备匹配之后的核心工作,这工作一般是向用户空间提供API,即实现并填充file_operation结构体成员函数

led_drv.c

#include <linux/module.h>#include <linux/version.h>#include <linux/init.h>#include <linux/fs.h>#include <linux/interrupt.h>#include <linux/irq.h>#include <linux/sched.h>#include <linux/pm.h>#include <linux/sysctl.h>#include <linux/proc_fs.h>#include <linux/delay.h>#include <linux/platform_device.h>#include <linux/input.h>#include <linux/irq.h>#include <asm/uaccess.h>#include <asm/io.h>static int major;static struct class *cls;static struct device *led_drv_device;/* 在platform_get_resource()和ioremap()之前,下面这变量没有任何实际意义 */static volatile unsigned long *gpio_con;static volatile unsigned long *gpio_dat;static int pin;static int led_open(struct inode *inode, struct file *file){/* 配置为输出 */*gpio_con &= ~(0x3<<(pin*2));*gpio_con |= (0x1<<(pin*2));return 0;}static ssize_t led_write(struct file *file, const char __user *buf, size_t count, loff_t * ppos){int val;copy_from_user(&val, buf, count);if (val == 1){// 点灯*gpio_dat &= ~(1<<pin);}else{// 灭灯*gpio_dat |= (1<<pin);}return 0;}static struct file_operations led_fops = {.owner = THIS_MODULE,.open = led_open,.write=led_write,};/* 驱动和设备匹配成功之后的核心工作:回到了过去注册字符设备那一套 */static int led_probe(struct platform_device *pdev){struct resource*res;/* 根据platform_device的资源进行ioremap */res = platform_get_resource(pdev, IORESOURCE_MEM, 0);gpio_con = ioremap(res->start, res->end – res->start + 1);gpio_dat = gpio_con + 1;res = platform_get_resource(pdev, IORESOURCE_IRQ, 0);pin = res->start;/* 注册字符设备驱动程序 */major = register_chrdev(0, "myled", &led_fops);cls = class_create(THIS_MODULE, "myled");led_drv_device = device_create(cls, NULL, MKDEV(major, 0), NULL, "led"); /* /dev/led */return 0;}static int led_remove(struct platform_device *pdev){device_unregister(led_drv_device);class_destroy(cls);unregister_chrdev(major, "myled");iounmap(gpio_con);return 0;}struct platform_driver led_drv = {.probe= led_probe,.remove= led_remove,.driver= {.name= "myled",}};static int led_drv_init(void){platform_driver_register(&led_drv);return 0;}static void led_drv_exit(void){platform_driver_unregister(&led_drv);}module_init(led_drv_init);module_exit(led_drv_exit);MODULE_LICENSE("GPL");小结:写一个platform驱动需要我们做哪些事情?①一个xxx_drv.c文件:驱动初始化和注销函数:xxx_drv_init()

platform_driver_register(struct platform_driver *drv);

xxx_drv_exit()

platform_driver_unregister(struct platform_driver *drv);

驱动初始化原材料:struct platform_driver xxx_drv = {

.probe= xxx_probe,

.remove= xxx_remove,

.driver= {

.name= "xxx",

}

};实现原材料的成员函数:xxx_probe(struct platform_device *pdev){

//工作:

//1.根据需要向总线获取对应设备资源,

//2.然后拿着这资源该干嘛就干嘛去,有一点是通常都要做的:给应用层提供接口xxx_read() xxx_write() xxx_open() xxx_close()

}xxx_remove(struct platform_device *pdev){

//把刚才probe函数里边注册申请的空间释放掉,动作刚好相反

}②一个xxx_dev.c文件:设备初始化和注销函数:xxx_dev_init()

platform_device_register(struct platform_device *pdev);

xxx_dev_exit()

platform_device_unregister(struct platform_device *pdev);

设备初始化原材料:static struct platform_device xxx_dev = {

.name = "xxx", //重构name成员

.id = -1,

.num_resources = ARRAY_SIZE(xxx_resource),

.resource = xxx_resource,

.dev = {

.release = xxx_release, //这个函数是要自己重定义的不然加载驱动模块时会出错,哪怕这个函数里边什么都不做都好。

},

};定义并构造xxx_resource资源:有如下成员struct resource {

resource_size_t start;

resource_size_t end;

const char *name;

unsigned longflags; //只是一种代表

struct resource *parent, *sibling, *child;

};

可是我知道结果是惨淡的,但还是心存希望!

基于platform驱动模型的LED驱动

相关文章:

你感兴趣的文章:

标签云: