1 // SPDX-License-Identifier: GPL-2.0 2 // 3 // Renesas R-Car SRU/SCU/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 * Renesas R-Car sound device structure 13 * 14 * Gen1 15 * 16 * SRU : Sound Routing Unit 17 * - SRC : Sampling Rate Converter 18 * - CMD 19 * - CTU : Channel Count Conversion Unit 20 * - MIX : Mixer 21 * - DVC : Digital Volume and Mute Function 22 * - SSI : Serial Sound Interface 23 * 24 * Gen2 25 * 26 * SCU : Sampling Rate Converter Unit 27 * - SRC : Sampling Rate Converter 28 * - CMD 29 * - CTU : Channel Count Conversion Unit 30 * - MIX : Mixer 31 * - DVC : Digital Volume and Mute Function 32 * SSIU : Serial Sound Interface Unit 33 * - SSI : Serial Sound Interface 34 */ 35 36 /* 37 * driver data Image 38 * 39 * rsnd_priv 40 * | 41 * | ** this depends on Gen1/Gen2 42 * | 43 * +- gen 44 * | 45 * | ** these depend on data path 46 * | ** gen and platform data control it 47 * | 48 * +- rdai[0] 49 * | | sru ssiu ssi 50 * | +- playback -> [mod] -> [mod] -> [mod] -> ... 51 * | | 52 * | | sru ssiu ssi 53 * | +- capture -> [mod] -> [mod] -> [mod] -> ... 54 * | 55 * +- rdai[1] 56 * | | sru ssiu ssi 57 * | +- playback -> [mod] -> [mod] -> [mod] -> ... 58 * | | 59 * | | sru ssiu ssi 60 * | +- capture -> [mod] -> [mod] -> [mod] -> ... 61 * ... 62 * | 63 * | ** these control ssi 64 * | 65 * +- ssi 66 * | | 67 * | +- ssi[0] 68 * | +- ssi[1] 69 * | +- ssi[2] 70 * | ... 71 * | 72 * | ** these control src 73 * | 74 * +- src 75 * | 76 * +- src[0] 77 * +- src[1] 78 * +- src[2] 79 * ... 80 * 81 * 82 * for_each_rsnd_dai(xx, priv, xx) 83 * rdai[0] => rdai[1] => rdai[2] => ... 84 * 85 * for_each_rsnd_mod(xx, rdai, xx) 86 * [mod] => [mod] => [mod] => ... 87 * 88 * rsnd_dai_call(xxx, fn ) 89 * [mod]->fn() -> [mod]->fn() -> [mod]->fn()... 90 * 91 */ 92 93 /* 94 * you can enable below define if you don't need 95 * DAI status debug message when debugging 96 * see rsnd_dbg_dai_call() 97 * 98 * #define RSND_DEBUG_NO_DAI_CALL 1 99 */ 100 101 #include <linux/pm_runtime.h> 102 #include "rsnd.h" 103 104 #define RSND_RATES SNDRV_PCM_RATE_8000_192000 105 #define RSND_FMTS (SNDRV_PCM_FMTBIT_S8 |\ 106 SNDRV_PCM_FMTBIT_S16_LE |\ 107 SNDRV_PCM_FMTBIT_S24_LE) 108 109 static const struct of_device_id rsnd_of_match[] = { 110 { .compatible = "renesas,rcar_sound-gen1", .data = (void *)RSND_GEN1 }, 111 { .compatible = "renesas,rcar_sound-gen2", .data = (void *)RSND_GEN2 }, 112 { .compatible = "renesas,rcar_sound-gen3", .data = (void *)RSND_GEN3 }, 113 {}, 114 }; 115 MODULE_DEVICE_TABLE(of, rsnd_of_match); 116 117 /* 118 * rsnd_mod functions 119 */ 120 void rsnd_mod_make_sure(struct rsnd_mod *mod, enum rsnd_mod_type type) 121 { 122 if (mod->type != type) { 123 struct rsnd_priv *priv = rsnd_mod_to_priv(mod); 124 struct device *dev = rsnd_priv_to_dev(priv); 125 126 dev_warn(dev, "%s[%d] is not your expected module\n", 127 rsnd_mod_name(mod), rsnd_mod_id(mod)); 128 } 129 } 130 131 struct dma_chan *rsnd_mod_dma_req(struct rsnd_dai_stream *io, 132 struct rsnd_mod *mod) 133 { 134 if (!mod || !mod->ops || !mod->ops->dma_req) 135 return NULL; 136 137 return mod->ops->dma_req(io, mod); 138 } 139 140 u32 *rsnd_mod_get_status(struct rsnd_dai_stream *io, 141 struct rsnd_mod *mod, 142 enum rsnd_mod_type type) 143 { 144 return &mod->status; 145 } 146 147 int rsnd_mod_init(struct rsnd_priv *priv, 148 struct rsnd_mod *mod, 149 struct rsnd_mod_ops *ops, 150 struct clk *clk, 151 u32* (*get_status)(struct rsnd_dai_stream *io, 152 struct rsnd_mod *mod, 153 enum rsnd_mod_type type), 154 enum rsnd_mod_type type, 155 int id) 156 { 157 int ret = clk_prepare(clk); 158 159 if (ret) 160 return ret; 161 162 mod->id = id; 163 mod->ops = ops; 164 mod->type = type; 165 mod->clk = clk; 166 mod->priv = priv; 167 mod->get_status = get_status; 168 169 return ret; 170 } 171 172 void rsnd_mod_quit(struct rsnd_mod *mod) 173 { 174 clk_unprepare(mod->clk); 175 mod->clk = NULL; 176 } 177 178 void rsnd_mod_interrupt(struct rsnd_mod *mod, 179 void (*callback)(struct rsnd_mod *mod, 180 struct rsnd_dai_stream *io)) 181 { 182 struct rsnd_priv *priv = rsnd_mod_to_priv(mod); 183 struct rsnd_dai_stream *io; 184 struct rsnd_dai *rdai; 185 int i; 186 187 for_each_rsnd_dai(rdai, priv, i) { 188 io = &rdai->playback; 189 if (mod == io->mod[mod->type]) 190 callback(mod, io); 191 192 io = &rdai->capture; 193 if (mod == io->mod[mod->type]) 194 callback(mod, io); 195 } 196 } 197 198 int rsnd_io_is_working(struct rsnd_dai_stream *io) 199 { 200 /* see rsnd_dai_stream_init/quit() */ 201 if (io->substream) 202 return snd_pcm_running(io->substream); 203 204 return 0; 205 } 206 207 int rsnd_runtime_channel_original_with_params(struct rsnd_dai_stream *io, 208 struct snd_pcm_hw_params *params) 209 { 210 struct snd_pcm_runtime *runtime = rsnd_io_to_runtime(io); 211 212 /* 213 * params will be added when refine 214 * see 215 * __rsnd_soc_hw_rule_rate() 216 * __rsnd_soc_hw_rule_channels() 217 */ 218 if (params) 219 return params_channels(params); 220 else 221 return runtime->channels; 222 } 223 224 int rsnd_runtime_channel_after_ctu_with_params(struct rsnd_dai_stream *io, 225 struct snd_pcm_hw_params *params) 226 { 227 int chan = rsnd_runtime_channel_original_with_params(io, params); 228 struct rsnd_mod *ctu_mod = rsnd_io_to_mod_ctu(io); 229 230 if (ctu_mod) { 231 u32 converted_chan = rsnd_ctu_converted_channel(ctu_mod); 232 233 if (converted_chan) 234 return converted_chan; 235 } 236 237 return chan; 238 } 239 240 int rsnd_runtime_channel_for_ssi_with_params(struct rsnd_dai_stream *io, 241 struct snd_pcm_hw_params *params) 242 { 243 struct rsnd_dai *rdai = rsnd_io_to_rdai(io); 244 int chan = rsnd_io_is_play(io) ? 245 rsnd_runtime_channel_after_ctu_with_params(io, params) : 246 rsnd_runtime_channel_original_with_params(io, params); 247 248 /* Use Multi SSI */ 249 if (rsnd_runtime_is_ssi_multi(io)) 250 chan /= rsnd_rdai_ssi_lane_get(rdai); 251 252 /* TDM Extend Mode needs 8ch */ 253 if (chan == 6) 254 chan = 8; 255 256 return chan; 257 } 258 259 int rsnd_runtime_is_ssi_multi(struct rsnd_dai_stream *io) 260 { 261 struct rsnd_dai *rdai = rsnd_io_to_rdai(io); 262 int lane = rsnd_rdai_ssi_lane_get(rdai); 263 int chan = rsnd_io_is_play(io) ? 264 rsnd_runtime_channel_after_ctu(io) : 265 rsnd_runtime_channel_original(io); 266 267 return (chan > 2) && (lane > 1); 268 } 269 270 int rsnd_runtime_is_ssi_tdm(struct rsnd_dai_stream *io) 271 { 272 return rsnd_runtime_channel_for_ssi(io) >= 6; 273 } 274 275 /* 276 * ADINR function 277 */ 278 u32 rsnd_get_adinr_bit(struct rsnd_mod *mod, struct rsnd_dai_stream *io) 279 { 280 struct rsnd_priv *priv = rsnd_mod_to_priv(mod); 281 struct snd_pcm_runtime *runtime = rsnd_io_to_runtime(io); 282 struct device *dev = rsnd_priv_to_dev(priv); 283 284 switch (snd_pcm_format_width(runtime->format)) { 285 case 8: 286 return 16 << 16; 287 case 16: 288 return 8 << 16; 289 case 24: 290 return 0 << 16; 291 } 292 293 dev_warn(dev, "not supported sample bits\n"); 294 295 return 0; 296 } 297 298 /* 299 * DALIGN function 300 */ 301 u32 rsnd_get_dalign(struct rsnd_mod *mod, struct rsnd_dai_stream *io) 302 { 303 struct rsnd_mod *ssiu = rsnd_io_to_mod_ssiu(io); 304 struct rsnd_mod *target; 305 struct snd_pcm_runtime *runtime = rsnd_io_to_runtime(io); 306 307 /* 308 * *Hardware* L/R and *Software* L/R are inverted for 16bit data. 309 * 31..16 15...0 310 * HW: [L ch] [R ch] 311 * SW: [R ch] [L ch] 312 * We need to care about inversion timing to control 313 * Playback/Capture correctly. 314 * The point is [DVC] needs *Hardware* L/R, [MEM] needs *Software* L/R 315 * 316 * sL/R : software L/R 317 * hL/R : hardware L/R 318 * (*) : conversion timing 319 * 320 * Playback 321 * sL/R (*) hL/R hL/R hL/R hL/R hL/R 322 * [MEM] -> [SRC] -> [DVC] -> [CMD] -> [SSIU] -> [SSI] -> codec 323 * 324 * Capture 325 * hL/R hL/R hL/R hL/R hL/R (*) sL/R 326 * codec -> [SSI] -> [SSIU] -> [SRC] -> [DVC] -> [CMD] -> [MEM] 327 */ 328 if (rsnd_io_is_play(io)) { 329 struct rsnd_mod *src = rsnd_io_to_mod_src(io); 330 331 target = src ? src : ssiu; 332 } else { 333 struct rsnd_mod *cmd = rsnd_io_to_mod_cmd(io); 334 335 target = cmd ? cmd : ssiu; 336 } 337 338 /* Non target mod or non 16bit needs normal DALIGN */ 339 if ((snd_pcm_format_width(runtime->format) != 16) || 340 (mod != target)) 341 return 0x76543210; 342 /* Target mod needs inverted DALIGN when 16bit */ 343 else 344 return 0x67452301; 345 } 346 347 u32 rsnd_get_busif_shift(struct rsnd_dai_stream *io, struct rsnd_mod *mod) 348 { 349 enum rsnd_mod_type playback_mods[] = { 350 RSND_MOD_SRC, 351 RSND_MOD_CMD, 352 RSND_MOD_SSIU, 353 }; 354 enum rsnd_mod_type capture_mods[] = { 355 RSND_MOD_CMD, 356 RSND_MOD_SRC, 357 RSND_MOD_SSIU, 358 }; 359 struct snd_pcm_runtime *runtime = rsnd_io_to_runtime(io); 360 struct rsnd_mod *tmod = NULL; 361 enum rsnd_mod_type *mods = 362 rsnd_io_is_play(io) ? 363 playback_mods : capture_mods; 364 int i; 365 366 /* 367 * This is needed for 24bit data 368 * We need to shift 8bit 369 * 370 * Linux 24bit data is located as 0x00****** 371 * HW 24bit data is located as 0x******00 372 * 373 */ 374 if (snd_pcm_format_width(runtime->format) != 24) 375 return 0; 376 377 for (i = 0; i < ARRAY_SIZE(playback_mods); i++) { 378 tmod = rsnd_io_to_mod(io, mods[i]); 379 if (tmod) 380 break; 381 } 382 383 if (tmod != mod) 384 return 0; 385 386 if (rsnd_io_is_play(io)) 387 return (0 << 20) | /* shift to Left */ 388 (8 << 16); /* 8bit */ 389 else 390 return (1 << 20) | /* shift to Right */ 391 (8 << 16); /* 8bit */ 392 } 393 394 /* 395 * rsnd_dai functions 396 */ 397 struct rsnd_mod *rsnd_mod_next(int *iterator, 398 struct rsnd_dai_stream *io, 399 enum rsnd_mod_type *array, 400 int array_size) 401 { 402 struct rsnd_mod *mod; 403 enum rsnd_mod_type type; 404 int max = array ? array_size : RSND_MOD_MAX; 405 406 for (; *iterator < max; (*iterator)++) { 407 type = (array) ? array[*iterator] : *iterator; 408 mod = rsnd_io_to_mod(io, type); 409 if (mod) 410 return mod; 411 } 412 413 return NULL; 414 } 415 416 static enum rsnd_mod_type rsnd_mod_sequence[][RSND_MOD_MAX] = { 417 { 418 /* CAPTURE */ 419 RSND_MOD_AUDMAPP, 420 RSND_MOD_AUDMA, 421 RSND_MOD_DVC, 422 RSND_MOD_MIX, 423 RSND_MOD_CTU, 424 RSND_MOD_CMD, 425 RSND_MOD_SRC, 426 RSND_MOD_SSIU, 427 RSND_MOD_SSIM3, 428 RSND_MOD_SSIM2, 429 RSND_MOD_SSIM1, 430 RSND_MOD_SSIP, 431 RSND_MOD_SSI, 432 }, { 433 /* PLAYBACK */ 434 RSND_MOD_AUDMAPP, 435 RSND_MOD_AUDMA, 436 RSND_MOD_SSIM3, 437 RSND_MOD_SSIM2, 438 RSND_MOD_SSIM1, 439 RSND_MOD_SSIP, 440 RSND_MOD_SSI, 441 RSND_MOD_SSIU, 442 RSND_MOD_DVC, 443 RSND_MOD_MIX, 444 RSND_MOD_CTU, 445 RSND_MOD_CMD, 446 RSND_MOD_SRC, 447 }, 448 }; 449 450 static int rsnd_status_update(u32 *status, 451 int shift, int add, int timing) 452 { 453 u32 mask = 0xF << shift; 454 u8 val = (*status >> shift) & 0xF; 455 u8 next_val = (val + add) & 0xF; 456 int func_call = (val == timing); 457 458 if (next_val == 0xF) /* underflow case */ 459 func_call = 0; 460 else 461 *status = (*status & ~mask) + (next_val << shift); 462 463 return func_call; 464 } 465 466 #define rsnd_dai_call(fn, io, param...) \ 467 ({ \ 468 struct device *dev = rsnd_priv_to_dev(rsnd_io_to_priv(io)); \ 469 struct rsnd_mod *mod; \ 470 int is_play = rsnd_io_is_play(io); \ 471 int ret = 0, i; \ 472 enum rsnd_mod_type *types = rsnd_mod_sequence[is_play]; \ 473 for_each_rsnd_mod_arrays(i, mod, io, types, RSND_MOD_MAX) { \ 474 int tmp = 0; \ 475 u32 *status = mod->get_status(io, mod, types[i]); \ 476 int func_call = rsnd_status_update(status, \ 477 __rsnd_mod_shift_##fn, \ 478 __rsnd_mod_add_##fn, \ 479 __rsnd_mod_call_##fn); \ 480 rsnd_dbg_dai_call(dev, "%s[%d]\t0x%08x %s\n", \ 481 rsnd_mod_name(mod), rsnd_mod_id(mod), *status, \ 482 (func_call && (mod)->ops->fn) ? #fn : ""); \ 483 if (func_call && (mod)->ops->fn) \ 484 tmp = (mod)->ops->fn(mod, io, param); \ 485 if (tmp && (tmp != -EPROBE_DEFER)) \ 486 dev_err(dev, "%s[%d] : %s error %d\n", \ 487 rsnd_mod_name(mod), rsnd_mod_id(mod), \ 488 #fn, tmp); \ 489 ret |= tmp; \ 490 } \ 491 ret; \ 492 }) 493 494 int rsnd_dai_connect(struct rsnd_mod *mod, 495 struct rsnd_dai_stream *io, 496 enum rsnd_mod_type type) 497 { 498 struct rsnd_priv *priv; 499 struct device *dev; 500 501 if (!mod) 502 return -EIO; 503 504 if (io->mod[type] == mod) 505 return 0; 506 507 if (io->mod[type]) 508 return -EINVAL; 509 510 priv = rsnd_mod_to_priv(mod); 511 dev = rsnd_priv_to_dev(priv); 512 513 io->mod[type] = mod; 514 515 dev_dbg(dev, "%s[%d] is connected to io (%s)\n", 516 rsnd_mod_name(mod), rsnd_mod_id(mod), 517 rsnd_io_is_play(io) ? "Playback" : "Capture"); 518 519 return 0; 520 } 521 522 static void rsnd_dai_disconnect(struct rsnd_mod *mod, 523 struct rsnd_dai_stream *io, 524 enum rsnd_mod_type type) 525 { 526 io->mod[type] = NULL; 527 } 528 529 int rsnd_rdai_channels_ctrl(struct rsnd_dai *rdai, 530 int max_channels) 531 { 532 if (max_channels > 0) 533 rdai->max_channels = max_channels; 534 535 return rdai->max_channels; 536 } 537 538 int rsnd_rdai_ssi_lane_ctrl(struct rsnd_dai *rdai, 539 int ssi_lane) 540 { 541 if (ssi_lane > 0) 542 rdai->ssi_lane = ssi_lane; 543 544 return rdai->ssi_lane; 545 } 546 547 int rsnd_rdai_width_ctrl(struct rsnd_dai *rdai, int width) 548 { 549 if (width > 0) 550 rdai->chan_width = width; 551 552 return rdai->chan_width; 553 } 554 555 struct rsnd_dai *rsnd_rdai_get(struct rsnd_priv *priv, int id) 556 { 557 if ((id < 0) || (id >= rsnd_rdai_nr(priv))) 558 return NULL; 559 560 return priv->rdai + id; 561 } 562 563 static struct snd_soc_dai_driver 564 *rsnd_daidrv_get(struct rsnd_priv *priv, int id) 565 { 566 if ((id < 0) || (id >= rsnd_rdai_nr(priv))) 567 return NULL; 568 569 return priv->daidrv + id; 570 } 571 572 #define rsnd_dai_to_priv(dai) snd_soc_dai_get_drvdata(dai) 573 static struct rsnd_dai *rsnd_dai_to_rdai(struct snd_soc_dai *dai) 574 { 575 struct rsnd_priv *priv = rsnd_dai_to_priv(dai); 576 577 return rsnd_rdai_get(priv, dai->id); 578 } 579 580 /* 581 * rsnd_soc_dai functions 582 */ 583 void rsnd_dai_period_elapsed(struct rsnd_dai_stream *io) 584 { 585 struct snd_pcm_substream *substream = io->substream; 586 587 /* 588 * this function should be called... 589 * 590 * - if rsnd_dai_pointer_update() returns true 591 * - without spin lock 592 */ 593 594 snd_pcm_period_elapsed(substream); 595 } 596 597 static void rsnd_dai_stream_init(struct rsnd_dai_stream *io, 598 struct snd_pcm_substream *substream) 599 { 600 io->substream = substream; 601 } 602 603 static void rsnd_dai_stream_quit(struct rsnd_dai_stream *io) 604 { 605 io->substream = NULL; 606 } 607 608 static 609 struct snd_soc_dai *rsnd_substream_to_dai(struct snd_pcm_substream *substream) 610 { 611 struct snd_soc_pcm_runtime *rtd = substream->private_data; 612 613 return rtd->cpu_dai; 614 } 615 616 static 617 struct rsnd_dai_stream *rsnd_rdai_to_io(struct rsnd_dai *rdai, 618 struct snd_pcm_substream *substream) 619 { 620 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) 621 return &rdai->playback; 622 else 623 return &rdai->capture; 624 } 625 626 static int rsnd_soc_dai_trigger(struct snd_pcm_substream *substream, int cmd, 627 struct snd_soc_dai *dai) 628 { 629 struct rsnd_priv *priv = rsnd_dai_to_priv(dai); 630 struct rsnd_dai *rdai = rsnd_dai_to_rdai(dai); 631 struct rsnd_dai_stream *io = rsnd_rdai_to_io(rdai, substream); 632 int ret; 633 unsigned long flags; 634 635 spin_lock_irqsave(&priv->lock, flags); 636 637 switch (cmd) { 638 case SNDRV_PCM_TRIGGER_START: 639 case SNDRV_PCM_TRIGGER_RESUME: 640 ret = rsnd_dai_call(init, io, priv); 641 if (ret < 0) 642 goto dai_trigger_end; 643 644 ret = rsnd_dai_call(start, io, priv); 645 if (ret < 0) 646 goto dai_trigger_end; 647 648 ret = rsnd_dai_call(irq, io, priv, 1); 649 if (ret < 0) 650 goto dai_trigger_end; 651 652 break; 653 case SNDRV_PCM_TRIGGER_STOP: 654 case SNDRV_PCM_TRIGGER_SUSPEND: 655 ret = rsnd_dai_call(irq, io, priv, 0); 656 657 ret |= rsnd_dai_call(stop, io, priv); 658 659 ret |= rsnd_dai_call(quit, io, priv); 660 661 break; 662 default: 663 ret = -EINVAL; 664 } 665 666 dai_trigger_end: 667 spin_unlock_irqrestore(&priv->lock, flags); 668 669 return ret; 670 } 671 672 static int rsnd_soc_dai_set_fmt(struct snd_soc_dai *dai, unsigned int fmt) 673 { 674 struct rsnd_dai *rdai = rsnd_dai_to_rdai(dai); 675 676 /* set master/slave audio interface */ 677 switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) { 678 case SND_SOC_DAIFMT_CBM_CFM: 679 rdai->clk_master = 0; 680 break; 681 case SND_SOC_DAIFMT_CBS_CFS: 682 rdai->clk_master = 1; /* codec is slave, cpu is master */ 683 break; 684 default: 685 return -EINVAL; 686 } 687 688 /* set format */ 689 switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) { 690 case SND_SOC_DAIFMT_I2S: 691 rdai->sys_delay = 0; 692 rdai->data_alignment = 0; 693 rdai->frm_clk_inv = 0; 694 break; 695 case SND_SOC_DAIFMT_LEFT_J: 696 case SND_SOC_DAIFMT_DSP_B: 697 rdai->sys_delay = 1; 698 rdai->data_alignment = 0; 699 rdai->frm_clk_inv = 1; 700 break; 701 case SND_SOC_DAIFMT_RIGHT_J: 702 rdai->sys_delay = 1; 703 rdai->data_alignment = 1; 704 rdai->frm_clk_inv = 1; 705 break; 706 case SND_SOC_DAIFMT_DSP_A: 707 rdai->sys_delay = 0; 708 rdai->data_alignment = 0; 709 rdai->frm_clk_inv = 1; 710 break; 711 } 712 713 /* set clock inversion */ 714 switch (fmt & SND_SOC_DAIFMT_INV_MASK) { 715 case SND_SOC_DAIFMT_NB_IF: 716 rdai->frm_clk_inv = !rdai->frm_clk_inv; 717 break; 718 case SND_SOC_DAIFMT_IB_NF: 719 rdai->bit_clk_inv = !rdai->bit_clk_inv; 720 break; 721 case SND_SOC_DAIFMT_IB_IF: 722 rdai->bit_clk_inv = !rdai->bit_clk_inv; 723 rdai->frm_clk_inv = !rdai->frm_clk_inv; 724 break; 725 case SND_SOC_DAIFMT_NB_NF: 726 default: 727 break; 728 } 729 730 return 0; 731 } 732 733 static int rsnd_soc_set_dai_tdm_slot(struct snd_soc_dai *dai, 734 u32 tx_mask, u32 rx_mask, 735 int slots, int slot_width) 736 { 737 struct rsnd_priv *priv = rsnd_dai_to_priv(dai); 738 struct rsnd_dai *rdai = rsnd_dai_to_rdai(dai); 739 struct device *dev = rsnd_priv_to_dev(priv); 740 741 switch (slot_width) { 742 case 16: 743 case 24: 744 case 32: 745 break; 746 default: 747 /* use default */ 748 slot_width = 32; 749 } 750 751 switch (slots) { 752 case 2: 753 case 6: 754 case 8: 755 /* TDM Extend Mode */ 756 rsnd_rdai_channels_set(rdai, slots); 757 rsnd_rdai_ssi_lane_set(rdai, 1); 758 rsnd_rdai_width_set(rdai, slot_width); 759 break; 760 default: 761 dev_err(dev, "unsupported TDM slots (%d)\n", slots); 762 return -EINVAL; 763 } 764 765 return 0; 766 } 767 768 static unsigned int rsnd_soc_hw_channels_list[] = { 769 2, 6, 8, 770 }; 771 772 static unsigned int rsnd_soc_hw_rate_list[] = { 773 8000, 774 11025, 775 16000, 776 22050, 777 32000, 778 44100, 779 48000, 780 64000, 781 88200, 782 96000, 783 176400, 784 192000, 785 }; 786 787 static int rsnd_soc_hw_rule(struct rsnd_dai *rdai, 788 unsigned int *list, int list_num, 789 struct snd_interval *baseline, struct snd_interval *iv) 790 { 791 struct snd_interval p; 792 unsigned int rate; 793 int i; 794 795 snd_interval_any(&p); 796 p.min = UINT_MAX; 797 p.max = 0; 798 799 for (i = 0; i < list_num; i++) { 800 801 if (!snd_interval_test(iv, list[i])) 802 continue; 803 804 rate = rsnd_ssi_clk_query(rdai, 805 baseline->min, list[i], NULL); 806 if (rate > 0) { 807 p.min = min(p.min, list[i]); 808 p.max = max(p.max, list[i]); 809 } 810 811 rate = rsnd_ssi_clk_query(rdai, 812 baseline->max, list[i], NULL); 813 if (rate > 0) { 814 p.min = min(p.min, list[i]); 815 p.max = max(p.max, list[i]); 816 } 817 } 818 819 return snd_interval_refine(iv, &p); 820 } 821 822 static int rsnd_soc_hw_rule_rate(struct snd_pcm_hw_params *params, 823 struct snd_pcm_hw_rule *rule) 824 { 825 struct snd_interval *ic_ = hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS); 826 struct snd_interval *ir = hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE); 827 struct snd_interval ic; 828 struct rsnd_dai_stream *io = rule->private; 829 struct rsnd_dai *rdai = rsnd_io_to_rdai(io); 830 831 /* 832 * possible sampling rate limitation is same as 833 * 2ch if it supports multi ssi 834 * and same as 8ch if TDM 6ch (see rsnd_ssi_config_init()) 835 */ 836 ic = *ic_; 837 ic.min = 838 ic.max = rsnd_runtime_channel_for_ssi_with_params(io, params); 839 840 return rsnd_soc_hw_rule(rdai, rsnd_soc_hw_rate_list, 841 ARRAY_SIZE(rsnd_soc_hw_rate_list), 842 &ic, ir); 843 } 844 845 static int rsnd_soc_hw_rule_channels(struct snd_pcm_hw_params *params, 846 struct snd_pcm_hw_rule *rule) 847 { 848 struct snd_interval *ic_ = hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS); 849 struct snd_interval *ir = hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE); 850 struct snd_interval ic; 851 struct rsnd_dai_stream *io = rule->private; 852 struct rsnd_dai *rdai = rsnd_io_to_rdai(io); 853 854 /* 855 * possible sampling rate limitation is same as 856 * 2ch if it supports multi ssi 857 * and same as 8ch if TDM 6ch (see rsnd_ssi_config_init()) 858 */ 859 ic = *ic_; 860 ic.min = 861 ic.max = rsnd_runtime_channel_for_ssi_with_params(io, params); 862 863 return rsnd_soc_hw_rule(rdai, rsnd_soc_hw_channels_list, 864 ARRAY_SIZE(rsnd_soc_hw_channels_list), 865 ir, &ic); 866 } 867 868 static const struct snd_pcm_hardware rsnd_pcm_hardware = { 869 .info = SNDRV_PCM_INFO_INTERLEAVED | 870 SNDRV_PCM_INFO_MMAP | 871 SNDRV_PCM_INFO_MMAP_VALID, 872 .buffer_bytes_max = 64 * 1024, 873 .period_bytes_min = 32, 874 .period_bytes_max = 8192, 875 .periods_min = 1, 876 .periods_max = 32, 877 .fifo_size = 256, 878 }; 879 880 static int rsnd_soc_dai_startup(struct snd_pcm_substream *substream, 881 struct snd_soc_dai *dai) 882 { 883 struct rsnd_dai *rdai = rsnd_dai_to_rdai(dai); 884 struct rsnd_dai_stream *io = rsnd_rdai_to_io(rdai, substream); 885 struct snd_pcm_hw_constraint_list *constraint = &rdai->constraint; 886 struct snd_pcm_runtime *runtime = substream->runtime; 887 unsigned int max_channels = rsnd_rdai_channels_get(rdai); 888 int i; 889 890 rsnd_dai_stream_init(io, substream); 891 892 /* 893 * Channel Limitation 894 * It depends on Platform design 895 */ 896 constraint->list = rsnd_soc_hw_channels_list; 897 constraint->count = 0; 898 constraint->mask = 0; 899 900 for (i = 0; i < ARRAY_SIZE(rsnd_soc_hw_channels_list); i++) { 901 if (rsnd_soc_hw_channels_list[i] > max_channels) 902 break; 903 constraint->count = i + 1; 904 } 905 906 snd_soc_set_runtime_hwparams(substream, &rsnd_pcm_hardware); 907 908 snd_pcm_hw_constraint_list(runtime, 0, 909 SNDRV_PCM_HW_PARAM_CHANNELS, constraint); 910 911 snd_pcm_hw_constraint_integer(runtime, 912 SNDRV_PCM_HW_PARAM_PERIODS); 913 914 /* 915 * Sampling Rate / Channel Limitation 916 * It depends on Clock Master Mode 917 */ 918 if (rsnd_rdai_is_clk_master(rdai)) { 919 int is_play = substream->stream == SNDRV_PCM_STREAM_PLAYBACK; 920 921 snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_RATE, 922 rsnd_soc_hw_rule_rate, 923 is_play ? &rdai->playback : &rdai->capture, 924 SNDRV_PCM_HW_PARAM_CHANNELS, -1); 925 snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_CHANNELS, 926 rsnd_soc_hw_rule_channels, 927 is_play ? &rdai->playback : &rdai->capture, 928 SNDRV_PCM_HW_PARAM_RATE, -1); 929 } 930 931 return 0; 932 } 933 934 static void rsnd_soc_dai_shutdown(struct snd_pcm_substream *substream, 935 struct snd_soc_dai *dai) 936 { 937 struct rsnd_dai *rdai = rsnd_dai_to_rdai(dai); 938 struct rsnd_priv *priv = rsnd_rdai_to_priv(rdai); 939 struct rsnd_dai_stream *io = rsnd_rdai_to_io(rdai, substream); 940 941 /* 942 * call rsnd_dai_call without spinlock 943 */ 944 rsnd_dai_call(cleanup, io, priv); 945 946 rsnd_dai_stream_quit(io); 947 } 948 949 static int rsnd_soc_dai_prepare(struct snd_pcm_substream *substream, 950 struct snd_soc_dai *dai) 951 { 952 struct rsnd_priv *priv = rsnd_dai_to_priv(dai); 953 struct rsnd_dai *rdai = rsnd_dai_to_rdai(dai); 954 struct rsnd_dai_stream *io = rsnd_rdai_to_io(rdai, substream); 955 956 return rsnd_dai_call(prepare, io, priv); 957 } 958 959 static const struct snd_soc_dai_ops rsnd_soc_dai_ops = { 960 .startup = rsnd_soc_dai_startup, 961 .shutdown = rsnd_soc_dai_shutdown, 962 .trigger = rsnd_soc_dai_trigger, 963 .set_fmt = rsnd_soc_dai_set_fmt, 964 .set_tdm_slot = rsnd_soc_set_dai_tdm_slot, 965 .prepare = rsnd_soc_dai_prepare, 966 }; 967 968 void rsnd_parse_connect_common(struct rsnd_dai *rdai, 969 struct rsnd_mod* (*mod_get)(struct rsnd_priv *priv, int id), 970 struct device_node *node, 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 *np; 976 struct rsnd_mod *mod; 977 int i; 978 979 if (!node) 980 return; 981 982 i = 0; 983 for_each_child_of_node(node, np) { 984 mod = mod_get(priv, i); 985 if (np == playback) 986 rsnd_dai_connect(mod, &rdai->playback, mod->type); 987 if (np == capture) 988 rsnd_dai_connect(mod, &rdai->capture, mod->type); 989 i++; 990 } 991 992 of_node_put(node); 993 } 994 995 static struct device_node *rsnd_dai_of_node(struct rsnd_priv *priv, 996 int *is_graph) 997 { 998 struct device *dev = rsnd_priv_to_dev(priv); 999 struct device_node *np = dev->of_node; 1000 struct device_node *dai_node; 1001 struct device_node *ret; 1002 1003 *is_graph = 0; 1004 1005 /* 1006 * parse both previous dai (= rcar_sound,dai), and 1007 * graph dai (= ports/port) 1008 */ 1009 dai_node = of_get_child_by_name(np, RSND_NODE_DAI); 1010 if (dai_node) { 1011 ret = dai_node; 1012 goto of_node_compatible; 1013 } 1014 1015 ret = np; 1016 1017 dai_node = of_graph_get_next_endpoint(np, NULL); 1018 if (dai_node) 1019 goto of_node_graph; 1020 1021 return NULL; 1022 1023 of_node_graph: 1024 *is_graph = 1; 1025 of_node_compatible: 1026 of_node_put(dai_node); 1027 1028 return ret; 1029 } 1030 1031 static void __rsnd_dai_probe(struct rsnd_priv *priv, 1032 struct device_node *dai_np, 1033 int dai_i) 1034 { 1035 struct device_node *playback, *capture; 1036 struct rsnd_dai_stream *io_playback; 1037 struct rsnd_dai_stream *io_capture; 1038 struct snd_soc_dai_driver *drv; 1039 struct rsnd_dai *rdai; 1040 struct device *dev = rsnd_priv_to_dev(priv); 1041 int io_i; 1042 1043 rdai = rsnd_rdai_get(priv, dai_i); 1044 drv = rsnd_daidrv_get(priv, dai_i); 1045 io_playback = &rdai->playback; 1046 io_capture = &rdai->capture; 1047 1048 snprintf(rdai->name, RSND_DAI_NAME_SIZE, "rsnd-dai.%d", dai_i); 1049 1050 rdai->priv = priv; 1051 drv->name = rdai->name; 1052 drv->ops = &rsnd_soc_dai_ops; 1053 1054 snprintf(rdai->playback.name, RSND_DAI_NAME_SIZE, 1055 "DAI%d Playback", dai_i); 1056 drv->playback.rates = RSND_RATES; 1057 drv->playback.formats = RSND_FMTS; 1058 drv->playback.channels_min = 2; 1059 drv->playback.channels_max = 8; 1060 drv->playback.stream_name = rdai->playback.name; 1061 1062 snprintf(rdai->capture.name, RSND_DAI_NAME_SIZE, 1063 "DAI%d Capture", dai_i); 1064 drv->capture.rates = RSND_RATES; 1065 drv->capture.formats = RSND_FMTS; 1066 drv->capture.channels_min = 2; 1067 drv->capture.channels_max = 8; 1068 drv->capture.stream_name = rdai->capture.name; 1069 1070 rdai->playback.rdai = rdai; 1071 rdai->capture.rdai = rdai; 1072 rsnd_rdai_channels_set(rdai, 2); /* default 2ch */ 1073 rsnd_rdai_ssi_lane_set(rdai, 1); /* default 1lane */ 1074 rsnd_rdai_width_set(rdai, 32); /* default 32bit width */ 1075 1076 for (io_i = 0;; io_i++) { 1077 playback = of_parse_phandle(dai_np, "playback", io_i); 1078 capture = of_parse_phandle(dai_np, "capture", io_i); 1079 1080 if (!playback && !capture) 1081 break; 1082 1083 rsnd_parse_connect_ssi(rdai, playback, capture); 1084 rsnd_parse_connect_src(rdai, playback, capture); 1085 rsnd_parse_connect_ctu(rdai, playback, capture); 1086 rsnd_parse_connect_mix(rdai, playback, capture); 1087 rsnd_parse_connect_dvc(rdai, playback, capture); 1088 1089 of_node_put(playback); 1090 of_node_put(capture); 1091 } 1092 1093 if (rsnd_ssi_is_pin_sharing(io_capture) || 1094 rsnd_ssi_is_pin_sharing(io_playback)) { 1095 /* should have symmetric_rates if pin sharing */ 1096 drv->symmetric_rates = 1; 1097 } 1098 1099 dev_dbg(dev, "%s (%s/%s)\n", rdai->name, 1100 rsnd_io_to_mod_ssi(io_playback) ? "play" : " -- ", 1101 rsnd_io_to_mod_ssi(io_capture) ? "capture" : " -- "); 1102 } 1103 1104 static int rsnd_dai_probe(struct rsnd_priv *priv) 1105 { 1106 struct device_node *dai_node; 1107 struct device_node *dai_np; 1108 struct snd_soc_dai_driver *rdrv; 1109 struct device *dev = rsnd_priv_to_dev(priv); 1110 struct rsnd_dai *rdai; 1111 int nr; 1112 int is_graph; 1113 int dai_i; 1114 1115 dai_node = rsnd_dai_of_node(priv, &is_graph); 1116 if (is_graph) 1117 nr = of_graph_get_endpoint_count(dai_node); 1118 else 1119 nr = of_get_child_count(dai_node); 1120 1121 if (!nr) 1122 return -EINVAL; 1123 1124 rdrv = devm_kcalloc(dev, nr, sizeof(*rdrv), GFP_KERNEL); 1125 rdai = devm_kcalloc(dev, nr, sizeof(*rdai), GFP_KERNEL); 1126 if (!rdrv || !rdai) 1127 return -ENOMEM; 1128 1129 priv->rdai_nr = nr; 1130 priv->daidrv = rdrv; 1131 priv->rdai = rdai; 1132 1133 /* 1134 * parse all dai 1135 */ 1136 dai_i = 0; 1137 if (is_graph) { 1138 for_each_endpoint_of_node(dai_node, dai_np) { 1139 __rsnd_dai_probe(priv, dai_np, dai_i); 1140 rsnd_ssi_parse_hdmi_connection(priv, dai_np, dai_i); 1141 dai_i++; 1142 } 1143 } else { 1144 for_each_child_of_node(dai_node, dai_np) 1145 __rsnd_dai_probe(priv, dai_np, dai_i++); 1146 } 1147 1148 return 0; 1149 } 1150 1151 /* 1152 * pcm ops 1153 */ 1154 static int rsnd_hw_params(struct snd_pcm_substream *substream, 1155 struct snd_pcm_hw_params *hw_params) 1156 { 1157 struct snd_soc_dai *dai = rsnd_substream_to_dai(substream); 1158 struct rsnd_dai *rdai = rsnd_dai_to_rdai(dai); 1159 struct rsnd_dai_stream *io = rsnd_rdai_to_io(rdai, substream); 1160 int ret; 1161 1162 ret = rsnd_dai_call(hw_params, io, substream, hw_params); 1163 if (ret) 1164 return ret; 1165 1166 return snd_pcm_lib_malloc_pages(substream, 1167 params_buffer_bytes(hw_params)); 1168 } 1169 1170 static snd_pcm_uframes_t rsnd_pointer(struct snd_pcm_substream *substream) 1171 { 1172 struct snd_soc_dai *dai = rsnd_substream_to_dai(substream); 1173 struct rsnd_dai *rdai = rsnd_dai_to_rdai(dai); 1174 struct rsnd_dai_stream *io = rsnd_rdai_to_io(rdai, substream); 1175 snd_pcm_uframes_t pointer = 0; 1176 1177 rsnd_dai_call(pointer, io, &pointer); 1178 1179 return pointer; 1180 } 1181 1182 static const struct snd_pcm_ops rsnd_pcm_ops = { 1183 .ioctl = snd_pcm_lib_ioctl, 1184 .hw_params = rsnd_hw_params, 1185 .hw_free = snd_pcm_lib_free_pages, 1186 .pointer = rsnd_pointer, 1187 }; 1188 1189 /* 1190 * snd_kcontrol 1191 */ 1192 static int rsnd_kctrl_info(struct snd_kcontrol *kctrl, 1193 struct snd_ctl_elem_info *uinfo) 1194 { 1195 struct rsnd_kctrl_cfg *cfg = snd_kcontrol_chip(kctrl); 1196 1197 if (cfg->texts) { 1198 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED; 1199 uinfo->count = cfg->size; 1200 uinfo->value.enumerated.items = cfg->max; 1201 if (uinfo->value.enumerated.item >= cfg->max) 1202 uinfo->value.enumerated.item = cfg->max - 1; 1203 strlcpy(uinfo->value.enumerated.name, 1204 cfg->texts[uinfo->value.enumerated.item], 1205 sizeof(uinfo->value.enumerated.name)); 1206 } else { 1207 uinfo->count = cfg->size; 1208 uinfo->value.integer.min = 0; 1209 uinfo->value.integer.max = cfg->max; 1210 uinfo->type = (cfg->max == 1) ? 1211 SNDRV_CTL_ELEM_TYPE_BOOLEAN : 1212 SNDRV_CTL_ELEM_TYPE_INTEGER; 1213 } 1214 1215 return 0; 1216 } 1217 1218 static int rsnd_kctrl_get(struct snd_kcontrol *kctrl, 1219 struct snd_ctl_elem_value *uc) 1220 { 1221 struct rsnd_kctrl_cfg *cfg = snd_kcontrol_chip(kctrl); 1222 int i; 1223 1224 for (i = 0; i < cfg->size; i++) 1225 if (cfg->texts) 1226 uc->value.enumerated.item[i] = cfg->val[i]; 1227 else 1228 uc->value.integer.value[i] = cfg->val[i]; 1229 1230 return 0; 1231 } 1232 1233 static int rsnd_kctrl_put(struct snd_kcontrol *kctrl, 1234 struct snd_ctl_elem_value *uc) 1235 { 1236 struct rsnd_kctrl_cfg *cfg = snd_kcontrol_chip(kctrl); 1237 int i, change = 0; 1238 1239 if (!cfg->accept(cfg->io)) 1240 return 0; 1241 1242 for (i = 0; i < cfg->size; i++) { 1243 if (cfg->texts) { 1244 change |= (uc->value.enumerated.item[i] != cfg->val[i]); 1245 cfg->val[i] = uc->value.enumerated.item[i]; 1246 } else { 1247 change |= (uc->value.integer.value[i] != cfg->val[i]); 1248 cfg->val[i] = uc->value.integer.value[i]; 1249 } 1250 } 1251 1252 if (change && cfg->update) 1253 cfg->update(cfg->io, cfg->mod); 1254 1255 return change; 1256 } 1257 1258 int rsnd_kctrl_accept_anytime(struct rsnd_dai_stream *io) 1259 { 1260 return 1; 1261 } 1262 1263 int rsnd_kctrl_accept_runtime(struct rsnd_dai_stream *io) 1264 { 1265 struct snd_pcm_runtime *runtime = rsnd_io_to_runtime(io); 1266 struct rsnd_priv *priv = rsnd_io_to_priv(io); 1267 struct device *dev = rsnd_priv_to_dev(priv); 1268 1269 if (!runtime) { 1270 dev_warn(dev, "Can't update kctrl when idle\n"); 1271 return 0; 1272 } 1273 1274 return 1; 1275 } 1276 1277 struct rsnd_kctrl_cfg *rsnd_kctrl_init_m(struct rsnd_kctrl_cfg_m *cfg) 1278 { 1279 cfg->cfg.val = cfg->val; 1280 1281 return &cfg->cfg; 1282 } 1283 1284 struct rsnd_kctrl_cfg *rsnd_kctrl_init_s(struct rsnd_kctrl_cfg_s *cfg) 1285 { 1286 cfg->cfg.val = &cfg->val; 1287 1288 return &cfg->cfg; 1289 } 1290 1291 const char * const volume_ramp_rate[] = { 1292 "128 dB/1 step", /* 00000 */ 1293 "64 dB/1 step", /* 00001 */ 1294 "32 dB/1 step", /* 00010 */ 1295 "16 dB/1 step", /* 00011 */ 1296 "8 dB/1 step", /* 00100 */ 1297 "4 dB/1 step", /* 00101 */ 1298 "2 dB/1 step", /* 00110 */ 1299 "1 dB/1 step", /* 00111 */ 1300 "0.5 dB/1 step", /* 01000 */ 1301 "0.25 dB/1 step", /* 01001 */ 1302 "0.125 dB/1 step", /* 01010 = VOLUME_RAMP_MAX_MIX */ 1303 "0.125 dB/2 steps", /* 01011 */ 1304 "0.125 dB/4 steps", /* 01100 */ 1305 "0.125 dB/8 steps", /* 01101 */ 1306 "0.125 dB/16 steps", /* 01110 */ 1307 "0.125 dB/32 steps", /* 01111 */ 1308 "0.125 dB/64 steps", /* 10000 */ 1309 "0.125 dB/128 steps", /* 10001 */ 1310 "0.125 dB/256 steps", /* 10010 */ 1311 "0.125 dB/512 steps", /* 10011 */ 1312 "0.125 dB/1024 steps", /* 10100 */ 1313 "0.125 dB/2048 steps", /* 10101 */ 1314 "0.125 dB/4096 steps", /* 10110 */ 1315 "0.125 dB/8192 steps", /* 10111 = VOLUME_RAMP_MAX_DVC */ 1316 }; 1317 1318 int rsnd_kctrl_new(struct rsnd_mod *mod, 1319 struct rsnd_dai_stream *io, 1320 struct snd_soc_pcm_runtime *rtd, 1321 const unsigned char *name, 1322 int (*accept)(struct rsnd_dai_stream *io), 1323 void (*update)(struct rsnd_dai_stream *io, 1324 struct rsnd_mod *mod), 1325 struct rsnd_kctrl_cfg *cfg, 1326 const char * const *texts, 1327 int size, 1328 u32 max) 1329 { 1330 struct snd_card *card = rtd->card->snd_card; 1331 struct snd_kcontrol *kctrl; 1332 struct snd_kcontrol_new knew = { 1333 .iface = SNDRV_CTL_ELEM_IFACE_MIXER, 1334 .name = name, 1335 .info = rsnd_kctrl_info, 1336 .index = rtd->num, 1337 .get = rsnd_kctrl_get, 1338 .put = rsnd_kctrl_put, 1339 }; 1340 int ret; 1341 1342 if (size > RSND_MAX_CHANNELS) 1343 return -EINVAL; 1344 1345 kctrl = snd_ctl_new1(&knew, cfg); 1346 if (!kctrl) 1347 return -ENOMEM; 1348 1349 ret = snd_ctl_add(card, kctrl); 1350 if (ret < 0) 1351 return ret; 1352 1353 cfg->texts = texts; 1354 cfg->max = max; 1355 cfg->size = size; 1356 cfg->accept = accept; 1357 cfg->update = update; 1358 cfg->card = card; 1359 cfg->kctrl = kctrl; 1360 cfg->io = io; 1361 cfg->mod = mod; 1362 1363 return 0; 1364 } 1365 1366 /* 1367 * snd_soc_component 1368 */ 1369 1370 #define PREALLOC_BUFFER (32 * 1024) 1371 #define PREALLOC_BUFFER_MAX (32 * 1024) 1372 1373 static int rsnd_preallocate_pages(struct snd_soc_pcm_runtime *rtd, 1374 struct rsnd_dai_stream *io, 1375 int stream) 1376 { 1377 struct rsnd_priv *priv = rsnd_io_to_priv(io); 1378 struct device *dev = rsnd_priv_to_dev(priv); 1379 struct snd_pcm_substream *substream; 1380 int err; 1381 1382 /* 1383 * use Audio-DMAC dev if we can use IPMMU 1384 * see 1385 * rsnd_dmaen_attach() 1386 */ 1387 if (io->dmac_dev) 1388 dev = io->dmac_dev; 1389 1390 for (substream = rtd->pcm->streams[stream].substream; 1391 substream; 1392 substream = substream->next) { 1393 err = snd_pcm_lib_preallocate_pages(substream, 1394 SNDRV_DMA_TYPE_DEV, 1395 dev, 1396 PREALLOC_BUFFER, PREALLOC_BUFFER_MAX); 1397 if (err < 0) 1398 return err; 1399 } 1400 1401 return 0; 1402 } 1403 1404 static int rsnd_pcm_new(struct snd_soc_pcm_runtime *rtd) 1405 { 1406 struct snd_soc_dai *dai = rtd->cpu_dai; 1407 struct rsnd_dai *rdai = rsnd_dai_to_rdai(dai); 1408 int ret; 1409 1410 ret = rsnd_dai_call(pcm_new, &rdai->playback, rtd); 1411 if (ret) 1412 return ret; 1413 1414 ret = rsnd_dai_call(pcm_new, &rdai->capture, rtd); 1415 if (ret) 1416 return ret; 1417 1418 ret = rsnd_preallocate_pages(rtd, &rdai->playback, 1419 SNDRV_PCM_STREAM_PLAYBACK); 1420 if (ret) 1421 return ret; 1422 1423 ret = rsnd_preallocate_pages(rtd, &rdai->capture, 1424 SNDRV_PCM_STREAM_CAPTURE); 1425 if (ret) 1426 return ret; 1427 1428 return 0; 1429 } 1430 1431 static const struct snd_soc_component_driver rsnd_soc_component = { 1432 .ops = &rsnd_pcm_ops, 1433 .pcm_new = rsnd_pcm_new, 1434 .name = "rsnd", 1435 }; 1436 1437 static int rsnd_rdai_continuance_probe(struct rsnd_priv *priv, 1438 struct rsnd_dai_stream *io) 1439 { 1440 int ret; 1441 1442 ret = rsnd_dai_call(probe, io, priv); 1443 if (ret == -EAGAIN) { 1444 struct rsnd_mod *ssi_mod = rsnd_io_to_mod_ssi(io); 1445 struct rsnd_mod *mod; 1446 int i; 1447 1448 /* 1449 * Fallback to PIO mode 1450 */ 1451 1452 /* 1453 * call "remove" for SSI/SRC/DVC 1454 * SSI will be switch to PIO mode if it was DMA mode 1455 * see 1456 * rsnd_dma_init() 1457 * rsnd_ssi_fallback() 1458 */ 1459 rsnd_dai_call(remove, io, priv); 1460 1461 /* 1462 * remove all mod from io 1463 * and, re connect ssi 1464 */ 1465 for_each_rsnd_mod(i, mod, io) 1466 rsnd_dai_disconnect(mod, io, i); 1467 rsnd_dai_connect(ssi_mod, io, RSND_MOD_SSI); 1468 1469 /* 1470 * fallback 1471 */ 1472 rsnd_dai_call(fallback, io, priv); 1473 1474 /* 1475 * retry to "probe". 1476 * DAI has SSI which is PIO mode only now. 1477 */ 1478 ret = rsnd_dai_call(probe, io, priv); 1479 } 1480 1481 return ret; 1482 } 1483 1484 /* 1485 * rsnd probe 1486 */ 1487 static int rsnd_probe(struct platform_device *pdev) 1488 { 1489 struct rsnd_priv *priv; 1490 struct device *dev = &pdev->dev; 1491 struct rsnd_dai *rdai; 1492 int (*probe_func[])(struct rsnd_priv *priv) = { 1493 rsnd_gen_probe, 1494 rsnd_dma_probe, 1495 rsnd_ssi_probe, 1496 rsnd_ssiu_probe, 1497 rsnd_src_probe, 1498 rsnd_ctu_probe, 1499 rsnd_mix_probe, 1500 rsnd_dvc_probe, 1501 rsnd_cmd_probe, 1502 rsnd_adg_probe, 1503 rsnd_dai_probe, 1504 }; 1505 int ret, i; 1506 1507 /* 1508 * init priv data 1509 */ 1510 priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL); 1511 if (!priv) 1512 return -ENODEV; 1513 1514 priv->pdev = pdev; 1515 priv->flags = (unsigned long)of_device_get_match_data(dev); 1516 spin_lock_init(&priv->lock); 1517 1518 /* 1519 * init each module 1520 */ 1521 for (i = 0; i < ARRAY_SIZE(probe_func); i++) { 1522 ret = probe_func[i](priv); 1523 if (ret) 1524 return ret; 1525 } 1526 1527 for_each_rsnd_dai(rdai, priv, i) { 1528 ret = rsnd_rdai_continuance_probe(priv, &rdai->playback); 1529 if (ret) 1530 goto exit_snd_probe; 1531 1532 ret = rsnd_rdai_continuance_probe(priv, &rdai->capture); 1533 if (ret) 1534 goto exit_snd_probe; 1535 } 1536 1537 dev_set_drvdata(dev, priv); 1538 1539 /* 1540 * asoc register 1541 */ 1542 ret = devm_snd_soc_register_component(dev, &rsnd_soc_component, 1543 priv->daidrv, rsnd_rdai_nr(priv)); 1544 if (ret < 0) { 1545 dev_err(dev, "cannot snd dai register\n"); 1546 goto exit_snd_probe; 1547 } 1548 1549 pm_runtime_enable(dev); 1550 1551 dev_info(dev, "probed\n"); 1552 return ret; 1553 1554 exit_snd_probe: 1555 for_each_rsnd_dai(rdai, priv, i) { 1556 rsnd_dai_call(remove, &rdai->playback, priv); 1557 rsnd_dai_call(remove, &rdai->capture, priv); 1558 } 1559 1560 /* 1561 * adg is very special mod which can't use rsnd_dai_call(remove), 1562 * and it registers ADG clock on probe. 1563 * It should be unregister if probe failed. 1564 * Mainly it is assuming -EPROBE_DEFER case 1565 */ 1566 rsnd_adg_remove(priv); 1567 1568 return ret; 1569 } 1570 1571 static int rsnd_remove(struct platform_device *pdev) 1572 { 1573 struct rsnd_priv *priv = dev_get_drvdata(&pdev->dev); 1574 struct rsnd_dai *rdai; 1575 void (*remove_func[])(struct rsnd_priv *priv) = { 1576 rsnd_ssi_remove, 1577 rsnd_ssiu_remove, 1578 rsnd_src_remove, 1579 rsnd_ctu_remove, 1580 rsnd_mix_remove, 1581 rsnd_dvc_remove, 1582 rsnd_cmd_remove, 1583 rsnd_adg_remove, 1584 }; 1585 int ret = 0, i; 1586 1587 snd_soc_disconnect_sync(&pdev->dev); 1588 1589 pm_runtime_disable(&pdev->dev); 1590 1591 for_each_rsnd_dai(rdai, priv, i) { 1592 ret |= rsnd_dai_call(remove, &rdai->playback, priv); 1593 ret |= rsnd_dai_call(remove, &rdai->capture, priv); 1594 } 1595 1596 for (i = 0; i < ARRAY_SIZE(remove_func); i++) 1597 remove_func[i](priv); 1598 1599 return ret; 1600 } 1601 1602 static int __maybe_unused rsnd_suspend(struct device *dev) 1603 { 1604 struct rsnd_priv *priv = dev_get_drvdata(dev); 1605 1606 rsnd_adg_clk_disable(priv); 1607 1608 return 0; 1609 } 1610 1611 static int __maybe_unused rsnd_resume(struct device *dev) 1612 { 1613 struct rsnd_priv *priv = dev_get_drvdata(dev); 1614 1615 rsnd_adg_clk_enable(priv); 1616 1617 return 0; 1618 } 1619 1620 static const struct dev_pm_ops rsnd_pm_ops = { 1621 SET_SYSTEM_SLEEP_PM_OPS(rsnd_suspend, rsnd_resume) 1622 }; 1623 1624 static struct platform_driver rsnd_driver = { 1625 .driver = { 1626 .name = "rcar_sound", 1627 .pm = &rsnd_pm_ops, 1628 .of_match_table = rsnd_of_match, 1629 }, 1630 .probe = rsnd_probe, 1631 .remove = rsnd_remove, 1632 }; 1633 module_platform_driver(rsnd_driver); 1634 1635 MODULE_LICENSE("GPL v2"); 1636 MODULE_DESCRIPTION("Renesas R-Car audio driver"); 1637 MODULE_AUTHOR("Kuninori Morimoto <kuninori.morimoto.gx@renesas.com>"); 1638 MODULE_ALIAS("platform:rcar-pcm-audio"); 1639