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.
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>
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>
37 #include <sound/core.h>
38 #include <sound/jack.h>
39 #include <sound/pcm.h>
40 #include <sound/pcm_params.h>
41 #include <sound/soc.h>
42 #include <sound/soc-dpcm.h>
43 #include <sound/initval.h>
45 #define CREATE_TRACE_POINTS
46 #include <trace/events/asoc.h>
50 #ifdef CONFIG_DEBUG_FS
51 struct dentry *snd_soc_debugfs_root;
52 EXPORT_SYMBOL_GPL(snd_soc_debugfs_root);
55 static DEFINE_MUTEX(client_mutex);
56 static LIST_HEAD(platform_list);
57 static LIST_HEAD(codec_list);
58 static LIST_HEAD(component_list);
61 * This is a timeout to do a DAPM powerdown after a stream is closed().
62 * It can be used to eliminate pops between different playback streams, e.g.
63 * between two audio tracks.
65 static int pmdown_time = 5000;
66 module_param(pmdown_time, int, 0);
67 MODULE_PARM_DESC(pmdown_time, "DAPM stream powerdown time (msecs)");
69 /* returns the minimum number of bytes needed to represent
70 * a particular given value */
71 static int min_bytes_needed(unsigned long val)
76 for (i = (sizeof val * 8) - 1; i >= 0; --i, ++c)
79 c = (sizeof val * 8) - c;
87 /* fill buf which is 'len' bytes with a formatted
88 * string of the form 'reg: value\n' */
89 static int format_register_str(struct snd_soc_codec *codec,
90 unsigned int reg, char *buf, size_t len)
92 int wordsize = min_bytes_needed(codec->driver->reg_cache_size) * 2;
93 int regsize = codec->driver->reg_word_size * 2;
96 char regbuf[regsize + 1];
98 /* since tmpbuf is allocated on the stack, warn the callers if they
99 * try to abuse this function */
102 /* +2 for ': ' and + 1 for '\n' */
103 if (wordsize + regsize + 2 + 1 != len)
106 ret = snd_soc_read(codec, reg);
108 memset(regbuf, 'X', regsize);
109 regbuf[regsize] = '\0';
111 snprintf(regbuf, regsize + 1, "%.*x", regsize, ret);
114 /* prepare the buffer */
115 snprintf(tmpbuf, len + 1, "%.*x: %s\n", wordsize, reg, regbuf);
116 /* copy it back to the caller without the '\0' */
117 memcpy(buf, tmpbuf, len);
122 /* codec register dump */
123 static ssize_t soc_codec_reg_show(struct snd_soc_codec *codec, char *buf,
124 size_t count, loff_t pos)
127 int wordsize, regsize;
132 wordsize = min_bytes_needed(codec->driver->reg_cache_size) * 2;
133 regsize = codec->driver->reg_word_size * 2;
135 len = wordsize + regsize + 2 + 1;
137 if (!codec->driver->reg_cache_size)
140 if (codec->driver->reg_cache_step)
141 step = codec->driver->reg_cache_step;
143 for (i = 0; i < codec->driver->reg_cache_size; i += step) {
144 /* only support larger than PAGE_SIZE bytes debugfs
145 * entries for the default case */
147 if (total + len >= count - 1)
149 format_register_str(codec, i, buf + total, len);
155 total = min(total, count - 1);
160 static ssize_t codec_reg_show(struct device *dev,
161 struct device_attribute *attr, char *buf)
163 struct snd_soc_pcm_runtime *rtd = dev_get_drvdata(dev);
165 return soc_codec_reg_show(rtd->codec, buf, PAGE_SIZE, 0);
168 static DEVICE_ATTR(codec_reg, 0444, codec_reg_show, NULL);
170 static ssize_t pmdown_time_show(struct device *dev,
171 struct device_attribute *attr, char *buf)
173 struct snd_soc_pcm_runtime *rtd = dev_get_drvdata(dev);
175 return sprintf(buf, "%ld\n", rtd->pmdown_time);
178 static ssize_t pmdown_time_set(struct device *dev,
179 struct device_attribute *attr,
180 const char *buf, size_t count)
182 struct snd_soc_pcm_runtime *rtd = dev_get_drvdata(dev);
185 ret = kstrtol(buf, 10, &rtd->pmdown_time);
192 static DEVICE_ATTR(pmdown_time, 0644, pmdown_time_show, pmdown_time_set);
194 #ifdef CONFIG_DEBUG_FS
195 static ssize_t codec_reg_read_file(struct file *file, char __user *user_buf,
196 size_t count, loff_t *ppos)
199 struct snd_soc_codec *codec = file->private_data;
202 if (*ppos < 0 || !count)
205 buf = kmalloc(count, GFP_KERNEL);
209 ret = soc_codec_reg_show(codec, buf, count, *ppos);
211 if (copy_to_user(user_buf, buf, ret)) {
222 static ssize_t codec_reg_write_file(struct file *file,
223 const char __user *user_buf, size_t count, loff_t *ppos)
228 unsigned long reg, value;
229 struct snd_soc_codec *codec = file->private_data;
232 buf_size = min(count, (sizeof(buf)-1));
233 if (copy_from_user(buf, user_buf, buf_size))
237 while (*start == ' ')
239 reg = simple_strtoul(start, &start, 16);
240 while (*start == ' ')
242 ret = kstrtoul(start, 16, &value);
246 /* Userspace has been fiddling around behind the kernel's back */
247 add_taint(TAINT_USER, LOCKDEP_NOW_UNRELIABLE);
249 snd_soc_write(codec, reg, value);
253 static const struct file_operations codec_reg_fops = {
255 .read = codec_reg_read_file,
256 .write = codec_reg_write_file,
257 .llseek = default_llseek,
260 static void soc_init_component_debugfs(struct snd_soc_component *component)
262 if (component->debugfs_prefix) {
265 name = kasprintf(GFP_KERNEL, "%s:%s",
266 component->debugfs_prefix, component->name);
268 component->debugfs_root = debugfs_create_dir(name,
269 component->card->debugfs_card_root);
273 component->debugfs_root = debugfs_create_dir(component->name,
274 component->card->debugfs_card_root);
277 if (!component->debugfs_root) {
278 dev_warn(component->dev,
279 "ASoC: Failed to create component debugfs directory\n");
283 snd_soc_dapm_debugfs_init(snd_soc_component_get_dapm(component),
284 component->debugfs_root);
286 if (component->init_debugfs)
287 component->init_debugfs(component);
290 static void soc_cleanup_component_debugfs(struct snd_soc_component *component)
292 debugfs_remove_recursive(component->debugfs_root);
295 static void soc_init_codec_debugfs(struct snd_soc_component *component)
297 struct snd_soc_codec *codec = snd_soc_component_to_codec(component);
299 codec->debugfs_reg = debugfs_create_file("codec_reg", 0644,
300 codec->component.debugfs_root,
301 codec, &codec_reg_fops);
302 if (!codec->debugfs_reg)
304 "ASoC: Failed to create codec register debugfs file\n");
307 static ssize_t codec_list_read_file(struct file *file, char __user *user_buf,
308 size_t count, loff_t *ppos)
310 char *buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
311 ssize_t len, ret = 0;
312 struct snd_soc_codec *codec;
317 list_for_each_entry(codec, &codec_list, list) {
318 len = snprintf(buf + ret, PAGE_SIZE - ret, "%s\n",
319 codec->component.name);
322 if (ret > PAGE_SIZE) {
329 ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
336 static const struct file_operations codec_list_fops = {
337 .read = codec_list_read_file,
338 .llseek = default_llseek,/* read accesses f_pos */
341 static ssize_t dai_list_read_file(struct file *file, char __user *user_buf,
342 size_t count, loff_t *ppos)
344 char *buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
345 ssize_t len, ret = 0;
346 struct snd_soc_component *component;
347 struct snd_soc_dai *dai;
352 list_for_each_entry(component, &component_list, list) {
353 list_for_each_entry(dai, &component->dai_list, list) {
354 len = snprintf(buf + ret, PAGE_SIZE - ret, "%s\n",
358 if (ret > PAGE_SIZE) {
365 ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
372 static const struct file_operations dai_list_fops = {
373 .read = dai_list_read_file,
374 .llseek = default_llseek,/* read accesses f_pos */
377 static ssize_t platform_list_read_file(struct file *file,
378 char __user *user_buf,
379 size_t count, loff_t *ppos)
381 char *buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
382 ssize_t len, ret = 0;
383 struct snd_soc_platform *platform;
388 list_for_each_entry(platform, &platform_list, list) {
389 len = snprintf(buf + ret, PAGE_SIZE - ret, "%s\n",
390 platform->component.name);
393 if (ret > PAGE_SIZE) {
399 ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
406 static const struct file_operations platform_list_fops = {
407 .read = platform_list_read_file,
408 .llseek = default_llseek,/* read accesses f_pos */
411 static void soc_init_card_debugfs(struct snd_soc_card *card)
413 card->debugfs_card_root = debugfs_create_dir(card->name,
414 snd_soc_debugfs_root);
415 if (!card->debugfs_card_root) {
417 "ASoC: Failed to create card debugfs directory\n");
421 card->debugfs_pop_time = debugfs_create_u32("dapm_pop_time", 0644,
422 card->debugfs_card_root,
424 if (!card->debugfs_pop_time)
426 "ASoC: Failed to create pop time debugfs file\n");
429 static void soc_cleanup_card_debugfs(struct snd_soc_card *card)
431 debugfs_remove_recursive(card->debugfs_card_root);
436 #define soc_init_codec_debugfs NULL
438 static inline void soc_init_component_debugfs(
439 struct snd_soc_component *component)
443 static inline void soc_cleanup_component_debugfs(
444 struct snd_soc_component *component)
448 static inline void soc_init_card_debugfs(struct snd_soc_card *card)
452 static inline void soc_cleanup_card_debugfs(struct snd_soc_card *card)
457 struct snd_pcm_substream *snd_soc_get_dai_substream(struct snd_soc_card *card,
458 const char *dai_link, int stream)
462 for (i = 0; i < card->num_links; i++) {
463 if (card->rtd[i].dai_link->no_pcm &&
464 !strcmp(card->rtd[i].dai_link->name, dai_link))
465 return card->rtd[i].pcm->streams[stream].substream;
467 dev_dbg(card->dev, "ASoC: failed to find dai link %s\n", dai_link);
470 EXPORT_SYMBOL_GPL(snd_soc_get_dai_substream);
472 struct snd_soc_pcm_runtime *snd_soc_get_pcm_runtime(struct snd_soc_card *card,
473 const char *dai_link)
477 for (i = 0; i < card->num_links; i++) {
478 if (!strcmp(card->rtd[i].dai_link->name, dai_link))
479 return &card->rtd[i];
481 dev_dbg(card->dev, "ASoC: failed to find rtd %s\n", dai_link);
484 EXPORT_SYMBOL_GPL(snd_soc_get_pcm_runtime);
486 static void codec2codec_close_delayed_work(struct work_struct *work)
488 /* Currently nothing to do for c2c links
489 * Since c2c links are internal nodes in the DAPM graph and
490 * don't interface with the outside world or application layer
491 * we don't have to do any special handling on close.
495 #ifdef CONFIG_PM_SLEEP
496 /* powers down audio subsystem for suspend */
497 int snd_soc_suspend(struct device *dev)
499 struct snd_soc_card *card = dev_get_drvdata(dev);
500 struct snd_soc_codec *codec;
503 /* If the card is not initialized yet there is nothing to do */
504 if (!card->instantiated)
507 /* Due to the resume being scheduled into a workqueue we could
508 * suspend before that's finished - wait for it to complete.
510 snd_power_lock(card->snd_card);
511 snd_power_wait(card->snd_card, SNDRV_CTL_POWER_D0);
512 snd_power_unlock(card->snd_card);
514 /* we're going to block userspace touching us until resume completes */
515 snd_power_change_state(card->snd_card, SNDRV_CTL_POWER_D3hot);
517 /* mute any active DACs */
518 for (i = 0; i < card->num_rtd; i++) {
520 if (card->rtd[i].dai_link->ignore_suspend)
523 for (j = 0; j < card->rtd[i].num_codecs; j++) {
524 struct snd_soc_dai *dai = card->rtd[i].codec_dais[j];
525 struct snd_soc_dai_driver *drv = dai->driver;
527 if (drv->ops->digital_mute && dai->playback_active)
528 drv->ops->digital_mute(dai, 1);
532 /* suspend all pcms */
533 for (i = 0; i < card->num_rtd; i++) {
534 if (card->rtd[i].dai_link->ignore_suspend)
537 snd_pcm_suspend_all(card->rtd[i].pcm);
540 if (card->suspend_pre)
541 card->suspend_pre(card);
543 for (i = 0; i < card->num_rtd; i++) {
544 struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
546 if (card->rtd[i].dai_link->ignore_suspend)
549 if (cpu_dai->driver->suspend && !cpu_dai->driver->bus_control)
550 cpu_dai->driver->suspend(cpu_dai);
553 /* close any waiting streams and save state */
554 for (i = 0; i < card->num_rtd; i++) {
555 struct snd_soc_dai **codec_dais = card->rtd[i].codec_dais;
556 flush_delayed_work(&card->rtd[i].delayed_work);
557 for (j = 0; j < card->rtd[i].num_codecs; j++) {
558 codec_dais[j]->codec->dapm.suspend_bias_level =
559 codec_dais[j]->codec->dapm.bias_level;
563 for (i = 0; i < card->num_rtd; i++) {
565 if (card->rtd[i].dai_link->ignore_suspend)
568 snd_soc_dapm_stream_event(&card->rtd[i],
569 SNDRV_PCM_STREAM_PLAYBACK,
570 SND_SOC_DAPM_STREAM_SUSPEND);
572 snd_soc_dapm_stream_event(&card->rtd[i],
573 SNDRV_PCM_STREAM_CAPTURE,
574 SND_SOC_DAPM_STREAM_SUSPEND);
577 /* Recheck all endpoints too, their state is affected by suspend */
578 dapm_mark_endpoints_dirty(card);
579 snd_soc_dapm_sync(&card->dapm);
581 /* suspend all CODECs */
582 list_for_each_entry(codec, &card->codec_dev_list, card_list) {
583 /* If there are paths active then the CODEC will be held with
584 * bias _ON and should not be suspended. */
585 if (!codec->suspended) {
586 switch (codec->dapm.bias_level) {
587 case SND_SOC_BIAS_STANDBY:
589 * If the CODEC is capable of idle
590 * bias off then being in STANDBY
591 * means it's doing something,
592 * otherwise fall through.
594 if (codec->dapm.idle_bias_off) {
596 "ASoC: idle_bias_off CODEC on over suspend\n");
600 case SND_SOC_BIAS_OFF:
601 if (codec->driver->suspend)
602 codec->driver->suspend(codec);
603 codec->suspended = 1;
604 if (codec->component.regmap)
605 regcache_mark_dirty(codec->component.regmap);
606 /* deactivate pins to sleep state */
607 pinctrl_pm_select_sleep_state(codec->dev);
611 "ASoC: CODEC is on over suspend\n");
617 for (i = 0; i < card->num_rtd; i++) {
618 struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
620 if (card->rtd[i].dai_link->ignore_suspend)
623 if (cpu_dai->driver->suspend && cpu_dai->driver->bus_control)
624 cpu_dai->driver->suspend(cpu_dai);
626 /* deactivate pins to sleep state */
627 pinctrl_pm_select_sleep_state(cpu_dai->dev);
630 if (card->suspend_post)
631 card->suspend_post(card);
635 EXPORT_SYMBOL_GPL(snd_soc_suspend);
637 /* deferred resume work, so resume can complete before we finished
638 * setting our codec back up, which can be very slow on I2C
640 static void soc_resume_deferred(struct work_struct *work)
642 struct snd_soc_card *card =
643 container_of(work, struct snd_soc_card, deferred_resume_work);
644 struct snd_soc_codec *codec;
647 /* our power state is still SNDRV_CTL_POWER_D3hot from suspend time,
648 * so userspace apps are blocked from touching us
651 dev_dbg(card->dev, "ASoC: starting resume work\n");
653 /* Bring us up into D2 so that DAPM starts enabling things */
654 snd_power_change_state(card->snd_card, SNDRV_CTL_POWER_D2);
656 if (card->resume_pre)
657 card->resume_pre(card);
659 /* resume control bus DAIs */
660 for (i = 0; i < card->num_rtd; i++) {
661 struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
663 if (card->rtd[i].dai_link->ignore_suspend)
666 if (cpu_dai->driver->resume && cpu_dai->driver->bus_control)
667 cpu_dai->driver->resume(cpu_dai);
670 list_for_each_entry(codec, &card->codec_dev_list, card_list) {
671 /* If the CODEC was idle over suspend then it will have been
672 * left with bias OFF or STANDBY and suspended so we must now
673 * resume. Otherwise the suspend was suppressed.
675 if (codec->suspended) {
676 switch (codec->dapm.bias_level) {
677 case SND_SOC_BIAS_STANDBY:
678 case SND_SOC_BIAS_OFF:
679 if (codec->driver->resume)
680 codec->driver->resume(codec);
681 codec->suspended = 0;
685 "ASoC: CODEC was on over suspend\n");
691 for (i = 0; i < card->num_rtd; i++) {
693 if (card->rtd[i].dai_link->ignore_suspend)
696 snd_soc_dapm_stream_event(&card->rtd[i],
697 SNDRV_PCM_STREAM_PLAYBACK,
698 SND_SOC_DAPM_STREAM_RESUME);
700 snd_soc_dapm_stream_event(&card->rtd[i],
701 SNDRV_PCM_STREAM_CAPTURE,
702 SND_SOC_DAPM_STREAM_RESUME);
705 /* unmute any active DACs */
706 for (i = 0; i < card->num_rtd; i++) {
708 if (card->rtd[i].dai_link->ignore_suspend)
711 for (j = 0; j < card->rtd[i].num_codecs; j++) {
712 struct snd_soc_dai *dai = card->rtd[i].codec_dais[j];
713 struct snd_soc_dai_driver *drv = dai->driver;
715 if (drv->ops->digital_mute && dai->playback_active)
716 drv->ops->digital_mute(dai, 0);
720 for (i = 0; i < card->num_rtd; i++) {
721 struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
723 if (card->rtd[i].dai_link->ignore_suspend)
726 if (cpu_dai->driver->resume && !cpu_dai->driver->bus_control)
727 cpu_dai->driver->resume(cpu_dai);
730 if (card->resume_post)
731 card->resume_post(card);
733 dev_dbg(card->dev, "ASoC: resume work completed\n");
735 /* userspace can access us now we are back as we were before */
736 snd_power_change_state(card->snd_card, SNDRV_CTL_POWER_D0);
738 /* Recheck all endpoints too, their state is affected by suspend */
739 dapm_mark_endpoints_dirty(card);
740 snd_soc_dapm_sync(&card->dapm);
743 /* powers up audio subsystem after a suspend */
744 int snd_soc_resume(struct device *dev)
746 struct snd_soc_card *card = dev_get_drvdata(dev);
747 bool bus_control = false;
750 /* If the card is not initialized yet there is nothing to do */
751 if (!card->instantiated)
754 /* activate pins from sleep state */
755 for (i = 0; i < card->num_rtd; i++) {
756 struct snd_soc_pcm_runtime *rtd = &card->rtd[i];
757 struct snd_soc_dai **codec_dais = rtd->codec_dais;
758 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
762 pinctrl_pm_select_default_state(cpu_dai->dev);
764 for (j = 0; j < rtd->num_codecs; j++) {
765 struct snd_soc_dai *codec_dai = codec_dais[j];
766 if (codec_dai->active)
767 pinctrl_pm_select_default_state(codec_dai->dev);
772 * DAIs that also act as the control bus master might have other drivers
773 * hanging off them so need to resume immediately. Other drivers don't
774 * have that problem and may take a substantial amount of time to resume
775 * due to I/O costs and anti-pop so handle them out of line.
777 for (i = 0; i < card->num_rtd; i++) {
778 struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
779 bus_control |= cpu_dai->driver->bus_control;
782 dev_dbg(dev, "ASoC: Resuming control bus master immediately\n");
783 soc_resume_deferred(&card->deferred_resume_work);
785 dev_dbg(dev, "ASoC: Scheduling resume work\n");
786 if (!schedule_work(&card->deferred_resume_work))
787 dev_err(dev, "ASoC: resume work item may be lost\n");
792 EXPORT_SYMBOL_GPL(snd_soc_resume);
794 #define snd_soc_suspend NULL
795 #define snd_soc_resume NULL
798 static const struct snd_soc_dai_ops null_dai_ops = {
801 static struct snd_soc_component *soc_find_component(
802 const struct device_node *of_node, const char *name)
804 struct snd_soc_component *component;
806 list_for_each_entry(component, &component_list, list) {
808 if (component->dev->of_node == of_node)
810 } else if (strcmp(component->name, name) == 0) {
818 static struct snd_soc_dai *snd_soc_find_dai(
819 const struct snd_soc_dai_link_component *dlc)
821 struct snd_soc_component *component;
822 struct snd_soc_dai *dai;
824 /* Find CPU DAI from registered DAIs*/
825 list_for_each_entry(component, &component_list, list) {
826 if (dlc->of_node && component->dev->of_node != dlc->of_node)
828 if (dlc->name && strcmp(component->name, dlc->name))
830 list_for_each_entry(dai, &component->dai_list, list) {
831 if (dlc->dai_name && strcmp(dai->name, dlc->dai_name))
841 static int soc_bind_dai_link(struct snd_soc_card *card, int num)
843 struct snd_soc_dai_link *dai_link = &card->dai_link[num];
844 struct snd_soc_pcm_runtime *rtd = &card->rtd[num];
845 struct snd_soc_dai_link_component *codecs = dai_link->codecs;
846 struct snd_soc_dai_link_component cpu_dai_component;
847 struct snd_soc_dai **codec_dais = rtd->codec_dais;
848 struct snd_soc_platform *platform;
849 const char *platform_name;
852 dev_dbg(card->dev, "ASoC: binding %s at idx %d\n", dai_link->name, num);
854 cpu_dai_component.name = dai_link->cpu_name;
855 cpu_dai_component.of_node = dai_link->cpu_of_node;
856 cpu_dai_component.dai_name = dai_link->cpu_dai_name;
857 rtd->cpu_dai = snd_soc_find_dai(&cpu_dai_component);
859 dev_err(card->dev, "ASoC: CPU DAI %s not registered\n",
860 dai_link->cpu_dai_name);
861 return -EPROBE_DEFER;
864 rtd->num_codecs = dai_link->num_codecs;
866 /* Find CODEC from registered CODECs */
867 for (i = 0; i < rtd->num_codecs; i++) {
868 codec_dais[i] = snd_soc_find_dai(&codecs[i]);
869 if (!codec_dais[i]) {
870 dev_err(card->dev, "ASoC: CODEC DAI %s not registered\n",
872 return -EPROBE_DEFER;
876 /* Single codec links expect codec and codec_dai in runtime data */
877 rtd->codec_dai = codec_dais[0];
878 rtd->codec = rtd->codec_dai->codec;
880 /* if there's no platform we match on the empty platform */
881 platform_name = dai_link->platform_name;
882 if (!platform_name && !dai_link->platform_of_node)
883 platform_name = "snd-soc-dummy";
885 /* find one from the set of registered platforms */
886 list_for_each_entry(platform, &platform_list, list) {
887 if (dai_link->platform_of_node) {
888 if (platform->dev->of_node !=
889 dai_link->platform_of_node)
892 if (strcmp(platform->component.name, platform_name))
896 rtd->platform = platform;
898 if (!rtd->platform) {
899 dev_err(card->dev, "ASoC: platform %s not registered\n",
900 dai_link->platform_name);
901 return -EPROBE_DEFER;
909 static void soc_remove_component(struct snd_soc_component *component)
911 if (!component->probed)
914 /* This is a HACK and will be removed soon */
915 if (component->codec)
916 list_del(&component->codec->card_list);
918 if (component->remove)
919 component->remove(component);
921 snd_soc_dapm_free(snd_soc_component_get_dapm(component));
923 soc_cleanup_component_debugfs(component);
924 component->probed = 0;
925 module_put(component->dev->driver->owner);
928 static void soc_remove_dai(struct snd_soc_dai *dai, int order)
932 if (dai && dai->probed &&
933 dai->driver->remove_order == order) {
934 if (dai->driver->remove) {
935 err = dai->driver->remove(dai);
938 "ASoC: failed to remove %s: %d\n",
945 static void soc_remove_link_dais(struct snd_soc_card *card, int num, int order)
947 struct snd_soc_pcm_runtime *rtd = &card->rtd[num];
950 /* unregister the rtd device */
951 if (rtd->dev_registered) {
952 device_remove_file(rtd->dev, &dev_attr_pmdown_time);
953 device_remove_file(rtd->dev, &dev_attr_codec_reg);
954 device_unregister(rtd->dev);
955 rtd->dev_registered = 0;
958 /* remove the CODEC DAI */
959 for (i = 0; i < rtd->num_codecs; i++)
960 soc_remove_dai(rtd->codec_dais[i], order);
962 soc_remove_dai(rtd->cpu_dai, order);
965 static void soc_remove_link_components(struct snd_soc_card *card, int num,
968 struct snd_soc_pcm_runtime *rtd = &card->rtd[num];
969 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
970 struct snd_soc_platform *platform = rtd->platform;
971 struct snd_soc_component *component;
974 /* remove the platform */
975 if (platform && platform->component.driver->remove_order == order)
976 soc_remove_component(&platform->component);
978 /* remove the CODEC-side CODEC */
979 for (i = 0; i < rtd->num_codecs; i++) {
980 component = rtd->codec_dais[i]->component;
981 if (component->driver->remove_order == order)
982 soc_remove_component(component);
985 /* remove any CPU-side CODEC */
987 if (cpu_dai->component->driver->remove_order == order)
988 soc_remove_component(cpu_dai->component);
992 static void soc_remove_dai_links(struct snd_soc_card *card)
996 for (order = SND_SOC_COMP_ORDER_FIRST; order <= SND_SOC_COMP_ORDER_LAST;
998 for (dai = 0; dai < card->num_rtd; dai++)
999 soc_remove_link_dais(card, dai, order);
1002 for (order = SND_SOC_COMP_ORDER_FIRST; order <= SND_SOC_COMP_ORDER_LAST;
1004 for (dai = 0; dai < card->num_rtd; dai++)
1005 soc_remove_link_components(card, dai, order);
1011 static void soc_set_name_prefix(struct snd_soc_card *card,
1012 struct snd_soc_component *component)
1016 if (card->codec_conf == NULL)
1019 for (i = 0; i < card->num_configs; i++) {
1020 struct snd_soc_codec_conf *map = &card->codec_conf[i];
1021 if (map->of_node && component->dev->of_node != map->of_node)
1023 if (map->dev_name && strcmp(component->name, map->dev_name))
1025 component->name_prefix = map->name_prefix;
1030 static int soc_probe_component(struct snd_soc_card *card,
1031 struct snd_soc_component *component)
1033 struct snd_soc_dapm_context *dapm = snd_soc_component_get_dapm(component);
1034 struct snd_soc_dai *dai;
1037 if (component->probed)
1040 component->card = card;
1042 soc_set_name_prefix(card, component);
1044 if (!try_module_get(component->dev->driver->owner))
1047 soc_init_component_debugfs(component);
1049 if (component->dapm_widgets) {
1050 ret = snd_soc_dapm_new_controls(dapm, component->dapm_widgets,
1051 component->num_dapm_widgets);
1054 dev_err(component->dev,
1055 "Failed to create new controls %d\n", ret);
1060 list_for_each_entry(dai, &component->dai_list, list) {
1061 ret = snd_soc_dapm_new_dai_widgets(dapm, dai);
1063 dev_err(component->dev,
1064 "Failed to create DAI widgets %d\n", ret);
1069 if (component->probe) {
1070 ret = component->probe(component);
1072 dev_err(component->dev,
1073 "ASoC: failed to probe component %d\n", ret);
1077 WARN(dapm->idle_bias_off &&
1078 dapm->bias_level != SND_SOC_BIAS_OFF,
1079 "codec %s can not start from non-off bias with idle_bias_off==1\n",
1083 if (component->controls)
1084 snd_soc_add_component_controls(component, component->controls,
1085 component->num_controls);
1086 if (component->dapm_routes)
1087 snd_soc_dapm_add_routes(dapm, component->dapm_routes,
1088 component->num_dapm_routes);
1090 component->probed = 1;
1091 list_add(&dapm->list, &card->dapm_list);
1093 /* This is a HACK and will be removed soon */
1094 if (component->codec)
1095 list_add(&component->codec->card_list, &card->codec_dev_list);
1100 soc_cleanup_component_debugfs(component);
1101 module_put(component->dev->driver->owner);
1106 static void rtd_release(struct device *dev)
1111 static int soc_post_component_init(struct snd_soc_pcm_runtime *rtd,
1116 /* register the rtd device */
1117 rtd->dev = kzalloc(sizeof(struct device), GFP_KERNEL);
1120 device_initialize(rtd->dev);
1121 rtd->dev->parent = rtd->card->dev;
1122 rtd->dev->release = rtd_release;
1123 dev_set_name(rtd->dev, "%s", name);
1124 dev_set_drvdata(rtd->dev, rtd);
1125 mutex_init(&rtd->pcm_mutex);
1126 INIT_LIST_HEAD(&rtd->dpcm[SNDRV_PCM_STREAM_PLAYBACK].be_clients);
1127 INIT_LIST_HEAD(&rtd->dpcm[SNDRV_PCM_STREAM_CAPTURE].be_clients);
1128 INIT_LIST_HEAD(&rtd->dpcm[SNDRV_PCM_STREAM_PLAYBACK].fe_clients);
1129 INIT_LIST_HEAD(&rtd->dpcm[SNDRV_PCM_STREAM_CAPTURE].fe_clients);
1130 ret = device_add(rtd->dev);
1132 /* calling put_device() here to free the rtd->dev */
1133 put_device(rtd->dev);
1134 dev_err(rtd->card->dev,
1135 "ASoC: failed to register runtime device: %d\n", ret);
1138 rtd->dev_registered = 1;
1141 /* add DAPM sysfs entries for this codec */
1142 ret = snd_soc_dapm_sys_add(rtd->dev);
1145 "ASoC: failed to add codec dapm sysfs entries: %d\n",
1148 /* add codec sysfs entries */
1149 ret = device_create_file(rtd->dev, &dev_attr_codec_reg);
1152 "ASoC: failed to add codec sysfs files: %d\n",
1159 static int soc_probe_link_components(struct snd_soc_card *card, int num,
1162 struct snd_soc_pcm_runtime *rtd = &card->rtd[num];
1163 struct snd_soc_platform *platform = rtd->platform;
1164 struct snd_soc_component *component;
1167 /* probe the CPU-side component, if it is a CODEC */
1168 component = rtd->cpu_dai->component;
1169 if (component->driver->probe_order == order) {
1170 ret = soc_probe_component(card, component);
1175 /* probe the CODEC-side components */
1176 for (i = 0; i < rtd->num_codecs; i++) {
1177 component = rtd->codec_dais[i]->component;
1178 if (component->driver->probe_order == order) {
1179 ret = soc_probe_component(card, component);
1185 /* probe the platform */
1186 if (platform->component.driver->probe_order == order) {
1187 ret = soc_probe_component(card, &platform->component);
1195 static int soc_probe_dai(struct snd_soc_dai *dai, int order)
1199 if (!dai->probed && dai->driver->probe_order == order) {
1200 if (dai->driver->probe) {
1201 ret = dai->driver->probe(dai);
1204 "ASoC: failed to probe DAI %s: %d\n",
1216 static int soc_link_dai_widgets(struct snd_soc_card *card,
1217 struct snd_soc_dai_link *dai_link,
1218 struct snd_soc_pcm_runtime *rtd)
1220 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
1221 struct snd_soc_dai *codec_dai = rtd->codec_dai;
1222 struct snd_soc_dapm_widget *play_w, *capture_w;
1225 if (rtd->num_codecs > 1)
1226 dev_warn(card->dev, "ASoC: Multiple codecs not supported yet\n");
1228 /* link the DAI widgets */
1229 play_w = codec_dai->playback_widget;
1230 capture_w = cpu_dai->capture_widget;
1231 if (play_w && capture_w) {
1232 ret = snd_soc_dapm_new_pcm(card, dai_link->params,
1235 dev_err(card->dev, "ASoC: Can't link %s to %s: %d\n",
1236 play_w->name, capture_w->name, ret);
1241 play_w = cpu_dai->playback_widget;
1242 capture_w = codec_dai->capture_widget;
1243 if (play_w && capture_w) {
1244 ret = snd_soc_dapm_new_pcm(card, dai_link->params,
1247 dev_err(card->dev, "ASoC: Can't link %s to %s: %d\n",
1248 play_w->name, capture_w->name, ret);
1256 static int soc_probe_link_dais(struct snd_soc_card *card, int num, int order)
1258 struct snd_soc_dai_link *dai_link = &card->dai_link[num];
1259 struct snd_soc_pcm_runtime *rtd = &card->rtd[num];
1260 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
1263 dev_dbg(card->dev, "ASoC: probe %s dai link %d late %d\n",
1264 card->name, num, order);
1266 /* set default power off timeout */
1267 rtd->pmdown_time = pmdown_time;
1269 ret = soc_probe_dai(cpu_dai, order);
1273 /* probe the CODEC DAI */
1274 for (i = 0; i < rtd->num_codecs; i++) {
1275 ret = soc_probe_dai(rtd->codec_dais[i], order);
1280 /* complete DAI probe during last probe */
1281 if (order != SND_SOC_COMP_ORDER_LAST)
1284 /* do machine specific initialization */
1285 if (dai_link->init) {
1286 ret = dai_link->init(rtd);
1288 dev_err(card->dev, "ASoC: failed to init %s: %d\n",
1289 dai_link->name, ret);
1294 ret = soc_post_component_init(rtd, dai_link->name);
1298 #ifdef CONFIG_DEBUG_FS
1299 /* add DPCM sysfs entries */
1300 if (dai_link->dynamic) {
1301 ret = soc_dpcm_debugfs_add(rtd);
1304 "ASoC: failed to add dpcm sysfs entries: %d\n",
1311 ret = device_create_file(rtd->dev, &dev_attr_pmdown_time);
1313 dev_warn(rtd->dev, "ASoC: failed to add pmdown_time sysfs: %d\n",
1316 if (cpu_dai->driver->compress_dai) {
1317 /*create compress_device"*/
1318 ret = soc_new_compress(rtd, num);
1320 dev_err(card->dev, "ASoC: can't create compress %s\n",
1321 dai_link->stream_name);
1326 if (!dai_link->params) {
1327 /* create the pcm */
1328 ret = soc_new_pcm(rtd, num);
1330 dev_err(card->dev, "ASoC: can't create pcm %s :%d\n",
1331 dai_link->stream_name, ret);
1335 INIT_DELAYED_WORK(&rtd->delayed_work,
1336 codec2codec_close_delayed_work);
1338 /* link the DAI widgets */
1339 ret = soc_link_dai_widgets(card, dai_link, rtd);
1348 static int soc_bind_aux_dev(struct snd_soc_card *card, int num)
1350 struct snd_soc_pcm_runtime *rtd = &card->rtd_aux[num];
1351 struct snd_soc_aux_dev *aux_dev = &card->aux_dev[num];
1352 const char *name = aux_dev->codec_name;
1354 rtd->component = soc_find_component(aux_dev->codec_of_node, name);
1355 if (!rtd->component) {
1356 if (aux_dev->codec_of_node)
1357 name = of_node_full_name(aux_dev->codec_of_node);
1359 dev_err(card->dev, "ASoC: %s not registered\n", name);
1360 return -EPROBE_DEFER;
1364 * Some places still reference rtd->codec, so we have to keep that
1365 * initialized if the component is a CODEC. Once all those references
1366 * have been removed, this code can be removed as well.
1368 rtd->codec = rtd->component->codec;
1373 static int soc_probe_aux_dev(struct snd_soc_card *card, int num)
1375 struct snd_soc_pcm_runtime *rtd = &card->rtd_aux[num];
1376 struct snd_soc_aux_dev *aux_dev = &card->aux_dev[num];
1379 ret = soc_probe_component(card, rtd->component);
1383 /* do machine specific initialization */
1384 if (aux_dev->init) {
1385 ret = aux_dev->init(rtd->component);
1387 dev_err(card->dev, "ASoC: failed to init %s: %d\n",
1388 aux_dev->name, ret);
1393 return soc_post_component_init(rtd, aux_dev->name);
1396 static void soc_remove_aux_dev(struct snd_soc_card *card, int num)
1398 struct snd_soc_pcm_runtime *rtd = &card->rtd_aux[num];
1399 struct snd_soc_component *component = rtd->component;
1401 /* unregister the rtd device */
1402 if (rtd->dev_registered) {
1403 device_remove_file(rtd->dev, &dev_attr_codec_reg);
1404 device_unregister(rtd->dev);
1405 rtd->dev_registered = 0;
1408 if (component && component->probed)
1409 soc_remove_component(component);
1412 static int snd_soc_init_codec_cache(struct snd_soc_codec *codec)
1416 if (codec->cache_init)
1419 ret = snd_soc_cache_init(codec);
1422 "ASoC: Failed to set cache compression type: %d\n",
1426 codec->cache_init = 1;
1430 static int snd_soc_instantiate_card(struct snd_soc_card *card)
1432 struct snd_soc_codec *codec;
1433 struct snd_soc_dai_link *dai_link;
1434 int ret, i, order, dai_fmt;
1436 mutex_lock_nested(&card->mutex, SND_SOC_CARD_CLASS_INIT);
1439 for (i = 0; i < card->num_links; i++) {
1440 ret = soc_bind_dai_link(card, i);
1445 /* bind aux_devs too */
1446 for (i = 0; i < card->num_aux_devs; i++) {
1447 ret = soc_bind_aux_dev(card, i);
1452 /* initialize the register cache for each available codec */
1453 list_for_each_entry(codec, &codec_list, list) {
1454 if (codec->cache_init)
1456 ret = snd_soc_init_codec_cache(codec);
1461 /* card bind complete so register a sound card */
1462 ret = snd_card_new(card->dev, SNDRV_DEFAULT_IDX1, SNDRV_DEFAULT_STR1,
1463 card->owner, 0, &card->snd_card);
1466 "ASoC: can't create sound card for card %s: %d\n",
1471 card->dapm.bias_level = SND_SOC_BIAS_OFF;
1472 card->dapm.dev = card->dev;
1473 card->dapm.card = card;
1474 list_add(&card->dapm.list, &card->dapm_list);
1476 #ifdef CONFIG_DEBUG_FS
1477 snd_soc_dapm_debugfs_init(&card->dapm, card->debugfs_card_root);
1480 #ifdef CONFIG_PM_SLEEP
1481 /* deferred resume work */
1482 INIT_WORK(&card->deferred_resume_work, soc_resume_deferred);
1485 if (card->dapm_widgets)
1486 snd_soc_dapm_new_controls(&card->dapm, card->dapm_widgets,
1487 card->num_dapm_widgets);
1489 /* initialise the sound card only once */
1491 ret = card->probe(card);
1493 goto card_probe_error;
1496 /* probe all components used by DAI links on this card */
1497 for (order = SND_SOC_COMP_ORDER_FIRST; order <= SND_SOC_COMP_ORDER_LAST;
1499 for (i = 0; i < card->num_links; i++) {
1500 ret = soc_probe_link_components(card, i, order);
1503 "ASoC: failed to instantiate card %d\n",
1510 /* probe all DAI links on this card */
1511 for (order = SND_SOC_COMP_ORDER_FIRST; order <= SND_SOC_COMP_ORDER_LAST;
1513 for (i = 0; i < card->num_links; i++) {
1514 ret = soc_probe_link_dais(card, i, order);
1517 "ASoC: failed to instantiate card %d\n",
1524 for (i = 0; i < card->num_aux_devs; i++) {
1525 ret = soc_probe_aux_dev(card, i);
1528 "ASoC: failed to add auxiliary devices %d\n",
1530 goto probe_aux_dev_err;
1534 snd_soc_dapm_link_dai_widgets(card);
1535 snd_soc_dapm_connect_dai_link_widgets(card);
1538 snd_soc_add_card_controls(card, card->controls, card->num_controls);
1540 if (card->dapm_routes)
1541 snd_soc_dapm_add_routes(&card->dapm, card->dapm_routes,
1542 card->num_dapm_routes);
1544 for (i = 0; i < card->num_links; i++) {
1545 struct snd_soc_pcm_runtime *rtd = &card->rtd[i];
1546 dai_link = &card->dai_link[i];
1547 dai_fmt = dai_link->dai_fmt;
1550 struct snd_soc_dai **codec_dais = rtd->codec_dais;
1553 for (j = 0; j < rtd->num_codecs; j++) {
1554 struct snd_soc_dai *codec_dai = codec_dais[j];
1556 ret = snd_soc_dai_set_fmt(codec_dai, dai_fmt);
1557 if (ret != 0 && ret != -ENOTSUPP)
1558 dev_warn(codec_dai->dev,
1559 "ASoC: Failed to set DAI format: %d\n",
1564 /* If this is a regular CPU link there will be a platform */
1566 (dai_link->platform_name || dai_link->platform_of_node)) {
1567 ret = snd_soc_dai_set_fmt(card->rtd[i].cpu_dai,
1569 if (ret != 0 && ret != -ENOTSUPP)
1570 dev_warn(card->rtd[i].cpu_dai->dev,
1571 "ASoC: Failed to set DAI format: %d\n",
1573 } else if (dai_fmt) {
1574 /* Flip the polarity for the "CPU" end */
1575 dai_fmt &= ~SND_SOC_DAIFMT_MASTER_MASK;
1576 switch (dai_link->dai_fmt &
1577 SND_SOC_DAIFMT_MASTER_MASK) {
1578 case SND_SOC_DAIFMT_CBM_CFM:
1579 dai_fmt |= SND_SOC_DAIFMT_CBS_CFS;
1581 case SND_SOC_DAIFMT_CBM_CFS:
1582 dai_fmt |= SND_SOC_DAIFMT_CBS_CFM;
1584 case SND_SOC_DAIFMT_CBS_CFM:
1585 dai_fmt |= SND_SOC_DAIFMT_CBM_CFS;
1587 case SND_SOC_DAIFMT_CBS_CFS:
1588 dai_fmt |= SND_SOC_DAIFMT_CBM_CFM;
1592 ret = snd_soc_dai_set_fmt(card->rtd[i].cpu_dai,
1594 if (ret != 0 && ret != -ENOTSUPP)
1595 dev_warn(card->rtd[i].cpu_dai->dev,
1596 "ASoC: Failed to set DAI format: %d\n",
1601 snprintf(card->snd_card->shortname, sizeof(card->snd_card->shortname),
1603 snprintf(card->snd_card->longname, sizeof(card->snd_card->longname),
1604 "%s", card->long_name ? card->long_name : card->name);
1605 snprintf(card->snd_card->driver, sizeof(card->snd_card->driver),
1606 "%s", card->driver_name ? card->driver_name : card->name);
1607 for (i = 0; i < ARRAY_SIZE(card->snd_card->driver); i++) {
1608 switch (card->snd_card->driver[i]) {
1614 if (!isalnum(card->snd_card->driver[i]))
1615 card->snd_card->driver[i] = '_';
1620 if (card->late_probe) {
1621 ret = card->late_probe(card);
1623 dev_err(card->dev, "ASoC: %s late_probe() failed: %d\n",
1625 goto probe_aux_dev_err;
1629 if (card->fully_routed)
1630 snd_soc_dapm_auto_nc_pins(card);
1632 snd_soc_dapm_new_widgets(card);
1634 ret = snd_card_register(card->snd_card);
1636 dev_err(card->dev, "ASoC: failed to register soundcard %d\n",
1638 goto probe_aux_dev_err;
1641 card->instantiated = 1;
1642 snd_soc_dapm_sync(&card->dapm);
1643 mutex_unlock(&card->mutex);
1648 for (i = 0; i < card->num_aux_devs; i++)
1649 soc_remove_aux_dev(card, i);
1652 soc_remove_dai_links(card);
1658 snd_card_free(card->snd_card);
1661 mutex_unlock(&card->mutex);
1666 /* probes a new socdev */
1667 static int soc_probe(struct platform_device *pdev)
1669 struct snd_soc_card *card = platform_get_drvdata(pdev);
1672 * no card, so machine driver should be registering card
1673 * we should not be here in that case so ret error
1678 dev_warn(&pdev->dev,
1679 "ASoC: machine %s should use snd_soc_register_card()\n",
1682 /* Bodge while we unpick instantiation */
1683 card->dev = &pdev->dev;
1685 return snd_soc_register_card(card);
1688 static int soc_cleanup_card_resources(struct snd_soc_card *card)
1692 /* make sure any delayed work runs */
1693 for (i = 0; i < card->num_rtd; i++) {
1694 struct snd_soc_pcm_runtime *rtd = &card->rtd[i];
1695 flush_delayed_work(&rtd->delayed_work);
1698 /* remove auxiliary devices */
1699 for (i = 0; i < card->num_aux_devs; i++)
1700 soc_remove_aux_dev(card, i);
1702 /* remove and free each DAI */
1703 soc_remove_dai_links(card);
1705 soc_cleanup_card_debugfs(card);
1707 /* remove the card */
1711 snd_soc_dapm_free(&card->dapm);
1713 snd_card_free(card->snd_card);
1718 /* removes a socdev */
1719 static int soc_remove(struct platform_device *pdev)
1721 struct snd_soc_card *card = platform_get_drvdata(pdev);
1723 snd_soc_unregister_card(card);
1727 int snd_soc_poweroff(struct device *dev)
1729 struct snd_soc_card *card = dev_get_drvdata(dev);
1732 if (!card->instantiated)
1735 /* Flush out pmdown_time work - we actually do want to run it
1736 * now, we're shutting down so no imminent restart. */
1737 for (i = 0; i < card->num_rtd; i++) {
1738 struct snd_soc_pcm_runtime *rtd = &card->rtd[i];
1739 flush_delayed_work(&rtd->delayed_work);
1742 snd_soc_dapm_shutdown(card);
1744 /* deactivate pins to sleep state */
1745 for (i = 0; i < card->num_rtd; i++) {
1746 struct snd_soc_pcm_runtime *rtd = &card->rtd[i];
1747 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
1750 pinctrl_pm_select_sleep_state(cpu_dai->dev);
1751 for (j = 0; j < rtd->num_codecs; j++) {
1752 struct snd_soc_dai *codec_dai = rtd->codec_dais[j];
1753 pinctrl_pm_select_sleep_state(codec_dai->dev);
1759 EXPORT_SYMBOL_GPL(snd_soc_poweroff);
1761 const struct dev_pm_ops snd_soc_pm_ops = {
1762 .suspend = snd_soc_suspend,
1763 .resume = snd_soc_resume,
1764 .freeze = snd_soc_suspend,
1765 .thaw = snd_soc_resume,
1766 .poweroff = snd_soc_poweroff,
1767 .restore = snd_soc_resume,
1769 EXPORT_SYMBOL_GPL(snd_soc_pm_ops);
1771 /* ASoC platform driver */
1772 static struct platform_driver soc_driver = {
1774 .name = "soc-audio",
1775 .owner = THIS_MODULE,
1776 .pm = &snd_soc_pm_ops,
1779 .remove = soc_remove,
1783 * snd_soc_cnew - create new control
1784 * @_template: control template
1785 * @data: control private data
1786 * @long_name: control long name
1787 * @prefix: control name prefix
1789 * Create a new mixer control from a template control.
1791 * Returns 0 for success, else error.
1793 struct snd_kcontrol *snd_soc_cnew(const struct snd_kcontrol_new *_template,
1794 void *data, const char *long_name,
1797 struct snd_kcontrol_new template;
1798 struct snd_kcontrol *kcontrol;
1801 memcpy(&template, _template, sizeof(template));
1805 long_name = template.name;
1808 name = kasprintf(GFP_KERNEL, "%s %s", prefix, long_name);
1812 template.name = name;
1814 template.name = long_name;
1817 kcontrol = snd_ctl_new1(&template, data);
1823 EXPORT_SYMBOL_GPL(snd_soc_cnew);
1825 static int snd_soc_add_controls(struct snd_card *card, struct device *dev,
1826 const struct snd_kcontrol_new *controls, int num_controls,
1827 const char *prefix, void *data)
1831 for (i = 0; i < num_controls; i++) {
1832 const struct snd_kcontrol_new *control = &controls[i];
1833 err = snd_ctl_add(card, snd_soc_cnew(control, data,
1834 control->name, prefix));
1836 dev_err(dev, "ASoC: Failed to add %s: %d\n",
1837 control->name, err);
1845 struct snd_kcontrol *snd_soc_card_get_kcontrol(struct snd_soc_card *soc_card,
1848 struct snd_card *card = soc_card->snd_card;
1849 struct snd_kcontrol *kctl;
1851 if (unlikely(!name))
1854 list_for_each_entry(kctl, &card->controls, list)
1855 if (!strncmp(kctl->id.name, name, sizeof(kctl->id.name)))
1859 EXPORT_SYMBOL_GPL(snd_soc_card_get_kcontrol);
1862 * snd_soc_add_component_controls - Add an array of controls to a component.
1864 * @component: Component to add controls to
1865 * @controls: Array of controls to add
1866 * @num_controls: Number of elements in the array
1868 * Return: 0 for success, else error.
1870 int snd_soc_add_component_controls(struct snd_soc_component *component,
1871 const struct snd_kcontrol_new *controls, unsigned int num_controls)
1873 struct snd_card *card = component->card->snd_card;
1875 return snd_soc_add_controls(card, component->dev, controls,
1876 num_controls, component->name_prefix, component);
1878 EXPORT_SYMBOL_GPL(snd_soc_add_component_controls);
1881 * snd_soc_add_codec_controls - add an array of controls to a codec.
1882 * Convenience function to add a list of controls. Many codecs were
1883 * duplicating this code.
1885 * @codec: codec to add controls to
1886 * @controls: array of controls to add
1887 * @num_controls: number of elements in the array
1889 * Return 0 for success, else error.
1891 int snd_soc_add_codec_controls(struct snd_soc_codec *codec,
1892 const struct snd_kcontrol_new *controls, unsigned int num_controls)
1894 return snd_soc_add_component_controls(&codec->component, controls,
1897 EXPORT_SYMBOL_GPL(snd_soc_add_codec_controls);
1900 * snd_soc_add_platform_controls - add an array of controls to a platform.
1901 * Convenience function to add a list of controls.
1903 * @platform: platform to add controls to
1904 * @controls: array of controls to add
1905 * @num_controls: number of elements in the array
1907 * Return 0 for success, else error.
1909 int snd_soc_add_platform_controls(struct snd_soc_platform *platform,
1910 const struct snd_kcontrol_new *controls, unsigned int num_controls)
1912 return snd_soc_add_component_controls(&platform->component, controls,
1915 EXPORT_SYMBOL_GPL(snd_soc_add_platform_controls);
1918 * snd_soc_add_card_controls - add an array of controls to a SoC card.
1919 * Convenience function to add a list of controls.
1921 * @soc_card: SoC card to add controls to
1922 * @controls: array of controls to add
1923 * @num_controls: number of elements in the array
1925 * Return 0 for success, else error.
1927 int snd_soc_add_card_controls(struct snd_soc_card *soc_card,
1928 const struct snd_kcontrol_new *controls, int num_controls)
1930 struct snd_card *card = soc_card->snd_card;
1932 return snd_soc_add_controls(card, soc_card->dev, controls, num_controls,
1935 EXPORT_SYMBOL_GPL(snd_soc_add_card_controls);
1938 * snd_soc_add_dai_controls - add an array of controls to a DAI.
1939 * Convienience function to add a list of controls.
1941 * @dai: DAI to add controls to
1942 * @controls: array of controls to add
1943 * @num_controls: number of elements in the array
1945 * Return 0 for success, else error.
1947 int snd_soc_add_dai_controls(struct snd_soc_dai *dai,
1948 const struct snd_kcontrol_new *controls, int num_controls)
1950 struct snd_card *card = dai->component->card->snd_card;
1952 return snd_soc_add_controls(card, dai->dev, controls, num_controls,
1955 EXPORT_SYMBOL_GPL(snd_soc_add_dai_controls);
1958 * snd_soc_dai_set_sysclk - configure DAI system or master clock.
1960 * @clk_id: DAI specific clock ID
1961 * @freq: new clock frequency in Hz
1962 * @dir: new clock direction - input/output.
1964 * Configures the DAI master (MCLK) or system (SYSCLK) clocking.
1966 int snd_soc_dai_set_sysclk(struct snd_soc_dai *dai, int clk_id,
1967 unsigned int freq, int dir)
1969 if (dai->driver && dai->driver->ops->set_sysclk)
1970 return dai->driver->ops->set_sysclk(dai, clk_id, freq, dir);
1971 else if (dai->codec && dai->codec->driver->set_sysclk)
1972 return dai->codec->driver->set_sysclk(dai->codec, clk_id, 0,
1977 EXPORT_SYMBOL_GPL(snd_soc_dai_set_sysclk);
1980 * snd_soc_codec_set_sysclk - configure CODEC system or master clock.
1982 * @clk_id: DAI specific clock ID
1983 * @source: Source for the clock
1984 * @freq: new clock frequency in Hz
1985 * @dir: new clock direction - input/output.
1987 * Configures the CODEC master (MCLK) or system (SYSCLK) clocking.
1989 int snd_soc_codec_set_sysclk(struct snd_soc_codec *codec, int clk_id,
1990 int source, unsigned int freq, int dir)
1992 if (codec->driver->set_sysclk)
1993 return codec->driver->set_sysclk(codec, clk_id, source,
1998 EXPORT_SYMBOL_GPL(snd_soc_codec_set_sysclk);
2001 * snd_soc_dai_set_clkdiv - configure DAI clock dividers.
2003 * @div_id: DAI specific clock divider ID
2004 * @div: new clock divisor.
2006 * Configures the clock dividers. This is used to derive the best DAI bit and
2007 * frame clocks from the system or master clock. It's best to set the DAI bit
2008 * and frame clocks as low as possible to save system power.
2010 int snd_soc_dai_set_clkdiv(struct snd_soc_dai *dai,
2011 int div_id, int div)
2013 if (dai->driver && dai->driver->ops->set_clkdiv)
2014 return dai->driver->ops->set_clkdiv(dai, div_id, div);
2018 EXPORT_SYMBOL_GPL(snd_soc_dai_set_clkdiv);
2021 * snd_soc_dai_set_pll - configure DAI PLL.
2023 * @pll_id: DAI specific PLL ID
2024 * @source: DAI specific source for the PLL
2025 * @freq_in: PLL input clock frequency in Hz
2026 * @freq_out: requested PLL output clock frequency in Hz
2028 * Configures and enables PLL to generate output clock based on input clock.
2030 int snd_soc_dai_set_pll(struct snd_soc_dai *dai, int pll_id, int source,
2031 unsigned int freq_in, unsigned int freq_out)
2033 if (dai->driver && dai->driver->ops->set_pll)
2034 return dai->driver->ops->set_pll(dai, pll_id, source,
2036 else if (dai->codec && dai->codec->driver->set_pll)
2037 return dai->codec->driver->set_pll(dai->codec, pll_id, source,
2042 EXPORT_SYMBOL_GPL(snd_soc_dai_set_pll);
2045 * snd_soc_codec_set_pll - configure codec PLL.
2047 * @pll_id: DAI specific PLL ID
2048 * @source: DAI specific source for the PLL
2049 * @freq_in: PLL input clock frequency in Hz
2050 * @freq_out: requested PLL output clock frequency in Hz
2052 * Configures and enables PLL to generate output clock based on input clock.
2054 int snd_soc_codec_set_pll(struct snd_soc_codec *codec, int pll_id, int source,
2055 unsigned int freq_in, unsigned int freq_out)
2057 if (codec->driver->set_pll)
2058 return codec->driver->set_pll(codec, pll_id, source,
2063 EXPORT_SYMBOL_GPL(snd_soc_codec_set_pll);
2066 * snd_soc_dai_set_bclk_ratio - configure BCLK to sample rate ratio.
2068 * @ratio Ratio of BCLK to Sample rate.
2070 * Configures the DAI for a preset BCLK to sample rate ratio.
2072 int snd_soc_dai_set_bclk_ratio(struct snd_soc_dai *dai, unsigned int ratio)
2074 if (dai->driver && dai->driver->ops->set_bclk_ratio)
2075 return dai->driver->ops->set_bclk_ratio(dai, ratio);
2079 EXPORT_SYMBOL_GPL(snd_soc_dai_set_bclk_ratio);
2082 * snd_soc_dai_set_fmt - configure DAI hardware audio format.
2084 * @fmt: SND_SOC_DAIFMT_ format value.
2086 * Configures the DAI hardware format and clocking.
2088 int snd_soc_dai_set_fmt(struct snd_soc_dai *dai, unsigned int fmt)
2090 if (dai->driver == NULL)
2092 if (dai->driver->ops->set_fmt == NULL)
2094 return dai->driver->ops->set_fmt(dai, fmt);
2096 EXPORT_SYMBOL_GPL(snd_soc_dai_set_fmt);
2099 * snd_soc_xlate_tdm_slot - generate tx/rx slot mask.
2100 * @slots: Number of slots in use.
2101 * @tx_mask: bitmask representing active TX slots.
2102 * @rx_mask: bitmask representing active RX slots.
2104 * Generates the TDM tx and rx slot default masks for DAI.
2106 static int snd_soc_xlate_tdm_slot_mask(unsigned int slots,
2107 unsigned int *tx_mask,
2108 unsigned int *rx_mask)
2110 if (*tx_mask || *rx_mask)
2116 *tx_mask = (1 << slots) - 1;
2117 *rx_mask = (1 << slots) - 1;
2123 * snd_soc_dai_set_tdm_slot - configure DAI TDM.
2125 * @tx_mask: bitmask representing active TX slots.
2126 * @rx_mask: bitmask representing active RX slots.
2127 * @slots: Number of slots in use.
2128 * @slot_width: Width in bits for each slot.
2130 * Configures a DAI for TDM operation. Both mask and slots are codec and DAI
2133 int snd_soc_dai_set_tdm_slot(struct snd_soc_dai *dai,
2134 unsigned int tx_mask, unsigned int rx_mask, int slots, int slot_width)
2136 if (dai->driver && dai->driver->ops->xlate_tdm_slot_mask)
2137 dai->driver->ops->xlate_tdm_slot_mask(slots,
2138 &tx_mask, &rx_mask);
2140 snd_soc_xlate_tdm_slot_mask(slots, &tx_mask, &rx_mask);
2142 dai->tx_mask = tx_mask;
2143 dai->rx_mask = rx_mask;
2145 if (dai->driver && dai->driver->ops->set_tdm_slot)
2146 return dai->driver->ops->set_tdm_slot(dai, tx_mask, rx_mask,
2151 EXPORT_SYMBOL_GPL(snd_soc_dai_set_tdm_slot);
2154 * snd_soc_dai_set_channel_map - configure DAI audio channel map
2156 * @tx_num: how many TX channels
2157 * @tx_slot: pointer to an array which imply the TX slot number channel
2159 * @rx_num: how many RX channels
2160 * @rx_slot: pointer to an array which imply the RX slot number channel
2163 * configure the relationship between channel number and TDM slot number.
2165 int snd_soc_dai_set_channel_map(struct snd_soc_dai *dai,
2166 unsigned int tx_num, unsigned int *tx_slot,
2167 unsigned int rx_num, unsigned int *rx_slot)
2169 if (dai->driver && dai->driver->ops->set_channel_map)
2170 return dai->driver->ops->set_channel_map(dai, tx_num, tx_slot,
2175 EXPORT_SYMBOL_GPL(snd_soc_dai_set_channel_map);
2178 * snd_soc_dai_set_tristate - configure DAI system or master clock.
2180 * @tristate: tristate enable
2182 * Tristates the DAI so that others can use it.
2184 int snd_soc_dai_set_tristate(struct snd_soc_dai *dai, int tristate)
2186 if (dai->driver && dai->driver->ops->set_tristate)
2187 return dai->driver->ops->set_tristate(dai, tristate);
2191 EXPORT_SYMBOL_GPL(snd_soc_dai_set_tristate);
2194 * snd_soc_dai_digital_mute - configure DAI system or master clock.
2196 * @mute: mute enable
2197 * @direction: stream to mute
2199 * Mutes the DAI DAC.
2201 int snd_soc_dai_digital_mute(struct snd_soc_dai *dai, int mute,
2207 if (dai->driver->ops->mute_stream)
2208 return dai->driver->ops->mute_stream(dai, mute, direction);
2209 else if (direction == SNDRV_PCM_STREAM_PLAYBACK &&
2210 dai->driver->ops->digital_mute)
2211 return dai->driver->ops->digital_mute(dai, mute);
2215 EXPORT_SYMBOL_GPL(snd_soc_dai_digital_mute);
2217 static int snd_soc_init_multicodec(struct snd_soc_card *card,
2218 struct snd_soc_dai_link *dai_link)
2220 /* Legacy codec/codec_dai link is a single entry in multicodec */
2221 if (dai_link->codec_name || dai_link->codec_of_node ||
2222 dai_link->codec_dai_name) {
2223 dai_link->num_codecs = 1;
2225 dai_link->codecs = devm_kzalloc(card->dev,
2226 sizeof(struct snd_soc_dai_link_component),
2228 if (!dai_link->codecs)
2231 dai_link->codecs[0].name = dai_link->codec_name;
2232 dai_link->codecs[0].of_node = dai_link->codec_of_node;
2233 dai_link->codecs[0].dai_name = dai_link->codec_dai_name;
2236 if (!dai_link->codecs) {
2237 dev_err(card->dev, "ASoC: DAI link has no CODECs\n");
2245 * snd_soc_register_card - Register a card with the ASoC core
2247 * @card: Card to register
2250 int snd_soc_register_card(struct snd_soc_card *card)
2254 if (!card->name || !card->dev)
2257 for (i = 0; i < card->num_links; i++) {
2258 struct snd_soc_dai_link *link = &card->dai_link[i];
2260 ret = snd_soc_init_multicodec(card, link);
2262 dev_err(card->dev, "ASoC: failed to init multicodec\n");
2266 for (j = 0; j < link->num_codecs; j++) {
2268 * Codec must be specified by 1 of name or OF node,
2269 * not both or neither.
2271 if (!!link->codecs[j].name ==
2272 !!link->codecs[j].of_node) {
2273 dev_err(card->dev, "ASoC: Neither/both codec name/of_node are set for %s\n",
2277 /* Codec DAI name must be specified */
2278 if (!link->codecs[j].dai_name) {
2279 dev_err(card->dev, "ASoC: codec_dai_name not set for %s\n",
2286 * Platform may be specified by either name or OF node, but
2287 * can be left unspecified, and a dummy platform will be used.
2289 if (link->platform_name && link->platform_of_node) {
2291 "ASoC: Both platform name/of_node are set for %s\n",
2297 * CPU device may be specified by either name or OF node, but
2298 * can be left unspecified, and will be matched based on DAI
2301 if (link->cpu_name && link->cpu_of_node) {
2303 "ASoC: Neither/both cpu name/of_node are set for %s\n",
2308 * At least one of CPU DAI name or CPU device name/node must be
2311 if (!link->cpu_dai_name &&
2312 !(link->cpu_name || link->cpu_of_node)) {
2314 "ASoC: Neither cpu_dai_name nor cpu_name/of_node are set for %s\n",
2320 dev_set_drvdata(card->dev, card);
2322 snd_soc_initialize_card_lists(card);
2324 soc_init_card_debugfs(card);
2326 card->rtd = devm_kzalloc(card->dev,
2327 sizeof(struct snd_soc_pcm_runtime) *
2328 (card->num_links + card->num_aux_devs),
2330 if (card->rtd == NULL)
2333 card->rtd_aux = &card->rtd[card->num_links];
2335 for (i = 0; i < card->num_links; i++) {
2336 card->rtd[i].card = card;
2337 card->rtd[i].dai_link = &card->dai_link[i];
2338 card->rtd[i].codec_dais = devm_kzalloc(card->dev,
2339 sizeof(struct snd_soc_dai *) *
2340 (card->rtd[i].dai_link->num_codecs),
2342 if (card->rtd[i].codec_dais == NULL)
2346 for (i = 0; i < card->num_aux_devs; i++)
2347 card->rtd_aux[i].card = card;
2349 INIT_LIST_HEAD(&card->dapm_dirty);
2350 card->instantiated = 0;
2351 mutex_init(&card->mutex);
2352 mutex_init(&card->dapm_mutex);
2354 ret = snd_soc_instantiate_card(card);
2356 soc_cleanup_card_debugfs(card);
2358 /* deactivate pins to sleep state */
2359 for (i = 0; i < card->num_rtd; i++) {
2360 struct snd_soc_pcm_runtime *rtd = &card->rtd[i];
2361 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
2364 for (j = 0; j < rtd->num_codecs; j++) {
2365 struct snd_soc_dai *codec_dai = rtd->codec_dais[j];
2366 if (!codec_dai->active)
2367 pinctrl_pm_select_sleep_state(codec_dai->dev);
2370 if (!cpu_dai->active)
2371 pinctrl_pm_select_sleep_state(cpu_dai->dev);
2376 EXPORT_SYMBOL_GPL(snd_soc_register_card);
2379 * snd_soc_unregister_card - Unregister a card with the ASoC core
2381 * @card: Card to unregister
2384 int snd_soc_unregister_card(struct snd_soc_card *card)
2386 if (card->instantiated) {
2387 card->instantiated = false;
2388 snd_soc_dapm_shutdown(card);
2389 soc_cleanup_card_resources(card);
2391 dev_dbg(card->dev, "ASoC: Unregistered card '%s'\n", card->name);
2395 EXPORT_SYMBOL_GPL(snd_soc_unregister_card);
2398 * Simplify DAI link configuration by removing ".-1" from device names
2399 * and sanitizing names.
2401 static char *fmt_single_name(struct device *dev, int *id)
2403 char *found, name[NAME_SIZE];
2406 if (dev_name(dev) == NULL)
2409 strlcpy(name, dev_name(dev), NAME_SIZE);
2411 /* are we a "%s.%d" name (platform and SPI components) */
2412 found = strstr(name, dev->driver->name);
2415 if (sscanf(&found[strlen(dev->driver->name)], ".%d", id) == 1) {
2417 /* discard ID from name if ID == -1 */
2419 found[strlen(dev->driver->name)] = '\0';
2423 /* I2C component devices are named "bus-addr" */
2424 if (sscanf(name, "%x-%x", &id1, &id2) == 2) {
2425 char tmp[NAME_SIZE];
2427 /* create unique ID number from I2C addr and bus */
2428 *id = ((id1 & 0xffff) << 16) + id2;
2430 /* sanitize component name for DAI link creation */
2431 snprintf(tmp, NAME_SIZE, "%s.%s", dev->driver->name, name);
2432 strlcpy(name, tmp, NAME_SIZE);
2437 return kstrdup(name, GFP_KERNEL);
2441 * Simplify DAI link naming for single devices with multiple DAIs by removing
2442 * any ".-1" and using the DAI name (instead of device name).
2444 static inline char *fmt_multiple_name(struct device *dev,
2445 struct snd_soc_dai_driver *dai_drv)
2447 if (dai_drv->name == NULL) {
2449 "ASoC: error - multiple DAI %s registered with no name\n",
2454 return kstrdup(dai_drv->name, GFP_KERNEL);
2458 * snd_soc_unregister_dai - Unregister DAIs from the ASoC core
2460 * @component: The component for which the DAIs should be unregistered
2462 static void snd_soc_unregister_dais(struct snd_soc_component *component)
2464 struct snd_soc_dai *dai, *_dai;
2466 list_for_each_entry_safe(dai, _dai, &component->dai_list, list) {
2467 dev_dbg(component->dev, "ASoC: Unregistered DAI '%s'\n",
2469 list_del(&dai->list);
2476 * snd_soc_register_dais - Register a DAI with the ASoC core
2478 * @component: The component the DAIs are registered for
2479 * @dai_drv: DAI driver to use for the DAIs
2480 * @count: Number of DAIs
2481 * @legacy_dai_naming: Use the legacy naming scheme and let the DAI inherit the
2484 static int snd_soc_register_dais(struct snd_soc_component *component,
2485 struct snd_soc_dai_driver *dai_drv, size_t count,
2486 bool legacy_dai_naming)
2488 struct device *dev = component->dev;
2489 struct snd_soc_dai *dai;
2493 dev_dbg(dev, "ASoC: dai register %s #%Zu\n", dev_name(dev), count);
2495 component->dai_drv = dai_drv;
2496 component->num_dai = count;
2498 for (i = 0; i < count; i++) {
2500 dai = kzalloc(sizeof(struct snd_soc_dai), GFP_KERNEL);
2507 * Back in the old days when we still had component-less DAIs,
2508 * instead of having a static name, component-less DAIs would
2509 * inherit the name of the parent device so it is possible to
2510 * register multiple instances of the DAI. We still need to keep
2511 * the same naming style even though those DAIs are not
2512 * component-less anymore.
2514 if (count == 1 && legacy_dai_naming) {
2515 dai->name = fmt_single_name(dev, &dai->id);
2517 dai->name = fmt_multiple_name(dev, &dai_drv[i]);
2519 dai->id = dai_drv[i].id;
2523 if (dai->name == NULL) {
2529 dai->component = component;
2531 dai->driver = &dai_drv[i];
2532 if (!dai->driver->ops)
2533 dai->driver->ops = &null_dai_ops;
2535 list_add(&dai->list, &component->dai_list);
2537 dev_dbg(dev, "ASoC: Registered DAI '%s'\n", dai->name);
2543 snd_soc_unregister_dais(component);
2548 static void snd_soc_component_seq_notifier(struct snd_soc_dapm_context *dapm,
2549 enum snd_soc_dapm_type type, int subseq)
2551 struct snd_soc_component *component = dapm->component;
2553 component->driver->seq_notifier(component, type, subseq);
2556 static int snd_soc_component_stream_event(struct snd_soc_dapm_context *dapm,
2559 struct snd_soc_component *component = dapm->component;
2561 return component->driver->stream_event(component, event);
2564 static int snd_soc_component_initialize(struct snd_soc_component *component,
2565 const struct snd_soc_component_driver *driver, struct device *dev)
2567 struct snd_soc_dapm_context *dapm;
2569 component->name = fmt_single_name(dev, &component->id);
2570 if (!component->name) {
2571 dev_err(dev, "ASoC: Failed to allocate name\n");
2575 component->dev = dev;
2576 component->driver = driver;
2577 component->probe = component->driver->probe;
2578 component->remove = component->driver->remove;
2580 if (!component->dapm_ptr)
2581 component->dapm_ptr = &component->dapm;
2583 dapm = component->dapm_ptr;
2585 dapm->component = component;
2586 dapm->bias_level = SND_SOC_BIAS_OFF;
2587 dapm->idle_bias_off = true;
2588 if (driver->seq_notifier)
2589 dapm->seq_notifier = snd_soc_component_seq_notifier;
2590 if (driver->stream_event)
2591 dapm->stream_event = snd_soc_component_stream_event;
2593 component->controls = driver->controls;
2594 component->num_controls = driver->num_controls;
2595 component->dapm_widgets = driver->dapm_widgets;
2596 component->num_dapm_widgets = driver->num_dapm_widgets;
2597 component->dapm_routes = driver->dapm_routes;
2598 component->num_dapm_routes = driver->num_dapm_routes;
2600 INIT_LIST_HEAD(&component->dai_list);
2601 mutex_init(&component->io_mutex);
2606 static void snd_soc_component_setup_regmap(struct snd_soc_component *component)
2608 int val_bytes = regmap_get_val_bytes(component->regmap);
2610 /* Errors are legitimate for non-integer byte multiples */
2612 component->val_bytes = val_bytes;
2615 #ifdef CONFIG_REGMAP
2618 * snd_soc_component_init_regmap() - Initialize regmap instance for the component
2619 * @component: The component for which to initialize the regmap instance
2620 * @regmap: The regmap instance that should be used by the component
2622 * This function allows deferred assignment of the regmap instance that is
2623 * associated with the component. Only use this if the regmap instance is not
2624 * yet ready when the component is registered. The function must also be called
2625 * before the first IO attempt of the component.
2627 void snd_soc_component_init_regmap(struct snd_soc_component *component,
2628 struct regmap *regmap)
2630 component->regmap = regmap;
2631 snd_soc_component_setup_regmap(component);
2633 EXPORT_SYMBOL_GPL(snd_soc_component_init_regmap);
2636 * snd_soc_component_exit_regmap() - De-initialize regmap instance for the component
2637 * @component: The component for which to de-initialize the regmap instance
2639 * Calls regmap_exit() on the regmap instance associated to the component and
2640 * removes the regmap instance from the component.
2642 * This function should only be used if snd_soc_component_init_regmap() was used
2643 * to initialize the regmap instance.
2645 void snd_soc_component_exit_regmap(struct snd_soc_component *component)
2647 regmap_exit(component->regmap);
2648 component->regmap = NULL;
2650 EXPORT_SYMBOL_GPL(snd_soc_component_exit_regmap);
2654 static void snd_soc_component_add_unlocked(struct snd_soc_component *component)
2656 if (!component->write && !component->read) {
2657 if (!component->regmap)
2658 component->regmap = dev_get_regmap(component->dev, NULL);
2659 if (component->regmap)
2660 snd_soc_component_setup_regmap(component);
2663 list_add(&component->list, &component_list);
2666 static void snd_soc_component_add(struct snd_soc_component *component)
2668 mutex_lock(&client_mutex);
2669 snd_soc_component_add_unlocked(component);
2670 mutex_unlock(&client_mutex);
2673 static void snd_soc_component_cleanup(struct snd_soc_component *component)
2675 snd_soc_unregister_dais(component);
2676 kfree(component->name);
2679 static void snd_soc_component_del_unlocked(struct snd_soc_component *component)
2681 list_del(&component->list);
2684 static void snd_soc_component_del(struct snd_soc_component *component)
2686 mutex_lock(&client_mutex);
2687 snd_soc_component_del_unlocked(component);
2688 mutex_unlock(&client_mutex);
2691 int snd_soc_register_component(struct device *dev,
2692 const struct snd_soc_component_driver *cmpnt_drv,
2693 struct snd_soc_dai_driver *dai_drv,
2696 struct snd_soc_component *cmpnt;
2699 cmpnt = kzalloc(sizeof(*cmpnt), GFP_KERNEL);
2701 dev_err(dev, "ASoC: Failed to allocate memory\n");
2705 ret = snd_soc_component_initialize(cmpnt, cmpnt_drv, dev);
2709 cmpnt->ignore_pmdown_time = true;
2710 cmpnt->registered_as_component = true;
2712 ret = snd_soc_register_dais(cmpnt, dai_drv, num_dai, true);
2714 dev_err(dev, "ASoC: Failed to regster DAIs: %d\n", ret);
2718 snd_soc_component_add(cmpnt);
2723 snd_soc_component_cleanup(cmpnt);
2728 EXPORT_SYMBOL_GPL(snd_soc_register_component);
2731 * snd_soc_unregister_component - Unregister a component from the ASoC core
2734 void snd_soc_unregister_component(struct device *dev)
2736 struct snd_soc_component *cmpnt;
2738 list_for_each_entry(cmpnt, &component_list, list) {
2739 if (dev == cmpnt->dev && cmpnt->registered_as_component)
2745 snd_soc_component_del(cmpnt);
2746 snd_soc_component_cleanup(cmpnt);
2749 EXPORT_SYMBOL_GPL(snd_soc_unregister_component);
2751 static int snd_soc_platform_drv_probe(struct snd_soc_component *component)
2753 struct snd_soc_platform *platform = snd_soc_component_to_platform(component);
2755 return platform->driver->probe(platform);
2758 static void snd_soc_platform_drv_remove(struct snd_soc_component *component)
2760 struct snd_soc_platform *platform = snd_soc_component_to_platform(component);
2762 platform->driver->remove(platform);
2766 * snd_soc_add_platform - Add a platform to the ASoC core
2767 * @dev: The parent device for the platform
2768 * @platform: The platform to add
2769 * @platform_driver: The driver for the platform
2771 int snd_soc_add_platform(struct device *dev, struct snd_soc_platform *platform,
2772 const struct snd_soc_platform_driver *platform_drv)
2776 ret = snd_soc_component_initialize(&platform->component,
2777 &platform_drv->component_driver, dev);
2781 platform->dev = dev;
2782 platform->driver = platform_drv;
2784 if (platform_drv->probe)
2785 platform->component.probe = snd_soc_platform_drv_probe;
2786 if (platform_drv->remove)
2787 platform->component.remove = snd_soc_platform_drv_remove;
2789 #ifdef CONFIG_DEBUG_FS
2790 platform->component.debugfs_prefix = "platform";
2793 mutex_lock(&client_mutex);
2794 snd_soc_component_add_unlocked(&platform->component);
2795 list_add(&platform->list, &platform_list);
2796 mutex_unlock(&client_mutex);
2798 dev_dbg(dev, "ASoC: Registered platform '%s'\n",
2799 platform->component.name);
2803 EXPORT_SYMBOL_GPL(snd_soc_add_platform);
2806 * snd_soc_register_platform - Register a platform with the ASoC core
2808 * @platform: platform to register
2810 int snd_soc_register_platform(struct device *dev,
2811 const struct snd_soc_platform_driver *platform_drv)
2813 struct snd_soc_platform *platform;
2816 dev_dbg(dev, "ASoC: platform register %s\n", dev_name(dev));
2818 platform = kzalloc(sizeof(struct snd_soc_platform), GFP_KERNEL);
2819 if (platform == NULL)
2822 ret = snd_soc_add_platform(dev, platform, platform_drv);
2828 EXPORT_SYMBOL_GPL(snd_soc_register_platform);
2831 * snd_soc_remove_platform - Remove a platform from the ASoC core
2832 * @platform: the platform to remove
2834 void snd_soc_remove_platform(struct snd_soc_platform *platform)
2837 mutex_lock(&client_mutex);
2838 list_del(&platform->list);
2839 snd_soc_component_del_unlocked(&platform->component);
2840 mutex_unlock(&client_mutex);
2842 dev_dbg(platform->dev, "ASoC: Unregistered platform '%s'\n",
2843 platform->component.name);
2845 snd_soc_component_cleanup(&platform->component);
2847 EXPORT_SYMBOL_GPL(snd_soc_remove_platform);
2849 struct snd_soc_platform *snd_soc_lookup_platform(struct device *dev)
2851 struct snd_soc_platform *platform;
2853 list_for_each_entry(platform, &platform_list, list) {
2854 if (dev == platform->dev)
2860 EXPORT_SYMBOL_GPL(snd_soc_lookup_platform);
2863 * snd_soc_unregister_platform - Unregister a platform from the ASoC core
2865 * @platform: platform to unregister
2867 void snd_soc_unregister_platform(struct device *dev)
2869 struct snd_soc_platform *platform;
2871 platform = snd_soc_lookup_platform(dev);
2875 snd_soc_remove_platform(platform);
2878 EXPORT_SYMBOL_GPL(snd_soc_unregister_platform);
2880 static u64 codec_format_map[] = {
2881 SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S16_BE,
2882 SNDRV_PCM_FMTBIT_U16_LE | SNDRV_PCM_FMTBIT_U16_BE,
2883 SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S24_BE,
2884 SNDRV_PCM_FMTBIT_U24_LE | SNDRV_PCM_FMTBIT_U24_BE,
2885 SNDRV_PCM_FMTBIT_S32_LE | SNDRV_PCM_FMTBIT_S32_BE,
2886 SNDRV_PCM_FMTBIT_U32_LE | SNDRV_PCM_FMTBIT_U32_BE,
2887 SNDRV_PCM_FMTBIT_S24_3LE | SNDRV_PCM_FMTBIT_U24_3BE,
2888 SNDRV_PCM_FMTBIT_U24_3LE | SNDRV_PCM_FMTBIT_U24_3BE,
2889 SNDRV_PCM_FMTBIT_S20_3LE | SNDRV_PCM_FMTBIT_S20_3BE,
2890 SNDRV_PCM_FMTBIT_U20_3LE | SNDRV_PCM_FMTBIT_U20_3BE,
2891 SNDRV_PCM_FMTBIT_S18_3LE | SNDRV_PCM_FMTBIT_S18_3BE,
2892 SNDRV_PCM_FMTBIT_U18_3LE | SNDRV_PCM_FMTBIT_U18_3BE,
2893 SNDRV_PCM_FMTBIT_FLOAT_LE | SNDRV_PCM_FMTBIT_FLOAT_BE,
2894 SNDRV_PCM_FMTBIT_FLOAT64_LE | SNDRV_PCM_FMTBIT_FLOAT64_BE,
2895 SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_LE
2896 | SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_BE,
2899 /* Fix up the DAI formats for endianness: codecs don't actually see
2900 * the endianness of the data but we're using the CPU format
2901 * definitions which do need to include endianness so we ensure that
2902 * codec DAIs always have both big and little endian variants set.
2904 static void fixup_codec_formats(struct snd_soc_pcm_stream *stream)
2908 for (i = 0; i < ARRAY_SIZE(codec_format_map); i++)
2909 if (stream->formats & codec_format_map[i])
2910 stream->formats |= codec_format_map[i];
2913 static int snd_soc_codec_drv_probe(struct snd_soc_component *component)
2915 struct snd_soc_codec *codec = snd_soc_component_to_codec(component);
2917 return codec->driver->probe(codec);
2920 static void snd_soc_codec_drv_remove(struct snd_soc_component *component)
2922 struct snd_soc_codec *codec = snd_soc_component_to_codec(component);
2924 codec->driver->remove(codec);
2927 static int snd_soc_codec_drv_write(struct snd_soc_component *component,
2928 unsigned int reg, unsigned int val)
2930 struct snd_soc_codec *codec = snd_soc_component_to_codec(component);
2932 return codec->driver->write(codec, reg, val);
2935 static int snd_soc_codec_drv_read(struct snd_soc_component *component,
2936 unsigned int reg, unsigned int *val)
2938 struct snd_soc_codec *codec = snd_soc_component_to_codec(component);
2940 *val = codec->driver->read(codec, reg);
2945 static int snd_soc_codec_set_bias_level(struct snd_soc_dapm_context *dapm,
2946 enum snd_soc_bias_level level)
2948 struct snd_soc_codec *codec = snd_soc_dapm_to_codec(dapm);
2950 return codec->driver->set_bias_level(codec, level);
2954 * snd_soc_register_codec - Register a codec with the ASoC core
2956 * @codec: codec to register
2958 int snd_soc_register_codec(struct device *dev,
2959 const struct snd_soc_codec_driver *codec_drv,
2960 struct snd_soc_dai_driver *dai_drv,
2963 struct snd_soc_codec *codec;
2964 struct snd_soc_dai *dai;
2967 dev_dbg(dev, "codec register %s\n", dev_name(dev));
2969 codec = kzalloc(sizeof(struct snd_soc_codec), GFP_KERNEL);
2973 codec->component.dapm_ptr = &codec->dapm;
2974 codec->component.codec = codec;
2976 ret = snd_soc_component_initialize(&codec->component,
2977 &codec_drv->component_driver, dev);
2981 if (codec_drv->controls) {
2982 codec->component.controls = codec_drv->controls;
2983 codec->component.num_controls = codec_drv->num_controls;
2985 if (codec_drv->dapm_widgets) {
2986 codec->component.dapm_widgets = codec_drv->dapm_widgets;
2987 codec->component.num_dapm_widgets = codec_drv->num_dapm_widgets;
2989 if (codec_drv->dapm_routes) {
2990 codec->component.dapm_routes = codec_drv->dapm_routes;
2991 codec->component.num_dapm_routes = codec_drv->num_dapm_routes;
2994 if (codec_drv->probe)
2995 codec->component.probe = snd_soc_codec_drv_probe;
2996 if (codec_drv->remove)
2997 codec->component.remove = snd_soc_codec_drv_remove;
2998 if (codec_drv->write)
2999 codec->component.write = snd_soc_codec_drv_write;
3000 if (codec_drv->read)
3001 codec->component.read = snd_soc_codec_drv_read;
3002 codec->component.ignore_pmdown_time = codec_drv->ignore_pmdown_time;
3003 codec->dapm.idle_bias_off = codec_drv->idle_bias_off;
3004 codec->dapm.suspend_bias_off = codec_drv->suspend_bias_off;
3005 if (codec_drv->seq_notifier)
3006 codec->dapm.seq_notifier = codec_drv->seq_notifier;
3007 if (codec_drv->set_bias_level)
3008 codec->dapm.set_bias_level = snd_soc_codec_set_bias_level;
3010 codec->driver = codec_drv;
3011 codec->component.val_bytes = codec_drv->reg_word_size;
3013 #ifdef CONFIG_DEBUG_FS
3014 codec->component.init_debugfs = soc_init_codec_debugfs;
3015 codec->component.debugfs_prefix = "codec";
3018 if (codec_drv->get_regmap)
3019 codec->component.regmap = codec_drv->get_regmap(dev);
3021 for (i = 0; i < num_dai; i++) {
3022 fixup_codec_formats(&dai_drv[i].playback);
3023 fixup_codec_formats(&dai_drv[i].capture);
3026 ret = snd_soc_register_dais(&codec->component, dai_drv, num_dai, false);
3028 dev_err(dev, "ASoC: Failed to regster DAIs: %d\n", ret);
3032 list_for_each_entry(dai, &codec->component.dai_list, list)
3035 mutex_lock(&client_mutex);
3036 snd_soc_component_add_unlocked(&codec->component);
3037 list_add(&codec->list, &codec_list);
3038 mutex_unlock(&client_mutex);
3040 dev_dbg(codec->dev, "ASoC: Registered codec '%s'\n",
3041 codec->component.name);
3045 snd_soc_component_cleanup(&codec->component);
3050 EXPORT_SYMBOL_GPL(snd_soc_register_codec);
3053 * snd_soc_unregister_codec - Unregister a codec from the ASoC core
3055 * @codec: codec to unregister
3057 void snd_soc_unregister_codec(struct device *dev)
3059 struct snd_soc_codec *codec;
3061 list_for_each_entry(codec, &codec_list, list) {
3062 if (dev == codec->dev)
3069 mutex_lock(&client_mutex);
3070 list_del(&codec->list);
3071 snd_soc_component_del_unlocked(&codec->component);
3072 mutex_unlock(&client_mutex);
3074 dev_dbg(codec->dev, "ASoC: Unregistered codec '%s'\n",
3075 codec->component.name);
3077 snd_soc_component_cleanup(&codec->component);
3078 snd_soc_cache_exit(codec);
3081 EXPORT_SYMBOL_GPL(snd_soc_unregister_codec);
3083 /* Retrieve a card's name from device tree */
3084 int snd_soc_of_parse_card_name(struct snd_soc_card *card,
3085 const char *propname)
3087 struct device_node *np;
3091 pr_err("card->dev is not set before calling %s\n", __func__);
3095 np = card->dev->of_node;
3097 ret = of_property_read_string_index(np, propname, 0, &card->name);
3099 * EINVAL means the property does not exist. This is fine providing
3100 * card->name was previously set, which is checked later in
3101 * snd_soc_register_card.
3103 if (ret < 0 && ret != -EINVAL) {
3105 "ASoC: Property '%s' could not be read: %d\n",
3112 EXPORT_SYMBOL_GPL(snd_soc_of_parse_card_name);
3114 static const struct snd_soc_dapm_widget simple_widgets[] = {
3115 SND_SOC_DAPM_MIC("Microphone", NULL),
3116 SND_SOC_DAPM_LINE("Line", NULL),
3117 SND_SOC_DAPM_HP("Headphone", NULL),
3118 SND_SOC_DAPM_SPK("Speaker", NULL),
3121 int snd_soc_of_parse_audio_simple_widgets(struct snd_soc_card *card,
3122 const char *propname)
3124 struct device_node *np = card->dev->of_node;
3125 struct snd_soc_dapm_widget *widgets;
3126 const char *template, *wname;
3127 int i, j, num_widgets, ret;
3129 num_widgets = of_property_count_strings(np, propname);
3130 if (num_widgets < 0) {
3132 "ASoC: Property '%s' does not exist\n", propname);
3135 if (num_widgets & 1) {
3137 "ASoC: Property '%s' length is not even\n", propname);
3143 dev_err(card->dev, "ASoC: Property '%s's length is zero\n",
3148 widgets = devm_kcalloc(card->dev, num_widgets, sizeof(*widgets),
3152 "ASoC: Could not allocate memory for widgets\n");
3156 for (i = 0; i < num_widgets; i++) {
3157 ret = of_property_read_string_index(np, propname,
3161 "ASoC: Property '%s' index %d read error:%d\n",
3162 propname, 2 * i, ret);
3166 for (j = 0; j < ARRAY_SIZE(simple_widgets); j++) {
3167 if (!strncmp(template, simple_widgets[j].name,
3168 strlen(simple_widgets[j].name))) {
3169 widgets[i] = simple_widgets[j];
3174 if (j >= ARRAY_SIZE(simple_widgets)) {
3176 "ASoC: DAPM widget '%s' is not supported\n",
3181 ret = of_property_read_string_index(np, propname,
3186 "ASoC: Property '%s' index %d read error:%d\n",
3187 propname, (2 * i) + 1, ret);
3191 widgets[i].name = wname;
3194 card->dapm_widgets = widgets;
3195 card->num_dapm_widgets = num_widgets;
3199 EXPORT_SYMBOL_GPL(snd_soc_of_parse_audio_simple_widgets);
3201 int snd_soc_of_parse_tdm_slot(struct device_node *np,
3202 unsigned int *slots,
3203 unsigned int *slot_width)
3208 if (of_property_read_bool(np, "dai-tdm-slot-num")) {
3209 ret = of_property_read_u32(np, "dai-tdm-slot-num", &val);
3217 if (of_property_read_bool(np, "dai-tdm-slot-width")) {
3218 ret = of_property_read_u32(np, "dai-tdm-slot-width", &val);
3228 EXPORT_SYMBOL_GPL(snd_soc_of_parse_tdm_slot);
3230 int snd_soc_of_parse_audio_routing(struct snd_soc_card *card,
3231 const char *propname)
3233 struct device_node *np = card->dev->of_node;
3234 int num_routes, old_routes;
3235 struct snd_soc_dapm_route *routes;
3238 num_routes = of_property_count_strings(np, propname);
3239 if (num_routes < 0 || num_routes & 1) {
3241 "ASoC: Property '%s' does not exist or its length is not even\n",
3247 dev_err(card->dev, "ASoC: Property '%s's length is zero\n",
3252 old_routes = card->num_dapm_routes;
3253 routes = devm_kzalloc(card->dev,
3254 (old_routes + num_routes) * sizeof(*routes),
3258 "ASoC: Could not allocate DAPM route table\n");
3262 memcpy(routes, card->dapm_routes, old_routes * sizeof(*routes));
3264 for (i = 0; i < num_routes; i++) {
3265 ret = of_property_read_string_index(np, propname,
3266 2 * i, &routes[old_routes + i].sink);
3269 "ASoC: Property '%s' index %d could not be read: %d\n",
3270 propname, 2 * i, ret);
3273 ret = of_property_read_string_index(np, propname,
3274 (2 * i) + 1, &routes[old_routes + i].source);
3277 "ASoC: Property '%s' index %d could not be read: %d\n",
3278 propname, (2 * i) + 1, ret);
3283 card->num_dapm_routes += num_routes;
3284 card->dapm_routes = routes;
3288 EXPORT_SYMBOL_GPL(snd_soc_of_parse_audio_routing);
3290 unsigned int snd_soc_of_parse_daifmt(struct device_node *np,
3292 struct device_node **bitclkmaster,
3293 struct device_node **framemaster)
3297 unsigned int format = 0;
3303 } of_fmt_table[] = {
3304 { "i2s", SND_SOC_DAIFMT_I2S },
3305 { "right_j", SND_SOC_DAIFMT_RIGHT_J },
3306 { "left_j", SND_SOC_DAIFMT_LEFT_J },
3307 { "dsp_a", SND_SOC_DAIFMT_DSP_A },
3308 { "dsp_b", SND_SOC_DAIFMT_DSP_B },
3309 { "ac97", SND_SOC_DAIFMT_AC97 },
3310 { "pdm", SND_SOC_DAIFMT_PDM},
3311 { "msb", SND_SOC_DAIFMT_MSB },
3312 { "lsb", SND_SOC_DAIFMT_LSB },
3319 * check "[prefix]format = xxx"
3320 * SND_SOC_DAIFMT_FORMAT_MASK area
3322 snprintf(prop, sizeof(prop), "%sformat", prefix);
3323 ret = of_property_read_string(np, prop, &str);
3325 for (i = 0; i < ARRAY_SIZE(of_fmt_table); i++) {
3326 if (strcmp(str, of_fmt_table[i].name) == 0) {
3327 format |= of_fmt_table[i].val;
3334 * check "[prefix]continuous-clock"
3335 * SND_SOC_DAIFMT_CLOCK_MASK area
3337 snprintf(prop, sizeof(prop), "%scontinuous-clock", prefix);
3338 if (of_get_property(np, prop, NULL))
3339 format |= SND_SOC_DAIFMT_CONT;
3341 format |= SND_SOC_DAIFMT_GATED;
3344 * check "[prefix]bitclock-inversion"
3345 * check "[prefix]frame-inversion"
3346 * SND_SOC_DAIFMT_INV_MASK area
3348 snprintf(prop, sizeof(prop), "%sbitclock-inversion", prefix);
3349 bit = !!of_get_property(np, prop, NULL);
3351 snprintf(prop, sizeof(prop), "%sframe-inversion", prefix);
3352 frame = !!of_get_property(np, prop, NULL);
3354 switch ((bit << 4) + frame) {
3356 format |= SND_SOC_DAIFMT_IB_IF;
3359 format |= SND_SOC_DAIFMT_IB_NF;
3362 format |= SND_SOC_DAIFMT_NB_IF;
3365 /* SND_SOC_DAIFMT_NB_NF is default */
3370 * check "[prefix]bitclock-master"
3371 * check "[prefix]frame-master"
3372 * SND_SOC_DAIFMT_MASTER_MASK area
3374 snprintf(prop, sizeof(prop), "%sbitclock-master", prefix);
3375 bit = !!of_get_property(np, prop, NULL);
3376 if (bit && bitclkmaster)
3377 *bitclkmaster = of_parse_phandle(np, prop, 0);
3379 snprintf(prop, sizeof(prop), "%sframe-master", prefix);
3380 frame = !!of_get_property(np, prop, NULL);
3381 if (frame && framemaster)
3382 *framemaster = of_parse_phandle(np, prop, 0);
3384 switch ((bit << 4) + frame) {
3386 format |= SND_SOC_DAIFMT_CBM_CFM;
3389 format |= SND_SOC_DAIFMT_CBM_CFS;
3392 format |= SND_SOC_DAIFMT_CBS_CFM;
3395 format |= SND_SOC_DAIFMT_CBS_CFS;
3401 EXPORT_SYMBOL_GPL(snd_soc_of_parse_daifmt);
3403 static int snd_soc_get_dai_name(struct of_phandle_args *args,
3404 const char **dai_name)
3406 struct snd_soc_component *pos;
3407 int ret = -EPROBE_DEFER;
3409 mutex_lock(&client_mutex);
3410 list_for_each_entry(pos, &component_list, list) {
3411 if (pos->dev->of_node != args->np)
3414 if (pos->driver->of_xlate_dai_name) {
3415 ret = pos->driver->of_xlate_dai_name(pos,
3421 switch (args->args_count) {
3423 id = 0; /* same as dai_drv[0] */
3433 if (id < 0 || id >= pos->num_dai) {
3440 *dai_name = pos->dai_drv[id].name;
3442 *dai_name = pos->name;
3447 mutex_unlock(&client_mutex);
3451 int snd_soc_of_get_dai_name(struct device_node *of_node,
3452 const char **dai_name)
3454 struct of_phandle_args args;
3457 ret = of_parse_phandle_with_args(of_node, "sound-dai",
3458 "#sound-dai-cells", 0, &args);
3462 ret = snd_soc_get_dai_name(&args, dai_name);
3464 of_node_put(args.np);
3468 EXPORT_SYMBOL_GPL(snd_soc_of_get_dai_name);
3471 * snd_soc_of_get_dai_link_codecs - Parse a list of CODECs in the devicetree
3473 * @of_node: Device node
3474 * @dai_link: DAI link
3476 * Builds an array of CODEC DAI components from the DAI link property
3478 * The array is set in the DAI link and the number of DAIs is set accordingly.
3479 * The device nodes in the array (of_node) must be dereferenced by the caller.
3481 * Returns 0 for success
3483 int snd_soc_of_get_dai_link_codecs(struct device *dev,
3484 struct device_node *of_node,
3485 struct snd_soc_dai_link *dai_link)
3487 struct of_phandle_args args;
3488 struct snd_soc_dai_link_component *component;
3490 int index, num_codecs, ret;
3492 /* Count the number of CODECs */
3494 num_codecs = of_count_phandle_with_args(of_node, name,
3495 "#sound-dai-cells");
3496 if (num_codecs <= 0) {
3497 if (num_codecs == -ENOENT)
3498 dev_err(dev, "No 'sound-dai' property\n");
3500 dev_err(dev, "Bad phandle in 'sound-dai'\n");
3503 component = devm_kzalloc(dev,
3504 sizeof *component * num_codecs,
3508 dai_link->codecs = component;
3509 dai_link->num_codecs = num_codecs;
3511 /* Parse the list */
3512 for (index = 0, component = dai_link->codecs;
3513 index < dai_link->num_codecs;
3514 index++, component++) {
3515 ret = of_parse_phandle_with_args(of_node, name,
3520 component->of_node = args.np;
3521 ret = snd_soc_get_dai_name(&args, &component->dai_name);
3527 for (index = 0, component = dai_link->codecs;
3528 index < dai_link->num_codecs;
3529 index++, component++) {
3530 if (!component->of_node)
3532 of_node_put(component->of_node);
3533 component->of_node = NULL;
3535 dai_link->codecs = NULL;
3536 dai_link->num_codecs = 0;
3539 EXPORT_SYMBOL_GPL(snd_soc_of_get_dai_link_codecs);
3541 static int __init snd_soc_init(void)
3543 #ifdef CONFIG_DEBUG_FS
3544 snd_soc_debugfs_root = debugfs_create_dir("asoc", NULL);
3545 if (IS_ERR(snd_soc_debugfs_root) || !snd_soc_debugfs_root) {
3546 pr_warn("ASoC: Failed to create debugfs directory\n");
3547 snd_soc_debugfs_root = NULL;
3550 if (!debugfs_create_file("codecs", 0444, snd_soc_debugfs_root, NULL,
3552 pr_warn("ASoC: Failed to create CODEC list debugfs file\n");
3554 if (!debugfs_create_file("dais", 0444, snd_soc_debugfs_root, NULL,
3556 pr_warn("ASoC: Failed to create DAI list debugfs file\n");
3558 if (!debugfs_create_file("platforms", 0444, snd_soc_debugfs_root, NULL,
3559 &platform_list_fops))
3560 pr_warn("ASoC: Failed to create platform list debugfs file\n");
3563 snd_soc_util_init();
3565 return platform_driver_register(&soc_driver);
3567 module_init(snd_soc_init);
3569 static void __exit snd_soc_exit(void)
3571 snd_soc_util_exit();
3573 #ifdef CONFIG_DEBUG_FS
3574 debugfs_remove_recursive(snd_soc_debugfs_root);
3576 platform_driver_unregister(&soc_driver);
3578 module_exit(snd_soc_exit);
3580 /* Module information */
3581 MODULE_AUTHOR("Liam Girdwood, lrg@slimlogic.co.uk");
3582 MODULE_DESCRIPTION("ALSA SoC Core");
3583 MODULE_LICENSE("GPL");
3584 MODULE_ALIAS("platform:soc-audio");