1 // SPDX-License-Identifier: GPL-2.0 2 // 3 // Renesas R-Car SRC support 4 // 5 // Copyright (C) 2013 Renesas Solutions Corp. 6 // Kuninori Morimoto <kuninori.morimoto.gx@renesas.com> 7 8 /* 9 * you can enable below define if you don't need 10 * SSI interrupt status debug message when debugging 11 * see rsnd_dbg_irq_status() 12 * 13 * #define RSND_DEBUG_NO_IRQ_STATUS 1 14 */ 15 16 #include "rsnd.h" 17 18 #define SRC_NAME "src" 19 20 /* SCU_SYSTEM_STATUS0/1 */ 21 #define OUF_SRC(id) ((1 << (id + 16)) | (1 << id)) 22 23 struct rsnd_src { 24 struct rsnd_mod mod; 25 struct rsnd_mod *dma; 26 struct rsnd_kctrl_cfg_s sen; /* sync convert enable */ 27 struct rsnd_kctrl_cfg_s sync; /* sync convert */ 28 u32 convert_rate; /* sampling rate convert */ 29 int irq; 30 }; 31 32 #define RSND_SRC_NAME_SIZE 16 33 34 #define rsnd_src_get(priv, id) ((struct rsnd_src *)(priv->src) + id) 35 #define rsnd_src_nr(priv) ((priv)->src_nr) 36 #define rsnd_src_sync_is_enabled(mod) (rsnd_mod_to_src(mod)->sen.val) 37 38 #define rsnd_mod_to_src(_mod) \ 39 container_of((_mod), struct rsnd_src, mod) 40 41 #define for_each_rsnd_src(pos, priv, i) \ 42 for ((i) = 0; \ 43 ((i) < rsnd_src_nr(priv)) && \ 44 ((pos) = (struct rsnd_src *)(priv)->src + i); \ 45 i++) 46 47 48 /* 49 * image of SRC (Sampling Rate Converter) 50 * 51 * 96kHz <-> +-----+ 48kHz +-----+ 48kHz +-------+ 52 * 48kHz <-> | SRC | <------> | SSI | <-----> | codec | 53 * 44.1kHz <-> +-----+ +-----+ +-------+ 54 * ... 55 * 56 */ 57 58 static void rsnd_src_activation(struct rsnd_mod *mod) 59 { 60 rsnd_mod_write(mod, SRC_SWRSR, 0); 61 rsnd_mod_write(mod, SRC_SWRSR, 1); 62 } 63 64 static void rsnd_src_halt(struct rsnd_mod *mod) 65 { 66 rsnd_mod_write(mod, SRC_SRCIR, 1); 67 rsnd_mod_write(mod, SRC_SWRSR, 0); 68 } 69 70 static struct dma_chan *rsnd_src_dma_req(struct rsnd_dai_stream *io, 71 struct rsnd_mod *mod) 72 { 73 struct rsnd_priv *priv = rsnd_mod_to_priv(mod); 74 int is_play = rsnd_io_is_play(io); 75 76 return rsnd_dma_request_channel(rsnd_src_of_node(priv), 77 mod, 78 is_play ? "rx" : "tx"); 79 } 80 81 static u32 rsnd_src_convert_rate(struct rsnd_dai_stream *io, 82 struct rsnd_mod *mod) 83 { 84 struct snd_pcm_runtime *runtime = rsnd_io_to_runtime(io); 85 struct rsnd_src *src = rsnd_mod_to_src(mod); 86 u32 convert_rate; 87 88 if (!runtime) 89 return 0; 90 91 if (!rsnd_src_sync_is_enabled(mod)) 92 return src->convert_rate; 93 94 convert_rate = src->sync.val; 95 96 if (!convert_rate) 97 convert_rate = src->convert_rate; 98 99 if (!convert_rate) 100 convert_rate = runtime->rate; 101 102 return convert_rate; 103 } 104 105 unsigned int rsnd_src_get_rate(struct rsnd_priv *priv, 106 struct rsnd_dai_stream *io, 107 int is_in) 108 { 109 struct rsnd_mod *src_mod = rsnd_io_to_mod_src(io); 110 struct snd_pcm_runtime *runtime = rsnd_io_to_runtime(io); 111 unsigned int rate = 0; 112 int is_play = rsnd_io_is_play(io); 113 114 /* 115 * Playback 116 * runtime_rate -> [SRC] -> convert_rate 117 * 118 * Capture 119 * convert_rate -> [SRC] -> runtime_rate 120 */ 121 122 if (is_play == is_in) 123 return runtime->rate; 124 125 /* 126 * return convert rate if SRC is used, 127 * otherwise, return runtime->rate as usual 128 */ 129 if (src_mod) 130 rate = rsnd_src_convert_rate(io, src_mod); 131 132 if (!rate) 133 rate = runtime->rate; 134 135 return rate; 136 } 137 138 static int rsnd_src_hw_params(struct rsnd_mod *mod, 139 struct rsnd_dai_stream *io, 140 struct snd_pcm_substream *substream, 141 struct snd_pcm_hw_params *fe_params) 142 { 143 struct rsnd_src *src = rsnd_mod_to_src(mod); 144 struct snd_soc_pcm_runtime *fe = substream->private_data; 145 146 /* 147 * SRC assumes that it is used under DPCM if user want to use 148 * sampling rate convert. Then, SRC should be FE. 149 * And then, this function will be called *after* BE settings. 150 * this means, each BE already has fixuped hw_params. 151 * see 152 * dpcm_fe_dai_hw_params() 153 * dpcm_be_dai_hw_params() 154 */ 155 src->convert_rate = 0; 156 if (fe->dai_link->dynamic) { 157 int stream = substream->stream; 158 struct snd_soc_dpcm *dpcm; 159 struct snd_pcm_hw_params *be_params; 160 161 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) { 162 be_params = &dpcm->hw_params; 163 164 if (params_rate(fe_params) != params_rate(be_params)) 165 src->convert_rate = params_rate(be_params); 166 } 167 } 168 169 return 0; 170 } 171 172 static void rsnd_src_set_convert_rate(struct rsnd_dai_stream *io, 173 struct rsnd_mod *mod) 174 { 175 struct rsnd_priv *priv = rsnd_mod_to_priv(mod); 176 struct device *dev = rsnd_priv_to_dev(priv); 177 struct snd_pcm_runtime *runtime = rsnd_io_to_runtime(io); 178 int is_play = rsnd_io_is_play(io); 179 int use_src = 0; 180 u32 fin, fout; 181 u32 ifscr, fsrate, adinr; 182 u32 cr, route; 183 u32 bsdsr, bsisr; 184 u32 i_busif, o_busif, tmp; 185 uint ratio; 186 187 if (!runtime) 188 return; 189 190 fin = rsnd_src_get_in_rate(priv, io); 191 fout = rsnd_src_get_out_rate(priv, io); 192 193 /* 6 - 1/6 are very enough ratio for SRC_BSDSR */ 194 if (fin == fout) 195 ratio = 0; 196 else if (fin > fout) 197 ratio = 100 * fin / fout; 198 else 199 ratio = 100 * fout / fin; 200 201 if (ratio > 600) { 202 dev_err(dev, "FSO/FSI ratio error\n"); 203 return; 204 } 205 206 use_src = (fin != fout) | rsnd_src_sync_is_enabled(mod); 207 208 /* 209 * SRC_ADINR 210 */ 211 adinr = rsnd_get_adinr_bit(mod, io) | 212 rsnd_runtime_channel_original(io); 213 214 /* 215 * SRC_IFSCR / SRC_IFSVR 216 */ 217 ifscr = 0; 218 fsrate = 0; 219 if (use_src) { 220 u64 n; 221 222 ifscr = 1; 223 n = (u64)0x0400000 * fin; 224 do_div(n, fout); 225 fsrate = n; 226 } 227 228 /* 229 * SRC_SRCCR / SRC_ROUTE_MODE0 230 */ 231 cr = 0x00011110; 232 route = 0x0; 233 if (use_src) { 234 route = 0x1; 235 236 if (rsnd_src_sync_is_enabled(mod)) { 237 cr |= 0x1; 238 route |= rsnd_io_is_play(io) ? 239 (0x1 << 24) : (0x1 << 25); 240 } 241 } 242 243 /* 244 * SRC_BSDSR / SRC_BSISR 245 */ 246 switch (rsnd_mod_id(mod)) { 247 case 5: 248 case 6: 249 case 7: 250 case 8: 251 bsdsr = 0x02400000; /* 6 - 1/6 */ 252 bsisr = 0x00100060; /* 6 - 1/6 */ 253 break; 254 default: 255 bsdsr = 0x01800000; /* 6 - 1/6 */ 256 bsisr = 0x00100060 ;/* 6 - 1/6 */ 257 break; 258 } 259 260 /* BUSIF_MODE */ 261 tmp = rsnd_get_busif_shift(io, mod); 262 i_busif = ( is_play ? tmp : 0) | 1; 263 o_busif = (!is_play ? tmp : 0) | 1; 264 265 rsnd_mod_write(mod, SRC_ROUTE_MODE0, route); 266 267 rsnd_mod_write(mod, SRC_SRCIR, 1); /* initialize */ 268 rsnd_mod_write(mod, SRC_ADINR, adinr); 269 rsnd_mod_write(mod, SRC_IFSCR, ifscr); 270 rsnd_mod_write(mod, SRC_IFSVR, fsrate); 271 rsnd_mod_write(mod, SRC_SRCCR, cr); 272 rsnd_mod_write(mod, SRC_BSDSR, bsdsr); 273 rsnd_mod_write(mod, SRC_BSISR, bsisr); 274 rsnd_mod_write(mod, SRC_SRCIR, 0); /* cancel initialize */ 275 276 rsnd_mod_write(mod, SRC_I_BUSIF_MODE, i_busif); 277 rsnd_mod_write(mod, SRC_O_BUSIF_MODE, o_busif); 278 279 rsnd_mod_write(mod, SRC_BUSIF_DALIGN, rsnd_get_dalign(mod, io)); 280 281 rsnd_adg_set_src_timesel_gen2(mod, io, fin, fout); 282 } 283 284 static int rsnd_src_irq(struct rsnd_mod *mod, 285 struct rsnd_dai_stream *io, 286 struct rsnd_priv *priv, 287 int enable) 288 { 289 struct rsnd_src *src = rsnd_mod_to_src(mod); 290 u32 sys_int_val, int_val, sys_int_mask; 291 int irq = src->irq; 292 int id = rsnd_mod_id(mod); 293 294 sys_int_val = 295 sys_int_mask = OUF_SRC(id); 296 int_val = 0x3300; 297 298 /* 299 * IRQ is not supported on non-DT 300 * see 301 * rsnd_src_probe_() 302 */ 303 if ((irq <= 0) || !enable) { 304 sys_int_val = 0; 305 int_val = 0; 306 } 307 308 /* 309 * WORKAROUND 310 * 311 * ignore over flow error when rsnd_src_sync_is_enabled() 312 */ 313 if (rsnd_src_sync_is_enabled(mod)) 314 sys_int_val = sys_int_val & 0xffff; 315 316 rsnd_mod_write(mod, SRC_INT_ENABLE0, int_val); 317 rsnd_mod_bset(mod, SCU_SYS_INT_EN0, sys_int_mask, sys_int_val); 318 rsnd_mod_bset(mod, SCU_SYS_INT_EN1, sys_int_mask, sys_int_val); 319 320 return 0; 321 } 322 323 static void rsnd_src_status_clear(struct rsnd_mod *mod) 324 { 325 u32 val = OUF_SRC(rsnd_mod_id(mod)); 326 327 rsnd_mod_write(mod, SCU_SYS_STATUS0, val); 328 rsnd_mod_write(mod, SCU_SYS_STATUS1, val); 329 } 330 331 static bool rsnd_src_error_occurred(struct rsnd_mod *mod) 332 { 333 struct rsnd_priv *priv = rsnd_mod_to_priv(mod); 334 struct device *dev = rsnd_priv_to_dev(priv); 335 u32 val0, val1; 336 u32 status0, status1; 337 bool ret = false; 338 339 val0 = val1 = OUF_SRC(rsnd_mod_id(mod)); 340 341 /* 342 * WORKAROUND 343 * 344 * ignore over flow error when rsnd_src_sync_is_enabled() 345 */ 346 if (rsnd_src_sync_is_enabled(mod)) 347 val0 = val0 & 0xffff; 348 349 status0 = rsnd_mod_read(mod, SCU_SYS_STATUS0); 350 status1 = rsnd_mod_read(mod, SCU_SYS_STATUS1); 351 if ((status0 & val0) || (status1 & val1)) { 352 rsnd_dbg_irq_status(dev, "%s[%d] err status : 0x%08x, 0x%08x\n", 353 rsnd_mod_name(mod), rsnd_mod_id(mod), 354 status0, status1); 355 356 ret = true; 357 } 358 359 return ret; 360 } 361 362 static int rsnd_src_start(struct rsnd_mod *mod, 363 struct rsnd_dai_stream *io, 364 struct rsnd_priv *priv) 365 { 366 u32 val; 367 368 /* 369 * WORKAROUND 370 * 371 * Enable SRC output if you want to use sync convert together with DVC 372 */ 373 val = (rsnd_io_to_mod_dvc(io) && !rsnd_src_sync_is_enabled(mod)) ? 374 0x01 : 0x11; 375 376 rsnd_mod_write(mod, SRC_CTRL, val); 377 378 return 0; 379 } 380 381 static int rsnd_src_stop(struct rsnd_mod *mod, 382 struct rsnd_dai_stream *io, 383 struct rsnd_priv *priv) 384 { 385 rsnd_mod_write(mod, SRC_CTRL, 0); 386 387 return 0; 388 } 389 390 static int rsnd_src_init(struct rsnd_mod *mod, 391 struct rsnd_dai_stream *io, 392 struct rsnd_priv *priv) 393 { 394 struct rsnd_src *src = rsnd_mod_to_src(mod); 395 396 /* reset sync convert_rate */ 397 src->sync.val = 0; 398 399 rsnd_mod_power_on(mod); 400 401 rsnd_src_activation(mod); 402 403 rsnd_src_set_convert_rate(io, mod); 404 405 rsnd_src_status_clear(mod); 406 407 return 0; 408 } 409 410 static int rsnd_src_quit(struct rsnd_mod *mod, 411 struct rsnd_dai_stream *io, 412 struct rsnd_priv *priv) 413 { 414 struct rsnd_src *src = rsnd_mod_to_src(mod); 415 416 rsnd_src_halt(mod); 417 418 rsnd_mod_power_off(mod); 419 420 /* reset sync convert_rate */ 421 src->sync.val = 0; 422 423 return 0; 424 } 425 426 static void __rsnd_src_interrupt(struct rsnd_mod *mod, 427 struct rsnd_dai_stream *io) 428 { 429 struct rsnd_priv *priv = rsnd_mod_to_priv(mod); 430 bool stop = false; 431 432 spin_lock(&priv->lock); 433 434 /* ignore all cases if not working */ 435 if (!rsnd_io_is_working(io)) 436 goto rsnd_src_interrupt_out; 437 438 if (rsnd_src_error_occurred(mod)) 439 stop = true; 440 441 rsnd_src_status_clear(mod); 442 rsnd_src_interrupt_out: 443 444 spin_unlock(&priv->lock); 445 446 if (stop) 447 snd_pcm_stop_xrun(io->substream); 448 } 449 450 static irqreturn_t rsnd_src_interrupt(int irq, void *data) 451 { 452 struct rsnd_mod *mod = data; 453 454 rsnd_mod_interrupt(mod, __rsnd_src_interrupt); 455 456 return IRQ_HANDLED; 457 } 458 459 static int rsnd_src_probe_(struct rsnd_mod *mod, 460 struct rsnd_dai_stream *io, 461 struct rsnd_priv *priv) 462 { 463 struct rsnd_src *src = rsnd_mod_to_src(mod); 464 struct device *dev = rsnd_priv_to_dev(priv); 465 int irq = src->irq; 466 int ret; 467 468 if (irq > 0) { 469 /* 470 * IRQ is not supported on non-DT 471 * see 472 * rsnd_src_irq() 473 */ 474 ret = devm_request_irq(dev, irq, 475 rsnd_src_interrupt, 476 IRQF_SHARED, 477 dev_name(dev), mod); 478 if (ret) 479 return ret; 480 } 481 482 ret = rsnd_dma_attach(io, mod, &src->dma); 483 484 return ret; 485 } 486 487 static int rsnd_src_pcm_new(struct rsnd_mod *mod, 488 struct rsnd_dai_stream *io, 489 struct snd_soc_pcm_runtime *rtd) 490 { 491 struct rsnd_src *src = rsnd_mod_to_src(mod); 492 int ret; 493 494 /* 495 * enable SRC sync convert if possible 496 */ 497 498 /* 499 * It can't use SRC Synchronous convert 500 * when Capture if it uses CMD 501 */ 502 if (rsnd_io_to_mod_cmd(io) && !rsnd_io_is_play(io)) 503 return 0; 504 505 /* 506 * enable sync convert 507 */ 508 ret = rsnd_kctrl_new_s(mod, io, rtd, 509 rsnd_io_is_play(io) ? 510 "SRC Out Rate Switch" : 511 "SRC In Rate Switch", 512 rsnd_kctrl_accept_anytime, 513 rsnd_src_set_convert_rate, 514 &src->sen, 1); 515 if (ret < 0) 516 return ret; 517 518 ret = rsnd_kctrl_new_s(mod, io, rtd, 519 rsnd_io_is_play(io) ? 520 "SRC Out Rate" : 521 "SRC In Rate", 522 rsnd_kctrl_accept_runtime, 523 rsnd_src_set_convert_rate, 524 &src->sync, 192000); 525 526 return ret; 527 } 528 529 static struct rsnd_mod_ops rsnd_src_ops = { 530 .name = SRC_NAME, 531 .dma_req = rsnd_src_dma_req, 532 .probe = rsnd_src_probe_, 533 .init = rsnd_src_init, 534 .quit = rsnd_src_quit, 535 .start = rsnd_src_start, 536 .stop = rsnd_src_stop, 537 .irq = rsnd_src_irq, 538 .hw_params = rsnd_src_hw_params, 539 .pcm_new = rsnd_src_pcm_new, 540 }; 541 542 struct rsnd_mod *rsnd_src_mod_get(struct rsnd_priv *priv, int id) 543 { 544 if (WARN_ON(id < 0 || id >= rsnd_src_nr(priv))) 545 id = 0; 546 547 return rsnd_mod_get(rsnd_src_get(priv, id)); 548 } 549 550 int rsnd_src_probe(struct rsnd_priv *priv) 551 { 552 struct device_node *node; 553 struct device_node *np; 554 struct device *dev = rsnd_priv_to_dev(priv); 555 struct rsnd_src *src; 556 struct clk *clk; 557 char name[RSND_SRC_NAME_SIZE]; 558 int i, nr, ret; 559 560 /* This driver doesn't support Gen1 at this point */ 561 if (rsnd_is_gen1(priv)) 562 return 0; 563 564 node = rsnd_src_of_node(priv); 565 if (!node) 566 return 0; /* not used is not error */ 567 568 nr = of_get_child_count(node); 569 if (!nr) { 570 ret = -EINVAL; 571 goto rsnd_src_probe_done; 572 } 573 574 src = devm_kcalloc(dev, nr, sizeof(*src), GFP_KERNEL); 575 if (!src) { 576 ret = -ENOMEM; 577 goto rsnd_src_probe_done; 578 } 579 580 priv->src_nr = nr; 581 priv->src = src; 582 583 i = 0; 584 for_each_child_of_node(node, np) { 585 if (!of_device_is_available(np)) 586 goto skip; 587 588 src = rsnd_src_get(priv, i); 589 590 snprintf(name, RSND_SRC_NAME_SIZE, "%s.%d", 591 SRC_NAME, i); 592 593 src->irq = irq_of_parse_and_map(np, 0); 594 if (!src->irq) { 595 ret = -EINVAL; 596 of_node_put(np); 597 goto rsnd_src_probe_done; 598 } 599 600 clk = devm_clk_get(dev, name); 601 if (IS_ERR(clk)) { 602 ret = PTR_ERR(clk); 603 of_node_put(np); 604 goto rsnd_src_probe_done; 605 } 606 607 ret = rsnd_mod_init(priv, rsnd_mod_get(src), 608 &rsnd_src_ops, clk, rsnd_mod_get_status, 609 RSND_MOD_SRC, i); 610 if (ret) { 611 of_node_put(np); 612 goto rsnd_src_probe_done; 613 } 614 615 skip: 616 i++; 617 } 618 619 ret = 0; 620 621 rsnd_src_probe_done: 622 of_node_put(node); 623 624 return ret; 625 } 626 627 void rsnd_src_remove(struct rsnd_priv *priv) 628 { 629 struct rsnd_src *src; 630 int i; 631 632 for_each_rsnd_src(src, priv, i) { 633 rsnd_mod_quit(rsnd_mod_get(src)); 634 } 635 } 636