0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018
0019
0020
0021
0022
0023
0024
0025 #include "rsnd.h"
0026
0027 #define SRC_NAME "src"
0028
0029
0030 #define OUF_SRC(id) ((1 << (id + 16)) | (1 << id))
0031
0032 struct rsnd_src {
0033 struct rsnd_mod mod;
0034 struct rsnd_mod *dma;
0035 struct rsnd_kctrl_cfg_s sen;
0036 struct rsnd_kctrl_cfg_s sync;
0037 int irq;
0038 };
0039
0040 #define RSND_SRC_NAME_SIZE 16
0041
0042 #define rsnd_src_get(priv, id) ((struct rsnd_src *)(priv->src) + id)
0043 #define rsnd_src_nr(priv) ((priv)->src_nr)
0044 #define rsnd_src_sync_is_enabled(mod) (rsnd_mod_to_src(mod)->sen.val)
0045
0046 #define rsnd_mod_to_src(_mod) \
0047 container_of((_mod), struct rsnd_src, mod)
0048
0049 #define for_each_rsnd_src(pos, priv, i) \
0050 for ((i) = 0; \
0051 ((i) < rsnd_src_nr(priv)) && \
0052 ((pos) = (struct rsnd_src *)(priv)->src + i); \
0053 i++)
0054
0055
0056
0057
0058
0059
0060
0061
0062
0063
0064
0065
0066 static void rsnd_src_activation(struct rsnd_mod *mod)
0067 {
0068 rsnd_mod_write(mod, SRC_SWRSR, 0);
0069 rsnd_mod_write(mod, SRC_SWRSR, 1);
0070 }
0071
0072 static void rsnd_src_halt(struct rsnd_mod *mod)
0073 {
0074 rsnd_mod_write(mod, SRC_SRCIR, 1);
0075 rsnd_mod_write(mod, SRC_SWRSR, 0);
0076 }
0077
0078 static struct dma_chan *rsnd_src_dma_req(struct rsnd_dai_stream *io,
0079 struct rsnd_mod *mod)
0080 {
0081 struct rsnd_priv *priv = rsnd_mod_to_priv(mod);
0082 int is_play = rsnd_io_is_play(io);
0083
0084 return rsnd_dma_request_channel(rsnd_src_of_node(priv),
0085 SRC_NAME, mod,
0086 is_play ? "rx" : "tx");
0087 }
0088
0089 static u32 rsnd_src_convert_rate(struct rsnd_dai_stream *io,
0090 struct rsnd_mod *mod)
0091 {
0092 struct snd_pcm_runtime *runtime = rsnd_io_to_runtime(io);
0093 struct rsnd_src *src = rsnd_mod_to_src(mod);
0094 u32 convert_rate;
0095
0096 if (!runtime)
0097 return 0;
0098
0099 if (!rsnd_src_sync_is_enabled(mod))
0100 return rsnd_io_converted_rate(io);
0101
0102 convert_rate = src->sync.val;
0103
0104 if (!convert_rate)
0105 convert_rate = rsnd_io_converted_rate(io);
0106
0107 if (!convert_rate)
0108 convert_rate = runtime->rate;
0109
0110 return convert_rate;
0111 }
0112
0113 unsigned int rsnd_src_get_rate(struct rsnd_priv *priv,
0114 struct rsnd_dai_stream *io,
0115 int is_in)
0116 {
0117 struct rsnd_mod *src_mod = rsnd_io_to_mod_src(io);
0118 struct snd_pcm_runtime *runtime = rsnd_io_to_runtime(io);
0119 unsigned int rate = 0;
0120 int is_play = rsnd_io_is_play(io);
0121
0122
0123
0124
0125
0126
0127
0128
0129
0130 if (is_play == is_in)
0131 return runtime->rate;
0132
0133
0134
0135
0136
0137 if (src_mod)
0138 rate = rsnd_src_convert_rate(io, src_mod);
0139
0140 if (!rate)
0141 rate = runtime->rate;
0142
0143 return rate;
0144 }
0145
0146 static const u32 bsdsr_table_pattern1[] = {
0147 0x01800000,
0148 0x01000000,
0149 0x00c00000,
0150 0x00800000,
0151 0x00600000,
0152 0x00400000,
0153 };
0154
0155 static const u32 bsdsr_table_pattern2[] = {
0156 0x02400000,
0157 0x01800000,
0158 0x01200000,
0159 0x00c00000,
0160 0x00900000,
0161 0x00600000,
0162 };
0163
0164 static const u32 bsisr_table[] = {
0165 0x00100060,
0166 0x00100040,
0167 0x00100030,
0168 0x00100020,
0169 0x00100020,
0170 0x00100020,
0171 };
0172
0173 static const u32 chan288888[] = {
0174 0x00000006,
0175 0x000001fe,
0176 0x000001fe,
0177 0x000001fe,
0178 0x000001fe,
0179 0x000001fe,
0180 };
0181
0182 static const u32 chan244888[] = {
0183 0x00000006,
0184 0x0000001e,
0185 0x0000001e,
0186 0x000001fe,
0187 0x000001fe,
0188 0x000001fe,
0189 };
0190
0191 static const u32 chan222222[] = {
0192 0x00000006,
0193 0x00000006,
0194 0x00000006,
0195 0x00000006,
0196 0x00000006,
0197 0x00000006,
0198 };
0199
0200 static void rsnd_src_set_convert_rate(struct rsnd_dai_stream *io,
0201 struct rsnd_mod *mod)
0202 {
0203 struct rsnd_priv *priv = rsnd_mod_to_priv(mod);
0204 struct device *dev = rsnd_priv_to_dev(priv);
0205 struct snd_pcm_runtime *runtime = rsnd_io_to_runtime(io);
0206 int is_play = rsnd_io_is_play(io);
0207 int use_src = 0;
0208 u32 fin, fout;
0209 u32 ifscr, fsrate, adinr;
0210 u32 cr, route;
0211 u32 i_busif, o_busif, tmp;
0212 const u32 *bsdsr_table;
0213 const u32 *chptn;
0214 uint ratio;
0215 int chan;
0216 int idx;
0217
0218 if (!runtime)
0219 return;
0220
0221 fin = rsnd_src_get_in_rate(priv, io);
0222 fout = rsnd_src_get_out_rate(priv, io);
0223
0224 chan = rsnd_runtime_channel_original(io);
0225
0226
0227 if (fin == fout)
0228 ratio = 0;
0229 else if (fin > fout)
0230 ratio = 100 * fin / fout;
0231 else
0232 ratio = 100 * fout / fin;
0233
0234 if (ratio > 600) {
0235 dev_err(dev, "FSO/FSI ratio error\n");
0236 return;
0237 }
0238
0239 use_src = (fin != fout) | rsnd_src_sync_is_enabled(mod);
0240
0241
0242
0243
0244 adinr = rsnd_get_adinr_bit(mod, io) | chan;
0245
0246
0247
0248
0249 ifscr = 0;
0250 fsrate = 0;
0251 if (use_src) {
0252 u64 n;
0253
0254 ifscr = 1;
0255 n = (u64)0x0400000 * fin;
0256 do_div(n, fout);
0257 fsrate = n;
0258 }
0259
0260
0261
0262
0263 cr = 0x00011110;
0264 route = 0x0;
0265 if (use_src) {
0266 route = 0x1;
0267
0268 if (rsnd_src_sync_is_enabled(mod)) {
0269 cr |= 0x1;
0270 route |= rsnd_io_is_play(io) ?
0271 (0x1 << 24) : (0x1 << 25);
0272 }
0273 }
0274
0275
0276
0277
0278
0279
0280
0281
0282 switch (rsnd_mod_id(mod)) {
0283 case 0:
0284 chptn = chan288888;
0285 bsdsr_table = bsdsr_table_pattern1;
0286 break;
0287 case 1:
0288 case 3:
0289 case 4:
0290 chptn = chan244888;
0291 bsdsr_table = bsdsr_table_pattern1;
0292 break;
0293 case 2:
0294 case 9:
0295 chptn = chan222222;
0296 bsdsr_table = bsdsr_table_pattern1;
0297 break;
0298 case 5:
0299 case 6:
0300 case 7:
0301 case 8:
0302 chptn = chan222222;
0303 bsdsr_table = bsdsr_table_pattern2;
0304 break;
0305 default:
0306 goto convert_rate_err;
0307 }
0308
0309
0310
0311
0312 if (rsnd_is_e3(priv))
0313 switch (rsnd_mod_id(mod)) {
0314 case 0:
0315 case 4:
0316 chptn = chan222222;
0317 }
0318
0319 for (idx = 0; idx < ARRAY_SIZE(chan222222); idx++)
0320 if (chptn[idx] & (1 << chan))
0321 break;
0322
0323 if (chan > 8 ||
0324 idx >= ARRAY_SIZE(chan222222))
0325 goto convert_rate_err;
0326
0327
0328 tmp = rsnd_get_busif_shift(io, mod);
0329 i_busif = ( is_play ? tmp : 0) | 1;
0330 o_busif = (!is_play ? tmp : 0) | 1;
0331
0332 rsnd_mod_write(mod, SRC_ROUTE_MODE0, route);
0333
0334 rsnd_mod_write(mod, SRC_SRCIR, 1);
0335 rsnd_mod_write(mod, SRC_ADINR, adinr);
0336 rsnd_mod_write(mod, SRC_IFSCR, ifscr);
0337 rsnd_mod_write(mod, SRC_IFSVR, fsrate);
0338 rsnd_mod_write(mod, SRC_SRCCR, cr);
0339 rsnd_mod_write(mod, SRC_BSDSR, bsdsr_table[idx]);
0340 rsnd_mod_write(mod, SRC_BSISR, bsisr_table[idx]);
0341 rsnd_mod_write(mod, SRC_SRCIR, 0);
0342
0343 rsnd_mod_write(mod, SRC_I_BUSIF_MODE, i_busif);
0344 rsnd_mod_write(mod, SRC_O_BUSIF_MODE, o_busif);
0345
0346 rsnd_mod_write(mod, SRC_BUSIF_DALIGN, rsnd_get_dalign(mod, io));
0347
0348 rsnd_adg_set_src_timesel_gen2(mod, io, fin, fout);
0349
0350 return;
0351
0352 convert_rate_err:
0353 dev_err(dev, "unknown BSDSR/BSDIR settings\n");
0354 }
0355
0356 static int rsnd_src_irq(struct rsnd_mod *mod,
0357 struct rsnd_dai_stream *io,
0358 struct rsnd_priv *priv,
0359 int enable)
0360 {
0361 struct rsnd_src *src = rsnd_mod_to_src(mod);
0362 u32 sys_int_val, int_val, sys_int_mask;
0363 int irq = src->irq;
0364 int id = rsnd_mod_id(mod);
0365
0366 sys_int_val =
0367 sys_int_mask = OUF_SRC(id);
0368 int_val = 0x3300;
0369
0370
0371
0372
0373
0374
0375 if ((irq <= 0) || !enable) {
0376 sys_int_val = 0;
0377 int_val = 0;
0378 }
0379
0380
0381
0382
0383
0384
0385 if (rsnd_src_sync_is_enabled(mod))
0386 sys_int_val = sys_int_val & 0xffff;
0387
0388 rsnd_mod_write(mod, SRC_INT_ENABLE0, int_val);
0389 rsnd_mod_bset(mod, SCU_SYS_INT_EN0, sys_int_mask, sys_int_val);
0390 rsnd_mod_bset(mod, SCU_SYS_INT_EN1, sys_int_mask, sys_int_val);
0391
0392 return 0;
0393 }
0394
0395 static void rsnd_src_status_clear(struct rsnd_mod *mod)
0396 {
0397 u32 val = OUF_SRC(rsnd_mod_id(mod));
0398
0399 rsnd_mod_write(mod, SCU_SYS_STATUS0, val);
0400 rsnd_mod_write(mod, SCU_SYS_STATUS1, val);
0401 }
0402
0403 static bool rsnd_src_error_occurred(struct rsnd_mod *mod)
0404 {
0405 struct rsnd_priv *priv = rsnd_mod_to_priv(mod);
0406 struct device *dev = rsnd_priv_to_dev(priv);
0407 u32 val0, val1;
0408 u32 status0, status1;
0409 bool ret = false;
0410
0411 val0 = val1 = OUF_SRC(rsnd_mod_id(mod));
0412
0413
0414
0415
0416
0417
0418 if (rsnd_src_sync_is_enabled(mod))
0419 val0 = val0 & 0xffff;
0420
0421 status0 = rsnd_mod_read(mod, SCU_SYS_STATUS0);
0422 status1 = rsnd_mod_read(mod, SCU_SYS_STATUS1);
0423 if ((status0 & val0) || (status1 & val1)) {
0424 rsnd_print_irq_status(dev, "%s err status : 0x%08x, 0x%08x\n",
0425 rsnd_mod_name(mod), status0, status1);
0426
0427 ret = true;
0428 }
0429
0430 return ret;
0431 }
0432
0433 static int rsnd_src_start(struct rsnd_mod *mod,
0434 struct rsnd_dai_stream *io,
0435 struct rsnd_priv *priv)
0436 {
0437 u32 val;
0438
0439
0440
0441
0442
0443
0444 val = (rsnd_io_to_mod_dvc(io) && !rsnd_src_sync_is_enabled(mod)) ?
0445 0x01 : 0x11;
0446
0447 rsnd_mod_write(mod, SRC_CTRL, val);
0448
0449 return 0;
0450 }
0451
0452 static int rsnd_src_stop(struct rsnd_mod *mod,
0453 struct rsnd_dai_stream *io,
0454 struct rsnd_priv *priv)
0455 {
0456 rsnd_mod_write(mod, SRC_CTRL, 0);
0457
0458 return 0;
0459 }
0460
0461 static int rsnd_src_init(struct rsnd_mod *mod,
0462 struct rsnd_dai_stream *io,
0463 struct rsnd_priv *priv)
0464 {
0465 struct rsnd_src *src = rsnd_mod_to_src(mod);
0466
0467
0468 src->sync.val = 0;
0469
0470 rsnd_mod_power_on(mod);
0471
0472 rsnd_src_activation(mod);
0473
0474 rsnd_src_set_convert_rate(io, mod);
0475
0476 rsnd_src_status_clear(mod);
0477
0478 return 0;
0479 }
0480
0481 static int rsnd_src_quit(struct rsnd_mod *mod,
0482 struct rsnd_dai_stream *io,
0483 struct rsnd_priv *priv)
0484 {
0485 struct rsnd_src *src = rsnd_mod_to_src(mod);
0486
0487 rsnd_src_halt(mod);
0488
0489 rsnd_mod_power_off(mod);
0490
0491
0492 src->sync.val = 0;
0493
0494 return 0;
0495 }
0496
0497 static void __rsnd_src_interrupt(struct rsnd_mod *mod,
0498 struct rsnd_dai_stream *io)
0499 {
0500 struct rsnd_priv *priv = rsnd_mod_to_priv(mod);
0501 bool stop = false;
0502
0503 spin_lock(&priv->lock);
0504
0505
0506 if (!rsnd_io_is_working(io))
0507 goto rsnd_src_interrupt_out;
0508
0509 if (rsnd_src_error_occurred(mod))
0510 stop = true;
0511
0512 rsnd_src_status_clear(mod);
0513 rsnd_src_interrupt_out:
0514
0515 spin_unlock(&priv->lock);
0516
0517 if (stop)
0518 snd_pcm_stop_xrun(io->substream);
0519 }
0520
0521 static irqreturn_t rsnd_src_interrupt(int irq, void *data)
0522 {
0523 struct rsnd_mod *mod = data;
0524
0525 rsnd_mod_interrupt(mod, __rsnd_src_interrupt);
0526
0527 return IRQ_HANDLED;
0528 }
0529
0530 static int rsnd_src_probe_(struct rsnd_mod *mod,
0531 struct rsnd_dai_stream *io,
0532 struct rsnd_priv *priv)
0533 {
0534 struct rsnd_src *src = rsnd_mod_to_src(mod);
0535 struct device *dev = rsnd_priv_to_dev(priv);
0536 int irq = src->irq;
0537 int ret;
0538
0539 if (irq > 0) {
0540
0541
0542
0543
0544
0545 ret = devm_request_irq(dev, irq,
0546 rsnd_src_interrupt,
0547 IRQF_SHARED,
0548 dev_name(dev), mod);
0549 if (ret)
0550 return ret;
0551 }
0552
0553 ret = rsnd_dma_attach(io, mod, &src->dma);
0554
0555 return ret;
0556 }
0557
0558 static int rsnd_src_pcm_new(struct rsnd_mod *mod,
0559 struct rsnd_dai_stream *io,
0560 struct snd_soc_pcm_runtime *rtd)
0561 {
0562 struct rsnd_src *src = rsnd_mod_to_src(mod);
0563 int ret;
0564
0565
0566
0567
0568
0569
0570
0571
0572
0573 if (rsnd_io_to_mod_cmd(io) && !rsnd_io_is_play(io))
0574 return 0;
0575
0576
0577
0578
0579 ret = rsnd_kctrl_new_s(mod, io, rtd,
0580 rsnd_io_is_play(io) ?
0581 "SRC Out Rate Switch" :
0582 "SRC In Rate Switch",
0583 rsnd_kctrl_accept_anytime,
0584 rsnd_src_set_convert_rate,
0585 &src->sen, 1);
0586 if (ret < 0)
0587 return ret;
0588
0589 ret = rsnd_kctrl_new_s(mod, io, rtd,
0590 rsnd_io_is_play(io) ?
0591 "SRC Out Rate" :
0592 "SRC In Rate",
0593 rsnd_kctrl_accept_runtime,
0594 rsnd_src_set_convert_rate,
0595 &src->sync, 192000);
0596
0597 return ret;
0598 }
0599
0600 #ifdef CONFIG_DEBUG_FS
0601 static void rsnd_src_debug_info(struct seq_file *m,
0602 struct rsnd_dai_stream *io,
0603 struct rsnd_mod *mod)
0604 {
0605 rsnd_debugfs_mod_reg_show(m, mod, RSND_GEN2_SCU,
0606 rsnd_mod_id(mod) * 0x20, 0x20);
0607 seq_puts(m, "\n");
0608 rsnd_debugfs_mod_reg_show(m, mod, RSND_GEN2_SCU,
0609 0x1c0, 0x20);
0610 seq_puts(m, "\n");
0611 rsnd_debugfs_mod_reg_show(m, mod, RSND_GEN2_SCU,
0612 0x200 + rsnd_mod_id(mod) * 0x40, 0x40);
0613 }
0614 #define DEBUG_INFO .debug_info = rsnd_src_debug_info
0615 #else
0616 #define DEBUG_INFO
0617 #endif
0618
0619 static struct rsnd_mod_ops rsnd_src_ops = {
0620 .name = SRC_NAME,
0621 .dma_req = rsnd_src_dma_req,
0622 .probe = rsnd_src_probe_,
0623 .init = rsnd_src_init,
0624 .quit = rsnd_src_quit,
0625 .start = rsnd_src_start,
0626 .stop = rsnd_src_stop,
0627 .irq = rsnd_src_irq,
0628 .pcm_new = rsnd_src_pcm_new,
0629 .get_status = rsnd_mod_get_status,
0630 DEBUG_INFO
0631 };
0632
0633 struct rsnd_mod *rsnd_src_mod_get(struct rsnd_priv *priv, int id)
0634 {
0635 if (WARN_ON(id < 0 || id >= rsnd_src_nr(priv)))
0636 id = 0;
0637
0638 return rsnd_mod_get(rsnd_src_get(priv, id));
0639 }
0640
0641 int rsnd_src_probe(struct rsnd_priv *priv)
0642 {
0643 struct device_node *node;
0644 struct device_node *np;
0645 struct device *dev = rsnd_priv_to_dev(priv);
0646 struct rsnd_src *src;
0647 struct clk *clk;
0648 char name[RSND_SRC_NAME_SIZE];
0649 int i, nr, ret;
0650
0651
0652 if (rsnd_is_gen1(priv))
0653 return 0;
0654
0655 node = rsnd_src_of_node(priv);
0656 if (!node)
0657 return 0;
0658
0659 nr = rsnd_node_count(priv, node, SRC_NAME);
0660 if (!nr) {
0661 ret = -EINVAL;
0662 goto rsnd_src_probe_done;
0663 }
0664
0665 src = devm_kcalloc(dev, nr, sizeof(*src), GFP_KERNEL);
0666 if (!src) {
0667 ret = -ENOMEM;
0668 goto rsnd_src_probe_done;
0669 }
0670
0671 priv->src_nr = nr;
0672 priv->src = src;
0673
0674 i = 0;
0675 for_each_child_of_node(node, np) {
0676 if (!of_device_is_available(np))
0677 goto skip;
0678
0679 i = rsnd_node_fixed_index(dev, np, SRC_NAME, i);
0680 if (i < 0) {
0681 ret = -EINVAL;
0682 of_node_put(np);
0683 goto rsnd_src_probe_done;
0684 }
0685
0686 src = rsnd_src_get(priv, i);
0687
0688 snprintf(name, RSND_SRC_NAME_SIZE, "%s.%d",
0689 SRC_NAME, i);
0690
0691 src->irq = irq_of_parse_and_map(np, 0);
0692 if (!src->irq) {
0693 ret = -EINVAL;
0694 of_node_put(np);
0695 goto rsnd_src_probe_done;
0696 }
0697
0698 clk = devm_clk_get(dev, name);
0699 if (IS_ERR(clk)) {
0700 ret = PTR_ERR(clk);
0701 of_node_put(np);
0702 goto rsnd_src_probe_done;
0703 }
0704
0705 ret = rsnd_mod_init(priv, rsnd_mod_get(src),
0706 &rsnd_src_ops, clk, RSND_MOD_SRC, i);
0707 if (ret) {
0708 of_node_put(np);
0709 goto rsnd_src_probe_done;
0710 }
0711
0712 skip:
0713 i++;
0714 }
0715
0716 ret = 0;
0717
0718 rsnd_src_probe_done:
0719 of_node_put(node);
0720
0721 return ret;
0722 }
0723
0724 void rsnd_src_remove(struct rsnd_priv *priv)
0725 {
0726 struct rsnd_src *src;
0727 int i;
0728
0729 for_each_rsnd_src(src, priv, i) {
0730 rsnd_mod_quit(rsnd_mod_get(src));
0731 }
0732 }