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 sysclk_df[SPDIF_TXRATE_MAX];
80 u8 txclk_src[SPDIF_TXRATE_MAX];
82 struct clk *txclk[SPDIF_TXRATE_MAX];
86 struct snd_dmaengine_dai_dma_data dma_params_tx;
87 struct snd_dmaengine_dai_dma_data dma_params_rx;
89 /* The name space will be allocated dynamically */
94 /* DPLL locked and lock loss interrupt handler */
95 static void spdif_irq_dpll_lock(struct fsl_spdif_priv *spdif_priv)
97 struct regmap *regmap = spdif_priv->regmap;
98 struct platform_device *pdev = spdif_priv->pdev;
101 regmap_read(regmap, REG_SPDIF_SRPC, &locked);
102 locked &= SRPC_DPLL_LOCKED;
104 dev_dbg(&pdev->dev, "isr: Rx dpll %s \n",
105 locked ? "locked" : "loss lock");
107 spdif_priv->dpll_locked = locked ? true : false;
110 /* Receiver found illegal symbol interrupt handler */
111 static void spdif_irq_sym_error(struct fsl_spdif_priv *spdif_priv)
113 struct regmap *regmap = spdif_priv->regmap;
114 struct platform_device *pdev = spdif_priv->pdev;
116 dev_dbg(&pdev->dev, "isr: receiver found illegal symbol\n");
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);
124 /* U/Q Channel receive register full */
125 static void spdif_irq_uqrx_full(struct fsl_spdif_priv *spdif_priv, char name)
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;
135 size = SPDIF_UBITS_SIZE;
140 size = SPDIF_QSUB_SIZE;
144 dev_err(&pdev->dev, "unsupported channel name\n");
148 dev_dbg(&pdev->dev, "isr: %c Channel receive register full\n", name);
150 if (*pos >= size * 2) {
152 } else if (unlikely((*pos % size) + 3 > size)) {
153 dev_err(&pdev->dev, "User bit receivce buffer overflow\n");
157 regmap_read(regmap, reg, &val);
158 ctrl->subcode[*pos++] = val >> 16;
159 ctrl->subcode[*pos++] = val >> 8;
160 ctrl->subcode[*pos++] = val;
163 /* U/Q Channel sync found */
164 static void spdif_irq_uq_sync(struct fsl_spdif_priv *spdif_priv)
166 struct spdif_mixer_control *ctrl = &spdif_priv->fsl_spdif_control;
167 struct platform_device *pdev = spdif_priv->pdev;
169 dev_dbg(&pdev->dev, "isr: U/Q Channel sync found\n");
171 /* U/Q buffer reset */
175 /* Set ready to this buffer */
176 ctrl->ready_buf = (ctrl->qpos - 1) / SPDIF_QSUB_SIZE + 1;
179 /* U/Q Channel framing error */
180 static void spdif_irq_uq_err(struct fsl_spdif_priv *spdif_priv)
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;
187 dev_dbg(&pdev->dev, "isr: U/Q Channel framing error\n");
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);
193 /* Drop this U/Q buffer */
199 /* Get spdif interrupt status and clear the interrupt */
200 static u32 spdif_intr_status_clear(struct fsl_spdif_priv *spdif_priv)
202 struct regmap *regmap = spdif_priv->regmap;
205 regmap_read(regmap, REG_SPDIF_SIS, &val);
206 regmap_read(regmap, REG_SPDIF_SIE, &val2);
208 regmap_write(regmap, REG_SPDIF_SIC, val & val2);
213 static irqreturn_t spdif_isr(int irq, void *devid)
215 struct fsl_spdif_priv *spdif_priv = (struct fsl_spdif_priv *)devid;
216 struct platform_device *pdev = spdif_priv->pdev;
219 sis = spdif_intr_status_clear(spdif_priv);
221 if (sis & INT_DPLL_LOCKED)
222 spdif_irq_dpll_lock(spdif_priv);
224 if (sis & INT_TXFIFO_UNOV)
225 dev_dbg(&pdev->dev, "isr: Tx FIFO under/overrun\n");
227 if (sis & INT_TXFIFO_RESYNC)
228 dev_dbg(&pdev->dev, "isr: Tx FIFO resync\n");
231 dev_dbg(&pdev->dev, "isr: cstatus new\n");
233 if (sis & INT_VAL_NOGOOD)
234 dev_dbg(&pdev->dev, "isr: validity flag no good\n");
236 if (sis & INT_SYM_ERR)
237 spdif_irq_sym_error(spdif_priv);
239 if (sis & INT_BIT_ERR)
240 dev_dbg(&pdev->dev, "isr: receiver found parity bit error\n");
242 if (sis & INT_URX_FUL)
243 spdif_irq_uqrx_full(spdif_priv, 'U');
245 if (sis & INT_URX_OV)
246 dev_dbg(&pdev->dev, "isr: U Channel receive register overrun\n");
248 if (sis & INT_QRX_FUL)
249 spdif_irq_uqrx_full(spdif_priv, 'Q');
251 if (sis & INT_QRX_OV)
252 dev_dbg(&pdev->dev, "isr: Q Channel receive register overrun\n");
254 if (sis & INT_UQ_SYNC)
255 spdif_irq_uq_sync(spdif_priv);
257 if (sis & INT_UQ_ERR)
258 spdif_irq_uq_err(spdif_priv);
260 if (sis & INT_RXFIFO_UNOV)
261 dev_dbg(&pdev->dev, "isr: Rx FIFO under/overrun\n");
263 if (sis & INT_RXFIFO_RESYNC)
264 dev_dbg(&pdev->dev, "isr: Rx FIFO resync\n");
266 if (sis & INT_LOSS_LOCK)
267 spdif_irq_dpll_lock(spdif_priv);
269 /* FIXME: Write Tx FIFO to clear TxEm */
271 dev_dbg(&pdev->dev, "isr: Tx FIFO empty\n");
273 /* FIXME: Read Rx FIFO to clear RxFIFOFul */
274 if (sis & INT_RXFIFO_FUL)
275 dev_dbg(&pdev->dev, "isr: Rx FIFO full\n");
280 static int spdif_softreset(struct fsl_spdif_priv *spdif_priv)
282 struct regmap *regmap = spdif_priv->regmap;
283 u32 val, cycle = 1000;
285 regmap_write(regmap, REG_SPDIF_SCR, SCR_SOFT_RESET);
288 * RESET bit would be cleared after finishing its reset procedure,
289 * which typically lasts 8 cycles. 1000 cycles will keep it safe.
292 regmap_read(regmap, REG_SPDIF_SCR, &val);
293 } while ((val & SCR_SOFT_RESET) && cycle--);
301 static void spdif_set_cstatus(struct spdif_mixer_control *ctrl,
304 ctrl->ch_status[3] &= ~mask;
305 ctrl->ch_status[3] |= cstatus & mask;
308 static void spdif_write_channel_status(struct fsl_spdif_priv *spdif_priv)
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;
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);
320 dev_dbg(&pdev->dev, "STCSCH: 0x%06x\n", ch_status);
322 ch_status = bitrev8(ctrl->ch_status[3]) << 16;
323 regmap_write(regmap, REG_SPDIF_STCSCL, ch_status);
325 dev_dbg(&pdev->dev, "STCSCL: 0x%06x\n", ch_status);
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)
332 struct regmap *regmap = spdif_priv->regmap;
333 u8 clksrc = spdif_priv->rxclk_src;
335 if (clksrc >= SRPC_CLKSRC_MAX || gainsel >= GAINSEL_MULTI_MAX)
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));
345 static int spdif_set_sample_rate(struct snd_pcm_substream *substream,
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;
355 u8 clk, txclk_df, sysclk_df;
358 switch (sample_rate) {
360 rate = SPDIF_TXRATE_32000;
361 csfs = IEC958_AES3_CON_FS_32000;
364 rate = SPDIF_TXRATE_44100;
365 csfs = IEC958_AES3_CON_FS_44100;
368 rate = SPDIF_TXRATE_48000;
369 csfs = IEC958_AES3_CON_FS_48000;
372 dev_err(&pdev->dev, "unsupported sample rate %d\n", sample_rate);
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");
382 txclk_df = spdif_priv->txclk_df[rate];
384 dev_err(&pdev->dev, "the txclk_df can't be zero\n");
388 sysclk_df = spdif_priv->sysclk_df[rate];
390 /* Don't mess up the clocks from other modules */
391 if (clk != STC_TXCLK_SPDIF_ROOT)
395 * The S/PDIF block needs a clock of 64 * fs * txclk_df.
396 * So request 64 * fs * (txclk_df + 1) to get rounded.
398 ret = clk_set_rate(spdif_priv->txclk[rate], 64 * sample_rate * (txclk_df + 1));
400 dev_err(&pdev->dev, "failed to set tx clock rate\n");
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]));
410 /* set fs field in consumer channel status */
411 spdif_set_cstatus(ctrl, IEC958_AES3_CON_FS, csfs);
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);
418 regmap_update_bits(regmap, REG_SPDIF_STC,
419 STC_SYSCLK_DF_MASK, STC_SYSCLK_DF(sysclk_df));
421 dev_dbg(&pdev->dev, "set sample rate to %d\n", sample_rate);
426 static int fsl_spdif_startup(struct snd_pcm_substream *substream,
427 struct snd_soc_dai *cpu_dai)
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;
436 /* Reset module and interrupts only for first initialization */
437 if (!cpu_dai->active) {
438 ret = clk_prepare_enable(spdif_priv->coreclk);
440 dev_err(&pdev->dev, "failed to enable core clock\n");
444 ret = spdif_softreset(spdif_priv);
446 dev_err(&pdev->dev, "failed to soft reset\n");
450 /* Disable all the interrupts */
451 regmap_update_bits(regmap, REG_SPDIF_SIE, 0xffffff, 0);
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 |
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]);
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);
469 regmap_update_bits(regmap, REG_SPDIF_SCR, mask, scr);
471 /* Power up SPDIF module */
472 regmap_update_bits(regmap, REG_SPDIF_SCR, SCR_LOW_POWER, 0);
477 clk_disable_unprepare(spdif_priv->coreclk);
482 static void fsl_spdif_shutdown(struct snd_pcm_substream *substream,
483 struct snd_soc_dai *cpu_dai)
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;
490 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
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]);
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);
503 regmap_update_bits(regmap, REG_SPDIF_SCR, mask, scr);
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);
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)
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);
525 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
526 ret = spdif_set_sample_rate(substream, sample_rate);
528 dev_err(&pdev->dev, "%s: set sample rate failed: %d\n",
529 __func__, sample_rate);
532 spdif_set_cstatus(ctrl, IEC958_AES3_CON_CLOCK,
533 IEC958_AES3_CON_CLOCK_1000PPM);
534 spdif_write_channel_status(spdif_priv);
536 /* Setup rx clock source */
537 ret = spdif_set_rx_clksrc(spdif_priv, SPDIF_DEFAULT_GAINSEL, 1);
543 static int fsl_spdif_trigger(struct snd_pcm_substream *substream,
544 int cmd, struct snd_soc_dai *dai)
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;;
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);
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);
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,
582 * FSL SPDIF IEC958 controller(mixer) functions
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
591 static int fsl_spdif_info(struct snd_kcontrol *kcontrol,
592 struct snd_ctl_elem_info *uinfo)
594 uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
600 static int fsl_spdif_pb_get(struct snd_kcontrol *kcontrol,
601 struct snd_ctl_elem_value *uvalue)
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;
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];
615 static int fsl_spdif_pb_put(struct snd_kcontrol *kcontrol,
616 struct snd_ctl_elem_value *uvalue)
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;
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];
627 spdif_write_channel_status(spdif_priv);
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)
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;
641 regmap_read(regmap, REG_SPDIF_SIS, &val);
642 if (!(val & INT_CNEW)) {
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;
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;
657 regmap_write(regmap, REG_SPDIF_SIC, INT_CNEW);
663 * Get User bits (subcode) from chip value which readed out
664 * in UChannel register.
666 static int fsl_spdif_subcode_get(struct snd_kcontrol *kcontrol,
667 struct snd_ctl_elem_value *ucontrol)
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;
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);
683 spin_unlock_irqrestore(&ctrl->ctl_lock, flags);
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)
692 uinfo->type = SNDRV_CTL_ELEM_TYPE_BYTES;
693 uinfo->count = SPDIF_QSUB_SIZE;
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)
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;
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);
716 spin_unlock_irqrestore(&ctrl->ctl_lock, flags);
721 /* Valid bit infomation */
722 static int fsl_spdif_vbit_info(struct snd_kcontrol *kcontrol,
723 struct snd_ctl_elem_info *uinfo)
725 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
727 uinfo->value.integer.min = 0;
728 uinfo->value.integer.max = 1;
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)
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;
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);
749 /* DPLL lock infomation */
750 static int fsl_spdif_rxrate_info(struct snd_kcontrol *kcontrol,
751 struct snd_ctl_elem_info *uinfo)
753 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
755 uinfo->value.integer.min = 16000;
756 uinfo->value.integer.max = 96000;
761 static u32 gainsel_multi[GAINSEL_MULTI_MAX] = {
762 24, 16, 12, 8, 6, 4, 3,
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)
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;
775 regmap_read(regmap, REG_SPDIF_SRFM, &freqmeas);
776 regmap_read(regmap, REG_SPDIF_SRPC, &phaseconf);
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);
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);
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);
793 return (int)tmpval64;
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
801 static int fsl_spdif_rxrate_get(struct snd_kcontrol *kcontrol,
802 struct snd_ctl_elem_value *ucontrol)
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);
808 if (spdif_priv->dpll_locked)
809 ucontrol->value.integer.value[0] = rate;
811 ucontrol->value.integer.value[0] = 0;
816 /* User bit sync mode info */
817 static int fsl_spdif_usync_info(struct snd_kcontrol *kcontrol,
818 struct snd_ctl_elem_info *uinfo)
820 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
822 uinfo->value.integer.min = 0;
823 uinfo->value.integer.max = 1;
829 * User bit sync mode:
830 * 1 CD User channel subcode
833 static int fsl_spdif_usync_get(struct snd_kcontrol *kcontrol,
834 struct snd_ctl_elem_value *ucontrol)
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;
841 regmap_read(regmap, REG_SPDIF_SRCD, &val);
842 ucontrol->value.integer.value[0] = (val & SRCD_CD_USER) != 0;
848 * User bit sync mode:
849 * 1 CD User channel subcode
852 static int fsl_spdif_usync_put(struct snd_kcontrol *kcontrol,
853 struct snd_ctl_elem_value *ucontrol)
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;
860 regmap_update_bits(regmap, REG_SPDIF_SRCD, SRCD_CD_USER, val);
865 /* FSL SPDIF IEC958 controller defines */
866 static struct snd_kcontrol_new fsl_spdif_ctrls[] = {
867 /* Status cchanel controller */
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,
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,
886 /* User bits controller */
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,
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,
903 /* Valid bit error controller */
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,
912 /* DPLL lock info get controller */
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,
921 /* User bit sync mode set/get controller */
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,
934 static int fsl_spdif_dai_probe(struct snd_soc_dai *dai)
936 struct fsl_spdif_priv *spdif_private = snd_soc_dai_get_drvdata(dai);
938 snd_soc_dai_init_dma_data(dai, &spdif_private->dma_params_tx,
939 &spdif_private->dma_params_rx);
941 snd_soc_add_dai_controls(dai, fsl_spdif_ctrls, ARRAY_SIZE(fsl_spdif_ctrls));
946 static struct snd_soc_dai_driver fsl_spdif_dai = {
947 .probe = &fsl_spdif_dai_probe,
951 .rates = FSL_SPDIF_RATES_PLAYBACK,
952 .formats = FSL_SPDIF_FORMATS_PLAYBACK,
957 .rates = FSL_SPDIF_RATES_CAPTURE,
958 .formats = FSL_SPDIF_FORMATS_CAPTURE,
960 .ops = &fsl_spdif_dai_ops,
963 static const struct snd_soc_component_driver fsl_spdif_component = {
967 /* FSL SPDIF REGMAP */
969 static bool fsl_spdif_readable_reg(struct device *dev, unsigned int reg)
979 case REG_SPDIF_SRCSH:
980 case REG_SPDIF_SRCSL:
983 case REG_SPDIF_STCSCH:
984 case REG_SPDIF_STCSCL:
993 static bool fsl_spdif_writeable_reg(struct device *dev, unsigned int reg)
1003 case REG_SPDIF_STCSCH:
1004 case REG_SPDIF_STCSCL:
1012 static struct regmap_config fsl_spdif_regmap_config = {
1017 .max_register = REG_SPDIF_STC,
1018 .readable_reg = fsl_spdif_readable_reg,
1019 .writeable_reg = fsl_spdif_writeable_reg,
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)
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;
1032 /* The sysclk has an extra divisor [2, 512] */
1033 sysclk_dfmin = is_sysclk ? 2 : 1;
1034 sysclk_dfmax = is_sysclk ? 512 : 1;
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;
1040 rate_actual = clk_round_rate(clk, rate_ideal);
1042 rate_actual = clk_get_rate(clk);
1044 arate = rate_actual / 64;
1045 arate /= txclk_df * sysclk_df;
1047 if (arate == rate[index]) {
1050 spdif_priv->txclk_df[index] = txclk_df;
1051 spdif_priv->sysclk_df[index] = sysclk_df;
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]);
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]);
1069 spdif_priv->txclk_df[index] = txclk_df;
1070 spdif_priv->sysclk_df[index] = sysclk_df;
1079 static int fsl_spdif_probe_txclk(struct fsl_spdif_priv *spdif_priv,
1080 enum spdif_txrate index)
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;
1090 for (i = 0; i < STC_TXCLK_SRC_MAX; i++) {
1091 sprintf(tmp, "rxtx%d", i);
1092 clk = devm_clk_get(&pdev->dev, tmp);
1094 dev_err(dev, "no rxtx%d clock in devicetree\n", i);
1095 return PTR_ERR(clk);
1097 if (!clk_get_rate(clk))
1100 ret = fsl_spdif_txclk_caldiv(spdif_priv, clk, savesub, index,
1101 i == STC_TXCLK_SPDIF_ROOT);
1106 spdif_priv->txclk[index] = clk;
1107 spdif_priv->txclk_src[index] = i;
1109 /* To quick catch a divisor, we allow a 0.1% deviation */
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]);
1125 static int fsl_spdif_probe(struct platform_device *pdev)
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;
1137 spdif_priv = devm_kzalloc(&pdev->dev,
1138 sizeof(struct fsl_spdif_priv) + strlen(np->name) + 1,
1143 strcpy(spdif_priv->name, np->name);
1145 spdif_priv->pdev = pdev;
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;
1151 if (of_property_read_bool(np, "big-endian"))
1152 fsl_spdif_regmap_config.val_format_endian = REGMAP_ENDIAN_BIG;
1154 /* Get the addresses and IRQ */
1155 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1156 regs = devm_ioremap_resource(&pdev->dev, res);
1158 return PTR_ERR(regs);
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);
1167 irq = platform_get_irq(pdev, 0);
1169 dev_err(&pdev->dev, "no irq for node %s\n", np->full_name);
1173 ret = devm_request_irq(&pdev->dev, irq, spdif_isr, 0,
1174 spdif_priv->name, spdif_priv);
1176 dev_err(&pdev->dev, "could not claim irq %u\n", irq);
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);
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);
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);
1200 spdif_priv->rxclk_src = DEFAULT_RXCLK_SRC;
1202 for (i = 0; i < SPDIF_TXRATE_MAX; i++) {
1203 ret = fsl_spdif_probe_txclk(spdif_priv, i);
1208 /* Initial spinlock for control data */
1209 ctrl = &spdif_priv->fsl_spdif_control;
1210 spin_lock_init(&ctrl->ctl_lock);
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;
1220 spdif_priv->dpll_locked = false;
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;
1227 /* Register with ASoC */
1228 dev_set_drvdata(&pdev->dev, spdif_priv);
1230 ret = devm_snd_soc_register_component(&pdev->dev, &fsl_spdif_component,
1231 &spdif_priv->cpu_dai_drv, 1);
1233 dev_err(&pdev->dev, "failed to register DAI: %d\n", ret);
1237 ret = imx_pcm_dma_init(pdev);
1239 dev_err(&pdev->dev, "imx_pcm_dma_init failed: %d\n", ret);
1244 static const struct of_device_id fsl_spdif_dt_ids[] = {
1245 { .compatible = "fsl,imx35-spdif", },
1248 MODULE_DEVICE_TABLE(of, fsl_spdif_dt_ids);
1250 static struct platform_driver fsl_spdif_driver = {
1252 .name = "fsl-spdif-dai",
1253 .owner = THIS_MODULE,
1254 .of_match_table = fsl_spdif_dt_ids,
1256 .probe = fsl_spdif_probe,
1259 module_platform_driver(fsl_spdif_driver);
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");