Orange Pi5 kernel

Deprecated Linux kernel 5.10.110 for OrangePi 5/5B/5+ boards

3 Commits   0 Branches   0 Tags
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   1) // SPDX-License-Identifier: GPL-2.0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   2) //
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   3) // Copyright (c) 2020 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) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  10) #include "meson-card.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  11) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  12) int meson_card_i2s_set_sysclk(struct snd_pcm_substream *substream,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  13) 			      struct snd_pcm_hw_params *params,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  14) 			      unsigned int mclk_fs)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  15) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  16) 	struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  17) 	struct snd_soc_dai *codec_dai;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  18) 	unsigned int mclk;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  19) 	int ret, i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  20) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  21) 	if (!mclk_fs)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  22) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  23) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  24) 	mclk = params_rate(params) * mclk_fs;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  25) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  26) 	for_each_rtd_codec_dais(rtd, i, codec_dai) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  27) 		ret = snd_soc_dai_set_sysclk(codec_dai, 0, mclk,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  28) 					     SND_SOC_CLOCK_IN);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  29) 		if (ret && ret != -ENOTSUPP)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  30) 			return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  31) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  32) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  33) 	ret = snd_soc_dai_set_sysclk(asoc_rtd_to_cpu(rtd, 0), 0, mclk,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  34) 				     SND_SOC_CLOCK_OUT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  35) 	if (ret && ret != -ENOTSUPP)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  36) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  37) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  38) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  39) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  40) EXPORT_SYMBOL_GPL(meson_card_i2s_set_sysclk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  41) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  42) int meson_card_reallocate_links(struct snd_soc_card *card,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  43) 				unsigned int num_links)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  44) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  45) 	struct meson_card *priv = snd_soc_card_get_drvdata(card);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  46) 	struct snd_soc_dai_link *links;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  47) 	void **ldata;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  48) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  49) 	links = krealloc(priv->card.dai_link,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  50) 			 num_links * sizeof(*priv->card.dai_link),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  51) 			 GFP_KERNEL | __GFP_ZERO);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  52) 	if (!links)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  53) 		goto err_links;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  54) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  55) 	ldata = krealloc(priv->link_data,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  56) 			 num_links * sizeof(*priv->link_data),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  57) 			 GFP_KERNEL | __GFP_ZERO);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  58) 	if (!ldata)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  59) 		goto err_ldata;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  60) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  61) 	priv->card.dai_link = links;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  62) 	priv->link_data = ldata;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  63) 	priv->card.num_links = num_links;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  64) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  65) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  66) err_ldata:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  67) 	kfree(links);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  68) err_links:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  69) 	dev_err(priv->card.dev, "failed to allocate links\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  70) 	return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  71) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  72) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  73) EXPORT_SYMBOL_GPL(meson_card_reallocate_links);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  74) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  75) int meson_card_parse_dai(struct snd_soc_card *card,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  76) 			 struct device_node *node,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  77) 			 struct device_node **dai_of_node,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  78) 			 const char **dai_name)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  79) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  80) 	struct of_phandle_args args;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  81) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  82) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  83) 	if (!dai_name || !dai_of_node || !node)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  84) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  85) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  86) 	ret = of_parse_phandle_with_args(node, "sound-dai",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  87) 					 "#sound-dai-cells", 0, &args);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  88) 	if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  89) 		if (ret != -EPROBE_DEFER)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  90) 			dev_err(card->dev, "can't parse dai %d\n", ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  91) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  92) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  93) 	*dai_of_node = args.np;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  94) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  95) 	return snd_soc_get_dai_name(&args, dai_name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  96) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  97) EXPORT_SYMBOL_GPL(meson_card_parse_dai);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  98) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  99) static int meson_card_set_link_name(struct snd_soc_card *card,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) 				    struct snd_soc_dai_link *link,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) 				    struct device_node *node,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) 				    const char *prefix)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) 	char *name = devm_kasprintf(card->dev, GFP_KERNEL, "%s.%s",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) 				    prefix, node->full_name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) 	if (!name)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) 	link->name = name;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) 	link->stream_name = name;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) unsigned int meson_card_parse_daifmt(struct device_node *node,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) 				     struct device_node *cpu_node)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) 	struct device_node *bitclkmaster = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) 	struct device_node *framemaster = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) 	unsigned int daifmt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) 	daifmt = snd_soc_of_parse_daifmt(node, "",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) 					 &bitclkmaster, &framemaster);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) 	daifmt &= ~SND_SOC_DAIFMT_MASTER_MASK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) 	/* If no master is provided, default to cpu master */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) 	if (!bitclkmaster || bitclkmaster == cpu_node) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) 		daifmt |= (!framemaster || framemaster == cpu_node) ?
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) 			SND_SOC_DAIFMT_CBS_CFS : SND_SOC_DAIFMT_CBS_CFM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) 		daifmt |= (!framemaster || framemaster == cpu_node) ?
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) 			SND_SOC_DAIFMT_CBM_CFS : SND_SOC_DAIFMT_CBM_CFM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) 	of_node_put(bitclkmaster);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) 	of_node_put(framemaster);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) 	return daifmt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) EXPORT_SYMBOL_GPL(meson_card_parse_daifmt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) int meson_card_set_be_link(struct snd_soc_card *card,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) 			   struct snd_soc_dai_link *link,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) 			   struct device_node *node)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) 	struct snd_soc_dai_link_component *codec;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) 	struct device_node *np;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) 	int ret, num_codecs;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) 	num_codecs = of_get_child_count(node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) 	if (!num_codecs) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) 		dev_err(card->dev, "be link %s has no codec\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) 			node->full_name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) 		return -EINVAL;
^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) 	codec = devm_kcalloc(card->dev, num_codecs, sizeof(*codec), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) 	if (!codec)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) 	link->codecs = codec;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) 	link->num_codecs = num_codecs;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) 	for_each_child_of_node(node, np) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) 		ret = meson_card_parse_dai(card, np, &codec->of_node,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) 					   &codec->dai_name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) 		if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) 			of_node_put(np);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) 			return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) 		codec++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) 	ret = meson_card_set_link_name(card, link, node, "be");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) 	if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) 		dev_err(card->dev, "error setting %pOFn link name\n", np);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) EXPORT_SYMBOL_GPL(meson_card_set_be_link);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) int meson_card_set_fe_link(struct snd_soc_card *card,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) 			   struct snd_soc_dai_link *link,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) 			   struct device_node *node,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) 			   bool is_playback)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) 	struct snd_soc_dai_link_component *codec;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) 	codec = devm_kzalloc(card->dev, sizeof(*codec), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) 	if (!codec)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) 	link->codecs = codec;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) 	link->num_codecs = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) 	link->dynamic = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) 	link->dpcm_merged_format = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) 	link->dpcm_merged_chan = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) 	link->dpcm_merged_rate = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) 	link->codecs->dai_name = "snd-soc-dummy-dai";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) 	link->codecs->name = "snd-soc-dummy";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) 	if (is_playback)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) 		link->dpcm_playback = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) 		link->dpcm_capture = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) 	return meson_card_set_link_name(card, link, node, "fe");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) EXPORT_SYMBOL_GPL(meson_card_set_fe_link);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) static int meson_card_add_links(struct snd_soc_card *card)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) 	struct meson_card *priv = snd_soc_card_get_drvdata(card);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) 	struct device_node *node = card->dev->of_node;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) 	struct device_node *np;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) 	int num, i, ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) 	num = of_get_child_count(node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) 	if (!num) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) 		dev_err(card->dev, "card has no links\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) 	ret = meson_card_reallocate_links(card, num);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) 	if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) 	i = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) 	for_each_child_of_node(node, np) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) 		ret = priv->match_data->add_link(card, np, &i);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) 		if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) 			of_node_put(np);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) 			return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) 		i++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) 	return 0;
^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) static int meson_card_parse_of_optional(struct snd_soc_card *card,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) 					const char *propname,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) 					int (*func)(struct snd_soc_card *c,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) 						    const char *p))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) 	/* If property is not provided, don't fail ... */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) 	if (!of_property_read_bool(card->dev->of_node, propname))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) 	/* ... but do fail if it is provided and the parsing fails */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) 	return func(card, propname);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) static void meson_card_clean_references(struct meson_card *priv)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) 	struct snd_soc_card *card = &priv->card;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) 	struct snd_soc_dai_link *link;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) 	struct snd_soc_dai_link_component *codec;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) 	struct snd_soc_aux_dev *aux;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) 	int i, j;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) 	if (card->dai_link) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) 		for_each_card_prelinks(card, i, link) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) 			if (link->cpus)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) 				of_node_put(link->cpus->of_node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) 			for_each_link_codecs(link, j, codec)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) 				of_node_put(codec->of_node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) 	if (card->aux_dev) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) 		for_each_card_pre_auxs(card, i, aux)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) 			of_node_put(aux->dlc.of_node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) 	kfree(card->dai_link);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) 	kfree(priv->link_data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) int meson_card_probe(struct platform_device *pdev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) 	const struct meson_card_match_data *data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) 	struct device *dev = &pdev->dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) 	struct meson_card *priv;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) 	data = of_device_get_match_data(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) 	if (!data) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) 		dev_err(dev, "failed to match device\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) 		return -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) 	priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) 	if (!priv)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) 	platform_set_drvdata(pdev, priv);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) 	snd_soc_card_set_drvdata(&priv->card, priv);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) 	priv->card.owner = THIS_MODULE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) 	priv->card.dev = dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) 	priv->match_data = data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) 	ret = snd_soc_of_parse_card_name(&priv->card, "model");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) 	if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) 	ret = meson_card_parse_of_optional(&priv->card, "audio-routing",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) 					   snd_soc_of_parse_audio_routing);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) 	if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) 		dev_err(dev, "error while parsing routing\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) 	ret = meson_card_parse_of_optional(&priv->card, "audio-widgets",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) 					   snd_soc_of_parse_audio_simple_widgets);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) 	if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) 		dev_err(dev, "error while parsing widgets\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325) 	ret = meson_card_add_links(&priv->card);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326) 	if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327) 		goto out_err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329) 	ret = snd_soc_of_parse_aux_devs(&priv->card, "audio-aux-devs");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) 	if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331) 		goto out_err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333) 	ret = devm_snd_soc_register_card(dev, &priv->card);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334) 	if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335) 		goto out_err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339) out_err:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340) 	meson_card_clean_references(priv);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343) EXPORT_SYMBOL_GPL(meson_card_probe);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345) int meson_card_remove(struct platform_device *pdev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347) 	struct meson_card *priv = platform_get_drvdata(pdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349) 	meson_card_clean_references(priv);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353) EXPORT_SYMBOL_GPL(meson_card_remove);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 355) MODULE_DESCRIPTION("Amlogic Sound Card Utils");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 356) MODULE_AUTHOR("Jerome Brunet <jbrunet@baylibre.com>");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 357) MODULE_LICENSE("GPL v2");