Linux驅動修煉之道-SPI驅動框架源碼分析(中)

來自:http://blog.csdn.net/woshixingaaa/article/details/6574220

這篇來分析spi子系統的建立過程。
嵌入式微處理器訪問SPI設備有兩種方式:使用GPIO模擬SPI接口的工作時序或者使用SPI控制器。使用GPIO模擬SPI接口的工作時序是非常容易實現的,但是會導致大量的時間耗費在模擬SPI接口的時序上,訪問效率比較低,容易成爲系統瓶頸。這裏主要分析使用SPI控制器的情況。

這個是由sys文件系統導出的spi子系統在內核中的視圖了。
首先了解一下Linux內核中的幾個文件:spi.c也就是spi子系統的核心了,spi_s3c24xx.c是s3c24xx系列芯片的SPI controller驅動,它向更上層的SPI核心層(spi.c)提供接口用來控制芯片的SPI controller,是一個被其他驅動使用的驅動。而spidev.c是在覈心層基礎之上將SPI controller模擬成一個字符型的驅動,向文件系統提供標準的文件系統接口,用來操作對應的SPI controller。
下面我們來看看spi子系統是怎麼註冊進內核的:

  1. static int __init spi_init(void)  
  2. {  
  3.     int status;  
  4.     buf = kmalloc(SPI_BUFSIZ, GFP_KERNEL);  
  5.     if (!buf) {  
  6.         status = -ENOMEM;  
  7.         goto err0;  
  8.     }  
  9.     status = bus_register(&spi_bus_type);  
  10.     if (status < 0)  
  11.         goto err1;  
  12.     status = class_register(&spi_master_class);  
  13.     if (status < 0)  
  14.         goto err2;  
  15.     return 0;  
  16. err2:  
  17.     bus_unregister(&spi_bus_type);  
  18. err1:  
  19.     kfree(buf);  
  20.     buf = NULL;  
  21. err0:  
  22.     return status;  
  23. }  
  24. postcore_initcall(spi_init);  
這裏註冊了一個spi_bus_type,也就是一個spi總線,和一個spi_master的class。分別對應上圖中sys/bus/下的spi目錄和sys/class/下的spi_master目錄。

下面來分析SPI controller驅動的註冊與初始化過程,首先執行的是s3c24xx_spi_init。

  1. static int __init s3c24xx_spi_init(void)  
  2. {  
  3.         return platform_driver_probe(&s3c24xx_spi_driver, s3c24xx_spi_probe);  
  4. }  
platform_driver_probe中完成了s3c24xx_spi_driver這個平臺驅動的註冊,相應的平臺設備在devs.c中定義,在smdk2440_devices中添加&s3c_device_spi0,&s3c_device_spi1,這就生成了圖中所示的s3c24xx-spi.0與s3c24xx-spi.1,當然了這圖是在網上找的,不是我畫的,所以是6410的。這裏s3c24xx-spi.0表示s3c2440的spi controller的0號接口,s3c24xx-spi.1表示s3c2440的spi controller的1號接口。註冊了s3c24xx_spi_driver後,賦值了平臺驅動的probe函數爲s3c24xx_spi_probe。所以當match成功後,調用s3c24xx_spi_probe,這裏看其實現:

  1. <span style="font-size:18px;">static int __init s3c24xx_spi_probe(struct platform_device *pdev)  
  2. {  
  3.     struct s3c2410_spi_info *pdata;  
  4.     struct s3c24xx_spi *hw;  
  5.     struct spi_master *master;  
  6.     struct resource *res;  
  7.     int err = 0;  
  8.     /*分配struct spi_master+struct s3c24xx_spi大小的數據,把s3c24xx_spi設爲spi_master的私有數據*/  
  9.     master = spi_alloc_master(&pdev->dev, sizeof(struct s3c24xx_spi));  
  10.     if (master == NULL) {  
  11.         dev_err(&pdev->dev, "No memory for spi_master\n");  
  12.         err = -ENOMEM;  
  13.         goto err_nomem;  
  14.     }  
  15.     /*從master中獲得s3c24xx_spi*/  
  16.     hw = spi_master_get_devdata(master);  
  17.     memset(hw, 0, sizeof(struct s3c24xx_spi));  
  18.   
  19.   
  20.     hw->master = spi_master_get(master);  
  21.     /*驅動移植的時候需要實現的重要結構,初始化爲&s3c2410_spi0_platdata*/  
  22.     hw->pdata = pdata = pdev->dev.platform_data;  
  23.     hw->dev = &pdev->dev;  
  24.   
  25.   
  26.     if (pdata == NULL) {  
  27.         dev_err(&pdev->dev, "No platform data supplied\n");  
  28.         err = -ENOENT;  
  29.         goto err_no_pdata;  
  30.     }  
  31.     /*設置平臺的私有數據爲s3c24xx_spi*/  
  32.     platform_set_drvdata(pdev, hw);  
  33.     init_completion(&hw->done);  
  34.   
  35.   
  36.     /* setup the master state. */  
  37.     /*該總線上的設備數*/  
  38.     master->num_chipselect = hw->pdata->num_cs;  
  39.     /*總線號*/    
  40.     master->bus_num = pdata->bus_num;  
  41.   
  42.   
  43.     /* setup the state for the bitbang driver */  
  44.     /*spi_bitbang專門負責數據的傳輸*/  
  45.     hw->bitbang.master         = hw->master;  
  46.     hw->bitbang.setup_transfer = s3c24xx_spi_setupxfer;  
  47.     hw->bitbang.chipselect     = s3c24xx_spi_chipsel;  
  48.     hw->bitbang.txrx_bufs      = s3c24xx_spi_txrx;  
  49.     hw->bitbang.master->setup  = s3c24xx_spi_setup;  
  50.   
  51.   
  52.     dev_dbg(hw->dev, "bitbang at %p\n", &hw->bitbang);  
  53.        
  54.     。。。。。。。。。。。。。。。。。。。。。。。。  
  55.       
  56.     /*初始化設置寄存器,包括對SPIMOSI,SPIMISO,SPICLK引腳的設置*/  
  57.     s3c24xx_spi_initialsetup(hw);  
  58.   
  59.   
  60.     /* register our spi controller */  
  61.   
  62.   
  63.     err = spi_bitbang_start(&hw->bitbang);  
  64.         。。。。。。。。。。。。。。。。。。。。。  
  65. }  
  66. spi controller的register在spi_bitbang_start函數中實現:  
  67. int spi_bitbang_start(struct spi_bitbang *bitbang)  
  68. {  
  69.     int status;  
  70.   
  71.   
  72.     if (!bitbang->master || !bitbang->chipselect)  
  73.         return -EINVAL;  
  74.     /*動態創建一個work_struct結構,它的處理函數是bitbang_work*/  
  75.     INIT_WORK(&bitbang->work, bitbang_work);  
  76.     spin_lock_init(&bitbang->lock);  
  77.     INIT_LIST_HEAD(&bitbang->queue);  
  78.     /*spi的數據傳輸就是用這個方法*/  
  79.     if (!bitbang->master->transfer)  
  80.         bitbang->master->transfer = spi_bitbang_transfer;  
  81.     if (!bitbang->txrx_bufs) {  
  82.         bitbang->use_dma = 0;  
  83.         /*spi_s3c24xx.c中有spi_bitbang_bufs方法,在bitbang_work中被調用*/  
  84.         bitbang->txrx_bufs = spi_bitbang_bufs;  
  85.         if (!bitbang->master->setup) {  
  86.             if (!bitbang->setup_transfer)  
  87.                 bitbang->setup_transfer =  
  88.                      spi_bitbang_setup_transfer;  
  89.             /*在spi_s3c24xx.c中有setup的處理方法,在spi_new_device中被調用*/  
  90.             bitbang->master->setup = spi_bitbang_setup;  
  91.             bitbang->master->cleanup = spi_bitbang_cleanup;  
  92.         }  
  93.     } else if (!bitbang->master->setup)  
  94.         return -EINVAL;  
  95.   
  96.   
  97.     /* this task is the only thing to touch the SPI bits */  
  98.     bitbang->busy = 0;  
  99.     /調用create_singlethread_workqueue創建單個工作線程/  
  100.     bitbang->workqueue = create_singlethread_workqueue(  
  101.             dev_name(bitbang->master->dev.parent));  
  102.     if (bitbang->workqueue == NULL) {  
  103.         status = -EBUSY;  
  104.         goto err1;  
  105.     }  
  106.     status = spi_register_master(bitbang->master);  
  107.     if (status < 0)  
  108.         goto err2;  
  109.     return status;  
  110. err2:  
  111.     destroy_workqueue(bitbang->workqueue);  
  112. err1:  
  113.     return status;  
  114. }</span>  

然後看這裏是怎樣註冊spi主機控制器驅動的:

  1. int spi_register_master(struct spi_master *master)  
  2. {  
  3.     。。。。。。。。。。。。。。。。  
  4.     /*將spi添加到內核,這也是sys/class/Spi_master下產生Spi0,Spi1的原因*/  
  5.     dev_set_name(&master->dev, "spi%u", master->bus_num);  
  6.     status = device_add(&master->dev);  
  7.     scan_boardinfo(master);  
  8. }  

這裏跟蹤scan_boardinfo函數:

  1. static void scan_boardinfo(struct spi_master *master)  
  2. {  
  3.     struct boardinfo    *bi;  
  4. mutex_lock(&board_lock);  
  5.     /*遍歷所有掛在board_list上的struct boardinfo*/  
  6.     list_for_each_entry(bi, &board_list, list) {  
  7.         struct spi_board_info   *chip = bi->board_info;  
  8.         unsigned    n;  
  9.         /*遍歷每個boardinfo管理的spi_board_info,如果設備的總線號與控制器的總線好相等,則創建新設備*/  
  10.         for (n = bi->n_board_info; n > 0; n--, chip++) {  
  11.             if (chip->bus_num != master->bus_num)  
  12.                 continue;  
  13.             (void) spi_new_device(master, chip);  
  14.         }  
  15.     }  
  16.     mutex_unlock(&board_lock);  
  17. }  
在移植的時候我們會在mach-smdk2440.c中的smdk2440_machine_init中添加spi_register_board_info

這個函數完成了將spi_board_info交由boardinfo管理,並把boardinfo掛載到board_list鏈表上。也就是說在系統初始化的時候將spi_device交由到掛在board_list上的boardinfo管理,在spi controller的driver註冊的時候不但註冊這個主機控制器的驅動,還要遍歷這個主機控制器的總線上的spi_device,將總線上的spi_device全部註冊進內核。當註冊進內核並且spi_driver已經註冊的時候,如果總線match成功,則會調用spi_driver的probe函數,這個將在後邊進行分析。

  1. <span style="font-size:18px;">int __init  
  2. spi_register_board_info(struct spi_board_info const *info, unsigned n)  
  3. {  
  4.     struct boardinfo    *bi;  
  5.   
  6.   
  7.     bi = kmalloc(sizeof(*bi) + n * sizeof *info, GFP_KERNEL);  
  8.     if (!bi)  
  9.         return -ENOMEM;  
  10.     bi->n_board_info = n;  
  11.     memcpy(bi->board_info, info, n * sizeof *info);  
  12.   
  13.   
  14.     mutex_lock(&board_lock);  
  15.     list_add_tail(&bi->list, &board_list);  
  16.     mutex_unlock(&board_lock);  
  17.     return 0;  
  18. }</span>  
看一下創建新設備的函數:
  1. <span style="font-size:18px;">struct spi_device *spi_new_device(struct spi_master *master,  
  2.                   struct spi_board_info *chip)  
  3. {  
  4.     struct spi_device   *proxy;  
  5.     int         status;  
  6.     proxy = spi_alloc_device(master);  
  7.     if (!proxy)  
  8.         return NULL;  
  9.   
  10.   
  11.     WARN_ON(strlen(chip->modalias) >= sizeof(proxy->modalias));  
  12.     /*初始化spi_device的各個字段*/  
  13.     proxy->chip_select = chip->chip_select;  
  14.     proxy->max_speed_hz = chip->max_speed_hz;  
  15.     proxy->mode = chip->mode;  
  16.     proxy->irq = chip->irq;  
  17.     /*這裏獲得了spi_device的名字,這個modalias也是在我們移植時在mach-smdk2440.c中的s3c2410_spi0_board中設定的*/  
  18.     strlcpy(proxy->modalias, chip->modalias, sizeof(proxy->modalias));  
  19.     proxy->dev.platform_data = (void *) chip->platform_data;  
  20.     proxy->controller_data = chip->controller_data;  
  21.     proxy->controller_state = NULL;  
  22.     /*主要完成將spi_device添加到內核*/  
  23.     status = spi_add_device(proxy);  
  24.     if (status < 0) {  
  25.         spi_dev_put(proxy);  
  26.         return NULL;  
  27.     }  
  28.   
  29.   
  30.     return proxy;  
  31. }</span>  
下面來看分配spi_alloc_device的函數,主要完成了分配spi_device,並初始化spi->dev的一些字段。
  1. struct spi_device *spi_alloc_device(struct spi_master *master)  
  2. {  
  3.     struct spi_device   *spi;  
  4.     struct device       *dev = master->dev.parent;  
  5.     if (!spi_master_get(master))  
  6.         return NULL;  
  7.     spi = kzalloc(sizeof *spi, GFP_KERNEL);  
  8.     if (!spi) {  
  9.         dev_err(dev, "cannot alloc spi_device\n");  
  10.         spi_master_put(master);  
  11.         return NULL;  
  12.     }  
  13.     spi->master = master;  
  14.     spi->dev.parent = dev;  
  15.     /*設置總線是spi_bus_type,下面會講到spi_device與spi_driver是怎樣match上的*/  
  16.     spi->dev.bus = &spi_bus_type;  
  17.     spi->dev.release = spidev_release;  
  18.     device_initialize(&spi->dev);  
  19.     return spi;  
  20. }  
下面來看分配的這個spi_device是怎樣註冊進內核的:
  1. int spi_add_device(struct spi_device *spi)  
  2. {  
  3.     static DEFINE_MUTEX(spi_add_lock);  
  4.     struct device *dev = spi->master->dev.parent;  
  5.     int status;  
  6.     /*spi_device的片選號不能大於spi控制器的片選數*/  
  7.     if (spi->chip_select >= spi->master->num_chipselect) {  
  8.         dev_err(dev, "cs%d >= max %d\n",  
  9.             spi->chip_select,  
  10.             spi->master->num_chipselect);  
  11.         return -EINVAL;  
  12.     }  
  13.     /*這裏設置是spi_device在Linux設備驅動模型中的name,也就是圖中的spi0.0,而在/dev/下設備節點的名字是proxy->modalias中的名字*/  
  14.     dev_set_name(&spi->dev, "%s.%u", dev_name(&spi->master->dev),  
  15.             spi->chip_select);  
  16.     mutex_lock(&spi_add_lock);  
  17.     /*如果總線上掛的設備已經有這個名字,則設置狀態忙碌,並退出*/  
  18.     if (bus_find_device_by_name(&spi_bus_type, NULL, dev_name(&spi->dev))  
  19.             != NULL) {  
  20.         dev_err(dev, "chipselect %d already in use\n",  
  21.                 spi->chip_select);  
  22.         status = -EBUSY;  
  23.         goto done;  
  24.     }  
  25.     /對spi_device的時鐘等進行設置/  
  26.     status = spi->master->setup(spi);  
  27.     if (status < 0) {  
  28.         dev_err(dev, "can't %s %s, status %d\n",  
  29.                 "setup", dev_name(&spi->dev), status);  
  30.         goto done;  
  31.     }  
  32.     /*添加到內核*/  
  33.     status = device_add(&spi->dev);  
  34.     if (status < 0)  
  35.         dev_err(dev, "can't %s %s, status %d\n",  
  36.                 "add", dev_name(&spi->dev), status);  
  37.     else  
  38.         dev_dbg(dev, "registered child %s\n", dev_name(&spi->dev));  
  39.   
  40.   
  41. done:  
  42.     mutex_unlock(&spi_add_lock);  
  43.     return status;  
  44. }  
  45.   
  46.   
  47. static int s3c24xx_spi_setup(struct spi_device *spi)  
  48. {  
  49.     。。。。。。。。。。。。。。  
  50.     ret = s3c24xx_spi_setupxfer(spi, NULL);  
  51.     。。。。。。。。。。。。。。  
  52. }  
  53.   
  54.   
  55. static int s3c24xx_spi_setupxfer(struct spi_device *spi,  
  56.                  struct spi_transfer *t)  
  57. {  
  58.     struct s3c24xx_spi *hw = to_hw(spi);  
  59.     unsigned int bpw;  
  60.     unsigned int hz;  
  61.     unsigned int div;  
  62.     /*設置了每字長的位數,發送速度*/  
  63.     bpw = t ? t->bits_per_word : spi->bits_per_word;  
  64.     hz  = t ? t->speed_hz : spi->max_speed_hz;  
  65.   
  66.   
  67.     if (bpw != 8) {  
  68.         dev_err(&spi->dev, "invalid bits-per-word (%d)\n", bpw);  
  69.         return -EINVAL;  
  70.     }  
  71.     /*色黃志分頻值*/  
  72.     div = clk_get_rate(hw->clk) / hz;  
  73.   
  74.   
  75.     /* is clk = pclk / (2 * (pre+1)), or is it 
  76.      *    clk = (pclk * 2) / ( pre + 1) */  
  77.   
  78.   
  79.     div /= 2;  
  80.   
  81.   
  82.     if (div > 0)  
  83.         div -= 1;  
  84.   
  85.   
  86.     if (div > 255)  
  87.         div = 255;  
  88.   
  89.   
  90.     dev_dbg(&spi->dev, "setting pre-scaler to %d (hz %d)\n", div, hz);  
  91.     writeb(div, hw->regs + S3C2410_SPPRE);  
  92.   
  93.   
  94.     spin_lock(&hw->bitbang.lock);  
  95.     if (!hw->bitbang.busy) {  
  96.         hw->bitbang.chipselect(spi, BITBANG_CS_INACTIVE);  
  97.         /* need to ndelay for 0.5 clocktick ? */  
  98.     }  
  99.     spin_unlock(&hw->bitbang.lock);  
  100.   
  101.   
  102.     return 0;  
  103. }  
下面來看這個spi_driver是怎樣註冊的,又是與spi_device怎樣match上的。
在spidev.c中:
  1. static int __init spidev_init(void)  
  2. {  
  3.     int status;  
  4.     BUILD_BUG_ON(N_SPI_MINORS > 256);  
  5.     status = register_chrdev(SPIDEV_MAJOR, "spi", &spidev_fops);  
  6.     if (status < 0)  
  7.         return status;  
  8.     spidev_class = class_create(THIS_MODULE, "spidev");  
  9.     if (IS_ERR(spidev_class)) {  
  10.         unregister_chrdev(SPIDEV_MAJOR, spidev_spi.driver.name);  
  11.         return PTR_ERR(spidev_class);  
  12.     }  
  13.     status = spi_register_driver(&spidev_spi);  
  14.     if (status < 0) {  
  15.         class_destroy(spidev_class);  
  16.         unregister_chrdev(SPIDEV_MAJOR, spidev_spi.driver.name);  
  17.     }  
  18.     return status;  
  19. }  
註冊了名爲”spi”的字符驅動,然後註冊了spidev_spi驅動,這個就是圖中sys/Bus/Spi/Drivers/下的spidev。
  1. static struct spi_driver spidev_spi = {  
  2.     .driver = {  
  3.         .name =     "spidev",  
  4.         .owner =    THIS_MODULE,  
  5.     },  
  6.     .probe =    spidev_probe,  
  7.     .remove =   __devexit_p(spidev_remove),  
  8. };  
  1. static struct spi_driver spidev_spi = {  
  2.     .driver = {  
  3.         .name =     "spidev",  
  4.         .owner =    THIS_MODULE,  
  5.     },  
  6.     .probe =    spidev_probe,  
  7.     .remove =   __devexit_p(spidev_remove),  
  8. };  
這裏來看__driver_attach這個函數,其中分別調用了driver_match_device,driver_probe_device函數。如果匹配成果調用probe函數,否則返回。
  1. static int __driver_attach(struct device *dev, void *data)    
  2. {    
  3.     struct device_driver *drv = data;    
  4.     if (!driver_match_device(drv, dev))    
  5.         return 0;    
  6.     
  7.     if (dev->parent) /* Needed for USB */    
  8.         down(&dev->parent->sem);    
  9.     down(&dev->sem);    
  10.     if (!dev->driver)    
  11.         driver_probe_device(drv, dev);    
  12.     up(&dev->sem);    
  13.     if (dev->parent)    
  14.         up(&dev->parent->sem);    
  15.     
  16.     return 0;    
  17. }    
匹配的時候調用的bus的match函數。
  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. static int spi_match_device(struct device *dev, struct device_driver *drv)  
  10. {  
  11.     const struct spi_device *spi = to_spi_device(dev);  
  12.   
  13.   
  14.     return strcmp(spi->modalias, drv->name) == 0;  
  15. }  
可以看到這裏根據驅動和設備的名字進行匹配,匹配成功後調用驅動的probe函數。
  1. static int spi_drv_probe(struct device *dev)  
  2. {  
  3.     const struct spi_driver     *sdrv = to_spi_driver(dev->driver);  
  4.   
  5.   
  6.     return sdrv->probe(to_spi_device(dev));  
  7. }  
可以看大調用了具體的probe函數,這裏實現了把spidev添加到device_list,這樣這個虛擬的字符驅動就註冊並初始化完畢。
  1. static int spidev_remove(struct spi_device *spi)  
  2. {  
  3.     struct spidev_data  *spidev = spi_get_drvdata(spi);  
  4.   
  5.   
  6.     /* make sure ops on existing fds can abort cleanly */  
  7.     spin_lock_irq(&spidev->spi_lock);  
  8.     spidev->spi = NULL;  
  9.     spi_set_drvdata(spi, NULL);  
  10.     spin_unlock_irq(&spidev->spi_lock);  
  11.   
  12.   
  13.     /* prevent new opens */  
  14.     mutex_lock(&device_list_lock);  
  15.     list_del(&spidev->device_entry);  
  16.     device_destroy(spidev_class, spidev->devt);  
  17.     clear_bit(MINOR(spidev->devt), minors);  
  18.     if (spidev->users == 0)  
  19.         kfree(spidev);  
  20.     mutex_unlock(&device_list_lock);  
  21.   
  22.   
  23.     return 0;  
  24. }  
在spidev的註冊函數中註冊了文件操作集合file_operations,爲用戶空間提供了操作SPI controller的接口。
  1. static struct file_operations spidev_fops = {  
  2.     .owner =    THIS_MODULE,  
  3.     /* REVISIT switch to aio primitives, so that userspace 
  4.      * gets more complete API coverage.  It'll simplify things 
  5.      * too, except for the locking. 
  6.      */  
  7.     .write =    spidev_write,  
  8.     .read =     spidev_read,  
  9.     .unlocked_ioctl = spidev_ioctl,  
  10.     .open =     spidev_open,  
  11.     .release =  spidev_release,  
  12. };  
到此爲止spi子系統與spi_master,spi_device,spi_driver這個Linux設備驅動模型已經建立完了。
發佈了18 篇原創文章 · 獲贊 0 · 訪問量 7776
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章