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