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/device.h>
13 #include <linux/module.h>
15 #include <linux/platform_device.h>
16 #include <linux/string.h>
17 #include <sound/simple_card.h>
18 #include <sound/soc-dai.h>
19 #include <sound/soc.h>
21 struct simple_card_data {
22 struct snd_soc_card snd_card;
24 struct asoc_simple_dai cpu_dai;
25 struct asoc_simple_dai codec_dai;
26 struct snd_soc_dai_link snd_link;
29 static int __asoc_simple_card_dai_init(struct snd_soc_dai *dai,
30 struct asoc_simple_dai *set)
35 ret = snd_soc_dai_set_fmt(dai, set->fmt);
36 if (ret && ret != -ENOTSUPP) {
37 dev_err(dai->dev, "simple-card: set_fmt error\n");
43 ret = snd_soc_dai_set_sysclk(dai, 0, set->sysclk, 0);
44 if (ret && ret != -ENOTSUPP) {
45 dev_err(dai->dev, "simple-card: set_sysclk error\n");
51 ret = snd_soc_dai_set_tdm_slot(dai, 0, 0,
54 if (ret && ret != -ENOTSUPP) {
55 dev_err(dai->dev, "simple-card: set_tdm_slot error\n");
66 static int asoc_simple_card_dai_init(struct snd_soc_pcm_runtime *rtd)
68 struct simple_card_data *priv =
69 snd_soc_card_get_drvdata(rtd->card);
70 struct snd_soc_dai *codec = rtd->codec_dai;
71 struct snd_soc_dai *cpu = rtd->cpu_dai;
74 ret = __asoc_simple_card_dai_init(codec, &priv->codec_dai);
78 ret = __asoc_simple_card_dai_init(cpu, &priv->cpu_dai);
86 asoc_simple_card_sub_parse_of(struct device_node *np,
88 struct asoc_simple_dai *dai,
89 const struct device_node **p_node,
92 struct device_node *node;
97 * get node via "sound-dai = <&phandle port>"
98 * it will be used as xxx_of_node on soc_bind_dai_link()
100 node = of_parse_phandle(np, "sound-dai", 0);
106 ret = snd_soc_of_get_dai_name(np, name);
111 ret = snd_soc_of_parse_tdm_slot(np, &dai->slots, &dai->slot_width);
116 * bitclock-inversion, frame-inversion
117 * bitclock-master, frame-master
118 * and specific "format" if it has
120 dai->fmt = snd_soc_of_parse_daifmt(np, NULL);
124 * dai->sysclk come from
125 * "clocks = <&xxx>" (if system has common clock)
126 * or "system-clock-frequency = <xxx>"
127 * or device's module clock.
129 if (of_property_read_bool(np, "clocks")) {
130 clk = of_clk_get(np, 0);
136 dai->sysclk = clk_get_rate(clk);
137 } else if (of_property_read_bool(np, "system-clock-frequency")) {
138 of_property_read_u32(np,
139 "system-clock-frequency",
142 clk = of_clk_get(node, 0);
144 dai->sysclk = clk_get_rate(clk);
150 static int asoc_simple_card_parse_of(struct device_node *node,
151 struct simple_card_data *priv,
154 struct snd_soc_dai_link *dai_link = priv->snd_card.dai_link;
155 struct device_node *np;
159 /* parsing the card name from DT */
160 snd_soc_of_parse_card_name(&priv->snd_card, "simple-audio-card,name");
162 /* get CPU/CODEC common format via simple-audio-card,format */
163 priv->daifmt = snd_soc_of_parse_daifmt(node, "simple-audio-card,") &
164 (SND_SOC_DAIFMT_FORMAT_MASK | SND_SOC_DAIFMT_INV_MASK);
166 /* off-codec widgets */
167 if (of_property_read_bool(node, "simple-audio-card,widgets")) {
168 ret = snd_soc_of_parse_audio_simple_widgets(&priv->snd_card,
169 "simple-audio-card,widgets");
175 if (of_property_read_bool(node, "simple-audio-card,routing")) {
176 ret = snd_soc_of_parse_audio_routing(&priv->snd_card,
177 "simple-audio-card,routing");
184 np = of_get_child_by_name(node, "simple-audio-card,cpu");
186 ret = asoc_simple_card_sub_parse_of(np, priv->daifmt,
188 &dai_link->cpu_of_node,
189 &dai_link->cpu_dai_name);
197 np = of_get_child_by_name(node, "simple-audio-card,codec");
199 ret = asoc_simple_card_sub_parse_of(np, priv->daifmt,
201 &dai_link->codec_of_node,
202 &dai_link->codec_dai_name);
208 if (!dai_link->cpu_dai_name || !dai_link->codec_dai_name)
211 /* card name is created from CPU/CODEC dai name */
212 name = devm_kzalloc(dev,
213 strlen(dai_link->cpu_dai_name) +
214 strlen(dai_link->codec_dai_name) + 2,
216 sprintf(name, "%s-%s", dai_link->cpu_dai_name,
217 dai_link->codec_dai_name);
218 if (!priv->snd_card.name)
219 priv->snd_card.name = name;
220 dai_link->name = dai_link->stream_name = name;
222 /* simple-card assumes platform == cpu */
223 dai_link->platform_of_node = dai_link->cpu_of_node;
225 dev_dbg(dev, "card-name : %s\n", name);
226 dev_dbg(dev, "platform : %04x\n", priv->daifmt);
227 dev_dbg(dev, "cpu : %s / %04x / %d\n",
228 dai_link->cpu_dai_name,
230 priv->cpu_dai.sysclk);
231 dev_dbg(dev, "codec : %s / %04x / %d\n",
232 dai_link->codec_dai_name,
234 priv->codec_dai.sysclk);
237 * soc_bind_dai_link() will check cpu name
238 * after of_node matching if dai_link has cpu_dai_name.
239 * but, it will never match if name was created by fmt_single_name()
240 * remove cpu_dai_name to escape name matching.
243 * fmt_multiple_name()
245 dai_link->cpu_dai_name = NULL;
250 /* update the reference count of the devices nodes at end of probe */
251 static int asoc_simple_card_unref(struct platform_device *pdev)
253 struct snd_soc_card *card = platform_get_drvdata(pdev);
254 struct snd_soc_dai_link *dai_link;
255 struct device_node *np;
258 for (num_links = 0, dai_link = card->dai_link;
259 num_links < card->num_links;
260 num_links++, dai_link++) {
261 np = (struct device_node *) dai_link->cpu_of_node;
264 np = (struct device_node *) dai_link->codec_of_node;
271 static int asoc_simple_card_probe(struct platform_device *pdev)
273 struct simple_card_data *priv;
274 struct snd_soc_dai_link *dai_link;
275 struct device_node *np = pdev->dev.of_node;
276 struct device *dev = &pdev->dev;
279 priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
286 priv->snd_card.owner = THIS_MODULE;
287 priv->snd_card.dev = dev;
288 dai_link = &priv->snd_link;
289 priv->snd_card.dai_link = dai_link;
290 priv->snd_card.num_links = 1;
292 if (np && of_device_is_available(np)) {
294 ret = asoc_simple_card_parse_of(np, priv, dev);
296 if (ret != -EPROBE_DEFER)
297 dev_err(dev, "parse error %d\n", ret);
301 struct asoc_simple_card_info *cinfo;
303 cinfo = dev->platform_data;
305 dev_err(dev, "no info for asoc-simple-card\n");
310 !cinfo->codec_dai.name ||
313 !cinfo->cpu_dai.name) {
314 dev_err(dev, "insufficient asoc_simple_card_info settings\n");
318 priv->snd_card.name = (cinfo->card) ? cinfo->card : cinfo->name;
319 dai_link->name = cinfo->name;
320 dai_link->stream_name = cinfo->name;
321 dai_link->platform_name = cinfo->platform;
322 dai_link->codec_name = cinfo->codec;
323 dai_link->cpu_dai_name = cinfo->cpu_dai.name;
324 dai_link->codec_dai_name = cinfo->codec_dai.name;
325 memcpy(&priv->cpu_dai, &cinfo->cpu_dai,
326 sizeof(priv->cpu_dai));
327 memcpy(&priv->codec_dai, &cinfo->codec_dai,
328 sizeof(priv->codec_dai));
330 priv->cpu_dai.fmt |= cinfo->daifmt;
331 priv->codec_dai.fmt |= cinfo->daifmt;
335 * init snd_soc_dai_link
337 dai_link->init = asoc_simple_card_dai_init;
339 snd_soc_card_set_drvdata(&priv->snd_card, priv);
341 ret = devm_snd_soc_register_card(&pdev->dev, &priv->snd_card);
344 asoc_simple_card_unref(pdev);
348 static const struct of_device_id asoc_simple_of_match[] = {
349 { .compatible = "simple-audio-card", },
352 MODULE_DEVICE_TABLE(of, asoc_simple_of_match);
354 static struct platform_driver asoc_simple_card = {
356 .name = "asoc-simple-card",
357 .owner = THIS_MODULE,
358 .of_match_table = asoc_simple_of_match,
360 .probe = asoc_simple_card_probe,
363 module_platform_driver(asoc_simple_card);
365 MODULE_ALIAS("platform:asoc-simple-card");
366 MODULE_LICENSE("GPL");
367 MODULE_DESCRIPTION("ASoC Simple Sound Card");
368 MODULE_AUTHOR("Kuninori Morimoto <kuninori.morimoto.gx@renesas.com>");