blob: 08056fa0a0fa5ff1f5e18db91569224b7958b33f [file] [log] [blame]
Fabio Estevam3b5af9f2018-05-01 09:20:42 -03001// 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 Chen43d24e72014-01-10 17:54:06 +08006
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. Wangb2d337d2019-05-03 12:49:44 -070012#include <linux/pm_runtime.h>
Nicolin Chen43d24e72014-01-10 17:54:06 +080013#include <sound/dmaengine_pcm.h>
14#include <sound/pcm_params.h>
15
16#include "fsl_esai.h"
17#include "imx-pcm.h"
18
Nicolin Chen43d24e72014-01-10 17:54:06 +080019#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 Bossart3bae1712020-07-02 14:21:41 -050025 * struct fsl_esai_soc_data - soc specific data
Shengjiu Wang6878e752020-05-15 18:10:50 +080026 * @reset_at_xrun: flags for enable reset operaton
27 */
28struct fsl_esai_soc_data {
Shengjiu Wang6878e752020-05-15 18:10:50 +080029 bool reset_at_xrun;
30};
31
32/**
Pierre-Louis Bossart3bae1712020-07-02 14:21:41 -050033 * struct fsl_esai - ESAI private data
Nicolin Chen43d24e72014-01-10 17:54:06 +080034 * @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 Wanga2a4d602015-11-24 17:19:32 +080041 * @spbaclk: SPBA clock (optional, depending on SoC design)
Takashi Iwaia3d1f932020-09-03 12:47:47 +020042 * @work: work to handle the reset operation
Shengjiu Wang6878e752020-05-15 18:10:50 +080043 * @soc: soc specific data
Shengjiu Wang35dac622019-10-28 17:11:05 +080044 * @lock: spin lock between hw_reset() and trigger()
Nicolin Chen43d24e72014-01-10 17:54:06 +080045 * @fifo_depth: depth of tx/rx FIFO
46 * @slot_width: width of each DAI slot
Shengjiu Wangde0d7122014-08-08 14:47:21 +080047 * @slots: number of slots
Pierre-Louis Bossart3bae1712020-07-02 14:21:41 -050048 * @tx_mask: slot mask for TX
49 * @rx_mask: slot mask for RX
Shengjiu Wang5be61552019-07-11 18:49:45 +080050 * @channels: channel num for tx or rx
Nicolin Chen43d24e72014-01-10 17:54:06 +080051 * @hck_rate: clock rate of desired HCKx clock
Nicolin Chenf975ca42014-05-06 16:56:01 +080052 * @sck_rate: clock rate of desired SCKx clock
53 * @hck_dir: the direction of HCKx pads
Nicolin Chen43d24e72014-01-10 17:54:06 +080054 * @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 */
59struct 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 Wanga2a4d602015-11-24 17:19:32 +080067 struct clk *spbaclk;
Takashi Iwaia3d1f932020-09-03 12:47:47 +020068 struct work_struct work;
Shengjiu Wang6878e752020-05-15 18:10:50 +080069 const struct fsl_esai_soc_data *soc;
Shengjiu Wang35dac622019-10-28 17:11:05 +080070 spinlock_t lock; /* Protect hw_reset and trigger */
Nicolin Chen43d24e72014-01-10 17:54:06 +080071 u32 fifo_depth;
72 u32 slot_width;
Shengjiu Wangde0d7122014-08-08 14:47:21 +080073 u32 slots;
S.j. Wang0ff4e8c2019-02-27 06:31:12 +000074 u32 tx_mask;
75 u32 rx_mask;
Shengjiu Wang5be61552019-07-11 18:49:45 +080076 u32 channels[2];
Nicolin Chen43d24e72014-01-10 17:54:06 +080077 u32 hck_rate[2];
Nicolin Chenf975ca42014-05-06 16:56:01 +080078 u32 sck_rate[2];
79 bool hck_dir[2];
Nicolin Chen43d24e72014-01-10 17:54:06 +080080 bool sck_div[2];
81 bool slave_mode;
82 bool synchronous;
83 char name[32];
84};
85
Shengjiu Wang6878e752020-05-15 18:10:50 +080086static struct fsl_esai_soc_data fsl_esai_vf610 = {
Shengjiu Wang6878e752020-05-15 18:10:50 +080087 .reset_at_xrun = true,
88};
89
90static struct fsl_esai_soc_data fsl_esai_imx35 = {
Shengjiu Wang6878e752020-05-15 18:10:50 +080091 .reset_at_xrun = true,
92};
93
94static struct fsl_esai_soc_data fsl_esai_imx6ull = {
Shengjiu Wang6878e752020-05-15 18:10:50 +080095 .reset_at_xrun = false,
96};
97
Nicolin Chen43d24e72014-01-10 17:54:06 +080098static 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 Wang7ccafa22019-07-11 18:49:46 +0800103 u32 saisr;
Nicolin Chen43d24e72014-01-10 17:54:06 +0800104
105 regmap_read(esai_priv->regmap, REG_ESAI_ESR, &esr);
Shengjiu Wang7ccafa22019-07-11 18:49:46 +0800106 regmap_read(esai_priv->regmap, REG_ESAI_SAISR, &saisr);
107
108 if ((saisr & (ESAI_SAISR_TUE | ESAI_SAISR_ROE)) &&
Shengjiu Wang6878e752020-05-15 18:10:50 +0800109 esai_priv->soc->reset_at_xrun) {
Shengjiu Wang7ccafa22019-07-11 18:49:46 +0800110 dev_dbg(&pdev->dev, "reset module for xrun\n");
Shengjiu Wang1fecbb72020-04-27 14:23:21 +0800111 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 Iwaia3d1f932020-09-03 12:47:47 +0200115 schedule_work(&esai_priv->work);
Shengjiu Wang7ccafa22019-07-11 18:49:46 +0800116 }
Nicolin Chen43d24e72014-01-10 17:54:06 +0800117
118 if (esr & ESAI_ESR_TINIT_MASK)
Colin Ian King3bcc8652016-09-02 15:07:23 +0100119 dev_dbg(&pdev->dev, "isr: Transmission Initialized\n");
Nicolin Chen43d24e72014-01-10 17:54:06 +0800120
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 King3bcc8652016-09-02 15:07:23 +0100125 dev_warn(&pdev->dev, "isr: Transmission underrun\n");
Nicolin Chen43d24e72014-01-10 17:54:06 +0800126
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 King3bcc8652016-09-02 15:07:23 +0100131 dev_dbg(&pdev->dev, "isr: Transmission data exception\n");
Nicolin Chen43d24e72014-01-10 17:54:06 +0800132
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 Bossart3bae1712020-07-02 14:21:41 -0500155 * 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 Chen43d24e72014-01-10 17:54:06 +0800158 *
Pierre-Louis Bossart3bae1712020-07-02 14:21:41 -0500159 * @dai: pointer to DAI
160 * @tx: current setting is for playback or capture
Nicolin Chen43d24e72014-01-10 17:54:06 +0800161 * @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 Chen43d24e72014-01-10 17:54:06 +0800164 */
165static 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 Chenc6569412018-04-08 16:57:35 -0700189 /* 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 Chen43d24e72014-01-10 17:54:06 +0800196 /* 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
233out:
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
238out_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 Bossart3bae1712020-07-02 14:21:41 -0500250 * 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 Chen43d24e72014-01-10 17:54:06 +0800253 * (Input from outside; output from inside, FSYS or EXTAL)
Pierre-Louis Bossart3bae1712020-07-02 14:21:41 -0500254 * @freq: The required clock rate of HCKT/HCKR
255 * @dir: The clock direction of HCKT/HCKR
Nicolin Chen43d24e72014-01-10 17:54:06 +0800256 *
257 * Note: If the direction is input, we do not care about clk_id.
258 */
259static 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. Wang1997ee82019-04-04 09:40:56 +0000264 bool tx = (clk_id <= ESAI_HCKT_EXTAL || esai_priv->synchronous);
Nicolin Chen43d24e72014-01-10 17:54:06 +0800265 bool in = dir == SND_SOC_CLOCK_IN;
Xiubo Li3e185232014-04-04 15:10:26 +0800266 u32 ratio, ecr = 0;
Nicolin Chen43d24e72014-01-10 17:54:06 +0800267 unsigned long clk_rate;
Xiubo Li3e185232014-04-04 15:10:26 +0800268 int ret;
Nicolin Chen43d24e72014-01-10 17:54:06 +0800269
Nicolin Chen8a2278b2018-04-08 17:33:54 -0700270 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 Chenf975ca42014-05-06 16:56:01 +0800276 /* 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 Chen43d24e72014-01-10 17:54:06 +0800279
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. Wang903c2202019-04-28 02:24:27 +0000297 break;
Nicolin Chen43d24e72014-01-10 17:54:06 +0800298 case ESAI_HCKR_EXTAL:
S.j. Wang1997ee82019-04-04 09:40:56 +0000299 ecr |= esai_priv->synchronous ? ESAI_ECR_ETI : ESAI_ECR_ERI;
Nicolin Chen43d24e72014-01-10 17:54:06 +0800300 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 Chen57ebbca2014-05-06 16:56:00 +0800327 /* Only EXTAL source can be output directly without using PSR and PM */
328 if (ratio == 1 && clksrc == esai_priv->extalclk) {
Nicolin Chen43d24e72014-01-10 17:54:06 +0800329 /* Bypass all the dividers if not being needed */
330 ecr |= tx ? ESAI_ECR_ETO : ESAI_ECR_ERO;
331 goto out;
Nicolin Chen57ebbca2014-05-06 16:56:00 +0800332 } 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 Chen43d24e72014-01-10 17:54:06 +0800337 }
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
345out:
Nicolin Chenf975ca42014-05-06 16:56:01 +0800346 esai_priv->hck_dir[tx] = dir;
Nicolin Chen43d24e72014-01-10 17:54:06 +0800347 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 Bossart3bae1712020-07-02 14:21:41 -0500357 * 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 Chen43d24e72014-01-10 17:54:06 +0800361 */
362static 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 Chenf975ca42014-05-06 16:56:01 +0800367 int ret;
Nicolin Chen43d24e72014-01-10 17:54:06 +0800368
Nicolin Chenf975ca42014-05-06 16:56:01 +0800369 /* Don't apply for fully slave mode or unchanged bclk */
370 if (esai_priv->slave_mode || esai_priv->sck_rate[tx] == freq)
Nicolin Chen43d24e72014-01-10 17:54:06 +0800371 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 Chen89e47f62014-05-06 16:55:59 +0800387 /* 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 Chen43d24e72014-01-10 17:54:06 +0800389 dev_err(dai->dev, "the ratio is out of range (1 ~ 16)\n");
390 return -EINVAL;
391 }
392
Nicolin Chenf975ca42014-05-06 16:56:01 +0800393 ret = fsl_esai_divisor_cal(dai, tx, ratio, true,
Nicolin Chen43d24e72014-01-10 17:54:06 +0800394 esai_priv->sck_div[tx] ? 0 : ratio);
Nicolin Chenf975ca42014-05-06 16:56:01 +0800395 if (ret)
396 return ret;
397
398 /* Save current bclk rate */
399 esai_priv->sck_rate[tx] = freq;
400
401 return 0;
Nicolin Chen43d24e72014-01-10 17:54:06 +0800402}
403
404static 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 Chen43d24e72014-01-10 17:54:06 +0800412 regmap_update_bits(esai_priv->regmap, REG_ESAI_RCCR,
413 ESAI_xCCR_xDC_MASK, ESAI_xCCR_xDC(slots));
414
Nicolin Chen43d24e72014-01-10 17:54:06 +0800415 esai_priv->slot_width = slot_width;
Shengjiu Wangde0d7122014-08-08 14:47:21 +0800416 esai_priv->slots = slots;
S.j. Wang0ff4e8c2019-02-27 06:31:12 +0000417 esai_priv->tx_mask = tx_mask;
418 esai_priv->rx_mask = rx_mask;
Nicolin Chen43d24e72014-01-10 17:54:06 +0800419
420 return 0;
421}
422
423static 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. Wangcc29ea02019-02-18 08:29:11 +0000441 xccr |= ESAI_xCCR_xCKP | ESAI_xCCR_xHCKP;
442 xcr |= ESAI_xCR_xWA;
Nicolin Chen43d24e72014-01-10 17:54:06 +0800443 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. Wangcc29ea02019-02-18 08:29:11 +0000499 mask = ESAI_xCR_xFSL | ESAI_xCR_xFSR | ESAI_xCR_xWA;
Nicolin Chen43d24e72014-01-10 17:54:06 +0800500 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. Wangcc29ea02019-02-18 08:29:11 +0000504 ESAI_xCCR_xFSD | ESAI_xCCR_xCKD;
Nicolin Chen43d24e72014-01-10 17:54:06 +0800505 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
511static 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 Chen43d24e72014-01-10 17:54:06 +0800515
Kuninori Morimoto1d9fb192020-05-15 09:47:17 +0900516 if (!snd_soc_dai_active(dai)) {
Nicolin Chen43d24e72014-01-10 17:54:06 +0800517 /* 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 Estevam33529ec2014-02-10 16:01:28 -0200530
Nicolin Chen43d24e72014-01-10 17:54:06 +0800531}
532
533static 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 Wang4ca73042015-11-24 15:32:09 +0800539 u32 width = params_width(params);
Nicolin Chen43d24e72014-01-10 17:54:06 +0800540 u32 channels = params_channels(params);
Shengjiu Wangde0d7122014-08-08 14:47:21 +0800541 u32 pins = DIV_ROUND_UP(channels, esai_priv->slots);
Nicolin Chen86ea5222014-10-24 16:48:12 -0700542 u32 slot_width = width;
Xiubo Li3e185232014-04-04 15:10:26 +0800543 u32 bclk, mask, val;
544 int ret;
Nicolin Chen43d24e72014-01-10 17:54:06 +0800545
Geert Uytterhoevend8ffcf72015-05-21 14:02:18 +0200546 /* Override slot_width if being specifically set */
Nicolin Chen86ea5222014-10-24 16:48:12 -0700547 if (esai_priv->slot_width)
548 slot_width = esai_priv->slot_width;
549
550 bclk = params_rate(params) * slot_width * esai_priv->slots;
Nicolin Chen43d24e72014-01-10 17:54:06 +0800551
S.j. Wang1997ee82019-04-04 09:40:56 +0000552 ret = fsl_esai_set_bclk(dai, esai_priv->synchronous || tx, bclk);
Nicolin Chen43d24e72014-01-10 17:54:06 +0800553 if (ret)
554 return ret;
555
S.j. Wang1997ee82019-04-04 09:40:56 +0000556 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 Chen43d24e72014-01-10 17:54:06 +0800564 /* 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 Wangde0d7122014-08-08 14:47:21 +0800575 (tx ? ESAI_xFCR_TE(pins) | ESAI_xFCR_TIEN : ESAI_xFCR_RE(pins));
Nicolin Chen43d24e72014-01-10 17:54:06 +0800576
577 regmap_update_bits(esai_priv->regmap, REG_ESAI_xFCR(tx), mask, val);
578
S.j. Wang1997ee82019-04-04 09:40:56 +0000579 if (tx)
580 regmap_update_bits(esai_priv->regmap, REG_ESAI_TCR,
581 ESAI_xCR_PADC, ESAI_xCR_PADC);
Nicolin Chen43d24e72014-01-10 17:54:06 +0800582
Nicolin Chen4f8210f2014-05-06 16:56:02 +0800583 /* 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 Chen43d24e72014-01-10 17:54:06 +0800588 return 0;
589}
590
Shengjiu Wang5be61552019-07-11 18:49:45 +0800591static 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
625static 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
647static 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 Wang7ccafa22019-07-11 18:49:46 +0800681
682 /* Enable Exception interrupt */
683 regmap_update_bits(esai_priv->regmap, REG_ESAI_xCR(tx),
684 ESAI_xCR_xEIE_MASK, ESAI_xCR_xEIE);
Shengjiu Wang5be61552019-07-11 18:49:45 +0800685}
686
687static 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 Wang7ccafa22019-07-11 18:49:46 +0800690 ESAI_xCR_xEIE_MASK, 0);
691
692 regmap_update_bits(esai_priv->regmap, REG_ESAI_xCR(tx),
Shengjiu Wang5be61552019-07-11 18:49:45 +0800693 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 Iwaia3d1f932020-09-03 12:47:47 +0200706static void fsl_esai_hw_reset(struct work_struct *work)
Shengjiu Wang7ccafa22019-07-11 18:49:46 +0800707{
Takashi Iwaia3d1f932020-09-03 12:47:47 +0200708 struct fsl_esai *esai_priv = container_of(work, struct fsl_esai, work);
Shengjiu Wang7ccafa22019-07-11 18:49:46 +0800709 bool tx = true, rx = false, enabled[2];
Shengjiu Wang35dac622019-10-28 17:11:05 +0800710 unsigned long lock_flags;
Shengjiu Wang7ccafa22019-07-11 18:49:46 +0800711 u32 tfcr, rfcr;
712
Shengjiu Wang35dac622019-10-28 17:11:05 +0800713 spin_lock_irqsave(&esai_priv->lock, lock_flags);
Shengjiu Wang7ccafa22019-07-11 18:49:46 +0800714 /* 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 Wang35dac622019-10-28 17:11:05 +0800751
752 spin_unlock_irqrestore(&esai_priv->lock, lock_flags);
Shengjiu Wang7ccafa22019-07-11 18:49:46 +0800753}
754
Nicolin Chen43d24e72014-01-10 17:54:06 +0800755static 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 Wang35dac622019-10-28 17:11:05 +0800760 unsigned long lock_flags;
Shengjiu Wang5be61552019-07-11 18:49:45 +0800761
762 esai_priv->channels[tx] = substream->runtime->channels;
Nicolin Chen43d24e72014-01-10 17:54:06 +0800763
764 switch (cmd) {
765 case SNDRV_PCM_TRIGGER_START:
766 case SNDRV_PCM_TRIGGER_RESUME:
767 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
Shengjiu Wang35dac622019-10-28 17:11:05 +0800768 spin_lock_irqsave(&esai_priv->lock, lock_flags);
Shengjiu Wang5be61552019-07-11 18:49:45 +0800769 fsl_esai_trigger_start(esai_priv, tx);
Shengjiu Wang35dac622019-10-28 17:11:05 +0800770 spin_unlock_irqrestore(&esai_priv->lock, lock_flags);
Nicolin Chen43d24e72014-01-10 17:54:06 +0800771 break;
772 case SNDRV_PCM_TRIGGER_SUSPEND:
773 case SNDRV_PCM_TRIGGER_STOP:
774 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
Shengjiu Wang35dac622019-10-28 17:11:05 +0800775 spin_lock_irqsave(&esai_priv->lock, lock_flags);
Shengjiu Wang5be61552019-07-11 18:49:45 +0800776 fsl_esai_trigger_stop(esai_priv, tx);
Shengjiu Wang35dac622019-10-28 17:11:05 +0800777 spin_unlock_irqrestore(&esai_priv->lock, lock_flags);
Nicolin Chen43d24e72014-01-10 17:54:06 +0800778 break;
779 default:
780 return -EINVAL;
781 }
782
783 return 0;
784}
785
Gustavo A. R. Silva5d29e952017-07-13 02:19:25 -0500786static const struct snd_soc_dai_ops fsl_esai_dai_ops = {
Nicolin Chen43d24e72014-01-10 17:54:06 +0800787 .startup = fsl_esai_startup,
Nicolin Chen43d24e72014-01-10 17:54:06 +0800788 .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
795static 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
805static struct snd_soc_dai_driver fsl_esai_dai = {
806 .probe = fsl_esai_dai_probe,
807 .playback = {
Nicolin Chen74ccb272014-07-30 11:10:26 +0800808 .stream_name = "CPU-Playback",
Nicolin Chen43d24e72014-01-10 17:54:06 +0800809 .channels_min = 1,
810 .channels_max = 12,
Fabio Estevamf2a3ee02017-04-12 09:37:21 -0300811 .rates = SNDRV_PCM_RATE_8000_192000,
Nicolin Chen43d24e72014-01-10 17:54:06 +0800812 .formats = FSL_ESAI_FORMATS,
813 },
814 .capture = {
Nicolin Chen74ccb272014-07-30 11:10:26 +0800815 .stream_name = "CPU-Capture",
Nicolin Chen43d24e72014-01-10 17:54:06 +0800816 .channels_min = 1,
817 .channels_max = 8,
Fabio Estevamf2a3ee02017-04-12 09:37:21 -0300818 .rates = SNDRV_PCM_RATE_8000_192000,
Nicolin Chen43d24e72014-01-10 17:54:06 +0800819 .formats = FSL_ESAI_FORMATS,
820 },
821 .ops = &fsl_esai_dai_ops,
822};
823
824static const struct snd_soc_component_driver fsl_esai_component = {
825 .name = "fsl-esai",
826};
827
Zidan Wangc64c60762015-09-18 11:09:13 +0800828static const struct reg_default fsl_esai_reg_defaults[] = {
Zidan Wang89731122015-10-26 15:19:02 +0800829 {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 Wangc64c60762015-09-18 11:09:13 +0800851};
852
Nicolin Chen43d24e72014-01-10 17:54:06 +0800853static 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 Wangc64c60762015-09-18 11:09:13 +0800885static bool fsl_esai_volatile_reg(struct device *dev, unsigned int reg)
886{
887 switch (reg) {
Zidan Wangc64c60762015-09-18 11:09:13 +0800888 case REG_ESAI_ERDR:
889 case REG_ESAI_ESR:
890 case REG_ESAI_TFSR:
891 case REG_ESAI_RFSR:
Zidan Wangc64c60762015-09-18 11:09:13 +0800892 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 Chen43d24e72014-01-10 17:54:06 +0800903static 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 Li92bd0332014-08-25 11:31:00 +0800934static const struct regmap_config fsl_esai_regmap_config = {
Nicolin Chen43d24e72014-01-10 17:54:06 +0800935 .reg_bits = 32,
936 .reg_stride = 4,
937 .val_bits = 32,
938
939 .max_register = REG_ESAI_PCRC,
Zidan Wangc64c60762015-09-18 11:09:13 +0800940 .reg_defaults = fsl_esai_reg_defaults,
941 .num_reg_defaults = ARRAY_SIZE(fsl_esai_reg_defaults),
Nicolin Chen43d24e72014-01-10 17:54:06 +0800942 .readable_reg = fsl_esai_readable_reg,
Zidan Wangc64c60762015-09-18 11:09:13 +0800943 .volatile_reg = fsl_esai_volatile_reg,
Nicolin Chen43d24e72014-01-10 17:54:06 +0800944 .writeable_reg = fsl_esai_writeable_reg,
Marek Vasut0effb862016-09-19 21:30:26 +0200945 .cache_type = REGCACHE_FLAT,
Nicolin Chen43d24e72014-01-10 17:54:06 +0800946};
947
948static 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 Estevam0600b3e2018-02-11 19:53:19 -0200953 const __be32 *iprop;
Nicolin Chen43d24e72014-01-10 17:54:06 +0800954 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 Herring5d585e12018-08-28 10:44:28 -0500962 snprintf(esai_priv->name, sizeof(esai_priv->name), "%pOFn", np);
Nicolin Chen43d24e72014-01-10 17:54:06 +0800963
Shengjiu Wang6878e752020-05-15 18:10:50 +0800964 esai_priv->soc = of_device_get_match_data(&pdev->dev);
Shengjiu Wang7ccafa22019-07-11 18:49:46 +0800965
Nicolin Chen43d24e72014-01-10 17:54:06 +0800966 /* 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 Wanga2a4d602015-11-24 17:19:32 +0800997 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 Chen43d24e72014-01-10 17:54:06 +08001002 irq = platform_get_irq(pdev, 0);
Stephen Boydcf9441a2019-07-30 11:15:49 -07001003 if (irq < 0)
Nicolin Chen43d24e72014-01-10 17:54:06 +08001004 return irq;
Nicolin Chen43d24e72014-01-10 17:54:06 +08001005
Shengjiu Wangc8361752020-07-23 12:00:08 +08001006 ret = devm_request_irq(&pdev->dev, irq, esai_isr, IRQF_SHARED,
Nicolin Chen43d24e72014-01-10 17:54:06 +08001007 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 Wangde0d7122014-08-08 14:47:21 +08001013 /* Set a default slot number */
1014 esai_priv->slots = 2;
1015
Nicolin Chen43d24e72014-01-10 17:54:06 +08001016 /* 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 Morimotocb2f6922021-01-15 13:54:08 +09001036 fsl_esai_dai.symmetric_rate = 1;
Nicolin Chen43d24e72014-01-10 17:54:06 +08001037 fsl_esai_dai.symmetric_channels = 1;
Kuninori Morimotocb2f6922021-01-15 13:54:08 +09001038 fsl_esai_dai.symmetric_sample_bits = 1;
Nicolin Chen43d24e72014-01-10 17:54:06 +08001039 }
1040
1041 dev_set_drvdata(&pdev->dev, esai_priv);
1042
Shengjiu Wang35dac622019-10-28 17:11:05 +08001043 spin_lock_init(&esai_priv->lock);
Shengjiu Wang5be61552019-07-11 18:49:45 +08001044 ret = fsl_esai_hw_init(esai_priv);
1045 if (ret)
Nicolin Chen43d24e72014-01-10 17:54:06 +08001046 return ret;
Nicolin Chen43d24e72014-01-10 17:54:06 +08001047
S.j. Wang0ff4e8c2019-02-27 06:31:12 +00001048 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 Chen43d24e72014-01-10 17:54:06 +08001057 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 Iwaia3d1f932020-09-03 12:47:47 +02001064 INIT_WORK(&esai_priv->work, fsl_esai_hw_reset);
Shengjiu Wang7ccafa22019-07-11 18:49:46 +08001065
S.j. Wangb2d337d2019-05-03 12:49:44 -07001066 pm_runtime_enable(&pdev->dev);
1067
1068 regcache_cache_only(esai_priv->regmap, true);
1069
Shengjiu Wang0d69e0d2015-06-23 18:23:53 +08001070 ret = imx_pcm_dma_init(pdev, IMX_ESAI_DMABUF_SIZE);
Nicolin Chen43d24e72014-01-10 17:54:06 +08001071 if (ret)
1072 dev_err(&pdev->dev, "failed to init imx pcm dma: %d\n", ret);
1073
1074 return ret;
1075}
1076
S.j. Wangb2d337d2019-05-03 12:49:44 -07001077static int fsl_esai_remove(struct platform_device *pdev)
1078{
Shengjiu Wang7ccafa22019-07-11 18:49:46 +08001079 struct fsl_esai *esai_priv = platform_get_drvdata(pdev);
1080
S.j. Wangb2d337d2019-05-03 12:49:44 -07001081 pm_runtime_disable(&pdev->dev);
Takashi Iwaia3d1f932020-09-03 12:47:47 +02001082 cancel_work_sync(&esai_priv->work);
S.j. Wangb2d337d2019-05-03 12:49:44 -07001083
1084 return 0;
1085}
1086
Nicolin Chen43d24e72014-01-10 17:54:06 +08001087static const struct of_device_id fsl_esai_dt_ids[] = {
Shengjiu Wang6878e752020-05-15 18:10:50 +08001088 { .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 Chen43d24e72014-01-10 17:54:06 +08001091 {}
1092};
1093MODULE_DEVICE_TABLE(of, fsl_esai_dt_ids);
1094
S.j. Wangb2d337d2019-05-03 12:49:44 -07001095#ifdef CONFIG_PM
1096static int fsl_esai_runtime_resume(struct device *dev)
Zidan Wangc64c60762015-09-18 11:09:13 +08001097{
1098 struct fsl_esai *esai = dev_get_drvdata(dev);
1099 int ret;
1100
S.j. Wangb2d337d2019-05-03 12:49:44 -07001101 /*
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 Wangc64c60762015-09-18 11:09:13 +08001124 regcache_cache_only(esai->regmap, false);
1125
Shengjiu Wang5be61552019-07-11 18:49:45 +08001126 ret = fsl_esai_register_restore(esai);
Zidan Wangc64c60762015-09-18 11:09:13 +08001127 if (ret)
S.j. Wangb2d337d2019-05-03 12:49:44 -07001128 goto err_regcache_sync;
Zidan Wangc64c60762015-09-18 11:09:13 +08001129
Zidan Wangc64c60762015-09-18 11:09:13 +08001130 return 0;
S.j. Wangb2d337d2019-05-03 12:49:44 -07001131
1132err_regcache_sync:
1133 if (!IS_ERR(esai->fsysclk))
1134 clk_disable_unprepare(esai->fsysclk);
1135err_fsysclk:
1136 if (!IS_ERR(esai->extalclk))
1137 clk_disable_unprepare(esai->extalclk);
1138err_extalclk:
1139 if (!IS_ERR(esai->spbaclk))
1140 clk_disable_unprepare(esai->spbaclk);
1141err_spbaclk:
1142 clk_disable_unprepare(esai->coreclk);
1143
1144 return ret;
Zidan Wangc64c60762015-09-18 11:09:13 +08001145}
S.j. Wangb2d337d2019-05-03 12:49:44 -07001146
1147static 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. Wangb2d337d2019-05-03 12:49:44 -07001152
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 Wangc64c60762015-09-18 11:09:13 +08001164
1165static const struct dev_pm_ops fsl_esai_pm_ops = {
S.j. Wangb2d337d2019-05-03 12:49:44 -07001166 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 Wangc64c60762015-09-18 11:09:13 +08001171};
1172
Nicolin Chen43d24e72014-01-10 17:54:06 +08001173static struct platform_driver fsl_esai_driver = {
1174 .probe = fsl_esai_probe,
S.j. Wangb2d337d2019-05-03 12:49:44 -07001175 .remove = fsl_esai_remove,
Nicolin Chen43d24e72014-01-10 17:54:06 +08001176 .driver = {
1177 .name = "fsl-esai-dai",
Zidan Wangc64c60762015-09-18 11:09:13 +08001178 .pm = &fsl_esai_pm_ops,
Nicolin Chen43d24e72014-01-10 17:54:06 +08001179 .of_match_table = fsl_esai_dt_ids,
1180 },
1181};
1182
1183module_platform_driver(fsl_esai_driver);
1184
1185MODULE_AUTHOR("Freescale Semiconductor, Inc.");
1186MODULE_DESCRIPTION("Freescale ESAI CPU DAI driver");
1187MODULE_LICENSE("GPL v2");
1188MODULE_ALIAS("platform:fsl-esai-dai");