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>
13 #include <linux/platform_device.h>
14 #include <linux/module.h>
15 #include <sound/simple_card.h>
17 #define asoc_simple_get_card_info(p) \
18 container_of(p->dai_link, struct asoc_simple_card_info, snd_link)
20 static int __asoc_simple_card_dai_init(struct snd_soc_dai *dai,
21 struct asoc_simple_dai *set,
29 ret = snd_soc_dai_set_fmt(dai, daifmt);
31 if (ret == -ENOTSUPP) {
32 dev_dbg(dai->dev, "ASoC: set_fmt is not supported\n");
36 if (!ret && set->sysclk)
37 ret = snd_soc_dai_set_sysclk(dai, 0, set->sysclk, 0);
42 static int asoc_simple_card_dai_init(struct snd_soc_pcm_runtime *rtd)
44 struct asoc_simple_card_info *info = asoc_simple_get_card_info(rtd);
45 struct snd_soc_dai *codec = rtd->codec_dai;
46 struct snd_soc_dai *cpu = rtd->cpu_dai;
47 unsigned int daifmt = info->daifmt;
50 ret = __asoc_simple_card_dai_init(codec, &info->codec_dai, daifmt);
54 ret = __asoc_simple_card_dai_init(cpu, &info->cpu_dai, daifmt);
62 asoc_simple_card_sub_parse_of(struct device_node *np,
63 struct asoc_simple_dai *dai,
64 struct device_node **node)
70 * get node via "sound-dai = <&phandle port>"
71 * it will be used as xxx_of_node on soc_bind_dai_link()
73 *node = of_parse_phandle(np, "sound-dai", 0);
78 ret = snd_soc_of_get_dai_name(np, &dai->name);
83 * bitclock-inversion, frame-inversion
84 * bitclock-master, frame-master
85 * and specific "format" if it has
87 dai->fmt = snd_soc_of_parse_daifmt(np, NULL);
90 * dai->sysclk come from
91 * "clocks = <&xxx>" (if system has common clock)
92 * or "system-clock-frequency = <xxx>"
93 * or device's module clock.
95 if (of_property_read_bool(np, "clocks")) {
96 clk = of_clk_get(np, 0);
102 dai->sysclk = clk_get_rate(clk);
103 } else if (of_property_read_bool(np, "system-clock-frequency")) {
104 of_property_read_u32(np,
105 "system-clock-frequency",
108 clk = of_clk_get(*node, 0);
114 dai->sysclk = clk_get_rate(clk);
125 static int asoc_simple_card_parse_of(struct device_node *node,
126 struct asoc_simple_card_info *info,
128 struct device_node **of_cpu,
129 struct device_node **of_codec,
130 struct device_node **of_platform)
132 struct device_node *np;
136 /* get CPU/CODEC common format via simple-audio-card,format */
137 info->daifmt = snd_soc_of_parse_daifmt(node, "simple-audio-card,") &
138 (SND_SOC_DAIFMT_FORMAT_MASK | SND_SOC_DAIFMT_INV_MASK);
141 ret = snd_soc_of_parse_audio_routing(&info->snd_card,
142 "simple-audio-routing");
148 np = of_get_child_by_name(node, "simple-audio-card,cpu");
150 ret = asoc_simple_card_sub_parse_of(np,
158 np = of_get_child_by_name(node, "simple-audio-card,codec");
160 ret = asoc_simple_card_sub_parse_of(np,
166 if (!info->cpu_dai.name || !info->codec_dai.name)
169 /* card name is created from CPU/CODEC dai name */
170 name = devm_kzalloc(dev,
171 strlen(info->cpu_dai.name) +
172 strlen(info->codec_dai.name) + 2,
174 sprintf(name, "%s-%s", info->cpu_dai.name, info->codec_dai.name);
175 info->name = info->card = name;
177 /* simple-card assumes platform == cpu */
178 *of_platform = *of_cpu;
180 dev_dbg(dev, "card-name : %s\n", info->card);
181 dev_dbg(dev, "platform : %04x\n", info->daifmt);
182 dev_dbg(dev, "cpu : %s / %04x / %d\n",
185 info->cpu_dai.sysclk);
186 dev_dbg(dev, "codec : %s / %04x / %d\n",
187 info->codec_dai.name,
189 info->codec_dai.sysclk);
194 static int asoc_simple_card_probe(struct platform_device *pdev)
196 struct asoc_simple_card_info *cinfo;
197 struct device_node *np = pdev->dev.of_node;
198 struct device_node *of_cpu, *of_codec, *of_platform;
199 struct device *dev = &pdev->dev;
205 if (np && of_device_is_available(np)) {
206 cinfo = devm_kzalloc(dev, sizeof(*cinfo), GFP_KERNEL);
209 cinfo->snd_card.dev = &pdev->dev;
210 ret = asoc_simple_card_parse_of(np, cinfo, dev,
215 if (ret != -EPROBE_DEFER)
216 dev_err(dev, "parse error %d\n", ret);
221 cinfo->snd_card.dev = &pdev->dev;
222 cinfo = pdev->dev.platform_data;
226 dev_err(dev, "no info for asoc-simple-card\n");
232 !cinfo->codec_dai.name ||
233 !(cinfo->codec || of_codec) ||
234 !(cinfo->platform || of_platform) ||
235 !(cinfo->cpu_dai.name || of_cpu)) {
236 dev_err(dev, "insufficient asoc_simple_card_info settings\n");
241 * init snd_soc_dai_link
243 cinfo->snd_link.name = cinfo->name;
244 cinfo->snd_link.stream_name = cinfo->name;
245 cinfo->snd_link.cpu_dai_name = cinfo->cpu_dai.name;
246 cinfo->snd_link.platform_name = cinfo->platform;
247 cinfo->snd_link.codec_name = cinfo->codec;
248 cinfo->snd_link.codec_dai_name = cinfo->codec_dai.name;
249 cinfo->snd_link.cpu_of_node = of_cpu;
250 cinfo->snd_link.codec_of_node = of_codec;
251 cinfo->snd_link.platform_of_node = of_platform;
252 cinfo->snd_link.init = asoc_simple_card_dai_init;
257 cinfo->snd_card.name = cinfo->card;
258 cinfo->snd_card.owner = THIS_MODULE;
259 cinfo->snd_card.dai_link = &cinfo->snd_link;
260 cinfo->snd_card.num_links = 1;
262 return devm_snd_soc_register_card(&pdev->dev, &cinfo->snd_card);
265 static const struct of_device_id asoc_simple_of_match[] = {
266 { .compatible = "simple-audio-card", },
269 MODULE_DEVICE_TABLE(of, asoc_simple_of_match);
271 static struct platform_driver asoc_simple_card = {
273 .name = "asoc-simple-card",
274 .owner = THIS_MODULE,
275 .of_match_table = asoc_simple_of_match,
277 .probe = asoc_simple_card_probe,
280 module_platform_driver(asoc_simple_card);
282 MODULE_ALIAS("platform:asoc-simple-card");
283 MODULE_LICENSE("GPL");
284 MODULE_DESCRIPTION("ASoC Simple Sound Card");
285 MODULE_AUTHOR("Kuninori Morimoto <kuninori.morimoto.gx@renesas.com>");