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