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