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