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 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; 5743d24e76SNicolin Chen u32 fifo_depth; 5843d24e76SNicolin Chen u32 slot_width; 5943d24e76SNicolin Chen u32 hck_rate[2]; 60f975ca46SNicolin Chen u32 sck_rate[2]; 61f975ca46SNicolin Chen bool hck_dir[2]; 6243d24e76SNicolin Chen bool sck_div[2]; 6343d24e76SNicolin Chen bool slave_mode; 6443d24e76SNicolin Chen bool synchronous; 6543d24e76SNicolin Chen char name[32]; 6643d24e76SNicolin Chen }; 6743d24e76SNicolin Chen 6843d24e76SNicolin Chen static irqreturn_t esai_isr(int irq, void *devid) 6943d24e76SNicolin Chen { 7043d24e76SNicolin Chen struct fsl_esai *esai_priv = (struct fsl_esai *)devid; 7143d24e76SNicolin Chen struct platform_device *pdev = esai_priv->pdev; 7243d24e76SNicolin Chen u32 esr; 7343d24e76SNicolin Chen 7443d24e76SNicolin Chen regmap_read(esai_priv->regmap, REG_ESAI_ESR, &esr); 7543d24e76SNicolin Chen 7643d24e76SNicolin Chen if (esr & ESAI_ESR_TINIT_MASK) 7743d24e76SNicolin Chen dev_dbg(&pdev->dev, "isr: Transmition Initialized\n"); 7843d24e76SNicolin Chen 7943d24e76SNicolin Chen if (esr & ESAI_ESR_RFF_MASK) 8043d24e76SNicolin Chen dev_warn(&pdev->dev, "isr: Receiving overrun\n"); 8143d24e76SNicolin Chen 8243d24e76SNicolin Chen if (esr & ESAI_ESR_TFE_MASK) 8343d24e76SNicolin Chen dev_warn(&pdev->dev, "isr: Transmition underrun\n"); 8443d24e76SNicolin Chen 8543d24e76SNicolin Chen if (esr & ESAI_ESR_TLS_MASK) 8643d24e76SNicolin Chen dev_dbg(&pdev->dev, "isr: Just transmitted the last slot\n"); 8743d24e76SNicolin Chen 8843d24e76SNicolin Chen if (esr & ESAI_ESR_TDE_MASK) 8943d24e76SNicolin Chen dev_dbg(&pdev->dev, "isr: Transmition data exception\n"); 9043d24e76SNicolin Chen 9143d24e76SNicolin Chen if (esr & ESAI_ESR_TED_MASK) 9243d24e76SNicolin Chen dev_dbg(&pdev->dev, "isr: Transmitting even slots\n"); 9343d24e76SNicolin Chen 9443d24e76SNicolin Chen if (esr & ESAI_ESR_TD_MASK) 9543d24e76SNicolin Chen dev_dbg(&pdev->dev, "isr: Transmitting data\n"); 9643d24e76SNicolin Chen 9743d24e76SNicolin Chen if (esr & ESAI_ESR_RLS_MASK) 9843d24e76SNicolin Chen dev_dbg(&pdev->dev, "isr: Just received the last slot\n"); 9943d24e76SNicolin Chen 10043d24e76SNicolin Chen if (esr & ESAI_ESR_RDE_MASK) 10143d24e76SNicolin Chen dev_dbg(&pdev->dev, "isr: Receiving data exception\n"); 10243d24e76SNicolin Chen 10343d24e76SNicolin Chen if (esr & ESAI_ESR_RED_MASK) 10443d24e76SNicolin Chen dev_dbg(&pdev->dev, "isr: Receiving even slots\n"); 10543d24e76SNicolin Chen 10643d24e76SNicolin Chen if (esr & ESAI_ESR_RD_MASK) 10743d24e76SNicolin Chen dev_dbg(&pdev->dev, "isr: Receiving data\n"); 10843d24e76SNicolin Chen 10943d24e76SNicolin Chen return IRQ_HANDLED; 11043d24e76SNicolin Chen } 11143d24e76SNicolin Chen 11243d24e76SNicolin Chen /** 11343d24e76SNicolin Chen * This function is used to calculate the divisors of psr, pm, fp and it is 11443d24e76SNicolin Chen * supposed to be called in set_dai_sysclk() and set_bclk(). 11543d24e76SNicolin Chen * 11643d24e76SNicolin Chen * @ratio: desired overall ratio for the paticipating dividers 11743d24e76SNicolin Chen * @usefp: for HCK setting, there is no need to set fp divider 11843d24e76SNicolin Chen * @fp: bypass other dividers by setting fp directly if fp != 0 11943d24e76SNicolin Chen * @tx: current setting is for playback or capture 12043d24e76SNicolin Chen */ 12143d24e76SNicolin Chen static int fsl_esai_divisor_cal(struct snd_soc_dai *dai, bool tx, u32 ratio, 12243d24e76SNicolin Chen bool usefp, u32 fp) 12343d24e76SNicolin Chen { 12443d24e76SNicolin Chen struct fsl_esai *esai_priv = snd_soc_dai_get_drvdata(dai); 12543d24e76SNicolin Chen u32 psr, pm = 999, maxfp, prod, sub, savesub, i, j; 12643d24e76SNicolin Chen 12743d24e76SNicolin Chen maxfp = usefp ? 16 : 1; 12843d24e76SNicolin Chen 12943d24e76SNicolin Chen if (usefp && fp) 13043d24e76SNicolin Chen goto out_fp; 13143d24e76SNicolin Chen 13243d24e76SNicolin Chen if (ratio > 2 * 8 * 256 * maxfp || ratio < 2) { 13343d24e76SNicolin Chen dev_err(dai->dev, "the ratio is out of range (2 ~ %d)\n", 13443d24e76SNicolin Chen 2 * 8 * 256 * maxfp); 13543d24e76SNicolin Chen return -EINVAL; 13643d24e76SNicolin Chen } else if (ratio % 2) { 13743d24e76SNicolin Chen dev_err(dai->dev, "the raio must be even if using upper divider\n"); 13843d24e76SNicolin Chen return -EINVAL; 13943d24e76SNicolin Chen } 14043d24e76SNicolin Chen 14143d24e76SNicolin Chen ratio /= 2; 14243d24e76SNicolin Chen 14343d24e76SNicolin Chen psr = ratio <= 256 * maxfp ? ESAI_xCCR_xPSR_BYPASS : ESAI_xCCR_xPSR_DIV8; 14443d24e76SNicolin Chen 14543d24e76SNicolin Chen /* Set the max fluctuation -- 0.1% of the max devisor */ 14643d24e76SNicolin Chen savesub = (psr ? 1 : 8) * 256 * maxfp / 1000; 14743d24e76SNicolin Chen 14843d24e76SNicolin Chen /* Find the best value for PM */ 14943d24e76SNicolin Chen for (i = 1; i <= 256; i++) { 15043d24e76SNicolin Chen for (j = 1; j <= maxfp; j++) { 15143d24e76SNicolin Chen /* PSR (1 or 8) * PM (1 ~ 256) * FP (1 ~ 16) */ 15243d24e76SNicolin Chen prod = (psr ? 1 : 8) * i * j; 15343d24e76SNicolin Chen 15443d24e76SNicolin Chen if (prod == ratio) 15543d24e76SNicolin Chen sub = 0; 15643d24e76SNicolin Chen else if (prod / ratio == 1) 15743d24e76SNicolin Chen sub = prod - ratio; 15843d24e76SNicolin Chen else if (ratio / prod == 1) 15943d24e76SNicolin Chen sub = ratio - prod; 16043d24e76SNicolin Chen else 16143d24e76SNicolin Chen continue; 16243d24e76SNicolin Chen 16343d24e76SNicolin Chen /* Calculate the fraction */ 16443d24e76SNicolin Chen sub = sub * 1000 / ratio; 16543d24e76SNicolin Chen if (sub < savesub) { 16643d24e76SNicolin Chen savesub = sub; 16743d24e76SNicolin Chen pm = i; 16843d24e76SNicolin Chen fp = j; 16943d24e76SNicolin Chen } 17043d24e76SNicolin Chen 17143d24e76SNicolin Chen /* We are lucky */ 17243d24e76SNicolin Chen if (savesub == 0) 17343d24e76SNicolin Chen goto out; 17443d24e76SNicolin Chen } 17543d24e76SNicolin Chen } 17643d24e76SNicolin Chen 17743d24e76SNicolin Chen if (pm == 999) { 17843d24e76SNicolin Chen dev_err(dai->dev, "failed to calculate proper divisors\n"); 17943d24e76SNicolin Chen return -EINVAL; 18043d24e76SNicolin Chen } 18143d24e76SNicolin Chen 18243d24e76SNicolin Chen out: 18343d24e76SNicolin Chen regmap_update_bits(esai_priv->regmap, REG_ESAI_xCCR(tx), 18443d24e76SNicolin Chen ESAI_xCCR_xPSR_MASK | ESAI_xCCR_xPM_MASK, 18543d24e76SNicolin Chen psr | ESAI_xCCR_xPM(pm)); 18643d24e76SNicolin Chen 18743d24e76SNicolin Chen out_fp: 18843d24e76SNicolin Chen /* Bypass fp if not being required */ 18943d24e76SNicolin Chen if (maxfp <= 1) 19043d24e76SNicolin Chen return 0; 19143d24e76SNicolin Chen 19243d24e76SNicolin Chen regmap_update_bits(esai_priv->regmap, REG_ESAI_xCCR(tx), 19343d24e76SNicolin Chen ESAI_xCCR_xFP_MASK, ESAI_xCCR_xFP(fp)); 19443d24e76SNicolin Chen 19543d24e76SNicolin Chen return 0; 19643d24e76SNicolin Chen } 19743d24e76SNicolin Chen 19843d24e76SNicolin Chen /** 19943d24e76SNicolin Chen * This function mainly configures the clock frequency of MCLK (HCKT/HCKR) 20043d24e76SNicolin Chen * 20143d24e76SNicolin Chen * @Parameters: 20243d24e76SNicolin Chen * clk_id: The clock source of HCKT/HCKR 20343d24e76SNicolin Chen * (Input from outside; output from inside, FSYS or EXTAL) 20443d24e76SNicolin Chen * freq: The required clock rate of HCKT/HCKR 20543d24e76SNicolin Chen * dir: The clock direction of HCKT/HCKR 20643d24e76SNicolin Chen * 20743d24e76SNicolin Chen * Note: If the direction is input, we do not care about clk_id. 20843d24e76SNicolin Chen */ 20943d24e76SNicolin Chen static int fsl_esai_set_dai_sysclk(struct snd_soc_dai *dai, int clk_id, 21043d24e76SNicolin Chen unsigned int freq, int dir) 21143d24e76SNicolin Chen { 21243d24e76SNicolin Chen struct fsl_esai *esai_priv = snd_soc_dai_get_drvdata(dai); 21343d24e76SNicolin Chen struct clk *clksrc = esai_priv->extalclk; 21443d24e76SNicolin Chen bool tx = clk_id <= ESAI_HCKT_EXTAL; 21543d24e76SNicolin Chen bool in = dir == SND_SOC_CLOCK_IN; 2163e185238SXiubo Li u32 ratio, ecr = 0; 21743d24e76SNicolin Chen unsigned long clk_rate; 2183e185238SXiubo Li int ret; 21943d24e76SNicolin Chen 220f975ca46SNicolin Chen /* Bypass divider settings if the requirement doesn't change */ 221f975ca46SNicolin Chen if (freq == esai_priv->hck_rate[tx] && dir == esai_priv->hck_dir[tx]) 222f975ca46SNicolin Chen return 0; 22343d24e76SNicolin Chen 22443d24e76SNicolin Chen /* sck_div can be only bypassed if ETO/ERO=0 and SNC_SOC_CLOCK_OUT */ 22543d24e76SNicolin Chen esai_priv->sck_div[tx] = true; 22643d24e76SNicolin Chen 22743d24e76SNicolin Chen /* Set the direction of HCKT/HCKR pins */ 22843d24e76SNicolin Chen regmap_update_bits(esai_priv->regmap, REG_ESAI_xCCR(tx), 22943d24e76SNicolin Chen ESAI_xCCR_xHCKD, in ? 0 : ESAI_xCCR_xHCKD); 23043d24e76SNicolin Chen 23143d24e76SNicolin Chen if (in) 23243d24e76SNicolin Chen goto out; 23343d24e76SNicolin Chen 23443d24e76SNicolin Chen switch (clk_id) { 23543d24e76SNicolin Chen case ESAI_HCKT_FSYS: 23643d24e76SNicolin Chen case ESAI_HCKR_FSYS: 23743d24e76SNicolin Chen clksrc = esai_priv->fsysclk; 23843d24e76SNicolin Chen break; 23943d24e76SNicolin Chen case ESAI_HCKT_EXTAL: 24043d24e76SNicolin Chen ecr |= ESAI_ECR_ETI; 24143d24e76SNicolin Chen case ESAI_HCKR_EXTAL: 24243d24e76SNicolin Chen ecr |= ESAI_ECR_ERI; 24343d24e76SNicolin Chen break; 24443d24e76SNicolin Chen default: 24543d24e76SNicolin Chen return -EINVAL; 24643d24e76SNicolin Chen } 24743d24e76SNicolin Chen 24843d24e76SNicolin Chen if (IS_ERR(clksrc)) { 24943d24e76SNicolin Chen dev_err(dai->dev, "no assigned %s clock\n", 25043d24e76SNicolin Chen clk_id % 2 ? "extal" : "fsys"); 25143d24e76SNicolin Chen return PTR_ERR(clksrc); 25243d24e76SNicolin Chen } 25343d24e76SNicolin Chen clk_rate = clk_get_rate(clksrc); 25443d24e76SNicolin Chen 25543d24e76SNicolin Chen ratio = clk_rate / freq; 25643d24e76SNicolin Chen if (ratio * freq > clk_rate) 25743d24e76SNicolin Chen ret = ratio * freq - clk_rate; 25843d24e76SNicolin Chen else if (ratio * freq < clk_rate) 25943d24e76SNicolin Chen ret = clk_rate - ratio * freq; 26043d24e76SNicolin Chen else 26143d24e76SNicolin Chen ret = 0; 26243d24e76SNicolin Chen 26343d24e76SNicolin Chen /* Block if clock source can not be divided into the required rate */ 26443d24e76SNicolin Chen if (ret != 0 && clk_rate / ret < 1000) { 26543d24e76SNicolin Chen dev_err(dai->dev, "failed to derive required HCK%c rate\n", 26643d24e76SNicolin Chen tx ? 'T' : 'R'); 26743d24e76SNicolin Chen return -EINVAL; 26843d24e76SNicolin Chen } 26943d24e76SNicolin Chen 27057ebbcafSNicolin Chen /* Only EXTAL source can be output directly without using PSR and PM */ 27157ebbcafSNicolin Chen if (ratio == 1 && clksrc == esai_priv->extalclk) { 27243d24e76SNicolin Chen /* Bypass all the dividers if not being needed */ 27343d24e76SNicolin Chen ecr |= tx ? ESAI_ECR_ETO : ESAI_ECR_ERO; 27443d24e76SNicolin Chen goto out; 27557ebbcafSNicolin Chen } else if (ratio < 2) { 27657ebbcafSNicolin Chen /* The ratio should be no less than 2 if using other sources */ 27757ebbcafSNicolin Chen dev_err(dai->dev, "failed to derive required HCK%c rate\n", 27857ebbcafSNicolin Chen tx ? 'T' : 'R'); 27957ebbcafSNicolin Chen return -EINVAL; 28043d24e76SNicolin Chen } 28143d24e76SNicolin Chen 28243d24e76SNicolin Chen ret = fsl_esai_divisor_cal(dai, tx, ratio, false, 0); 28343d24e76SNicolin Chen if (ret) 28443d24e76SNicolin Chen return ret; 28543d24e76SNicolin Chen 28643d24e76SNicolin Chen esai_priv->sck_div[tx] = false; 28743d24e76SNicolin Chen 28843d24e76SNicolin Chen out: 289f975ca46SNicolin Chen esai_priv->hck_dir[tx] = dir; 29043d24e76SNicolin Chen esai_priv->hck_rate[tx] = freq; 29143d24e76SNicolin Chen 29243d24e76SNicolin Chen regmap_update_bits(esai_priv->regmap, REG_ESAI_ECR, 29343d24e76SNicolin Chen tx ? ESAI_ECR_ETI | ESAI_ECR_ETO : 29443d24e76SNicolin Chen ESAI_ECR_ERI | ESAI_ECR_ERO, ecr); 29543d24e76SNicolin Chen 29643d24e76SNicolin Chen return 0; 29743d24e76SNicolin Chen } 29843d24e76SNicolin Chen 29943d24e76SNicolin Chen /** 30043d24e76SNicolin Chen * This function configures the related dividers according to the bclk rate 30143d24e76SNicolin Chen */ 30243d24e76SNicolin Chen static int fsl_esai_set_bclk(struct snd_soc_dai *dai, bool tx, u32 freq) 30343d24e76SNicolin Chen { 30443d24e76SNicolin Chen struct fsl_esai *esai_priv = snd_soc_dai_get_drvdata(dai); 30543d24e76SNicolin Chen u32 hck_rate = esai_priv->hck_rate[tx]; 30643d24e76SNicolin Chen u32 sub, ratio = hck_rate / freq; 307f975ca46SNicolin Chen int ret; 30843d24e76SNicolin Chen 309f975ca46SNicolin Chen /* Don't apply for fully slave mode or unchanged bclk */ 310f975ca46SNicolin Chen if (esai_priv->slave_mode || esai_priv->sck_rate[tx] == freq) 31143d24e76SNicolin Chen return 0; 31243d24e76SNicolin Chen 31343d24e76SNicolin Chen if (ratio * freq > hck_rate) 31443d24e76SNicolin Chen sub = ratio * freq - hck_rate; 31543d24e76SNicolin Chen else if (ratio * freq < hck_rate) 31643d24e76SNicolin Chen sub = hck_rate - ratio * freq; 31743d24e76SNicolin Chen else 31843d24e76SNicolin Chen sub = 0; 31943d24e76SNicolin Chen 32043d24e76SNicolin Chen /* Block if clock source can not be divided into the required rate */ 32143d24e76SNicolin Chen if (sub != 0 && hck_rate / sub < 1000) { 32243d24e76SNicolin Chen dev_err(dai->dev, "failed to derive required SCK%c rate\n", 32343d24e76SNicolin Chen tx ? 'T' : 'R'); 32443d24e76SNicolin Chen return -EINVAL; 32543d24e76SNicolin Chen } 32643d24e76SNicolin Chen 32789e47f62SNicolin Chen /* The ratio should be contented by FP alone if bypassing PM and PSR */ 32889e47f62SNicolin Chen if (!esai_priv->sck_div[tx] && (ratio > 16 || ratio == 0)) { 32943d24e76SNicolin Chen dev_err(dai->dev, "the ratio is out of range (1 ~ 16)\n"); 33043d24e76SNicolin Chen return -EINVAL; 33143d24e76SNicolin Chen } 33243d24e76SNicolin Chen 333f975ca46SNicolin Chen ret = fsl_esai_divisor_cal(dai, tx, ratio, true, 33443d24e76SNicolin Chen esai_priv->sck_div[tx] ? 0 : ratio); 335f975ca46SNicolin Chen if (ret) 336f975ca46SNicolin Chen return ret; 337f975ca46SNicolin Chen 338f975ca46SNicolin Chen /* Save current bclk rate */ 339f975ca46SNicolin Chen esai_priv->sck_rate[tx] = freq; 340f975ca46SNicolin Chen 341f975ca46SNicolin Chen return 0; 34243d24e76SNicolin Chen } 34343d24e76SNicolin Chen 34443d24e76SNicolin Chen static int fsl_esai_set_dai_tdm_slot(struct snd_soc_dai *dai, u32 tx_mask, 34543d24e76SNicolin Chen u32 rx_mask, int slots, int slot_width) 34643d24e76SNicolin Chen { 34743d24e76SNicolin Chen struct fsl_esai *esai_priv = snd_soc_dai_get_drvdata(dai); 34843d24e76SNicolin Chen 34943d24e76SNicolin Chen regmap_update_bits(esai_priv->regmap, REG_ESAI_TCCR, 35043d24e76SNicolin Chen ESAI_xCCR_xDC_MASK, ESAI_xCCR_xDC(slots)); 35143d24e76SNicolin Chen 35243d24e76SNicolin Chen regmap_update_bits(esai_priv->regmap, REG_ESAI_TSMA, 35343d24e76SNicolin Chen ESAI_xSMA_xS_MASK, ESAI_xSMA_xS(tx_mask)); 35443d24e76SNicolin Chen regmap_update_bits(esai_priv->regmap, REG_ESAI_TSMB, 355236014acSXiubo Li ESAI_xSMB_xS_MASK, ESAI_xSMB_xS(tx_mask)); 35643d24e76SNicolin Chen 35743d24e76SNicolin Chen regmap_update_bits(esai_priv->regmap, REG_ESAI_RCCR, 35843d24e76SNicolin Chen ESAI_xCCR_xDC_MASK, ESAI_xCCR_xDC(slots)); 35943d24e76SNicolin Chen 36043d24e76SNicolin Chen regmap_update_bits(esai_priv->regmap, REG_ESAI_RSMA, 36143d24e76SNicolin Chen ESAI_xSMA_xS_MASK, ESAI_xSMA_xS(rx_mask)); 36243d24e76SNicolin Chen regmap_update_bits(esai_priv->regmap, REG_ESAI_RSMB, 363236014acSXiubo Li ESAI_xSMB_xS_MASK, ESAI_xSMB_xS(rx_mask)); 36443d24e76SNicolin Chen 36543d24e76SNicolin Chen esai_priv->slot_width = slot_width; 36643d24e76SNicolin Chen 36743d24e76SNicolin Chen return 0; 36843d24e76SNicolin Chen } 36943d24e76SNicolin Chen 37043d24e76SNicolin Chen static int fsl_esai_set_dai_fmt(struct snd_soc_dai *dai, unsigned int fmt) 37143d24e76SNicolin Chen { 37243d24e76SNicolin Chen struct fsl_esai *esai_priv = snd_soc_dai_get_drvdata(dai); 37343d24e76SNicolin Chen u32 xcr = 0, xccr = 0, mask; 37443d24e76SNicolin Chen 37543d24e76SNicolin Chen /* DAI mode */ 37643d24e76SNicolin Chen switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) { 37743d24e76SNicolin Chen case SND_SOC_DAIFMT_I2S: 37843d24e76SNicolin Chen /* Data on rising edge of bclk, frame low, 1clk before data */ 37943d24e76SNicolin Chen xcr |= ESAI_xCR_xFSR; 38043d24e76SNicolin Chen xccr |= ESAI_xCCR_xFSP | ESAI_xCCR_xCKP | ESAI_xCCR_xHCKP; 38143d24e76SNicolin Chen break; 38243d24e76SNicolin Chen case SND_SOC_DAIFMT_LEFT_J: 38343d24e76SNicolin Chen /* Data on rising edge of bclk, frame high */ 38443d24e76SNicolin Chen xccr |= ESAI_xCCR_xCKP | ESAI_xCCR_xHCKP; 38543d24e76SNicolin Chen break; 38643d24e76SNicolin Chen case SND_SOC_DAIFMT_RIGHT_J: 38743d24e76SNicolin Chen /* Data on rising edge of bclk, frame high, right aligned */ 38843d24e76SNicolin Chen xccr |= ESAI_xCCR_xCKP | ESAI_xCCR_xHCKP | ESAI_xCR_xWA; 38943d24e76SNicolin Chen break; 39043d24e76SNicolin Chen case SND_SOC_DAIFMT_DSP_A: 39143d24e76SNicolin Chen /* Data on rising edge of bclk, frame high, 1clk before data */ 39243d24e76SNicolin Chen xcr |= ESAI_xCR_xFSL | ESAI_xCR_xFSR; 39343d24e76SNicolin Chen xccr |= ESAI_xCCR_xCKP | ESAI_xCCR_xHCKP; 39443d24e76SNicolin Chen break; 39543d24e76SNicolin Chen case SND_SOC_DAIFMT_DSP_B: 39643d24e76SNicolin Chen /* Data on rising edge of bclk, frame high */ 39743d24e76SNicolin Chen xcr |= ESAI_xCR_xFSL; 39843d24e76SNicolin Chen xccr |= ESAI_xCCR_xCKP | ESAI_xCCR_xHCKP; 39943d24e76SNicolin Chen break; 40043d24e76SNicolin Chen default: 40143d24e76SNicolin Chen return -EINVAL; 40243d24e76SNicolin Chen } 40343d24e76SNicolin Chen 40443d24e76SNicolin Chen /* DAI clock inversion */ 40543d24e76SNicolin Chen switch (fmt & SND_SOC_DAIFMT_INV_MASK) { 40643d24e76SNicolin Chen case SND_SOC_DAIFMT_NB_NF: 40743d24e76SNicolin Chen /* Nothing to do for both normal cases */ 40843d24e76SNicolin Chen break; 40943d24e76SNicolin Chen case SND_SOC_DAIFMT_IB_NF: 41043d24e76SNicolin Chen /* Invert bit clock */ 41143d24e76SNicolin Chen xccr ^= ESAI_xCCR_xCKP | ESAI_xCCR_xHCKP; 41243d24e76SNicolin Chen break; 41343d24e76SNicolin Chen case SND_SOC_DAIFMT_NB_IF: 41443d24e76SNicolin Chen /* Invert frame clock */ 41543d24e76SNicolin Chen xccr ^= ESAI_xCCR_xFSP; 41643d24e76SNicolin Chen break; 41743d24e76SNicolin Chen case SND_SOC_DAIFMT_IB_IF: 41843d24e76SNicolin Chen /* Invert both clocks */ 41943d24e76SNicolin Chen xccr ^= ESAI_xCCR_xCKP | ESAI_xCCR_xHCKP | ESAI_xCCR_xFSP; 42043d24e76SNicolin Chen break; 42143d24e76SNicolin Chen default: 42243d24e76SNicolin Chen return -EINVAL; 42343d24e76SNicolin Chen } 42443d24e76SNicolin Chen 42543d24e76SNicolin Chen esai_priv->slave_mode = false; 42643d24e76SNicolin Chen 42743d24e76SNicolin Chen /* DAI clock master masks */ 42843d24e76SNicolin Chen switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) { 42943d24e76SNicolin Chen case SND_SOC_DAIFMT_CBM_CFM: 43043d24e76SNicolin Chen esai_priv->slave_mode = true; 43143d24e76SNicolin Chen break; 43243d24e76SNicolin Chen case SND_SOC_DAIFMT_CBS_CFM: 43343d24e76SNicolin Chen xccr |= ESAI_xCCR_xCKD; 43443d24e76SNicolin Chen break; 43543d24e76SNicolin Chen case SND_SOC_DAIFMT_CBM_CFS: 43643d24e76SNicolin Chen xccr |= ESAI_xCCR_xFSD; 43743d24e76SNicolin Chen break; 43843d24e76SNicolin Chen case SND_SOC_DAIFMT_CBS_CFS: 43943d24e76SNicolin Chen xccr |= ESAI_xCCR_xFSD | ESAI_xCCR_xCKD; 44043d24e76SNicolin Chen break; 44143d24e76SNicolin Chen default: 44243d24e76SNicolin Chen return -EINVAL; 44343d24e76SNicolin Chen } 44443d24e76SNicolin Chen 44543d24e76SNicolin Chen mask = ESAI_xCR_xFSL | ESAI_xCR_xFSR; 44643d24e76SNicolin Chen regmap_update_bits(esai_priv->regmap, REG_ESAI_TCR, mask, xcr); 44743d24e76SNicolin Chen regmap_update_bits(esai_priv->regmap, REG_ESAI_RCR, mask, xcr); 44843d24e76SNicolin Chen 44943d24e76SNicolin Chen mask = ESAI_xCCR_xCKP | ESAI_xCCR_xHCKP | ESAI_xCCR_xFSP | 45043d24e76SNicolin Chen ESAI_xCCR_xFSD | ESAI_xCCR_xCKD | ESAI_xCR_xWA; 45143d24e76SNicolin Chen regmap_update_bits(esai_priv->regmap, REG_ESAI_TCCR, mask, xccr); 45243d24e76SNicolin Chen regmap_update_bits(esai_priv->regmap, REG_ESAI_RCCR, mask, xccr); 45343d24e76SNicolin Chen 45443d24e76SNicolin Chen return 0; 45543d24e76SNicolin Chen } 45643d24e76SNicolin Chen 45743d24e76SNicolin Chen static int fsl_esai_startup(struct snd_pcm_substream *substream, 45843d24e76SNicolin Chen struct snd_soc_dai *dai) 45943d24e76SNicolin Chen { 46043d24e76SNicolin Chen struct fsl_esai *esai_priv = snd_soc_dai_get_drvdata(dai); 4613e185238SXiubo Li int ret; 46243d24e76SNicolin Chen 46343d24e76SNicolin Chen /* 46443d24e76SNicolin Chen * Some platforms might use the same bit to gate all three or two of 46543d24e76SNicolin Chen * clocks, so keep all clocks open/close at the same time for safety 46643d24e76SNicolin Chen */ 46733529ec9SFabio Estevam ret = clk_prepare_enable(esai_priv->coreclk); 46833529ec9SFabio Estevam if (ret) 46933529ec9SFabio Estevam return ret; 47033529ec9SFabio Estevam if (!IS_ERR(esai_priv->extalclk)) { 47133529ec9SFabio Estevam ret = clk_prepare_enable(esai_priv->extalclk); 47233529ec9SFabio Estevam if (ret) 47333529ec9SFabio Estevam goto err_extalck; 47433529ec9SFabio Estevam } 47533529ec9SFabio Estevam if (!IS_ERR(esai_priv->fsysclk)) { 47633529ec9SFabio Estevam ret = clk_prepare_enable(esai_priv->fsysclk); 47733529ec9SFabio Estevam if (ret) 47833529ec9SFabio Estevam goto err_fsysclk; 47933529ec9SFabio Estevam } 48043d24e76SNicolin Chen 48143d24e76SNicolin Chen if (!dai->active) { 48243d24e76SNicolin Chen /* Set synchronous mode */ 48343d24e76SNicolin Chen regmap_update_bits(esai_priv->regmap, REG_ESAI_SAICR, 48443d24e76SNicolin Chen ESAI_SAICR_SYNC, esai_priv->synchronous ? 48543d24e76SNicolin Chen ESAI_SAICR_SYNC : 0); 48643d24e76SNicolin Chen 48743d24e76SNicolin Chen /* Set a default slot number -- 2 */ 48843d24e76SNicolin Chen regmap_update_bits(esai_priv->regmap, REG_ESAI_TCCR, 48943d24e76SNicolin Chen ESAI_xCCR_xDC_MASK, ESAI_xCCR_xDC(2)); 49043d24e76SNicolin Chen regmap_update_bits(esai_priv->regmap, REG_ESAI_RCCR, 49143d24e76SNicolin Chen ESAI_xCCR_xDC_MASK, ESAI_xCCR_xDC(2)); 49243d24e76SNicolin Chen } 49343d24e76SNicolin Chen 49443d24e76SNicolin Chen return 0; 49533529ec9SFabio Estevam 49633529ec9SFabio Estevam err_fsysclk: 49733529ec9SFabio Estevam if (!IS_ERR(esai_priv->extalclk)) 49833529ec9SFabio Estevam clk_disable_unprepare(esai_priv->extalclk); 49933529ec9SFabio Estevam err_extalck: 50033529ec9SFabio Estevam clk_disable_unprepare(esai_priv->coreclk); 50133529ec9SFabio Estevam 50233529ec9SFabio Estevam return ret; 50343d24e76SNicolin Chen } 50443d24e76SNicolin Chen 50543d24e76SNicolin Chen static int fsl_esai_hw_params(struct snd_pcm_substream *substream, 50643d24e76SNicolin Chen struct snd_pcm_hw_params *params, 50743d24e76SNicolin Chen struct snd_soc_dai *dai) 50843d24e76SNicolin Chen { 50943d24e76SNicolin Chen struct fsl_esai *esai_priv = snd_soc_dai_get_drvdata(dai); 51043d24e76SNicolin Chen bool tx = substream->stream == SNDRV_PCM_STREAM_PLAYBACK; 51143d24e76SNicolin Chen u32 width = snd_pcm_format_width(params_format(params)); 51243d24e76SNicolin Chen u32 channels = params_channels(params); 5133e185238SXiubo Li u32 bclk, mask, val; 5143e185238SXiubo Li int ret; 51543d24e76SNicolin Chen 51643d24e76SNicolin Chen bclk = params_rate(params) * esai_priv->slot_width * 2; 51743d24e76SNicolin Chen 51843d24e76SNicolin Chen ret = fsl_esai_set_bclk(dai, tx, bclk); 51943d24e76SNicolin Chen if (ret) 52043d24e76SNicolin Chen return ret; 52143d24e76SNicolin Chen 52243d24e76SNicolin Chen /* Use Normal mode to support monaural audio */ 52343d24e76SNicolin Chen regmap_update_bits(esai_priv->regmap, REG_ESAI_xCR(tx), 52443d24e76SNicolin Chen ESAI_xCR_xMOD_MASK, params_channels(params) > 1 ? 52543d24e76SNicolin Chen ESAI_xCR_xMOD_NETWORK : 0); 52643d24e76SNicolin Chen 52743d24e76SNicolin Chen regmap_update_bits(esai_priv->regmap, REG_ESAI_xFCR(tx), 52843d24e76SNicolin Chen ESAI_xFCR_xFR_MASK, ESAI_xFCR_xFR); 52943d24e76SNicolin Chen 53043d24e76SNicolin Chen mask = ESAI_xFCR_xFR_MASK | ESAI_xFCR_xWA_MASK | ESAI_xFCR_xFWM_MASK | 53143d24e76SNicolin Chen (tx ? ESAI_xFCR_TE_MASK | ESAI_xFCR_TIEN : ESAI_xFCR_RE_MASK); 53243d24e76SNicolin Chen val = ESAI_xFCR_xWA(width) | ESAI_xFCR_xFWM(esai_priv->fifo_depth) | 53343d24e76SNicolin Chen (tx ? ESAI_xFCR_TE(channels) | ESAI_xFCR_TIEN : ESAI_xFCR_RE(channels)); 53443d24e76SNicolin Chen 53543d24e76SNicolin Chen regmap_update_bits(esai_priv->regmap, REG_ESAI_xFCR(tx), mask, val); 53643d24e76SNicolin Chen 53743d24e76SNicolin Chen mask = ESAI_xCR_xSWS_MASK | (tx ? ESAI_xCR_PADC : 0); 53843d24e76SNicolin Chen val = ESAI_xCR_xSWS(esai_priv->slot_width, width) | (tx ? ESAI_xCR_PADC : 0); 53943d24e76SNicolin Chen 54043d24e76SNicolin Chen regmap_update_bits(esai_priv->regmap, REG_ESAI_xCR(tx), mask, val); 54143d24e76SNicolin Chen 5424f8210f6SNicolin Chen /* Remove ESAI personal reset by configuring ESAI_PCRC and ESAI_PRRC */ 5434f8210f6SNicolin Chen regmap_update_bits(esai_priv->regmap, REG_ESAI_PRRC, 5444f8210f6SNicolin Chen ESAI_PRRC_PDC_MASK, ESAI_PRRC_PDC(ESAI_GPIO)); 5454f8210f6SNicolin Chen regmap_update_bits(esai_priv->regmap, REG_ESAI_PCRC, 5464f8210f6SNicolin Chen ESAI_PCRC_PC_MASK, ESAI_PCRC_PC(ESAI_GPIO)); 54743d24e76SNicolin Chen return 0; 54843d24e76SNicolin Chen } 54943d24e76SNicolin Chen 55043d24e76SNicolin Chen static void fsl_esai_shutdown(struct snd_pcm_substream *substream, 55143d24e76SNicolin Chen struct snd_soc_dai *dai) 55243d24e76SNicolin Chen { 55343d24e76SNicolin Chen struct fsl_esai *esai_priv = snd_soc_dai_get_drvdata(dai); 55443d24e76SNicolin Chen 55543d24e76SNicolin Chen if (!IS_ERR(esai_priv->fsysclk)) 55643d24e76SNicolin Chen clk_disable_unprepare(esai_priv->fsysclk); 55743d24e76SNicolin Chen if (!IS_ERR(esai_priv->extalclk)) 55843d24e76SNicolin Chen clk_disable_unprepare(esai_priv->extalclk); 55943d24e76SNicolin Chen clk_disable_unprepare(esai_priv->coreclk); 56043d24e76SNicolin Chen } 56143d24e76SNicolin Chen 56243d24e76SNicolin Chen static int fsl_esai_trigger(struct snd_pcm_substream *substream, int cmd, 56343d24e76SNicolin Chen struct snd_soc_dai *dai) 56443d24e76SNicolin Chen { 56543d24e76SNicolin Chen struct fsl_esai *esai_priv = snd_soc_dai_get_drvdata(dai); 56643d24e76SNicolin Chen bool tx = substream->stream == SNDRV_PCM_STREAM_PLAYBACK; 56743d24e76SNicolin Chen u8 i, channels = substream->runtime->channels; 56843d24e76SNicolin Chen 56943d24e76SNicolin Chen switch (cmd) { 57043d24e76SNicolin Chen case SNDRV_PCM_TRIGGER_START: 57143d24e76SNicolin Chen case SNDRV_PCM_TRIGGER_RESUME: 57243d24e76SNicolin Chen case SNDRV_PCM_TRIGGER_PAUSE_RELEASE: 57343d24e76SNicolin Chen regmap_update_bits(esai_priv->regmap, REG_ESAI_xFCR(tx), 57443d24e76SNicolin Chen ESAI_xFCR_xFEN_MASK, ESAI_xFCR_xFEN); 57543d24e76SNicolin Chen 57643d24e76SNicolin Chen /* Write initial words reqiured by ESAI as normal procedure */ 57743d24e76SNicolin Chen for (i = 0; tx && i < channels; i++) 57843d24e76SNicolin Chen regmap_write(esai_priv->regmap, REG_ESAI_ETDR, 0x0); 57943d24e76SNicolin Chen 58043d24e76SNicolin Chen regmap_update_bits(esai_priv->regmap, REG_ESAI_xCR(tx), 58143d24e76SNicolin Chen tx ? ESAI_xCR_TE_MASK : ESAI_xCR_RE_MASK, 58243d24e76SNicolin Chen tx ? ESAI_xCR_TE(channels) : ESAI_xCR_RE(channels)); 58343d24e76SNicolin Chen break; 58443d24e76SNicolin Chen case SNDRV_PCM_TRIGGER_SUSPEND: 58543d24e76SNicolin Chen case SNDRV_PCM_TRIGGER_STOP: 58643d24e76SNicolin Chen case SNDRV_PCM_TRIGGER_PAUSE_PUSH: 58743d24e76SNicolin Chen regmap_update_bits(esai_priv->regmap, REG_ESAI_xCR(tx), 58843d24e76SNicolin Chen tx ? ESAI_xCR_TE_MASK : ESAI_xCR_RE_MASK, 0); 58943d24e76SNicolin Chen 59043d24e76SNicolin Chen /* Disable and reset FIFO */ 59143d24e76SNicolin Chen regmap_update_bits(esai_priv->regmap, REG_ESAI_xFCR(tx), 59243d24e76SNicolin Chen ESAI_xFCR_xFR | ESAI_xFCR_xFEN, ESAI_xFCR_xFR); 59343d24e76SNicolin Chen regmap_update_bits(esai_priv->regmap, REG_ESAI_xFCR(tx), 59443d24e76SNicolin Chen ESAI_xFCR_xFR, 0); 59543d24e76SNicolin Chen break; 59643d24e76SNicolin Chen default: 59743d24e76SNicolin Chen return -EINVAL; 59843d24e76SNicolin Chen } 59943d24e76SNicolin Chen 60043d24e76SNicolin Chen return 0; 60143d24e76SNicolin Chen } 60243d24e76SNicolin Chen 60343d24e76SNicolin Chen static struct snd_soc_dai_ops fsl_esai_dai_ops = { 60443d24e76SNicolin Chen .startup = fsl_esai_startup, 60543d24e76SNicolin Chen .shutdown = fsl_esai_shutdown, 60643d24e76SNicolin Chen .trigger = fsl_esai_trigger, 60743d24e76SNicolin Chen .hw_params = fsl_esai_hw_params, 60843d24e76SNicolin Chen .set_sysclk = fsl_esai_set_dai_sysclk, 60943d24e76SNicolin Chen .set_fmt = fsl_esai_set_dai_fmt, 610a603c8eeSXiubo Li .xlate_tdm_slot_mask = fsl_asoc_xlate_tdm_slot_mask, 61143d24e76SNicolin Chen .set_tdm_slot = fsl_esai_set_dai_tdm_slot, 61243d24e76SNicolin Chen }; 61343d24e76SNicolin Chen 61443d24e76SNicolin Chen static int fsl_esai_dai_probe(struct snd_soc_dai *dai) 61543d24e76SNicolin Chen { 61643d24e76SNicolin Chen struct fsl_esai *esai_priv = snd_soc_dai_get_drvdata(dai); 61743d24e76SNicolin Chen 61843d24e76SNicolin Chen snd_soc_dai_init_dma_data(dai, &esai_priv->dma_params_tx, 61943d24e76SNicolin Chen &esai_priv->dma_params_rx); 62043d24e76SNicolin Chen 62143d24e76SNicolin Chen return 0; 62243d24e76SNicolin Chen } 62343d24e76SNicolin Chen 62443d24e76SNicolin Chen static struct snd_soc_dai_driver fsl_esai_dai = { 62543d24e76SNicolin Chen .probe = fsl_esai_dai_probe, 62643d24e76SNicolin Chen .playback = { 62774ccb27cSNicolin Chen .stream_name = "CPU-Playback", 62843d24e76SNicolin Chen .channels_min = 1, 62943d24e76SNicolin Chen .channels_max = 12, 63043d24e76SNicolin Chen .rates = FSL_ESAI_RATES, 63143d24e76SNicolin Chen .formats = FSL_ESAI_FORMATS, 63243d24e76SNicolin Chen }, 63343d24e76SNicolin Chen .capture = { 63474ccb27cSNicolin Chen .stream_name = "CPU-Capture", 63543d24e76SNicolin Chen .channels_min = 1, 63643d24e76SNicolin Chen .channels_max = 8, 63743d24e76SNicolin Chen .rates = FSL_ESAI_RATES, 63843d24e76SNicolin Chen .formats = FSL_ESAI_FORMATS, 63943d24e76SNicolin Chen }, 64043d24e76SNicolin Chen .ops = &fsl_esai_dai_ops, 64143d24e76SNicolin Chen }; 64243d24e76SNicolin Chen 64343d24e76SNicolin Chen static const struct snd_soc_component_driver fsl_esai_component = { 64443d24e76SNicolin Chen .name = "fsl-esai", 64543d24e76SNicolin Chen }; 64643d24e76SNicolin Chen 64743d24e76SNicolin Chen static bool fsl_esai_readable_reg(struct device *dev, unsigned int reg) 64843d24e76SNicolin Chen { 64943d24e76SNicolin Chen switch (reg) { 65043d24e76SNicolin Chen case REG_ESAI_ERDR: 65143d24e76SNicolin Chen case REG_ESAI_ECR: 65243d24e76SNicolin Chen case REG_ESAI_ESR: 65343d24e76SNicolin Chen case REG_ESAI_TFCR: 65443d24e76SNicolin Chen case REG_ESAI_TFSR: 65543d24e76SNicolin Chen case REG_ESAI_RFCR: 65643d24e76SNicolin Chen case REG_ESAI_RFSR: 65743d24e76SNicolin Chen case REG_ESAI_RX0: 65843d24e76SNicolin Chen case REG_ESAI_RX1: 65943d24e76SNicolin Chen case REG_ESAI_RX2: 66043d24e76SNicolin Chen case REG_ESAI_RX3: 66143d24e76SNicolin Chen case REG_ESAI_SAISR: 66243d24e76SNicolin Chen case REG_ESAI_SAICR: 66343d24e76SNicolin Chen case REG_ESAI_TCR: 66443d24e76SNicolin Chen case REG_ESAI_TCCR: 66543d24e76SNicolin Chen case REG_ESAI_RCR: 66643d24e76SNicolin Chen case REG_ESAI_RCCR: 66743d24e76SNicolin Chen case REG_ESAI_TSMA: 66843d24e76SNicolin Chen case REG_ESAI_TSMB: 66943d24e76SNicolin Chen case REG_ESAI_RSMA: 67043d24e76SNicolin Chen case REG_ESAI_RSMB: 67143d24e76SNicolin Chen case REG_ESAI_PRRC: 67243d24e76SNicolin Chen case REG_ESAI_PCRC: 67343d24e76SNicolin Chen return true; 67443d24e76SNicolin Chen default: 67543d24e76SNicolin Chen return false; 67643d24e76SNicolin Chen } 67743d24e76SNicolin Chen } 67843d24e76SNicolin Chen 67943d24e76SNicolin Chen static bool fsl_esai_writeable_reg(struct device *dev, unsigned int reg) 68043d24e76SNicolin Chen { 68143d24e76SNicolin Chen switch (reg) { 68243d24e76SNicolin Chen case REG_ESAI_ETDR: 68343d24e76SNicolin Chen case REG_ESAI_ECR: 68443d24e76SNicolin Chen case REG_ESAI_TFCR: 68543d24e76SNicolin Chen case REG_ESAI_RFCR: 68643d24e76SNicolin Chen case REG_ESAI_TX0: 68743d24e76SNicolin Chen case REG_ESAI_TX1: 68843d24e76SNicolin Chen case REG_ESAI_TX2: 68943d24e76SNicolin Chen case REG_ESAI_TX3: 69043d24e76SNicolin Chen case REG_ESAI_TX4: 69143d24e76SNicolin Chen case REG_ESAI_TX5: 69243d24e76SNicolin Chen case REG_ESAI_TSR: 69343d24e76SNicolin Chen case REG_ESAI_SAICR: 69443d24e76SNicolin Chen case REG_ESAI_TCR: 69543d24e76SNicolin Chen case REG_ESAI_TCCR: 69643d24e76SNicolin Chen case REG_ESAI_RCR: 69743d24e76SNicolin Chen case REG_ESAI_RCCR: 69843d24e76SNicolin Chen case REG_ESAI_TSMA: 69943d24e76SNicolin Chen case REG_ESAI_TSMB: 70043d24e76SNicolin Chen case REG_ESAI_RSMA: 70143d24e76SNicolin Chen case REG_ESAI_RSMB: 70243d24e76SNicolin Chen case REG_ESAI_PRRC: 70343d24e76SNicolin Chen case REG_ESAI_PCRC: 70443d24e76SNicolin Chen return true; 70543d24e76SNicolin Chen default: 70643d24e76SNicolin Chen return false; 70743d24e76SNicolin Chen } 70843d24e76SNicolin Chen } 70943d24e76SNicolin Chen 710*92bd0334SXiubo Li static const struct regmap_config fsl_esai_regmap_config = { 71143d24e76SNicolin Chen .reg_bits = 32, 71243d24e76SNicolin Chen .reg_stride = 4, 71343d24e76SNicolin Chen .val_bits = 32, 71443d24e76SNicolin Chen 71543d24e76SNicolin Chen .max_register = REG_ESAI_PCRC, 71643d24e76SNicolin Chen .readable_reg = fsl_esai_readable_reg, 71743d24e76SNicolin Chen .writeable_reg = fsl_esai_writeable_reg, 71843d24e76SNicolin Chen }; 71943d24e76SNicolin Chen 72043d24e76SNicolin Chen static int fsl_esai_probe(struct platform_device *pdev) 72143d24e76SNicolin Chen { 72243d24e76SNicolin Chen struct device_node *np = pdev->dev.of_node; 72343d24e76SNicolin Chen struct fsl_esai *esai_priv; 72443d24e76SNicolin Chen struct resource *res; 72543d24e76SNicolin Chen const uint32_t *iprop; 72643d24e76SNicolin Chen void __iomem *regs; 72743d24e76SNicolin Chen int irq, ret; 72843d24e76SNicolin Chen 72943d24e76SNicolin Chen esai_priv = devm_kzalloc(&pdev->dev, sizeof(*esai_priv), GFP_KERNEL); 73043d24e76SNicolin Chen if (!esai_priv) 73143d24e76SNicolin Chen return -ENOMEM; 73243d24e76SNicolin Chen 73343d24e76SNicolin Chen esai_priv->pdev = pdev; 73443d24e76SNicolin Chen strcpy(esai_priv->name, np->name); 73543d24e76SNicolin Chen 73643d24e76SNicolin Chen /* Get the addresses and IRQ */ 73743d24e76SNicolin Chen res = platform_get_resource(pdev, IORESOURCE_MEM, 0); 73843d24e76SNicolin Chen regs = devm_ioremap_resource(&pdev->dev, res); 73943d24e76SNicolin Chen if (IS_ERR(regs)) 74043d24e76SNicolin Chen return PTR_ERR(regs); 74143d24e76SNicolin Chen 74243d24e76SNicolin Chen esai_priv->regmap = devm_regmap_init_mmio_clk(&pdev->dev, 74343d24e76SNicolin Chen "core", regs, &fsl_esai_regmap_config); 74443d24e76SNicolin Chen if (IS_ERR(esai_priv->regmap)) { 74543d24e76SNicolin Chen dev_err(&pdev->dev, "failed to init regmap: %ld\n", 74643d24e76SNicolin Chen PTR_ERR(esai_priv->regmap)); 74743d24e76SNicolin Chen return PTR_ERR(esai_priv->regmap); 74843d24e76SNicolin Chen } 74943d24e76SNicolin Chen 75043d24e76SNicolin Chen esai_priv->coreclk = devm_clk_get(&pdev->dev, "core"); 75143d24e76SNicolin Chen if (IS_ERR(esai_priv->coreclk)) { 75243d24e76SNicolin Chen dev_err(&pdev->dev, "failed to get core clock: %ld\n", 75343d24e76SNicolin Chen PTR_ERR(esai_priv->coreclk)); 75443d24e76SNicolin Chen return PTR_ERR(esai_priv->coreclk); 75543d24e76SNicolin Chen } 75643d24e76SNicolin Chen 75743d24e76SNicolin Chen esai_priv->extalclk = devm_clk_get(&pdev->dev, "extal"); 75843d24e76SNicolin Chen if (IS_ERR(esai_priv->extalclk)) 75943d24e76SNicolin Chen dev_warn(&pdev->dev, "failed to get extal clock: %ld\n", 76043d24e76SNicolin Chen PTR_ERR(esai_priv->extalclk)); 76143d24e76SNicolin Chen 76243d24e76SNicolin Chen esai_priv->fsysclk = devm_clk_get(&pdev->dev, "fsys"); 76343d24e76SNicolin Chen if (IS_ERR(esai_priv->fsysclk)) 76443d24e76SNicolin Chen dev_warn(&pdev->dev, "failed to get fsys clock: %ld\n", 76543d24e76SNicolin Chen PTR_ERR(esai_priv->fsysclk)); 76643d24e76SNicolin Chen 76743d24e76SNicolin Chen irq = platform_get_irq(pdev, 0); 76843d24e76SNicolin Chen if (irq < 0) { 76943d24e76SNicolin Chen dev_err(&pdev->dev, "no irq for node %s\n", np->full_name); 77043d24e76SNicolin Chen return irq; 77143d24e76SNicolin Chen } 77243d24e76SNicolin Chen 77343d24e76SNicolin Chen ret = devm_request_irq(&pdev->dev, irq, esai_isr, 0, 77443d24e76SNicolin Chen esai_priv->name, esai_priv); 77543d24e76SNicolin Chen if (ret) { 77643d24e76SNicolin Chen dev_err(&pdev->dev, "failed to claim irq %u\n", irq); 77743d24e76SNicolin Chen return ret; 77843d24e76SNicolin Chen } 77943d24e76SNicolin Chen 78043d24e76SNicolin Chen /* Set a default slot size */ 78143d24e76SNicolin Chen esai_priv->slot_width = 32; 78243d24e76SNicolin Chen 78343d24e76SNicolin Chen /* Set a default master/slave state */ 78443d24e76SNicolin Chen esai_priv->slave_mode = true; 78543d24e76SNicolin Chen 78643d24e76SNicolin Chen /* Determine the FIFO depth */ 78743d24e76SNicolin Chen iprop = of_get_property(np, "fsl,fifo-depth", NULL); 78843d24e76SNicolin Chen if (iprop) 78943d24e76SNicolin Chen esai_priv->fifo_depth = be32_to_cpup(iprop); 79043d24e76SNicolin Chen else 79143d24e76SNicolin Chen esai_priv->fifo_depth = 64; 79243d24e76SNicolin Chen 79343d24e76SNicolin Chen esai_priv->dma_params_tx.maxburst = 16; 79443d24e76SNicolin Chen esai_priv->dma_params_rx.maxburst = 16; 79543d24e76SNicolin Chen esai_priv->dma_params_tx.addr = res->start + REG_ESAI_ETDR; 79643d24e76SNicolin Chen esai_priv->dma_params_rx.addr = res->start + REG_ESAI_ERDR; 79743d24e76SNicolin Chen 79843d24e76SNicolin Chen esai_priv->synchronous = 79943d24e76SNicolin Chen of_property_read_bool(np, "fsl,esai-synchronous"); 80043d24e76SNicolin Chen 80143d24e76SNicolin Chen /* Implement full symmetry for synchronous mode */ 80243d24e76SNicolin Chen if (esai_priv->synchronous) { 80343d24e76SNicolin Chen fsl_esai_dai.symmetric_rates = 1; 80443d24e76SNicolin Chen fsl_esai_dai.symmetric_channels = 1; 80543d24e76SNicolin Chen fsl_esai_dai.symmetric_samplebits = 1; 80643d24e76SNicolin Chen } 80743d24e76SNicolin Chen 80843d24e76SNicolin Chen dev_set_drvdata(&pdev->dev, esai_priv); 80943d24e76SNicolin Chen 81043d24e76SNicolin Chen /* Reset ESAI unit */ 81143d24e76SNicolin Chen ret = regmap_write(esai_priv->regmap, REG_ESAI_ECR, ESAI_ECR_ERST); 81243d24e76SNicolin Chen if (ret) { 81343d24e76SNicolin Chen dev_err(&pdev->dev, "failed to reset ESAI: %d\n", ret); 81443d24e76SNicolin Chen return ret; 81543d24e76SNicolin Chen } 81643d24e76SNicolin Chen 81743d24e76SNicolin Chen /* 81843d24e76SNicolin Chen * We need to enable ESAI so as to access some of its registers. 81943d24e76SNicolin Chen * Otherwise, we would fail to dump regmap from user space. 82043d24e76SNicolin Chen */ 82143d24e76SNicolin Chen ret = regmap_write(esai_priv->regmap, REG_ESAI_ECR, ESAI_ECR_ESAIEN); 82243d24e76SNicolin Chen if (ret) { 82343d24e76SNicolin Chen dev_err(&pdev->dev, "failed to enable ESAI: %d\n", ret); 82443d24e76SNicolin Chen return ret; 82543d24e76SNicolin Chen } 82643d24e76SNicolin Chen 82743d24e76SNicolin Chen ret = devm_snd_soc_register_component(&pdev->dev, &fsl_esai_component, 82843d24e76SNicolin Chen &fsl_esai_dai, 1); 82943d24e76SNicolin Chen if (ret) { 83043d24e76SNicolin Chen dev_err(&pdev->dev, "failed to register DAI: %d\n", ret); 83143d24e76SNicolin Chen return ret; 83243d24e76SNicolin Chen } 83343d24e76SNicolin Chen 83443d24e76SNicolin Chen ret = imx_pcm_dma_init(pdev); 83543d24e76SNicolin Chen if (ret) 83643d24e76SNicolin Chen dev_err(&pdev->dev, "failed to init imx pcm dma: %d\n", ret); 83743d24e76SNicolin Chen 83843d24e76SNicolin Chen return ret; 83943d24e76SNicolin Chen } 84043d24e76SNicolin Chen 84143d24e76SNicolin Chen static const struct of_device_id fsl_esai_dt_ids[] = { 84243d24e76SNicolin Chen { .compatible = "fsl,imx35-esai", }, 843b21cc2f5SXiubo Li { .compatible = "fsl,vf610-esai", }, 84443d24e76SNicolin Chen {} 84543d24e76SNicolin Chen }; 84643d24e76SNicolin Chen MODULE_DEVICE_TABLE(of, fsl_esai_dt_ids); 84743d24e76SNicolin Chen 84843d24e76SNicolin Chen static struct platform_driver fsl_esai_driver = { 84943d24e76SNicolin Chen .probe = fsl_esai_probe, 85043d24e76SNicolin Chen .driver = { 85143d24e76SNicolin Chen .name = "fsl-esai-dai", 85243d24e76SNicolin Chen .owner = THIS_MODULE, 85343d24e76SNicolin Chen .of_match_table = fsl_esai_dt_ids, 85443d24e76SNicolin Chen }, 85543d24e76SNicolin Chen }; 85643d24e76SNicolin Chen 85743d24e76SNicolin Chen module_platform_driver(fsl_esai_driver); 85843d24e76SNicolin Chen 85943d24e76SNicolin Chen MODULE_AUTHOR("Freescale Semiconductor, Inc."); 86043d24e76SNicolin Chen MODULE_DESCRIPTION("Freescale ESAI CPU DAI driver"); 86143d24e76SNicolin Chen MODULE_LICENSE("GPL v2"); 86243d24e76SNicolin Chen MODULE_ALIAS("platform:fsl-esai-dai"); 863