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" 21a603c8eeSXiubo Li #include "fsl_utils.h" 2243d24e76SNicolin Chen 2343d24e76SNicolin Chen #define FSL_ESAI_RATES SNDRV_PCM_RATE_8000_192000 2443d24e76SNicolin Chen #define FSL_ESAI_FORMATS (SNDRV_PCM_FMTBIT_S8 | \ 2543d24e76SNicolin Chen SNDRV_PCM_FMTBIT_S16_LE | \ 2643d24e76SNicolin Chen SNDRV_PCM_FMTBIT_S20_3LE | \ 2743d24e76SNicolin Chen SNDRV_PCM_FMTBIT_S24_LE) 2843d24e76SNicolin Chen 2943d24e76SNicolin Chen /** 3043d24e76SNicolin Chen * fsl_esai: ESAI private data 3143d24e76SNicolin Chen * 3243d24e76SNicolin Chen * @dma_params_rx: DMA parameters for receive channel 3343d24e76SNicolin Chen * @dma_params_tx: DMA parameters for transmit channel 3443d24e76SNicolin Chen * @pdev: platform device pointer 3543d24e76SNicolin Chen * @regmap: regmap handler 3643d24e76SNicolin Chen * @coreclk: clock source to access register 3743d24e76SNicolin Chen * @extalclk: esai clock source to derive HCK, SCK and FS 3843d24e76SNicolin Chen * @fsysclk: system clock source to derive HCK, SCK and FS 3943d24e76SNicolin Chen * @fifo_depth: depth of tx/rx FIFO 4043d24e76SNicolin Chen * @slot_width: width of each DAI slot 41*de0d712aSShengjiu Wang * @slots: number of slots 4243d24e76SNicolin Chen * @hck_rate: clock rate of desired HCKx clock 43f975ca46SNicolin Chen * @sck_rate: clock rate of desired SCKx clock 44f975ca46SNicolin Chen * @hck_dir: the direction of HCKx pads 4543d24e76SNicolin Chen * @sck_div: if using PSR/PM dividers for SCKx clock 4643d24e76SNicolin Chen * @slave_mode: if fully using DAI slave mode 4743d24e76SNicolin Chen * @synchronous: if using tx/rx synchronous mode 4843d24e76SNicolin Chen * @name: driver name 4943d24e76SNicolin Chen */ 5043d24e76SNicolin Chen struct fsl_esai { 5143d24e76SNicolin Chen struct snd_dmaengine_dai_dma_data dma_params_rx; 5243d24e76SNicolin Chen struct snd_dmaengine_dai_dma_data dma_params_tx; 5343d24e76SNicolin Chen struct platform_device *pdev; 5443d24e76SNicolin Chen struct regmap *regmap; 5543d24e76SNicolin Chen struct clk *coreclk; 5643d24e76SNicolin Chen struct clk *extalclk; 5743d24e76SNicolin Chen struct clk *fsysclk; 5843d24e76SNicolin Chen u32 fifo_depth; 5943d24e76SNicolin Chen u32 slot_width; 60*de0d712aSShengjiu 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) 7943d24e76SNicolin Chen dev_dbg(&pdev->dev, "isr: Transmition 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) 8543d24e76SNicolin Chen dev_warn(&pdev->dev, "isr: Transmition 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) 9143d24e76SNicolin Chen dev_dbg(&pdev->dev, "isr: Transmition 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; 368*de0d712aSShengjiu 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; 47333529ec9SFabio Estevam if (!IS_ERR(esai_priv->extalclk)) { 47433529ec9SFabio Estevam ret = clk_prepare_enable(esai_priv->extalclk); 47533529ec9SFabio Estevam if (ret) 47633529ec9SFabio Estevam goto err_extalck; 47733529ec9SFabio Estevam } 47833529ec9SFabio Estevam if (!IS_ERR(esai_priv->fsysclk)) { 47933529ec9SFabio Estevam ret = clk_prepare_enable(esai_priv->fsysclk); 48033529ec9SFabio Estevam if (ret) 48133529ec9SFabio Estevam goto err_fsysclk; 48233529ec9SFabio Estevam } 48343d24e76SNicolin Chen 48443d24e76SNicolin Chen if (!dai->active) { 48543d24e76SNicolin Chen /* Set synchronous mode */ 48643d24e76SNicolin Chen regmap_update_bits(esai_priv->regmap, REG_ESAI_SAICR, 48743d24e76SNicolin Chen ESAI_SAICR_SYNC, esai_priv->synchronous ? 48843d24e76SNicolin Chen ESAI_SAICR_SYNC : 0); 48943d24e76SNicolin Chen 49043d24e76SNicolin Chen /* Set a default slot number -- 2 */ 49143d24e76SNicolin Chen regmap_update_bits(esai_priv->regmap, REG_ESAI_TCCR, 49243d24e76SNicolin Chen ESAI_xCCR_xDC_MASK, ESAI_xCCR_xDC(2)); 49343d24e76SNicolin Chen regmap_update_bits(esai_priv->regmap, REG_ESAI_RCCR, 49443d24e76SNicolin Chen ESAI_xCCR_xDC_MASK, ESAI_xCCR_xDC(2)); 49543d24e76SNicolin Chen } 49643d24e76SNicolin Chen 49743d24e76SNicolin Chen return 0; 49833529ec9SFabio Estevam 49933529ec9SFabio Estevam err_fsysclk: 50033529ec9SFabio Estevam if (!IS_ERR(esai_priv->extalclk)) 50133529ec9SFabio Estevam clk_disable_unprepare(esai_priv->extalclk); 50233529ec9SFabio Estevam err_extalck: 50333529ec9SFabio Estevam clk_disable_unprepare(esai_priv->coreclk); 50433529ec9SFabio Estevam 50533529ec9SFabio Estevam return ret; 50643d24e76SNicolin Chen } 50743d24e76SNicolin Chen 50843d24e76SNicolin Chen static int fsl_esai_hw_params(struct snd_pcm_substream *substream, 50943d24e76SNicolin Chen struct snd_pcm_hw_params *params, 51043d24e76SNicolin Chen struct snd_soc_dai *dai) 51143d24e76SNicolin Chen { 51243d24e76SNicolin Chen struct fsl_esai *esai_priv = snd_soc_dai_get_drvdata(dai); 51343d24e76SNicolin Chen bool tx = substream->stream == SNDRV_PCM_STREAM_PLAYBACK; 51443d24e76SNicolin Chen u32 width = snd_pcm_format_width(params_format(params)); 51543d24e76SNicolin Chen u32 channels = params_channels(params); 516*de0d712aSShengjiu Wang u32 pins = DIV_ROUND_UP(channels, esai_priv->slots); 5173e185238SXiubo Li u32 bclk, mask, val; 5183e185238SXiubo Li int ret; 51943d24e76SNicolin Chen 520*de0d712aSShengjiu Wang bclk = params_rate(params) * esai_priv->slot_width * esai_priv->slots; 52143d24e76SNicolin Chen 52243d24e76SNicolin Chen ret = fsl_esai_set_bclk(dai, tx, bclk); 52343d24e76SNicolin Chen if (ret) 52443d24e76SNicolin Chen return ret; 52543d24e76SNicolin Chen 52643d24e76SNicolin Chen /* Use Normal mode to support monaural audio */ 52743d24e76SNicolin Chen regmap_update_bits(esai_priv->regmap, REG_ESAI_xCR(tx), 52843d24e76SNicolin Chen ESAI_xCR_xMOD_MASK, params_channels(params) > 1 ? 52943d24e76SNicolin Chen ESAI_xCR_xMOD_NETWORK : 0); 53043d24e76SNicolin Chen 53143d24e76SNicolin Chen regmap_update_bits(esai_priv->regmap, REG_ESAI_xFCR(tx), 53243d24e76SNicolin Chen ESAI_xFCR_xFR_MASK, ESAI_xFCR_xFR); 53343d24e76SNicolin Chen 53443d24e76SNicolin Chen mask = ESAI_xFCR_xFR_MASK | ESAI_xFCR_xWA_MASK | ESAI_xFCR_xFWM_MASK | 53543d24e76SNicolin Chen (tx ? ESAI_xFCR_TE_MASK | ESAI_xFCR_TIEN : ESAI_xFCR_RE_MASK); 53643d24e76SNicolin Chen val = ESAI_xFCR_xWA(width) | ESAI_xFCR_xFWM(esai_priv->fifo_depth) | 537*de0d712aSShengjiu Wang (tx ? ESAI_xFCR_TE(pins) | ESAI_xFCR_TIEN : ESAI_xFCR_RE(pins)); 53843d24e76SNicolin Chen 53943d24e76SNicolin Chen regmap_update_bits(esai_priv->regmap, REG_ESAI_xFCR(tx), mask, val); 54043d24e76SNicolin Chen 54143d24e76SNicolin Chen mask = ESAI_xCR_xSWS_MASK | (tx ? ESAI_xCR_PADC : 0); 54243d24e76SNicolin Chen val = ESAI_xCR_xSWS(esai_priv->slot_width, width) | (tx ? ESAI_xCR_PADC : 0); 54343d24e76SNicolin Chen 54443d24e76SNicolin Chen regmap_update_bits(esai_priv->regmap, REG_ESAI_xCR(tx), mask, val); 54543d24e76SNicolin Chen 5464f8210f6SNicolin Chen /* Remove ESAI personal reset by configuring ESAI_PCRC and ESAI_PRRC */ 5474f8210f6SNicolin Chen regmap_update_bits(esai_priv->regmap, REG_ESAI_PRRC, 5484f8210f6SNicolin Chen ESAI_PRRC_PDC_MASK, ESAI_PRRC_PDC(ESAI_GPIO)); 5494f8210f6SNicolin Chen regmap_update_bits(esai_priv->regmap, REG_ESAI_PCRC, 5504f8210f6SNicolin Chen ESAI_PCRC_PC_MASK, ESAI_PCRC_PC(ESAI_GPIO)); 55143d24e76SNicolin Chen return 0; 55243d24e76SNicolin Chen } 55343d24e76SNicolin Chen 55443d24e76SNicolin Chen static void fsl_esai_shutdown(struct snd_pcm_substream *substream, 55543d24e76SNicolin Chen struct snd_soc_dai *dai) 55643d24e76SNicolin Chen { 55743d24e76SNicolin Chen struct fsl_esai *esai_priv = snd_soc_dai_get_drvdata(dai); 55843d24e76SNicolin Chen 55943d24e76SNicolin Chen if (!IS_ERR(esai_priv->fsysclk)) 56043d24e76SNicolin Chen clk_disable_unprepare(esai_priv->fsysclk); 56143d24e76SNicolin Chen if (!IS_ERR(esai_priv->extalclk)) 56243d24e76SNicolin Chen clk_disable_unprepare(esai_priv->extalclk); 56343d24e76SNicolin Chen clk_disable_unprepare(esai_priv->coreclk); 56443d24e76SNicolin Chen } 56543d24e76SNicolin Chen 56643d24e76SNicolin Chen static int fsl_esai_trigger(struct snd_pcm_substream *substream, int cmd, 56743d24e76SNicolin Chen struct snd_soc_dai *dai) 56843d24e76SNicolin Chen { 56943d24e76SNicolin Chen struct fsl_esai *esai_priv = snd_soc_dai_get_drvdata(dai); 57043d24e76SNicolin Chen bool tx = substream->stream == SNDRV_PCM_STREAM_PLAYBACK; 57143d24e76SNicolin Chen u8 i, channels = substream->runtime->channels; 572*de0d712aSShengjiu Wang u32 pins = DIV_ROUND_UP(channels, esai_priv->slots); 57343d24e76SNicolin Chen 57443d24e76SNicolin Chen switch (cmd) { 57543d24e76SNicolin Chen case SNDRV_PCM_TRIGGER_START: 57643d24e76SNicolin Chen case SNDRV_PCM_TRIGGER_RESUME: 57743d24e76SNicolin Chen case SNDRV_PCM_TRIGGER_PAUSE_RELEASE: 57843d24e76SNicolin Chen regmap_update_bits(esai_priv->regmap, REG_ESAI_xFCR(tx), 57943d24e76SNicolin Chen ESAI_xFCR_xFEN_MASK, ESAI_xFCR_xFEN); 58043d24e76SNicolin Chen 58143d24e76SNicolin Chen /* Write initial words reqiured by ESAI as normal procedure */ 58243d24e76SNicolin Chen for (i = 0; tx && i < channels; i++) 58343d24e76SNicolin Chen regmap_write(esai_priv->regmap, REG_ESAI_ETDR, 0x0); 58443d24e76SNicolin Chen 58543d24e76SNicolin Chen regmap_update_bits(esai_priv->regmap, REG_ESAI_xCR(tx), 58643d24e76SNicolin Chen tx ? ESAI_xCR_TE_MASK : ESAI_xCR_RE_MASK, 587*de0d712aSShengjiu Wang tx ? ESAI_xCR_TE(pins) : ESAI_xCR_RE(pins)); 58843d24e76SNicolin Chen break; 58943d24e76SNicolin Chen case SNDRV_PCM_TRIGGER_SUSPEND: 59043d24e76SNicolin Chen case SNDRV_PCM_TRIGGER_STOP: 59143d24e76SNicolin Chen case SNDRV_PCM_TRIGGER_PAUSE_PUSH: 59243d24e76SNicolin Chen regmap_update_bits(esai_priv->regmap, REG_ESAI_xCR(tx), 59343d24e76SNicolin Chen tx ? ESAI_xCR_TE_MASK : ESAI_xCR_RE_MASK, 0); 59443d24e76SNicolin Chen 59543d24e76SNicolin Chen /* Disable and reset FIFO */ 59643d24e76SNicolin Chen regmap_update_bits(esai_priv->regmap, REG_ESAI_xFCR(tx), 59743d24e76SNicolin Chen ESAI_xFCR_xFR | ESAI_xFCR_xFEN, ESAI_xFCR_xFR); 59843d24e76SNicolin Chen regmap_update_bits(esai_priv->regmap, REG_ESAI_xFCR(tx), 59943d24e76SNicolin Chen ESAI_xFCR_xFR, 0); 60043d24e76SNicolin Chen break; 60143d24e76SNicolin Chen default: 60243d24e76SNicolin Chen return -EINVAL; 60343d24e76SNicolin Chen } 60443d24e76SNicolin Chen 60543d24e76SNicolin Chen return 0; 60643d24e76SNicolin Chen } 60743d24e76SNicolin Chen 60843d24e76SNicolin Chen static struct snd_soc_dai_ops fsl_esai_dai_ops = { 60943d24e76SNicolin Chen .startup = fsl_esai_startup, 61043d24e76SNicolin Chen .shutdown = fsl_esai_shutdown, 61143d24e76SNicolin Chen .trigger = fsl_esai_trigger, 61243d24e76SNicolin Chen .hw_params = fsl_esai_hw_params, 61343d24e76SNicolin Chen .set_sysclk = fsl_esai_set_dai_sysclk, 61443d24e76SNicolin Chen .set_fmt = fsl_esai_set_dai_fmt, 615a603c8eeSXiubo Li .xlate_tdm_slot_mask = fsl_asoc_xlate_tdm_slot_mask, 61643d24e76SNicolin Chen .set_tdm_slot = fsl_esai_set_dai_tdm_slot, 61743d24e76SNicolin Chen }; 61843d24e76SNicolin Chen 61943d24e76SNicolin Chen static int fsl_esai_dai_probe(struct snd_soc_dai *dai) 62043d24e76SNicolin Chen { 62143d24e76SNicolin Chen struct fsl_esai *esai_priv = snd_soc_dai_get_drvdata(dai); 62243d24e76SNicolin Chen 62343d24e76SNicolin Chen snd_soc_dai_init_dma_data(dai, &esai_priv->dma_params_tx, 62443d24e76SNicolin Chen &esai_priv->dma_params_rx); 62543d24e76SNicolin Chen 62643d24e76SNicolin Chen return 0; 62743d24e76SNicolin Chen } 62843d24e76SNicolin Chen 62943d24e76SNicolin Chen static struct snd_soc_dai_driver fsl_esai_dai = { 63043d24e76SNicolin Chen .probe = fsl_esai_dai_probe, 63143d24e76SNicolin Chen .playback = { 63274ccb27cSNicolin Chen .stream_name = "CPU-Playback", 63343d24e76SNicolin Chen .channels_min = 1, 63443d24e76SNicolin Chen .channels_max = 12, 63543d24e76SNicolin Chen .rates = FSL_ESAI_RATES, 63643d24e76SNicolin Chen .formats = FSL_ESAI_FORMATS, 63743d24e76SNicolin Chen }, 63843d24e76SNicolin Chen .capture = { 63974ccb27cSNicolin Chen .stream_name = "CPU-Capture", 64043d24e76SNicolin Chen .channels_min = 1, 64143d24e76SNicolin Chen .channels_max = 8, 64243d24e76SNicolin Chen .rates = FSL_ESAI_RATES, 64343d24e76SNicolin Chen .formats = FSL_ESAI_FORMATS, 64443d24e76SNicolin Chen }, 64543d24e76SNicolin Chen .ops = &fsl_esai_dai_ops, 64643d24e76SNicolin Chen }; 64743d24e76SNicolin Chen 64843d24e76SNicolin Chen static const struct snd_soc_component_driver fsl_esai_component = { 64943d24e76SNicolin Chen .name = "fsl-esai", 65043d24e76SNicolin Chen }; 65143d24e76SNicolin Chen 65243d24e76SNicolin Chen static bool fsl_esai_readable_reg(struct device *dev, unsigned int reg) 65343d24e76SNicolin Chen { 65443d24e76SNicolin Chen switch (reg) { 65543d24e76SNicolin Chen case REG_ESAI_ERDR: 65643d24e76SNicolin Chen case REG_ESAI_ECR: 65743d24e76SNicolin Chen case REG_ESAI_ESR: 65843d24e76SNicolin Chen case REG_ESAI_TFCR: 65943d24e76SNicolin Chen case REG_ESAI_TFSR: 66043d24e76SNicolin Chen case REG_ESAI_RFCR: 66143d24e76SNicolin Chen case REG_ESAI_RFSR: 66243d24e76SNicolin Chen case REG_ESAI_RX0: 66343d24e76SNicolin Chen case REG_ESAI_RX1: 66443d24e76SNicolin Chen case REG_ESAI_RX2: 66543d24e76SNicolin Chen case REG_ESAI_RX3: 66643d24e76SNicolin Chen case REG_ESAI_SAISR: 66743d24e76SNicolin Chen case REG_ESAI_SAICR: 66843d24e76SNicolin Chen case REG_ESAI_TCR: 66943d24e76SNicolin Chen case REG_ESAI_TCCR: 67043d24e76SNicolin Chen case REG_ESAI_RCR: 67143d24e76SNicolin Chen case REG_ESAI_RCCR: 67243d24e76SNicolin Chen case REG_ESAI_TSMA: 67343d24e76SNicolin Chen case REG_ESAI_TSMB: 67443d24e76SNicolin Chen case REG_ESAI_RSMA: 67543d24e76SNicolin Chen case REG_ESAI_RSMB: 67643d24e76SNicolin Chen case REG_ESAI_PRRC: 67743d24e76SNicolin Chen case REG_ESAI_PCRC: 67843d24e76SNicolin Chen return true; 67943d24e76SNicolin Chen default: 68043d24e76SNicolin Chen return false; 68143d24e76SNicolin Chen } 68243d24e76SNicolin Chen } 68343d24e76SNicolin Chen 68443d24e76SNicolin Chen static bool fsl_esai_writeable_reg(struct device *dev, unsigned int reg) 68543d24e76SNicolin Chen { 68643d24e76SNicolin Chen switch (reg) { 68743d24e76SNicolin Chen case REG_ESAI_ETDR: 68843d24e76SNicolin Chen case REG_ESAI_ECR: 68943d24e76SNicolin Chen case REG_ESAI_TFCR: 69043d24e76SNicolin Chen case REG_ESAI_RFCR: 69143d24e76SNicolin Chen case REG_ESAI_TX0: 69243d24e76SNicolin Chen case REG_ESAI_TX1: 69343d24e76SNicolin Chen case REG_ESAI_TX2: 69443d24e76SNicolin Chen case REG_ESAI_TX3: 69543d24e76SNicolin Chen case REG_ESAI_TX4: 69643d24e76SNicolin Chen case REG_ESAI_TX5: 69743d24e76SNicolin Chen case REG_ESAI_TSR: 69843d24e76SNicolin Chen case REG_ESAI_SAICR: 69943d24e76SNicolin Chen case REG_ESAI_TCR: 70043d24e76SNicolin Chen case REG_ESAI_TCCR: 70143d24e76SNicolin Chen case REG_ESAI_RCR: 70243d24e76SNicolin Chen case REG_ESAI_RCCR: 70343d24e76SNicolin Chen case REG_ESAI_TSMA: 70443d24e76SNicolin Chen case REG_ESAI_TSMB: 70543d24e76SNicolin Chen case REG_ESAI_RSMA: 70643d24e76SNicolin Chen case REG_ESAI_RSMB: 70743d24e76SNicolin Chen case REG_ESAI_PRRC: 70843d24e76SNicolin Chen case REG_ESAI_PCRC: 70943d24e76SNicolin Chen return true; 71043d24e76SNicolin Chen default: 71143d24e76SNicolin Chen return false; 71243d24e76SNicolin Chen } 71343d24e76SNicolin Chen } 71443d24e76SNicolin Chen 715eaba603fSXiubo Li static struct regmap_config fsl_esai_regmap_config = { 71643d24e76SNicolin Chen .reg_bits = 32, 71743d24e76SNicolin Chen .reg_stride = 4, 71843d24e76SNicolin Chen .val_bits = 32, 71943d24e76SNicolin Chen 72043d24e76SNicolin Chen .max_register = REG_ESAI_PCRC, 72143d24e76SNicolin Chen .readable_reg = fsl_esai_readable_reg, 72243d24e76SNicolin Chen .writeable_reg = fsl_esai_writeable_reg, 72343d24e76SNicolin Chen }; 72443d24e76SNicolin Chen 72543d24e76SNicolin Chen static int fsl_esai_probe(struct platform_device *pdev) 72643d24e76SNicolin Chen { 72743d24e76SNicolin Chen struct device_node *np = pdev->dev.of_node; 72843d24e76SNicolin Chen struct fsl_esai *esai_priv; 72943d24e76SNicolin Chen struct resource *res; 73043d24e76SNicolin Chen const uint32_t *iprop; 73143d24e76SNicolin Chen void __iomem *regs; 73243d24e76SNicolin Chen int irq, ret; 73343d24e76SNicolin Chen 73443d24e76SNicolin Chen esai_priv = devm_kzalloc(&pdev->dev, sizeof(*esai_priv), GFP_KERNEL); 73543d24e76SNicolin Chen if (!esai_priv) 73643d24e76SNicolin Chen return -ENOMEM; 73743d24e76SNicolin Chen 73843d24e76SNicolin Chen esai_priv->pdev = pdev; 73943d24e76SNicolin Chen strcpy(esai_priv->name, np->name); 74043d24e76SNicolin Chen 741eaba603fSXiubo Li if (of_property_read_bool(np, "big-endian")) 742eaba603fSXiubo Li fsl_esai_regmap_config.val_format_endian = REGMAP_ENDIAN_BIG; 743eaba603fSXiubo Li 74443d24e76SNicolin Chen /* Get the addresses and IRQ */ 74543d24e76SNicolin Chen res = platform_get_resource(pdev, IORESOURCE_MEM, 0); 74643d24e76SNicolin Chen regs = devm_ioremap_resource(&pdev->dev, res); 74743d24e76SNicolin Chen if (IS_ERR(regs)) 74843d24e76SNicolin Chen return PTR_ERR(regs); 74943d24e76SNicolin Chen 75043d24e76SNicolin Chen esai_priv->regmap = devm_regmap_init_mmio_clk(&pdev->dev, 75143d24e76SNicolin Chen "core", regs, &fsl_esai_regmap_config); 75243d24e76SNicolin Chen if (IS_ERR(esai_priv->regmap)) { 75343d24e76SNicolin Chen dev_err(&pdev->dev, "failed to init regmap: %ld\n", 75443d24e76SNicolin Chen PTR_ERR(esai_priv->regmap)); 75543d24e76SNicolin Chen return PTR_ERR(esai_priv->regmap); 75643d24e76SNicolin Chen } 75743d24e76SNicolin Chen 75843d24e76SNicolin Chen esai_priv->coreclk = devm_clk_get(&pdev->dev, "core"); 75943d24e76SNicolin Chen if (IS_ERR(esai_priv->coreclk)) { 76043d24e76SNicolin Chen dev_err(&pdev->dev, "failed to get core clock: %ld\n", 76143d24e76SNicolin Chen PTR_ERR(esai_priv->coreclk)); 76243d24e76SNicolin Chen return PTR_ERR(esai_priv->coreclk); 76343d24e76SNicolin Chen } 76443d24e76SNicolin Chen 76543d24e76SNicolin Chen esai_priv->extalclk = devm_clk_get(&pdev->dev, "extal"); 76643d24e76SNicolin Chen if (IS_ERR(esai_priv->extalclk)) 76743d24e76SNicolin Chen dev_warn(&pdev->dev, "failed to get extal clock: %ld\n", 76843d24e76SNicolin Chen PTR_ERR(esai_priv->extalclk)); 76943d24e76SNicolin Chen 77043d24e76SNicolin Chen esai_priv->fsysclk = devm_clk_get(&pdev->dev, "fsys"); 77143d24e76SNicolin Chen if (IS_ERR(esai_priv->fsysclk)) 77243d24e76SNicolin Chen dev_warn(&pdev->dev, "failed to get fsys clock: %ld\n", 77343d24e76SNicolin Chen PTR_ERR(esai_priv->fsysclk)); 77443d24e76SNicolin Chen 77543d24e76SNicolin Chen irq = platform_get_irq(pdev, 0); 77643d24e76SNicolin Chen if (irq < 0) { 77743d24e76SNicolin Chen dev_err(&pdev->dev, "no irq for node %s\n", np->full_name); 77843d24e76SNicolin Chen return irq; 77943d24e76SNicolin Chen } 78043d24e76SNicolin Chen 78143d24e76SNicolin Chen ret = devm_request_irq(&pdev->dev, irq, esai_isr, 0, 78243d24e76SNicolin Chen esai_priv->name, esai_priv); 78343d24e76SNicolin Chen if (ret) { 78443d24e76SNicolin Chen dev_err(&pdev->dev, "failed to claim irq %u\n", irq); 78543d24e76SNicolin Chen return ret; 78643d24e76SNicolin Chen } 78743d24e76SNicolin Chen 78843d24e76SNicolin Chen /* Set a default slot size */ 78943d24e76SNicolin Chen esai_priv->slot_width = 32; 79043d24e76SNicolin Chen 791*de0d712aSShengjiu Wang /* Set a default slot number */ 792*de0d712aSShengjiu Wang esai_priv->slots = 2; 793*de0d712aSShengjiu Wang 79443d24e76SNicolin Chen /* Set a default master/slave state */ 79543d24e76SNicolin Chen esai_priv->slave_mode = true; 79643d24e76SNicolin Chen 79743d24e76SNicolin Chen /* Determine the FIFO depth */ 79843d24e76SNicolin Chen iprop = of_get_property(np, "fsl,fifo-depth", NULL); 79943d24e76SNicolin Chen if (iprop) 80043d24e76SNicolin Chen esai_priv->fifo_depth = be32_to_cpup(iprop); 80143d24e76SNicolin Chen else 80243d24e76SNicolin Chen esai_priv->fifo_depth = 64; 80343d24e76SNicolin Chen 80443d24e76SNicolin Chen esai_priv->dma_params_tx.maxburst = 16; 80543d24e76SNicolin Chen esai_priv->dma_params_rx.maxburst = 16; 80643d24e76SNicolin Chen esai_priv->dma_params_tx.addr = res->start + REG_ESAI_ETDR; 80743d24e76SNicolin Chen esai_priv->dma_params_rx.addr = res->start + REG_ESAI_ERDR; 80843d24e76SNicolin Chen 80943d24e76SNicolin Chen esai_priv->synchronous = 81043d24e76SNicolin Chen of_property_read_bool(np, "fsl,esai-synchronous"); 81143d24e76SNicolin Chen 81243d24e76SNicolin Chen /* Implement full symmetry for synchronous mode */ 81343d24e76SNicolin Chen if (esai_priv->synchronous) { 81443d24e76SNicolin Chen fsl_esai_dai.symmetric_rates = 1; 81543d24e76SNicolin Chen fsl_esai_dai.symmetric_channels = 1; 81643d24e76SNicolin Chen fsl_esai_dai.symmetric_samplebits = 1; 81743d24e76SNicolin Chen } 81843d24e76SNicolin Chen 81943d24e76SNicolin Chen dev_set_drvdata(&pdev->dev, esai_priv); 82043d24e76SNicolin Chen 82143d24e76SNicolin Chen /* Reset ESAI unit */ 82243d24e76SNicolin Chen ret = regmap_write(esai_priv->regmap, REG_ESAI_ECR, ESAI_ECR_ERST); 82343d24e76SNicolin Chen if (ret) { 82443d24e76SNicolin Chen dev_err(&pdev->dev, "failed to reset ESAI: %d\n", ret); 82543d24e76SNicolin Chen return ret; 82643d24e76SNicolin Chen } 82743d24e76SNicolin Chen 82843d24e76SNicolin Chen /* 82943d24e76SNicolin Chen * We need to enable ESAI so as to access some of its registers. 83043d24e76SNicolin Chen * Otherwise, we would fail to dump regmap from user space. 83143d24e76SNicolin Chen */ 83243d24e76SNicolin Chen ret = regmap_write(esai_priv->regmap, REG_ESAI_ECR, ESAI_ECR_ESAIEN); 83343d24e76SNicolin Chen if (ret) { 83443d24e76SNicolin Chen dev_err(&pdev->dev, "failed to enable ESAI: %d\n", ret); 83543d24e76SNicolin Chen return ret; 83643d24e76SNicolin Chen } 83743d24e76SNicolin Chen 83843d24e76SNicolin Chen ret = devm_snd_soc_register_component(&pdev->dev, &fsl_esai_component, 83943d24e76SNicolin Chen &fsl_esai_dai, 1); 84043d24e76SNicolin Chen if (ret) { 84143d24e76SNicolin Chen dev_err(&pdev->dev, "failed to register DAI: %d\n", ret); 84243d24e76SNicolin Chen return ret; 84343d24e76SNicolin Chen } 84443d24e76SNicolin Chen 84543d24e76SNicolin Chen ret = imx_pcm_dma_init(pdev); 84643d24e76SNicolin Chen if (ret) 84743d24e76SNicolin Chen dev_err(&pdev->dev, "failed to init imx pcm dma: %d\n", ret); 84843d24e76SNicolin Chen 84943d24e76SNicolin Chen return ret; 85043d24e76SNicolin Chen } 85143d24e76SNicolin Chen 85243d24e76SNicolin Chen static const struct of_device_id fsl_esai_dt_ids[] = { 85343d24e76SNicolin Chen { .compatible = "fsl,imx35-esai", }, 854b21cc2f5SXiubo Li { .compatible = "fsl,vf610-esai", }, 85543d24e76SNicolin Chen {} 85643d24e76SNicolin Chen }; 85743d24e76SNicolin Chen MODULE_DEVICE_TABLE(of, fsl_esai_dt_ids); 85843d24e76SNicolin Chen 85943d24e76SNicolin Chen static struct platform_driver fsl_esai_driver = { 86043d24e76SNicolin Chen .probe = fsl_esai_probe, 86143d24e76SNicolin Chen .driver = { 86243d24e76SNicolin Chen .name = "fsl-esai-dai", 86343d24e76SNicolin Chen .owner = THIS_MODULE, 86443d24e76SNicolin Chen .of_match_table = fsl_esai_dt_ids, 86543d24e76SNicolin Chen }, 86643d24e76SNicolin Chen }; 86743d24e76SNicolin Chen 86843d24e76SNicolin Chen module_platform_driver(fsl_esai_driver); 86943d24e76SNicolin Chen 87043d24e76SNicolin Chen MODULE_AUTHOR("Freescale Semiconductor, Inc."); 87143d24e76SNicolin Chen MODULE_DESCRIPTION("Freescale ESAI CPU DAI driver"); 87243d24e76SNicolin Chen MODULE_LICENSE("GPL v2"); 87343d24e76SNicolin Chen MODULE_ALIAS("platform:fsl-esai-dai"); 874