ASoC: simple-card: Merge single and muti DAI link(s) code.
[deliverable/linux.git] / sound / soc / generic / simple-card.c
CommitLineData
f2390880
KM
1/*
2 * ASoC simple sound card support
3 *
4 * Copyright (C) 2012 Renesas Solutions Corp.
5 * Kuninori Morimoto <kuninori.morimoto.gx@renesas.com>
6 *
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.
10 */
fa558c28 11#include <linux/clk.h>
6ff62eed 12#include <linux/device.h>
f2390880 13#include <linux/module.h>
fa558c28 14#include <linux/of.h>
f2390880 15#include <linux/platform_device.h>
ca919fe4 16#include <linux/string.h>
f2390880 17#include <sound/simple_card.h>
6ff62eed
XL
18#include <sound/soc-dai.h>
19#include <sound/soc.h>
f2390880 20
45fce594
JFM
21struct simple_card_data {
22 struct snd_soc_card snd_card;
cf7dc23c
JFM
23 struct simple_dai_props {
24 struct asoc_simple_dai cpu_dai;
25 struct asoc_simple_dai codec_dai;
26 } *dai_props;
2942a0e2 27 unsigned int mclk_fs;
cf7dc23c 28 struct snd_soc_dai_link dai_link[]; /* dynamically allocated */
45fce594
JFM
29};
30
2942a0e2
AL
31static int asoc_simple_card_hw_params(struct snd_pcm_substream *substream,
32 struct snd_pcm_hw_params *params)
33{
34 struct snd_soc_pcm_runtime *rtd = substream->private_data;
35 struct snd_soc_dai *codec_dai = rtd->codec_dai;
36 struct simple_card_data *priv = snd_soc_card_get_drvdata(rtd->card);
37 unsigned int mclk;
38 int ret = 0;
39
40 if (priv->mclk_fs) {
41 mclk = params_rate(params) * priv->mclk_fs;
42 ret = snd_soc_dai_set_sysclk(codec_dai, 0, mclk,
43 SND_SOC_CLOCK_IN);
44 }
45
46 return ret;
47}
48
49static struct snd_soc_ops asoc_simple_card_ops = {
50 .hw_params = asoc_simple_card_hw_params,
51};
52
a4a2992c 53static int __asoc_simple_card_dai_init(struct snd_soc_dai *dai,
30d0341e 54 struct asoc_simple_dai *set)
a4a2992c 55{
4763ebe2 56 int ret;
a4a2992c 57
30d0341e
XL
58 if (set->fmt) {
59 ret = snd_soc_dai_set_fmt(dai, set->fmt);
4763ebe2
XL
60 if (ret && ret != -ENOTSUPP) {
61 dev_err(dai->dev, "simple-card: set_fmt error\n");
62 goto err;
63 }
e244bb9b
KM
64 }
65
4763ebe2 66 if (set->sysclk) {
a4a2992c 67 ret = snd_soc_dai_set_sysclk(dai, 0, set->sysclk, 0);
4763ebe2
XL
68 if (ret && ret != -ENOTSUPP) {
69 dev_err(dai->dev, "simple-card: set_sysclk error\n");
70 goto err;
71 }
72 }
73
6ff62eed
XL
74 if (set->slots) {
75 ret = snd_soc_dai_set_tdm_slot(dai, 0, 0,
76 set->slots,
77 set->slot_width);
78 if (ret && ret != -ENOTSUPP) {
79 dev_err(dai->dev, "simple-card: set_tdm_slot error\n");
80 goto err;
81 }
82 }
83
4763ebe2 84 ret = 0;
a4a2992c 85
4763ebe2 86err:
a4a2992c
KM
87 return ret;
88}
89
f2390880
KM
90static int asoc_simple_card_dai_init(struct snd_soc_pcm_runtime *rtd)
91{
781cbebe 92 struct simple_card_data *priv = snd_soc_card_get_drvdata(rtd->card);
f2390880
KM
93 struct snd_soc_dai *codec = rtd->codec_dai;
94 struct snd_soc_dai *cpu = rtd->cpu_dai;
6a91a17b
JFM
95 struct simple_dai_props *dai_props;
96 int num, ret;
f2390880 97
6a91a17b
JFM
98 num = rtd - rtd->card->rtd;
99 dai_props = &priv->dai_props[num];
100 ret = __asoc_simple_card_dai_init(codec, &dai_props->codec_dai);
a4a2992c
KM
101 if (ret < 0)
102 return ret;
f2390880 103
6a91a17b 104 ret = __asoc_simple_card_dai_init(cpu, &dai_props->cpu_dai);
a4a2992c
KM
105 if (ret < 0)
106 return ret;
f2390880
KM
107
108 return 0;
109}
110
fa558c28
KM
111static int
112asoc_simple_card_sub_parse_of(struct device_node *np,
113 struct asoc_simple_dai *dai,
8ea21348 114 struct device_node **p_node,
7c7b9cf5
KM
115 const char **name,
116 int *args_count)
fa558c28 117{
7c7b9cf5 118 struct of_phandle_args args;
201a0eac 119 struct device_node *node;
fa558c28 120 struct clk *clk;
c7099eb1 121 u32 val;
fa558c28
KM
122 int ret;
123
124 /*
125 * get node via "sound-dai = <&phandle port>"
126 * it will be used as xxx_of_node on soc_bind_dai_link()
127 */
7c7b9cf5
KM
128 ret = of_parse_phandle_with_args(np, "sound-dai",
129 "#sound-dai-cells", 0, &args);
130 if (ret)
131 return ret;
132
133 *p_node = args.np;
134
135 if (args_count)
136 *args_count = args.args_count;
fa558c28
KM
137
138 /* get dai->name */
52008472 139 ret = snd_soc_of_get_dai_name(np, name);
fa558c28 140 if (ret < 0)
e512e001 141 return ret;
fa558c28 142
6ff62eed
XL
143 /* parse TDM slot */
144 ret = snd_soc_of_parse_tdm_slot(np, &dai->slots, &dai->slot_width);
145 if (ret)
e512e001 146 return ret;
6ff62eed 147
fa558c28
KM
148 /*
149 * dai->sysclk come from
150 * "clocks = <&xxx>" (if system has common clock)
151 * or "system-clock-frequency = <xxx>"
71467e46 152 * or device's module clock.
fa558c28 153 */
71467e46
XL
154 if (of_property_read_bool(np, "clocks")) {
155 clk = of_clk_get(np, 0);
156 if (IS_ERR(clk)) {
157 ret = PTR_ERR(clk);
e512e001 158 return ret;
71467e46
XL
159 }
160
161 dai->sysclk = clk_get_rate(clk);
c7099eb1
JS
162 } else if (!of_property_read_u32(np, "system-clock-frequency", &val)) {
163 dai->sysclk = val;
71467e46 164 } else {
201a0eac 165 clk = of_clk_get(node, 0);
e2a19ac6
XL
166 if (!IS_ERR(clk))
167 dai->sysclk = clk_get_rate(clk);
71467e46 168 }
fa558c28 169
e512e001 170 return 0;
fa558c28
KM
171}
172
2d82eeb0
KM
173static int asoc_simple_card_dai_link_of(struct device_node *node,
174 struct device *dev,
175 struct snd_soc_dai_link *dai_link,
176 struct simple_dai_props *dai_props,
177 bool is_top_level_node)
6a91a17b 178{
b3ca11ff
JS
179 struct device_node *np = NULL;
180 struct device_node *bitclkmaster = NULL;
181 struct device_node *framemaster = NULL;
182 unsigned int daifmt;
183 char *name;
184 char prop[128];
185 char *prefix = "";
7c7b9cf5 186 int ret, cpu_args;
6a91a17b 187
2080437d 188 /* For single DAI link & old style of DT node */
64872215
JS
189 if (is_top_level_node)
190 prefix = "simple-audio-card,";
b3ca11ff
JS
191
192 daifmt = snd_soc_of_parse_daifmt(node, prefix,
193 &bitclkmaster, &framemaster);
194 daifmt &= ~SND_SOC_DAIFMT_MASTER_MASK;
195
196 snprintf(prop, sizeof(prop), "%scpu", prefix);
197 np = of_get_child_by_name(node, prop);
198 if (!np) {
199 ret = -EINVAL;
966b8063 200 dev_err(dev, "%s: Can't find %s DT node\n", __func__, prop);
b3ca11ff 201 goto dai_link_of_err;
6a91a17b 202 }
b3ca11ff
JS
203
204 ret = asoc_simple_card_sub_parse_of(np, &dai_props->cpu_dai,
205 &dai_link->cpu_of_node,
7c7b9cf5
KM
206 &dai_link->cpu_dai_name,
207 &cpu_args);
6a91a17b 208 if (ret < 0)
b3ca11ff
JS
209 goto dai_link_of_err;
210
211 dai_props->cpu_dai.fmt = daifmt;
781cbebe 212 switch (((np == bitclkmaster) << 4) | (np == framemaster)) {
b3ca11ff
JS
213 case 0x11:
214 dai_props->cpu_dai.fmt |= SND_SOC_DAIFMT_CBS_CFS;
215 break;
216 case 0x10:
217 dai_props->cpu_dai.fmt |= SND_SOC_DAIFMT_CBS_CFM;
218 break;
219 case 0x01:
220 dai_props->cpu_dai.fmt |= SND_SOC_DAIFMT_CBM_CFS;
221 break;
222 default:
223 dai_props->cpu_dai.fmt |= SND_SOC_DAIFMT_CBM_CFM;
224 break;
225 }
6a91a17b 226
b3ca11ff
JS
227 of_node_put(np);
228 snprintf(prop, sizeof(prop), "%scodec", prefix);
229 np = of_get_child_by_name(node, prop);
230 if (!np) {
231 ret = -EINVAL;
966b8063 232 dev_err(dev, "%s: Can't find %s DT node\n", __func__, prop);
b3ca11ff 233 goto dai_link_of_err;
6a91a17b 234 }
b3ca11ff
JS
235
236 ret = asoc_simple_card_sub_parse_of(np, &dai_props->codec_dai,
237 &dai_link->codec_of_node,
7c7b9cf5 238 &dai_link->codec_dai_name, NULL);
b3ca11ff
JS
239 if (ret < 0)
240 goto dai_link_of_err;
241
242 if (strlen(prefix) && !bitclkmaster && !framemaster) {
243 /* No dai-link level and master setting was not found from
244 sound node level, revert back to legacy DT parsing and
245 take the settings from codec node. */
246 dev_dbg(dev, "%s: Revert to legacy daifmt parsing\n",
247 __func__);
248 dai_props->cpu_dai.fmt = dai_props->codec_dai.fmt =
249 snd_soc_of_parse_daifmt(np, NULL, NULL, NULL) |
250 (daifmt & ~SND_SOC_DAIFMT_CLOCK_MASK);
251 } else {
252 dai_props->codec_dai.fmt = daifmt;
781cbebe 253 switch (((np == bitclkmaster) << 4) | (np == framemaster)) {
b3ca11ff
JS
254 case 0x11:
255 dai_props->codec_dai.fmt |= SND_SOC_DAIFMT_CBM_CFM;
256 break;
257 case 0x10:
258 dai_props->codec_dai.fmt |= SND_SOC_DAIFMT_CBM_CFS;
259 break;
260 case 0x01:
261 dai_props->codec_dai.fmt |= SND_SOC_DAIFMT_CBS_CFM;
262 break;
263 default:
264 dai_props->codec_dai.fmt |= SND_SOC_DAIFMT_CBS_CFS;
265 break;
266 }
267 }
268
269 if (!dai_link->cpu_dai_name || !dai_link->codec_dai_name) {
781cbebe
NC
270 ret = -EINVAL;
271 goto dai_link_of_err;
b3ca11ff
JS
272 }
273
274 /* simple-card assumes platform == cpu */
275 dai_link->platform_of_node = dai_link->cpu_of_node;
276
277 /* Link name is created from CPU/CODEC dai name */
278 name = devm_kzalloc(dev,
279 strlen(dai_link->cpu_dai_name) +
280 strlen(dai_link->codec_dai_name) + 2,
281 GFP_KERNEL);
282 sprintf(name, "%s-%s", dai_link->cpu_dai_name,
283 dai_link->codec_dai_name);
284 dai_link->name = dai_link->stream_name = name;
2942a0e2 285 dai_link->ops = &asoc_simple_card_ops;
a5960bd5 286 dai_link->init = asoc_simple_card_dai_init;
b3ca11ff
JS
287
288 dev_dbg(dev, "\tname : %s\n", dai_link->stream_name);
289 dev_dbg(dev, "\tcpu : %s / %04x / %d\n",
290 dai_link->cpu_dai_name,
291 dai_props->cpu_dai.fmt,
292 dai_props->cpu_dai.sysclk);
293 dev_dbg(dev, "\tcodec : %s / %04x / %d\n",
294 dai_link->codec_dai_name,
295 dai_props->codec_dai.fmt,
296 dai_props->codec_dai.sysclk);
297
179949bc
KM
298 /*
299 * soc_bind_dai_link() will check cpu name
300 * after of_node matching if dai_link has cpu_dai_name.
301 * but, it will never match if name was created by fmt_single_name()
7c7b9cf5 302 * remove cpu_dai_name if cpu_args was 0.
179949bc
KM
303 * see
304 * fmt_single_name()
305 * fmt_multiple_name()
306 */
7c7b9cf5
KM
307 if (!cpu_args)
308 dai_link->cpu_dai_name = NULL;
179949bc 309
b3ca11ff
JS
310dai_link_of_err:
311 if (np)
312 of_node_put(np);
313 if (bitclkmaster)
314 of_node_put(bitclkmaster);
315 if (framemaster)
316 of_node_put(framemaster);
6a91a17b
JFM
317 return ret;
318}
319
fa558c28 320static int asoc_simple_card_parse_of(struct device_node *node,
b367a325 321 struct simple_card_data *priv,
2080437d 322 struct device *dev)
fa558c28 323{
b367a325 324 struct snd_soc_dai_link *dai_link = priv->snd_card.dai_link;
6a91a17b 325 struct simple_dai_props *dai_props = priv->dai_props;
c7099eb1 326 u32 val;
d4c22094 327 int ret;
fa558c28 328
2080437d
XL
329 if (!node)
330 return -EINVAL;
331
2772555b
XL
332 /* parsing the card name from DT */
333 snd_soc_of_parse_card_name(&priv->snd_card, "simple-audio-card,name");
334
9d681f5b
XL
335 /* off-codec widgets */
336 if (of_property_read_bool(node, "simple-audio-card,widgets")) {
337 ret = snd_soc_of_parse_audio_simple_widgets(&priv->snd_card,
338 "simple-audio-card,widgets");
339 if (ret)
340 return ret;
341 }
342
d4c22094 343 /* DAPM routes */
8c0b8230 344 if (of_property_read_bool(node, "simple-audio-card,routing")) {
b367a325 345 ret = snd_soc_of_parse_audio_routing(&priv->snd_card,
8c0b8230 346 "simple-audio-card,routing");
f87a3e82
XL
347 if (ret)
348 return ret;
349 }
d4c22094 350
2942a0e2 351 /* Factor to mclk, used in hw_params() */
c7099eb1
JS
352 ret = of_property_read_u32(node, "simple-audio-card,mclk-fs", &val);
353 if (ret == 0)
354 priv->mclk_fs = val;
2942a0e2 355
b3ca11ff
JS
356 dev_dbg(dev, "New simple-card: %s\n", priv->snd_card.name ?
357 priv->snd_card.name : "");
358
2080437d
XL
359 /* Single/Muti DAI link(s) & New style of DT node */
360 if (of_get_child_by_name(node, "simple-audio-card,dai-link")) {
b3ca11ff 361 struct device_node *np = NULL;
a44a750e
KM
362 int i = 0;
363
364 for_each_child_of_node(node, np) {
b3ca11ff 365 dev_dbg(dev, "\tlink %d:\n", i);
2d82eeb0
KM
366 ret = asoc_simple_card_dai_link_of(np, dev,
367 dai_link + i,
368 dai_props + i,
369 false);
b3ca11ff
JS
370 if (ret < 0) {
371 of_node_put(np);
372 return ret;
373 }
a44a750e 374 i++;
6a91a17b 375 }
b3ca11ff 376 } else {
2080437d 377 /* For single DAI link & old style of DT node */
2d82eeb0
KM
378 ret = asoc_simple_card_dai_link_of(node, dev,
379 dai_link, dai_props, true);
6a91a17b 380 if (ret < 0)
b3ca11ff 381 return ret;
6a91a17b 382 }
dd41e0c4 383
2772555b 384 if (!priv->snd_card.name)
b3ca11ff 385 priv->snd_card.name = priv->snd_card.dai_link->name;
f687d900 386
fa558c28
KM
387 return 0;
388}
389
e512e001
JFM
390/* update the reference count of the devices nodes at end of probe */
391static int asoc_simple_card_unref(struct platform_device *pdev)
392{
393 struct snd_soc_card *card = platform_get_drvdata(pdev);
394 struct snd_soc_dai_link *dai_link;
395 struct device_node *np;
396 int num_links;
397
398 for (num_links = 0, dai_link = card->dai_link;
399 num_links < card->num_links;
400 num_links++, dai_link++) {
401 np = (struct device_node *) dai_link->cpu_of_node;
402 if (np)
403 of_node_put(np);
404 np = (struct device_node *) dai_link->codec_of_node;
405 if (np)
406 of_node_put(np);
407 }
408 return 0;
409}
410
f2390880
KM
411static int asoc_simple_card_probe(struct platform_device *pdev)
412{
45fce594 413 struct simple_card_data *priv;
5ca8ba41 414 struct snd_soc_dai_link *dai_link;
fa558c28 415 struct device_node *np = pdev->dev.of_node;
f89983ef 416 struct device *dev = &pdev->dev;
2080437d 417 int num_links, ret;
6a91a17b
JFM
418
419 /* get the number of DAI links */
2080437d 420 if (np && of_get_child_by_name(np, "simple-audio-card,dai-link"))
6a91a17b 421 num_links = of_get_child_count(np);
2080437d 422 else
6a91a17b 423 num_links = 1;
f2390880 424
cf7dc23c
JFM
425 /* allocate the private data and the DAI link array */
426 priv = devm_kzalloc(dev,
6a91a17b 427 sizeof(*priv) + sizeof(*dai_link) * num_links,
cf7dc23c 428 GFP_KERNEL);
ca65b492 429 if (!priv)
ca919fe4
XL
430 return -ENOMEM;
431
201a0eac
JFM
432 /*
433 * init snd_soc_card
434 */
ca65b492
JFM
435 priv->snd_card.owner = THIS_MODULE;
436 priv->snd_card.dev = dev;
cf7dc23c 437 dai_link = priv->dai_link;
ca65b492 438 priv->snd_card.dai_link = dai_link;
6a91a17b 439 priv->snd_card.num_links = num_links;
201a0eac 440
cf7dc23c
JFM
441 /* get room for the other properties */
442 priv->dai_props = devm_kzalloc(dev,
6a91a17b 443 sizeof(*priv->dai_props) * num_links,
cf7dc23c
JFM
444 GFP_KERNEL);
445 if (!priv->dai_props)
446 return -ENOMEM;
447
fa558c28 448 if (np && of_device_is_available(np)) {
ca919fe4 449
2080437d 450 ret = asoc_simple_card_parse_of(np, priv, dev);
ca919fe4
XL
451 if (ret < 0) {
452 if (ret != -EPROBE_DEFER)
453 dev_err(dev, "parse error %d\n", ret);
e512e001 454 goto err;
fa558c28 455 }
6a91a17b 456
fa558c28 457 } else {
ca65b492
JFM
458 struct asoc_simple_card_info *cinfo;
459
460 cinfo = dev->platform_data;
461 if (!cinfo) {
34787d0a
XL
462 dev_err(dev, "no info for asoc-simple-card\n");
463 return -EINVAL;
464 }
fa558c28 465
781cbebe
NC
466 if (!cinfo->name ||
467 !cinfo->codec_dai.name ||
468 !cinfo->codec ||
469 !cinfo->platform ||
7722f830
JFM
470 !cinfo->cpu_dai.name) {
471 dev_err(dev, "insufficient asoc_simple_card_info settings\n");
472 return -EINVAL;
473 }
2bee9914 474
12ffa6fc 475 priv->snd_card.name = (cinfo->card) ? cinfo->card : cinfo->name;
5ca8ba41
JFM
476 dai_link->name = cinfo->name;
477 dai_link->stream_name = cinfo->name;
478 dai_link->platform_name = cinfo->platform;
479 dai_link->codec_name = cinfo->codec;
52008472
JFM
480 dai_link->cpu_dai_name = cinfo->cpu_dai.name;
481 dai_link->codec_dai_name = cinfo->codec_dai.name;
a5960bd5 482 dai_link->init = asoc_simple_card_dai_init;
cf7dc23c
JFM
483 memcpy(&priv->dai_props->cpu_dai, &cinfo->cpu_dai,
484 sizeof(priv->dai_props->cpu_dai));
485 memcpy(&priv->dai_props->codec_dai, &cinfo->codec_dai,
486 sizeof(priv->dai_props->codec_dai));
81985bd6 487
cf7dc23c
JFM
488 priv->dai_props->cpu_dai.fmt |= cinfo->daifmt;
489 priv->dai_props->codec_dai.fmt |= cinfo->daifmt;
f2390880
KM
490 }
491
ca65b492 492 snd_soc_card_set_drvdata(&priv->snd_card, priv);
f2390880 493
e512e001
JFM
494 ret = devm_snd_soc_register_card(&pdev->dev, &priv->snd_card);
495
496err:
497 asoc_simple_card_unref(pdev);
498 return ret;
f2390880
KM
499}
500
fa558c28
KM
501static const struct of_device_id asoc_simple_of_match[] = {
502 { .compatible = "simple-audio-card", },
503 {},
504};
505MODULE_DEVICE_TABLE(of, asoc_simple_of_match);
506
f2390880
KM
507static struct platform_driver asoc_simple_card = {
508 .driver = {
781cbebe 509 .name = "asoc-simple-card",
c445be35 510 .owner = THIS_MODULE,
fa558c28 511 .of_match_table = asoc_simple_of_match,
f2390880 512 },
781cbebe 513 .probe = asoc_simple_card_probe,
f2390880
KM
514};
515
516module_platform_driver(asoc_simple_card);
517
c445be35 518MODULE_ALIAS("platform:asoc-simple-card");
f2390880
KM
519MODULE_LICENSE("GPL");
520MODULE_DESCRIPTION("ASoC Simple Sound Card");
521MODULE_AUTHOR("Kuninori Morimoto <kuninori.morimoto.gx@renesas.com>");
This page took 0.397052 seconds and 5 git commands to generate.