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