]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - sound/soc/generic/simple-card.c
ASoC: core: Update snd_soc_of_parse_daifmt() interface
[mirror_ubuntu-bionic-kernel.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;
27 struct snd_soc_dai_link dai_link[]; /* dynamically allocated */
45fce594
JFM
28};
29
a4a2992c 30static int __asoc_simple_card_dai_init(struct snd_soc_dai *dai,
30d0341e 31 struct asoc_simple_dai *set)
a4a2992c 32{
4763ebe2 33 int ret;
a4a2992c 34
30d0341e
XL
35 if (set->fmt) {
36 ret = snd_soc_dai_set_fmt(dai, set->fmt);
4763ebe2
XL
37 if (ret && ret != -ENOTSUPP) {
38 dev_err(dai->dev, "simple-card: set_fmt error\n");
39 goto err;
40 }
e244bb9b
KM
41 }
42
4763ebe2 43 if (set->sysclk) {
a4a2992c 44 ret = snd_soc_dai_set_sysclk(dai, 0, set->sysclk, 0);
4763ebe2
XL
45 if (ret && ret != -ENOTSUPP) {
46 dev_err(dai->dev, "simple-card: set_sysclk error\n");
47 goto err;
48 }
49 }
50
6ff62eed
XL
51 if (set->slots) {
52 ret = snd_soc_dai_set_tdm_slot(dai, 0, 0,
53 set->slots,
54 set->slot_width);
55 if (ret && ret != -ENOTSUPP) {
56 dev_err(dai->dev, "simple-card: set_tdm_slot error\n");
57 goto err;
58 }
59 }
60
4763ebe2 61 ret = 0;
a4a2992c 62
4763ebe2 63err:
a4a2992c
KM
64 return ret;
65}
66
f2390880
KM
67static int asoc_simple_card_dai_init(struct snd_soc_pcm_runtime *rtd)
68{
b367a325 69 struct simple_card_data *priv =
ba194a4d 70 snd_soc_card_get_drvdata(rtd->card);
f2390880
KM
71 struct snd_soc_dai *codec = rtd->codec_dai;
72 struct snd_soc_dai *cpu = rtd->cpu_dai;
6a91a17b
JFM
73 struct simple_dai_props *dai_props;
74 int num, ret;
f2390880 75
6a91a17b
JFM
76 num = rtd - rtd->card->rtd;
77 dai_props = &priv->dai_props[num];
78 ret = __asoc_simple_card_dai_init(codec, &dai_props->codec_dai);
a4a2992c
KM
79 if (ret < 0)
80 return ret;
f2390880 81
6a91a17b 82 ret = __asoc_simple_card_dai_init(cpu, &dai_props->cpu_dai);
a4a2992c
KM
83 if (ret < 0)
84 return ret;
f2390880
KM
85
86 return 0;
87}
88
fa558c28
KM
89static int
90asoc_simple_card_sub_parse_of(struct device_node *np,
30d0341e 91 unsigned int daifmt,
fa558c28 92 struct asoc_simple_dai *dai,
52008472
JFM
93 const struct device_node **p_node,
94 const char **name)
fa558c28 95{
201a0eac 96 struct device_node *node;
fa558c28
KM
97 struct clk *clk;
98 int ret;
99
100 /*
101 * get node via "sound-dai = <&phandle port>"
102 * it will be used as xxx_of_node on soc_bind_dai_link()
103 */
201a0eac
JFM
104 node = of_parse_phandle(np, "sound-dai", 0);
105 if (!node)
fa558c28 106 return -ENODEV;
201a0eac 107 *p_node = node;
fa558c28
KM
108
109 /* get dai->name */
52008472 110 ret = snd_soc_of_get_dai_name(np, name);
fa558c28 111 if (ret < 0)
e512e001 112 return ret;
fa558c28 113
6ff62eed
XL
114 /* parse TDM slot */
115 ret = snd_soc_of_parse_tdm_slot(np, &dai->slots, &dai->slot_width);
116 if (ret)
e512e001 117 return ret;
6ff62eed 118
fa558c28
KM
119 /*
120 * bitclock-inversion, frame-inversion
121 * bitclock-master, frame-master
122 * and specific "format" if it has
123 */
389cb834 124 dai->fmt = snd_soc_of_parse_daifmt(np, NULL, NULL, NULL);
30d0341e 125 dai->fmt |= daifmt;
fa558c28
KM
126
127 /*
128 * dai->sysclk come from
129 * "clocks = <&xxx>" (if system has common clock)
130 * or "system-clock-frequency = <xxx>"
71467e46 131 * or device's module clock.
fa558c28 132 */
71467e46
XL
133 if (of_property_read_bool(np, "clocks")) {
134 clk = of_clk_get(np, 0);
135 if (IS_ERR(clk)) {
136 ret = PTR_ERR(clk);
e512e001 137 return ret;
71467e46
XL
138 }
139
140 dai->sysclk = clk_get_rate(clk);
141 } else if (of_property_read_bool(np, "system-clock-frequency")) {
fa558c28
KM
142 of_property_read_u32(np,
143 "system-clock-frequency",
144 &dai->sysclk);
71467e46 145 } else {
201a0eac 146 clk = of_clk_get(node, 0);
e2a19ac6
XL
147 if (!IS_ERR(clk))
148 dai->sysclk = clk_get_rate(clk);
71467e46 149 }
fa558c28 150
e512e001 151 return 0;
fa558c28
KM
152}
153
6a91a17b
JFM
154static int simple_card_cpu_codec_of(struct device_node *node,
155 int daifmt,
156 struct snd_soc_dai_link *dai_link,
157 struct simple_dai_props *dai_props)
158{
159 struct device_node *np;
160 int ret;
161
162 /* CPU sub-node */
163 ret = -EINVAL;
164 np = of_get_child_by_name(node, "simple-audio-card,cpu");
165 if (np) {
166 ret = asoc_simple_card_sub_parse_of(np, daifmt,
167 &dai_props->cpu_dai,
168 &dai_link->cpu_of_node,
169 &dai_link->cpu_dai_name);
170 of_node_put(np);
171 }
172 if (ret < 0)
173 return ret;
174
175 /* CODEC sub-node */
176 ret = -EINVAL;
177 np = of_get_child_by_name(node, "simple-audio-card,codec");
178 if (np) {
179 ret = asoc_simple_card_sub_parse_of(np, daifmt,
180 &dai_props->codec_dai,
181 &dai_link->codec_of_node,
182 &dai_link->codec_dai_name);
183 of_node_put(np);
184 }
185 return ret;
186}
187
fa558c28 188static int asoc_simple_card_parse_of(struct device_node *node,
b367a325 189 struct simple_card_data *priv,
6a91a17b
JFM
190 struct device *dev,
191 int multi)
fa558c28 192{
b367a325 193 struct snd_soc_dai_link *dai_link = priv->snd_card.dai_link;
6a91a17b 194 struct simple_dai_props *dai_props = priv->dai_props;
fa558c28
KM
195 struct device_node *np;
196 char *name;
c56c4d74 197 unsigned int daifmt;
d4c22094 198 int ret;
fa558c28 199
2772555b
XL
200 /* parsing the card name from DT */
201 snd_soc_of_parse_card_name(&priv->snd_card, "simple-audio-card,name");
202
fa558c28 203 /* get CPU/CODEC common format via simple-audio-card,format */
389cb834
JS
204 daifmt = snd_soc_of_parse_daifmt(node, "simple-audio-card,", NULL,
205 NULL) &
fa558c28
KM
206 (SND_SOC_DAIFMT_FORMAT_MASK | SND_SOC_DAIFMT_INV_MASK);
207
9d681f5b
XL
208 /* off-codec widgets */
209 if (of_property_read_bool(node, "simple-audio-card,widgets")) {
210 ret = snd_soc_of_parse_audio_simple_widgets(&priv->snd_card,
211 "simple-audio-card,widgets");
212 if (ret)
213 return ret;
214 }
215
d4c22094 216 /* DAPM routes */
8c0b8230 217 if (of_property_read_bool(node, "simple-audio-card,routing")) {
b367a325 218 ret = snd_soc_of_parse_audio_routing(&priv->snd_card,
8c0b8230 219 "simple-audio-card,routing");
f87a3e82
XL
220 if (ret)
221 return ret;
222 }
d4c22094 223
6a91a17b
JFM
224 /* loop on the DAI links */
225 np = NULL;
226 for (;;) {
227 if (multi) {
228 np = of_get_next_child(node, np);
229 if (!np)
230 break;
231 }
fa558c28 232
6a91a17b
JFM
233 ret = simple_card_cpu_codec_of(multi ? np : node,
234 daifmt, dai_link, dai_props);
235 if (ret < 0)
236 goto err;
fa558c28 237
6a91a17b
JFM
238 /*
239 * overwrite cpu_dai->fmt as its DAIFMT_MASTER bit is based on CODEC
240 * while the other bits should be identical unless buggy SW/HW design.
241 */
242 dai_props->cpu_dai.fmt = dai_props->codec_dai.fmt;
243
244 if (!dai_link->cpu_dai_name || !dai_link->codec_dai_name) {
245 ret = -EINVAL;
246 goto err;
247 }
248
249 /* simple-card assumes platform == cpu */
250 dai_link->platform_of_node = dai_link->cpu_of_node;
46c39cae 251
6a91a17b
JFM
252 name = devm_kzalloc(dev,
253 strlen(dai_link->cpu_dai_name) +
254 strlen(dai_link->codec_dai_name) + 2,
255 GFP_KERNEL);
256 sprintf(name, "%s-%s", dai_link->cpu_dai_name,
257 dai_link->codec_dai_name);
258 dai_link->name = dai_link->stream_name = name;
259
260 if (!multi)
261 break;
262
263 dai_link++;
264 dai_props++;
265 }
dd41e0c4 266
fa558c28 267 /* card name is created from CPU/CODEC dai name */
6a91a17b 268 dai_link = priv->snd_card.dai_link;
2772555b 269 if (!priv->snd_card.name)
6a91a17b 270 priv->snd_card.name = dai_link->name;
fa558c28 271
6a91a17b 272 dev_dbg(dev, "card-name : %s\n", priv->snd_card.name);
c56c4d74 273 dev_dbg(dev, "platform : %04x\n", daifmt);
6a91a17b 274 dai_props = priv->dai_props;
fa558c28 275 dev_dbg(dev, "cpu : %s / %04x / %d\n",
52008472 276 dai_link->cpu_dai_name,
6a91a17b
JFM
277 dai_props->cpu_dai.fmt,
278 dai_props->cpu_dai.sysclk);
fa558c28 279 dev_dbg(dev, "codec : %s / %04x / %d\n",
52008472 280 dai_link->codec_dai_name,
6a91a17b
JFM
281 dai_props->codec_dai.fmt,
282 dai_props->codec_dai.sysclk);
f687d900 283
fa558c28 284 return 0;
6a91a17b
JFM
285
286err:
287 of_node_put(np);
288 return ret;
fa558c28
KM
289}
290
e512e001
JFM
291/* update the reference count of the devices nodes at end of probe */
292static int asoc_simple_card_unref(struct platform_device *pdev)
293{
294 struct snd_soc_card *card = platform_get_drvdata(pdev);
295 struct snd_soc_dai_link *dai_link;
296 struct device_node *np;
297 int num_links;
298
299 for (num_links = 0, dai_link = card->dai_link;
300 num_links < card->num_links;
301 num_links++, dai_link++) {
302 np = (struct device_node *) dai_link->cpu_of_node;
303 if (np)
304 of_node_put(np);
305 np = (struct device_node *) dai_link->codec_of_node;
306 if (np)
307 of_node_put(np);
308 }
309 return 0;
310}
311
f2390880
KM
312static int asoc_simple_card_probe(struct platform_device *pdev)
313{
45fce594 314 struct simple_card_data *priv;
5ca8ba41 315 struct snd_soc_dai_link *dai_link;
fa558c28 316 struct device_node *np = pdev->dev.of_node;
f89983ef 317 struct device *dev = &pdev->dev;
6a91a17b
JFM
318 int num_links, multi, ret;
319
320 /* get the number of DAI links */
321 if (np && of_get_child_by_name(np, "simple-audio-card,dai-link")) {
322 num_links = of_get_child_count(np);
323 multi = 1;
324 } else {
325 num_links = 1;
326 multi = 0;
327 }
f2390880 328
cf7dc23c
JFM
329 /* allocate the private data and the DAI link array */
330 priv = devm_kzalloc(dev,
6a91a17b 331 sizeof(*priv) + sizeof(*dai_link) * num_links,
cf7dc23c 332 GFP_KERNEL);
ca65b492 333 if (!priv)
ca919fe4
XL
334 return -ENOMEM;
335
201a0eac
JFM
336 /*
337 * init snd_soc_card
338 */
ca65b492
JFM
339 priv->snd_card.owner = THIS_MODULE;
340 priv->snd_card.dev = dev;
cf7dc23c 341 dai_link = priv->dai_link;
ca65b492 342 priv->snd_card.dai_link = dai_link;
6a91a17b 343 priv->snd_card.num_links = num_links;
201a0eac 344
cf7dc23c
JFM
345 /* get room for the other properties */
346 priv->dai_props = devm_kzalloc(dev,
6a91a17b 347 sizeof(*priv->dai_props) * num_links,
cf7dc23c
JFM
348 GFP_KERNEL);
349 if (!priv->dai_props)
350 return -ENOMEM;
351
fa558c28 352 if (np && of_device_is_available(np)) {
ca919fe4 353
6a91a17b 354 ret = asoc_simple_card_parse_of(np, priv, dev, multi);
ca919fe4
XL
355 if (ret < 0) {
356 if (ret != -EPROBE_DEFER)
357 dev_err(dev, "parse error %d\n", ret);
e512e001 358 goto err;
fa558c28 359 }
6a91a17b
JFM
360
361 /*
362 * soc_bind_dai_link() will check cpu name
363 * after of_node matching if dai_link has cpu_dai_name.
364 * but, it will never match if name was created by fmt_single_name()
365 * remove cpu_dai_name to escape name matching.
366 * see
367 * fmt_single_name()
368 * fmt_multiple_name()
369 */
370 if (num_links == 1)
371 dai_link->cpu_dai_name = NULL;
372
fa558c28 373 } else {
ca65b492
JFM
374 struct asoc_simple_card_info *cinfo;
375
376 cinfo = dev->platform_data;
377 if (!cinfo) {
34787d0a
XL
378 dev_err(dev, "no info for asoc-simple-card\n");
379 return -EINVAL;
380 }
fa558c28 381
7722f830 382 if (!cinfo->name ||
7722f830
JFM
383 !cinfo->codec_dai.name ||
384 !cinfo->codec ||
385 !cinfo->platform ||
386 !cinfo->cpu_dai.name) {
387 dev_err(dev, "insufficient asoc_simple_card_info settings\n");
388 return -EINVAL;
389 }
2bee9914 390
12ffa6fc 391 priv->snd_card.name = (cinfo->card) ? cinfo->card : cinfo->name;
5ca8ba41
JFM
392 dai_link->name = cinfo->name;
393 dai_link->stream_name = cinfo->name;
394 dai_link->platform_name = cinfo->platform;
395 dai_link->codec_name = cinfo->codec;
52008472
JFM
396 dai_link->cpu_dai_name = cinfo->cpu_dai.name;
397 dai_link->codec_dai_name = cinfo->codec_dai.name;
cf7dc23c
JFM
398 memcpy(&priv->dai_props->cpu_dai, &cinfo->cpu_dai,
399 sizeof(priv->dai_props->cpu_dai));
400 memcpy(&priv->dai_props->codec_dai, &cinfo->codec_dai,
401 sizeof(priv->dai_props->codec_dai));
81985bd6 402
cf7dc23c
JFM
403 priv->dai_props->cpu_dai.fmt |= cinfo->daifmt;
404 priv->dai_props->codec_dai.fmt |= cinfo->daifmt;
f2390880
KM
405 }
406
407 /*
408 * init snd_soc_dai_link
409 */
5ca8ba41 410 dai_link->init = asoc_simple_card_dai_init;
f2390880 411
ca65b492 412 snd_soc_card_set_drvdata(&priv->snd_card, priv);
f2390880 413
e512e001
JFM
414 ret = devm_snd_soc_register_card(&pdev->dev, &priv->snd_card);
415
416err:
417 asoc_simple_card_unref(pdev);
418 return ret;
f2390880
KM
419}
420
fa558c28
KM
421static const struct of_device_id asoc_simple_of_match[] = {
422 { .compatible = "simple-audio-card", },
423 {},
424};
425MODULE_DEVICE_TABLE(of, asoc_simple_of_match);
426
f2390880
KM
427static struct platform_driver asoc_simple_card = {
428 .driver = {
429 .name = "asoc-simple-card",
c445be35 430 .owner = THIS_MODULE,
fa558c28 431 .of_match_table = asoc_simple_of_match,
f2390880
KM
432 },
433 .probe = asoc_simple_card_probe,
f2390880
KM
434};
435
436module_platform_driver(asoc_simple_card);
437
c445be35 438MODULE_ALIAS("platform:asoc-simple-card");
f2390880
KM
439MODULE_LICENSE("GPL");
440MODULE_DESCRIPTION("ASoC Simple Sound Card");
441MODULE_AUTHOR("Kuninori Morimoto <kuninori.morimoto.gx@renesas.com>");