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