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