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