2 * ASoC simple sound card support
4 * Copyright (C) 2012 Renesas Solutions Corp.
5 * Kuninori Morimoto <kuninori.morimoto.gx@renesas.com>
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 as
9 * published by the Free Software Foundation.
11 #include <linux/clk.h>
12 #include <linux/module.h>
14 #include <linux/platform_device.h>
15 #include <linux/string.h>
16 #include <sound/simple_card.h>
18 struct simple_card_data {
19 struct snd_soc_card snd_card;
21 struct asoc_simple_dai cpu_dai;
22 struct asoc_simple_dai codec_dai;
23 struct snd_soc_dai_link snd_link;
26 static int __asoc_simple_card_dai_init(struct snd_soc_dai *dai,
27 struct asoc_simple_dai *set)
32 ret = snd_soc_dai_set_fmt(dai, set->fmt);
33 if (ret && ret != -ENOTSUPP) {
34 dev_err(dai->dev, "simple-card: set_fmt error\n");
40 ret = snd_soc_dai_set_sysclk(dai, 0, set->sysclk, 0);
41 if (ret && ret != -ENOTSUPP) {
42 dev_err(dai->dev, "simple-card: set_sysclk error\n");
53 static int asoc_simple_card_dai_init(struct snd_soc_pcm_runtime *rtd)
55 struct simple_card_data *priv =
56 snd_soc_card_get_drvdata(rtd->card);
57 struct snd_soc_dai *codec = rtd->codec_dai;
58 struct snd_soc_dai *cpu = rtd->cpu_dai;
61 ret = __asoc_simple_card_dai_init(codec, &priv->codec_dai);
65 ret = __asoc_simple_card_dai_init(cpu, &priv->cpu_dai);
73 asoc_simple_card_sub_parse_of(struct device_node *np,
75 struct asoc_simple_dai *dai,
76 const struct device_node **p_node,
79 struct device_node *node;
84 * get node via "sound-dai = <&phandle port>"
85 * it will be used as xxx_of_node on soc_bind_dai_link()
87 node = of_parse_phandle(np, "sound-dai", 0);
93 ret = snd_soc_of_get_dai_name(np, name);
98 * bitclock-inversion, frame-inversion
99 * bitclock-master, frame-master
100 * and specific "format" if it has
102 dai->fmt = snd_soc_of_parse_daifmt(np, NULL);
106 * dai->sysclk come from
107 * "clocks = <&xxx>" (if system has common clock)
108 * or "system-clock-frequency = <xxx>"
109 * or device's module clock.
111 if (of_property_read_bool(np, "clocks")) {
112 clk = of_clk_get(np, 0);
118 dai->sysclk = clk_get_rate(clk);
119 } else if (of_property_read_bool(np, "system-clock-frequency")) {
120 of_property_read_u32(np,
121 "system-clock-frequency",
124 clk = of_clk_get(node, 0);
126 dai->sysclk = clk_get_rate(clk);
137 static int asoc_simple_card_parse_of(struct device_node *node,
138 struct simple_card_data *priv,
141 struct snd_soc_dai_link *dai_link = priv->snd_card.dai_link;
142 struct device_node *np;
146 /* parsing the card name from DT */
147 snd_soc_of_parse_card_name(&priv->snd_card, "simple-audio-card,name");
149 /* get CPU/CODEC common format via simple-audio-card,format */
150 priv->daifmt = snd_soc_of_parse_daifmt(node, "simple-audio-card,") &
151 (SND_SOC_DAIFMT_FORMAT_MASK | SND_SOC_DAIFMT_INV_MASK);
154 if (of_property_read_bool(node, "simple-audio-card,routing")) {
155 ret = snd_soc_of_parse_audio_routing(&priv->snd_card,
156 "simple-audio-card,routing");
163 np = of_get_child_by_name(node, "simple-audio-card,cpu");
165 ret = asoc_simple_card_sub_parse_of(np, priv->daifmt,
167 &dai_link->cpu_of_node,
168 &dai_link->cpu_dai_name);
174 np = of_get_child_by_name(node, "simple-audio-card,codec");
176 ret = asoc_simple_card_sub_parse_of(np, priv->daifmt,
178 &dai_link->codec_of_node,
179 &dai_link->codec_dai_name);
183 if (!dai_link->cpu_dai_name || !dai_link->codec_dai_name)
186 /* card name is created from CPU/CODEC dai name */
187 name = devm_kzalloc(dev,
188 strlen(dai_link->cpu_dai_name) +
189 strlen(dai_link->codec_dai_name) + 2,
191 sprintf(name, "%s-%s", dai_link->cpu_dai_name,
192 dai_link->codec_dai_name);
193 if (!priv->snd_card.name)
194 priv->snd_card.name = name;
195 dai_link->name = dai_link->stream_name = name;
197 /* simple-card assumes platform == cpu */
198 dai_link->platform_of_node = dai_link->cpu_of_node;
200 dev_dbg(dev, "card-name : %s\n", name);
201 dev_dbg(dev, "platform : %04x\n", priv->daifmt);
202 dev_dbg(dev, "cpu : %s / %04x / %d\n",
203 dai_link->cpu_dai_name,
205 priv->cpu_dai.sysclk);
206 dev_dbg(dev, "codec : %s / %04x / %d\n",
207 dai_link->codec_dai_name,
209 priv->codec_dai.sysclk);
214 static int asoc_simple_card_probe(struct platform_device *pdev)
216 struct simple_card_data *priv;
217 struct snd_soc_dai_link *dai_link;
218 struct device_node *np = pdev->dev.of_node;
219 struct device *dev = &pdev->dev;
222 priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
229 priv->snd_card.owner = THIS_MODULE;
230 priv->snd_card.dev = dev;
231 dai_link = &priv->snd_link;
232 priv->snd_card.dai_link = dai_link;
233 priv->snd_card.num_links = 1;
235 if (np && of_device_is_available(np)) {
237 ret = asoc_simple_card_parse_of(np, priv, dev);
239 if (ret != -EPROBE_DEFER)
240 dev_err(dev, "parse error %d\n", ret);
244 struct asoc_simple_card_info *cinfo;
246 cinfo = dev->platform_data;
248 dev_err(dev, "no info for asoc-simple-card\n");
254 !cinfo->codec_dai.name ||
257 !cinfo->cpu_dai.name) {
258 dev_err(dev, "insufficient asoc_simple_card_info settings\n");
262 priv->snd_card.name = cinfo->card;
263 dai_link->name = cinfo->name;
264 dai_link->stream_name = cinfo->name;
265 dai_link->platform_name = cinfo->platform;
266 dai_link->codec_name = cinfo->codec;
267 dai_link->cpu_dai_name = cinfo->cpu_dai.name;
268 dai_link->codec_dai_name = cinfo->codec_dai.name;
269 memcpy(&priv->cpu_dai, &cinfo->cpu_dai,
270 sizeof(priv->cpu_dai));
271 memcpy(&priv->codec_dai, &cinfo->codec_dai,
272 sizeof(priv->codec_dai));
276 * init snd_soc_dai_link
278 dai_link->init = asoc_simple_card_dai_init;
280 snd_soc_card_set_drvdata(&priv->snd_card, priv);
282 return devm_snd_soc_register_card(&pdev->dev, &priv->snd_card);
285 static const struct of_device_id asoc_simple_of_match[] = {
286 { .compatible = "simple-audio-card", },
289 MODULE_DEVICE_TABLE(of, asoc_simple_of_match);
291 static struct platform_driver asoc_simple_card = {
293 .name = "asoc-simple-card",
294 .owner = THIS_MODULE,
295 .of_match_table = asoc_simple_of_match,
297 .probe = asoc_simple_card_probe,
300 module_platform_driver(asoc_simple_card);
302 MODULE_ALIAS("platform:asoc-simple-card");
303 MODULE_LICENSE("GPL");
304 MODULE_DESCRIPTION("ASoC Simple Sound Card");
305 MODULE_AUTHOR("Kuninori Morimoto <kuninori.morimoto.gx@renesas.com>");