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
{
52 unsigned int tx_slot_mask
;
53 unsigned int rx_slot_mask
;
57 char dai_name
[DAI_NAME_NUM
];
62 struct rsrc_card_priv
{
63 struct snd_soc_card snd_card
;
64 struct snd_soc_codec_conf codec_conf
;
65 struct rsrc_card_dai
*dai_props
;
66 struct snd_soc_dai_link
*dai_link
;
72 #define rsrc_priv_to_dev(priv) ((priv)->snd_card.dev)
73 #define rsrc_priv_to_link(priv, i) ((priv)->snd_card.dai_link + (i))
74 #define rsrc_priv_to_props(priv, i) ((priv)->dai_props + (i))
76 static int rsrc_card_startup(struct snd_pcm_substream
*substream
)
78 struct snd_soc_pcm_runtime
*rtd
= substream
->private_data
;
79 struct rsrc_card_priv
*priv
= snd_soc_card_get_drvdata(rtd
->card
);
80 struct rsrc_card_dai
*dai_props
=
81 rsrc_priv_to_props(priv
, rtd
->num
);
83 return clk_prepare_enable(dai_props
->clk
);
86 static void rsrc_card_shutdown(struct snd_pcm_substream
*substream
)
88 struct snd_soc_pcm_runtime
*rtd
= substream
->private_data
;
89 struct rsrc_card_priv
*priv
= snd_soc_card_get_drvdata(rtd
->card
);
90 struct rsrc_card_dai
*dai_props
=
91 rsrc_priv_to_props(priv
, rtd
->num
);
93 clk_disable_unprepare(dai_props
->clk
);
96 static struct snd_soc_ops rsrc_card_ops
= {
97 .startup
= rsrc_card_startup
,
98 .shutdown
= rsrc_card_shutdown
,
101 static int rsrc_card_dai_init(struct snd_soc_pcm_runtime
*rtd
)
103 struct rsrc_card_priv
*priv
= snd_soc_card_get_drvdata(rtd
->card
);
104 struct snd_soc_dai
*dai
;
105 struct snd_soc_dai_link
*dai_link
;
106 struct rsrc_card_dai
*dai_props
;
110 dai_link
= rsrc_priv_to_link(priv
, num
);
111 dai_props
= rsrc_priv_to_props(priv
, num
);
112 dai
= dai_link
->dynamic
?
116 if (dai_props
->sysclk
) {
117 ret
= snd_soc_dai_set_sysclk(dai
, 0, dai_props
->sysclk
, 0);
118 if (ret
&& ret
!= -ENOTSUPP
) {
119 dev_err(dai
->dev
, "set_sysclk error\n");
124 if (dai_props
->slots
) {
125 ret
= snd_soc_dai_set_tdm_slot(dai
,
126 dai_props
->tx_slot_mask
,
127 dai_props
->rx_slot_mask
,
129 dai_props
->slot_width
);
130 if (ret
&& ret
!= -ENOTSUPP
) {
131 dev_err(dai
->dev
, "set_tdm_slot error\n");
142 static int rsrc_card_be_hw_params_fixup(struct snd_soc_pcm_runtime
*rtd
,
143 struct snd_pcm_hw_params
*params
)
145 struct rsrc_card_priv
*priv
= snd_soc_card_get_drvdata(rtd
->card
);
146 struct snd_interval
*rate
= hw_param_interval(params
,
147 SNDRV_PCM_HW_PARAM_RATE
);
148 struct snd_interval
*channels
= hw_param_interval(params
,
149 SNDRV_PCM_HW_PARAM_CHANNELS
);
151 if (priv
->convert_rate
)
153 rate
->max
= priv
->convert_rate
;
155 if (priv
->convert_channels
)
157 channels
->max
= priv
->convert_channels
;
162 static int rsrc_card_parse_daifmt(struct device_node
*node
,
163 struct device_node
*codec
,
164 struct rsrc_card_priv
*priv
,
165 struct snd_soc_dai_link
*dai_link
,
166 unsigned int *retfmt
)
168 struct device_node
*bitclkmaster
= NULL
;
169 struct device_node
*framemaster
= NULL
;
172 daifmt
= snd_soc_of_parse_daifmt(node
, NULL
,
173 &bitclkmaster
, &framemaster
);
174 daifmt
&= ~SND_SOC_DAIFMT_MASTER_MASK
;
176 if (!bitclkmaster
&& !framemaster
)
179 if (codec
== bitclkmaster
)
180 daifmt
|= (codec
== framemaster
) ?
181 SND_SOC_DAIFMT_CBM_CFM
: SND_SOC_DAIFMT_CBM_CFS
;
183 daifmt
|= (codec
== framemaster
) ?
184 SND_SOC_DAIFMT_CBS_CFM
: SND_SOC_DAIFMT_CBS_CFS
;
186 of_node_put(bitclkmaster
);
187 of_node_put(framemaster
);
194 static int rsrc_card_parse_links(struct device_node
*np
,
195 struct rsrc_card_priv
*priv
,
198 struct snd_soc_dai_link
*dai_link
= rsrc_priv_to_link(priv
, idx
);
199 struct rsrc_card_dai
*dai_props
= rsrc_priv_to_props(priv
, idx
);
200 struct of_phandle_args args
;
204 * Get node via "sound-dai = <&phandle port>"
205 * it will be used as xxx_of_node on soc_bind_dai_link()
207 ret
= of_parse_phandle_with_args(np
, "sound-dai",
208 "#sound-dai-cells", 0, &args
);
213 ret
= snd_soc_of_parse_tdm_slot(np
,
214 &dai_props
->tx_slot_mask
,
215 &dai_props
->rx_slot_mask
,
217 &dai_props
->slot_width
);
223 dai_link
->codec_of_node
= NULL
;
224 dai_link
->codec_dai_name
= "snd-soc-dummy-dai";
225 dai_link
->codec_name
= "snd-soc-dummy";
228 dai_link
->dynamic
= 1;
229 dai_link
->dpcm_merged_format
= 1;
230 dai_link
->cpu_of_node
= args
.np
;
231 ret
= snd_soc_of_get_dai_name(np
, &dai_link
->cpu_dai_name
);
236 snprintf(dai_props
->dai_name
, DAI_NAME_NUM
, "fe.%s",
237 dai_link
->cpu_dai_name
);
240 * In soc_bind_dai_link() will check cpu name after
241 * of_node matching if dai_link has cpu_dai_name.
242 * but, it will never match if name was created by
243 * fmt_single_name() remove cpu_dai_name if cpu_args
246 * fmt_multiple_name()
248 if (!args
.args_count
)
249 dai_link
->cpu_dai_name
= NULL
;
251 struct device
*dev
= rsrc_priv_to_dev(priv
);
252 const struct rsrc_card_of_data
*of_data
;
254 of_data
= of_device_get_match_data(dev
);
257 dai_link
->cpu_of_node
= NULL
;
258 dai_link
->cpu_dai_name
= "snd-soc-dummy-dai";
259 dai_link
->cpu_name
= "snd-soc-dummy";
262 dai_link
->no_pcm
= 1;
263 dai_link
->be_hw_params_fixup
= rsrc_card_be_hw_params_fixup
;
264 dai_link
->codec_of_node
= args
.np
;
265 ret
= snd_soc_of_get_dai_name(np
, &dai_link
->codec_dai_name
);
269 /* additional name prefix */
271 priv
->codec_conf
.of_node
= dai_link
->codec_of_node
;
272 priv
->codec_conf
.name_prefix
= of_data
->prefix
;
274 snd_soc_of_parse_audio_prefix(&priv
->snd_card
,
276 dai_link
->codec_of_node
,
281 snprintf(dai_props
->dai_name
, DAI_NAME_NUM
, "be.%s",
282 dai_link
->codec_dai_name
);
285 /* Simple Card assumes platform == cpu */
286 dai_link
->platform_of_node
= dai_link
->cpu_of_node
;
287 dai_link
->dpcm_playback
= 1;
288 dai_link
->dpcm_capture
= 1;
289 dai_link
->name
= dai_props
->dai_name
;
290 dai_link
->stream_name
= dai_props
->dai_name
;
291 dai_link
->ops
= &rsrc_card_ops
;
292 dai_link
->init
= rsrc_card_dai_init
;
297 static int rsrc_card_parse_clk(struct device_node
*np
,
298 struct rsrc_card_priv
*priv
,
301 struct snd_soc_dai_link
*dai_link
= rsrc_priv_to_link(priv
, idx
);
302 struct rsrc_card_dai
*dai_props
= rsrc_priv_to_props(priv
, idx
);
304 struct device_node
*of_np
= is_fe
? dai_link
->cpu_of_node
:
305 dai_link
->codec_of_node
;
309 * Parse dai->sysclk come from "clocks = <&xxx>"
310 * (if system has common clock)
311 * or "system-clock-frequency = <xxx>"
312 * or device's module clock.
314 if (of_property_read_bool(np
, "clocks")) {
315 clk
= of_clk_get(np
, 0);
319 dai_props
->sysclk
= clk_get_rate(clk
);
320 dai_props
->clk
= clk
;
321 } else if (!of_property_read_u32(np
, "system-clock-frequency", &val
)) {
322 dai_props
->sysclk
= val
;
324 clk
= of_clk_get(of_np
, 0);
326 dai_props
->sysclk
= clk_get_rate(clk
);
332 static int rsrc_card_dai_sub_link_of(struct device_node
*node
,
333 struct device_node
*np
,
334 struct rsrc_card_priv
*priv
,
337 struct device
*dev
= rsrc_priv_to_dev(priv
);
338 struct snd_soc_dai_link
*dai_link
= rsrc_priv_to_link(priv
, idx
);
339 struct rsrc_card_dai
*dai_props
= rsrc_priv_to_props(priv
, idx
);
342 ret
= rsrc_card_parse_links(np
, priv
, idx
, is_fe
);
346 ret
= rsrc_card_parse_clk(np
, priv
, idx
, is_fe
);
350 dev_dbg(dev
, "\t%s / %04x / %d\n",
358 static int rsrc_card_dai_link_of(struct device_node
*node
,
359 struct rsrc_card_priv
*priv
)
361 struct snd_soc_dai_link
*dai_link
;
362 struct device_node
*np
;
363 unsigned int daifmt
= 0;
369 for_each_child_of_node(node
, np
) {
370 dai_link
= rsrc_priv_to_link(priv
, i
);
372 if (strcmp(np
->name
, "codec") == 0) {
373 ret
= rsrc_card_parse_daifmt(node
, np
, priv
,
383 for_each_child_of_node(node
, np
) {
384 dai_link
= rsrc_priv_to_link(priv
, i
);
385 dai_link
->dai_fmt
= daifmt
;
388 if (strcmp(np
->name
, "cpu") == 0)
391 ret
= rsrc_card_dai_sub_link_of(node
, np
, priv
, i
, is_fe
);
400 static int rsrc_card_parse_of(struct device_node
*node
,
401 struct rsrc_card_priv
*priv
,
404 const struct rsrc_card_of_data
*of_data
= of_device_get_match_data(dev
);
405 struct rsrc_card_dai
*props
;
406 struct snd_soc_dai_link
*links
;
413 num
= of_get_child_count(node
);
414 props
= devm_kzalloc(dev
, sizeof(*props
) * num
, GFP_KERNEL
);
415 links
= devm_kzalloc(dev
, sizeof(*links
) * num
, GFP_KERNEL
);
416 if (!props
|| !links
)
419 priv
->dai_props
= props
;
420 priv
->dai_link
= links
;
423 /* Init snd_soc_card */
424 priv
->snd_card
.owner
= THIS_MODULE
;
425 priv
->snd_card
.dev
= dev
;
426 priv
->snd_card
.dai_link
= priv
->dai_link
;
427 priv
->snd_card
.num_links
= num
;
428 priv
->snd_card
.codec_conf
= &priv
->codec_conf
;
429 priv
->snd_card
.num_configs
= 1;
432 priv
->snd_card
.of_dapm_routes
= of_data
->routes
;
433 priv
->snd_card
.num_of_dapm_routes
= of_data
->num_routes
;
435 snd_soc_of_parse_audio_routing(&priv
->snd_card
,
439 /* Parse the card name from DT */
440 snd_soc_of_parse_card_name(&priv
->snd_card
, "card-name");
442 /* sampling rate convert */
443 of_property_read_u32(node
, "convert-rate", &priv
->convert_rate
);
445 /* channels transfer */
446 of_property_read_u32(node
, "convert-channels", &priv
->convert_channels
);
448 dev_dbg(dev
, "New rsrc-audio-card: %s\n",
449 priv
->snd_card
.name
? priv
->snd_card
.name
: "");
450 dev_dbg(dev
, "SRC : convert_rate %d\n", priv
->convert_rate
);
451 dev_dbg(dev
, "CTU : convert_channels %d\n", priv
->convert_channels
);
453 ret
= rsrc_card_dai_link_of(node
, priv
);
457 if (!priv
->snd_card
.name
)
458 priv
->snd_card
.name
= priv
->snd_card
.dai_link
->name
;
463 /* Decrease the reference count of the device nodes */
464 static int rsrc_card_unref(struct snd_soc_card
*card
)
466 struct snd_soc_dai_link
*dai_link
;
469 for (num_links
= 0, dai_link
= card
->dai_link
;
470 num_links
< card
->num_links
;
471 num_links
++, dai_link
++) {
472 of_node_put(dai_link
->cpu_of_node
);
473 of_node_put(dai_link
->codec_of_node
);
478 static int rsrc_card_probe(struct platform_device
*pdev
)
480 struct rsrc_card_priv
*priv
;
481 struct device_node
*np
= pdev
->dev
.of_node
;
482 struct device
*dev
= &pdev
->dev
;
485 /* Allocate the private data */
486 priv
= devm_kzalloc(dev
, sizeof(*priv
), GFP_KERNEL
);
490 ret
= rsrc_card_parse_of(np
, priv
, dev
);
492 if (ret
!= -EPROBE_DEFER
)
493 dev_err(dev
, "parse error %d\n", ret
);
497 snd_soc_card_set_drvdata(&priv
->snd_card
, priv
);
499 ret
= devm_snd_soc_register_card(&pdev
->dev
, &priv
->snd_card
);
503 rsrc_card_unref(&priv
->snd_card
);
508 static int rsrc_card_remove(struct platform_device
*pdev
)
510 struct snd_soc_card
*card
= platform_get_drvdata(pdev
);
512 return rsrc_card_unref(card
);
515 static struct platform_driver rsrc_card
= {
517 .name
= "renesas-src-audio-card",
518 .of_match_table
= rsrc_card_of_match
,
520 .probe
= rsrc_card_probe
,
521 .remove
= rsrc_card_remove
,
524 module_platform_driver(rsrc_card
);
526 MODULE_ALIAS("platform:renesas-src-audio-card");
527 MODULE_LICENSE("GPL");
528 MODULE_DESCRIPTION("Renesas Sampling Rate Convert Sound Card");
529 MODULE_AUTHOR("Kuninori Morimoto <kuninori.morimoto.gx@renesas.com>");