blob: fe4d9e3da793d00821b025c16e50469d9d5e2618 [file] [log] [blame]
Nicolin Chena2388a42013-08-21 11:13:16 +08001/*
2 * Freescale S/PDIF ALSA SoC Digital Audio Interface (DAI) driver
3 *
4 * Copyright (C) 2013 Freescale Semiconductor, Inc.
5 *
6 * Based on stmp3xxx_spdif_dai.c
7 * Vladimir Barinov <vbarinov@embeddedalley.com>
8 * Copyright 2008 SigmaTel, Inc
9 * Copyright 2008 Embedded Alley Solutions, Inc
10 *
11 * This file is licensed under the terms of the GNU General Public License
12 * version 2. This program is licensed "as is" without any warranty of any
13 * kind, whether express or implied.
14 */
15
Xiubo Liadd180e2014-04-04 15:10:27 +080016#include <linux/bitrev.h>
Nicolin Chena2388a42013-08-21 11:13:16 +080017#include <linux/clk.h>
18#include <linux/clk-private.h>
Xiubo Liadd180e2014-04-04 15:10:27 +080019#include <linux/module.h>
Nicolin Chena2388a42013-08-21 11:13:16 +080020#include <linux/of_address.h>
21#include <linux/of_device.h>
22#include <linux/of_irq.h>
Xiubo Liadd180e2014-04-04 15:10:27 +080023#include <linux/regmap.h>
Nicolin Chena2388a42013-08-21 11:13:16 +080024
25#include <sound/asoundef.h>
Nicolin Chena2388a42013-08-21 11:13:16 +080026#include <sound/dmaengine_pcm.h>
Xiubo Liadd180e2014-04-04 15:10:27 +080027#include <sound/soc.h>
Nicolin Chena2388a42013-08-21 11:13:16 +080028
29#include "fsl_spdif.h"
30#include "imx-pcm.h"
31
32#define FSL_SPDIF_TXFIFO_WML 0x8
33#define FSL_SPDIF_RXFIFO_WML 0x8
34
Nicolin Chenf3a30ba2014-05-06 16:42:25 +080035#define INTR_FOR_PLAYBACK (INT_TXFIFO_RESYNC)
36#define INTR_FOR_CAPTURE (INT_SYM_ERR | INT_BIT_ERR | INT_URX_FUL |\
37 INT_URX_OV | INT_QRX_FUL | INT_QRX_OV |\
38 INT_UQ_SYNC | INT_UQ_ERR | INT_RXFIFO_RESYNC |\
39 INT_LOSS_LOCK | INT_DPLL_LOCKED)
40
41#define SIE_INTR_FOR(tx) (tx ? INTR_FOR_PLAYBACK : INTR_FOR_CAPTURE)
Nicolin Chena2388a42013-08-21 11:13:16 +080042
43/* Index list for the values that has if (DPLL Locked) condition */
44static u8 srpc_dpll_locked[] = { 0x0, 0x1, 0x2, 0x3, 0x4, 0xa, 0xb };
45#define SRPC_NODPLL_START1 0x5
46#define SRPC_NODPLL_START2 0xc
47
48#define DEFAULT_RXCLK_SRC 1
49
50/*
51 * SPDIF control structure
52 * Defines channel status, subcode and Q sub
53 */
54struct spdif_mixer_control {
55 /* spinlock to access control data */
56 spinlock_t ctl_lock;
57
58 /* IEC958 channel tx status bit */
59 unsigned char ch_status[4];
60
61 /* User bits */
62 unsigned char subcode[2 * SPDIF_UBITS_SIZE];
63
64 /* Q subcode part of user bits */
65 unsigned char qsub[2 * SPDIF_QSUB_SIZE];
66
67 /* Buffer offset for U/Q */
68 u32 upos;
69 u32 qpos;
70
71 /* Ready buffer index of the two buffers */
72 u32 ready_buf;
73};
74
Nicolin Chenb8a832a2014-04-30 18:54:09 +080075/**
76 * fsl_spdif_priv: Freescale SPDIF private data
77 *
78 * @fsl_spdif_control: SPDIF control data
79 * @cpu_dai_drv: cpu dai driver
80 * @pdev: platform device pointer
81 * @regmap: regmap handler
82 * @dpll_locked: dpll lock flag
83 * @txrate: the best rates for playback
84 * @txclk_df: STC_TXCLK_DF dividers value for playback
85 * @sysclk_df: STC_SYSCLK_DF dividers value for playback
86 * @txclk_src: STC_TXCLK_SRC values for playback
87 * @rxclk_src: SRPC_CLKSRC_SEL values for capture
88 * @txclk: tx clock sources for playback
89 * @rxclk: rx clock sources for capture
90 * @coreclk: core clock for register access via DMA
91 * @sysclk: system clock for rx clock rate measurement
92 * @dma_params_tx: DMA parameters for transmit channel
93 * @dma_params_rx: DMA parameters for receive channel
94 * @name: driver name
95 */
Nicolin Chena2388a42013-08-21 11:13:16 +080096struct fsl_spdif_priv {
97 struct spdif_mixer_control fsl_spdif_control;
98 struct snd_soc_dai_driver cpu_dai_drv;
99 struct platform_device *pdev;
100 struct regmap *regmap;
101 bool dpll_locked;
Nicolin Chen527cda72014-04-30 18:54:08 +0800102 u16 txrate[SPDIF_TXRATE_MAX];
Nicolin Chene41a4a72014-04-30 18:54:06 +0800103 u8 txclk_df[SPDIF_TXRATE_MAX];
Nicolin Chen27c647b2014-04-30 18:54:07 +0800104 u8 sysclk_df[SPDIF_TXRATE_MAX];
Nicolin Chena2388a42013-08-21 11:13:16 +0800105 u8 txclk_src[SPDIF_TXRATE_MAX];
106 u8 rxclk_src;
107 struct clk *txclk[SPDIF_TXRATE_MAX];
108 struct clk *rxclk;
Nicolin Chen08f73362014-04-24 18:52:24 +0800109 struct clk *coreclk;
Nicolin Chen0b864392014-04-28 23:07:51 +0800110 struct clk *sysclk;
Nicolin Chena2388a42013-08-21 11:13:16 +0800111 struct snd_dmaengine_dai_dma_data dma_params_tx;
112 struct snd_dmaengine_dai_dma_data dma_params_rx;
113
114 /* The name space will be allocated dynamically */
115 char name[0];
116};
117
118
119/* DPLL locked and lock loss interrupt handler */
120static void spdif_irq_dpll_lock(struct fsl_spdif_priv *spdif_priv)
121{
122 struct regmap *regmap = spdif_priv->regmap;
123 struct platform_device *pdev = spdif_priv->pdev;
124 u32 locked;
125
126 regmap_read(regmap, REG_SPDIF_SRPC, &locked);
127 locked &= SRPC_DPLL_LOCKED;
128
129 dev_dbg(&pdev->dev, "isr: Rx dpll %s \n",
130 locked ? "locked" : "loss lock");
131
132 spdif_priv->dpll_locked = locked ? true : false;
133}
134
135/* Receiver found illegal symbol interrupt handler */
136static void spdif_irq_sym_error(struct fsl_spdif_priv *spdif_priv)
137{
138 struct regmap *regmap = spdif_priv->regmap;
139 struct platform_device *pdev = spdif_priv->pdev;
140
141 dev_dbg(&pdev->dev, "isr: receiver found illegal symbol\n");
142
Nicolin Chenf3a30ba2014-05-06 16:42:25 +0800143 /* Clear illegal symbol if DPLL unlocked since no audio stream */
144 if (!spdif_priv->dpll_locked)
Nicolin Chena2388a42013-08-21 11:13:16 +0800145 regmap_update_bits(regmap, REG_SPDIF_SIE, INT_SYM_ERR, 0);
Nicolin Chena2388a42013-08-21 11:13:16 +0800146}
147
148/* U/Q Channel receive register full */
149static void spdif_irq_uqrx_full(struct fsl_spdif_priv *spdif_priv, char name)
150{
151 struct spdif_mixer_control *ctrl = &spdif_priv->fsl_spdif_control;
152 struct regmap *regmap = spdif_priv->regmap;
153 struct platform_device *pdev = spdif_priv->pdev;
154 u32 *pos, size, val, reg;
155
156 switch (name) {
157 case 'U':
158 pos = &ctrl->upos;
159 size = SPDIF_UBITS_SIZE;
160 reg = REG_SPDIF_SRU;
161 break;
162 case 'Q':
163 pos = &ctrl->qpos;
164 size = SPDIF_QSUB_SIZE;
165 reg = REG_SPDIF_SRQ;
166 break;
167 default:
168 dev_err(&pdev->dev, "unsupported channel name\n");
169 return;
170 }
171
172 dev_dbg(&pdev->dev, "isr: %c Channel receive register full\n", name);
173
174 if (*pos >= size * 2) {
175 *pos = 0;
176 } else if (unlikely((*pos % size) + 3 > size)) {
177 dev_err(&pdev->dev, "User bit receivce buffer overflow\n");
178 return;
179 }
180
181 regmap_read(regmap, reg, &val);
182 ctrl->subcode[*pos++] = val >> 16;
183 ctrl->subcode[*pos++] = val >> 8;
184 ctrl->subcode[*pos++] = val;
185}
186
187/* U/Q Channel sync found */
188static void spdif_irq_uq_sync(struct fsl_spdif_priv *spdif_priv)
189{
190 struct spdif_mixer_control *ctrl = &spdif_priv->fsl_spdif_control;
191 struct platform_device *pdev = spdif_priv->pdev;
192
193 dev_dbg(&pdev->dev, "isr: U/Q Channel sync found\n");
194
195 /* U/Q buffer reset */
196 if (ctrl->qpos == 0)
197 return;
198
199 /* Set ready to this buffer */
200 ctrl->ready_buf = (ctrl->qpos - 1) / SPDIF_QSUB_SIZE + 1;
201}
202
203/* U/Q Channel framing error */
204static void spdif_irq_uq_err(struct fsl_spdif_priv *spdif_priv)
205{
206 struct spdif_mixer_control *ctrl = &spdif_priv->fsl_spdif_control;
207 struct regmap *regmap = spdif_priv->regmap;
208 struct platform_device *pdev = spdif_priv->pdev;
209 u32 val;
210
211 dev_dbg(&pdev->dev, "isr: U/Q Channel framing error\n");
212
213 /* Read U/Q data to clear the irq and do buffer reset */
214 regmap_read(regmap, REG_SPDIF_SRU, &val);
215 regmap_read(regmap, REG_SPDIF_SRQ, &val);
216
217 /* Drop this U/Q buffer */
218 ctrl->ready_buf = 0;
219 ctrl->upos = 0;
220 ctrl->qpos = 0;
221}
222
223/* Get spdif interrupt status and clear the interrupt */
224static u32 spdif_intr_status_clear(struct fsl_spdif_priv *spdif_priv)
225{
226 struct regmap *regmap = spdif_priv->regmap;
227 u32 val, val2;
228
229 regmap_read(regmap, REG_SPDIF_SIS, &val);
230 regmap_read(regmap, REG_SPDIF_SIE, &val2);
231
232 regmap_write(regmap, REG_SPDIF_SIC, val & val2);
233
234 return val;
235}
236
237static irqreturn_t spdif_isr(int irq, void *devid)
238{
239 struct fsl_spdif_priv *spdif_priv = (struct fsl_spdif_priv *)devid;
240 struct platform_device *pdev = spdif_priv->pdev;
241 u32 sis;
242
243 sis = spdif_intr_status_clear(spdif_priv);
244
245 if (sis & INT_DPLL_LOCKED)
246 spdif_irq_dpll_lock(spdif_priv);
247
248 if (sis & INT_TXFIFO_UNOV)
249 dev_dbg(&pdev->dev, "isr: Tx FIFO under/overrun\n");
250
251 if (sis & INT_TXFIFO_RESYNC)
252 dev_dbg(&pdev->dev, "isr: Tx FIFO resync\n");
253
254 if (sis & INT_CNEW)
255 dev_dbg(&pdev->dev, "isr: cstatus new\n");
256
257 if (sis & INT_VAL_NOGOOD)
258 dev_dbg(&pdev->dev, "isr: validity flag no good\n");
259
260 if (sis & INT_SYM_ERR)
261 spdif_irq_sym_error(spdif_priv);
262
263 if (sis & INT_BIT_ERR)
264 dev_dbg(&pdev->dev, "isr: receiver found parity bit error\n");
265
266 if (sis & INT_URX_FUL)
267 spdif_irq_uqrx_full(spdif_priv, 'U');
268
269 if (sis & INT_URX_OV)
270 dev_dbg(&pdev->dev, "isr: U Channel receive register overrun\n");
271
272 if (sis & INT_QRX_FUL)
273 spdif_irq_uqrx_full(spdif_priv, 'Q');
274
275 if (sis & INT_QRX_OV)
276 dev_dbg(&pdev->dev, "isr: Q Channel receive register overrun\n");
277
278 if (sis & INT_UQ_SYNC)
279 spdif_irq_uq_sync(spdif_priv);
280
281 if (sis & INT_UQ_ERR)
282 spdif_irq_uq_err(spdif_priv);
283
284 if (sis & INT_RXFIFO_UNOV)
285 dev_dbg(&pdev->dev, "isr: Rx FIFO under/overrun\n");
286
287 if (sis & INT_RXFIFO_RESYNC)
288 dev_dbg(&pdev->dev, "isr: Rx FIFO resync\n");
289
290 if (sis & INT_LOSS_LOCK)
291 spdif_irq_dpll_lock(spdif_priv);
292
293 /* FIXME: Write Tx FIFO to clear TxEm */
294 if (sis & INT_TX_EM)
295 dev_dbg(&pdev->dev, "isr: Tx FIFO empty\n");
296
297 /* FIXME: Read Rx FIFO to clear RxFIFOFul */
298 if (sis & INT_RXFIFO_FUL)
299 dev_dbg(&pdev->dev, "isr: Rx FIFO full\n");
300
301 return IRQ_HANDLED;
302}
303
304static int spdif_softreset(struct fsl_spdif_priv *spdif_priv)
305{
306 struct regmap *regmap = spdif_priv->regmap;
307 u32 val, cycle = 1000;
308
309 regmap_write(regmap, REG_SPDIF_SCR, SCR_SOFT_RESET);
310
311 /*
312 * RESET bit would be cleared after finishing its reset procedure,
313 * which typically lasts 8 cycles. 1000 cycles will keep it safe.
314 */
315 do {
316 regmap_read(regmap, REG_SPDIF_SCR, &val);
317 } while ((val & SCR_SOFT_RESET) && cycle--);
318
319 if (cycle)
320 return 0;
321 else
322 return -EBUSY;
323}
324
325static void spdif_set_cstatus(struct spdif_mixer_control *ctrl,
326 u8 mask, u8 cstatus)
327{
328 ctrl->ch_status[3] &= ~mask;
329 ctrl->ch_status[3] |= cstatus & mask;
330}
331
332static void spdif_write_channel_status(struct fsl_spdif_priv *spdif_priv)
333{
334 struct spdif_mixer_control *ctrl = &spdif_priv->fsl_spdif_control;
335 struct regmap *regmap = spdif_priv->regmap;
336 struct platform_device *pdev = spdif_priv->pdev;
337 u32 ch_status;
338
339 ch_status = (bitrev8(ctrl->ch_status[0]) << 16) |
Nicolin Chenf3a30ba2014-05-06 16:42:25 +0800340 (bitrev8(ctrl->ch_status[1]) << 8) |
341 bitrev8(ctrl->ch_status[2]);
Nicolin Chena2388a42013-08-21 11:13:16 +0800342 regmap_write(regmap, REG_SPDIF_STCSCH, ch_status);
343
344 dev_dbg(&pdev->dev, "STCSCH: 0x%06x\n", ch_status);
345
346 ch_status = bitrev8(ctrl->ch_status[3]) << 16;
347 regmap_write(regmap, REG_SPDIF_STCSCL, ch_status);
348
349 dev_dbg(&pdev->dev, "STCSCL: 0x%06x\n", ch_status);
350}
351
352/* Set SPDIF PhaseConfig register for rx clock */
353static int spdif_set_rx_clksrc(struct fsl_spdif_priv *spdif_priv,
354 enum spdif_gainsel gainsel, int dpll_locked)
355{
356 struct regmap *regmap = spdif_priv->regmap;
357 u8 clksrc = spdif_priv->rxclk_src;
358
359 if (clksrc >= SRPC_CLKSRC_MAX || gainsel >= GAINSEL_MULTI_MAX)
360 return -EINVAL;
361
362 regmap_update_bits(regmap, REG_SPDIF_SRPC,
363 SRPC_CLKSRC_SEL_MASK | SRPC_GAINSEL_MASK,
364 SRPC_CLKSRC_SEL_SET(clksrc) | SRPC_GAINSEL_SET(gainsel));
365
366 return 0;
367}
368
369static int spdif_set_sample_rate(struct snd_pcm_substream *substream,
370 int sample_rate)
371{
372 struct snd_soc_pcm_runtime *rtd = substream->private_data;
373 struct fsl_spdif_priv *spdif_priv = snd_soc_dai_get_drvdata(rtd->cpu_dai);
374 struct spdif_mixer_control *ctrl = &spdif_priv->fsl_spdif_control;
375 struct regmap *regmap = spdif_priv->regmap;
376 struct platform_device *pdev = spdif_priv->pdev;
377 unsigned long csfs = 0;
378 u32 stc, mask, rate;
Nicolin Chen27c647b2014-04-30 18:54:07 +0800379 u8 clk, txclk_df, sysclk_df;
Nicolin Chena2388a42013-08-21 11:13:16 +0800380 int ret;
381
382 switch (sample_rate) {
383 case 32000:
384 rate = SPDIF_TXRATE_32000;
385 csfs = IEC958_AES3_CON_FS_32000;
386 break;
387 case 44100:
388 rate = SPDIF_TXRATE_44100;
389 csfs = IEC958_AES3_CON_FS_44100;
390 break;
391 case 48000:
392 rate = SPDIF_TXRATE_48000;
393 csfs = IEC958_AES3_CON_FS_48000;
394 break;
395 default:
396 dev_err(&pdev->dev, "unsupported sample rate %d\n", sample_rate);
397 return -EINVAL;
398 }
399
400 clk = spdif_priv->txclk_src[rate];
401 if (clk >= STC_TXCLK_SRC_MAX) {
402 dev_err(&pdev->dev, "tx clock source is out of range\n");
403 return -EINVAL;
404 }
405
Nicolin Chene41a4a72014-04-30 18:54:06 +0800406 txclk_df = spdif_priv->txclk_df[rate];
407 if (txclk_df == 0) {
408 dev_err(&pdev->dev, "the txclk_df can't be zero\n");
Nicolin Chena2388a42013-08-21 11:13:16 +0800409 return -EINVAL;
410 }
411
Nicolin Chen27c647b2014-04-30 18:54:07 +0800412 sysclk_df = spdif_priv->sysclk_df[rate];
413
Nicolin Chen9c6344b2014-04-30 18:54:05 +0800414 /* Don't mess up the clocks from other modules */
415 if (clk != STC_TXCLK_SPDIF_ROOT)
416 goto clk_set_bypass;
417
Nicolin Chena2388a42013-08-21 11:13:16 +0800418 /*
Nicolin Chene41a4a72014-04-30 18:54:06 +0800419 * The S/PDIF block needs a clock of 64 * fs * txclk_df.
420 * So request 64 * fs * (txclk_df + 1) to get rounded.
Nicolin Chena2388a42013-08-21 11:13:16 +0800421 */
Nicolin Chene41a4a72014-04-30 18:54:06 +0800422 ret = clk_set_rate(spdif_priv->txclk[rate], 64 * sample_rate * (txclk_df + 1));
Nicolin Chena2388a42013-08-21 11:13:16 +0800423 if (ret) {
424 dev_err(&pdev->dev, "failed to set tx clock rate\n");
425 return ret;
426 }
427
Nicolin Chen9c6344b2014-04-30 18:54:05 +0800428clk_set_bypass:
Nicolin Chena2388a42013-08-21 11:13:16 +0800429 dev_dbg(&pdev->dev, "expected clock rate = %d\n",
Nicolin Chen27c647b2014-04-30 18:54:07 +0800430 (64 * sample_rate * txclk_df * sysclk_df));
Nicolin Chena2388a42013-08-21 11:13:16 +0800431 dev_dbg(&pdev->dev, "actual clock rate = %ld\n",
432 clk_get_rate(spdif_priv->txclk[rate]));
433
434 /* set fs field in consumer channel status */
435 spdif_set_cstatus(ctrl, IEC958_AES3_CON_FS, csfs);
436
437 /* select clock source and divisor */
Nicolin Chenf3a30ba2014-05-06 16:42:25 +0800438 stc = STC_TXCLK_ALL_EN | STC_TXCLK_SRC_SET(clk) |
439 STC_TXCLK_DF(txclk_df) | STC_SYSCLK_DF(sysclk_df);
440 mask = STC_TXCLK_ALL_EN_MASK | STC_TXCLK_SRC_MASK |
441 STC_TXCLK_DF_MASK | STC_SYSCLK_DF_MASK;
Nicolin Chena2388a42013-08-21 11:13:16 +0800442 regmap_update_bits(regmap, REG_SPDIF_STC, mask, stc);
443
Nicolin Chen527cda72014-04-30 18:54:08 +0800444 dev_dbg(&pdev->dev, "set sample rate to %dHz for %dHz playback\n",
445 spdif_priv->txrate[rate], sample_rate);
Nicolin Chena2388a42013-08-21 11:13:16 +0800446
447 return 0;
448}
449
Mark Brown6b4c80f2013-08-31 16:40:51 +0100450static int fsl_spdif_startup(struct snd_pcm_substream *substream,
451 struct snd_soc_dai *cpu_dai)
Nicolin Chena2388a42013-08-21 11:13:16 +0800452{
453 struct snd_soc_pcm_runtime *rtd = substream->private_data;
454 struct fsl_spdif_priv *spdif_priv = snd_soc_dai_get_drvdata(rtd->cpu_dai);
455 struct platform_device *pdev = spdif_priv->pdev;
456 struct regmap *regmap = spdif_priv->regmap;
457 u32 scr, mask, i;
458 int ret;
459
460 /* Reset module and interrupts only for first initialization */
461 if (!cpu_dai->active) {
Nicolin Chen08f73362014-04-24 18:52:24 +0800462 ret = clk_prepare_enable(spdif_priv->coreclk);
463 if (ret) {
464 dev_err(&pdev->dev, "failed to enable core clock\n");
465 return ret;
466 }
467
Nicolin Chena2388a42013-08-21 11:13:16 +0800468 ret = spdif_softreset(spdif_priv);
469 if (ret) {
470 dev_err(&pdev->dev, "failed to soft reset\n");
Nicolin Chen08f73362014-04-24 18:52:24 +0800471 goto err;
Nicolin Chena2388a42013-08-21 11:13:16 +0800472 }
473
474 /* Disable all the interrupts */
475 regmap_update_bits(regmap, REG_SPDIF_SIE, 0xffffff, 0);
476 }
477
478 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
479 scr = SCR_TXFIFO_AUTOSYNC | SCR_TXFIFO_CTRL_NORMAL |
480 SCR_TXSEL_NORMAL | SCR_USRC_SEL_CHIP |
481 SCR_TXFIFO_FSEL_IF8;
482 mask = SCR_TXFIFO_AUTOSYNC_MASK | SCR_TXFIFO_CTRL_MASK |
483 SCR_TXSEL_MASK | SCR_USRC_SEL_MASK |
484 SCR_TXFIFO_FSEL_MASK;
485 for (i = 0; i < SPDIF_TXRATE_MAX; i++)
486 clk_prepare_enable(spdif_priv->txclk[i]);
487 } else {
488 scr = SCR_RXFIFO_FSEL_IF8 | SCR_RXFIFO_AUTOSYNC;
489 mask = SCR_RXFIFO_FSEL_MASK | SCR_RXFIFO_AUTOSYNC_MASK|
490 SCR_RXFIFO_CTL_MASK | SCR_RXFIFO_OFF_MASK;
491 clk_prepare_enable(spdif_priv->rxclk);
492 }
493 regmap_update_bits(regmap, REG_SPDIF_SCR, mask, scr);
494
495 /* Power up SPDIF module */
496 regmap_update_bits(regmap, REG_SPDIF_SCR, SCR_LOW_POWER, 0);
497
498 return 0;
Nicolin Chen08f73362014-04-24 18:52:24 +0800499
500err:
501 clk_disable_unprepare(spdif_priv->coreclk);
502
503 return ret;
Nicolin Chena2388a42013-08-21 11:13:16 +0800504}
505
506static void fsl_spdif_shutdown(struct snd_pcm_substream *substream,
507 struct snd_soc_dai *cpu_dai)
508{
509 struct snd_soc_pcm_runtime *rtd = substream->private_data;
510 struct fsl_spdif_priv *spdif_priv = snd_soc_dai_get_drvdata(rtd->cpu_dai);
511 struct regmap *regmap = spdif_priv->regmap;
512 u32 scr, mask, i;
513
514 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
515 scr = 0;
516 mask = SCR_TXFIFO_AUTOSYNC_MASK | SCR_TXFIFO_CTRL_MASK |
517 SCR_TXSEL_MASK | SCR_USRC_SEL_MASK |
518 SCR_TXFIFO_FSEL_MASK;
519 for (i = 0; i < SPDIF_TXRATE_MAX; i++)
520 clk_disable_unprepare(spdif_priv->txclk[i]);
521 } else {
522 scr = SCR_RXFIFO_OFF | SCR_RXFIFO_CTL_ZERO;
523 mask = SCR_RXFIFO_FSEL_MASK | SCR_RXFIFO_AUTOSYNC_MASK|
524 SCR_RXFIFO_CTL_MASK | SCR_RXFIFO_OFF_MASK;
525 clk_disable_unprepare(spdif_priv->rxclk);
526 }
527 regmap_update_bits(regmap, REG_SPDIF_SCR, mask, scr);
528
529 /* Power down SPDIF module only if tx&rx are both inactive */
530 if (!cpu_dai->active) {
531 spdif_intr_status_clear(spdif_priv);
532 regmap_update_bits(regmap, REG_SPDIF_SCR,
533 SCR_LOW_POWER, SCR_LOW_POWER);
Nicolin Chen08f73362014-04-24 18:52:24 +0800534 clk_disable_unprepare(spdif_priv->coreclk);
Nicolin Chena2388a42013-08-21 11:13:16 +0800535 }
536}
537
538static int fsl_spdif_hw_params(struct snd_pcm_substream *substream,
539 struct snd_pcm_hw_params *params,
540 struct snd_soc_dai *dai)
541{
542 struct snd_soc_pcm_runtime *rtd = substream->private_data;
543 struct fsl_spdif_priv *spdif_priv = snd_soc_dai_get_drvdata(rtd->cpu_dai);
544 struct spdif_mixer_control *ctrl = &spdif_priv->fsl_spdif_control;
545 struct platform_device *pdev = spdif_priv->pdev;
546 u32 sample_rate = params_rate(params);
547 int ret = 0;
548
549 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
550 ret = spdif_set_sample_rate(substream, sample_rate);
551 if (ret) {
552 dev_err(&pdev->dev, "%s: set sample rate failed: %d\n",
553 __func__, sample_rate);
554 return ret;
555 }
556 spdif_set_cstatus(ctrl, IEC958_AES3_CON_CLOCK,
Nicolin Chenf3a30ba2014-05-06 16:42:25 +0800557 IEC958_AES3_CON_CLOCK_1000PPM);
Nicolin Chena2388a42013-08-21 11:13:16 +0800558 spdif_write_channel_status(spdif_priv);
559 } else {
560 /* Setup rx clock source */
561 ret = spdif_set_rx_clksrc(spdif_priv, SPDIF_DEFAULT_GAINSEL, 1);
562 }
563
564 return ret;
565}
566
567static int fsl_spdif_trigger(struct snd_pcm_substream *substream,
568 int cmd, struct snd_soc_dai *dai)
569{
570 struct snd_soc_pcm_runtime *rtd = substream->private_data;
571 struct fsl_spdif_priv *spdif_priv = snd_soc_dai_get_drvdata(rtd->cpu_dai);
572 struct regmap *regmap = spdif_priv->regmap;
Nicolin Chenf3a30ba2014-05-06 16:42:25 +0800573 bool tx = substream->stream == SNDRV_PCM_STREAM_PLAYBACK;
574 u32 intr = SIE_INTR_FOR(tx);
575 u32 dmaen = SCR_DMA_xX_EN(tx);
Nicolin Chena2388a42013-08-21 11:13:16 +0800576
577 switch (cmd) {
578 case SNDRV_PCM_TRIGGER_START:
579 case SNDRV_PCM_TRIGGER_RESUME:
580 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
581 regmap_update_bits(regmap, REG_SPDIF_SIE, intr, intr);
582 regmap_update_bits(regmap, REG_SPDIF_SCR, dmaen, dmaen);
583 break;
584 case SNDRV_PCM_TRIGGER_STOP:
585 case SNDRV_PCM_TRIGGER_SUSPEND:
586 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
587 regmap_update_bits(regmap, REG_SPDIF_SCR, dmaen, 0);
588 regmap_update_bits(regmap, REG_SPDIF_SIE, intr, 0);
589 break;
590 default:
591 return -EINVAL;
592 }
593
594 return 0;
595}
596
Mark Brown6b4c80f2013-08-31 16:40:51 +0100597static struct snd_soc_dai_ops fsl_spdif_dai_ops = {
Nicolin Chena2388a42013-08-21 11:13:16 +0800598 .startup = fsl_spdif_startup,
599 .hw_params = fsl_spdif_hw_params,
600 .trigger = fsl_spdif_trigger,
601 .shutdown = fsl_spdif_shutdown,
602};
603
604
605/*
Nicolin Chena2388a42013-08-21 11:13:16 +0800606 * FSL SPDIF IEC958 controller(mixer) functions
607 *
608 * Channel status get/put control
609 * User bit value get/put control
610 * Valid bit value get control
611 * DPLL lock status get control
612 * User bit sync mode selection control
Nicolin Chena2388a42013-08-21 11:13:16 +0800613 */
614
615static int fsl_spdif_info(struct snd_kcontrol *kcontrol,
616 struct snd_ctl_elem_info *uinfo)
617{
618 uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
619 uinfo->count = 1;
620
621 return 0;
622}
623
624static int fsl_spdif_pb_get(struct snd_kcontrol *kcontrol,
625 struct snd_ctl_elem_value *uvalue)
626{
627 struct snd_soc_dai *cpu_dai = snd_kcontrol_chip(kcontrol);
628 struct fsl_spdif_priv *spdif_priv = snd_soc_dai_get_drvdata(cpu_dai);
629 struct spdif_mixer_control *ctrl = &spdif_priv->fsl_spdif_control;
630
631 uvalue->value.iec958.status[0] = ctrl->ch_status[0];
632 uvalue->value.iec958.status[1] = ctrl->ch_status[1];
633 uvalue->value.iec958.status[2] = ctrl->ch_status[2];
634 uvalue->value.iec958.status[3] = ctrl->ch_status[3];
635
636 return 0;
637}
638
639static int fsl_spdif_pb_put(struct snd_kcontrol *kcontrol,
640 struct snd_ctl_elem_value *uvalue)
641{
642 struct snd_soc_dai *cpu_dai = snd_kcontrol_chip(kcontrol);
643 struct fsl_spdif_priv *spdif_priv = snd_soc_dai_get_drvdata(cpu_dai);
644 struct spdif_mixer_control *ctrl = &spdif_priv->fsl_spdif_control;
645
646 ctrl->ch_status[0] = uvalue->value.iec958.status[0];
647 ctrl->ch_status[1] = uvalue->value.iec958.status[1];
648 ctrl->ch_status[2] = uvalue->value.iec958.status[2];
649 ctrl->ch_status[3] = uvalue->value.iec958.status[3];
650
651 spdif_write_channel_status(spdif_priv);
652
653 return 0;
654}
655
656/* Get channel status from SPDIF_RX_CCHAN register */
657static int fsl_spdif_capture_get(struct snd_kcontrol *kcontrol,
658 struct snd_ctl_elem_value *ucontrol)
659{
660 struct snd_soc_dai *cpu_dai = snd_kcontrol_chip(kcontrol);
661 struct fsl_spdif_priv *spdif_priv = snd_soc_dai_get_drvdata(cpu_dai);
662 struct regmap *regmap = spdif_priv->regmap;
663 u32 cstatus, val;
664
665 regmap_read(regmap, REG_SPDIF_SIS, &val);
Nicolin Chenf3a30ba2014-05-06 16:42:25 +0800666 if (!(val & INT_CNEW))
Nicolin Chena2388a42013-08-21 11:13:16 +0800667 return -EAGAIN;
Nicolin Chena2388a42013-08-21 11:13:16 +0800668
669 regmap_read(regmap, REG_SPDIF_SRCSH, &cstatus);
670 ucontrol->value.iec958.status[0] = (cstatus >> 16) & 0xFF;
671 ucontrol->value.iec958.status[1] = (cstatus >> 8) & 0xFF;
672 ucontrol->value.iec958.status[2] = cstatus & 0xFF;
673
674 regmap_read(regmap, REG_SPDIF_SRCSL, &cstatus);
675 ucontrol->value.iec958.status[3] = (cstatus >> 16) & 0xFF;
676 ucontrol->value.iec958.status[4] = (cstatus >> 8) & 0xFF;
677 ucontrol->value.iec958.status[5] = cstatus & 0xFF;
678
679 /* Clear intr */
680 regmap_write(regmap, REG_SPDIF_SIC, INT_CNEW);
681
682 return 0;
683}
684
685/*
686 * Get User bits (subcode) from chip value which readed out
687 * in UChannel register.
688 */
689static int fsl_spdif_subcode_get(struct snd_kcontrol *kcontrol,
690 struct snd_ctl_elem_value *ucontrol)
691{
692 struct snd_soc_dai *cpu_dai = snd_kcontrol_chip(kcontrol);
693 struct fsl_spdif_priv *spdif_priv = snd_soc_dai_get_drvdata(cpu_dai);
694 struct spdif_mixer_control *ctrl = &spdif_priv->fsl_spdif_control;
695 unsigned long flags;
Nicolin Chenf3a30ba2014-05-06 16:42:25 +0800696 int ret = -EAGAIN;
Nicolin Chena2388a42013-08-21 11:13:16 +0800697
698 spin_lock_irqsave(&ctrl->ctl_lock, flags);
699 if (ctrl->ready_buf) {
700 int idx = (ctrl->ready_buf - 1) * SPDIF_UBITS_SIZE;
701 memcpy(&ucontrol->value.iec958.subcode[0],
702 &ctrl->subcode[idx], SPDIF_UBITS_SIZE);
Nicolin Chenf3a30ba2014-05-06 16:42:25 +0800703 ret = 0;
Nicolin Chena2388a42013-08-21 11:13:16 +0800704 }
705 spin_unlock_irqrestore(&ctrl->ctl_lock, flags);
706
707 return ret;
708}
709
710/* Q-subcode infomation. The byte size is SPDIF_UBITS_SIZE/8 */
711static int fsl_spdif_qinfo(struct snd_kcontrol *kcontrol,
712 struct snd_ctl_elem_info *uinfo)
713{
714 uinfo->type = SNDRV_CTL_ELEM_TYPE_BYTES;
715 uinfo->count = SPDIF_QSUB_SIZE;
716
717 return 0;
718}
719
720/* Get Q subcode from chip value which readed out in QChannel register */
721static int fsl_spdif_qget(struct snd_kcontrol *kcontrol,
722 struct snd_ctl_elem_value *ucontrol)
723{
724 struct snd_soc_dai *cpu_dai = snd_kcontrol_chip(kcontrol);
725 struct fsl_spdif_priv *spdif_priv = snd_soc_dai_get_drvdata(cpu_dai);
726 struct spdif_mixer_control *ctrl = &spdif_priv->fsl_spdif_control;
727 unsigned long flags;
Nicolin Chenf3a30ba2014-05-06 16:42:25 +0800728 int ret = -EAGAIN;
Nicolin Chena2388a42013-08-21 11:13:16 +0800729
730 spin_lock_irqsave(&ctrl->ctl_lock, flags);
731 if (ctrl->ready_buf) {
732 int idx = (ctrl->ready_buf - 1) * SPDIF_QSUB_SIZE;
733 memcpy(&ucontrol->value.bytes.data[0],
734 &ctrl->qsub[idx], SPDIF_QSUB_SIZE);
Nicolin Chenf3a30ba2014-05-06 16:42:25 +0800735 ret = 0;
Nicolin Chena2388a42013-08-21 11:13:16 +0800736 }
737 spin_unlock_irqrestore(&ctrl->ctl_lock, flags);
738
739 return ret;
740}
741
742/* Valid bit infomation */
743static int fsl_spdif_vbit_info(struct snd_kcontrol *kcontrol,
744 struct snd_ctl_elem_info *uinfo)
745{
746 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
747 uinfo->count = 1;
748 uinfo->value.integer.min = 0;
749 uinfo->value.integer.max = 1;
750
751 return 0;
752}
753
754/* Get valid good bit from interrupt status register */
755static int fsl_spdif_vbit_get(struct snd_kcontrol *kcontrol,
756 struct snd_ctl_elem_value *ucontrol)
757{
758 struct snd_soc_dai *cpu_dai = snd_kcontrol_chip(kcontrol);
759 struct fsl_spdif_priv *spdif_priv = snd_soc_dai_get_drvdata(cpu_dai);
760 struct regmap *regmap = spdif_priv->regmap;
761 u32 val;
762
763 val = regmap_read(regmap, REG_SPDIF_SIS, &val);
764 ucontrol->value.integer.value[0] = (val & INT_VAL_NOGOOD) != 0;
765 regmap_write(regmap, REG_SPDIF_SIC, INT_VAL_NOGOOD);
766
767 return 0;
768}
769
770/* DPLL lock infomation */
771static int fsl_spdif_rxrate_info(struct snd_kcontrol *kcontrol,
772 struct snd_ctl_elem_info *uinfo)
773{
774 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
775 uinfo->count = 1;
776 uinfo->value.integer.min = 16000;
777 uinfo->value.integer.max = 96000;
778
779 return 0;
780}
781
782static u32 gainsel_multi[GAINSEL_MULTI_MAX] = {
783 24, 16, 12, 8, 6, 4, 3,
784};
785
786/* Get RX data clock rate given the SPDIF bus_clk */
787static int spdif_get_rxclk_rate(struct fsl_spdif_priv *spdif_priv,
788 enum spdif_gainsel gainsel)
789{
790 struct regmap *regmap = spdif_priv->regmap;
791 struct platform_device *pdev = spdif_priv->pdev;
792 u64 tmpval64, busclk_freq = 0;
793 u32 freqmeas, phaseconf;
794 u8 clksrc;
795
796 regmap_read(regmap, REG_SPDIF_SRFM, &freqmeas);
797 regmap_read(regmap, REG_SPDIF_SRPC, &phaseconf);
798
799 clksrc = (phaseconf >> SRPC_CLKSRC_SEL_OFFSET) & 0xf;
Nicolin Chenf3a30ba2014-05-06 16:42:25 +0800800
801 /* Get bus clock from system */
802 if (srpc_dpll_locked[clksrc] && (phaseconf & SRPC_DPLL_LOCKED))
Nicolin Chen0b864392014-04-28 23:07:51 +0800803 busclk_freq = clk_get_rate(spdif_priv->sysclk);
Nicolin Chena2388a42013-08-21 11:13:16 +0800804
805 /* FreqMeas_CLK = (BUS_CLK * FreqMeas) / 2 ^ 10 / GAINSEL / 128 */
806 tmpval64 = (u64) busclk_freq * freqmeas;
807 do_div(tmpval64, gainsel_multi[gainsel] * 1024);
808 do_div(tmpval64, 128 * 1024);
809
810 dev_dbg(&pdev->dev, "FreqMeas: %d\n", freqmeas);
811 dev_dbg(&pdev->dev, "BusclkFreq: %lld\n", busclk_freq);
812 dev_dbg(&pdev->dev, "RxRate: %lld\n", tmpval64);
813
814 return (int)tmpval64;
815}
816
817/*
818 * Get DPLL lock or not info from stable interrupt status register.
819 * User application must use this control to get locked,
820 * then can do next PCM operation
821 */
822static int fsl_spdif_rxrate_get(struct snd_kcontrol *kcontrol,
823 struct snd_ctl_elem_value *ucontrol)
824{
825 struct snd_soc_dai *cpu_dai = snd_kcontrol_chip(kcontrol);
826 struct fsl_spdif_priv *spdif_priv = snd_soc_dai_get_drvdata(cpu_dai);
Nicolin Chenf3a30ba2014-05-06 16:42:25 +0800827 int rate = 0;
Nicolin Chena2388a42013-08-21 11:13:16 +0800828
829 if (spdif_priv->dpll_locked)
Nicolin Chenf3a30ba2014-05-06 16:42:25 +0800830 rate = spdif_get_rxclk_rate(spdif_priv, SPDIF_DEFAULT_GAINSEL);
831
832 ucontrol->value.integer.value[0] = rate;
Nicolin Chena2388a42013-08-21 11:13:16 +0800833
834 return 0;
835}
836
837/* User bit sync mode info */
838static int fsl_spdif_usync_info(struct snd_kcontrol *kcontrol,
839 struct snd_ctl_elem_info *uinfo)
840{
841 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
842 uinfo->count = 1;
843 uinfo->value.integer.min = 0;
844 uinfo->value.integer.max = 1;
845
846 return 0;
847}
848
849/*
850 * User bit sync mode:
851 * 1 CD User channel subcode
852 * 0 Non-CD data
853 */
854static int fsl_spdif_usync_get(struct snd_kcontrol *kcontrol,
855 struct snd_ctl_elem_value *ucontrol)
856{
857 struct snd_soc_dai *cpu_dai = snd_kcontrol_chip(kcontrol);
858 struct fsl_spdif_priv *spdif_priv = snd_soc_dai_get_drvdata(cpu_dai);
859 struct regmap *regmap = spdif_priv->regmap;
860 u32 val;
861
862 regmap_read(regmap, REG_SPDIF_SRCD, &val);
863 ucontrol->value.integer.value[0] = (val & SRCD_CD_USER) != 0;
864
865 return 0;
866}
867
868/*
869 * User bit sync mode:
870 * 1 CD User channel subcode
871 * 0 Non-CD data
872 */
873static int fsl_spdif_usync_put(struct snd_kcontrol *kcontrol,
874 struct snd_ctl_elem_value *ucontrol)
875{
876 struct snd_soc_dai *cpu_dai = snd_kcontrol_chip(kcontrol);
877 struct fsl_spdif_priv *spdif_priv = snd_soc_dai_get_drvdata(cpu_dai);
878 struct regmap *regmap = spdif_priv->regmap;
879 u32 val = ucontrol->value.integer.value[0] << SRCD_CD_USER_OFFSET;
880
881 regmap_update_bits(regmap, REG_SPDIF_SRCD, SRCD_CD_USER, val);
882
883 return 0;
884}
885
886/* FSL SPDIF IEC958 controller defines */
887static struct snd_kcontrol_new fsl_spdif_ctrls[] = {
888 /* Status cchanel controller */
889 {
890 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
891 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, DEFAULT),
892 .access = SNDRV_CTL_ELEM_ACCESS_READ |
893 SNDRV_CTL_ELEM_ACCESS_WRITE |
894 SNDRV_CTL_ELEM_ACCESS_VOLATILE,
895 .info = fsl_spdif_info,
896 .get = fsl_spdif_pb_get,
897 .put = fsl_spdif_pb_put,
898 },
899 {
900 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
901 .name = SNDRV_CTL_NAME_IEC958("", CAPTURE, DEFAULT),
902 .access = SNDRV_CTL_ELEM_ACCESS_READ |
903 SNDRV_CTL_ELEM_ACCESS_VOLATILE,
904 .info = fsl_spdif_info,
905 .get = fsl_spdif_capture_get,
906 },
907 /* User bits controller */
908 {
909 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
910 .name = "IEC958 Subcode Capture Default",
911 .access = SNDRV_CTL_ELEM_ACCESS_READ |
912 SNDRV_CTL_ELEM_ACCESS_VOLATILE,
913 .info = fsl_spdif_info,
914 .get = fsl_spdif_subcode_get,
915 },
916 {
917 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
918 .name = "IEC958 Q-subcode Capture Default",
919 .access = SNDRV_CTL_ELEM_ACCESS_READ |
920 SNDRV_CTL_ELEM_ACCESS_VOLATILE,
921 .info = fsl_spdif_qinfo,
922 .get = fsl_spdif_qget,
923 },
924 /* Valid bit error controller */
925 {
926 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
927 .name = "IEC958 V-Bit Errors",
928 .access = SNDRV_CTL_ELEM_ACCESS_READ |
929 SNDRV_CTL_ELEM_ACCESS_VOLATILE,
930 .info = fsl_spdif_vbit_info,
931 .get = fsl_spdif_vbit_get,
932 },
933 /* DPLL lock info get controller */
934 {
935 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
936 .name = "RX Sample Rate",
937 .access = SNDRV_CTL_ELEM_ACCESS_READ |
938 SNDRV_CTL_ELEM_ACCESS_VOLATILE,
939 .info = fsl_spdif_rxrate_info,
940 .get = fsl_spdif_rxrate_get,
941 },
942 /* User bit sync mode set/get controller */
943 {
944 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
945 .name = "IEC958 USyncMode CDText",
946 .access = SNDRV_CTL_ELEM_ACCESS_READ |
947 SNDRV_CTL_ELEM_ACCESS_WRITE |
948 SNDRV_CTL_ELEM_ACCESS_VOLATILE,
949 .info = fsl_spdif_usync_info,
950 .get = fsl_spdif_usync_get,
951 .put = fsl_spdif_usync_put,
952 },
953};
954
955static int fsl_spdif_dai_probe(struct snd_soc_dai *dai)
956{
957 struct fsl_spdif_priv *spdif_private = snd_soc_dai_get_drvdata(dai);
958
Xiubo Li05cf4822014-01-20 15:27:26 +0800959 snd_soc_dai_init_dma_data(dai, &spdif_private->dma_params_tx,
960 &spdif_private->dma_params_rx);
Nicolin Chena2388a42013-08-21 11:13:16 +0800961
962 snd_soc_add_dai_controls(dai, fsl_spdif_ctrls, ARRAY_SIZE(fsl_spdif_ctrls));
963
964 return 0;
965}
966
Mark Brown6b4c80f2013-08-31 16:40:51 +0100967static struct snd_soc_dai_driver fsl_spdif_dai = {
Nicolin Chena2388a42013-08-21 11:13:16 +0800968 .probe = &fsl_spdif_dai_probe,
969 .playback = {
970 .channels_min = 2,
971 .channels_max = 2,
972 .rates = FSL_SPDIF_RATES_PLAYBACK,
973 .formats = FSL_SPDIF_FORMATS_PLAYBACK,
974 },
975 .capture = {
976 .channels_min = 2,
977 .channels_max = 2,
978 .rates = FSL_SPDIF_RATES_CAPTURE,
979 .formats = FSL_SPDIF_FORMATS_CAPTURE,
980 },
981 .ops = &fsl_spdif_dai_ops,
982};
983
984static const struct snd_soc_component_driver fsl_spdif_component = {
985 .name = "fsl-spdif",
986};
987
Fabio Estevam6d22db42013-08-23 18:14:46 -0300988/* FSL SPDIF REGMAP */
Nicolin Chena2388a42013-08-21 11:13:16 +0800989
990static bool fsl_spdif_readable_reg(struct device *dev, unsigned int reg)
991{
992 switch (reg) {
993 case REG_SPDIF_SCR:
994 case REG_SPDIF_SRCD:
995 case REG_SPDIF_SRPC:
996 case REG_SPDIF_SIE:
997 case REG_SPDIF_SIS:
998 case REG_SPDIF_SRL:
999 case REG_SPDIF_SRR:
1000 case REG_SPDIF_SRCSH:
1001 case REG_SPDIF_SRCSL:
1002 case REG_SPDIF_SRU:
1003 case REG_SPDIF_SRQ:
1004 case REG_SPDIF_STCSCH:
1005 case REG_SPDIF_STCSCL:
1006 case REG_SPDIF_SRFM:
1007 case REG_SPDIF_STC:
1008 return true;
1009 default:
1010 return false;
Sachin Kamate19bcb62013-09-13 15:52:42 +05301011 }
Nicolin Chena2388a42013-08-21 11:13:16 +08001012}
1013
1014static bool fsl_spdif_writeable_reg(struct device *dev, unsigned int reg)
1015{
1016 switch (reg) {
1017 case REG_SPDIF_SCR:
1018 case REG_SPDIF_SRCD:
1019 case REG_SPDIF_SRPC:
1020 case REG_SPDIF_SIE:
1021 case REG_SPDIF_SIC:
1022 case REG_SPDIF_STL:
1023 case REG_SPDIF_STR:
1024 case REG_SPDIF_STCSCH:
1025 case REG_SPDIF_STCSCL:
1026 case REG_SPDIF_STC:
1027 return true;
1028 default:
1029 return false;
Sachin Kamate19bcb62013-09-13 15:52:42 +05301030 }
Nicolin Chena2388a42013-08-21 11:13:16 +08001031}
1032
Xiubo Li86f28d72014-02-11 15:42:48 +08001033static struct regmap_config fsl_spdif_regmap_config = {
Nicolin Chena2388a42013-08-21 11:13:16 +08001034 .reg_bits = 32,
1035 .reg_stride = 4,
1036 .val_bits = 32,
1037
1038 .max_register = REG_SPDIF_STC,
1039 .readable_reg = fsl_spdif_readable_reg,
1040 .writeable_reg = fsl_spdif_writeable_reg,
1041};
1042
1043static u32 fsl_spdif_txclk_caldiv(struct fsl_spdif_priv *spdif_priv,
1044 struct clk *clk, u64 savesub,
Nicolin Chen9c6344b2014-04-30 18:54:05 +08001045 enum spdif_txrate index, bool round)
Nicolin Chena2388a42013-08-21 11:13:16 +08001046{
1047 const u32 rate[] = { 32000, 44100, 48000 };
Nicolin Chen27c647b2014-04-30 18:54:07 +08001048 bool is_sysclk = clk == spdif_priv->sysclk;
Nicolin Chena2388a42013-08-21 11:13:16 +08001049 u64 rate_ideal, rate_actual, sub;
Nicolin Chen27c647b2014-04-30 18:54:07 +08001050 u32 sysclk_dfmin, sysclk_dfmax;
1051 u32 txclk_df, sysclk_df, arate;
Nicolin Chena2388a42013-08-21 11:13:16 +08001052
Nicolin Chen27c647b2014-04-30 18:54:07 +08001053 /* The sysclk has an extra divisor [2, 512] */
1054 sysclk_dfmin = is_sysclk ? 2 : 1;
1055 sysclk_dfmax = is_sysclk ? 512 : 1;
Nicolin Chena2388a42013-08-21 11:13:16 +08001056
Nicolin Chen27c647b2014-04-30 18:54:07 +08001057 for (sysclk_df = sysclk_dfmin; sysclk_df <= sysclk_dfmax; sysclk_df++) {
1058 for (txclk_df = 1; txclk_df <= 128; txclk_df++) {
1059 rate_ideal = rate[index] * (txclk_df + 1) * 64;
1060 if (round)
1061 rate_actual = clk_round_rate(clk, rate_ideal);
1062 else
1063 rate_actual = clk_get_rate(clk);
Nicolin Chena2388a42013-08-21 11:13:16 +08001064
Nicolin Chen27c647b2014-04-30 18:54:07 +08001065 arate = rate_actual / 64;
1066 arate /= txclk_df * sysclk_df;
1067
1068 if (arate == rate[index]) {
1069 /* We are lucky */
1070 savesub = 0;
1071 spdif_priv->txclk_df[index] = txclk_df;
1072 spdif_priv->sysclk_df[index] = sysclk_df;
Nicolin Chen527cda72014-04-30 18:54:08 +08001073 spdif_priv->txrate[index] = arate;
Nicolin Chen27c647b2014-04-30 18:54:07 +08001074 goto out;
1075 } else if (arate / rate[index] == 1) {
1076 /* A little bigger than expect */
1077 sub = (arate - rate[index]) * 100000;
1078 do_div(sub, rate[index]);
1079 if (sub >= savesub)
1080 continue;
Nicolin Chena2388a42013-08-21 11:13:16 +08001081 savesub = sub;
Nicolin Chene41a4a72014-04-30 18:54:06 +08001082 spdif_priv->txclk_df[index] = txclk_df;
Nicolin Chen27c647b2014-04-30 18:54:07 +08001083 spdif_priv->sysclk_df[index] = sysclk_df;
Nicolin Chen527cda72014-04-30 18:54:08 +08001084 spdif_priv->txrate[index] = arate;
Nicolin Chen27c647b2014-04-30 18:54:07 +08001085 } else if (rate[index] / arate == 1) {
1086 /* A little smaller than expect */
1087 sub = (rate[index] - arate) * 100000;
1088 do_div(sub, rate[index]);
1089 if (sub >= savesub)
1090 continue;
Nicolin Chena2388a42013-08-21 11:13:16 +08001091 savesub = sub;
Nicolin Chene41a4a72014-04-30 18:54:06 +08001092 spdif_priv->txclk_df[index] = txclk_df;
Nicolin Chen27c647b2014-04-30 18:54:07 +08001093 spdif_priv->sysclk_df[index] = sysclk_df;
Nicolin Chen527cda72014-04-30 18:54:08 +08001094 spdif_priv->txrate[index] = arate;
Nicolin Chena2388a42013-08-21 11:13:16 +08001095 }
1096 }
1097 }
1098
Nicolin Chen27c647b2014-04-30 18:54:07 +08001099out:
Nicolin Chena2388a42013-08-21 11:13:16 +08001100 return savesub;
1101}
1102
1103static int fsl_spdif_probe_txclk(struct fsl_spdif_priv *spdif_priv,
1104 enum spdif_txrate index)
1105{
1106 const u32 rate[] = { 32000, 44100, 48000 };
1107 struct platform_device *pdev = spdif_priv->pdev;
1108 struct device *dev = &pdev->dev;
1109 u64 savesub = 100000, ret;
1110 struct clk *clk;
1111 char tmp[16];
1112 int i;
1113
1114 for (i = 0; i < STC_TXCLK_SRC_MAX; i++) {
1115 sprintf(tmp, "rxtx%d", i);
1116 clk = devm_clk_get(&pdev->dev, tmp);
1117 if (IS_ERR(clk)) {
1118 dev_err(dev, "no rxtx%d clock in devicetree\n", i);
1119 return PTR_ERR(clk);
1120 }
1121 if (!clk_get_rate(clk))
1122 continue;
1123
Nicolin Chen9c6344b2014-04-30 18:54:05 +08001124 ret = fsl_spdif_txclk_caldiv(spdif_priv, clk, savesub, index,
1125 i == STC_TXCLK_SPDIF_ROOT);
Nicolin Chena2388a42013-08-21 11:13:16 +08001126 if (savesub == ret)
1127 continue;
1128
1129 savesub = ret;
1130 spdif_priv->txclk[index] = clk;
1131 spdif_priv->txclk_src[index] = i;
1132
1133 /* To quick catch a divisor, we allow a 0.1% deviation */
1134 if (savesub < 100)
1135 break;
1136 }
1137
Nicolin Chen8a309d72013-08-30 17:38:08 +08001138 dev_dbg(&pdev->dev, "use rxtx%d as tx clock source for %dHz sample rate\n",
Nicolin Chena2388a42013-08-21 11:13:16 +08001139 spdif_priv->txclk_src[index], rate[index]);
Nicolin Chene41a4a72014-04-30 18:54:06 +08001140 dev_dbg(&pdev->dev, "use txclk df %d for %dHz sample rate\n",
1141 spdif_priv->txclk_df[index], rate[index]);
Nicolin Chen27c647b2014-04-30 18:54:07 +08001142 if (spdif_priv->txclk[index] == spdif_priv->sysclk)
1143 dev_dbg(&pdev->dev, "use sysclk df %d for %dHz sample rate\n",
1144 spdif_priv->sysclk_df[index], rate[index]);
Nicolin Chen527cda72014-04-30 18:54:08 +08001145 dev_dbg(&pdev->dev, "the best rate for %dHz sample rate is %dHz\n",
1146 rate[index], spdif_priv->txrate[index]);
Nicolin Chena2388a42013-08-21 11:13:16 +08001147
1148 return 0;
1149}
1150
1151static int fsl_spdif_probe(struct platform_device *pdev)
1152{
1153 struct device_node *np = pdev->dev.of_node;
1154 struct fsl_spdif_priv *spdif_priv;
1155 struct spdif_mixer_control *ctrl;
1156 struct resource *res;
1157 void __iomem *regs;
1158 int irq, ret, i;
1159
1160 if (!np)
1161 return -ENODEV;
1162
1163 spdif_priv = devm_kzalloc(&pdev->dev,
1164 sizeof(struct fsl_spdif_priv) + strlen(np->name) + 1,
1165 GFP_KERNEL);
1166 if (!spdif_priv)
1167 return -ENOMEM;
1168
1169 strcpy(spdif_priv->name, np->name);
1170
1171 spdif_priv->pdev = pdev;
1172
1173 /* Initialize this copy of the CPU DAI driver structure */
1174 memcpy(&spdif_priv->cpu_dai_drv, &fsl_spdif_dai, sizeof(fsl_spdif_dai));
1175 spdif_priv->cpu_dai_drv.name = spdif_priv->name;
1176
Xiubo Li86f28d72014-02-11 15:42:48 +08001177 if (of_property_read_bool(np, "big-endian"))
1178 fsl_spdif_regmap_config.val_format_endian = REGMAP_ENDIAN_BIG;
1179
Nicolin Chena2388a42013-08-21 11:13:16 +08001180 /* Get the addresses and IRQ */
1181 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
Nicolin Chena2388a42013-08-21 11:13:16 +08001182 regs = devm_ioremap_resource(&pdev->dev, res);
Wei Yongjunbfd7d1a2013-08-29 08:00:05 +08001183 if (IS_ERR(regs))
Nicolin Chena2388a42013-08-21 11:13:16 +08001184 return PTR_ERR(regs);
Nicolin Chena2388a42013-08-21 11:13:16 +08001185
1186 spdif_priv->regmap = devm_regmap_init_mmio_clk(&pdev->dev,
1187 "core", regs, &fsl_spdif_regmap_config);
1188 if (IS_ERR(spdif_priv->regmap)) {
1189 dev_err(&pdev->dev, "regmap init failed\n");
1190 return PTR_ERR(spdif_priv->regmap);
1191 }
1192
1193 irq = platform_get_irq(pdev, 0);
1194 if (irq < 0) {
1195 dev_err(&pdev->dev, "no irq for node %s\n", np->full_name);
1196 return irq;
1197 }
1198
1199 ret = devm_request_irq(&pdev->dev, irq, spdif_isr, 0,
1200 spdif_priv->name, spdif_priv);
1201 if (ret) {
1202 dev_err(&pdev->dev, "could not claim irq %u\n", irq);
1203 return ret;
1204 }
1205
Nicolin Chen0b864392014-04-28 23:07:51 +08001206 /* Get system clock for rx clock rate calculation */
1207 spdif_priv->sysclk = devm_clk_get(&pdev->dev, "rxtx5");
1208 if (IS_ERR(spdif_priv->sysclk)) {
1209 dev_err(&pdev->dev, "no sys clock (rxtx5) in devicetree\n");
1210 return PTR_ERR(spdif_priv->sysclk);
1211 }
1212
Nicolin Chen08f73362014-04-24 18:52:24 +08001213 /* Get core clock for data register access via DMA */
1214 spdif_priv->coreclk = devm_clk_get(&pdev->dev, "core");
1215 if (IS_ERR(spdif_priv->coreclk)) {
1216 dev_err(&pdev->dev, "no core clock in devicetree\n");
1217 return PTR_ERR(spdif_priv->coreclk);
1218 }
1219
Nicolin Chena2388a42013-08-21 11:13:16 +08001220 /* Select clock source for rx/tx clock */
1221 spdif_priv->rxclk = devm_clk_get(&pdev->dev, "rxtx1");
1222 if (IS_ERR(spdif_priv->rxclk)) {
1223 dev_err(&pdev->dev, "no rxtx1 clock in devicetree\n");
1224 return PTR_ERR(spdif_priv->rxclk);
1225 }
1226 spdif_priv->rxclk_src = DEFAULT_RXCLK_SRC;
1227
1228 for (i = 0; i < SPDIF_TXRATE_MAX; i++) {
1229 ret = fsl_spdif_probe_txclk(spdif_priv, i);
1230 if (ret)
1231 return ret;
1232 }
1233
1234 /* Initial spinlock for control data */
1235 ctrl = &spdif_priv->fsl_spdif_control;
1236 spin_lock_init(&ctrl->ctl_lock);
1237
1238 /* Init tx channel status default value */
Nicolin Chenf3a30ba2014-05-06 16:42:25 +08001239 ctrl->ch_status[0] = IEC958_AES0_CON_NOT_COPYRIGHT |
1240 IEC958_AES0_CON_EMPHASIS_5015;
Nicolin Chena2388a42013-08-21 11:13:16 +08001241 ctrl->ch_status[1] = IEC958_AES1_CON_DIGDIGCONV_ID;
1242 ctrl->ch_status[2] = 0x00;
Nicolin Chenf3a30ba2014-05-06 16:42:25 +08001243 ctrl->ch_status[3] = IEC958_AES3_CON_FS_44100 |
1244 IEC958_AES3_CON_CLOCK_1000PPM;
Nicolin Chena2388a42013-08-21 11:13:16 +08001245
1246 spdif_priv->dpll_locked = false;
1247
1248 spdif_priv->dma_params_tx.maxburst = FSL_SPDIF_TXFIFO_WML;
1249 spdif_priv->dma_params_rx.maxburst = FSL_SPDIF_RXFIFO_WML;
1250 spdif_priv->dma_params_tx.addr = res->start + REG_SPDIF_STL;
1251 spdif_priv->dma_params_rx.addr = res->start + REG_SPDIF_SRL;
1252
1253 /* Register with ASoC */
1254 dev_set_drvdata(&pdev->dev, spdif_priv);
1255
Sachin Kamat256218a2013-09-17 10:13:49 +05301256 ret = devm_snd_soc_register_component(&pdev->dev, &fsl_spdif_component,
1257 &spdif_priv->cpu_dai_drv, 1);
Nicolin Chena2388a42013-08-21 11:13:16 +08001258 if (ret) {
1259 dev_err(&pdev->dev, "failed to register DAI: %d\n", ret);
Fabio Estevam5af407c2013-08-23 18:14:45 -03001260 return ret;
Nicolin Chena2388a42013-08-21 11:13:16 +08001261 }
1262
1263 ret = imx_pcm_dma_init(pdev);
Sachin Kamat256218a2013-09-17 10:13:49 +05301264 if (ret)
Nicolin Chena2388a42013-08-21 11:13:16 +08001265 dev_err(&pdev->dev, "imx_pcm_dma_init failed: %d\n", ret);
Nicolin Chena2388a42013-08-21 11:13:16 +08001266
1267 return ret;
1268}
1269
Nicolin Chena2388a42013-08-21 11:13:16 +08001270static const struct of_device_id fsl_spdif_dt_ids[] = {
1271 { .compatible = "fsl,imx35-spdif", },
Xiubo Li1014fad2014-04-04 15:10:29 +08001272 { .compatible = "fsl,vf610-spdif", },
Nicolin Chena2388a42013-08-21 11:13:16 +08001273 {}
1274};
1275MODULE_DEVICE_TABLE(of, fsl_spdif_dt_ids);
1276
1277static struct platform_driver fsl_spdif_driver = {
1278 .driver = {
1279 .name = "fsl-spdif-dai",
1280 .owner = THIS_MODULE,
1281 .of_match_table = fsl_spdif_dt_ids,
1282 },
1283 .probe = fsl_spdif_probe,
Nicolin Chena2388a42013-08-21 11:13:16 +08001284};
1285
1286module_platform_driver(fsl_spdif_driver);
1287
1288MODULE_AUTHOR("Freescale Semiconductor, Inc.");
1289MODULE_DESCRIPTION("Freescale S/PDIF CPU DAI Driver");
1290MODULE_LICENSE("GPL v2");
1291MODULE_ALIAS("platform:fsl-spdif-dai");