ixgbe: remove useless bd_number from adapter struct
[linux-2.6/btrfs-unstable.git] / sound / soc / soc-core.c
blob889f4e3d35dc53548f59f159666fbc668f7bd39b
1 /*
2 * soc-core.c -- ALSA SoC Audio Layer
4 * Copyright 2005 Wolfson Microelectronics PLC.
5 * Copyright 2005 Openedhand Ltd.
6 * Copyright (C) 2010 Slimlogic Ltd.
7 * Copyright (C) 2010 Texas Instruments Inc.
9 * Author: Liam Girdwood <lrg@slimlogic.co.uk>
10 * with code, comments and ideas from :-
11 * Richard Purdie <richard@openedhand.com>
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.
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
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>
31 #include <linux/debugfs.h>
32 #include <linux/platform_device.h>
33 #include <linux/pinctrl/consumer.h>
34 #include <linux/ctype.h>
35 #include <linux/slab.h>
36 #include <linux/of.h>
37 #include <linux/gpio.h>
38 #include <linux/of_gpio.h>
39 #include <sound/ac97_codec.h>
40 #include <sound/core.h>
41 #include <sound/jack.h>
42 #include <sound/pcm.h>
43 #include <sound/pcm_params.h>
44 #include <sound/soc.h>
45 #include <sound/soc-dpcm.h>
46 #include <sound/initval.h>
48 #define CREATE_TRACE_POINTS
49 #include <trace/events/asoc.h>
51 #define NAME_SIZE 32
53 #ifdef CONFIG_DEBUG_FS
54 struct dentry *snd_soc_debugfs_root;
55 EXPORT_SYMBOL_GPL(snd_soc_debugfs_root);
56 #endif
58 static DEFINE_MUTEX(client_mutex);
59 static LIST_HEAD(platform_list);
60 static LIST_HEAD(codec_list);
61 static LIST_HEAD(component_list);
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.
68 static int pmdown_time = 5000;
69 module_param(pmdown_time, int, 0);
70 MODULE_PARM_DESC(pmdown_time, "DAPM stream powerdown time (msecs)");
72 struct 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;
82 /* returns the minimum number of bytes needed to represent
83 * a particular given value */
84 static int min_bytes_needed(unsigned long val)
86 int c = 0;
87 int i;
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;
100 /* fill buf which is 'len' bytes with a formatted
101 * string of the form 'reg: value\n' */
102 static int format_register_str(struct snd_soc_codec *codec,
103 unsigned int reg, char *buf, size_t len)
105 int wordsize = min_bytes_needed(codec->driver->reg_cache_size) * 2;
106 int regsize = codec->driver->reg_word_size * 2;
107 int ret;
108 char tmpbuf[len + 1];
109 char regbuf[regsize + 1];
111 /* since tmpbuf is allocated on the stack, warn the callers if they
112 * try to abuse this function */
113 WARN_ON(len > 63);
115 /* +2 for ': ' and + 1 for '\n' */
116 if (wordsize + regsize + 2 + 1 != len)
117 return -EINVAL;
119 ret = snd_soc_read(codec, reg);
120 if (ret < 0) {
121 memset(regbuf, 'X', regsize);
122 regbuf[regsize] = '\0';
123 } else {
124 snprintf(regbuf, regsize + 1, "%.*x", regsize, ret);
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);
132 return 0;
135 /* codec register dump */
136 static ssize_t soc_codec_reg_show(struct snd_soc_codec *codec, char *buf,
137 size_t count, loff_t pos)
139 int i, step = 1;
140 int wordsize, regsize;
141 int len;
142 size_t total = 0;
143 loff_t p = 0;
145 wordsize = min_bytes_needed(codec->driver->reg_cache_size) * 2;
146 regsize = codec->driver->reg_word_size * 2;
148 len = wordsize + regsize + 2 + 1;
150 if (!codec->driver->reg_cache_size)
151 return 0;
153 if (codec->driver->reg_cache_step)
154 step = codec->driver->reg_cache_step;
156 for (i = 0; i < codec->driver->reg_cache_size; i += step) {
157 /* only support larger than PAGE_SIZE bytes debugfs
158 * entries for the default case */
159 if (p >= pos) {
160 if (total + len >= count - 1)
161 break;
162 format_register_str(codec, i, buf + total, len);
163 total += len;
165 p += len;
168 total = min(total, count - 1);
170 return total;
173 static ssize_t codec_reg_show(struct device *dev,
174 struct device_attribute *attr, char *buf)
176 struct snd_soc_pcm_runtime *rtd = dev_get_drvdata(dev);
178 return soc_codec_reg_show(rtd->codec, buf, PAGE_SIZE, 0);
181 static DEVICE_ATTR(codec_reg, 0444, codec_reg_show, NULL);
183 static ssize_t pmdown_time_show(struct device *dev,
184 struct device_attribute *attr, char *buf)
186 struct snd_soc_pcm_runtime *rtd = dev_get_drvdata(dev);
188 return sprintf(buf, "%ld\n", rtd->pmdown_time);
191 static ssize_t pmdown_time_set(struct device *dev,
192 struct device_attribute *attr,
193 const char *buf, size_t count)
195 struct snd_soc_pcm_runtime *rtd = dev_get_drvdata(dev);
196 int ret;
198 ret = kstrtol(buf, 10, &rtd->pmdown_time);
199 if (ret)
200 return ret;
202 return count;
205 static DEVICE_ATTR(pmdown_time, 0644, pmdown_time_show, pmdown_time_set);
207 #ifdef CONFIG_DEBUG_FS
208 static ssize_t codec_reg_read_file(struct file *file, char __user *user_buf,
209 size_t count, loff_t *ppos)
211 ssize_t ret;
212 struct snd_soc_codec *codec = file->private_data;
213 char *buf;
215 if (*ppos < 0 || !count)
216 return -EINVAL;
218 buf = kmalloc(count, GFP_KERNEL);
219 if (!buf)
220 return -ENOMEM;
222 ret = soc_codec_reg_show(codec, buf, count, *ppos);
223 if (ret >= 0) {
224 if (copy_to_user(user_buf, buf, ret)) {
225 kfree(buf);
226 return -EFAULT;
228 *ppos += ret;
231 kfree(buf);
232 return ret;
235 static ssize_t codec_reg_write_file(struct file *file,
236 const char __user *user_buf, size_t count, loff_t *ppos)
238 char buf[32];
239 size_t buf_size;
240 char *start = buf;
241 unsigned long reg, value;
242 struct snd_soc_codec *codec = file->private_data;
243 int ret;
245 buf_size = min(count, (sizeof(buf)-1));
246 if (copy_from_user(buf, user_buf, buf_size))
247 return -EFAULT;
248 buf[buf_size] = 0;
250 while (*start == ' ')
251 start++;
252 reg = simple_strtoul(start, &start, 16);
253 while (*start == ' ')
254 start++;
255 ret = kstrtoul(start, 16, &value);
256 if (ret)
257 return ret;
259 /* Userspace has been fiddling around behind the kernel's back */
260 add_taint(TAINT_USER, LOCKDEP_NOW_UNRELIABLE);
262 snd_soc_write(codec, reg, value);
263 return buf_size;
266 static const struct file_operations codec_reg_fops = {
267 .open = simple_open,
268 .read = codec_reg_read_file,
269 .write = codec_reg_write_file,
270 .llseek = default_llseek,
273 static struct dentry *soc_debugfs_create_dir(struct dentry *parent,
274 const char *fmt, ...)
276 struct dentry *de;
277 va_list ap;
278 char *s;
280 va_start(ap, fmt);
281 s = kvasprintf(GFP_KERNEL, fmt, ap);
282 va_end(ap);
284 if (!s)
285 return NULL;
287 de = debugfs_create_dir(s, parent);
288 kfree(s);
290 return de;
293 static void soc_init_codec_debugfs(struct snd_soc_codec *codec)
295 struct dentry *debugfs_card_root = codec->component.card->debugfs_card_root;
297 codec->debugfs_codec_root = soc_debugfs_create_dir(debugfs_card_root,
298 "codec:%s",
299 codec->component.name);
300 if (!codec->debugfs_codec_root) {
301 dev_warn(codec->dev,
302 "ASoC: Failed to create codec debugfs directory\n");
303 return;
306 debugfs_create_bool("cache_sync", 0444, codec->debugfs_codec_root,
307 &codec->cache_sync);
308 debugfs_create_bool("cache_only", 0444, codec->debugfs_codec_root,
309 &codec->cache_only);
311 codec->debugfs_reg = debugfs_create_file("codec_reg", 0644,
312 codec->debugfs_codec_root,
313 codec, &codec_reg_fops);
314 if (!codec->debugfs_reg)
315 dev_warn(codec->dev,
316 "ASoC: Failed to create codec register debugfs file\n");
318 snd_soc_dapm_debugfs_init(&codec->dapm, codec->debugfs_codec_root);
321 static void soc_cleanup_codec_debugfs(struct snd_soc_codec *codec)
323 debugfs_remove_recursive(codec->debugfs_codec_root);
326 static void soc_init_platform_debugfs(struct snd_soc_platform *platform)
328 struct dentry *debugfs_card_root = platform->component.card->debugfs_card_root;
330 platform->debugfs_platform_root = soc_debugfs_create_dir(debugfs_card_root,
331 "platform:%s",
332 platform->component.name);
333 if (!platform->debugfs_platform_root) {
334 dev_warn(platform->dev,
335 "ASoC: Failed to create platform debugfs directory\n");
336 return;
339 snd_soc_dapm_debugfs_init(&platform->component.dapm,
340 platform->debugfs_platform_root);
343 static void soc_cleanup_platform_debugfs(struct snd_soc_platform *platform)
345 debugfs_remove_recursive(platform->debugfs_platform_root);
348 static ssize_t codec_list_read_file(struct file *file, char __user *user_buf,
349 size_t count, loff_t *ppos)
351 char *buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
352 ssize_t len, ret = 0;
353 struct snd_soc_codec *codec;
355 if (!buf)
356 return -ENOMEM;
358 list_for_each_entry(codec, &codec_list, list) {
359 len = snprintf(buf + ret, PAGE_SIZE - ret, "%s\n",
360 codec->component.name);
361 if (len >= 0)
362 ret += len;
363 if (ret > PAGE_SIZE) {
364 ret = PAGE_SIZE;
365 break;
369 if (ret >= 0)
370 ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
372 kfree(buf);
374 return ret;
377 static const struct file_operations codec_list_fops = {
378 .read = codec_list_read_file,
379 .llseek = default_llseek,/* read accesses f_pos */
382 static ssize_t dai_list_read_file(struct file *file, char __user *user_buf,
383 size_t count, loff_t *ppos)
385 char *buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
386 ssize_t len, ret = 0;
387 struct snd_soc_component *component;
388 struct snd_soc_dai *dai;
390 if (!buf)
391 return -ENOMEM;
393 list_for_each_entry(component, &component_list, list) {
394 list_for_each_entry(dai, &component->dai_list, list) {
395 len = snprintf(buf + ret, PAGE_SIZE - ret, "%s\n",
396 dai->name);
397 if (len >= 0)
398 ret += len;
399 if (ret > PAGE_SIZE) {
400 ret = PAGE_SIZE;
401 break;
406 ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
408 kfree(buf);
410 return ret;
413 static const struct file_operations dai_list_fops = {
414 .read = dai_list_read_file,
415 .llseek = default_llseek,/* read accesses f_pos */
418 static ssize_t platform_list_read_file(struct file *file,
419 char __user *user_buf,
420 size_t count, loff_t *ppos)
422 char *buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
423 ssize_t len, ret = 0;
424 struct snd_soc_platform *platform;
426 if (!buf)
427 return -ENOMEM;
429 list_for_each_entry(platform, &platform_list, list) {
430 len = snprintf(buf + ret, PAGE_SIZE - ret, "%s\n",
431 platform->component.name);
432 if (len >= 0)
433 ret += len;
434 if (ret > PAGE_SIZE) {
435 ret = PAGE_SIZE;
436 break;
440 ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
442 kfree(buf);
444 return ret;
447 static const struct file_operations platform_list_fops = {
448 .read = platform_list_read_file,
449 .llseek = default_llseek,/* read accesses f_pos */
452 static void soc_init_card_debugfs(struct snd_soc_card *card)
454 card->debugfs_card_root = debugfs_create_dir(card->name,
455 snd_soc_debugfs_root);
456 if (!card->debugfs_card_root) {
457 dev_warn(card->dev,
458 "ASoC: Failed to create card debugfs directory\n");
459 return;
462 card->debugfs_pop_time = debugfs_create_u32("dapm_pop_time", 0644,
463 card->debugfs_card_root,
464 &card->pop_time);
465 if (!card->debugfs_pop_time)
466 dev_warn(card->dev,
467 "ASoC: Failed to create pop time debugfs file\n");
470 static void soc_cleanup_card_debugfs(struct snd_soc_card *card)
472 debugfs_remove_recursive(card->debugfs_card_root);
475 #else
477 static inline void soc_init_codec_debugfs(struct snd_soc_codec *codec)
481 static inline void soc_cleanup_codec_debugfs(struct snd_soc_codec *codec)
485 static inline void soc_init_platform_debugfs(struct snd_soc_platform *platform)
489 static inline void soc_cleanup_platform_debugfs(struct snd_soc_platform *platform)
493 static inline void soc_init_card_debugfs(struct snd_soc_card *card)
497 static inline void soc_cleanup_card_debugfs(struct snd_soc_card *card)
500 #endif
502 struct snd_pcm_substream *snd_soc_get_dai_substream(struct snd_soc_card *card,
503 const char *dai_link, int stream)
505 int i;
507 for (i = 0; i < card->num_links; i++) {
508 if (card->rtd[i].dai_link->no_pcm &&
509 !strcmp(card->rtd[i].dai_link->name, dai_link))
510 return card->rtd[i].pcm->streams[stream].substream;
512 dev_dbg(card->dev, "ASoC: failed to find dai link %s\n", dai_link);
513 return NULL;
515 EXPORT_SYMBOL_GPL(snd_soc_get_dai_substream);
517 struct snd_soc_pcm_runtime *snd_soc_get_pcm_runtime(struct snd_soc_card *card,
518 const char *dai_link)
520 int i;
522 for (i = 0; i < card->num_links; i++) {
523 if (!strcmp(card->rtd[i].dai_link->name, dai_link))
524 return &card->rtd[i];
526 dev_dbg(card->dev, "ASoC: failed to find rtd %s\n", dai_link);
527 return NULL;
529 EXPORT_SYMBOL_GPL(snd_soc_get_pcm_runtime);
531 #ifdef CONFIG_SND_SOC_AC97_BUS
532 /* unregister ac97 codec */
533 static int soc_ac97_dev_unregister(struct snd_soc_codec *codec)
535 if (codec->ac97->dev.bus)
536 device_unregister(&codec->ac97->dev);
537 return 0;
540 /* stop no dev release warning */
541 static void soc_ac97_device_release(struct device *dev){}
543 /* register ac97 codec to bus */
544 static int soc_ac97_dev_register(struct snd_soc_codec *codec)
546 int err;
548 codec->ac97->dev.bus = &ac97_bus_type;
549 codec->ac97->dev.parent = codec->component.card->dev;
550 codec->ac97->dev.release = soc_ac97_device_release;
552 dev_set_name(&codec->ac97->dev, "%d-%d:%s",
553 codec->component.card->snd_card->number, 0,
554 codec->component.name);
555 err = device_register(&codec->ac97->dev);
556 if (err < 0) {
557 dev_err(codec->dev, "ASoC: Can't register ac97 bus\n");
558 codec->ac97->dev.bus = NULL;
559 return err;
561 return 0;
563 #endif
565 static void codec2codec_close_delayed_work(struct work_struct *work)
567 /* Currently nothing to do for c2c links
568 * Since c2c links are internal nodes in the DAPM graph and
569 * don't interface with the outside world or application layer
570 * we don't have to do any special handling on close.
574 #ifdef CONFIG_PM_SLEEP
575 /* powers down audio subsystem for suspend */
576 int snd_soc_suspend(struct device *dev)
578 struct snd_soc_card *card = dev_get_drvdata(dev);
579 struct snd_soc_codec *codec;
580 int i, j;
582 /* If the initialization of this soc device failed, there is no codec
583 * associated with it. Just bail out in this case.
585 if (list_empty(&card->codec_dev_list))
586 return 0;
588 /* Due to the resume being scheduled into a workqueue we could
589 * suspend before that's finished - wait for it to complete.
591 snd_power_lock(card->snd_card);
592 snd_power_wait(card->snd_card, SNDRV_CTL_POWER_D0);
593 snd_power_unlock(card->snd_card);
595 /* we're going to block userspace touching us until resume completes */
596 snd_power_change_state(card->snd_card, SNDRV_CTL_POWER_D3hot);
598 /* mute any active DACs */
599 for (i = 0; i < card->num_rtd; i++) {
601 if (card->rtd[i].dai_link->ignore_suspend)
602 continue;
604 for (j = 0; j < card->rtd[i].num_codecs; j++) {
605 struct snd_soc_dai *dai = card->rtd[i].codec_dais[j];
606 struct snd_soc_dai_driver *drv = dai->driver;
608 if (drv->ops->digital_mute && dai->playback_active)
609 drv->ops->digital_mute(dai, 1);
613 /* suspend all pcms */
614 for (i = 0; i < card->num_rtd; i++) {
615 if (card->rtd[i].dai_link->ignore_suspend)
616 continue;
618 snd_pcm_suspend_all(card->rtd[i].pcm);
621 if (card->suspend_pre)
622 card->suspend_pre(card);
624 for (i = 0; i < card->num_rtd; i++) {
625 struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
626 struct snd_soc_platform *platform = card->rtd[i].platform;
628 if (card->rtd[i].dai_link->ignore_suspend)
629 continue;
631 if (cpu_dai->driver->suspend && !cpu_dai->driver->ac97_control)
632 cpu_dai->driver->suspend(cpu_dai);
633 if (platform->driver->suspend && !platform->suspended) {
634 platform->driver->suspend(cpu_dai);
635 platform->suspended = 1;
639 /* close any waiting streams and save state */
640 for (i = 0; i < card->num_rtd; i++) {
641 struct snd_soc_dai **codec_dais = card->rtd[i].codec_dais;
642 flush_delayed_work(&card->rtd[i].delayed_work);
643 for (j = 0; j < card->rtd[i].num_codecs; j++) {
644 codec_dais[j]->codec->dapm.suspend_bias_level =
645 codec_dais[j]->codec->dapm.bias_level;
649 for (i = 0; i < card->num_rtd; i++) {
651 if (card->rtd[i].dai_link->ignore_suspend)
652 continue;
654 snd_soc_dapm_stream_event(&card->rtd[i],
655 SNDRV_PCM_STREAM_PLAYBACK,
656 SND_SOC_DAPM_STREAM_SUSPEND);
658 snd_soc_dapm_stream_event(&card->rtd[i],
659 SNDRV_PCM_STREAM_CAPTURE,
660 SND_SOC_DAPM_STREAM_SUSPEND);
663 /* Recheck all analogue paths too */
664 dapm_mark_io_dirty(&card->dapm);
665 snd_soc_dapm_sync(&card->dapm);
667 /* suspend all CODECs */
668 list_for_each_entry(codec, &card->codec_dev_list, card_list) {
669 /* If there are paths active then the CODEC will be held with
670 * bias _ON and should not be suspended. */
671 if (!codec->suspended && codec->driver->suspend) {
672 switch (codec->dapm.bias_level) {
673 case SND_SOC_BIAS_STANDBY:
675 * If the CODEC is capable of idle
676 * bias off then being in STANDBY
677 * means it's doing something,
678 * otherwise fall through.
680 if (codec->dapm.idle_bias_off) {
681 dev_dbg(codec->dev,
682 "ASoC: idle_bias_off CODEC on over suspend\n");
683 break;
685 case SND_SOC_BIAS_OFF:
686 codec->driver->suspend(codec);
687 codec->suspended = 1;
688 codec->cache_sync = 1;
689 if (codec->component.regmap)
690 regcache_mark_dirty(codec->component.regmap);
691 /* deactivate pins to sleep state */
692 pinctrl_pm_select_sleep_state(codec->dev);
693 break;
694 default:
695 dev_dbg(codec->dev,
696 "ASoC: CODEC is on over suspend\n");
697 break;
702 for (i = 0; i < card->num_rtd; i++) {
703 struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
705 if (card->rtd[i].dai_link->ignore_suspend)
706 continue;
708 if (cpu_dai->driver->suspend && cpu_dai->driver->ac97_control)
709 cpu_dai->driver->suspend(cpu_dai);
711 /* deactivate pins to sleep state */
712 pinctrl_pm_select_sleep_state(cpu_dai->dev);
715 if (card->suspend_post)
716 card->suspend_post(card);
718 return 0;
720 EXPORT_SYMBOL_GPL(snd_soc_suspend);
722 /* deferred resume work, so resume can complete before we finished
723 * setting our codec back up, which can be very slow on I2C
725 static void soc_resume_deferred(struct work_struct *work)
727 struct snd_soc_card *card =
728 container_of(work, struct snd_soc_card, deferred_resume_work);
729 struct snd_soc_codec *codec;
730 int i, j;
732 /* our power state is still SNDRV_CTL_POWER_D3hot from suspend time,
733 * so userspace apps are blocked from touching us
736 dev_dbg(card->dev, "ASoC: starting resume work\n");
738 /* Bring us up into D2 so that DAPM starts enabling things */
739 snd_power_change_state(card->snd_card, SNDRV_CTL_POWER_D2);
741 if (card->resume_pre)
742 card->resume_pre(card);
744 /* resume AC97 DAIs */
745 for (i = 0; i < card->num_rtd; i++) {
746 struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
748 if (card->rtd[i].dai_link->ignore_suspend)
749 continue;
751 if (cpu_dai->driver->resume && cpu_dai->driver->ac97_control)
752 cpu_dai->driver->resume(cpu_dai);
755 list_for_each_entry(codec, &card->codec_dev_list, card_list) {
756 /* If the CODEC was idle over suspend then it will have been
757 * left with bias OFF or STANDBY and suspended so we must now
758 * resume. Otherwise the suspend was suppressed.
760 if (codec->driver->resume && codec->suspended) {
761 switch (codec->dapm.bias_level) {
762 case SND_SOC_BIAS_STANDBY:
763 case SND_SOC_BIAS_OFF:
764 codec->driver->resume(codec);
765 codec->suspended = 0;
766 break;
767 default:
768 dev_dbg(codec->dev,
769 "ASoC: CODEC was on over suspend\n");
770 break;
775 for (i = 0; i < card->num_rtd; i++) {
777 if (card->rtd[i].dai_link->ignore_suspend)
778 continue;
780 snd_soc_dapm_stream_event(&card->rtd[i],
781 SNDRV_PCM_STREAM_PLAYBACK,
782 SND_SOC_DAPM_STREAM_RESUME);
784 snd_soc_dapm_stream_event(&card->rtd[i],
785 SNDRV_PCM_STREAM_CAPTURE,
786 SND_SOC_DAPM_STREAM_RESUME);
789 /* unmute any active DACs */
790 for (i = 0; i < card->num_rtd; i++) {
792 if (card->rtd[i].dai_link->ignore_suspend)
793 continue;
795 for (j = 0; j < card->rtd[i].num_codecs; j++) {
796 struct snd_soc_dai *dai = card->rtd[i].codec_dais[j];
797 struct snd_soc_dai_driver *drv = dai->driver;
799 if (drv->ops->digital_mute && dai->playback_active)
800 drv->ops->digital_mute(dai, 0);
804 for (i = 0; i < card->num_rtd; i++) {
805 struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
806 struct snd_soc_platform *platform = card->rtd[i].platform;
808 if (card->rtd[i].dai_link->ignore_suspend)
809 continue;
811 if (cpu_dai->driver->resume && !cpu_dai->driver->ac97_control)
812 cpu_dai->driver->resume(cpu_dai);
813 if (platform->driver->resume && platform->suspended) {
814 platform->driver->resume(cpu_dai);
815 platform->suspended = 0;
819 if (card->resume_post)
820 card->resume_post(card);
822 dev_dbg(card->dev, "ASoC: resume work completed\n");
824 /* userspace can access us now we are back as we were before */
825 snd_power_change_state(card->snd_card, SNDRV_CTL_POWER_D0);
827 /* Recheck all analogue paths too */
828 dapm_mark_io_dirty(&card->dapm);
829 snd_soc_dapm_sync(&card->dapm);
832 /* powers up audio subsystem after a suspend */
833 int snd_soc_resume(struct device *dev)
835 struct snd_soc_card *card = dev_get_drvdata(dev);
836 int i, ac97_control = 0;
838 /* If the initialization of this soc device failed, there is no codec
839 * associated with it. Just bail out in this case.
841 if (list_empty(&card->codec_dev_list))
842 return 0;
844 /* activate pins from sleep state */
845 for (i = 0; i < card->num_rtd; i++) {
846 struct snd_soc_pcm_runtime *rtd = &card->rtd[i];
847 struct snd_soc_dai **codec_dais = rtd->codec_dais;
848 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
849 int j;
851 if (cpu_dai->active)
852 pinctrl_pm_select_default_state(cpu_dai->dev);
854 for (j = 0; j < rtd->num_codecs; j++) {
855 struct snd_soc_dai *codec_dai = codec_dais[j];
856 if (codec_dai->active)
857 pinctrl_pm_select_default_state(codec_dai->dev);
861 /* AC97 devices might have other drivers hanging off them so
862 * need to resume immediately. Other drivers don't have that
863 * problem and may take a substantial amount of time to resume
864 * due to I/O costs and anti-pop so handle them out of line.
866 for (i = 0; i < card->num_rtd; i++) {
867 struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
868 ac97_control |= cpu_dai->driver->ac97_control;
870 if (ac97_control) {
871 dev_dbg(dev, "ASoC: Resuming AC97 immediately\n");
872 soc_resume_deferred(&card->deferred_resume_work);
873 } else {
874 dev_dbg(dev, "ASoC: Scheduling resume work\n");
875 if (!schedule_work(&card->deferred_resume_work))
876 dev_err(dev, "ASoC: resume work item may be lost\n");
879 return 0;
881 EXPORT_SYMBOL_GPL(snd_soc_resume);
882 #else
883 #define snd_soc_suspend NULL
884 #define snd_soc_resume NULL
885 #endif
887 static const struct snd_soc_dai_ops null_dai_ops = {
890 static struct snd_soc_codec *soc_find_codec(
891 const struct device_node *codec_of_node,
892 const char *codec_name)
894 struct snd_soc_codec *codec;
896 list_for_each_entry(codec, &codec_list, list) {
897 if (codec_of_node) {
898 if (codec->dev->of_node != codec_of_node)
899 continue;
900 } else {
901 if (strcmp(codec->component.name, codec_name))
902 continue;
905 return codec;
908 return NULL;
911 static struct snd_soc_dai *soc_find_codec_dai(struct snd_soc_codec *codec,
912 const char *codec_dai_name)
914 struct snd_soc_dai *codec_dai;
916 list_for_each_entry(codec_dai, &codec->component.dai_list, list) {
917 if (!strcmp(codec_dai->name, codec_dai_name)) {
918 return codec_dai;
922 return NULL;
925 static int soc_bind_dai_link(struct snd_soc_card *card, int num)
927 struct snd_soc_dai_link *dai_link = &card->dai_link[num];
928 struct snd_soc_pcm_runtime *rtd = &card->rtd[num];
929 struct snd_soc_component *component;
930 struct snd_soc_dai_link_component *codecs = dai_link->codecs;
931 struct snd_soc_dai **codec_dais = rtd->codec_dais;
932 struct snd_soc_platform *platform;
933 struct snd_soc_dai *cpu_dai;
934 const char *platform_name;
935 int i;
937 dev_dbg(card->dev, "ASoC: binding %s at idx %d\n", dai_link->name, num);
939 /* Find CPU DAI from registered DAIs*/
940 list_for_each_entry(component, &component_list, list) {
941 if (dai_link->cpu_of_node &&
942 component->dev->of_node != dai_link->cpu_of_node)
943 continue;
944 if (dai_link->cpu_name &&
945 strcmp(dev_name(component->dev), dai_link->cpu_name))
946 continue;
947 list_for_each_entry(cpu_dai, &component->dai_list, list) {
948 if (dai_link->cpu_dai_name &&
949 strcmp(cpu_dai->name, dai_link->cpu_dai_name))
950 continue;
952 rtd->cpu_dai = cpu_dai;
956 if (!rtd->cpu_dai) {
957 dev_err(card->dev, "ASoC: CPU DAI %s not registered\n",
958 dai_link->cpu_dai_name);
959 return -EPROBE_DEFER;
962 rtd->num_codecs = dai_link->num_codecs;
964 /* Find CODEC from registered CODECs */
965 for (i = 0; i < rtd->num_codecs; i++) {
966 struct snd_soc_codec *codec;
967 codec = soc_find_codec(codecs[i].of_node, codecs[i].name);
968 if (!codec) {
969 dev_err(card->dev, "ASoC: CODEC %s not registered\n",
970 codecs[i].name);
971 return -EPROBE_DEFER;
974 codec_dais[i] = soc_find_codec_dai(codec, codecs[i].dai_name);
975 if (!codec_dais[i]) {
976 dev_err(card->dev, "ASoC: CODEC DAI %s not registered\n",
977 codecs[i].dai_name);
978 return -EPROBE_DEFER;
982 /* Single codec links expect codec and codec_dai in runtime data */
983 rtd->codec_dai = codec_dais[0];
984 rtd->codec = rtd->codec_dai->codec;
986 /* if there's no platform we match on the empty platform */
987 platform_name = dai_link->platform_name;
988 if (!platform_name && !dai_link->platform_of_node)
989 platform_name = "snd-soc-dummy";
991 /* find one from the set of registered platforms */
992 list_for_each_entry(platform, &platform_list, list) {
993 if (dai_link->platform_of_node) {
994 if (platform->dev->of_node !=
995 dai_link->platform_of_node)
996 continue;
997 } else {
998 if (strcmp(platform->component.name, platform_name))
999 continue;
1002 rtd->platform = platform;
1004 if (!rtd->platform) {
1005 dev_err(card->dev, "ASoC: platform %s not registered\n",
1006 dai_link->platform_name);
1007 return -EPROBE_DEFER;
1010 card->num_rtd++;
1012 return 0;
1015 static int soc_remove_platform(struct snd_soc_platform *platform)
1017 int ret;
1019 if (platform->driver->remove) {
1020 ret = platform->driver->remove(platform);
1021 if (ret < 0)
1022 dev_err(platform->dev, "ASoC: failed to remove %d\n",
1023 ret);
1026 /* Make sure all DAPM widgets are freed */
1027 snd_soc_dapm_free(&platform->component.dapm);
1029 soc_cleanup_platform_debugfs(platform);
1030 platform->probed = 0;
1031 module_put(platform->dev->driver->owner);
1033 return 0;
1036 static void soc_remove_codec(struct snd_soc_codec *codec)
1038 int err;
1040 if (codec->driver->remove) {
1041 err = codec->driver->remove(codec);
1042 if (err < 0)
1043 dev_err(codec->dev, "ASoC: failed to remove %d\n", err);
1046 /* Make sure all DAPM widgets are freed */
1047 snd_soc_dapm_free(&codec->dapm);
1049 soc_cleanup_codec_debugfs(codec);
1050 codec->probed = 0;
1051 list_del(&codec->card_list);
1052 module_put(codec->dev->driver->owner);
1055 static void soc_remove_codec_dai(struct snd_soc_dai *codec_dai, int order)
1057 int err;
1059 if (codec_dai && codec_dai->probed &&
1060 codec_dai->driver->remove_order == order) {
1061 if (codec_dai->driver->remove) {
1062 err = codec_dai->driver->remove(codec_dai);
1063 if (err < 0)
1064 dev_err(codec_dai->dev,
1065 "ASoC: failed to remove %s: %d\n",
1066 codec_dai->name, err);
1068 codec_dai->probed = 0;
1072 static void soc_remove_link_dais(struct snd_soc_card *card, int num, int order)
1074 struct snd_soc_pcm_runtime *rtd = &card->rtd[num];
1075 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
1076 int i, err;
1078 /* unregister the rtd device */
1079 if (rtd->dev_registered) {
1080 device_remove_file(rtd->dev, &dev_attr_pmdown_time);
1081 device_remove_file(rtd->dev, &dev_attr_codec_reg);
1082 device_unregister(rtd->dev);
1083 rtd->dev_registered = 0;
1086 /* remove the CODEC DAI */
1087 for (i = 0; i < rtd->num_codecs; i++)
1088 soc_remove_codec_dai(rtd->codec_dais[i], order);
1090 /* remove the cpu_dai */
1091 if (cpu_dai && cpu_dai->probed &&
1092 cpu_dai->driver->remove_order == order) {
1093 if (cpu_dai->driver->remove) {
1094 err = cpu_dai->driver->remove(cpu_dai);
1095 if (err < 0)
1096 dev_err(cpu_dai->dev,
1097 "ASoC: failed to remove %s: %d\n",
1098 cpu_dai->name, err);
1100 cpu_dai->probed = 0;
1101 if (!cpu_dai->codec)
1102 module_put(cpu_dai->dev->driver->owner);
1106 static void soc_remove_link_components(struct snd_soc_card *card, int num,
1107 int order)
1109 struct snd_soc_pcm_runtime *rtd = &card->rtd[num];
1110 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
1111 struct snd_soc_platform *platform = rtd->platform;
1112 struct snd_soc_codec *codec;
1113 int i;
1115 /* remove the platform */
1116 if (platform && platform->probed &&
1117 platform->driver->remove_order == order) {
1118 soc_remove_platform(platform);
1121 /* remove the CODEC-side CODEC */
1122 for (i = 0; i < rtd->num_codecs; i++) {
1123 codec = rtd->codec_dais[i]->codec;
1124 if (codec && codec->probed &&
1125 codec->driver->remove_order == order)
1126 soc_remove_codec(codec);
1129 /* remove any CPU-side CODEC */
1130 if (cpu_dai) {
1131 codec = cpu_dai->codec;
1132 if (codec && codec->probed &&
1133 codec->driver->remove_order == order)
1134 soc_remove_codec(codec);
1138 static void soc_remove_dai_links(struct snd_soc_card *card)
1140 int dai, order;
1142 for (order = SND_SOC_COMP_ORDER_FIRST; order <= SND_SOC_COMP_ORDER_LAST;
1143 order++) {
1144 for (dai = 0; dai < card->num_rtd; dai++)
1145 soc_remove_link_dais(card, dai, order);
1148 for (order = SND_SOC_COMP_ORDER_FIRST; order <= SND_SOC_COMP_ORDER_LAST;
1149 order++) {
1150 for (dai = 0; dai < card->num_rtd; dai++)
1151 soc_remove_link_components(card, dai, order);
1154 card->num_rtd = 0;
1157 static void soc_set_name_prefix(struct snd_soc_card *card,
1158 struct snd_soc_component *component)
1160 int i;
1162 if (card->codec_conf == NULL)
1163 return;
1165 for (i = 0; i < card->num_configs; i++) {
1166 struct snd_soc_codec_conf *map = &card->codec_conf[i];
1167 if (map->of_node && component->dev->of_node != map->of_node)
1168 continue;
1169 if (map->dev_name && strcmp(component->name, map->dev_name))
1170 continue;
1171 component->name_prefix = map->name_prefix;
1172 break;
1176 static int soc_probe_codec(struct snd_soc_card *card,
1177 struct snd_soc_codec *codec)
1179 int ret = 0;
1180 const struct snd_soc_codec_driver *driver = codec->driver;
1181 struct snd_soc_dai *dai;
1183 codec->component.card = card;
1184 codec->dapm.card = card;
1185 soc_set_name_prefix(card, &codec->component);
1187 if (!try_module_get(codec->dev->driver->owner))
1188 return -ENODEV;
1190 soc_init_codec_debugfs(codec);
1192 if (driver->dapm_widgets) {
1193 ret = snd_soc_dapm_new_controls(&codec->dapm,
1194 driver->dapm_widgets,
1195 driver->num_dapm_widgets);
1197 if (ret != 0) {
1198 dev_err(codec->dev,
1199 "Failed to create new controls %d\n", ret);
1200 goto err_probe;
1204 /* Create DAPM widgets for each DAI stream */
1205 list_for_each_entry(dai, &codec->component.dai_list, list) {
1206 ret = snd_soc_dapm_new_dai_widgets(&codec->dapm, dai);
1208 if (ret != 0) {
1209 dev_err(codec->dev,
1210 "Failed to create DAI widgets %d\n", ret);
1211 goto err_probe;
1215 codec->dapm.idle_bias_off = driver->idle_bias_off;
1217 if (driver->probe) {
1218 ret = driver->probe(codec);
1219 if (ret < 0) {
1220 dev_err(codec->dev,
1221 "ASoC: failed to probe CODEC %d\n", ret);
1222 goto err_probe;
1224 WARN(codec->dapm.idle_bias_off &&
1225 codec->dapm.bias_level != SND_SOC_BIAS_OFF,
1226 "codec %s can not start from non-off bias with idle_bias_off==1\n",
1227 codec->component.name);
1230 if (driver->controls)
1231 snd_soc_add_codec_controls(codec, driver->controls,
1232 driver->num_controls);
1233 if (driver->dapm_routes)
1234 snd_soc_dapm_add_routes(&codec->dapm, driver->dapm_routes,
1235 driver->num_dapm_routes);
1237 /* mark codec as probed and add to card codec list */
1238 codec->probed = 1;
1239 list_add(&codec->card_list, &card->codec_dev_list);
1240 list_add(&codec->dapm.list, &card->dapm_list);
1242 return 0;
1244 err_probe:
1245 soc_cleanup_codec_debugfs(codec);
1246 module_put(codec->dev->driver->owner);
1248 return ret;
1251 static int soc_probe_platform(struct snd_soc_card *card,
1252 struct snd_soc_platform *platform)
1254 int ret = 0;
1255 const struct snd_soc_platform_driver *driver = platform->driver;
1256 struct snd_soc_component *component;
1257 struct snd_soc_dai *dai;
1259 platform->component.card = card;
1260 platform->component.dapm.card = card;
1262 if (!try_module_get(platform->dev->driver->owner))
1263 return -ENODEV;
1265 soc_init_platform_debugfs(platform);
1267 if (driver->dapm_widgets)
1268 snd_soc_dapm_new_controls(&platform->component.dapm,
1269 driver->dapm_widgets, driver->num_dapm_widgets);
1271 /* Create DAPM widgets for each DAI stream */
1272 list_for_each_entry(component, &component_list, list) {
1273 if (component->dev != platform->dev)
1274 continue;
1275 list_for_each_entry(dai, &component->dai_list, list)
1276 snd_soc_dapm_new_dai_widgets(&platform->component.dapm,
1277 dai);
1280 platform->component.dapm.idle_bias_off = 1;
1282 if (driver->probe) {
1283 ret = driver->probe(platform);
1284 if (ret < 0) {
1285 dev_err(platform->dev,
1286 "ASoC: failed to probe platform %d\n", ret);
1287 goto err_probe;
1291 if (driver->controls)
1292 snd_soc_add_platform_controls(platform, driver->controls,
1293 driver->num_controls);
1294 if (driver->dapm_routes)
1295 snd_soc_dapm_add_routes(&platform->component.dapm,
1296 driver->dapm_routes, driver->num_dapm_routes);
1298 /* mark platform as probed and add to card platform list */
1299 platform->probed = 1;
1300 list_add(&platform->component.dapm.list, &card->dapm_list);
1302 return 0;
1304 err_probe:
1305 soc_cleanup_platform_debugfs(platform);
1306 module_put(platform->dev->driver->owner);
1308 return ret;
1311 static void rtd_release(struct device *dev)
1313 kfree(dev);
1316 static int soc_post_component_init(struct snd_soc_pcm_runtime *rtd,
1317 const char *name)
1319 int ret = 0;
1321 /* register the rtd device */
1322 rtd->dev = kzalloc(sizeof(struct device), GFP_KERNEL);
1323 if (!rtd->dev)
1324 return -ENOMEM;
1325 device_initialize(rtd->dev);
1326 rtd->dev->parent = rtd->card->dev;
1327 rtd->dev->release = rtd_release;
1328 dev_set_name(rtd->dev, "%s", name);
1329 dev_set_drvdata(rtd->dev, rtd);
1330 mutex_init(&rtd->pcm_mutex);
1331 INIT_LIST_HEAD(&rtd->dpcm[SNDRV_PCM_STREAM_PLAYBACK].be_clients);
1332 INIT_LIST_HEAD(&rtd->dpcm[SNDRV_PCM_STREAM_CAPTURE].be_clients);
1333 INIT_LIST_HEAD(&rtd->dpcm[SNDRV_PCM_STREAM_PLAYBACK].fe_clients);
1334 INIT_LIST_HEAD(&rtd->dpcm[SNDRV_PCM_STREAM_CAPTURE].fe_clients);
1335 ret = device_add(rtd->dev);
1336 if (ret < 0) {
1337 /* calling put_device() here to free the rtd->dev */
1338 put_device(rtd->dev);
1339 dev_err(rtd->card->dev,
1340 "ASoC: failed to register runtime device: %d\n", ret);
1341 return ret;
1343 rtd->dev_registered = 1;
1345 /* add DAPM sysfs entries for this codec */
1346 ret = snd_soc_dapm_sys_add(rtd->dev);
1347 if (ret < 0)
1348 dev_err(rtd->dev,
1349 "ASoC: failed to add codec dapm sysfs entries: %d\n", ret);
1351 /* add codec sysfs entries */
1352 ret = device_create_file(rtd->dev, &dev_attr_codec_reg);
1353 if (ret < 0)
1354 dev_err(rtd->dev,
1355 "ASoC: failed to add codec sysfs files: %d\n", ret);
1357 return 0;
1360 static int soc_probe_link_components(struct snd_soc_card *card, int num,
1361 int order)
1363 struct snd_soc_pcm_runtime *rtd = &card->rtd[num];
1364 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
1365 struct snd_soc_platform *platform = rtd->platform;
1366 int i, ret;
1368 /* probe the CPU-side component, if it is a CODEC */
1369 if (cpu_dai->codec &&
1370 !cpu_dai->codec->probed &&
1371 cpu_dai->codec->driver->probe_order == order) {
1372 ret = soc_probe_codec(card, cpu_dai->codec);
1373 if (ret < 0)
1374 return ret;
1377 /* probe the CODEC-side components */
1378 for (i = 0; i < rtd->num_codecs; i++) {
1379 if (!rtd->codec_dais[i]->codec->probed &&
1380 rtd->codec_dais[i]->codec->driver->probe_order == order) {
1381 ret = soc_probe_codec(card, rtd->codec_dais[i]->codec);
1382 if (ret < 0)
1383 return ret;
1387 /* probe the platform */
1388 if (!platform->probed &&
1389 platform->driver->probe_order == order) {
1390 ret = soc_probe_platform(card, platform);
1391 if (ret < 0)
1392 return ret;
1395 return 0;
1398 static int soc_probe_codec_dai(struct snd_soc_card *card,
1399 struct snd_soc_dai *codec_dai,
1400 int order)
1402 int ret;
1404 if (!codec_dai->probed && codec_dai->driver->probe_order == order) {
1405 if (codec_dai->driver->probe) {
1406 ret = codec_dai->driver->probe(codec_dai);
1407 if (ret < 0) {
1408 dev_err(codec_dai->dev,
1409 "ASoC: failed to probe CODEC DAI %s: %d\n",
1410 codec_dai->name, ret);
1411 return ret;
1415 /* mark codec_dai as probed and add to card dai list */
1416 codec_dai->probed = 1;
1419 return 0;
1422 static int soc_link_dai_widgets(struct snd_soc_card *card,
1423 struct snd_soc_dai_link *dai_link,
1424 struct snd_soc_pcm_runtime *rtd)
1426 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
1427 struct snd_soc_dai *codec_dai = rtd->codec_dai;
1428 struct snd_soc_dapm_widget *play_w, *capture_w;
1429 int ret;
1431 if (rtd->num_codecs > 1)
1432 dev_warn(card->dev, "ASoC: Multiple codecs not supported yet\n");
1434 /* link the DAI widgets */
1435 play_w = codec_dai->playback_widget;
1436 capture_w = cpu_dai->capture_widget;
1437 if (play_w && capture_w) {
1438 ret = snd_soc_dapm_new_pcm(card, dai_link->params,
1439 capture_w, play_w);
1440 if (ret != 0) {
1441 dev_err(card->dev, "ASoC: Can't link %s to %s: %d\n",
1442 play_w->name, capture_w->name, ret);
1443 return ret;
1447 play_w = cpu_dai->playback_widget;
1448 capture_w = codec_dai->capture_widget;
1449 if (play_w && capture_w) {
1450 ret = snd_soc_dapm_new_pcm(card, dai_link->params,
1451 capture_w, play_w);
1452 if (ret != 0) {
1453 dev_err(card->dev, "ASoC: Can't link %s to %s: %d\n",
1454 play_w->name, capture_w->name, ret);
1455 return ret;
1459 return 0;
1462 static int soc_probe_link_dais(struct snd_soc_card *card, int num, int order)
1464 struct snd_soc_dai_link *dai_link = &card->dai_link[num];
1465 struct snd_soc_pcm_runtime *rtd = &card->rtd[num];
1466 struct snd_soc_platform *platform = rtd->platform;
1467 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
1468 int i, ret;
1470 dev_dbg(card->dev, "ASoC: probe %s dai link %d late %d\n",
1471 card->name, num, order);
1473 /* config components */
1474 cpu_dai->platform = platform;
1475 cpu_dai->card = card;
1476 for (i = 0; i < rtd->num_codecs; i++)
1477 rtd->codec_dais[i]->card = card;
1479 /* set default power off timeout */
1480 rtd->pmdown_time = pmdown_time;
1482 /* probe the cpu_dai */
1483 if (!cpu_dai->probed &&
1484 cpu_dai->driver->probe_order == order) {
1485 if (!cpu_dai->codec) {
1486 if (!try_module_get(cpu_dai->dev->driver->owner))
1487 return -ENODEV;
1490 if (cpu_dai->driver->probe) {
1491 ret = cpu_dai->driver->probe(cpu_dai);
1492 if (ret < 0) {
1493 dev_err(cpu_dai->dev,
1494 "ASoC: failed to probe CPU DAI %s: %d\n",
1495 cpu_dai->name, ret);
1496 module_put(cpu_dai->dev->driver->owner);
1497 return ret;
1500 cpu_dai->probed = 1;
1503 /* probe the CODEC DAI */
1504 for (i = 0; i < rtd->num_codecs; i++) {
1505 ret = soc_probe_codec_dai(card, rtd->codec_dais[i], order);
1506 if (ret)
1507 return ret;
1510 /* complete DAI probe during last probe */
1511 if (order != SND_SOC_COMP_ORDER_LAST)
1512 return 0;
1514 /* do machine specific initialization */
1515 if (dai_link->init) {
1516 ret = dai_link->init(rtd);
1517 if (ret < 0) {
1518 dev_err(card->dev, "ASoC: failed to init %s: %d\n",
1519 dai_link->name, ret);
1520 return ret;
1524 ret = soc_post_component_init(rtd, dai_link->name);
1525 if (ret)
1526 return ret;
1528 #ifdef CONFIG_DEBUG_FS
1529 /* add DPCM sysfs entries */
1530 if (dai_link->dynamic) {
1531 ret = soc_dpcm_debugfs_add(rtd);
1532 if (ret < 0) {
1533 dev_err(rtd->dev,
1534 "ASoC: failed to add dpcm sysfs entries: %d\n",
1535 ret);
1536 return ret;
1539 #endif
1541 ret = device_create_file(rtd->dev, &dev_attr_pmdown_time);
1542 if (ret < 0)
1543 dev_warn(rtd->dev, "ASoC: failed to add pmdown_time sysfs: %d\n",
1544 ret);
1546 if (cpu_dai->driver->compress_dai) {
1547 /*create compress_device"*/
1548 ret = soc_new_compress(rtd, num);
1549 if (ret < 0) {
1550 dev_err(card->dev, "ASoC: can't create compress %s\n",
1551 dai_link->stream_name);
1552 return ret;
1554 } else {
1556 if (!dai_link->params) {
1557 /* create the pcm */
1558 ret = soc_new_pcm(rtd, num);
1559 if (ret < 0) {
1560 dev_err(card->dev, "ASoC: can't create pcm %s :%d\n",
1561 dai_link->stream_name, ret);
1562 return ret;
1564 } else {
1565 INIT_DELAYED_WORK(&rtd->delayed_work,
1566 codec2codec_close_delayed_work);
1568 /* link the DAI widgets */
1569 ret = soc_link_dai_widgets(card, dai_link, rtd);
1570 if (ret)
1571 return ret;
1575 /* add platform data for AC97 devices */
1576 for (i = 0; i < rtd->num_codecs; i++) {
1577 if (rtd->codec_dais[i]->driver->ac97_control)
1578 snd_ac97_dev_add_pdata(rtd->codec_dais[i]->codec->ac97,
1579 rtd->cpu_dai->ac97_pdata);
1582 return 0;
1585 #ifdef CONFIG_SND_SOC_AC97_BUS
1586 static int soc_register_ac97_codec(struct snd_soc_codec *codec,
1587 struct snd_soc_dai *codec_dai)
1589 int ret;
1591 /* Only instantiate AC97 if not already done by the adaptor
1592 * for the generic AC97 subsystem.
1594 if (codec_dai->driver->ac97_control && !codec->ac97_registered) {
1596 * It is possible that the AC97 device is already registered to
1597 * the device subsystem. This happens when the device is created
1598 * via snd_ac97_mixer(). Currently only SoC codec that does so
1599 * is the generic AC97 glue but others migh emerge.
1601 * In those cases we don't try to register the device again.
1603 if (!codec->ac97_created)
1604 return 0;
1606 ret = soc_ac97_dev_register(codec);
1607 if (ret < 0) {
1608 dev_err(codec->dev,
1609 "ASoC: AC97 device register failed: %d\n", ret);
1610 return ret;
1613 codec->ac97_registered = 1;
1615 return 0;
1618 static void soc_unregister_ac97_codec(struct snd_soc_codec *codec)
1620 if (codec->ac97_registered) {
1621 soc_ac97_dev_unregister(codec);
1622 codec->ac97_registered = 0;
1626 static int soc_register_ac97_dai_link(struct snd_soc_pcm_runtime *rtd)
1628 int i, ret;
1630 for (i = 0; i < rtd->num_codecs; i++) {
1631 struct snd_soc_dai *codec_dai = rtd->codec_dais[i];
1633 ret = soc_register_ac97_codec(codec_dai->codec, codec_dai);
1634 if (ret) {
1635 while (--i >= 0)
1636 soc_unregister_ac97_codec(codec_dai->codec);
1637 return ret;
1641 return 0;
1644 static void soc_unregister_ac97_dai_link(struct snd_soc_pcm_runtime *rtd)
1646 int i;
1648 for (i = 0; i < rtd->num_codecs; i++)
1649 soc_unregister_ac97_codec(rtd->codec_dais[i]->codec);
1651 #endif
1653 static int soc_bind_aux_dev(struct snd_soc_card *card, int num)
1655 struct snd_soc_pcm_runtime *rtd = &card->rtd_aux[num];
1656 struct snd_soc_aux_dev *aux_dev = &card->aux_dev[num];
1657 const char *codecname = aux_dev->codec_name;
1659 rtd->codec = soc_find_codec(aux_dev->codec_of_node, codecname);
1660 if (!rtd->codec) {
1661 if (aux_dev->codec_of_node)
1662 codecname = of_node_full_name(aux_dev->codec_of_node);
1664 dev_err(card->dev, "ASoC: %s not registered\n", codecname);
1665 return -EPROBE_DEFER;
1668 return 0;
1671 static int soc_probe_aux_dev(struct snd_soc_card *card, int num)
1673 struct snd_soc_pcm_runtime *rtd = &card->rtd_aux[num];
1674 struct snd_soc_aux_dev *aux_dev = &card->aux_dev[num];
1675 int ret;
1677 if (rtd->codec->probed) {
1678 dev_err(rtd->codec->dev, "ASoC: codec already probed\n");
1679 return -EBUSY;
1682 ret = soc_probe_codec(card, rtd->codec);
1683 if (ret < 0)
1684 return ret;
1686 /* do machine specific initialization */
1687 if (aux_dev->init) {
1688 ret = aux_dev->init(&rtd->codec->dapm);
1689 if (ret < 0) {
1690 dev_err(card->dev, "ASoC: failed to init %s: %d\n",
1691 aux_dev->name, ret);
1692 return ret;
1696 return soc_post_component_init(rtd, aux_dev->name);
1699 static void soc_remove_aux_dev(struct snd_soc_card *card, int num)
1701 struct snd_soc_pcm_runtime *rtd = &card->rtd_aux[num];
1702 struct snd_soc_codec *codec = rtd->codec;
1704 /* unregister the rtd device */
1705 if (rtd->dev_registered) {
1706 device_remove_file(rtd->dev, &dev_attr_codec_reg);
1707 device_unregister(rtd->dev);
1708 rtd->dev_registered = 0;
1711 if (codec && codec->probed)
1712 soc_remove_codec(codec);
1715 static int snd_soc_init_codec_cache(struct snd_soc_codec *codec)
1717 int ret;
1719 if (codec->cache_init)
1720 return 0;
1722 ret = snd_soc_cache_init(codec);
1723 if (ret < 0) {
1724 dev_err(codec->dev,
1725 "ASoC: Failed to set cache compression type: %d\n",
1726 ret);
1727 return ret;
1729 codec->cache_init = 1;
1730 return 0;
1733 static int snd_soc_instantiate_card(struct snd_soc_card *card)
1735 struct snd_soc_codec *codec;
1736 struct snd_soc_dai_link *dai_link;
1737 int ret, i, order, dai_fmt;
1739 mutex_lock_nested(&card->mutex, SND_SOC_CARD_CLASS_INIT);
1741 /* bind DAIs */
1742 for (i = 0; i < card->num_links; i++) {
1743 ret = soc_bind_dai_link(card, i);
1744 if (ret != 0)
1745 goto base_error;
1748 /* bind aux_devs too */
1749 for (i = 0; i < card->num_aux_devs; i++) {
1750 ret = soc_bind_aux_dev(card, i);
1751 if (ret != 0)
1752 goto base_error;
1755 /* initialize the register cache for each available codec */
1756 list_for_each_entry(codec, &codec_list, list) {
1757 if (codec->cache_init)
1758 continue;
1759 ret = snd_soc_init_codec_cache(codec);
1760 if (ret < 0)
1761 goto base_error;
1764 /* card bind complete so register a sound card */
1765 ret = snd_card_new(card->dev, SNDRV_DEFAULT_IDX1, SNDRV_DEFAULT_STR1,
1766 card->owner, 0, &card->snd_card);
1767 if (ret < 0) {
1768 dev_err(card->dev,
1769 "ASoC: can't create sound card for card %s: %d\n",
1770 card->name, ret);
1771 goto base_error;
1774 card->dapm.bias_level = SND_SOC_BIAS_OFF;
1775 card->dapm.dev = card->dev;
1776 card->dapm.card = card;
1777 list_add(&card->dapm.list, &card->dapm_list);
1779 #ifdef CONFIG_DEBUG_FS
1780 snd_soc_dapm_debugfs_init(&card->dapm, card->debugfs_card_root);
1781 #endif
1783 #ifdef CONFIG_PM_SLEEP
1784 /* deferred resume work */
1785 INIT_WORK(&card->deferred_resume_work, soc_resume_deferred);
1786 #endif
1788 if (card->dapm_widgets)
1789 snd_soc_dapm_new_controls(&card->dapm, card->dapm_widgets,
1790 card->num_dapm_widgets);
1792 /* initialise the sound card only once */
1793 if (card->probe) {
1794 ret = card->probe(card);
1795 if (ret < 0)
1796 goto card_probe_error;
1799 /* probe all components used by DAI links on this card */
1800 for (order = SND_SOC_COMP_ORDER_FIRST; order <= SND_SOC_COMP_ORDER_LAST;
1801 order++) {
1802 for (i = 0; i < card->num_links; i++) {
1803 ret = soc_probe_link_components(card, i, order);
1804 if (ret < 0) {
1805 dev_err(card->dev,
1806 "ASoC: failed to instantiate card %d\n",
1807 ret);
1808 goto probe_dai_err;
1813 /* probe all DAI links on this card */
1814 for (order = SND_SOC_COMP_ORDER_FIRST; order <= SND_SOC_COMP_ORDER_LAST;
1815 order++) {
1816 for (i = 0; i < card->num_links; i++) {
1817 ret = soc_probe_link_dais(card, i, order);
1818 if (ret < 0) {
1819 dev_err(card->dev,
1820 "ASoC: failed to instantiate card %d\n",
1821 ret);
1822 goto probe_dai_err;
1827 for (i = 0; i < card->num_aux_devs; i++) {
1828 ret = soc_probe_aux_dev(card, i);
1829 if (ret < 0) {
1830 dev_err(card->dev,
1831 "ASoC: failed to add auxiliary devices %d\n",
1832 ret);
1833 goto probe_aux_dev_err;
1837 snd_soc_dapm_link_dai_widgets(card);
1838 snd_soc_dapm_connect_dai_link_widgets(card);
1840 if (card->controls)
1841 snd_soc_add_card_controls(card, card->controls, card->num_controls);
1843 if (card->dapm_routes)
1844 snd_soc_dapm_add_routes(&card->dapm, card->dapm_routes,
1845 card->num_dapm_routes);
1847 for (i = 0; i < card->num_links; i++) {
1848 struct snd_soc_pcm_runtime *rtd = &card->rtd[i];
1849 dai_link = &card->dai_link[i];
1850 dai_fmt = dai_link->dai_fmt;
1852 if (dai_fmt) {
1853 struct snd_soc_dai **codec_dais = rtd->codec_dais;
1854 int j;
1856 for (j = 0; j < rtd->num_codecs; j++) {
1857 struct snd_soc_dai *codec_dai = codec_dais[j];
1859 ret = snd_soc_dai_set_fmt(codec_dai, dai_fmt);
1860 if (ret != 0 && ret != -ENOTSUPP)
1861 dev_warn(codec_dai->dev,
1862 "ASoC: Failed to set DAI format: %d\n",
1863 ret);
1867 /* If this is a regular CPU link there will be a platform */
1868 if (dai_fmt &&
1869 (dai_link->platform_name || dai_link->platform_of_node)) {
1870 ret = snd_soc_dai_set_fmt(card->rtd[i].cpu_dai,
1871 dai_fmt);
1872 if (ret != 0 && ret != -ENOTSUPP)
1873 dev_warn(card->rtd[i].cpu_dai->dev,
1874 "ASoC: Failed to set DAI format: %d\n",
1875 ret);
1876 } else if (dai_fmt) {
1877 /* Flip the polarity for the "CPU" end */
1878 dai_fmt &= ~SND_SOC_DAIFMT_MASTER_MASK;
1879 switch (dai_link->dai_fmt &
1880 SND_SOC_DAIFMT_MASTER_MASK) {
1881 case SND_SOC_DAIFMT_CBM_CFM:
1882 dai_fmt |= SND_SOC_DAIFMT_CBS_CFS;
1883 break;
1884 case SND_SOC_DAIFMT_CBM_CFS:
1885 dai_fmt |= SND_SOC_DAIFMT_CBS_CFM;
1886 break;
1887 case SND_SOC_DAIFMT_CBS_CFM:
1888 dai_fmt |= SND_SOC_DAIFMT_CBM_CFS;
1889 break;
1890 case SND_SOC_DAIFMT_CBS_CFS:
1891 dai_fmt |= SND_SOC_DAIFMT_CBM_CFM;
1892 break;
1895 ret = snd_soc_dai_set_fmt(card->rtd[i].cpu_dai,
1896 dai_fmt);
1897 if (ret != 0 && ret != -ENOTSUPP)
1898 dev_warn(card->rtd[i].cpu_dai->dev,
1899 "ASoC: Failed to set DAI format: %d\n",
1900 ret);
1904 snprintf(card->snd_card->shortname, sizeof(card->snd_card->shortname),
1905 "%s", card->name);
1906 snprintf(card->snd_card->longname, sizeof(card->snd_card->longname),
1907 "%s", card->long_name ? card->long_name : card->name);
1908 snprintf(card->snd_card->driver, sizeof(card->snd_card->driver),
1909 "%s", card->driver_name ? card->driver_name : card->name);
1910 for (i = 0; i < ARRAY_SIZE(card->snd_card->driver); i++) {
1911 switch (card->snd_card->driver[i]) {
1912 case '_':
1913 case '-':
1914 case '\0':
1915 break;
1916 default:
1917 if (!isalnum(card->snd_card->driver[i]))
1918 card->snd_card->driver[i] = '_';
1919 break;
1923 if (card->late_probe) {
1924 ret = card->late_probe(card);
1925 if (ret < 0) {
1926 dev_err(card->dev, "ASoC: %s late_probe() failed: %d\n",
1927 card->name, ret);
1928 goto probe_aux_dev_err;
1932 if (card->fully_routed)
1933 snd_soc_dapm_auto_nc_pins(card);
1935 snd_soc_dapm_new_widgets(card);
1937 ret = snd_card_register(card->snd_card);
1938 if (ret < 0) {
1939 dev_err(card->dev, "ASoC: failed to register soundcard %d\n",
1940 ret);
1941 goto probe_aux_dev_err;
1944 #ifdef CONFIG_SND_SOC_AC97_BUS
1945 /* register any AC97 codecs */
1946 for (i = 0; i < card->num_rtd; i++) {
1947 ret = soc_register_ac97_dai_link(&card->rtd[i]);
1948 if (ret < 0) {
1949 dev_err(card->dev,
1950 "ASoC: failed to register AC97: %d\n", ret);
1951 while (--i >= 0)
1952 soc_unregister_ac97_dai_link(&card->rtd[i]);
1953 goto probe_aux_dev_err;
1956 #endif
1958 card->instantiated = 1;
1959 snd_soc_dapm_sync(&card->dapm);
1960 mutex_unlock(&card->mutex);
1962 return 0;
1964 probe_aux_dev_err:
1965 for (i = 0; i < card->num_aux_devs; i++)
1966 soc_remove_aux_dev(card, i);
1968 probe_dai_err:
1969 soc_remove_dai_links(card);
1971 card_probe_error:
1972 if (card->remove)
1973 card->remove(card);
1975 snd_card_free(card->snd_card);
1977 base_error:
1978 mutex_unlock(&card->mutex);
1980 return ret;
1983 /* probes a new socdev */
1984 static int soc_probe(struct platform_device *pdev)
1986 struct snd_soc_card *card = platform_get_drvdata(pdev);
1989 * no card, so machine driver should be registering card
1990 * we should not be here in that case so ret error
1992 if (!card)
1993 return -EINVAL;
1995 dev_warn(&pdev->dev,
1996 "ASoC: machine %s should use snd_soc_register_card()\n",
1997 card->name);
1999 /* Bodge while we unpick instantiation */
2000 card->dev = &pdev->dev;
2002 return snd_soc_register_card(card);
2005 static int soc_cleanup_card_resources(struct snd_soc_card *card)
2007 int i;
2009 /* make sure any delayed work runs */
2010 for (i = 0; i < card->num_rtd; i++) {
2011 struct snd_soc_pcm_runtime *rtd = &card->rtd[i];
2012 flush_delayed_work(&rtd->delayed_work);
2015 /* remove auxiliary devices */
2016 for (i = 0; i < card->num_aux_devs; i++)
2017 soc_remove_aux_dev(card, i);
2019 /* remove and free each DAI */
2020 soc_remove_dai_links(card);
2022 soc_cleanup_card_debugfs(card);
2024 /* remove the card */
2025 if (card->remove)
2026 card->remove(card);
2028 snd_soc_dapm_free(&card->dapm);
2030 snd_card_free(card->snd_card);
2031 return 0;
2035 /* removes a socdev */
2036 static int soc_remove(struct platform_device *pdev)
2038 struct snd_soc_card *card = platform_get_drvdata(pdev);
2040 snd_soc_unregister_card(card);
2041 return 0;
2044 int snd_soc_poweroff(struct device *dev)
2046 struct snd_soc_card *card = dev_get_drvdata(dev);
2047 int i;
2049 if (!card->instantiated)
2050 return 0;
2052 /* Flush out pmdown_time work - we actually do want to run it
2053 * now, we're shutting down so no imminent restart. */
2054 for (i = 0; i < card->num_rtd; i++) {
2055 struct snd_soc_pcm_runtime *rtd = &card->rtd[i];
2056 flush_delayed_work(&rtd->delayed_work);
2059 snd_soc_dapm_shutdown(card);
2061 /* deactivate pins to sleep state */
2062 for (i = 0; i < card->num_rtd; i++) {
2063 struct snd_soc_pcm_runtime *rtd = &card->rtd[i];
2064 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
2065 int j;
2067 pinctrl_pm_select_sleep_state(cpu_dai->dev);
2068 for (j = 0; j < rtd->num_codecs; j++) {
2069 struct snd_soc_dai *codec_dai = rtd->codec_dais[j];
2070 pinctrl_pm_select_sleep_state(codec_dai->dev);
2074 return 0;
2076 EXPORT_SYMBOL_GPL(snd_soc_poweroff);
2078 const struct dev_pm_ops snd_soc_pm_ops = {
2079 .suspend = snd_soc_suspend,
2080 .resume = snd_soc_resume,
2081 .freeze = snd_soc_suspend,
2082 .thaw = snd_soc_resume,
2083 .poweroff = snd_soc_poweroff,
2084 .restore = snd_soc_resume,
2086 EXPORT_SYMBOL_GPL(snd_soc_pm_ops);
2088 /* ASoC platform driver */
2089 static struct platform_driver soc_driver = {
2090 .driver = {
2091 .name = "soc-audio",
2092 .owner = THIS_MODULE,
2093 .pm = &snd_soc_pm_ops,
2095 .probe = soc_probe,
2096 .remove = soc_remove,
2100 * snd_soc_new_ac97_codec - initailise AC97 device
2101 * @codec: audio codec
2102 * @ops: AC97 bus operations
2103 * @num: AC97 codec number
2105 * Initialises AC97 codec resources for use by ad-hoc devices only.
2107 int snd_soc_new_ac97_codec(struct snd_soc_codec *codec,
2108 struct snd_ac97_bus_ops *ops, int num)
2110 mutex_lock(&codec->mutex);
2112 codec->ac97 = kzalloc(sizeof(struct snd_ac97), GFP_KERNEL);
2113 if (codec->ac97 == NULL) {
2114 mutex_unlock(&codec->mutex);
2115 return -ENOMEM;
2118 codec->ac97->bus = kzalloc(sizeof(struct snd_ac97_bus), GFP_KERNEL);
2119 if (codec->ac97->bus == NULL) {
2120 kfree(codec->ac97);
2121 codec->ac97 = NULL;
2122 mutex_unlock(&codec->mutex);
2123 return -ENOMEM;
2126 codec->ac97->bus->ops = ops;
2127 codec->ac97->num = num;
2130 * Mark the AC97 device to be created by us. This way we ensure that the
2131 * device will be registered with the device subsystem later on.
2133 codec->ac97_created = 1;
2135 mutex_unlock(&codec->mutex);
2136 return 0;
2138 EXPORT_SYMBOL_GPL(snd_soc_new_ac97_codec);
2140 static struct snd_ac97_reset_cfg snd_ac97_rst_cfg;
2142 static void snd_soc_ac97_warm_reset(struct snd_ac97 *ac97)
2144 struct pinctrl *pctl = snd_ac97_rst_cfg.pctl;
2146 pinctrl_select_state(pctl, snd_ac97_rst_cfg.pstate_warm_reset);
2148 gpio_direction_output(snd_ac97_rst_cfg.gpio_sync, 1);
2150 udelay(10);
2152 gpio_direction_output(snd_ac97_rst_cfg.gpio_sync, 0);
2154 pinctrl_select_state(pctl, snd_ac97_rst_cfg.pstate_run);
2155 msleep(2);
2158 static void snd_soc_ac97_reset(struct snd_ac97 *ac97)
2160 struct pinctrl *pctl = snd_ac97_rst_cfg.pctl;
2162 pinctrl_select_state(pctl, snd_ac97_rst_cfg.pstate_reset);
2164 gpio_direction_output(snd_ac97_rst_cfg.gpio_sync, 0);
2165 gpio_direction_output(snd_ac97_rst_cfg.gpio_sdata, 0);
2166 gpio_direction_output(snd_ac97_rst_cfg.gpio_reset, 0);
2168 udelay(10);
2170 gpio_direction_output(snd_ac97_rst_cfg.gpio_reset, 1);
2172 pinctrl_select_state(pctl, snd_ac97_rst_cfg.pstate_run);
2173 msleep(2);
2176 static int snd_soc_ac97_parse_pinctl(struct device *dev,
2177 struct snd_ac97_reset_cfg *cfg)
2179 struct pinctrl *p;
2180 struct pinctrl_state *state;
2181 int gpio;
2182 int ret;
2184 p = devm_pinctrl_get(dev);
2185 if (IS_ERR(p)) {
2186 dev_err(dev, "Failed to get pinctrl\n");
2187 return PTR_ERR(p);
2189 cfg->pctl = p;
2191 state = pinctrl_lookup_state(p, "ac97-reset");
2192 if (IS_ERR(state)) {
2193 dev_err(dev, "Can't find pinctrl state ac97-reset\n");
2194 return PTR_ERR(state);
2196 cfg->pstate_reset = state;
2198 state = pinctrl_lookup_state(p, "ac97-warm-reset");
2199 if (IS_ERR(state)) {
2200 dev_err(dev, "Can't find pinctrl state ac97-warm-reset\n");
2201 return PTR_ERR(state);
2203 cfg->pstate_warm_reset = state;
2205 state = pinctrl_lookup_state(p, "ac97-running");
2206 if (IS_ERR(state)) {
2207 dev_err(dev, "Can't find pinctrl state ac97-running\n");
2208 return PTR_ERR(state);
2210 cfg->pstate_run = state;
2212 gpio = of_get_named_gpio(dev->of_node, "ac97-gpios", 0);
2213 if (gpio < 0) {
2214 dev_err(dev, "Can't find ac97-sync gpio\n");
2215 return gpio;
2217 ret = devm_gpio_request(dev, gpio, "AC97 link sync");
2218 if (ret) {
2219 dev_err(dev, "Failed requesting ac97-sync gpio\n");
2220 return ret;
2222 cfg->gpio_sync = gpio;
2224 gpio = of_get_named_gpio(dev->of_node, "ac97-gpios", 1);
2225 if (gpio < 0) {
2226 dev_err(dev, "Can't find ac97-sdata gpio %d\n", gpio);
2227 return gpio;
2229 ret = devm_gpio_request(dev, gpio, "AC97 link sdata");
2230 if (ret) {
2231 dev_err(dev, "Failed requesting ac97-sdata gpio\n");
2232 return ret;
2234 cfg->gpio_sdata = gpio;
2236 gpio = of_get_named_gpio(dev->of_node, "ac97-gpios", 2);
2237 if (gpio < 0) {
2238 dev_err(dev, "Can't find ac97-reset gpio\n");
2239 return gpio;
2241 ret = devm_gpio_request(dev, gpio, "AC97 link reset");
2242 if (ret) {
2243 dev_err(dev, "Failed requesting ac97-reset gpio\n");
2244 return ret;
2246 cfg->gpio_reset = gpio;
2248 return 0;
2251 struct snd_ac97_bus_ops *soc_ac97_ops;
2252 EXPORT_SYMBOL_GPL(soc_ac97_ops);
2254 int snd_soc_set_ac97_ops(struct snd_ac97_bus_ops *ops)
2256 if (ops == soc_ac97_ops)
2257 return 0;
2259 if (soc_ac97_ops && ops)
2260 return -EBUSY;
2262 soc_ac97_ops = ops;
2264 return 0;
2266 EXPORT_SYMBOL_GPL(snd_soc_set_ac97_ops);
2269 * snd_soc_set_ac97_ops_of_reset - Set ac97 ops with generic ac97 reset functions
2271 * This function sets the reset and warm_reset properties of ops and parses
2272 * the device node of pdev to get pinctrl states and gpio numbers to use.
2274 int snd_soc_set_ac97_ops_of_reset(struct snd_ac97_bus_ops *ops,
2275 struct platform_device *pdev)
2277 struct device *dev = &pdev->dev;
2278 struct snd_ac97_reset_cfg cfg;
2279 int ret;
2281 ret = snd_soc_ac97_parse_pinctl(dev, &cfg);
2282 if (ret)
2283 return ret;
2285 ret = snd_soc_set_ac97_ops(ops);
2286 if (ret)
2287 return ret;
2289 ops->warm_reset = snd_soc_ac97_warm_reset;
2290 ops->reset = snd_soc_ac97_reset;
2292 snd_ac97_rst_cfg = cfg;
2293 return 0;
2295 EXPORT_SYMBOL_GPL(snd_soc_set_ac97_ops_of_reset);
2298 * snd_soc_free_ac97_codec - free AC97 codec device
2299 * @codec: audio codec
2301 * Frees AC97 codec device resources.
2303 void snd_soc_free_ac97_codec(struct snd_soc_codec *codec)
2305 mutex_lock(&codec->mutex);
2306 #ifdef CONFIG_SND_SOC_AC97_BUS
2307 soc_unregister_ac97_codec(codec);
2308 #endif
2309 kfree(codec->ac97->bus);
2310 kfree(codec->ac97);
2311 codec->ac97 = NULL;
2312 codec->ac97_created = 0;
2313 mutex_unlock(&codec->mutex);
2315 EXPORT_SYMBOL_GPL(snd_soc_free_ac97_codec);
2318 * snd_soc_cnew - create new control
2319 * @_template: control template
2320 * @data: control private data
2321 * @long_name: control long name
2322 * @prefix: control name prefix
2324 * Create a new mixer control from a template control.
2326 * Returns 0 for success, else error.
2328 struct snd_kcontrol *snd_soc_cnew(const struct snd_kcontrol_new *_template,
2329 void *data, const char *long_name,
2330 const char *prefix)
2332 struct snd_kcontrol_new template;
2333 struct snd_kcontrol *kcontrol;
2334 char *name = NULL;
2336 memcpy(&template, _template, sizeof(template));
2337 template.index = 0;
2339 if (!long_name)
2340 long_name = template.name;
2342 if (prefix) {
2343 name = kasprintf(GFP_KERNEL, "%s %s", prefix, long_name);
2344 if (!name)
2345 return NULL;
2347 template.name = name;
2348 } else {
2349 template.name = long_name;
2352 kcontrol = snd_ctl_new1(&template, data);
2354 kfree(name);
2356 return kcontrol;
2358 EXPORT_SYMBOL_GPL(snd_soc_cnew);
2360 static int snd_soc_add_controls(struct snd_card *card, struct device *dev,
2361 const struct snd_kcontrol_new *controls, int num_controls,
2362 const char *prefix, void *data)
2364 int err, i;
2366 for (i = 0; i < num_controls; i++) {
2367 const struct snd_kcontrol_new *control = &controls[i];
2368 err = snd_ctl_add(card, snd_soc_cnew(control, data,
2369 control->name, prefix));
2370 if (err < 0) {
2371 dev_err(dev, "ASoC: Failed to add %s: %d\n",
2372 control->name, err);
2373 return err;
2377 return 0;
2380 struct snd_kcontrol *snd_soc_card_get_kcontrol(struct snd_soc_card *soc_card,
2381 const char *name)
2383 struct snd_card *card = soc_card->snd_card;
2384 struct snd_kcontrol *kctl;
2386 if (unlikely(!name))
2387 return NULL;
2389 list_for_each_entry(kctl, &card->controls, list)
2390 if (!strncmp(kctl->id.name, name, sizeof(kctl->id.name)))
2391 return kctl;
2392 return NULL;
2394 EXPORT_SYMBOL_GPL(snd_soc_card_get_kcontrol);
2397 * snd_soc_add_component_controls - Add an array of controls to a component.
2399 * @component: Component to add controls to
2400 * @controls: Array of controls to add
2401 * @num_controls: Number of elements in the array
2403 * Return: 0 for success, else error.
2405 int snd_soc_add_component_controls(struct snd_soc_component *component,
2406 const struct snd_kcontrol_new *controls, unsigned int num_controls)
2408 struct snd_card *card = component->card->snd_card;
2410 return snd_soc_add_controls(card, component->dev, controls,
2411 num_controls, component->name_prefix, component);
2413 EXPORT_SYMBOL_GPL(snd_soc_add_component_controls);
2416 * snd_soc_add_codec_controls - add an array of controls to a codec.
2417 * Convenience function to add a list of controls. Many codecs were
2418 * duplicating this code.
2420 * @codec: codec to add controls to
2421 * @controls: array of controls to add
2422 * @num_controls: number of elements in the array
2424 * Return 0 for success, else error.
2426 int snd_soc_add_codec_controls(struct snd_soc_codec *codec,
2427 const struct snd_kcontrol_new *controls, unsigned int num_controls)
2429 return snd_soc_add_component_controls(&codec->component, controls,
2430 num_controls);
2432 EXPORT_SYMBOL_GPL(snd_soc_add_codec_controls);
2435 * snd_soc_add_platform_controls - add an array of controls to a platform.
2436 * Convenience function to add a list of controls.
2438 * @platform: platform to add controls to
2439 * @controls: array of controls to add
2440 * @num_controls: number of elements in the array
2442 * Return 0 for success, else error.
2444 int snd_soc_add_platform_controls(struct snd_soc_platform *platform,
2445 const struct snd_kcontrol_new *controls, unsigned int num_controls)
2447 return snd_soc_add_component_controls(&platform->component, controls,
2448 num_controls);
2450 EXPORT_SYMBOL_GPL(snd_soc_add_platform_controls);
2453 * snd_soc_add_card_controls - add an array of controls to a SoC card.
2454 * Convenience function to add a list of controls.
2456 * @soc_card: SoC card to add controls to
2457 * @controls: array of controls to add
2458 * @num_controls: number of elements in the array
2460 * Return 0 for success, else error.
2462 int snd_soc_add_card_controls(struct snd_soc_card *soc_card,
2463 const struct snd_kcontrol_new *controls, int num_controls)
2465 struct snd_card *card = soc_card->snd_card;
2467 return snd_soc_add_controls(card, soc_card->dev, controls, num_controls,
2468 NULL, soc_card);
2470 EXPORT_SYMBOL_GPL(snd_soc_add_card_controls);
2473 * snd_soc_add_dai_controls - add an array of controls to a DAI.
2474 * Convienience function to add a list of controls.
2476 * @dai: DAI to add controls to
2477 * @controls: array of controls to add
2478 * @num_controls: number of elements in the array
2480 * Return 0 for success, else error.
2482 int snd_soc_add_dai_controls(struct snd_soc_dai *dai,
2483 const struct snd_kcontrol_new *controls, int num_controls)
2485 struct snd_card *card = dai->card->snd_card;
2487 return snd_soc_add_controls(card, dai->dev, controls, num_controls,
2488 NULL, dai);
2490 EXPORT_SYMBOL_GPL(snd_soc_add_dai_controls);
2493 * snd_soc_info_enum_double - enumerated double mixer info callback
2494 * @kcontrol: mixer control
2495 * @uinfo: control element information
2497 * Callback to provide information about a double enumerated
2498 * mixer control.
2500 * Returns 0 for success.
2502 int snd_soc_info_enum_double(struct snd_kcontrol *kcontrol,
2503 struct snd_ctl_elem_info *uinfo)
2505 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
2507 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
2508 uinfo->count = e->shift_l == e->shift_r ? 1 : 2;
2509 uinfo->value.enumerated.items = e->items;
2511 if (uinfo->value.enumerated.item >= e->items)
2512 uinfo->value.enumerated.item = e->items - 1;
2513 strlcpy(uinfo->value.enumerated.name,
2514 e->texts[uinfo->value.enumerated.item],
2515 sizeof(uinfo->value.enumerated.name));
2516 return 0;
2518 EXPORT_SYMBOL_GPL(snd_soc_info_enum_double);
2521 * snd_soc_get_enum_double - enumerated double mixer get callback
2522 * @kcontrol: mixer control
2523 * @ucontrol: control element information
2525 * Callback to get the value of a double enumerated mixer.
2527 * Returns 0 for success.
2529 int snd_soc_get_enum_double(struct snd_kcontrol *kcontrol,
2530 struct snd_ctl_elem_value *ucontrol)
2532 struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
2533 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
2534 unsigned int val, item;
2535 unsigned int reg_val;
2536 int ret;
2538 ret = snd_soc_component_read(component, e->reg, &reg_val);
2539 if (ret)
2540 return ret;
2541 val = (reg_val >> e->shift_l) & e->mask;
2542 item = snd_soc_enum_val_to_item(e, val);
2543 ucontrol->value.enumerated.item[0] = item;
2544 if (e->shift_l != e->shift_r) {
2545 val = (reg_val >> e->shift_l) & e->mask;
2546 item = snd_soc_enum_val_to_item(e, val);
2547 ucontrol->value.enumerated.item[1] = item;
2550 return 0;
2552 EXPORT_SYMBOL_GPL(snd_soc_get_enum_double);
2555 * snd_soc_put_enum_double - enumerated double mixer put callback
2556 * @kcontrol: mixer control
2557 * @ucontrol: control element information
2559 * Callback to set the value of a double enumerated mixer.
2561 * Returns 0 for success.
2563 int snd_soc_put_enum_double(struct snd_kcontrol *kcontrol,
2564 struct snd_ctl_elem_value *ucontrol)
2566 struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
2567 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
2568 unsigned int *item = ucontrol->value.enumerated.item;
2569 unsigned int val;
2570 unsigned int mask;
2572 if (item[0] >= e->items)
2573 return -EINVAL;
2574 val = snd_soc_enum_item_to_val(e, item[0]) << e->shift_l;
2575 mask = e->mask << e->shift_l;
2576 if (e->shift_l != e->shift_r) {
2577 if (item[1] >= e->items)
2578 return -EINVAL;
2579 val |= snd_soc_enum_item_to_val(e, item[1]) << e->shift_r;
2580 mask |= e->mask << e->shift_r;
2583 return snd_soc_component_update_bits(component, e->reg, mask, val);
2585 EXPORT_SYMBOL_GPL(snd_soc_put_enum_double);
2588 * snd_soc_read_signed - Read a codec register and interprete as signed value
2589 * @component: component
2590 * @reg: Register to read
2591 * @mask: Mask to use after shifting the register value
2592 * @shift: Right shift of register value
2593 * @sign_bit: Bit that describes if a number is negative or not.
2594 * @signed_val: Pointer to where the read value should be stored
2596 * This functions reads a codec register. The register value is shifted right
2597 * by 'shift' bits and masked with the given 'mask'. Afterwards it translates
2598 * the given registervalue into a signed integer if sign_bit is non-zero.
2600 * Returns 0 on sucess, otherwise an error value
2602 static int snd_soc_read_signed(struct snd_soc_component *component,
2603 unsigned int reg, unsigned int mask, unsigned int shift,
2604 unsigned int sign_bit, int *signed_val)
2606 int ret;
2607 unsigned int val;
2609 ret = snd_soc_component_read(component, reg, &val);
2610 if (ret < 0)
2611 return ret;
2613 val = (val >> shift) & mask;
2615 if (!sign_bit) {
2616 *signed_val = val;
2617 return 0;
2620 /* non-negative number */
2621 if (!(val & BIT(sign_bit))) {
2622 *signed_val = val;
2623 return 0;
2626 ret = val;
2629 * The register most probably does not contain a full-sized int.
2630 * Instead we have an arbitrary number of bits in a signed
2631 * representation which has to be translated into a full-sized int.
2632 * This is done by filling up all bits above the sign-bit.
2634 ret |= ~((int)(BIT(sign_bit) - 1));
2636 *signed_val = ret;
2638 return 0;
2642 * snd_soc_info_volsw - single mixer info callback
2643 * @kcontrol: mixer control
2644 * @uinfo: control element information
2646 * Callback to provide information about a single mixer control, or a double
2647 * mixer control that spans 2 registers.
2649 * Returns 0 for success.
2651 int snd_soc_info_volsw(struct snd_kcontrol *kcontrol,
2652 struct snd_ctl_elem_info *uinfo)
2654 struct soc_mixer_control *mc =
2655 (struct soc_mixer_control *)kcontrol->private_value;
2656 int platform_max;
2658 if (!mc->platform_max)
2659 mc->platform_max = mc->max;
2660 platform_max = mc->platform_max;
2662 if (platform_max == 1 && !strstr(kcontrol->id.name, " Volume"))
2663 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
2664 else
2665 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2667 uinfo->count = snd_soc_volsw_is_stereo(mc) ? 2 : 1;
2668 uinfo->value.integer.min = 0;
2669 uinfo->value.integer.max = platform_max - mc->min;
2670 return 0;
2672 EXPORT_SYMBOL_GPL(snd_soc_info_volsw);
2675 * snd_soc_get_volsw - single mixer get callback
2676 * @kcontrol: mixer control
2677 * @ucontrol: control element information
2679 * Callback to get the value of a single mixer control, or a double mixer
2680 * control that spans 2 registers.
2682 * Returns 0 for success.
2684 int snd_soc_get_volsw(struct snd_kcontrol *kcontrol,
2685 struct snd_ctl_elem_value *ucontrol)
2687 struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
2688 struct soc_mixer_control *mc =
2689 (struct soc_mixer_control *)kcontrol->private_value;
2690 unsigned int reg = mc->reg;
2691 unsigned int reg2 = mc->rreg;
2692 unsigned int shift = mc->shift;
2693 unsigned int rshift = mc->rshift;
2694 int max = mc->max;
2695 int min = mc->min;
2696 int sign_bit = mc->sign_bit;
2697 unsigned int mask = (1 << fls(max)) - 1;
2698 unsigned int invert = mc->invert;
2699 int val;
2700 int ret;
2702 if (sign_bit)
2703 mask = BIT(sign_bit + 1) - 1;
2705 ret = snd_soc_read_signed(component, reg, mask, shift, sign_bit, &val);
2706 if (ret)
2707 return ret;
2709 ucontrol->value.integer.value[0] = val - min;
2710 if (invert)
2711 ucontrol->value.integer.value[0] =
2712 max - ucontrol->value.integer.value[0];
2714 if (snd_soc_volsw_is_stereo(mc)) {
2715 if (reg == reg2)
2716 ret = snd_soc_read_signed(component, reg, mask, rshift,
2717 sign_bit, &val);
2718 else
2719 ret = snd_soc_read_signed(component, reg2, mask, shift,
2720 sign_bit, &val);
2721 if (ret)
2722 return ret;
2724 ucontrol->value.integer.value[1] = val - min;
2725 if (invert)
2726 ucontrol->value.integer.value[1] =
2727 max - ucontrol->value.integer.value[1];
2730 return 0;
2732 EXPORT_SYMBOL_GPL(snd_soc_get_volsw);
2735 * snd_soc_put_volsw - single mixer put callback
2736 * @kcontrol: mixer control
2737 * @ucontrol: control element information
2739 * Callback to set the value of a single mixer control, or a double mixer
2740 * control that spans 2 registers.
2742 * Returns 0 for success.
2744 int snd_soc_put_volsw(struct snd_kcontrol *kcontrol,
2745 struct snd_ctl_elem_value *ucontrol)
2747 struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
2748 struct soc_mixer_control *mc =
2749 (struct soc_mixer_control *)kcontrol->private_value;
2750 unsigned int reg = mc->reg;
2751 unsigned int reg2 = mc->rreg;
2752 unsigned int shift = mc->shift;
2753 unsigned int rshift = mc->rshift;
2754 int max = mc->max;
2755 int min = mc->min;
2756 unsigned int sign_bit = mc->sign_bit;
2757 unsigned int mask = (1 << fls(max)) - 1;
2758 unsigned int invert = mc->invert;
2759 int err;
2760 bool type_2r = false;
2761 unsigned int val2 = 0;
2762 unsigned int val, val_mask;
2764 if (sign_bit)
2765 mask = BIT(sign_bit + 1) - 1;
2767 val = ((ucontrol->value.integer.value[0] + min) & mask);
2768 if (invert)
2769 val = max - val;
2770 val_mask = mask << shift;
2771 val = val << shift;
2772 if (snd_soc_volsw_is_stereo(mc)) {
2773 val2 = ((ucontrol->value.integer.value[1] + min) & mask);
2774 if (invert)
2775 val2 = max - val2;
2776 if (reg == reg2) {
2777 val_mask |= mask << rshift;
2778 val |= val2 << rshift;
2779 } else {
2780 val2 = val2 << shift;
2781 type_2r = true;
2784 err = snd_soc_component_update_bits(component, reg, val_mask, val);
2785 if (err < 0)
2786 return err;
2788 if (type_2r)
2789 err = snd_soc_component_update_bits(component, reg2, val_mask,
2790 val2);
2792 return err;
2794 EXPORT_SYMBOL_GPL(snd_soc_put_volsw);
2797 * snd_soc_get_volsw_sx - single mixer get callback
2798 * @kcontrol: mixer control
2799 * @ucontrol: control element information
2801 * Callback to get the value of a single mixer control, or a double mixer
2802 * control that spans 2 registers.
2804 * Returns 0 for success.
2806 int snd_soc_get_volsw_sx(struct snd_kcontrol *kcontrol,
2807 struct snd_ctl_elem_value *ucontrol)
2809 struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
2810 struct soc_mixer_control *mc =
2811 (struct soc_mixer_control *)kcontrol->private_value;
2812 unsigned int reg = mc->reg;
2813 unsigned int reg2 = mc->rreg;
2814 unsigned int shift = mc->shift;
2815 unsigned int rshift = mc->rshift;
2816 int max = mc->max;
2817 int min = mc->min;
2818 int mask = (1 << (fls(min + max) - 1)) - 1;
2819 unsigned int val;
2820 int ret;
2822 ret = snd_soc_component_read(component, reg, &val);
2823 if (ret < 0)
2824 return ret;
2826 ucontrol->value.integer.value[0] = ((val >> shift) - min) & mask;
2828 if (snd_soc_volsw_is_stereo(mc)) {
2829 ret = snd_soc_component_read(component, reg2, &val);
2830 if (ret < 0)
2831 return ret;
2833 val = ((val >> rshift) - min) & mask;
2834 ucontrol->value.integer.value[1] = val;
2837 return 0;
2839 EXPORT_SYMBOL_GPL(snd_soc_get_volsw_sx);
2842 * snd_soc_put_volsw_sx - double mixer set callback
2843 * @kcontrol: mixer control
2844 * @uinfo: control element information
2846 * Callback to set the value of a double mixer control that spans 2 registers.
2848 * Returns 0 for success.
2850 int snd_soc_put_volsw_sx(struct snd_kcontrol *kcontrol,
2851 struct snd_ctl_elem_value *ucontrol)
2853 struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
2854 struct soc_mixer_control *mc =
2855 (struct soc_mixer_control *)kcontrol->private_value;
2857 unsigned int reg = mc->reg;
2858 unsigned int reg2 = mc->rreg;
2859 unsigned int shift = mc->shift;
2860 unsigned int rshift = mc->rshift;
2861 int max = mc->max;
2862 int min = mc->min;
2863 int mask = (1 << (fls(min + max) - 1)) - 1;
2864 int err = 0;
2865 unsigned int val, val_mask, val2 = 0;
2867 val_mask = mask << shift;
2868 val = (ucontrol->value.integer.value[0] + min) & mask;
2869 val = val << shift;
2871 err = snd_soc_component_update_bits(component, reg, val_mask, val);
2872 if (err < 0)
2873 return err;
2875 if (snd_soc_volsw_is_stereo(mc)) {
2876 val_mask = mask << rshift;
2877 val2 = (ucontrol->value.integer.value[1] + min) & mask;
2878 val2 = val2 << rshift;
2880 err = snd_soc_component_update_bits(component, reg2, val_mask,
2881 val2);
2883 return err;
2885 EXPORT_SYMBOL_GPL(snd_soc_put_volsw_sx);
2888 * snd_soc_info_volsw_s8 - signed mixer info callback
2889 * @kcontrol: mixer control
2890 * @uinfo: control element information
2892 * Callback to provide information about a signed mixer control.
2894 * Returns 0 for success.
2896 int snd_soc_info_volsw_s8(struct snd_kcontrol *kcontrol,
2897 struct snd_ctl_elem_info *uinfo)
2899 struct soc_mixer_control *mc =
2900 (struct soc_mixer_control *)kcontrol->private_value;
2901 int platform_max;
2902 int min = mc->min;
2904 if (!mc->platform_max)
2905 mc->platform_max = mc->max;
2906 platform_max = mc->platform_max;
2908 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2909 uinfo->count = 2;
2910 uinfo->value.integer.min = 0;
2911 uinfo->value.integer.max = platform_max - min;
2912 return 0;
2914 EXPORT_SYMBOL_GPL(snd_soc_info_volsw_s8);
2917 * snd_soc_get_volsw_s8 - signed mixer get callback
2918 * @kcontrol: mixer control
2919 * @ucontrol: control element information
2921 * Callback to get the value of a signed mixer control.
2923 * Returns 0 for success.
2925 int snd_soc_get_volsw_s8(struct snd_kcontrol *kcontrol,
2926 struct snd_ctl_elem_value *ucontrol)
2928 struct soc_mixer_control *mc =
2929 (struct soc_mixer_control *)kcontrol->private_value;
2930 struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
2931 unsigned int reg = mc->reg;
2932 unsigned int val;
2933 int min = mc->min;
2934 int ret;
2936 ret = snd_soc_component_read(component, reg, &val);
2937 if (ret)
2938 return ret;
2940 ucontrol->value.integer.value[0] =
2941 ((signed char)(val & 0xff))-min;
2942 ucontrol->value.integer.value[1] =
2943 ((signed char)((val >> 8) & 0xff))-min;
2944 return 0;
2946 EXPORT_SYMBOL_GPL(snd_soc_get_volsw_s8);
2949 * snd_soc_put_volsw_sgn - signed mixer put callback
2950 * @kcontrol: mixer control
2951 * @ucontrol: control element information
2953 * Callback to set the value of a signed mixer control.
2955 * Returns 0 for success.
2957 int snd_soc_put_volsw_s8(struct snd_kcontrol *kcontrol,
2958 struct snd_ctl_elem_value *ucontrol)
2960 struct soc_mixer_control *mc =
2961 (struct soc_mixer_control *)kcontrol->private_value;
2962 struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
2963 unsigned int reg = mc->reg;
2964 int min = mc->min;
2965 unsigned int val;
2967 val = (ucontrol->value.integer.value[0]+min) & 0xff;
2968 val |= ((ucontrol->value.integer.value[1]+min) & 0xff) << 8;
2970 return snd_soc_component_update_bits(component, reg, 0xffff, val);
2972 EXPORT_SYMBOL_GPL(snd_soc_put_volsw_s8);
2975 * snd_soc_info_volsw_range - single mixer info callback with range.
2976 * @kcontrol: mixer control
2977 * @uinfo: control element information
2979 * Callback to provide information, within a range, about a single
2980 * mixer control.
2982 * returns 0 for success.
2984 int snd_soc_info_volsw_range(struct snd_kcontrol *kcontrol,
2985 struct snd_ctl_elem_info *uinfo)
2987 struct soc_mixer_control *mc =
2988 (struct soc_mixer_control *)kcontrol->private_value;
2989 int platform_max;
2990 int min = mc->min;
2992 if (!mc->platform_max)
2993 mc->platform_max = mc->max;
2994 platform_max = mc->platform_max;
2996 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2997 uinfo->count = snd_soc_volsw_is_stereo(mc) ? 2 : 1;
2998 uinfo->value.integer.min = 0;
2999 uinfo->value.integer.max = platform_max - min;
3001 return 0;
3003 EXPORT_SYMBOL_GPL(snd_soc_info_volsw_range);
3006 * snd_soc_put_volsw_range - single mixer put value callback with range.
3007 * @kcontrol: mixer control
3008 * @ucontrol: control element information
3010 * Callback to set the value, within a range, for a single mixer control.
3012 * Returns 0 for success.
3014 int snd_soc_put_volsw_range(struct snd_kcontrol *kcontrol,
3015 struct snd_ctl_elem_value *ucontrol)
3017 struct soc_mixer_control *mc =
3018 (struct soc_mixer_control *)kcontrol->private_value;
3019 struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
3020 unsigned int reg = mc->reg;
3021 unsigned int rreg = mc->rreg;
3022 unsigned int shift = mc->shift;
3023 int min = mc->min;
3024 int max = mc->max;
3025 unsigned int mask = (1 << fls(max)) - 1;
3026 unsigned int invert = mc->invert;
3027 unsigned int val, val_mask;
3028 int ret;
3030 val = ((ucontrol->value.integer.value[0] + min) & mask);
3031 if (invert)
3032 val = max - val;
3033 val_mask = mask << shift;
3034 val = val << shift;
3036 ret = snd_soc_component_update_bits(component, reg, val_mask, val);
3037 if (ret < 0)
3038 return ret;
3040 if (snd_soc_volsw_is_stereo(mc)) {
3041 val = ((ucontrol->value.integer.value[1] + min) & mask);
3042 if (invert)
3043 val = max - val;
3044 val_mask = mask << shift;
3045 val = val << shift;
3047 ret = snd_soc_component_update_bits(component, rreg, val_mask,
3048 val);
3051 return ret;
3053 EXPORT_SYMBOL_GPL(snd_soc_put_volsw_range);
3056 * snd_soc_get_volsw_range - single mixer get callback with range
3057 * @kcontrol: mixer control
3058 * @ucontrol: control element information
3060 * Callback to get the value, within a range, of a single mixer control.
3062 * Returns 0 for success.
3064 int snd_soc_get_volsw_range(struct snd_kcontrol *kcontrol,
3065 struct snd_ctl_elem_value *ucontrol)
3067 struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
3068 struct soc_mixer_control *mc =
3069 (struct soc_mixer_control *)kcontrol->private_value;
3070 unsigned int reg = mc->reg;
3071 unsigned int rreg = mc->rreg;
3072 unsigned int shift = mc->shift;
3073 int min = mc->min;
3074 int max = mc->max;
3075 unsigned int mask = (1 << fls(max)) - 1;
3076 unsigned int invert = mc->invert;
3077 unsigned int val;
3078 int ret;
3080 ret = snd_soc_component_read(component, reg, &val);
3081 if (ret)
3082 return ret;
3084 ucontrol->value.integer.value[0] = (val >> shift) & mask;
3085 if (invert)
3086 ucontrol->value.integer.value[0] =
3087 max - ucontrol->value.integer.value[0];
3088 ucontrol->value.integer.value[0] =
3089 ucontrol->value.integer.value[0] - min;
3091 if (snd_soc_volsw_is_stereo(mc)) {
3092 ret = snd_soc_component_read(component, rreg, &val);
3093 if (ret)
3094 return ret;
3096 ucontrol->value.integer.value[1] = (val >> shift) & mask;
3097 if (invert)
3098 ucontrol->value.integer.value[1] =
3099 max - ucontrol->value.integer.value[1];
3100 ucontrol->value.integer.value[1] =
3101 ucontrol->value.integer.value[1] - min;
3104 return 0;
3106 EXPORT_SYMBOL_GPL(snd_soc_get_volsw_range);
3109 * snd_soc_limit_volume - Set new limit to an existing volume control.
3111 * @codec: where to look for the control
3112 * @name: Name of the control
3113 * @max: new maximum limit
3115 * Return 0 for success, else error.
3117 int snd_soc_limit_volume(struct snd_soc_codec *codec,
3118 const char *name, int max)
3120 struct snd_card *card = codec->component.card->snd_card;
3121 struct snd_kcontrol *kctl;
3122 struct soc_mixer_control *mc;
3123 int found = 0;
3124 int ret = -EINVAL;
3126 /* Sanity check for name and max */
3127 if (unlikely(!name || max <= 0))
3128 return -EINVAL;
3130 list_for_each_entry(kctl, &card->controls, list) {
3131 if (!strncmp(kctl->id.name, name, sizeof(kctl->id.name))) {
3132 found = 1;
3133 break;
3136 if (found) {
3137 mc = (struct soc_mixer_control *)kctl->private_value;
3138 if (max <= mc->max) {
3139 mc->platform_max = max;
3140 ret = 0;
3143 return ret;
3145 EXPORT_SYMBOL_GPL(snd_soc_limit_volume);
3147 int snd_soc_bytes_info(struct snd_kcontrol *kcontrol,
3148 struct snd_ctl_elem_info *uinfo)
3150 struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
3151 struct soc_bytes *params = (void *)kcontrol->private_value;
3153 uinfo->type = SNDRV_CTL_ELEM_TYPE_BYTES;
3154 uinfo->count = params->num_regs * component->val_bytes;
3156 return 0;
3158 EXPORT_SYMBOL_GPL(snd_soc_bytes_info);
3160 int snd_soc_bytes_get(struct snd_kcontrol *kcontrol,
3161 struct snd_ctl_elem_value *ucontrol)
3163 struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
3164 struct soc_bytes *params = (void *)kcontrol->private_value;
3165 int ret;
3167 if (component->regmap)
3168 ret = regmap_raw_read(component->regmap, params->base,
3169 ucontrol->value.bytes.data,
3170 params->num_regs * component->val_bytes);
3171 else
3172 ret = -EINVAL;
3174 /* Hide any masked bytes to ensure consistent data reporting */
3175 if (ret == 0 && params->mask) {
3176 switch (component->val_bytes) {
3177 case 1:
3178 ucontrol->value.bytes.data[0] &= ~params->mask;
3179 break;
3180 case 2:
3181 ((u16 *)(&ucontrol->value.bytes.data))[0]
3182 &= cpu_to_be16(~params->mask);
3183 break;
3184 case 4:
3185 ((u32 *)(&ucontrol->value.bytes.data))[0]
3186 &= cpu_to_be32(~params->mask);
3187 break;
3188 default:
3189 return -EINVAL;
3193 return ret;
3195 EXPORT_SYMBOL_GPL(snd_soc_bytes_get);
3197 int snd_soc_bytes_put(struct snd_kcontrol *kcontrol,
3198 struct snd_ctl_elem_value *ucontrol)
3200 struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
3201 struct soc_bytes *params = (void *)kcontrol->private_value;
3202 int ret, len;
3203 unsigned int val, mask;
3204 void *data;
3206 if (!component->regmap)
3207 return -EINVAL;
3209 len = params->num_regs * component->val_bytes;
3211 data = kmemdup(ucontrol->value.bytes.data, len, GFP_KERNEL | GFP_DMA);
3212 if (!data)
3213 return -ENOMEM;
3216 * If we've got a mask then we need to preserve the register
3217 * bits. We shouldn't modify the incoming data so take a
3218 * copy.
3220 if (params->mask) {
3221 ret = regmap_read(component->regmap, params->base, &val);
3222 if (ret != 0)
3223 goto out;
3225 val &= params->mask;
3227 switch (component->val_bytes) {
3228 case 1:
3229 ((u8 *)data)[0] &= ~params->mask;
3230 ((u8 *)data)[0] |= val;
3231 break;
3232 case 2:
3233 mask = ~params->mask;
3234 ret = regmap_parse_val(component->regmap,
3235 &mask, &mask);
3236 if (ret != 0)
3237 goto out;
3239 ((u16 *)data)[0] &= mask;
3241 ret = regmap_parse_val(component->regmap,
3242 &val, &val);
3243 if (ret != 0)
3244 goto out;
3246 ((u16 *)data)[0] |= val;
3247 break;
3248 case 4:
3249 mask = ~params->mask;
3250 ret = regmap_parse_val(component->regmap,
3251 &mask, &mask);
3252 if (ret != 0)
3253 goto out;
3255 ((u32 *)data)[0] &= mask;
3257 ret = regmap_parse_val(component->regmap,
3258 &val, &val);
3259 if (ret != 0)
3260 goto out;
3262 ((u32 *)data)[0] |= val;
3263 break;
3264 default:
3265 ret = -EINVAL;
3266 goto out;
3270 ret = regmap_raw_write(component->regmap, params->base,
3271 data, len);
3273 out:
3274 kfree(data);
3276 return ret;
3278 EXPORT_SYMBOL_GPL(snd_soc_bytes_put);
3280 int snd_soc_bytes_info_ext(struct snd_kcontrol *kcontrol,
3281 struct snd_ctl_elem_info *ucontrol)
3283 struct soc_bytes_ext *params = (void *)kcontrol->private_value;
3285 ucontrol->type = SNDRV_CTL_ELEM_TYPE_BYTES;
3286 ucontrol->count = params->max;
3288 return 0;
3290 EXPORT_SYMBOL_GPL(snd_soc_bytes_info_ext);
3292 int snd_soc_bytes_tlv_callback(struct snd_kcontrol *kcontrol, int op_flag,
3293 unsigned int size, unsigned int __user *tlv)
3295 struct soc_bytes_ext *params = (void *)kcontrol->private_value;
3296 unsigned int count = size < params->max ? size : params->max;
3297 int ret = -ENXIO;
3299 switch (op_flag) {
3300 case SNDRV_CTL_TLV_OP_READ:
3301 if (params->get)
3302 ret = params->get(tlv, count);
3303 break;
3304 case SNDRV_CTL_TLV_OP_WRITE:
3305 if (params->put)
3306 ret = params->put(tlv, count);
3307 break;
3309 return ret;
3311 EXPORT_SYMBOL_GPL(snd_soc_bytes_tlv_callback);
3314 * snd_soc_info_xr_sx - signed multi register info callback
3315 * @kcontrol: mreg control
3316 * @uinfo: control element information
3318 * Callback to provide information of a control that can
3319 * span multiple codec registers which together
3320 * forms a single signed value in a MSB/LSB manner.
3322 * Returns 0 for success.
3324 int snd_soc_info_xr_sx(struct snd_kcontrol *kcontrol,
3325 struct snd_ctl_elem_info *uinfo)
3327 struct soc_mreg_control *mc =
3328 (struct soc_mreg_control *)kcontrol->private_value;
3329 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
3330 uinfo->count = 1;
3331 uinfo->value.integer.min = mc->min;
3332 uinfo->value.integer.max = mc->max;
3334 return 0;
3336 EXPORT_SYMBOL_GPL(snd_soc_info_xr_sx);
3339 * snd_soc_get_xr_sx - signed multi register get callback
3340 * @kcontrol: mreg control
3341 * @ucontrol: control element information
3343 * Callback to get the value of a control that can span
3344 * multiple codec registers which together forms a single
3345 * signed value in a MSB/LSB manner. The control supports
3346 * specifying total no of bits used to allow for bitfields
3347 * across the multiple codec registers.
3349 * Returns 0 for success.
3351 int snd_soc_get_xr_sx(struct snd_kcontrol *kcontrol,
3352 struct snd_ctl_elem_value *ucontrol)
3354 struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
3355 struct soc_mreg_control *mc =
3356 (struct soc_mreg_control *)kcontrol->private_value;
3357 unsigned int regbase = mc->regbase;
3358 unsigned int regcount = mc->regcount;
3359 unsigned int regwshift = component->val_bytes * BITS_PER_BYTE;
3360 unsigned int regwmask = (1<<regwshift)-1;
3361 unsigned int invert = mc->invert;
3362 unsigned long mask = (1UL<<mc->nbits)-1;
3363 long min = mc->min;
3364 long max = mc->max;
3365 long val = 0;
3366 unsigned int regval;
3367 unsigned int i;
3368 int ret;
3370 for (i = 0; i < regcount; i++) {
3371 ret = snd_soc_component_read(component, regbase+i, &regval);
3372 if (ret)
3373 return ret;
3374 val |= (regval & regwmask) << (regwshift*(regcount-i-1));
3376 val &= mask;
3377 if (min < 0 && val > max)
3378 val |= ~mask;
3379 if (invert)
3380 val = max - val;
3381 ucontrol->value.integer.value[0] = val;
3383 return 0;
3385 EXPORT_SYMBOL_GPL(snd_soc_get_xr_sx);
3388 * snd_soc_put_xr_sx - signed multi register get callback
3389 * @kcontrol: mreg control
3390 * @ucontrol: control element information
3392 * Callback to set 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.
3398 * Returns 0 for success.
3400 int snd_soc_put_xr_sx(struct snd_kcontrol *kcontrol,
3401 struct snd_ctl_elem_value *ucontrol)
3403 struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
3404 struct soc_mreg_control *mc =
3405 (struct soc_mreg_control *)kcontrol->private_value;
3406 unsigned int regbase = mc->regbase;
3407 unsigned int regcount = mc->regcount;
3408 unsigned int regwshift = component->val_bytes * 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 max = mc->max;
3413 long val = ucontrol->value.integer.value[0];
3414 unsigned int i, regval, regmask;
3415 int err;
3417 if (invert)
3418 val = max - val;
3419 val &= mask;
3420 for (i = 0; i < regcount; i++) {
3421 regval = (val >> (regwshift*(regcount-i-1))) & regwmask;
3422 regmask = (mask >> (regwshift*(regcount-i-1))) & regwmask;
3423 err = snd_soc_component_update_bits(component, regbase+i,
3424 regmask, regval);
3425 if (err < 0)
3426 return err;
3429 return 0;
3431 EXPORT_SYMBOL_GPL(snd_soc_put_xr_sx);
3434 * snd_soc_get_strobe - strobe get callback
3435 * @kcontrol: mixer control
3436 * @ucontrol: control element information
3438 * Callback get the value of a strobe mixer control.
3440 * Returns 0 for success.
3442 int snd_soc_get_strobe(struct snd_kcontrol *kcontrol,
3443 struct snd_ctl_elem_value *ucontrol)
3445 struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
3446 struct soc_mixer_control *mc =
3447 (struct soc_mixer_control *)kcontrol->private_value;
3448 unsigned int reg = mc->reg;
3449 unsigned int shift = mc->shift;
3450 unsigned int mask = 1 << shift;
3451 unsigned int invert = mc->invert != 0;
3452 unsigned int val;
3453 int ret;
3455 ret = snd_soc_component_read(component, reg, &val);
3456 if (ret)
3457 return ret;
3459 val &= mask;
3461 if (shift != 0 && val != 0)
3462 val = val >> shift;
3463 ucontrol->value.enumerated.item[0] = val ^ invert;
3465 return 0;
3467 EXPORT_SYMBOL_GPL(snd_soc_get_strobe);
3470 * snd_soc_put_strobe - strobe put callback
3471 * @kcontrol: mixer control
3472 * @ucontrol: control element information
3474 * Callback strobe a register bit to high then low (or the inverse)
3475 * in one pass of a single mixer enum control.
3477 * Returns 1 for success.
3479 int snd_soc_put_strobe(struct snd_kcontrol *kcontrol,
3480 struct snd_ctl_elem_value *ucontrol)
3482 struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
3483 struct soc_mixer_control *mc =
3484 (struct soc_mixer_control *)kcontrol->private_value;
3485 unsigned int reg = mc->reg;
3486 unsigned int shift = mc->shift;
3487 unsigned int mask = 1 << shift;
3488 unsigned int invert = mc->invert != 0;
3489 unsigned int strobe = ucontrol->value.enumerated.item[0] != 0;
3490 unsigned int val1 = (strobe ^ invert) ? mask : 0;
3491 unsigned int val2 = (strobe ^ invert) ? 0 : mask;
3492 int err;
3494 err = snd_soc_component_update_bits(component, reg, mask, val1);
3495 if (err < 0)
3496 return err;
3498 return snd_soc_component_update_bits(component, reg, mask, val2);
3500 EXPORT_SYMBOL_GPL(snd_soc_put_strobe);
3503 * snd_soc_dai_set_sysclk - configure DAI system or master clock.
3504 * @dai: DAI
3505 * @clk_id: DAI specific clock ID
3506 * @freq: new clock frequency in Hz
3507 * @dir: new clock direction - input/output.
3509 * Configures the DAI master (MCLK) or system (SYSCLK) clocking.
3511 int snd_soc_dai_set_sysclk(struct snd_soc_dai *dai, int clk_id,
3512 unsigned int freq, int dir)
3514 if (dai->driver && dai->driver->ops->set_sysclk)
3515 return dai->driver->ops->set_sysclk(dai, clk_id, freq, dir);
3516 else if (dai->codec && dai->codec->driver->set_sysclk)
3517 return dai->codec->driver->set_sysclk(dai->codec, clk_id, 0,
3518 freq, dir);
3519 else
3520 return -ENOTSUPP;
3522 EXPORT_SYMBOL_GPL(snd_soc_dai_set_sysclk);
3525 * snd_soc_codec_set_sysclk - configure CODEC system or master clock.
3526 * @codec: CODEC
3527 * @clk_id: DAI specific clock ID
3528 * @source: Source for the clock
3529 * @freq: new clock frequency in Hz
3530 * @dir: new clock direction - input/output.
3532 * Configures the CODEC master (MCLK) or system (SYSCLK) clocking.
3534 int snd_soc_codec_set_sysclk(struct snd_soc_codec *codec, int clk_id,
3535 int source, unsigned int freq, int dir)
3537 if (codec->driver->set_sysclk)
3538 return codec->driver->set_sysclk(codec, clk_id, source,
3539 freq, dir);
3540 else
3541 return -ENOTSUPP;
3543 EXPORT_SYMBOL_GPL(snd_soc_codec_set_sysclk);
3546 * snd_soc_dai_set_clkdiv - configure DAI clock dividers.
3547 * @dai: DAI
3548 * @div_id: DAI specific clock divider ID
3549 * @div: new clock divisor.
3551 * Configures the clock dividers. This is used to derive the best DAI bit and
3552 * frame clocks from the system or master clock. It's best to set the DAI bit
3553 * and frame clocks as low as possible to save system power.
3555 int snd_soc_dai_set_clkdiv(struct snd_soc_dai *dai,
3556 int div_id, int div)
3558 if (dai->driver && dai->driver->ops->set_clkdiv)
3559 return dai->driver->ops->set_clkdiv(dai, div_id, div);
3560 else
3561 return -EINVAL;
3563 EXPORT_SYMBOL_GPL(snd_soc_dai_set_clkdiv);
3566 * snd_soc_dai_set_pll - configure DAI PLL.
3567 * @dai: DAI
3568 * @pll_id: DAI specific PLL ID
3569 * @source: DAI specific source for the PLL
3570 * @freq_in: PLL input clock frequency in Hz
3571 * @freq_out: requested PLL output clock frequency in Hz
3573 * Configures and enables PLL to generate output clock based on input clock.
3575 int snd_soc_dai_set_pll(struct snd_soc_dai *dai, int pll_id, int source,
3576 unsigned int freq_in, unsigned int freq_out)
3578 if (dai->driver && dai->driver->ops->set_pll)
3579 return dai->driver->ops->set_pll(dai, pll_id, source,
3580 freq_in, freq_out);
3581 else if (dai->codec && dai->codec->driver->set_pll)
3582 return dai->codec->driver->set_pll(dai->codec, pll_id, source,
3583 freq_in, freq_out);
3584 else
3585 return -EINVAL;
3587 EXPORT_SYMBOL_GPL(snd_soc_dai_set_pll);
3590 * snd_soc_codec_set_pll - configure codec PLL.
3591 * @codec: CODEC
3592 * @pll_id: DAI specific PLL ID
3593 * @source: DAI specific source for the PLL
3594 * @freq_in: PLL input clock frequency in Hz
3595 * @freq_out: requested PLL output clock frequency in Hz
3597 * Configures and enables PLL to generate output clock based on input clock.
3599 int snd_soc_codec_set_pll(struct snd_soc_codec *codec, int pll_id, int source,
3600 unsigned int freq_in, unsigned int freq_out)
3602 if (codec->driver->set_pll)
3603 return codec->driver->set_pll(codec, pll_id, source,
3604 freq_in, freq_out);
3605 else
3606 return -EINVAL;
3608 EXPORT_SYMBOL_GPL(snd_soc_codec_set_pll);
3611 * snd_soc_dai_set_bclk_ratio - configure BCLK to sample rate ratio.
3612 * @dai: DAI
3613 * @ratio Ratio of BCLK to Sample rate.
3615 * Configures the DAI for a preset BCLK to sample rate ratio.
3617 int snd_soc_dai_set_bclk_ratio(struct snd_soc_dai *dai, unsigned int ratio)
3619 if (dai->driver && dai->driver->ops->set_bclk_ratio)
3620 return dai->driver->ops->set_bclk_ratio(dai, ratio);
3621 else
3622 return -EINVAL;
3624 EXPORT_SYMBOL_GPL(snd_soc_dai_set_bclk_ratio);
3627 * snd_soc_dai_set_fmt - configure DAI hardware audio format.
3628 * @dai: DAI
3629 * @fmt: SND_SOC_DAIFMT_ format value.
3631 * Configures the DAI hardware format and clocking.
3633 int snd_soc_dai_set_fmt(struct snd_soc_dai *dai, unsigned int fmt)
3635 if (dai->driver == NULL)
3636 return -EINVAL;
3637 if (dai->driver->ops->set_fmt == NULL)
3638 return -ENOTSUPP;
3639 return dai->driver->ops->set_fmt(dai, fmt);
3641 EXPORT_SYMBOL_GPL(snd_soc_dai_set_fmt);
3644 * snd_soc_xlate_tdm_slot - generate tx/rx slot mask.
3645 * @slots: Number of slots in use.
3646 * @tx_mask: bitmask representing active TX slots.
3647 * @rx_mask: bitmask representing active RX slots.
3649 * Generates the TDM tx and rx slot default masks for DAI.
3651 static int snd_soc_xlate_tdm_slot_mask(unsigned int slots,
3652 unsigned int *tx_mask,
3653 unsigned int *rx_mask)
3655 if (*tx_mask || *rx_mask)
3656 return 0;
3658 if (!slots)
3659 return -EINVAL;
3661 *tx_mask = (1 << slots) - 1;
3662 *rx_mask = (1 << slots) - 1;
3664 return 0;
3668 * snd_soc_dai_set_tdm_slot - configure DAI TDM.
3669 * @dai: DAI
3670 * @tx_mask: bitmask representing active TX slots.
3671 * @rx_mask: bitmask representing active RX slots.
3672 * @slots: Number of slots in use.
3673 * @slot_width: Width in bits for each slot.
3675 * Configures a DAI for TDM operation. Both mask and slots are codec and DAI
3676 * specific.
3678 int snd_soc_dai_set_tdm_slot(struct snd_soc_dai *dai,
3679 unsigned int tx_mask, unsigned int rx_mask, int slots, int slot_width)
3681 if (dai->driver && dai->driver->ops->xlate_tdm_slot_mask)
3682 dai->driver->ops->xlate_tdm_slot_mask(slots,
3683 &tx_mask, &rx_mask);
3684 else
3685 snd_soc_xlate_tdm_slot_mask(slots, &tx_mask, &rx_mask);
3687 dai->tx_mask = tx_mask;
3688 dai->rx_mask = rx_mask;
3690 if (dai->driver && dai->driver->ops->set_tdm_slot)
3691 return dai->driver->ops->set_tdm_slot(dai, tx_mask, rx_mask,
3692 slots, slot_width);
3693 else
3694 return -ENOTSUPP;
3696 EXPORT_SYMBOL_GPL(snd_soc_dai_set_tdm_slot);
3699 * snd_soc_dai_set_channel_map - configure DAI audio channel map
3700 * @dai: DAI
3701 * @tx_num: how many TX channels
3702 * @tx_slot: pointer to an array which imply the TX slot number channel
3703 * 0~num-1 uses
3704 * @rx_num: how many RX channels
3705 * @rx_slot: pointer to an array which imply the RX slot number channel
3706 * 0~num-1 uses
3708 * configure the relationship between channel number and TDM slot number.
3710 int snd_soc_dai_set_channel_map(struct snd_soc_dai *dai,
3711 unsigned int tx_num, unsigned int *tx_slot,
3712 unsigned int rx_num, unsigned int *rx_slot)
3714 if (dai->driver && dai->driver->ops->set_channel_map)
3715 return dai->driver->ops->set_channel_map(dai, tx_num, tx_slot,
3716 rx_num, rx_slot);
3717 else
3718 return -EINVAL;
3720 EXPORT_SYMBOL_GPL(snd_soc_dai_set_channel_map);
3723 * snd_soc_dai_set_tristate - configure DAI system or master clock.
3724 * @dai: DAI
3725 * @tristate: tristate enable
3727 * Tristates the DAI so that others can use it.
3729 int snd_soc_dai_set_tristate(struct snd_soc_dai *dai, int tristate)
3731 if (dai->driver && dai->driver->ops->set_tristate)
3732 return dai->driver->ops->set_tristate(dai, tristate);
3733 else
3734 return -EINVAL;
3736 EXPORT_SYMBOL_GPL(snd_soc_dai_set_tristate);
3739 * snd_soc_dai_digital_mute - configure DAI system or master clock.
3740 * @dai: DAI
3741 * @mute: mute enable
3742 * @direction: stream to mute
3744 * Mutes the DAI DAC.
3746 int snd_soc_dai_digital_mute(struct snd_soc_dai *dai, int mute,
3747 int direction)
3749 if (!dai->driver)
3750 return -ENOTSUPP;
3752 if (dai->driver->ops->mute_stream)
3753 return dai->driver->ops->mute_stream(dai, mute, direction);
3754 else if (direction == SNDRV_PCM_STREAM_PLAYBACK &&
3755 dai->driver->ops->digital_mute)
3756 return dai->driver->ops->digital_mute(dai, mute);
3757 else
3758 return -ENOTSUPP;
3760 EXPORT_SYMBOL_GPL(snd_soc_dai_digital_mute);
3762 static int snd_soc_init_multicodec(struct snd_soc_card *card,
3763 struct snd_soc_dai_link *dai_link)
3765 /* Legacy codec/codec_dai link is a single entry in multicodec */
3766 if (dai_link->codec_name || dai_link->codec_of_node ||
3767 dai_link->codec_dai_name) {
3768 dai_link->num_codecs = 1;
3770 dai_link->codecs = devm_kzalloc(card->dev,
3771 sizeof(struct snd_soc_dai_link_component),
3772 GFP_KERNEL);
3773 if (!dai_link->codecs)
3774 return -ENOMEM;
3776 dai_link->codecs[0].name = dai_link->codec_name;
3777 dai_link->codecs[0].of_node = dai_link->codec_of_node;
3778 dai_link->codecs[0].dai_name = dai_link->codec_dai_name;
3781 if (!dai_link->codecs) {
3782 dev_err(card->dev, "ASoC: DAI link has no CODECs\n");
3783 return -EINVAL;
3786 return 0;
3790 * snd_soc_register_card - Register a card with the ASoC core
3792 * @card: Card to register
3795 int snd_soc_register_card(struct snd_soc_card *card)
3797 int i, j, ret;
3799 if (!card->name || !card->dev)
3800 return -EINVAL;
3802 for (i = 0; i < card->num_links; i++) {
3803 struct snd_soc_dai_link *link = &card->dai_link[i];
3805 ret = snd_soc_init_multicodec(card, link);
3806 if (ret) {
3807 dev_err(card->dev, "ASoC: failed to init multicodec\n");
3808 return ret;
3811 for (j = 0; j < link->num_codecs; j++) {
3813 * Codec must be specified by 1 of name or OF node,
3814 * not both or neither.
3816 if (!!link->codecs[j].name ==
3817 !!link->codecs[j].of_node) {
3818 dev_err(card->dev, "ASoC: Neither/both codec name/of_node are set for %s\n",
3819 link->name);
3820 return -EINVAL;
3822 /* Codec DAI name must be specified */
3823 if (!link->codecs[j].dai_name) {
3824 dev_err(card->dev, "ASoC: codec_dai_name not set for %s\n",
3825 link->name);
3826 return -EINVAL;
3831 * Platform may be specified by either name or OF node, but
3832 * can be left unspecified, and a dummy platform will be used.
3834 if (link->platform_name && link->platform_of_node) {
3835 dev_err(card->dev,
3836 "ASoC: Both platform name/of_node are set for %s\n",
3837 link->name);
3838 return -EINVAL;
3842 * CPU device may be specified by either name or OF node, but
3843 * can be left unspecified, and will be matched based on DAI
3844 * name alone..
3846 if (link->cpu_name && link->cpu_of_node) {
3847 dev_err(card->dev,
3848 "ASoC: Neither/both cpu name/of_node are set for %s\n",
3849 link->name);
3850 return -EINVAL;
3853 * At least one of CPU DAI name or CPU device name/node must be
3854 * specified
3856 if (!link->cpu_dai_name &&
3857 !(link->cpu_name || link->cpu_of_node)) {
3858 dev_err(card->dev,
3859 "ASoC: Neither cpu_dai_name nor cpu_name/of_node are set for %s\n",
3860 link->name);
3861 return -EINVAL;
3865 dev_set_drvdata(card->dev, card);
3867 snd_soc_initialize_card_lists(card);
3869 soc_init_card_debugfs(card);
3871 card->rtd = devm_kzalloc(card->dev,
3872 sizeof(struct snd_soc_pcm_runtime) *
3873 (card->num_links + card->num_aux_devs),
3874 GFP_KERNEL);
3875 if (card->rtd == NULL)
3876 return -ENOMEM;
3877 card->num_rtd = 0;
3878 card->rtd_aux = &card->rtd[card->num_links];
3880 for (i = 0; i < card->num_links; i++) {
3881 card->rtd[i].card = card;
3882 card->rtd[i].dai_link = &card->dai_link[i];
3883 card->rtd[i].codec_dais = devm_kzalloc(card->dev,
3884 sizeof(struct snd_soc_dai *) *
3885 (card->rtd[i].dai_link->num_codecs),
3886 GFP_KERNEL);
3887 if (card->rtd[i].codec_dais == NULL)
3888 return -ENOMEM;
3891 for (i = 0; i < card->num_aux_devs; i++)
3892 card->rtd_aux[i].card = card;
3894 INIT_LIST_HEAD(&card->dapm_dirty);
3895 card->instantiated = 0;
3896 mutex_init(&card->mutex);
3897 mutex_init(&card->dapm_mutex);
3899 ret = snd_soc_instantiate_card(card);
3900 if (ret != 0)
3901 soc_cleanup_card_debugfs(card);
3903 /* deactivate pins to sleep state */
3904 for (i = 0; i < card->num_rtd; i++) {
3905 struct snd_soc_pcm_runtime *rtd = &card->rtd[i];
3906 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
3907 int j;
3909 for (j = 0; j < rtd->num_codecs; j++) {
3910 struct snd_soc_dai *codec_dai = rtd->codec_dais[j];
3911 if (!codec_dai->active)
3912 pinctrl_pm_select_sleep_state(codec_dai->dev);
3915 if (!cpu_dai->active)
3916 pinctrl_pm_select_sleep_state(cpu_dai->dev);
3919 return ret;
3921 EXPORT_SYMBOL_GPL(snd_soc_register_card);
3924 * snd_soc_unregister_card - Unregister a card with the ASoC core
3926 * @card: Card to unregister
3929 int snd_soc_unregister_card(struct snd_soc_card *card)
3931 if (card->instantiated)
3932 soc_cleanup_card_resources(card);
3933 dev_dbg(card->dev, "ASoC: Unregistered card '%s'\n", card->name);
3935 return 0;
3937 EXPORT_SYMBOL_GPL(snd_soc_unregister_card);
3940 * Simplify DAI link configuration by removing ".-1" from device names
3941 * and sanitizing names.
3943 static char *fmt_single_name(struct device *dev, int *id)
3945 char *found, name[NAME_SIZE];
3946 int id1, id2;
3948 if (dev_name(dev) == NULL)
3949 return NULL;
3951 strlcpy(name, dev_name(dev), NAME_SIZE);
3953 /* are we a "%s.%d" name (platform and SPI components) */
3954 found = strstr(name, dev->driver->name);
3955 if (found) {
3956 /* get ID */
3957 if (sscanf(&found[strlen(dev->driver->name)], ".%d", id) == 1) {
3959 /* discard ID from name if ID == -1 */
3960 if (*id == -1)
3961 found[strlen(dev->driver->name)] = '\0';
3964 } else {
3965 /* I2C component devices are named "bus-addr" */
3966 if (sscanf(name, "%x-%x", &id1, &id2) == 2) {
3967 char tmp[NAME_SIZE];
3969 /* create unique ID number from I2C addr and bus */
3970 *id = ((id1 & 0xffff) << 16) + id2;
3972 /* sanitize component name for DAI link creation */
3973 snprintf(tmp, NAME_SIZE, "%s.%s", dev->driver->name, name);
3974 strlcpy(name, tmp, NAME_SIZE);
3975 } else
3976 *id = 0;
3979 return kstrdup(name, GFP_KERNEL);
3983 * Simplify DAI link naming for single devices with multiple DAIs by removing
3984 * any ".-1" and using the DAI name (instead of device name).
3986 static inline char *fmt_multiple_name(struct device *dev,
3987 struct snd_soc_dai_driver *dai_drv)
3989 if (dai_drv->name == NULL) {
3990 dev_err(dev,
3991 "ASoC: error - multiple DAI %s registered with no name\n",
3992 dev_name(dev));
3993 return NULL;
3996 return kstrdup(dai_drv->name, GFP_KERNEL);
4000 * snd_soc_unregister_dai - Unregister DAIs from the ASoC core
4002 * @component: The component for which the DAIs should be unregistered
4004 static void snd_soc_unregister_dais(struct snd_soc_component *component)
4006 struct snd_soc_dai *dai, *_dai;
4008 list_for_each_entry_safe(dai, _dai, &component->dai_list, list) {
4009 dev_dbg(component->dev, "ASoC: Unregistered DAI '%s'\n",
4010 dai->name);
4011 list_del(&dai->list);
4012 kfree(dai->name);
4013 kfree(dai);
4018 * snd_soc_register_dais - Register a DAI with the ASoC core
4020 * @component: The component the DAIs are registered for
4021 * @dai_drv: DAI driver to use for the DAIs
4022 * @count: Number of DAIs
4023 * @legacy_dai_naming: Use the legacy naming scheme and let the DAI inherit the
4024 * parent's name.
4026 static int snd_soc_register_dais(struct snd_soc_component *component,
4027 struct snd_soc_dai_driver *dai_drv, size_t count,
4028 bool legacy_dai_naming)
4030 struct device *dev = component->dev;
4031 struct snd_soc_dai *dai;
4032 unsigned int i;
4033 int ret;
4035 dev_dbg(dev, "ASoC: dai register %s #%Zu\n", dev_name(dev), count);
4037 component->dai_drv = dai_drv;
4038 component->num_dai = count;
4040 for (i = 0; i < count; i++) {
4042 dai = kzalloc(sizeof(struct snd_soc_dai), GFP_KERNEL);
4043 if (dai == NULL) {
4044 ret = -ENOMEM;
4045 goto err;
4049 * Back in the old days when we still had component-less DAIs,
4050 * instead of having a static name, component-less DAIs would
4051 * inherit the name of the parent device so it is possible to
4052 * register multiple instances of the DAI. We still need to keep
4053 * the same naming style even though those DAIs are not
4054 * component-less anymore.
4056 if (count == 1 && legacy_dai_naming) {
4057 dai->name = fmt_single_name(dev, &dai->id);
4058 } else {
4059 dai->name = fmt_multiple_name(dev, &dai_drv[i]);
4060 if (dai_drv[i].id)
4061 dai->id = dai_drv[i].id;
4062 else
4063 dai->id = i;
4065 if (dai->name == NULL) {
4066 kfree(dai);
4067 ret = -ENOMEM;
4068 goto err;
4071 dai->component = component;
4072 dai->dev = dev;
4073 dai->driver = &dai_drv[i];
4074 if (!dai->driver->ops)
4075 dai->driver->ops = &null_dai_ops;
4077 list_add(&dai->list, &component->dai_list);
4079 dev_dbg(dev, "ASoC: Registered DAI '%s'\n", dai->name);
4082 return 0;
4084 err:
4085 snd_soc_unregister_dais(component);
4087 return ret;
4090 static void snd_soc_component_seq_notifier(struct snd_soc_dapm_context *dapm,
4091 enum snd_soc_dapm_type type, int subseq)
4093 struct snd_soc_component *component = dapm->component;
4095 component->driver->seq_notifier(component, type, subseq);
4098 static int snd_soc_component_stream_event(struct snd_soc_dapm_context *dapm,
4099 int event)
4101 struct snd_soc_component *component = dapm->component;
4103 return component->driver->stream_event(component, event);
4106 static int snd_soc_component_initialize(struct snd_soc_component *component,
4107 const struct snd_soc_component_driver *driver, struct device *dev)
4109 struct snd_soc_dapm_context *dapm;
4111 component->name = fmt_single_name(dev, &component->id);
4112 if (!component->name) {
4113 dev_err(dev, "ASoC: Failed to allocate name\n");
4114 return -ENOMEM;
4117 component->dev = dev;
4118 component->driver = driver;
4120 if (!component->dapm_ptr)
4121 component->dapm_ptr = &component->dapm;
4123 dapm = component->dapm_ptr;
4124 dapm->dev = dev;
4125 dapm->component = component;
4126 dapm->bias_level = SND_SOC_BIAS_OFF;
4127 if (driver->seq_notifier)
4128 dapm->seq_notifier = snd_soc_component_seq_notifier;
4129 if (driver->stream_event)
4130 dapm->stream_event = snd_soc_component_stream_event;
4132 INIT_LIST_HEAD(&component->dai_list);
4133 mutex_init(&component->io_mutex);
4135 return 0;
4138 static void snd_soc_component_add_unlocked(struct snd_soc_component *component)
4140 list_add(&component->list, &component_list);
4143 static void snd_soc_component_add(struct snd_soc_component *component)
4145 mutex_lock(&client_mutex);
4146 snd_soc_component_add_unlocked(component);
4147 mutex_unlock(&client_mutex);
4150 static void snd_soc_component_cleanup(struct snd_soc_component *component)
4152 snd_soc_unregister_dais(component);
4153 kfree(component->name);
4156 static void snd_soc_component_del_unlocked(struct snd_soc_component *component)
4158 list_del(&component->list);
4161 static void snd_soc_component_del(struct snd_soc_component *component)
4163 mutex_lock(&client_mutex);
4164 snd_soc_component_del_unlocked(component);
4165 mutex_unlock(&client_mutex);
4168 int snd_soc_register_component(struct device *dev,
4169 const struct snd_soc_component_driver *cmpnt_drv,
4170 struct snd_soc_dai_driver *dai_drv,
4171 int num_dai)
4173 struct snd_soc_component *cmpnt;
4174 int ret;
4176 cmpnt = kzalloc(sizeof(*cmpnt), GFP_KERNEL);
4177 if (!cmpnt) {
4178 dev_err(dev, "ASoC: Failed to allocate memory\n");
4179 return -ENOMEM;
4182 ret = snd_soc_component_initialize(cmpnt, cmpnt_drv, dev);
4183 if (ret)
4184 goto err_free;
4186 cmpnt->ignore_pmdown_time = true;
4187 cmpnt->registered_as_component = true;
4189 ret = snd_soc_register_dais(cmpnt, dai_drv, num_dai, true);
4190 if (ret < 0) {
4191 dev_err(dev, "ASoC: Failed to regster DAIs: %d\n", ret);
4192 goto err_cleanup;
4195 snd_soc_component_add(cmpnt);
4197 return 0;
4199 err_cleanup:
4200 snd_soc_component_cleanup(cmpnt);
4201 err_free:
4202 kfree(cmpnt);
4203 return ret;
4205 EXPORT_SYMBOL_GPL(snd_soc_register_component);
4208 * snd_soc_unregister_component - Unregister a component from the ASoC core
4211 void snd_soc_unregister_component(struct device *dev)
4213 struct snd_soc_component *cmpnt;
4215 list_for_each_entry(cmpnt, &component_list, list) {
4216 if (dev == cmpnt->dev && cmpnt->registered_as_component)
4217 goto found;
4219 return;
4221 found:
4222 snd_soc_component_del(cmpnt);
4223 snd_soc_component_cleanup(cmpnt);
4224 kfree(cmpnt);
4226 EXPORT_SYMBOL_GPL(snd_soc_unregister_component);
4228 static int snd_soc_platform_drv_write(struct snd_soc_component *component,
4229 unsigned int reg, unsigned int val)
4231 struct snd_soc_platform *platform = snd_soc_component_to_platform(component);
4233 return platform->driver->write(platform, reg, val);
4236 static int snd_soc_platform_drv_read(struct snd_soc_component *component,
4237 unsigned int reg, unsigned int *val)
4239 struct snd_soc_platform *platform = snd_soc_component_to_platform(component);
4241 *val = platform->driver->read(platform, reg);
4243 return 0;
4247 * snd_soc_add_platform - Add a platform to the ASoC core
4248 * @dev: The parent device for the platform
4249 * @platform: The platform to add
4250 * @platform_driver: The driver for the platform
4252 int snd_soc_add_platform(struct device *dev, struct snd_soc_platform *platform,
4253 const struct snd_soc_platform_driver *platform_drv)
4255 int ret;
4257 ret = snd_soc_component_initialize(&platform->component,
4258 &platform_drv->component_driver, dev);
4259 if (ret)
4260 return ret;
4262 platform->dev = dev;
4263 platform->driver = platform_drv;
4264 if (platform_drv->write)
4265 platform->component.write = snd_soc_platform_drv_write;
4266 if (platform_drv->read)
4267 platform->component.read = snd_soc_platform_drv_read;
4269 mutex_lock(&client_mutex);
4270 snd_soc_component_add_unlocked(&platform->component);
4271 list_add(&platform->list, &platform_list);
4272 mutex_unlock(&client_mutex);
4274 dev_dbg(dev, "ASoC: Registered platform '%s'\n",
4275 platform->component.name);
4277 return 0;
4279 EXPORT_SYMBOL_GPL(snd_soc_add_platform);
4282 * snd_soc_register_platform - Register a platform with the ASoC core
4284 * @platform: platform to register
4286 int snd_soc_register_platform(struct device *dev,
4287 const struct snd_soc_platform_driver *platform_drv)
4289 struct snd_soc_platform *platform;
4290 int ret;
4292 dev_dbg(dev, "ASoC: platform register %s\n", dev_name(dev));
4294 platform = kzalloc(sizeof(struct snd_soc_platform), GFP_KERNEL);
4295 if (platform == NULL)
4296 return -ENOMEM;
4298 ret = snd_soc_add_platform(dev, platform, platform_drv);
4299 if (ret)
4300 kfree(platform);
4302 return ret;
4304 EXPORT_SYMBOL_GPL(snd_soc_register_platform);
4307 * snd_soc_remove_platform - Remove a platform from the ASoC core
4308 * @platform: the platform to remove
4310 void snd_soc_remove_platform(struct snd_soc_platform *platform)
4313 mutex_lock(&client_mutex);
4314 list_del(&platform->list);
4315 snd_soc_component_del_unlocked(&platform->component);
4316 mutex_unlock(&client_mutex);
4318 snd_soc_component_cleanup(&platform->component);
4320 dev_dbg(platform->dev, "ASoC: Unregistered platform '%s'\n",
4321 platform->component.name);
4323 EXPORT_SYMBOL_GPL(snd_soc_remove_platform);
4325 struct snd_soc_platform *snd_soc_lookup_platform(struct device *dev)
4327 struct snd_soc_platform *platform;
4329 list_for_each_entry(platform, &platform_list, list) {
4330 if (dev == platform->dev)
4331 return platform;
4334 return NULL;
4336 EXPORT_SYMBOL_GPL(snd_soc_lookup_platform);
4339 * snd_soc_unregister_platform - Unregister a platform from the ASoC core
4341 * @platform: platform to unregister
4343 void snd_soc_unregister_platform(struct device *dev)
4345 struct snd_soc_platform *platform;
4347 platform = snd_soc_lookup_platform(dev);
4348 if (!platform)
4349 return;
4351 snd_soc_remove_platform(platform);
4352 kfree(platform);
4354 EXPORT_SYMBOL_GPL(snd_soc_unregister_platform);
4356 static u64 codec_format_map[] = {
4357 SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S16_BE,
4358 SNDRV_PCM_FMTBIT_U16_LE | SNDRV_PCM_FMTBIT_U16_BE,
4359 SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S24_BE,
4360 SNDRV_PCM_FMTBIT_U24_LE | SNDRV_PCM_FMTBIT_U24_BE,
4361 SNDRV_PCM_FMTBIT_S32_LE | SNDRV_PCM_FMTBIT_S32_BE,
4362 SNDRV_PCM_FMTBIT_U32_LE | SNDRV_PCM_FMTBIT_U32_BE,
4363 SNDRV_PCM_FMTBIT_S24_3LE | SNDRV_PCM_FMTBIT_U24_3BE,
4364 SNDRV_PCM_FMTBIT_U24_3LE | SNDRV_PCM_FMTBIT_U24_3BE,
4365 SNDRV_PCM_FMTBIT_S20_3LE | SNDRV_PCM_FMTBIT_S20_3BE,
4366 SNDRV_PCM_FMTBIT_U20_3LE | SNDRV_PCM_FMTBIT_U20_3BE,
4367 SNDRV_PCM_FMTBIT_S18_3LE | SNDRV_PCM_FMTBIT_S18_3BE,
4368 SNDRV_PCM_FMTBIT_U18_3LE | SNDRV_PCM_FMTBIT_U18_3BE,
4369 SNDRV_PCM_FMTBIT_FLOAT_LE | SNDRV_PCM_FMTBIT_FLOAT_BE,
4370 SNDRV_PCM_FMTBIT_FLOAT64_LE | SNDRV_PCM_FMTBIT_FLOAT64_BE,
4371 SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_LE
4372 | SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_BE,
4375 /* Fix up the DAI formats for endianness: codecs don't actually see
4376 * the endianness of the data but we're using the CPU format
4377 * definitions which do need to include endianness so we ensure that
4378 * codec DAIs always have both big and little endian variants set.
4380 static void fixup_codec_formats(struct snd_soc_pcm_stream *stream)
4382 int i;
4384 for (i = 0; i < ARRAY_SIZE(codec_format_map); i++)
4385 if (stream->formats & codec_format_map[i])
4386 stream->formats |= codec_format_map[i];
4389 static int snd_soc_codec_drv_write(struct snd_soc_component *component,
4390 unsigned int reg, unsigned int val)
4392 struct snd_soc_codec *codec = snd_soc_component_to_codec(component);
4394 return codec->driver->write(codec, reg, val);
4397 static int snd_soc_codec_drv_read(struct snd_soc_component *component,
4398 unsigned int reg, unsigned int *val)
4400 struct snd_soc_codec *codec = snd_soc_component_to_codec(component);
4402 *val = codec->driver->read(codec, reg);
4404 return 0;
4407 static int snd_soc_codec_set_bias_level(struct snd_soc_dapm_context *dapm,
4408 enum snd_soc_bias_level level)
4410 struct snd_soc_codec *codec = snd_soc_dapm_to_codec(dapm);
4412 return codec->driver->set_bias_level(codec, level);
4416 * snd_soc_register_codec - Register a codec with the ASoC core
4418 * @codec: codec to register
4420 int snd_soc_register_codec(struct device *dev,
4421 const struct snd_soc_codec_driver *codec_drv,
4422 struct snd_soc_dai_driver *dai_drv,
4423 int num_dai)
4425 struct snd_soc_codec *codec;
4426 struct snd_soc_dai *dai;
4427 struct regmap *regmap;
4428 int ret, i;
4430 dev_dbg(dev, "codec register %s\n", dev_name(dev));
4432 codec = kzalloc(sizeof(struct snd_soc_codec), GFP_KERNEL);
4433 if (codec == NULL)
4434 return -ENOMEM;
4436 codec->component.dapm_ptr = &codec->dapm;
4438 ret = snd_soc_component_initialize(&codec->component,
4439 &codec_drv->component_driver, dev);
4440 if (ret)
4441 goto err_free;
4443 if (codec_drv->write)
4444 codec->component.write = snd_soc_codec_drv_write;
4445 if (codec_drv->read)
4446 codec->component.read = snd_soc_codec_drv_read;
4447 codec->component.ignore_pmdown_time = codec_drv->ignore_pmdown_time;
4448 codec->dapm.codec = codec;
4449 if (codec_drv->seq_notifier)
4450 codec->dapm.seq_notifier = codec_drv->seq_notifier;
4451 if (codec_drv->set_bias_level)
4452 codec->dapm.set_bias_level = snd_soc_codec_set_bias_level;
4453 codec->dev = dev;
4454 codec->driver = codec_drv;
4455 codec->component.val_bytes = codec_drv->reg_word_size;
4456 mutex_init(&codec->mutex);
4458 if (!codec->component.write) {
4459 if (codec_drv->get_regmap)
4460 regmap = codec_drv->get_regmap(dev);
4461 else
4462 regmap = dev_get_regmap(dev, NULL);
4464 if (regmap) {
4465 ret = snd_soc_component_init_io(&codec->component,
4466 regmap);
4467 if (ret) {
4468 dev_err(codec->dev,
4469 "Failed to set cache I/O:%d\n",
4470 ret);
4471 goto err_cleanup;
4476 for (i = 0; i < num_dai; i++) {
4477 fixup_codec_formats(&dai_drv[i].playback);
4478 fixup_codec_formats(&dai_drv[i].capture);
4481 ret = snd_soc_register_dais(&codec->component, dai_drv, num_dai, false);
4482 if (ret < 0) {
4483 dev_err(dev, "ASoC: Failed to regster DAIs: %d\n", ret);
4484 goto err_cleanup;
4487 list_for_each_entry(dai, &codec->component.dai_list, list)
4488 dai->codec = codec;
4490 mutex_lock(&client_mutex);
4491 snd_soc_component_add_unlocked(&codec->component);
4492 list_add(&codec->list, &codec_list);
4493 mutex_unlock(&client_mutex);
4495 dev_dbg(codec->dev, "ASoC: Registered codec '%s'\n",
4496 codec->component.name);
4497 return 0;
4499 err_cleanup:
4500 snd_soc_component_cleanup(&codec->component);
4501 err_free:
4502 kfree(codec);
4503 return ret;
4505 EXPORT_SYMBOL_GPL(snd_soc_register_codec);
4508 * snd_soc_unregister_codec - Unregister a codec from the ASoC core
4510 * @codec: codec to unregister
4512 void snd_soc_unregister_codec(struct device *dev)
4514 struct snd_soc_codec *codec;
4516 list_for_each_entry(codec, &codec_list, list) {
4517 if (dev == codec->dev)
4518 goto found;
4520 return;
4522 found:
4524 mutex_lock(&client_mutex);
4525 list_del(&codec->list);
4526 snd_soc_component_del_unlocked(&codec->component);
4527 mutex_unlock(&client_mutex);
4529 dev_dbg(codec->dev, "ASoC: Unregistered codec '%s'\n",
4530 codec->component.name);
4532 snd_soc_component_cleanup(&codec->component);
4533 snd_soc_cache_exit(codec);
4534 kfree(codec);
4536 EXPORT_SYMBOL_GPL(snd_soc_unregister_codec);
4538 /* Retrieve a card's name from device tree */
4539 int snd_soc_of_parse_card_name(struct snd_soc_card *card,
4540 const char *propname)
4542 struct device_node *np;
4543 int ret;
4545 if (!card->dev) {
4546 pr_err("card->dev is not set before calling %s\n", __func__);
4547 return -EINVAL;
4550 np = card->dev->of_node;
4552 ret = of_property_read_string_index(np, propname, 0, &card->name);
4554 * EINVAL means the property does not exist. This is fine providing
4555 * card->name was previously set, which is checked later in
4556 * snd_soc_register_card.
4558 if (ret < 0 && ret != -EINVAL) {
4559 dev_err(card->dev,
4560 "ASoC: Property '%s' could not be read: %d\n",
4561 propname, ret);
4562 return ret;
4565 return 0;
4567 EXPORT_SYMBOL_GPL(snd_soc_of_parse_card_name);
4569 static const struct snd_soc_dapm_widget simple_widgets[] = {
4570 SND_SOC_DAPM_MIC("Microphone", NULL),
4571 SND_SOC_DAPM_LINE("Line", NULL),
4572 SND_SOC_DAPM_HP("Headphone", NULL),
4573 SND_SOC_DAPM_SPK("Speaker", NULL),
4576 int snd_soc_of_parse_audio_simple_widgets(struct snd_soc_card *card,
4577 const char *propname)
4579 struct device_node *np = card->dev->of_node;
4580 struct snd_soc_dapm_widget *widgets;
4581 const char *template, *wname;
4582 int i, j, num_widgets, ret;
4584 num_widgets = of_property_count_strings(np, propname);
4585 if (num_widgets < 0) {
4586 dev_err(card->dev,
4587 "ASoC: Property '%s' does not exist\n", propname);
4588 return -EINVAL;
4590 if (num_widgets & 1) {
4591 dev_err(card->dev,
4592 "ASoC: Property '%s' length is not even\n", propname);
4593 return -EINVAL;
4596 num_widgets /= 2;
4597 if (!num_widgets) {
4598 dev_err(card->dev, "ASoC: Property '%s's length is zero\n",
4599 propname);
4600 return -EINVAL;
4603 widgets = devm_kcalloc(card->dev, num_widgets, sizeof(*widgets),
4604 GFP_KERNEL);
4605 if (!widgets) {
4606 dev_err(card->dev,
4607 "ASoC: Could not allocate memory for widgets\n");
4608 return -ENOMEM;
4611 for (i = 0; i < num_widgets; i++) {
4612 ret = of_property_read_string_index(np, propname,
4613 2 * i, &template);
4614 if (ret) {
4615 dev_err(card->dev,
4616 "ASoC: Property '%s' index %d read error:%d\n",
4617 propname, 2 * i, ret);
4618 return -EINVAL;
4621 for (j = 0; j < ARRAY_SIZE(simple_widgets); j++) {
4622 if (!strncmp(template, simple_widgets[j].name,
4623 strlen(simple_widgets[j].name))) {
4624 widgets[i] = simple_widgets[j];
4625 break;
4629 if (j >= ARRAY_SIZE(simple_widgets)) {
4630 dev_err(card->dev,
4631 "ASoC: DAPM widget '%s' is not supported\n",
4632 template);
4633 return -EINVAL;
4636 ret = of_property_read_string_index(np, propname,
4637 (2 * i) + 1,
4638 &wname);
4639 if (ret) {
4640 dev_err(card->dev,
4641 "ASoC: Property '%s' index %d read error:%d\n",
4642 propname, (2 * i) + 1, ret);
4643 return -EINVAL;
4646 widgets[i].name = wname;
4649 card->dapm_widgets = widgets;
4650 card->num_dapm_widgets = num_widgets;
4652 return 0;
4654 EXPORT_SYMBOL_GPL(snd_soc_of_parse_audio_simple_widgets);
4656 int snd_soc_of_parse_tdm_slot(struct device_node *np,
4657 unsigned int *slots,
4658 unsigned int *slot_width)
4660 u32 val;
4661 int ret;
4663 if (of_property_read_bool(np, "dai-tdm-slot-num")) {
4664 ret = of_property_read_u32(np, "dai-tdm-slot-num", &val);
4665 if (ret)
4666 return ret;
4668 if (slots)
4669 *slots = val;
4672 if (of_property_read_bool(np, "dai-tdm-slot-width")) {
4673 ret = of_property_read_u32(np, "dai-tdm-slot-width", &val);
4674 if (ret)
4675 return ret;
4677 if (slot_width)
4678 *slot_width = val;
4681 return 0;
4683 EXPORT_SYMBOL_GPL(snd_soc_of_parse_tdm_slot);
4685 int snd_soc_of_parse_audio_routing(struct snd_soc_card *card,
4686 const char *propname)
4688 struct device_node *np = card->dev->of_node;
4689 int num_routes;
4690 struct snd_soc_dapm_route *routes;
4691 int i, ret;
4693 num_routes = of_property_count_strings(np, propname);
4694 if (num_routes < 0 || num_routes & 1) {
4695 dev_err(card->dev,
4696 "ASoC: Property '%s' does not exist or its length is not even\n",
4697 propname);
4698 return -EINVAL;
4700 num_routes /= 2;
4701 if (!num_routes) {
4702 dev_err(card->dev, "ASoC: Property '%s's length is zero\n",
4703 propname);
4704 return -EINVAL;
4707 routes = devm_kzalloc(card->dev, num_routes * sizeof(*routes),
4708 GFP_KERNEL);
4709 if (!routes) {
4710 dev_err(card->dev,
4711 "ASoC: Could not allocate DAPM route table\n");
4712 return -EINVAL;
4715 for (i = 0; i < num_routes; i++) {
4716 ret = of_property_read_string_index(np, propname,
4717 2 * i, &routes[i].sink);
4718 if (ret) {
4719 dev_err(card->dev,
4720 "ASoC: Property '%s' index %d could not be read: %d\n",
4721 propname, 2 * i, ret);
4722 return -EINVAL;
4724 ret = of_property_read_string_index(np, propname,
4725 (2 * i) + 1, &routes[i].source);
4726 if (ret) {
4727 dev_err(card->dev,
4728 "ASoC: Property '%s' index %d could not be read: %d\n",
4729 propname, (2 * i) + 1, ret);
4730 return -EINVAL;
4734 card->num_dapm_routes = num_routes;
4735 card->dapm_routes = routes;
4737 return 0;
4739 EXPORT_SYMBOL_GPL(snd_soc_of_parse_audio_routing);
4741 unsigned int snd_soc_of_parse_daifmt(struct device_node *np,
4742 const char *prefix,
4743 struct device_node **bitclkmaster,
4744 struct device_node **framemaster)
4746 int ret, i;
4747 char prop[128];
4748 unsigned int format = 0;
4749 int bit, frame;
4750 const char *str;
4751 struct {
4752 char *name;
4753 unsigned int val;
4754 } of_fmt_table[] = {
4755 { "i2s", SND_SOC_DAIFMT_I2S },
4756 { "right_j", SND_SOC_DAIFMT_RIGHT_J },
4757 { "left_j", SND_SOC_DAIFMT_LEFT_J },
4758 { "dsp_a", SND_SOC_DAIFMT_DSP_A },
4759 { "dsp_b", SND_SOC_DAIFMT_DSP_B },
4760 { "ac97", SND_SOC_DAIFMT_AC97 },
4761 { "pdm", SND_SOC_DAIFMT_PDM},
4762 { "msb", SND_SOC_DAIFMT_MSB },
4763 { "lsb", SND_SOC_DAIFMT_LSB },
4766 if (!prefix)
4767 prefix = "";
4770 * check "[prefix]format = xxx"
4771 * SND_SOC_DAIFMT_FORMAT_MASK area
4773 snprintf(prop, sizeof(prop), "%sformat", prefix);
4774 ret = of_property_read_string(np, prop, &str);
4775 if (ret == 0) {
4776 for (i = 0; i < ARRAY_SIZE(of_fmt_table); i++) {
4777 if (strcmp(str, of_fmt_table[i].name) == 0) {
4778 format |= of_fmt_table[i].val;
4779 break;
4785 * check "[prefix]continuous-clock"
4786 * SND_SOC_DAIFMT_CLOCK_MASK area
4788 snprintf(prop, sizeof(prop), "%scontinuous-clock", prefix);
4789 if (of_get_property(np, prop, NULL))
4790 format |= SND_SOC_DAIFMT_CONT;
4791 else
4792 format |= SND_SOC_DAIFMT_GATED;
4795 * check "[prefix]bitclock-inversion"
4796 * check "[prefix]frame-inversion"
4797 * SND_SOC_DAIFMT_INV_MASK area
4799 snprintf(prop, sizeof(prop), "%sbitclock-inversion", prefix);
4800 bit = !!of_get_property(np, prop, NULL);
4802 snprintf(prop, sizeof(prop), "%sframe-inversion", prefix);
4803 frame = !!of_get_property(np, prop, NULL);
4805 switch ((bit << 4) + frame) {
4806 case 0x11:
4807 format |= SND_SOC_DAIFMT_IB_IF;
4808 break;
4809 case 0x10:
4810 format |= SND_SOC_DAIFMT_IB_NF;
4811 break;
4812 case 0x01:
4813 format |= SND_SOC_DAIFMT_NB_IF;
4814 break;
4815 default:
4816 /* SND_SOC_DAIFMT_NB_NF is default */
4817 break;
4821 * check "[prefix]bitclock-master"
4822 * check "[prefix]frame-master"
4823 * SND_SOC_DAIFMT_MASTER_MASK area
4825 snprintf(prop, sizeof(prop), "%sbitclock-master", prefix);
4826 bit = !!of_get_property(np, prop, NULL);
4827 if (bit && bitclkmaster)
4828 *bitclkmaster = of_parse_phandle(np, prop, 0);
4830 snprintf(prop, sizeof(prop), "%sframe-master", prefix);
4831 frame = !!of_get_property(np, prop, NULL);
4832 if (frame && framemaster)
4833 *framemaster = of_parse_phandle(np, prop, 0);
4835 switch ((bit << 4) + frame) {
4836 case 0x11:
4837 format |= SND_SOC_DAIFMT_CBM_CFM;
4838 break;
4839 case 0x10:
4840 format |= SND_SOC_DAIFMT_CBM_CFS;
4841 break;
4842 case 0x01:
4843 format |= SND_SOC_DAIFMT_CBS_CFM;
4844 break;
4845 default:
4846 format |= SND_SOC_DAIFMT_CBS_CFS;
4847 break;
4850 return format;
4852 EXPORT_SYMBOL_GPL(snd_soc_of_parse_daifmt);
4854 int snd_soc_of_get_dai_name(struct device_node *of_node,
4855 const char **dai_name)
4857 struct snd_soc_component *pos;
4858 struct of_phandle_args args;
4859 int ret;
4861 ret = of_parse_phandle_with_args(of_node, "sound-dai",
4862 "#sound-dai-cells", 0, &args);
4863 if (ret)
4864 return ret;
4866 ret = -EPROBE_DEFER;
4868 mutex_lock(&client_mutex);
4869 list_for_each_entry(pos, &component_list, list) {
4870 if (pos->dev->of_node != args.np)
4871 continue;
4873 if (pos->driver->of_xlate_dai_name) {
4874 ret = pos->driver->of_xlate_dai_name(pos, &args, dai_name);
4875 } else {
4876 int id = -1;
4878 switch (args.args_count) {
4879 case 0:
4880 id = 0; /* same as dai_drv[0] */
4881 break;
4882 case 1:
4883 id = args.args[0];
4884 break;
4885 default:
4886 /* not supported */
4887 break;
4890 if (id < 0 || id >= pos->num_dai) {
4891 ret = -EINVAL;
4892 continue;
4895 ret = 0;
4897 *dai_name = pos->dai_drv[id].name;
4898 if (!*dai_name)
4899 *dai_name = pos->name;
4902 break;
4904 mutex_unlock(&client_mutex);
4906 of_node_put(args.np);
4908 return ret;
4910 EXPORT_SYMBOL_GPL(snd_soc_of_get_dai_name);
4912 static int __init snd_soc_init(void)
4914 #ifdef CONFIG_DEBUG_FS
4915 snd_soc_debugfs_root = debugfs_create_dir("asoc", NULL);
4916 if (IS_ERR(snd_soc_debugfs_root) || !snd_soc_debugfs_root) {
4917 pr_warn("ASoC: Failed to create debugfs directory\n");
4918 snd_soc_debugfs_root = NULL;
4921 if (!debugfs_create_file("codecs", 0444, snd_soc_debugfs_root, NULL,
4922 &codec_list_fops))
4923 pr_warn("ASoC: Failed to create CODEC list debugfs file\n");
4925 if (!debugfs_create_file("dais", 0444, snd_soc_debugfs_root, NULL,
4926 &dai_list_fops))
4927 pr_warn("ASoC: Failed to create DAI list debugfs file\n");
4929 if (!debugfs_create_file("platforms", 0444, snd_soc_debugfs_root, NULL,
4930 &platform_list_fops))
4931 pr_warn("ASoC: Failed to create platform list debugfs file\n");
4932 #endif
4934 snd_soc_util_init();
4936 return platform_driver_register(&soc_driver);
4938 module_init(snd_soc_init);
4940 static void __exit snd_soc_exit(void)
4942 snd_soc_util_exit();
4944 #ifdef CONFIG_DEBUG_FS
4945 debugfs_remove_recursive(snd_soc_debugfs_root);
4946 #endif
4947 platform_driver_unregister(&soc_driver);
4949 module_exit(snd_soc_exit);
4951 /* Module information */
4952 MODULE_AUTHOR("Liam Girdwood, lrg@slimlogic.co.uk");
4953 MODULE_DESCRIPTION("ALSA SoC Core");
4954 MODULE_LICENSE("GPL");
4955 MODULE_ALIAS("platform:soc-audio");