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