1 // SPDX-License-Identifier: (GPL-2.0 OR MIT)
3 // Copyright (c) 2018 BayLibre, SAS.
6 #include <linux/module.h>
7 #include <linux/of_platform.h>
9 #include <sound/soc-dai.h>
12 #include "meson-card.h"
14 struct axg_dai_link_tdm_mask {
19 struct axg_dai_link_tdm_data {
22 unsigned int slot_width;
25 struct axg_dai_link_tdm_mask *codec_masks;
29 * Base params for the codec to codec links
30 * Those will be over-written by the CPU side of the link
32 static const struct snd_soc_pcm_stream codec_params = {
33 .formats = SNDRV_PCM_FMTBIT_S24_LE,
40 static int axg_card_tdm_be_hw_params(struct snd_pcm_substream *substream,
41 struct snd_pcm_hw_params *params)
43 struct snd_soc_pcm_runtime *rtd = substream->private_data;
44 struct meson_card *priv = snd_soc_card_get_drvdata(rtd->card);
45 struct axg_dai_link_tdm_data *be =
46 (struct axg_dai_link_tdm_data *)priv->link_data[rtd->num];
48 return meson_card_i2s_set_sysclk(substream, params, be->mclk_fs);
51 static const struct snd_soc_ops axg_card_tdm_be_ops = {
52 .hw_params = axg_card_tdm_be_hw_params,
55 static int axg_card_tdm_dai_init(struct snd_soc_pcm_runtime *rtd)
57 struct meson_card *priv = snd_soc_card_get_drvdata(rtd->card);
58 struct axg_dai_link_tdm_data *be =
59 (struct axg_dai_link_tdm_data *)priv->link_data[rtd->num];
60 struct snd_soc_dai *codec_dai;
63 for_each_rtd_codec_dais(rtd, i, codec_dai) {
64 ret = snd_soc_dai_set_tdm_slot(codec_dai,
65 be->codec_masks[i].tx,
66 be->codec_masks[i].rx,
67 be->slots, be->slot_width);
68 if (ret && ret != -ENOTSUPP) {
69 dev_err(codec_dai->dev,
70 "setting tdm link slots failed\n");
75 ret = axg_tdm_set_tdm_slots(asoc_rtd_to_cpu(rtd, 0), be->tx_mask, be->rx_mask,
76 be->slots, be->slot_width);
78 dev_err(asoc_rtd_to_cpu(rtd, 0)->dev, "setting tdm link slots failed\n");
85 static int axg_card_tdm_dai_lb_init(struct snd_soc_pcm_runtime *rtd)
87 struct meson_card *priv = snd_soc_card_get_drvdata(rtd->card);
88 struct axg_dai_link_tdm_data *be =
89 (struct axg_dai_link_tdm_data *)priv->link_data[rtd->num];
92 /* The loopback rx_mask is the pad tx_mask */
93 ret = axg_tdm_set_tdm_slots(asoc_rtd_to_cpu(rtd, 0), NULL, be->tx_mask,
94 be->slots, be->slot_width);
96 dev_err(asoc_rtd_to_cpu(rtd, 0)->dev, "setting tdm link slots failed\n");
103 static int axg_card_add_tdm_loopback(struct snd_soc_card *card,
106 struct meson_card *priv = snd_soc_card_get_drvdata(card);
107 struct snd_soc_dai_link *pad = &card->dai_link[*index];
108 struct snd_soc_dai_link *lb;
109 struct snd_soc_dai_link_component *dlc;
113 ret = meson_card_reallocate_links(card, card->num_links + 1);
117 lb = &card->dai_link[*index + 1];
119 lb->name = kasprintf(GFP_KERNEL, "%s-lb", pad->name);
123 dlc = devm_kzalloc(card->dev, 2 * sizeof(*dlc), GFP_KERNEL);
128 lb->codecs = &dlc[1];
132 lb->stream_name = lb->name;
133 lb->cpus->of_node = pad->cpus->of_node;
134 lb->cpus->dai_name = "TDM Loopback";
135 lb->codecs->name = "snd-soc-dummy";
136 lb->codecs->dai_name = "snd-soc-dummy-dai";
137 lb->dpcm_capture = 1;
139 lb->ops = &axg_card_tdm_be_ops;
140 lb->init = axg_card_tdm_dai_lb_init;
142 /* Provide the same link data to the loopback */
143 priv->link_data[*index + 1] = priv->link_data[*index];
146 * axg_card_clean_references() will iterate over this link,
147 * make sure the node count is balanced
149 of_node_get(lb->cpus->of_node);
151 /* Let add_links continue where it should */
157 static int axg_card_parse_cpu_tdm_slots(struct snd_soc_card *card,
158 struct snd_soc_dai_link *link,
159 struct device_node *node,
160 struct axg_dai_link_tdm_data *be)
166 be->tx_mask = devm_kcalloc(card->dev, AXG_TDM_NUM_LANES,
167 sizeof(*be->tx_mask), GFP_KERNEL);
168 be->rx_mask = devm_kcalloc(card->dev, AXG_TDM_NUM_LANES,
169 sizeof(*be->rx_mask), GFP_KERNEL);
170 if (!be->tx_mask || !be->rx_mask)
173 for (i = 0, tx = 0; i < AXG_TDM_NUM_LANES; i++) {
174 snprintf(propname, 32, "dai-tdm-slot-tx-mask-%d", i);
175 snd_soc_of_get_slot_mask(node, propname, &be->tx_mask[i]);
176 tx = max(tx, be->tx_mask[i]);
179 /* Disable playback is the interface has no tx slots */
181 link->dpcm_playback = 0;
183 for (i = 0, rx = 0; i < AXG_TDM_NUM_LANES; i++) {
184 snprintf(propname, 32, "dai-tdm-slot-rx-mask-%d", i);
185 snd_soc_of_get_slot_mask(node, propname, &be->rx_mask[i]);
186 rx = max(rx, be->rx_mask[i]);
189 /* Disable capture is the interface has no rx slots */
191 link->dpcm_capture = 0;
193 /* ... but the interface should at least have one of them */
195 dev_err(card->dev, "tdm link has no cpu slots\n");
199 of_property_read_u32(node, "dai-tdm-slot-num", &be->slots);
202 * If the slot number is not provided, set it such as it
203 * accommodates the largest mask
205 be->slots = fls(max(tx, rx));
206 } else if (be->slots < fls(max(tx, rx)) || be->slots > 32) {
208 * Error if the slots can't accommodate the largest mask or
209 * if it is just too big
211 dev_err(card->dev, "bad slot number\n");
215 of_property_read_u32(node, "dai-tdm-slot-width", &be->slot_width);
220 static int axg_card_parse_codecs_masks(struct snd_soc_card *card,
221 struct snd_soc_dai_link *link,
222 struct device_node *node,
223 struct axg_dai_link_tdm_data *be)
225 struct axg_dai_link_tdm_mask *codec_mask;
226 struct device_node *np;
228 codec_mask = devm_kcalloc(card->dev, link->num_codecs,
229 sizeof(*codec_mask), GFP_KERNEL);
233 be->codec_masks = codec_mask;
235 for_each_child_of_node(node, np) {
236 snd_soc_of_get_slot_mask(np, "dai-tdm-slot-rx-mask",
238 snd_soc_of_get_slot_mask(np, "dai-tdm-slot-tx-mask",
247 static int axg_card_parse_tdm(struct snd_soc_card *card,
248 struct device_node *node,
251 struct meson_card *priv = snd_soc_card_get_drvdata(card);
252 struct snd_soc_dai_link *link = &card->dai_link[*index];
253 struct axg_dai_link_tdm_data *be;
256 /* Allocate tdm link parameters */
257 be = devm_kzalloc(card->dev, sizeof(*be), GFP_KERNEL);
260 priv->link_data[*index] = be;
263 link->ops = &axg_card_tdm_be_ops;
264 link->init = axg_card_tdm_dai_init;
265 link->dai_fmt = meson_card_parse_daifmt(node, link->cpus->of_node);
267 of_property_read_u32(node, "mclk-fs", &be->mclk_fs);
269 ret = axg_card_parse_cpu_tdm_slots(card, link, node, be);
271 dev_err(card->dev, "error parsing tdm link slots\n");
275 ret = axg_card_parse_codecs_masks(card, link, node, be);
279 /* Add loopback if the pad dai has playback */
280 if (link->dpcm_playback) {
281 ret = axg_card_add_tdm_loopback(card, index);
289 static int axg_card_cpu_is_capture_fe(struct device_node *np)
291 return of_device_is_compatible(np, DT_PREFIX "axg-toddr");
294 static int axg_card_cpu_is_playback_fe(struct device_node *np)
296 return of_device_is_compatible(np, DT_PREFIX "axg-frddr");
299 static int axg_card_cpu_is_tdm_iface(struct device_node *np)
301 return of_device_is_compatible(np, DT_PREFIX "axg-tdm-iface");
304 static int axg_card_cpu_is_codec(struct device_node *np)
306 return of_device_is_compatible(np, DT_PREFIX "g12a-tohdmitx") ||
307 of_device_is_compatible(np, DT_PREFIX "g12a-toacodec");
310 static int axg_card_add_link(struct snd_soc_card *card, struct device_node *np,
313 struct snd_soc_dai_link *dai_link = &card->dai_link[*index];
314 struct snd_soc_dai_link_component *cpu;
317 cpu = devm_kzalloc(card->dev, sizeof(*cpu), GFP_KERNEL);
321 dai_link->cpus = cpu;
322 dai_link->num_cpus = 1;
324 ret = meson_card_parse_dai(card, np, &dai_link->cpus->of_node,
325 &dai_link->cpus->dai_name);
329 if (axg_card_cpu_is_playback_fe(dai_link->cpus->of_node))
330 ret = meson_card_set_fe_link(card, dai_link, np, true);
331 else if (axg_card_cpu_is_capture_fe(dai_link->cpus->of_node))
332 ret = meson_card_set_fe_link(card, dai_link, np, false);
334 ret = meson_card_set_be_link(card, dai_link, np);
339 if (axg_card_cpu_is_tdm_iface(dai_link->cpus->of_node))
340 ret = axg_card_parse_tdm(card, np, index);
341 else if (axg_card_cpu_is_codec(dai_link->cpus->of_node)) {
342 dai_link->params = &codec_params;
343 dai_link->no_pcm = 0; /* link is not a DPCM BE */
349 static const struct meson_card_match_data axg_card_match_data = {
350 .add_link = axg_card_add_link,
353 static const struct of_device_id axg_card_of_match[] = {
355 .compatible = "amlogic,axg-sound-card",
356 .data = &axg_card_match_data,
359 MODULE_DEVICE_TABLE(of, axg_card_of_match);
361 static struct platform_driver axg_card_pdrv = {
362 .probe = meson_card_probe,
363 .remove = meson_card_remove,
365 .name = "axg-sound-card",
366 .of_match_table = axg_card_of_match,
369 module_platform_driver(axg_card_pdrv);
371 MODULE_DESCRIPTION("Amlogic AXG ALSA machine driver");
373 MODULE_LICENSE("GPL v2");