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