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