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