]> git.proxmox.com Git - mirror_ubuntu-zesty-kernel.git/blame - sound/soc/soc-core.c
ASoC: Pass component instead of DAPM context to AUX dev init callback
[mirror_ubuntu-zesty-kernel.git] / sound / soc / soc-core.c
CommitLineData
db2a4165
FM
1/*
2 * soc-core.c -- ALSA SoC Audio Layer
3 *
4 * Copyright 2005 Wolfson Microelectronics PLC.
0664d888 5 * Copyright 2005 Openedhand Ltd.
f0fba2ad
LG
6 * Copyright (C) 2010 Slimlogic Ltd.
7 * Copyright (C) 2010 Texas Instruments Inc.
0664d888 8 *
d331124d 9 * Author: Liam Girdwood <lrg@slimlogic.co.uk>
0664d888
LG
10 * with code, comments and ideas from :-
11 * Richard Purdie <richard@openedhand.com>
db2a4165
FM
12 *
13 * This program is free software; you can redistribute it and/or modify it
14 * under the terms of the GNU General Public License as published by the
15 * Free Software Foundation; either version 2 of the License, or (at your
16 * option) any later version.
17 *
db2a4165
FM
18 * TODO:
19 * o Add hw rules to enforce rates, etc.
20 * o More testing with other codecs/machines.
21 * o Add more codecs and platforms to ensure good API coverage.
22 * o Support TDM on PCM and I2S
23 */
24
25#include <linux/module.h>
26#include <linux/moduleparam.h>
27#include <linux/init.h>
28#include <linux/delay.h>
29#include <linux/pm.h>
30#include <linux/bitops.h>
12ef193d 31#include <linux/debugfs.h>
db2a4165 32#include <linux/platform_device.h>
741a509f 33#include <linux/pinctrl/consumer.h>
f0e8ed85 34#include <linux/ctype.h>
5a0e3ad6 35#include <linux/slab.h>
bec4fa05 36#include <linux/of.h>
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);
81c7cfd1 314 debugfs_create_bool("cache_only", 0444, codec->component.debugfs_root,
aaee8ef1
MB
315 &codec->cache_only);
316
2624d5fa 317 codec->debugfs_reg = debugfs_create_file("codec_reg", 0644,
81c7cfd1 318 codec->component.debugfs_root,
2624d5fa
MB
319 codec, &codec_reg_fops);
320 if (!codec->debugfs_reg)
10e8aa9a
MM
321 dev_warn(codec->dev,
322 "ASoC: Failed to create codec register debugfs file\n");
731f1ab2
SG
323}
324
c3c5a19a
MB
325static ssize_t codec_list_read_file(struct file *file, char __user *user_buf,
326 size_t count, loff_t *ppos)
327{
328 char *buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
2b194f9d 329 ssize_t len, ret = 0;
c3c5a19a
MB
330 struct snd_soc_codec *codec;
331
332 if (!buf)
333 return -ENOMEM;
334
2b194f9d
MB
335 list_for_each_entry(codec, &codec_list, list) {
336 len = snprintf(buf + ret, PAGE_SIZE - ret, "%s\n",
f4333203 337 codec->component.name);
2b194f9d
MB
338 if (len >= 0)
339 ret += len;
340 if (ret > PAGE_SIZE) {
341 ret = PAGE_SIZE;
342 break;
343 }
344 }
c3c5a19a
MB
345
346 if (ret >= 0)
347 ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
348
349 kfree(buf);
350
351 return ret;
352}
353
354static const struct file_operations codec_list_fops = {
355 .read = codec_list_read_file,
356 .llseek = default_llseek,/* read accesses f_pos */
357};
358
f3208780
MB
359static ssize_t dai_list_read_file(struct file *file, char __user *user_buf,
360 size_t count, loff_t *ppos)
361{
362 char *buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
2b194f9d 363 ssize_t len, ret = 0;
1438c2f6 364 struct snd_soc_component *component;
f3208780
MB
365 struct snd_soc_dai *dai;
366
367 if (!buf)
368 return -ENOMEM;
369
1438c2f6
LPC
370 list_for_each_entry(component, &component_list, list) {
371 list_for_each_entry(dai, &component->dai_list, list) {
372 len = snprintf(buf + ret, PAGE_SIZE - ret, "%s\n",
373 dai->name);
374 if (len >= 0)
375 ret += len;
376 if (ret > PAGE_SIZE) {
377 ret = PAGE_SIZE;
378 break;
379 }
2b194f9d
MB
380 }
381 }
f3208780 382
2b194f9d 383 ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
f3208780
MB
384
385 kfree(buf);
386
387 return ret;
388}
389
390static const struct file_operations dai_list_fops = {
391 .read = dai_list_read_file,
392 .llseek = default_llseek,/* read accesses f_pos */
393};
394
19c7ac27
MB
395static ssize_t platform_list_read_file(struct file *file,
396 char __user *user_buf,
397 size_t count, loff_t *ppos)
398{
399 char *buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
2b194f9d 400 ssize_t len, ret = 0;
19c7ac27
MB
401 struct snd_soc_platform *platform;
402
403 if (!buf)
404 return -ENOMEM;
405
2b194f9d
MB
406 list_for_each_entry(platform, &platform_list, list) {
407 len = snprintf(buf + ret, PAGE_SIZE - ret, "%s\n",
f4333203 408 platform->component.name);
2b194f9d
MB
409 if (len >= 0)
410 ret += len;
411 if (ret > PAGE_SIZE) {
412 ret = PAGE_SIZE;
413 break;
414 }
415 }
19c7ac27 416
2b194f9d 417 ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
19c7ac27
MB
418
419 kfree(buf);
420
421 return ret;
422}
423
424static const struct file_operations platform_list_fops = {
425 .read = platform_list_read_file,
426 .llseek = default_llseek,/* read accesses f_pos */
427};
428
a6052154
JN
429static void soc_init_card_debugfs(struct snd_soc_card *card)
430{
431 card->debugfs_card_root = debugfs_create_dir(card->name,
8a9dab1a 432 snd_soc_debugfs_root);
3a45b867 433 if (!card->debugfs_card_root) {
a6052154 434 dev_warn(card->dev,
7c08be84 435 "ASoC: Failed to create card debugfs directory\n");
3a45b867
JN
436 return;
437 }
438
439 card->debugfs_pop_time = debugfs_create_u32("dapm_pop_time", 0644,
440 card->debugfs_card_root,
441 &card->pop_time);
442 if (!card->debugfs_pop_time)
443 dev_warn(card->dev,
f110bfc7 444 "ASoC: Failed to create pop time debugfs file\n");
a6052154
JN
445}
446
447static void soc_cleanup_card_debugfs(struct snd_soc_card *card)
448{
449 debugfs_remove_recursive(card->debugfs_card_root);
450}
451
2624d5fa
MB
452#else
453
81c7cfd1 454#define soc_init_codec_debugfs NULL
b95fccbc 455
81c7cfd1
LPC
456static inline void soc_init_component_debugfs(
457 struct snd_soc_component *component)
731f1ab2
SG
458{
459}
460
81c7cfd1
LPC
461static inline void soc_cleanup_component_debugfs(
462 struct snd_soc_component *component)
731f1ab2
SG
463{
464}
465
b95fccbc
AL
466static inline void soc_init_card_debugfs(struct snd_soc_card *card)
467{
468}
469
470static inline void soc_cleanup_card_debugfs(struct snd_soc_card *card)
471{
472}
2624d5fa
MB
473#endif
474
47c88fff
LG
475struct snd_pcm_substream *snd_soc_get_dai_substream(struct snd_soc_card *card,
476 const char *dai_link, int stream)
477{
478 int i;
479
480 for (i = 0; i < card->num_links; i++) {
481 if (card->rtd[i].dai_link->no_pcm &&
482 !strcmp(card->rtd[i].dai_link->name, dai_link))
483 return card->rtd[i].pcm->streams[stream].substream;
484 }
f110bfc7 485 dev_dbg(card->dev, "ASoC: failed to find dai link %s\n", dai_link);
47c88fff
LG
486 return NULL;
487}
488EXPORT_SYMBOL_GPL(snd_soc_get_dai_substream);
489
490struct snd_soc_pcm_runtime *snd_soc_get_pcm_runtime(struct snd_soc_card *card,
491 const char *dai_link)
492{
493 int i;
494
495 for (i = 0; i < card->num_links; i++) {
496 if (!strcmp(card->rtd[i].dai_link->name, dai_link))
497 return &card->rtd[i];
498 }
f110bfc7 499 dev_dbg(card->dev, "ASoC: failed to find rtd %s\n", dai_link);
47c88fff
LG
500 return NULL;
501}
502EXPORT_SYMBOL_GPL(snd_soc_get_pcm_runtime);
503
db2a4165
FM
504#ifdef CONFIG_SND_SOC_AC97_BUS
505/* unregister ac97 codec */
506static int soc_ac97_dev_unregister(struct snd_soc_codec *codec)
507{
508 if (codec->ac97->dev.bus)
509 device_unregister(&codec->ac97->dev);
510 return 0;
511}
512
513/* stop no dev release warning */
514static void soc_ac97_device_release(struct device *dev){}
515
516/* register ac97 codec to bus */
517static int soc_ac97_dev_register(struct snd_soc_codec *codec)
518{
519 int err;
520
521 codec->ac97->dev.bus = &ac97_bus_type;
00200107 522 codec->ac97->dev.parent = codec->component.card->dev;
db2a4165
FM
523 codec->ac97->dev.release = soc_ac97_device_release;
524
bb072bf0 525 dev_set_name(&codec->ac97->dev, "%d-%d:%s",
00200107
LPC
526 codec->component.card->snd_card->number, 0,
527 codec->component.name);
db2a4165
FM
528 err = device_register(&codec->ac97->dev);
529 if (err < 0) {
f110bfc7 530 dev_err(codec->dev, "ASoC: Can't register ac97 bus\n");
db2a4165
FM
531 codec->ac97->dev.bus = NULL;
532 return err;
533 }
534 return 0;
535}
536#endif
537
9d58a077
RF
538static void codec2codec_close_delayed_work(struct work_struct *work)
539{
540 /* Currently nothing to do for c2c links
541 * Since c2c links are internal nodes in the DAPM graph and
542 * don't interface with the outside world or application layer
543 * we don't have to do any special handling on close.
544 */
545}
546
6f8ab4ac 547#ifdef CONFIG_PM_SLEEP
db2a4165 548/* powers down audio subsystem for suspend */
6f8ab4ac 549int snd_soc_suspend(struct device *dev)
db2a4165 550{
6f8ab4ac 551 struct snd_soc_card *card = dev_get_drvdata(dev);
2eea392d 552 struct snd_soc_codec *codec;
88bd870f 553 int i, j;
db2a4165 554
e3509ff0
DM
555 /* If the initialization of this soc device failed, there is no codec
556 * associated with it. Just bail out in this case.
557 */
f0fba2ad 558 if (list_empty(&card->codec_dev_list))
e3509ff0
DM
559 return 0;
560
6ed25978
AG
561 /* Due to the resume being scheduled into a workqueue we could
562 * suspend before that's finished - wait for it to complete.
563 */
f0fba2ad
LG
564 snd_power_lock(card->snd_card);
565 snd_power_wait(card->snd_card, SNDRV_CTL_POWER_D0);
566 snd_power_unlock(card->snd_card);
6ed25978
AG
567
568 /* we're going to block userspace touching us until resume completes */
f0fba2ad 569 snd_power_change_state(card->snd_card, SNDRV_CTL_POWER_D3hot);
6ed25978 570
a00f90f9 571 /* mute any active DACs */
f0fba2ad 572 for (i = 0; i < card->num_rtd; i++) {
3efab7dc 573
f0fba2ad 574 if (card->rtd[i].dai_link->ignore_suspend)
3efab7dc
MB
575 continue;
576
88bd870f
BC
577 for (j = 0; j < card->rtd[i].num_codecs; j++) {
578 struct snd_soc_dai *dai = card->rtd[i].codec_dais[j];
579 struct snd_soc_dai_driver *drv = dai->driver;
580
581 if (drv->ops->digital_mute && dai->playback_active)
582 drv->ops->digital_mute(dai, 1);
583 }
db2a4165
FM
584 }
585
4ccab3e7 586 /* suspend all pcms */
f0fba2ad
LG
587 for (i = 0; i < card->num_rtd; i++) {
588 if (card->rtd[i].dai_link->ignore_suspend)
3efab7dc
MB
589 continue;
590
f0fba2ad 591 snd_pcm_suspend_all(card->rtd[i].pcm);
3efab7dc 592 }
4ccab3e7 593
87506549 594 if (card->suspend_pre)
70b2ac12 595 card->suspend_pre(card);
db2a4165 596
f0fba2ad
LG
597 for (i = 0; i < card->num_rtd; i++) {
598 struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
599 struct snd_soc_platform *platform = card->rtd[i].platform;
3efab7dc 600
f0fba2ad 601 if (card->rtd[i].dai_link->ignore_suspend)
3efab7dc
MB
602 continue;
603
f0fba2ad
LG
604 if (cpu_dai->driver->suspend && !cpu_dai->driver->ac97_control)
605 cpu_dai->driver->suspend(cpu_dai);
606 if (platform->driver->suspend && !platform->suspended) {
607 platform->driver->suspend(cpu_dai);
608 platform->suspended = 1;
609 }
db2a4165
FM
610 }
611
612 /* close any waiting streams and save state */
f0fba2ad 613 for (i = 0; i < card->num_rtd; i++) {
88bd870f 614 struct snd_soc_dai **codec_dais = card->rtd[i].codec_dais;
43829731 615 flush_delayed_work(&card->rtd[i].delayed_work);
88bd870f
BC
616 for (j = 0; j < card->rtd[i].num_codecs; j++) {
617 codec_dais[j]->codec->dapm.suspend_bias_level =
618 codec_dais[j]->codec->dapm.bias_level;
619 }
f0fba2ad 620 }
db2a4165 621
f0fba2ad 622 for (i = 0; i < card->num_rtd; i++) {
3efab7dc 623
f0fba2ad 624 if (card->rtd[i].dai_link->ignore_suspend)
3efab7dc
MB
625 continue;
626
7bd3a6f3
MB
627 snd_soc_dapm_stream_event(&card->rtd[i],
628 SNDRV_PCM_STREAM_PLAYBACK,
7bd3a6f3 629 SND_SOC_DAPM_STREAM_SUSPEND);
f0fba2ad 630
7bd3a6f3
MB
631 snd_soc_dapm_stream_event(&card->rtd[i],
632 SNDRV_PCM_STREAM_CAPTURE,
7bd3a6f3 633 SND_SOC_DAPM_STREAM_SUSPEND);
db2a4165
FM
634 }
635
e2d32ff6
MB
636 /* Recheck all analogue paths too */
637 dapm_mark_io_dirty(&card->dapm);
638 snd_soc_dapm_sync(&card->dapm);
639
f0fba2ad 640 /* suspend all CODECs */
2eea392d 641 list_for_each_entry(codec, &card->codec_dev_list, card_list) {
f0fba2ad
LG
642 /* If there are paths active then the CODEC will be held with
643 * bias _ON and should not be suspended. */
644 if (!codec->suspended && codec->driver->suspend) {
ce6120cc 645 switch (codec->dapm.bias_level) {
f0fba2ad 646 case SND_SOC_BIAS_STANDBY:
125a25da
MB
647 /*
648 * If the CODEC is capable of idle
649 * bias off then being in STANDBY
650 * means it's doing something,
651 * otherwise fall through.
652 */
653 if (codec->dapm.idle_bias_off) {
654 dev_dbg(codec->dev,
10e8aa9a 655 "ASoC: idle_bias_off CODEC on over suspend\n");
125a25da
MB
656 break;
657 }
f0fba2ad 658 case SND_SOC_BIAS_OFF:
84b315ee 659 codec->driver->suspend(codec);
f0fba2ad 660 codec->suspended = 1;
7be4ba24 661 codec->cache_sync = 1;
e2c330b9
LPC
662 if (codec->component.regmap)
663 regcache_mark_dirty(codec->component.regmap);
988e8cc4
NC
664 /* deactivate pins to sleep state */
665 pinctrl_pm_select_sleep_state(codec->dev);
f0fba2ad
LG
666 break;
667 default:
10e8aa9a
MM
668 dev_dbg(codec->dev,
669 "ASoC: CODEC is on over suspend\n");
f0fba2ad
LG
670 break;
671 }
1547aba9
MB
672 }
673 }
db2a4165 674
f0fba2ad
LG
675 for (i = 0; i < card->num_rtd; i++) {
676 struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
3efab7dc 677
f0fba2ad 678 if (card->rtd[i].dai_link->ignore_suspend)
3efab7dc
MB
679 continue;
680
f0fba2ad
LG
681 if (cpu_dai->driver->suspend && cpu_dai->driver->ac97_control)
682 cpu_dai->driver->suspend(cpu_dai);
988e8cc4
NC
683
684 /* deactivate pins to sleep state */
685 pinctrl_pm_select_sleep_state(cpu_dai->dev);
db2a4165
FM
686 }
687
87506549 688 if (card->suspend_post)
70b2ac12 689 card->suspend_post(card);
db2a4165
FM
690
691 return 0;
692}
6f8ab4ac 693EXPORT_SYMBOL_GPL(snd_soc_suspend);
db2a4165 694
6ed25978
AG
695/* deferred resume work, so resume can complete before we finished
696 * setting our codec back up, which can be very slow on I2C
697 */
698static void soc_resume_deferred(struct work_struct *work)
db2a4165 699{
f0fba2ad
LG
700 struct snd_soc_card *card =
701 container_of(work, struct snd_soc_card, deferred_resume_work);
2eea392d 702 struct snd_soc_codec *codec;
88bd870f 703 int i, j;
db2a4165 704
6ed25978
AG
705 /* our power state is still SNDRV_CTL_POWER_D3hot from suspend time,
706 * so userspace apps are blocked from touching us
707 */
708
f110bfc7 709 dev_dbg(card->dev, "ASoC: starting resume work\n");
6ed25978 710
9949788b 711 /* Bring us up into D2 so that DAPM starts enabling things */
f0fba2ad 712 snd_power_change_state(card->snd_card, SNDRV_CTL_POWER_D2);
9949788b 713
87506549 714 if (card->resume_pre)
70b2ac12 715 card->resume_pre(card);
db2a4165 716
f0fba2ad
LG
717 /* resume AC97 DAIs */
718 for (i = 0; i < card->num_rtd; i++) {
719 struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
3efab7dc 720
f0fba2ad 721 if (card->rtd[i].dai_link->ignore_suspend)
3efab7dc
MB
722 continue;
723
f0fba2ad
LG
724 if (cpu_dai->driver->resume && cpu_dai->driver->ac97_control)
725 cpu_dai->driver->resume(cpu_dai);
726 }
727
2eea392d 728 list_for_each_entry(codec, &card->codec_dev_list, card_list) {
f0fba2ad
LG
729 /* If the CODEC was idle over suspend then it will have been
730 * left with bias OFF or STANDBY and suspended so we must now
731 * resume. Otherwise the suspend was suppressed.
732 */
733 if (codec->driver->resume && codec->suspended) {
ce6120cc 734 switch (codec->dapm.bias_level) {
f0fba2ad
LG
735 case SND_SOC_BIAS_STANDBY:
736 case SND_SOC_BIAS_OFF:
737 codec->driver->resume(codec);
738 codec->suspended = 0;
739 break;
740 default:
10e8aa9a
MM
741 dev_dbg(codec->dev,
742 "ASoC: CODEC was on over suspend\n");
f0fba2ad
LG
743 break;
744 }
1547aba9
MB
745 }
746 }
db2a4165 747
f0fba2ad 748 for (i = 0; i < card->num_rtd; i++) {
3efab7dc 749
f0fba2ad 750 if (card->rtd[i].dai_link->ignore_suspend)
3efab7dc
MB
751 continue;
752
7bd3a6f3 753 snd_soc_dapm_stream_event(&card->rtd[i],
d9b0951b 754 SNDRV_PCM_STREAM_PLAYBACK,
7bd3a6f3 755 SND_SOC_DAPM_STREAM_RESUME);
f0fba2ad 756
7bd3a6f3 757 snd_soc_dapm_stream_event(&card->rtd[i],
d9b0951b 758 SNDRV_PCM_STREAM_CAPTURE,
7bd3a6f3 759 SND_SOC_DAPM_STREAM_RESUME);
db2a4165
FM
760 }
761
3ff3f64b 762 /* unmute any active DACs */
f0fba2ad 763 for (i = 0; i < card->num_rtd; i++) {
3efab7dc 764
f0fba2ad 765 if (card->rtd[i].dai_link->ignore_suspend)
3efab7dc
MB
766 continue;
767
88bd870f
BC
768 for (j = 0; j < card->rtd[i].num_codecs; j++) {
769 struct snd_soc_dai *dai = card->rtd[i].codec_dais[j];
770 struct snd_soc_dai_driver *drv = dai->driver;
771
772 if (drv->ops->digital_mute && dai->playback_active)
773 drv->ops->digital_mute(dai, 0);
774 }
db2a4165
FM
775 }
776
f0fba2ad
LG
777 for (i = 0; i < card->num_rtd; i++) {
778 struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
779 struct snd_soc_platform *platform = card->rtd[i].platform;
3efab7dc 780
f0fba2ad 781 if (card->rtd[i].dai_link->ignore_suspend)
3efab7dc
MB
782 continue;
783
f0fba2ad
LG
784 if (cpu_dai->driver->resume && !cpu_dai->driver->ac97_control)
785 cpu_dai->driver->resume(cpu_dai);
786 if (platform->driver->resume && platform->suspended) {
787 platform->driver->resume(cpu_dai);
788 platform->suspended = 0;
789 }
db2a4165
FM
790 }
791
87506549 792 if (card->resume_post)
70b2ac12 793 card->resume_post(card);
db2a4165 794
f110bfc7 795 dev_dbg(card->dev, "ASoC: resume work completed\n");
6ed25978
AG
796
797 /* userspace can access us now we are back as we were before */
f0fba2ad 798 snd_power_change_state(card->snd_card, SNDRV_CTL_POWER_D0);
e2d32ff6
MB
799
800 /* Recheck all analogue paths too */
801 dapm_mark_io_dirty(&card->dapm);
802 snd_soc_dapm_sync(&card->dapm);
6ed25978
AG
803}
804
805/* powers up audio subsystem after a suspend */
6f8ab4ac 806int snd_soc_resume(struct device *dev)
6ed25978 807{
6f8ab4ac 808 struct snd_soc_card *card = dev_get_drvdata(dev);
82e14e8b 809 int i, ac97_control = 0;
b9dd94a8 810
5ff1ddf2
EM
811 /* If the initialization of this soc device failed, there is no codec
812 * associated with it. Just bail out in this case.
813 */
814 if (list_empty(&card->codec_dev_list))
815 return 0;
816
988e8cc4
NC
817 /* activate pins from sleep state */
818 for (i = 0; i < card->num_rtd; i++) {
88bd870f
BC
819 struct snd_soc_pcm_runtime *rtd = &card->rtd[i];
820 struct snd_soc_dai **codec_dais = rtd->codec_dais;
821 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
822 int j;
823
988e8cc4
NC
824 if (cpu_dai->active)
825 pinctrl_pm_select_default_state(cpu_dai->dev);
88bd870f
BC
826
827 for (j = 0; j < rtd->num_codecs; j++) {
828 struct snd_soc_dai *codec_dai = codec_dais[j];
829 if (codec_dai->active)
830 pinctrl_pm_select_default_state(codec_dai->dev);
831 }
988e8cc4
NC
832 }
833
64ab9baa
MB
834 /* AC97 devices might have other drivers hanging off them so
835 * need to resume immediately. Other drivers don't have that
836 * problem and may take a substantial amount of time to resume
837 * due to I/O costs and anti-pop so handle them out of line.
838 */
f0fba2ad
LG
839 for (i = 0; i < card->num_rtd; i++) {
840 struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
82e14e8b
SW
841 ac97_control |= cpu_dai->driver->ac97_control;
842 }
843 if (ac97_control) {
f110bfc7 844 dev_dbg(dev, "ASoC: Resuming AC97 immediately\n");
82e14e8b
SW
845 soc_resume_deferred(&card->deferred_resume_work);
846 } else {
f110bfc7 847 dev_dbg(dev, "ASoC: Scheduling resume work\n");
82e14e8b 848 if (!schedule_work(&card->deferred_resume_work))
f110bfc7 849 dev_err(dev, "ASoC: resume work item may be lost\n");
64ab9baa 850 }
6ed25978 851
db2a4165
FM
852 return 0;
853}
6f8ab4ac 854EXPORT_SYMBOL_GPL(snd_soc_resume);
db2a4165 855#else
6f8ab4ac
MB
856#define snd_soc_suspend NULL
857#define snd_soc_resume NULL
db2a4165
FM
858#endif
859
85e7652d 860static const struct snd_soc_dai_ops null_dai_ops = {
02a06d30
BS
861};
862
65d9361f
LPC
863static struct snd_soc_component *soc_find_component(
864 const struct device_node *of_node, const char *name)
865{
866 struct snd_soc_component *component;
867
868 list_for_each_entry(component, &component_list, list) {
869 if (of_node) {
870 if (component->dev->of_node == of_node)
871 return component;
872 } else if (strcmp(component->name, name) == 0) {
873 return component;
874 }
875 }
876
877 return NULL;
878}
879
88bd870f
BC
880static struct snd_soc_codec *soc_find_codec(
881 const struct device_node *codec_of_node,
882 const char *codec_name)
12023a9a
MLC
883{
884 struct snd_soc_codec *codec;
885
886 list_for_each_entry(codec, &codec_list, list) {
887 if (codec_of_node) {
888 if (codec->dev->of_node != codec_of_node)
889 continue;
890 } else {
f4333203 891 if (strcmp(codec->component.name, codec_name))
12023a9a
MLC
892 continue;
893 }
894
895 return codec;
896 }
897
898 return NULL;
899}
900
901static struct snd_soc_dai *soc_find_codec_dai(struct snd_soc_codec *codec,
902 const char *codec_dai_name)
903{
904 struct snd_soc_dai *codec_dai;
905
906 list_for_each_entry(codec_dai, &codec->component.dai_list, list) {
907 if (!strcmp(codec_dai->name, codec_dai_name)) {
908 return codec_dai;
909 }
910 }
911
912 return NULL;
913}
914
f0fba2ad 915static int soc_bind_dai_link(struct snd_soc_card *card, int num)
db2a4165 916{
f0fba2ad
LG
917 struct snd_soc_dai_link *dai_link = &card->dai_link[num];
918 struct snd_soc_pcm_runtime *rtd = &card->rtd[num];
1438c2f6 919 struct snd_soc_component *component;
88bd870f
BC
920 struct snd_soc_dai_link_component *codecs = dai_link->codecs;
921 struct snd_soc_dai **codec_dais = rtd->codec_dais;
435c5e25 922 struct snd_soc_platform *platform;
12023a9a 923 struct snd_soc_dai *cpu_dai;
848dd8be 924 const char *platform_name;
88bd870f 925 int i;
435c5e25 926
f110bfc7 927 dev_dbg(card->dev, "ASoC: binding %s at idx %d\n", dai_link->name, num);
435c5e25 928
b19e6e7b 929 /* Find CPU DAI from registered DAIs*/
1438c2f6 930 list_for_each_entry(component, &component_list, list) {
bc92657a 931 if (dai_link->cpu_of_node &&
1438c2f6 932 component->dev->of_node != dai_link->cpu_of_node)
bc92657a
SW
933 continue;
934 if (dai_link->cpu_name &&
1438c2f6 935 strcmp(dev_name(component->dev), dai_link->cpu_name))
bc92657a 936 continue;
1438c2f6
LPC
937 list_for_each_entry(cpu_dai, &component->dai_list, list) {
938 if (dai_link->cpu_dai_name &&
939 strcmp(cpu_dai->name, dai_link->cpu_dai_name))
940 continue;
2610ab77 941
1438c2f6
LPC
942 rtd->cpu_dai = cpu_dai;
943 }
435c5e25 944 }
6308419a 945
b19e6e7b 946 if (!rtd->cpu_dai) {
f110bfc7 947 dev_err(card->dev, "ASoC: CPU DAI %s not registered\n",
b19e6e7b
MB
948 dai_link->cpu_dai_name);
949 return -EPROBE_DEFER;
f0fba2ad
LG
950 }
951
88bd870f 952 rtd->num_codecs = dai_link->num_codecs;
848dd8be 953
88bd870f
BC
954 /* Find CODEC from registered CODECs */
955 for (i = 0; i < rtd->num_codecs; i++) {
956 struct snd_soc_codec *codec;
957 codec = soc_find_codec(codecs[i].of_node, codecs[i].name);
958 if (!codec) {
959 dev_err(card->dev, "ASoC: CODEC %s not registered\n",
960 codecs[i].name);
961 return -EPROBE_DEFER;
962 }
963
964 codec_dais[i] = soc_find_codec_dai(codec, codecs[i].dai_name);
965 if (!codec_dais[i]) {
966 dev_err(card->dev, "ASoC: CODEC DAI %s not registered\n",
967 codecs[i].dai_name);
968 return -EPROBE_DEFER;
969 }
12023a9a
MLC
970 }
971
88bd870f
BC
972 /* Single codec links expect codec and codec_dai in runtime data */
973 rtd->codec_dai = codec_dais[0];
974 rtd->codec = rtd->codec_dai->codec;
975
848dd8be
MB
976 /* if there's no platform we match on the empty platform */
977 platform_name = dai_link->platform_name;
5a504963 978 if (!platform_name && !dai_link->platform_of_node)
848dd8be
MB
979 platform_name = "snd-soc-dummy";
980
b19e6e7b 981 /* find one from the set of registered platforms */
f0fba2ad 982 list_for_each_entry(platform, &platform_list, list) {
5a504963
SW
983 if (dai_link->platform_of_node) {
984 if (platform->dev->of_node !=
985 dai_link->platform_of_node)
986 continue;
987 } else {
f4333203 988 if (strcmp(platform->component.name, platform_name))
5a504963
SW
989 continue;
990 }
2610ab77
SW
991
992 rtd->platform = platform;
02a06d30 993 }
b19e6e7b 994 if (!rtd->platform) {
f110bfc7 995 dev_err(card->dev, "ASoC: platform %s not registered\n",
f0fba2ad 996 dai_link->platform_name);
b19e6e7b 997 return -EPROBE_DEFER;
f0fba2ad 998 }
b19e6e7b
MB
999
1000 card->num_rtd++;
1001
1002 return 0;
f0fba2ad
LG
1003}
1004
f1d45cc3 1005static void soc_remove_component(struct snd_soc_component *component)
d12cd198 1006{
f1d45cc3
LPC
1007 /* This is a HACK and will be removed soon */
1008 if (component->codec)
1009 list_del(&component->codec->card_list);
589c3563 1010
f1d45cc3
LPC
1011 if (component->remove)
1012 component->remove(component);
589c3563 1013
f1d45cc3 1014 snd_soc_dapm_free(snd_soc_component_get_dapm(component));
589c3563 1015
f1d45cc3
LPC
1016 soc_cleanup_component_debugfs(component);
1017 component->probed = 0;
1018 module_put(component->dev->driver->owner);
589c3563
JN
1019}
1020
b0aa88af 1021static void soc_remove_codec_dai(struct snd_soc_dai *codec_dai, int order)
f0fba2ad 1022{
f0fba2ad
LG
1023 int err;
1024
0168bf0d
LG
1025 if (codec_dai && codec_dai->probed &&
1026 codec_dai->driver->remove_order == order) {
f0fba2ad
LG
1027 if (codec_dai->driver->remove) {
1028 err = codec_dai->driver->remove(codec_dai);
1029 if (err < 0)
f110bfc7
LG
1030 dev_err(codec_dai->dev,
1031 "ASoC: failed to remove %s: %d\n",
1032 codec_dai->name, err);
6b05eda6 1033 }
f0fba2ad 1034 codec_dai->probed = 0;
f0fba2ad 1035 }
b0aa88af
MLC
1036}
1037
1038static void soc_remove_link_dais(struct snd_soc_card *card, int num, int order)
1039{
1040 struct snd_soc_pcm_runtime *rtd = &card->rtd[num];
88bd870f
BC
1041 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
1042 int i, err;
b0aa88af
MLC
1043
1044 /* unregister the rtd device */
1045 if (rtd->dev_registered) {
1046 device_remove_file(rtd->dev, &dev_attr_pmdown_time);
1047 device_remove_file(rtd->dev, &dev_attr_codec_reg);
1048 device_unregister(rtd->dev);
1049 rtd->dev_registered = 0;
1050 }
1051
1052 /* remove the CODEC DAI */
88bd870f
BC
1053 for (i = 0; i < rtd->num_codecs; i++)
1054 soc_remove_codec_dai(rtd->codec_dais[i], order);
6b05eda6 1055
f0fba2ad 1056 /* remove the cpu_dai */
0168bf0d
LG
1057 if (cpu_dai && cpu_dai->probed &&
1058 cpu_dai->driver->remove_order == order) {
f0fba2ad
LG
1059 if (cpu_dai->driver->remove) {
1060 err = cpu_dai->driver->remove(cpu_dai);
1061 if (err < 0)
f110bfc7
LG
1062 dev_err(cpu_dai->dev,
1063 "ASoC: failed to remove %s: %d\n",
1064 cpu_dai->name, err);
db2a4165 1065 }
f0fba2ad 1066 cpu_dai->probed = 0;
9420d97b 1067 if (!cpu_dai->codec)
a9db7dbe 1068 module_put(cpu_dai->dev->driver->owner);
db2a4165 1069 }
f0fba2ad 1070}
db2a4165 1071
62ae68fa
SW
1072static void soc_remove_link_components(struct snd_soc_card *card, int num,
1073 int order)
1074{
1075 struct snd_soc_pcm_runtime *rtd = &card->rtd[num];
1076 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
62ae68fa 1077 struct snd_soc_platform *platform = rtd->platform;
61aca564 1078 struct snd_soc_component *component;
88bd870f 1079 int i;
62ae68fa
SW
1080
1081 /* remove the platform */
f1d45cc3
LPC
1082 if (platform && platform->component.probed &&
1083 platform->component.driver->remove_order == order)
1084 soc_remove_component(&platform->component);
62ae68fa
SW
1085
1086 /* remove the CODEC-side CODEC */
88bd870f 1087 for (i = 0; i < rtd->num_codecs; i++) {
61aca564
LPC
1088 component = rtd->codec_dais[i]->component;
1089 if (component->probed &&
1090 component->driver->remove_order == order)
1091 soc_remove_component(component);
62ae68fa
SW
1092 }
1093
1094 /* remove any CPU-side CODEC */
1095 if (cpu_dai) {
61aca564
LPC
1096 if (cpu_dai->component->probed &&
1097 cpu_dai->component->driver->remove_order == order)
1098 soc_remove_component(cpu_dai->component);
62ae68fa
SW
1099 }
1100}
1101
0671fd8e
KM
1102static void soc_remove_dai_links(struct snd_soc_card *card)
1103{
0168bf0d 1104 int dai, order;
0671fd8e 1105
0168bf0d
LG
1106 for (order = SND_SOC_COMP_ORDER_FIRST; order <= SND_SOC_COMP_ORDER_LAST;
1107 order++) {
1108 for (dai = 0; dai < card->num_rtd; dai++)
62ae68fa
SW
1109 soc_remove_link_dais(card, dai, order);
1110 }
1111
1112 for (order = SND_SOC_COMP_ORDER_FIRST; order <= SND_SOC_COMP_ORDER_LAST;
1113 order++) {
1114 for (dai = 0; dai < card->num_rtd; dai++)
1115 soc_remove_link_components(card, dai, order);
0168bf0d 1116 }
62ae68fa 1117
0671fd8e
KM
1118 card->num_rtd = 0;
1119}
1120
ead9b919 1121static void soc_set_name_prefix(struct snd_soc_card *card,
94f99c87 1122 struct snd_soc_component *component)
ead9b919
JN
1123{
1124 int i;
1125
ff819b83 1126 if (card->codec_conf == NULL)
ead9b919
JN
1127 return;
1128
ff819b83
DP
1129 for (i = 0; i < card->num_configs; i++) {
1130 struct snd_soc_codec_conf *map = &card->codec_conf[i];
94f99c87 1131 if (map->of_node && component->dev->of_node != map->of_node)
3ca041ed 1132 continue;
94f99c87 1133 if (map->dev_name && strcmp(component->name, map->dev_name))
3ca041ed 1134 continue;
94f99c87 1135 component->name_prefix = map->name_prefix;
3ca041ed 1136 break;
ead9b919
JN
1137 }
1138}
1139
f1d45cc3
LPC
1140static int soc_probe_component(struct snd_soc_card *card,
1141 struct snd_soc_component *component)
589c3563 1142{
f1d45cc3
LPC
1143 struct snd_soc_dapm_context *dapm = snd_soc_component_get_dapm(component);
1144 struct snd_soc_component *dai_component, *component2;
888df395 1145 struct snd_soc_dai *dai;
f1d45cc3 1146 int ret;
589c3563 1147
f1d45cc3
LPC
1148 component->card = card;
1149 dapm->card = card;
1150 soc_set_name_prefix(card, component);
589c3563 1151
f1d45cc3 1152 if (!try_module_get(component->dev->driver->owner))
70d29331
JN
1153 return -ENODEV;
1154
f1d45cc3 1155 soc_init_component_debugfs(component);
d5d1e0be 1156
f1d45cc3
LPC
1157 if (component->dapm_widgets) {
1158 ret = snd_soc_dapm_new_controls(dapm, component->dapm_widgets,
1159 component->num_dapm_widgets);
b318ad50
NP
1160
1161 if (ret != 0) {
f1d45cc3 1162 dev_err(component->dev,
b318ad50
NP
1163 "Failed to create new controls %d\n", ret);
1164 goto err_probe;
1165 }
1166 }
77530150 1167
f1d45cc3
LPC
1168 /*
1169 * This is rather ugly, but certain platforms expect that the DAPM
1170 * widgets for the DAIs for components with the same parent device are
1171 * created in the platforms DAPM context. Until that is fixed we need to
1172 * keep this.
1173 */
1174 if (component->steal_sibling_dai_widgets) {
1175 dai_component = NULL;
1176 list_for_each_entry(component2, &component_list, list) {
1177 if (component == component2)
1178 continue;
261edc70 1179
f1d45cc3
LPC
1180 if (component2->dev == component->dev &&
1181 !list_empty(&component2->dai_list)) {
1182 dai_component = component2;
1183 break;
1184 }
261edc70 1185 }
f1d45cc3
LPC
1186 } else {
1187 dai_component = component;
1188 list_for_each_entry(component2, &component_list, list) {
1189 if (component2->dev == component->dev &&
1190 component2->steal_sibling_dai_widgets) {
1191 dai_component = NULL;
1192 break;
1193 }
589c3563
JN
1194 }
1195 }
1196
f1d45cc3
LPC
1197 if (dai_component) {
1198 list_for_each_entry(dai, &dai_component->dai_list, list) {
1199 snd_soc_dapm_new_dai_widgets(dapm, dai);
1200 if (ret != 0) {
1201 dev_err(component->dev,
1202 "Failed to create DAI widgets %d\n",
1203 ret);
1204 goto err_probe;
1205 }
1206 }
be09ad90
LG
1207 }
1208
f1d45cc3
LPC
1209 if (component->probe) {
1210 ret = component->probe(component);
956245e9 1211 if (ret < 0) {
f1d45cc3
LPC
1212 dev_err(component->dev,
1213 "ASoC: failed to probe component %d\n", ret);
956245e9
LG
1214 goto err_probe;
1215 }
f1d45cc3
LPC
1216
1217 WARN(dapm->idle_bias_off &&
1218 dapm->bias_level != SND_SOC_BIAS_OFF,
1219 "codec %s can not start from non-off bias with idle_bias_off==1\n",
1220 component->name);
956245e9
LG
1221 }
1222
f1d45cc3
LPC
1223 if (component->controls)
1224 snd_soc_add_component_controls(component, component->controls,
1225 component->num_controls);
1226 if (component->dapm_routes)
1227 snd_soc_dapm_add_routes(dapm, component->dapm_routes,
1228 component->num_dapm_routes);
1229
1230 component->probed = 1;
1231 list_add(&dapm->list, &card->dapm_list);
cb2cf612 1232
f1d45cc3
LPC
1233 /* This is a HACK and will be removed soon */
1234 if (component->codec)
1235 list_add(&component->codec->card_list, &card->codec_dev_list);
956245e9
LG
1236
1237 return 0;
1238
1239err_probe:
f1d45cc3
LPC
1240 soc_cleanup_component_debugfs(component);
1241 module_put(component->dev->driver->owner);
956245e9
LG
1242
1243 return ret;
1244}
1245
36ae1a96
MB
1246static void rtd_release(struct device *dev)
1247{
1248 kfree(dev);
1249}
f0fba2ad 1250
5f3484ac
LPC
1251static int soc_post_component_init(struct snd_soc_pcm_runtime *rtd,
1252 const char *name)
503ae5e0 1253{
589c3563
JN
1254 int ret = 0;
1255
589c3563 1256 /* register the rtd device */
36ae1a96
MB
1257 rtd->dev = kzalloc(sizeof(struct device), GFP_KERNEL);
1258 if (!rtd->dev)
1259 return -ENOMEM;
1260 device_initialize(rtd->dev);
5f3484ac 1261 rtd->dev->parent = rtd->card->dev;
36ae1a96
MB
1262 rtd->dev->release = rtd_release;
1263 rtd->dev->init_name = name;
1264 dev_set_drvdata(rtd->dev, rtd);
b8c0dab9 1265 mutex_init(&rtd->pcm_mutex);
01d7584c
LG
1266 INIT_LIST_HEAD(&rtd->dpcm[SNDRV_PCM_STREAM_PLAYBACK].be_clients);
1267 INIT_LIST_HEAD(&rtd->dpcm[SNDRV_PCM_STREAM_CAPTURE].be_clients);
1268 INIT_LIST_HEAD(&rtd->dpcm[SNDRV_PCM_STREAM_PLAYBACK].fe_clients);
1269 INIT_LIST_HEAD(&rtd->dpcm[SNDRV_PCM_STREAM_CAPTURE].fe_clients);
36ae1a96 1270 ret = device_add(rtd->dev);
589c3563 1271 if (ret < 0) {
865df9cb
CL
1272 /* calling put_device() here to free the rtd->dev */
1273 put_device(rtd->dev);
5f3484ac 1274 dev_err(rtd->card->dev,
f110bfc7 1275 "ASoC: failed to register runtime device: %d\n", ret);
589c3563
JN
1276 return ret;
1277 }
1278 rtd->dev_registered = 1;
1279
93c3ce76
LPC
1280 if (rtd->codec) {
1281 /* add DAPM sysfs entries for this codec */
1282 ret = snd_soc_dapm_sys_add(rtd->dev);
1283 if (ret < 0)
1284 dev_err(rtd->dev,
1285 "ASoC: failed to add codec dapm sysfs entries: %d\n",
1286 ret);
1287
1288 /* add codec sysfs entries */
1289 ret = device_create_file(rtd->dev, &dev_attr_codec_reg);
1290 if (ret < 0)
1291 dev_err(rtd->dev,
1292 "ASoC: failed to add codec sysfs files: %d\n",
1293 ret);
1294 }
589c3563
JN
1295
1296 return 0;
1297}
1298
62ae68fa
SW
1299static int soc_probe_link_components(struct snd_soc_card *card, int num,
1300 int order)
1301{
1302 struct snd_soc_pcm_runtime *rtd = &card->rtd[num];
62ae68fa 1303 struct snd_soc_platform *platform = rtd->platform;
f1d45cc3 1304 struct snd_soc_component *component;
88bd870f 1305 int i, ret;
62ae68fa
SW
1306
1307 /* probe the CPU-side component, if it is a CODEC */
61aca564
LPC
1308 component = rtd->cpu_dai->component;
1309 if (!component->probed &&
1310 component->driver->probe_order == order) {
1311 ret = soc_probe_component(card, component);
1312 if (ret < 0)
1313 return ret;
62ae68fa
SW
1314 }
1315
88bd870f
BC
1316 /* probe the CODEC-side components */
1317 for (i = 0; i < rtd->num_codecs; i++) {
61aca564 1318 component = rtd->codec_dais[i]->component;
f1d45cc3
LPC
1319 if (!component->probed &&
1320 component->driver->probe_order == order) {
1321 ret = soc_probe_component(card, component);
88bd870f
BC
1322 if (ret < 0)
1323 return ret;
1324 }
62ae68fa
SW
1325 }
1326
1327 /* probe the platform */
f1d45cc3
LPC
1328 if (!platform->component.probed &&
1329 platform->component.driver->probe_order == order) {
1330 ret = soc_probe_component(card, &platform->component);
62ae68fa
SW
1331 if (ret < 0)
1332 return ret;
1333 }
1334
1335 return 0;
1336}
1337
b0aa88af
MLC
1338static int soc_probe_codec_dai(struct snd_soc_card *card,
1339 struct snd_soc_dai *codec_dai,
1340 int order)
1341{
1342 int ret;
1343
1344 if (!codec_dai->probed && codec_dai->driver->probe_order == order) {
1345 if (codec_dai->driver->probe) {
1346 ret = codec_dai->driver->probe(codec_dai);
1347 if (ret < 0) {
1348 dev_err(codec_dai->dev,
1349 "ASoC: failed to probe CODEC DAI %s: %d\n",
1350 codec_dai->name, ret);
1351 return ret;
1352 }
1353 }
1354
1355 /* mark codec_dai as probed and add to card dai list */
1356 codec_dai->probed = 1;
b0aa88af
MLC
1357 }
1358
1359 return 0;
1360}
1361
2436a723
MLC
1362static int soc_link_dai_widgets(struct snd_soc_card *card,
1363 struct snd_soc_dai_link *dai_link,
3f901a02 1364 struct snd_soc_pcm_runtime *rtd)
2436a723 1365{
3f901a02
BC
1366 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
1367 struct snd_soc_dai *codec_dai = rtd->codec_dai;
2436a723
MLC
1368 struct snd_soc_dapm_widget *play_w, *capture_w;
1369 int ret;
1370
88bd870f
BC
1371 if (rtd->num_codecs > 1)
1372 dev_warn(card->dev, "ASoC: Multiple codecs not supported yet\n");
1373
2436a723
MLC
1374 /* link the DAI widgets */
1375 play_w = codec_dai->playback_widget;
1376 capture_w = cpu_dai->capture_widget;
1377 if (play_w && capture_w) {
1378 ret = snd_soc_dapm_new_pcm(card, dai_link->params,
1379 capture_w, play_w);
1380 if (ret != 0) {
1381 dev_err(card->dev, "ASoC: Can't link %s to %s: %d\n",
1382 play_w->name, capture_w->name, ret);
1383 return ret;
1384 }
1385 }
1386
1387 play_w = cpu_dai->playback_widget;
1388 capture_w = codec_dai->capture_widget;
1389 if (play_w && capture_w) {
1390 ret = snd_soc_dapm_new_pcm(card, dai_link->params,
1391 capture_w, play_w);
1392 if (ret != 0) {
1393 dev_err(card->dev, "ASoC: Can't link %s to %s: %d\n",
1394 play_w->name, capture_w->name, ret);
1395 return ret;
1396 }
1397 }
1398
1399 return 0;
1400}
1401
62ae68fa 1402static int soc_probe_link_dais(struct snd_soc_card *card, int num, int order)
f0fba2ad
LG
1403{
1404 struct snd_soc_dai_link *dai_link = &card->dai_link[num];
1405 struct snd_soc_pcm_runtime *rtd = &card->rtd[num];
f0fba2ad 1406 struct snd_soc_platform *platform = rtd->platform;
c74184ed 1407 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
88bd870f 1408 int i, ret;
f0fba2ad 1409
f110bfc7 1410 dev_dbg(card->dev, "ASoC: probe %s dai link %d late %d\n",
0168bf0d 1411 card->name, num, order);
f0fba2ad
LG
1412
1413 /* config components */
f0fba2ad 1414 cpu_dai->platform = platform;
f0fba2ad 1415 cpu_dai->card = card;
88bd870f
BC
1416 for (i = 0; i < rtd->num_codecs; i++)
1417 rtd->codec_dais[i]->card = card;
f0fba2ad
LG
1418
1419 /* set default power off timeout */
1420 rtd->pmdown_time = pmdown_time;
1421
1422 /* probe the cpu_dai */
0168bf0d
LG
1423 if (!cpu_dai->probed &&
1424 cpu_dai->driver->probe_order == order) {
a9db7dbe 1425 if (!cpu_dai->codec) {
a9db7dbe
SW
1426 if (!try_module_get(cpu_dai->dev->driver->owner))
1427 return -ENODEV;
a9db7dbe 1428 }
be09ad90 1429
f0fba2ad
LG
1430 if (cpu_dai->driver->probe) {
1431 ret = cpu_dai->driver->probe(cpu_dai);
1432 if (ret < 0) {
f110bfc7
LG
1433 dev_err(cpu_dai->dev,
1434 "ASoC: failed to probe CPU DAI %s: %d\n",
1435 cpu_dai->name, ret);
61b61e3c 1436 module_put(cpu_dai->dev->driver->owner);
f0fba2ad
LG
1437 return ret;
1438 }
1439 }
1440 cpu_dai->probed = 1;
db2a4165
FM
1441 }
1442
f0fba2ad 1443 /* probe the CODEC DAI */
88bd870f
BC
1444 for (i = 0; i < rtd->num_codecs; i++) {
1445 ret = soc_probe_codec_dai(card, rtd->codec_dais[i], order);
1446 if (ret)
1447 return ret;
1448 }
fe3e78e0 1449
0168bf0d
LG
1450 /* complete DAI probe during last probe */
1451 if (order != SND_SOC_COMP_ORDER_LAST)
1452 return 0;
1453
5f3484ac
LPC
1454 /* do machine specific initialization */
1455 if (dai_link->init) {
1456 ret = dai_link->init(rtd);
1457 if (ret < 0) {
1458 dev_err(card->dev, "ASoC: failed to init %s: %d\n",
1459 dai_link->name, ret);
1460 return ret;
1461 }
1462 }
1463
1464 ret = soc_post_component_init(rtd, dai_link->name);
589c3563 1465 if (ret)
f0fba2ad 1466 return ret;
fe3e78e0 1467
5f3484ac
LPC
1468#ifdef CONFIG_DEBUG_FS
1469 /* add DPCM sysfs entries */
1470 if (dai_link->dynamic) {
1471 ret = soc_dpcm_debugfs_add(rtd);
1472 if (ret < 0) {
1473 dev_err(rtd->dev,
1474 "ASoC: failed to add dpcm sysfs entries: %d\n",
1475 ret);
1476 return ret;
1477 }
1478 }
1479#endif
1480
36ae1a96 1481 ret = device_create_file(rtd->dev, &dev_attr_pmdown_time);
f0fba2ad 1482 if (ret < 0)
f110bfc7
LG
1483 dev_warn(rtd->dev, "ASoC: failed to add pmdown_time sysfs: %d\n",
1484 ret);
f0fba2ad 1485
1245b700
NK
1486 if (cpu_dai->driver->compress_dai) {
1487 /*create compress_device"*/
1488 ret = soc_new_compress(rtd, num);
c74184ed 1489 if (ret < 0) {
f110bfc7 1490 dev_err(card->dev, "ASoC: can't create compress %s\n",
1245b700 1491 dai_link->stream_name);
c74184ed
MB
1492 return ret;
1493 }
1494 } else {
1245b700
NK
1495
1496 if (!dai_link->params) {
1497 /* create the pcm */
1498 ret = soc_new_pcm(rtd, num);
1499 if (ret < 0) {
f110bfc7 1500 dev_err(card->dev, "ASoC: can't create pcm %s :%d\n",
1245b700 1501 dai_link->stream_name, ret);
c74184ed
MB
1502 return ret;
1503 }
1245b700 1504 } else {
9d58a077
RF
1505 INIT_DELAYED_WORK(&rtd->delayed_work,
1506 codec2codec_close_delayed_work);
1507
1245b700 1508 /* link the DAI widgets */
3f901a02 1509 ret = soc_link_dai_widgets(card, dai_link, rtd);
2436a723
MLC
1510 if (ret)
1511 return ret;
c74184ed 1512 }
f0fba2ad
LG
1513 }
1514
1515 /* add platform data for AC97 devices */
88bd870f
BC
1516 for (i = 0; i < rtd->num_codecs; i++) {
1517 if (rtd->codec_dais[i]->driver->ac97_control)
1518 snd_ac97_dev_add_pdata(rtd->codec_dais[i]->codec->ac97,
1519 rtd->cpu_dai->ac97_pdata);
1520 }
f0fba2ad
LG
1521
1522 return 0;
1523}
1524
fe3e78e0 1525#ifdef CONFIG_SND_SOC_AC97_BUS
02c9c7b9
MLC
1526static int soc_register_ac97_codec(struct snd_soc_codec *codec,
1527 struct snd_soc_dai *codec_dai)
f0fba2ad
LG
1528{
1529 int ret;
1530
fe3e78e0
MB
1531 /* Only instantiate AC97 if not already done by the adaptor
1532 * for the generic AC97 subsystem.
1533 */
02c9c7b9 1534 if (codec_dai->driver->ac97_control && !codec->ac97_registered) {
0562f788
MW
1535 /*
1536 * It is possible that the AC97 device is already registered to
1537 * the device subsystem. This happens when the device is created
1538 * via snd_ac97_mixer(). Currently only SoC codec that does so
1539 * is the generic AC97 glue but others migh emerge.
1540 *
1541 * In those cases we don't try to register the device again.
1542 */
02c9c7b9 1543 if (!codec->ac97_created)
0562f788 1544 return 0;
f0fba2ad 1545
02c9c7b9 1546 ret = soc_ac97_dev_register(codec);
fe3e78e0 1547 if (ret < 0) {
02c9c7b9 1548 dev_err(codec->dev,
f110bfc7 1549 "ASoC: AC97 device register failed: %d\n", ret);
f0fba2ad 1550 return ret;
fe3e78e0 1551 }
f0fba2ad 1552
02c9c7b9 1553 codec->ac97_registered = 1;
fe3e78e0 1554 }
f0fba2ad
LG
1555 return 0;
1556}
1557
02c9c7b9 1558static void soc_unregister_ac97_codec(struct snd_soc_codec *codec)
f0fba2ad
LG
1559{
1560 if (codec->ac97_registered) {
1561 soc_ac97_dev_unregister(codec);
1562 codec->ac97_registered = 0;
1563 }
1564}
02c9c7b9 1565
88bd870f
BC
1566static int soc_register_ac97_dai_link(struct snd_soc_pcm_runtime *rtd)
1567{
1568 int i, ret;
1569
1570 for (i = 0; i < rtd->num_codecs; i++) {
1571 struct snd_soc_dai *codec_dai = rtd->codec_dais[i];
1572
1573 ret = soc_register_ac97_codec(codec_dai->codec, codec_dai);
1574 if (ret) {
1575 while (--i >= 0)
1576 soc_unregister_ac97_codec(codec_dai->codec);
1577 return ret;
1578 }
1579 }
1580
1581 return 0;
1582}
1583
02c9c7b9
MLC
1584static void soc_unregister_ac97_dai_link(struct snd_soc_pcm_runtime *rtd)
1585{
88bd870f
BC
1586 int i;
1587
1588 for (i = 0; i < rtd->num_codecs; i++)
1589 soc_unregister_ac97_codec(rtd->codec_dais[i]->codec);
02c9c7b9 1590}
fe3e78e0
MB
1591#endif
1592
44c69bb1 1593static int soc_bind_aux_dev(struct snd_soc_card *card, int num)
3ca041ed 1594{
44c69bb1 1595 struct snd_soc_pcm_runtime *rtd = &card->rtd_aux[num];
3ca041ed 1596 struct snd_soc_aux_dev *aux_dev = &card->aux_dev[num];
65d9361f 1597 const char *name = aux_dev->codec_name;
3ca041ed 1598
65d9361f
LPC
1599 rtd->component = soc_find_component(aux_dev->codec_of_node, name);
1600 if (!rtd->component) {
3ca041ed 1601 if (aux_dev->codec_of_node)
65d9361f 1602 name = of_node_full_name(aux_dev->codec_of_node);
3ca041ed 1603
65d9361f 1604 dev_err(card->dev, "ASoC: %s not registered\n", name);
3ca041ed
SR
1605 return -EPROBE_DEFER;
1606 }
fb099cb7 1607
65d9361f
LPC
1608 /*
1609 * Some places still reference rtd->codec, so we have to keep that
1610 * initialized if the component is a CODEC. Once all those references
1611 * have been removed, this code can be removed as well.
1612 */
1613 rtd->codec = rtd->component->codec;
1614
44c69bb1 1615 return 0;
b19e6e7b
MB
1616}
1617
2eea392d
JN
1618static int soc_probe_aux_dev(struct snd_soc_card *card, int num)
1619{
44c69bb1 1620 struct snd_soc_pcm_runtime *rtd = &card->rtd_aux[num];
2eea392d 1621 struct snd_soc_aux_dev *aux_dev = &card->aux_dev[num];
44c69bb1 1622 int ret;
3ca041ed 1623
65d9361f
LPC
1624 if (rtd->component->probed) {
1625 dev_err(rtd->dev, "ASoC: codec already probed\n");
3ca041ed 1626 return -EBUSY;
2eea392d
JN
1627 }
1628
65d9361f 1629 ret = soc_probe_component(card, rtd->component);
2eea392d 1630 if (ret < 0)
589c3563 1631 return ret;
2eea392d 1632
5f3484ac
LPC
1633 /* do machine specific initialization */
1634 if (aux_dev->init) {
57bf7726 1635 ret = aux_dev->init(rtd->component);
5f3484ac
LPC
1636 if (ret < 0) {
1637 dev_err(card->dev, "ASoC: failed to init %s: %d\n",
1638 aux_dev->name, ret);
1639 return ret;
1640 }
1641 }
2eea392d 1642
5f3484ac 1643 return soc_post_component_init(rtd, aux_dev->name);
2eea392d
JN
1644}
1645
1646static void soc_remove_aux_dev(struct snd_soc_card *card, int num)
1647{
1648 struct snd_soc_pcm_runtime *rtd = &card->rtd_aux[num];
65d9361f 1649 struct snd_soc_component *component = rtd->component;
2eea392d
JN
1650
1651 /* unregister the rtd device */
1652 if (rtd->dev_registered) {
36ae1a96 1653 device_remove_file(rtd->dev, &dev_attr_codec_reg);
d3bf1561 1654 device_unregister(rtd->dev);
2eea392d
JN
1655 rtd->dev_registered = 0;
1656 }
1657
65d9361f
LPC
1658 if (component && component->probed)
1659 soc_remove_component(component);
2eea392d
JN
1660}
1661
f90fb3f7 1662static int snd_soc_init_codec_cache(struct snd_soc_codec *codec)
fdf0f54d
DP
1663{
1664 int ret;
1665
1666 if (codec->cache_init)
1667 return 0;
1668
fdf0f54d
DP
1669 ret = snd_soc_cache_init(codec);
1670 if (ret < 0) {
10e8aa9a
MM
1671 dev_err(codec->dev,
1672 "ASoC: Failed to set cache compression type: %d\n",
1673 ret);
fdf0f54d
DP
1674 return ret;
1675 }
1676 codec->cache_init = 1;
1677 return 0;
1678}
1679
b19e6e7b 1680static int snd_soc_instantiate_card(struct snd_soc_card *card)
f0fba2ad 1681{
fdf0f54d 1682 struct snd_soc_codec *codec;
75d9ac46 1683 struct snd_soc_dai_link *dai_link;
f04209a7 1684 int ret, i, order, dai_fmt;
fe3e78e0 1685
01b9d99a 1686 mutex_lock_nested(&card->mutex, SND_SOC_CARD_CLASS_INIT);
dbe21408 1687
f0fba2ad 1688 /* bind DAIs */
b19e6e7b
MB
1689 for (i = 0; i < card->num_links; i++) {
1690 ret = soc_bind_dai_link(card, i);
1691 if (ret != 0)
1692 goto base_error;
1693 }
fe3e78e0 1694
44c69bb1 1695 /* bind aux_devs too */
b19e6e7b 1696 for (i = 0; i < card->num_aux_devs; i++) {
44c69bb1 1697 ret = soc_bind_aux_dev(card, i);
b19e6e7b
MB
1698 if (ret != 0)
1699 goto base_error;
f0fba2ad 1700 }
435c5e25 1701
fdf0f54d
DP
1702 /* initialize the register cache for each available codec */
1703 list_for_each_entry(codec, &codec_list, list) {
1704 if (codec->cache_init)
1705 continue;
f90fb3f7 1706 ret = snd_soc_init_codec_cache(codec);
b19e6e7b
MB
1707 if (ret < 0)
1708 goto base_error;
fdf0f54d
DP
1709 }
1710
f0fba2ad 1711 /* card bind complete so register a sound card */
102b5a8d 1712 ret = snd_card_new(card->dev, SNDRV_DEFAULT_IDX1, SNDRV_DEFAULT_STR1,
f0fba2ad
LG
1713 card->owner, 0, &card->snd_card);
1714 if (ret < 0) {
10e8aa9a
MM
1715 dev_err(card->dev,
1716 "ASoC: can't create sound card for card %s: %d\n",
1717 card->name, ret);
b19e6e7b 1718 goto base_error;
f0fba2ad 1719 }
f0fba2ad 1720
e37a4970
MB
1721 card->dapm.bias_level = SND_SOC_BIAS_OFF;
1722 card->dapm.dev = card->dev;
1723 card->dapm.card = card;
1724 list_add(&card->dapm.list, &card->dapm_list);
1725
d5d1e0be
LPC
1726#ifdef CONFIG_DEBUG_FS
1727 snd_soc_dapm_debugfs_init(&card->dapm, card->debugfs_card_root);
1728#endif
1729
88ee1c61 1730#ifdef CONFIG_PM_SLEEP
f0fba2ad
LG
1731 /* deferred resume work */
1732 INIT_WORK(&card->deferred_resume_work, soc_resume_deferred);
1733#endif
db2a4165 1734
9a841ebb
MB
1735 if (card->dapm_widgets)
1736 snd_soc_dapm_new_controls(&card->dapm, card->dapm_widgets,
1737 card->num_dapm_widgets);
1738
f0fba2ad
LG
1739 /* initialise the sound card only once */
1740 if (card->probe) {
e7361ec4 1741 ret = card->probe(card);
f0fba2ad
LG
1742 if (ret < 0)
1743 goto card_probe_error;
1744 }
fe3e78e0 1745
62ae68fa 1746 /* probe all components used by DAI links on this card */
0168bf0d
LG
1747 for (order = SND_SOC_COMP_ORDER_FIRST; order <= SND_SOC_COMP_ORDER_LAST;
1748 order++) {
1749 for (i = 0; i < card->num_links; i++) {
62ae68fa 1750 ret = soc_probe_link_components(card, i, order);
0168bf0d 1751 if (ret < 0) {
f110bfc7
LG
1752 dev_err(card->dev,
1753 "ASoC: failed to instantiate card %d\n",
1754 ret);
62ae68fa
SW
1755 goto probe_dai_err;
1756 }
1757 }
1758 }
1759
1760 /* probe all DAI links on this card */
1761 for (order = SND_SOC_COMP_ORDER_FIRST; order <= SND_SOC_COMP_ORDER_LAST;
1762 order++) {
1763 for (i = 0; i < card->num_links; i++) {
1764 ret = soc_probe_link_dais(card, i, order);
1765 if (ret < 0) {
f110bfc7
LG
1766 dev_err(card->dev,
1767 "ASoC: failed to instantiate card %d\n",
1768 ret);
0168bf0d
LG
1769 goto probe_dai_err;
1770 }
f0fba2ad
LG
1771 }
1772 }
db2a4165 1773
2eea392d
JN
1774 for (i = 0; i < card->num_aux_devs; i++) {
1775 ret = soc_probe_aux_dev(card, i);
1776 if (ret < 0) {
f110bfc7
LG
1777 dev_err(card->dev,
1778 "ASoC: failed to add auxiliary devices %d\n",
1779 ret);
2eea392d
JN
1780 goto probe_aux_dev_err;
1781 }
1782 }
1783
888df395 1784 snd_soc_dapm_link_dai_widgets(card);
b893ea5f 1785 snd_soc_dapm_connect_dai_link_widgets(card);
888df395 1786
b7af1daf 1787 if (card->controls)
022658be 1788 snd_soc_add_card_controls(card, card->controls, card->num_controls);
b7af1daf 1789
b8ad29de
MB
1790 if (card->dapm_routes)
1791 snd_soc_dapm_add_routes(&card->dapm, card->dapm_routes,
1792 card->num_dapm_routes);
1793
75d9ac46 1794 for (i = 0; i < card->num_links; i++) {
88bd870f 1795 struct snd_soc_pcm_runtime *rtd = &card->rtd[i];
75d9ac46 1796 dai_link = &card->dai_link[i];
f04209a7 1797 dai_fmt = dai_link->dai_fmt;
75d9ac46 1798
f04209a7 1799 if (dai_fmt) {
88bd870f
BC
1800 struct snd_soc_dai **codec_dais = rtd->codec_dais;
1801 int j;
1802
1803 for (j = 0; j < rtd->num_codecs; j++) {
1804 struct snd_soc_dai *codec_dai = codec_dais[j];
1805
1806 ret = snd_soc_dai_set_fmt(codec_dai, dai_fmt);
1807 if (ret != 0 && ret != -ENOTSUPP)
1808 dev_warn(codec_dai->dev,
1809 "ASoC: Failed to set DAI format: %d\n",
1810 ret);
1811 }
f04209a7
MB
1812 }
1813
1814 /* If this is a regular CPU link there will be a platform */
fe33d4c5
SW
1815 if (dai_fmt &&
1816 (dai_link->platform_name || dai_link->platform_of_node)) {
f04209a7
MB
1817 ret = snd_soc_dai_set_fmt(card->rtd[i].cpu_dai,
1818 dai_fmt);
1819 if (ret != 0 && ret != -ENOTSUPP)
1820 dev_warn(card->rtd[i].cpu_dai->dev,
f110bfc7 1821 "ASoC: Failed to set DAI format: %d\n",
f04209a7
MB
1822 ret);
1823 } else if (dai_fmt) {
1824 /* Flip the polarity for the "CPU" end */
1825 dai_fmt &= ~SND_SOC_DAIFMT_MASTER_MASK;
1826 switch (dai_link->dai_fmt &
1827 SND_SOC_DAIFMT_MASTER_MASK) {
1828 case SND_SOC_DAIFMT_CBM_CFM:
1829 dai_fmt |= SND_SOC_DAIFMT_CBS_CFS;
1830 break;
1831 case SND_SOC_DAIFMT_CBM_CFS:
1832 dai_fmt |= SND_SOC_DAIFMT_CBS_CFM;
1833 break;
1834 case SND_SOC_DAIFMT_CBS_CFM:
1835 dai_fmt |= SND_SOC_DAIFMT_CBM_CFS;
1836 break;
1837 case SND_SOC_DAIFMT_CBS_CFS:
1838 dai_fmt |= SND_SOC_DAIFMT_CBM_CFM;
1839 break;
1840 }
75d9ac46
MB
1841
1842 ret = snd_soc_dai_set_fmt(card->rtd[i].cpu_dai,
f04209a7 1843 dai_fmt);
5e4ba569 1844 if (ret != 0 && ret != -ENOTSUPP)
75d9ac46 1845 dev_warn(card->rtd[i].cpu_dai->dev,
f110bfc7 1846 "ASoC: Failed to set DAI format: %d\n",
75d9ac46
MB
1847 ret);
1848 }
1849 }
1850
f0fba2ad 1851 snprintf(card->snd_card->shortname, sizeof(card->snd_card->shortname),
f0fba2ad 1852 "%s", card->name);
22de71ba
LG
1853 snprintf(card->snd_card->longname, sizeof(card->snd_card->longname),
1854 "%s", card->long_name ? card->long_name : card->name);
f0e8ed85
MB
1855 snprintf(card->snd_card->driver, sizeof(card->snd_card->driver),
1856 "%s", card->driver_name ? card->driver_name : card->name);
1857 for (i = 0; i < ARRAY_SIZE(card->snd_card->driver); i++) {
1858 switch (card->snd_card->driver[i]) {
1859 case '_':
1860 case '-':
1861 case '\0':
1862 break;
1863 default:
1864 if (!isalnum(card->snd_card->driver[i]))
1865 card->snd_card->driver[i] = '_';
1866 break;
1867 }
1868 }
f0fba2ad 1869
28e9ad92
MB
1870 if (card->late_probe) {
1871 ret = card->late_probe(card);
1872 if (ret < 0) {
f110bfc7 1873 dev_err(card->dev, "ASoC: %s late_probe() failed: %d\n",
28e9ad92
MB
1874 card->name, ret);
1875 goto probe_aux_dev_err;
1876 }
1877 }
1878
1633281b 1879 if (card->fully_routed)
7df37884 1880 snd_soc_dapm_auto_nc_pins(card);
1633281b 1881
824ef826 1882 snd_soc_dapm_new_widgets(card);
8c193b8d 1883
f0fba2ad
LG
1884 ret = snd_card_register(card->snd_card);
1885 if (ret < 0) {
f110bfc7
LG
1886 dev_err(card->dev, "ASoC: failed to register soundcard %d\n",
1887 ret);
6b3ed785 1888 goto probe_aux_dev_err;
db2a4165
FM
1889 }
1890
f0fba2ad
LG
1891#ifdef CONFIG_SND_SOC_AC97_BUS
1892 /* register any AC97 codecs */
1893 for (i = 0; i < card->num_rtd; i++) {
6b3ed785
AL
1894 ret = soc_register_ac97_dai_link(&card->rtd[i]);
1895 if (ret < 0) {
10e8aa9a
MM
1896 dev_err(card->dev,
1897 "ASoC: failed to register AC97: %d\n", ret);
6b3ed785 1898 while (--i >= 0)
02c9c7b9 1899 soc_unregister_ac97_dai_link(&card->rtd[i]);
6b3ed785 1900 goto probe_aux_dev_err;
f0fba2ad 1901 }
6b3ed785 1902 }
f0fba2ad
LG
1903#endif
1904
1905 card->instantiated = 1;
4f4c0072 1906 snd_soc_dapm_sync(&card->dapm);
f0fba2ad 1907 mutex_unlock(&card->mutex);
b19e6e7b
MB
1908
1909 return 0;
f0fba2ad 1910
2eea392d
JN
1911probe_aux_dev_err:
1912 for (i = 0; i < card->num_aux_devs; i++)
1913 soc_remove_aux_dev(card, i);
1914
f0fba2ad 1915probe_dai_err:
0671fd8e 1916 soc_remove_dai_links(card);
f0fba2ad
LG
1917
1918card_probe_error:
87506549 1919 if (card->remove)
e7361ec4 1920 card->remove(card);
f0fba2ad
LG
1921
1922 snd_card_free(card->snd_card);
1923
b19e6e7b 1924base_error:
f0fba2ad 1925 mutex_unlock(&card->mutex);
db2a4165 1926
b19e6e7b 1927 return ret;
435c5e25
MB
1928}
1929
1930/* probes a new socdev */
1931static int soc_probe(struct platform_device *pdev)
1932{
f0fba2ad 1933 struct snd_soc_card *card = platform_get_drvdata(pdev);
435c5e25 1934
70a7ca34
VK
1935 /*
1936 * no card, so machine driver should be registering card
1937 * we should not be here in that case so ret error
1938 */
1939 if (!card)
1940 return -EINVAL;
1941
fe4085e8 1942 dev_warn(&pdev->dev,
f110bfc7 1943 "ASoC: machine %s should use snd_soc_register_card()\n",
fe4085e8
MB
1944 card->name);
1945
435c5e25
MB
1946 /* Bodge while we unpick instantiation */
1947 card->dev = &pdev->dev;
f0fba2ad 1948
28d528c8 1949 return snd_soc_register_card(card);
db2a4165
FM
1950}
1951
b0e26485 1952static int soc_cleanup_card_resources(struct snd_soc_card *card)
db2a4165
FM
1953{
1954 int i;
db2a4165 1955
b0e26485
VK
1956 /* make sure any delayed work runs */
1957 for (i = 0; i < card->num_rtd; i++) {
1958 struct snd_soc_pcm_runtime *rtd = &card->rtd[i];
43829731 1959 flush_delayed_work(&rtd->delayed_work);
b0e26485 1960 }
914dc182 1961
b0e26485
VK
1962 /* remove auxiliary devices */
1963 for (i = 0; i < card->num_aux_devs; i++)
1964 soc_remove_aux_dev(card, i);
db2a4165 1965
b0e26485 1966 /* remove and free each DAI */
0671fd8e 1967 soc_remove_dai_links(card);
2eea392d 1968
b0e26485 1969 soc_cleanup_card_debugfs(card);
f0fba2ad 1970
b0e26485
VK
1971 /* remove the card */
1972 if (card->remove)
e7361ec4 1973 card->remove(card);
a6052154 1974
0aaae527
LPC
1975 snd_soc_dapm_free(&card->dapm);
1976
b0e26485
VK
1977 snd_card_free(card->snd_card);
1978 return 0;
1979
1980}
1981
1982/* removes a socdev */
1983static int soc_remove(struct platform_device *pdev)
1984{
1985 struct snd_soc_card *card = platform_get_drvdata(pdev);
db2a4165 1986
c5af3a2e 1987 snd_soc_unregister_card(card);
db2a4165
FM
1988 return 0;
1989}
1990
6f8ab4ac 1991int snd_soc_poweroff(struct device *dev)
51737470 1992{
6f8ab4ac 1993 struct snd_soc_card *card = dev_get_drvdata(dev);
f0fba2ad 1994 int i;
51737470
MB
1995
1996 if (!card->instantiated)
416356fc 1997 return 0;
51737470
MB
1998
1999 /* Flush out pmdown_time work - we actually do want to run it
2000 * now, we're shutting down so no imminent restart. */
f0fba2ad
LG
2001 for (i = 0; i < card->num_rtd; i++) {
2002 struct snd_soc_pcm_runtime *rtd = &card->rtd[i];
43829731 2003 flush_delayed_work(&rtd->delayed_work);
f0fba2ad 2004 }
51737470 2005
f0fba2ad 2006 snd_soc_dapm_shutdown(card);
416356fc 2007
988e8cc4
NC
2008 /* deactivate pins to sleep state */
2009 for (i = 0; i < card->num_rtd; i++) {
88bd870f
BC
2010 struct snd_soc_pcm_runtime *rtd = &card->rtd[i];
2011 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
2012 int j;
2013
988e8cc4 2014 pinctrl_pm_select_sleep_state(cpu_dai->dev);
88bd870f
BC
2015 for (j = 0; j < rtd->num_codecs; j++) {
2016 struct snd_soc_dai *codec_dai = rtd->codec_dais[j];
2017 pinctrl_pm_select_sleep_state(codec_dai->dev);
2018 }
988e8cc4
NC
2019 }
2020
416356fc 2021 return 0;
51737470 2022}
6f8ab4ac 2023EXPORT_SYMBOL_GPL(snd_soc_poweroff);
51737470 2024
6f8ab4ac 2025const struct dev_pm_ops snd_soc_pm_ops = {
b1dd5897
VK
2026 .suspend = snd_soc_suspend,
2027 .resume = snd_soc_resume,
2028 .freeze = snd_soc_suspend,
2029 .thaw = snd_soc_resume,
6f8ab4ac 2030 .poweroff = snd_soc_poweroff,
b1dd5897 2031 .restore = snd_soc_resume,
416356fc 2032};
deb2607e 2033EXPORT_SYMBOL_GPL(snd_soc_pm_ops);
416356fc 2034
db2a4165
FM
2035/* ASoC platform driver */
2036static struct platform_driver soc_driver = {
2037 .driver = {
2038 .name = "soc-audio",
8b45a209 2039 .owner = THIS_MODULE,
6f8ab4ac 2040 .pm = &snd_soc_pm_ops,
db2a4165
FM
2041 },
2042 .probe = soc_probe,
2043 .remove = soc_remove,
db2a4165
FM
2044};
2045
db2a4165
FM
2046/**
2047 * snd_soc_new_ac97_codec - initailise AC97 device
2048 * @codec: audio codec
2049 * @ops: AC97 bus operations
2050 * @num: AC97 codec number
2051 *
2052 * Initialises AC97 codec resources for use by ad-hoc devices only.
2053 */
2054int snd_soc_new_ac97_codec(struct snd_soc_codec *codec,
2055 struct snd_ac97_bus_ops *ops, int num)
2056{
2057 mutex_lock(&codec->mutex);
2058
2059 codec->ac97 = kzalloc(sizeof(struct snd_ac97), GFP_KERNEL);
2060 if (codec->ac97 == NULL) {
2061 mutex_unlock(&codec->mutex);
2062 return -ENOMEM;
2063 }
2064
2065 codec->ac97->bus = kzalloc(sizeof(struct snd_ac97_bus), GFP_KERNEL);
2066 if (codec->ac97->bus == NULL) {
2067 kfree(codec->ac97);
2068 codec->ac97 = NULL;
2069 mutex_unlock(&codec->mutex);
2070 return -ENOMEM;
2071 }
2072
2073 codec->ac97->bus->ops = ops;
2074 codec->ac97->num = num;
0562f788
MW
2075
2076 /*
2077 * Mark the AC97 device to be created by us. This way we ensure that the
2078 * device will be registered with the device subsystem later on.
2079 */
2080 codec->ac97_created = 1;
2081
db2a4165
FM
2082 mutex_unlock(&codec->mutex);
2083 return 0;
2084}
2085EXPORT_SYMBOL_GPL(snd_soc_new_ac97_codec);
2086
741a509f
MP
2087static struct snd_ac97_reset_cfg snd_ac97_rst_cfg;
2088
2089static void snd_soc_ac97_warm_reset(struct snd_ac97 *ac97)
2090{
2091 struct pinctrl *pctl = snd_ac97_rst_cfg.pctl;
2092
2093 pinctrl_select_state(pctl, snd_ac97_rst_cfg.pstate_warm_reset);
2094
2095 gpio_direction_output(snd_ac97_rst_cfg.gpio_sync, 1);
2096
2097 udelay(10);
2098
2099 gpio_direction_output(snd_ac97_rst_cfg.gpio_sync, 0);
2100
2101 pinctrl_select_state(pctl, snd_ac97_rst_cfg.pstate_run);
2102 msleep(2);
2103}
2104
2105static void snd_soc_ac97_reset(struct snd_ac97 *ac97)
2106{
2107 struct pinctrl *pctl = snd_ac97_rst_cfg.pctl;
2108
2109 pinctrl_select_state(pctl, snd_ac97_rst_cfg.pstate_reset);
2110
2111 gpio_direction_output(snd_ac97_rst_cfg.gpio_sync, 0);
2112 gpio_direction_output(snd_ac97_rst_cfg.gpio_sdata, 0);
2113 gpio_direction_output(snd_ac97_rst_cfg.gpio_reset, 0);
2114
2115 udelay(10);
2116
2117 gpio_direction_output(snd_ac97_rst_cfg.gpio_reset, 1);
2118
2119 pinctrl_select_state(pctl, snd_ac97_rst_cfg.pstate_run);
2120 msleep(2);
2121}
2122
2123static int snd_soc_ac97_parse_pinctl(struct device *dev,
2124 struct snd_ac97_reset_cfg *cfg)
2125{
2126 struct pinctrl *p;
2127 struct pinctrl_state *state;
2128 int gpio;
2129 int ret;
2130
2131 p = devm_pinctrl_get(dev);
2132 if (IS_ERR(p)) {
2133 dev_err(dev, "Failed to get pinctrl\n");
b7580cde 2134 return PTR_ERR(p);
741a509f
MP
2135 }
2136 cfg->pctl = p;
2137
2138 state = pinctrl_lookup_state(p, "ac97-reset");
2139 if (IS_ERR(state)) {
2140 dev_err(dev, "Can't find pinctrl state ac97-reset\n");
b7580cde 2141 return PTR_ERR(state);
741a509f
MP
2142 }
2143 cfg->pstate_reset = state;
2144
2145 state = pinctrl_lookup_state(p, "ac97-warm-reset");
2146 if (IS_ERR(state)) {
2147 dev_err(dev, "Can't find pinctrl state ac97-warm-reset\n");
b7580cde 2148 return PTR_ERR(state);
741a509f
MP
2149 }
2150 cfg->pstate_warm_reset = state;
2151
2152 state = pinctrl_lookup_state(p, "ac97-running");
2153 if (IS_ERR(state)) {
2154 dev_err(dev, "Can't find pinctrl state ac97-running\n");
b7580cde 2155 return PTR_ERR(state);
741a509f
MP
2156 }
2157 cfg->pstate_run = state;
2158
2159 gpio = of_get_named_gpio(dev->of_node, "ac97-gpios", 0);
2160 if (gpio < 0) {
2161 dev_err(dev, "Can't find ac97-sync gpio\n");
2162 return gpio;
2163 }
2164 ret = devm_gpio_request(dev, gpio, "AC97 link sync");
2165 if (ret) {
2166 dev_err(dev, "Failed requesting ac97-sync gpio\n");
2167 return ret;
2168 }
2169 cfg->gpio_sync = gpio;
2170
2171 gpio = of_get_named_gpio(dev->of_node, "ac97-gpios", 1);
2172 if (gpio < 0) {
2173 dev_err(dev, "Can't find ac97-sdata gpio %d\n", gpio);
2174 return gpio;
2175 }
2176 ret = devm_gpio_request(dev, gpio, "AC97 link sdata");
2177 if (ret) {
2178 dev_err(dev, "Failed requesting ac97-sdata gpio\n");
2179 return ret;
2180 }
2181 cfg->gpio_sdata = gpio;
2182
2183 gpio = of_get_named_gpio(dev->of_node, "ac97-gpios", 2);
2184 if (gpio < 0) {
2185 dev_err(dev, "Can't find ac97-reset gpio\n");
2186 return gpio;
2187 }
2188 ret = devm_gpio_request(dev, gpio, "AC97 link reset");
2189 if (ret) {
2190 dev_err(dev, "Failed requesting ac97-reset gpio\n");
2191 return ret;
2192 }
2193 cfg->gpio_reset = gpio;
2194
2195 return 0;
2196}
2197
b047e1cc 2198struct snd_ac97_bus_ops *soc_ac97_ops;
f74b5e25 2199EXPORT_SYMBOL_GPL(soc_ac97_ops);
b047e1cc
MB
2200
2201int snd_soc_set_ac97_ops(struct snd_ac97_bus_ops *ops)
2202{
2203 if (ops == soc_ac97_ops)
2204 return 0;
2205
2206 if (soc_ac97_ops && ops)
2207 return -EBUSY;
2208
2209 soc_ac97_ops = ops;
2210
2211 return 0;
2212}
2213EXPORT_SYMBOL_GPL(snd_soc_set_ac97_ops);
2214
741a509f
MP
2215/**
2216 * snd_soc_set_ac97_ops_of_reset - Set ac97 ops with generic ac97 reset functions
2217 *
2218 * This function sets the reset and warm_reset properties of ops and parses
2219 * the device node of pdev to get pinctrl states and gpio numbers to use.
2220 */
2221int snd_soc_set_ac97_ops_of_reset(struct snd_ac97_bus_ops *ops,
2222 struct platform_device *pdev)
2223{
2224 struct device *dev = &pdev->dev;
2225 struct snd_ac97_reset_cfg cfg;
2226 int ret;
2227
2228 ret = snd_soc_ac97_parse_pinctl(dev, &cfg);
2229 if (ret)
2230 return ret;
2231
2232 ret = snd_soc_set_ac97_ops(ops);
2233 if (ret)
2234 return ret;
2235
2236 ops->warm_reset = snd_soc_ac97_warm_reset;
2237 ops->reset = snd_soc_ac97_reset;
2238
2239 snd_ac97_rst_cfg = cfg;
2240 return 0;
2241}
2242EXPORT_SYMBOL_GPL(snd_soc_set_ac97_ops_of_reset);
2243
db2a4165
FM
2244/**
2245 * snd_soc_free_ac97_codec - free AC97 codec device
2246 * @codec: audio codec
2247 *
2248 * Frees AC97 codec device resources.
2249 */
2250void snd_soc_free_ac97_codec(struct snd_soc_codec *codec)
2251{
2252 mutex_lock(&codec->mutex);
f0fba2ad 2253#ifdef CONFIG_SND_SOC_AC97_BUS
02c9c7b9 2254 soc_unregister_ac97_codec(codec);
f0fba2ad 2255#endif
db2a4165
FM
2256 kfree(codec->ac97->bus);
2257 kfree(codec->ac97);
2258 codec->ac97 = NULL;
0562f788 2259 codec->ac97_created = 0;
db2a4165
FM
2260 mutex_unlock(&codec->mutex);
2261}
2262EXPORT_SYMBOL_GPL(snd_soc_free_ac97_codec);
2263
db2a4165
FM
2264/**
2265 * snd_soc_cnew - create new control
2266 * @_template: control template
2267 * @data: control private data
ac11a2b3 2268 * @long_name: control long name
efb7ac3f 2269 * @prefix: control name prefix
db2a4165
FM
2270 *
2271 * Create a new mixer control from a template control.
2272 *
2273 * Returns 0 for success, else error.
2274 */
2275struct snd_kcontrol *snd_soc_cnew(const struct snd_kcontrol_new *_template,
3056557f 2276 void *data, const char *long_name,
efb7ac3f 2277 const char *prefix)
db2a4165
FM
2278{
2279 struct snd_kcontrol_new template;
efb7ac3f
MB
2280 struct snd_kcontrol *kcontrol;
2281 char *name = NULL;
db2a4165
FM
2282
2283 memcpy(&template, _template, sizeof(template));
db2a4165
FM
2284 template.index = 0;
2285
efb7ac3f
MB
2286 if (!long_name)
2287 long_name = template.name;
2288
2289 if (prefix) {
2b581074 2290 name = kasprintf(GFP_KERNEL, "%s %s", prefix, long_name);
efb7ac3f
MB
2291 if (!name)
2292 return NULL;
2293
efb7ac3f
MB
2294 template.name = name;
2295 } else {
2296 template.name = long_name;
2297 }
2298
2299 kcontrol = snd_ctl_new1(&template, data);
2300
2301 kfree(name);
2302
2303 return kcontrol;
db2a4165
FM
2304}
2305EXPORT_SYMBOL_GPL(snd_soc_cnew);
2306
022658be
LG
2307static int snd_soc_add_controls(struct snd_card *card, struct device *dev,
2308 const struct snd_kcontrol_new *controls, int num_controls,
2309 const char *prefix, void *data)
2310{
2311 int err, i;
2312
2313 for (i = 0; i < num_controls; i++) {
2314 const struct snd_kcontrol_new *control = &controls[i];
2315 err = snd_ctl_add(card, snd_soc_cnew(control, data,
2316 control->name, prefix));
2317 if (err < 0) {
f110bfc7
LG
2318 dev_err(dev, "ASoC: Failed to add %s: %d\n",
2319 control->name, err);
022658be
LG
2320 return err;
2321 }
2322 }
2323
2324 return 0;
2325}
2326
4fefd698
DP
2327struct snd_kcontrol *snd_soc_card_get_kcontrol(struct snd_soc_card *soc_card,
2328 const char *name)
2329{
2330 struct snd_card *card = soc_card->snd_card;
2331 struct snd_kcontrol *kctl;
2332
2333 if (unlikely(!name))
2334 return NULL;
2335
2336 list_for_each_entry(kctl, &card->controls, list)
2337 if (!strncmp(kctl->id.name, name, sizeof(kctl->id.name)))
2338 return kctl;
2339 return NULL;
2340}
2341EXPORT_SYMBOL_GPL(snd_soc_card_get_kcontrol);
2342
0f2780ad
LPC
2343/**
2344 * snd_soc_add_component_controls - Add an array of controls to a component.
2345 *
2346 * @component: Component to add controls to
2347 * @controls: Array of controls to add
2348 * @num_controls: Number of elements in the array
2349 *
2350 * Return: 0 for success, else error.
2351 */
2352int snd_soc_add_component_controls(struct snd_soc_component *component,
2353 const struct snd_kcontrol_new *controls, unsigned int num_controls)
2354{
2355 struct snd_card *card = component->card->snd_card;
2356
2357 return snd_soc_add_controls(card, component->dev, controls,
2358 num_controls, component->name_prefix, component);
2359}
2360EXPORT_SYMBOL_GPL(snd_soc_add_component_controls);
2361
3e8e1952 2362/**
022658be
LG
2363 * snd_soc_add_codec_controls - add an array of controls to a codec.
2364 * Convenience function to add a list of controls. Many codecs were
3e8e1952
IM
2365 * duplicating this code.
2366 *
2367 * @codec: codec to add controls to
2368 * @controls: array of controls to add
2369 * @num_controls: number of elements in the array
2370 *
2371 * Return 0 for success, else error.
2372 */
022658be 2373int snd_soc_add_codec_controls(struct snd_soc_codec *codec,
0f2780ad 2374 const struct snd_kcontrol_new *controls, unsigned int num_controls)
3e8e1952 2375{
0f2780ad
LPC
2376 return snd_soc_add_component_controls(&codec->component, controls,
2377 num_controls);
3e8e1952 2378}
022658be 2379EXPORT_SYMBOL_GPL(snd_soc_add_codec_controls);
3e8e1952 2380
a491a5c8
LG
2381/**
2382 * snd_soc_add_platform_controls - add an array of controls to a platform.
022658be 2383 * Convenience function to add a list of controls.
a491a5c8
LG
2384 *
2385 * @platform: platform to add controls to
2386 * @controls: array of controls to add
2387 * @num_controls: number of elements in the array
2388 *
2389 * Return 0 for success, else error.
2390 */
2391int snd_soc_add_platform_controls(struct snd_soc_platform *platform,
0f2780ad 2392 const struct snd_kcontrol_new *controls, unsigned int num_controls)
a491a5c8 2393{
0f2780ad
LPC
2394 return snd_soc_add_component_controls(&platform->component, controls,
2395 num_controls);
a491a5c8
LG
2396}
2397EXPORT_SYMBOL_GPL(snd_soc_add_platform_controls);
2398
022658be
LG
2399/**
2400 * snd_soc_add_card_controls - add an array of controls to a SoC card.
2401 * Convenience function to add a list of controls.
2402 *
2403 * @soc_card: SoC card to add controls to
2404 * @controls: array of controls to add
2405 * @num_controls: number of elements in the array
2406 *
2407 * Return 0 for success, else error.
2408 */
2409int snd_soc_add_card_controls(struct snd_soc_card *soc_card,
2410 const struct snd_kcontrol_new *controls, int num_controls)
2411{
2412 struct snd_card *card = soc_card->snd_card;
2413
2414 return snd_soc_add_controls(card, soc_card->dev, controls, num_controls,
2415 NULL, soc_card);
2416}
2417EXPORT_SYMBOL_GPL(snd_soc_add_card_controls);
2418
2419/**
2420 * snd_soc_add_dai_controls - add an array of controls to a DAI.
2421 * Convienience function to add a list of controls.
2422 *
2423 * @dai: DAI to add controls to
2424 * @controls: array of controls to add
2425 * @num_controls: number of elements in the array
2426 *
2427 * Return 0 for success, else error.
2428 */
2429int snd_soc_add_dai_controls(struct snd_soc_dai *dai,
2430 const struct snd_kcontrol_new *controls, int num_controls)
2431{
2432 struct snd_card *card = dai->card->snd_card;
2433
2434 return snd_soc_add_controls(card, dai->dev, controls, num_controls,
2435 NULL, dai);
2436}
2437EXPORT_SYMBOL_GPL(snd_soc_add_dai_controls);
2438
db2a4165
FM
2439/**
2440 * snd_soc_info_enum_double - enumerated double mixer info callback
2441 * @kcontrol: mixer control
2442 * @uinfo: control element information
2443 *
2444 * Callback to provide information about a double enumerated
2445 * mixer control.
2446 *
2447 * Returns 0 for success.
2448 */
2449int snd_soc_info_enum_double(struct snd_kcontrol *kcontrol,
2450 struct snd_ctl_elem_info *uinfo)
2451{
2452 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
2453
2454 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
2455 uinfo->count = e->shift_l == e->shift_r ? 1 : 2;
9a8d38db 2456 uinfo->value.enumerated.items = e->items;
db2a4165 2457
9a8d38db
TI
2458 if (uinfo->value.enumerated.item >= e->items)
2459 uinfo->value.enumerated.item = e->items - 1;
a19685cb
TI
2460 strlcpy(uinfo->value.enumerated.name,
2461 e->texts[uinfo->value.enumerated.item],
2462 sizeof(uinfo->value.enumerated.name));
db2a4165
FM
2463 return 0;
2464}
2465EXPORT_SYMBOL_GPL(snd_soc_info_enum_double);
2466
2467/**
2468 * snd_soc_get_enum_double - enumerated double mixer get callback
2469 * @kcontrol: mixer control
ac11a2b3 2470 * @ucontrol: control element information
db2a4165
FM
2471 *
2472 * Callback to get the value of a double enumerated mixer.
2473 *
2474 * Returns 0 for success.
2475 */
2476int snd_soc_get_enum_double(struct snd_kcontrol *kcontrol,
2477 struct snd_ctl_elem_value *ucontrol)
2478{
907fe36a 2479 struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
db2a4165 2480 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
29ae2fa5
LPC
2481 unsigned int val, item;
2482 unsigned int reg_val;
907fe36a 2483 int ret;
db2a4165 2484
907fe36a
LPC
2485 ret = snd_soc_component_read(component, e->reg, &reg_val);
2486 if (ret)
2487 return ret;
29ae2fa5
LPC
2488 val = (reg_val >> e->shift_l) & e->mask;
2489 item = snd_soc_enum_val_to_item(e, val);
2490 ucontrol->value.enumerated.item[0] = item;
2491 if (e->shift_l != e->shift_r) {
2492 val = (reg_val >> e->shift_l) & e->mask;
2493 item = snd_soc_enum_val_to_item(e, val);
2494 ucontrol->value.enumerated.item[1] = item;
2495 }
db2a4165
FM
2496
2497 return 0;
2498}
2499EXPORT_SYMBOL_GPL(snd_soc_get_enum_double);
2500
2501/**
2502 * snd_soc_put_enum_double - enumerated double mixer put callback
2503 * @kcontrol: mixer control
ac11a2b3 2504 * @ucontrol: control element information
db2a4165
FM
2505 *
2506 * Callback to set the value of a double enumerated mixer.
2507 *
2508 * Returns 0 for success.
2509 */
2510int snd_soc_put_enum_double(struct snd_kcontrol *kcontrol,
2511 struct snd_ctl_elem_value *ucontrol)
2512{
907fe36a 2513 struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
db2a4165 2514 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
29ae2fa5 2515 unsigned int *item = ucontrol->value.enumerated.item;
46f5822f 2516 unsigned int val;
86767b7d 2517 unsigned int mask;
db2a4165 2518
29ae2fa5 2519 if (item[0] >= e->items)
db2a4165 2520 return -EINVAL;
29ae2fa5 2521 val = snd_soc_enum_item_to_val(e, item[0]) << e->shift_l;
86767b7d 2522 mask = e->mask << e->shift_l;
db2a4165 2523 if (e->shift_l != e->shift_r) {
29ae2fa5 2524 if (item[1] >= e->items)
db2a4165 2525 return -EINVAL;
29ae2fa5 2526 val |= snd_soc_enum_item_to_val(e, item[1]) << e->shift_r;
86767b7d 2527 mask |= e->mask << e->shift_r;
db2a4165
FM
2528 }
2529
907fe36a 2530 return snd_soc_component_update_bits(component, e->reg, mask, val);
db2a4165
FM
2531}
2532EXPORT_SYMBOL_GPL(snd_soc_put_enum_double);
2533
2e72f8e3 2534/**
f227b88f 2535 * snd_soc_read_signed - Read a codec register and interprete as signed value
907fe36a 2536 * @component: component
f227b88f
MP
2537 * @reg: Register to read
2538 * @mask: Mask to use after shifting the register value
2539 * @shift: Right shift of register value
2540 * @sign_bit: Bit that describes if a number is negative or not.
907fe36a 2541 * @signed_val: Pointer to where the read value should be stored
2e72f8e3 2542 *
f227b88f
MP
2543 * This functions reads a codec register. The register value is shifted right
2544 * by 'shift' bits and masked with the given 'mask'. Afterwards it translates
2545 * the given registervalue into a signed integer if sign_bit is non-zero.
2e72f8e3 2546 *
907fe36a 2547 * Returns 0 on sucess, otherwise an error value
2e72f8e3 2548 */
907fe36a
LPC
2549static int snd_soc_read_signed(struct snd_soc_component *component,
2550 unsigned int reg, unsigned int mask, unsigned int shift,
2551 unsigned int sign_bit, int *signed_val)
2e72f8e3 2552{
f227b88f
MP
2553 int ret;
2554 unsigned int val;
2e72f8e3 2555
907fe36a
LPC
2556 ret = snd_soc_component_read(component, reg, &val);
2557 if (ret < 0)
2558 return ret;
2559
2560 val = (val >> shift) & mask;
2e72f8e3 2561
907fe36a
LPC
2562 if (!sign_bit) {
2563 *signed_val = val;
2564 return 0;
2565 }
2e72f8e3 2566
f227b88f 2567 /* non-negative number */
907fe36a
LPC
2568 if (!(val & BIT(sign_bit))) {
2569 *signed_val = val;
2570 return 0;
2571 }
2e72f8e3 2572
f227b88f 2573 ret = val;
2e72f8e3 2574
f227b88f
MP
2575 /*
2576 * The register most probably does not contain a full-sized int.
2577 * Instead we have an arbitrary number of bits in a signed
2578 * representation which has to be translated into a full-sized int.
2579 * This is done by filling up all bits above the sign-bit.
2580 */
2581 ret |= ~((int)(BIT(sign_bit) - 1));
2582
907fe36a
LPC
2583 *signed_val = ret;
2584
2585 return 0;
2e72f8e3 2586}
2e72f8e3 2587
db2a4165
FM
2588/**
2589 * snd_soc_info_volsw - single mixer info callback
2590 * @kcontrol: mixer control
2591 * @uinfo: control element information
2592 *
e8f5a103
PU
2593 * Callback to provide information about a single mixer control, or a double
2594 * mixer control that spans 2 registers.
db2a4165
FM
2595 *
2596 * Returns 0 for success.
2597 */
2598int snd_soc_info_volsw(struct snd_kcontrol *kcontrol,
2599 struct snd_ctl_elem_info *uinfo)
2600{
4eaa9819
JS
2601 struct soc_mixer_control *mc =
2602 (struct soc_mixer_control *)kcontrol->private_value;
d11bb4a9 2603 int platform_max;
db2a4165 2604
d11bb4a9
PU
2605 if (!mc->platform_max)
2606 mc->platform_max = mc->max;
2607 platform_max = mc->platform_max;
2608
2609 if (platform_max == 1 && !strstr(kcontrol->id.name, " Volume"))
a7a4ac86
PZ
2610 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
2611 else
2612 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2613
e8f5a103 2614 uinfo->count = snd_soc_volsw_is_stereo(mc) ? 2 : 1;
db2a4165 2615 uinfo->value.integer.min = 0;
f227b88f 2616 uinfo->value.integer.max = platform_max - mc->min;
db2a4165
FM
2617 return 0;
2618}
2619EXPORT_SYMBOL_GPL(snd_soc_info_volsw);
2620
2621/**
2622 * snd_soc_get_volsw - single mixer get callback
2623 * @kcontrol: mixer control
ac11a2b3 2624 * @ucontrol: control element information
db2a4165 2625 *
f7915d99
PU
2626 * Callback to get the value of a single mixer control, or a double mixer
2627 * control that spans 2 registers.
db2a4165
FM
2628 *
2629 * Returns 0 for success.
2630 */
2631int snd_soc_get_volsw(struct snd_kcontrol *kcontrol,
2632 struct snd_ctl_elem_value *ucontrol)
2633{
907fe36a 2634 struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
4eaa9819
JS
2635 struct soc_mixer_control *mc =
2636 (struct soc_mixer_control *)kcontrol->private_value;
815ecf8d 2637 unsigned int reg = mc->reg;
f7915d99 2638 unsigned int reg2 = mc->rreg;
815ecf8d
JS
2639 unsigned int shift = mc->shift;
2640 unsigned int rshift = mc->rshift;
4eaa9819 2641 int max = mc->max;
f227b88f
MP
2642 int min = mc->min;
2643 int sign_bit = mc->sign_bit;
815ecf8d
JS
2644 unsigned int mask = (1 << fls(max)) - 1;
2645 unsigned int invert = mc->invert;
907fe36a
LPC
2646 int val;
2647 int ret;
db2a4165 2648
f227b88f
MP
2649 if (sign_bit)
2650 mask = BIT(sign_bit + 1) - 1;
2651
907fe36a
LPC
2652 ret = snd_soc_read_signed(component, reg, mask, shift, sign_bit, &val);
2653 if (ret)
2654 return ret;
2655
2656 ucontrol->value.integer.value[0] = val - min;
f7915d99 2657 if (invert)
db2a4165 2658 ucontrol->value.integer.value[0] =
a7a4ac86 2659 max - ucontrol->value.integer.value[0];
f7915d99
PU
2660
2661 if (snd_soc_volsw_is_stereo(mc)) {
2662 if (reg == reg2)
907fe36a
LPC
2663 ret = snd_soc_read_signed(component, reg, mask, rshift,
2664 sign_bit, &val);
f7915d99 2665 else
907fe36a
LPC
2666 ret = snd_soc_read_signed(component, reg2, mask, shift,
2667 sign_bit, &val);
2668 if (ret)
2669 return ret;
2670
2671 ucontrol->value.integer.value[1] = val - min;
f7915d99 2672 if (invert)
db2a4165 2673 ucontrol->value.integer.value[1] =
a7a4ac86 2674 max - ucontrol->value.integer.value[1];
db2a4165
FM
2675 }
2676
2677 return 0;
2678}
2679EXPORT_SYMBOL_GPL(snd_soc_get_volsw);
2680
2681/**
2682 * snd_soc_put_volsw - single mixer put callback
2683 * @kcontrol: mixer control
ac11a2b3 2684 * @ucontrol: control element information
db2a4165 2685 *
974815ba
PU
2686 * Callback to set the value of a single mixer control, or a double mixer
2687 * control that spans 2 registers.
db2a4165
FM
2688 *
2689 * Returns 0 for success.
2690 */
2691int snd_soc_put_volsw(struct snd_kcontrol *kcontrol,
2692 struct snd_ctl_elem_value *ucontrol)
2693{
907fe36a 2694 struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
4eaa9819
JS
2695 struct soc_mixer_control *mc =
2696 (struct soc_mixer_control *)kcontrol->private_value;
815ecf8d 2697 unsigned int reg = mc->reg;
974815ba 2698 unsigned int reg2 = mc->rreg;
815ecf8d
JS
2699 unsigned int shift = mc->shift;
2700 unsigned int rshift = mc->rshift;
4eaa9819 2701 int max = mc->max;
f227b88f
MP
2702 int min = mc->min;
2703 unsigned int sign_bit = mc->sign_bit;
815ecf8d
JS
2704 unsigned int mask = (1 << fls(max)) - 1;
2705 unsigned int invert = mc->invert;
974815ba 2706 int err;
939d9f16 2707 bool type_2r = false;
974815ba
PU
2708 unsigned int val2 = 0;
2709 unsigned int val, val_mask;
db2a4165 2710
f227b88f
MP
2711 if (sign_bit)
2712 mask = BIT(sign_bit + 1) - 1;
2713
2714 val = ((ucontrol->value.integer.value[0] + min) & mask);
db2a4165 2715 if (invert)
a7a4ac86 2716 val = max - val;
db2a4165
FM
2717 val_mask = mask << shift;
2718 val = val << shift;
974815ba 2719 if (snd_soc_volsw_is_stereo(mc)) {
f227b88f 2720 val2 = ((ucontrol->value.integer.value[1] + min) & mask);
db2a4165 2721 if (invert)
a7a4ac86 2722 val2 = max - val2;
974815ba
PU
2723 if (reg == reg2) {
2724 val_mask |= mask << rshift;
2725 val |= val2 << rshift;
2726 } else {
2727 val2 = val2 << shift;
939d9f16 2728 type_2r = true;
974815ba 2729 }
db2a4165 2730 }
907fe36a 2731 err = snd_soc_component_update_bits(component, reg, val_mask, val);
3ff3f64b 2732 if (err < 0)
db2a4165
FM
2733 return err;
2734
974815ba 2735 if (type_2r)
907fe36a
LPC
2736 err = snd_soc_component_update_bits(component, reg2, val_mask,
2737 val2);
974815ba 2738
db2a4165
FM
2739 return err;
2740}
974815ba 2741EXPORT_SYMBOL_GPL(snd_soc_put_volsw);
db2a4165 2742
1d99f243
BA
2743/**
2744 * snd_soc_get_volsw_sx - single mixer get callback
2745 * @kcontrol: mixer control
2746 * @ucontrol: control element information
2747 *
2748 * Callback to get the value of a single mixer control, or a double mixer
2749 * control that spans 2 registers.
2750 *
2751 * Returns 0 for success.
2752 */
2753int snd_soc_get_volsw_sx(struct snd_kcontrol *kcontrol,
2754 struct snd_ctl_elem_value *ucontrol)
2755{
907fe36a 2756 struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
1d99f243
BA
2757 struct soc_mixer_control *mc =
2758 (struct soc_mixer_control *)kcontrol->private_value;
1d99f243
BA
2759 unsigned int reg = mc->reg;
2760 unsigned int reg2 = mc->rreg;
2761 unsigned int shift = mc->shift;
2762 unsigned int rshift = mc->rshift;
2763 int max = mc->max;
2764 int min = mc->min;
2765 int mask = (1 << (fls(min + max) - 1)) - 1;
907fe36a
LPC
2766 unsigned int val;
2767 int ret;
1d99f243 2768
907fe36a
LPC
2769 ret = snd_soc_component_read(component, reg, &val);
2770 if (ret < 0)
2771 return ret;
1d99f243 2772
907fe36a
LPC
2773 ucontrol->value.integer.value[0] = ((val >> shift) - min) & mask;
2774
2775 if (snd_soc_volsw_is_stereo(mc)) {
2776 ret = snd_soc_component_read(component, reg2, &val);
2777 if (ret < 0)
2778 return ret;
2779
2780 val = ((val >> rshift) - min) & mask;
2781 ucontrol->value.integer.value[1] = val;
2782 }
1d99f243
BA
2783
2784 return 0;
2785}
2786EXPORT_SYMBOL_GPL(snd_soc_get_volsw_sx);
2787
2788/**
2789 * snd_soc_put_volsw_sx - double mixer set callback
2790 * @kcontrol: mixer control
2791 * @uinfo: control element information
2792 *
2793 * Callback to set the value of a double mixer control that spans 2 registers.
2794 *
2795 * Returns 0 for success.
2796 */
2797int snd_soc_put_volsw_sx(struct snd_kcontrol *kcontrol,
2798 struct snd_ctl_elem_value *ucontrol)
2799{
907fe36a 2800 struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
1d99f243
BA
2801 struct soc_mixer_control *mc =
2802 (struct soc_mixer_control *)kcontrol->private_value;
2803
2804 unsigned int reg = mc->reg;
2805 unsigned int reg2 = mc->rreg;
2806 unsigned int shift = mc->shift;
2807 unsigned int rshift = mc->rshift;
2808 int max = mc->max;
2809 int min = mc->min;
2810 int mask = (1 << (fls(min + max) - 1)) - 1;
27f1d759 2811 int err = 0;
1a39019e 2812 unsigned int val, val_mask, val2 = 0;
1d99f243
BA
2813
2814 val_mask = mask << shift;
2815 val = (ucontrol->value.integer.value[0] + min) & mask;
2816 val = val << shift;
2817
907fe36a 2818 err = snd_soc_component_update_bits(component, reg, val_mask, val);
d055852e
MN
2819 if (err < 0)
2820 return err;
1d99f243
BA
2821
2822 if (snd_soc_volsw_is_stereo(mc)) {
2823 val_mask = mask << rshift;
2824 val2 = (ucontrol->value.integer.value[1] + min) & mask;
2825 val2 = val2 << rshift;
2826
907fe36a
LPC
2827 err = snd_soc_component_update_bits(component, reg2, val_mask,
2828 val2);
1d99f243 2829 }
907fe36a 2830 return err;
1d99f243
BA
2831}
2832EXPORT_SYMBOL_GPL(snd_soc_put_volsw_sx);
2833
e13ac2e9
MB
2834/**
2835 * snd_soc_info_volsw_s8 - signed mixer info callback
2836 * @kcontrol: mixer control
2837 * @uinfo: control element information
2838 *
2839 * Callback to provide information about a signed mixer control.
2840 *
2841 * Returns 0 for success.
2842 */
2843int snd_soc_info_volsw_s8(struct snd_kcontrol *kcontrol,
2844 struct snd_ctl_elem_info *uinfo)
2845{
4eaa9819
JS
2846 struct soc_mixer_control *mc =
2847 (struct soc_mixer_control *)kcontrol->private_value;
d11bb4a9 2848 int platform_max;
4eaa9819 2849 int min = mc->min;
e13ac2e9 2850
d11bb4a9
PU
2851 if (!mc->platform_max)
2852 mc->platform_max = mc->max;
2853 platform_max = mc->platform_max;
2854
e13ac2e9
MB
2855 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2856 uinfo->count = 2;
2857 uinfo->value.integer.min = 0;
d11bb4a9 2858 uinfo->value.integer.max = platform_max - min;
e13ac2e9
MB
2859 return 0;
2860}
2861EXPORT_SYMBOL_GPL(snd_soc_info_volsw_s8);
2862
2863/**
2864 * snd_soc_get_volsw_s8 - signed mixer get callback
2865 * @kcontrol: mixer control
ac11a2b3 2866 * @ucontrol: control element information
e13ac2e9
MB
2867 *
2868 * Callback to get the value of a signed mixer control.
2869 *
2870 * Returns 0 for success.
2871 */
2872int snd_soc_get_volsw_s8(struct snd_kcontrol *kcontrol,
2873 struct snd_ctl_elem_value *ucontrol)
2874{
4eaa9819
JS
2875 struct soc_mixer_control *mc =
2876 (struct soc_mixer_control *)kcontrol->private_value;
907fe36a 2877 struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
815ecf8d 2878 unsigned int reg = mc->reg;
907fe36a 2879 unsigned int val;
4eaa9819 2880 int min = mc->min;
907fe36a
LPC
2881 int ret;
2882
2883 ret = snd_soc_component_read(component, reg, &val);
2884 if (ret)
2885 return ret;
e13ac2e9
MB
2886
2887 ucontrol->value.integer.value[0] =
2888 ((signed char)(val & 0xff))-min;
2889 ucontrol->value.integer.value[1] =
2890 ((signed char)((val >> 8) & 0xff))-min;
2891 return 0;
2892}
2893EXPORT_SYMBOL_GPL(snd_soc_get_volsw_s8);
2894
2895/**
2896 * snd_soc_put_volsw_sgn - signed mixer put callback
2897 * @kcontrol: mixer control
ac11a2b3 2898 * @ucontrol: control element information
e13ac2e9
MB
2899 *
2900 * Callback to set the value of a signed mixer control.
2901 *
2902 * Returns 0 for success.
2903 */
2904int snd_soc_put_volsw_s8(struct snd_kcontrol *kcontrol,
2905 struct snd_ctl_elem_value *ucontrol)
2906{
4eaa9819
JS
2907 struct soc_mixer_control *mc =
2908 (struct soc_mixer_control *)kcontrol->private_value;
907fe36a 2909 struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
815ecf8d 2910 unsigned int reg = mc->reg;
4eaa9819 2911 int min = mc->min;
46f5822f 2912 unsigned int val;
e13ac2e9
MB
2913
2914 val = (ucontrol->value.integer.value[0]+min) & 0xff;
2915 val |= ((ucontrol->value.integer.value[1]+min) & 0xff) << 8;
2916
907fe36a 2917 return snd_soc_component_update_bits(component, reg, 0xffff, val);
e13ac2e9
MB
2918}
2919EXPORT_SYMBOL_GPL(snd_soc_put_volsw_s8);
2920
6c9d8cf6
AT
2921/**
2922 * snd_soc_info_volsw_range - single mixer info callback with range.
2923 * @kcontrol: mixer control
2924 * @uinfo: control element information
2925 *
2926 * Callback to provide information, within a range, about a single
2927 * mixer control.
2928 *
2929 * returns 0 for success.
2930 */
2931int snd_soc_info_volsw_range(struct snd_kcontrol *kcontrol,
2932 struct snd_ctl_elem_info *uinfo)
2933{
2934 struct soc_mixer_control *mc =
2935 (struct soc_mixer_control *)kcontrol->private_value;
2936 int platform_max;
2937 int min = mc->min;
2938
2939 if (!mc->platform_max)
2940 mc->platform_max = mc->max;
2941 platform_max = mc->platform_max;
2942
2943 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
9bde4f0b 2944 uinfo->count = snd_soc_volsw_is_stereo(mc) ? 2 : 1;
6c9d8cf6
AT
2945 uinfo->value.integer.min = 0;
2946 uinfo->value.integer.max = platform_max - min;
2947
2948 return 0;
2949}
2950EXPORT_SYMBOL_GPL(snd_soc_info_volsw_range);
2951
2952/**
2953 * snd_soc_put_volsw_range - single mixer put value callback with range.
2954 * @kcontrol: mixer control
2955 * @ucontrol: control element information
2956 *
2957 * Callback to set the value, within a range, for a single mixer control.
2958 *
2959 * Returns 0 for success.
2960 */
2961int snd_soc_put_volsw_range(struct snd_kcontrol *kcontrol,
2962 struct snd_ctl_elem_value *ucontrol)
2963{
2964 struct soc_mixer_control *mc =
2965 (struct soc_mixer_control *)kcontrol->private_value;
907fe36a 2966 struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
6c9d8cf6 2967 unsigned int reg = mc->reg;
9bde4f0b 2968 unsigned int rreg = mc->rreg;
6c9d8cf6
AT
2969 unsigned int shift = mc->shift;
2970 int min = mc->min;
2971 int max = mc->max;
2972 unsigned int mask = (1 << fls(max)) - 1;
2973 unsigned int invert = mc->invert;
2974 unsigned int val, val_mask;
9bde4f0b 2975 int ret;
6c9d8cf6
AT
2976
2977 val = ((ucontrol->value.integer.value[0] + min) & mask);
2978 if (invert)
2979 val = max - val;
2980 val_mask = mask << shift;
2981 val = val << shift;
2982
907fe36a 2983 ret = snd_soc_component_update_bits(component, reg, val_mask, val);
0eaa6cca 2984 if (ret < 0)
9bde4f0b
MB
2985 return ret;
2986
2987 if (snd_soc_volsw_is_stereo(mc)) {
2988 val = ((ucontrol->value.integer.value[1] + min) & mask);
2989 if (invert)
2990 val = max - val;
2991 val_mask = mask << shift;
2992 val = val << shift;
2993
907fe36a
LPC
2994 ret = snd_soc_component_update_bits(component, rreg, val_mask,
2995 val);
9bde4f0b
MB
2996 }
2997
2998 return ret;
6c9d8cf6
AT
2999}
3000EXPORT_SYMBOL_GPL(snd_soc_put_volsw_range);
3001
3002/**
3003 * snd_soc_get_volsw_range - single mixer get callback with range
3004 * @kcontrol: mixer control
3005 * @ucontrol: control element information
3006 *
3007 * Callback to get the value, within a range, of a single mixer control.
3008 *
3009 * Returns 0 for success.
3010 */
3011int snd_soc_get_volsw_range(struct snd_kcontrol *kcontrol,
3012 struct snd_ctl_elem_value *ucontrol)
3013{
907fe36a 3014 struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
6c9d8cf6
AT
3015 struct soc_mixer_control *mc =
3016 (struct soc_mixer_control *)kcontrol->private_value;
6c9d8cf6 3017 unsigned int reg = mc->reg;
9bde4f0b 3018 unsigned int rreg = mc->rreg;
6c9d8cf6
AT
3019 unsigned int shift = mc->shift;
3020 int min = mc->min;
3021 int max = mc->max;
3022 unsigned int mask = (1 << fls(max)) - 1;
3023 unsigned int invert = mc->invert;
907fe36a
LPC
3024 unsigned int val;
3025 int ret;
6c9d8cf6 3026
907fe36a
LPC
3027 ret = snd_soc_component_read(component, reg, &val);
3028 if (ret)
3029 return ret;
3030
3031 ucontrol->value.integer.value[0] = (val >> shift) & mask;
6c9d8cf6
AT
3032 if (invert)
3033 ucontrol->value.integer.value[0] =
3034 max - ucontrol->value.integer.value[0];
3035 ucontrol->value.integer.value[0] =
3036 ucontrol->value.integer.value[0] - min;
3037
9bde4f0b 3038 if (snd_soc_volsw_is_stereo(mc)) {
907fe36a
LPC
3039 ret = snd_soc_component_read(component, rreg, &val);
3040 if (ret)
3041 return ret;
3042
3043 ucontrol->value.integer.value[1] = (val >> shift) & mask;
9bde4f0b
MB
3044 if (invert)
3045 ucontrol->value.integer.value[1] =
3046 max - ucontrol->value.integer.value[1];
3047 ucontrol->value.integer.value[1] =
3048 ucontrol->value.integer.value[1] - min;
3049 }
3050
6c9d8cf6
AT
3051 return 0;
3052}
3053EXPORT_SYMBOL_GPL(snd_soc_get_volsw_range);
3054
637d3847
PU
3055/**
3056 * snd_soc_limit_volume - Set new limit to an existing volume control.
3057 *
3058 * @codec: where to look for the control
3059 * @name: Name of the control
3060 * @max: new maximum limit
3061 *
3062 * Return 0 for success, else error.
3063 */
3064int snd_soc_limit_volume(struct snd_soc_codec *codec,
3065 const char *name, int max)
3066{
00200107 3067 struct snd_card *card = codec->component.card->snd_card;
637d3847
PU
3068 struct snd_kcontrol *kctl;
3069 struct soc_mixer_control *mc;
3070 int found = 0;
3071 int ret = -EINVAL;
3072
3073 /* Sanity check for name and max */
3074 if (unlikely(!name || max <= 0))
3075 return -EINVAL;
3076
3077 list_for_each_entry(kctl, &card->controls, list) {
3078 if (!strncmp(kctl->id.name, name, sizeof(kctl->id.name))) {
3079 found = 1;
3080 break;
3081 }
3082 }
3083 if (found) {
3084 mc = (struct soc_mixer_control *)kctl->private_value;
3085 if (max <= mc->max) {
d11bb4a9 3086 mc->platform_max = max;
637d3847
PU
3087 ret = 0;
3088 }
3089 }
3090 return ret;
3091}
3092EXPORT_SYMBOL_GPL(snd_soc_limit_volume);
3093
71d08516
MB
3094int snd_soc_bytes_info(struct snd_kcontrol *kcontrol,
3095 struct snd_ctl_elem_info *uinfo)
3096{
907fe36a 3097 struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
71d08516
MB
3098 struct soc_bytes *params = (void *)kcontrol->private_value;
3099
3100 uinfo->type = SNDRV_CTL_ELEM_TYPE_BYTES;
907fe36a 3101 uinfo->count = params->num_regs * component->val_bytes;
71d08516
MB
3102
3103 return 0;
3104}
3105EXPORT_SYMBOL_GPL(snd_soc_bytes_info);
3106
3107int snd_soc_bytes_get(struct snd_kcontrol *kcontrol,
3108 struct snd_ctl_elem_value *ucontrol)
3109{
907fe36a 3110 struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
71d08516 3111 struct soc_bytes *params = (void *)kcontrol->private_value;
71d08516
MB
3112 int ret;
3113
907fe36a
LPC
3114 if (component->regmap)
3115 ret = regmap_raw_read(component->regmap, params->base,
71d08516 3116 ucontrol->value.bytes.data,
907fe36a 3117 params->num_regs * component->val_bytes);
71d08516
MB
3118 else
3119 ret = -EINVAL;
3120
f831b055
MB
3121 /* Hide any masked bytes to ensure consistent data reporting */
3122 if (ret == 0 && params->mask) {
907fe36a 3123 switch (component->val_bytes) {
f831b055
MB
3124 case 1:
3125 ucontrol->value.bytes.data[0] &= ~params->mask;
3126 break;
3127 case 2:
3128 ((u16 *)(&ucontrol->value.bytes.data))[0]
8f1ec93a 3129 &= cpu_to_be16(~params->mask);
f831b055
MB
3130 break;
3131 case 4:
3132 ((u32 *)(&ucontrol->value.bytes.data))[0]
8f1ec93a 3133 &= cpu_to_be32(~params->mask);
f831b055
MB
3134 break;
3135 default:
3136 return -EINVAL;
3137 }
3138 }
3139
71d08516
MB
3140 return ret;
3141}
3142EXPORT_SYMBOL_GPL(snd_soc_bytes_get);
3143
3144int snd_soc_bytes_put(struct snd_kcontrol *kcontrol,
3145 struct snd_ctl_elem_value *ucontrol)
3146{
907fe36a 3147 struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
71d08516 3148 struct soc_bytes *params = (void *)kcontrol->private_value;
f831b055 3149 int ret, len;
a1a564ed 3150 unsigned int val, mask;
f831b055 3151 void *data;
71d08516 3152
907fe36a 3153 if (!component->regmap)
f831b055
MB
3154 return -EINVAL;
3155
907fe36a 3156 len = params->num_regs * component->val_bytes;
f831b055 3157
b5a8fe43
MB
3158 data = kmemdup(ucontrol->value.bytes.data, len, GFP_KERNEL | GFP_DMA);
3159 if (!data)
3160 return -ENOMEM;
3161
f831b055
MB
3162 /*
3163 * If we've got a mask then we need to preserve the register
3164 * bits. We shouldn't modify the incoming data so take a
3165 * copy.
3166 */
3167 if (params->mask) {
907fe36a 3168 ret = regmap_read(component->regmap, params->base, &val);
f831b055 3169 if (ret != 0)
e8b18add 3170 goto out;
f831b055
MB
3171
3172 val &= params->mask;
3173
907fe36a 3174 switch (component->val_bytes) {
f831b055
MB
3175 case 1:
3176 ((u8 *)data)[0] &= ~params->mask;
3177 ((u8 *)data)[0] |= val;
3178 break;
3179 case 2:
a1a564ed 3180 mask = ~params->mask;
907fe36a 3181 ret = regmap_parse_val(component->regmap,
a1a564ed
NC
3182 &mask, &mask);
3183 if (ret != 0)
3184 goto out;
3185
3186 ((u16 *)data)[0] &= mask;
3187
907fe36a 3188 ret = regmap_parse_val(component->regmap,
a1a564ed
NC
3189 &val, &val);
3190 if (ret != 0)
3191 goto out;
3192
3193 ((u16 *)data)[0] |= val;
f831b055
MB
3194 break;
3195 case 4:
a1a564ed 3196 mask = ~params->mask;
907fe36a 3197 ret = regmap_parse_val(component->regmap,
a1a564ed
NC
3198 &mask, &mask);
3199 if (ret != 0)
3200 goto out;
3201
3202 ((u32 *)data)[0] &= mask;
3203
907fe36a 3204 ret = regmap_parse_val(component->regmap,
a1a564ed
NC
3205 &val, &val);
3206 if (ret != 0)
3207 goto out;
3208
3209 ((u32 *)data)[0] |= val;
f831b055
MB
3210 break;
3211 default:
e8b18add
WY
3212 ret = -EINVAL;
3213 goto out;
f831b055
MB
3214 }
3215 }
3216
907fe36a 3217 ret = regmap_raw_write(component->regmap, params->base,
f831b055
MB
3218 data, len);
3219
e8b18add 3220out:
b5a8fe43 3221 kfree(data);
71d08516
MB
3222
3223 return ret;
3224}
3225EXPORT_SYMBOL_GPL(snd_soc_bytes_put);
3226
d9881208
VK
3227int snd_soc_bytes_info_ext(struct snd_kcontrol *kcontrol,
3228 struct snd_ctl_elem_info *ucontrol)
3229{
3230 struct soc_bytes_ext *params = (void *)kcontrol->private_value;
3231
3232 ucontrol->type = SNDRV_CTL_ELEM_TYPE_BYTES;
3233 ucontrol->count = params->max;
3234
3235 return 0;
3236}
3237EXPORT_SYMBOL_GPL(snd_soc_bytes_info_ext);
3238
7523a271
OMA
3239int snd_soc_bytes_tlv_callback(struct snd_kcontrol *kcontrol, int op_flag,
3240 unsigned int size, unsigned int __user *tlv)
3241{
3242 struct soc_bytes_ext *params = (void *)kcontrol->private_value;
3243 unsigned int count = size < params->max ? size : params->max;
3244 int ret = -ENXIO;
3245
3246 switch (op_flag) {
3247 case SNDRV_CTL_TLV_OP_READ:
3248 if (params->get)
3249 ret = params->get(tlv, count);
3250 break;
3251 case SNDRV_CTL_TLV_OP_WRITE:
3252 if (params->put)
3253 ret = params->put(tlv, count);
3254 break;
3255 }
3256 return ret;
3257}
3258EXPORT_SYMBOL_GPL(snd_soc_bytes_tlv_callback);
3259
4183eed2
KK
3260/**
3261 * snd_soc_info_xr_sx - signed multi register info callback
3262 * @kcontrol: mreg control
3263 * @uinfo: control element information
3264 *
3265 * Callback to provide information of a control that can
3266 * span multiple codec registers which together
3267 * forms a single signed value in a MSB/LSB manner.
3268 *
3269 * Returns 0 for success.
3270 */
3271int snd_soc_info_xr_sx(struct snd_kcontrol *kcontrol,
3272 struct snd_ctl_elem_info *uinfo)
3273{
3274 struct soc_mreg_control *mc =
3275 (struct soc_mreg_control *)kcontrol->private_value;
3276 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
3277 uinfo->count = 1;
3278 uinfo->value.integer.min = mc->min;
3279 uinfo->value.integer.max = mc->max;
3280
3281 return 0;
3282}
3283EXPORT_SYMBOL_GPL(snd_soc_info_xr_sx);
3284
3285/**
3286 * snd_soc_get_xr_sx - signed multi register get callback
3287 * @kcontrol: mreg control
3288 * @ucontrol: control element information
3289 *
3290 * Callback to get the value of a control that can span
3291 * multiple codec registers which together forms a single
3292 * signed value in a MSB/LSB manner. The control supports
3293 * specifying total no of bits used to allow for bitfields
3294 * across the multiple codec registers.
3295 *
3296 * Returns 0 for success.
3297 */
3298int snd_soc_get_xr_sx(struct snd_kcontrol *kcontrol,
3299 struct snd_ctl_elem_value *ucontrol)
3300{
907fe36a 3301 struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
4183eed2
KK
3302 struct soc_mreg_control *mc =
3303 (struct soc_mreg_control *)kcontrol->private_value;
4183eed2
KK
3304 unsigned int regbase = mc->regbase;
3305 unsigned int regcount = mc->regcount;
907fe36a 3306 unsigned int regwshift = component->val_bytes * BITS_PER_BYTE;
4183eed2
KK
3307 unsigned int regwmask = (1<<regwshift)-1;
3308 unsigned int invert = mc->invert;
3309 unsigned long mask = (1UL<<mc->nbits)-1;
3310 long min = mc->min;
3311 long max = mc->max;
3312 long val = 0;
907fe36a 3313 unsigned int regval;
4183eed2 3314 unsigned int i;
907fe36a 3315 int ret;
4183eed2
KK
3316
3317 for (i = 0; i < regcount; i++) {
907fe36a
LPC
3318 ret = snd_soc_component_read(component, regbase+i, &regval);
3319 if (ret)
3320 return ret;
3321 val |= (regval & regwmask) << (regwshift*(regcount-i-1));
4183eed2
KK
3322 }
3323 val &= mask;
3324 if (min < 0 && val > max)
3325 val |= ~mask;
3326 if (invert)
3327 val = max - val;
3328 ucontrol->value.integer.value[0] = val;
3329
3330 return 0;
3331}
3332EXPORT_SYMBOL_GPL(snd_soc_get_xr_sx);
3333
3334/**
3335 * snd_soc_put_xr_sx - signed multi register get callback
3336 * @kcontrol: mreg control
3337 * @ucontrol: control element information
3338 *
3339 * Callback to set the value of a control that can span
3340 * multiple codec registers which together forms a single
3341 * signed value in a MSB/LSB manner. The control supports
3342 * specifying total no of bits used to allow for bitfields
3343 * across the multiple codec registers.
3344 *
3345 * Returns 0 for success.
3346 */
3347int snd_soc_put_xr_sx(struct snd_kcontrol *kcontrol,
3348 struct snd_ctl_elem_value *ucontrol)
3349{
907fe36a 3350 struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
4183eed2
KK
3351 struct soc_mreg_control *mc =
3352 (struct soc_mreg_control *)kcontrol->private_value;
4183eed2
KK
3353 unsigned int regbase = mc->regbase;
3354 unsigned int regcount = mc->regcount;
907fe36a 3355 unsigned int regwshift = component->val_bytes * BITS_PER_BYTE;
4183eed2
KK
3356 unsigned int regwmask = (1<<regwshift)-1;
3357 unsigned int invert = mc->invert;
3358 unsigned long mask = (1UL<<mc->nbits)-1;
4183eed2
KK
3359 long max = mc->max;
3360 long val = ucontrol->value.integer.value[0];
3361 unsigned int i, regval, regmask;
3362 int err;
3363
3364 if (invert)
3365 val = max - val;
3366 val &= mask;
3367 for (i = 0; i < regcount; i++) {
3368 regval = (val >> (regwshift*(regcount-i-1))) & regwmask;
3369 regmask = (mask >> (regwshift*(regcount-i-1))) & regwmask;
907fe36a 3370 err = snd_soc_component_update_bits(component, regbase+i,
4183eed2
KK
3371 regmask, regval);
3372 if (err < 0)
3373 return err;
3374 }
3375
3376 return 0;
3377}
3378EXPORT_SYMBOL_GPL(snd_soc_put_xr_sx);
3379
dd7b10b3
KK
3380/**
3381 * snd_soc_get_strobe - strobe get callback
3382 * @kcontrol: mixer control
3383 * @ucontrol: control element information
3384 *
3385 * Callback get the value of a strobe mixer control.
3386 *
3387 * Returns 0 for success.
3388 */
3389int snd_soc_get_strobe(struct snd_kcontrol *kcontrol,
3390 struct snd_ctl_elem_value *ucontrol)
3391{
907fe36a 3392 struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
dd7b10b3
KK
3393 struct soc_mixer_control *mc =
3394 (struct soc_mixer_control *)kcontrol->private_value;
dd7b10b3
KK
3395 unsigned int reg = mc->reg;
3396 unsigned int shift = mc->shift;
3397 unsigned int mask = 1 << shift;
3398 unsigned int invert = mc->invert != 0;
907fe36a
LPC
3399 unsigned int val;
3400 int ret;
3401
3402 ret = snd_soc_component_read(component, reg, &val);
3403 if (ret)
3404 return ret;
3405
3406 val &= mask;
dd7b10b3
KK
3407
3408 if (shift != 0 && val != 0)
3409 val = val >> shift;
3410 ucontrol->value.enumerated.item[0] = val ^ invert;
3411
3412 return 0;
3413}
3414EXPORT_SYMBOL_GPL(snd_soc_get_strobe);
3415
3416/**
3417 * snd_soc_put_strobe - strobe put callback
3418 * @kcontrol: mixer control
3419 * @ucontrol: control element information
3420 *
3421 * Callback strobe a register bit to high then low (or the inverse)
3422 * in one pass of a single mixer enum control.
3423 *
3424 * Returns 1 for success.
3425 */
3426int snd_soc_put_strobe(struct snd_kcontrol *kcontrol,
3427 struct snd_ctl_elem_value *ucontrol)
3428{
907fe36a 3429 struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
dd7b10b3
KK
3430 struct soc_mixer_control *mc =
3431 (struct soc_mixer_control *)kcontrol->private_value;
dd7b10b3
KK
3432 unsigned int reg = mc->reg;
3433 unsigned int shift = mc->shift;
3434 unsigned int mask = 1 << shift;
3435 unsigned int invert = mc->invert != 0;
3436 unsigned int strobe = ucontrol->value.enumerated.item[0] != 0;
3437 unsigned int val1 = (strobe ^ invert) ? mask : 0;
3438 unsigned int val2 = (strobe ^ invert) ? 0 : mask;
3439 int err;
3440
907fe36a 3441 err = snd_soc_component_update_bits(component, reg, mask, val1);
dd7b10b3
KK
3442 if (err < 0)
3443 return err;
3444
907fe36a 3445 return snd_soc_component_update_bits(component, reg, mask, val2);
dd7b10b3
KK
3446}
3447EXPORT_SYMBOL_GPL(snd_soc_put_strobe);
3448
8c6529db
LG
3449/**
3450 * snd_soc_dai_set_sysclk - configure DAI system or master clock.
3451 * @dai: DAI
3452 * @clk_id: DAI specific clock ID
3453 * @freq: new clock frequency in Hz
3454 * @dir: new clock direction - input/output.
3455 *
3456 * Configures the DAI master (MCLK) or system (SYSCLK) clocking.
3457 */
3458int snd_soc_dai_set_sysclk(struct snd_soc_dai *dai, int clk_id,
3459 unsigned int freq, int dir)
3460{
f0fba2ad
LG
3461 if (dai->driver && dai->driver->ops->set_sysclk)
3462 return dai->driver->ops->set_sysclk(dai, clk_id, freq, dir);
ec4ee52a 3463 else if (dai->codec && dai->codec->driver->set_sysclk)
da1c6ea6 3464 return dai->codec->driver->set_sysclk(dai->codec, clk_id, 0,
ec4ee52a 3465 freq, dir);
8c6529db 3466 else
1104a9c8 3467 return -ENOTSUPP;
8c6529db
LG
3468}
3469EXPORT_SYMBOL_GPL(snd_soc_dai_set_sysclk);
3470
ec4ee52a
MB
3471/**
3472 * snd_soc_codec_set_sysclk - configure CODEC system or master clock.
3473 * @codec: CODEC
3474 * @clk_id: DAI specific clock ID
da1c6ea6 3475 * @source: Source for the clock
ec4ee52a
MB
3476 * @freq: new clock frequency in Hz
3477 * @dir: new clock direction - input/output.
3478 *
3479 * Configures the CODEC master (MCLK) or system (SYSCLK) clocking.
3480 */
3481int snd_soc_codec_set_sysclk(struct snd_soc_codec *codec, int clk_id,
da1c6ea6 3482 int source, unsigned int freq, int dir)
ec4ee52a
MB
3483{
3484 if (codec->driver->set_sysclk)
da1c6ea6
MB
3485 return codec->driver->set_sysclk(codec, clk_id, source,
3486 freq, dir);
ec4ee52a 3487 else
1104a9c8 3488 return -ENOTSUPP;
ec4ee52a
MB
3489}
3490EXPORT_SYMBOL_GPL(snd_soc_codec_set_sysclk);
3491
8c6529db
LG
3492/**
3493 * snd_soc_dai_set_clkdiv - configure DAI clock dividers.
3494 * @dai: DAI
ac11a2b3 3495 * @div_id: DAI specific clock divider ID
8c6529db
LG
3496 * @div: new clock divisor.
3497 *
3498 * Configures the clock dividers. This is used to derive the best DAI bit and
3499 * frame clocks from the system or master clock. It's best to set the DAI bit
3500 * and frame clocks as low as possible to save system power.
3501 */
3502int snd_soc_dai_set_clkdiv(struct snd_soc_dai *dai,
3503 int div_id, int div)
3504{
f0fba2ad
LG
3505 if (dai->driver && dai->driver->ops->set_clkdiv)
3506 return dai->driver->ops->set_clkdiv(dai, div_id, div);
8c6529db
LG
3507 else
3508 return -EINVAL;
3509}
3510EXPORT_SYMBOL_GPL(snd_soc_dai_set_clkdiv);
3511
3512/**
3513 * snd_soc_dai_set_pll - configure DAI PLL.
3514 * @dai: DAI
3515 * @pll_id: DAI specific PLL ID
85488037 3516 * @source: DAI specific source for the PLL
8c6529db
LG
3517 * @freq_in: PLL input clock frequency in Hz
3518 * @freq_out: requested PLL output clock frequency in Hz
3519 *
3520 * Configures and enables PLL to generate output clock based on input clock.
3521 */
85488037
MB
3522int snd_soc_dai_set_pll(struct snd_soc_dai *dai, int pll_id, int source,
3523 unsigned int freq_in, unsigned int freq_out)
8c6529db 3524{
f0fba2ad
LG
3525 if (dai->driver && dai->driver->ops->set_pll)
3526 return dai->driver->ops->set_pll(dai, pll_id, source,
85488037 3527 freq_in, freq_out);
ec4ee52a
MB
3528 else if (dai->codec && dai->codec->driver->set_pll)
3529 return dai->codec->driver->set_pll(dai->codec, pll_id, source,
3530 freq_in, freq_out);
8c6529db
LG
3531 else
3532 return -EINVAL;
3533}
3534EXPORT_SYMBOL_GPL(snd_soc_dai_set_pll);
3535
ec4ee52a
MB
3536/*
3537 * snd_soc_codec_set_pll - configure codec PLL.
3538 * @codec: CODEC
3539 * @pll_id: DAI specific PLL ID
3540 * @source: DAI specific source for the PLL
3541 * @freq_in: PLL input clock frequency in Hz
3542 * @freq_out: requested PLL output clock frequency in Hz
3543 *
3544 * Configures and enables PLL to generate output clock based on input clock.
3545 */
3546int snd_soc_codec_set_pll(struct snd_soc_codec *codec, int pll_id, int source,
3547 unsigned int freq_in, unsigned int freq_out)
3548{
3549 if (codec->driver->set_pll)
3550 return codec->driver->set_pll(codec, pll_id, source,
3551 freq_in, freq_out);
3552 else
3553 return -EINVAL;
3554}
3555EXPORT_SYMBOL_GPL(snd_soc_codec_set_pll);
3556
e54cf76b
LG
3557/**
3558 * snd_soc_dai_set_bclk_ratio - configure BCLK to sample rate ratio.
3559 * @dai: DAI
3560 * @ratio Ratio of BCLK to Sample rate.
3561 *
3562 * Configures the DAI for a preset BCLK to sample rate ratio.
3563 */
3564int snd_soc_dai_set_bclk_ratio(struct snd_soc_dai *dai, unsigned int ratio)
3565{
3566 if (dai->driver && dai->driver->ops->set_bclk_ratio)
3567 return dai->driver->ops->set_bclk_ratio(dai, ratio);
3568 else
3569 return -EINVAL;
3570}
3571EXPORT_SYMBOL_GPL(snd_soc_dai_set_bclk_ratio);
3572
8c6529db
LG
3573/**
3574 * snd_soc_dai_set_fmt - configure DAI hardware audio format.
3575 * @dai: DAI
8c6529db
LG
3576 * @fmt: SND_SOC_DAIFMT_ format value.
3577 *
3578 * Configures the DAI hardware format and clocking.
3579 */
3580int snd_soc_dai_set_fmt(struct snd_soc_dai *dai, unsigned int fmt)
3581{
5e4ba569 3582 if (dai->driver == NULL)
8c6529db 3583 return -EINVAL;
5e4ba569
SG
3584 if (dai->driver->ops->set_fmt == NULL)
3585 return -ENOTSUPP;
3586 return dai->driver->ops->set_fmt(dai, fmt);
8c6529db
LG
3587}
3588EXPORT_SYMBOL_GPL(snd_soc_dai_set_fmt);
3589
89c67857 3590/**
e5c21514 3591 * snd_soc_xlate_tdm_slot - generate tx/rx slot mask.
89c67857
XL
3592 * @slots: Number of slots in use.
3593 * @tx_mask: bitmask representing active TX slots.
3594 * @rx_mask: bitmask representing active RX slots.
3595 *
3596 * Generates the TDM tx and rx slot default masks for DAI.
3597 */
e5c21514 3598static int snd_soc_xlate_tdm_slot_mask(unsigned int slots,
89c67857
XL
3599 unsigned int *tx_mask,
3600 unsigned int *rx_mask)
3601{
3602 if (*tx_mask || *rx_mask)
3603 return 0;
3604
3605 if (!slots)
3606 return -EINVAL;
3607
3608 *tx_mask = (1 << slots) - 1;
3609 *rx_mask = (1 << slots) - 1;
3610
3611 return 0;
3612}
3613
8c6529db
LG
3614/**
3615 * snd_soc_dai_set_tdm_slot - configure DAI TDM.
3616 * @dai: DAI
a5479e38
DR
3617 * @tx_mask: bitmask representing active TX slots.
3618 * @rx_mask: bitmask representing active RX slots.
8c6529db 3619 * @slots: Number of slots in use.
a5479e38 3620 * @slot_width: Width in bits for each slot.
8c6529db
LG
3621 *
3622 * Configures a DAI for TDM operation. Both mask and slots are codec and DAI
3623 * specific.
3624 */
3625int snd_soc_dai_set_tdm_slot(struct snd_soc_dai *dai,
a5479e38 3626 unsigned int tx_mask, unsigned int rx_mask, int slots, int slot_width)
8c6529db 3627{
e5c21514
XL
3628 if (dai->driver && dai->driver->ops->xlate_tdm_slot_mask)
3629 dai->driver->ops->xlate_tdm_slot_mask(slots,
89c67857
XL
3630 &tx_mask, &rx_mask);
3631 else
e5c21514 3632 snd_soc_xlate_tdm_slot_mask(slots, &tx_mask, &rx_mask);
89c67857 3633
88bd870f
BC
3634 dai->tx_mask = tx_mask;
3635 dai->rx_mask = rx_mask;
3636
f0fba2ad
LG
3637 if (dai->driver && dai->driver->ops->set_tdm_slot)
3638 return dai->driver->ops->set_tdm_slot(dai, tx_mask, rx_mask,
a5479e38 3639 slots, slot_width);
8c6529db 3640 else
b2cbb6e1 3641 return -ENOTSUPP;
8c6529db
LG
3642}
3643EXPORT_SYMBOL_GPL(snd_soc_dai_set_tdm_slot);
3644
472df3cb
BS
3645/**
3646 * snd_soc_dai_set_channel_map - configure DAI audio channel map
3647 * @dai: DAI
3648 * @tx_num: how many TX channels
3649 * @tx_slot: pointer to an array which imply the TX slot number channel
3650 * 0~num-1 uses
3651 * @rx_num: how many RX channels
3652 * @rx_slot: pointer to an array which imply the RX slot number channel
3653 * 0~num-1 uses
3654 *
3655 * configure the relationship between channel number and TDM slot number.
3656 */
3657int snd_soc_dai_set_channel_map(struct snd_soc_dai *dai,
3658 unsigned int tx_num, unsigned int *tx_slot,
3659 unsigned int rx_num, unsigned int *rx_slot)
3660{
f0fba2ad
LG
3661 if (dai->driver && dai->driver->ops->set_channel_map)
3662 return dai->driver->ops->set_channel_map(dai, tx_num, tx_slot,
472df3cb
BS
3663 rx_num, rx_slot);
3664 else
3665 return -EINVAL;
3666}
3667EXPORT_SYMBOL_GPL(snd_soc_dai_set_channel_map);
3668
8c6529db
LG
3669/**
3670 * snd_soc_dai_set_tristate - configure DAI system or master clock.
3671 * @dai: DAI
3672 * @tristate: tristate enable
3673 *
3674 * Tristates the DAI so that others can use it.
3675 */
3676int snd_soc_dai_set_tristate(struct snd_soc_dai *dai, int tristate)
3677{
f0fba2ad
LG
3678 if (dai->driver && dai->driver->ops->set_tristate)
3679 return dai->driver->ops->set_tristate(dai, tristate);
8c6529db
LG
3680 else
3681 return -EINVAL;
3682}
3683EXPORT_SYMBOL_GPL(snd_soc_dai_set_tristate);
3684
3685/**
3686 * snd_soc_dai_digital_mute - configure DAI system or master clock.
3687 * @dai: DAI
3688 * @mute: mute enable
da18396f 3689 * @direction: stream to mute
8c6529db
LG
3690 *
3691 * Mutes the DAI DAC.
3692 */
da18396f
MB
3693int snd_soc_dai_digital_mute(struct snd_soc_dai *dai, int mute,
3694 int direction)
8c6529db 3695{
da18396f
MB
3696 if (!dai->driver)
3697 return -ENOTSUPP;
3698
3699 if (dai->driver->ops->mute_stream)
3700 return dai->driver->ops->mute_stream(dai, mute, direction);
3701 else if (direction == SNDRV_PCM_STREAM_PLAYBACK &&
3702 dai->driver->ops->digital_mute)
f0fba2ad 3703 return dai->driver->ops->digital_mute(dai, mute);
8c6529db 3704 else
04570c62 3705 return -ENOTSUPP;
8c6529db
LG
3706}
3707EXPORT_SYMBOL_GPL(snd_soc_dai_digital_mute);
3708
88bd870f
BC
3709static int snd_soc_init_multicodec(struct snd_soc_card *card,
3710 struct snd_soc_dai_link *dai_link)
3711{
3712 /* Legacy codec/codec_dai link is a single entry in multicodec */
3713 if (dai_link->codec_name || dai_link->codec_of_node ||
3714 dai_link->codec_dai_name) {
3715 dai_link->num_codecs = 1;
3716
3717 dai_link->codecs = devm_kzalloc(card->dev,
3718 sizeof(struct snd_soc_dai_link_component),
3719 GFP_KERNEL);
3720 if (!dai_link->codecs)
3721 return -ENOMEM;
3722
3723 dai_link->codecs[0].name = dai_link->codec_name;
3724 dai_link->codecs[0].of_node = dai_link->codec_of_node;
3725 dai_link->codecs[0].dai_name = dai_link->codec_dai_name;
3726 }
3727
3728 if (!dai_link->codecs) {
3729 dev_err(card->dev, "ASoC: DAI link has no CODECs\n");
3730 return -EINVAL;
3731 }
3732
3733 return 0;
3734}
3735
c5af3a2e
MB
3736/**
3737 * snd_soc_register_card - Register a card with the ASoC core
3738 *
ac11a2b3 3739 * @card: Card to register
c5af3a2e 3740 *
c5af3a2e 3741 */
70a7ca34 3742int snd_soc_register_card(struct snd_soc_card *card)
c5af3a2e 3743{
88bd870f 3744 int i, j, ret;
f0fba2ad 3745
c5af3a2e
MB
3746 if (!card->name || !card->dev)
3747 return -EINVAL;
3748
5a504963
SW
3749 for (i = 0; i < card->num_links; i++) {
3750 struct snd_soc_dai_link *link = &card->dai_link[i];
3751
88bd870f
BC
3752 ret = snd_soc_init_multicodec(card, link);
3753 if (ret) {
3754 dev_err(card->dev, "ASoC: failed to init multicodec\n");
3755 return ret;
5a504963 3756 }
88bd870f
BC
3757
3758 for (j = 0; j < link->num_codecs; j++) {
3759 /*
3760 * Codec must be specified by 1 of name or OF node,
3761 * not both or neither.
3762 */
3763 if (!!link->codecs[j].name ==
3764 !!link->codecs[j].of_node) {
3765 dev_err(card->dev, "ASoC: Neither/both codec name/of_node are set for %s\n",
3766 link->name);
3767 return -EINVAL;
3768 }
3769 /* Codec DAI name must be specified */
3770 if (!link->codecs[j].dai_name) {
3771 dev_err(card->dev, "ASoC: codec_dai_name not set for %s\n",
3772 link->name);
3773 return -EINVAL;
3774 }
bc92657a 3775 }
5a504963
SW
3776
3777 /*
3778 * Platform may be specified by either name or OF node, but
3779 * can be left unspecified, and a dummy platform will be used.
3780 */
3781 if (link->platform_name && link->platform_of_node) {
10e8aa9a
MM
3782 dev_err(card->dev,
3783 "ASoC: Both platform name/of_node are set for %s\n",
3784 link->name);
5a504963
SW
3785 return -EINVAL;
3786 }
3787
3788 /*
bc92657a
SW
3789 * CPU device may be specified by either name or OF node, but
3790 * can be left unspecified, and will be matched based on DAI
3791 * name alone..
3792 */
3793 if (link->cpu_name && link->cpu_of_node) {
10e8aa9a
MM
3794 dev_err(card->dev,
3795 "ASoC: Neither/both cpu name/of_node are set for %s\n",
3796 link->name);
bc92657a
SW
3797 return -EINVAL;
3798 }
3799 /*
3800 * At least one of CPU DAI name or CPU device name/node must be
3801 * specified
5a504963 3802 */
bc92657a
SW
3803 if (!link->cpu_dai_name &&
3804 !(link->cpu_name || link->cpu_of_node)) {
10e8aa9a
MM
3805 dev_err(card->dev,
3806 "ASoC: Neither cpu_dai_name nor cpu_name/of_node are set for %s\n",
3807 link->name);
5a504963
SW
3808 return -EINVAL;
3809 }
3810 }
3811
ed77cc12
MB
3812 dev_set_drvdata(card->dev, card);
3813
111c6419
SW
3814 snd_soc_initialize_card_lists(card);
3815
150dd2f8
VK
3816 soc_init_card_debugfs(card);
3817
181a6892
MB
3818 card->rtd = devm_kzalloc(card->dev,
3819 sizeof(struct snd_soc_pcm_runtime) *
3820 (card->num_links + card->num_aux_devs),
3821 GFP_KERNEL);
f0fba2ad
LG
3822 if (card->rtd == NULL)
3823 return -ENOMEM;
a7dbb603 3824 card->num_rtd = 0;
2eea392d 3825 card->rtd_aux = &card->rtd[card->num_links];
f0fba2ad 3826
5f3484ac
LPC
3827 for (i = 0; i < card->num_links; i++) {
3828 card->rtd[i].card = card;
f0fba2ad 3829 card->rtd[i].dai_link = &card->dai_link[i];
88bd870f
BC
3830 card->rtd[i].codec_dais = devm_kzalloc(card->dev,
3831 sizeof(struct snd_soc_dai *) *
3832 (card->rtd[i].dai_link->num_codecs),
3833 GFP_KERNEL);
3834 if (card->rtd[i].codec_dais == NULL)
3835 return -ENOMEM;
5f3484ac
LPC
3836 }
3837
3838 for (i = 0; i < card->num_aux_devs; i++)
3839 card->rtd_aux[i].card = card;
f0fba2ad 3840
db432b41 3841 INIT_LIST_HEAD(&card->dapm_dirty);
c5af3a2e 3842 card->instantiated = 0;
f0fba2ad 3843 mutex_init(&card->mutex);
a73fb2df 3844 mutex_init(&card->dapm_mutex);
c5af3a2e 3845
b19e6e7b
MB
3846 ret = snd_soc_instantiate_card(card);
3847 if (ret != 0)
3848 soc_cleanup_card_debugfs(card);
c5af3a2e 3849
988e8cc4
NC
3850 /* deactivate pins to sleep state */
3851 for (i = 0; i < card->num_rtd; i++) {
88bd870f
BC
3852 struct snd_soc_pcm_runtime *rtd = &card->rtd[i];
3853 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
3854 int j;
3855
3856 for (j = 0; j < rtd->num_codecs; j++) {
3857 struct snd_soc_dai *codec_dai = rtd->codec_dais[j];
3858 if (!codec_dai->active)
3859 pinctrl_pm_select_sleep_state(codec_dai->dev);
3860 }
3861
988e8cc4
NC
3862 if (!cpu_dai->active)
3863 pinctrl_pm_select_sleep_state(cpu_dai->dev);
3864 }
3865
b19e6e7b 3866 return ret;
c5af3a2e 3867}
70a7ca34 3868EXPORT_SYMBOL_GPL(snd_soc_register_card);
c5af3a2e
MB
3869
3870/**
3871 * snd_soc_unregister_card - Unregister a card with the ASoC core
3872 *
ac11a2b3 3873 * @card: Card to unregister
c5af3a2e 3874 *
c5af3a2e 3875 */
70a7ca34 3876int snd_soc_unregister_card(struct snd_soc_card *card)
c5af3a2e 3877{
b0e26485
VK
3878 if (card->instantiated)
3879 soc_cleanup_card_resources(card);
f110bfc7 3880 dev_dbg(card->dev, "ASoC: Unregistered card '%s'\n", card->name);
c5af3a2e
MB
3881
3882 return 0;
3883}
70a7ca34 3884EXPORT_SYMBOL_GPL(snd_soc_unregister_card);
c5af3a2e 3885
f0fba2ad
LG
3886/*
3887 * Simplify DAI link configuration by removing ".-1" from device names
3888 * and sanitizing names.
3889 */
0b9a214a 3890static char *fmt_single_name(struct device *dev, int *id)
f0fba2ad
LG
3891{
3892 char *found, name[NAME_SIZE];
3893 int id1, id2;
3894
3895 if (dev_name(dev) == NULL)
3896 return NULL;
3897
58818a77 3898 strlcpy(name, dev_name(dev), NAME_SIZE);
f0fba2ad
LG
3899
3900 /* are we a "%s.%d" name (platform and SPI components) */
3901 found = strstr(name, dev->driver->name);
3902 if (found) {
3903 /* get ID */
3904 if (sscanf(&found[strlen(dev->driver->name)], ".%d", id) == 1) {
3905
3906 /* discard ID from name if ID == -1 */
3907 if (*id == -1)
3908 found[strlen(dev->driver->name)] = '\0';
3909 }
3910
3911 } else {
3912 /* I2C component devices are named "bus-addr" */
3913 if (sscanf(name, "%x-%x", &id1, &id2) == 2) {
3914 char tmp[NAME_SIZE];
3915
3916 /* create unique ID number from I2C addr and bus */
05899446 3917 *id = ((id1 & 0xffff) << 16) + id2;
f0fba2ad
LG
3918
3919 /* sanitize component name for DAI link creation */
3920 snprintf(tmp, NAME_SIZE, "%s.%s", dev->driver->name, name);
58818a77 3921 strlcpy(name, tmp, NAME_SIZE);
f0fba2ad
LG
3922 } else
3923 *id = 0;
3924 }
3925
3926 return kstrdup(name, GFP_KERNEL);
3927}
3928
3929/*
3930 * Simplify DAI link naming for single devices with multiple DAIs by removing
3931 * any ".-1" and using the DAI name (instead of device name).
3932 */
3933static inline char *fmt_multiple_name(struct device *dev,
3934 struct snd_soc_dai_driver *dai_drv)
3935{
3936 if (dai_drv->name == NULL) {
10e8aa9a
MM
3937 dev_err(dev,
3938 "ASoC: error - multiple DAI %s registered with no name\n",
3939 dev_name(dev));
f0fba2ad
LG
3940 return NULL;
3941 }
3942
3943 return kstrdup(dai_drv->name, GFP_KERNEL);
3944}
3945
9115171a 3946/**
32c9ba54 3947 * snd_soc_unregister_dai - Unregister DAIs from the ASoC core
9115171a 3948 *
32c9ba54 3949 * @component: The component for which the DAIs should be unregistered
9115171a 3950 */
32c9ba54 3951static void snd_soc_unregister_dais(struct snd_soc_component *component)
9115171a 3952{
5c1d5f09 3953 struct snd_soc_dai *dai, *_dai;
9115171a 3954
5c1d5f09 3955 list_for_each_entry_safe(dai, _dai, &component->dai_list, list) {
32c9ba54
LPC
3956 dev_dbg(component->dev, "ASoC: Unregistered DAI '%s'\n",
3957 dai->name);
5c1d5f09 3958 list_del(&dai->list);
32c9ba54 3959 kfree(dai->name);
f0fba2ad 3960 kfree(dai);
f0fba2ad 3961 }
9115171a 3962}
9115171a
MB
3963
3964/**
32c9ba54 3965 * snd_soc_register_dais - Register a DAI with the ASoC core
9115171a 3966 *
6106d129
LPC
3967 * @component: The component the DAIs are registered for
3968 * @dai_drv: DAI driver to use for the DAIs
ac11a2b3 3969 * @count: Number of DAIs
32c9ba54
LPC
3970 * @legacy_dai_naming: Use the legacy naming scheme and let the DAI inherit the
3971 * parent's name.
9115171a 3972 */
6106d129 3973static int snd_soc_register_dais(struct snd_soc_component *component,
bb13109d
LPC
3974 struct snd_soc_dai_driver *dai_drv, size_t count,
3975 bool legacy_dai_naming)
9115171a 3976{
6106d129 3977 struct device *dev = component->dev;
f0fba2ad 3978 struct snd_soc_dai *dai;
32c9ba54
LPC
3979 unsigned int i;
3980 int ret;
f0fba2ad 3981
f110bfc7 3982 dev_dbg(dev, "ASoC: dai register %s #%Zu\n", dev_name(dev), count);
9115171a 3983
bb13109d
LPC
3984 component->dai_drv = dai_drv;
3985 component->num_dai = count;
3986
9115171a 3987 for (i = 0; i < count; i++) {
f0fba2ad
LG
3988
3989 dai = kzalloc(sizeof(struct snd_soc_dai), GFP_KERNEL);
c46e0079
AL
3990 if (dai == NULL) {
3991 ret = -ENOMEM;
3992 goto err;
3993 }
f0fba2ad 3994
32c9ba54
LPC
3995 /*
3996 * Back in the old days when we still had component-less DAIs,
3997 * instead of having a static name, component-less DAIs would
3998 * inherit the name of the parent device so it is possible to
3999 * register multiple instances of the DAI. We still need to keep
4000 * the same naming style even though those DAIs are not
4001 * component-less anymore.
4002 */
4003 if (count == 1 && legacy_dai_naming) {
4004 dai->name = fmt_single_name(dev, &dai->id);
4005 } else {
4006 dai->name = fmt_multiple_name(dev, &dai_drv[i]);
4007 if (dai_drv[i].id)
4008 dai->id = dai_drv[i].id;
4009 else
4010 dai->id = i;
4011 }
f0fba2ad
LG
4012 if (dai->name == NULL) {
4013 kfree(dai);
32c9ba54 4014 ret = -ENOMEM;
9115171a 4015 goto err;
f0fba2ad
LG
4016 }
4017
6106d129 4018 dai->component = component;
f0fba2ad 4019 dai->dev = dev;
f0fba2ad
LG
4020 dai->driver = &dai_drv[i];
4021 if (!dai->driver->ops)
4022 dai->driver->ops = &null_dai_ops;
4023
1438c2f6 4024 list_add(&dai->list, &component->dai_list);
054880fe 4025
32c9ba54 4026 dev_dbg(dev, "ASoC: Registered DAI '%s'\n", dai->name);
9115171a
MB
4027 }
4028
4029 return 0;
4030
4031err:
32c9ba54 4032 snd_soc_unregister_dais(component);
9115171a
MB
4033
4034 return ret;
4035}
9115171a 4036
14e8bdeb
LPC
4037static void snd_soc_component_seq_notifier(struct snd_soc_dapm_context *dapm,
4038 enum snd_soc_dapm_type type, int subseq)
d191bd8d 4039{
14e8bdeb 4040 struct snd_soc_component *component = dapm->component;
d191bd8d 4041
14e8bdeb
LPC
4042 component->driver->seq_notifier(component, type, subseq);
4043}
d191bd8d 4044
14e8bdeb
LPC
4045static int snd_soc_component_stream_event(struct snd_soc_dapm_context *dapm,
4046 int event)
4047{
4048 struct snd_soc_component *component = dapm->component;
d191bd8d 4049
14e8bdeb
LPC
4050 return component->driver->stream_event(component, event);
4051}
e2c330b9 4052
bb13109d
LPC
4053static int snd_soc_component_initialize(struct snd_soc_component *component,
4054 const struct snd_soc_component_driver *driver, struct device *dev)
d191bd8d 4055{
ce0fc93a
LPC
4056 struct snd_soc_dapm_context *dapm;
4057
bb13109d
LPC
4058 component->name = fmt_single_name(dev, &component->id);
4059 if (!component->name) {
4060 dev_err(dev, "ASoC: Failed to allocate name\n");
d191bd8d
KM
4061 return -ENOMEM;
4062 }
4063
bb13109d
LPC
4064 component->dev = dev;
4065 component->driver = driver;
61aca564
LPC
4066 component->probe = component->driver->probe;
4067 component->remove = component->driver->remove;
d191bd8d 4068
ce0fc93a
LPC
4069 if (!component->dapm_ptr)
4070 component->dapm_ptr = &component->dapm;
4071
4072 dapm = component->dapm_ptr;
4073 dapm->dev = dev;
4074 dapm->component = component;
4075 dapm->bias_level = SND_SOC_BIAS_OFF;
14e8bdeb
LPC
4076 if (driver->seq_notifier)
4077 dapm->seq_notifier = snd_soc_component_seq_notifier;
4078 if (driver->stream_event)
4079 dapm->stream_event = snd_soc_component_stream_event;
d191bd8d 4080
61aca564
LPC
4081 component->controls = driver->controls;
4082 component->num_controls = driver->num_controls;
4083 component->dapm_widgets = driver->dapm_widgets;
4084 component->num_dapm_widgets = driver->num_dapm_widgets;
4085 component->dapm_routes = driver->dapm_routes;
4086 component->num_dapm_routes = driver->num_dapm_routes;
4087
bb13109d
LPC
4088 INIT_LIST_HEAD(&component->dai_list);
4089 mutex_init(&component->io_mutex);
d191bd8d 4090
bb13109d
LPC
4091 return 0;
4092}
d191bd8d 4093
bb13109d
LPC
4094static void snd_soc_component_add_unlocked(struct snd_soc_component *component)
4095{
4096 list_add(&component->list, &component_list);
4097}
d191bd8d 4098
bb13109d
LPC
4099static void snd_soc_component_add(struct snd_soc_component *component)
4100{
d191bd8d 4101 mutex_lock(&client_mutex);
bb13109d 4102 snd_soc_component_add_unlocked(component);
d191bd8d 4103 mutex_unlock(&client_mutex);
bb13109d 4104}
d191bd8d 4105
bb13109d
LPC
4106static void snd_soc_component_cleanup(struct snd_soc_component *component)
4107{
4108 snd_soc_unregister_dais(component);
4109 kfree(component->name);
4110}
d191bd8d 4111
bb13109d
LPC
4112static void snd_soc_component_del_unlocked(struct snd_soc_component *component)
4113{
4114 list_del(&component->list);
4115}
d191bd8d 4116
bb13109d
LPC
4117static void snd_soc_component_del(struct snd_soc_component *component)
4118{
4119 mutex_lock(&client_mutex);
4120 snd_soc_component_del_unlocked(component);
4121 mutex_unlock(&client_mutex);
d191bd8d
KM
4122}
4123
4124int snd_soc_register_component(struct device *dev,
4125 const struct snd_soc_component_driver *cmpnt_drv,
4126 struct snd_soc_dai_driver *dai_drv,
4127 int num_dai)
4128{
4129 struct snd_soc_component *cmpnt;
bb13109d 4130 int ret;
d191bd8d 4131
bb13109d 4132 cmpnt = kzalloc(sizeof(*cmpnt), GFP_KERNEL);
d191bd8d
KM
4133 if (!cmpnt) {
4134 dev_err(dev, "ASoC: Failed to allocate memory\n");
4135 return -ENOMEM;
4136 }
4137
bb13109d
LPC
4138 ret = snd_soc_component_initialize(cmpnt, cmpnt_drv, dev);
4139 if (ret)
4140 goto err_free;
4141
3d59400f 4142 cmpnt->ignore_pmdown_time = true;
98e639fb 4143 cmpnt->registered_as_component = true;
3d59400f 4144
bb13109d
LPC
4145 ret = snd_soc_register_dais(cmpnt, dai_drv, num_dai, true);
4146 if (ret < 0) {
4147 dev_err(dev, "ASoC: Failed to regster DAIs: %d\n", ret);
4148 goto err_cleanup;
4149 }
d191bd8d 4150
bb13109d 4151 snd_soc_component_add(cmpnt);
4da53393 4152
bb13109d 4153 return 0;
4da53393 4154
bb13109d
LPC
4155err_cleanup:
4156 snd_soc_component_cleanup(cmpnt);
4157err_free:
4158 kfree(cmpnt);
4159 return ret;
4da53393 4160}
bb13109d 4161EXPORT_SYMBOL_GPL(snd_soc_register_component);
4da53393 4162
d191bd8d
KM
4163/**
4164 * snd_soc_unregister_component - Unregister a component from the ASoC core
4165 *
4166 */
4167void snd_soc_unregister_component(struct device *dev)
4168{
4169 struct snd_soc_component *cmpnt;
4170
4171 list_for_each_entry(cmpnt, &component_list, list) {
98e639fb 4172 if (dev == cmpnt->dev && cmpnt->registered_as_component)
d191bd8d
KM
4173 goto found;
4174 }
4175 return;
4176
4177found:
bb13109d
LPC
4178 snd_soc_component_del(cmpnt);
4179 snd_soc_component_cleanup(cmpnt);
4180 kfree(cmpnt);
d191bd8d
KM
4181}
4182EXPORT_SYMBOL_GPL(snd_soc_unregister_component);
d191bd8d 4183
f1d45cc3
LPC
4184static int snd_soc_platform_drv_probe(struct snd_soc_component *component)
4185{
4186 struct snd_soc_platform *platform = snd_soc_component_to_platform(component);
4187
4188 return platform->driver->probe(platform);
4189}
4190
4191static void snd_soc_platform_drv_remove(struct snd_soc_component *component)
4192{
4193 struct snd_soc_platform *platform = snd_soc_component_to_platform(component);
4194
4195 platform->driver->remove(platform);
4196}
4197
e2c330b9
LPC
4198static int snd_soc_platform_drv_write(struct snd_soc_component *component,
4199 unsigned int reg, unsigned int val)
4200{
4201 struct snd_soc_platform *platform = snd_soc_component_to_platform(component);
d191bd8d 4202
e2c330b9
LPC
4203 return platform->driver->write(platform, reg, val);
4204}
4205
4206static int snd_soc_platform_drv_read(struct snd_soc_component *component,
4207 unsigned int reg, unsigned int *val)
4208{
4209 struct snd_soc_platform *platform = snd_soc_component_to_platform(component);
4210
4211 *val = platform->driver->read(platform, reg);
4212
4213 return 0;
d191bd8d 4214}
d191bd8d 4215
12a48a8c 4216/**
71a45cda
LPC
4217 * snd_soc_add_platform - Add a platform to the ASoC core
4218 * @dev: The parent device for the platform
4219 * @platform: The platform to add
4220 * @platform_driver: The driver for the platform
12a48a8c 4221 */
71a45cda 4222int snd_soc_add_platform(struct device *dev, struct snd_soc_platform *platform,
d79e57db 4223 const struct snd_soc_platform_driver *platform_drv)
12a48a8c 4224{
b37f1d12
LPC
4225 int ret;
4226
bb13109d
LPC
4227 ret = snd_soc_component_initialize(&platform->component,
4228 &platform_drv->component_driver, dev);
4229 if (ret)
4230 return ret;
12a48a8c 4231
f0fba2ad
LG
4232 platform->dev = dev;
4233 platform->driver = platform_drv;
f1d45cc3
LPC
4234 if (platform_drv->controls) {
4235 platform->component.controls = platform_drv->controls;
4236 platform->component.num_controls = platform_drv->num_controls;
4237 }
4238 if (platform_drv->dapm_widgets) {
4239 platform->component.dapm_widgets = platform_drv->dapm_widgets;
4240 platform->component.num_dapm_widgets = platform_drv->num_dapm_widgets;
4241 platform->component.steal_sibling_dai_widgets = true;
4242 }
4243 if (platform_drv->dapm_routes) {
4244 platform->component.dapm_routes = platform_drv->dapm_routes;
4245 platform->component.num_dapm_routes = platform_drv->num_dapm_routes;
4246 }
4247
4248 if (platform_drv->probe)
4249 platform->component.probe = snd_soc_platform_drv_probe;
4250 if (platform_drv->remove)
4251 platform->component.remove = snd_soc_platform_drv_remove;
e2c330b9
LPC
4252 if (platform_drv->write)
4253 platform->component.write = snd_soc_platform_drv_write;
4254 if (platform_drv->read)
4255 platform->component.read = snd_soc_platform_drv_read;
12a48a8c 4256
81c7cfd1
LPC
4257#ifdef CONFIG_DEBUG_FS
4258 platform->component.debugfs_prefix = "platform";
4259#endif
4260
12a48a8c 4261 mutex_lock(&client_mutex);
bb13109d 4262 snd_soc_component_add_unlocked(&platform->component);
12a48a8c
MB
4263 list_add(&platform->list, &platform_list);
4264 mutex_unlock(&client_mutex);
4265
f4333203
LPC
4266 dev_dbg(dev, "ASoC: Registered platform '%s'\n",
4267 platform->component.name);
12a48a8c
MB
4268
4269 return 0;
4270}
71a45cda 4271EXPORT_SYMBOL_GPL(snd_soc_add_platform);
12a48a8c
MB
4272
4273/**
71a45cda 4274 * snd_soc_register_platform - Register a platform with the ASoC core
12a48a8c 4275 *
71a45cda 4276 * @platform: platform to register
12a48a8c 4277 */
71a45cda
LPC
4278int snd_soc_register_platform(struct device *dev,
4279 const struct snd_soc_platform_driver *platform_drv)
12a48a8c 4280{
f0fba2ad 4281 struct snd_soc_platform *platform;
71a45cda 4282 int ret;
f0fba2ad 4283
71a45cda 4284 dev_dbg(dev, "ASoC: platform register %s\n", dev_name(dev));
f0fba2ad 4285
71a45cda
LPC
4286 platform = kzalloc(sizeof(struct snd_soc_platform), GFP_KERNEL);
4287 if (platform == NULL)
4288 return -ENOMEM;
4289
4290 ret = snd_soc_add_platform(dev, platform, platform_drv);
4291 if (ret)
4292 kfree(platform);
4293
4294 return ret;
4295}
4296EXPORT_SYMBOL_GPL(snd_soc_register_platform);
4297
4298/**
4299 * snd_soc_remove_platform - Remove a platform from the ASoC core
4300 * @platform: the platform to remove
4301 */
4302void snd_soc_remove_platform(struct snd_soc_platform *platform)
4303{
b37f1d12 4304
12a48a8c
MB
4305 mutex_lock(&client_mutex);
4306 list_del(&platform->list);
bb13109d 4307 snd_soc_component_del_unlocked(&platform->component);
12a48a8c
MB
4308 mutex_unlock(&client_mutex);
4309
bb13109d
LPC
4310 snd_soc_component_cleanup(&platform->component);
4311
71a45cda 4312 dev_dbg(platform->dev, "ASoC: Unregistered platform '%s'\n",
f4333203 4313 platform->component.name);
71a45cda
LPC
4314}
4315EXPORT_SYMBOL_GPL(snd_soc_remove_platform);
4316
4317struct snd_soc_platform *snd_soc_lookup_platform(struct device *dev)
4318{
4319 struct snd_soc_platform *platform;
4320
4321 list_for_each_entry(platform, &platform_list, list) {
4322 if (dev == platform->dev)
4323 return platform;
4324 }
4325
4326 return NULL;
4327}
4328EXPORT_SYMBOL_GPL(snd_soc_lookup_platform);
4329
4330/**
4331 * snd_soc_unregister_platform - Unregister a platform from the ASoC core
4332 *
4333 * @platform: platform to unregister
4334 */
4335void snd_soc_unregister_platform(struct device *dev)
4336{
4337 struct snd_soc_platform *platform;
4338
4339 platform = snd_soc_lookup_platform(dev);
4340 if (!platform)
4341 return;
4342
4343 snd_soc_remove_platform(platform);
f0fba2ad 4344 kfree(platform);
12a48a8c
MB
4345}
4346EXPORT_SYMBOL_GPL(snd_soc_unregister_platform);
4347
151ab22c
MB
4348static u64 codec_format_map[] = {
4349 SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S16_BE,
4350 SNDRV_PCM_FMTBIT_U16_LE | SNDRV_PCM_FMTBIT_U16_BE,
4351 SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S24_BE,
4352 SNDRV_PCM_FMTBIT_U24_LE | SNDRV_PCM_FMTBIT_U24_BE,
4353 SNDRV_PCM_FMTBIT_S32_LE | SNDRV_PCM_FMTBIT_S32_BE,
4354 SNDRV_PCM_FMTBIT_U32_LE | SNDRV_PCM_FMTBIT_U32_BE,
4355 SNDRV_PCM_FMTBIT_S24_3LE | SNDRV_PCM_FMTBIT_U24_3BE,
4356 SNDRV_PCM_FMTBIT_U24_3LE | SNDRV_PCM_FMTBIT_U24_3BE,
4357 SNDRV_PCM_FMTBIT_S20_3LE | SNDRV_PCM_FMTBIT_S20_3BE,
4358 SNDRV_PCM_FMTBIT_U20_3LE | SNDRV_PCM_FMTBIT_U20_3BE,
4359 SNDRV_PCM_FMTBIT_S18_3LE | SNDRV_PCM_FMTBIT_S18_3BE,
4360 SNDRV_PCM_FMTBIT_U18_3LE | SNDRV_PCM_FMTBIT_U18_3BE,
4361 SNDRV_PCM_FMTBIT_FLOAT_LE | SNDRV_PCM_FMTBIT_FLOAT_BE,
4362 SNDRV_PCM_FMTBIT_FLOAT64_LE | SNDRV_PCM_FMTBIT_FLOAT64_BE,
4363 SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_LE
4364 | SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_BE,
4365};
4366
4367/* Fix up the DAI formats for endianness: codecs don't actually see
4368 * the endianness of the data but we're using the CPU format
4369 * definitions which do need to include endianness so we ensure that
4370 * codec DAIs always have both big and little endian variants set.
4371 */
4372static void fixup_codec_formats(struct snd_soc_pcm_stream *stream)
4373{
4374 int i;
4375
4376 for (i = 0; i < ARRAY_SIZE(codec_format_map); i++)
4377 if (stream->formats & codec_format_map[i])
4378 stream->formats |= codec_format_map[i];
4379}
4380
f1d45cc3
LPC
4381static int snd_soc_codec_drv_probe(struct snd_soc_component *component)
4382{
4383 struct snd_soc_codec *codec = snd_soc_component_to_codec(component);
4384
4385 return codec->driver->probe(codec);
4386}
4387
4388static void snd_soc_codec_drv_remove(struct snd_soc_component *component)
4389{
4390 struct snd_soc_codec *codec = snd_soc_component_to_codec(component);
4391
4392 codec->driver->remove(codec);
4393}
4394
e2c330b9
LPC
4395static int snd_soc_codec_drv_write(struct snd_soc_component *component,
4396 unsigned int reg, unsigned int val)
4397{
4398 struct snd_soc_codec *codec = snd_soc_component_to_codec(component);
4399
4400 return codec->driver->write(codec, reg, val);
4401}
4402
4403static int snd_soc_codec_drv_read(struct snd_soc_component *component,
4404 unsigned int reg, unsigned int *val)
4405{
4406 struct snd_soc_codec *codec = snd_soc_component_to_codec(component);
4407
4408 *val = codec->driver->read(codec, reg);
4409
4410 return 0;
4411}
4412
68f831c2
LPC
4413static int snd_soc_codec_set_bias_level(struct snd_soc_dapm_context *dapm,
4414 enum snd_soc_bias_level level)
4415{
4416 struct snd_soc_codec *codec = snd_soc_dapm_to_codec(dapm);
4417
4418 return codec->driver->set_bias_level(codec, level);
4419}
4420
0d0cf00a
MB
4421/**
4422 * snd_soc_register_codec - Register a codec with the ASoC core
4423 *
ac11a2b3 4424 * @codec: codec to register
0d0cf00a 4425 */
f0fba2ad 4426int snd_soc_register_codec(struct device *dev,
001ae4c0
MB
4427 const struct snd_soc_codec_driver *codec_drv,
4428 struct snd_soc_dai_driver *dai_drv,
4429 int num_dai)
0d0cf00a 4430{
f0fba2ad 4431 struct snd_soc_codec *codec;
bb13109d 4432 struct snd_soc_dai *dai;
a39f75f7 4433 struct regmap *regmap;
f0fba2ad 4434 int ret, i;
151ab22c 4435
f0fba2ad
LG
4436 dev_dbg(dev, "codec register %s\n", dev_name(dev));
4437
4438 codec = kzalloc(sizeof(struct snd_soc_codec), GFP_KERNEL);
4439 if (codec == NULL)
4440 return -ENOMEM;
0d0cf00a 4441
ce0fc93a 4442 codec->component.dapm_ptr = &codec->dapm;
f1d45cc3 4443 codec->component.codec = codec;
ce0fc93a 4444
bb13109d
LPC
4445 ret = snd_soc_component_initialize(&codec->component,
4446 &codec_drv->component_driver, dev);
4447 if (ret)
4448 goto err_free;
f0fba2ad 4449
f1d45cc3
LPC
4450 if (codec_drv->controls) {
4451 codec->component.controls = codec_drv->controls;
4452 codec->component.num_controls = codec_drv->num_controls;
4453 }
4454 if (codec_drv->dapm_widgets) {
4455 codec->component.dapm_widgets = codec_drv->dapm_widgets;
4456 codec->component.num_dapm_widgets = codec_drv->num_dapm_widgets;
4457 }
4458 if (codec_drv->dapm_routes) {
4459 codec->component.dapm_routes = codec_drv->dapm_routes;
4460 codec->component.num_dapm_routes = codec_drv->num_dapm_routes;
4461 }
4462
4463 if (codec_drv->probe)
4464 codec->component.probe = snd_soc_codec_drv_probe;
4465 if (codec_drv->remove)
4466 codec->component.remove = snd_soc_codec_drv_remove;
e2c330b9
LPC
4467 if (codec_drv->write)
4468 codec->component.write = snd_soc_codec_drv_write;
4469 if (codec_drv->read)
4470 codec->component.read = snd_soc_codec_drv_read;
3d59400f 4471 codec->component.ignore_pmdown_time = codec_drv->ignore_pmdown_time;
ce6120cc 4472 codec->dapm.codec = codec;
14e8bdeb
LPC
4473 if (codec_drv->seq_notifier)
4474 codec->dapm.seq_notifier = codec_drv->seq_notifier;
68f831c2
LPC
4475 if (codec_drv->set_bias_level)
4476 codec->dapm.set_bias_level = snd_soc_codec_set_bias_level;
7a30a3db
DP
4477 codec->dev = dev;
4478 codec->driver = codec_drv;
e2c330b9 4479 codec->component.val_bytes = codec_drv->reg_word_size;
7a30a3db 4480 mutex_init(&codec->mutex);
ce6120cc 4481
81c7cfd1
LPC
4482#ifdef CONFIG_DEBUG_FS
4483 codec->component.init_debugfs = soc_init_codec_debugfs;
4484 codec->component.debugfs_prefix = "codec";
4485#endif
4486
e2c330b9 4487 if (!codec->component.write) {
a39f75f7
XL
4488 if (codec_drv->get_regmap)
4489 regmap = codec_drv->get_regmap(dev);
4490 else
4491 regmap = dev_get_regmap(dev, NULL);
4492
4493 if (regmap) {
e2c330b9
LPC
4494 ret = snd_soc_component_init_io(&codec->component,
4495 regmap);
4496 if (ret) {
a39f75f7
XL
4497 dev_err(codec->dev,
4498 "Failed to set cache I/O:%d\n",
4499 ret);
bb13109d 4500 goto err_cleanup;
a39f75f7
XL
4501 }
4502 }
4503 }
4504
f0fba2ad
LG
4505 for (i = 0; i < num_dai; i++) {
4506 fixup_codec_formats(&dai_drv[i].playback);
4507 fixup_codec_formats(&dai_drv[i].capture);
4508 }
4509
bb13109d 4510 ret = snd_soc_register_dais(&codec->component, dai_drv, num_dai, false);
5acd7dfb 4511 if (ret < 0) {
bb13109d
LPC
4512 dev_err(dev, "ASoC: Failed to regster DAIs: %d\n", ret);
4513 goto err_cleanup;
26b01ccd 4514 }
f0fba2ad 4515
bb13109d
LPC
4516 list_for_each_entry(dai, &codec->component.dai_list, list)
4517 dai->codec = codec;
f0fba2ad 4518
e1328a83 4519 mutex_lock(&client_mutex);
bb13109d 4520 snd_soc_component_add_unlocked(&codec->component);
054880fe 4521 list_add(&codec->list, &codec_list);
e1328a83
KM
4522 mutex_unlock(&client_mutex);
4523
f4333203
LPC
4524 dev_dbg(codec->dev, "ASoC: Registered codec '%s'\n",
4525 codec->component.name);
0d0cf00a 4526 return 0;
f0fba2ad 4527
bb13109d
LPC
4528err_cleanup:
4529 snd_soc_component_cleanup(&codec->component);
4530err_free:
f0fba2ad
LG
4531 kfree(codec);
4532 return ret;
0d0cf00a
MB
4533}
4534EXPORT_SYMBOL_GPL(snd_soc_register_codec);
4535
4536/**
4537 * snd_soc_unregister_codec - Unregister a codec from the ASoC core
4538 *
ac11a2b3 4539 * @codec: codec to unregister
0d0cf00a 4540 */
f0fba2ad 4541void snd_soc_unregister_codec(struct device *dev)
0d0cf00a 4542{
f0fba2ad 4543 struct snd_soc_codec *codec;
f0fba2ad
LG
4544
4545 list_for_each_entry(codec, &codec_list, list) {
4546 if (dev == codec->dev)
4547 goto found;
4548 }
4549 return;
4550
4551found:
f0fba2ad 4552
0d0cf00a
MB
4553 mutex_lock(&client_mutex);
4554 list_del(&codec->list);
bb13109d 4555 snd_soc_component_del_unlocked(&codec->component);
0d0cf00a
MB
4556 mutex_unlock(&client_mutex);
4557
f4333203
LPC
4558 dev_dbg(codec->dev, "ASoC: Unregistered codec '%s'\n",
4559 codec->component.name);
f0fba2ad 4560
bb13109d 4561 snd_soc_component_cleanup(&codec->component);
7a30a3db 4562 snd_soc_cache_exit(codec);
f0fba2ad 4563 kfree(codec);
0d0cf00a
MB
4564}
4565EXPORT_SYMBOL_GPL(snd_soc_unregister_codec);
4566
bec4fa05
SW
4567/* Retrieve a card's name from device tree */
4568int snd_soc_of_parse_card_name(struct snd_soc_card *card,
4569 const char *propname)
4570{
7e07e7c0 4571 struct device_node *np;
bec4fa05
SW
4572 int ret;
4573
7e07e7c0
TB
4574 if (!card->dev) {
4575 pr_err("card->dev is not set before calling %s\n", __func__);
4576 return -EINVAL;
4577 }
4578
4579 np = card->dev->of_node;
4580
bec4fa05
SW
4581 ret = of_property_read_string_index(np, propname, 0, &card->name);
4582 /*
4583 * EINVAL means the property does not exist. This is fine providing
4584 * card->name was previously set, which is checked later in
4585 * snd_soc_register_card.
4586 */
4587 if (ret < 0 && ret != -EINVAL) {
4588 dev_err(card->dev,
f110bfc7 4589 "ASoC: Property '%s' could not be read: %d\n",
bec4fa05
SW
4590 propname, ret);
4591 return ret;
4592 }
4593
4594 return 0;
4595}
4596EXPORT_SYMBOL_GPL(snd_soc_of_parse_card_name);
4597
9a6d4860
XL
4598static const struct snd_soc_dapm_widget simple_widgets[] = {
4599 SND_SOC_DAPM_MIC("Microphone", NULL),
4600 SND_SOC_DAPM_LINE("Line", NULL),
4601 SND_SOC_DAPM_HP("Headphone", NULL),
4602 SND_SOC_DAPM_SPK("Speaker", NULL),
4603};
4604
4605int snd_soc_of_parse_audio_simple_widgets(struct snd_soc_card *card,
4606 const char *propname)
4607{
4608 struct device_node *np = card->dev->of_node;
4609 struct snd_soc_dapm_widget *widgets;
4610 const char *template, *wname;
4611 int i, j, num_widgets, ret;
4612
4613 num_widgets = of_property_count_strings(np, propname);
4614 if (num_widgets < 0) {
4615 dev_err(card->dev,
4616 "ASoC: Property '%s' does not exist\n", propname);
4617 return -EINVAL;
4618 }
4619 if (num_widgets & 1) {
4620 dev_err(card->dev,
4621 "ASoC: Property '%s' length is not even\n", propname);
4622 return -EINVAL;
4623 }
4624
4625 num_widgets /= 2;
4626 if (!num_widgets) {
4627 dev_err(card->dev, "ASoC: Property '%s's length is zero\n",
4628 propname);
4629 return -EINVAL;
4630 }
4631
4632 widgets = devm_kcalloc(card->dev, num_widgets, sizeof(*widgets),
4633 GFP_KERNEL);
4634 if (!widgets) {
4635 dev_err(card->dev,
4636 "ASoC: Could not allocate memory for widgets\n");
4637 return -ENOMEM;
4638 }
4639
4640 for (i = 0; i < num_widgets; i++) {
4641 ret = of_property_read_string_index(np, propname,
4642 2 * i, &template);
4643 if (ret) {
4644 dev_err(card->dev,
4645 "ASoC: Property '%s' index %d read error:%d\n",
4646 propname, 2 * i, ret);
4647 return -EINVAL;
4648 }
4649
4650 for (j = 0; j < ARRAY_SIZE(simple_widgets); j++) {
4651 if (!strncmp(template, simple_widgets[j].name,
4652 strlen(simple_widgets[j].name))) {
4653 widgets[i] = simple_widgets[j];
4654 break;
4655 }
4656 }
4657
4658 if (j >= ARRAY_SIZE(simple_widgets)) {
4659 dev_err(card->dev,
4660 "ASoC: DAPM widget '%s' is not supported\n",
4661 template);
4662 return -EINVAL;
4663 }
4664
4665 ret = of_property_read_string_index(np, propname,
4666 (2 * i) + 1,
4667 &wname);
4668 if (ret) {
4669 dev_err(card->dev,
4670 "ASoC: Property '%s' index %d read error:%d\n",
4671 propname, (2 * i) + 1, ret);
4672 return -EINVAL;
4673 }
4674
4675 widgets[i].name = wname;
4676 }
4677
4678 card->dapm_widgets = widgets;
4679 card->num_dapm_widgets = num_widgets;
4680
4681 return 0;
4682}
4683EXPORT_SYMBOL_GPL(snd_soc_of_parse_audio_simple_widgets);
4684
89c67857
XL
4685int snd_soc_of_parse_tdm_slot(struct device_node *np,
4686 unsigned int *slots,
4687 unsigned int *slot_width)
4688{
4689 u32 val;
4690 int ret;
4691
4692 if (of_property_read_bool(np, "dai-tdm-slot-num")) {
4693 ret = of_property_read_u32(np, "dai-tdm-slot-num", &val);
4694 if (ret)
4695 return ret;
4696
4697 if (slots)
4698 *slots = val;
4699 }
4700
4701 if (of_property_read_bool(np, "dai-tdm-slot-width")) {
4702 ret = of_property_read_u32(np, "dai-tdm-slot-width", &val);
4703 if (ret)
4704 return ret;
4705
4706 if (slot_width)
4707 *slot_width = val;
4708 }
4709
4710 return 0;
4711}
4712EXPORT_SYMBOL_GPL(snd_soc_of_parse_tdm_slot);
4713
a4a54dd5
SW
4714int snd_soc_of_parse_audio_routing(struct snd_soc_card *card,
4715 const char *propname)
4716{
4717 struct device_node *np = card->dev->of_node;
4718 int num_routes;
4719 struct snd_soc_dapm_route *routes;
4720 int i, ret;
4721
4722 num_routes = of_property_count_strings(np, propname);
c34ce320 4723 if (num_routes < 0 || num_routes & 1) {
10e8aa9a
MM
4724 dev_err(card->dev,
4725 "ASoC: Property '%s' does not exist or its length is not even\n",
4726 propname);
a4a54dd5
SW
4727 return -EINVAL;
4728 }
4729 num_routes /= 2;
4730 if (!num_routes) {
f110bfc7 4731 dev_err(card->dev, "ASoC: Property '%s's length is zero\n",
a4a54dd5
SW
4732 propname);
4733 return -EINVAL;
4734 }
4735
4736 routes = devm_kzalloc(card->dev, num_routes * sizeof(*routes),
4737 GFP_KERNEL);
4738 if (!routes) {
4739 dev_err(card->dev,
f110bfc7 4740 "ASoC: Could not allocate DAPM route table\n");
a4a54dd5
SW
4741 return -EINVAL;
4742 }
4743
4744 for (i = 0; i < num_routes; i++) {
4745 ret = of_property_read_string_index(np, propname,
4746 2 * i, &routes[i].sink);
4747 if (ret) {
c871bd0b
MB
4748 dev_err(card->dev,
4749 "ASoC: Property '%s' index %d could not be read: %d\n",
4750 propname, 2 * i, ret);
a4a54dd5
SW
4751 return -EINVAL;
4752 }
4753 ret = of_property_read_string_index(np, propname,
4754 (2 * i) + 1, &routes[i].source);
4755 if (ret) {
4756 dev_err(card->dev,
c871bd0b
MB
4757 "ASoC: Property '%s' index %d could not be read: %d\n",
4758 propname, (2 * i) + 1, ret);
a4a54dd5
SW
4759 return -EINVAL;
4760 }
4761 }
4762
4763 card->num_dapm_routes = num_routes;
4764 card->dapm_routes = routes;
4765
4766 return 0;
4767}
4768EXPORT_SYMBOL_GPL(snd_soc_of_parse_audio_routing);
4769
a7930ed4 4770unsigned int snd_soc_of_parse_daifmt(struct device_node *np,
389cb834
JS
4771 const char *prefix,
4772 struct device_node **bitclkmaster,
4773 struct device_node **framemaster)
a7930ed4
KM
4774{
4775 int ret, i;
4776 char prop[128];
4777 unsigned int format = 0;
4778 int bit, frame;
4779 const char *str;
4780 struct {
4781 char *name;
4782 unsigned int val;
4783 } of_fmt_table[] = {
4784 { "i2s", SND_SOC_DAIFMT_I2S },
4785 { "right_j", SND_SOC_DAIFMT_RIGHT_J },
4786 { "left_j", SND_SOC_DAIFMT_LEFT_J },
4787 { "dsp_a", SND_SOC_DAIFMT_DSP_A },
4788 { "dsp_b", SND_SOC_DAIFMT_DSP_B },
4789 { "ac97", SND_SOC_DAIFMT_AC97 },
4790 { "pdm", SND_SOC_DAIFMT_PDM},
4791 { "msb", SND_SOC_DAIFMT_MSB },
4792 { "lsb", SND_SOC_DAIFMT_LSB },
a7930ed4
KM
4793 };
4794
4795 if (!prefix)
4796 prefix = "";
4797
4798 /*
4799 * check "[prefix]format = xxx"
4800 * SND_SOC_DAIFMT_FORMAT_MASK area
4801 */
4802 snprintf(prop, sizeof(prop), "%sformat", prefix);
4803 ret = of_property_read_string(np, prop, &str);
4804 if (ret == 0) {
4805 for (i = 0; i < ARRAY_SIZE(of_fmt_table); i++) {
4806 if (strcmp(str, of_fmt_table[i].name) == 0) {
4807 format |= of_fmt_table[i].val;
4808 break;
4809 }
4810 }
4811 }
4812
4813 /*
8c2d6a9f 4814 * check "[prefix]continuous-clock"
a7930ed4
KM
4815 * SND_SOC_DAIFMT_CLOCK_MASK area
4816 */
8c2d6a9f
KM
4817 snprintf(prop, sizeof(prop), "%scontinuous-clock", prefix);
4818 if (of_get_property(np, prop, NULL))
4819 format |= SND_SOC_DAIFMT_CONT;
4820 else
4821 format |= SND_SOC_DAIFMT_GATED;
a7930ed4
KM
4822
4823 /*
4824 * check "[prefix]bitclock-inversion"
4825 * check "[prefix]frame-inversion"
4826 * SND_SOC_DAIFMT_INV_MASK area
4827 */
4828 snprintf(prop, sizeof(prop), "%sbitclock-inversion", prefix);
4829 bit = !!of_get_property(np, prop, NULL);
4830
4831 snprintf(prop, sizeof(prop), "%sframe-inversion", prefix);
4832 frame = !!of_get_property(np, prop, NULL);
4833
4834 switch ((bit << 4) + frame) {
4835 case 0x11:
4836 format |= SND_SOC_DAIFMT_IB_IF;
4837 break;
4838 case 0x10:
4839 format |= SND_SOC_DAIFMT_IB_NF;
4840 break;
4841 case 0x01:
4842 format |= SND_SOC_DAIFMT_NB_IF;
4843 break;
4844 default:
4845 /* SND_SOC_DAIFMT_NB_NF is default */
4846 break;
4847 }
4848
4849 /*
4850 * check "[prefix]bitclock-master"
4851 * check "[prefix]frame-master"
4852 * SND_SOC_DAIFMT_MASTER_MASK area
4853 */
4854 snprintf(prop, sizeof(prop), "%sbitclock-master", prefix);
4855 bit = !!of_get_property(np, prop, NULL);
389cb834
JS
4856 if (bit && bitclkmaster)
4857 *bitclkmaster = of_parse_phandle(np, prop, 0);
a7930ed4
KM
4858
4859 snprintf(prop, sizeof(prop), "%sframe-master", prefix);
4860 frame = !!of_get_property(np, prop, NULL);
389cb834
JS
4861 if (frame && framemaster)
4862 *framemaster = of_parse_phandle(np, prop, 0);
a7930ed4
KM
4863
4864 switch ((bit << 4) + frame) {
4865 case 0x11:
4866 format |= SND_SOC_DAIFMT_CBM_CFM;
4867 break;
4868 case 0x10:
4869 format |= SND_SOC_DAIFMT_CBM_CFS;
4870 break;
4871 case 0x01:
4872 format |= SND_SOC_DAIFMT_CBS_CFM;
4873 break;
4874 default:
4875 format |= SND_SOC_DAIFMT_CBS_CFS;
4876 break;
4877 }
4878
4879 return format;
4880}
4881EXPORT_SYMBOL_GPL(snd_soc_of_parse_daifmt);
4882
cb470087
KM
4883int snd_soc_of_get_dai_name(struct device_node *of_node,
4884 const char **dai_name)
4885{
4886 struct snd_soc_component *pos;
4887 struct of_phandle_args args;
4888 int ret;
4889
4890 ret = of_parse_phandle_with_args(of_node, "sound-dai",
4891 "#sound-dai-cells", 0, &args);
4892 if (ret)
4893 return ret;
4894
4895 ret = -EPROBE_DEFER;
4896
4897 mutex_lock(&client_mutex);
4898 list_for_each_entry(pos, &component_list, list) {
4899 if (pos->dev->of_node != args.np)
4900 continue;
4901
6833c452
KM
4902 if (pos->driver->of_xlate_dai_name) {
4903 ret = pos->driver->of_xlate_dai_name(pos, &args, dai_name);
4904 } else {
4905 int id = -1;
4906
4907 switch (args.args_count) {
4908 case 0:
4909 id = 0; /* same as dai_drv[0] */
4910 break;
4911 case 1:
4912 id = args.args[0];
4913 break;
4914 default:
4915 /* not supported */
4916 break;
4917 }
4918
4919 if (id < 0 || id >= pos->num_dai) {
4920 ret = -EINVAL;
3dcba280 4921 continue;
6833c452 4922 }
e41975ed
XL
4923
4924 ret = 0;
4925
4926 *dai_name = pos->dai_drv[id].name;
4927 if (!*dai_name)
4928 *dai_name = pos->name;
cb470087
KM
4929 }
4930
cb470087
KM
4931 break;
4932 }
4933 mutex_unlock(&client_mutex);
4934
4935 of_node_put(args.np);
4936
4937 return ret;
4938}
4939EXPORT_SYMBOL_GPL(snd_soc_of_get_dai_name);
4940
c9b3a40f 4941static int __init snd_soc_init(void)
db2a4165 4942{
384c89e2 4943#ifdef CONFIG_DEBUG_FS
8a9dab1a
MB
4944 snd_soc_debugfs_root = debugfs_create_dir("asoc", NULL);
4945 if (IS_ERR(snd_soc_debugfs_root) || !snd_soc_debugfs_root) {
0837fc62 4946 pr_warn("ASoC: Failed to create debugfs directory\n");
8a9dab1a 4947 snd_soc_debugfs_root = NULL;
384c89e2 4948 }
c3c5a19a 4949
8a9dab1a 4950 if (!debugfs_create_file("codecs", 0444, snd_soc_debugfs_root, NULL,
c3c5a19a
MB
4951 &codec_list_fops))
4952 pr_warn("ASoC: Failed to create CODEC list debugfs file\n");
4953
8a9dab1a 4954 if (!debugfs_create_file("dais", 0444, snd_soc_debugfs_root, NULL,
f3208780
MB
4955 &dai_list_fops))
4956 pr_warn("ASoC: Failed to create DAI list debugfs file\n");
19c7ac27 4957
8a9dab1a 4958 if (!debugfs_create_file("platforms", 0444, snd_soc_debugfs_root, NULL,
19c7ac27
MB
4959 &platform_list_fops))
4960 pr_warn("ASoC: Failed to create platform list debugfs file\n");
384c89e2
MB
4961#endif
4962
fb257897
MB
4963 snd_soc_util_init();
4964
db2a4165
FM
4965 return platform_driver_register(&soc_driver);
4966}
4abe8e16 4967module_init(snd_soc_init);
db2a4165 4968
7d8c16a6 4969static void __exit snd_soc_exit(void)
db2a4165 4970{
fb257897
MB
4971 snd_soc_util_exit();
4972
384c89e2 4973#ifdef CONFIG_DEBUG_FS
8a9dab1a 4974 debugfs_remove_recursive(snd_soc_debugfs_root);
384c89e2 4975#endif
3ff3f64b 4976 platform_driver_unregister(&soc_driver);
db2a4165 4977}
db2a4165
FM
4978module_exit(snd_soc_exit);
4979
4980/* Module information */
d331124d 4981MODULE_AUTHOR("Liam Girdwood, lrg@slimlogic.co.uk");
db2a4165
FM
4982MODULE_DESCRIPTION("ALSA SoC Core");
4983MODULE_LICENSE("GPL");
8b45a209 4984MODULE_ALIAS("platform:soc-audio");