143d24e76SNicolin Chen /* 243d24e76SNicolin Chen * Freescale ESAI ALSA SoC Digital Audio Interface (DAI) driver 343d24e76SNicolin Chen * 443d24e76SNicolin Chen * Copyright (C) 2014 Freescale Semiconductor, Inc. 543d24e76SNicolin Chen * 643d24e76SNicolin Chen * This file is licensed under the terms of the GNU General Public License 743d24e76SNicolin Chen * version 2. This program is licensed "as is" without any warranty of any 843d24e76SNicolin Chen * kind, whether express or implied. 943d24e76SNicolin Chen */ 1043d24e76SNicolin Chen 1143d24e76SNicolin Chen #include <linux/clk.h> 1243d24e76SNicolin Chen #include <linux/dmaengine.h> 1343d24e76SNicolin Chen #include <linux/module.h> 1443d24e76SNicolin Chen #include <linux/of_irq.h> 1543d24e76SNicolin Chen #include <linux/of_platform.h> 1643d24e76SNicolin Chen #include <sound/dmaengine_pcm.h> 1743d24e76SNicolin Chen #include <sound/pcm_params.h> 1843d24e76SNicolin Chen 1943d24e76SNicolin Chen #include "fsl_esai.h" 2043d24e76SNicolin Chen #include "imx-pcm.h" 2143d24e76SNicolin Chen 2243d24e76SNicolin Chen #define FSL_ESAI_FORMATS (SNDRV_PCM_FMTBIT_S8 | \ 2343d24e76SNicolin Chen SNDRV_PCM_FMTBIT_S16_LE | \ 2443d24e76SNicolin Chen SNDRV_PCM_FMTBIT_S20_3LE | \ 2543d24e76SNicolin Chen SNDRV_PCM_FMTBIT_S24_LE) 2643d24e76SNicolin Chen 2743d24e76SNicolin Chen /** 2843d24e76SNicolin Chen * fsl_esai: ESAI private data 2943d24e76SNicolin Chen * 3043d24e76SNicolin Chen * @dma_params_rx: DMA parameters for receive channel 3143d24e76SNicolin Chen * @dma_params_tx: DMA parameters for transmit channel 3243d24e76SNicolin Chen * @pdev: platform device pointer 3343d24e76SNicolin Chen * @regmap: regmap handler 3443d24e76SNicolin Chen * @coreclk: clock source to access register 3543d24e76SNicolin Chen * @extalclk: esai clock source to derive HCK, SCK and FS 3643d24e76SNicolin Chen * @fsysclk: system clock source to derive HCK, SCK and FS 37a2a4d604SShengjiu Wang * @spbaclk: SPBA clock (optional, depending on SoC design) 3843d24e76SNicolin Chen * @fifo_depth: depth of tx/rx FIFO 3943d24e76SNicolin Chen * @slot_width: width of each DAI slot 40de0d712aSShengjiu Wang * @slots: number of slots 4143d24e76SNicolin Chen * @hck_rate: clock rate of desired HCKx clock 42f975ca46SNicolin Chen * @sck_rate: clock rate of desired SCKx clock 43f975ca46SNicolin Chen * @hck_dir: the direction of HCKx pads 4443d24e76SNicolin Chen * @sck_div: if using PSR/PM dividers for SCKx clock 4543d24e76SNicolin Chen * @slave_mode: if fully using DAI slave mode 4643d24e76SNicolin Chen * @synchronous: if using tx/rx synchronous mode 4743d24e76SNicolin Chen * @name: driver name 4843d24e76SNicolin Chen */ 4943d24e76SNicolin Chen struct fsl_esai { 5043d24e76SNicolin Chen struct snd_dmaengine_dai_dma_data dma_params_rx; 5143d24e76SNicolin Chen struct snd_dmaengine_dai_dma_data dma_params_tx; 5243d24e76SNicolin Chen struct platform_device *pdev; 5343d24e76SNicolin Chen struct regmap *regmap; 5443d24e76SNicolin Chen struct clk *coreclk; 5543d24e76SNicolin Chen struct clk *extalclk; 5643d24e76SNicolin Chen struct clk *fsysclk; 57a2a4d604SShengjiu Wang struct clk *spbaclk; 5843d24e76SNicolin Chen u32 fifo_depth; 5943d24e76SNicolin Chen u32 slot_width; 60de0d712aSShengjiu Wang u32 slots; 6143d24e76SNicolin Chen u32 hck_rate[2]; 62f975ca46SNicolin Chen u32 sck_rate[2]; 63f975ca46SNicolin Chen bool hck_dir[2]; 6443d24e76SNicolin Chen bool sck_div[2]; 6543d24e76SNicolin Chen bool slave_mode; 6643d24e76SNicolin Chen bool synchronous; 6743d24e76SNicolin Chen char name[32]; 6843d24e76SNicolin Chen }; 6943d24e76SNicolin Chen 7043d24e76SNicolin Chen static irqreturn_t esai_isr(int irq, void *devid) 7143d24e76SNicolin Chen { 7243d24e76SNicolin Chen struct fsl_esai *esai_priv = (struct fsl_esai *)devid; 7343d24e76SNicolin Chen struct platform_device *pdev = esai_priv->pdev; 7443d24e76SNicolin Chen u32 esr; 7543d24e76SNicolin Chen 7643d24e76SNicolin Chen regmap_read(esai_priv->regmap, REG_ESAI_ESR, &esr); 7743d24e76SNicolin Chen 7843d24e76SNicolin Chen if (esr & ESAI_ESR_TINIT_MASK) 793bcc8656SColin Ian King dev_dbg(&pdev->dev, "isr: Transmission Initialized\n"); 8043d24e76SNicolin Chen 8143d24e76SNicolin Chen if (esr & ESAI_ESR_RFF_MASK) 8243d24e76SNicolin Chen dev_warn(&pdev->dev, "isr: Receiving overrun\n"); 8343d24e76SNicolin Chen 8443d24e76SNicolin Chen if (esr & ESAI_ESR_TFE_MASK) 853bcc8656SColin Ian King dev_warn(&pdev->dev, "isr: Transmission underrun\n"); 8643d24e76SNicolin Chen 8743d24e76SNicolin Chen if (esr & ESAI_ESR_TLS_MASK) 8843d24e76SNicolin Chen dev_dbg(&pdev->dev, "isr: Just transmitted the last slot\n"); 8943d24e76SNicolin Chen 9043d24e76SNicolin Chen if (esr & ESAI_ESR_TDE_MASK) 913bcc8656SColin Ian King dev_dbg(&pdev->dev, "isr: Transmission data exception\n"); 9243d24e76SNicolin Chen 9343d24e76SNicolin Chen if (esr & ESAI_ESR_TED_MASK) 9443d24e76SNicolin Chen dev_dbg(&pdev->dev, "isr: Transmitting even slots\n"); 9543d24e76SNicolin Chen 9643d24e76SNicolin Chen if (esr & ESAI_ESR_TD_MASK) 9743d24e76SNicolin Chen dev_dbg(&pdev->dev, "isr: Transmitting data\n"); 9843d24e76SNicolin Chen 9943d24e76SNicolin Chen if (esr & ESAI_ESR_RLS_MASK) 10043d24e76SNicolin Chen dev_dbg(&pdev->dev, "isr: Just received the last slot\n"); 10143d24e76SNicolin Chen 10243d24e76SNicolin Chen if (esr & ESAI_ESR_RDE_MASK) 10343d24e76SNicolin Chen dev_dbg(&pdev->dev, "isr: Receiving data exception\n"); 10443d24e76SNicolin Chen 10543d24e76SNicolin Chen if (esr & ESAI_ESR_RED_MASK) 10643d24e76SNicolin Chen dev_dbg(&pdev->dev, "isr: Receiving even slots\n"); 10743d24e76SNicolin Chen 10843d24e76SNicolin Chen if (esr & ESAI_ESR_RD_MASK) 10943d24e76SNicolin Chen dev_dbg(&pdev->dev, "isr: Receiving data\n"); 11043d24e76SNicolin Chen 11143d24e76SNicolin Chen return IRQ_HANDLED; 11243d24e76SNicolin Chen } 11343d24e76SNicolin Chen 11443d24e76SNicolin Chen /** 11543d24e76SNicolin Chen * This function is used to calculate the divisors of psr, pm, fp and it is 11643d24e76SNicolin Chen * supposed to be called in set_dai_sysclk() and set_bclk(). 11743d24e76SNicolin Chen * 11843d24e76SNicolin Chen * @ratio: desired overall ratio for the paticipating dividers 11943d24e76SNicolin Chen * @usefp: for HCK setting, there is no need to set fp divider 12043d24e76SNicolin Chen * @fp: bypass other dividers by setting fp directly if fp != 0 12143d24e76SNicolin Chen * @tx: current setting is for playback or capture 12243d24e76SNicolin Chen */ 12343d24e76SNicolin Chen static int fsl_esai_divisor_cal(struct snd_soc_dai *dai, bool tx, u32 ratio, 12443d24e76SNicolin Chen bool usefp, u32 fp) 12543d24e76SNicolin Chen { 12643d24e76SNicolin Chen struct fsl_esai *esai_priv = snd_soc_dai_get_drvdata(dai); 12743d24e76SNicolin Chen u32 psr, pm = 999, maxfp, prod, sub, savesub, i, j; 12843d24e76SNicolin Chen 12943d24e76SNicolin Chen maxfp = usefp ? 16 : 1; 13043d24e76SNicolin Chen 13143d24e76SNicolin Chen if (usefp && fp) 13243d24e76SNicolin Chen goto out_fp; 13343d24e76SNicolin Chen 13443d24e76SNicolin Chen if (ratio > 2 * 8 * 256 * maxfp || ratio < 2) { 13543d24e76SNicolin Chen dev_err(dai->dev, "the ratio is out of range (2 ~ %d)\n", 13643d24e76SNicolin Chen 2 * 8 * 256 * maxfp); 13743d24e76SNicolin Chen return -EINVAL; 13843d24e76SNicolin Chen } else if (ratio % 2) { 13943d24e76SNicolin Chen dev_err(dai->dev, "the raio must be even if using upper divider\n"); 14043d24e76SNicolin Chen return -EINVAL; 14143d24e76SNicolin Chen } 14243d24e76SNicolin Chen 14343d24e76SNicolin Chen ratio /= 2; 14443d24e76SNicolin Chen 14543d24e76SNicolin Chen psr = ratio <= 256 * maxfp ? ESAI_xCCR_xPSR_BYPASS : ESAI_xCCR_xPSR_DIV8; 14643d24e76SNicolin Chen 14743d24e76SNicolin Chen /* Set the max fluctuation -- 0.1% of the max devisor */ 14843d24e76SNicolin Chen savesub = (psr ? 1 : 8) * 256 * maxfp / 1000; 14943d24e76SNicolin Chen 15043d24e76SNicolin Chen /* Find the best value for PM */ 15143d24e76SNicolin Chen for (i = 1; i <= 256; i++) { 15243d24e76SNicolin Chen for (j = 1; j <= maxfp; j++) { 15343d24e76SNicolin Chen /* PSR (1 or 8) * PM (1 ~ 256) * FP (1 ~ 16) */ 15443d24e76SNicolin Chen prod = (psr ? 1 : 8) * i * j; 15543d24e76SNicolin Chen 15643d24e76SNicolin Chen if (prod == ratio) 15743d24e76SNicolin Chen sub = 0; 15843d24e76SNicolin Chen else if (prod / ratio == 1) 15943d24e76SNicolin Chen sub = prod - ratio; 16043d24e76SNicolin Chen else if (ratio / prod == 1) 16143d24e76SNicolin Chen sub = ratio - prod; 16243d24e76SNicolin Chen else 16343d24e76SNicolin Chen continue; 16443d24e76SNicolin Chen 16543d24e76SNicolin Chen /* Calculate the fraction */ 16643d24e76SNicolin Chen sub = sub * 1000 / ratio; 16743d24e76SNicolin Chen if (sub < savesub) { 16843d24e76SNicolin Chen savesub = sub; 16943d24e76SNicolin Chen pm = i; 17043d24e76SNicolin Chen fp = j; 17143d24e76SNicolin Chen } 17243d24e76SNicolin Chen 17343d24e76SNicolin Chen /* We are lucky */ 17443d24e76SNicolin Chen if (savesub == 0) 17543d24e76SNicolin Chen goto out; 17643d24e76SNicolin Chen } 17743d24e76SNicolin Chen } 17843d24e76SNicolin Chen 17943d24e76SNicolin Chen if (pm == 999) { 18043d24e76SNicolin Chen dev_err(dai->dev, "failed to calculate proper divisors\n"); 18143d24e76SNicolin Chen return -EINVAL; 18243d24e76SNicolin Chen } 18343d24e76SNicolin Chen 18443d24e76SNicolin Chen out: 18543d24e76SNicolin Chen regmap_update_bits(esai_priv->regmap, REG_ESAI_xCCR(tx), 18643d24e76SNicolin Chen ESAI_xCCR_xPSR_MASK | ESAI_xCCR_xPM_MASK, 18743d24e76SNicolin Chen psr | ESAI_xCCR_xPM(pm)); 18843d24e76SNicolin Chen 18943d24e76SNicolin Chen out_fp: 19043d24e76SNicolin Chen /* Bypass fp if not being required */ 19143d24e76SNicolin Chen if (maxfp <= 1) 19243d24e76SNicolin Chen return 0; 19343d24e76SNicolin Chen 19443d24e76SNicolin Chen regmap_update_bits(esai_priv->regmap, REG_ESAI_xCCR(tx), 19543d24e76SNicolin Chen ESAI_xCCR_xFP_MASK, ESAI_xCCR_xFP(fp)); 19643d24e76SNicolin Chen 19743d24e76SNicolin Chen return 0; 19843d24e76SNicolin Chen } 19943d24e76SNicolin Chen 20043d24e76SNicolin Chen /** 20143d24e76SNicolin Chen * This function mainly configures the clock frequency of MCLK (HCKT/HCKR) 20243d24e76SNicolin Chen * 20343d24e76SNicolin Chen * @Parameters: 20443d24e76SNicolin Chen * clk_id: The clock source of HCKT/HCKR 20543d24e76SNicolin Chen * (Input from outside; output from inside, FSYS or EXTAL) 20643d24e76SNicolin Chen * freq: The required clock rate of HCKT/HCKR 20743d24e76SNicolin Chen * dir: The clock direction of HCKT/HCKR 20843d24e76SNicolin Chen * 20943d24e76SNicolin Chen * Note: If the direction is input, we do not care about clk_id. 21043d24e76SNicolin Chen */ 21143d24e76SNicolin Chen static int fsl_esai_set_dai_sysclk(struct snd_soc_dai *dai, int clk_id, 21243d24e76SNicolin Chen unsigned int freq, int dir) 21343d24e76SNicolin Chen { 21443d24e76SNicolin Chen struct fsl_esai *esai_priv = snd_soc_dai_get_drvdata(dai); 21543d24e76SNicolin Chen struct clk *clksrc = esai_priv->extalclk; 21643d24e76SNicolin Chen bool tx = clk_id <= ESAI_HCKT_EXTAL; 21743d24e76SNicolin Chen bool in = dir == SND_SOC_CLOCK_IN; 2183e185238SXiubo Li u32 ratio, ecr = 0; 21943d24e76SNicolin Chen unsigned long clk_rate; 2203e185238SXiubo Li int ret; 22143d24e76SNicolin Chen 222f975ca46SNicolin Chen /* Bypass divider settings if the requirement doesn't change */ 223f975ca46SNicolin Chen if (freq == esai_priv->hck_rate[tx] && dir == esai_priv->hck_dir[tx]) 224f975ca46SNicolin Chen return 0; 22543d24e76SNicolin Chen 22643d24e76SNicolin Chen /* sck_div can be only bypassed if ETO/ERO=0 and SNC_SOC_CLOCK_OUT */ 22743d24e76SNicolin Chen esai_priv->sck_div[tx] = true; 22843d24e76SNicolin Chen 22943d24e76SNicolin Chen /* Set the direction of HCKT/HCKR pins */ 23043d24e76SNicolin Chen regmap_update_bits(esai_priv->regmap, REG_ESAI_xCCR(tx), 23143d24e76SNicolin Chen ESAI_xCCR_xHCKD, in ? 0 : ESAI_xCCR_xHCKD); 23243d24e76SNicolin Chen 23343d24e76SNicolin Chen if (in) 23443d24e76SNicolin Chen goto out; 23543d24e76SNicolin Chen 23643d24e76SNicolin Chen switch (clk_id) { 23743d24e76SNicolin Chen case ESAI_HCKT_FSYS: 23843d24e76SNicolin Chen case ESAI_HCKR_FSYS: 23943d24e76SNicolin Chen clksrc = esai_priv->fsysclk; 24043d24e76SNicolin Chen break; 24143d24e76SNicolin Chen case ESAI_HCKT_EXTAL: 24243d24e76SNicolin Chen ecr |= ESAI_ECR_ETI; 24343d24e76SNicolin Chen case ESAI_HCKR_EXTAL: 24443d24e76SNicolin Chen ecr |= ESAI_ECR_ERI; 24543d24e76SNicolin Chen break; 24643d24e76SNicolin Chen default: 24743d24e76SNicolin Chen return -EINVAL; 24843d24e76SNicolin Chen } 24943d24e76SNicolin Chen 25043d24e76SNicolin Chen if (IS_ERR(clksrc)) { 25143d24e76SNicolin Chen dev_err(dai->dev, "no assigned %s clock\n", 25243d24e76SNicolin Chen clk_id % 2 ? "extal" : "fsys"); 25343d24e76SNicolin Chen return PTR_ERR(clksrc); 25443d24e76SNicolin Chen } 25543d24e76SNicolin Chen clk_rate = clk_get_rate(clksrc); 25643d24e76SNicolin Chen 25743d24e76SNicolin Chen ratio = clk_rate / freq; 25843d24e76SNicolin Chen if (ratio * freq > clk_rate) 25943d24e76SNicolin Chen ret = ratio * freq - clk_rate; 26043d24e76SNicolin Chen else if (ratio * freq < clk_rate) 26143d24e76SNicolin Chen ret = clk_rate - ratio * freq; 26243d24e76SNicolin Chen else 26343d24e76SNicolin Chen ret = 0; 26443d24e76SNicolin Chen 26543d24e76SNicolin Chen /* Block if clock source can not be divided into the required rate */ 26643d24e76SNicolin Chen if (ret != 0 && clk_rate / ret < 1000) { 26743d24e76SNicolin Chen dev_err(dai->dev, "failed to derive required HCK%c rate\n", 26843d24e76SNicolin Chen tx ? 'T' : 'R'); 26943d24e76SNicolin Chen return -EINVAL; 27043d24e76SNicolin Chen } 27143d24e76SNicolin Chen 27257ebbcafSNicolin Chen /* Only EXTAL source can be output directly without using PSR and PM */ 27357ebbcafSNicolin Chen if (ratio == 1 && clksrc == esai_priv->extalclk) { 27443d24e76SNicolin Chen /* Bypass all the dividers if not being needed */ 27543d24e76SNicolin Chen ecr |= tx ? ESAI_ECR_ETO : ESAI_ECR_ERO; 27643d24e76SNicolin Chen goto out; 27757ebbcafSNicolin Chen } else if (ratio < 2) { 27857ebbcafSNicolin Chen /* The ratio should be no less than 2 if using other sources */ 27957ebbcafSNicolin Chen dev_err(dai->dev, "failed to derive required HCK%c rate\n", 28057ebbcafSNicolin Chen tx ? 'T' : 'R'); 28157ebbcafSNicolin Chen return -EINVAL; 28243d24e76SNicolin Chen } 28343d24e76SNicolin Chen 28443d24e76SNicolin Chen ret = fsl_esai_divisor_cal(dai, tx, ratio, false, 0); 28543d24e76SNicolin Chen if (ret) 28643d24e76SNicolin Chen return ret; 28743d24e76SNicolin Chen 28843d24e76SNicolin Chen esai_priv->sck_div[tx] = false; 28943d24e76SNicolin Chen 29043d24e76SNicolin Chen out: 291f975ca46SNicolin Chen esai_priv->hck_dir[tx] = dir; 29243d24e76SNicolin Chen esai_priv->hck_rate[tx] = freq; 29343d24e76SNicolin Chen 29443d24e76SNicolin Chen regmap_update_bits(esai_priv->regmap, REG_ESAI_ECR, 29543d24e76SNicolin Chen tx ? ESAI_ECR_ETI | ESAI_ECR_ETO : 29643d24e76SNicolin Chen ESAI_ECR_ERI | ESAI_ECR_ERO, ecr); 29743d24e76SNicolin Chen 29843d24e76SNicolin Chen return 0; 29943d24e76SNicolin Chen } 30043d24e76SNicolin Chen 30143d24e76SNicolin Chen /** 30243d24e76SNicolin Chen * This function configures the related dividers according to the bclk rate 30343d24e76SNicolin Chen */ 30443d24e76SNicolin Chen static int fsl_esai_set_bclk(struct snd_soc_dai *dai, bool tx, u32 freq) 30543d24e76SNicolin Chen { 30643d24e76SNicolin Chen struct fsl_esai *esai_priv = snd_soc_dai_get_drvdata(dai); 30743d24e76SNicolin Chen u32 hck_rate = esai_priv->hck_rate[tx]; 30843d24e76SNicolin Chen u32 sub, ratio = hck_rate / freq; 309f975ca46SNicolin Chen int ret; 31043d24e76SNicolin Chen 311f975ca46SNicolin Chen /* Don't apply for fully slave mode or unchanged bclk */ 312f975ca46SNicolin Chen if (esai_priv->slave_mode || esai_priv->sck_rate[tx] == freq) 31343d24e76SNicolin Chen return 0; 31443d24e76SNicolin Chen 31543d24e76SNicolin Chen if (ratio * freq > hck_rate) 31643d24e76SNicolin Chen sub = ratio * freq - hck_rate; 31743d24e76SNicolin Chen else if (ratio * freq < hck_rate) 31843d24e76SNicolin Chen sub = hck_rate - ratio * freq; 31943d24e76SNicolin Chen else 32043d24e76SNicolin Chen sub = 0; 32143d24e76SNicolin Chen 32243d24e76SNicolin Chen /* Block if clock source can not be divided into the required rate */ 32343d24e76SNicolin Chen if (sub != 0 && hck_rate / sub < 1000) { 32443d24e76SNicolin Chen dev_err(dai->dev, "failed to derive required SCK%c rate\n", 32543d24e76SNicolin Chen tx ? 'T' : 'R'); 32643d24e76SNicolin Chen return -EINVAL; 32743d24e76SNicolin Chen } 32843d24e76SNicolin Chen 32989e47f62SNicolin Chen /* The ratio should be contented by FP alone if bypassing PM and PSR */ 33089e47f62SNicolin Chen if (!esai_priv->sck_div[tx] && (ratio > 16 || ratio == 0)) { 33143d24e76SNicolin Chen dev_err(dai->dev, "the ratio is out of range (1 ~ 16)\n"); 33243d24e76SNicolin Chen return -EINVAL; 33343d24e76SNicolin Chen } 33443d24e76SNicolin Chen 335f975ca46SNicolin Chen ret = fsl_esai_divisor_cal(dai, tx, ratio, true, 33643d24e76SNicolin Chen esai_priv->sck_div[tx] ? 0 : ratio); 337f975ca46SNicolin Chen if (ret) 338f975ca46SNicolin Chen return ret; 339f975ca46SNicolin Chen 340f975ca46SNicolin Chen /* Save current bclk rate */ 341f975ca46SNicolin Chen esai_priv->sck_rate[tx] = freq; 342f975ca46SNicolin Chen 343f975ca46SNicolin Chen return 0; 34443d24e76SNicolin Chen } 34543d24e76SNicolin Chen 34643d24e76SNicolin Chen static int fsl_esai_set_dai_tdm_slot(struct snd_soc_dai *dai, u32 tx_mask, 34743d24e76SNicolin Chen u32 rx_mask, int slots, int slot_width) 34843d24e76SNicolin Chen { 34943d24e76SNicolin Chen struct fsl_esai *esai_priv = snd_soc_dai_get_drvdata(dai); 35043d24e76SNicolin Chen 35143d24e76SNicolin Chen regmap_update_bits(esai_priv->regmap, REG_ESAI_TCCR, 35243d24e76SNicolin Chen ESAI_xCCR_xDC_MASK, ESAI_xCCR_xDC(slots)); 35343d24e76SNicolin Chen 35443d24e76SNicolin Chen regmap_update_bits(esai_priv->regmap, REG_ESAI_TSMA, 35543d24e76SNicolin Chen ESAI_xSMA_xS_MASK, ESAI_xSMA_xS(tx_mask)); 35643d24e76SNicolin Chen regmap_update_bits(esai_priv->regmap, REG_ESAI_TSMB, 357236014acSXiubo Li ESAI_xSMB_xS_MASK, ESAI_xSMB_xS(tx_mask)); 35843d24e76SNicolin Chen 35943d24e76SNicolin Chen regmap_update_bits(esai_priv->regmap, REG_ESAI_RCCR, 36043d24e76SNicolin Chen ESAI_xCCR_xDC_MASK, ESAI_xCCR_xDC(slots)); 36143d24e76SNicolin Chen 36243d24e76SNicolin Chen regmap_update_bits(esai_priv->regmap, REG_ESAI_RSMA, 36343d24e76SNicolin Chen ESAI_xSMA_xS_MASK, ESAI_xSMA_xS(rx_mask)); 36443d24e76SNicolin Chen regmap_update_bits(esai_priv->regmap, REG_ESAI_RSMB, 365236014acSXiubo Li ESAI_xSMB_xS_MASK, ESAI_xSMB_xS(rx_mask)); 36643d24e76SNicolin Chen 36743d24e76SNicolin Chen esai_priv->slot_width = slot_width; 368de0d712aSShengjiu Wang esai_priv->slots = slots; 36943d24e76SNicolin Chen 37043d24e76SNicolin Chen return 0; 37143d24e76SNicolin Chen } 37243d24e76SNicolin Chen 37343d24e76SNicolin Chen static int fsl_esai_set_dai_fmt(struct snd_soc_dai *dai, unsigned int fmt) 37443d24e76SNicolin Chen { 37543d24e76SNicolin Chen struct fsl_esai *esai_priv = snd_soc_dai_get_drvdata(dai); 37643d24e76SNicolin Chen u32 xcr = 0, xccr = 0, mask; 37743d24e76SNicolin Chen 37843d24e76SNicolin Chen /* DAI mode */ 37943d24e76SNicolin Chen switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) { 38043d24e76SNicolin Chen case SND_SOC_DAIFMT_I2S: 38143d24e76SNicolin Chen /* Data on rising edge of bclk, frame low, 1clk before data */ 38243d24e76SNicolin Chen xcr |= ESAI_xCR_xFSR; 38343d24e76SNicolin Chen xccr |= ESAI_xCCR_xFSP | ESAI_xCCR_xCKP | ESAI_xCCR_xHCKP; 38443d24e76SNicolin Chen break; 38543d24e76SNicolin Chen case SND_SOC_DAIFMT_LEFT_J: 38643d24e76SNicolin Chen /* Data on rising edge of bclk, frame high */ 38743d24e76SNicolin Chen xccr |= ESAI_xCCR_xCKP | ESAI_xCCR_xHCKP; 38843d24e76SNicolin Chen break; 38943d24e76SNicolin Chen case SND_SOC_DAIFMT_RIGHT_J: 39043d24e76SNicolin Chen /* Data on rising edge of bclk, frame high, right aligned */ 39143d24e76SNicolin Chen xccr |= ESAI_xCCR_xCKP | ESAI_xCCR_xHCKP | ESAI_xCR_xWA; 39243d24e76SNicolin Chen break; 39343d24e76SNicolin Chen case SND_SOC_DAIFMT_DSP_A: 39443d24e76SNicolin Chen /* Data on rising edge of bclk, frame high, 1clk before data */ 39543d24e76SNicolin Chen xcr |= ESAI_xCR_xFSL | ESAI_xCR_xFSR; 39643d24e76SNicolin Chen xccr |= ESAI_xCCR_xCKP | ESAI_xCCR_xHCKP; 39743d24e76SNicolin Chen break; 39843d24e76SNicolin Chen case SND_SOC_DAIFMT_DSP_B: 39943d24e76SNicolin Chen /* Data on rising edge of bclk, frame high */ 40043d24e76SNicolin Chen xcr |= ESAI_xCR_xFSL; 40143d24e76SNicolin Chen xccr |= ESAI_xCCR_xCKP | ESAI_xCCR_xHCKP; 40243d24e76SNicolin Chen break; 40343d24e76SNicolin Chen default: 40443d24e76SNicolin Chen return -EINVAL; 40543d24e76SNicolin Chen } 40643d24e76SNicolin Chen 40743d24e76SNicolin Chen /* DAI clock inversion */ 40843d24e76SNicolin Chen switch (fmt & SND_SOC_DAIFMT_INV_MASK) { 40943d24e76SNicolin Chen case SND_SOC_DAIFMT_NB_NF: 41043d24e76SNicolin Chen /* Nothing to do for both normal cases */ 41143d24e76SNicolin Chen break; 41243d24e76SNicolin Chen case SND_SOC_DAIFMT_IB_NF: 41343d24e76SNicolin Chen /* Invert bit clock */ 41443d24e76SNicolin Chen xccr ^= ESAI_xCCR_xCKP | ESAI_xCCR_xHCKP; 41543d24e76SNicolin Chen break; 41643d24e76SNicolin Chen case SND_SOC_DAIFMT_NB_IF: 41743d24e76SNicolin Chen /* Invert frame clock */ 41843d24e76SNicolin Chen xccr ^= ESAI_xCCR_xFSP; 41943d24e76SNicolin Chen break; 42043d24e76SNicolin Chen case SND_SOC_DAIFMT_IB_IF: 42143d24e76SNicolin Chen /* Invert both clocks */ 42243d24e76SNicolin Chen xccr ^= ESAI_xCCR_xCKP | ESAI_xCCR_xHCKP | ESAI_xCCR_xFSP; 42343d24e76SNicolin Chen break; 42443d24e76SNicolin Chen default: 42543d24e76SNicolin Chen return -EINVAL; 42643d24e76SNicolin Chen } 42743d24e76SNicolin Chen 42843d24e76SNicolin Chen esai_priv->slave_mode = false; 42943d24e76SNicolin Chen 43043d24e76SNicolin Chen /* DAI clock master masks */ 43143d24e76SNicolin Chen switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) { 43243d24e76SNicolin Chen case SND_SOC_DAIFMT_CBM_CFM: 43343d24e76SNicolin Chen esai_priv->slave_mode = true; 43443d24e76SNicolin Chen break; 43543d24e76SNicolin Chen case SND_SOC_DAIFMT_CBS_CFM: 43643d24e76SNicolin Chen xccr |= ESAI_xCCR_xCKD; 43743d24e76SNicolin Chen break; 43843d24e76SNicolin Chen case SND_SOC_DAIFMT_CBM_CFS: 43943d24e76SNicolin Chen xccr |= ESAI_xCCR_xFSD; 44043d24e76SNicolin Chen break; 44143d24e76SNicolin Chen case SND_SOC_DAIFMT_CBS_CFS: 44243d24e76SNicolin Chen xccr |= ESAI_xCCR_xFSD | ESAI_xCCR_xCKD; 44343d24e76SNicolin Chen break; 44443d24e76SNicolin Chen default: 44543d24e76SNicolin Chen return -EINVAL; 44643d24e76SNicolin Chen } 44743d24e76SNicolin Chen 44843d24e76SNicolin Chen mask = ESAI_xCR_xFSL | ESAI_xCR_xFSR; 44943d24e76SNicolin Chen regmap_update_bits(esai_priv->regmap, REG_ESAI_TCR, mask, xcr); 45043d24e76SNicolin Chen regmap_update_bits(esai_priv->regmap, REG_ESAI_RCR, mask, xcr); 45143d24e76SNicolin Chen 45243d24e76SNicolin Chen mask = ESAI_xCCR_xCKP | ESAI_xCCR_xHCKP | ESAI_xCCR_xFSP | 45343d24e76SNicolin Chen ESAI_xCCR_xFSD | ESAI_xCCR_xCKD | ESAI_xCR_xWA; 45443d24e76SNicolin Chen regmap_update_bits(esai_priv->regmap, REG_ESAI_TCCR, mask, xccr); 45543d24e76SNicolin Chen regmap_update_bits(esai_priv->regmap, REG_ESAI_RCCR, mask, xccr); 45643d24e76SNicolin Chen 45743d24e76SNicolin Chen return 0; 45843d24e76SNicolin Chen } 45943d24e76SNicolin Chen 46043d24e76SNicolin Chen static int fsl_esai_startup(struct snd_pcm_substream *substream, 46143d24e76SNicolin Chen struct snd_soc_dai *dai) 46243d24e76SNicolin Chen { 46343d24e76SNicolin Chen struct fsl_esai *esai_priv = snd_soc_dai_get_drvdata(dai); 4643e185238SXiubo Li int ret; 46543d24e76SNicolin Chen 46643d24e76SNicolin Chen /* 46743d24e76SNicolin Chen * Some platforms might use the same bit to gate all three or two of 46843d24e76SNicolin Chen * clocks, so keep all clocks open/close at the same time for safety 46943d24e76SNicolin Chen */ 47033529ec9SFabio Estevam ret = clk_prepare_enable(esai_priv->coreclk); 47133529ec9SFabio Estevam if (ret) 47233529ec9SFabio Estevam return ret; 473a2a4d604SShengjiu Wang if (!IS_ERR(esai_priv->spbaclk)) { 474a2a4d604SShengjiu Wang ret = clk_prepare_enable(esai_priv->spbaclk); 475a2a4d604SShengjiu Wang if (ret) 476a2a4d604SShengjiu Wang goto err_spbaclk; 477a2a4d604SShengjiu Wang } 47833529ec9SFabio Estevam if (!IS_ERR(esai_priv->extalclk)) { 47933529ec9SFabio Estevam ret = clk_prepare_enable(esai_priv->extalclk); 48033529ec9SFabio Estevam if (ret) 48133529ec9SFabio Estevam goto err_extalck; 48233529ec9SFabio Estevam } 48333529ec9SFabio Estevam if (!IS_ERR(esai_priv->fsysclk)) { 48433529ec9SFabio Estevam ret = clk_prepare_enable(esai_priv->fsysclk); 48533529ec9SFabio Estevam if (ret) 48633529ec9SFabio Estevam goto err_fsysclk; 48733529ec9SFabio Estevam } 48843d24e76SNicolin Chen 48943d24e76SNicolin Chen if (!dai->active) { 49043d24e76SNicolin Chen /* Set synchronous mode */ 49143d24e76SNicolin Chen regmap_update_bits(esai_priv->regmap, REG_ESAI_SAICR, 49243d24e76SNicolin Chen ESAI_SAICR_SYNC, esai_priv->synchronous ? 49343d24e76SNicolin Chen ESAI_SAICR_SYNC : 0); 49443d24e76SNicolin Chen 49543d24e76SNicolin Chen /* Set a default slot number -- 2 */ 49643d24e76SNicolin Chen regmap_update_bits(esai_priv->regmap, REG_ESAI_TCCR, 49743d24e76SNicolin Chen ESAI_xCCR_xDC_MASK, ESAI_xCCR_xDC(2)); 49843d24e76SNicolin Chen regmap_update_bits(esai_priv->regmap, REG_ESAI_RCCR, 49943d24e76SNicolin Chen ESAI_xCCR_xDC_MASK, ESAI_xCCR_xDC(2)); 50043d24e76SNicolin Chen } 50143d24e76SNicolin Chen 50243d24e76SNicolin Chen return 0; 50333529ec9SFabio Estevam 50433529ec9SFabio Estevam err_fsysclk: 50533529ec9SFabio Estevam if (!IS_ERR(esai_priv->extalclk)) 50633529ec9SFabio Estevam clk_disable_unprepare(esai_priv->extalclk); 50733529ec9SFabio Estevam err_extalck: 508a2a4d604SShengjiu Wang if (!IS_ERR(esai_priv->spbaclk)) 509a2a4d604SShengjiu Wang clk_disable_unprepare(esai_priv->spbaclk); 510a2a4d604SShengjiu Wang err_spbaclk: 51133529ec9SFabio Estevam clk_disable_unprepare(esai_priv->coreclk); 51233529ec9SFabio Estevam 51333529ec9SFabio Estevam return ret; 51443d24e76SNicolin Chen } 51543d24e76SNicolin Chen 51643d24e76SNicolin Chen static int fsl_esai_hw_params(struct snd_pcm_substream *substream, 51743d24e76SNicolin Chen struct snd_pcm_hw_params *params, 51843d24e76SNicolin Chen struct snd_soc_dai *dai) 51943d24e76SNicolin Chen { 52043d24e76SNicolin Chen struct fsl_esai *esai_priv = snd_soc_dai_get_drvdata(dai); 52143d24e76SNicolin Chen bool tx = substream->stream == SNDRV_PCM_STREAM_PLAYBACK; 5224ca73043SZidan Wang u32 width = params_width(params); 52343d24e76SNicolin Chen u32 channels = params_channels(params); 524de0d712aSShengjiu Wang u32 pins = DIV_ROUND_UP(channels, esai_priv->slots); 52586ea522bSNicolin Chen u32 slot_width = width; 5263e185238SXiubo Li u32 bclk, mask, val; 5273e185238SXiubo Li int ret; 52843d24e76SNicolin Chen 529d8ffcf71SGeert Uytterhoeven /* Override slot_width if being specifically set */ 53086ea522bSNicolin Chen if (esai_priv->slot_width) 53186ea522bSNicolin Chen slot_width = esai_priv->slot_width; 53286ea522bSNicolin Chen 53386ea522bSNicolin Chen bclk = params_rate(params) * slot_width * esai_priv->slots; 53443d24e76SNicolin Chen 53543d24e76SNicolin Chen ret = fsl_esai_set_bclk(dai, tx, bclk); 53643d24e76SNicolin Chen if (ret) 53743d24e76SNicolin Chen return ret; 53843d24e76SNicolin Chen 53943d24e76SNicolin Chen /* Use Normal mode to support monaural audio */ 54043d24e76SNicolin Chen regmap_update_bits(esai_priv->regmap, REG_ESAI_xCR(tx), 54143d24e76SNicolin Chen ESAI_xCR_xMOD_MASK, params_channels(params) > 1 ? 54243d24e76SNicolin Chen ESAI_xCR_xMOD_NETWORK : 0); 54343d24e76SNicolin Chen 54443d24e76SNicolin Chen regmap_update_bits(esai_priv->regmap, REG_ESAI_xFCR(tx), 54543d24e76SNicolin Chen ESAI_xFCR_xFR_MASK, ESAI_xFCR_xFR); 54643d24e76SNicolin Chen 54743d24e76SNicolin Chen mask = ESAI_xFCR_xFR_MASK | ESAI_xFCR_xWA_MASK | ESAI_xFCR_xFWM_MASK | 54843d24e76SNicolin Chen (tx ? ESAI_xFCR_TE_MASK | ESAI_xFCR_TIEN : ESAI_xFCR_RE_MASK); 54943d24e76SNicolin Chen val = ESAI_xFCR_xWA(width) | ESAI_xFCR_xFWM(esai_priv->fifo_depth) | 550de0d712aSShengjiu Wang (tx ? ESAI_xFCR_TE(pins) | ESAI_xFCR_TIEN : ESAI_xFCR_RE(pins)); 55143d24e76SNicolin Chen 55243d24e76SNicolin Chen regmap_update_bits(esai_priv->regmap, REG_ESAI_xFCR(tx), mask, val); 55343d24e76SNicolin Chen 55443d24e76SNicolin Chen mask = ESAI_xCR_xSWS_MASK | (tx ? ESAI_xCR_PADC : 0); 55586ea522bSNicolin Chen val = ESAI_xCR_xSWS(slot_width, width) | (tx ? ESAI_xCR_PADC : 0); 55643d24e76SNicolin Chen 55743d24e76SNicolin Chen regmap_update_bits(esai_priv->regmap, REG_ESAI_xCR(tx), mask, val); 55843d24e76SNicolin Chen 5594f8210f6SNicolin Chen /* Remove ESAI personal reset by configuring ESAI_PCRC and ESAI_PRRC */ 5604f8210f6SNicolin Chen regmap_update_bits(esai_priv->regmap, REG_ESAI_PRRC, 5614f8210f6SNicolin Chen ESAI_PRRC_PDC_MASK, ESAI_PRRC_PDC(ESAI_GPIO)); 5624f8210f6SNicolin Chen regmap_update_bits(esai_priv->regmap, REG_ESAI_PCRC, 5634f8210f6SNicolin Chen ESAI_PCRC_PC_MASK, ESAI_PCRC_PC(ESAI_GPIO)); 56443d24e76SNicolin Chen return 0; 56543d24e76SNicolin Chen } 56643d24e76SNicolin Chen 56743d24e76SNicolin Chen static void fsl_esai_shutdown(struct snd_pcm_substream *substream, 56843d24e76SNicolin Chen struct snd_soc_dai *dai) 56943d24e76SNicolin Chen { 57043d24e76SNicolin Chen struct fsl_esai *esai_priv = snd_soc_dai_get_drvdata(dai); 57143d24e76SNicolin Chen 57243d24e76SNicolin Chen if (!IS_ERR(esai_priv->fsysclk)) 57343d24e76SNicolin Chen clk_disable_unprepare(esai_priv->fsysclk); 57443d24e76SNicolin Chen if (!IS_ERR(esai_priv->extalclk)) 57543d24e76SNicolin Chen clk_disable_unprepare(esai_priv->extalclk); 576a2a4d604SShengjiu Wang if (!IS_ERR(esai_priv->spbaclk)) 577a2a4d604SShengjiu Wang clk_disable_unprepare(esai_priv->spbaclk); 57843d24e76SNicolin Chen clk_disable_unprepare(esai_priv->coreclk); 57943d24e76SNicolin Chen } 58043d24e76SNicolin Chen 58143d24e76SNicolin Chen static int fsl_esai_trigger(struct snd_pcm_substream *substream, int cmd, 58243d24e76SNicolin Chen struct snd_soc_dai *dai) 58343d24e76SNicolin Chen { 58443d24e76SNicolin Chen struct fsl_esai *esai_priv = snd_soc_dai_get_drvdata(dai); 58543d24e76SNicolin Chen bool tx = substream->stream == SNDRV_PCM_STREAM_PLAYBACK; 58643d24e76SNicolin Chen u8 i, channels = substream->runtime->channels; 587de0d712aSShengjiu Wang u32 pins = DIV_ROUND_UP(channels, esai_priv->slots); 58843d24e76SNicolin Chen 58943d24e76SNicolin Chen switch (cmd) { 59043d24e76SNicolin Chen case SNDRV_PCM_TRIGGER_START: 59143d24e76SNicolin Chen case SNDRV_PCM_TRIGGER_RESUME: 59243d24e76SNicolin Chen case SNDRV_PCM_TRIGGER_PAUSE_RELEASE: 59343d24e76SNicolin Chen regmap_update_bits(esai_priv->regmap, REG_ESAI_xFCR(tx), 59443d24e76SNicolin Chen ESAI_xFCR_xFEN_MASK, ESAI_xFCR_xFEN); 59543d24e76SNicolin Chen 59643d24e76SNicolin Chen /* Write initial words reqiured by ESAI as normal procedure */ 59743d24e76SNicolin Chen for (i = 0; tx && i < channels; i++) 59843d24e76SNicolin Chen regmap_write(esai_priv->regmap, REG_ESAI_ETDR, 0x0); 59943d24e76SNicolin Chen 60043d24e76SNicolin Chen regmap_update_bits(esai_priv->regmap, REG_ESAI_xCR(tx), 60143d24e76SNicolin Chen tx ? ESAI_xCR_TE_MASK : ESAI_xCR_RE_MASK, 602de0d712aSShengjiu Wang tx ? ESAI_xCR_TE(pins) : ESAI_xCR_RE(pins)); 60343d24e76SNicolin Chen break; 60443d24e76SNicolin Chen case SNDRV_PCM_TRIGGER_SUSPEND: 60543d24e76SNicolin Chen case SNDRV_PCM_TRIGGER_STOP: 60643d24e76SNicolin Chen case SNDRV_PCM_TRIGGER_PAUSE_PUSH: 60743d24e76SNicolin Chen regmap_update_bits(esai_priv->regmap, REG_ESAI_xCR(tx), 60843d24e76SNicolin Chen tx ? ESAI_xCR_TE_MASK : ESAI_xCR_RE_MASK, 0); 60943d24e76SNicolin Chen 61043d24e76SNicolin Chen /* Disable and reset FIFO */ 61143d24e76SNicolin Chen regmap_update_bits(esai_priv->regmap, REG_ESAI_xFCR(tx), 61243d24e76SNicolin Chen ESAI_xFCR_xFR | ESAI_xFCR_xFEN, ESAI_xFCR_xFR); 61343d24e76SNicolin Chen regmap_update_bits(esai_priv->regmap, REG_ESAI_xFCR(tx), 61443d24e76SNicolin Chen ESAI_xFCR_xFR, 0); 61543d24e76SNicolin Chen break; 61643d24e76SNicolin Chen default: 61743d24e76SNicolin Chen return -EINVAL; 61843d24e76SNicolin Chen } 61943d24e76SNicolin Chen 62043d24e76SNicolin Chen return 0; 62143d24e76SNicolin Chen } 62243d24e76SNicolin Chen 62343d24e76SNicolin Chen static struct snd_soc_dai_ops fsl_esai_dai_ops = { 62443d24e76SNicolin Chen .startup = fsl_esai_startup, 62543d24e76SNicolin Chen .shutdown = fsl_esai_shutdown, 62643d24e76SNicolin Chen .trigger = fsl_esai_trigger, 62743d24e76SNicolin Chen .hw_params = fsl_esai_hw_params, 62843d24e76SNicolin Chen .set_sysclk = fsl_esai_set_dai_sysclk, 62943d24e76SNicolin Chen .set_fmt = fsl_esai_set_dai_fmt, 63043d24e76SNicolin Chen .set_tdm_slot = fsl_esai_set_dai_tdm_slot, 63143d24e76SNicolin Chen }; 63243d24e76SNicolin Chen 63343d24e76SNicolin Chen static int fsl_esai_dai_probe(struct snd_soc_dai *dai) 63443d24e76SNicolin Chen { 63543d24e76SNicolin Chen struct fsl_esai *esai_priv = snd_soc_dai_get_drvdata(dai); 63643d24e76SNicolin Chen 63743d24e76SNicolin Chen snd_soc_dai_init_dma_data(dai, &esai_priv->dma_params_tx, 63843d24e76SNicolin Chen &esai_priv->dma_params_rx); 63943d24e76SNicolin Chen 64043d24e76SNicolin Chen return 0; 64143d24e76SNicolin Chen } 64243d24e76SNicolin Chen 64343d24e76SNicolin Chen static struct snd_soc_dai_driver fsl_esai_dai = { 64443d24e76SNicolin Chen .probe = fsl_esai_dai_probe, 64543d24e76SNicolin Chen .playback = { 64674ccb27cSNicolin Chen .stream_name = "CPU-Playback", 64743d24e76SNicolin Chen .channels_min = 1, 64843d24e76SNicolin Chen .channels_max = 12, 649*f2a3ee01SFabio Estevam .rates = SNDRV_PCM_RATE_8000_192000, 65043d24e76SNicolin Chen .formats = FSL_ESAI_FORMATS, 65143d24e76SNicolin Chen }, 65243d24e76SNicolin Chen .capture = { 65374ccb27cSNicolin Chen .stream_name = "CPU-Capture", 65443d24e76SNicolin Chen .channels_min = 1, 65543d24e76SNicolin Chen .channels_max = 8, 656*f2a3ee01SFabio Estevam .rates = SNDRV_PCM_RATE_8000_192000, 65743d24e76SNicolin Chen .formats = FSL_ESAI_FORMATS, 65843d24e76SNicolin Chen }, 65943d24e76SNicolin Chen .ops = &fsl_esai_dai_ops, 66043d24e76SNicolin Chen }; 66143d24e76SNicolin Chen 66243d24e76SNicolin Chen static const struct snd_soc_component_driver fsl_esai_component = { 66343d24e76SNicolin Chen .name = "fsl-esai", 66443d24e76SNicolin Chen }; 66543d24e76SNicolin Chen 666c64c6076SZidan Wang static const struct reg_default fsl_esai_reg_defaults[] = { 6678973112aSZidan Wang {REG_ESAI_ETDR, 0x00000000}, 6688973112aSZidan Wang {REG_ESAI_ECR, 0x00000000}, 6698973112aSZidan Wang {REG_ESAI_TFCR, 0x00000000}, 6708973112aSZidan Wang {REG_ESAI_RFCR, 0x00000000}, 6718973112aSZidan Wang {REG_ESAI_TX0, 0x00000000}, 6728973112aSZidan Wang {REG_ESAI_TX1, 0x00000000}, 6738973112aSZidan Wang {REG_ESAI_TX2, 0x00000000}, 6748973112aSZidan Wang {REG_ESAI_TX3, 0x00000000}, 6758973112aSZidan Wang {REG_ESAI_TX4, 0x00000000}, 6768973112aSZidan Wang {REG_ESAI_TX5, 0x00000000}, 6778973112aSZidan Wang {REG_ESAI_TSR, 0x00000000}, 6788973112aSZidan Wang {REG_ESAI_SAICR, 0x00000000}, 6798973112aSZidan Wang {REG_ESAI_TCR, 0x00000000}, 6808973112aSZidan Wang {REG_ESAI_TCCR, 0x00000000}, 6818973112aSZidan Wang {REG_ESAI_RCR, 0x00000000}, 6828973112aSZidan Wang {REG_ESAI_RCCR, 0x00000000}, 6838973112aSZidan Wang {REG_ESAI_TSMA, 0x0000ffff}, 6848973112aSZidan Wang {REG_ESAI_TSMB, 0x0000ffff}, 6858973112aSZidan Wang {REG_ESAI_RSMA, 0x0000ffff}, 6868973112aSZidan Wang {REG_ESAI_RSMB, 0x0000ffff}, 6878973112aSZidan Wang {REG_ESAI_PRRC, 0x00000000}, 6888973112aSZidan Wang {REG_ESAI_PCRC, 0x00000000}, 689c64c6076SZidan Wang }; 690c64c6076SZidan Wang 69143d24e76SNicolin Chen static bool fsl_esai_readable_reg(struct device *dev, unsigned int reg) 69243d24e76SNicolin Chen { 69343d24e76SNicolin Chen switch (reg) { 69443d24e76SNicolin Chen case REG_ESAI_ERDR: 69543d24e76SNicolin Chen case REG_ESAI_ECR: 69643d24e76SNicolin Chen case REG_ESAI_ESR: 69743d24e76SNicolin Chen case REG_ESAI_TFCR: 69843d24e76SNicolin Chen case REG_ESAI_TFSR: 69943d24e76SNicolin Chen case REG_ESAI_RFCR: 70043d24e76SNicolin Chen case REG_ESAI_RFSR: 70143d24e76SNicolin Chen case REG_ESAI_RX0: 70243d24e76SNicolin Chen case REG_ESAI_RX1: 70343d24e76SNicolin Chen case REG_ESAI_RX2: 70443d24e76SNicolin Chen case REG_ESAI_RX3: 70543d24e76SNicolin Chen case REG_ESAI_SAISR: 70643d24e76SNicolin Chen case REG_ESAI_SAICR: 70743d24e76SNicolin Chen case REG_ESAI_TCR: 70843d24e76SNicolin Chen case REG_ESAI_TCCR: 70943d24e76SNicolin Chen case REG_ESAI_RCR: 71043d24e76SNicolin Chen case REG_ESAI_RCCR: 71143d24e76SNicolin Chen case REG_ESAI_TSMA: 71243d24e76SNicolin Chen case REG_ESAI_TSMB: 71343d24e76SNicolin Chen case REG_ESAI_RSMA: 71443d24e76SNicolin Chen case REG_ESAI_RSMB: 71543d24e76SNicolin Chen case REG_ESAI_PRRC: 71643d24e76SNicolin Chen case REG_ESAI_PCRC: 71743d24e76SNicolin Chen return true; 71843d24e76SNicolin Chen default: 71943d24e76SNicolin Chen return false; 72043d24e76SNicolin Chen } 72143d24e76SNicolin Chen } 72243d24e76SNicolin Chen 723c64c6076SZidan Wang static bool fsl_esai_volatile_reg(struct device *dev, unsigned int reg) 724c64c6076SZidan Wang { 725c64c6076SZidan Wang switch (reg) { 726c64c6076SZidan Wang case REG_ESAI_ERDR: 727c64c6076SZidan Wang case REG_ESAI_ESR: 728c64c6076SZidan Wang case REG_ESAI_TFSR: 729c64c6076SZidan Wang case REG_ESAI_RFSR: 730c64c6076SZidan Wang case REG_ESAI_RX0: 731c64c6076SZidan Wang case REG_ESAI_RX1: 732c64c6076SZidan Wang case REG_ESAI_RX2: 733c64c6076SZidan Wang case REG_ESAI_RX3: 734c64c6076SZidan Wang case REG_ESAI_SAISR: 735c64c6076SZidan Wang return true; 736c64c6076SZidan Wang default: 737c64c6076SZidan Wang return false; 738c64c6076SZidan Wang } 739c64c6076SZidan Wang } 740c64c6076SZidan Wang 74143d24e76SNicolin Chen static bool fsl_esai_writeable_reg(struct device *dev, unsigned int reg) 74243d24e76SNicolin Chen { 74343d24e76SNicolin Chen switch (reg) { 74443d24e76SNicolin Chen case REG_ESAI_ETDR: 74543d24e76SNicolin Chen case REG_ESAI_ECR: 74643d24e76SNicolin Chen case REG_ESAI_TFCR: 74743d24e76SNicolin Chen case REG_ESAI_RFCR: 74843d24e76SNicolin Chen case REG_ESAI_TX0: 74943d24e76SNicolin Chen case REG_ESAI_TX1: 75043d24e76SNicolin Chen case REG_ESAI_TX2: 75143d24e76SNicolin Chen case REG_ESAI_TX3: 75243d24e76SNicolin Chen case REG_ESAI_TX4: 75343d24e76SNicolin Chen case REG_ESAI_TX5: 75443d24e76SNicolin Chen case REG_ESAI_TSR: 75543d24e76SNicolin Chen case REG_ESAI_SAICR: 75643d24e76SNicolin Chen case REG_ESAI_TCR: 75743d24e76SNicolin Chen case REG_ESAI_TCCR: 75843d24e76SNicolin Chen case REG_ESAI_RCR: 75943d24e76SNicolin Chen case REG_ESAI_RCCR: 76043d24e76SNicolin Chen case REG_ESAI_TSMA: 76143d24e76SNicolin Chen case REG_ESAI_TSMB: 76243d24e76SNicolin Chen case REG_ESAI_RSMA: 76343d24e76SNicolin Chen case REG_ESAI_RSMB: 76443d24e76SNicolin Chen case REG_ESAI_PRRC: 76543d24e76SNicolin Chen case REG_ESAI_PCRC: 76643d24e76SNicolin Chen return true; 76743d24e76SNicolin Chen default: 76843d24e76SNicolin Chen return false; 76943d24e76SNicolin Chen } 77043d24e76SNicolin Chen } 77143d24e76SNicolin Chen 77292bd0334SXiubo Li static const struct regmap_config fsl_esai_regmap_config = { 77343d24e76SNicolin Chen .reg_bits = 32, 77443d24e76SNicolin Chen .reg_stride = 4, 77543d24e76SNicolin Chen .val_bits = 32, 77643d24e76SNicolin Chen 77743d24e76SNicolin Chen .max_register = REG_ESAI_PCRC, 778c64c6076SZidan Wang .reg_defaults = fsl_esai_reg_defaults, 779c64c6076SZidan Wang .num_reg_defaults = ARRAY_SIZE(fsl_esai_reg_defaults), 78043d24e76SNicolin Chen .readable_reg = fsl_esai_readable_reg, 781c64c6076SZidan Wang .volatile_reg = fsl_esai_volatile_reg, 78243d24e76SNicolin Chen .writeable_reg = fsl_esai_writeable_reg, 7830effb865SMarek Vasut .cache_type = REGCACHE_FLAT, 78443d24e76SNicolin Chen }; 78543d24e76SNicolin Chen 78643d24e76SNicolin Chen static int fsl_esai_probe(struct platform_device *pdev) 78743d24e76SNicolin Chen { 78843d24e76SNicolin Chen struct device_node *np = pdev->dev.of_node; 78943d24e76SNicolin Chen struct fsl_esai *esai_priv; 79043d24e76SNicolin Chen struct resource *res; 79143d24e76SNicolin Chen const uint32_t *iprop; 79243d24e76SNicolin Chen void __iomem *regs; 79343d24e76SNicolin Chen int irq, ret; 79443d24e76SNicolin Chen 79543d24e76SNicolin Chen esai_priv = devm_kzalloc(&pdev->dev, sizeof(*esai_priv), GFP_KERNEL); 79643d24e76SNicolin Chen if (!esai_priv) 79743d24e76SNicolin Chen return -ENOMEM; 79843d24e76SNicolin Chen 79943d24e76SNicolin Chen esai_priv->pdev = pdev; 8006aa256b6SDaniel Mack strncpy(esai_priv->name, np->name, sizeof(esai_priv->name) - 1); 80143d24e76SNicolin Chen 80243d24e76SNicolin Chen /* Get the addresses and IRQ */ 80343d24e76SNicolin Chen res = platform_get_resource(pdev, IORESOURCE_MEM, 0); 80443d24e76SNicolin Chen regs = devm_ioremap_resource(&pdev->dev, res); 80543d24e76SNicolin Chen if (IS_ERR(regs)) 80643d24e76SNicolin Chen return PTR_ERR(regs); 80743d24e76SNicolin Chen 80843d24e76SNicolin Chen esai_priv->regmap = devm_regmap_init_mmio_clk(&pdev->dev, 80943d24e76SNicolin Chen "core", regs, &fsl_esai_regmap_config); 81043d24e76SNicolin Chen if (IS_ERR(esai_priv->regmap)) { 81143d24e76SNicolin Chen dev_err(&pdev->dev, "failed to init regmap: %ld\n", 81243d24e76SNicolin Chen PTR_ERR(esai_priv->regmap)); 81343d24e76SNicolin Chen return PTR_ERR(esai_priv->regmap); 81443d24e76SNicolin Chen } 81543d24e76SNicolin Chen 81643d24e76SNicolin Chen esai_priv->coreclk = devm_clk_get(&pdev->dev, "core"); 81743d24e76SNicolin Chen if (IS_ERR(esai_priv->coreclk)) { 81843d24e76SNicolin Chen dev_err(&pdev->dev, "failed to get core clock: %ld\n", 81943d24e76SNicolin Chen PTR_ERR(esai_priv->coreclk)); 82043d24e76SNicolin Chen return PTR_ERR(esai_priv->coreclk); 82143d24e76SNicolin Chen } 82243d24e76SNicolin Chen 82343d24e76SNicolin Chen esai_priv->extalclk = devm_clk_get(&pdev->dev, "extal"); 82443d24e76SNicolin Chen if (IS_ERR(esai_priv->extalclk)) 82543d24e76SNicolin Chen dev_warn(&pdev->dev, "failed to get extal clock: %ld\n", 82643d24e76SNicolin Chen PTR_ERR(esai_priv->extalclk)); 82743d24e76SNicolin Chen 82843d24e76SNicolin Chen esai_priv->fsysclk = devm_clk_get(&pdev->dev, "fsys"); 82943d24e76SNicolin Chen if (IS_ERR(esai_priv->fsysclk)) 83043d24e76SNicolin Chen dev_warn(&pdev->dev, "failed to get fsys clock: %ld\n", 83143d24e76SNicolin Chen PTR_ERR(esai_priv->fsysclk)); 83243d24e76SNicolin Chen 833a2a4d604SShengjiu Wang esai_priv->spbaclk = devm_clk_get(&pdev->dev, "spba"); 834a2a4d604SShengjiu Wang if (IS_ERR(esai_priv->spbaclk)) 835a2a4d604SShengjiu Wang dev_warn(&pdev->dev, "failed to get spba clock: %ld\n", 836a2a4d604SShengjiu Wang PTR_ERR(esai_priv->spbaclk)); 837a2a4d604SShengjiu Wang 83843d24e76SNicolin Chen irq = platform_get_irq(pdev, 0); 83943d24e76SNicolin Chen if (irq < 0) { 840da2d4524SFabio Estevam dev_err(&pdev->dev, "no irq for node %s\n", pdev->name); 84143d24e76SNicolin Chen return irq; 84243d24e76SNicolin Chen } 84343d24e76SNicolin Chen 84443d24e76SNicolin Chen ret = devm_request_irq(&pdev->dev, irq, esai_isr, 0, 84543d24e76SNicolin Chen esai_priv->name, esai_priv); 84643d24e76SNicolin Chen if (ret) { 84743d24e76SNicolin Chen dev_err(&pdev->dev, "failed to claim irq %u\n", irq); 84843d24e76SNicolin Chen return ret; 84943d24e76SNicolin Chen } 85043d24e76SNicolin Chen 851de0d712aSShengjiu Wang /* Set a default slot number */ 852de0d712aSShengjiu Wang esai_priv->slots = 2; 853de0d712aSShengjiu Wang 85443d24e76SNicolin Chen /* Set a default master/slave state */ 85543d24e76SNicolin Chen esai_priv->slave_mode = true; 85643d24e76SNicolin Chen 85743d24e76SNicolin Chen /* Determine the FIFO depth */ 85843d24e76SNicolin Chen iprop = of_get_property(np, "fsl,fifo-depth", NULL); 85943d24e76SNicolin Chen if (iprop) 86043d24e76SNicolin Chen esai_priv->fifo_depth = be32_to_cpup(iprop); 86143d24e76SNicolin Chen else 86243d24e76SNicolin Chen esai_priv->fifo_depth = 64; 86343d24e76SNicolin Chen 86443d24e76SNicolin Chen esai_priv->dma_params_tx.maxburst = 16; 86543d24e76SNicolin Chen esai_priv->dma_params_rx.maxburst = 16; 86643d24e76SNicolin Chen esai_priv->dma_params_tx.addr = res->start + REG_ESAI_ETDR; 86743d24e76SNicolin Chen esai_priv->dma_params_rx.addr = res->start + REG_ESAI_ERDR; 86843d24e76SNicolin Chen 86943d24e76SNicolin Chen esai_priv->synchronous = 87043d24e76SNicolin Chen of_property_read_bool(np, "fsl,esai-synchronous"); 87143d24e76SNicolin Chen 87243d24e76SNicolin Chen /* Implement full symmetry for synchronous mode */ 87343d24e76SNicolin Chen if (esai_priv->synchronous) { 87443d24e76SNicolin Chen fsl_esai_dai.symmetric_rates = 1; 87543d24e76SNicolin Chen fsl_esai_dai.symmetric_channels = 1; 87643d24e76SNicolin Chen fsl_esai_dai.symmetric_samplebits = 1; 87743d24e76SNicolin Chen } 87843d24e76SNicolin Chen 87943d24e76SNicolin Chen dev_set_drvdata(&pdev->dev, esai_priv); 88043d24e76SNicolin Chen 88143d24e76SNicolin Chen /* Reset ESAI unit */ 88243d24e76SNicolin Chen ret = regmap_write(esai_priv->regmap, REG_ESAI_ECR, ESAI_ECR_ERST); 88343d24e76SNicolin Chen if (ret) { 88443d24e76SNicolin Chen dev_err(&pdev->dev, "failed to reset ESAI: %d\n", ret); 88543d24e76SNicolin Chen return ret; 88643d24e76SNicolin Chen } 88743d24e76SNicolin Chen 88843d24e76SNicolin Chen /* 88943d24e76SNicolin Chen * We need to enable ESAI so as to access some of its registers. 89043d24e76SNicolin Chen * Otherwise, we would fail to dump regmap from user space. 89143d24e76SNicolin Chen */ 89243d24e76SNicolin Chen ret = regmap_write(esai_priv->regmap, REG_ESAI_ECR, ESAI_ECR_ESAIEN); 89343d24e76SNicolin Chen if (ret) { 89443d24e76SNicolin Chen dev_err(&pdev->dev, "failed to enable ESAI: %d\n", ret); 89543d24e76SNicolin Chen return ret; 89643d24e76SNicolin Chen } 89743d24e76SNicolin Chen 89843d24e76SNicolin Chen ret = devm_snd_soc_register_component(&pdev->dev, &fsl_esai_component, 89943d24e76SNicolin Chen &fsl_esai_dai, 1); 90043d24e76SNicolin Chen if (ret) { 90143d24e76SNicolin Chen dev_err(&pdev->dev, "failed to register DAI: %d\n", ret); 90243d24e76SNicolin Chen return ret; 90343d24e76SNicolin Chen } 90443d24e76SNicolin Chen 9050d69e0ddSShengjiu Wang ret = imx_pcm_dma_init(pdev, IMX_ESAI_DMABUF_SIZE); 90643d24e76SNicolin Chen if (ret) 90743d24e76SNicolin Chen dev_err(&pdev->dev, "failed to init imx pcm dma: %d\n", ret); 90843d24e76SNicolin Chen 90943d24e76SNicolin Chen return ret; 91043d24e76SNicolin Chen } 91143d24e76SNicolin Chen 91243d24e76SNicolin Chen static const struct of_device_id fsl_esai_dt_ids[] = { 91343d24e76SNicolin Chen { .compatible = "fsl,imx35-esai", }, 914b21cc2f5SXiubo Li { .compatible = "fsl,vf610-esai", }, 91543d24e76SNicolin Chen {} 91643d24e76SNicolin Chen }; 91743d24e76SNicolin Chen MODULE_DEVICE_TABLE(of, fsl_esai_dt_ids); 91843d24e76SNicolin Chen 919739146b6SNicolin Chen #ifdef CONFIG_PM_SLEEP 920c64c6076SZidan Wang static int fsl_esai_suspend(struct device *dev) 921c64c6076SZidan Wang { 922c64c6076SZidan Wang struct fsl_esai *esai = dev_get_drvdata(dev); 923c64c6076SZidan Wang 924c64c6076SZidan Wang regcache_cache_only(esai->regmap, true); 925c64c6076SZidan Wang regcache_mark_dirty(esai->regmap); 926c64c6076SZidan Wang 927c64c6076SZidan Wang return 0; 928c64c6076SZidan Wang } 929c64c6076SZidan Wang 930c64c6076SZidan Wang static int fsl_esai_resume(struct device *dev) 931c64c6076SZidan Wang { 932c64c6076SZidan Wang struct fsl_esai *esai = dev_get_drvdata(dev); 933c64c6076SZidan Wang int ret; 934c64c6076SZidan Wang 935c64c6076SZidan Wang regcache_cache_only(esai->regmap, false); 936c64c6076SZidan Wang 937c64c6076SZidan Wang /* FIFO reset for safety */ 938c64c6076SZidan Wang regmap_update_bits(esai->regmap, REG_ESAI_TFCR, 939c64c6076SZidan Wang ESAI_xFCR_xFR, ESAI_xFCR_xFR); 940c64c6076SZidan Wang regmap_update_bits(esai->regmap, REG_ESAI_RFCR, 941c64c6076SZidan Wang ESAI_xFCR_xFR, ESAI_xFCR_xFR); 942c64c6076SZidan Wang 943c64c6076SZidan Wang ret = regcache_sync(esai->regmap); 944c64c6076SZidan Wang if (ret) 945c64c6076SZidan Wang return ret; 946c64c6076SZidan Wang 947c64c6076SZidan Wang /* FIFO reset done */ 948c64c6076SZidan Wang regmap_update_bits(esai->regmap, REG_ESAI_TFCR, ESAI_xFCR_xFR, 0); 949c64c6076SZidan Wang regmap_update_bits(esai->regmap, REG_ESAI_RFCR, ESAI_xFCR_xFR, 0); 950c64c6076SZidan Wang 951c64c6076SZidan Wang return 0; 952c64c6076SZidan Wang } 953c64c6076SZidan Wang #endif /* CONFIG_PM_SLEEP */ 954c64c6076SZidan Wang 955c64c6076SZidan Wang static const struct dev_pm_ops fsl_esai_pm_ops = { 956c64c6076SZidan Wang SET_SYSTEM_SLEEP_PM_OPS(fsl_esai_suspend, fsl_esai_resume) 957c64c6076SZidan Wang }; 958c64c6076SZidan Wang 95943d24e76SNicolin Chen static struct platform_driver fsl_esai_driver = { 96043d24e76SNicolin Chen .probe = fsl_esai_probe, 96143d24e76SNicolin Chen .driver = { 96243d24e76SNicolin Chen .name = "fsl-esai-dai", 963c64c6076SZidan Wang .pm = &fsl_esai_pm_ops, 96443d24e76SNicolin Chen .of_match_table = fsl_esai_dt_ids, 96543d24e76SNicolin Chen }, 96643d24e76SNicolin Chen }; 96743d24e76SNicolin Chen 96843d24e76SNicolin Chen module_platform_driver(fsl_esai_driver); 96943d24e76SNicolin Chen 97043d24e76SNicolin Chen MODULE_AUTHOR("Freescale Semiconductor, Inc."); 97143d24e76SNicolin Chen MODULE_DESCRIPTION("Freescale ESAI CPU DAI driver"); 97243d24e76SNicolin Chen MODULE_LICENSE("GPL v2"); 97343d24e76SNicolin Chen MODULE_ALIAS("platform:fsl-esai-dai"); 974