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