1 /* 2 * Renesas R-Car SRC support 3 * 4 * Copyright (C) 2013 Renesas Solutions Corp. 5 * Kuninori Morimoto <kuninori.morimoto.gx@renesas.com> 6 * 7 * This program is free software; you can redistribute it and/or modify 8 * it under the terms of the GNU General Public License version 2 as 9 * published by the Free Software Foundation. 10 */ 11 #include "rsnd.h" 12 13 struct rsnd_src { 14 struct rsnd_src_platform_info *info; /* rcar_snd.h */ 15 struct rsnd_mod mod; 16 struct clk *clk; 17 }; 18 19 #define RSND_SRC_NAME_SIZE 16 20 21 /* 22 * ADINR 23 */ 24 #define OTBL_24 (0 << 16) 25 #define OTBL_22 (2 << 16) 26 #define OTBL_20 (4 << 16) 27 #define OTBL_18 (6 << 16) 28 #define OTBL_16 (8 << 16) 29 30 #define rsnd_src_mode_flags(p) ((p)->info->flags) 31 #define rsnd_src_convert_rate(p) ((p)->info->convert_rate) 32 #define rsnd_mod_to_src(_mod) \ 33 container_of((_mod), struct rsnd_src, mod) 34 #define rsnd_src_hpbif_is_enable(src) \ 35 (rsnd_src_mode_flags(src) & RSND_SCU_USE_HPBIF) 36 #define rsnd_src_dma_available(src) \ 37 rsnd_dma_available(rsnd_mod_to_dma(&(src)->mod)) 38 39 #define for_each_rsnd_src(pos, priv, i) \ 40 for ((i) = 0; \ 41 ((i) < rsnd_src_nr(priv)) && \ 42 ((pos) = (struct rsnd_src *)(priv)->src + i); \ 43 i++) 44 45 46 /* 47 * image of SRC (Sampling Rate Converter) 48 * 49 * 96kHz <-> +-----+ 48kHz +-----+ 48kHz +-------+ 50 * 48kHz <-> | SRC | <------> | SSI | <-----> | codec | 51 * 44.1kHz <-> +-----+ +-----+ +-------+ 52 * ... 53 * 54 */ 55 56 /* 57 * src.c is caring... 58 * 59 * Gen1 60 * 61 * [mem] -> [SRU] -> [SSI] 62 * |--------| 63 * 64 * Gen2 65 * 66 * [mem] -> [SRC] -> [SSIU] -> [SSI] 67 * |-----------------| 68 */ 69 70 /* 71 * How to use SRC bypass mode for debugging 72 * 73 * SRC has bypass mode, and it is useful for debugging. 74 * In Gen2 case, 75 * SRCm_MODE controls whether SRC is used or not 76 * SSI_MODE0 controls whether SSIU which receives SRC data 77 * is used or not. 78 * Both SRCm_MODE/SSI_MODE0 settings are needed if you use SRC, 79 * but SRC bypass mode needs SSI_MODE0 only. 80 * 81 * This driver request 82 * struct rsnd_src_platform_info { 83 * u32 flags; 84 * u32 convert_rate; 85 * } 86 * 87 * rsnd_src_hpbif_is_enable() will be true 88 * if flags had RSND_SRC_USE_HPBIF, 89 * and it controls whether SSIU is used or not. 90 * 91 * rsnd_src_convert_rate() indicates 92 * above convert_rate, and it controls 93 * whether SRC is used or not. 94 * 95 * ex) doesn't use SRC 96 * struct rsnd_src_platform_info info = { 97 * .flags = 0, 98 * .convert_rate = 0, 99 * }; 100 * 101 * ex) uses SRC 102 * struct rsnd_src_platform_info info = { 103 * .flags = RSND_SRC_USE_HPBIF, 104 * .convert_rate = 48000, 105 * }; 106 * 107 * ex) uses SRC bypass mode 108 * struct rsnd_src_platform_info info = { 109 * .flags = RSND_SRC_USE_HPBIF, 110 * .convert_rate = 0, 111 * }; 112 * 113 */ 114 115 /* 116 * Gen1/Gen2 common functions 117 */ 118 int rsnd_src_ssi_mode_init(struct rsnd_mod *ssi_mod, 119 struct rsnd_dai *rdai, 120 struct rsnd_dai_stream *io) 121 { 122 struct rsnd_priv *priv = rsnd_mod_to_priv(ssi_mod); 123 struct rsnd_mod *src_mod = rsnd_io_to_mod_src(io); 124 struct rcar_snd_info *info = rsnd_priv_to_info(priv); 125 int ssi_id = rsnd_mod_id(ssi_mod); 126 int has_src = 0; 127 128 /* 129 * SSI_MODE0 130 */ 131 if (info->dai_info) { 132 has_src = !!src_mod; 133 } else { 134 struct rsnd_src *src = rsnd_mod_to_src(src_mod); 135 has_src = rsnd_src_hpbif_is_enable(src); 136 } 137 138 rsnd_mod_bset(ssi_mod, SSI_MODE0, (1 << ssi_id), 139 has_src ? 0 : (1 << ssi_id)); 140 141 /* 142 * SSI_MODE1 143 */ 144 if (rsnd_ssi_is_pin_sharing(ssi_mod)) { 145 int shift = -1; 146 switch (ssi_id) { 147 case 1: 148 shift = 0; 149 break; 150 case 2: 151 shift = 2; 152 break; 153 case 4: 154 shift = 16; 155 break; 156 } 157 158 if (shift >= 0) 159 rsnd_mod_bset(ssi_mod, SSI_MODE1, 160 0x3 << shift, 161 rsnd_dai_is_clk_master(rdai) ? 162 0x2 << shift : 0x1 << shift); 163 } 164 165 return 0; 166 } 167 168 int rsnd_src_enable_ssi_irq(struct rsnd_mod *ssi_mod, 169 struct rsnd_dai *rdai, 170 struct rsnd_dai_stream *io) 171 { 172 struct rsnd_priv *priv = rsnd_mod_to_priv(ssi_mod); 173 174 /* enable PIO interrupt if Gen2 */ 175 if (rsnd_is_gen2(priv)) 176 rsnd_mod_write(ssi_mod, INT_ENABLE, 0x0f000000); 177 178 return 0; 179 } 180 181 unsigned int rsnd_src_get_ssi_rate(struct rsnd_priv *priv, 182 struct rsnd_dai_stream *io, 183 struct snd_pcm_runtime *runtime) 184 { 185 struct rsnd_mod *src_mod = rsnd_io_to_mod_src(io); 186 struct rsnd_src *src; 187 unsigned int rate = 0; 188 189 if (src_mod) { 190 src = rsnd_mod_to_src(src_mod); 191 192 /* 193 * return convert rate if SRC is used, 194 * otherwise, return runtime->rate as usual 195 */ 196 rate = rsnd_src_convert_rate(src); 197 } 198 199 if (!rate) 200 rate = runtime->rate; 201 202 return rate; 203 } 204 205 static int rsnd_src_set_convert_rate(struct rsnd_mod *mod, 206 struct rsnd_dai *rdai, 207 struct rsnd_dai_stream *io) 208 { 209 struct snd_pcm_runtime *runtime = rsnd_io_to_runtime(io); 210 struct rsnd_src *src = rsnd_mod_to_src(mod); 211 u32 convert_rate = rsnd_src_convert_rate(src); 212 u32 adinr = runtime->channels; 213 u32 fsrate = 0; 214 215 if (convert_rate) 216 fsrate = 0x0400000 / convert_rate * runtime->rate; 217 218 /* set/clear soft reset */ 219 rsnd_mod_write(mod, SRC_SWRSR, 0); 220 rsnd_mod_write(mod, SRC_SWRSR, 1); 221 222 /* 223 * Initialize the operation of the SRC internal circuits 224 * see rsnd_src_start() 225 */ 226 rsnd_mod_write(mod, SRC_SRCIR, 1); 227 228 /* Set channel number and output bit length */ 229 switch (runtime->sample_bits) { 230 case 16: 231 adinr |= OTBL_16; 232 break; 233 case 32: 234 adinr |= OTBL_24; 235 break; 236 default: 237 return -EIO; 238 } 239 rsnd_mod_write(mod, SRC_ADINR, adinr); 240 241 /* Enable the initial value of IFS */ 242 if (fsrate) { 243 rsnd_mod_write(mod, SRC_IFSCR, 1); 244 245 /* Set initial value of IFS */ 246 rsnd_mod_write(mod, SRC_IFSVR, fsrate); 247 } 248 249 /* use DMA transfer */ 250 rsnd_mod_write(mod, SRC_BUSIF_MODE, 1); 251 252 return 0; 253 } 254 255 static int rsnd_src_init(struct rsnd_mod *mod, 256 struct rsnd_dai *rdai, 257 struct rsnd_dai_stream *io) 258 { 259 struct rsnd_src *src = rsnd_mod_to_src(mod); 260 261 clk_enable(src->clk); 262 263 return 0; 264 } 265 266 static int rsnd_src_quit(struct rsnd_mod *mod, 267 struct rsnd_dai *rdai, 268 struct rsnd_dai_stream *io) 269 { 270 struct rsnd_src *src = rsnd_mod_to_src(mod); 271 272 clk_disable(src->clk); 273 274 return 0; 275 } 276 277 static int rsnd_src_start(struct rsnd_mod *mod, 278 struct rsnd_dai *rdai, 279 struct rsnd_dai_stream *io) 280 { 281 struct rsnd_src *src = rsnd_mod_to_src(mod); 282 283 /* 284 * Cancel the initialization and operate the SRC function 285 * see rsnd_src_set_convert_rate() 286 */ 287 rsnd_mod_write(mod, SRC_SRCIR, 0); 288 289 if (rsnd_src_convert_rate(src)) 290 rsnd_mod_write(mod, SRC_ROUTE_MODE0, 1); 291 292 return 0; 293 } 294 295 296 static int rsnd_src_stop(struct rsnd_mod *mod, 297 struct rsnd_dai *rdai, 298 struct rsnd_dai_stream *io) 299 { 300 struct rsnd_src *src = rsnd_mod_to_src(mod); 301 302 if (rsnd_src_convert_rate(src)) 303 rsnd_mod_write(mod, SRC_ROUTE_MODE0, 0); 304 305 return 0; 306 } 307 308 static struct rsnd_mod_ops rsnd_src_non_ops = { 309 .name = "src (non)", 310 }; 311 312 /* 313 * Gen1 functions 314 */ 315 static int rsnd_src_set_route_gen1(struct rsnd_mod *mod, 316 struct rsnd_dai *rdai, 317 struct rsnd_dai_stream *io) 318 { 319 struct src_route_config { 320 u32 mask; 321 int shift; 322 } routes[] = { 323 { 0xF, 0, }, /* 0 */ 324 { 0xF, 4, }, /* 1 */ 325 { 0xF, 8, }, /* 2 */ 326 { 0x7, 12, }, /* 3 */ 327 { 0x7, 16, }, /* 4 */ 328 { 0x7, 20, }, /* 5 */ 329 { 0x7, 24, }, /* 6 */ 330 { 0x3, 28, }, /* 7 */ 331 { 0x3, 30, }, /* 8 */ 332 }; 333 u32 mask; 334 u32 val; 335 int id; 336 337 id = rsnd_mod_id(mod); 338 if (id < 0 || id >= ARRAY_SIZE(routes)) 339 return -EIO; 340 341 /* 342 * SRC_ROUTE_SELECT 343 */ 344 val = rsnd_dai_is_play(rdai, io) ? 0x1 : 0x2; 345 val = val << routes[id].shift; 346 mask = routes[id].mask << routes[id].shift; 347 348 rsnd_mod_bset(mod, SRC_ROUTE_SEL, mask, val); 349 350 return 0; 351 } 352 353 static int rsnd_src_set_convert_timing_gen1(struct rsnd_mod *mod, 354 struct rsnd_dai *rdai, 355 struct rsnd_dai_stream *io) 356 { 357 struct rsnd_priv *priv = rsnd_mod_to_priv(mod); 358 struct rsnd_src *src = rsnd_mod_to_src(mod); 359 struct snd_pcm_runtime *runtime = rsnd_io_to_runtime(io); 360 u32 convert_rate = rsnd_src_convert_rate(src); 361 u32 mask; 362 u32 val; 363 int shift; 364 int id = rsnd_mod_id(mod); 365 int ret; 366 367 /* 368 * SRC_TIMING_SELECT 369 */ 370 shift = (id % 4) * 8; 371 mask = 0x1F << shift; 372 373 /* 374 * ADG is used as source clock if SRC was used, 375 * then, SSI WS is used as destination clock. 376 * SSI WS is used as source clock if SRC is not used 377 * (when playback, source/destination become reverse when capture) 378 */ 379 ret = 0; 380 if (convert_rate) { 381 /* use ADG */ 382 val = 0; 383 ret = rsnd_adg_set_convert_clk_gen1(priv, mod, 384 runtime->rate, 385 convert_rate); 386 } else if (8 == id) { 387 /* use SSI WS, but SRU8 is special */ 388 val = id << shift; 389 } else { 390 /* use SSI WS */ 391 val = (id + 1) << shift; 392 } 393 394 if (ret < 0) 395 return ret; 396 397 switch (id / 4) { 398 case 0: 399 rsnd_mod_bset(mod, SRC_TMG_SEL0, mask, val); 400 break; 401 case 1: 402 rsnd_mod_bset(mod, SRC_TMG_SEL1, mask, val); 403 break; 404 case 2: 405 rsnd_mod_bset(mod, SRC_TMG_SEL2, mask, val); 406 break; 407 } 408 409 return 0; 410 } 411 412 static int rsnd_src_set_convert_rate_gen1(struct rsnd_mod *mod, 413 struct rsnd_dai *rdai, 414 struct rsnd_dai_stream *io) 415 { 416 int ret; 417 418 ret = rsnd_src_set_convert_rate(mod, rdai, io); 419 if (ret < 0) 420 return ret; 421 422 /* Select SRC mode (fixed value) */ 423 rsnd_mod_write(mod, SRC_SRCCR, 0x00010110); 424 425 /* Set the restriction value of the FS ratio (98%) */ 426 rsnd_mod_write(mod, SRC_MNFSR, 427 rsnd_mod_read(mod, SRC_IFSVR) / 100 * 98); 428 429 /* no SRC_BFSSR settings, since SRC_SRCCR::BUFMD is 0 */ 430 431 return 0; 432 } 433 434 static int rsnd_src_init_gen1(struct rsnd_mod *mod, 435 struct rsnd_dai *rdai, 436 struct rsnd_dai_stream *io) 437 { 438 int ret; 439 440 ret = rsnd_src_init(mod, rdai, io); 441 if (ret < 0) 442 return ret; 443 444 ret = rsnd_src_set_route_gen1(mod, rdai, io); 445 if (ret < 0) 446 return ret; 447 448 ret = rsnd_src_set_convert_rate_gen1(mod, rdai, io); 449 if (ret < 0) 450 return ret; 451 452 ret = rsnd_src_set_convert_timing_gen1(mod, rdai, io); 453 if (ret < 0) 454 return ret; 455 456 return 0; 457 } 458 459 static int rsnd_src_start_gen1(struct rsnd_mod *mod, 460 struct rsnd_dai *rdai, 461 struct rsnd_dai_stream *io) 462 { 463 int id = rsnd_mod_id(mod); 464 465 rsnd_mod_bset(mod, SRC_ROUTE_CTRL, (1 << id), (1 << id)); 466 467 return rsnd_src_start(mod, rdai, io); 468 } 469 470 static int rsnd_src_stop_gen1(struct rsnd_mod *mod, 471 struct rsnd_dai *rdai, 472 struct rsnd_dai_stream *io) 473 { 474 int id = rsnd_mod_id(mod); 475 476 rsnd_mod_bset(mod, SRC_ROUTE_CTRL, (1 << id), 0); 477 478 return rsnd_src_stop(mod, rdai, io); 479 } 480 481 static struct rsnd_mod_ops rsnd_src_gen1_ops = { 482 .name = "sru (gen1)", 483 .init = rsnd_src_init_gen1, 484 .quit = rsnd_src_quit, 485 .start = rsnd_src_start_gen1, 486 .stop = rsnd_src_stop_gen1, 487 }; 488 489 /* 490 * Gen2 functions 491 */ 492 static int rsnd_src_set_convert_rate_gen2(struct rsnd_mod *mod, 493 struct rsnd_dai *rdai, 494 struct rsnd_dai_stream *io) 495 { 496 int ret; 497 498 ret = rsnd_src_set_convert_rate(mod, rdai, io); 499 if (ret < 0) 500 return ret; 501 502 rsnd_mod_write(mod, SSI_BUSIF_ADINR, rsnd_mod_read(mod, SRC_ADINR)); 503 rsnd_mod_write(mod, SSI_BUSIF_MODE, rsnd_mod_read(mod, SRC_BUSIF_MODE)); 504 505 rsnd_mod_write(mod, SRC_SRCCR, 0x00011110); 506 507 rsnd_mod_write(mod, SRC_BSDSR, 0x01800000); 508 rsnd_mod_write(mod, SRC_BSISR, 0x00100060); 509 510 return 0; 511 } 512 513 static int rsnd_src_set_convert_timing_gen2(struct rsnd_mod *mod, 514 struct rsnd_dai *rdai, 515 struct rsnd_dai_stream *io) 516 { 517 struct snd_pcm_runtime *runtime = rsnd_io_to_runtime(io); 518 struct rsnd_src *src = rsnd_mod_to_src(mod); 519 u32 convert_rate = rsnd_src_convert_rate(src); 520 int ret; 521 522 if (convert_rate) 523 ret = rsnd_adg_set_convert_clk_gen2(mod, rdai, io, 524 runtime->rate, 525 convert_rate); 526 else 527 ret = rsnd_adg_set_convert_timing_gen2(mod, rdai, io); 528 529 return ret; 530 } 531 532 static int rsnd_src_probe_gen2(struct rsnd_mod *mod, 533 struct rsnd_dai *rdai, 534 struct rsnd_dai_stream *io) 535 { 536 struct rsnd_priv *priv = rsnd_mod_to_priv(mod); 537 struct rcar_snd_info *info = rsnd_priv_to_info(priv); 538 struct rsnd_src *src = rsnd_mod_to_src(mod); 539 struct rsnd_mod *ssi = rsnd_ssi_mod_get(priv, rsnd_mod_id(mod)); 540 struct device *dev = rsnd_priv_to_dev(priv); 541 int ret; 542 int is_play; 543 544 if (info->dai_info) 545 is_play = rsnd_info_is_playback(priv, src); 546 else 547 is_play = rsnd_ssi_is_play(ssi); 548 549 ret = rsnd_dma_init(priv, 550 rsnd_mod_to_dma(mod), 551 is_play, 552 src->info->dma_id); 553 if (ret < 0) 554 dev_err(dev, "SRC DMA failed\n"); 555 556 return ret; 557 } 558 559 static int rsnd_src_remove_gen2(struct rsnd_mod *mod, 560 struct rsnd_dai *rdai, 561 struct rsnd_dai_stream *io) 562 { 563 rsnd_dma_quit(rsnd_mod_to_priv(mod), rsnd_mod_to_dma(mod)); 564 565 return 0; 566 } 567 568 static int rsnd_src_init_gen2(struct rsnd_mod *mod, 569 struct rsnd_dai *rdai, 570 struct rsnd_dai_stream *io) 571 { 572 int ret; 573 574 ret = rsnd_src_init(mod, rdai, io); 575 if (ret < 0) 576 return ret; 577 578 ret = rsnd_src_set_convert_rate_gen2(mod, rdai, io); 579 if (ret < 0) 580 return ret; 581 582 ret = rsnd_src_set_convert_timing_gen2(mod, rdai, io); 583 if (ret < 0) 584 return ret; 585 586 return 0; 587 } 588 589 static int rsnd_src_start_gen2(struct rsnd_mod *mod, 590 struct rsnd_dai *rdai, 591 struct rsnd_dai_stream *io) 592 { 593 struct rsnd_src *src = rsnd_mod_to_src(mod); 594 595 rsnd_dma_start(rsnd_mod_to_dma(&src->mod)); 596 597 rsnd_mod_write(mod, SSI_CTRL, 0x1); 598 rsnd_mod_write(mod, SRC_CTRL, 0x11); 599 600 return rsnd_src_start(mod, rdai, io); 601 } 602 603 static int rsnd_src_stop_gen2(struct rsnd_mod *mod, 604 struct rsnd_dai *rdai, 605 struct rsnd_dai_stream *io) 606 { 607 struct rsnd_src *src = rsnd_mod_to_src(mod); 608 609 rsnd_mod_write(mod, SSI_CTRL, 0); 610 rsnd_mod_write(mod, SRC_CTRL, 0); 611 612 rsnd_dma_stop(rsnd_mod_to_dma(&src->mod)); 613 614 return rsnd_src_stop(mod, rdai, io); 615 } 616 617 static struct rsnd_mod_ops rsnd_src_gen2_ops = { 618 .name = "src (gen2)", 619 .probe = rsnd_src_probe_gen2, 620 .remove = rsnd_src_remove_gen2, 621 .init = rsnd_src_init_gen2, 622 .quit = rsnd_src_quit, 623 .start = rsnd_src_start_gen2, 624 .stop = rsnd_src_stop_gen2, 625 }; 626 627 struct rsnd_mod *rsnd_src_mod_get(struct rsnd_priv *priv, int id) 628 { 629 if (WARN_ON(id < 0 || id >= rsnd_src_nr(priv))) 630 id = 0; 631 632 return &((struct rsnd_src *)(priv->src) + id)->mod; 633 } 634 635 static void rsnd_of_parse_src(struct platform_device *pdev, 636 const struct rsnd_of_data *of_data, 637 struct rsnd_priv *priv) 638 { 639 struct device_node *src_node; 640 struct rcar_snd_info *info = rsnd_priv_to_info(priv); 641 struct rsnd_src_platform_info *src_info; 642 struct device *dev = &pdev->dev; 643 int nr; 644 645 if (!of_data) 646 return; 647 648 src_node = of_get_child_by_name(dev->of_node, "rcar_sound,src"); 649 if (!src_node) 650 return; 651 652 nr = of_get_child_count(src_node); 653 if (!nr) 654 return; 655 656 src_info = devm_kzalloc(dev, 657 sizeof(struct rsnd_src_platform_info) * nr, 658 GFP_KERNEL); 659 if (!src_info) { 660 dev_err(dev, "src info allocation error\n"); 661 return; 662 } 663 664 info->src_info = src_info; 665 info->src_info_nr = nr; 666 } 667 668 int rsnd_src_probe(struct platform_device *pdev, 669 const struct rsnd_of_data *of_data, 670 struct rsnd_priv *priv) 671 { 672 struct rcar_snd_info *info = rsnd_priv_to_info(priv); 673 struct device *dev = rsnd_priv_to_dev(priv); 674 struct rsnd_src *src; 675 struct rsnd_mod_ops *ops; 676 struct clk *clk; 677 char name[RSND_SRC_NAME_SIZE]; 678 int i, nr; 679 680 rsnd_of_parse_src(pdev, of_data, priv); 681 682 /* 683 * init SRC 684 */ 685 nr = info->src_info_nr; 686 if (!nr) 687 return 0; 688 689 src = devm_kzalloc(dev, sizeof(*src) * nr, GFP_KERNEL); 690 if (!src) { 691 dev_err(dev, "SRC allocate failed\n"); 692 return -ENOMEM; 693 } 694 695 priv->src_nr = nr; 696 priv->src = src; 697 698 for_each_rsnd_src(src, priv, i) { 699 snprintf(name, RSND_SRC_NAME_SIZE, "src.%d", i); 700 701 clk = devm_clk_get(dev, name); 702 if (IS_ERR(clk)) { 703 snprintf(name, RSND_SRC_NAME_SIZE, "scu.%d", i); 704 clk = devm_clk_get(dev, name); 705 } 706 707 if (IS_ERR(clk)) 708 return PTR_ERR(clk); 709 710 src->info = &info->src_info[i]; 711 src->clk = clk; 712 713 ops = &rsnd_src_non_ops; 714 if (rsnd_src_hpbif_is_enable(src)) { 715 if (rsnd_is_gen1(priv)) 716 ops = &rsnd_src_gen1_ops; 717 if (rsnd_is_gen2(priv)) 718 ops = &rsnd_src_gen2_ops; 719 } 720 721 rsnd_mod_init(priv, &src->mod, ops, RSND_MOD_SRC, i); 722 723 dev_dbg(dev, "SRC%d probed\n", i); 724 } 725 726 return 0; 727 } 728