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

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

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

最后,调用了spi_register_master函数,该函数将完成SPI控制器的注册,其中还牵涉到spi_device的注册。因此该函数也非常重要。我们来看看这个函数

下列函数位于drivers/spi/spi_bitbang.c

  1. /** 
  2.  * spi_register_master - register SPI master controller 
  3.  * @master: initialized master, originally from spi_alloc_master() 
  4.  * Context: can sleep 
  5.  * 
  6.  * SPI master controllers connect to their drivers using some non-SPI bus, 
  7.  * such as the platform bus.  The final stage of probe() in that code 
  8.  * includes calling spi_register_master() to hook up to this SPI bus glue. 
  9.  * 
  10.  * SPI controllers use board specific (often SOC specific) bus numbers, 
  11.  * and board-specific addressing for SPI devices combines those numbers 
  12.  * with chip select numbers.  Since SPI does not directly support dynamic 
  13.  * device identification, boards need configuration tables telling which 
  14.  * chip is at which address. 
  15.  * 
  16.  * This must be called from context that can sleep.  It returns zero on 
  17.  * success, else a negative error code (dropping the master's refcount). 
  18.  * After a successful return, the caller is responsible for calling 
  19.  * spi_unregister_master(). 
  20.  */  
  21. int spi_register_master(struct spi_master *master)  
  22. {  
  23.     static atomic_t     dyn_bus_id = ATOMIC_INIT((1<<15) - 1);  
  24.     struct device       *dev = master->dev.parent;  
  25.     int         status = -ENODEV;  
  26.     int         dynamic = 0;  
  27.   
  28.     if (!dev)  
  29.         return -ENODEV;  
  30.   
  31.     /* even if it's just one always-selected device, there must 
  32.      * be at least one chipselect 
  33.      */  
  34.     if (master->num_chipselect == 0)  
  35.         return -EINVAL;  
  36.   
  37.     /* convention:  dynamically assigned bus IDs count down from the max */  
  38.     if (master->bus_num < 0) {  
  39.         /* FIXME switch to an IDR based scheme, something like 
  40.          * I2C now uses, so we can't run out of "dynamic" IDs 
  41.          */  
  42.         master->bus_num = atomic_dec_return(&dyn_bus_id);  
  43.         dynamic = 1;  
  44.     }  
  45.   
  46.     /* register the device, then userspace will see it. 
  47.      * registration fails if the bus ID is in use. 
  48.      */  
  49.     dev_set_name(&master->dev, "spi%u", master->bus_num);  
  50.     status = device_add(&master->dev);   /*注册设备*/  
  51.     if (status < 0)  
  52.         goto done;  
  53.     dev_dbg(dev, "registered master %s%s\n", dev_name(&master->dev),  
  54.             dynamic ? " (dynamic)" : "");  
  55.   
  56.     /* populate children from any spi device tables */  
  57.     scan_boardinfo(master);  
  58.     status = 0;  
  59. done:  
  60.     return status;  
  61. }  
  62. EXPORT_SYMBOL_GPL(spi_register_master);  

该函数中,执行了相关的检查,然后注册了master设备,随后调用了scan_boardinfo。函数如下:

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

  1. /* FIXME someone should add support for a __setup("spi", ...) that 
  2.  * creates board info from kernel command lines 
  3.  */  
  4.   
  5. static void scan_boardinfo(struct spi_master *master)  
  6. {  
  7.     struct boardinfo    *bi;  
  8.   
  9.     mutex_lock(&board_lock);  
  10.     /*以board_list为链表头,遍历所有的boardinfo结构,链表由spi_register_board_info添加*/  
  11.     list_for_each_entry(bi, &board_list, list) {      
  12.         struct spi_board_info    *chip = bi->board_info;  
  13.         unsigned        n;  
  14.         /*遍历该boardinfo指向的spi_board_info数组*/  
  15.         for (n = bi->n_board_info; n > 0; n--, chip++) {  
  16.             if (chip->bus_num != master->bus_num) /*通过bus_num对spi设备和master进行匹配*/  
  17.                 continue;  
  18.             /* NOTE: this relies on spi_new_device to 
  19.              * issue diagnostics when given bogus inputs 
  20.              */  
  21.              /*执行到此,表示匹配完成,SPI设备由该SPI接口来控制,开始创建spi_device*/  
  22.             (void) spi_new_device(master, chip);  
  23.         }  
  24.     }  
  25.     mutex_unlock(&board_lock);  
  26. }  

NOTE:这个函数通过boardinfo遍历的spi_board_info数组,而spi_board_info是在内核初始化过程中由spi_register_board_info进行注册的,在

linux/arch/arm/mach-s3c2440/mach-smdk2440.c中并没有调用过该函数,因此这也是需要移植的地方。

S3C2440共有两个接口:spi0和spi1。chip->bus_num表示该设备使用哪个spi接口,而master->bus_num正好表示了当前的接口。

该函数中,遍历spi_board_info,通过bus_num完成SPI设备和SPI控制器的匹配,匹配成功则开始建立spi_device设备,该过程通过调用spi_new_device实现。我们接着看下这个函数。

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

  1. /** 
  2.  * spi_new_device - instantiate one new SPI device 
  3.  * @master: Controller to which device is connected 
  4.  * @chip: Describes the SPI device 
  5.  * Context: can sleep 
  6.  * 
  7.  * On typical mainboards, this is purely internal; and it's not needed 
  8.  * after board init creates the hard-wired devices.  Some development 
  9.  * platforms may not be able to use spi_register_board_info though, and 
  10.  * this is exported so that for example a USB or parport based adapter 
  11.  * driver could add devices (which it would learn about out-of-band). 
  12.  * 
  13.  * Returns the new device, or NULL. 
  14.  */  
  15. struct spi_device *spi_new_device(struct spi_master *master,  
  16.                   struct spi_board_info *chip)  
  17. {  
  18.     struct spi_device   *proxy;  
  19.     int         status;  
  20.   
  21.     /* NOTE:  caller did any chip->bus_num checks necessary. 
  22.      * 
  23.      * Also, unless we change the return value convention to use 
  24.      * error-or-pointer (not NULL-or-pointer), troubleshootability 
  25.      * suggests syslogged diagnostics are best here (ugh). 
  26.      */  
  27.   
  28.     proxy = spi_alloc_device(master);   /*分配spi_device结构,并初始化一些字段*/  
  29.     if (!proxy)  
  30.         return NULL;  
  31.   
  32.     WARN_ON(strlen(chip->modalias) >= sizeof(proxy->modalias));  
  33.     /*从spi_board_info获取SPI从设备的参数*/  
  34.     proxy->chip_select = chip->chip_select;  
  35.     proxy->max_speed_hz = chip->max_speed_hz;  
  36.     proxy->mode = chip->mode;  
  37.     proxy->irq = chip->irq;  
  38.     strlcpy(proxy->modalias, chip->modalias, sizeof(proxy->modalias));  
  39.     proxy->dev.platform_data = (void *) chip->platform_data;  
  40.     proxy->controller_data = chip->controller_data;  
  41.     proxy->controller_state = NULL;  
  42.   
  43.     status = spi_add_device(proxy);  
  44.     if (status < 0) {  
  45.         spi_dev_put(proxy);  
  46.         return NULL;  
  47.     }  
  48.   
  49.     return proxy;  
  50. }  
  51. EXPORT_SYMBOL_GPL(spi_new_device);  

首先,创建了spi_device结构,让后通过板级信息spi_board_info将SPI从设备的相关信息复制给spi_device结构,从而完成了spi_device结构的定义,最后调用spi_add_device,完成spi_device的注册。

看下spi_add_device函数,该函数位于drivers/spi/spi.c

  1. /** 
  2.  * spi_add_device - Add spi_device allocated with spi_alloc_device 
  3.  * @spi: spi_device to register 
  4.  * 
  5.  * Companion function to spi_alloc_device.  Devices allocated with 
  6.  * spi_alloc_device can be added onto the spi bus with this function. 
  7.  * 
  8.  * Returns 0 on success; negative errno on failure 
  9.  */  
  10. int spi_add_device(struct spi_device *spi)  
  11. {  
  12.     static DEFINE_MUTEX(spi_add_lock);  
  13.     struct device *dev = spi->master->dev.parent;  
  14.     int status;  
  15.   
  16.     /* Chipselects are numbered 0..max; validate. */  
  17.     if (spi->chip_select >= spi->master->num_chipselect) {  
  18.         dev_err(dev, "cs%d >= max %d\n",  
  19.             spi->chip_select,  
  20.             spi->master->num_chipselect);  
  21.         return -EINVAL;  
  22.     }  
  23.   
  24.     /* Set the bus ID string */  
  25.     dev_set_name(&spi->dev, "%s.%u", dev_name(&spi->master->dev),  
  26.             spi->chip_select);  
  27.   
  28.   
  29.     /* We need to make sure there's no other device with this 
  30.      * chipselect **BEFORE** we call setup(), else we'll trash 
  31.      * its configuration.  Lock against concurrent add() calls. 
  32.      */  
  33.     mutex_lock(&spi_add_lock);  
  34.   
  35.     if (bus_find_device_by_name(&spi_bus_type, NULL, dev_name(&spi->dev))  
  36.             != NULL) {  
  37.         dev_err(dev, "chipselect %d already in use\n",  
  38.                 spi->chip_select);  
  39.         status = -EBUSY;  
  40.         goto done;  
  41.     }  
  42.   
  43.     /* Drivers may modify this initial i/o setup, but will 
  44.      * normally rely on the device being setup.  Devices 
  45.      * using SPI_CS_HIGH can't coexist well otherwise... 
  46.      */  
  47.     status = spi->master->setup(spi); /*调用setup方法,即s3c24xx_spi_setup函数*/  
  48.     if (status < 0) {  
  49.         dev_err(dev, "can't %s %s, status %d\n",  
  50.                 "setup", dev_name(&spi->dev), status);  
  51.         goto done;  
  52.     }  
  53.   
  54.     /* Device may be bound to an active driver when this returns */  
  55.     status = device_add(&spi->dev);        /*注册SPI_device*/  
  56.     if (status < 0)  
  57.         dev_err(dev, "can't %s %s, status %d\n",  
  58.                 "add", dev_name(&spi->dev), status);  
  59.     else  
  60.         dev_dbg(dev, "registered child %s\n", dev_name(&spi->dev));  
  61.   
  62. done:  
  63.     mutex_unlock(&spi_add_lock);  
  64.     return status;  
  65. }  
  66. EXPORT_SYMBOL_GPL(spi_add_device);  
linux
相关资讯       嵌入式Linux  S3C2440 
本文评论   查看全部评论 (0)
表情: 表情 姓名: 字数

       

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