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
);
155 static int asoc_simple_card_parse_of(struct device_node
*node
,
156 struct simple_card_data
*priv
,
159 struct snd_soc_dai_link
*dai_link
= priv
->snd_card
.dai_link
;
160 struct device_node
*np
;
164 /* parsing the card name from DT */
165 snd_soc_of_parse_card_name(&priv
->snd_card
, "simple-audio-card,name");
167 /* get CPU/CODEC common format via simple-audio-card,format */
168 priv
->daifmt
= snd_soc_of_parse_daifmt(node
, "simple-audio-card,") &
169 (SND_SOC_DAIFMT_FORMAT_MASK
| SND_SOC_DAIFMT_INV_MASK
);
171 /* off-codec widgets */
172 if (of_property_read_bool(node
, "simple-audio-card,widgets")) {
173 ret
= snd_soc_of_parse_audio_simple_widgets(&priv
->snd_card
,
174 "simple-audio-card,widgets");
180 if (of_property_read_bool(node
, "simple-audio-card,routing")) {
181 ret
= snd_soc_of_parse_audio_routing(&priv
->snd_card
,
182 "simple-audio-card,routing");
189 np
= of_get_child_by_name(node
, "simple-audio-card,cpu");
191 ret
= asoc_simple_card_sub_parse_of(np
, priv
->daifmt
,
193 &dai_link
->cpu_of_node
,
194 &dai_link
->cpu_dai_name
);
200 np
= of_get_child_by_name(node
, "simple-audio-card,codec");
202 ret
= asoc_simple_card_sub_parse_of(np
, priv
->daifmt
,
204 &dai_link
->codec_of_node
,
205 &dai_link
->codec_dai_name
);
209 if (!dai_link
->cpu_dai_name
|| !dai_link
->codec_dai_name
)
212 /* card name is created from CPU/CODEC dai name */
213 name
= devm_kzalloc(dev
,
214 strlen(dai_link
->cpu_dai_name
) +
215 strlen(dai_link
->codec_dai_name
) + 2,
217 sprintf(name
, "%s-%s", dai_link
->cpu_dai_name
,
218 dai_link
->codec_dai_name
);
219 if (!priv
->snd_card
.name
)
220 priv
->snd_card
.name
= name
;
221 dai_link
->name
= dai_link
->stream_name
= name
;
223 /* simple-card assumes platform == cpu */
224 dai_link
->platform_of_node
= dai_link
->cpu_of_node
;
226 dev_dbg(dev
, "card-name : %s\n", name
);
227 dev_dbg(dev
, "platform : %04x\n", priv
->daifmt
);
228 dev_dbg(dev
, "cpu : %s / %04x / %d\n",
229 dai_link
->cpu_dai_name
,
231 priv
->cpu_dai
.sysclk
);
232 dev_dbg(dev
, "codec : %s / %04x / %d\n",
233 dai_link
->codec_dai_name
,
235 priv
->codec_dai
.sysclk
);
238 * soc_bind_dai_link() will check cpu name
239 * after of_node matching if dai_link has cpu_dai_name.
240 * but, it will never match if name was created by fmt_single_name()
241 * remove cpu_dai_name to escape name matching.
244 * fmt_multiple_name()
246 dai_link
->cpu_dai_name
= NULL
;
251 static int asoc_simple_card_probe(struct platform_device
*pdev
)
253 struct simple_card_data
*priv
;
254 struct snd_soc_dai_link
*dai_link
;
255 struct device_node
*np
= pdev
->dev
.of_node
;
256 struct device
*dev
= &pdev
->dev
;
259 priv
= devm_kzalloc(dev
, sizeof(*priv
), GFP_KERNEL
);
266 priv
->snd_card
.owner
= THIS_MODULE
;
267 priv
->snd_card
.dev
= dev
;
268 dai_link
= &priv
->snd_link
;
269 priv
->snd_card
.dai_link
= dai_link
;
270 priv
->snd_card
.num_links
= 1;
272 if (np
&& of_device_is_available(np
)) {
274 ret
= asoc_simple_card_parse_of(np
, priv
, dev
);
276 if (ret
!= -EPROBE_DEFER
)
277 dev_err(dev
, "parse error %d\n", ret
);
281 struct asoc_simple_card_info
*cinfo
;
283 cinfo
= dev
->platform_data
;
285 dev_err(dev
, "no info for asoc-simple-card\n");
291 !cinfo
->codec_dai
.name
||
294 !cinfo
->cpu_dai
.name
) {
295 dev_err(dev
, "insufficient asoc_simple_card_info settings\n");
299 priv
->snd_card
.name
= cinfo
->card
;
300 dai_link
->name
= cinfo
->name
;
301 dai_link
->stream_name
= cinfo
->name
;
302 dai_link
->platform_name
= cinfo
->platform
;
303 dai_link
->codec_name
= cinfo
->codec
;
304 dai_link
->cpu_dai_name
= cinfo
->cpu_dai
.name
;
305 dai_link
->codec_dai_name
= cinfo
->codec_dai
.name
;
306 memcpy(&priv
->cpu_dai
, &cinfo
->cpu_dai
,
307 sizeof(priv
->cpu_dai
));
308 memcpy(&priv
->codec_dai
, &cinfo
->codec_dai
,
309 sizeof(priv
->codec_dai
));
311 priv
->cpu_dai
.fmt
|= cinfo
->daifmt
;
312 priv
->codec_dai
.fmt
|= cinfo
->daifmt
;
316 * init snd_soc_dai_link
318 dai_link
->init
= asoc_simple_card_dai_init
;
320 snd_soc_card_set_drvdata(&priv
->snd_card
, priv
);
322 return devm_snd_soc_register_card(&pdev
->dev
, &priv
->snd_card
);
325 static const struct of_device_id asoc_simple_of_match
[] = {
326 { .compatible
= "simple-audio-card", },
329 MODULE_DEVICE_TABLE(of
, asoc_simple_of_match
);
331 static struct platform_driver asoc_simple_card
= {
333 .name
= "asoc-simple-card",
334 .owner
= THIS_MODULE
,
335 .of_match_table
= asoc_simple_of_match
,
337 .probe
= asoc_simple_card_probe
,
340 module_platform_driver(asoc_simple_card
);
342 MODULE_ALIAS("platform:asoc-simple-card");
343 MODULE_LICENSE("GPL");
344 MODULE_DESCRIPTION("ASoC Simple Sound Card");
345 MODULE_AUTHOR("Kuninori Morimoto <kuninori.morimoto.gx@renesas.com>");