手机版
你好,游客 登录 注册 搜索
背景:
阅读新闻

基于S3C2440的嵌入式Linux驱动——SPI子系统解读(一)

[日期:2012-08-16] 来源:Linux社区  作者:yj4231 [字体:大 中 小]

3. 注册SPI总线

下列函数位于drivers/spi/spi.c中。

  1. struct bus_type spi_bus_type = {  
  2.     .name        = "spi",  
  3.     .dev_attrs    = spi_dev_attrs,  
  4.     .match        = spi_match_device,  
  5.     .uevent        = spi_uevent,  
  6.     .suspend    = spi_suspend,  
  7.     .resume        = spi_resume,  
  8. };  
  9. EXPORT_SYMBOL_GPL(spi_bus_type);  
  10.   
  11. static struct class spi_master_class = {  
  12.     .name        = "spi_master",  
  13.     .owner        = THIS_MODULE,  
  14.     .dev_release    = spi_master_release,  
  15. };  
  16.   
  17. /* portable code must never pass more than 32 bytes */  
  18. #define    SPI_BUFSIZ    max(32,SMP_CACHE_BYTES)   
  19.   
  20. static u8    *buf;  
  21.   
  22. static int __init spi_init(void)  
  23. {  
  24.     int status;  
  25.   
  26.     buf = kmalloc(SPI_BUFSIZ, GFP_KERNEL);  
  27.     if (!buf) {  
  28.         status = -ENOMEM;  
  29.         goto err0;  
  30.     }  
  31.   
  32.     status = bus_register(&spi_bus_type);       /*注册SPI总线*/  
  33.     if (status < 0)  
  34.         goto err1;  
  35.   
  36.     status = class_register(&spi_master_class);/*注册SPI类*/  
  37.     if (status < 0)  
  38.         goto err2;  
  39.     return 0;  
  40.   
  41. err2:  
  42.     bus_unregister(&spi_bus_type);  
  43. err1:  
  44.     kfree(buf);  
  45.     buf = NULL;  
  46. err0:  
  47.     return status;  
  48. }  
  49.   
  50. /* board_info is normally registered in arch_initcall(), 
  51.  * but even essential drivers wait till later 
  52.  * 
  53.  * REVISIT only boardinfo really needs static linking. the rest (device and 
  54.  * driver registration) _could_ be dynamically linked (modular) ... costs 
  55.  * include needing to have boardinfo data structures be much more public. 
  56.  */  
  57. postcore_initcall(spi_init);  

spi_init函数注册SPI总线以及SPI类到内核中。该函数在内核初始化的postcore_initcall阶段被调用。

顺便看看下总线的匹配函数。

下列函数位于drivers/spi/spi.c中。

  1. /* modalias support makes "modprobe $MODALIAS" new-style hotplug work, 
  2.  * and the sysfs version makes coldplug work too. 
  3.  */  
  4.   
  5. static int spi_match_device(struct device *dev, struct device_driver *drv)  
  6. {  
  7.     const struct spi_device *spi = to_spi_device(dev);  
  8.   
  9.     return strcmp(spi->modalias, drv->name) == 0;  
  10. }  

从这里我们可以看出,SPI设备和驱动的匹配是通过spi的modalias字段和driver的name字段,这两个字段相等则绑定设备和驱动。

linux
相关资讯       嵌入式Linux  S3C2440  SPI 
本文评论   查看全部评论 (0)
表情: 表情 姓名: 字数

       

评论声明
  • 尊重网上道德,遵守中华人民共和国的各项有关法律法规
  • 承担一切因您的行为而直接或间接导致的民事或刑事法律责任
  • 本站管理人员有权保留或删除其管辖留言中的任意内容
  • 本站有权在网站内转载或引用您的评论
  • 参与本评论即表明您已经阅读并接受上述条款