在注册spi_device之前,调用了master的setup方法,该方法又将调用s3c24xx_spi_setupxfer和s3c24xx_spi_chipsel函数。
s3c24xx_spi_setupxfer函数计算预分频系数并写入寄存器。
s3c24xx_spi_chipsel函数用于禁止或使能CS信号。当使能CS信号时,要设置控制寄存器。这里调用是禁止CS信号。
下列代码位于deivers/spi/s3c24xx.c。
- #define SPCON_DEFAULT (S3C2410_SPCON_MSTR | S3C2410_SPCON_SMOD_INT)
- #define SPPIN_DEFAULT (S3C2410_SPPIN_KEEP)
- static inline struct s3c24xx_spi *to_hw(struct spi_device *sdev)
- {
- return spi_master_get_devdata(sdev->master);
- }
- static void s3c24xx_spi_gpiocs(struct s3c2410_spi_info *spi, int cs, int pol)
- {
- gpio_set_value(spi->pin_cs, pol);
- }
- static void s3c24xx_spi_chipsel(struct spi_device *spi, int value)
- {
- struct s3c24xx_spi *hw = to_hw(spi);
- unsigned int cspol = spi->mode & SPI_CS_HIGH ? 1 : 0;
- unsigned int spcon;
- switch (value) {
- case BITBANG_CS_INACTIVE: /*CS无效时*/
- hw->set_cs(hw->pdata, spi->chip_select, cspol^1);/*即调用s3c24xx_spi_gpiocs使CS无效*/
- break;
- case BITBANG_CS_ACTIVE: /*CS有效时*/
- spcon = readb(hw->regs + S3C2410_SPCON); /*获取目前SPCON寄存器的值*/
- /*开始设置工作模式*/
- if (spi->mode & SPI_CPHA)
- spcon |= S3C2410_SPCON_CPHA_FMTB;
- else
- spcon &= ~S3C2410_SPCON_CPHA_FMTB;
- if (spi->mode & SPI_CPOL)
- spcon |= S3C2410_SPCON_CPOL_HIGH;
- else
- spcon &= ~S3C2410_SPCON_CPOL_HIGH;
- /*激活时钟sck输出*/
- spcon |= S3C2410_SPCON_ENSCK;
- /* write new configration */
- writeb(spcon, hw->regs + S3C2410_SPCON); /*保存新的配置*/
- hw->set_cs(hw->pdata, spi->chip_select, cspol);/*即调用s3c24xx_spi_gpiocs使CS有效*/
- break;
- }
- }
- static int s3c24xx_spi_setupxfer(struct spi_device *spi,
- struct spi_transfer *t)
- {
- struct s3c24xx_spi *hw = to_hw(spi);
- unsigned int bpw;
- unsigned int hz;
- unsigned int div;
- /*没有transfer,则使用spi_device进行配置*/
- bpw = t ? t->bits_per_word : spi->bits_per_word;
- hz = t ? t->speed_hz : spi->max_speed_hz;
- if (bpw != 8) {
- dev_err(&spi->dev, "invalid bits-per-word (%d)\n", bpw);
- return -EINVAL;
- }
- div = clk_get_rate(hw->clk) / hz;
- /* is clk = pclk / (2 * (pre+1)), or is it
- * clk = (pclk * 2) / ( pre + 1) */
- /*计算预分频系数*/
- div /= 2;
- if (div > 0)
- div -= 1;
- if (div > 255) /*只有8位*/
- div = 255;
- dev_dbg(&spi->dev, "setting pre-scaler to %d (hz %d)\n", div, hz);
- writeb(div, hw->regs + S3C2410_SPPRE); /*设置预分频系数*/
- spin_lock(&hw->bitbang.lock); /*自旋锁加锁*/
- if (!hw->bitbang.busy) { /*如果不忙*/
- hw->bitbang.chipselect(spi, BITBANG_CS_INACTIVE);/*即调用s3c24xx_spi_chipsel使CS无效*/
- /* need to ndelay for 0.5 clocktick ? */
- }
- spin_unlock(&hw->bitbang.lock);
- return 0;
- }
- /* the spi->mode bits understood by this driver: */
- #define MODEBITS (SPI_CPOL | SPI_CPHA | SPI_CS_HIGH)
- static int s3c24xx_spi_setup(struct spi_device *spi) /*maser.setup方法*/
- {
- int ret;
- if (!spi->bits_per_word)
- spi->bits_per_word = 8; /*没有设置则使用8位*/
- if (spi->mode & ~MODEBITS) { /*检查mode是否有错*/
- dev_dbg(&spi->dev, "setup: unsupported mode bits %x\n",
- spi->mode & ~MODEBITS);
- return -EINVAL;
- }
- ret = s3c24xx_spi_setupxfer(spi, NULL);
- if (ret < 0) {
- dev_err(&spi->dev, "setupxfer returned %d\n", ret);
- return ret;
- }
- dev_dbg(&spi->dev, "%s: mode %d, %u bpw, %d hz\n",
- __func__, spi->mode, spi->bits_per_word,
- spi->max_speed_hz);
- return 0;
- }
