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

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

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

在注册spi_device之前,调用了master的setup方法,该方法又将调用s3c24xx_spi_setupxfer和s3c24xx_spi_chipsel函数。

s3c24xx_spi_setupxfer函数计算预分频系数并写入寄存器。

s3c24xx_spi_chipsel函数用于禁止或使能CS信号。当使能CS信号时,要设置控制寄存器。这里调用是禁止CS信号。

下列代码位于deivers/spi/s3c24xx.c。

  1. #define SPCON_DEFAULT (S3C2410_SPCON_MSTR | S3C2410_SPCON_SMOD_INT)   
  2. #define SPPIN_DEFAULT (S3C2410_SPPIN_KEEP)   
  3.   
  4. static inline struct s3c24xx_spi *to_hw(struct spi_device *sdev)  
  5. {  
  6.     return spi_master_get_devdata(sdev->master);  
  7. }  
  8.   
  9. static void s3c24xx_spi_gpiocs(struct s3c2410_spi_info *spi, int cs, int pol)  
  10. {  
  11.     gpio_set_value(spi->pin_cs, pol);  
  12. }  
  13.   
  14. static void s3c24xx_spi_chipsel(struct spi_device *spi, int value)  
  15. {  
  16.     struct s3c24xx_spi *hw = to_hw(spi);  
  17.     unsigned int cspol = spi->mode & SPI_CS_HIGH ? 1 : 0;  
  18.     unsigned int spcon;  
  19.   
  20.     switch (value) {  
  21.     case BITBANG_CS_INACTIVE:   /*CS无效时*/  
  22.         hw->set_cs(hw->pdata, spi->chip_select, cspol^1);/*即调用s3c24xx_spi_gpiocs使CS无效*/  
  23.         break;  
  24.   
  25.     case BITBANG_CS_ACTIVE:     /*CS有效时*/  
  26.         spcon = readb(hw->regs + S3C2410_SPCON); /*获取目前SPCON寄存器的值*/  
  27.   
  28.         /*开始设置工作模式*/  
  29.         if (spi->mode & SPI_CPHA)      
  30.             spcon |= S3C2410_SPCON_CPHA_FMTB;  
  31.         else  
  32.             spcon &= ~S3C2410_SPCON_CPHA_FMTB;  
  33.   
  34.         if (spi->mode & SPI_CPOL)  
  35.             spcon |= S3C2410_SPCON_CPOL_HIGH;  
  36.         else  
  37.             spcon &= ~S3C2410_SPCON_CPOL_HIGH;  
  38.         /*激活时钟sck输出*/  
  39.         spcon |= S3C2410_SPCON_ENSCK;  
  40.   
  41.         /* write new configration */  
  42.   
  43.         writeb(spcon, hw->regs + S3C2410_SPCON); /*保存新的配置*/  
  44.         hw->set_cs(hw->pdata, spi->chip_select, cspol);/*即调用s3c24xx_spi_gpiocs使CS有效*/  
  45.   
  46.         break;  
  47.     }  
  48. }  
  49.   
  50. static int s3c24xx_spi_setupxfer(struct spi_device *spi,  
  51.                  struct spi_transfer *t)  
  52. {  
  53.     struct s3c24xx_spi *hw = to_hw(spi);  
  54.     unsigned int bpw;  
  55.     unsigned int hz;  
  56.     unsigned int div;  
  57.   
  58.     /*没有transfer,则使用spi_device进行配置*/  
  59.     bpw = t ? t->bits_per_word : spi->bits_per_word;  
  60.     hz  = t ? t->speed_hz : spi->max_speed_hz;  
  61.   
  62.     if (bpw != 8) {  
  63.         dev_err(&spi->dev, "invalid bits-per-word (%d)\n", bpw);  
  64.         return -EINVAL;  
  65.     }  
  66.   
  67.     div = clk_get_rate(hw->clk) / hz;  
  68.   
  69.     /* is clk = pclk / (2 * (pre+1)), or is it 
  70.      *    clk = (pclk * 2) / ( pre + 1) */  
  71.     /*计算预分频系数*/  
  72.     div /= 2;  
  73.   
  74.     if (div > 0)  
  75.         div -= 1;  
  76.   
  77.     if (div > 255)   /*只有8位*/  
  78.         div = 255;  
  79.   
  80.     dev_dbg(&spi->dev, "setting pre-scaler to %d (hz %d)\n", div, hz);  
  81.     writeb(div, hw->regs + S3C2410_SPPRE);   /*设置预分频系数*/  
  82.   
  83.     spin_lock(&hw->bitbang.lock); /*自旋锁加锁*/  
  84.     if (!hw->bitbang.busy) {     /*如果不忙*/  
  85.         hw->bitbang.chipselect(spi, BITBANG_CS_INACTIVE);/*即调用s3c24xx_spi_chipsel使CS无效*/  
  86.         /* need to ndelay for 0.5 clocktick ? */  
  87.     }  
  88.     spin_unlock(&hw->bitbang.lock);  
  89.   
  90.     return 0;  
  91. }  
  92.   
  93. /* the spi->mode bits understood by this driver: */  
  94. #define MODEBITS (SPI_CPOL | SPI_CPHA | SPI_CS_HIGH)   
  95.   
  96. static int s3c24xx_spi_setup(struct spi_device *spi) /*maser.setup方法*/  
  97. {  
  98.     int ret;  
  99.   
  100.     if (!spi->bits_per_word)  
  101.         spi->bits_per_word = 8;  /*没有设置则使用8位*/  
  102.   
  103.     if (spi->mode & ~MODEBITS) { /*检查mode是否有错*/  
  104.         dev_dbg(&spi->dev, "setup: unsupported mode bits %x\n",  
  105.             spi->mode & ~MODEBITS);  
  106.         return -EINVAL;  
  107.     }  
  108.   
  109.     ret = s3c24xx_spi_setupxfer(spi, NULL);  
  110.     if (ret < 0) {  
  111.         dev_err(&spi->dev, "setupxfer returned %d\n", ret);  
  112.         return ret;  
  113.     }  
  114.   
  115.     dev_dbg(&spi->dev, "%s: mode %d, %u bpw, %d hz\n",  
  116.         __func__, spi->mode, spi->bits_per_word,  
  117.         spi->max_speed_hz);  
  118.   
  119.     return 0;  
  120. }  
linux
相关资讯       嵌入式Linux  S3C2440 
本文评论   查看全部评论 (0)
表情: 表情 姓名: 字数

       

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