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