]> git.karo-electronics.de Git - karo-tx-linux.git/blob - sound/soc/fsl/fsl_spdif.c
ASoC: fsl_spdif: Rename all _div to _df
[karo-tx-linux.git] / sound / soc / fsl / fsl_spdif.c
1 /*
2  * Freescale S/PDIF ALSA SoC Digital Audio Interface (DAI) driver
3  *
4  * Copyright (C) 2013 Freescale Semiconductor, Inc.
5  *
6  * Based on stmp3xxx_spdif_dai.c
7  * Vladimir Barinov <vbarinov@embeddedalley.com>
8  * Copyright 2008 SigmaTel, Inc
9  * Copyright 2008 Embedded Alley Solutions, Inc
10  *
11  * This file is licensed under the terms of the GNU General Public License
12  * version 2.  This program  is licensed "as is" without any warranty of any
13  * kind, whether express or implied.
14  */
15
16 #include <linux/module.h>
17 #include <linux/clk.h>
18 #include <linux/clk-private.h>
19 #include <linux/bitrev.h>
20 #include <linux/regmap.h>
21 #include <linux/of_address.h>
22 #include <linux/of_device.h>
23 #include <linux/of_irq.h>
24
25 #include <sound/asoundef.h>
26 #include <sound/soc.h>
27 #include <sound/dmaengine_pcm.h>
28
29 #include "fsl_spdif.h"
30 #include "imx-pcm.h"
31
32 #define FSL_SPDIF_TXFIFO_WML    0x8
33 #define FSL_SPDIF_RXFIFO_WML    0x8
34
35 #define INTR_FOR_PLAYBACK (INT_TXFIFO_RESYNC)
36 #define INTR_FOR_CAPTURE (INT_SYM_ERR | INT_BIT_ERR | INT_URX_FUL | INT_URX_OV|\
37                 INT_QRX_FUL | INT_QRX_OV | INT_UQ_SYNC | INT_UQ_ERR |\
38                 INT_RXFIFO_RESYNC | INT_LOSS_LOCK | INT_DPLL_LOCKED)
39
40 /* Index list for the values that has if (DPLL Locked) condition */
41 static u8 srpc_dpll_locked[] = { 0x0, 0x1, 0x2, 0x3, 0x4, 0xa, 0xb };
42 #define SRPC_NODPLL_START1      0x5
43 #define SRPC_NODPLL_START2      0xc
44
45 #define DEFAULT_RXCLK_SRC       1
46
47 /*
48  * SPDIF control structure
49  * Defines channel status, subcode and Q sub
50  */
51 struct spdif_mixer_control {
52         /* spinlock to access control data */
53         spinlock_t ctl_lock;
54
55         /* IEC958 channel tx status bit */
56         unsigned char ch_status[4];
57
58         /* User bits */
59         unsigned char subcode[2 * SPDIF_UBITS_SIZE];
60
61         /* Q subcode part of user bits */
62         unsigned char qsub[2 * SPDIF_QSUB_SIZE];
63
64         /* Buffer offset for U/Q */
65         u32 upos;
66         u32 qpos;
67
68         /* Ready buffer index of the two buffers */
69         u32 ready_buf;
70 };
71
72 struct fsl_spdif_priv {
73         struct spdif_mixer_control fsl_spdif_control;
74         struct snd_soc_dai_driver cpu_dai_drv;
75         struct platform_device *pdev;
76         struct regmap *regmap;
77         bool dpll_locked;
78         u8 txclk_df[SPDIF_TXRATE_MAX];
79         u8 txclk_src[SPDIF_TXRATE_MAX];
80         u8 rxclk_src;
81         struct clk *txclk[SPDIF_TXRATE_MAX];
82         struct clk *rxclk;
83         struct clk *coreclk;
84         struct clk *sysclk;
85         struct snd_dmaengine_dai_dma_data dma_params_tx;
86         struct snd_dmaengine_dai_dma_data dma_params_rx;
87
88         /* The name space will be allocated dynamically */
89         char name[0];
90 };
91
92
93 /* DPLL locked and lock loss interrupt handler */
94 static void spdif_irq_dpll_lock(struct fsl_spdif_priv *spdif_priv)
95 {
96         struct regmap *regmap = spdif_priv->regmap;
97         struct platform_device *pdev = spdif_priv->pdev;
98         u32 locked;
99
100         regmap_read(regmap, REG_SPDIF_SRPC, &locked);
101         locked &= SRPC_DPLL_LOCKED;
102
103         dev_dbg(&pdev->dev, "isr: Rx dpll %s \n",
104                         locked ? "locked" : "loss lock");
105
106         spdif_priv->dpll_locked = locked ? true : false;
107 }
108
109 /* Receiver found illegal symbol interrupt handler */
110 static void spdif_irq_sym_error(struct fsl_spdif_priv *spdif_priv)
111 {
112         struct regmap *regmap = spdif_priv->regmap;
113         struct platform_device *pdev = spdif_priv->pdev;
114
115         dev_dbg(&pdev->dev, "isr: receiver found illegal symbol\n");
116
117         if (!spdif_priv->dpll_locked) {
118                 /* DPLL unlocked seems no audio stream */
119                 regmap_update_bits(regmap, REG_SPDIF_SIE, INT_SYM_ERR, 0);
120         }
121 }
122
123 /* U/Q Channel receive register full */
124 static void spdif_irq_uqrx_full(struct fsl_spdif_priv *spdif_priv, char name)
125 {
126         struct spdif_mixer_control *ctrl = &spdif_priv->fsl_spdif_control;
127         struct regmap *regmap = spdif_priv->regmap;
128         struct platform_device *pdev = spdif_priv->pdev;
129         u32 *pos, size, val, reg;
130
131         switch (name) {
132         case 'U':
133                 pos = &ctrl->upos;
134                 size = SPDIF_UBITS_SIZE;
135                 reg = REG_SPDIF_SRU;
136                 break;
137         case 'Q':
138                 pos = &ctrl->qpos;
139                 size = SPDIF_QSUB_SIZE;
140                 reg = REG_SPDIF_SRQ;
141                 break;
142         default:
143                 dev_err(&pdev->dev, "unsupported channel name\n");
144                 return;
145         }
146
147         dev_dbg(&pdev->dev, "isr: %c Channel receive register full\n", name);
148
149         if (*pos >= size * 2) {
150                 *pos = 0;
151         } else if (unlikely((*pos % size) + 3 > size)) {
152                 dev_err(&pdev->dev, "User bit receivce buffer overflow\n");
153                 return;
154         }
155
156         regmap_read(regmap, reg, &val);
157         ctrl->subcode[*pos++] = val >> 16;
158         ctrl->subcode[*pos++] = val >> 8;
159         ctrl->subcode[*pos++] = val;
160 }
161
162 /* U/Q Channel sync found */
163 static void spdif_irq_uq_sync(struct fsl_spdif_priv *spdif_priv)
164 {
165         struct spdif_mixer_control *ctrl = &spdif_priv->fsl_spdif_control;
166         struct platform_device *pdev = spdif_priv->pdev;
167
168         dev_dbg(&pdev->dev, "isr: U/Q Channel sync found\n");
169
170         /* U/Q buffer reset */
171         if (ctrl->qpos == 0)
172                 return;
173
174         /* Set ready to this buffer */
175         ctrl->ready_buf = (ctrl->qpos - 1) / SPDIF_QSUB_SIZE + 1;
176 }
177
178 /* U/Q Channel framing error */
179 static void spdif_irq_uq_err(struct fsl_spdif_priv *spdif_priv)
180 {
181         struct spdif_mixer_control *ctrl = &spdif_priv->fsl_spdif_control;
182         struct regmap *regmap = spdif_priv->regmap;
183         struct platform_device *pdev = spdif_priv->pdev;
184         u32 val;
185
186         dev_dbg(&pdev->dev, "isr: U/Q Channel framing error\n");
187
188         /* Read U/Q data to clear the irq and do buffer reset */
189         regmap_read(regmap, REG_SPDIF_SRU, &val);
190         regmap_read(regmap, REG_SPDIF_SRQ, &val);
191
192         /* Drop this U/Q buffer */
193         ctrl->ready_buf = 0;
194         ctrl->upos = 0;
195         ctrl->qpos = 0;
196 }
197
198 /* Get spdif interrupt status and clear the interrupt */
199 static u32 spdif_intr_status_clear(struct fsl_spdif_priv *spdif_priv)
200 {
201         struct regmap *regmap = spdif_priv->regmap;
202         u32 val, val2;
203
204         regmap_read(regmap, REG_SPDIF_SIS, &val);
205         regmap_read(regmap, REG_SPDIF_SIE, &val2);
206
207         regmap_write(regmap, REG_SPDIF_SIC, val & val2);
208
209         return val;
210 }
211
212 static irqreturn_t spdif_isr(int irq, void *devid)
213 {
214         struct fsl_spdif_priv *spdif_priv = (struct fsl_spdif_priv *)devid;
215         struct platform_device *pdev = spdif_priv->pdev;
216         u32 sis;
217
218         sis = spdif_intr_status_clear(spdif_priv);
219
220         if (sis & INT_DPLL_LOCKED)
221                 spdif_irq_dpll_lock(spdif_priv);
222
223         if (sis & INT_TXFIFO_UNOV)
224                 dev_dbg(&pdev->dev, "isr: Tx FIFO under/overrun\n");
225
226         if (sis & INT_TXFIFO_RESYNC)
227                 dev_dbg(&pdev->dev, "isr: Tx FIFO resync\n");
228
229         if (sis & INT_CNEW)
230                 dev_dbg(&pdev->dev, "isr: cstatus new\n");
231
232         if (sis & INT_VAL_NOGOOD)
233                 dev_dbg(&pdev->dev, "isr: validity flag no good\n");
234
235         if (sis & INT_SYM_ERR)
236                 spdif_irq_sym_error(spdif_priv);
237
238         if (sis & INT_BIT_ERR)
239                 dev_dbg(&pdev->dev, "isr: receiver found parity bit error\n");
240
241         if (sis & INT_URX_FUL)
242                 spdif_irq_uqrx_full(spdif_priv, 'U');
243
244         if (sis & INT_URX_OV)
245                 dev_dbg(&pdev->dev, "isr: U Channel receive register overrun\n");
246
247         if (sis & INT_QRX_FUL)
248                 spdif_irq_uqrx_full(spdif_priv, 'Q');
249
250         if (sis & INT_QRX_OV)
251                 dev_dbg(&pdev->dev, "isr: Q Channel receive register overrun\n");
252
253         if (sis & INT_UQ_SYNC)
254                 spdif_irq_uq_sync(spdif_priv);
255
256         if (sis & INT_UQ_ERR)
257                 spdif_irq_uq_err(spdif_priv);
258
259         if (sis & INT_RXFIFO_UNOV)
260                 dev_dbg(&pdev->dev, "isr: Rx FIFO under/overrun\n");
261
262         if (sis & INT_RXFIFO_RESYNC)
263                 dev_dbg(&pdev->dev, "isr: Rx FIFO resync\n");
264
265         if (sis & INT_LOSS_LOCK)
266                 spdif_irq_dpll_lock(spdif_priv);
267
268         /* FIXME: Write Tx FIFO to clear TxEm */
269         if (sis & INT_TX_EM)
270                 dev_dbg(&pdev->dev, "isr: Tx FIFO empty\n");
271
272         /* FIXME: Read Rx FIFO to clear RxFIFOFul */
273         if (sis & INT_RXFIFO_FUL)
274                 dev_dbg(&pdev->dev, "isr: Rx FIFO full\n");
275
276         return IRQ_HANDLED;
277 }
278
279 static int spdif_softreset(struct fsl_spdif_priv *spdif_priv)
280 {
281         struct regmap *regmap = spdif_priv->regmap;
282         u32 val, cycle = 1000;
283
284         regmap_write(regmap, REG_SPDIF_SCR, SCR_SOFT_RESET);
285
286         /*
287          * RESET bit would be cleared after finishing its reset procedure,
288          * which typically lasts 8 cycles. 1000 cycles will keep it safe.
289          */
290         do {
291                 regmap_read(regmap, REG_SPDIF_SCR, &val);
292         } while ((val & SCR_SOFT_RESET) && cycle--);
293
294         if (cycle)
295                 return 0;
296         else
297                 return -EBUSY;
298 }
299
300 static void spdif_set_cstatus(struct spdif_mixer_control *ctrl,
301                                 u8 mask, u8 cstatus)
302 {
303         ctrl->ch_status[3] &= ~mask;
304         ctrl->ch_status[3] |= cstatus & mask;
305 }
306
307 static void spdif_write_channel_status(struct fsl_spdif_priv *spdif_priv)
308 {
309         struct spdif_mixer_control *ctrl = &spdif_priv->fsl_spdif_control;
310         struct regmap *regmap = spdif_priv->regmap;
311         struct platform_device *pdev = spdif_priv->pdev;
312         u32 ch_status;
313
314         ch_status = (bitrev8(ctrl->ch_status[0]) << 16) |
315                 (bitrev8(ctrl->ch_status[1]) << 8) |
316                 bitrev8(ctrl->ch_status[2]);
317         regmap_write(regmap, REG_SPDIF_STCSCH, ch_status);
318
319         dev_dbg(&pdev->dev, "STCSCH: 0x%06x\n", ch_status);
320
321         ch_status = bitrev8(ctrl->ch_status[3]) << 16;
322         regmap_write(regmap, REG_SPDIF_STCSCL, ch_status);
323
324         dev_dbg(&pdev->dev, "STCSCL: 0x%06x\n", ch_status);
325 }
326
327 /* Set SPDIF PhaseConfig register for rx clock */
328 static int spdif_set_rx_clksrc(struct fsl_spdif_priv *spdif_priv,
329                                 enum spdif_gainsel gainsel, int dpll_locked)
330 {
331         struct regmap *regmap = spdif_priv->regmap;
332         u8 clksrc = spdif_priv->rxclk_src;
333
334         if (clksrc >= SRPC_CLKSRC_MAX || gainsel >= GAINSEL_MULTI_MAX)
335                 return -EINVAL;
336
337         regmap_update_bits(regmap, REG_SPDIF_SRPC,
338                         SRPC_CLKSRC_SEL_MASK | SRPC_GAINSEL_MASK,
339                         SRPC_CLKSRC_SEL_SET(clksrc) | SRPC_GAINSEL_SET(gainsel));
340
341         return 0;
342 }
343
344 static int spdif_set_sample_rate(struct snd_pcm_substream *substream,
345                                 int sample_rate)
346 {
347         struct snd_soc_pcm_runtime *rtd = substream->private_data;
348         struct fsl_spdif_priv *spdif_priv = snd_soc_dai_get_drvdata(rtd->cpu_dai);
349         struct spdif_mixer_control *ctrl = &spdif_priv->fsl_spdif_control;
350         struct regmap *regmap = spdif_priv->regmap;
351         struct platform_device *pdev = spdif_priv->pdev;
352         unsigned long csfs = 0;
353         u32 stc, mask, rate;
354         u8 clk, txclk_df;
355         int ret;
356
357         switch (sample_rate) {
358         case 32000:
359                 rate = SPDIF_TXRATE_32000;
360                 csfs = IEC958_AES3_CON_FS_32000;
361                 break;
362         case 44100:
363                 rate = SPDIF_TXRATE_44100;
364                 csfs = IEC958_AES3_CON_FS_44100;
365                 break;
366         case 48000:
367                 rate = SPDIF_TXRATE_48000;
368                 csfs = IEC958_AES3_CON_FS_48000;
369                 break;
370         default:
371                 dev_err(&pdev->dev, "unsupported sample rate %d\n", sample_rate);
372                 return -EINVAL;
373         }
374
375         clk = spdif_priv->txclk_src[rate];
376         if (clk >= STC_TXCLK_SRC_MAX) {
377                 dev_err(&pdev->dev, "tx clock source is out of range\n");
378                 return -EINVAL;
379         }
380
381         txclk_df = spdif_priv->txclk_df[rate];
382         if (txclk_df == 0) {
383                 dev_err(&pdev->dev, "the txclk_df can't be zero\n");
384                 return -EINVAL;
385         }
386
387         /* Don't mess up the clocks from other modules */
388         if (clk != STC_TXCLK_SPDIF_ROOT)
389                 goto clk_set_bypass;
390
391         /*
392          * The S/PDIF block needs a clock of 64 * fs * txclk_df.
393          * So request 64 * fs * (txclk_df + 1) to get rounded.
394          */
395         ret = clk_set_rate(spdif_priv->txclk[rate], 64 * sample_rate * (txclk_df + 1));
396         if (ret) {
397                 dev_err(&pdev->dev, "failed to set tx clock rate\n");
398                 return ret;
399         }
400
401 clk_set_bypass:
402         dev_dbg(&pdev->dev, "expected clock rate = %d\n",
403                         (64 * sample_rate * txclk_df));
404         dev_dbg(&pdev->dev, "actual clock rate = %ld\n",
405                         clk_get_rate(spdif_priv->txclk[rate]));
406
407         /* set fs field in consumer channel status */
408         spdif_set_cstatus(ctrl, IEC958_AES3_CON_FS, csfs);
409
410         /* select clock source and divisor */
411         stc = STC_TXCLK_ALL_EN | STC_TXCLK_SRC_SET(clk) | STC_TXCLK_DF(txclk_df);
412         mask = STC_TXCLK_ALL_EN_MASK | STC_TXCLK_SRC_MASK | STC_TXCLK_DF_MASK;
413         regmap_update_bits(regmap, REG_SPDIF_STC, mask, stc);
414
415         dev_dbg(&pdev->dev, "set sample rate to %d\n", sample_rate);
416
417         return 0;
418 }
419
420 static int fsl_spdif_startup(struct snd_pcm_substream *substream,
421                              struct snd_soc_dai *cpu_dai)
422 {
423         struct snd_soc_pcm_runtime *rtd = substream->private_data;
424         struct fsl_spdif_priv *spdif_priv = snd_soc_dai_get_drvdata(rtd->cpu_dai);
425         struct platform_device *pdev = spdif_priv->pdev;
426         struct regmap *regmap = spdif_priv->regmap;
427         u32 scr, mask, i;
428         int ret;
429
430         /* Reset module and interrupts only for first initialization */
431         if (!cpu_dai->active) {
432                 ret = clk_prepare_enable(spdif_priv->coreclk);
433                 if (ret) {
434                         dev_err(&pdev->dev, "failed to enable core clock\n");
435                         return ret;
436                 }
437
438                 ret = spdif_softreset(spdif_priv);
439                 if (ret) {
440                         dev_err(&pdev->dev, "failed to soft reset\n");
441                         goto err;
442                 }
443
444                 /* Disable all the interrupts */
445                 regmap_update_bits(regmap, REG_SPDIF_SIE, 0xffffff, 0);
446         }
447
448         if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
449                 scr = SCR_TXFIFO_AUTOSYNC | SCR_TXFIFO_CTRL_NORMAL |
450                         SCR_TXSEL_NORMAL | SCR_USRC_SEL_CHIP |
451                         SCR_TXFIFO_FSEL_IF8;
452                 mask = SCR_TXFIFO_AUTOSYNC_MASK | SCR_TXFIFO_CTRL_MASK |
453                         SCR_TXSEL_MASK | SCR_USRC_SEL_MASK |
454                         SCR_TXFIFO_FSEL_MASK;
455                 for (i = 0; i < SPDIF_TXRATE_MAX; i++)
456                         clk_prepare_enable(spdif_priv->txclk[i]);
457         } else {
458                 scr = SCR_RXFIFO_FSEL_IF8 | SCR_RXFIFO_AUTOSYNC;
459                 mask = SCR_RXFIFO_FSEL_MASK | SCR_RXFIFO_AUTOSYNC_MASK|
460                         SCR_RXFIFO_CTL_MASK | SCR_RXFIFO_OFF_MASK;
461                 clk_prepare_enable(spdif_priv->rxclk);
462         }
463         regmap_update_bits(regmap, REG_SPDIF_SCR, mask, scr);
464
465         /* Power up SPDIF module */
466         regmap_update_bits(regmap, REG_SPDIF_SCR, SCR_LOW_POWER, 0);
467
468         return 0;
469
470 err:
471         clk_disable_unprepare(spdif_priv->coreclk);
472
473         return ret;
474 }
475
476 static void fsl_spdif_shutdown(struct snd_pcm_substream *substream,
477                                 struct snd_soc_dai *cpu_dai)
478 {
479         struct snd_soc_pcm_runtime *rtd = substream->private_data;
480         struct fsl_spdif_priv *spdif_priv = snd_soc_dai_get_drvdata(rtd->cpu_dai);
481         struct regmap *regmap = spdif_priv->regmap;
482         u32 scr, mask, i;
483
484         if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
485                 scr = 0;
486                 mask = SCR_TXFIFO_AUTOSYNC_MASK | SCR_TXFIFO_CTRL_MASK |
487                         SCR_TXSEL_MASK | SCR_USRC_SEL_MASK |
488                         SCR_TXFIFO_FSEL_MASK;
489                 for (i = 0; i < SPDIF_TXRATE_MAX; i++)
490                         clk_disable_unprepare(spdif_priv->txclk[i]);
491         } else {
492                 scr = SCR_RXFIFO_OFF | SCR_RXFIFO_CTL_ZERO;
493                 mask = SCR_RXFIFO_FSEL_MASK | SCR_RXFIFO_AUTOSYNC_MASK|
494                         SCR_RXFIFO_CTL_MASK | SCR_RXFIFO_OFF_MASK;
495                 clk_disable_unprepare(spdif_priv->rxclk);
496         }
497         regmap_update_bits(regmap, REG_SPDIF_SCR, mask, scr);
498
499         /* Power down SPDIF module only if tx&rx are both inactive */
500         if (!cpu_dai->active) {
501                 spdif_intr_status_clear(spdif_priv);
502                 regmap_update_bits(regmap, REG_SPDIF_SCR,
503                                 SCR_LOW_POWER, SCR_LOW_POWER);
504                 clk_disable_unprepare(spdif_priv->coreclk);
505         }
506 }
507
508 static int fsl_spdif_hw_params(struct snd_pcm_substream *substream,
509                                 struct snd_pcm_hw_params *params,
510                                 struct snd_soc_dai *dai)
511 {
512         struct snd_soc_pcm_runtime *rtd = substream->private_data;
513         struct fsl_spdif_priv *spdif_priv = snd_soc_dai_get_drvdata(rtd->cpu_dai);
514         struct spdif_mixer_control *ctrl = &spdif_priv->fsl_spdif_control;
515         struct platform_device *pdev = spdif_priv->pdev;
516         u32 sample_rate = params_rate(params);
517         int ret = 0;
518
519         if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
520                 ret  = spdif_set_sample_rate(substream, sample_rate);
521                 if (ret) {
522                         dev_err(&pdev->dev, "%s: set sample rate failed: %d\n",
523                                         __func__, sample_rate);
524                         return ret;
525                 }
526                 spdif_set_cstatus(ctrl, IEC958_AES3_CON_CLOCK,
527                                 IEC958_AES3_CON_CLOCK_1000PPM);
528                 spdif_write_channel_status(spdif_priv);
529         } else {
530                 /* Setup rx clock source */
531                 ret = spdif_set_rx_clksrc(spdif_priv, SPDIF_DEFAULT_GAINSEL, 1);
532         }
533
534         return ret;
535 }
536
537 static int fsl_spdif_trigger(struct snd_pcm_substream *substream,
538                                 int cmd, struct snd_soc_dai *dai)
539 {
540         struct snd_soc_pcm_runtime *rtd = substream->private_data;
541         struct fsl_spdif_priv *spdif_priv = snd_soc_dai_get_drvdata(rtd->cpu_dai);
542         struct regmap *regmap = spdif_priv->regmap;
543         int is_playack = (substream->stream == SNDRV_PCM_STREAM_PLAYBACK);
544         u32 intr = is_playack ? INTR_FOR_PLAYBACK : INTR_FOR_CAPTURE;
545         u32 dmaen = is_playack ? SCR_DMA_TX_EN : SCR_DMA_RX_EN;;
546
547         switch (cmd) {
548         case SNDRV_PCM_TRIGGER_START:
549         case SNDRV_PCM_TRIGGER_RESUME:
550         case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
551                 regmap_update_bits(regmap, REG_SPDIF_SIE, intr, intr);
552                 regmap_update_bits(regmap, REG_SPDIF_SCR, dmaen, dmaen);
553                 break;
554         case SNDRV_PCM_TRIGGER_STOP:
555         case SNDRV_PCM_TRIGGER_SUSPEND:
556         case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
557                 regmap_update_bits(regmap, REG_SPDIF_SCR, dmaen, 0);
558                 regmap_update_bits(regmap, REG_SPDIF_SIE, intr, 0);
559                 break;
560         default:
561                 return -EINVAL;
562         }
563
564         return 0;
565 }
566
567 static struct snd_soc_dai_ops fsl_spdif_dai_ops = {
568         .startup = fsl_spdif_startup,
569         .hw_params = fsl_spdif_hw_params,
570         .trigger = fsl_spdif_trigger,
571         .shutdown = fsl_spdif_shutdown,
572 };
573
574
575 /*
576  * FSL SPDIF IEC958 controller(mixer) functions
577  *
578  *      Channel status get/put control
579  *      User bit value get/put control
580  *      Valid bit value get control
581  *      DPLL lock status get control
582  *      User bit sync mode selection control
583  */
584
585 static int fsl_spdif_info(struct snd_kcontrol *kcontrol,
586                                 struct snd_ctl_elem_info *uinfo)
587 {
588         uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
589         uinfo->count = 1;
590
591         return 0;
592 }
593
594 static int fsl_spdif_pb_get(struct snd_kcontrol *kcontrol,
595                                 struct snd_ctl_elem_value *uvalue)
596 {
597         struct snd_soc_dai *cpu_dai = snd_kcontrol_chip(kcontrol);
598         struct fsl_spdif_priv *spdif_priv = snd_soc_dai_get_drvdata(cpu_dai);
599         struct spdif_mixer_control *ctrl = &spdif_priv->fsl_spdif_control;
600
601         uvalue->value.iec958.status[0] = ctrl->ch_status[0];
602         uvalue->value.iec958.status[1] = ctrl->ch_status[1];
603         uvalue->value.iec958.status[2] = ctrl->ch_status[2];
604         uvalue->value.iec958.status[3] = ctrl->ch_status[3];
605
606         return 0;
607 }
608
609 static int fsl_spdif_pb_put(struct snd_kcontrol *kcontrol,
610                                 struct snd_ctl_elem_value *uvalue)
611 {
612         struct snd_soc_dai *cpu_dai = snd_kcontrol_chip(kcontrol);
613         struct fsl_spdif_priv *spdif_priv = snd_soc_dai_get_drvdata(cpu_dai);
614         struct spdif_mixer_control *ctrl = &spdif_priv->fsl_spdif_control;
615
616         ctrl->ch_status[0] = uvalue->value.iec958.status[0];
617         ctrl->ch_status[1] = uvalue->value.iec958.status[1];
618         ctrl->ch_status[2] = uvalue->value.iec958.status[2];
619         ctrl->ch_status[3] = uvalue->value.iec958.status[3];
620
621         spdif_write_channel_status(spdif_priv);
622
623         return 0;
624 }
625
626 /* Get channel status from SPDIF_RX_CCHAN register */
627 static int fsl_spdif_capture_get(struct snd_kcontrol *kcontrol,
628                                 struct snd_ctl_elem_value *ucontrol)
629 {
630         struct snd_soc_dai *cpu_dai = snd_kcontrol_chip(kcontrol);
631         struct fsl_spdif_priv *spdif_priv = snd_soc_dai_get_drvdata(cpu_dai);
632         struct regmap *regmap = spdif_priv->regmap;
633         u32 cstatus, val;
634
635         regmap_read(regmap, REG_SPDIF_SIS, &val);
636         if (!(val & INT_CNEW)) {
637                 return -EAGAIN;
638         }
639
640         regmap_read(regmap, REG_SPDIF_SRCSH, &cstatus);
641         ucontrol->value.iec958.status[0] = (cstatus >> 16) & 0xFF;
642         ucontrol->value.iec958.status[1] = (cstatus >> 8) & 0xFF;
643         ucontrol->value.iec958.status[2] = cstatus & 0xFF;
644
645         regmap_read(regmap, REG_SPDIF_SRCSL, &cstatus);
646         ucontrol->value.iec958.status[3] = (cstatus >> 16) & 0xFF;
647         ucontrol->value.iec958.status[4] = (cstatus >> 8) & 0xFF;
648         ucontrol->value.iec958.status[5] = cstatus & 0xFF;
649
650         /* Clear intr */
651         regmap_write(regmap, REG_SPDIF_SIC, INT_CNEW);
652
653         return 0;
654 }
655
656 /*
657  * Get User bits (subcode) from chip value which readed out
658  * in UChannel register.
659  */
660 static int fsl_spdif_subcode_get(struct snd_kcontrol *kcontrol,
661                                 struct snd_ctl_elem_value *ucontrol)
662 {
663         struct snd_soc_dai *cpu_dai = snd_kcontrol_chip(kcontrol);
664         struct fsl_spdif_priv *spdif_priv = snd_soc_dai_get_drvdata(cpu_dai);
665         struct spdif_mixer_control *ctrl = &spdif_priv->fsl_spdif_control;
666         unsigned long flags;
667         int ret = 0;
668
669         spin_lock_irqsave(&ctrl->ctl_lock, flags);
670         if (ctrl->ready_buf) {
671                 int idx = (ctrl->ready_buf - 1) * SPDIF_UBITS_SIZE;
672                 memcpy(&ucontrol->value.iec958.subcode[0],
673                                 &ctrl->subcode[idx], SPDIF_UBITS_SIZE);
674         } else {
675                 ret = -EAGAIN;
676         }
677         spin_unlock_irqrestore(&ctrl->ctl_lock, flags);
678
679         return ret;
680 }
681
682 /* Q-subcode infomation. The byte size is SPDIF_UBITS_SIZE/8 */
683 static int fsl_spdif_qinfo(struct snd_kcontrol *kcontrol,
684                                 struct snd_ctl_elem_info *uinfo)
685 {
686         uinfo->type = SNDRV_CTL_ELEM_TYPE_BYTES;
687         uinfo->count = SPDIF_QSUB_SIZE;
688
689         return 0;
690 }
691
692 /* Get Q subcode from chip value which readed out in QChannel register */
693 static int fsl_spdif_qget(struct snd_kcontrol *kcontrol,
694                                 struct snd_ctl_elem_value *ucontrol)
695 {
696         struct snd_soc_dai *cpu_dai = snd_kcontrol_chip(kcontrol);
697         struct fsl_spdif_priv *spdif_priv = snd_soc_dai_get_drvdata(cpu_dai);
698         struct spdif_mixer_control *ctrl = &spdif_priv->fsl_spdif_control;
699         unsigned long flags;
700         int ret = 0;
701
702         spin_lock_irqsave(&ctrl->ctl_lock, flags);
703         if (ctrl->ready_buf) {
704                 int idx = (ctrl->ready_buf - 1) * SPDIF_QSUB_SIZE;
705                 memcpy(&ucontrol->value.bytes.data[0],
706                                 &ctrl->qsub[idx], SPDIF_QSUB_SIZE);
707         } else {
708                 ret = -EAGAIN;
709         }
710         spin_unlock_irqrestore(&ctrl->ctl_lock, flags);
711
712         return ret;
713 }
714
715 /* Valid bit infomation */
716 static int fsl_spdif_vbit_info(struct snd_kcontrol *kcontrol,
717                                 struct snd_ctl_elem_info *uinfo)
718 {
719         uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
720         uinfo->count = 1;
721         uinfo->value.integer.min = 0;
722         uinfo->value.integer.max = 1;
723
724         return 0;
725 }
726
727 /* Get valid good bit from interrupt status register */
728 static int fsl_spdif_vbit_get(struct snd_kcontrol *kcontrol,
729                                 struct snd_ctl_elem_value *ucontrol)
730 {
731         struct snd_soc_dai *cpu_dai = snd_kcontrol_chip(kcontrol);
732         struct fsl_spdif_priv *spdif_priv = snd_soc_dai_get_drvdata(cpu_dai);
733         struct regmap *regmap = spdif_priv->regmap;
734         u32 val;
735
736         val = regmap_read(regmap, REG_SPDIF_SIS, &val);
737         ucontrol->value.integer.value[0] = (val & INT_VAL_NOGOOD) != 0;
738         regmap_write(regmap, REG_SPDIF_SIC, INT_VAL_NOGOOD);
739
740         return 0;
741 }
742
743 /* DPLL lock infomation */
744 static int fsl_spdif_rxrate_info(struct snd_kcontrol *kcontrol,
745                                 struct snd_ctl_elem_info *uinfo)
746 {
747         uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
748         uinfo->count = 1;
749         uinfo->value.integer.min = 16000;
750         uinfo->value.integer.max = 96000;
751
752         return 0;
753 }
754
755 static u32 gainsel_multi[GAINSEL_MULTI_MAX] = {
756         24, 16, 12, 8, 6, 4, 3,
757 };
758
759 /* Get RX data clock rate given the SPDIF bus_clk */
760 static int spdif_get_rxclk_rate(struct fsl_spdif_priv *spdif_priv,
761                                 enum spdif_gainsel gainsel)
762 {
763         struct regmap *regmap = spdif_priv->regmap;
764         struct platform_device *pdev = spdif_priv->pdev;
765         u64 tmpval64, busclk_freq = 0;
766         u32 freqmeas, phaseconf;
767         u8 clksrc;
768
769         regmap_read(regmap, REG_SPDIF_SRFM, &freqmeas);
770         regmap_read(regmap, REG_SPDIF_SRPC, &phaseconf);
771
772         clksrc = (phaseconf >> SRPC_CLKSRC_SEL_OFFSET) & 0xf;
773         if (srpc_dpll_locked[clksrc] && (phaseconf & SRPC_DPLL_LOCKED)) {
774                 /* Get bus clock from system */
775                 busclk_freq = clk_get_rate(spdif_priv->sysclk);
776         }
777
778         /* FreqMeas_CLK = (BUS_CLK * FreqMeas) / 2 ^ 10 / GAINSEL / 128 */
779         tmpval64 = (u64) busclk_freq * freqmeas;
780         do_div(tmpval64, gainsel_multi[gainsel] * 1024);
781         do_div(tmpval64, 128 * 1024);
782
783         dev_dbg(&pdev->dev, "FreqMeas: %d\n", freqmeas);
784         dev_dbg(&pdev->dev, "BusclkFreq: %lld\n", busclk_freq);
785         dev_dbg(&pdev->dev, "RxRate: %lld\n", tmpval64);
786
787         return (int)tmpval64;
788 }
789
790 /*
791  * Get DPLL lock or not info from stable interrupt status register.
792  * User application must use this control to get locked,
793  * then can do next PCM operation
794  */
795 static int fsl_spdif_rxrate_get(struct snd_kcontrol *kcontrol,
796                                 struct snd_ctl_elem_value *ucontrol)
797 {
798         struct snd_soc_dai *cpu_dai = snd_kcontrol_chip(kcontrol);
799         struct fsl_spdif_priv *spdif_priv = snd_soc_dai_get_drvdata(cpu_dai);
800         int rate = spdif_get_rxclk_rate(spdif_priv, SPDIF_DEFAULT_GAINSEL);
801
802         if (spdif_priv->dpll_locked)
803                 ucontrol->value.integer.value[0] = rate;
804         else
805                 ucontrol->value.integer.value[0] = 0;
806
807         return 0;
808 }
809
810 /* User bit sync mode info */
811 static int fsl_spdif_usync_info(struct snd_kcontrol *kcontrol,
812                                 struct snd_ctl_elem_info *uinfo)
813 {
814         uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
815         uinfo->count = 1;
816         uinfo->value.integer.min = 0;
817         uinfo->value.integer.max = 1;
818
819         return 0;
820 }
821
822 /*
823  * User bit sync mode:
824  * 1 CD User channel subcode
825  * 0 Non-CD data
826  */
827 static int fsl_spdif_usync_get(struct snd_kcontrol *kcontrol,
828                                struct snd_ctl_elem_value *ucontrol)
829 {
830         struct snd_soc_dai *cpu_dai = snd_kcontrol_chip(kcontrol);
831         struct fsl_spdif_priv *spdif_priv = snd_soc_dai_get_drvdata(cpu_dai);
832         struct regmap *regmap = spdif_priv->regmap;
833         u32 val;
834
835         regmap_read(regmap, REG_SPDIF_SRCD, &val);
836         ucontrol->value.integer.value[0] = (val & SRCD_CD_USER) != 0;
837
838         return 0;
839 }
840
841 /*
842  * User bit sync mode:
843  * 1 CD User channel subcode
844  * 0 Non-CD data
845  */
846 static int fsl_spdif_usync_put(struct snd_kcontrol *kcontrol,
847                                 struct snd_ctl_elem_value *ucontrol)
848 {
849         struct snd_soc_dai *cpu_dai = snd_kcontrol_chip(kcontrol);
850         struct fsl_spdif_priv *spdif_priv = snd_soc_dai_get_drvdata(cpu_dai);
851         struct regmap *regmap = spdif_priv->regmap;
852         u32 val = ucontrol->value.integer.value[0] << SRCD_CD_USER_OFFSET;
853
854         regmap_update_bits(regmap, REG_SPDIF_SRCD, SRCD_CD_USER, val);
855
856         return 0;
857 }
858
859 /* FSL SPDIF IEC958 controller defines */
860 static struct snd_kcontrol_new fsl_spdif_ctrls[] = {
861         /* Status cchanel controller */
862         {
863                 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
864                 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, DEFAULT),
865                 .access = SNDRV_CTL_ELEM_ACCESS_READ |
866                         SNDRV_CTL_ELEM_ACCESS_WRITE |
867                         SNDRV_CTL_ELEM_ACCESS_VOLATILE,
868                 .info = fsl_spdif_info,
869                 .get = fsl_spdif_pb_get,
870                 .put = fsl_spdif_pb_put,
871         },
872         {
873                 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
874                 .name = SNDRV_CTL_NAME_IEC958("", CAPTURE, DEFAULT),
875                 .access = SNDRV_CTL_ELEM_ACCESS_READ |
876                         SNDRV_CTL_ELEM_ACCESS_VOLATILE,
877                 .info = fsl_spdif_info,
878                 .get = fsl_spdif_capture_get,
879         },
880         /* User bits controller */
881         {
882                 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
883                 .name = "IEC958 Subcode Capture Default",
884                 .access = SNDRV_CTL_ELEM_ACCESS_READ |
885                         SNDRV_CTL_ELEM_ACCESS_VOLATILE,
886                 .info = fsl_spdif_info,
887                 .get = fsl_spdif_subcode_get,
888         },
889         {
890                 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
891                 .name = "IEC958 Q-subcode Capture Default",
892                 .access = SNDRV_CTL_ELEM_ACCESS_READ |
893                         SNDRV_CTL_ELEM_ACCESS_VOLATILE,
894                 .info = fsl_spdif_qinfo,
895                 .get = fsl_spdif_qget,
896         },
897         /* Valid bit error controller */
898         {
899                 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
900                 .name = "IEC958 V-Bit Errors",
901                 .access = SNDRV_CTL_ELEM_ACCESS_READ |
902                         SNDRV_CTL_ELEM_ACCESS_VOLATILE,
903                 .info = fsl_spdif_vbit_info,
904                 .get = fsl_spdif_vbit_get,
905         },
906         /* DPLL lock info get controller */
907         {
908                 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
909                 .name = "RX Sample Rate",
910                 .access = SNDRV_CTL_ELEM_ACCESS_READ |
911                         SNDRV_CTL_ELEM_ACCESS_VOLATILE,
912                 .info = fsl_spdif_rxrate_info,
913                 .get = fsl_spdif_rxrate_get,
914         },
915         /* User bit sync mode set/get controller */
916         {
917                 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
918                 .name = "IEC958 USyncMode CDText",
919                 .access = SNDRV_CTL_ELEM_ACCESS_READ |
920                         SNDRV_CTL_ELEM_ACCESS_WRITE |
921                         SNDRV_CTL_ELEM_ACCESS_VOLATILE,
922                 .info = fsl_spdif_usync_info,
923                 .get = fsl_spdif_usync_get,
924                 .put = fsl_spdif_usync_put,
925         },
926 };
927
928 static int fsl_spdif_dai_probe(struct snd_soc_dai *dai)
929 {
930         struct fsl_spdif_priv *spdif_private = snd_soc_dai_get_drvdata(dai);
931
932         snd_soc_dai_init_dma_data(dai, &spdif_private->dma_params_tx,
933                                   &spdif_private->dma_params_rx);
934
935         snd_soc_add_dai_controls(dai, fsl_spdif_ctrls, ARRAY_SIZE(fsl_spdif_ctrls));
936
937         return 0;
938 }
939
940 static struct snd_soc_dai_driver fsl_spdif_dai = {
941         .probe = &fsl_spdif_dai_probe,
942         .playback = {
943                 .channels_min = 2,
944                 .channels_max = 2,
945                 .rates = FSL_SPDIF_RATES_PLAYBACK,
946                 .formats = FSL_SPDIF_FORMATS_PLAYBACK,
947         },
948         .capture = {
949                 .channels_min = 2,
950                 .channels_max = 2,
951                 .rates = FSL_SPDIF_RATES_CAPTURE,
952                 .formats = FSL_SPDIF_FORMATS_CAPTURE,
953         },
954         .ops = &fsl_spdif_dai_ops,
955 };
956
957 static const struct snd_soc_component_driver fsl_spdif_component = {
958         .name           = "fsl-spdif",
959 };
960
961 /* FSL SPDIF REGMAP */
962
963 static bool fsl_spdif_readable_reg(struct device *dev, unsigned int reg)
964 {
965         switch (reg) {
966         case REG_SPDIF_SCR:
967         case REG_SPDIF_SRCD:
968         case REG_SPDIF_SRPC:
969         case REG_SPDIF_SIE:
970         case REG_SPDIF_SIS:
971         case REG_SPDIF_SRL:
972         case REG_SPDIF_SRR:
973         case REG_SPDIF_SRCSH:
974         case REG_SPDIF_SRCSL:
975         case REG_SPDIF_SRU:
976         case REG_SPDIF_SRQ:
977         case REG_SPDIF_STCSCH:
978         case REG_SPDIF_STCSCL:
979         case REG_SPDIF_SRFM:
980         case REG_SPDIF_STC:
981                 return true;
982         default:
983                 return false;
984         }
985 }
986
987 static bool fsl_spdif_writeable_reg(struct device *dev, unsigned int reg)
988 {
989         switch (reg) {
990         case REG_SPDIF_SCR:
991         case REG_SPDIF_SRCD:
992         case REG_SPDIF_SRPC:
993         case REG_SPDIF_SIE:
994         case REG_SPDIF_SIC:
995         case REG_SPDIF_STL:
996         case REG_SPDIF_STR:
997         case REG_SPDIF_STCSCH:
998         case REG_SPDIF_STCSCL:
999         case REG_SPDIF_STC:
1000                 return true;
1001         default:
1002                 return false;
1003         }
1004 }
1005
1006 static struct regmap_config fsl_spdif_regmap_config = {
1007         .reg_bits = 32,
1008         .reg_stride = 4,
1009         .val_bits = 32,
1010
1011         .max_register = REG_SPDIF_STC,
1012         .readable_reg = fsl_spdif_readable_reg,
1013         .writeable_reg = fsl_spdif_writeable_reg,
1014 };
1015
1016 static u32 fsl_spdif_txclk_caldiv(struct fsl_spdif_priv *spdif_priv,
1017                                 struct clk *clk, u64 savesub,
1018                                 enum spdif_txrate index, bool round)
1019 {
1020         const u32 rate[] = { 32000, 44100, 48000 };
1021         u64 rate_ideal, rate_actual, sub;
1022         u32 txclk_df, arate;
1023
1024         for (txclk_df = 1; txclk_df <= 128; txclk_df++) {
1025                 rate_ideal = rate[index] * (txclk_df + 1) * 64;
1026                 if (round)
1027                         rate_actual = clk_round_rate(clk, rate_ideal);
1028                 else
1029                         rate_actual = clk_get_rate(clk);
1030
1031                 arate = rate_actual / 64;
1032                 arate /= txclk_df;
1033
1034                 if (arate == rate[index]) {
1035                         /* We are lucky */
1036                         savesub = 0;
1037                         spdif_priv->txclk_df[index] = txclk_df;
1038                         break;
1039                 } else if (arate / rate[index] == 1) {
1040                         /* A little bigger than expect */
1041                         sub = (arate - rate[index]) * 100000;
1042                         do_div(sub, rate[index]);
1043                         if (sub < savesub) {
1044                                 savesub = sub;
1045                                 spdif_priv->txclk_df[index] = txclk_df;
1046                         }
1047                 } else if (rate[index] / arate == 1) {
1048                         /* A little smaller than expect */
1049                         sub = (rate[index] - arate) * 100000;
1050                         do_div(sub, rate[index]);
1051                         if (sub < savesub) {
1052                                 savesub = sub;
1053                                 spdif_priv->txclk_df[index] = txclk_df;
1054                         }
1055                 }
1056         }
1057
1058         return savesub;
1059 }
1060
1061 static int fsl_spdif_probe_txclk(struct fsl_spdif_priv *spdif_priv,
1062                                 enum spdif_txrate index)
1063 {
1064         const u32 rate[] = { 32000, 44100, 48000 };
1065         struct platform_device *pdev = spdif_priv->pdev;
1066         struct device *dev = &pdev->dev;
1067         u64 savesub = 100000, ret;
1068         struct clk *clk;
1069         char tmp[16];
1070         int i;
1071
1072         for (i = 0; i < STC_TXCLK_SRC_MAX; i++) {
1073                 sprintf(tmp, "rxtx%d", i);
1074                 clk = devm_clk_get(&pdev->dev, tmp);
1075                 if (IS_ERR(clk)) {
1076                         dev_err(dev, "no rxtx%d clock in devicetree\n", i);
1077                         return PTR_ERR(clk);
1078                 }
1079                 if (!clk_get_rate(clk))
1080                         continue;
1081
1082                 ret = fsl_spdif_txclk_caldiv(spdif_priv, clk, savesub, index,
1083                                              i == STC_TXCLK_SPDIF_ROOT);
1084                 if (savesub == ret)
1085                         continue;
1086
1087                 savesub = ret;
1088                 spdif_priv->txclk[index] = clk;
1089                 spdif_priv->txclk_src[index] = i;
1090
1091                 /* To quick catch a divisor, we allow a 0.1% deviation */
1092                 if (savesub < 100)
1093                         break;
1094         }
1095
1096         dev_dbg(&pdev->dev, "use rxtx%d as tx clock source for %dHz sample rate\n",
1097                         spdif_priv->txclk_src[index], rate[index]);
1098         dev_dbg(&pdev->dev, "use txclk df %d for %dHz sample rate\n",
1099                         spdif_priv->txclk_df[index], rate[index]);
1100
1101         return 0;
1102 }
1103
1104 static int fsl_spdif_probe(struct platform_device *pdev)
1105 {
1106         struct device_node *np = pdev->dev.of_node;
1107         struct fsl_spdif_priv *spdif_priv;
1108         struct spdif_mixer_control *ctrl;
1109         struct resource *res;
1110         void __iomem *regs;
1111         int irq, ret, i;
1112
1113         if (!np)
1114                 return -ENODEV;
1115
1116         spdif_priv = devm_kzalloc(&pdev->dev,
1117                         sizeof(struct fsl_spdif_priv) + strlen(np->name) + 1,
1118                         GFP_KERNEL);
1119         if (!spdif_priv)
1120                 return -ENOMEM;
1121
1122         strcpy(spdif_priv->name, np->name);
1123
1124         spdif_priv->pdev = pdev;
1125
1126         /* Initialize this copy of the CPU DAI driver structure */
1127         memcpy(&spdif_priv->cpu_dai_drv, &fsl_spdif_dai, sizeof(fsl_spdif_dai));
1128         spdif_priv->cpu_dai_drv.name = spdif_priv->name;
1129
1130         if (of_property_read_bool(np, "big-endian"))
1131                 fsl_spdif_regmap_config.val_format_endian = REGMAP_ENDIAN_BIG;
1132
1133         /* Get the addresses and IRQ */
1134         res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1135         regs = devm_ioremap_resource(&pdev->dev, res);
1136         if (IS_ERR(regs))
1137                 return PTR_ERR(regs);
1138
1139         spdif_priv->regmap = devm_regmap_init_mmio_clk(&pdev->dev,
1140                         "core", regs, &fsl_spdif_regmap_config);
1141         if (IS_ERR(spdif_priv->regmap)) {
1142                 dev_err(&pdev->dev, "regmap init failed\n");
1143                 return PTR_ERR(spdif_priv->regmap);
1144         }
1145
1146         irq = platform_get_irq(pdev, 0);
1147         if (irq < 0) {
1148                 dev_err(&pdev->dev, "no irq for node %s\n", np->full_name);
1149                 return irq;
1150         }
1151
1152         ret = devm_request_irq(&pdev->dev, irq, spdif_isr, 0,
1153                         spdif_priv->name, spdif_priv);
1154         if (ret) {
1155                 dev_err(&pdev->dev, "could not claim irq %u\n", irq);
1156                 return ret;
1157         }
1158
1159         /* Get system clock for rx clock rate calculation */
1160         spdif_priv->sysclk = devm_clk_get(&pdev->dev, "rxtx5");
1161         if (IS_ERR(spdif_priv->sysclk)) {
1162                 dev_err(&pdev->dev, "no sys clock (rxtx5) in devicetree\n");
1163                 return PTR_ERR(spdif_priv->sysclk);
1164         }
1165
1166         /* Get core clock for data register access via DMA */
1167         spdif_priv->coreclk = devm_clk_get(&pdev->dev, "core");
1168         if (IS_ERR(spdif_priv->coreclk)) {
1169                 dev_err(&pdev->dev, "no core clock in devicetree\n");
1170                 return PTR_ERR(spdif_priv->coreclk);
1171         }
1172
1173         /* Select clock source for rx/tx clock */
1174         spdif_priv->rxclk = devm_clk_get(&pdev->dev, "rxtx1");
1175         if (IS_ERR(spdif_priv->rxclk)) {
1176                 dev_err(&pdev->dev, "no rxtx1 clock in devicetree\n");
1177                 return PTR_ERR(spdif_priv->rxclk);
1178         }
1179         spdif_priv->rxclk_src = DEFAULT_RXCLK_SRC;
1180
1181         for (i = 0; i < SPDIF_TXRATE_MAX; i++) {
1182                 ret = fsl_spdif_probe_txclk(spdif_priv, i);
1183                 if (ret)
1184                         return ret;
1185         }
1186
1187         /* Initial spinlock for control data */
1188         ctrl = &spdif_priv->fsl_spdif_control;
1189         spin_lock_init(&ctrl->ctl_lock);
1190
1191         /* Init tx channel status default value */
1192         ctrl->ch_status[0] =
1193                 IEC958_AES0_CON_NOT_COPYRIGHT | IEC958_AES0_CON_EMPHASIS_5015;
1194         ctrl->ch_status[1] = IEC958_AES1_CON_DIGDIGCONV_ID;
1195         ctrl->ch_status[2] = 0x00;
1196         ctrl->ch_status[3] =
1197                 IEC958_AES3_CON_FS_44100 | IEC958_AES3_CON_CLOCK_1000PPM;
1198
1199         spdif_priv->dpll_locked = false;
1200
1201         spdif_priv->dma_params_tx.maxburst = FSL_SPDIF_TXFIFO_WML;
1202         spdif_priv->dma_params_rx.maxburst = FSL_SPDIF_RXFIFO_WML;
1203         spdif_priv->dma_params_tx.addr = res->start + REG_SPDIF_STL;
1204         spdif_priv->dma_params_rx.addr = res->start + REG_SPDIF_SRL;
1205
1206         /* Register with ASoC */
1207         dev_set_drvdata(&pdev->dev, spdif_priv);
1208
1209         ret = devm_snd_soc_register_component(&pdev->dev, &fsl_spdif_component,
1210                                               &spdif_priv->cpu_dai_drv, 1);
1211         if (ret) {
1212                 dev_err(&pdev->dev, "failed to register DAI: %d\n", ret);
1213                 return ret;
1214         }
1215
1216         ret = imx_pcm_dma_init(pdev);
1217         if (ret)
1218                 dev_err(&pdev->dev, "imx_pcm_dma_init failed: %d\n", ret);
1219
1220         return ret;
1221 }
1222
1223 static const struct of_device_id fsl_spdif_dt_ids[] = {
1224         { .compatible = "fsl,imx35-spdif", },
1225         {}
1226 };
1227 MODULE_DEVICE_TABLE(of, fsl_spdif_dt_ids);
1228
1229 static struct platform_driver fsl_spdif_driver = {
1230         .driver = {
1231                 .name = "fsl-spdif-dai",
1232                 .owner = THIS_MODULE,
1233                 .of_match_table = fsl_spdif_dt_ids,
1234         },
1235         .probe = fsl_spdif_probe,
1236 };
1237
1238 module_platform_driver(fsl_spdif_driver);
1239
1240 MODULE_AUTHOR("Freescale Semiconductor, Inc.");
1241 MODULE_DESCRIPTION("Freescale S/PDIF CPU DAI Driver");
1242 MODULE_LICENSE("GPL v2");
1243 MODULE_ALIAS("platform:fsl-spdif-dai");