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