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