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