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