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 static int __asoc_simple_card_dai_init(struct snd_soc_dai *dai,
18 struct asoc_simple_dai *set,
26 ret = snd_soc_dai_set_fmt(dai, daifmt);
28 if (ret == -ENOTSUPP) {
29 dev_dbg(dai->dev, "ASoC: set_fmt is not supported\n");
33 if (!ret && set->sysclk)
34 ret = snd_soc_dai_set_sysclk(dai, 0, set->sysclk, 0);
39 static int asoc_simple_card_dai_init(struct snd_soc_pcm_runtime *rtd)
41 struct asoc_simple_card_info *info =
42 snd_soc_card_get_drvdata(rtd->card);
43 struct snd_soc_dai *codec = rtd->codec_dai;
44 struct snd_soc_dai *cpu = rtd->cpu_dai;
45 unsigned int daifmt = info->daifmt;
48 ret = __asoc_simple_card_dai_init(codec, &info->codec_dai, daifmt);
52 ret = __asoc_simple_card_dai_init(cpu, &info->cpu_dai, daifmt);
60 asoc_simple_card_sub_parse_of(struct device_node *np,
61 struct asoc_simple_dai *dai,
62 struct device_node **node)
68 * get node via "sound-dai = <&phandle port>"
69 * it will be used as xxx_of_node on soc_bind_dai_link()
71 *node = of_parse_phandle(np, "sound-dai", 0);
76 ret = snd_soc_of_get_dai_name(np, &dai->name);
81 * bitclock-inversion, frame-inversion
82 * bitclock-master, frame-master
83 * and specific "format" if it has
85 dai->fmt = snd_soc_of_parse_daifmt(np, NULL);
88 * dai->sysclk come from
89 * "clocks = <&xxx>" (if system has common clock)
90 * or "system-clock-frequency = <xxx>"
91 * or device's module clock.
93 if (of_property_read_bool(np, "clocks")) {
94 clk = of_clk_get(np, 0);
100 dai->sysclk = clk_get_rate(clk);
101 } else if (of_property_read_bool(np, "system-clock-frequency")) {
102 of_property_read_u32(np,
103 "system-clock-frequency",
106 clk = of_clk_get(*node, 0);
108 dai->sysclk = clk_get_rate(clk);
119 static int asoc_simple_card_parse_of(struct device_node *node,
120 struct asoc_simple_card_info *info,
122 struct device_node **of_cpu,
123 struct device_node **of_codec,
124 struct device_node **of_platform)
126 struct device_node *np;
130 /* get CPU/CODEC common format via simple-audio-card,format */
131 info->daifmt = snd_soc_of_parse_daifmt(node, "simple-audio-card,") &
132 (SND_SOC_DAIFMT_FORMAT_MASK | SND_SOC_DAIFMT_INV_MASK);
135 if (of_property_read_bool(node, "simple-audio-card,routing")) {
136 ret = snd_soc_of_parse_audio_routing(&info->snd_card,
137 "simple-audio-card,routing");
144 np = of_get_child_by_name(node, "simple-audio-card,cpu");
146 ret = asoc_simple_card_sub_parse_of(np,
154 np = of_get_child_by_name(node, "simple-audio-card,codec");
156 ret = asoc_simple_card_sub_parse_of(np,
162 /* card name is created from CPU/CODEC dai name */
163 name = devm_kzalloc(dev,
164 strlen(info->cpu_dai.name) +
165 strlen(info->codec_dai.name) + 2,
167 sprintf(name, "%s-%s", info->cpu_dai.name, info->codec_dai.name);
168 info->name = info->card = name;
170 /* simple-card assumes platform == cpu */
171 *of_platform = *of_cpu;
173 dev_dbg(dev, "card-name : %s\n", info->card);
174 dev_dbg(dev, "platform : %04x\n", info->daifmt);
175 dev_dbg(dev, "cpu : %s / %04x / %d\n",
178 info->cpu_dai.sysclk);
179 dev_dbg(dev, "codec : %s / %04x / %d\n",
180 info->codec_dai.name,
182 info->codec_dai.sysclk);
187 static int asoc_simple_card_probe(struct platform_device *pdev)
189 struct asoc_simple_card_info *cinfo;
190 struct device_node *np = pdev->dev.of_node;
191 struct device_node *of_cpu, *of_codec, *of_platform;
192 struct device *dev = &pdev->dev;
198 if (np && of_device_is_available(np)) {
199 cinfo = devm_kzalloc(dev, sizeof(*cinfo), GFP_KERNEL);
202 cinfo->snd_card.dev = &pdev->dev;
203 ret = asoc_simple_card_parse_of(np, cinfo, dev,
208 if (ret != -EPROBE_DEFER)
209 dev_err(dev, "parse error %d\n", ret);
216 cinfo = pdev->dev.platform_data;
218 dev_err(dev, "no info for asoc-simple-card\n");
222 cinfo->snd_card.dev = &pdev->dev;
227 !cinfo->codec_dai.name ||
228 !(cinfo->codec || of_codec) ||
229 !(cinfo->platform || of_platform) ||
230 !(cinfo->cpu_dai.name || of_cpu)) {
231 dev_err(dev, "insufficient asoc_simple_card_info settings\n");
236 * init snd_soc_dai_link
238 cinfo->snd_link.name = cinfo->name;
239 cinfo->snd_link.stream_name = cinfo->name;
240 cinfo->snd_link.cpu_dai_name = cinfo->cpu_dai.name;
241 cinfo->snd_link.platform_name = cinfo->platform;
242 cinfo->snd_link.codec_name = cinfo->codec;
243 cinfo->snd_link.codec_dai_name = cinfo->codec_dai.name;
244 cinfo->snd_link.cpu_of_node = of_cpu;
245 cinfo->snd_link.codec_of_node = of_codec;
246 cinfo->snd_link.platform_of_node = of_platform;
247 cinfo->snd_link.init = asoc_simple_card_dai_init;
252 cinfo->snd_card.name = cinfo->card;
253 cinfo->snd_card.owner = THIS_MODULE;
254 cinfo->snd_card.dai_link = &cinfo->snd_link;
255 cinfo->snd_card.num_links = 1;
257 snd_soc_card_set_drvdata(&cinfo->snd_card, cinfo);
259 return devm_snd_soc_register_card(&pdev->dev, &cinfo->snd_card);
262 static const struct of_device_id asoc_simple_of_match[] = {
263 { .compatible = "simple-audio-card", },
266 MODULE_DEVICE_TABLE(of, asoc_simple_of_match);
268 static struct platform_driver asoc_simple_card = {
270 .name = "asoc-simple-card",
271 .owner = THIS_MODULE,
272 .of_match_table = asoc_simple_of_match,
274 .probe = asoc_simple_card_probe,
277 module_platform_driver(asoc_simple_card);
279 MODULE_ALIAS("platform:asoc-simple-card");
280 MODULE_LICENSE("GPL");
281 MODULE_DESCRIPTION("ASoC Simple Sound Card");
282 MODULE_AUTHOR("Kuninori Morimoto <kuninori.morimoto.gx@renesas.com>");