^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) // SPDX-License-Identifier: (GPL-2.0 OR MIT)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) //
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) // Copyright (c) 2018 BayLibre, SAS.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) // Author: Jerome Brunet <jbrunet@baylibre.com>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) #include <linux/of_platform.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) #include <sound/soc.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) #include <sound/soc-dai.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) #include "axg-tdm.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) #include "meson-card.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) struct axg_dai_link_tdm_mask {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) u32 tx;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) u32 rx;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) struct axg_dai_link_tdm_data {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) unsigned int mclk_fs;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) unsigned int slots;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) unsigned int slot_width;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) u32 *tx_mask;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) u32 *rx_mask;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) struct axg_dai_link_tdm_mask *codec_masks;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) * Base params for the codec to codec links
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) * Those will be over-written by the CPU side of the link
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) static const struct snd_soc_pcm_stream codec_params = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) .formats = SNDRV_PCM_FMTBIT_S24_LE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) .rate_min = 5525,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) .rate_max = 192000,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) .channels_min = 1,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) .channels_max = 8,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) static int axg_card_tdm_be_hw_params(struct snd_pcm_substream *substream,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) struct snd_pcm_hw_params *params)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) struct meson_card *priv = snd_soc_card_get_drvdata(rtd->card);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) struct axg_dai_link_tdm_data *be =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) (struct axg_dai_link_tdm_data *)priv->link_data[rtd->num];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) return meson_card_i2s_set_sysclk(substream, params, be->mclk_fs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) static const struct snd_soc_ops axg_card_tdm_be_ops = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) .hw_params = axg_card_tdm_be_hw_params,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) static int axg_card_tdm_dai_init(struct snd_soc_pcm_runtime *rtd)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) struct meson_card *priv = snd_soc_card_get_drvdata(rtd->card);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) struct axg_dai_link_tdm_data *be =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) (struct axg_dai_link_tdm_data *)priv->link_data[rtd->num];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) struct snd_soc_dai *codec_dai;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) int ret, i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) for_each_rtd_codec_dais(rtd, i, codec_dai) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) ret = snd_soc_dai_set_tdm_slot(codec_dai,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) be->codec_masks[i].tx,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) be->codec_masks[i].rx,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) be->slots, be->slot_width);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) if (ret && ret != -ENOTSUPP) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) dev_err(codec_dai->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) "setting tdm link slots failed\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) ret = axg_tdm_set_tdm_slots(asoc_rtd_to_cpu(rtd, 0), be->tx_mask, be->rx_mask,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) be->slots, be->slot_width);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) dev_err(asoc_rtd_to_cpu(rtd, 0)->dev, "setting tdm link slots failed\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) static int axg_card_tdm_dai_lb_init(struct snd_soc_pcm_runtime *rtd)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) struct meson_card *priv = snd_soc_card_get_drvdata(rtd->card);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) struct axg_dai_link_tdm_data *be =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) (struct axg_dai_link_tdm_data *)priv->link_data[rtd->num];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) /* The loopback rx_mask is the pad tx_mask */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) ret = axg_tdm_set_tdm_slots(asoc_rtd_to_cpu(rtd, 0), NULL, be->tx_mask,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) be->slots, be->slot_width);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) dev_err(asoc_rtd_to_cpu(rtd, 0)->dev, "setting tdm link slots failed\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) static int axg_card_add_tdm_loopback(struct snd_soc_card *card,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) int *index)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) struct meson_card *priv = snd_soc_card_get_drvdata(card);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) struct snd_soc_dai_link *pad = &card->dai_link[*index];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) struct snd_soc_dai_link *lb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) struct snd_soc_dai_link_component *dlc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) /* extend links */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) ret = meson_card_reallocate_links(card, card->num_links + 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) lb = &card->dai_link[*index + 1];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) lb->name = devm_kasprintf(card->dev, GFP_KERNEL, "%s-lb", pad->name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) if (!lb->name)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) dlc = devm_kzalloc(card->dev, 2 * sizeof(*dlc), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) if (!dlc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) lb->cpus = &dlc[0];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) lb->codecs = &dlc[1];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) lb->num_cpus = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) lb->num_codecs = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) lb->stream_name = lb->name;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) lb->cpus->of_node = pad->cpus->of_node;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) lb->cpus->dai_name = "TDM Loopback";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) lb->codecs->name = "snd-soc-dummy";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) lb->codecs->dai_name = "snd-soc-dummy-dai";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) lb->dpcm_capture = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) lb->no_pcm = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) lb->ops = &axg_card_tdm_be_ops;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) lb->init = axg_card_tdm_dai_lb_init;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) /* Provide the same link data to the loopback */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) priv->link_data[*index + 1] = priv->link_data[*index];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) * axg_card_clean_references() will iterate over this link,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) * make sure the node count is balanced
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) of_node_get(lb->cpus->of_node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) /* Let add_links continue where it should */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) *index += 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) static int axg_card_parse_cpu_tdm_slots(struct snd_soc_card *card,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) struct snd_soc_dai_link *link,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) struct device_node *node,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) struct axg_dai_link_tdm_data *be)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) char propname[32];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) u32 tx, rx;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) be->tx_mask = devm_kcalloc(card->dev, AXG_TDM_NUM_LANES,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) sizeof(*be->tx_mask), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) be->rx_mask = devm_kcalloc(card->dev, AXG_TDM_NUM_LANES,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) sizeof(*be->rx_mask), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) if (!be->tx_mask || !be->rx_mask)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) for (i = 0, tx = 0; i < AXG_TDM_NUM_LANES; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) snprintf(propname, 32, "dai-tdm-slot-tx-mask-%d", i);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) snd_soc_of_get_slot_mask(node, propname, &be->tx_mask[i]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) tx = max(tx, be->tx_mask[i]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) /* Disable playback is the interface has no tx slots */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) if (!tx)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) link->dpcm_playback = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) for (i = 0, rx = 0; i < AXG_TDM_NUM_LANES; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) snprintf(propname, 32, "dai-tdm-slot-rx-mask-%d", i);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) snd_soc_of_get_slot_mask(node, propname, &be->rx_mask[i]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) rx = max(rx, be->rx_mask[i]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) /* Disable capture is the interface has no rx slots */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) if (!rx)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) link->dpcm_capture = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) /* ... but the interface should at least have one of them */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) if (!tx && !rx) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) dev_err(card->dev, "tdm link has no cpu slots\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) of_property_read_u32(node, "dai-tdm-slot-num", &be->slots);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) if (!be->slots) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) * If the slot number is not provided, set it such as it
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) * accommodates the largest mask
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) be->slots = fls(max(tx, rx));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) } else if (be->slots < fls(max(tx, rx)) || be->slots > 32) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) * Error if the slots can't accommodate the largest mask or
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) * if it is just too big
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) dev_err(card->dev, "bad slot number\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) of_property_read_u32(node, "dai-tdm-slot-width", &be->slot_width);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) static int axg_card_parse_codecs_masks(struct snd_soc_card *card,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) struct snd_soc_dai_link *link,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) struct device_node *node,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) struct axg_dai_link_tdm_data *be)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) struct axg_dai_link_tdm_mask *codec_mask;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) struct device_node *np;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) codec_mask = devm_kcalloc(card->dev, link->num_codecs,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) sizeof(*codec_mask), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) if (!codec_mask)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) be->codec_masks = codec_mask;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) for_each_child_of_node(node, np) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) snd_soc_of_get_slot_mask(np, "dai-tdm-slot-rx-mask",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) &codec_mask->rx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) snd_soc_of_get_slot_mask(np, "dai-tdm-slot-tx-mask",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) &codec_mask->tx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) codec_mask++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) static int axg_card_parse_tdm(struct snd_soc_card *card,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) struct device_node *node,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) int *index)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) struct meson_card *priv = snd_soc_card_get_drvdata(card);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) struct snd_soc_dai_link *link = &card->dai_link[*index];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) struct axg_dai_link_tdm_data *be;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) /* Allocate tdm link parameters */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) be = devm_kzalloc(card->dev, sizeof(*be), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) if (!be)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) priv->link_data[*index] = be;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) /* Setup tdm link */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) link->ops = &axg_card_tdm_be_ops;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) link->init = axg_card_tdm_dai_init;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) link->dai_fmt = meson_card_parse_daifmt(node, link->cpus->of_node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) of_property_read_u32(node, "mclk-fs", &be->mclk_fs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) ret = axg_card_parse_cpu_tdm_slots(card, link, node, be);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) dev_err(card->dev, "error parsing tdm link slots\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) ret = axg_card_parse_codecs_masks(card, link, node, be);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) /* Add loopback if the pad dai has playback */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) if (link->dpcm_playback) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) ret = axg_card_add_tdm_loopback(card, index);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) static int axg_card_cpu_is_capture_fe(struct device_node *np)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) return of_device_is_compatible(np, DT_PREFIX "axg-toddr");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) static int axg_card_cpu_is_playback_fe(struct device_node *np)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) return of_device_is_compatible(np, DT_PREFIX "axg-frddr");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) static int axg_card_cpu_is_tdm_iface(struct device_node *np)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) return of_device_is_compatible(np, DT_PREFIX "axg-tdm-iface");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) static int axg_card_cpu_is_codec(struct device_node *np)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) return of_device_is_compatible(np, DT_PREFIX "g12a-tohdmitx") ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) of_device_is_compatible(np, DT_PREFIX "g12a-toacodec");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) static int axg_card_add_link(struct snd_soc_card *card, struct device_node *np,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) int *index)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) struct snd_soc_dai_link *dai_link = &card->dai_link[*index];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) struct snd_soc_dai_link_component *cpu;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) cpu = devm_kzalloc(card->dev, sizeof(*cpu), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) if (!cpu)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) dai_link->cpus = cpu;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) dai_link->num_cpus = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324) ret = meson_card_parse_dai(card, np, &dai_link->cpus->of_node,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325) &dai_link->cpus->dai_name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329) if (axg_card_cpu_is_playback_fe(dai_link->cpus->of_node))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) return meson_card_set_fe_link(card, dai_link, np, true);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331) else if (axg_card_cpu_is_capture_fe(dai_link->cpus->of_node))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332) return meson_card_set_fe_link(card, dai_link, np, false);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335) ret = meson_card_set_be_link(card, dai_link, np);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339) if (axg_card_cpu_is_codec(dai_link->cpus->of_node)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340) dai_link->params = &codec_params;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342) dai_link->no_pcm = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343) snd_soc_dai_link_set_capabilities(dai_link);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344) if (axg_card_cpu_is_tdm_iface(dai_link->cpus->of_node))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345) ret = axg_card_parse_tdm(card, np, index);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351) static const struct meson_card_match_data axg_card_match_data = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352) .add_link = axg_card_add_link,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 355) static const struct of_device_id axg_card_of_match[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 356) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 357) .compatible = "amlogic,axg-sound-card",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 358) .data = &axg_card_match_data,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 359) }, {}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 360) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 361) MODULE_DEVICE_TABLE(of, axg_card_of_match);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 362)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 363) static struct platform_driver axg_card_pdrv = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 364) .probe = meson_card_probe,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 365) .remove = meson_card_remove,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 366) .driver = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 367) .name = "axg-sound-card",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 368) .of_match_table = axg_card_of_match,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 369) },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 370) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 371) module_platform_driver(axg_card_pdrv);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 372)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 373) MODULE_DESCRIPTION("Amlogic AXG ALSA machine driver");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 374) MODULE_AUTHOR("Jerome Brunet <jbrunet@baylibre.com>");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 375) MODULE_LICENSE("GPL v2");