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