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