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

Linux下的SPI总线驱动

[日期:2012-12-16] 来源:CSDN  作者:weiqing1981127 [字体:大 中 小]

现在看看scan_boardinfo中创建新设备的函数spi_new_device

struct spi_device *spi_new_device(struct spi_master *master,

                              struct spi_board_info *chip)

{

      struct spi_device    *proxy;

      int                  status;

      proxy = spi_alloc_device(master); //分配spi_device

      if (!proxy)

              return NULL;

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

      //初始化spi_device的各个字段

      proxy->chip_select = chip->chip_select;

      proxy->max_speed_hz = chip->max_speed_hz;

      proxy->mode = chip->mode;

      proxy->irq = chip->irq;

      //获得spi_device的名字,移植时在mach-smdk2440.c中的s3c2410_spi0_board中设定的

      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);    //主要完成将spi_device添加到内核

      if (status < 0) {

              spi_dev_put(proxy);

              return NULL;

      }

      return proxy;

}

之前我们说过spi_new_device中的分配spi_device函数会调用spi->dev.bus = &spi_bus_type;设置SPI接口设备的总线类型为spi_bus_type,这样便于与SPI接口设备驱动相联系。

好了,现在我们看看spi_new_device中将spi_device添加到内核的函数spi_add_device

int spi_add_device(struct spi_device *spi)

{

      static DEFINE_MUTEX(spi_add_lock);

      struct device *dev = spi->master->dev.parent;

      int status;

      if (spi->chip_select >= spi->master->num_chipselect) {

              dev_err(dev, "cs%d >= max %d\n",

                    spi->chip_select,

                    spi->master->num_chipselect);

              return -EINVAL;

      }

      //设置是spi_device在Linux设备驱动模型中的name,也就是spi0.0    dev_set_name(&spi->dev, "%s.%u", dev_name(&spi->master->dev),

                    spi->chip_select);

      mutex_lock(&spi_add_lock);

      //如果总线上挂的设备已经有这个名字,则设置状态忙碌,并退出

      if (bus_find_device_by_name(&spi_bus_type, NULL, dev_name(&spi->dev))

                    != NULL) {

              dev_err(dev, "chipselect %d already in use\n",

                            spi->chip_select);

              status = -EBUSY;

              goto done;

      }

      status = spi_setup(spi); // 对spi_device的时钟等进行设置

      if (status < 0) {

              dev_err(dev, "can't %s %s, status %d\n",

                            "setup", dev_name(&spi->dev), status);

              goto done;

      }

      status = device_add(&spi->dev);  //添加到内核

      if (status < 0)

              dev_err(dev, "can't %s %s, status %d\n",

                            "add", dev_name(&spi->dev), status);

      else

              dev_dbg(dev, "registered child %s\n", dev_name(&spi->dev));

done:

      mutex_unlock(&spi_add_lock);

      return status;

}

下面跟踪spi_add_device中对spi_device的时钟等进行设置函数spi_setup

status = spi->master->setup(spi);而我们知道master所指向的setup在spi_s3c24xx.c中定义为 s3c24xx_spi_setup,我们跟踪这个函数会看到ret = s3c24xx_spi_update_state(spi, NULL);

接着看看s3c24xx_spi_update_state

static int s3c24xx_spi_update_state(struct spi_device *spi,

                                struct spi_transfer *t)

{

      struct s3c24xx_spi *hw = to_hw(spi);

      struct s3c24xx_spi_devstate *cs = spi->controller_state;

      unsigned int bpw;

      unsigned int hz;

      unsigned int div;

      unsigned long clk;

//设置了每字长的位数和发送速度

      bpw = t ? t->bits_per_word : spi->bits_per_word;

      hz  = t ? t->speed_hz : spi->max_speed_hz;

      if (!bpw)

              bpw = 8;

      if (!hz)

              hz = spi->max_speed_hz;

      if (bpw != 8) {

              dev_err(&spi->dev, "invalid bits-per-word (%d)\n", bpw);

              return -EINVAL;

      }

      if (spi->mode != cs->mode) {

              u8 spcon = SPCON_DEFAULT;

              if (spi->mode & SPI_CPHA)

                    spcon |= S3C2410_SPCON_CPHA_FMTB;

              if (spi->mode & SPI_CPOL)

                    spcon |= S3C2410_SPCON_CPOL_HIGH;

              cs->mode = spi->mode;

              cs->spcon = spcon;

      }

      if (cs->hz != hz) {

              clk = clk_get_rate(hw->clk);  //设置分频值

              div = DIV_ROUND_UP(clk, hz * 2) - 1;

              if (div > 255)

                    div = 255;

              dev_dbg(&spi->dev, "pre-scaler=%d (wanted %d, got %ld)\n",

                    div, hz, clk / (2 * (div + 1)));

              cs->hz = hz;

              cs->sppre = div;

      }

      return 0;

}

好了,到此为止,我们跟着spi_s3c24xx.c中s3c24xx_spi_probe已经走完了,看到了SPI接口设备注册的整个流程。

linux
相关资讯       SPI驱动  Linux SPI驱动  SPI总线  LinuxI驱动开发 
本文评论   查看全部评论 (0)
表情: 表情 姓名: 字数

       

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