linux的led之platform驱动 210开发板 来自ZUOPENG

来源:互联网 发布:淘宝客服催单算提成吗 编辑:程序博客网 时间:2024/06/06 17:46

1、概述

在一般情况下,2.6内核中已经初始化并挂载了一条platform总线在sysfs文件系统中。那么我们编写platform模型驱动时,需要完成两个工作:1:实现platform驱动 2:实现platform设备,然而在实现这两个工作的过程中还需要实现其他的很多小工作,在后面介绍。platform模型驱动的实现过程核心架构就很简单,如下所示。

platform驱动模型三个对象:platform总线、platform设备、platform驱动。
platform总线对应的内核结构:struct bus_type-->它包含的最关键的函数:match()
platform设备对应的内核结构:struct platform_device-->注册:platform_device_register(unregiste)
platform驱动对应的内核结构:struct platform_driver-->注册:platform_driver_register(unregiste)

简单介绍下platform驱动的工作过程:设备(或驱动)注册的时候,都会引发总线调用自己的match函数来寻找目前platform总线是否挂载有与该设备(或驱动)名字匹配的驱动(或设备)( http://blog.csdn.net/xy010902100449/article/details/45700523 前面我们讲了platform设备驱动的match自动匹配过程), 如果存在则将双方绑定;如果先注册设备,驱动还没有注册,那么设备在被注册到总线上时,将不会匹配到与自己同名的驱动,然后在驱动注册到总线上时,因为设备已注册,那么总线会立即匹配与绑定这时的同名的设备与驱动,再调用驱动中的probe函数等;如果是驱动先注册,同设备驱动一样先会匹配失败,匹配失败将导致它的probe函数暂不调用,而是要等到设备注册成功并与自己匹配绑定后才会调用,device注册和driver注册顺序没关系,随便哪个先注册。

2、代码

[cpp] view plaincopyprint?
  1. /* 
  2.  * Author: ZP1015  
  3.  * 
  4.  * Copyright 2015 SCUT. 
  5.  * 
  6.  * platform  device for led  
  7.  */  
  8. #include <linux/init.h>  
  9. #include <linux/module.h>  
  10. #include <linux/fs.h>  
  11. #include <asm/io.h>  
  12. #include <asm/uaccess.h>  
  13. #include <linux/device.h>  
  14. #include <linux/cdev.h>  
  15. #include <linux/platform_device.h>  
  16.   
  17. static void Myled_platform_device_release(struct device * dev)  
  18. {  
  19.     return ;  
  20. }  
  21. static struct resource Myled_resource[] = {  
  22.         [0] = {  
  23.                 .start = 0xe0200280,  
  24.                 .end   = 0xe0200280 + 12,  
  25.                 .flags = IORESOURCE_MEM  
  26.         },  
  27. };  
  28. static struct platform_device Myledplatform_device_led = {  
  29.         .name           = "Myled_platform_device_driver",  
  30.         .id             = -1,  
  31.         .num_resources  = ARRAY_SIZE(Myled_resource),  
  32.         .resource       = Myled_resource,  
  33.         .dev            = {  
  34.               .release  = Myled_platform_device_release,  
  35.         },  
  36. };  
  37. static int __init Myled_platform_device_init(void)  
  38. {  
  39.     printk("Myled_platform_device add ok!\n");  
  40.     return platform_device_register(&Myledplatform_device_led);  
  41. }  
  42. static void __exit Myled_platform_device_exit(void)  
  43. {  
  44.     printk("Myled_platform_device remove ok!\n");  
  45.     platform_device_unregister(&Myledplatform_device_led);  
  46. }  
  47. MODULE_AUTHOR("ZP1015");  
  48. MODULE_LICENSE("GPL");  
  49. module_init(Myled_platform_device_init);  
  50. module_exit(Myled_platform_device_exit);  

[cpp] view plaincopyprint?
  1. /* 
  2.  * Author: ZP1015  
  3.  * 
  4.  * Copyright 2015 SCUT. 
  5.  * 
  6.  * platform  driver for led  
  7.  */  
  8. #include <linux/init.h>  
  9. #include <linux/module.h>  
  10. #include <linux/fs.h>  
  11. #include <asm/io.h>  
  12. #include <asm/uaccess.h>  
  13. #include <linux/device.h>  
  14. #include <linux/cdev.h>  
  15. #include <linux/platform_device.h>  
  16.   
  17. #define GLOBAL_LED_MAJOR  250  
  18.   
  19. static unsigned int global_led_major = GLOBAL_LED_MAJOR;   
  20. static struct cdev *led_cdev = NULL;   
  21. static struct class *led_class = NULL;  
  22.   
  23. volatile unsigned long *gpbcon = NULL;  
  24. volatile unsigned long *gpbdat = NULL;   
  25. volatile unsigned long *gpbup = NULL;   
  26.   
  27. static int Myled_open(struct inode * inode,struct file * file)  
  28. {  
  29.     return 0;  
  30. }  
  31.   
  32. static ssize_t Myled_read(struct file * file,const char __user * in,size_t size,loff_t * off)  
  33. {  
  34.     return 0;  
  35. }  
  36.   
  37. static ssize_t Myled_write(struct file * file,const char __user * in,size_t size,loff_t * off)  
  38. {  
  39.     return 0;  
  40. }  
  41.   
  42. static void myled_configure(void)  
  43. {     
  44.     *gpbcon &= ~((0x3<<(0*4)) | (0x3<<(1*4)) | (0x3<<(2*4)) | (0x3<<(3*4)));  
  45.     *gpbcon |=  ((0x1<<(0*4)) | (0x1<<(1*4)) | (0x1<<(2*4)) | (0x1<<(3*4)));  
  46. }  
  47.   
  48.   
  49. static void myled_on(void)  
  50. {  
  51.     *gpbdat &= ~((1<<0) | (1<<1) | (1<<2) | (1<<3));// 点灯  
  52. }  
  53. static void myled_off(void)  
  54. {  
  55.     *gpbdat |= (1 << 0)|(1 << 1)|(1 << 2)|(1 << 3);// 灭灯      
  56. }  
  57.   
  58. struct file_operations led_fops = {  
  59.     .owner = THIS_MODULE,  
  60.     .open  = Myled_open,  
  61.     .read  = Myled_read,  
  62.     .write = Myled_write,  
  63. };  
  64. static int __devinit Myled_probe(struct platform_device *pdev)  
  65. {  
  66.     int ret;  
  67.     int err;  
  68.     dev_t devno;  
  69.     struct resource *pIORESOURCE_MEM;  
  70.     devno = MKDEV(global_led_major,0);  
  71.     printk(KERN_ALERT"Myled_probe!\n");  
  72.       
  73.     if (devno) {  
  74.         ret = register_chrdev_region(devno,1,"Myled_platfor_driver");  
  75.     }   
  76.     else {  
  77.         ret = alloc_chrdev_region(&devno,0,1,"Myled_platfor_driver");  
  78.         global_led_major = MAJOR(devno);  
  79.     }  
  80.       
  81.     if (ret < 0) {  
  82.         return ret;  
  83.     }  
  84.       
  85.     led_cdev = cdev_alloc();  
  86.     cdev_init(led_cdev,&led_fops);  
  87.     led_cdev->owner = THIS_MODULE;  
  88.     err = cdev_add(led_cdev,devno,1);  
  89.     led_class = class_create(THIS_MODULE,"Myled_platfor_driver");  
  90.     device_create(led_class,NULL,MKDEV(global_led_major,0),NULL,"platfor_driver_for_Myled");  
  91.   
  92.     pIORESOURCE_MEM = platform_get_resource(pdev,IORESOURCE_MEM,0);  
  93.     gpbcon = ioremap(pIORESOURCE_MEM->start,pIORESOURCE_MEM->end - pIORESOURCE_MEM->start);  
  94.       
  95.   
  96.     gpbdat = gpbcon + 1;  
  97.     gpbup  = gpbcon + 2;  
  98.       
  99.     myled_configure();  
  100.     myled_on();  
  101.       
  102.     if (err) {  
  103.         printk(KERN_NOTICE"Error %d adding led_cdev",err);  
  104.         return -1;  
  105.     } else {  
  106.         printk(KERN_NOTICE"platform_driver_for_Myled init ok!\n");  
  107.         return 0;     
  108.     }  
  109.   
  110.       
  111. }  
  112.   
  113. static int __devexit Myled_remove(struct platform_device *pdev)  
  114. {  
  115.     printk("Myled_remove!\n");  
  116.     cdev_del(led_cdev);  
  117.     iounmap(gpbcon);  
  118.     unregister_chrdev_region(MKDEV(global_led_major,0),1);  
  119.     device_destroy(led_class, MKDEV(global_led_major,0));  
  120.     class_destroy(led_class);  
  121.     myled_off();  
  122.     return 0;  
  123. }  
  124. static struct platform_driver Myled_platform_driver = {  
  125.     .probe  = Myled_probe,  
  126.     .remove = __devexit_p(Myled_remove),  
  127.     .driver = {  
  128.         .name = "Myled_platform_device_driver",  
  129.         .owner = THIS_MODULE,  
  130.     }  
  131. };  
  132. static int __init Myled_platform_driver_init(void)  
  133. {  
  134.     printk("platform_driver_for_Myled init\n");  
  135.     return platform_driver_register(&Myled_platform_driver);  
  136. }  
  137.   
  138. static void __exit Myled_platform_driver_exit(void)  
  139. {  
  140.     printk("platform_driver_for_Myled exit\n");  
  141.     platform_driver_unregister(&Myled_platform_driver);  
  142. }  
  143. MODULE_AUTHOR("ZP1015");  
  144. MODULE_LICENSE("GPL");  
  145. module_param(global_led_major,int,S_IRUGO);  
  146. module_init(Myled_platform_driver_init);  
  147. module_exit(Myled_platform_driver_exit);  

3、 实现结果

先注册设备,然后注册设备驱动
insmod 一下灯全亮,rmmod 一下等全灭。
0 0
原创粉丝点击