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
0026
0027
0028
0029
0030
0031
0032
0033
0034
0035
0036
0037
0038
0039
0040
0041
0042
0043
0044
0045
0046
0047
0048
0049
0050
0051
0052
0053
0054
0055
0056
0057
0058
0059
0060
0061
0062
0063
0064
0065
0066
0067
0068
0069
0070
0071
0072
0073
0074
0075
0076
0077
0078
0079
0080
0081
0082
0083
0084
0085
0086
0087
0088
0089
0090
0091
0092
0093 #include <linux/pm_runtime.h>
0094 #include "rsnd.h"
0095
0096 #define RSND_RATES SNDRV_PCM_RATE_8000_192000
0097 #define RSND_FMTS (SNDRV_PCM_FMTBIT_S8 |\
0098 SNDRV_PCM_FMTBIT_S16_LE |\
0099 SNDRV_PCM_FMTBIT_S24_LE)
0100
0101 static const struct of_device_id rsnd_of_match[] = {
0102 { .compatible = "renesas,rcar_sound-gen1", .data = (void *)RSND_GEN1 },
0103 { .compatible = "renesas,rcar_sound-gen2", .data = (void *)RSND_GEN2 },
0104 { .compatible = "renesas,rcar_sound-gen3", .data = (void *)RSND_GEN3 },
0105
0106 { .compatible = "renesas,rcar_sound-r8a77990", .data = (void *)(RSND_GEN3 | RSND_SOC_E) },
0107 {},
0108 };
0109 MODULE_DEVICE_TABLE(of, rsnd_of_match);
0110
0111
0112
0113
0114 void rsnd_mod_make_sure(struct rsnd_mod *mod, enum rsnd_mod_type type)
0115 {
0116 if (mod->type != type) {
0117 struct rsnd_priv *priv = rsnd_mod_to_priv(mod);
0118 struct device *dev = rsnd_priv_to_dev(priv);
0119
0120 dev_warn(dev, "%s is not your expected module\n",
0121 rsnd_mod_name(mod));
0122 }
0123 }
0124
0125 struct dma_chan *rsnd_mod_dma_req(struct rsnd_dai_stream *io,
0126 struct rsnd_mod *mod)
0127 {
0128 if (!mod || !mod->ops || !mod->ops->dma_req)
0129 return NULL;
0130
0131 return mod->ops->dma_req(io, mod);
0132 }
0133
0134 #define MOD_NAME_NUM 5
0135 #define MOD_NAME_SIZE 16
0136 char *rsnd_mod_name(struct rsnd_mod *mod)
0137 {
0138 static char names[MOD_NAME_NUM][MOD_NAME_SIZE];
0139 static int num;
0140 char *name = names[num];
0141
0142 num++;
0143 if (num >= MOD_NAME_NUM)
0144 num = 0;
0145
0146
0147
0148
0149
0150
0151 if ((mod)->ops->id_sub) {
0152 snprintf(name, MOD_NAME_SIZE, "%s[%d%d]",
0153 mod->ops->name,
0154 rsnd_mod_id(mod),
0155 rsnd_mod_id_sub(mod));
0156 } else {
0157 snprintf(name, MOD_NAME_SIZE, "%s[%d]",
0158 mod->ops->name,
0159 rsnd_mod_id(mod));
0160 }
0161
0162 return name;
0163 }
0164
0165 u32 *rsnd_mod_get_status(struct rsnd_mod *mod,
0166 struct rsnd_dai_stream *io,
0167 enum rsnd_mod_type type)
0168 {
0169 return &mod->status;
0170 }
0171
0172 int rsnd_mod_id_raw(struct rsnd_mod *mod)
0173 {
0174 return mod->id;
0175 }
0176
0177 int rsnd_mod_id(struct rsnd_mod *mod)
0178 {
0179 if ((mod)->ops->id)
0180 return (mod)->ops->id(mod);
0181
0182 return rsnd_mod_id_raw(mod);
0183 }
0184
0185 int rsnd_mod_id_sub(struct rsnd_mod *mod)
0186 {
0187 if ((mod)->ops->id_sub)
0188 return (mod)->ops->id_sub(mod);
0189
0190 return 0;
0191 }
0192
0193 int rsnd_mod_init(struct rsnd_priv *priv,
0194 struct rsnd_mod *mod,
0195 struct rsnd_mod_ops *ops,
0196 struct clk *clk,
0197 enum rsnd_mod_type type,
0198 int id)
0199 {
0200 int ret = clk_prepare(clk);
0201
0202 if (ret)
0203 return ret;
0204
0205 mod->id = id;
0206 mod->ops = ops;
0207 mod->type = type;
0208 mod->clk = clk;
0209 mod->priv = priv;
0210
0211 return 0;
0212 }
0213
0214 void rsnd_mod_quit(struct rsnd_mod *mod)
0215 {
0216 clk_unprepare(mod->clk);
0217 mod->clk = NULL;
0218 }
0219
0220 void rsnd_mod_interrupt(struct rsnd_mod *mod,
0221 void (*callback)(struct rsnd_mod *mod,
0222 struct rsnd_dai_stream *io))
0223 {
0224 struct rsnd_priv *priv = rsnd_mod_to_priv(mod);
0225 struct rsnd_dai *rdai;
0226 int i;
0227
0228 for_each_rsnd_dai(rdai, priv, i) {
0229 struct rsnd_dai_stream *io = &rdai->playback;
0230
0231 if (mod == io->mod[mod->type])
0232 callback(mod, io);
0233
0234 io = &rdai->capture;
0235 if (mod == io->mod[mod->type])
0236 callback(mod, io);
0237 }
0238 }
0239
0240 int rsnd_io_is_working(struct rsnd_dai_stream *io)
0241 {
0242
0243 if (io->substream)
0244 return snd_pcm_running(io->substream);
0245
0246 return 0;
0247 }
0248
0249 int rsnd_runtime_channel_original_with_params(struct rsnd_dai_stream *io,
0250 struct snd_pcm_hw_params *params)
0251 {
0252 struct snd_pcm_runtime *runtime = rsnd_io_to_runtime(io);
0253
0254
0255
0256
0257
0258
0259
0260 if (params)
0261 return params_channels(params);
0262 else if (runtime)
0263 return runtime->channels;
0264 return 0;
0265 }
0266
0267 int rsnd_runtime_channel_after_ctu_with_params(struct rsnd_dai_stream *io,
0268 struct snd_pcm_hw_params *params)
0269 {
0270 int chan = rsnd_runtime_channel_original_with_params(io, params);
0271 struct rsnd_mod *ctu_mod = rsnd_io_to_mod_ctu(io);
0272
0273 if (ctu_mod) {
0274 u32 converted_chan = rsnd_io_converted_chan(io);
0275
0276
0277
0278
0279
0280
0281
0282
0283 if (rsnd_runtime_is_tdm_split(io)) {
0284 struct device *dev = rsnd_priv_to_dev(rsnd_io_to_priv(io));
0285
0286 dev_err(dev, "CTU and TDM Split should be used\n");
0287 }
0288
0289 if (converted_chan)
0290 return converted_chan;
0291 }
0292
0293 return chan;
0294 }
0295
0296 int rsnd_channel_normalization(int chan)
0297 {
0298 if (WARN_ON((chan > 8) || (chan < 0)))
0299 return 0;
0300
0301
0302 if (chan == 6)
0303 chan = 8;
0304
0305 return chan;
0306 }
0307
0308 int rsnd_runtime_channel_for_ssi_with_params(struct rsnd_dai_stream *io,
0309 struct snd_pcm_hw_params *params)
0310 {
0311 struct rsnd_dai *rdai = rsnd_io_to_rdai(io);
0312 int chan = rsnd_io_is_play(io) ?
0313 rsnd_runtime_channel_after_ctu_with_params(io, params) :
0314 rsnd_runtime_channel_original_with_params(io, params);
0315
0316
0317 if (rsnd_runtime_is_multi_ssi(io))
0318 chan /= rsnd_rdai_ssi_lane_get(rdai);
0319
0320 return rsnd_channel_normalization(chan);
0321 }
0322
0323 int rsnd_runtime_is_multi_ssi(struct rsnd_dai_stream *io)
0324 {
0325 struct rsnd_dai *rdai = rsnd_io_to_rdai(io);
0326 int lane = rsnd_rdai_ssi_lane_get(rdai);
0327 int chan = rsnd_io_is_play(io) ?
0328 rsnd_runtime_channel_after_ctu(io) :
0329 rsnd_runtime_channel_original(io);
0330
0331 return (chan > 2) && (lane > 1);
0332 }
0333
0334 int rsnd_runtime_is_tdm(struct rsnd_dai_stream *io)
0335 {
0336 return rsnd_runtime_channel_for_ssi(io) >= 6;
0337 }
0338
0339 int rsnd_runtime_is_tdm_split(struct rsnd_dai_stream *io)
0340 {
0341 return !!rsnd_flags_has(io, RSND_STREAM_TDM_SPLIT);
0342 }
0343
0344
0345
0346
0347 u32 rsnd_get_adinr_bit(struct rsnd_mod *mod, struct rsnd_dai_stream *io)
0348 {
0349 struct rsnd_priv *priv = rsnd_mod_to_priv(mod);
0350 struct snd_pcm_runtime *runtime = rsnd_io_to_runtime(io);
0351 struct device *dev = rsnd_priv_to_dev(priv);
0352
0353 switch (snd_pcm_format_width(runtime->format)) {
0354 case 8:
0355 return 16 << 16;
0356 case 16:
0357 return 8 << 16;
0358 case 24:
0359 return 0 << 16;
0360 }
0361
0362 dev_warn(dev, "not supported sample bits\n");
0363
0364 return 0;
0365 }
0366
0367
0368
0369
0370 u32 rsnd_get_dalign(struct rsnd_mod *mod, struct rsnd_dai_stream *io)
0371 {
0372 static const u32 dalign_values[8] = {
0373 0x76543210, 0x00000032, 0x00007654, 0x00000076,
0374 0xfedcba98, 0x000000ba, 0x0000fedc, 0x000000fe,
0375 };
0376 int id = 0;
0377 struct rsnd_mod *ssiu = rsnd_io_to_mod_ssiu(io);
0378 struct rsnd_mod *target;
0379 struct snd_pcm_runtime *runtime = rsnd_io_to_runtime(io);
0380 u32 dalign;
0381
0382
0383
0384
0385
0386
0387
0388
0389
0390
0391
0392
0393
0394
0395
0396
0397
0398
0399
0400
0401
0402
0403 if (rsnd_io_is_play(io)) {
0404 struct rsnd_mod *src = rsnd_io_to_mod_src(io);
0405
0406 target = src ? src : ssiu;
0407 } else {
0408 struct rsnd_mod *cmd = rsnd_io_to_mod_cmd(io);
0409
0410 target = cmd ? cmd : ssiu;
0411 }
0412
0413 if (mod == ssiu)
0414 id = rsnd_mod_id_sub(mod);
0415
0416 dalign = dalign_values[id];
0417
0418 if (mod == target && snd_pcm_format_width(runtime->format) == 16) {
0419
0420 dalign = (dalign & 0xf0f0f0f0) >> 4 |
0421 (dalign & 0x0f0f0f0f) << 4;
0422 }
0423
0424 return dalign;
0425 }
0426
0427 u32 rsnd_get_busif_shift(struct rsnd_dai_stream *io, struct rsnd_mod *mod)
0428 {
0429 static const enum rsnd_mod_type playback_mods[] = {
0430 RSND_MOD_SRC,
0431 RSND_MOD_CMD,
0432 RSND_MOD_SSIU,
0433 };
0434 static const enum rsnd_mod_type capture_mods[] = {
0435 RSND_MOD_CMD,
0436 RSND_MOD_SRC,
0437 RSND_MOD_SSIU,
0438 };
0439 struct snd_pcm_runtime *runtime = rsnd_io_to_runtime(io);
0440 struct rsnd_mod *tmod = NULL;
0441 const enum rsnd_mod_type *mods =
0442 rsnd_io_is_play(io) ?
0443 playback_mods : capture_mods;
0444 int i;
0445
0446
0447
0448
0449
0450
0451
0452
0453
0454 if (snd_pcm_format_width(runtime->format) != 24)
0455 return 0;
0456
0457 for (i = 0; i < ARRAY_SIZE(playback_mods); i++) {
0458 tmod = rsnd_io_to_mod(io, mods[i]);
0459 if (tmod)
0460 break;
0461 }
0462
0463 if (tmod != mod)
0464 return 0;
0465
0466 if (rsnd_io_is_play(io))
0467 return (0 << 20) |
0468 (8 << 16);
0469 else
0470 return (1 << 20) |
0471 (8 << 16);
0472 }
0473
0474
0475
0476
0477 struct rsnd_mod *rsnd_mod_next(int *iterator,
0478 struct rsnd_dai_stream *io,
0479 enum rsnd_mod_type *array,
0480 int array_size)
0481 {
0482 int max = array ? array_size : RSND_MOD_MAX;
0483
0484 for (; *iterator < max; (*iterator)++) {
0485 enum rsnd_mod_type type = (array) ? array[*iterator] : *iterator;
0486 struct rsnd_mod *mod = rsnd_io_to_mod(io, type);
0487
0488 if (mod)
0489 return mod;
0490 }
0491
0492 return NULL;
0493 }
0494
0495 static enum rsnd_mod_type rsnd_mod_sequence[][RSND_MOD_MAX] = {
0496 {
0497
0498 RSND_MOD_AUDMAPP,
0499 RSND_MOD_AUDMA,
0500 RSND_MOD_DVC,
0501 RSND_MOD_MIX,
0502 RSND_MOD_CTU,
0503 RSND_MOD_CMD,
0504 RSND_MOD_SRC,
0505 RSND_MOD_SSIU,
0506 RSND_MOD_SSIM3,
0507 RSND_MOD_SSIM2,
0508 RSND_MOD_SSIM1,
0509 RSND_MOD_SSIP,
0510 RSND_MOD_SSI,
0511 }, {
0512
0513 RSND_MOD_AUDMAPP,
0514 RSND_MOD_AUDMA,
0515 RSND_MOD_SSIM3,
0516 RSND_MOD_SSIM2,
0517 RSND_MOD_SSIM1,
0518 RSND_MOD_SSIP,
0519 RSND_MOD_SSI,
0520 RSND_MOD_SSIU,
0521 RSND_MOD_DVC,
0522 RSND_MOD_MIX,
0523 RSND_MOD_CTU,
0524 RSND_MOD_CMD,
0525 RSND_MOD_SRC,
0526 },
0527 };
0528
0529 static int rsnd_status_update(struct rsnd_dai_stream *io,
0530 struct rsnd_mod *mod, enum rsnd_mod_type type,
0531 int shift, int add, int timing)
0532 {
0533 u32 *status = mod->ops->get_status(mod, io, type);
0534 u32 mask = 0xF << shift;
0535 u8 val = (*status >> shift) & 0xF;
0536 u8 next_val = (val + add) & 0xF;
0537 int func_call = (val == timing);
0538
0539
0540 if (add == 0 || shift == 28)
0541 return 1;
0542
0543 if (next_val == 0xF)
0544 func_call = -1;
0545 else
0546 *status = (*status & ~mask) + (next_val << shift);
0547
0548 return func_call;
0549 }
0550
0551 #define rsnd_dai_call(fn, io, param...) \
0552 ({ \
0553 struct device *dev = rsnd_priv_to_dev(rsnd_io_to_priv(io)); \
0554 struct rsnd_mod *mod; \
0555 int is_play = rsnd_io_is_play(io); \
0556 int ret = 0, i; \
0557 enum rsnd_mod_type *types = rsnd_mod_sequence[is_play]; \
0558 for_each_rsnd_mod_arrays(i, mod, io, types, RSND_MOD_MAX) { \
0559 int tmp = 0; \
0560 int func_call = rsnd_status_update(io, mod, types[i], \
0561 __rsnd_mod_shift_##fn, \
0562 __rsnd_mod_add_##fn, \
0563 __rsnd_mod_call_##fn); \
0564 if (func_call > 0 && (mod)->ops->fn) \
0565 tmp = (mod)->ops->fn(mod, io, param); \
0566 if (unlikely(func_call < 0) || \
0567 unlikely(tmp && (tmp != -EPROBE_DEFER))) \
0568 dev_err(dev, "%s : %s error (%d, %d)\n", \
0569 rsnd_mod_name(mod), #fn, tmp, func_call);\
0570 ret |= tmp; \
0571 } \
0572 ret; \
0573 })
0574
0575 int rsnd_dai_connect(struct rsnd_mod *mod,
0576 struct rsnd_dai_stream *io,
0577 enum rsnd_mod_type type)
0578 {
0579 struct rsnd_priv *priv;
0580 struct device *dev;
0581
0582 if (!mod)
0583 return -EIO;
0584
0585 if (io->mod[type] == mod)
0586 return 0;
0587
0588 if (io->mod[type])
0589 return -EINVAL;
0590
0591 priv = rsnd_mod_to_priv(mod);
0592 dev = rsnd_priv_to_dev(priv);
0593
0594 io->mod[type] = mod;
0595
0596 dev_dbg(dev, "%s is connected to io (%s)\n",
0597 rsnd_mod_name(mod),
0598 rsnd_io_is_play(io) ? "Playback" : "Capture");
0599
0600 return 0;
0601 }
0602
0603 static void rsnd_dai_disconnect(struct rsnd_mod *mod,
0604 struct rsnd_dai_stream *io,
0605 enum rsnd_mod_type type)
0606 {
0607 io->mod[type] = NULL;
0608 }
0609
0610 int rsnd_rdai_channels_ctrl(struct rsnd_dai *rdai,
0611 int max_channels)
0612 {
0613 if (max_channels > 0)
0614 rdai->max_channels = max_channels;
0615
0616 return rdai->max_channels;
0617 }
0618
0619 int rsnd_rdai_ssi_lane_ctrl(struct rsnd_dai *rdai,
0620 int ssi_lane)
0621 {
0622 if (ssi_lane > 0)
0623 rdai->ssi_lane = ssi_lane;
0624
0625 return rdai->ssi_lane;
0626 }
0627
0628 int rsnd_rdai_width_ctrl(struct rsnd_dai *rdai, int width)
0629 {
0630 if (width > 0)
0631 rdai->chan_width = width;
0632
0633 return rdai->chan_width;
0634 }
0635
0636 struct rsnd_dai *rsnd_rdai_get(struct rsnd_priv *priv, int id)
0637 {
0638 if ((id < 0) || (id >= rsnd_rdai_nr(priv)))
0639 return NULL;
0640
0641 return priv->rdai + id;
0642 }
0643
0644 static struct snd_soc_dai_driver
0645 *rsnd_daidrv_get(struct rsnd_priv *priv, int id)
0646 {
0647 if ((id < 0) || (id >= rsnd_rdai_nr(priv)))
0648 return NULL;
0649
0650 return priv->daidrv + id;
0651 }
0652
0653 #define rsnd_dai_to_priv(dai) snd_soc_dai_get_drvdata(dai)
0654 static struct rsnd_dai *rsnd_dai_to_rdai(struct snd_soc_dai *dai)
0655 {
0656 struct rsnd_priv *priv = rsnd_dai_to_priv(dai);
0657
0658 return rsnd_rdai_get(priv, dai->id);
0659 }
0660
0661
0662
0663
0664 void rsnd_dai_period_elapsed(struct rsnd_dai_stream *io)
0665 {
0666 struct snd_pcm_substream *substream = io->substream;
0667
0668
0669
0670
0671
0672
0673
0674
0675 snd_pcm_period_elapsed(substream);
0676 }
0677
0678 static void rsnd_dai_stream_init(struct rsnd_dai_stream *io,
0679 struct snd_pcm_substream *substream)
0680 {
0681 io->substream = substream;
0682 }
0683
0684 static void rsnd_dai_stream_quit(struct rsnd_dai_stream *io)
0685 {
0686 io->substream = NULL;
0687 }
0688
0689 static
0690 struct snd_soc_dai *rsnd_substream_to_dai(struct snd_pcm_substream *substream)
0691 {
0692 struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
0693
0694 return asoc_rtd_to_cpu(rtd, 0);
0695 }
0696
0697 static
0698 struct rsnd_dai_stream *rsnd_rdai_to_io(struct rsnd_dai *rdai,
0699 struct snd_pcm_substream *substream)
0700 {
0701 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
0702 return &rdai->playback;
0703 else
0704 return &rdai->capture;
0705 }
0706
0707 static int rsnd_soc_dai_trigger(struct snd_pcm_substream *substream, int cmd,
0708 struct snd_soc_dai *dai)
0709 {
0710 struct rsnd_priv *priv = rsnd_dai_to_priv(dai);
0711 struct rsnd_dai *rdai = rsnd_dai_to_rdai(dai);
0712 struct rsnd_dai_stream *io = rsnd_rdai_to_io(rdai, substream);
0713 int ret;
0714 unsigned long flags;
0715
0716 spin_lock_irqsave(&priv->lock, flags);
0717
0718 switch (cmd) {
0719 case SNDRV_PCM_TRIGGER_START:
0720 case SNDRV_PCM_TRIGGER_RESUME:
0721 ret = rsnd_dai_call(init, io, priv);
0722 if (ret < 0)
0723 goto dai_trigger_end;
0724
0725 ret = rsnd_dai_call(start, io, priv);
0726 if (ret < 0)
0727 goto dai_trigger_end;
0728
0729 ret = rsnd_dai_call(irq, io, priv, 1);
0730 if (ret < 0)
0731 goto dai_trigger_end;
0732
0733 break;
0734 case SNDRV_PCM_TRIGGER_STOP:
0735 case SNDRV_PCM_TRIGGER_SUSPEND:
0736 ret = rsnd_dai_call(irq, io, priv, 0);
0737
0738 ret |= rsnd_dai_call(stop, io, priv);
0739
0740 ret |= rsnd_dai_call(quit, io, priv);
0741
0742 break;
0743 default:
0744 ret = -EINVAL;
0745 }
0746
0747 dai_trigger_end:
0748 spin_unlock_irqrestore(&priv->lock, flags);
0749
0750 return ret;
0751 }
0752
0753 static int rsnd_soc_dai_set_fmt(struct snd_soc_dai *dai, unsigned int fmt)
0754 {
0755 struct rsnd_dai *rdai = rsnd_dai_to_rdai(dai);
0756
0757
0758 switch (fmt & SND_SOC_DAIFMT_CLOCK_PROVIDER_MASK) {
0759 case SND_SOC_DAIFMT_BC_FC:
0760 rdai->clk_master = 0;
0761 break;
0762 case SND_SOC_DAIFMT_BP_FP:
0763 rdai->clk_master = 1;
0764 break;
0765 default:
0766 return -EINVAL;
0767 }
0768
0769
0770 rdai->bit_clk_inv = 0;
0771 switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
0772 case SND_SOC_DAIFMT_I2S:
0773 rdai->sys_delay = 0;
0774 rdai->data_alignment = 0;
0775 rdai->frm_clk_inv = 0;
0776 break;
0777 case SND_SOC_DAIFMT_LEFT_J:
0778 case SND_SOC_DAIFMT_DSP_B:
0779 rdai->sys_delay = 1;
0780 rdai->data_alignment = 0;
0781 rdai->frm_clk_inv = 1;
0782 break;
0783 case SND_SOC_DAIFMT_RIGHT_J:
0784 rdai->sys_delay = 1;
0785 rdai->data_alignment = 1;
0786 rdai->frm_clk_inv = 1;
0787 break;
0788 case SND_SOC_DAIFMT_DSP_A:
0789 rdai->sys_delay = 0;
0790 rdai->data_alignment = 0;
0791 rdai->frm_clk_inv = 1;
0792 break;
0793 }
0794
0795
0796 switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
0797 case SND_SOC_DAIFMT_NB_IF:
0798 rdai->frm_clk_inv = !rdai->frm_clk_inv;
0799 break;
0800 case SND_SOC_DAIFMT_IB_NF:
0801 rdai->bit_clk_inv = !rdai->bit_clk_inv;
0802 break;
0803 case SND_SOC_DAIFMT_IB_IF:
0804 rdai->bit_clk_inv = !rdai->bit_clk_inv;
0805 rdai->frm_clk_inv = !rdai->frm_clk_inv;
0806 break;
0807 case SND_SOC_DAIFMT_NB_NF:
0808 default:
0809 break;
0810 }
0811
0812 return 0;
0813 }
0814
0815 static int rsnd_soc_set_dai_tdm_slot(struct snd_soc_dai *dai,
0816 u32 tx_mask, u32 rx_mask,
0817 int slots, int slot_width)
0818 {
0819 struct rsnd_priv *priv = rsnd_dai_to_priv(dai);
0820 struct rsnd_dai *rdai = rsnd_dai_to_rdai(dai);
0821 struct device *dev = rsnd_priv_to_dev(priv);
0822
0823 switch (slot_width) {
0824 case 16:
0825 case 24:
0826 case 32:
0827 break;
0828 default:
0829
0830 slot_width = 32;
0831 }
0832
0833 switch (slots) {
0834 case 2:
0835
0836 case 6:
0837 case 8:
0838
0839 rsnd_rdai_channels_set(rdai, slots);
0840 rsnd_rdai_ssi_lane_set(rdai, 1);
0841 rsnd_rdai_width_set(rdai, slot_width);
0842 break;
0843 default:
0844 dev_err(dev, "unsupported TDM slots (%d)\n", slots);
0845 return -EINVAL;
0846 }
0847
0848 return 0;
0849 }
0850
0851 static unsigned int rsnd_soc_hw_channels_list[] = {
0852 2, 6, 8,
0853 };
0854
0855 static unsigned int rsnd_soc_hw_rate_list[] = {
0856 8000,
0857 11025,
0858 16000,
0859 22050,
0860 32000,
0861 44100,
0862 48000,
0863 64000,
0864 88200,
0865 96000,
0866 176400,
0867 192000,
0868 };
0869
0870 static int rsnd_soc_hw_rule(struct rsnd_dai *rdai,
0871 unsigned int *list, int list_num,
0872 struct snd_interval *baseline, struct snd_interval *iv)
0873 {
0874 struct snd_interval p;
0875 unsigned int rate;
0876 int i;
0877
0878 snd_interval_any(&p);
0879 p.min = UINT_MAX;
0880 p.max = 0;
0881
0882 for (i = 0; i < list_num; i++) {
0883
0884 if (!snd_interval_test(iv, list[i]))
0885 continue;
0886
0887 rate = rsnd_ssi_clk_query(rdai,
0888 baseline->min, list[i], NULL);
0889 if (rate > 0) {
0890 p.min = min(p.min, list[i]);
0891 p.max = max(p.max, list[i]);
0892 }
0893
0894 rate = rsnd_ssi_clk_query(rdai,
0895 baseline->max, list[i], NULL);
0896 if (rate > 0) {
0897 p.min = min(p.min, list[i]);
0898 p.max = max(p.max, list[i]);
0899 }
0900 }
0901
0902 return snd_interval_refine(iv, &p);
0903 }
0904
0905 static int rsnd_soc_hw_rule_rate(struct snd_pcm_hw_params *params,
0906 struct snd_pcm_hw_rule *rule)
0907 {
0908 struct snd_interval *ic_ = hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS);
0909 struct snd_interval *ir = hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE);
0910 struct snd_interval ic;
0911 struct rsnd_dai_stream *io = rule->private;
0912 struct rsnd_dai *rdai = rsnd_io_to_rdai(io);
0913
0914
0915
0916
0917
0918
0919 ic = *ic_;
0920 ic.min =
0921 ic.max = rsnd_runtime_channel_for_ssi_with_params(io, params);
0922
0923 return rsnd_soc_hw_rule(rdai, rsnd_soc_hw_rate_list,
0924 ARRAY_SIZE(rsnd_soc_hw_rate_list),
0925 &ic, ir);
0926 }
0927
0928 static int rsnd_soc_hw_rule_channels(struct snd_pcm_hw_params *params,
0929 struct snd_pcm_hw_rule *rule)
0930 {
0931 struct snd_interval *ic_ = hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS);
0932 struct snd_interval *ir = hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE);
0933 struct snd_interval ic;
0934 struct rsnd_dai_stream *io = rule->private;
0935 struct rsnd_dai *rdai = rsnd_io_to_rdai(io);
0936
0937
0938
0939
0940
0941
0942 ic = *ic_;
0943 ic.min =
0944 ic.max = rsnd_runtime_channel_for_ssi_with_params(io, params);
0945
0946 return rsnd_soc_hw_rule(rdai, rsnd_soc_hw_channels_list,
0947 ARRAY_SIZE(rsnd_soc_hw_channels_list),
0948 ir, &ic);
0949 }
0950
0951 static const struct snd_pcm_hardware rsnd_pcm_hardware = {
0952 .info = SNDRV_PCM_INFO_INTERLEAVED |
0953 SNDRV_PCM_INFO_MMAP |
0954 SNDRV_PCM_INFO_MMAP_VALID,
0955 .buffer_bytes_max = 64 * 1024,
0956 .period_bytes_min = 32,
0957 .period_bytes_max = 8192,
0958 .periods_min = 1,
0959 .periods_max = 32,
0960 .fifo_size = 256,
0961 };
0962
0963 static int rsnd_soc_dai_startup(struct snd_pcm_substream *substream,
0964 struct snd_soc_dai *dai)
0965 {
0966 struct rsnd_dai *rdai = rsnd_dai_to_rdai(dai);
0967 struct rsnd_dai_stream *io = rsnd_rdai_to_io(rdai, substream);
0968 struct snd_pcm_hw_constraint_list *constraint = &rdai->constraint;
0969 struct snd_pcm_runtime *runtime = substream->runtime;
0970 unsigned int max_channels = rsnd_rdai_channels_get(rdai);
0971 int i;
0972
0973 rsnd_dai_stream_init(io, substream);
0974
0975
0976
0977
0978
0979 constraint->list = rsnd_soc_hw_channels_list;
0980 constraint->count = 0;
0981 constraint->mask = 0;
0982
0983 for (i = 0; i < ARRAY_SIZE(rsnd_soc_hw_channels_list); i++) {
0984 if (rsnd_soc_hw_channels_list[i] > max_channels)
0985 break;
0986 constraint->count = i + 1;
0987 }
0988
0989 snd_soc_set_runtime_hwparams(substream, &rsnd_pcm_hardware);
0990
0991 snd_pcm_hw_constraint_list(runtime, 0,
0992 SNDRV_PCM_HW_PARAM_CHANNELS, constraint);
0993
0994 snd_pcm_hw_constraint_integer(runtime,
0995 SNDRV_PCM_HW_PARAM_PERIODS);
0996
0997
0998
0999
1000
1001 if (rsnd_rdai_is_clk_master(rdai)) {
1002 int is_play = substream->stream == SNDRV_PCM_STREAM_PLAYBACK;
1003
1004 snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
1005 rsnd_soc_hw_rule_rate,
1006 is_play ? &rdai->playback : &rdai->capture,
1007 SNDRV_PCM_HW_PARAM_CHANNELS, -1);
1008 snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_CHANNELS,
1009 rsnd_soc_hw_rule_channels,
1010 is_play ? &rdai->playback : &rdai->capture,
1011 SNDRV_PCM_HW_PARAM_RATE, -1);
1012 }
1013
1014 return 0;
1015 }
1016
1017 static void rsnd_soc_dai_shutdown(struct snd_pcm_substream *substream,
1018 struct snd_soc_dai *dai)
1019 {
1020 struct rsnd_dai *rdai = rsnd_dai_to_rdai(dai);
1021 struct rsnd_priv *priv = rsnd_rdai_to_priv(rdai);
1022 struct rsnd_dai_stream *io = rsnd_rdai_to_io(rdai, substream);
1023
1024
1025
1026
1027 rsnd_dai_call(cleanup, io, priv);
1028
1029 rsnd_dai_stream_quit(io);
1030 }
1031
1032 static int rsnd_soc_dai_prepare(struct snd_pcm_substream *substream,
1033 struct snd_soc_dai *dai)
1034 {
1035 struct rsnd_priv *priv = rsnd_dai_to_priv(dai);
1036 struct rsnd_dai *rdai = rsnd_dai_to_rdai(dai);
1037 struct rsnd_dai_stream *io = rsnd_rdai_to_io(rdai, substream);
1038
1039 return rsnd_dai_call(prepare, io, priv);
1040 }
1041
1042 static u64 rsnd_soc_dai_formats[] = {
1043
1044
1045
1046
1047
1048
1049
1050
1051 SND_SOC_POSSIBLE_DAIFMT_I2S |
1052 SND_SOC_POSSIBLE_DAIFMT_RIGHT_J |
1053 SND_SOC_POSSIBLE_DAIFMT_LEFT_J |
1054 SND_SOC_POSSIBLE_DAIFMT_NB_NF |
1055 SND_SOC_POSSIBLE_DAIFMT_NB_IF |
1056 SND_SOC_POSSIBLE_DAIFMT_IB_NF |
1057 SND_SOC_POSSIBLE_DAIFMT_IB_IF,
1058
1059
1060
1061
1062
1063 SND_SOC_POSSIBLE_DAIFMT_DSP_A |
1064 SND_SOC_POSSIBLE_DAIFMT_DSP_B,
1065 };
1066
1067 static const struct snd_soc_dai_ops rsnd_soc_dai_ops = {
1068 .startup = rsnd_soc_dai_startup,
1069 .shutdown = rsnd_soc_dai_shutdown,
1070 .trigger = rsnd_soc_dai_trigger,
1071 .set_fmt = rsnd_soc_dai_set_fmt,
1072 .set_tdm_slot = rsnd_soc_set_dai_tdm_slot,
1073 .prepare = rsnd_soc_dai_prepare,
1074 .auto_selectable_formats = rsnd_soc_dai_formats,
1075 .num_auto_selectable_formats = ARRAY_SIZE(rsnd_soc_dai_formats),
1076 };
1077
1078 static void rsnd_parse_tdm_split_mode(struct rsnd_priv *priv,
1079 struct rsnd_dai_stream *io,
1080 struct device_node *dai_np)
1081 {
1082 struct device *dev = rsnd_priv_to_dev(priv);
1083 struct device_node *ssiu_np = rsnd_ssiu_of_node(priv);
1084 struct device_node *np;
1085 int is_play = rsnd_io_is_play(io);
1086 int i;
1087
1088 if (!ssiu_np)
1089 return;
1090
1091
1092
1093
1094
1095 for (i = 0;; i++) {
1096 struct device_node *node = is_play ?
1097 of_parse_phandle(dai_np, "playback", i) :
1098 of_parse_phandle(dai_np, "capture", i);
1099
1100 if (!node)
1101 break;
1102
1103 for_each_child_of_node(ssiu_np, np) {
1104 if (np == node) {
1105 rsnd_flags_set(io, RSND_STREAM_TDM_SPLIT);
1106 dev_dbg(dev, "%s is part of TDM Split\n", io->name);
1107 }
1108 }
1109
1110 of_node_put(node);
1111 }
1112
1113 of_node_put(ssiu_np);
1114 }
1115
1116 static void rsnd_parse_connect_simple(struct rsnd_priv *priv,
1117 struct rsnd_dai_stream *io,
1118 struct device_node *dai_np)
1119 {
1120 if (!rsnd_io_to_mod_ssi(io))
1121 return;
1122
1123 rsnd_parse_tdm_split_mode(priv, io, dai_np);
1124 }
1125
1126 static void rsnd_parse_connect_graph(struct rsnd_priv *priv,
1127 struct rsnd_dai_stream *io,
1128 struct device_node *endpoint)
1129 {
1130 struct device *dev = rsnd_priv_to_dev(priv);
1131 struct device_node *remote_node;
1132
1133 if (!rsnd_io_to_mod_ssi(io))
1134 return;
1135
1136 remote_node = of_graph_get_remote_port_parent(endpoint);
1137
1138
1139 if (strstr(remote_node->full_name, "hdmi@fead0000")) {
1140 rsnd_flags_set(io, RSND_STREAM_HDMI0);
1141 dev_dbg(dev, "%s connected to HDMI0\n", io->name);
1142 }
1143
1144
1145 if (strstr(remote_node->full_name, "hdmi@feae0000")) {
1146 rsnd_flags_set(io, RSND_STREAM_HDMI1);
1147 dev_dbg(dev, "%s connected to HDMI1\n", io->name);
1148 }
1149
1150 rsnd_parse_tdm_split_mode(priv, io, endpoint);
1151
1152 of_node_put(remote_node);
1153 }
1154
1155 void rsnd_parse_connect_common(struct rsnd_dai *rdai, char *name,
1156 struct rsnd_mod* (*mod_get)(struct rsnd_priv *priv, int id),
1157 struct device_node *node,
1158 struct device_node *playback,
1159 struct device_node *capture)
1160 {
1161 struct rsnd_priv *priv = rsnd_rdai_to_priv(rdai);
1162 struct device *dev = rsnd_priv_to_dev(priv);
1163 struct device_node *np;
1164 int i;
1165
1166 if (!node)
1167 return;
1168
1169 i = 0;
1170 for_each_child_of_node(node, np) {
1171 struct rsnd_mod *mod;
1172
1173 i = rsnd_node_fixed_index(dev, np, name, i);
1174 if (i < 0) {
1175 of_node_put(np);
1176 break;
1177 }
1178
1179 mod = mod_get(priv, i);
1180
1181 if (np == playback)
1182 rsnd_dai_connect(mod, &rdai->playback, mod->type);
1183 if (np == capture)
1184 rsnd_dai_connect(mod, &rdai->capture, mod->type);
1185 i++;
1186 }
1187
1188 of_node_put(node);
1189 }
1190
1191 int rsnd_node_fixed_index(struct device *dev, struct device_node *node, char *name, int idx)
1192 {
1193 char node_name[16];
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211 for (; idx < 64; idx++) {
1212 snprintf(node_name, sizeof(node_name), "%s-%d", name, idx);
1213
1214 if (strncmp(node_name, of_node_full_name(node), sizeof(node_name)) == 0)
1215 return idx;
1216 }
1217
1218 dev_err(dev, "strange node numbering (%s)",
1219 of_node_full_name(node));
1220 return -EINVAL;
1221 }
1222
1223 int rsnd_node_count(struct rsnd_priv *priv, struct device_node *node, char *name)
1224 {
1225 struct device *dev = rsnd_priv_to_dev(priv);
1226 struct device_node *np;
1227 int i;
1228
1229 i = 0;
1230 for_each_child_of_node(node, np) {
1231 i = rsnd_node_fixed_index(dev, np, name, i);
1232 if (i < 0) {
1233 of_node_put(np);
1234 return 0;
1235 }
1236 i++;
1237 }
1238
1239 return i;
1240 }
1241
1242 static struct device_node *rsnd_dai_of_node(struct rsnd_priv *priv,
1243 int *is_graph)
1244 {
1245 struct device *dev = rsnd_priv_to_dev(priv);
1246 struct device_node *np = dev->of_node;
1247 struct device_node *dai_node;
1248 struct device_node *ret;
1249
1250 *is_graph = 0;
1251
1252
1253
1254
1255
1256 dai_node = of_get_child_by_name(np, RSND_NODE_DAI);
1257 if (dai_node) {
1258 ret = dai_node;
1259 goto of_node_compatible;
1260 }
1261
1262 ret = np;
1263
1264 dai_node = of_graph_get_next_endpoint(np, NULL);
1265 if (dai_node)
1266 goto of_node_graph;
1267
1268 return NULL;
1269
1270 of_node_graph:
1271 *is_graph = 1;
1272 of_node_compatible:
1273 of_node_put(dai_node);
1274
1275 return ret;
1276 }
1277
1278
1279 #define PREALLOC_BUFFER (32 * 1024)
1280 #define PREALLOC_BUFFER_MAX (32 * 1024)
1281
1282 static int rsnd_preallocate_pages(struct snd_soc_pcm_runtime *rtd,
1283 struct rsnd_dai_stream *io,
1284 int stream)
1285 {
1286 struct rsnd_priv *priv = rsnd_io_to_priv(io);
1287 struct device *dev = rsnd_priv_to_dev(priv);
1288 struct snd_pcm_substream *substream;
1289
1290
1291
1292
1293
1294
1295 if (io->dmac_dev)
1296 dev = io->dmac_dev;
1297
1298 for (substream = rtd->pcm->streams[stream].substream;
1299 substream;
1300 substream = substream->next) {
1301 snd_pcm_set_managed_buffer(substream,
1302 SNDRV_DMA_TYPE_DEV,
1303 dev,
1304 PREALLOC_BUFFER, PREALLOC_BUFFER_MAX);
1305 }
1306
1307 return 0;
1308 }
1309
1310 static int rsnd_pcm_new(struct snd_soc_pcm_runtime *rtd,
1311 struct snd_soc_dai *dai)
1312 {
1313 struct rsnd_dai *rdai = rsnd_dai_to_rdai(dai);
1314 int ret;
1315
1316 ret = rsnd_dai_call(pcm_new, &rdai->playback, rtd);
1317 if (ret)
1318 return ret;
1319
1320 ret = rsnd_dai_call(pcm_new, &rdai->capture, rtd);
1321 if (ret)
1322 return ret;
1323
1324 ret = rsnd_preallocate_pages(rtd, &rdai->playback,
1325 SNDRV_PCM_STREAM_PLAYBACK);
1326 if (ret)
1327 return ret;
1328
1329 ret = rsnd_preallocate_pages(rtd, &rdai->capture,
1330 SNDRV_PCM_STREAM_CAPTURE);
1331 if (ret)
1332 return ret;
1333
1334 return 0;
1335 }
1336
1337 static void __rsnd_dai_probe(struct rsnd_priv *priv,
1338 struct device_node *dai_np,
1339 int dai_i)
1340 {
1341 struct rsnd_dai_stream *io_playback;
1342 struct rsnd_dai_stream *io_capture;
1343 struct snd_soc_dai_driver *drv;
1344 struct rsnd_dai *rdai;
1345 struct device *dev = rsnd_priv_to_dev(priv);
1346 int io_i;
1347
1348 rdai = rsnd_rdai_get(priv, dai_i);
1349 drv = rsnd_daidrv_get(priv, dai_i);
1350 io_playback = &rdai->playback;
1351 io_capture = &rdai->capture;
1352
1353 snprintf(rdai->name, RSND_DAI_NAME_SIZE, "rsnd-dai.%d", dai_i);
1354
1355 rdai->priv = priv;
1356 drv->name = rdai->name;
1357 drv->ops = &rsnd_soc_dai_ops;
1358 drv->pcm_new = rsnd_pcm_new;
1359
1360 snprintf(io_playback->name, RSND_DAI_NAME_SIZE,
1361 "DAI%d Playback", dai_i);
1362 drv->playback.rates = RSND_RATES;
1363 drv->playback.formats = RSND_FMTS;
1364 drv->playback.channels_min = 2;
1365 drv->playback.channels_max = 8;
1366 drv->playback.stream_name = io_playback->name;
1367
1368 snprintf(io_capture->name, RSND_DAI_NAME_SIZE,
1369 "DAI%d Capture", dai_i);
1370 drv->capture.rates = RSND_RATES;
1371 drv->capture.formats = RSND_FMTS;
1372 drv->capture.channels_min = 2;
1373 drv->capture.channels_max = 8;
1374 drv->capture.stream_name = io_capture->name;
1375
1376 io_playback->rdai = rdai;
1377 io_capture->rdai = rdai;
1378 rsnd_rdai_channels_set(rdai, 2);
1379 rsnd_rdai_ssi_lane_set(rdai, 1);
1380 rsnd_rdai_width_set(rdai, 32);
1381
1382 for (io_i = 0;; io_i++) {
1383 struct device_node *playback = of_parse_phandle(dai_np, "playback", io_i);
1384 struct device_node *capture = of_parse_phandle(dai_np, "capture", io_i);
1385
1386 if (!playback && !capture)
1387 break;
1388
1389 rsnd_parse_connect_ssi(rdai, playback, capture);
1390 rsnd_parse_connect_ssiu(rdai, playback, capture);
1391 rsnd_parse_connect_src(rdai, playback, capture);
1392 rsnd_parse_connect_ctu(rdai, playback, capture);
1393 rsnd_parse_connect_mix(rdai, playback, capture);
1394 rsnd_parse_connect_dvc(rdai, playback, capture);
1395
1396 of_node_put(playback);
1397 of_node_put(capture);
1398 }
1399
1400 if (rsnd_ssi_is_pin_sharing(io_capture) ||
1401 rsnd_ssi_is_pin_sharing(io_playback)) {
1402
1403 drv->symmetric_rate = 1;
1404 }
1405
1406 dev_dbg(dev, "%s (%s/%s)\n", rdai->name,
1407 rsnd_io_to_mod_ssi(io_playback) ? "play" : " -- ",
1408 rsnd_io_to_mod_ssi(io_capture) ? "capture" : " -- ");
1409 }
1410
1411 static int rsnd_dai_probe(struct rsnd_priv *priv)
1412 {
1413 struct device_node *dai_node;
1414 struct device_node *dai_np;
1415 struct snd_soc_dai_driver *rdrv;
1416 struct device *dev = rsnd_priv_to_dev(priv);
1417 struct rsnd_dai *rdai;
1418 int nr;
1419 int is_graph;
1420 int dai_i;
1421
1422 dai_node = rsnd_dai_of_node(priv, &is_graph);
1423 if (is_graph)
1424 nr = of_graph_get_endpoint_count(dai_node);
1425 else
1426 nr = of_get_child_count(dai_node);
1427
1428 if (!nr)
1429 return -EINVAL;
1430
1431 rdrv = devm_kcalloc(dev, nr, sizeof(*rdrv), GFP_KERNEL);
1432 rdai = devm_kcalloc(dev, nr, sizeof(*rdai), GFP_KERNEL);
1433 if (!rdrv || !rdai)
1434 return -ENOMEM;
1435
1436 priv->rdai_nr = nr;
1437 priv->daidrv = rdrv;
1438 priv->rdai = rdai;
1439
1440
1441
1442
1443 dai_i = 0;
1444 if (is_graph) {
1445 for_each_endpoint_of_node(dai_node, dai_np) {
1446 __rsnd_dai_probe(priv, dai_np, dai_i);
1447 if (rsnd_is_gen3(priv)) {
1448 rdai = rsnd_rdai_get(priv, dai_i);
1449
1450 rsnd_parse_connect_graph(priv, &rdai->playback, dai_np);
1451 rsnd_parse_connect_graph(priv, &rdai->capture, dai_np);
1452 }
1453 dai_i++;
1454 }
1455 } else {
1456 for_each_child_of_node(dai_node, dai_np) {
1457 __rsnd_dai_probe(priv, dai_np, dai_i);
1458 if (rsnd_is_gen3(priv)) {
1459 rdai = rsnd_rdai_get(priv, dai_i);
1460
1461 rsnd_parse_connect_simple(priv, &rdai->playback, dai_np);
1462 rsnd_parse_connect_simple(priv, &rdai->capture, dai_np);
1463 }
1464 dai_i++;
1465 }
1466 }
1467
1468 return 0;
1469 }
1470
1471
1472
1473
1474 static int rsnd_hw_update(struct snd_pcm_substream *substream,
1475 struct snd_pcm_hw_params *hw_params)
1476 {
1477 struct snd_soc_dai *dai = rsnd_substream_to_dai(substream);
1478 struct rsnd_dai *rdai = rsnd_dai_to_rdai(dai);
1479 struct rsnd_dai_stream *io = rsnd_rdai_to_io(rdai, substream);
1480 struct rsnd_priv *priv = rsnd_io_to_priv(io);
1481 unsigned long flags;
1482 int ret;
1483
1484 spin_lock_irqsave(&priv->lock, flags);
1485 if (hw_params)
1486 ret = rsnd_dai_call(hw_params, io, substream, hw_params);
1487 else
1488 ret = rsnd_dai_call(hw_free, io, substream);
1489 spin_unlock_irqrestore(&priv->lock, flags);
1490
1491 return ret;
1492 }
1493
1494 static int rsnd_hw_params(struct snd_soc_component *component,
1495 struct snd_pcm_substream *substream,
1496 struct snd_pcm_hw_params *hw_params)
1497 {
1498 struct snd_soc_dai *dai = rsnd_substream_to_dai(substream);
1499 struct rsnd_dai *rdai = rsnd_dai_to_rdai(dai);
1500 struct rsnd_dai_stream *io = rsnd_rdai_to_io(rdai, substream);
1501 struct snd_soc_pcm_runtime *fe = asoc_substream_to_rtd(substream);
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512 io->converted_rate = 0;
1513 io->converted_chan = 0;
1514 if (fe->dai_link->dynamic) {
1515 struct rsnd_priv *priv = rsnd_io_to_priv(io);
1516 struct device *dev = rsnd_priv_to_dev(priv);
1517 struct snd_soc_dpcm *dpcm;
1518 int stream = substream->stream;
1519
1520 for_each_dpcm_be(fe, stream, dpcm) {
1521 struct snd_pcm_hw_params *be_params = &dpcm->hw_params;
1522
1523 if (params_channels(hw_params) != params_channels(be_params))
1524 io->converted_chan = params_channels(be_params);
1525 if (params_rate(hw_params) != params_rate(be_params))
1526 io->converted_rate = params_rate(be_params);
1527 }
1528 if (io->converted_chan)
1529 dev_dbg(dev, "convert channels = %d\n", io->converted_chan);
1530 if (io->converted_rate) {
1531
1532
1533
1534
1535
1536
1537
1538
1539 int k_up = 6;
1540 int k_down = 6;
1541 int channel;
1542 struct rsnd_mod *src_mod = rsnd_io_to_mod_src(io);
1543
1544 dev_dbg(dev, "convert rate = %d\n", io->converted_rate);
1545
1546 channel = io->converted_chan ? io->converted_chan :
1547 params_channels(hw_params);
1548
1549 switch (rsnd_mod_id(src_mod)) {
1550
1551
1552
1553
1554
1555
1556
1557 case 1:
1558 case 3:
1559 case 4:
1560 if (channel > 4) {
1561 k_down = 2;
1562 break;
1563 }
1564 fallthrough;
1565 case 0:
1566 if (channel > 2)
1567 k_down = 4;
1568 break;
1569
1570
1571 default:
1572 if (channel > 2)
1573 return -EINVAL;
1574 }
1575
1576 if (params_rate(hw_params) > io->converted_rate * k_down) {
1577 hw_param_interval(hw_params, SNDRV_PCM_HW_PARAM_RATE)->min =
1578 io->converted_rate * k_down;
1579 hw_param_interval(hw_params, SNDRV_PCM_HW_PARAM_RATE)->max =
1580 io->converted_rate * k_down;
1581 hw_params->cmask |= SNDRV_PCM_HW_PARAM_RATE;
1582 } else if (params_rate(hw_params) * k_up < io->converted_rate) {
1583 hw_param_interval(hw_params, SNDRV_PCM_HW_PARAM_RATE)->min =
1584 (io->converted_rate + k_up - 1) / k_up;
1585 hw_param_interval(hw_params, SNDRV_PCM_HW_PARAM_RATE)->max =
1586 (io->converted_rate + k_up - 1) / k_up;
1587 hw_params->cmask |= SNDRV_PCM_HW_PARAM_RATE;
1588 }
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598 }
1599 }
1600
1601 return rsnd_hw_update(substream, hw_params);
1602 }
1603
1604 static int rsnd_hw_free(struct snd_soc_component *component,
1605 struct snd_pcm_substream *substream)
1606 {
1607 return rsnd_hw_update(substream, NULL);
1608 }
1609
1610 static snd_pcm_uframes_t rsnd_pointer(struct snd_soc_component *component,
1611 struct snd_pcm_substream *substream)
1612 {
1613 struct snd_soc_dai *dai = rsnd_substream_to_dai(substream);
1614 struct rsnd_dai *rdai = rsnd_dai_to_rdai(dai);
1615 struct rsnd_dai_stream *io = rsnd_rdai_to_io(rdai, substream);
1616 snd_pcm_uframes_t pointer = 0;
1617
1618 rsnd_dai_call(pointer, io, &pointer);
1619
1620 return pointer;
1621 }
1622
1623
1624
1625
1626 static int rsnd_kctrl_info(struct snd_kcontrol *kctrl,
1627 struct snd_ctl_elem_info *uinfo)
1628 {
1629 struct rsnd_kctrl_cfg *cfg = snd_kcontrol_chip(kctrl);
1630
1631 if (cfg->texts) {
1632 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
1633 uinfo->count = cfg->size;
1634 uinfo->value.enumerated.items = cfg->max;
1635 if (uinfo->value.enumerated.item >= cfg->max)
1636 uinfo->value.enumerated.item = cfg->max - 1;
1637 strscpy(uinfo->value.enumerated.name,
1638 cfg->texts[uinfo->value.enumerated.item],
1639 sizeof(uinfo->value.enumerated.name));
1640 } else {
1641 uinfo->count = cfg->size;
1642 uinfo->value.integer.min = 0;
1643 uinfo->value.integer.max = cfg->max;
1644 uinfo->type = (cfg->max == 1) ?
1645 SNDRV_CTL_ELEM_TYPE_BOOLEAN :
1646 SNDRV_CTL_ELEM_TYPE_INTEGER;
1647 }
1648
1649 return 0;
1650 }
1651
1652 static int rsnd_kctrl_get(struct snd_kcontrol *kctrl,
1653 struct snd_ctl_elem_value *uc)
1654 {
1655 struct rsnd_kctrl_cfg *cfg = snd_kcontrol_chip(kctrl);
1656 int i;
1657
1658 for (i = 0; i < cfg->size; i++)
1659 if (cfg->texts)
1660 uc->value.enumerated.item[i] = cfg->val[i];
1661 else
1662 uc->value.integer.value[i] = cfg->val[i];
1663
1664 return 0;
1665 }
1666
1667 static int rsnd_kctrl_put(struct snd_kcontrol *kctrl,
1668 struct snd_ctl_elem_value *uc)
1669 {
1670 struct rsnd_kctrl_cfg *cfg = snd_kcontrol_chip(kctrl);
1671 int i, change = 0;
1672
1673 if (!cfg->accept(cfg->io))
1674 return 0;
1675
1676 for (i = 0; i < cfg->size; i++) {
1677 if (cfg->texts) {
1678 change |= (uc->value.enumerated.item[i] != cfg->val[i]);
1679 cfg->val[i] = uc->value.enumerated.item[i];
1680 } else {
1681 change |= (uc->value.integer.value[i] != cfg->val[i]);
1682 cfg->val[i] = uc->value.integer.value[i];
1683 }
1684 }
1685
1686 if (change && cfg->update)
1687 cfg->update(cfg->io, cfg->mod);
1688
1689 return change;
1690 }
1691
1692 int rsnd_kctrl_accept_anytime(struct rsnd_dai_stream *io)
1693 {
1694 return 1;
1695 }
1696
1697 int rsnd_kctrl_accept_runtime(struct rsnd_dai_stream *io)
1698 {
1699 struct snd_pcm_runtime *runtime = rsnd_io_to_runtime(io);
1700 struct rsnd_priv *priv = rsnd_io_to_priv(io);
1701 struct device *dev = rsnd_priv_to_dev(priv);
1702
1703 if (!runtime) {
1704 dev_warn(dev, "Can't update kctrl when idle\n");
1705 return 0;
1706 }
1707
1708 return 1;
1709 }
1710
1711 struct rsnd_kctrl_cfg *rsnd_kctrl_init_m(struct rsnd_kctrl_cfg_m *cfg)
1712 {
1713 cfg->cfg.val = cfg->val;
1714
1715 return &cfg->cfg;
1716 }
1717
1718 struct rsnd_kctrl_cfg *rsnd_kctrl_init_s(struct rsnd_kctrl_cfg_s *cfg)
1719 {
1720 cfg->cfg.val = &cfg->val;
1721
1722 return &cfg->cfg;
1723 }
1724
1725 const char * const volume_ramp_rate[] = {
1726 "128 dB/1 step",
1727 "64 dB/1 step",
1728 "32 dB/1 step",
1729 "16 dB/1 step",
1730 "8 dB/1 step",
1731 "4 dB/1 step",
1732 "2 dB/1 step",
1733 "1 dB/1 step",
1734 "0.5 dB/1 step",
1735 "0.25 dB/1 step",
1736 "0.125 dB/1 step",
1737 "0.125 dB/2 steps",
1738 "0.125 dB/4 steps",
1739 "0.125 dB/8 steps",
1740 "0.125 dB/16 steps",
1741 "0.125 dB/32 steps",
1742 "0.125 dB/64 steps",
1743 "0.125 dB/128 steps",
1744 "0.125 dB/256 steps",
1745 "0.125 dB/512 steps",
1746 "0.125 dB/1024 steps",
1747 "0.125 dB/2048 steps",
1748 "0.125 dB/4096 steps",
1749 "0.125 dB/8192 steps",
1750 };
1751
1752 int rsnd_kctrl_new(struct rsnd_mod *mod,
1753 struct rsnd_dai_stream *io,
1754 struct snd_soc_pcm_runtime *rtd,
1755 const unsigned char *name,
1756 int (*accept)(struct rsnd_dai_stream *io),
1757 void (*update)(struct rsnd_dai_stream *io,
1758 struct rsnd_mod *mod),
1759 struct rsnd_kctrl_cfg *cfg,
1760 const char * const *texts,
1761 int size,
1762 u32 max)
1763 {
1764 struct snd_card *card = rtd->card->snd_card;
1765 struct snd_kcontrol *kctrl;
1766 struct snd_kcontrol_new knew = {
1767 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1768 .name = name,
1769 .info = rsnd_kctrl_info,
1770 .index = rtd->num,
1771 .get = rsnd_kctrl_get,
1772 .put = rsnd_kctrl_put,
1773 };
1774 int ret;
1775
1776
1777
1778
1779
1780
1781 list_for_each_entry(kctrl, &card->controls, list) {
1782 struct rsnd_kctrl_cfg *c = kctrl->private_data;
1783
1784 if (c == cfg)
1785 return 0;
1786 }
1787
1788 if (size > RSND_MAX_CHANNELS)
1789 return -EINVAL;
1790
1791 kctrl = snd_ctl_new1(&knew, cfg);
1792 if (!kctrl)
1793 return -ENOMEM;
1794
1795 ret = snd_ctl_add(card, kctrl);
1796 if (ret < 0)
1797 return ret;
1798
1799 cfg->texts = texts;
1800 cfg->max = max;
1801 cfg->size = size;
1802 cfg->accept = accept;
1803 cfg->update = update;
1804 cfg->card = card;
1805 cfg->kctrl = kctrl;
1806 cfg->io = io;
1807 cfg->mod = mod;
1808
1809 return 0;
1810 }
1811
1812
1813
1814
1815 static const struct snd_soc_component_driver rsnd_soc_component = {
1816 .name = "rsnd",
1817 .probe = rsnd_debugfs_probe,
1818 .hw_params = rsnd_hw_params,
1819 .hw_free = rsnd_hw_free,
1820 .pointer = rsnd_pointer,
1821 .legacy_dai_naming = 1,
1822 };
1823
1824 static int rsnd_rdai_continuance_probe(struct rsnd_priv *priv,
1825 struct rsnd_dai_stream *io)
1826 {
1827 int ret;
1828
1829 ret = rsnd_dai_call(probe, io, priv);
1830 if (ret == -EAGAIN) {
1831 struct rsnd_mod *ssi_mod = rsnd_io_to_mod_ssi(io);
1832 struct rsnd_mod *mod;
1833 int i;
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846 rsnd_dai_call(remove, io, priv);
1847
1848
1849
1850
1851
1852 for_each_rsnd_mod(i, mod, io)
1853 rsnd_dai_disconnect(mod, io, i);
1854 rsnd_dai_connect(ssi_mod, io, RSND_MOD_SSI);
1855
1856
1857
1858
1859 rsnd_dai_call(fallback, io, priv);
1860
1861
1862
1863
1864
1865 ret = rsnd_dai_call(probe, io, priv);
1866 }
1867
1868 return ret;
1869 }
1870
1871
1872
1873
1874 static int rsnd_probe(struct platform_device *pdev)
1875 {
1876 struct rsnd_priv *priv;
1877 struct device *dev = &pdev->dev;
1878 struct rsnd_dai *rdai;
1879 int (*probe_func[])(struct rsnd_priv *priv) = {
1880 rsnd_gen_probe,
1881 rsnd_dma_probe,
1882 rsnd_ssi_probe,
1883 rsnd_ssiu_probe,
1884 rsnd_src_probe,
1885 rsnd_ctu_probe,
1886 rsnd_mix_probe,
1887 rsnd_dvc_probe,
1888 rsnd_cmd_probe,
1889 rsnd_adg_probe,
1890 rsnd_dai_probe,
1891 };
1892 int ret, i;
1893
1894
1895
1896
1897 priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
1898 if (!priv)
1899 return -ENODEV;
1900
1901 priv->pdev = pdev;
1902 priv->flags = (unsigned long)of_device_get_match_data(dev);
1903 spin_lock_init(&priv->lock);
1904
1905
1906
1907
1908 for (i = 0; i < ARRAY_SIZE(probe_func); i++) {
1909 ret = probe_func[i](priv);
1910 if (ret)
1911 return ret;
1912 }
1913
1914 for_each_rsnd_dai(rdai, priv, i) {
1915 ret = rsnd_rdai_continuance_probe(priv, &rdai->playback);
1916 if (ret)
1917 goto exit_snd_probe;
1918
1919 ret = rsnd_rdai_continuance_probe(priv, &rdai->capture);
1920 if (ret)
1921 goto exit_snd_probe;
1922 }
1923
1924 dev_set_drvdata(dev, priv);
1925
1926
1927
1928
1929 ret = devm_snd_soc_register_component(dev, &rsnd_soc_component,
1930 priv->daidrv, rsnd_rdai_nr(priv));
1931 if (ret < 0) {
1932 dev_err(dev, "cannot snd dai register\n");
1933 goto exit_snd_probe;
1934 }
1935
1936 pm_runtime_enable(dev);
1937
1938 dev_info(dev, "probed\n");
1939 return ret;
1940
1941 exit_snd_probe:
1942 for_each_rsnd_dai(rdai, priv, i) {
1943 rsnd_dai_call(remove, &rdai->playback, priv);
1944 rsnd_dai_call(remove, &rdai->capture, priv);
1945 }
1946
1947
1948
1949
1950
1951
1952
1953 rsnd_adg_remove(priv);
1954
1955 return ret;
1956 }
1957
1958 static int rsnd_remove(struct platform_device *pdev)
1959 {
1960 struct rsnd_priv *priv = dev_get_drvdata(&pdev->dev);
1961 struct rsnd_dai *rdai;
1962 void (*remove_func[])(struct rsnd_priv *priv) = {
1963 rsnd_ssi_remove,
1964 rsnd_ssiu_remove,
1965 rsnd_src_remove,
1966 rsnd_ctu_remove,
1967 rsnd_mix_remove,
1968 rsnd_dvc_remove,
1969 rsnd_cmd_remove,
1970 rsnd_adg_remove,
1971 };
1972 int i;
1973
1974 pm_runtime_disable(&pdev->dev);
1975
1976 for_each_rsnd_dai(rdai, priv, i) {
1977 int ret;
1978
1979 ret = rsnd_dai_call(remove, &rdai->playback, priv);
1980 if (ret)
1981 dev_warn(&pdev->dev, "Failed to remove playback dai #%d\n", i);
1982
1983 ret = rsnd_dai_call(remove, &rdai->capture, priv);
1984 if (ret)
1985 dev_warn(&pdev->dev, "Failed to remove capture dai #%d\n", i);
1986 }
1987
1988 for (i = 0; i < ARRAY_SIZE(remove_func); i++)
1989 remove_func[i](priv);
1990
1991 return 0;
1992 }
1993
1994 static int __maybe_unused rsnd_suspend(struct device *dev)
1995 {
1996 struct rsnd_priv *priv = dev_get_drvdata(dev);
1997
1998 rsnd_adg_clk_disable(priv);
1999
2000 return 0;
2001 }
2002
2003 static int __maybe_unused rsnd_resume(struct device *dev)
2004 {
2005 struct rsnd_priv *priv = dev_get_drvdata(dev);
2006
2007 rsnd_adg_clk_enable(priv);
2008
2009 return 0;
2010 }
2011
2012 static const struct dev_pm_ops rsnd_pm_ops = {
2013 SET_SYSTEM_SLEEP_PM_OPS(rsnd_suspend, rsnd_resume)
2014 };
2015
2016 static struct platform_driver rsnd_driver = {
2017 .driver = {
2018 .name = "rcar_sound",
2019 .pm = &rsnd_pm_ops,
2020 .of_match_table = rsnd_of_match,
2021 },
2022 .probe = rsnd_probe,
2023 .remove = rsnd_remove,
2024 };
2025 module_platform_driver(rsnd_driver);
2026
2027 MODULE_LICENSE("GPL v2");
2028 MODULE_DESCRIPTION("Renesas R-Car audio driver");
2029 MODULE_AUTHOR("Kuninori Morimoto <kuninori.morimoto.gx@renesas.com>");
2030 MODULE_ALIAS("platform:rcar-pcm-audio");