Linux驱动修炼之道-SPI驱动框架源码分析(中)

这篇来分析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>  
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值