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