Nicolin Chen | a2388a4 | 2013-08-21 11:13:16 +0800 | [diff] [blame] | 1 | /* |
| 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 | |
| 16 | #include <linux/module.h> |
| 17 | #include <linux/clk.h> |
| 18 | #include <linux/clk-private.h> |
| 19 | #include <linux/bitrev.h> |
| 20 | #include <linux/regmap.h> |
| 21 | #include <linux/of_address.h> |
| 22 | #include <linux/of_device.h> |
| 23 | #include <linux/of_irq.h> |
| 24 | |
| 25 | #include <sound/asoundef.h> |
| 26 | #include <sound/soc.h> |
| 27 | #include <sound/dmaengine_pcm.h> |
| 28 | |
| 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 | |
| 35 | #define INTR_FOR_PLAYBACK (INT_TXFIFO_RESYNC) |
| 36 | #define INTR_FOR_CAPTURE (INT_SYM_ERR | INT_BIT_ERR | INT_URX_FUL | INT_URX_OV|\ |
| 37 | INT_QRX_FUL | INT_QRX_OV | INT_UQ_SYNC | INT_UQ_ERR |\ |
| 38 | INT_RXFIFO_RESYNC | INT_LOSS_LOCK | INT_DPLL_LOCKED) |
| 39 | |
| 40 | /* Index list for the values that has if (DPLL Locked) condition */ |
| 41 | static u8 srpc_dpll_locked[] = { 0x0, 0x1, 0x2, 0x3, 0x4, 0xa, 0xb }; |
| 42 | #define SRPC_NODPLL_START1 0x5 |
| 43 | #define SRPC_NODPLL_START2 0xc |
| 44 | |
| 45 | #define DEFAULT_RXCLK_SRC 1 |
| 46 | |
| 47 | /* |
| 48 | * SPDIF control structure |
| 49 | * Defines channel status, subcode and Q sub |
| 50 | */ |
| 51 | struct spdif_mixer_control { |
| 52 | /* spinlock to access control data */ |
| 53 | spinlock_t ctl_lock; |
| 54 | |
| 55 | /* IEC958 channel tx status bit */ |
| 56 | unsigned char ch_status[4]; |
| 57 | |
| 58 | /* User bits */ |
| 59 | unsigned char subcode[2 * SPDIF_UBITS_SIZE]; |
| 60 | |
| 61 | /* Q subcode part of user bits */ |
| 62 | unsigned char qsub[2 * SPDIF_QSUB_SIZE]; |
| 63 | |
| 64 | /* Buffer offset for U/Q */ |
| 65 | u32 upos; |
| 66 | u32 qpos; |
| 67 | |
| 68 | /* Ready buffer index of the two buffers */ |
| 69 | u32 ready_buf; |
| 70 | }; |
| 71 | |
| 72 | struct fsl_spdif_priv { |
| 73 | struct spdif_mixer_control fsl_spdif_control; |
| 74 | struct snd_soc_dai_driver cpu_dai_drv; |
| 75 | struct platform_device *pdev; |
| 76 | struct regmap *regmap; |
| 77 | bool dpll_locked; |
| 78 | u8 txclk_div[SPDIF_TXRATE_MAX]; |
| 79 | u8 txclk_src[SPDIF_TXRATE_MAX]; |
| 80 | u8 rxclk_src; |
| 81 | struct clk *txclk[SPDIF_TXRATE_MAX]; |
| 82 | struct clk *rxclk; |
Nicolin Chen | 08f7336 | 2014-04-24 18:52:24 +0800 | [diff] [blame^] | 83 | struct clk *coreclk; |
Nicolin Chen | a2388a4 | 2013-08-21 11:13:16 +0800 | [diff] [blame] | 84 | struct snd_dmaengine_dai_dma_data dma_params_tx; |
| 85 | struct snd_dmaengine_dai_dma_data dma_params_rx; |
| 86 | |
| 87 | /* The name space will be allocated dynamically */ |
| 88 | char name[0]; |
| 89 | }; |
| 90 | |
| 91 | |
| 92 | /* DPLL locked and lock loss interrupt handler */ |
| 93 | static void spdif_irq_dpll_lock(struct fsl_spdif_priv *spdif_priv) |
| 94 | { |
| 95 | struct regmap *regmap = spdif_priv->regmap; |
| 96 | struct platform_device *pdev = spdif_priv->pdev; |
| 97 | u32 locked; |
| 98 | |
| 99 | regmap_read(regmap, REG_SPDIF_SRPC, &locked); |
| 100 | locked &= SRPC_DPLL_LOCKED; |
| 101 | |
| 102 | dev_dbg(&pdev->dev, "isr: Rx dpll %s \n", |
| 103 | locked ? "locked" : "loss lock"); |
| 104 | |
| 105 | spdif_priv->dpll_locked = locked ? true : false; |
| 106 | } |
| 107 | |
| 108 | /* Receiver found illegal symbol interrupt handler */ |
| 109 | static void spdif_irq_sym_error(struct fsl_spdif_priv *spdif_priv) |
| 110 | { |
| 111 | struct regmap *regmap = spdif_priv->regmap; |
| 112 | struct platform_device *pdev = spdif_priv->pdev; |
| 113 | |
| 114 | dev_dbg(&pdev->dev, "isr: receiver found illegal symbol\n"); |
| 115 | |
| 116 | if (!spdif_priv->dpll_locked) { |
| 117 | /* DPLL unlocked seems no audio stream */ |
| 118 | regmap_update_bits(regmap, REG_SPDIF_SIE, INT_SYM_ERR, 0); |
| 119 | } |
| 120 | } |
| 121 | |
| 122 | /* U/Q Channel receive register full */ |
| 123 | static void spdif_irq_uqrx_full(struct fsl_spdif_priv *spdif_priv, char name) |
| 124 | { |
| 125 | struct spdif_mixer_control *ctrl = &spdif_priv->fsl_spdif_control; |
| 126 | struct regmap *regmap = spdif_priv->regmap; |
| 127 | struct platform_device *pdev = spdif_priv->pdev; |
| 128 | u32 *pos, size, val, reg; |
| 129 | |
| 130 | switch (name) { |
| 131 | case 'U': |
| 132 | pos = &ctrl->upos; |
| 133 | size = SPDIF_UBITS_SIZE; |
| 134 | reg = REG_SPDIF_SRU; |
| 135 | break; |
| 136 | case 'Q': |
| 137 | pos = &ctrl->qpos; |
| 138 | size = SPDIF_QSUB_SIZE; |
| 139 | reg = REG_SPDIF_SRQ; |
| 140 | break; |
| 141 | default: |
| 142 | dev_err(&pdev->dev, "unsupported channel name\n"); |
| 143 | return; |
| 144 | } |
| 145 | |
| 146 | dev_dbg(&pdev->dev, "isr: %c Channel receive register full\n", name); |
| 147 | |
| 148 | if (*pos >= size * 2) { |
| 149 | *pos = 0; |
| 150 | } else if (unlikely((*pos % size) + 3 > size)) { |
| 151 | dev_err(&pdev->dev, "User bit receivce buffer overflow\n"); |
| 152 | return; |
| 153 | } |
| 154 | |
| 155 | regmap_read(regmap, reg, &val); |
| 156 | ctrl->subcode[*pos++] = val >> 16; |
| 157 | ctrl->subcode[*pos++] = val >> 8; |
| 158 | ctrl->subcode[*pos++] = val; |
| 159 | } |
| 160 | |
| 161 | /* U/Q Channel sync found */ |
| 162 | static void spdif_irq_uq_sync(struct fsl_spdif_priv *spdif_priv) |
| 163 | { |
| 164 | struct spdif_mixer_control *ctrl = &spdif_priv->fsl_spdif_control; |
| 165 | struct platform_device *pdev = spdif_priv->pdev; |
| 166 | |
| 167 | dev_dbg(&pdev->dev, "isr: U/Q Channel sync found\n"); |
| 168 | |
| 169 | /* U/Q buffer reset */ |
| 170 | if (ctrl->qpos == 0) |
| 171 | return; |
| 172 | |
| 173 | /* Set ready to this buffer */ |
| 174 | ctrl->ready_buf = (ctrl->qpos - 1) / SPDIF_QSUB_SIZE + 1; |
| 175 | } |
| 176 | |
| 177 | /* U/Q Channel framing error */ |
| 178 | static void spdif_irq_uq_err(struct fsl_spdif_priv *spdif_priv) |
| 179 | { |
| 180 | struct spdif_mixer_control *ctrl = &spdif_priv->fsl_spdif_control; |
| 181 | struct regmap *regmap = spdif_priv->regmap; |
| 182 | struct platform_device *pdev = spdif_priv->pdev; |
| 183 | u32 val; |
| 184 | |
| 185 | dev_dbg(&pdev->dev, "isr: U/Q Channel framing error\n"); |
| 186 | |
| 187 | /* Read U/Q data to clear the irq and do buffer reset */ |
| 188 | regmap_read(regmap, REG_SPDIF_SRU, &val); |
| 189 | regmap_read(regmap, REG_SPDIF_SRQ, &val); |
| 190 | |
| 191 | /* Drop this U/Q buffer */ |
| 192 | ctrl->ready_buf = 0; |
| 193 | ctrl->upos = 0; |
| 194 | ctrl->qpos = 0; |
| 195 | } |
| 196 | |
| 197 | /* Get spdif interrupt status and clear the interrupt */ |
| 198 | static u32 spdif_intr_status_clear(struct fsl_spdif_priv *spdif_priv) |
| 199 | { |
| 200 | struct regmap *regmap = spdif_priv->regmap; |
| 201 | u32 val, val2; |
| 202 | |
| 203 | regmap_read(regmap, REG_SPDIF_SIS, &val); |
| 204 | regmap_read(regmap, REG_SPDIF_SIE, &val2); |
| 205 | |
| 206 | regmap_write(regmap, REG_SPDIF_SIC, val & val2); |
| 207 | |
| 208 | return val; |
| 209 | } |
| 210 | |
| 211 | static irqreturn_t spdif_isr(int irq, void *devid) |
| 212 | { |
| 213 | struct fsl_spdif_priv *spdif_priv = (struct fsl_spdif_priv *)devid; |
| 214 | struct platform_device *pdev = spdif_priv->pdev; |
| 215 | u32 sis; |
| 216 | |
| 217 | sis = spdif_intr_status_clear(spdif_priv); |
| 218 | |
| 219 | if (sis & INT_DPLL_LOCKED) |
| 220 | spdif_irq_dpll_lock(spdif_priv); |
| 221 | |
| 222 | if (sis & INT_TXFIFO_UNOV) |
| 223 | dev_dbg(&pdev->dev, "isr: Tx FIFO under/overrun\n"); |
| 224 | |
| 225 | if (sis & INT_TXFIFO_RESYNC) |
| 226 | dev_dbg(&pdev->dev, "isr: Tx FIFO resync\n"); |
| 227 | |
| 228 | if (sis & INT_CNEW) |
| 229 | dev_dbg(&pdev->dev, "isr: cstatus new\n"); |
| 230 | |
| 231 | if (sis & INT_VAL_NOGOOD) |
| 232 | dev_dbg(&pdev->dev, "isr: validity flag no good\n"); |
| 233 | |
| 234 | if (sis & INT_SYM_ERR) |
| 235 | spdif_irq_sym_error(spdif_priv); |
| 236 | |
| 237 | if (sis & INT_BIT_ERR) |
| 238 | dev_dbg(&pdev->dev, "isr: receiver found parity bit error\n"); |
| 239 | |
| 240 | if (sis & INT_URX_FUL) |
| 241 | spdif_irq_uqrx_full(spdif_priv, 'U'); |
| 242 | |
| 243 | if (sis & INT_URX_OV) |
| 244 | dev_dbg(&pdev->dev, "isr: U Channel receive register overrun\n"); |
| 245 | |
| 246 | if (sis & INT_QRX_FUL) |
| 247 | spdif_irq_uqrx_full(spdif_priv, 'Q'); |
| 248 | |
| 249 | if (sis & INT_QRX_OV) |
| 250 | dev_dbg(&pdev->dev, "isr: Q Channel receive register overrun\n"); |
| 251 | |
| 252 | if (sis & INT_UQ_SYNC) |
| 253 | spdif_irq_uq_sync(spdif_priv); |
| 254 | |
| 255 | if (sis & INT_UQ_ERR) |
| 256 | spdif_irq_uq_err(spdif_priv); |
| 257 | |
| 258 | if (sis & INT_RXFIFO_UNOV) |
| 259 | dev_dbg(&pdev->dev, "isr: Rx FIFO under/overrun\n"); |
| 260 | |
| 261 | if (sis & INT_RXFIFO_RESYNC) |
| 262 | dev_dbg(&pdev->dev, "isr: Rx FIFO resync\n"); |
| 263 | |
| 264 | if (sis & INT_LOSS_LOCK) |
| 265 | spdif_irq_dpll_lock(spdif_priv); |
| 266 | |
| 267 | /* FIXME: Write Tx FIFO to clear TxEm */ |
| 268 | if (sis & INT_TX_EM) |
| 269 | dev_dbg(&pdev->dev, "isr: Tx FIFO empty\n"); |
| 270 | |
| 271 | /* FIXME: Read Rx FIFO to clear RxFIFOFul */ |
| 272 | if (sis & INT_RXFIFO_FUL) |
| 273 | dev_dbg(&pdev->dev, "isr: Rx FIFO full\n"); |
| 274 | |
| 275 | return IRQ_HANDLED; |
| 276 | } |
| 277 | |
| 278 | static int spdif_softreset(struct fsl_spdif_priv *spdif_priv) |
| 279 | { |
| 280 | struct regmap *regmap = spdif_priv->regmap; |
| 281 | u32 val, cycle = 1000; |
| 282 | |
| 283 | regmap_write(regmap, REG_SPDIF_SCR, SCR_SOFT_RESET); |
| 284 | |
| 285 | /* |
| 286 | * RESET bit would be cleared after finishing its reset procedure, |
| 287 | * which typically lasts 8 cycles. 1000 cycles will keep it safe. |
| 288 | */ |
| 289 | do { |
| 290 | regmap_read(regmap, REG_SPDIF_SCR, &val); |
| 291 | } while ((val & SCR_SOFT_RESET) && cycle--); |
| 292 | |
| 293 | if (cycle) |
| 294 | return 0; |
| 295 | else |
| 296 | return -EBUSY; |
| 297 | } |
| 298 | |
| 299 | static void spdif_set_cstatus(struct spdif_mixer_control *ctrl, |
| 300 | u8 mask, u8 cstatus) |
| 301 | { |
| 302 | ctrl->ch_status[3] &= ~mask; |
| 303 | ctrl->ch_status[3] |= cstatus & mask; |
| 304 | } |
| 305 | |
| 306 | static void spdif_write_channel_status(struct fsl_spdif_priv *spdif_priv) |
| 307 | { |
| 308 | struct spdif_mixer_control *ctrl = &spdif_priv->fsl_spdif_control; |
| 309 | struct regmap *regmap = spdif_priv->regmap; |
| 310 | struct platform_device *pdev = spdif_priv->pdev; |
| 311 | u32 ch_status; |
| 312 | |
| 313 | ch_status = (bitrev8(ctrl->ch_status[0]) << 16) | |
| 314 | (bitrev8(ctrl->ch_status[1]) << 8) | |
| 315 | bitrev8(ctrl->ch_status[2]); |
| 316 | regmap_write(regmap, REG_SPDIF_STCSCH, ch_status); |
| 317 | |
| 318 | dev_dbg(&pdev->dev, "STCSCH: 0x%06x\n", ch_status); |
| 319 | |
| 320 | ch_status = bitrev8(ctrl->ch_status[3]) << 16; |
| 321 | regmap_write(regmap, REG_SPDIF_STCSCL, ch_status); |
| 322 | |
| 323 | dev_dbg(&pdev->dev, "STCSCL: 0x%06x\n", ch_status); |
| 324 | } |
| 325 | |
| 326 | /* Set SPDIF PhaseConfig register for rx clock */ |
| 327 | static int spdif_set_rx_clksrc(struct fsl_spdif_priv *spdif_priv, |
| 328 | enum spdif_gainsel gainsel, int dpll_locked) |
| 329 | { |
| 330 | struct regmap *regmap = spdif_priv->regmap; |
| 331 | u8 clksrc = spdif_priv->rxclk_src; |
| 332 | |
| 333 | if (clksrc >= SRPC_CLKSRC_MAX || gainsel >= GAINSEL_MULTI_MAX) |
| 334 | return -EINVAL; |
| 335 | |
| 336 | regmap_update_bits(regmap, REG_SPDIF_SRPC, |
| 337 | SRPC_CLKSRC_SEL_MASK | SRPC_GAINSEL_MASK, |
| 338 | SRPC_CLKSRC_SEL_SET(clksrc) | SRPC_GAINSEL_SET(gainsel)); |
| 339 | |
| 340 | return 0; |
| 341 | } |
| 342 | |
| 343 | static int spdif_set_sample_rate(struct snd_pcm_substream *substream, |
| 344 | int sample_rate) |
| 345 | { |
| 346 | struct snd_soc_pcm_runtime *rtd = substream->private_data; |
| 347 | struct fsl_spdif_priv *spdif_priv = snd_soc_dai_get_drvdata(rtd->cpu_dai); |
| 348 | struct spdif_mixer_control *ctrl = &spdif_priv->fsl_spdif_control; |
| 349 | struct regmap *regmap = spdif_priv->regmap; |
| 350 | struct platform_device *pdev = spdif_priv->pdev; |
| 351 | unsigned long csfs = 0; |
| 352 | u32 stc, mask, rate; |
| 353 | u8 clk, div; |
| 354 | int ret; |
| 355 | |
| 356 | switch (sample_rate) { |
| 357 | case 32000: |
| 358 | rate = SPDIF_TXRATE_32000; |
| 359 | csfs = IEC958_AES3_CON_FS_32000; |
| 360 | break; |
| 361 | case 44100: |
| 362 | rate = SPDIF_TXRATE_44100; |
| 363 | csfs = IEC958_AES3_CON_FS_44100; |
| 364 | break; |
| 365 | case 48000: |
| 366 | rate = SPDIF_TXRATE_48000; |
| 367 | csfs = IEC958_AES3_CON_FS_48000; |
| 368 | break; |
| 369 | default: |
| 370 | dev_err(&pdev->dev, "unsupported sample rate %d\n", sample_rate); |
| 371 | return -EINVAL; |
| 372 | } |
| 373 | |
| 374 | clk = spdif_priv->txclk_src[rate]; |
| 375 | if (clk >= STC_TXCLK_SRC_MAX) { |
| 376 | dev_err(&pdev->dev, "tx clock source is out of range\n"); |
| 377 | return -EINVAL; |
| 378 | } |
| 379 | |
| 380 | div = spdif_priv->txclk_div[rate]; |
| 381 | if (div == 0) { |
| 382 | dev_err(&pdev->dev, "the divisor can't be zero\n"); |
| 383 | return -EINVAL; |
| 384 | } |
| 385 | |
| 386 | /* |
| 387 | * The S/PDIF block needs a clock of 64 * fs * div. The S/PDIF block |
| 388 | * will divide by (div). So request 64 * fs * (div+1) which will |
| 389 | * get rounded. |
| 390 | */ |
| 391 | ret = clk_set_rate(spdif_priv->txclk[rate], 64 * sample_rate * (div + 1)); |
| 392 | if (ret) { |
| 393 | dev_err(&pdev->dev, "failed to set tx clock rate\n"); |
| 394 | return ret; |
| 395 | } |
| 396 | |
| 397 | dev_dbg(&pdev->dev, "expected clock rate = %d\n", |
| 398 | (64 * sample_rate * div)); |
| 399 | dev_dbg(&pdev->dev, "actual clock rate = %ld\n", |
| 400 | clk_get_rate(spdif_priv->txclk[rate])); |
| 401 | |
| 402 | /* set fs field in consumer channel status */ |
| 403 | spdif_set_cstatus(ctrl, IEC958_AES3_CON_FS, csfs); |
| 404 | |
| 405 | /* select clock source and divisor */ |
| 406 | stc = STC_TXCLK_ALL_EN | STC_TXCLK_SRC_SET(clk) | STC_TXCLK_DIV(div); |
| 407 | mask = STC_TXCLK_ALL_EN_MASK | STC_TXCLK_SRC_MASK | STC_TXCLK_DIV_MASK; |
| 408 | regmap_update_bits(regmap, REG_SPDIF_STC, mask, stc); |
| 409 | |
| 410 | dev_dbg(&pdev->dev, "set sample rate to %d\n", sample_rate); |
| 411 | |
| 412 | return 0; |
| 413 | } |
| 414 | |
Mark Brown | 6b4c80f | 2013-08-31 16:40:51 +0100 | [diff] [blame] | 415 | static int fsl_spdif_startup(struct snd_pcm_substream *substream, |
| 416 | struct snd_soc_dai *cpu_dai) |
Nicolin Chen | a2388a4 | 2013-08-21 11:13:16 +0800 | [diff] [blame] | 417 | { |
| 418 | struct snd_soc_pcm_runtime *rtd = substream->private_data; |
| 419 | struct fsl_spdif_priv *spdif_priv = snd_soc_dai_get_drvdata(rtd->cpu_dai); |
| 420 | struct platform_device *pdev = spdif_priv->pdev; |
| 421 | struct regmap *regmap = spdif_priv->regmap; |
| 422 | u32 scr, mask, i; |
| 423 | int ret; |
| 424 | |
| 425 | /* Reset module and interrupts only for first initialization */ |
| 426 | if (!cpu_dai->active) { |
Nicolin Chen | 08f7336 | 2014-04-24 18:52:24 +0800 | [diff] [blame^] | 427 | ret = clk_prepare_enable(spdif_priv->coreclk); |
| 428 | if (ret) { |
| 429 | dev_err(&pdev->dev, "failed to enable core clock\n"); |
| 430 | return ret; |
| 431 | } |
| 432 | |
Nicolin Chen | a2388a4 | 2013-08-21 11:13:16 +0800 | [diff] [blame] | 433 | ret = spdif_softreset(spdif_priv); |
| 434 | if (ret) { |
| 435 | dev_err(&pdev->dev, "failed to soft reset\n"); |
Nicolin Chen | 08f7336 | 2014-04-24 18:52:24 +0800 | [diff] [blame^] | 436 | goto err; |
Nicolin Chen | a2388a4 | 2013-08-21 11:13:16 +0800 | [diff] [blame] | 437 | } |
| 438 | |
| 439 | /* Disable all the interrupts */ |
| 440 | regmap_update_bits(regmap, REG_SPDIF_SIE, 0xffffff, 0); |
| 441 | } |
| 442 | |
| 443 | if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) { |
| 444 | scr = SCR_TXFIFO_AUTOSYNC | SCR_TXFIFO_CTRL_NORMAL | |
| 445 | SCR_TXSEL_NORMAL | SCR_USRC_SEL_CHIP | |
| 446 | SCR_TXFIFO_FSEL_IF8; |
| 447 | mask = SCR_TXFIFO_AUTOSYNC_MASK | SCR_TXFIFO_CTRL_MASK | |
| 448 | SCR_TXSEL_MASK | SCR_USRC_SEL_MASK | |
| 449 | SCR_TXFIFO_FSEL_MASK; |
| 450 | for (i = 0; i < SPDIF_TXRATE_MAX; i++) |
| 451 | clk_prepare_enable(spdif_priv->txclk[i]); |
| 452 | } else { |
| 453 | scr = SCR_RXFIFO_FSEL_IF8 | SCR_RXFIFO_AUTOSYNC; |
| 454 | mask = SCR_RXFIFO_FSEL_MASK | SCR_RXFIFO_AUTOSYNC_MASK| |
| 455 | SCR_RXFIFO_CTL_MASK | SCR_RXFIFO_OFF_MASK; |
| 456 | clk_prepare_enable(spdif_priv->rxclk); |
| 457 | } |
| 458 | regmap_update_bits(regmap, REG_SPDIF_SCR, mask, scr); |
| 459 | |
| 460 | /* Power up SPDIF module */ |
| 461 | regmap_update_bits(regmap, REG_SPDIF_SCR, SCR_LOW_POWER, 0); |
| 462 | |
| 463 | return 0; |
Nicolin Chen | 08f7336 | 2014-04-24 18:52:24 +0800 | [diff] [blame^] | 464 | |
| 465 | err: |
| 466 | clk_disable_unprepare(spdif_priv->coreclk); |
| 467 | |
| 468 | return ret; |
Nicolin Chen | a2388a4 | 2013-08-21 11:13:16 +0800 | [diff] [blame] | 469 | } |
| 470 | |
| 471 | static void fsl_spdif_shutdown(struct snd_pcm_substream *substream, |
| 472 | struct snd_soc_dai *cpu_dai) |
| 473 | { |
| 474 | struct snd_soc_pcm_runtime *rtd = substream->private_data; |
| 475 | struct fsl_spdif_priv *spdif_priv = snd_soc_dai_get_drvdata(rtd->cpu_dai); |
| 476 | struct regmap *regmap = spdif_priv->regmap; |
| 477 | u32 scr, mask, i; |
| 478 | |
| 479 | if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) { |
| 480 | scr = 0; |
| 481 | mask = SCR_TXFIFO_AUTOSYNC_MASK | SCR_TXFIFO_CTRL_MASK | |
| 482 | SCR_TXSEL_MASK | SCR_USRC_SEL_MASK | |
| 483 | SCR_TXFIFO_FSEL_MASK; |
| 484 | for (i = 0; i < SPDIF_TXRATE_MAX; i++) |
| 485 | clk_disable_unprepare(spdif_priv->txclk[i]); |
| 486 | } else { |
| 487 | scr = SCR_RXFIFO_OFF | SCR_RXFIFO_CTL_ZERO; |
| 488 | mask = SCR_RXFIFO_FSEL_MASK | SCR_RXFIFO_AUTOSYNC_MASK| |
| 489 | SCR_RXFIFO_CTL_MASK | SCR_RXFIFO_OFF_MASK; |
| 490 | clk_disable_unprepare(spdif_priv->rxclk); |
| 491 | } |
| 492 | regmap_update_bits(regmap, REG_SPDIF_SCR, mask, scr); |
| 493 | |
| 494 | /* Power down SPDIF module only if tx&rx are both inactive */ |
| 495 | if (!cpu_dai->active) { |
| 496 | spdif_intr_status_clear(spdif_priv); |
| 497 | regmap_update_bits(regmap, REG_SPDIF_SCR, |
| 498 | SCR_LOW_POWER, SCR_LOW_POWER); |
Nicolin Chen | 08f7336 | 2014-04-24 18:52:24 +0800 | [diff] [blame^] | 499 | clk_disable_unprepare(spdif_priv->coreclk); |
Nicolin Chen | a2388a4 | 2013-08-21 11:13:16 +0800 | [diff] [blame] | 500 | } |
| 501 | } |
| 502 | |
| 503 | static int fsl_spdif_hw_params(struct snd_pcm_substream *substream, |
| 504 | struct snd_pcm_hw_params *params, |
| 505 | struct snd_soc_dai *dai) |
| 506 | { |
| 507 | struct snd_soc_pcm_runtime *rtd = substream->private_data; |
| 508 | struct fsl_spdif_priv *spdif_priv = snd_soc_dai_get_drvdata(rtd->cpu_dai); |
| 509 | struct spdif_mixer_control *ctrl = &spdif_priv->fsl_spdif_control; |
| 510 | struct platform_device *pdev = spdif_priv->pdev; |
| 511 | u32 sample_rate = params_rate(params); |
| 512 | int ret = 0; |
| 513 | |
| 514 | if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) { |
| 515 | ret = spdif_set_sample_rate(substream, sample_rate); |
| 516 | if (ret) { |
| 517 | dev_err(&pdev->dev, "%s: set sample rate failed: %d\n", |
| 518 | __func__, sample_rate); |
| 519 | return ret; |
| 520 | } |
| 521 | spdif_set_cstatus(ctrl, IEC958_AES3_CON_CLOCK, |
| 522 | IEC958_AES3_CON_CLOCK_1000PPM); |
| 523 | spdif_write_channel_status(spdif_priv); |
| 524 | } else { |
| 525 | /* Setup rx clock source */ |
| 526 | ret = spdif_set_rx_clksrc(spdif_priv, SPDIF_DEFAULT_GAINSEL, 1); |
| 527 | } |
| 528 | |
| 529 | return ret; |
| 530 | } |
| 531 | |
| 532 | static int fsl_spdif_trigger(struct snd_pcm_substream *substream, |
| 533 | int cmd, struct snd_soc_dai *dai) |
| 534 | { |
| 535 | struct snd_soc_pcm_runtime *rtd = substream->private_data; |
| 536 | struct fsl_spdif_priv *spdif_priv = snd_soc_dai_get_drvdata(rtd->cpu_dai); |
| 537 | struct regmap *regmap = spdif_priv->regmap; |
| 538 | int is_playack = (substream->stream == SNDRV_PCM_STREAM_PLAYBACK); |
| 539 | u32 intr = is_playack ? INTR_FOR_PLAYBACK : INTR_FOR_CAPTURE; |
| 540 | u32 dmaen = is_playack ? SCR_DMA_TX_EN : SCR_DMA_RX_EN;; |
| 541 | |
| 542 | switch (cmd) { |
| 543 | case SNDRV_PCM_TRIGGER_START: |
| 544 | case SNDRV_PCM_TRIGGER_RESUME: |
| 545 | case SNDRV_PCM_TRIGGER_PAUSE_RELEASE: |
| 546 | regmap_update_bits(regmap, REG_SPDIF_SIE, intr, intr); |
| 547 | regmap_update_bits(regmap, REG_SPDIF_SCR, dmaen, dmaen); |
| 548 | break; |
| 549 | case SNDRV_PCM_TRIGGER_STOP: |
| 550 | case SNDRV_PCM_TRIGGER_SUSPEND: |
| 551 | case SNDRV_PCM_TRIGGER_PAUSE_PUSH: |
| 552 | regmap_update_bits(regmap, REG_SPDIF_SCR, dmaen, 0); |
| 553 | regmap_update_bits(regmap, REG_SPDIF_SIE, intr, 0); |
| 554 | break; |
| 555 | default: |
| 556 | return -EINVAL; |
| 557 | } |
| 558 | |
| 559 | return 0; |
| 560 | } |
| 561 | |
Mark Brown | 6b4c80f | 2013-08-31 16:40:51 +0100 | [diff] [blame] | 562 | static struct snd_soc_dai_ops fsl_spdif_dai_ops = { |
Nicolin Chen | a2388a4 | 2013-08-21 11:13:16 +0800 | [diff] [blame] | 563 | .startup = fsl_spdif_startup, |
| 564 | .hw_params = fsl_spdif_hw_params, |
| 565 | .trigger = fsl_spdif_trigger, |
| 566 | .shutdown = fsl_spdif_shutdown, |
| 567 | }; |
| 568 | |
| 569 | |
| 570 | /* |
Nicolin Chen | a2388a4 | 2013-08-21 11:13:16 +0800 | [diff] [blame] | 571 | * FSL SPDIF IEC958 controller(mixer) functions |
| 572 | * |
| 573 | * Channel status get/put control |
| 574 | * User bit value get/put control |
| 575 | * Valid bit value get control |
| 576 | * DPLL lock status get control |
| 577 | * User bit sync mode selection control |
Nicolin Chen | a2388a4 | 2013-08-21 11:13:16 +0800 | [diff] [blame] | 578 | */ |
| 579 | |
| 580 | static int fsl_spdif_info(struct snd_kcontrol *kcontrol, |
| 581 | struct snd_ctl_elem_info *uinfo) |
| 582 | { |
| 583 | uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958; |
| 584 | uinfo->count = 1; |
| 585 | |
| 586 | return 0; |
| 587 | } |
| 588 | |
| 589 | static int fsl_spdif_pb_get(struct snd_kcontrol *kcontrol, |
| 590 | struct snd_ctl_elem_value *uvalue) |
| 591 | { |
| 592 | struct snd_soc_dai *cpu_dai = snd_kcontrol_chip(kcontrol); |
| 593 | struct fsl_spdif_priv *spdif_priv = snd_soc_dai_get_drvdata(cpu_dai); |
| 594 | struct spdif_mixer_control *ctrl = &spdif_priv->fsl_spdif_control; |
| 595 | |
| 596 | uvalue->value.iec958.status[0] = ctrl->ch_status[0]; |
| 597 | uvalue->value.iec958.status[1] = ctrl->ch_status[1]; |
| 598 | uvalue->value.iec958.status[2] = ctrl->ch_status[2]; |
| 599 | uvalue->value.iec958.status[3] = ctrl->ch_status[3]; |
| 600 | |
| 601 | return 0; |
| 602 | } |
| 603 | |
| 604 | static int fsl_spdif_pb_put(struct snd_kcontrol *kcontrol, |
| 605 | struct snd_ctl_elem_value *uvalue) |
| 606 | { |
| 607 | struct snd_soc_dai *cpu_dai = snd_kcontrol_chip(kcontrol); |
| 608 | struct fsl_spdif_priv *spdif_priv = snd_soc_dai_get_drvdata(cpu_dai); |
| 609 | struct spdif_mixer_control *ctrl = &spdif_priv->fsl_spdif_control; |
| 610 | |
| 611 | ctrl->ch_status[0] = uvalue->value.iec958.status[0]; |
| 612 | ctrl->ch_status[1] = uvalue->value.iec958.status[1]; |
| 613 | ctrl->ch_status[2] = uvalue->value.iec958.status[2]; |
| 614 | ctrl->ch_status[3] = uvalue->value.iec958.status[3]; |
| 615 | |
| 616 | spdif_write_channel_status(spdif_priv); |
| 617 | |
| 618 | return 0; |
| 619 | } |
| 620 | |
| 621 | /* Get channel status from SPDIF_RX_CCHAN register */ |
| 622 | static int fsl_spdif_capture_get(struct snd_kcontrol *kcontrol, |
| 623 | struct snd_ctl_elem_value *ucontrol) |
| 624 | { |
| 625 | struct snd_soc_dai *cpu_dai = snd_kcontrol_chip(kcontrol); |
| 626 | struct fsl_spdif_priv *spdif_priv = snd_soc_dai_get_drvdata(cpu_dai); |
| 627 | struct regmap *regmap = spdif_priv->regmap; |
| 628 | u32 cstatus, val; |
| 629 | |
| 630 | regmap_read(regmap, REG_SPDIF_SIS, &val); |
| 631 | if (!(val & INT_CNEW)) { |
| 632 | return -EAGAIN; |
| 633 | } |
| 634 | |
| 635 | regmap_read(regmap, REG_SPDIF_SRCSH, &cstatus); |
| 636 | ucontrol->value.iec958.status[0] = (cstatus >> 16) & 0xFF; |
| 637 | ucontrol->value.iec958.status[1] = (cstatus >> 8) & 0xFF; |
| 638 | ucontrol->value.iec958.status[2] = cstatus & 0xFF; |
| 639 | |
| 640 | regmap_read(regmap, REG_SPDIF_SRCSL, &cstatus); |
| 641 | ucontrol->value.iec958.status[3] = (cstatus >> 16) & 0xFF; |
| 642 | ucontrol->value.iec958.status[4] = (cstatus >> 8) & 0xFF; |
| 643 | ucontrol->value.iec958.status[5] = cstatus & 0xFF; |
| 644 | |
| 645 | /* Clear intr */ |
| 646 | regmap_write(regmap, REG_SPDIF_SIC, INT_CNEW); |
| 647 | |
| 648 | return 0; |
| 649 | } |
| 650 | |
| 651 | /* |
| 652 | * Get User bits (subcode) from chip value which readed out |
| 653 | * in UChannel register. |
| 654 | */ |
| 655 | static int fsl_spdif_subcode_get(struct snd_kcontrol *kcontrol, |
| 656 | struct snd_ctl_elem_value *ucontrol) |
| 657 | { |
| 658 | struct snd_soc_dai *cpu_dai = snd_kcontrol_chip(kcontrol); |
| 659 | struct fsl_spdif_priv *spdif_priv = snd_soc_dai_get_drvdata(cpu_dai); |
| 660 | struct spdif_mixer_control *ctrl = &spdif_priv->fsl_spdif_control; |
| 661 | unsigned long flags; |
| 662 | int ret = 0; |
| 663 | |
| 664 | spin_lock_irqsave(&ctrl->ctl_lock, flags); |
| 665 | if (ctrl->ready_buf) { |
| 666 | int idx = (ctrl->ready_buf - 1) * SPDIF_UBITS_SIZE; |
| 667 | memcpy(&ucontrol->value.iec958.subcode[0], |
| 668 | &ctrl->subcode[idx], SPDIF_UBITS_SIZE); |
| 669 | } else { |
| 670 | ret = -EAGAIN; |
| 671 | } |
| 672 | spin_unlock_irqrestore(&ctrl->ctl_lock, flags); |
| 673 | |
| 674 | return ret; |
| 675 | } |
| 676 | |
| 677 | /* Q-subcode infomation. The byte size is SPDIF_UBITS_SIZE/8 */ |
| 678 | static int fsl_spdif_qinfo(struct snd_kcontrol *kcontrol, |
| 679 | struct snd_ctl_elem_info *uinfo) |
| 680 | { |
| 681 | uinfo->type = SNDRV_CTL_ELEM_TYPE_BYTES; |
| 682 | uinfo->count = SPDIF_QSUB_SIZE; |
| 683 | |
| 684 | return 0; |
| 685 | } |
| 686 | |
| 687 | /* Get Q subcode from chip value which readed out in QChannel register */ |
| 688 | static int fsl_spdif_qget(struct snd_kcontrol *kcontrol, |
| 689 | struct snd_ctl_elem_value *ucontrol) |
| 690 | { |
| 691 | struct snd_soc_dai *cpu_dai = snd_kcontrol_chip(kcontrol); |
| 692 | struct fsl_spdif_priv *spdif_priv = snd_soc_dai_get_drvdata(cpu_dai); |
| 693 | struct spdif_mixer_control *ctrl = &spdif_priv->fsl_spdif_control; |
| 694 | unsigned long flags; |
| 695 | int ret = 0; |
| 696 | |
| 697 | spin_lock_irqsave(&ctrl->ctl_lock, flags); |
| 698 | if (ctrl->ready_buf) { |
| 699 | int idx = (ctrl->ready_buf - 1) * SPDIF_QSUB_SIZE; |
| 700 | memcpy(&ucontrol->value.bytes.data[0], |
| 701 | &ctrl->qsub[idx], SPDIF_QSUB_SIZE); |
| 702 | } else { |
| 703 | ret = -EAGAIN; |
| 704 | } |
| 705 | spin_unlock_irqrestore(&ctrl->ctl_lock, flags); |
| 706 | |
| 707 | return ret; |
| 708 | } |
| 709 | |
| 710 | /* Valid bit infomation */ |
| 711 | static int fsl_spdif_vbit_info(struct snd_kcontrol *kcontrol, |
| 712 | struct snd_ctl_elem_info *uinfo) |
| 713 | { |
| 714 | uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN; |
| 715 | uinfo->count = 1; |
| 716 | uinfo->value.integer.min = 0; |
| 717 | uinfo->value.integer.max = 1; |
| 718 | |
| 719 | return 0; |
| 720 | } |
| 721 | |
| 722 | /* Get valid good bit from interrupt status register */ |
| 723 | static int fsl_spdif_vbit_get(struct snd_kcontrol *kcontrol, |
| 724 | struct snd_ctl_elem_value *ucontrol) |
| 725 | { |
| 726 | struct snd_soc_dai *cpu_dai = snd_kcontrol_chip(kcontrol); |
| 727 | struct fsl_spdif_priv *spdif_priv = snd_soc_dai_get_drvdata(cpu_dai); |
| 728 | struct regmap *regmap = spdif_priv->regmap; |
| 729 | u32 val; |
| 730 | |
| 731 | val = regmap_read(regmap, REG_SPDIF_SIS, &val); |
| 732 | ucontrol->value.integer.value[0] = (val & INT_VAL_NOGOOD) != 0; |
| 733 | regmap_write(regmap, REG_SPDIF_SIC, INT_VAL_NOGOOD); |
| 734 | |
| 735 | return 0; |
| 736 | } |
| 737 | |
| 738 | /* DPLL lock infomation */ |
| 739 | static int fsl_spdif_rxrate_info(struct snd_kcontrol *kcontrol, |
| 740 | struct snd_ctl_elem_info *uinfo) |
| 741 | { |
| 742 | uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER; |
| 743 | uinfo->count = 1; |
| 744 | uinfo->value.integer.min = 16000; |
| 745 | uinfo->value.integer.max = 96000; |
| 746 | |
| 747 | return 0; |
| 748 | } |
| 749 | |
| 750 | static u32 gainsel_multi[GAINSEL_MULTI_MAX] = { |
| 751 | 24, 16, 12, 8, 6, 4, 3, |
| 752 | }; |
| 753 | |
| 754 | /* Get RX data clock rate given the SPDIF bus_clk */ |
| 755 | static int spdif_get_rxclk_rate(struct fsl_spdif_priv *spdif_priv, |
| 756 | enum spdif_gainsel gainsel) |
| 757 | { |
| 758 | struct regmap *regmap = spdif_priv->regmap; |
| 759 | struct platform_device *pdev = spdif_priv->pdev; |
| 760 | u64 tmpval64, busclk_freq = 0; |
| 761 | u32 freqmeas, phaseconf; |
| 762 | u8 clksrc; |
| 763 | |
| 764 | regmap_read(regmap, REG_SPDIF_SRFM, &freqmeas); |
| 765 | regmap_read(regmap, REG_SPDIF_SRPC, &phaseconf); |
| 766 | |
| 767 | clksrc = (phaseconf >> SRPC_CLKSRC_SEL_OFFSET) & 0xf; |
| 768 | if (srpc_dpll_locked[clksrc] && (phaseconf & SRPC_DPLL_LOCKED)) { |
| 769 | /* Get bus clock from system */ |
| 770 | busclk_freq = clk_get_rate(spdif_priv->rxclk); |
| 771 | } |
| 772 | |
| 773 | /* FreqMeas_CLK = (BUS_CLK * FreqMeas) / 2 ^ 10 / GAINSEL / 128 */ |
| 774 | tmpval64 = (u64) busclk_freq * freqmeas; |
| 775 | do_div(tmpval64, gainsel_multi[gainsel] * 1024); |
| 776 | do_div(tmpval64, 128 * 1024); |
| 777 | |
| 778 | dev_dbg(&pdev->dev, "FreqMeas: %d\n", freqmeas); |
| 779 | dev_dbg(&pdev->dev, "BusclkFreq: %lld\n", busclk_freq); |
| 780 | dev_dbg(&pdev->dev, "RxRate: %lld\n", tmpval64); |
| 781 | |
| 782 | return (int)tmpval64; |
| 783 | } |
| 784 | |
| 785 | /* |
| 786 | * Get DPLL lock or not info from stable interrupt status register. |
| 787 | * User application must use this control to get locked, |
| 788 | * then can do next PCM operation |
| 789 | */ |
| 790 | static int fsl_spdif_rxrate_get(struct snd_kcontrol *kcontrol, |
| 791 | struct snd_ctl_elem_value *ucontrol) |
| 792 | { |
| 793 | struct snd_soc_dai *cpu_dai = snd_kcontrol_chip(kcontrol); |
| 794 | struct fsl_spdif_priv *spdif_priv = snd_soc_dai_get_drvdata(cpu_dai); |
| 795 | int rate = spdif_get_rxclk_rate(spdif_priv, SPDIF_DEFAULT_GAINSEL); |
| 796 | |
| 797 | if (spdif_priv->dpll_locked) |
| 798 | ucontrol->value.integer.value[0] = rate; |
| 799 | else |
| 800 | ucontrol->value.integer.value[0] = 0; |
| 801 | |
| 802 | return 0; |
| 803 | } |
| 804 | |
| 805 | /* User bit sync mode info */ |
| 806 | static int fsl_spdif_usync_info(struct snd_kcontrol *kcontrol, |
| 807 | struct snd_ctl_elem_info *uinfo) |
| 808 | { |
| 809 | uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN; |
| 810 | uinfo->count = 1; |
| 811 | uinfo->value.integer.min = 0; |
| 812 | uinfo->value.integer.max = 1; |
| 813 | |
| 814 | return 0; |
| 815 | } |
| 816 | |
| 817 | /* |
| 818 | * User bit sync mode: |
| 819 | * 1 CD User channel subcode |
| 820 | * 0 Non-CD data |
| 821 | */ |
| 822 | static int fsl_spdif_usync_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); |
| 827 | struct regmap *regmap = spdif_priv->regmap; |
| 828 | u32 val; |
| 829 | |
| 830 | regmap_read(regmap, REG_SPDIF_SRCD, &val); |
| 831 | ucontrol->value.integer.value[0] = (val & SRCD_CD_USER) != 0; |
| 832 | |
| 833 | return 0; |
| 834 | } |
| 835 | |
| 836 | /* |
| 837 | * User bit sync mode: |
| 838 | * 1 CD User channel subcode |
| 839 | * 0 Non-CD data |
| 840 | */ |
| 841 | static int fsl_spdif_usync_put(struct snd_kcontrol *kcontrol, |
| 842 | struct snd_ctl_elem_value *ucontrol) |
| 843 | { |
| 844 | struct snd_soc_dai *cpu_dai = snd_kcontrol_chip(kcontrol); |
| 845 | struct fsl_spdif_priv *spdif_priv = snd_soc_dai_get_drvdata(cpu_dai); |
| 846 | struct regmap *regmap = spdif_priv->regmap; |
| 847 | u32 val = ucontrol->value.integer.value[0] << SRCD_CD_USER_OFFSET; |
| 848 | |
| 849 | regmap_update_bits(regmap, REG_SPDIF_SRCD, SRCD_CD_USER, val); |
| 850 | |
| 851 | return 0; |
| 852 | } |
| 853 | |
| 854 | /* FSL SPDIF IEC958 controller defines */ |
| 855 | static struct snd_kcontrol_new fsl_spdif_ctrls[] = { |
| 856 | /* Status cchanel controller */ |
| 857 | { |
| 858 | .iface = SNDRV_CTL_ELEM_IFACE_MIXER, |
| 859 | .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, DEFAULT), |
| 860 | .access = SNDRV_CTL_ELEM_ACCESS_READ | |
| 861 | SNDRV_CTL_ELEM_ACCESS_WRITE | |
| 862 | SNDRV_CTL_ELEM_ACCESS_VOLATILE, |
| 863 | .info = fsl_spdif_info, |
| 864 | .get = fsl_spdif_pb_get, |
| 865 | .put = fsl_spdif_pb_put, |
| 866 | }, |
| 867 | { |
| 868 | .iface = SNDRV_CTL_ELEM_IFACE_PCM, |
| 869 | .name = SNDRV_CTL_NAME_IEC958("", CAPTURE, DEFAULT), |
| 870 | .access = SNDRV_CTL_ELEM_ACCESS_READ | |
| 871 | SNDRV_CTL_ELEM_ACCESS_VOLATILE, |
| 872 | .info = fsl_spdif_info, |
| 873 | .get = fsl_spdif_capture_get, |
| 874 | }, |
| 875 | /* User bits controller */ |
| 876 | { |
| 877 | .iface = SNDRV_CTL_ELEM_IFACE_PCM, |
| 878 | .name = "IEC958 Subcode Capture Default", |
| 879 | .access = SNDRV_CTL_ELEM_ACCESS_READ | |
| 880 | SNDRV_CTL_ELEM_ACCESS_VOLATILE, |
| 881 | .info = fsl_spdif_info, |
| 882 | .get = fsl_spdif_subcode_get, |
| 883 | }, |
| 884 | { |
| 885 | .iface = SNDRV_CTL_ELEM_IFACE_PCM, |
| 886 | .name = "IEC958 Q-subcode Capture Default", |
| 887 | .access = SNDRV_CTL_ELEM_ACCESS_READ | |
| 888 | SNDRV_CTL_ELEM_ACCESS_VOLATILE, |
| 889 | .info = fsl_spdif_qinfo, |
| 890 | .get = fsl_spdif_qget, |
| 891 | }, |
| 892 | /* Valid bit error controller */ |
| 893 | { |
| 894 | .iface = SNDRV_CTL_ELEM_IFACE_PCM, |
| 895 | .name = "IEC958 V-Bit Errors", |
| 896 | .access = SNDRV_CTL_ELEM_ACCESS_READ | |
| 897 | SNDRV_CTL_ELEM_ACCESS_VOLATILE, |
| 898 | .info = fsl_spdif_vbit_info, |
| 899 | .get = fsl_spdif_vbit_get, |
| 900 | }, |
| 901 | /* DPLL lock info get controller */ |
| 902 | { |
| 903 | .iface = SNDRV_CTL_ELEM_IFACE_PCM, |
| 904 | .name = "RX Sample Rate", |
| 905 | .access = SNDRV_CTL_ELEM_ACCESS_READ | |
| 906 | SNDRV_CTL_ELEM_ACCESS_VOLATILE, |
| 907 | .info = fsl_spdif_rxrate_info, |
| 908 | .get = fsl_spdif_rxrate_get, |
| 909 | }, |
| 910 | /* User bit sync mode set/get controller */ |
| 911 | { |
| 912 | .iface = SNDRV_CTL_ELEM_IFACE_PCM, |
| 913 | .name = "IEC958 USyncMode CDText", |
| 914 | .access = SNDRV_CTL_ELEM_ACCESS_READ | |
| 915 | SNDRV_CTL_ELEM_ACCESS_WRITE | |
| 916 | SNDRV_CTL_ELEM_ACCESS_VOLATILE, |
| 917 | .info = fsl_spdif_usync_info, |
| 918 | .get = fsl_spdif_usync_get, |
| 919 | .put = fsl_spdif_usync_put, |
| 920 | }, |
| 921 | }; |
| 922 | |
| 923 | static int fsl_spdif_dai_probe(struct snd_soc_dai *dai) |
| 924 | { |
| 925 | struct fsl_spdif_priv *spdif_private = snd_soc_dai_get_drvdata(dai); |
| 926 | |
Xiubo Li | 05cf482 | 2014-01-20 15:27:26 +0800 | [diff] [blame] | 927 | snd_soc_dai_init_dma_data(dai, &spdif_private->dma_params_tx, |
| 928 | &spdif_private->dma_params_rx); |
Nicolin Chen | a2388a4 | 2013-08-21 11:13:16 +0800 | [diff] [blame] | 929 | |
| 930 | snd_soc_add_dai_controls(dai, fsl_spdif_ctrls, ARRAY_SIZE(fsl_spdif_ctrls)); |
| 931 | |
| 932 | return 0; |
| 933 | } |
| 934 | |
Mark Brown | 6b4c80f | 2013-08-31 16:40:51 +0100 | [diff] [blame] | 935 | static struct snd_soc_dai_driver fsl_spdif_dai = { |
Nicolin Chen | a2388a4 | 2013-08-21 11:13:16 +0800 | [diff] [blame] | 936 | .probe = &fsl_spdif_dai_probe, |
| 937 | .playback = { |
| 938 | .channels_min = 2, |
| 939 | .channels_max = 2, |
| 940 | .rates = FSL_SPDIF_RATES_PLAYBACK, |
| 941 | .formats = FSL_SPDIF_FORMATS_PLAYBACK, |
| 942 | }, |
| 943 | .capture = { |
| 944 | .channels_min = 2, |
| 945 | .channels_max = 2, |
| 946 | .rates = FSL_SPDIF_RATES_CAPTURE, |
| 947 | .formats = FSL_SPDIF_FORMATS_CAPTURE, |
| 948 | }, |
| 949 | .ops = &fsl_spdif_dai_ops, |
| 950 | }; |
| 951 | |
| 952 | static const struct snd_soc_component_driver fsl_spdif_component = { |
| 953 | .name = "fsl-spdif", |
| 954 | }; |
| 955 | |
Fabio Estevam | 6d22db4 | 2013-08-23 18:14:46 -0300 | [diff] [blame] | 956 | /* FSL SPDIF REGMAP */ |
Nicolin Chen | a2388a4 | 2013-08-21 11:13:16 +0800 | [diff] [blame] | 957 | |
| 958 | static bool fsl_spdif_readable_reg(struct device *dev, unsigned int reg) |
| 959 | { |
| 960 | switch (reg) { |
| 961 | case REG_SPDIF_SCR: |
| 962 | case REG_SPDIF_SRCD: |
| 963 | case REG_SPDIF_SRPC: |
| 964 | case REG_SPDIF_SIE: |
| 965 | case REG_SPDIF_SIS: |
| 966 | case REG_SPDIF_SRL: |
| 967 | case REG_SPDIF_SRR: |
| 968 | case REG_SPDIF_SRCSH: |
| 969 | case REG_SPDIF_SRCSL: |
| 970 | case REG_SPDIF_SRU: |
| 971 | case REG_SPDIF_SRQ: |
| 972 | case REG_SPDIF_STCSCH: |
| 973 | case REG_SPDIF_STCSCL: |
| 974 | case REG_SPDIF_SRFM: |
| 975 | case REG_SPDIF_STC: |
| 976 | return true; |
| 977 | default: |
| 978 | return false; |
Sachin Kamat | e19bcb6 | 2013-09-13 15:52:42 +0530 | [diff] [blame] | 979 | } |
Nicolin Chen | a2388a4 | 2013-08-21 11:13:16 +0800 | [diff] [blame] | 980 | } |
| 981 | |
| 982 | static bool fsl_spdif_writeable_reg(struct device *dev, unsigned int reg) |
| 983 | { |
| 984 | switch (reg) { |
| 985 | case REG_SPDIF_SCR: |
| 986 | case REG_SPDIF_SRCD: |
| 987 | case REG_SPDIF_SRPC: |
| 988 | case REG_SPDIF_SIE: |
| 989 | case REG_SPDIF_SIC: |
| 990 | case REG_SPDIF_STL: |
| 991 | case REG_SPDIF_STR: |
| 992 | case REG_SPDIF_STCSCH: |
| 993 | case REG_SPDIF_STCSCL: |
| 994 | case REG_SPDIF_STC: |
| 995 | return true; |
| 996 | default: |
| 997 | return false; |
Sachin Kamat | e19bcb6 | 2013-09-13 15:52:42 +0530 | [diff] [blame] | 998 | } |
Nicolin Chen | a2388a4 | 2013-08-21 11:13:16 +0800 | [diff] [blame] | 999 | } |
| 1000 | |
Xiubo Li | 86f28d7 | 2014-02-11 15:42:48 +0800 | [diff] [blame] | 1001 | static struct regmap_config fsl_spdif_regmap_config = { |
Nicolin Chen | a2388a4 | 2013-08-21 11:13:16 +0800 | [diff] [blame] | 1002 | .reg_bits = 32, |
| 1003 | .reg_stride = 4, |
| 1004 | .val_bits = 32, |
| 1005 | |
| 1006 | .max_register = REG_SPDIF_STC, |
| 1007 | .readable_reg = fsl_spdif_readable_reg, |
| 1008 | .writeable_reg = fsl_spdif_writeable_reg, |
| 1009 | }; |
| 1010 | |
| 1011 | static u32 fsl_spdif_txclk_caldiv(struct fsl_spdif_priv *spdif_priv, |
| 1012 | struct clk *clk, u64 savesub, |
| 1013 | enum spdif_txrate index) |
| 1014 | { |
| 1015 | const u32 rate[] = { 32000, 44100, 48000 }; |
| 1016 | u64 rate_ideal, rate_actual, sub; |
| 1017 | u32 div, arate; |
| 1018 | |
| 1019 | for (div = 1; div <= 128; div++) { |
| 1020 | rate_ideal = rate[index] * (div + 1) * 64; |
| 1021 | rate_actual = clk_round_rate(clk, rate_ideal); |
| 1022 | |
| 1023 | arate = rate_actual / 64; |
| 1024 | arate /= div; |
| 1025 | |
| 1026 | if (arate == rate[index]) { |
| 1027 | /* We are lucky */ |
| 1028 | savesub = 0; |
| 1029 | spdif_priv->txclk_div[index] = div; |
| 1030 | break; |
| 1031 | } else if (arate / rate[index] == 1) { |
| 1032 | /* A little bigger than expect */ |
| 1033 | sub = (arate - rate[index]) * 100000; |
| 1034 | do_div(sub, rate[index]); |
| 1035 | if (sub < savesub) { |
| 1036 | savesub = sub; |
| 1037 | spdif_priv->txclk_div[index] = div; |
| 1038 | } |
| 1039 | } else if (rate[index] / arate == 1) { |
| 1040 | /* A little smaller than expect */ |
| 1041 | sub = (rate[index] - arate) * 100000; |
| 1042 | do_div(sub, rate[index]); |
| 1043 | if (sub < savesub) { |
| 1044 | savesub = sub; |
| 1045 | spdif_priv->txclk_div[index] = div; |
| 1046 | } |
| 1047 | } |
| 1048 | } |
| 1049 | |
| 1050 | return savesub; |
| 1051 | } |
| 1052 | |
| 1053 | static int fsl_spdif_probe_txclk(struct fsl_spdif_priv *spdif_priv, |
| 1054 | enum spdif_txrate index) |
| 1055 | { |
| 1056 | const u32 rate[] = { 32000, 44100, 48000 }; |
| 1057 | struct platform_device *pdev = spdif_priv->pdev; |
| 1058 | struct device *dev = &pdev->dev; |
| 1059 | u64 savesub = 100000, ret; |
| 1060 | struct clk *clk; |
| 1061 | char tmp[16]; |
| 1062 | int i; |
| 1063 | |
| 1064 | for (i = 0; i < STC_TXCLK_SRC_MAX; i++) { |
| 1065 | sprintf(tmp, "rxtx%d", i); |
| 1066 | clk = devm_clk_get(&pdev->dev, tmp); |
| 1067 | if (IS_ERR(clk)) { |
| 1068 | dev_err(dev, "no rxtx%d clock in devicetree\n", i); |
| 1069 | return PTR_ERR(clk); |
| 1070 | } |
| 1071 | if (!clk_get_rate(clk)) |
| 1072 | continue; |
| 1073 | |
| 1074 | ret = fsl_spdif_txclk_caldiv(spdif_priv, clk, savesub, index); |
| 1075 | if (savesub == ret) |
| 1076 | continue; |
| 1077 | |
| 1078 | savesub = ret; |
| 1079 | spdif_priv->txclk[index] = clk; |
| 1080 | spdif_priv->txclk_src[index] = i; |
| 1081 | |
| 1082 | /* To quick catch a divisor, we allow a 0.1% deviation */ |
| 1083 | if (savesub < 100) |
| 1084 | break; |
| 1085 | } |
| 1086 | |
Nicolin Chen | 8a309d7 | 2013-08-30 17:38:08 +0800 | [diff] [blame] | 1087 | dev_dbg(&pdev->dev, "use rxtx%d as tx clock source for %dHz sample rate\n", |
Nicolin Chen | a2388a4 | 2013-08-21 11:13:16 +0800 | [diff] [blame] | 1088 | spdif_priv->txclk_src[index], rate[index]); |
Nicolin Chen | 8a309d7 | 2013-08-30 17:38:08 +0800 | [diff] [blame] | 1089 | dev_dbg(&pdev->dev, "use divisor %d for %dHz sample rate\n", |
Nicolin Chen | a2388a4 | 2013-08-21 11:13:16 +0800 | [diff] [blame] | 1090 | spdif_priv->txclk_div[index], rate[index]); |
| 1091 | |
| 1092 | return 0; |
| 1093 | } |
| 1094 | |
| 1095 | static int fsl_spdif_probe(struct platform_device *pdev) |
| 1096 | { |
| 1097 | struct device_node *np = pdev->dev.of_node; |
| 1098 | struct fsl_spdif_priv *spdif_priv; |
| 1099 | struct spdif_mixer_control *ctrl; |
| 1100 | struct resource *res; |
| 1101 | void __iomem *regs; |
| 1102 | int irq, ret, i; |
| 1103 | |
| 1104 | if (!np) |
| 1105 | return -ENODEV; |
| 1106 | |
| 1107 | spdif_priv = devm_kzalloc(&pdev->dev, |
| 1108 | sizeof(struct fsl_spdif_priv) + strlen(np->name) + 1, |
| 1109 | GFP_KERNEL); |
| 1110 | if (!spdif_priv) |
| 1111 | return -ENOMEM; |
| 1112 | |
| 1113 | strcpy(spdif_priv->name, np->name); |
| 1114 | |
| 1115 | spdif_priv->pdev = pdev; |
| 1116 | |
| 1117 | /* Initialize this copy of the CPU DAI driver structure */ |
| 1118 | memcpy(&spdif_priv->cpu_dai_drv, &fsl_spdif_dai, sizeof(fsl_spdif_dai)); |
| 1119 | spdif_priv->cpu_dai_drv.name = spdif_priv->name; |
| 1120 | |
Xiubo Li | 86f28d7 | 2014-02-11 15:42:48 +0800 | [diff] [blame] | 1121 | if (of_property_read_bool(np, "big-endian")) |
| 1122 | fsl_spdif_regmap_config.val_format_endian = REGMAP_ENDIAN_BIG; |
| 1123 | |
Nicolin Chen | a2388a4 | 2013-08-21 11:13:16 +0800 | [diff] [blame] | 1124 | /* Get the addresses and IRQ */ |
| 1125 | res = platform_get_resource(pdev, IORESOURCE_MEM, 0); |
Nicolin Chen | a2388a4 | 2013-08-21 11:13:16 +0800 | [diff] [blame] | 1126 | regs = devm_ioremap_resource(&pdev->dev, res); |
Wei Yongjun | bfd7d1a | 2013-08-29 08:00:05 +0800 | [diff] [blame] | 1127 | if (IS_ERR(regs)) |
Nicolin Chen | a2388a4 | 2013-08-21 11:13:16 +0800 | [diff] [blame] | 1128 | return PTR_ERR(regs); |
Nicolin Chen | a2388a4 | 2013-08-21 11:13:16 +0800 | [diff] [blame] | 1129 | |
| 1130 | spdif_priv->regmap = devm_regmap_init_mmio_clk(&pdev->dev, |
| 1131 | "core", regs, &fsl_spdif_regmap_config); |
| 1132 | if (IS_ERR(spdif_priv->regmap)) { |
| 1133 | dev_err(&pdev->dev, "regmap init failed\n"); |
| 1134 | return PTR_ERR(spdif_priv->regmap); |
| 1135 | } |
| 1136 | |
| 1137 | irq = platform_get_irq(pdev, 0); |
| 1138 | if (irq < 0) { |
| 1139 | dev_err(&pdev->dev, "no irq for node %s\n", np->full_name); |
| 1140 | return irq; |
| 1141 | } |
| 1142 | |
| 1143 | ret = devm_request_irq(&pdev->dev, irq, spdif_isr, 0, |
| 1144 | spdif_priv->name, spdif_priv); |
| 1145 | if (ret) { |
| 1146 | dev_err(&pdev->dev, "could not claim irq %u\n", irq); |
| 1147 | return ret; |
| 1148 | } |
| 1149 | |
Nicolin Chen | 08f7336 | 2014-04-24 18:52:24 +0800 | [diff] [blame^] | 1150 | /* Get core clock for data register access via DMA */ |
| 1151 | spdif_priv->coreclk = devm_clk_get(&pdev->dev, "core"); |
| 1152 | if (IS_ERR(spdif_priv->coreclk)) { |
| 1153 | dev_err(&pdev->dev, "no core clock in devicetree\n"); |
| 1154 | return PTR_ERR(spdif_priv->coreclk); |
| 1155 | } |
| 1156 | |
Nicolin Chen | a2388a4 | 2013-08-21 11:13:16 +0800 | [diff] [blame] | 1157 | /* Select clock source for rx/tx clock */ |
| 1158 | spdif_priv->rxclk = devm_clk_get(&pdev->dev, "rxtx1"); |
| 1159 | if (IS_ERR(spdif_priv->rxclk)) { |
| 1160 | dev_err(&pdev->dev, "no rxtx1 clock in devicetree\n"); |
| 1161 | return PTR_ERR(spdif_priv->rxclk); |
| 1162 | } |
| 1163 | spdif_priv->rxclk_src = DEFAULT_RXCLK_SRC; |
| 1164 | |
| 1165 | for (i = 0; i < SPDIF_TXRATE_MAX; i++) { |
| 1166 | ret = fsl_spdif_probe_txclk(spdif_priv, i); |
| 1167 | if (ret) |
| 1168 | return ret; |
| 1169 | } |
| 1170 | |
| 1171 | /* Initial spinlock for control data */ |
| 1172 | ctrl = &spdif_priv->fsl_spdif_control; |
| 1173 | spin_lock_init(&ctrl->ctl_lock); |
| 1174 | |
| 1175 | /* Init tx channel status default value */ |
| 1176 | ctrl->ch_status[0] = |
| 1177 | IEC958_AES0_CON_NOT_COPYRIGHT | IEC958_AES0_CON_EMPHASIS_5015; |
| 1178 | ctrl->ch_status[1] = IEC958_AES1_CON_DIGDIGCONV_ID; |
| 1179 | ctrl->ch_status[2] = 0x00; |
| 1180 | ctrl->ch_status[3] = |
| 1181 | IEC958_AES3_CON_FS_44100 | IEC958_AES3_CON_CLOCK_1000PPM; |
| 1182 | |
| 1183 | spdif_priv->dpll_locked = false; |
| 1184 | |
| 1185 | spdif_priv->dma_params_tx.maxburst = FSL_SPDIF_TXFIFO_WML; |
| 1186 | spdif_priv->dma_params_rx.maxburst = FSL_SPDIF_RXFIFO_WML; |
| 1187 | spdif_priv->dma_params_tx.addr = res->start + REG_SPDIF_STL; |
| 1188 | spdif_priv->dma_params_rx.addr = res->start + REG_SPDIF_SRL; |
| 1189 | |
| 1190 | /* Register with ASoC */ |
| 1191 | dev_set_drvdata(&pdev->dev, spdif_priv); |
| 1192 | |
Sachin Kamat | 256218a | 2013-09-17 10:13:49 +0530 | [diff] [blame] | 1193 | ret = devm_snd_soc_register_component(&pdev->dev, &fsl_spdif_component, |
| 1194 | &spdif_priv->cpu_dai_drv, 1); |
Nicolin Chen | a2388a4 | 2013-08-21 11:13:16 +0800 | [diff] [blame] | 1195 | if (ret) { |
| 1196 | dev_err(&pdev->dev, "failed to register DAI: %d\n", ret); |
Fabio Estevam | 5af407c | 2013-08-23 18:14:45 -0300 | [diff] [blame] | 1197 | return ret; |
Nicolin Chen | a2388a4 | 2013-08-21 11:13:16 +0800 | [diff] [blame] | 1198 | } |
| 1199 | |
| 1200 | ret = imx_pcm_dma_init(pdev); |
Sachin Kamat | 256218a | 2013-09-17 10:13:49 +0530 | [diff] [blame] | 1201 | if (ret) |
Nicolin Chen | a2388a4 | 2013-08-21 11:13:16 +0800 | [diff] [blame] | 1202 | dev_err(&pdev->dev, "imx_pcm_dma_init failed: %d\n", ret); |
Nicolin Chen | a2388a4 | 2013-08-21 11:13:16 +0800 | [diff] [blame] | 1203 | |
| 1204 | return ret; |
| 1205 | } |
| 1206 | |
Nicolin Chen | a2388a4 | 2013-08-21 11:13:16 +0800 | [diff] [blame] | 1207 | static const struct of_device_id fsl_spdif_dt_ids[] = { |
| 1208 | { .compatible = "fsl,imx35-spdif", }, |
| 1209 | {} |
| 1210 | }; |
| 1211 | MODULE_DEVICE_TABLE(of, fsl_spdif_dt_ids); |
| 1212 | |
| 1213 | static struct platform_driver fsl_spdif_driver = { |
| 1214 | .driver = { |
| 1215 | .name = "fsl-spdif-dai", |
| 1216 | .owner = THIS_MODULE, |
| 1217 | .of_match_table = fsl_spdif_dt_ids, |
| 1218 | }, |
| 1219 | .probe = fsl_spdif_probe, |
Nicolin Chen | a2388a4 | 2013-08-21 11:13:16 +0800 | [diff] [blame] | 1220 | }; |
| 1221 | |
| 1222 | module_platform_driver(fsl_spdif_driver); |
| 1223 | |
| 1224 | MODULE_AUTHOR("Freescale Semiconductor, Inc."); |
| 1225 | MODULE_DESCRIPTION("Freescale S/PDIF CPU DAI Driver"); |
| 1226 | MODULE_LICENSE("GPL v2"); |
| 1227 | MODULE_ALIAS("platform:fsl-spdif-dai"); |