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