1 // SPDX-License-Identifier: GPL-2.0
3 // ASoC audio graph sound card support
5 // Copyright (C) 2016 Renesas Solutions Corp.
6 // Kuninori Morimoto <kuninori.morimoto.gx@renesas.com>
8 // based on ${LINUX}/sound/soc/generic/simple-card.c
10 #include <linux/clk.h>
11 #include <linux/device.h>
12 #include <linux/gpio.h>
13 #include <linux/gpio/consumer.h>
14 #include <linux/module.h>
16 #include <linux/of_device.h>
17 #include <linux/of_gpio.h>
18 #include <linux/of_graph.h>
19 #include <linux/platform_device.h>
20 #include <linux/string.h>
21 #include <sound/simple_card_utils.h>
23 struct graph_card_data {
24 struct snd_soc_card snd_card;
25 struct graph_dai_props {
26 struct asoc_simple_dai cpu_dai;
27 struct asoc_simple_dai codec_dai;
28 struct snd_soc_dai_link_component codecs; /* single codec */
29 struct snd_soc_dai_link_component platform;
33 struct asoc_simple_jack hp_jack;
34 struct asoc_simple_jack mic_jack;
35 struct snd_soc_dai_link *dai_link;
36 struct gpio_desc *pa_gpio;
39 #define graph_priv_to_card(priv) (&(priv)->snd_card)
40 #define graph_priv_to_props(priv, i) ((priv)->dai_props + (i))
41 #define graph_priv_to_dev(priv) (graph_priv_to_card(priv)->dev)
42 #define graph_priv_to_link(priv, i) (graph_priv_to_card(priv)->dai_link + (i))
44 static int asoc_graph_card_outdrv_event(struct snd_soc_dapm_widget *w,
45 struct snd_kcontrol *kcontrol,
48 struct snd_soc_dapm_context *dapm = w->dapm;
49 struct graph_card_data *priv = snd_soc_card_get_drvdata(dapm->card);
52 case SND_SOC_DAPM_POST_PMU:
53 gpiod_set_value_cansleep(priv->pa_gpio, 1);
55 case SND_SOC_DAPM_PRE_PMD:
56 gpiod_set_value_cansleep(priv->pa_gpio, 0);
65 static const struct snd_soc_dapm_widget asoc_graph_card_dapm_widgets[] = {
66 SND_SOC_DAPM_OUT_DRV_E("Amplifier", SND_SOC_NOPM,
67 0, 0, NULL, 0, asoc_graph_card_outdrv_event,
68 SND_SOC_DAPM_POST_PMU | SND_SOC_DAPM_PRE_PMD),
71 static int asoc_graph_card_startup(struct snd_pcm_substream *substream)
73 struct snd_soc_pcm_runtime *rtd = substream->private_data;
74 struct graph_card_data *priv = snd_soc_card_get_drvdata(rtd->card);
75 struct graph_dai_props *dai_props = graph_priv_to_props(priv, rtd->num);
78 ret = asoc_simple_card_clk_enable(&dai_props->cpu_dai);
82 ret = asoc_simple_card_clk_enable(&dai_props->codec_dai);
84 asoc_simple_card_clk_disable(&dai_props->cpu_dai);
89 static void asoc_graph_card_shutdown(struct snd_pcm_substream *substream)
91 struct snd_soc_pcm_runtime *rtd = substream->private_data;
92 struct graph_card_data *priv = snd_soc_card_get_drvdata(rtd->card);
93 struct graph_dai_props *dai_props = graph_priv_to_props(priv, rtd->num);
95 asoc_simple_card_clk_disable(&dai_props->cpu_dai);
97 asoc_simple_card_clk_disable(&dai_props->codec_dai);
100 static int asoc_graph_card_hw_params(struct snd_pcm_substream *substream,
101 struct snd_pcm_hw_params *params)
103 struct snd_soc_pcm_runtime *rtd = substream->private_data;
104 struct snd_soc_dai *codec_dai = rtd->codec_dai;
105 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
106 struct graph_card_data *priv = snd_soc_card_get_drvdata(rtd->card);
107 struct graph_dai_props *dai_props = graph_priv_to_props(priv, rtd->num);
108 unsigned int mclk, mclk_fs = 0;
112 mclk_fs = priv->mclk_fs;
113 else if (dai_props->mclk_fs)
114 mclk_fs = dai_props->mclk_fs;
117 mclk = params_rate(params) * mclk_fs;
118 ret = snd_soc_dai_set_sysclk(codec_dai, 0, mclk,
120 if (ret && ret != -ENOTSUPP)
123 ret = snd_soc_dai_set_sysclk(cpu_dai, 0, mclk,
125 if (ret && ret != -ENOTSUPP)
133 static const struct snd_soc_ops asoc_graph_card_ops = {
134 .startup = asoc_graph_card_startup,
135 .shutdown = asoc_graph_card_shutdown,
136 .hw_params = asoc_graph_card_hw_params,
139 static int asoc_graph_card_dai_init(struct snd_soc_pcm_runtime *rtd)
141 struct graph_card_data *priv = snd_soc_card_get_drvdata(rtd->card);
142 struct snd_soc_dai *codec = rtd->codec_dai;
143 struct snd_soc_dai *cpu = rtd->cpu_dai;
144 struct graph_dai_props *dai_props =
145 graph_priv_to_props(priv, rtd->num);
148 ret = asoc_simple_card_init_dai(codec, &dai_props->codec_dai);
152 ret = asoc_simple_card_init_dai(cpu, &dai_props->cpu_dai);
159 static int asoc_graph_card_dai_link_of(struct device_node *cpu_port,
160 struct graph_card_data *priv,
163 struct device *dev = graph_priv_to_dev(priv);
164 struct snd_soc_dai_link *dai_link = graph_priv_to_link(priv, idx);
165 struct graph_dai_props *dai_props = graph_priv_to_props(priv, idx);
166 struct asoc_simple_dai *cpu_dai = &dai_props->cpu_dai;
167 struct asoc_simple_dai *codec_dai = &dai_props->codec_dai;
168 struct device_node *cpu_ep = of_get_next_child(cpu_port, NULL);
169 struct device_node *codec_ep = of_graph_get_remote_endpoint(cpu_ep);
172 ret = asoc_simple_card_parse_daifmt(dev, cpu_ep, codec_ep,
173 NULL, &dai_link->dai_fmt);
175 goto dai_link_of_err;
177 of_property_read_u32(cpu_ep, "mclk-fs", &dai_props->mclk_fs);
178 of_property_read_u32(codec_ep, "mclk-fs", &dai_props->mclk_fs);
180 ret = asoc_simple_card_parse_graph_cpu(cpu_ep, dai_link);
182 goto dai_link_of_err;
184 ret = asoc_simple_card_parse_graph_codec(codec_ep, dai_link);
186 goto dai_link_of_err;
188 ret = asoc_simple_card_of_parse_tdm(cpu_ep, cpu_dai);
190 goto dai_link_of_err;
192 ret = asoc_simple_card_of_parse_tdm(codec_ep, codec_dai);
194 goto dai_link_of_err;
196 ret = asoc_simple_card_parse_clk_cpu(dev, cpu_ep, dai_link, cpu_dai);
198 goto dai_link_of_err;
200 ret = asoc_simple_card_parse_clk_codec(dev, codec_ep, dai_link, codec_dai);
202 goto dai_link_of_err;
204 ret = asoc_simple_card_canonicalize_dailink(dai_link);
206 goto dai_link_of_err;
208 ret = asoc_simple_card_set_dailink_name(dev, dai_link,
210 dai_link->cpu_dai_name,
211 dai_link->codecs->dai_name);
213 goto dai_link_of_err;
215 dai_link->ops = &asoc_graph_card_ops;
216 dai_link->init = asoc_graph_card_dai_init;
218 asoc_simple_card_canonicalize_cpu(dai_link,
219 of_graph_get_endpoint_count(dai_link->cpu_of_node) == 1);
223 of_node_put(codec_ep);
228 static int asoc_graph_card_parse_of(struct graph_card_data *priv)
230 struct of_phandle_iterator it;
231 struct device *dev = graph_priv_to_dev(priv);
232 struct snd_soc_card *card = graph_priv_to_card(priv);
233 struct device_node *node = dev->of_node;
237 ret = asoc_simple_card_of_parse_widgets(card, NULL);
241 ret = asoc_simple_card_of_parse_routing(card, NULL);
245 /* Factor to mclk, used in hw_params() */
246 of_property_read_u32(node, "mclk-fs", &priv->mclk_fs);
248 of_for_each_phandle(&it, rc, node, "dais", NULL, 0) {
249 ret = asoc_graph_card_dai_link_of(it.node, priv, idx++);
251 of_node_put(it.node);
257 return asoc_simple_card_parse_card_name(card, NULL);
260 static int asoc_graph_get_dais_count(struct device *dev)
262 struct of_phandle_iterator it;
263 struct device_node *node = dev->of_node;
267 of_for_each_phandle(&it, rc, node, "dais", NULL, 0)
273 static int asoc_graph_soc_card_probe(struct snd_soc_card *card)
275 struct graph_card_data *priv = snd_soc_card_get_drvdata(card);
278 ret = asoc_simple_card_init_hp(card, &priv->hp_jack, NULL);
282 ret = asoc_simple_card_init_mic(card, &priv->mic_jack, NULL);
289 static int asoc_graph_card_probe(struct platform_device *pdev)
291 struct graph_card_data *priv;
292 struct snd_soc_dai_link *dai_link;
293 struct graph_dai_props *dai_props;
294 struct device *dev = &pdev->dev;
295 struct snd_soc_card *card;
298 /* Allocate the private data and the DAI link array */
299 priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
303 num = asoc_graph_get_dais_count(dev);
307 dai_props = devm_kcalloc(dev, num, sizeof(*dai_props), GFP_KERNEL);
308 dai_link = devm_kcalloc(dev, num, sizeof(*dai_link), GFP_KERNEL);
309 if (!dai_props || !dai_link)
313 * Use snd_soc_dai_link_component instead of legacy style
314 * It is codec only. but cpu/platform will be supported in the future.
316 * soc-core.c :: snd_soc_init_multicodec()
318 for (i = 0; i < num; i++) {
319 dai_link[i].codecs = &dai_props[i].codecs;
320 dai_link[i].num_codecs = 1;
321 dai_link[i].platform = &dai_props[i].platform;
324 priv->pa_gpio = devm_gpiod_get_optional(dev, "pa", GPIOD_OUT_LOW);
325 if (IS_ERR(priv->pa_gpio)) {
326 ret = PTR_ERR(priv->pa_gpio);
327 dev_err(dev, "failed to get amplifier gpio: %d\n", ret);
331 priv->dai_props = dai_props;
332 priv->dai_link = dai_link;
334 /* Init snd_soc_card */
335 card = graph_priv_to_card(priv);
336 card->owner = THIS_MODULE;
338 card->dai_link = dai_link;
339 card->num_links = num;
340 card->dapm_widgets = asoc_graph_card_dapm_widgets;
341 card->num_dapm_widgets = ARRAY_SIZE(asoc_graph_card_dapm_widgets);
342 card->probe = asoc_graph_soc_card_probe;
344 ret = asoc_graph_card_parse_of(priv);
346 if (ret != -EPROBE_DEFER)
347 dev_err(dev, "parse error %d\n", ret);
351 snd_soc_card_set_drvdata(card, priv);
353 ret = devm_snd_soc_register_card(dev, card);
359 asoc_simple_card_clean_reference(card);
364 static int asoc_graph_card_remove(struct platform_device *pdev)
366 struct snd_soc_card *card = platform_get_drvdata(pdev);
368 return asoc_simple_card_clean_reference(card);
371 static const struct of_device_id asoc_graph_of_match[] = {
372 { .compatible = "audio-graph-card", },
375 MODULE_DEVICE_TABLE(of, asoc_graph_of_match);
377 static struct platform_driver asoc_graph_card = {
379 .name = "asoc-audio-graph-card",
380 .pm = &snd_soc_pm_ops,
381 .of_match_table = asoc_graph_of_match,
383 .probe = asoc_graph_card_probe,
384 .remove = asoc_graph_card_remove,
386 module_platform_driver(asoc_graph_card);
388 MODULE_ALIAS("platform:asoc-audio-graph-card");
389 MODULE_LICENSE("GPL v2");
390 MODULE_DESCRIPTION("ASoC Audio Graph Sound Card");
391 MODULE_AUTHOR("Kuninori Morimoto <kuninori.morimoto.gx@renesas.com>");