1 // SPDX-License-Identifier: GPL-2.0 2 // 3 // Renesas R-Car SSIU/SSI support 4 // 5 // Copyright (C) 2013 Renesas Solutions Corp. 6 // Kuninori Morimoto <kuninori.morimoto.gx@renesas.com> 7 // 8 // Based on fsi.c 9 // Kuninori Morimoto <morimoto.kuninori@renesas.com> 10 11 /* 12 * you can enable below define if you don't need 13 * SSI interrupt status debug message when debugging 14 * see rsnd_print_irq_status() 15 * 16 * #define RSND_DEBUG_NO_IRQ_STATUS 1 17 */ 18 19 #include <sound/simple_card_utils.h> 20 #include <linux/delay.h> 21 #include "rsnd.h" 22 #define RSND_SSI_NAME_SIZE 16 23 24 /* 25 * SSICR 26 */ 27 #define FORCE (1u << 31) /* Fixed */ 28 #define DMEN (1u << 28) /* DMA Enable */ 29 #define UIEN (1u << 27) /* Underflow Interrupt Enable */ 30 #define OIEN (1u << 26) /* Overflow Interrupt Enable */ 31 #define IIEN (1u << 25) /* Idle Mode Interrupt Enable */ 32 #define DIEN (1u << 24) /* Data Interrupt Enable */ 33 #define CHNL_4 (1u << 22) /* Channels */ 34 #define CHNL_6 (2u << 22) /* Channels */ 35 #define CHNL_8 (3u << 22) /* Channels */ 36 #define DWL_MASK (7u << 19) /* Data Word Length mask */ 37 #define DWL_8 (0u << 19) /* Data Word Length */ 38 #define DWL_16 (1u << 19) /* Data Word Length */ 39 #define DWL_18 (2u << 19) /* Data Word Length */ 40 #define DWL_20 (3u << 19) /* Data Word Length */ 41 #define DWL_22 (4u << 19) /* Data Word Length */ 42 #define DWL_24 (5u << 19) /* Data Word Length */ 43 #define DWL_32 (6u << 19) /* Data Word Length */ 44 45 /* 46 * System word length 47 */ 48 #define SWL_16 (1 << 16) /* R/W System Word Length */ 49 #define SWL_24 (2 << 16) /* R/W System Word Length */ 50 #define SWL_32 (3 << 16) /* R/W System Word Length */ 51 52 #define SCKD (1 << 15) /* Serial Bit Clock Direction */ 53 #define SWSD (1 << 14) /* Serial WS Direction */ 54 #define SCKP (1 << 13) /* Serial Bit Clock Polarity */ 55 #define SWSP (1 << 12) /* Serial WS Polarity */ 56 #define SDTA (1 << 10) /* Serial Data Alignment */ 57 #define PDTA (1 << 9) /* Parallel Data Alignment */ 58 #define DEL (1 << 8) /* Serial Data Delay */ 59 #define CKDV(v) (v << 4) /* Serial Clock Division Ratio */ 60 #define TRMD (1 << 1) /* Transmit/Receive Mode Select */ 61 #define EN (1 << 0) /* SSI Module Enable */ 62 63 /* 64 * SSISR 65 */ 66 #define UIRQ (1 << 27) /* Underflow Error Interrupt Status */ 67 #define OIRQ (1 << 26) /* Overflow Error Interrupt Status */ 68 #define IIRQ (1 << 25) /* Idle Mode Interrupt Status */ 69 #define DIRQ (1 << 24) /* Data Interrupt Status Flag */ 70 71 /* 72 * SSIWSR 73 */ 74 #define CONT (1 << 8) /* WS Continue Function */ 75 #define WS_MODE (1 << 0) /* WS Mode */ 76 77 #define SSI_NAME "ssi" 78 79 struct rsnd_ssi { 80 struct rsnd_mod mod; 81 82 u32 flags; 83 u32 cr_own; 84 u32 cr_clk; 85 u32 cr_mode; 86 u32 cr_en; 87 u32 wsr; 88 int chan; 89 int rate; 90 int irq; 91 unsigned int usrcnt; 92 93 /* for PIO */ 94 int byte_pos; 95 int byte_per_period; 96 int next_period_byte; 97 }; 98 99 /* flags */ 100 #define RSND_SSI_CLK_PIN_SHARE (1 << 0) 101 #define RSND_SSI_NO_BUSIF (1 << 1) /* SSI+DMA without BUSIF */ 102 #define RSND_SSI_PROBED (1 << 2) 103 104 #define for_each_rsnd_ssi(pos, priv, i) \ 105 for (i = 0; \ 106 (i < rsnd_ssi_nr(priv)) && \ 107 ((pos) = ((struct rsnd_ssi *)(priv)->ssi + i)); \ 108 i++) 109 110 #define rsnd_ssi_get(priv, id) ((struct rsnd_ssi *)(priv->ssi) + id) 111 #define rsnd_ssi_nr(priv) ((priv)->ssi_nr) 112 #define rsnd_mod_to_ssi(_mod) container_of((_mod), struct rsnd_ssi, mod) 113 #define rsnd_ssi_is_parent(ssi, io) ((ssi) == rsnd_io_to_mod_ssip(io)) 114 #define rsnd_ssi_is_multi_secondary(mod, io) \ 115 (rsnd_ssi_multi_secondaries(io) & (1 << rsnd_mod_id(mod))) 116 #define rsnd_ssi_is_run_mods(mod, io) \ 117 (rsnd_ssi_run_mods(io) & (1 << rsnd_mod_id(mod))) 118 #define rsnd_ssi_can_output_clk(mod) (!__rsnd_ssi_is_pin_sharing(mod)) 119 120 int rsnd_ssi_use_busif(struct rsnd_dai_stream *io) 121 { 122 struct rsnd_mod *mod = rsnd_io_to_mod_ssi(io); 123 struct rsnd_ssi *ssi = rsnd_mod_to_ssi(mod); 124 int use_busif = 0; 125 126 if (!rsnd_ssi_is_dma_mode(mod)) 127 return 0; 128 129 if (!(rsnd_flags_has(ssi, RSND_SSI_NO_BUSIF))) 130 use_busif = 1; 131 if (rsnd_io_to_mod_src(io)) 132 use_busif = 1; 133 134 return use_busif; 135 } 136 137 static void rsnd_ssi_status_clear(struct rsnd_mod *mod) 138 { 139 rsnd_mod_write(mod, SSISR, 0); 140 } 141 142 static u32 rsnd_ssi_status_get(struct rsnd_mod *mod) 143 { 144 return rsnd_mod_read(mod, SSISR); 145 } 146 147 static void rsnd_ssi_status_check(struct rsnd_mod *mod, 148 u32 bit) 149 { 150 struct rsnd_priv *priv = rsnd_mod_to_priv(mod); 151 struct device *dev = rsnd_priv_to_dev(priv); 152 u32 status; 153 int i; 154 155 for (i = 0; i < 1024; i++) { 156 status = rsnd_ssi_status_get(mod); 157 if (status & bit) 158 return; 159 160 udelay(5); 161 } 162 163 dev_warn(dev, "%s status check failed\n", rsnd_mod_name(mod)); 164 } 165 166 static u32 rsnd_ssi_multi_secondaries(struct rsnd_dai_stream *io) 167 { 168 static const enum rsnd_mod_type types[] = { 169 RSND_MOD_SSIM1, 170 RSND_MOD_SSIM2, 171 RSND_MOD_SSIM3, 172 }; 173 int i, mask; 174 175 mask = 0; 176 for (i = 0; i < ARRAY_SIZE(types); i++) { 177 struct rsnd_mod *mod = rsnd_io_to_mod(io, types[i]); 178 179 if (!mod) 180 continue; 181 182 mask |= 1 << rsnd_mod_id(mod); 183 } 184 185 return mask; 186 } 187 188 static u32 rsnd_ssi_run_mods(struct rsnd_dai_stream *io) 189 { 190 struct rsnd_mod *ssi_mod = rsnd_io_to_mod_ssi(io); 191 struct rsnd_mod *ssi_parent_mod = rsnd_io_to_mod_ssip(io); 192 u32 mods; 193 194 mods = rsnd_ssi_multi_secondaries_runtime(io) | 195 1 << rsnd_mod_id(ssi_mod); 196 197 if (ssi_parent_mod) 198 mods |= 1 << rsnd_mod_id(ssi_parent_mod); 199 200 return mods; 201 } 202 203 u32 rsnd_ssi_multi_secondaries_runtime(struct rsnd_dai_stream *io) 204 { 205 if (rsnd_runtime_is_multi_ssi(io)) 206 return rsnd_ssi_multi_secondaries(io); 207 208 return 0; 209 } 210 211 static u32 rsnd_rdai_width_to_swl(struct rsnd_dai *rdai) 212 { 213 struct rsnd_priv *priv = rsnd_rdai_to_priv(rdai); 214 struct device *dev = rsnd_priv_to_dev(priv); 215 int width = rsnd_rdai_width_get(rdai); 216 217 switch (width) { 218 case 32: return SWL_32; 219 case 24: return SWL_24; 220 case 16: return SWL_16; 221 } 222 223 dev_err(dev, "unsupported slot width value: %d\n", width); 224 return 0; 225 } 226 227 unsigned int rsnd_ssi_clk_query(struct rsnd_dai *rdai, 228 int param1, int param2, int *idx) 229 { 230 struct rsnd_priv *priv = rsnd_rdai_to_priv(rdai); 231 static const int ssi_clk_mul_table[] = { 232 1, 2, 4, 8, 16, 6, 12, 233 }; 234 int j, ret; 235 unsigned int main_rate; 236 int width = rsnd_rdai_width_get(rdai); 237 238 for (j = 0; j < ARRAY_SIZE(ssi_clk_mul_table); j++) { 239 240 /* 241 * It will set SSIWSR.CONT here, but SSICR.CKDV = 000 242 * with it is not allowed. (SSIWSR.WS_MODE with 243 * SSICR.CKDV = 000 is not allowed either). 244 * Skip it. See SSICR.CKDV 245 */ 246 if (j == 0) 247 continue; 248 249 main_rate = width * param1 * param2 * ssi_clk_mul_table[j]; 250 251 ret = rsnd_adg_clk_query(priv, main_rate); 252 if (ret < 0) 253 continue; 254 255 if (idx) 256 *idx = j; 257 258 return main_rate; 259 } 260 261 return 0; 262 } 263 264 static int rsnd_ssi_master_clk_start(struct rsnd_mod *mod, 265 struct rsnd_dai_stream *io) 266 { 267 struct rsnd_priv *priv = rsnd_io_to_priv(io); 268 struct device *dev = rsnd_priv_to_dev(priv); 269 struct rsnd_dai *rdai = rsnd_io_to_rdai(io); 270 struct rsnd_ssi *ssi = rsnd_mod_to_ssi(mod); 271 int chan = rsnd_runtime_channel_for_ssi(io); 272 int idx, ret; 273 unsigned int main_rate; 274 unsigned int rate = rsnd_io_is_play(io) ? 275 rsnd_src_get_out_rate(priv, io) : 276 rsnd_src_get_in_rate(priv, io); 277 278 if (!rsnd_rdai_is_clk_master(rdai)) 279 return 0; 280 281 if (!rsnd_ssi_can_output_clk(mod)) 282 return 0; 283 284 if (rsnd_ssi_is_multi_secondary(mod, io)) 285 return 0; 286 287 if (rsnd_runtime_is_tdm_split(io)) 288 chan = rsnd_io_converted_chan(io); 289 290 chan = rsnd_channel_normalization(chan); 291 292 if (ssi->usrcnt > 0) { 293 if (ssi->rate != rate) { 294 dev_err(dev, "SSI parent/child should use same rate\n"); 295 return -EINVAL; 296 } 297 298 if (ssi->chan != chan) { 299 dev_err(dev, "SSI parent/child should use same chan\n"); 300 return -EINVAL; 301 } 302 303 return 0; 304 } 305 306 ret = -EIO; 307 main_rate = rsnd_ssi_clk_query(rdai, rate, chan, &idx); 308 if (!main_rate) 309 goto rate_err; 310 311 ret = rsnd_adg_ssi_clk_try_start(mod, main_rate); 312 if (ret < 0) 313 goto rate_err; 314 315 /* 316 * SSI clock will be output contiguously 317 * by below settings. 318 * This means, rsnd_ssi_master_clk_start() 319 * and rsnd_ssi_register_setup() are necessary 320 * for SSI parent 321 * 322 * SSICR : FORCE, SCKD, SWSD 323 * SSIWSR : CONT 324 */ 325 ssi->cr_clk = FORCE | rsnd_rdai_width_to_swl(rdai) | 326 SCKD | SWSD | CKDV(idx); 327 ssi->wsr = CONT; 328 ssi->rate = rate; 329 ssi->chan = chan; 330 331 dev_dbg(dev, "%s outputs %d chan %u Hz\n", 332 rsnd_mod_name(mod), chan, rate); 333 334 return 0; 335 336 rate_err: 337 dev_err(dev, "unsupported clock rate\n"); 338 return ret; 339 } 340 341 static void rsnd_ssi_master_clk_stop(struct rsnd_mod *mod, 342 struct rsnd_dai_stream *io) 343 { 344 struct rsnd_dai *rdai = rsnd_io_to_rdai(io); 345 struct rsnd_ssi *ssi = rsnd_mod_to_ssi(mod); 346 347 if (!rsnd_rdai_is_clk_master(rdai)) 348 return; 349 350 if (!rsnd_ssi_can_output_clk(mod)) 351 return; 352 353 if (ssi->usrcnt > 1) 354 return; 355 356 ssi->cr_clk = 0; 357 ssi->rate = 0; 358 ssi->chan = 0; 359 360 rsnd_adg_ssi_clk_stop(mod); 361 } 362 363 static void rsnd_ssi_config_init(struct rsnd_mod *mod, 364 struct rsnd_dai_stream *io) 365 { 366 struct rsnd_dai *rdai = rsnd_io_to_rdai(io); 367 struct rsnd_priv *priv = rsnd_rdai_to_priv(rdai); 368 struct device *dev = rsnd_priv_to_dev(priv); 369 struct snd_pcm_runtime *runtime = rsnd_io_to_runtime(io); 370 struct rsnd_ssi *ssi = rsnd_mod_to_ssi(mod); 371 u32 cr_own = ssi->cr_own; 372 u32 cr_mode = ssi->cr_mode; 373 u32 wsr = ssi->wsr; 374 int width; 375 int is_tdm, is_tdm_split; 376 377 is_tdm = rsnd_runtime_is_tdm(io); 378 is_tdm_split = rsnd_runtime_is_tdm_split(io); 379 380 if (is_tdm) 381 dev_dbg(dev, "TDM mode\n"); 382 if (is_tdm_split) 383 dev_dbg(dev, "TDM Split mode\n"); 384 385 cr_own |= FORCE | rsnd_rdai_width_to_swl(rdai); 386 387 if (rdai->bit_clk_inv) 388 cr_own |= SCKP; 389 if (rdai->frm_clk_inv && !is_tdm) 390 cr_own |= SWSP; 391 if (rdai->data_alignment) 392 cr_own |= SDTA; 393 if (rdai->sys_delay) 394 cr_own |= DEL; 395 396 /* 397 * TDM Mode 398 * see 399 * rsnd_ssiu_init_gen2() 400 */ 401 if (is_tdm || is_tdm_split) { 402 wsr |= WS_MODE; 403 cr_own |= CHNL_8; 404 } 405 406 /* 407 * We shouldn't exchange SWSP after running. 408 * This means, parent needs to care it. 409 */ 410 if (rsnd_ssi_is_parent(mod, io)) 411 goto init_end; 412 413 if (rsnd_io_is_play(io)) 414 cr_own |= TRMD; 415 416 cr_own &= ~DWL_MASK; 417 width = snd_pcm_format_width(runtime->format); 418 if (is_tdm_split) { 419 /* 420 * The SWL and DWL bits in SSICR should be fixed at 32-bit 421 * setting when TDM split mode. 422 * see datasheet 423 * Operation :: TDM Format Split Function (TDM Split Mode) 424 */ 425 width = 32; 426 } 427 428 switch (width) { 429 case 8: 430 cr_own |= DWL_8; 431 break; 432 case 16: 433 cr_own |= DWL_16; 434 break; 435 case 24: 436 cr_own |= DWL_24; 437 break; 438 case 32: 439 cr_own |= DWL_32; 440 break; 441 } 442 443 if (rsnd_ssi_is_dma_mode(mod)) { 444 cr_mode = UIEN | OIEN | /* over/under run */ 445 DMEN; /* DMA : enable DMA */ 446 } else { 447 cr_mode = DIEN; /* PIO : enable Data interrupt */ 448 } 449 450 init_end: 451 ssi->cr_own = cr_own; 452 ssi->cr_mode = cr_mode; 453 ssi->wsr = wsr; 454 } 455 456 static void rsnd_ssi_register_setup(struct rsnd_mod *mod) 457 { 458 struct rsnd_ssi *ssi = rsnd_mod_to_ssi(mod); 459 460 rsnd_mod_write(mod, SSIWSR, ssi->wsr); 461 rsnd_mod_write(mod, SSICR, ssi->cr_own | 462 ssi->cr_clk | 463 ssi->cr_mode | 464 ssi->cr_en); 465 } 466 467 /* 468 * SSI mod common functions 469 */ 470 static int rsnd_ssi_init(struct rsnd_mod *mod, 471 struct rsnd_dai_stream *io, 472 struct rsnd_priv *priv) 473 { 474 struct rsnd_ssi *ssi = rsnd_mod_to_ssi(mod); 475 int ret; 476 477 if (!rsnd_ssi_is_run_mods(mod, io)) 478 return 0; 479 480 ret = rsnd_ssi_master_clk_start(mod, io); 481 if (ret < 0) 482 return ret; 483 484 ssi->usrcnt++; 485 486 ret = rsnd_mod_power_on(mod); 487 if (ret < 0) 488 return ret; 489 490 rsnd_ssi_config_init(mod, io); 491 492 rsnd_ssi_register_setup(mod); 493 494 /* clear error status */ 495 rsnd_ssi_status_clear(mod); 496 497 return 0; 498 } 499 500 static int rsnd_ssi_quit(struct rsnd_mod *mod, 501 struct rsnd_dai_stream *io, 502 struct rsnd_priv *priv) 503 { 504 struct rsnd_ssi *ssi = rsnd_mod_to_ssi(mod); 505 struct device *dev = rsnd_priv_to_dev(priv); 506 507 if (!rsnd_ssi_is_run_mods(mod, io)) 508 return 0; 509 510 if (!ssi->usrcnt) { 511 dev_err(dev, "%s usrcnt error\n", rsnd_mod_name(mod)); 512 return -EIO; 513 } 514 515 rsnd_ssi_master_clk_stop(mod, io); 516 517 rsnd_mod_power_off(mod); 518 519 ssi->usrcnt--; 520 521 if (!ssi->usrcnt) { 522 ssi->cr_own = 0; 523 ssi->cr_mode = 0; 524 ssi->wsr = 0; 525 } 526 527 return 0; 528 } 529 530 static int rsnd_ssi_hw_params(struct rsnd_mod *mod, 531 struct rsnd_dai_stream *io, 532 struct snd_pcm_substream *substream, 533 struct snd_pcm_hw_params *params) 534 { 535 struct rsnd_dai *rdai = rsnd_io_to_rdai(io); 536 unsigned int fmt_width = snd_pcm_format_width(params_format(params)); 537 538 if (fmt_width > rdai->chan_width) { 539 struct rsnd_priv *priv = rsnd_io_to_priv(io); 540 struct device *dev = rsnd_priv_to_dev(priv); 541 542 dev_err(dev, "invalid combination of slot-width and format-data-width\n"); 543 return -EINVAL; 544 } 545 546 return 0; 547 } 548 549 static int rsnd_ssi_start(struct rsnd_mod *mod, 550 struct rsnd_dai_stream *io, 551 struct rsnd_priv *priv) 552 { 553 struct rsnd_ssi *ssi = rsnd_mod_to_ssi(mod); 554 555 if (!rsnd_ssi_is_run_mods(mod, io)) 556 return 0; 557 558 /* 559 * EN will be set via SSIU :: SSI_CONTROL 560 * if Multi channel mode 561 */ 562 if (rsnd_ssi_multi_secondaries_runtime(io)) 563 return 0; 564 565 /* 566 * EN is for data output. 567 * SSI parent EN is not needed. 568 */ 569 if (rsnd_ssi_is_parent(mod, io)) 570 return 0; 571 572 ssi->cr_en = EN; 573 574 rsnd_mod_write(mod, SSICR, ssi->cr_own | 575 ssi->cr_clk | 576 ssi->cr_mode | 577 ssi->cr_en); 578 579 return 0; 580 } 581 582 static int rsnd_ssi_stop(struct rsnd_mod *mod, 583 struct rsnd_dai_stream *io, 584 struct rsnd_priv *priv) 585 { 586 struct rsnd_ssi *ssi = rsnd_mod_to_ssi(mod); 587 u32 cr; 588 589 if (!rsnd_ssi_is_run_mods(mod, io)) 590 return 0; 591 592 if (rsnd_ssi_is_parent(mod, io)) 593 return 0; 594 595 cr = ssi->cr_own | 596 ssi->cr_clk; 597 598 /* 599 * disable all IRQ, 600 * Playback: Wait all data was sent 601 * Capture: It might not receave data. Do nothing 602 */ 603 if (rsnd_io_is_play(io)) { 604 rsnd_mod_write(mod, SSICR, cr | ssi->cr_en); 605 rsnd_ssi_status_check(mod, DIRQ); 606 } 607 608 /* In multi-SSI mode, stop is performed by setting ssi0129 in 609 * SSI_CONTROL to 0 (in rsnd_ssio_stop_gen2). Do nothing here. 610 */ 611 if (rsnd_ssi_multi_secondaries_runtime(io)) 612 return 0; 613 614 /* 615 * disable SSI, 616 * and, wait idle state 617 */ 618 rsnd_mod_write(mod, SSICR, cr); /* disabled all */ 619 rsnd_ssi_status_check(mod, IIRQ); 620 621 ssi->cr_en = 0; 622 623 return 0; 624 } 625 626 static int rsnd_ssi_irq(struct rsnd_mod *mod, 627 struct rsnd_dai_stream *io, 628 struct rsnd_priv *priv, 629 int enable) 630 { 631 u32 val = 0; 632 int is_tdm, is_tdm_split; 633 int id = rsnd_mod_id(mod); 634 635 is_tdm = rsnd_runtime_is_tdm(io); 636 is_tdm_split = rsnd_runtime_is_tdm_split(io); 637 638 if (rsnd_is_gen1(priv)) 639 return 0; 640 641 if (rsnd_ssi_is_parent(mod, io)) 642 return 0; 643 644 if (!rsnd_ssi_is_run_mods(mod, io)) 645 return 0; 646 647 if (enable) 648 val = rsnd_ssi_is_dma_mode(mod) ? 0x0e000000 : 0x0f000000; 649 650 if (is_tdm || is_tdm_split) { 651 switch (id) { 652 case 0: 653 case 1: 654 case 2: 655 case 3: 656 case 4: 657 case 9: 658 val |= 0x0000ff00; 659 break; 660 } 661 } 662 663 rsnd_mod_write(mod, SSI_INT_ENABLE, val); 664 665 return 0; 666 } 667 668 static bool rsnd_ssi_pio_interrupt(struct rsnd_mod *mod, 669 struct rsnd_dai_stream *io); 670 static void __rsnd_ssi_interrupt(struct rsnd_mod *mod, 671 struct rsnd_dai_stream *io) 672 { 673 struct rsnd_priv *priv = rsnd_mod_to_priv(mod); 674 struct device *dev = rsnd_priv_to_dev(priv); 675 int is_dma = rsnd_ssi_is_dma_mode(mod); 676 u32 status; 677 bool elapsed = false; 678 bool stop = false; 679 680 spin_lock(&priv->lock); 681 682 /* ignore all cases if not working */ 683 if (!rsnd_io_is_working(io)) 684 goto rsnd_ssi_interrupt_out; 685 686 status = rsnd_ssi_status_get(mod); 687 688 /* PIO only */ 689 if (!is_dma && (status & DIRQ)) 690 elapsed = rsnd_ssi_pio_interrupt(mod, io); 691 692 /* DMA only */ 693 if (is_dma && (status & (UIRQ | OIRQ))) { 694 rsnd_print_irq_status(dev, "%s err status : 0x%08x\n", 695 rsnd_mod_name(mod), status); 696 697 stop = true; 698 } 699 700 stop |= rsnd_ssiu_busif_err_status_clear(mod); 701 702 rsnd_ssi_status_clear(mod); 703 rsnd_ssi_interrupt_out: 704 spin_unlock(&priv->lock); 705 706 if (elapsed) 707 rsnd_dai_period_elapsed(io); 708 709 if (stop) 710 snd_pcm_stop_xrun(io->substream); 711 712 } 713 714 static irqreturn_t rsnd_ssi_interrupt(int irq, void *data) 715 { 716 struct rsnd_mod *mod = data; 717 718 rsnd_mod_interrupt(mod, __rsnd_ssi_interrupt); 719 720 return IRQ_HANDLED; 721 } 722 723 static u32 *rsnd_ssi_get_status(struct rsnd_mod *mod, 724 struct rsnd_dai_stream *io, 725 enum rsnd_mod_type type) 726 { 727 /* 728 * SSIP (= SSI parent) needs to be special, otherwise, 729 * 2nd SSI might doesn't start. see also rsnd_mod_call() 730 * 731 * We can't include parent SSI status on SSI, because we don't know 732 * how many SSI requests parent SSI. Thus, it is localed on "io" now. 733 * ex) trouble case 734 * Playback: SSI0 735 * Capture : SSI1 (needs SSI0) 736 * 737 * 1) start Capture -> SSI0/SSI1 are started. 738 * 2) start Playback -> SSI0 doesn't work, because it is already 739 * marked as "started" on 1) 740 * 741 * OTOH, using each mod's status is good for MUX case. 742 * It doesn't need to start in 2nd start 743 * ex) 744 * IO-0: SRC0 -> CTU1 -+-> MUX -> DVC -> SSIU -> SSI0 745 * | 746 * IO-1: SRC1 -> CTU2 -+ 747 * 748 * 1) start IO-0 -> start SSI0 749 * 2) start IO-1 -> SSI0 doesn't need to start, because it is 750 * already started on 1) 751 */ 752 if (type == RSND_MOD_SSIP) 753 return &io->parent_ssi_status; 754 755 return rsnd_mod_get_status(mod, io, type); 756 } 757 758 /* 759 * SSI PIO 760 */ 761 static void rsnd_ssi_parent_attach(struct rsnd_mod *mod, 762 struct rsnd_dai_stream *io) 763 { 764 struct rsnd_dai *rdai = rsnd_io_to_rdai(io); 765 struct rsnd_priv *priv = rsnd_mod_to_priv(mod); 766 767 if (!__rsnd_ssi_is_pin_sharing(mod)) 768 return; 769 770 if (!rsnd_rdai_is_clk_master(rdai)) 771 return; 772 773 if (rsnd_ssi_is_multi_secondary(mod, io)) 774 return; 775 776 switch (rsnd_mod_id(mod)) { 777 case 1: 778 case 2: 779 case 9: 780 rsnd_dai_connect(rsnd_ssi_mod_get(priv, 0), io, RSND_MOD_SSIP); 781 break; 782 case 4: 783 rsnd_dai_connect(rsnd_ssi_mod_get(priv, 3), io, RSND_MOD_SSIP); 784 break; 785 case 8: 786 rsnd_dai_connect(rsnd_ssi_mod_get(priv, 7), io, RSND_MOD_SSIP); 787 break; 788 } 789 } 790 791 static int rsnd_ssi_pcm_new(struct rsnd_mod *mod, 792 struct rsnd_dai_stream *io, 793 struct snd_soc_pcm_runtime *rtd) 794 { 795 /* 796 * rsnd_rdai_is_clk_master() will be enabled after set_fmt, 797 * and, pcm_new will be called after it. 798 * This function reuse pcm_new at this point. 799 */ 800 rsnd_ssi_parent_attach(mod, io); 801 802 return 0; 803 } 804 805 static int rsnd_ssi_common_probe(struct rsnd_mod *mod, 806 struct rsnd_dai_stream *io, 807 struct rsnd_priv *priv) 808 { 809 struct device *dev = rsnd_priv_to_dev(priv); 810 struct rsnd_ssi *ssi = rsnd_mod_to_ssi(mod); 811 int ret = 0; 812 813 /* 814 * SSIP/SSIU/IRQ are not needed on 815 * SSI Multi secondaries 816 */ 817 if (rsnd_ssi_is_multi_secondary(mod, io)) 818 return 0; 819 820 /* 821 * It can't judge ssi parent at this point 822 * see rsnd_ssi_pcm_new() 823 */ 824 825 /* 826 * SSI might be called again as PIO fallback 827 * It is easy to manual handling for IRQ request/free 828 * 829 * OTOH, this function might be called many times if platform is 830 * using MIX. It needs xxx_attach() many times on xxx_probe(). 831 * Because of it, we can't control .probe/.remove calling count by 832 * mod->status. 833 * But it don't need to call request_irq() many times. 834 * Let's control it by RSND_SSI_PROBED flag. 835 */ 836 if (!rsnd_flags_has(ssi, RSND_SSI_PROBED)) { 837 ret = request_irq(ssi->irq, 838 rsnd_ssi_interrupt, 839 IRQF_SHARED, 840 dev_name(dev), mod); 841 842 rsnd_flags_set(ssi, RSND_SSI_PROBED); 843 } 844 845 return ret; 846 } 847 848 static int rsnd_ssi_common_remove(struct rsnd_mod *mod, 849 struct rsnd_dai_stream *io, 850 struct rsnd_priv *priv) 851 { 852 struct rsnd_ssi *ssi = rsnd_mod_to_ssi(mod); 853 struct rsnd_mod *pure_ssi_mod = rsnd_io_to_mod_ssi(io); 854 855 /* Do nothing if non SSI (= SSI parent, multi SSI) mod */ 856 if (pure_ssi_mod != mod) 857 return 0; 858 859 /* PIO will request IRQ again */ 860 if (rsnd_flags_has(ssi, RSND_SSI_PROBED)) { 861 free_irq(ssi->irq, mod); 862 863 rsnd_flags_del(ssi, RSND_SSI_PROBED); 864 } 865 866 return 0; 867 } 868 869 /* 870 * SSI PIO functions 871 */ 872 static bool rsnd_ssi_pio_interrupt(struct rsnd_mod *mod, 873 struct rsnd_dai_stream *io) 874 { 875 struct snd_pcm_runtime *runtime = rsnd_io_to_runtime(io); 876 struct rsnd_ssi *ssi = rsnd_mod_to_ssi(mod); 877 u32 *buf = (u32 *)(runtime->dma_area + ssi->byte_pos); 878 int shift = 0; 879 int byte_pos; 880 bool elapsed = false; 881 882 if (snd_pcm_format_width(runtime->format) == 24) 883 shift = 8; 884 885 /* 886 * 8/16/32 data can be assesse to TDR/RDR register 887 * directly as 32bit data 888 * see rsnd_ssi_init() 889 */ 890 if (rsnd_io_is_play(io)) 891 rsnd_mod_write(mod, SSITDR, (*buf) << shift); 892 else 893 *buf = (rsnd_mod_read(mod, SSIRDR) >> shift); 894 895 byte_pos = ssi->byte_pos + sizeof(*buf); 896 897 if (byte_pos >= ssi->next_period_byte) { 898 int period_pos = byte_pos / ssi->byte_per_period; 899 900 if (period_pos >= runtime->periods) { 901 byte_pos = 0; 902 period_pos = 0; 903 } 904 905 ssi->next_period_byte = (period_pos + 1) * ssi->byte_per_period; 906 907 elapsed = true; 908 } 909 910 WRITE_ONCE(ssi->byte_pos, byte_pos); 911 912 return elapsed; 913 } 914 915 static int rsnd_ssi_pio_init(struct rsnd_mod *mod, 916 struct rsnd_dai_stream *io, 917 struct rsnd_priv *priv) 918 { 919 struct snd_pcm_runtime *runtime = rsnd_io_to_runtime(io); 920 struct rsnd_ssi *ssi = rsnd_mod_to_ssi(mod); 921 922 if (!rsnd_ssi_is_parent(mod, io)) { 923 ssi->byte_pos = 0; 924 ssi->byte_per_period = runtime->period_size * 925 runtime->channels * 926 samples_to_bytes(runtime, 1); 927 ssi->next_period_byte = ssi->byte_per_period; 928 } 929 930 return rsnd_ssi_init(mod, io, priv); 931 } 932 933 static int rsnd_ssi_pio_pointer(struct rsnd_mod *mod, 934 struct rsnd_dai_stream *io, 935 snd_pcm_uframes_t *pointer) 936 { 937 struct rsnd_ssi *ssi = rsnd_mod_to_ssi(mod); 938 struct snd_pcm_runtime *runtime = rsnd_io_to_runtime(io); 939 940 *pointer = bytes_to_frames(runtime, READ_ONCE(ssi->byte_pos)); 941 942 return 0; 943 } 944 945 static struct rsnd_mod_ops rsnd_ssi_pio_ops = { 946 .name = SSI_NAME, 947 .probe = rsnd_ssi_common_probe, 948 .remove = rsnd_ssi_common_remove, 949 .init = rsnd_ssi_pio_init, 950 .quit = rsnd_ssi_quit, 951 .start = rsnd_ssi_start, 952 .stop = rsnd_ssi_stop, 953 .irq = rsnd_ssi_irq, 954 .pointer = rsnd_ssi_pio_pointer, 955 .pcm_new = rsnd_ssi_pcm_new, 956 .hw_params = rsnd_ssi_hw_params, 957 .get_status = rsnd_ssi_get_status, 958 }; 959 960 static int rsnd_ssi_dma_probe(struct rsnd_mod *mod, 961 struct rsnd_dai_stream *io, 962 struct rsnd_priv *priv) 963 { 964 int ret; 965 966 /* 967 * SSIP/SSIU/IRQ/DMA are not needed on 968 * SSI Multi secondaries 969 */ 970 if (rsnd_ssi_is_multi_secondary(mod, io)) 971 return 0; 972 973 ret = rsnd_ssi_common_probe(mod, io, priv); 974 if (ret) 975 return ret; 976 977 /* SSI probe might be called many times in MUX multi path */ 978 ret = rsnd_dma_attach(io, mod, &io->dma); 979 980 return ret; 981 } 982 983 static int rsnd_ssi_fallback(struct rsnd_mod *mod, 984 struct rsnd_dai_stream *io, 985 struct rsnd_priv *priv) 986 { 987 struct device *dev = rsnd_priv_to_dev(priv); 988 989 /* 990 * fallback to PIO 991 * 992 * SSI .probe might be called again. 993 * see 994 * rsnd_rdai_continuance_probe() 995 */ 996 mod->ops = &rsnd_ssi_pio_ops; 997 998 dev_info(dev, "%s fallback to PIO mode\n", rsnd_mod_name(mod)); 999 1000 return 0; 1001 } 1002 1003 static struct dma_chan *rsnd_ssi_dma_req(struct rsnd_dai_stream *io, 1004 struct rsnd_mod *mod) 1005 { 1006 struct rsnd_priv *priv = rsnd_mod_to_priv(mod); 1007 int is_play = rsnd_io_is_play(io); 1008 char *name; 1009 1010 /* 1011 * It should use "rcar_sound,ssiu" on DT. 1012 * But, we need to keep compatibility for old version. 1013 * 1014 * If it has "rcar_sound.ssiu", it will be used. 1015 * If not, "rcar_sound.ssi" will be used. 1016 * see 1017 * rsnd_ssiu_dma_req() 1018 * rsnd_dma_of_path() 1019 */ 1020 1021 if (rsnd_ssi_use_busif(io)) 1022 name = is_play ? "rxu" : "txu"; 1023 else 1024 name = is_play ? "rx" : "tx"; 1025 1026 return rsnd_dma_request_channel(rsnd_ssi_of_node(priv), 1027 SSI_NAME, mod, name); 1028 } 1029 1030 #ifdef CONFIG_DEBUG_FS 1031 static void rsnd_ssi_debug_info(struct seq_file *m, 1032 struct rsnd_dai_stream *io, 1033 struct rsnd_mod *mod) 1034 { 1035 struct rsnd_dai *rdai = rsnd_io_to_rdai(io); 1036 struct rsnd_ssi *ssi = rsnd_mod_to_ssi(mod); 1037 1038 seq_printf(m, "clock: %s\n", rsnd_rdai_is_clk_master(rdai) ? 1039 "provider" : "consumer"); 1040 seq_printf(m, "bit_clk_inv: %d\n", rdai->bit_clk_inv); 1041 seq_printf(m, "frm_clk_inv: %d\n", rdai->frm_clk_inv); 1042 seq_printf(m, "pin share: %d\n", __rsnd_ssi_is_pin_sharing(mod)); 1043 seq_printf(m, "can out clk: %d\n", rsnd_ssi_can_output_clk(mod)); 1044 seq_printf(m, "multi secondary: %d\n", rsnd_ssi_is_multi_secondary(mod, io)); 1045 seq_printf(m, "tdm: %d, %d\n", rsnd_runtime_is_tdm(io), 1046 rsnd_runtime_is_tdm_split(io)); 1047 seq_printf(m, "chan: %d\n", ssi->chan); 1048 seq_printf(m, "user: %d\n", ssi->usrcnt); 1049 1050 rsnd_debugfs_mod_reg_show(m, mod, RSND_GEN2_SSI, 1051 rsnd_mod_id(mod) * 0x40, 0x40); 1052 } 1053 #define DEBUG_INFO .debug_info = rsnd_ssi_debug_info 1054 #else 1055 #define DEBUG_INFO 1056 #endif 1057 1058 static struct rsnd_mod_ops rsnd_ssi_dma_ops = { 1059 .name = SSI_NAME, 1060 .dma_req = rsnd_ssi_dma_req, 1061 .probe = rsnd_ssi_dma_probe, 1062 .remove = rsnd_ssi_common_remove, 1063 .init = rsnd_ssi_init, 1064 .quit = rsnd_ssi_quit, 1065 .start = rsnd_ssi_start, 1066 .stop = rsnd_ssi_stop, 1067 .irq = rsnd_ssi_irq, 1068 .pcm_new = rsnd_ssi_pcm_new, 1069 .fallback = rsnd_ssi_fallback, 1070 .hw_params = rsnd_ssi_hw_params, 1071 .get_status = rsnd_ssi_get_status, 1072 DEBUG_INFO 1073 }; 1074 1075 int rsnd_ssi_is_dma_mode(struct rsnd_mod *mod) 1076 { 1077 return mod->ops == &rsnd_ssi_dma_ops; 1078 } 1079 1080 /* 1081 * ssi mod function 1082 */ 1083 static void rsnd_ssi_connect(struct rsnd_mod *mod, 1084 struct rsnd_dai_stream *io) 1085 { 1086 struct rsnd_dai *rdai = rsnd_io_to_rdai(io); 1087 static const enum rsnd_mod_type types[] = { 1088 RSND_MOD_SSI, 1089 RSND_MOD_SSIM1, 1090 RSND_MOD_SSIM2, 1091 RSND_MOD_SSIM3, 1092 }; 1093 enum rsnd_mod_type type; 1094 int i; 1095 1096 /* try SSI -> SSIM1 -> SSIM2 -> SSIM3 */ 1097 for (i = 0; i < ARRAY_SIZE(types); i++) { 1098 type = types[i]; 1099 if (!rsnd_io_to_mod(io, type)) { 1100 rsnd_dai_connect(mod, io, type); 1101 rsnd_rdai_channels_set(rdai, (i + 1) * 2); 1102 rsnd_rdai_ssi_lane_set(rdai, (i + 1)); 1103 return; 1104 } 1105 } 1106 } 1107 1108 void rsnd_parse_connect_ssi(struct rsnd_dai *rdai, 1109 struct device_node *playback, 1110 struct device_node *capture) 1111 { 1112 struct rsnd_priv *priv = rsnd_rdai_to_priv(rdai); 1113 struct device *dev = rsnd_priv_to_dev(priv); 1114 struct device_node *node; 1115 struct device_node *np; 1116 int i; 1117 1118 node = rsnd_ssi_of_node(priv); 1119 if (!node) 1120 return; 1121 1122 i = 0; 1123 for_each_child_of_node(node, np) { 1124 struct rsnd_mod *mod; 1125 1126 i = rsnd_node_fixed_index(dev, np, SSI_NAME, i); 1127 if (i < 0) { 1128 of_node_put(np); 1129 break; 1130 } 1131 1132 mod = rsnd_ssi_mod_get(priv, i); 1133 1134 if (np == playback) 1135 rsnd_ssi_connect(mod, &rdai->playback); 1136 if (np == capture) 1137 rsnd_ssi_connect(mod, &rdai->capture); 1138 i++; 1139 } 1140 1141 of_node_put(node); 1142 } 1143 1144 struct rsnd_mod *rsnd_ssi_mod_get(struct rsnd_priv *priv, int id) 1145 { 1146 if (WARN_ON(id < 0 || id >= rsnd_ssi_nr(priv))) 1147 id = 0; 1148 1149 return rsnd_mod_get(rsnd_ssi_get(priv, id)); 1150 } 1151 1152 int __rsnd_ssi_is_pin_sharing(struct rsnd_mod *mod) 1153 { 1154 if (!mod) 1155 return 0; 1156 1157 return !!(rsnd_flags_has(rsnd_mod_to_ssi(mod), RSND_SSI_CLK_PIN_SHARE)); 1158 } 1159 1160 int rsnd_ssi_probe(struct rsnd_priv *priv) 1161 { 1162 struct device_node *node; 1163 struct device_node *np; 1164 struct device *dev = rsnd_priv_to_dev(priv); 1165 struct rsnd_mod_ops *ops; 1166 struct clk *clk; 1167 struct rsnd_ssi *ssi; 1168 char name[RSND_SSI_NAME_SIZE]; 1169 int i, nr, ret; 1170 1171 node = rsnd_ssi_of_node(priv); 1172 if (!node) 1173 return -EINVAL; 1174 1175 nr = rsnd_node_count(priv, node, SSI_NAME); 1176 if (!nr) { 1177 ret = -EINVAL; 1178 goto rsnd_ssi_probe_done; 1179 } 1180 1181 ssi = devm_kcalloc(dev, nr, sizeof(*ssi), GFP_KERNEL); 1182 if (!ssi) { 1183 ret = -ENOMEM; 1184 goto rsnd_ssi_probe_done; 1185 } 1186 1187 priv->ssi = ssi; 1188 priv->ssi_nr = nr; 1189 1190 i = 0; 1191 for_each_child_of_node(node, np) { 1192 if (!of_device_is_available(np)) 1193 goto skip; 1194 1195 i = rsnd_node_fixed_index(dev, np, SSI_NAME, i); 1196 if (i < 0) { 1197 ret = -EINVAL; 1198 of_node_put(np); 1199 goto rsnd_ssi_probe_done; 1200 } 1201 1202 ssi = rsnd_ssi_get(priv, i); 1203 1204 snprintf(name, RSND_SSI_NAME_SIZE, "%s.%d", 1205 SSI_NAME, i); 1206 1207 clk = devm_clk_get(dev, name); 1208 if (IS_ERR(clk)) { 1209 ret = PTR_ERR(clk); 1210 of_node_put(np); 1211 goto rsnd_ssi_probe_done; 1212 } 1213 1214 if (of_get_property(np, "shared-pin", NULL)) 1215 rsnd_flags_set(ssi, RSND_SSI_CLK_PIN_SHARE); 1216 1217 if (of_get_property(np, "no-busif", NULL)) 1218 rsnd_flags_set(ssi, RSND_SSI_NO_BUSIF); 1219 1220 ssi->irq = irq_of_parse_and_map(np, 0); 1221 if (!ssi->irq) { 1222 ret = -EINVAL; 1223 of_node_put(np); 1224 goto rsnd_ssi_probe_done; 1225 } 1226 1227 if (of_property_read_bool(np, "pio-transfer")) 1228 ops = &rsnd_ssi_pio_ops; 1229 else 1230 ops = &rsnd_ssi_dma_ops; 1231 1232 ret = rsnd_mod_init(priv, rsnd_mod_get(ssi), ops, clk, 1233 RSND_MOD_SSI, i); 1234 if (ret) { 1235 of_node_put(np); 1236 goto rsnd_ssi_probe_done; 1237 } 1238 skip: 1239 i++; 1240 } 1241 1242 ret = 0; 1243 1244 rsnd_ssi_probe_done: 1245 of_node_put(node); 1246 1247 return ret; 1248 } 1249 1250 void rsnd_ssi_remove(struct rsnd_priv *priv) 1251 { 1252 struct rsnd_ssi *ssi; 1253 int i; 1254 1255 for_each_rsnd_ssi(ssi, priv, i) { 1256 rsnd_mod_quit(rsnd_mod_get(ssi)); 1257 } 1258 } 1259