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