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 /** 25*6878e752SShengjiu Wang * fsl_esai_soc_data: soc specific data 26*6878e752SShengjiu Wang * 27*6878e752SShengjiu Wang * @imx: for imx platform 28*6878e752SShengjiu Wang * @reset_at_xrun: flags for enable reset operaton 29*6878e752SShengjiu Wang */ 30*6878e752SShengjiu Wang struct fsl_esai_soc_data { 31*6878e752SShengjiu Wang bool imx; 32*6878e752SShengjiu Wang bool reset_at_xrun; 33*6878e752SShengjiu Wang }; 34*6878e752SShengjiu Wang 35*6878e752SShengjiu Wang /** 3643d24e76SNicolin Chen * fsl_esai: ESAI private data 3743d24e76SNicolin Chen * 3843d24e76SNicolin Chen * @dma_params_rx: DMA parameters for receive channel 3943d24e76SNicolin Chen * @dma_params_tx: DMA parameters for transmit channel 4043d24e76SNicolin Chen * @pdev: platform device pointer 4143d24e76SNicolin Chen * @regmap: regmap handler 4243d24e76SNicolin Chen * @coreclk: clock source to access register 4343d24e76SNicolin Chen * @extalclk: esai clock source to derive HCK, SCK and FS 4443d24e76SNicolin Chen * @fsysclk: system clock source to derive HCK, SCK and FS 45a2a4d604SShengjiu Wang * @spbaclk: SPBA clock (optional, depending on SoC design) 467ccafa2bSShengjiu Wang * @task: tasklet to handle the reset operation 47*6878e752SShengjiu Wang * @soc: soc specific data 4835dac627SShengjiu Wang * @lock: spin lock between hw_reset() and trigger() 4943d24e76SNicolin Chen * @fifo_depth: depth of tx/rx FIFO 5043d24e76SNicolin Chen * @slot_width: width of each DAI slot 51de0d712aSShengjiu Wang * @slots: number of slots 525be6155bSShengjiu Wang * @channels: channel num for tx or rx 5343d24e76SNicolin Chen * @hck_rate: clock rate of desired HCKx clock 54f975ca46SNicolin Chen * @sck_rate: clock rate of desired SCKx clock 55f975ca46SNicolin Chen * @hck_dir: the direction of HCKx pads 5643d24e76SNicolin Chen * @sck_div: if using PSR/PM dividers for SCKx clock 5743d24e76SNicolin Chen * @slave_mode: if fully using DAI slave mode 5843d24e76SNicolin Chen * @synchronous: if using tx/rx synchronous mode 5943d24e76SNicolin Chen * @name: driver name 6043d24e76SNicolin Chen */ 6143d24e76SNicolin Chen struct fsl_esai { 6243d24e76SNicolin Chen struct snd_dmaengine_dai_dma_data dma_params_rx; 6343d24e76SNicolin Chen struct snd_dmaengine_dai_dma_data dma_params_tx; 6443d24e76SNicolin Chen struct platform_device *pdev; 6543d24e76SNicolin Chen struct regmap *regmap; 6643d24e76SNicolin Chen struct clk *coreclk; 6743d24e76SNicolin Chen struct clk *extalclk; 6843d24e76SNicolin Chen struct clk *fsysclk; 69a2a4d604SShengjiu Wang struct clk *spbaclk; 707ccafa2bSShengjiu Wang struct tasklet_struct task; 71*6878e752SShengjiu Wang const struct fsl_esai_soc_data *soc; 7235dac627SShengjiu Wang spinlock_t lock; /* Protect hw_reset and trigger */ 7343d24e76SNicolin Chen u32 fifo_depth; 7443d24e76SNicolin Chen u32 slot_width; 75de0d712aSShengjiu Wang u32 slots; 760ff4e8c6SS.j. Wang u32 tx_mask; 770ff4e8c6SS.j. Wang u32 rx_mask; 785be6155bSShengjiu Wang u32 channels[2]; 7943d24e76SNicolin Chen u32 hck_rate[2]; 80f975ca46SNicolin Chen u32 sck_rate[2]; 81f975ca46SNicolin Chen bool hck_dir[2]; 8243d24e76SNicolin Chen bool sck_div[2]; 8343d24e76SNicolin Chen bool slave_mode; 8443d24e76SNicolin Chen bool synchronous; 8543d24e76SNicolin Chen char name[32]; 8643d24e76SNicolin Chen }; 8743d24e76SNicolin Chen 88*6878e752SShengjiu Wang static struct fsl_esai_soc_data fsl_esai_vf610 = { 89*6878e752SShengjiu Wang .imx = false, 90*6878e752SShengjiu Wang .reset_at_xrun = true, 91*6878e752SShengjiu Wang }; 92*6878e752SShengjiu Wang 93*6878e752SShengjiu Wang static struct fsl_esai_soc_data fsl_esai_imx35 = { 94*6878e752SShengjiu Wang .imx = true, 95*6878e752SShengjiu Wang .reset_at_xrun = true, 96*6878e752SShengjiu Wang }; 97*6878e752SShengjiu Wang 98*6878e752SShengjiu Wang static struct fsl_esai_soc_data fsl_esai_imx6ull = { 99*6878e752SShengjiu Wang .imx = true, 100*6878e752SShengjiu Wang .reset_at_xrun = false, 101*6878e752SShengjiu Wang }; 102*6878e752SShengjiu Wang 10343d24e76SNicolin Chen static irqreturn_t esai_isr(int irq, void *devid) 10443d24e76SNicolin Chen { 10543d24e76SNicolin Chen struct fsl_esai *esai_priv = (struct fsl_esai *)devid; 10643d24e76SNicolin Chen struct platform_device *pdev = esai_priv->pdev; 10743d24e76SNicolin Chen u32 esr; 1087ccafa2bSShengjiu Wang u32 saisr; 10943d24e76SNicolin Chen 11043d24e76SNicolin Chen regmap_read(esai_priv->regmap, REG_ESAI_ESR, &esr); 1117ccafa2bSShengjiu Wang regmap_read(esai_priv->regmap, REG_ESAI_SAISR, &saisr); 1127ccafa2bSShengjiu Wang 1137ccafa2bSShengjiu Wang if ((saisr & (ESAI_SAISR_TUE | ESAI_SAISR_ROE)) && 114*6878e752SShengjiu Wang esai_priv->soc->reset_at_xrun) { 1157ccafa2bSShengjiu Wang dev_dbg(&pdev->dev, "reset module for xrun\n"); 1161fecbb71SShengjiu Wang regmap_update_bits(esai_priv->regmap, REG_ESAI_TCR, 1171fecbb71SShengjiu Wang ESAI_xCR_xEIE_MASK, 0); 1181fecbb71SShengjiu Wang regmap_update_bits(esai_priv->regmap, REG_ESAI_RCR, 1191fecbb71SShengjiu Wang ESAI_xCR_xEIE_MASK, 0); 1207ccafa2bSShengjiu Wang tasklet_schedule(&esai_priv->task); 1217ccafa2bSShengjiu Wang } 12243d24e76SNicolin Chen 12343d24e76SNicolin Chen if (esr & ESAI_ESR_TINIT_MASK) 1243bcc8656SColin Ian King dev_dbg(&pdev->dev, "isr: Transmission Initialized\n"); 12543d24e76SNicolin Chen 12643d24e76SNicolin Chen if (esr & ESAI_ESR_RFF_MASK) 12743d24e76SNicolin Chen dev_warn(&pdev->dev, "isr: Receiving overrun\n"); 12843d24e76SNicolin Chen 12943d24e76SNicolin Chen if (esr & ESAI_ESR_TFE_MASK) 1303bcc8656SColin Ian King dev_warn(&pdev->dev, "isr: Transmission underrun\n"); 13143d24e76SNicolin Chen 13243d24e76SNicolin Chen if (esr & ESAI_ESR_TLS_MASK) 13343d24e76SNicolin Chen dev_dbg(&pdev->dev, "isr: Just transmitted the last slot\n"); 13443d24e76SNicolin Chen 13543d24e76SNicolin Chen if (esr & ESAI_ESR_TDE_MASK) 1363bcc8656SColin Ian King dev_dbg(&pdev->dev, "isr: Transmission data exception\n"); 13743d24e76SNicolin Chen 13843d24e76SNicolin Chen if (esr & ESAI_ESR_TED_MASK) 13943d24e76SNicolin Chen dev_dbg(&pdev->dev, "isr: Transmitting even slots\n"); 14043d24e76SNicolin Chen 14143d24e76SNicolin Chen if (esr & ESAI_ESR_TD_MASK) 14243d24e76SNicolin Chen dev_dbg(&pdev->dev, "isr: Transmitting data\n"); 14343d24e76SNicolin Chen 14443d24e76SNicolin Chen if (esr & ESAI_ESR_RLS_MASK) 14543d24e76SNicolin Chen dev_dbg(&pdev->dev, "isr: Just received the last slot\n"); 14643d24e76SNicolin Chen 14743d24e76SNicolin Chen if (esr & ESAI_ESR_RDE_MASK) 14843d24e76SNicolin Chen dev_dbg(&pdev->dev, "isr: Receiving data exception\n"); 14943d24e76SNicolin Chen 15043d24e76SNicolin Chen if (esr & ESAI_ESR_RED_MASK) 15143d24e76SNicolin Chen dev_dbg(&pdev->dev, "isr: Receiving even slots\n"); 15243d24e76SNicolin Chen 15343d24e76SNicolin Chen if (esr & ESAI_ESR_RD_MASK) 15443d24e76SNicolin Chen dev_dbg(&pdev->dev, "isr: Receiving data\n"); 15543d24e76SNicolin Chen 15643d24e76SNicolin Chen return IRQ_HANDLED; 15743d24e76SNicolin Chen } 15843d24e76SNicolin Chen 15943d24e76SNicolin Chen /** 16043d24e76SNicolin Chen * This function is used to calculate the divisors of psr, pm, fp and it is 16143d24e76SNicolin Chen * supposed to be called in set_dai_sysclk() and set_bclk(). 16243d24e76SNicolin Chen * 16343d24e76SNicolin Chen * @ratio: desired overall ratio for the paticipating dividers 16443d24e76SNicolin Chen * @usefp: for HCK setting, there is no need to set fp divider 16543d24e76SNicolin Chen * @fp: bypass other dividers by setting fp directly if fp != 0 16643d24e76SNicolin Chen * @tx: current setting is for playback or capture 16743d24e76SNicolin Chen */ 16843d24e76SNicolin Chen static int fsl_esai_divisor_cal(struct snd_soc_dai *dai, bool tx, u32 ratio, 16943d24e76SNicolin Chen bool usefp, u32 fp) 17043d24e76SNicolin Chen { 17143d24e76SNicolin Chen struct fsl_esai *esai_priv = snd_soc_dai_get_drvdata(dai); 17243d24e76SNicolin Chen u32 psr, pm = 999, maxfp, prod, sub, savesub, i, j; 17343d24e76SNicolin Chen 17443d24e76SNicolin Chen maxfp = usefp ? 16 : 1; 17543d24e76SNicolin Chen 17643d24e76SNicolin Chen if (usefp && fp) 17743d24e76SNicolin Chen goto out_fp; 17843d24e76SNicolin Chen 17943d24e76SNicolin Chen if (ratio > 2 * 8 * 256 * maxfp || ratio < 2) { 18043d24e76SNicolin Chen dev_err(dai->dev, "the ratio is out of range (2 ~ %d)\n", 18143d24e76SNicolin Chen 2 * 8 * 256 * maxfp); 18243d24e76SNicolin Chen return -EINVAL; 18343d24e76SNicolin Chen } else if (ratio % 2) { 18443d24e76SNicolin Chen dev_err(dai->dev, "the raio must be even if using upper divider\n"); 18543d24e76SNicolin Chen return -EINVAL; 18643d24e76SNicolin Chen } 18743d24e76SNicolin Chen 18843d24e76SNicolin Chen ratio /= 2; 18943d24e76SNicolin Chen 19043d24e76SNicolin Chen psr = ratio <= 256 * maxfp ? ESAI_xCCR_xPSR_BYPASS : ESAI_xCCR_xPSR_DIV8; 19143d24e76SNicolin Chen 192c656941dSNicolin Chen /* Do not loop-search if PM (1 ~ 256) alone can serve the ratio */ 193c656941dSNicolin Chen if (ratio <= 256) { 194c656941dSNicolin Chen pm = ratio; 195c656941dSNicolin Chen fp = 1; 196c656941dSNicolin Chen goto out; 197c656941dSNicolin Chen } 198c656941dSNicolin Chen 19943d24e76SNicolin Chen /* Set the max fluctuation -- 0.1% of the max devisor */ 20043d24e76SNicolin Chen savesub = (psr ? 1 : 8) * 256 * maxfp / 1000; 20143d24e76SNicolin Chen 20243d24e76SNicolin Chen /* Find the best value for PM */ 20343d24e76SNicolin Chen for (i = 1; i <= 256; i++) { 20443d24e76SNicolin Chen for (j = 1; j <= maxfp; j++) { 20543d24e76SNicolin Chen /* PSR (1 or 8) * PM (1 ~ 256) * FP (1 ~ 16) */ 20643d24e76SNicolin Chen prod = (psr ? 1 : 8) * i * j; 20743d24e76SNicolin Chen 20843d24e76SNicolin Chen if (prod == ratio) 20943d24e76SNicolin Chen sub = 0; 21043d24e76SNicolin Chen else if (prod / ratio == 1) 21143d24e76SNicolin Chen sub = prod - ratio; 21243d24e76SNicolin Chen else if (ratio / prod == 1) 21343d24e76SNicolin Chen sub = ratio - prod; 21443d24e76SNicolin Chen else 21543d24e76SNicolin Chen continue; 21643d24e76SNicolin Chen 21743d24e76SNicolin Chen /* Calculate the fraction */ 21843d24e76SNicolin Chen sub = sub * 1000 / ratio; 21943d24e76SNicolin Chen if (sub < savesub) { 22043d24e76SNicolin Chen savesub = sub; 22143d24e76SNicolin Chen pm = i; 22243d24e76SNicolin Chen fp = j; 22343d24e76SNicolin Chen } 22443d24e76SNicolin Chen 22543d24e76SNicolin Chen /* We are lucky */ 22643d24e76SNicolin Chen if (savesub == 0) 22743d24e76SNicolin Chen goto out; 22843d24e76SNicolin Chen } 22943d24e76SNicolin Chen } 23043d24e76SNicolin Chen 23143d24e76SNicolin Chen if (pm == 999) { 23243d24e76SNicolin Chen dev_err(dai->dev, "failed to calculate proper divisors\n"); 23343d24e76SNicolin Chen return -EINVAL; 23443d24e76SNicolin Chen } 23543d24e76SNicolin Chen 23643d24e76SNicolin Chen out: 23743d24e76SNicolin Chen regmap_update_bits(esai_priv->regmap, REG_ESAI_xCCR(tx), 23843d24e76SNicolin Chen ESAI_xCCR_xPSR_MASK | ESAI_xCCR_xPM_MASK, 23943d24e76SNicolin Chen psr | ESAI_xCCR_xPM(pm)); 24043d24e76SNicolin Chen 24143d24e76SNicolin Chen out_fp: 24243d24e76SNicolin Chen /* Bypass fp if not being required */ 24343d24e76SNicolin Chen if (maxfp <= 1) 24443d24e76SNicolin Chen return 0; 24543d24e76SNicolin Chen 24643d24e76SNicolin Chen regmap_update_bits(esai_priv->regmap, REG_ESAI_xCCR(tx), 24743d24e76SNicolin Chen ESAI_xCCR_xFP_MASK, ESAI_xCCR_xFP(fp)); 24843d24e76SNicolin Chen 24943d24e76SNicolin Chen return 0; 25043d24e76SNicolin Chen } 25143d24e76SNicolin Chen 25243d24e76SNicolin Chen /** 25343d24e76SNicolin Chen * This function mainly configures the clock frequency of MCLK (HCKT/HCKR) 25443d24e76SNicolin Chen * 25543d24e76SNicolin Chen * @Parameters: 25643d24e76SNicolin Chen * clk_id: The clock source of HCKT/HCKR 25743d24e76SNicolin Chen * (Input from outside; output from inside, FSYS or EXTAL) 25843d24e76SNicolin Chen * freq: The required clock rate of HCKT/HCKR 25943d24e76SNicolin Chen * dir: The clock direction of HCKT/HCKR 26043d24e76SNicolin Chen * 26143d24e76SNicolin Chen * Note: If the direction is input, we do not care about clk_id. 26243d24e76SNicolin Chen */ 26343d24e76SNicolin Chen static int fsl_esai_set_dai_sysclk(struct snd_soc_dai *dai, int clk_id, 26443d24e76SNicolin Chen unsigned int freq, int dir) 26543d24e76SNicolin Chen { 26643d24e76SNicolin Chen struct fsl_esai *esai_priv = snd_soc_dai_get_drvdata(dai); 26743d24e76SNicolin Chen struct clk *clksrc = esai_priv->extalclk; 2681997ee89SS.j. Wang bool tx = (clk_id <= ESAI_HCKT_EXTAL || esai_priv->synchronous); 26943d24e76SNicolin Chen bool in = dir == SND_SOC_CLOCK_IN; 2703e185238SXiubo Li u32 ratio, ecr = 0; 27143d24e76SNicolin Chen unsigned long clk_rate; 2723e185238SXiubo Li int ret; 27343d24e76SNicolin Chen 2748a2278b7SNicolin Chen if (freq == 0) { 2758a2278b7SNicolin Chen dev_err(dai->dev, "%sput freq of HCK%c should not be 0Hz\n", 2768a2278b7SNicolin Chen in ? "in" : "out", tx ? 'T' : 'R'); 2778a2278b7SNicolin Chen return -EINVAL; 2788a2278b7SNicolin Chen } 2798a2278b7SNicolin Chen 280f975ca46SNicolin Chen /* Bypass divider settings if the requirement doesn't change */ 281f975ca46SNicolin Chen if (freq == esai_priv->hck_rate[tx] && dir == esai_priv->hck_dir[tx]) 282f975ca46SNicolin Chen return 0; 28343d24e76SNicolin Chen 28443d24e76SNicolin Chen /* sck_div can be only bypassed if ETO/ERO=0 and SNC_SOC_CLOCK_OUT */ 28543d24e76SNicolin Chen esai_priv->sck_div[tx] = true; 28643d24e76SNicolin Chen 28743d24e76SNicolin Chen /* Set the direction of HCKT/HCKR pins */ 28843d24e76SNicolin Chen regmap_update_bits(esai_priv->regmap, REG_ESAI_xCCR(tx), 28943d24e76SNicolin Chen ESAI_xCCR_xHCKD, in ? 0 : ESAI_xCCR_xHCKD); 29043d24e76SNicolin Chen 29143d24e76SNicolin Chen if (in) 29243d24e76SNicolin Chen goto out; 29343d24e76SNicolin Chen 29443d24e76SNicolin Chen switch (clk_id) { 29543d24e76SNicolin Chen case ESAI_HCKT_FSYS: 29643d24e76SNicolin Chen case ESAI_HCKR_FSYS: 29743d24e76SNicolin Chen clksrc = esai_priv->fsysclk; 29843d24e76SNicolin Chen break; 29943d24e76SNicolin Chen case ESAI_HCKT_EXTAL: 30043d24e76SNicolin Chen ecr |= ESAI_ECR_ETI; 301903c220bSS.j. Wang break; 30243d24e76SNicolin Chen case ESAI_HCKR_EXTAL: 3031997ee89SS.j. Wang ecr |= esai_priv->synchronous ? ESAI_ECR_ETI : ESAI_ECR_ERI; 30443d24e76SNicolin Chen break; 30543d24e76SNicolin Chen default: 30643d24e76SNicolin Chen return -EINVAL; 30743d24e76SNicolin Chen } 30843d24e76SNicolin Chen 30943d24e76SNicolin Chen if (IS_ERR(clksrc)) { 31043d24e76SNicolin Chen dev_err(dai->dev, "no assigned %s clock\n", 31143d24e76SNicolin Chen clk_id % 2 ? "extal" : "fsys"); 31243d24e76SNicolin Chen return PTR_ERR(clksrc); 31343d24e76SNicolin Chen } 31443d24e76SNicolin Chen clk_rate = clk_get_rate(clksrc); 31543d24e76SNicolin Chen 31643d24e76SNicolin Chen ratio = clk_rate / freq; 31743d24e76SNicolin Chen if (ratio * freq > clk_rate) 31843d24e76SNicolin Chen ret = ratio * freq - clk_rate; 31943d24e76SNicolin Chen else if (ratio * freq < clk_rate) 32043d24e76SNicolin Chen ret = clk_rate - ratio * freq; 32143d24e76SNicolin Chen else 32243d24e76SNicolin Chen ret = 0; 32343d24e76SNicolin Chen 32443d24e76SNicolin Chen /* Block if clock source can not be divided into the required rate */ 32543d24e76SNicolin Chen if (ret != 0 && clk_rate / ret < 1000) { 32643d24e76SNicolin Chen dev_err(dai->dev, "failed to derive required HCK%c rate\n", 32743d24e76SNicolin Chen tx ? 'T' : 'R'); 32843d24e76SNicolin Chen return -EINVAL; 32943d24e76SNicolin Chen } 33043d24e76SNicolin Chen 33157ebbcafSNicolin Chen /* Only EXTAL source can be output directly without using PSR and PM */ 33257ebbcafSNicolin Chen if (ratio == 1 && clksrc == esai_priv->extalclk) { 33343d24e76SNicolin Chen /* Bypass all the dividers if not being needed */ 33443d24e76SNicolin Chen ecr |= tx ? ESAI_ECR_ETO : ESAI_ECR_ERO; 33543d24e76SNicolin Chen goto out; 33657ebbcafSNicolin Chen } else if (ratio < 2) { 33757ebbcafSNicolin Chen /* The ratio should be no less than 2 if using other sources */ 33857ebbcafSNicolin Chen dev_err(dai->dev, "failed to derive required HCK%c rate\n", 33957ebbcafSNicolin Chen tx ? 'T' : 'R'); 34057ebbcafSNicolin Chen return -EINVAL; 34143d24e76SNicolin Chen } 34243d24e76SNicolin Chen 34343d24e76SNicolin Chen ret = fsl_esai_divisor_cal(dai, tx, ratio, false, 0); 34443d24e76SNicolin Chen if (ret) 34543d24e76SNicolin Chen return ret; 34643d24e76SNicolin Chen 34743d24e76SNicolin Chen esai_priv->sck_div[tx] = false; 34843d24e76SNicolin Chen 34943d24e76SNicolin Chen out: 350f975ca46SNicolin Chen esai_priv->hck_dir[tx] = dir; 35143d24e76SNicolin Chen esai_priv->hck_rate[tx] = freq; 35243d24e76SNicolin Chen 35343d24e76SNicolin Chen regmap_update_bits(esai_priv->regmap, REG_ESAI_ECR, 35443d24e76SNicolin Chen tx ? ESAI_ECR_ETI | ESAI_ECR_ETO : 35543d24e76SNicolin Chen ESAI_ECR_ERI | ESAI_ECR_ERO, ecr); 35643d24e76SNicolin Chen 35743d24e76SNicolin Chen return 0; 35843d24e76SNicolin Chen } 35943d24e76SNicolin Chen 36043d24e76SNicolin Chen /** 36143d24e76SNicolin Chen * This function configures the related dividers according to the bclk rate 36243d24e76SNicolin Chen */ 36343d24e76SNicolin Chen static int fsl_esai_set_bclk(struct snd_soc_dai *dai, bool tx, u32 freq) 36443d24e76SNicolin Chen { 36543d24e76SNicolin Chen struct fsl_esai *esai_priv = snd_soc_dai_get_drvdata(dai); 36643d24e76SNicolin Chen u32 hck_rate = esai_priv->hck_rate[tx]; 36743d24e76SNicolin Chen u32 sub, ratio = hck_rate / freq; 368f975ca46SNicolin Chen int ret; 36943d24e76SNicolin Chen 370f975ca46SNicolin Chen /* Don't apply for fully slave mode or unchanged bclk */ 371f975ca46SNicolin Chen if (esai_priv->slave_mode || esai_priv->sck_rate[tx] == freq) 37243d24e76SNicolin Chen return 0; 37343d24e76SNicolin Chen 37443d24e76SNicolin Chen if (ratio * freq > hck_rate) 37543d24e76SNicolin Chen sub = ratio * freq - hck_rate; 37643d24e76SNicolin Chen else if (ratio * freq < hck_rate) 37743d24e76SNicolin Chen sub = hck_rate - ratio * freq; 37843d24e76SNicolin Chen else 37943d24e76SNicolin Chen sub = 0; 38043d24e76SNicolin Chen 38143d24e76SNicolin Chen /* Block if clock source can not be divided into the required rate */ 38243d24e76SNicolin Chen if (sub != 0 && hck_rate / sub < 1000) { 38343d24e76SNicolin Chen dev_err(dai->dev, "failed to derive required SCK%c rate\n", 38443d24e76SNicolin Chen tx ? 'T' : 'R'); 38543d24e76SNicolin Chen return -EINVAL; 38643d24e76SNicolin Chen } 38743d24e76SNicolin Chen 38889e47f62SNicolin Chen /* The ratio should be contented by FP alone if bypassing PM and PSR */ 38989e47f62SNicolin Chen if (!esai_priv->sck_div[tx] && (ratio > 16 || ratio == 0)) { 39043d24e76SNicolin Chen dev_err(dai->dev, "the ratio is out of range (1 ~ 16)\n"); 39143d24e76SNicolin Chen return -EINVAL; 39243d24e76SNicolin Chen } 39343d24e76SNicolin Chen 394f975ca46SNicolin Chen ret = fsl_esai_divisor_cal(dai, tx, ratio, true, 39543d24e76SNicolin Chen esai_priv->sck_div[tx] ? 0 : ratio); 396f975ca46SNicolin Chen if (ret) 397f975ca46SNicolin Chen return ret; 398f975ca46SNicolin Chen 399f975ca46SNicolin Chen /* Save current bclk rate */ 400f975ca46SNicolin Chen esai_priv->sck_rate[tx] = freq; 401f975ca46SNicolin Chen 402f975ca46SNicolin Chen return 0; 40343d24e76SNicolin Chen } 40443d24e76SNicolin Chen 40543d24e76SNicolin Chen static int fsl_esai_set_dai_tdm_slot(struct snd_soc_dai *dai, u32 tx_mask, 40643d24e76SNicolin Chen u32 rx_mask, int slots, int slot_width) 40743d24e76SNicolin Chen { 40843d24e76SNicolin Chen struct fsl_esai *esai_priv = snd_soc_dai_get_drvdata(dai); 40943d24e76SNicolin Chen 41043d24e76SNicolin Chen regmap_update_bits(esai_priv->regmap, REG_ESAI_TCCR, 41143d24e76SNicolin Chen ESAI_xCCR_xDC_MASK, ESAI_xCCR_xDC(slots)); 41243d24e76SNicolin Chen 41343d24e76SNicolin Chen regmap_update_bits(esai_priv->regmap, REG_ESAI_RCCR, 41443d24e76SNicolin Chen ESAI_xCCR_xDC_MASK, ESAI_xCCR_xDC(slots)); 41543d24e76SNicolin Chen 41643d24e76SNicolin Chen esai_priv->slot_width = slot_width; 417de0d712aSShengjiu Wang esai_priv->slots = slots; 4180ff4e8c6SS.j. Wang esai_priv->tx_mask = tx_mask; 4190ff4e8c6SS.j. Wang esai_priv->rx_mask = rx_mask; 42043d24e76SNicolin Chen 42143d24e76SNicolin Chen return 0; 42243d24e76SNicolin Chen } 42343d24e76SNicolin Chen 42443d24e76SNicolin Chen static int fsl_esai_set_dai_fmt(struct snd_soc_dai *dai, unsigned int fmt) 42543d24e76SNicolin Chen { 42643d24e76SNicolin Chen struct fsl_esai *esai_priv = snd_soc_dai_get_drvdata(dai); 42743d24e76SNicolin Chen u32 xcr = 0, xccr = 0, mask; 42843d24e76SNicolin Chen 42943d24e76SNicolin Chen /* DAI mode */ 43043d24e76SNicolin Chen switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) { 43143d24e76SNicolin Chen case SND_SOC_DAIFMT_I2S: 43243d24e76SNicolin Chen /* Data on rising edge of bclk, frame low, 1clk before data */ 43343d24e76SNicolin Chen xcr |= ESAI_xCR_xFSR; 43443d24e76SNicolin Chen xccr |= ESAI_xCCR_xFSP | ESAI_xCCR_xCKP | ESAI_xCCR_xHCKP; 43543d24e76SNicolin Chen break; 43643d24e76SNicolin Chen case SND_SOC_DAIFMT_LEFT_J: 43743d24e76SNicolin Chen /* Data on rising edge of bclk, frame high */ 43843d24e76SNicolin Chen xccr |= ESAI_xCCR_xCKP | ESAI_xCCR_xHCKP; 43943d24e76SNicolin Chen break; 44043d24e76SNicolin Chen case SND_SOC_DAIFMT_RIGHT_J: 44143d24e76SNicolin Chen /* Data on rising edge of bclk, frame high, right aligned */ 442cc29ea00SS.j. Wang xccr |= ESAI_xCCR_xCKP | ESAI_xCCR_xHCKP; 443cc29ea00SS.j. Wang xcr |= ESAI_xCR_xWA; 44443d24e76SNicolin Chen break; 44543d24e76SNicolin Chen case SND_SOC_DAIFMT_DSP_A: 44643d24e76SNicolin Chen /* Data on rising edge of bclk, frame high, 1clk before data */ 44743d24e76SNicolin Chen xcr |= ESAI_xCR_xFSL | ESAI_xCR_xFSR; 44843d24e76SNicolin Chen xccr |= ESAI_xCCR_xCKP | ESAI_xCCR_xHCKP; 44943d24e76SNicolin Chen break; 45043d24e76SNicolin Chen case SND_SOC_DAIFMT_DSP_B: 45143d24e76SNicolin Chen /* Data on rising edge of bclk, frame high */ 45243d24e76SNicolin Chen xcr |= ESAI_xCR_xFSL; 45343d24e76SNicolin Chen xccr |= ESAI_xCCR_xCKP | ESAI_xCCR_xHCKP; 45443d24e76SNicolin Chen break; 45543d24e76SNicolin Chen default: 45643d24e76SNicolin Chen return -EINVAL; 45743d24e76SNicolin Chen } 45843d24e76SNicolin Chen 45943d24e76SNicolin Chen /* DAI clock inversion */ 46043d24e76SNicolin Chen switch (fmt & SND_SOC_DAIFMT_INV_MASK) { 46143d24e76SNicolin Chen case SND_SOC_DAIFMT_NB_NF: 46243d24e76SNicolin Chen /* Nothing to do for both normal cases */ 46343d24e76SNicolin Chen break; 46443d24e76SNicolin Chen case SND_SOC_DAIFMT_IB_NF: 46543d24e76SNicolin Chen /* Invert bit clock */ 46643d24e76SNicolin Chen xccr ^= ESAI_xCCR_xCKP | ESAI_xCCR_xHCKP; 46743d24e76SNicolin Chen break; 46843d24e76SNicolin Chen case SND_SOC_DAIFMT_NB_IF: 46943d24e76SNicolin Chen /* Invert frame clock */ 47043d24e76SNicolin Chen xccr ^= ESAI_xCCR_xFSP; 47143d24e76SNicolin Chen break; 47243d24e76SNicolin Chen case SND_SOC_DAIFMT_IB_IF: 47343d24e76SNicolin Chen /* Invert both clocks */ 47443d24e76SNicolin Chen xccr ^= ESAI_xCCR_xCKP | ESAI_xCCR_xHCKP | ESAI_xCCR_xFSP; 47543d24e76SNicolin Chen break; 47643d24e76SNicolin Chen default: 47743d24e76SNicolin Chen return -EINVAL; 47843d24e76SNicolin Chen } 47943d24e76SNicolin Chen 48043d24e76SNicolin Chen esai_priv->slave_mode = false; 48143d24e76SNicolin Chen 48243d24e76SNicolin Chen /* DAI clock master masks */ 48343d24e76SNicolin Chen switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) { 48443d24e76SNicolin Chen case SND_SOC_DAIFMT_CBM_CFM: 48543d24e76SNicolin Chen esai_priv->slave_mode = true; 48643d24e76SNicolin Chen break; 48743d24e76SNicolin Chen case SND_SOC_DAIFMT_CBS_CFM: 48843d24e76SNicolin Chen xccr |= ESAI_xCCR_xCKD; 48943d24e76SNicolin Chen break; 49043d24e76SNicolin Chen case SND_SOC_DAIFMT_CBM_CFS: 49143d24e76SNicolin Chen xccr |= ESAI_xCCR_xFSD; 49243d24e76SNicolin Chen break; 49343d24e76SNicolin Chen case SND_SOC_DAIFMT_CBS_CFS: 49443d24e76SNicolin Chen xccr |= ESAI_xCCR_xFSD | ESAI_xCCR_xCKD; 49543d24e76SNicolin Chen break; 49643d24e76SNicolin Chen default: 49743d24e76SNicolin Chen return -EINVAL; 49843d24e76SNicolin Chen } 49943d24e76SNicolin Chen 500cc29ea00SS.j. Wang mask = ESAI_xCR_xFSL | ESAI_xCR_xFSR | ESAI_xCR_xWA; 50143d24e76SNicolin Chen regmap_update_bits(esai_priv->regmap, REG_ESAI_TCR, mask, xcr); 50243d24e76SNicolin Chen regmap_update_bits(esai_priv->regmap, REG_ESAI_RCR, mask, xcr); 50343d24e76SNicolin Chen 50443d24e76SNicolin Chen mask = ESAI_xCCR_xCKP | ESAI_xCCR_xHCKP | ESAI_xCCR_xFSP | 505cc29ea00SS.j. Wang ESAI_xCCR_xFSD | ESAI_xCCR_xCKD; 50643d24e76SNicolin Chen regmap_update_bits(esai_priv->regmap, REG_ESAI_TCCR, mask, xccr); 50743d24e76SNicolin Chen regmap_update_bits(esai_priv->regmap, REG_ESAI_RCCR, mask, xccr); 50843d24e76SNicolin Chen 50943d24e76SNicolin Chen return 0; 51043d24e76SNicolin Chen } 51143d24e76SNicolin Chen 51243d24e76SNicolin Chen static int fsl_esai_startup(struct snd_pcm_substream *substream, 51343d24e76SNicolin Chen struct snd_soc_dai *dai) 51443d24e76SNicolin Chen { 51543d24e76SNicolin Chen struct fsl_esai *esai_priv = snd_soc_dai_get_drvdata(dai); 51643d24e76SNicolin Chen 51743d24e76SNicolin Chen if (!dai->active) { 51843d24e76SNicolin Chen /* Set synchronous mode */ 51943d24e76SNicolin Chen regmap_update_bits(esai_priv->regmap, REG_ESAI_SAICR, 52043d24e76SNicolin Chen ESAI_SAICR_SYNC, esai_priv->synchronous ? 52143d24e76SNicolin Chen ESAI_SAICR_SYNC : 0); 52243d24e76SNicolin Chen 52343d24e76SNicolin Chen /* Set a default slot number -- 2 */ 52443d24e76SNicolin Chen regmap_update_bits(esai_priv->regmap, REG_ESAI_TCCR, 52543d24e76SNicolin Chen ESAI_xCCR_xDC_MASK, ESAI_xCCR_xDC(2)); 52643d24e76SNicolin Chen regmap_update_bits(esai_priv->regmap, REG_ESAI_RCCR, 52743d24e76SNicolin Chen ESAI_xCCR_xDC_MASK, ESAI_xCCR_xDC(2)); 52843d24e76SNicolin Chen } 52943d24e76SNicolin Chen 53043d24e76SNicolin Chen return 0; 53133529ec9SFabio Estevam 53243d24e76SNicolin Chen } 53343d24e76SNicolin Chen 53443d24e76SNicolin Chen static int fsl_esai_hw_params(struct snd_pcm_substream *substream, 53543d24e76SNicolin Chen struct snd_pcm_hw_params *params, 53643d24e76SNicolin Chen struct snd_soc_dai *dai) 53743d24e76SNicolin Chen { 53843d24e76SNicolin Chen struct fsl_esai *esai_priv = snd_soc_dai_get_drvdata(dai); 53943d24e76SNicolin Chen bool tx = substream->stream == SNDRV_PCM_STREAM_PLAYBACK; 5404ca73043SZidan Wang u32 width = params_width(params); 54143d24e76SNicolin Chen u32 channels = params_channels(params); 542de0d712aSShengjiu Wang u32 pins = DIV_ROUND_UP(channels, esai_priv->slots); 54386ea522bSNicolin Chen u32 slot_width = width; 5443e185238SXiubo Li u32 bclk, mask, val; 5453e185238SXiubo Li int ret; 54643d24e76SNicolin Chen 547d8ffcf71SGeert Uytterhoeven /* Override slot_width if being specifically set */ 54886ea522bSNicolin Chen if (esai_priv->slot_width) 54986ea522bSNicolin Chen slot_width = esai_priv->slot_width; 55086ea522bSNicolin Chen 55186ea522bSNicolin Chen bclk = params_rate(params) * slot_width * esai_priv->slots; 55243d24e76SNicolin Chen 5531997ee89SS.j. Wang ret = fsl_esai_set_bclk(dai, esai_priv->synchronous || tx, bclk); 55443d24e76SNicolin Chen if (ret) 55543d24e76SNicolin Chen return ret; 55643d24e76SNicolin Chen 5571997ee89SS.j. Wang mask = ESAI_xCR_xSWS_MASK; 5581997ee89SS.j. Wang val = ESAI_xCR_xSWS(slot_width, width); 5591997ee89SS.j. Wang 5601997ee89SS.j. Wang regmap_update_bits(esai_priv->regmap, REG_ESAI_xCR(tx), mask, val); 5611997ee89SS.j. Wang /* Recording in synchronous mode needs to set TCR also */ 5621997ee89SS.j. Wang if (!tx && esai_priv->synchronous) 5631997ee89SS.j. Wang regmap_update_bits(esai_priv->regmap, REG_ESAI_TCR, mask, val); 5641997ee89SS.j. Wang 56543d24e76SNicolin Chen /* Use Normal mode to support monaural audio */ 56643d24e76SNicolin Chen regmap_update_bits(esai_priv->regmap, REG_ESAI_xCR(tx), 56743d24e76SNicolin Chen ESAI_xCR_xMOD_MASK, params_channels(params) > 1 ? 56843d24e76SNicolin Chen ESAI_xCR_xMOD_NETWORK : 0); 56943d24e76SNicolin Chen 57043d24e76SNicolin Chen regmap_update_bits(esai_priv->regmap, REG_ESAI_xFCR(tx), 57143d24e76SNicolin Chen ESAI_xFCR_xFR_MASK, ESAI_xFCR_xFR); 57243d24e76SNicolin Chen 57343d24e76SNicolin Chen mask = ESAI_xFCR_xFR_MASK | ESAI_xFCR_xWA_MASK | ESAI_xFCR_xFWM_MASK | 57443d24e76SNicolin Chen (tx ? ESAI_xFCR_TE_MASK | ESAI_xFCR_TIEN : ESAI_xFCR_RE_MASK); 57543d24e76SNicolin Chen val = ESAI_xFCR_xWA(width) | ESAI_xFCR_xFWM(esai_priv->fifo_depth) | 576de0d712aSShengjiu Wang (tx ? ESAI_xFCR_TE(pins) | ESAI_xFCR_TIEN : ESAI_xFCR_RE(pins)); 57743d24e76SNicolin Chen 57843d24e76SNicolin Chen regmap_update_bits(esai_priv->regmap, REG_ESAI_xFCR(tx), mask, val); 57943d24e76SNicolin Chen 5801997ee89SS.j. Wang if (tx) 5811997ee89SS.j. Wang regmap_update_bits(esai_priv->regmap, REG_ESAI_TCR, 5821997ee89SS.j. Wang ESAI_xCR_PADC, ESAI_xCR_PADC); 58343d24e76SNicolin Chen 5844f8210f6SNicolin Chen /* Remove ESAI personal reset by configuring ESAI_PCRC and ESAI_PRRC */ 5854f8210f6SNicolin Chen regmap_update_bits(esai_priv->regmap, REG_ESAI_PRRC, 5864f8210f6SNicolin Chen ESAI_PRRC_PDC_MASK, ESAI_PRRC_PDC(ESAI_GPIO)); 5874f8210f6SNicolin Chen regmap_update_bits(esai_priv->regmap, REG_ESAI_PCRC, 5884f8210f6SNicolin Chen ESAI_PCRC_PC_MASK, ESAI_PCRC_PC(ESAI_GPIO)); 58943d24e76SNicolin Chen return 0; 59043d24e76SNicolin Chen } 59143d24e76SNicolin Chen 5925be6155bSShengjiu Wang static int fsl_esai_hw_init(struct fsl_esai *esai_priv) 59343d24e76SNicolin Chen { 5945be6155bSShengjiu Wang struct platform_device *pdev = esai_priv->pdev; 5955be6155bSShengjiu Wang int ret; 5965be6155bSShengjiu Wang 5975be6155bSShengjiu Wang /* Reset ESAI unit */ 5985be6155bSShengjiu Wang ret = regmap_update_bits(esai_priv->regmap, REG_ESAI_ECR, 5995be6155bSShengjiu Wang ESAI_ECR_ESAIEN_MASK | ESAI_ECR_ERST_MASK, 6005be6155bSShengjiu Wang ESAI_ECR_ESAIEN | ESAI_ECR_ERST); 6015be6155bSShengjiu Wang if (ret) { 6025be6155bSShengjiu Wang dev_err(&pdev->dev, "failed to reset ESAI: %d\n", ret); 6035be6155bSShengjiu Wang return ret; 6045be6155bSShengjiu Wang } 6055be6155bSShengjiu Wang 6065be6155bSShengjiu Wang /* 6075be6155bSShengjiu Wang * We need to enable ESAI so as to access some of its registers. 6085be6155bSShengjiu Wang * Otherwise, we would fail to dump regmap from user space. 6095be6155bSShengjiu Wang */ 6105be6155bSShengjiu Wang ret = regmap_update_bits(esai_priv->regmap, REG_ESAI_ECR, 6115be6155bSShengjiu Wang ESAI_ECR_ESAIEN_MASK | ESAI_ECR_ERST_MASK, 6125be6155bSShengjiu Wang ESAI_ECR_ESAIEN); 6135be6155bSShengjiu Wang if (ret) { 6145be6155bSShengjiu Wang dev_err(&pdev->dev, "failed to enable ESAI: %d\n", ret); 6155be6155bSShengjiu Wang return ret; 6165be6155bSShengjiu Wang } 6175be6155bSShengjiu Wang 6185be6155bSShengjiu Wang regmap_update_bits(esai_priv->regmap, REG_ESAI_PRRC, 6195be6155bSShengjiu Wang ESAI_PRRC_PDC_MASK, 0); 6205be6155bSShengjiu Wang regmap_update_bits(esai_priv->regmap, REG_ESAI_PCRC, 6215be6155bSShengjiu Wang ESAI_PCRC_PC_MASK, 0); 6225be6155bSShengjiu Wang 6235be6155bSShengjiu Wang return 0; 6245be6155bSShengjiu Wang } 6255be6155bSShengjiu Wang 6265be6155bSShengjiu Wang static int fsl_esai_register_restore(struct fsl_esai *esai_priv) 6275be6155bSShengjiu Wang { 6285be6155bSShengjiu Wang int ret; 6295be6155bSShengjiu Wang 6305be6155bSShengjiu Wang /* FIFO reset for safety */ 6315be6155bSShengjiu Wang regmap_update_bits(esai_priv->regmap, REG_ESAI_TFCR, 6325be6155bSShengjiu Wang ESAI_xFCR_xFR, ESAI_xFCR_xFR); 6335be6155bSShengjiu Wang regmap_update_bits(esai_priv->regmap, REG_ESAI_RFCR, 6345be6155bSShengjiu Wang ESAI_xFCR_xFR, ESAI_xFCR_xFR); 6355be6155bSShengjiu Wang 6365be6155bSShengjiu Wang regcache_mark_dirty(esai_priv->regmap); 6375be6155bSShengjiu Wang ret = regcache_sync(esai_priv->regmap); 6385be6155bSShengjiu Wang if (ret) 6395be6155bSShengjiu Wang return ret; 6405be6155bSShengjiu Wang 6415be6155bSShengjiu Wang /* FIFO reset done */ 6425be6155bSShengjiu Wang regmap_update_bits(esai_priv->regmap, REG_ESAI_TFCR, ESAI_xFCR_xFR, 0); 6435be6155bSShengjiu Wang regmap_update_bits(esai_priv->regmap, REG_ESAI_RFCR, ESAI_xFCR_xFR, 0); 6445be6155bSShengjiu Wang 6455be6155bSShengjiu Wang return 0; 6465be6155bSShengjiu Wang } 6475be6155bSShengjiu Wang 6485be6155bSShengjiu Wang static void fsl_esai_trigger_start(struct fsl_esai *esai_priv, bool tx) 6495be6155bSShengjiu Wang { 6505be6155bSShengjiu Wang u8 i, channels = esai_priv->channels[tx]; 651de0d712aSShengjiu Wang u32 pins = DIV_ROUND_UP(channels, esai_priv->slots); 6520ff4e8c6SS.j. Wang u32 mask; 65343d24e76SNicolin Chen 65443d24e76SNicolin Chen regmap_update_bits(esai_priv->regmap, REG_ESAI_xFCR(tx), 65543d24e76SNicolin Chen ESAI_xFCR_xFEN_MASK, ESAI_xFCR_xFEN); 65643d24e76SNicolin Chen 65743d24e76SNicolin Chen /* Write initial words reqiured by ESAI as normal procedure */ 65843d24e76SNicolin Chen for (i = 0; tx && i < channels; i++) 65943d24e76SNicolin Chen regmap_write(esai_priv->regmap, REG_ESAI_ETDR, 0x0); 66043d24e76SNicolin Chen 6610ff4e8c6SS.j. Wang /* 6620ff4e8c6SS.j. Wang * When set the TE/RE in the end of enablement flow, there 6630ff4e8c6SS.j. Wang * will be channel swap issue for multi data line case. 6640ff4e8c6SS.j. Wang * In order to workaround this issue, we switch the bit 6650ff4e8c6SS.j. Wang * enablement sequence to below sequence 6660ff4e8c6SS.j. Wang * 1) clear the xSMB & xSMA: which is done in probe and 6670ff4e8c6SS.j. Wang * stop state. 6680ff4e8c6SS.j. Wang * 2) set TE/RE 6690ff4e8c6SS.j. Wang * 3) set xSMB 6700ff4e8c6SS.j. Wang * 4) set xSMA: xSMA is the last one in this flow, which 6710ff4e8c6SS.j. Wang * will trigger esai to start. 6720ff4e8c6SS.j. Wang */ 67343d24e76SNicolin Chen regmap_update_bits(esai_priv->regmap, REG_ESAI_xCR(tx), 67443d24e76SNicolin Chen tx ? ESAI_xCR_TE_MASK : ESAI_xCR_RE_MASK, 675de0d712aSShengjiu Wang tx ? ESAI_xCR_TE(pins) : ESAI_xCR_RE(pins)); 6760ff4e8c6SS.j. Wang mask = tx ? esai_priv->tx_mask : esai_priv->rx_mask; 6770ff4e8c6SS.j. Wang 6780ff4e8c6SS.j. Wang regmap_update_bits(esai_priv->regmap, REG_ESAI_xSMB(tx), 6790ff4e8c6SS.j. Wang ESAI_xSMB_xS_MASK, ESAI_xSMB_xS(mask)); 6800ff4e8c6SS.j. Wang regmap_update_bits(esai_priv->regmap, REG_ESAI_xSMA(tx), 6810ff4e8c6SS.j. Wang ESAI_xSMA_xS_MASK, ESAI_xSMA_xS(mask)); 6827ccafa2bSShengjiu Wang 6837ccafa2bSShengjiu Wang /* Enable Exception interrupt */ 6847ccafa2bSShengjiu Wang regmap_update_bits(esai_priv->regmap, REG_ESAI_xCR(tx), 6857ccafa2bSShengjiu Wang ESAI_xCR_xEIE_MASK, ESAI_xCR_xEIE); 6865be6155bSShengjiu Wang } 6870ff4e8c6SS.j. Wang 6885be6155bSShengjiu Wang static void fsl_esai_trigger_stop(struct fsl_esai *esai_priv, bool tx) 6895be6155bSShengjiu Wang { 69043d24e76SNicolin Chen regmap_update_bits(esai_priv->regmap, REG_ESAI_xCR(tx), 6917ccafa2bSShengjiu Wang ESAI_xCR_xEIE_MASK, 0); 6927ccafa2bSShengjiu Wang 6937ccafa2bSShengjiu Wang regmap_update_bits(esai_priv->regmap, REG_ESAI_xCR(tx), 69443d24e76SNicolin Chen tx ? ESAI_xCR_TE_MASK : ESAI_xCR_RE_MASK, 0); 6950ff4e8c6SS.j. Wang regmap_update_bits(esai_priv->regmap, REG_ESAI_xSMA(tx), 6960ff4e8c6SS.j. Wang ESAI_xSMA_xS_MASK, 0); 6970ff4e8c6SS.j. Wang regmap_update_bits(esai_priv->regmap, REG_ESAI_xSMB(tx), 6980ff4e8c6SS.j. Wang ESAI_xSMB_xS_MASK, 0); 69943d24e76SNicolin Chen 70043d24e76SNicolin Chen /* Disable and reset FIFO */ 70143d24e76SNicolin Chen regmap_update_bits(esai_priv->regmap, REG_ESAI_xFCR(tx), 70243d24e76SNicolin Chen ESAI_xFCR_xFR | ESAI_xFCR_xFEN, ESAI_xFCR_xFR); 70343d24e76SNicolin Chen regmap_update_bits(esai_priv->regmap, REG_ESAI_xFCR(tx), 70443d24e76SNicolin Chen ESAI_xFCR_xFR, 0); 7055be6155bSShengjiu Wang } 7065be6155bSShengjiu Wang 7077ccafa2bSShengjiu Wang static void fsl_esai_hw_reset(unsigned long arg) 7087ccafa2bSShengjiu Wang { 7097ccafa2bSShengjiu Wang struct fsl_esai *esai_priv = (struct fsl_esai *)arg; 7107ccafa2bSShengjiu Wang bool tx = true, rx = false, enabled[2]; 71135dac627SShengjiu Wang unsigned long lock_flags; 7127ccafa2bSShengjiu Wang u32 tfcr, rfcr; 7137ccafa2bSShengjiu Wang 71435dac627SShengjiu Wang spin_lock_irqsave(&esai_priv->lock, lock_flags); 7157ccafa2bSShengjiu Wang /* Save the registers */ 7167ccafa2bSShengjiu Wang regmap_read(esai_priv->regmap, REG_ESAI_TFCR, &tfcr); 7177ccafa2bSShengjiu Wang regmap_read(esai_priv->regmap, REG_ESAI_RFCR, &rfcr); 7187ccafa2bSShengjiu Wang enabled[tx] = tfcr & ESAI_xFCR_xFEN; 7197ccafa2bSShengjiu Wang enabled[rx] = rfcr & ESAI_xFCR_xFEN; 7207ccafa2bSShengjiu Wang 7217ccafa2bSShengjiu Wang /* Stop the tx & rx */ 7227ccafa2bSShengjiu Wang fsl_esai_trigger_stop(esai_priv, tx); 7237ccafa2bSShengjiu Wang fsl_esai_trigger_stop(esai_priv, rx); 7247ccafa2bSShengjiu Wang 7257ccafa2bSShengjiu Wang /* Reset the esai, and ignore return value */ 7267ccafa2bSShengjiu Wang fsl_esai_hw_init(esai_priv); 7277ccafa2bSShengjiu Wang 7287ccafa2bSShengjiu Wang /* Enforce ESAI personal resets for both TX and RX */ 7297ccafa2bSShengjiu Wang regmap_update_bits(esai_priv->regmap, REG_ESAI_TCR, 7307ccafa2bSShengjiu Wang ESAI_xCR_xPR_MASK, ESAI_xCR_xPR); 7317ccafa2bSShengjiu Wang regmap_update_bits(esai_priv->regmap, REG_ESAI_RCR, 7327ccafa2bSShengjiu Wang ESAI_xCR_xPR_MASK, ESAI_xCR_xPR); 7337ccafa2bSShengjiu Wang 7347ccafa2bSShengjiu Wang /* Restore registers by regcache_sync, and ignore return value */ 7357ccafa2bSShengjiu Wang fsl_esai_register_restore(esai_priv); 7367ccafa2bSShengjiu Wang 7377ccafa2bSShengjiu Wang /* Remove ESAI personal resets by configuring PCRC and PRRC also */ 7387ccafa2bSShengjiu Wang regmap_update_bits(esai_priv->regmap, REG_ESAI_TCR, 7397ccafa2bSShengjiu Wang ESAI_xCR_xPR_MASK, 0); 7407ccafa2bSShengjiu Wang regmap_update_bits(esai_priv->regmap, REG_ESAI_RCR, 7417ccafa2bSShengjiu Wang ESAI_xCR_xPR_MASK, 0); 7427ccafa2bSShengjiu Wang regmap_update_bits(esai_priv->regmap, REG_ESAI_PRRC, 7437ccafa2bSShengjiu Wang ESAI_PRRC_PDC_MASK, ESAI_PRRC_PDC(ESAI_GPIO)); 7447ccafa2bSShengjiu Wang regmap_update_bits(esai_priv->regmap, REG_ESAI_PCRC, 7457ccafa2bSShengjiu Wang ESAI_PCRC_PC_MASK, ESAI_PCRC_PC(ESAI_GPIO)); 7467ccafa2bSShengjiu Wang 7477ccafa2bSShengjiu Wang /* Restart tx / rx, if they already enabled */ 7487ccafa2bSShengjiu Wang if (enabled[tx]) 7497ccafa2bSShengjiu Wang fsl_esai_trigger_start(esai_priv, tx); 7507ccafa2bSShengjiu Wang if (enabled[rx]) 7517ccafa2bSShengjiu Wang fsl_esai_trigger_start(esai_priv, rx); 75235dac627SShengjiu Wang 75335dac627SShengjiu Wang spin_unlock_irqrestore(&esai_priv->lock, lock_flags); 7547ccafa2bSShengjiu Wang } 7557ccafa2bSShengjiu Wang 7565be6155bSShengjiu Wang static int fsl_esai_trigger(struct snd_pcm_substream *substream, int cmd, 7575be6155bSShengjiu Wang struct snd_soc_dai *dai) 7585be6155bSShengjiu Wang { 7595be6155bSShengjiu Wang struct fsl_esai *esai_priv = snd_soc_dai_get_drvdata(dai); 7605be6155bSShengjiu Wang bool tx = substream->stream == SNDRV_PCM_STREAM_PLAYBACK; 76135dac627SShengjiu Wang unsigned long lock_flags; 7625be6155bSShengjiu Wang 7635be6155bSShengjiu Wang esai_priv->channels[tx] = substream->runtime->channels; 7645be6155bSShengjiu Wang 7655be6155bSShengjiu Wang switch (cmd) { 7665be6155bSShengjiu Wang case SNDRV_PCM_TRIGGER_START: 7675be6155bSShengjiu Wang case SNDRV_PCM_TRIGGER_RESUME: 7685be6155bSShengjiu Wang case SNDRV_PCM_TRIGGER_PAUSE_RELEASE: 76935dac627SShengjiu Wang spin_lock_irqsave(&esai_priv->lock, lock_flags); 7705be6155bSShengjiu Wang fsl_esai_trigger_start(esai_priv, tx); 77135dac627SShengjiu Wang spin_unlock_irqrestore(&esai_priv->lock, lock_flags); 7725be6155bSShengjiu Wang break; 7735be6155bSShengjiu Wang case SNDRV_PCM_TRIGGER_SUSPEND: 7745be6155bSShengjiu Wang case SNDRV_PCM_TRIGGER_STOP: 7755be6155bSShengjiu Wang case SNDRV_PCM_TRIGGER_PAUSE_PUSH: 77635dac627SShengjiu Wang spin_lock_irqsave(&esai_priv->lock, lock_flags); 7775be6155bSShengjiu Wang fsl_esai_trigger_stop(esai_priv, tx); 77835dac627SShengjiu Wang spin_unlock_irqrestore(&esai_priv->lock, lock_flags); 77943d24e76SNicolin Chen break; 78043d24e76SNicolin Chen default: 78143d24e76SNicolin Chen return -EINVAL; 78243d24e76SNicolin Chen } 78343d24e76SNicolin Chen 78443d24e76SNicolin Chen return 0; 78543d24e76SNicolin Chen } 78643d24e76SNicolin Chen 7875d29e95eSGustavo A. R. Silva static const struct snd_soc_dai_ops fsl_esai_dai_ops = { 78843d24e76SNicolin Chen .startup = fsl_esai_startup, 78943d24e76SNicolin Chen .trigger = fsl_esai_trigger, 79043d24e76SNicolin Chen .hw_params = fsl_esai_hw_params, 79143d24e76SNicolin Chen .set_sysclk = fsl_esai_set_dai_sysclk, 79243d24e76SNicolin Chen .set_fmt = fsl_esai_set_dai_fmt, 79343d24e76SNicolin Chen .set_tdm_slot = fsl_esai_set_dai_tdm_slot, 79443d24e76SNicolin Chen }; 79543d24e76SNicolin Chen 79643d24e76SNicolin Chen static int fsl_esai_dai_probe(struct snd_soc_dai *dai) 79743d24e76SNicolin Chen { 79843d24e76SNicolin Chen struct fsl_esai *esai_priv = snd_soc_dai_get_drvdata(dai); 79943d24e76SNicolin Chen 80043d24e76SNicolin Chen snd_soc_dai_init_dma_data(dai, &esai_priv->dma_params_tx, 80143d24e76SNicolin Chen &esai_priv->dma_params_rx); 80243d24e76SNicolin Chen 80343d24e76SNicolin Chen return 0; 80443d24e76SNicolin Chen } 80543d24e76SNicolin Chen 80643d24e76SNicolin Chen static struct snd_soc_dai_driver fsl_esai_dai = { 80743d24e76SNicolin Chen .probe = fsl_esai_dai_probe, 80843d24e76SNicolin Chen .playback = { 80974ccb27cSNicolin Chen .stream_name = "CPU-Playback", 81043d24e76SNicolin Chen .channels_min = 1, 81143d24e76SNicolin Chen .channels_max = 12, 812f2a3ee01SFabio Estevam .rates = SNDRV_PCM_RATE_8000_192000, 81343d24e76SNicolin Chen .formats = FSL_ESAI_FORMATS, 81443d24e76SNicolin Chen }, 81543d24e76SNicolin Chen .capture = { 81674ccb27cSNicolin Chen .stream_name = "CPU-Capture", 81743d24e76SNicolin Chen .channels_min = 1, 81843d24e76SNicolin Chen .channels_max = 8, 819f2a3ee01SFabio Estevam .rates = SNDRV_PCM_RATE_8000_192000, 82043d24e76SNicolin Chen .formats = FSL_ESAI_FORMATS, 82143d24e76SNicolin Chen }, 82243d24e76SNicolin Chen .ops = &fsl_esai_dai_ops, 82343d24e76SNicolin Chen }; 82443d24e76SNicolin Chen 82543d24e76SNicolin Chen static const struct snd_soc_component_driver fsl_esai_component = { 82643d24e76SNicolin Chen .name = "fsl-esai", 82743d24e76SNicolin Chen }; 82843d24e76SNicolin Chen 829c64c6076SZidan Wang static const struct reg_default fsl_esai_reg_defaults[] = { 8308973112aSZidan Wang {REG_ESAI_ETDR, 0x00000000}, 8318973112aSZidan Wang {REG_ESAI_ECR, 0x00000000}, 8328973112aSZidan Wang {REG_ESAI_TFCR, 0x00000000}, 8338973112aSZidan Wang {REG_ESAI_RFCR, 0x00000000}, 8348973112aSZidan Wang {REG_ESAI_TX0, 0x00000000}, 8358973112aSZidan Wang {REG_ESAI_TX1, 0x00000000}, 8368973112aSZidan Wang {REG_ESAI_TX2, 0x00000000}, 8378973112aSZidan Wang {REG_ESAI_TX3, 0x00000000}, 8388973112aSZidan Wang {REG_ESAI_TX4, 0x00000000}, 8398973112aSZidan Wang {REG_ESAI_TX5, 0x00000000}, 8408973112aSZidan Wang {REG_ESAI_TSR, 0x00000000}, 8418973112aSZidan Wang {REG_ESAI_SAICR, 0x00000000}, 8428973112aSZidan Wang {REG_ESAI_TCR, 0x00000000}, 8438973112aSZidan Wang {REG_ESAI_TCCR, 0x00000000}, 8448973112aSZidan Wang {REG_ESAI_RCR, 0x00000000}, 8458973112aSZidan Wang {REG_ESAI_RCCR, 0x00000000}, 8468973112aSZidan Wang {REG_ESAI_TSMA, 0x0000ffff}, 8478973112aSZidan Wang {REG_ESAI_TSMB, 0x0000ffff}, 8488973112aSZidan Wang {REG_ESAI_RSMA, 0x0000ffff}, 8498973112aSZidan Wang {REG_ESAI_RSMB, 0x0000ffff}, 8508973112aSZidan Wang {REG_ESAI_PRRC, 0x00000000}, 8518973112aSZidan Wang {REG_ESAI_PCRC, 0x00000000}, 852c64c6076SZidan Wang }; 853c64c6076SZidan Wang 85443d24e76SNicolin Chen static bool fsl_esai_readable_reg(struct device *dev, unsigned int reg) 85543d24e76SNicolin Chen { 85643d24e76SNicolin Chen switch (reg) { 85743d24e76SNicolin Chen case REG_ESAI_ERDR: 85843d24e76SNicolin Chen case REG_ESAI_ECR: 85943d24e76SNicolin Chen case REG_ESAI_ESR: 86043d24e76SNicolin Chen case REG_ESAI_TFCR: 86143d24e76SNicolin Chen case REG_ESAI_TFSR: 86243d24e76SNicolin Chen case REG_ESAI_RFCR: 86343d24e76SNicolin Chen case REG_ESAI_RFSR: 86443d24e76SNicolin Chen case REG_ESAI_RX0: 86543d24e76SNicolin Chen case REG_ESAI_RX1: 86643d24e76SNicolin Chen case REG_ESAI_RX2: 86743d24e76SNicolin Chen case REG_ESAI_RX3: 86843d24e76SNicolin Chen case REG_ESAI_SAISR: 86943d24e76SNicolin Chen case REG_ESAI_SAICR: 87043d24e76SNicolin Chen case REG_ESAI_TCR: 87143d24e76SNicolin Chen case REG_ESAI_TCCR: 87243d24e76SNicolin Chen case REG_ESAI_RCR: 87343d24e76SNicolin Chen case REG_ESAI_RCCR: 87443d24e76SNicolin Chen case REG_ESAI_TSMA: 87543d24e76SNicolin Chen case REG_ESAI_TSMB: 87643d24e76SNicolin Chen case REG_ESAI_RSMA: 87743d24e76SNicolin Chen case REG_ESAI_RSMB: 87843d24e76SNicolin Chen case REG_ESAI_PRRC: 87943d24e76SNicolin Chen case REG_ESAI_PCRC: 88043d24e76SNicolin Chen return true; 88143d24e76SNicolin Chen default: 88243d24e76SNicolin Chen return false; 88343d24e76SNicolin Chen } 88443d24e76SNicolin Chen } 88543d24e76SNicolin Chen 886c64c6076SZidan Wang static bool fsl_esai_volatile_reg(struct device *dev, unsigned int reg) 887c64c6076SZidan Wang { 888c64c6076SZidan Wang switch (reg) { 889c64c6076SZidan Wang case REG_ESAI_ERDR: 890c64c6076SZidan Wang case REG_ESAI_ESR: 891c64c6076SZidan Wang case REG_ESAI_TFSR: 892c64c6076SZidan Wang case REG_ESAI_RFSR: 893c64c6076SZidan Wang case REG_ESAI_RX0: 894c64c6076SZidan Wang case REG_ESAI_RX1: 895c64c6076SZidan Wang case REG_ESAI_RX2: 896c64c6076SZidan Wang case REG_ESAI_RX3: 897c64c6076SZidan Wang case REG_ESAI_SAISR: 898c64c6076SZidan Wang return true; 899c64c6076SZidan Wang default: 900c64c6076SZidan Wang return false; 901c64c6076SZidan Wang } 902c64c6076SZidan Wang } 903c64c6076SZidan Wang 90443d24e76SNicolin Chen static bool fsl_esai_writeable_reg(struct device *dev, unsigned int reg) 90543d24e76SNicolin Chen { 90643d24e76SNicolin Chen switch (reg) { 90743d24e76SNicolin Chen case REG_ESAI_ETDR: 90843d24e76SNicolin Chen case REG_ESAI_ECR: 90943d24e76SNicolin Chen case REG_ESAI_TFCR: 91043d24e76SNicolin Chen case REG_ESAI_RFCR: 91143d24e76SNicolin Chen case REG_ESAI_TX0: 91243d24e76SNicolin Chen case REG_ESAI_TX1: 91343d24e76SNicolin Chen case REG_ESAI_TX2: 91443d24e76SNicolin Chen case REG_ESAI_TX3: 91543d24e76SNicolin Chen case REG_ESAI_TX4: 91643d24e76SNicolin Chen case REG_ESAI_TX5: 91743d24e76SNicolin Chen case REG_ESAI_TSR: 91843d24e76SNicolin Chen case REG_ESAI_SAICR: 91943d24e76SNicolin Chen case REG_ESAI_TCR: 92043d24e76SNicolin Chen case REG_ESAI_TCCR: 92143d24e76SNicolin Chen case REG_ESAI_RCR: 92243d24e76SNicolin Chen case REG_ESAI_RCCR: 92343d24e76SNicolin Chen case REG_ESAI_TSMA: 92443d24e76SNicolin Chen case REG_ESAI_TSMB: 92543d24e76SNicolin Chen case REG_ESAI_RSMA: 92643d24e76SNicolin Chen case REG_ESAI_RSMB: 92743d24e76SNicolin Chen case REG_ESAI_PRRC: 92843d24e76SNicolin Chen case REG_ESAI_PCRC: 92943d24e76SNicolin Chen return true; 93043d24e76SNicolin Chen default: 93143d24e76SNicolin Chen return false; 93243d24e76SNicolin Chen } 93343d24e76SNicolin Chen } 93443d24e76SNicolin Chen 93592bd0334SXiubo Li static const struct regmap_config fsl_esai_regmap_config = { 93643d24e76SNicolin Chen .reg_bits = 32, 93743d24e76SNicolin Chen .reg_stride = 4, 93843d24e76SNicolin Chen .val_bits = 32, 93943d24e76SNicolin Chen 94043d24e76SNicolin Chen .max_register = REG_ESAI_PCRC, 941c64c6076SZidan Wang .reg_defaults = fsl_esai_reg_defaults, 942c64c6076SZidan Wang .num_reg_defaults = ARRAY_SIZE(fsl_esai_reg_defaults), 94343d24e76SNicolin Chen .readable_reg = fsl_esai_readable_reg, 944c64c6076SZidan Wang .volatile_reg = fsl_esai_volatile_reg, 94543d24e76SNicolin Chen .writeable_reg = fsl_esai_writeable_reg, 9460effb865SMarek Vasut .cache_type = REGCACHE_FLAT, 94743d24e76SNicolin Chen }; 94843d24e76SNicolin Chen 94943d24e76SNicolin Chen static int fsl_esai_probe(struct platform_device *pdev) 95043d24e76SNicolin Chen { 95143d24e76SNicolin Chen struct device_node *np = pdev->dev.of_node; 95243d24e76SNicolin Chen struct fsl_esai *esai_priv; 95343d24e76SNicolin Chen struct resource *res; 9540600b3e1SFabio Estevam const __be32 *iprop; 95543d24e76SNicolin Chen void __iomem *regs; 95643d24e76SNicolin Chen int irq, ret; 95743d24e76SNicolin Chen 95843d24e76SNicolin Chen esai_priv = devm_kzalloc(&pdev->dev, sizeof(*esai_priv), GFP_KERNEL); 95943d24e76SNicolin Chen if (!esai_priv) 96043d24e76SNicolin Chen return -ENOMEM; 96143d24e76SNicolin Chen 96243d24e76SNicolin Chen esai_priv->pdev = pdev; 9635d585e1eSRob Herring snprintf(esai_priv->name, sizeof(esai_priv->name), "%pOFn", np); 96443d24e76SNicolin Chen 965*6878e752SShengjiu Wang esai_priv->soc = of_device_get_match_data(&pdev->dev); 966*6878e752SShengjiu Wang if (!esai_priv->soc) { 967*6878e752SShengjiu Wang dev_err(&pdev->dev, "failed to get soc data\n"); 968*6878e752SShengjiu Wang return -ENODEV; 969*6878e752SShengjiu Wang } 9707ccafa2bSShengjiu Wang 97143d24e76SNicolin Chen /* Get the addresses and IRQ */ 97243d24e76SNicolin Chen res = platform_get_resource(pdev, IORESOURCE_MEM, 0); 97343d24e76SNicolin Chen regs = devm_ioremap_resource(&pdev->dev, res); 97443d24e76SNicolin Chen if (IS_ERR(regs)) 97543d24e76SNicolin Chen return PTR_ERR(regs); 97643d24e76SNicolin Chen 97743d24e76SNicolin Chen esai_priv->regmap = devm_regmap_init_mmio_clk(&pdev->dev, 97843d24e76SNicolin Chen "core", regs, &fsl_esai_regmap_config); 97943d24e76SNicolin Chen if (IS_ERR(esai_priv->regmap)) { 98043d24e76SNicolin Chen dev_err(&pdev->dev, "failed to init regmap: %ld\n", 98143d24e76SNicolin Chen PTR_ERR(esai_priv->regmap)); 98243d24e76SNicolin Chen return PTR_ERR(esai_priv->regmap); 98343d24e76SNicolin Chen } 98443d24e76SNicolin Chen 98543d24e76SNicolin Chen esai_priv->coreclk = devm_clk_get(&pdev->dev, "core"); 98643d24e76SNicolin Chen if (IS_ERR(esai_priv->coreclk)) { 98743d24e76SNicolin Chen dev_err(&pdev->dev, "failed to get core clock: %ld\n", 98843d24e76SNicolin Chen PTR_ERR(esai_priv->coreclk)); 98943d24e76SNicolin Chen return PTR_ERR(esai_priv->coreclk); 99043d24e76SNicolin Chen } 99143d24e76SNicolin Chen 99243d24e76SNicolin Chen esai_priv->extalclk = devm_clk_get(&pdev->dev, "extal"); 99343d24e76SNicolin Chen if (IS_ERR(esai_priv->extalclk)) 99443d24e76SNicolin Chen dev_warn(&pdev->dev, "failed to get extal clock: %ld\n", 99543d24e76SNicolin Chen PTR_ERR(esai_priv->extalclk)); 99643d24e76SNicolin Chen 99743d24e76SNicolin Chen esai_priv->fsysclk = devm_clk_get(&pdev->dev, "fsys"); 99843d24e76SNicolin Chen if (IS_ERR(esai_priv->fsysclk)) 99943d24e76SNicolin Chen dev_warn(&pdev->dev, "failed to get fsys clock: %ld\n", 100043d24e76SNicolin Chen PTR_ERR(esai_priv->fsysclk)); 100143d24e76SNicolin Chen 1002a2a4d604SShengjiu Wang esai_priv->spbaclk = devm_clk_get(&pdev->dev, "spba"); 1003a2a4d604SShengjiu Wang if (IS_ERR(esai_priv->spbaclk)) 1004a2a4d604SShengjiu Wang dev_warn(&pdev->dev, "failed to get spba clock: %ld\n", 1005a2a4d604SShengjiu Wang PTR_ERR(esai_priv->spbaclk)); 1006a2a4d604SShengjiu Wang 100743d24e76SNicolin Chen irq = platform_get_irq(pdev, 0); 1008cf9441adSStephen Boyd if (irq < 0) 100943d24e76SNicolin Chen return irq; 101043d24e76SNicolin Chen 101143d24e76SNicolin Chen ret = devm_request_irq(&pdev->dev, irq, esai_isr, 0, 101243d24e76SNicolin Chen esai_priv->name, esai_priv); 101343d24e76SNicolin Chen if (ret) { 101443d24e76SNicolin Chen dev_err(&pdev->dev, "failed to claim irq %u\n", irq); 101543d24e76SNicolin Chen return ret; 101643d24e76SNicolin Chen } 101743d24e76SNicolin Chen 1018de0d712aSShengjiu Wang /* Set a default slot number */ 1019de0d712aSShengjiu Wang esai_priv->slots = 2; 1020de0d712aSShengjiu Wang 102143d24e76SNicolin Chen /* Set a default master/slave state */ 102243d24e76SNicolin Chen esai_priv->slave_mode = true; 102343d24e76SNicolin Chen 102443d24e76SNicolin Chen /* Determine the FIFO depth */ 102543d24e76SNicolin Chen iprop = of_get_property(np, "fsl,fifo-depth", NULL); 102643d24e76SNicolin Chen if (iprop) 102743d24e76SNicolin Chen esai_priv->fifo_depth = be32_to_cpup(iprop); 102843d24e76SNicolin Chen else 102943d24e76SNicolin Chen esai_priv->fifo_depth = 64; 103043d24e76SNicolin Chen 103143d24e76SNicolin Chen esai_priv->dma_params_tx.maxburst = 16; 103243d24e76SNicolin Chen esai_priv->dma_params_rx.maxburst = 16; 103343d24e76SNicolin Chen esai_priv->dma_params_tx.addr = res->start + REG_ESAI_ETDR; 103443d24e76SNicolin Chen esai_priv->dma_params_rx.addr = res->start + REG_ESAI_ERDR; 103543d24e76SNicolin Chen 103643d24e76SNicolin Chen esai_priv->synchronous = 103743d24e76SNicolin Chen of_property_read_bool(np, "fsl,esai-synchronous"); 103843d24e76SNicolin Chen 103943d24e76SNicolin Chen /* Implement full symmetry for synchronous mode */ 104043d24e76SNicolin Chen if (esai_priv->synchronous) { 104143d24e76SNicolin Chen fsl_esai_dai.symmetric_rates = 1; 104243d24e76SNicolin Chen fsl_esai_dai.symmetric_channels = 1; 104343d24e76SNicolin Chen fsl_esai_dai.symmetric_samplebits = 1; 104443d24e76SNicolin Chen } 104543d24e76SNicolin Chen 104643d24e76SNicolin Chen dev_set_drvdata(&pdev->dev, esai_priv); 104743d24e76SNicolin Chen 104835dac627SShengjiu Wang spin_lock_init(&esai_priv->lock); 10495be6155bSShengjiu Wang ret = fsl_esai_hw_init(esai_priv); 10505be6155bSShengjiu Wang if (ret) 105143d24e76SNicolin Chen return ret; 105243d24e76SNicolin Chen 10530ff4e8c6SS.j. Wang esai_priv->tx_mask = 0xFFFFFFFF; 10540ff4e8c6SS.j. Wang esai_priv->rx_mask = 0xFFFFFFFF; 10550ff4e8c6SS.j. Wang 10560ff4e8c6SS.j. Wang /* Clear the TSMA, TSMB, RSMA, RSMB */ 10570ff4e8c6SS.j. Wang regmap_write(esai_priv->regmap, REG_ESAI_TSMA, 0); 10580ff4e8c6SS.j. Wang regmap_write(esai_priv->regmap, REG_ESAI_TSMB, 0); 10590ff4e8c6SS.j. Wang regmap_write(esai_priv->regmap, REG_ESAI_RSMA, 0); 10600ff4e8c6SS.j. Wang regmap_write(esai_priv->regmap, REG_ESAI_RSMB, 0); 10610ff4e8c6SS.j. Wang 106243d24e76SNicolin Chen ret = devm_snd_soc_register_component(&pdev->dev, &fsl_esai_component, 106343d24e76SNicolin Chen &fsl_esai_dai, 1); 106443d24e76SNicolin Chen if (ret) { 106543d24e76SNicolin Chen dev_err(&pdev->dev, "failed to register DAI: %d\n", ret); 106643d24e76SNicolin Chen return ret; 106743d24e76SNicolin Chen } 106843d24e76SNicolin Chen 10697ccafa2bSShengjiu Wang tasklet_init(&esai_priv->task, fsl_esai_hw_reset, 10707ccafa2bSShengjiu Wang (unsigned long)esai_priv); 10717ccafa2bSShengjiu Wang 1072b2d337d8SS.j. Wang pm_runtime_enable(&pdev->dev); 1073b2d337d8SS.j. Wang 1074b2d337d8SS.j. Wang regcache_cache_only(esai_priv->regmap, true); 1075b2d337d8SS.j. Wang 10760d69e0ddSShengjiu Wang ret = imx_pcm_dma_init(pdev, IMX_ESAI_DMABUF_SIZE); 107743d24e76SNicolin Chen if (ret) 107843d24e76SNicolin Chen dev_err(&pdev->dev, "failed to init imx pcm dma: %d\n", ret); 107943d24e76SNicolin Chen 108043d24e76SNicolin Chen return ret; 108143d24e76SNicolin Chen } 108243d24e76SNicolin Chen 1083b2d337d8SS.j. Wang static int fsl_esai_remove(struct platform_device *pdev) 1084b2d337d8SS.j. Wang { 10857ccafa2bSShengjiu Wang struct fsl_esai *esai_priv = platform_get_drvdata(pdev); 10867ccafa2bSShengjiu Wang 1087b2d337d8SS.j. Wang pm_runtime_disable(&pdev->dev); 10887ccafa2bSShengjiu Wang tasklet_kill(&esai_priv->task); 1089b2d337d8SS.j. Wang 1090b2d337d8SS.j. Wang return 0; 1091b2d337d8SS.j. Wang } 1092b2d337d8SS.j. Wang 109343d24e76SNicolin Chen static const struct of_device_id fsl_esai_dt_ids[] = { 1094*6878e752SShengjiu Wang { .compatible = "fsl,imx35-esai", .data = &fsl_esai_imx35 }, 1095*6878e752SShengjiu Wang { .compatible = "fsl,vf610-esai", .data = &fsl_esai_vf610 }, 1096*6878e752SShengjiu Wang { .compatible = "fsl,imx6ull-esai", .data = &fsl_esai_imx6ull }, 109743d24e76SNicolin Chen {} 109843d24e76SNicolin Chen }; 109943d24e76SNicolin Chen MODULE_DEVICE_TABLE(of, fsl_esai_dt_ids); 110043d24e76SNicolin Chen 1101b2d337d8SS.j. Wang #ifdef CONFIG_PM 1102b2d337d8SS.j. Wang static int fsl_esai_runtime_resume(struct device *dev) 1103c64c6076SZidan Wang { 1104c64c6076SZidan Wang struct fsl_esai *esai = dev_get_drvdata(dev); 1105c64c6076SZidan Wang int ret; 1106c64c6076SZidan Wang 1107b2d337d8SS.j. Wang /* 1108b2d337d8SS.j. Wang * Some platforms might use the same bit to gate all three or two of 1109b2d337d8SS.j. Wang * clocks, so keep all clocks open/close at the same time for safety 1110b2d337d8SS.j. Wang */ 1111b2d337d8SS.j. Wang ret = clk_prepare_enable(esai->coreclk); 1112b2d337d8SS.j. Wang if (ret) 1113b2d337d8SS.j. Wang return ret; 1114b2d337d8SS.j. Wang if (!IS_ERR(esai->spbaclk)) { 1115b2d337d8SS.j. Wang ret = clk_prepare_enable(esai->spbaclk); 1116b2d337d8SS.j. Wang if (ret) 1117b2d337d8SS.j. Wang goto err_spbaclk; 1118b2d337d8SS.j. Wang } 1119b2d337d8SS.j. Wang if (!IS_ERR(esai->extalclk)) { 1120b2d337d8SS.j. Wang ret = clk_prepare_enable(esai->extalclk); 1121b2d337d8SS.j. Wang if (ret) 1122b2d337d8SS.j. Wang goto err_extalclk; 1123b2d337d8SS.j. Wang } 1124b2d337d8SS.j. Wang if (!IS_ERR(esai->fsysclk)) { 1125b2d337d8SS.j. Wang ret = clk_prepare_enable(esai->fsysclk); 1126b2d337d8SS.j. Wang if (ret) 1127b2d337d8SS.j. Wang goto err_fsysclk; 1128b2d337d8SS.j. Wang } 1129b2d337d8SS.j. Wang 1130c64c6076SZidan Wang regcache_cache_only(esai->regmap, false); 1131c64c6076SZidan Wang 11325be6155bSShengjiu Wang ret = fsl_esai_register_restore(esai); 1133c64c6076SZidan Wang if (ret) 1134b2d337d8SS.j. Wang goto err_regcache_sync; 1135c64c6076SZidan Wang 1136c64c6076SZidan Wang return 0; 1137b2d337d8SS.j. Wang 1138b2d337d8SS.j. Wang err_regcache_sync: 1139b2d337d8SS.j. Wang if (!IS_ERR(esai->fsysclk)) 1140b2d337d8SS.j. Wang clk_disable_unprepare(esai->fsysclk); 1141b2d337d8SS.j. Wang err_fsysclk: 1142b2d337d8SS.j. Wang if (!IS_ERR(esai->extalclk)) 1143b2d337d8SS.j. Wang clk_disable_unprepare(esai->extalclk); 1144b2d337d8SS.j. Wang err_extalclk: 1145b2d337d8SS.j. Wang if (!IS_ERR(esai->spbaclk)) 1146b2d337d8SS.j. Wang clk_disable_unprepare(esai->spbaclk); 1147b2d337d8SS.j. Wang err_spbaclk: 1148b2d337d8SS.j. Wang clk_disable_unprepare(esai->coreclk); 1149b2d337d8SS.j. Wang 1150b2d337d8SS.j. Wang return ret; 1151c64c6076SZidan Wang } 1152b2d337d8SS.j. Wang 1153b2d337d8SS.j. Wang static int fsl_esai_runtime_suspend(struct device *dev) 1154b2d337d8SS.j. Wang { 1155b2d337d8SS.j. Wang struct fsl_esai *esai = dev_get_drvdata(dev); 1156b2d337d8SS.j. Wang 1157b2d337d8SS.j. Wang regcache_cache_only(esai->regmap, true); 1158b2d337d8SS.j. Wang 1159b2d337d8SS.j. Wang if (!IS_ERR(esai->fsysclk)) 1160b2d337d8SS.j. Wang clk_disable_unprepare(esai->fsysclk); 1161b2d337d8SS.j. Wang if (!IS_ERR(esai->extalclk)) 1162b2d337d8SS.j. Wang clk_disable_unprepare(esai->extalclk); 1163b2d337d8SS.j. Wang if (!IS_ERR(esai->spbaclk)) 1164b2d337d8SS.j. Wang clk_disable_unprepare(esai->spbaclk); 1165b2d337d8SS.j. Wang clk_disable_unprepare(esai->coreclk); 1166b2d337d8SS.j. Wang 1167b2d337d8SS.j. Wang return 0; 1168b2d337d8SS.j. Wang } 1169b2d337d8SS.j. Wang #endif /* CONFIG_PM */ 1170c64c6076SZidan Wang 1171c64c6076SZidan Wang static const struct dev_pm_ops fsl_esai_pm_ops = { 1172b2d337d8SS.j. Wang SET_RUNTIME_PM_OPS(fsl_esai_runtime_suspend, 1173b2d337d8SS.j. Wang fsl_esai_runtime_resume, 1174b2d337d8SS.j. Wang NULL) 1175b2d337d8SS.j. Wang SET_SYSTEM_SLEEP_PM_OPS(pm_runtime_force_suspend, 1176b2d337d8SS.j. Wang pm_runtime_force_resume) 1177c64c6076SZidan Wang }; 1178c64c6076SZidan Wang 117943d24e76SNicolin Chen static struct platform_driver fsl_esai_driver = { 118043d24e76SNicolin Chen .probe = fsl_esai_probe, 1181b2d337d8SS.j. Wang .remove = fsl_esai_remove, 118243d24e76SNicolin Chen .driver = { 118343d24e76SNicolin Chen .name = "fsl-esai-dai", 1184c64c6076SZidan Wang .pm = &fsl_esai_pm_ops, 118543d24e76SNicolin Chen .of_match_table = fsl_esai_dt_ids, 118643d24e76SNicolin Chen }, 118743d24e76SNicolin Chen }; 118843d24e76SNicolin Chen 118943d24e76SNicolin Chen module_platform_driver(fsl_esai_driver); 119043d24e76SNicolin Chen 119143d24e76SNicolin Chen MODULE_AUTHOR("Freescale Semiconductor, Inc."); 119243d24e76SNicolin Chen MODULE_DESCRIPTION("Freescale ESAI CPU DAI driver"); 119343d24e76SNicolin Chen MODULE_LICENSE("GPL v2"); 119443d24e76SNicolin Chen MODULE_ALIAS("platform:fsl-esai-dai"); 1195