1. 程式人生 > >SPI驅動之主控制器驅動程式

SPI驅動之主控制器驅動程式



嵌入式微處理器訪問SPI裝置有兩種方式:使用GPIO模擬SPI介面的工作時序或者使用SPI控制器。使用GPIO模擬SPI介面的工作時序是非常容易實現的,但是會導致大量的時間耗費在模擬SPI介面的時序上,訪問效率比較低,容易成為系統瓶頸。這裡主要分析使用SPI控制器的情況。

在核心的drivers/spi/目錄下有兩個spi主控制器驅動程式:spi_s3c24xx.c和spi_s3c24xx_gpio.c其中spi_s3c24xx.c是基於s3c24xx下相應的spi介面的驅動程式,spi_s3c24xx_gpio.c執行使用者指定3個gpio口分別充當spi_clk、spi_mosi和spi_miso介面,模擬標準的spi匯流排。UT4412BV01開發板預留了兩路的spi介面(spi0和spi1),對於UT4412BV01開發板而言,使用的是spi_s3c64xx.c,也就是硬體SPI,不是軟體SPI。注:下面是基於硬體SPI的spi1分析。

1. 定義platform device

kernel3.0.15/arch/arm/mach-exynos/dev-spi.c

[cpp] view plain copy print?
  1. staticstruct resource exynos_spi1_resource[] = {  
  2.     [0] = {  
  3.         .start = EXYNOS_PA_SPI1,  
  4.         .end   = EXYNOS_PA_SPI1 + 0x100 - 1,  
  5.         .flags = IORESOURCE_MEM,  
  6.     },  
  7.     [1] = {  
  8.         .start = DMACH_SPI1_TX,  
  9.         .end   = DMACH_SPI1_TX,  
  10.         .flags = IORESOURCE_DMA,  
  11.     },  
  12.     [2] = {  
  13.         .start = DMACH_SPI1_RX,  
  14.         .end   = DMACH_SPI1_RX,  
  15.         .flags = IORESOURCE_DMA,  
  16.     },  
  17.     [3] = {  
  18.         .start = IRQ_SPI1,  
  19.         .end   = IRQ_SPI1,  
  20.         .flags = IORESOURCE_IRQ,  
  21.     },  
  22. };  
  23. staticstruct s3c64xx_spi_info exynos_spi1_pdata = {  
  24.     .cfg_gpio = exynos_spi_cfg_gpio,  
  25.     .fifo_lvl_mask = 0x7f,  
  26.     .rx_lvl_offset = 15,  
  27.     .high_speed = 1,  
  28.     .clk_from_cmu = true,  
  29.     .tx_st_done = 25,  
  30. };  
  31. struct platform_device exynos_device_spi1 = {  
  32.     .name         = "s3c64xx-spi",  
  33.     .id       = 1,  
  34.     .num_resources    = ARRAY_SIZE(exynos_spi1_resource),  
  35.     .resource     = exynos_spi1_resource,  
  36.     .dev = {  
  37.         .dma_mask       = &spi_dmamask,  
  38.         .coherent_dma_mask  = DMA_BIT_MASK(32),  
  39.         .platform_data = &exynos_spi1_pdata,  
  40.     },  
  41. };  
static struct resource exynos_spi1_resource[] = {
	[0] = {
		.start = EXYNOS_PA_SPI1,
		.end   = EXYNOS_PA_SPI1 + 0x100 - 1,
		.flags = IORESOURCE_MEM,
	},
	[1] = {
		.start = DMACH_SPI1_TX,
		.end   = DMACH_SPI1_TX,
		.flags = IORESOURCE_DMA,
	},
	[2] = {
		.start = DMACH_SPI1_RX,
		.end   = DMACH_SPI1_RX,
		.flags = IORESOURCE_DMA,
	},
	[3] = {
		.start = IRQ_SPI1,
		.end   = IRQ_SPI1,
		.flags = IORESOURCE_IRQ,
	},
};

static struct s3c64xx_spi_info exynos_spi1_pdata = {
	.cfg_gpio = exynos_spi_cfg_gpio,
	.fifo_lvl_mask = 0x7f,
	.rx_lvl_offset = 15,
	.high_speed = 1,
	.clk_from_cmu = true,
	.tx_st_done = 25,
};

struct platform_device exynos_device_spi1 = {
	.name		  = "s3c64xx-spi",
	.id		  = 1,
	.num_resources	  = ARRAY_SIZE(exynos_spi1_resource),
	.resource	  = exynos_spi1_resource,
	.dev = {
		.dma_mask		= &spi_dmamask,
		.coherent_dma_mask	= DMA_BIT_MASK(32),
		.platform_data = &exynos_spi1_pdata,
	},
};
exynos4412總共定義了三個spi控制器平臺裝置,實際上UT4412BV01開發板只預留了兩個spi控制器(spi0和spi1)。platform裝置給出了spi1介面的暫存器地址資源及IRQ資源。注意其裝置名為s3c64xx-spi

2. 定義platform driver

kernel3.0.15/drivers/spi/spi_s3c64xx.c

[cpp] view plain copy print?
  1. staticstruct platform_driver s3c64xx_spi_driver = {  
  2.     .driver = {  
  3.         .name   = "s3c64xx-spi",  
  4.         .owner = THIS_MODULE,  
  5.     },  
  6.     .remove = s3c64xx_spi_remove,  
  7.     .suspend = s3c64xx_spi_suspend,  
  8.     .resume = s3c64xx_spi_resume,  
  9. };  
  10. MODULE_ALIAS("platform:s3c64xx-spi");  
  11. staticint __init s3c64xx_spi_init(void)  
  12. {  
  13. <span style="white-space: pre;">    </span>//裝置不可熱插拔,所以使用該函式,而不是platform_driver_register
  14.     return platform_driver_probe(&s3c64xx_spi_driver, s3c64xx_spi_probe);  
  15. }  
  16. subsys_initcall(s3c64xx_spi_init);  
  17. staticvoid __exit s3c64xx_spi_exit(void)  
  18. {  
  19.     platform_driver_unregister(&s3c64xx_spi_driver);  
  20. }  
  21. module_exit(s3c64xx_spi_exit);  
  22. MODULE_AUTHOR("Jaswinder Singh <[email protected]>");  
  23. MODULE_DESCRIPTION("S3C64XX SPI Controller Driver");  
  24. MODULE_LICENSE("GPL");  
static struct platform_driver s3c64xx_spi_driver = {
	.driver = {
		.name	= "s3c64xx-spi",
		.owner = THIS_MODULE,
	},
	.remove = s3c64xx_spi_remove,
	.suspend = s3c64xx_spi_suspend,
	.resume = s3c64xx_spi_resume,
};
MODULE_ALIAS("platform:s3c64xx-spi");

static int __init s3c64xx_spi_init(void)
{
//裝置不可熱插拔,所以使用該函式,而不是platform_driver_register
	return platform_driver_probe(&s3c64xx_spi_driver, s3c64xx_spi_probe);
}
subsys_initcall(s3c64xx_spi_init);

static void __exit s3c64xx_spi_exit(void)
{
	platform_driver_unregister(&s3c64xx_spi_driver);
}
module_exit(s3c64xx_spi_exit);

MODULE_AUTHOR("Jaswinder Singh <[email protected]>");
MODULE_DESCRIPTION("S3C64XX SPI Controller Driver");
MODULE_LICENSE("GPL");
呼叫了platform_driver_probe註冊platform驅動,註冊完成以後將會呼叫platform的s3c64xx_spi_probe函式。注意:platform驅動的name和platform device的name是相同的

3. s3c64xx_spi_probe函式

kernel3.0.15/drivers/spi/spi_s3c64xx.c

當exynos_device_spi1中的name與s3c64xx_spi_driver中的name相同時,也就是是裝置名字跟驅動名字可以匹配,s3c64xx_spi_probe驅動探測函式被呼叫,該函式程式碼如下所示:

[cpp] view plain copy print?
  1. staticint __init s3c64xx_spi_probe(struct platform_device *pdev)  
  2. {  
  3.     struct resource *mem_res, *dmatx_res, *dmarx_res;  
  4.     struct s3c64xx_spi_driver_data *sdd;  
  5.     struct s3c64xx_spi_info *sci;  
  6.     struct spi_master *master;  
  7.     int ret;  
  8.     if (pdev->id < 0) { //pdev->id = 1
  9.         dev_err(&pdev->dev,  
  10.                 "Invalid platform device id-%d\n", pdev->id);  
  11.         return -ENODEV;  
  12.     }  
  13.     if (pdev->dev.platform_data == NULL) { //pdev->dev.platform_data = &exynos_spi1_pdata
  14.         dev_err(&pdev->dev, "platform_data missing!\n");  
  15.         return -ENODEV;  
  16.     }  
  17.     sci = pdev->dev.platform_data;  
  18.     if (!sci->src_clk_name) { //在板級檔案中通過呼叫s3c64xx_spi_set_info()來初始化
  19.         dev_err(&pdev->dev,  
  20.             "Board init must call s3c64xx_spi_set_info()\n");  
  21.         return -EINVAL;  
  22.     }  
  23.     /* Check for availability of necessary resource */
  24.     //獲取DMA0資源
  25.     dmatx_res = platform_get_resource(pdev, IORESOURCE_DMA, 0);  
  26.     if (dmatx_res == NULL) {  
  27.         dev_err(&pdev->dev, "Unable to get SPI-Tx dma resource\n");  
  28.         return -ENXIO;  
  29.     }  
  30.     //獲取DMA1資源
  31.     dmarx_res = platform_get_resource(pdev, IORESOURCE_DMA, 1);  
  32.     if (dmarx_res == NULL) {  
  33.         dev_err(&pdev->dev, "Unable to get SPI-Rx dma resource\n");  
  34.         return -ENXIO;  
  35.     }  
  36.     //獲取IO記憶體資源
  37.     mem_res = platform_get_resource(pdev, IORESOURCE_MEM, 0);  
  38.     if (mem_res == NULL) {  
  39.         dev_err(&pdev->dev, "Unable to get SPI MEM resource\n");  
  40.         return -ENXIO;  
  41.     }  
  42.     /**  
  43.      * 通過跟蹤spi_alloc_master相關原始碼可知, 
  44.      * 此處分配struct spi_master + struct s3c64xx_spi_driver_data大小的資料, 
  45.      * 把s3c64xx_spi_driver_data設為spi_master的私有資料  
  46.      */
  47.     master = spi_alloc_master(&pdev->dev,  
  48.                 sizeof(struct s3c64xx_spi_driver_data));  
  49.     if (master == NULL) {  
  50.         dev_err(&pdev->dev, "Unable to allocate SPI Master\n");  
  51.         return -ENOMEM;  
  52.     }  
  53.     /** 
  54.      * platform_set_drvdata 和 platform_get_drvdata 
  55.      * probe函式中定義的區域性變數,如果我想在其他地方使用它怎麼辦呢? 
  56.      * 這就需要把它儲存起來。核心提供了這個方法, 
  57.      * 使用函式platform_set_drvdata()可以將master儲存成平臺匯流排裝置的私有資料。 
  58.      * 以後再要使用它時只需呼叫platform_get_drvdata()就可以了。 
  59.      */
  60.     platform_set_drvdata(pdev, master);  
  61.     //從master中獲得s3c64xx_spi_driver_data,並初始化相關成員
  62.     sdd = spi_master_get_devdata(master);  
  63.     sdd->master = master;  
  64.     sdd->cntrlr_info = sci;  
  65.     sdd->pdev = pdev;  
  66.     sdd->sfr_start = mem_res->start;  
  67.     sdd->tx_dmach = dmatx_res->start;  
  68.     sdd->rx_dmach = dmarx_res->start;  
  69.     sdd->cur_bpw = 8;  
  70.     //master相關成員的初始化
  71.     master->bus_num = pdev->id; //匯流排號
  72.     master->setup = s3c64xx_spi_setup;  
  73.     master->transfer = s3c64xx_spi_transfer;  
  74.     master->num_chipselect = sci->num_cs; //該總線上的裝置數
  75.     master->dma_alignment = 8;  
  76.     /* the spi->mode bits understood by this driver: */
  77.     master->mode_bits = SPI_CPOL | SPI_CPHA | SPI_CS_HIGH; //mode_3
  78.     //申請IO記憶體
  79.     if (request_mem_region(mem_res->start,  
  80.             resource_size(mem_res), pdev->name) == NULL) {  
  81.         dev_err(&pdev->dev, "Req mem region failed\n");  
  82.         ret = -ENXIO;  
  83.         goto err0;  
  84.     }  
  85.     //建立對映
  86.     sdd->regs = ioremap(mem_res->start, resource_size(mem_res));  
  87.     if (sdd->regs == NULL) {  
  88.         dev_err(&pdev->dev, "Unable to remap IO\n");  
  89.         ret = -ENXIO;  
  90.         goto err1;  
  91.     }  
  92.     //SPI的IO管腳配置,將相應的IO管腳設定為SPI功能
  93.     if (sci->cfg_gpio == NULL || sci->cfg_gpio(pdev)) {  
  94.         dev_err(&pdev->dev, "Unable to config gpio\n");  
  95.         ret = -EBUSY;  
  96.         goto err2;  
  97.     }  
  98.     //使能時鐘
  99.     sdd->clk = clk_get(&pdev->dev, "spi");  
  100.     if (IS_ERR(sdd->clk)) {  
  101.         dev_err(&pdev->dev, "Unable to acquire clock 'spi'\n");  
  102.         ret = PTR_ERR(sdd->clk);  
  103.         goto err3;  
  104.     }  
  105.     if (clk_enable(sdd->clk)) {  
  106.         dev_err(&pdev->dev, "Couldn't enable clock 'spi'\n");  
  107.         ret = -EBUSY;  
  108.         goto err4;  
  109.     }  
  110.     sdd->src_clk = clk_get(&pdev->dev, sci->src_clk_name);  
  111.     if (IS_ERR(sdd->src_clk)) {  
  112.         dev_err(&pdev->dev,  
  113.             "Unable to acquire clock '%s'\n", sci->src_clk_name);  
  114.         ret = PTR_ERR(sdd->src_clk);  
  115.         goto err5;  
  116.     }  
  117.     if (clk_enable(sdd->src_clk)) {  
  118.         dev_err(&pdev->dev, "Couldn't enable clock '%s'\n",  
  119.                             sci->src_clk_name);  
  120.         ret = -EBUSY;  
  121.         goto err6;  
  122.     }  
  123.     //建立單個執行緒的工作佇列,用於資料收發操作
  124.     sdd->workqueue = create_singlethread_workqueue(  
  125.                         dev_name(master->dev.parent));  
  126.     if (sdd->workqueue == NULL) {  
  127.         dev_err(&pdev->dev, "Unable to create workqueue\n");  
  128.         ret = -ENOMEM;  
  129.         goto err7;  
  130.     }  
  131.     //硬體初始化,初始化設定暫存器,包括對SPIMOSI、SPIMISO、SPICLK引腳的設定
  132.     s3c64xx_spi_hwinit(sdd, pdev->id);  
  133.     //鎖、工作佇列等初始化
  134.     spin_lock_init(&sdd->lock);  
  135.     init_completion(&sdd->xfer_completion);  
  136.     INIT_WORK(&sdd->work, s3c64xx_spi_work);  
  137.     INIT_LIST_HEAD(&sdd->queue);  
  138.     if (spi_register_master(master)) {  
  139.         dev_err(&pdev->dev, "cannot register SPI master\n");  
  140.         ret = -EBUSY;  
  141.         goto err8;  
  142.     }  
  143.     dev_dbg(&pdev->dev, "Samsung SoC SPI Driver loaded for Bus SPI-%d "
  144.                     "with %d Slaves attached\n",  
  145.                     pdev->id, master->num_chipselect);  
  146.     dev_dbg(&pdev->dev, "\tIOmem=[0x%x-0x%x]\tDMA=[Rx-%d, Tx-%d]\n",  
  147.                     mem_res->end, mem_res->start,  
  148.                     sdd->rx_dmach, sdd->tx_dmach);  
  149.     return 0;  
  150. err8:  
  151.     destroy_workqueue(sdd->workqueue);  
  152. err7:  
  153.     clk_disable(sdd->src_clk);  
  154. err6:  
  155.     clk_put(sdd->src_clk);  
  156. err5:  
  157.     clk_disable(sdd->clk);  
  158. err4:  
  159.     clk_put(sdd->clk);  
  160. err3:  
  161. err2:  
  162.     iounmap((void *) sdd->regs);  
  163. err1:  
  164.     release_mem_region(mem_res->start, resource_size(mem_res));  
  165. err0:  
  166.     platform_set_drvdata(pdev, NULL);  
  167.     spi_master_put(master);  
  168.     return ret;  
  169. }  
static int __init s3c64xx_spi_probe(struct platform_device *pdev)
{
	struct resource	*mem_res, *dmatx_res, *dmarx_res;
	struct s3c64xx_spi_driver_data *sdd;
	struct s3c64xx_spi_info *sci;
	struct spi_master *master;
	int ret;

	if (pdev->id < 0) { //pdev->id = 1
		dev_err(&pdev->dev,
				"Invalid platform device id-%d\n", pdev->id);
		return -ENODEV;
	}

	if (pdev->dev.platform_data == NULL) { //pdev->dev.platform_data = &exynos_spi1_pdata
		dev_err(&pdev->dev, "platform_data missing!\n");
		return -ENODEV;
	}

	sci = pdev->dev.platform_data;
	if (!sci->src_clk_name) { //在板級檔案中通過呼叫s3c64xx_spi_set_info()來初始化
		dev_err(&pdev->dev,
			"Board init must call s3c64xx_spi_set_info()\n");
		return -EINVAL;
	}

	/* Check for availability of necessary resource */

	//獲取DMA0資源
	dmatx_res = platform_get_resource(pdev, IORESOURCE_DMA, 0);
	if (dmatx_res == NULL) {
		dev_err(&pdev->dev, "Unable to get SPI-Tx dma resource\n");
		return -ENXIO;
	}

	//獲取DMA1資源
	dmarx_res = platform_get_resource(pdev, IORESOURCE_DMA, 1);
	if (dmarx_res == NULL) {
		dev_err(&pdev->dev, "Unable to get SPI-Rx dma resource\n");
		return -ENXIO;
	}

	//獲取IO記憶體資源
	mem_res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
	if (mem_res == NULL) {
		dev_err(&pdev->dev, "Unable to get SPI MEM resource\n");
		return -ENXIO;
	}

	/** 
	 * 通過跟蹤spi_alloc_master相關原始碼可知,
	 * 此處分配struct spi_master + struct s3c64xx_spi_driver_data大小的資料,
	 * 把s3c64xx_spi_driver_data設為spi_master的私有資料 
	 */
	master = spi_alloc_master(&pdev->dev,
				sizeof(struct s3c64xx_spi_driver_data));
	if (master == NULL) {
		dev_err(&pdev->dev, "Unable to allocate SPI Master\n");
		return -ENOMEM;
	}

	/**
	 * platform_set_drvdata 和 platform_get_drvdata
	 * probe函式中定義的區域性變數,如果我想在其他地方使用它怎麼辦呢?
	 * 這就需要把它儲存起來。核心提供了這個方法,
	 * 使用函式platform_set_drvdata()可以將master儲存成平臺匯流排裝置的私有資料。
	 * 以後再要使用它時只需呼叫platform_get_drvdata()就可以了。
	 */
	platform_set_drvdata(pdev, master);

	//從master中獲得s3c64xx_spi_driver_data,並初始化相關成員
	sdd = spi_master_get_devdata(master);
	sdd->master = master;
	sdd->cntrlr_info = sci;
	sdd->pdev = pdev;
	sdd->sfr_start = mem_res->start;
	sdd->tx_dmach = dmatx_res->start;
	sdd->rx_dmach = dmarx_res->start;

	sdd->cur_bpw = 8;

	//master相關成員的初始化
	master->bus_num = pdev->id; //匯流排號
	master->setup = s3c64xx_spi_setup;
	master->transfer = s3c64xx_spi_transfer;
	master->num_chipselect = sci->num_cs; //該總線上的裝置數
	master->dma_alignment = 8;
	/* the spi->mode bits understood by this driver: */
	master->mode_bits = SPI_CPOL | SPI_CPHA | SPI_CS_HIGH; //mode_3

	//申請IO記憶體
	if (request_mem_region(mem_res->start,
			resource_size(mem_res), pdev->name) == NULL) {
		dev_err(&pdev->dev, "Req mem region failed\n");
		ret = -ENXIO;
		goto err0;
	}

	//建立對映
	sdd->regs = ioremap(mem_res->start, resource_size(mem_res));
	if (sdd->regs == NULL) {
		dev_err(&pdev->dev, "Unable to remap IO\n");
		ret = -ENXIO;
		goto err1;
	}

	//SPI的IO管腳配置,將相應的IO管腳設定為SPI功能
	if (sci->cfg_gpio == NULL || sci->cfg_gpio(pdev)) {
		dev_err(&pdev->dev, "Unable to config gpio\n");
		ret = -EBUSY;
		goto err2;
	}

	//使能時鐘
	sdd->clk = clk_get(&pdev->dev, "spi");
	if (IS_ERR(sdd->clk)) {
		dev_err(&pdev->dev, "Unable to acquire clock 'spi'\n");
		ret = PTR_ERR(sdd->clk);
		goto err3;
	}

	if (clk_enable(sdd->clk)) {
		dev_err(&pdev->dev, "Couldn't enable clock 'spi'\n");
		ret = -EBUSY;
		goto err4;
	}

	sdd->src_clk = clk_get(&pdev->dev, sci->src_clk_name);
	if (IS_ERR(sdd->src_clk)) {
		dev_err(&pdev->dev,
			"Unable to acquire clock '%s'\n", sci->src_clk_name);
		ret = PTR_ERR(sdd->src_clk);
		goto err5;
	}

	if (clk_enable(sdd->src_clk)) {
		dev_err(&pdev->dev, "Couldn't enable clock '%s'\n",
							sci->src_clk_name);
		ret = -EBUSY;
		goto err6;
	}

	//建立單個執行緒的工作佇列,用於資料收發操作
	sdd->workqueue = create_singlethread_workqueue(
						dev_name(master->dev.parent));
	if (sdd->workqueue == NULL) {
		dev_err(&pdev->dev, "Unable to create workqueue\n");
		ret = -ENOMEM;
		goto err7;
	}

	//硬體初始化,初始化設定暫存器,包括對SPIMOSI、SPIMISO、SPICLK引腳的設定
	s3c64xx_spi_hwinit(sdd, pdev->id);

	//鎖、工作佇列等初始化
	spin_lock_init(&sdd->lock);
	init_completion(&sdd->xfer_completion);
	INIT_WORK(&sdd->work, s3c64xx_spi_work);
	INIT_LIST_HEAD(&sdd->queue);

	if (spi_register_master(master)) {
		dev_err(&pdev->dev, "cannot register SPI master\n");
		ret = -EBUSY;
		goto err8;
	}

	dev_dbg(&pdev->dev, "Samsung SoC SPI Driver loaded for Bus SPI-%d "
					"with %d Slaves attached\n",
					pdev->id, master->num_chipselect);
	dev_dbg(&pdev->dev, "\tIOmem=[0x%x-0x%x]\tDMA=[Rx-%d, Tx-%d]\n",
					mem_res->end, mem_res->start,
					sdd->rx_dmach, sdd->tx_dmach);

	return 0;

err8:
	destroy_workqueue(sdd->workqueue);
err7:
	clk_disable(sdd->src_clk);
err6:
	clk_put(sdd->src_clk);
err5:
	clk_disable(sdd->clk);
err4:
	clk_put(sdd->clk);
err3:
err2:
	iounmap((void *) sdd->regs);
err1:
	release_mem_region(mem_res->start, resource_size(mem_res));
err0:
	platform_set_drvdata(pdev, NULL);
	spi_master_put(master);

	return ret;
}
s3c64xx_spi_probe函式很長,但做的事情卻很簡單,從上面程式碼的註釋可以基本理清整個探測流程。其中用到幾個比較重要的函式,下面來一一解釋。

spi_alloc_master(kernel3.0.15/drivers/spi/spi.c)

spi_alloc_master函式用於請求分配一個spi_master。

[cpp] view plain copy print?
  1. struct spi_master *spi_alloc_master(struct device *dev, unsigned size)  
  2. {  
  3.     struct spi_master   *master;  
  4.     if (!dev)  
  5.         return NULL;  
  6.     /* 分配記憶體,分配的記憶體大小是*master + size,包含了兩部分記憶體 */
  7.     master = kzalloc(size + sizeof *master, GFP_KERNEL);  
  8.     if (!master)  
  9.         return NULL;  
  10.     device_initialize(&master->dev); //裝置模型中的初始裝置函式
  11.     master->dev.class = &spi_master_class; //spi_master_class在SPI子系統初始化的時候就已經註冊好了
  12.     master->dev.parent = get_device(dev); //裝置當前裝置的父裝置,這與裝置模型相關
  13.     spi_master_set_devdata(master, &master[1]); //&master[1]就是master之後的另一部分記憶體的起始地址
  14.     return master;  
  15. }  
struct spi_master *spi_alloc_master(struct device *dev, unsigned size)
{
	struct spi_master	*master;

	if (!dev)
		return NULL;

	/* 分配記憶體,分配的記憶體大小是*master + size,包含了兩部分記憶體 */
	master = kzalloc(size + sizeof *master, GFP_KERNEL);
	if (!master)
		return NULL;

	device_initialize(&master->dev); //裝置模型中的初始裝置函式
	master->dev.class = &spi_master_class; //spi_master_class在SPI子系統初始化的時候就已經註冊好了
	master->dev.parent = get_device(dev); //裝置當前裝置的父裝置,這與裝置模型相關
	spi_master_set_devdata(master, &master[1]); //&master[1]就是master之後的另一部分記憶體的起始地址

	return master;
}

spi_master_register(kernel3.0.15/drivers/spi/spi.c)

spi_master_register函式用於向核心註冊一個spi_master。

[cpp] view plain copy print?
  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.     struct boardinfo    *bi;  
  6.     int         status = -ENODEV;  
  7.     int         dynamic = 0;  
  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.     if (master->num_chipselect == 0) //一個SPI控制器至少有一個片選,因此片選數為0則出錯
  14.         return -EINVAL;  
  15.     /* convention:  dynamically assigned bus IDs count down from the max */
  16.     if (master->bus_num < 0) { //如果匯流排號小於0則動態分配一個匯流排號
  17.         /* FIXME switch to an IDR based scheme, something like 
  18.          * I2C now uses, so we can't run out of "dynamic" IDs 
  19.          */
  20.         master->bus_num = atomic_dec_return(&dyn_bus_id);  
  21.         dynamic = 1;  
  22.     }  
  23.     spin_lock_init(&master->bus_lock_spinlock);  
  24.     mutex_init(&master->bus_lock_mutex);  
  25.     master->bus_lock_flag = 0;  
  26.     /* register the device, then userspace will see it. 
  27.      * registration fails if the bus ID is in use. 
  28.      */
  29.     dev_set_name(&master->dev, "spi%u", master->bus_num); //把master加入到裝置模型中
  30.     status = device_add(&master->dev);  
  31.     if (status < 0)  
  32.         goto done;  
  33.     dev_dbg(dev, "registered master %s%s\n", dev_name(&master->dev),  
  34.             dynamic ? " (dynamic)" : "");  
  35.     mutex_lock(&board_lock);  
  36.     list_add_tail(&master->list, &spi_master_list);  
  37.     list_for_each_entry(bi, &board_list, list) //遍歷board_list這個連結串列
  38.         spi_match_master_to_boardinfo(master, &bi->board_info);  
  39.     mutex_unlock(&board_lock);  
  40.     status = 0;  
  41.     /* Register devices from the device tree */
  42.     of_register_spi_devices(master);  
  43. done:  
  44.     return status;  
  45. }  
int spi_register_master(struct spi_master *master)
{
	static atomic_t		dyn_bus_id = ATOMIC_INIT((1<<15) - 1);
	struct device		*dev = master->dev.parent;
	struct boardinfo	*bi;
	int			status = -ENODEV;
	int			dynamic = 0;

	if (!dev)
		return -ENODEV;

	/* even if it's just one always-selected device, there must
	 * be at least one chipselect
	 */
	if (master->num_chipselect == 0) //一個SPI控制器至少有一個片選,因此片選數為0則出錯
		return -EINVAL;

	/* convention:  dynamically assigned bus IDs count down from the max */
	if (master->bus_num < 0) { //如果匯流排號小於0則動態分配一個匯流排號
		/* FIXME switch to an IDR based scheme, something like
		 * I2C now uses, so we can't run out of "dynamic" IDs
		 */
		master->bus_num = atomic_dec_return(&dyn_bus_id);
		dynamic = 1;
	}

	spin_lock_init(&master->bus_lock_spinlock);
	mutex_init(&master->bus_lock_mutex);
	master->bus_lock_flag = 0;

	/* register the device, then userspace will see it.
	 * registration fails if the bus ID is in use.
	 */
	dev_set_name(&master->dev, "spi%u", master->bus_num); //把master加入到裝置模型中
	status = device_add(&master->dev);
	if (status < 0)
		goto done;
	dev_dbg(dev, "registered master %s%s\n", dev_name(&master->dev),
			dynamic ? " (dynamic)" : "");

	mutex_lock(&board_lock);
	list_add_tail(&master->list, &spi_master_list);
	list_for_each_entry(bi, &board_list, list) //遍歷board_list這個連結串列
		spi_match_master_to_boardinfo(master, &bi->board_info);
	mutex_unlock(&board_lock);

	status = 0;

	/* Register devices from the device tree */
	of_register_spi_devices(master);
done:
	return status;
}

spi_match_master_to_boardinfo(kernel3.0.15/drivers/spi/spi.c)

[cpp] view plain copy print?
  1. staticvoid spi_match_master_to_boardinfo(struct spi_master *master,  
  2.                 struct spi_board_info *bi)  
  3. {  
  4.     struct spi_device *dev;  
  5.     if (master->bus_num != bi->bus_num) //每找到一個成員就將它的匯流排號與master的匯流排號進行比較,如果相等則呼叫spi_new_device函式建立一個spi裝置
  6.         return;  
  7.     dev = spi_new_device(master, bi);  
  8.     if (!dev)  
  9.         dev_err(master->dev.parent, "can't create new device for %s\n",  
  10.             bi->modalias);  
  11. }  
static void spi_match_master_to_boardinfo(struct spi_master *master,
				struct spi_board_info *bi)
{
	struct spi_device *dev;

	if (master->bus_num != bi->bus_num) //每找到一個成員就將它的匯流排號與master的匯流排號進行比較,如果相等則呼叫spi_new_device函式建立一個spi裝置
		return;

	dev = spi_new_device(master, bi);
	if (!dev)
		dev_err(master->dev.parent, "can't create new device for %s\n",
			bi->modalias);
}

spi_new_device(kernel3.0.15/drivers/spi/spi.c)

[cpp] view plain copy print?
  1. 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.     /* NOTE:  caller did any chip->bus_num checks necessary. 
  7.      * 
  8.      * Also, unless we change the return value convention to use 
  9.      * error-or-pointer (not NULL-or-pointer), troubleshootability 
  10.      * suggests syslogged diagnostics are best here (ugh). 
  11.      */
  12.     proxy = spi_alloc_device(master);  
  13.     if (!proxy)  
  14.         return NULL;  
  15.     WARN_ON(strlen(chip->modalias) >= sizeof(proxy->modalias));  
  16.     proxy->chip_select = chip->chip_select;  
  17.     proxy->max_speed_hz = chip->max_speed_hz;  
  18.     proxy->mode = chip->mode;  
  19.     proxy->irq = chip->irq;  
  20.     strlcpy(proxy->modalias, chip->modalias, sizeof(proxy->modalias)); //此處比較關鍵,裝置名字拷貝
  21.     proxy->dev.platform_data = (void *) chip->platform_data;  
  22.     proxy->controller_data = chip->controller_data;  
  23.     proxy->controller_state = NULL;  
  24.     status = spi_add_device(proxy);  
  25.     if (status < 0) {  
  26.         spi_dev_put(proxy);  
  27.         return NULL;  
  28.     }  
  29.     return proxy;  
  30. }  
struct spi_device *spi_new_device(struct spi_master *master,
				  struct spi_board_info *chip)
{
	struct spi_device	*proxy;
	int			status;

	/* NOTE:  caller did any chip->bus_num checks necessary.
	 *
	 * Also, unless we change the return value convention to use
	 * error-or-pointer (not NULL-or-pointer), troubleshootability
	 * suggests syslogged diagnostics are best here (ugh).
	 */

	proxy = spi_alloc_device(master);
	if (!proxy)
		return NULL;

	WARN_ON(strlen(chip->modalias) >= sizeof(proxy->modalias));

	proxy->chip_select = chip->chip_select;
	proxy->max_speed_hz = chip->max_speed_hz;
	proxy->mode = chip->mode;
	proxy->irq = chip->irq;
	strlcpy(proxy->modalias, chip->modalias, sizeof(proxy->modalias)); //此處比較關鍵,裝置名字拷貝
	proxy->dev.platform_data = (void *) chip->platform_data;
	proxy->controller_data = chip->controller_data;
	proxy->controller_state = NULL;

	status = spi_add_device(proxy);
	if (status < 0) {
		spi_dev_put(proxy);
		return NULL;
	}

	return proxy;
}
spi_alloc_device(kernel3.0.15/drivers/spi/spi.c) [cpp] view plain copy print?
  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->dev.bus = &spi_bus_type; //該spi裝置屬於SPI子系統初始化時註冊的叫“spi”的匯流排
  16.     spi->dev.release = spidev_release;  
  17.     device_initialize(&spi->dev); //裝置模型方面的初始化
  18.     return spi;  
  19. }  
struct spi_device *spi_alloc_device(struct spi_master *master)
{
	struct spi_device	*spi;
	struct device		*dev = master->dev.parent;

	if (!spi_master_get(master)) //錯誤檢測
		return NULL;

	spi = kzalloc(sizeof *spi, GFP_KERNEL); //分配記憶體
	if (!spi) {
		dev_err(dev, "cannot alloc spi_device\n");
		spi_master_put(master);
		return NULL;
	}

	spi->master = master;
	spi->dev.parent = dev;
	spi->dev.bus = &spi_bus_type; //該spi裝置屬於SPI子系統初始化時註冊的叫“spi”的匯流排
	spi->dev.release = spidev_release;
	device_initialize(&spi->dev); //裝置模型方面的初始化
	return spi;
}
spi_add_device(kernel3.0.15/drivers/spi/spi.c) [cpp] view plain copy print?
  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.     struct device *d;  
  6.     int status;  
  7.     /* Chipselects are numbered 0..max; validate. */
  8.     if (spi->chip_select >= spi->master->num_chipselect) { //片選號是從0開始的,如果大於或者等於片選數的話則返回出錯
  9.         dev_err(dev, "cs%d >= max %d\n",  
  10.             spi->chip_select,  
  11.             spi->master->num_chipselect);  
  12.         return -EINVAL;  
  13.     }  
  14.     /* Set the bus ID string */
  15.     dev_set_name(&spi->dev, "%s.%u", dev_name(&spi->master->dev),   
  16.             spi->chip_select);  
  17.     /* We need to make sure there's no other device with this 
  18.      * chipselect **BEFORE** we call setup(), else we'll trash 
  19.      * its configuration.  Lock against concurrent add() calls. 
  20.      */
  21.     mutex_lock(&spi_add_lock);  
  22.     d = bus_find_device_by_name(&spi_bus_type, NULL, dev_name(&spi->dev)); //遍歷spi匯流排,看是否已經註冊過該裝置
  23.     if (d != NULL) {  
  24.         dev_err(dev, "chipselect %d already in use\n",  
  25.                 spi->chip_select);  
  26.         put_device(d);  
  27.         status = -EBUSY;  
  28.         goto done;  
  29.     }  
  30.     /* Drivers may modify this initial i/o setup, but will 
  31.      * normally rely on the device being setup.  Devices 
  32.      * using SPI_CS_HIGH can't coexist well otherwise... 
  33.      */
  34.     status = spi_setup(spi);  
  35.     if (status < 0) {  
  36.         dev_err(dev, "can't setup %s, status %d\n",  
  37.                 dev_name(&spi->dev), status);  
  38.         goto done;  
  39.     }  
  40.     /* Device may be bound to an active driver when this returns */
  41.     status = device_add(&spi->dev);  
  42.     if (status < 0)  
  43.         dev_err(dev, "can't add %s, status %d\n",  
  44.                 dev_name(&spi->dev), status);  
  45.     else
  46.         dev_dbg(dev, "registered child %s\n", dev_name(&spi->dev));  
  47. done:  
  48.     mutex_unlock(&spi_add_lock);  
  49.     return status;  
  50. }  
int spi_add_device(struct spi_device *spi)
{
	static DEFINE_MUTEX(spi_add_lock);
	struct device *dev = spi->master->dev.parent;
	struct device *d;
	int status;

	/* Chipselects are numbered 0..max; validate. */
	if (spi->chip_select >= spi->master->num_chipselect) { //片選號是從0開始的,如果大於或者等於片選數的話則返回出錯
		dev_err(dev, "cs%d >= max %d\n",
			spi->chip_select,
			spi->master->num_chipselect);
		return -EINVAL;
	}

	/* Set the bus ID string */
	dev_set_name(&spi->dev, "%s.%u", dev_name(&spi->master->dev), 
			spi->chip_select);


	/* We need to make sure there's no other device with this
	 * chipselect **BEFORE** we call setup(), else we'll trash
	 * its configuration.  Lock against concurrent add() calls.
	 */
	mutex_lock(&spi_add_lock);

	d = bus_find_device_by_name(&spi_bus_type, NULL, dev_name(&spi->dev)); //遍歷spi匯流排,看是否已經註冊過該裝置
	if (d != NULL) {
		dev_err(dev, "chipselect %d already in use\n",