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 },
46 MODULE_DEVICE_TABLE(of, rsrc_card_of_match);
48 #define DAI_NAME_NUM 32
49 struct rsrc_card_dai {
53 char dai_name[DAI_NAME_NUM];
58 struct rsrc_card_priv {
59 struct snd_soc_card snd_card;
60 struct snd_soc_codec_conf codec_conf;
61 struct rsrc_card_dai *dai_props;
62 struct snd_soc_dai_link *dai_link;
67 #define rsrc_priv_to_dev(priv) ((priv)->snd_card.dev)
68 #define rsrc_priv_to_link(priv, i) ((priv)->snd_card.dai_link + (i))
69 #define rsrc_priv_to_props(priv, i) ((priv)->dai_props + (i))
70 #define rsrc_dev_to_of_data(dev) (of_match_device(rsrc_card_of_match, (dev))->data)
72 static int rsrc_card_startup(struct snd_pcm_substream *substream)
74 struct snd_soc_pcm_runtime *rtd = substream->private_data;
75 struct rsrc_card_priv *priv = snd_soc_card_get_drvdata(rtd->card);
76 struct rsrc_card_dai *dai_props =
77 rsrc_priv_to_props(priv, rtd - rtd->card->rtd);
79 return clk_prepare_enable(dai_props->clk);
82 static void rsrc_card_shutdown(struct snd_pcm_substream *substream)
84 struct snd_soc_pcm_runtime *rtd = substream->private_data;
85 struct rsrc_card_priv *priv = snd_soc_card_get_drvdata(rtd->card);
86 struct rsrc_card_dai *dai_props =
87 rsrc_priv_to_props(priv, rtd - rtd->card->rtd);
89 clk_disable_unprepare(dai_props->clk);
92 static struct snd_soc_ops rsrc_card_ops = {
93 .startup = rsrc_card_startup,
94 .shutdown = rsrc_card_shutdown,
97 static int rsrc_card_dai_init(struct snd_soc_pcm_runtime *rtd)
99 struct rsrc_card_priv *priv = snd_soc_card_get_drvdata(rtd->card);
100 struct snd_soc_dai *dai;
101 struct snd_soc_dai_link *dai_link;
102 struct rsrc_card_dai *dai_props;
103 int num = rtd - rtd->card->rtd;
106 dai_link = rsrc_priv_to_link(priv, num);
107 dai_props = rsrc_priv_to_props(priv, num);
108 dai = dai_link->dynamic ?
112 if (dai_props->fmt) {
113 ret = snd_soc_dai_set_fmt(dai, dai_props->fmt);
114 if (ret && ret != -ENOTSUPP) {
115 dev_err(dai->dev, "set_fmt error\n");
120 if (dai_props->sysclk) {
121 ret = snd_soc_dai_set_sysclk(dai, 0, dai_props->sysclk, 0);
122 if (ret && ret != -ENOTSUPP) {
123 dev_err(dai->dev, "set_sysclk error\n");
134 static int rsrc_card_be_hw_params_fixup(struct snd_soc_pcm_runtime *rtd,
135 struct snd_pcm_hw_params *params)
137 struct rsrc_card_priv *priv = snd_soc_card_get_drvdata(rtd->card);
138 struct snd_interval *rate = hw_param_interval(params,
139 SNDRV_PCM_HW_PARAM_RATE);
141 if (!priv->convert_rate)
144 rate->min = rate->max = priv->convert_rate;
149 static int rsrc_card_parse_daifmt(struct device_node *node,
150 struct device_node *np,
151 struct rsrc_card_priv *priv,
154 struct rsrc_card_dai *dai_props = rsrc_priv_to_props(priv, idx);
155 struct device_node *bitclkmaster = NULL;
156 struct device_node *framemaster = NULL;
157 struct device_node *codec = is_fe ? NULL : np;
160 daifmt = snd_soc_of_parse_daifmt(node, NULL,
161 &bitclkmaster, &framemaster);
162 daifmt &= ~SND_SOC_DAIFMT_MASTER_MASK;
164 if (!bitclkmaster && !framemaster)
167 if (codec == bitclkmaster)
168 daifmt |= (codec == framemaster) ?
169 SND_SOC_DAIFMT_CBM_CFM : SND_SOC_DAIFMT_CBM_CFS;
171 daifmt |= (codec == framemaster) ?
172 SND_SOC_DAIFMT_CBS_CFM : SND_SOC_DAIFMT_CBS_CFS;
174 dai_props->fmt = daifmt;
176 of_node_put(bitclkmaster);
177 of_node_put(framemaster);
182 static int rsrc_card_parse_links(struct device_node *np,
183 struct rsrc_card_priv *priv,
186 struct snd_soc_dai_link *dai_link = rsrc_priv_to_link(priv, idx);
187 struct rsrc_card_dai *dai_props = rsrc_priv_to_props(priv, idx);
188 struct of_phandle_args args;
192 * Get node via "sound-dai = <&phandle port>"
193 * it will be used as xxx_of_node on soc_bind_dai_link()
195 ret = of_parse_phandle_with_args(np, "sound-dai",
196 "#sound-dai-cells", 0, &args);
202 dai_link->codec_of_node = NULL;
203 dai_link->codec_dai_name = "snd-soc-dummy-dai";
204 dai_link->codec_name = "snd-soc-dummy";
207 dai_link->dynamic = 1;
208 dai_link->dpcm_merged_format = 1;
209 dai_link->cpu_of_node = args.np;
210 snd_soc_of_get_dai_name(np, &dai_link->cpu_dai_name);
213 snprintf(dai_props->dai_name, DAI_NAME_NUM, "fe.%s",
214 dai_link->cpu_dai_name);
217 * In soc_bind_dai_link() will check cpu name after
218 * of_node matching if dai_link has cpu_dai_name.
219 * but, it will never match if name was created by
220 * fmt_single_name() remove cpu_dai_name if cpu_args
223 * fmt_multiple_name()
225 if (!args.args_count)
226 dai_link->cpu_dai_name = NULL;
228 struct device *dev = rsrc_priv_to_dev(priv);
229 const struct rsrc_card_of_data *of_data;
231 of_data = rsrc_dev_to_of_data(dev);
234 dai_link->cpu_of_node = NULL;
235 dai_link->cpu_dai_name = "snd-soc-dummy-dai";
236 dai_link->cpu_name = "snd-soc-dummy";
239 dai_link->no_pcm = 1;
240 dai_link->be_hw_params_fixup = rsrc_card_be_hw_params_fixup;
241 dai_link->codec_of_node = args.np;
242 snd_soc_of_get_dai_name(np, &dai_link->codec_dai_name);
244 /* additional name prefix */
245 priv->codec_conf.of_node = dai_link->codec_of_node;
246 priv->codec_conf.name_prefix = of_data->prefix;
249 snprintf(dai_props->dai_name, DAI_NAME_NUM, "be.%s",
250 dai_link->codec_dai_name);
253 /* Simple Card assumes platform == cpu */
254 dai_link->platform_of_node = dai_link->cpu_of_node;
255 dai_link->dpcm_playback = 1;
256 dai_link->dpcm_capture = 1;
257 dai_link->name = dai_props->dai_name;
258 dai_link->stream_name = dai_props->dai_name;
259 dai_link->ops = &rsrc_card_ops;
260 dai_link->init = rsrc_card_dai_init;
265 static int rsrc_card_parse_clk(struct device_node *np,
266 struct rsrc_card_priv *priv,
269 struct snd_soc_dai_link *dai_link = rsrc_priv_to_link(priv, idx);
270 struct rsrc_card_dai *dai_props = rsrc_priv_to_props(priv, idx);
272 struct device_node *of_np = is_fe ? dai_link->cpu_of_node :
273 dai_link->codec_of_node;
277 * Parse dai->sysclk come from "clocks = <&xxx>"
278 * (if system has common clock)
279 * or "system-clock-frequency = <xxx>"
280 * or device's module clock.
282 if (of_property_read_bool(np, "clocks")) {
283 clk = of_clk_get(np, 0);
287 dai_props->sysclk = clk_get_rate(clk);
288 dai_props->clk = clk;
289 } else if (!of_property_read_u32(np, "system-clock-frequency", &val)) {
290 dai_props->sysclk = val;
292 clk = of_clk_get(of_np, 0);
294 dai_props->sysclk = clk_get_rate(clk);
300 static int rsrc_card_dai_link_of(struct device_node *node,
301 struct device_node *np,
302 struct rsrc_card_priv *priv,
305 struct device *dev = rsrc_priv_to_dev(priv);
306 struct rsrc_card_dai *dai_props = rsrc_priv_to_props(priv, idx);
310 if (0 == strcmp(np->name, "cpu"))
313 ret = rsrc_card_parse_daifmt(node, np, priv, idx, is_fe);
317 ret = rsrc_card_parse_links(np, priv, idx, is_fe);
321 ret = rsrc_card_parse_clk(np, priv, idx, is_fe);
325 dev_dbg(dev, "\t%s / %04x / %d\n",
333 static int rsrc_card_parse_of(struct device_node *node,
334 struct rsrc_card_priv *priv,
337 const struct rsrc_card_of_data *of_data = rsrc_dev_to_of_data(dev);
338 struct rsrc_card_dai *props;
339 struct snd_soc_dai_link *links;
340 struct device_node *np;
347 num = of_get_child_count(node);
348 props = devm_kzalloc(dev, sizeof(*props) * num, GFP_KERNEL);
349 links = devm_kzalloc(dev, sizeof(*links) * num, GFP_KERNEL);
350 if (!props || !links)
353 priv->dai_props = props;
354 priv->dai_link = links;
357 /* Init snd_soc_card */
358 priv->snd_card.owner = THIS_MODULE;
359 priv->snd_card.dev = dev;
360 priv->snd_card.dai_link = priv->dai_link;
361 priv->snd_card.num_links = num;
362 priv->snd_card.codec_conf = &priv->codec_conf;
363 priv->snd_card.num_configs = 1;
364 priv->snd_card.of_dapm_routes = of_data->routes;
365 priv->snd_card.num_of_dapm_routes = of_data->num_routes;
367 /* Parse the card name from DT */
368 snd_soc_of_parse_card_name(&priv->snd_card, "card-name");
370 /* sampling rate convert */
371 of_property_read_u32(node, "convert-rate", &priv->convert_rate);
373 dev_dbg(dev, "New rsrc-audio-card: %s (%d)\n",
374 priv->snd_card.name ? priv->snd_card.name : "",
378 for_each_child_of_node(node, np) {
379 ret = rsrc_card_dai_link_of(node, np, priv, i);
385 if (!priv->snd_card.name)
386 priv->snd_card.name = priv->snd_card.dai_link->name;
391 /* Decrease the reference count of the device nodes */
392 static int rsrc_card_unref(struct snd_soc_card *card)
394 struct snd_soc_dai_link *dai_link;
397 for (num_links = 0, dai_link = card->dai_link;
398 num_links < card->num_links;
399 num_links++, dai_link++) {
400 of_node_put(dai_link->cpu_of_node);
401 of_node_put(dai_link->codec_of_node);
406 static int rsrc_card_probe(struct platform_device *pdev)
408 struct rsrc_card_priv *priv;
409 struct device_node *np = pdev->dev.of_node;
410 struct device *dev = &pdev->dev;
413 /* Allocate the private data */
414 priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
418 ret = rsrc_card_parse_of(np, priv, dev);
420 if (ret != -EPROBE_DEFER)
421 dev_err(dev, "parse error %d\n", ret);
425 snd_soc_card_set_drvdata(&priv->snd_card, priv);
427 ret = devm_snd_soc_register_card(&pdev->dev, &priv->snd_card);
431 rsrc_card_unref(&priv->snd_card);
436 static int rsrc_card_remove(struct platform_device *pdev)
438 struct snd_soc_card *card = platform_get_drvdata(pdev);
440 return rsrc_card_unref(card);
443 static struct platform_driver rsrc_card = {
445 .name = "renesas-src-audio-card",
446 .of_match_table = rsrc_card_of_match,
448 .probe = rsrc_card_probe,
449 .remove = rsrc_card_remove,
452 module_platform_driver(rsrc_card);
454 MODULE_ALIAS("platform:renesas-src-audio-card");
455 MODULE_LICENSE("GPL");
456 MODULE_DESCRIPTION("Renesas Sampling Rate Convert Sound Card");
457 MODULE_AUTHOR("Kuninori Morimoto <kuninori.morimoto.gx@renesas.com>");