1 /* 2 * Renesas R-Car SRU/SCU/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 * Renesas R-Car sound device structure 17 * 18 * Gen1 19 * 20 * SRU : Sound Routing Unit 21 * - SRC : Sampling Rate Converter 22 * - CMD 23 * - CTU : Channel Count Conversion Unit 24 * - MIX : Mixer 25 * - DVC : Digital Volume and Mute Function 26 * - SSI : Serial Sound Interface 27 * 28 * Gen2 29 * 30 * SCU : Sampling Rate Converter Unit 31 * - SRC : Sampling Rate Converter 32 * - CMD 33 * - CTU : Channel Count Conversion Unit 34 * - MIX : Mixer 35 * - DVC : Digital Volume and Mute Function 36 * SSIU : Serial Sound Interface Unit 37 * - SSI : Serial Sound Interface 38 */ 39 40 /* 41 * driver data Image 42 * 43 * rsnd_priv 44 * | 45 * | ** this depends on Gen1/Gen2 46 * | 47 * +- gen 48 * | 49 * | ** these depend on data path 50 * | ** gen and platform data control it 51 * | 52 * +- rdai[0] 53 * | | sru ssiu ssi 54 * | +- playback -> [mod] -> [mod] -> [mod] -> ... 55 * | | 56 * | | sru ssiu ssi 57 * | +- capture -> [mod] -> [mod] -> [mod] -> ... 58 * | 59 * +- rdai[1] 60 * | | sru ssiu ssi 61 * | +- playback -> [mod] -> [mod] -> [mod] -> ... 62 * | | 63 * | | sru ssiu ssi 64 * | +- capture -> [mod] -> [mod] -> [mod] -> ... 65 * ... 66 * | 67 * | ** these control ssi 68 * | 69 * +- ssi 70 * | | 71 * | +- ssi[0] 72 * | +- ssi[1] 73 * | +- ssi[2] 74 * | ... 75 * | 76 * | ** these control src 77 * | 78 * +- src 79 * | 80 * +- src[0] 81 * +- src[1] 82 * +- src[2] 83 * ... 84 * 85 * 86 * for_each_rsnd_dai(xx, priv, xx) 87 * rdai[0] => rdai[1] => rdai[2] => ... 88 * 89 * for_each_rsnd_mod(xx, rdai, xx) 90 * [mod] => [mod] => [mod] => ... 91 * 92 * rsnd_dai_call(xxx, fn ) 93 * [mod]->fn() -> [mod]->fn() -> [mod]->fn()... 94 * 95 */ 96 #include <linux/pm_runtime.h> 97 #include "rsnd.h" 98 99 #define RSND_RATES SNDRV_PCM_RATE_8000_192000 100 #define RSND_FMTS (SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S16_LE) 101 102 static const struct of_device_id rsnd_of_match[] = { 103 { .compatible = "renesas,rcar_sound-gen1", .data = (void *)RSND_GEN1 }, 104 { .compatible = "renesas,rcar_sound-gen2", .data = (void *)RSND_GEN2 }, 105 { .compatible = "renesas,rcar_sound-gen3", .data = (void *)RSND_GEN2 }, /* gen2 compatible */ 106 {}, 107 }; 108 MODULE_DEVICE_TABLE(of, rsnd_of_match); 109 110 /* 111 * rsnd_mod functions 112 */ 113 void rsnd_mod_make_sure(struct rsnd_mod *mod, enum rsnd_mod_type type) 114 { 115 if (mod->type != type) { 116 struct rsnd_priv *priv = rsnd_mod_to_priv(mod); 117 struct device *dev = rsnd_priv_to_dev(priv); 118 119 dev_warn(dev, "%s[%d] is not your expected module\n", 120 rsnd_mod_name(mod), rsnd_mod_id(mod)); 121 } 122 } 123 124 char *rsnd_mod_name(struct rsnd_mod *mod) 125 { 126 if (!mod || !mod->ops) 127 return "unknown"; 128 129 return mod->ops->name; 130 } 131 132 struct dma_chan *rsnd_mod_dma_req(struct rsnd_dai_stream *io, 133 struct rsnd_mod *mod) 134 { 135 if (!mod || !mod->ops || !mod->ops->dma_req) 136 return NULL; 137 138 return mod->ops->dma_req(io, mod); 139 } 140 141 u32 *rsnd_mod_get_status(struct rsnd_dai_stream *io, 142 struct rsnd_mod *mod, 143 enum rsnd_mod_type type) 144 { 145 return &mod->status; 146 } 147 148 int rsnd_mod_init(struct rsnd_priv *priv, 149 struct rsnd_mod *mod, 150 struct rsnd_mod_ops *ops, 151 struct clk *clk, 152 u32* (*get_status)(struct rsnd_dai_stream *io, 153 struct rsnd_mod *mod, 154 enum rsnd_mod_type type), 155 enum rsnd_mod_type type, 156 int id) 157 { 158 int ret = clk_prepare(clk); 159 160 if (ret) 161 return ret; 162 163 mod->id = id; 164 mod->ops = ops; 165 mod->type = type; 166 mod->clk = clk; 167 mod->priv = priv; 168 mod->get_status = get_status; 169 170 return ret; 171 } 172 173 void rsnd_mod_quit(struct rsnd_mod *mod) 174 { 175 if (mod->clk) 176 clk_unprepare(mod->clk); 177 mod->clk = NULL; 178 } 179 180 void rsnd_mod_interrupt(struct rsnd_mod *mod, 181 void (*callback)(struct rsnd_mod *mod, 182 struct rsnd_dai_stream *io)) 183 { 184 struct rsnd_priv *priv = rsnd_mod_to_priv(mod); 185 struct rsnd_dai_stream *io; 186 struct rsnd_dai *rdai; 187 int i; 188 189 for_each_rsnd_dai(rdai, priv, i) { 190 io = &rdai->playback; 191 if (mod == io->mod[mod->type]) 192 callback(mod, io); 193 194 io = &rdai->capture; 195 if (mod == io->mod[mod->type]) 196 callback(mod, io); 197 } 198 } 199 200 int rsnd_io_is_working(struct rsnd_dai_stream *io) 201 { 202 /* see rsnd_dai_stream_init/quit() */ 203 return !!io->substream; 204 } 205 206 void rsnd_set_slot(struct rsnd_dai *rdai, 207 int slots, int num) 208 { 209 rdai->slots = slots; 210 rdai->slots_num = num; 211 } 212 213 int rsnd_get_slot(struct rsnd_dai_stream *io) 214 { 215 struct rsnd_dai *rdai = rsnd_io_to_rdai(io); 216 217 return rdai->slots; 218 } 219 220 int rsnd_get_slot_num(struct rsnd_dai_stream *io) 221 { 222 struct rsnd_dai *rdai = rsnd_io_to_rdai(io); 223 224 return rdai->slots_num; 225 } 226 227 int rsnd_runtime_channel_original(struct rsnd_dai_stream *io) 228 { 229 struct snd_pcm_runtime *runtime = rsnd_io_to_runtime(io); 230 231 return runtime->channels; 232 } 233 234 int rsnd_runtime_channel_after_ctu(struct rsnd_dai_stream *io) 235 { 236 int chan = rsnd_runtime_channel_original(io); 237 struct rsnd_mod *ctu_mod = rsnd_io_to_mod_ctu(io); 238 239 if (ctu_mod) { 240 u32 converted_chan = rsnd_ctu_converted_channel(ctu_mod); 241 242 if (converted_chan) 243 return converted_chan; 244 } 245 246 return chan; 247 } 248 249 int rsnd_runtime_channel_for_ssi(struct rsnd_dai_stream *io) 250 { 251 int chan = rsnd_io_is_play(io) ? 252 rsnd_runtime_channel_after_ctu(io) : 253 rsnd_runtime_channel_original(io); 254 255 /* Use Multi SSI */ 256 if (rsnd_runtime_is_ssi_multi(io)) 257 chan /= rsnd_get_slot_num(io); 258 259 /* TDM Extend Mode needs 8ch */ 260 if (chan == 6) 261 chan = 8; 262 263 return chan; 264 } 265 266 int rsnd_runtime_is_ssi_multi(struct rsnd_dai_stream *io) 267 { 268 int slots = rsnd_get_slot_num(io); 269 int chan = rsnd_io_is_play(io) ? 270 rsnd_runtime_channel_after_ctu(io) : 271 rsnd_runtime_channel_original(io); 272 273 return (chan >= 6) && (slots > 1); 274 } 275 276 int rsnd_runtime_is_ssi_tdm(struct rsnd_dai_stream *io) 277 { 278 return rsnd_runtime_channel_for_ssi(io) >= 6; 279 } 280 281 /* 282 * ADINR function 283 */ 284 u32 rsnd_get_adinr_bit(struct rsnd_mod *mod, struct rsnd_dai_stream *io) 285 { 286 struct rsnd_priv *priv = rsnd_mod_to_priv(mod); 287 struct snd_pcm_runtime *runtime = rsnd_io_to_runtime(io); 288 struct device *dev = rsnd_priv_to_dev(priv); 289 290 switch (runtime->sample_bits) { 291 case 16: 292 return 8 << 16; 293 case 32: 294 return 0 << 16; 295 } 296 297 dev_warn(dev, "not supported sample bits\n"); 298 299 return 0; 300 } 301 302 /* 303 * DALIGN function 304 */ 305 u32 rsnd_get_dalign(struct rsnd_mod *mod, struct rsnd_dai_stream *io) 306 { 307 struct rsnd_mod *ssiu = rsnd_io_to_mod_ssiu(io); 308 struct rsnd_mod *target; 309 struct snd_pcm_runtime *runtime = rsnd_io_to_runtime(io); 310 u32 val = 0x76543210; 311 u32 mask = ~0; 312 313 if (rsnd_io_is_play(io)) { 314 struct rsnd_mod *src = rsnd_io_to_mod_src(io); 315 316 target = src ? src : ssiu; 317 } else { 318 struct rsnd_mod *cmd = rsnd_io_to_mod_cmd(io); 319 320 target = cmd ? cmd : ssiu; 321 } 322 323 mask <<= runtime->channels * 4; 324 val = val & mask; 325 326 switch (runtime->sample_bits) { 327 case 16: 328 val |= 0x67452301 & ~mask; 329 break; 330 case 32: 331 val |= 0x76543210 & ~mask; 332 break; 333 } 334 335 /* 336 * exchange channeles on SRC if possible, 337 * otherwise, R/L volume settings on DVC 338 * changes inverted channels 339 */ 340 if (mod == target) 341 return val; 342 else 343 return 0x76543210; 344 } 345 346 u32 rsnd_get_busif_shift(struct rsnd_dai_stream *io, struct rsnd_mod *mod) 347 { 348 enum rsnd_mod_type playback_mods[] = { 349 RSND_MOD_SRC, 350 RSND_MOD_CMD, 351 RSND_MOD_SSIU, 352 }; 353 enum rsnd_mod_type capture_mods[] = { 354 RSND_MOD_CMD, 355 RSND_MOD_SRC, 356 RSND_MOD_SSIU, 357 }; 358 struct snd_pcm_runtime *runtime = rsnd_io_to_runtime(io); 359 struct rsnd_mod *tmod = NULL; 360 enum rsnd_mod_type *mods = 361 rsnd_io_is_play(io) ? 362 playback_mods : capture_mods; 363 int i; 364 365 /* 366 * This is needed for 24bit data 367 * We need to shift 8bit 368 * 369 * Linux 24bit data is located as 0x00****** 370 * HW 24bit data is located as 0x******00 371 * 372 */ 373 switch (runtime->sample_bits) { 374 case 16: 375 return 0; 376 case 32: 377 break; 378 } 379 380 for (i = 0; i < ARRAY_SIZE(playback_mods); i++) { 381 tmod = rsnd_io_to_mod(io, mods[i]); 382 if (tmod) 383 break; 384 } 385 386 if (tmod != mod) 387 return 0; 388 389 if (rsnd_io_is_play(io)) 390 return (0 << 20) | /* shift to Left */ 391 (8 << 16); /* 8bit */ 392 else 393 return (1 << 20) | /* shift to Right */ 394 (8 << 16); /* 8bit */ 395 } 396 397 /* 398 * rsnd_dai functions 399 */ 400 struct rsnd_mod *rsnd_mod_next(int *iterator, 401 struct rsnd_dai_stream *io, 402 enum rsnd_mod_type *array, 403 int array_size) 404 { 405 struct rsnd_mod *mod; 406 enum rsnd_mod_type type; 407 int max = array ? array_size : RSND_MOD_MAX; 408 409 for (; *iterator < max; (*iterator)++) { 410 type = (array) ? array[*iterator] : *iterator; 411 mod = io->mod[type]; 412 if (!mod) 413 continue; 414 415 return mod; 416 } 417 418 return NULL; 419 } 420 421 static enum rsnd_mod_type rsnd_mod_sequence[][RSND_MOD_MAX] = { 422 { 423 /* CAPTURE */ 424 RSND_MOD_AUDMAPP, 425 RSND_MOD_AUDMA, 426 RSND_MOD_DVC, 427 RSND_MOD_MIX, 428 RSND_MOD_CTU, 429 RSND_MOD_CMD, 430 RSND_MOD_SRC, 431 RSND_MOD_SSIU, 432 RSND_MOD_SSIM3, 433 RSND_MOD_SSIM2, 434 RSND_MOD_SSIM1, 435 RSND_MOD_SSIP, 436 RSND_MOD_SSI, 437 }, { 438 /* PLAYBACK */ 439 RSND_MOD_AUDMAPP, 440 RSND_MOD_AUDMA, 441 RSND_MOD_SSIM3, 442 RSND_MOD_SSIM2, 443 RSND_MOD_SSIM1, 444 RSND_MOD_SSIP, 445 RSND_MOD_SSI, 446 RSND_MOD_SSIU, 447 RSND_MOD_DVC, 448 RSND_MOD_MIX, 449 RSND_MOD_CTU, 450 RSND_MOD_CMD, 451 RSND_MOD_SRC, 452 }, 453 }; 454 455 static int rsnd_status_update(u32 *status, 456 int shift, int add, int timing) 457 { 458 u32 mask = 0xF << shift; 459 u8 val = (*status >> shift) & 0xF; 460 u8 next_val = (val + add) & 0xF; 461 int func_call = (val == timing); 462 463 if (next_val == 0xF) /* underflow case */ 464 func_call = 0; 465 else 466 *status = (*status & ~mask) + (next_val << shift); 467 468 return func_call; 469 } 470 471 #define rsnd_dai_call(fn, io, param...) \ 472 ({ \ 473 struct rsnd_priv *priv = rsnd_io_to_priv(io); \ 474 struct device *dev = rsnd_priv_to_dev(priv); \ 475 struct rsnd_mod *mod; \ 476 int is_play = rsnd_io_is_play(io); \ 477 int ret = 0, i; \ 478 enum rsnd_mod_type *types = rsnd_mod_sequence[is_play]; \ 479 for_each_rsnd_mod_arrays(i, mod, io, types, RSND_MOD_MAX) { \ 480 int tmp = 0; \ 481 u32 *status = mod->get_status(io, mod, types[i]); \ 482 int func_call = rsnd_status_update(status, \ 483 __rsnd_mod_shift_##fn, \ 484 __rsnd_mod_add_##fn, \ 485 __rsnd_mod_call_##fn); \ 486 dev_dbg(dev, "%s[%d]\t0x%08x %s\n", \ 487 rsnd_mod_name(mod), rsnd_mod_id(mod), *status, \ 488 (func_call && (mod)->ops->fn) ? #fn : ""); \ 489 if (func_call && (mod)->ops->fn) \ 490 tmp = (mod)->ops->fn(mod, io, param); \ 491 if (tmp) \ 492 dev_err(dev, "%s[%d] : %s error %d\n", \ 493 rsnd_mod_name(mod), rsnd_mod_id(mod), \ 494 #fn, tmp); \ 495 ret |= tmp; \ 496 } \ 497 ret; \ 498 }) 499 500 int rsnd_dai_connect(struct rsnd_mod *mod, 501 struct rsnd_dai_stream *io, 502 enum rsnd_mod_type type) 503 { 504 struct rsnd_priv *priv; 505 struct device *dev; 506 507 if (!mod) 508 return -EIO; 509 510 if (io->mod[type] == mod) 511 return 0; 512 513 if (io->mod[type]) 514 return -EINVAL; 515 516 priv = rsnd_mod_to_priv(mod); 517 dev = rsnd_priv_to_dev(priv); 518 519 io->mod[type] = mod; 520 521 dev_dbg(dev, "%s[%d] is connected to io (%s)\n", 522 rsnd_mod_name(mod), rsnd_mod_id(mod), 523 rsnd_io_is_play(io) ? "Playback" : "Capture"); 524 525 return 0; 526 } 527 528 static void rsnd_dai_disconnect(struct rsnd_mod *mod, 529 struct rsnd_dai_stream *io, 530 enum rsnd_mod_type type) 531 { 532 io->mod[type] = NULL; 533 } 534 535 struct rsnd_dai *rsnd_rdai_get(struct rsnd_priv *priv, int id) 536 { 537 if ((id < 0) || (id >= rsnd_rdai_nr(priv))) 538 return NULL; 539 540 return priv->rdai + id; 541 } 542 543 #define rsnd_dai_to_priv(dai) snd_soc_dai_get_drvdata(dai) 544 static struct rsnd_dai *rsnd_dai_to_rdai(struct snd_soc_dai *dai) 545 { 546 struct rsnd_priv *priv = rsnd_dai_to_priv(dai); 547 548 return rsnd_rdai_get(priv, dai->id); 549 } 550 551 /* 552 * rsnd_soc_dai functions 553 */ 554 int rsnd_dai_pointer_offset(struct rsnd_dai_stream *io, int additional) 555 { 556 struct snd_pcm_substream *substream = io->substream; 557 struct snd_pcm_runtime *runtime = substream->runtime; 558 int pos = io->byte_pos + additional; 559 560 pos %= (runtime->periods * io->byte_per_period); 561 562 return pos; 563 } 564 565 bool rsnd_dai_pointer_update(struct rsnd_dai_stream *io, int byte) 566 { 567 io->byte_pos += byte; 568 569 if (io->byte_pos >= io->next_period_byte) { 570 struct snd_pcm_substream *substream = io->substream; 571 struct snd_pcm_runtime *runtime = substream->runtime; 572 573 io->period_pos++; 574 io->next_period_byte += io->byte_per_period; 575 576 if (io->period_pos >= runtime->periods) { 577 io->byte_pos = 0; 578 io->period_pos = 0; 579 io->next_period_byte = io->byte_per_period; 580 } 581 582 return true; 583 } 584 585 return false; 586 } 587 588 void rsnd_dai_period_elapsed(struct rsnd_dai_stream *io) 589 { 590 struct snd_pcm_substream *substream = io->substream; 591 592 /* 593 * this function should be called... 594 * 595 * - if rsnd_dai_pointer_update() returns true 596 * - without spin lock 597 */ 598 599 snd_pcm_period_elapsed(substream); 600 } 601 602 static void rsnd_dai_stream_init(struct rsnd_dai_stream *io, 603 struct snd_pcm_substream *substream) 604 { 605 struct snd_pcm_runtime *runtime = substream->runtime; 606 607 io->substream = substream; 608 io->byte_pos = 0; 609 io->period_pos = 0; 610 io->byte_per_period = runtime->period_size * 611 runtime->channels * 612 samples_to_bytes(runtime, 1); 613 io->next_period_byte = io->byte_per_period; 614 } 615 616 static void rsnd_dai_stream_quit(struct rsnd_dai_stream *io) 617 { 618 io->substream = NULL; 619 } 620 621 static 622 struct snd_soc_dai *rsnd_substream_to_dai(struct snd_pcm_substream *substream) 623 { 624 struct snd_soc_pcm_runtime *rtd = substream->private_data; 625 626 return rtd->cpu_dai; 627 } 628 629 static 630 struct rsnd_dai_stream *rsnd_rdai_to_io(struct rsnd_dai *rdai, 631 struct snd_pcm_substream *substream) 632 { 633 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) 634 return &rdai->playback; 635 else 636 return &rdai->capture; 637 } 638 639 static int rsnd_soc_dai_trigger(struct snd_pcm_substream *substream, int cmd, 640 struct snd_soc_dai *dai) 641 { 642 struct rsnd_priv *priv = rsnd_dai_to_priv(dai); 643 struct rsnd_dai *rdai = rsnd_dai_to_rdai(dai); 644 struct rsnd_dai_stream *io = rsnd_rdai_to_io(rdai, substream); 645 int ret; 646 unsigned long flags; 647 648 spin_lock_irqsave(&priv->lock, flags); 649 650 switch (cmd) { 651 case SNDRV_PCM_TRIGGER_START: 652 case SNDRV_PCM_TRIGGER_RESUME: 653 rsnd_dai_stream_init(io, substream); 654 655 ret = rsnd_dai_call(init, io, priv); 656 if (ret < 0) 657 goto dai_trigger_end; 658 659 ret = rsnd_dai_call(start, io, priv); 660 if (ret < 0) 661 goto dai_trigger_end; 662 663 ret = rsnd_dai_call(irq, io, priv, 1); 664 if (ret < 0) 665 goto dai_trigger_end; 666 667 break; 668 case SNDRV_PCM_TRIGGER_STOP: 669 case SNDRV_PCM_TRIGGER_SUSPEND: 670 ret = rsnd_dai_call(irq, io, priv, 0); 671 672 ret |= rsnd_dai_call(stop, io, priv); 673 674 ret |= rsnd_dai_call(quit, io, priv); 675 676 rsnd_dai_stream_quit(io); 677 break; 678 default: 679 ret = -EINVAL; 680 } 681 682 dai_trigger_end: 683 spin_unlock_irqrestore(&priv->lock, flags); 684 685 return ret; 686 } 687 688 static int rsnd_soc_dai_set_fmt(struct snd_soc_dai *dai, unsigned int fmt) 689 { 690 struct rsnd_dai *rdai = rsnd_dai_to_rdai(dai); 691 692 /* set master/slave audio interface */ 693 switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) { 694 case SND_SOC_DAIFMT_CBM_CFM: 695 rdai->clk_master = 0; 696 break; 697 case SND_SOC_DAIFMT_CBS_CFS: 698 rdai->clk_master = 1; /* codec is slave, cpu is master */ 699 break; 700 default: 701 return -EINVAL; 702 } 703 704 /* set format */ 705 switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) { 706 case SND_SOC_DAIFMT_I2S: 707 rdai->sys_delay = 0; 708 rdai->data_alignment = 0; 709 rdai->frm_clk_inv = 0; 710 break; 711 case SND_SOC_DAIFMT_LEFT_J: 712 rdai->sys_delay = 1; 713 rdai->data_alignment = 0; 714 rdai->frm_clk_inv = 1; 715 break; 716 case SND_SOC_DAIFMT_RIGHT_J: 717 rdai->sys_delay = 1; 718 rdai->data_alignment = 1; 719 rdai->frm_clk_inv = 1; 720 break; 721 } 722 723 /* set clock inversion */ 724 switch (fmt & SND_SOC_DAIFMT_INV_MASK) { 725 case SND_SOC_DAIFMT_NB_IF: 726 rdai->frm_clk_inv = !rdai->frm_clk_inv; 727 break; 728 case SND_SOC_DAIFMT_IB_NF: 729 rdai->bit_clk_inv = !rdai->bit_clk_inv; 730 break; 731 case SND_SOC_DAIFMT_IB_IF: 732 rdai->bit_clk_inv = !rdai->bit_clk_inv; 733 rdai->frm_clk_inv = !rdai->frm_clk_inv; 734 break; 735 case SND_SOC_DAIFMT_NB_NF: 736 default: 737 break; 738 } 739 740 return 0; 741 } 742 743 static int rsnd_soc_set_dai_tdm_slot(struct snd_soc_dai *dai, 744 u32 tx_mask, u32 rx_mask, 745 int slots, int slot_width) 746 { 747 struct rsnd_priv *priv = rsnd_dai_to_priv(dai); 748 struct rsnd_dai *rdai = rsnd_dai_to_rdai(dai); 749 struct device *dev = rsnd_priv_to_dev(priv); 750 751 switch (slots) { 752 case 6: 753 /* TDM Extend Mode */ 754 rsnd_set_slot(rdai, slots, 1); 755 break; 756 default: 757 dev_err(dev, "unsupported TDM slots (%d)\n", slots); 758 return -EINVAL; 759 } 760 761 return 0; 762 } 763 764 static int rsnd_soc_dai_startup(struct snd_pcm_substream *substream, 765 struct snd_soc_dai *dai) 766 { 767 struct rsnd_dai *rdai = rsnd_dai_to_rdai(dai); 768 struct rsnd_dai_stream *io = rsnd_rdai_to_io(rdai, substream); 769 770 /* 771 * call rsnd_dai_call without spinlock 772 */ 773 return rsnd_dai_call(nolock_start, io, priv); 774 } 775 776 static void rsnd_soc_dai_shutdown(struct snd_pcm_substream *substream, 777 struct snd_soc_dai *dai) 778 { 779 struct rsnd_dai *rdai = rsnd_dai_to_rdai(dai); 780 struct rsnd_dai_stream *io = rsnd_rdai_to_io(rdai, substream); 781 782 /* 783 * call rsnd_dai_call without spinlock 784 */ 785 rsnd_dai_call(nolock_stop, io, priv); 786 } 787 788 static const struct snd_soc_dai_ops rsnd_soc_dai_ops = { 789 .startup = rsnd_soc_dai_startup, 790 .shutdown = rsnd_soc_dai_shutdown, 791 .trigger = rsnd_soc_dai_trigger, 792 .set_fmt = rsnd_soc_dai_set_fmt, 793 .set_tdm_slot = rsnd_soc_set_dai_tdm_slot, 794 }; 795 796 void rsnd_parse_connect_common(struct rsnd_dai *rdai, 797 struct rsnd_mod* (*mod_get)(struct rsnd_priv *priv, int id), 798 struct device_node *node, 799 struct device_node *playback, 800 struct device_node *capture) 801 { 802 struct rsnd_priv *priv = rsnd_rdai_to_priv(rdai); 803 struct device_node *np; 804 struct rsnd_mod *mod; 805 int i; 806 807 if (!node) 808 return; 809 810 i = 0; 811 for_each_child_of_node(node, np) { 812 mod = mod_get(priv, i); 813 if (np == playback) 814 rsnd_dai_connect(mod, &rdai->playback, mod->type); 815 if (np == capture) 816 rsnd_dai_connect(mod, &rdai->capture, mod->type); 817 i++; 818 } 819 820 of_node_put(node); 821 } 822 823 static int rsnd_dai_probe(struct rsnd_priv *priv) 824 { 825 struct device_node *dai_node; 826 struct device_node *dai_np; 827 struct device_node *playback, *capture; 828 struct rsnd_dai_stream *io_playback; 829 struct rsnd_dai_stream *io_capture; 830 struct snd_soc_dai_driver *rdrv, *drv; 831 struct rsnd_dai *rdai; 832 struct device *dev = rsnd_priv_to_dev(priv); 833 int nr, dai_i, io_i; 834 int ret; 835 836 dai_node = rsnd_dai_of_node(priv); 837 nr = of_get_child_count(dai_node); 838 if (!nr) { 839 ret = -EINVAL; 840 goto rsnd_dai_probe_done; 841 } 842 843 rdrv = devm_kzalloc(dev, sizeof(*rdrv) * nr, GFP_KERNEL); 844 rdai = devm_kzalloc(dev, sizeof(*rdai) * nr, GFP_KERNEL); 845 if (!rdrv || !rdai) { 846 ret = -ENOMEM; 847 goto rsnd_dai_probe_done; 848 } 849 850 priv->rdai_nr = nr; 851 priv->daidrv = rdrv; 852 priv->rdai = rdai; 853 854 /* 855 * parse all dai 856 */ 857 dai_i = 0; 858 for_each_child_of_node(dai_node, dai_np) { 859 rdai = rsnd_rdai_get(priv, dai_i); 860 drv = rdrv + dai_i; 861 io_playback = &rdai->playback; 862 io_capture = &rdai->capture; 863 864 snprintf(rdai->name, RSND_DAI_NAME_SIZE, "rsnd-dai.%d", dai_i); 865 866 rdai->priv = priv; 867 drv->name = rdai->name; 868 drv->ops = &rsnd_soc_dai_ops; 869 870 snprintf(rdai->playback.name, RSND_DAI_NAME_SIZE, 871 "DAI%d Playback", dai_i); 872 drv->playback.rates = RSND_RATES; 873 drv->playback.formats = RSND_FMTS; 874 drv->playback.channels_min = 2; 875 drv->playback.channels_max = 6; 876 drv->playback.stream_name = rdai->playback.name; 877 878 snprintf(rdai->capture.name, RSND_DAI_NAME_SIZE, 879 "DAI%d Capture", dai_i); 880 drv->capture.rates = RSND_RATES; 881 drv->capture.formats = RSND_FMTS; 882 drv->capture.channels_min = 2; 883 drv->capture.channels_max = 6; 884 drv->capture.stream_name = rdai->capture.name; 885 886 rdai->playback.rdai = rdai; 887 rdai->capture.rdai = rdai; 888 rsnd_set_slot(rdai, 2, 1); /* default */ 889 890 for (io_i = 0;; io_i++) { 891 playback = of_parse_phandle(dai_np, "playback", io_i); 892 capture = of_parse_phandle(dai_np, "capture", io_i); 893 894 if (!playback && !capture) 895 break; 896 897 rsnd_parse_connect_ssi(rdai, playback, capture); 898 rsnd_parse_connect_src(rdai, playback, capture); 899 rsnd_parse_connect_ctu(rdai, playback, capture); 900 rsnd_parse_connect_mix(rdai, playback, capture); 901 rsnd_parse_connect_dvc(rdai, playback, capture); 902 903 of_node_put(playback); 904 of_node_put(capture); 905 } 906 907 dai_i++; 908 909 dev_dbg(dev, "%s (%s/%s)\n", rdai->name, 910 rsnd_io_to_mod_ssi(io_playback) ? "play" : " -- ", 911 rsnd_io_to_mod_ssi(io_capture) ? "capture" : " -- "); 912 } 913 914 ret = 0; 915 916 rsnd_dai_probe_done: 917 of_node_put(dai_node); 918 919 return ret; 920 } 921 922 /* 923 * pcm ops 924 */ 925 static struct snd_pcm_hardware rsnd_pcm_hardware = { 926 .info = SNDRV_PCM_INFO_INTERLEAVED | 927 SNDRV_PCM_INFO_MMAP | 928 SNDRV_PCM_INFO_MMAP_VALID, 929 .buffer_bytes_max = 64 * 1024, 930 .period_bytes_min = 32, 931 .period_bytes_max = 8192, 932 .periods_min = 1, 933 .periods_max = 32, 934 .fifo_size = 256, 935 }; 936 937 static int rsnd_pcm_open(struct snd_pcm_substream *substream) 938 { 939 struct snd_pcm_runtime *runtime = substream->runtime; 940 int ret = 0; 941 942 snd_soc_set_runtime_hwparams(substream, &rsnd_pcm_hardware); 943 944 ret = snd_pcm_hw_constraint_integer(runtime, 945 SNDRV_PCM_HW_PARAM_PERIODS); 946 947 return ret; 948 } 949 950 static int rsnd_hw_params(struct snd_pcm_substream *substream, 951 struct snd_pcm_hw_params *hw_params) 952 { 953 struct snd_soc_dai *dai = rsnd_substream_to_dai(substream); 954 struct rsnd_dai *rdai = rsnd_dai_to_rdai(dai); 955 struct rsnd_dai_stream *io = rsnd_rdai_to_io(rdai, substream); 956 int ret; 957 958 ret = rsnd_dai_call(hw_params, io, substream, hw_params); 959 if (ret) 960 return ret; 961 962 return snd_pcm_lib_malloc_pages(substream, 963 params_buffer_bytes(hw_params)); 964 } 965 966 static snd_pcm_uframes_t rsnd_pointer(struct snd_pcm_substream *substream) 967 { 968 struct snd_pcm_runtime *runtime = substream->runtime; 969 struct snd_soc_dai *dai = rsnd_substream_to_dai(substream); 970 struct rsnd_dai *rdai = rsnd_dai_to_rdai(dai); 971 struct rsnd_dai_stream *io = rsnd_rdai_to_io(rdai, substream); 972 973 return bytes_to_frames(runtime, io->byte_pos); 974 } 975 976 static struct snd_pcm_ops rsnd_pcm_ops = { 977 .open = rsnd_pcm_open, 978 .ioctl = snd_pcm_lib_ioctl, 979 .hw_params = rsnd_hw_params, 980 .hw_free = snd_pcm_lib_free_pages, 981 .pointer = rsnd_pointer, 982 }; 983 984 /* 985 * snd_kcontrol 986 */ 987 #define kcontrol_to_cfg(kctrl) ((struct rsnd_kctrl_cfg *)kctrl->private_value) 988 static int rsnd_kctrl_info(struct snd_kcontrol *kctrl, 989 struct snd_ctl_elem_info *uinfo) 990 { 991 struct rsnd_kctrl_cfg *cfg = kcontrol_to_cfg(kctrl); 992 993 if (cfg->texts) { 994 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED; 995 uinfo->count = cfg->size; 996 uinfo->value.enumerated.items = cfg->max; 997 if (uinfo->value.enumerated.item >= cfg->max) 998 uinfo->value.enumerated.item = cfg->max - 1; 999 strlcpy(uinfo->value.enumerated.name, 1000 cfg->texts[uinfo->value.enumerated.item], 1001 sizeof(uinfo->value.enumerated.name)); 1002 } else { 1003 uinfo->count = cfg->size; 1004 uinfo->value.integer.min = 0; 1005 uinfo->value.integer.max = cfg->max; 1006 uinfo->type = (cfg->max == 1) ? 1007 SNDRV_CTL_ELEM_TYPE_BOOLEAN : 1008 SNDRV_CTL_ELEM_TYPE_INTEGER; 1009 } 1010 1011 return 0; 1012 } 1013 1014 static int rsnd_kctrl_get(struct snd_kcontrol *kctrl, 1015 struct snd_ctl_elem_value *uc) 1016 { 1017 struct rsnd_kctrl_cfg *cfg = kcontrol_to_cfg(kctrl); 1018 int i; 1019 1020 for (i = 0; i < cfg->size; i++) 1021 if (cfg->texts) 1022 uc->value.enumerated.item[i] = cfg->val[i]; 1023 else 1024 uc->value.integer.value[i] = cfg->val[i]; 1025 1026 return 0; 1027 } 1028 1029 static int rsnd_kctrl_put(struct snd_kcontrol *kctrl, 1030 struct snd_ctl_elem_value *uc) 1031 { 1032 struct rsnd_mod *mod = snd_kcontrol_chip(kctrl); 1033 struct rsnd_kctrl_cfg *cfg = kcontrol_to_cfg(kctrl); 1034 int i, change = 0; 1035 1036 for (i = 0; i < cfg->size; i++) { 1037 if (cfg->texts) { 1038 change |= (uc->value.enumerated.item[i] != cfg->val[i]); 1039 cfg->val[i] = uc->value.enumerated.item[i]; 1040 } else { 1041 change |= (uc->value.integer.value[i] != cfg->val[i]); 1042 cfg->val[i] = uc->value.integer.value[i]; 1043 } 1044 } 1045 1046 if (change && cfg->update) 1047 cfg->update(cfg->io, mod); 1048 1049 return change; 1050 } 1051 1052 struct rsnd_kctrl_cfg *rsnd_kctrl_init_m(struct rsnd_kctrl_cfg_m *cfg) 1053 { 1054 cfg->cfg.val = cfg->val; 1055 1056 return &cfg->cfg; 1057 } 1058 1059 struct rsnd_kctrl_cfg *rsnd_kctrl_init_s(struct rsnd_kctrl_cfg_s *cfg) 1060 { 1061 cfg->cfg.val = &cfg->val; 1062 1063 return &cfg->cfg; 1064 } 1065 1066 int rsnd_kctrl_new(struct rsnd_mod *mod, 1067 struct rsnd_dai_stream *io, 1068 struct snd_soc_pcm_runtime *rtd, 1069 const unsigned char *name, 1070 void (*update)(struct rsnd_dai_stream *io, 1071 struct rsnd_mod *mod), 1072 struct rsnd_kctrl_cfg *cfg, 1073 const char * const *texts, 1074 int size, 1075 u32 max) 1076 { 1077 struct snd_card *card = rtd->card->snd_card; 1078 struct snd_kcontrol *kctrl; 1079 struct snd_kcontrol_new knew = { 1080 .iface = SNDRV_CTL_ELEM_IFACE_MIXER, 1081 .name = name, 1082 .info = rsnd_kctrl_info, 1083 .index = rtd->num, 1084 .get = rsnd_kctrl_get, 1085 .put = rsnd_kctrl_put, 1086 .private_value = (unsigned long)cfg, 1087 }; 1088 int ret; 1089 1090 if (size > RSND_MAX_CHANNELS) 1091 return -EINVAL; 1092 1093 kctrl = snd_ctl_new1(&knew, mod); 1094 if (!kctrl) 1095 return -ENOMEM; 1096 1097 ret = snd_ctl_add(card, kctrl); 1098 if (ret < 0) 1099 return ret; 1100 1101 cfg->texts = texts; 1102 cfg->max = max; 1103 cfg->size = size; 1104 cfg->update = update; 1105 cfg->card = card; 1106 cfg->kctrl = kctrl; 1107 cfg->io = io; 1108 1109 return 0; 1110 } 1111 1112 /* 1113 * snd_soc_platform 1114 */ 1115 1116 #define PREALLOC_BUFFER (32 * 1024) 1117 #define PREALLOC_BUFFER_MAX (32 * 1024) 1118 1119 static int rsnd_pcm_new(struct snd_soc_pcm_runtime *rtd) 1120 { 1121 struct snd_soc_dai *dai = rtd->cpu_dai; 1122 struct rsnd_dai *rdai = rsnd_dai_to_rdai(dai); 1123 int ret; 1124 1125 ret = rsnd_dai_call(pcm_new, &rdai->playback, rtd); 1126 if (ret) 1127 return ret; 1128 1129 ret = rsnd_dai_call(pcm_new, &rdai->capture, rtd); 1130 if (ret) 1131 return ret; 1132 1133 return snd_pcm_lib_preallocate_pages_for_all( 1134 rtd->pcm, 1135 SNDRV_DMA_TYPE_CONTINUOUS, 1136 snd_dma_continuous_data(GFP_KERNEL), 1137 PREALLOC_BUFFER, PREALLOC_BUFFER_MAX); 1138 } 1139 1140 static struct snd_soc_platform_driver rsnd_soc_platform = { 1141 .ops = &rsnd_pcm_ops, 1142 .pcm_new = rsnd_pcm_new, 1143 }; 1144 1145 static const struct snd_soc_component_driver rsnd_soc_component = { 1146 .name = "rsnd", 1147 }; 1148 1149 static int rsnd_rdai_continuance_probe(struct rsnd_priv *priv, 1150 struct rsnd_dai_stream *io) 1151 { 1152 int ret; 1153 1154 ret = rsnd_dai_call(probe, io, priv); 1155 if (ret == -EAGAIN) { 1156 struct rsnd_mod *ssi_mod = rsnd_io_to_mod_ssi(io); 1157 struct rsnd_mod *mod; 1158 int i; 1159 1160 /* 1161 * Fallback to PIO mode 1162 */ 1163 1164 /* 1165 * call "remove" for SSI/SRC/DVC 1166 * SSI will be switch to PIO mode if it was DMA mode 1167 * see 1168 * rsnd_dma_init() 1169 * rsnd_ssi_fallback() 1170 */ 1171 rsnd_dai_call(remove, io, priv); 1172 1173 /* 1174 * remove all mod from io 1175 * and, re connect ssi 1176 */ 1177 for_each_rsnd_mod(i, mod, io) 1178 rsnd_dai_disconnect(mod, io, i); 1179 rsnd_dai_connect(ssi_mod, io, RSND_MOD_SSI); 1180 1181 /* 1182 * fallback 1183 */ 1184 rsnd_dai_call(fallback, io, priv); 1185 1186 /* 1187 * retry to "probe". 1188 * DAI has SSI which is PIO mode only now. 1189 */ 1190 ret = rsnd_dai_call(probe, io, priv); 1191 } 1192 1193 return ret; 1194 } 1195 1196 /* 1197 * rsnd probe 1198 */ 1199 static int rsnd_probe(struct platform_device *pdev) 1200 { 1201 struct rsnd_priv *priv; 1202 struct device *dev = &pdev->dev; 1203 struct rsnd_dai *rdai; 1204 int (*probe_func[])(struct rsnd_priv *priv) = { 1205 rsnd_gen_probe, 1206 rsnd_dma_probe, 1207 rsnd_ssi_probe, 1208 rsnd_ssiu_probe, 1209 rsnd_src_probe, 1210 rsnd_ctu_probe, 1211 rsnd_mix_probe, 1212 rsnd_dvc_probe, 1213 rsnd_cmd_probe, 1214 rsnd_adg_probe, 1215 rsnd_dai_probe, 1216 }; 1217 int ret, i; 1218 1219 /* 1220 * init priv data 1221 */ 1222 priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL); 1223 if (!priv) { 1224 dev_err(dev, "priv allocate failed\n"); 1225 return -ENODEV; 1226 } 1227 1228 priv->pdev = pdev; 1229 priv->flags = (unsigned long)of_device_get_match_data(dev); 1230 spin_lock_init(&priv->lock); 1231 1232 /* 1233 * init each module 1234 */ 1235 for (i = 0; i < ARRAY_SIZE(probe_func); i++) { 1236 ret = probe_func[i](priv); 1237 if (ret) 1238 return ret; 1239 } 1240 1241 for_each_rsnd_dai(rdai, priv, i) { 1242 ret = rsnd_rdai_continuance_probe(priv, &rdai->playback); 1243 if (ret) 1244 goto exit_snd_probe; 1245 1246 ret = rsnd_rdai_continuance_probe(priv, &rdai->capture); 1247 if (ret) 1248 goto exit_snd_probe; 1249 } 1250 1251 dev_set_drvdata(dev, priv); 1252 1253 /* 1254 * asoc register 1255 */ 1256 ret = snd_soc_register_platform(dev, &rsnd_soc_platform); 1257 if (ret < 0) { 1258 dev_err(dev, "cannot snd soc register\n"); 1259 return ret; 1260 } 1261 1262 ret = snd_soc_register_component(dev, &rsnd_soc_component, 1263 priv->daidrv, rsnd_rdai_nr(priv)); 1264 if (ret < 0) { 1265 dev_err(dev, "cannot snd dai register\n"); 1266 goto exit_snd_soc; 1267 } 1268 1269 pm_runtime_enable(dev); 1270 1271 dev_info(dev, "probed\n"); 1272 return ret; 1273 1274 exit_snd_soc: 1275 snd_soc_unregister_platform(dev); 1276 exit_snd_probe: 1277 for_each_rsnd_dai(rdai, priv, i) { 1278 rsnd_dai_call(remove, &rdai->playback, priv); 1279 rsnd_dai_call(remove, &rdai->capture, priv); 1280 } 1281 1282 return ret; 1283 } 1284 1285 static int rsnd_remove(struct platform_device *pdev) 1286 { 1287 struct rsnd_priv *priv = dev_get_drvdata(&pdev->dev); 1288 struct rsnd_dai *rdai; 1289 void (*remove_func[])(struct rsnd_priv *priv) = { 1290 rsnd_ssi_remove, 1291 rsnd_ssiu_remove, 1292 rsnd_src_remove, 1293 rsnd_ctu_remove, 1294 rsnd_mix_remove, 1295 rsnd_dvc_remove, 1296 rsnd_cmd_remove, 1297 rsnd_adg_remove, 1298 }; 1299 int ret = 0, i; 1300 1301 pm_runtime_disable(&pdev->dev); 1302 1303 for_each_rsnd_dai(rdai, priv, i) { 1304 ret |= rsnd_dai_call(remove, &rdai->playback, priv); 1305 ret |= rsnd_dai_call(remove, &rdai->capture, priv); 1306 } 1307 1308 for (i = 0; i < ARRAY_SIZE(remove_func); i++) 1309 remove_func[i](priv); 1310 1311 snd_soc_unregister_component(&pdev->dev); 1312 snd_soc_unregister_platform(&pdev->dev); 1313 1314 return ret; 1315 } 1316 1317 static int rsnd_suspend(struct device *dev) 1318 { 1319 struct rsnd_priv *priv = dev_get_drvdata(dev); 1320 1321 rsnd_adg_clk_disable(priv); 1322 1323 return 0; 1324 } 1325 1326 static int rsnd_resume(struct device *dev) 1327 { 1328 struct rsnd_priv *priv = dev_get_drvdata(dev); 1329 1330 rsnd_adg_clk_enable(priv); 1331 1332 return 0; 1333 } 1334 1335 static struct dev_pm_ops rsnd_pm_ops = { 1336 .suspend = rsnd_suspend, 1337 .resume = rsnd_resume, 1338 }; 1339 1340 static struct platform_driver rsnd_driver = { 1341 .driver = { 1342 .name = "rcar_sound", 1343 .pm = &rsnd_pm_ops, 1344 .of_match_table = rsnd_of_match, 1345 }, 1346 .probe = rsnd_probe, 1347 .remove = rsnd_remove, 1348 }; 1349 module_platform_driver(rsnd_driver); 1350 1351 MODULE_LICENSE("GPL"); 1352 MODULE_DESCRIPTION("Renesas R-Car audio driver"); 1353 MODULE_AUTHOR("Kuninori Morimoto <kuninori.morimoto.gx@renesas.com>"); 1354 MODULE_ALIAS("platform:rcar-pcm-audio"); 1355