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