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;
23 struct simple_dai_props {
24 struct asoc_simple_dai cpu_dai;
25 struct asoc_simple_dai codec_dai;
27 struct snd_soc_dai_link dai_link[]; /* dynamically allocated */
30 static int __asoc_simple_card_dai_init(struct snd_soc_dai *dai,
31 struct asoc_simple_dai *set)
36 ret = snd_soc_dai_set_fmt(dai, set->fmt);
37 if (ret && ret != -ENOTSUPP) {
38 dev_err(dai->dev, "simple-card: set_fmt error\n");
44 ret = snd_soc_dai_set_sysclk(dai, 0, set->sysclk, 0);
45 if (ret && ret != -ENOTSUPP) {
46 dev_err(dai->dev, "simple-card: set_sysclk error\n");
52 ret = snd_soc_dai_set_tdm_slot(dai, 0, 0,
55 if (ret && ret != -ENOTSUPP) {
56 dev_err(dai->dev, "simple-card: set_tdm_slot error\n");
67 static int asoc_simple_card_dai_init(struct snd_soc_pcm_runtime *rtd)
69 struct simple_card_data *priv =
70 snd_soc_card_get_drvdata(rtd->card);
71 struct snd_soc_dai *codec = rtd->codec_dai;
72 struct snd_soc_dai *cpu = rtd->cpu_dai;
75 ret = __asoc_simple_card_dai_init(codec, &priv->dai_props->codec_dai);
79 ret = __asoc_simple_card_dai_init(cpu, &priv->dai_props->cpu_dai);
87 asoc_simple_card_sub_parse_of(struct device_node *np,
89 struct asoc_simple_dai *dai,
90 const struct device_node **p_node,
93 struct device_node *node;
98 * get node via "sound-dai = <&phandle port>"
99 * it will be used as xxx_of_node on soc_bind_dai_link()
101 node = of_parse_phandle(np, "sound-dai", 0);
107 ret = snd_soc_of_get_dai_name(np, name);
112 ret = snd_soc_of_parse_tdm_slot(np, &dai->slots, &dai->slot_width);
117 * bitclock-inversion, frame-inversion
118 * bitclock-master, frame-master
119 * and specific "format" if it has
121 dai->fmt = snd_soc_of_parse_daifmt(np, NULL);
125 * dai->sysclk come from
126 * "clocks = <&xxx>" (if system has common clock)
127 * or "system-clock-frequency = <xxx>"
128 * or device's module clock.
130 if (of_property_read_bool(np, "clocks")) {
131 clk = of_clk_get(np, 0);
137 dai->sysclk = clk_get_rate(clk);
138 } else if (of_property_read_bool(np, "system-clock-frequency")) {
139 of_property_read_u32(np,
140 "system-clock-frequency",
143 clk = of_clk_get(node, 0);
145 dai->sysclk = clk_get_rate(clk);
151 static int asoc_simple_card_parse_of(struct device_node *node,
152 struct simple_card_data *priv,
155 struct snd_soc_dai_link *dai_link = priv->snd_card.dai_link;
156 struct asoc_simple_dai *codec_dai = &priv->dai_props->codec_dai;
157 struct asoc_simple_dai *cpu_dai = &priv->dai_props->cpu_dai;
158 struct device_node *np;
163 /* parsing the card name from DT */
164 snd_soc_of_parse_card_name(&priv->snd_card, "simple-audio-card,name");
166 /* get CPU/CODEC common format via simple-audio-card,format */
167 daifmt = snd_soc_of_parse_daifmt(node, "simple-audio-card,") &
168 (SND_SOC_DAIFMT_FORMAT_MASK | SND_SOC_DAIFMT_INV_MASK);
170 /* off-codec widgets */
171 if (of_property_read_bool(node, "simple-audio-card,widgets")) {
172 ret = snd_soc_of_parse_audio_simple_widgets(&priv->snd_card,
173 "simple-audio-card,widgets");
179 if (of_property_read_bool(node, "simple-audio-card,routing")) {
180 ret = snd_soc_of_parse_audio_routing(&priv->snd_card,
181 "simple-audio-card,routing");
188 np = of_get_child_by_name(node, "simple-audio-card,cpu");
190 ret = asoc_simple_card_sub_parse_of(np, daifmt,
192 &dai_link->cpu_of_node,
193 &dai_link->cpu_dai_name);
201 np = of_get_child_by_name(node, "simple-audio-card,codec");
203 ret = asoc_simple_card_sub_parse_of(np, daifmt,
205 &dai_link->codec_of_node,
206 &dai_link->codec_dai_name);
213 * overwrite cpu_dai->fmt as its DAIFMT_MASTER bit is based on CODEC
214 * while the other bits should be identical unless buggy SW/HW design.
216 cpu_dai->fmt = codec_dai->fmt;
218 if (!dai_link->cpu_dai_name || !dai_link->codec_dai_name)
221 /* card name is created from CPU/CODEC dai name */
222 name = devm_kzalloc(dev,
223 strlen(dai_link->cpu_dai_name) +
224 strlen(dai_link->codec_dai_name) + 2,
226 sprintf(name, "%s-%s", dai_link->cpu_dai_name,
227 dai_link->codec_dai_name);
228 if (!priv->snd_card.name)
229 priv->snd_card.name = name;
230 dai_link->name = dai_link->stream_name = name;
232 /* simple-card assumes platform == cpu */
233 dai_link->platform_of_node = dai_link->cpu_of_node;
235 dev_dbg(dev, "card-name : %s\n", name);
236 dev_dbg(dev, "platform : %04x\n", daifmt);
237 dev_dbg(dev, "cpu : %s / %04x / %d\n",
238 dai_link->cpu_dai_name,
241 dev_dbg(dev, "codec : %s / %04x / %d\n",
242 dai_link->codec_dai_name,
247 * soc_bind_dai_link() will check cpu name
248 * after of_node matching if dai_link has cpu_dai_name.
249 * but, it will never match if name was created by fmt_single_name()
250 * remove cpu_dai_name to escape name matching.
253 * fmt_multiple_name()
255 dai_link->cpu_dai_name = NULL;
260 /* update the reference count of the devices nodes at end of probe */
261 static int asoc_simple_card_unref(struct platform_device *pdev)
263 struct snd_soc_card *card = platform_get_drvdata(pdev);
264 struct snd_soc_dai_link *dai_link;
265 struct device_node *np;
268 for (num_links = 0, dai_link = card->dai_link;
269 num_links < card->num_links;
270 num_links++, dai_link++) {
271 np = (struct device_node *) dai_link->cpu_of_node;
274 np = (struct device_node *) dai_link->codec_of_node;
281 static int asoc_simple_card_probe(struct platform_device *pdev)
283 struct simple_card_data *priv;
284 struct snd_soc_dai_link *dai_link;
285 struct device_node *np = pdev->dev.of_node;
286 struct device *dev = &pdev->dev;
289 /* allocate the private data and the DAI link array */
290 priv = devm_kzalloc(dev,
291 sizeof(*priv) + sizeof(*dai_link),
299 priv->snd_card.owner = THIS_MODULE;
300 priv->snd_card.dev = dev;
301 dai_link = priv->dai_link;
302 priv->snd_card.dai_link = dai_link;
303 priv->snd_card.num_links = 1;
305 /* get room for the other properties */
306 priv->dai_props = devm_kzalloc(dev,
307 sizeof(*priv->dai_props),
309 if (!priv->dai_props)
312 if (np && of_device_is_available(np)) {
314 ret = asoc_simple_card_parse_of(np, priv, dev);
316 if (ret != -EPROBE_DEFER)
317 dev_err(dev, "parse error %d\n", ret);
321 struct asoc_simple_card_info *cinfo;
323 cinfo = dev->platform_data;
325 dev_err(dev, "no info for asoc-simple-card\n");
330 !cinfo->codec_dai.name ||
333 !cinfo->cpu_dai.name) {
334 dev_err(dev, "insufficient asoc_simple_card_info settings\n");
338 priv->snd_card.name = (cinfo->card) ? cinfo->card : cinfo->name;
339 dai_link->name = cinfo->name;
340 dai_link->stream_name = cinfo->name;
341 dai_link->platform_name = cinfo->platform;
342 dai_link->codec_name = cinfo->codec;
343 dai_link->cpu_dai_name = cinfo->cpu_dai.name;
344 dai_link->codec_dai_name = cinfo->codec_dai.name;
345 memcpy(&priv->dai_props->cpu_dai, &cinfo->cpu_dai,
346 sizeof(priv->dai_props->cpu_dai));
347 memcpy(&priv->dai_props->codec_dai, &cinfo->codec_dai,
348 sizeof(priv->dai_props->codec_dai));
350 priv->dai_props->cpu_dai.fmt |= cinfo->daifmt;
351 priv->dai_props->codec_dai.fmt |= cinfo->daifmt;
355 * init snd_soc_dai_link
357 dai_link->init = asoc_simple_card_dai_init;
359 snd_soc_card_set_drvdata(&priv->snd_card, priv);
361 ret = devm_snd_soc_register_card(&pdev->dev, &priv->snd_card);
364 asoc_simple_card_unref(pdev);
368 static const struct of_device_id asoc_simple_of_match[] = {
369 { .compatible = "simple-audio-card", },
372 MODULE_DEVICE_TABLE(of, asoc_simple_of_match);
374 static struct platform_driver asoc_simple_card = {
376 .name = "asoc-simple-card",
377 .owner = THIS_MODULE,
378 .of_match_table = asoc_simple_of_match,
380 .probe = asoc_simple_card_probe,
383 module_platform_driver(asoc_simple_card);
385 MODULE_ALIAS("platform:asoc-simple-card");
386 MODULE_LICENSE("GPL");
387 MODULE_DESCRIPTION("ASoC Simple Sound Card");
388 MODULE_AUTHOR("Kuninori Morimoto <kuninori.morimoto.gx@renesas.com>");