13b5af9f1SFabio Estevam // SPDX-License-Identifier: GPL-2.0 23b5af9f1SFabio Estevam // 33b5af9f1SFabio Estevam // Freescale ESAI ALSA SoC Digital Audio Interface (DAI) driver 43b5af9f1SFabio Estevam // 53b5af9f1SFabio Estevam // Copyright (C) 2014 Freescale Semiconductor, Inc. 643d24e76SNicolin Chen 743d24e76SNicolin Chen #include <linux/clk.h> 843d24e76SNicolin Chen #include <linux/dmaengine.h> 943d24e76SNicolin Chen #include <linux/module.h> 1043d24e76SNicolin Chen #include <linux/of_irq.h> 1143d24e76SNicolin Chen #include <linux/of_platform.h> 12b2d337d8SS.j. Wang #include <linux/pm_runtime.h> 1343d24e76SNicolin Chen #include <sound/dmaengine_pcm.h> 1443d24e76SNicolin Chen #include <sound/pcm_params.h> 1543d24e76SNicolin Chen 1643d24e76SNicolin Chen #include "fsl_esai.h" 1743d24e76SNicolin Chen #include "imx-pcm.h" 1843d24e76SNicolin Chen 1943d24e76SNicolin Chen #define FSL_ESAI_FORMATS (SNDRV_PCM_FMTBIT_S8 | \ 2043d24e76SNicolin Chen SNDRV_PCM_FMTBIT_S16_LE | \ 2143d24e76SNicolin Chen SNDRV_PCM_FMTBIT_S20_3LE | \ 2243d24e76SNicolin Chen SNDRV_PCM_FMTBIT_S24_LE) 2343d24e76SNicolin Chen 2443d24e76SNicolin Chen /** 253bae1719SPierre-Louis Bossart * struct fsl_esai_soc_data - soc specific data 266878e752SShengjiu Wang * @reset_at_xrun: flags for enable reset operaton 276878e752SShengjiu Wang */ 286878e752SShengjiu Wang struct fsl_esai_soc_data { 296878e752SShengjiu Wang bool reset_at_xrun; 306878e752SShengjiu Wang }; 316878e752SShengjiu Wang 326878e752SShengjiu Wang /** 333bae1719SPierre-Louis Bossart * struct fsl_esai - ESAI private data 3443d24e76SNicolin Chen * @dma_params_rx: DMA parameters for receive channel 3543d24e76SNicolin Chen * @dma_params_tx: DMA parameters for transmit channel 3643d24e76SNicolin Chen * @pdev: platform device pointer 3743d24e76SNicolin Chen * @regmap: regmap handler 3843d24e76SNicolin Chen * @coreclk: clock source to access register 3943d24e76SNicolin Chen * @extalclk: esai clock source to derive HCK, SCK and FS 4043d24e76SNicolin Chen * @fsysclk: system clock source to derive HCK, SCK and FS 41a2a4d604SShengjiu Wang * @spbaclk: SPBA clock (optional, depending on SoC design) 42a3d1f931STakashi Iwai * @work: work to handle the reset operation 436878e752SShengjiu Wang * @soc: soc specific data 4435dac627SShengjiu Wang * @lock: spin lock between hw_reset() and trigger() 4543d24e76SNicolin Chen * @fifo_depth: depth of tx/rx FIFO 4643d24e76SNicolin Chen * @slot_width: width of each DAI slot 47de0d712aSShengjiu Wang * @slots: number of slots 483bae1719SPierre-Louis Bossart * @tx_mask: slot mask for TX 493bae1719SPierre-Louis Bossart * @rx_mask: slot mask for RX 505be6155bSShengjiu Wang * @channels: channel num for tx or rx 5143d24e76SNicolin Chen * @hck_rate: clock rate of desired HCKx clock 52f975ca46SNicolin Chen * @sck_rate: clock rate of desired SCKx clock 53f975ca46SNicolin Chen * @hck_dir: the direction of HCKx pads 5443d24e76SNicolin Chen * @sck_div: if using PSR/PM dividers for SCKx clock 5543d24e76SNicolin Chen * @slave_mode: if fully using DAI slave mode 5643d24e76SNicolin Chen * @synchronous: if using tx/rx synchronous mode 5743d24e76SNicolin Chen * @name: driver name 5843d24e76SNicolin Chen */ 5943d24e76SNicolin Chen struct fsl_esai { 6043d24e76SNicolin Chen struct snd_dmaengine_dai_dma_data dma_params_rx; 6143d24e76SNicolin Chen struct snd_dmaengine_dai_dma_data dma_params_tx; 6243d24e76SNicolin Chen struct platform_device *pdev; 6343d24e76SNicolin Chen struct regmap *regmap; 6443d24e76SNicolin Chen struct clk *coreclk; 6543d24e76SNicolin Chen struct clk *extalclk; 6643d24e76SNicolin Chen struct clk *fsysclk; 67a2a4d604SShengjiu Wang struct clk *spbaclk; 68a3d1f931STakashi Iwai struct work_struct work; 696878e752SShengjiu Wang const struct fsl_esai_soc_data *soc; 7035dac627SShengjiu Wang spinlock_t lock; /* Protect hw_reset and trigger */ 7143d24e76SNicolin Chen u32 fifo_depth; 7243d24e76SNicolin Chen u32 slot_width; 73de0d712aSShengjiu Wang u32 slots; 740ff4e8c6SS.j. Wang u32 tx_mask; 750ff4e8c6SS.j. Wang u32 rx_mask; 765be6155bSShengjiu Wang u32 channels[2]; 7743d24e76SNicolin Chen u32 hck_rate[2]; 78f975ca46SNicolin Chen u32 sck_rate[2]; 79f975ca46SNicolin Chen bool hck_dir[2]; 8043d24e76SNicolin Chen bool sck_div[2]; 8143d24e76SNicolin Chen bool slave_mode; 8243d24e76SNicolin Chen bool synchronous; 8343d24e76SNicolin Chen char name[32]; 8443d24e76SNicolin Chen }; 8543d24e76SNicolin Chen 866878e752SShengjiu Wang static struct fsl_esai_soc_data fsl_esai_vf610 = { 876878e752SShengjiu Wang .reset_at_xrun = true, 886878e752SShengjiu Wang }; 896878e752SShengjiu Wang 906878e752SShengjiu Wang static struct fsl_esai_soc_data fsl_esai_imx35 = { 916878e752SShengjiu Wang .reset_at_xrun = true, 926878e752SShengjiu Wang }; 936878e752SShengjiu Wang 946878e752SShengjiu Wang static struct fsl_esai_soc_data fsl_esai_imx6ull = { 956878e752SShengjiu Wang .reset_at_xrun = false, 966878e752SShengjiu Wang }; 976878e752SShengjiu Wang 9843d24e76SNicolin Chen static irqreturn_t esai_isr(int irq, void *devid) 9943d24e76SNicolin Chen { 10043d24e76SNicolin Chen struct fsl_esai *esai_priv = (struct fsl_esai *)devid; 10143d24e76SNicolin Chen struct platform_device *pdev = esai_priv->pdev; 10243d24e76SNicolin Chen u32 esr; 1037ccafa2bSShengjiu Wang u32 saisr; 10443d24e76SNicolin Chen 10543d24e76SNicolin Chen regmap_read(esai_priv->regmap, REG_ESAI_ESR, &esr); 1067ccafa2bSShengjiu Wang regmap_read(esai_priv->regmap, REG_ESAI_SAISR, &saisr); 1077ccafa2bSShengjiu Wang 1087ccafa2bSShengjiu Wang if ((saisr & (ESAI_SAISR_TUE | ESAI_SAISR_ROE)) && 1096878e752SShengjiu Wang esai_priv->soc->reset_at_xrun) { 1107ccafa2bSShengjiu Wang dev_dbg(&pdev->dev, "reset module for xrun\n"); 1111fecbb71SShengjiu Wang regmap_update_bits(esai_priv->regmap, REG_ESAI_TCR, 1121fecbb71SShengjiu Wang ESAI_xCR_xEIE_MASK, 0); 1131fecbb71SShengjiu Wang regmap_update_bits(esai_priv->regmap, REG_ESAI_RCR, 1141fecbb71SShengjiu Wang ESAI_xCR_xEIE_MASK, 0); 115a3d1f931STakashi Iwai schedule_work(&esai_priv->work); 1167ccafa2bSShengjiu Wang } 11743d24e76SNicolin Chen 11843d24e76SNicolin Chen if (esr & ESAI_ESR_TINIT_MASK) 1193bcc8656SColin Ian King dev_dbg(&pdev->dev, "isr: Transmission Initialized\n"); 12043d24e76SNicolin Chen 12143d24e76SNicolin Chen if (esr & ESAI_ESR_RFF_MASK) 12243d24e76SNicolin Chen dev_warn(&pdev->dev, "isr: Receiving overrun\n"); 12343d24e76SNicolin Chen 12443d24e76SNicolin Chen if (esr & ESAI_ESR_TFE_MASK) 1253bcc8656SColin Ian King dev_warn(&pdev->dev, "isr: Transmission underrun\n"); 12643d24e76SNicolin Chen 12743d24e76SNicolin Chen if (esr & ESAI_ESR_TLS_MASK) 12843d24e76SNicolin Chen dev_dbg(&pdev->dev, "isr: Just transmitted the last slot\n"); 12943d24e76SNicolin Chen 13043d24e76SNicolin Chen if (esr & ESAI_ESR_TDE_MASK) 1313bcc8656SColin Ian King dev_dbg(&pdev->dev, "isr: Transmission data exception\n"); 13243d24e76SNicolin Chen 13343d24e76SNicolin Chen if (esr & ESAI_ESR_TED_MASK) 13443d24e76SNicolin Chen dev_dbg(&pdev->dev, "isr: Transmitting even slots\n"); 13543d24e76SNicolin Chen 13643d24e76SNicolin Chen if (esr & ESAI_ESR_TD_MASK) 13743d24e76SNicolin Chen dev_dbg(&pdev->dev, "isr: Transmitting data\n"); 13843d24e76SNicolin Chen 13943d24e76SNicolin Chen if (esr & ESAI_ESR_RLS_MASK) 14043d24e76SNicolin Chen dev_dbg(&pdev->dev, "isr: Just received the last slot\n"); 14143d24e76SNicolin Chen 14243d24e76SNicolin Chen if (esr & ESAI_ESR_RDE_MASK) 14343d24e76SNicolin Chen dev_dbg(&pdev->dev, "isr: Receiving data exception\n"); 14443d24e76SNicolin Chen 14543d24e76SNicolin Chen if (esr & ESAI_ESR_RED_MASK) 14643d24e76SNicolin Chen dev_dbg(&pdev->dev, "isr: Receiving even slots\n"); 14743d24e76SNicolin Chen 14843d24e76SNicolin Chen if (esr & ESAI_ESR_RD_MASK) 14943d24e76SNicolin Chen dev_dbg(&pdev->dev, "isr: Receiving data\n"); 15043d24e76SNicolin Chen 15143d24e76SNicolin Chen return IRQ_HANDLED; 15243d24e76SNicolin Chen } 15343d24e76SNicolin Chen 15443d24e76SNicolin Chen /** 1553bae1719SPierre-Louis Bossart * fsl_esai_divisor_cal - This function is used to calculate the 1563bae1719SPierre-Louis Bossart * divisors of psr, pm, fp and it is supposed to be called in 1573bae1719SPierre-Louis Bossart * set_dai_sysclk() and set_bclk(). 15843d24e76SNicolin Chen * 1593bae1719SPierre-Louis Bossart * @dai: pointer to DAI 1603bae1719SPierre-Louis Bossart * @tx: current setting is for playback or capture 16143d24e76SNicolin Chen * @ratio: desired overall ratio for the paticipating dividers 16243d24e76SNicolin Chen * @usefp: for HCK setting, there is no need to set fp divider 16343d24e76SNicolin Chen * @fp: bypass other dividers by setting fp directly if fp != 0 16443d24e76SNicolin Chen */ 16543d24e76SNicolin Chen static int fsl_esai_divisor_cal(struct snd_soc_dai *dai, bool tx, u32 ratio, 16643d24e76SNicolin Chen bool usefp, u32 fp) 16743d24e76SNicolin Chen { 16843d24e76SNicolin Chen struct fsl_esai *esai_priv = snd_soc_dai_get_drvdata(dai); 16943d24e76SNicolin Chen u32 psr, pm = 999, maxfp, prod, sub, savesub, i, j; 17043d24e76SNicolin Chen 17143d24e76SNicolin Chen maxfp = usefp ? 16 : 1; 17243d24e76SNicolin Chen 17343d24e76SNicolin Chen if (usefp && fp) 17443d24e76SNicolin Chen goto out_fp; 17543d24e76SNicolin Chen 17643d24e76SNicolin Chen if (ratio > 2 * 8 * 256 * maxfp || ratio < 2) { 17743d24e76SNicolin Chen dev_err(dai->dev, "the ratio is out of range (2 ~ %d)\n", 17843d24e76SNicolin Chen 2 * 8 * 256 * maxfp); 17943d24e76SNicolin Chen return -EINVAL; 18043d24e76SNicolin Chen } else if (ratio % 2) { 18143d24e76SNicolin Chen dev_err(dai->dev, "the raio must be even if using upper divider\n"); 18243d24e76SNicolin Chen return -EINVAL; 18343d24e76SNicolin Chen } 18443d24e76SNicolin Chen 18543d24e76SNicolin Chen ratio /= 2; 18643d24e76SNicolin Chen 18743d24e76SNicolin Chen psr = ratio <= 256 * maxfp ? ESAI_xCCR_xPSR_BYPASS : ESAI_xCCR_xPSR_DIV8; 18843d24e76SNicolin Chen 189c656941dSNicolin Chen /* Do not loop-search if PM (1 ~ 256) alone can serve the ratio */ 190c656941dSNicolin Chen if (ratio <= 256) { 191c656941dSNicolin Chen pm = ratio; 192c656941dSNicolin Chen fp = 1; 193c656941dSNicolin Chen goto out; 194c656941dSNicolin Chen } 195c656941dSNicolin Chen 19643d24e76SNicolin Chen /* Set the max fluctuation -- 0.1% of the max devisor */ 19743d24e76SNicolin Chen savesub = (psr ? 1 : 8) * 256 * maxfp / 1000; 19843d24e76SNicolin Chen 19943d24e76SNicolin Chen /* Find the best value for PM */ 20043d24e76SNicolin Chen for (i = 1; i <= 256; i++) { 20143d24e76SNicolin Chen for (j = 1; j <= maxfp; j++) { 20243d24e76SNicolin Chen /* PSR (1 or 8) * PM (1 ~ 256) * FP (1 ~ 16) */ 20343d24e76SNicolin Chen prod = (psr ? 1 : 8) * i * j; 20443d24e76SNicolin Chen 20543d24e76SNicolin Chen if (prod == ratio) 20643d24e76SNicolin Chen sub = 0; 20743d24e76SNicolin Chen else if (prod / ratio == 1) 20843d24e76SNicolin Chen sub = prod - ratio; 20943d24e76SNicolin Chen else if (ratio / prod == 1) 21043d24e76SNicolin Chen sub = ratio - prod; 21143d24e76SNicolin Chen else 21243d24e76SNicolin Chen continue; 21343d24e76SNicolin Chen 21443d24e76SNicolin Chen /* Calculate the fraction */ 21543d24e76SNicolin Chen sub = sub * 1000 / ratio; 21643d24e76SNicolin Chen if (sub < savesub) { 21743d24e76SNicolin Chen savesub = sub; 21843d24e76SNicolin Chen pm = i; 21943d24e76SNicolin Chen fp = j; 22043d24e76SNicolin Chen } 22143d24e76SNicolin Chen 22243d24e76SNicolin Chen /* We are lucky */ 22343d24e76SNicolin Chen if (savesub == 0) 22443d24e76SNicolin Chen goto out; 22543d24e76SNicolin Chen } 22643d24e76SNicolin Chen } 22743d24e76SNicolin Chen 22843d24e76SNicolin Chen if (pm == 999) { 22943d24e76SNicolin Chen dev_err(dai->dev, "failed to calculate proper divisors\n"); 23043d24e76SNicolin Chen return -EINVAL; 23143d24e76SNicolin Chen } 23243d24e76SNicolin Chen 23343d24e76SNicolin Chen out: 23443d24e76SNicolin Chen regmap_update_bits(esai_priv->regmap, REG_ESAI_xCCR(tx), 23543d24e76SNicolin Chen ESAI_xCCR_xPSR_MASK | ESAI_xCCR_xPM_MASK, 23643d24e76SNicolin Chen psr | ESAI_xCCR_xPM(pm)); 23743d24e76SNicolin Chen 23843d24e76SNicolin Chen out_fp: 23943d24e76SNicolin Chen /* Bypass fp if not being required */ 24043d24e76SNicolin Chen if (maxfp <= 1) 24143d24e76SNicolin Chen return 0; 24243d24e76SNicolin Chen 24343d24e76SNicolin Chen regmap_update_bits(esai_priv->regmap, REG_ESAI_xCCR(tx), 24443d24e76SNicolin Chen ESAI_xCCR_xFP_MASK, ESAI_xCCR_xFP(fp)); 24543d24e76SNicolin Chen 24643d24e76SNicolin Chen return 0; 24743d24e76SNicolin Chen } 24843d24e76SNicolin Chen 24943d24e76SNicolin Chen /** 2503bae1719SPierre-Louis Bossart * fsl_esai_set_dai_sysclk - configure the clock frequency of MCLK (HCKT/HCKR) 2513bae1719SPierre-Louis Bossart * @dai: pointer to DAI 2523bae1719SPierre-Louis Bossart * @clk_id: The clock source of HCKT/HCKR 25343d24e76SNicolin Chen * (Input from outside; output from inside, FSYS or EXTAL) 2543bae1719SPierre-Louis Bossart * @freq: The required clock rate of HCKT/HCKR 2553bae1719SPierre-Louis Bossart * @dir: The clock direction of HCKT/HCKR 25643d24e76SNicolin Chen * 25743d24e76SNicolin Chen * Note: If the direction is input, we do not care about clk_id. 25843d24e76SNicolin Chen */ 25943d24e76SNicolin Chen static int fsl_esai_set_dai_sysclk(struct snd_soc_dai *dai, int clk_id, 26043d24e76SNicolin Chen unsigned int freq, int dir) 26143d24e76SNicolin Chen { 26243d24e76SNicolin Chen struct fsl_esai *esai_priv = snd_soc_dai_get_drvdata(dai); 26343d24e76SNicolin Chen struct clk *clksrc = esai_priv->extalclk; 2641997ee89SS.j. Wang bool tx = (clk_id <= ESAI_HCKT_EXTAL || esai_priv->synchronous); 26543d24e76SNicolin Chen bool in = dir == SND_SOC_CLOCK_IN; 2663e185238SXiubo Li u32 ratio, ecr = 0; 26743d24e76SNicolin Chen unsigned long clk_rate; 2683e185238SXiubo Li int ret; 26943d24e76SNicolin Chen 2708a2278b7SNicolin Chen if (freq == 0) { 2718a2278b7SNicolin Chen dev_err(dai->dev, "%sput freq of HCK%c should not be 0Hz\n", 2728a2278b7SNicolin Chen in ? "in" : "out", tx ? 'T' : 'R'); 2738a2278b7SNicolin Chen return -EINVAL; 2748a2278b7SNicolin Chen } 2758a2278b7SNicolin Chen 276f975ca46SNicolin Chen /* Bypass divider settings if the requirement doesn't change */ 277f975ca46SNicolin Chen if (freq == esai_priv->hck_rate[tx] && dir == esai_priv->hck_dir[tx]) 278f975ca46SNicolin Chen return 0; 27943d24e76SNicolin Chen 28043d24e76SNicolin Chen /* sck_div can be only bypassed if ETO/ERO=0 and SNC_SOC_CLOCK_OUT */ 28143d24e76SNicolin Chen esai_priv->sck_div[tx] = true; 28243d24e76SNicolin Chen 28343d24e76SNicolin Chen /* Set the direction of HCKT/HCKR pins */ 28443d24e76SNicolin Chen regmap_update_bits(esai_priv->regmap, REG_ESAI_xCCR(tx), 28543d24e76SNicolin Chen ESAI_xCCR_xHCKD, in ? 0 : ESAI_xCCR_xHCKD); 28643d24e76SNicolin Chen 28743d24e76SNicolin Chen if (in) 28843d24e76SNicolin Chen goto out; 28943d24e76SNicolin Chen 29043d24e76SNicolin Chen switch (clk_id) { 29143d24e76SNicolin Chen case ESAI_HCKT_FSYS: 29243d24e76SNicolin Chen case ESAI_HCKR_FSYS: 29343d24e76SNicolin Chen clksrc = esai_priv->fsysclk; 29443d24e76SNicolin Chen break; 29543d24e76SNicolin Chen case ESAI_HCKT_EXTAL: 29643d24e76SNicolin Chen ecr |= ESAI_ECR_ETI; 297903c220bSS.j. Wang break; 29843d24e76SNicolin Chen case ESAI_HCKR_EXTAL: 2991997ee89SS.j. Wang ecr |= esai_priv->synchronous ? ESAI_ECR_ETI : ESAI_ECR_ERI; 30043d24e76SNicolin Chen break; 30143d24e76SNicolin Chen default: 30243d24e76SNicolin Chen return -EINVAL; 30343d24e76SNicolin Chen } 30443d24e76SNicolin Chen 30543d24e76SNicolin Chen if (IS_ERR(clksrc)) { 30643d24e76SNicolin Chen dev_err(dai->dev, "no assigned %s clock\n", 3078f6fef01SPierre-Louis Bossart (clk_id % 2) ? "extal" : "fsys"); 30843d24e76SNicolin Chen return PTR_ERR(clksrc); 30943d24e76SNicolin Chen } 31043d24e76SNicolin Chen clk_rate = clk_get_rate(clksrc); 31143d24e76SNicolin Chen 31243d24e76SNicolin Chen ratio = clk_rate / freq; 31343d24e76SNicolin Chen if (ratio * freq > clk_rate) 31443d24e76SNicolin Chen ret = ratio * freq - clk_rate; 31543d24e76SNicolin Chen else if (ratio * freq < clk_rate) 31643d24e76SNicolin Chen ret = clk_rate - ratio * freq; 31743d24e76SNicolin Chen else 31843d24e76SNicolin Chen ret = 0; 31943d24e76SNicolin Chen 32043d24e76SNicolin Chen /* Block if clock source can not be divided into the required rate */ 32143d24e76SNicolin Chen if (ret != 0 && clk_rate / ret < 1000) { 32243d24e76SNicolin Chen dev_err(dai->dev, "failed to derive required HCK%c rate\n", 32343d24e76SNicolin Chen tx ? 'T' : 'R'); 32443d24e76SNicolin Chen return -EINVAL; 32543d24e76SNicolin Chen } 32643d24e76SNicolin Chen 32757ebbcafSNicolin Chen /* Only EXTAL source can be output directly without using PSR and PM */ 32857ebbcafSNicolin Chen if (ratio == 1 && clksrc == esai_priv->extalclk) { 32943d24e76SNicolin Chen /* Bypass all the dividers if not being needed */ 33043d24e76SNicolin Chen ecr |= tx ? ESAI_ECR_ETO : ESAI_ECR_ERO; 33143d24e76SNicolin Chen goto out; 33257ebbcafSNicolin Chen } else if (ratio < 2) { 33357ebbcafSNicolin Chen /* The ratio should be no less than 2 if using other sources */ 33457ebbcafSNicolin Chen dev_err(dai->dev, "failed to derive required HCK%c rate\n", 33557ebbcafSNicolin Chen tx ? 'T' : 'R'); 33657ebbcafSNicolin Chen return -EINVAL; 33743d24e76SNicolin Chen } 33843d24e76SNicolin Chen 33943d24e76SNicolin Chen ret = fsl_esai_divisor_cal(dai, tx, ratio, false, 0); 34043d24e76SNicolin Chen if (ret) 34143d24e76SNicolin Chen return ret; 34243d24e76SNicolin Chen 34343d24e76SNicolin Chen esai_priv->sck_div[tx] = false; 34443d24e76SNicolin Chen 34543d24e76SNicolin Chen out: 346f975ca46SNicolin Chen esai_priv->hck_dir[tx] = dir; 34743d24e76SNicolin Chen esai_priv->hck_rate[tx] = freq; 34843d24e76SNicolin Chen 34943d24e76SNicolin Chen regmap_update_bits(esai_priv->regmap, REG_ESAI_ECR, 35043d24e76SNicolin Chen tx ? ESAI_ECR_ETI | ESAI_ECR_ETO : 35143d24e76SNicolin Chen ESAI_ECR_ERI | ESAI_ECR_ERO, ecr); 35243d24e76SNicolin Chen 35343d24e76SNicolin Chen return 0; 35443d24e76SNicolin Chen } 35543d24e76SNicolin Chen 35643d24e76SNicolin Chen /** 3573bae1719SPierre-Louis Bossart * fsl_esai_set_bclk - configure the related dividers according to the bclk rate 3583bae1719SPierre-Louis Bossart * @dai: pointer to DAI 3593bae1719SPierre-Louis Bossart * @tx: direction boolean 3603bae1719SPierre-Louis Bossart * @freq: bclk freq 36143d24e76SNicolin Chen */ 36243d24e76SNicolin Chen static int fsl_esai_set_bclk(struct snd_soc_dai *dai, bool tx, u32 freq) 36343d24e76SNicolin Chen { 36443d24e76SNicolin Chen struct fsl_esai *esai_priv = snd_soc_dai_get_drvdata(dai); 36543d24e76SNicolin Chen u32 hck_rate = esai_priv->hck_rate[tx]; 36643d24e76SNicolin Chen u32 sub, ratio = hck_rate / freq; 367f975ca46SNicolin Chen int ret; 36843d24e76SNicolin Chen 369f975ca46SNicolin Chen /* Don't apply for fully slave mode or unchanged bclk */ 370f975ca46SNicolin Chen if (esai_priv->slave_mode || esai_priv->sck_rate[tx] == freq) 37143d24e76SNicolin Chen return 0; 37243d24e76SNicolin Chen 37343d24e76SNicolin Chen if (ratio * freq > hck_rate) 37443d24e76SNicolin Chen sub = ratio * freq - hck_rate; 37543d24e76SNicolin Chen else if (ratio * freq < hck_rate) 37643d24e76SNicolin Chen sub = hck_rate - ratio * freq; 37743d24e76SNicolin Chen else 37843d24e76SNicolin Chen sub = 0; 37943d24e76SNicolin Chen 38043d24e76SNicolin Chen /* Block if clock source can not be divided into the required rate */ 38143d24e76SNicolin Chen if (sub != 0 && hck_rate / sub < 1000) { 38243d24e76SNicolin Chen dev_err(dai->dev, "failed to derive required SCK%c rate\n", 38343d24e76SNicolin Chen tx ? 'T' : 'R'); 38443d24e76SNicolin Chen return -EINVAL; 38543d24e76SNicolin Chen } 38643d24e76SNicolin Chen 38789e47f62SNicolin Chen /* The ratio should be contented by FP alone if bypassing PM and PSR */ 38889e47f62SNicolin Chen if (!esai_priv->sck_div[tx] && (ratio > 16 || ratio == 0)) { 38943d24e76SNicolin Chen dev_err(dai->dev, "the ratio is out of range (1 ~ 16)\n"); 39043d24e76SNicolin Chen return -EINVAL; 39143d24e76SNicolin Chen } 39243d24e76SNicolin Chen 393f975ca46SNicolin Chen ret = fsl_esai_divisor_cal(dai, tx, ratio, true, 39443d24e76SNicolin Chen esai_priv->sck_div[tx] ? 0 : ratio); 395f975ca46SNicolin Chen if (ret) 396f975ca46SNicolin Chen return ret; 397f975ca46SNicolin Chen 398f975ca46SNicolin Chen /* Save current bclk rate */ 399f975ca46SNicolin Chen esai_priv->sck_rate[tx] = freq; 400f975ca46SNicolin Chen 401f975ca46SNicolin Chen return 0; 40243d24e76SNicolin Chen } 40343d24e76SNicolin Chen 40443d24e76SNicolin Chen static int fsl_esai_set_dai_tdm_slot(struct snd_soc_dai *dai, u32 tx_mask, 40543d24e76SNicolin Chen u32 rx_mask, int slots, int slot_width) 40643d24e76SNicolin Chen { 40743d24e76SNicolin Chen struct fsl_esai *esai_priv = snd_soc_dai_get_drvdata(dai); 40843d24e76SNicolin Chen 40943d24e76SNicolin Chen regmap_update_bits(esai_priv->regmap, REG_ESAI_TCCR, 41043d24e76SNicolin Chen ESAI_xCCR_xDC_MASK, ESAI_xCCR_xDC(slots)); 41143d24e76SNicolin Chen 41243d24e76SNicolin Chen regmap_update_bits(esai_priv->regmap, REG_ESAI_RCCR, 41343d24e76SNicolin Chen ESAI_xCCR_xDC_MASK, ESAI_xCCR_xDC(slots)); 41443d24e76SNicolin Chen 41543d24e76SNicolin Chen esai_priv->slot_width = slot_width; 416de0d712aSShengjiu Wang esai_priv->slots = slots; 4170ff4e8c6SS.j. Wang esai_priv->tx_mask = tx_mask; 4180ff4e8c6SS.j. Wang esai_priv->rx_mask = rx_mask; 41943d24e76SNicolin Chen 42043d24e76SNicolin Chen return 0; 42143d24e76SNicolin Chen } 42243d24e76SNicolin Chen 42343d24e76SNicolin Chen static int fsl_esai_set_dai_fmt(struct snd_soc_dai *dai, unsigned int fmt) 42443d24e76SNicolin Chen { 42543d24e76SNicolin Chen struct fsl_esai *esai_priv = snd_soc_dai_get_drvdata(dai); 42643d24e76SNicolin Chen u32 xcr = 0, xccr = 0, mask; 42743d24e76SNicolin Chen 42843d24e76SNicolin Chen /* DAI mode */ 42943d24e76SNicolin Chen switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) { 43043d24e76SNicolin Chen case SND_SOC_DAIFMT_I2S: 43143d24e76SNicolin Chen /* Data on rising edge of bclk, frame low, 1clk before data */ 43243d24e76SNicolin Chen xcr |= ESAI_xCR_xFSR; 43343d24e76SNicolin Chen xccr |= ESAI_xCCR_xFSP | ESAI_xCCR_xCKP | ESAI_xCCR_xHCKP; 43443d24e76SNicolin Chen break; 43543d24e76SNicolin Chen case SND_SOC_DAIFMT_LEFT_J: 43643d24e76SNicolin Chen /* Data on rising edge of bclk, frame high */ 43743d24e76SNicolin Chen xccr |= ESAI_xCCR_xCKP | ESAI_xCCR_xHCKP; 43843d24e76SNicolin Chen break; 43943d24e76SNicolin Chen case SND_SOC_DAIFMT_RIGHT_J: 44043d24e76SNicolin Chen /* Data on rising edge of bclk, frame high, right aligned */ 441cc29ea00SS.j. Wang xccr |= ESAI_xCCR_xCKP | ESAI_xCCR_xHCKP; 442cc29ea00SS.j. Wang xcr |= ESAI_xCR_xWA; 44343d24e76SNicolin Chen break; 44443d24e76SNicolin Chen case SND_SOC_DAIFMT_DSP_A: 44543d24e76SNicolin Chen /* Data on rising edge of bclk, frame high, 1clk before data */ 44643d24e76SNicolin Chen xcr |= ESAI_xCR_xFSL | ESAI_xCR_xFSR; 44743d24e76SNicolin Chen xccr |= ESAI_xCCR_xCKP | ESAI_xCCR_xHCKP; 44843d24e76SNicolin Chen break; 44943d24e76SNicolin Chen case SND_SOC_DAIFMT_DSP_B: 45043d24e76SNicolin Chen /* Data on rising edge of bclk, frame high */ 45143d24e76SNicolin Chen xcr |= ESAI_xCR_xFSL; 45243d24e76SNicolin Chen xccr |= ESAI_xCCR_xCKP | ESAI_xCCR_xHCKP; 45343d24e76SNicolin Chen break; 45443d24e76SNicolin Chen default: 45543d24e76SNicolin Chen return -EINVAL; 45643d24e76SNicolin Chen } 45743d24e76SNicolin Chen 45843d24e76SNicolin Chen /* DAI clock inversion */ 45943d24e76SNicolin Chen switch (fmt & SND_SOC_DAIFMT_INV_MASK) { 46043d24e76SNicolin Chen case SND_SOC_DAIFMT_NB_NF: 46143d24e76SNicolin Chen /* Nothing to do for both normal cases */ 46243d24e76SNicolin Chen break; 46343d24e76SNicolin Chen case SND_SOC_DAIFMT_IB_NF: 46443d24e76SNicolin Chen /* Invert bit clock */ 46543d24e76SNicolin Chen xccr ^= ESAI_xCCR_xCKP | ESAI_xCCR_xHCKP; 46643d24e76SNicolin Chen break; 46743d24e76SNicolin Chen case SND_SOC_DAIFMT_NB_IF: 46843d24e76SNicolin Chen /* Invert frame clock */ 46943d24e76SNicolin Chen xccr ^= ESAI_xCCR_xFSP; 47043d24e76SNicolin Chen break; 47143d24e76SNicolin Chen case SND_SOC_DAIFMT_IB_IF: 47243d24e76SNicolin Chen /* Invert both clocks */ 47343d24e76SNicolin Chen xccr ^= ESAI_xCCR_xCKP | ESAI_xCCR_xHCKP | ESAI_xCCR_xFSP; 47443d24e76SNicolin Chen break; 47543d24e76SNicolin Chen default: 47643d24e76SNicolin Chen return -EINVAL; 47743d24e76SNicolin Chen } 47843d24e76SNicolin Chen 47943d24e76SNicolin Chen esai_priv->slave_mode = false; 48043d24e76SNicolin Chen 48143d24e76SNicolin Chen /* DAI clock master masks */ 48243d24e76SNicolin Chen switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) { 48343d24e76SNicolin Chen case SND_SOC_DAIFMT_CBM_CFM: 48443d24e76SNicolin Chen esai_priv->slave_mode = true; 48543d24e76SNicolin Chen break; 48643d24e76SNicolin Chen case SND_SOC_DAIFMT_CBS_CFM: 48743d24e76SNicolin Chen xccr |= ESAI_xCCR_xCKD; 48843d24e76SNicolin Chen break; 48943d24e76SNicolin Chen case SND_SOC_DAIFMT_CBM_CFS: 49043d24e76SNicolin Chen xccr |= ESAI_xCCR_xFSD; 49143d24e76SNicolin Chen break; 49243d24e76SNicolin Chen case SND_SOC_DAIFMT_CBS_CFS: 49343d24e76SNicolin Chen xccr |= ESAI_xCCR_xFSD | ESAI_xCCR_xCKD; 49443d24e76SNicolin Chen break; 49543d24e76SNicolin Chen default: 49643d24e76SNicolin Chen return -EINVAL; 49743d24e76SNicolin Chen } 49843d24e76SNicolin Chen 499cc29ea00SS.j. Wang mask = ESAI_xCR_xFSL | ESAI_xCR_xFSR | ESAI_xCR_xWA; 50043d24e76SNicolin Chen regmap_update_bits(esai_priv->regmap, REG_ESAI_TCR, mask, xcr); 50143d24e76SNicolin Chen regmap_update_bits(esai_priv->regmap, REG_ESAI_RCR, mask, xcr); 50243d24e76SNicolin Chen 50343d24e76SNicolin Chen mask = ESAI_xCCR_xCKP | ESAI_xCCR_xHCKP | ESAI_xCCR_xFSP | 504cc29ea00SS.j. Wang ESAI_xCCR_xFSD | ESAI_xCCR_xCKD; 50543d24e76SNicolin Chen regmap_update_bits(esai_priv->regmap, REG_ESAI_TCCR, mask, xccr); 50643d24e76SNicolin Chen regmap_update_bits(esai_priv->regmap, REG_ESAI_RCCR, mask, xccr); 50743d24e76SNicolin Chen 50843d24e76SNicolin Chen return 0; 50943d24e76SNicolin Chen } 51043d24e76SNicolin Chen 51143d24e76SNicolin Chen static int fsl_esai_startup(struct snd_pcm_substream *substream, 51243d24e76SNicolin Chen struct snd_soc_dai *dai) 51343d24e76SNicolin Chen { 51443d24e76SNicolin Chen struct fsl_esai *esai_priv = snd_soc_dai_get_drvdata(dai); 51543d24e76SNicolin Chen 5161d9fb19dSKuninori Morimoto if (!snd_soc_dai_active(dai)) { 51743d24e76SNicolin Chen /* Set synchronous mode */ 51843d24e76SNicolin Chen regmap_update_bits(esai_priv->regmap, REG_ESAI_SAICR, 51943d24e76SNicolin Chen ESAI_SAICR_SYNC, esai_priv->synchronous ? 52043d24e76SNicolin Chen ESAI_SAICR_SYNC : 0); 52143d24e76SNicolin Chen 52243d24e76SNicolin Chen /* Set a default slot number -- 2 */ 52343d24e76SNicolin Chen regmap_update_bits(esai_priv->regmap, REG_ESAI_TCCR, 52443d24e76SNicolin Chen ESAI_xCCR_xDC_MASK, ESAI_xCCR_xDC(2)); 52543d24e76SNicolin Chen regmap_update_bits(esai_priv->regmap, REG_ESAI_RCCR, 52643d24e76SNicolin Chen ESAI_xCCR_xDC_MASK, ESAI_xCCR_xDC(2)); 52743d24e76SNicolin Chen } 52843d24e76SNicolin Chen 52943d24e76SNicolin Chen return 0; 53033529ec9SFabio Estevam 53143d24e76SNicolin Chen } 53243d24e76SNicolin Chen 53343d24e76SNicolin Chen static int fsl_esai_hw_params(struct snd_pcm_substream *substream, 53443d24e76SNicolin Chen struct snd_pcm_hw_params *params, 53543d24e76SNicolin Chen struct snd_soc_dai *dai) 53643d24e76SNicolin Chen { 53743d24e76SNicolin Chen struct fsl_esai *esai_priv = snd_soc_dai_get_drvdata(dai); 53843d24e76SNicolin Chen bool tx = substream->stream == SNDRV_PCM_STREAM_PLAYBACK; 5394ca73043SZidan Wang u32 width = params_width(params); 54043d24e76SNicolin Chen u32 channels = params_channels(params); 541de0d712aSShengjiu Wang u32 pins = DIV_ROUND_UP(channels, esai_priv->slots); 54286ea522bSNicolin Chen u32 slot_width = width; 5433e185238SXiubo Li u32 bclk, mask, val; 5443e185238SXiubo Li int ret; 54543d24e76SNicolin Chen 546d8ffcf71SGeert Uytterhoeven /* Override slot_width if being specifically set */ 54786ea522bSNicolin Chen if (esai_priv->slot_width) 54886ea522bSNicolin Chen slot_width = esai_priv->slot_width; 54986ea522bSNicolin Chen 55086ea522bSNicolin Chen bclk = params_rate(params) * slot_width * esai_priv->slots; 55143d24e76SNicolin Chen 5521997ee89SS.j. Wang ret = fsl_esai_set_bclk(dai, esai_priv->synchronous || tx, bclk); 55343d24e76SNicolin Chen if (ret) 55443d24e76SNicolin Chen return ret; 55543d24e76SNicolin Chen 5561997ee89SS.j. Wang mask = ESAI_xCR_xSWS_MASK; 5571997ee89SS.j. Wang val = ESAI_xCR_xSWS(slot_width, width); 5581997ee89SS.j. Wang 5591997ee89SS.j. Wang regmap_update_bits(esai_priv->regmap, REG_ESAI_xCR(tx), mask, val); 5601997ee89SS.j. Wang /* Recording in synchronous mode needs to set TCR also */ 5611997ee89SS.j. Wang if (!tx && esai_priv->synchronous) 5621997ee89SS.j. Wang regmap_update_bits(esai_priv->regmap, REG_ESAI_TCR, mask, val); 5631997ee89SS.j. Wang 56443d24e76SNicolin Chen /* Use Normal mode to support monaural audio */ 56543d24e76SNicolin Chen regmap_update_bits(esai_priv->regmap, REG_ESAI_xCR(tx), 56643d24e76SNicolin Chen ESAI_xCR_xMOD_MASK, params_channels(params) > 1 ? 56743d24e76SNicolin Chen ESAI_xCR_xMOD_NETWORK : 0); 56843d24e76SNicolin Chen 56943d24e76SNicolin Chen regmap_update_bits(esai_priv->regmap, REG_ESAI_xFCR(tx), 57043d24e76SNicolin Chen ESAI_xFCR_xFR_MASK, ESAI_xFCR_xFR); 57143d24e76SNicolin Chen 57243d24e76SNicolin Chen mask = ESAI_xFCR_xFR_MASK | ESAI_xFCR_xWA_MASK | ESAI_xFCR_xFWM_MASK | 57343d24e76SNicolin Chen (tx ? ESAI_xFCR_TE_MASK | ESAI_xFCR_TIEN : ESAI_xFCR_RE_MASK); 57443d24e76SNicolin Chen val = ESAI_xFCR_xWA(width) | ESAI_xFCR_xFWM(esai_priv->fifo_depth) | 575de0d712aSShengjiu Wang (tx ? ESAI_xFCR_TE(pins) | ESAI_xFCR_TIEN : ESAI_xFCR_RE(pins)); 57643d24e76SNicolin Chen 57743d24e76SNicolin Chen regmap_update_bits(esai_priv->regmap, REG_ESAI_xFCR(tx), mask, val); 57843d24e76SNicolin Chen 5791997ee89SS.j. Wang if (tx) 5801997ee89SS.j. Wang regmap_update_bits(esai_priv->regmap, REG_ESAI_TCR, 5811997ee89SS.j. Wang ESAI_xCR_PADC, ESAI_xCR_PADC); 58243d24e76SNicolin Chen 5834f8210f6SNicolin Chen /* Remove ESAI personal reset by configuring ESAI_PCRC and ESAI_PRRC */ 5844f8210f6SNicolin Chen regmap_update_bits(esai_priv->regmap, REG_ESAI_PRRC, 5854f8210f6SNicolin Chen ESAI_PRRC_PDC_MASK, ESAI_PRRC_PDC(ESAI_GPIO)); 5864f8210f6SNicolin Chen regmap_update_bits(esai_priv->regmap, REG_ESAI_PCRC, 5874f8210f6SNicolin Chen ESAI_PCRC_PC_MASK, ESAI_PCRC_PC(ESAI_GPIO)); 58843d24e76SNicolin Chen return 0; 58943d24e76SNicolin Chen } 59043d24e76SNicolin Chen 5915be6155bSShengjiu Wang static int fsl_esai_hw_init(struct fsl_esai *esai_priv) 59243d24e76SNicolin Chen { 5935be6155bSShengjiu Wang struct platform_device *pdev = esai_priv->pdev; 5945be6155bSShengjiu Wang int ret; 5955be6155bSShengjiu Wang 5965be6155bSShengjiu Wang /* Reset ESAI unit */ 5975be6155bSShengjiu Wang ret = regmap_update_bits(esai_priv->regmap, REG_ESAI_ECR, 5985be6155bSShengjiu Wang ESAI_ECR_ESAIEN_MASK | ESAI_ECR_ERST_MASK, 5995be6155bSShengjiu Wang ESAI_ECR_ESAIEN | ESAI_ECR_ERST); 6005be6155bSShengjiu Wang if (ret) { 6015be6155bSShengjiu Wang dev_err(&pdev->dev, "failed to reset ESAI: %d\n", ret); 6025be6155bSShengjiu Wang return ret; 6035be6155bSShengjiu Wang } 6045be6155bSShengjiu Wang 6055be6155bSShengjiu Wang /* 6065be6155bSShengjiu Wang * We need to enable ESAI so as to access some of its registers. 6075be6155bSShengjiu Wang * Otherwise, we would fail to dump regmap from user space. 6085be6155bSShengjiu Wang */ 6095be6155bSShengjiu Wang ret = regmap_update_bits(esai_priv->regmap, REG_ESAI_ECR, 6105be6155bSShengjiu Wang ESAI_ECR_ESAIEN_MASK | ESAI_ECR_ERST_MASK, 6115be6155bSShengjiu Wang ESAI_ECR_ESAIEN); 6125be6155bSShengjiu Wang if (ret) { 6135be6155bSShengjiu Wang dev_err(&pdev->dev, "failed to enable ESAI: %d\n", ret); 6145be6155bSShengjiu Wang return ret; 6155be6155bSShengjiu Wang } 6165be6155bSShengjiu Wang 6175be6155bSShengjiu Wang regmap_update_bits(esai_priv->regmap, REG_ESAI_PRRC, 6185be6155bSShengjiu Wang ESAI_PRRC_PDC_MASK, 0); 6195be6155bSShengjiu Wang regmap_update_bits(esai_priv->regmap, REG_ESAI_PCRC, 6205be6155bSShengjiu Wang ESAI_PCRC_PC_MASK, 0); 6215be6155bSShengjiu Wang 6225be6155bSShengjiu Wang return 0; 6235be6155bSShengjiu Wang } 6245be6155bSShengjiu Wang 6255be6155bSShengjiu Wang static int fsl_esai_register_restore(struct fsl_esai *esai_priv) 6265be6155bSShengjiu Wang { 6275be6155bSShengjiu Wang int ret; 6285be6155bSShengjiu Wang 6295be6155bSShengjiu Wang /* FIFO reset for safety */ 6305be6155bSShengjiu Wang regmap_update_bits(esai_priv->regmap, REG_ESAI_TFCR, 6315be6155bSShengjiu Wang ESAI_xFCR_xFR, ESAI_xFCR_xFR); 6325be6155bSShengjiu Wang regmap_update_bits(esai_priv->regmap, REG_ESAI_RFCR, 6335be6155bSShengjiu Wang ESAI_xFCR_xFR, ESAI_xFCR_xFR); 6345be6155bSShengjiu Wang 6355be6155bSShengjiu Wang regcache_mark_dirty(esai_priv->regmap); 6365be6155bSShengjiu Wang ret = regcache_sync(esai_priv->regmap); 6375be6155bSShengjiu Wang if (ret) 6385be6155bSShengjiu Wang return ret; 6395be6155bSShengjiu Wang 6405be6155bSShengjiu Wang /* FIFO reset done */ 6415be6155bSShengjiu Wang regmap_update_bits(esai_priv->regmap, REG_ESAI_TFCR, ESAI_xFCR_xFR, 0); 6425be6155bSShengjiu Wang regmap_update_bits(esai_priv->regmap, REG_ESAI_RFCR, ESAI_xFCR_xFR, 0); 6435be6155bSShengjiu Wang 6445be6155bSShengjiu Wang return 0; 6455be6155bSShengjiu Wang } 6465be6155bSShengjiu Wang 6475be6155bSShengjiu Wang static void fsl_esai_trigger_start(struct fsl_esai *esai_priv, bool tx) 6485be6155bSShengjiu Wang { 6495be6155bSShengjiu Wang u8 i, channels = esai_priv->channels[tx]; 650de0d712aSShengjiu Wang u32 pins = DIV_ROUND_UP(channels, esai_priv->slots); 6510ff4e8c6SS.j. Wang u32 mask; 65243d24e76SNicolin Chen 65343d24e76SNicolin Chen regmap_update_bits(esai_priv->regmap, REG_ESAI_xFCR(tx), 65443d24e76SNicolin Chen ESAI_xFCR_xFEN_MASK, ESAI_xFCR_xFEN); 65543d24e76SNicolin Chen 65643d24e76SNicolin Chen /* Write initial words reqiured by ESAI as normal procedure */ 65743d24e76SNicolin Chen for (i = 0; tx && i < channels; i++) 65843d24e76SNicolin Chen regmap_write(esai_priv->regmap, REG_ESAI_ETDR, 0x0); 65943d24e76SNicolin Chen 6600ff4e8c6SS.j. Wang /* 6610ff4e8c6SS.j. Wang * When set the TE/RE in the end of enablement flow, there 6620ff4e8c6SS.j. Wang * will be channel swap issue for multi data line case. 6630ff4e8c6SS.j. Wang * In order to workaround this issue, we switch the bit 6640ff4e8c6SS.j. Wang * enablement sequence to below sequence 6650ff4e8c6SS.j. Wang * 1) clear the xSMB & xSMA: which is done in probe and 6660ff4e8c6SS.j. Wang * stop state. 6670ff4e8c6SS.j. Wang * 2) set TE/RE 6680ff4e8c6SS.j. Wang * 3) set xSMB 6690ff4e8c6SS.j. Wang * 4) set xSMA: xSMA is the last one in this flow, which 6700ff4e8c6SS.j. Wang * will trigger esai to start. 6710ff4e8c6SS.j. Wang */ 67243d24e76SNicolin Chen regmap_update_bits(esai_priv->regmap, REG_ESAI_xCR(tx), 67343d24e76SNicolin Chen tx ? ESAI_xCR_TE_MASK : ESAI_xCR_RE_MASK, 674de0d712aSShengjiu Wang tx ? ESAI_xCR_TE(pins) : ESAI_xCR_RE(pins)); 6750ff4e8c6SS.j. Wang mask = tx ? esai_priv->tx_mask : esai_priv->rx_mask; 6760ff4e8c6SS.j. Wang 6770ff4e8c6SS.j. Wang regmap_update_bits(esai_priv->regmap, REG_ESAI_xSMB(tx), 6780ff4e8c6SS.j. Wang ESAI_xSMB_xS_MASK, ESAI_xSMB_xS(mask)); 6790ff4e8c6SS.j. Wang regmap_update_bits(esai_priv->regmap, REG_ESAI_xSMA(tx), 6800ff4e8c6SS.j. Wang ESAI_xSMA_xS_MASK, ESAI_xSMA_xS(mask)); 6817ccafa2bSShengjiu Wang 6827ccafa2bSShengjiu Wang /* Enable Exception interrupt */ 6837ccafa2bSShengjiu Wang regmap_update_bits(esai_priv->regmap, REG_ESAI_xCR(tx), 6847ccafa2bSShengjiu Wang ESAI_xCR_xEIE_MASK, ESAI_xCR_xEIE); 6855be6155bSShengjiu Wang } 6860ff4e8c6SS.j. Wang 6875be6155bSShengjiu Wang static void fsl_esai_trigger_stop(struct fsl_esai *esai_priv, bool tx) 6885be6155bSShengjiu Wang { 68943d24e76SNicolin Chen regmap_update_bits(esai_priv->regmap, REG_ESAI_xCR(tx), 6907ccafa2bSShengjiu Wang ESAI_xCR_xEIE_MASK, 0); 6917ccafa2bSShengjiu Wang 6927ccafa2bSShengjiu Wang regmap_update_bits(esai_priv->regmap, REG_ESAI_xCR(tx), 69343d24e76SNicolin Chen tx ? ESAI_xCR_TE_MASK : ESAI_xCR_RE_MASK, 0); 6940ff4e8c6SS.j. Wang regmap_update_bits(esai_priv->regmap, REG_ESAI_xSMA(tx), 6950ff4e8c6SS.j. Wang ESAI_xSMA_xS_MASK, 0); 6960ff4e8c6SS.j. Wang regmap_update_bits(esai_priv->regmap, REG_ESAI_xSMB(tx), 6970ff4e8c6SS.j. Wang ESAI_xSMB_xS_MASK, 0); 69843d24e76SNicolin Chen 69943d24e76SNicolin Chen /* Disable and reset FIFO */ 70043d24e76SNicolin Chen regmap_update_bits(esai_priv->regmap, REG_ESAI_xFCR(tx), 70143d24e76SNicolin Chen ESAI_xFCR_xFR | ESAI_xFCR_xFEN, ESAI_xFCR_xFR); 70243d24e76SNicolin Chen regmap_update_bits(esai_priv->regmap, REG_ESAI_xFCR(tx), 70343d24e76SNicolin Chen ESAI_xFCR_xFR, 0); 7045be6155bSShengjiu Wang } 7055be6155bSShengjiu Wang 706a3d1f931STakashi Iwai static void fsl_esai_hw_reset(struct work_struct *work) 7077ccafa2bSShengjiu Wang { 708a3d1f931STakashi Iwai struct fsl_esai *esai_priv = container_of(work, struct fsl_esai, work); 7097ccafa2bSShengjiu Wang bool tx = true, rx = false, enabled[2]; 71035dac627SShengjiu Wang unsigned long lock_flags; 7117ccafa2bSShengjiu Wang u32 tfcr, rfcr; 7127ccafa2bSShengjiu Wang 71335dac627SShengjiu Wang spin_lock_irqsave(&esai_priv->lock, lock_flags); 7147ccafa2bSShengjiu Wang /* Save the registers */ 7157ccafa2bSShengjiu Wang regmap_read(esai_priv->regmap, REG_ESAI_TFCR, &tfcr); 7167ccafa2bSShengjiu Wang regmap_read(esai_priv->regmap, REG_ESAI_RFCR, &rfcr); 7177ccafa2bSShengjiu Wang enabled[tx] = tfcr & ESAI_xFCR_xFEN; 7187ccafa2bSShengjiu Wang enabled[rx] = rfcr & ESAI_xFCR_xFEN; 7197ccafa2bSShengjiu Wang 7207ccafa2bSShengjiu Wang /* Stop the tx & rx */ 7217ccafa2bSShengjiu Wang fsl_esai_trigger_stop(esai_priv, tx); 7227ccafa2bSShengjiu Wang fsl_esai_trigger_stop(esai_priv, rx); 7237ccafa2bSShengjiu Wang 7247ccafa2bSShengjiu Wang /* Reset the esai, and ignore return value */ 7257ccafa2bSShengjiu Wang fsl_esai_hw_init(esai_priv); 7267ccafa2bSShengjiu Wang 7277ccafa2bSShengjiu Wang /* Enforce ESAI personal resets for both TX and RX */ 7287ccafa2bSShengjiu Wang regmap_update_bits(esai_priv->regmap, REG_ESAI_TCR, 7297ccafa2bSShengjiu Wang ESAI_xCR_xPR_MASK, ESAI_xCR_xPR); 7307ccafa2bSShengjiu Wang regmap_update_bits(esai_priv->regmap, REG_ESAI_RCR, 7317ccafa2bSShengjiu Wang ESAI_xCR_xPR_MASK, ESAI_xCR_xPR); 7327ccafa2bSShengjiu Wang 7337ccafa2bSShengjiu Wang /* Restore registers by regcache_sync, and ignore return value */ 7347ccafa2bSShengjiu Wang fsl_esai_register_restore(esai_priv); 7357ccafa2bSShengjiu Wang 7367ccafa2bSShengjiu Wang /* Remove ESAI personal resets by configuring PCRC and PRRC also */ 7377ccafa2bSShengjiu Wang regmap_update_bits(esai_priv->regmap, REG_ESAI_TCR, 7387ccafa2bSShengjiu Wang ESAI_xCR_xPR_MASK, 0); 7397ccafa2bSShengjiu Wang regmap_update_bits(esai_priv->regmap, REG_ESAI_RCR, 7407ccafa2bSShengjiu Wang ESAI_xCR_xPR_MASK, 0); 7417ccafa2bSShengjiu Wang regmap_update_bits(esai_priv->regmap, REG_ESAI_PRRC, 7427ccafa2bSShengjiu Wang ESAI_PRRC_PDC_MASK, ESAI_PRRC_PDC(ESAI_GPIO)); 7437ccafa2bSShengjiu Wang regmap_update_bits(esai_priv->regmap, REG_ESAI_PCRC, 7447ccafa2bSShengjiu Wang ESAI_PCRC_PC_MASK, ESAI_PCRC_PC(ESAI_GPIO)); 7457ccafa2bSShengjiu Wang 7467ccafa2bSShengjiu Wang /* Restart tx / rx, if they already enabled */ 7477ccafa2bSShengjiu Wang if (enabled[tx]) 7487ccafa2bSShengjiu Wang fsl_esai_trigger_start(esai_priv, tx); 7497ccafa2bSShengjiu Wang if (enabled[rx]) 7507ccafa2bSShengjiu Wang fsl_esai_trigger_start(esai_priv, rx); 75135dac627SShengjiu Wang 75235dac627SShengjiu Wang spin_unlock_irqrestore(&esai_priv->lock, lock_flags); 7537ccafa2bSShengjiu Wang } 7547ccafa2bSShengjiu Wang 7555be6155bSShengjiu Wang static int fsl_esai_trigger(struct snd_pcm_substream *substream, int cmd, 7565be6155bSShengjiu Wang struct snd_soc_dai *dai) 7575be6155bSShengjiu Wang { 7585be6155bSShengjiu Wang struct fsl_esai *esai_priv = snd_soc_dai_get_drvdata(dai); 7595be6155bSShengjiu Wang bool tx = substream->stream == SNDRV_PCM_STREAM_PLAYBACK; 76035dac627SShengjiu Wang unsigned long lock_flags; 7615be6155bSShengjiu Wang 7625be6155bSShengjiu Wang esai_priv->channels[tx] = substream->runtime->channels; 7635be6155bSShengjiu Wang 7645be6155bSShengjiu Wang switch (cmd) { 7655be6155bSShengjiu Wang case SNDRV_PCM_TRIGGER_START: 7665be6155bSShengjiu Wang case SNDRV_PCM_TRIGGER_RESUME: 7675be6155bSShengjiu Wang case SNDRV_PCM_TRIGGER_PAUSE_RELEASE: 76835dac627SShengjiu Wang spin_lock_irqsave(&esai_priv->lock, lock_flags); 7695be6155bSShengjiu Wang fsl_esai_trigger_start(esai_priv, tx); 77035dac627SShengjiu Wang spin_unlock_irqrestore(&esai_priv->lock, lock_flags); 7715be6155bSShengjiu Wang break; 7725be6155bSShengjiu Wang case SNDRV_PCM_TRIGGER_SUSPEND: 7735be6155bSShengjiu Wang case SNDRV_PCM_TRIGGER_STOP: 7745be6155bSShengjiu Wang case SNDRV_PCM_TRIGGER_PAUSE_PUSH: 77535dac627SShengjiu Wang spin_lock_irqsave(&esai_priv->lock, lock_flags); 7765be6155bSShengjiu Wang fsl_esai_trigger_stop(esai_priv, tx); 77735dac627SShengjiu Wang spin_unlock_irqrestore(&esai_priv->lock, lock_flags); 77843d24e76SNicolin Chen break; 77943d24e76SNicolin Chen default: 78043d24e76SNicolin Chen return -EINVAL; 78143d24e76SNicolin Chen } 78243d24e76SNicolin Chen 78343d24e76SNicolin Chen return 0; 78443d24e76SNicolin Chen } 78543d24e76SNicolin Chen 7865d29e95eSGustavo A. R. Silva static const struct snd_soc_dai_ops fsl_esai_dai_ops = { 78743d24e76SNicolin Chen .startup = fsl_esai_startup, 78843d24e76SNicolin Chen .trigger = fsl_esai_trigger, 78943d24e76SNicolin Chen .hw_params = fsl_esai_hw_params, 79043d24e76SNicolin Chen .set_sysclk = fsl_esai_set_dai_sysclk, 79143d24e76SNicolin Chen .set_fmt = fsl_esai_set_dai_fmt, 79243d24e76SNicolin Chen .set_tdm_slot = fsl_esai_set_dai_tdm_slot, 79343d24e76SNicolin Chen }; 79443d24e76SNicolin Chen 79543d24e76SNicolin Chen static int fsl_esai_dai_probe(struct snd_soc_dai *dai) 79643d24e76SNicolin Chen { 79743d24e76SNicolin Chen struct fsl_esai *esai_priv = snd_soc_dai_get_drvdata(dai); 79843d24e76SNicolin Chen 79943d24e76SNicolin Chen snd_soc_dai_init_dma_data(dai, &esai_priv->dma_params_tx, 80043d24e76SNicolin Chen &esai_priv->dma_params_rx); 80143d24e76SNicolin Chen 80243d24e76SNicolin Chen return 0; 80343d24e76SNicolin Chen } 80443d24e76SNicolin Chen 80543d24e76SNicolin Chen static struct snd_soc_dai_driver fsl_esai_dai = { 80643d24e76SNicolin Chen .probe = fsl_esai_dai_probe, 80743d24e76SNicolin Chen .playback = { 80874ccb27cSNicolin Chen .stream_name = "CPU-Playback", 80943d24e76SNicolin Chen .channels_min = 1, 81043d24e76SNicolin Chen .channels_max = 12, 811f2a3ee01SFabio Estevam .rates = SNDRV_PCM_RATE_8000_192000, 81243d24e76SNicolin Chen .formats = FSL_ESAI_FORMATS, 81343d24e76SNicolin Chen }, 81443d24e76SNicolin Chen .capture = { 81574ccb27cSNicolin Chen .stream_name = "CPU-Capture", 81643d24e76SNicolin Chen .channels_min = 1, 81743d24e76SNicolin Chen .channels_max = 8, 818f2a3ee01SFabio Estevam .rates = SNDRV_PCM_RATE_8000_192000, 81943d24e76SNicolin Chen .formats = FSL_ESAI_FORMATS, 82043d24e76SNicolin Chen }, 82143d24e76SNicolin Chen .ops = &fsl_esai_dai_ops, 82243d24e76SNicolin Chen }; 82343d24e76SNicolin Chen 82443d24e76SNicolin Chen static const struct snd_soc_component_driver fsl_esai_component = { 82543d24e76SNicolin Chen .name = "fsl-esai", 82643d24e76SNicolin Chen }; 82743d24e76SNicolin Chen 828c64c6076SZidan Wang static const struct reg_default fsl_esai_reg_defaults[] = { 8298973112aSZidan Wang {REG_ESAI_ETDR, 0x00000000}, 8308973112aSZidan Wang {REG_ESAI_ECR, 0x00000000}, 8318973112aSZidan Wang {REG_ESAI_TFCR, 0x00000000}, 8328973112aSZidan Wang {REG_ESAI_RFCR, 0x00000000}, 8338973112aSZidan Wang {REG_ESAI_TX0, 0x00000000}, 8348973112aSZidan Wang {REG_ESAI_TX1, 0x00000000}, 8358973112aSZidan Wang {REG_ESAI_TX2, 0x00000000}, 8368973112aSZidan Wang {REG_ESAI_TX3, 0x00000000}, 8378973112aSZidan Wang {REG_ESAI_TX4, 0x00000000}, 8388973112aSZidan Wang {REG_ESAI_TX5, 0x00000000}, 8398973112aSZidan Wang {REG_ESAI_TSR, 0x00000000}, 8408973112aSZidan Wang {REG_ESAI_SAICR, 0x00000000}, 8418973112aSZidan Wang {REG_ESAI_TCR, 0x00000000}, 8428973112aSZidan Wang {REG_ESAI_TCCR, 0x00000000}, 8438973112aSZidan Wang {REG_ESAI_RCR, 0x00000000}, 8448973112aSZidan Wang {REG_ESAI_RCCR, 0x00000000}, 8458973112aSZidan Wang {REG_ESAI_TSMA, 0x0000ffff}, 8468973112aSZidan Wang {REG_ESAI_TSMB, 0x0000ffff}, 8478973112aSZidan Wang {REG_ESAI_RSMA, 0x0000ffff}, 8488973112aSZidan Wang {REG_ESAI_RSMB, 0x0000ffff}, 8498973112aSZidan Wang {REG_ESAI_PRRC, 0x00000000}, 8508973112aSZidan Wang {REG_ESAI_PCRC, 0x00000000}, 851c64c6076SZidan Wang }; 852c64c6076SZidan Wang 85343d24e76SNicolin Chen static bool fsl_esai_readable_reg(struct device *dev, unsigned int reg) 85443d24e76SNicolin Chen { 85543d24e76SNicolin Chen switch (reg) { 85643d24e76SNicolin Chen case REG_ESAI_ERDR: 85743d24e76SNicolin Chen case REG_ESAI_ECR: 85843d24e76SNicolin Chen case REG_ESAI_ESR: 85943d24e76SNicolin Chen case REG_ESAI_TFCR: 86043d24e76SNicolin Chen case REG_ESAI_TFSR: 86143d24e76SNicolin Chen case REG_ESAI_RFCR: 86243d24e76SNicolin Chen case REG_ESAI_RFSR: 86343d24e76SNicolin Chen case REG_ESAI_RX0: 86443d24e76SNicolin Chen case REG_ESAI_RX1: 86543d24e76SNicolin Chen case REG_ESAI_RX2: 86643d24e76SNicolin Chen case REG_ESAI_RX3: 86743d24e76SNicolin Chen case REG_ESAI_SAISR: 86843d24e76SNicolin Chen case REG_ESAI_SAICR: 86943d24e76SNicolin Chen case REG_ESAI_TCR: 87043d24e76SNicolin Chen case REG_ESAI_TCCR: 87143d24e76SNicolin Chen case REG_ESAI_RCR: 87243d24e76SNicolin Chen case REG_ESAI_RCCR: 87343d24e76SNicolin Chen case REG_ESAI_TSMA: 87443d24e76SNicolin Chen case REG_ESAI_TSMB: 87543d24e76SNicolin Chen case REG_ESAI_RSMA: 87643d24e76SNicolin Chen case REG_ESAI_RSMB: 87743d24e76SNicolin Chen case REG_ESAI_PRRC: 87843d24e76SNicolin Chen case REG_ESAI_PCRC: 87943d24e76SNicolin Chen return true; 88043d24e76SNicolin Chen default: 88143d24e76SNicolin Chen return false; 88243d24e76SNicolin Chen } 88343d24e76SNicolin Chen } 88443d24e76SNicolin Chen 885c64c6076SZidan Wang static bool fsl_esai_volatile_reg(struct device *dev, unsigned int reg) 886c64c6076SZidan Wang { 887c64c6076SZidan Wang switch (reg) { 888c64c6076SZidan Wang case REG_ESAI_ERDR: 889c64c6076SZidan Wang case REG_ESAI_ESR: 890c64c6076SZidan Wang case REG_ESAI_TFSR: 891c64c6076SZidan Wang case REG_ESAI_RFSR: 892c64c6076SZidan Wang case REG_ESAI_RX0: 893c64c6076SZidan Wang case REG_ESAI_RX1: 894c64c6076SZidan Wang case REG_ESAI_RX2: 895c64c6076SZidan Wang case REG_ESAI_RX3: 896c64c6076SZidan Wang case REG_ESAI_SAISR: 897c64c6076SZidan Wang return true; 898c64c6076SZidan Wang default: 899c64c6076SZidan Wang return false; 900c64c6076SZidan Wang } 901c64c6076SZidan Wang } 902c64c6076SZidan Wang 90343d24e76SNicolin Chen static bool fsl_esai_writeable_reg(struct device *dev, unsigned int reg) 90443d24e76SNicolin Chen { 90543d24e76SNicolin Chen switch (reg) { 90643d24e76SNicolin Chen case REG_ESAI_ETDR: 90743d24e76SNicolin Chen case REG_ESAI_ECR: 90843d24e76SNicolin Chen case REG_ESAI_TFCR: 90943d24e76SNicolin Chen case REG_ESAI_RFCR: 91043d24e76SNicolin Chen case REG_ESAI_TX0: 91143d24e76SNicolin Chen case REG_ESAI_TX1: 91243d24e76SNicolin Chen case REG_ESAI_TX2: 91343d24e76SNicolin Chen case REG_ESAI_TX3: 91443d24e76SNicolin Chen case REG_ESAI_TX4: 91543d24e76SNicolin Chen case REG_ESAI_TX5: 91643d24e76SNicolin Chen case REG_ESAI_TSR: 91743d24e76SNicolin Chen case REG_ESAI_SAICR: 91843d24e76SNicolin Chen case REG_ESAI_TCR: 91943d24e76SNicolin Chen case REG_ESAI_TCCR: 92043d24e76SNicolin Chen case REG_ESAI_RCR: 92143d24e76SNicolin Chen case REG_ESAI_RCCR: 92243d24e76SNicolin Chen case REG_ESAI_TSMA: 92343d24e76SNicolin Chen case REG_ESAI_TSMB: 92443d24e76SNicolin Chen case REG_ESAI_RSMA: 92543d24e76SNicolin Chen case REG_ESAI_RSMB: 92643d24e76SNicolin Chen case REG_ESAI_PRRC: 92743d24e76SNicolin Chen case REG_ESAI_PCRC: 92843d24e76SNicolin Chen return true; 92943d24e76SNicolin Chen default: 93043d24e76SNicolin Chen return false; 93143d24e76SNicolin Chen } 93243d24e76SNicolin Chen } 93343d24e76SNicolin Chen 93492bd0334SXiubo Li static const struct regmap_config fsl_esai_regmap_config = { 93543d24e76SNicolin Chen .reg_bits = 32, 93643d24e76SNicolin Chen .reg_stride = 4, 93743d24e76SNicolin Chen .val_bits = 32, 93843d24e76SNicolin Chen 93943d24e76SNicolin Chen .max_register = REG_ESAI_PCRC, 940c64c6076SZidan Wang .reg_defaults = fsl_esai_reg_defaults, 941c64c6076SZidan Wang .num_reg_defaults = ARRAY_SIZE(fsl_esai_reg_defaults), 94243d24e76SNicolin Chen .readable_reg = fsl_esai_readable_reg, 943c64c6076SZidan Wang .volatile_reg = fsl_esai_volatile_reg, 94443d24e76SNicolin Chen .writeable_reg = fsl_esai_writeable_reg, 9450effb865SMarek Vasut .cache_type = REGCACHE_FLAT, 94643d24e76SNicolin Chen }; 94743d24e76SNicolin Chen 948*203773e3SShengjiu Wang static int fsl_esai_runtime_resume(struct device *dev); 949*203773e3SShengjiu Wang static int fsl_esai_runtime_suspend(struct device *dev); 950*203773e3SShengjiu Wang 95143d24e76SNicolin Chen static int fsl_esai_probe(struct platform_device *pdev) 95243d24e76SNicolin Chen { 95343d24e76SNicolin Chen struct device_node *np = pdev->dev.of_node; 95443d24e76SNicolin Chen struct fsl_esai *esai_priv; 95543d24e76SNicolin Chen struct resource *res; 9560600b3e1SFabio Estevam const __be32 *iprop; 95743d24e76SNicolin Chen void __iomem *regs; 95843d24e76SNicolin Chen int irq, ret; 95943d24e76SNicolin Chen 96043d24e76SNicolin Chen esai_priv = devm_kzalloc(&pdev->dev, sizeof(*esai_priv), GFP_KERNEL); 96143d24e76SNicolin Chen if (!esai_priv) 96243d24e76SNicolin Chen return -ENOMEM; 96343d24e76SNicolin Chen 96443d24e76SNicolin Chen esai_priv->pdev = pdev; 9655d585e1eSRob Herring snprintf(esai_priv->name, sizeof(esai_priv->name), "%pOFn", np); 96643d24e76SNicolin Chen 9676878e752SShengjiu Wang esai_priv->soc = of_device_get_match_data(&pdev->dev); 9687ccafa2bSShengjiu Wang 96943d24e76SNicolin Chen /* Get the addresses and IRQ */ 97043d24e76SNicolin Chen res = platform_get_resource(pdev, IORESOURCE_MEM, 0); 97143d24e76SNicolin Chen regs = devm_ioremap_resource(&pdev->dev, res); 97243d24e76SNicolin Chen if (IS_ERR(regs)) 97343d24e76SNicolin Chen return PTR_ERR(regs); 97443d24e76SNicolin Chen 975*203773e3SShengjiu Wang esai_priv->regmap = devm_regmap_init_mmio(&pdev->dev, regs, &fsl_esai_regmap_config); 97643d24e76SNicolin Chen if (IS_ERR(esai_priv->regmap)) { 97743d24e76SNicolin Chen dev_err(&pdev->dev, "failed to init regmap: %ld\n", 97843d24e76SNicolin Chen PTR_ERR(esai_priv->regmap)); 97943d24e76SNicolin Chen return PTR_ERR(esai_priv->regmap); 98043d24e76SNicolin Chen } 98143d24e76SNicolin Chen 98243d24e76SNicolin Chen esai_priv->coreclk = devm_clk_get(&pdev->dev, "core"); 98343d24e76SNicolin Chen if (IS_ERR(esai_priv->coreclk)) { 98443d24e76SNicolin Chen dev_err(&pdev->dev, "failed to get core clock: %ld\n", 98543d24e76SNicolin Chen PTR_ERR(esai_priv->coreclk)); 98643d24e76SNicolin Chen return PTR_ERR(esai_priv->coreclk); 98743d24e76SNicolin Chen } 98843d24e76SNicolin Chen 98943d24e76SNicolin Chen esai_priv->extalclk = devm_clk_get(&pdev->dev, "extal"); 99043d24e76SNicolin Chen if (IS_ERR(esai_priv->extalclk)) 99143d24e76SNicolin Chen dev_warn(&pdev->dev, "failed to get extal clock: %ld\n", 99243d24e76SNicolin Chen PTR_ERR(esai_priv->extalclk)); 99343d24e76SNicolin Chen 99443d24e76SNicolin Chen esai_priv->fsysclk = devm_clk_get(&pdev->dev, "fsys"); 99543d24e76SNicolin Chen if (IS_ERR(esai_priv->fsysclk)) 99643d24e76SNicolin Chen dev_warn(&pdev->dev, "failed to get fsys clock: %ld\n", 99743d24e76SNicolin Chen PTR_ERR(esai_priv->fsysclk)); 99843d24e76SNicolin Chen 999a2a4d604SShengjiu Wang esai_priv->spbaclk = devm_clk_get(&pdev->dev, "spba"); 1000a2a4d604SShengjiu Wang if (IS_ERR(esai_priv->spbaclk)) 1001a2a4d604SShengjiu Wang dev_warn(&pdev->dev, "failed to get spba clock: %ld\n", 1002a2a4d604SShengjiu Wang PTR_ERR(esai_priv->spbaclk)); 1003a2a4d604SShengjiu Wang 100443d24e76SNicolin Chen irq = platform_get_irq(pdev, 0); 1005cf9441adSStephen Boyd if (irq < 0) 100643d24e76SNicolin Chen return irq; 100743d24e76SNicolin Chen 1008c8361757SShengjiu Wang ret = devm_request_irq(&pdev->dev, irq, esai_isr, IRQF_SHARED, 100943d24e76SNicolin Chen esai_priv->name, esai_priv); 101043d24e76SNicolin Chen if (ret) { 101143d24e76SNicolin Chen dev_err(&pdev->dev, "failed to claim irq %u\n", irq); 101243d24e76SNicolin Chen return ret; 101343d24e76SNicolin Chen } 101443d24e76SNicolin Chen 1015de0d712aSShengjiu Wang /* Set a default slot number */ 1016de0d712aSShengjiu Wang esai_priv->slots = 2; 1017de0d712aSShengjiu Wang 101843d24e76SNicolin Chen /* Set a default master/slave state */ 101943d24e76SNicolin Chen esai_priv->slave_mode = true; 102043d24e76SNicolin Chen 102143d24e76SNicolin Chen /* Determine the FIFO depth */ 102243d24e76SNicolin Chen iprop = of_get_property(np, "fsl,fifo-depth", NULL); 102343d24e76SNicolin Chen if (iprop) 102443d24e76SNicolin Chen esai_priv->fifo_depth = be32_to_cpup(iprop); 102543d24e76SNicolin Chen else 102643d24e76SNicolin Chen esai_priv->fifo_depth = 64; 102743d24e76SNicolin Chen 102843d24e76SNicolin Chen esai_priv->dma_params_tx.maxburst = 16; 102943d24e76SNicolin Chen esai_priv->dma_params_rx.maxburst = 16; 103043d24e76SNicolin Chen esai_priv->dma_params_tx.addr = res->start + REG_ESAI_ETDR; 103143d24e76SNicolin Chen esai_priv->dma_params_rx.addr = res->start + REG_ESAI_ERDR; 103243d24e76SNicolin Chen 103343d24e76SNicolin Chen esai_priv->synchronous = 103443d24e76SNicolin Chen of_property_read_bool(np, "fsl,esai-synchronous"); 103543d24e76SNicolin Chen 103643d24e76SNicolin Chen /* Implement full symmetry for synchronous mode */ 103743d24e76SNicolin Chen if (esai_priv->synchronous) { 1038cb2f6927SKuninori Morimoto fsl_esai_dai.symmetric_rate = 1; 103943d24e76SNicolin Chen fsl_esai_dai.symmetric_channels = 1; 1040cb2f6927SKuninori Morimoto fsl_esai_dai.symmetric_sample_bits = 1; 104143d24e76SNicolin Chen } 104243d24e76SNicolin Chen 104343d24e76SNicolin Chen dev_set_drvdata(&pdev->dev, esai_priv); 104435dac627SShengjiu Wang spin_lock_init(&esai_priv->lock); 1045*203773e3SShengjiu Wang pm_runtime_enable(&pdev->dev); 1046*203773e3SShengjiu Wang if (!pm_runtime_enabled(&pdev->dev)) { 1047*203773e3SShengjiu Wang ret = fsl_esai_runtime_resume(&pdev->dev); 1048*203773e3SShengjiu Wang if (ret) 1049*203773e3SShengjiu Wang goto err_pm_disable; 1050*203773e3SShengjiu Wang } 1051*203773e3SShengjiu Wang 1052*203773e3SShengjiu Wang ret = pm_runtime_get_sync(&pdev->dev); 1053*203773e3SShengjiu Wang if (ret < 0) { 1054*203773e3SShengjiu Wang pm_runtime_put_noidle(&pdev->dev); 1055*203773e3SShengjiu Wang goto err_pm_get_sync; 1056*203773e3SShengjiu Wang } 1057*203773e3SShengjiu Wang 10585be6155bSShengjiu Wang ret = fsl_esai_hw_init(esai_priv); 10595be6155bSShengjiu Wang if (ret) 1060*203773e3SShengjiu Wang goto err_pm_get_sync; 106143d24e76SNicolin Chen 10620ff4e8c6SS.j. Wang esai_priv->tx_mask = 0xFFFFFFFF; 10630ff4e8c6SS.j. Wang esai_priv->rx_mask = 0xFFFFFFFF; 10640ff4e8c6SS.j. Wang 10650ff4e8c6SS.j. Wang /* Clear the TSMA, TSMB, RSMA, RSMB */ 10660ff4e8c6SS.j. Wang regmap_write(esai_priv->regmap, REG_ESAI_TSMA, 0); 10670ff4e8c6SS.j. Wang regmap_write(esai_priv->regmap, REG_ESAI_TSMB, 0); 10680ff4e8c6SS.j. Wang regmap_write(esai_priv->regmap, REG_ESAI_RSMA, 0); 10690ff4e8c6SS.j. Wang regmap_write(esai_priv->regmap, REG_ESAI_RSMB, 0); 10700ff4e8c6SS.j. Wang 1071*203773e3SShengjiu Wang ret = pm_runtime_put_sync(&pdev->dev); 1072*203773e3SShengjiu Wang if (ret < 0) 1073*203773e3SShengjiu Wang goto err_pm_get_sync; 1074*203773e3SShengjiu Wang 107543d24e76SNicolin Chen ret = devm_snd_soc_register_component(&pdev->dev, &fsl_esai_component, 107643d24e76SNicolin Chen &fsl_esai_dai, 1); 107743d24e76SNicolin Chen if (ret) { 107843d24e76SNicolin Chen dev_err(&pdev->dev, "failed to register DAI: %d\n", ret); 1079*203773e3SShengjiu Wang goto err_pm_get_sync; 108043d24e76SNicolin Chen } 108143d24e76SNicolin Chen 1082a3d1f931STakashi Iwai INIT_WORK(&esai_priv->work, fsl_esai_hw_reset); 10837ccafa2bSShengjiu Wang 10840d69e0ddSShengjiu Wang ret = imx_pcm_dma_init(pdev, IMX_ESAI_DMABUF_SIZE); 1085*203773e3SShengjiu Wang if (ret) { 108643d24e76SNicolin Chen dev_err(&pdev->dev, "failed to init imx pcm dma: %d\n", ret); 1087*203773e3SShengjiu Wang goto err_pm_get_sync; 1088*203773e3SShengjiu Wang } 108943d24e76SNicolin Chen 109043d24e76SNicolin Chen return ret; 1091*203773e3SShengjiu Wang 1092*203773e3SShengjiu Wang err_pm_get_sync: 1093*203773e3SShengjiu Wang if (!pm_runtime_status_suspended(&pdev->dev)) 1094*203773e3SShengjiu Wang fsl_esai_runtime_suspend(&pdev->dev); 1095*203773e3SShengjiu Wang err_pm_disable: 1096*203773e3SShengjiu Wang pm_runtime_disable(&pdev->dev); 1097*203773e3SShengjiu Wang return ret; 109843d24e76SNicolin Chen } 109943d24e76SNicolin Chen 1100b2d337d8SS.j. Wang static int fsl_esai_remove(struct platform_device *pdev) 1101b2d337d8SS.j. Wang { 11027ccafa2bSShengjiu Wang struct fsl_esai *esai_priv = platform_get_drvdata(pdev); 11037ccafa2bSShengjiu Wang 1104b2d337d8SS.j. Wang pm_runtime_disable(&pdev->dev); 1105*203773e3SShengjiu Wang if (!pm_runtime_status_suspended(&pdev->dev)) 1106*203773e3SShengjiu Wang fsl_esai_runtime_suspend(&pdev->dev); 1107*203773e3SShengjiu Wang 1108a3d1f931STakashi Iwai cancel_work_sync(&esai_priv->work); 1109b2d337d8SS.j. Wang 1110b2d337d8SS.j. Wang return 0; 1111b2d337d8SS.j. Wang } 1112b2d337d8SS.j. Wang 111343d24e76SNicolin Chen static const struct of_device_id fsl_esai_dt_ids[] = { 11146878e752SShengjiu Wang { .compatible = "fsl,imx35-esai", .data = &fsl_esai_imx35 }, 11156878e752SShengjiu Wang { .compatible = "fsl,vf610-esai", .data = &fsl_esai_vf610 }, 11166878e752SShengjiu Wang { .compatible = "fsl,imx6ull-esai", .data = &fsl_esai_imx6ull }, 111743d24e76SNicolin Chen {} 111843d24e76SNicolin Chen }; 111943d24e76SNicolin Chen MODULE_DEVICE_TABLE(of, fsl_esai_dt_ids); 112043d24e76SNicolin Chen 1121b2d337d8SS.j. Wang static int fsl_esai_runtime_resume(struct device *dev) 1122c64c6076SZidan Wang { 1123c64c6076SZidan Wang struct fsl_esai *esai = dev_get_drvdata(dev); 1124c64c6076SZidan Wang int ret; 1125c64c6076SZidan Wang 1126b2d337d8SS.j. Wang /* 1127b2d337d8SS.j. Wang * Some platforms might use the same bit to gate all three or two of 1128b2d337d8SS.j. Wang * clocks, so keep all clocks open/close at the same time for safety 1129b2d337d8SS.j. Wang */ 1130b2d337d8SS.j. Wang ret = clk_prepare_enable(esai->coreclk); 1131b2d337d8SS.j. Wang if (ret) 1132b2d337d8SS.j. Wang return ret; 1133b2d337d8SS.j. Wang if (!IS_ERR(esai->spbaclk)) { 1134b2d337d8SS.j. Wang ret = clk_prepare_enable(esai->spbaclk); 1135b2d337d8SS.j. Wang if (ret) 1136b2d337d8SS.j. Wang goto err_spbaclk; 1137b2d337d8SS.j. Wang } 1138b2d337d8SS.j. Wang if (!IS_ERR(esai->extalclk)) { 1139b2d337d8SS.j. Wang ret = clk_prepare_enable(esai->extalclk); 1140b2d337d8SS.j. Wang if (ret) 1141b2d337d8SS.j. Wang goto err_extalclk; 1142b2d337d8SS.j. Wang } 1143b2d337d8SS.j. Wang if (!IS_ERR(esai->fsysclk)) { 1144b2d337d8SS.j. Wang ret = clk_prepare_enable(esai->fsysclk); 1145b2d337d8SS.j. Wang if (ret) 1146b2d337d8SS.j. Wang goto err_fsysclk; 1147b2d337d8SS.j. Wang } 1148b2d337d8SS.j. Wang 1149c64c6076SZidan Wang regcache_cache_only(esai->regmap, false); 1150c64c6076SZidan Wang 11515be6155bSShengjiu Wang ret = fsl_esai_register_restore(esai); 1152c64c6076SZidan Wang if (ret) 1153b2d337d8SS.j. Wang goto err_regcache_sync; 1154c64c6076SZidan Wang 1155c64c6076SZidan Wang return 0; 1156b2d337d8SS.j. Wang 1157b2d337d8SS.j. Wang err_regcache_sync: 1158b2d337d8SS.j. Wang if (!IS_ERR(esai->fsysclk)) 1159b2d337d8SS.j. Wang clk_disable_unprepare(esai->fsysclk); 1160b2d337d8SS.j. Wang err_fsysclk: 1161b2d337d8SS.j. Wang if (!IS_ERR(esai->extalclk)) 1162b2d337d8SS.j. Wang clk_disable_unprepare(esai->extalclk); 1163b2d337d8SS.j. Wang err_extalclk: 1164b2d337d8SS.j. Wang if (!IS_ERR(esai->spbaclk)) 1165b2d337d8SS.j. Wang clk_disable_unprepare(esai->spbaclk); 1166b2d337d8SS.j. Wang err_spbaclk: 1167b2d337d8SS.j. Wang clk_disable_unprepare(esai->coreclk); 1168b2d337d8SS.j. Wang 1169b2d337d8SS.j. Wang return ret; 1170c64c6076SZidan Wang } 1171b2d337d8SS.j. Wang 1172b2d337d8SS.j. Wang static int fsl_esai_runtime_suspend(struct device *dev) 1173b2d337d8SS.j. Wang { 1174b2d337d8SS.j. Wang struct fsl_esai *esai = dev_get_drvdata(dev); 1175b2d337d8SS.j. Wang 1176b2d337d8SS.j. Wang regcache_cache_only(esai->regmap, true); 1177b2d337d8SS.j. Wang 1178b2d337d8SS.j. Wang if (!IS_ERR(esai->fsysclk)) 1179b2d337d8SS.j. Wang clk_disable_unprepare(esai->fsysclk); 1180b2d337d8SS.j. Wang if (!IS_ERR(esai->extalclk)) 1181b2d337d8SS.j. Wang clk_disable_unprepare(esai->extalclk); 1182b2d337d8SS.j. Wang if (!IS_ERR(esai->spbaclk)) 1183b2d337d8SS.j. Wang clk_disable_unprepare(esai->spbaclk); 1184b2d337d8SS.j. Wang clk_disable_unprepare(esai->coreclk); 1185b2d337d8SS.j. Wang 1186b2d337d8SS.j. Wang return 0; 1187b2d337d8SS.j. Wang } 1188c64c6076SZidan Wang 1189c64c6076SZidan Wang static const struct dev_pm_ops fsl_esai_pm_ops = { 1190b2d337d8SS.j. Wang SET_RUNTIME_PM_OPS(fsl_esai_runtime_suspend, 1191b2d337d8SS.j. Wang fsl_esai_runtime_resume, 1192b2d337d8SS.j. Wang NULL) 1193b2d337d8SS.j. Wang SET_SYSTEM_SLEEP_PM_OPS(pm_runtime_force_suspend, 1194b2d337d8SS.j. Wang pm_runtime_force_resume) 1195c64c6076SZidan Wang }; 1196c64c6076SZidan Wang 119743d24e76SNicolin Chen static struct platform_driver fsl_esai_driver = { 119843d24e76SNicolin Chen .probe = fsl_esai_probe, 1199b2d337d8SS.j. Wang .remove = fsl_esai_remove, 120043d24e76SNicolin Chen .driver = { 120143d24e76SNicolin Chen .name = "fsl-esai-dai", 1202c64c6076SZidan Wang .pm = &fsl_esai_pm_ops, 120343d24e76SNicolin Chen .of_match_table = fsl_esai_dt_ids, 120443d24e76SNicolin Chen }, 120543d24e76SNicolin Chen }; 120643d24e76SNicolin Chen 120743d24e76SNicolin Chen module_platform_driver(fsl_esai_driver); 120843d24e76SNicolin Chen 120943d24e76SNicolin Chen MODULE_AUTHOR("Freescale Semiconductor, Inc."); 121043d24e76SNicolin Chen MODULE_DESCRIPTION("Freescale ESAI CPU DAI driver"); 121143d24e76SNicolin Chen MODULE_LICENSE("GPL v2"); 121243d24e76SNicolin Chen MODULE_ALIAS("platform:fsl-esai-dai"); 1213