1 // SPDX-License-Identifier: GPL-2.0-or-later 2 /* 3 * ALSA SoC Audio Layer - Rockchip SAI Controller driver 4 * 5 * Copyright (c) 2022 Rockchip Electronics Co. Ltd. 6 * Copyright (c) 2025 Collabora Ltd. 7 */ 8 9 #include <linux/module.h> 10 #include <linux/mfd/syscon.h> 11 #include <linux/delay.h> 12 #include <linux/of_device.h> 13 #include <linux/clk.h> 14 #include <linux/pm_runtime.h> 15 #include <linux/regmap.h> 16 #include <linux/reset.h> 17 #include <linux/spinlock.h> 18 #include <sound/pcm_params.h> 19 #include <sound/dmaengine_pcm.h> 20 #include <sound/tlv.h> 21 22 #include "rockchip_sai.h" 23 24 #define DRV_NAME "rockchip-sai" 25 26 #define CLK_SHIFT_RATE_HZ_MAX 5 27 #define FW_RATIO_MAX 8 28 #define FW_RATIO_MIN 1 29 #define MAXBURST_PER_FIFO 8 30 31 #define TIMEOUT_US 1000 32 #define WAIT_TIME_MS_MAX 10000 33 34 #define MAX_LANES 4 35 36 enum fpw_mode { 37 FPW_ONE_BCLK_WIDTH, 38 FPW_ONE_SLOT_WIDTH, 39 FPW_HALF_FRAME_WIDTH, 40 }; 41 42 struct rk_sai_dev { 43 struct device *dev; 44 struct clk *hclk; 45 struct clk *mclk; 46 struct regmap *regmap; 47 struct reset_control *rst_h; 48 struct reset_control *rst_m; 49 struct snd_dmaengine_dai_dma_data capture_dma_data; 50 struct snd_dmaengine_dai_dma_data playback_dma_data; 51 struct snd_pcm_substream *substreams[SNDRV_PCM_STREAM_LAST + 1]; 52 unsigned int mclk_rate; 53 unsigned int wait_time[SNDRV_PCM_STREAM_LAST + 1]; 54 unsigned int tx_lanes; 55 unsigned int rx_lanes; 56 unsigned int sdi[MAX_LANES]; 57 unsigned int sdo[MAX_LANES]; 58 unsigned int version; 59 enum fpw_mode fpw; 60 int fw_ratio; 61 bool has_capture; 62 bool has_playback; 63 bool is_master_mode; 64 bool is_tdm; 65 bool initialized; 66 /* protects register writes that depend on the state of XFER[1:0] */ 67 spinlock_t xfer_lock; 68 }; 69 70 static bool rockchip_sai_stream_valid(struct snd_pcm_substream *substream, 71 struct snd_soc_dai *dai) 72 { 73 struct rk_sai_dev *sai = snd_soc_dai_get_drvdata(dai); 74 75 if (!substream) 76 return false; 77 78 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK && 79 sai->has_playback) 80 return true; 81 82 if (substream->stream == SNDRV_PCM_STREAM_CAPTURE && 83 sai->has_capture) 84 return true; 85 86 return false; 87 } 88 89 static int rockchip_sai_fsync_lost_detect(struct rk_sai_dev *sai, bool en) 90 { 91 unsigned int fw, cnt; 92 93 if (sai->is_master_mode || sai->version < SAI_VER_2311) 94 return 0; 95 96 regmap_read(sai->regmap, SAI_FSCR, &fw); 97 cnt = SAI_FSCR_FW_V(fw) << 1; /* two fsync lost */ 98 99 regmap_update_bits(sai->regmap, SAI_INTCR, 100 SAI_INTCR_FSLOSTC, SAI_INTCR_FSLOSTC); 101 regmap_update_bits(sai->regmap, SAI_INTCR, 102 SAI_INTCR_FSLOST_MASK, 103 SAI_INTCR_FSLOST(en)); 104 /* 105 * The `cnt` is the number of SCLK cycles of the CRU's SCLK signal that 106 * should be used as timeout. Consequently, in slave mode, this value 107 * is only correct if the CRU SCLK is equal to the external SCLK. 108 */ 109 regmap_update_bits(sai->regmap, SAI_FS_TIMEOUT, 110 SAI_FS_TIMEOUT_VAL_MASK | SAI_FS_TIMEOUT_EN_MASK, 111 SAI_FS_TIMEOUT_VAL(cnt) | SAI_FS_TIMEOUT_EN(en)); 112 113 return 0; 114 } 115 116 static int rockchip_sai_fsync_err_detect(struct rk_sai_dev *sai, 117 bool en) 118 { 119 if (sai->is_master_mode || sai->version < SAI_VER_2311) 120 return 0; 121 122 regmap_update_bits(sai->regmap, SAI_INTCR, 123 SAI_INTCR_FSERRC, SAI_INTCR_FSERRC); 124 regmap_update_bits(sai->regmap, SAI_INTCR, 125 SAI_INTCR_FSERR_MASK, 126 SAI_INTCR_FSERR(en)); 127 128 return 0; 129 } 130 131 static int rockchip_sai_poll_clk_idle(struct rk_sai_dev *sai) 132 { 133 unsigned int reg, idle, val; 134 int ret; 135 136 if (sai->version >= SAI_VER_2307) { 137 reg = SAI_STATUS; 138 idle = SAI_STATUS_FS_IDLE; 139 idle = sai->version >= SAI_VER_2311 ? idle >> 1 : idle; 140 } else { 141 reg = SAI_XFER; 142 idle = SAI_XFER_FS_IDLE; 143 } 144 145 ret = regmap_read_poll_timeout_atomic(sai->regmap, reg, val, 146 (val & idle), 10, TIMEOUT_US); 147 if (ret < 0) 148 dev_warn(sai->dev, "Failed to idle FS\n"); 149 150 return ret; 151 } 152 153 static int rockchip_sai_poll_stream_idle(struct rk_sai_dev *sai, bool playback, bool capture) 154 { 155 unsigned int reg, val; 156 unsigned int idle = 0; 157 int ret; 158 159 if (sai->version >= SAI_VER_2307) { 160 reg = SAI_STATUS; 161 if (playback) 162 idle |= SAI_STATUS_TX_IDLE; 163 if (capture) 164 idle |= SAI_STATUS_RX_IDLE; 165 idle = sai->version >= SAI_VER_2311 ? idle >> 1 : idle; 166 } else { 167 reg = SAI_XFER; 168 if (playback) 169 idle |= SAI_XFER_TX_IDLE; 170 if (capture) 171 idle |= SAI_XFER_RX_IDLE; 172 } 173 174 ret = regmap_read_poll_timeout_atomic(sai->regmap, reg, val, 175 (val & idle), 10, TIMEOUT_US); 176 if (ret < 0) 177 dev_warn(sai->dev, "Failed to idle stream\n"); 178 179 return ret; 180 } 181 182 /** 183 * rockchip_sai_xfer_clk_stop_and_wait() - stop the xfer clock and wait for it to be idle 184 * @sai: pointer to the driver instance's rk_sai_dev struct 185 * @to_restore: pointer to store the CLK/FSS register values in as they were 186 * found before they were cleared, or NULL. 187 * 188 * Clear the XFER_CLK and XFER_FSS registers if needed, then busy-waits for the 189 * XFER clocks to be idle. Before clearing the bits, it stores the state of the 190 * registers as it encountered them in to_restore if it isn't NULL. 191 * 192 * Context: Any context. Expects sai->xfer_lock to be held by caller. 193 */ 194 static void rockchip_sai_xfer_clk_stop_and_wait(struct rk_sai_dev *sai, unsigned int *to_restore) 195 { 196 unsigned int mask = SAI_XFER_CLK_MASK | SAI_XFER_FSS_MASK; 197 unsigned int disable = SAI_XFER_CLK_DIS | SAI_XFER_FSS_DIS; 198 unsigned int val; 199 200 assert_spin_locked(&sai->xfer_lock); 201 202 regmap_read(sai->regmap, SAI_XFER, &val); 203 if ((val & mask) == disable) 204 goto wait_for_idle; 205 206 if (sai->is_master_mode) 207 regmap_update_bits(sai->regmap, SAI_XFER, mask, disable); 208 209 wait_for_idle: 210 rockchip_sai_poll_clk_idle(sai); 211 212 if (to_restore) 213 *to_restore = val; 214 } 215 216 static int rockchip_sai_runtime_suspend(struct device *dev) 217 { 218 struct rk_sai_dev *sai = dev_get_drvdata(dev); 219 unsigned long flags; 220 221 rockchip_sai_fsync_lost_detect(sai, 0); 222 rockchip_sai_fsync_err_detect(sai, 0); 223 224 spin_lock_irqsave(&sai->xfer_lock, flags); 225 rockchip_sai_xfer_clk_stop_and_wait(sai, NULL); 226 spin_unlock_irqrestore(&sai->xfer_lock, flags); 227 228 regcache_cache_only(sai->regmap, true); 229 /* 230 * After FS is idle, we should wait at least 2 BCLK cycles to make sure 231 * the CLK gate operation has completed, and only then disable mclk. 232 * 233 * Otherwise, the BCLK is still ungated, and once the mclk is enabled, 234 * there is a risk that a few BCLK cycles leak. This is true especially 235 * at low speeds, such as with a samplerate of 8k. 236 * 237 * Ideally we'd adjust the delay based on the samplerate, but it's such 238 * a tiny value that we can just delay for the maximum clock period 239 * for the sake of simplicity. 240 * 241 * The maximum BCLK period is 31us @ 8K-8Bit (64kHz BCLK). We wait for 242 * 40us to give ourselves a safety margin in case udelay falls short. 243 */ 244 udelay(40); 245 clk_disable_unprepare(sai->mclk); 246 clk_disable_unprepare(sai->hclk); 247 248 return 0; 249 } 250 251 static int rockchip_sai_runtime_resume(struct device *dev) 252 { 253 struct rk_sai_dev *sai = dev_get_drvdata(dev); 254 int ret; 255 256 ret = clk_prepare_enable(sai->hclk); 257 if (ret) 258 goto err_hclk; 259 260 ret = clk_prepare_enable(sai->mclk); 261 if (ret) 262 goto err_mclk; 263 264 regcache_cache_only(sai->regmap, false); 265 regcache_mark_dirty(sai->regmap); 266 ret = regcache_sync(sai->regmap); 267 if (ret) 268 goto err_regmap; 269 270 return 0; 271 272 err_regmap: 273 clk_disable_unprepare(sai->mclk); 274 err_mclk: 275 clk_disable_unprepare(sai->hclk); 276 err_hclk: 277 return ret; 278 } 279 280 static void rockchip_sai_fifo_xrun_detect(struct rk_sai_dev *sai, 281 int stream, bool en) 282 { 283 if (stream == SNDRV_PCM_STREAM_PLAYBACK) { 284 /* clear irq status which was asserted before TXUIE enabled */ 285 regmap_update_bits(sai->regmap, SAI_INTCR, 286 SAI_INTCR_TXUIC, SAI_INTCR_TXUIC); 287 regmap_update_bits(sai->regmap, SAI_INTCR, 288 SAI_INTCR_TXUIE_MASK, 289 SAI_INTCR_TXUIE(en)); 290 } else { 291 /* clear irq status which was asserted before RXOIE enabled */ 292 regmap_update_bits(sai->regmap, SAI_INTCR, 293 SAI_INTCR_RXOIC, SAI_INTCR_RXOIC); 294 regmap_update_bits(sai->regmap, SAI_INTCR, 295 SAI_INTCR_RXOIE_MASK, 296 SAI_INTCR_RXOIE(en)); 297 } 298 } 299 300 static void rockchip_sai_dma_ctrl(struct rk_sai_dev *sai, 301 int stream, bool en) 302 { 303 if (!en) 304 rockchip_sai_fifo_xrun_detect(sai, stream, 0); 305 306 if (stream == SNDRV_PCM_STREAM_PLAYBACK) { 307 regmap_update_bits(sai->regmap, SAI_DMACR, 308 SAI_DMACR_TDE_MASK, 309 SAI_DMACR_TDE(en)); 310 } else { 311 regmap_update_bits(sai->regmap, SAI_DMACR, 312 SAI_DMACR_RDE_MASK, 313 SAI_DMACR_RDE(en)); 314 } 315 316 if (en) 317 rockchip_sai_fifo_xrun_detect(sai, stream, 1); 318 } 319 320 static void rockchip_sai_reset(struct rk_sai_dev *sai) 321 { 322 /* 323 * It is advised to reset the hclk domain before resetting the mclk 324 * domain, especially in slave mode without a clock input. 325 * 326 * To deal with the aforementioned case of slave mode without a clock 327 * input, we work around a potential issue by resetting the whole 328 * controller, bringing it back into master mode, and then recovering 329 * the controller configuration in the regmap. 330 */ 331 reset_control_assert(sai->rst_h); 332 udelay(10); 333 reset_control_deassert(sai->rst_h); 334 udelay(10); 335 reset_control_assert(sai->rst_m); 336 udelay(10); 337 reset_control_deassert(sai->rst_m); 338 udelay(10); 339 340 /* recover regmap config */ 341 regcache_mark_dirty(sai->regmap); 342 regcache_sync(sai->regmap); 343 } 344 345 static int rockchip_sai_clear(struct rk_sai_dev *sai, unsigned int clr) 346 { 347 unsigned int val = 0; 348 int ret = 0; 349 350 regmap_update_bits(sai->regmap, SAI_CLR, clr, clr); 351 ret = regmap_read_poll_timeout_atomic(sai->regmap, SAI_CLR, val, 352 !(val & clr), 10, TIMEOUT_US); 353 if (ret < 0) { 354 dev_warn(sai->dev, "Failed to clear %u\n", clr); 355 rockchip_sai_reset(sai); 356 } 357 358 return ret; 359 } 360 361 static void rockchip_sai_xfer_start(struct rk_sai_dev *sai, int stream) 362 { 363 unsigned int msk, val; 364 365 if (stream == SNDRV_PCM_STREAM_PLAYBACK) { 366 msk = SAI_XFER_TXS_MASK; 367 val = SAI_XFER_TXS_EN; 368 369 } else { 370 msk = SAI_XFER_RXS_MASK; 371 val = SAI_XFER_RXS_EN; 372 } 373 374 regmap_update_bits(sai->regmap, SAI_XFER, msk, val); 375 } 376 377 static void rockchip_sai_xfer_stop(struct rk_sai_dev *sai, int stream) 378 { 379 unsigned int msk = 0, val = 0, clr = 0; 380 bool capture = stream == SNDRV_PCM_STREAM_CAPTURE || stream < 0; 381 bool playback = stream == SNDRV_PCM_STREAM_PLAYBACK || stream < 0; 382 /* could be <= 0 but we don't want to depend on enum values */ 383 384 if (playback) { 385 msk |= SAI_XFER_TXS_MASK; 386 val |= SAI_XFER_TXS_DIS; 387 clr |= SAI_CLR_TXC; 388 } 389 if (capture) { 390 msk |= SAI_XFER_RXS_MASK; 391 val |= SAI_XFER_RXS_DIS; 392 clr |= SAI_CLR_RXC; 393 } 394 395 regmap_update_bits(sai->regmap, SAI_XFER, msk, val); 396 rockchip_sai_poll_stream_idle(sai, playback, capture); 397 398 rockchip_sai_clear(sai, clr); 399 } 400 401 static void rockchip_sai_start(struct rk_sai_dev *sai, int stream) 402 { 403 rockchip_sai_dma_ctrl(sai, stream, 1); 404 rockchip_sai_xfer_start(sai, stream); 405 } 406 407 static void rockchip_sai_stop(struct rk_sai_dev *sai, int stream) 408 { 409 rockchip_sai_dma_ctrl(sai, stream, 0); 410 rockchip_sai_xfer_stop(sai, stream); 411 } 412 413 static void rockchip_sai_fmt_create(struct rk_sai_dev *sai, unsigned int fmt) 414 { 415 unsigned int xcr_mask = 0, xcr_val = 0, xsft_mask = 0, xsft_val = 0; 416 unsigned int fscr_mask = 0, fscr_val = 0; 417 418 assert_spin_locked(&sai->xfer_lock); 419 420 switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) { 421 case SND_SOC_DAIFMT_RIGHT_J: 422 xcr_mask = SAI_XCR_VDJ_MASK | SAI_XCR_EDGE_SHIFT_MASK; 423 xcr_val = SAI_XCR_VDJ_R | SAI_XCR_EDGE_SHIFT_0; 424 xsft_mask = SAI_XSHIFT_RIGHT_MASK; 425 xsft_val = SAI_XSHIFT_RIGHT(0); 426 fscr_mask = SAI_FSCR_EDGE_MASK; 427 fscr_val = SAI_FSCR_EDGE_DUAL; 428 sai->fpw = FPW_HALF_FRAME_WIDTH; 429 break; 430 case SND_SOC_DAIFMT_LEFT_J: 431 xcr_mask = SAI_XCR_VDJ_MASK | SAI_XCR_EDGE_SHIFT_MASK; 432 xcr_val = SAI_XCR_VDJ_L | SAI_XCR_EDGE_SHIFT_0; 433 xsft_mask = SAI_XSHIFT_RIGHT_MASK; 434 xsft_val = SAI_XSHIFT_RIGHT(0); 435 fscr_mask = SAI_FSCR_EDGE_MASK; 436 fscr_val = SAI_FSCR_EDGE_DUAL; 437 sai->fpw = FPW_HALF_FRAME_WIDTH; 438 break; 439 case SND_SOC_DAIFMT_I2S: 440 xcr_mask = SAI_XCR_VDJ_MASK | SAI_XCR_EDGE_SHIFT_MASK; 441 xcr_val = SAI_XCR_VDJ_L | SAI_XCR_EDGE_SHIFT_1; 442 xsft_mask = SAI_XSHIFT_RIGHT_MASK; 443 if (sai->is_tdm) 444 xsft_val = SAI_XSHIFT_RIGHT(1); 445 else 446 xsft_val = SAI_XSHIFT_RIGHT(2); 447 fscr_mask = SAI_FSCR_EDGE_MASK; 448 fscr_val = SAI_FSCR_EDGE_DUAL; 449 sai->fpw = FPW_HALF_FRAME_WIDTH; 450 break; 451 case SND_SOC_DAIFMT_DSP_A: 452 xcr_mask = SAI_XCR_VDJ_MASK | SAI_XCR_EDGE_SHIFT_MASK; 453 xcr_val = SAI_XCR_VDJ_L | SAI_XCR_EDGE_SHIFT_0; 454 xsft_mask = SAI_XSHIFT_RIGHT_MASK; 455 xsft_val = SAI_XSHIFT_RIGHT(2); 456 fscr_mask = SAI_FSCR_EDGE_MASK; 457 fscr_val = SAI_FSCR_EDGE_RISING; 458 sai->fpw = FPW_ONE_BCLK_WIDTH; 459 break; 460 case SND_SOC_DAIFMT_DSP_B: 461 xcr_mask = SAI_XCR_VDJ_MASK | SAI_XCR_EDGE_SHIFT_MASK; 462 xcr_val = SAI_XCR_VDJ_L | SAI_XCR_EDGE_SHIFT_0; 463 xsft_mask = SAI_XSHIFT_RIGHT_MASK; 464 xsft_val = SAI_XSHIFT_RIGHT(0); 465 fscr_mask = SAI_FSCR_EDGE_MASK; 466 fscr_val = SAI_FSCR_EDGE_RISING; 467 sai->fpw = FPW_ONE_BCLK_WIDTH; 468 break; 469 default: 470 dev_err(sai->dev, "Unsupported fmt %u\n", fmt); 471 break; 472 } 473 474 regmap_update_bits(sai->regmap, SAI_TXCR, xcr_mask, xcr_val); 475 regmap_update_bits(sai->regmap, SAI_RXCR, xcr_mask, xcr_val); 476 regmap_update_bits(sai->regmap, SAI_TX_SHIFT, xsft_mask, xsft_val); 477 regmap_update_bits(sai->regmap, SAI_RX_SHIFT, xsft_mask, xsft_val); 478 regmap_update_bits(sai->regmap, SAI_FSCR, fscr_mask, fscr_val); 479 } 480 481 static int rockchip_sai_set_fmt(struct snd_soc_dai *dai, unsigned int fmt) 482 { 483 struct rk_sai_dev *sai = snd_soc_dai_get_drvdata(dai); 484 unsigned int mask = 0, val = 0; 485 unsigned int clk_gates; 486 unsigned long flags; 487 int ret = 0; 488 489 pm_runtime_get_sync(dai->dev); 490 491 mask = SAI_CKR_MSS_MASK; 492 switch (fmt & SND_SOC_DAIFMT_CLOCK_PROVIDER_MASK) { 493 case SND_SOC_DAIFMT_BP_FP: 494 val = SAI_CKR_MSS_MASTER; 495 sai->is_master_mode = true; 496 break; 497 case SND_SOC_DAIFMT_BC_FC: 498 val = SAI_CKR_MSS_SLAVE; 499 sai->is_master_mode = false; 500 break; 501 default: 502 ret = -EINVAL; 503 goto err_pm_put; 504 } 505 506 spin_lock_irqsave(&sai->xfer_lock, flags); 507 rockchip_sai_xfer_clk_stop_and_wait(sai, &clk_gates); 508 if (sai->initialized) { 509 if (sai->has_capture && sai->has_playback) 510 rockchip_sai_xfer_stop(sai, -1); 511 else if (sai->has_capture) 512 rockchip_sai_xfer_stop(sai, SNDRV_PCM_STREAM_CAPTURE); 513 else 514 rockchip_sai_xfer_stop(sai, SNDRV_PCM_STREAM_PLAYBACK); 515 } else { 516 rockchip_sai_clear(sai, 0); 517 sai->initialized = true; 518 } 519 520 regmap_update_bits(sai->regmap, SAI_CKR, mask, val); 521 522 mask = SAI_CKR_CKP_MASK | SAI_CKR_FSP_MASK; 523 switch (fmt & SND_SOC_DAIFMT_INV_MASK) { 524 case SND_SOC_DAIFMT_NB_NF: 525 val = SAI_CKR_CKP_NORMAL | SAI_CKR_FSP_NORMAL; 526 break; 527 case SND_SOC_DAIFMT_NB_IF: 528 val = SAI_CKR_CKP_NORMAL | SAI_CKR_FSP_INVERTED; 529 break; 530 case SND_SOC_DAIFMT_IB_NF: 531 val = SAI_CKR_CKP_INVERTED | SAI_CKR_FSP_NORMAL; 532 break; 533 case SND_SOC_DAIFMT_IB_IF: 534 val = SAI_CKR_CKP_INVERTED | SAI_CKR_FSP_INVERTED; 535 break; 536 default: 537 ret = -EINVAL; 538 goto err_xfer_unlock; 539 } 540 541 regmap_update_bits(sai->regmap, SAI_CKR, mask, val); 542 543 rockchip_sai_fmt_create(sai, fmt); 544 545 err_xfer_unlock: 546 if (clk_gates) 547 regmap_update_bits(sai->regmap, SAI_XFER, 548 SAI_XFER_CLK_MASK | SAI_XFER_FSS_MASK, 549 clk_gates); 550 spin_unlock_irqrestore(&sai->xfer_lock, flags); 551 err_pm_put: 552 pm_runtime_put(dai->dev); 553 554 return ret; 555 } 556 557 static int rockchip_sai_hw_params(struct snd_pcm_substream *substream, 558 struct snd_pcm_hw_params *params, 559 struct snd_soc_dai *dai) 560 { 561 struct rk_sai_dev *sai = snd_soc_dai_get_drvdata(dai); 562 struct snd_dmaengine_dai_dma_data *dma_data; 563 unsigned int mclk_rate, mclk_req_rate, bclk_rate, div_bclk; 564 unsigned int ch_per_lane, slot_width; 565 unsigned int val, fscr, reg; 566 unsigned int lanes, req_lanes; 567 unsigned long flags; 568 int ret = 0; 569 570 if (!rockchip_sai_stream_valid(substream, dai)) 571 return 0; 572 573 dma_data = snd_soc_dai_get_dma_data(dai, substream); 574 dma_data->maxburst = MAXBURST_PER_FIFO * params_channels(params) / 2; 575 576 pm_runtime_get_sync(sai->dev); 577 578 regmap_read(sai->regmap, SAI_DMACR, &val); 579 580 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) { 581 reg = SAI_TXCR; 582 lanes = sai->tx_lanes; 583 } else { 584 reg = SAI_RXCR; 585 lanes = sai->rx_lanes; 586 } 587 588 if (!sai->is_tdm) { 589 req_lanes = DIV_ROUND_UP(params_channels(params), 2); 590 if (lanes < req_lanes) { 591 dev_err(sai->dev, "not enough lanes (%d) for requested number of %s channels (%d)\n", 592 lanes, reg == SAI_TXCR ? "playback" : "capture", 593 params_channels(params)); 594 ret = -EINVAL; 595 goto err_pm_put; 596 } else { 597 lanes = req_lanes; 598 } 599 } 600 601 dev_dbg(sai->dev, "using %d lanes totalling %d%s channels for %s\n", 602 lanes, params_channels(params), sai->is_tdm ? " (TDM)" : "", 603 reg == SAI_TXCR ? "playback" : "capture"); 604 605 switch (params_format(params)) { 606 case SNDRV_PCM_FORMAT_S8: 607 case SNDRV_PCM_FORMAT_U8: 608 val = SAI_XCR_VDW(8); 609 break; 610 case SNDRV_PCM_FORMAT_S16_LE: 611 val = SAI_XCR_VDW(16); 612 break; 613 case SNDRV_PCM_FORMAT_S24_LE: 614 val = SAI_XCR_VDW(24); 615 break; 616 case SNDRV_PCM_FORMAT_S32_LE: 617 case SNDRV_PCM_FORMAT_IEC958_SUBFRAME_LE: 618 val = SAI_XCR_VDW(32); 619 break; 620 default: 621 ret = -EINVAL; 622 goto err_pm_put; 623 } 624 625 val |= SAI_XCR_CSR(lanes); 626 627 spin_lock_irqsave(&sai->xfer_lock, flags); 628 629 regmap_update_bits(sai->regmap, reg, SAI_XCR_VDW_MASK | SAI_XCR_CSR_MASK, val); 630 631 if (!sai->is_tdm) 632 regmap_update_bits(sai->regmap, reg, SAI_XCR_SBW_MASK, 633 SAI_XCR_SBW(params_physical_width(params))); 634 635 regmap_read(sai->regmap, reg, &val); 636 637 slot_width = SAI_XCR_SBW_V(val); 638 ch_per_lane = params_channels(params) / lanes; 639 640 regmap_update_bits(sai->regmap, reg, SAI_XCR_SNB_MASK, 641 SAI_XCR_SNB(ch_per_lane)); 642 643 fscr = SAI_FSCR_FW(sai->fw_ratio * slot_width * ch_per_lane); 644 645 switch (sai->fpw) { 646 case FPW_ONE_BCLK_WIDTH: 647 fscr |= SAI_FSCR_FPW(1); 648 break; 649 case FPW_ONE_SLOT_WIDTH: 650 fscr |= SAI_FSCR_FPW(slot_width); 651 break; 652 case FPW_HALF_FRAME_WIDTH: 653 fscr |= SAI_FSCR_FPW(sai->fw_ratio * slot_width * ch_per_lane / 2); 654 break; 655 default: 656 dev_err(sai->dev, "Invalid Frame Pulse Width %d\n", sai->fpw); 657 ret = -EINVAL; 658 goto err_xfer_unlock; 659 } 660 661 regmap_update_bits(sai->regmap, SAI_FSCR, 662 SAI_FSCR_FW_MASK | SAI_FSCR_FPW_MASK, fscr); 663 664 if (sai->is_master_mode) { 665 bclk_rate = sai->fw_ratio * slot_width * ch_per_lane * params_rate(params); 666 ret = clk_set_rate(sai->mclk, sai->mclk_rate); 667 if (ret) { 668 dev_err(sai->dev, "Failed to set mclk to %u: %pe\n", 669 sai->mclk_rate, ERR_PTR(ret)); 670 goto err_xfer_unlock; 671 } 672 673 mclk_rate = clk_get_rate(sai->mclk); 674 if (mclk_rate < bclk_rate) { 675 dev_err(sai->dev, "Mismatch mclk: %u, at least %u\n", 676 mclk_rate, bclk_rate); 677 ret = -EINVAL; 678 goto err_xfer_unlock; 679 } 680 681 div_bclk = DIV_ROUND_CLOSEST(mclk_rate, bclk_rate); 682 mclk_req_rate = bclk_rate * div_bclk; 683 684 if (mclk_rate < mclk_req_rate - CLK_SHIFT_RATE_HZ_MAX || 685 mclk_rate > mclk_req_rate + CLK_SHIFT_RATE_HZ_MAX) { 686 dev_err(sai->dev, "Mismatch mclk: %u, expected %u (+/- %dHz)\n", 687 mclk_rate, mclk_req_rate, CLK_SHIFT_RATE_HZ_MAX); 688 ret = -EINVAL; 689 goto err_xfer_unlock; 690 } 691 692 regmap_update_bits(sai->regmap, SAI_CKR, SAI_CKR_MDIV_MASK, 693 SAI_CKR_MDIV(div_bclk)); 694 } 695 696 err_xfer_unlock: 697 spin_unlock_irqrestore(&sai->xfer_lock, flags); 698 err_pm_put: 699 pm_runtime_put(sai->dev); 700 701 return ret; 702 } 703 704 static int rockchip_sai_prepare(struct snd_pcm_substream *substream, 705 struct snd_soc_dai *dai) 706 { 707 struct rk_sai_dev *sai = snd_soc_dai_get_drvdata(dai); 708 unsigned long flags; 709 710 if (!rockchip_sai_stream_valid(substream, dai)) 711 return 0; 712 713 if (sai->is_master_mode) { 714 /* 715 * We should wait for the first BCLK pulse to have definitely 716 * occurred after any DIV settings have potentially been 717 * changed in order to guarantee a clean clock signal once we 718 * ungate the clock. 719 * 720 * Ideally, this would be done depending on the samplerate, but 721 * for the sake of simplicity, we'll just delay for the maximum 722 * possible clock offset time, which is quite a small value. 723 * 724 * The maximum BCLK offset is 15.6us @ 8K-8Bit (64kHz BCLK). We 725 * wait for 20us in order to give us a safety margin in case 726 * udelay falls short. 727 */ 728 udelay(20); 729 spin_lock_irqsave(&sai->xfer_lock, flags); 730 regmap_update_bits(sai->regmap, SAI_XFER, 731 SAI_XFER_CLK_MASK | 732 SAI_XFER_FSS_MASK, 733 SAI_XFER_CLK_EN | 734 SAI_XFER_FSS_EN); 735 spin_unlock_irqrestore(&sai->xfer_lock, flags); 736 } 737 738 rockchip_sai_fsync_lost_detect(sai, 1); 739 rockchip_sai_fsync_err_detect(sai, 1); 740 741 return 0; 742 } 743 744 static void rockchip_sai_path_config(struct rk_sai_dev *sai, 745 int num, bool is_rx) 746 { 747 int i; 748 749 if (is_rx) 750 for (i = 0; i < num; i++) 751 regmap_update_bits(sai->regmap, SAI_PATH_SEL, 752 SAI_RX_PATH_MASK(i), 753 SAI_RX_PATH(i, sai->sdi[i])); 754 else 755 for (i = 0; i < num; i++) 756 regmap_update_bits(sai->regmap, SAI_PATH_SEL, 757 SAI_TX_PATH_MASK(i), 758 SAI_TX_PATH(i, sai->sdo[i])); 759 } 760 761 static int rockchip_sai_path_prepare(struct rk_sai_dev *sai, 762 struct device_node *np, 763 bool is_rx) 764 { 765 const char *path_prop; 766 unsigned int *data; 767 unsigned int *lanes; 768 int i, num, ret; 769 770 if (is_rx) { 771 path_prop = "rockchip,sai-rx-route"; 772 data = sai->sdi; 773 lanes = &sai->rx_lanes; 774 } else { 775 path_prop = "rockchip,sai-tx-route"; 776 data = sai->sdo; 777 lanes = &sai->tx_lanes; 778 } 779 780 num = of_count_phandle_with_args(np, path_prop, NULL); 781 if (num == -ENOENT) { 782 return 0; 783 } else if (num > MAX_LANES || num == 0) { 784 dev_err(sai->dev, "found %d entries in %s, outside of range 1 to %d\n", 785 num, path_prop, MAX_LANES); 786 return -EINVAL; 787 } else if (num < 0) { 788 dev_err(sai->dev, "error in %s property: %pe\n", path_prop, 789 ERR_PTR(num)); 790 return num; 791 } 792 793 *lanes = num; 794 ret = device_property_read_u32_array(sai->dev, path_prop, data, num); 795 if (ret < 0) { 796 dev_err(sai->dev, "failed to read property '%s': %pe\n", 797 path_prop, ERR_PTR(ret)); 798 return ret; 799 } 800 801 for (i = 0; i < num; i++) { 802 if (data[i] >= MAX_LANES) { 803 dev_err(sai->dev, "%s[%d] is %d, should be less than %d\n", 804 path_prop, i, data[i], MAX_LANES); 805 return -EINVAL; 806 } 807 } 808 809 rockchip_sai_path_config(sai, num, is_rx); 810 811 return 0; 812 } 813 814 static int rockchip_sai_parse_paths(struct rk_sai_dev *sai, 815 struct device_node *np) 816 { 817 int ret; 818 819 if (sai->has_playback) { 820 sai->tx_lanes = 1; 821 ret = rockchip_sai_path_prepare(sai, np, false); 822 if (ret < 0) { 823 dev_err(sai->dev, "Failed to prepare TX path: %pe\n", 824 ERR_PTR(ret)); 825 return ret; 826 } 827 } 828 829 if (sai->has_capture) { 830 sai->rx_lanes = 1; 831 ret = rockchip_sai_path_prepare(sai, np, true); 832 if (ret < 0) { 833 dev_err(sai->dev, "Failed to prepare RX path: %pe\n", 834 ERR_PTR(ret)); 835 return ret; 836 } 837 } 838 839 return 0; 840 } 841 842 static int rockchip_sai_trigger(struct snd_pcm_substream *substream, 843 int cmd, struct snd_soc_dai *dai) 844 { 845 struct rk_sai_dev *sai = snd_soc_dai_get_drvdata(dai); 846 int ret = 0; 847 848 if (!rockchip_sai_stream_valid(substream, dai)) 849 return 0; 850 851 switch (cmd) { 852 case SNDRV_PCM_TRIGGER_START: 853 case SNDRV_PCM_TRIGGER_RESUME: 854 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE: 855 rockchip_sai_start(sai, substream->stream); 856 break; 857 case SNDRV_PCM_TRIGGER_SUSPEND: 858 case SNDRV_PCM_TRIGGER_STOP: 859 case SNDRV_PCM_TRIGGER_PAUSE_PUSH: 860 rockchip_sai_stop(sai, substream->stream); 861 break; 862 default: 863 ret = -EINVAL; 864 break; 865 } 866 867 return ret; 868 } 869 870 871 static int rockchip_sai_dai_probe(struct snd_soc_dai *dai) 872 { 873 struct rk_sai_dev *sai = snd_soc_dai_get_drvdata(dai); 874 875 snd_soc_dai_init_dma_data(dai, 876 sai->has_playback ? &sai->playback_dma_data : NULL, 877 sai->has_capture ? &sai->capture_dma_data : NULL); 878 879 return 0; 880 } 881 882 static int rockchip_sai_startup(struct snd_pcm_substream *substream, 883 struct snd_soc_dai *dai) 884 { 885 struct rk_sai_dev *sai = snd_soc_dai_get_drvdata(dai); 886 int stream = substream->stream; 887 888 if (!rockchip_sai_stream_valid(substream, dai)) 889 return 0; 890 891 if (sai->substreams[stream]) 892 return -EBUSY; 893 894 if (sai->wait_time[stream]) 895 substream->wait_time = sai->wait_time[stream]; 896 897 sai->substreams[stream] = substream; 898 899 return 0; 900 } 901 902 static void rockchip_sai_shutdown(struct snd_pcm_substream *substream, 903 struct snd_soc_dai *dai) 904 { 905 struct rk_sai_dev *sai = snd_soc_dai_get_drvdata(dai); 906 907 if (!rockchip_sai_stream_valid(substream, dai)) 908 return; 909 910 sai->substreams[substream->stream] = NULL; 911 } 912 913 static int rockchip_sai_set_tdm_slot(struct snd_soc_dai *dai, 914 unsigned int tx_mask, unsigned int rx_mask, 915 int slots, int slot_width) 916 { 917 struct rk_sai_dev *sai = snd_soc_dai_get_drvdata(dai); 918 unsigned long flags; 919 unsigned int clk_gates; 920 int sw = slot_width; 921 922 if (!slots) { 923 /* Disabling TDM, set slot width back to 32 bits */ 924 sai->is_tdm = false; 925 sw = 32; 926 } else { 927 sai->is_tdm = true; 928 } 929 930 if (sw < 16 || sw > 32) 931 return -EINVAL; 932 933 pm_runtime_get_sync(dai->dev); 934 spin_lock_irqsave(&sai->xfer_lock, flags); 935 rockchip_sai_xfer_clk_stop_and_wait(sai, &clk_gates); 936 regmap_update_bits(sai->regmap, SAI_TXCR, SAI_XCR_SBW_MASK, 937 SAI_XCR_SBW(sw)); 938 regmap_update_bits(sai->regmap, SAI_RXCR, SAI_XCR_SBW_MASK, 939 SAI_XCR_SBW(sw)); 940 regmap_update_bits(sai->regmap, SAI_XFER, 941 SAI_XFER_CLK_MASK | SAI_XFER_FSS_MASK, 942 clk_gates); 943 spin_unlock_irqrestore(&sai->xfer_lock, flags); 944 pm_runtime_put(dai->dev); 945 946 return 0; 947 } 948 949 static int rockchip_sai_set_sysclk(struct snd_soc_dai *dai, int stream, 950 unsigned int freq, int dir) 951 { 952 struct rk_sai_dev *sai = snd_soc_dai_get_drvdata(dai); 953 954 sai->mclk_rate = freq; 955 956 return 0; 957 } 958 959 static const struct snd_soc_dai_ops rockchip_sai_dai_ops = { 960 .probe = rockchip_sai_dai_probe, 961 .startup = rockchip_sai_startup, 962 .shutdown = rockchip_sai_shutdown, 963 .hw_params = rockchip_sai_hw_params, 964 .set_fmt = rockchip_sai_set_fmt, 965 .set_sysclk = rockchip_sai_set_sysclk, 966 .prepare = rockchip_sai_prepare, 967 .trigger = rockchip_sai_trigger, 968 .set_tdm_slot = rockchip_sai_set_tdm_slot, 969 }; 970 971 static const struct snd_soc_dai_driver rockchip_sai_dai = { 972 .ops = &rockchip_sai_dai_ops, 973 .symmetric_rate = 1, 974 }; 975 976 static bool rockchip_sai_wr_reg(struct device *dev, unsigned int reg) 977 { 978 switch (reg) { 979 case SAI_TXCR: 980 case SAI_FSCR: 981 case SAI_RXCR: 982 case SAI_MONO_CR: 983 case SAI_XFER: 984 case SAI_CLR: 985 case SAI_CKR: 986 case SAI_DMACR: 987 case SAI_INTCR: 988 case SAI_TXDR: 989 case SAI_PATH_SEL: 990 case SAI_TX_SLOT_MASK0: 991 case SAI_TX_SLOT_MASK1: 992 case SAI_TX_SLOT_MASK2: 993 case SAI_TX_SLOT_MASK3: 994 case SAI_RX_SLOT_MASK0: 995 case SAI_RX_SLOT_MASK1: 996 case SAI_RX_SLOT_MASK2: 997 case SAI_RX_SLOT_MASK3: 998 case SAI_TX_SHIFT: 999 case SAI_RX_SHIFT: 1000 case SAI_FSXN: 1001 case SAI_FS_TIMEOUT: 1002 case SAI_LOOPBACK_LR: 1003 return true; 1004 default: 1005 return false; 1006 } 1007 } 1008 1009 static bool rockchip_sai_rd_reg(struct device *dev, unsigned int reg) 1010 { 1011 switch (reg) { 1012 case SAI_TXCR: 1013 case SAI_FSCR: 1014 case SAI_RXCR: 1015 case SAI_MONO_CR: 1016 case SAI_XFER: 1017 case SAI_CLR: 1018 case SAI_CKR: 1019 case SAI_TXFIFOLR: 1020 case SAI_RXFIFOLR: 1021 case SAI_DMACR: 1022 case SAI_INTCR: 1023 case SAI_INTSR: 1024 case SAI_TXDR: 1025 case SAI_RXDR: 1026 case SAI_PATH_SEL: 1027 case SAI_TX_SLOT_MASK0: 1028 case SAI_TX_SLOT_MASK1: 1029 case SAI_TX_SLOT_MASK2: 1030 case SAI_TX_SLOT_MASK3: 1031 case SAI_RX_SLOT_MASK0: 1032 case SAI_RX_SLOT_MASK1: 1033 case SAI_RX_SLOT_MASK2: 1034 case SAI_RX_SLOT_MASK3: 1035 case SAI_TX_DATA_CNT: 1036 case SAI_RX_DATA_CNT: 1037 case SAI_TX_SHIFT: 1038 case SAI_RX_SHIFT: 1039 case SAI_STATUS: 1040 case SAI_VERSION: 1041 case SAI_FSXN: 1042 case SAI_FS_TIMEOUT: 1043 case SAI_LOOPBACK_LR: 1044 return true; 1045 default: 1046 return false; 1047 } 1048 } 1049 1050 static bool rockchip_sai_volatile_reg(struct device *dev, unsigned int reg) 1051 { 1052 switch (reg) { 1053 case SAI_XFER: 1054 case SAI_INTCR: 1055 case SAI_INTSR: 1056 case SAI_CLR: 1057 case SAI_TXFIFOLR: 1058 case SAI_RXFIFOLR: 1059 case SAI_TXDR: 1060 case SAI_RXDR: 1061 case SAI_TX_DATA_CNT: 1062 case SAI_RX_DATA_CNT: 1063 case SAI_STATUS: 1064 case SAI_VERSION: 1065 return true; 1066 default: 1067 return false; 1068 } 1069 } 1070 1071 static bool rockchip_sai_precious_reg(struct device *dev, unsigned int reg) 1072 { 1073 switch (reg) { 1074 case SAI_RXDR: 1075 return true; 1076 default: 1077 return false; 1078 } 1079 } 1080 1081 static const struct reg_default rockchip_sai_reg_defaults[] = { 1082 { SAI_TXCR, 0x00000bff }, 1083 { SAI_FSCR, 0x0001f03f }, 1084 { SAI_RXCR, 0x00000bff }, 1085 { SAI_PATH_SEL, 0x0000e4e4 }, 1086 }; 1087 1088 static const struct regmap_config rockchip_sai_regmap_config = { 1089 .reg_bits = 32, 1090 .reg_stride = 4, 1091 .val_bits = 32, 1092 .max_register = SAI_LOOPBACK_LR, 1093 .reg_defaults = rockchip_sai_reg_defaults, 1094 .num_reg_defaults = ARRAY_SIZE(rockchip_sai_reg_defaults), 1095 .writeable_reg = rockchip_sai_wr_reg, 1096 .readable_reg = rockchip_sai_rd_reg, 1097 .volatile_reg = rockchip_sai_volatile_reg, 1098 .precious_reg = rockchip_sai_precious_reg, 1099 .cache_type = REGCACHE_FLAT, 1100 }; 1101 1102 static int rockchip_sai_init_dai(struct rk_sai_dev *sai, struct resource *res, 1103 struct snd_soc_dai_driver **dp) 1104 { 1105 struct device_node *node = sai->dev->of_node; 1106 struct snd_soc_dai_driver *dai; 1107 struct property *dma_names; 1108 const char *dma_name; 1109 1110 of_property_for_each_string(node, "dma-names", dma_names, dma_name) { 1111 if (!strcmp(dma_name, "tx")) 1112 sai->has_playback = true; 1113 if (!strcmp(dma_name, "rx")) 1114 sai->has_capture = true; 1115 } 1116 1117 dai = devm_kmemdup(sai->dev, &rockchip_sai_dai, 1118 sizeof(*dai), GFP_KERNEL); 1119 if (!dai) 1120 return -ENOMEM; 1121 1122 if (sai->has_playback) { 1123 dai->playback.stream_name = "Playback"; 1124 dai->playback.channels_min = 1; 1125 dai->playback.channels_max = 512; 1126 dai->playback.rates = SNDRV_PCM_RATE_8000_384000; 1127 dai->playback.formats = SNDRV_PCM_FMTBIT_S8 | 1128 SNDRV_PCM_FMTBIT_S16_LE | 1129 SNDRV_PCM_FMTBIT_S24_LE | 1130 SNDRV_PCM_FMTBIT_S32_LE | 1131 SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_LE; 1132 1133 sai->playback_dma_data.addr = res->start + SAI_TXDR; 1134 sai->playback_dma_data.addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES; 1135 sai->playback_dma_data.maxburst = MAXBURST_PER_FIFO; 1136 } 1137 1138 if (sai->has_capture) { 1139 dai->capture.stream_name = "Capture"; 1140 dai->capture.channels_min = 1; 1141 dai->capture.channels_max = 512; 1142 dai->capture.rates = SNDRV_PCM_RATE_8000_384000; 1143 dai->capture.formats = SNDRV_PCM_FMTBIT_S8 | 1144 SNDRV_PCM_FMTBIT_S16_LE | 1145 SNDRV_PCM_FMTBIT_S24_LE | 1146 SNDRV_PCM_FMTBIT_S32_LE | 1147 SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_LE; 1148 1149 sai->capture_dma_data.addr = res->start + SAI_RXDR; 1150 sai->capture_dma_data.addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES; 1151 sai->capture_dma_data.maxburst = MAXBURST_PER_FIFO; 1152 } 1153 1154 regmap_update_bits(sai->regmap, SAI_DMACR, SAI_DMACR_TDL_MASK, 1155 SAI_DMACR_TDL(16)); 1156 regmap_update_bits(sai->regmap, SAI_DMACR, SAI_DMACR_RDL_MASK, 1157 SAI_DMACR_RDL(16)); 1158 1159 if (dp) 1160 *dp = dai; 1161 1162 return 0; 1163 } 1164 1165 static const char * const mono_text[] = { "Disable", "Enable" }; 1166 1167 static DECLARE_TLV_DB_SCALE(rmss_tlv, 0, 128, 0); 1168 1169 static const char * const lplrc_text[] = { "L:MIC R:LP", "L:LP R:MIC" }; 1170 static const char * const lplr_text[] = { "Disable", "Enable" }; 1171 1172 static const char * const lpx_text[] = { 1173 "From SDO0", "From SDO1", "From SDO2", "From SDO3" }; 1174 1175 static const char * const lps_text[] = { "Disable", "Enable" }; 1176 static const char * const sync_out_text[] = { "From CRU", "From IO" }; 1177 static const char * const sync_in_text[] = { "From IO", "From Sync Port" }; 1178 1179 static const char * const rpaths_text[] = { 1180 "From SDI0", "From SDI1", "From SDI2", "From SDI3" }; 1181 1182 static const char * const tpaths_text[] = { 1183 "From PATH0", "From PATH1", "From PATH2", "From PATH3" }; 1184 1185 /* MONO_CR */ 1186 static SOC_ENUM_SINGLE_DECL(rmono_switch, SAI_MONO_CR, 1, mono_text); 1187 static SOC_ENUM_SINGLE_DECL(tmono_switch, SAI_MONO_CR, 0, mono_text); 1188 1189 /* PATH_SEL */ 1190 static SOC_ENUM_SINGLE_DECL(lp3_enum, SAI_PATH_SEL, 28, lpx_text); 1191 static SOC_ENUM_SINGLE_DECL(lp2_enum, SAI_PATH_SEL, 26, lpx_text); 1192 static SOC_ENUM_SINGLE_DECL(lp1_enum, SAI_PATH_SEL, 24, lpx_text); 1193 static SOC_ENUM_SINGLE_DECL(lp0_enum, SAI_PATH_SEL, 22, lpx_text); 1194 static SOC_ENUM_SINGLE_DECL(lp3_switch, SAI_PATH_SEL, 21, lps_text); 1195 static SOC_ENUM_SINGLE_DECL(lp2_switch, SAI_PATH_SEL, 20, lps_text); 1196 static SOC_ENUM_SINGLE_DECL(lp1_switch, SAI_PATH_SEL, 19, lps_text); 1197 static SOC_ENUM_SINGLE_DECL(lp0_switch, SAI_PATH_SEL, 18, lps_text); 1198 static SOC_ENUM_SINGLE_DECL(sync_out_switch, SAI_PATH_SEL, 17, sync_out_text); 1199 static SOC_ENUM_SINGLE_DECL(sync_in_switch, SAI_PATH_SEL, 16, sync_in_text); 1200 static SOC_ENUM_SINGLE_DECL(rpath3_enum, SAI_PATH_SEL, 14, rpaths_text); 1201 static SOC_ENUM_SINGLE_DECL(rpath2_enum, SAI_PATH_SEL, 12, rpaths_text); 1202 static SOC_ENUM_SINGLE_DECL(rpath1_enum, SAI_PATH_SEL, 10, rpaths_text); 1203 static SOC_ENUM_SINGLE_DECL(rpath0_enum, SAI_PATH_SEL, 8, rpaths_text); 1204 static SOC_ENUM_SINGLE_DECL(tpath3_enum, SAI_PATH_SEL, 6, tpaths_text); 1205 static SOC_ENUM_SINGLE_DECL(tpath2_enum, SAI_PATH_SEL, 4, tpaths_text); 1206 static SOC_ENUM_SINGLE_DECL(tpath1_enum, SAI_PATH_SEL, 2, tpaths_text); 1207 static SOC_ENUM_SINGLE_DECL(tpath0_enum, SAI_PATH_SEL, 0, tpaths_text); 1208 1209 /* LOOPBACK_LR */ 1210 static SOC_ENUM_SINGLE_DECL(lp3lrc_enum, SAI_LOOPBACK_LR, 7, lplrc_text); 1211 static SOC_ENUM_SINGLE_DECL(lp2lrc_enum, SAI_LOOPBACK_LR, 6, lplrc_text); 1212 static SOC_ENUM_SINGLE_DECL(lp1lrc_enum, SAI_LOOPBACK_LR, 5, lplrc_text); 1213 static SOC_ENUM_SINGLE_DECL(lp0lrc_enum, SAI_LOOPBACK_LR, 4, lplrc_text); 1214 static SOC_ENUM_SINGLE_DECL(lp3lr_switch, SAI_LOOPBACK_LR, 3, lplr_text); 1215 static SOC_ENUM_SINGLE_DECL(lp2lr_switch, SAI_LOOPBACK_LR, 2, lplr_text); 1216 static SOC_ENUM_SINGLE_DECL(lp1lr_switch, SAI_LOOPBACK_LR, 1, lplr_text); 1217 static SOC_ENUM_SINGLE_DECL(lp0lr_switch, SAI_LOOPBACK_LR, 0, lplr_text); 1218 1219 static int rockchip_sai_wait_time_info(struct snd_kcontrol *kcontrol, 1220 struct snd_ctl_elem_info *uinfo) 1221 { 1222 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER; 1223 uinfo->count = 1; 1224 uinfo->value.integer.min = 0; 1225 uinfo->value.integer.max = WAIT_TIME_MS_MAX; 1226 uinfo->value.integer.step = 1; 1227 1228 return 0; 1229 } 1230 1231 static int rockchip_sai_rd_wait_time_get(struct snd_kcontrol *kcontrol, 1232 struct snd_ctl_elem_value *ucontrol) 1233 { 1234 struct snd_soc_component *component = snd_kcontrol_chip(kcontrol); 1235 struct rk_sai_dev *sai = snd_soc_component_get_drvdata(component); 1236 1237 ucontrol->value.integer.value[0] = sai->wait_time[SNDRV_PCM_STREAM_CAPTURE]; 1238 1239 return 0; 1240 } 1241 1242 static int rockchip_sai_rd_wait_time_put(struct snd_kcontrol *kcontrol, 1243 struct snd_ctl_elem_value *ucontrol) 1244 { 1245 struct snd_soc_component *component = snd_kcontrol_chip(kcontrol); 1246 struct rk_sai_dev *sai = snd_soc_component_get_drvdata(component); 1247 1248 if (ucontrol->value.integer.value[0] > WAIT_TIME_MS_MAX) 1249 return -EINVAL; 1250 1251 sai->wait_time[SNDRV_PCM_STREAM_CAPTURE] = ucontrol->value.integer.value[0]; 1252 1253 return 1; 1254 } 1255 1256 static int rockchip_sai_wr_wait_time_get(struct snd_kcontrol *kcontrol, 1257 struct snd_ctl_elem_value *ucontrol) 1258 { 1259 struct snd_soc_component *component = snd_kcontrol_chip(kcontrol); 1260 struct rk_sai_dev *sai = snd_soc_component_get_drvdata(component); 1261 1262 ucontrol->value.integer.value[0] = sai->wait_time[SNDRV_PCM_STREAM_PLAYBACK]; 1263 1264 return 0; 1265 } 1266 1267 static int rockchip_sai_wr_wait_time_put(struct snd_kcontrol *kcontrol, 1268 struct snd_ctl_elem_value *ucontrol) 1269 { 1270 struct snd_soc_component *component = snd_kcontrol_chip(kcontrol); 1271 struct rk_sai_dev *sai = snd_soc_component_get_drvdata(component); 1272 1273 if (ucontrol->value.integer.value[0] > WAIT_TIME_MS_MAX) 1274 return -EINVAL; 1275 1276 sai->wait_time[SNDRV_PCM_STREAM_PLAYBACK] = ucontrol->value.integer.value[0]; 1277 1278 return 1; 1279 } 1280 1281 #define SAI_PCM_WAIT_TIME(xname, xhandler_get, xhandler_put) \ 1282 { .iface = SNDRV_CTL_ELEM_IFACE_PCM, .name = xname, \ 1283 .info = rockchip_sai_wait_time_info, \ 1284 .get = xhandler_get, .put = xhandler_put } 1285 1286 static const struct snd_kcontrol_new rockchip_sai_controls[] = { 1287 SOC_SINGLE_TLV("Receive Mono Slot Select", SAI_MONO_CR, 1288 2, 128, 0, rmss_tlv), 1289 SOC_ENUM("Receive Mono Switch", rmono_switch), 1290 SOC_ENUM("Transmit Mono Switch", tmono_switch), 1291 1292 SOC_ENUM("SDI3 Loopback I2S LR Channel Sel", lp3lrc_enum), 1293 SOC_ENUM("SDI2 Loopback I2S LR Channel Sel", lp2lrc_enum), 1294 SOC_ENUM("SDI1 Loopback I2S LR Channel Sel", lp1lrc_enum), 1295 SOC_ENUM("SDI0 Loopback I2S LR Channel Sel", lp0lrc_enum), 1296 SOC_ENUM("SDI3 Loopback I2S LR Switch", lp3lr_switch), 1297 SOC_ENUM("SDI2 Loopback I2S LR Switch", lp2lr_switch), 1298 SOC_ENUM("SDI1 Loopback I2S LR Switch", lp1lr_switch), 1299 SOC_ENUM("SDI0 Loopback I2S LR Switch", lp0lr_switch), 1300 1301 SOC_ENUM("SDI3 Loopback Src Select", lp3_enum), 1302 SOC_ENUM("SDI2 Loopback Src Select", lp2_enum), 1303 SOC_ENUM("SDI1 Loopback Src Select", lp1_enum), 1304 SOC_ENUM("SDI0 Loopback Src Select", lp0_enum), 1305 SOC_ENUM("SDI3 Loopback Switch", lp3_switch), 1306 SOC_ENUM("SDI2 Loopback Switch", lp2_switch), 1307 SOC_ENUM("SDI1 Loopback Switch", lp1_switch), 1308 SOC_ENUM("SDI0 Loopback Switch", lp0_switch), 1309 SOC_ENUM("Sync Out Switch", sync_out_switch), 1310 SOC_ENUM("Sync In Switch", sync_in_switch), 1311 SOC_ENUM("Receive PATH3 Source Select", rpath3_enum), 1312 SOC_ENUM("Receive PATH2 Source Select", rpath2_enum), 1313 SOC_ENUM("Receive PATH1 Source Select", rpath1_enum), 1314 SOC_ENUM("Receive PATH0 Source Select", rpath0_enum), 1315 SOC_ENUM("Transmit SDO3 Source Select", tpath3_enum), 1316 SOC_ENUM("Transmit SDO2 Source Select", tpath2_enum), 1317 SOC_ENUM("Transmit SDO1 Source Select", tpath1_enum), 1318 SOC_ENUM("Transmit SDO0 Source Select", tpath0_enum), 1319 1320 SAI_PCM_WAIT_TIME("PCM Read Wait Time MS", 1321 rockchip_sai_rd_wait_time_get, 1322 rockchip_sai_rd_wait_time_put), 1323 SAI_PCM_WAIT_TIME("PCM Write Wait Time MS", 1324 rockchip_sai_wr_wait_time_get, 1325 rockchip_sai_wr_wait_time_put), 1326 }; 1327 1328 static const struct snd_soc_component_driver rockchip_sai_component = { 1329 .name = DRV_NAME, 1330 .controls = rockchip_sai_controls, 1331 .num_controls = ARRAY_SIZE(rockchip_sai_controls), 1332 .legacy_dai_naming = 1, 1333 }; 1334 1335 static irqreturn_t rockchip_sai_isr(int irq, void *devid) 1336 { 1337 struct rk_sai_dev *sai = (struct rk_sai_dev *)devid; 1338 struct snd_pcm_substream *substream; 1339 u32 val; 1340 1341 regmap_read(sai->regmap, SAI_INTSR, &val); 1342 if (val & SAI_INTSR_TXUI_ACT) { 1343 dev_warn_ratelimited(sai->dev, "TX FIFO Underrun\n"); 1344 regmap_update_bits(sai->regmap, SAI_INTCR, 1345 SAI_INTCR_TXUIC, SAI_INTCR_TXUIC); 1346 regmap_update_bits(sai->regmap, SAI_INTCR, 1347 SAI_INTCR_TXUIE_MASK, 1348 SAI_INTCR_TXUIE(0)); 1349 substream = sai->substreams[SNDRV_PCM_STREAM_PLAYBACK]; 1350 if (substream) 1351 snd_pcm_stop_xrun(substream); 1352 } 1353 1354 if (val & SAI_INTSR_RXOI_ACT) { 1355 dev_warn_ratelimited(sai->dev, "RX FIFO Overrun\n"); 1356 regmap_update_bits(sai->regmap, SAI_INTCR, 1357 SAI_INTCR_RXOIC, SAI_INTCR_RXOIC); 1358 regmap_update_bits(sai->regmap, SAI_INTCR, 1359 SAI_INTCR_RXOIE_MASK, 1360 SAI_INTCR_RXOIE(0)); 1361 substream = sai->substreams[SNDRV_PCM_STREAM_CAPTURE]; 1362 if (substream) 1363 snd_pcm_stop_xrun(substream); 1364 } 1365 1366 if (val & SAI_INTSR_FSERRI_ACT) { 1367 dev_warn_ratelimited(sai->dev, "Frame Sync Error\n"); 1368 regmap_update_bits(sai->regmap, SAI_INTCR, 1369 SAI_INTCR_FSERRC, SAI_INTCR_FSERRC); 1370 regmap_update_bits(sai->regmap, SAI_INTCR, 1371 SAI_INTCR_FSERR_MASK, 1372 SAI_INTCR_FSERR(0)); 1373 } 1374 1375 if (val & SAI_INTSR_FSLOSTI_ACT) { 1376 dev_warn_ratelimited(sai->dev, "Frame Sync Lost\n"); 1377 regmap_update_bits(sai->regmap, SAI_INTCR, 1378 SAI_INTCR_FSLOSTC, SAI_INTCR_FSLOSTC); 1379 regmap_update_bits(sai->regmap, SAI_INTCR, 1380 SAI_INTCR_FSLOST_MASK, 1381 SAI_INTCR_FSLOST(0)); 1382 } 1383 1384 return IRQ_HANDLED; 1385 } 1386 1387 static int rockchip_sai_probe(struct platform_device *pdev) 1388 { 1389 struct device_node *node = pdev->dev.of_node; 1390 struct rk_sai_dev *sai; 1391 struct snd_soc_dai_driver *dai; 1392 struct resource *res; 1393 void __iomem *regs; 1394 int ret, irq; 1395 1396 sai = devm_kzalloc(&pdev->dev, sizeof(*sai), GFP_KERNEL); 1397 if (!sai) 1398 return -ENOMEM; 1399 1400 sai->dev = &pdev->dev; 1401 sai->fw_ratio = 1; 1402 /* match to register default */ 1403 sai->is_master_mode = true; 1404 dev_set_drvdata(&pdev->dev, sai); 1405 1406 spin_lock_init(&sai->xfer_lock); 1407 1408 sai->rst_h = devm_reset_control_get_optional_exclusive(&pdev->dev, "h"); 1409 if (IS_ERR(sai->rst_h)) 1410 return dev_err_probe(&pdev->dev, PTR_ERR(sai->rst_h), 1411 "Error in 'h' reset control\n"); 1412 1413 sai->rst_m = devm_reset_control_get_optional_exclusive(&pdev->dev, "m"); 1414 if (IS_ERR(sai->rst_m)) 1415 return dev_err_probe(&pdev->dev, PTR_ERR(sai->rst_m), 1416 "Error in 'm' reset control\n"); 1417 1418 regs = devm_platform_get_and_ioremap_resource(pdev, 0, &res); 1419 if (IS_ERR(regs)) 1420 return dev_err_probe(&pdev->dev, PTR_ERR(regs), 1421 "Failed to get and ioremap resource\n"); 1422 1423 sai->regmap = devm_regmap_init_mmio(&pdev->dev, regs, 1424 &rockchip_sai_regmap_config); 1425 if (IS_ERR(sai->regmap)) 1426 return dev_err_probe(&pdev->dev, PTR_ERR(sai->regmap), 1427 "Failed to initialize regmap\n"); 1428 1429 irq = platform_get_irq_optional(pdev, 0); 1430 if (irq > 0) { 1431 ret = devm_request_irq(&pdev->dev, irq, rockchip_sai_isr, 1432 IRQF_SHARED, node->name, sai); 1433 if (ret) 1434 return dev_err_probe(&pdev->dev, ret, 1435 "Failed to request irq %d\n", irq); 1436 } else { 1437 dev_dbg(&pdev->dev, "Asked for an IRQ but got %d\n", irq); 1438 } 1439 1440 sai->mclk = devm_clk_get(&pdev->dev, "mclk"); 1441 if (IS_ERR(sai->mclk)) 1442 return dev_err_probe(&pdev->dev, PTR_ERR(sai->mclk), 1443 "Failed to get mclk\n"); 1444 1445 sai->hclk = devm_clk_get_enabled(&pdev->dev, "hclk"); 1446 if (IS_ERR(sai->hclk)) 1447 return dev_err_probe(&pdev->dev, PTR_ERR(sai->hclk), 1448 "Failed to get hclk\n"); 1449 1450 regmap_read(sai->regmap, SAI_VERSION, &sai->version); 1451 1452 ret = rockchip_sai_init_dai(sai, res, &dai); 1453 if (ret) 1454 return dev_err_probe(&pdev->dev, ret, "Failed to initialize DAI\n"); 1455 1456 ret = rockchip_sai_parse_paths(sai, node); 1457 if (ret) 1458 return dev_err_probe(&pdev->dev, ret, "Failed to parse paths\n"); 1459 1460 /* 1461 * From here on, all register accesses need to be wrapped in 1462 * pm_runtime_get_sync/pm_runtime_put calls 1463 * 1464 * NB: we don't rely on _resume_and_get in case of !CONFIG_PM 1465 */ 1466 devm_pm_runtime_enable(&pdev->dev); 1467 pm_runtime_get_noresume(&pdev->dev); 1468 ret = rockchip_sai_runtime_resume(&pdev->dev); 1469 if (ret) 1470 return dev_err_probe(&pdev->dev, ret, "Failed to resume device\n"); 1471 1472 ret = devm_snd_dmaengine_pcm_register(&pdev->dev, NULL, 0); 1473 if (ret) { 1474 dev_err(&pdev->dev, "Failed to register PCM: %d\n", ret); 1475 goto err_runtime_suspend; 1476 } 1477 1478 ret = devm_snd_soc_register_component(&pdev->dev, 1479 &rockchip_sai_component, 1480 dai, 1); 1481 if (ret) { 1482 dev_err(&pdev->dev, "Failed to register component: %d\n", ret); 1483 goto err_runtime_suspend; 1484 } 1485 1486 pm_runtime_use_autosuspend(&pdev->dev); 1487 pm_runtime_put(&pdev->dev); 1488 1489 clk_disable_unprepare(sai->hclk); 1490 1491 return 0; 1492 1493 err_runtime_suspend: 1494 if (IS_ENABLED(CONFIG_PM)) 1495 pm_runtime_put(&pdev->dev); 1496 else 1497 rockchip_sai_runtime_suspend(&pdev->dev); 1498 1499 return ret; 1500 } 1501 1502 static void rockchip_sai_remove(struct platform_device *pdev) 1503 { 1504 #ifndef CONFIG_PM 1505 rockchip_sai_runtime_suspend(&pdev->dev); 1506 #endif 1507 } 1508 1509 static const struct dev_pm_ops rockchip_sai_pm_ops = { 1510 SET_RUNTIME_PM_OPS(rockchip_sai_runtime_suspend, rockchip_sai_runtime_resume, NULL) 1511 SET_SYSTEM_SLEEP_PM_OPS(pm_runtime_force_suspend, pm_runtime_force_resume) 1512 }; 1513 1514 static const struct of_device_id rockchip_sai_match[] = { 1515 { .compatible = "rockchip,rk3576-sai", }, 1516 {}, 1517 }; 1518 MODULE_DEVICE_TABLE(of, rockchip_sai_match); 1519 1520 static struct platform_driver rockchip_sai_driver = { 1521 .probe = rockchip_sai_probe, 1522 .remove = rockchip_sai_remove, 1523 .driver = { 1524 .name = DRV_NAME, 1525 .of_match_table = rockchip_sai_match, 1526 .pm = &rockchip_sai_pm_ops, 1527 }, 1528 }; 1529 module_platform_driver(rockchip_sai_driver); 1530 1531 MODULE_DESCRIPTION("Rockchip SAI ASoC Interface"); 1532 MODULE_AUTHOR("Sugar Zhang <sugar.zhang@rock-chips.com>"); 1533 MODULE_AUTHOR("Nicolas Frattaroli <nicolas.frattaroli@collabora.com>"); 1534 MODULE_LICENSE("GPL"); 1535