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 static int __asoc_simple_card_dai_init(struct snd_soc_dai *dai,
19 struct asoc_simple_dai *set,
27 ret = snd_soc_dai_set_fmt(dai, daifmt);
29 if (ret == -ENOTSUPP) {
30 dev_dbg(dai->dev, "ASoC: set_fmt is not supported\n");
34 if (!ret && set->sysclk)
35 ret = snd_soc_dai_set_sysclk(dai, 0, set->sysclk, 0);
40 static int asoc_simple_card_dai_init(struct snd_soc_pcm_runtime *rtd)
42 struct asoc_simple_card_info *info =
43 snd_soc_card_get_drvdata(rtd->card);
44 struct snd_soc_dai *codec = rtd->codec_dai;
45 struct snd_soc_dai *cpu = rtd->cpu_dai;
46 unsigned int daifmt = info->daifmt;
49 ret = __asoc_simple_card_dai_init(codec, &info->codec_dai, daifmt);
53 ret = __asoc_simple_card_dai_init(cpu, &info->cpu_dai, daifmt);
61 asoc_simple_card_sub_parse_of(struct device_node *np,
62 struct asoc_simple_dai *dai,
63 struct device_node **node)
69 * get node via "sound-dai = <&phandle port>"
70 * it will be used as xxx_of_node on soc_bind_dai_link()
72 *node = of_parse_phandle(np, "sound-dai", 0);
77 ret = snd_soc_of_get_dai_name(np, &dai->name);
82 * bitclock-inversion, frame-inversion
83 * bitclock-master, frame-master
84 * and specific "format" if it has
86 dai->fmt = snd_soc_of_parse_daifmt(np, NULL);
89 * dai->sysclk come from
90 * "clocks = <&xxx>" (if system has common clock)
91 * or "system-clock-frequency = <xxx>"
92 * or device's module clock.
94 if (of_property_read_bool(np, "clocks")) {
95 clk = of_clk_get(np, 0);
101 dai->sysclk = clk_get_rate(clk);
102 } else if (of_property_read_bool(np, "system-clock-frequency")) {
103 of_property_read_u32(np,
104 "system-clock-frequency",
107 clk = of_clk_get(*node, 0);
109 dai->sysclk = clk_get_rate(clk);
120 static int asoc_simple_card_parse_of(struct device_node *node,
121 struct asoc_simple_card_info *info,
123 struct device_node **of_cpu,
124 struct device_node **of_codec,
125 struct device_node **of_platform)
127 struct device_node *np;
131 /* get CPU/CODEC common format via simple-audio-card,format */
132 info->daifmt = snd_soc_of_parse_daifmt(node, "simple-audio-card,") &
133 (SND_SOC_DAIFMT_FORMAT_MASK | SND_SOC_DAIFMT_INV_MASK);
136 if (of_property_read_bool(node, "simple-audio-card,routing")) {
137 ret = snd_soc_of_parse_audio_routing(&info->snd_card,
138 "simple-audio-card,routing");
145 np = of_get_child_by_name(node, "simple-audio-card,cpu");
147 ret = asoc_simple_card_sub_parse_of(np,
155 np = of_get_child_by_name(node, "simple-audio-card,codec");
157 ret = asoc_simple_card_sub_parse_of(np,
163 if (!info->cpu_dai.name || !info->codec_dai.name)
166 /* card name is created from CPU/CODEC dai name */
167 name = devm_kzalloc(dev,
168 strlen(info->cpu_dai.name) +
169 strlen(info->codec_dai.name) + 2,
171 sprintf(name, "%s-%s", info->cpu_dai.name, info->codec_dai.name);
172 info->snd_card.name = name;
173 info->snd_link.name = info->snd_link.stream_name = name;
175 /* simple-card assumes platform == cpu */
176 *of_platform = *of_cpu;
178 dev_dbg(dev, "card-name : %s\n", name);
179 dev_dbg(dev, "platform : %04x\n", info->daifmt);
180 dev_dbg(dev, "cpu : %s / %04x / %d\n",
183 info->cpu_dai.sysclk);
184 dev_dbg(dev, "codec : %s / %04x / %d\n",
185 info->codec_dai.name,
187 info->codec_dai.sysclk);
192 static int asoc_simple_card_probe(struct platform_device *pdev)
194 struct asoc_simple_card_info *cinfo;
195 struct device_node *np = pdev->dev.of_node;
196 struct device_node *of_cpu, *of_codec, *of_platform;
197 struct device *dev = &pdev->dev;
205 cinfo = devm_kzalloc(dev, sizeof(*cinfo), GFP_KERNEL);
209 if (np && of_device_is_available(np)) {
210 cinfo->snd_card.dev = dev;
212 ret = asoc_simple_card_parse_of(np, cinfo, dev,
217 if (ret != -EPROBE_DEFER)
218 dev_err(dev, "parse error %d\n", ret);
221 cinfo->snd_link.cpu_of_node = of_cpu;
222 cinfo->snd_link.codec_of_node = of_codec;
223 cinfo->snd_link.platform_of_node = of_platform;
225 if (!dev->platform_data) {
226 dev_err(dev, "no info for asoc-simple-card\n");
230 memcpy(cinfo, dev->platform_data, sizeof(*cinfo));
231 cinfo->snd_card.dev = dev;
235 !cinfo->codec_dai.name ||
238 !cinfo->cpu_dai.name) {
239 dev_err(dev, "insufficient asoc_simple_card_info settings\n");
243 cinfo->snd_card.name = cinfo->card;
244 cinfo->snd_link.name = cinfo->name;
245 cinfo->snd_link.stream_name = cinfo->name;
246 cinfo->snd_link.platform_name = cinfo->platform;
247 cinfo->snd_link.codec_name = cinfo->codec;
251 * init snd_soc_dai_link
253 cinfo->snd_link.cpu_dai_name = cinfo->cpu_dai.name;
254 cinfo->snd_link.codec_dai_name = cinfo->codec_dai.name;
255 cinfo->snd_link.init = asoc_simple_card_dai_init;
260 cinfo->snd_card.owner = THIS_MODULE;
261 cinfo->snd_card.dai_link = &cinfo->snd_link;
262 cinfo->snd_card.num_links = 1;
264 snd_soc_card_set_drvdata(&cinfo->snd_card, cinfo);
266 return devm_snd_soc_register_card(&pdev->dev, &cinfo->snd_card);
269 static const struct of_device_id asoc_simple_of_match[] = {
270 { .compatible = "simple-audio-card", },
273 MODULE_DEVICE_TABLE(of, asoc_simple_of_match);
275 static struct platform_driver asoc_simple_card = {
277 .name = "asoc-simple-card",
278 .owner = THIS_MODULE,
279 .of_match_table = asoc_simple_of_match,
281 .probe = asoc_simple_card_probe,
284 module_platform_driver(asoc_simple_card);
286 MODULE_ALIAS("platform:asoc-simple-card");
287 MODULE_LICENSE("GPL");
288 MODULE_DESCRIPTION("ASoC Simple Sound Card");
289 MODULE_AUTHOR("Kuninori Morimoto <kuninori.morimoto.gx@renesas.com>");