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