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