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