1 // SPDX-License-Identifier: GPL-2.0
3 // Freescale S/PDIF ALSA SoC Digital Audio Interface (DAI) driver
5 // Copyright (C) 2013 Freescale Semiconductor, Inc.
7 // Based on stmp3xxx_spdif_dai.c
8 // Vladimir Barinov <vbarinov@embeddedalley.com>
9 // Copyright 2008 SigmaTel, Inc
10 // Copyright 2008 Embedded Alley Solutions, Inc
12 #include <linux/bitrev.h>
13 #include <linux/clk.h>
14 #include <linux/module.h>
15 #include <linux/of_address.h>
16 #include <linux/of_device.h>
17 #include <linux/of_irq.h>
18 #include <linux/regmap.h>
19 #include <linux/pm_runtime.h>
21 #include <sound/asoundef.h>
22 #include <sound/dmaengine_pcm.h>
23 #include <sound/soc.h>
25 #include "fsl_spdif.h"
28 #define FSL_SPDIF_TXFIFO_WML 0x8
29 #define FSL_SPDIF_RXFIFO_WML 0x8
31 #define INTR_FOR_PLAYBACK (INT_TXFIFO_RESYNC)
32 #define INTR_FOR_CAPTURE (INT_SYM_ERR | INT_BIT_ERR | INT_URX_FUL |\
33 INT_URX_OV | INT_QRX_FUL | INT_QRX_OV |\
34 INT_UQ_SYNC | INT_UQ_ERR | INT_RXFIFO_RESYNC |\
35 INT_LOSS_LOCK | INT_DPLL_LOCKED)
37 #define SIE_INTR_FOR(tx) (tx ? INTR_FOR_PLAYBACK : INTR_FOR_CAPTURE)
39 /* Index list for the values that has if (DPLL Locked) condition */
40 static u8 srpc_dpll_locked[] = { 0x0, 0x1, 0x2, 0x3, 0x4, 0xa, 0xb };
41 #define SRPC_NODPLL_START1 0x5
42 #define SRPC_NODPLL_START2 0xc
44 #define DEFAULT_RXCLK_SRC 1
47 * struct fsl_spdif_soc_data: soc specific data
49 * @imx: for imx platform
50 * @shared_root_clock: flag of sharing a clock source with others;
51 * so the driver shouldn't set root clock rate
52 * @interrupts: interrupt number
53 * @tx_burst: tx maxburst size
54 * @rx_burst: rx maxburst size
55 * @tx_formats: tx supported data format
57 struct fsl_spdif_soc_data {
59 bool shared_root_clock;
67 * SPDIF control structure
68 * Defines channel status, subcode and Q sub
70 struct spdif_mixer_control {
71 /* spinlock to access control data */
74 /* IEC958 channel tx status bit */
75 unsigned char ch_status[4];
78 unsigned char subcode[2 * SPDIF_UBITS_SIZE];
80 /* Q subcode part of user bits */
81 unsigned char qsub[2 * SPDIF_QSUB_SIZE];
83 /* Buffer offset for U/Q */
87 /* Ready buffer index of the two buffers */
92 * struct fsl_spdif_priv - Freescale SPDIF private data
93 * @soc: SPDIF soc data
94 * @fsl_spdif_control: SPDIF control data
95 * @cpu_dai_drv: cpu dai driver
96 * @pdev: platform device pointer
97 * @regmap: regmap handler
98 * @dpll_locked: dpll lock flag
99 * @txrate: the best rates for playback
100 * @txclk_df: STC_TXCLK_DF dividers value for playback
101 * @sysclk_df: STC_SYSCLK_DF dividers value for playback
102 * @txclk_src: STC_TXCLK_SRC values for playback
103 * @rxclk_src: SRPC_CLKSRC_SEL values for capture
104 * @txclk: tx clock sources for playback
105 * @rxclk: rx clock sources for capture
106 * @coreclk: core clock for register access via DMA
107 * @sysclk: system clock for rx clock rate measurement
108 * @spbaclk: SPBA clock (optional, depending on SoC design)
109 * @dma_params_tx: DMA parameters for transmit channel
110 * @dma_params_rx: DMA parameters for receive channel
111 * @regcache_srpc: regcache for SRPC
113 struct fsl_spdif_priv {
114 const struct fsl_spdif_soc_data *soc;
115 struct spdif_mixer_control fsl_spdif_control;
116 struct snd_soc_dai_driver cpu_dai_drv;
117 struct platform_device *pdev;
118 struct regmap *regmap;
120 u32 txrate[SPDIF_TXRATE_MAX];
121 u8 txclk_df[SPDIF_TXRATE_MAX];
122 u16 sysclk_df[SPDIF_TXRATE_MAX];
123 u8 txclk_src[SPDIF_TXRATE_MAX];
125 struct clk *txclk[SPDIF_TXRATE_MAX];
130 struct snd_dmaengine_dai_dma_data dma_params_tx;
131 struct snd_dmaengine_dai_dma_data dma_params_rx;
132 /* regcache for SRPC */
136 static struct fsl_spdif_soc_data fsl_spdif_vf610 = {
138 .shared_root_clock = false,
140 .tx_burst = FSL_SPDIF_TXFIFO_WML,
141 .rx_burst = FSL_SPDIF_RXFIFO_WML,
142 .tx_formats = FSL_SPDIF_FORMATS_PLAYBACK,
145 static struct fsl_spdif_soc_data fsl_spdif_imx35 = {
147 .shared_root_clock = false,
149 .tx_burst = FSL_SPDIF_TXFIFO_WML,
150 .rx_burst = FSL_SPDIF_RXFIFO_WML,
151 .tx_formats = FSL_SPDIF_FORMATS_PLAYBACK,
154 static struct fsl_spdif_soc_data fsl_spdif_imx6sx = {
156 .shared_root_clock = true,
158 .tx_burst = FSL_SPDIF_TXFIFO_WML,
159 .rx_burst = FSL_SPDIF_RXFIFO_WML,
160 .tx_formats = FSL_SPDIF_FORMATS_PLAYBACK,
164 static struct fsl_spdif_soc_data fsl_spdif_imx8qm = {
166 .shared_root_clock = true,
168 .tx_burst = 2, /* Applied for EDMA */
169 .rx_burst = 2, /* Applied for EDMA */
170 .tx_formats = SNDRV_PCM_FMTBIT_S24_LE, /* Applied for EDMA */
173 /* Check if clk is a root clock that does not share clock source with others */
174 static inline bool fsl_spdif_can_set_clk_rate(struct fsl_spdif_priv *spdif, int clk)
176 return (clk == STC_TXCLK_SPDIF_ROOT) && !spdif->soc->shared_root_clock;
179 /* DPLL locked and lock loss interrupt handler */
180 static void spdif_irq_dpll_lock(struct fsl_spdif_priv *spdif_priv)
182 struct regmap *regmap = spdif_priv->regmap;
183 struct platform_device *pdev = spdif_priv->pdev;
186 regmap_read(regmap, REG_SPDIF_SRPC, &locked);
187 locked &= SRPC_DPLL_LOCKED;
189 dev_dbg(&pdev->dev, "isr: Rx dpll %s \n",
190 locked ? "locked" : "loss lock");
192 spdif_priv->dpll_locked = locked ? true : false;
195 /* Receiver found illegal symbol interrupt handler */
196 static void spdif_irq_sym_error(struct fsl_spdif_priv *spdif_priv)
198 struct regmap *regmap = spdif_priv->regmap;
199 struct platform_device *pdev = spdif_priv->pdev;
201 dev_dbg(&pdev->dev, "isr: receiver found illegal symbol\n");
203 /* Clear illegal symbol if DPLL unlocked since no audio stream */
204 if (!spdif_priv->dpll_locked)
205 regmap_update_bits(regmap, REG_SPDIF_SIE, INT_SYM_ERR, 0);
208 /* U/Q Channel receive register full */
209 static void spdif_irq_uqrx_full(struct fsl_spdif_priv *spdif_priv, char name)
211 struct spdif_mixer_control *ctrl = &spdif_priv->fsl_spdif_control;
212 struct regmap *regmap = spdif_priv->regmap;
213 struct platform_device *pdev = spdif_priv->pdev;
214 u32 *pos, size, val, reg;
219 size = SPDIF_UBITS_SIZE;
224 size = SPDIF_QSUB_SIZE;
228 dev_err(&pdev->dev, "unsupported channel name\n");
232 dev_dbg(&pdev->dev, "isr: %c Channel receive register full\n", name);
234 if (*pos >= size * 2) {
236 } else if (unlikely((*pos % size) + 3 > size)) {
237 dev_err(&pdev->dev, "User bit receive buffer overflow\n");
241 regmap_read(regmap, reg, &val);
242 ctrl->subcode[*pos++] = val >> 16;
243 ctrl->subcode[*pos++] = val >> 8;
244 ctrl->subcode[*pos++] = val;
247 /* U/Q Channel sync found */
248 static void spdif_irq_uq_sync(struct fsl_spdif_priv *spdif_priv)
250 struct spdif_mixer_control *ctrl = &spdif_priv->fsl_spdif_control;
251 struct platform_device *pdev = spdif_priv->pdev;
253 dev_dbg(&pdev->dev, "isr: U/Q Channel sync found\n");
255 /* U/Q buffer reset */
259 /* Set ready to this buffer */
260 ctrl->ready_buf = (ctrl->qpos - 1) / SPDIF_QSUB_SIZE + 1;
263 /* U/Q Channel framing error */
264 static void spdif_irq_uq_err(struct fsl_spdif_priv *spdif_priv)
266 struct spdif_mixer_control *ctrl = &spdif_priv->fsl_spdif_control;
267 struct regmap *regmap = spdif_priv->regmap;
268 struct platform_device *pdev = spdif_priv->pdev;
271 dev_dbg(&pdev->dev, "isr: U/Q Channel framing error\n");
273 /* Read U/Q data to clear the irq and do buffer reset */
274 regmap_read(regmap, REG_SPDIF_SRU, &val);
275 regmap_read(regmap, REG_SPDIF_SRQ, &val);
277 /* Drop this U/Q buffer */
283 /* Get spdif interrupt status and clear the interrupt */
284 static u32 spdif_intr_status_clear(struct fsl_spdif_priv *spdif_priv)
286 struct regmap *regmap = spdif_priv->regmap;
289 regmap_read(regmap, REG_SPDIF_SIS, &val);
290 regmap_read(regmap, REG_SPDIF_SIE, &val2);
292 regmap_write(regmap, REG_SPDIF_SIC, val & val2);
297 static irqreturn_t spdif_isr(int irq, void *devid)
299 struct fsl_spdif_priv *spdif_priv = (struct fsl_spdif_priv *)devid;
300 struct platform_device *pdev = spdif_priv->pdev;
303 sis = spdif_intr_status_clear(spdif_priv);
305 if (sis & INT_DPLL_LOCKED)
306 spdif_irq_dpll_lock(spdif_priv);
308 if (sis & INT_TXFIFO_UNOV)
309 dev_dbg(&pdev->dev, "isr: Tx FIFO under/overrun\n");
311 if (sis & INT_TXFIFO_RESYNC)
312 dev_dbg(&pdev->dev, "isr: Tx FIFO resync\n");
315 dev_dbg(&pdev->dev, "isr: cstatus new\n");
317 if (sis & INT_VAL_NOGOOD)
318 dev_dbg(&pdev->dev, "isr: validity flag no good\n");
320 if (sis & INT_SYM_ERR)
321 spdif_irq_sym_error(spdif_priv);
323 if (sis & INT_BIT_ERR)
324 dev_dbg(&pdev->dev, "isr: receiver found parity bit error\n");
326 if (sis & INT_URX_FUL)
327 spdif_irq_uqrx_full(spdif_priv, 'U');
329 if (sis & INT_URX_OV)
330 dev_dbg(&pdev->dev, "isr: U Channel receive register overrun\n");
332 if (sis & INT_QRX_FUL)
333 spdif_irq_uqrx_full(spdif_priv, 'Q');
335 if (sis & INT_QRX_OV)
336 dev_dbg(&pdev->dev, "isr: Q Channel receive register overrun\n");
338 if (sis & INT_UQ_SYNC)
339 spdif_irq_uq_sync(spdif_priv);
341 if (sis & INT_UQ_ERR)
342 spdif_irq_uq_err(spdif_priv);
344 if (sis & INT_RXFIFO_UNOV)
345 dev_dbg(&pdev->dev, "isr: Rx FIFO under/overrun\n");
347 if (sis & INT_RXFIFO_RESYNC)
348 dev_dbg(&pdev->dev, "isr: Rx FIFO resync\n");
350 if (sis & INT_LOSS_LOCK)
351 spdif_irq_dpll_lock(spdif_priv);
353 /* FIXME: Write Tx FIFO to clear TxEm */
355 dev_dbg(&pdev->dev, "isr: Tx FIFO empty\n");
357 /* FIXME: Read Rx FIFO to clear RxFIFOFul */
358 if (sis & INT_RXFIFO_FUL)
359 dev_dbg(&pdev->dev, "isr: Rx FIFO full\n");
364 static int spdif_softreset(struct fsl_spdif_priv *spdif_priv)
366 struct regmap *regmap = spdif_priv->regmap;
367 u32 val, cycle = 1000;
369 regcache_cache_bypass(regmap, true);
371 regmap_write(regmap, REG_SPDIF_SCR, SCR_SOFT_RESET);
374 * RESET bit would be cleared after finishing its reset procedure,
375 * which typically lasts 8 cycles. 1000 cycles will keep it safe.
378 regmap_read(regmap, REG_SPDIF_SCR, &val);
379 } while ((val & SCR_SOFT_RESET) && cycle--);
381 regcache_cache_bypass(regmap, false);
382 regcache_mark_dirty(regmap);
383 regcache_sync(regmap);
391 static void spdif_set_cstatus(struct spdif_mixer_control *ctrl,
394 ctrl->ch_status[3] &= ~mask;
395 ctrl->ch_status[3] |= cstatus & mask;
398 static void spdif_write_channel_status(struct fsl_spdif_priv *spdif_priv)
400 struct spdif_mixer_control *ctrl = &spdif_priv->fsl_spdif_control;
401 struct regmap *regmap = spdif_priv->regmap;
402 struct platform_device *pdev = spdif_priv->pdev;
405 ch_status = (bitrev8(ctrl->ch_status[0]) << 16) |
406 (bitrev8(ctrl->ch_status[1]) << 8) |
407 bitrev8(ctrl->ch_status[2]);
408 regmap_write(regmap, REG_SPDIF_STCSCH, ch_status);
410 dev_dbg(&pdev->dev, "STCSCH: 0x%06x\n", ch_status);
412 ch_status = bitrev8(ctrl->ch_status[3]) << 16;
413 regmap_write(regmap, REG_SPDIF_STCSCL, ch_status);
415 dev_dbg(&pdev->dev, "STCSCL: 0x%06x\n", ch_status);
418 /* Set SPDIF PhaseConfig register for rx clock */
419 static int spdif_set_rx_clksrc(struct fsl_spdif_priv *spdif_priv,
420 enum spdif_gainsel gainsel, int dpll_locked)
422 struct regmap *regmap = spdif_priv->regmap;
423 u8 clksrc = spdif_priv->rxclk_src;
425 if (clksrc >= SRPC_CLKSRC_MAX || gainsel >= GAINSEL_MULTI_MAX)
428 regmap_update_bits(regmap, REG_SPDIF_SRPC,
429 SRPC_CLKSRC_SEL_MASK | SRPC_GAINSEL_MASK,
430 SRPC_CLKSRC_SEL_SET(clksrc) | SRPC_GAINSEL_SET(gainsel));
435 static int spdif_set_sample_rate(struct snd_pcm_substream *substream,
438 struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
439 struct fsl_spdif_priv *spdif_priv = snd_soc_dai_get_drvdata(asoc_rtd_to_cpu(rtd, 0));
440 struct spdif_mixer_control *ctrl = &spdif_priv->fsl_spdif_control;
441 struct regmap *regmap = spdif_priv->regmap;
442 struct platform_device *pdev = spdif_priv->pdev;
443 unsigned long csfs = 0;
449 switch (sample_rate) {
451 rate = SPDIF_TXRATE_32000;
452 csfs = IEC958_AES3_CON_FS_32000;
455 rate = SPDIF_TXRATE_44100;
456 csfs = IEC958_AES3_CON_FS_44100;
459 rate = SPDIF_TXRATE_48000;
460 csfs = IEC958_AES3_CON_FS_48000;
463 rate = SPDIF_TXRATE_88200;
464 csfs = IEC958_AES3_CON_FS_88200;
467 rate = SPDIF_TXRATE_96000;
468 csfs = IEC958_AES3_CON_FS_96000;
471 rate = SPDIF_TXRATE_176400;
472 csfs = IEC958_AES3_CON_FS_176400;
475 rate = SPDIF_TXRATE_192000;
476 csfs = IEC958_AES3_CON_FS_192000;
479 dev_err(&pdev->dev, "unsupported sample rate %d\n", sample_rate);
483 clk = spdif_priv->txclk_src[rate];
484 if (clk >= STC_TXCLK_SRC_MAX) {
485 dev_err(&pdev->dev, "tx clock source is out of range\n");
489 txclk_df = spdif_priv->txclk_df[rate];
491 dev_err(&pdev->dev, "the txclk_df can't be zero\n");
495 sysclk_df = spdif_priv->sysclk_df[rate];
497 if (!fsl_spdif_can_set_clk_rate(spdif_priv, clk))
500 /* The S/PDIF block needs a clock of 64 * fs * txclk_df */
501 ret = clk_set_rate(spdif_priv->txclk[rate],
502 64 * sample_rate * txclk_df);
504 dev_err(&pdev->dev, "failed to set tx clock rate\n");
509 dev_dbg(&pdev->dev, "expected clock rate = %d\n",
510 (64 * sample_rate * txclk_df * sysclk_df));
511 dev_dbg(&pdev->dev, "actual clock rate = %ld\n",
512 clk_get_rate(spdif_priv->txclk[rate]));
514 /* set fs field in consumer channel status */
515 spdif_set_cstatus(ctrl, IEC958_AES3_CON_FS, csfs);
517 /* select clock source and divisor */
518 stc = STC_TXCLK_ALL_EN | STC_TXCLK_SRC_SET(clk) |
519 STC_TXCLK_DF(txclk_df) | STC_SYSCLK_DF(sysclk_df);
520 mask = STC_TXCLK_ALL_EN_MASK | STC_TXCLK_SRC_MASK |
521 STC_TXCLK_DF_MASK | STC_SYSCLK_DF_MASK;
522 regmap_update_bits(regmap, REG_SPDIF_STC, mask, stc);
524 dev_dbg(&pdev->dev, "set sample rate to %dHz for %dHz playback\n",
525 spdif_priv->txrate[rate], sample_rate);
530 static int fsl_spdif_startup(struct snd_pcm_substream *substream,
531 struct snd_soc_dai *cpu_dai)
533 struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
534 struct fsl_spdif_priv *spdif_priv = snd_soc_dai_get_drvdata(asoc_rtd_to_cpu(rtd, 0));
535 struct platform_device *pdev = spdif_priv->pdev;
536 struct regmap *regmap = spdif_priv->regmap;
540 /* Reset module and interrupts only for first initialization */
541 if (!snd_soc_dai_active(cpu_dai)) {
542 ret = spdif_softreset(spdif_priv);
544 dev_err(&pdev->dev, "failed to soft reset\n");
548 /* Disable all the interrupts */
549 regmap_update_bits(regmap, REG_SPDIF_SIE, 0xffffff, 0);
552 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
553 scr = SCR_TXFIFO_AUTOSYNC | SCR_TXFIFO_CTRL_NORMAL |
554 SCR_TXSEL_NORMAL | SCR_USRC_SEL_CHIP |
556 mask = SCR_TXFIFO_AUTOSYNC_MASK | SCR_TXFIFO_CTRL_MASK |
557 SCR_TXSEL_MASK | SCR_USRC_SEL_MASK |
558 SCR_TXFIFO_FSEL_MASK;
560 scr = SCR_RXFIFO_FSEL_IF8 | SCR_RXFIFO_AUTOSYNC;
561 mask = SCR_RXFIFO_FSEL_MASK | SCR_RXFIFO_AUTOSYNC_MASK|
562 SCR_RXFIFO_CTL_MASK | SCR_RXFIFO_OFF_MASK;
564 regmap_update_bits(regmap, REG_SPDIF_SCR, mask, scr);
566 /* Power up SPDIF module */
567 regmap_update_bits(regmap, REG_SPDIF_SCR, SCR_LOW_POWER, 0);
572 static void fsl_spdif_shutdown(struct snd_pcm_substream *substream,
573 struct snd_soc_dai *cpu_dai)
575 struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
576 struct fsl_spdif_priv *spdif_priv = snd_soc_dai_get_drvdata(asoc_rtd_to_cpu(rtd, 0));
577 struct regmap *regmap = spdif_priv->regmap;
580 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
582 mask = SCR_TXFIFO_AUTOSYNC_MASK | SCR_TXFIFO_CTRL_MASK |
583 SCR_TXSEL_MASK | SCR_USRC_SEL_MASK |
584 SCR_TXFIFO_FSEL_MASK;
586 scr = SCR_RXFIFO_OFF | SCR_RXFIFO_CTL_ZERO;
587 mask = SCR_RXFIFO_FSEL_MASK | SCR_RXFIFO_AUTOSYNC_MASK|
588 SCR_RXFIFO_CTL_MASK | SCR_RXFIFO_OFF_MASK;
590 regmap_update_bits(regmap, REG_SPDIF_SCR, mask, scr);
592 /* Power down SPDIF module only if tx&rx are both inactive */
593 if (!snd_soc_dai_active(cpu_dai)) {
594 spdif_intr_status_clear(spdif_priv);
595 regmap_update_bits(regmap, REG_SPDIF_SCR,
596 SCR_LOW_POWER, SCR_LOW_POWER);
600 static int fsl_spdif_hw_params(struct snd_pcm_substream *substream,
601 struct snd_pcm_hw_params *params,
602 struct snd_soc_dai *dai)
604 struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
605 struct fsl_spdif_priv *spdif_priv = snd_soc_dai_get_drvdata(asoc_rtd_to_cpu(rtd, 0));
606 struct spdif_mixer_control *ctrl = &spdif_priv->fsl_spdif_control;
607 struct platform_device *pdev = spdif_priv->pdev;
608 u32 sample_rate = params_rate(params);
611 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
612 ret = spdif_set_sample_rate(substream, sample_rate);
614 dev_err(&pdev->dev, "%s: set sample rate failed: %d\n",
615 __func__, sample_rate);
618 spdif_set_cstatus(ctrl, IEC958_AES3_CON_CLOCK,
619 IEC958_AES3_CON_CLOCK_1000PPM);
620 spdif_write_channel_status(spdif_priv);
622 /* Setup rx clock source */
623 ret = spdif_set_rx_clksrc(spdif_priv, SPDIF_DEFAULT_GAINSEL, 1);
629 static int fsl_spdif_trigger(struct snd_pcm_substream *substream,
630 int cmd, struct snd_soc_dai *dai)
632 struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
633 struct fsl_spdif_priv *spdif_priv = snd_soc_dai_get_drvdata(asoc_rtd_to_cpu(rtd, 0));
634 struct regmap *regmap = spdif_priv->regmap;
635 bool tx = substream->stream == SNDRV_PCM_STREAM_PLAYBACK;
636 u32 intr = SIE_INTR_FOR(tx);
637 u32 dmaen = SCR_DMA_xX_EN(tx);
640 case SNDRV_PCM_TRIGGER_START:
641 case SNDRV_PCM_TRIGGER_RESUME:
642 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
643 regmap_update_bits(regmap, REG_SPDIF_SIE, intr, intr);
644 regmap_update_bits(regmap, REG_SPDIF_SCR, dmaen, dmaen);
646 case SNDRV_PCM_TRIGGER_STOP:
647 case SNDRV_PCM_TRIGGER_SUSPEND:
648 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
649 regmap_update_bits(regmap, REG_SPDIF_SCR, dmaen, 0);
650 regmap_update_bits(regmap, REG_SPDIF_SIE, intr, 0);
659 static const struct snd_soc_dai_ops fsl_spdif_dai_ops = {
660 .startup = fsl_spdif_startup,
661 .hw_params = fsl_spdif_hw_params,
662 .trigger = fsl_spdif_trigger,
663 .shutdown = fsl_spdif_shutdown,
668 * FSL SPDIF IEC958 controller(mixer) functions
670 * Channel status get/put control
671 * User bit value get/put control
672 * Valid bit value get control
673 * DPLL lock status get control
674 * User bit sync mode selection control
677 static int fsl_spdif_info(struct snd_kcontrol *kcontrol,
678 struct snd_ctl_elem_info *uinfo)
680 uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
686 static int fsl_spdif_pb_get(struct snd_kcontrol *kcontrol,
687 struct snd_ctl_elem_value *uvalue)
689 struct snd_soc_dai *cpu_dai = snd_kcontrol_chip(kcontrol);
690 struct fsl_spdif_priv *spdif_priv = snd_soc_dai_get_drvdata(cpu_dai);
691 struct spdif_mixer_control *ctrl = &spdif_priv->fsl_spdif_control;
693 uvalue->value.iec958.status[0] = ctrl->ch_status[0];
694 uvalue->value.iec958.status[1] = ctrl->ch_status[1];
695 uvalue->value.iec958.status[2] = ctrl->ch_status[2];
696 uvalue->value.iec958.status[3] = ctrl->ch_status[3];
701 static int fsl_spdif_pb_put(struct snd_kcontrol *kcontrol,
702 struct snd_ctl_elem_value *uvalue)
704 struct snd_soc_dai *cpu_dai = snd_kcontrol_chip(kcontrol);
705 struct fsl_spdif_priv *spdif_priv = snd_soc_dai_get_drvdata(cpu_dai);
706 struct spdif_mixer_control *ctrl = &spdif_priv->fsl_spdif_control;
708 ctrl->ch_status[0] = uvalue->value.iec958.status[0];
709 ctrl->ch_status[1] = uvalue->value.iec958.status[1];
710 ctrl->ch_status[2] = uvalue->value.iec958.status[2];
711 ctrl->ch_status[3] = uvalue->value.iec958.status[3];
713 spdif_write_channel_status(spdif_priv);
718 /* Get channel status from SPDIF_RX_CCHAN register */
719 static int fsl_spdif_capture_get(struct snd_kcontrol *kcontrol,
720 struct snd_ctl_elem_value *ucontrol)
722 struct snd_soc_dai *cpu_dai = snd_kcontrol_chip(kcontrol);
723 struct fsl_spdif_priv *spdif_priv = snd_soc_dai_get_drvdata(cpu_dai);
724 struct regmap *regmap = spdif_priv->regmap;
727 regmap_read(regmap, REG_SPDIF_SIS, &val);
728 if (!(val & INT_CNEW))
731 regmap_read(regmap, REG_SPDIF_SRCSH, &cstatus);
732 ucontrol->value.iec958.status[0] = (cstatus >> 16) & 0xFF;
733 ucontrol->value.iec958.status[1] = (cstatus >> 8) & 0xFF;
734 ucontrol->value.iec958.status[2] = cstatus & 0xFF;
736 regmap_read(regmap, REG_SPDIF_SRCSL, &cstatus);
737 ucontrol->value.iec958.status[3] = (cstatus >> 16) & 0xFF;
738 ucontrol->value.iec958.status[4] = (cstatus >> 8) & 0xFF;
739 ucontrol->value.iec958.status[5] = cstatus & 0xFF;
742 regmap_write(regmap, REG_SPDIF_SIC, INT_CNEW);
748 * Get User bits (subcode) from chip value which readed out
749 * in UChannel register.
751 static int fsl_spdif_subcode_get(struct snd_kcontrol *kcontrol,
752 struct snd_ctl_elem_value *ucontrol)
754 struct snd_soc_dai *cpu_dai = snd_kcontrol_chip(kcontrol);
755 struct fsl_spdif_priv *spdif_priv = snd_soc_dai_get_drvdata(cpu_dai);
756 struct spdif_mixer_control *ctrl = &spdif_priv->fsl_spdif_control;
760 spin_lock_irqsave(&ctrl->ctl_lock, flags);
761 if (ctrl->ready_buf) {
762 int idx = (ctrl->ready_buf - 1) * SPDIF_UBITS_SIZE;
763 memcpy(&ucontrol->value.iec958.subcode[0],
764 &ctrl->subcode[idx], SPDIF_UBITS_SIZE);
767 spin_unlock_irqrestore(&ctrl->ctl_lock, flags);
772 /* Q-subcode information. The byte size is SPDIF_UBITS_SIZE/8 */
773 static int fsl_spdif_qinfo(struct snd_kcontrol *kcontrol,
774 struct snd_ctl_elem_info *uinfo)
776 uinfo->type = SNDRV_CTL_ELEM_TYPE_BYTES;
777 uinfo->count = SPDIF_QSUB_SIZE;
782 /* Get Q subcode from chip value which readed out in QChannel register */
783 static int fsl_spdif_qget(struct snd_kcontrol *kcontrol,
784 struct snd_ctl_elem_value *ucontrol)
786 struct snd_soc_dai *cpu_dai = snd_kcontrol_chip(kcontrol);
787 struct fsl_spdif_priv *spdif_priv = snd_soc_dai_get_drvdata(cpu_dai);
788 struct spdif_mixer_control *ctrl = &spdif_priv->fsl_spdif_control;
792 spin_lock_irqsave(&ctrl->ctl_lock, flags);
793 if (ctrl->ready_buf) {
794 int idx = (ctrl->ready_buf - 1) * SPDIF_QSUB_SIZE;
795 memcpy(&ucontrol->value.bytes.data[0],
796 &ctrl->qsub[idx], SPDIF_QSUB_SIZE);
799 spin_unlock_irqrestore(&ctrl->ctl_lock, flags);
804 /* Valid bit information */
805 static int fsl_spdif_vbit_info(struct snd_kcontrol *kcontrol,
806 struct snd_ctl_elem_info *uinfo)
808 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
810 uinfo->value.integer.min = 0;
811 uinfo->value.integer.max = 1;
816 /* Get valid good bit from interrupt status register */
817 static int fsl_spdif_rx_vbit_get(struct snd_kcontrol *kcontrol,
818 struct snd_ctl_elem_value *ucontrol)
820 struct snd_soc_dai *cpu_dai = snd_kcontrol_chip(kcontrol);
821 struct fsl_spdif_priv *spdif_priv = snd_soc_dai_get_drvdata(cpu_dai);
822 struct regmap *regmap = spdif_priv->regmap;
825 regmap_read(regmap, REG_SPDIF_SIS, &val);
826 ucontrol->value.integer.value[0] = (val & INT_VAL_NOGOOD) != 0;
827 regmap_write(regmap, REG_SPDIF_SIC, INT_VAL_NOGOOD);
832 static int fsl_spdif_tx_vbit_get(struct snd_kcontrol *kcontrol,
833 struct snd_ctl_elem_value *ucontrol)
835 struct snd_soc_dai *cpu_dai = snd_kcontrol_chip(kcontrol);
836 struct fsl_spdif_priv *spdif_priv = snd_soc_dai_get_drvdata(cpu_dai);
837 struct regmap *regmap = spdif_priv->regmap;
840 regmap_read(regmap, REG_SPDIF_SCR, &val);
841 val = (val & SCR_VAL_MASK) >> SCR_VAL_OFFSET;
843 ucontrol->value.integer.value[0] = val;
848 static int fsl_spdif_tx_vbit_put(struct snd_kcontrol *kcontrol,
849 struct snd_ctl_elem_value *ucontrol)
851 struct snd_soc_dai *cpu_dai = snd_kcontrol_chip(kcontrol);
852 struct fsl_spdif_priv *spdif_priv = snd_soc_dai_get_drvdata(cpu_dai);
853 struct regmap *regmap = spdif_priv->regmap;
854 u32 val = (1 - ucontrol->value.integer.value[0]) << SCR_VAL_OFFSET;
856 regmap_update_bits(regmap, REG_SPDIF_SCR, SCR_VAL_MASK, val);
861 /* DPLL lock information */
862 static int fsl_spdif_rxrate_info(struct snd_kcontrol *kcontrol,
863 struct snd_ctl_elem_info *uinfo)
865 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
867 uinfo->value.integer.min = 16000;
868 uinfo->value.integer.max = 192000;
873 static u32 gainsel_multi[GAINSEL_MULTI_MAX] = {
874 24, 16, 12, 8, 6, 4, 3,
877 /* Get RX data clock rate given the SPDIF bus_clk */
878 static int spdif_get_rxclk_rate(struct fsl_spdif_priv *spdif_priv,
879 enum spdif_gainsel gainsel)
881 struct regmap *regmap = spdif_priv->regmap;
882 struct platform_device *pdev = spdif_priv->pdev;
883 u64 tmpval64, busclk_freq = 0;
884 u32 freqmeas, phaseconf;
887 regmap_read(regmap, REG_SPDIF_SRFM, &freqmeas);
888 regmap_read(regmap, REG_SPDIF_SRPC, &phaseconf);
890 clksrc = (phaseconf >> SRPC_CLKSRC_SEL_OFFSET) & 0xf;
892 /* Get bus clock from system */
893 if (srpc_dpll_locked[clksrc] && (phaseconf & SRPC_DPLL_LOCKED))
894 busclk_freq = clk_get_rate(spdif_priv->sysclk);
896 /* FreqMeas_CLK = (BUS_CLK * FreqMeas) / 2 ^ 10 / GAINSEL / 128 */
897 tmpval64 = (u64) busclk_freq * freqmeas;
898 do_div(tmpval64, gainsel_multi[gainsel] * 1024);
899 do_div(tmpval64, 128 * 1024);
901 dev_dbg(&pdev->dev, "FreqMeas: %d\n", freqmeas);
902 dev_dbg(&pdev->dev, "BusclkFreq: %lld\n", busclk_freq);
903 dev_dbg(&pdev->dev, "RxRate: %lld\n", tmpval64);
905 return (int)tmpval64;
909 * Get DPLL lock or not info from stable interrupt status register.
910 * User application must use this control to get locked,
911 * then can do next PCM operation
913 static int fsl_spdif_rxrate_get(struct snd_kcontrol *kcontrol,
914 struct snd_ctl_elem_value *ucontrol)
916 struct snd_soc_dai *cpu_dai = snd_kcontrol_chip(kcontrol);
917 struct fsl_spdif_priv *spdif_priv = snd_soc_dai_get_drvdata(cpu_dai);
920 if (spdif_priv->dpll_locked)
921 rate = spdif_get_rxclk_rate(spdif_priv, SPDIF_DEFAULT_GAINSEL);
923 ucontrol->value.integer.value[0] = rate;
928 /* User bit sync mode info */
929 static int fsl_spdif_usync_info(struct snd_kcontrol *kcontrol,
930 struct snd_ctl_elem_info *uinfo)
932 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
934 uinfo->value.integer.min = 0;
935 uinfo->value.integer.max = 1;
941 * User bit sync mode:
942 * 1 CD User channel subcode
945 static int fsl_spdif_usync_get(struct snd_kcontrol *kcontrol,
946 struct snd_ctl_elem_value *ucontrol)
948 struct snd_soc_dai *cpu_dai = snd_kcontrol_chip(kcontrol);
949 struct fsl_spdif_priv *spdif_priv = snd_soc_dai_get_drvdata(cpu_dai);
950 struct regmap *regmap = spdif_priv->regmap;
953 regmap_read(regmap, REG_SPDIF_SRCD, &val);
954 ucontrol->value.integer.value[0] = (val & SRCD_CD_USER) != 0;
960 * User bit sync mode:
961 * 1 CD User channel subcode
964 static int fsl_spdif_usync_put(struct snd_kcontrol *kcontrol,
965 struct snd_ctl_elem_value *ucontrol)
967 struct snd_soc_dai *cpu_dai = snd_kcontrol_chip(kcontrol);
968 struct fsl_spdif_priv *spdif_priv = snd_soc_dai_get_drvdata(cpu_dai);
969 struct regmap *regmap = spdif_priv->regmap;
970 u32 val = ucontrol->value.integer.value[0] << SRCD_CD_USER_OFFSET;
972 regmap_update_bits(regmap, REG_SPDIF_SRCD, SRCD_CD_USER, val);
977 /* FSL SPDIF IEC958 controller defines */
978 static struct snd_kcontrol_new fsl_spdif_ctrls[] = {
979 /* Status cchanel controller */
981 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
982 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, DEFAULT),
983 .access = SNDRV_CTL_ELEM_ACCESS_READ |
984 SNDRV_CTL_ELEM_ACCESS_WRITE |
985 SNDRV_CTL_ELEM_ACCESS_VOLATILE,
986 .info = fsl_spdif_info,
987 .get = fsl_spdif_pb_get,
988 .put = fsl_spdif_pb_put,
991 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
992 .name = SNDRV_CTL_NAME_IEC958("", CAPTURE, DEFAULT),
993 .access = SNDRV_CTL_ELEM_ACCESS_READ |
994 SNDRV_CTL_ELEM_ACCESS_VOLATILE,
995 .info = fsl_spdif_info,
996 .get = fsl_spdif_capture_get,
998 /* User bits controller */
1000 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
1001 .name = "IEC958 Subcode Capture Default",
1002 .access = SNDRV_CTL_ELEM_ACCESS_READ |
1003 SNDRV_CTL_ELEM_ACCESS_VOLATILE,
1004 .info = fsl_spdif_info,
1005 .get = fsl_spdif_subcode_get,
1008 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
1009 .name = "IEC958 Q-subcode Capture Default",
1010 .access = SNDRV_CTL_ELEM_ACCESS_READ |
1011 SNDRV_CTL_ELEM_ACCESS_VOLATILE,
1012 .info = fsl_spdif_qinfo,
1013 .get = fsl_spdif_qget,
1015 /* Valid bit error controller */
1017 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
1018 .name = "IEC958 RX V-Bit Errors",
1019 .access = SNDRV_CTL_ELEM_ACCESS_READ |
1020 SNDRV_CTL_ELEM_ACCESS_VOLATILE,
1021 .info = fsl_spdif_vbit_info,
1022 .get = fsl_spdif_rx_vbit_get,
1025 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
1026 .name = "IEC958 TX V-Bit",
1027 .access = SNDRV_CTL_ELEM_ACCESS_READ |
1028 SNDRV_CTL_ELEM_ACCESS_WRITE |
1029 SNDRV_CTL_ELEM_ACCESS_VOLATILE,
1030 .info = fsl_spdif_vbit_info,
1031 .get = fsl_spdif_tx_vbit_get,
1032 .put = fsl_spdif_tx_vbit_put,
1034 /* DPLL lock info get controller */
1036 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
1037 .name = "RX Sample Rate",
1038 .access = SNDRV_CTL_ELEM_ACCESS_READ |
1039 SNDRV_CTL_ELEM_ACCESS_VOLATILE,
1040 .info = fsl_spdif_rxrate_info,
1041 .get = fsl_spdif_rxrate_get,
1043 /* User bit sync mode set/get controller */
1045 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
1046 .name = "IEC958 USyncMode CDText",
1047 .access = SNDRV_CTL_ELEM_ACCESS_READ |
1048 SNDRV_CTL_ELEM_ACCESS_WRITE |
1049 SNDRV_CTL_ELEM_ACCESS_VOLATILE,
1050 .info = fsl_spdif_usync_info,
1051 .get = fsl_spdif_usync_get,
1052 .put = fsl_spdif_usync_put,
1056 static int fsl_spdif_dai_probe(struct snd_soc_dai *dai)
1058 struct fsl_spdif_priv *spdif_private = snd_soc_dai_get_drvdata(dai);
1060 snd_soc_dai_init_dma_data(dai, &spdif_private->dma_params_tx,
1061 &spdif_private->dma_params_rx);
1063 snd_soc_add_dai_controls(dai, fsl_spdif_ctrls, ARRAY_SIZE(fsl_spdif_ctrls));
1065 /*Clear the val bit for Tx*/
1066 regmap_update_bits(spdif_private->regmap, REG_SPDIF_SCR,
1067 SCR_VAL_MASK, SCR_VAL_CLEAR);
1072 static struct snd_soc_dai_driver fsl_spdif_dai = {
1073 .probe = &fsl_spdif_dai_probe,
1075 .stream_name = "CPU-Playback",
1078 .rates = FSL_SPDIF_RATES_PLAYBACK,
1079 .formats = FSL_SPDIF_FORMATS_PLAYBACK,
1082 .stream_name = "CPU-Capture",
1085 .rates = FSL_SPDIF_RATES_CAPTURE,
1086 .formats = FSL_SPDIF_FORMATS_CAPTURE,
1088 .ops = &fsl_spdif_dai_ops,
1091 static const struct snd_soc_component_driver fsl_spdif_component = {
1092 .name = "fsl-spdif",
1095 /* FSL SPDIF REGMAP */
1096 static const struct reg_default fsl_spdif_reg_defaults[] = {
1097 {REG_SPDIF_SCR, 0x00000400},
1098 {REG_SPDIF_SRCD, 0x00000000},
1099 {REG_SPDIF_SIE, 0x00000000},
1100 {REG_SPDIF_STL, 0x00000000},
1101 {REG_SPDIF_STR, 0x00000000},
1102 {REG_SPDIF_STCSCH, 0x00000000},
1103 {REG_SPDIF_STCSCL, 0x00000000},
1104 {REG_SPDIF_STC, 0x00020f00},
1107 static bool fsl_spdif_readable_reg(struct device *dev, unsigned int reg)
1111 case REG_SPDIF_SRCD:
1112 case REG_SPDIF_SRPC:
1117 case REG_SPDIF_SRCSH:
1118 case REG_SPDIF_SRCSL:
1121 case REG_SPDIF_STCSCH:
1122 case REG_SPDIF_STCSCL:
1123 case REG_SPDIF_SRFM:
1131 static bool fsl_spdif_volatile_reg(struct device *dev, unsigned int reg)
1134 case REG_SPDIF_SRPC:
1138 case REG_SPDIF_SRCSH:
1139 case REG_SPDIF_SRCSL:
1142 case REG_SPDIF_SRFM:
1149 static bool fsl_spdif_writeable_reg(struct device *dev, unsigned int reg)
1153 case REG_SPDIF_SRCD:
1154 case REG_SPDIF_SRPC:
1159 case REG_SPDIF_STCSCH:
1160 case REG_SPDIF_STCSCL:
1168 static const struct regmap_config fsl_spdif_regmap_config = {
1173 .max_register = REG_SPDIF_STC,
1174 .reg_defaults = fsl_spdif_reg_defaults,
1175 .num_reg_defaults = ARRAY_SIZE(fsl_spdif_reg_defaults),
1176 .readable_reg = fsl_spdif_readable_reg,
1177 .volatile_reg = fsl_spdif_volatile_reg,
1178 .writeable_reg = fsl_spdif_writeable_reg,
1179 .cache_type = REGCACHE_FLAT,
1182 static u32 fsl_spdif_txclk_caldiv(struct fsl_spdif_priv *spdif_priv,
1183 struct clk *clk, u64 savesub,
1184 enum spdif_txrate index, bool round)
1186 static const u32 rate[] = { 32000, 44100, 48000, 88200, 96000, 176400,
1188 bool is_sysclk = clk_is_match(clk, spdif_priv->sysclk);
1189 u64 rate_ideal, rate_actual, sub;
1191 u16 sysclk_dfmin, sysclk_dfmax, sysclk_df;
1194 /* The sysclk has an extra divisor [2, 512] */
1195 sysclk_dfmin = is_sysclk ? 2 : 1;
1196 sysclk_dfmax = is_sysclk ? 512 : 1;
1198 for (sysclk_df = sysclk_dfmin; sysclk_df <= sysclk_dfmax; sysclk_df++) {
1199 for (txclk_df = 1; txclk_df <= 128; txclk_df++) {
1200 rate_ideal = rate[index] * txclk_df * 64ULL;
1202 rate_actual = clk_round_rate(clk, rate_ideal);
1204 rate_actual = clk_get_rate(clk);
1206 arate = rate_actual / 64;
1207 arate /= txclk_df * sysclk_df;
1209 if (arate == rate[index]) {
1212 spdif_priv->txclk_df[index] = txclk_df;
1213 spdif_priv->sysclk_df[index] = sysclk_df;
1214 spdif_priv->txrate[index] = arate;
1216 } else if (arate / rate[index] == 1) {
1217 /* A little bigger than expect */
1218 sub = (u64)(arate - rate[index]) * 100000;
1219 do_div(sub, rate[index]);
1223 spdif_priv->txclk_df[index] = txclk_df;
1224 spdif_priv->sysclk_df[index] = sysclk_df;
1225 spdif_priv->txrate[index] = arate;
1226 } else if (rate[index] / arate == 1) {
1227 /* A little smaller than expect */
1228 sub = (u64)(rate[index] - arate) * 100000;
1229 do_div(sub, rate[index]);
1233 spdif_priv->txclk_df[index] = txclk_df;
1234 spdif_priv->sysclk_df[index] = sysclk_df;
1235 spdif_priv->txrate[index] = arate;
1244 static int fsl_spdif_probe_txclk(struct fsl_spdif_priv *spdif_priv,
1245 enum spdif_txrate index)
1247 static const u32 rate[] = { 32000, 44100, 48000, 88200, 96000, 176400,
1249 struct platform_device *pdev = spdif_priv->pdev;
1250 struct device *dev = &pdev->dev;
1251 u64 savesub = 100000, ret;
1256 for (i = 0; i < STC_TXCLK_SRC_MAX; i++) {
1257 sprintf(tmp, "rxtx%d", i);
1258 clk = devm_clk_get(&pdev->dev, tmp);
1260 dev_err(dev, "no rxtx%d clock in devicetree\n", i);
1261 return PTR_ERR(clk);
1263 if (!clk_get_rate(clk))
1266 ret = fsl_spdif_txclk_caldiv(spdif_priv, clk, savesub, index,
1267 fsl_spdif_can_set_clk_rate(spdif_priv, i));
1272 spdif_priv->txclk[index] = clk;
1273 spdif_priv->txclk_src[index] = i;
1275 /* To quick catch a divisor, we allow a 0.1% deviation */
1280 dev_dbg(&pdev->dev, "use rxtx%d as tx clock source for %dHz sample rate\n",
1281 spdif_priv->txclk_src[index], rate[index]);
1282 dev_dbg(&pdev->dev, "use txclk df %d for %dHz sample rate\n",
1283 spdif_priv->txclk_df[index], rate[index]);
1284 if (clk_is_match(spdif_priv->txclk[index], spdif_priv->sysclk))
1285 dev_dbg(&pdev->dev, "use sysclk df %d for %dHz sample rate\n",
1286 spdif_priv->sysclk_df[index], rate[index]);
1287 dev_dbg(&pdev->dev, "the best rate for %dHz sample rate is %dHz\n",
1288 rate[index], spdif_priv->txrate[index]);
1293 static int fsl_spdif_probe(struct platform_device *pdev)
1295 struct fsl_spdif_priv *spdif_priv;
1296 struct spdif_mixer_control *ctrl;
1297 struct resource *res;
1301 spdif_priv = devm_kzalloc(&pdev->dev, sizeof(*spdif_priv), GFP_KERNEL);
1305 spdif_priv->pdev = pdev;
1307 spdif_priv->soc = of_device_get_match_data(&pdev->dev);
1308 if (!spdif_priv->soc) {
1309 dev_err(&pdev->dev, "failed to get soc data\n");
1313 /* Initialize this copy of the CPU DAI driver structure */
1314 memcpy(&spdif_priv->cpu_dai_drv, &fsl_spdif_dai, sizeof(fsl_spdif_dai));
1315 spdif_priv->cpu_dai_drv.name = dev_name(&pdev->dev);
1316 spdif_priv->cpu_dai_drv.playback.formats =
1317 spdif_priv->soc->tx_formats;
1319 /* Get the addresses and IRQ */
1320 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1321 regs = devm_ioremap_resource(&pdev->dev, res);
1323 return PTR_ERR(regs);
1325 spdif_priv->regmap = devm_regmap_init_mmio_clk(&pdev->dev,
1326 "core", regs, &fsl_spdif_regmap_config);
1327 if (IS_ERR(spdif_priv->regmap)) {
1328 dev_err(&pdev->dev, "regmap init failed\n");
1329 return PTR_ERR(spdif_priv->regmap);
1332 for (i = 0; i < spdif_priv->soc->interrupts; i++) {
1333 irq = platform_get_irq(pdev, i);
1335 dev_err(&pdev->dev, "no irq for node %s\n", pdev->name);
1339 ret = devm_request_irq(&pdev->dev, irq, spdif_isr, 0,
1340 dev_name(&pdev->dev), spdif_priv);
1342 dev_err(&pdev->dev, "could not claim irq %u\n", irq);
1347 /* Get system clock for rx clock rate calculation */
1348 spdif_priv->sysclk = devm_clk_get(&pdev->dev, "rxtx5");
1349 if (IS_ERR(spdif_priv->sysclk)) {
1350 dev_err(&pdev->dev, "no sys clock (rxtx5) in devicetree\n");
1351 return PTR_ERR(spdif_priv->sysclk);
1354 /* Get core clock for data register access via DMA */
1355 spdif_priv->coreclk = devm_clk_get(&pdev->dev, "core");
1356 if (IS_ERR(spdif_priv->coreclk)) {
1357 dev_err(&pdev->dev, "no core clock in devicetree\n");
1358 return PTR_ERR(spdif_priv->coreclk);
1361 spdif_priv->spbaclk = devm_clk_get(&pdev->dev, "spba");
1362 if (IS_ERR(spdif_priv->spbaclk))
1363 dev_warn(&pdev->dev, "no spba clock in devicetree\n");
1365 /* Select clock source for rx/tx clock */
1366 spdif_priv->rxclk = devm_clk_get(&pdev->dev, "rxtx1");
1367 if (IS_ERR(spdif_priv->rxclk)) {
1368 dev_err(&pdev->dev, "no rxtx1 clock in devicetree\n");
1369 return PTR_ERR(spdif_priv->rxclk);
1371 spdif_priv->rxclk_src = DEFAULT_RXCLK_SRC;
1373 for (i = 0; i < SPDIF_TXRATE_MAX; i++) {
1374 ret = fsl_spdif_probe_txclk(spdif_priv, i);
1379 /* Initial spinlock for control data */
1380 ctrl = &spdif_priv->fsl_spdif_control;
1381 spin_lock_init(&ctrl->ctl_lock);
1383 /* Init tx channel status default value */
1384 ctrl->ch_status[0] = IEC958_AES0_CON_NOT_COPYRIGHT |
1385 IEC958_AES0_CON_EMPHASIS_5015;
1386 ctrl->ch_status[1] = IEC958_AES1_CON_DIGDIGCONV_ID;
1387 ctrl->ch_status[2] = 0x00;
1388 ctrl->ch_status[3] = IEC958_AES3_CON_FS_44100 |
1389 IEC958_AES3_CON_CLOCK_1000PPM;
1391 spdif_priv->dpll_locked = false;
1393 spdif_priv->dma_params_tx.maxburst = spdif_priv->soc->tx_burst;
1394 spdif_priv->dma_params_rx.maxburst = spdif_priv->soc->rx_burst;
1395 spdif_priv->dma_params_tx.addr = res->start + REG_SPDIF_STL;
1396 spdif_priv->dma_params_rx.addr = res->start + REG_SPDIF_SRL;
1398 /* Register with ASoC */
1399 dev_set_drvdata(&pdev->dev, spdif_priv);
1400 pm_runtime_enable(&pdev->dev);
1401 regcache_cache_only(spdif_priv->regmap, true);
1403 ret = devm_snd_soc_register_component(&pdev->dev, &fsl_spdif_component,
1404 &spdif_priv->cpu_dai_drv, 1);
1406 dev_err(&pdev->dev, "failed to register DAI: %d\n", ret);
1410 ret = imx_pcm_dma_init(pdev, IMX_SPDIF_DMABUF_SIZE);
1411 if (ret && ret != -EPROBE_DEFER)
1412 dev_err(&pdev->dev, "imx_pcm_dma_init failed: %d\n", ret);
1418 static int fsl_spdif_runtime_suspend(struct device *dev)
1420 struct fsl_spdif_priv *spdif_priv = dev_get_drvdata(dev);
1423 regmap_read(spdif_priv->regmap, REG_SPDIF_SRPC,
1424 &spdif_priv->regcache_srpc);
1425 regcache_cache_only(spdif_priv->regmap, true);
1427 clk_disable_unprepare(spdif_priv->rxclk);
1429 for (i = 0; i < SPDIF_TXRATE_MAX; i++)
1430 clk_disable_unprepare(spdif_priv->txclk[i]);
1432 if (!IS_ERR(spdif_priv->spbaclk))
1433 clk_disable_unprepare(spdif_priv->spbaclk);
1434 clk_disable_unprepare(spdif_priv->coreclk);
1439 static int fsl_spdif_runtime_resume(struct device *dev)
1441 struct fsl_spdif_priv *spdif_priv = dev_get_drvdata(dev);
1445 ret = clk_prepare_enable(spdif_priv->coreclk);
1447 dev_err(dev, "failed to enable core clock\n");
1451 if (!IS_ERR(spdif_priv->spbaclk)) {
1452 ret = clk_prepare_enable(spdif_priv->spbaclk);
1454 dev_err(dev, "failed to enable spba clock\n");
1455 goto disable_core_clk;
1459 for (i = 0; i < SPDIF_TXRATE_MAX; i++) {
1460 ret = clk_prepare_enable(spdif_priv->txclk[i]);
1462 goto disable_tx_clk;
1465 ret = clk_prepare_enable(spdif_priv->rxclk);
1467 goto disable_tx_clk;
1469 regcache_cache_only(spdif_priv->regmap, false);
1470 regcache_mark_dirty(spdif_priv->regmap);
1472 regmap_update_bits(spdif_priv->regmap, REG_SPDIF_SRPC,
1473 SRPC_CLKSRC_SEL_MASK | SRPC_GAINSEL_MASK,
1474 spdif_priv->regcache_srpc);
1476 ret = regcache_sync(spdif_priv->regmap);
1478 goto disable_rx_clk;
1483 clk_disable_unprepare(spdif_priv->rxclk);
1485 for (i--; i >= 0; i--)
1486 clk_disable_unprepare(spdif_priv->txclk[i]);
1487 if (!IS_ERR(spdif_priv->spbaclk))
1488 clk_disable_unprepare(spdif_priv->spbaclk);
1490 clk_disable_unprepare(spdif_priv->coreclk);
1494 #endif /* CONFIG_PM */
1496 static const struct dev_pm_ops fsl_spdif_pm = {
1497 SET_SYSTEM_SLEEP_PM_OPS(pm_runtime_force_suspend,
1498 pm_runtime_force_resume)
1499 SET_RUNTIME_PM_OPS(fsl_spdif_runtime_suspend, fsl_spdif_runtime_resume,
1503 static const struct of_device_id fsl_spdif_dt_ids[] = {
1504 { .compatible = "fsl,imx35-spdif", .data = &fsl_spdif_imx35, },
1505 { .compatible = "fsl,vf610-spdif", .data = &fsl_spdif_vf610, },
1506 { .compatible = "fsl,imx6sx-spdif", .data = &fsl_spdif_imx6sx, },
1507 { .compatible = "fsl,imx8qm-spdif", .data = &fsl_spdif_imx8qm, },
1510 MODULE_DEVICE_TABLE(of, fsl_spdif_dt_ids);
1512 static struct platform_driver fsl_spdif_driver = {
1514 .name = "fsl-spdif-dai",
1515 .of_match_table = fsl_spdif_dt_ids,
1516 .pm = &fsl_spdif_pm,
1518 .probe = fsl_spdif_probe,
1521 module_platform_driver(fsl_spdif_driver);
1523 MODULE_AUTHOR("Freescale Semiconductor, Inc.");
1524 MODULE_DESCRIPTION("Freescale S/PDIF CPU DAI Driver");
1525 MODULE_LICENSE("GPL v2");
1526 MODULE_ALIAS("platform:fsl-spdif-dai");