2 * ASoC audio graph sound card support
4 * Copyright (C) 2016 Renesas Solutions Corp.
5 * Kuninori Morimoto <kuninori.morimoto.gx@renesas.com>
7 * based on ${LINUX}/sound/soc/generic/simple-card.c
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License version 2 as
11 * published by the Free Software Foundation.
13 #include <linux/clk.h>
14 #include <linux/device.h>
15 #include <linux/gpio.h>
16 #include <linux/gpio/consumer.h>
17 #include <linux/module.h>
19 #include <linux/of_device.h>
20 #include <linux/of_gpio.h>
21 #include <linux/of_graph.h>
22 #include <linux/platform_device.h>
23 #include <linux/string.h>
24 #include <sound/jack.h>
25 #include <sound/simple_card_utils.h>
27 struct graph_card_data
{
28 struct snd_soc_card snd_card
;
29 struct graph_dai_props
{
30 struct asoc_simple_dai cpu_dai
;
31 struct asoc_simple_dai codec_dai
;
33 struct snd_soc_dai_link
*dai_link
;
34 struct gpio_desc
*pa_gpio
;
37 static int asoc_graph_card_outdrv_event(struct snd_soc_dapm_widget
*w
,
38 struct snd_kcontrol
*kcontrol
,
41 struct snd_soc_dapm_context
*dapm
= w
->dapm
;
42 struct graph_card_data
*priv
= snd_soc_card_get_drvdata(dapm
->card
);
45 case SND_SOC_DAPM_POST_PMU
:
46 gpiod_set_value_cansleep(priv
->pa_gpio
, 1);
48 case SND_SOC_DAPM_PRE_PMD
:
49 gpiod_set_value_cansleep(priv
->pa_gpio
, 0);
58 static const struct snd_soc_dapm_widget asoc_graph_card_dapm_widgets
[] = {
59 SND_SOC_DAPM_OUT_DRV_E("Amplifier", SND_SOC_NOPM
,
60 0, 0, NULL
, 0, asoc_graph_card_outdrv_event
,
61 SND_SOC_DAPM_POST_PMU
| SND_SOC_DAPM_PRE_PMD
),
64 #define graph_priv_to_card(priv) (&(priv)->snd_card)
65 #define graph_priv_to_props(priv, i) ((priv)->dai_props + (i))
66 #define graph_priv_to_dev(priv) (graph_priv_to_card(priv)->dev)
67 #define graph_priv_to_link(priv, i) (graph_priv_to_card(priv)->dai_link + (i))
69 static int asoc_graph_card_startup(struct snd_pcm_substream
*substream
)
71 struct snd_soc_pcm_runtime
*rtd
= substream
->private_data
;
72 struct graph_card_data
*priv
= snd_soc_card_get_drvdata(rtd
->card
);
73 struct graph_dai_props
*dai_props
= graph_priv_to_props(priv
, rtd
->num
);
76 ret
= asoc_simple_card_clk_enable(&dai_props
->cpu_dai
);
80 ret
= asoc_simple_card_clk_enable(&dai_props
->codec_dai
);
82 asoc_simple_card_clk_disable(&dai_props
->cpu_dai
);
87 static void asoc_graph_card_shutdown(struct snd_pcm_substream
*substream
)
89 struct snd_soc_pcm_runtime
*rtd
= substream
->private_data
;
90 struct graph_card_data
*priv
= snd_soc_card_get_drvdata(rtd
->card
);
91 struct graph_dai_props
*dai_props
= graph_priv_to_props(priv
, rtd
->num
);
93 asoc_simple_card_clk_disable(&dai_props
->cpu_dai
);
95 asoc_simple_card_clk_disable(&dai_props
->codec_dai
);
98 static const struct snd_soc_ops asoc_graph_card_ops
= {
99 .startup
= asoc_graph_card_startup
,
100 .shutdown
= asoc_graph_card_shutdown
,
103 static int asoc_graph_card_dai_init(struct snd_soc_pcm_runtime
*rtd
)
105 struct graph_card_data
*priv
= snd_soc_card_get_drvdata(rtd
->card
);
106 struct snd_soc_dai
*codec
= rtd
->codec_dai
;
107 struct snd_soc_dai
*cpu
= rtd
->cpu_dai
;
108 struct graph_dai_props
*dai_props
=
109 graph_priv_to_props(priv
, rtd
->num
);
112 ret
= asoc_simple_card_init_dai(codec
, &dai_props
->codec_dai
);
116 ret
= asoc_simple_card_init_dai(cpu
, &dai_props
->cpu_dai
);
123 static int asoc_graph_card_dai_link_of(struct device_node
*cpu_port
,
124 struct graph_card_data
*priv
,
127 struct device
*dev
= graph_priv_to_dev(priv
);
128 struct snd_soc_dai_link
*dai_link
= graph_priv_to_link(priv
, idx
);
129 struct graph_dai_props
*dai_props
= graph_priv_to_props(priv
, idx
);
130 struct asoc_simple_dai
*cpu_dai
= &dai_props
->cpu_dai
;
131 struct asoc_simple_dai
*codec_dai
= &dai_props
->codec_dai
;
132 struct device_node
*cpu_ep
= of_get_next_child(cpu_port
, NULL
);
133 struct device_node
*codec_ep
= of_graph_get_remote_endpoint(cpu_ep
);
134 struct device_node
*rcpu_ep
= of_graph_get_remote_endpoint(codec_ep
);
137 if (rcpu_ep
!= cpu_ep
) {
138 dev_err(dev
, "remote-endpoint mismatch (%s/%s/%s)\n",
139 cpu_ep
->name
, codec_ep
->name
, rcpu_ep
->name
);
141 goto dai_link_of_err
;
144 ret
= asoc_simple_card_parse_daifmt(dev
, cpu_ep
, codec_ep
,
145 NULL
, &dai_link
->dai_fmt
);
147 goto dai_link_of_err
;
150 * we need to consider "mclk-fs" around here
154 ret
= asoc_simple_card_parse_graph_cpu(cpu_ep
, dai_link
);
156 goto dai_link_of_err
;
158 ret
= asoc_simple_card_parse_graph_codec(codec_ep
, dai_link
);
160 goto dai_link_of_err
;
162 ret
= asoc_simple_card_of_parse_tdm(cpu_ep
, cpu_dai
);
164 goto dai_link_of_err
;
166 ret
= asoc_simple_card_of_parse_tdm(codec_ep
, codec_dai
);
168 goto dai_link_of_err
;
170 ret
= asoc_simple_card_parse_clk_cpu(dev
, cpu_ep
, dai_link
, cpu_dai
);
172 goto dai_link_of_err
;
174 ret
= asoc_simple_card_parse_clk_codec(dev
, codec_ep
, dai_link
, codec_dai
);
176 goto dai_link_of_err
;
178 ret
= asoc_simple_card_canonicalize_dailink(dai_link
);
180 goto dai_link_of_err
;
182 ret
= asoc_simple_card_set_dailink_name(dev
, dai_link
,
184 dai_link
->cpu_dai_name
,
185 dai_link
->codec_dai_name
);
187 goto dai_link_of_err
;
189 dai_link
->ops
= &asoc_graph_card_ops
;
190 dai_link
->init
= asoc_graph_card_dai_init
;
192 asoc_simple_card_canonicalize_cpu(dai_link
,
193 of_graph_get_endpoint_count(dai_link
->cpu_of_node
) == 1);
197 of_node_put(rcpu_ep
);
198 of_node_put(codec_ep
);
203 static int asoc_graph_card_parse_of(struct graph_card_data
*priv
)
205 struct of_phandle_iterator it
;
206 struct device
*dev
= graph_priv_to_dev(priv
);
207 struct snd_soc_card
*card
= graph_priv_to_card(priv
);
208 struct device_node
*node
= dev
->of_node
;
212 ret
= asoc_simple_card_of_parse_widgets(card
, NULL
);
216 ret
= asoc_simple_card_of_parse_routing(card
, NULL
, 1);
221 * we need to consider "mclk-fs" around here
225 of_for_each_phandle(&it
, rc
, node
, "dais", NULL
, 0) {
226 ret
= asoc_graph_card_dai_link_of(it
.node
, priv
, idx
++);
228 of_node_put(it
.node
);
234 return asoc_simple_card_parse_card_name(card
, NULL
);
237 static int asoc_graph_get_dais_count(struct device
*dev
)
239 struct of_phandle_iterator it
;
240 struct device_node
*node
= dev
->of_node
;
244 of_for_each_phandle(&it
, rc
, node
, "dais", NULL
, 0)
250 static int asoc_graph_card_probe(struct platform_device
*pdev
)
252 struct graph_card_data
*priv
;
253 struct snd_soc_dai_link
*dai_link
;
254 struct graph_dai_props
*dai_props
;
255 struct device
*dev
= &pdev
->dev
;
256 struct snd_soc_card
*card
;
259 /* Allocate the private data and the DAI link array */
260 priv
= devm_kzalloc(dev
, sizeof(*priv
), GFP_KERNEL
);
264 num
= asoc_graph_get_dais_count(dev
);
268 dai_props
= devm_kzalloc(dev
, sizeof(*dai_props
) * num
, GFP_KERNEL
);
269 dai_link
= devm_kzalloc(dev
, sizeof(*dai_link
) * num
, GFP_KERNEL
);
270 if (!dai_props
|| !dai_link
)
273 priv
->pa_gpio
= devm_gpiod_get_optional(dev
, "pa", GPIOD_OUT_LOW
);
274 if (IS_ERR(priv
->pa_gpio
)) {
275 ret
= PTR_ERR(priv
->pa_gpio
);
276 dev_err(dev
, "failed to get amplifier gpio: %d\n", ret
);
280 priv
->dai_props
= dai_props
;
281 priv
->dai_link
= dai_link
;
283 /* Init snd_soc_card */
284 card
= graph_priv_to_card(priv
);
285 card
->owner
= THIS_MODULE
;
287 card
->dai_link
= dai_link
;
288 card
->num_links
= num
;
289 card
->dapm_widgets
= asoc_graph_card_dapm_widgets
;
290 card
->num_dapm_widgets
= ARRAY_SIZE(asoc_graph_card_dapm_widgets
);
292 ret
= asoc_graph_card_parse_of(priv
);
294 if (ret
!= -EPROBE_DEFER
)
295 dev_err(dev
, "parse error %d\n", ret
);
299 snd_soc_card_set_drvdata(card
, priv
);
301 ret
= devm_snd_soc_register_card(dev
, card
);
307 asoc_simple_card_clean_reference(card
);
312 static int asoc_graph_card_remove(struct platform_device
*pdev
)
314 struct snd_soc_card
*card
= platform_get_drvdata(pdev
);
316 return asoc_simple_card_clean_reference(card
);
319 static const struct of_device_id asoc_graph_of_match
[] = {
320 { .compatible
= "audio-graph-card", },
323 MODULE_DEVICE_TABLE(of
, asoc_graph_of_match
);
325 static struct platform_driver asoc_graph_card
= {
327 .name
= "asoc-audio-graph-card",
328 .pm
= &snd_soc_pm_ops
,
329 .of_match_table
= asoc_graph_of_match
,
331 .probe
= asoc_graph_card_probe
,
332 .remove
= asoc_graph_card_remove
,
334 module_platform_driver(asoc_graph_card
);
336 MODULE_ALIAS("platform:asoc-audio-graph-card");
337 MODULE_LICENSE("GPL v2");
338 MODULE_DESCRIPTION("ASoC Audio Graph Sound Card");
339 MODULE_AUTHOR("Kuninori Morimoto <kuninori.morimoto.gx@renesas.com>");