ASoC: soc-dai: add snd_soc_dai_suspend()
[linux-2.6-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>
db2a4165 34#include <sound/core.h>
3028eb8c 35#include <sound/jack.h>
db2a4165
FM
36#include <sound/pcm.h>
37#include <sound/pcm_params.h>
38#include <sound/soc.h>
01d7584c 39#include <sound/soc-dpcm.h>
8a978234 40#include <sound/soc-topology.h>
db2a4165
FM
41#include <sound/initval.h>
42
a8b1d34f
MB
43#define CREATE_TRACE_POINTS
44#include <trace/events/asoc.h>
45
f0fba2ad
LG
46#define NAME_SIZE 32
47
384c89e2 48#ifdef CONFIG_DEBUG_FS
8a9dab1a
MB
49struct dentry *snd_soc_debugfs_root;
50EXPORT_SYMBOL_GPL(snd_soc_debugfs_root);
384c89e2
MB
51#endif
52
c5af3a2e 53static DEFINE_MUTEX(client_mutex);
030e79f6 54static LIST_HEAD(component_list);
e894efef 55static LIST_HEAD(unbind_card_list);
c5af3a2e 56
368dee94
KM
57#define for_each_component(component) \
58 list_for_each_entry(component, &component_list, list)
59
587c9844
KM
60/*
61 * This is used if driver don't need to have CPU/Codec/Platform
62 * dai_link. see soc.h
63 */
64struct snd_soc_dai_link_component null_dailink_component[0];
65EXPORT_SYMBOL_GPL(null_dailink_component);
66
db2a4165
FM
67/*
68 * This is a timeout to do a DAPM powerdown after a stream is closed().
69 * It can be used to eliminate pops between different playback streams, e.g.
70 * between two audio tracks.
71 */
72static int pmdown_time = 5000;
73module_param(pmdown_time, int, 0);
74MODULE_PARM_DESC(pmdown_time, "DAPM stream powerdown time (msecs)");
75
2c7b696a
MZ
76/*
77 * If a DMI filed contain strings in this blacklist (e.g.
78 * "Type2 - Board Manufacturer" or "Type1 - TBD by OEM"), it will be taken
98faf436
ML
79 * as invalid and dropped when setting the card long name from DMI info.
80 */
81static const char * const dmi_blacklist[] = {
82 "To be filled by OEM",
83 "TBD by OEM",
84 "Default String",
85 "Board Manufacturer",
86 "Board Vendor Name",
87 "Board Product Name",
88 NULL, /* terminator */
89};
90
dbe21408
MB
91static ssize_t pmdown_time_show(struct device *dev,
92 struct device_attribute *attr, char *buf)
93{
36ae1a96 94 struct snd_soc_pcm_runtime *rtd = dev_get_drvdata(dev);
dbe21408 95
f0fba2ad 96 return sprintf(buf, "%ld\n", rtd->pmdown_time);
dbe21408
MB
97}
98
99static ssize_t pmdown_time_set(struct device *dev,
100 struct device_attribute *attr,
101 const char *buf, size_t count)
102{
36ae1a96 103 struct snd_soc_pcm_runtime *rtd = dev_get_drvdata(dev);
c593b520 104 int ret;
dbe21408 105
b785a492 106 ret = kstrtol(buf, 10, &rtd->pmdown_time);
c593b520
MB
107 if (ret)
108 return ret;
dbe21408
MB
109
110 return count;
111}
112
113static DEVICE_ATTR(pmdown_time, 0644, pmdown_time_show, pmdown_time_set);
114
d29697dc 115static struct attribute *soc_dev_attrs[] = {
d29697dc
TI
116 &dev_attr_pmdown_time.attr,
117 NULL
118};
119
120static umode_t soc_dev_attr_is_visible(struct kobject *kobj,
121 struct attribute *attr, int idx)
122{
123 struct device *dev = kobj_to_dev(kobj);
124 struct snd_soc_pcm_runtime *rtd = dev_get_drvdata(dev);
125
126 if (attr == &dev_attr_pmdown_time.attr)
127 return attr->mode; /* always visible */
3b6eed8d 128 return rtd->num_codecs ? attr->mode : 0; /* enabled only with codec */
d29697dc
TI
129}
130
131static const struct attribute_group soc_dapm_dev_group = {
132 .attrs = soc_dapm_dev_attrs,
133 .is_visible = soc_dev_attr_is_visible,
134};
135
f7e73b26 136static const struct attribute_group soc_dev_group = {
d29697dc
TI
137 .attrs = soc_dev_attrs,
138 .is_visible = soc_dev_attr_is_visible,
139};
140
141static const struct attribute_group *soc_dev_attr_groups[] = {
142 &soc_dapm_dev_group,
f7e73b26 143 &soc_dev_group,
d29697dc
TI
144 NULL
145};
146
2624d5fa 147#ifdef CONFIG_DEBUG_FS
81c7cfd1 148static void soc_init_component_debugfs(struct snd_soc_component *component)
e73f3de5 149{
6553bf06
LPC
150 if (!component->card->debugfs_card_root)
151 return;
152
81c7cfd1
LPC
153 if (component->debugfs_prefix) {
154 char *name;
e73f3de5 155
81c7cfd1
LPC
156 name = kasprintf(GFP_KERNEL, "%s:%s",
157 component->debugfs_prefix, component->name);
158 if (name) {
159 component->debugfs_root = debugfs_create_dir(name,
160 component->card->debugfs_card_root);
161 kfree(name);
162 }
163 } else {
164 component->debugfs_root = debugfs_create_dir(component->name,
165 component->card->debugfs_card_root);
166 }
e73f3de5 167
c2c928c9 168 if (IS_ERR(component->debugfs_root)) {
81c7cfd1 169 dev_warn(component->dev,
c2c928c9
MB
170 "ASoC: Failed to create component debugfs directory: %ld\n",
171 PTR_ERR(component->debugfs_root));
81c7cfd1
LPC
172 return;
173 }
e73f3de5 174
81c7cfd1
LPC
175 snd_soc_dapm_debugfs_init(snd_soc_component_get_dapm(component),
176 component->debugfs_root);
e73f3de5
RK
177}
178
81c7cfd1 179static void soc_cleanup_component_debugfs(struct snd_soc_component *component)
2624d5fa 180{
81c7cfd1
LPC
181 debugfs_remove_recursive(component->debugfs_root);
182}
d6ce4cf3 183
c15b2a1d 184static int dai_list_show(struct seq_file *m, void *v)
f3208780 185{
1438c2f6 186 struct snd_soc_component *component;
f3208780
MB
187 struct snd_soc_dai *dai;
188
34e81ab4
LPC
189 mutex_lock(&client_mutex);
190
368dee94 191 for_each_component(component)
15a0c645 192 for_each_component_dais(component, dai)
700c17ca 193 seq_printf(m, "%s\n", dai->name);
f3208780 194
34e81ab4
LPC
195 mutex_unlock(&client_mutex);
196
700c17ca
DP
197 return 0;
198}
c15b2a1d 199DEFINE_SHOW_ATTRIBUTE(dai_list);
f3208780 200
db795f9b
KM
201static int component_list_show(struct seq_file *m, void *v)
202{
203 struct snd_soc_component *component;
204
205 mutex_lock(&client_mutex);
206
368dee94 207 for_each_component(component)
db795f9b
KM
208 seq_printf(m, "%s\n", component->name);
209
210 mutex_unlock(&client_mutex);
211
212 return 0;
213}
214DEFINE_SHOW_ATTRIBUTE(component_list);
215
a6052154
JN
216static void soc_init_card_debugfs(struct snd_soc_card *card)
217{
6553bf06
LPC
218 if (!snd_soc_debugfs_root)
219 return;
220
a6052154 221 card->debugfs_card_root = debugfs_create_dir(card->name,
8a9dab1a 222 snd_soc_debugfs_root);
c2c928c9 223 if (IS_ERR(card->debugfs_card_root)) {
a6052154 224 dev_warn(card->dev,
c2c928c9
MB
225 "ASoC: Failed to create card debugfs directory: %ld\n",
226 PTR_ERR(card->debugfs_card_root));
227 card->debugfs_card_root = NULL;
3a45b867
JN
228 return;
229 }
230
231 card->debugfs_pop_time = debugfs_create_u32("dapm_pop_time", 0644,
232 card->debugfs_card_root,
233 &card->pop_time);
c2c928c9 234 if (IS_ERR(card->debugfs_pop_time))
3a45b867 235 dev_warn(card->dev,
c2c928c9
MB
236 "ASoC: Failed to create pop time debugfs file: %ld\n",
237 PTR_ERR(card->debugfs_pop_time));
a6052154
JN
238}
239
240static void soc_cleanup_card_debugfs(struct snd_soc_card *card)
241{
29040d1a
KM
242 if (!card->debugfs_card_root)
243 return;
a6052154 244 debugfs_remove_recursive(card->debugfs_card_root);
29040d1a 245 card->debugfs_card_root = NULL;
a6052154
JN
246}
247
6553bf06
LPC
248static void snd_soc_debugfs_init(void)
249{
250 snd_soc_debugfs_root = debugfs_create_dir("asoc", NULL);
d9a02c55 251 if (IS_ERR_OR_NULL(snd_soc_debugfs_root)) {
6553bf06
LPC
252 pr_warn("ASoC: Failed to create debugfs directory\n");
253 snd_soc_debugfs_root = NULL;
254 return;
255 }
256
6553bf06
LPC
257 if (!debugfs_create_file("dais", 0444, snd_soc_debugfs_root, NULL,
258 &dai_list_fops))
259 pr_warn("ASoC: Failed to create DAI list debugfs file\n");
db795f9b
KM
260
261 if (!debugfs_create_file("components", 0444, snd_soc_debugfs_root, NULL,
262 &component_list_fops))
263 pr_warn("ASoC: Failed to create component list debugfs file\n");
6553bf06
LPC
264}
265
266static void snd_soc_debugfs_exit(void)
267{
268 debugfs_remove_recursive(snd_soc_debugfs_root);
269}
270
2624d5fa
MB
271#else
272
81c7cfd1
LPC
273static inline void soc_init_component_debugfs(
274 struct snd_soc_component *component)
731f1ab2
SG
275{
276}
277
81c7cfd1
LPC
278static inline void soc_cleanup_component_debugfs(
279 struct snd_soc_component *component)
731f1ab2
SG
280{
281}
282
b95fccbc
AL
283static inline void soc_init_card_debugfs(struct snd_soc_card *card)
284{
285}
286
287static inline void soc_cleanup_card_debugfs(struct snd_soc_card *card)
288{
289}
6553bf06
LPC
290
291static inline void snd_soc_debugfs_init(void)
292{
293}
294
295static inline void snd_soc_debugfs_exit(void)
296{
297}
298
2624d5fa
MB
299#endif
300
a0ac4411
KM
301static int snd_soc_rtdcom_add(struct snd_soc_pcm_runtime *rtd,
302 struct snd_soc_component *component)
303{
304 struct snd_soc_rtdcom_list *rtdcom;
305 struct snd_soc_rtdcom_list *new_rtdcom;
306
307 for_each_rtdcom(rtd, rtdcom) {
308 /* already connected */
309 if (rtdcom->component == component)
310 return 0;
311 }
312
313 new_rtdcom = kmalloc(sizeof(*new_rtdcom), GFP_KERNEL);
314 if (!new_rtdcom)
315 return -ENOMEM;
316
317 new_rtdcom->component = component;
318 INIT_LIST_HEAD(&new_rtdcom->list);
319
320 list_add_tail(&new_rtdcom->list, &rtd->component_list);
321
322 return 0;
323}
324
325static void snd_soc_rtdcom_del_all(struct snd_soc_pcm_runtime *rtd)
326{
327 struct snd_soc_rtdcom_list *rtdcom1, *rtdcom2;
328
329 for_each_rtdcom_safe(rtd, rtdcom1, rtdcom2)
330 kfree(rtdcom1);
331
332 INIT_LIST_HEAD(&rtd->component_list);
333}
334
335struct snd_soc_component *snd_soc_rtdcom_lookup(struct snd_soc_pcm_runtime *rtd,
336 const char *driver_name)
337{
338 struct snd_soc_rtdcom_list *rtdcom;
339
971da24c
KM
340 if (!driver_name)
341 return NULL;
342
a0ac4411 343 for_each_rtdcom(rtd, rtdcom) {
971da24c
KM
344 const char *component_name = rtdcom->component->driver->name;
345
346 if (!component_name)
347 continue;
348
349 if ((component_name == driver_name) ||
350 strcmp(component_name, driver_name) == 0)
a0ac4411
KM
351 return rtdcom->component;
352 }
353
354 return NULL;
355}
031734b7 356EXPORT_SYMBOL_GPL(snd_soc_rtdcom_lookup);
a0ac4411 357
47c88fff
LG
358struct snd_pcm_substream *snd_soc_get_dai_substream(struct snd_soc_card *card,
359 const char *dai_link, int stream)
360{
1a497983 361 struct snd_soc_pcm_runtime *rtd;
47c88fff 362
bcb1fd1f 363 for_each_card_rtds(card, rtd) {
1a497983
ML
364 if (rtd->dai_link->no_pcm &&
365 !strcmp(rtd->dai_link->name, dai_link))
366 return rtd->pcm->streams[stream].substream;
47c88fff 367 }
f110bfc7 368 dev_dbg(card->dev, "ASoC: failed to find dai link %s\n", dai_link);
47c88fff
LG
369 return NULL;
370}
371EXPORT_SYMBOL_GPL(snd_soc_get_dai_substream);
372
75ab9eb6
KM
373static const struct snd_soc_ops null_snd_soc_ops;
374
1a497983
ML
375static struct snd_soc_pcm_runtime *soc_new_pcm_runtime(
376 struct snd_soc_card *card, struct snd_soc_dai_link *dai_link)
377{
378 struct snd_soc_pcm_runtime *rtd;
379
380 rtd = kzalloc(sizeof(struct snd_soc_pcm_runtime), GFP_KERNEL);
381 if (!rtd)
382 return NULL;
383
a0ac4411 384 INIT_LIST_HEAD(&rtd->component_list);
1a497983
ML
385 rtd->card = card;
386 rtd->dai_link = dai_link;
75ab9eb6
KM
387 if (!rtd->dai_link->ops)
388 rtd->dai_link->ops = &null_snd_soc_ops;
389
6396bb22
KC
390 rtd->codec_dais = kcalloc(dai_link->num_codecs,
391 sizeof(struct snd_soc_dai *),
1a497983
ML
392 GFP_KERNEL);
393 if (!rtd->codec_dais) {
394 kfree(rtd);
395 return NULL;
396 }
397
398 return rtd;
399}
400
401static void soc_free_pcm_runtime(struct snd_soc_pcm_runtime *rtd)
402{
db1721f5 403 kfree(rtd->codec_dais);
a0ac4411 404 snd_soc_rtdcom_del_all(rtd);
1a497983
ML
405 kfree(rtd);
406}
407
408static void soc_add_pcm_runtime(struct snd_soc_card *card,
409 struct snd_soc_pcm_runtime *rtd)
410{
411 list_add_tail(&rtd->list, &card->rtd_list);
412 rtd->num = card->num_rtd;
413 card->num_rtd++;
414}
415
416static void soc_remove_pcm_runtimes(struct snd_soc_card *card)
417{
418 struct snd_soc_pcm_runtime *rtd, *_rtd;
419
bcb1fd1f 420 for_each_card_rtds_safe(card, rtd, _rtd) {
1a497983
ML
421 list_del(&rtd->list);
422 soc_free_pcm_runtime(rtd);
423 }
424
425 card->num_rtd = 0;
426}
427
47c88fff
LG
428struct snd_soc_pcm_runtime *snd_soc_get_pcm_runtime(struct snd_soc_card *card,
429 const char *dai_link)
430{
1a497983 431 struct snd_soc_pcm_runtime *rtd;
47c88fff 432
bcb1fd1f 433 for_each_card_rtds(card, rtd) {
1a497983
ML
434 if (!strcmp(rtd->dai_link->name, dai_link))
435 return rtd;
47c88fff 436 }
f110bfc7 437 dev_dbg(card->dev, "ASoC: failed to find rtd %s\n", dai_link);
47c88fff
LG
438 return NULL;
439}
440EXPORT_SYMBOL_GPL(snd_soc_get_pcm_runtime);
441
65462e44
KM
442static void snd_soc_flush_all_delayed_work(struct snd_soc_card *card)
443{
444 struct snd_soc_pcm_runtime *rtd;
445
446 for_each_card_rtds(card, rtd)
447 flush_delayed_work(&rtd->delayed_work);
448}
449
9d58a077
RF
450static void codec2codec_close_delayed_work(struct work_struct *work)
451{
2c7b696a
MZ
452 /*
453 * Currently nothing to do for c2c links
9d58a077
RF
454 * Since c2c links are internal nodes in the DAPM graph and
455 * don't interface with the outside world or application layer
456 * we don't have to do any special handling on close.
457 */
458}
459
6f8ab4ac 460#ifdef CONFIG_PM_SLEEP
db2a4165 461/* powers down audio subsystem for suspend */
6f8ab4ac 462int snd_soc_suspend(struct device *dev)
db2a4165 463{
6f8ab4ac 464 struct snd_soc_card *card = dev_get_drvdata(dev);
d9fc4063 465 struct snd_soc_component *component;
1a497983
ML
466 struct snd_soc_pcm_runtime *rtd;
467 int i;
db2a4165 468
c5599b87
LPC
469 /* If the card is not initialized yet there is nothing to do */
470 if (!card->instantiated)
e3509ff0
DM
471 return 0;
472
2c7b696a
MZ
473 /*
474 * Due to the resume being scheduled into a workqueue we could
475 * suspend before that's finished - wait for it to complete.
6ed25978 476 */
f0fba2ad 477 snd_power_wait(card->snd_card, SNDRV_CTL_POWER_D0);
6ed25978
AG
478
479 /* we're going to block userspace touching us until resume completes */
f0fba2ad 480 snd_power_change_state(card->snd_card, SNDRV_CTL_POWER_D3hot);
6ed25978 481
a00f90f9 482 /* mute any active DACs */
bcb1fd1f 483 for_each_card_rtds(card, rtd) {
0b7990e3 484 struct snd_soc_dai *dai;
3efab7dc 485
1a497983 486 if (rtd->dai_link->ignore_suspend)
3efab7dc
MB
487 continue;
488
0b7990e3 489 for_each_rtd_codec_dai(rtd, i, dai) {
88bd870f
BC
490 struct snd_soc_dai_driver *drv = dai->driver;
491
492 if (drv->ops->digital_mute && dai->playback_active)
493 drv->ops->digital_mute(dai, 1);
494 }
db2a4165
FM
495 }
496
4ccab3e7 497 /* suspend all pcms */
bcb1fd1f 498 for_each_card_rtds(card, rtd) {
1a497983 499 if (rtd->dai_link->ignore_suspend)
3efab7dc
MB
500 continue;
501
1a497983 502 snd_pcm_suspend_all(rtd->pcm);
3efab7dc 503 }
4ccab3e7 504
87506549 505 if (card->suspend_pre)
70b2ac12 506 card->suspend_pre(card);
db2a4165 507
bcb1fd1f 508 for_each_card_rtds(card, rtd) {
1a497983 509 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
3efab7dc 510
1a497983 511 if (rtd->dai_link->ignore_suspend)
3efab7dc
MB
512 continue;
513
e0f22622
KM
514 if (!cpu_dai->driver->bus_control)
515 snd_soc_dai_suspend(cpu_dai);
db2a4165
FM
516 }
517
37660b6d 518 /* close any waiting streams */
65462e44 519 snd_soc_flush_all_delayed_work(card);
db2a4165 520
bcb1fd1f 521 for_each_card_rtds(card, rtd) {
3efab7dc 522
1a497983 523 if (rtd->dai_link->ignore_suspend)
3efab7dc
MB
524 continue;
525
1a497983 526 snd_soc_dapm_stream_event(rtd,
7bd3a6f3 527 SNDRV_PCM_STREAM_PLAYBACK,
7bd3a6f3 528 SND_SOC_DAPM_STREAM_SUSPEND);
f0fba2ad 529
1a497983 530 snd_soc_dapm_stream_event(rtd,
7bd3a6f3 531 SNDRV_PCM_STREAM_CAPTURE,
7bd3a6f3 532 SND_SOC_DAPM_STREAM_SUSPEND);
db2a4165
FM
533 }
534
8be4da29
LPC
535 /* Recheck all endpoints too, their state is affected by suspend */
536 dapm_mark_endpoints_dirty(card);
e2d32ff6
MB
537 snd_soc_dapm_sync(&card->dapm);
538
9178feb4 539 /* suspend all COMPONENTs */
f70f18f7 540 for_each_card_components(card, component) {
2c7b696a
MZ
541 struct snd_soc_dapm_context *dapm =
542 snd_soc_component_get_dapm(component);
d9fc4063 543
2c7b696a
MZ
544 /*
545 * If there are paths active then the COMPONENT will be held
546 * with bias _ON and should not be suspended.
547 */
9178feb4 548 if (!component->suspended) {
4890140f 549 switch (snd_soc_dapm_get_bias_level(dapm)) {
f0fba2ad 550 case SND_SOC_BIAS_STANDBY:
125a25da 551 /*
9178feb4 552 * If the COMPONENT is capable of idle
125a25da
MB
553 * bias off then being in STANDBY
554 * means it's doing something,
555 * otherwise fall through.
556 */
4890140f 557 if (dapm->idle_bias_off) {
9178feb4 558 dev_dbg(component->dev,
10e8aa9a 559 "ASoC: idle_bias_off CODEC on over suspend\n");
125a25da
MB
560 break;
561 }
1a12d5dc 562 /* fall through */
a8093297 563
f0fba2ad 564 case SND_SOC_BIAS_OFF:
999f7f5a
KM
565 if (component->driver->suspend)
566 component->driver->suspend(component);
9178feb4
KM
567 component->suspended = 1;
568 if (component->regmap)
569 regcache_mark_dirty(component->regmap);
988e8cc4 570 /* deactivate pins to sleep state */
9178feb4 571 pinctrl_pm_select_sleep_state(component->dev);
f0fba2ad
LG
572 break;
573 default:
9178feb4
KM
574 dev_dbg(component->dev,
575 "ASoC: COMPONENT is on over suspend\n");
f0fba2ad
LG
576 break;
577 }
1547aba9
MB
578 }
579 }
db2a4165 580
bcb1fd1f 581 for_each_card_rtds(card, rtd) {
1a497983 582 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
3efab7dc 583
1a497983 584 if (rtd->dai_link->ignore_suspend)
3efab7dc
MB
585 continue;
586
e0f22622
KM
587 if (cpu_dai->driver->bus_control)
588 snd_soc_dai_suspend(cpu_dai);
988e8cc4
NC
589
590 /* deactivate pins to sleep state */
591 pinctrl_pm_select_sleep_state(cpu_dai->dev);
db2a4165
FM
592 }
593
87506549 594 if (card->suspend_post)
70b2ac12 595 card->suspend_post(card);
db2a4165
FM
596
597 return 0;
598}
6f8ab4ac 599EXPORT_SYMBOL_GPL(snd_soc_suspend);
db2a4165 600
2c7b696a
MZ
601/*
602 * deferred resume work, so resume can complete before we finished
6ed25978
AG
603 * setting our codec back up, which can be very slow on I2C
604 */
605static void soc_resume_deferred(struct work_struct *work)
db2a4165 606{
f0fba2ad 607 struct snd_soc_card *card =
2c7b696a
MZ
608 container_of(work, struct snd_soc_card,
609 deferred_resume_work);
1a497983 610 struct snd_soc_pcm_runtime *rtd;
d9fc4063 611 struct snd_soc_component *component;
1a497983 612 int i;
db2a4165 613
2c7b696a
MZ
614 /*
615 * our power state is still SNDRV_CTL_POWER_D3hot from suspend time,
6ed25978
AG
616 * so userspace apps are blocked from touching us
617 */
618
f110bfc7 619 dev_dbg(card->dev, "ASoC: starting resume work\n");
6ed25978 620
9949788b 621 /* Bring us up into D2 so that DAPM starts enabling things */
f0fba2ad 622 snd_power_change_state(card->snd_card, SNDRV_CTL_POWER_D2);
9949788b 623
87506549 624 if (card->resume_pre)
70b2ac12 625 card->resume_pre(card);
db2a4165 626
bc263214 627 /* resume control bus DAIs */
bcb1fd1f 628 for_each_card_rtds(card, rtd) {
1a497983 629 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
3efab7dc 630
1a497983 631 if (rtd->dai_link->ignore_suspend)
3efab7dc
MB
632 continue;
633
bc263214 634 if (cpu_dai->driver->resume && cpu_dai->driver->bus_control)
f0fba2ad
LG
635 cpu_dai->driver->resume(cpu_dai);
636 }
637
f70f18f7 638 for_each_card_components(card, component) {
9178feb4 639 if (component->suspended) {
999f7f5a
KM
640 if (component->driver->resume)
641 component->driver->resume(component);
9178feb4 642 component->suspended = 0;
1547aba9
MB
643 }
644 }
db2a4165 645
bcb1fd1f 646 for_each_card_rtds(card, rtd) {
3efab7dc 647
1a497983 648 if (rtd->dai_link->ignore_suspend)
3efab7dc
MB
649 continue;
650
1a497983 651 snd_soc_dapm_stream_event(rtd,
d9b0951b 652 SNDRV_PCM_STREAM_PLAYBACK,
7bd3a6f3 653 SND_SOC_DAPM_STREAM_RESUME);
f0fba2ad 654
1a497983 655 snd_soc_dapm_stream_event(rtd,
d9b0951b 656 SNDRV_PCM_STREAM_CAPTURE,
7bd3a6f3 657 SND_SOC_DAPM_STREAM_RESUME);
db2a4165
FM
658 }
659
3ff3f64b 660 /* unmute any active DACs */
bcb1fd1f 661 for_each_card_rtds(card, rtd) {
0b7990e3 662 struct snd_soc_dai *dai;
3efab7dc 663
1a497983 664 if (rtd->dai_link->ignore_suspend)
3efab7dc
MB
665 continue;
666
0b7990e3 667 for_each_rtd_codec_dai(rtd, i, dai) {
88bd870f
BC
668 struct snd_soc_dai_driver *drv = dai->driver;
669
670 if (drv->ops->digital_mute && dai->playback_active)
671 drv->ops->digital_mute(dai, 0);
672 }
db2a4165
FM
673 }
674
bcb1fd1f 675 for_each_card_rtds(card, rtd) {
1a497983 676 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
3efab7dc 677
1a497983 678 if (rtd->dai_link->ignore_suspend)
3efab7dc
MB
679 continue;
680
bc263214 681 if (cpu_dai->driver->resume && !cpu_dai->driver->bus_control)
f0fba2ad 682 cpu_dai->driver->resume(cpu_dai);
db2a4165
FM
683 }
684
87506549 685 if (card->resume_post)
70b2ac12 686 card->resume_post(card);
db2a4165 687
f110bfc7 688 dev_dbg(card->dev, "ASoC: resume work completed\n");
6ed25978 689
8be4da29
LPC
690 /* Recheck all endpoints too, their state is affected by suspend */
691 dapm_mark_endpoints_dirty(card);
e2d32ff6 692 snd_soc_dapm_sync(&card->dapm);
1a7aaa58
JK
693
694 /* userspace can access us now we are back as we were before */
695 snd_power_change_state(card->snd_card, SNDRV_CTL_POWER_D0);
6ed25978
AG
696}
697
698/* powers up audio subsystem after a suspend */
6f8ab4ac 699int snd_soc_resume(struct device *dev)
6ed25978 700{
6f8ab4ac 701 struct snd_soc_card *card = dev_get_drvdata(dev);
bc263214 702 bool bus_control = false;
1a497983 703 struct snd_soc_pcm_runtime *rtd;
22d251a5
KM
704 struct snd_soc_dai *codec_dai;
705 int i;
b9dd94a8 706
c5599b87
LPC
707 /* If the card is not initialized yet there is nothing to do */
708 if (!card->instantiated)
5ff1ddf2
EM
709 return 0;
710
988e8cc4 711 /* activate pins from sleep state */
bcb1fd1f 712 for_each_card_rtds(card, rtd) {
88bd870f 713 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
88bd870f 714
988e8cc4
NC
715 if (cpu_dai->active)
716 pinctrl_pm_select_default_state(cpu_dai->dev);
88bd870f 717
22d251a5 718 for_each_rtd_codec_dai(rtd, i, codec_dai) {
88bd870f
BC
719 if (codec_dai->active)
720 pinctrl_pm_select_default_state(codec_dai->dev);
721 }
988e8cc4
NC
722 }
723
bc263214
LPC
724 /*
725 * DAIs that also act as the control bus master might have other drivers
726 * hanging off them so need to resume immediately. Other drivers don't
727 * have that problem and may take a substantial amount of time to resume
64ab9baa
MB
728 * due to I/O costs and anti-pop so handle them out of line.
729 */
bcb1fd1f 730 for_each_card_rtds(card, rtd) {
1a497983 731 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
2c7b696a 732
bc263214 733 bus_control |= cpu_dai->driver->bus_control;
82e14e8b 734 }
bc263214
LPC
735 if (bus_control) {
736 dev_dbg(dev, "ASoC: Resuming control bus master immediately\n");
82e14e8b
SW
737 soc_resume_deferred(&card->deferred_resume_work);
738 } else {
f110bfc7 739 dev_dbg(dev, "ASoC: Scheduling resume work\n");
82e14e8b 740 if (!schedule_work(&card->deferred_resume_work))
f110bfc7 741 dev_err(dev, "ASoC: resume work item may be lost\n");
64ab9baa 742 }
6ed25978 743
db2a4165
FM
744 return 0;
745}
6f8ab4ac 746EXPORT_SYMBOL_GPL(snd_soc_resume);
db2a4165 747#else
6f8ab4ac
MB
748#define snd_soc_suspend NULL
749#define snd_soc_resume NULL
db2a4165
FM
750#endif
751
85e7652d 752static const struct snd_soc_dai_ops null_dai_ops = {
02a06d30
BS
753};
754
c0834440
KM
755static struct device_node
756*soc_component_to_node(struct snd_soc_component *component)
12023a9a 757{
c0834440 758 struct device_node *of_node;
12023a9a 759
c0834440
KM
760 of_node = component->dev->of_node;
761 if (!of_node && component->dev->parent)
762 of_node = component->dev->parent->of_node;
12023a9a 763
c0834440 764 return of_node;
12023a9a
MLC
765}
766
be6ac0a9
KM
767static int snd_soc_is_matching_component(
768 const struct snd_soc_dai_link_component *dlc,
769 struct snd_soc_component *component)
770{
771 struct device_node *component_of_node;
772
7d7db5d3
KM
773 if (!dlc)
774 return 0;
775
776 component_of_node = soc_component_to_node(component);
be6ac0a9
KM
777
778 if (dlc->of_node && component_of_node != dlc->of_node)
779 return 0;
780 if (dlc->name && strcmp(component->name, dlc->name))
781 return 0;
782
783 return 1;
784}
785
65d9361f 786static struct snd_soc_component *soc_find_component(
c1e230f0 787 const struct snd_soc_dai_link_component *dlc)
12023a9a 788{
65d9361f 789 struct snd_soc_component *component;
12023a9a 790
34e81ab4
LPC
791 lockdep_assert_held(&client_mutex);
792
e3303268
KM
793 /*
794 * NOTE
795 *
796 * It returns *1st* found component, but some driver
797 * has few components by same of_node/name
798 * ex)
799 * CPU component and generic DMAEngine component
800 */
c1e230f0
KM
801 for_each_component(component)
802 if (snd_soc_is_matching_component(dlc, component))
65d9361f 803 return component;
12023a9a
MLC
804
805 return NULL;
806}
807
fbb88b5c
ML
808/**
809 * snd_soc_find_dai - Find a registered DAI
810 *
4958471b 811 * @dlc: name of the DAI or the DAI driver and optional component info to match
fbb88b5c 812 *
ad61dd30 813 * This function will search all registered components and their DAIs to
fbb88b5c
ML
814 * find the DAI of the same name. The component's of_node and name
815 * should also match if being specified.
816 *
817 * Return: pointer of DAI, or NULL if not found.
818 */
305e9020 819struct snd_soc_dai *snd_soc_find_dai(
14621c7e 820 const struct snd_soc_dai_link_component *dlc)
12023a9a 821{
14621c7e
LPC
822 struct snd_soc_component *component;
823 struct snd_soc_dai *dai;
12023a9a 824
34e81ab4
LPC
825 lockdep_assert_held(&client_mutex);
826
2c7b696a 827 /* Find CPU DAI from registered DAIs */
368dee94 828 for_each_component(component) {
be6ac0a9 829 if (!snd_soc_is_matching_component(dlc, component))
14621c7e 830 continue;
15a0c645 831 for_each_component_dais(component, dai) {
4958471b 832 if (dlc->dai_name && strcmp(dai->name, dlc->dai_name)
6a6dafda
JC
833 && (!dai->driver->name
834 || strcmp(dai->driver->name, dlc->dai_name)))
12023a9a 835 continue;
12023a9a 836
14621c7e 837 return dai;
12023a9a
MLC
838 }
839 }
840
841 return NULL;
842}
305e9020 843EXPORT_SYMBOL_GPL(snd_soc_find_dai);
12023a9a 844
17fb1755
ML
845/**
846 * snd_soc_find_dai_link - Find a DAI link
847 *
848 * @card: soc card
849 * @id: DAI link ID to match
850 * @name: DAI link name to match, optional
8abab35f 851 * @stream_name: DAI link stream name to match, optional
17fb1755
ML
852 *
853 * This function will search all existing DAI links of the soc card to
854 * find the link of the same ID. Since DAI links may not have their
855 * unique ID, so name and stream name should also match if being
856 * specified.
857 *
858 * Return: pointer of DAI link, or NULL if not found.
859 */
860struct snd_soc_dai_link *snd_soc_find_dai_link(struct snd_soc_card *card,
861 int id, const char *name,
862 const char *stream_name)
863{
864 struct snd_soc_dai_link *link, *_link;
865
866 lockdep_assert_held(&client_mutex);
867
98061fdb 868 for_each_card_links_safe(card, link, _link) {
17fb1755
ML
869 if (link->id != id)
870 continue;
871
872 if (name && (!link->name || strcmp(name, link->name)))
873 continue;
874
875 if (stream_name && (!link->stream_name
876 || strcmp(stream_name, link->stream_name)))
877 continue;
878
879 return link;
880 }
881
882 return NULL;
883}
884EXPORT_SYMBOL_GPL(snd_soc_find_dai_link);
885
49a5ba1c
ML
886static bool soc_is_dai_link_bound(struct snd_soc_card *card,
887 struct snd_soc_dai_link *dai_link)
888{
889 struct snd_soc_pcm_runtime *rtd;
890
bcb1fd1f 891 for_each_card_rtds(card, rtd) {
49a5ba1c
ML
892 if (rtd->dai_link == dai_link)
893 return true;
894 }
895
896 return false;
897}
898
6f2f1ff0
ML
899static int soc_bind_dai_link(struct snd_soc_card *card,
900 struct snd_soc_dai_link *dai_link)
db2a4165 901{
1a497983 902 struct snd_soc_pcm_runtime *rtd;
34614739 903 struct snd_soc_dai_link_component *codec, *platform;
90be711e 904 struct snd_soc_component *component;
88bd870f 905 int i;
435c5e25 906
a655de80
LG
907 if (dai_link->ignore)
908 return 0;
909
6f2f1ff0 910 dev_dbg(card->dev, "ASoC: binding %s\n", dai_link->name);
435c5e25 911
49a5ba1c
ML
912 if (soc_is_dai_link_bound(card, dai_link)) {
913 dev_dbg(card->dev, "ASoC: dai link %s already bound\n",
914 dai_link->name);
915 return 0;
916 }
435c5e25 917
513cb311
SM
918 rtd = soc_new_pcm_runtime(card, dai_link);
919 if (!rtd)
920 return -ENOMEM;
921
08a5841e
KM
922 /* FIXME: we need multi CPU support in the future */
923 rtd->cpu_dai = snd_soc_find_dai(dai_link->cpus);
b19e6e7b 924 if (!rtd->cpu_dai) {
6b490879 925 dev_info(card->dev, "ASoC: CPU DAI %s not registered\n",
08a5841e 926 dai_link->cpus->dai_name);
1a497983 927 goto _err_defer;
f0fba2ad 928 }
90be711e 929 snd_soc_rtdcom_add(rtd, rtd->cpu_dai->component);
f0fba2ad 930
88bd870f 931 /* Find CODEC from registered CODECs */
e2b30edf 932 rtd->num_codecs = dai_link->num_codecs;
34614739
JB
933 for_each_link_codecs(dai_link, i, codec) {
934 rtd->codec_dais[i] = snd_soc_find_dai(codec);
0a2cfcd9 935 if (!rtd->codec_dais[i]) {
7c7e2d6a 936 dev_info(card->dev, "ASoC: CODEC DAI %s not registered\n",
34614739 937 codec->dai_name);
1a497983 938 goto _err_defer;
88bd870f 939 }
34614739 940
0a2cfcd9 941 snd_soc_rtdcom_add(rtd, rtd->codec_dais[i]->component);
12023a9a
MLC
942 }
943
88bd870f 944 /* Single codec links expect codec and codec_dai in runtime data */
0a2cfcd9 945 rtd->codec_dai = rtd->codec_dais[0];
88bd870f 946
e2b30edf 947 /* Find PLATFORM from registered PLATFORMs */
34614739
JB
948 for_each_link_platforms(dai_link, i, platform) {
949 for_each_component(component) {
950 if (!snd_soc_is_matching_component(platform, component))
951 continue;
90be711e 952
34614739
JB
953 snd_soc_rtdcom_add(rtd, component);
954 }
90be711e
KM
955 }
956
1a497983 957 soc_add_pcm_runtime(card, rtd);
b19e6e7b 958 return 0;
1a497983
ML
959
960_err_defer:
961 soc_free_pcm_runtime(rtd);
2c7b696a 962 return -EPROBE_DEFER;
f0fba2ad
LG
963}
964
22d14231
KM
965static void soc_cleanup_component(struct snd_soc_component *component)
966{
3bb936f5 967 snd_soc_component_set_jack(component, NULL, NULL);
22d14231
KM
968 list_del(&component->card_list);
969 snd_soc_dapm_free(snd_soc_component_get_dapm(component));
970 soc_cleanup_component_debugfs(component);
971 component->card = NULL;
b4ed6b51 972 if (!component->driver->module_get_upon_open)
b450b878 973 module_put(component->dev->driver->owner);
22d14231
KM
974}
975
f1d45cc3 976static void soc_remove_component(struct snd_soc_component *component)
d12cd198 977{
abd31b32 978 if (!component->card)
70090bbb 979 return;
d12cd198 980
999f7f5a
KM
981 if (component->driver->remove)
982 component->driver->remove(component);
589c3563 983
22d14231 984 soc_cleanup_component(component);
589c3563
JN
985}
986
e60cd14f 987static void soc_remove_dai(struct snd_soc_dai *dai, int order)
f0fba2ad 988{
f0fba2ad
LG
989 int err;
990
52abe6cc 991 if (!dai || !dai->probed || !dai->driver ||
2eda3cb1
KM
992 dai->driver->remove_order != order)
993 return;
994
995 if (dai->driver->remove) {
996 err = dai->driver->remove(dai);
997 if (err < 0)
998 dev_err(dai->dev,
999 "ASoC: failed to remove %s: %d\n",
1000 dai->name, err);
f0fba2ad 1001 }
2eda3cb1 1002 dai->probed = 0;
b0aa88af
MLC
1003}
1004
1a497983
ML
1005static void soc_remove_link_dais(struct snd_soc_card *card,
1006 struct snd_soc_pcm_runtime *rtd, int order)
b0aa88af 1007{
e60cd14f 1008 int i;
0b7990e3 1009 struct snd_soc_dai *codec_dai;
b0aa88af
MLC
1010
1011 /* unregister the rtd device */
1012 if (rtd->dev_registered) {
b0aa88af
MLC
1013 device_unregister(rtd->dev);
1014 rtd->dev_registered = 0;
1015 }
1016
1017 /* remove the CODEC DAI */
0b7990e3
KM
1018 for_each_rtd_codec_dai(rtd, i, codec_dai)
1019 soc_remove_dai(codec_dai, order);
6b05eda6 1020
e60cd14f 1021 soc_remove_dai(rtd->cpu_dai, order);
f0fba2ad 1022}
db2a4165 1023
1a497983
ML
1024static void soc_remove_link_components(struct snd_soc_card *card,
1025 struct snd_soc_pcm_runtime *rtd, int order)
62ae68fa 1026{
61aca564 1027 struct snd_soc_component *component;
90be711e 1028 struct snd_soc_rtdcom_list *rtdcom;
62ae68fa 1029
90be711e
KM
1030 for_each_rtdcom(rtd, rtdcom) {
1031 component = rtdcom->component;
62ae68fa 1032
70090bbb 1033 if (component->driver->remove_order == order)
61aca564 1034 soc_remove_component(component);
62ae68fa 1035 }
62ae68fa
SW
1036}
1037
0671fd8e
KM
1038static void soc_remove_dai_links(struct snd_soc_card *card)
1039{
1a497983
ML
1040 int order;
1041 struct snd_soc_pcm_runtime *rtd;
f8f80361 1042 struct snd_soc_dai_link *link, *_link;
0671fd8e 1043
1a1035a9 1044 for_each_comp_order(order) {
bcb1fd1f 1045 for_each_card_rtds(card, rtd)
1a497983 1046 soc_remove_link_dais(card, rtd, order);
62ae68fa
SW
1047 }
1048
1a1035a9 1049 for_each_comp_order(order) {
bcb1fd1f 1050 for_each_card_rtds(card, rtd)
1a497983 1051 soc_remove_link_components(card, rtd, order);
0168bf0d 1052 }
62ae68fa 1053
98061fdb 1054 for_each_card_links_safe(card, link, _link) {
f8f80361
ML
1055 if (link->dobj.type == SND_SOC_DOBJ_DAI_LINK)
1056 dev_warn(card->dev, "Topology forgot to remove link %s?\n",
1057 link->name);
1058
1059 list_del(&link->list);
f8f80361 1060 }
0671fd8e
KM
1061}
1062
923c5e61 1063static int soc_init_dai_link(struct snd_soc_card *card,
2c7b696a 1064 struct snd_soc_dai_link *link)
923c5e61 1065{
adb76b5b 1066 int i;
34614739 1067 struct snd_soc_dai_link_component *codec, *platform;
923c5e61 1068
3db769f1 1069 for_each_link_codecs(link, i, codec) {
923c5e61
ML
1070 /*
1071 * Codec must be specified by 1 of name or OF node,
1072 * not both or neither.
1073 */
34614739 1074 if (!!codec->name == !!codec->of_node) {
923c5e61
ML
1075 dev_err(card->dev, "ASoC: Neither/both codec name/of_node are set for %s\n",
1076 link->name);
1077 return -EINVAL;
1078 }
af18b13f 1079
923c5e61 1080 /* Codec DAI name must be specified */
3db769f1 1081 if (!codec->dai_name) {
923c5e61
ML
1082 dev_err(card->dev, "ASoC: codec_dai_name not set for %s\n",
1083 link->name);
1084 return -EINVAL;
1085 }
af18b13f
JB
1086
1087 /*
1088 * Defer card registration if codec component is not added to
1089 * component list.
1090 */
1091 if (!soc_find_component(codec))
1092 return -EPROBE_DEFER;
923c5e61
ML
1093 }
1094
34614739
JB
1095 for_each_link_platforms(link, i, platform) {
1096 /*
1097 * Platform may be specified by either name or OF node, but it
1098 * can be left unspecified, then no components will be inserted
1099 * in the rtdcom list
1100 */
1101 if (!!platform->name == !!platform->of_node) {
1d768989 1102 dev_err(card->dev,
34614739 1103 "ASoC: Neither/both platform name/of_node are set for %s\n",
1d768989
KM
1104 link->name);
1105 return -EINVAL;
1106 }
1107
1108 /*
34614739
JB
1109 * Defer card registration if platform component is not added to
1110 * component list.
1d768989 1111 */
34614739 1112 if (!soc_find_component(platform))
1d768989 1113 return -EPROBE_DEFER;
910fdcab
KM
1114 }
1115
08a5841e
KM
1116 /* FIXME */
1117 if (link->num_cpus > 1) {
923c5e61 1118 dev_err(card->dev,
08a5841e 1119 "ASoC: multi cpu is not yet supported %s\n",
923c5e61
ML
1120 link->name);
1121 return -EINVAL;
1122 }
8780cf11 1123
923c5e61
ML
1124 /*
1125 * CPU device may be specified by either name or OF node, but
1126 * can be left unspecified, and will be matched based on DAI
1127 * name alone..
1128 */
08a5841e 1129 if (link->cpus->name && link->cpus->of_node) {
923c5e61
ML
1130 dev_err(card->dev,
1131 "ASoC: Neither/both cpu name/of_node are set for %s\n",
1132 link->name);
1133 return -EINVAL;
1134 }
8780cf11
AP
1135
1136 /*
1137 * Defer card registartion if cpu dai component is not added to
1138 * component list.
1139 */
08a5841e 1140 if ((link->cpus->of_node || link->cpus->name) &&
c1e230f0 1141 !soc_find_component(link->cpus))
8780cf11
AP
1142 return -EPROBE_DEFER;
1143
923c5e61
ML
1144 /*
1145 * At least one of CPU DAI name or CPU device name/node must be
1146 * specified
1147 */
08a5841e
KM
1148 if (!link->cpus->dai_name &&
1149 !(link->cpus->name || link->cpus->of_node)) {
923c5e61
ML
1150 dev_err(card->dev,
1151 "ASoC: Neither cpu_dai_name nor cpu_name/of_node are set for %s\n",
1152 link->name);
1153 return -EINVAL;
1154 }
1155
1156 return 0;
0671fd8e
KM
1157}
1158
ef2e8175
KM
1159void snd_soc_disconnect_sync(struct device *dev)
1160{
2c7b696a
MZ
1161 struct snd_soc_component *component =
1162 snd_soc_lookup_component(dev, NULL);
ef2e8175
KM
1163
1164 if (!component || !component->card)
1165 return;
1166
1167 snd_card_disconnect_sync(component->card->snd_card);
1168}
df532185 1169EXPORT_SYMBOL_GPL(snd_soc_disconnect_sync);
ef2e8175 1170
f8f80361
ML
1171/**
1172 * snd_soc_add_dai_link - Add a DAI link dynamically
1173 * @card: The ASoC card to which the DAI link is added
1174 * @dai_link: The new DAI link to add
1175 *
1176 * This function adds a DAI link to the ASoC card's link list.
1177 *
1178 * Note: Topology can use this API to add DAI links when probing the
1179 * topology component. And machine drivers can still define static
1180 * DAI links in dai_link array.
1181 */
1182int snd_soc_add_dai_link(struct snd_soc_card *card,
1183 struct snd_soc_dai_link *dai_link)
1184{
1185 if (dai_link->dobj.type
1186 && dai_link->dobj.type != SND_SOC_DOBJ_DAI_LINK) {
1187 dev_err(card->dev, "Invalid dai link type %d\n",
1188 dai_link->dobj.type);
1189 return -EINVAL;
1190 }
1191
1192 lockdep_assert_held(&client_mutex);
2c7b696a
MZ
1193 /*
1194 * Notify the machine driver for extra initialization
d6f220ea
ML
1195 * on the link created by topology.
1196 */
1197 if (dai_link->dobj.type && card->add_dai_link)
1198 card->add_dai_link(card, dai_link);
1199
f8f80361 1200 list_add_tail(&dai_link->list, &card->dai_link_list);
f8f80361
ML
1201
1202 return 0;
1203}
1204EXPORT_SYMBOL_GPL(snd_soc_add_dai_link);
1205
1206/**
1207 * snd_soc_remove_dai_link - Remove a DAI link from the list
1208 * @card: The ASoC card that owns the link
1209 * @dai_link: The DAI link to remove
1210 *
1211 * This function removes a DAI link from the ASoC card's link list.
1212 *
1213 * For DAI links previously added by topology, topology should
1214 * remove them by using the dobj embedded in the link.
1215 */
1216void snd_soc_remove_dai_link(struct snd_soc_card *card,
1217 struct snd_soc_dai_link *dai_link)
1218{
1219 struct snd_soc_dai_link *link, *_link;
1220
1221 if (dai_link->dobj.type
1222 && dai_link->dobj.type != SND_SOC_DOBJ_DAI_LINK) {
1223 dev_err(card->dev, "Invalid dai link type %d\n",
1224 dai_link->dobj.type);
1225 return;
1226 }
1227
1228 lockdep_assert_held(&client_mutex);
2c7b696a
MZ
1229 /*
1230 * Notify the machine driver for extra destruction
d6f220ea
ML
1231 * on the link created by topology.
1232 */
1233 if (dai_link->dobj.type && card->remove_dai_link)
1234 card->remove_dai_link(card, dai_link);
1235
98061fdb 1236 for_each_card_links_safe(card, link, _link) {
f8f80361
ML
1237 if (link == dai_link) {
1238 list_del(&link->list);
f8f80361
ML
1239 return;
1240 }
1241 }
1242}
1243EXPORT_SYMBOL_GPL(snd_soc_remove_dai_link);
1244
aefba455
JB
1245static void soc_set_of_name_prefix(struct snd_soc_component *component)
1246{
c0834440 1247 struct device_node *component_of_node = soc_component_to_node(component);
aefba455
JB
1248 const char *str;
1249 int ret;
1250
aefba455
JB
1251 ret = of_property_read_string(component_of_node, "sound-name-prefix",
1252 &str);
1253 if (!ret)
1254 component->name_prefix = str;
1255}
1256
ead9b919 1257static void soc_set_name_prefix(struct snd_soc_card *card,
94f99c87 1258 struct snd_soc_component *component)
ead9b919
JN
1259{
1260 int i;
1261
aefba455 1262 for (i = 0; i < card->num_configs && card->codec_conf; i++) {
ff819b83 1263 struct snd_soc_codec_conf *map = &card->codec_conf[i];
c0834440 1264 struct device_node *component_of_node = soc_component_to_node(component);
b24c539b
CK
1265
1266 if (map->of_node && component_of_node != map->of_node)
3ca041ed 1267 continue;
94f99c87 1268 if (map->dev_name && strcmp(component->name, map->dev_name))
3ca041ed 1269 continue;
94f99c87 1270 component->name_prefix = map->name_prefix;
aefba455 1271 return;
ead9b919 1272 }
aefba455
JB
1273
1274 /*
1275 * If there is no configuration table or no match in the table,
1276 * check if a prefix is provided in the node
1277 */
1278 soc_set_of_name_prefix(component);
ead9b919
JN
1279}
1280
f1d45cc3
LPC
1281static int soc_probe_component(struct snd_soc_card *card,
1282 struct snd_soc_component *component)
589c3563 1283{
2c7b696a
MZ
1284 struct snd_soc_dapm_context *dapm =
1285 snd_soc_component_get_dapm(component);
888df395 1286 struct snd_soc_dai *dai;
f1d45cc3 1287 int ret;
589c3563 1288
1b7c1231 1289 if (!strcmp(component->name, "snd-soc-dummy"))
70090bbb 1290 return 0;
589c3563 1291
abd31b32 1292 if (component->card) {
1b7c1231
LPC
1293 if (component->card != card) {
1294 dev_err(component->dev,
1295 "Trying to bind component to card \"%s\" but is already bound to card \"%s\"\n",
1296 card->name, component->card->name);
1297 return -ENODEV;
1298 }
1299 return 0;
1300 }
589c3563 1301
b4ed6b51 1302 if (!component->driver->module_get_upon_open &&
b450b878 1303 !try_module_get(component->dev->driver->owner))
70d29331
JN
1304 return -ENODEV;
1305
f1d45cc3
LPC
1306 component->card = card;
1307 dapm->card = card;
22d14231
KM
1308 INIT_LIST_HEAD(&component->card_list);
1309 INIT_LIST_HEAD(&dapm->list);
f1d45cc3 1310 soc_set_name_prefix(card, component);
589c3563 1311
f1d45cc3 1312 soc_init_component_debugfs(component);
d5d1e0be 1313
688d0ebf
KM
1314 if (component->driver->dapm_widgets) {
1315 ret = snd_soc_dapm_new_controls(dapm,
1316 component->driver->dapm_widgets,
1317 component->driver->num_dapm_widgets);
b318ad50
NP
1318
1319 if (ret != 0) {
f1d45cc3 1320 dev_err(component->dev,
b318ad50
NP
1321 "Failed to create new controls %d\n", ret);
1322 goto err_probe;
1323 }
1324 }
77530150 1325
15a0c645 1326 for_each_component_dais(component, dai) {
0634814f 1327 ret = snd_soc_dapm_new_dai_widgets(dapm, dai);
261edc70 1328 if (ret != 0) {
0634814f 1329 dev_err(component->dev,
261edc70
NP
1330 "Failed to create DAI widgets %d\n", ret);
1331 goto err_probe;
1332 }
1333 }
888df395 1334
999f7f5a
KM
1335 if (component->driver->probe) {
1336 ret = component->driver->probe(component);
589c3563 1337 if (ret < 0) {
f1d45cc3
LPC
1338 dev_err(component->dev,
1339 "ASoC: failed to probe component %d\n", ret);
70d29331 1340 goto err_probe;
589c3563 1341 }
be09ad90 1342 }
2ffb0f58
KM
1343 WARN(dapm->idle_bias_off &&
1344 dapm->bias_level != SND_SOC_BIAS_OFF,
1345 "codec %s can not start from non-off bias with idle_bias_off==1\n",
1346 component->name);
be09ad90 1347
f2ed6b07
ML
1348 /* machine specific init */
1349 if (component->init) {
1350 ret = component->init(component);
1351 if (ret < 0) {
1352 dev_err(component->dev,
1353 "Failed to do machine specific init %d\n", ret);
1354 goto err_probe;
1355 }
1356 }
1357
b8972bf0
KM
1358 if (component->driver->controls)
1359 snd_soc_add_component_controls(component,
1360 component->driver->controls,
1361 component->driver->num_controls);
6969b2ba
KM
1362 if (component->driver->dapm_routes)
1363 snd_soc_dapm_add_routes(dapm,
1364 component->driver->dapm_routes,
1365 component->driver->num_dapm_routes);
3fec6b6d 1366
f1d45cc3 1367 list_add(&dapm->list, &card->dapm_list);
f70f18f7 1368 /* see for_each_card_components */
d9fc4063 1369 list_add(&component->card_list, &card->component_dev_list);
956245e9 1370
956245e9 1371err_probe:
22d14231
KM
1372 if (ret < 0)
1373 soc_cleanup_component(component);
956245e9
LG
1374
1375 return ret;
1376}
1377
36ae1a96
MB
1378static void rtd_release(struct device *dev)
1379{
1380 kfree(dev);
1381}
f0fba2ad 1382
5f3484ac
LPC
1383static int soc_post_component_init(struct snd_soc_pcm_runtime *rtd,
1384 const char *name)
503ae5e0 1385{
589c3563
JN
1386 int ret = 0;
1387
589c3563 1388 /* register the rtd device */
36ae1a96
MB
1389 rtd->dev = kzalloc(sizeof(struct device), GFP_KERNEL);
1390 if (!rtd->dev)
1391 return -ENOMEM;
1392 device_initialize(rtd->dev);
5f3484ac 1393 rtd->dev->parent = rtd->card->dev;
36ae1a96 1394 rtd->dev->release = rtd_release;
d29697dc 1395 rtd->dev->groups = soc_dev_attr_groups;
f294afed 1396 dev_set_name(rtd->dev, "%s", name);
36ae1a96 1397 dev_set_drvdata(rtd->dev, rtd);
b8c0dab9 1398 mutex_init(&rtd->pcm_mutex);
01d7584c
LG
1399 INIT_LIST_HEAD(&rtd->dpcm[SNDRV_PCM_STREAM_PLAYBACK].be_clients);
1400 INIT_LIST_HEAD(&rtd->dpcm[SNDRV_PCM_STREAM_CAPTURE].be_clients);
1401 INIT_LIST_HEAD(&rtd->dpcm[SNDRV_PCM_STREAM_PLAYBACK].fe_clients);
1402 INIT_LIST_HEAD(&rtd->dpcm[SNDRV_PCM_STREAM_CAPTURE].fe_clients);
36ae1a96 1403 ret = device_add(rtd->dev);
589c3563 1404 if (ret < 0) {
865df9cb
CL
1405 /* calling put_device() here to free the rtd->dev */
1406 put_device(rtd->dev);
5f3484ac 1407 dev_err(rtd->card->dev,
f110bfc7 1408 "ASoC: failed to register runtime device: %d\n", ret);
589c3563
JN
1409 return ret;
1410 }
1411 rtd->dev_registered = 1;
589c3563
JN
1412 return 0;
1413}
1414
1a497983 1415static int soc_probe_link_components(struct snd_soc_card *card,
2c7b696a 1416 struct snd_soc_pcm_runtime *rtd, int order)
62ae68fa 1417{
f1d45cc3 1418 struct snd_soc_component *component;
90be711e
KM
1419 struct snd_soc_rtdcom_list *rtdcom;
1420 int ret;
62ae68fa 1421
90be711e
KM
1422 for_each_rtdcom(rtd, rtdcom) {
1423 component = rtdcom->component;
62ae68fa 1424
70090bbb 1425 if (component->driver->probe_order == order) {
f1d45cc3 1426 ret = soc_probe_component(card, component);
88bd870f
BC
1427 if (ret < 0)
1428 return ret;
1429 }
62ae68fa
SW
1430 }
1431
62ae68fa
SW
1432 return 0;
1433}
1434
8e2be562 1435static int soc_probe_dai(struct snd_soc_dai *dai, int order)
b0aa88af 1436{
7a2ccad5
KM
1437 if (dai->probed ||
1438 dai->driver->probe_order != order)
1439 return 0;
b0aa88af 1440
7a2ccad5
KM
1441 if (dai->driver->probe) {
1442 int ret = dai->driver->probe(dai);
2c7b696a 1443
7a2ccad5
KM
1444 if (ret < 0) {
1445 dev_err(dai->dev, "ASoC: failed to probe DAI %s: %d\n",
1446 dai->name, ret);
1447 return ret;
b0aa88af 1448 }
b0aa88af
MLC
1449 }
1450
7a2ccad5
KM
1451 dai->probed = 1;
1452
b0aa88af
MLC
1453 return 0;
1454}
1455
25f7b701
AP
1456static int soc_link_dai_pcm_new(struct snd_soc_dai **dais, int num_dais,
1457 struct snd_soc_pcm_runtime *rtd)
1458{
1459 int i, ret = 0;
1460
1461 for (i = 0; i < num_dais; ++i) {
1462 struct snd_soc_dai_driver *drv = dais[i]->driver;
1463
de17f14e 1464 if (drv->pcm_new)
25f7b701
AP
1465 ret = drv->pcm_new(rtd, dais[i]);
1466 if (ret < 0) {
1467 dev_err(dais[i]->dev,
1468 "ASoC: Failed to bind %s with pcm device\n",
1469 dais[i]->name);
1470 return ret;
1471 }
1472 }
1473
1474 return 0;
1475}
1476
1a497983
ML
1477static int soc_probe_link_dais(struct snd_soc_card *card,
1478 struct snd_soc_pcm_runtime *rtd, int order)
f0fba2ad 1479{
1a497983 1480 struct snd_soc_dai_link *dai_link = rtd->dai_link;
c74184ed 1481 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
a655de80
LG
1482 struct snd_soc_rtdcom_list *rtdcom;
1483 struct snd_soc_component *component;
0b7990e3 1484 struct snd_soc_dai *codec_dai;
a655de80 1485 int i, ret, num;
f0fba2ad 1486
f110bfc7 1487 dev_dbg(card->dev, "ASoC: probe %s dai link %d late %d\n",
1a497983 1488 card->name, rtd->num, order);
f0fba2ad 1489
f0fba2ad
LG
1490 /* set default power off timeout */
1491 rtd->pmdown_time = pmdown_time;
1492
8e2be562
LPC
1493 ret = soc_probe_dai(cpu_dai, order);
1494 if (ret)
1495 return ret;
db2a4165 1496
f0fba2ad 1497 /* probe the CODEC DAI */
0b7990e3
KM
1498 for_each_rtd_codec_dai(rtd, i, codec_dai) {
1499 ret = soc_probe_dai(codec_dai, order);
88bd870f
BC
1500 if (ret)
1501 return ret;
1502 }
fe3e78e0 1503
0168bf0d
LG
1504 /* complete DAI probe during last probe */
1505 if (order != SND_SOC_COMP_ORDER_LAST)
1506 return 0;
1507
5f3484ac
LPC
1508 /* do machine specific initialization */
1509 if (dai_link->init) {
1510 ret = dai_link->init(rtd);
1511 if (ret < 0) {
1512 dev_err(card->dev, "ASoC: failed to init %s: %d\n",
1513 dai_link->name, ret);
1514 return ret;
1515 }
1516 }
1517
a5053a8e
KM
1518 if (dai_link->dai_fmt)
1519 snd_soc_runtime_set_dai_fmt(rtd, dai_link->dai_fmt);
1520
5f3484ac 1521 ret = soc_post_component_init(rtd, dai_link->name);
589c3563 1522 if (ret)
f0fba2ad 1523 return ret;
fe3e78e0 1524
5f3484ac
LPC
1525#ifdef CONFIG_DEBUG_FS
1526 /* add DPCM sysfs entries */
2e55b90a
LPC
1527 if (dai_link->dynamic)
1528 soc_dpcm_debugfs_add(rtd);
5f3484ac
LPC
1529#endif
1530
a655de80
LG
1531 num = rtd->num;
1532
1533 /*
1534 * most drivers will register their PCMs using DAI link ordering but
1535 * topology based drivers can use the DAI link id field to set PCM
1536 * device number and then use rtd + a base offset of the BEs.
1537 */
1538 for_each_rtdcom(rtd, rtdcom) {
1539 component = rtdcom->component;
1540
1541 if (!component->driver->use_dai_pcm_id)
1542 continue;
1543
1544 if (rtd->dai_link->no_pcm)
1545 num += component->driver->be_pcm_base;
1546 else
1547 num = rtd->dai_link->id;
1548 }
1549
6f0c4226 1550 if (cpu_dai->driver->compress_new) {
2c7b696a 1551 /* create compress_device" */
a655de80 1552 ret = cpu_dai->driver->compress_new(rtd, num);
c74184ed 1553 if (ret < 0) {
f110bfc7 1554 dev_err(card->dev, "ASoC: can't create compress %s\n",
1245b700 1555 dai_link->stream_name);
c74184ed
MB
1556 return ret;
1557 }
52293596
KM
1558 } else if (!dai_link->params) {
1559 /* create the pcm */
1560 ret = soc_new_pcm(rtd, num);
1561 if (ret < 0) {
1562 dev_err(card->dev, "ASoC: can't create pcm %s :%d\n",
1563 dai_link->stream_name, ret);
1564 return ret;
c74184ed 1565 }
52293596
KM
1566 ret = soc_link_dai_pcm_new(&cpu_dai, 1, rtd);
1567 if (ret < 0)
1568 return ret;
1569 ret = soc_link_dai_pcm_new(rtd->codec_dais,
1570 rtd->num_codecs, rtd);
1571 if (ret < 0)
1572 return ret;
1573 } else {
1574 INIT_DELAYED_WORK(&rtd->delayed_work,
1575 codec2codec_close_delayed_work);
f0fba2ad
LG
1576 }
1577
f0fba2ad
LG
1578 return 0;
1579}
1580
44c69bb1 1581static int soc_bind_aux_dev(struct snd_soc_card *card, int num)
3ca041ed
SR
1582{
1583 struct snd_soc_aux_dev *aux_dev = &card->aux_dev[num];
f2ed6b07 1584 struct snd_soc_component *component;
c1e230f0 1585 struct snd_soc_dai_link_component dlc;
f2ed6b07
ML
1586
1587 if (aux_dev->codec_of_node || aux_dev->codec_name) {
1588 /* codecs, usually analog devices */
c1e230f0
KM
1589 dlc.name = aux_dev->codec_name;
1590 dlc.of_node = aux_dev->codec_of_node;
1591 component = soc_find_component(&dlc);
f2ed6b07 1592 if (!component) {
c1e230f0
KM
1593 if (dlc.of_node)
1594 dlc.name = of_node_full_name(dlc.of_node);
f2ed6b07
ML
1595 goto err_defer;
1596 }
1597 } else if (aux_dev->name) {
1598 /* generic components */
c1e230f0
KM
1599 dlc.name = aux_dev->name;
1600 dlc.of_node = NULL;
1601 component = soc_find_component(&dlc);
f2ed6b07
ML
1602 if (!component)
1603 goto err_defer;
1604 } else {
1605 dev_err(card->dev, "ASoC: Invalid auxiliary device\n");
1606 return -EINVAL;
3ca041ed 1607 }
fb099cb7 1608
f2ed6b07 1609 component->init = aux_dev->init;
d2e3a135 1610 list_add(&component->card_aux_list, &card->aux_comp_list);
1a653aa4 1611
44c69bb1 1612 return 0;
f2ed6b07
ML
1613
1614err_defer:
c1e230f0 1615 dev_err(card->dev, "ASoC: %s not registered\n", dlc.name);
f2ed6b07 1616 return -EPROBE_DEFER;
b19e6e7b
MB
1617}
1618
f2ed6b07 1619static int soc_probe_aux_devices(struct snd_soc_card *card)
2eea392d 1620{
991454e1 1621 struct snd_soc_component *comp;
f2ed6b07 1622 int order;
44c69bb1 1623 int ret;
3ca041ed 1624
1a1035a9 1625 for_each_comp_order(order) {
991454e1 1626 list_for_each_entry(comp, &card->aux_comp_list, card_aux_list) {
f2ed6b07
ML
1627 if (comp->driver->probe_order == order) {
1628 ret = soc_probe_component(card, comp);
1629 if (ret < 0) {
1630 dev_err(card->dev,
1631 "ASoC: failed to probe aux component %s %d\n",
1632 comp->name, ret);
1633 return ret;
1634 }
1635 }
5f3484ac
LPC
1636 }
1637 }
2eea392d 1638
f2ed6b07 1639 return 0;
2eea392d
JN
1640}
1641
f2ed6b07 1642static void soc_remove_aux_devices(struct snd_soc_card *card)
2eea392d 1643{
f2ed6b07
ML
1644 struct snd_soc_component *comp, *_comp;
1645 int order;
2eea392d 1646
1a1035a9 1647 for_each_comp_order(order) {
f2ed6b07 1648 list_for_each_entry_safe(comp, _comp,
991454e1 1649 &card->aux_comp_list, card_aux_list) {
1a653aa4 1650
f2ed6b07
ML
1651 if (comp->driver->remove_order == order) {
1652 soc_remove_component(comp);
991454e1
KM
1653 /* remove it from the card's aux_comp_list */
1654 list_del(&comp->card_aux_list);
f2ed6b07
ML
1655 }
1656 }
2eea392d 1657 }
2eea392d
JN
1658}
1659
ce64c8b9
LPC
1660/**
1661 * snd_soc_runtime_set_dai_fmt() - Change DAI link format for a ASoC runtime
1662 * @rtd: The runtime for which the DAI link format should be changed
1663 * @dai_fmt: The new DAI link format
1664 *
1665 * This function updates the DAI link format for all DAIs connected to the DAI
1666 * link for the specified runtime.
1667 *
1668 * Note: For setups with a static format set the dai_fmt field in the
1669 * corresponding snd_dai_link struct instead of using this function.
1670 *
1671 * Returns 0 on success, otherwise a negative error code.
1672 */
1673int snd_soc_runtime_set_dai_fmt(struct snd_soc_pcm_runtime *rtd,
1674 unsigned int dai_fmt)
1675{
ce64c8b9 1676 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
0b7990e3 1677 struct snd_soc_dai *codec_dai;
ce64c8b9
LPC
1678 unsigned int i;
1679 int ret;
1680
0b7990e3 1681 for_each_rtd_codec_dai(rtd, i, codec_dai) {
ce64c8b9
LPC
1682 ret = snd_soc_dai_set_fmt(codec_dai, dai_fmt);
1683 if (ret != 0 && ret != -ENOTSUPP) {
1684 dev_warn(codec_dai->dev,
1685 "ASoC: Failed to set DAI format: %d\n", ret);
1686 return ret;
1687 }
1688 }
1689
2c7b696a
MZ
1690 /*
1691 * Flip the polarity for the "CPU" end of a CODEC<->CODEC link
1692 * the component which has non_legacy_dai_naming is Codec
1693 */
999f7f5a 1694 if (cpu_dai->component->driver->non_legacy_dai_naming) {
ce64c8b9
LPC
1695 unsigned int inv_dai_fmt;
1696
1697 inv_dai_fmt = dai_fmt & ~SND_SOC_DAIFMT_MASTER_MASK;
1698 switch (dai_fmt & SND_SOC_DAIFMT_MASTER_MASK) {
1699 case SND_SOC_DAIFMT_CBM_CFM:
1700 inv_dai_fmt |= SND_SOC_DAIFMT_CBS_CFS;
1701 break;
1702 case SND_SOC_DAIFMT_CBM_CFS:
1703 inv_dai_fmt |= SND_SOC_DAIFMT_CBS_CFM;
1704 break;
1705 case SND_SOC_DAIFMT_CBS_CFM:
1706 inv_dai_fmt |= SND_SOC_DAIFMT_CBM_CFS;
1707 break;
1708 case SND_SOC_DAIFMT_CBS_CFS:
1709 inv_dai_fmt |= SND_SOC_DAIFMT_CBM_CFM;
1710 break;
1711 }
1712
1713 dai_fmt = inv_dai_fmt;
1714 }
1715
1716 ret = snd_soc_dai_set_fmt(cpu_dai, dai_fmt);
1717 if (ret != 0 && ret != -ENOTSUPP) {
1718 dev_warn(cpu_dai->dev,
1719 "ASoC: Failed to set DAI format: %d\n", ret);
1720 return ret;
1721 }
1722
1723 return 0;
1724}
ddaca25a 1725EXPORT_SYMBOL_GPL(snd_soc_runtime_set_dai_fmt);
ce64c8b9 1726
1f5a4535 1727#ifdef CONFIG_DMI
2c7b696a
MZ
1728/*
1729 * Trim special characters, and replace '-' with '_' since '-' is used to
345233d7
LG
1730 * separate different DMI fields in the card long name. Only number and
1731 * alphabet characters and a few separator characters are kept.
1732 */
1733static void cleanup_dmi_name(char *name)
1734{
1735 int i, j = 0;
1736
1737 for (i = 0; name[i]; i++) {
1738 if (isalnum(name[i]) || (name[i] == '.')
1739 || (name[i] == '_'))
1740 name[j++] = name[i];
1741 else if (name[i] == '-')
1742 name[j++] = '_';
1743 }
1744
1745 name[j] = '\0';
1746}
1747
2c7b696a
MZ
1748/*
1749 * Check if a DMI field is valid, i.e. not containing any string
98faf436
ML
1750 * in the black list.
1751 */
1752static int is_dmi_valid(const char *field)
1753{
1754 int i = 0;
1755
1756 while (dmi_blacklist[i]) {
1757 if (strstr(field, dmi_blacklist[i]))
1758 return 0;
1759 i++;
46b5a4d2 1760 }
98faf436
ML
1761
1762 return 1;
1763}
1764
345233d7
LG
1765/**
1766 * snd_soc_set_dmi_name() - Register DMI names to card
1767 * @card: The card to register DMI names
1768 * @flavour: The flavour "differentiator" for the card amongst its peers.
1769 *
1770 * An Intel machine driver may be used by many different devices but are
1771 * difficult for userspace to differentiate, since machine drivers ususally
1772 * use their own name as the card short name and leave the card long name
1773 * blank. To differentiate such devices and fix bugs due to lack of
1774 * device-specific configurations, this function allows DMI info to be used
1775 * as the sound card long name, in the format of
1776 * "vendor-product-version-board"
1777 * (Character '-' is used to separate different DMI fields here).
1778 * This will help the user space to load the device-specific Use Case Manager
1779 * (UCM) configurations for the card.
1780 *
1781 * Possible card long names may be:
1782 * DellInc.-XPS139343-01-0310JH
1783 * ASUSTeKCOMPUTERINC.-T100TA-1.0-T100TA
1784 * Circuitco-MinnowboardMaxD0PLATFORM-D0-MinnowBoardMAX
1785 *
1786 * This function also supports flavoring the card longname to provide
1787 * the extra differentiation, like "vendor-product-version-board-flavor".
1788 *
1789 * We only keep number and alphabet characters and a few separator characters
1790 * in the card long name since UCM in the user space uses the card long names
1791 * as card configuration directory names and AudoConf cannot support special
1792 * charactors like SPACE.
1793 *
1794 * Returns 0 on success, otherwise a negative error code.
1795 */
1796int snd_soc_set_dmi_name(struct snd_soc_card *card, const char *flavour)
1797{
1798 const char *vendor, *product, *product_version, *board;
1799 size_t longname_buf_size = sizeof(card->snd_card->longname);
1800 size_t len;
1801
1802 if (card->long_name)
1803 return 0; /* long name already set by driver or from DMI */
1804
1805 /* make up dmi long name as: vendor.product.version.board */
1806 vendor = dmi_get_system_info(DMI_BOARD_VENDOR);
98faf436 1807 if (!vendor || !is_dmi_valid(vendor)) {
345233d7
LG
1808 dev_warn(card->dev, "ASoC: no DMI vendor name!\n");
1809 return 0;
1810 }
1811
1812 snprintf(card->dmi_longname, sizeof(card->snd_card->longname),
1813 "%s", vendor);
1814 cleanup_dmi_name(card->dmi_longname);
1815
1816 product = dmi_get_system_info(DMI_PRODUCT_NAME);
98faf436 1817 if (product && is_dmi_valid(product)) {
345233d7
LG
1818 len = strlen(card->dmi_longname);
1819 snprintf(card->dmi_longname + len,
1820 longname_buf_size - len,
1821 "-%s", product);
1822
1823 len++; /* skip the separator "-" */
1824 if (len < longname_buf_size)
1825 cleanup_dmi_name(card->dmi_longname + len);
1826
2c7b696a
MZ
1827 /*
1828 * some vendors like Lenovo may only put a self-explanatory
345233d7
LG
1829 * name in the product version field
1830 */
1831 product_version = dmi_get_system_info(DMI_PRODUCT_VERSION);
98faf436 1832 if (product_version && is_dmi_valid(product_version)) {
345233d7
LG
1833 len = strlen(card->dmi_longname);
1834 snprintf(card->dmi_longname + len,
1835 longname_buf_size - len,
1836 "-%s", product_version);
1837
1838 len++;
1839 if (len < longname_buf_size)
1840 cleanup_dmi_name(card->dmi_longname + len);
1841 }
1842 }
1843
1844 board = dmi_get_system_info(DMI_BOARD_NAME);
98faf436 1845 if (board && is_dmi_valid(board)) {
345233d7
LG
1846 len = strlen(card->dmi_longname);
1847 snprintf(card->dmi_longname + len,
1848 longname_buf_size - len,
1849 "-%s", board);
1850
1851 len++;
1852 if (len < longname_buf_size)
1853 cleanup_dmi_name(card->dmi_longname + len);
1854 } else if (!product) {
1855 /* fall back to using legacy name */
1856 dev_warn(card->dev, "ASoC: no DMI board/product name!\n");
1857 return 0;
1858 }
1859
1860 /* Add flavour to dmi long name */
1861 if (flavour) {
1862 len = strlen(card->dmi_longname);
1863 snprintf(card->dmi_longname + len,
1864 longname_buf_size - len,
1865 "-%s", flavour);
1866
1867 len++;
1868 if (len < longname_buf_size)
1869 cleanup_dmi_name(card->dmi_longname + len);
1870 }
1871
1872 /* set the card long name */
1873 card->long_name = card->dmi_longname;
1874
1875 return 0;
1876}
1877EXPORT_SYMBOL_GPL(snd_soc_set_dmi_name);
1f5a4535 1878#endif /* CONFIG_DMI */
345233d7 1879
a655de80
LG
1880static void soc_check_tplg_fes(struct snd_soc_card *card)
1881{
1882 struct snd_soc_component *component;
1883 const struct snd_soc_component_driver *comp_drv;
1884 struct snd_soc_dai_link *dai_link;
1885 int i;
1886
368dee94 1887 for_each_component(component) {
a655de80
LG
1888
1889 /* does this component override FEs ? */
1890 if (!component->driver->ignore_machine)
1891 continue;
1892
1893 /* for this machine ? */
e194098b
PLB
1894 if (!strcmp(component->driver->ignore_machine,
1895 card->dev->driver->name))
1896 goto match;
a655de80 1897 if (strcmp(component->driver->ignore_machine,
e194098b 1898 dev_name(card->dev)))
a655de80 1899 continue;
e194098b 1900match:
a655de80 1901 /* machine matches, so override the rtd data */
7fe072b4 1902 for_each_card_prelinks(card, i, dai_link) {
a655de80
LG
1903
1904 /* ignore this FE */
1905 if (dai_link->dynamic) {
1906 dai_link->ignore = true;
1907 continue;
1908 }
1909
1910 dev_info(card->dev, "info: override FE DAI link %s\n",
1911 card->dai_link[i].name);
1912
1913 /* override platform component */
adb76b5b 1914 if (!dai_link->platforms) {
daecf46e
KM
1915 dev_err(card->dev, "init platform error");
1916 continue;
1917 }
910fdcab 1918 dai_link->platforms->name = component->name;
a655de80
LG
1919
1920 /* convert non BE into BE */
1921 dai_link->no_pcm = 1;
1922
1923 /* override any BE fixups */
1924 dai_link->be_hw_params_fixup =
1925 component->driver->be_hw_params_fixup;
1926
2c7b696a
MZ
1927 /*
1928 * most BE links don't set stream name, so set it to
a655de80
LG
1929 * dai link name if it's NULL to help bind widgets.
1930 */
1931 if (!dai_link->stream_name)
1932 dai_link->stream_name = dai_link->name;
1933 }
1934
1935 /* Inform userspace we are using alternate topology */
1936 if (component->driver->topology_name_prefix) {
1937
2c7b696a 1938 /* topology shortname created? */
a655de80
LG
1939 if (!card->topology_shortname_created) {
1940 comp_drv = component->driver;
1941
1942 snprintf(card->topology_shortname, 32, "%s-%s",
1943 comp_drv->topology_name_prefix,
1944 card->name);
1945 card->topology_shortname_created = true;
1946 }
1947
1948 /* use topology shortname */
1949 card->name = card->topology_shortname;
1950 }
1951 }
1952}
1953
53e947a0
KM
1954static int soc_cleanup_card_resources(struct snd_soc_card *card)
1955{
1956 /* free the ALSA card at first; this syncs with pending operations */
29040d1a 1957 if (card->snd_card) {
53e947a0 1958 snd_card_free(card->snd_card);
29040d1a
KM
1959 card->snd_card = NULL;
1960 }
53e947a0
KM
1961
1962 /* remove and free each DAI */
1963 soc_remove_dai_links(card);
1964 soc_remove_pcm_runtimes(card);
1965
1966 /* remove auxiliary devices */
1967 soc_remove_aux_devices(card);
1968
1969 snd_soc_dapm_free(&card->dapm);
1970 soc_cleanup_card_debugfs(card);
1971
1972 /* remove the card */
1973 if (card->remove)
1974 card->remove(card);
1975
1976 return 0;
1977}
1978
b19e6e7b 1979static int snd_soc_instantiate_card(struct snd_soc_card *card)
f0fba2ad 1980{
1a497983 1981 struct snd_soc_pcm_runtime *rtd;
61b0088b 1982 struct snd_soc_dai_link *dai_link;
ce64c8b9 1983 int ret, i, order;
fe3e78e0 1984
34e81ab4 1985 mutex_lock(&client_mutex);
70fc5373
TBS
1986 for_each_card_prelinks(card, i, dai_link) {
1987 ret = soc_init_dai_link(card, dai_link);
1988 if (ret) {
70fc5373
TBS
1989 dev_err(card->dev, "ASoC: failed to init link %s: %d\n",
1990 dai_link->name, ret);
1991 mutex_unlock(&client_mutex);
1992 return ret;
1993 }
1994 }
01b9d99a 1995 mutex_lock_nested(&card->mutex, SND_SOC_CARD_CLASS_INIT);
dbe21408 1996
53e947a0
KM
1997 card->dapm.bias_level = SND_SOC_BIAS_OFF;
1998 card->dapm.dev = card->dev;
1999 card->dapm.card = card;
2000 list_add(&card->dapm.list, &card->dapm_list);
2001
a655de80
LG
2002 /* check whether any platform is ignore machine FE and using topology */
2003 soc_check_tplg_fes(card);
2004
f0fba2ad 2005 /* bind DAIs */
7fe072b4
KM
2006 for_each_card_prelinks(card, i, dai_link) {
2007 ret = soc_bind_dai_link(card, dai_link);
b19e6e7b 2008 if (ret != 0)
53e947a0 2009 goto probe_end;
b19e6e7b 2010 }
fe3e78e0 2011
44c69bb1 2012 /* bind aux_devs too */
b19e6e7b 2013 for (i = 0; i < card->num_aux_devs; i++) {
44c69bb1 2014 ret = soc_bind_aux_dev(card, i);
b19e6e7b 2015 if (ret != 0)
53e947a0 2016 goto probe_end;
f0fba2ad 2017 }
435c5e25 2018
f8f80361 2019 /* add predefined DAI links to the list */
7fe072b4
KM
2020 for_each_card_prelinks(card, i, dai_link)
2021 snd_soc_add_dai_link(card, dai_link);
f8f80361 2022
f0fba2ad 2023 /* card bind complete so register a sound card */
102b5a8d 2024 ret = snd_card_new(card->dev, SNDRV_DEFAULT_IDX1, SNDRV_DEFAULT_STR1,
f0fba2ad
LG
2025 card->owner, 0, &card->snd_card);
2026 if (ret < 0) {
10e8aa9a
MM
2027 dev_err(card->dev,
2028 "ASoC: can't create sound card for card %s: %d\n",
2029 card->name, ret);
53e947a0 2030 goto probe_end;
f0fba2ad 2031 }
f0fba2ad 2032
0757d834
LPC
2033 soc_init_card_debugfs(card);
2034
d5d1e0be
LPC
2035#ifdef CONFIG_DEBUG_FS
2036 snd_soc_dapm_debugfs_init(&card->dapm, card->debugfs_card_root);
2037#endif
2038
88ee1c61 2039#ifdef CONFIG_PM_SLEEP
f0fba2ad
LG
2040 /* deferred resume work */
2041 INIT_WORK(&card->deferred_resume_work, soc_resume_deferred);
2042#endif
db2a4165 2043
9a841ebb
MB
2044 if (card->dapm_widgets)
2045 snd_soc_dapm_new_controls(&card->dapm, card->dapm_widgets,
2046 card->num_dapm_widgets);
2047
f23e860e
NC
2048 if (card->of_dapm_widgets)
2049 snd_soc_dapm_new_controls(&card->dapm, card->of_dapm_widgets,
2050 card->num_of_dapm_widgets);
2051
f0fba2ad
LG
2052 /* initialise the sound card only once */
2053 if (card->probe) {
e7361ec4 2054 ret = card->probe(card);
f0fba2ad 2055 if (ret < 0)
53e947a0 2056 goto probe_end;
f0fba2ad 2057 }
fe3e78e0 2058
62ae68fa 2059 /* probe all components used by DAI links on this card */
1a1035a9 2060 for_each_comp_order(order) {
bcb1fd1f 2061 for_each_card_rtds(card, rtd) {
1a497983 2062 ret = soc_probe_link_components(card, rtd, order);
0168bf0d 2063 if (ret < 0) {
f110bfc7
LG
2064 dev_err(card->dev,
2065 "ASoC: failed to instantiate card %d\n",
2066 ret);
53e947a0 2067 goto probe_end;
62ae68fa
SW
2068 }
2069 }
2070 }
2071
f2ed6b07
ML
2072 /* probe auxiliary components */
2073 ret = soc_probe_aux_devices(card);
2074 if (ret < 0)
53e947a0 2075 goto probe_end;
f2ed6b07 2076
2c7b696a
MZ
2077 /*
2078 * Find new DAI links added during probing components and bind them.
61b0088b
ML
2079 * Components with topology may bring new DAIs and DAI links.
2080 */
98061fdb 2081 for_each_card_links(card, dai_link) {
61b0088b
ML
2082 if (soc_is_dai_link_bound(card, dai_link))
2083 continue;
2084
2085 ret = soc_init_dai_link(card, dai_link);
2086 if (ret)
53e947a0 2087 goto probe_end;
61b0088b
ML
2088 ret = soc_bind_dai_link(card, dai_link);
2089 if (ret)
53e947a0 2090 goto probe_end;
61b0088b
ML
2091 }
2092
62ae68fa 2093 /* probe all DAI links on this card */
1a1035a9 2094 for_each_comp_order(order) {
bcb1fd1f 2095 for_each_card_rtds(card, rtd) {
1a497983 2096 ret = soc_probe_link_dais(card, rtd, order);
62ae68fa 2097 if (ret < 0) {
f110bfc7
LG
2098 dev_err(card->dev,
2099 "ASoC: failed to instantiate card %d\n",
2100 ret);
53e947a0 2101 goto probe_end;
0168bf0d 2102 }
f0fba2ad
LG
2103 }
2104 }
db2a4165 2105
888df395 2106 snd_soc_dapm_link_dai_widgets(card);
b893ea5f 2107 snd_soc_dapm_connect_dai_link_widgets(card);
888df395 2108
b7af1daf 2109 if (card->controls)
2c7b696a
MZ
2110 snd_soc_add_card_controls(card, card->controls,
2111 card->num_controls);
b7af1daf 2112
b8ad29de
MB
2113 if (card->dapm_routes)
2114 snd_soc_dapm_add_routes(&card->dapm, card->dapm_routes,
2115 card->num_dapm_routes);
2116
f23e860e
NC
2117 if (card->of_dapm_routes)
2118 snd_soc_dapm_add_routes(&card->dapm, card->of_dapm_routes,
2119 card->num_of_dapm_routes);
75d9ac46 2120
861886d3
TI
2121 /* try to set some sane longname if DMI is available */
2122 snd_soc_set_dmi_name(card, NULL);
2123
f0fba2ad 2124 snprintf(card->snd_card->shortname, sizeof(card->snd_card->shortname),
f0fba2ad 2125 "%s", card->name);
22de71ba
LG
2126 snprintf(card->snd_card->longname, sizeof(card->snd_card->longname),
2127 "%s", card->long_name ? card->long_name : card->name);
f0e8ed85
MB
2128 snprintf(card->snd_card->driver, sizeof(card->snd_card->driver),
2129 "%s", card->driver_name ? card->driver_name : card->name);
2130 for (i = 0; i < ARRAY_SIZE(card->snd_card->driver); i++) {
2131 switch (card->snd_card->driver[i]) {
2132 case '_':
2133 case '-':
2134 case '\0':
2135 break;
2136 default:
2137 if (!isalnum(card->snd_card->driver[i]))
2138 card->snd_card->driver[i] = '_';
2139 break;
2140 }
2141 }
f0fba2ad 2142
28e9ad92
MB
2143 if (card->late_probe) {
2144 ret = card->late_probe(card);
2145 if (ret < 0) {
f110bfc7 2146 dev_err(card->dev, "ASoC: %s late_probe() failed: %d\n",
28e9ad92 2147 card->name, ret);
53e947a0 2148 goto probe_end;
28e9ad92
MB
2149 }
2150 }
2151
824ef826 2152 snd_soc_dapm_new_widgets(card);
8c193b8d 2153
f0fba2ad
LG
2154 ret = snd_card_register(card->snd_card);
2155 if (ret < 0) {
f110bfc7
LG
2156 dev_err(card->dev, "ASoC: failed to register soundcard %d\n",
2157 ret);
53e947a0 2158 goto probe_end;
db2a4165
FM
2159 }
2160
f0fba2ad 2161 card->instantiated = 1;
882eab6c 2162 dapm_mark_endpoints_dirty(card);
4f4c0072 2163 snd_soc_dapm_sync(&card->dapm);
f0fba2ad 2164
53e947a0
KM
2165probe_end:
2166 if (ret < 0)
2167 soc_cleanup_card_resources(card);
f0fba2ad
LG
2168
2169 mutex_unlock(&card->mutex);
34e81ab4 2170 mutex_unlock(&client_mutex);
db2a4165 2171
b19e6e7b 2172 return ret;
435c5e25
MB
2173}
2174
2175/* probes a new socdev */
2176static int soc_probe(struct platform_device *pdev)
2177{
f0fba2ad 2178 struct snd_soc_card *card = platform_get_drvdata(pdev);
435c5e25 2179
70a7ca34
VK
2180 /*
2181 * no card, so machine driver should be registering card
2182 * we should not be here in that case so ret error
2183 */
2184 if (!card)
2185 return -EINVAL;
2186
fe4085e8 2187 dev_warn(&pdev->dev,
f110bfc7 2188 "ASoC: machine %s should use snd_soc_register_card()\n",
fe4085e8
MB
2189 card->name);
2190
435c5e25
MB
2191 /* Bodge while we unpick instantiation */
2192 card->dev = &pdev->dev;
f0fba2ad 2193
28d528c8 2194 return snd_soc_register_card(card);
db2a4165
FM
2195}
2196
b0e26485
VK
2197/* removes a socdev */
2198static int soc_remove(struct platform_device *pdev)
2199{
2200 struct snd_soc_card *card = platform_get_drvdata(pdev);
db2a4165 2201
c5af3a2e 2202 snd_soc_unregister_card(card);
db2a4165
FM
2203 return 0;
2204}
2205
6f8ab4ac 2206int snd_soc_poweroff(struct device *dev)
51737470 2207{
6f8ab4ac 2208 struct snd_soc_card *card = dev_get_drvdata(dev);
1a497983 2209 struct snd_soc_pcm_runtime *rtd;
51737470
MB
2210
2211 if (!card->instantiated)
416356fc 2212 return 0;
51737470 2213
2c7b696a
MZ
2214 /*
2215 * Flush out pmdown_time work - we actually do want to run it
2216 * now, we're shutting down so no imminent restart.
2217 */
65462e44 2218 snd_soc_flush_all_delayed_work(card);
51737470 2219
f0fba2ad 2220 snd_soc_dapm_shutdown(card);
416356fc 2221
988e8cc4 2222 /* deactivate pins to sleep state */
bcb1fd1f 2223 for_each_card_rtds(card, rtd) {
88bd870f 2224 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
0b7990e3 2225 struct snd_soc_dai *codec_dai;
1a497983 2226 int i;
88bd870f 2227
988e8cc4 2228 pinctrl_pm_select_sleep_state(cpu_dai->dev);
0b7990e3 2229 for_each_rtd_codec_dai(rtd, i, codec_dai) {
88bd870f
BC
2230 pinctrl_pm_select_sleep_state(codec_dai->dev);
2231 }
988e8cc4
NC
2232 }
2233
416356fc 2234 return 0;
51737470 2235}
6f8ab4ac 2236EXPORT_SYMBOL_GPL(snd_soc_poweroff);
51737470 2237
6f8ab4ac 2238const struct dev_pm_ops snd_soc_pm_ops = {
b1dd5897
VK
2239 .suspend = snd_soc_suspend,
2240 .resume = snd_soc_resume,
2241 .freeze = snd_soc_suspend,
2242 .thaw = snd_soc_resume,
6f8ab4ac 2243 .poweroff = snd_soc_poweroff,
b1dd5897 2244 .restore = snd_soc_resume,
416356fc 2245};
deb2607e 2246EXPORT_SYMBOL_GPL(snd_soc_pm_ops);
416356fc 2247
db2a4165
FM
2248/* ASoC platform driver */
2249static struct platform_driver soc_driver = {
2250 .driver = {
2251 .name = "soc-audio",
6f8ab4ac 2252 .pm = &snd_soc_pm_ops,
db2a4165
FM
2253 },
2254 .probe = soc_probe,
2255 .remove = soc_remove,
db2a4165
FM
2256};
2257
db2a4165
FM
2258/**
2259 * snd_soc_cnew - create new control
2260 * @_template: control template
2261 * @data: control private data
ac11a2b3 2262 * @long_name: control long name
efb7ac3f 2263 * @prefix: control name prefix
db2a4165
FM
2264 *
2265 * Create a new mixer control from a template control.
2266 *
2267 * Returns 0 for success, else error.
2268 */
2269struct snd_kcontrol *snd_soc_cnew(const struct snd_kcontrol_new *_template,
3056557f 2270 void *data, const char *long_name,
efb7ac3f 2271 const char *prefix)
db2a4165
FM
2272{
2273 struct snd_kcontrol_new template;
efb7ac3f
MB
2274 struct snd_kcontrol *kcontrol;
2275 char *name = NULL;
db2a4165
FM
2276
2277 memcpy(&template, _template, sizeof(template));
db2a4165
FM
2278 template.index = 0;
2279
efb7ac3f
MB
2280 if (!long_name)
2281 long_name = template.name;
2282
2283 if (prefix) {
2b581074 2284 name = kasprintf(GFP_KERNEL, "%s %s", prefix, long_name);
efb7ac3f
MB
2285 if (!name)
2286 return NULL;
2287
efb7ac3f
MB
2288 template.name = name;
2289 } else {
2290 template.name = long_name;
2291 }
2292
2293 kcontrol = snd_ctl_new1(&template, data);
2294
2295 kfree(name);
2296
2297 return kcontrol;
db2a4165
FM
2298}
2299EXPORT_SYMBOL_GPL(snd_soc_cnew);
2300
022658be
LG
2301static int snd_soc_add_controls(struct snd_card *card, struct device *dev,
2302 const struct snd_kcontrol_new *controls, int num_controls,
2303 const char *prefix, void *data)
2304{
2305 int err, i;
2306
2307 for (i = 0; i < num_controls; i++) {
2308 const struct snd_kcontrol_new *control = &controls[i];
2c7b696a 2309
022658be
LG
2310 err = snd_ctl_add(card, snd_soc_cnew(control, data,
2311 control->name, prefix));
2312 if (err < 0) {
f110bfc7
LG
2313 dev_err(dev, "ASoC: Failed to add %s: %d\n",
2314 control->name, err);
022658be
LG
2315 return err;
2316 }
2317 }
2318
2319 return 0;
2320}
2321
4fefd698
DP
2322struct snd_kcontrol *snd_soc_card_get_kcontrol(struct snd_soc_card *soc_card,
2323 const char *name)
2324{
2325 struct snd_card *card = soc_card->snd_card;
2326 struct snd_kcontrol *kctl;
2327
2328 if (unlikely(!name))
2329 return NULL;
2330
2331 list_for_each_entry(kctl, &card->controls, list)
2332 if (!strncmp(kctl->id.name, name, sizeof(kctl->id.name)))
2333 return kctl;
2334 return NULL;
2335}
2336EXPORT_SYMBOL_GPL(snd_soc_card_get_kcontrol);
2337
0f2780ad
LPC
2338/**
2339 * snd_soc_add_component_controls - Add an array of controls to a component.
2340 *
2341 * @component: Component to add controls to
2342 * @controls: Array of controls to add
2343 * @num_controls: Number of elements in the array
2344 *
2345 * Return: 0 for success, else error.
2346 */
2347int snd_soc_add_component_controls(struct snd_soc_component *component,
2348 const struct snd_kcontrol_new *controls, unsigned int num_controls)
2349{
2350 struct snd_card *card = component->card->snd_card;
2351
2352 return snd_soc_add_controls(card, component->dev, controls,
2353 num_controls, component->name_prefix, component);
2354}
2355EXPORT_SYMBOL_GPL(snd_soc_add_component_controls);
2356
022658be
LG
2357/**
2358 * snd_soc_add_card_controls - add an array of controls to a SoC card.
2359 * Convenience function to add a list of controls.
2360 *
2361 * @soc_card: SoC card to add controls to
2362 * @controls: array of controls to add
2363 * @num_controls: number of elements in the array
2364 *
2365 * Return 0 for success, else error.
2366 */
2367int snd_soc_add_card_controls(struct snd_soc_card *soc_card,
2368 const struct snd_kcontrol_new *controls, int num_controls)
2369{
2370 struct snd_card *card = soc_card->snd_card;
2371
2372 return snd_soc_add_controls(card, soc_card->dev, controls, num_controls,
2373 NULL, soc_card);
2374}
2375EXPORT_SYMBOL_GPL(snd_soc_add_card_controls);
2376
2377/**
2378 * snd_soc_add_dai_controls - add an array of controls to a DAI.
2379 * Convienience function to add a list of controls.
2380 *
2381 * @dai: DAI to add controls to
2382 * @controls: array of controls to add
2383 * @num_controls: number of elements in the array
2384 *
2385 * Return 0 for success, else error.
2386 */
2387int snd_soc_add_dai_controls(struct snd_soc_dai *dai,
2388 const struct snd_kcontrol_new *controls, int num_controls)
2389{
313665b9 2390 struct snd_card *card = dai->component->card->snd_card;
022658be
LG
2391
2392 return snd_soc_add_controls(card, dai->dev, controls, num_controls,
2393 NULL, dai);
2394}
2395EXPORT_SYMBOL_GPL(snd_soc_add_dai_controls);
2396
71ccef0d
KM
2397/**
2398 * snd_soc_component_set_sysclk - configure COMPONENT system or master clock.
2399 * @component: COMPONENT
2400 * @clk_id: DAI specific clock ID
2401 * @source: Source for the clock
2402 * @freq: new clock frequency in Hz
2403 * @dir: new clock direction - input/output.
2404 *
2405 * Configures the CODEC master (MCLK) or system (SYSCLK) clocking.
2406 */
2c7b696a
MZ
2407int snd_soc_component_set_sysclk(struct snd_soc_component *component,
2408 int clk_id, int source, unsigned int freq,
2409 int dir)
71ccef0d 2410{
71ccef0d
KM
2411 if (component->driver->set_sysclk)
2412 return component->driver->set_sysclk(component, clk_id, source,
2413 freq, dir);
2414
2415 return -ENOTSUPP;
2416}
2417EXPORT_SYMBOL_GPL(snd_soc_component_set_sysclk);
2418
ef641e5d
KM
2419/*
2420 * snd_soc_component_set_pll - configure component PLL.
2421 * @component: COMPONENT
2422 * @pll_id: DAI specific PLL ID
2423 * @source: DAI specific source for the PLL
2424 * @freq_in: PLL input clock frequency in Hz
2425 * @freq_out: requested PLL output clock frequency in Hz
2426 *
2427 * Configures and enables PLL to generate output clock based on input clock.
2428 */
2429int snd_soc_component_set_pll(struct snd_soc_component *component, int pll_id,
2430 int source, unsigned int freq_in,
2431 unsigned int freq_out)
2432{
ef641e5d
KM
2433 if (component->driver->set_pll)
2434 return component->driver->set_pll(component, pll_id, source,
2c7b696a 2435 freq_in, freq_out);
ef641e5d
KM
2436
2437 return -EINVAL;
2438}
2439EXPORT_SYMBOL_GPL(snd_soc_component_set_pll);
2440
e894efef
SK
2441static int snd_soc_bind_card(struct snd_soc_card *card)
2442{
2443 struct snd_soc_pcm_runtime *rtd;
2444 int ret;
2445
2446 ret = snd_soc_instantiate_card(card);
2447 if (ret != 0)
2448 return ret;
2449
2450 /* deactivate pins to sleep state */
2c7b696a 2451 for_each_card_rtds(card, rtd) {
e894efef
SK
2452 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
2453 struct snd_soc_dai *codec_dai;
2454 int j;
2455
2456 for_each_rtd_codec_dai(rtd, j, codec_dai) {
2457 if (!codec_dai->active)
2458 pinctrl_pm_select_sleep_state(codec_dai->dev);
2459 }
2460
2461 if (!cpu_dai->active)
2462 pinctrl_pm_select_sleep_state(cpu_dai->dev);
2463 }
2464
2465 return ret;
2466}
2467
c5af3a2e
MB
2468/**
2469 * snd_soc_register_card - Register a card with the ASoC core
2470 *
ac11a2b3 2471 * @card: Card to register
c5af3a2e 2472 *
c5af3a2e 2473 */
70a7ca34 2474int snd_soc_register_card(struct snd_soc_card *card)
c5af3a2e
MB
2475{
2476 if (!card->name || !card->dev)
2477 return -EINVAL;
2478
ed77cc12
MB
2479 dev_set_drvdata(card->dev, card);
2480
111c6419
SW
2481 snd_soc_initialize_card_lists(card);
2482
f8f80361 2483 INIT_LIST_HEAD(&card->dai_link_list);
f8f80361 2484
1a497983
ML
2485 INIT_LIST_HEAD(&card->rtd_list);
2486 card->num_rtd = 0;
2487
db432b41 2488 INIT_LIST_HEAD(&card->dapm_dirty);
8a978234 2489 INIT_LIST_HEAD(&card->dobj_list);
c5af3a2e 2490 card->instantiated = 0;
f0fba2ad 2491 mutex_init(&card->mutex);
a73fb2df 2492 mutex_init(&card->dapm_mutex);
a9764869 2493 spin_lock_init(&card->dpcm_lock);
c5af3a2e 2494
e894efef
SK
2495 return snd_soc_bind_card(card);
2496}
2497EXPORT_SYMBOL_GPL(snd_soc_register_card);
88bd870f 2498
e894efef
SK
2499static void snd_soc_unbind_card(struct snd_soc_card *card, bool unregister)
2500{
f96fb7d1
RS
2501 struct snd_soc_pcm_runtime *rtd;
2502 int order;
2503
e894efef
SK
2504 if (card->instantiated) {
2505 card->instantiated = false;
2506 snd_soc_dapm_shutdown(card);
53e947a0 2507 snd_soc_flush_all_delayed_work(card);
f96fb7d1
RS
2508
2509 /* remove all components used by DAI links on this card */
2510 for_each_comp_order(order) {
2511 for_each_card_rtds(card, rtd) {
2512 soc_remove_link_components(card, rtd, order);
2513 }
2514 }
2515
e894efef
SK
2516 soc_cleanup_card_resources(card);
2517 if (!unregister)
2518 list_add(&card->list, &unbind_card_list);
2519 } else {
2520 if (unregister)
2521 list_del(&card->list);
988e8cc4 2522 }
c5af3a2e
MB
2523}
2524
2525/**
2526 * snd_soc_unregister_card - Unregister a card with the ASoC core
2527 *
ac11a2b3 2528 * @card: Card to unregister
c5af3a2e 2529 *
c5af3a2e 2530 */
70a7ca34 2531int snd_soc_unregister_card(struct snd_soc_card *card)
c5af3a2e 2532{
b545542a 2533 mutex_lock(&client_mutex);
e894efef 2534 snd_soc_unbind_card(card, true);
b545542a 2535 mutex_unlock(&client_mutex);
e894efef 2536 dev_dbg(card->dev, "ASoC: Unregistered card '%s'\n", card->name);
c5af3a2e
MB
2537
2538 return 0;
2539}
70a7ca34 2540EXPORT_SYMBOL_GPL(snd_soc_unregister_card);
c5af3a2e 2541
f0fba2ad
LG
2542/*
2543 * Simplify DAI link configuration by removing ".-1" from device names
2544 * and sanitizing names.
2545 */
0b9a214a 2546static char *fmt_single_name(struct device *dev, int *id)
f0fba2ad
LG
2547{
2548 char *found, name[NAME_SIZE];
2549 int id1, id2;
2550
2551 if (dev_name(dev) == NULL)
2552 return NULL;
2553
58818a77 2554 strlcpy(name, dev_name(dev), NAME_SIZE);
f0fba2ad
LG
2555
2556 /* are we a "%s.%d" name (platform and SPI components) */
2557 found = strstr(name, dev->driver->name);
2558 if (found) {
2559 /* get ID */
2560 if (sscanf(&found[strlen(dev->driver->name)], ".%d", id) == 1) {
2561
2562 /* discard ID from name if ID == -1 */
2563 if (*id == -1)
2564 found[strlen(dev->driver->name)] = '\0';
2565 }
2566
2567 } else {
2c7b696a 2568 /* I2C component devices are named "bus-addr" */
f0fba2ad
LG
2569 if (sscanf(name, "%x-%x", &id1, &id2) == 2) {
2570 char tmp[NAME_SIZE];
2571
2572 /* create unique ID number from I2C addr and bus */
05899446 2573 *id = ((id1 & 0xffff) << 16) + id2;
f0fba2ad
LG
2574
2575 /* sanitize component name for DAI link creation */
2c7b696a
MZ
2576 snprintf(tmp, NAME_SIZE, "%s.%s", dev->driver->name,
2577 name);
58818a77 2578 strlcpy(name, tmp, NAME_SIZE);
f0fba2ad
LG
2579 } else
2580 *id = 0;
2581 }
2582
2583 return kstrdup(name, GFP_KERNEL);
2584}
2585
2586/*
2587 * Simplify DAI link naming for single devices with multiple DAIs by removing
2588 * any ".-1" and using the DAI name (instead of device name).
2589 */
2590static inline char *fmt_multiple_name(struct device *dev,
2591 struct snd_soc_dai_driver *dai_drv)
2592{
2593 if (dai_drv->name == NULL) {
10e8aa9a
MM
2594 dev_err(dev,
2595 "ASoC: error - multiple DAI %s registered with no name\n",
2596 dev_name(dev));
f0fba2ad
LG
2597 return NULL;
2598 }
2599
2600 return kstrdup(dai_drv->name, GFP_KERNEL);
2601}
2602
9115171a 2603/**
32c9ba54 2604 * snd_soc_unregister_dai - Unregister DAIs from the ASoC core
9115171a 2605 *
32c9ba54 2606 * @component: The component for which the DAIs should be unregistered
9115171a 2607 */
32c9ba54 2608static void snd_soc_unregister_dais(struct snd_soc_component *component)
9115171a 2609{
5c1d5f09 2610 struct snd_soc_dai *dai, *_dai;
9115171a 2611
15a0c645 2612 for_each_component_dais_safe(component, dai, _dai) {
32c9ba54
LPC
2613 dev_dbg(component->dev, "ASoC: Unregistered DAI '%s'\n",
2614 dai->name);
5c1d5f09 2615 list_del(&dai->list);
32c9ba54 2616 kfree(dai->name);
f0fba2ad 2617 kfree(dai);
f0fba2ad 2618 }
9115171a 2619}
9115171a 2620
5e4fb372
ML
2621/* Create a DAI and add it to the component's DAI list */
2622static struct snd_soc_dai *soc_add_dai(struct snd_soc_component *component,
2623 struct snd_soc_dai_driver *dai_drv,
2624 bool legacy_dai_naming)
2625{
2626 struct device *dev = component->dev;
2627 struct snd_soc_dai *dai;
2628
2629 dev_dbg(dev, "ASoC: dynamically register DAI %s\n", dev_name(dev));
2630
2631 dai = kzalloc(sizeof(struct snd_soc_dai), GFP_KERNEL);
2632 if (dai == NULL)
2633 return NULL;
2634
2635 /*
2636 * Back in the old days when we still had component-less DAIs,
2637 * instead of having a static name, component-less DAIs would
2638 * inherit the name of the parent device so it is possible to
2639 * register multiple instances of the DAI. We still need to keep
2640 * the same naming style even though those DAIs are not
2641 * component-less anymore.
2642 */
2643 if (legacy_dai_naming &&
2c7b696a 2644 (dai_drv->id == 0 || dai_drv->name == NULL)) {
5e4fb372
ML
2645 dai->name = fmt_single_name(dev, &dai->id);
2646 } else {
2647 dai->name = fmt_multiple_name(dev, dai_drv);
2648 if (dai_drv->id)
2649 dai->id = dai_drv->id;
2650 else
2651 dai->id = component->num_dai;
2652 }
2653 if (dai->name == NULL) {
2654 kfree(dai);
2655 return NULL;
2656 }
2657
2658 dai->component = component;
2659 dai->dev = dev;
2660 dai->driver = dai_drv;
2661 if (!dai->driver->ops)
2662 dai->driver->ops = &null_dai_ops;
2663
15a0c645 2664 /* see for_each_component_dais */
58bf4179 2665 list_add_tail(&dai->list, &component->dai_list);
5e4fb372
ML
2666 component->num_dai++;
2667
2668 dev_dbg(dev, "ASoC: Registered DAI '%s'\n", dai->name);
2669 return dai;
2670}
2671
9115171a 2672/**
32c9ba54 2673 * snd_soc_register_dais - Register a DAI with the ASoC core
9115171a 2674 *
6106d129
LPC
2675 * @component: The component the DAIs are registered for
2676 * @dai_drv: DAI driver to use for the DAIs
ac11a2b3 2677 * @count: Number of DAIs
9115171a 2678 */
6106d129 2679static int snd_soc_register_dais(struct snd_soc_component *component,
2c7b696a
MZ
2680 struct snd_soc_dai_driver *dai_drv,
2681 size_t count)
9115171a 2682{
6106d129 2683 struct device *dev = component->dev;
f0fba2ad 2684 struct snd_soc_dai *dai;
32c9ba54
LPC
2685 unsigned int i;
2686 int ret;
f0fba2ad 2687
5b5e0928 2688 dev_dbg(dev, "ASoC: dai register %s #%zu\n", dev_name(dev), count);
9115171a
MB
2689
2690 for (i = 0; i < count; i++) {
f0fba2ad 2691
2c7b696a
MZ
2692 dai = soc_add_dai(component, dai_drv + i, count == 1 &&
2693 !component->driver->non_legacy_dai_naming);
c46e0079
AL
2694 if (dai == NULL) {
2695 ret = -ENOMEM;
2696 goto err;
2697 }
9115171a 2698 }
f0fba2ad 2699
9115171a 2700 return 0;
f0fba2ad 2701
9115171a 2702err:
32c9ba54 2703 snd_soc_unregister_dais(component);
f0fba2ad 2704
9115171a
MB
2705 return ret;
2706}
9115171a 2707
68003e6c
ML
2708/**
2709 * snd_soc_register_dai - Register a DAI dynamically & create its widgets
2710 *
2711 * @component: The component the DAIs are registered for
2712 * @dai_drv: DAI driver to use for the DAI
2713 *
2714 * Topology can use this API to register DAIs when probing a component.
2715 * These DAIs's widgets will be freed in the card cleanup and the DAIs
2716 * will be freed in the component cleanup.
2717 */
2718int snd_soc_register_dai(struct snd_soc_component *component,
2719 struct snd_soc_dai_driver *dai_drv)
2720{
2721 struct snd_soc_dapm_context *dapm =
2722 snd_soc_component_get_dapm(component);
2723 struct snd_soc_dai *dai;
2724 int ret;
054880fe 2725
68003e6c
ML
2726 if (dai_drv->dobj.type != SND_SOC_DOBJ_PCM) {
2727 dev_err(component->dev, "Invalid dai type %d\n",
2728 dai_drv->dobj.type);
2729 return -EINVAL;
9115171a
MB
2730 }
2731
68003e6c
ML
2732 lockdep_assert_held(&client_mutex);
2733 dai = soc_add_dai(component, dai_drv, false);
2734 if (!dai)
2735 return -ENOMEM;
9115171a 2736
2c7b696a
MZ
2737 /*
2738 * Create the DAI widgets here. After adding DAIs, topology may
68003e6c
ML
2739 * also add routes that need these widgets as source or sink.
2740 */
2741 ret = snd_soc_dapm_new_dai_widgets(dapm, dai);
2742 if (ret != 0) {
2743 dev_err(component->dev,
2744 "Failed to create DAI widgets %d\n", ret);
2745 }
9115171a
MB
2746
2747 return ret;
2748}
68003e6c 2749EXPORT_SYMBOL_GPL(snd_soc_register_dai);
9115171a 2750
14e8bdeb
LPC
2751static void snd_soc_component_seq_notifier(struct snd_soc_dapm_context *dapm,
2752 enum snd_soc_dapm_type type, int subseq)
d191bd8d 2753{
14e8bdeb 2754 struct snd_soc_component *component = dapm->component;
d191bd8d 2755
14e8bdeb
LPC
2756 component->driver->seq_notifier(component, type, subseq);
2757}
d191bd8d 2758
14e8bdeb
LPC
2759static int snd_soc_component_stream_event(struct snd_soc_dapm_context *dapm,
2760 int event)
2761{
2762 struct snd_soc_component *component = dapm->component;
d191bd8d 2763
14e8bdeb
LPC
2764 return component->driver->stream_event(component, event);
2765}
e2c330b9 2766
7ba236ce
KM
2767static int snd_soc_component_set_bias_level(struct snd_soc_dapm_context *dapm,
2768 enum snd_soc_bias_level level)
2769{
2770 struct snd_soc_component *component = dapm->component;
2771
2772 return component->driver->set_bias_level(component, level);
2773}
2774
bb13109d
LPC
2775static int snd_soc_component_initialize(struct snd_soc_component *component,
2776 const struct snd_soc_component_driver *driver, struct device *dev)
d191bd8d 2777{
ce0fc93a
LPC
2778 struct snd_soc_dapm_context *dapm;
2779
bb13109d
LPC
2780 component->name = fmt_single_name(dev, &component->id);
2781 if (!component->name) {
2782 dev_err(dev, "ASoC: Failed to allocate name\n");
d191bd8d
KM
2783 return -ENOMEM;
2784 }
2785
bb13109d
LPC
2786 component->dev = dev;
2787 component->driver = driver;
d191bd8d 2788
88c27465 2789 dapm = snd_soc_component_get_dapm(component);
ce0fc93a
LPC
2790 dapm->dev = dev;
2791 dapm->component = component;
2792 dapm->bias_level = SND_SOC_BIAS_OFF;
7ba236ce
KM
2793 dapm->idle_bias_off = !driver->idle_bias_on;
2794 dapm->suspend_bias_off = driver->suspend_bias_off;
14e8bdeb
LPC
2795 if (driver->seq_notifier)
2796 dapm->seq_notifier = snd_soc_component_seq_notifier;
2797 if (driver->stream_event)
2798 dapm->stream_event = snd_soc_component_stream_event;
7ba236ce
KM
2799 if (driver->set_bias_level)
2800 dapm->set_bias_level = snd_soc_component_set_bias_level;
d191bd8d 2801
bb13109d
LPC
2802 INIT_LIST_HEAD(&component->dai_list);
2803 mutex_init(&component->io_mutex);
d191bd8d 2804
bb13109d
LPC
2805 return 0;
2806}
d191bd8d 2807
20feb881 2808static void snd_soc_component_setup_regmap(struct snd_soc_component *component)
886f5692 2809{
20feb881
LPC
2810 int val_bytes = regmap_get_val_bytes(component->regmap);
2811
2812 /* Errors are legitimate for non-integer byte multiples */
2813 if (val_bytes > 0)
2814 component->val_bytes = val_bytes;
2815}
2816
e874bf5f
LPC
2817#ifdef CONFIG_REGMAP
2818
20feb881 2819/**
2c7b696a
MZ
2820 * snd_soc_component_init_regmap() - Initialize regmap instance for the
2821 * component
20feb881
LPC
2822 * @component: The component for which to initialize the regmap instance
2823 * @regmap: The regmap instance that should be used by the component
2824 *
2825 * This function allows deferred assignment of the regmap instance that is
2826 * associated with the component. Only use this if the regmap instance is not
2827 * yet ready when the component is registered. The function must also be called
2828 * before the first IO attempt of the component.
2829 */
2830void snd_soc_component_init_regmap(struct snd_soc_component *component,
2831 struct regmap *regmap)
2832{
2833 component->regmap = regmap;
2834 snd_soc_component_setup_regmap(component);
886f5692 2835}
20feb881
LPC
2836EXPORT_SYMBOL_GPL(snd_soc_component_init_regmap);
2837
2838/**
2c7b696a
MZ
2839 * snd_soc_component_exit_regmap() - De-initialize regmap instance for the
2840 * component
20feb881
LPC
2841 * @component: The component for which to de-initialize the regmap instance
2842 *
2843 * Calls regmap_exit() on the regmap instance associated to the component and
2844 * removes the regmap instance from the component.
2845 *
2846 * This function should only be used if snd_soc_component_init_regmap() was used
2847 * to initialize the regmap instance.
2848 */
2849void snd_soc_component_exit_regmap(struct snd_soc_component *component)
2850{
2851 regmap_exit(component->regmap);
2852 component->regmap = NULL;
2853}
2854EXPORT_SYMBOL_GPL(snd_soc_component_exit_regmap);
886f5692 2855
e874bf5f 2856#endif
886f5692 2857
359c71ee 2858static void snd_soc_component_add(struct snd_soc_component *component)
bb13109d 2859{
359c71ee
KM
2860 mutex_lock(&client_mutex);
2861
999f7f5a 2862 if (!component->driver->write && !component->driver->read) {
20feb881 2863 if (!component->regmap)
2c7b696a
MZ
2864 component->regmap = dev_get_regmap(component->dev,
2865 NULL);
20feb881
LPC
2866 if (component->regmap)
2867 snd_soc_component_setup_regmap(component);
2868 }
886f5692 2869
368dee94 2870 /* see for_each_component */
bb13109d 2871 list_add(&component->list, &component_list);
8a978234 2872 INIT_LIST_HEAD(&component->dobj_list);
d191bd8d 2873
d191bd8d 2874 mutex_unlock(&client_mutex);
bb13109d 2875}
d191bd8d 2876
bb13109d
LPC
2877static void snd_soc_component_cleanup(struct snd_soc_component *component)
2878{
2879 snd_soc_unregister_dais(component);
2880 kfree(component->name);
2881}
d191bd8d 2882
bb13109d
LPC
2883static void snd_soc_component_del_unlocked(struct snd_soc_component *component)
2884{
c12c1aad
KM
2885 struct snd_soc_card *card = component->card;
2886
2887 if (card)
e894efef 2888 snd_soc_unbind_card(card, false);
c12c1aad 2889
bb13109d
LPC
2890 list_del(&component->list);
2891}
d191bd8d 2892
273d778e
KM
2893#define ENDIANNESS_MAP(name) \
2894 (SNDRV_PCM_FMTBIT_##name##LE | SNDRV_PCM_FMTBIT_##name##BE)
2895static u64 endianness_format_map[] = {
2896 ENDIANNESS_MAP(S16_),
2897 ENDIANNESS_MAP(U16_),
2898 ENDIANNESS_MAP(S24_),
2899 ENDIANNESS_MAP(U24_),
2900 ENDIANNESS_MAP(S32_),
2901 ENDIANNESS_MAP(U32_),
2902 ENDIANNESS_MAP(S24_3),
2903 ENDIANNESS_MAP(U24_3),
2904 ENDIANNESS_MAP(S20_3),
2905 ENDIANNESS_MAP(U20_3),
2906 ENDIANNESS_MAP(S18_3),
2907 ENDIANNESS_MAP(U18_3),
2908 ENDIANNESS_MAP(FLOAT_),
2909 ENDIANNESS_MAP(FLOAT64_),
2910 ENDIANNESS_MAP(IEC958_SUBFRAME_),
2911};
2912
2913/*
2914 * Fix up the DAI formats for endianness: codecs don't actually see
2915 * the endianness of the data but we're using the CPU format
2916 * definitions which do need to include endianness so we ensure that
2917 * codec DAIs always have both big and little endian variants set.
2918 */
2919static void convert_endianness_formats(struct snd_soc_pcm_stream *stream)
2920{
2921 int i;
2922
2923 for (i = 0; i < ARRAY_SIZE(endianness_format_map); i++)
2924 if (stream->formats & endianness_format_map[i])
2925 stream->formats |= endianness_format_map[i];
2926}
2927
e894efef
SK
2928static void snd_soc_try_rebind_card(void)
2929{
2930 struct snd_soc_card *card, *c;
2931
2932 if (!list_empty(&unbind_card_list)) {
2933 list_for_each_entry_safe(card, c, &unbind_card_list, list) {
2934 if (!snd_soc_bind_card(card))
2935 list_del(&card->list);
2936 }
2937 }
2938}
2939
e0dac41b
KM
2940int snd_soc_add_component(struct device *dev,
2941 struct snd_soc_component *component,
2942 const struct snd_soc_component_driver *component_driver,
2943 struct snd_soc_dai_driver *dai_drv,
2944 int num_dai)
d191bd8d 2945{
bb13109d 2946 int ret;
273d778e 2947 int i;
d191bd8d 2948
cf9e829e 2949 ret = snd_soc_component_initialize(component, component_driver, dev);
bb13109d
LPC
2950 if (ret)
2951 goto err_free;
2952
273d778e
KM
2953 if (component_driver->endianness) {
2954 for (i = 0; i < num_dai; i++) {
2955 convert_endianness_formats(&dai_drv[i].playback);
2956 convert_endianness_formats(&dai_drv[i].capture);
2957 }
2958 }
2959
0e7b25c6 2960 ret = snd_soc_register_dais(component, dai_drv, num_dai);
bb13109d 2961 if (ret < 0) {
f42cf8d6 2962 dev_err(dev, "ASoC: Failed to register DAIs: %d\n", ret);
bb13109d
LPC
2963 goto err_cleanup;
2964 }
d191bd8d 2965
cf9e829e 2966 snd_soc_component_add(component);
e894efef 2967 snd_soc_try_rebind_card();
4da53393 2968
bb13109d 2969 return 0;
4da53393 2970
bb13109d 2971err_cleanup:
cf9e829e 2972 snd_soc_component_cleanup(component);
bb13109d 2973err_free:
bb13109d 2974 return ret;
4da53393 2975}
e0dac41b
KM
2976EXPORT_SYMBOL_GPL(snd_soc_add_component);
2977
2978int snd_soc_register_component(struct device *dev,
2979 const struct snd_soc_component_driver *component_driver,
2980 struct snd_soc_dai_driver *dai_drv,
2981 int num_dai)
2982{
2983 struct snd_soc_component *component;
2984
7ecbd6a9 2985 component = devm_kzalloc(dev, sizeof(*component), GFP_KERNEL);
08e61d03 2986 if (!component)
e0dac41b 2987 return -ENOMEM;
e0dac41b
KM
2988
2989 return snd_soc_add_component(dev, component, component_driver,
2990 dai_drv, num_dai);
2991}
bb13109d 2992EXPORT_SYMBOL_GPL(snd_soc_register_component);
4da53393 2993
d191bd8d 2994/**
2eccea8c
KM
2995 * snd_soc_unregister_component - Unregister all related component
2996 * from the ASoC core
d191bd8d 2997 *
628536ea 2998 * @dev: The device to unregister
d191bd8d 2999 */
2eccea8c 3000static int __snd_soc_unregister_component(struct device *dev)
d191bd8d 3001{
cf9e829e 3002 struct snd_soc_component *component;
21a03528 3003 int found = 0;
d191bd8d 3004
34e81ab4 3005 mutex_lock(&client_mutex);
368dee94 3006 for_each_component(component) {
999f7f5a 3007 if (dev != component->dev)
21a03528
KM
3008 continue;
3009
2c7b696a
MZ
3010 snd_soc_tplg_component_remove(component,
3011 SND_SOC_TPLG_INDEX_ALL);
21a03528
KM
3012 snd_soc_component_del_unlocked(component);
3013 found = 1;
3014 break;
d191bd8d 3015 }
34e81ab4 3016 mutex_unlock(&client_mutex);
d191bd8d 3017
2c7b696a 3018 if (found)
21a03528 3019 snd_soc_component_cleanup(component);
2eccea8c
KM
3020
3021 return found;
3022}
3023
3024void snd_soc_unregister_component(struct device *dev)
3025{
2c7b696a
MZ
3026 while (__snd_soc_unregister_component(dev))
3027 ;
d191bd8d
KM
3028}
3029EXPORT_SYMBOL_GPL(snd_soc_unregister_component);
d191bd8d 3030
7dd5d0d9
KM
3031struct snd_soc_component *snd_soc_lookup_component(struct device *dev,
3032 const char *driver_name)
3033{
3034 struct snd_soc_component *component;
3035 struct snd_soc_component *ret;
3036
3037 ret = NULL;
3038 mutex_lock(&client_mutex);
368dee94 3039 for_each_component(component) {
7dd5d0d9
KM
3040 if (dev != component->dev)
3041 continue;
3042
3043 if (driver_name &&
3044 (driver_name != component->driver->name) &&
3045 (strcmp(component->driver->name, driver_name) != 0))
3046 continue;
3047
3048 ret = component;
3049 break;
3050 }
3051 mutex_unlock(&client_mutex);
3052
3053 return ret;
3054}
3055EXPORT_SYMBOL_GPL(snd_soc_lookup_component);
3056
bec4fa05 3057/* Retrieve a card's name from device tree */
b07609ce
KM
3058int snd_soc_of_parse_card_name(struct snd_soc_card *card,
3059 const char *propname)
bec4fa05 3060{
b07609ce 3061 struct device_node *np;
bec4fa05
SW
3062 int ret;
3063
7e07e7c0
TB
3064 if (!card->dev) {
3065 pr_err("card->dev is not set before calling %s\n", __func__);
3066 return -EINVAL;
3067 }
3068
b07609ce 3069 np = card->dev->of_node;
7e07e7c0 3070
bec4fa05
SW
3071 ret = of_property_read_string_index(np, propname, 0, &card->name);
3072 /*
3073 * EINVAL means the property does not exist. This is fine providing
3074 * card->name was previously set, which is checked later in
3075 * snd_soc_register_card.
3076 */
3077 if (ret < 0 && ret != -EINVAL) {
3078 dev_err(card->dev,
f110bfc7 3079 "ASoC: Property '%s' could not be read: %d\n",
bec4fa05
SW
3080 propname, ret);
3081 return ret;
3082 }
3083
3084 return 0;
3085}
b07609ce 3086EXPORT_SYMBOL_GPL(snd_soc_of_parse_card_name);
bec4fa05 3087
9a6d4860
XL
3088static const struct snd_soc_dapm_widget simple_widgets[] = {
3089 SND_SOC_DAPM_MIC("Microphone", NULL),
3090 SND_SOC_DAPM_LINE("Line", NULL),
3091 SND_SOC_DAPM_HP("Headphone", NULL),
3092 SND_SOC_DAPM_SPK("Speaker", NULL),
3093};
3094
21efde50 3095int snd_soc_of_parse_audio_simple_widgets(struct snd_soc_card *card,
9a6d4860
XL
3096 const char *propname)
3097{
21efde50 3098 struct device_node *np = card->dev->of_node;
9a6d4860
XL
3099 struct snd_soc_dapm_widget *widgets;
3100 const char *template, *wname;
3101 int i, j, num_widgets, ret;
3102
3103 num_widgets = of_property_count_strings(np, propname);
3104 if (num_widgets < 0) {
3105 dev_err(card->dev,
3106 "ASoC: Property '%s' does not exist\n", propname);
3107 return -EINVAL;
3108 }
3109 if (num_widgets & 1) {
3110 dev_err(card->dev,
3111 "ASoC: Property '%s' length is not even\n", propname);
3112 return -EINVAL;
3113 }
3114
3115 num_widgets /= 2;
3116 if (!num_widgets) {
3117 dev_err(card->dev, "ASoC: Property '%s's length is zero\n",
3118 propname);
3119 return -EINVAL;
3120 }
3121
3122 widgets = devm_kcalloc(card->dev, num_widgets, sizeof(*widgets),
3123 GFP_KERNEL);
3124 if (!widgets) {
3125 dev_err(card->dev,
3126 "ASoC: Could not allocate memory for widgets\n");
3127 return -ENOMEM;
3128 }
3129
3130 for (i = 0; i < num_widgets; i++) {
3131 ret = of_property_read_string_index(np, propname,
3132 2 * i, &template);
3133 if (ret) {
3134 dev_err(card->dev,
3135 "ASoC: Property '%s' index %d read error:%d\n",
3136 propname, 2 * i, ret);
3137 return -EINVAL;
3138 }
3139
3140 for (j = 0; j < ARRAY_SIZE(simple_widgets); j++) {
3141 if (!strncmp(template, simple_widgets[j].name,
3142 strlen(simple_widgets[j].name))) {
3143 widgets[i] = simple_widgets[j];
3144 break;
3145 }
3146 }
3147
3148 if (j >= ARRAY_SIZE(simple_widgets)) {
3149 dev_err(card->dev,
3150 "ASoC: DAPM widget '%s' is not supported\n",
3151 template);
3152 return -EINVAL;
3153 }
3154
3155 ret = of_property_read_string_index(np, propname,
3156 (2 * i) + 1,
3157 &wname);
3158 if (ret) {
3159 dev_err(card->dev,
3160 "ASoC: Property '%s' index %d read error:%d\n",
3161 propname, (2 * i) + 1, ret);
3162 return -EINVAL;
3163 }
3164
3165 widgets[i].name = wname;
3166 }
3167
f23e860e
NC
3168 card->of_dapm_widgets = widgets;
3169 card->num_of_dapm_widgets = num_widgets;
9a6d4860
XL
3170
3171 return 0;
3172}
21efde50 3173EXPORT_SYMBOL_GPL(snd_soc_of_parse_audio_simple_widgets);
9a6d4860 3174
cbdfab3b
JB
3175int snd_soc_of_get_slot_mask(struct device_node *np,
3176 const char *prop_name,
3177 unsigned int *mask)
6131084a
JS
3178{
3179 u32 val;
6c84e591 3180 const __be32 *of_slot_mask = of_get_property(np, prop_name, &val);
6131084a
JS
3181 int i;
3182
3183 if (!of_slot_mask)
3184 return 0;
3185 val /= sizeof(u32);
3186 for (i = 0; i < val; i++)
3187 if (be32_to_cpup(&of_slot_mask[i]))
3188 *mask |= (1 << i);
3189
3190 return val;
3191}
cbdfab3b 3192EXPORT_SYMBOL_GPL(snd_soc_of_get_slot_mask);
6131084a 3193
89c67857 3194int snd_soc_of_parse_tdm_slot(struct device_node *np,
6131084a
JS
3195 unsigned int *tx_mask,
3196 unsigned int *rx_mask,
89c67857
XL
3197 unsigned int *slots,
3198 unsigned int *slot_width)
3199{
3200 u32 val;
3201 int ret;
3202
6131084a
JS
3203 if (tx_mask)
3204 snd_soc_of_get_slot_mask(np, "dai-tdm-slot-tx-mask", tx_mask);
3205 if (rx_mask)
3206 snd_soc_of_get_slot_mask(np, "dai-tdm-slot-rx-mask", rx_mask);
3207
89c67857
XL
3208 if (of_property_read_bool(np, "dai-tdm-slot-num")) {
3209 ret = of_property_read_u32(np, "dai-tdm-slot-num", &val);
3210 if (ret)
3211 return ret;
3212
3213 if (slots)
3214 *slots = val;
3215 }
3216
3217 if (of_property_read_bool(np, "dai-tdm-slot-width")) {
3218 ret = of_property_read_u32(np, "dai-tdm-slot-width", &val);
3219 if (ret)
3220 return ret;
3221
3222 if (slot_width)
3223 *slot_width = val;
3224 }
3225
3226 return 0;
3227}
3228EXPORT_SYMBOL_GPL(snd_soc_of_parse_tdm_slot);
3229
3b710356
KM
3230void snd_soc_of_parse_node_prefix(struct device_node *np,
3231 struct snd_soc_codec_conf *codec_conf,
3232 struct device_node *of_node,
3233 const char *propname)
5e3cdaa2 3234{
5e3cdaa2
KM
3235 const char *str;
3236 int ret;
3237
3238 ret = of_property_read_string(np, propname, &str);
3239 if (ret < 0) {
3240 /* no prefix is not error */
3241 return;
3242 }
3243
3244 codec_conf->of_node = of_node;
3245 codec_conf->name_prefix = str;
3246}
3b710356 3247EXPORT_SYMBOL_GPL(snd_soc_of_parse_node_prefix);
5e3cdaa2 3248
2bc644af 3249int snd_soc_of_parse_audio_routing(struct snd_soc_card *card,
a4a54dd5
SW
3250 const char *propname)
3251{
2bc644af 3252 struct device_node *np = card->dev->of_node;
e3b1e6a1 3253 int num_routes;
a4a54dd5
SW
3254 struct snd_soc_dapm_route *routes;
3255 int i, ret;
3256
3257 num_routes = of_property_count_strings(np, propname);
c34ce320 3258 if (num_routes < 0 || num_routes & 1) {
10e8aa9a
MM
3259 dev_err(card->dev,
3260 "ASoC: Property '%s' does not exist or its length is not even\n",
3261 propname);
a4a54dd5
SW
3262 return -EINVAL;
3263 }
3264 num_routes /= 2;
3265 if (!num_routes) {
f110bfc7 3266 dev_err(card->dev, "ASoC: Property '%s's length is zero\n",
a4a54dd5
SW
3267 propname);
3268 return -EINVAL;
3269 }
3270
a86854d0 3271 routes = devm_kcalloc(card->dev, num_routes, sizeof(*routes),
a4a54dd5
SW
3272 GFP_KERNEL);
3273 if (!routes) {
3274 dev_err(card->dev,
f110bfc7 3275 "ASoC: Could not allocate DAPM route table\n");
a4a54dd5
SW
3276 return -EINVAL;
3277 }
3278
3279 for (i = 0; i < num_routes; i++) {
3280 ret = of_property_read_string_index(np, propname,
e3b1e6a1 3281 2 * i, &routes[i].sink);
a4a54dd5 3282 if (ret) {
c871bd0b
MB
3283 dev_err(card->dev,
3284 "ASoC: Property '%s' index %d could not be read: %d\n",
3285 propname, 2 * i, ret);
a4a54dd5
SW
3286 return -EINVAL;
3287 }
3288 ret = of_property_read_string_index(np, propname,
e3b1e6a1 3289 (2 * i) + 1, &routes[i].source);
a4a54dd5
SW
3290 if (ret) {
3291 dev_err(card->dev,
c871bd0b
MB
3292 "ASoC: Property '%s' index %d could not be read: %d\n",
3293 propname, (2 * i) + 1, ret);
a4a54dd5
SW
3294 return -EINVAL;
3295 }
3296 }
3297
f23e860e
NC
3298 card->num_of_dapm_routes = num_routes;
3299 card->of_dapm_routes = routes;
a4a54dd5
SW
3300
3301 return 0;
3302}
2bc644af 3303EXPORT_SYMBOL_GPL(snd_soc_of_parse_audio_routing);
a4a54dd5 3304
a7930ed4 3305unsigned int snd_soc_of_parse_daifmt(struct device_node *np,
389cb834
JS
3306 const char *prefix,
3307 struct device_node **bitclkmaster,
3308 struct device_node **framemaster)
a7930ed4
KM
3309{
3310 int ret, i;
3311 char prop[128];
3312 unsigned int format = 0;
3313 int bit, frame;
3314 const char *str;
3315 struct {
3316 char *name;
3317 unsigned int val;
3318 } of_fmt_table[] = {
3319 { "i2s", SND_SOC_DAIFMT_I2S },
3320 { "right_j", SND_SOC_DAIFMT_RIGHT_J },
3321 { "left_j", SND_SOC_DAIFMT_LEFT_J },
3322 { "dsp_a", SND_SOC_DAIFMT_DSP_A },
3323 { "dsp_b", SND_SOC_DAIFMT_DSP_B },
3324 { "ac97", SND_SOC_DAIFMT_AC97 },
3325 { "pdm", SND_SOC_DAIFMT_PDM},
3326 { "msb", SND_SOC_DAIFMT_MSB },
3327 { "lsb", SND_SOC_DAIFMT_LSB },
a7930ed4
KM
3328 };
3329
3330 if (!prefix)
3331 prefix = "";
3332
3333 /*
5711c979
KM
3334 * check "dai-format = xxx"
3335 * or "[prefix]format = xxx"
a7930ed4
KM
3336 * SND_SOC_DAIFMT_FORMAT_MASK area
3337 */
5711c979
KM
3338 ret = of_property_read_string(np, "dai-format", &str);
3339 if (ret < 0) {
3340 snprintf(prop, sizeof(prop), "%sformat", prefix);
3341 ret = of_property_read_string(np, prop, &str);
3342 }
a7930ed4
KM
3343 if (ret == 0) {
3344 for (i = 0; i < ARRAY_SIZE(of_fmt_table); i++) {
3345 if (strcmp(str, of_fmt_table[i].name) == 0) {
3346 format |= of_fmt_table[i].val;
3347 break;
3348 }
3349 }
3350 }
3351
3352 /*
8c2d6a9f 3353 * check "[prefix]continuous-clock"
a7930ed4
KM
3354 * SND_SOC_DAIFMT_CLOCK_MASK area
3355 */
8c2d6a9f 3356 snprintf(prop, sizeof(prop), "%scontinuous-clock", prefix);
51930295 3357 if (of_property_read_bool(np, prop))
8c2d6a9f
KM
3358 format |= SND_SOC_DAIFMT_CONT;
3359 else
3360 format |= SND_SOC_DAIFMT_GATED;
a7930ed4
KM
3361
3362 /*
3363 * check "[prefix]bitclock-inversion"
3364 * check "[prefix]frame-inversion"
3365 * SND_SOC_DAIFMT_INV_MASK area
3366 */
3367 snprintf(prop, sizeof(prop), "%sbitclock-inversion", prefix);
3368 bit = !!of_get_property(np, prop, NULL);
3369
3370 snprintf(prop, sizeof(prop), "%sframe-inversion", prefix);
3371 frame = !!of_get_property(np, prop, NULL);
3372
3373 switch ((bit << 4) + frame) {
3374 case 0x11:
3375 format |= SND_SOC_DAIFMT_IB_IF;
3376 break;
3377 case 0x10:
3378 format |= SND_SOC_DAIFMT_IB_NF;
3379 break;
3380 case 0x01:
3381 format |= SND_SOC_DAIFMT_NB_IF;
3382 break;
3383 default:
3384 /* SND_SOC_DAIFMT_NB_NF is default */
3385 break;
3386 }
3387
3388 /*
3389 * check "[prefix]bitclock-master"
3390 * check "[prefix]frame-master"
3391 * SND_SOC_DAIFMT_MASTER_MASK area
3392 */
3393 snprintf(prop, sizeof(prop), "%sbitclock-master", prefix);
3394 bit = !!of_get_property(np, prop, NULL);
389cb834
JS
3395 if (bit && bitclkmaster)
3396 *bitclkmaster = of_parse_phandle(np, prop, 0);
a7930ed4
KM
3397
3398 snprintf(prop, sizeof(prop), "%sframe-master", prefix);
3399 frame = !!of_get_property(np, prop, NULL);
389cb834
JS
3400 if (frame && framemaster)
3401 *framemaster = of_parse_phandle(np, prop, 0);
a7930ed4
KM
3402
3403 switch ((bit << 4) + frame) {
3404 case 0x11:
3405 format |= SND_SOC_DAIFMT_CBM_CFM;
3406 break;
3407 case 0x10:
3408 format |= SND_SOC_DAIFMT_CBM_CFS;
3409 break;
3410 case 0x01:
3411 format |= SND_SOC_DAIFMT_CBS_CFM;
3412 break;
3413 default:
3414 format |= SND_SOC_DAIFMT_CBS_CFS;
3415 break;
3416 }
3417
3418 return format;
3419}
3420EXPORT_SYMBOL_GPL(snd_soc_of_parse_daifmt);
3421
a180e8b9
KM
3422int snd_soc_get_dai_id(struct device_node *ep)
3423{
09d4cc03 3424 struct snd_soc_component *component;
c1e230f0 3425 struct snd_soc_dai_link_component dlc;
a180e8b9
KM
3426 int ret;
3427
c1e230f0
KM
3428 dlc.of_node = of_graph_get_port_parent(ep);
3429 dlc.name = NULL;
a180e8b9
KM
3430 /*
3431 * For example HDMI case, HDMI has video/sound port,
3432 * but ALSA SoC needs sound port number only.
3433 * Thus counting HDMI DT port/endpoint doesn't work.
3434 * Then, it should have .of_xlate_dai_id
3435 */
3436 ret = -ENOTSUPP;
3437 mutex_lock(&client_mutex);
c1e230f0 3438 component = soc_find_component(&dlc);
09d4cc03
KM
3439 if (component &&
3440 component->driver->of_xlate_dai_id)
3441 ret = component->driver->of_xlate_dai_id(component, ep);
a180e8b9
KM
3442 mutex_unlock(&client_mutex);
3443
c1e230f0 3444 of_node_put(dlc.of_node);
c0a480d1 3445
a180e8b9
KM
3446 return ret;
3447}
3448EXPORT_SYMBOL_GPL(snd_soc_get_dai_id);
3449
1ad8ec53 3450int snd_soc_get_dai_name(struct of_phandle_args *args,
93b0f3ee 3451 const char **dai_name)
cb470087
KM
3452{
3453 struct snd_soc_component *pos;
3e0aa8d8 3454 struct device_node *component_of_node;
93b0f3ee 3455 int ret = -EPROBE_DEFER;
cb470087
KM
3456
3457 mutex_lock(&client_mutex);
368dee94 3458 for_each_component(pos) {
c0834440 3459 component_of_node = soc_component_to_node(pos);
3e0aa8d8
JS
3460
3461 if (component_of_node != args->np)
cb470087
KM
3462 continue;
3463
6833c452 3464 if (pos->driver->of_xlate_dai_name) {
93b0f3ee
JFM
3465 ret = pos->driver->of_xlate_dai_name(pos,
3466 args,
3467 dai_name);
6833c452 3468 } else {
58bf4179 3469 struct snd_soc_dai *dai;
6833c452
KM
3470 int id = -1;
3471
93b0f3ee 3472 switch (args->args_count) {
6833c452
KM
3473 case 0:
3474 id = 0; /* same as dai_drv[0] */
3475 break;
3476 case 1:
93b0f3ee 3477 id = args->args[0];
6833c452
KM
3478 break;
3479 default:
3480 /* not supported */
3481 break;
3482 }
3483
3484 if (id < 0 || id >= pos->num_dai) {
3485 ret = -EINVAL;
3dcba280 3486 continue;
6833c452 3487 }
e41975ed
XL
3488
3489 ret = 0;
3490
58bf4179 3491 /* find target DAI */
15a0c645 3492 for_each_component_dais(pos, dai) {
58bf4179
KM
3493 if (id == 0)
3494 break;
3495 id--;
3496 }
3497
3498 *dai_name = dai->driver->name;
e41975ed
XL
3499 if (!*dai_name)
3500 *dai_name = pos->name;
cb470087
KM
3501 }
3502
cb470087
KM
3503 break;
3504 }
3505 mutex_unlock(&client_mutex);
93b0f3ee
JFM
3506 return ret;
3507}
1ad8ec53 3508EXPORT_SYMBOL_GPL(snd_soc_get_dai_name);
93b0f3ee
JFM
3509
3510int snd_soc_of_get_dai_name(struct device_node *of_node,
3511 const char **dai_name)
3512{
3513 struct of_phandle_args args;
3514 int ret;
3515
3516 ret = of_parse_phandle_with_args(of_node, "sound-dai",
3517 "#sound-dai-cells", 0, &args);
3518 if (ret)
3519 return ret;
3520
3521 ret = snd_soc_get_dai_name(&args, dai_name);
cb470087
KM
3522
3523 of_node_put(args.np);
3524
3525 return ret;
3526}
3527EXPORT_SYMBOL_GPL(snd_soc_of_get_dai_name);
3528
94685763
SN
3529/*
3530 * snd_soc_of_put_dai_link_codecs - Dereference device nodes in the codecs array
3531 * @dai_link: DAI link
3532 *
3533 * Dereference device nodes acquired by snd_soc_of_get_dai_link_codecs().
3534 */
3535void snd_soc_of_put_dai_link_codecs(struct snd_soc_dai_link *dai_link)
3536{
3db769f1 3537 struct snd_soc_dai_link_component *component;
94685763
SN
3538 int index;
3539
3db769f1 3540 for_each_link_codecs(dai_link, index, component) {
94685763
SN
3541 if (!component->of_node)
3542 break;
3543 of_node_put(component->of_node);
3544 component->of_node = NULL;
3545 }
3546}
3547EXPORT_SYMBOL_GPL(snd_soc_of_put_dai_link_codecs);
3548
93b0f3ee
JFM
3549/*
3550 * snd_soc_of_get_dai_link_codecs - Parse a list of CODECs in the devicetree
3551 * @dev: Card device
3552 * @of_node: Device node
3553 * @dai_link: DAI link
3554 *
3555 * Builds an array of CODEC DAI components from the DAI link property
3556 * 'sound-dai'.
3557 * The array is set in the DAI link and the number of DAIs is set accordingly.
94685763
SN
3558 * The device nodes in the array (of_node) must be dereferenced by calling
3559 * snd_soc_of_put_dai_link_codecs() on @dai_link.
93b0f3ee
JFM
3560 *
3561 * Returns 0 for success
3562 */
3563int snd_soc_of_get_dai_link_codecs(struct device *dev,
3564 struct device_node *of_node,
3565 struct snd_soc_dai_link *dai_link)
3566{
3567 struct of_phandle_args args;
3568 struct snd_soc_dai_link_component *component;
3569 char *name;
3570 int index, num_codecs, ret;
3571
3572 /* Count the number of CODECs */
3573 name = "sound-dai";
3574 num_codecs = of_count_phandle_with_args(of_node, name,
3575 "#sound-dai-cells");
3576 if (num_codecs <= 0) {
3577 if (num_codecs == -ENOENT)
3578 dev_err(dev, "No 'sound-dai' property\n");
3579 else
3580 dev_err(dev, "Bad phandle in 'sound-dai'\n");
3581 return num_codecs;
3582 }
a86854d0
KC
3583 component = devm_kcalloc(dev,
3584 num_codecs, sizeof(*component),
93b0f3ee
JFM
3585 GFP_KERNEL);
3586 if (!component)
3587 return -ENOMEM;
3588 dai_link->codecs = component;
3589 dai_link->num_codecs = num_codecs;
3590
3591 /* Parse the list */
3db769f1 3592 for_each_link_codecs(dai_link, index, component) {
93b0f3ee
JFM
3593 ret = of_parse_phandle_with_args(of_node, name,
3594 "#sound-dai-cells",
2c7b696a 3595 index, &args);
93b0f3ee
JFM
3596 if (ret)
3597 goto err;
3598 component->of_node = args.np;
3599 ret = snd_soc_get_dai_name(&args, &component->dai_name);
3600 if (ret < 0)
3601 goto err;
3602 }
3603 return 0;
3604err:
94685763 3605 snd_soc_of_put_dai_link_codecs(dai_link);
93b0f3ee
JFM
3606 dai_link->codecs = NULL;
3607 dai_link->num_codecs = 0;
3608 return ret;
3609}
3610EXPORT_SYMBOL_GPL(snd_soc_of_get_dai_link_codecs);
3611
c9b3a40f 3612static int __init snd_soc_init(void)
db2a4165 3613{
6553bf06 3614 snd_soc_debugfs_init();
fb257897
MB
3615 snd_soc_util_init();
3616
db2a4165
FM
3617 return platform_driver_register(&soc_driver);
3618}
4abe8e16 3619module_init(snd_soc_init);
db2a4165 3620
7d8c16a6 3621static void __exit snd_soc_exit(void)
db2a4165 3622{
fb257897 3623 snd_soc_util_exit();
6553bf06 3624 snd_soc_debugfs_exit();
fb257897 3625
3ff3f64b 3626 platform_driver_unregister(&soc_driver);
db2a4165 3627}
db2a4165
FM
3628module_exit(snd_soc_exit);
3629
3630/* Module information */
d331124d 3631MODULE_AUTHOR("Liam Girdwood, lrg@slimlogic.co.uk");
db2a4165
FM
3632MODULE_DESCRIPTION("ALSA SoC Core");
3633MODULE_LICENSE("GPL");
8b45a209 3634MODULE_ALIAS("platform:soc-audio");