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