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