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