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

Linux设备模型之spi子系统

[日期:2012-01-30] 来源:csdn  作者:gdt_a20 [字体:大 中 小]

再看一下对于spi设备结构更高层次的表述结构:

[cpp]
  1. struct spidev_data {  
  2.     dev_t           devt;  
  3.     spinlock_t      spi_lock;  
  4.     struct spi_device   *spi;            //指向spi设备结构   
  5.     struct list_head    device_entry;    //spi设备链表device_list挂接点      
  6.     /* buffer is NULL unless this device is open (users > 0) */  
  7.     struct mutex        buf_lock;  
  8.     unsigned        users;  
  9.     u8          *buffer;  
  10. };  

而driver端的表述结构struct spi_driver,具体如下:

[cpp]
  1. struct spi_driver {  
  2.     const struct spi_device_id *id_table; //匹配的设备表   
  3.     int         (*probe)(struct spi_device *spi);  
  4.     int         (*remove)(struct spi_device *spi);  
  5.     void            (*shutdown)(struct spi_device *spi);  
  6.     int         (*suspend)(struct spi_device *spi, pm_message_t mesg);  
  7.     int         (*resume)(struct spi_device *spi);  
  8.     struct device_driver    driver;  
  9. };  

是不是很标准?哈,好了,关于相关的数据结构就先介绍这么多。

二、spi核心代码的初始化分析

      首先看一下spi总线的注册代码很简单,位于driver/spi/spi.c下:

[cpp]
  1. static int __init spi_init(void)  
  2. {  
  3.     int status;  
  4.    
  5.   //其中buf为static u8 *buf,    
  6.   //SPI_BUFSIZ为#define  SPI_BUFSIZ  max(32,SMP_CACHE_BYTES)   
  7.     buf = kmalloc(SPI_BUFSIZ, GFP_KERNEL);  
  8.     if (!buf) {  
  9.         status = -ENOMEM;  
  10.         goto err0;  
  11.     }  
  12.     
  13.   //注册spi_bus   
  14.     status = bus_register(&spi_bus_type);  
  15.     if (status < 0)  
  16.         goto err1;  
  17.     
  18.   //在sys/class下产生spi_master这个节点,用于自动产生设备节点   
  19.   //下面挂接的是控制器的节点   
  20.     status = class_register(&spi_master_class);  
  21.     if (status < 0)  
  22.         goto err2;  
  23.     return 0;  
  24. err2:  
  25.     bus_unregister(&spi_bus_type);  
  26. err1:  
  27.     kfree(buf);  
  28.     buf = NULL;  
  29. err0:  
  30.     return status;  
  31. }  
  32. //其中注册bus结构为   
  33. struct bus_type spi_bus_type = {  
  34.     .name       = "spi",  
  35.     .dev_attrs  = spi_dev_attrs,  
  36.     .match      = spi_match_device,    //匹配函数   
  37.     .uevent     = spi_uevent,  
  38.     .suspend    = spi_suspend,  
  39.     .resume     = spi_resume,  
  40. };  
  41. //类函数为   
  42. static struct class spi_master_class = {  
  43.     .name       = "spi_master",  
  44.     .owner      = THIS_MODULE,  
  45.     .dev_release    = spi_master_release,  
  46. };  
  47. //顺便把driver与device的匹配函数也一起分析了吧,很简单   
  48. static int spi_match_device(struct device *dev, struct device_driver *drv)  
  49. {  
  50.     const struct spi_device *spi = to_spi_device(dev);  
  51.     const struct spi_driver *sdrv = to_spi_driver(drv);  
  52.     /* Attempt an OF style match */  
  53.       
  54.     //利用of表进行匹配   
  55.     if (of_driver_match_device(dev, drv))  
  56.         return 1;  
  57.     
  58.         //利用id表进行匹配   
  59.     if (sdrv->id_table)  
  60.         return !!spi_match_id(sdrv->id_table, spi);  
  61.     
  62.         //利用名字进行匹配   
  63.     return strcmp(spi->modalias, drv->name) == 0;  
  64. }   

    再看一下driver的注册,spi_register_driver函数:

[cpp]
  1. int spi_register_driver(struct spi_driver *sdrv)  
  2. {  
  3.       
  4.     //很类似于platfor的注册,很简单,   
  5.     //就不具体介绍了   
  6.     sdrv->driver.bus = &spi_bus_type;  
  7.     if (sdrv->probe)  
  8.         sdrv->driver.probe = spi_drv_probe;  
  9.     if (sdrv->remove)  
  10.         sdrv->driver.remove = spi_drv_remove;  
  11.     if (sdrv->shutdown)  
  12.         sdrv->driver.shutdown = spi_drv_shutdown;  
  13.           
  14.     //注册标准的driver,此时会去匹配bus设备链表上   
  15.     //支持的device,找到会调用相应函数   
  16.     return driver_register(&sdrv->driver);  
  17. }  

还有个板级信息的添加函数:

[cpp]
  1. int __init  
  2. spi_register_board_info(struct spi_board_info const *info, unsigned n)  
  3. {  
  4.     struct boardinfo    *bi;  
  5.     bi = kmalloc(sizeof(*bi) + n * sizeof *info, GFP_KERNEL);  
  6.     if (!bi)  
  7.         return -ENOMEM;  
  8.     bi->n_board_info = n;  
  9.     memcpy(bi->board_info, info, n * sizeof *info);  
  10.     mutex_lock(&board_lock);  
  11.     list_add_tail(&bi->list, &board_list);  
  12.     mutex_unlock(&board_lock);  
  13.     return 0;  
  14. }  

函数很简单,利用定义的spi_board_info信息,填充了boardinfo结构,并挂到board_list链表。

最后来看一下一个重量级函数,master的注册函数:

[cpp]
  1. int spi_register_master(struct spi_master *master)  
  2. {  
  3.     static atomic_t     dyn_bus_id = ATOMIC_INIT((1<<15) - 1);  
  4.     struct device       *dev = master->dev.parent;  
  5.     int         status = -ENODEV;  
  6.     int         dynamic = 0;  
  7.   //内嵌的标准设备结构必须初始化好   
  8.     if (!dev)  
  9.         return -ENODEV;  
  10.     /* even if it's just one always-selected device, there must 
  11.      * be at least one chipselect 
  12.      */  
  13.        
  14.     //支持的设备数量为0,出错返回   
  15.     if (master->num_chipselect == 0)  
  16.         return -EINVAL;  
  17.     /* convention:  dynamically assigned bus IDs count down from the max */  
  18.       
  19.     if (master->bus_num < 0) {  
  20.         /* FIXME switch to an IDR based scheme, something like 
  21.          * I2C now uses, so we can't run out of "dynamic" IDs 
  22.          */  
  23.       //控制器编号(总线编号)不能小于0   
  24.         master->bus_num = atomic_dec_return(&dyn_bus_id);  
  25.         dynamic = 1;  
  26.     }  
  27.     spin_lock_init(&master->bus_lock_spinlock);  
  28.     mutex_init(&master->bus_lock_mutex);  
  29.     master->bus_lock_flag = 0;  
  30.     /* register the device, then userspace will see it. 
  31.      * registration fails if the bus ID is in use. 
  32.      */  
  33.        
  34.     //设置master->dev的名字,形式为spi+bus_num,如:spi0   
  35.     dev_set_name(&master->dev, "spi%u", master->bus_num);  
  36.       
  37.     //注册master内嵌的标准device结构   
  38.     status = device_add(&master->dev);  
  39.     if (status < 0)  
  40.         goto done;  
  41.     dev_dbg(dev, "registered master %s%s/n", dev_name(&master->dev),  
  42.             dynamic ? " (dynamic)" : "");  
  43.     /* populate children from any spi device tables */  
  44.       
  45.     //重点哦,扫描并实例化spi设备   
  46.     scan_boardinfo(master);  
  47.     status = 0;  
  48.     /* Register devices from the device tree */  
  49.     of_register_spi_devices(master);  
  50. done:  
  51.     return status;  
  52. }  
  53. //先来看scan_boardinfo这个分支   
  54. static void scan_boardinfo(struct spi_master *master)  
  55. {  
  56.     struct boardinfo    *bi;  
  57.     mutex_lock(&board_lock);  
  58.       
  59.     //找到我们注册的spi相关的板级信息结构体   
  60.     //还记得board_list吧   
  61.     list_for_each_entry(bi, &board_list, list) {  
  62.         struct spi_board_info   *chip = bi->board_info;  
  63.         unsigned        n;  
  64.       
  65.     //因为可能存在多个spi总线,因此spi信息结构也会有   
  66.     //多个,找到bus号匹配的就对了   
  67.         for (n = bi->n_board_info; n > 0; n--, chip++) {  
  68.             if (chip->bus_num != master->bus_num)  
  69.                 continue;  
  70.             /* NOTE: this relies on spi_new_device to 
  71.              * issue diagnostics when given bogus inputs 
  72.              */  
  73.               
  74.             //找到了就要实例化它上面的设备了   
  75.             (void) spi_new_device(master, chip);  
  76.         }  
  77.     }  
  78.     mutex_unlock(&board_lock);  
  79. }  
  80. //跟进spi_new_device函数   
  81. struct spi_device *spi_new_device(struct spi_master *master,  
  82.                   struct spi_board_info *chip)  
  83. {  
  84.     struct spi_device   *proxy;  
  85.     int         status;  
  86.     /* NOTE:  caller did any chip->bus_num checks necessary. 
  87.      * 
  88.      * Also, unless we change the return value convention to use 
  89.      * error-or-pointer (not NULL-or-pointer), troubleshootability 
  90.      * suggests syslogged diagnostics are best here (ugh). 
  91.      */  
  92.     
  93.   //为需要实例化的设备分配内存   
  94.   //同时会将bus成员制定为spi_bus_type,parent   
  95.   //指定为该master,层次关系   
  96.     proxy = spi_alloc_device(master);  
  97.     if (!proxy)  
  98.         return NULL;  
  99.     WARN_ON(strlen(chip->modalias) >= sizeof(proxy->modalias));  
  100.   //各个成员赋值,都是用我们注册的板级信息   
  101.     proxy->chip_select = chip->chip_select;  
  102.     proxy->max_speed_hz = chip->max_speed_hz;  
  103.     proxy->mode = chip->mode;  
  104.     proxy->irq = chip->irq;  
  105.     strlcpy(proxy->modalias, chip->modalias, sizeof(proxy->modalias));  
  106.     proxy->dev.platform_data = (void *) chip->platform_data;  
  107.     proxy->controller_data = chip->controller_data;  
  108.     proxy->controller_state = NULL;  
  109.     
  110.   //把该设备添加到spi总线   
  111.     status = spi_add_device(proxy);  
  112.     if (status < 0) {  
  113.         spi_dev_put(proxy);  
  114.         return NULL;  
  115.     }  
  116.     return proxy;  
  117. }  
  118. //继续spi_add_device函数   
  119. int spi_add_device(struct spi_device *spi)  
  120. {  
  121.     static DEFINE_MUTEX(spi_add_lock);  
  122.     struct device *dev = spi->master->dev.parent;  
  123.     struct device *d;  
  124.     int status;  
  125.     /* Chipselects are numbered 0..max; validate. */  
  126.       
  127.     //spi设备的编号不能大于master定义的最大数目   
  128.     if (spi->chip_select >= spi->master->num_chipselect) {  
  129.         dev_err(dev, "cs%d >= max %d/n",  
  130.             spi->chip_select,  
  131.             spi->master->num_chipselect);  
  132.         return -EINVAL;  
  133.     }  
  134.     /* Set the bus ID string */  
  135.     //设备节点名字,形式:spi0.0   
  136.     dev_set_name(&spi->dev, "%s.%u", dev_name(&spi->master->dev),  
  137.             spi->chip_select);  
  138.   
  139.     /* We need to make sure there's no other device with this 
  140.      * chipselect **BEFORE** we call setup(), else we'll trash 
  141.      * its configuration.  Lock against concurrent add() calls. 
  142.      */  
  143.     mutex_lock(&spi_add_lock);  
  144.     
  145.   //确保设备不会重复注册   
  146.     d = bus_find_device_by_name(&spi_bus_type, NULL, dev_name(&spi->dev));  
  147.     if (d != NULL) {  
  148.         dev_err(dev, "chipselect %d already in use/n",  
  149.                 spi->chip_select);  
  150.         put_device(d);  
  151.         status = -EBUSY;  
  152.         goto done;  
  153.     }  
  154.     /* Drivers may modify this initial i/o setup, but will 
  155.      * normally rely on the device being setup.  Devices 
  156.      * using SPI_CS_HIGH can't coexist well otherwise... 
  157.      */  
  158.        
  159.     //设置spi模式和时钟速率   
  160.     //会调用spi->master->setup函数设置   
  161.     status = spi_setup(spi);  
  162.     if (status < 0) {  
  163.         dev_err(dev, "can't %s %s, status %d/n",  
  164.                 "setup", dev_name(&spi->dev), status);  
  165.         goto done;  
  166.     }  
  167.     /* Device may be bound to an active driver when this returns */  
  168.       
  169.     //将该实例化的设备添加到总线   
  170.     status = device_add(&spi->dev);  
  171.     if (status < 0)  
  172.         dev_err(dev, "can't %s %s, status %d/n",  
  173.                 "add", dev_name(&spi->dev), status);  
  174.     else  
  175.         dev_dbg(dev, "registered child %s/n", dev_name(&spi->dev));  
  176. done:  
  177.     mutex_unlock(&spi_add_lock);  
  178.     return status;  
  179. }  
  180. //最后看下spi_setup函数   
  181. int spi_setup(struct spi_device *spi)  
  182. {  
  183.     unsigned    bad_bits;  
  184.     int     status;  
  185.     /* help drivers fail *cleanly* when they need options 
  186.      * that aren't supported with their current master 
  187.      */  
  188.       
  189.     //必须和当前的master设置的模式匹配   
  190.     bad_bits = spi->mode & ~spi->master->mode_bits;  
  191.     if (bad_bits) {  
  192.         dev_dbg(&spi->dev, "setup: unsupported mode bits %x/n",  
  193.             bad_bits);  
  194.         return -EINVAL;  
  195.     }  
  196.     if (!spi->bits_per_word)  
  197.         spi->bits_per_word = 8;  
  198.           
  199.         //最后调用控制器平台相关的setup成员设置该spi设备   
  200.     status = spi->master->setup(spi);  
  201.     dev_dbg(&spi->dev, "setup mode %d, %s%s%s%s"  
  202.                 "%u bits/w, %u Hz max --> %d/n",  
  203.             (int) (spi->mode & (SPI_CPOL | SPI_CPHA)),  
  204.             (spi->mode & SPI_CS_HIGH) ? "cs_high, " : "",  
  205.             (spi->mode & SPI_LSB_FIRST) ? "lsb, " : "",  
  206.             (spi->mode & SPI_3WIRE) ? "3wire, " : "",  
  207.             (spi->mode & SPI_LOOP) ? "loopback, " : "",  
  208.             spi->bits_per_word, spi->max_speed_hz,  
  209.             status);  
  210.     return status;  
  211. }  
  212. //回头看下一开始下面的那个分支of_register_spi_devices   
  213. void of_register_spi_devices(struct spi_master *master)  
  214. {  
  215.     struct spi_device *spi;  
  216.     struct device_node *nc;  
  217.     const __be32 *prop;  
  218.     int rc;  
  219.     int len;  
  220.   //如果定义了of_node成员才会走该分支   
  221.     if (!master->dev.of_node)  
  222.         return;  
  223.           
  224.   //从控制器master->dev.of_node中去获取注册SPI设备spi_device的信息,然   
  225.   //后分配结构体spi_device并注册   
  226.     for_each_child_of_node(master->dev.of_node, nc) {  
  227.         /* Alloc an spi_device */  
  228.         spi = spi_alloc_device(master);  
  229.         if (!spi) {  
  230.             dev_err(&master->dev, "spi_device alloc error for %s/n",  
  231.                 nc->full_name);  
  232.             spi_dev_put(spi);  
  233.             continue;  
  234.         }  
  235.         /* Select device driver */  
  236.         if (of_modalias_node(nc, spi->modalias,  
  237.                      sizeof(spi->modalias)) < 0) {  
  238.             dev_err(&master->dev, "cannot find modalias for %s/n",  
  239.                 nc->full_name);  
  240.             spi_dev_put(spi);  
  241.             continue;  
  242.         }  
  243.         /* Device address */  
  244.         prop = of_get_property(nc, "reg", &len);  
  245.         if (!prop || len < sizeof(*prop)) {  
  246.             dev_err(&master->dev, "%s has no 'reg' property/n",  
  247.                 nc->full_name);  
  248.             spi_dev_put(spi);  
  249.             continue;  
  250.         }  
  251.         spi->chip_select = be32_to_cpup(prop);  
  252.         /* Mode (clock phase/polarity/etc.) */  
  253.         if (of_find_property(nc, "spi-cpha", NULL))  
  254.             spi->mode |= SPI_CPHA;  
  255.         if (of_find_property(nc, "spi-cpol", NULL))  
  256.             spi->mode |= SPI_CPOL;  
  257.         if (of_find_property(nc, "spi-cs-high", NULL))  
  258.             spi->mode |= SPI_CS_HIGH;  
  259.         /* Device speed */  
  260.         prop = of_get_property(nc, "spi-max-frequency", &len);  
  261.         if (!prop || len < sizeof(*prop)) {  
  262.             dev_err(&master->dev, "%s has no 'spi-max-frequency' property/n",  
  263.                 nc->full_name);  
  264.             spi_dev_put(spi);  
  265.             continue;  
  266.         }  
  267.         spi->max_speed_hz = be32_to_cpup(prop);  
  268.         /* IRQ */  
  269.         spi->irq = irq_of_parse_and_map(nc, 0);  
  270.         /* Store a pointer to the node in the device structure */  
  271.         of_node_get(nc);  
  272.         spi->dev.of_node = nc;  
  273.         /* Register the new device */  
  274.         request_module(spi->modalias);  
  275.         rc = spi_add_device(spi);  
  276.         if (rc) {  
  277.             dev_err(&master->dev, "spi_device register error %s/n",  
  278.                 nc->full_name);  
  279.             spi_dev_put(spi);  
  280.         }  
  281.     }  
  282. }  
linux
本文评论   查看全部评论 (0)
表情: 表情 姓名: 字数

       

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