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