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