Fabio Estevam | 3b5af9f | 2018-05-01 09:20:42 -0300 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
| 2 | // |
| 3 | // Freescale ESAI ALSA SoC Digital Audio Interface (DAI) driver |
| 4 | // |
| 5 | // Copyright (C) 2014 Freescale Semiconductor, Inc. |
Nicolin Chen | 43d24e7 | 2014-01-10 17:54:06 +0800 | [diff] [blame] | 6 | |
| 7 | #include <linux/clk.h> |
| 8 | #include <linux/dmaengine.h> |
| 9 | #include <linux/module.h> |
| 10 | #include <linux/of_irq.h> |
| 11 | #include <linux/of_platform.h> |
S.j. Wang | b2d337d | 2019-05-03 12:49:44 -0700 | [diff] [blame] | 12 | #include <linux/pm_runtime.h> |
Nicolin Chen | 43d24e7 | 2014-01-10 17:54:06 +0800 | [diff] [blame] | 13 | #include <sound/dmaengine_pcm.h> |
| 14 | #include <sound/pcm_params.h> |
| 15 | |
| 16 | #include "fsl_esai.h" |
| 17 | #include "imx-pcm.h" |
| 18 | |
Nicolin Chen | 43d24e7 | 2014-01-10 17:54:06 +0800 | [diff] [blame] | 19 | #define FSL_ESAI_FORMATS (SNDRV_PCM_FMTBIT_S8 | \ |
| 20 | SNDRV_PCM_FMTBIT_S16_LE | \ |
| 21 | SNDRV_PCM_FMTBIT_S20_3LE | \ |
| 22 | SNDRV_PCM_FMTBIT_S24_LE) |
| 23 | |
| 24 | /** |
Pierre-Louis Bossart | 3bae171 | 2020-07-02 14:21:41 -0500 | [diff] [blame] | 25 | * struct fsl_esai_soc_data - soc specific data |
Shengjiu Wang | 6878e75 | 2020-05-15 18:10:50 +0800 | [diff] [blame] | 26 | * @reset_at_xrun: flags for enable reset operaton |
| 27 | */ |
| 28 | struct fsl_esai_soc_data { |
Shengjiu Wang | 6878e75 | 2020-05-15 18:10:50 +0800 | [diff] [blame] | 29 | bool reset_at_xrun; |
| 30 | }; |
| 31 | |
| 32 | /** |
Pierre-Louis Bossart | 3bae171 | 2020-07-02 14:21:41 -0500 | [diff] [blame] | 33 | * struct fsl_esai - ESAI private data |
Nicolin Chen | 43d24e7 | 2014-01-10 17:54:06 +0800 | [diff] [blame] | 34 | * @dma_params_rx: DMA parameters for receive channel |
| 35 | * @dma_params_tx: DMA parameters for transmit channel |
| 36 | * @pdev: platform device pointer |
| 37 | * @regmap: regmap handler |
| 38 | * @coreclk: clock source to access register |
| 39 | * @extalclk: esai clock source to derive HCK, SCK and FS |
| 40 | * @fsysclk: system clock source to derive HCK, SCK and FS |
Shengjiu Wang | a2a4d60 | 2015-11-24 17:19:32 +0800 | [diff] [blame] | 41 | * @spbaclk: SPBA clock (optional, depending on SoC design) |
Takashi Iwai | a3d1f93 | 2020-09-03 12:47:47 +0200 | [diff] [blame] | 42 | * @work: work to handle the reset operation |
Shengjiu Wang | 6878e75 | 2020-05-15 18:10:50 +0800 | [diff] [blame] | 43 | * @soc: soc specific data |
Shengjiu Wang | 35dac62 | 2019-10-28 17:11:05 +0800 | [diff] [blame] | 44 | * @lock: spin lock between hw_reset() and trigger() |
Nicolin Chen | 43d24e7 | 2014-01-10 17:54:06 +0800 | [diff] [blame] | 45 | * @fifo_depth: depth of tx/rx FIFO |
| 46 | * @slot_width: width of each DAI slot |
Shengjiu Wang | de0d712 | 2014-08-08 14:47:21 +0800 | [diff] [blame] | 47 | * @slots: number of slots |
Pierre-Louis Bossart | 3bae171 | 2020-07-02 14:21:41 -0500 | [diff] [blame] | 48 | * @tx_mask: slot mask for TX |
| 49 | * @rx_mask: slot mask for RX |
Shengjiu Wang | 5be6155 | 2019-07-11 18:49:45 +0800 | [diff] [blame] | 50 | * @channels: channel num for tx or rx |
Nicolin Chen | 43d24e7 | 2014-01-10 17:54:06 +0800 | [diff] [blame] | 51 | * @hck_rate: clock rate of desired HCKx clock |
Nicolin Chen | f975ca4 | 2014-05-06 16:56:01 +0800 | [diff] [blame] | 52 | * @sck_rate: clock rate of desired SCKx clock |
| 53 | * @hck_dir: the direction of HCKx pads |
Nicolin Chen | 43d24e7 | 2014-01-10 17:54:06 +0800 | [diff] [blame] | 54 | * @sck_div: if using PSR/PM dividers for SCKx clock |
| 55 | * @slave_mode: if fully using DAI slave mode |
| 56 | * @synchronous: if using tx/rx synchronous mode |
| 57 | * @name: driver name |
| 58 | */ |
| 59 | struct fsl_esai { |
| 60 | struct snd_dmaengine_dai_dma_data dma_params_rx; |
| 61 | struct snd_dmaengine_dai_dma_data dma_params_tx; |
| 62 | struct platform_device *pdev; |
| 63 | struct regmap *regmap; |
| 64 | struct clk *coreclk; |
| 65 | struct clk *extalclk; |
| 66 | struct clk *fsysclk; |
Shengjiu Wang | a2a4d60 | 2015-11-24 17:19:32 +0800 | [diff] [blame] | 67 | struct clk *spbaclk; |
Takashi Iwai | a3d1f93 | 2020-09-03 12:47:47 +0200 | [diff] [blame] | 68 | struct work_struct work; |
Shengjiu Wang | 6878e75 | 2020-05-15 18:10:50 +0800 | [diff] [blame] | 69 | const struct fsl_esai_soc_data *soc; |
Shengjiu Wang | 35dac62 | 2019-10-28 17:11:05 +0800 | [diff] [blame] | 70 | spinlock_t lock; /* Protect hw_reset and trigger */ |
Nicolin Chen | 43d24e7 | 2014-01-10 17:54:06 +0800 | [diff] [blame] | 71 | u32 fifo_depth; |
| 72 | u32 slot_width; |
Shengjiu Wang | de0d712 | 2014-08-08 14:47:21 +0800 | [diff] [blame] | 73 | u32 slots; |
S.j. Wang | 0ff4e8c | 2019-02-27 06:31:12 +0000 | [diff] [blame] | 74 | u32 tx_mask; |
| 75 | u32 rx_mask; |
Shengjiu Wang | 5be6155 | 2019-07-11 18:49:45 +0800 | [diff] [blame] | 76 | u32 channels[2]; |
Nicolin Chen | 43d24e7 | 2014-01-10 17:54:06 +0800 | [diff] [blame] | 77 | u32 hck_rate[2]; |
Nicolin Chen | f975ca4 | 2014-05-06 16:56:01 +0800 | [diff] [blame] | 78 | u32 sck_rate[2]; |
| 79 | bool hck_dir[2]; |
Nicolin Chen | 43d24e7 | 2014-01-10 17:54:06 +0800 | [diff] [blame] | 80 | bool sck_div[2]; |
| 81 | bool slave_mode; |
| 82 | bool synchronous; |
| 83 | char name[32]; |
| 84 | }; |
| 85 | |
Shengjiu Wang | 6878e75 | 2020-05-15 18:10:50 +0800 | [diff] [blame] | 86 | static struct fsl_esai_soc_data fsl_esai_vf610 = { |
Shengjiu Wang | 6878e75 | 2020-05-15 18:10:50 +0800 | [diff] [blame] | 87 | .reset_at_xrun = true, |
| 88 | }; |
| 89 | |
| 90 | static struct fsl_esai_soc_data fsl_esai_imx35 = { |
Shengjiu Wang | 6878e75 | 2020-05-15 18:10:50 +0800 | [diff] [blame] | 91 | .reset_at_xrun = true, |
| 92 | }; |
| 93 | |
| 94 | static struct fsl_esai_soc_data fsl_esai_imx6ull = { |
Shengjiu Wang | 6878e75 | 2020-05-15 18:10:50 +0800 | [diff] [blame] | 95 | .reset_at_xrun = false, |
| 96 | }; |
| 97 | |
Nicolin Chen | 43d24e7 | 2014-01-10 17:54:06 +0800 | [diff] [blame] | 98 | static irqreturn_t esai_isr(int irq, void *devid) |
| 99 | { |
| 100 | struct fsl_esai *esai_priv = (struct fsl_esai *)devid; |
| 101 | struct platform_device *pdev = esai_priv->pdev; |
| 102 | u32 esr; |
Shengjiu Wang | 7ccafa2 | 2019-07-11 18:49:46 +0800 | [diff] [blame] | 103 | u32 saisr; |
Nicolin Chen | 43d24e7 | 2014-01-10 17:54:06 +0800 | [diff] [blame] | 104 | |
| 105 | regmap_read(esai_priv->regmap, REG_ESAI_ESR, &esr); |
Shengjiu Wang | 7ccafa2 | 2019-07-11 18:49:46 +0800 | [diff] [blame] | 106 | regmap_read(esai_priv->regmap, REG_ESAI_SAISR, &saisr); |
| 107 | |
| 108 | if ((saisr & (ESAI_SAISR_TUE | ESAI_SAISR_ROE)) && |
Shengjiu Wang | 6878e75 | 2020-05-15 18:10:50 +0800 | [diff] [blame] | 109 | esai_priv->soc->reset_at_xrun) { |
Shengjiu Wang | 7ccafa2 | 2019-07-11 18:49:46 +0800 | [diff] [blame] | 110 | dev_dbg(&pdev->dev, "reset module for xrun\n"); |
Shengjiu Wang | 1fecbb7 | 2020-04-27 14:23:21 +0800 | [diff] [blame] | 111 | regmap_update_bits(esai_priv->regmap, REG_ESAI_TCR, |
| 112 | ESAI_xCR_xEIE_MASK, 0); |
| 113 | regmap_update_bits(esai_priv->regmap, REG_ESAI_RCR, |
| 114 | ESAI_xCR_xEIE_MASK, 0); |
Takashi Iwai | a3d1f93 | 2020-09-03 12:47:47 +0200 | [diff] [blame] | 115 | schedule_work(&esai_priv->work); |
Shengjiu Wang | 7ccafa2 | 2019-07-11 18:49:46 +0800 | [diff] [blame] | 116 | } |
Nicolin Chen | 43d24e7 | 2014-01-10 17:54:06 +0800 | [diff] [blame] | 117 | |
| 118 | if (esr & ESAI_ESR_TINIT_MASK) |
Colin Ian King | 3bcc865 | 2016-09-02 15:07:23 +0100 | [diff] [blame] | 119 | dev_dbg(&pdev->dev, "isr: Transmission Initialized\n"); |
Nicolin Chen | 43d24e7 | 2014-01-10 17:54:06 +0800 | [diff] [blame] | 120 | |
| 121 | if (esr & ESAI_ESR_RFF_MASK) |
| 122 | dev_warn(&pdev->dev, "isr: Receiving overrun\n"); |
| 123 | |
| 124 | if (esr & ESAI_ESR_TFE_MASK) |
Colin Ian King | 3bcc865 | 2016-09-02 15:07:23 +0100 | [diff] [blame] | 125 | dev_warn(&pdev->dev, "isr: Transmission underrun\n"); |
Nicolin Chen | 43d24e7 | 2014-01-10 17:54:06 +0800 | [diff] [blame] | 126 | |
| 127 | if (esr & ESAI_ESR_TLS_MASK) |
| 128 | dev_dbg(&pdev->dev, "isr: Just transmitted the last slot\n"); |
| 129 | |
| 130 | if (esr & ESAI_ESR_TDE_MASK) |
Colin Ian King | 3bcc865 | 2016-09-02 15:07:23 +0100 | [diff] [blame] | 131 | dev_dbg(&pdev->dev, "isr: Transmission data exception\n"); |
Nicolin Chen | 43d24e7 | 2014-01-10 17:54:06 +0800 | [diff] [blame] | 132 | |
| 133 | if (esr & ESAI_ESR_TED_MASK) |
| 134 | dev_dbg(&pdev->dev, "isr: Transmitting even slots\n"); |
| 135 | |
| 136 | if (esr & ESAI_ESR_TD_MASK) |
| 137 | dev_dbg(&pdev->dev, "isr: Transmitting data\n"); |
| 138 | |
| 139 | if (esr & ESAI_ESR_RLS_MASK) |
| 140 | dev_dbg(&pdev->dev, "isr: Just received the last slot\n"); |
| 141 | |
| 142 | if (esr & ESAI_ESR_RDE_MASK) |
| 143 | dev_dbg(&pdev->dev, "isr: Receiving data exception\n"); |
| 144 | |
| 145 | if (esr & ESAI_ESR_RED_MASK) |
| 146 | dev_dbg(&pdev->dev, "isr: Receiving even slots\n"); |
| 147 | |
| 148 | if (esr & ESAI_ESR_RD_MASK) |
| 149 | dev_dbg(&pdev->dev, "isr: Receiving data\n"); |
| 150 | |
| 151 | return IRQ_HANDLED; |
| 152 | } |
| 153 | |
| 154 | /** |
Pierre-Louis Bossart | 3bae171 | 2020-07-02 14:21:41 -0500 | [diff] [blame] | 155 | * fsl_esai_divisor_cal - This function is used to calculate the |
| 156 | * divisors of psr, pm, fp and it is supposed to be called in |
| 157 | * set_dai_sysclk() and set_bclk(). |
Nicolin Chen | 43d24e7 | 2014-01-10 17:54:06 +0800 | [diff] [blame] | 158 | * |
Pierre-Louis Bossart | 3bae171 | 2020-07-02 14:21:41 -0500 | [diff] [blame] | 159 | * @dai: pointer to DAI |
| 160 | * @tx: current setting is for playback or capture |
Nicolin Chen | 43d24e7 | 2014-01-10 17:54:06 +0800 | [diff] [blame] | 161 | * @ratio: desired overall ratio for the paticipating dividers |
| 162 | * @usefp: for HCK setting, there is no need to set fp divider |
| 163 | * @fp: bypass other dividers by setting fp directly if fp != 0 |
Nicolin Chen | 43d24e7 | 2014-01-10 17:54:06 +0800 | [diff] [blame] | 164 | */ |
| 165 | static int fsl_esai_divisor_cal(struct snd_soc_dai *dai, bool tx, u32 ratio, |
| 166 | bool usefp, u32 fp) |
| 167 | { |
| 168 | struct fsl_esai *esai_priv = snd_soc_dai_get_drvdata(dai); |
| 169 | u32 psr, pm = 999, maxfp, prod, sub, savesub, i, j; |
| 170 | |
| 171 | maxfp = usefp ? 16 : 1; |
| 172 | |
| 173 | if (usefp && fp) |
| 174 | goto out_fp; |
| 175 | |
| 176 | if (ratio > 2 * 8 * 256 * maxfp || ratio < 2) { |
| 177 | dev_err(dai->dev, "the ratio is out of range (2 ~ %d)\n", |
| 178 | 2 * 8 * 256 * maxfp); |
| 179 | return -EINVAL; |
| 180 | } else if (ratio % 2) { |
| 181 | dev_err(dai->dev, "the raio must be even if using upper divider\n"); |
| 182 | return -EINVAL; |
| 183 | } |
| 184 | |
| 185 | ratio /= 2; |
| 186 | |
| 187 | psr = ratio <= 256 * maxfp ? ESAI_xCCR_xPSR_BYPASS : ESAI_xCCR_xPSR_DIV8; |
| 188 | |
Nicolin Chen | c656941 | 2018-04-08 16:57:35 -0700 | [diff] [blame] | 189 | /* Do not loop-search if PM (1 ~ 256) alone can serve the ratio */ |
| 190 | if (ratio <= 256) { |
| 191 | pm = ratio; |
| 192 | fp = 1; |
| 193 | goto out; |
| 194 | } |
| 195 | |
Nicolin Chen | 43d24e7 | 2014-01-10 17:54:06 +0800 | [diff] [blame] | 196 | /* Set the max fluctuation -- 0.1% of the max devisor */ |
| 197 | savesub = (psr ? 1 : 8) * 256 * maxfp / 1000; |
| 198 | |
| 199 | /* Find the best value for PM */ |
| 200 | for (i = 1; i <= 256; i++) { |
| 201 | for (j = 1; j <= maxfp; j++) { |
| 202 | /* PSR (1 or 8) * PM (1 ~ 256) * FP (1 ~ 16) */ |
| 203 | prod = (psr ? 1 : 8) * i * j; |
| 204 | |
| 205 | if (prod == ratio) |
| 206 | sub = 0; |
| 207 | else if (prod / ratio == 1) |
| 208 | sub = prod - ratio; |
| 209 | else if (ratio / prod == 1) |
| 210 | sub = ratio - prod; |
| 211 | else |
| 212 | continue; |
| 213 | |
| 214 | /* Calculate the fraction */ |
| 215 | sub = sub * 1000 / ratio; |
| 216 | if (sub < savesub) { |
| 217 | savesub = sub; |
| 218 | pm = i; |
| 219 | fp = j; |
| 220 | } |
| 221 | |
| 222 | /* We are lucky */ |
| 223 | if (savesub == 0) |
| 224 | goto out; |
| 225 | } |
| 226 | } |
| 227 | |
| 228 | if (pm == 999) { |
| 229 | dev_err(dai->dev, "failed to calculate proper divisors\n"); |
| 230 | return -EINVAL; |
| 231 | } |
| 232 | |
| 233 | out: |
| 234 | regmap_update_bits(esai_priv->regmap, REG_ESAI_xCCR(tx), |
| 235 | ESAI_xCCR_xPSR_MASK | ESAI_xCCR_xPM_MASK, |
| 236 | psr | ESAI_xCCR_xPM(pm)); |
| 237 | |
| 238 | out_fp: |
| 239 | /* Bypass fp if not being required */ |
| 240 | if (maxfp <= 1) |
| 241 | return 0; |
| 242 | |
| 243 | regmap_update_bits(esai_priv->regmap, REG_ESAI_xCCR(tx), |
| 244 | ESAI_xCCR_xFP_MASK, ESAI_xCCR_xFP(fp)); |
| 245 | |
| 246 | return 0; |
| 247 | } |
| 248 | |
| 249 | /** |
Pierre-Louis Bossart | 3bae171 | 2020-07-02 14:21:41 -0500 | [diff] [blame] | 250 | * fsl_esai_set_dai_sysclk - configure the clock frequency of MCLK (HCKT/HCKR) |
| 251 | * @dai: pointer to DAI |
| 252 | * @clk_id: The clock source of HCKT/HCKR |
Nicolin Chen | 43d24e7 | 2014-01-10 17:54:06 +0800 | [diff] [blame] | 253 | * (Input from outside; output from inside, FSYS or EXTAL) |
Pierre-Louis Bossart | 3bae171 | 2020-07-02 14:21:41 -0500 | [diff] [blame] | 254 | * @freq: The required clock rate of HCKT/HCKR |
| 255 | * @dir: The clock direction of HCKT/HCKR |
Nicolin Chen | 43d24e7 | 2014-01-10 17:54:06 +0800 | [diff] [blame] | 256 | * |
| 257 | * Note: If the direction is input, we do not care about clk_id. |
| 258 | */ |
| 259 | static int fsl_esai_set_dai_sysclk(struct snd_soc_dai *dai, int clk_id, |
| 260 | unsigned int freq, int dir) |
| 261 | { |
| 262 | struct fsl_esai *esai_priv = snd_soc_dai_get_drvdata(dai); |
| 263 | struct clk *clksrc = esai_priv->extalclk; |
S.j. Wang | 1997ee8 | 2019-04-04 09:40:56 +0000 | [diff] [blame] | 264 | bool tx = (clk_id <= ESAI_HCKT_EXTAL || esai_priv->synchronous); |
Nicolin Chen | 43d24e7 | 2014-01-10 17:54:06 +0800 | [diff] [blame] | 265 | bool in = dir == SND_SOC_CLOCK_IN; |
Xiubo Li | 3e18523 | 2014-04-04 15:10:26 +0800 | [diff] [blame] | 266 | u32 ratio, ecr = 0; |
Nicolin Chen | 43d24e7 | 2014-01-10 17:54:06 +0800 | [diff] [blame] | 267 | unsigned long clk_rate; |
Xiubo Li | 3e18523 | 2014-04-04 15:10:26 +0800 | [diff] [blame] | 268 | int ret; |
Nicolin Chen | 43d24e7 | 2014-01-10 17:54:06 +0800 | [diff] [blame] | 269 | |
Nicolin Chen | 8a2278b | 2018-04-08 17:33:54 -0700 | [diff] [blame] | 270 | if (freq == 0) { |
| 271 | dev_err(dai->dev, "%sput freq of HCK%c should not be 0Hz\n", |
| 272 | in ? "in" : "out", tx ? 'T' : 'R'); |
| 273 | return -EINVAL; |
| 274 | } |
| 275 | |
Nicolin Chen | f975ca4 | 2014-05-06 16:56:01 +0800 | [diff] [blame] | 276 | /* Bypass divider settings if the requirement doesn't change */ |
| 277 | if (freq == esai_priv->hck_rate[tx] && dir == esai_priv->hck_dir[tx]) |
| 278 | return 0; |
Nicolin Chen | 43d24e7 | 2014-01-10 17:54:06 +0800 | [diff] [blame] | 279 | |
| 280 | /* sck_div can be only bypassed if ETO/ERO=0 and SNC_SOC_CLOCK_OUT */ |
| 281 | esai_priv->sck_div[tx] = true; |
| 282 | |
| 283 | /* Set the direction of HCKT/HCKR pins */ |
| 284 | regmap_update_bits(esai_priv->regmap, REG_ESAI_xCCR(tx), |
| 285 | ESAI_xCCR_xHCKD, in ? 0 : ESAI_xCCR_xHCKD); |
| 286 | |
| 287 | if (in) |
| 288 | goto out; |
| 289 | |
| 290 | switch (clk_id) { |
| 291 | case ESAI_HCKT_FSYS: |
| 292 | case ESAI_HCKR_FSYS: |
| 293 | clksrc = esai_priv->fsysclk; |
| 294 | break; |
| 295 | case ESAI_HCKT_EXTAL: |
| 296 | ecr |= ESAI_ECR_ETI; |
S.j. Wang | 903c220 | 2019-04-28 02:24:27 +0000 | [diff] [blame] | 297 | break; |
Nicolin Chen | 43d24e7 | 2014-01-10 17:54:06 +0800 | [diff] [blame] | 298 | case ESAI_HCKR_EXTAL: |
S.j. Wang | 1997ee8 | 2019-04-04 09:40:56 +0000 | [diff] [blame] | 299 | ecr |= esai_priv->synchronous ? ESAI_ECR_ETI : ESAI_ECR_ERI; |
Nicolin Chen | 43d24e7 | 2014-01-10 17:54:06 +0800 | [diff] [blame] | 300 | break; |
| 301 | default: |
| 302 | return -EINVAL; |
| 303 | } |
| 304 | |
| 305 | if (IS_ERR(clksrc)) { |
| 306 | dev_err(dai->dev, "no assigned %s clock\n", |
| 307 | clk_id % 2 ? "extal" : "fsys"); |
| 308 | return PTR_ERR(clksrc); |
| 309 | } |
| 310 | clk_rate = clk_get_rate(clksrc); |
| 311 | |
| 312 | ratio = clk_rate / freq; |
| 313 | if (ratio * freq > clk_rate) |
| 314 | ret = ratio * freq - clk_rate; |
| 315 | else if (ratio * freq < clk_rate) |
| 316 | ret = clk_rate - ratio * freq; |
| 317 | else |
| 318 | ret = 0; |
| 319 | |
| 320 | /* Block if clock source can not be divided into the required rate */ |
| 321 | if (ret != 0 && clk_rate / ret < 1000) { |
| 322 | dev_err(dai->dev, "failed to derive required HCK%c rate\n", |
| 323 | tx ? 'T' : 'R'); |
| 324 | return -EINVAL; |
| 325 | } |
| 326 | |
Nicolin Chen | 57ebbca | 2014-05-06 16:56:00 +0800 | [diff] [blame] | 327 | /* Only EXTAL source can be output directly without using PSR and PM */ |
| 328 | if (ratio == 1 && clksrc == esai_priv->extalclk) { |
Nicolin Chen | 43d24e7 | 2014-01-10 17:54:06 +0800 | [diff] [blame] | 329 | /* Bypass all the dividers if not being needed */ |
| 330 | ecr |= tx ? ESAI_ECR_ETO : ESAI_ECR_ERO; |
| 331 | goto out; |
Nicolin Chen | 57ebbca | 2014-05-06 16:56:00 +0800 | [diff] [blame] | 332 | } else if (ratio < 2) { |
| 333 | /* The ratio should be no less than 2 if using other sources */ |
| 334 | dev_err(dai->dev, "failed to derive required HCK%c rate\n", |
| 335 | tx ? 'T' : 'R'); |
| 336 | return -EINVAL; |
Nicolin Chen | 43d24e7 | 2014-01-10 17:54:06 +0800 | [diff] [blame] | 337 | } |
| 338 | |
| 339 | ret = fsl_esai_divisor_cal(dai, tx, ratio, false, 0); |
| 340 | if (ret) |
| 341 | return ret; |
| 342 | |
| 343 | esai_priv->sck_div[tx] = false; |
| 344 | |
| 345 | out: |
Nicolin Chen | f975ca4 | 2014-05-06 16:56:01 +0800 | [diff] [blame] | 346 | esai_priv->hck_dir[tx] = dir; |
Nicolin Chen | 43d24e7 | 2014-01-10 17:54:06 +0800 | [diff] [blame] | 347 | esai_priv->hck_rate[tx] = freq; |
| 348 | |
| 349 | regmap_update_bits(esai_priv->regmap, REG_ESAI_ECR, |
| 350 | tx ? ESAI_ECR_ETI | ESAI_ECR_ETO : |
| 351 | ESAI_ECR_ERI | ESAI_ECR_ERO, ecr); |
| 352 | |
| 353 | return 0; |
| 354 | } |
| 355 | |
| 356 | /** |
Pierre-Louis Bossart | 3bae171 | 2020-07-02 14:21:41 -0500 | [diff] [blame] | 357 | * fsl_esai_set_bclk - configure the related dividers according to the bclk rate |
| 358 | * @dai: pointer to DAI |
| 359 | * @tx: direction boolean |
| 360 | * @freq: bclk freq |
Nicolin Chen | 43d24e7 | 2014-01-10 17:54:06 +0800 | [diff] [blame] | 361 | */ |
| 362 | static int fsl_esai_set_bclk(struct snd_soc_dai *dai, bool tx, u32 freq) |
| 363 | { |
| 364 | struct fsl_esai *esai_priv = snd_soc_dai_get_drvdata(dai); |
| 365 | u32 hck_rate = esai_priv->hck_rate[tx]; |
| 366 | u32 sub, ratio = hck_rate / freq; |
Nicolin Chen | f975ca4 | 2014-05-06 16:56:01 +0800 | [diff] [blame] | 367 | int ret; |
Nicolin Chen | 43d24e7 | 2014-01-10 17:54:06 +0800 | [diff] [blame] | 368 | |
Nicolin Chen | f975ca4 | 2014-05-06 16:56:01 +0800 | [diff] [blame] | 369 | /* Don't apply for fully slave mode or unchanged bclk */ |
| 370 | if (esai_priv->slave_mode || esai_priv->sck_rate[tx] == freq) |
Nicolin Chen | 43d24e7 | 2014-01-10 17:54:06 +0800 | [diff] [blame] | 371 | return 0; |
| 372 | |
| 373 | if (ratio * freq > hck_rate) |
| 374 | sub = ratio * freq - hck_rate; |
| 375 | else if (ratio * freq < hck_rate) |
| 376 | sub = hck_rate - ratio * freq; |
| 377 | else |
| 378 | sub = 0; |
| 379 | |
| 380 | /* Block if clock source can not be divided into the required rate */ |
| 381 | if (sub != 0 && hck_rate / sub < 1000) { |
| 382 | dev_err(dai->dev, "failed to derive required SCK%c rate\n", |
| 383 | tx ? 'T' : 'R'); |
| 384 | return -EINVAL; |
| 385 | } |
| 386 | |
Nicolin Chen | 89e47f6 | 2014-05-06 16:55:59 +0800 | [diff] [blame] | 387 | /* The ratio should be contented by FP alone if bypassing PM and PSR */ |
| 388 | if (!esai_priv->sck_div[tx] && (ratio > 16 || ratio == 0)) { |
Nicolin Chen | 43d24e7 | 2014-01-10 17:54:06 +0800 | [diff] [blame] | 389 | dev_err(dai->dev, "the ratio is out of range (1 ~ 16)\n"); |
| 390 | return -EINVAL; |
| 391 | } |
| 392 | |
Nicolin Chen | f975ca4 | 2014-05-06 16:56:01 +0800 | [diff] [blame] | 393 | ret = fsl_esai_divisor_cal(dai, tx, ratio, true, |
Nicolin Chen | 43d24e7 | 2014-01-10 17:54:06 +0800 | [diff] [blame] | 394 | esai_priv->sck_div[tx] ? 0 : ratio); |
Nicolin Chen | f975ca4 | 2014-05-06 16:56:01 +0800 | [diff] [blame] | 395 | if (ret) |
| 396 | return ret; |
| 397 | |
| 398 | /* Save current bclk rate */ |
| 399 | esai_priv->sck_rate[tx] = freq; |
| 400 | |
| 401 | return 0; |
Nicolin Chen | 43d24e7 | 2014-01-10 17:54:06 +0800 | [diff] [blame] | 402 | } |
| 403 | |
| 404 | static int fsl_esai_set_dai_tdm_slot(struct snd_soc_dai *dai, u32 tx_mask, |
| 405 | u32 rx_mask, int slots, int slot_width) |
| 406 | { |
| 407 | struct fsl_esai *esai_priv = snd_soc_dai_get_drvdata(dai); |
| 408 | |
| 409 | regmap_update_bits(esai_priv->regmap, REG_ESAI_TCCR, |
| 410 | ESAI_xCCR_xDC_MASK, ESAI_xCCR_xDC(slots)); |
| 411 | |
Nicolin Chen | 43d24e7 | 2014-01-10 17:54:06 +0800 | [diff] [blame] | 412 | regmap_update_bits(esai_priv->regmap, REG_ESAI_RCCR, |
| 413 | ESAI_xCCR_xDC_MASK, ESAI_xCCR_xDC(slots)); |
| 414 | |
Nicolin Chen | 43d24e7 | 2014-01-10 17:54:06 +0800 | [diff] [blame] | 415 | esai_priv->slot_width = slot_width; |
Shengjiu Wang | de0d712 | 2014-08-08 14:47:21 +0800 | [diff] [blame] | 416 | esai_priv->slots = slots; |
S.j. Wang | 0ff4e8c | 2019-02-27 06:31:12 +0000 | [diff] [blame] | 417 | esai_priv->tx_mask = tx_mask; |
| 418 | esai_priv->rx_mask = rx_mask; |
Nicolin Chen | 43d24e7 | 2014-01-10 17:54:06 +0800 | [diff] [blame] | 419 | |
| 420 | return 0; |
| 421 | } |
| 422 | |
| 423 | static int fsl_esai_set_dai_fmt(struct snd_soc_dai *dai, unsigned int fmt) |
| 424 | { |
| 425 | struct fsl_esai *esai_priv = snd_soc_dai_get_drvdata(dai); |
| 426 | u32 xcr = 0, xccr = 0, mask; |
| 427 | |
| 428 | /* DAI mode */ |
| 429 | switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) { |
| 430 | case SND_SOC_DAIFMT_I2S: |
| 431 | /* Data on rising edge of bclk, frame low, 1clk before data */ |
| 432 | xcr |= ESAI_xCR_xFSR; |
| 433 | xccr |= ESAI_xCCR_xFSP | ESAI_xCCR_xCKP | ESAI_xCCR_xHCKP; |
| 434 | break; |
| 435 | case SND_SOC_DAIFMT_LEFT_J: |
| 436 | /* Data on rising edge of bclk, frame high */ |
| 437 | xccr |= ESAI_xCCR_xCKP | ESAI_xCCR_xHCKP; |
| 438 | break; |
| 439 | case SND_SOC_DAIFMT_RIGHT_J: |
| 440 | /* Data on rising edge of bclk, frame high, right aligned */ |
S.j. Wang | cc29ea0 | 2019-02-18 08:29:11 +0000 | [diff] [blame] | 441 | xccr |= ESAI_xCCR_xCKP | ESAI_xCCR_xHCKP; |
| 442 | xcr |= ESAI_xCR_xWA; |
Nicolin Chen | 43d24e7 | 2014-01-10 17:54:06 +0800 | [diff] [blame] | 443 | break; |
| 444 | case SND_SOC_DAIFMT_DSP_A: |
| 445 | /* Data on rising edge of bclk, frame high, 1clk before data */ |
| 446 | xcr |= ESAI_xCR_xFSL | ESAI_xCR_xFSR; |
| 447 | xccr |= ESAI_xCCR_xCKP | ESAI_xCCR_xHCKP; |
| 448 | break; |
| 449 | case SND_SOC_DAIFMT_DSP_B: |
| 450 | /* Data on rising edge of bclk, frame high */ |
| 451 | xcr |= ESAI_xCR_xFSL; |
| 452 | xccr |= ESAI_xCCR_xCKP | ESAI_xCCR_xHCKP; |
| 453 | break; |
| 454 | default: |
| 455 | return -EINVAL; |
| 456 | } |
| 457 | |
| 458 | /* DAI clock inversion */ |
| 459 | switch (fmt & SND_SOC_DAIFMT_INV_MASK) { |
| 460 | case SND_SOC_DAIFMT_NB_NF: |
| 461 | /* Nothing to do for both normal cases */ |
| 462 | break; |
| 463 | case SND_SOC_DAIFMT_IB_NF: |
| 464 | /* Invert bit clock */ |
| 465 | xccr ^= ESAI_xCCR_xCKP | ESAI_xCCR_xHCKP; |
| 466 | break; |
| 467 | case SND_SOC_DAIFMT_NB_IF: |
| 468 | /* Invert frame clock */ |
| 469 | xccr ^= ESAI_xCCR_xFSP; |
| 470 | break; |
| 471 | case SND_SOC_DAIFMT_IB_IF: |
| 472 | /* Invert both clocks */ |
| 473 | xccr ^= ESAI_xCCR_xCKP | ESAI_xCCR_xHCKP | ESAI_xCCR_xFSP; |
| 474 | break; |
| 475 | default: |
| 476 | return -EINVAL; |
| 477 | } |
| 478 | |
| 479 | esai_priv->slave_mode = false; |
| 480 | |
| 481 | /* DAI clock master masks */ |
| 482 | switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) { |
| 483 | case SND_SOC_DAIFMT_CBM_CFM: |
| 484 | esai_priv->slave_mode = true; |
| 485 | break; |
| 486 | case SND_SOC_DAIFMT_CBS_CFM: |
| 487 | xccr |= ESAI_xCCR_xCKD; |
| 488 | break; |
| 489 | case SND_SOC_DAIFMT_CBM_CFS: |
| 490 | xccr |= ESAI_xCCR_xFSD; |
| 491 | break; |
| 492 | case SND_SOC_DAIFMT_CBS_CFS: |
| 493 | xccr |= ESAI_xCCR_xFSD | ESAI_xCCR_xCKD; |
| 494 | break; |
| 495 | default: |
| 496 | return -EINVAL; |
| 497 | } |
| 498 | |
S.j. Wang | cc29ea0 | 2019-02-18 08:29:11 +0000 | [diff] [blame] | 499 | mask = ESAI_xCR_xFSL | ESAI_xCR_xFSR | ESAI_xCR_xWA; |
Nicolin Chen | 43d24e7 | 2014-01-10 17:54:06 +0800 | [diff] [blame] | 500 | regmap_update_bits(esai_priv->regmap, REG_ESAI_TCR, mask, xcr); |
| 501 | regmap_update_bits(esai_priv->regmap, REG_ESAI_RCR, mask, xcr); |
| 502 | |
| 503 | mask = ESAI_xCCR_xCKP | ESAI_xCCR_xHCKP | ESAI_xCCR_xFSP | |
S.j. Wang | cc29ea0 | 2019-02-18 08:29:11 +0000 | [diff] [blame] | 504 | ESAI_xCCR_xFSD | ESAI_xCCR_xCKD; |
Nicolin Chen | 43d24e7 | 2014-01-10 17:54:06 +0800 | [diff] [blame] | 505 | regmap_update_bits(esai_priv->regmap, REG_ESAI_TCCR, mask, xccr); |
| 506 | regmap_update_bits(esai_priv->regmap, REG_ESAI_RCCR, mask, xccr); |
| 507 | |
| 508 | return 0; |
| 509 | } |
| 510 | |
| 511 | static int fsl_esai_startup(struct snd_pcm_substream *substream, |
| 512 | struct snd_soc_dai *dai) |
| 513 | { |
| 514 | struct fsl_esai *esai_priv = snd_soc_dai_get_drvdata(dai); |
Nicolin Chen | 43d24e7 | 2014-01-10 17:54:06 +0800 | [diff] [blame] | 515 | |
Kuninori Morimoto | 1d9fb19 | 2020-05-15 09:47:17 +0900 | [diff] [blame] | 516 | if (!snd_soc_dai_active(dai)) { |
Nicolin Chen | 43d24e7 | 2014-01-10 17:54:06 +0800 | [diff] [blame] | 517 | /* Set synchronous mode */ |
| 518 | regmap_update_bits(esai_priv->regmap, REG_ESAI_SAICR, |
| 519 | ESAI_SAICR_SYNC, esai_priv->synchronous ? |
| 520 | ESAI_SAICR_SYNC : 0); |
| 521 | |
| 522 | /* Set a default slot number -- 2 */ |
| 523 | regmap_update_bits(esai_priv->regmap, REG_ESAI_TCCR, |
| 524 | ESAI_xCCR_xDC_MASK, ESAI_xCCR_xDC(2)); |
| 525 | regmap_update_bits(esai_priv->regmap, REG_ESAI_RCCR, |
| 526 | ESAI_xCCR_xDC_MASK, ESAI_xCCR_xDC(2)); |
| 527 | } |
| 528 | |
| 529 | return 0; |
Fabio Estevam | 33529ec | 2014-02-10 16:01:28 -0200 | [diff] [blame] | 530 | |
Nicolin Chen | 43d24e7 | 2014-01-10 17:54:06 +0800 | [diff] [blame] | 531 | } |
| 532 | |
| 533 | static int fsl_esai_hw_params(struct snd_pcm_substream *substream, |
| 534 | struct snd_pcm_hw_params *params, |
| 535 | struct snd_soc_dai *dai) |
| 536 | { |
| 537 | struct fsl_esai *esai_priv = snd_soc_dai_get_drvdata(dai); |
| 538 | bool tx = substream->stream == SNDRV_PCM_STREAM_PLAYBACK; |
Zidan Wang | 4ca7304 | 2015-11-24 15:32:09 +0800 | [diff] [blame] | 539 | u32 width = params_width(params); |
Nicolin Chen | 43d24e7 | 2014-01-10 17:54:06 +0800 | [diff] [blame] | 540 | u32 channels = params_channels(params); |
Shengjiu Wang | de0d712 | 2014-08-08 14:47:21 +0800 | [diff] [blame] | 541 | u32 pins = DIV_ROUND_UP(channels, esai_priv->slots); |
Nicolin Chen | 86ea522 | 2014-10-24 16:48:12 -0700 | [diff] [blame] | 542 | u32 slot_width = width; |
Xiubo Li | 3e18523 | 2014-04-04 15:10:26 +0800 | [diff] [blame] | 543 | u32 bclk, mask, val; |
| 544 | int ret; |
Nicolin Chen | 43d24e7 | 2014-01-10 17:54:06 +0800 | [diff] [blame] | 545 | |
Geert Uytterhoeven | d8ffcf7 | 2015-05-21 14:02:18 +0200 | [diff] [blame] | 546 | /* Override slot_width if being specifically set */ |
Nicolin Chen | 86ea522 | 2014-10-24 16:48:12 -0700 | [diff] [blame] | 547 | if (esai_priv->slot_width) |
| 548 | slot_width = esai_priv->slot_width; |
| 549 | |
| 550 | bclk = params_rate(params) * slot_width * esai_priv->slots; |
Nicolin Chen | 43d24e7 | 2014-01-10 17:54:06 +0800 | [diff] [blame] | 551 | |
S.j. Wang | 1997ee8 | 2019-04-04 09:40:56 +0000 | [diff] [blame] | 552 | ret = fsl_esai_set_bclk(dai, esai_priv->synchronous || tx, bclk); |
Nicolin Chen | 43d24e7 | 2014-01-10 17:54:06 +0800 | [diff] [blame] | 553 | if (ret) |
| 554 | return ret; |
| 555 | |
S.j. Wang | 1997ee8 | 2019-04-04 09:40:56 +0000 | [diff] [blame] | 556 | mask = ESAI_xCR_xSWS_MASK; |
| 557 | val = ESAI_xCR_xSWS(slot_width, width); |
| 558 | |
| 559 | regmap_update_bits(esai_priv->regmap, REG_ESAI_xCR(tx), mask, val); |
| 560 | /* Recording in synchronous mode needs to set TCR also */ |
| 561 | if (!tx && esai_priv->synchronous) |
| 562 | regmap_update_bits(esai_priv->regmap, REG_ESAI_TCR, mask, val); |
| 563 | |
Nicolin Chen | 43d24e7 | 2014-01-10 17:54:06 +0800 | [diff] [blame] | 564 | /* Use Normal mode to support monaural audio */ |
| 565 | regmap_update_bits(esai_priv->regmap, REG_ESAI_xCR(tx), |
| 566 | ESAI_xCR_xMOD_MASK, params_channels(params) > 1 ? |
| 567 | ESAI_xCR_xMOD_NETWORK : 0); |
| 568 | |
| 569 | regmap_update_bits(esai_priv->regmap, REG_ESAI_xFCR(tx), |
| 570 | ESAI_xFCR_xFR_MASK, ESAI_xFCR_xFR); |
| 571 | |
| 572 | mask = ESAI_xFCR_xFR_MASK | ESAI_xFCR_xWA_MASK | ESAI_xFCR_xFWM_MASK | |
| 573 | (tx ? ESAI_xFCR_TE_MASK | ESAI_xFCR_TIEN : ESAI_xFCR_RE_MASK); |
| 574 | val = ESAI_xFCR_xWA(width) | ESAI_xFCR_xFWM(esai_priv->fifo_depth) | |
Shengjiu Wang | de0d712 | 2014-08-08 14:47:21 +0800 | [diff] [blame] | 575 | (tx ? ESAI_xFCR_TE(pins) | ESAI_xFCR_TIEN : ESAI_xFCR_RE(pins)); |
Nicolin Chen | 43d24e7 | 2014-01-10 17:54:06 +0800 | [diff] [blame] | 576 | |
| 577 | regmap_update_bits(esai_priv->regmap, REG_ESAI_xFCR(tx), mask, val); |
| 578 | |
S.j. Wang | 1997ee8 | 2019-04-04 09:40:56 +0000 | [diff] [blame] | 579 | if (tx) |
| 580 | regmap_update_bits(esai_priv->regmap, REG_ESAI_TCR, |
| 581 | ESAI_xCR_PADC, ESAI_xCR_PADC); |
Nicolin Chen | 43d24e7 | 2014-01-10 17:54:06 +0800 | [diff] [blame] | 582 | |
Nicolin Chen | 4f8210f | 2014-05-06 16:56:02 +0800 | [diff] [blame] | 583 | /* Remove ESAI personal reset by configuring ESAI_PCRC and ESAI_PRRC */ |
| 584 | regmap_update_bits(esai_priv->regmap, REG_ESAI_PRRC, |
| 585 | ESAI_PRRC_PDC_MASK, ESAI_PRRC_PDC(ESAI_GPIO)); |
| 586 | regmap_update_bits(esai_priv->regmap, REG_ESAI_PCRC, |
| 587 | ESAI_PCRC_PC_MASK, ESAI_PCRC_PC(ESAI_GPIO)); |
Nicolin Chen | 43d24e7 | 2014-01-10 17:54:06 +0800 | [diff] [blame] | 588 | return 0; |
| 589 | } |
| 590 | |
Shengjiu Wang | 5be6155 | 2019-07-11 18:49:45 +0800 | [diff] [blame] | 591 | static int fsl_esai_hw_init(struct fsl_esai *esai_priv) |
| 592 | { |
| 593 | struct platform_device *pdev = esai_priv->pdev; |
| 594 | int ret; |
| 595 | |
| 596 | /* Reset ESAI unit */ |
| 597 | ret = regmap_update_bits(esai_priv->regmap, REG_ESAI_ECR, |
| 598 | ESAI_ECR_ESAIEN_MASK | ESAI_ECR_ERST_MASK, |
| 599 | ESAI_ECR_ESAIEN | ESAI_ECR_ERST); |
| 600 | if (ret) { |
| 601 | dev_err(&pdev->dev, "failed to reset ESAI: %d\n", ret); |
| 602 | return ret; |
| 603 | } |
| 604 | |
| 605 | /* |
| 606 | * We need to enable ESAI so as to access some of its registers. |
| 607 | * Otherwise, we would fail to dump regmap from user space. |
| 608 | */ |
| 609 | ret = regmap_update_bits(esai_priv->regmap, REG_ESAI_ECR, |
| 610 | ESAI_ECR_ESAIEN_MASK | ESAI_ECR_ERST_MASK, |
| 611 | ESAI_ECR_ESAIEN); |
| 612 | if (ret) { |
| 613 | dev_err(&pdev->dev, "failed to enable ESAI: %d\n", ret); |
| 614 | return ret; |
| 615 | } |
| 616 | |
| 617 | regmap_update_bits(esai_priv->regmap, REG_ESAI_PRRC, |
| 618 | ESAI_PRRC_PDC_MASK, 0); |
| 619 | regmap_update_bits(esai_priv->regmap, REG_ESAI_PCRC, |
| 620 | ESAI_PCRC_PC_MASK, 0); |
| 621 | |
| 622 | return 0; |
| 623 | } |
| 624 | |
| 625 | static int fsl_esai_register_restore(struct fsl_esai *esai_priv) |
| 626 | { |
| 627 | int ret; |
| 628 | |
| 629 | /* FIFO reset for safety */ |
| 630 | regmap_update_bits(esai_priv->regmap, REG_ESAI_TFCR, |
| 631 | ESAI_xFCR_xFR, ESAI_xFCR_xFR); |
| 632 | regmap_update_bits(esai_priv->regmap, REG_ESAI_RFCR, |
| 633 | ESAI_xFCR_xFR, ESAI_xFCR_xFR); |
| 634 | |
| 635 | regcache_mark_dirty(esai_priv->regmap); |
| 636 | ret = regcache_sync(esai_priv->regmap); |
| 637 | if (ret) |
| 638 | return ret; |
| 639 | |
| 640 | /* FIFO reset done */ |
| 641 | regmap_update_bits(esai_priv->regmap, REG_ESAI_TFCR, ESAI_xFCR_xFR, 0); |
| 642 | regmap_update_bits(esai_priv->regmap, REG_ESAI_RFCR, ESAI_xFCR_xFR, 0); |
| 643 | |
| 644 | return 0; |
| 645 | } |
| 646 | |
| 647 | static void fsl_esai_trigger_start(struct fsl_esai *esai_priv, bool tx) |
| 648 | { |
| 649 | u8 i, channels = esai_priv->channels[tx]; |
| 650 | u32 pins = DIV_ROUND_UP(channels, esai_priv->slots); |
| 651 | u32 mask; |
| 652 | |
| 653 | regmap_update_bits(esai_priv->regmap, REG_ESAI_xFCR(tx), |
| 654 | ESAI_xFCR_xFEN_MASK, ESAI_xFCR_xFEN); |
| 655 | |
| 656 | /* Write initial words reqiured by ESAI as normal procedure */ |
| 657 | for (i = 0; tx && i < channels; i++) |
| 658 | regmap_write(esai_priv->regmap, REG_ESAI_ETDR, 0x0); |
| 659 | |
| 660 | /* |
| 661 | * When set the TE/RE in the end of enablement flow, there |
| 662 | * will be channel swap issue for multi data line case. |
| 663 | * In order to workaround this issue, we switch the bit |
| 664 | * enablement sequence to below sequence |
| 665 | * 1) clear the xSMB & xSMA: which is done in probe and |
| 666 | * stop state. |
| 667 | * 2) set TE/RE |
| 668 | * 3) set xSMB |
| 669 | * 4) set xSMA: xSMA is the last one in this flow, which |
| 670 | * will trigger esai to start. |
| 671 | */ |
| 672 | regmap_update_bits(esai_priv->regmap, REG_ESAI_xCR(tx), |
| 673 | tx ? ESAI_xCR_TE_MASK : ESAI_xCR_RE_MASK, |
| 674 | tx ? ESAI_xCR_TE(pins) : ESAI_xCR_RE(pins)); |
| 675 | mask = tx ? esai_priv->tx_mask : esai_priv->rx_mask; |
| 676 | |
| 677 | regmap_update_bits(esai_priv->regmap, REG_ESAI_xSMB(tx), |
| 678 | ESAI_xSMB_xS_MASK, ESAI_xSMB_xS(mask)); |
| 679 | regmap_update_bits(esai_priv->regmap, REG_ESAI_xSMA(tx), |
| 680 | ESAI_xSMA_xS_MASK, ESAI_xSMA_xS(mask)); |
Shengjiu Wang | 7ccafa2 | 2019-07-11 18:49:46 +0800 | [diff] [blame] | 681 | |
| 682 | /* Enable Exception interrupt */ |
| 683 | regmap_update_bits(esai_priv->regmap, REG_ESAI_xCR(tx), |
| 684 | ESAI_xCR_xEIE_MASK, ESAI_xCR_xEIE); |
Shengjiu Wang | 5be6155 | 2019-07-11 18:49:45 +0800 | [diff] [blame] | 685 | } |
| 686 | |
| 687 | static void fsl_esai_trigger_stop(struct fsl_esai *esai_priv, bool tx) |
| 688 | { |
| 689 | regmap_update_bits(esai_priv->regmap, REG_ESAI_xCR(tx), |
Shengjiu Wang | 7ccafa2 | 2019-07-11 18:49:46 +0800 | [diff] [blame] | 690 | ESAI_xCR_xEIE_MASK, 0); |
| 691 | |
| 692 | regmap_update_bits(esai_priv->regmap, REG_ESAI_xCR(tx), |
Shengjiu Wang | 5be6155 | 2019-07-11 18:49:45 +0800 | [diff] [blame] | 693 | tx ? ESAI_xCR_TE_MASK : ESAI_xCR_RE_MASK, 0); |
| 694 | regmap_update_bits(esai_priv->regmap, REG_ESAI_xSMA(tx), |
| 695 | ESAI_xSMA_xS_MASK, 0); |
| 696 | regmap_update_bits(esai_priv->regmap, REG_ESAI_xSMB(tx), |
| 697 | ESAI_xSMB_xS_MASK, 0); |
| 698 | |
| 699 | /* Disable and reset FIFO */ |
| 700 | regmap_update_bits(esai_priv->regmap, REG_ESAI_xFCR(tx), |
| 701 | ESAI_xFCR_xFR | ESAI_xFCR_xFEN, ESAI_xFCR_xFR); |
| 702 | regmap_update_bits(esai_priv->regmap, REG_ESAI_xFCR(tx), |
| 703 | ESAI_xFCR_xFR, 0); |
| 704 | } |
| 705 | |
Takashi Iwai | a3d1f93 | 2020-09-03 12:47:47 +0200 | [diff] [blame] | 706 | static void fsl_esai_hw_reset(struct work_struct *work) |
Shengjiu Wang | 7ccafa2 | 2019-07-11 18:49:46 +0800 | [diff] [blame] | 707 | { |
Takashi Iwai | a3d1f93 | 2020-09-03 12:47:47 +0200 | [diff] [blame] | 708 | struct fsl_esai *esai_priv = container_of(work, struct fsl_esai, work); |
Shengjiu Wang | 7ccafa2 | 2019-07-11 18:49:46 +0800 | [diff] [blame] | 709 | bool tx = true, rx = false, enabled[2]; |
Shengjiu Wang | 35dac62 | 2019-10-28 17:11:05 +0800 | [diff] [blame] | 710 | unsigned long lock_flags; |
Shengjiu Wang | 7ccafa2 | 2019-07-11 18:49:46 +0800 | [diff] [blame] | 711 | u32 tfcr, rfcr; |
| 712 | |
Shengjiu Wang | 35dac62 | 2019-10-28 17:11:05 +0800 | [diff] [blame] | 713 | spin_lock_irqsave(&esai_priv->lock, lock_flags); |
Shengjiu Wang | 7ccafa2 | 2019-07-11 18:49:46 +0800 | [diff] [blame] | 714 | /* Save the registers */ |
| 715 | regmap_read(esai_priv->regmap, REG_ESAI_TFCR, &tfcr); |
| 716 | regmap_read(esai_priv->regmap, REG_ESAI_RFCR, &rfcr); |
| 717 | enabled[tx] = tfcr & ESAI_xFCR_xFEN; |
| 718 | enabled[rx] = rfcr & ESAI_xFCR_xFEN; |
| 719 | |
| 720 | /* Stop the tx & rx */ |
| 721 | fsl_esai_trigger_stop(esai_priv, tx); |
| 722 | fsl_esai_trigger_stop(esai_priv, rx); |
| 723 | |
| 724 | /* Reset the esai, and ignore return value */ |
| 725 | fsl_esai_hw_init(esai_priv); |
| 726 | |
| 727 | /* Enforce ESAI personal resets for both TX and RX */ |
| 728 | regmap_update_bits(esai_priv->regmap, REG_ESAI_TCR, |
| 729 | ESAI_xCR_xPR_MASK, ESAI_xCR_xPR); |
| 730 | regmap_update_bits(esai_priv->regmap, REG_ESAI_RCR, |
| 731 | ESAI_xCR_xPR_MASK, ESAI_xCR_xPR); |
| 732 | |
| 733 | /* Restore registers by regcache_sync, and ignore return value */ |
| 734 | fsl_esai_register_restore(esai_priv); |
| 735 | |
| 736 | /* Remove ESAI personal resets by configuring PCRC and PRRC also */ |
| 737 | regmap_update_bits(esai_priv->regmap, REG_ESAI_TCR, |
| 738 | ESAI_xCR_xPR_MASK, 0); |
| 739 | regmap_update_bits(esai_priv->regmap, REG_ESAI_RCR, |
| 740 | ESAI_xCR_xPR_MASK, 0); |
| 741 | regmap_update_bits(esai_priv->regmap, REG_ESAI_PRRC, |
| 742 | ESAI_PRRC_PDC_MASK, ESAI_PRRC_PDC(ESAI_GPIO)); |
| 743 | regmap_update_bits(esai_priv->regmap, REG_ESAI_PCRC, |
| 744 | ESAI_PCRC_PC_MASK, ESAI_PCRC_PC(ESAI_GPIO)); |
| 745 | |
| 746 | /* Restart tx / rx, if they already enabled */ |
| 747 | if (enabled[tx]) |
| 748 | fsl_esai_trigger_start(esai_priv, tx); |
| 749 | if (enabled[rx]) |
| 750 | fsl_esai_trigger_start(esai_priv, rx); |
Shengjiu Wang | 35dac62 | 2019-10-28 17:11:05 +0800 | [diff] [blame] | 751 | |
| 752 | spin_unlock_irqrestore(&esai_priv->lock, lock_flags); |
Shengjiu Wang | 7ccafa2 | 2019-07-11 18:49:46 +0800 | [diff] [blame] | 753 | } |
| 754 | |
Nicolin Chen | 43d24e7 | 2014-01-10 17:54:06 +0800 | [diff] [blame] | 755 | static int fsl_esai_trigger(struct snd_pcm_substream *substream, int cmd, |
| 756 | struct snd_soc_dai *dai) |
| 757 | { |
| 758 | struct fsl_esai *esai_priv = snd_soc_dai_get_drvdata(dai); |
| 759 | bool tx = substream->stream == SNDRV_PCM_STREAM_PLAYBACK; |
Shengjiu Wang | 35dac62 | 2019-10-28 17:11:05 +0800 | [diff] [blame] | 760 | unsigned long lock_flags; |
Shengjiu Wang | 5be6155 | 2019-07-11 18:49:45 +0800 | [diff] [blame] | 761 | |
| 762 | esai_priv->channels[tx] = substream->runtime->channels; |
Nicolin Chen | 43d24e7 | 2014-01-10 17:54:06 +0800 | [diff] [blame] | 763 | |
| 764 | switch (cmd) { |
| 765 | case SNDRV_PCM_TRIGGER_START: |
| 766 | case SNDRV_PCM_TRIGGER_RESUME: |
| 767 | case SNDRV_PCM_TRIGGER_PAUSE_RELEASE: |
Shengjiu Wang | 35dac62 | 2019-10-28 17:11:05 +0800 | [diff] [blame] | 768 | spin_lock_irqsave(&esai_priv->lock, lock_flags); |
Shengjiu Wang | 5be6155 | 2019-07-11 18:49:45 +0800 | [diff] [blame] | 769 | fsl_esai_trigger_start(esai_priv, tx); |
Shengjiu Wang | 35dac62 | 2019-10-28 17:11:05 +0800 | [diff] [blame] | 770 | spin_unlock_irqrestore(&esai_priv->lock, lock_flags); |
Nicolin Chen | 43d24e7 | 2014-01-10 17:54:06 +0800 | [diff] [blame] | 771 | break; |
| 772 | case SNDRV_PCM_TRIGGER_SUSPEND: |
| 773 | case SNDRV_PCM_TRIGGER_STOP: |
| 774 | case SNDRV_PCM_TRIGGER_PAUSE_PUSH: |
Shengjiu Wang | 35dac62 | 2019-10-28 17:11:05 +0800 | [diff] [blame] | 775 | spin_lock_irqsave(&esai_priv->lock, lock_flags); |
Shengjiu Wang | 5be6155 | 2019-07-11 18:49:45 +0800 | [diff] [blame] | 776 | fsl_esai_trigger_stop(esai_priv, tx); |
Shengjiu Wang | 35dac62 | 2019-10-28 17:11:05 +0800 | [diff] [blame] | 777 | spin_unlock_irqrestore(&esai_priv->lock, lock_flags); |
Nicolin Chen | 43d24e7 | 2014-01-10 17:54:06 +0800 | [diff] [blame] | 778 | break; |
| 779 | default: |
| 780 | return -EINVAL; |
| 781 | } |
| 782 | |
| 783 | return 0; |
| 784 | } |
| 785 | |
Gustavo A. R. Silva | 5d29e95 | 2017-07-13 02:19:25 -0500 | [diff] [blame] | 786 | static const struct snd_soc_dai_ops fsl_esai_dai_ops = { |
Nicolin Chen | 43d24e7 | 2014-01-10 17:54:06 +0800 | [diff] [blame] | 787 | .startup = fsl_esai_startup, |
Nicolin Chen | 43d24e7 | 2014-01-10 17:54:06 +0800 | [diff] [blame] | 788 | .trigger = fsl_esai_trigger, |
| 789 | .hw_params = fsl_esai_hw_params, |
| 790 | .set_sysclk = fsl_esai_set_dai_sysclk, |
| 791 | .set_fmt = fsl_esai_set_dai_fmt, |
| 792 | .set_tdm_slot = fsl_esai_set_dai_tdm_slot, |
| 793 | }; |
| 794 | |
| 795 | static int fsl_esai_dai_probe(struct snd_soc_dai *dai) |
| 796 | { |
| 797 | struct fsl_esai *esai_priv = snd_soc_dai_get_drvdata(dai); |
| 798 | |
| 799 | snd_soc_dai_init_dma_data(dai, &esai_priv->dma_params_tx, |
| 800 | &esai_priv->dma_params_rx); |
| 801 | |
| 802 | return 0; |
| 803 | } |
| 804 | |
| 805 | static struct snd_soc_dai_driver fsl_esai_dai = { |
| 806 | .probe = fsl_esai_dai_probe, |
| 807 | .playback = { |
Nicolin Chen | 74ccb27 | 2014-07-30 11:10:26 +0800 | [diff] [blame] | 808 | .stream_name = "CPU-Playback", |
Nicolin Chen | 43d24e7 | 2014-01-10 17:54:06 +0800 | [diff] [blame] | 809 | .channels_min = 1, |
| 810 | .channels_max = 12, |
Fabio Estevam | f2a3ee0 | 2017-04-12 09:37:21 -0300 | [diff] [blame] | 811 | .rates = SNDRV_PCM_RATE_8000_192000, |
Nicolin Chen | 43d24e7 | 2014-01-10 17:54:06 +0800 | [diff] [blame] | 812 | .formats = FSL_ESAI_FORMATS, |
| 813 | }, |
| 814 | .capture = { |
Nicolin Chen | 74ccb27 | 2014-07-30 11:10:26 +0800 | [diff] [blame] | 815 | .stream_name = "CPU-Capture", |
Nicolin Chen | 43d24e7 | 2014-01-10 17:54:06 +0800 | [diff] [blame] | 816 | .channels_min = 1, |
| 817 | .channels_max = 8, |
Fabio Estevam | f2a3ee0 | 2017-04-12 09:37:21 -0300 | [diff] [blame] | 818 | .rates = SNDRV_PCM_RATE_8000_192000, |
Nicolin Chen | 43d24e7 | 2014-01-10 17:54:06 +0800 | [diff] [blame] | 819 | .formats = FSL_ESAI_FORMATS, |
| 820 | }, |
| 821 | .ops = &fsl_esai_dai_ops, |
| 822 | }; |
| 823 | |
| 824 | static const struct snd_soc_component_driver fsl_esai_component = { |
| 825 | .name = "fsl-esai", |
| 826 | }; |
| 827 | |
Zidan Wang | c64c6076 | 2015-09-18 11:09:13 +0800 | [diff] [blame] | 828 | static const struct reg_default fsl_esai_reg_defaults[] = { |
Zidan Wang | 8973112 | 2015-10-26 15:19:02 +0800 | [diff] [blame] | 829 | {REG_ESAI_ETDR, 0x00000000}, |
| 830 | {REG_ESAI_ECR, 0x00000000}, |
| 831 | {REG_ESAI_TFCR, 0x00000000}, |
| 832 | {REG_ESAI_RFCR, 0x00000000}, |
| 833 | {REG_ESAI_TX0, 0x00000000}, |
| 834 | {REG_ESAI_TX1, 0x00000000}, |
| 835 | {REG_ESAI_TX2, 0x00000000}, |
| 836 | {REG_ESAI_TX3, 0x00000000}, |
| 837 | {REG_ESAI_TX4, 0x00000000}, |
| 838 | {REG_ESAI_TX5, 0x00000000}, |
| 839 | {REG_ESAI_TSR, 0x00000000}, |
| 840 | {REG_ESAI_SAICR, 0x00000000}, |
| 841 | {REG_ESAI_TCR, 0x00000000}, |
| 842 | {REG_ESAI_TCCR, 0x00000000}, |
| 843 | {REG_ESAI_RCR, 0x00000000}, |
| 844 | {REG_ESAI_RCCR, 0x00000000}, |
| 845 | {REG_ESAI_TSMA, 0x0000ffff}, |
| 846 | {REG_ESAI_TSMB, 0x0000ffff}, |
| 847 | {REG_ESAI_RSMA, 0x0000ffff}, |
| 848 | {REG_ESAI_RSMB, 0x0000ffff}, |
| 849 | {REG_ESAI_PRRC, 0x00000000}, |
| 850 | {REG_ESAI_PCRC, 0x00000000}, |
Zidan Wang | c64c6076 | 2015-09-18 11:09:13 +0800 | [diff] [blame] | 851 | }; |
| 852 | |
Nicolin Chen | 43d24e7 | 2014-01-10 17:54:06 +0800 | [diff] [blame] | 853 | static bool fsl_esai_readable_reg(struct device *dev, unsigned int reg) |
| 854 | { |
| 855 | switch (reg) { |
| 856 | case REG_ESAI_ERDR: |
| 857 | case REG_ESAI_ECR: |
| 858 | case REG_ESAI_ESR: |
| 859 | case REG_ESAI_TFCR: |
| 860 | case REG_ESAI_TFSR: |
| 861 | case REG_ESAI_RFCR: |
| 862 | case REG_ESAI_RFSR: |
| 863 | case REG_ESAI_RX0: |
| 864 | case REG_ESAI_RX1: |
| 865 | case REG_ESAI_RX2: |
| 866 | case REG_ESAI_RX3: |
| 867 | case REG_ESAI_SAISR: |
| 868 | case REG_ESAI_SAICR: |
| 869 | case REG_ESAI_TCR: |
| 870 | case REG_ESAI_TCCR: |
| 871 | case REG_ESAI_RCR: |
| 872 | case REG_ESAI_RCCR: |
| 873 | case REG_ESAI_TSMA: |
| 874 | case REG_ESAI_TSMB: |
| 875 | case REG_ESAI_RSMA: |
| 876 | case REG_ESAI_RSMB: |
| 877 | case REG_ESAI_PRRC: |
| 878 | case REG_ESAI_PCRC: |
| 879 | return true; |
| 880 | default: |
| 881 | return false; |
| 882 | } |
| 883 | } |
| 884 | |
Zidan Wang | c64c6076 | 2015-09-18 11:09:13 +0800 | [diff] [blame] | 885 | static bool fsl_esai_volatile_reg(struct device *dev, unsigned int reg) |
| 886 | { |
| 887 | switch (reg) { |
Zidan Wang | c64c6076 | 2015-09-18 11:09:13 +0800 | [diff] [blame] | 888 | case REG_ESAI_ERDR: |
| 889 | case REG_ESAI_ESR: |
| 890 | case REG_ESAI_TFSR: |
| 891 | case REG_ESAI_RFSR: |
Zidan Wang | c64c6076 | 2015-09-18 11:09:13 +0800 | [diff] [blame] | 892 | case REG_ESAI_RX0: |
| 893 | case REG_ESAI_RX1: |
| 894 | case REG_ESAI_RX2: |
| 895 | case REG_ESAI_RX3: |
| 896 | case REG_ESAI_SAISR: |
| 897 | return true; |
| 898 | default: |
| 899 | return false; |
| 900 | } |
| 901 | } |
| 902 | |
Nicolin Chen | 43d24e7 | 2014-01-10 17:54:06 +0800 | [diff] [blame] | 903 | static bool fsl_esai_writeable_reg(struct device *dev, unsigned int reg) |
| 904 | { |
| 905 | switch (reg) { |
| 906 | case REG_ESAI_ETDR: |
| 907 | case REG_ESAI_ECR: |
| 908 | case REG_ESAI_TFCR: |
| 909 | case REG_ESAI_RFCR: |
| 910 | case REG_ESAI_TX0: |
| 911 | case REG_ESAI_TX1: |
| 912 | case REG_ESAI_TX2: |
| 913 | case REG_ESAI_TX3: |
| 914 | case REG_ESAI_TX4: |
| 915 | case REG_ESAI_TX5: |
| 916 | case REG_ESAI_TSR: |
| 917 | case REG_ESAI_SAICR: |
| 918 | case REG_ESAI_TCR: |
| 919 | case REG_ESAI_TCCR: |
| 920 | case REG_ESAI_RCR: |
| 921 | case REG_ESAI_RCCR: |
| 922 | case REG_ESAI_TSMA: |
| 923 | case REG_ESAI_TSMB: |
| 924 | case REG_ESAI_RSMA: |
| 925 | case REG_ESAI_RSMB: |
| 926 | case REG_ESAI_PRRC: |
| 927 | case REG_ESAI_PCRC: |
| 928 | return true; |
| 929 | default: |
| 930 | return false; |
| 931 | } |
| 932 | } |
| 933 | |
Xiubo Li | 92bd033 | 2014-08-25 11:31:00 +0800 | [diff] [blame] | 934 | static const struct regmap_config fsl_esai_regmap_config = { |
Nicolin Chen | 43d24e7 | 2014-01-10 17:54:06 +0800 | [diff] [blame] | 935 | .reg_bits = 32, |
| 936 | .reg_stride = 4, |
| 937 | .val_bits = 32, |
| 938 | |
| 939 | .max_register = REG_ESAI_PCRC, |
Zidan Wang | c64c6076 | 2015-09-18 11:09:13 +0800 | [diff] [blame] | 940 | .reg_defaults = fsl_esai_reg_defaults, |
| 941 | .num_reg_defaults = ARRAY_SIZE(fsl_esai_reg_defaults), |
Nicolin Chen | 43d24e7 | 2014-01-10 17:54:06 +0800 | [diff] [blame] | 942 | .readable_reg = fsl_esai_readable_reg, |
Zidan Wang | c64c6076 | 2015-09-18 11:09:13 +0800 | [diff] [blame] | 943 | .volatile_reg = fsl_esai_volatile_reg, |
Nicolin Chen | 43d24e7 | 2014-01-10 17:54:06 +0800 | [diff] [blame] | 944 | .writeable_reg = fsl_esai_writeable_reg, |
Marek Vasut | 0effb86 | 2016-09-19 21:30:26 +0200 | [diff] [blame] | 945 | .cache_type = REGCACHE_FLAT, |
Nicolin Chen | 43d24e7 | 2014-01-10 17:54:06 +0800 | [diff] [blame] | 946 | }; |
| 947 | |
| 948 | static int fsl_esai_probe(struct platform_device *pdev) |
| 949 | { |
| 950 | struct device_node *np = pdev->dev.of_node; |
| 951 | struct fsl_esai *esai_priv; |
| 952 | struct resource *res; |
Fabio Estevam | 0600b3e | 2018-02-11 19:53:19 -0200 | [diff] [blame] | 953 | const __be32 *iprop; |
Nicolin Chen | 43d24e7 | 2014-01-10 17:54:06 +0800 | [diff] [blame] | 954 | void __iomem *regs; |
| 955 | int irq, ret; |
| 956 | |
| 957 | esai_priv = devm_kzalloc(&pdev->dev, sizeof(*esai_priv), GFP_KERNEL); |
| 958 | if (!esai_priv) |
| 959 | return -ENOMEM; |
| 960 | |
| 961 | esai_priv->pdev = pdev; |
Rob Herring | 5d585e1 | 2018-08-28 10:44:28 -0500 | [diff] [blame] | 962 | snprintf(esai_priv->name, sizeof(esai_priv->name), "%pOFn", np); |
Nicolin Chen | 43d24e7 | 2014-01-10 17:54:06 +0800 | [diff] [blame] | 963 | |
Shengjiu Wang | 6878e75 | 2020-05-15 18:10:50 +0800 | [diff] [blame] | 964 | esai_priv->soc = of_device_get_match_data(&pdev->dev); |
Shengjiu Wang | 7ccafa2 | 2019-07-11 18:49:46 +0800 | [diff] [blame] | 965 | |
Nicolin Chen | 43d24e7 | 2014-01-10 17:54:06 +0800 | [diff] [blame] | 966 | /* Get the addresses and IRQ */ |
| 967 | res = platform_get_resource(pdev, IORESOURCE_MEM, 0); |
| 968 | regs = devm_ioremap_resource(&pdev->dev, res); |
| 969 | if (IS_ERR(regs)) |
| 970 | return PTR_ERR(regs); |
| 971 | |
| 972 | esai_priv->regmap = devm_regmap_init_mmio_clk(&pdev->dev, |
| 973 | "core", regs, &fsl_esai_regmap_config); |
| 974 | if (IS_ERR(esai_priv->regmap)) { |
| 975 | dev_err(&pdev->dev, "failed to init regmap: %ld\n", |
| 976 | PTR_ERR(esai_priv->regmap)); |
| 977 | return PTR_ERR(esai_priv->regmap); |
| 978 | } |
| 979 | |
| 980 | esai_priv->coreclk = devm_clk_get(&pdev->dev, "core"); |
| 981 | if (IS_ERR(esai_priv->coreclk)) { |
| 982 | dev_err(&pdev->dev, "failed to get core clock: %ld\n", |
| 983 | PTR_ERR(esai_priv->coreclk)); |
| 984 | return PTR_ERR(esai_priv->coreclk); |
| 985 | } |
| 986 | |
| 987 | esai_priv->extalclk = devm_clk_get(&pdev->dev, "extal"); |
| 988 | if (IS_ERR(esai_priv->extalclk)) |
| 989 | dev_warn(&pdev->dev, "failed to get extal clock: %ld\n", |
| 990 | PTR_ERR(esai_priv->extalclk)); |
| 991 | |
| 992 | esai_priv->fsysclk = devm_clk_get(&pdev->dev, "fsys"); |
| 993 | if (IS_ERR(esai_priv->fsysclk)) |
| 994 | dev_warn(&pdev->dev, "failed to get fsys clock: %ld\n", |
| 995 | PTR_ERR(esai_priv->fsysclk)); |
| 996 | |
Shengjiu Wang | a2a4d60 | 2015-11-24 17:19:32 +0800 | [diff] [blame] | 997 | esai_priv->spbaclk = devm_clk_get(&pdev->dev, "spba"); |
| 998 | if (IS_ERR(esai_priv->spbaclk)) |
| 999 | dev_warn(&pdev->dev, "failed to get spba clock: %ld\n", |
| 1000 | PTR_ERR(esai_priv->spbaclk)); |
| 1001 | |
Nicolin Chen | 43d24e7 | 2014-01-10 17:54:06 +0800 | [diff] [blame] | 1002 | irq = platform_get_irq(pdev, 0); |
Stephen Boyd | cf9441a | 2019-07-30 11:15:49 -0700 | [diff] [blame] | 1003 | if (irq < 0) |
Nicolin Chen | 43d24e7 | 2014-01-10 17:54:06 +0800 | [diff] [blame] | 1004 | return irq; |
Nicolin Chen | 43d24e7 | 2014-01-10 17:54:06 +0800 | [diff] [blame] | 1005 | |
Shengjiu Wang | c836175 | 2020-07-23 12:00:08 +0800 | [diff] [blame] | 1006 | ret = devm_request_irq(&pdev->dev, irq, esai_isr, IRQF_SHARED, |
Nicolin Chen | 43d24e7 | 2014-01-10 17:54:06 +0800 | [diff] [blame] | 1007 | esai_priv->name, esai_priv); |
| 1008 | if (ret) { |
| 1009 | dev_err(&pdev->dev, "failed to claim irq %u\n", irq); |
| 1010 | return ret; |
| 1011 | } |
| 1012 | |
Shengjiu Wang | de0d712 | 2014-08-08 14:47:21 +0800 | [diff] [blame] | 1013 | /* Set a default slot number */ |
| 1014 | esai_priv->slots = 2; |
| 1015 | |
Nicolin Chen | 43d24e7 | 2014-01-10 17:54:06 +0800 | [diff] [blame] | 1016 | /* Set a default master/slave state */ |
| 1017 | esai_priv->slave_mode = true; |
| 1018 | |
| 1019 | /* Determine the FIFO depth */ |
| 1020 | iprop = of_get_property(np, "fsl,fifo-depth", NULL); |
| 1021 | if (iprop) |
| 1022 | esai_priv->fifo_depth = be32_to_cpup(iprop); |
| 1023 | else |
| 1024 | esai_priv->fifo_depth = 64; |
| 1025 | |
| 1026 | esai_priv->dma_params_tx.maxburst = 16; |
| 1027 | esai_priv->dma_params_rx.maxburst = 16; |
| 1028 | esai_priv->dma_params_tx.addr = res->start + REG_ESAI_ETDR; |
| 1029 | esai_priv->dma_params_rx.addr = res->start + REG_ESAI_ERDR; |
| 1030 | |
| 1031 | esai_priv->synchronous = |
| 1032 | of_property_read_bool(np, "fsl,esai-synchronous"); |
| 1033 | |
| 1034 | /* Implement full symmetry for synchronous mode */ |
| 1035 | if (esai_priv->synchronous) { |
Kuninori Morimoto | cb2f692 | 2021-01-15 13:54:08 +0900 | [diff] [blame] | 1036 | fsl_esai_dai.symmetric_rate = 1; |
Nicolin Chen | 43d24e7 | 2014-01-10 17:54:06 +0800 | [diff] [blame] | 1037 | fsl_esai_dai.symmetric_channels = 1; |
Kuninori Morimoto | cb2f692 | 2021-01-15 13:54:08 +0900 | [diff] [blame] | 1038 | fsl_esai_dai.symmetric_sample_bits = 1; |
Nicolin Chen | 43d24e7 | 2014-01-10 17:54:06 +0800 | [diff] [blame] | 1039 | } |
| 1040 | |
| 1041 | dev_set_drvdata(&pdev->dev, esai_priv); |
| 1042 | |
Shengjiu Wang | 35dac62 | 2019-10-28 17:11:05 +0800 | [diff] [blame] | 1043 | spin_lock_init(&esai_priv->lock); |
Shengjiu Wang | 5be6155 | 2019-07-11 18:49:45 +0800 | [diff] [blame] | 1044 | ret = fsl_esai_hw_init(esai_priv); |
| 1045 | if (ret) |
Nicolin Chen | 43d24e7 | 2014-01-10 17:54:06 +0800 | [diff] [blame] | 1046 | return ret; |
Nicolin Chen | 43d24e7 | 2014-01-10 17:54:06 +0800 | [diff] [blame] | 1047 | |
S.j. Wang | 0ff4e8c | 2019-02-27 06:31:12 +0000 | [diff] [blame] | 1048 | esai_priv->tx_mask = 0xFFFFFFFF; |
| 1049 | esai_priv->rx_mask = 0xFFFFFFFF; |
| 1050 | |
| 1051 | /* Clear the TSMA, TSMB, RSMA, RSMB */ |
| 1052 | regmap_write(esai_priv->regmap, REG_ESAI_TSMA, 0); |
| 1053 | regmap_write(esai_priv->regmap, REG_ESAI_TSMB, 0); |
| 1054 | regmap_write(esai_priv->regmap, REG_ESAI_RSMA, 0); |
| 1055 | regmap_write(esai_priv->regmap, REG_ESAI_RSMB, 0); |
| 1056 | |
Nicolin Chen | 43d24e7 | 2014-01-10 17:54:06 +0800 | [diff] [blame] | 1057 | ret = devm_snd_soc_register_component(&pdev->dev, &fsl_esai_component, |
| 1058 | &fsl_esai_dai, 1); |
| 1059 | if (ret) { |
| 1060 | dev_err(&pdev->dev, "failed to register DAI: %d\n", ret); |
| 1061 | return ret; |
| 1062 | } |
| 1063 | |
Takashi Iwai | a3d1f93 | 2020-09-03 12:47:47 +0200 | [diff] [blame] | 1064 | INIT_WORK(&esai_priv->work, fsl_esai_hw_reset); |
Shengjiu Wang | 7ccafa2 | 2019-07-11 18:49:46 +0800 | [diff] [blame] | 1065 | |
S.j. Wang | b2d337d | 2019-05-03 12:49:44 -0700 | [diff] [blame] | 1066 | pm_runtime_enable(&pdev->dev); |
| 1067 | |
| 1068 | regcache_cache_only(esai_priv->regmap, true); |
| 1069 | |
Shengjiu Wang | 0d69e0d | 2015-06-23 18:23:53 +0800 | [diff] [blame] | 1070 | ret = imx_pcm_dma_init(pdev, IMX_ESAI_DMABUF_SIZE); |
Nicolin Chen | 43d24e7 | 2014-01-10 17:54:06 +0800 | [diff] [blame] | 1071 | if (ret) |
| 1072 | dev_err(&pdev->dev, "failed to init imx pcm dma: %d\n", ret); |
| 1073 | |
| 1074 | return ret; |
| 1075 | } |
| 1076 | |
S.j. Wang | b2d337d | 2019-05-03 12:49:44 -0700 | [diff] [blame] | 1077 | static int fsl_esai_remove(struct platform_device *pdev) |
| 1078 | { |
Shengjiu Wang | 7ccafa2 | 2019-07-11 18:49:46 +0800 | [diff] [blame] | 1079 | struct fsl_esai *esai_priv = platform_get_drvdata(pdev); |
| 1080 | |
S.j. Wang | b2d337d | 2019-05-03 12:49:44 -0700 | [diff] [blame] | 1081 | pm_runtime_disable(&pdev->dev); |
Takashi Iwai | a3d1f93 | 2020-09-03 12:47:47 +0200 | [diff] [blame] | 1082 | cancel_work_sync(&esai_priv->work); |
S.j. Wang | b2d337d | 2019-05-03 12:49:44 -0700 | [diff] [blame] | 1083 | |
| 1084 | return 0; |
| 1085 | } |
| 1086 | |
Nicolin Chen | 43d24e7 | 2014-01-10 17:54:06 +0800 | [diff] [blame] | 1087 | static const struct of_device_id fsl_esai_dt_ids[] = { |
Shengjiu Wang | 6878e75 | 2020-05-15 18:10:50 +0800 | [diff] [blame] | 1088 | { .compatible = "fsl,imx35-esai", .data = &fsl_esai_imx35 }, |
| 1089 | { .compatible = "fsl,vf610-esai", .data = &fsl_esai_vf610 }, |
| 1090 | { .compatible = "fsl,imx6ull-esai", .data = &fsl_esai_imx6ull }, |
Nicolin Chen | 43d24e7 | 2014-01-10 17:54:06 +0800 | [diff] [blame] | 1091 | {} |
| 1092 | }; |
| 1093 | MODULE_DEVICE_TABLE(of, fsl_esai_dt_ids); |
| 1094 | |
S.j. Wang | b2d337d | 2019-05-03 12:49:44 -0700 | [diff] [blame] | 1095 | #ifdef CONFIG_PM |
| 1096 | static int fsl_esai_runtime_resume(struct device *dev) |
Zidan Wang | c64c6076 | 2015-09-18 11:09:13 +0800 | [diff] [blame] | 1097 | { |
| 1098 | struct fsl_esai *esai = dev_get_drvdata(dev); |
| 1099 | int ret; |
| 1100 | |
S.j. Wang | b2d337d | 2019-05-03 12:49:44 -0700 | [diff] [blame] | 1101 | /* |
| 1102 | * Some platforms might use the same bit to gate all three or two of |
| 1103 | * clocks, so keep all clocks open/close at the same time for safety |
| 1104 | */ |
| 1105 | ret = clk_prepare_enable(esai->coreclk); |
| 1106 | if (ret) |
| 1107 | return ret; |
| 1108 | if (!IS_ERR(esai->spbaclk)) { |
| 1109 | ret = clk_prepare_enable(esai->spbaclk); |
| 1110 | if (ret) |
| 1111 | goto err_spbaclk; |
| 1112 | } |
| 1113 | if (!IS_ERR(esai->extalclk)) { |
| 1114 | ret = clk_prepare_enable(esai->extalclk); |
| 1115 | if (ret) |
| 1116 | goto err_extalclk; |
| 1117 | } |
| 1118 | if (!IS_ERR(esai->fsysclk)) { |
| 1119 | ret = clk_prepare_enable(esai->fsysclk); |
| 1120 | if (ret) |
| 1121 | goto err_fsysclk; |
| 1122 | } |
| 1123 | |
Zidan Wang | c64c6076 | 2015-09-18 11:09:13 +0800 | [diff] [blame] | 1124 | regcache_cache_only(esai->regmap, false); |
| 1125 | |
Shengjiu Wang | 5be6155 | 2019-07-11 18:49:45 +0800 | [diff] [blame] | 1126 | ret = fsl_esai_register_restore(esai); |
Zidan Wang | c64c6076 | 2015-09-18 11:09:13 +0800 | [diff] [blame] | 1127 | if (ret) |
S.j. Wang | b2d337d | 2019-05-03 12:49:44 -0700 | [diff] [blame] | 1128 | goto err_regcache_sync; |
Zidan Wang | c64c6076 | 2015-09-18 11:09:13 +0800 | [diff] [blame] | 1129 | |
Zidan Wang | c64c6076 | 2015-09-18 11:09:13 +0800 | [diff] [blame] | 1130 | return 0; |
S.j. Wang | b2d337d | 2019-05-03 12:49:44 -0700 | [diff] [blame] | 1131 | |
| 1132 | err_regcache_sync: |
| 1133 | if (!IS_ERR(esai->fsysclk)) |
| 1134 | clk_disable_unprepare(esai->fsysclk); |
| 1135 | err_fsysclk: |
| 1136 | if (!IS_ERR(esai->extalclk)) |
| 1137 | clk_disable_unprepare(esai->extalclk); |
| 1138 | err_extalclk: |
| 1139 | if (!IS_ERR(esai->spbaclk)) |
| 1140 | clk_disable_unprepare(esai->spbaclk); |
| 1141 | err_spbaclk: |
| 1142 | clk_disable_unprepare(esai->coreclk); |
| 1143 | |
| 1144 | return ret; |
Zidan Wang | c64c6076 | 2015-09-18 11:09:13 +0800 | [diff] [blame] | 1145 | } |
S.j. Wang | b2d337d | 2019-05-03 12:49:44 -0700 | [diff] [blame] | 1146 | |
| 1147 | static int fsl_esai_runtime_suspend(struct device *dev) |
| 1148 | { |
| 1149 | struct fsl_esai *esai = dev_get_drvdata(dev); |
| 1150 | |
| 1151 | regcache_cache_only(esai->regmap, true); |
S.j. Wang | b2d337d | 2019-05-03 12:49:44 -0700 | [diff] [blame] | 1152 | |
| 1153 | if (!IS_ERR(esai->fsysclk)) |
| 1154 | clk_disable_unprepare(esai->fsysclk); |
| 1155 | if (!IS_ERR(esai->extalclk)) |
| 1156 | clk_disable_unprepare(esai->extalclk); |
| 1157 | if (!IS_ERR(esai->spbaclk)) |
| 1158 | clk_disable_unprepare(esai->spbaclk); |
| 1159 | clk_disable_unprepare(esai->coreclk); |
| 1160 | |
| 1161 | return 0; |
| 1162 | } |
| 1163 | #endif /* CONFIG_PM */ |
Zidan Wang | c64c6076 | 2015-09-18 11:09:13 +0800 | [diff] [blame] | 1164 | |
| 1165 | static const struct dev_pm_ops fsl_esai_pm_ops = { |
S.j. Wang | b2d337d | 2019-05-03 12:49:44 -0700 | [diff] [blame] | 1166 | SET_RUNTIME_PM_OPS(fsl_esai_runtime_suspend, |
| 1167 | fsl_esai_runtime_resume, |
| 1168 | NULL) |
| 1169 | SET_SYSTEM_SLEEP_PM_OPS(pm_runtime_force_suspend, |
| 1170 | pm_runtime_force_resume) |
Zidan Wang | c64c6076 | 2015-09-18 11:09:13 +0800 | [diff] [blame] | 1171 | }; |
| 1172 | |
Nicolin Chen | 43d24e7 | 2014-01-10 17:54:06 +0800 | [diff] [blame] | 1173 | static struct platform_driver fsl_esai_driver = { |
| 1174 | .probe = fsl_esai_probe, |
S.j. Wang | b2d337d | 2019-05-03 12:49:44 -0700 | [diff] [blame] | 1175 | .remove = fsl_esai_remove, |
Nicolin Chen | 43d24e7 | 2014-01-10 17:54:06 +0800 | [diff] [blame] | 1176 | .driver = { |
| 1177 | .name = "fsl-esai-dai", |
Zidan Wang | c64c6076 | 2015-09-18 11:09:13 +0800 | [diff] [blame] | 1178 | .pm = &fsl_esai_pm_ops, |
Nicolin Chen | 43d24e7 | 2014-01-10 17:54:06 +0800 | [diff] [blame] | 1179 | .of_match_table = fsl_esai_dt_ids, |
| 1180 | }, |
| 1181 | }; |
| 1182 | |
| 1183 | module_platform_driver(fsl_esai_driver); |
| 1184 | |
| 1185 | MODULE_AUTHOR("Freescale Semiconductor, Inc."); |
| 1186 | MODULE_DESCRIPTION("Freescale ESAI CPU DAI driver"); |
| 1187 | MODULE_LICENSE("GPL v2"); |
| 1188 | MODULE_ALIAS("platform:fsl-esai-dai"); |