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