1873486edSKuninori Morimoto // SPDX-License-Identifier: GPL-2.0+ 2873486edSKuninori Morimoto // 3873486edSKuninori Morimoto // soc-core.c -- ALSA SoC Audio Layer 4873486edSKuninori Morimoto // 5873486edSKuninori Morimoto // Copyright 2005 Wolfson Microelectronics PLC. 6873486edSKuninori Morimoto // Copyright 2005 Openedhand Ltd. 7873486edSKuninori Morimoto // Copyright (C) 2010 Slimlogic Ltd. 8873486edSKuninori Morimoto // Copyright (C) 2010 Texas Instruments Inc. 9873486edSKuninori Morimoto // 10873486edSKuninori Morimoto // Author: Liam Girdwood <lrg@slimlogic.co.uk> 11873486edSKuninori Morimoto // with code, comments and ideas from :- 12873486edSKuninori Morimoto // Richard Purdie <richard@openedhand.com> 13873486edSKuninori Morimoto // 14873486edSKuninori Morimoto // TODO: 15873486edSKuninori Morimoto // o Add hw rules to enforce rates, etc. 16873486edSKuninori Morimoto // o More testing with other codecs/machines. 17873486edSKuninori Morimoto // o Add more codecs and platforms to ensure good API coverage. 18873486edSKuninori Morimoto // o Support TDM on PCM and I2S 19db2a4165SFrank Mandarino 20db2a4165SFrank Mandarino #include <linux/module.h> 21db2a4165SFrank Mandarino #include <linux/moduleparam.h> 22db2a4165SFrank Mandarino #include <linux/init.h> 23db2a4165SFrank Mandarino #include <linux/delay.h> 24db2a4165SFrank Mandarino #include <linux/pm.h> 25db2a4165SFrank Mandarino #include <linux/bitops.h> 2612ef193dSTroy Kisky #include <linux/debugfs.h> 27db2a4165SFrank Mandarino #include <linux/platform_device.h> 28741a509fSMarkus Pargmann #include <linux/pinctrl/consumer.h> 29f0e8ed85SMark Brown #include <linux/ctype.h> 305a0e3ad6STejun Heo #include <linux/slab.h> 31bec4fa05SStephen Warren #include <linux/of.h> 32a180e8b9SKuninori Morimoto #include <linux/of_graph.h> 33345233d7SLiam Girdwood #include <linux/dmi.h> 34db2a4165SFrank Mandarino #include <sound/core.h> 353028eb8cSMark Brown #include <sound/jack.h> 36db2a4165SFrank Mandarino #include <sound/pcm.h> 37db2a4165SFrank Mandarino #include <sound/pcm_params.h> 38db2a4165SFrank Mandarino #include <sound/soc.h> 3901d7584cSLiam Girdwood #include <sound/soc-dpcm.h> 408a978234SLiam Girdwood #include <sound/soc-topology.h> 41db2a4165SFrank Mandarino #include <sound/initval.h> 42db2a4165SFrank Mandarino 43a8b1d34fSMark Brown #define CREATE_TRACE_POINTS 44a8b1d34fSMark Brown #include <trace/events/asoc.h> 45a8b1d34fSMark Brown 46f0fba2adSLiam Girdwood #define NAME_SIZE 32 47f0fba2adSLiam Girdwood 48c5af3a2eSMark Brown static DEFINE_MUTEX(client_mutex); 49030e79f6SKuninori Morimoto static LIST_HEAD(component_list); 50e894efefSSrinivas Kandagatla static LIST_HEAD(unbind_card_list); 51c5af3a2eSMark Brown 52368dee94SKuninori Morimoto #define for_each_component(component) \ 53368dee94SKuninori Morimoto list_for_each_entry(component, &component_list, list) 54368dee94SKuninori Morimoto 55db2a4165SFrank Mandarino /* 56587c9844SKuninori Morimoto * This is used if driver don't need to have CPU/Codec/Platform 57587c9844SKuninori Morimoto * dai_link. see soc.h 58587c9844SKuninori Morimoto */ 59587c9844SKuninori Morimoto struct snd_soc_dai_link_component null_dailink_component[0]; 60587c9844SKuninori Morimoto EXPORT_SYMBOL_GPL(null_dailink_component); 61587c9844SKuninori Morimoto 62587c9844SKuninori Morimoto /* 63db2a4165SFrank Mandarino * This is a timeout to do a DAPM powerdown after a stream is closed(). 64db2a4165SFrank Mandarino * It can be used to eliminate pops between different playback streams, e.g. 65db2a4165SFrank Mandarino * between two audio tracks. 66db2a4165SFrank Mandarino */ 67db2a4165SFrank Mandarino static int pmdown_time = 5000; 68db2a4165SFrank Mandarino module_param(pmdown_time, int, 0); 69db2a4165SFrank Mandarino MODULE_PARM_DESC(pmdown_time, "DAPM stream powerdown time (msecs)"); 70db2a4165SFrank Mandarino 71dbe21408SMark Brown static ssize_t pmdown_time_show(struct device *dev, 72dbe21408SMark Brown struct device_attribute *attr, char *buf) 73dbe21408SMark Brown { 7436ae1a96SMark Brown struct snd_soc_pcm_runtime *rtd = dev_get_drvdata(dev); 75dbe21408SMark Brown 76f0fba2adSLiam Girdwood return sprintf(buf, "%ld\n", rtd->pmdown_time); 77dbe21408SMark Brown } 78dbe21408SMark Brown 79dbe21408SMark Brown static ssize_t pmdown_time_set(struct device *dev, 80dbe21408SMark Brown struct device_attribute *attr, 81dbe21408SMark Brown const char *buf, size_t count) 82dbe21408SMark Brown { 8336ae1a96SMark Brown struct snd_soc_pcm_runtime *rtd = dev_get_drvdata(dev); 84c593b520SMark Brown int ret; 85dbe21408SMark Brown 86b785a492SJingoo Han ret = kstrtol(buf, 10, &rtd->pmdown_time); 87c593b520SMark Brown if (ret) 88c593b520SMark Brown return ret; 89dbe21408SMark Brown 90dbe21408SMark Brown return count; 91dbe21408SMark Brown } 92dbe21408SMark Brown 93dbe21408SMark Brown static DEVICE_ATTR(pmdown_time, 0644, pmdown_time_show, pmdown_time_set); 94dbe21408SMark Brown 95d29697dcSTakashi Iwai static struct attribute *soc_dev_attrs[] = { 96d29697dcSTakashi Iwai &dev_attr_pmdown_time.attr, 97d29697dcSTakashi Iwai NULL 98d29697dcSTakashi Iwai }; 99d29697dcSTakashi Iwai 100d29697dcSTakashi Iwai static umode_t soc_dev_attr_is_visible(struct kobject *kobj, 101d29697dcSTakashi Iwai struct attribute *attr, int idx) 102d29697dcSTakashi Iwai { 103d29697dcSTakashi Iwai struct device *dev = kobj_to_dev(kobj); 104d29697dcSTakashi Iwai struct snd_soc_pcm_runtime *rtd = dev_get_drvdata(dev); 105d29697dcSTakashi Iwai 106d918a376SKuninori Morimoto if (!rtd) 107d918a376SKuninori Morimoto return 0; 108d918a376SKuninori Morimoto 109d29697dcSTakashi Iwai if (attr == &dev_attr_pmdown_time.attr) 110d29697dcSTakashi Iwai return attr->mode; /* always visible */ 1113b6eed8dSKuninori Morimoto return rtd->num_codecs ? attr->mode : 0; /* enabled only with codec */ 112d29697dcSTakashi Iwai } 113d29697dcSTakashi Iwai 114d29697dcSTakashi Iwai static const struct attribute_group soc_dapm_dev_group = { 115d29697dcSTakashi Iwai .attrs = soc_dapm_dev_attrs, 116d29697dcSTakashi Iwai .is_visible = soc_dev_attr_is_visible, 117d29697dcSTakashi Iwai }; 118d29697dcSTakashi Iwai 119f7e73b26SMark Brown static const struct attribute_group soc_dev_group = { 120d29697dcSTakashi Iwai .attrs = soc_dev_attrs, 121d29697dcSTakashi Iwai .is_visible = soc_dev_attr_is_visible, 122d29697dcSTakashi Iwai }; 123d29697dcSTakashi Iwai 124d29697dcSTakashi Iwai static const struct attribute_group *soc_dev_attr_groups[] = { 125d29697dcSTakashi Iwai &soc_dapm_dev_group, 126f7e73b26SMark Brown &soc_dev_group, 127d29697dcSTakashi Iwai NULL 128d29697dcSTakashi Iwai }; 129d29697dcSTakashi Iwai 1302624d5faSMark Brown #ifdef CONFIG_DEBUG_FS 131a4072cdfSKuninori Morimoto struct dentry *snd_soc_debugfs_root; 132a4072cdfSKuninori Morimoto EXPORT_SYMBOL_GPL(snd_soc_debugfs_root); 133a4072cdfSKuninori Morimoto 13481c7cfd1SLars-Peter Clausen static void soc_init_component_debugfs(struct snd_soc_component *component) 135e73f3de5SRussell King { 1366553bf06SLars-Peter Clausen if (!component->card->debugfs_card_root) 1376553bf06SLars-Peter Clausen return; 1386553bf06SLars-Peter Clausen 13981c7cfd1SLars-Peter Clausen if (component->debugfs_prefix) { 14081c7cfd1SLars-Peter Clausen char *name; 141e73f3de5SRussell King 14281c7cfd1SLars-Peter Clausen name = kasprintf(GFP_KERNEL, "%s:%s", 14381c7cfd1SLars-Peter Clausen component->debugfs_prefix, component->name); 14481c7cfd1SLars-Peter Clausen if (name) { 14581c7cfd1SLars-Peter Clausen component->debugfs_root = debugfs_create_dir(name, 14681c7cfd1SLars-Peter Clausen component->card->debugfs_card_root); 14781c7cfd1SLars-Peter Clausen kfree(name); 14881c7cfd1SLars-Peter Clausen } 14981c7cfd1SLars-Peter Clausen } else { 15081c7cfd1SLars-Peter Clausen component->debugfs_root = debugfs_create_dir(component->name, 15181c7cfd1SLars-Peter Clausen component->card->debugfs_card_root); 152e73f3de5SRussell King } 153e73f3de5SRussell King 15481c7cfd1SLars-Peter Clausen snd_soc_dapm_debugfs_init(snd_soc_component_get_dapm(component), 15581c7cfd1SLars-Peter Clausen component->debugfs_root); 15681c7cfd1SLars-Peter Clausen } 15781c7cfd1SLars-Peter Clausen 15881c7cfd1SLars-Peter Clausen static void soc_cleanup_component_debugfs(struct snd_soc_component *component) 15981c7cfd1SLars-Peter Clausen { 160ad64bfbdSKuninori Morimoto if (!component->debugfs_root) 161ad64bfbdSKuninori Morimoto return; 16281c7cfd1SLars-Peter Clausen debugfs_remove_recursive(component->debugfs_root); 163ad64bfbdSKuninori Morimoto component->debugfs_root = NULL; 16481c7cfd1SLars-Peter Clausen } 16581c7cfd1SLars-Peter Clausen 166c15b2a1dSPeng Donglin static int dai_list_show(struct seq_file *m, void *v) 167f3208780SMark Brown { 1681438c2f6SLars-Peter Clausen struct snd_soc_component *component; 169f3208780SMark Brown struct snd_soc_dai *dai; 170f3208780SMark Brown 17134e81ab4SLars-Peter Clausen mutex_lock(&client_mutex); 17234e81ab4SLars-Peter Clausen 173368dee94SKuninori Morimoto for_each_component(component) 17415a0c645SKuninori Morimoto for_each_component_dais(component, dai) 175700c17caSDonglin Peng seq_printf(m, "%s\n", dai->name); 176f3208780SMark Brown 17734e81ab4SLars-Peter Clausen mutex_unlock(&client_mutex); 17834e81ab4SLars-Peter Clausen 179700c17caSDonglin Peng return 0; 180700c17caSDonglin Peng } 181c15b2a1dSPeng Donglin DEFINE_SHOW_ATTRIBUTE(dai_list); 182f3208780SMark Brown 183db795f9bSKuninori Morimoto static int component_list_show(struct seq_file *m, void *v) 184db795f9bSKuninori Morimoto { 185db795f9bSKuninori Morimoto struct snd_soc_component *component; 186db795f9bSKuninori Morimoto 187db795f9bSKuninori Morimoto mutex_lock(&client_mutex); 188db795f9bSKuninori Morimoto 189368dee94SKuninori Morimoto for_each_component(component) 190db795f9bSKuninori Morimoto seq_printf(m, "%s\n", component->name); 191db795f9bSKuninori Morimoto 192db795f9bSKuninori Morimoto mutex_unlock(&client_mutex); 193db795f9bSKuninori Morimoto 194db795f9bSKuninori Morimoto return 0; 195db795f9bSKuninori Morimoto } 196db795f9bSKuninori Morimoto DEFINE_SHOW_ATTRIBUTE(component_list); 197db795f9bSKuninori Morimoto 198a6052154SJarkko Nikula static void soc_init_card_debugfs(struct snd_soc_card *card) 199a6052154SJarkko Nikula { 200a6052154SJarkko Nikula card->debugfs_card_root = debugfs_create_dir(card->name, 2018a9dab1aSMark Brown snd_soc_debugfs_root); 2023a45b867SJarkko Nikula 203fee531d6SGreg Kroah-Hartman debugfs_create_u32("dapm_pop_time", 0644, card->debugfs_card_root, 2043a45b867SJarkko Nikula &card->pop_time); 205d8ca7a0aSKuninori Morimoto 206d8ca7a0aSKuninori Morimoto snd_soc_dapm_debugfs_init(&card->dapm, card->debugfs_card_root); 207a6052154SJarkko Nikula } 208a6052154SJarkko Nikula 209a6052154SJarkko Nikula static void soc_cleanup_card_debugfs(struct snd_soc_card *card) 210a6052154SJarkko Nikula { 211a6052154SJarkko Nikula debugfs_remove_recursive(card->debugfs_card_root); 21229040d1aSKuninori Morimoto card->debugfs_card_root = NULL; 213a6052154SJarkko Nikula } 214a6052154SJarkko Nikula 2156553bf06SLars-Peter Clausen static void snd_soc_debugfs_init(void) 2166553bf06SLars-Peter Clausen { 2176553bf06SLars-Peter Clausen snd_soc_debugfs_root = debugfs_create_dir("asoc", NULL); 2186553bf06SLars-Peter Clausen 219fee531d6SGreg Kroah-Hartman debugfs_create_file("dais", 0444, snd_soc_debugfs_root, NULL, 220fee531d6SGreg Kroah-Hartman &dai_list_fops); 221db795f9bSKuninori Morimoto 222fee531d6SGreg Kroah-Hartman debugfs_create_file("components", 0444, snd_soc_debugfs_root, NULL, 223fee531d6SGreg Kroah-Hartman &component_list_fops); 2246553bf06SLars-Peter Clausen } 2256553bf06SLars-Peter Clausen 2266553bf06SLars-Peter Clausen static void snd_soc_debugfs_exit(void) 2276553bf06SLars-Peter Clausen { 2286553bf06SLars-Peter Clausen debugfs_remove_recursive(snd_soc_debugfs_root); 2296553bf06SLars-Peter Clausen } 2306553bf06SLars-Peter Clausen 2312624d5faSMark Brown #else 2322624d5faSMark Brown 23381c7cfd1SLars-Peter Clausen static inline void soc_init_component_debugfs( 23481c7cfd1SLars-Peter Clausen struct snd_soc_component *component) 2352624d5faSMark Brown { 2362624d5faSMark Brown } 2372624d5faSMark Brown 23881c7cfd1SLars-Peter Clausen static inline void soc_cleanup_component_debugfs( 23981c7cfd1SLars-Peter Clausen struct snd_soc_component *component) 240731f1ab2SSebastien Guiriec { 241731f1ab2SSebastien Guiriec } 242731f1ab2SSebastien Guiriec 243b95fccbcSAxel Lin static inline void soc_init_card_debugfs(struct snd_soc_card *card) 244b95fccbcSAxel Lin { 245b95fccbcSAxel Lin } 246b95fccbcSAxel Lin 247b95fccbcSAxel Lin static inline void soc_cleanup_card_debugfs(struct snd_soc_card *card) 248b95fccbcSAxel Lin { 249b95fccbcSAxel Lin } 2506553bf06SLars-Peter Clausen 2516553bf06SLars-Peter Clausen static inline void snd_soc_debugfs_init(void) 2526553bf06SLars-Peter Clausen { 2536553bf06SLars-Peter Clausen } 2546553bf06SLars-Peter Clausen 2556553bf06SLars-Peter Clausen static inline void snd_soc_debugfs_exit(void) 2566553bf06SLars-Peter Clausen { 2576553bf06SLars-Peter Clausen } 2586553bf06SLars-Peter Clausen 2592624d5faSMark Brown #endif 2602624d5faSMark Brown 26112b05232SKuninori Morimoto static int snd_soc_rtd_add_component(struct snd_soc_pcm_runtime *rtd, 262a0ac4411SKuninori Morimoto struct snd_soc_component *component) 263a0ac4411SKuninori Morimoto { 2642b544dd7SKuninori Morimoto struct snd_soc_component *comp; 265613fb500SKuninori Morimoto int i; 266a0ac4411SKuninori Morimoto 267613fb500SKuninori Morimoto for_each_rtd_components(rtd, i, comp) { 268a0ac4411SKuninori Morimoto /* already connected */ 2692b544dd7SKuninori Morimoto if (comp == component) 270a0ac4411SKuninori Morimoto return 0; 271a0ac4411SKuninori Morimoto } 272a0ac4411SKuninori Morimoto 273613fb500SKuninori Morimoto /* see for_each_rtd_components */ 274613fb500SKuninori Morimoto rtd->components[rtd->num_components] = component; 275613fb500SKuninori Morimoto rtd->num_components++; 276a0ac4411SKuninori Morimoto 277a0ac4411SKuninori Morimoto return 0; 278a0ac4411SKuninori Morimoto } 279a0ac4411SKuninori Morimoto 280a0ac4411SKuninori Morimoto struct snd_soc_component *snd_soc_rtdcom_lookup(struct snd_soc_pcm_runtime *rtd, 281a0ac4411SKuninori Morimoto const char *driver_name) 282a0ac4411SKuninori Morimoto { 2832b544dd7SKuninori Morimoto struct snd_soc_component *component; 284613fb500SKuninori Morimoto int i; 285a0ac4411SKuninori Morimoto 286971da24cSKuninori Morimoto if (!driver_name) 287971da24cSKuninori Morimoto return NULL; 288971da24cSKuninori Morimoto 289a33c0d16SKuninori Morimoto /* 290a33c0d16SKuninori Morimoto * NOTE 291a33c0d16SKuninori Morimoto * 292a33c0d16SKuninori Morimoto * snd_soc_rtdcom_lookup() will find component from rtd by using 293a33c0d16SKuninori Morimoto * specified driver name. 294a33c0d16SKuninori Morimoto * But, if many components which have same driver name are connected 295a33c0d16SKuninori Morimoto * to 1 rtd, this function will return 1st found component. 296a33c0d16SKuninori Morimoto */ 297613fb500SKuninori Morimoto for_each_rtd_components(rtd, i, component) { 2982b544dd7SKuninori Morimoto const char *component_name = component->driver->name; 299971da24cSKuninori Morimoto 300971da24cSKuninori Morimoto if (!component_name) 301971da24cSKuninori Morimoto continue; 302971da24cSKuninori Morimoto 303971da24cSKuninori Morimoto if ((component_name == driver_name) || 304971da24cSKuninori Morimoto strcmp(component_name, driver_name) == 0) 305613fb500SKuninori Morimoto return component; 306a0ac4411SKuninori Morimoto } 307a0ac4411SKuninori Morimoto 308a0ac4411SKuninori Morimoto return NULL; 309a0ac4411SKuninori Morimoto } 310031734b7SKuninori Morimoto EXPORT_SYMBOL_GPL(snd_soc_rtdcom_lookup); 311a0ac4411SKuninori Morimoto 31218dd66eaSKuninori Morimoto static struct snd_soc_component 31318dd66eaSKuninori Morimoto *snd_soc_lookup_component_nolocked(struct device *dev, const char *driver_name) 314b8132657SKuninori Morimoto { 315b8132657SKuninori Morimoto struct snd_soc_component *component; 3165bd7e08bSKuninori Morimoto struct snd_soc_component *found_component; 317b8132657SKuninori Morimoto 3185bd7e08bSKuninori Morimoto found_component = NULL; 319b8132657SKuninori Morimoto for_each_component(component) { 3205bd7e08bSKuninori Morimoto if ((dev == component->dev) && 3215bd7e08bSKuninori Morimoto (!driver_name || 3225bd7e08bSKuninori Morimoto (driver_name == component->driver->name) || 3235bd7e08bSKuninori Morimoto (strcmp(component->driver->name, driver_name) == 0))) { 3245bd7e08bSKuninori Morimoto found_component = component; 325b8132657SKuninori Morimoto break; 326b8132657SKuninori Morimoto } 3275bd7e08bSKuninori Morimoto } 328b8132657SKuninori Morimoto 3295bd7e08bSKuninori Morimoto return found_component; 330b8132657SKuninori Morimoto } 33118dd66eaSKuninori Morimoto 33218dd66eaSKuninori Morimoto struct snd_soc_component *snd_soc_lookup_component(struct device *dev, 33318dd66eaSKuninori Morimoto const char *driver_name) 33418dd66eaSKuninori Morimoto { 33518dd66eaSKuninori Morimoto struct snd_soc_component *component; 33618dd66eaSKuninori Morimoto 33718dd66eaSKuninori Morimoto mutex_lock(&client_mutex); 33818dd66eaSKuninori Morimoto component = snd_soc_lookup_component_nolocked(dev, driver_name); 33918dd66eaSKuninori Morimoto mutex_unlock(&client_mutex); 34018dd66eaSKuninori Morimoto 34118dd66eaSKuninori Morimoto return component; 34218dd66eaSKuninori Morimoto } 343b8132657SKuninori Morimoto EXPORT_SYMBOL_GPL(snd_soc_lookup_component); 344b8132657SKuninori Morimoto 34594def8eaSKuninori Morimoto struct snd_soc_pcm_runtime 34694def8eaSKuninori Morimoto *snd_soc_get_pcm_runtime(struct snd_soc_card *card, 3474468189fSKuninori Morimoto struct snd_soc_dai_link *dai_link) 34894def8eaSKuninori Morimoto { 34994def8eaSKuninori Morimoto struct snd_soc_pcm_runtime *rtd; 35094def8eaSKuninori Morimoto 35194def8eaSKuninori Morimoto for_each_card_rtds(card, rtd) { 3524468189fSKuninori Morimoto if (rtd->dai_link == dai_link) 35394def8eaSKuninori Morimoto return rtd; 35494def8eaSKuninori Morimoto } 3554468189fSKuninori Morimoto dev_dbg(card->dev, "ASoC: failed to find rtd %s\n", dai_link->name); 35694def8eaSKuninori Morimoto return NULL; 35794def8eaSKuninori Morimoto } 35894def8eaSKuninori Morimoto EXPORT_SYMBOL_GPL(snd_soc_get_pcm_runtime); 35994def8eaSKuninori Morimoto 36083f94a2eSKuninori Morimoto /* 36183f94a2eSKuninori Morimoto * Power down the audio subsystem pmdown_time msecs after close is called. 36283f94a2eSKuninori Morimoto * This is to ensure there are no pops or clicks in between any music tracks 36383f94a2eSKuninori Morimoto * due to DAPM power cycling. 36483f94a2eSKuninori Morimoto */ 36583f94a2eSKuninori Morimoto void snd_soc_close_delayed_work(struct snd_soc_pcm_runtime *rtd) 36683f94a2eSKuninori Morimoto { 367c2233a26SKuninori Morimoto struct snd_soc_dai *codec_dai = asoc_rtd_to_codec(rtd, 0); 3680f6011fdSKuninori Morimoto int playback = SNDRV_PCM_STREAM_PLAYBACK; 36983f94a2eSKuninori Morimoto 37083f94a2eSKuninori Morimoto mutex_lock_nested(&rtd->card->pcm_mutex, rtd->card->pcm_subclass); 37183f94a2eSKuninori Morimoto 37283f94a2eSKuninori Morimoto dev_dbg(rtd->dev, 37383f94a2eSKuninori Morimoto "ASoC: pop wq checking: %s status: %s waiting: %s\n", 37483f94a2eSKuninori Morimoto codec_dai->driver->playback.stream_name, 375*b3dea624SKuninori Morimoto snd_soc_dai_stream_active(codec_dai, playback) ? 376*b3dea624SKuninori Morimoto "active" : "inactive", 37783f94a2eSKuninori Morimoto rtd->pop_wait ? "yes" : "no"); 37883f94a2eSKuninori Morimoto 37983f94a2eSKuninori Morimoto /* are we waiting on this codec DAI stream */ 38083f94a2eSKuninori Morimoto if (rtd->pop_wait == 1) { 38183f94a2eSKuninori Morimoto rtd->pop_wait = 0; 3820f6011fdSKuninori Morimoto snd_soc_dapm_stream_event(rtd, playback, 38383f94a2eSKuninori Morimoto SND_SOC_DAPM_STREAM_STOP); 38483f94a2eSKuninori Morimoto } 38583f94a2eSKuninori Morimoto 38683f94a2eSKuninori Morimoto mutex_unlock(&rtd->card->pcm_mutex); 38783f94a2eSKuninori Morimoto } 38883f94a2eSKuninori Morimoto EXPORT_SYMBOL_GPL(snd_soc_close_delayed_work); 38983f94a2eSKuninori Morimoto 3906e864344SKuninori Morimoto static void soc_release_rtd_dev(struct device *dev) 3916e864344SKuninori Morimoto { 3926e864344SKuninori Morimoto /* "dev" means "rtd->dev" */ 3936e864344SKuninori Morimoto kfree(dev); 3946e864344SKuninori Morimoto } 3956e864344SKuninori Morimoto 3961c93a9e0SKuninori Morimoto static void soc_free_pcm_runtime(struct snd_soc_pcm_runtime *rtd) 3971c93a9e0SKuninori Morimoto { 3986e864344SKuninori Morimoto if (!rtd) 3996e864344SKuninori Morimoto return; 4006e864344SKuninori Morimoto 401753ace0aSKuninori Morimoto list_del(&rtd->list); 402b7c5bc45SKuninori Morimoto 4039c9b6520SCurtis Malainey if (delayed_work_pending(&rtd->delayed_work)) 4040ced7b05SKuninori Morimoto flush_delayed_work(&rtd->delayed_work); 4050ced7b05SKuninori Morimoto snd_soc_pcm_component_free(rtd); 4060ced7b05SKuninori Morimoto 407b7c5bc45SKuninori Morimoto /* 408b7c5bc45SKuninori Morimoto * we don't need to call kfree() for rtd->dev 409b7c5bc45SKuninori Morimoto * see 410b7c5bc45SKuninori Morimoto * soc_release_rtd_dev() 411d918a376SKuninori Morimoto * 412d918a376SKuninori Morimoto * We don't need rtd->dev NULL check, because 413d918a376SKuninori Morimoto * it is alloced *before* rtd. 414d918a376SKuninori Morimoto * see 415d918a376SKuninori Morimoto * soc_new_pcm_runtime() 416b7c5bc45SKuninori Morimoto */ 417b7c5bc45SKuninori Morimoto device_unregister(rtd->dev); 4181c93a9e0SKuninori Morimoto } 4191c93a9e0SKuninori Morimoto 4204bf2e385SCurtis Malainey static void close_delayed_work(struct work_struct *work) { 4214bf2e385SCurtis Malainey struct snd_soc_pcm_runtime *rtd = 4224bf2e385SCurtis Malainey container_of(work, struct snd_soc_pcm_runtime, 4234bf2e385SCurtis Malainey delayed_work.work); 4244bf2e385SCurtis Malainey 4254bf2e385SCurtis Malainey if (rtd->close_delayed_work_func) 4264bf2e385SCurtis Malainey rtd->close_delayed_work_func(rtd); 4274bf2e385SCurtis Malainey } 4284bf2e385SCurtis Malainey 4291a497983SMengdong Lin static struct snd_soc_pcm_runtime *soc_new_pcm_runtime( 4301a497983SMengdong Lin struct snd_soc_card *card, struct snd_soc_dai_link *dai_link) 4311a497983SMengdong Lin { 4321a497983SMengdong Lin struct snd_soc_pcm_runtime *rtd; 433613fb500SKuninori Morimoto struct snd_soc_component *component; 434d918a376SKuninori Morimoto struct device *dev; 4356e864344SKuninori Morimoto int ret; 436d74c2a15SKuninori Morimoto int stream; 4371a497983SMengdong Lin 4386e864344SKuninori Morimoto /* 439d918a376SKuninori Morimoto * for rtd->dev 440d918a376SKuninori Morimoto */ 441d918a376SKuninori Morimoto dev = kzalloc(sizeof(struct device), GFP_KERNEL); 442d918a376SKuninori Morimoto if (!dev) 443d918a376SKuninori Morimoto return NULL; 444d918a376SKuninori Morimoto 445d918a376SKuninori Morimoto dev->parent = card->dev; 446d918a376SKuninori Morimoto dev->release = soc_release_rtd_dev; 447d918a376SKuninori Morimoto dev->groups = soc_dev_attr_groups; 448d918a376SKuninori Morimoto 449d918a376SKuninori Morimoto dev_set_name(dev, "%s", dai_link->name); 450d918a376SKuninori Morimoto 451d918a376SKuninori Morimoto ret = device_register(dev); 452d918a376SKuninori Morimoto if (ret < 0) { 453d918a376SKuninori Morimoto put_device(dev); /* soc_release_rtd_dev */ 454d918a376SKuninori Morimoto return NULL; 455d918a376SKuninori Morimoto } 456d918a376SKuninori Morimoto 457d918a376SKuninori Morimoto /* 4586e864344SKuninori Morimoto * for rtd 4596e864344SKuninori Morimoto */ 460613fb500SKuninori Morimoto rtd = devm_kzalloc(dev, 461613fb500SKuninori Morimoto sizeof(*rtd) + 462613fb500SKuninori Morimoto sizeof(*component) * (dai_link->num_cpus + 463613fb500SKuninori Morimoto dai_link->num_codecs + 464613fb500SKuninori Morimoto dai_link->num_platforms), 465613fb500SKuninori Morimoto GFP_KERNEL); 4661a497983SMengdong Lin if (!rtd) 4676e864344SKuninori Morimoto goto free_rtd; 4681a497983SMengdong Lin 469d918a376SKuninori Morimoto rtd->dev = dev; 47007d22a9bSTakashi Iwai INIT_LIST_HEAD(&rtd->list); 471d74c2a15SKuninori Morimoto for_each_pcm_streams(stream) { 472d74c2a15SKuninori Morimoto INIT_LIST_HEAD(&rtd->dpcm[stream].be_clients); 473d74c2a15SKuninori Morimoto INIT_LIST_HEAD(&rtd->dpcm[stream].fe_clients); 474d74c2a15SKuninori Morimoto } 475d918a376SKuninori Morimoto dev_set_drvdata(dev, rtd); 4764bf2e385SCurtis Malainey INIT_DELAYED_WORK(&rtd->delayed_work, close_delayed_work); 477d918a376SKuninori Morimoto 4786e864344SKuninori Morimoto /* 47922a2fc81SKuninori Morimoto * for rtd->dais 4806e864344SKuninori Morimoto */ 48122a2fc81SKuninori Morimoto rtd->dais = devm_kcalloc(dev, dai_link->num_cpus + dai_link->num_codecs, 4826396bb22SKees Cook sizeof(struct snd_soc_dai *), 4831a497983SMengdong Lin GFP_KERNEL); 48422a2fc81SKuninori Morimoto if (!rtd->dais) 4856e864344SKuninori Morimoto goto free_rtd; 4866e864344SKuninori Morimoto 4876e864344SKuninori Morimoto /* 48822a2fc81SKuninori Morimoto * dais = [][][][][][][][][][][][][][][][][][] 48922a2fc81SKuninori Morimoto * ^cpu_dais ^codec_dais 49022a2fc81SKuninori Morimoto * |--- num_cpus ---|--- num_codecs --| 4911729025bSKuninori Morimoto * see 4921729025bSKuninori Morimoto * asoc_rtd_to_cpu() 4931729025bSKuninori Morimoto * asoc_rtd_to_codec() 49476afa643SShreyas NC */ 49549648d02SKuninori Morimoto rtd->num_cpus = dai_link->num_cpus; 49649648d02SKuninori Morimoto rtd->num_codecs = dai_link->num_codecs; 497929deb84SKuninori Morimoto rtd->card = card; 498929deb84SKuninori Morimoto rtd->dai_link = dai_link; 49901faf501SKuninori Morimoto rtd->num = card->num_rtd++; 500929deb84SKuninori Morimoto 5016634e3d6SKuninori Morimoto /* see for_each_card_rtds */ 5021a497983SMengdong Lin list_add_tail(&rtd->list, &card->rtd_list); 503a848125eSKuninori Morimoto 504a848125eSKuninori Morimoto return rtd; 5056e864344SKuninori Morimoto 5066e864344SKuninori Morimoto free_rtd: 5076e864344SKuninori Morimoto soc_free_pcm_runtime(rtd); 5086e864344SKuninori Morimoto return NULL; 5091a497983SMengdong Lin } 5101a497983SMengdong Lin 51165462e44SKuninori Morimoto static void snd_soc_flush_all_delayed_work(struct snd_soc_card *card) 51265462e44SKuninori Morimoto { 51365462e44SKuninori Morimoto struct snd_soc_pcm_runtime *rtd; 51465462e44SKuninori Morimoto 51565462e44SKuninori Morimoto for_each_card_rtds(card, rtd) 51665462e44SKuninori Morimoto flush_delayed_work(&rtd->delayed_work); 51765462e44SKuninori Morimoto } 51865462e44SKuninori Morimoto 5196f8ab4acSMark Brown #ifdef CONFIG_PM_SLEEP 520db2a4165SFrank Mandarino /* powers down audio subsystem for suspend */ 5216f8ab4acSMark Brown int snd_soc_suspend(struct device *dev) 522db2a4165SFrank Mandarino { 5236f8ab4acSMark Brown struct snd_soc_card *card = dev_get_drvdata(dev); 524d9fc4063SKuninori Morimoto struct snd_soc_component *component; 5251a497983SMengdong Lin struct snd_soc_pcm_runtime *rtd; 5260f6011fdSKuninori Morimoto int playback = SNDRV_PCM_STREAM_PLAYBACK; 5271a497983SMengdong Lin int i; 528db2a4165SFrank Mandarino 529c5599b87SLars-Peter Clausen /* If the card is not initialized yet there is nothing to do */ 530c5599b87SLars-Peter Clausen if (!card->instantiated) 531e3509ff0SDaniel Mack return 0; 532e3509ff0SDaniel Mack 5332c7b696aSMarcel Ziswiler /* 5342c7b696aSMarcel Ziswiler * Due to the resume being scheduled into a workqueue we could 5356ed25978SAndy Green * suspend before that's finished - wait for it to complete. 5366ed25978SAndy Green */ 537f0fba2adSLiam Girdwood snd_power_wait(card->snd_card, SNDRV_CTL_POWER_D0); 5386ed25978SAndy Green 5396ed25978SAndy Green /* we're going to block userspace touching us until resume completes */ 540f0fba2adSLiam Girdwood snd_power_change_state(card->snd_card, SNDRV_CTL_POWER_D3hot); 5416ed25978SAndy Green 542a00f90f9SMark Brown /* mute any active DACs */ 543bcb1fd1fSKuninori Morimoto for_each_card_rtds(card, rtd) { 5440b7990e3SKuninori Morimoto struct snd_soc_dai *dai; 5453efab7dcSMark Brown 5461a497983SMengdong Lin if (rtd->dai_link->ignore_suspend) 5473efab7dcSMark Brown continue; 5483efab7dcSMark Brown 549a4be4187SKuninori Morimoto for_each_rtd_codec_dais(rtd, i, dai) { 550*b3dea624SKuninori Morimoto if (snd_soc_dai_stream_active(dai, playback)) 5510f6011fdSKuninori Morimoto snd_soc_dai_digital_mute(dai, 1, playback); 552db2a4165SFrank Mandarino } 55388bd870fSBenoit Cousson } 554db2a4165SFrank Mandarino 5554ccab3e7SLiam Girdwood /* suspend all pcms */ 556bcb1fd1fSKuninori Morimoto for_each_card_rtds(card, rtd) { 5571a497983SMengdong Lin if (rtd->dai_link->ignore_suspend) 5583efab7dcSMark Brown continue; 5593efab7dcSMark Brown 5601a497983SMengdong Lin snd_pcm_suspend_all(rtd->pcm); 5613efab7dcSMark Brown } 5624ccab3e7SLiam Girdwood 56387506549SMark Brown if (card->suspend_pre) 56470b2ac12SMark Brown card->suspend_pre(card); 565db2a4165SFrank Mandarino 56637660b6dSLars-Peter Clausen /* close any waiting streams */ 56765462e44SKuninori Morimoto snd_soc_flush_all_delayed_work(card); 568db2a4165SFrank Mandarino 569bcb1fd1fSKuninori Morimoto for_each_card_rtds(card, rtd) { 570d74c2a15SKuninori Morimoto int stream; 5713efab7dcSMark Brown 5721a497983SMengdong Lin if (rtd->dai_link->ignore_suspend) 5733efab7dcSMark Brown continue; 5743efab7dcSMark Brown 575d74c2a15SKuninori Morimoto for_each_pcm_streams(stream) 576d74c2a15SKuninori Morimoto snd_soc_dapm_stream_event(rtd, stream, 577db2a4165SFrank Mandarino SND_SOC_DAPM_STREAM_SUSPEND); 578db2a4165SFrank Mandarino } 579db2a4165SFrank Mandarino 5808be4da29SLars-Peter Clausen /* Recheck all endpoints too, their state is affected by suspend */ 5818be4da29SLars-Peter Clausen dapm_mark_endpoints_dirty(card); 582e2d32ff6SMark Brown snd_soc_dapm_sync(&card->dapm); 583e2d32ff6SMark Brown 5849178feb4SKuninori Morimoto /* suspend all COMPONENTs */ 5851272063aSKuninori Morimoto for_each_card_rtds(card, rtd) { 5861272063aSKuninori Morimoto 5871272063aSKuninori Morimoto if (rtd->dai_link->ignore_suspend) 5881272063aSKuninori Morimoto continue; 5891272063aSKuninori Morimoto 5901272063aSKuninori Morimoto for_each_rtd_components(rtd, i, component) { 5912c7b696aSMarcel Ziswiler struct snd_soc_dapm_context *dapm = 5922c7b696aSMarcel Ziswiler snd_soc_component_get_dapm(component); 593d9fc4063SKuninori Morimoto 5942c7b696aSMarcel Ziswiler /* 5951272063aSKuninori Morimoto * ignore if component was already suspended 5962c7b696aSMarcel Ziswiler */ 5971272063aSKuninori Morimoto if (snd_soc_component_is_suspended(component)) 5981272063aSKuninori Morimoto continue; 5991272063aSKuninori Morimoto 6001272063aSKuninori Morimoto /* 6011272063aSKuninori Morimoto * If there are paths active then the COMPONENT will be 6021272063aSKuninori Morimoto * held with bias _ON and should not be suspended. 6031272063aSKuninori Morimoto */ 6044890140fSLars-Peter Clausen switch (snd_soc_dapm_get_bias_level(dapm)) { 6051547aba9SMark Brown case SND_SOC_BIAS_STANDBY: 606125a25daSMark Brown /* 6079178feb4SKuninori Morimoto * If the COMPONENT is capable of idle 608125a25daSMark Brown * bias off then being in STANDBY 609125a25daSMark Brown * means it's doing something, 610125a25daSMark Brown * otherwise fall through. 611125a25daSMark Brown */ 6124890140fSLars-Peter Clausen if (dapm->idle_bias_off) { 6139178feb4SKuninori Morimoto dev_dbg(component->dev, 61410e8aa9aSMichał Mirosław "ASoC: idle_bias_off CODEC on over suspend\n"); 615125a25daSMark Brown break; 616125a25daSMark Brown } 6171a12d5dcSGustavo A. R. Silva /* fall through */ 618a8093297SLars-Peter Clausen 6191547aba9SMark Brown case SND_SOC_BIAS_OFF: 62066c51573SKuninori Morimoto snd_soc_component_suspend(component); 6219178feb4SKuninori Morimoto if (component->regmap) 6229178feb4SKuninori Morimoto regcache_mark_dirty(component->regmap); 623988e8cc4SNicolin Chen /* deactivate pins to sleep state */ 6249178feb4SKuninori Morimoto pinctrl_pm_select_sleep_state(component->dev); 6251547aba9SMark Brown break; 6261547aba9SMark Brown default: 6279178feb4SKuninori Morimoto dev_dbg(component->dev, 6289178feb4SKuninori Morimoto "ASoC: COMPONENT is on over suspend\n"); 6291547aba9SMark Brown break; 6301547aba9SMark Brown } 6311547aba9SMark Brown } 632f0fba2adSLiam Girdwood } 633db2a4165SFrank Mandarino 63487506549SMark Brown if (card->suspend_post) 63570b2ac12SMark Brown card->suspend_post(card); 636db2a4165SFrank Mandarino 637db2a4165SFrank Mandarino return 0; 638db2a4165SFrank Mandarino } 6396f8ab4acSMark Brown EXPORT_SYMBOL_GPL(snd_soc_suspend); 640db2a4165SFrank Mandarino 6412c7b696aSMarcel Ziswiler /* 6422c7b696aSMarcel Ziswiler * deferred resume work, so resume can complete before we finished 6436ed25978SAndy Green * setting our codec back up, which can be very slow on I2C 6446ed25978SAndy Green */ 6456ed25978SAndy Green static void soc_resume_deferred(struct work_struct *work) 646db2a4165SFrank Mandarino { 647f0fba2adSLiam Girdwood struct snd_soc_card *card = 6482c7b696aSMarcel Ziswiler container_of(work, struct snd_soc_card, 6492c7b696aSMarcel Ziswiler deferred_resume_work); 6501a497983SMengdong Lin struct snd_soc_pcm_runtime *rtd; 651d9fc4063SKuninori Morimoto struct snd_soc_component *component; 6521a497983SMengdong Lin int i; 653db2a4165SFrank Mandarino 6542c7b696aSMarcel Ziswiler /* 6552c7b696aSMarcel Ziswiler * our power state is still SNDRV_CTL_POWER_D3hot from suspend time, 6566ed25978SAndy Green * so userspace apps are blocked from touching us 6576ed25978SAndy Green */ 6586ed25978SAndy Green 659f110bfc7SLiam Girdwood dev_dbg(card->dev, "ASoC: starting resume work\n"); 6606ed25978SAndy Green 6619949788bSMark Brown /* Bring us up into D2 so that DAPM starts enabling things */ 662f0fba2adSLiam Girdwood snd_power_change_state(card->snd_card, SNDRV_CTL_POWER_D2); 6639949788bSMark Brown 66487506549SMark Brown if (card->resume_pre) 66570b2ac12SMark Brown card->resume_pre(card); 666db2a4165SFrank Mandarino 667f70f18f7SKuninori Morimoto for_each_card_components(card, component) { 668e40fadbcSKuninori Morimoto if (snd_soc_component_is_suspended(component)) 6699a840cbaSKuninori Morimoto snd_soc_component_resume(component); 6701547aba9SMark Brown } 671db2a4165SFrank Mandarino 672bcb1fd1fSKuninori Morimoto for_each_card_rtds(card, rtd) { 673d74c2a15SKuninori Morimoto int stream; 6743efab7dcSMark Brown 6751a497983SMengdong Lin if (rtd->dai_link->ignore_suspend) 6763efab7dcSMark Brown continue; 6773efab7dcSMark Brown 678d74c2a15SKuninori Morimoto for_each_pcm_streams(stream) 679d74c2a15SKuninori Morimoto snd_soc_dapm_stream_event(rtd, stream, 680db2a4165SFrank Mandarino SND_SOC_DAPM_STREAM_RESUME); 681db2a4165SFrank Mandarino } 682db2a4165SFrank Mandarino 6833ff3f64bSMark Brown /* unmute any active DACs */ 684bcb1fd1fSKuninori Morimoto for_each_card_rtds(card, rtd) { 6850b7990e3SKuninori Morimoto struct snd_soc_dai *dai; 6860f6011fdSKuninori Morimoto int playback = SNDRV_PCM_STREAM_PLAYBACK; 6873efab7dcSMark Brown 6881a497983SMengdong Lin if (rtd->dai_link->ignore_suspend) 6893efab7dcSMark Brown continue; 6903efab7dcSMark Brown 691a4be4187SKuninori Morimoto for_each_rtd_codec_dais(rtd, i, dai) { 692*b3dea624SKuninori Morimoto if (snd_soc_dai_stream_active(dai, playback)) 6930f6011fdSKuninori Morimoto snd_soc_dai_digital_mute(dai, 0, playback); 694db2a4165SFrank Mandarino } 69588bd870fSBenoit Cousson } 696db2a4165SFrank Mandarino 69787506549SMark Brown if (card->resume_post) 69870b2ac12SMark Brown card->resume_post(card); 699db2a4165SFrank Mandarino 700f110bfc7SLiam Girdwood dev_dbg(card->dev, "ASoC: resume work completed\n"); 7016ed25978SAndy Green 7028be4da29SLars-Peter Clausen /* Recheck all endpoints too, their state is affected by suspend */ 7038be4da29SLars-Peter Clausen dapm_mark_endpoints_dirty(card); 704e2d32ff6SMark Brown snd_soc_dapm_sync(&card->dapm); 7051a7aaa58SJeeja KP 7061a7aaa58SJeeja KP /* userspace can access us now we are back as we were before */ 7071a7aaa58SJeeja KP snd_power_change_state(card->snd_card, SNDRV_CTL_POWER_D0); 7086ed25978SAndy Green } 7096ed25978SAndy Green 7106ed25978SAndy Green /* powers up audio subsystem after a suspend */ 7116f8ab4acSMark Brown int snd_soc_resume(struct device *dev) 7126ed25978SAndy Green { 7136f8ab4acSMark Brown struct snd_soc_card *card = dev_get_drvdata(dev); 71476c39e86SKuninori Morimoto struct snd_soc_component *component; 715b9dd94a8SPeter Ujfalusi 716c5599b87SLars-Peter Clausen /* If the card is not initialized yet there is nothing to do */ 717c5599b87SLars-Peter Clausen if (!card->instantiated) 7185ff1ddf2SEric Miao return 0; 7195ff1ddf2SEric Miao 720988e8cc4SNicolin Chen /* activate pins from sleep state */ 72176c39e86SKuninori Morimoto for_each_card_components(card, component) 722*b3dea624SKuninori Morimoto if (snd_soc_component_active(component)) 72376c39e86SKuninori Morimoto pinctrl_pm_select_default_state(component->dev); 724988e8cc4SNicolin Chen 725f110bfc7SLiam Girdwood dev_dbg(dev, "ASoC: Scheduling resume work\n"); 7266308419aSMark Brown if (!schedule_work(&card->deferred_resume_work)) 727f110bfc7SLiam Girdwood dev_err(dev, "ASoC: resume work item may be lost\n"); 7286ed25978SAndy Green 729db2a4165SFrank Mandarino return 0; 730db2a4165SFrank Mandarino } 7316f8ab4acSMark Brown EXPORT_SYMBOL_GPL(snd_soc_resume); 732b3da4251SKuninori Morimoto 733b3da4251SKuninori Morimoto static void soc_resume_init(struct snd_soc_card *card) 734b3da4251SKuninori Morimoto { 735b3da4251SKuninori Morimoto /* deferred resume work */ 736b3da4251SKuninori Morimoto INIT_WORK(&card->deferred_resume_work, soc_resume_deferred); 737b3da4251SKuninori Morimoto } 738db2a4165SFrank Mandarino #else 7396f8ab4acSMark Brown #define snd_soc_suspend NULL 7406f8ab4acSMark Brown #define snd_soc_resume NULL 741b3da4251SKuninori Morimoto static inline void soc_resume_init(struct snd_soc_card *card) 742b3da4251SKuninori Morimoto { 743b3da4251SKuninori Morimoto } 744db2a4165SFrank Mandarino #endif 745db2a4165SFrank Mandarino 746c0834440SKuninori Morimoto static struct device_node 747c0834440SKuninori Morimoto *soc_component_to_node(struct snd_soc_component *component) 74812023a9aSMisael Lopez Cruz { 749c0834440SKuninori Morimoto struct device_node *of_node; 75012023a9aSMisael Lopez Cruz 751c0834440SKuninori Morimoto of_node = component->dev->of_node; 752c0834440SKuninori Morimoto if (!of_node && component->dev->parent) 753c0834440SKuninori Morimoto of_node = component->dev->parent->of_node; 75434e81ab4SLars-Peter Clausen 755c0834440SKuninori Morimoto return of_node; 75612023a9aSMisael Lopez Cruz } 75712023a9aSMisael Lopez Cruz 758be6ac0a9SKuninori Morimoto static int snd_soc_is_matching_component( 759be6ac0a9SKuninori Morimoto const struct snd_soc_dai_link_component *dlc, 760be6ac0a9SKuninori Morimoto struct snd_soc_component *component) 761be6ac0a9SKuninori Morimoto { 762be6ac0a9SKuninori Morimoto struct device_node *component_of_node; 763be6ac0a9SKuninori Morimoto 7647d7db5d3SKuninori Morimoto if (!dlc) 7657d7db5d3SKuninori Morimoto return 0; 7667d7db5d3SKuninori Morimoto 7677d7db5d3SKuninori Morimoto component_of_node = soc_component_to_node(component); 768be6ac0a9SKuninori Morimoto 769be6ac0a9SKuninori Morimoto if (dlc->of_node && component_of_node != dlc->of_node) 770be6ac0a9SKuninori Morimoto return 0; 771be6ac0a9SKuninori Morimoto if (dlc->name && strcmp(component->name, dlc->name)) 772be6ac0a9SKuninori Morimoto return 0; 773be6ac0a9SKuninori Morimoto 774be6ac0a9SKuninori Morimoto return 1; 775be6ac0a9SKuninori Morimoto } 776be6ac0a9SKuninori Morimoto 777db2a4165SFrank Mandarino static struct snd_soc_component *soc_find_component( 778c1e230f0SKuninori Morimoto const struct snd_soc_dai_link_component *dlc) 779db2a4165SFrank Mandarino { 780db2a4165SFrank Mandarino struct snd_soc_component *component; 781db2a4165SFrank Mandarino 782db2a4165SFrank Mandarino lockdep_assert_held(&client_mutex); 783db2a4165SFrank Mandarino 784e3303268SKuninori Morimoto /* 785e3303268SKuninori Morimoto * NOTE 786e3303268SKuninori Morimoto * 787e3303268SKuninori Morimoto * It returns *1st* found component, but some driver 788e3303268SKuninori Morimoto * has few components by same of_node/name 789e3303268SKuninori Morimoto * ex) 790e3303268SKuninori Morimoto * CPU component and generic DMAEngine component 791e3303268SKuninori Morimoto */ 792c1e230f0SKuninori Morimoto for_each_component(component) 793c1e230f0SKuninori Morimoto if (snd_soc_is_matching_component(dlc, component)) 794db2a4165SFrank Mandarino return component; 79502a06d30SBarry Song 79602a06d30SBarry Song return NULL; 79712023a9aSMisael Lopez Cruz } 79812023a9aSMisael Lopez Cruz 799fbb88b5cSMengdong Lin /** 800fbb88b5cSMengdong Lin * snd_soc_find_dai - Find a registered DAI 801fbb88b5cSMengdong Lin * 8024958471bSJeffy Chen * @dlc: name of the DAI or the DAI driver and optional component info to match 803fbb88b5cSMengdong Lin * 804ad61dd30SStephen Boyd * This function will search all registered components and their DAIs to 805fbb88b5cSMengdong Lin * find the DAI of the same name. The component's of_node and name 806fbb88b5cSMengdong Lin * should also match if being specified. 807fbb88b5cSMengdong Lin * 808fbb88b5cSMengdong Lin * Return: pointer of DAI, or NULL if not found. 809fbb88b5cSMengdong Lin */ 810305e9020SMengdong Lin struct snd_soc_dai *snd_soc_find_dai( 81114621c7eSLars-Peter Clausen const struct snd_soc_dai_link_component *dlc) 81212023a9aSMisael Lopez Cruz { 81314621c7eSLars-Peter Clausen struct snd_soc_component *component; 81414621c7eSLars-Peter Clausen struct snd_soc_dai *dai; 81512023a9aSMisael Lopez Cruz 81634e81ab4SLars-Peter Clausen lockdep_assert_held(&client_mutex); 81734e81ab4SLars-Peter Clausen 81814621c7eSLars-Peter Clausen /* Find CPU DAI from registered DAIs */ 819368dee94SKuninori Morimoto for_each_component(component) { 820be6ac0a9SKuninori Morimoto if (!snd_soc_is_matching_component(dlc, component)) 82112023a9aSMisael Lopez Cruz continue; 82215a0c645SKuninori Morimoto for_each_component_dais(component, dai) { 8234958471bSJeffy Chen if (dlc->dai_name && strcmp(dai->name, dlc->dai_name) 8246a6dafdaSJeffy Chen && (!dai->driver->name 8256a6dafdaSJeffy Chen || strcmp(dai->driver->name, dlc->dai_name))) 82614621c7eSLars-Peter Clausen continue; 82712023a9aSMisael Lopez Cruz 82814621c7eSLars-Peter Clausen return dai; 82912023a9aSMisael Lopez Cruz } 83012023a9aSMisael Lopez Cruz } 83112023a9aSMisael Lopez Cruz 83212023a9aSMisael Lopez Cruz return NULL; 83312023a9aSMisael Lopez Cruz } 834305e9020SMengdong Lin EXPORT_SYMBOL_GPL(snd_soc_find_dai); 83512023a9aSMisael Lopez Cruz 836bfce78a5SKuninori Morimoto static int soc_dai_link_sanity_check(struct snd_soc_card *card, 83736794902SKuninori Morimoto struct snd_soc_dai_link *link) 83836794902SKuninori Morimoto { 83936794902SKuninori Morimoto int i; 84076afa643SShreyas NC struct snd_soc_dai_link_component *cpu, *codec, *platform; 84136794902SKuninori Morimoto 84236794902SKuninori Morimoto for_each_link_codecs(link, i, codec) { 84336794902SKuninori Morimoto /* 84436794902SKuninori Morimoto * Codec must be specified by 1 of name or OF node, 84536794902SKuninori Morimoto * not both or neither. 84636794902SKuninori Morimoto */ 84736794902SKuninori Morimoto if (!!codec->name == !!codec->of_node) { 84836794902SKuninori Morimoto dev_err(card->dev, "ASoC: Neither/both codec name/of_node are set for %s\n", 84936794902SKuninori Morimoto link->name); 85036794902SKuninori Morimoto return -EINVAL; 85136794902SKuninori Morimoto } 85236794902SKuninori Morimoto 85336794902SKuninori Morimoto /* Codec DAI name must be specified */ 85436794902SKuninori Morimoto if (!codec->dai_name) { 85536794902SKuninori Morimoto dev_err(card->dev, "ASoC: codec_dai_name not set for %s\n", 85636794902SKuninori Morimoto link->name); 85736794902SKuninori Morimoto return -EINVAL; 85836794902SKuninori Morimoto } 85936794902SKuninori Morimoto 86036794902SKuninori Morimoto /* 86136794902SKuninori Morimoto * Defer card registration if codec component is not added to 86236794902SKuninori Morimoto * component list. 86336794902SKuninori Morimoto */ 864b2acc24cSRanjani Sridharan if (!soc_find_component(codec)) { 865b2acc24cSRanjani Sridharan dev_dbg(card->dev, 866b2acc24cSRanjani Sridharan "ASoC: codec component %s not found for link %s\n", 867b2acc24cSRanjani Sridharan codec->name, link->name); 86836794902SKuninori Morimoto return -EPROBE_DEFER; 86936794902SKuninori Morimoto } 870b2acc24cSRanjani Sridharan } 87136794902SKuninori Morimoto 87236794902SKuninori Morimoto for_each_link_platforms(link, i, platform) { 87336794902SKuninori Morimoto /* 87436794902SKuninori Morimoto * Platform may be specified by either name or OF node, but it 87536794902SKuninori Morimoto * can be left unspecified, then no components will be inserted 87636794902SKuninori Morimoto * in the rtdcom list 87736794902SKuninori Morimoto */ 87836794902SKuninori Morimoto if (!!platform->name == !!platform->of_node) { 87936794902SKuninori Morimoto dev_err(card->dev, 88036794902SKuninori Morimoto "ASoC: Neither/both platform name/of_node are set for %s\n", 88136794902SKuninori Morimoto link->name); 88236794902SKuninori Morimoto return -EINVAL; 88336794902SKuninori Morimoto } 88436794902SKuninori Morimoto 88536794902SKuninori Morimoto /* 88636794902SKuninori Morimoto * Defer card registration if platform component is not added to 88736794902SKuninori Morimoto * component list. 88836794902SKuninori Morimoto */ 889b2acc24cSRanjani Sridharan if (!soc_find_component(platform)) { 890b2acc24cSRanjani Sridharan dev_dbg(card->dev, 891b2acc24cSRanjani Sridharan "ASoC: platform component %s not found for link %s\n", 892b2acc24cSRanjani Sridharan platform->name, link->name); 89336794902SKuninori Morimoto return -EPROBE_DEFER; 89436794902SKuninori Morimoto } 895b2acc24cSRanjani Sridharan } 89636794902SKuninori Morimoto 89776afa643SShreyas NC for_each_link_cpus(link, i, cpu) { 89836794902SKuninori Morimoto /* 89936794902SKuninori Morimoto * CPU device may be specified by either name or OF node, but 90036794902SKuninori Morimoto * can be left unspecified, and will be matched based on DAI 90136794902SKuninori Morimoto * name alone.. 90236794902SKuninori Morimoto */ 90376afa643SShreyas NC if (cpu->name && cpu->of_node) { 90436794902SKuninori Morimoto dev_err(card->dev, 90536794902SKuninori Morimoto "ASoC: Neither/both cpu name/of_node are set for %s\n", 90636794902SKuninori Morimoto link->name); 90736794902SKuninori Morimoto return -EINVAL; 90836794902SKuninori Morimoto } 90936794902SKuninori Morimoto 91036794902SKuninori Morimoto /* 911cd3c5ad7SKuninori Morimoto * Defer card registration if cpu dai component is not added to 91236794902SKuninori Morimoto * component list. 91336794902SKuninori Morimoto */ 91476afa643SShreyas NC if ((cpu->of_node || cpu->name) && 915b2acc24cSRanjani Sridharan !soc_find_component(cpu)) { 916b2acc24cSRanjani Sridharan dev_dbg(card->dev, 917b2acc24cSRanjani Sridharan "ASoC: cpu component %s not found for link %s\n", 918b2acc24cSRanjani Sridharan cpu->name, link->name); 91936794902SKuninori Morimoto return -EPROBE_DEFER; 920b2acc24cSRanjani Sridharan } 92136794902SKuninori Morimoto 92236794902SKuninori Morimoto /* 92336794902SKuninori Morimoto * At least one of CPU DAI name or CPU device name/node must be 92436794902SKuninori Morimoto * specified 92536794902SKuninori Morimoto */ 92676afa643SShreyas NC if (!cpu->dai_name && 92776afa643SShreyas NC !(cpu->name || cpu->of_node)) { 92836794902SKuninori Morimoto dev_err(card->dev, 92936794902SKuninori Morimoto "ASoC: Neither cpu_dai_name nor cpu_name/of_node are set for %s\n", 93036794902SKuninori Morimoto link->name); 93136794902SKuninori Morimoto return -EINVAL; 93236794902SKuninori Morimoto } 93376afa643SShreyas NC } 93436794902SKuninori Morimoto 93536794902SKuninori Morimoto return 0; 93636794902SKuninori Morimoto } 93736794902SKuninori Morimoto 938da704f26SKuninori Morimoto /** 93950cd9b53SKuninori Morimoto * snd_soc_remove_pcm_runtime - Remove a pcm_runtime from card 94050cd9b53SKuninori Morimoto * @card: The ASoC card to which the pcm_runtime has 94150cd9b53SKuninori Morimoto * @rtd: The pcm_runtime to remove 942da704f26SKuninori Morimoto * 94350cd9b53SKuninori Morimoto * This function removes a pcm_runtime from the ASoC card. 944da704f26SKuninori Morimoto */ 94550cd9b53SKuninori Morimoto void snd_soc_remove_pcm_runtime(struct snd_soc_card *card, 94650cd9b53SKuninori Morimoto struct snd_soc_pcm_runtime *rtd) 947bc7a9091SKuninori Morimoto { 948da704f26SKuninori Morimoto lockdep_assert_held(&client_mutex); 949da704f26SKuninori Morimoto 950da704f26SKuninori Morimoto /* 951da704f26SKuninori Morimoto * Notify the machine driver for extra destruction 952da704f26SKuninori Morimoto */ 953da704f26SKuninori Morimoto if (card->remove_dai_link) 95450cd9b53SKuninori Morimoto card->remove_dai_link(card, rtd->dai_link); 955da704f26SKuninori Morimoto 956bc7a9091SKuninori Morimoto soc_free_pcm_runtime(rtd); 957bc7a9091SKuninori Morimoto } 95850cd9b53SKuninori Morimoto EXPORT_SYMBOL_GPL(snd_soc_remove_pcm_runtime); 959bc7a9091SKuninori Morimoto 96063dc47daSKuninori Morimoto /** 9610c048004SKuninori Morimoto * snd_soc_add_pcm_runtime - Add a pcm_runtime dynamically via dai_link 9620c048004SKuninori Morimoto * @card: The ASoC card to which the pcm_runtime is added 9630c048004SKuninori Morimoto * @dai_link: The DAI link to find pcm_runtime 96463dc47daSKuninori Morimoto * 9650c048004SKuninori Morimoto * This function adds a pcm_runtime ASoC card by using dai_link. 96663dc47daSKuninori Morimoto * 9670c048004SKuninori Morimoto * Note: Topology can use this API to add pcm_runtime when probing the 96863dc47daSKuninori Morimoto * topology component. And machine drivers can still define static 96963dc47daSKuninori Morimoto * DAI links in dai_link array. 97063dc47daSKuninori Morimoto */ 9710c048004SKuninori Morimoto int snd_soc_add_pcm_runtime(struct snd_soc_card *card, 9726f2f1ff0SMengdong Lin struct snd_soc_dai_link *dai_link) 973db2a4165SFrank Mandarino { 9741a497983SMengdong Lin struct snd_soc_pcm_runtime *rtd; 97576afa643SShreyas NC struct snd_soc_dai_link_component *codec, *platform, *cpu; 97690be711eSKuninori Morimoto struct snd_soc_component *component; 977bfce78a5SKuninori Morimoto int i, ret; 978db2a4165SFrank Mandarino 97963dc47daSKuninori Morimoto lockdep_assert_held(&client_mutex); 98063dc47daSKuninori Morimoto 98163dc47daSKuninori Morimoto /* 98263dc47daSKuninori Morimoto * Notify the machine driver for extra initialization 98363dc47daSKuninori Morimoto */ 98463dc47daSKuninori Morimoto if (card->add_dai_link) 98563dc47daSKuninori Morimoto card->add_dai_link(card, dai_link); 98663dc47daSKuninori Morimoto 987a655de80SLiam Girdwood if (dai_link->ignore) 988a655de80SLiam Girdwood return 0; 989a655de80SLiam Girdwood 9906f2f1ff0SMengdong Lin dev_dbg(card->dev, "ASoC: binding %s\n", dai_link->name); 9916308419aSMark Brown 992bfce78a5SKuninori Morimoto ret = soc_dai_link_sanity_check(card, dai_link); 993bfce78a5SKuninori Morimoto if (ret < 0) 994bfce78a5SKuninori Morimoto return ret; 995bfce78a5SKuninori Morimoto 996513cb311SSudip Mukherjee rtd = soc_new_pcm_runtime(card, dai_link); 997513cb311SSudip Mukherjee if (!rtd) 998513cb311SSudip Mukherjee return -ENOMEM; 999513cb311SSudip Mukherjee 100076afa643SShreyas NC for_each_link_cpus(dai_link, i, cpu) { 1001c2233a26SKuninori Morimoto asoc_rtd_to_cpu(rtd, i) = snd_soc_find_dai(cpu); 1002c2233a26SKuninori Morimoto if (!asoc_rtd_to_cpu(rtd, i)) { 10036b490879SMartin Hundebøll dev_info(card->dev, "ASoC: CPU DAI %s not registered\n", 100476afa643SShreyas NC cpu->dai_name); 10051a497983SMengdong Lin goto _err_defer; 1006c5af3a2eSMark Brown } 1007c2233a26SKuninori Morimoto snd_soc_rtd_add_component(rtd, asoc_rtd_to_cpu(rtd, i)->component); 100876afa643SShreyas NC } 100976afa643SShreyas NC 101088bd870fSBenoit Cousson /* Find CODEC from registered CODECs */ 101134614739SJerome Brunet for_each_link_codecs(dai_link, i, codec) { 1012c2233a26SKuninori Morimoto asoc_rtd_to_codec(rtd, i) = snd_soc_find_dai(codec); 1013c2233a26SKuninori Morimoto if (!asoc_rtd_to_codec(rtd, i)) { 10147c7e2d6aSStefan Agner dev_info(card->dev, "ASoC: CODEC DAI %s not registered\n", 101534614739SJerome Brunet codec->dai_name); 10161a497983SMengdong Lin goto _err_defer; 101712023a9aSMisael Lopez Cruz } 101834614739SJerome Brunet 1019c2233a26SKuninori Morimoto snd_soc_rtd_add_component(rtd, asoc_rtd_to_codec(rtd, i)->component); 102088bd870fSBenoit Cousson } 102188bd870fSBenoit Cousson 1022e2b30edfSKuninori Morimoto /* Find PLATFORM from registered PLATFORMs */ 102334614739SJerome Brunet for_each_link_platforms(dai_link, i, platform) { 1024368dee94SKuninori Morimoto for_each_component(component) { 102534614739SJerome Brunet if (!snd_soc_is_matching_component(platform, component)) 102690be711eSKuninori Morimoto continue; 102790be711eSKuninori Morimoto 102812b05232SKuninori Morimoto snd_soc_rtd_add_component(rtd, component); 102990be711eSKuninori Morimoto } 103034614739SJerome Brunet } 103190be711eSKuninori Morimoto 1032b19e6e7bSMark Brown return 0; 10331a497983SMengdong Lin 10341a497983SMengdong Lin _err_defer: 103550cd9b53SKuninori Morimoto snd_soc_remove_pcm_runtime(card, rtd); 10361a497983SMengdong Lin return -EPROBE_DEFER; 10376b05eda6SMark Brown } 10380c048004SKuninori Morimoto EXPORT_SYMBOL_GPL(snd_soc_add_pcm_runtime); 10396b05eda6SMark Brown 1040eaffeefbSKuninori Morimoto static int soc_init_pcm_runtime(struct snd_soc_card *card, 104146496acbSKuninori Morimoto struct snd_soc_pcm_runtime *rtd) 104246496acbSKuninori Morimoto { 104346496acbSKuninori Morimoto struct snd_soc_dai_link *dai_link = rtd->dai_link; 1044c2233a26SKuninori Morimoto struct snd_soc_dai *cpu_dai = asoc_rtd_to_cpu(rtd, 0); 104546496acbSKuninori Morimoto struct snd_soc_component *component; 1046613fb500SKuninori Morimoto int ret, num, i; 104746496acbSKuninori Morimoto 104846496acbSKuninori Morimoto /* set default power off timeout */ 104946496acbSKuninori Morimoto rtd->pmdown_time = pmdown_time; 105046496acbSKuninori Morimoto 105146496acbSKuninori Morimoto /* do machine specific initialization */ 105246496acbSKuninori Morimoto if (dai_link->init) { 105346496acbSKuninori Morimoto ret = dai_link->init(rtd); 105446496acbSKuninori Morimoto if (ret < 0) { 105546496acbSKuninori Morimoto dev_err(card->dev, "ASoC: failed to init %s: %d\n", 105646496acbSKuninori Morimoto dai_link->name, ret); 105746496acbSKuninori Morimoto return ret; 105846496acbSKuninori Morimoto } 105946496acbSKuninori Morimoto } 106046496acbSKuninori Morimoto 106146496acbSKuninori Morimoto if (dai_link->dai_fmt) { 106246496acbSKuninori Morimoto ret = snd_soc_runtime_set_dai_fmt(rtd, dai_link->dai_fmt); 106346496acbSKuninori Morimoto if (ret) 106446496acbSKuninori Morimoto return ret; 106546496acbSKuninori Morimoto } 106646496acbSKuninori Morimoto 106746496acbSKuninori Morimoto /* add DPCM sysfs entries */ 106846496acbSKuninori Morimoto soc_dpcm_debugfs_add(rtd); 106946496acbSKuninori Morimoto 107046496acbSKuninori Morimoto num = rtd->num; 107146496acbSKuninori Morimoto 107246496acbSKuninori Morimoto /* 107346496acbSKuninori Morimoto * most drivers will register their PCMs using DAI link ordering but 107446496acbSKuninori Morimoto * topology based drivers can use the DAI link id field to set PCM 107546496acbSKuninori Morimoto * device number and then use rtd + a base offset of the BEs. 107646496acbSKuninori Morimoto */ 1077613fb500SKuninori Morimoto for_each_rtd_components(rtd, i, component) { 107846496acbSKuninori Morimoto if (!component->driver->use_dai_pcm_id) 107946496acbSKuninori Morimoto continue; 108046496acbSKuninori Morimoto 108146496acbSKuninori Morimoto if (rtd->dai_link->no_pcm) 108246496acbSKuninori Morimoto num += component->driver->be_pcm_base; 108346496acbSKuninori Morimoto else 108446496acbSKuninori Morimoto num = rtd->dai_link->id; 108546496acbSKuninori Morimoto } 108646496acbSKuninori Morimoto 108746496acbSKuninori Morimoto /* create compress_device if possible */ 108846496acbSKuninori Morimoto ret = snd_soc_dai_compress_new(cpu_dai, rtd, num); 108946496acbSKuninori Morimoto if (ret != -ENOTSUPP) { 109046496acbSKuninori Morimoto if (ret < 0) 109146496acbSKuninori Morimoto dev_err(card->dev, "ASoC: can't create compress %s\n", 109246496acbSKuninori Morimoto dai_link->stream_name); 109346496acbSKuninori Morimoto return ret; 109446496acbSKuninori Morimoto } 109546496acbSKuninori Morimoto 109646496acbSKuninori Morimoto /* create the pcm */ 109746496acbSKuninori Morimoto ret = soc_new_pcm(rtd, num); 109846496acbSKuninori Morimoto if (ret < 0) { 109946496acbSKuninori Morimoto dev_err(card->dev, "ASoC: can't create pcm %s :%d\n", 110046496acbSKuninori Morimoto dai_link->stream_name, ret); 110146496acbSKuninori Morimoto return ret; 110246496acbSKuninori Morimoto } 1103d1eb6d11SKuninori Morimoto 11040b73ba55SKuninori Morimoto return snd_soc_pcm_dai_new(rtd); 110546496acbSKuninori Morimoto } 110646496acbSKuninori Morimoto 1107ffd60fbaSKuninori Morimoto static void soc_set_name_prefix(struct snd_soc_card *card, 1108ffd60fbaSKuninori Morimoto struct snd_soc_component *component) 1109ffd60fbaSKuninori Morimoto { 1110aec3ff99SKuninori Morimoto struct device_node *of_node = soc_component_to_node(component); 111129d9fc7aSKuninori Morimoto const char *str; 111229d9fc7aSKuninori Morimoto int ret, i; 1113ffd60fbaSKuninori Morimoto 11144702f991SKuninori Morimoto for (i = 0; i < card->num_configs; i++) { 1115ffd60fbaSKuninori Morimoto struct snd_soc_codec_conf *map = &card->codec_conf[i]; 1116ffd60fbaSKuninori Morimoto 1117c13493a2SKuninori Morimoto if (snd_soc_is_matching_component(&map->dlc, component)) { 1118ffd60fbaSKuninori Morimoto component->name_prefix = map->name_prefix; 1119ffd60fbaSKuninori Morimoto return; 1120ffd60fbaSKuninori Morimoto } 1121c13493a2SKuninori Morimoto } 1122ffd60fbaSKuninori Morimoto 1123ffd60fbaSKuninori Morimoto /* 1124ffd60fbaSKuninori Morimoto * If there is no configuration table or no match in the table, 1125ffd60fbaSKuninori Morimoto * check if a prefix is provided in the node 1126ffd60fbaSKuninori Morimoto */ 112729d9fc7aSKuninori Morimoto ret = of_property_read_string(of_node, "sound-name-prefix", &str); 112829d9fc7aSKuninori Morimoto if (ret < 0) 112929d9fc7aSKuninori Morimoto return; 113029d9fc7aSKuninori Morimoto 113129d9fc7aSKuninori Morimoto component->name_prefix = str; 1132ffd60fbaSKuninori Morimoto } 1133ffd60fbaSKuninori Morimoto 1134c6619b72SKuninori Morimoto static void soc_remove_component(struct snd_soc_component *component, 1135c6619b72SKuninori Morimoto int probed) 113622d14231SKuninori Morimoto { 1137c6619b72SKuninori Morimoto 1138c6619b72SKuninori Morimoto if (!component->card) 1139c6619b72SKuninori Morimoto return; 1140c6619b72SKuninori Morimoto 1141c6619b72SKuninori Morimoto if (probed) 1142c6619b72SKuninori Morimoto snd_soc_component_remove(component); 1143c6619b72SKuninori Morimoto 114404f770d9SKuninori Morimoto /* For framework level robustness */ 11453bb936f5SAmadeusz Sławiński snd_soc_component_set_jack(component, NULL, NULL); 114604f770d9SKuninori Morimoto 11477a5d9815SBard liao list_del_init(&component->card_list); 114822d14231SKuninori Morimoto snd_soc_dapm_free(snd_soc_component_get_dapm(component)); 114922d14231SKuninori Morimoto soc_cleanup_component_debugfs(component); 115022d14231SKuninori Morimoto component->card = NULL; 11510e36f36bSPierre-Louis Bossart snd_soc_component_module_put_when_remove(component); 115222d14231SKuninori Morimoto } 115322d14231SKuninori Morimoto 1154ffd60fbaSKuninori Morimoto static int soc_probe_component(struct snd_soc_card *card, 1155ffd60fbaSKuninori Morimoto struct snd_soc_component *component) 1156ffd60fbaSKuninori Morimoto { 1157ffd60fbaSKuninori Morimoto struct snd_soc_dapm_context *dapm = 1158ffd60fbaSKuninori Morimoto snd_soc_component_get_dapm(component); 1159ffd60fbaSKuninori Morimoto struct snd_soc_dai *dai; 1160c6619b72SKuninori Morimoto int probed = 0; 1161ffd60fbaSKuninori Morimoto int ret; 1162ffd60fbaSKuninori Morimoto 1163ffd60fbaSKuninori Morimoto if (!strcmp(component->name, "snd-soc-dummy")) 1164ffd60fbaSKuninori Morimoto return 0; 1165ffd60fbaSKuninori Morimoto 1166ffd60fbaSKuninori Morimoto if (component->card) { 1167ffd60fbaSKuninori Morimoto if (component->card != card) { 1168ffd60fbaSKuninori Morimoto dev_err(component->dev, 1169ffd60fbaSKuninori Morimoto "Trying to bind component to card \"%s\" but is already bound to card \"%s\"\n", 1170ffd60fbaSKuninori Morimoto card->name, component->card->name); 1171ffd60fbaSKuninori Morimoto return -ENODEV; 1172ffd60fbaSKuninori Morimoto } 1173ffd60fbaSKuninori Morimoto return 0; 1174ffd60fbaSKuninori Morimoto } 1175ffd60fbaSKuninori Morimoto 1176ffd60fbaSKuninori Morimoto ret = snd_soc_component_module_get_when_probe(component); 1177ffd60fbaSKuninori Morimoto if (ret < 0) 1178ffd60fbaSKuninori Morimoto return ret; 1179ffd60fbaSKuninori Morimoto 1180ffd60fbaSKuninori Morimoto component->card = card; 1181ffd60fbaSKuninori Morimoto soc_set_name_prefix(card, component); 1182ffd60fbaSKuninori Morimoto 1183ffd60fbaSKuninori Morimoto soc_init_component_debugfs(component); 1184ffd60fbaSKuninori Morimoto 118595c267ddSKuninori Morimoto snd_soc_dapm_init(dapm, card, component); 1186b614beafSKuninori Morimoto 1187ffd60fbaSKuninori Morimoto ret = snd_soc_dapm_new_controls(dapm, 1188ffd60fbaSKuninori Morimoto component->driver->dapm_widgets, 1189ffd60fbaSKuninori Morimoto component->driver->num_dapm_widgets); 1190ffd60fbaSKuninori Morimoto 1191ffd60fbaSKuninori Morimoto if (ret != 0) { 1192ffd60fbaSKuninori Morimoto dev_err(component->dev, 1193ffd60fbaSKuninori Morimoto "Failed to create new controls %d\n", ret); 1194ffd60fbaSKuninori Morimoto goto err_probe; 1195ffd60fbaSKuninori Morimoto } 1196ffd60fbaSKuninori Morimoto 1197ffd60fbaSKuninori Morimoto for_each_component_dais(component, dai) { 1198ffd60fbaSKuninori Morimoto ret = snd_soc_dapm_new_dai_widgets(dapm, dai); 1199ffd60fbaSKuninori Morimoto if (ret != 0) { 1200ffd60fbaSKuninori Morimoto dev_err(component->dev, 1201ffd60fbaSKuninori Morimoto "Failed to create DAI widgets %d\n", ret); 1202ffd60fbaSKuninori Morimoto goto err_probe; 1203ffd60fbaSKuninori Morimoto } 1204ffd60fbaSKuninori Morimoto } 1205ffd60fbaSKuninori Morimoto 1206ffd60fbaSKuninori Morimoto ret = snd_soc_component_probe(component); 1207ffd60fbaSKuninori Morimoto if (ret < 0) { 1208ffd60fbaSKuninori Morimoto dev_err(component->dev, 1209ffd60fbaSKuninori Morimoto "ASoC: failed to probe component %d\n", ret); 1210ffd60fbaSKuninori Morimoto goto err_probe; 1211ffd60fbaSKuninori Morimoto } 1212ffd60fbaSKuninori Morimoto WARN(dapm->idle_bias_off && 1213ffd60fbaSKuninori Morimoto dapm->bias_level != SND_SOC_BIAS_OFF, 1214ffd60fbaSKuninori Morimoto "codec %s can not start from non-off bias with idle_bias_off==1\n", 1215ffd60fbaSKuninori Morimoto component->name); 1216c6619b72SKuninori Morimoto probed = 1; 1217ffd60fbaSKuninori Morimoto 1218ffd60fbaSKuninori Morimoto /* machine specific init */ 1219ffd60fbaSKuninori Morimoto if (component->init) { 1220ffd60fbaSKuninori Morimoto ret = component->init(component); 1221ffd60fbaSKuninori Morimoto if (ret < 0) { 1222ffd60fbaSKuninori Morimoto dev_err(component->dev, 1223ffd60fbaSKuninori Morimoto "Failed to do machine specific init %d\n", ret); 1224ffd60fbaSKuninori Morimoto goto err_probe; 1225ffd60fbaSKuninori Morimoto } 1226ffd60fbaSKuninori Morimoto } 1227ffd60fbaSKuninori Morimoto 1228ffd60fbaSKuninori Morimoto ret = snd_soc_add_component_controls(component, 1229ffd60fbaSKuninori Morimoto component->driver->controls, 1230ffd60fbaSKuninori Morimoto component->driver->num_controls); 1231ffd60fbaSKuninori Morimoto if (ret < 0) 1232ffd60fbaSKuninori Morimoto goto err_probe; 1233ffd60fbaSKuninori Morimoto 1234ffd60fbaSKuninori Morimoto ret = snd_soc_dapm_add_routes(dapm, 1235ffd60fbaSKuninori Morimoto component->driver->dapm_routes, 1236ffd60fbaSKuninori Morimoto component->driver->num_dapm_routes); 1237a22ae72bSPierre-Louis Bossart if (ret < 0) { 1238a22ae72bSPierre-Louis Bossart if (card->disable_route_checks) { 1239a22ae72bSPierre-Louis Bossart dev_info(card->dev, 1240a22ae72bSPierre-Louis Bossart "%s: disable_route_checks set, ignoring errors on add_routes\n", 1241a22ae72bSPierre-Louis Bossart __func__); 1242a22ae72bSPierre-Louis Bossart } else { 1243a22ae72bSPierre-Louis Bossart dev_err(card->dev, 1244a22ae72bSPierre-Louis Bossart "%s: snd_soc_dapm_add_routes failed: %d\n", 1245a22ae72bSPierre-Louis Bossart __func__, ret); 1246ffd60fbaSKuninori Morimoto goto err_probe; 1247a22ae72bSPierre-Louis Bossart } 1248a22ae72bSPierre-Louis Bossart } 1249ffd60fbaSKuninori Morimoto 1250ffd60fbaSKuninori Morimoto /* see for_each_card_components */ 1251ffd60fbaSKuninori Morimoto list_add(&component->card_list, &card->component_dev_list); 1252ffd60fbaSKuninori Morimoto 1253ffd60fbaSKuninori Morimoto err_probe: 1254ffd60fbaSKuninori Morimoto if (ret < 0) 1255c6619b72SKuninori Morimoto soc_remove_component(component, probed); 1256ffd60fbaSKuninori Morimoto 1257ffd60fbaSKuninori Morimoto return ret; 1258ffd60fbaSKuninori Morimoto } 1259ffd60fbaSKuninori Morimoto 12604ca47d21SKuninori Morimoto static void soc_remove_link_dais(struct snd_soc_card *card) 1261f0fba2adSLiam Girdwood { 12624ca47d21SKuninori Morimoto struct snd_soc_pcm_runtime *rtd; 12634ca47d21SKuninori Morimoto int order; 12644ca47d21SKuninori Morimoto 12654ca47d21SKuninori Morimoto for_each_comp_order(order) { 12664ca47d21SKuninori Morimoto for_each_card_rtds(card, rtd) { 12677eaa313bSKuninori Morimoto /* remove all rtd connected DAIs in good order */ 12687eaa313bSKuninori Morimoto snd_soc_pcm_dai_remove(rtd, order); 1269f0fba2adSLiam Girdwood } 12704ca47d21SKuninori Morimoto } 12714ca47d21SKuninori Morimoto } 1272f0fba2adSLiam Girdwood 1273bc7c16c2SKuninori Morimoto static int soc_probe_link_dais(struct snd_soc_card *card) 1274bc7c16c2SKuninori Morimoto { 1275bc7c16c2SKuninori Morimoto struct snd_soc_pcm_runtime *rtd; 127651801aeaSKuninori Morimoto int order, ret; 1277bc7c16c2SKuninori Morimoto 1278bc7c16c2SKuninori Morimoto for_each_comp_order(order) { 1279bc7c16c2SKuninori Morimoto for_each_card_rtds(card, rtd) { 1280bc7c16c2SKuninori Morimoto 1281bc7c16c2SKuninori Morimoto dev_dbg(card->dev, 1282bc7c16c2SKuninori Morimoto "ASoC: probe %s dai link %d late %d\n", 1283bc7c16c2SKuninori Morimoto card->name, rtd->num, order); 1284bc7c16c2SKuninori Morimoto 128551801aeaSKuninori Morimoto /* probe all rtd connected DAIs in good order */ 128651801aeaSKuninori Morimoto ret = snd_soc_pcm_dai_probe(rtd, order); 1287bc7c16c2SKuninori Morimoto if (ret) 1288bc7c16c2SKuninori Morimoto return ret; 1289bc7c16c2SKuninori Morimoto } 1290bc7c16c2SKuninori Morimoto } 1291bc7c16c2SKuninori Morimoto 1292bc7c16c2SKuninori Morimoto return 0; 1293bc7c16c2SKuninori Morimoto } 1294bc7c16c2SKuninori Morimoto 1295b006c0c6SKuninori Morimoto static void soc_remove_link_components(struct snd_soc_card *card) 129662ae68faSStephen Warren { 129761aca564SLars-Peter Clausen struct snd_soc_component *component; 1298b006c0c6SKuninori Morimoto struct snd_soc_pcm_runtime *rtd; 1299613fb500SKuninori Morimoto int i, order; 130062ae68faSStephen Warren 1301b006c0c6SKuninori Morimoto for_each_comp_order(order) { 1302b006c0c6SKuninori Morimoto for_each_card_rtds(card, rtd) { 1303613fb500SKuninori Morimoto for_each_rtd_components(rtd, i, component) { 1304b006c0c6SKuninori Morimoto if (component->driver->remove_order != order) 1305b006c0c6SKuninori Morimoto continue; 1306b006c0c6SKuninori Morimoto 1307c6619b72SKuninori Morimoto soc_remove_component(component, 1); 130862ae68faSStephen Warren } 130962ae68faSStephen Warren } 1310b006c0c6SKuninori Morimoto } 1311b006c0c6SKuninori Morimoto } 131262ae68faSStephen Warren 131362f07a6bSKuninori Morimoto static int soc_probe_link_components(struct snd_soc_card *card) 13146fb03550SKuninori Morimoto { 13156fb03550SKuninori Morimoto struct snd_soc_component *component; 131662f07a6bSKuninori Morimoto struct snd_soc_pcm_runtime *rtd; 1317613fb500SKuninori Morimoto int i, ret, order; 13186fb03550SKuninori Morimoto 131962f07a6bSKuninori Morimoto for_each_comp_order(order) { 132062f07a6bSKuninori Morimoto for_each_card_rtds(card, rtd) { 1321613fb500SKuninori Morimoto for_each_rtd_components(rtd, i, component) { 132262f07a6bSKuninori Morimoto if (component->driver->probe_order != order) 132362f07a6bSKuninori Morimoto continue; 132462f07a6bSKuninori Morimoto 13256fb03550SKuninori Morimoto ret = soc_probe_component(card, component); 13266fb03550SKuninori Morimoto if (ret < 0) 13276fb03550SKuninori Morimoto return ret; 13286fb03550SKuninori Morimoto } 13296fb03550SKuninori Morimoto } 133062f07a6bSKuninori Morimoto } 13316fb03550SKuninori Morimoto 13326fb03550SKuninori Morimoto return 0; 13336fb03550SKuninori Morimoto } 13346fb03550SKuninori Morimoto 1335e8fbd250SKuninori Morimoto static void soc_unbind_aux_dev(struct snd_soc_card *card) 13364893a2ebSKuninori Morimoto { 1337e8fbd250SKuninori Morimoto struct snd_soc_component *component, *_component; 1338e8fbd250SKuninori Morimoto 1339e8fbd250SKuninori Morimoto for_each_card_auxs_safe(card, component, _component) { 13404893a2ebSKuninori Morimoto component->init = NULL; 13414893a2ebSKuninori Morimoto list_del(&component->card_aux_list); 13424893a2ebSKuninori Morimoto } 1343e8fbd250SKuninori Morimoto } 13444893a2ebSKuninori Morimoto 1345bee886f1SKuninori Morimoto static int soc_bind_aux_dev(struct snd_soc_card *card) 1346b19e6e7bSMark Brown { 1347f2ed6b07SMengdong Lin struct snd_soc_component *component; 1348bee886f1SKuninori Morimoto struct snd_soc_aux_dev *aux; 1349bee886f1SKuninori Morimoto int i; 13503ca041edSSebastian Reichel 1351bee886f1SKuninori Morimoto for_each_card_pre_auxs(card, i, aux) { 1352f2ed6b07SMengdong Lin /* codecs, usually analog devices */ 1353bee886f1SKuninori Morimoto component = soc_find_component(&aux->dlc); 1354f2ed6b07SMengdong Lin if (!component) 13553dc29b8bSKuninori Morimoto return -EPROBE_DEFER; 13563ca041edSSebastian Reichel 1357bee886f1SKuninori Morimoto component->init = aux->init; 1358c2b71c71SKuninori Morimoto /* see for_each_card_auxs */ 1359d2e3a135SSylwester Nawrocki list_add(&component->card_aux_list, &card->aux_comp_list); 1360bee886f1SKuninori Morimoto } 1361f2ed6b07SMengdong Lin return 0; 1362b19e6e7bSMark Brown } 1363b19e6e7bSMark Brown 1364f2ed6b07SMengdong Lin static int soc_probe_aux_devices(struct snd_soc_card *card) 1365f2ed6b07SMengdong Lin { 136674bd3f92SKuninori Morimoto struct snd_soc_component *component; 1367f2ed6b07SMengdong Lin int order; 1368f2ed6b07SMengdong Lin int ret; 1369f2ed6b07SMengdong Lin 13701a1035a9SKuninori Morimoto for_each_comp_order(order) { 137174bd3f92SKuninori Morimoto for_each_card_auxs(card, component) { 137274bd3f92SKuninori Morimoto if (component->driver->probe_order != order) 137374bd3f92SKuninori Morimoto continue; 137474bd3f92SKuninori Morimoto 137574bd3f92SKuninori Morimoto ret = soc_probe_component(card, component); 137674bd3f92SKuninori Morimoto if (ret < 0) 1377f2ed6b07SMengdong Lin return ret; 1378f2ed6b07SMengdong Lin } 1379f2ed6b07SMengdong Lin } 138065d9361fSLars-Peter Clausen 138144c69bb1SLars-Peter Clausen return 0; 13823ca041edSSebastian Reichel } 13832eea392dSJarkko Nikula 1384f2ed6b07SMengdong Lin static void soc_remove_aux_devices(struct snd_soc_card *card) 138544c69bb1SLars-Peter Clausen { 1386f2ed6b07SMengdong Lin struct snd_soc_component *comp, *_comp; 1387f2ed6b07SMengdong Lin int order; 138844c69bb1SLars-Peter Clausen 13891a1035a9SKuninori Morimoto for_each_comp_order(order) { 1390c2b71c71SKuninori Morimoto for_each_card_auxs_safe(card, comp, _comp) { 1391e8fbd250SKuninori Morimoto if (comp->driver->remove_order == order) 1392c6619b72SKuninori Morimoto soc_remove_component(comp, 1); 13935f3484acSLars-Peter Clausen } 13942eea392dSJarkko Nikula } 13952eea392dSJarkko Nikula } 13962eea392dSJarkko Nikula 1397ce64c8b9SLars-Peter Clausen /** 1398ce64c8b9SLars-Peter Clausen * snd_soc_runtime_set_dai_fmt() - Change DAI link format for a ASoC runtime 1399ce64c8b9SLars-Peter Clausen * @rtd: The runtime for which the DAI link format should be changed 1400ce64c8b9SLars-Peter Clausen * @dai_fmt: The new DAI link format 1401ce64c8b9SLars-Peter Clausen * 1402ce64c8b9SLars-Peter Clausen * This function updates the DAI link format for all DAIs connected to the DAI 1403ce64c8b9SLars-Peter Clausen * link for the specified runtime. 1404ce64c8b9SLars-Peter Clausen * 1405ce64c8b9SLars-Peter Clausen * Note: For setups with a static format set the dai_fmt field in the 1406ce64c8b9SLars-Peter Clausen * corresponding snd_dai_link struct instead of using this function. 1407ce64c8b9SLars-Peter Clausen * 1408ce64c8b9SLars-Peter Clausen * Returns 0 on success, otherwise a negative error code. 1409ce64c8b9SLars-Peter Clausen */ 1410ce64c8b9SLars-Peter Clausen int snd_soc_runtime_set_dai_fmt(struct snd_soc_pcm_runtime *rtd, 1411ce64c8b9SLars-Peter Clausen unsigned int dai_fmt) 1412ce64c8b9SLars-Peter Clausen { 141376afa643SShreyas NC struct snd_soc_dai *cpu_dai; 14140b7990e3SKuninori Morimoto struct snd_soc_dai *codec_dai; 141576afa643SShreyas NC unsigned int inv_dai_fmt; 1416ce64c8b9SLars-Peter Clausen unsigned int i; 1417ce64c8b9SLars-Peter Clausen int ret; 1418ce64c8b9SLars-Peter Clausen 1419a4be4187SKuninori Morimoto for_each_rtd_codec_dais(rtd, i, codec_dai) { 1420ce64c8b9SLars-Peter Clausen ret = snd_soc_dai_set_fmt(codec_dai, dai_fmt); 1421ce64c8b9SLars-Peter Clausen if (ret != 0 && ret != -ENOTSUPP) { 1422ce64c8b9SLars-Peter Clausen dev_warn(codec_dai->dev, 1423ce64c8b9SLars-Peter Clausen "ASoC: Failed to set DAI format: %d\n", ret); 1424ce64c8b9SLars-Peter Clausen return ret; 1425ce64c8b9SLars-Peter Clausen } 1426ce64c8b9SLars-Peter Clausen } 1427ce64c8b9SLars-Peter Clausen 14282c7b696aSMarcel Ziswiler /* 14292c7b696aSMarcel Ziswiler * Flip the polarity for the "CPU" end of a CODEC<->CODEC link 14302c7b696aSMarcel Ziswiler * the component which has non_legacy_dai_naming is Codec 14312c7b696aSMarcel Ziswiler */ 1432ce64c8b9SLars-Peter Clausen inv_dai_fmt = dai_fmt & ~SND_SOC_DAIFMT_MASTER_MASK; 1433ce64c8b9SLars-Peter Clausen switch (dai_fmt & SND_SOC_DAIFMT_MASTER_MASK) { 1434ce64c8b9SLars-Peter Clausen case SND_SOC_DAIFMT_CBM_CFM: 1435ce64c8b9SLars-Peter Clausen inv_dai_fmt |= SND_SOC_DAIFMT_CBS_CFS; 1436ce64c8b9SLars-Peter Clausen break; 1437ce64c8b9SLars-Peter Clausen case SND_SOC_DAIFMT_CBM_CFS: 1438ce64c8b9SLars-Peter Clausen inv_dai_fmt |= SND_SOC_DAIFMT_CBS_CFM; 1439ce64c8b9SLars-Peter Clausen break; 1440ce64c8b9SLars-Peter Clausen case SND_SOC_DAIFMT_CBS_CFM: 1441ce64c8b9SLars-Peter Clausen inv_dai_fmt |= SND_SOC_DAIFMT_CBM_CFS; 1442ce64c8b9SLars-Peter Clausen break; 1443ce64c8b9SLars-Peter Clausen case SND_SOC_DAIFMT_CBS_CFS: 1444ce64c8b9SLars-Peter Clausen inv_dai_fmt |= SND_SOC_DAIFMT_CBM_CFM; 1445ce64c8b9SLars-Peter Clausen break; 1446ce64c8b9SLars-Peter Clausen } 1447a4be4187SKuninori Morimoto for_each_rtd_cpu_dais(rtd, i, cpu_dai) { 144876afa643SShreyas NC unsigned int fmt = dai_fmt; 1449ce64c8b9SLars-Peter Clausen 145076afa643SShreyas NC if (cpu_dai->component->driver->non_legacy_dai_naming) 145176afa643SShreyas NC fmt = inv_dai_fmt; 1452ce64c8b9SLars-Peter Clausen 145376afa643SShreyas NC ret = snd_soc_dai_set_fmt(cpu_dai, fmt); 1454ce64c8b9SLars-Peter Clausen if (ret != 0 && ret != -ENOTSUPP) { 1455ce64c8b9SLars-Peter Clausen dev_warn(cpu_dai->dev, 1456ce64c8b9SLars-Peter Clausen "ASoC: Failed to set DAI format: %d\n", ret); 1457ce64c8b9SLars-Peter Clausen return ret; 1458ce64c8b9SLars-Peter Clausen } 145976afa643SShreyas NC } 1460ce64c8b9SLars-Peter Clausen 1461ce64c8b9SLars-Peter Clausen return 0; 1462ce64c8b9SLars-Peter Clausen } 1463ddaca25aSLars-Peter Clausen EXPORT_SYMBOL_GPL(snd_soc_runtime_set_dai_fmt); 1464ce64c8b9SLars-Peter Clausen 14651f5a4535STakashi Iwai #ifdef CONFIG_DMI 14662c7b696aSMarcel Ziswiler /* 14678a6a6a38SKuninori Morimoto * If a DMI filed contain strings in this blacklist (e.g. 14688a6a6a38SKuninori Morimoto * "Type2 - Board Manufacturer" or "Type1 - TBD by OEM"), it will be taken 14698a6a6a38SKuninori Morimoto * as invalid and dropped when setting the card long name from DMI info. 14708a6a6a38SKuninori Morimoto */ 14718a6a6a38SKuninori Morimoto static const char * const dmi_blacklist[] = { 14728a6a6a38SKuninori Morimoto "To be filled by OEM", 14738a6a6a38SKuninori Morimoto "TBD by OEM", 14748a6a6a38SKuninori Morimoto "Default String", 14758a6a6a38SKuninori Morimoto "Board Manufacturer", 14768a6a6a38SKuninori Morimoto "Board Vendor Name", 14778a6a6a38SKuninori Morimoto "Board Product Name", 14788a6a6a38SKuninori Morimoto NULL, /* terminator */ 14798a6a6a38SKuninori Morimoto }; 14808a6a6a38SKuninori Morimoto 14818a6a6a38SKuninori Morimoto /* 14822c7b696aSMarcel Ziswiler * Trim special characters, and replace '-' with '_' since '-' is used to 1483345233d7SLiam Girdwood * separate different DMI fields in the card long name. Only number and 1484345233d7SLiam Girdwood * alphabet characters and a few separator characters are kept. 1485345233d7SLiam Girdwood */ 1486345233d7SLiam Girdwood static void cleanup_dmi_name(char *name) 1487345233d7SLiam Girdwood { 1488345233d7SLiam Girdwood int i, j = 0; 1489345233d7SLiam Girdwood 1490345233d7SLiam Girdwood for (i = 0; name[i]; i++) { 1491345233d7SLiam Girdwood if (isalnum(name[i]) || (name[i] == '.') 1492345233d7SLiam Girdwood || (name[i] == '_')) 1493345233d7SLiam Girdwood name[j++] = name[i]; 1494345233d7SLiam Girdwood else if (name[i] == '-') 1495345233d7SLiam Girdwood name[j++] = '_'; 1496345233d7SLiam Girdwood } 1497345233d7SLiam Girdwood 1498345233d7SLiam Girdwood name[j] = '\0'; 1499345233d7SLiam Girdwood } 1500345233d7SLiam Girdwood 15012c7b696aSMarcel Ziswiler /* 15022c7b696aSMarcel Ziswiler * Check if a DMI field is valid, i.e. not containing any string 150398faf436SMengdong Lin * in the black list. 150498faf436SMengdong Lin */ 150598faf436SMengdong Lin static int is_dmi_valid(const char *field) 150698faf436SMengdong Lin { 150798faf436SMengdong Lin int i = 0; 150898faf436SMengdong Lin 150998faf436SMengdong Lin while (dmi_blacklist[i]) { 151098faf436SMengdong Lin if (strstr(field, dmi_blacklist[i])) 151198faf436SMengdong Lin return 0; 151298faf436SMengdong Lin i++; 151346b5a4d2SWu Fengguang } 151498faf436SMengdong Lin 151598faf436SMengdong Lin return 1; 151698faf436SMengdong Lin } 151798faf436SMengdong Lin 15184e01e5dbSJaroslav Kysela /* 15194e01e5dbSJaroslav Kysela * Append a string to card->dmi_longname with character cleanups. 15204e01e5dbSJaroslav Kysela */ 15214e01e5dbSJaroslav Kysela static void append_dmi_string(struct snd_soc_card *card, const char *str) 15224e01e5dbSJaroslav Kysela { 15234e01e5dbSJaroslav Kysela char *dst = card->dmi_longname; 15244e01e5dbSJaroslav Kysela size_t dst_len = sizeof(card->dmi_longname); 15254e01e5dbSJaroslav Kysela size_t len; 15264e01e5dbSJaroslav Kysela 15274e01e5dbSJaroslav Kysela len = strlen(dst); 15284e01e5dbSJaroslav Kysela snprintf(dst + len, dst_len - len, "-%s", str); 15294e01e5dbSJaroslav Kysela 15304e01e5dbSJaroslav Kysela len++; /* skip the separator "-" */ 15314e01e5dbSJaroslav Kysela if (len < dst_len) 15324e01e5dbSJaroslav Kysela cleanup_dmi_name(dst + len); 15334e01e5dbSJaroslav Kysela } 15344e01e5dbSJaroslav Kysela 1535345233d7SLiam Girdwood /** 1536345233d7SLiam Girdwood * snd_soc_set_dmi_name() - Register DMI names to card 1537345233d7SLiam Girdwood * @card: The card to register DMI names 1538345233d7SLiam Girdwood * @flavour: The flavour "differentiator" for the card amongst its peers. 1539345233d7SLiam Girdwood * 1540345233d7SLiam Girdwood * An Intel machine driver may be used by many different devices but are 1541345233d7SLiam Girdwood * difficult for userspace to differentiate, since machine drivers ususally 1542345233d7SLiam Girdwood * use their own name as the card short name and leave the card long name 1543345233d7SLiam Girdwood * blank. To differentiate such devices and fix bugs due to lack of 1544345233d7SLiam Girdwood * device-specific configurations, this function allows DMI info to be used 1545345233d7SLiam Girdwood * as the sound card long name, in the format of 1546345233d7SLiam Girdwood * "vendor-product-version-board" 1547345233d7SLiam Girdwood * (Character '-' is used to separate different DMI fields here). 1548345233d7SLiam Girdwood * This will help the user space to load the device-specific Use Case Manager 1549345233d7SLiam Girdwood * (UCM) configurations for the card. 1550345233d7SLiam Girdwood * 1551345233d7SLiam Girdwood * Possible card long names may be: 1552345233d7SLiam Girdwood * DellInc.-XPS139343-01-0310JH 1553345233d7SLiam Girdwood * ASUSTeKCOMPUTERINC.-T100TA-1.0-T100TA 1554345233d7SLiam Girdwood * Circuitco-MinnowboardMaxD0PLATFORM-D0-MinnowBoardMAX 1555345233d7SLiam Girdwood * 1556345233d7SLiam Girdwood * This function also supports flavoring the card longname to provide 1557345233d7SLiam Girdwood * the extra differentiation, like "vendor-product-version-board-flavor". 1558345233d7SLiam Girdwood * 1559345233d7SLiam Girdwood * We only keep number and alphabet characters and a few separator characters 1560345233d7SLiam Girdwood * in the card long name since UCM in the user space uses the card long names 1561345233d7SLiam Girdwood * as card configuration directory names and AudoConf cannot support special 1562345233d7SLiam Girdwood * charactors like SPACE. 1563345233d7SLiam Girdwood * 1564345233d7SLiam Girdwood * Returns 0 on success, otherwise a negative error code. 1565345233d7SLiam Girdwood */ 1566345233d7SLiam Girdwood int snd_soc_set_dmi_name(struct snd_soc_card *card, const char *flavour) 1567345233d7SLiam Girdwood { 1568345233d7SLiam Girdwood const char *vendor, *product, *product_version, *board; 1569345233d7SLiam Girdwood 1570345233d7SLiam Girdwood if (card->long_name) 1571345233d7SLiam Girdwood return 0; /* long name already set by driver or from DMI */ 1572345233d7SLiam Girdwood 15734e01e5dbSJaroslav Kysela /* make up dmi long name as: vendor-product-version-board */ 1574345233d7SLiam Girdwood vendor = dmi_get_system_info(DMI_BOARD_VENDOR); 157598faf436SMengdong Lin if (!vendor || !is_dmi_valid(vendor)) { 1576345233d7SLiam Girdwood dev_warn(card->dev, "ASoC: no DMI vendor name!\n"); 1577345233d7SLiam Girdwood return 0; 1578345233d7SLiam Girdwood } 1579345233d7SLiam Girdwood 15804e01e5dbSJaroslav Kysela snprintf(card->dmi_longname, sizeof(card->dmi_longname), "%s", vendor); 1581345233d7SLiam Girdwood cleanup_dmi_name(card->dmi_longname); 1582345233d7SLiam Girdwood 1583345233d7SLiam Girdwood product = dmi_get_system_info(DMI_PRODUCT_NAME); 158498faf436SMengdong Lin if (product && is_dmi_valid(product)) { 15854e01e5dbSJaroslav Kysela append_dmi_string(card, product); 1586345233d7SLiam Girdwood 15872c7b696aSMarcel Ziswiler /* 15882c7b696aSMarcel Ziswiler * some vendors like Lenovo may only put a self-explanatory 1589345233d7SLiam Girdwood * name in the product version field 1590345233d7SLiam Girdwood */ 1591345233d7SLiam Girdwood product_version = dmi_get_system_info(DMI_PRODUCT_VERSION); 15924e01e5dbSJaroslav Kysela if (product_version && is_dmi_valid(product_version)) 15934e01e5dbSJaroslav Kysela append_dmi_string(card, product_version); 1594345233d7SLiam Girdwood } 1595345233d7SLiam Girdwood 1596345233d7SLiam Girdwood board = dmi_get_system_info(DMI_BOARD_NAME); 159798faf436SMengdong Lin if (board && is_dmi_valid(board)) { 159839870b0dSJaroslav Kysela if (!product || strcasecmp(board, product)) 15994e01e5dbSJaroslav Kysela append_dmi_string(card, board); 1600345233d7SLiam Girdwood } else if (!product) { 1601345233d7SLiam Girdwood /* fall back to using legacy name */ 1602345233d7SLiam Girdwood dev_warn(card->dev, "ASoC: no DMI board/product name!\n"); 1603345233d7SLiam Girdwood return 0; 1604345233d7SLiam Girdwood } 1605345233d7SLiam Girdwood 1606345233d7SLiam Girdwood /* Add flavour to dmi long name */ 16074e01e5dbSJaroslav Kysela if (flavour) 16084e01e5dbSJaroslav Kysela append_dmi_string(card, flavour); 1609345233d7SLiam Girdwood 1610345233d7SLiam Girdwood /* set the card long name */ 1611345233d7SLiam Girdwood card->long_name = card->dmi_longname; 1612345233d7SLiam Girdwood 1613345233d7SLiam Girdwood return 0; 1614345233d7SLiam Girdwood } 1615345233d7SLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_set_dmi_name); 16161f5a4535STakashi Iwai #endif /* CONFIG_DMI */ 1617345233d7SLiam Girdwood 1618a655de80SLiam Girdwood static void soc_check_tplg_fes(struct snd_soc_card *card) 1619a655de80SLiam Girdwood { 1620a655de80SLiam Girdwood struct snd_soc_component *component; 1621a655de80SLiam Girdwood const struct snd_soc_component_driver *comp_drv; 1622a655de80SLiam Girdwood struct snd_soc_dai_link *dai_link; 1623a655de80SLiam Girdwood int i; 1624a655de80SLiam Girdwood 1625368dee94SKuninori Morimoto for_each_component(component) { 1626a655de80SLiam Girdwood 162749f9c4f2SDaniel Baluta /* does this component override BEs ? */ 1628a655de80SLiam Girdwood if (!component->driver->ignore_machine) 1629a655de80SLiam Girdwood continue; 1630a655de80SLiam Girdwood 1631a655de80SLiam Girdwood /* for this machine ? */ 1632e194098bSPierre-Louis Bossart if (!strcmp(component->driver->ignore_machine, 1633a655de80SLiam Girdwood card->dev->driver->name)) 1634e194098bSPierre-Louis Bossart goto match; 1635e194098bSPierre-Louis Bossart if (strcmp(component->driver->ignore_machine, 1636e194098bSPierre-Louis Bossart dev_name(card->dev))) 1637a655de80SLiam Girdwood continue; 1638e194098bSPierre-Louis Bossart match: 1639a655de80SLiam Girdwood /* machine matches, so override the rtd data */ 16407fe072b4SKuninori Morimoto for_each_card_prelinks(card, i, dai_link) { 1641a655de80SLiam Girdwood 1642a655de80SLiam Girdwood /* ignore this FE */ 1643a655de80SLiam Girdwood if (dai_link->dynamic) { 1644a655de80SLiam Girdwood dai_link->ignore = true; 1645a655de80SLiam Girdwood continue; 1646a655de80SLiam Girdwood } 1647a655de80SLiam Girdwood 164849f9c4f2SDaniel Baluta dev_info(card->dev, "info: override BE DAI link %s\n", 1649a655de80SLiam Girdwood card->dai_link[i].name); 1650a655de80SLiam Girdwood 1651a655de80SLiam Girdwood /* override platform component */ 1652adb76b5bSKuninori Morimoto if (!dai_link->platforms) { 1653daecf46eSKuninori Morimoto dev_err(card->dev, "init platform error"); 1654daecf46eSKuninori Morimoto continue; 1655daecf46eSKuninori Morimoto } 1656910fdcabSKuninori Morimoto dai_link->platforms->name = component->name; 1657a655de80SLiam Girdwood 1658a655de80SLiam Girdwood /* convert non BE into BE */ 1659a655de80SLiam Girdwood dai_link->no_pcm = 1; 1660218fe9b7SDaniel Baluta dai_link->dpcm_playback = 1; 1661218fe9b7SDaniel Baluta dai_link->dpcm_capture = 1; 1662a655de80SLiam Girdwood 1663a655de80SLiam Girdwood /* override any BE fixups */ 1664a655de80SLiam Girdwood dai_link->be_hw_params_fixup = 1665a655de80SLiam Girdwood component->driver->be_hw_params_fixup; 1666a655de80SLiam Girdwood 16672c7b696aSMarcel Ziswiler /* 16682c7b696aSMarcel Ziswiler * most BE links don't set stream name, so set it to 1669a655de80SLiam Girdwood * dai link name if it's NULL to help bind widgets. 1670a655de80SLiam Girdwood */ 1671a655de80SLiam Girdwood if (!dai_link->stream_name) 1672a655de80SLiam Girdwood dai_link->stream_name = dai_link->name; 1673a655de80SLiam Girdwood } 1674a655de80SLiam Girdwood 1675a655de80SLiam Girdwood /* Inform userspace we are using alternate topology */ 1676a655de80SLiam Girdwood if (component->driver->topology_name_prefix) { 1677a655de80SLiam Girdwood 1678a655de80SLiam Girdwood /* topology shortname created? */ 1679a655de80SLiam Girdwood if (!card->topology_shortname_created) { 1680a655de80SLiam Girdwood comp_drv = component->driver; 1681a655de80SLiam Girdwood 1682a655de80SLiam Girdwood snprintf(card->topology_shortname, 32, "%s-%s", 1683a655de80SLiam Girdwood comp_drv->topology_name_prefix, 1684a655de80SLiam Girdwood card->name); 1685a655de80SLiam Girdwood card->topology_shortname_created = true; 1686a655de80SLiam Girdwood } 1687a655de80SLiam Girdwood 1688a655de80SLiam Girdwood /* use topology shortname */ 1689a655de80SLiam Girdwood card->name = card->topology_shortname; 1690a655de80SLiam Girdwood } 1691a655de80SLiam Girdwood } 1692a655de80SLiam Girdwood } 1693a655de80SLiam Girdwood 16940f23f718SKuninori Morimoto #define soc_setup_card_name(name, name1, name2, norm) \ 16950f23f718SKuninori Morimoto __soc_setup_card_name(name, sizeof(name), name1, name2, norm) 16960f23f718SKuninori Morimoto static void __soc_setup_card_name(char *name, int len, 16970f23f718SKuninori Morimoto const char *name1, const char *name2, 16980f23f718SKuninori Morimoto int normalization) 16990f23f718SKuninori Morimoto { 17000f23f718SKuninori Morimoto int i; 17010f23f718SKuninori Morimoto 17020f23f718SKuninori Morimoto snprintf(name, len, "%s", name1 ? name1 : name2); 17030f23f718SKuninori Morimoto 17040f23f718SKuninori Morimoto if (!normalization) 17050f23f718SKuninori Morimoto return; 17060f23f718SKuninori Morimoto 17070f23f718SKuninori Morimoto /* 17080f23f718SKuninori Morimoto * Name normalization 17090f23f718SKuninori Morimoto * 17100f23f718SKuninori Morimoto * The driver name is somewhat special, as it's used as a key for 17110f23f718SKuninori Morimoto * searches in the user-space. 17120f23f718SKuninori Morimoto * 17130f23f718SKuninori Morimoto * ex) 17140f23f718SKuninori Morimoto * "abcd??efg" -> "abcd__efg" 17150f23f718SKuninori Morimoto */ 17160f23f718SKuninori Morimoto for (i = 0; i < len; i++) { 17170f23f718SKuninori Morimoto switch (name[i]) { 17180f23f718SKuninori Morimoto case '_': 17190f23f718SKuninori Morimoto case '-': 17200f23f718SKuninori Morimoto case '\0': 17210f23f718SKuninori Morimoto break; 17220f23f718SKuninori Morimoto default: 17230f23f718SKuninori Morimoto if (!isalnum(name[i])) 17240f23f718SKuninori Morimoto name[i] = '_'; 17250f23f718SKuninori Morimoto break; 17260f23f718SKuninori Morimoto } 17270f23f718SKuninori Morimoto } 17280f23f718SKuninori Morimoto } 17290f23f718SKuninori Morimoto 1730ce21401cSKuninori Morimoto static void soc_cleanup_card_resources(struct snd_soc_card *card, 1731ce21401cSKuninori Morimoto int card_probed) 173253e947a0SKuninori Morimoto { 1733cc733900SKuninori Morimoto struct snd_soc_pcm_runtime *rtd, *n; 17347ce6088fSKuninori Morimoto 17350ced7b05SKuninori Morimoto if (card->snd_card) 17360ced7b05SKuninori Morimoto snd_card_disconnect_sync(card->snd_card); 173753e947a0SKuninori Morimoto 17382a6f0892SKuninori Morimoto snd_soc_dapm_shutdown(card); 17392a6f0892SKuninori Morimoto 174053e947a0SKuninori Morimoto /* remove and free each DAI */ 17417ce6088fSKuninori Morimoto soc_remove_link_dais(card); 17420ced7b05SKuninori Morimoto soc_remove_link_components(card); 17437ce6088fSKuninori Morimoto 1744cc733900SKuninori Morimoto for_each_card_rtds_safe(card, rtd, n) 174550cd9b53SKuninori Morimoto snd_soc_remove_pcm_runtime(card, rtd); 17467ce6088fSKuninori Morimoto 174753e947a0SKuninori Morimoto /* remove auxiliary devices */ 174853e947a0SKuninori Morimoto soc_remove_aux_devices(card); 1749e8fbd250SKuninori Morimoto soc_unbind_aux_dev(card); 175053e947a0SKuninori Morimoto 175153e947a0SKuninori Morimoto snd_soc_dapm_free(&card->dapm); 175253e947a0SKuninori Morimoto soc_cleanup_card_debugfs(card); 175353e947a0SKuninori Morimoto 175453e947a0SKuninori Morimoto /* remove the card */ 1755ce21401cSKuninori Morimoto if (card_probed && card->remove) 175653e947a0SKuninori Morimoto card->remove(card); 17570ced7b05SKuninori Morimoto 17580ced7b05SKuninori Morimoto if (card->snd_card) { 17590ced7b05SKuninori Morimoto snd_card_free(card->snd_card); 17600ced7b05SKuninori Morimoto card->snd_card = NULL; 17610ced7b05SKuninori Morimoto } 176253e947a0SKuninori Morimoto } 176353e947a0SKuninori Morimoto 17642cc1afcfSKuninori Morimoto static void snd_soc_unbind_card(struct snd_soc_card *card, bool unregister) 17652cc1afcfSKuninori Morimoto { 17662cc1afcfSKuninori Morimoto if (card->instantiated) { 1767ce21401cSKuninori Morimoto int card_probed = 1; 1768ce21401cSKuninori Morimoto 17692cc1afcfSKuninori Morimoto card->instantiated = false; 17702cc1afcfSKuninori Morimoto snd_soc_flush_all_delayed_work(card); 17712cc1afcfSKuninori Morimoto 1772ce21401cSKuninori Morimoto soc_cleanup_card_resources(card, card_probed); 17732cc1afcfSKuninori Morimoto if (!unregister) 17742cc1afcfSKuninori Morimoto list_add(&card->list, &unbind_card_list); 17752cc1afcfSKuninori Morimoto } else { 17762cc1afcfSKuninori Morimoto if (unregister) 17772cc1afcfSKuninori Morimoto list_del(&card->list); 17782cc1afcfSKuninori Morimoto } 17792cc1afcfSKuninori Morimoto } 17802cc1afcfSKuninori Morimoto 1781ed90c013SKuninori Morimoto static int snd_soc_bind_card(struct snd_soc_card *card) 1782f0fba2adSLiam Girdwood { 17831a497983SMengdong Lin struct snd_soc_pcm_runtime *rtd; 178476c39e86SKuninori Morimoto struct snd_soc_component *component; 178561b0088bSMengdong Lin struct snd_soc_dai_link *dai_link; 1786ce21401cSKuninori Morimoto int ret, i, card_probed = 0; 178796dd3622SMark Brown 178834e81ab4SLars-Peter Clausen mutex_lock(&client_mutex); 178901b9d99aSLiam Girdwood mutex_lock_nested(&card->mutex, SND_SOC_CARD_CLASS_INIT); 1790f0fba2adSLiam Girdwood 179195c267ddSKuninori Morimoto snd_soc_dapm_init(&card->dapm, card, NULL); 179253e947a0SKuninori Morimoto 1793a655de80SLiam Girdwood /* check whether any platform is ignore machine FE and using topology */ 1794a655de80SLiam Girdwood soc_check_tplg_fes(card); 1795a655de80SLiam Girdwood 179644c69bb1SLars-Peter Clausen /* bind aux_devs too */ 1797bee886f1SKuninori Morimoto ret = soc_bind_aux_dev(card); 1798bee886f1SKuninori Morimoto if (ret < 0) 179953e947a0SKuninori Morimoto goto probe_end; 1800db2a4165SFrank Mandarino 1801f8f80361SMengdong Lin /* add predefined DAI links to the list */ 1802d8145989SKuninori Morimoto card->num_rtd = 0; 18035b99a0aaSKuninori Morimoto for_each_card_prelinks(card, i, dai_link) { 18040c048004SKuninori Morimoto ret = snd_soc_add_pcm_runtime(card, dai_link); 18055b99a0aaSKuninori Morimoto if (ret < 0) 18065b99a0aaSKuninori Morimoto goto probe_end; 18075b99a0aaSKuninori Morimoto } 1808f8f80361SMengdong Lin 1809f0fba2adSLiam Girdwood /* card bind complete so register a sound card */ 1810102b5a8dSTakashi Iwai ret = snd_card_new(card->dev, SNDRV_DEFAULT_IDX1, SNDRV_DEFAULT_STR1, 1811f0fba2adSLiam Girdwood card->owner, 0, &card->snd_card); 1812f0fba2adSLiam Girdwood if (ret < 0) { 181310e8aa9aSMichał Mirosław dev_err(card->dev, 181410e8aa9aSMichał Mirosław "ASoC: can't create sound card for card %s: %d\n", 181510e8aa9aSMichał Mirosław card->name, ret); 181653e947a0SKuninori Morimoto goto probe_end; 1817db2a4165SFrank Mandarino } 1818db2a4165SFrank Mandarino 18190757d834SLars-Peter Clausen soc_init_card_debugfs(card); 18200757d834SLars-Peter Clausen 1821b3da4251SKuninori Morimoto soc_resume_init(card); 18226ed25978SAndy Green 1823b8ba3b57SKuninori Morimoto ret = snd_soc_dapm_new_controls(&card->dapm, card->dapm_widgets, 18249a841ebbSMark Brown card->num_dapm_widgets); 1825b8ba3b57SKuninori Morimoto if (ret < 0) 1826b8ba3b57SKuninori Morimoto goto probe_end; 18279a841ebbSMark Brown 1828b8ba3b57SKuninori Morimoto ret = snd_soc_dapm_new_controls(&card->dapm, card->of_dapm_widgets, 1829f23e860eSNicolin Chen card->num_of_dapm_widgets); 1830b8ba3b57SKuninori Morimoto if (ret < 0) 1831b8ba3b57SKuninori Morimoto goto probe_end; 1832f23e860eSNicolin Chen 1833f0fba2adSLiam Girdwood /* initialise the sound card only once */ 1834f0fba2adSLiam Girdwood if (card->probe) { 1835e7361ec4SMark Brown ret = card->probe(card); 1836f0fba2adSLiam Girdwood if (ret < 0) 183753e947a0SKuninori Morimoto goto probe_end; 1838ce21401cSKuninori Morimoto card_probed = 1; 1839f0fba2adSLiam Girdwood } 1840f0fba2adSLiam Girdwood 184162ae68faSStephen Warren /* probe all components used by DAI links on this card */ 184262f07a6bSKuninori Morimoto ret = soc_probe_link_components(card); 184362ae68faSStephen Warren if (ret < 0) { 1844f110bfc7SLiam Girdwood dev_err(card->dev, 184562f07a6bSKuninori Morimoto "ASoC: failed to instantiate card %d\n", ret); 184653e947a0SKuninori Morimoto goto probe_end; 184762ae68faSStephen Warren } 184862ae68faSStephen Warren 1849f2ed6b07SMengdong Lin /* probe auxiliary components */ 1850f2ed6b07SMengdong Lin ret = soc_probe_aux_devices(card); 185174bd3f92SKuninori Morimoto if (ret < 0) { 185274bd3f92SKuninori Morimoto dev_err(card->dev, 185374bd3f92SKuninori Morimoto "ASoC: failed to probe aux component %d\n", ret); 185453e947a0SKuninori Morimoto goto probe_end; 185574bd3f92SKuninori Morimoto } 1856f2ed6b07SMengdong Lin 185762ae68faSStephen Warren /* probe all DAI links on this card */ 1858c7e73774SKuninori Morimoto ret = soc_probe_link_dais(card); 1859fe3e78e0SMark Brown if (ret < 0) { 1860f110bfc7SLiam Girdwood dev_err(card->dev, 1861c7e73774SKuninori Morimoto "ASoC: failed to instantiate card %d\n", ret); 186253e947a0SKuninori Morimoto goto probe_end; 1863fe3e78e0SMark Brown } 1864fe3e78e0SMark Brown 1865626c2e57SKuninori Morimoto for_each_card_rtds(card, rtd) { 1866eaffeefbSKuninori Morimoto ret = soc_init_pcm_runtime(card, rtd); 1867626c2e57SKuninori Morimoto if (ret < 0) 1868626c2e57SKuninori Morimoto goto probe_end; 1869626c2e57SKuninori Morimoto } 1870c4b46982SKuninori Morimoto 1871888df395SMark Brown snd_soc_dapm_link_dai_widgets(card); 1872b893ea5fSLiam Girdwood snd_soc_dapm_connect_dai_link_widgets(card); 1873888df395SMark Brown 18749b98c7c2SKuninori Morimoto ret = snd_soc_add_card_controls(card, card->controls, 18752c7b696aSMarcel Ziswiler card->num_controls); 18769b98c7c2SKuninori Morimoto if (ret < 0) 18779b98c7c2SKuninori Morimoto goto probe_end; 1878b7af1dafSMark Brown 1879daa480bdSKuninori Morimoto ret = snd_soc_dapm_add_routes(&card->dapm, card->dapm_routes, 1880b8ad29deSMark Brown card->num_dapm_routes); 1881a22ae72bSPierre-Louis Bossart if (ret < 0) { 1882a22ae72bSPierre-Louis Bossart if (card->disable_route_checks) { 1883a22ae72bSPierre-Louis Bossart dev_info(card->dev, 1884a22ae72bSPierre-Louis Bossart "%s: disable_route_checks set, ignoring errors on add_routes\n", 1885a22ae72bSPierre-Louis Bossart __func__); 1886a22ae72bSPierre-Louis Bossart } else { 1887a22ae72bSPierre-Louis Bossart dev_err(card->dev, 1888a22ae72bSPierre-Louis Bossart "%s: snd_soc_dapm_add_routes failed: %d\n", 1889a22ae72bSPierre-Louis Bossart __func__, ret); 1890daa480bdSKuninori Morimoto goto probe_end; 1891a22ae72bSPierre-Louis Bossart } 1892a22ae72bSPierre-Louis Bossart } 1893b8ad29deSMark Brown 1894daa480bdSKuninori Morimoto ret = snd_soc_dapm_add_routes(&card->dapm, card->of_dapm_routes, 1895f23e860eSNicolin Chen card->num_of_dapm_routes); 1896daa480bdSKuninori Morimoto if (ret < 0) 1897daa480bdSKuninori Morimoto goto probe_end; 189875d9ac46SMark Brown 1899861886d3STakashi Iwai /* try to set some sane longname if DMI is available */ 1900861886d3STakashi Iwai snd_soc_set_dmi_name(card, NULL); 1901861886d3STakashi Iwai 19020f23f718SKuninori Morimoto soc_setup_card_name(card->snd_card->shortname, 19030f23f718SKuninori Morimoto card->name, NULL, 0); 19040f23f718SKuninori Morimoto soc_setup_card_name(card->snd_card->longname, 19050f23f718SKuninori Morimoto card->long_name, card->name, 0); 19060f23f718SKuninori Morimoto soc_setup_card_name(card->snd_card->driver, 19070f23f718SKuninori Morimoto card->driver_name, card->name, 1); 1908fe3e78e0SMark Brown 1909dc73d73aSJaroslav Kysela if (card->components) { 1910dc73d73aSJaroslav Kysela /* the current implementation of snd_component_add() accepts */ 1911dc73d73aSJaroslav Kysela /* multiple components in the string separated by space, */ 1912dc73d73aSJaroslav Kysela /* but the string collision (identical string) check might */ 1913dc73d73aSJaroslav Kysela /* not work correctly */ 1914dc73d73aSJaroslav Kysela ret = snd_component_add(card->snd_card, card->components); 1915dc73d73aSJaroslav Kysela if (ret < 0) { 1916dc73d73aSJaroslav Kysela dev_err(card->dev, "ASoC: %s snd_component_add() failed: %d\n", 1917dc73d73aSJaroslav Kysela card->name, ret); 1918dc73d73aSJaroslav Kysela goto probe_end; 1919dc73d73aSJaroslav Kysela } 1920dc73d73aSJaroslav Kysela } 1921dc73d73aSJaroslav Kysela 192228e9ad92SMark Brown if (card->late_probe) { 192328e9ad92SMark Brown ret = card->late_probe(card); 192428e9ad92SMark Brown if (ret < 0) { 1925f110bfc7SLiam Girdwood dev_err(card->dev, "ASoC: %s late_probe() failed: %d\n", 192628e9ad92SMark Brown card->name, ret); 192753e947a0SKuninori Morimoto goto probe_end; 192828e9ad92SMark Brown } 192928e9ad92SMark Brown } 1930ce21401cSKuninori Morimoto card_probed = 1; 193128e9ad92SMark Brown 1932824ef826SLars-Peter Clausen snd_soc_dapm_new_widgets(card); 19338c193b8dSLars-Peter Clausen 1934f0fba2adSLiam Girdwood ret = snd_card_register(card->snd_card); 1935fe3e78e0SMark Brown if (ret < 0) { 1936f110bfc7SLiam Girdwood dev_err(card->dev, "ASoC: failed to register soundcard %d\n", 1937f110bfc7SLiam Girdwood ret); 193853e947a0SKuninori Morimoto goto probe_end; 1939fe3e78e0SMark Brown } 1940fe3e78e0SMark Brown 1941435c5e25SMark Brown card->instantiated = 1; 1942882eab6cSTzung-Bi Shih dapm_mark_endpoints_dirty(card); 19434f4c0072SMark Brown snd_soc_dapm_sync(&card->dapm); 1944b19e6e7bSMark Brown 1945ed90c013SKuninori Morimoto /* deactivate pins to sleep state */ 194676c39e86SKuninori Morimoto for_each_card_components(card, component) 1947*b3dea624SKuninori Morimoto if (!snd_soc_component_active(component)) 194876c39e86SKuninori Morimoto pinctrl_pm_select_sleep_state(component->dev); 1949ed90c013SKuninori Morimoto 195053e947a0SKuninori Morimoto probe_end: 195153e947a0SKuninori Morimoto if (ret < 0) 1952ce21401cSKuninori Morimoto soc_cleanup_card_resources(card, card_probed); 1953db2a4165SFrank Mandarino 1954f0fba2adSLiam Girdwood mutex_unlock(&card->mutex); 195534e81ab4SLars-Peter Clausen mutex_unlock(&client_mutex); 1956db2a4165SFrank Mandarino 1957b19e6e7bSMark Brown return ret; 1958435c5e25SMark Brown } 1959435c5e25SMark Brown 1960435c5e25SMark Brown /* probes a new socdev */ 1961435c5e25SMark Brown static int soc_probe(struct platform_device *pdev) 1962435c5e25SMark Brown { 1963f0fba2adSLiam Girdwood struct snd_soc_card *card = platform_get_drvdata(pdev); 1964435c5e25SMark Brown 196570a7ca34SVinod Koul /* 196670a7ca34SVinod Koul * no card, so machine driver should be registering card 196770a7ca34SVinod Koul * we should not be here in that case so ret error 196870a7ca34SVinod Koul */ 196970a7ca34SVinod Koul if (!card) 197070a7ca34SVinod Koul return -EINVAL; 197170a7ca34SVinod Koul 1972fe4085e8SMark Brown dev_warn(&pdev->dev, 1973f110bfc7SLiam Girdwood "ASoC: machine %s should use snd_soc_register_card()\n", 1974fe4085e8SMark Brown card->name); 1975fe4085e8SMark Brown 1976435c5e25SMark Brown /* Bodge while we unpick instantiation */ 1977435c5e25SMark Brown card->dev = &pdev->dev; 1978f0fba2adSLiam Girdwood 197928d528c8SMark Brown return snd_soc_register_card(card); 1980435c5e25SMark Brown } 1981435c5e25SMark Brown 1982b0e26485SVinod Koul /* removes a socdev */ 1983b0e26485SVinod Koul static int soc_remove(struct platform_device *pdev) 1984b0e26485SVinod Koul { 1985b0e26485SVinod Koul struct snd_soc_card *card = platform_get_drvdata(pdev); 1986b0e26485SVinod Koul 1987c5af3a2eSMark Brown snd_soc_unregister_card(card); 1988db2a4165SFrank Mandarino return 0; 1989db2a4165SFrank Mandarino } 1990db2a4165SFrank Mandarino 19916f8ab4acSMark Brown int snd_soc_poweroff(struct device *dev) 199251737470SMark Brown { 19936f8ab4acSMark Brown struct snd_soc_card *card = dev_get_drvdata(dev); 199476c39e86SKuninori Morimoto struct snd_soc_component *component; 199551737470SMark Brown 199651737470SMark Brown if (!card->instantiated) 1997416356fcSMark Brown return 0; 199851737470SMark Brown 19992c7b696aSMarcel Ziswiler /* 20002c7b696aSMarcel Ziswiler * Flush out pmdown_time work - we actually do want to run it 20012c7b696aSMarcel Ziswiler * now, we're shutting down so no imminent restart. 20022c7b696aSMarcel Ziswiler */ 200365462e44SKuninori Morimoto snd_soc_flush_all_delayed_work(card); 200451737470SMark Brown 2005f0fba2adSLiam Girdwood snd_soc_dapm_shutdown(card); 2006416356fcSMark Brown 2007988e8cc4SNicolin Chen /* deactivate pins to sleep state */ 200876c39e86SKuninori Morimoto for_each_card_components(card, component) 200976c39e86SKuninori Morimoto pinctrl_pm_select_sleep_state(component->dev); 2010988e8cc4SNicolin Chen 2011416356fcSMark Brown return 0; 201251737470SMark Brown } 20136f8ab4acSMark Brown EXPORT_SYMBOL_GPL(snd_soc_poweroff); 201451737470SMark Brown 20156f8ab4acSMark Brown const struct dev_pm_ops snd_soc_pm_ops = { 2016b1dd5897SViresh Kumar .suspend = snd_soc_suspend, 2017b1dd5897SViresh Kumar .resume = snd_soc_resume, 2018b1dd5897SViresh Kumar .freeze = snd_soc_suspend, 2019b1dd5897SViresh Kumar .thaw = snd_soc_resume, 20206f8ab4acSMark Brown .poweroff = snd_soc_poweroff, 2021b1dd5897SViresh Kumar .restore = snd_soc_resume, 2022416356fcSMark Brown }; 2023deb2607eSStephen Warren EXPORT_SYMBOL_GPL(snd_soc_pm_ops); 2024416356fcSMark Brown 2025db2a4165SFrank Mandarino /* ASoC platform driver */ 2026db2a4165SFrank Mandarino static struct platform_driver soc_driver = { 2027db2a4165SFrank Mandarino .driver = { 2028db2a4165SFrank Mandarino .name = "soc-audio", 20296f8ab4acSMark Brown .pm = &snd_soc_pm_ops, 2030db2a4165SFrank Mandarino }, 2031db2a4165SFrank Mandarino .probe = soc_probe, 2032db2a4165SFrank Mandarino .remove = soc_remove, 2033db2a4165SFrank Mandarino }; 2034db2a4165SFrank Mandarino 2035096e49d5SMark Brown /** 2036db2a4165SFrank Mandarino * snd_soc_cnew - create new control 2037db2a4165SFrank Mandarino * @_template: control template 2038db2a4165SFrank Mandarino * @data: control private data 2039ac11a2b3SMark Brown * @long_name: control long name 2040efb7ac3fSMark Brown * @prefix: control name prefix 2041db2a4165SFrank Mandarino * 2042db2a4165SFrank Mandarino * Create a new mixer control from a template control. 2043db2a4165SFrank Mandarino * 2044db2a4165SFrank Mandarino * Returns 0 for success, else error. 2045db2a4165SFrank Mandarino */ 2046db2a4165SFrank Mandarino struct snd_kcontrol *snd_soc_cnew(const struct snd_kcontrol_new *_template, 20473056557fSMark Brown void *data, const char *long_name, 2048efb7ac3fSMark Brown const char *prefix) 2049db2a4165SFrank Mandarino { 2050db2a4165SFrank Mandarino struct snd_kcontrol_new template; 2051efb7ac3fSMark Brown struct snd_kcontrol *kcontrol; 2052efb7ac3fSMark Brown char *name = NULL; 2053db2a4165SFrank Mandarino 2054db2a4165SFrank Mandarino memcpy(&template, _template, sizeof(template)); 2055db2a4165SFrank Mandarino template.index = 0; 2056db2a4165SFrank Mandarino 2057efb7ac3fSMark Brown if (!long_name) 2058efb7ac3fSMark Brown long_name = template.name; 2059efb7ac3fSMark Brown 2060efb7ac3fSMark Brown if (prefix) { 20612b581074SLars-Peter Clausen name = kasprintf(GFP_KERNEL, "%s %s", prefix, long_name); 2062efb7ac3fSMark Brown if (!name) 2063efb7ac3fSMark Brown return NULL; 2064efb7ac3fSMark Brown 2065efb7ac3fSMark Brown template.name = name; 2066efb7ac3fSMark Brown } else { 2067efb7ac3fSMark Brown template.name = long_name; 2068efb7ac3fSMark Brown } 2069efb7ac3fSMark Brown 2070efb7ac3fSMark Brown kcontrol = snd_ctl_new1(&template, data); 2071efb7ac3fSMark Brown 2072efb7ac3fSMark Brown kfree(name); 2073efb7ac3fSMark Brown 2074efb7ac3fSMark Brown return kcontrol; 2075db2a4165SFrank Mandarino } 2076db2a4165SFrank Mandarino EXPORT_SYMBOL_GPL(snd_soc_cnew); 2077db2a4165SFrank Mandarino 2078022658beSLiam Girdwood static int snd_soc_add_controls(struct snd_card *card, struct device *dev, 2079022658beSLiam Girdwood const struct snd_kcontrol_new *controls, int num_controls, 2080022658beSLiam Girdwood const char *prefix, void *data) 2081022658beSLiam Girdwood { 2082022658beSLiam Girdwood int err, i; 2083022658beSLiam Girdwood 2084022658beSLiam Girdwood for (i = 0; i < num_controls; i++) { 2085022658beSLiam Girdwood const struct snd_kcontrol_new *control = &controls[i]; 20862c7b696aSMarcel Ziswiler 2087022658beSLiam Girdwood err = snd_ctl_add(card, snd_soc_cnew(control, data, 2088022658beSLiam Girdwood control->name, prefix)); 2089022658beSLiam Girdwood if (err < 0) { 2090f110bfc7SLiam Girdwood dev_err(dev, "ASoC: Failed to add %s: %d\n", 2091f110bfc7SLiam Girdwood control->name, err); 2092022658beSLiam Girdwood return err; 2093022658beSLiam Girdwood } 2094022658beSLiam Girdwood } 2095022658beSLiam Girdwood 2096022658beSLiam Girdwood return 0; 2097022658beSLiam Girdwood } 2098022658beSLiam Girdwood 20994fefd698SDimitris Papastamos struct snd_kcontrol *snd_soc_card_get_kcontrol(struct snd_soc_card *soc_card, 21004fefd698SDimitris Papastamos const char *name) 21014fefd698SDimitris Papastamos { 21024fefd698SDimitris Papastamos struct snd_card *card = soc_card->snd_card; 21034fefd698SDimitris Papastamos struct snd_kcontrol *kctl; 21044fefd698SDimitris Papastamos 21054fefd698SDimitris Papastamos if (unlikely(!name)) 21064fefd698SDimitris Papastamos return NULL; 21074fefd698SDimitris Papastamos 21084fefd698SDimitris Papastamos list_for_each_entry(kctl, &card->controls, list) 21094fefd698SDimitris Papastamos if (!strncmp(kctl->id.name, name, sizeof(kctl->id.name))) 21104fefd698SDimitris Papastamos return kctl; 21114fefd698SDimitris Papastamos return NULL; 21124fefd698SDimitris Papastamos } 21134fefd698SDimitris Papastamos EXPORT_SYMBOL_GPL(snd_soc_card_get_kcontrol); 21144fefd698SDimitris Papastamos 2115db2a4165SFrank Mandarino /** 21160f2780adSLars-Peter Clausen * snd_soc_add_component_controls - Add an array of controls to a component. 21170f2780adSLars-Peter Clausen * 21180f2780adSLars-Peter Clausen * @component: Component to add controls to 21190f2780adSLars-Peter Clausen * @controls: Array of controls to add 21200f2780adSLars-Peter Clausen * @num_controls: Number of elements in the array 21210f2780adSLars-Peter Clausen * 21220f2780adSLars-Peter Clausen * Return: 0 for success, else error. 21230f2780adSLars-Peter Clausen */ 21240f2780adSLars-Peter Clausen int snd_soc_add_component_controls(struct snd_soc_component *component, 21250f2780adSLars-Peter Clausen const struct snd_kcontrol_new *controls, unsigned int num_controls) 21260f2780adSLars-Peter Clausen { 21270f2780adSLars-Peter Clausen struct snd_card *card = component->card->snd_card; 21280f2780adSLars-Peter Clausen 21290f2780adSLars-Peter Clausen return snd_soc_add_controls(card, component->dev, controls, 21300f2780adSLars-Peter Clausen num_controls, component->name_prefix, component); 21310f2780adSLars-Peter Clausen } 21320f2780adSLars-Peter Clausen EXPORT_SYMBOL_GPL(snd_soc_add_component_controls); 21330f2780adSLars-Peter Clausen 21340f2780adSLars-Peter Clausen /** 2135022658beSLiam Girdwood * snd_soc_add_card_controls - add an array of controls to a SoC card. 2136022658beSLiam Girdwood * Convenience function to add a list of controls. 2137022658beSLiam Girdwood * 2138022658beSLiam Girdwood * @soc_card: SoC card to add controls to 2139022658beSLiam Girdwood * @controls: array of controls to add 2140022658beSLiam Girdwood * @num_controls: number of elements in the array 2141022658beSLiam Girdwood * 2142022658beSLiam Girdwood * Return 0 for success, else error. 2143022658beSLiam Girdwood */ 2144022658beSLiam Girdwood int snd_soc_add_card_controls(struct snd_soc_card *soc_card, 2145022658beSLiam Girdwood const struct snd_kcontrol_new *controls, int num_controls) 2146022658beSLiam Girdwood { 2147022658beSLiam Girdwood struct snd_card *card = soc_card->snd_card; 2148022658beSLiam Girdwood 2149022658beSLiam Girdwood return snd_soc_add_controls(card, soc_card->dev, controls, num_controls, 2150022658beSLiam Girdwood NULL, soc_card); 2151022658beSLiam Girdwood } 2152022658beSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_add_card_controls); 2153022658beSLiam Girdwood 2154022658beSLiam Girdwood /** 2155022658beSLiam Girdwood * snd_soc_add_dai_controls - add an array of controls to a DAI. 2156022658beSLiam Girdwood * Convienience function to add a list of controls. 2157022658beSLiam Girdwood * 2158022658beSLiam Girdwood * @dai: DAI to add controls to 2159022658beSLiam Girdwood * @controls: array of controls to add 2160022658beSLiam Girdwood * @num_controls: number of elements in the array 2161022658beSLiam Girdwood * 2162022658beSLiam Girdwood * Return 0 for success, else error. 2163022658beSLiam Girdwood */ 2164022658beSLiam Girdwood int snd_soc_add_dai_controls(struct snd_soc_dai *dai, 2165022658beSLiam Girdwood const struct snd_kcontrol_new *controls, int num_controls) 2166022658beSLiam Girdwood { 2167313665b9SLars-Peter Clausen struct snd_card *card = dai->component->card->snd_card; 2168022658beSLiam Girdwood 2169022658beSLiam Girdwood return snd_soc_add_controls(card, dai->dev, controls, num_controls, 2170022658beSLiam Girdwood NULL, dai); 2171022658beSLiam Girdwood } 2172022658beSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_add_dai_controls); 2173022658beSLiam Girdwood 2174c5af3a2eSMark Brown /** 2175c5af3a2eSMark Brown * snd_soc_register_card - Register a card with the ASoC core 2176c5af3a2eSMark Brown * 2177ac11a2b3SMark Brown * @card: Card to register 2178c5af3a2eSMark Brown * 2179c5af3a2eSMark Brown */ 218070a7ca34SVinod Koul int snd_soc_register_card(struct snd_soc_card *card) 2181c5af3a2eSMark Brown { 2182c5af3a2eSMark Brown if (!card->name || !card->dev) 2183c5af3a2eSMark Brown return -EINVAL; 2184c5af3a2eSMark Brown 2185ed77cc12SMark Brown dev_set_drvdata(card->dev, card); 2186ed77cc12SMark Brown 2187b03bfaecSKuninori Morimoto INIT_LIST_HEAD(&card->widgets); 2188b03bfaecSKuninori Morimoto INIT_LIST_HEAD(&card->paths); 2189b03bfaecSKuninori Morimoto INIT_LIST_HEAD(&card->dapm_list); 2190b03bfaecSKuninori Morimoto INIT_LIST_HEAD(&card->aux_comp_list); 2191b03bfaecSKuninori Morimoto INIT_LIST_HEAD(&card->component_dev_list); 2192b03bfaecSKuninori Morimoto INIT_LIST_HEAD(&card->list); 21931a497983SMengdong Lin INIT_LIST_HEAD(&card->rtd_list); 2194db432b41SMark Brown INIT_LIST_HEAD(&card->dapm_dirty); 21958a978234SLiam Girdwood INIT_LIST_HEAD(&card->dobj_list); 2196b03bfaecSKuninori Morimoto 2197c5af3a2eSMark Brown card->instantiated = 0; 2198f0fba2adSLiam Girdwood mutex_init(&card->mutex); 2199a73fb2dfSLiam Girdwood mutex_init(&card->dapm_mutex); 220072b745e3SPeter Ujfalusi mutex_init(&card->pcm_mutex); 2201a9764869SKaiChieh Chuang spin_lock_init(&card->dpcm_lock); 2202c5af3a2eSMark Brown 2203e894efefSSrinivas Kandagatla return snd_soc_bind_card(card); 2204c5af3a2eSMark Brown } 220570a7ca34SVinod Koul EXPORT_SYMBOL_GPL(snd_soc_register_card); 2206c5af3a2eSMark Brown 2207c5af3a2eSMark Brown /** 2208c5af3a2eSMark Brown * snd_soc_unregister_card - Unregister a card with the ASoC core 2209c5af3a2eSMark Brown * 2210ac11a2b3SMark Brown * @card: Card to unregister 2211c5af3a2eSMark Brown * 2212c5af3a2eSMark Brown */ 221370a7ca34SVinod Koul int snd_soc_unregister_card(struct snd_soc_card *card) 2214c5af3a2eSMark Brown { 2215b545542aSKuninori Morimoto mutex_lock(&client_mutex); 2216e894efefSSrinivas Kandagatla snd_soc_unbind_card(card, true); 2217b545542aSKuninori Morimoto mutex_unlock(&client_mutex); 2218f110bfc7SLiam Girdwood dev_dbg(card->dev, "ASoC: Unregistered card '%s'\n", card->name); 2219c5af3a2eSMark Brown 2220c5af3a2eSMark Brown return 0; 2221c5af3a2eSMark Brown } 222270a7ca34SVinod Koul EXPORT_SYMBOL_GPL(snd_soc_unregister_card); 2223c5af3a2eSMark Brown 2224f0fba2adSLiam Girdwood /* 2225f0fba2adSLiam Girdwood * Simplify DAI link configuration by removing ".-1" from device names 2226f0fba2adSLiam Girdwood * and sanitizing names. 2227f0fba2adSLiam Girdwood */ 22280b9a214aSDimitris Papastamos static char *fmt_single_name(struct device *dev, int *id) 2229f0fba2adSLiam Girdwood { 2230f0fba2adSLiam Girdwood char *found, name[NAME_SIZE]; 2231f0fba2adSLiam Girdwood int id1, id2; 2232f0fba2adSLiam Girdwood 2233f0fba2adSLiam Girdwood if (dev_name(dev) == NULL) 2234f0fba2adSLiam Girdwood return NULL; 2235f0fba2adSLiam Girdwood 223658818a77SDimitris Papastamos strlcpy(name, dev_name(dev), NAME_SIZE); 2237f0fba2adSLiam Girdwood 2238f0fba2adSLiam Girdwood /* are we a "%s.%d" name (platform and SPI components) */ 2239f0fba2adSLiam Girdwood found = strstr(name, dev->driver->name); 2240f0fba2adSLiam Girdwood if (found) { 2241f0fba2adSLiam Girdwood /* get ID */ 2242f0fba2adSLiam Girdwood if (sscanf(&found[strlen(dev->driver->name)], ".%d", id) == 1) { 2243f0fba2adSLiam Girdwood 2244f0fba2adSLiam Girdwood /* discard ID from name if ID == -1 */ 2245f0fba2adSLiam Girdwood if (*id == -1) 2246f0fba2adSLiam Girdwood found[strlen(dev->driver->name)] = '\0'; 2247f0fba2adSLiam Girdwood } 2248f0fba2adSLiam Girdwood 2249f0fba2adSLiam Girdwood } else { 2250f0fba2adSLiam Girdwood /* I2C component devices are named "bus-addr" */ 2251f0fba2adSLiam Girdwood if (sscanf(name, "%x-%x", &id1, &id2) == 2) { 2252f0fba2adSLiam Girdwood char tmp[NAME_SIZE]; 2253f0fba2adSLiam Girdwood 2254f0fba2adSLiam Girdwood /* create unique ID number from I2C addr and bus */ 225505899446SJarkko Nikula *id = ((id1 & 0xffff) << 16) + id2; 2256f0fba2adSLiam Girdwood 2257f0fba2adSLiam Girdwood /* sanitize component name for DAI link creation */ 22582c7b696aSMarcel Ziswiler snprintf(tmp, NAME_SIZE, "%s.%s", dev->driver->name, 22592c7b696aSMarcel Ziswiler name); 226058818a77SDimitris Papastamos strlcpy(name, tmp, NAME_SIZE); 2261f0fba2adSLiam Girdwood } else 2262f0fba2adSLiam Girdwood *id = 0; 2263f0fba2adSLiam Girdwood } 2264f0fba2adSLiam Girdwood 226550014499SKuninori Morimoto return devm_kstrdup(dev, name, GFP_KERNEL); 2266f0fba2adSLiam Girdwood } 2267f0fba2adSLiam Girdwood 2268f0fba2adSLiam Girdwood /* 2269f0fba2adSLiam Girdwood * Simplify DAI link naming for single devices with multiple DAIs by removing 2270f0fba2adSLiam Girdwood * any ".-1" and using the DAI name (instead of device name). 2271f0fba2adSLiam Girdwood */ 2272f0fba2adSLiam Girdwood static inline char *fmt_multiple_name(struct device *dev, 2273f0fba2adSLiam Girdwood struct snd_soc_dai_driver *dai_drv) 2274f0fba2adSLiam Girdwood { 2275f0fba2adSLiam Girdwood if (dai_drv->name == NULL) { 227610e8aa9aSMichał Mirosław dev_err(dev, 227710e8aa9aSMichał Mirosław "ASoC: error - multiple DAI %s registered with no name\n", 227810e8aa9aSMichał Mirosław dev_name(dev)); 2279f0fba2adSLiam Girdwood return NULL; 2280f0fba2adSLiam Girdwood } 2281f0fba2adSLiam Girdwood 228250014499SKuninori Morimoto return devm_kstrdup(dev, dai_drv->name, GFP_KERNEL); 2283f0fba2adSLiam Girdwood } 2284f0fba2adSLiam Girdwood 2285ffdbca0bSKuninori Morimoto void snd_soc_unregister_dai(struct snd_soc_dai *dai) 2286e11381f3SKuninori Morimoto { 2287e11381f3SKuninori Morimoto dev_dbg(dai->dev, "ASoC: Unregistered DAI '%s'\n", dai->name); 2288e11381f3SKuninori Morimoto list_del(&dai->list); 2289e11381f3SKuninori Morimoto } 2290ffdbca0bSKuninori Morimoto EXPORT_SYMBOL_GPL(snd_soc_unregister_dai); 2291e11381f3SKuninori Morimoto 22927ca24386SKuninori Morimoto /** 22937ca24386SKuninori Morimoto * snd_soc_register_dai - Register a DAI dynamically & create its widgets 22947ca24386SKuninori Morimoto * 22957ca24386SKuninori Morimoto * @component: The component the DAIs are registered for 22967ca24386SKuninori Morimoto * @dai_drv: DAI driver to use for the DAI 2297bc9a6655SRandy Dunlap * @legacy_dai_naming: if %true, use legacy single-name format; 2298bc9a6655SRandy Dunlap * if %false, use multiple-name format; 22997ca24386SKuninori Morimoto * 23007ca24386SKuninori Morimoto * Topology can use this API to register DAIs when probing a component. 23017ca24386SKuninori Morimoto * These DAIs's widgets will be freed in the card cleanup and the DAIs 23027ca24386SKuninori Morimoto * will be freed in the component cleanup. 23037ca24386SKuninori Morimoto */ 23047ca24386SKuninori Morimoto struct snd_soc_dai *snd_soc_register_dai(struct snd_soc_component *component, 23055e4fb372SMengdong Lin struct snd_soc_dai_driver *dai_drv, 23065e4fb372SMengdong Lin bool legacy_dai_naming) 23075e4fb372SMengdong Lin { 23085e4fb372SMengdong Lin struct device *dev = component->dev; 23095e4fb372SMengdong Lin struct snd_soc_dai *dai; 23105e4fb372SMengdong Lin 23115e4fb372SMengdong Lin dev_dbg(dev, "ASoC: dynamically register DAI %s\n", dev_name(dev)); 23125e4fb372SMengdong Lin 23137ca24386SKuninori Morimoto lockdep_assert_held(&client_mutex); 23147ca24386SKuninori Morimoto 231550014499SKuninori Morimoto dai = devm_kzalloc(dev, sizeof(*dai), GFP_KERNEL); 23165e4fb372SMengdong Lin if (dai == NULL) 23175e4fb372SMengdong Lin return NULL; 23185e4fb372SMengdong Lin 23195e4fb372SMengdong Lin /* 23205e4fb372SMengdong Lin * Back in the old days when we still had component-less DAIs, 23215e4fb372SMengdong Lin * instead of having a static name, component-less DAIs would 23225e4fb372SMengdong Lin * inherit the name of the parent device so it is possible to 23235e4fb372SMengdong Lin * register multiple instances of the DAI. We still need to keep 23245e4fb372SMengdong Lin * the same naming style even though those DAIs are not 23255e4fb372SMengdong Lin * component-less anymore. 23265e4fb372SMengdong Lin */ 23275e4fb372SMengdong Lin if (legacy_dai_naming && 23285e4fb372SMengdong Lin (dai_drv->id == 0 || dai_drv->name == NULL)) { 23295e4fb372SMengdong Lin dai->name = fmt_single_name(dev, &dai->id); 23305e4fb372SMengdong Lin } else { 23315e4fb372SMengdong Lin dai->name = fmt_multiple_name(dev, dai_drv); 23325e4fb372SMengdong Lin if (dai_drv->id) 23335e4fb372SMengdong Lin dai->id = dai_drv->id; 23345e4fb372SMengdong Lin else 23355e4fb372SMengdong Lin dai->id = component->num_dai; 23365e4fb372SMengdong Lin } 233750014499SKuninori Morimoto if (!dai->name) 23385e4fb372SMengdong Lin return NULL; 23395e4fb372SMengdong Lin 23405e4fb372SMengdong Lin dai->component = component; 23415e4fb372SMengdong Lin dai->dev = dev; 23425e4fb372SMengdong Lin dai->driver = dai_drv; 23435e4fb372SMengdong Lin 234415a0c645SKuninori Morimoto /* see for_each_component_dais */ 234558bf4179SKuninori Morimoto list_add_tail(&dai->list, &component->dai_list); 23465e4fb372SMengdong Lin component->num_dai++; 23475e4fb372SMengdong Lin 23485e4fb372SMengdong Lin dev_dbg(dev, "ASoC: Registered DAI '%s'\n", dai->name); 23495e4fb372SMengdong Lin return dai; 23505e4fb372SMengdong Lin } 2351e11381f3SKuninori Morimoto 23529115171aSMark Brown /** 23533f6674aeSKuninori Morimoto * snd_soc_unregister_dai - Unregister DAIs from the ASoC core 23543f6674aeSKuninori Morimoto * 23553f6674aeSKuninori Morimoto * @component: The component for which the DAIs should be unregistered 23563f6674aeSKuninori Morimoto */ 23573f6674aeSKuninori Morimoto static void snd_soc_unregister_dais(struct snd_soc_component *component) 23583f6674aeSKuninori Morimoto { 23593f6674aeSKuninori Morimoto struct snd_soc_dai *dai, *_dai; 23603f6674aeSKuninori Morimoto 2361e11381f3SKuninori Morimoto for_each_component_dais_safe(component, dai, _dai) 2362e11381f3SKuninori Morimoto snd_soc_unregister_dai(dai); 23633f6674aeSKuninori Morimoto } 23643f6674aeSKuninori Morimoto 23653f6674aeSKuninori Morimoto /** 2366daf77373SKuninori Morimoto * snd_soc_register_dais - Register a DAI with the ASoC core 2367daf77373SKuninori Morimoto * 2368daf77373SKuninori Morimoto * @component: The component the DAIs are registered for 2369daf77373SKuninori Morimoto * @dai_drv: DAI driver to use for the DAIs 2370daf77373SKuninori Morimoto * @count: Number of DAIs 2371daf77373SKuninori Morimoto */ 2372daf77373SKuninori Morimoto static int snd_soc_register_dais(struct snd_soc_component *component, 2373daf77373SKuninori Morimoto struct snd_soc_dai_driver *dai_drv, 2374daf77373SKuninori Morimoto size_t count) 2375daf77373SKuninori Morimoto { 2376daf77373SKuninori Morimoto struct snd_soc_dai *dai; 2377daf77373SKuninori Morimoto unsigned int i; 2378daf77373SKuninori Morimoto int ret; 2379daf77373SKuninori Morimoto 2380daf77373SKuninori Morimoto for (i = 0; i < count; i++) { 238171cb85f5SKuninori Morimoto dai = snd_soc_register_dai(component, dai_drv + i, count == 1 && 2382daf77373SKuninori Morimoto !component->driver->non_legacy_dai_naming); 2383daf77373SKuninori Morimoto if (dai == NULL) { 2384daf77373SKuninori Morimoto ret = -ENOMEM; 2385daf77373SKuninori Morimoto goto err; 2386daf77373SKuninori Morimoto } 2387daf77373SKuninori Morimoto } 2388daf77373SKuninori Morimoto 2389daf77373SKuninori Morimoto return 0; 2390daf77373SKuninori Morimoto 2391daf77373SKuninori Morimoto err: 2392daf77373SKuninori Morimoto snd_soc_unregister_dais(component); 2393daf77373SKuninori Morimoto 2394daf77373SKuninori Morimoto return ret; 2395daf77373SKuninori Morimoto } 2396daf77373SKuninori Morimoto 2397bb13109dSLars-Peter Clausen static int snd_soc_component_initialize(struct snd_soc_component *component, 2398bb13109dSLars-Peter Clausen const struct snd_soc_component_driver *driver, struct device *dev) 2399d191bd8dSKuninori Morimoto { 24008d92bb51SKuninori Morimoto INIT_LIST_HEAD(&component->dai_list); 2401495efdb0SKuninori Morimoto INIT_LIST_HEAD(&component->dobj_list); 2402495efdb0SKuninori Morimoto INIT_LIST_HEAD(&component->card_list); 24038d92bb51SKuninori Morimoto mutex_init(&component->io_mutex); 24048d92bb51SKuninori Morimoto 24056b62fa95SJerome Brunet component->name = fmt_single_name(dev, &component->id); 2406bb13109dSLars-Peter Clausen if (!component->name) { 2407bb13109dSLars-Peter Clausen dev_err(dev, "ASoC: Failed to allocate name\n"); 2408d191bd8dSKuninori Morimoto return -ENOMEM; 2409d191bd8dSKuninori Morimoto } 2410d191bd8dSKuninori Morimoto 2411bb13109dSLars-Peter Clausen component->dev = dev; 2412bb13109dSLars-Peter Clausen component->driver = driver; 2413e2c330b9SLars-Peter Clausen 2414bb13109dSLars-Peter Clausen return 0; 2415d191bd8dSKuninori Morimoto } 2416d191bd8dSKuninori Morimoto 241720feb881SLars-Peter Clausen static void snd_soc_component_setup_regmap(struct snd_soc_component *component) 2418886f5692SLars-Peter Clausen { 2419886f5692SLars-Peter Clausen int val_bytes = regmap_get_val_bytes(component->regmap); 242020feb881SLars-Peter Clausen 2421886f5692SLars-Peter Clausen /* Errors are legitimate for non-integer byte multiples */ 2422886f5692SLars-Peter Clausen if (val_bytes > 0) 2423886f5692SLars-Peter Clausen component->val_bytes = val_bytes; 2424886f5692SLars-Peter Clausen } 242520feb881SLars-Peter Clausen 2426e874bf5fSLars-Peter Clausen #ifdef CONFIG_REGMAP 2427e874bf5fSLars-Peter Clausen 242820feb881SLars-Peter Clausen /** 24292c7b696aSMarcel Ziswiler * snd_soc_component_init_regmap() - Initialize regmap instance for the 24302c7b696aSMarcel Ziswiler * component 243120feb881SLars-Peter Clausen * @component: The component for which to initialize the regmap instance 243220feb881SLars-Peter Clausen * @regmap: The regmap instance that should be used by the component 243320feb881SLars-Peter Clausen * 243420feb881SLars-Peter Clausen * This function allows deferred assignment of the regmap instance that is 243520feb881SLars-Peter Clausen * associated with the component. Only use this if the regmap instance is not 243620feb881SLars-Peter Clausen * yet ready when the component is registered. The function must also be called 243720feb881SLars-Peter Clausen * before the first IO attempt of the component. 243820feb881SLars-Peter Clausen */ 243920feb881SLars-Peter Clausen void snd_soc_component_init_regmap(struct snd_soc_component *component, 244020feb881SLars-Peter Clausen struct regmap *regmap) 244120feb881SLars-Peter Clausen { 244220feb881SLars-Peter Clausen component->regmap = regmap; 244320feb881SLars-Peter Clausen snd_soc_component_setup_regmap(component); 2444886f5692SLars-Peter Clausen } 244520feb881SLars-Peter Clausen EXPORT_SYMBOL_GPL(snd_soc_component_init_regmap); 244620feb881SLars-Peter Clausen 244720feb881SLars-Peter Clausen /** 24482c7b696aSMarcel Ziswiler * snd_soc_component_exit_regmap() - De-initialize regmap instance for the 24492c7b696aSMarcel Ziswiler * component 245020feb881SLars-Peter Clausen * @component: The component for which to de-initialize the regmap instance 245120feb881SLars-Peter Clausen * 245220feb881SLars-Peter Clausen * Calls regmap_exit() on the regmap instance associated to the component and 245320feb881SLars-Peter Clausen * removes the regmap instance from the component. 245420feb881SLars-Peter Clausen * 245520feb881SLars-Peter Clausen * This function should only be used if snd_soc_component_init_regmap() was used 245620feb881SLars-Peter Clausen * to initialize the regmap instance. 245720feb881SLars-Peter Clausen */ 245820feb881SLars-Peter Clausen void snd_soc_component_exit_regmap(struct snd_soc_component *component) 245920feb881SLars-Peter Clausen { 246020feb881SLars-Peter Clausen regmap_exit(component->regmap); 246120feb881SLars-Peter Clausen component->regmap = NULL; 246220feb881SLars-Peter Clausen } 246320feb881SLars-Peter Clausen EXPORT_SYMBOL_GPL(snd_soc_component_exit_regmap); 2464886f5692SLars-Peter Clausen 2465e874bf5fSLars-Peter Clausen #endif 2466d191bd8dSKuninori Morimoto 2467273d778eSKuninori Morimoto #define ENDIANNESS_MAP(name) \ 2468273d778eSKuninori Morimoto (SNDRV_PCM_FMTBIT_##name##LE | SNDRV_PCM_FMTBIT_##name##BE) 2469273d778eSKuninori Morimoto static u64 endianness_format_map[] = { 2470273d778eSKuninori Morimoto ENDIANNESS_MAP(S16_), 2471273d778eSKuninori Morimoto ENDIANNESS_MAP(U16_), 2472273d778eSKuninori Morimoto ENDIANNESS_MAP(S24_), 2473273d778eSKuninori Morimoto ENDIANNESS_MAP(U24_), 2474273d778eSKuninori Morimoto ENDIANNESS_MAP(S32_), 2475273d778eSKuninori Morimoto ENDIANNESS_MAP(U32_), 2476273d778eSKuninori Morimoto ENDIANNESS_MAP(S24_3), 2477273d778eSKuninori Morimoto ENDIANNESS_MAP(U24_3), 2478273d778eSKuninori Morimoto ENDIANNESS_MAP(S20_3), 2479273d778eSKuninori Morimoto ENDIANNESS_MAP(U20_3), 2480273d778eSKuninori Morimoto ENDIANNESS_MAP(S18_3), 2481273d778eSKuninori Morimoto ENDIANNESS_MAP(U18_3), 2482273d778eSKuninori Morimoto ENDIANNESS_MAP(FLOAT_), 2483273d778eSKuninori Morimoto ENDIANNESS_MAP(FLOAT64_), 2484273d778eSKuninori Morimoto ENDIANNESS_MAP(IEC958_SUBFRAME_), 2485273d778eSKuninori Morimoto }; 2486273d778eSKuninori Morimoto 2487273d778eSKuninori Morimoto /* 2488273d778eSKuninori Morimoto * Fix up the DAI formats for endianness: codecs don't actually see 2489273d778eSKuninori Morimoto * the endianness of the data but we're using the CPU format 2490273d778eSKuninori Morimoto * definitions which do need to include endianness so we ensure that 2491273d778eSKuninori Morimoto * codec DAIs always have both big and little endian variants set. 2492273d778eSKuninori Morimoto */ 2493273d778eSKuninori Morimoto static void convert_endianness_formats(struct snd_soc_pcm_stream *stream) 2494273d778eSKuninori Morimoto { 2495273d778eSKuninori Morimoto int i; 2496273d778eSKuninori Morimoto 2497273d778eSKuninori Morimoto for (i = 0; i < ARRAY_SIZE(endianness_format_map); i++) 2498273d778eSKuninori Morimoto if (stream->formats & endianness_format_map[i]) 2499273d778eSKuninori Morimoto stream->formats |= endianness_format_map[i]; 2500273d778eSKuninori Morimoto } 2501273d778eSKuninori Morimoto 2502e894efefSSrinivas Kandagatla static void snd_soc_try_rebind_card(void) 2503e894efefSSrinivas Kandagatla { 2504e894efefSSrinivas Kandagatla struct snd_soc_card *card, *c; 2505e894efefSSrinivas Kandagatla 2506b245d273SKuninori Morimoto list_for_each_entry_safe(card, c, &unbind_card_list, list) 2507e894efefSSrinivas Kandagatla if (!snd_soc_bind_card(card)) 2508e894efefSSrinivas Kandagatla list_del(&card->list); 2509e894efefSSrinivas Kandagatla } 2510e894efefSSrinivas Kandagatla 2511486c7978SKuninori Morimoto static void snd_soc_del_component_unlocked(struct snd_soc_component *component) 2512486c7978SKuninori Morimoto { 2513b18768f5SKuninori Morimoto struct snd_soc_card *card = component->card; 2514b18768f5SKuninori Morimoto 2515486c7978SKuninori Morimoto snd_soc_unregister_dais(component); 2516b18768f5SKuninori Morimoto 2517b18768f5SKuninori Morimoto if (card) 2518b18768f5SKuninori Morimoto snd_soc_unbind_card(card, false); 2519b18768f5SKuninori Morimoto 2520b18768f5SKuninori Morimoto list_del(&component->list); 2521486c7978SKuninori Morimoto } 2522486c7978SKuninori Morimoto 2523e0dac41bSKuninori Morimoto int snd_soc_add_component(struct device *dev, 2524e0dac41bSKuninori Morimoto struct snd_soc_component *component, 2525cf9e829eSKuninori Morimoto const struct snd_soc_component_driver *component_driver, 2526d191bd8dSKuninori Morimoto struct snd_soc_dai_driver *dai_drv, 2527d191bd8dSKuninori Morimoto int num_dai) 2528d191bd8dSKuninori Morimoto { 2529bb13109dSLars-Peter Clausen int ret; 2530273d778eSKuninori Morimoto int i; 2531d191bd8dSKuninori Morimoto 2532b18768f5SKuninori Morimoto mutex_lock(&client_mutex); 2533b18768f5SKuninori Morimoto 2534cf9e829eSKuninori Morimoto ret = snd_soc_component_initialize(component, component_driver, dev); 2535bb13109dSLars-Peter Clausen if (ret) 2536bb13109dSLars-Peter Clausen goto err_free; 2537bb13109dSLars-Peter Clausen 2538273d778eSKuninori Morimoto if (component_driver->endianness) { 2539273d778eSKuninori Morimoto for (i = 0; i < num_dai; i++) { 2540273d778eSKuninori Morimoto convert_endianness_formats(&dai_drv[i].playback); 2541273d778eSKuninori Morimoto convert_endianness_formats(&dai_drv[i].capture); 2542273d778eSKuninori Morimoto } 2543273d778eSKuninori Morimoto } 2544273d778eSKuninori Morimoto 25450e7b25c6SKuninori Morimoto ret = snd_soc_register_dais(component, dai_drv, num_dai); 2546bb13109dSLars-Peter Clausen if (ret < 0) { 2547f42cf8d6SMasanari Iida dev_err(dev, "ASoC: Failed to register DAIs: %d\n", ret); 2548bb13109dSLars-Peter Clausen goto err_cleanup; 2549bb13109dSLars-Peter Clausen } 2550bb13109dSLars-Peter Clausen 2551b18768f5SKuninori Morimoto if (!component->driver->write && !component->driver->read) { 2552b18768f5SKuninori Morimoto if (!component->regmap) 2553b18768f5SKuninori Morimoto component->regmap = dev_get_regmap(component->dev, 2554b18768f5SKuninori Morimoto NULL); 2555b18768f5SKuninori Morimoto if (component->regmap) 2556b18768f5SKuninori Morimoto snd_soc_component_setup_regmap(component); 2557b18768f5SKuninori Morimoto } 2558bb13109dSLars-Peter Clausen 2559b18768f5SKuninori Morimoto /* see for_each_component */ 2560b18768f5SKuninori Morimoto list_add(&component->list, &component_list); 2561bb13109dSLars-Peter Clausen 2562bb13109dSLars-Peter Clausen err_cleanup: 2563b18768f5SKuninori Morimoto if (ret < 0) 2564486c7978SKuninori Morimoto snd_soc_del_component_unlocked(component); 2565bb13109dSLars-Peter Clausen err_free: 2566b18768f5SKuninori Morimoto mutex_unlock(&client_mutex); 2567b18768f5SKuninori Morimoto 2568b18768f5SKuninori Morimoto if (ret == 0) 2569b18768f5SKuninori Morimoto snd_soc_try_rebind_card(); 2570b18768f5SKuninori Morimoto 2571bb13109dSLars-Peter Clausen return ret; 2572d191bd8dSKuninori Morimoto } 2573e0dac41bSKuninori Morimoto EXPORT_SYMBOL_GPL(snd_soc_add_component); 2574e0dac41bSKuninori Morimoto 2575e0dac41bSKuninori Morimoto int snd_soc_register_component(struct device *dev, 2576e0dac41bSKuninori Morimoto const struct snd_soc_component_driver *component_driver, 2577e0dac41bSKuninori Morimoto struct snd_soc_dai_driver *dai_drv, 2578e0dac41bSKuninori Morimoto int num_dai) 2579e0dac41bSKuninori Morimoto { 2580e0dac41bSKuninori Morimoto struct snd_soc_component *component; 2581e0dac41bSKuninori Morimoto 25827ecbd6a9SKuninori Morimoto component = devm_kzalloc(dev, sizeof(*component), GFP_KERNEL); 258308e61d03SKuninori Morimoto if (!component) 2584e0dac41bSKuninori Morimoto return -ENOMEM; 2585e0dac41bSKuninori Morimoto 2586e0dac41bSKuninori Morimoto return snd_soc_add_component(dev, component, component_driver, 2587e0dac41bSKuninori Morimoto dai_drv, num_dai); 2588e0dac41bSKuninori Morimoto } 2589d191bd8dSKuninori Morimoto EXPORT_SYMBOL_GPL(snd_soc_register_component); 2590d191bd8dSKuninori Morimoto 2591d191bd8dSKuninori Morimoto /** 25922eccea8cSKuninori Morimoto * snd_soc_unregister_component - Unregister all related component 25932eccea8cSKuninori Morimoto * from the ASoC core 2594d191bd8dSKuninori Morimoto * 2595628536eaSJonathan Corbet * @dev: The device to unregister 2596d191bd8dSKuninori Morimoto */ 25972eccea8cSKuninori Morimoto void snd_soc_unregister_component(struct device *dev) 25982eccea8cSKuninori Morimoto { 2599ac6a4dd3SKuninori Morimoto struct snd_soc_component *component; 2600ac6a4dd3SKuninori Morimoto 2601ac6a4dd3SKuninori Morimoto mutex_lock(&client_mutex); 2602ac6a4dd3SKuninori Morimoto while (1) { 260318dd66eaSKuninori Morimoto component = snd_soc_lookup_component_nolocked(dev, NULL); 2604ac6a4dd3SKuninori Morimoto if (!component) 2605ac6a4dd3SKuninori Morimoto break; 2606ac6a4dd3SKuninori Morimoto 2607ac6a4dd3SKuninori Morimoto snd_soc_del_component_unlocked(component); 2608ac6a4dd3SKuninori Morimoto } 2609ac6a4dd3SKuninori Morimoto mutex_unlock(&client_mutex); 2610d191bd8dSKuninori Morimoto } 2611d191bd8dSKuninori Morimoto EXPORT_SYMBOL_GPL(snd_soc_unregister_component); 2612d191bd8dSKuninori Morimoto 2613bec4fa05SStephen Warren /* Retrieve a card's name from device tree */ 2614b07609ceSKuninori Morimoto int snd_soc_of_parse_card_name(struct snd_soc_card *card, 2615bec4fa05SStephen Warren const char *propname) 2616bec4fa05SStephen Warren { 2617b07609ceSKuninori Morimoto struct device_node *np; 2618bec4fa05SStephen Warren int ret; 2619bec4fa05SStephen Warren 26207e07e7c0STushar Behera if (!card->dev) { 26217e07e7c0STushar Behera pr_err("card->dev is not set before calling %s\n", __func__); 26227e07e7c0STushar Behera return -EINVAL; 26237e07e7c0STushar Behera } 26247e07e7c0STushar Behera 26257e07e7c0STushar Behera np = card->dev->of_node; 26267e07e7c0STushar Behera 2627bec4fa05SStephen Warren ret = of_property_read_string_index(np, propname, 0, &card->name); 2628bec4fa05SStephen Warren /* 2629bec4fa05SStephen Warren * EINVAL means the property does not exist. This is fine providing 2630bec4fa05SStephen Warren * card->name was previously set, which is checked later in 2631bec4fa05SStephen Warren * snd_soc_register_card. 2632bec4fa05SStephen Warren */ 2633bec4fa05SStephen Warren if (ret < 0 && ret != -EINVAL) { 2634bec4fa05SStephen Warren dev_err(card->dev, 2635f110bfc7SLiam Girdwood "ASoC: Property '%s' could not be read: %d\n", 2636bec4fa05SStephen Warren propname, ret); 2637bec4fa05SStephen Warren return ret; 2638bec4fa05SStephen Warren } 2639bec4fa05SStephen Warren 2640bec4fa05SStephen Warren return 0; 2641bec4fa05SStephen Warren } 2642b07609ceSKuninori Morimoto EXPORT_SYMBOL_GPL(snd_soc_of_parse_card_name); 2643bec4fa05SStephen Warren 26449a6d4860SXiubo Li static const struct snd_soc_dapm_widget simple_widgets[] = { 26459a6d4860SXiubo Li SND_SOC_DAPM_MIC("Microphone", NULL), 26469a6d4860SXiubo Li SND_SOC_DAPM_LINE("Line", NULL), 26479a6d4860SXiubo Li SND_SOC_DAPM_HP("Headphone", NULL), 26489a6d4860SXiubo Li SND_SOC_DAPM_SPK("Speaker", NULL), 26499a6d4860SXiubo Li }; 26509a6d4860SXiubo Li 265121efde50SKuninori Morimoto int snd_soc_of_parse_audio_simple_widgets(struct snd_soc_card *card, 26529a6d4860SXiubo Li const char *propname) 26539a6d4860SXiubo Li { 265421efde50SKuninori Morimoto struct device_node *np = card->dev->of_node; 26559a6d4860SXiubo Li struct snd_soc_dapm_widget *widgets; 26569a6d4860SXiubo Li const char *template, *wname; 26579a6d4860SXiubo Li int i, j, num_widgets, ret; 26589a6d4860SXiubo Li 26599a6d4860SXiubo Li num_widgets = of_property_count_strings(np, propname); 26609a6d4860SXiubo Li if (num_widgets < 0) { 26619a6d4860SXiubo Li dev_err(card->dev, 26629a6d4860SXiubo Li "ASoC: Property '%s' does not exist\n", propname); 26639a6d4860SXiubo Li return -EINVAL; 26649a6d4860SXiubo Li } 26659a6d4860SXiubo Li if (num_widgets & 1) { 26669a6d4860SXiubo Li dev_err(card->dev, 26679a6d4860SXiubo Li "ASoC: Property '%s' length is not even\n", propname); 26689a6d4860SXiubo Li return -EINVAL; 26699a6d4860SXiubo Li } 26709a6d4860SXiubo Li 26719a6d4860SXiubo Li num_widgets /= 2; 26729a6d4860SXiubo Li if (!num_widgets) { 26739a6d4860SXiubo Li dev_err(card->dev, "ASoC: Property '%s's length is zero\n", 26749a6d4860SXiubo Li propname); 26759a6d4860SXiubo Li return -EINVAL; 26769a6d4860SXiubo Li } 26779a6d4860SXiubo Li 26789a6d4860SXiubo Li widgets = devm_kcalloc(card->dev, num_widgets, sizeof(*widgets), 26799a6d4860SXiubo Li GFP_KERNEL); 26809a6d4860SXiubo Li if (!widgets) { 26819a6d4860SXiubo Li dev_err(card->dev, 26829a6d4860SXiubo Li "ASoC: Could not allocate memory for widgets\n"); 26839a6d4860SXiubo Li return -ENOMEM; 26849a6d4860SXiubo Li } 26859a6d4860SXiubo Li 26869a6d4860SXiubo Li for (i = 0; i < num_widgets; i++) { 26879a6d4860SXiubo Li ret = of_property_read_string_index(np, propname, 26889a6d4860SXiubo Li 2 * i, &template); 26899a6d4860SXiubo Li if (ret) { 26909a6d4860SXiubo Li dev_err(card->dev, 26919a6d4860SXiubo Li "ASoC: Property '%s' index %d read error:%d\n", 26929a6d4860SXiubo Li propname, 2 * i, ret); 26939a6d4860SXiubo Li return -EINVAL; 26949a6d4860SXiubo Li } 26959a6d4860SXiubo Li 26969a6d4860SXiubo Li for (j = 0; j < ARRAY_SIZE(simple_widgets); j++) { 26979a6d4860SXiubo Li if (!strncmp(template, simple_widgets[j].name, 26989a6d4860SXiubo Li strlen(simple_widgets[j].name))) { 26999a6d4860SXiubo Li widgets[i] = simple_widgets[j]; 27009a6d4860SXiubo Li break; 27019a6d4860SXiubo Li } 27029a6d4860SXiubo Li } 27039a6d4860SXiubo Li 27049a6d4860SXiubo Li if (j >= ARRAY_SIZE(simple_widgets)) { 27059a6d4860SXiubo Li dev_err(card->dev, 27069a6d4860SXiubo Li "ASoC: DAPM widget '%s' is not supported\n", 27079a6d4860SXiubo Li template); 27089a6d4860SXiubo Li return -EINVAL; 27099a6d4860SXiubo Li } 27109a6d4860SXiubo Li 27119a6d4860SXiubo Li ret = of_property_read_string_index(np, propname, 27129a6d4860SXiubo Li (2 * i) + 1, 27139a6d4860SXiubo Li &wname); 27149a6d4860SXiubo Li if (ret) { 27159a6d4860SXiubo Li dev_err(card->dev, 27169a6d4860SXiubo Li "ASoC: Property '%s' index %d read error:%d\n", 27179a6d4860SXiubo Li propname, (2 * i) + 1, ret); 27189a6d4860SXiubo Li return -EINVAL; 27199a6d4860SXiubo Li } 27209a6d4860SXiubo Li 27219a6d4860SXiubo Li widgets[i].name = wname; 27229a6d4860SXiubo Li } 27239a6d4860SXiubo Li 2724f23e860eSNicolin Chen card->of_dapm_widgets = widgets; 2725f23e860eSNicolin Chen card->num_of_dapm_widgets = num_widgets; 27269a6d4860SXiubo Li 27279a6d4860SXiubo Li return 0; 27289a6d4860SXiubo Li } 272921efde50SKuninori Morimoto EXPORT_SYMBOL_GPL(snd_soc_of_parse_audio_simple_widgets); 27309a6d4860SXiubo Li 2731cbdfab3bSJerome Brunet int snd_soc_of_get_slot_mask(struct device_node *np, 27326131084aSJyri Sarha const char *prop_name, 27336131084aSJyri Sarha unsigned int *mask) 27346131084aSJyri Sarha { 27356131084aSJyri Sarha u32 val; 27366c84e591SJyri Sarha const __be32 *of_slot_mask = of_get_property(np, prop_name, &val); 27376131084aSJyri Sarha int i; 27386131084aSJyri Sarha 27396131084aSJyri Sarha if (!of_slot_mask) 27406131084aSJyri Sarha return 0; 27416131084aSJyri Sarha val /= sizeof(u32); 27426131084aSJyri Sarha for (i = 0; i < val; i++) 27436131084aSJyri Sarha if (be32_to_cpup(&of_slot_mask[i])) 27446131084aSJyri Sarha *mask |= (1 << i); 27456131084aSJyri Sarha 27466131084aSJyri Sarha return val; 27476131084aSJyri Sarha } 2748cbdfab3bSJerome Brunet EXPORT_SYMBOL_GPL(snd_soc_of_get_slot_mask); 27496131084aSJyri Sarha 275089c67857SXiubo Li int snd_soc_of_parse_tdm_slot(struct device_node *np, 27516131084aSJyri Sarha unsigned int *tx_mask, 27526131084aSJyri Sarha unsigned int *rx_mask, 275389c67857SXiubo Li unsigned int *slots, 275489c67857SXiubo Li unsigned int *slot_width) 275589c67857SXiubo Li { 275689c67857SXiubo Li u32 val; 275789c67857SXiubo Li int ret; 275889c67857SXiubo Li 27596131084aSJyri Sarha if (tx_mask) 27606131084aSJyri Sarha snd_soc_of_get_slot_mask(np, "dai-tdm-slot-tx-mask", tx_mask); 27616131084aSJyri Sarha if (rx_mask) 27626131084aSJyri Sarha snd_soc_of_get_slot_mask(np, "dai-tdm-slot-rx-mask", rx_mask); 27636131084aSJyri Sarha 276489c67857SXiubo Li if (of_property_read_bool(np, "dai-tdm-slot-num")) { 276589c67857SXiubo Li ret = of_property_read_u32(np, "dai-tdm-slot-num", &val); 276689c67857SXiubo Li if (ret) 276789c67857SXiubo Li return ret; 276889c67857SXiubo Li 276989c67857SXiubo Li if (slots) 277089c67857SXiubo Li *slots = val; 277189c67857SXiubo Li } 277289c67857SXiubo Li 277389c67857SXiubo Li if (of_property_read_bool(np, "dai-tdm-slot-width")) { 277489c67857SXiubo Li ret = of_property_read_u32(np, "dai-tdm-slot-width", &val); 277589c67857SXiubo Li if (ret) 277689c67857SXiubo Li return ret; 277789c67857SXiubo Li 277889c67857SXiubo Li if (slot_width) 277989c67857SXiubo Li *slot_width = val; 278089c67857SXiubo Li } 278189c67857SXiubo Li 278289c67857SXiubo Li return 0; 278389c67857SXiubo Li } 278489c67857SXiubo Li EXPORT_SYMBOL_GPL(snd_soc_of_parse_tdm_slot); 278589c67857SXiubo Li 27863b710356SKuninori Morimoto void snd_soc_of_parse_node_prefix(struct device_node *np, 27875e3cdaa2SKuninori Morimoto struct snd_soc_codec_conf *codec_conf, 27885e3cdaa2SKuninori Morimoto struct device_node *of_node, 27895e3cdaa2SKuninori Morimoto const char *propname) 27905e3cdaa2SKuninori Morimoto { 27915e3cdaa2SKuninori Morimoto const char *str; 27925e3cdaa2SKuninori Morimoto int ret; 27935e3cdaa2SKuninori Morimoto 27945e3cdaa2SKuninori Morimoto ret = of_property_read_string(np, propname, &str); 27955e3cdaa2SKuninori Morimoto if (ret < 0) { 27965e3cdaa2SKuninori Morimoto /* no prefix is not error */ 27975e3cdaa2SKuninori Morimoto return; 27985e3cdaa2SKuninori Morimoto } 27995e3cdaa2SKuninori Morimoto 2800c13493a2SKuninori Morimoto codec_conf->dlc.of_node = of_node; 28015e3cdaa2SKuninori Morimoto codec_conf->name_prefix = str; 28025e3cdaa2SKuninori Morimoto } 28033b710356SKuninori Morimoto EXPORT_SYMBOL_GPL(snd_soc_of_parse_node_prefix); 28045e3cdaa2SKuninori Morimoto 28052bc644afSKuninori Morimoto int snd_soc_of_parse_audio_routing(struct snd_soc_card *card, 2806a4a54dd5SStephen Warren const char *propname) 2807a4a54dd5SStephen Warren { 28082bc644afSKuninori Morimoto struct device_node *np = card->dev->of_node; 2809e3b1e6a1SMark Brown int num_routes; 2810a4a54dd5SStephen Warren struct snd_soc_dapm_route *routes; 2811a4a54dd5SStephen Warren int i, ret; 2812a4a54dd5SStephen Warren 2813a4a54dd5SStephen Warren num_routes = of_property_count_strings(np, propname); 2814c34ce320SRichard Zhao if (num_routes < 0 || num_routes & 1) { 281510e8aa9aSMichał Mirosław dev_err(card->dev, 281610e8aa9aSMichał Mirosław "ASoC: Property '%s' does not exist or its length is not even\n", 281710e8aa9aSMichał Mirosław propname); 2818a4a54dd5SStephen Warren return -EINVAL; 2819a4a54dd5SStephen Warren } 2820a4a54dd5SStephen Warren num_routes /= 2; 2821a4a54dd5SStephen Warren if (!num_routes) { 2822f110bfc7SLiam Girdwood dev_err(card->dev, "ASoC: Property '%s's length is zero\n", 2823a4a54dd5SStephen Warren propname); 2824a4a54dd5SStephen Warren return -EINVAL; 2825a4a54dd5SStephen Warren } 2826a4a54dd5SStephen Warren 2827a86854d0SKees Cook routes = devm_kcalloc(card->dev, num_routes, sizeof(*routes), 2828a4a54dd5SStephen Warren GFP_KERNEL); 2829a4a54dd5SStephen Warren if (!routes) { 2830a4a54dd5SStephen Warren dev_err(card->dev, 2831f110bfc7SLiam Girdwood "ASoC: Could not allocate DAPM route table\n"); 2832a4a54dd5SStephen Warren return -EINVAL; 2833a4a54dd5SStephen Warren } 2834a4a54dd5SStephen Warren 2835a4a54dd5SStephen Warren for (i = 0; i < num_routes; i++) { 2836a4a54dd5SStephen Warren ret = of_property_read_string_index(np, propname, 2837e3b1e6a1SMark Brown 2 * i, &routes[i].sink); 2838a4a54dd5SStephen Warren if (ret) { 2839c871bd0bSMark Brown dev_err(card->dev, 2840c871bd0bSMark Brown "ASoC: Property '%s' index %d could not be read: %d\n", 2841c871bd0bSMark Brown propname, 2 * i, ret); 2842a4a54dd5SStephen Warren return -EINVAL; 2843a4a54dd5SStephen Warren } 2844a4a54dd5SStephen Warren ret = of_property_read_string_index(np, propname, 2845e3b1e6a1SMark Brown (2 * i) + 1, &routes[i].source); 2846a4a54dd5SStephen Warren if (ret) { 2847a4a54dd5SStephen Warren dev_err(card->dev, 2848c871bd0bSMark Brown "ASoC: Property '%s' index %d could not be read: %d\n", 2849c871bd0bSMark Brown propname, (2 * i) + 1, ret); 2850a4a54dd5SStephen Warren return -EINVAL; 2851a4a54dd5SStephen Warren } 2852a4a54dd5SStephen Warren } 2853a4a54dd5SStephen Warren 2854f23e860eSNicolin Chen card->num_of_dapm_routes = num_routes; 2855f23e860eSNicolin Chen card->of_dapm_routes = routes; 2856a4a54dd5SStephen Warren 2857a4a54dd5SStephen Warren return 0; 2858a4a54dd5SStephen Warren } 28592bc644afSKuninori Morimoto EXPORT_SYMBOL_GPL(snd_soc_of_parse_audio_routing); 2860a4a54dd5SStephen Warren 2861a7930ed4SKuninori Morimoto unsigned int snd_soc_of_parse_daifmt(struct device_node *np, 2862389cb834SJyri Sarha const char *prefix, 2863389cb834SJyri Sarha struct device_node **bitclkmaster, 2864389cb834SJyri Sarha struct device_node **framemaster) 2865a7930ed4SKuninori Morimoto { 2866a7930ed4SKuninori Morimoto int ret, i; 2867a7930ed4SKuninori Morimoto char prop[128]; 2868a7930ed4SKuninori Morimoto unsigned int format = 0; 2869a7930ed4SKuninori Morimoto int bit, frame; 2870a7930ed4SKuninori Morimoto const char *str; 2871a7930ed4SKuninori Morimoto struct { 2872a7930ed4SKuninori Morimoto char *name; 2873a7930ed4SKuninori Morimoto unsigned int val; 2874a7930ed4SKuninori Morimoto } of_fmt_table[] = { 2875a7930ed4SKuninori Morimoto { "i2s", SND_SOC_DAIFMT_I2S }, 2876a7930ed4SKuninori Morimoto { "right_j", SND_SOC_DAIFMT_RIGHT_J }, 2877a7930ed4SKuninori Morimoto { "left_j", SND_SOC_DAIFMT_LEFT_J }, 2878a7930ed4SKuninori Morimoto { "dsp_a", SND_SOC_DAIFMT_DSP_A }, 2879a7930ed4SKuninori Morimoto { "dsp_b", SND_SOC_DAIFMT_DSP_B }, 2880a7930ed4SKuninori Morimoto { "ac97", SND_SOC_DAIFMT_AC97 }, 2881a7930ed4SKuninori Morimoto { "pdm", SND_SOC_DAIFMT_PDM}, 2882a7930ed4SKuninori Morimoto { "msb", SND_SOC_DAIFMT_MSB }, 2883a7930ed4SKuninori Morimoto { "lsb", SND_SOC_DAIFMT_LSB }, 2884a7930ed4SKuninori Morimoto }; 2885a7930ed4SKuninori Morimoto 2886a7930ed4SKuninori Morimoto if (!prefix) 2887a7930ed4SKuninori Morimoto prefix = ""; 2888a7930ed4SKuninori Morimoto 2889a7930ed4SKuninori Morimoto /* 28905711c979SKuninori Morimoto * check "dai-format = xxx" 28915711c979SKuninori Morimoto * or "[prefix]format = xxx" 2892a7930ed4SKuninori Morimoto * SND_SOC_DAIFMT_FORMAT_MASK area 2893a7930ed4SKuninori Morimoto */ 28945711c979SKuninori Morimoto ret = of_property_read_string(np, "dai-format", &str); 28955711c979SKuninori Morimoto if (ret < 0) { 2896a7930ed4SKuninori Morimoto snprintf(prop, sizeof(prop), "%sformat", prefix); 2897a7930ed4SKuninori Morimoto ret = of_property_read_string(np, prop, &str); 28985711c979SKuninori Morimoto } 2899a7930ed4SKuninori Morimoto if (ret == 0) { 2900a7930ed4SKuninori Morimoto for (i = 0; i < ARRAY_SIZE(of_fmt_table); i++) { 2901a7930ed4SKuninori Morimoto if (strcmp(str, of_fmt_table[i].name) == 0) { 2902a7930ed4SKuninori Morimoto format |= of_fmt_table[i].val; 2903a7930ed4SKuninori Morimoto break; 2904a7930ed4SKuninori Morimoto } 2905a7930ed4SKuninori Morimoto } 2906a7930ed4SKuninori Morimoto } 2907a7930ed4SKuninori Morimoto 2908a7930ed4SKuninori Morimoto /* 29098c2d6a9fSKuninori Morimoto * check "[prefix]continuous-clock" 2910a7930ed4SKuninori Morimoto * SND_SOC_DAIFMT_CLOCK_MASK area 2911a7930ed4SKuninori Morimoto */ 29128c2d6a9fSKuninori Morimoto snprintf(prop, sizeof(prop), "%scontinuous-clock", prefix); 291351930295SJulia Lawall if (of_property_read_bool(np, prop)) 29148c2d6a9fSKuninori Morimoto format |= SND_SOC_DAIFMT_CONT; 29158c2d6a9fSKuninori Morimoto else 29168c2d6a9fSKuninori Morimoto format |= SND_SOC_DAIFMT_GATED; 2917a7930ed4SKuninori Morimoto 2918a7930ed4SKuninori Morimoto /* 2919a7930ed4SKuninori Morimoto * check "[prefix]bitclock-inversion" 2920a7930ed4SKuninori Morimoto * check "[prefix]frame-inversion" 2921a7930ed4SKuninori Morimoto * SND_SOC_DAIFMT_INV_MASK area 2922a7930ed4SKuninori Morimoto */ 2923a7930ed4SKuninori Morimoto snprintf(prop, sizeof(prop), "%sbitclock-inversion", prefix); 2924a7930ed4SKuninori Morimoto bit = !!of_get_property(np, prop, NULL); 2925a7930ed4SKuninori Morimoto 2926a7930ed4SKuninori Morimoto snprintf(prop, sizeof(prop), "%sframe-inversion", prefix); 2927a7930ed4SKuninori Morimoto frame = !!of_get_property(np, prop, NULL); 2928a7930ed4SKuninori Morimoto 2929a7930ed4SKuninori Morimoto switch ((bit << 4) + frame) { 2930a7930ed4SKuninori Morimoto case 0x11: 2931a7930ed4SKuninori Morimoto format |= SND_SOC_DAIFMT_IB_IF; 2932a7930ed4SKuninori Morimoto break; 2933a7930ed4SKuninori Morimoto case 0x10: 2934a7930ed4SKuninori Morimoto format |= SND_SOC_DAIFMT_IB_NF; 2935a7930ed4SKuninori Morimoto break; 2936a7930ed4SKuninori Morimoto case 0x01: 2937a7930ed4SKuninori Morimoto format |= SND_SOC_DAIFMT_NB_IF; 2938a7930ed4SKuninori Morimoto break; 2939a7930ed4SKuninori Morimoto default: 2940a7930ed4SKuninori Morimoto /* SND_SOC_DAIFMT_NB_NF is default */ 2941a7930ed4SKuninori Morimoto break; 2942a7930ed4SKuninori Morimoto } 2943a7930ed4SKuninori Morimoto 2944a7930ed4SKuninori Morimoto /* 2945a7930ed4SKuninori Morimoto * check "[prefix]bitclock-master" 2946a7930ed4SKuninori Morimoto * check "[prefix]frame-master" 2947a7930ed4SKuninori Morimoto * SND_SOC_DAIFMT_MASTER_MASK area 2948a7930ed4SKuninori Morimoto */ 2949a7930ed4SKuninori Morimoto snprintf(prop, sizeof(prop), "%sbitclock-master", prefix); 2950a7930ed4SKuninori Morimoto bit = !!of_get_property(np, prop, NULL); 2951389cb834SJyri Sarha if (bit && bitclkmaster) 2952389cb834SJyri Sarha *bitclkmaster = of_parse_phandle(np, prop, 0); 2953a7930ed4SKuninori Morimoto 2954a7930ed4SKuninori Morimoto snprintf(prop, sizeof(prop), "%sframe-master", prefix); 2955a7930ed4SKuninori Morimoto frame = !!of_get_property(np, prop, NULL); 2956389cb834SJyri Sarha if (frame && framemaster) 2957389cb834SJyri Sarha *framemaster = of_parse_phandle(np, prop, 0); 2958a7930ed4SKuninori Morimoto 2959a7930ed4SKuninori Morimoto switch ((bit << 4) + frame) { 2960a7930ed4SKuninori Morimoto case 0x11: 2961a7930ed4SKuninori Morimoto format |= SND_SOC_DAIFMT_CBM_CFM; 2962a7930ed4SKuninori Morimoto break; 2963a7930ed4SKuninori Morimoto case 0x10: 2964a7930ed4SKuninori Morimoto format |= SND_SOC_DAIFMT_CBM_CFS; 2965a7930ed4SKuninori Morimoto break; 2966a7930ed4SKuninori Morimoto case 0x01: 2967a7930ed4SKuninori Morimoto format |= SND_SOC_DAIFMT_CBS_CFM; 2968a7930ed4SKuninori Morimoto break; 2969a7930ed4SKuninori Morimoto default: 2970a7930ed4SKuninori Morimoto format |= SND_SOC_DAIFMT_CBS_CFS; 2971a7930ed4SKuninori Morimoto break; 2972a7930ed4SKuninori Morimoto } 2973a7930ed4SKuninori Morimoto 2974a7930ed4SKuninori Morimoto return format; 2975a7930ed4SKuninori Morimoto } 2976a7930ed4SKuninori Morimoto EXPORT_SYMBOL_GPL(snd_soc_of_parse_daifmt); 2977a7930ed4SKuninori Morimoto 2978a180e8b9SKuninori Morimoto int snd_soc_get_dai_id(struct device_node *ep) 2979a180e8b9SKuninori Morimoto { 298009d4cc03SKuninori Morimoto struct snd_soc_component *component; 2981c1e230f0SKuninori Morimoto struct snd_soc_dai_link_component dlc; 2982a180e8b9SKuninori Morimoto int ret; 2983a180e8b9SKuninori Morimoto 2984c1e230f0SKuninori Morimoto dlc.of_node = of_graph_get_port_parent(ep); 2985c1e230f0SKuninori Morimoto dlc.name = NULL; 2986a180e8b9SKuninori Morimoto /* 2987a180e8b9SKuninori Morimoto * For example HDMI case, HDMI has video/sound port, 2988a180e8b9SKuninori Morimoto * but ALSA SoC needs sound port number only. 2989a180e8b9SKuninori Morimoto * Thus counting HDMI DT port/endpoint doesn't work. 2990a180e8b9SKuninori Morimoto * Then, it should have .of_xlate_dai_id 2991a180e8b9SKuninori Morimoto */ 2992a180e8b9SKuninori Morimoto ret = -ENOTSUPP; 2993a180e8b9SKuninori Morimoto mutex_lock(&client_mutex); 2994c1e230f0SKuninori Morimoto component = soc_find_component(&dlc); 29952c7b1704SKuninori Morimoto if (component) 29962c7b1704SKuninori Morimoto ret = snd_soc_component_of_xlate_dai_id(component, ep); 2997a180e8b9SKuninori Morimoto mutex_unlock(&client_mutex); 2998a180e8b9SKuninori Morimoto 2999c1e230f0SKuninori Morimoto of_node_put(dlc.of_node); 3000c0a480d1STony Lindgren 3001a180e8b9SKuninori Morimoto return ret; 3002a180e8b9SKuninori Morimoto } 3003a180e8b9SKuninori Morimoto EXPORT_SYMBOL_GPL(snd_soc_get_dai_id); 3004a180e8b9SKuninori Morimoto 30051ad8ec53SKuninori Morimoto int snd_soc_get_dai_name(struct of_phandle_args *args, 3006cb470087SKuninori Morimoto const char **dai_name) 3007cb470087SKuninori Morimoto { 3008cb470087SKuninori Morimoto struct snd_soc_component *pos; 30093e0aa8d8SJyri Sarha struct device_node *component_of_node; 301093b0f3eeSJean-Francois Moine int ret = -EPROBE_DEFER; 3011cb470087SKuninori Morimoto 3012cb470087SKuninori Morimoto mutex_lock(&client_mutex); 3013368dee94SKuninori Morimoto for_each_component(pos) { 3014c0834440SKuninori Morimoto component_of_node = soc_component_to_node(pos); 30153e0aa8d8SJyri Sarha 30163e0aa8d8SJyri Sarha if (component_of_node != args->np) 3017cb470087SKuninori Morimoto continue; 3018cb470087SKuninori Morimoto 3019a2a34175SKuninori Morimoto ret = snd_soc_component_of_xlate_dai_name(pos, args, dai_name); 3020a2a34175SKuninori Morimoto if (ret == -ENOTSUPP) { 302158bf4179SKuninori Morimoto struct snd_soc_dai *dai; 30226833c452SKuninori Morimoto int id = -1; 30236833c452SKuninori Morimoto 302493b0f3eeSJean-Francois Moine switch (args->args_count) { 30256833c452SKuninori Morimoto case 0: 30266833c452SKuninori Morimoto id = 0; /* same as dai_drv[0] */ 30276833c452SKuninori Morimoto break; 30286833c452SKuninori Morimoto case 1: 302993b0f3eeSJean-Francois Moine id = args->args[0]; 30306833c452SKuninori Morimoto break; 30316833c452SKuninori Morimoto default: 30326833c452SKuninori Morimoto /* not supported */ 3033cb470087SKuninori Morimoto break; 3034cb470087SKuninori Morimoto } 3035cb470087SKuninori Morimoto 30366833c452SKuninori Morimoto if (id < 0 || id >= pos->num_dai) { 30376833c452SKuninori Morimoto ret = -EINVAL; 30383dcba280SNicolin Chen continue; 30396833c452SKuninori Morimoto } 3040e41975edSXiubo Li 3041e41975edSXiubo Li ret = 0; 3042e41975edSXiubo Li 304358bf4179SKuninori Morimoto /* find target DAI */ 304415a0c645SKuninori Morimoto for_each_component_dais(pos, dai) { 304558bf4179SKuninori Morimoto if (id == 0) 304658bf4179SKuninori Morimoto break; 304758bf4179SKuninori Morimoto id--; 304858bf4179SKuninori Morimoto } 304958bf4179SKuninori Morimoto 305058bf4179SKuninori Morimoto *dai_name = dai->driver->name; 3051e41975edSXiubo Li if (!*dai_name) 3052e41975edSXiubo Li *dai_name = pos->name; 30531dfa5a5aSJerome Brunet } else if (ret) { 30541dfa5a5aSJerome Brunet /* 30551dfa5a5aSJerome Brunet * if another error than ENOTSUPP is returned go on and 30561dfa5a5aSJerome Brunet * check if another component is provided with the same 30571dfa5a5aSJerome Brunet * node. This may happen if a device provides several 30581dfa5a5aSJerome Brunet * components 30591dfa5a5aSJerome Brunet */ 30601dfa5a5aSJerome Brunet continue; 30616833c452SKuninori Morimoto } 30626833c452SKuninori Morimoto 3063cb470087SKuninori Morimoto break; 3064cb470087SKuninori Morimoto } 3065cb470087SKuninori Morimoto mutex_unlock(&client_mutex); 306693b0f3eeSJean-Francois Moine return ret; 306793b0f3eeSJean-Francois Moine } 30681ad8ec53SKuninori Morimoto EXPORT_SYMBOL_GPL(snd_soc_get_dai_name); 306993b0f3eeSJean-Francois Moine 307093b0f3eeSJean-Francois Moine int snd_soc_of_get_dai_name(struct device_node *of_node, 307193b0f3eeSJean-Francois Moine const char **dai_name) 307293b0f3eeSJean-Francois Moine { 307393b0f3eeSJean-Francois Moine struct of_phandle_args args; 307493b0f3eeSJean-Francois Moine int ret; 307593b0f3eeSJean-Francois Moine 307693b0f3eeSJean-Francois Moine ret = of_parse_phandle_with_args(of_node, "sound-dai", 307793b0f3eeSJean-Francois Moine "#sound-dai-cells", 0, &args); 307893b0f3eeSJean-Francois Moine if (ret) 307993b0f3eeSJean-Francois Moine return ret; 308093b0f3eeSJean-Francois Moine 308193b0f3eeSJean-Francois Moine ret = snd_soc_get_dai_name(&args, dai_name); 3082cb470087SKuninori Morimoto 3083cb470087SKuninori Morimoto of_node_put(args.np); 3084cb470087SKuninori Morimoto 3085cb470087SKuninori Morimoto return ret; 3086cb470087SKuninori Morimoto } 3087cb470087SKuninori Morimoto EXPORT_SYMBOL_GPL(snd_soc_of_get_dai_name); 3088cb470087SKuninori Morimoto 308993b0f3eeSJean-Francois Moine /* 309094685763SSylwester Nawrocki * snd_soc_of_put_dai_link_codecs - Dereference device nodes in the codecs array 309194685763SSylwester Nawrocki * @dai_link: DAI link 309294685763SSylwester Nawrocki * 309394685763SSylwester Nawrocki * Dereference device nodes acquired by snd_soc_of_get_dai_link_codecs(). 309494685763SSylwester Nawrocki */ 309594685763SSylwester Nawrocki void snd_soc_of_put_dai_link_codecs(struct snd_soc_dai_link *dai_link) 309694685763SSylwester Nawrocki { 30973db769f1SKuninori Morimoto struct snd_soc_dai_link_component *component; 309894685763SSylwester Nawrocki int index; 309994685763SSylwester Nawrocki 31003db769f1SKuninori Morimoto for_each_link_codecs(dai_link, index, component) { 310194685763SSylwester Nawrocki if (!component->of_node) 310294685763SSylwester Nawrocki break; 310394685763SSylwester Nawrocki of_node_put(component->of_node); 310494685763SSylwester Nawrocki component->of_node = NULL; 310594685763SSylwester Nawrocki } 310694685763SSylwester Nawrocki } 310794685763SSylwester Nawrocki EXPORT_SYMBOL_GPL(snd_soc_of_put_dai_link_codecs); 310894685763SSylwester Nawrocki 310994685763SSylwester Nawrocki /* 311093b0f3eeSJean-Francois Moine * snd_soc_of_get_dai_link_codecs - Parse a list of CODECs in the devicetree 311193b0f3eeSJean-Francois Moine * @dev: Card device 311293b0f3eeSJean-Francois Moine * @of_node: Device node 311393b0f3eeSJean-Francois Moine * @dai_link: DAI link 311493b0f3eeSJean-Francois Moine * 311593b0f3eeSJean-Francois Moine * Builds an array of CODEC DAI components from the DAI link property 311693b0f3eeSJean-Francois Moine * 'sound-dai'. 311793b0f3eeSJean-Francois Moine * The array is set in the DAI link and the number of DAIs is set accordingly. 311894685763SSylwester Nawrocki * The device nodes in the array (of_node) must be dereferenced by calling 311994685763SSylwester Nawrocki * snd_soc_of_put_dai_link_codecs() on @dai_link. 312093b0f3eeSJean-Francois Moine * 312193b0f3eeSJean-Francois Moine * Returns 0 for success 312293b0f3eeSJean-Francois Moine */ 312393b0f3eeSJean-Francois Moine int snd_soc_of_get_dai_link_codecs(struct device *dev, 312493b0f3eeSJean-Francois Moine struct device_node *of_node, 312593b0f3eeSJean-Francois Moine struct snd_soc_dai_link *dai_link) 312693b0f3eeSJean-Francois Moine { 312793b0f3eeSJean-Francois Moine struct of_phandle_args args; 312893b0f3eeSJean-Francois Moine struct snd_soc_dai_link_component *component; 312993b0f3eeSJean-Francois Moine char *name; 313093b0f3eeSJean-Francois Moine int index, num_codecs, ret; 313193b0f3eeSJean-Francois Moine 313293b0f3eeSJean-Francois Moine /* Count the number of CODECs */ 313393b0f3eeSJean-Francois Moine name = "sound-dai"; 313493b0f3eeSJean-Francois Moine num_codecs = of_count_phandle_with_args(of_node, name, 313593b0f3eeSJean-Francois Moine "#sound-dai-cells"); 313693b0f3eeSJean-Francois Moine if (num_codecs <= 0) { 313793b0f3eeSJean-Francois Moine if (num_codecs == -ENOENT) 313893b0f3eeSJean-Francois Moine dev_err(dev, "No 'sound-dai' property\n"); 313993b0f3eeSJean-Francois Moine else 314093b0f3eeSJean-Francois Moine dev_err(dev, "Bad phandle in 'sound-dai'\n"); 314193b0f3eeSJean-Francois Moine return num_codecs; 314293b0f3eeSJean-Francois Moine } 3143a86854d0SKees Cook component = devm_kcalloc(dev, 3144a86854d0SKees Cook num_codecs, sizeof(*component), 314593b0f3eeSJean-Francois Moine GFP_KERNEL); 314693b0f3eeSJean-Francois Moine if (!component) 314793b0f3eeSJean-Francois Moine return -ENOMEM; 314893b0f3eeSJean-Francois Moine dai_link->codecs = component; 314993b0f3eeSJean-Francois Moine dai_link->num_codecs = num_codecs; 315093b0f3eeSJean-Francois Moine 315193b0f3eeSJean-Francois Moine /* Parse the list */ 31523db769f1SKuninori Morimoto for_each_link_codecs(dai_link, index, component) { 315393b0f3eeSJean-Francois Moine ret = of_parse_phandle_with_args(of_node, name, 315493b0f3eeSJean-Francois Moine "#sound-dai-cells", 315593b0f3eeSJean-Francois Moine index, &args); 315693b0f3eeSJean-Francois Moine if (ret) 315793b0f3eeSJean-Francois Moine goto err; 315893b0f3eeSJean-Francois Moine component->of_node = args.np; 315993b0f3eeSJean-Francois Moine ret = snd_soc_get_dai_name(&args, &component->dai_name); 316093b0f3eeSJean-Francois Moine if (ret < 0) 316193b0f3eeSJean-Francois Moine goto err; 316293b0f3eeSJean-Francois Moine } 316393b0f3eeSJean-Francois Moine return 0; 316493b0f3eeSJean-Francois Moine err: 316594685763SSylwester Nawrocki snd_soc_of_put_dai_link_codecs(dai_link); 316693b0f3eeSJean-Francois Moine dai_link->codecs = NULL; 316793b0f3eeSJean-Francois Moine dai_link->num_codecs = 0; 316893b0f3eeSJean-Francois Moine return ret; 316993b0f3eeSJean-Francois Moine } 317093b0f3eeSJean-Francois Moine EXPORT_SYMBOL_GPL(snd_soc_of_get_dai_link_codecs); 317193b0f3eeSJean-Francois Moine 3172c9b3a40fSTakashi Iwai static int __init snd_soc_init(void) 3173db2a4165SFrank Mandarino { 31746553bf06SLars-Peter Clausen snd_soc_debugfs_init(); 3175fb257897SMark Brown snd_soc_util_init(); 3176fb257897SMark Brown 3177db2a4165SFrank Mandarino return platform_driver_register(&soc_driver); 3178db2a4165SFrank Mandarino } 31794abe8e16SMark Brown module_init(snd_soc_init); 3180db2a4165SFrank Mandarino 31817d8c16a6SMark Brown static void __exit snd_soc_exit(void) 3182db2a4165SFrank Mandarino { 3183fb257897SMark Brown snd_soc_util_exit(); 31846553bf06SLars-Peter Clausen snd_soc_debugfs_exit(); 3185fb257897SMark Brown 3186db2a4165SFrank Mandarino platform_driver_unregister(&soc_driver); 3187db2a4165SFrank Mandarino } 3188db2a4165SFrank Mandarino module_exit(snd_soc_exit); 3189db2a4165SFrank Mandarino 3190db2a4165SFrank Mandarino /* Module information */ 3191d331124dSLiam Girdwood MODULE_AUTHOR("Liam Girdwood, lrg@slimlogic.co.uk"); 3192db2a4165SFrank Mandarino MODULE_DESCRIPTION("ALSA SoC Core"); 3193db2a4165SFrank Mandarino MODULE_LICENSE("GPL"); 31948b45a209SKay Sievers MODULE_ALIAS("platform:soc-audio"); 3195