2 * Freescale S/PDIF ALSA SoC Digital Audio Interface (DAI) driver
4 * Copyright (C) 2013 Freescale Semiconductor, Inc.
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
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.
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>
25 #include <sound/asoundef.h>
26 #include <sound/soc.h>
27 #include <sound/dmaengine_pcm.h>
29 #include "fsl_spdif.h"
32 #define FSL_SPDIF_TXFIFO_WML 0x8
33 #define FSL_SPDIF_RXFIFO_WML 0x8
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)
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
45 #define DEFAULT_RXCLK_SRC 1
48 * SPDIF control structure
49 * Defines channel status, subcode and Q sub
51 struct spdif_mixer_control {
52 /* spinlock to access control data */
55 /* IEC958 channel tx status bit */
56 unsigned char ch_status[4];
59 unsigned char subcode[2 * SPDIF_UBITS_SIZE];
61 /* Q subcode part of user bits */
62 unsigned char qsub[2 * SPDIF_QSUB_SIZE];
64 /* Buffer offset for U/Q */
68 /* Ready buffer index of the two buffers */
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;
78 u8 txclk_df[SPDIF_TXRATE_MAX];
79 u8 txclk_src[SPDIF_TXRATE_MAX];
81 struct clk *txclk[SPDIF_TXRATE_MAX];
85 struct snd_dmaengine_dai_dma_data dma_params_tx;
86 struct snd_dmaengine_dai_dma_data dma_params_rx;
88 /* The name space will be allocated dynamically */
93 /* DPLL locked and lock loss interrupt handler */
94 static void spdif_irq_dpll_lock(struct fsl_spdif_priv *spdif_priv)
96 struct regmap *regmap = spdif_priv->regmap;
97 struct platform_device *pdev = spdif_priv->pdev;
100 regmap_read(regmap, REG_SPDIF_SRPC, &locked);
101 locked &= SRPC_DPLL_LOCKED;
103 dev_dbg(&pdev->dev, "isr: Rx dpll %s \n",
104 locked ? "locked" : "loss lock");
106 spdif_priv->dpll_locked = locked ? true : false;
109 /* Receiver found illegal symbol interrupt handler */
110 static void spdif_irq_sym_error(struct fsl_spdif_priv *spdif_priv)
112 struct regmap *regmap = spdif_priv->regmap;
113 struct platform_device *pdev = spdif_priv->pdev;
115 dev_dbg(&pdev->dev, "isr: receiver found illegal symbol\n");
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);
123 /* U/Q Channel receive register full */
124 static void spdif_irq_uqrx_full(struct fsl_spdif_priv *spdif_priv, char name)
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;
134 size = SPDIF_UBITS_SIZE;
139 size = SPDIF_QSUB_SIZE;
143 dev_err(&pdev->dev, "unsupported channel name\n");
147 dev_dbg(&pdev->dev, "isr: %c Channel receive register full\n", name);
149 if (*pos >= size * 2) {
151 } else if (unlikely((*pos % size) + 3 > size)) {
152 dev_err(&pdev->dev, "User bit receivce buffer overflow\n");
156 regmap_read(regmap, reg, &val);
157 ctrl->subcode[*pos++] = val >> 16;
158 ctrl->subcode[*pos++] = val >> 8;
159 ctrl->subcode[*pos++] = val;
162 /* U/Q Channel sync found */
163 static void spdif_irq_uq_sync(struct fsl_spdif_priv *spdif_priv)
165 struct spdif_mixer_control *ctrl = &spdif_priv->fsl_spdif_control;
166 struct platform_device *pdev = spdif_priv->pdev;
168 dev_dbg(&pdev->dev, "isr: U/Q Channel sync found\n");
170 /* U/Q buffer reset */
174 /* Set ready to this buffer */
175 ctrl->ready_buf = (ctrl->qpos - 1) / SPDIF_QSUB_SIZE + 1;
178 /* U/Q Channel framing error */
179 static void spdif_irq_uq_err(struct fsl_spdif_priv *spdif_priv)
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;
186 dev_dbg(&pdev->dev, "isr: U/Q Channel framing error\n");
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);
192 /* Drop this U/Q buffer */
198 /* Get spdif interrupt status and clear the interrupt */
199 static u32 spdif_intr_status_clear(struct fsl_spdif_priv *spdif_priv)
201 struct regmap *regmap = spdif_priv->regmap;
204 regmap_read(regmap, REG_SPDIF_SIS, &val);
205 regmap_read(regmap, REG_SPDIF_SIE, &val2);
207 regmap_write(regmap, REG_SPDIF_SIC, val & val2);
212 static irqreturn_t spdif_isr(int irq, void *devid)
214 struct fsl_spdif_priv *spdif_priv = (struct fsl_spdif_priv *)devid;
215 struct platform_device *pdev = spdif_priv->pdev;
218 sis = spdif_intr_status_clear(spdif_priv);
220 if (sis & INT_DPLL_LOCKED)
221 spdif_irq_dpll_lock(spdif_priv);
223 if (sis & INT_TXFIFO_UNOV)
224 dev_dbg(&pdev->dev, "isr: Tx FIFO under/overrun\n");
226 if (sis & INT_TXFIFO_RESYNC)
227 dev_dbg(&pdev->dev, "isr: Tx FIFO resync\n");
230 dev_dbg(&pdev->dev, "isr: cstatus new\n");
232 if (sis & INT_VAL_NOGOOD)
233 dev_dbg(&pdev->dev, "isr: validity flag no good\n");
235 if (sis & INT_SYM_ERR)
236 spdif_irq_sym_error(spdif_priv);
238 if (sis & INT_BIT_ERR)
239 dev_dbg(&pdev->dev, "isr: receiver found parity bit error\n");
241 if (sis & INT_URX_FUL)
242 spdif_irq_uqrx_full(spdif_priv, 'U');
244 if (sis & INT_URX_OV)
245 dev_dbg(&pdev->dev, "isr: U Channel receive register overrun\n");
247 if (sis & INT_QRX_FUL)
248 spdif_irq_uqrx_full(spdif_priv, 'Q');
250 if (sis & INT_QRX_OV)
251 dev_dbg(&pdev->dev, "isr: Q Channel receive register overrun\n");
253 if (sis & INT_UQ_SYNC)
254 spdif_irq_uq_sync(spdif_priv);
256 if (sis & INT_UQ_ERR)
257 spdif_irq_uq_err(spdif_priv);
259 if (sis & INT_RXFIFO_UNOV)
260 dev_dbg(&pdev->dev, "isr: Rx FIFO under/overrun\n");
262 if (sis & INT_RXFIFO_RESYNC)
263 dev_dbg(&pdev->dev, "isr: Rx FIFO resync\n");
265 if (sis & INT_LOSS_LOCK)
266 spdif_irq_dpll_lock(spdif_priv);
268 /* FIXME: Write Tx FIFO to clear TxEm */
270 dev_dbg(&pdev->dev, "isr: Tx FIFO empty\n");
272 /* FIXME: Read Rx FIFO to clear RxFIFOFul */
273 if (sis & INT_RXFIFO_FUL)
274 dev_dbg(&pdev->dev, "isr: Rx FIFO full\n");
279 static int spdif_softreset(struct fsl_spdif_priv *spdif_priv)
281 struct regmap *regmap = spdif_priv->regmap;
282 u32 val, cycle = 1000;
284 regmap_write(regmap, REG_SPDIF_SCR, SCR_SOFT_RESET);
287 * RESET bit would be cleared after finishing its reset procedure,
288 * which typically lasts 8 cycles. 1000 cycles will keep it safe.
291 regmap_read(regmap, REG_SPDIF_SCR, &val);
292 } while ((val & SCR_SOFT_RESET) && cycle--);
300 static void spdif_set_cstatus(struct spdif_mixer_control *ctrl,
303 ctrl->ch_status[3] &= ~mask;
304 ctrl->ch_status[3] |= cstatus & mask;
307 static void spdif_write_channel_status(struct fsl_spdif_priv *spdif_priv)
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;
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);
319 dev_dbg(&pdev->dev, "STCSCH: 0x%06x\n", ch_status);
321 ch_status = bitrev8(ctrl->ch_status[3]) << 16;
322 regmap_write(regmap, REG_SPDIF_STCSCL, ch_status);
324 dev_dbg(&pdev->dev, "STCSCL: 0x%06x\n", ch_status);
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)
331 struct regmap *regmap = spdif_priv->regmap;
332 u8 clksrc = spdif_priv->rxclk_src;
334 if (clksrc >= SRPC_CLKSRC_MAX || gainsel >= GAINSEL_MULTI_MAX)
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));
344 static int spdif_set_sample_rate(struct snd_pcm_substream *substream,
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;
357 switch (sample_rate) {
359 rate = SPDIF_TXRATE_32000;
360 csfs = IEC958_AES3_CON_FS_32000;
363 rate = SPDIF_TXRATE_44100;
364 csfs = IEC958_AES3_CON_FS_44100;
367 rate = SPDIF_TXRATE_48000;
368 csfs = IEC958_AES3_CON_FS_48000;
371 dev_err(&pdev->dev, "unsupported sample rate %d\n", sample_rate);
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");
381 txclk_df = spdif_priv->txclk_df[rate];
383 dev_err(&pdev->dev, "the txclk_df can't be zero\n");
387 /* Don't mess up the clocks from other modules */
388 if (clk != STC_TXCLK_SPDIF_ROOT)
392 * The S/PDIF block needs a clock of 64 * fs * txclk_df.
393 * So request 64 * fs * (txclk_df + 1) to get rounded.
395 ret = clk_set_rate(spdif_priv->txclk[rate], 64 * sample_rate * (txclk_df + 1));
397 dev_err(&pdev->dev, "failed to set tx clock rate\n");
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]));
407 /* set fs field in consumer channel status */
408 spdif_set_cstatus(ctrl, IEC958_AES3_CON_FS, csfs);
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);
415 dev_dbg(&pdev->dev, "set sample rate to %d\n", sample_rate);
420 static int fsl_spdif_startup(struct snd_pcm_substream *substream,
421 struct snd_soc_dai *cpu_dai)
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;
430 /* Reset module and interrupts only for first initialization */
431 if (!cpu_dai->active) {
432 ret = clk_prepare_enable(spdif_priv->coreclk);
434 dev_err(&pdev->dev, "failed to enable core clock\n");
438 ret = spdif_softreset(spdif_priv);
440 dev_err(&pdev->dev, "failed to soft reset\n");
444 /* Disable all the interrupts */
445 regmap_update_bits(regmap, REG_SPDIF_SIE, 0xffffff, 0);
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 |
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]);
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);
463 regmap_update_bits(regmap, REG_SPDIF_SCR, mask, scr);
465 /* Power up SPDIF module */
466 regmap_update_bits(regmap, REG_SPDIF_SCR, SCR_LOW_POWER, 0);
471 clk_disable_unprepare(spdif_priv->coreclk);
476 static void fsl_spdif_shutdown(struct snd_pcm_substream *substream,
477 struct snd_soc_dai *cpu_dai)
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;
484 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
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]);
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);
497 regmap_update_bits(regmap, REG_SPDIF_SCR, mask, scr);
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);
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)
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);
519 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
520 ret = spdif_set_sample_rate(substream, sample_rate);
522 dev_err(&pdev->dev, "%s: set sample rate failed: %d\n",
523 __func__, sample_rate);
526 spdif_set_cstatus(ctrl, IEC958_AES3_CON_CLOCK,
527 IEC958_AES3_CON_CLOCK_1000PPM);
528 spdif_write_channel_status(spdif_priv);
530 /* Setup rx clock source */
531 ret = spdif_set_rx_clksrc(spdif_priv, SPDIF_DEFAULT_GAINSEL, 1);
537 static int fsl_spdif_trigger(struct snd_pcm_substream *substream,
538 int cmd, struct snd_soc_dai *dai)
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;;
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);
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);
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,
576 * FSL SPDIF IEC958 controller(mixer) functions
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
585 static int fsl_spdif_info(struct snd_kcontrol *kcontrol,
586 struct snd_ctl_elem_info *uinfo)
588 uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
594 static int fsl_spdif_pb_get(struct snd_kcontrol *kcontrol,
595 struct snd_ctl_elem_value *uvalue)
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;
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];
609 static int fsl_spdif_pb_put(struct snd_kcontrol *kcontrol,
610 struct snd_ctl_elem_value *uvalue)
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;
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];
621 spdif_write_channel_status(spdif_priv);
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)
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;
635 regmap_read(regmap, REG_SPDIF_SIS, &val);
636 if (!(val & INT_CNEW)) {
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;
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;
651 regmap_write(regmap, REG_SPDIF_SIC, INT_CNEW);
657 * Get User bits (subcode) from chip value which readed out
658 * in UChannel register.
660 static int fsl_spdif_subcode_get(struct snd_kcontrol *kcontrol,
661 struct snd_ctl_elem_value *ucontrol)
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;
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);
677 spin_unlock_irqrestore(&ctrl->ctl_lock, flags);
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)
686 uinfo->type = SNDRV_CTL_ELEM_TYPE_BYTES;
687 uinfo->count = SPDIF_QSUB_SIZE;
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)
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;
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);
710 spin_unlock_irqrestore(&ctrl->ctl_lock, flags);
715 /* Valid bit infomation */
716 static int fsl_spdif_vbit_info(struct snd_kcontrol *kcontrol,
717 struct snd_ctl_elem_info *uinfo)
719 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
721 uinfo->value.integer.min = 0;
722 uinfo->value.integer.max = 1;
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)
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;
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);
743 /* DPLL lock infomation */
744 static int fsl_spdif_rxrate_info(struct snd_kcontrol *kcontrol,
745 struct snd_ctl_elem_info *uinfo)
747 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
749 uinfo->value.integer.min = 16000;
750 uinfo->value.integer.max = 96000;
755 static u32 gainsel_multi[GAINSEL_MULTI_MAX] = {
756 24, 16, 12, 8, 6, 4, 3,
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)
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;
769 regmap_read(regmap, REG_SPDIF_SRFM, &freqmeas);
770 regmap_read(regmap, REG_SPDIF_SRPC, &phaseconf);
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);
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);
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);
787 return (int)tmpval64;
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
795 static int fsl_spdif_rxrate_get(struct snd_kcontrol *kcontrol,
796 struct snd_ctl_elem_value *ucontrol)
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);
802 if (spdif_priv->dpll_locked)
803 ucontrol->value.integer.value[0] = rate;
805 ucontrol->value.integer.value[0] = 0;
810 /* User bit sync mode info */
811 static int fsl_spdif_usync_info(struct snd_kcontrol *kcontrol,
812 struct snd_ctl_elem_info *uinfo)
814 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
816 uinfo->value.integer.min = 0;
817 uinfo->value.integer.max = 1;
823 * User bit sync mode:
824 * 1 CD User channel subcode
827 static int fsl_spdif_usync_get(struct snd_kcontrol *kcontrol,
828 struct snd_ctl_elem_value *ucontrol)
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;
835 regmap_read(regmap, REG_SPDIF_SRCD, &val);
836 ucontrol->value.integer.value[0] = (val & SRCD_CD_USER) != 0;
842 * User bit sync mode:
843 * 1 CD User channel subcode
846 static int fsl_spdif_usync_put(struct snd_kcontrol *kcontrol,
847 struct snd_ctl_elem_value *ucontrol)
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;
854 regmap_update_bits(regmap, REG_SPDIF_SRCD, SRCD_CD_USER, val);
859 /* FSL SPDIF IEC958 controller defines */
860 static struct snd_kcontrol_new fsl_spdif_ctrls[] = {
861 /* Status cchanel controller */
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,
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,
880 /* User bits controller */
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,
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,
897 /* Valid bit error controller */
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,
906 /* DPLL lock info get controller */
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,
915 /* User bit sync mode set/get controller */
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,
928 static int fsl_spdif_dai_probe(struct snd_soc_dai *dai)
930 struct fsl_spdif_priv *spdif_private = snd_soc_dai_get_drvdata(dai);
932 snd_soc_dai_init_dma_data(dai, &spdif_private->dma_params_tx,
933 &spdif_private->dma_params_rx);
935 snd_soc_add_dai_controls(dai, fsl_spdif_ctrls, ARRAY_SIZE(fsl_spdif_ctrls));
940 static struct snd_soc_dai_driver fsl_spdif_dai = {
941 .probe = &fsl_spdif_dai_probe,
945 .rates = FSL_SPDIF_RATES_PLAYBACK,
946 .formats = FSL_SPDIF_FORMATS_PLAYBACK,
951 .rates = FSL_SPDIF_RATES_CAPTURE,
952 .formats = FSL_SPDIF_FORMATS_CAPTURE,
954 .ops = &fsl_spdif_dai_ops,
957 static const struct snd_soc_component_driver fsl_spdif_component = {
961 /* FSL SPDIF REGMAP */
963 static bool fsl_spdif_readable_reg(struct device *dev, unsigned int reg)
973 case REG_SPDIF_SRCSH:
974 case REG_SPDIF_SRCSL:
977 case REG_SPDIF_STCSCH:
978 case REG_SPDIF_STCSCL:
987 static bool fsl_spdif_writeable_reg(struct device *dev, unsigned int reg)
997 case REG_SPDIF_STCSCH:
998 case REG_SPDIF_STCSCL:
1006 static struct regmap_config fsl_spdif_regmap_config = {
1011 .max_register = REG_SPDIF_STC,
1012 .readable_reg = fsl_spdif_readable_reg,
1013 .writeable_reg = fsl_spdif_writeable_reg,
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)
1020 const u32 rate[] = { 32000, 44100, 48000 };
1021 u64 rate_ideal, rate_actual, sub;
1022 u32 txclk_df, arate;
1024 for (txclk_df = 1; txclk_df <= 128; txclk_df++) {
1025 rate_ideal = rate[index] * (txclk_df + 1) * 64;
1027 rate_actual = clk_round_rate(clk, rate_ideal);
1029 rate_actual = clk_get_rate(clk);
1031 arate = rate_actual / 64;
1034 if (arate == rate[index]) {
1037 spdif_priv->txclk_df[index] = txclk_df;
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) {
1045 spdif_priv->txclk_df[index] = txclk_df;
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) {
1053 spdif_priv->txclk_df[index] = txclk_df;
1061 static int fsl_spdif_probe_txclk(struct fsl_spdif_priv *spdif_priv,
1062 enum spdif_txrate index)
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;
1072 for (i = 0; i < STC_TXCLK_SRC_MAX; i++) {
1073 sprintf(tmp, "rxtx%d", i);
1074 clk = devm_clk_get(&pdev->dev, tmp);
1076 dev_err(dev, "no rxtx%d clock in devicetree\n", i);
1077 return PTR_ERR(clk);
1079 if (!clk_get_rate(clk))
1082 ret = fsl_spdif_txclk_caldiv(spdif_priv, clk, savesub, index,
1083 i == STC_TXCLK_SPDIF_ROOT);
1088 spdif_priv->txclk[index] = clk;
1089 spdif_priv->txclk_src[index] = i;
1091 /* To quick catch a divisor, we allow a 0.1% deviation */
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]);
1104 static int fsl_spdif_probe(struct platform_device *pdev)
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;
1116 spdif_priv = devm_kzalloc(&pdev->dev,
1117 sizeof(struct fsl_spdif_priv) + strlen(np->name) + 1,
1122 strcpy(spdif_priv->name, np->name);
1124 spdif_priv->pdev = pdev;
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;
1130 if (of_property_read_bool(np, "big-endian"))
1131 fsl_spdif_regmap_config.val_format_endian = REGMAP_ENDIAN_BIG;
1133 /* Get the addresses and IRQ */
1134 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1135 regs = devm_ioremap_resource(&pdev->dev, res);
1137 return PTR_ERR(regs);
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);
1146 irq = platform_get_irq(pdev, 0);
1148 dev_err(&pdev->dev, "no irq for node %s\n", np->full_name);
1152 ret = devm_request_irq(&pdev->dev, irq, spdif_isr, 0,
1153 spdif_priv->name, spdif_priv);
1155 dev_err(&pdev->dev, "could not claim irq %u\n", irq);
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);
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);
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);
1179 spdif_priv->rxclk_src = DEFAULT_RXCLK_SRC;
1181 for (i = 0; i < SPDIF_TXRATE_MAX; i++) {
1182 ret = fsl_spdif_probe_txclk(spdif_priv, i);
1187 /* Initial spinlock for control data */
1188 ctrl = &spdif_priv->fsl_spdif_control;
1189 spin_lock_init(&ctrl->ctl_lock);
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;
1199 spdif_priv->dpll_locked = false;
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;
1206 /* Register with ASoC */
1207 dev_set_drvdata(&pdev->dev, spdif_priv);
1209 ret = devm_snd_soc_register_component(&pdev->dev, &fsl_spdif_component,
1210 &spdif_priv->cpu_dai_drv, 1);
1212 dev_err(&pdev->dev, "failed to register DAI: %d\n", ret);
1216 ret = imx_pcm_dma_init(pdev);
1218 dev_err(&pdev->dev, "imx_pcm_dma_init failed: %d\n", ret);
1223 static const struct of_device_id fsl_spdif_dt_ids[] = {
1224 { .compatible = "fsl,imx35-spdif", },
1227 MODULE_DEVICE_TABLE(of, fsl_spdif_dt_ids);
1229 static struct platform_driver fsl_spdif_driver = {
1231 .name = "fsl-spdif-dai",
1232 .owner = THIS_MODULE,
1233 .of_match_table = fsl_spdif_dt_ids,
1235 .probe = fsl_spdif_probe,
1238 module_platform_driver(fsl_spdif_driver);
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");