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 55e0b64fa3SMark Brown * @consumer_mode: if fully using DAI clock consumer 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]; 81e0b64fa3SMark Brown bool consumer_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 369e0b64fa3SMark Brown /* Don't apply for fully consumer mode or unchanged bclk */ 370e0b64fa3SMark Brown if (esai_priv->consumer_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 479e0b64fa3SMark Brown esai_priv->consumer_mode = false; 48043d24e76SNicolin Chen 481e0b64fa3SMark Brown /* DAI clock provider masks */ 482e0b64fa3SMark Brown switch (fmt & SND_SOC_DAIFMT_CLOCK_PROVIDER_MASK) { 483e0b64fa3SMark Brown case SND_SOC_DAIFMT_CBP_CFP: 484e0b64fa3SMark Brown esai_priv->consumer_mode = true; 48543d24e76SNicolin Chen break; 486e0b64fa3SMark Brown case SND_SOC_DAIFMT_CBC_CFP: 48743d24e76SNicolin Chen xccr |= ESAI_xCCR_xCKD; 48843d24e76SNicolin Chen break; 489e0b64fa3SMark Brown case SND_SOC_DAIFMT_CBP_CFC: 49043d24e76SNicolin Chen xccr |= ESAI_xCCR_xFSD; 49143d24e76SNicolin Chen break; 492e0b64fa3SMark Brown case SND_SOC_DAIFMT_CBC_CFC: 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 522e7a48c71SAlexander Shiyan /* Set slots count */ 52343d24e76SNicolin Chen regmap_update_bits(esai_priv->regmap, REG_ESAI_TCCR, 524e7a48c71SAlexander Shiyan ESAI_xCCR_xDC_MASK, 525e7a48c71SAlexander Shiyan ESAI_xCCR_xDC(esai_priv->slots)); 52643d24e76SNicolin Chen regmap_update_bits(esai_priv->regmap, REG_ESAI_RCCR, 527e7a48c71SAlexander Shiyan ESAI_xCCR_xDC_MASK, 528e7a48c71SAlexander Shiyan ESAI_xCCR_xDC(esai_priv->slots)); 52943d24e76SNicolin Chen } 53043d24e76SNicolin Chen 53143d24e76SNicolin Chen return 0; 53233529ec9SFabio Estevam 53343d24e76SNicolin Chen } 53443d24e76SNicolin Chen 53543d24e76SNicolin Chen static int fsl_esai_hw_params(struct snd_pcm_substream *substream, 53643d24e76SNicolin Chen struct snd_pcm_hw_params *params, 53743d24e76SNicolin Chen struct snd_soc_dai *dai) 53843d24e76SNicolin Chen { 53943d24e76SNicolin Chen struct fsl_esai *esai_priv = snd_soc_dai_get_drvdata(dai); 54043d24e76SNicolin Chen bool tx = substream->stream == SNDRV_PCM_STREAM_PLAYBACK; 5414ca73043SZidan Wang u32 width = params_width(params); 54243d24e76SNicolin Chen u32 channels = params_channels(params); 543de0d712aSShengjiu Wang u32 pins = DIV_ROUND_UP(channels, esai_priv->slots); 54486ea522bSNicolin Chen u32 slot_width = width; 5453e185238SXiubo Li u32 bclk, mask, val; 5463e185238SXiubo Li int ret; 54743d24e76SNicolin Chen 548d8ffcf71SGeert Uytterhoeven /* Override slot_width if being specifically set */ 54986ea522bSNicolin Chen if (esai_priv->slot_width) 55086ea522bSNicolin Chen slot_width = esai_priv->slot_width; 55186ea522bSNicolin Chen 55286ea522bSNicolin Chen bclk = params_rate(params) * slot_width * esai_priv->slots; 55343d24e76SNicolin Chen 5541997ee89SS.j. Wang ret = fsl_esai_set_bclk(dai, esai_priv->synchronous || tx, bclk); 55543d24e76SNicolin Chen if (ret) 55643d24e76SNicolin Chen return ret; 55743d24e76SNicolin Chen 5581997ee89SS.j. Wang mask = ESAI_xCR_xSWS_MASK; 5591997ee89SS.j. Wang val = ESAI_xCR_xSWS(slot_width, width); 5601997ee89SS.j. Wang 5611997ee89SS.j. Wang regmap_update_bits(esai_priv->regmap, REG_ESAI_xCR(tx), mask, val); 5621997ee89SS.j. Wang /* Recording in synchronous mode needs to set TCR also */ 5631997ee89SS.j. Wang if (!tx && esai_priv->synchronous) 5641997ee89SS.j. Wang regmap_update_bits(esai_priv->regmap, REG_ESAI_TCR, mask, val); 5651997ee89SS.j. Wang 56643d24e76SNicolin Chen /* Use Normal mode to support monaural audio */ 56743d24e76SNicolin Chen regmap_update_bits(esai_priv->regmap, REG_ESAI_xCR(tx), 56843d24e76SNicolin Chen ESAI_xCR_xMOD_MASK, params_channels(params) > 1 ? 56943d24e76SNicolin Chen ESAI_xCR_xMOD_NETWORK : 0); 57043d24e76SNicolin Chen 57143d24e76SNicolin Chen regmap_update_bits(esai_priv->regmap, REG_ESAI_xFCR(tx), 57243d24e76SNicolin Chen ESAI_xFCR_xFR_MASK, ESAI_xFCR_xFR); 57343d24e76SNicolin Chen 57443d24e76SNicolin Chen mask = ESAI_xFCR_xFR_MASK | ESAI_xFCR_xWA_MASK | ESAI_xFCR_xFWM_MASK | 57543d24e76SNicolin Chen (tx ? ESAI_xFCR_TE_MASK | ESAI_xFCR_TIEN : ESAI_xFCR_RE_MASK); 57643d24e76SNicolin Chen val = ESAI_xFCR_xWA(width) | ESAI_xFCR_xFWM(esai_priv->fifo_depth) | 577de0d712aSShengjiu Wang (tx ? ESAI_xFCR_TE(pins) | ESAI_xFCR_TIEN : ESAI_xFCR_RE(pins)); 57843d24e76SNicolin Chen 57943d24e76SNicolin Chen regmap_update_bits(esai_priv->regmap, REG_ESAI_xFCR(tx), mask, val); 58043d24e76SNicolin Chen 5811997ee89SS.j. Wang if (tx) 5821997ee89SS.j. Wang regmap_update_bits(esai_priv->regmap, REG_ESAI_TCR, 5831997ee89SS.j. Wang ESAI_xCR_PADC, ESAI_xCR_PADC); 58443d24e76SNicolin Chen 5854f8210f6SNicolin Chen /* Remove ESAI personal reset by configuring ESAI_PCRC and ESAI_PRRC */ 5864f8210f6SNicolin Chen regmap_update_bits(esai_priv->regmap, REG_ESAI_PRRC, 5874f8210f6SNicolin Chen ESAI_PRRC_PDC_MASK, ESAI_PRRC_PDC(ESAI_GPIO)); 5884f8210f6SNicolin Chen regmap_update_bits(esai_priv->regmap, REG_ESAI_PCRC, 5894f8210f6SNicolin Chen ESAI_PCRC_PC_MASK, ESAI_PCRC_PC(ESAI_GPIO)); 59043d24e76SNicolin Chen return 0; 59143d24e76SNicolin Chen } 59243d24e76SNicolin Chen 5935be6155bSShengjiu Wang static int fsl_esai_hw_init(struct fsl_esai *esai_priv) 59443d24e76SNicolin Chen { 5955be6155bSShengjiu Wang struct platform_device *pdev = esai_priv->pdev; 5965be6155bSShengjiu Wang int ret; 5975be6155bSShengjiu Wang 5985be6155bSShengjiu Wang /* Reset ESAI unit */ 5995be6155bSShengjiu Wang ret = regmap_update_bits(esai_priv->regmap, REG_ESAI_ECR, 6005be6155bSShengjiu Wang ESAI_ECR_ESAIEN_MASK | ESAI_ECR_ERST_MASK, 6015be6155bSShengjiu Wang ESAI_ECR_ESAIEN | ESAI_ECR_ERST); 6025be6155bSShengjiu Wang if (ret) { 6035be6155bSShengjiu Wang dev_err(&pdev->dev, "failed to reset ESAI: %d\n", ret); 6045be6155bSShengjiu Wang return ret; 6055be6155bSShengjiu Wang } 6065be6155bSShengjiu Wang 6075be6155bSShengjiu Wang /* 6085be6155bSShengjiu Wang * We need to enable ESAI so as to access some of its registers. 6095be6155bSShengjiu Wang * Otherwise, we would fail to dump regmap from user space. 6105be6155bSShengjiu Wang */ 6115be6155bSShengjiu Wang ret = regmap_update_bits(esai_priv->regmap, REG_ESAI_ECR, 6125be6155bSShengjiu Wang ESAI_ECR_ESAIEN_MASK | ESAI_ECR_ERST_MASK, 6135be6155bSShengjiu Wang ESAI_ECR_ESAIEN); 6145be6155bSShengjiu Wang if (ret) { 6155be6155bSShengjiu Wang dev_err(&pdev->dev, "failed to enable ESAI: %d\n", ret); 6165be6155bSShengjiu Wang return ret; 6175be6155bSShengjiu Wang } 6185be6155bSShengjiu Wang 6195be6155bSShengjiu Wang regmap_update_bits(esai_priv->regmap, REG_ESAI_PRRC, 6205be6155bSShengjiu Wang ESAI_PRRC_PDC_MASK, 0); 6215be6155bSShengjiu Wang regmap_update_bits(esai_priv->regmap, REG_ESAI_PCRC, 6225be6155bSShengjiu Wang ESAI_PCRC_PC_MASK, 0); 6235be6155bSShengjiu Wang 6245be6155bSShengjiu Wang return 0; 6255be6155bSShengjiu Wang } 6265be6155bSShengjiu Wang 6275be6155bSShengjiu Wang static int fsl_esai_register_restore(struct fsl_esai *esai_priv) 6285be6155bSShengjiu Wang { 6295be6155bSShengjiu Wang int ret; 6305be6155bSShengjiu Wang 6315be6155bSShengjiu Wang /* FIFO reset for safety */ 6325be6155bSShengjiu Wang regmap_update_bits(esai_priv->regmap, REG_ESAI_TFCR, 6335be6155bSShengjiu Wang ESAI_xFCR_xFR, ESAI_xFCR_xFR); 6345be6155bSShengjiu Wang regmap_update_bits(esai_priv->regmap, REG_ESAI_RFCR, 6355be6155bSShengjiu Wang ESAI_xFCR_xFR, ESAI_xFCR_xFR); 6365be6155bSShengjiu Wang 6375be6155bSShengjiu Wang regcache_mark_dirty(esai_priv->regmap); 6385be6155bSShengjiu Wang ret = regcache_sync(esai_priv->regmap); 6395be6155bSShengjiu Wang if (ret) 6405be6155bSShengjiu Wang return ret; 6415be6155bSShengjiu Wang 6425be6155bSShengjiu Wang /* FIFO reset done */ 6435be6155bSShengjiu Wang regmap_update_bits(esai_priv->regmap, REG_ESAI_TFCR, ESAI_xFCR_xFR, 0); 6445be6155bSShengjiu Wang regmap_update_bits(esai_priv->regmap, REG_ESAI_RFCR, ESAI_xFCR_xFR, 0); 6455be6155bSShengjiu Wang 6465be6155bSShengjiu Wang return 0; 6475be6155bSShengjiu Wang } 6485be6155bSShengjiu Wang 6495be6155bSShengjiu Wang static void fsl_esai_trigger_start(struct fsl_esai *esai_priv, bool tx) 6505be6155bSShengjiu Wang { 6515be6155bSShengjiu Wang u8 i, channels = esai_priv->channels[tx]; 652de0d712aSShengjiu Wang u32 pins = DIV_ROUND_UP(channels, esai_priv->slots); 6530ff4e8c6SS.j. Wang u32 mask; 65443d24e76SNicolin Chen 65543d24e76SNicolin Chen regmap_update_bits(esai_priv->regmap, REG_ESAI_xFCR(tx), 65643d24e76SNicolin Chen ESAI_xFCR_xFEN_MASK, ESAI_xFCR_xFEN); 65743d24e76SNicolin Chen 65843d24e76SNicolin Chen /* Write initial words reqiured by ESAI as normal procedure */ 65943d24e76SNicolin Chen for (i = 0; tx && i < channels; i++) 66043d24e76SNicolin Chen regmap_write(esai_priv->regmap, REG_ESAI_ETDR, 0x0); 66143d24e76SNicolin Chen 6620ff4e8c6SS.j. Wang /* 6630ff4e8c6SS.j. Wang * When set the TE/RE in the end of enablement flow, there 6640ff4e8c6SS.j. Wang * will be channel swap issue for multi data line case. 6650ff4e8c6SS.j. Wang * In order to workaround this issue, we switch the bit 6660ff4e8c6SS.j. Wang * enablement sequence to below sequence 6670ff4e8c6SS.j. Wang * 1) clear the xSMB & xSMA: which is done in probe and 6680ff4e8c6SS.j. Wang * stop state. 6690ff4e8c6SS.j. Wang * 2) set TE/RE 6700ff4e8c6SS.j. Wang * 3) set xSMB 6710ff4e8c6SS.j. Wang * 4) set xSMA: xSMA is the last one in this flow, which 6720ff4e8c6SS.j. Wang * will trigger esai to start. 6730ff4e8c6SS.j. Wang */ 67443d24e76SNicolin Chen regmap_update_bits(esai_priv->regmap, REG_ESAI_xCR(tx), 67543d24e76SNicolin Chen tx ? ESAI_xCR_TE_MASK : ESAI_xCR_RE_MASK, 676de0d712aSShengjiu Wang tx ? ESAI_xCR_TE(pins) : ESAI_xCR_RE(pins)); 6770ff4e8c6SS.j. Wang mask = tx ? esai_priv->tx_mask : esai_priv->rx_mask; 6780ff4e8c6SS.j. Wang 6790ff4e8c6SS.j. Wang regmap_update_bits(esai_priv->regmap, REG_ESAI_xSMB(tx), 6800ff4e8c6SS.j. Wang ESAI_xSMB_xS_MASK, ESAI_xSMB_xS(mask)); 6810ff4e8c6SS.j. Wang regmap_update_bits(esai_priv->regmap, REG_ESAI_xSMA(tx), 6820ff4e8c6SS.j. Wang ESAI_xSMA_xS_MASK, ESAI_xSMA_xS(mask)); 6837ccafa2bSShengjiu Wang 6847ccafa2bSShengjiu Wang /* Enable Exception interrupt */ 6857ccafa2bSShengjiu Wang regmap_update_bits(esai_priv->regmap, REG_ESAI_xCR(tx), 6867ccafa2bSShengjiu Wang ESAI_xCR_xEIE_MASK, ESAI_xCR_xEIE); 6875be6155bSShengjiu Wang } 6880ff4e8c6SS.j. Wang 6895be6155bSShengjiu Wang static void fsl_esai_trigger_stop(struct fsl_esai *esai_priv, bool tx) 6905be6155bSShengjiu Wang { 69143d24e76SNicolin Chen regmap_update_bits(esai_priv->regmap, REG_ESAI_xCR(tx), 6927ccafa2bSShengjiu Wang ESAI_xCR_xEIE_MASK, 0); 6937ccafa2bSShengjiu Wang 6947ccafa2bSShengjiu Wang regmap_update_bits(esai_priv->regmap, REG_ESAI_xCR(tx), 69543d24e76SNicolin Chen tx ? ESAI_xCR_TE_MASK : ESAI_xCR_RE_MASK, 0); 6960ff4e8c6SS.j. Wang regmap_update_bits(esai_priv->regmap, REG_ESAI_xSMA(tx), 6970ff4e8c6SS.j. Wang ESAI_xSMA_xS_MASK, 0); 6980ff4e8c6SS.j. Wang regmap_update_bits(esai_priv->regmap, REG_ESAI_xSMB(tx), 6990ff4e8c6SS.j. Wang ESAI_xSMB_xS_MASK, 0); 70043d24e76SNicolin Chen 70143d24e76SNicolin Chen /* Disable and reset FIFO */ 70243d24e76SNicolin Chen regmap_update_bits(esai_priv->regmap, REG_ESAI_xFCR(tx), 70343d24e76SNicolin Chen ESAI_xFCR_xFR | ESAI_xFCR_xFEN, ESAI_xFCR_xFR); 70443d24e76SNicolin Chen regmap_update_bits(esai_priv->regmap, REG_ESAI_xFCR(tx), 70543d24e76SNicolin Chen ESAI_xFCR_xFR, 0); 7065be6155bSShengjiu Wang } 7075be6155bSShengjiu Wang 708a3d1f931STakashi Iwai static void fsl_esai_hw_reset(struct work_struct *work) 7097ccafa2bSShengjiu Wang { 710a3d1f931STakashi Iwai struct fsl_esai *esai_priv = container_of(work, struct fsl_esai, work); 7117ccafa2bSShengjiu Wang bool tx = true, rx = false, enabled[2]; 71235dac627SShengjiu Wang unsigned long lock_flags; 7137ccafa2bSShengjiu Wang u32 tfcr, rfcr; 7147ccafa2bSShengjiu Wang 71535dac627SShengjiu Wang spin_lock_irqsave(&esai_priv->lock, lock_flags); 7167ccafa2bSShengjiu Wang /* Save the registers */ 7177ccafa2bSShengjiu Wang regmap_read(esai_priv->regmap, REG_ESAI_TFCR, &tfcr); 7187ccafa2bSShengjiu Wang regmap_read(esai_priv->regmap, REG_ESAI_RFCR, &rfcr); 7197ccafa2bSShengjiu Wang enabled[tx] = tfcr & ESAI_xFCR_xFEN; 7207ccafa2bSShengjiu Wang enabled[rx] = rfcr & ESAI_xFCR_xFEN; 7217ccafa2bSShengjiu Wang 7227ccafa2bSShengjiu Wang /* Stop the tx & rx */ 7237ccafa2bSShengjiu Wang fsl_esai_trigger_stop(esai_priv, tx); 7247ccafa2bSShengjiu Wang fsl_esai_trigger_stop(esai_priv, rx); 7257ccafa2bSShengjiu Wang 7267ccafa2bSShengjiu Wang /* Reset the esai, and ignore return value */ 7277ccafa2bSShengjiu Wang fsl_esai_hw_init(esai_priv); 7287ccafa2bSShengjiu Wang 7297ccafa2bSShengjiu Wang /* Enforce ESAI personal resets for both TX and RX */ 7307ccafa2bSShengjiu Wang regmap_update_bits(esai_priv->regmap, REG_ESAI_TCR, 7317ccafa2bSShengjiu Wang ESAI_xCR_xPR_MASK, ESAI_xCR_xPR); 7327ccafa2bSShengjiu Wang regmap_update_bits(esai_priv->regmap, REG_ESAI_RCR, 7337ccafa2bSShengjiu Wang ESAI_xCR_xPR_MASK, ESAI_xCR_xPR); 7347ccafa2bSShengjiu Wang 7357ccafa2bSShengjiu Wang /* Restore registers by regcache_sync, and ignore return value */ 7367ccafa2bSShengjiu Wang fsl_esai_register_restore(esai_priv); 7377ccafa2bSShengjiu Wang 7387ccafa2bSShengjiu Wang /* Remove ESAI personal resets by configuring PCRC and PRRC also */ 7397ccafa2bSShengjiu Wang regmap_update_bits(esai_priv->regmap, REG_ESAI_TCR, 7407ccafa2bSShengjiu Wang ESAI_xCR_xPR_MASK, 0); 7417ccafa2bSShengjiu Wang regmap_update_bits(esai_priv->regmap, REG_ESAI_RCR, 7427ccafa2bSShengjiu Wang ESAI_xCR_xPR_MASK, 0); 7437ccafa2bSShengjiu Wang regmap_update_bits(esai_priv->regmap, REG_ESAI_PRRC, 7447ccafa2bSShengjiu Wang ESAI_PRRC_PDC_MASK, ESAI_PRRC_PDC(ESAI_GPIO)); 7457ccafa2bSShengjiu Wang regmap_update_bits(esai_priv->regmap, REG_ESAI_PCRC, 7467ccafa2bSShengjiu Wang ESAI_PCRC_PC_MASK, ESAI_PCRC_PC(ESAI_GPIO)); 7477ccafa2bSShengjiu Wang 7487ccafa2bSShengjiu Wang /* Restart tx / rx, if they already enabled */ 7497ccafa2bSShengjiu Wang if (enabled[tx]) 7507ccafa2bSShengjiu Wang fsl_esai_trigger_start(esai_priv, tx); 7517ccafa2bSShengjiu Wang if (enabled[rx]) 7527ccafa2bSShengjiu Wang fsl_esai_trigger_start(esai_priv, rx); 75335dac627SShengjiu Wang 75435dac627SShengjiu Wang spin_unlock_irqrestore(&esai_priv->lock, lock_flags); 7557ccafa2bSShengjiu Wang } 7567ccafa2bSShengjiu Wang 7575be6155bSShengjiu Wang static int fsl_esai_trigger(struct snd_pcm_substream *substream, int cmd, 7585be6155bSShengjiu Wang struct snd_soc_dai *dai) 7595be6155bSShengjiu Wang { 7605be6155bSShengjiu Wang struct fsl_esai *esai_priv = snd_soc_dai_get_drvdata(dai); 7615be6155bSShengjiu Wang bool tx = substream->stream == SNDRV_PCM_STREAM_PLAYBACK; 76235dac627SShengjiu Wang unsigned long lock_flags; 7635be6155bSShengjiu Wang 7645be6155bSShengjiu Wang esai_priv->channels[tx] = substream->runtime->channels; 7655be6155bSShengjiu Wang 7665be6155bSShengjiu Wang switch (cmd) { 7675be6155bSShengjiu Wang case SNDRV_PCM_TRIGGER_START: 7685be6155bSShengjiu Wang case SNDRV_PCM_TRIGGER_RESUME: 7695be6155bSShengjiu Wang case SNDRV_PCM_TRIGGER_PAUSE_RELEASE: 77035dac627SShengjiu Wang spin_lock_irqsave(&esai_priv->lock, lock_flags); 7715be6155bSShengjiu Wang fsl_esai_trigger_start(esai_priv, tx); 77235dac627SShengjiu Wang spin_unlock_irqrestore(&esai_priv->lock, lock_flags); 7735be6155bSShengjiu Wang break; 7745be6155bSShengjiu Wang case SNDRV_PCM_TRIGGER_SUSPEND: 7755be6155bSShengjiu Wang case SNDRV_PCM_TRIGGER_STOP: 7765be6155bSShengjiu Wang case SNDRV_PCM_TRIGGER_PAUSE_PUSH: 77735dac627SShengjiu Wang spin_lock_irqsave(&esai_priv->lock, lock_flags); 7785be6155bSShengjiu Wang fsl_esai_trigger_stop(esai_priv, tx); 77935dac627SShengjiu Wang spin_unlock_irqrestore(&esai_priv->lock, lock_flags); 78043d24e76SNicolin Chen break; 78143d24e76SNicolin Chen default: 78243d24e76SNicolin Chen return -EINVAL; 78343d24e76SNicolin Chen } 78443d24e76SNicolin Chen 78543d24e76SNicolin Chen return 0; 78643d24e76SNicolin Chen } 78743d24e76SNicolin Chen 7885d29e95eSGustavo A. R. Silva static const struct snd_soc_dai_ops fsl_esai_dai_ops = { 78943d24e76SNicolin Chen .startup = fsl_esai_startup, 79043d24e76SNicolin Chen .trigger = fsl_esai_trigger, 79143d24e76SNicolin Chen .hw_params = fsl_esai_hw_params, 79243d24e76SNicolin Chen .set_sysclk = fsl_esai_set_dai_sysclk, 79343d24e76SNicolin Chen .set_fmt = fsl_esai_set_dai_fmt, 79443d24e76SNicolin Chen .set_tdm_slot = fsl_esai_set_dai_tdm_slot, 79543d24e76SNicolin Chen }; 79643d24e76SNicolin Chen 79743d24e76SNicolin Chen static int fsl_esai_dai_probe(struct snd_soc_dai *dai) 79843d24e76SNicolin Chen { 79943d24e76SNicolin Chen struct fsl_esai *esai_priv = snd_soc_dai_get_drvdata(dai); 80043d24e76SNicolin Chen 80143d24e76SNicolin Chen snd_soc_dai_init_dma_data(dai, &esai_priv->dma_params_tx, 80243d24e76SNicolin Chen &esai_priv->dma_params_rx); 80343d24e76SNicolin Chen 80443d24e76SNicolin Chen return 0; 80543d24e76SNicolin Chen } 80643d24e76SNicolin Chen 80743d24e76SNicolin Chen static struct snd_soc_dai_driver fsl_esai_dai = { 80843d24e76SNicolin Chen .probe = fsl_esai_dai_probe, 80943d24e76SNicolin Chen .playback = { 81074ccb27cSNicolin Chen .stream_name = "CPU-Playback", 81143d24e76SNicolin Chen .channels_min = 1, 81243d24e76SNicolin Chen .channels_max = 12, 813f2a3ee01SFabio Estevam .rates = SNDRV_PCM_RATE_8000_192000, 81443d24e76SNicolin Chen .formats = FSL_ESAI_FORMATS, 81543d24e76SNicolin Chen }, 81643d24e76SNicolin Chen .capture = { 81774ccb27cSNicolin Chen .stream_name = "CPU-Capture", 81843d24e76SNicolin Chen .channels_min = 1, 81943d24e76SNicolin Chen .channels_max = 8, 820f2a3ee01SFabio Estevam .rates = SNDRV_PCM_RATE_8000_192000, 82143d24e76SNicolin Chen .formats = FSL_ESAI_FORMATS, 82243d24e76SNicolin Chen }, 82343d24e76SNicolin Chen .ops = &fsl_esai_dai_ops, 82443d24e76SNicolin Chen }; 82543d24e76SNicolin Chen 82643d24e76SNicolin Chen static const struct snd_soc_component_driver fsl_esai_component = { 82743d24e76SNicolin Chen .name = "fsl-esai", 82843d24e76SNicolin Chen }; 82943d24e76SNicolin Chen 830c64c6076SZidan Wang static const struct reg_default fsl_esai_reg_defaults[] = { 8318973112aSZidan Wang {REG_ESAI_ETDR, 0x00000000}, 8328973112aSZidan Wang {REG_ESAI_ECR, 0x00000000}, 8338973112aSZidan Wang {REG_ESAI_TFCR, 0x00000000}, 8348973112aSZidan Wang {REG_ESAI_RFCR, 0x00000000}, 8358973112aSZidan Wang {REG_ESAI_TX0, 0x00000000}, 8368973112aSZidan Wang {REG_ESAI_TX1, 0x00000000}, 8378973112aSZidan Wang {REG_ESAI_TX2, 0x00000000}, 8388973112aSZidan Wang {REG_ESAI_TX3, 0x00000000}, 8398973112aSZidan Wang {REG_ESAI_TX4, 0x00000000}, 8408973112aSZidan Wang {REG_ESAI_TX5, 0x00000000}, 8418973112aSZidan Wang {REG_ESAI_TSR, 0x00000000}, 8428973112aSZidan Wang {REG_ESAI_SAICR, 0x00000000}, 8438973112aSZidan Wang {REG_ESAI_TCR, 0x00000000}, 8448973112aSZidan Wang {REG_ESAI_TCCR, 0x00000000}, 8458973112aSZidan Wang {REG_ESAI_RCR, 0x00000000}, 8468973112aSZidan Wang {REG_ESAI_RCCR, 0x00000000}, 8478973112aSZidan Wang {REG_ESAI_TSMA, 0x0000ffff}, 8488973112aSZidan Wang {REG_ESAI_TSMB, 0x0000ffff}, 8498973112aSZidan Wang {REG_ESAI_RSMA, 0x0000ffff}, 8508973112aSZidan Wang {REG_ESAI_RSMB, 0x0000ffff}, 8518973112aSZidan Wang {REG_ESAI_PRRC, 0x00000000}, 8528973112aSZidan Wang {REG_ESAI_PCRC, 0x00000000}, 853c64c6076SZidan Wang }; 854c64c6076SZidan Wang 85543d24e76SNicolin Chen static bool fsl_esai_readable_reg(struct device *dev, unsigned int reg) 85643d24e76SNicolin Chen { 85743d24e76SNicolin Chen switch (reg) { 85843d24e76SNicolin Chen case REG_ESAI_ERDR: 85943d24e76SNicolin Chen case REG_ESAI_ECR: 86043d24e76SNicolin Chen case REG_ESAI_ESR: 86143d24e76SNicolin Chen case REG_ESAI_TFCR: 86243d24e76SNicolin Chen case REG_ESAI_TFSR: 86343d24e76SNicolin Chen case REG_ESAI_RFCR: 86443d24e76SNicolin Chen case REG_ESAI_RFSR: 86543d24e76SNicolin Chen case REG_ESAI_RX0: 86643d24e76SNicolin Chen case REG_ESAI_RX1: 86743d24e76SNicolin Chen case REG_ESAI_RX2: 86843d24e76SNicolin Chen case REG_ESAI_RX3: 86943d24e76SNicolin Chen case REG_ESAI_SAISR: 87043d24e76SNicolin Chen case REG_ESAI_SAICR: 87143d24e76SNicolin Chen case REG_ESAI_TCR: 87243d24e76SNicolin Chen case REG_ESAI_TCCR: 87343d24e76SNicolin Chen case REG_ESAI_RCR: 87443d24e76SNicolin Chen case REG_ESAI_RCCR: 87543d24e76SNicolin Chen case REG_ESAI_TSMA: 87643d24e76SNicolin Chen case REG_ESAI_TSMB: 87743d24e76SNicolin Chen case REG_ESAI_RSMA: 87843d24e76SNicolin Chen case REG_ESAI_RSMB: 87943d24e76SNicolin Chen case REG_ESAI_PRRC: 88043d24e76SNicolin Chen case REG_ESAI_PCRC: 88143d24e76SNicolin Chen return true; 88243d24e76SNicolin Chen default: 88343d24e76SNicolin Chen return false; 88443d24e76SNicolin Chen } 88543d24e76SNicolin Chen } 88643d24e76SNicolin Chen 887c64c6076SZidan Wang static bool fsl_esai_volatile_reg(struct device *dev, unsigned int reg) 888c64c6076SZidan Wang { 889c64c6076SZidan Wang switch (reg) { 890c64c6076SZidan Wang case REG_ESAI_ERDR: 891c64c6076SZidan Wang case REG_ESAI_ESR: 892c64c6076SZidan Wang case REG_ESAI_TFSR: 893c64c6076SZidan Wang case REG_ESAI_RFSR: 894c64c6076SZidan Wang case REG_ESAI_RX0: 895c64c6076SZidan Wang case REG_ESAI_RX1: 896c64c6076SZidan Wang case REG_ESAI_RX2: 897c64c6076SZidan Wang case REG_ESAI_RX3: 898c64c6076SZidan Wang case REG_ESAI_SAISR: 899c64c6076SZidan Wang return true; 900c64c6076SZidan Wang default: 901c64c6076SZidan Wang return false; 902c64c6076SZidan Wang } 903c64c6076SZidan Wang } 904c64c6076SZidan Wang 90543d24e76SNicolin Chen static bool fsl_esai_writeable_reg(struct device *dev, unsigned int reg) 90643d24e76SNicolin Chen { 90743d24e76SNicolin Chen switch (reg) { 90843d24e76SNicolin Chen case REG_ESAI_ETDR: 90943d24e76SNicolin Chen case REG_ESAI_ECR: 91043d24e76SNicolin Chen case REG_ESAI_TFCR: 91143d24e76SNicolin Chen case REG_ESAI_RFCR: 91243d24e76SNicolin Chen case REG_ESAI_TX0: 91343d24e76SNicolin Chen case REG_ESAI_TX1: 91443d24e76SNicolin Chen case REG_ESAI_TX2: 91543d24e76SNicolin Chen case REG_ESAI_TX3: 91643d24e76SNicolin Chen case REG_ESAI_TX4: 91743d24e76SNicolin Chen case REG_ESAI_TX5: 91843d24e76SNicolin Chen case REG_ESAI_TSR: 91943d24e76SNicolin Chen case REG_ESAI_SAICR: 92043d24e76SNicolin Chen case REG_ESAI_TCR: 92143d24e76SNicolin Chen case REG_ESAI_TCCR: 92243d24e76SNicolin Chen case REG_ESAI_RCR: 92343d24e76SNicolin Chen case REG_ESAI_RCCR: 92443d24e76SNicolin Chen case REG_ESAI_TSMA: 92543d24e76SNicolin Chen case REG_ESAI_TSMB: 92643d24e76SNicolin Chen case REG_ESAI_RSMA: 92743d24e76SNicolin Chen case REG_ESAI_RSMB: 92843d24e76SNicolin Chen case REG_ESAI_PRRC: 92943d24e76SNicolin Chen case REG_ESAI_PCRC: 93043d24e76SNicolin Chen return true; 93143d24e76SNicolin Chen default: 93243d24e76SNicolin Chen return false; 93343d24e76SNicolin Chen } 93443d24e76SNicolin Chen } 93543d24e76SNicolin Chen 93692bd0334SXiubo Li static const struct regmap_config fsl_esai_regmap_config = { 93743d24e76SNicolin Chen .reg_bits = 32, 93843d24e76SNicolin Chen .reg_stride = 4, 93943d24e76SNicolin Chen .val_bits = 32, 94043d24e76SNicolin Chen 94143d24e76SNicolin Chen .max_register = REG_ESAI_PCRC, 942c64c6076SZidan Wang .reg_defaults = fsl_esai_reg_defaults, 943c64c6076SZidan Wang .num_reg_defaults = ARRAY_SIZE(fsl_esai_reg_defaults), 94443d24e76SNicolin Chen .readable_reg = fsl_esai_readable_reg, 945c64c6076SZidan Wang .volatile_reg = fsl_esai_volatile_reg, 94643d24e76SNicolin Chen .writeable_reg = fsl_esai_writeable_reg, 9470effb865SMarek Vasut .cache_type = REGCACHE_FLAT, 94843d24e76SNicolin Chen }; 94943d24e76SNicolin Chen 950203773e3SShengjiu Wang static int fsl_esai_runtime_resume(struct device *dev); 951203773e3SShengjiu Wang static int fsl_esai_runtime_suspend(struct device *dev); 952203773e3SShengjiu Wang 95343d24e76SNicolin Chen static int fsl_esai_probe(struct platform_device *pdev) 95443d24e76SNicolin Chen { 95543d24e76SNicolin Chen struct device_node *np = pdev->dev.of_node; 95643d24e76SNicolin Chen struct fsl_esai *esai_priv; 95743d24e76SNicolin Chen struct resource *res; 9580600b3e1SFabio Estevam const __be32 *iprop; 95943d24e76SNicolin Chen void __iomem *regs; 96043d24e76SNicolin Chen int irq, ret; 96143d24e76SNicolin Chen 96243d24e76SNicolin Chen esai_priv = devm_kzalloc(&pdev->dev, sizeof(*esai_priv), GFP_KERNEL); 96343d24e76SNicolin Chen if (!esai_priv) 96443d24e76SNicolin Chen return -ENOMEM; 96543d24e76SNicolin Chen 96643d24e76SNicolin Chen esai_priv->pdev = pdev; 9675d585e1eSRob Herring snprintf(esai_priv->name, sizeof(esai_priv->name), "%pOFn", np); 96843d24e76SNicolin Chen 9696878e752SShengjiu Wang esai_priv->soc = of_device_get_match_data(&pdev->dev); 9707ccafa2bSShengjiu Wang 97143d24e76SNicolin Chen /* Get the addresses and IRQ */ 972f25bb69eSYang Yingliang regs = devm_platform_get_and_ioremap_resource(pdev, 0, &res); 97343d24e76SNicolin Chen if (IS_ERR(regs)) 97443d24e76SNicolin Chen return PTR_ERR(regs); 97543d24e76SNicolin Chen 976203773e3SShengjiu Wang esai_priv->regmap = devm_regmap_init_mmio(&pdev->dev, regs, &fsl_esai_regmap_config); 97743d24e76SNicolin Chen if (IS_ERR(esai_priv->regmap)) { 97843d24e76SNicolin Chen dev_err(&pdev->dev, "failed to init regmap: %ld\n", 97943d24e76SNicolin Chen PTR_ERR(esai_priv->regmap)); 98043d24e76SNicolin Chen return PTR_ERR(esai_priv->regmap); 98143d24e76SNicolin Chen } 98243d24e76SNicolin Chen 98343d24e76SNicolin Chen esai_priv->coreclk = devm_clk_get(&pdev->dev, "core"); 98443d24e76SNicolin Chen if (IS_ERR(esai_priv->coreclk)) { 98543d24e76SNicolin Chen dev_err(&pdev->dev, "failed to get core clock: %ld\n", 98643d24e76SNicolin Chen PTR_ERR(esai_priv->coreclk)); 98743d24e76SNicolin Chen return PTR_ERR(esai_priv->coreclk); 98843d24e76SNicolin Chen } 98943d24e76SNicolin Chen 99043d24e76SNicolin Chen esai_priv->extalclk = devm_clk_get(&pdev->dev, "extal"); 99143d24e76SNicolin Chen if (IS_ERR(esai_priv->extalclk)) 99243d24e76SNicolin Chen dev_warn(&pdev->dev, "failed to get extal clock: %ld\n", 99343d24e76SNicolin Chen PTR_ERR(esai_priv->extalclk)); 99443d24e76SNicolin Chen 99543d24e76SNicolin Chen esai_priv->fsysclk = devm_clk_get(&pdev->dev, "fsys"); 99643d24e76SNicolin Chen if (IS_ERR(esai_priv->fsysclk)) 99743d24e76SNicolin Chen dev_warn(&pdev->dev, "failed to get fsys clock: %ld\n", 99843d24e76SNicolin Chen PTR_ERR(esai_priv->fsysclk)); 99943d24e76SNicolin Chen 1000a2a4d604SShengjiu Wang esai_priv->spbaclk = devm_clk_get(&pdev->dev, "spba"); 1001a2a4d604SShengjiu Wang if (IS_ERR(esai_priv->spbaclk)) 1002a2a4d604SShengjiu Wang dev_warn(&pdev->dev, "failed to get spba clock: %ld\n", 1003a2a4d604SShengjiu Wang PTR_ERR(esai_priv->spbaclk)); 1004a2a4d604SShengjiu Wang 100543d24e76SNicolin Chen irq = platform_get_irq(pdev, 0); 1006cf9441adSStephen Boyd if (irq < 0) 100743d24e76SNicolin Chen return irq; 100843d24e76SNicolin Chen 1009c8361757SShengjiu Wang ret = devm_request_irq(&pdev->dev, irq, esai_isr, IRQF_SHARED, 101043d24e76SNicolin Chen esai_priv->name, esai_priv); 101143d24e76SNicolin Chen if (ret) { 101243d24e76SNicolin Chen dev_err(&pdev->dev, "failed to claim irq %u\n", irq); 101343d24e76SNicolin Chen return ret; 101443d24e76SNicolin Chen } 101543d24e76SNicolin Chen 1016de0d712aSShengjiu Wang /* Set a default slot number */ 1017de0d712aSShengjiu Wang esai_priv->slots = 2; 1018de0d712aSShengjiu Wang 1019e0b64fa3SMark Brown /* Set a default clock provider state */ 1020e0b64fa3SMark Brown esai_priv->consumer_mode = true; 102143d24e76SNicolin Chen 102243d24e76SNicolin Chen /* Determine the FIFO depth */ 102343d24e76SNicolin Chen iprop = of_get_property(np, "fsl,fifo-depth", NULL); 102443d24e76SNicolin Chen if (iprop) 102543d24e76SNicolin Chen esai_priv->fifo_depth = be32_to_cpup(iprop); 102643d24e76SNicolin Chen else 102743d24e76SNicolin Chen esai_priv->fifo_depth = 64; 102843d24e76SNicolin Chen 102943d24e76SNicolin Chen esai_priv->dma_params_tx.maxburst = 16; 103043d24e76SNicolin Chen esai_priv->dma_params_rx.maxburst = 16; 103143d24e76SNicolin Chen esai_priv->dma_params_tx.addr = res->start + REG_ESAI_ETDR; 103243d24e76SNicolin Chen esai_priv->dma_params_rx.addr = res->start + REG_ESAI_ERDR; 103343d24e76SNicolin Chen 103443d24e76SNicolin Chen esai_priv->synchronous = 103543d24e76SNicolin Chen of_property_read_bool(np, "fsl,esai-synchronous"); 103643d24e76SNicolin Chen 103743d24e76SNicolin Chen /* Implement full symmetry for synchronous mode */ 103843d24e76SNicolin Chen if (esai_priv->synchronous) { 1039cb2f6927SKuninori Morimoto fsl_esai_dai.symmetric_rate = 1; 104043d24e76SNicolin Chen fsl_esai_dai.symmetric_channels = 1; 1041cb2f6927SKuninori Morimoto fsl_esai_dai.symmetric_sample_bits = 1; 104243d24e76SNicolin Chen } 104343d24e76SNicolin Chen 104443d24e76SNicolin Chen dev_set_drvdata(&pdev->dev, esai_priv); 104535dac627SShengjiu Wang spin_lock_init(&esai_priv->lock); 1046203773e3SShengjiu Wang pm_runtime_enable(&pdev->dev); 1047203773e3SShengjiu Wang if (!pm_runtime_enabled(&pdev->dev)) { 1048203773e3SShengjiu Wang ret = fsl_esai_runtime_resume(&pdev->dev); 1049203773e3SShengjiu Wang if (ret) 1050203773e3SShengjiu Wang goto err_pm_disable; 1051203773e3SShengjiu Wang } 1052203773e3SShengjiu Wang 1053203773e3SShengjiu Wang ret = pm_runtime_get_sync(&pdev->dev); 1054203773e3SShengjiu Wang if (ret < 0) { 1055203773e3SShengjiu Wang pm_runtime_put_noidle(&pdev->dev); 1056203773e3SShengjiu Wang goto err_pm_get_sync; 1057203773e3SShengjiu Wang } 1058203773e3SShengjiu Wang 10595be6155bSShengjiu Wang ret = fsl_esai_hw_init(esai_priv); 10605be6155bSShengjiu Wang if (ret) 1061203773e3SShengjiu Wang goto err_pm_get_sync; 106243d24e76SNicolin Chen 10630ff4e8c6SS.j. Wang esai_priv->tx_mask = 0xFFFFFFFF; 10640ff4e8c6SS.j. Wang esai_priv->rx_mask = 0xFFFFFFFF; 10650ff4e8c6SS.j. Wang 10660ff4e8c6SS.j. Wang /* Clear the TSMA, TSMB, RSMA, RSMB */ 10670ff4e8c6SS.j. Wang regmap_write(esai_priv->regmap, REG_ESAI_TSMA, 0); 10680ff4e8c6SS.j. Wang regmap_write(esai_priv->regmap, REG_ESAI_TSMB, 0); 10690ff4e8c6SS.j. Wang regmap_write(esai_priv->regmap, REG_ESAI_RSMA, 0); 10700ff4e8c6SS.j. Wang regmap_write(esai_priv->regmap, REG_ESAI_RSMB, 0); 10710ff4e8c6SS.j. Wang 1072203773e3SShengjiu Wang ret = pm_runtime_put_sync(&pdev->dev); 1073203773e3SShengjiu Wang if (ret < 0) 1074203773e3SShengjiu Wang goto err_pm_get_sync; 1075203773e3SShengjiu Wang 1076f12ce92eSShengjiu Wang /* 1077f12ce92eSShengjiu Wang * Register platform component before registering cpu dai for there 1078f12ce92eSShengjiu Wang * is not defer probe for platform component in snd_soc_add_pcm_runtime(). 1079f12ce92eSShengjiu Wang */ 1080*9b3ff637SSascha Hauer ret = imx_pcm_dma_init(pdev); 1081f12ce92eSShengjiu Wang if (ret) { 1082f12ce92eSShengjiu Wang dev_err(&pdev->dev, "failed to init imx pcm dma: %d\n", ret); 1083f12ce92eSShengjiu Wang goto err_pm_get_sync; 1084f12ce92eSShengjiu Wang } 1085f12ce92eSShengjiu Wang 108643d24e76SNicolin Chen ret = devm_snd_soc_register_component(&pdev->dev, &fsl_esai_component, 108743d24e76SNicolin Chen &fsl_esai_dai, 1); 108843d24e76SNicolin Chen if (ret) { 108943d24e76SNicolin Chen dev_err(&pdev->dev, "failed to register DAI: %d\n", ret); 1090203773e3SShengjiu Wang goto err_pm_get_sync; 109143d24e76SNicolin Chen } 109243d24e76SNicolin Chen 1093a3d1f931STakashi Iwai INIT_WORK(&esai_priv->work, fsl_esai_hw_reset); 10947ccafa2bSShengjiu Wang 109543d24e76SNicolin Chen return ret; 1096203773e3SShengjiu Wang 1097203773e3SShengjiu Wang err_pm_get_sync: 1098203773e3SShengjiu Wang if (!pm_runtime_status_suspended(&pdev->dev)) 1099203773e3SShengjiu Wang fsl_esai_runtime_suspend(&pdev->dev); 1100203773e3SShengjiu Wang err_pm_disable: 1101203773e3SShengjiu Wang pm_runtime_disable(&pdev->dev); 1102203773e3SShengjiu Wang return ret; 110343d24e76SNicolin Chen } 110443d24e76SNicolin Chen 1105b2d337d8SS.j. Wang static int fsl_esai_remove(struct platform_device *pdev) 1106b2d337d8SS.j. Wang { 11077ccafa2bSShengjiu Wang struct fsl_esai *esai_priv = platform_get_drvdata(pdev); 11087ccafa2bSShengjiu Wang 1109b2d337d8SS.j. Wang pm_runtime_disable(&pdev->dev); 1110203773e3SShengjiu Wang if (!pm_runtime_status_suspended(&pdev->dev)) 1111203773e3SShengjiu Wang fsl_esai_runtime_suspend(&pdev->dev); 1112203773e3SShengjiu Wang 1113a3d1f931STakashi Iwai cancel_work_sync(&esai_priv->work); 1114b2d337d8SS.j. Wang 1115b2d337d8SS.j. Wang return 0; 1116b2d337d8SS.j. Wang } 1117b2d337d8SS.j. Wang 111843d24e76SNicolin Chen static const struct of_device_id fsl_esai_dt_ids[] = { 11196878e752SShengjiu Wang { .compatible = "fsl,imx35-esai", .data = &fsl_esai_imx35 }, 11206878e752SShengjiu Wang { .compatible = "fsl,vf610-esai", .data = &fsl_esai_vf610 }, 11216878e752SShengjiu Wang { .compatible = "fsl,imx6ull-esai", .data = &fsl_esai_imx6ull }, 112243d24e76SNicolin Chen {} 112343d24e76SNicolin Chen }; 112443d24e76SNicolin Chen MODULE_DEVICE_TABLE(of, fsl_esai_dt_ids); 112543d24e76SNicolin Chen 1126b2d337d8SS.j. Wang static int fsl_esai_runtime_resume(struct device *dev) 1127c64c6076SZidan Wang { 1128c64c6076SZidan Wang struct fsl_esai *esai = dev_get_drvdata(dev); 1129c64c6076SZidan Wang int ret; 1130c64c6076SZidan Wang 1131b2d337d8SS.j. Wang /* 1132b2d337d8SS.j. Wang * Some platforms might use the same bit to gate all three or two of 1133b2d337d8SS.j. Wang * clocks, so keep all clocks open/close at the same time for safety 1134b2d337d8SS.j. Wang */ 1135b2d337d8SS.j. Wang ret = clk_prepare_enable(esai->coreclk); 1136b2d337d8SS.j. Wang if (ret) 1137b2d337d8SS.j. Wang return ret; 1138b2d337d8SS.j. Wang if (!IS_ERR(esai->spbaclk)) { 1139b2d337d8SS.j. Wang ret = clk_prepare_enable(esai->spbaclk); 1140b2d337d8SS.j. Wang if (ret) 1141b2d337d8SS.j. Wang goto err_spbaclk; 1142b2d337d8SS.j. Wang } 1143b2d337d8SS.j. Wang if (!IS_ERR(esai->extalclk)) { 1144b2d337d8SS.j. Wang ret = clk_prepare_enable(esai->extalclk); 1145b2d337d8SS.j. Wang if (ret) 1146b2d337d8SS.j. Wang goto err_extalclk; 1147b2d337d8SS.j. Wang } 1148b2d337d8SS.j. Wang if (!IS_ERR(esai->fsysclk)) { 1149b2d337d8SS.j. Wang ret = clk_prepare_enable(esai->fsysclk); 1150b2d337d8SS.j. Wang if (ret) 1151b2d337d8SS.j. Wang goto err_fsysclk; 1152b2d337d8SS.j. Wang } 1153b2d337d8SS.j. Wang 1154c64c6076SZidan Wang regcache_cache_only(esai->regmap, false); 1155c64c6076SZidan Wang 11565be6155bSShengjiu Wang ret = fsl_esai_register_restore(esai); 1157c64c6076SZidan Wang if (ret) 1158b2d337d8SS.j. Wang goto err_regcache_sync; 1159c64c6076SZidan Wang 1160c64c6076SZidan Wang return 0; 1161b2d337d8SS.j. Wang 1162b2d337d8SS.j. Wang err_regcache_sync: 1163b2d337d8SS.j. Wang if (!IS_ERR(esai->fsysclk)) 1164b2d337d8SS.j. Wang clk_disable_unprepare(esai->fsysclk); 1165b2d337d8SS.j. Wang err_fsysclk: 1166b2d337d8SS.j. Wang if (!IS_ERR(esai->extalclk)) 1167b2d337d8SS.j. Wang clk_disable_unprepare(esai->extalclk); 1168b2d337d8SS.j. Wang err_extalclk: 1169b2d337d8SS.j. Wang if (!IS_ERR(esai->spbaclk)) 1170b2d337d8SS.j. Wang clk_disable_unprepare(esai->spbaclk); 1171b2d337d8SS.j. Wang err_spbaclk: 1172b2d337d8SS.j. Wang clk_disable_unprepare(esai->coreclk); 1173b2d337d8SS.j. Wang 1174b2d337d8SS.j. Wang return ret; 1175c64c6076SZidan Wang } 1176b2d337d8SS.j. Wang 1177b2d337d8SS.j. Wang static int fsl_esai_runtime_suspend(struct device *dev) 1178b2d337d8SS.j. Wang { 1179b2d337d8SS.j. Wang struct fsl_esai *esai = dev_get_drvdata(dev); 1180b2d337d8SS.j. Wang 1181b2d337d8SS.j. Wang regcache_cache_only(esai->regmap, true); 1182b2d337d8SS.j. Wang 1183b2d337d8SS.j. Wang if (!IS_ERR(esai->fsysclk)) 1184b2d337d8SS.j. Wang clk_disable_unprepare(esai->fsysclk); 1185b2d337d8SS.j. Wang if (!IS_ERR(esai->extalclk)) 1186b2d337d8SS.j. Wang clk_disable_unprepare(esai->extalclk); 1187b2d337d8SS.j. Wang if (!IS_ERR(esai->spbaclk)) 1188b2d337d8SS.j. Wang clk_disable_unprepare(esai->spbaclk); 1189b2d337d8SS.j. Wang clk_disable_unprepare(esai->coreclk); 1190b2d337d8SS.j. Wang 1191b2d337d8SS.j. Wang return 0; 1192b2d337d8SS.j. Wang } 1193c64c6076SZidan Wang 1194c64c6076SZidan Wang static const struct dev_pm_ops fsl_esai_pm_ops = { 1195b2d337d8SS.j. Wang SET_RUNTIME_PM_OPS(fsl_esai_runtime_suspend, 1196b2d337d8SS.j. Wang fsl_esai_runtime_resume, 1197b2d337d8SS.j. Wang NULL) 1198b2d337d8SS.j. Wang SET_SYSTEM_SLEEP_PM_OPS(pm_runtime_force_suspend, 1199b2d337d8SS.j. Wang pm_runtime_force_resume) 1200c64c6076SZidan Wang }; 1201c64c6076SZidan Wang 120243d24e76SNicolin Chen static struct platform_driver fsl_esai_driver = { 120343d24e76SNicolin Chen .probe = fsl_esai_probe, 1204b2d337d8SS.j. Wang .remove = fsl_esai_remove, 120543d24e76SNicolin Chen .driver = { 120643d24e76SNicolin Chen .name = "fsl-esai-dai", 1207c64c6076SZidan Wang .pm = &fsl_esai_pm_ops, 120843d24e76SNicolin Chen .of_match_table = fsl_esai_dt_ids, 120943d24e76SNicolin Chen }, 121043d24e76SNicolin Chen }; 121143d24e76SNicolin Chen 121243d24e76SNicolin Chen module_platform_driver(fsl_esai_driver); 121343d24e76SNicolin Chen 121443d24e76SNicolin Chen MODULE_AUTHOR("Freescale Semiconductor, Inc."); 121543d24e76SNicolin Chen MODULE_DESCRIPTION("Freescale ESAI CPU DAI driver"); 121643d24e76SNicolin Chen MODULE_LICENSE("GPL v2"); 121743d24e76SNicolin Chen MODULE_ALIAS("platform:fsl-esai-dai"); 1218