2 * Renesas Sampling Rate Convert Sound Card for DPCM
4 * Copyright (C) 2015 Renesas Solutions Corp.
5 * Kuninori Morimoto <kuninori.morimoto.gx@renesas.com>
7 * based on ${LINUX}/sound/soc/generic/simple-card.c
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License version 2 as
11 * published by the Free Software Foundation.
13 #include <linux/clk.h>
14 #include <linux/device.h>
15 #include <linux/module.h>
17 #include <linux/of_device.h>
18 #include <linux/platform_device.h>
19 #include <linux/string.h>
20 #include <sound/jack.h>
21 #include <sound/soc.h>
22 #include <sound/soc-dai.h>
24 struct rsrc_card_of_data
{
26 const struct snd_soc_dapm_route
*routes
;
30 static const struct snd_soc_dapm_route routes_ssi0_ak4642
[] = {
31 {"ak4642 Playback", NULL
, "DAI0 Playback"},
32 {"DAI0 Capture", NULL
, "ak4642 Capture"},
35 static const struct rsrc_card_of_data routes_of_ssi0_ak4642
= {
37 .routes
= routes_ssi0_ak4642
,
38 .num_routes
= ARRAY_SIZE(routes_ssi0_ak4642
),
41 static const struct of_device_id rsrc_card_of_match
[] = {
42 { .compatible
= "renesas,rsrc-card,lager", .data
= &routes_of_ssi0_ak4642
},
43 { .compatible
= "renesas,rsrc-card,koelsch", .data
= &routes_of_ssi0_ak4642
},
44 { .compatible
= "renesas,rsrc-card", },
47 MODULE_DEVICE_TABLE(of
, rsrc_card_of_match
);
49 #define DAI_NAME_NUM 32
50 struct rsrc_card_dai
{
54 char dai_name
[DAI_NAME_NUM
];
59 struct rsrc_card_priv
{
60 struct snd_soc_card snd_card
;
61 struct snd_soc_codec_conf codec_conf
;
62 struct rsrc_card_dai
*dai_props
;
63 struct snd_soc_dai_link
*dai_link
;
68 #define rsrc_priv_to_dev(priv) ((priv)->snd_card.dev)
69 #define rsrc_priv_to_link(priv, i) ((priv)->snd_card.dai_link + (i))
70 #define rsrc_priv_to_props(priv, i) ((priv)->dai_props + (i))
71 #define rsrc_dev_to_of_data(dev) (of_match_device(rsrc_card_of_match, (dev))->data)
73 static int rsrc_card_startup(struct snd_pcm_substream
*substream
)
75 struct snd_soc_pcm_runtime
*rtd
= substream
->private_data
;
76 struct rsrc_card_priv
*priv
= snd_soc_card_get_drvdata(rtd
->card
);
77 struct rsrc_card_dai
*dai_props
=
78 rsrc_priv_to_props(priv
, rtd
- rtd
->card
->rtd
);
80 return clk_prepare_enable(dai_props
->clk
);
83 static void rsrc_card_shutdown(struct snd_pcm_substream
*substream
)
85 struct snd_soc_pcm_runtime
*rtd
= substream
->private_data
;
86 struct rsrc_card_priv
*priv
= snd_soc_card_get_drvdata(rtd
->card
);
87 struct rsrc_card_dai
*dai_props
=
88 rsrc_priv_to_props(priv
, rtd
- rtd
->card
->rtd
);
90 clk_disable_unprepare(dai_props
->clk
);
93 static struct snd_soc_ops rsrc_card_ops
= {
94 .startup
= rsrc_card_startup
,
95 .shutdown
= rsrc_card_shutdown
,
98 static int rsrc_card_dai_init(struct snd_soc_pcm_runtime
*rtd
)
100 struct rsrc_card_priv
*priv
= snd_soc_card_get_drvdata(rtd
->card
);
101 struct snd_soc_dai
*dai
;
102 struct snd_soc_dai_link
*dai_link
;
103 struct rsrc_card_dai
*dai_props
;
104 int num
= rtd
- rtd
->card
->rtd
;
107 dai_link
= rsrc_priv_to_link(priv
, num
);
108 dai_props
= rsrc_priv_to_props(priv
, num
);
109 dai
= dai_link
->dynamic
?
113 if (dai_props
->fmt
) {
114 ret
= snd_soc_dai_set_fmt(dai
, dai_props
->fmt
);
115 if (ret
&& ret
!= -ENOTSUPP
) {
116 dev_err(dai
->dev
, "set_fmt error\n");
121 if (dai_props
->sysclk
) {
122 ret
= snd_soc_dai_set_sysclk(dai
, 0, dai_props
->sysclk
, 0);
123 if (ret
&& ret
!= -ENOTSUPP
) {
124 dev_err(dai
->dev
, "set_sysclk error\n");
135 static int rsrc_card_be_hw_params_fixup(struct snd_soc_pcm_runtime
*rtd
,
136 struct snd_pcm_hw_params
*params
)
138 struct rsrc_card_priv
*priv
= snd_soc_card_get_drvdata(rtd
->card
);
139 struct snd_interval
*rate
= hw_param_interval(params
,
140 SNDRV_PCM_HW_PARAM_RATE
);
142 if (!priv
->convert_rate
)
145 rate
->min
= rate
->max
= priv
->convert_rate
;
150 static int rsrc_card_parse_daifmt(struct device_node
*node
,
151 struct device_node
*np
,
152 struct rsrc_card_priv
*priv
,
155 struct rsrc_card_dai
*dai_props
= rsrc_priv_to_props(priv
, idx
);
156 struct device_node
*bitclkmaster
= NULL
;
157 struct device_node
*framemaster
= NULL
;
158 struct device_node
*codec
= is_fe
? NULL
: np
;
161 daifmt
= snd_soc_of_parse_daifmt(node
, NULL
,
162 &bitclkmaster
, &framemaster
);
163 daifmt
&= ~SND_SOC_DAIFMT_MASTER_MASK
;
165 if (!bitclkmaster
&& !framemaster
)
168 if (codec
== bitclkmaster
)
169 daifmt
|= (codec
== framemaster
) ?
170 SND_SOC_DAIFMT_CBM_CFM
: SND_SOC_DAIFMT_CBM_CFS
;
172 daifmt
|= (codec
== framemaster
) ?
173 SND_SOC_DAIFMT_CBS_CFM
: SND_SOC_DAIFMT_CBS_CFS
;
175 dai_props
->fmt
= daifmt
;
177 of_node_put(bitclkmaster
);
178 of_node_put(framemaster
);
183 static int rsrc_card_parse_links(struct device_node
*np
,
184 struct rsrc_card_priv
*priv
,
187 struct snd_soc_dai_link
*dai_link
= rsrc_priv_to_link(priv
, idx
);
188 struct rsrc_card_dai
*dai_props
= rsrc_priv_to_props(priv
, idx
);
189 struct of_phandle_args args
;
193 * Get node via "sound-dai = <&phandle port>"
194 * it will be used as xxx_of_node on soc_bind_dai_link()
196 ret
= of_parse_phandle_with_args(np
, "sound-dai",
197 "#sound-dai-cells", 0, &args
);
203 dai_link
->codec_of_node
= NULL
;
204 dai_link
->codec_dai_name
= "snd-soc-dummy-dai";
205 dai_link
->codec_name
= "snd-soc-dummy";
208 dai_link
->dynamic
= 1;
209 dai_link
->dpcm_merged_format
= 1;
210 dai_link
->cpu_of_node
= args
.np
;
211 snd_soc_of_get_dai_name(np
, &dai_link
->cpu_dai_name
);
214 snprintf(dai_props
->dai_name
, DAI_NAME_NUM
, "fe.%s",
215 dai_link
->cpu_dai_name
);
218 * In soc_bind_dai_link() will check cpu name after
219 * of_node matching if dai_link has cpu_dai_name.
220 * but, it will never match if name was created by
221 * fmt_single_name() remove cpu_dai_name if cpu_args
224 * fmt_multiple_name()
226 if (!args
.args_count
)
227 dai_link
->cpu_dai_name
= NULL
;
229 struct device
*dev
= rsrc_priv_to_dev(priv
);
230 const struct rsrc_card_of_data
*of_data
;
232 of_data
= rsrc_dev_to_of_data(dev
);
235 dai_link
->cpu_of_node
= NULL
;
236 dai_link
->cpu_dai_name
= "snd-soc-dummy-dai";
237 dai_link
->cpu_name
= "snd-soc-dummy";
240 dai_link
->no_pcm
= 1;
241 dai_link
->be_hw_params_fixup
= rsrc_card_be_hw_params_fixup
;
242 dai_link
->codec_of_node
= args
.np
;
243 snd_soc_of_get_dai_name(np
, &dai_link
->codec_dai_name
);
245 /* additional name prefix */
247 priv
->codec_conf
.of_node
= dai_link
->codec_of_node
;
248 priv
->codec_conf
.name_prefix
= of_data
->prefix
;
250 snd_soc_of_parse_audio_prefix(&priv
->snd_card
,
252 dai_link
->codec_of_node
,
257 snprintf(dai_props
->dai_name
, DAI_NAME_NUM
, "be.%s",
258 dai_link
->codec_dai_name
);
261 /* Simple Card assumes platform == cpu */
262 dai_link
->platform_of_node
= dai_link
->cpu_of_node
;
263 dai_link
->dpcm_playback
= 1;
264 dai_link
->dpcm_capture
= 1;
265 dai_link
->name
= dai_props
->dai_name
;
266 dai_link
->stream_name
= dai_props
->dai_name
;
267 dai_link
->ops
= &rsrc_card_ops
;
268 dai_link
->init
= rsrc_card_dai_init
;
273 static int rsrc_card_parse_clk(struct device_node
*np
,
274 struct rsrc_card_priv
*priv
,
277 struct snd_soc_dai_link
*dai_link
= rsrc_priv_to_link(priv
, idx
);
278 struct rsrc_card_dai
*dai_props
= rsrc_priv_to_props(priv
, idx
);
280 struct device_node
*of_np
= is_fe
? dai_link
->cpu_of_node
:
281 dai_link
->codec_of_node
;
285 * Parse dai->sysclk come from "clocks = <&xxx>"
286 * (if system has common clock)
287 * or "system-clock-frequency = <xxx>"
288 * or device's module clock.
290 if (of_property_read_bool(np
, "clocks")) {
291 clk
= of_clk_get(np
, 0);
295 dai_props
->sysclk
= clk_get_rate(clk
);
296 dai_props
->clk
= clk
;
297 } else if (!of_property_read_u32(np
, "system-clock-frequency", &val
)) {
298 dai_props
->sysclk
= val
;
300 clk
= of_clk_get(of_np
, 0);
302 dai_props
->sysclk
= clk_get_rate(clk
);
308 static int rsrc_card_dai_link_of(struct device_node
*node
,
309 struct device_node
*np
,
310 struct rsrc_card_priv
*priv
,
313 struct device
*dev
= rsrc_priv_to_dev(priv
);
314 struct rsrc_card_dai
*dai_props
= rsrc_priv_to_props(priv
, idx
);
318 if (0 == strcmp(np
->name
, "cpu"))
321 ret
= rsrc_card_parse_daifmt(node
, np
, priv
, idx
, is_fe
);
325 ret
= rsrc_card_parse_links(np
, priv
, idx
, is_fe
);
329 ret
= rsrc_card_parse_clk(np
, priv
, idx
, is_fe
);
333 dev_dbg(dev
, "\t%s / %04x / %d\n",
341 static int rsrc_card_parse_of(struct device_node
*node
,
342 struct rsrc_card_priv
*priv
,
345 const struct rsrc_card_of_data
*of_data
= rsrc_dev_to_of_data(dev
);
346 struct rsrc_card_dai
*props
;
347 struct snd_soc_dai_link
*links
;
348 struct device_node
*np
;
355 num
= of_get_child_count(node
);
356 props
= devm_kzalloc(dev
, sizeof(*props
) * num
, GFP_KERNEL
);
357 links
= devm_kzalloc(dev
, sizeof(*links
) * num
, GFP_KERNEL
);
358 if (!props
|| !links
)
361 priv
->dai_props
= props
;
362 priv
->dai_link
= links
;
365 /* Init snd_soc_card */
366 priv
->snd_card
.owner
= THIS_MODULE
;
367 priv
->snd_card
.dev
= dev
;
368 priv
->snd_card
.dai_link
= priv
->dai_link
;
369 priv
->snd_card
.num_links
= num
;
370 priv
->snd_card
.codec_conf
= &priv
->codec_conf
;
371 priv
->snd_card
.num_configs
= 1;
374 priv
->snd_card
.of_dapm_routes
= of_data
->routes
;
375 priv
->snd_card
.num_of_dapm_routes
= of_data
->num_routes
;
377 snd_soc_of_parse_audio_routing(&priv
->snd_card
,
381 /* Parse the card name from DT */
382 snd_soc_of_parse_card_name(&priv
->snd_card
, "card-name");
384 /* sampling rate convert */
385 of_property_read_u32(node
, "convert-rate", &priv
->convert_rate
);
387 dev_dbg(dev
, "New rsrc-audio-card: %s (%d)\n",
388 priv
->snd_card
.name
? priv
->snd_card
.name
: "",
392 for_each_child_of_node(node
, np
) {
393 ret
= rsrc_card_dai_link_of(node
, np
, priv
, i
);
399 if (!priv
->snd_card
.name
)
400 priv
->snd_card
.name
= priv
->snd_card
.dai_link
->name
;
405 /* Decrease the reference count of the device nodes */
406 static int rsrc_card_unref(struct snd_soc_card
*card
)
408 struct snd_soc_dai_link
*dai_link
;
411 for (num_links
= 0, dai_link
= card
->dai_link
;
412 num_links
< card
->num_links
;
413 num_links
++, dai_link
++) {
414 of_node_put(dai_link
->cpu_of_node
);
415 of_node_put(dai_link
->codec_of_node
);
420 static int rsrc_card_probe(struct platform_device
*pdev
)
422 struct rsrc_card_priv
*priv
;
423 struct device_node
*np
= pdev
->dev
.of_node
;
424 struct device
*dev
= &pdev
->dev
;
427 /* Allocate the private data */
428 priv
= devm_kzalloc(dev
, sizeof(*priv
), GFP_KERNEL
);
432 ret
= rsrc_card_parse_of(np
, priv
, dev
);
434 if (ret
!= -EPROBE_DEFER
)
435 dev_err(dev
, "parse error %d\n", ret
);
439 snd_soc_card_set_drvdata(&priv
->snd_card
, priv
);
441 ret
= devm_snd_soc_register_card(&pdev
->dev
, &priv
->snd_card
);
445 rsrc_card_unref(&priv
->snd_card
);
450 static int rsrc_card_remove(struct platform_device
*pdev
)
452 struct snd_soc_card
*card
= platform_get_drvdata(pdev
);
454 return rsrc_card_unref(card
);
457 static struct platform_driver rsrc_card
= {
459 .name
= "renesas-src-audio-card",
460 .of_match_table
= rsrc_card_of_match
,
462 .probe
= rsrc_card_probe
,
463 .remove
= rsrc_card_remove
,
466 module_platform_driver(rsrc_card
);
468 MODULE_ALIAS("platform:renesas-src-audio-card");
469 MODULE_LICENSE("GPL");
470 MODULE_DESCRIPTION("Renesas Sampling Rate Convert Sound Card");
471 MODULE_AUTHOR("Kuninori Morimoto <kuninori.morimoto.gx@renesas.com>");