Merge tag 'pm-6.16-rc5' of git://git.kernel.org/pub/scm/linux/kernel/git/rafael/linux-pm
[linux-block.git] / sound / soc / soc-core.c
CommitLineData
873486ed
KM
1// SPDX-License-Identifier: GPL-2.0+
2//
3// soc-core.c -- ALSA SoC Audio Layer
4//
5// Copyright 2005 Wolfson Microelectronics PLC.
6// Copyright 2005 Openedhand Ltd.
7// Copyright (C) 2010 Slimlogic Ltd.
8// Copyright (C) 2010 Texas Instruments Inc.
9//
10// Author: Liam Girdwood <lrg@slimlogic.co.uk>
11// with code, comments and ideas from :-
12// Richard Purdie <richard@openedhand.com>
13//
14// TODO:
15// o Add hw rules to enforce rates, etc.
16// o More testing with other codecs/machines.
17// o Add more codecs and platforms to ensure good API coverage.
18// o Support TDM on PCM and I2S
db2a4165
FM
19
20#include <linux/module.h>
21#include <linux/moduleparam.h>
22#include <linux/init.h>
23#include <linux/delay.h>
24#include <linux/pm.h>
25#include <linux/bitops.h>
12ef193d 26#include <linux/debugfs.h>
db2a4165 27#include <linux/platform_device.h>
741a509f 28#include <linux/pinctrl/consumer.h>
f0e8ed85 29#include <linux/ctype.h>
5a0e3ad6 30#include <linux/slab.h>
bec4fa05 31#include <linux/of.h>
a180e8b9 32#include <linux/of_graph.h>
345233d7 33#include <linux/dmi.h>
7de14d58 34#include <linux/acpi.h>
64899904 35#include <linux/string_choices.h>
db2a4165
FM
36#include <sound/core.h>
37#include <sound/pcm.h>
38#include <sound/pcm_params.h>
39#include <sound/soc.h>
01d7584c 40#include <sound/soc-dpcm.h>
8a978234 41#include <sound/soc-topology.h>
02e75636 42#include <sound/soc-link.h>
db2a4165
FM
43#include <sound/initval.h>
44
a8b1d34f
MB
45#define CREATE_TRACE_POINTS
46#include <trace/events/asoc.h>
47
c5af3a2e 48static DEFINE_MUTEX(client_mutex);
030e79f6 49static LIST_HEAD(component_list);
e894efef 50static LIST_HEAD(unbind_card_list);
c5af3a2e 51
368dee94
KM
52#define for_each_component(component) \
53 list_for_each_entry(component, &component_list, list)
54
587c9844
KM
55/*
56 * This is used if driver don't need to have CPU/Codec/Platform
57 * dai_link. see soc.h
58 */
59struct snd_soc_dai_link_component null_dailink_component[0];
60EXPORT_SYMBOL_GPL(null_dailink_component);
61
db2a4165
FM
62/*
63 * This is a timeout to do a DAPM powerdown after a stream is closed().
64 * It can be used to eliminate pops between different playback streams, e.g.
65 * between two audio tracks.
66 */
67static int pmdown_time = 5000;
68module_param(pmdown_time, int, 0);
69MODULE_PARM_DESC(pmdown_time, "DAPM stream powerdown time (msecs)");
70
dbe21408
MB
71static ssize_t pmdown_time_show(struct device *dev,
72 struct device_attribute *attr, char *buf)
73{
36ae1a96 74 struct snd_soc_pcm_runtime *rtd = dev_get_drvdata(dev);
dbe21408 75
628d0f72 76 return sysfs_emit(buf, "%ld\n", rtd->pmdown_time);
dbe21408
MB
77}
78
5a3f869c
Y
79static ssize_t pmdown_time_store(struct device *dev,
80 struct device_attribute *attr,
81 const char *buf, size_t count)
dbe21408 82{
36ae1a96 83 struct snd_soc_pcm_runtime *rtd = dev_get_drvdata(dev);
c593b520 84 int ret;
dbe21408 85
b785a492 86 ret = kstrtol(buf, 10, &rtd->pmdown_time);
c593b520
MB
87 if (ret)
88 return ret;
dbe21408
MB
89
90 return count;
91}
92
5a3f869c 93static DEVICE_ATTR_RW(pmdown_time);
dbe21408 94
d29697dc 95static struct attribute *soc_dev_attrs[] = {
d29697dc
TI
96 &dev_attr_pmdown_time.attr,
97 NULL
98};
99
100static umode_t soc_dev_attr_is_visible(struct kobject *kobj,
101 struct attribute *attr, int idx)
102{
103 struct device *dev = kobj_to_dev(kobj);
104 struct snd_soc_pcm_runtime *rtd = dev_get_drvdata(dev);
105
d918a376
KM
106 if (!rtd)
107 return 0;
108
d29697dc
TI
109 if (attr == &dev_attr_pmdown_time.attr)
110 return attr->mode; /* always visible */
3989ade2 111 return rtd->dai_link->num_codecs ? attr->mode : 0; /* enabled only with codec */
d29697dc
TI
112}
113
114static const struct attribute_group soc_dapm_dev_group = {
115 .attrs = soc_dapm_dev_attrs,
116 .is_visible = soc_dev_attr_is_visible,
117};
118
f7e73b26 119static const struct attribute_group soc_dev_group = {
d29697dc
TI
120 .attrs = soc_dev_attrs,
121 .is_visible = soc_dev_attr_is_visible,
122};
123
124static const struct attribute_group *soc_dev_attr_groups[] = {
125 &soc_dapm_dev_group,
f7e73b26 126 &soc_dev_group,
d29697dc
TI
127 NULL
128};
129
2624d5fa 130#ifdef CONFIG_DEBUG_FS
a4072cdf
KM
131struct dentry *snd_soc_debugfs_root;
132EXPORT_SYMBOL_GPL(snd_soc_debugfs_root);
133
81c7cfd1 134static void soc_init_component_debugfs(struct snd_soc_component *component)
e73f3de5 135{
6553bf06
LPC
136 if (!component->card->debugfs_card_root)
137 return;
138
81c7cfd1
LPC
139 if (component->debugfs_prefix) {
140 char *name;
e73f3de5 141
81c7cfd1
LPC
142 name = kasprintf(GFP_KERNEL, "%s:%s",
143 component->debugfs_prefix, component->name);
144 if (name) {
145 component->debugfs_root = debugfs_create_dir(name,
146 component->card->debugfs_card_root);
147 kfree(name);
148 }
149 } else {
150 component->debugfs_root = debugfs_create_dir(component->name,
151 component->card->debugfs_card_root);
152 }
e73f3de5 153
81c7cfd1
LPC
154 snd_soc_dapm_debugfs_init(snd_soc_component_get_dapm(component),
155 component->debugfs_root);
e73f3de5
RK
156}
157
81c7cfd1 158static void soc_cleanup_component_debugfs(struct snd_soc_component *component)
2624d5fa 159{
ad64bfbd
KM
160 if (!component->debugfs_root)
161 return;
81c7cfd1 162 debugfs_remove_recursive(component->debugfs_root);
ad64bfbd 163 component->debugfs_root = NULL;
81c7cfd1 164}
d6ce4cf3 165
c15b2a1d 166static int dai_list_show(struct seq_file *m, void *v)
f3208780 167{
1438c2f6 168 struct snd_soc_component *component;
f3208780
MB
169 struct snd_soc_dai *dai;
170
34e81ab4
LPC
171 mutex_lock(&client_mutex);
172
368dee94 173 for_each_component(component)
15a0c645 174 for_each_component_dais(component, dai)
700c17ca 175 seq_printf(m, "%s\n", dai->name);
f3208780 176
34e81ab4
LPC
177 mutex_unlock(&client_mutex);
178
700c17ca
DP
179 return 0;
180}
c15b2a1d 181DEFINE_SHOW_ATTRIBUTE(dai_list);
f3208780 182
db795f9b
KM
183static int component_list_show(struct seq_file *m, void *v)
184{
185 struct snd_soc_component *component;
186
187 mutex_lock(&client_mutex);
188
368dee94 189 for_each_component(component)
db795f9b
KM
190 seq_printf(m, "%s\n", component->name);
191
192 mutex_unlock(&client_mutex);
193
194 return 0;
195}
196DEFINE_SHOW_ATTRIBUTE(component_list);
197
a6052154
JN
198static void soc_init_card_debugfs(struct snd_soc_card *card)
199{
200 card->debugfs_card_root = debugfs_create_dir(card->name,
8a9dab1a 201 snd_soc_debugfs_root);
3a45b867 202
fee531d6
GKH
203 debugfs_create_u32("dapm_pop_time", 0644, card->debugfs_card_root,
204 &card->pop_time);
d8ca7a0a
KM
205
206 snd_soc_dapm_debugfs_init(&card->dapm, card->debugfs_card_root);
a6052154
JN
207}
208
209static void soc_cleanup_card_debugfs(struct snd_soc_card *card)
210{
211 debugfs_remove_recursive(card->debugfs_card_root);
29040d1a 212 card->debugfs_card_root = NULL;
a6052154
JN
213}
214
6553bf06
LPC
215static void snd_soc_debugfs_init(void)
216{
217 snd_soc_debugfs_root = debugfs_create_dir("asoc", NULL);
6553bf06 218
fee531d6
GKH
219 debugfs_create_file("dais", 0444, snd_soc_debugfs_root, NULL,
220 &dai_list_fops);
db795f9b 221
fee531d6
GKH
222 debugfs_create_file("components", 0444, snd_soc_debugfs_root, NULL,
223 &component_list_fops);
6553bf06
LPC
224}
225
226static void snd_soc_debugfs_exit(void)
227{
228 debugfs_remove_recursive(snd_soc_debugfs_root);
229}
230
2624d5fa
MB
231#else
232
41b1774f
KM
233static inline void soc_init_component_debugfs(struct snd_soc_component *component) { }
234static inline void soc_cleanup_component_debugfs(struct snd_soc_component *component) { }
235static inline void soc_init_card_debugfs(struct snd_soc_card *card) { }
236static inline void soc_cleanup_card_debugfs(struct snd_soc_card *card) { }
237static inline void snd_soc_debugfs_init(void) { }
238static inline void snd_soc_debugfs_exit(void) { }
6553bf06 239
2624d5fa
MB
240#endif
241
cf88ab48
KK
242static int snd_soc_is_match_dai_args(const struct of_phandle_args *args1,
243 const struct of_phandle_args *args2)
45655ec6
KM
244{
245 if (!args1 || !args2)
246 return 0;
247
248 if (args1->np != args2->np)
249 return 0;
250
251 for (int i = 0; i < args1->args_count; i++)
252 if (args1->args[i] != args2->args[i])
253 return 0;
254
255 return 1;
256}
257
0e66a2c6
KM
258static inline int snd_soc_dlc_component_is_empty(struct snd_soc_dai_link_component *dlc)
259{
45655ec6 260 return !(dlc->dai_args || dlc->name || dlc->of_node);
0e66a2c6
KM
261}
262
263static inline int snd_soc_dlc_component_is_invalid(struct snd_soc_dai_link_component *dlc)
264{
265 return (dlc->name && dlc->of_node);
266}
267
268static inline int snd_soc_dlc_dai_is_empty(struct snd_soc_dai_link_component *dlc)
269{
45655ec6 270 return !(dlc->dai_args || dlc->dai_name);
0e66a2c6
KM
271}
272
09f75f09
KM
273static int snd_soc_is_matching_dai(const struct snd_soc_dai_link_component *dlc,
274 struct snd_soc_dai *dai)
275{
276 if (!dlc)
277 return 0;
278
45655ec6
KM
279 if (dlc->dai_args)
280 return snd_soc_is_match_dai_args(dai->driver->dai_args, dlc->dai_args);
281
09f75f09
KM
282 if (!dlc->dai_name)
283 return 1;
284
285 /* see snd_soc_dai_name_get() */
286
09f75f09 287 if (dai->driver->name &&
b1724c00 288 strcmp(dlc->dai_name, dai->driver->name) == 0)
09f75f09
KM
289 return 1;
290
755bb9a4
CL
291 if (strcmp(dlc->dai_name, dai->name) == 0)
292 return 1;
293
09f75f09
KM
294 if (dai->component->name &&
295 strcmp(dlc->dai_name, dai->component->name) == 0)
296 return 1;
297
298 return 0;
299}
300
f3ac3da7 301const char *snd_soc_dai_name_get(const struct snd_soc_dai *dai)
09f75f09
KM
302{
303 /* see snd_soc_is_matching_dai() */
09f75f09
KM
304 if (dai->driver->name)
305 return dai->driver->name;
306
755bb9a4
CL
307 if (dai->name)
308 return dai->name;
309
09f75f09
KM
310 if (dai->component->name)
311 return dai->component->name;
312
313 return NULL;
314}
315EXPORT_SYMBOL_GPL(snd_soc_dai_name_get);
316
12b05232
KM
317static int snd_soc_rtd_add_component(struct snd_soc_pcm_runtime *rtd,
318 struct snd_soc_component *component)
a0ac4411 319{
2b544dd7 320 struct snd_soc_component *comp;
613fb500 321 int i;
a0ac4411 322
613fb500 323 for_each_rtd_components(rtd, i, comp) {
a0ac4411 324 /* already connected */
2b544dd7 325 if (comp == component)
a0ac4411
KM
326 return 0;
327 }
328
613fb500 329 /* see for_each_rtd_components */
aaa5e1aa
TI
330 rtd->num_components++; // increment flex array count at first
331 rtd->components[rtd->num_components - 1] = component;
a0ac4411
KM
332
333 return 0;
334}
335
a0ac4411
KM
336struct snd_soc_component *snd_soc_rtdcom_lookup(struct snd_soc_pcm_runtime *rtd,
337 const char *driver_name)
338{
2b544dd7 339 struct snd_soc_component *component;
613fb500 340 int i;
a0ac4411 341
971da24c
KM
342 if (!driver_name)
343 return NULL;
344
a33c0d16
KM
345 /*
346 * NOTE
347 *
348 * snd_soc_rtdcom_lookup() will find component from rtd by using
349 * specified driver name.
350 * But, if many components which have same driver name are connected
351 * to 1 rtd, this function will return 1st found component.
352 */
613fb500 353 for_each_rtd_components(rtd, i, component) {
2b544dd7 354 const char *component_name = component->driver->name;
971da24c
KM
355
356 if (!component_name)
357 continue;
358
359 if ((component_name == driver_name) ||
360 strcmp(component_name, driver_name) == 0)
613fb500 361 return component;
a0ac4411
KM
362 }
363
364 return NULL;
365}
031734b7 366EXPORT_SYMBOL_GPL(snd_soc_rtdcom_lookup);
a0ac4411 367
6fbea6b6 368struct snd_soc_component
18dd66ea 369*snd_soc_lookup_component_nolocked(struct device *dev, const char *driver_name)
b8132657
KM
370{
371 struct snd_soc_component *component;
5bd7e08b 372 struct snd_soc_component *found_component;
b8132657 373
5bd7e08b 374 found_component = NULL;
b8132657 375 for_each_component(component) {
5bd7e08b
KM
376 if ((dev == component->dev) &&
377 (!driver_name ||
378 (driver_name == component->driver->name) ||
379 (strcmp(component->driver->name, driver_name) == 0))) {
380 found_component = component;
381 break;
382 }
b8132657 383 }
b8132657 384
5bd7e08b 385 return found_component;
b8132657 386}
6fbea6b6 387EXPORT_SYMBOL_GPL(snd_soc_lookup_component_nolocked);
18dd66ea
KM
388
389struct snd_soc_component *snd_soc_lookup_component(struct device *dev,
390 const char *driver_name)
391{
392 struct snd_soc_component *component;
393
394 mutex_lock(&client_mutex);
395 component = snd_soc_lookup_component_nolocked(dev, driver_name);
396 mutex_unlock(&client_mutex);
397
398 return component;
399}
b8132657
KM
400EXPORT_SYMBOL_GPL(snd_soc_lookup_component);
401
94def8ea
KM
402struct snd_soc_pcm_runtime
403*snd_soc_get_pcm_runtime(struct snd_soc_card *card,
4468189f 404 struct snd_soc_dai_link *dai_link)
94def8ea
KM
405{
406 struct snd_soc_pcm_runtime *rtd;
407
408 for_each_card_rtds(card, rtd) {
4468189f 409 if (rtd->dai_link == dai_link)
94def8ea
KM
410 return rtd;
411 }
4468189f 412 dev_dbg(card->dev, "ASoC: failed to find rtd %s\n", dai_link->name);
94def8ea
KM
413 return NULL;
414}
415EXPORT_SYMBOL_GPL(snd_soc_get_pcm_runtime);
416
83f94a2e
KM
417/*
418 * Power down the audio subsystem pmdown_time msecs after close is called.
419 * This is to ensure there are no pops or clicks in between any music tracks
420 * due to DAPM power cycling.
421 */
422void snd_soc_close_delayed_work(struct snd_soc_pcm_runtime *rtd)
423{
eeec74aa 424 struct snd_soc_dai *codec_dai = snd_soc_rtd_to_codec(rtd, 0);
0f6011fd 425 int playback = SNDRV_PCM_STREAM_PLAYBACK;
83f94a2e 426
38e42f6d 427 snd_soc_dpcm_mutex_lock(rtd);
83f94a2e
KM
428
429 dev_dbg(rtd->dev,
430 "ASoC: pop wq checking: %s status: %s waiting: %s\n",
431 codec_dai->driver->playback.stream_name,
b3dea624
KM
432 snd_soc_dai_stream_active(codec_dai, playback) ?
433 "active" : "inactive",
64899904 434 str_yes_no(rtd->pop_wait));
83f94a2e
KM
435
436 /* are we waiting on this codec DAI stream */
437 if (rtd->pop_wait == 1) {
438 rtd->pop_wait = 0;
0f6011fd 439 snd_soc_dapm_stream_event(rtd, playback,
83f94a2e
KM
440 SND_SOC_DAPM_STREAM_STOP);
441 }
442
38e42f6d 443 snd_soc_dpcm_mutex_unlock(rtd);
83f94a2e
KM
444}
445EXPORT_SYMBOL_GPL(snd_soc_close_delayed_work);
446
6e864344
KM
447static void soc_release_rtd_dev(struct device *dev)
448{
449 /* "dev" means "rtd->dev" */
450 kfree(dev);
451}
452
1c93a9e0
KM
453static void soc_free_pcm_runtime(struct snd_soc_pcm_runtime *rtd)
454{
6e864344
KM
455 if (!rtd)
456 return;
457
753ace0a 458 list_del(&rtd->list);
b7c5bc45 459
9c9b6520
CM
460 if (delayed_work_pending(&rtd->delayed_work))
461 flush_delayed_work(&rtd->delayed_work);
0ced7b05
KM
462 snd_soc_pcm_component_free(rtd);
463
b7c5bc45
KM
464 /*
465 * we don't need to call kfree() for rtd->dev
466 * see
467 * soc_release_rtd_dev()
d918a376
KM
468 *
469 * We don't need rtd->dev NULL check, because
470 * it is alloced *before* rtd.
471 * see
472 * soc_new_pcm_runtime()
f1b3ee78
KM
473 *
474 * We don't need to mind freeing for rtd,
475 * because it was created from dev (= rtd->dev)
476 * see
477 * soc_new_pcm_runtime()
478 *
479 * rtd = devm_kzalloc(dev, ...);
480 * rtd->dev = dev
b7c5bc45 481 */
d918a376 482 device_unregister(rtd->dev);
1c93a9e0
KM
483}
484
4bf2e385
CM
485static void close_delayed_work(struct work_struct *work) {
486 struct snd_soc_pcm_runtime *rtd =
487 container_of(work, struct snd_soc_pcm_runtime,
488 delayed_work.work);
489
490 if (rtd->close_delayed_work_func)
491 rtd->close_delayed_work_func(rtd);
492}
493
1a497983
ML
494static struct snd_soc_pcm_runtime *soc_new_pcm_runtime(
495 struct snd_soc_card *card, struct snd_soc_dai_link *dai_link)
496{
497 struct snd_soc_pcm_runtime *rtd;
d918a376 498 struct device *dev;
6e864344 499 int ret;
d74c2a15 500 int stream;
1a497983 501
d918a376
KM
502 /*
503 * for rtd->dev
504 */
505 dev = kzalloc(sizeof(struct device), GFP_KERNEL);
506 if (!dev)
507 return NULL;
508
509 dev->parent = card->dev;
510 dev->release = soc_release_rtd_dev;
d918a376
KM
511
512 dev_set_name(dev, "%s", dai_link->name);
513
514 ret = device_register(dev);
515 if (ret < 0) {
516 put_device(dev); /* soc_release_rtd_dev */
517 return NULL;
518 }
519
6e864344
KM
520 /*
521 * for rtd
522 */
613fb500 523 rtd = devm_kzalloc(dev,
aaa5e1aa
TI
524 struct_size(rtd, components,
525 dai_link->num_cpus +
526 dai_link->num_codecs +
527 dai_link->num_platforms),
613fb500 528 GFP_KERNEL);
5fa7553d
KM
529 if (!rtd) {
530 device_unregister(dev);
531 return NULL;
532 }
1a497983 533
d918a376 534 rtd->dev = dev;
07d22a9b 535 INIT_LIST_HEAD(&rtd->list);
d74c2a15
KM
536 for_each_pcm_streams(stream) {
537 INIT_LIST_HEAD(&rtd->dpcm[stream].be_clients);
538 INIT_LIST_HEAD(&rtd->dpcm[stream].fe_clients);
539 }
d918a376 540 dev_set_drvdata(dev, rtd);
4bf2e385 541 INIT_DELAYED_WORK(&rtd->delayed_work, close_delayed_work);
d918a376 542
6e864344 543 /*
22a2fc81 544 * for rtd->dais
6e864344 545 */
22a2fc81 546 rtd->dais = devm_kcalloc(dev, dai_link->num_cpus + dai_link->num_codecs,
6396bb22 547 sizeof(struct snd_soc_dai *),
1a497983 548 GFP_KERNEL);
22a2fc81 549 if (!rtd->dais)
6e864344
KM
550 goto free_rtd;
551
76afa643 552 /*
22a2fc81
KM
553 * dais = [][][][][][][][][][][][][][][][][][]
554 * ^cpu_dais ^codec_dais
555 * |--- num_cpus ---|--- num_codecs --|
1729025b 556 * see
eeec74aa
KM
557 * snd_soc_rtd_to_cpu()
558 * snd_soc_rtd_to_codec()
76afa643 559 */
01faf501
KM
560 rtd->card = card;
561 rtd->dai_link = dai_link;
40e47e2d 562 rtd->id = card->num_rtd++;
c403dcd8 563 rtd->pmdown_time = pmdown_time; /* default power off timeout */
929deb84 564
6634e3d6 565 /* see for_each_card_rtds */
1a497983 566 list_add_tail(&rtd->list, &card->rtd_list);
a848125e 567
5c74c9d3
CK
568 ret = device_add_groups(dev, soc_dev_attr_groups);
569 if (ret < 0)
570 goto free_rtd;
571
a848125e 572 return rtd;
6e864344
KM
573
574free_rtd:
575 soc_free_pcm_runtime(rtd);
576 return NULL;
1a497983
ML
577}
578
13f58267
KM
579static void snd_soc_fill_dummy_dai(struct snd_soc_card *card)
580{
581 struct snd_soc_dai_link *dai_link;
582 int i;
583
584 /*
585 * COMP_DUMMY() creates size 0 array on dai_link.
586 * Fill it as dummy DAI in case of CPU/Codec here.
587 * Do nothing for Platform.
588 */
589 for_each_card_prelinks(card, i, dai_link) {
590 if (dai_link->num_cpus == 0 && dai_link->cpus) {
591 dai_link->num_cpus = 1;
592 dai_link->cpus = &snd_soc_dummy_dlc;
593 }
594 if (dai_link->num_codecs == 0 && dai_link->codecs) {
595 dai_link->num_codecs = 1;
596 dai_link->codecs = &snd_soc_dummy_dlc;
597 }
598 }
599}
600
65462e44
KM
601static void snd_soc_flush_all_delayed_work(struct snd_soc_card *card)
602{
603 struct snd_soc_pcm_runtime *rtd;
604
605 for_each_card_rtds(card, rtd)
606 flush_delayed_work(&rtd->delayed_work);
607}
608
6f8ab4ac 609#ifdef CONFIG_PM_SLEEP
d4c1d9eb
KM
610static void soc_playback_digital_mute(struct snd_soc_card *card, int mute)
611{
612 struct snd_soc_pcm_runtime *rtd;
613 struct snd_soc_dai *dai;
614 int playback = SNDRV_PCM_STREAM_PLAYBACK;
615 int i;
616
617 for_each_card_rtds(card, rtd) {
618
619 if (rtd->dai_link->ignore_suspend)
620 continue;
621
622 for_each_rtd_dais(rtd, i, dai) {
623 if (snd_soc_dai_stream_active(dai, playback))
624 snd_soc_dai_digital_mute(dai, mute, playback);
625 }
626 }
627}
628
baed393e
KM
629static void soc_dapm_suspend_resume(struct snd_soc_card *card, int event)
630{
631 struct snd_soc_pcm_runtime *rtd;
632 int stream;
633
634 for_each_card_rtds(card, rtd) {
635
636 if (rtd->dai_link->ignore_suspend)
637 continue;
638
639 for_each_pcm_streams(stream)
640 snd_soc_dapm_stream_event(rtd, stream, event);
641 }
642}
643
db2a4165 644/* powers down audio subsystem for suspend */
6f8ab4ac 645int snd_soc_suspend(struct device *dev)
db2a4165 646{
6f8ab4ac 647 struct snd_soc_card *card = dev_get_drvdata(dev);
d9fc4063 648 struct snd_soc_component *component;
1a497983
ML
649 struct snd_soc_pcm_runtime *rtd;
650 int i;
db2a4165 651
c5599b87 652 /* If the card is not initialized yet there is nothing to do */
9024bae4 653 if (!snd_soc_card_is_instantiated(card))
e3509ff0
DM
654 return 0;
655
2c7b696a
MZ
656 /*
657 * Due to the resume being scheduled into a workqueue we could
658 * suspend before that's finished - wait for it to complete.
6ed25978 659 */
b6cc78da 660 snd_power_wait(card->snd_card);
6ed25978
AG
661
662 /* we're going to block userspace touching us until resume completes */
f0fba2ad 663 snd_power_change_state(card->snd_card, SNDRV_CTL_POWER_D3hot);
6ed25978 664
a00f90f9 665 /* mute any active DACs */
d4c1d9eb 666 soc_playback_digital_mute(card, 1);
db2a4165 667
4ccab3e7 668 /* suspend all pcms */
bcb1fd1f 669 for_each_card_rtds(card, rtd) {
1a497983 670 if (rtd->dai_link->ignore_suspend)
3efab7dc
MB
671 continue;
672
1a497983 673 snd_pcm_suspend_all(rtd->pcm);
3efab7dc 674 }
4ccab3e7 675
130dc08c 676 snd_soc_card_suspend_pre(card);
db2a4165 677
37660b6d 678 /* close any waiting streams */
65462e44 679 snd_soc_flush_all_delayed_work(card);
db2a4165 680
baed393e 681 soc_dapm_suspend_resume(card, SND_SOC_DAPM_STREAM_SUSPEND);
db2a4165 682
8be4da29
LPC
683 /* Recheck all endpoints too, their state is affected by suspend */
684 dapm_mark_endpoints_dirty(card);
e2d32ff6
MB
685 snd_soc_dapm_sync(&card->dapm);
686
9178feb4 687 /* suspend all COMPONENTs */
1272063a
KM
688 for_each_card_rtds(card, rtd) {
689
690 if (rtd->dai_link->ignore_suspend)
691 continue;
692
693 for_each_rtd_components(rtd, i, component) {
694 struct snd_soc_dapm_context *dapm =
2c7b696a 695 snd_soc_component_get_dapm(component);
d9fc4063 696
1272063a
KM
697 /*
698 * ignore if component was already suspended
699 */
700 if (snd_soc_component_is_suspended(component))
701 continue;
702
703 /*
704 * If there are paths active then the COMPONENT will be
705 * held with bias _ON and should not be suspended.
706 */
4890140f 707 switch (snd_soc_dapm_get_bias_level(dapm)) {
f0fba2ad 708 case SND_SOC_BIAS_STANDBY:
125a25da 709 /*
9178feb4 710 * If the COMPONENT is capable of idle
125a25da
MB
711 * bias off then being in STANDBY
712 * means it's doing something,
713 * otherwise fall through.
714 */
4890140f 715 if (dapm->idle_bias_off) {
9178feb4 716 dev_dbg(component->dev,
10e8aa9a 717 "ASoC: idle_bias_off CODEC on over suspend\n");
125a25da
MB
718 break;
719 }
df561f66 720 fallthrough;
a8093297 721
f0fba2ad 722 case SND_SOC_BIAS_OFF:
66c51573 723 snd_soc_component_suspend(component);
9178feb4
KM
724 if (component->regmap)
725 regcache_mark_dirty(component->regmap);
988e8cc4 726 /* deactivate pins to sleep state */
9178feb4 727 pinctrl_pm_select_sleep_state(component->dev);
f0fba2ad
LG
728 break;
729 default:
9178feb4
KM
730 dev_dbg(component->dev,
731 "ASoC: COMPONENT is on over suspend\n");
f0fba2ad
LG
732 break;
733 }
1547aba9
MB
734 }
735 }
db2a4165 736
d17b60b2 737 snd_soc_card_suspend_post(card);
db2a4165
FM
738
739 return 0;
740}
6f8ab4ac 741EXPORT_SYMBOL_GPL(snd_soc_suspend);
db2a4165 742
2c7b696a
MZ
743/*
744 * deferred resume work, so resume can complete before we finished
6ed25978
AG
745 * setting our codec back up, which can be very slow on I2C
746 */
747static void soc_resume_deferred(struct work_struct *work)
db2a4165 748{
f0fba2ad 749 struct snd_soc_card *card =
2c7b696a
MZ
750 container_of(work, struct snd_soc_card,
751 deferred_resume_work);
d9fc4063 752 struct snd_soc_component *component;
db2a4165 753
2c7b696a
MZ
754 /*
755 * our power state is still SNDRV_CTL_POWER_D3hot from suspend time,
6ed25978
AG
756 * so userspace apps are blocked from touching us
757 */
758
f110bfc7 759 dev_dbg(card->dev, "ASoC: starting resume work\n");
6ed25978 760
9949788b 761 /* Bring us up into D2 so that DAPM starts enabling things */
f0fba2ad 762 snd_power_change_state(card->snd_card, SNDRV_CTL_POWER_D2);
9949788b 763
934c752c 764 snd_soc_card_resume_pre(card);
db2a4165 765
f70f18f7 766 for_each_card_components(card, component) {
e40fadbc 767 if (snd_soc_component_is_suspended(component))
9a840cba 768 snd_soc_component_resume(component);
1547aba9 769 }
db2a4165 770
baed393e 771 soc_dapm_suspend_resume(card, SND_SOC_DAPM_STREAM_RESUME);
db2a4165 772
3ff3f64b 773 /* unmute any active DACs */
d4c1d9eb 774 soc_playback_digital_mute(card, 0);
db2a4165 775
739443d1 776 snd_soc_card_resume_post(card);
db2a4165 777
f110bfc7 778 dev_dbg(card->dev, "ASoC: resume work completed\n");
6ed25978 779
8be4da29
LPC
780 /* Recheck all endpoints too, their state is affected by suspend */
781 dapm_mark_endpoints_dirty(card);
e2d32ff6 782 snd_soc_dapm_sync(&card->dapm);
1a7aaa58
JK
783
784 /* userspace can access us now we are back as we were before */
785 snd_power_change_state(card->snd_card, SNDRV_CTL_POWER_D0);
6ed25978
AG
786}
787
788/* powers up audio subsystem after a suspend */
6f8ab4ac 789int snd_soc_resume(struct device *dev)
6ed25978 790{
6f8ab4ac 791 struct snd_soc_card *card = dev_get_drvdata(dev);
76c39e86 792 struct snd_soc_component *component;
b9dd94a8 793
c5599b87 794 /* If the card is not initialized yet there is nothing to do */
9024bae4 795 if (!snd_soc_card_is_instantiated(card))
5ff1ddf2
EM
796 return 0;
797
988e8cc4 798 /* activate pins from sleep state */
76c39e86 799 for_each_card_components(card, component)
b3dea624 800 if (snd_soc_component_active(component))
76c39e86 801 pinctrl_pm_select_default_state(component->dev);
988e8cc4 802
250a15cf
KM
803 dev_dbg(dev, "ASoC: Scheduling resume work\n");
804 if (!schedule_work(&card->deferred_resume_work))
805 dev_err(dev, "ASoC: resume work item may be lost\n");
6ed25978 806
db2a4165
FM
807 return 0;
808}
6f8ab4ac 809EXPORT_SYMBOL_GPL(snd_soc_resume);
b3da4251
KM
810
811static void soc_resume_init(struct snd_soc_card *card)
812{
813 /* deferred resume work */
814 INIT_WORK(&card->deferred_resume_work, soc_resume_deferred);
815}
db2a4165 816#else
6f8ab4ac
MB
817#define snd_soc_suspend NULL
818#define snd_soc_resume NULL
41b1774f 819static inline void soc_resume_init(struct snd_soc_card *card) { }
db2a4165
FM
820#endif
821
c0834440
KM
822static struct device_node
823*soc_component_to_node(struct snd_soc_component *component)
12023a9a 824{
c0834440 825 struct device_node *of_node;
12023a9a 826
c0834440
KM
827 of_node = component->dev->of_node;
828 if (!of_node && component->dev->parent)
829 of_node = component->dev->parent->of_node;
12023a9a 830
c0834440 831 return of_node;
12023a9a
MLC
832}
833
cf88ab48
KK
834struct of_phandle_args *snd_soc_copy_dai_args(struct device *dev,
835 const struct of_phandle_args *args)
bbde4a30
KM
836{
837 struct of_phandle_args *ret = devm_kzalloc(dev, sizeof(*ret), GFP_KERNEL);
838
839 if (!ret)
840 return NULL;
841
842 *ret = *args;
843
844 return ret;
845}
846EXPORT_SYMBOL_GPL(snd_soc_copy_dai_args);
847
be6ac0a9
KM
848static int snd_soc_is_matching_component(
849 const struct snd_soc_dai_link_component *dlc,
850 struct snd_soc_component *component)
851{
852 struct device_node *component_of_node;
853
7d7db5d3
KM
854 if (!dlc)
855 return 0;
856
45655ec6
KM
857 if (dlc->dai_args) {
858 struct snd_soc_dai *dai;
859
860 for_each_component_dais(component, dai)
861 if (snd_soc_is_matching_dai(dlc, dai))
862 return 1;
863 return 0;
864 }
865
7d7db5d3 866 component_of_node = soc_component_to_node(component);
be6ac0a9
KM
867
868 if (dlc->of_node && component_of_node != dlc->of_node)
869 return 0;
870 if (dlc->name && strcmp(component->name, dlc->name))
871 return 0;
872
873 return 1;
874}
875
65d9361f 876static struct snd_soc_component *soc_find_component(
c1e230f0 877 const struct snd_soc_dai_link_component *dlc)
12023a9a 878{
65d9361f 879 struct snd_soc_component *component;
12023a9a 880
34e81ab4
LPC
881 lockdep_assert_held(&client_mutex);
882
e3303268
KM
883 /*
884 * NOTE
885 *
886 * It returns *1st* found component, but some driver
887 * has few components by same of_node/name
888 * ex)
889 * CPU component and generic DMAEngine component
890 */
c1e230f0
KM
891 for_each_component(component)
892 if (snd_soc_is_matching_component(dlc, component))
65d9361f 893 return component;
12023a9a
MLC
894
895 return NULL;
896}
897
fbb88b5c
ML
898/**
899 * snd_soc_find_dai - Find a registered DAI
900 *
4958471b 901 * @dlc: name of the DAI or the DAI driver and optional component info to match
fbb88b5c 902 *
ad61dd30 903 * This function will search all registered components and their DAIs to
fbb88b5c
ML
904 * find the DAI of the same name. The component's of_node and name
905 * should also match if being specified.
906 *
907 * Return: pointer of DAI, or NULL if not found.
908 */
305e9020 909struct snd_soc_dai *snd_soc_find_dai(
14621c7e 910 const struct snd_soc_dai_link_component *dlc)
12023a9a 911{
14621c7e
LPC
912 struct snd_soc_component *component;
913 struct snd_soc_dai *dai;
12023a9a 914
34e81ab4
LPC
915 lockdep_assert_held(&client_mutex);
916
2c7b696a 917 /* Find CPU DAI from registered DAIs */
09f75f09
KM
918 for_each_component(component)
919 if (snd_soc_is_matching_component(dlc, component))
920 for_each_component_dais(component, dai)
921 if (snd_soc_is_matching_dai(dlc, dai))
922 return dai;
12023a9a
MLC
923
924 return NULL;
925}
305e9020 926EXPORT_SYMBOL_GPL(snd_soc_find_dai);
12023a9a 927
c1c277b2
KM
928struct snd_soc_dai *snd_soc_find_dai_with_mutex(
929 const struct snd_soc_dai_link_component *dlc)
930{
931 struct snd_soc_dai *dai;
932
933 mutex_lock(&client_mutex);
934 dai = snd_soc_find_dai(dlc);
935 mutex_unlock(&client_mutex);
936
937 return dai;
938}
939EXPORT_SYMBOL_GPL(snd_soc_find_dai_with_mutex);
940
bfce78a5
KM
941static int soc_dai_link_sanity_check(struct snd_soc_card *card,
942 struct snd_soc_dai_link *link)
36794902
KM
943{
944 int i;
0e66a2c6 945 struct snd_soc_dai_link_component *dlc;
36794902 946
0e66a2c6
KM
947 /* Codec check */
948 for_each_link_codecs(link, i, dlc) {
36794902
KM
949 /*
950 * Codec must be specified by 1 of name or OF node,
951 * not both or neither.
952 */
0e66a2c6
KM
953 if (snd_soc_dlc_component_is_invalid(dlc))
954 goto component_invalid;
955
956 if (snd_soc_dlc_component_is_empty(dlc))
957 goto component_empty;
36794902
KM
958
959 /* Codec DAI name must be specified */
0e66a2c6
KM
960 if (snd_soc_dlc_dai_is_empty(dlc))
961 goto dai_empty;
36794902
KM
962
963 /*
964 * Defer card registration if codec component is not added to
965 * component list.
966 */
0e66a2c6 967 if (!soc_find_component(dlc))
199cd641 968 goto component_not_found;
36794902
KM
969 }
970
0e66a2c6
KM
971 /* Platform check */
972 for_each_link_platforms(link, i, dlc) {
36794902
KM
973 /*
974 * Platform may be specified by either name or OF node, but it
975 * can be left unspecified, then no components will be inserted
976 * in the rtdcom list
977 */
0e66a2c6
KM
978 if (snd_soc_dlc_component_is_invalid(dlc))
979 goto component_invalid;
980
981 if (snd_soc_dlc_component_is_empty(dlc))
982 goto component_empty;
36794902
KM
983
984 /*
985 * Defer card registration if platform component is not added to
986 * component list.
987 */
0e66a2c6 988 if (!soc_find_component(dlc))
199cd641 989 goto component_not_found;
36794902
KM
990 }
991
0e66a2c6
KM
992 /* CPU check */
993 for_each_link_cpus(link, i, dlc) {
76afa643
SN
994 /*
995 * CPU device may be specified by either name or OF node, but
996 * can be left unspecified, and will be matched based on DAI
997 * name alone..
998 */
0e66a2c6
KM
999 if (snd_soc_dlc_component_is_invalid(dlc))
1000 goto component_invalid;
36794902 1001
36794902 1002
0e66a2c6
KM
1003 if (snd_soc_dlc_component_is_empty(dlc)) {
1004 /*
1005 * At least one of CPU DAI name or CPU device name/node must be specified
1006 */
1007 if (snd_soc_dlc_dai_is_empty(dlc))
1008 goto component_dai_empty;
1009 } else {
1010 /*
1011 * Defer card registration if Component is not added
1012 */
1013 if (!soc_find_component(dlc))
199cd641 1014 goto component_not_found;
76afa643 1015 }
36794902
KM
1016 }
1017
1018 return 0;
0e66a2c6
KM
1019
1020component_invalid:
1021 dev_err(card->dev, "ASoC: Both Component name/of_node are set for %s\n", link->name);
1022 return -EINVAL;
1023
1024component_empty:
1025 dev_err(card->dev, "ASoC: Neither Component name/of_node are set for %s\n", link->name);
1026 return -EINVAL;
1027
199cd641
CC
1028component_not_found:
1029 dev_dbg(card->dev, "ASoC: Component %s not found for link %s\n", dlc->name, link->name);
0e66a2c6
KM
1030 return -EPROBE_DEFER;
1031
1032dai_empty:
1033 dev_err(card->dev, "ASoC: DAI name is not set for %s\n", link->name);
1034 return -EINVAL;
1035
1036component_dai_empty:
1037 dev_err(card->dev, "ASoC: Neither DAI/Component name/of_node are set for %s\n", link->name);
1038 return -EINVAL;
36794902
KM
1039}
1040
70b47699 1041#define MAX_DEFAULT_CH_MAP_SIZE 8
45cc50d1
KM
1042static struct snd_soc_dai_link_ch_map default_ch_map_sync[MAX_DEFAULT_CH_MAP_SIZE] = {
1043 { .cpu = 0, .codec = 0 },
1044 { .cpu = 1, .codec = 1 },
1045 { .cpu = 2, .codec = 2 },
1046 { .cpu = 3, .codec = 3 },
1047 { .cpu = 4, .codec = 4 },
1048 { .cpu = 5, .codec = 5 },
1049 { .cpu = 6, .codec = 6 },
70b47699 1050 { .cpu = 7, .codec = 7 },
45cc50d1
KM
1051};
1052static struct snd_soc_dai_link_ch_map default_ch_map_1cpu[MAX_DEFAULT_CH_MAP_SIZE] = {
1053 { .cpu = 0, .codec = 0 },
1054 { .cpu = 0, .codec = 1 },
1055 { .cpu = 0, .codec = 2 },
1056 { .cpu = 0, .codec = 3 },
1057 { .cpu = 0, .codec = 4 },
1058 { .cpu = 0, .codec = 5 },
1059 { .cpu = 0, .codec = 6 },
70b47699 1060 { .cpu = 0, .codec = 7 },
45cc50d1
KM
1061};
1062static struct snd_soc_dai_link_ch_map default_ch_map_1codec[MAX_DEFAULT_CH_MAP_SIZE] = {
1063 { .cpu = 0, .codec = 0 },
1064 { .cpu = 1, .codec = 0 },
1065 { .cpu = 2, .codec = 0 },
1066 { .cpu = 3, .codec = 0 },
1067 { .cpu = 4, .codec = 0 },
1068 { .cpu = 5, .codec = 0 },
1069 { .cpu = 6, .codec = 0 },
70b47699 1070 { .cpu = 7, .codec = 0 },
45cc50d1
KM
1071};
1072static int snd_soc_compensate_channel_connection_map(struct snd_soc_card *card,
1073 struct snd_soc_dai_link *dai_link)
1074{
1075 struct snd_soc_dai_link_ch_map *ch_maps;
1076 int i;
1077
1078 /*
1079 * dai_link->ch_maps indicates how CPU/Codec are connected.
1080 * It will be a map seen from a larger number of DAI.
1081 * see
1082 * soc.h :: [dai_link->ch_maps Image sample]
1083 */
1084
1085 /* it should have ch_maps if connection was N:M */
1086 if (dai_link->num_cpus > 1 && dai_link->num_codecs > 1 &&
1087 dai_link->num_cpus != dai_link->num_codecs && !dai_link->ch_maps) {
e9a92dfc 1088 dev_err(card->dev, "need to have ch_maps when N:M connection (%s)",
45cc50d1
KM
1089 dai_link->name);
1090 return -EINVAL;
1091 }
1092
1093 /* do nothing if it has own maps */
1094 if (dai_link->ch_maps)
1095 goto sanity_check;
1096
1097 /* check default map size */
1098 if (dai_link->num_cpus > MAX_DEFAULT_CH_MAP_SIZE ||
1099 dai_link->num_codecs > MAX_DEFAULT_CH_MAP_SIZE) {
1100 dev_err(card->dev, "soc-core.c needs update default_connection_maps");
1101 return -EINVAL;
1102 }
1103
1104 /* Compensate missing map for ... */
1105 if (dai_link->num_cpus == dai_link->num_codecs)
1106 dai_link->ch_maps = default_ch_map_sync; /* for 1:1 or N:N */
1107 else if (dai_link->num_cpus < dai_link->num_codecs)
1108 dai_link->ch_maps = default_ch_map_1cpu; /* for 1:N */
1109 else
1110 dai_link->ch_maps = default_ch_map_1codec; /* for N:1 */
1111
1112sanity_check:
1113 dev_dbg(card->dev, "dai_link %s\n", dai_link->stream_name);
1114 for_each_link_ch_maps(dai_link, i, ch_maps) {
1115 if ((ch_maps->cpu >= dai_link->num_cpus) ||
1116 (ch_maps->codec >= dai_link->num_codecs)) {
1117 dev_err(card->dev,
1118 "unexpected dai_link->ch_maps[%d] index (cpu(%d/%d) codec(%d/%d))",
1119 i,
1120 ch_maps->cpu, dai_link->num_cpus,
1121 ch_maps->codec, dai_link->num_codecs);
1122 return -EINVAL;
1123 }
1124
1125 dev_dbg(card->dev, " [%d] cpu%d <-> codec%d\n",
1126 i, ch_maps->cpu, ch_maps->codec);
1127 }
1128
1129 return 0;
1130}
1131
da704f26 1132/**
50cd9b53
KM
1133 * snd_soc_remove_pcm_runtime - Remove a pcm_runtime from card
1134 * @card: The ASoC card to which the pcm_runtime has
1135 * @rtd: The pcm_runtime to remove
da704f26 1136 *
50cd9b53 1137 * This function removes a pcm_runtime from the ASoC card.
da704f26 1138 */
50cd9b53
KM
1139void snd_soc_remove_pcm_runtime(struct snd_soc_card *card,
1140 struct snd_soc_pcm_runtime *rtd)
bc7a9091 1141{
da704f26
KM
1142 lockdep_assert_held(&client_mutex);
1143
1144 /*
1145 * Notify the machine driver for extra destruction
1146 */
fcbbcc32 1147 snd_soc_card_remove_dai_link(card, rtd->dai_link);
da704f26 1148
50cd9b53 1149 soc_free_pcm_runtime(rtd);
bc7a9091 1150}
50cd9b53 1151EXPORT_SYMBOL_GPL(snd_soc_remove_pcm_runtime);
bc7a9091 1152
63dc47da 1153/**
0c048004
KM
1154 * snd_soc_add_pcm_runtime - Add a pcm_runtime dynamically via dai_link
1155 * @card: The ASoC card to which the pcm_runtime is added
1156 * @dai_link: The DAI link to find pcm_runtime
63dc47da 1157 *
0c048004 1158 * This function adds a pcm_runtime ASoC card by using dai_link.
63dc47da 1159 *
0c048004 1160 * Note: Topology can use this API to add pcm_runtime when probing the
63dc47da
KM
1161 * topology component. And machine drivers can still define static
1162 * DAI links in dai_link array.
1163 */
ffaf886e
KM
1164static int snd_soc_add_pcm_runtime(struct snd_soc_card *card,
1165 struct snd_soc_dai_link *dai_link)
db2a4165 1166{
1a497983 1167 struct snd_soc_pcm_runtime *rtd;
76afa643 1168 struct snd_soc_dai_link_component *codec, *platform, *cpu;
90be711e 1169 struct snd_soc_component *component;
cb18cd26 1170 int i, id, ret;
435c5e25 1171
63dc47da
KM
1172 lockdep_assert_held(&client_mutex);
1173
1174 /*
1175 * Notify the machine driver for extra initialization
1176 */
cbc7a6b5
KM
1177 ret = snd_soc_card_add_dai_link(card, dai_link);
1178 if (ret < 0)
1179 return ret;
63dc47da 1180
a655de80
LG
1181 if (dai_link->ignore)
1182 return 0;
1183
6f2f1ff0 1184 dev_dbg(card->dev, "ASoC: binding %s\n", dai_link->name);
435c5e25 1185
bfce78a5
KM
1186 ret = soc_dai_link_sanity_check(card, dai_link);
1187 if (ret < 0)
1188 return ret;
1189
513cb311
SM
1190 rtd = soc_new_pcm_runtime(card, dai_link);
1191 if (!rtd)
1192 return -ENOMEM;
1193
76afa643 1194 for_each_link_cpus(dai_link, i, cpu) {
eeec74aa
KM
1195 snd_soc_rtd_to_cpu(rtd, i) = snd_soc_find_dai(cpu);
1196 if (!snd_soc_rtd_to_cpu(rtd, i)) {
76afa643
SN
1197 dev_info(card->dev, "ASoC: CPU DAI %s not registered\n",
1198 cpu->dai_name);
1199 goto _err_defer;
1200 }
eeec74aa 1201 snd_soc_rtd_add_component(rtd, snd_soc_rtd_to_cpu(rtd, i)->component);
f0fba2ad 1202 }
76afa643 1203
88bd870f 1204 /* Find CODEC from registered CODECs */
34614739 1205 for_each_link_codecs(dai_link, i, codec) {
eeec74aa
KM
1206 snd_soc_rtd_to_codec(rtd, i) = snd_soc_find_dai(codec);
1207 if (!snd_soc_rtd_to_codec(rtd, i)) {
7c7e2d6a 1208 dev_info(card->dev, "ASoC: CODEC DAI %s not registered\n",
34614739 1209 codec->dai_name);
1a497983 1210 goto _err_defer;
88bd870f 1211 }
34614739 1212
eeec74aa 1213 snd_soc_rtd_add_component(rtd, snd_soc_rtd_to_codec(rtd, i)->component);
12023a9a
MLC
1214 }
1215
e2b30edf 1216 /* Find PLATFORM from registered PLATFORMs */
34614739
JB
1217 for_each_link_platforms(dai_link, i, platform) {
1218 for_each_component(component) {
1219 if (!snd_soc_is_matching_component(platform, component))
1220 continue;
90be711e 1221
23fb6bc2
CL
1222 if (snd_soc_component_is_dummy(component) && component->num_dai)
1223 continue;
1224
12b05232 1225 snd_soc_rtd_add_component(rtd, component);
34614739 1226 }
90be711e
KM
1227 }
1228
cb18cd26
KM
1229 /*
1230 * Most drivers will register their PCMs using DAI link ordering but
1231 * topology based drivers can use the DAI link id field to set PCM
1232 * device number and then use rtd + a base offset of the BEs.
1233 *
1234 * FIXME
1235 *
1236 * This should be implemented by using "dai_link" feature instead of
1237 * "component" feature.
1238 */
1239 id = rtd->id;
1240 for_each_rtd_components(rtd, i, component) {
1241 if (!component->driver->use_dai_pcm_id)
1242 continue;
1243
1244 if (rtd->dai_link->no_pcm)
1245 id += component->driver->be_pcm_base;
1246 else
1247 id = rtd->dai_link->id;
1248 }
1249 rtd->id = id;
1250
b19e6e7b 1251 return 0;
1a497983
ML
1252
1253_err_defer:
50cd9b53 1254 snd_soc_remove_pcm_runtime(card, rtd);
2c7b696a 1255 return -EPROBE_DEFER;
f0fba2ad 1256}
ffaf886e
KM
1257
1258int snd_soc_add_pcm_runtimes(struct snd_soc_card *card,
1259 struct snd_soc_dai_link *dai_link,
1260 int num_dai_link)
1261{
1262 for (int i = 0; i < num_dai_link; i++) {
45cc50d1
KM
1263 int ret;
1264
1265 ret = snd_soc_compensate_channel_connection_map(card, dai_link + i);
1266 if (ret < 0)
1267 return ret;
ffaf886e 1268
45cc50d1 1269 ret = snd_soc_add_pcm_runtime(card, dai_link + i);
ffaf886e
KM
1270 if (ret < 0)
1271 return ret;
1272 }
1273
1274 return 0;
1275}
1276EXPORT_SYMBOL_GPL(snd_soc_add_pcm_runtimes);
f0fba2ad 1277
ba9e82a1
KM
1278static void snd_soc_runtime_get_dai_fmt(struct snd_soc_pcm_runtime *rtd)
1279{
1280 struct snd_soc_dai_link *dai_link = rtd->dai_link;
1281 struct snd_soc_dai *dai, *not_used;
ba9e82a1
KM
1282 u64 pos, possible_fmt;
1283 unsigned int mask = 0, dai_fmt = 0;
1284 int i, j, priority, pri, until;
1285
1286 /*
1287 * Get selectable format from each DAIs.
1288 *
1289 ****************************
1290 * NOTE
1291 * Using .auto_selectable_formats is not mandatory,
1292 * we can select format manually from Sound Card.
1293 * When use it, driver should list well tested format only.
1294 ****************************
1295 *
1296 * ex)
1297 * auto_selectable_formats (= SND_SOC_POSSIBLE_xxx)
1298 * (A) (B) (C)
1299 * DAI0_: { 0x000F, 0x00F0, 0x0F00 };
1300 * DAI1 : { 0xF000, 0x0F00 };
1301 * (X) (Y)
1302 *
1303 * "until" will be 3 in this case (MAX array size from DAI0 and DAI1)
1304 * Here is dev_dbg() message and comments
1305 *
1306 * priority = 1
1307 * DAI0: (pri, fmt) = (1, 000000000000000F) // 1st check (A) DAI1 is not selected
1308 * DAI1: (pri, fmt) = (0, 0000000000000000) // Necessary Waste
1309 * DAI0: (pri, fmt) = (1, 000000000000000F) // 2nd check (A)
1310 * DAI1: (pri, fmt) = (1, 000000000000F000) // (X)
1311 * priority = 2
1312 * DAI0: (pri, fmt) = (2, 00000000000000FF) // 3rd check (A) + (B)
1313 * DAI1: (pri, fmt) = (1, 000000000000F000) // (X)
1314 * DAI0: (pri, fmt) = (2, 00000000000000FF) // 4th check (A) + (B)
1315 * DAI1: (pri, fmt) = (2, 000000000000FF00) // (X) + (Y)
1316 * priority = 3
1317 * DAI0: (pri, fmt) = (3, 0000000000000FFF) // 5th check (A) + (B) + (C)
1318 * DAI1: (pri, fmt) = (2, 000000000000FF00) // (X) + (Y)
1319 * found auto selected format: 0000000000000F00
1320 */
1321 until = snd_soc_dai_get_fmt_max_priority(rtd);
1322 for (priority = 1; priority <= until; priority++) {
ba9e82a1
KM
1323 for_each_rtd_dais(rtd, j, not_used) {
1324
1325 possible_fmt = ULLONG_MAX;
1326 for_each_rtd_dais(rtd, i, dai) {
1327 u64 fmt = 0;
1328
1329 pri = (j >= i) ? priority : priority - 1;
1330 fmt = snd_soc_dai_get_fmt(dai, pri);
ba9e82a1
KM
1331 possible_fmt &= fmt;
1332 }
1333 if (possible_fmt)
1334 goto found;
1335 }
1336 }
1337 /* Not Found */
1338 return;
1339found:
ba9e82a1
KM
1340 /*
1341 * convert POSSIBLE_DAIFMT to DAIFMT
1342 *
1343 * Some basic/default settings on each is defined as 0.
1344 * see
1345 * SND_SOC_DAIFMT_NB_NF
1346 * SND_SOC_DAIFMT_GATED
1347 *
1348 * SND_SOC_DAIFMT_xxx_MASK can't notice it if Sound Card specify
1349 * these value, and will be overwrite to auto selected value.
1350 *
1351 * To avoid such issue, loop from 63 to 0 here.
1352 * Small number of SND_SOC_POSSIBLE_xxx will be Hi priority.
e9a92dfc 1353 * Basic/Default settings of each part and above are defined
ba9e82a1
KM
1354 * as Hi priority (= small number) of SND_SOC_POSSIBLE_xxx.
1355 */
1356 for (i = 63; i >= 0; i--) {
1357 pos = 1ULL << i;
1358 switch (possible_fmt & pos) {
1359 /*
1360 * for format
1361 */
1362 case SND_SOC_POSSIBLE_DAIFMT_I2S:
1363 case SND_SOC_POSSIBLE_DAIFMT_RIGHT_J:
1364 case SND_SOC_POSSIBLE_DAIFMT_LEFT_J:
1365 case SND_SOC_POSSIBLE_DAIFMT_DSP_A:
1366 case SND_SOC_POSSIBLE_DAIFMT_DSP_B:
1367 case SND_SOC_POSSIBLE_DAIFMT_AC97:
1368 case SND_SOC_POSSIBLE_DAIFMT_PDM:
1369 dai_fmt = (dai_fmt & ~SND_SOC_DAIFMT_FORMAT_MASK) | i;
1370 break;
1371 /*
1372 * for clock
1373 */
1374 case SND_SOC_POSSIBLE_DAIFMT_CONT:
1375 dai_fmt = (dai_fmt & ~SND_SOC_DAIFMT_CLOCK_MASK) | SND_SOC_DAIFMT_CONT;
1376 break;
1377 case SND_SOC_POSSIBLE_DAIFMT_GATED:
1378 dai_fmt = (dai_fmt & ~SND_SOC_DAIFMT_CLOCK_MASK) | SND_SOC_DAIFMT_GATED;
1379 break;
1380 /*
1381 * for clock invert
1382 */
1383 case SND_SOC_POSSIBLE_DAIFMT_NB_NF:
1384 dai_fmt = (dai_fmt & ~SND_SOC_DAIFMT_INV_MASK) | SND_SOC_DAIFMT_NB_NF;
1385 break;
1386 case SND_SOC_POSSIBLE_DAIFMT_NB_IF:
1387 dai_fmt = (dai_fmt & ~SND_SOC_DAIFMT_INV_MASK) | SND_SOC_DAIFMT_NB_IF;
1388 break;
1389 case SND_SOC_POSSIBLE_DAIFMT_IB_NF:
1390 dai_fmt = (dai_fmt & ~SND_SOC_DAIFMT_INV_MASK) | SND_SOC_DAIFMT_IB_NF;
1391 break;
1392 case SND_SOC_POSSIBLE_DAIFMT_IB_IF:
1393 dai_fmt = (dai_fmt & ~SND_SOC_DAIFMT_INV_MASK) | SND_SOC_DAIFMT_IB_IF;
1394 break;
1395 /*
1396 * for clock provider / consumer
1397 */
1398 case SND_SOC_POSSIBLE_DAIFMT_CBP_CFP:
1399 dai_fmt = (dai_fmt & ~SND_SOC_DAIFMT_CLOCK_PROVIDER_MASK) | SND_SOC_DAIFMT_CBP_CFP;
1400 break;
1401 case SND_SOC_POSSIBLE_DAIFMT_CBC_CFP:
1402 dai_fmt = (dai_fmt & ~SND_SOC_DAIFMT_CLOCK_PROVIDER_MASK) | SND_SOC_DAIFMT_CBC_CFP;
1403 break;
1404 case SND_SOC_POSSIBLE_DAIFMT_CBP_CFC:
1405 dai_fmt = (dai_fmt & ~SND_SOC_DAIFMT_CLOCK_PROVIDER_MASK) | SND_SOC_DAIFMT_CBP_CFC;
1406 break;
1407 case SND_SOC_POSSIBLE_DAIFMT_CBC_CFC:
1408 dai_fmt = (dai_fmt & ~SND_SOC_DAIFMT_CLOCK_PROVIDER_MASK) | SND_SOC_DAIFMT_CBC_CFC;
1409 break;
1410 }
1411 }
1412
1413 /*
1414 * Some driver might have very complex limitation.
1415 * In such case, user want to auto-select non-limitation part,
1416 * and want to manually specify complex part.
1417 *
1418 * Or for example, if both CPU and Codec can be clock provider,
1419 * but because of its quality, user want to specify it manually.
1420 *
1421 * Use manually specified settings if sound card did.
1422 */
1423 if (!(dai_link->dai_fmt & SND_SOC_DAIFMT_FORMAT_MASK))
1424 mask |= SND_SOC_DAIFMT_FORMAT_MASK;
1425 if (!(dai_link->dai_fmt & SND_SOC_DAIFMT_CLOCK_MASK))
1426 mask |= SND_SOC_DAIFMT_CLOCK_MASK;
1427 if (!(dai_link->dai_fmt & SND_SOC_DAIFMT_INV_MASK))
1428 mask |= SND_SOC_DAIFMT_INV_MASK;
640eac4c
KM
1429 if (!(dai_link->dai_fmt & SND_SOC_DAIFMT_CLOCK_PROVIDER_MASK))
1430 mask |= SND_SOC_DAIFMT_CLOCK_PROVIDER_MASK;
ba9e82a1
KM
1431
1432 dai_link->dai_fmt |= (dai_fmt & mask);
1433}
1434
4d1a98b5
KM
1435/**
1436 * snd_soc_runtime_set_dai_fmt() - Change DAI link format for a ASoC runtime
1437 * @rtd: The runtime for which the DAI link format should be changed
1438 * @dai_fmt: The new DAI link format
1439 *
1440 * This function updates the DAI link format for all DAIs connected to the DAI
1441 * link for the specified runtime.
1442 *
1443 * Note: For setups with a static format set the dai_fmt field in the
1444 * corresponding snd_dai_link struct instead of using this function.
1445 *
1446 * Returns 0 on success, otherwise a negative error code.
1447 */
1448int snd_soc_runtime_set_dai_fmt(struct snd_soc_pcm_runtime *rtd,
1449 unsigned int dai_fmt)
1450{
1451 struct snd_soc_dai *cpu_dai;
1452 struct snd_soc_dai *codec_dai;
24410f49 1453 unsigned int ext_fmt;
4d1a98b5
KM
1454 unsigned int i;
1455 int ret;
1456
7db07e37
KM
1457 if (!dai_fmt)
1458 return 0;
1459
24410f49
KM
1460 /*
1461 * dai_fmt has 4 types
1462 * 1. SND_SOC_DAIFMT_FORMAT_MASK
1463 * 2. SND_SOC_DAIFMT_CLOCK
1464 * 3. SND_SOC_DAIFMT_INV
1465 * 4. SND_SOC_DAIFMT_CLOCK_PROVIDER
1466 *
1467 * 4. CLOCK_PROVIDER is set from Codec perspective in dai_fmt. So it will be flipped
1468 * when this function calls set_fmt() for CPU (CBx_CFx -> Bx_Cx). see below.
1469 * This mean, we can't set CPU/Codec both are clock consumer for example.
1470 * New idea handles 4. in each dai->ext_fmt. It can keep compatibility.
1471 *
1472 * Legacy
1473 * dai_fmt includes 1, 2, 3, 4
1474 *
1475 * New idea
1476 * dai_fmt includes 1, 2, 3
1477 * ext_fmt includes 4
1478 */
4d1a98b5 1479 for_each_rtd_codec_dais(rtd, i, codec_dai) {
24410f49
KM
1480 ext_fmt = rtd->dai_link->codecs[i].ext_fmt;
1481 ret = snd_soc_dai_set_fmt(codec_dai, dai_fmt | ext_fmt);
4d1a98b5
KM
1482 if (ret != 0 && ret != -ENOTSUPP)
1483 return ret;
1484 }
1485
6c076273 1486 /* Flip the polarity for the "CPU" end of link */
24410f49 1487 /* Will effect only for 4. SND_SOC_DAIFMT_CLOCK_PROVIDER */
6c076273 1488 dai_fmt = snd_soc_daifmt_clock_provider_flipped(dai_fmt);
b44a67f8 1489
4d1a98b5 1490 for_each_rtd_cpu_dais(rtd, i, cpu_dai) {
24410f49
KM
1491 ext_fmt = rtd->dai_link->cpus[i].ext_fmt;
1492 ret = snd_soc_dai_set_fmt(cpu_dai, dai_fmt | ext_fmt);
4d1a98b5
KM
1493 if (ret != 0 && ret != -ENOTSUPP)
1494 return ret;
1495 }
1496
1497 return 0;
1498}
1499EXPORT_SYMBOL_GPL(snd_soc_runtime_set_dai_fmt);
1500
eaffeefb
KM
1501static int soc_init_pcm_runtime(struct snd_soc_card *card,
1502 struct snd_soc_pcm_runtime *rtd)
46496acb
KM
1503{
1504 struct snd_soc_dai_link *dai_link = rtd->dai_link;
eeec74aa 1505 struct snd_soc_dai *cpu_dai = snd_soc_rtd_to_cpu(rtd, 0);
8b12da9a 1506 int ret;
46496acb 1507
46496acb 1508 /* do machine specific initialization */
02e75636
KM
1509 ret = snd_soc_link_init(rtd);
1510 if (ret < 0)
1511 return ret;
46496acb 1512
ba9e82a1 1513 snd_soc_runtime_get_dai_fmt(rtd);
7db07e37
KM
1514 ret = snd_soc_runtime_set_dai_fmt(rtd, dai_link->dai_fmt);
1515 if (ret)
dd9f9cc1 1516 goto err;
46496acb
KM
1517
1518 /* add DPCM sysfs entries */
1519 soc_dpcm_debugfs_add(rtd);
1520
46496acb 1521 /* create compress_device if possible */
8b12da9a 1522 ret = snd_soc_dai_compress_new(cpu_dai, rtd);
62462e01 1523 if (ret != -ENOTSUPP)
dd9f9cc1 1524 goto err;
46496acb
KM
1525
1526 /* create the pcm */
8b12da9a 1527 ret = soc_new_pcm(rtd);
46496acb
KM
1528 if (ret < 0) {
1529 dev_err(card->dev, "ASoC: can't create pcm %s :%d\n",
1530 dai_link->stream_name, ret);
dd9f9cc1 1531 goto err;
46496acb 1532 }
d1eb6d11 1533
dd9f9cc1
AS
1534 ret = snd_soc_pcm_dai_new(rtd);
1535 if (ret < 0)
1536 goto err;
1537
1538 rtd->initialized = true;
1539
1540 return 0;
1541err:
1542 snd_soc_link_exit(rtd);
1543 return ret;
46496acb
KM
1544}
1545
ffd60fba
KM
1546static void soc_set_name_prefix(struct snd_soc_card *card,
1547 struct snd_soc_component *component)
1548{
aec3ff99 1549 struct device_node *of_node = soc_component_to_node(component);
29d9fc7a
KM
1550 const char *str;
1551 int ret, i;
ffd60fba 1552
4702f991 1553 for (i = 0; i < card->num_configs; i++) {
ffd60fba 1554 struct snd_soc_codec_conf *map = &card->codec_conf[i];
ffd60fba 1555
3256ef98
SP
1556 if (snd_soc_is_matching_component(&map->dlc, component) &&
1557 map->name_prefix) {
c13493a2
KM
1558 component->name_prefix = map->name_prefix;
1559 return;
1560 }
ffd60fba
KM
1561 }
1562
1563 /*
1564 * If there is no configuration table or no match in the table,
1565 * check if a prefix is provided in the node
1566 */
29d9fc7a
KM
1567 ret = of_property_read_string(of_node, "sound-name-prefix", &str);
1568 if (ret < 0)
1569 return;
1570
1571 component->name_prefix = str;
ffd60fba
KM
1572}
1573
c6619b72
KM
1574static void soc_remove_component(struct snd_soc_component *component,
1575 int probed)
22d14231 1576{
c6619b72
KM
1577
1578 if (!component->card)
1579 return;
1580
1581 if (probed)
1582 snd_soc_component_remove(component);
1583
7a5d9815 1584 list_del_init(&component->card_list);
22d14231
KM
1585 snd_soc_dapm_free(snd_soc_component_get_dapm(component));
1586 soc_cleanup_component_debugfs(component);
1587 component->card = NULL;
0e36f36b 1588 snd_soc_component_module_put_when_remove(component);
22d14231
KM
1589}
1590
ffd60fba
KM
1591static int soc_probe_component(struct snd_soc_card *card,
1592 struct snd_soc_component *component)
1593{
1594 struct snd_soc_dapm_context *dapm =
1595 snd_soc_component_get_dapm(component);
1596 struct snd_soc_dai *dai;
c6619b72 1597 int probed = 0;
ffd60fba
KM
1598 int ret;
1599
8f1a1681 1600 if (snd_soc_component_is_dummy(component))
ffd60fba
KM
1601 return 0;
1602
1603 if (component->card) {
1604 if (component->card != card) {
1605 dev_err(component->dev,
e80f238d
AS
1606 "Trying to bind component \"%s\" to card \"%s\" but is already bound to card \"%s\"\n",
1607 component->name, card->name, component->card->name);
ffd60fba
KM
1608 return -ENODEV;
1609 }
1610 return 0;
1611 }
1612
1613 ret = snd_soc_component_module_get_when_probe(component);
1614 if (ret < 0)
1615 return ret;
1616
1617 component->card = card;
ffd60fba
KM
1618 soc_set_name_prefix(card, component);
1619
1620 soc_init_component_debugfs(component);
1621
95c267dd 1622 snd_soc_dapm_init(dapm, card, component);
b614beaf 1623
ffd60fba
KM
1624 ret = snd_soc_dapm_new_controls(dapm,
1625 component->driver->dapm_widgets,
1626 component->driver->num_dapm_widgets);
1627
1628 if (ret != 0) {
1629 dev_err(component->dev,
1630 "Failed to create new controls %d\n", ret);
1631 goto err_probe;
1632 }
1633
1634 for_each_component_dais(component, dai) {
1635 ret = snd_soc_dapm_new_dai_widgets(dapm, dai);
1636 if (ret != 0) {
1637 dev_err(component->dev,
1638 "Failed to create DAI widgets %d\n", ret);
1639 goto err_probe;
1640 }
1641 }
1642
1643 ret = snd_soc_component_probe(component);
60adbd8f 1644 if (ret < 0)
ffd60fba 1645 goto err_probe;
60adbd8f 1646
ffd60fba
KM
1647 WARN(dapm->idle_bias_off &&
1648 dapm->bias_level != SND_SOC_BIAS_OFF,
1649 "codec %s can not start from non-off bias with idle_bias_off==1\n",
1650 component->name);
c6619b72 1651 probed = 1;
ffd60fba 1652
257c4dac
KM
1653 /*
1654 * machine specific init
1655 * see
1656 * snd_soc_component_set_aux()
1657 */
1658 ret = snd_soc_component_init(component);
1659 if (ret < 0)
1660 goto err_probe;
ffd60fba
KM
1661
1662 ret = snd_soc_add_component_controls(component,
1663 component->driver->controls,
1664 component->driver->num_controls);
1665 if (ret < 0)
1666 goto err_probe;
1667
1668 ret = snd_soc_dapm_add_routes(dapm,
1669 component->driver->dapm_routes,
1670 component->driver->num_dapm_routes);
dd239516
KM
1671 if (ret < 0)
1672 goto err_probe;
ffd60fba 1673
ffd60fba
KM
1674 /* see for_each_card_components */
1675 list_add(&component->card_list, &card->component_dev_list);
1676
1677err_probe:
1678 if (ret < 0)
c6619b72 1679 soc_remove_component(component, probed);
ffd60fba
KM
1680
1681 return ret;
1682}
1683
4ca47d21 1684static void soc_remove_link_dais(struct snd_soc_card *card)
b0aa88af 1685{
4ca47d21
KM
1686 struct snd_soc_pcm_runtime *rtd;
1687 int order;
1688
1689 for_each_comp_order(order) {
1690 for_each_card_rtds(card, rtd) {
7eaa313b
KM
1691 /* remove all rtd connected DAIs in good order */
1692 snd_soc_pcm_dai_remove(rtd, order);
4ca47d21
KM
1693 }
1694 }
f0fba2ad 1695}
db2a4165 1696
bc7c16c2
KM
1697static int soc_probe_link_dais(struct snd_soc_card *card)
1698{
bc7c16c2 1699 struct snd_soc_pcm_runtime *rtd;
51801aea 1700 int order, ret;
bc7c16c2
KM
1701
1702 for_each_comp_order(order) {
1703 for_each_card_rtds(card, rtd) {
51801aea
KM
1704 /* probe all rtd connected DAIs in good order */
1705 ret = snd_soc_pcm_dai_probe(rtd, order);
1706 if (ret)
1707 return ret;
bc7c16c2
KM
1708 }
1709 }
1710
1711 return 0;
1712}
1713
b006c0c6 1714static void soc_remove_link_components(struct snd_soc_card *card)
62ae68fa 1715{
61aca564 1716 struct snd_soc_component *component;
b006c0c6 1717 struct snd_soc_pcm_runtime *rtd;
613fb500 1718 int i, order;
62ae68fa 1719
b006c0c6
KM
1720 for_each_comp_order(order) {
1721 for_each_card_rtds(card, rtd) {
613fb500 1722 for_each_rtd_components(rtd, i, component) {
b006c0c6
KM
1723 if (component->driver->remove_order != order)
1724 continue;
62ae68fa 1725
c6619b72 1726 soc_remove_component(component, 1);
b006c0c6
KM
1727 }
1728 }
62ae68fa 1729 }
62ae68fa
SW
1730}
1731
62f07a6b 1732static int soc_probe_link_components(struct snd_soc_card *card)
6fb03550
KM
1733{
1734 struct snd_soc_component *component;
62f07a6b 1735 struct snd_soc_pcm_runtime *rtd;
613fb500 1736 int i, ret, order;
6fb03550 1737
62f07a6b
KM
1738 for_each_comp_order(order) {
1739 for_each_card_rtds(card, rtd) {
613fb500 1740 for_each_rtd_components(rtd, i, component) {
62f07a6b
KM
1741 if (component->driver->probe_order != order)
1742 continue;
1743
1744 ret = soc_probe_component(card, component);
1745 if (ret < 0)
1746 return ret;
1747 }
6fb03550
KM
1748 }
1749 }
1750
1751 return 0;
1752}
1753
e8fbd250 1754static void soc_unbind_aux_dev(struct snd_soc_card *card)
4893a2eb 1755{
e8fbd250
KM
1756 struct snd_soc_component *component, *_component;
1757
1758 for_each_card_auxs_safe(card, component, _component) {
257c4dac
KM
1759 /* for snd_soc_component_init() */
1760 snd_soc_component_set_aux(component, NULL);
e8fbd250
KM
1761 list_del(&component->card_aux_list);
1762 }
4893a2eb
KM
1763}
1764
bee886f1 1765static int soc_bind_aux_dev(struct snd_soc_card *card)
3ca041ed 1766{
f2ed6b07 1767 struct snd_soc_component *component;
bee886f1
KM
1768 struct snd_soc_aux_dev *aux;
1769 int i;
3dc29b8b 1770
bee886f1
KM
1771 for_each_card_pre_auxs(card, i, aux) {
1772 /* codecs, usually analog devices */
1773 component = soc_find_component(&aux->dlc);
1774 if (!component)
1775 return -EPROBE_DEFER;
1a653aa4 1776
257c4dac
KM
1777 /* for snd_soc_component_init() */
1778 snd_soc_component_set_aux(component, aux);
bee886f1
KM
1779 /* see for_each_card_auxs */
1780 list_add(&component->card_aux_list, &card->aux_comp_list);
1781 }
44c69bb1 1782 return 0;
b19e6e7b
MB
1783}
1784
f2ed6b07 1785static int soc_probe_aux_devices(struct snd_soc_card *card)
2eea392d 1786{
74bd3f92 1787 struct snd_soc_component *component;
f2ed6b07 1788 int order;
44c69bb1 1789 int ret;
3ca041ed 1790
1a1035a9 1791 for_each_comp_order(order) {
74bd3f92
KM
1792 for_each_card_auxs(card, component) {
1793 if (component->driver->probe_order != order)
1794 continue;
1795
1796 ret = soc_probe_component(card, component);
1797 if (ret < 0)
1798 return ret;
5f3484ac
LPC
1799 }
1800 }
2eea392d 1801
f2ed6b07 1802 return 0;
2eea392d
JN
1803}
1804
f2ed6b07 1805static void soc_remove_aux_devices(struct snd_soc_card *card)
2eea392d 1806{
f2ed6b07
ML
1807 struct snd_soc_component *comp, *_comp;
1808 int order;
2eea392d 1809
1a1035a9 1810 for_each_comp_order(order) {
c2b71c71 1811 for_each_card_auxs_safe(card, comp, _comp) {
e8fbd250 1812 if (comp->driver->remove_order == order)
c6619b72 1813 soc_remove_component(comp, 1);
f2ed6b07 1814 }
2eea392d 1815 }
2eea392d
JN
1816}
1817
1f5a4535 1818#ifdef CONFIG_DMI
8a6a6a38
KM
1819/*
1820 * If a DMI filed contain strings in this blacklist (e.g.
1821 * "Type2 - Board Manufacturer" or "Type1 - TBD by OEM"), it will be taken
1822 * as invalid and dropped when setting the card long name from DMI info.
1823 */
1824static const char * const dmi_blacklist[] = {
1825 "To be filled by OEM",
1826 "TBD by OEM",
1827 "Default String",
1828 "Board Manufacturer",
1829 "Board Vendor Name",
1830 "Board Product Name",
1831 NULL, /* terminator */
1832};
1833
2c7b696a
MZ
1834/*
1835 * Trim special characters, and replace '-' with '_' since '-' is used to
345233d7
LG
1836 * separate different DMI fields in the card long name. Only number and
1837 * alphabet characters and a few separator characters are kept.
1838 */
1839static void cleanup_dmi_name(char *name)
1840{
1841 int i, j = 0;
1842
1843 for (i = 0; name[i]; i++) {
1844 if (isalnum(name[i]) || (name[i] == '.')
1845 || (name[i] == '_'))
1846 name[j++] = name[i];
1847 else if (name[i] == '-')
1848 name[j++] = '_';
1849 }
1850
1851 name[j] = '\0';
1852}
1853
2c7b696a
MZ
1854/*
1855 * Check if a DMI field is valid, i.e. not containing any string
98faf436
ML
1856 * in the black list.
1857 */
1858static int is_dmi_valid(const char *field)
1859{
1860 int i = 0;
1861
1862 while (dmi_blacklist[i]) {
1863 if (strstr(field, dmi_blacklist[i]))
1864 return 0;
1865 i++;
46b5a4d2 1866 }
98faf436
ML
1867
1868 return 1;
1869}
1870
4e01e5db
JK
1871/*
1872 * Append a string to card->dmi_longname with character cleanups.
1873 */
1874static void append_dmi_string(struct snd_soc_card *card, const char *str)
1875{
1876 char *dst = card->dmi_longname;
1877 size_t dst_len = sizeof(card->dmi_longname);
1878 size_t len;
1879
1880 len = strlen(dst);
1881 snprintf(dst + len, dst_len - len, "-%s", str);
1882
1883 len++; /* skip the separator "-" */
1884 if (len < dst_len)
1885 cleanup_dmi_name(dst + len);
1886}
1887
345233d7
LG
1888/**
1889 * snd_soc_set_dmi_name() - Register DMI names to card
1890 * @card: The card to register DMI names
345233d7
LG
1891 *
1892 * An Intel machine driver may be used by many different devices but are
e9a92dfc 1893 * difficult for userspace to differentiate, since machine drivers usually
345233d7
LG
1894 * use their own name as the card short name and leave the card long name
1895 * blank. To differentiate such devices and fix bugs due to lack of
1896 * device-specific configurations, this function allows DMI info to be used
1897 * as the sound card long name, in the format of
1898 * "vendor-product-version-board"
1899 * (Character '-' is used to separate different DMI fields here).
1900 * This will help the user space to load the device-specific Use Case Manager
1901 * (UCM) configurations for the card.
1902 *
1903 * Possible card long names may be:
1904 * DellInc.-XPS139343-01-0310JH
1905 * ASUSTeKCOMPUTERINC.-T100TA-1.0-T100TA
1906 * Circuitco-MinnowboardMaxD0PLATFORM-D0-MinnowBoardMAX
1907 *
1908 * This function also supports flavoring the card longname to provide
1909 * the extra differentiation, like "vendor-product-version-board-flavor".
1910 *
1911 * We only keep number and alphabet characters and a few separator characters
1912 * in the card long name since UCM in the user space uses the card long names
1913 * as card configuration directory names and AudoConf cannot support special
e9a92dfc 1914 * characters like SPACE.
345233d7
LG
1915 *
1916 * Returns 0 on success, otherwise a negative error code.
1917 */
238c863e 1918static int snd_soc_set_dmi_name(struct snd_soc_card *card)
345233d7 1919{
cdb76568 1920 const char *vendor, *product, *board;
345233d7
LG
1921
1922 if (card->long_name)
1923 return 0; /* long name already set by driver or from DMI */
1924
c68fded7 1925 if (!dmi_available)
7de14d58
JH
1926 return 0;
1927
4e01e5db 1928 /* make up dmi long name as: vendor-product-version-board */
345233d7 1929 vendor = dmi_get_system_info(DMI_BOARD_VENDOR);
98faf436 1930 if (!vendor || !is_dmi_valid(vendor)) {
345233d7
LG
1931 dev_warn(card->dev, "ASoC: no DMI vendor name!\n");
1932 return 0;
1933 }
1934
4e01e5db 1935 snprintf(card->dmi_longname, sizeof(card->dmi_longname), "%s", vendor);
345233d7
LG
1936 cleanup_dmi_name(card->dmi_longname);
1937
1938 product = dmi_get_system_info(DMI_PRODUCT_NAME);
98faf436 1939 if (product && is_dmi_valid(product)) {
cdb76568
KM
1940 const char *product_version = dmi_get_system_info(DMI_PRODUCT_VERSION);
1941
4e01e5db 1942 append_dmi_string(card, product);
345233d7 1943
2c7b696a
MZ
1944 /*
1945 * some vendors like Lenovo may only put a self-explanatory
345233d7
LG
1946 * name in the product version field
1947 */
4e01e5db
JK
1948 if (product_version && is_dmi_valid(product_version))
1949 append_dmi_string(card, product_version);
345233d7
LG
1950 }
1951
1952 board = dmi_get_system_info(DMI_BOARD_NAME);
98faf436 1953 if (board && is_dmi_valid(board)) {
39870b0d
JK
1954 if (!product || strcasecmp(board, product))
1955 append_dmi_string(card, board);
345233d7
LG
1956 } else if (!product) {
1957 /* fall back to using legacy name */
1958 dev_warn(card->dev, "ASoC: no DMI board/product name!\n");
1959 return 0;
1960 }
1961
345233d7
LG
1962 /* set the card long name */
1963 card->long_name = card->dmi_longname;
1964
1965 return 0;
1966}
238c863e
KM
1967#else
1968static inline int snd_soc_set_dmi_name(struct snd_soc_card *card)
1969{
1970 return 0;
1971}
1f5a4535 1972#endif /* CONFIG_DMI */
345233d7 1973
a655de80
LG
1974static void soc_check_tplg_fes(struct snd_soc_card *card)
1975{
1976 struct snd_soc_component *component;
1977 const struct snd_soc_component_driver *comp_drv;
1978 struct snd_soc_dai_link *dai_link;
1979 int i;
1980
368dee94 1981 for_each_component(component) {
a655de80 1982
49f9c4f2 1983 /* does this component override BEs ? */
a655de80
LG
1984 if (!component->driver->ignore_machine)
1985 continue;
1986
1987 /* for this machine ? */
e194098b
PLB
1988 if (!strcmp(component->driver->ignore_machine,
1989 card->dev->driver->name))
1990 goto match;
a655de80 1991 if (strcmp(component->driver->ignore_machine,
e194098b 1992 dev_name(card->dev)))
a655de80 1993 continue;
e194098b 1994match:
a655de80 1995 /* machine matches, so override the rtd data */
7fe072b4 1996 for_each_card_prelinks(card, i, dai_link) {
a655de80
LG
1997
1998 /* ignore this FE */
1999 if (dai_link->dynamic) {
2000 dai_link->ignore = true;
2001 continue;
2002 }
2003
1328948f
PLB
2004 dev_dbg(card->dev, "info: override BE DAI link %s\n",
2005 card->dai_link[i].name);
a655de80
LG
2006
2007 /* override platform component */
adb76b5b 2008 if (!dai_link->platforms) {
daecf46e
KM
2009 dev_err(card->dev, "init platform error");
2010 continue;
2011 }
d9714003
DB
2012
2013 if (component->dev->of_node)
2014 dai_link->platforms->of_node = component->dev->of_node;
2015 else
2016 dai_link->platforms->name = component->name;
a655de80
LG
2017
2018 /* convert non BE into BE */
b6b8caf6 2019 dai_link->no_pcm = 1;
a655de80 2020
0cbbf8a0
KM
2021 /*
2022 * override any BE fixups
2023 * see
2024 * snd_soc_link_be_hw_params_fixup()
2025 */
a655de80
LG
2026 dai_link->be_hw_params_fixup =
2027 component->driver->be_hw_params_fixup;
2028
2c7b696a
MZ
2029 /*
2030 * most BE links don't set stream name, so set it to
a655de80
LG
2031 * dai link name if it's NULL to help bind widgets.
2032 */
2033 if (!dai_link->stream_name)
2034 dai_link->stream_name = dai_link->name;
2035 }
2036
2037 /* Inform userspace we are using alternate topology */
2038 if (component->driver->topology_name_prefix) {
2039
2c7b696a 2040 /* topology shortname created? */
a655de80
LG
2041 if (!card->topology_shortname_created) {
2042 comp_drv = component->driver;
2043
2044 snprintf(card->topology_shortname, 32, "%s-%s",
2045 comp_drv->topology_name_prefix,
2046 card->name);
2047 card->topology_shortname_created = true;
2048 }
2049
2050 /* use topology shortname */
2051 card->name = card->topology_shortname;
2052 }
2053 }
2054}
2055
c8d18e44
JK
2056#define soc_setup_card_name(card, name, name1, name2) \
2057 __soc_setup_card_name(card, name, sizeof(name), name1, name2)
2058static void __soc_setup_card_name(struct snd_soc_card *card,
2059 char *name, int len,
2060 const char *name1, const char *name2)
0f23f718 2061{
c8d18e44 2062 const char *src = name1 ? name1 : name2;
0f23f718
KM
2063 int i;
2064
c8d18e44 2065 snprintf(name, len, "%s", src);
0f23f718 2066
c8d18e44 2067 if (name != card->snd_card->driver)
0f23f718
KM
2068 return;
2069
2070 /*
c8d18e44 2071 * Name normalization (driver field)
0f23f718
KM
2072 *
2073 * The driver name is somewhat special, as it's used as a key for
2074 * searches in the user-space.
2075 *
2076 * ex)
2077 * "abcd??efg" -> "abcd__efg"
2078 */
2079 for (i = 0; i < len; i++) {
2080 switch (name[i]) {
2081 case '_':
2082 case '-':
2083 case '\0':
2084 break;
2085 default:
2086 if (!isalnum(name[i]))
2087 name[i] = '_';
2088 break;
2089 }
2090 }
c8d18e44
JK
2091
2092 /*
2093 * The driver field should contain a valid string from the user view.
2094 * The wrapping usually does not work so well here. Set a smaller string
2095 * in the specific ASoC driver.
2096 */
2097 if (strlen(src) > len - 1)
2098 dev_err(card->dev, "ASoC: driver name too long '%s' -> '%s'\n", src, name);
0f23f718
KM
2099}
2100
27f07cac 2101static void soc_cleanup_card_resources(struct snd_soc_card *card)
53e947a0 2102{
cc733900 2103 struct snd_soc_pcm_runtime *rtd, *n;
7ce6088f 2104
0ced7b05
KM
2105 if (card->snd_card)
2106 snd_card_disconnect_sync(card->snd_card);
53e947a0 2107
2a6f0892
KM
2108 snd_soc_dapm_shutdown(card);
2109
c7eb967d
CR
2110 /* release machine specific resources */
2111 for_each_card_rtds(card, rtd)
dd9f9cc1
AS
2112 if (rtd->initialized)
2113 snd_soc_link_exit(rtd);
53e947a0 2114 /* remove and free each DAI */
7ce6088f 2115 soc_remove_link_dais(card);
0ced7b05 2116 soc_remove_link_components(card);
7ce6088f 2117
cc733900 2118 for_each_card_rtds_safe(card, rtd, n)
50cd9b53 2119 snd_soc_remove_pcm_runtime(card, rtd);
7ce6088f 2120
53e947a0
KM
2121 /* remove auxiliary devices */
2122 soc_remove_aux_devices(card);
e8fbd250 2123 soc_unbind_aux_dev(card);
53e947a0
KM
2124
2125 snd_soc_dapm_free(&card->dapm);
2126 soc_cleanup_card_debugfs(card);
2127
2128 /* remove the card */
b0275d95 2129 snd_soc_card_remove(card);
0ced7b05
KM
2130
2131 if (card->snd_card) {
2132 snd_card_free(card->snd_card);
2133 card->snd_card = NULL;
2134 }
53e947a0
KM
2135}
2136
a3375522 2137static void snd_soc_unbind_card(struct snd_soc_card *card)
2cc1afcf 2138{
9024bae4 2139 if (snd_soc_card_is_instantiated(card)) {
2cc1afcf
KM
2140 card->instantiated = false;
2141 snd_soc_flush_all_delayed_work(card);
2142
27f07cac 2143 soc_cleanup_card_resources(card);
2cc1afcf
KM
2144 }
2145}
2146
ed90c013 2147static int snd_soc_bind_card(struct snd_soc_card *card)
f0fba2ad 2148{
1a497983 2149 struct snd_soc_pcm_runtime *rtd;
76c39e86 2150 struct snd_soc_component *component;
ffaf886e 2151 int ret;
fe3e78e0 2152
0f3b8184 2153 snd_soc_card_mutex_lock_root(card);
13f58267
KM
2154 snd_soc_fill_dummy_dai(card);
2155
95c267dd 2156 snd_soc_dapm_init(&card->dapm, card, NULL);
53e947a0 2157
a655de80
LG
2158 /* check whether any platform is ignore machine FE and using topology */
2159 soc_check_tplg_fes(card);
2160
44c69bb1 2161 /* bind aux_devs too */
bee886f1
KM
2162 ret = soc_bind_aux_dev(card);
2163 if (ret < 0)
2164 goto probe_end;
435c5e25 2165
f8f80361 2166 /* add predefined DAI links to the list */
d8145989 2167 card->num_rtd = 0;
ffaf886e
KM
2168 ret = snd_soc_add_pcm_runtimes(card, card->dai_link, card->num_links);
2169 if (ret < 0)
2170 goto probe_end;
f8f80361 2171
f0fba2ad 2172 /* card bind complete so register a sound card */
102b5a8d 2173 ret = snd_card_new(card->dev, SNDRV_DEFAULT_IDX1, SNDRV_DEFAULT_STR1,
f0fba2ad
LG
2174 card->owner, 0, &card->snd_card);
2175 if (ret < 0) {
10e8aa9a
MM
2176 dev_err(card->dev,
2177 "ASoC: can't create sound card for card %s: %d\n",
2178 card->name, ret);
53e947a0 2179 goto probe_end;
f0fba2ad 2180 }
f0fba2ad 2181
0757d834
LPC
2182 soc_init_card_debugfs(card);
2183
b3da4251 2184 soc_resume_init(card);
db2a4165 2185
b8ba3b57
KM
2186 ret = snd_soc_dapm_new_controls(&card->dapm, card->dapm_widgets,
2187 card->num_dapm_widgets);
2188 if (ret < 0)
2189 goto probe_end;
9a841ebb 2190
b8ba3b57
KM
2191 ret = snd_soc_dapm_new_controls(&card->dapm, card->of_dapm_widgets,
2192 card->num_of_dapm_widgets);
2193 if (ret < 0)
2194 goto probe_end;
f23e860e 2195
f0fba2ad 2196 /* initialise the sound card only once */
73de4b02
KM
2197 ret = snd_soc_card_probe(card);
2198 if (ret < 0)
2199 goto probe_end;
fe3e78e0 2200
62ae68fa 2201 /* probe all components used by DAI links on this card */
62f07a6b
KM
2202 ret = soc_probe_link_components(card);
2203 if (ret < 0) {
f09b6e96
JH
2204 if (ret != -EPROBE_DEFER) {
2205 dev_err(card->dev,
2206 "ASoC: failed to instantiate card %d\n", ret);
2207 }
62f07a6b 2208 goto probe_end;
62ae68fa
SW
2209 }
2210
f2ed6b07
ML
2211 /* probe auxiliary components */
2212 ret = soc_probe_aux_devices(card);
74bd3f92
KM
2213 if (ret < 0) {
2214 dev_err(card->dev,
2215 "ASoC: failed to probe aux component %d\n", ret);
53e947a0 2216 goto probe_end;
74bd3f92 2217 }
f2ed6b07 2218
62ae68fa 2219 /* probe all DAI links on this card */
c7e73774
KM
2220 ret = soc_probe_link_dais(card);
2221 if (ret < 0) {
2222 dev_err(card->dev,
2223 "ASoC: failed to instantiate card %d\n", ret);
2224 goto probe_end;
f0fba2ad 2225 }
db2a4165 2226
626c2e57 2227 for_each_card_rtds(card, rtd) {
eaffeefb 2228 ret = soc_init_pcm_runtime(card, rtd);
626c2e57
KM
2229 if (ret < 0)
2230 goto probe_end;
2231 }
c4b46982 2232
888df395 2233 snd_soc_dapm_link_dai_widgets(card);
b893ea5f 2234 snd_soc_dapm_connect_dai_link_widgets(card);
888df395 2235
9b98c7c2
KM
2236 ret = snd_soc_add_card_controls(card, card->controls,
2237 card->num_controls);
2238 if (ret < 0)
2239 goto probe_end;
b7af1daf 2240
daa480bd
KM
2241 ret = snd_soc_dapm_add_routes(&card->dapm, card->dapm_routes,
2242 card->num_dapm_routes);
dd239516
KM
2243 if (ret < 0)
2244 goto probe_end;
b8ad29de 2245
daa480bd
KM
2246 ret = snd_soc_dapm_add_routes(&card->dapm, card->of_dapm_routes,
2247 card->num_of_dapm_routes);
2248 if (ret < 0)
2249 goto probe_end;
75d9ac46 2250
861886d3 2251 /* try to set some sane longname if DMI is available */
238c863e 2252 snd_soc_set_dmi_name(card);
861886d3 2253
c8d18e44
JK
2254 soc_setup_card_name(card, card->snd_card->shortname,
2255 card->name, NULL);
2256 soc_setup_card_name(card, card->snd_card->longname,
2257 card->long_name, card->name);
2258 soc_setup_card_name(card, card->snd_card->driver,
2259 card->driver_name, card->name);
f0fba2ad 2260
dc73d73a
JK
2261 if (card->components) {
2262 /* the current implementation of snd_component_add() accepts */
2263 /* multiple components in the string separated by space, */
2264 /* but the string collision (identical string) check might */
2265 /* not work correctly */
2266 ret = snd_component_add(card->snd_card, card->components);
2267 if (ret < 0) {
2268 dev_err(card->dev, "ASoC: %s snd_component_add() failed: %d\n",
2269 card->name, ret);
2270 goto probe_end;
2271 }
2272 }
2273
5c0eac03
KM
2274 ret = snd_soc_card_late_probe(card);
2275 if (ret < 0)
2276 goto probe_end;
28e9ad92 2277
824ef826 2278 snd_soc_dapm_new_widgets(card);
df4d27b1 2279 snd_soc_card_fixup_controls(card);
8c193b8d 2280
f0fba2ad
LG
2281 ret = snd_card_register(card->snd_card);
2282 if (ret < 0) {
f110bfc7
LG
2283 dev_err(card->dev, "ASoC: failed to register soundcard %d\n",
2284 ret);
53e947a0 2285 goto probe_end;
db2a4165
FM
2286 }
2287
f0fba2ad 2288 card->instantiated = 1;
882eab6c 2289 dapm_mark_endpoints_dirty(card);
4f4c0072 2290 snd_soc_dapm_sync(&card->dapm);
f0fba2ad 2291
ed90c013 2292 /* deactivate pins to sleep state */
76c39e86 2293 for_each_card_components(card, component)
b3dea624 2294 if (!snd_soc_component_active(component))
76c39e86 2295 pinctrl_pm_select_sleep_state(component->dev);
ed90c013 2296
53e947a0
KM
2297probe_end:
2298 if (ret < 0)
27f07cac 2299 soc_cleanup_card_resources(card);
0f3b8184 2300 snd_soc_card_mutex_unlock(card);
a3375522
CR
2301
2302 return ret;
2303}
2304
2305static void devm_card_bind_release(struct device *dev, void *res)
2306{
2307 snd_soc_unregister_card(*(struct snd_soc_card **)res);
2308}
2309
2310static int devm_snd_soc_bind_card(struct device *dev, struct snd_soc_card *card)
2311{
2312 struct snd_soc_card **ptr;
2313 int ret;
2314
2315 ptr = devres_alloc(devm_card_bind_release, sizeof(*ptr), GFP_KERNEL);
2316 if (!ptr)
2317 return -ENOMEM;
2318
2319 ret = snd_soc_bind_card(card);
2320 if (ret == 0 || ret == -EPROBE_DEFER) {
2321 *ptr = card;
2322 devres_add(dev, ptr);
2323 } else {
2324 devres_free(ptr);
2325 }
2326
2327 return ret;
2328}
2329
2330static int snd_soc_rebind_card(struct snd_soc_card *card)
2331{
2332 int ret;
2333
2334 if (card->devres_dev) {
2335 devres_destroy(card->devres_dev, devm_card_bind_release, NULL, NULL);
2336 ret = devm_snd_soc_bind_card(card->devres_dev, card);
2337 } else {
2338 ret = snd_soc_bind_card(card);
2339 }
2340
2341 if (ret != -EPROBE_DEFER)
2342 list_del_init(&card->list);
db2a4165 2343
b19e6e7b 2344 return ret;
435c5e25
MB
2345}
2346
2347/* probes a new socdev */
2348static int soc_probe(struct platform_device *pdev)
2349{
f0fba2ad 2350 struct snd_soc_card *card = platform_get_drvdata(pdev);
435c5e25 2351
70a7ca34
VK
2352 /*
2353 * no card, so machine driver should be registering card
2354 * we should not be here in that case so ret error
2355 */
2356 if (!card)
2357 return -EINVAL;
2358
fe4085e8 2359 dev_warn(&pdev->dev,
f110bfc7 2360 "ASoC: machine %s should use snd_soc_register_card()\n",
fe4085e8
MB
2361 card->name);
2362
435c5e25
MB
2363 /* Bodge while we unpick instantiation */
2364 card->dev = &pdev->dev;
f0fba2ad 2365
ad61b78e 2366 return devm_snd_soc_register_card(&pdev->dev, card);
db2a4165
FM
2367}
2368
6f8ab4ac 2369int snd_soc_poweroff(struct device *dev)
51737470 2370{
6f8ab4ac 2371 struct snd_soc_card *card = dev_get_drvdata(dev);
76c39e86 2372 struct snd_soc_component *component;
51737470 2373
9024bae4 2374 if (!snd_soc_card_is_instantiated(card))
416356fc 2375 return 0;
51737470 2376
2c7b696a
MZ
2377 /*
2378 * Flush out pmdown_time work - we actually do want to run it
2379 * now, we're shutting down so no imminent restart.
2380 */
65462e44 2381 snd_soc_flush_all_delayed_work(card);
51737470 2382
f0fba2ad 2383 snd_soc_dapm_shutdown(card);
416356fc 2384
988e8cc4 2385 /* deactivate pins to sleep state */
76c39e86
KM
2386 for_each_card_components(card, component)
2387 pinctrl_pm_select_sleep_state(component->dev);
988e8cc4 2388
416356fc 2389 return 0;
51737470 2390}
6f8ab4ac 2391EXPORT_SYMBOL_GPL(snd_soc_poweroff);
51737470 2392
6f8ab4ac 2393const struct dev_pm_ops snd_soc_pm_ops = {
b1dd5897
VK
2394 .suspend = snd_soc_suspend,
2395 .resume = snd_soc_resume,
2396 .freeze = snd_soc_suspend,
2397 .thaw = snd_soc_resume,
6f8ab4ac 2398 .poweroff = snd_soc_poweroff,
b1dd5897 2399 .restore = snd_soc_resume,
416356fc 2400};
deb2607e 2401EXPORT_SYMBOL_GPL(snd_soc_pm_ops);
416356fc 2402
db2a4165
FM
2403/* ASoC platform driver */
2404static struct platform_driver soc_driver = {
2405 .driver = {
2406 .name = "soc-audio",
6f8ab4ac 2407 .pm = &snd_soc_pm_ops,
db2a4165
FM
2408 },
2409 .probe = soc_probe,
db2a4165
FM
2410};
2411
db2a4165
FM
2412/**
2413 * snd_soc_cnew - create new control
2414 * @_template: control template
2415 * @data: control private data
ac11a2b3 2416 * @long_name: control long name
efb7ac3f 2417 * @prefix: control name prefix
db2a4165
FM
2418 *
2419 * Create a new mixer control from a template control.
2420 *
2421 * Returns 0 for success, else error.
2422 */
2423struct snd_kcontrol *snd_soc_cnew(const struct snd_kcontrol_new *_template,
3056557f 2424 void *data, const char *long_name,
efb7ac3f 2425 const char *prefix)
db2a4165
FM
2426{
2427 struct snd_kcontrol_new template;
efb7ac3f
MB
2428 struct snd_kcontrol *kcontrol;
2429 char *name = NULL;
db2a4165
FM
2430
2431 memcpy(&template, _template, sizeof(template));
db2a4165
FM
2432 template.index = 0;
2433
efb7ac3f
MB
2434 if (!long_name)
2435 long_name = template.name;
2436
2437 if (prefix) {
2b581074 2438 name = kasprintf(GFP_KERNEL, "%s %s", prefix, long_name);
efb7ac3f
MB
2439 if (!name)
2440 return NULL;
2441
efb7ac3f
MB
2442 template.name = name;
2443 } else {
2444 template.name = long_name;
2445 }
2446
2447 kcontrol = snd_ctl_new1(&template, data);
2448
2449 kfree(name);
2450
2451 return kcontrol;
db2a4165
FM
2452}
2453EXPORT_SYMBOL_GPL(snd_soc_cnew);
2454
022658be
LG
2455static int snd_soc_add_controls(struct snd_card *card, struct device *dev,
2456 const struct snd_kcontrol_new *controls, int num_controls,
2457 const char *prefix, void *data)
2458{
eaf2469c 2459 int i;
022658be
LG
2460
2461 for (i = 0; i < num_controls; i++) {
2462 const struct snd_kcontrol_new *control = &controls[i];
eaf2469c
KM
2463 int err = snd_ctl_add(card, snd_soc_cnew(control, data,
2464 control->name, prefix));
022658be 2465 if (err < 0) {
f110bfc7
LG
2466 dev_err(dev, "ASoC: Failed to add %s: %d\n",
2467 control->name, err);
022658be
LG
2468 return err;
2469 }
2470 }
2471
2472 return 0;
2473}
2474
0f2780ad
LPC
2475/**
2476 * snd_soc_add_component_controls - Add an array of controls to a component.
2477 *
2478 * @component: Component to add controls to
2479 * @controls: Array of controls to add
2480 * @num_controls: Number of elements in the array
2481 *
2482 * Return: 0 for success, else error.
2483 */
2484int snd_soc_add_component_controls(struct snd_soc_component *component,
2485 const struct snd_kcontrol_new *controls, unsigned int num_controls)
2486{
2487 struct snd_card *card = component->card->snd_card;
2488
2489 return snd_soc_add_controls(card, component->dev, controls,
2490 num_controls, component->name_prefix, component);
2491}
2492EXPORT_SYMBOL_GPL(snd_soc_add_component_controls);
2493
022658be
LG
2494/**
2495 * snd_soc_add_card_controls - add an array of controls to a SoC card.
2496 * Convenience function to add a list of controls.
2497 *
2498 * @soc_card: SoC card to add controls to
2499 * @controls: array of controls to add
2500 * @num_controls: number of elements in the array
2501 *
2502 * Return 0 for success, else error.
2503 */
2504int snd_soc_add_card_controls(struct snd_soc_card *soc_card,
2505 const struct snd_kcontrol_new *controls, int num_controls)
2506{
2507 struct snd_card *card = soc_card->snd_card;
2508
2509 return snd_soc_add_controls(card, soc_card->dev, controls, num_controls,
2510 NULL, soc_card);
2511}
2512EXPORT_SYMBOL_GPL(snd_soc_add_card_controls);
2513
2514/**
2515 * snd_soc_add_dai_controls - add an array of controls to a DAI.
e9a92dfc 2516 * Convenience function to add a list of controls.
022658be
LG
2517 *
2518 * @dai: DAI to add controls to
2519 * @controls: array of controls to add
2520 * @num_controls: number of elements in the array
2521 *
2522 * Return 0 for success, else error.
2523 */
2524int snd_soc_add_dai_controls(struct snd_soc_dai *dai,
2525 const struct snd_kcontrol_new *controls, int num_controls)
2526{
313665b9 2527 struct snd_card *card = dai->component->card->snd_card;
022658be
LG
2528
2529 return snd_soc_add_controls(card, dai->dev, controls, num_controls,
2530 NULL, dai);
2531}
2532EXPORT_SYMBOL_GPL(snd_soc_add_dai_controls);
2533
c5af3a2e
MB
2534/**
2535 * snd_soc_register_card - Register a card with the ASoC core
2536 *
ac11a2b3 2537 * @card: Card to register
c5af3a2e 2538 *
c5af3a2e 2539 */
70a7ca34 2540int snd_soc_register_card(struct snd_soc_card *card)
c5af3a2e 2541{
a3375522
CR
2542 int ret;
2543
c5af3a2e
MB
2544 if (!card->name || !card->dev)
2545 return -EINVAL;
2546
ed77cc12
MB
2547 dev_set_drvdata(card->dev, card);
2548
b03bfaec
KM
2549 INIT_LIST_HEAD(&card->widgets);
2550 INIT_LIST_HEAD(&card->paths);
2551 INIT_LIST_HEAD(&card->dapm_list);
2552 INIT_LIST_HEAD(&card->aux_comp_list);
2553 INIT_LIST_HEAD(&card->component_dev_list);
2554 INIT_LIST_HEAD(&card->list);
1a497983 2555 INIT_LIST_HEAD(&card->rtd_list);
db432b41 2556 INIT_LIST_HEAD(&card->dapm_dirty);
8a978234 2557 INIT_LIST_HEAD(&card->dobj_list);
b03bfaec 2558
c5af3a2e 2559 card->instantiated = 0;
f0fba2ad 2560 mutex_init(&card->mutex);
a73fb2df 2561 mutex_init(&card->dapm_mutex);
72b745e3 2562 mutex_init(&card->pcm_mutex);
c5af3a2e 2563
a3375522
CR
2564 mutex_lock(&client_mutex);
2565
2566 if (card->devres_dev) {
2567 ret = devm_snd_soc_bind_card(card->devres_dev, card);
2568 if (ret == -EPROBE_DEFER) {
2569 list_add(&card->list, &unbind_card_list);
2570 ret = 0;
2571 }
2572 } else {
2573 ret = snd_soc_bind_card(card);
2574 }
2575
2576 mutex_unlock(&client_mutex);
2577
2578 return ret;
e894efef
SK
2579}
2580EXPORT_SYMBOL_GPL(snd_soc_register_card);
88bd870f 2581
c5af3a2e
MB
2582/**
2583 * snd_soc_unregister_card - Unregister a card with the ASoC core
2584 *
ac11a2b3 2585 * @card: Card to unregister
c5af3a2e 2586 *
c5af3a2e 2587 */
1892a991 2588void snd_soc_unregister_card(struct snd_soc_card *card)
c5af3a2e 2589{
b545542a 2590 mutex_lock(&client_mutex);
a3375522
CR
2591 snd_soc_unbind_card(card);
2592 list_del(&card->list);
b545542a 2593 mutex_unlock(&client_mutex);
e894efef 2594 dev_dbg(card->dev, "ASoC: Unregistered card '%s'\n", card->name);
c5af3a2e 2595}
70a7ca34 2596EXPORT_SYMBOL_GPL(snd_soc_unregister_card);
c5af3a2e 2597
f0fba2ad
LG
2598/*
2599 * Simplify DAI link configuration by removing ".-1" from device names
2600 * and sanitizing names.
2601 */
0b9a214a 2602static char *fmt_single_name(struct device *dev, int *id)
f0fba2ad 2603{
45dd9943
DB
2604 const char *devname = dev_name(dev);
2605 char *found, *name;
d3b47a88 2606 unsigned int id1, id2;
f0fba2ad 2607
45dd9943 2608 if (devname == NULL)
f0fba2ad
LG
2609 return NULL;
2610
45dd9943 2611 name = devm_kstrdup(dev, devname, GFP_KERNEL);
41daf6ba
KW
2612 if (!name)
2613 return NULL;
f0fba2ad
LG
2614
2615 /* are we a "%s.%d" name (platform and SPI components) */
2616 found = strstr(name, dev->driver->name);
2617 if (found) {
2618 /* get ID */
2619 if (sscanf(&found[strlen(dev->driver->name)], ".%d", id) == 1) {
2620
2621 /* discard ID from name if ID == -1 */
2622 if (*id == -1)
2623 found[strlen(dev->driver->name)] = '\0';
2624 }
2625
45dd9943
DB
2626 /* I2C component devices are named "bus-addr" */
2627 } else if (sscanf(name, "%x-%x", &id1, &id2) == 2) {
f0fba2ad 2628
45dd9943
DB
2629 /* create unique ID number from I2C addr and bus */
2630 *id = ((id1 & 0xffff) << 16) + id2;
f0fba2ad 2631
45dd9943
DB
2632 devm_kfree(dev, name);
2633
2634 /* sanitize component name for DAI link creation */
2635 name = devm_kasprintf(dev, GFP_KERNEL, "%s.%s", dev->driver->name, devname);
2636 } else {
2637 *id = 0;
f0fba2ad
LG
2638 }
2639
45dd9943 2640 return name;
f0fba2ad
LG
2641}
2642
2643/*
2644 * Simplify DAI link naming for single devices with multiple DAIs by removing
2645 * any ".-1" and using the DAI name (instead of device name).
2646 */
2647static inline char *fmt_multiple_name(struct device *dev,
2648 struct snd_soc_dai_driver *dai_drv)
2649{
2650 if (dai_drv->name == NULL) {
10e8aa9a
MM
2651 dev_err(dev,
2652 "ASoC: error - multiple DAI %s registered with no name\n",
2653 dev_name(dev));
f0fba2ad
LG
2654 return NULL;
2655 }
2656
50014499 2657 return devm_kstrdup(dev, dai_drv->name, GFP_KERNEL);
f0fba2ad
LG
2658}
2659
ffdbca0b 2660void snd_soc_unregister_dai(struct snd_soc_dai *dai)
e11381f3
KM
2661{
2662 dev_dbg(dai->dev, "ASoC: Unregistered DAI '%s'\n", dai->name);
2663 list_del(&dai->list);
2664}
ffdbca0b 2665EXPORT_SYMBOL_GPL(snd_soc_unregister_dai);
e11381f3 2666
7ca24386
KM
2667/**
2668 * snd_soc_register_dai - Register a DAI dynamically & create its widgets
2669 *
2670 * @component: The component the DAIs are registered for
2671 * @dai_drv: DAI driver to use for the DAI
bc9a6655
RD
2672 * @legacy_dai_naming: if %true, use legacy single-name format;
2673 * if %false, use multiple-name format;
7ca24386
KM
2674 *
2675 * Topology can use this API to register DAIs when probing a component.
2676 * These DAIs's widgets will be freed in the card cleanup and the DAIs
2677 * will be freed in the component cleanup.
2678 */
2679struct snd_soc_dai *snd_soc_register_dai(struct snd_soc_component *component,
2680 struct snd_soc_dai_driver *dai_drv,
2681 bool legacy_dai_naming)
5e4fb372
ML
2682{
2683 struct device *dev = component->dev;
2684 struct snd_soc_dai *dai;
2685
7ca24386
KM
2686 lockdep_assert_held(&client_mutex);
2687
50014499 2688 dai = devm_kzalloc(dev, sizeof(*dai), GFP_KERNEL);
5e4fb372
ML
2689 if (dai == NULL)
2690 return NULL;
2691
2692 /*
2693 * Back in the old days when we still had component-less DAIs,
2694 * instead of having a static name, component-less DAIs would
2695 * inherit the name of the parent device so it is possible to
2696 * register multiple instances of the DAI. We still need to keep
2697 * the same naming style even though those DAIs are not
2698 * component-less anymore.
2699 */
2700 if (legacy_dai_naming &&
2c7b696a 2701 (dai_drv->id == 0 || dai_drv->name == NULL)) {
5e4fb372
ML
2702 dai->name = fmt_single_name(dev, &dai->id);
2703 } else {
2704 dai->name = fmt_multiple_name(dev, dai_drv);
2705 if (dai_drv->id)
2706 dai->id = dai_drv->id;
2707 else
2708 dai->id = component->num_dai;
2709 }
50014499 2710 if (!dai->name)
5e4fb372 2711 return NULL;
5e4fb372
ML
2712
2713 dai->component = component;
2714 dai->dev = dev;
2715 dai->driver = dai_drv;
5e4fb372 2716
15a0c645 2717 /* see for_each_component_dais */
58bf4179 2718 list_add_tail(&dai->list, &component->dai_list);
5e4fb372
ML
2719 component->num_dai++;
2720
2721 dev_dbg(dev, "ASoC: Registered DAI '%s'\n", dai->name);
2722 return dai;
2723}
da039809 2724EXPORT_SYMBOL_GPL(snd_soc_register_dai);
e11381f3 2725
3f6674ae 2726/**
f7b6603c 2727 * snd_soc_unregister_dais - Unregister DAIs from the ASoC core
3f6674ae
KM
2728 *
2729 * @component: The component for which the DAIs should be unregistered
2730 */
2731static void snd_soc_unregister_dais(struct snd_soc_component *component)
2732{
2733 struct snd_soc_dai *dai, *_dai;
2734
e11381f3
KM
2735 for_each_component_dais_safe(component, dai, _dai)
2736 snd_soc_unregister_dai(dai);
3f6674ae
KM
2737}
2738
daf77373
KM
2739/**
2740 * snd_soc_register_dais - Register a DAI with the ASoC core
2741 *
2742 * @component: The component the DAIs are registered for
2743 * @dai_drv: DAI driver to use for the DAIs
2744 * @count: Number of DAIs
2745 */
2746static int snd_soc_register_dais(struct snd_soc_component *component,
2747 struct snd_soc_dai_driver *dai_drv,
2748 size_t count)
2749{
daf77373
KM
2750 struct snd_soc_dai *dai;
2751 unsigned int i;
2752 int ret;
2753
daf77373 2754 for (i = 0; i < count; i++) {
71cb85f5 2755 dai = snd_soc_register_dai(component, dai_drv + i, count == 1 &&
129f055a 2756 component->driver->legacy_dai_naming);
daf77373
KM
2757 if (dai == NULL) {
2758 ret = -ENOMEM;
2759 goto err;
2760 }
2761 }
2762
2763 return 0;
2764
2765err:
2766 snd_soc_unregister_dais(component);
2767
2768 return ret;
2769}
2770
273d778e
KM
2771#define ENDIANNESS_MAP(name) \
2772 (SNDRV_PCM_FMTBIT_##name##LE | SNDRV_PCM_FMTBIT_##name##BE)
2773static u64 endianness_format_map[] = {
2774 ENDIANNESS_MAP(S16_),
2775 ENDIANNESS_MAP(U16_),
2776 ENDIANNESS_MAP(S24_),
2777 ENDIANNESS_MAP(U24_),
2778 ENDIANNESS_MAP(S32_),
2779 ENDIANNESS_MAP(U32_),
2780 ENDIANNESS_MAP(S24_3),
2781 ENDIANNESS_MAP(U24_3),
2782 ENDIANNESS_MAP(S20_3),
2783 ENDIANNESS_MAP(U20_3),
2784 ENDIANNESS_MAP(S18_3),
2785 ENDIANNESS_MAP(U18_3),
2786 ENDIANNESS_MAP(FLOAT_),
2787 ENDIANNESS_MAP(FLOAT64_),
2788 ENDIANNESS_MAP(IEC958_SUBFRAME_),
2789};
2790
2791/*
2792 * Fix up the DAI formats for endianness: codecs don't actually see
2793 * the endianness of the data but we're using the CPU format
2794 * definitions which do need to include endianness so we ensure that
2795 * codec DAIs always have both big and little endian variants set.
2796 */
2797static void convert_endianness_formats(struct snd_soc_pcm_stream *stream)
2798{
2799 int i;
2800
2801 for (i = 0; i < ARRAY_SIZE(endianness_format_map); i++)
2802 if (stream->formats & endianness_format_map[i])
2803 stream->formats |= endianness_format_map[i];
2804}
2805
486c7978
KM
2806static void snd_soc_del_component_unlocked(struct snd_soc_component *component)
2807{
b18768f5 2808 struct snd_soc_card *card = component->card;
a3375522 2809 bool instantiated;
b18768f5 2810
486c7978 2811 snd_soc_unregister_dais(component);
b18768f5 2812
a3375522
CR
2813 if (card) {
2814 instantiated = card->instantiated;
2815 snd_soc_unbind_card(card);
2816 if (instantiated)
2817 list_add(&card->list, &unbind_card_list);
2818 }
b18768f5
KM
2819
2820 list_del(&component->list);
486c7978
KM
2821}
2822
08ff7209
CR
2823int snd_soc_component_initialize(struct snd_soc_component *component,
2824 const struct snd_soc_component_driver *driver,
7274d4cd 2825 struct device *dev)
08ff7209
CR
2826{
2827 INIT_LIST_HEAD(&component->dai_list);
2828 INIT_LIST_HEAD(&component->dobj_list);
2829 INIT_LIST_HEAD(&component->card_list);
c448b7aa 2830 INIT_LIST_HEAD(&component->list);
08ff7209
CR
2831 mutex_init(&component->io_mutex);
2832
7274d4cd 2833 if (!component->name) {
cee28113
TV
2834 component->name = fmt_single_name(dev, &component->id);
2835 if (!component->name) {
2836 dev_err(dev, "ASoC: Failed to allocate name\n");
2837 return -ENOMEM;
2838 }
7274d4cd
CR
2839 }
2840
08ff7209
CR
2841 component->dev = dev;
2842 component->driver = driver;
2843
d462f6ed
HK
2844#ifdef CONFIG_DEBUG_FS
2845 if (!component->debugfs_prefix)
2846 component->debugfs_prefix = driver->debugfs_prefix;
2847#endif
2848
08ff7209
CR
2849 return 0;
2850}
2851EXPORT_SYMBOL_GPL(snd_soc_component_initialize);
2852
ea029dd8
CR
2853int snd_soc_add_component(struct snd_soc_component *component,
2854 struct snd_soc_dai_driver *dai_drv,
2855 int num_dai)
d191bd8d 2856{
a3375522 2857 struct snd_soc_card *card, *c;
bb13109d 2858 int ret;
273d778e 2859 int i;
d191bd8d 2860
b18768f5
KM
2861 mutex_lock(&client_mutex);
2862
ea029dd8 2863 if (component->driver->endianness) {
273d778e
KM
2864 for (i = 0; i < num_dai; i++) {
2865 convert_endianness_formats(&dai_drv[i].playback);
2866 convert_endianness_formats(&dai_drv[i].capture);
2867 }
2868 }
2869
0e7b25c6 2870 ret = snd_soc_register_dais(component, dai_drv, num_dai);
bb13109d 2871 if (ret < 0) {
ea029dd8
CR
2872 dev_err(component->dev, "ASoC: Failed to register DAIs: %d\n",
2873 ret);
bb13109d
LPC
2874 goto err_cleanup;
2875 }
d191bd8d 2876
b18768f5
KM
2877 if (!component->driver->write && !component->driver->read) {
2878 if (!component->regmap)
2879 component->regmap = dev_get_regmap(component->dev,
2880 NULL);
2881 if (component->regmap)
2882 snd_soc_component_setup_regmap(component);
2883 }
4da53393 2884
b18768f5
KM
2885 /* see for_each_component */
2886 list_add(&component->list, &component_list);
4da53393 2887
a3375522
CR
2888 list_for_each_entry_safe(card, c, &unbind_card_list, list)
2889 snd_soc_rebind_card(card);
2890
bb13109d 2891err_cleanup:
b18768f5
KM
2892 if (ret < 0)
2893 snd_soc_del_component_unlocked(component);
ea029dd8 2894
b18768f5 2895 mutex_unlock(&client_mutex);
bb13109d 2896 return ret;
4da53393 2897}
e0dac41b
KM
2898EXPORT_SYMBOL_GPL(snd_soc_add_component);
2899
2900int snd_soc_register_component(struct device *dev,
2901 const struct snd_soc_component_driver *component_driver,
2902 struct snd_soc_dai_driver *dai_drv,
2903 int num_dai)
2904{
2905 struct snd_soc_component *component;
ea029dd8 2906 int ret;
e0dac41b 2907
7ecbd6a9 2908 component = devm_kzalloc(dev, sizeof(*component), GFP_KERNEL);
08e61d03 2909 if (!component)
e0dac41b 2910 return -ENOMEM;
e0dac41b 2911
ea029dd8
CR
2912 ret = snd_soc_component_initialize(component, component_driver, dev);
2913 if (ret < 0)
2914 return ret;
2915
2916 return snd_soc_add_component(component, dai_drv, num_dai);
e0dac41b 2917}
bb13109d 2918EXPORT_SYMBOL_GPL(snd_soc_register_component);
4da53393 2919
58f30150
MR
2920/**
2921 * snd_soc_unregister_component_by_driver - Unregister component using a given driver
2922 * from the ASoC core
2923 *
2924 * @dev: The device to unregister
2925 * @component_driver: The component driver to unregister
2926 */
2927void snd_soc_unregister_component_by_driver(struct device *dev,
2928 const struct snd_soc_component_driver *component_driver)
2929{
144d6dfc 2930 const char *driver_name = NULL;
58f30150 2931
144d6dfc
KM
2932 if (component_driver)
2933 driver_name = component_driver->name;
58f30150 2934
34e81ab4 2935 mutex_lock(&client_mutex);
ac6a4dd3 2936 while (1) {
144d6dfc 2937 struct snd_soc_component *component = snd_soc_lookup_component_nolocked(dev, driver_name);
5600f3d5 2938
ac6a4dd3
KM
2939 if (!component)
2940 break;
21a03528 2941
486c7978 2942 snd_soc_del_component_unlocked(component);
d191bd8d 2943 }
34e81ab4 2944 mutex_unlock(&client_mutex);
d191bd8d 2945}
144d6dfc 2946EXPORT_SYMBOL_GPL(snd_soc_unregister_component_by_driver);
d191bd8d 2947
bec4fa05 2948/* Retrieve a card's name from device tree */
b07609ce
KM
2949int snd_soc_of_parse_card_name(struct snd_soc_card *card,
2950 const char *propname)
bec4fa05 2951{
b07609ce 2952 struct device_node *np;
bec4fa05
SW
2953 int ret;
2954
7e07e7c0
TB
2955 if (!card->dev) {
2956 pr_err("card->dev is not set before calling %s\n", __func__);
2957 return -EINVAL;
2958 }
2959
b07609ce 2960 np = card->dev->of_node;
7e07e7c0 2961
bec4fa05
SW
2962 ret = of_property_read_string_index(np, propname, 0, &card->name);
2963 /*
2964 * EINVAL means the property does not exist. This is fine providing
2965 * card->name was previously set, which is checked later in
2966 * snd_soc_register_card.
2967 */
2968 if (ret < 0 && ret != -EINVAL) {
2969 dev_err(card->dev,
f110bfc7 2970 "ASoC: Property '%s' could not be read: %d\n",
bec4fa05
SW
2971 propname, ret);
2972 return ret;
2973 }
2974
2975 return 0;
2976}
b07609ce 2977EXPORT_SYMBOL_GPL(snd_soc_of_parse_card_name);
bec4fa05 2978
9a6d4860
XL
2979static const struct snd_soc_dapm_widget simple_widgets[] = {
2980 SND_SOC_DAPM_MIC("Microphone", NULL),
2981 SND_SOC_DAPM_LINE("Line", NULL),
2982 SND_SOC_DAPM_HP("Headphone", NULL),
2983 SND_SOC_DAPM_SPK("Speaker", NULL),
2984};
2985
21efde50 2986int snd_soc_of_parse_audio_simple_widgets(struct snd_soc_card *card,
9a6d4860
XL
2987 const char *propname)
2988{
21efde50 2989 struct device_node *np = card->dev->of_node;
9a6d4860
XL
2990 struct snd_soc_dapm_widget *widgets;
2991 const char *template, *wname;
99c68653 2992 int i, j, num_widgets;
9a6d4860
XL
2993
2994 num_widgets = of_property_count_strings(np, propname);
2995 if (num_widgets < 0) {
2996 dev_err(card->dev,
2997 "ASoC: Property '%s' does not exist\n", propname);
2998 return -EINVAL;
2999 }
8ad0b83e
PLB
3000 if (!num_widgets) {
3001 dev_err(card->dev, "ASoC: Property '%s's length is zero\n",
3002 propname);
3003 return -EINVAL;
3004 }
9a6d4860
XL
3005 if (num_widgets & 1) {
3006 dev_err(card->dev,
3007 "ASoC: Property '%s' length is not even\n", propname);
3008 return -EINVAL;
3009 }
3010
3011 num_widgets /= 2;
9a6d4860
XL
3012
3013 widgets = devm_kcalloc(card->dev, num_widgets, sizeof(*widgets),
3014 GFP_KERNEL);
3015 if (!widgets) {
3016 dev_err(card->dev,
3017 "ASoC: Could not allocate memory for widgets\n");
3018 return -ENOMEM;
3019 }
3020
3021 for (i = 0; i < num_widgets; i++) {
99c68653
KM
3022 int ret = of_property_read_string_index(np, propname,
3023 2 * i, &template);
9a6d4860
XL
3024 if (ret) {
3025 dev_err(card->dev,
3026 "ASoC: Property '%s' index %d read error:%d\n",
3027 propname, 2 * i, ret);
3028 return -EINVAL;
3029 }
3030
3031 for (j = 0; j < ARRAY_SIZE(simple_widgets); j++) {
3032 if (!strncmp(template, simple_widgets[j].name,
3033 strlen(simple_widgets[j].name))) {
3034 widgets[i] = simple_widgets[j];
3035 break;
3036 }
3037 }
3038
3039 if (j >= ARRAY_SIZE(simple_widgets)) {
3040 dev_err(card->dev,
3041 "ASoC: DAPM widget '%s' is not supported\n",
3042 template);
3043 return -EINVAL;
3044 }
3045
3046 ret = of_property_read_string_index(np, propname,
3047 (2 * i) + 1,
3048 &wname);
3049 if (ret) {
3050 dev_err(card->dev,
3051 "ASoC: Property '%s' index %d read error:%d\n",
3052 propname, (2 * i) + 1, ret);
3053 return -EINVAL;
3054 }
3055
3056 widgets[i].name = wname;
3057 }
3058
f23e860e
NC
3059 card->of_dapm_widgets = widgets;
3060 card->num_of_dapm_widgets = num_widgets;
9a6d4860
XL
3061
3062 return 0;
3063}
21efde50 3064EXPORT_SYMBOL_GPL(snd_soc_of_parse_audio_simple_widgets);
9a6d4860 3065
3d4641a4
SG
3066int snd_soc_of_parse_pin_switches(struct snd_soc_card *card, const char *prop)
3067{
3068 const unsigned int nb_controls_max = 16;
3069 const char **strings, *control_name;
3070 struct snd_kcontrol_new *controls;
3071 struct device *dev = card->dev;
3072 unsigned int i, nb_controls;
3073 int ret;
3074
6eab7034 3075 if (!of_property_present(dev->of_node, prop))
3d4641a4
SG
3076 return 0;
3077
3078 strings = devm_kcalloc(dev, nb_controls_max,
3079 sizeof(*strings), GFP_KERNEL);
3080 if (!strings)
3081 return -ENOMEM;
3082
3083 ret = of_property_read_string_array(dev->of_node, prop,
3084 strings, nb_controls_max);
3085 if (ret < 0)
3086 return ret;
3087
3088 nb_controls = (unsigned int)ret;
3089
3090 controls = devm_kcalloc(dev, nb_controls,
3091 sizeof(*controls), GFP_KERNEL);
3092 if (!controls)
3093 return -ENOMEM;
3094
3095 for (i = 0; i < nb_controls; i++) {
3096 control_name = devm_kasprintf(dev, GFP_KERNEL,
3097 "%s Switch", strings[i]);
3098 if (!control_name)
3099 return -ENOMEM;
3100
3101 controls[i].iface = SNDRV_CTL_ELEM_IFACE_MIXER;
3102 controls[i].name = control_name;
3103 controls[i].info = snd_soc_dapm_info_pin_switch;
3104 controls[i].get = snd_soc_dapm_get_pin_switch;
3105 controls[i].put = snd_soc_dapm_put_pin_switch;
3106 controls[i].private_value = (unsigned long)strings[i];
3107 }
3108
3109 card->controls = controls;
3110 card->num_controls = nb_controls;
3111
3112 return 0;
3113}
3114EXPORT_SYMBOL_GPL(snd_soc_of_parse_pin_switches);
3115
cbdfab3b
JB
3116int snd_soc_of_get_slot_mask(struct device_node *np,
3117 const char *prop_name,
3118 unsigned int *mask)
6131084a
JS
3119{
3120 u32 val;
6c84e591 3121 const __be32 *of_slot_mask = of_get_property(np, prop_name, &val);
6131084a
JS
3122 int i;
3123
3124 if (!of_slot_mask)
3125 return 0;
3126 val /= sizeof(u32);
3127 for (i = 0; i < val; i++)
3128 if (be32_to_cpup(&of_slot_mask[i]))
3129 *mask |= (1 << i);
3130
3131 return val;
3132}
cbdfab3b 3133EXPORT_SYMBOL_GPL(snd_soc_of_get_slot_mask);
6131084a 3134
89c67857 3135int snd_soc_of_parse_tdm_slot(struct device_node *np,
6131084a
JS
3136 unsigned int *tx_mask,
3137 unsigned int *rx_mask,
89c67857
XL
3138 unsigned int *slots,
3139 unsigned int *slot_width)
3140{
3141 u32 val;
3142 int ret;
3143
6131084a
JS
3144 if (tx_mask)
3145 snd_soc_of_get_slot_mask(np, "dai-tdm-slot-tx-mask", tx_mask);
3146 if (rx_mask)
3147 snd_soc_of_get_slot_mask(np, "dai-tdm-slot-rx-mask", rx_mask);
3148
6eab7034
GU
3149 ret = of_property_read_u32(np, "dai-tdm-slot-num", &val);
3150 if (ret && ret != -EINVAL)
3151 return ret;
3152 if (!ret && slots)
3153 *slots = val;
89c67857 3154
6eab7034
GU
3155 ret = of_property_read_u32(np, "dai-tdm-slot-width", &val);
3156 if (ret && ret != -EINVAL)
3157 return ret;
3158 if (!ret && slot_width)
3159 *slot_width = val;
89c67857
XL
3160
3161 return 0;
3162}
3163EXPORT_SYMBOL_GPL(snd_soc_of_parse_tdm_slot);
3164
988bad5e
KM
3165void snd_soc_dlc_use_cpu_as_platform(struct snd_soc_dai_link_component *platforms,
3166 struct snd_soc_dai_link_component *cpus)
3167{
3168 platforms->of_node = cpus->of_node;
3169 platforms->dai_args = cpus->dai_args;
3170}
3171EXPORT_SYMBOL_GPL(snd_soc_dlc_use_cpu_as_platform);
3172
3b710356
KM
3173void snd_soc_of_parse_node_prefix(struct device_node *np,
3174 struct snd_soc_codec_conf *codec_conf,
3175 struct device_node *of_node,
3176 const char *propname)
5e3cdaa2 3177{
5e3cdaa2
KM
3178 const char *str;
3179 int ret;
3180
3181 ret = of_property_read_string(np, propname, &str);
3182 if (ret < 0) {
3183 /* no prefix is not error */
3184 return;
3185 }
3186
c13493a2 3187 codec_conf->dlc.of_node = of_node;
5e3cdaa2
KM
3188 codec_conf->name_prefix = str;
3189}
3b710356 3190EXPORT_SYMBOL_GPL(snd_soc_of_parse_node_prefix);
5e3cdaa2 3191
2bc644af 3192int snd_soc_of_parse_audio_routing(struct snd_soc_card *card,
a4a54dd5
SW
3193 const char *propname)
3194{
2bc644af 3195 struct device_node *np = card->dev->of_node;
e3b1e6a1 3196 int num_routes;
a4a54dd5 3197 struct snd_soc_dapm_route *routes;
51a3dd58 3198 int i;
a4a54dd5
SW
3199
3200 num_routes = of_property_count_strings(np, propname);
c34ce320 3201 if (num_routes < 0 || num_routes & 1) {
10e8aa9a
MM
3202 dev_err(card->dev,
3203 "ASoC: Property '%s' does not exist or its length is not even\n",
3204 propname);
a4a54dd5
SW
3205 return -EINVAL;
3206 }
3207 num_routes /= 2;
a4a54dd5 3208
a86854d0 3209 routes = devm_kcalloc(card->dev, num_routes, sizeof(*routes),
a4a54dd5
SW
3210 GFP_KERNEL);
3211 if (!routes) {
3212 dev_err(card->dev,
f110bfc7 3213 "ASoC: Could not allocate DAPM route table\n");
7d3865a1 3214 return -ENOMEM;
a4a54dd5
SW
3215 }
3216
3217 for (i = 0; i < num_routes; i++) {
51a3dd58
KM
3218 int ret = of_property_read_string_index(np, propname,
3219 2 * i, &routes[i].sink);
a4a54dd5 3220 if (ret) {
c871bd0b
MB
3221 dev_err(card->dev,
3222 "ASoC: Property '%s' index %d could not be read: %d\n",
3223 propname, 2 * i, ret);
a4a54dd5
SW
3224 return -EINVAL;
3225 }
3226 ret = of_property_read_string_index(np, propname,
e3b1e6a1 3227 (2 * i) + 1, &routes[i].source);
a4a54dd5
SW
3228 if (ret) {
3229 dev_err(card->dev,
c871bd0b
MB
3230 "ASoC: Property '%s' index %d could not be read: %d\n",
3231 propname, (2 * i) + 1, ret);
a4a54dd5
SW
3232 return -EINVAL;
3233 }
3234 }
3235
f23e860e
NC
3236 card->num_of_dapm_routes = num_routes;
3237 card->of_dapm_routes = routes;
a4a54dd5
SW
3238
3239 return 0;
3240}
2bc644af 3241EXPORT_SYMBOL_GPL(snd_soc_of_parse_audio_routing);
a4a54dd5 3242
1ae0965d
SG
3243int snd_soc_of_parse_aux_devs(struct snd_soc_card *card, const char *propname)
3244{
3245 struct device_node *node = card->dev->of_node;
3246 struct snd_soc_aux_dev *aux;
3247 int num, i;
3248
3249 num = of_count_phandle_with_args(node, propname, NULL);
3250 if (num == -ENOENT) {
3251 return 0;
3252 } else if (num < 0) {
3253 dev_err(card->dev, "ASOC: Property '%s' could not be read: %d\n",
3254 propname, num);
3255 return num;
3256 }
3257
3258 aux = devm_kcalloc(card->dev, num, sizeof(*aux), GFP_KERNEL);
3259 if (!aux)
3260 return -ENOMEM;
3261 card->aux_dev = aux;
3262 card->num_aux_devs = num;
3263
3264 for_each_card_pre_auxs(card, i, aux) {
3265 aux->dlc.of_node = of_parse_phandle(node, propname, i);
3266 if (!aux->dlc.of_node)
3267 return -EINVAL;
3268 }
3269
3270 return 0;
3271}
3272EXPORT_SYMBOL_GPL(snd_soc_of_parse_aux_devs);
3273
64c917d1 3274unsigned int snd_soc_daifmt_clock_provider_flipped(unsigned int dai_fmt)
b44a67f8
KM
3275{
3276 unsigned int inv_dai_fmt = dai_fmt & ~SND_SOC_DAIFMT_CLOCK_PROVIDER_MASK;
3277
3278 switch (dai_fmt & SND_SOC_DAIFMT_CLOCK_PROVIDER_MASK) {
3279 case SND_SOC_DAIFMT_CBP_CFP:
3280 inv_dai_fmt |= SND_SOC_DAIFMT_CBC_CFC;
3281 break;
3282 case SND_SOC_DAIFMT_CBP_CFC:
3283 inv_dai_fmt |= SND_SOC_DAIFMT_CBC_CFP;
3284 break;
3285 case SND_SOC_DAIFMT_CBC_CFP:
3286 inv_dai_fmt |= SND_SOC_DAIFMT_CBP_CFC;
3287 break;
3288 case SND_SOC_DAIFMT_CBC_CFC:
3289 inv_dai_fmt |= SND_SOC_DAIFMT_CBP_CFP;
3290 break;
3291 }
3292
3293 return inv_dai_fmt;
3294}
64c917d1 3295EXPORT_SYMBOL_GPL(snd_soc_daifmt_clock_provider_flipped);
b44a67f8 3296
91ae4477
KM
3297unsigned int snd_soc_daifmt_clock_provider_from_bitmap(unsigned int bit_frame)
3298{
7766861d
KM
3299 /*
3300 * bit_frame is return value from
3301 * snd_soc_daifmt_parse_clock_provider_raw()
3302 */
3303
91ae4477
KM
3304 /* Codec base */
3305 switch (bit_frame) {
3306 case 0x11:
3307 return SND_SOC_DAIFMT_CBP_CFP;
3308 case 0x10:
3309 return SND_SOC_DAIFMT_CBP_CFC;
3310 case 0x01:
3311 return SND_SOC_DAIFMT_CBC_CFP;
3312 default:
3313 return SND_SOC_DAIFMT_CBC_CFC;
3314 }
3315
3316 return 0;
3317}
3318EXPORT_SYMBOL_GPL(snd_soc_daifmt_clock_provider_from_bitmap);
3319
7766861d
KM
3320unsigned int snd_soc_daifmt_parse_format(struct device_node *np,
3321 const char *prefix)
a7930ed4 3322{
5ad76775 3323 int ret;
a7930ed4
KM
3324 char prop[128];
3325 unsigned int format = 0;
3326 int bit, frame;
3327 const char *str;
3328 struct {
3329 char *name;
3330 unsigned int val;
3331 } of_fmt_table[] = {
3332 { "i2s", SND_SOC_DAIFMT_I2S },
3333 { "right_j", SND_SOC_DAIFMT_RIGHT_J },
3334 { "left_j", SND_SOC_DAIFMT_LEFT_J },
3335 { "dsp_a", SND_SOC_DAIFMT_DSP_A },
3336 { "dsp_b", SND_SOC_DAIFMT_DSP_B },
3337 { "ac97", SND_SOC_DAIFMT_AC97 },
3338 { "pdm", SND_SOC_DAIFMT_PDM},
3339 { "msb", SND_SOC_DAIFMT_MSB },
3340 { "lsb", SND_SOC_DAIFMT_LSB },
a7930ed4
KM
3341 };
3342
3343 if (!prefix)
3344 prefix = "";
3345
3346 /*
5711c979
KM
3347 * check "dai-format = xxx"
3348 * or "[prefix]format = xxx"
a7930ed4
KM
3349 * SND_SOC_DAIFMT_FORMAT_MASK area
3350 */
5711c979
KM
3351 ret = of_property_read_string(np, "dai-format", &str);
3352 if (ret < 0) {
3353 snprintf(prop, sizeof(prop), "%sformat", prefix);
3354 ret = of_property_read_string(np, prop, &str);
3355 }
a7930ed4 3356 if (ret == 0) {
5ad76775
KM
3357 int i;
3358
a7930ed4
KM
3359 for (i = 0; i < ARRAY_SIZE(of_fmt_table); i++) {
3360 if (strcmp(str, of_fmt_table[i].name) == 0) {
3361 format |= of_fmt_table[i].val;
3362 break;
3363 }
3364 }
3365 }
3366
3367 /*
8c2d6a9f 3368 * check "[prefix]continuous-clock"
a7930ed4
KM
3369 * SND_SOC_DAIFMT_CLOCK_MASK area
3370 */
8c2d6a9f 3371 snprintf(prop, sizeof(prop), "%scontinuous-clock", prefix);
51930295 3372 if (of_property_read_bool(np, prop))
8c2d6a9f
KM
3373 format |= SND_SOC_DAIFMT_CONT;
3374 else
3375 format |= SND_SOC_DAIFMT_GATED;
a7930ed4
KM
3376
3377 /*
3378 * check "[prefix]bitclock-inversion"
3379 * check "[prefix]frame-inversion"
3380 * SND_SOC_DAIFMT_INV_MASK area
3381 */
3382 snprintf(prop, sizeof(prop), "%sbitclock-inversion", prefix);
69dd15a8 3383 bit = of_property_read_bool(np, prop);
a7930ed4
KM
3384
3385 snprintf(prop, sizeof(prop), "%sframe-inversion", prefix);
69dd15a8 3386 frame = of_property_read_bool(np, prop);
a7930ed4
KM
3387
3388 switch ((bit << 4) + frame) {
3389 case 0x11:
3390 format |= SND_SOC_DAIFMT_IB_IF;
3391 break;
3392 case 0x10:
3393 format |= SND_SOC_DAIFMT_IB_NF;
3394 break;
3395 case 0x01:
3396 format |= SND_SOC_DAIFMT_NB_IF;
3397 break;
3398 default:
3399 /* SND_SOC_DAIFMT_NB_NF is default */
3400 break;
3401 }
3402
7766861d
KM
3403 return format;
3404}
3405EXPORT_SYMBOL_GPL(snd_soc_daifmt_parse_format);
3406
3407unsigned int snd_soc_daifmt_parse_clock_provider_raw(struct device_node *np,
3408 const char *prefix,
3409 struct device_node **bitclkmaster,
3410 struct device_node **framemaster)
3411{
3412 char prop[128];
3413 unsigned int bit, frame;
3414
c8903242
KM
3415 if (!np)
3416 return 0;
3417
7766861d
KM
3418 if (!prefix)
3419 prefix = "";
3420
a7930ed4
KM
3421 /*
3422 * check "[prefix]bitclock-master"
3423 * check "[prefix]frame-master"
a7930ed4
KM
3424 */
3425 snprintf(prop, sizeof(prop), "%sbitclock-master", prefix);
6eab7034 3426 bit = of_property_present(np, prop);
389cb834
JS
3427 if (bit && bitclkmaster)
3428 *bitclkmaster = of_parse_phandle(np, prop, 0);
a7930ed4
KM
3429
3430 snprintf(prop, sizeof(prop), "%sframe-master", prefix);
6eab7034 3431 frame = of_property_present(np, prop);
389cb834
JS
3432 if (frame && framemaster)
3433 *framemaster = of_parse_phandle(np, prop, 0);
a7930ed4 3434
7766861d
KM
3435 /*
3436 * return bitmap.
3437 * It will be parameter of
3438 * snd_soc_daifmt_clock_provider_from_bitmap()
3439 */
3440 return (bit << 4) + frame;
a7930ed4 3441}
7766861d 3442EXPORT_SYMBOL_GPL(snd_soc_daifmt_parse_clock_provider_raw);
a7930ed4 3443
f3ac3da7 3444int snd_soc_get_stream_cpu(const struct snd_soc_dai_link *dai_link, int stream)
1c943f60
KM
3445{
3446 /*
3447 * [Normal]
3448 *
3449 * Playback
3450 * CPU : SNDRV_PCM_STREAM_PLAYBACK
3451 * Codec: SNDRV_PCM_STREAM_PLAYBACK
3452 *
3453 * Capture
3454 * CPU : SNDRV_PCM_STREAM_CAPTURE
3455 * Codec: SNDRV_PCM_STREAM_CAPTURE
3456 */
3457 if (!dai_link->c2c_params)
3458 return stream;
3459
3460 /*
3461 * [Codec2Codec]
3462 *
3463 * Playback
3464 * CPU : SNDRV_PCM_STREAM_CAPTURE
3465 * Codec: SNDRV_PCM_STREAM_PLAYBACK
3466 *
3467 * Capture
3468 * CPU : SNDRV_PCM_STREAM_PLAYBACK
3469 * Codec: SNDRV_PCM_STREAM_CAPTURE
3470 */
3471 if (stream == SNDRV_PCM_STREAM_CAPTURE)
3472 return SNDRV_PCM_STREAM_PLAYBACK;
3473
3474 return SNDRV_PCM_STREAM_CAPTURE;
3475}
3476EXPORT_SYMBOL_GPL(snd_soc_get_stream_cpu);
3477
a180e8b9
KM
3478int snd_soc_get_dai_id(struct device_node *ep)
3479{
09d4cc03 3480 struct snd_soc_component *component;
521d675d
KM
3481 struct snd_soc_dai_link_component dlc = {
3482 .of_node = of_graph_get_port_parent(ep),
3483 };
a180e8b9
KM
3484 int ret;
3485
521d675d 3486
a180e8b9
KM
3487 /*
3488 * For example HDMI case, HDMI has video/sound port,
3489 * but ALSA SoC needs sound port number only.
3490 * Thus counting HDMI DT port/endpoint doesn't work.
3491 * Then, it should have .of_xlate_dai_id
3492 */
3493 ret = -ENOTSUPP;
3494 mutex_lock(&client_mutex);
c1e230f0 3495 component = soc_find_component(&dlc);
2c7b1704
KM
3496 if (component)
3497 ret = snd_soc_component_of_xlate_dai_id(component, ep);
a180e8b9
KM
3498 mutex_unlock(&client_mutex);
3499
c1e230f0 3500 of_node_put(dlc.of_node);
c0a480d1 3501
a180e8b9
KM
3502 return ret;
3503}
3504EXPORT_SYMBOL_GPL(snd_soc_get_dai_id);
3505
05722a0c 3506int snd_soc_get_dlc(const struct of_phandle_args *args, struct snd_soc_dai_link_component *dlc)
cb470087
KM
3507{
3508 struct snd_soc_component *pos;
93b0f3ee 3509 int ret = -EPROBE_DEFER;
cb470087
KM
3510
3511 mutex_lock(&client_mutex);
368dee94 3512 for_each_component(pos) {
bce00560 3513 struct device_node *component_of_node = soc_component_to_node(pos);
3e0aa8d8 3514
f7d344a2 3515 if (component_of_node != args->np || !pos->num_dai)
cb470087
KM
3516 continue;
3517
05722a0c 3518 ret = snd_soc_component_of_xlate_dai_name(pos, args, &dlc->dai_name);
a2a34175 3519 if (ret == -ENOTSUPP) {
58bf4179 3520 struct snd_soc_dai *dai;
6833c452
KM
3521 int id = -1;
3522
93b0f3ee 3523 switch (args->args_count) {
6833c452
KM
3524 case 0:
3525 id = 0; /* same as dai_drv[0] */
3526 break;
3527 case 1:
93b0f3ee 3528 id = args->args[0];
6833c452
KM
3529 break;
3530 default:
3531 /* not supported */
3532 break;
3533 }
3534
3535 if (id < 0 || id >= pos->num_dai) {
3536 ret = -EINVAL;
3dcba280 3537 continue;
6833c452 3538 }
e41975ed
XL
3539
3540 ret = 0;
3541
58bf4179 3542 /* find target DAI */
15a0c645 3543 for_each_component_dais(pos, dai) {
58bf4179
KM
3544 if (id == 0)
3545 break;
3546 id--;
3547 }
3548
09f75f09 3549 dlc->dai_name = snd_soc_dai_name_get(dai);
1dfa5a5a
JB
3550 } else if (ret) {
3551 /*
3552 * if another error than ENOTSUPP is returned go on and
3553 * check if another component is provided with the same
3554 * node. This may happen if a device provides several
3555 * components
3556 */
3557 continue;
cb470087
KM
3558 }
3559
cb470087
KM
3560 break;
3561 }
065aa861
KM
3562
3563 if (ret == 0)
3564 dlc->of_node = args->np;
3565
cb470087 3566 mutex_unlock(&client_mutex);
93b0f3ee
JFM
3567 return ret;
3568}
05722a0c 3569EXPORT_SYMBOL_GPL(snd_soc_get_dlc);
93b0f3ee 3570
05722a0c
KM
3571int snd_soc_of_get_dlc(struct device_node *of_node,
3572 struct of_phandle_args *args,
3573 struct snd_soc_dai_link_component *dlc,
3574 int index)
93b0f3ee 3575{
05722a0c 3576 struct of_phandle_args __args;
93b0f3ee
JFM
3577 int ret;
3578
05722a0c
KM
3579 if (!args)
3580 args = &__args;
3581
93b0f3ee 3582 ret = of_parse_phandle_with_args(of_node, "sound-dai",
05722a0c 3583 "#sound-dai-cells", index, args);
93b0f3ee
JFM
3584 if (ret)
3585 return ret;
3586
05722a0c
KM
3587 return snd_soc_get_dlc(args, dlc);
3588}
3589EXPORT_SYMBOL_GPL(snd_soc_of_get_dlc);
3590
3591int snd_soc_get_dai_name(const struct of_phandle_args *args,
3592 const char **dai_name)
3593{
3594 struct snd_soc_dai_link_component dlc;
3595 int ret = snd_soc_get_dlc(args, &dlc);
3596
3597 if (ret == 0)
3598 *dai_name = dlc.dai_name;
3599
3600 return ret;
3601}
3602EXPORT_SYMBOL_GPL(snd_soc_get_dai_name);
cb470087 3603
05722a0c 3604int snd_soc_of_get_dai_name(struct device_node *of_node,
3c8b5861 3605 const char **dai_name, int index)
05722a0c
KM
3606{
3607 struct snd_soc_dai_link_component dlc;
3c8b5861 3608 int ret = snd_soc_of_get_dlc(of_node, NULL, &dlc, index);
05722a0c
KM
3609
3610 if (ret == 0)
3611 *dai_name = dlc.dai_name;
cb470087
KM
3612
3613 return ret;
3614}
3615EXPORT_SYMBOL_GPL(snd_soc_of_get_dai_name);
3616
cf88ab48 3617struct snd_soc_dai *snd_soc_get_dai_via_args(const struct of_phandle_args *dai_args)
442ae56c
KM
3618{
3619 struct snd_soc_dai *dai;
3620 struct snd_soc_component *component;
3621
3622 mutex_lock(&client_mutex);
3623 for_each_component(component) {
3624 for_each_component_dais(component, dai)
3625 if (snd_soc_is_match_dai_args(dai->driver->dai_args, dai_args))
3626 goto found;
3627 }
3628 dai = NULL;
3629found:
3630 mutex_unlock(&client_mutex);
3631 return dai;
3632}
3633EXPORT_SYMBOL_GPL(snd_soc_get_dai_via_args);
3634
9cc69528
KM
3635static void __snd_soc_of_put_component(struct snd_soc_dai_link_component *component)
3636{
3637 if (component->of_node) {
3638 of_node_put(component->of_node);
3639 component->of_node = NULL;
3640 }
3641}
3642
3643static int __snd_soc_of_get_dai_link_component_alloc(
3644 struct device *dev, struct device_node *of_node,
3645 struct snd_soc_dai_link_component **ret_component,
3646 int *ret_num)
3647{
3648 struct snd_soc_dai_link_component *component;
3649 int num;
3650
3651 /* Count the number of CPUs/CODECs */
3652 num = of_count_phandle_with_args(of_node, "sound-dai", "#sound-dai-cells");
3653 if (num <= 0) {
3654 if (num == -ENOENT)
3655 dev_err(dev, "No 'sound-dai' property\n");
3656 else
3657 dev_err(dev, "Bad phandle in 'sound-dai'\n");
3658 return num;
3659 }
3660 component = devm_kcalloc(dev, num, sizeof(*component), GFP_KERNEL);
3661 if (!component)
3662 return -ENOMEM;
3663
3664 *ret_component = component;
3665 *ret_num = num;
3666
3667 return 0;
3668}
3669
94685763
SN
3670/*
3671 * snd_soc_of_put_dai_link_codecs - Dereference device nodes in the codecs array
3672 * @dai_link: DAI link
3673 *
3674 * Dereference device nodes acquired by snd_soc_of_get_dai_link_codecs().
3675 */
3676void snd_soc_of_put_dai_link_codecs(struct snd_soc_dai_link *dai_link)
3677{
3db769f1 3678 struct snd_soc_dai_link_component *component;
94685763
SN
3679 int index;
3680
9cc69528
KM
3681 for_each_link_codecs(dai_link, index, component)
3682 __snd_soc_of_put_component(component);
94685763
SN
3683}
3684EXPORT_SYMBOL_GPL(snd_soc_of_put_dai_link_codecs);
3685
93b0f3ee
JFM
3686/*
3687 * snd_soc_of_get_dai_link_codecs - Parse a list of CODECs in the devicetree
3688 * @dev: Card device
3689 * @of_node: Device node
3690 * @dai_link: DAI link
3691 *
3692 * Builds an array of CODEC DAI components from the DAI link property
3693 * 'sound-dai'.
3694 * The array is set in the DAI link and the number of DAIs is set accordingly.
94685763
SN
3695 * The device nodes in the array (of_node) must be dereferenced by calling
3696 * snd_soc_of_put_dai_link_codecs() on @dai_link.
93b0f3ee
JFM
3697 *
3698 * Returns 0 for success
3699 */
3700int snd_soc_of_get_dai_link_codecs(struct device *dev,
3701 struct device_node *of_node,
3702 struct snd_soc_dai_link *dai_link)
3703{
93b0f3ee 3704 struct snd_soc_dai_link_component *component;
9cc69528
KM
3705 int index, ret;
3706
3707 ret = __snd_soc_of_get_dai_link_component_alloc(dev, of_node,
3708 &dai_link->codecs, &dai_link->num_codecs);
3709 if (ret < 0)
3710 return ret;
93b0f3ee
JFM
3711
3712 /* Parse the list */
3db769f1 3713 for_each_link_codecs(dai_link, index, component) {
14c9b25f 3714 ret = snd_soc_of_get_dlc(of_node, NULL, component, index);
93b0f3ee
JFM
3715 if (ret)
3716 goto err;
93b0f3ee
JFM
3717 }
3718 return 0;
3719err:
94685763 3720 snd_soc_of_put_dai_link_codecs(dai_link);
93b0f3ee
JFM
3721 dai_link->codecs = NULL;
3722 dai_link->num_codecs = 0;
3723 return ret;
3724}
3725EXPORT_SYMBOL_GPL(snd_soc_of_get_dai_link_codecs);
3726
900dedd7
MP
3727/*
3728 * snd_soc_of_put_dai_link_cpus - Dereference device nodes in the codecs array
3729 * @dai_link: DAI link
3730 *
3731 * Dereference device nodes acquired by snd_soc_of_get_dai_link_cpus().
3732 */
3733void snd_soc_of_put_dai_link_cpus(struct snd_soc_dai_link *dai_link)
3734{
3735 struct snd_soc_dai_link_component *component;
3736 int index;
3737
9cc69528
KM
3738 for_each_link_cpus(dai_link, index, component)
3739 __snd_soc_of_put_component(component);
900dedd7
MP
3740}
3741EXPORT_SYMBOL_GPL(snd_soc_of_put_dai_link_cpus);
3742
3743/*
3744 * snd_soc_of_get_dai_link_cpus - Parse a list of CPU DAIs in the devicetree
3745 * @dev: Card device
3746 * @of_node: Device node
3747 * @dai_link: DAI link
3748 *
3749 * Is analogous to snd_soc_of_get_dai_link_codecs but parses a list of CPU DAIs
3750 * instead.
3751 *
3752 * Returns 0 for success
3753 */
3754int snd_soc_of_get_dai_link_cpus(struct device *dev,
3755 struct device_node *of_node,
3756 struct snd_soc_dai_link *dai_link)
3757{
900dedd7 3758 struct snd_soc_dai_link_component *component;
9cc69528 3759 int index, ret;
900dedd7 3760
586fb264 3761 /* Count the number of CPUs */
9cc69528
KM
3762 ret = __snd_soc_of_get_dai_link_component_alloc(dev, of_node,
3763 &dai_link->cpus, &dai_link->num_cpus);
3764 if (ret < 0)
3765 return ret;
900dedd7
MP
3766
3767 /* Parse the list */
3768 for_each_link_cpus(dai_link, index, component) {
14c9b25f 3769 ret = snd_soc_of_get_dlc(of_node, NULL, component, index);
900dedd7
MP
3770 if (ret)
3771 goto err;
900dedd7
MP
3772 }
3773 return 0;
3774err:
586fb264 3775 snd_soc_of_put_dai_link_cpus(dai_link);
900dedd7
MP
3776 dai_link->cpus = NULL;
3777 dai_link->num_cpus = 0;
3778 return ret;
3779}
3780EXPORT_SYMBOL_GPL(snd_soc_of_get_dai_link_cpus);
3781
c9b3a40f 3782static int __init snd_soc_init(void)
db2a4165 3783{
6ec27c53
CZ
3784 int ret;
3785
6553bf06 3786 snd_soc_debugfs_init();
6ec27c53
CZ
3787 ret = snd_soc_util_init();
3788 if (ret)
3789 goto err_util_init;
fb257897 3790
6ec27c53
CZ
3791 ret = platform_driver_register(&soc_driver);
3792 if (ret)
3793 goto err_register;
3794 return 0;
3795
3796err_register:
3797 snd_soc_util_exit();
3798err_util_init:
3799 snd_soc_debugfs_exit();
3800 return ret;
db2a4165 3801}
4abe8e16 3802module_init(snd_soc_init);
db2a4165 3803
7d8c16a6 3804static void __exit snd_soc_exit(void)
db2a4165 3805{
fb257897 3806 snd_soc_util_exit();
6553bf06 3807 snd_soc_debugfs_exit();
fb257897 3808
3ff3f64b 3809 platform_driver_unregister(&soc_driver);
db2a4165 3810}
db2a4165
FM
3811module_exit(snd_soc_exit);
3812
3813/* Module information */
d331124d 3814MODULE_AUTHOR("Liam Girdwood, lrg@slimlogic.co.uk");
db2a4165
FM
3815MODULE_DESCRIPTION("ALSA SoC Core");
3816MODULE_LICENSE("GPL");
8b45a209 3817MODULE_ALIAS("platform:soc-audio");