blob: e494ad6ec7d71084db6118d8fc40e1934e2ccc17 [file] [log] [blame]
Fabio Estevam165a30e2018-05-01 09:20:43 -03001// SPDX-License-Identifier: GPL-2.0
2//
3// Freescale S/PDIF ALSA SoC Digital Audio Interface (DAI) driver
4//
5// Copyright (C) 2013 Freescale Semiconductor, Inc.
6//
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
Nicolin Chena2388a42013-08-21 11:13:16 +080011
Xiubo Liadd180e2014-04-04 15:10:27 +080012#include <linux/bitrev.h>
Nicolin Chena2388a42013-08-21 11:13:16 +080013#include <linux/clk.h>
Xiubo Liadd180e2014-04-04 15:10:27 +080014#include <linux/module.h>
Nicolin Chena2388a42013-08-21 11:13:16 +080015#include <linux/of_address.h>
16#include <linux/of_device.h>
17#include <linux/of_irq.h>
Xiubo Liadd180e2014-04-04 15:10:27 +080018#include <linux/regmap.h>
Shengjiu Wang9cb2b372020-06-19 15:54:33 +080019#include <linux/pm_runtime.h>
Nicolin Chena2388a42013-08-21 11:13:16 +080020
21#include <sound/asoundef.h>
Nicolin Chena2388a42013-08-21 11:13:16 +080022#include <sound/dmaengine_pcm.h>
Xiubo Liadd180e2014-04-04 15:10:27 +080023#include <sound/soc.h>
Nicolin Chena2388a42013-08-21 11:13:16 +080024
25#include "fsl_spdif.h"
26#include "imx-pcm.h"
27
28#define FSL_SPDIF_TXFIFO_WML 0x8
29#define FSL_SPDIF_RXFIFO_WML 0x8
30
Nicolin Chenf3a30ba2014-05-06 16:42:25 +080031#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)
36
37#define SIE_INTR_FOR(tx) (tx ? INTR_FOR_PLAYBACK : INTR_FOR_CAPTURE)
Nicolin Chena2388a42013-08-21 11:13:16 +080038
39/* Index list for the values that has if (DPLL Locked) condition */
40static u8 srpc_dpll_locked[] = { 0x0, 0x1, 0x2, 0x3, 0x4, 0xa, 0xb };
41#define SRPC_NODPLL_START1 0x5
42#define SRPC_NODPLL_START2 0xc
43
44#define DEFAULT_RXCLK_SRC 1
45
Shengjiu Wangf61b9272020-06-17 14:58:01 +080046/**
47 * struct fsl_spdif_soc_data: soc specific data
48 *
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
Shengjiu Wang516232e2020-10-15 13:28:48 +080052 * @interrupts: interrupt number
53 * @tx_burst: tx maxburst size
54 * @rx_burst: rx maxburst size
55 * @tx_formats: tx supported data format
Shengjiu Wangf61b9272020-06-17 14:58:01 +080056 */
57struct fsl_spdif_soc_data {
58 bool imx;
59 bool shared_root_clock;
Shengjiu Wang516232e2020-10-15 13:28:48 +080060 u32 interrupts;
61 u32 tx_burst;
62 u32 rx_burst;
63 u64 tx_formats;
Shengjiu Wangf61b9272020-06-17 14:58:01 +080064};
65
Nicolin Chena2388a42013-08-21 11:13:16 +080066/*
67 * SPDIF control structure
68 * Defines channel status, subcode and Q sub
69 */
70struct spdif_mixer_control {
71 /* spinlock to access control data */
72 spinlock_t ctl_lock;
73
74 /* IEC958 channel tx status bit */
75 unsigned char ch_status[4];
76
77 /* User bits */
78 unsigned char subcode[2 * SPDIF_UBITS_SIZE];
79
80 /* Q subcode part of user bits */
81 unsigned char qsub[2 * SPDIF_QSUB_SIZE];
82
83 /* Buffer offset for U/Q */
84 u32 upos;
85 u32 qpos;
86
87 /* Ready buffer index of the two buffers */
88 u32 ready_buf;
89};
90
Nicolin Chenb8a832a2014-04-30 18:54:09 +080091/**
Pierre-Louis Bossart28fd6ff2020-07-02 14:21:39 -050092 * struct fsl_spdif_priv - Freescale SPDIF private data
93 * @soc: SPDIF soc data
Nicolin Chenb8a832a2014-04-30 18:54:09 +080094 * @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
Shengjiu Wang0bc56802015-11-24 17:19:33 +0800108 * @spbaclk: SPBA clock (optional, depending on SoC design)
Nicolin Chenb8a832a2014-04-30 18:54:09 +0800109 * @dma_params_tx: DMA parameters for transmit channel
110 * @dma_params_rx: DMA parameters for receive channel
Pierre-Louis Bossart28fd6ff2020-07-02 14:21:39 -0500111 * @regcache_srpc: regcache for SRPC
Nicolin Chenb8a832a2014-04-30 18:54:09 +0800112 */
Nicolin Chena2388a42013-08-21 11:13:16 +0800113struct fsl_spdif_priv {
Shengjiu Wangf61b9272020-06-17 14:58:01 +0800114 const struct fsl_spdif_soc_data *soc;
Nicolin Chena2388a42013-08-21 11:13:16 +0800115 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;
119 bool dpll_locked;
Anssi Hannulac7dfeed2014-06-16 02:56:42 +0300120 u32 txrate[SPDIF_TXRATE_MAX];
Nicolin Chene41a4a72014-04-30 18:54:06 +0800121 u8 txclk_df[SPDIF_TXRATE_MAX];
Viorel Suman22316092019-02-18 15:25:00 +0000122 u16 sysclk_df[SPDIF_TXRATE_MAX];
Nicolin Chena2388a42013-08-21 11:13:16 +0800123 u8 txclk_src[SPDIF_TXRATE_MAX];
124 u8 rxclk_src;
125 struct clk *txclk[SPDIF_TXRATE_MAX];
126 struct clk *rxclk;
Nicolin Chen08f73362014-04-24 18:52:24 +0800127 struct clk *coreclk;
Nicolin Chen0b864392014-04-28 23:07:51 +0800128 struct clk *sysclk;
Shengjiu Wang0bc56802015-11-24 17:19:33 +0800129 struct clk *spbaclk;
Nicolin Chena2388a42013-08-21 11:13:16 +0800130 struct snd_dmaengine_dai_dma_data dma_params_tx;
131 struct snd_dmaengine_dai_dma_data dma_params_rx;
Zidan Wangf9f4fa62015-09-18 11:09:11 +0800132 /* regcache for SRPC */
133 u32 regcache_srpc;
Nicolin Chena2388a42013-08-21 11:13:16 +0800134};
135
Shengjiu Wangf61b9272020-06-17 14:58:01 +0800136static struct fsl_spdif_soc_data fsl_spdif_vf610 = {
137 .imx = false,
138 .shared_root_clock = false,
Shengjiu Wang516232e2020-10-15 13:28:48 +0800139 .interrupts = 1,
140 .tx_burst = FSL_SPDIF_TXFIFO_WML,
141 .rx_burst = FSL_SPDIF_RXFIFO_WML,
142 .tx_formats = FSL_SPDIF_FORMATS_PLAYBACK,
Shengjiu Wangf61b9272020-06-17 14:58:01 +0800143};
144
145static struct fsl_spdif_soc_data fsl_spdif_imx35 = {
146 .imx = true,
147 .shared_root_clock = false,
Shengjiu Wang516232e2020-10-15 13:28:48 +0800148 .interrupts = 1,
149 .tx_burst = FSL_SPDIF_TXFIFO_WML,
150 .rx_burst = FSL_SPDIF_RXFIFO_WML,
151 .tx_formats = FSL_SPDIF_FORMATS_PLAYBACK,
Shengjiu Wangf61b9272020-06-17 14:58:01 +0800152};
153
154static struct fsl_spdif_soc_data fsl_spdif_imx6sx = {
155 .imx = true,
156 .shared_root_clock = true,
Shengjiu Wang516232e2020-10-15 13:28:48 +0800157 .interrupts = 1,
158 .tx_burst = FSL_SPDIF_TXFIFO_WML,
159 .rx_burst = FSL_SPDIF_RXFIFO_WML,
160 .tx_formats = FSL_SPDIF_FORMATS_PLAYBACK,
161
162};
163
164static struct fsl_spdif_soc_data fsl_spdif_imx8qm = {
165 .imx = true,
166 .shared_root_clock = true,
167 .interrupts = 2,
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 */
Shengjiu Wangf61b9272020-06-17 14:58:01 +0800171};
172
173/* Check if clk is a root clock that does not share clock source with others */
174static inline bool fsl_spdif_can_set_clk_rate(struct fsl_spdif_priv *spdif, int clk)
175{
176 return (clk == STC_TXCLK_SPDIF_ROOT) && !spdif->soc->shared_root_clock;
177}
178
Nicolin Chena2388a42013-08-21 11:13:16 +0800179/* DPLL locked and lock loss interrupt handler */
180static void spdif_irq_dpll_lock(struct fsl_spdif_priv *spdif_priv)
181{
182 struct regmap *regmap = spdif_priv->regmap;
183 struct platform_device *pdev = spdif_priv->pdev;
184 u32 locked;
185
186 regmap_read(regmap, REG_SPDIF_SRPC, &locked);
187 locked &= SRPC_DPLL_LOCKED;
188
189 dev_dbg(&pdev->dev, "isr: Rx dpll %s \n",
190 locked ? "locked" : "loss lock");
191
192 spdif_priv->dpll_locked = locked ? true : false;
193}
194
195/* Receiver found illegal symbol interrupt handler */
196static void spdif_irq_sym_error(struct fsl_spdif_priv *spdif_priv)
197{
198 struct regmap *regmap = spdif_priv->regmap;
199 struct platform_device *pdev = spdif_priv->pdev;
200
201 dev_dbg(&pdev->dev, "isr: receiver found illegal symbol\n");
202
Nicolin Chenf3a30ba2014-05-06 16:42:25 +0800203 /* Clear illegal symbol if DPLL unlocked since no audio stream */
204 if (!spdif_priv->dpll_locked)
Nicolin Chena2388a42013-08-21 11:13:16 +0800205 regmap_update_bits(regmap, REG_SPDIF_SIE, INT_SYM_ERR, 0);
Nicolin Chena2388a42013-08-21 11:13:16 +0800206}
207
208/* U/Q Channel receive register full */
209static void spdif_irq_uqrx_full(struct fsl_spdif_priv *spdif_priv, char name)
210{
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;
215
216 switch (name) {
217 case 'U':
218 pos = &ctrl->upos;
219 size = SPDIF_UBITS_SIZE;
220 reg = REG_SPDIF_SRU;
221 break;
222 case 'Q':
223 pos = &ctrl->qpos;
224 size = SPDIF_QSUB_SIZE;
225 reg = REG_SPDIF_SRQ;
226 break;
227 default:
228 dev_err(&pdev->dev, "unsupported channel name\n");
229 return;
230 }
231
232 dev_dbg(&pdev->dev, "isr: %c Channel receive register full\n", name);
233
234 if (*pos >= size * 2) {
235 *pos = 0;
236 } else if (unlikely((*pos % size) + 3 > size)) {
Colin Ian Kingd93c5062016-06-28 13:47:59 +0100237 dev_err(&pdev->dev, "User bit receive buffer overflow\n");
Nicolin Chena2388a42013-08-21 11:13:16 +0800238 return;
239 }
240
241 regmap_read(regmap, reg, &val);
242 ctrl->subcode[*pos++] = val >> 16;
243 ctrl->subcode[*pos++] = val >> 8;
244 ctrl->subcode[*pos++] = val;
245}
246
247/* U/Q Channel sync found */
248static void spdif_irq_uq_sync(struct fsl_spdif_priv *spdif_priv)
249{
250 struct spdif_mixer_control *ctrl = &spdif_priv->fsl_spdif_control;
251 struct platform_device *pdev = spdif_priv->pdev;
252
253 dev_dbg(&pdev->dev, "isr: U/Q Channel sync found\n");
254
255 /* U/Q buffer reset */
256 if (ctrl->qpos == 0)
257 return;
258
259 /* Set ready to this buffer */
260 ctrl->ready_buf = (ctrl->qpos - 1) / SPDIF_QSUB_SIZE + 1;
261}
262
263/* U/Q Channel framing error */
264static void spdif_irq_uq_err(struct fsl_spdif_priv *spdif_priv)
265{
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;
269 u32 val;
270
271 dev_dbg(&pdev->dev, "isr: U/Q Channel framing error\n");
272
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);
276
277 /* Drop this U/Q buffer */
278 ctrl->ready_buf = 0;
279 ctrl->upos = 0;
280 ctrl->qpos = 0;
281}
282
283/* Get spdif interrupt status and clear the interrupt */
284static u32 spdif_intr_status_clear(struct fsl_spdif_priv *spdif_priv)
285{
286 struct regmap *regmap = spdif_priv->regmap;
287 u32 val, val2;
288
289 regmap_read(regmap, REG_SPDIF_SIS, &val);
290 regmap_read(regmap, REG_SPDIF_SIE, &val2);
291
292 regmap_write(regmap, REG_SPDIF_SIC, val & val2);
293
294 return val;
295}
296
297static irqreturn_t spdif_isr(int irq, void *devid)
298{
299 struct fsl_spdif_priv *spdif_priv = (struct fsl_spdif_priv *)devid;
300 struct platform_device *pdev = spdif_priv->pdev;
301 u32 sis;
302
303 sis = spdif_intr_status_clear(spdif_priv);
304
305 if (sis & INT_DPLL_LOCKED)
306 spdif_irq_dpll_lock(spdif_priv);
307
308 if (sis & INT_TXFIFO_UNOV)
309 dev_dbg(&pdev->dev, "isr: Tx FIFO under/overrun\n");
310
311 if (sis & INT_TXFIFO_RESYNC)
312 dev_dbg(&pdev->dev, "isr: Tx FIFO resync\n");
313
314 if (sis & INT_CNEW)
315 dev_dbg(&pdev->dev, "isr: cstatus new\n");
316
317 if (sis & INT_VAL_NOGOOD)
318 dev_dbg(&pdev->dev, "isr: validity flag no good\n");
319
320 if (sis & INT_SYM_ERR)
321 spdif_irq_sym_error(spdif_priv);
322
323 if (sis & INT_BIT_ERR)
324 dev_dbg(&pdev->dev, "isr: receiver found parity bit error\n");
325
326 if (sis & INT_URX_FUL)
327 spdif_irq_uqrx_full(spdif_priv, 'U');
328
329 if (sis & INT_URX_OV)
330 dev_dbg(&pdev->dev, "isr: U Channel receive register overrun\n");
331
332 if (sis & INT_QRX_FUL)
333 spdif_irq_uqrx_full(spdif_priv, 'Q');
334
335 if (sis & INT_QRX_OV)
336 dev_dbg(&pdev->dev, "isr: Q Channel receive register overrun\n");
337
338 if (sis & INT_UQ_SYNC)
339 spdif_irq_uq_sync(spdif_priv);
340
341 if (sis & INT_UQ_ERR)
342 spdif_irq_uq_err(spdif_priv);
343
344 if (sis & INT_RXFIFO_UNOV)
345 dev_dbg(&pdev->dev, "isr: Rx FIFO under/overrun\n");
346
347 if (sis & INT_RXFIFO_RESYNC)
348 dev_dbg(&pdev->dev, "isr: Rx FIFO resync\n");
349
350 if (sis & INT_LOSS_LOCK)
351 spdif_irq_dpll_lock(spdif_priv);
352
353 /* FIXME: Write Tx FIFO to clear TxEm */
354 if (sis & INT_TX_EM)
355 dev_dbg(&pdev->dev, "isr: Tx FIFO empty\n");
356
357 /* FIXME: Read Rx FIFO to clear RxFIFOFul */
358 if (sis & INT_RXFIFO_FUL)
359 dev_dbg(&pdev->dev, "isr: Rx FIFO full\n");
360
361 return IRQ_HANDLED;
362}
363
364static int spdif_softreset(struct fsl_spdif_priv *spdif_priv)
365{
366 struct regmap *regmap = spdif_priv->regmap;
367 u32 val, cycle = 1000;
368
Zidan Wangf9f4fa62015-09-18 11:09:11 +0800369 regcache_cache_bypass(regmap, true);
370
Nicolin Chena2388a42013-08-21 11:13:16 +0800371 regmap_write(regmap, REG_SPDIF_SCR, SCR_SOFT_RESET);
372
373 /*
374 * RESET bit would be cleared after finishing its reset procedure,
375 * which typically lasts 8 cycles. 1000 cycles will keep it safe.
376 */
377 do {
378 regmap_read(regmap, REG_SPDIF_SCR, &val);
379 } while ((val & SCR_SOFT_RESET) && cycle--);
380
Zidan Wangf9f4fa62015-09-18 11:09:11 +0800381 regcache_cache_bypass(regmap, false);
382 regcache_mark_dirty(regmap);
383 regcache_sync(regmap);
384
Nicolin Chena2388a42013-08-21 11:13:16 +0800385 if (cycle)
386 return 0;
387 else
388 return -EBUSY;
389}
390
391static void spdif_set_cstatus(struct spdif_mixer_control *ctrl,
392 u8 mask, u8 cstatus)
393{
394 ctrl->ch_status[3] &= ~mask;
395 ctrl->ch_status[3] |= cstatus & mask;
396}
397
398static void spdif_write_channel_status(struct fsl_spdif_priv *spdif_priv)
399{
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;
403 u32 ch_status;
404
405 ch_status = (bitrev8(ctrl->ch_status[0]) << 16) |
Nicolin Chenf3a30ba2014-05-06 16:42:25 +0800406 (bitrev8(ctrl->ch_status[1]) << 8) |
407 bitrev8(ctrl->ch_status[2]);
Nicolin Chena2388a42013-08-21 11:13:16 +0800408 regmap_write(regmap, REG_SPDIF_STCSCH, ch_status);
409
410 dev_dbg(&pdev->dev, "STCSCH: 0x%06x\n", ch_status);
411
412 ch_status = bitrev8(ctrl->ch_status[3]) << 16;
413 regmap_write(regmap, REG_SPDIF_STCSCL, ch_status);
414
415 dev_dbg(&pdev->dev, "STCSCL: 0x%06x\n", ch_status);
416}
417
418/* Set SPDIF PhaseConfig register for rx clock */
419static int spdif_set_rx_clksrc(struct fsl_spdif_priv *spdif_priv,
420 enum spdif_gainsel gainsel, int dpll_locked)
421{
422 struct regmap *regmap = spdif_priv->regmap;
423 u8 clksrc = spdif_priv->rxclk_src;
424
425 if (clksrc >= SRPC_CLKSRC_MAX || gainsel >= GAINSEL_MULTI_MAX)
426 return -EINVAL;
427
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));
431
432 return 0;
433}
434
435static int spdif_set_sample_rate(struct snd_pcm_substream *substream,
436 int sample_rate)
437{
Kuninori Morimoto9f5f0782020-07-20 10:18:38 +0900438 struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
Kuninori Morimoto17198ae2020-03-23 14:18:30 +0900439 struct fsl_spdif_priv *spdif_priv = snd_soc_dai_get_drvdata(asoc_rtd_to_cpu(rtd, 0));
Nicolin Chena2388a42013-08-21 11:13:16 +0800440 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;
444 u32 stc, mask, rate;
Viorel Suman22316092019-02-18 15:25:00 +0000445 u16 sysclk_df;
446 u8 clk, txclk_df;
Nicolin Chena2388a42013-08-21 11:13:16 +0800447 int ret;
448
449 switch (sample_rate) {
450 case 32000:
451 rate = SPDIF_TXRATE_32000;
452 csfs = IEC958_AES3_CON_FS_32000;
453 break;
454 case 44100:
455 rate = SPDIF_TXRATE_44100;
456 csfs = IEC958_AES3_CON_FS_44100;
457 break;
458 case 48000:
459 rate = SPDIF_TXRATE_48000;
460 csfs = IEC958_AES3_CON_FS_48000;
461 break;
Anssi Hannulac7dfeed2014-06-16 02:56:42 +0300462 case 96000:
463 rate = SPDIF_TXRATE_96000;
464 csfs = IEC958_AES3_CON_FS_96000;
465 break;
466 case 192000:
467 rate = SPDIF_TXRATE_192000;
468 csfs = IEC958_AES3_CON_FS_192000;
469 break;
Nicolin Chena2388a42013-08-21 11:13:16 +0800470 default:
471 dev_err(&pdev->dev, "unsupported sample rate %d\n", sample_rate);
472 return -EINVAL;
473 }
474
475 clk = spdif_priv->txclk_src[rate];
476 if (clk >= STC_TXCLK_SRC_MAX) {
477 dev_err(&pdev->dev, "tx clock source is out of range\n");
478 return -EINVAL;
479 }
480
Nicolin Chene41a4a72014-04-30 18:54:06 +0800481 txclk_df = spdif_priv->txclk_df[rate];
482 if (txclk_df == 0) {
483 dev_err(&pdev->dev, "the txclk_df can't be zero\n");
Nicolin Chena2388a42013-08-21 11:13:16 +0800484 return -EINVAL;
485 }
486
Nicolin Chen27c647b2014-04-30 18:54:07 +0800487 sysclk_df = spdif_priv->sysclk_df[rate];
488
Shengjiu Wangf61b9272020-06-17 14:58:01 +0800489 if (!fsl_spdif_can_set_clk_rate(spdif_priv, clk))
Nicolin Chen9c6344b2014-04-30 18:54:05 +0800490 goto clk_set_bypass;
491
Nicolin Chenf490f322015-05-24 01:12:41 -0700492 /* The S/PDIF block needs a clock of 64 * fs * txclk_df */
493 ret = clk_set_rate(spdif_priv->txclk[rate],
494 64 * sample_rate * txclk_df);
Nicolin Chena2388a42013-08-21 11:13:16 +0800495 if (ret) {
496 dev_err(&pdev->dev, "failed to set tx clock rate\n");
497 return ret;
498 }
499
Nicolin Chen9c6344b2014-04-30 18:54:05 +0800500clk_set_bypass:
Nicolin Chena2388a42013-08-21 11:13:16 +0800501 dev_dbg(&pdev->dev, "expected clock rate = %d\n",
Nicolin Chen27c647b2014-04-30 18:54:07 +0800502 (64 * sample_rate * txclk_df * sysclk_df));
Nicolin Chena2388a42013-08-21 11:13:16 +0800503 dev_dbg(&pdev->dev, "actual clock rate = %ld\n",
504 clk_get_rate(spdif_priv->txclk[rate]));
505
506 /* set fs field in consumer channel status */
507 spdif_set_cstatus(ctrl, IEC958_AES3_CON_FS, csfs);
508
509 /* select clock source and divisor */
Nicolin Chenf3a30ba2014-05-06 16:42:25 +0800510 stc = STC_TXCLK_ALL_EN | STC_TXCLK_SRC_SET(clk) |
511 STC_TXCLK_DF(txclk_df) | STC_SYSCLK_DF(sysclk_df);
512 mask = STC_TXCLK_ALL_EN_MASK | STC_TXCLK_SRC_MASK |
513 STC_TXCLK_DF_MASK | STC_SYSCLK_DF_MASK;
Nicolin Chena2388a42013-08-21 11:13:16 +0800514 regmap_update_bits(regmap, REG_SPDIF_STC, mask, stc);
515
Nicolin Chen527cda72014-04-30 18:54:08 +0800516 dev_dbg(&pdev->dev, "set sample rate to %dHz for %dHz playback\n",
517 spdif_priv->txrate[rate], sample_rate);
Nicolin Chena2388a42013-08-21 11:13:16 +0800518
519 return 0;
520}
521
Mark Brown6b4c80f2013-08-31 16:40:51 +0100522static int fsl_spdif_startup(struct snd_pcm_substream *substream,
523 struct snd_soc_dai *cpu_dai)
Nicolin Chena2388a42013-08-21 11:13:16 +0800524{
Kuninori Morimoto9f5f0782020-07-20 10:18:38 +0900525 struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
Kuninori Morimoto17198ae2020-03-23 14:18:30 +0900526 struct fsl_spdif_priv *spdif_priv = snd_soc_dai_get_drvdata(asoc_rtd_to_cpu(rtd, 0));
Nicolin Chena2388a42013-08-21 11:13:16 +0800527 struct platform_device *pdev = spdif_priv->pdev;
528 struct regmap *regmap = spdif_priv->regmap;
Dan Carpenter89e0e252015-07-09 11:21:03 +0300529 u32 scr, mask;
Nicolin Chena2388a42013-08-21 11:13:16 +0800530 int ret;
531
532 /* Reset module and interrupts only for first initialization */
Kuninori Morimoto1d9fb192020-05-15 09:47:17 +0900533 if (!snd_soc_dai_active(cpu_dai)) {
Nicolin Chena2388a42013-08-21 11:13:16 +0800534 ret = spdif_softreset(spdif_priv);
535 if (ret) {
536 dev_err(&pdev->dev, "failed to soft reset\n");
Shengjiu Wang9cb2b372020-06-19 15:54:33 +0800537 return ret;
Nicolin Chena2388a42013-08-21 11:13:16 +0800538 }
539
540 /* Disable all the interrupts */
541 regmap_update_bits(regmap, REG_SPDIF_SIE, 0xffffff, 0);
542 }
543
544 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
545 scr = SCR_TXFIFO_AUTOSYNC | SCR_TXFIFO_CTRL_NORMAL |
546 SCR_TXSEL_NORMAL | SCR_USRC_SEL_CHIP |
547 SCR_TXFIFO_FSEL_IF8;
548 mask = SCR_TXFIFO_AUTOSYNC_MASK | SCR_TXFIFO_CTRL_MASK |
549 SCR_TXSEL_MASK | SCR_USRC_SEL_MASK |
550 SCR_TXFIFO_FSEL_MASK;
Nicolin Chena2388a42013-08-21 11:13:16 +0800551 } else {
552 scr = SCR_RXFIFO_FSEL_IF8 | SCR_RXFIFO_AUTOSYNC;
553 mask = SCR_RXFIFO_FSEL_MASK | SCR_RXFIFO_AUTOSYNC_MASK|
554 SCR_RXFIFO_CTL_MASK | SCR_RXFIFO_OFF_MASK;
Nicolin Chena2388a42013-08-21 11:13:16 +0800555 }
556 regmap_update_bits(regmap, REG_SPDIF_SCR, mask, scr);
557
558 /* Power up SPDIF module */
559 regmap_update_bits(regmap, REG_SPDIF_SCR, SCR_LOW_POWER, 0);
560
561 return 0;
562}
563
564static void fsl_spdif_shutdown(struct snd_pcm_substream *substream,
565 struct snd_soc_dai *cpu_dai)
566{
Kuninori Morimoto9f5f0782020-07-20 10:18:38 +0900567 struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
Kuninori Morimoto17198ae2020-03-23 14:18:30 +0900568 struct fsl_spdif_priv *spdif_priv = snd_soc_dai_get_drvdata(asoc_rtd_to_cpu(rtd, 0));
Nicolin Chena2388a42013-08-21 11:13:16 +0800569 struct regmap *regmap = spdif_priv->regmap;
Shengjiu Wang9cb2b372020-06-19 15:54:33 +0800570 u32 scr, mask;
Nicolin Chena2388a42013-08-21 11:13:16 +0800571
572 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
573 scr = 0;
574 mask = SCR_TXFIFO_AUTOSYNC_MASK | SCR_TXFIFO_CTRL_MASK |
575 SCR_TXSEL_MASK | SCR_USRC_SEL_MASK |
576 SCR_TXFIFO_FSEL_MASK;
Nicolin Chena2388a42013-08-21 11:13:16 +0800577 } else {
578 scr = SCR_RXFIFO_OFF | SCR_RXFIFO_CTL_ZERO;
579 mask = SCR_RXFIFO_FSEL_MASK | SCR_RXFIFO_AUTOSYNC_MASK|
580 SCR_RXFIFO_CTL_MASK | SCR_RXFIFO_OFF_MASK;
Nicolin Chena2388a42013-08-21 11:13:16 +0800581 }
582 regmap_update_bits(regmap, REG_SPDIF_SCR, mask, scr);
583
584 /* Power down SPDIF module only if tx&rx are both inactive */
Kuninori Morimoto1d9fb192020-05-15 09:47:17 +0900585 if (!snd_soc_dai_active(cpu_dai)) {
Nicolin Chena2388a42013-08-21 11:13:16 +0800586 spdif_intr_status_clear(spdif_priv);
587 regmap_update_bits(regmap, REG_SPDIF_SCR,
588 SCR_LOW_POWER, SCR_LOW_POWER);
589 }
590}
591
592static int fsl_spdif_hw_params(struct snd_pcm_substream *substream,
593 struct snd_pcm_hw_params *params,
594 struct snd_soc_dai *dai)
595{
Kuninori Morimoto9f5f0782020-07-20 10:18:38 +0900596 struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
Kuninori Morimoto17198ae2020-03-23 14:18:30 +0900597 struct fsl_spdif_priv *spdif_priv = snd_soc_dai_get_drvdata(asoc_rtd_to_cpu(rtd, 0));
Nicolin Chena2388a42013-08-21 11:13:16 +0800598 struct spdif_mixer_control *ctrl = &spdif_priv->fsl_spdif_control;
599 struct platform_device *pdev = spdif_priv->pdev;
600 u32 sample_rate = params_rate(params);
601 int ret = 0;
602
603 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
604 ret = spdif_set_sample_rate(substream, sample_rate);
605 if (ret) {
606 dev_err(&pdev->dev, "%s: set sample rate failed: %d\n",
607 __func__, sample_rate);
608 return ret;
609 }
610 spdif_set_cstatus(ctrl, IEC958_AES3_CON_CLOCK,
Nicolin Chenf3a30ba2014-05-06 16:42:25 +0800611 IEC958_AES3_CON_CLOCK_1000PPM);
Nicolin Chena2388a42013-08-21 11:13:16 +0800612 spdif_write_channel_status(spdif_priv);
613 } else {
614 /* Setup rx clock source */
615 ret = spdif_set_rx_clksrc(spdif_priv, SPDIF_DEFAULT_GAINSEL, 1);
616 }
617
618 return ret;
619}
620
621static int fsl_spdif_trigger(struct snd_pcm_substream *substream,
622 int cmd, struct snd_soc_dai *dai)
623{
Kuninori Morimoto9f5f0782020-07-20 10:18:38 +0900624 struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
Kuninori Morimoto17198ae2020-03-23 14:18:30 +0900625 struct fsl_spdif_priv *spdif_priv = snd_soc_dai_get_drvdata(asoc_rtd_to_cpu(rtd, 0));
Nicolin Chena2388a42013-08-21 11:13:16 +0800626 struct regmap *regmap = spdif_priv->regmap;
Nicolin Chenf3a30ba2014-05-06 16:42:25 +0800627 bool tx = substream->stream == SNDRV_PCM_STREAM_PLAYBACK;
628 u32 intr = SIE_INTR_FOR(tx);
629 u32 dmaen = SCR_DMA_xX_EN(tx);
Nicolin Chena2388a42013-08-21 11:13:16 +0800630
631 switch (cmd) {
632 case SNDRV_PCM_TRIGGER_START:
633 case SNDRV_PCM_TRIGGER_RESUME:
634 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
635 regmap_update_bits(regmap, REG_SPDIF_SIE, intr, intr);
636 regmap_update_bits(regmap, REG_SPDIF_SCR, dmaen, dmaen);
637 break;
638 case SNDRV_PCM_TRIGGER_STOP:
639 case SNDRV_PCM_TRIGGER_SUSPEND:
640 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
641 regmap_update_bits(regmap, REG_SPDIF_SCR, dmaen, 0);
642 regmap_update_bits(regmap, REG_SPDIF_SIE, intr, 0);
643 break;
644 default:
645 return -EINVAL;
646 }
647
648 return 0;
649}
650
Gustavo A. R. Silva06305d72017-07-13 02:14:21 -0500651static const struct snd_soc_dai_ops fsl_spdif_dai_ops = {
Nicolin Chena2388a42013-08-21 11:13:16 +0800652 .startup = fsl_spdif_startup,
653 .hw_params = fsl_spdif_hw_params,
654 .trigger = fsl_spdif_trigger,
655 .shutdown = fsl_spdif_shutdown,
656};
657
658
659/*
Nicolin Chena2388a42013-08-21 11:13:16 +0800660 * FSL SPDIF IEC958 controller(mixer) functions
661 *
662 * Channel status get/put control
663 * User bit value get/put control
664 * Valid bit value get control
665 * DPLL lock status get control
666 * User bit sync mode selection control
Nicolin Chena2388a42013-08-21 11:13:16 +0800667 */
668
669static int fsl_spdif_info(struct snd_kcontrol *kcontrol,
670 struct snd_ctl_elem_info *uinfo)
671{
672 uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
673 uinfo->count = 1;
674
675 return 0;
676}
677
678static int fsl_spdif_pb_get(struct snd_kcontrol *kcontrol,
679 struct snd_ctl_elem_value *uvalue)
680{
681 struct snd_soc_dai *cpu_dai = snd_kcontrol_chip(kcontrol);
682 struct fsl_spdif_priv *spdif_priv = snd_soc_dai_get_drvdata(cpu_dai);
683 struct spdif_mixer_control *ctrl = &spdif_priv->fsl_spdif_control;
684
685 uvalue->value.iec958.status[0] = ctrl->ch_status[0];
686 uvalue->value.iec958.status[1] = ctrl->ch_status[1];
687 uvalue->value.iec958.status[2] = ctrl->ch_status[2];
688 uvalue->value.iec958.status[3] = ctrl->ch_status[3];
689
690 return 0;
691}
692
693static int fsl_spdif_pb_put(struct snd_kcontrol *kcontrol,
694 struct snd_ctl_elem_value *uvalue)
695{
696 struct snd_soc_dai *cpu_dai = snd_kcontrol_chip(kcontrol);
697 struct fsl_spdif_priv *spdif_priv = snd_soc_dai_get_drvdata(cpu_dai);
698 struct spdif_mixer_control *ctrl = &spdif_priv->fsl_spdif_control;
699
700 ctrl->ch_status[0] = uvalue->value.iec958.status[0];
701 ctrl->ch_status[1] = uvalue->value.iec958.status[1];
702 ctrl->ch_status[2] = uvalue->value.iec958.status[2];
703 ctrl->ch_status[3] = uvalue->value.iec958.status[3];
704
705 spdif_write_channel_status(spdif_priv);
706
707 return 0;
708}
709
710/* Get channel status from SPDIF_RX_CCHAN register */
711static int fsl_spdif_capture_get(struct snd_kcontrol *kcontrol,
712 struct snd_ctl_elem_value *ucontrol)
713{
714 struct snd_soc_dai *cpu_dai = snd_kcontrol_chip(kcontrol);
715 struct fsl_spdif_priv *spdif_priv = snd_soc_dai_get_drvdata(cpu_dai);
716 struct regmap *regmap = spdif_priv->regmap;
717 u32 cstatus, val;
718
719 regmap_read(regmap, REG_SPDIF_SIS, &val);
Nicolin Chenf3a30ba2014-05-06 16:42:25 +0800720 if (!(val & INT_CNEW))
Nicolin Chena2388a42013-08-21 11:13:16 +0800721 return -EAGAIN;
Nicolin Chena2388a42013-08-21 11:13:16 +0800722
723 regmap_read(regmap, REG_SPDIF_SRCSH, &cstatus);
724 ucontrol->value.iec958.status[0] = (cstatus >> 16) & 0xFF;
725 ucontrol->value.iec958.status[1] = (cstatus >> 8) & 0xFF;
726 ucontrol->value.iec958.status[2] = cstatus & 0xFF;
727
728 regmap_read(regmap, REG_SPDIF_SRCSL, &cstatus);
729 ucontrol->value.iec958.status[3] = (cstatus >> 16) & 0xFF;
730 ucontrol->value.iec958.status[4] = (cstatus >> 8) & 0xFF;
731 ucontrol->value.iec958.status[5] = cstatus & 0xFF;
732
733 /* Clear intr */
734 regmap_write(regmap, REG_SPDIF_SIC, INT_CNEW);
735
736 return 0;
737}
738
739/*
740 * Get User bits (subcode) from chip value which readed out
741 * in UChannel register.
742 */
743static int fsl_spdif_subcode_get(struct snd_kcontrol *kcontrol,
744 struct snd_ctl_elem_value *ucontrol)
745{
746 struct snd_soc_dai *cpu_dai = snd_kcontrol_chip(kcontrol);
747 struct fsl_spdif_priv *spdif_priv = snd_soc_dai_get_drvdata(cpu_dai);
748 struct spdif_mixer_control *ctrl = &spdif_priv->fsl_spdif_control;
749 unsigned long flags;
Nicolin Chenf3a30ba2014-05-06 16:42:25 +0800750 int ret = -EAGAIN;
Nicolin Chena2388a42013-08-21 11:13:16 +0800751
752 spin_lock_irqsave(&ctrl->ctl_lock, flags);
753 if (ctrl->ready_buf) {
754 int idx = (ctrl->ready_buf - 1) * SPDIF_UBITS_SIZE;
755 memcpy(&ucontrol->value.iec958.subcode[0],
756 &ctrl->subcode[idx], SPDIF_UBITS_SIZE);
Nicolin Chenf3a30ba2014-05-06 16:42:25 +0800757 ret = 0;
Nicolin Chena2388a42013-08-21 11:13:16 +0800758 }
759 spin_unlock_irqrestore(&ctrl->ctl_lock, flags);
760
761 return ret;
762}
763
Xiubo Lidcfcf2c2015-08-12 14:38:18 +0800764/* Q-subcode information. The byte size is SPDIF_UBITS_SIZE/8 */
Nicolin Chena2388a42013-08-21 11:13:16 +0800765static int fsl_spdif_qinfo(struct snd_kcontrol *kcontrol,
766 struct snd_ctl_elem_info *uinfo)
767{
768 uinfo->type = SNDRV_CTL_ELEM_TYPE_BYTES;
769 uinfo->count = SPDIF_QSUB_SIZE;
770
771 return 0;
772}
773
774/* Get Q subcode from chip value which readed out in QChannel register */
775static int fsl_spdif_qget(struct snd_kcontrol *kcontrol,
776 struct snd_ctl_elem_value *ucontrol)
777{
778 struct snd_soc_dai *cpu_dai = snd_kcontrol_chip(kcontrol);
779 struct fsl_spdif_priv *spdif_priv = snd_soc_dai_get_drvdata(cpu_dai);
780 struct spdif_mixer_control *ctrl = &spdif_priv->fsl_spdif_control;
781 unsigned long flags;
Nicolin Chenf3a30ba2014-05-06 16:42:25 +0800782 int ret = -EAGAIN;
Nicolin Chena2388a42013-08-21 11:13:16 +0800783
784 spin_lock_irqsave(&ctrl->ctl_lock, flags);
785 if (ctrl->ready_buf) {
786 int idx = (ctrl->ready_buf - 1) * SPDIF_QSUB_SIZE;
787 memcpy(&ucontrol->value.bytes.data[0],
788 &ctrl->qsub[idx], SPDIF_QSUB_SIZE);
Nicolin Chenf3a30ba2014-05-06 16:42:25 +0800789 ret = 0;
Nicolin Chena2388a42013-08-21 11:13:16 +0800790 }
791 spin_unlock_irqrestore(&ctrl->ctl_lock, flags);
792
793 return ret;
794}
795
Xiubo Lidcfcf2c2015-08-12 14:38:18 +0800796/* Valid bit information */
Nicolin Chena2388a42013-08-21 11:13:16 +0800797static int fsl_spdif_vbit_info(struct snd_kcontrol *kcontrol,
798 struct snd_ctl_elem_info *uinfo)
799{
800 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
801 uinfo->count = 1;
802 uinfo->value.integer.min = 0;
803 uinfo->value.integer.max = 1;
804
805 return 0;
806}
807
808/* Get valid good bit from interrupt status register */
Shengjiu Wangaa3fce52020-07-07 16:54:26 +0800809static int fsl_spdif_rx_vbit_get(struct snd_kcontrol *kcontrol,
810 struct snd_ctl_elem_value *ucontrol)
Nicolin Chena2388a42013-08-21 11:13:16 +0800811{
812 struct snd_soc_dai *cpu_dai = snd_kcontrol_chip(kcontrol);
813 struct fsl_spdif_priv *spdif_priv = snd_soc_dai_get_drvdata(cpu_dai);
814 struct regmap *regmap = spdif_priv->regmap;
815 u32 val;
816
Nicolin Chene9b383d2014-05-06 16:41:39 +0800817 regmap_read(regmap, REG_SPDIF_SIS, &val);
Nicolin Chena2388a42013-08-21 11:13:16 +0800818 ucontrol->value.integer.value[0] = (val & INT_VAL_NOGOOD) != 0;
819 regmap_write(regmap, REG_SPDIF_SIC, INT_VAL_NOGOOD);
820
821 return 0;
822}
823
Shengjiu Wangaa3fce52020-07-07 16:54:26 +0800824static int fsl_spdif_tx_vbit_get(struct snd_kcontrol *kcontrol,
825 struct snd_ctl_elem_value *ucontrol)
826{
827 struct snd_soc_dai *cpu_dai = snd_kcontrol_chip(kcontrol);
828 struct fsl_spdif_priv *spdif_priv = snd_soc_dai_get_drvdata(cpu_dai);
829 struct regmap *regmap = spdif_priv->regmap;
830 u32 val;
831
832 regmap_read(regmap, REG_SPDIF_SCR, &val);
833 val = (val & SCR_VAL_MASK) >> SCR_VAL_OFFSET;
834 val = 1 - val;
835 ucontrol->value.integer.value[0] = val;
836
837 return 0;
838}
839
840static int fsl_spdif_tx_vbit_put(struct snd_kcontrol *kcontrol,
841 struct snd_ctl_elem_value *ucontrol)
842{
843 struct snd_soc_dai *cpu_dai = snd_kcontrol_chip(kcontrol);
844 struct fsl_spdif_priv *spdif_priv = snd_soc_dai_get_drvdata(cpu_dai);
845 struct regmap *regmap = spdif_priv->regmap;
846 u32 val = (1 - ucontrol->value.integer.value[0]) << SCR_VAL_OFFSET;
847
848 regmap_update_bits(regmap, REG_SPDIF_SCR, SCR_VAL_MASK, val);
849
850 return 0;
851}
852
Xiubo Lidcfcf2c2015-08-12 14:38:18 +0800853/* DPLL lock information */
Nicolin Chena2388a42013-08-21 11:13:16 +0800854static int fsl_spdif_rxrate_info(struct snd_kcontrol *kcontrol,
855 struct snd_ctl_elem_info *uinfo)
856{
857 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
858 uinfo->count = 1;
859 uinfo->value.integer.min = 16000;
860 uinfo->value.integer.max = 96000;
861
862 return 0;
863}
864
865static u32 gainsel_multi[GAINSEL_MULTI_MAX] = {
866 24, 16, 12, 8, 6, 4, 3,
867};
868
869/* Get RX data clock rate given the SPDIF bus_clk */
870static int spdif_get_rxclk_rate(struct fsl_spdif_priv *spdif_priv,
871 enum spdif_gainsel gainsel)
872{
873 struct regmap *regmap = spdif_priv->regmap;
874 struct platform_device *pdev = spdif_priv->pdev;
875 u64 tmpval64, busclk_freq = 0;
876 u32 freqmeas, phaseconf;
877 u8 clksrc;
878
879 regmap_read(regmap, REG_SPDIF_SRFM, &freqmeas);
880 regmap_read(regmap, REG_SPDIF_SRPC, &phaseconf);
881
882 clksrc = (phaseconf >> SRPC_CLKSRC_SEL_OFFSET) & 0xf;
Nicolin Chenf3a30ba2014-05-06 16:42:25 +0800883
884 /* Get bus clock from system */
885 if (srpc_dpll_locked[clksrc] && (phaseconf & SRPC_DPLL_LOCKED))
Nicolin Chen0b864392014-04-28 23:07:51 +0800886 busclk_freq = clk_get_rate(spdif_priv->sysclk);
Nicolin Chena2388a42013-08-21 11:13:16 +0800887
888 /* FreqMeas_CLK = (BUS_CLK * FreqMeas) / 2 ^ 10 / GAINSEL / 128 */
889 tmpval64 = (u64) busclk_freq * freqmeas;
890 do_div(tmpval64, gainsel_multi[gainsel] * 1024);
891 do_div(tmpval64, 128 * 1024);
892
893 dev_dbg(&pdev->dev, "FreqMeas: %d\n", freqmeas);
894 dev_dbg(&pdev->dev, "BusclkFreq: %lld\n", busclk_freq);
895 dev_dbg(&pdev->dev, "RxRate: %lld\n", tmpval64);
896
897 return (int)tmpval64;
898}
899
900/*
901 * Get DPLL lock or not info from stable interrupt status register.
902 * User application must use this control to get locked,
903 * then can do next PCM operation
904 */
905static int fsl_spdif_rxrate_get(struct snd_kcontrol *kcontrol,
906 struct snd_ctl_elem_value *ucontrol)
907{
908 struct snd_soc_dai *cpu_dai = snd_kcontrol_chip(kcontrol);
909 struct fsl_spdif_priv *spdif_priv = snd_soc_dai_get_drvdata(cpu_dai);
Nicolin Chenf3a30ba2014-05-06 16:42:25 +0800910 int rate = 0;
Nicolin Chena2388a42013-08-21 11:13:16 +0800911
912 if (spdif_priv->dpll_locked)
Nicolin Chenf3a30ba2014-05-06 16:42:25 +0800913 rate = spdif_get_rxclk_rate(spdif_priv, SPDIF_DEFAULT_GAINSEL);
914
915 ucontrol->value.integer.value[0] = rate;
Nicolin Chena2388a42013-08-21 11:13:16 +0800916
917 return 0;
918}
919
920/* User bit sync mode info */
921static int fsl_spdif_usync_info(struct snd_kcontrol *kcontrol,
922 struct snd_ctl_elem_info *uinfo)
923{
924 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
925 uinfo->count = 1;
926 uinfo->value.integer.min = 0;
927 uinfo->value.integer.max = 1;
928
929 return 0;
930}
931
932/*
933 * User bit sync mode:
934 * 1 CD User channel subcode
935 * 0 Non-CD data
936 */
937static int fsl_spdif_usync_get(struct snd_kcontrol *kcontrol,
938 struct snd_ctl_elem_value *ucontrol)
939{
940 struct snd_soc_dai *cpu_dai = snd_kcontrol_chip(kcontrol);
941 struct fsl_spdif_priv *spdif_priv = snd_soc_dai_get_drvdata(cpu_dai);
942 struct regmap *regmap = spdif_priv->regmap;
943 u32 val;
944
945 regmap_read(regmap, REG_SPDIF_SRCD, &val);
946 ucontrol->value.integer.value[0] = (val & SRCD_CD_USER) != 0;
947
948 return 0;
949}
950
951/*
952 * User bit sync mode:
953 * 1 CD User channel subcode
954 * 0 Non-CD data
955 */
956static int fsl_spdif_usync_put(struct snd_kcontrol *kcontrol,
957 struct snd_ctl_elem_value *ucontrol)
958{
959 struct snd_soc_dai *cpu_dai = snd_kcontrol_chip(kcontrol);
960 struct fsl_spdif_priv *spdif_priv = snd_soc_dai_get_drvdata(cpu_dai);
961 struct regmap *regmap = spdif_priv->regmap;
962 u32 val = ucontrol->value.integer.value[0] << SRCD_CD_USER_OFFSET;
963
964 regmap_update_bits(regmap, REG_SPDIF_SRCD, SRCD_CD_USER, val);
965
966 return 0;
967}
968
969/* FSL SPDIF IEC958 controller defines */
970static struct snd_kcontrol_new fsl_spdif_ctrls[] = {
971 /* Status cchanel controller */
972 {
973 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
974 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, DEFAULT),
975 .access = SNDRV_CTL_ELEM_ACCESS_READ |
976 SNDRV_CTL_ELEM_ACCESS_WRITE |
977 SNDRV_CTL_ELEM_ACCESS_VOLATILE,
978 .info = fsl_spdif_info,
979 .get = fsl_spdif_pb_get,
980 .put = fsl_spdif_pb_put,
981 },
982 {
983 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
984 .name = SNDRV_CTL_NAME_IEC958("", CAPTURE, DEFAULT),
985 .access = SNDRV_CTL_ELEM_ACCESS_READ |
986 SNDRV_CTL_ELEM_ACCESS_VOLATILE,
987 .info = fsl_spdif_info,
988 .get = fsl_spdif_capture_get,
989 },
990 /* User bits controller */
991 {
992 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
993 .name = "IEC958 Subcode Capture Default",
994 .access = SNDRV_CTL_ELEM_ACCESS_READ |
995 SNDRV_CTL_ELEM_ACCESS_VOLATILE,
996 .info = fsl_spdif_info,
997 .get = fsl_spdif_subcode_get,
998 },
999 {
1000 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
1001 .name = "IEC958 Q-subcode Capture Default",
1002 .access = SNDRV_CTL_ELEM_ACCESS_READ |
1003 SNDRV_CTL_ELEM_ACCESS_VOLATILE,
1004 .info = fsl_spdif_qinfo,
1005 .get = fsl_spdif_qget,
1006 },
1007 /* Valid bit error controller */
1008 {
1009 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
Shengjiu Wangaa3fce52020-07-07 16:54:26 +08001010 .name = "IEC958 RX V-Bit Errors",
Nicolin Chena2388a42013-08-21 11:13:16 +08001011 .access = SNDRV_CTL_ELEM_ACCESS_READ |
1012 SNDRV_CTL_ELEM_ACCESS_VOLATILE,
1013 .info = fsl_spdif_vbit_info,
Shengjiu Wangaa3fce52020-07-07 16:54:26 +08001014 .get = fsl_spdif_rx_vbit_get,
1015 },
1016 {
1017 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
1018 .name = "IEC958 TX V-Bit",
1019 .access = SNDRV_CTL_ELEM_ACCESS_READ |
1020 SNDRV_CTL_ELEM_ACCESS_WRITE |
1021 SNDRV_CTL_ELEM_ACCESS_VOLATILE,
1022 .info = fsl_spdif_vbit_info,
1023 .get = fsl_spdif_tx_vbit_get,
1024 .put = fsl_spdif_tx_vbit_put,
Nicolin Chena2388a42013-08-21 11:13:16 +08001025 },
1026 /* DPLL lock info get controller */
1027 {
1028 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
1029 .name = "RX Sample Rate",
1030 .access = SNDRV_CTL_ELEM_ACCESS_READ |
1031 SNDRV_CTL_ELEM_ACCESS_VOLATILE,
1032 .info = fsl_spdif_rxrate_info,
1033 .get = fsl_spdif_rxrate_get,
1034 },
1035 /* User bit sync mode set/get controller */
1036 {
1037 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
1038 .name = "IEC958 USyncMode CDText",
1039 .access = SNDRV_CTL_ELEM_ACCESS_READ |
1040 SNDRV_CTL_ELEM_ACCESS_WRITE |
1041 SNDRV_CTL_ELEM_ACCESS_VOLATILE,
1042 .info = fsl_spdif_usync_info,
1043 .get = fsl_spdif_usync_get,
1044 .put = fsl_spdif_usync_put,
1045 },
1046};
1047
1048static int fsl_spdif_dai_probe(struct snd_soc_dai *dai)
1049{
1050 struct fsl_spdif_priv *spdif_private = snd_soc_dai_get_drvdata(dai);
1051
Xiubo Li05cf4822014-01-20 15:27:26 +08001052 snd_soc_dai_init_dma_data(dai, &spdif_private->dma_params_tx,
1053 &spdif_private->dma_params_rx);
Nicolin Chena2388a42013-08-21 11:13:16 +08001054
1055 snd_soc_add_dai_controls(dai, fsl_spdif_ctrls, ARRAY_SIZE(fsl_spdif_ctrls));
1056
Shengjiu Wang055b0822020-07-07 16:54:25 +08001057 /*Clear the val bit for Tx*/
1058 regmap_update_bits(spdif_private->regmap, REG_SPDIF_SCR,
1059 SCR_VAL_MASK, SCR_VAL_CLEAR);
1060
Nicolin Chena2388a42013-08-21 11:13:16 +08001061 return 0;
1062}
1063
Mark Brown6b4c80f2013-08-31 16:40:51 +01001064static struct snd_soc_dai_driver fsl_spdif_dai = {
Nicolin Chena2388a42013-08-21 11:13:16 +08001065 .probe = &fsl_spdif_dai_probe,
1066 .playback = {
Nicolin Chen75640932014-07-30 11:10:28 +08001067 .stream_name = "CPU-Playback",
Nicolin Chena2388a42013-08-21 11:13:16 +08001068 .channels_min = 2,
1069 .channels_max = 2,
1070 .rates = FSL_SPDIF_RATES_PLAYBACK,
1071 .formats = FSL_SPDIF_FORMATS_PLAYBACK,
1072 },
1073 .capture = {
Nicolin Chen75640932014-07-30 11:10:28 +08001074 .stream_name = "CPU-Capture",
Nicolin Chena2388a42013-08-21 11:13:16 +08001075 .channels_min = 2,
1076 .channels_max = 2,
1077 .rates = FSL_SPDIF_RATES_CAPTURE,
1078 .formats = FSL_SPDIF_FORMATS_CAPTURE,
1079 },
1080 .ops = &fsl_spdif_dai_ops,
1081};
1082
1083static const struct snd_soc_component_driver fsl_spdif_component = {
1084 .name = "fsl-spdif",
1085};
1086
Fabio Estevam6d22db42013-08-23 18:14:46 -03001087/* FSL SPDIF REGMAP */
Zidan Wangf9f4fa62015-09-18 11:09:11 +08001088static const struct reg_default fsl_spdif_reg_defaults[] = {
Zidan Wang9f1206d2015-10-26 15:19:04 +08001089 {REG_SPDIF_SCR, 0x00000400},
1090 {REG_SPDIF_SRCD, 0x00000000},
1091 {REG_SPDIF_SIE, 0x00000000},
1092 {REG_SPDIF_STL, 0x00000000},
1093 {REG_SPDIF_STR, 0x00000000},
1094 {REG_SPDIF_STCSCH, 0x00000000},
1095 {REG_SPDIF_STCSCL, 0x00000000},
1096 {REG_SPDIF_STC, 0x00020f00},
Zidan Wangf9f4fa62015-09-18 11:09:11 +08001097};
Nicolin Chena2388a42013-08-21 11:13:16 +08001098
1099static bool fsl_spdif_readable_reg(struct device *dev, unsigned int reg)
1100{
1101 switch (reg) {
1102 case REG_SPDIF_SCR:
1103 case REG_SPDIF_SRCD:
1104 case REG_SPDIF_SRPC:
1105 case REG_SPDIF_SIE:
1106 case REG_SPDIF_SIS:
1107 case REG_SPDIF_SRL:
1108 case REG_SPDIF_SRR:
1109 case REG_SPDIF_SRCSH:
1110 case REG_SPDIF_SRCSL:
1111 case REG_SPDIF_SRU:
1112 case REG_SPDIF_SRQ:
1113 case REG_SPDIF_STCSCH:
1114 case REG_SPDIF_STCSCL:
1115 case REG_SPDIF_SRFM:
1116 case REG_SPDIF_STC:
1117 return true;
1118 default:
1119 return false;
Sachin Kamate19bcb62013-09-13 15:52:42 +05301120 }
Nicolin Chena2388a42013-08-21 11:13:16 +08001121}
1122
Zidan Wangf9f4fa62015-09-18 11:09:11 +08001123static bool fsl_spdif_volatile_reg(struct device *dev, unsigned int reg)
1124{
1125 switch (reg) {
1126 case REG_SPDIF_SRPC:
1127 case REG_SPDIF_SIS:
1128 case REG_SPDIF_SRL:
1129 case REG_SPDIF_SRR:
1130 case REG_SPDIF_SRCSH:
1131 case REG_SPDIF_SRCSL:
1132 case REG_SPDIF_SRU:
1133 case REG_SPDIF_SRQ:
Zidan Wangf9f4fa62015-09-18 11:09:11 +08001134 case REG_SPDIF_SRFM:
1135 return true;
1136 default:
1137 return false;
1138 }
1139}
1140
Nicolin Chena2388a42013-08-21 11:13:16 +08001141static bool fsl_spdif_writeable_reg(struct device *dev, unsigned int reg)
1142{
1143 switch (reg) {
1144 case REG_SPDIF_SCR:
1145 case REG_SPDIF_SRCD:
1146 case REG_SPDIF_SRPC:
1147 case REG_SPDIF_SIE:
1148 case REG_SPDIF_SIC:
1149 case REG_SPDIF_STL:
1150 case REG_SPDIF_STR:
1151 case REG_SPDIF_STCSCH:
1152 case REG_SPDIF_STCSCL:
1153 case REG_SPDIF_STC:
1154 return true;
1155 default:
1156 return false;
Sachin Kamate19bcb62013-09-13 15:52:42 +05301157 }
Nicolin Chena2388a42013-08-21 11:13:16 +08001158}
1159
Xiubo Li66491502014-08-25 11:31:01 +08001160static const struct regmap_config fsl_spdif_regmap_config = {
Nicolin Chena2388a42013-08-21 11:13:16 +08001161 .reg_bits = 32,
1162 .reg_stride = 4,
1163 .val_bits = 32,
1164
1165 .max_register = REG_SPDIF_STC,
Zidan Wangf9f4fa62015-09-18 11:09:11 +08001166 .reg_defaults = fsl_spdif_reg_defaults,
1167 .num_reg_defaults = ARRAY_SIZE(fsl_spdif_reg_defaults),
Nicolin Chena2388a42013-08-21 11:13:16 +08001168 .readable_reg = fsl_spdif_readable_reg,
Zidan Wangf9f4fa62015-09-18 11:09:11 +08001169 .volatile_reg = fsl_spdif_volatile_reg,
Nicolin Chena2388a42013-08-21 11:13:16 +08001170 .writeable_reg = fsl_spdif_writeable_reg,
Marek Vasut35ddb152016-09-19 21:30:27 +02001171 .cache_type = REGCACHE_FLAT,
Nicolin Chena2388a42013-08-21 11:13:16 +08001172};
1173
1174static u32 fsl_spdif_txclk_caldiv(struct fsl_spdif_priv *spdif_priv,
1175 struct clk *clk, u64 savesub,
Nicolin Chen9c6344b2014-04-30 18:54:05 +08001176 enum spdif_txrate index, bool round)
Nicolin Chena2388a42013-08-21 11:13:16 +08001177{
Colin Ian King2db5f9e2017-09-03 14:54:08 +01001178 static const u32 rate[] = { 32000, 44100, 48000, 96000, 192000 };
Shawn Guo81efec82015-02-25 22:53:37 +08001179 bool is_sysclk = clk_is_match(clk, spdif_priv->sysclk);
Nicolin Chena2388a42013-08-21 11:13:16 +08001180 u64 rate_ideal, rate_actual, sub;
Viorel Suman22316092019-02-18 15:25:00 +00001181 u32 arate;
1182 u16 sysclk_dfmin, sysclk_dfmax, sysclk_df;
1183 u8 txclk_df;
Nicolin Chena2388a42013-08-21 11:13:16 +08001184
Nicolin Chen27c647b2014-04-30 18:54:07 +08001185 /* The sysclk has an extra divisor [2, 512] */
1186 sysclk_dfmin = is_sysclk ? 2 : 1;
1187 sysclk_dfmax = is_sysclk ? 512 : 1;
Nicolin Chena2388a42013-08-21 11:13:16 +08001188
Nicolin Chen27c647b2014-04-30 18:54:07 +08001189 for (sysclk_df = sysclk_dfmin; sysclk_df <= sysclk_dfmax; sysclk_df++) {
1190 for (txclk_df = 1; txclk_df <= 128; txclk_df++) {
Gustavo A. R. Silvab999a7a2018-07-04 09:18:33 -05001191 rate_ideal = rate[index] * txclk_df * 64ULL;
Nicolin Chen27c647b2014-04-30 18:54:07 +08001192 if (round)
1193 rate_actual = clk_round_rate(clk, rate_ideal);
1194 else
1195 rate_actual = clk_get_rate(clk);
Nicolin Chena2388a42013-08-21 11:13:16 +08001196
Nicolin Chen27c647b2014-04-30 18:54:07 +08001197 arate = rate_actual / 64;
1198 arate /= txclk_df * sysclk_df;
1199
1200 if (arate == rate[index]) {
1201 /* We are lucky */
1202 savesub = 0;
1203 spdif_priv->txclk_df[index] = txclk_df;
1204 spdif_priv->sysclk_df[index] = sysclk_df;
Nicolin Chen527cda72014-04-30 18:54:08 +08001205 spdif_priv->txrate[index] = arate;
Nicolin Chen27c647b2014-04-30 18:54:07 +08001206 goto out;
1207 } else if (arate / rate[index] == 1) {
1208 /* A little bigger than expect */
Anssi Hannulac89c7e92014-06-09 19:16:43 +03001209 sub = (u64)(arate - rate[index]) * 100000;
Nicolin Chen27c647b2014-04-30 18:54:07 +08001210 do_div(sub, rate[index]);
1211 if (sub >= savesub)
1212 continue;
Nicolin Chena2388a42013-08-21 11:13:16 +08001213 savesub = sub;
Nicolin Chene41a4a72014-04-30 18:54:06 +08001214 spdif_priv->txclk_df[index] = txclk_df;
Nicolin Chen27c647b2014-04-30 18:54:07 +08001215 spdif_priv->sysclk_df[index] = sysclk_df;
Nicolin Chen527cda72014-04-30 18:54:08 +08001216 spdif_priv->txrate[index] = arate;
Nicolin Chen27c647b2014-04-30 18:54:07 +08001217 } else if (rate[index] / arate == 1) {
1218 /* A little smaller than expect */
Anssi Hannulac89c7e92014-06-09 19:16:43 +03001219 sub = (u64)(rate[index] - arate) * 100000;
Nicolin Chen27c647b2014-04-30 18:54:07 +08001220 do_div(sub, rate[index]);
1221 if (sub >= savesub)
1222 continue;
Nicolin Chena2388a42013-08-21 11:13:16 +08001223 savesub = sub;
Nicolin Chene41a4a72014-04-30 18:54:06 +08001224 spdif_priv->txclk_df[index] = txclk_df;
Nicolin Chen27c647b2014-04-30 18:54:07 +08001225 spdif_priv->sysclk_df[index] = sysclk_df;
Nicolin Chen527cda72014-04-30 18:54:08 +08001226 spdif_priv->txrate[index] = arate;
Nicolin Chena2388a42013-08-21 11:13:16 +08001227 }
1228 }
1229 }
1230
Nicolin Chen27c647b2014-04-30 18:54:07 +08001231out:
Nicolin Chena2388a42013-08-21 11:13:16 +08001232 return savesub;
1233}
1234
1235static int fsl_spdif_probe_txclk(struct fsl_spdif_priv *spdif_priv,
1236 enum spdif_txrate index)
1237{
Colin Ian King2db5f9e2017-09-03 14:54:08 +01001238 static const u32 rate[] = { 32000, 44100, 48000, 96000, 192000 };
Nicolin Chena2388a42013-08-21 11:13:16 +08001239 struct platform_device *pdev = spdif_priv->pdev;
1240 struct device *dev = &pdev->dev;
1241 u64 savesub = 100000, ret;
1242 struct clk *clk;
1243 char tmp[16];
1244 int i;
1245
1246 for (i = 0; i < STC_TXCLK_SRC_MAX; i++) {
1247 sprintf(tmp, "rxtx%d", i);
1248 clk = devm_clk_get(&pdev->dev, tmp);
1249 if (IS_ERR(clk)) {
1250 dev_err(dev, "no rxtx%d clock in devicetree\n", i);
1251 return PTR_ERR(clk);
1252 }
1253 if (!clk_get_rate(clk))
1254 continue;
1255
Nicolin Chen9c6344b2014-04-30 18:54:05 +08001256 ret = fsl_spdif_txclk_caldiv(spdif_priv, clk, savesub, index,
Shengjiu Wangf61b9272020-06-17 14:58:01 +08001257 fsl_spdif_can_set_clk_rate(spdif_priv, i));
Nicolin Chena2388a42013-08-21 11:13:16 +08001258 if (savesub == ret)
1259 continue;
1260
1261 savesub = ret;
1262 spdif_priv->txclk[index] = clk;
1263 spdif_priv->txclk_src[index] = i;
1264
1265 /* To quick catch a divisor, we allow a 0.1% deviation */
1266 if (savesub < 100)
1267 break;
1268 }
1269
Nicolin Chen8a309d72013-08-30 17:38:08 +08001270 dev_dbg(&pdev->dev, "use rxtx%d as tx clock source for %dHz sample rate\n",
Nicolin Chena2388a42013-08-21 11:13:16 +08001271 spdif_priv->txclk_src[index], rate[index]);
Nicolin Chene41a4a72014-04-30 18:54:06 +08001272 dev_dbg(&pdev->dev, "use txclk df %d for %dHz sample rate\n",
1273 spdif_priv->txclk_df[index], rate[index]);
Shawn Guo81efec82015-02-25 22:53:37 +08001274 if (clk_is_match(spdif_priv->txclk[index], spdif_priv->sysclk))
Nicolin Chen27c647b2014-04-30 18:54:07 +08001275 dev_dbg(&pdev->dev, "use sysclk df %d for %dHz sample rate\n",
1276 spdif_priv->sysclk_df[index], rate[index]);
Nicolin Chen527cda72014-04-30 18:54:08 +08001277 dev_dbg(&pdev->dev, "the best rate for %dHz sample rate is %dHz\n",
1278 rate[index], spdif_priv->txrate[index]);
Nicolin Chena2388a42013-08-21 11:13:16 +08001279
1280 return 0;
1281}
1282
1283static int fsl_spdif_probe(struct platform_device *pdev)
1284{
Nicolin Chena2388a42013-08-21 11:13:16 +08001285 struct fsl_spdif_priv *spdif_priv;
1286 struct spdif_mixer_control *ctrl;
1287 struct resource *res;
1288 void __iomem *regs;
1289 int irq, ret, i;
1290
Fabio Estevam7c27ba42014-12-29 23:52:35 -02001291 spdif_priv = devm_kzalloc(&pdev->dev, sizeof(*spdif_priv), GFP_KERNEL);
Nicolin Chena2388a42013-08-21 11:13:16 +08001292 if (!spdif_priv)
1293 return -ENOMEM;
1294
Nicolin Chena2388a42013-08-21 11:13:16 +08001295 spdif_priv->pdev = pdev;
1296
Shengjiu Wangf61b9272020-06-17 14:58:01 +08001297 spdif_priv->soc = of_device_get_match_data(&pdev->dev);
1298 if (!spdif_priv->soc) {
1299 dev_err(&pdev->dev, "failed to get soc data\n");
1300 return -ENODEV;
1301 }
1302
Nicolin Chena2388a42013-08-21 11:13:16 +08001303 /* Initialize this copy of the CPU DAI driver structure */
1304 memcpy(&spdif_priv->cpu_dai_drv, &fsl_spdif_dai, sizeof(fsl_spdif_dai));
Fabio Estevam7c27ba42014-12-29 23:52:35 -02001305 spdif_priv->cpu_dai_drv.name = dev_name(&pdev->dev);
Shengjiu Wang516232e2020-10-15 13:28:48 +08001306 spdif_priv->cpu_dai_drv.playback.formats =
1307 spdif_priv->soc->tx_formats;
Nicolin Chena2388a42013-08-21 11:13:16 +08001308
1309 /* Get the addresses and IRQ */
1310 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
Nicolin Chena2388a42013-08-21 11:13:16 +08001311 regs = devm_ioremap_resource(&pdev->dev, res);
Wei Yongjunbfd7d1a2013-08-29 08:00:05 +08001312 if (IS_ERR(regs))
Nicolin Chena2388a42013-08-21 11:13:16 +08001313 return PTR_ERR(regs);
Nicolin Chena2388a42013-08-21 11:13:16 +08001314
1315 spdif_priv->regmap = devm_regmap_init_mmio_clk(&pdev->dev,
1316 "core", regs, &fsl_spdif_regmap_config);
1317 if (IS_ERR(spdif_priv->regmap)) {
1318 dev_err(&pdev->dev, "regmap init failed\n");
1319 return PTR_ERR(spdif_priv->regmap);
1320 }
1321
Shengjiu Wang516232e2020-10-15 13:28:48 +08001322 for (i = 0; i < spdif_priv->soc->interrupts; i++) {
1323 irq = platform_get_irq(pdev, i);
1324 if (irq < 0) {
1325 dev_err(&pdev->dev, "no irq for node %s\n", pdev->name);
1326 return irq;
1327 }
Nicolin Chena2388a42013-08-21 11:13:16 +08001328
Shengjiu Wang516232e2020-10-15 13:28:48 +08001329 ret = devm_request_irq(&pdev->dev, irq, spdif_isr, 0,
1330 dev_name(&pdev->dev), spdif_priv);
1331 if (ret) {
1332 dev_err(&pdev->dev, "could not claim irq %u\n", irq);
1333 return ret;
1334 }
Nicolin Chena2388a42013-08-21 11:13:16 +08001335 }
1336
Nicolin Chen0b864392014-04-28 23:07:51 +08001337 /* Get system clock for rx clock rate calculation */
1338 spdif_priv->sysclk = devm_clk_get(&pdev->dev, "rxtx5");
1339 if (IS_ERR(spdif_priv->sysclk)) {
1340 dev_err(&pdev->dev, "no sys clock (rxtx5) in devicetree\n");
1341 return PTR_ERR(spdif_priv->sysclk);
1342 }
1343
Nicolin Chen08f73362014-04-24 18:52:24 +08001344 /* Get core clock for data register access via DMA */
1345 spdif_priv->coreclk = devm_clk_get(&pdev->dev, "core");
1346 if (IS_ERR(spdif_priv->coreclk)) {
1347 dev_err(&pdev->dev, "no core clock in devicetree\n");
1348 return PTR_ERR(spdif_priv->coreclk);
1349 }
1350
Shengjiu Wang0bc56802015-11-24 17:19:33 +08001351 spdif_priv->spbaclk = devm_clk_get(&pdev->dev, "spba");
1352 if (IS_ERR(spdif_priv->spbaclk))
1353 dev_warn(&pdev->dev, "no spba clock in devicetree\n");
1354
Nicolin Chena2388a42013-08-21 11:13:16 +08001355 /* Select clock source for rx/tx clock */
1356 spdif_priv->rxclk = devm_clk_get(&pdev->dev, "rxtx1");
1357 if (IS_ERR(spdif_priv->rxclk)) {
1358 dev_err(&pdev->dev, "no rxtx1 clock in devicetree\n");
1359 return PTR_ERR(spdif_priv->rxclk);
1360 }
1361 spdif_priv->rxclk_src = DEFAULT_RXCLK_SRC;
1362
1363 for (i = 0; i < SPDIF_TXRATE_MAX; i++) {
1364 ret = fsl_spdif_probe_txclk(spdif_priv, i);
1365 if (ret)
1366 return ret;
1367 }
1368
1369 /* Initial spinlock for control data */
1370 ctrl = &spdif_priv->fsl_spdif_control;
1371 spin_lock_init(&ctrl->ctl_lock);
1372
1373 /* Init tx channel status default value */
Nicolin Chenf3a30ba2014-05-06 16:42:25 +08001374 ctrl->ch_status[0] = IEC958_AES0_CON_NOT_COPYRIGHT |
1375 IEC958_AES0_CON_EMPHASIS_5015;
Nicolin Chena2388a42013-08-21 11:13:16 +08001376 ctrl->ch_status[1] = IEC958_AES1_CON_DIGDIGCONV_ID;
1377 ctrl->ch_status[2] = 0x00;
Nicolin Chenf3a30ba2014-05-06 16:42:25 +08001378 ctrl->ch_status[3] = IEC958_AES3_CON_FS_44100 |
1379 IEC958_AES3_CON_CLOCK_1000PPM;
Nicolin Chena2388a42013-08-21 11:13:16 +08001380
1381 spdif_priv->dpll_locked = false;
1382
Shengjiu Wang516232e2020-10-15 13:28:48 +08001383 spdif_priv->dma_params_tx.maxburst = spdif_priv->soc->tx_burst;
1384 spdif_priv->dma_params_rx.maxburst = spdif_priv->soc->rx_burst;
Nicolin Chena2388a42013-08-21 11:13:16 +08001385 spdif_priv->dma_params_tx.addr = res->start + REG_SPDIF_STL;
1386 spdif_priv->dma_params_rx.addr = res->start + REG_SPDIF_SRL;
1387
1388 /* Register with ASoC */
1389 dev_set_drvdata(&pdev->dev, spdif_priv);
Shengjiu Wang9cb2b372020-06-19 15:54:33 +08001390 pm_runtime_enable(&pdev->dev);
1391 regcache_cache_only(spdif_priv->regmap, true);
Nicolin Chena2388a42013-08-21 11:13:16 +08001392
Sachin Kamat256218a2013-09-17 10:13:49 +05301393 ret = devm_snd_soc_register_component(&pdev->dev, &fsl_spdif_component,
1394 &spdif_priv->cpu_dai_drv, 1);
Nicolin Chena2388a42013-08-21 11:13:16 +08001395 if (ret) {
1396 dev_err(&pdev->dev, "failed to register DAI: %d\n", ret);
Fabio Estevam5af407c2013-08-23 18:14:45 -03001397 return ret;
Nicolin Chena2388a42013-08-21 11:13:16 +08001398 }
1399
Shengjiu Wang0d69e0d2015-06-23 18:23:53 +08001400 ret = imx_pcm_dma_init(pdev, IMX_SPDIF_DMABUF_SIZE);
Stefan Agner1aabff22019-01-18 10:06:55 +01001401 if (ret && ret != -EPROBE_DEFER)
Nicolin Chena2388a42013-08-21 11:13:16 +08001402 dev_err(&pdev->dev, "imx_pcm_dma_init failed: %d\n", ret);
Nicolin Chena2388a42013-08-21 11:13:16 +08001403
1404 return ret;
1405}
1406
Shengjiu Wang9cb2b372020-06-19 15:54:33 +08001407#ifdef CONFIG_PM
1408static int fsl_spdif_runtime_suspend(struct device *dev)
Zidan Wangf9f4fa62015-09-18 11:09:11 +08001409{
1410 struct fsl_spdif_priv *spdif_priv = dev_get_drvdata(dev);
Shengjiu Wang9cb2b372020-06-19 15:54:33 +08001411 int i;
Zidan Wangf9f4fa62015-09-18 11:09:11 +08001412
1413 regmap_read(spdif_priv->regmap, REG_SPDIF_SRPC,
1414 &spdif_priv->regcache_srpc);
Zidan Wangf9f4fa62015-09-18 11:09:11 +08001415 regcache_cache_only(spdif_priv->regmap, true);
Shengjiu Wang9cb2b372020-06-19 15:54:33 +08001416
1417 clk_disable_unprepare(spdif_priv->rxclk);
1418
1419 for (i = 0; i < SPDIF_TXRATE_MAX; i++)
1420 clk_disable_unprepare(spdif_priv->txclk[i]);
1421
1422 if (!IS_ERR(spdif_priv->spbaclk))
1423 clk_disable_unprepare(spdif_priv->spbaclk);
1424 clk_disable_unprepare(spdif_priv->coreclk);
Zidan Wangf9f4fa62015-09-18 11:09:11 +08001425
1426 return 0;
1427}
1428
Shengjiu Wang9cb2b372020-06-19 15:54:33 +08001429static int fsl_spdif_runtime_resume(struct device *dev)
Zidan Wangf9f4fa62015-09-18 11:09:11 +08001430{
1431 struct fsl_spdif_priv *spdif_priv = dev_get_drvdata(dev);
Shengjiu Wang9cb2b372020-06-19 15:54:33 +08001432 int ret;
1433 int i;
1434
1435 ret = clk_prepare_enable(spdif_priv->coreclk);
1436 if (ret) {
1437 dev_err(dev, "failed to enable core clock\n");
1438 return ret;
1439 }
1440
1441 if (!IS_ERR(spdif_priv->spbaclk)) {
1442 ret = clk_prepare_enable(spdif_priv->spbaclk);
1443 if (ret) {
1444 dev_err(dev, "failed to enable spba clock\n");
1445 goto disable_core_clk;
1446 }
1447 }
1448
1449 for (i = 0; i < SPDIF_TXRATE_MAX; i++) {
1450 ret = clk_prepare_enable(spdif_priv->txclk[i]);
1451 if (ret)
1452 goto disable_tx_clk;
1453 }
1454
1455 ret = clk_prepare_enable(spdif_priv->rxclk);
1456 if (ret)
1457 goto disable_tx_clk;
Zidan Wangf9f4fa62015-09-18 11:09:11 +08001458
1459 regcache_cache_only(spdif_priv->regmap, false);
Shengjiu Wang9cb2b372020-06-19 15:54:33 +08001460 regcache_mark_dirty(spdif_priv->regmap);
Zidan Wangf9f4fa62015-09-18 11:09:11 +08001461
1462 regmap_update_bits(spdif_priv->regmap, REG_SPDIF_SRPC,
1463 SRPC_CLKSRC_SEL_MASK | SRPC_GAINSEL_MASK,
1464 spdif_priv->regcache_srpc);
1465
Shengjiu Wang9cb2b372020-06-19 15:54:33 +08001466 ret = regcache_sync(spdif_priv->regmap);
1467 if (ret)
1468 goto disable_rx_clk;
1469
1470 return 0;
1471
1472disable_rx_clk:
1473 clk_disable_unprepare(spdif_priv->rxclk);
1474disable_tx_clk:
1475 for (i--; i >= 0; i--)
1476 clk_disable_unprepare(spdif_priv->txclk[i]);
1477 if (!IS_ERR(spdif_priv->spbaclk))
1478 clk_disable_unprepare(spdif_priv->spbaclk);
1479disable_core_clk:
1480 clk_disable_unprepare(spdif_priv->coreclk);
1481
1482 return ret;
Zidan Wangf9f4fa62015-09-18 11:09:11 +08001483}
Shengjiu Wang9cb2b372020-06-19 15:54:33 +08001484#endif /* CONFIG_PM */
Zidan Wangf9f4fa62015-09-18 11:09:11 +08001485
1486static const struct dev_pm_ops fsl_spdif_pm = {
Shengjiu Wang9cb2b372020-06-19 15:54:33 +08001487 SET_SYSTEM_SLEEP_PM_OPS(pm_runtime_force_suspend,
1488 pm_runtime_force_resume)
1489 SET_RUNTIME_PM_OPS(fsl_spdif_runtime_suspend, fsl_spdif_runtime_resume,
1490 NULL)
Zidan Wangf9f4fa62015-09-18 11:09:11 +08001491};
1492
Nicolin Chena2388a42013-08-21 11:13:16 +08001493static const struct of_device_id fsl_spdif_dt_ids[] = {
Shengjiu Wangf61b9272020-06-17 14:58:01 +08001494 { .compatible = "fsl,imx35-spdif", .data = &fsl_spdif_imx35, },
1495 { .compatible = "fsl,vf610-spdif", .data = &fsl_spdif_vf610, },
1496 { .compatible = "fsl,imx6sx-spdif", .data = &fsl_spdif_imx6sx, },
Shengjiu Wang516232e2020-10-15 13:28:48 +08001497 { .compatible = "fsl,imx8qm-spdif", .data = &fsl_spdif_imx8qm, },
Nicolin Chena2388a42013-08-21 11:13:16 +08001498 {}
1499};
1500MODULE_DEVICE_TABLE(of, fsl_spdif_dt_ids);
1501
1502static struct platform_driver fsl_spdif_driver = {
1503 .driver = {
1504 .name = "fsl-spdif-dai",
Nicolin Chena2388a42013-08-21 11:13:16 +08001505 .of_match_table = fsl_spdif_dt_ids,
Zidan Wangf9f4fa62015-09-18 11:09:11 +08001506 .pm = &fsl_spdif_pm,
Nicolin Chena2388a42013-08-21 11:13:16 +08001507 },
1508 .probe = fsl_spdif_probe,
Nicolin Chena2388a42013-08-21 11:13:16 +08001509};
1510
1511module_platform_driver(fsl_spdif_driver);
1512
1513MODULE_AUTHOR("Freescale Semiconductor, Inc.");
1514MODULE_DESCRIPTION("Freescale S/PDIF CPU DAI Driver");
1515MODULE_LICENSE("GPL v2");
1516MODULE_ALIAS("platform:fsl-spdif-dai");