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