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> 4102e75636SKuninori Morimoto #include <sound/soc-link.h> 42db2a4165SFrank Mandarino #include <sound/initval.h> 43db2a4165SFrank Mandarino 44a8b1d34fSMark Brown #define CREATE_TRACE_POINTS 45a8b1d34fSMark Brown #include <trace/events/asoc.h> 46a8b1d34fSMark Brown 47f0fba2adSLiam Girdwood #define NAME_SIZE 32 48f0fba2adSLiam Girdwood 49c5af3a2eSMark Brown static DEFINE_MUTEX(client_mutex); 50030e79f6SKuninori Morimoto static LIST_HEAD(component_list); 51e894efefSSrinivas Kandagatla static LIST_HEAD(unbind_card_list); 52c5af3a2eSMark Brown 53368dee94SKuninori Morimoto #define for_each_component(component) \ 54368dee94SKuninori Morimoto list_for_each_entry(component, &component_list, list) 55368dee94SKuninori Morimoto 56db2a4165SFrank Mandarino /* 57587c9844SKuninori Morimoto * This is used if driver don't need to have CPU/Codec/Platform 58587c9844SKuninori Morimoto * dai_link. see soc.h 59587c9844SKuninori Morimoto */ 60587c9844SKuninori Morimoto struct snd_soc_dai_link_component null_dailink_component[0]; 61587c9844SKuninori Morimoto EXPORT_SYMBOL_GPL(null_dailink_component); 62587c9844SKuninori Morimoto 63587c9844SKuninori Morimoto /* 64db2a4165SFrank Mandarino * This is a timeout to do a DAPM powerdown after a stream is closed(). 65db2a4165SFrank Mandarino * It can be used to eliminate pops between different playback streams, e.g. 66db2a4165SFrank Mandarino * between two audio tracks. 67db2a4165SFrank Mandarino */ 68db2a4165SFrank Mandarino static int pmdown_time = 5000; 69db2a4165SFrank Mandarino module_param(pmdown_time, int, 0); 70db2a4165SFrank Mandarino MODULE_PARM_DESC(pmdown_time, "DAPM stream powerdown time (msecs)"); 71db2a4165SFrank Mandarino 72dbe21408SMark Brown static ssize_t pmdown_time_show(struct device *dev, 73dbe21408SMark Brown struct device_attribute *attr, char *buf) 74dbe21408SMark Brown { 7536ae1a96SMark Brown struct snd_soc_pcm_runtime *rtd = dev_get_drvdata(dev); 76dbe21408SMark Brown 77f0fba2adSLiam Girdwood return sprintf(buf, "%ld\n", rtd->pmdown_time); 78dbe21408SMark Brown } 79dbe21408SMark Brown 80dbe21408SMark Brown static ssize_t pmdown_time_set(struct device *dev, 81dbe21408SMark Brown struct device_attribute *attr, 82dbe21408SMark Brown const char *buf, size_t count) 83dbe21408SMark Brown { 8436ae1a96SMark Brown struct snd_soc_pcm_runtime *rtd = dev_get_drvdata(dev); 85c593b520SMark Brown int ret; 86dbe21408SMark Brown 87b785a492SJingoo Han ret = kstrtol(buf, 10, &rtd->pmdown_time); 88c593b520SMark Brown if (ret) 89c593b520SMark Brown return ret; 90dbe21408SMark Brown 91dbe21408SMark Brown return count; 92dbe21408SMark Brown } 93dbe21408SMark Brown 94dbe21408SMark Brown static DEVICE_ATTR(pmdown_time, 0644, pmdown_time_show, pmdown_time_set); 95dbe21408SMark Brown 96d29697dcSTakashi Iwai static struct attribute *soc_dev_attrs[] = { 97d29697dcSTakashi Iwai &dev_attr_pmdown_time.attr, 98d29697dcSTakashi Iwai NULL 99d29697dcSTakashi Iwai }; 100d29697dcSTakashi Iwai 101d29697dcSTakashi Iwai static umode_t soc_dev_attr_is_visible(struct kobject *kobj, 102d29697dcSTakashi Iwai struct attribute *attr, int idx) 103d29697dcSTakashi Iwai { 104d29697dcSTakashi Iwai struct device *dev = kobj_to_dev(kobj); 105d29697dcSTakashi Iwai struct snd_soc_pcm_runtime *rtd = dev_get_drvdata(dev); 106d29697dcSTakashi Iwai 107d918a376SKuninori Morimoto if (!rtd) 108d918a376SKuninori Morimoto return 0; 109d918a376SKuninori Morimoto 110d29697dcSTakashi Iwai if (attr == &dev_attr_pmdown_time.attr) 111d29697dcSTakashi Iwai return attr->mode; /* always visible */ 1123b6eed8dSKuninori Morimoto return rtd->num_codecs ? attr->mode : 0; /* enabled only with codec */ 113d29697dcSTakashi Iwai } 114d29697dcSTakashi Iwai 115d29697dcSTakashi Iwai static const struct attribute_group soc_dapm_dev_group = { 116d29697dcSTakashi Iwai .attrs = soc_dapm_dev_attrs, 117d29697dcSTakashi Iwai .is_visible = soc_dev_attr_is_visible, 118d29697dcSTakashi Iwai }; 119d29697dcSTakashi Iwai 120f7e73b26SMark Brown static const struct attribute_group soc_dev_group = { 121d29697dcSTakashi Iwai .attrs = soc_dev_attrs, 122d29697dcSTakashi Iwai .is_visible = soc_dev_attr_is_visible, 123d29697dcSTakashi Iwai }; 124d29697dcSTakashi Iwai 125d29697dcSTakashi Iwai static const struct attribute_group *soc_dev_attr_groups[] = { 126d29697dcSTakashi Iwai &soc_dapm_dev_group, 127f7e73b26SMark Brown &soc_dev_group, 128d29697dcSTakashi Iwai NULL 129d29697dcSTakashi Iwai }; 130d29697dcSTakashi Iwai 1312624d5faSMark Brown #ifdef CONFIG_DEBUG_FS 132a4072cdfSKuninori Morimoto struct dentry *snd_soc_debugfs_root; 133a4072cdfSKuninori Morimoto EXPORT_SYMBOL_GPL(snd_soc_debugfs_root); 134a4072cdfSKuninori Morimoto 13581c7cfd1SLars-Peter Clausen static void soc_init_component_debugfs(struct snd_soc_component *component) 136e73f3de5SRussell King { 1376553bf06SLars-Peter Clausen if (!component->card->debugfs_card_root) 1386553bf06SLars-Peter Clausen return; 1396553bf06SLars-Peter Clausen 14081c7cfd1SLars-Peter Clausen if (component->debugfs_prefix) { 14181c7cfd1SLars-Peter Clausen char *name; 142e73f3de5SRussell King 14381c7cfd1SLars-Peter Clausen name = kasprintf(GFP_KERNEL, "%s:%s", 14481c7cfd1SLars-Peter Clausen component->debugfs_prefix, component->name); 14581c7cfd1SLars-Peter Clausen if (name) { 14681c7cfd1SLars-Peter Clausen component->debugfs_root = debugfs_create_dir(name, 14781c7cfd1SLars-Peter Clausen component->card->debugfs_card_root); 14881c7cfd1SLars-Peter Clausen kfree(name); 14981c7cfd1SLars-Peter Clausen } 15081c7cfd1SLars-Peter Clausen } else { 15181c7cfd1SLars-Peter Clausen component->debugfs_root = debugfs_create_dir(component->name, 15281c7cfd1SLars-Peter Clausen component->card->debugfs_card_root); 153e73f3de5SRussell King } 154e73f3de5SRussell King 15581c7cfd1SLars-Peter Clausen snd_soc_dapm_debugfs_init(snd_soc_component_get_dapm(component), 15681c7cfd1SLars-Peter Clausen component->debugfs_root); 15781c7cfd1SLars-Peter Clausen } 15881c7cfd1SLars-Peter Clausen 15981c7cfd1SLars-Peter Clausen static void soc_cleanup_component_debugfs(struct snd_soc_component *component) 16081c7cfd1SLars-Peter Clausen { 161ad64bfbdSKuninori Morimoto if (!component->debugfs_root) 162ad64bfbdSKuninori Morimoto return; 16381c7cfd1SLars-Peter Clausen debugfs_remove_recursive(component->debugfs_root); 164ad64bfbdSKuninori Morimoto component->debugfs_root = NULL; 16581c7cfd1SLars-Peter Clausen } 16681c7cfd1SLars-Peter Clausen 167c15b2a1dSPeng Donglin static int dai_list_show(struct seq_file *m, void *v) 168f3208780SMark Brown { 1691438c2f6SLars-Peter Clausen struct snd_soc_component *component; 170f3208780SMark Brown struct snd_soc_dai *dai; 171f3208780SMark Brown 17234e81ab4SLars-Peter Clausen mutex_lock(&client_mutex); 17334e81ab4SLars-Peter Clausen 174368dee94SKuninori Morimoto for_each_component(component) 17515a0c645SKuninori Morimoto for_each_component_dais(component, dai) 176700c17caSDonglin Peng seq_printf(m, "%s\n", dai->name); 177f3208780SMark Brown 17834e81ab4SLars-Peter Clausen mutex_unlock(&client_mutex); 17934e81ab4SLars-Peter Clausen 180700c17caSDonglin Peng return 0; 181700c17caSDonglin Peng } 182c15b2a1dSPeng Donglin DEFINE_SHOW_ATTRIBUTE(dai_list); 183f3208780SMark Brown 184db795f9bSKuninori Morimoto static int component_list_show(struct seq_file *m, void *v) 185db795f9bSKuninori Morimoto { 186db795f9bSKuninori Morimoto struct snd_soc_component *component; 187db795f9bSKuninori Morimoto 188db795f9bSKuninori Morimoto mutex_lock(&client_mutex); 189db795f9bSKuninori Morimoto 190368dee94SKuninori Morimoto for_each_component(component) 191db795f9bSKuninori Morimoto seq_printf(m, "%s\n", component->name); 192db795f9bSKuninori Morimoto 193db795f9bSKuninori Morimoto mutex_unlock(&client_mutex); 194db795f9bSKuninori Morimoto 195db795f9bSKuninori Morimoto return 0; 196db795f9bSKuninori Morimoto } 197db795f9bSKuninori Morimoto DEFINE_SHOW_ATTRIBUTE(component_list); 198db795f9bSKuninori Morimoto 199a6052154SJarkko Nikula static void soc_init_card_debugfs(struct snd_soc_card *card) 200a6052154SJarkko Nikula { 201a6052154SJarkko Nikula card->debugfs_card_root = debugfs_create_dir(card->name, 2028a9dab1aSMark Brown snd_soc_debugfs_root); 2033a45b867SJarkko Nikula 204fee531d6SGreg Kroah-Hartman debugfs_create_u32("dapm_pop_time", 0644, card->debugfs_card_root, 2053a45b867SJarkko Nikula &card->pop_time); 206d8ca7a0aSKuninori Morimoto 207d8ca7a0aSKuninori Morimoto snd_soc_dapm_debugfs_init(&card->dapm, card->debugfs_card_root); 208a6052154SJarkko Nikula } 209a6052154SJarkko Nikula 210a6052154SJarkko Nikula static void soc_cleanup_card_debugfs(struct snd_soc_card *card) 211a6052154SJarkko Nikula { 212a6052154SJarkko Nikula debugfs_remove_recursive(card->debugfs_card_root); 21329040d1aSKuninori Morimoto card->debugfs_card_root = NULL; 214a6052154SJarkko Nikula } 215a6052154SJarkko Nikula 2166553bf06SLars-Peter Clausen static void snd_soc_debugfs_init(void) 2176553bf06SLars-Peter Clausen { 2186553bf06SLars-Peter Clausen snd_soc_debugfs_root = debugfs_create_dir("asoc", NULL); 2196553bf06SLars-Peter Clausen 220fee531d6SGreg Kroah-Hartman debugfs_create_file("dais", 0444, snd_soc_debugfs_root, NULL, 221fee531d6SGreg Kroah-Hartman &dai_list_fops); 222db795f9bSKuninori Morimoto 223fee531d6SGreg Kroah-Hartman debugfs_create_file("components", 0444, snd_soc_debugfs_root, NULL, 224fee531d6SGreg Kroah-Hartman &component_list_fops); 2256553bf06SLars-Peter Clausen } 2266553bf06SLars-Peter Clausen 2276553bf06SLars-Peter Clausen static void snd_soc_debugfs_exit(void) 2286553bf06SLars-Peter Clausen { 2296553bf06SLars-Peter Clausen debugfs_remove_recursive(snd_soc_debugfs_root); 2306553bf06SLars-Peter Clausen } 2316553bf06SLars-Peter Clausen 2322624d5faSMark Brown #else 2332624d5faSMark Brown 23481c7cfd1SLars-Peter Clausen static inline void soc_init_component_debugfs( 23581c7cfd1SLars-Peter Clausen struct snd_soc_component *component) 2362624d5faSMark Brown { 2372624d5faSMark Brown } 2382624d5faSMark Brown 23981c7cfd1SLars-Peter Clausen static inline void soc_cleanup_component_debugfs( 24081c7cfd1SLars-Peter Clausen struct snd_soc_component *component) 241731f1ab2SSebastien Guiriec { 242731f1ab2SSebastien Guiriec } 243731f1ab2SSebastien Guiriec 244b95fccbcSAxel Lin static inline void soc_init_card_debugfs(struct snd_soc_card *card) 245b95fccbcSAxel Lin { 246b95fccbcSAxel Lin } 247b95fccbcSAxel Lin 248b95fccbcSAxel Lin static inline void soc_cleanup_card_debugfs(struct snd_soc_card *card) 249b95fccbcSAxel Lin { 250b95fccbcSAxel Lin } 2516553bf06SLars-Peter Clausen 2526553bf06SLars-Peter Clausen static inline void snd_soc_debugfs_init(void) 2536553bf06SLars-Peter Clausen { 2546553bf06SLars-Peter Clausen } 2556553bf06SLars-Peter Clausen 2566553bf06SLars-Peter Clausen static inline void snd_soc_debugfs_exit(void) 2576553bf06SLars-Peter Clausen { 2586553bf06SLars-Peter Clausen } 2596553bf06SLars-Peter Clausen 2602624d5faSMark Brown #endif 2612624d5faSMark Brown 26212b05232SKuninori Morimoto static int snd_soc_rtd_add_component(struct snd_soc_pcm_runtime *rtd, 263a0ac4411SKuninori Morimoto struct snd_soc_component *component) 264a0ac4411SKuninori Morimoto { 2652b544dd7SKuninori Morimoto struct snd_soc_component *comp; 266613fb500SKuninori Morimoto int i; 267a0ac4411SKuninori Morimoto 268613fb500SKuninori Morimoto for_each_rtd_components(rtd, i, comp) { 269a0ac4411SKuninori Morimoto /* already connected */ 2702b544dd7SKuninori Morimoto if (comp == component) 271a0ac4411SKuninori Morimoto return 0; 272a0ac4411SKuninori Morimoto } 273a0ac4411SKuninori Morimoto 274613fb500SKuninori Morimoto /* see for_each_rtd_components */ 275613fb500SKuninori Morimoto rtd->components[rtd->num_components] = component; 276613fb500SKuninori Morimoto rtd->num_components++; 277a0ac4411SKuninori Morimoto 278a0ac4411SKuninori Morimoto return 0; 279a0ac4411SKuninori Morimoto } 280a0ac4411SKuninori Morimoto 281a0ac4411SKuninori Morimoto struct snd_soc_component *snd_soc_rtdcom_lookup(struct snd_soc_pcm_runtime *rtd, 282a0ac4411SKuninori Morimoto const char *driver_name) 283a0ac4411SKuninori Morimoto { 2842b544dd7SKuninori Morimoto struct snd_soc_component *component; 285613fb500SKuninori Morimoto int i; 286a0ac4411SKuninori Morimoto 287971da24cSKuninori Morimoto if (!driver_name) 288971da24cSKuninori Morimoto return NULL; 289971da24cSKuninori Morimoto 290a33c0d16SKuninori Morimoto /* 291a33c0d16SKuninori Morimoto * NOTE 292a33c0d16SKuninori Morimoto * 293a33c0d16SKuninori Morimoto * snd_soc_rtdcom_lookup() will find component from rtd by using 294a33c0d16SKuninori Morimoto * specified driver name. 295a33c0d16SKuninori Morimoto * But, if many components which have same driver name are connected 296a33c0d16SKuninori Morimoto * to 1 rtd, this function will return 1st found component. 297a33c0d16SKuninori Morimoto */ 298613fb500SKuninori Morimoto for_each_rtd_components(rtd, i, component) { 2992b544dd7SKuninori Morimoto const char *component_name = component->driver->name; 300971da24cSKuninori Morimoto 301971da24cSKuninori Morimoto if (!component_name) 302971da24cSKuninori Morimoto continue; 303971da24cSKuninori Morimoto 304971da24cSKuninori Morimoto if ((component_name == driver_name) || 305971da24cSKuninori Morimoto strcmp(component_name, driver_name) == 0) 306613fb500SKuninori Morimoto return component; 307a0ac4411SKuninori Morimoto } 308a0ac4411SKuninori Morimoto 309a0ac4411SKuninori Morimoto return NULL; 310a0ac4411SKuninori Morimoto } 311031734b7SKuninori Morimoto EXPORT_SYMBOL_GPL(snd_soc_rtdcom_lookup); 312a0ac4411SKuninori Morimoto 3136fbea6b6SShengjiu Wang struct snd_soc_component 31418dd66eaSKuninori Morimoto *snd_soc_lookup_component_nolocked(struct device *dev, const char *driver_name) 315b8132657SKuninori Morimoto { 316b8132657SKuninori Morimoto struct snd_soc_component *component; 3175bd7e08bSKuninori Morimoto struct snd_soc_component *found_component; 318b8132657SKuninori Morimoto 3195bd7e08bSKuninori Morimoto found_component = NULL; 320b8132657SKuninori Morimoto for_each_component(component) { 3215bd7e08bSKuninori Morimoto if ((dev == component->dev) && 3225bd7e08bSKuninori Morimoto (!driver_name || 3235bd7e08bSKuninori Morimoto (driver_name == component->driver->name) || 3245bd7e08bSKuninori Morimoto (strcmp(component->driver->name, driver_name) == 0))) { 3255bd7e08bSKuninori Morimoto found_component = component; 326b8132657SKuninori Morimoto break; 327b8132657SKuninori Morimoto } 3285bd7e08bSKuninori Morimoto } 329b8132657SKuninori Morimoto 3305bd7e08bSKuninori Morimoto return found_component; 331b8132657SKuninori Morimoto } 3326fbea6b6SShengjiu Wang EXPORT_SYMBOL_GPL(snd_soc_lookup_component_nolocked); 33318dd66eaSKuninori Morimoto 33418dd66eaSKuninori Morimoto struct snd_soc_component *snd_soc_lookup_component(struct device *dev, 33518dd66eaSKuninori Morimoto const char *driver_name) 33618dd66eaSKuninori Morimoto { 33718dd66eaSKuninori Morimoto struct snd_soc_component *component; 33818dd66eaSKuninori Morimoto 33918dd66eaSKuninori Morimoto mutex_lock(&client_mutex); 34018dd66eaSKuninori Morimoto component = snd_soc_lookup_component_nolocked(dev, driver_name); 34118dd66eaSKuninori Morimoto mutex_unlock(&client_mutex); 34218dd66eaSKuninori Morimoto 34318dd66eaSKuninori Morimoto return component; 34418dd66eaSKuninori Morimoto } 345b8132657SKuninori Morimoto EXPORT_SYMBOL_GPL(snd_soc_lookup_component); 346b8132657SKuninori Morimoto 34794def8eaSKuninori Morimoto struct snd_soc_pcm_runtime 34894def8eaSKuninori Morimoto *snd_soc_get_pcm_runtime(struct snd_soc_card *card, 3494468189fSKuninori Morimoto struct snd_soc_dai_link *dai_link) 35094def8eaSKuninori Morimoto { 35194def8eaSKuninori Morimoto struct snd_soc_pcm_runtime *rtd; 35294def8eaSKuninori Morimoto 35394def8eaSKuninori Morimoto for_each_card_rtds(card, rtd) { 3544468189fSKuninori Morimoto if (rtd->dai_link == dai_link) 35594def8eaSKuninori Morimoto return rtd; 35694def8eaSKuninori Morimoto } 3574468189fSKuninori Morimoto dev_dbg(card->dev, "ASoC: failed to find rtd %s\n", dai_link->name); 35894def8eaSKuninori Morimoto return NULL; 35994def8eaSKuninori Morimoto } 36094def8eaSKuninori Morimoto EXPORT_SYMBOL_GPL(snd_soc_get_pcm_runtime); 36194def8eaSKuninori Morimoto 36283f94a2eSKuninori Morimoto /* 36383f94a2eSKuninori Morimoto * Power down the audio subsystem pmdown_time msecs after close is called. 36483f94a2eSKuninori Morimoto * This is to ensure there are no pops or clicks in between any music tracks 36583f94a2eSKuninori Morimoto * due to DAPM power cycling. 36683f94a2eSKuninori Morimoto */ 36783f94a2eSKuninori Morimoto void snd_soc_close_delayed_work(struct snd_soc_pcm_runtime *rtd) 36883f94a2eSKuninori Morimoto { 369c2233a26SKuninori Morimoto struct snd_soc_dai *codec_dai = asoc_rtd_to_codec(rtd, 0); 3700f6011fdSKuninori Morimoto int playback = SNDRV_PCM_STREAM_PLAYBACK; 37183f94a2eSKuninori Morimoto 37283f94a2eSKuninori Morimoto mutex_lock_nested(&rtd->card->pcm_mutex, rtd->card->pcm_subclass); 37383f94a2eSKuninori Morimoto 37483f94a2eSKuninori Morimoto dev_dbg(rtd->dev, 37583f94a2eSKuninori Morimoto "ASoC: pop wq checking: %s status: %s waiting: %s\n", 37683f94a2eSKuninori Morimoto codec_dai->driver->playback.stream_name, 377b3dea624SKuninori Morimoto snd_soc_dai_stream_active(codec_dai, playback) ? 378b3dea624SKuninori Morimoto "active" : "inactive", 37983f94a2eSKuninori Morimoto rtd->pop_wait ? "yes" : "no"); 38083f94a2eSKuninori Morimoto 38183f94a2eSKuninori Morimoto /* are we waiting on this codec DAI stream */ 38283f94a2eSKuninori Morimoto if (rtd->pop_wait == 1) { 38383f94a2eSKuninori Morimoto rtd->pop_wait = 0; 3840f6011fdSKuninori Morimoto snd_soc_dapm_stream_event(rtd, playback, 38583f94a2eSKuninori Morimoto SND_SOC_DAPM_STREAM_STOP); 38683f94a2eSKuninori Morimoto } 38783f94a2eSKuninori Morimoto 38883f94a2eSKuninori Morimoto mutex_unlock(&rtd->card->pcm_mutex); 38983f94a2eSKuninori Morimoto } 39083f94a2eSKuninori Morimoto EXPORT_SYMBOL_GPL(snd_soc_close_delayed_work); 39183f94a2eSKuninori Morimoto 3926e864344SKuninori Morimoto static void soc_release_rtd_dev(struct device *dev) 3936e864344SKuninori Morimoto { 3946e864344SKuninori Morimoto /* "dev" means "rtd->dev" */ 3956e864344SKuninori Morimoto kfree(dev); 3966e864344SKuninori Morimoto } 3976e864344SKuninori Morimoto 3981c93a9e0SKuninori Morimoto static void soc_free_pcm_runtime(struct snd_soc_pcm_runtime *rtd) 3991c93a9e0SKuninori Morimoto { 4006e864344SKuninori Morimoto if (!rtd) 4016e864344SKuninori Morimoto return; 4026e864344SKuninori Morimoto 403753ace0aSKuninori Morimoto list_del(&rtd->list); 404b7c5bc45SKuninori Morimoto 4059c9b6520SCurtis Malainey if (delayed_work_pending(&rtd->delayed_work)) 4060ced7b05SKuninori Morimoto flush_delayed_work(&rtd->delayed_work); 4070ced7b05SKuninori Morimoto snd_soc_pcm_component_free(rtd); 4080ced7b05SKuninori Morimoto 409b7c5bc45SKuninori Morimoto /* 410b7c5bc45SKuninori Morimoto * we don't need to call kfree() for rtd->dev 411b7c5bc45SKuninori Morimoto * see 412b7c5bc45SKuninori Morimoto * soc_release_rtd_dev() 413d918a376SKuninori Morimoto * 414d918a376SKuninori Morimoto * We don't need rtd->dev NULL check, because 415d918a376SKuninori Morimoto * it is alloced *before* rtd. 416d918a376SKuninori Morimoto * see 417d918a376SKuninori Morimoto * soc_new_pcm_runtime() 418b7c5bc45SKuninori Morimoto */ 419b7c5bc45SKuninori Morimoto device_unregister(rtd->dev); 4201c93a9e0SKuninori Morimoto } 4211c93a9e0SKuninori Morimoto 4224bf2e385SCurtis Malainey static void close_delayed_work(struct work_struct *work) { 4234bf2e385SCurtis Malainey struct snd_soc_pcm_runtime *rtd = 4244bf2e385SCurtis Malainey container_of(work, struct snd_soc_pcm_runtime, 4254bf2e385SCurtis Malainey delayed_work.work); 4264bf2e385SCurtis Malainey 4274bf2e385SCurtis Malainey if (rtd->close_delayed_work_func) 4284bf2e385SCurtis Malainey rtd->close_delayed_work_func(rtd); 4294bf2e385SCurtis Malainey } 4304bf2e385SCurtis Malainey 4311a497983SMengdong Lin static struct snd_soc_pcm_runtime *soc_new_pcm_runtime( 4321a497983SMengdong Lin struct snd_soc_card *card, struct snd_soc_dai_link *dai_link) 4331a497983SMengdong Lin { 4341a497983SMengdong Lin struct snd_soc_pcm_runtime *rtd; 435613fb500SKuninori Morimoto struct snd_soc_component *component; 436d918a376SKuninori Morimoto struct device *dev; 4376e864344SKuninori Morimoto int ret; 438d74c2a15SKuninori Morimoto int stream; 4391a497983SMengdong Lin 4406e864344SKuninori Morimoto /* 441d918a376SKuninori Morimoto * for rtd->dev 442d918a376SKuninori Morimoto */ 443d918a376SKuninori Morimoto dev = kzalloc(sizeof(struct device), GFP_KERNEL); 444d918a376SKuninori Morimoto if (!dev) 445d918a376SKuninori Morimoto return NULL; 446d918a376SKuninori Morimoto 447d918a376SKuninori Morimoto dev->parent = card->dev; 448d918a376SKuninori Morimoto dev->release = soc_release_rtd_dev; 449d918a376SKuninori Morimoto 450d918a376SKuninori Morimoto dev_set_name(dev, "%s", dai_link->name); 451d918a376SKuninori Morimoto 452d918a376SKuninori Morimoto ret = device_register(dev); 453d918a376SKuninori Morimoto if (ret < 0) { 454d918a376SKuninori Morimoto put_device(dev); /* soc_release_rtd_dev */ 455d918a376SKuninori Morimoto return NULL; 456d918a376SKuninori Morimoto } 457d918a376SKuninori Morimoto 458d918a376SKuninori Morimoto /* 4596e864344SKuninori Morimoto * for rtd 4606e864344SKuninori Morimoto */ 461613fb500SKuninori Morimoto rtd = devm_kzalloc(dev, 462613fb500SKuninori Morimoto sizeof(*rtd) + 463613fb500SKuninori Morimoto sizeof(*component) * (dai_link->num_cpus + 464613fb500SKuninori Morimoto dai_link->num_codecs + 465613fb500SKuninori Morimoto dai_link->num_platforms), 466613fb500SKuninori Morimoto GFP_KERNEL); 4671a497983SMengdong Lin if (!rtd) 4686e864344SKuninori Morimoto goto free_rtd; 4691a497983SMengdong Lin 470d918a376SKuninori Morimoto rtd->dev = dev; 47107d22a9bSTakashi Iwai INIT_LIST_HEAD(&rtd->list); 472d74c2a15SKuninori Morimoto for_each_pcm_streams(stream) { 473d74c2a15SKuninori Morimoto INIT_LIST_HEAD(&rtd->dpcm[stream].be_clients); 474d74c2a15SKuninori Morimoto INIT_LIST_HEAD(&rtd->dpcm[stream].fe_clients); 475d74c2a15SKuninori Morimoto } 476d918a376SKuninori Morimoto dev_set_drvdata(dev, rtd); 4774bf2e385SCurtis Malainey INIT_DELAYED_WORK(&rtd->delayed_work, close_delayed_work); 478d918a376SKuninori Morimoto 4796e864344SKuninori Morimoto /* 48022a2fc81SKuninori Morimoto * for rtd->dais 4816e864344SKuninori Morimoto */ 48222a2fc81SKuninori Morimoto rtd->dais = devm_kcalloc(dev, dai_link->num_cpus + dai_link->num_codecs, 4836396bb22SKees Cook sizeof(struct snd_soc_dai *), 4841a497983SMengdong Lin GFP_KERNEL); 48522a2fc81SKuninori Morimoto if (!rtd->dais) 4866e864344SKuninori Morimoto goto free_rtd; 4876e864344SKuninori Morimoto 4886e864344SKuninori Morimoto /* 48922a2fc81SKuninori Morimoto * dais = [][][][][][][][][][][][][][][][][][] 49022a2fc81SKuninori Morimoto * ^cpu_dais ^codec_dais 49122a2fc81SKuninori Morimoto * |--- num_cpus ---|--- num_codecs --| 4921729025bSKuninori Morimoto * see 4931729025bSKuninori Morimoto * asoc_rtd_to_cpu() 4941729025bSKuninori Morimoto * asoc_rtd_to_codec() 49576afa643SShreyas NC */ 49649648d02SKuninori Morimoto rtd->num_cpus = dai_link->num_cpus; 49749648d02SKuninori Morimoto rtd->num_codecs = dai_link->num_codecs; 498929deb84SKuninori Morimoto rtd->card = card; 499929deb84SKuninori Morimoto rtd->dai_link = dai_link; 50001faf501SKuninori Morimoto rtd->num = card->num_rtd++; 501929deb84SKuninori Morimoto 5026634e3d6SKuninori Morimoto /* see for_each_card_rtds */ 5031a497983SMengdong Lin list_add_tail(&rtd->list, &card->rtd_list); 504a848125eSKuninori Morimoto 505*5c74c9d3SCharles Keepax ret = device_add_groups(dev, soc_dev_attr_groups); 506*5c74c9d3SCharles Keepax if (ret < 0) 507*5c74c9d3SCharles Keepax goto free_rtd; 508*5c74c9d3SCharles Keepax 509a848125eSKuninori Morimoto return rtd; 5106e864344SKuninori Morimoto 5116e864344SKuninori Morimoto free_rtd: 5126e864344SKuninori Morimoto soc_free_pcm_runtime(rtd); 5136e864344SKuninori Morimoto return NULL; 5141a497983SMengdong Lin } 5151a497983SMengdong Lin 51665462e44SKuninori Morimoto static void snd_soc_flush_all_delayed_work(struct snd_soc_card *card) 51765462e44SKuninori Morimoto { 51865462e44SKuninori Morimoto struct snd_soc_pcm_runtime *rtd; 51965462e44SKuninori Morimoto 52065462e44SKuninori Morimoto for_each_card_rtds(card, rtd) 52165462e44SKuninori Morimoto flush_delayed_work(&rtd->delayed_work); 52265462e44SKuninori Morimoto } 52365462e44SKuninori Morimoto 5246f8ab4acSMark Brown #ifdef CONFIG_PM_SLEEP 525db2a4165SFrank Mandarino /* powers down audio subsystem for suspend */ 5266f8ab4acSMark Brown int snd_soc_suspend(struct device *dev) 527db2a4165SFrank Mandarino { 5286f8ab4acSMark Brown struct snd_soc_card *card = dev_get_drvdata(dev); 529d9fc4063SKuninori Morimoto struct snd_soc_component *component; 5301a497983SMengdong Lin struct snd_soc_pcm_runtime *rtd; 5310f6011fdSKuninori Morimoto int playback = SNDRV_PCM_STREAM_PLAYBACK; 5321a497983SMengdong Lin int i; 533db2a4165SFrank Mandarino 534c5599b87SLars-Peter Clausen /* If the card is not initialized yet there is nothing to do */ 535c5599b87SLars-Peter Clausen if (!card->instantiated) 536e3509ff0SDaniel Mack return 0; 537e3509ff0SDaniel Mack 5382c7b696aSMarcel Ziswiler /* 5392c7b696aSMarcel Ziswiler * Due to the resume being scheduled into a workqueue we could 5406ed25978SAndy Green * suspend before that's finished - wait for it to complete. 5416ed25978SAndy Green */ 542f0fba2adSLiam Girdwood snd_power_wait(card->snd_card, SNDRV_CTL_POWER_D0); 5436ed25978SAndy Green 5446ed25978SAndy Green /* we're going to block userspace touching us until resume completes */ 545f0fba2adSLiam Girdwood snd_power_change_state(card->snd_card, SNDRV_CTL_POWER_D3hot); 5466ed25978SAndy Green 547a00f90f9SMark Brown /* mute any active DACs */ 548bcb1fd1fSKuninori Morimoto for_each_card_rtds(card, rtd) { 5490b7990e3SKuninori Morimoto struct snd_soc_dai *dai; 5503efab7dcSMark Brown 5511a497983SMengdong Lin if (rtd->dai_link->ignore_suspend) 5523efab7dcSMark Brown continue; 5533efab7dcSMark Brown 554a4be4187SKuninori Morimoto for_each_rtd_codec_dais(rtd, i, dai) { 555b3dea624SKuninori Morimoto if (snd_soc_dai_stream_active(dai, playback)) 5560f6011fdSKuninori Morimoto snd_soc_dai_digital_mute(dai, 1, playback); 557db2a4165SFrank Mandarino } 55888bd870fSBenoit Cousson } 559db2a4165SFrank Mandarino 5604ccab3e7SLiam Girdwood /* suspend all pcms */ 561bcb1fd1fSKuninori Morimoto for_each_card_rtds(card, rtd) { 5621a497983SMengdong Lin if (rtd->dai_link->ignore_suspend) 5633efab7dcSMark Brown continue; 5643efab7dcSMark Brown 5651a497983SMengdong Lin snd_pcm_suspend_all(rtd->pcm); 5663efab7dcSMark Brown } 5674ccab3e7SLiam Girdwood 568130dc08cSKuninori Morimoto snd_soc_card_suspend_pre(card); 569db2a4165SFrank Mandarino 57037660b6dSLars-Peter Clausen /* close any waiting streams */ 57165462e44SKuninori Morimoto snd_soc_flush_all_delayed_work(card); 572db2a4165SFrank Mandarino 573bcb1fd1fSKuninori Morimoto for_each_card_rtds(card, rtd) { 574d74c2a15SKuninori Morimoto int stream; 5753efab7dcSMark Brown 5761a497983SMengdong Lin if (rtd->dai_link->ignore_suspend) 5773efab7dcSMark Brown continue; 5783efab7dcSMark Brown 579d74c2a15SKuninori Morimoto for_each_pcm_streams(stream) 580d74c2a15SKuninori Morimoto snd_soc_dapm_stream_event(rtd, stream, 581db2a4165SFrank Mandarino SND_SOC_DAPM_STREAM_SUSPEND); 582db2a4165SFrank Mandarino } 583db2a4165SFrank Mandarino 5848be4da29SLars-Peter Clausen /* Recheck all endpoints too, their state is affected by suspend */ 5858be4da29SLars-Peter Clausen dapm_mark_endpoints_dirty(card); 586e2d32ff6SMark Brown snd_soc_dapm_sync(&card->dapm); 587e2d32ff6SMark Brown 5889178feb4SKuninori Morimoto /* suspend all COMPONENTs */ 5891272063aSKuninori Morimoto for_each_card_rtds(card, rtd) { 5901272063aSKuninori Morimoto 5911272063aSKuninori Morimoto if (rtd->dai_link->ignore_suspend) 5921272063aSKuninori Morimoto continue; 5931272063aSKuninori Morimoto 5941272063aSKuninori Morimoto for_each_rtd_components(rtd, i, component) { 5952c7b696aSMarcel Ziswiler struct snd_soc_dapm_context *dapm = 5962c7b696aSMarcel Ziswiler snd_soc_component_get_dapm(component); 597d9fc4063SKuninori Morimoto 5982c7b696aSMarcel Ziswiler /* 5991272063aSKuninori Morimoto * ignore if component was already suspended 6002c7b696aSMarcel Ziswiler */ 6011272063aSKuninori Morimoto if (snd_soc_component_is_suspended(component)) 6021272063aSKuninori Morimoto continue; 6031272063aSKuninori Morimoto 6041272063aSKuninori Morimoto /* 6051272063aSKuninori Morimoto * If there are paths active then the COMPONENT will be 6061272063aSKuninori Morimoto * held with bias _ON and should not be suspended. 6071272063aSKuninori Morimoto */ 6084890140fSLars-Peter Clausen switch (snd_soc_dapm_get_bias_level(dapm)) { 6091547aba9SMark Brown case SND_SOC_BIAS_STANDBY: 610125a25daSMark Brown /* 6119178feb4SKuninori Morimoto * If the COMPONENT is capable of idle 612125a25daSMark Brown * bias off then being in STANDBY 613125a25daSMark Brown * means it's doing something, 614125a25daSMark Brown * otherwise fall through. 615125a25daSMark Brown */ 6164890140fSLars-Peter Clausen if (dapm->idle_bias_off) { 6179178feb4SKuninori Morimoto dev_dbg(component->dev, 61810e8aa9aSMichał Mirosław "ASoC: idle_bias_off CODEC on over suspend\n"); 619125a25daSMark Brown break; 620125a25daSMark Brown } 6211a12d5dcSGustavo A. R. Silva /* fall through */ 622a8093297SLars-Peter Clausen 6231547aba9SMark Brown case SND_SOC_BIAS_OFF: 62466c51573SKuninori Morimoto snd_soc_component_suspend(component); 6259178feb4SKuninori Morimoto if (component->regmap) 6269178feb4SKuninori Morimoto regcache_mark_dirty(component->regmap); 627988e8cc4SNicolin Chen /* deactivate pins to sleep state */ 6289178feb4SKuninori Morimoto pinctrl_pm_select_sleep_state(component->dev); 6291547aba9SMark Brown break; 6301547aba9SMark Brown default: 6319178feb4SKuninori Morimoto dev_dbg(component->dev, 6329178feb4SKuninori Morimoto "ASoC: COMPONENT is on over suspend\n"); 6331547aba9SMark Brown break; 6341547aba9SMark Brown } 6351547aba9SMark Brown } 636f0fba2adSLiam Girdwood } 637db2a4165SFrank Mandarino 638d17b60b2SKuninori Morimoto snd_soc_card_suspend_post(card); 639db2a4165SFrank Mandarino 640db2a4165SFrank Mandarino return 0; 641db2a4165SFrank Mandarino } 6426f8ab4acSMark Brown EXPORT_SYMBOL_GPL(snd_soc_suspend); 643db2a4165SFrank Mandarino 6442c7b696aSMarcel Ziswiler /* 6452c7b696aSMarcel Ziswiler * deferred resume work, so resume can complete before we finished 6466ed25978SAndy Green * setting our codec back up, which can be very slow on I2C 6476ed25978SAndy Green */ 6486ed25978SAndy Green static void soc_resume_deferred(struct work_struct *work) 649db2a4165SFrank Mandarino { 650f0fba2adSLiam Girdwood struct snd_soc_card *card = 6512c7b696aSMarcel Ziswiler container_of(work, struct snd_soc_card, 6522c7b696aSMarcel Ziswiler deferred_resume_work); 6531a497983SMengdong Lin struct snd_soc_pcm_runtime *rtd; 654d9fc4063SKuninori Morimoto struct snd_soc_component *component; 6551a497983SMengdong Lin int i; 656db2a4165SFrank Mandarino 6572c7b696aSMarcel Ziswiler /* 6582c7b696aSMarcel Ziswiler * our power state is still SNDRV_CTL_POWER_D3hot from suspend time, 6596ed25978SAndy Green * so userspace apps are blocked from touching us 6606ed25978SAndy Green */ 6616ed25978SAndy Green 662f110bfc7SLiam Girdwood dev_dbg(card->dev, "ASoC: starting resume work\n"); 6636ed25978SAndy Green 6649949788bSMark Brown /* Bring us up into D2 so that DAPM starts enabling things */ 665f0fba2adSLiam Girdwood snd_power_change_state(card->snd_card, SNDRV_CTL_POWER_D2); 6669949788bSMark Brown 667934c752cSKuninori Morimoto snd_soc_card_resume_pre(card); 668db2a4165SFrank Mandarino 669f70f18f7SKuninori Morimoto for_each_card_components(card, component) { 670e40fadbcSKuninori Morimoto if (snd_soc_component_is_suspended(component)) 6719a840cbaSKuninori Morimoto snd_soc_component_resume(component); 6721547aba9SMark Brown } 673db2a4165SFrank Mandarino 674bcb1fd1fSKuninori Morimoto for_each_card_rtds(card, rtd) { 675d74c2a15SKuninori Morimoto int stream; 6763efab7dcSMark Brown 6771a497983SMengdong Lin if (rtd->dai_link->ignore_suspend) 6783efab7dcSMark Brown continue; 6793efab7dcSMark Brown 680d74c2a15SKuninori Morimoto for_each_pcm_streams(stream) 681d74c2a15SKuninori Morimoto snd_soc_dapm_stream_event(rtd, stream, 682db2a4165SFrank Mandarino SND_SOC_DAPM_STREAM_RESUME); 683db2a4165SFrank Mandarino } 684db2a4165SFrank Mandarino 6853ff3f64bSMark Brown /* unmute any active DACs */ 686bcb1fd1fSKuninori Morimoto for_each_card_rtds(card, rtd) { 6870b7990e3SKuninori Morimoto struct snd_soc_dai *dai; 6880f6011fdSKuninori Morimoto int playback = SNDRV_PCM_STREAM_PLAYBACK; 6893efab7dcSMark Brown 6901a497983SMengdong Lin if (rtd->dai_link->ignore_suspend) 6913efab7dcSMark Brown continue; 6923efab7dcSMark Brown 693a4be4187SKuninori Morimoto for_each_rtd_codec_dais(rtd, i, dai) { 694b3dea624SKuninori Morimoto if (snd_soc_dai_stream_active(dai, playback)) 6950f6011fdSKuninori Morimoto snd_soc_dai_digital_mute(dai, 0, playback); 696db2a4165SFrank Mandarino } 69788bd870fSBenoit Cousson } 698db2a4165SFrank Mandarino 699739443d1SKuninori Morimoto snd_soc_card_resume_post(card); 700db2a4165SFrank Mandarino 701f110bfc7SLiam Girdwood dev_dbg(card->dev, "ASoC: resume work completed\n"); 7026ed25978SAndy Green 7038be4da29SLars-Peter Clausen /* Recheck all endpoints too, their state is affected by suspend */ 7048be4da29SLars-Peter Clausen dapm_mark_endpoints_dirty(card); 705e2d32ff6SMark Brown snd_soc_dapm_sync(&card->dapm); 7061a7aaa58SJeeja KP 7071a7aaa58SJeeja KP /* userspace can access us now we are back as we were before */ 7081a7aaa58SJeeja KP snd_power_change_state(card->snd_card, SNDRV_CTL_POWER_D0); 7096ed25978SAndy Green } 7106ed25978SAndy Green 7116ed25978SAndy Green /* powers up audio subsystem after a suspend */ 7126f8ab4acSMark Brown int snd_soc_resume(struct device *dev) 7136ed25978SAndy Green { 7146f8ab4acSMark Brown struct snd_soc_card *card = dev_get_drvdata(dev); 71576c39e86SKuninori Morimoto struct snd_soc_component *component; 716b9dd94a8SPeter Ujfalusi 717c5599b87SLars-Peter Clausen /* If the card is not initialized yet there is nothing to do */ 718c5599b87SLars-Peter Clausen if (!card->instantiated) 7195ff1ddf2SEric Miao return 0; 7205ff1ddf2SEric Miao 721988e8cc4SNicolin Chen /* activate pins from sleep state */ 72276c39e86SKuninori Morimoto for_each_card_components(card, component) 723b3dea624SKuninori Morimoto if (snd_soc_component_active(component)) 72476c39e86SKuninori Morimoto pinctrl_pm_select_default_state(component->dev); 725988e8cc4SNicolin Chen 726f110bfc7SLiam Girdwood dev_dbg(dev, "ASoC: Scheduling resume work\n"); 7276308419aSMark Brown if (!schedule_work(&card->deferred_resume_work)) 728f110bfc7SLiam Girdwood dev_err(dev, "ASoC: resume work item may be lost\n"); 7296ed25978SAndy Green 730db2a4165SFrank Mandarino return 0; 731db2a4165SFrank Mandarino } 7326f8ab4acSMark Brown EXPORT_SYMBOL_GPL(snd_soc_resume); 733b3da4251SKuninori Morimoto 734b3da4251SKuninori Morimoto static void soc_resume_init(struct snd_soc_card *card) 735b3da4251SKuninori Morimoto { 736b3da4251SKuninori Morimoto /* deferred resume work */ 737b3da4251SKuninori Morimoto INIT_WORK(&card->deferred_resume_work, soc_resume_deferred); 738b3da4251SKuninori Morimoto } 739db2a4165SFrank Mandarino #else 7406f8ab4acSMark Brown #define snd_soc_suspend NULL 7416f8ab4acSMark Brown #define snd_soc_resume NULL 742b3da4251SKuninori Morimoto static inline void soc_resume_init(struct snd_soc_card *card) 743b3da4251SKuninori Morimoto { 744b3da4251SKuninori Morimoto } 745db2a4165SFrank Mandarino #endif 746db2a4165SFrank Mandarino 747c0834440SKuninori Morimoto static struct device_node 748c0834440SKuninori Morimoto *soc_component_to_node(struct snd_soc_component *component) 74912023a9aSMisael Lopez Cruz { 750c0834440SKuninori Morimoto struct device_node *of_node; 75112023a9aSMisael Lopez Cruz 752c0834440SKuninori Morimoto of_node = component->dev->of_node; 753c0834440SKuninori Morimoto if (!of_node && component->dev->parent) 754c0834440SKuninori Morimoto of_node = component->dev->parent->of_node; 75534e81ab4SLars-Peter Clausen 756c0834440SKuninori Morimoto return of_node; 75712023a9aSMisael Lopez Cruz } 75812023a9aSMisael Lopez Cruz 759be6ac0a9SKuninori Morimoto static int snd_soc_is_matching_component( 760be6ac0a9SKuninori Morimoto const struct snd_soc_dai_link_component *dlc, 761be6ac0a9SKuninori Morimoto struct snd_soc_component *component) 762be6ac0a9SKuninori Morimoto { 763be6ac0a9SKuninori Morimoto struct device_node *component_of_node; 764be6ac0a9SKuninori Morimoto 7657d7db5d3SKuninori Morimoto if (!dlc) 7667d7db5d3SKuninori Morimoto return 0; 7677d7db5d3SKuninori Morimoto 7687d7db5d3SKuninori Morimoto component_of_node = soc_component_to_node(component); 769be6ac0a9SKuninori Morimoto 770be6ac0a9SKuninori Morimoto if (dlc->of_node && component_of_node != dlc->of_node) 771be6ac0a9SKuninori Morimoto return 0; 772be6ac0a9SKuninori Morimoto if (dlc->name && strcmp(component->name, dlc->name)) 773be6ac0a9SKuninori Morimoto return 0; 774be6ac0a9SKuninori Morimoto 775be6ac0a9SKuninori Morimoto return 1; 776be6ac0a9SKuninori Morimoto } 777be6ac0a9SKuninori Morimoto 778db2a4165SFrank Mandarino static struct snd_soc_component *soc_find_component( 779c1e230f0SKuninori Morimoto const struct snd_soc_dai_link_component *dlc) 780db2a4165SFrank Mandarino { 781db2a4165SFrank Mandarino struct snd_soc_component *component; 782db2a4165SFrank Mandarino 783db2a4165SFrank Mandarino lockdep_assert_held(&client_mutex); 784db2a4165SFrank Mandarino 785e3303268SKuninori Morimoto /* 786e3303268SKuninori Morimoto * NOTE 787e3303268SKuninori Morimoto * 788e3303268SKuninori Morimoto * It returns *1st* found component, but some driver 789e3303268SKuninori Morimoto * has few components by same of_node/name 790e3303268SKuninori Morimoto * ex) 791e3303268SKuninori Morimoto * CPU component and generic DMAEngine component 792e3303268SKuninori Morimoto */ 793c1e230f0SKuninori Morimoto for_each_component(component) 794c1e230f0SKuninori Morimoto if (snd_soc_is_matching_component(dlc, component)) 795db2a4165SFrank Mandarino return component; 79602a06d30SBarry Song 79702a06d30SBarry Song return NULL; 79812023a9aSMisael Lopez Cruz } 79912023a9aSMisael Lopez Cruz 800fbb88b5cSMengdong Lin /** 801fbb88b5cSMengdong Lin * snd_soc_find_dai - Find a registered DAI 802fbb88b5cSMengdong Lin * 8034958471bSJeffy Chen * @dlc: name of the DAI or the DAI driver and optional component info to match 804fbb88b5cSMengdong Lin * 805ad61dd30SStephen Boyd * This function will search all registered components and their DAIs to 806fbb88b5cSMengdong Lin * find the DAI of the same name. The component's of_node and name 807fbb88b5cSMengdong Lin * should also match if being specified. 808fbb88b5cSMengdong Lin * 809fbb88b5cSMengdong Lin * Return: pointer of DAI, or NULL if not found. 810fbb88b5cSMengdong Lin */ 811305e9020SMengdong Lin struct snd_soc_dai *snd_soc_find_dai( 81214621c7eSLars-Peter Clausen const struct snd_soc_dai_link_component *dlc) 81312023a9aSMisael Lopez Cruz { 81414621c7eSLars-Peter Clausen struct snd_soc_component *component; 81514621c7eSLars-Peter Clausen struct snd_soc_dai *dai; 81612023a9aSMisael Lopez Cruz 81734e81ab4SLars-Peter Clausen lockdep_assert_held(&client_mutex); 81834e81ab4SLars-Peter Clausen 81914621c7eSLars-Peter Clausen /* Find CPU DAI from registered DAIs */ 820368dee94SKuninori Morimoto for_each_component(component) { 821be6ac0a9SKuninori Morimoto if (!snd_soc_is_matching_component(dlc, component)) 82212023a9aSMisael Lopez Cruz continue; 82315a0c645SKuninori Morimoto for_each_component_dais(component, dai) { 8244958471bSJeffy Chen if (dlc->dai_name && strcmp(dai->name, dlc->dai_name) 8256a6dafdaSJeffy Chen && (!dai->driver->name 8266a6dafdaSJeffy Chen || strcmp(dai->driver->name, dlc->dai_name))) 82714621c7eSLars-Peter Clausen continue; 82812023a9aSMisael Lopez Cruz 82914621c7eSLars-Peter Clausen return dai; 83012023a9aSMisael Lopez Cruz } 83112023a9aSMisael Lopez Cruz } 83212023a9aSMisael Lopez Cruz 83312023a9aSMisael Lopez Cruz return NULL; 83412023a9aSMisael Lopez Cruz } 835305e9020SMengdong Lin EXPORT_SYMBOL_GPL(snd_soc_find_dai); 83612023a9aSMisael Lopez Cruz 837bfce78a5SKuninori Morimoto static int soc_dai_link_sanity_check(struct snd_soc_card *card, 83836794902SKuninori Morimoto struct snd_soc_dai_link *link) 83936794902SKuninori Morimoto { 84036794902SKuninori Morimoto int i; 84176afa643SShreyas NC struct snd_soc_dai_link_component *cpu, *codec, *platform; 84236794902SKuninori Morimoto 84336794902SKuninori Morimoto for_each_link_codecs(link, i, codec) { 84436794902SKuninori Morimoto /* 84536794902SKuninori Morimoto * Codec must be specified by 1 of name or OF node, 84636794902SKuninori Morimoto * not both or neither. 84736794902SKuninori Morimoto */ 84836794902SKuninori Morimoto if (!!codec->name == !!codec->of_node) { 84936794902SKuninori Morimoto dev_err(card->dev, "ASoC: Neither/both codec name/of_node are set for %s\n", 85036794902SKuninori Morimoto link->name); 85136794902SKuninori Morimoto return -EINVAL; 85236794902SKuninori Morimoto } 85336794902SKuninori Morimoto 85436794902SKuninori Morimoto /* Codec DAI name must be specified */ 85536794902SKuninori Morimoto if (!codec->dai_name) { 85636794902SKuninori Morimoto dev_err(card->dev, "ASoC: codec_dai_name not set for %s\n", 85736794902SKuninori Morimoto link->name); 85836794902SKuninori Morimoto return -EINVAL; 85936794902SKuninori Morimoto } 86036794902SKuninori Morimoto 86136794902SKuninori Morimoto /* 86236794902SKuninori Morimoto * Defer card registration if codec component is not added to 86336794902SKuninori Morimoto * component list. 86436794902SKuninori Morimoto */ 865b2acc24cSRanjani Sridharan if (!soc_find_component(codec)) { 866b2acc24cSRanjani Sridharan dev_dbg(card->dev, 867b2acc24cSRanjani Sridharan "ASoC: codec component %s not found for link %s\n", 868b2acc24cSRanjani Sridharan codec->name, link->name); 86936794902SKuninori Morimoto return -EPROBE_DEFER; 87036794902SKuninori Morimoto } 871b2acc24cSRanjani Sridharan } 87236794902SKuninori Morimoto 87336794902SKuninori Morimoto for_each_link_platforms(link, i, platform) { 87436794902SKuninori Morimoto /* 87536794902SKuninori Morimoto * Platform may be specified by either name or OF node, but it 87636794902SKuninori Morimoto * can be left unspecified, then no components will be inserted 87736794902SKuninori Morimoto * in the rtdcom list 87836794902SKuninori Morimoto */ 87936794902SKuninori Morimoto if (!!platform->name == !!platform->of_node) { 88036794902SKuninori Morimoto dev_err(card->dev, 88136794902SKuninori Morimoto "ASoC: Neither/both platform name/of_node are set for %s\n", 88236794902SKuninori Morimoto link->name); 88336794902SKuninori Morimoto return -EINVAL; 88436794902SKuninori Morimoto } 88536794902SKuninori Morimoto 88636794902SKuninori Morimoto /* 88736794902SKuninori Morimoto * Defer card registration if platform component is not added to 88836794902SKuninori Morimoto * component list. 88936794902SKuninori Morimoto */ 890b2acc24cSRanjani Sridharan if (!soc_find_component(platform)) { 891b2acc24cSRanjani Sridharan dev_dbg(card->dev, 892b2acc24cSRanjani Sridharan "ASoC: platform component %s not found for link %s\n", 893b2acc24cSRanjani Sridharan platform->name, link->name); 89436794902SKuninori Morimoto return -EPROBE_DEFER; 89536794902SKuninori Morimoto } 896b2acc24cSRanjani Sridharan } 89736794902SKuninori Morimoto 89876afa643SShreyas NC for_each_link_cpus(link, i, cpu) { 89936794902SKuninori Morimoto /* 90036794902SKuninori Morimoto * CPU device may be specified by either name or OF node, but 90136794902SKuninori Morimoto * can be left unspecified, and will be matched based on DAI 90236794902SKuninori Morimoto * name alone.. 90336794902SKuninori Morimoto */ 90476afa643SShreyas NC if (cpu->name && cpu->of_node) { 90536794902SKuninori Morimoto dev_err(card->dev, 90636794902SKuninori Morimoto "ASoC: Neither/both cpu name/of_node are set for %s\n", 90736794902SKuninori Morimoto link->name); 90836794902SKuninori Morimoto return -EINVAL; 90936794902SKuninori Morimoto } 91036794902SKuninori Morimoto 91136794902SKuninori Morimoto /* 912cd3c5ad7SKuninori Morimoto * Defer card registration if cpu dai component is not added to 91336794902SKuninori Morimoto * component list. 91436794902SKuninori Morimoto */ 91576afa643SShreyas NC if ((cpu->of_node || cpu->name) && 916b2acc24cSRanjani Sridharan !soc_find_component(cpu)) { 917b2acc24cSRanjani Sridharan dev_dbg(card->dev, 918b2acc24cSRanjani Sridharan "ASoC: cpu component %s not found for link %s\n", 919b2acc24cSRanjani Sridharan cpu->name, link->name); 92036794902SKuninori Morimoto return -EPROBE_DEFER; 921b2acc24cSRanjani Sridharan } 92236794902SKuninori Morimoto 92336794902SKuninori Morimoto /* 92436794902SKuninori Morimoto * At least one of CPU DAI name or CPU device name/node must be 92536794902SKuninori Morimoto * specified 92636794902SKuninori Morimoto */ 92776afa643SShreyas NC if (!cpu->dai_name && 92876afa643SShreyas NC !(cpu->name || cpu->of_node)) { 92936794902SKuninori Morimoto dev_err(card->dev, 93036794902SKuninori Morimoto "ASoC: Neither cpu_dai_name nor cpu_name/of_node are set for %s\n", 93136794902SKuninori Morimoto link->name); 93236794902SKuninori Morimoto return -EINVAL; 93336794902SKuninori Morimoto } 93476afa643SShreyas NC } 93536794902SKuninori Morimoto 93636794902SKuninori Morimoto return 0; 93736794902SKuninori Morimoto } 93836794902SKuninori Morimoto 939da704f26SKuninori Morimoto /** 94050cd9b53SKuninori Morimoto * snd_soc_remove_pcm_runtime - Remove a pcm_runtime from card 94150cd9b53SKuninori Morimoto * @card: The ASoC card to which the pcm_runtime has 94250cd9b53SKuninori Morimoto * @rtd: The pcm_runtime to remove 943da704f26SKuninori Morimoto * 94450cd9b53SKuninori Morimoto * This function removes a pcm_runtime from the ASoC card. 945da704f26SKuninori Morimoto */ 94650cd9b53SKuninori Morimoto void snd_soc_remove_pcm_runtime(struct snd_soc_card *card, 94750cd9b53SKuninori Morimoto struct snd_soc_pcm_runtime *rtd) 948bc7a9091SKuninori Morimoto { 949da704f26SKuninori Morimoto lockdep_assert_held(&client_mutex); 950da704f26SKuninori Morimoto 951da704f26SKuninori Morimoto /* 952da704f26SKuninori Morimoto * Notify the machine driver for extra destruction 953da704f26SKuninori Morimoto */ 954fcbbcc32SKuninori Morimoto snd_soc_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 */ 984cbc7a6b5SKuninori Morimoto ret = snd_soc_card_add_dai_link(card, dai_link); 985cbc7a6b5SKuninori Morimoto if (ret < 0) 986cbc7a6b5SKuninori Morimoto return ret; 98763dc47daSKuninori Morimoto 988a655de80SLiam Girdwood if (dai_link->ignore) 989a655de80SLiam Girdwood return 0; 990a655de80SLiam Girdwood 9916f2f1ff0SMengdong Lin dev_dbg(card->dev, "ASoC: binding %s\n", dai_link->name); 9926308419aSMark Brown 993bfce78a5SKuninori Morimoto ret = soc_dai_link_sanity_check(card, dai_link); 994bfce78a5SKuninori Morimoto if (ret < 0) 995bfce78a5SKuninori Morimoto return ret; 996bfce78a5SKuninori Morimoto 997513cb311SSudip Mukherjee rtd = soc_new_pcm_runtime(card, dai_link); 998513cb311SSudip Mukherjee if (!rtd) 999513cb311SSudip Mukherjee return -ENOMEM; 1000513cb311SSudip Mukherjee 100176afa643SShreyas NC for_each_link_cpus(dai_link, i, cpu) { 1002c2233a26SKuninori Morimoto asoc_rtd_to_cpu(rtd, i) = snd_soc_find_dai(cpu); 1003c2233a26SKuninori Morimoto if (!asoc_rtd_to_cpu(rtd, i)) { 10046b490879SMartin Hundebøll dev_info(card->dev, "ASoC: CPU DAI %s not registered\n", 100576afa643SShreyas NC cpu->dai_name); 10061a497983SMengdong Lin goto _err_defer; 1007c5af3a2eSMark Brown } 1008c2233a26SKuninori Morimoto snd_soc_rtd_add_component(rtd, asoc_rtd_to_cpu(rtd, i)->component); 100976afa643SShreyas NC } 101076afa643SShreyas NC 101188bd870fSBenoit Cousson /* Find CODEC from registered CODECs */ 101234614739SJerome Brunet for_each_link_codecs(dai_link, i, codec) { 1013c2233a26SKuninori Morimoto asoc_rtd_to_codec(rtd, i) = snd_soc_find_dai(codec); 1014c2233a26SKuninori Morimoto if (!asoc_rtd_to_codec(rtd, i)) { 10157c7e2d6aSStefan Agner dev_info(card->dev, "ASoC: CODEC DAI %s not registered\n", 101634614739SJerome Brunet codec->dai_name); 10171a497983SMengdong Lin goto _err_defer; 101812023a9aSMisael Lopez Cruz } 101934614739SJerome Brunet 1020c2233a26SKuninori Morimoto snd_soc_rtd_add_component(rtd, asoc_rtd_to_codec(rtd, i)->component); 102188bd870fSBenoit Cousson } 102288bd870fSBenoit Cousson 1023e2b30edfSKuninori Morimoto /* Find PLATFORM from registered PLATFORMs */ 102434614739SJerome Brunet for_each_link_platforms(dai_link, i, platform) { 1025368dee94SKuninori Morimoto for_each_component(component) { 102634614739SJerome Brunet if (!snd_soc_is_matching_component(platform, component)) 102790be711eSKuninori Morimoto continue; 102890be711eSKuninori Morimoto 102912b05232SKuninori Morimoto snd_soc_rtd_add_component(rtd, component); 103090be711eSKuninori Morimoto } 103134614739SJerome Brunet } 103290be711eSKuninori Morimoto 1033b19e6e7bSMark Brown return 0; 10341a497983SMengdong Lin 10351a497983SMengdong Lin _err_defer: 103650cd9b53SKuninori Morimoto snd_soc_remove_pcm_runtime(card, rtd); 10371a497983SMengdong Lin return -EPROBE_DEFER; 10386b05eda6SMark Brown } 10390c048004SKuninori Morimoto EXPORT_SYMBOL_GPL(snd_soc_add_pcm_runtime); 10406b05eda6SMark Brown 1041eaffeefbSKuninori Morimoto static int soc_init_pcm_runtime(struct snd_soc_card *card, 104246496acbSKuninori Morimoto struct snd_soc_pcm_runtime *rtd) 104346496acbSKuninori Morimoto { 104446496acbSKuninori Morimoto struct snd_soc_dai_link *dai_link = rtd->dai_link; 1045c2233a26SKuninori Morimoto struct snd_soc_dai *cpu_dai = asoc_rtd_to_cpu(rtd, 0); 104646496acbSKuninori Morimoto struct snd_soc_component *component; 1047613fb500SKuninori Morimoto int ret, num, i; 104846496acbSKuninori Morimoto 104946496acbSKuninori Morimoto /* set default power off timeout */ 105046496acbSKuninori Morimoto rtd->pmdown_time = pmdown_time; 105146496acbSKuninori Morimoto 105246496acbSKuninori Morimoto /* do machine specific initialization */ 105302e75636SKuninori Morimoto ret = snd_soc_link_init(rtd); 105402e75636SKuninori Morimoto if (ret < 0) 105546496acbSKuninori Morimoto return ret; 105646496acbSKuninori Morimoto 105746496acbSKuninori Morimoto if (dai_link->dai_fmt) { 105846496acbSKuninori Morimoto ret = snd_soc_runtime_set_dai_fmt(rtd, dai_link->dai_fmt); 105946496acbSKuninori Morimoto if (ret) 106046496acbSKuninori Morimoto return ret; 106146496acbSKuninori Morimoto } 106246496acbSKuninori Morimoto 106346496acbSKuninori Morimoto /* add DPCM sysfs entries */ 106446496acbSKuninori Morimoto soc_dpcm_debugfs_add(rtd); 106546496acbSKuninori Morimoto 106646496acbSKuninori Morimoto num = rtd->num; 106746496acbSKuninori Morimoto 106846496acbSKuninori Morimoto /* 106946496acbSKuninori Morimoto * most drivers will register their PCMs using DAI link ordering but 107046496acbSKuninori Morimoto * topology based drivers can use the DAI link id field to set PCM 107146496acbSKuninori Morimoto * device number and then use rtd + a base offset of the BEs. 107246496acbSKuninori Morimoto */ 1073613fb500SKuninori Morimoto for_each_rtd_components(rtd, i, component) { 107446496acbSKuninori Morimoto if (!component->driver->use_dai_pcm_id) 107546496acbSKuninori Morimoto continue; 107646496acbSKuninori Morimoto 107746496acbSKuninori Morimoto if (rtd->dai_link->no_pcm) 107846496acbSKuninori Morimoto num += component->driver->be_pcm_base; 107946496acbSKuninori Morimoto else 108046496acbSKuninori Morimoto num = rtd->dai_link->id; 108146496acbSKuninori Morimoto } 108246496acbSKuninori Morimoto 108346496acbSKuninori Morimoto /* create compress_device if possible */ 108446496acbSKuninori Morimoto ret = snd_soc_dai_compress_new(cpu_dai, rtd, num); 108546496acbSKuninori Morimoto if (ret != -ENOTSUPP) { 108646496acbSKuninori Morimoto if (ret < 0) 108746496acbSKuninori Morimoto dev_err(card->dev, "ASoC: can't create compress %s\n", 108846496acbSKuninori Morimoto dai_link->stream_name); 108946496acbSKuninori Morimoto return ret; 109046496acbSKuninori Morimoto } 109146496acbSKuninori Morimoto 109246496acbSKuninori Morimoto /* create the pcm */ 109346496acbSKuninori Morimoto ret = soc_new_pcm(rtd, num); 109446496acbSKuninori Morimoto if (ret < 0) { 109546496acbSKuninori Morimoto dev_err(card->dev, "ASoC: can't create pcm %s :%d\n", 109646496acbSKuninori Morimoto dai_link->stream_name, ret); 109746496acbSKuninori Morimoto return ret; 109846496acbSKuninori Morimoto } 1099d1eb6d11SKuninori Morimoto 11000b73ba55SKuninori Morimoto return snd_soc_pcm_dai_new(rtd); 110146496acbSKuninori Morimoto } 110246496acbSKuninori Morimoto 1103ffd60fbaSKuninori Morimoto static void soc_set_name_prefix(struct snd_soc_card *card, 1104ffd60fbaSKuninori Morimoto struct snd_soc_component *component) 1105ffd60fbaSKuninori Morimoto { 1106aec3ff99SKuninori Morimoto struct device_node *of_node = soc_component_to_node(component); 110729d9fc7aSKuninori Morimoto const char *str; 110829d9fc7aSKuninori Morimoto int ret, i; 1109ffd60fbaSKuninori Morimoto 11104702f991SKuninori Morimoto for (i = 0; i < card->num_configs; i++) { 1111ffd60fbaSKuninori Morimoto struct snd_soc_codec_conf *map = &card->codec_conf[i]; 1112ffd60fbaSKuninori Morimoto 1113c13493a2SKuninori Morimoto if (snd_soc_is_matching_component(&map->dlc, component)) { 1114ffd60fbaSKuninori Morimoto component->name_prefix = map->name_prefix; 1115ffd60fbaSKuninori Morimoto return; 1116ffd60fbaSKuninori Morimoto } 1117c13493a2SKuninori Morimoto } 1118ffd60fbaSKuninori Morimoto 1119ffd60fbaSKuninori Morimoto /* 1120ffd60fbaSKuninori Morimoto * If there is no configuration table or no match in the table, 1121ffd60fbaSKuninori Morimoto * check if a prefix is provided in the node 1122ffd60fbaSKuninori Morimoto */ 112329d9fc7aSKuninori Morimoto ret = of_property_read_string(of_node, "sound-name-prefix", &str); 112429d9fc7aSKuninori Morimoto if (ret < 0) 112529d9fc7aSKuninori Morimoto return; 112629d9fc7aSKuninori Morimoto 112729d9fc7aSKuninori Morimoto component->name_prefix = str; 1128ffd60fbaSKuninori Morimoto } 1129ffd60fbaSKuninori Morimoto 1130c6619b72SKuninori Morimoto static void soc_remove_component(struct snd_soc_component *component, 1131c6619b72SKuninori Morimoto int probed) 113222d14231SKuninori Morimoto { 1133c6619b72SKuninori Morimoto 1134c6619b72SKuninori Morimoto if (!component->card) 1135c6619b72SKuninori Morimoto return; 1136c6619b72SKuninori Morimoto 1137c6619b72SKuninori Morimoto if (probed) 1138c6619b72SKuninori Morimoto snd_soc_component_remove(component); 1139c6619b72SKuninori Morimoto 114004f770d9SKuninori Morimoto /* For framework level robustness */ 11413bb936f5SAmadeusz Sławiński snd_soc_component_set_jack(component, NULL, NULL); 114204f770d9SKuninori Morimoto 11437a5d9815SBard liao list_del_init(&component->card_list); 114422d14231SKuninori Morimoto snd_soc_dapm_free(snd_soc_component_get_dapm(component)); 114522d14231SKuninori Morimoto soc_cleanup_component_debugfs(component); 114622d14231SKuninori Morimoto component->card = NULL; 11470e36f36bSPierre-Louis Bossart snd_soc_component_module_put_when_remove(component); 114822d14231SKuninori Morimoto } 114922d14231SKuninori Morimoto 1150ffd60fbaSKuninori Morimoto static int soc_probe_component(struct snd_soc_card *card, 1151ffd60fbaSKuninori Morimoto struct snd_soc_component *component) 1152ffd60fbaSKuninori Morimoto { 1153ffd60fbaSKuninori Morimoto struct snd_soc_dapm_context *dapm = 1154ffd60fbaSKuninori Morimoto snd_soc_component_get_dapm(component); 1155ffd60fbaSKuninori Morimoto struct snd_soc_dai *dai; 1156c6619b72SKuninori Morimoto int probed = 0; 1157ffd60fbaSKuninori Morimoto int ret; 1158ffd60fbaSKuninori Morimoto 1159ffd60fbaSKuninori Morimoto if (!strcmp(component->name, "snd-soc-dummy")) 1160ffd60fbaSKuninori Morimoto return 0; 1161ffd60fbaSKuninori Morimoto 1162ffd60fbaSKuninori Morimoto if (component->card) { 1163ffd60fbaSKuninori Morimoto if (component->card != card) { 1164ffd60fbaSKuninori Morimoto dev_err(component->dev, 1165ffd60fbaSKuninori Morimoto "Trying to bind component to card \"%s\" but is already bound to card \"%s\"\n", 1166ffd60fbaSKuninori Morimoto card->name, component->card->name); 1167ffd60fbaSKuninori Morimoto return -ENODEV; 1168ffd60fbaSKuninori Morimoto } 1169ffd60fbaSKuninori Morimoto return 0; 1170ffd60fbaSKuninori Morimoto } 1171ffd60fbaSKuninori Morimoto 1172ffd60fbaSKuninori Morimoto ret = snd_soc_component_module_get_when_probe(component); 1173ffd60fbaSKuninori Morimoto if (ret < 0) 1174ffd60fbaSKuninori Morimoto return ret; 1175ffd60fbaSKuninori Morimoto 1176ffd60fbaSKuninori Morimoto component->card = card; 1177ffd60fbaSKuninori Morimoto soc_set_name_prefix(card, component); 1178ffd60fbaSKuninori Morimoto 1179ffd60fbaSKuninori Morimoto soc_init_component_debugfs(component); 1180ffd60fbaSKuninori Morimoto 118195c267ddSKuninori Morimoto snd_soc_dapm_init(dapm, card, component); 1182b614beafSKuninori Morimoto 1183ffd60fbaSKuninori Morimoto ret = snd_soc_dapm_new_controls(dapm, 1184ffd60fbaSKuninori Morimoto component->driver->dapm_widgets, 1185ffd60fbaSKuninori Morimoto component->driver->num_dapm_widgets); 1186ffd60fbaSKuninori Morimoto 1187ffd60fbaSKuninori Morimoto if (ret != 0) { 1188ffd60fbaSKuninori Morimoto dev_err(component->dev, 1189ffd60fbaSKuninori Morimoto "Failed to create new controls %d\n", ret); 1190ffd60fbaSKuninori Morimoto goto err_probe; 1191ffd60fbaSKuninori Morimoto } 1192ffd60fbaSKuninori Morimoto 1193ffd60fbaSKuninori Morimoto for_each_component_dais(component, dai) { 1194ffd60fbaSKuninori Morimoto ret = snd_soc_dapm_new_dai_widgets(dapm, dai); 1195ffd60fbaSKuninori Morimoto if (ret != 0) { 1196ffd60fbaSKuninori Morimoto dev_err(component->dev, 1197ffd60fbaSKuninori Morimoto "Failed to create DAI widgets %d\n", ret); 1198ffd60fbaSKuninori Morimoto goto err_probe; 1199ffd60fbaSKuninori Morimoto } 1200ffd60fbaSKuninori Morimoto } 1201ffd60fbaSKuninori Morimoto 1202ffd60fbaSKuninori Morimoto ret = snd_soc_component_probe(component); 1203ffd60fbaSKuninori Morimoto if (ret < 0) { 1204ffd60fbaSKuninori Morimoto dev_err(component->dev, 1205ffd60fbaSKuninori Morimoto "ASoC: failed to probe component %d\n", ret); 1206ffd60fbaSKuninori Morimoto goto err_probe; 1207ffd60fbaSKuninori Morimoto } 1208ffd60fbaSKuninori Morimoto WARN(dapm->idle_bias_off && 1209ffd60fbaSKuninori Morimoto dapm->bias_level != SND_SOC_BIAS_OFF, 1210ffd60fbaSKuninori Morimoto "codec %s can not start from non-off bias with idle_bias_off==1\n", 1211ffd60fbaSKuninori Morimoto component->name); 1212c6619b72SKuninori Morimoto probed = 1; 1213ffd60fbaSKuninori Morimoto 1214ffd60fbaSKuninori Morimoto /* machine specific init */ 1215ffd60fbaSKuninori Morimoto if (component->init) { 1216ffd60fbaSKuninori Morimoto ret = component->init(component); 1217ffd60fbaSKuninori Morimoto if (ret < 0) { 1218ffd60fbaSKuninori Morimoto dev_err(component->dev, 1219ffd60fbaSKuninori Morimoto "Failed to do machine specific init %d\n", ret); 1220ffd60fbaSKuninori Morimoto goto err_probe; 1221ffd60fbaSKuninori Morimoto } 1222ffd60fbaSKuninori Morimoto } 1223ffd60fbaSKuninori Morimoto 1224ffd60fbaSKuninori Morimoto ret = snd_soc_add_component_controls(component, 1225ffd60fbaSKuninori Morimoto component->driver->controls, 1226ffd60fbaSKuninori Morimoto component->driver->num_controls); 1227ffd60fbaSKuninori Morimoto if (ret < 0) 1228ffd60fbaSKuninori Morimoto goto err_probe; 1229ffd60fbaSKuninori Morimoto 1230ffd60fbaSKuninori Morimoto ret = snd_soc_dapm_add_routes(dapm, 1231ffd60fbaSKuninori Morimoto component->driver->dapm_routes, 1232ffd60fbaSKuninori Morimoto component->driver->num_dapm_routes); 1233a22ae72bSPierre-Louis Bossart if (ret < 0) { 1234a22ae72bSPierre-Louis Bossart if (card->disable_route_checks) { 1235a22ae72bSPierre-Louis Bossart dev_info(card->dev, 1236a22ae72bSPierre-Louis Bossart "%s: disable_route_checks set, ignoring errors on add_routes\n", 1237a22ae72bSPierre-Louis Bossart __func__); 1238a22ae72bSPierre-Louis Bossart } else { 1239a22ae72bSPierre-Louis Bossart dev_err(card->dev, 1240a22ae72bSPierre-Louis Bossart "%s: snd_soc_dapm_add_routes failed: %d\n", 1241a22ae72bSPierre-Louis Bossart __func__, ret); 1242ffd60fbaSKuninori Morimoto goto err_probe; 1243a22ae72bSPierre-Louis Bossart } 1244a22ae72bSPierre-Louis Bossart } 1245ffd60fbaSKuninori Morimoto 1246ffd60fbaSKuninori Morimoto /* see for_each_card_components */ 1247ffd60fbaSKuninori Morimoto list_add(&component->card_list, &card->component_dev_list); 1248ffd60fbaSKuninori Morimoto 1249ffd60fbaSKuninori Morimoto err_probe: 1250ffd60fbaSKuninori Morimoto if (ret < 0) 1251c6619b72SKuninori Morimoto soc_remove_component(component, probed); 1252ffd60fbaSKuninori Morimoto 1253ffd60fbaSKuninori Morimoto return ret; 1254ffd60fbaSKuninori Morimoto } 1255ffd60fbaSKuninori Morimoto 12564ca47d21SKuninori Morimoto static void soc_remove_link_dais(struct snd_soc_card *card) 1257f0fba2adSLiam Girdwood { 12584ca47d21SKuninori Morimoto struct snd_soc_pcm_runtime *rtd; 12594ca47d21SKuninori Morimoto int order; 12604ca47d21SKuninori Morimoto 12614ca47d21SKuninori Morimoto for_each_comp_order(order) { 12624ca47d21SKuninori Morimoto for_each_card_rtds(card, rtd) { 12637eaa313bSKuninori Morimoto /* remove all rtd connected DAIs in good order */ 12647eaa313bSKuninori Morimoto snd_soc_pcm_dai_remove(rtd, order); 1265f0fba2adSLiam Girdwood } 12664ca47d21SKuninori Morimoto } 12674ca47d21SKuninori Morimoto } 1268f0fba2adSLiam Girdwood 1269bc7c16c2SKuninori Morimoto static int soc_probe_link_dais(struct snd_soc_card *card) 1270bc7c16c2SKuninori Morimoto { 1271bc7c16c2SKuninori Morimoto struct snd_soc_pcm_runtime *rtd; 127251801aeaSKuninori Morimoto int order, ret; 1273bc7c16c2SKuninori Morimoto 1274bc7c16c2SKuninori Morimoto for_each_comp_order(order) { 1275bc7c16c2SKuninori Morimoto for_each_card_rtds(card, rtd) { 1276bc7c16c2SKuninori Morimoto 1277bc7c16c2SKuninori Morimoto dev_dbg(card->dev, 1278bc7c16c2SKuninori Morimoto "ASoC: probe %s dai link %d late %d\n", 1279bc7c16c2SKuninori Morimoto card->name, rtd->num, order); 1280bc7c16c2SKuninori Morimoto 128151801aeaSKuninori Morimoto /* probe all rtd connected DAIs in good order */ 128251801aeaSKuninori Morimoto ret = snd_soc_pcm_dai_probe(rtd, order); 1283bc7c16c2SKuninori Morimoto if (ret) 1284bc7c16c2SKuninori Morimoto return ret; 1285bc7c16c2SKuninori Morimoto } 1286bc7c16c2SKuninori Morimoto } 1287bc7c16c2SKuninori Morimoto 1288bc7c16c2SKuninori Morimoto return 0; 1289bc7c16c2SKuninori Morimoto } 1290bc7c16c2SKuninori Morimoto 1291b006c0c6SKuninori Morimoto static void soc_remove_link_components(struct snd_soc_card *card) 129262ae68faSStephen Warren { 129361aca564SLars-Peter Clausen struct snd_soc_component *component; 1294b006c0c6SKuninori Morimoto struct snd_soc_pcm_runtime *rtd; 1295613fb500SKuninori Morimoto int i, order; 129662ae68faSStephen Warren 1297b006c0c6SKuninori Morimoto for_each_comp_order(order) { 1298b006c0c6SKuninori Morimoto for_each_card_rtds(card, rtd) { 1299613fb500SKuninori Morimoto for_each_rtd_components(rtd, i, component) { 1300b006c0c6SKuninori Morimoto if (component->driver->remove_order != order) 1301b006c0c6SKuninori Morimoto continue; 1302b006c0c6SKuninori Morimoto 1303c6619b72SKuninori Morimoto soc_remove_component(component, 1); 130462ae68faSStephen Warren } 130562ae68faSStephen Warren } 1306b006c0c6SKuninori Morimoto } 1307b006c0c6SKuninori Morimoto } 130862ae68faSStephen Warren 130962f07a6bSKuninori Morimoto static int soc_probe_link_components(struct snd_soc_card *card) 13106fb03550SKuninori Morimoto { 13116fb03550SKuninori Morimoto struct snd_soc_component *component; 131262f07a6bSKuninori Morimoto struct snd_soc_pcm_runtime *rtd; 1313613fb500SKuninori Morimoto int i, ret, order; 13146fb03550SKuninori Morimoto 131562f07a6bSKuninori Morimoto for_each_comp_order(order) { 131662f07a6bSKuninori Morimoto for_each_card_rtds(card, rtd) { 1317613fb500SKuninori Morimoto for_each_rtd_components(rtd, i, component) { 131862f07a6bSKuninori Morimoto if (component->driver->probe_order != order) 131962f07a6bSKuninori Morimoto continue; 132062f07a6bSKuninori Morimoto 13216fb03550SKuninori Morimoto ret = soc_probe_component(card, component); 13226fb03550SKuninori Morimoto if (ret < 0) 13236fb03550SKuninori Morimoto return ret; 13246fb03550SKuninori Morimoto } 13256fb03550SKuninori Morimoto } 132662f07a6bSKuninori Morimoto } 13276fb03550SKuninori Morimoto 13286fb03550SKuninori Morimoto return 0; 13296fb03550SKuninori Morimoto } 13306fb03550SKuninori Morimoto 1331e8fbd250SKuninori Morimoto static void soc_unbind_aux_dev(struct snd_soc_card *card) 13324893a2ebSKuninori Morimoto { 1333e8fbd250SKuninori Morimoto struct snd_soc_component *component, *_component; 1334e8fbd250SKuninori Morimoto 1335e8fbd250SKuninori Morimoto for_each_card_auxs_safe(card, component, _component) { 13364893a2ebSKuninori Morimoto component->init = NULL; 13374893a2ebSKuninori Morimoto list_del(&component->card_aux_list); 13384893a2ebSKuninori Morimoto } 1339e8fbd250SKuninori Morimoto } 13404893a2ebSKuninori Morimoto 1341bee886f1SKuninori Morimoto static int soc_bind_aux_dev(struct snd_soc_card *card) 1342b19e6e7bSMark Brown { 1343f2ed6b07SMengdong Lin struct snd_soc_component *component; 1344bee886f1SKuninori Morimoto struct snd_soc_aux_dev *aux; 1345bee886f1SKuninori Morimoto int i; 13463ca041edSSebastian Reichel 1347bee886f1SKuninori Morimoto for_each_card_pre_auxs(card, i, aux) { 1348f2ed6b07SMengdong Lin /* codecs, usually analog devices */ 1349bee886f1SKuninori Morimoto component = soc_find_component(&aux->dlc); 1350f2ed6b07SMengdong Lin if (!component) 13513dc29b8bSKuninori Morimoto return -EPROBE_DEFER; 13523ca041edSSebastian Reichel 1353bee886f1SKuninori Morimoto component->init = aux->init; 1354c2b71c71SKuninori Morimoto /* see for_each_card_auxs */ 1355d2e3a135SSylwester Nawrocki list_add(&component->card_aux_list, &card->aux_comp_list); 1356bee886f1SKuninori Morimoto } 1357f2ed6b07SMengdong Lin return 0; 1358b19e6e7bSMark Brown } 1359b19e6e7bSMark Brown 1360f2ed6b07SMengdong Lin static int soc_probe_aux_devices(struct snd_soc_card *card) 1361f2ed6b07SMengdong Lin { 136274bd3f92SKuninori Morimoto struct snd_soc_component *component; 1363f2ed6b07SMengdong Lin int order; 1364f2ed6b07SMengdong Lin int ret; 1365f2ed6b07SMengdong Lin 13661a1035a9SKuninori Morimoto for_each_comp_order(order) { 136774bd3f92SKuninori Morimoto for_each_card_auxs(card, component) { 136874bd3f92SKuninori Morimoto if (component->driver->probe_order != order) 136974bd3f92SKuninori Morimoto continue; 137074bd3f92SKuninori Morimoto 137174bd3f92SKuninori Morimoto ret = soc_probe_component(card, component); 137274bd3f92SKuninori Morimoto if (ret < 0) 1373f2ed6b07SMengdong Lin return ret; 1374f2ed6b07SMengdong Lin } 1375f2ed6b07SMengdong Lin } 137665d9361fSLars-Peter Clausen 137744c69bb1SLars-Peter Clausen return 0; 13783ca041edSSebastian Reichel } 13792eea392dSJarkko Nikula 1380f2ed6b07SMengdong Lin static void soc_remove_aux_devices(struct snd_soc_card *card) 138144c69bb1SLars-Peter Clausen { 1382f2ed6b07SMengdong Lin struct snd_soc_component *comp, *_comp; 1383f2ed6b07SMengdong Lin int order; 138444c69bb1SLars-Peter Clausen 13851a1035a9SKuninori Morimoto for_each_comp_order(order) { 1386c2b71c71SKuninori Morimoto for_each_card_auxs_safe(card, comp, _comp) { 1387e8fbd250SKuninori Morimoto if (comp->driver->remove_order == order) 1388c6619b72SKuninori Morimoto soc_remove_component(comp, 1); 13895f3484acSLars-Peter Clausen } 13902eea392dSJarkko Nikula } 13912eea392dSJarkko Nikula } 13922eea392dSJarkko Nikula 1393ce64c8b9SLars-Peter Clausen /** 1394ce64c8b9SLars-Peter Clausen * snd_soc_runtime_set_dai_fmt() - Change DAI link format for a ASoC runtime 1395ce64c8b9SLars-Peter Clausen * @rtd: The runtime for which the DAI link format should be changed 1396ce64c8b9SLars-Peter Clausen * @dai_fmt: The new DAI link format 1397ce64c8b9SLars-Peter Clausen * 1398ce64c8b9SLars-Peter Clausen * This function updates the DAI link format for all DAIs connected to the DAI 1399ce64c8b9SLars-Peter Clausen * link for the specified runtime. 1400ce64c8b9SLars-Peter Clausen * 1401ce64c8b9SLars-Peter Clausen * Note: For setups with a static format set the dai_fmt field in the 1402ce64c8b9SLars-Peter Clausen * corresponding snd_dai_link struct instead of using this function. 1403ce64c8b9SLars-Peter Clausen * 1404ce64c8b9SLars-Peter Clausen * Returns 0 on success, otherwise a negative error code. 1405ce64c8b9SLars-Peter Clausen */ 1406ce64c8b9SLars-Peter Clausen int snd_soc_runtime_set_dai_fmt(struct snd_soc_pcm_runtime *rtd, 1407ce64c8b9SLars-Peter Clausen unsigned int dai_fmt) 1408ce64c8b9SLars-Peter Clausen { 140976afa643SShreyas NC struct snd_soc_dai *cpu_dai; 14100b7990e3SKuninori Morimoto struct snd_soc_dai *codec_dai; 141176afa643SShreyas NC unsigned int inv_dai_fmt; 1412ce64c8b9SLars-Peter Clausen unsigned int i; 1413ce64c8b9SLars-Peter Clausen int ret; 1414ce64c8b9SLars-Peter Clausen 1415a4be4187SKuninori Morimoto for_each_rtd_codec_dais(rtd, i, codec_dai) { 1416ce64c8b9SLars-Peter Clausen ret = snd_soc_dai_set_fmt(codec_dai, dai_fmt); 1417ce64c8b9SLars-Peter Clausen if (ret != 0 && ret != -ENOTSUPP) { 1418ce64c8b9SLars-Peter Clausen dev_warn(codec_dai->dev, 1419ce64c8b9SLars-Peter Clausen "ASoC: Failed to set DAI format: %d\n", ret); 1420ce64c8b9SLars-Peter Clausen return ret; 1421ce64c8b9SLars-Peter Clausen } 1422ce64c8b9SLars-Peter Clausen } 1423ce64c8b9SLars-Peter Clausen 14242c7b696aSMarcel Ziswiler /* 14252c7b696aSMarcel Ziswiler * Flip the polarity for the "CPU" end of a CODEC<->CODEC link 14262c7b696aSMarcel Ziswiler * the component which has non_legacy_dai_naming is Codec 14272c7b696aSMarcel Ziswiler */ 1428ce64c8b9SLars-Peter Clausen inv_dai_fmt = dai_fmt & ~SND_SOC_DAIFMT_MASTER_MASK; 1429ce64c8b9SLars-Peter Clausen switch (dai_fmt & SND_SOC_DAIFMT_MASTER_MASK) { 1430ce64c8b9SLars-Peter Clausen case SND_SOC_DAIFMT_CBM_CFM: 1431ce64c8b9SLars-Peter Clausen inv_dai_fmt |= SND_SOC_DAIFMT_CBS_CFS; 1432ce64c8b9SLars-Peter Clausen break; 1433ce64c8b9SLars-Peter Clausen case SND_SOC_DAIFMT_CBM_CFS: 1434ce64c8b9SLars-Peter Clausen inv_dai_fmt |= SND_SOC_DAIFMT_CBS_CFM; 1435ce64c8b9SLars-Peter Clausen break; 1436ce64c8b9SLars-Peter Clausen case SND_SOC_DAIFMT_CBS_CFM: 1437ce64c8b9SLars-Peter Clausen inv_dai_fmt |= SND_SOC_DAIFMT_CBM_CFS; 1438ce64c8b9SLars-Peter Clausen break; 1439ce64c8b9SLars-Peter Clausen case SND_SOC_DAIFMT_CBS_CFS: 1440ce64c8b9SLars-Peter Clausen inv_dai_fmt |= SND_SOC_DAIFMT_CBM_CFM; 1441ce64c8b9SLars-Peter Clausen break; 1442ce64c8b9SLars-Peter Clausen } 1443a4be4187SKuninori Morimoto for_each_rtd_cpu_dais(rtd, i, cpu_dai) { 144476afa643SShreyas NC unsigned int fmt = dai_fmt; 1445ce64c8b9SLars-Peter Clausen 144676afa643SShreyas NC if (cpu_dai->component->driver->non_legacy_dai_naming) 144776afa643SShreyas NC fmt = inv_dai_fmt; 1448ce64c8b9SLars-Peter Clausen 144976afa643SShreyas NC ret = snd_soc_dai_set_fmt(cpu_dai, fmt); 1450ce64c8b9SLars-Peter Clausen if (ret != 0 && ret != -ENOTSUPP) { 1451ce64c8b9SLars-Peter Clausen dev_warn(cpu_dai->dev, 1452ce64c8b9SLars-Peter Clausen "ASoC: Failed to set DAI format: %d\n", ret); 1453ce64c8b9SLars-Peter Clausen return ret; 1454ce64c8b9SLars-Peter Clausen } 145576afa643SShreyas NC } 1456ce64c8b9SLars-Peter Clausen 1457ce64c8b9SLars-Peter Clausen return 0; 1458ce64c8b9SLars-Peter Clausen } 1459ddaca25aSLars-Peter Clausen EXPORT_SYMBOL_GPL(snd_soc_runtime_set_dai_fmt); 1460ce64c8b9SLars-Peter Clausen 14611f5a4535STakashi Iwai #ifdef CONFIG_DMI 14622c7b696aSMarcel Ziswiler /* 14638a6a6a38SKuninori Morimoto * If a DMI filed contain strings in this blacklist (e.g. 14648a6a6a38SKuninori Morimoto * "Type2 - Board Manufacturer" or "Type1 - TBD by OEM"), it will be taken 14658a6a6a38SKuninori Morimoto * as invalid and dropped when setting the card long name from DMI info. 14668a6a6a38SKuninori Morimoto */ 14678a6a6a38SKuninori Morimoto static const char * const dmi_blacklist[] = { 14688a6a6a38SKuninori Morimoto "To be filled by OEM", 14698a6a6a38SKuninori Morimoto "TBD by OEM", 14708a6a6a38SKuninori Morimoto "Default String", 14718a6a6a38SKuninori Morimoto "Board Manufacturer", 14728a6a6a38SKuninori Morimoto "Board Vendor Name", 14738a6a6a38SKuninori Morimoto "Board Product Name", 14748a6a6a38SKuninori Morimoto NULL, /* terminator */ 14758a6a6a38SKuninori Morimoto }; 14768a6a6a38SKuninori Morimoto 14778a6a6a38SKuninori Morimoto /* 14782c7b696aSMarcel Ziswiler * Trim special characters, and replace '-' with '_' since '-' is used to 1479345233d7SLiam Girdwood * separate different DMI fields in the card long name. Only number and 1480345233d7SLiam Girdwood * alphabet characters and a few separator characters are kept. 1481345233d7SLiam Girdwood */ 1482345233d7SLiam Girdwood static void cleanup_dmi_name(char *name) 1483345233d7SLiam Girdwood { 1484345233d7SLiam Girdwood int i, j = 0; 1485345233d7SLiam Girdwood 1486345233d7SLiam Girdwood for (i = 0; name[i]; i++) { 1487345233d7SLiam Girdwood if (isalnum(name[i]) || (name[i] == '.') 1488345233d7SLiam Girdwood || (name[i] == '_')) 1489345233d7SLiam Girdwood name[j++] = name[i]; 1490345233d7SLiam Girdwood else if (name[i] == '-') 1491345233d7SLiam Girdwood name[j++] = '_'; 1492345233d7SLiam Girdwood } 1493345233d7SLiam Girdwood 1494345233d7SLiam Girdwood name[j] = '\0'; 1495345233d7SLiam Girdwood } 1496345233d7SLiam Girdwood 14972c7b696aSMarcel Ziswiler /* 14982c7b696aSMarcel Ziswiler * Check if a DMI field is valid, i.e. not containing any string 149998faf436SMengdong Lin * in the black list. 150098faf436SMengdong Lin */ 150198faf436SMengdong Lin static int is_dmi_valid(const char *field) 150298faf436SMengdong Lin { 150398faf436SMengdong Lin int i = 0; 150498faf436SMengdong Lin 150598faf436SMengdong Lin while (dmi_blacklist[i]) { 150698faf436SMengdong Lin if (strstr(field, dmi_blacklist[i])) 150798faf436SMengdong Lin return 0; 150898faf436SMengdong Lin i++; 150946b5a4d2SWu Fengguang } 151098faf436SMengdong Lin 151198faf436SMengdong Lin return 1; 151298faf436SMengdong Lin } 151398faf436SMengdong Lin 15144e01e5dbSJaroslav Kysela /* 15154e01e5dbSJaroslav Kysela * Append a string to card->dmi_longname with character cleanups. 15164e01e5dbSJaroslav Kysela */ 15174e01e5dbSJaroslav Kysela static void append_dmi_string(struct snd_soc_card *card, const char *str) 15184e01e5dbSJaroslav Kysela { 15194e01e5dbSJaroslav Kysela char *dst = card->dmi_longname; 15204e01e5dbSJaroslav Kysela size_t dst_len = sizeof(card->dmi_longname); 15214e01e5dbSJaroslav Kysela size_t len; 15224e01e5dbSJaroslav Kysela 15234e01e5dbSJaroslav Kysela len = strlen(dst); 15244e01e5dbSJaroslav Kysela snprintf(dst + len, dst_len - len, "-%s", str); 15254e01e5dbSJaroslav Kysela 15264e01e5dbSJaroslav Kysela len++; /* skip the separator "-" */ 15274e01e5dbSJaroslav Kysela if (len < dst_len) 15284e01e5dbSJaroslav Kysela cleanup_dmi_name(dst + len); 15294e01e5dbSJaroslav Kysela } 15304e01e5dbSJaroslav Kysela 1531345233d7SLiam Girdwood /** 1532345233d7SLiam Girdwood * snd_soc_set_dmi_name() - Register DMI names to card 1533345233d7SLiam Girdwood * @card: The card to register DMI names 1534345233d7SLiam Girdwood * @flavour: The flavour "differentiator" for the card amongst its peers. 1535345233d7SLiam Girdwood * 1536345233d7SLiam Girdwood * An Intel machine driver may be used by many different devices but are 1537345233d7SLiam Girdwood * difficult for userspace to differentiate, since machine drivers ususally 1538345233d7SLiam Girdwood * use their own name as the card short name and leave the card long name 1539345233d7SLiam Girdwood * blank. To differentiate such devices and fix bugs due to lack of 1540345233d7SLiam Girdwood * device-specific configurations, this function allows DMI info to be used 1541345233d7SLiam Girdwood * as the sound card long name, in the format of 1542345233d7SLiam Girdwood * "vendor-product-version-board" 1543345233d7SLiam Girdwood * (Character '-' is used to separate different DMI fields here). 1544345233d7SLiam Girdwood * This will help the user space to load the device-specific Use Case Manager 1545345233d7SLiam Girdwood * (UCM) configurations for the card. 1546345233d7SLiam Girdwood * 1547345233d7SLiam Girdwood * Possible card long names may be: 1548345233d7SLiam Girdwood * DellInc.-XPS139343-01-0310JH 1549345233d7SLiam Girdwood * ASUSTeKCOMPUTERINC.-T100TA-1.0-T100TA 1550345233d7SLiam Girdwood * Circuitco-MinnowboardMaxD0PLATFORM-D0-MinnowBoardMAX 1551345233d7SLiam Girdwood * 1552345233d7SLiam Girdwood * This function also supports flavoring the card longname to provide 1553345233d7SLiam Girdwood * the extra differentiation, like "vendor-product-version-board-flavor". 1554345233d7SLiam Girdwood * 1555345233d7SLiam Girdwood * We only keep number and alphabet characters and a few separator characters 1556345233d7SLiam Girdwood * in the card long name since UCM in the user space uses the card long names 1557345233d7SLiam Girdwood * as card configuration directory names and AudoConf cannot support special 1558345233d7SLiam Girdwood * charactors like SPACE. 1559345233d7SLiam Girdwood * 1560345233d7SLiam Girdwood * Returns 0 on success, otherwise a negative error code. 1561345233d7SLiam Girdwood */ 1562345233d7SLiam Girdwood int snd_soc_set_dmi_name(struct snd_soc_card *card, const char *flavour) 1563345233d7SLiam Girdwood { 1564345233d7SLiam Girdwood const char *vendor, *product, *product_version, *board; 1565345233d7SLiam Girdwood 1566345233d7SLiam Girdwood if (card->long_name) 1567345233d7SLiam Girdwood return 0; /* long name already set by driver or from DMI */ 1568345233d7SLiam Girdwood 15694e01e5dbSJaroslav Kysela /* make up dmi long name as: vendor-product-version-board */ 1570345233d7SLiam Girdwood vendor = dmi_get_system_info(DMI_BOARD_VENDOR); 157198faf436SMengdong Lin if (!vendor || !is_dmi_valid(vendor)) { 1572345233d7SLiam Girdwood dev_warn(card->dev, "ASoC: no DMI vendor name!\n"); 1573345233d7SLiam Girdwood return 0; 1574345233d7SLiam Girdwood } 1575345233d7SLiam Girdwood 15764e01e5dbSJaroslav Kysela snprintf(card->dmi_longname, sizeof(card->dmi_longname), "%s", vendor); 1577345233d7SLiam Girdwood cleanup_dmi_name(card->dmi_longname); 1578345233d7SLiam Girdwood 1579345233d7SLiam Girdwood product = dmi_get_system_info(DMI_PRODUCT_NAME); 158098faf436SMengdong Lin if (product && is_dmi_valid(product)) { 15814e01e5dbSJaroslav Kysela append_dmi_string(card, product); 1582345233d7SLiam Girdwood 15832c7b696aSMarcel Ziswiler /* 15842c7b696aSMarcel Ziswiler * some vendors like Lenovo may only put a self-explanatory 1585345233d7SLiam Girdwood * name in the product version field 1586345233d7SLiam Girdwood */ 1587345233d7SLiam Girdwood product_version = dmi_get_system_info(DMI_PRODUCT_VERSION); 15884e01e5dbSJaroslav Kysela if (product_version && is_dmi_valid(product_version)) 15894e01e5dbSJaroslav Kysela append_dmi_string(card, product_version); 1590345233d7SLiam Girdwood } 1591345233d7SLiam Girdwood 1592345233d7SLiam Girdwood board = dmi_get_system_info(DMI_BOARD_NAME); 159398faf436SMengdong Lin if (board && is_dmi_valid(board)) { 159439870b0dSJaroslav Kysela if (!product || strcasecmp(board, product)) 15954e01e5dbSJaroslav Kysela append_dmi_string(card, board); 1596345233d7SLiam Girdwood } else if (!product) { 1597345233d7SLiam Girdwood /* fall back to using legacy name */ 1598345233d7SLiam Girdwood dev_warn(card->dev, "ASoC: no DMI board/product name!\n"); 1599345233d7SLiam Girdwood return 0; 1600345233d7SLiam Girdwood } 1601345233d7SLiam Girdwood 1602345233d7SLiam Girdwood /* Add flavour to dmi long name */ 16034e01e5dbSJaroslav Kysela if (flavour) 16044e01e5dbSJaroslav Kysela append_dmi_string(card, flavour); 1605345233d7SLiam Girdwood 1606345233d7SLiam Girdwood /* set the card long name */ 1607345233d7SLiam Girdwood card->long_name = card->dmi_longname; 1608345233d7SLiam Girdwood 1609345233d7SLiam Girdwood return 0; 1610345233d7SLiam Girdwood } 1611345233d7SLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_set_dmi_name); 16121f5a4535STakashi Iwai #endif /* CONFIG_DMI */ 1613345233d7SLiam Girdwood 1614a655de80SLiam Girdwood static void soc_check_tplg_fes(struct snd_soc_card *card) 1615a655de80SLiam Girdwood { 1616a655de80SLiam Girdwood struct snd_soc_component *component; 1617a655de80SLiam Girdwood const struct snd_soc_component_driver *comp_drv; 1618a655de80SLiam Girdwood struct snd_soc_dai_link *dai_link; 1619a655de80SLiam Girdwood int i; 1620a655de80SLiam Girdwood 1621368dee94SKuninori Morimoto for_each_component(component) { 1622a655de80SLiam Girdwood 162349f9c4f2SDaniel Baluta /* does this component override BEs ? */ 1624a655de80SLiam Girdwood if (!component->driver->ignore_machine) 1625a655de80SLiam Girdwood continue; 1626a655de80SLiam Girdwood 1627a655de80SLiam Girdwood /* for this machine ? */ 1628e194098bSPierre-Louis Bossart if (!strcmp(component->driver->ignore_machine, 1629a655de80SLiam Girdwood card->dev->driver->name)) 1630e194098bSPierre-Louis Bossart goto match; 1631e194098bSPierre-Louis Bossart if (strcmp(component->driver->ignore_machine, 1632e194098bSPierre-Louis Bossart dev_name(card->dev))) 1633a655de80SLiam Girdwood continue; 1634e194098bSPierre-Louis Bossart match: 1635a655de80SLiam Girdwood /* machine matches, so override the rtd data */ 16367fe072b4SKuninori Morimoto for_each_card_prelinks(card, i, dai_link) { 1637a655de80SLiam Girdwood 1638a655de80SLiam Girdwood /* ignore this FE */ 1639a655de80SLiam Girdwood if (dai_link->dynamic) { 1640a655de80SLiam Girdwood dai_link->ignore = true; 1641a655de80SLiam Girdwood continue; 1642a655de80SLiam Girdwood } 1643a655de80SLiam Girdwood 164449f9c4f2SDaniel Baluta dev_info(card->dev, "info: override BE DAI link %s\n", 1645a655de80SLiam Girdwood card->dai_link[i].name); 1646a655de80SLiam Girdwood 1647a655de80SLiam Girdwood /* override platform component */ 1648adb76b5bSKuninori Morimoto if (!dai_link->platforms) { 1649daecf46eSKuninori Morimoto dev_err(card->dev, "init platform error"); 1650daecf46eSKuninori Morimoto continue; 1651daecf46eSKuninori Morimoto } 1652910fdcabSKuninori Morimoto dai_link->platforms->name = component->name; 1653a655de80SLiam Girdwood 1654a655de80SLiam Girdwood /* convert non BE into BE */ 1655607fa205SBard Liao if (!dai_link->no_pcm) { 1656a655de80SLiam Girdwood dai_link->no_pcm = 1; 1657607fa205SBard Liao 1658607fa205SBard Liao if (dai_link->dpcm_playback) 1659607fa205SBard Liao dev_warn(card->dev, 1660607fa205SBard Liao "invalid configuration, dailink %s has flags no_pcm=0 and dpcm_playback=1\n", 1661607fa205SBard Liao dai_link->name); 1662607fa205SBard Liao if (dai_link->dpcm_capture) 1663607fa205SBard Liao dev_warn(card->dev, 1664607fa205SBard Liao "invalid configuration, dailink %s has flags no_pcm=0 and dpcm_capture=1\n", 1665607fa205SBard Liao dai_link->name); 1666607fa205SBard Liao 1667607fa205SBard Liao /* convert normal link into DPCM one */ 1668607fa205SBard Liao if (!(dai_link->dpcm_playback || 1669607fa205SBard Liao dai_link->dpcm_capture)) { 1670607fa205SBard Liao dai_link->dpcm_playback = !dai_link->capture_only; 1671607fa205SBard Liao dai_link->dpcm_capture = !dai_link->playback_only; 1672607fa205SBard Liao } 1673607fa205SBard Liao } 1674a655de80SLiam Girdwood 16750cbbf8a0SKuninori Morimoto /* 16760cbbf8a0SKuninori Morimoto * override any BE fixups 16770cbbf8a0SKuninori Morimoto * see 16780cbbf8a0SKuninori Morimoto * snd_soc_link_be_hw_params_fixup() 16790cbbf8a0SKuninori Morimoto */ 1680a655de80SLiam Girdwood dai_link->be_hw_params_fixup = 1681a655de80SLiam Girdwood component->driver->be_hw_params_fixup; 1682a655de80SLiam Girdwood 16832c7b696aSMarcel Ziswiler /* 16842c7b696aSMarcel Ziswiler * most BE links don't set stream name, so set it to 1685a655de80SLiam Girdwood * dai link name if it's NULL to help bind widgets. 1686a655de80SLiam Girdwood */ 1687a655de80SLiam Girdwood if (!dai_link->stream_name) 1688a655de80SLiam Girdwood dai_link->stream_name = dai_link->name; 1689a655de80SLiam Girdwood } 1690a655de80SLiam Girdwood 1691a655de80SLiam Girdwood /* Inform userspace we are using alternate topology */ 1692a655de80SLiam Girdwood if (component->driver->topology_name_prefix) { 1693a655de80SLiam Girdwood 1694a655de80SLiam Girdwood /* topology shortname created? */ 1695a655de80SLiam Girdwood if (!card->topology_shortname_created) { 1696a655de80SLiam Girdwood comp_drv = component->driver; 1697a655de80SLiam Girdwood 1698a655de80SLiam Girdwood snprintf(card->topology_shortname, 32, "%s-%s", 1699a655de80SLiam Girdwood comp_drv->topology_name_prefix, 1700a655de80SLiam Girdwood card->name); 1701a655de80SLiam Girdwood card->topology_shortname_created = true; 1702a655de80SLiam Girdwood } 1703a655de80SLiam Girdwood 1704a655de80SLiam Girdwood /* use topology shortname */ 1705a655de80SLiam Girdwood card->name = card->topology_shortname; 1706a655de80SLiam Girdwood } 1707a655de80SLiam Girdwood } 1708a655de80SLiam Girdwood } 1709a655de80SLiam Girdwood 17100f23f718SKuninori Morimoto #define soc_setup_card_name(name, name1, name2, norm) \ 17110f23f718SKuninori Morimoto __soc_setup_card_name(name, sizeof(name), name1, name2, norm) 17120f23f718SKuninori Morimoto static void __soc_setup_card_name(char *name, int len, 17130f23f718SKuninori Morimoto const char *name1, const char *name2, 17140f23f718SKuninori Morimoto int normalization) 17150f23f718SKuninori Morimoto { 17160f23f718SKuninori Morimoto int i; 17170f23f718SKuninori Morimoto 17180f23f718SKuninori Morimoto snprintf(name, len, "%s", name1 ? name1 : name2); 17190f23f718SKuninori Morimoto 17200f23f718SKuninori Morimoto if (!normalization) 17210f23f718SKuninori Morimoto return; 17220f23f718SKuninori Morimoto 17230f23f718SKuninori Morimoto /* 17240f23f718SKuninori Morimoto * Name normalization 17250f23f718SKuninori Morimoto * 17260f23f718SKuninori Morimoto * The driver name is somewhat special, as it's used as a key for 17270f23f718SKuninori Morimoto * searches in the user-space. 17280f23f718SKuninori Morimoto * 17290f23f718SKuninori Morimoto * ex) 17300f23f718SKuninori Morimoto * "abcd??efg" -> "abcd__efg" 17310f23f718SKuninori Morimoto */ 17320f23f718SKuninori Morimoto for (i = 0; i < len; i++) { 17330f23f718SKuninori Morimoto switch (name[i]) { 17340f23f718SKuninori Morimoto case '_': 17350f23f718SKuninori Morimoto case '-': 17360f23f718SKuninori Morimoto case '\0': 17370f23f718SKuninori Morimoto break; 17380f23f718SKuninori Morimoto default: 17390f23f718SKuninori Morimoto if (!isalnum(name[i])) 17400f23f718SKuninori Morimoto name[i] = '_'; 17410f23f718SKuninori Morimoto break; 17420f23f718SKuninori Morimoto } 17430f23f718SKuninori Morimoto } 17440f23f718SKuninori Morimoto } 17450f23f718SKuninori Morimoto 174627f07cacSKuninori Morimoto static void soc_cleanup_card_resources(struct snd_soc_card *card) 174753e947a0SKuninori Morimoto { 1748cc733900SKuninori Morimoto struct snd_soc_pcm_runtime *rtd, *n; 17497ce6088fSKuninori Morimoto 17500ced7b05SKuninori Morimoto if (card->snd_card) 17510ced7b05SKuninori Morimoto snd_card_disconnect_sync(card->snd_card); 175253e947a0SKuninori Morimoto 17532a6f0892SKuninori Morimoto snd_soc_dapm_shutdown(card); 17542a6f0892SKuninori Morimoto 175553e947a0SKuninori Morimoto /* remove and free each DAI */ 17567ce6088fSKuninori Morimoto soc_remove_link_dais(card); 17570ced7b05SKuninori Morimoto soc_remove_link_components(card); 17587ce6088fSKuninori Morimoto 1759cc733900SKuninori Morimoto for_each_card_rtds_safe(card, rtd, n) 176050cd9b53SKuninori Morimoto snd_soc_remove_pcm_runtime(card, rtd); 17617ce6088fSKuninori Morimoto 176253e947a0SKuninori Morimoto /* remove auxiliary devices */ 176353e947a0SKuninori Morimoto soc_remove_aux_devices(card); 1764e8fbd250SKuninori Morimoto soc_unbind_aux_dev(card); 176553e947a0SKuninori Morimoto 176653e947a0SKuninori Morimoto snd_soc_dapm_free(&card->dapm); 176753e947a0SKuninori Morimoto soc_cleanup_card_debugfs(card); 176853e947a0SKuninori Morimoto 176953e947a0SKuninori Morimoto /* remove the card */ 1770b0275d95SKuninori Morimoto snd_soc_card_remove(card); 17710ced7b05SKuninori Morimoto 17720ced7b05SKuninori Morimoto if (card->snd_card) { 17730ced7b05SKuninori Morimoto snd_card_free(card->snd_card); 17740ced7b05SKuninori Morimoto card->snd_card = NULL; 17750ced7b05SKuninori Morimoto } 177653e947a0SKuninori Morimoto } 177753e947a0SKuninori Morimoto 17782cc1afcfSKuninori Morimoto static void snd_soc_unbind_card(struct snd_soc_card *card, bool unregister) 17792cc1afcfSKuninori Morimoto { 17802cc1afcfSKuninori Morimoto if (card->instantiated) { 17812cc1afcfSKuninori Morimoto card->instantiated = false; 17822cc1afcfSKuninori Morimoto snd_soc_flush_all_delayed_work(card); 17832cc1afcfSKuninori Morimoto 178427f07cacSKuninori Morimoto soc_cleanup_card_resources(card); 17852cc1afcfSKuninori Morimoto if (!unregister) 17862cc1afcfSKuninori Morimoto list_add(&card->list, &unbind_card_list); 17872cc1afcfSKuninori Morimoto } else { 17882cc1afcfSKuninori Morimoto if (unregister) 17892cc1afcfSKuninori Morimoto list_del(&card->list); 17902cc1afcfSKuninori Morimoto } 17912cc1afcfSKuninori Morimoto } 17922cc1afcfSKuninori Morimoto 1793ed90c013SKuninori Morimoto static int snd_soc_bind_card(struct snd_soc_card *card) 1794f0fba2adSLiam Girdwood { 17951a497983SMengdong Lin struct snd_soc_pcm_runtime *rtd; 179676c39e86SKuninori Morimoto struct snd_soc_component *component; 179761b0088bSMengdong Lin struct snd_soc_dai_link *dai_link; 179827f07cacSKuninori Morimoto int ret, i; 179996dd3622SMark Brown 180034e81ab4SLars-Peter Clausen mutex_lock(&client_mutex); 180101b9d99aSLiam Girdwood mutex_lock_nested(&card->mutex, SND_SOC_CARD_CLASS_INIT); 1802f0fba2adSLiam Girdwood 180395c267ddSKuninori Morimoto snd_soc_dapm_init(&card->dapm, card, NULL); 180453e947a0SKuninori Morimoto 1805a655de80SLiam Girdwood /* check whether any platform is ignore machine FE and using topology */ 1806a655de80SLiam Girdwood soc_check_tplg_fes(card); 1807a655de80SLiam Girdwood 180844c69bb1SLars-Peter Clausen /* bind aux_devs too */ 1809bee886f1SKuninori Morimoto ret = soc_bind_aux_dev(card); 1810bee886f1SKuninori Morimoto if (ret < 0) 181153e947a0SKuninori Morimoto goto probe_end; 1812db2a4165SFrank Mandarino 1813f8f80361SMengdong Lin /* add predefined DAI links to the list */ 1814d8145989SKuninori Morimoto card->num_rtd = 0; 18155b99a0aaSKuninori Morimoto for_each_card_prelinks(card, i, dai_link) { 18160c048004SKuninori Morimoto ret = snd_soc_add_pcm_runtime(card, dai_link); 18175b99a0aaSKuninori Morimoto if (ret < 0) 18185b99a0aaSKuninori Morimoto goto probe_end; 18195b99a0aaSKuninori Morimoto } 1820f8f80361SMengdong Lin 1821f0fba2adSLiam Girdwood /* card bind complete so register a sound card */ 1822102b5a8dSTakashi Iwai ret = snd_card_new(card->dev, SNDRV_DEFAULT_IDX1, SNDRV_DEFAULT_STR1, 1823f0fba2adSLiam Girdwood card->owner, 0, &card->snd_card); 1824f0fba2adSLiam Girdwood if (ret < 0) { 182510e8aa9aSMichał Mirosław dev_err(card->dev, 182610e8aa9aSMichał Mirosław "ASoC: can't create sound card for card %s: %d\n", 182710e8aa9aSMichał Mirosław card->name, ret); 182853e947a0SKuninori Morimoto goto probe_end; 1829db2a4165SFrank Mandarino } 1830db2a4165SFrank Mandarino 18310757d834SLars-Peter Clausen soc_init_card_debugfs(card); 18320757d834SLars-Peter Clausen 1833b3da4251SKuninori Morimoto soc_resume_init(card); 18346ed25978SAndy Green 1835b8ba3b57SKuninori Morimoto ret = snd_soc_dapm_new_controls(&card->dapm, card->dapm_widgets, 18369a841ebbSMark Brown card->num_dapm_widgets); 1837b8ba3b57SKuninori Morimoto if (ret < 0) 1838b8ba3b57SKuninori Morimoto goto probe_end; 18399a841ebbSMark Brown 1840b8ba3b57SKuninori Morimoto ret = snd_soc_dapm_new_controls(&card->dapm, card->of_dapm_widgets, 1841f23e860eSNicolin Chen card->num_of_dapm_widgets); 1842b8ba3b57SKuninori Morimoto if (ret < 0) 1843b8ba3b57SKuninori Morimoto goto probe_end; 1844f23e860eSNicolin Chen 1845f0fba2adSLiam Girdwood /* initialise the sound card only once */ 184673de4b02SKuninori Morimoto ret = snd_soc_card_probe(card); 1847f0fba2adSLiam Girdwood if (ret < 0) 184853e947a0SKuninori Morimoto goto probe_end; 1849f0fba2adSLiam Girdwood 185062ae68faSStephen Warren /* probe all components used by DAI links on this card */ 185162f07a6bSKuninori Morimoto ret = soc_probe_link_components(card); 185262ae68faSStephen Warren if (ret < 0) { 1853f110bfc7SLiam Girdwood dev_err(card->dev, 185462f07a6bSKuninori Morimoto "ASoC: failed to instantiate card %d\n", ret); 185553e947a0SKuninori Morimoto goto probe_end; 185662ae68faSStephen Warren } 185762ae68faSStephen Warren 1858f2ed6b07SMengdong Lin /* probe auxiliary components */ 1859f2ed6b07SMengdong Lin ret = soc_probe_aux_devices(card); 186074bd3f92SKuninori Morimoto if (ret < 0) { 186174bd3f92SKuninori Morimoto dev_err(card->dev, 186274bd3f92SKuninori Morimoto "ASoC: failed to probe aux component %d\n", ret); 186353e947a0SKuninori Morimoto goto probe_end; 186474bd3f92SKuninori Morimoto } 1865f2ed6b07SMengdong Lin 186662ae68faSStephen Warren /* probe all DAI links on this card */ 1867c7e73774SKuninori Morimoto ret = soc_probe_link_dais(card); 1868fe3e78e0SMark Brown if (ret < 0) { 1869f110bfc7SLiam Girdwood dev_err(card->dev, 1870c7e73774SKuninori Morimoto "ASoC: failed to instantiate card %d\n", ret); 187153e947a0SKuninori Morimoto goto probe_end; 1872fe3e78e0SMark Brown } 1873fe3e78e0SMark Brown 1874626c2e57SKuninori Morimoto for_each_card_rtds(card, rtd) { 1875eaffeefbSKuninori Morimoto ret = soc_init_pcm_runtime(card, rtd); 1876626c2e57SKuninori Morimoto if (ret < 0) 1877626c2e57SKuninori Morimoto goto probe_end; 1878626c2e57SKuninori Morimoto } 1879c4b46982SKuninori Morimoto 1880888df395SMark Brown snd_soc_dapm_link_dai_widgets(card); 1881b893ea5fSLiam Girdwood snd_soc_dapm_connect_dai_link_widgets(card); 1882888df395SMark Brown 18839b98c7c2SKuninori Morimoto ret = snd_soc_add_card_controls(card, card->controls, 18842c7b696aSMarcel Ziswiler card->num_controls); 18859b98c7c2SKuninori Morimoto if (ret < 0) 18869b98c7c2SKuninori Morimoto goto probe_end; 1887b7af1dafSMark Brown 1888daa480bdSKuninori Morimoto ret = snd_soc_dapm_add_routes(&card->dapm, card->dapm_routes, 1889b8ad29deSMark Brown card->num_dapm_routes); 1890a22ae72bSPierre-Louis Bossart if (ret < 0) { 1891a22ae72bSPierre-Louis Bossart if (card->disable_route_checks) { 1892a22ae72bSPierre-Louis Bossart dev_info(card->dev, 1893a22ae72bSPierre-Louis Bossart "%s: disable_route_checks set, ignoring errors on add_routes\n", 1894a22ae72bSPierre-Louis Bossart __func__); 1895a22ae72bSPierre-Louis Bossart } else { 1896a22ae72bSPierre-Louis Bossart dev_err(card->dev, 1897a22ae72bSPierre-Louis Bossart "%s: snd_soc_dapm_add_routes failed: %d\n", 1898a22ae72bSPierre-Louis Bossart __func__, ret); 1899daa480bdSKuninori Morimoto goto probe_end; 1900a22ae72bSPierre-Louis Bossart } 1901a22ae72bSPierre-Louis Bossart } 1902b8ad29deSMark Brown 1903daa480bdSKuninori Morimoto ret = snd_soc_dapm_add_routes(&card->dapm, card->of_dapm_routes, 1904f23e860eSNicolin Chen card->num_of_dapm_routes); 1905daa480bdSKuninori Morimoto if (ret < 0) 1906daa480bdSKuninori Morimoto goto probe_end; 190775d9ac46SMark Brown 1908861886d3STakashi Iwai /* try to set some sane longname if DMI is available */ 1909861886d3STakashi Iwai snd_soc_set_dmi_name(card, NULL); 1910861886d3STakashi Iwai 19110f23f718SKuninori Morimoto soc_setup_card_name(card->snd_card->shortname, 19120f23f718SKuninori Morimoto card->name, NULL, 0); 19130f23f718SKuninori Morimoto soc_setup_card_name(card->snd_card->longname, 19140f23f718SKuninori Morimoto card->long_name, card->name, 0); 19150f23f718SKuninori Morimoto soc_setup_card_name(card->snd_card->driver, 19160f23f718SKuninori Morimoto card->driver_name, card->name, 1); 1917fe3e78e0SMark Brown 1918dc73d73aSJaroslav Kysela if (card->components) { 1919dc73d73aSJaroslav Kysela /* the current implementation of snd_component_add() accepts */ 1920dc73d73aSJaroslav Kysela /* multiple components in the string separated by space, */ 1921dc73d73aSJaroslav Kysela /* but the string collision (identical string) check might */ 1922dc73d73aSJaroslav Kysela /* not work correctly */ 1923dc73d73aSJaroslav Kysela ret = snd_component_add(card->snd_card, card->components); 1924dc73d73aSJaroslav Kysela if (ret < 0) { 1925dc73d73aSJaroslav Kysela dev_err(card->dev, "ASoC: %s snd_component_add() failed: %d\n", 1926dc73d73aSJaroslav Kysela card->name, ret); 1927dc73d73aSJaroslav Kysela goto probe_end; 1928dc73d73aSJaroslav Kysela } 1929dc73d73aSJaroslav Kysela } 1930dc73d73aSJaroslav Kysela 19315c0eac03SKuninori Morimoto ret = snd_soc_card_late_probe(card); 19325c0eac03SKuninori Morimoto if (ret < 0) 193353e947a0SKuninori Morimoto goto probe_end; 193428e9ad92SMark Brown 1935824ef826SLars-Peter Clausen snd_soc_dapm_new_widgets(card); 19368c193b8dSLars-Peter Clausen 1937f0fba2adSLiam Girdwood ret = snd_card_register(card->snd_card); 1938fe3e78e0SMark Brown if (ret < 0) { 1939f110bfc7SLiam Girdwood dev_err(card->dev, "ASoC: failed to register soundcard %d\n", 1940f110bfc7SLiam Girdwood ret); 194153e947a0SKuninori Morimoto goto probe_end; 1942fe3e78e0SMark Brown } 1943fe3e78e0SMark Brown 1944435c5e25SMark Brown card->instantiated = 1; 1945882eab6cSTzung-Bi Shih dapm_mark_endpoints_dirty(card); 19464f4c0072SMark Brown snd_soc_dapm_sync(&card->dapm); 1947b19e6e7bSMark Brown 1948ed90c013SKuninori Morimoto /* deactivate pins to sleep state */ 194976c39e86SKuninori Morimoto for_each_card_components(card, component) 1950b3dea624SKuninori Morimoto if (!snd_soc_component_active(component)) 195176c39e86SKuninori Morimoto pinctrl_pm_select_sleep_state(component->dev); 1952ed90c013SKuninori Morimoto 195353e947a0SKuninori Morimoto probe_end: 195453e947a0SKuninori Morimoto if (ret < 0) 195527f07cacSKuninori Morimoto soc_cleanup_card_resources(card); 1956db2a4165SFrank Mandarino 1957f0fba2adSLiam Girdwood mutex_unlock(&card->mutex); 195834e81ab4SLars-Peter Clausen mutex_unlock(&client_mutex); 1959db2a4165SFrank Mandarino 1960b19e6e7bSMark Brown return ret; 1961435c5e25SMark Brown } 1962435c5e25SMark Brown 1963435c5e25SMark Brown /* probes a new socdev */ 1964435c5e25SMark Brown static int soc_probe(struct platform_device *pdev) 1965435c5e25SMark Brown { 1966f0fba2adSLiam Girdwood struct snd_soc_card *card = platform_get_drvdata(pdev); 1967435c5e25SMark Brown 196870a7ca34SVinod Koul /* 196970a7ca34SVinod Koul * no card, so machine driver should be registering card 197070a7ca34SVinod Koul * we should not be here in that case so ret error 197170a7ca34SVinod Koul */ 197270a7ca34SVinod Koul if (!card) 197370a7ca34SVinod Koul return -EINVAL; 197470a7ca34SVinod Koul 1975fe4085e8SMark Brown dev_warn(&pdev->dev, 1976f110bfc7SLiam Girdwood "ASoC: machine %s should use snd_soc_register_card()\n", 1977fe4085e8SMark Brown card->name); 1978fe4085e8SMark Brown 1979435c5e25SMark Brown /* Bodge while we unpick instantiation */ 1980435c5e25SMark Brown card->dev = &pdev->dev; 1981f0fba2adSLiam Girdwood 198228d528c8SMark Brown return snd_soc_register_card(card); 1983435c5e25SMark Brown } 1984435c5e25SMark Brown 1985b0e26485SVinod Koul /* removes a socdev */ 1986b0e26485SVinod Koul static int soc_remove(struct platform_device *pdev) 1987b0e26485SVinod Koul { 1988b0e26485SVinod Koul struct snd_soc_card *card = platform_get_drvdata(pdev); 1989b0e26485SVinod Koul 1990c5af3a2eSMark Brown snd_soc_unregister_card(card); 1991db2a4165SFrank Mandarino return 0; 1992db2a4165SFrank Mandarino } 1993db2a4165SFrank Mandarino 19946f8ab4acSMark Brown int snd_soc_poweroff(struct device *dev) 199551737470SMark Brown { 19966f8ab4acSMark Brown struct snd_soc_card *card = dev_get_drvdata(dev); 199776c39e86SKuninori Morimoto struct snd_soc_component *component; 199851737470SMark Brown 199951737470SMark Brown if (!card->instantiated) 2000416356fcSMark Brown return 0; 200151737470SMark Brown 20022c7b696aSMarcel Ziswiler /* 20032c7b696aSMarcel Ziswiler * Flush out pmdown_time work - we actually do want to run it 20042c7b696aSMarcel Ziswiler * now, we're shutting down so no imminent restart. 20052c7b696aSMarcel Ziswiler */ 200665462e44SKuninori Morimoto snd_soc_flush_all_delayed_work(card); 200751737470SMark Brown 2008f0fba2adSLiam Girdwood snd_soc_dapm_shutdown(card); 2009416356fcSMark Brown 2010988e8cc4SNicolin Chen /* deactivate pins to sleep state */ 201176c39e86SKuninori Morimoto for_each_card_components(card, component) 201276c39e86SKuninori Morimoto pinctrl_pm_select_sleep_state(component->dev); 2013988e8cc4SNicolin Chen 2014416356fcSMark Brown return 0; 201551737470SMark Brown } 20166f8ab4acSMark Brown EXPORT_SYMBOL_GPL(snd_soc_poweroff); 201751737470SMark Brown 20186f8ab4acSMark Brown const struct dev_pm_ops snd_soc_pm_ops = { 2019b1dd5897SViresh Kumar .suspend = snd_soc_suspend, 2020b1dd5897SViresh Kumar .resume = snd_soc_resume, 2021b1dd5897SViresh Kumar .freeze = snd_soc_suspend, 2022b1dd5897SViresh Kumar .thaw = snd_soc_resume, 20236f8ab4acSMark Brown .poweroff = snd_soc_poweroff, 2024b1dd5897SViresh Kumar .restore = snd_soc_resume, 2025416356fcSMark Brown }; 2026deb2607eSStephen Warren EXPORT_SYMBOL_GPL(snd_soc_pm_ops); 2027416356fcSMark Brown 2028db2a4165SFrank Mandarino /* ASoC platform driver */ 2029db2a4165SFrank Mandarino static struct platform_driver soc_driver = { 2030db2a4165SFrank Mandarino .driver = { 2031db2a4165SFrank Mandarino .name = "soc-audio", 20326f8ab4acSMark Brown .pm = &snd_soc_pm_ops, 2033db2a4165SFrank Mandarino }, 2034db2a4165SFrank Mandarino .probe = soc_probe, 2035db2a4165SFrank Mandarino .remove = soc_remove, 2036db2a4165SFrank Mandarino }; 2037db2a4165SFrank Mandarino 2038096e49d5SMark Brown /** 2039db2a4165SFrank Mandarino * snd_soc_cnew - create new control 2040db2a4165SFrank Mandarino * @_template: control template 2041db2a4165SFrank Mandarino * @data: control private data 2042ac11a2b3SMark Brown * @long_name: control long name 2043efb7ac3fSMark Brown * @prefix: control name prefix 2044db2a4165SFrank Mandarino * 2045db2a4165SFrank Mandarino * Create a new mixer control from a template control. 2046db2a4165SFrank Mandarino * 2047db2a4165SFrank Mandarino * Returns 0 for success, else error. 2048db2a4165SFrank Mandarino */ 2049db2a4165SFrank Mandarino struct snd_kcontrol *snd_soc_cnew(const struct snd_kcontrol_new *_template, 20503056557fSMark Brown void *data, const char *long_name, 2051efb7ac3fSMark Brown const char *prefix) 2052db2a4165SFrank Mandarino { 2053db2a4165SFrank Mandarino struct snd_kcontrol_new template; 2054efb7ac3fSMark Brown struct snd_kcontrol *kcontrol; 2055efb7ac3fSMark Brown char *name = NULL; 2056db2a4165SFrank Mandarino 2057db2a4165SFrank Mandarino memcpy(&template, _template, sizeof(template)); 2058db2a4165SFrank Mandarino template.index = 0; 2059db2a4165SFrank Mandarino 2060efb7ac3fSMark Brown if (!long_name) 2061efb7ac3fSMark Brown long_name = template.name; 2062efb7ac3fSMark Brown 2063efb7ac3fSMark Brown if (prefix) { 20642b581074SLars-Peter Clausen name = kasprintf(GFP_KERNEL, "%s %s", prefix, long_name); 2065efb7ac3fSMark Brown if (!name) 2066efb7ac3fSMark Brown return NULL; 2067efb7ac3fSMark Brown 2068efb7ac3fSMark Brown template.name = name; 2069efb7ac3fSMark Brown } else { 2070efb7ac3fSMark Brown template.name = long_name; 2071efb7ac3fSMark Brown } 2072efb7ac3fSMark Brown 2073efb7ac3fSMark Brown kcontrol = snd_ctl_new1(&template, data); 2074efb7ac3fSMark Brown 2075efb7ac3fSMark Brown kfree(name); 2076efb7ac3fSMark Brown 2077efb7ac3fSMark Brown return kcontrol; 2078db2a4165SFrank Mandarino } 2079db2a4165SFrank Mandarino EXPORT_SYMBOL_GPL(snd_soc_cnew); 2080db2a4165SFrank Mandarino 2081022658beSLiam Girdwood static int snd_soc_add_controls(struct snd_card *card, struct device *dev, 2082022658beSLiam Girdwood const struct snd_kcontrol_new *controls, int num_controls, 2083022658beSLiam Girdwood const char *prefix, void *data) 2084022658beSLiam Girdwood { 2085022658beSLiam Girdwood int err, i; 2086022658beSLiam Girdwood 2087022658beSLiam Girdwood for (i = 0; i < num_controls; i++) { 2088022658beSLiam Girdwood const struct snd_kcontrol_new *control = &controls[i]; 20892c7b696aSMarcel Ziswiler 2090022658beSLiam Girdwood err = snd_ctl_add(card, snd_soc_cnew(control, data, 2091022658beSLiam Girdwood control->name, prefix)); 2092022658beSLiam Girdwood if (err < 0) { 2093f110bfc7SLiam Girdwood dev_err(dev, "ASoC: Failed to add %s: %d\n", 2094f110bfc7SLiam Girdwood control->name, err); 2095022658beSLiam Girdwood return err; 2096022658beSLiam Girdwood } 2097022658beSLiam Girdwood } 2098022658beSLiam Girdwood 2099022658beSLiam Girdwood return 0; 2100022658beSLiam Girdwood } 2101022658beSLiam Girdwood 2102db2a4165SFrank Mandarino /** 21030f2780adSLars-Peter Clausen * snd_soc_add_component_controls - Add an array of controls to a component. 21040f2780adSLars-Peter Clausen * 21050f2780adSLars-Peter Clausen * @component: Component to add controls to 21060f2780adSLars-Peter Clausen * @controls: Array of controls to add 21070f2780adSLars-Peter Clausen * @num_controls: Number of elements in the array 21080f2780adSLars-Peter Clausen * 21090f2780adSLars-Peter Clausen * Return: 0 for success, else error. 21100f2780adSLars-Peter Clausen */ 21110f2780adSLars-Peter Clausen int snd_soc_add_component_controls(struct snd_soc_component *component, 21120f2780adSLars-Peter Clausen const struct snd_kcontrol_new *controls, unsigned int num_controls) 21130f2780adSLars-Peter Clausen { 21140f2780adSLars-Peter Clausen struct snd_card *card = component->card->snd_card; 21150f2780adSLars-Peter Clausen 21160f2780adSLars-Peter Clausen return snd_soc_add_controls(card, component->dev, controls, 21170f2780adSLars-Peter Clausen num_controls, component->name_prefix, component); 21180f2780adSLars-Peter Clausen } 21190f2780adSLars-Peter Clausen EXPORT_SYMBOL_GPL(snd_soc_add_component_controls); 21200f2780adSLars-Peter Clausen 21210f2780adSLars-Peter Clausen /** 2122022658beSLiam Girdwood * snd_soc_add_card_controls - add an array of controls to a SoC card. 2123022658beSLiam Girdwood * Convenience function to add a list of controls. 2124022658beSLiam Girdwood * 2125022658beSLiam Girdwood * @soc_card: SoC card to add controls to 2126022658beSLiam Girdwood * @controls: array of controls to add 2127022658beSLiam Girdwood * @num_controls: number of elements in the array 2128022658beSLiam Girdwood * 2129022658beSLiam Girdwood * Return 0 for success, else error. 2130022658beSLiam Girdwood */ 2131022658beSLiam Girdwood int snd_soc_add_card_controls(struct snd_soc_card *soc_card, 2132022658beSLiam Girdwood const struct snd_kcontrol_new *controls, int num_controls) 2133022658beSLiam Girdwood { 2134022658beSLiam Girdwood struct snd_card *card = soc_card->snd_card; 2135022658beSLiam Girdwood 2136022658beSLiam Girdwood return snd_soc_add_controls(card, soc_card->dev, controls, num_controls, 2137022658beSLiam Girdwood NULL, soc_card); 2138022658beSLiam Girdwood } 2139022658beSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_add_card_controls); 2140022658beSLiam Girdwood 2141022658beSLiam Girdwood /** 2142022658beSLiam Girdwood * snd_soc_add_dai_controls - add an array of controls to a DAI. 2143022658beSLiam Girdwood * Convienience function to add a list of controls. 2144022658beSLiam Girdwood * 2145022658beSLiam Girdwood * @dai: DAI to add controls to 2146022658beSLiam Girdwood * @controls: array of controls to add 2147022658beSLiam Girdwood * @num_controls: number of elements in the array 2148022658beSLiam Girdwood * 2149022658beSLiam Girdwood * Return 0 for success, else error. 2150022658beSLiam Girdwood */ 2151022658beSLiam Girdwood int snd_soc_add_dai_controls(struct snd_soc_dai *dai, 2152022658beSLiam Girdwood const struct snd_kcontrol_new *controls, int num_controls) 2153022658beSLiam Girdwood { 2154313665b9SLars-Peter Clausen struct snd_card *card = dai->component->card->snd_card; 2155022658beSLiam Girdwood 2156022658beSLiam Girdwood return snd_soc_add_controls(card, dai->dev, controls, num_controls, 2157022658beSLiam Girdwood NULL, dai); 2158022658beSLiam Girdwood } 2159022658beSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_add_dai_controls); 2160022658beSLiam Girdwood 2161c5af3a2eSMark Brown /** 2162c5af3a2eSMark Brown * snd_soc_register_card - Register a card with the ASoC core 2163c5af3a2eSMark Brown * 2164ac11a2b3SMark Brown * @card: Card to register 2165c5af3a2eSMark Brown * 2166c5af3a2eSMark Brown */ 216770a7ca34SVinod Koul int snd_soc_register_card(struct snd_soc_card *card) 2168c5af3a2eSMark Brown { 2169c5af3a2eSMark Brown if (!card->name || !card->dev) 2170c5af3a2eSMark Brown return -EINVAL; 2171c5af3a2eSMark Brown 2172ed77cc12SMark Brown dev_set_drvdata(card->dev, card); 2173ed77cc12SMark Brown 2174b03bfaecSKuninori Morimoto INIT_LIST_HEAD(&card->widgets); 2175b03bfaecSKuninori Morimoto INIT_LIST_HEAD(&card->paths); 2176b03bfaecSKuninori Morimoto INIT_LIST_HEAD(&card->dapm_list); 2177b03bfaecSKuninori Morimoto INIT_LIST_HEAD(&card->aux_comp_list); 2178b03bfaecSKuninori Morimoto INIT_LIST_HEAD(&card->component_dev_list); 2179b03bfaecSKuninori Morimoto INIT_LIST_HEAD(&card->list); 21801a497983SMengdong Lin INIT_LIST_HEAD(&card->rtd_list); 2181db432b41SMark Brown INIT_LIST_HEAD(&card->dapm_dirty); 21828a978234SLiam Girdwood INIT_LIST_HEAD(&card->dobj_list); 2183b03bfaecSKuninori Morimoto 2184c5af3a2eSMark Brown card->instantiated = 0; 2185f0fba2adSLiam Girdwood mutex_init(&card->mutex); 2186a73fb2dfSLiam Girdwood mutex_init(&card->dapm_mutex); 218772b745e3SPeter Ujfalusi mutex_init(&card->pcm_mutex); 2188a9764869SKaiChieh Chuang spin_lock_init(&card->dpcm_lock); 2189c5af3a2eSMark Brown 2190e894efefSSrinivas Kandagatla return snd_soc_bind_card(card); 2191c5af3a2eSMark Brown } 219270a7ca34SVinod Koul EXPORT_SYMBOL_GPL(snd_soc_register_card); 2193c5af3a2eSMark Brown 2194c5af3a2eSMark Brown /** 2195c5af3a2eSMark Brown * snd_soc_unregister_card - Unregister a card with the ASoC core 2196c5af3a2eSMark Brown * 2197ac11a2b3SMark Brown * @card: Card to unregister 2198c5af3a2eSMark Brown * 2199c5af3a2eSMark Brown */ 220070a7ca34SVinod Koul int snd_soc_unregister_card(struct snd_soc_card *card) 2201c5af3a2eSMark Brown { 2202b545542aSKuninori Morimoto mutex_lock(&client_mutex); 2203e894efefSSrinivas Kandagatla snd_soc_unbind_card(card, true); 2204b545542aSKuninori Morimoto mutex_unlock(&client_mutex); 2205f110bfc7SLiam Girdwood dev_dbg(card->dev, "ASoC: Unregistered card '%s'\n", card->name); 2206c5af3a2eSMark Brown 2207c5af3a2eSMark Brown return 0; 2208c5af3a2eSMark Brown } 220970a7ca34SVinod Koul EXPORT_SYMBOL_GPL(snd_soc_unregister_card); 2210c5af3a2eSMark Brown 2211f0fba2adSLiam Girdwood /* 2212f0fba2adSLiam Girdwood * Simplify DAI link configuration by removing ".-1" from device names 2213f0fba2adSLiam Girdwood * and sanitizing names. 2214f0fba2adSLiam Girdwood */ 22150b9a214aSDimitris Papastamos static char *fmt_single_name(struct device *dev, int *id) 2216f0fba2adSLiam Girdwood { 2217f0fba2adSLiam Girdwood char *found, name[NAME_SIZE]; 2218f0fba2adSLiam Girdwood int id1, id2; 2219f0fba2adSLiam Girdwood 2220f0fba2adSLiam Girdwood if (dev_name(dev) == NULL) 2221f0fba2adSLiam Girdwood return NULL; 2222f0fba2adSLiam Girdwood 222358818a77SDimitris Papastamos strlcpy(name, dev_name(dev), NAME_SIZE); 2224f0fba2adSLiam Girdwood 2225f0fba2adSLiam Girdwood /* are we a "%s.%d" name (platform and SPI components) */ 2226f0fba2adSLiam Girdwood found = strstr(name, dev->driver->name); 2227f0fba2adSLiam Girdwood if (found) { 2228f0fba2adSLiam Girdwood /* get ID */ 2229f0fba2adSLiam Girdwood if (sscanf(&found[strlen(dev->driver->name)], ".%d", id) == 1) { 2230f0fba2adSLiam Girdwood 2231f0fba2adSLiam Girdwood /* discard ID from name if ID == -1 */ 2232f0fba2adSLiam Girdwood if (*id == -1) 2233f0fba2adSLiam Girdwood found[strlen(dev->driver->name)] = '\0'; 2234f0fba2adSLiam Girdwood } 2235f0fba2adSLiam Girdwood 2236f0fba2adSLiam Girdwood } else { 2237f0fba2adSLiam Girdwood /* I2C component devices are named "bus-addr" */ 2238f0fba2adSLiam Girdwood if (sscanf(name, "%x-%x", &id1, &id2) == 2) { 2239f0fba2adSLiam Girdwood char tmp[NAME_SIZE]; 2240f0fba2adSLiam Girdwood 2241f0fba2adSLiam Girdwood /* create unique ID number from I2C addr and bus */ 224205899446SJarkko Nikula *id = ((id1 & 0xffff) << 16) + id2; 2243f0fba2adSLiam Girdwood 2244f0fba2adSLiam Girdwood /* sanitize component name for DAI link creation */ 22452c7b696aSMarcel Ziswiler snprintf(tmp, NAME_SIZE, "%s.%s", dev->driver->name, 22462c7b696aSMarcel Ziswiler name); 224758818a77SDimitris Papastamos strlcpy(name, tmp, NAME_SIZE); 2248f0fba2adSLiam Girdwood } else 2249f0fba2adSLiam Girdwood *id = 0; 2250f0fba2adSLiam Girdwood } 2251f0fba2adSLiam Girdwood 225250014499SKuninori Morimoto return devm_kstrdup(dev, name, GFP_KERNEL); 2253f0fba2adSLiam Girdwood } 2254f0fba2adSLiam Girdwood 2255f0fba2adSLiam Girdwood /* 2256f0fba2adSLiam Girdwood * Simplify DAI link naming for single devices with multiple DAIs by removing 2257f0fba2adSLiam Girdwood * any ".-1" and using the DAI name (instead of device name). 2258f0fba2adSLiam Girdwood */ 2259f0fba2adSLiam Girdwood static inline char *fmt_multiple_name(struct device *dev, 2260f0fba2adSLiam Girdwood struct snd_soc_dai_driver *dai_drv) 2261f0fba2adSLiam Girdwood { 2262f0fba2adSLiam Girdwood if (dai_drv->name == NULL) { 226310e8aa9aSMichał Mirosław dev_err(dev, 226410e8aa9aSMichał Mirosław "ASoC: error - multiple DAI %s registered with no name\n", 226510e8aa9aSMichał Mirosław dev_name(dev)); 2266f0fba2adSLiam Girdwood return NULL; 2267f0fba2adSLiam Girdwood } 2268f0fba2adSLiam Girdwood 226950014499SKuninori Morimoto return devm_kstrdup(dev, dai_drv->name, GFP_KERNEL); 2270f0fba2adSLiam Girdwood } 2271f0fba2adSLiam Girdwood 2272ffdbca0bSKuninori Morimoto void snd_soc_unregister_dai(struct snd_soc_dai *dai) 2273e11381f3SKuninori Morimoto { 2274e11381f3SKuninori Morimoto dev_dbg(dai->dev, "ASoC: Unregistered DAI '%s'\n", dai->name); 2275e11381f3SKuninori Morimoto list_del(&dai->list); 2276e11381f3SKuninori Morimoto } 2277ffdbca0bSKuninori Morimoto EXPORT_SYMBOL_GPL(snd_soc_unregister_dai); 2278e11381f3SKuninori Morimoto 22797ca24386SKuninori Morimoto /** 22807ca24386SKuninori Morimoto * snd_soc_register_dai - Register a DAI dynamically & create its widgets 22817ca24386SKuninori Morimoto * 22827ca24386SKuninori Morimoto * @component: The component the DAIs are registered for 22837ca24386SKuninori Morimoto * @dai_drv: DAI driver to use for the DAI 2284bc9a6655SRandy Dunlap * @legacy_dai_naming: if %true, use legacy single-name format; 2285bc9a6655SRandy Dunlap * if %false, use multiple-name format; 22867ca24386SKuninori Morimoto * 22877ca24386SKuninori Morimoto * Topology can use this API to register DAIs when probing a component. 22887ca24386SKuninori Morimoto * These DAIs's widgets will be freed in the card cleanup and the DAIs 22897ca24386SKuninori Morimoto * will be freed in the component cleanup. 22907ca24386SKuninori Morimoto */ 22917ca24386SKuninori Morimoto struct snd_soc_dai *snd_soc_register_dai(struct snd_soc_component *component, 22925e4fb372SMengdong Lin struct snd_soc_dai_driver *dai_drv, 22935e4fb372SMengdong Lin bool legacy_dai_naming) 22945e4fb372SMengdong Lin { 22955e4fb372SMengdong Lin struct device *dev = component->dev; 22965e4fb372SMengdong Lin struct snd_soc_dai *dai; 22975e4fb372SMengdong Lin 22985e4fb372SMengdong Lin dev_dbg(dev, "ASoC: dynamically register DAI %s\n", dev_name(dev)); 22995e4fb372SMengdong Lin 23007ca24386SKuninori Morimoto lockdep_assert_held(&client_mutex); 23017ca24386SKuninori Morimoto 230250014499SKuninori Morimoto dai = devm_kzalloc(dev, sizeof(*dai), GFP_KERNEL); 23035e4fb372SMengdong Lin if (dai == NULL) 23045e4fb372SMengdong Lin return NULL; 23055e4fb372SMengdong Lin 23065e4fb372SMengdong Lin /* 23075e4fb372SMengdong Lin * Back in the old days when we still had component-less DAIs, 23085e4fb372SMengdong Lin * instead of having a static name, component-less DAIs would 23095e4fb372SMengdong Lin * inherit the name of the parent device so it is possible to 23105e4fb372SMengdong Lin * register multiple instances of the DAI. We still need to keep 23115e4fb372SMengdong Lin * the same naming style even though those DAIs are not 23125e4fb372SMengdong Lin * component-less anymore. 23135e4fb372SMengdong Lin */ 23145e4fb372SMengdong Lin if (legacy_dai_naming && 23155e4fb372SMengdong Lin (dai_drv->id == 0 || dai_drv->name == NULL)) { 23165e4fb372SMengdong Lin dai->name = fmt_single_name(dev, &dai->id); 23175e4fb372SMengdong Lin } else { 23185e4fb372SMengdong Lin dai->name = fmt_multiple_name(dev, dai_drv); 23195e4fb372SMengdong Lin if (dai_drv->id) 23205e4fb372SMengdong Lin dai->id = dai_drv->id; 23215e4fb372SMengdong Lin else 23225e4fb372SMengdong Lin dai->id = component->num_dai; 23235e4fb372SMengdong Lin } 232450014499SKuninori Morimoto if (!dai->name) 23255e4fb372SMengdong Lin return NULL; 23265e4fb372SMengdong Lin 23275e4fb372SMengdong Lin dai->component = component; 23285e4fb372SMengdong Lin dai->dev = dev; 23295e4fb372SMengdong Lin dai->driver = dai_drv; 23305e4fb372SMengdong Lin 233115a0c645SKuninori Morimoto /* see for_each_component_dais */ 233258bf4179SKuninori Morimoto list_add_tail(&dai->list, &component->dai_list); 23335e4fb372SMengdong Lin component->num_dai++; 23345e4fb372SMengdong Lin 23355e4fb372SMengdong Lin dev_dbg(dev, "ASoC: Registered DAI '%s'\n", dai->name); 23365e4fb372SMengdong Lin return dai; 23375e4fb372SMengdong Lin } 2338e11381f3SKuninori Morimoto 23399115171aSMark Brown /** 23403f6674aeSKuninori Morimoto * snd_soc_unregister_dai - Unregister DAIs from the ASoC core 23413f6674aeSKuninori Morimoto * 23423f6674aeSKuninori Morimoto * @component: The component for which the DAIs should be unregistered 23433f6674aeSKuninori Morimoto */ 23443f6674aeSKuninori Morimoto static void snd_soc_unregister_dais(struct snd_soc_component *component) 23453f6674aeSKuninori Morimoto { 23463f6674aeSKuninori Morimoto struct snd_soc_dai *dai, *_dai; 23473f6674aeSKuninori Morimoto 2348e11381f3SKuninori Morimoto for_each_component_dais_safe(component, dai, _dai) 2349e11381f3SKuninori Morimoto snd_soc_unregister_dai(dai); 23503f6674aeSKuninori Morimoto } 23513f6674aeSKuninori Morimoto 23523f6674aeSKuninori Morimoto /** 2353daf77373SKuninori Morimoto * snd_soc_register_dais - Register a DAI with the ASoC core 2354daf77373SKuninori Morimoto * 2355daf77373SKuninori Morimoto * @component: The component the DAIs are registered for 2356daf77373SKuninori Morimoto * @dai_drv: DAI driver to use for the DAIs 2357daf77373SKuninori Morimoto * @count: Number of DAIs 2358daf77373SKuninori Morimoto */ 2359daf77373SKuninori Morimoto static int snd_soc_register_dais(struct snd_soc_component *component, 2360daf77373SKuninori Morimoto struct snd_soc_dai_driver *dai_drv, 2361daf77373SKuninori Morimoto size_t count) 2362daf77373SKuninori Morimoto { 2363daf77373SKuninori Morimoto struct snd_soc_dai *dai; 2364daf77373SKuninori Morimoto unsigned int i; 2365daf77373SKuninori Morimoto int ret; 2366daf77373SKuninori Morimoto 2367daf77373SKuninori Morimoto for (i = 0; i < count; i++) { 236871cb85f5SKuninori Morimoto dai = snd_soc_register_dai(component, dai_drv + i, count == 1 && 2369daf77373SKuninori Morimoto !component->driver->non_legacy_dai_naming); 2370daf77373SKuninori Morimoto if (dai == NULL) { 2371daf77373SKuninori Morimoto ret = -ENOMEM; 2372daf77373SKuninori Morimoto goto err; 2373daf77373SKuninori Morimoto } 2374daf77373SKuninori Morimoto } 2375daf77373SKuninori Morimoto 2376daf77373SKuninori Morimoto return 0; 2377daf77373SKuninori Morimoto 2378daf77373SKuninori Morimoto err: 2379daf77373SKuninori Morimoto snd_soc_unregister_dais(component); 2380daf77373SKuninori Morimoto 2381daf77373SKuninori Morimoto return ret; 2382daf77373SKuninori Morimoto } 2383daf77373SKuninori Morimoto 2384bb13109dSLars-Peter Clausen static int snd_soc_component_initialize(struct snd_soc_component *component, 2385bb13109dSLars-Peter Clausen const struct snd_soc_component_driver *driver, struct device *dev) 2386d191bd8dSKuninori Morimoto { 23878d92bb51SKuninori Morimoto INIT_LIST_HEAD(&component->dai_list); 2388495efdb0SKuninori Morimoto INIT_LIST_HEAD(&component->dobj_list); 2389495efdb0SKuninori Morimoto INIT_LIST_HEAD(&component->card_list); 23908d92bb51SKuninori Morimoto mutex_init(&component->io_mutex); 23918d92bb51SKuninori Morimoto 23926b62fa95SJerome Brunet component->name = fmt_single_name(dev, &component->id); 2393bb13109dSLars-Peter Clausen if (!component->name) { 2394bb13109dSLars-Peter Clausen dev_err(dev, "ASoC: Failed to allocate name\n"); 2395d191bd8dSKuninori Morimoto return -ENOMEM; 2396d191bd8dSKuninori Morimoto } 2397d191bd8dSKuninori Morimoto 2398bb13109dSLars-Peter Clausen component->dev = dev; 2399bb13109dSLars-Peter Clausen component->driver = driver; 2400e2c330b9SLars-Peter Clausen 2401bb13109dSLars-Peter Clausen return 0; 2402d191bd8dSKuninori Morimoto } 2403d191bd8dSKuninori Morimoto 240420feb881SLars-Peter Clausen static void snd_soc_component_setup_regmap(struct snd_soc_component *component) 2405886f5692SLars-Peter Clausen { 2406886f5692SLars-Peter Clausen int val_bytes = regmap_get_val_bytes(component->regmap); 240720feb881SLars-Peter Clausen 2408886f5692SLars-Peter Clausen /* Errors are legitimate for non-integer byte multiples */ 2409886f5692SLars-Peter Clausen if (val_bytes > 0) 2410886f5692SLars-Peter Clausen component->val_bytes = val_bytes; 2411886f5692SLars-Peter Clausen } 241220feb881SLars-Peter Clausen 2413e874bf5fSLars-Peter Clausen #ifdef CONFIG_REGMAP 2414e874bf5fSLars-Peter Clausen 241520feb881SLars-Peter Clausen /** 24162c7b696aSMarcel Ziswiler * snd_soc_component_init_regmap() - Initialize regmap instance for the 24172c7b696aSMarcel Ziswiler * component 241820feb881SLars-Peter Clausen * @component: The component for which to initialize the regmap instance 241920feb881SLars-Peter Clausen * @regmap: The regmap instance that should be used by the component 242020feb881SLars-Peter Clausen * 242120feb881SLars-Peter Clausen * This function allows deferred assignment of the regmap instance that is 242220feb881SLars-Peter Clausen * associated with the component. Only use this if the regmap instance is not 242320feb881SLars-Peter Clausen * yet ready when the component is registered. The function must also be called 242420feb881SLars-Peter Clausen * before the first IO attempt of the component. 242520feb881SLars-Peter Clausen */ 242620feb881SLars-Peter Clausen void snd_soc_component_init_regmap(struct snd_soc_component *component, 242720feb881SLars-Peter Clausen struct regmap *regmap) 242820feb881SLars-Peter Clausen { 242920feb881SLars-Peter Clausen component->regmap = regmap; 243020feb881SLars-Peter Clausen snd_soc_component_setup_regmap(component); 2431886f5692SLars-Peter Clausen } 243220feb881SLars-Peter Clausen EXPORT_SYMBOL_GPL(snd_soc_component_init_regmap); 243320feb881SLars-Peter Clausen 243420feb881SLars-Peter Clausen /** 24352c7b696aSMarcel Ziswiler * snd_soc_component_exit_regmap() - De-initialize regmap instance for the 24362c7b696aSMarcel Ziswiler * component 243720feb881SLars-Peter Clausen * @component: The component for which to de-initialize the regmap instance 243820feb881SLars-Peter Clausen * 243920feb881SLars-Peter Clausen * Calls regmap_exit() on the regmap instance associated to the component and 244020feb881SLars-Peter Clausen * removes the regmap instance from the component. 244120feb881SLars-Peter Clausen * 244220feb881SLars-Peter Clausen * This function should only be used if snd_soc_component_init_regmap() was used 244320feb881SLars-Peter Clausen * to initialize the regmap instance. 244420feb881SLars-Peter Clausen */ 244520feb881SLars-Peter Clausen void snd_soc_component_exit_regmap(struct snd_soc_component *component) 244620feb881SLars-Peter Clausen { 244720feb881SLars-Peter Clausen regmap_exit(component->regmap); 244820feb881SLars-Peter Clausen component->regmap = NULL; 244920feb881SLars-Peter Clausen } 245020feb881SLars-Peter Clausen EXPORT_SYMBOL_GPL(snd_soc_component_exit_regmap); 2451886f5692SLars-Peter Clausen 2452e874bf5fSLars-Peter Clausen #endif 2453d191bd8dSKuninori Morimoto 2454273d778eSKuninori Morimoto #define ENDIANNESS_MAP(name) \ 2455273d778eSKuninori Morimoto (SNDRV_PCM_FMTBIT_##name##LE | SNDRV_PCM_FMTBIT_##name##BE) 2456273d778eSKuninori Morimoto static u64 endianness_format_map[] = { 2457273d778eSKuninori Morimoto ENDIANNESS_MAP(S16_), 2458273d778eSKuninori Morimoto ENDIANNESS_MAP(U16_), 2459273d778eSKuninori Morimoto ENDIANNESS_MAP(S24_), 2460273d778eSKuninori Morimoto ENDIANNESS_MAP(U24_), 2461273d778eSKuninori Morimoto ENDIANNESS_MAP(S32_), 2462273d778eSKuninori Morimoto ENDIANNESS_MAP(U32_), 2463273d778eSKuninori Morimoto ENDIANNESS_MAP(S24_3), 2464273d778eSKuninori Morimoto ENDIANNESS_MAP(U24_3), 2465273d778eSKuninori Morimoto ENDIANNESS_MAP(S20_3), 2466273d778eSKuninori Morimoto ENDIANNESS_MAP(U20_3), 2467273d778eSKuninori Morimoto ENDIANNESS_MAP(S18_3), 2468273d778eSKuninori Morimoto ENDIANNESS_MAP(U18_3), 2469273d778eSKuninori Morimoto ENDIANNESS_MAP(FLOAT_), 2470273d778eSKuninori Morimoto ENDIANNESS_MAP(FLOAT64_), 2471273d778eSKuninori Morimoto ENDIANNESS_MAP(IEC958_SUBFRAME_), 2472273d778eSKuninori Morimoto }; 2473273d778eSKuninori Morimoto 2474273d778eSKuninori Morimoto /* 2475273d778eSKuninori Morimoto * Fix up the DAI formats for endianness: codecs don't actually see 2476273d778eSKuninori Morimoto * the endianness of the data but we're using the CPU format 2477273d778eSKuninori Morimoto * definitions which do need to include endianness so we ensure that 2478273d778eSKuninori Morimoto * codec DAIs always have both big and little endian variants set. 2479273d778eSKuninori Morimoto */ 2480273d778eSKuninori Morimoto static void convert_endianness_formats(struct snd_soc_pcm_stream *stream) 2481273d778eSKuninori Morimoto { 2482273d778eSKuninori Morimoto int i; 2483273d778eSKuninori Morimoto 2484273d778eSKuninori Morimoto for (i = 0; i < ARRAY_SIZE(endianness_format_map); i++) 2485273d778eSKuninori Morimoto if (stream->formats & endianness_format_map[i]) 2486273d778eSKuninori Morimoto stream->formats |= endianness_format_map[i]; 2487273d778eSKuninori Morimoto } 2488273d778eSKuninori Morimoto 2489e894efefSSrinivas Kandagatla static void snd_soc_try_rebind_card(void) 2490e894efefSSrinivas Kandagatla { 2491e894efefSSrinivas Kandagatla struct snd_soc_card *card, *c; 2492e894efefSSrinivas Kandagatla 2493b245d273SKuninori Morimoto list_for_each_entry_safe(card, c, &unbind_card_list, list) 2494e894efefSSrinivas Kandagatla if (!snd_soc_bind_card(card)) 2495e894efefSSrinivas Kandagatla list_del(&card->list); 2496e894efefSSrinivas Kandagatla } 2497e894efefSSrinivas Kandagatla 2498486c7978SKuninori Morimoto static void snd_soc_del_component_unlocked(struct snd_soc_component *component) 2499486c7978SKuninori Morimoto { 2500b18768f5SKuninori Morimoto struct snd_soc_card *card = component->card; 2501b18768f5SKuninori Morimoto 2502486c7978SKuninori Morimoto snd_soc_unregister_dais(component); 2503b18768f5SKuninori Morimoto 2504b18768f5SKuninori Morimoto if (card) 2505b18768f5SKuninori Morimoto snd_soc_unbind_card(card, false); 2506b18768f5SKuninori Morimoto 2507b18768f5SKuninori Morimoto list_del(&component->list); 2508486c7978SKuninori Morimoto } 2509486c7978SKuninori Morimoto 2510e0dac41bSKuninori Morimoto int snd_soc_add_component(struct device *dev, 2511e0dac41bSKuninori Morimoto struct snd_soc_component *component, 2512cf9e829eSKuninori Morimoto const struct snd_soc_component_driver *component_driver, 2513d191bd8dSKuninori Morimoto struct snd_soc_dai_driver *dai_drv, 2514d191bd8dSKuninori Morimoto int num_dai) 2515d191bd8dSKuninori Morimoto { 2516bb13109dSLars-Peter Clausen int ret; 2517273d778eSKuninori Morimoto int i; 2518d191bd8dSKuninori Morimoto 2519b18768f5SKuninori Morimoto mutex_lock(&client_mutex); 2520b18768f5SKuninori Morimoto 2521cf9e829eSKuninori Morimoto ret = snd_soc_component_initialize(component, component_driver, dev); 2522bb13109dSLars-Peter Clausen if (ret) 2523bb13109dSLars-Peter Clausen goto err_free; 2524bb13109dSLars-Peter Clausen 2525273d778eSKuninori Morimoto if (component_driver->endianness) { 2526273d778eSKuninori Morimoto for (i = 0; i < num_dai; i++) { 2527273d778eSKuninori Morimoto convert_endianness_formats(&dai_drv[i].playback); 2528273d778eSKuninori Morimoto convert_endianness_formats(&dai_drv[i].capture); 2529273d778eSKuninori Morimoto } 2530273d778eSKuninori Morimoto } 2531273d778eSKuninori Morimoto 25320e7b25c6SKuninori Morimoto ret = snd_soc_register_dais(component, dai_drv, num_dai); 2533bb13109dSLars-Peter Clausen if (ret < 0) { 2534f42cf8d6SMasanari Iida dev_err(dev, "ASoC: Failed to register DAIs: %d\n", ret); 2535bb13109dSLars-Peter Clausen goto err_cleanup; 2536bb13109dSLars-Peter Clausen } 2537bb13109dSLars-Peter Clausen 2538b18768f5SKuninori Morimoto if (!component->driver->write && !component->driver->read) { 2539b18768f5SKuninori Morimoto if (!component->regmap) 2540b18768f5SKuninori Morimoto component->regmap = dev_get_regmap(component->dev, 2541b18768f5SKuninori Morimoto NULL); 2542b18768f5SKuninori Morimoto if (component->regmap) 2543b18768f5SKuninori Morimoto snd_soc_component_setup_regmap(component); 2544b18768f5SKuninori Morimoto } 2545bb13109dSLars-Peter Clausen 2546b18768f5SKuninori Morimoto /* see for_each_component */ 2547b18768f5SKuninori Morimoto list_add(&component->list, &component_list); 2548bb13109dSLars-Peter Clausen 2549bb13109dSLars-Peter Clausen err_cleanup: 2550b18768f5SKuninori Morimoto if (ret < 0) 2551486c7978SKuninori Morimoto snd_soc_del_component_unlocked(component); 2552bb13109dSLars-Peter Clausen err_free: 2553b18768f5SKuninori Morimoto mutex_unlock(&client_mutex); 2554b18768f5SKuninori Morimoto 2555b18768f5SKuninori Morimoto if (ret == 0) 2556b18768f5SKuninori Morimoto snd_soc_try_rebind_card(); 2557b18768f5SKuninori Morimoto 2558bb13109dSLars-Peter Clausen return ret; 2559d191bd8dSKuninori Morimoto } 2560e0dac41bSKuninori Morimoto EXPORT_SYMBOL_GPL(snd_soc_add_component); 2561e0dac41bSKuninori Morimoto 2562e0dac41bSKuninori Morimoto int snd_soc_register_component(struct device *dev, 2563e0dac41bSKuninori Morimoto const struct snd_soc_component_driver *component_driver, 2564e0dac41bSKuninori Morimoto struct snd_soc_dai_driver *dai_drv, 2565e0dac41bSKuninori Morimoto int num_dai) 2566e0dac41bSKuninori Morimoto { 2567e0dac41bSKuninori Morimoto struct snd_soc_component *component; 2568e0dac41bSKuninori Morimoto 25697ecbd6a9SKuninori Morimoto component = devm_kzalloc(dev, sizeof(*component), GFP_KERNEL); 257008e61d03SKuninori Morimoto if (!component) 2571e0dac41bSKuninori Morimoto return -ENOMEM; 2572e0dac41bSKuninori Morimoto 2573e0dac41bSKuninori Morimoto return snd_soc_add_component(dev, component, component_driver, 2574e0dac41bSKuninori Morimoto dai_drv, num_dai); 2575e0dac41bSKuninori Morimoto } 2576d191bd8dSKuninori Morimoto EXPORT_SYMBOL_GPL(snd_soc_register_component); 2577d191bd8dSKuninori Morimoto 2578d191bd8dSKuninori Morimoto /** 257958f30150SMaxime Ripard * snd_soc_unregister_component_by_driver - Unregister component using a given driver 258058f30150SMaxime Ripard * from the ASoC core 258158f30150SMaxime Ripard * 258258f30150SMaxime Ripard * @dev: The device to unregister 258358f30150SMaxime Ripard * @component_driver: The component driver to unregister 258458f30150SMaxime Ripard */ 258558f30150SMaxime Ripard void snd_soc_unregister_component_by_driver(struct device *dev, 258658f30150SMaxime Ripard const struct snd_soc_component_driver *component_driver) 258758f30150SMaxime Ripard { 258858f30150SMaxime Ripard struct snd_soc_component *component; 258958f30150SMaxime Ripard 259058f30150SMaxime Ripard if (!component_driver) 259158f30150SMaxime Ripard return; 259258f30150SMaxime Ripard 259358f30150SMaxime Ripard mutex_lock(&client_mutex); 259458f30150SMaxime Ripard component = snd_soc_lookup_component_nolocked(dev, component_driver->name); 259558f30150SMaxime Ripard if (!component) 259658f30150SMaxime Ripard goto out; 259758f30150SMaxime Ripard 259858f30150SMaxime Ripard snd_soc_del_component_unlocked(component); 259958f30150SMaxime Ripard 260058f30150SMaxime Ripard out: 260158f30150SMaxime Ripard mutex_unlock(&client_mutex); 260258f30150SMaxime Ripard } 260358f30150SMaxime Ripard EXPORT_SYMBOL_GPL(snd_soc_unregister_component_by_driver); 260458f30150SMaxime Ripard 260558f30150SMaxime Ripard /** 26062eccea8cSKuninori Morimoto * snd_soc_unregister_component - Unregister all related component 26072eccea8cSKuninori Morimoto * from the ASoC core 2608d191bd8dSKuninori Morimoto * 2609628536eaSJonathan Corbet * @dev: The device to unregister 2610d191bd8dSKuninori Morimoto */ 26112eccea8cSKuninori Morimoto void snd_soc_unregister_component(struct device *dev) 26122eccea8cSKuninori Morimoto { 2613ac6a4dd3SKuninori Morimoto struct snd_soc_component *component; 2614ac6a4dd3SKuninori Morimoto 2615ac6a4dd3SKuninori Morimoto mutex_lock(&client_mutex); 2616ac6a4dd3SKuninori Morimoto while (1) { 261718dd66eaSKuninori Morimoto component = snd_soc_lookup_component_nolocked(dev, NULL); 2618ac6a4dd3SKuninori Morimoto if (!component) 2619ac6a4dd3SKuninori Morimoto break; 2620ac6a4dd3SKuninori Morimoto 2621ac6a4dd3SKuninori Morimoto snd_soc_del_component_unlocked(component); 2622ac6a4dd3SKuninori Morimoto } 2623ac6a4dd3SKuninori Morimoto mutex_unlock(&client_mutex); 2624d191bd8dSKuninori Morimoto } 2625d191bd8dSKuninori Morimoto EXPORT_SYMBOL_GPL(snd_soc_unregister_component); 2626d191bd8dSKuninori Morimoto 2627bec4fa05SStephen Warren /* Retrieve a card's name from device tree */ 2628b07609ceSKuninori Morimoto int snd_soc_of_parse_card_name(struct snd_soc_card *card, 2629bec4fa05SStephen Warren const char *propname) 2630bec4fa05SStephen Warren { 2631b07609ceSKuninori Morimoto struct device_node *np; 2632bec4fa05SStephen Warren int ret; 2633bec4fa05SStephen Warren 26347e07e7c0STushar Behera if (!card->dev) { 26357e07e7c0STushar Behera pr_err("card->dev is not set before calling %s\n", __func__); 26367e07e7c0STushar Behera return -EINVAL; 26377e07e7c0STushar Behera } 26387e07e7c0STushar Behera 26397e07e7c0STushar Behera np = card->dev->of_node; 26407e07e7c0STushar Behera 2641bec4fa05SStephen Warren ret = of_property_read_string_index(np, propname, 0, &card->name); 2642bec4fa05SStephen Warren /* 2643bec4fa05SStephen Warren * EINVAL means the property does not exist. This is fine providing 2644bec4fa05SStephen Warren * card->name was previously set, which is checked later in 2645bec4fa05SStephen Warren * snd_soc_register_card. 2646bec4fa05SStephen Warren */ 2647bec4fa05SStephen Warren if (ret < 0 && ret != -EINVAL) { 2648bec4fa05SStephen Warren dev_err(card->dev, 2649f110bfc7SLiam Girdwood "ASoC: Property '%s' could not be read: %d\n", 2650bec4fa05SStephen Warren propname, ret); 2651bec4fa05SStephen Warren return ret; 2652bec4fa05SStephen Warren } 2653bec4fa05SStephen Warren 2654bec4fa05SStephen Warren return 0; 2655bec4fa05SStephen Warren } 2656b07609ceSKuninori Morimoto EXPORT_SYMBOL_GPL(snd_soc_of_parse_card_name); 2657bec4fa05SStephen Warren 26589a6d4860SXiubo Li static const struct snd_soc_dapm_widget simple_widgets[] = { 26599a6d4860SXiubo Li SND_SOC_DAPM_MIC("Microphone", NULL), 26609a6d4860SXiubo Li SND_SOC_DAPM_LINE("Line", NULL), 26619a6d4860SXiubo Li SND_SOC_DAPM_HP("Headphone", NULL), 26629a6d4860SXiubo Li SND_SOC_DAPM_SPK("Speaker", NULL), 26639a6d4860SXiubo Li }; 26649a6d4860SXiubo Li 266521efde50SKuninori Morimoto int snd_soc_of_parse_audio_simple_widgets(struct snd_soc_card *card, 26669a6d4860SXiubo Li const char *propname) 26679a6d4860SXiubo Li { 266821efde50SKuninori Morimoto struct device_node *np = card->dev->of_node; 26699a6d4860SXiubo Li struct snd_soc_dapm_widget *widgets; 26709a6d4860SXiubo Li const char *template, *wname; 26719a6d4860SXiubo Li int i, j, num_widgets, ret; 26729a6d4860SXiubo Li 26739a6d4860SXiubo Li num_widgets = of_property_count_strings(np, propname); 26749a6d4860SXiubo Li if (num_widgets < 0) { 26759a6d4860SXiubo Li dev_err(card->dev, 26769a6d4860SXiubo Li "ASoC: Property '%s' does not exist\n", propname); 26779a6d4860SXiubo Li return -EINVAL; 26789a6d4860SXiubo Li } 26799a6d4860SXiubo Li if (num_widgets & 1) { 26809a6d4860SXiubo Li dev_err(card->dev, 26819a6d4860SXiubo Li "ASoC: Property '%s' length is not even\n", propname); 26829a6d4860SXiubo Li return -EINVAL; 26839a6d4860SXiubo Li } 26849a6d4860SXiubo Li 26859a6d4860SXiubo Li num_widgets /= 2; 26869a6d4860SXiubo Li if (!num_widgets) { 26879a6d4860SXiubo Li dev_err(card->dev, "ASoC: Property '%s's length is zero\n", 26889a6d4860SXiubo Li propname); 26899a6d4860SXiubo Li return -EINVAL; 26909a6d4860SXiubo Li } 26919a6d4860SXiubo Li 26929a6d4860SXiubo Li widgets = devm_kcalloc(card->dev, num_widgets, sizeof(*widgets), 26939a6d4860SXiubo Li GFP_KERNEL); 26949a6d4860SXiubo Li if (!widgets) { 26959a6d4860SXiubo Li dev_err(card->dev, 26969a6d4860SXiubo Li "ASoC: Could not allocate memory for widgets\n"); 26979a6d4860SXiubo Li return -ENOMEM; 26989a6d4860SXiubo Li } 26999a6d4860SXiubo Li 27009a6d4860SXiubo Li for (i = 0; i < num_widgets; i++) { 27019a6d4860SXiubo Li ret = of_property_read_string_index(np, propname, 27029a6d4860SXiubo Li 2 * i, &template); 27039a6d4860SXiubo Li if (ret) { 27049a6d4860SXiubo Li dev_err(card->dev, 27059a6d4860SXiubo Li "ASoC: Property '%s' index %d read error:%d\n", 27069a6d4860SXiubo Li propname, 2 * i, ret); 27079a6d4860SXiubo Li return -EINVAL; 27089a6d4860SXiubo Li } 27099a6d4860SXiubo Li 27109a6d4860SXiubo Li for (j = 0; j < ARRAY_SIZE(simple_widgets); j++) { 27119a6d4860SXiubo Li if (!strncmp(template, simple_widgets[j].name, 27129a6d4860SXiubo Li strlen(simple_widgets[j].name))) { 27139a6d4860SXiubo Li widgets[i] = simple_widgets[j]; 27149a6d4860SXiubo Li break; 27159a6d4860SXiubo Li } 27169a6d4860SXiubo Li } 27179a6d4860SXiubo Li 27189a6d4860SXiubo Li if (j >= ARRAY_SIZE(simple_widgets)) { 27199a6d4860SXiubo Li dev_err(card->dev, 27209a6d4860SXiubo Li "ASoC: DAPM widget '%s' is not supported\n", 27219a6d4860SXiubo Li template); 27229a6d4860SXiubo Li return -EINVAL; 27239a6d4860SXiubo Li } 27249a6d4860SXiubo Li 27259a6d4860SXiubo Li ret = of_property_read_string_index(np, propname, 27269a6d4860SXiubo Li (2 * i) + 1, 27279a6d4860SXiubo Li &wname); 27289a6d4860SXiubo Li if (ret) { 27299a6d4860SXiubo Li dev_err(card->dev, 27309a6d4860SXiubo Li "ASoC: Property '%s' index %d read error:%d\n", 27319a6d4860SXiubo Li propname, (2 * i) + 1, ret); 27329a6d4860SXiubo Li return -EINVAL; 27339a6d4860SXiubo Li } 27349a6d4860SXiubo Li 27359a6d4860SXiubo Li widgets[i].name = wname; 27369a6d4860SXiubo Li } 27379a6d4860SXiubo Li 2738f23e860eSNicolin Chen card->of_dapm_widgets = widgets; 2739f23e860eSNicolin Chen card->num_of_dapm_widgets = num_widgets; 27409a6d4860SXiubo Li 27419a6d4860SXiubo Li return 0; 27429a6d4860SXiubo Li } 274321efde50SKuninori Morimoto EXPORT_SYMBOL_GPL(snd_soc_of_parse_audio_simple_widgets); 27449a6d4860SXiubo Li 2745cbdfab3bSJerome Brunet int snd_soc_of_get_slot_mask(struct device_node *np, 27466131084aSJyri Sarha const char *prop_name, 27476131084aSJyri Sarha unsigned int *mask) 27486131084aSJyri Sarha { 27496131084aSJyri Sarha u32 val; 27506c84e591SJyri Sarha const __be32 *of_slot_mask = of_get_property(np, prop_name, &val); 27516131084aSJyri Sarha int i; 27526131084aSJyri Sarha 27536131084aSJyri Sarha if (!of_slot_mask) 27546131084aSJyri Sarha return 0; 27556131084aSJyri Sarha val /= sizeof(u32); 27566131084aSJyri Sarha for (i = 0; i < val; i++) 27576131084aSJyri Sarha if (be32_to_cpup(&of_slot_mask[i])) 27586131084aSJyri Sarha *mask |= (1 << i); 27596131084aSJyri Sarha 27606131084aSJyri Sarha return val; 27616131084aSJyri Sarha } 2762cbdfab3bSJerome Brunet EXPORT_SYMBOL_GPL(snd_soc_of_get_slot_mask); 27636131084aSJyri Sarha 276489c67857SXiubo Li int snd_soc_of_parse_tdm_slot(struct device_node *np, 27656131084aSJyri Sarha unsigned int *tx_mask, 27666131084aSJyri Sarha unsigned int *rx_mask, 276789c67857SXiubo Li unsigned int *slots, 276889c67857SXiubo Li unsigned int *slot_width) 276989c67857SXiubo Li { 277089c67857SXiubo Li u32 val; 277189c67857SXiubo Li int ret; 277289c67857SXiubo Li 27736131084aSJyri Sarha if (tx_mask) 27746131084aSJyri Sarha snd_soc_of_get_slot_mask(np, "dai-tdm-slot-tx-mask", tx_mask); 27756131084aSJyri Sarha if (rx_mask) 27766131084aSJyri Sarha snd_soc_of_get_slot_mask(np, "dai-tdm-slot-rx-mask", rx_mask); 27776131084aSJyri Sarha 277889c67857SXiubo Li if (of_property_read_bool(np, "dai-tdm-slot-num")) { 277989c67857SXiubo Li ret = of_property_read_u32(np, "dai-tdm-slot-num", &val); 278089c67857SXiubo Li if (ret) 278189c67857SXiubo Li return ret; 278289c67857SXiubo Li 278389c67857SXiubo Li if (slots) 278489c67857SXiubo Li *slots = val; 278589c67857SXiubo Li } 278689c67857SXiubo Li 278789c67857SXiubo Li if (of_property_read_bool(np, "dai-tdm-slot-width")) { 278889c67857SXiubo Li ret = of_property_read_u32(np, "dai-tdm-slot-width", &val); 278989c67857SXiubo Li if (ret) 279089c67857SXiubo Li return ret; 279189c67857SXiubo Li 279289c67857SXiubo Li if (slot_width) 279389c67857SXiubo Li *slot_width = val; 279489c67857SXiubo Li } 279589c67857SXiubo Li 279689c67857SXiubo Li return 0; 279789c67857SXiubo Li } 279889c67857SXiubo Li EXPORT_SYMBOL_GPL(snd_soc_of_parse_tdm_slot); 279989c67857SXiubo Li 28003b710356SKuninori Morimoto void snd_soc_of_parse_node_prefix(struct device_node *np, 28015e3cdaa2SKuninori Morimoto struct snd_soc_codec_conf *codec_conf, 28025e3cdaa2SKuninori Morimoto struct device_node *of_node, 28035e3cdaa2SKuninori Morimoto const char *propname) 28045e3cdaa2SKuninori Morimoto { 28055e3cdaa2SKuninori Morimoto const char *str; 28065e3cdaa2SKuninori Morimoto int ret; 28075e3cdaa2SKuninori Morimoto 28085e3cdaa2SKuninori Morimoto ret = of_property_read_string(np, propname, &str); 28095e3cdaa2SKuninori Morimoto if (ret < 0) { 28105e3cdaa2SKuninori Morimoto /* no prefix is not error */ 28115e3cdaa2SKuninori Morimoto return; 28125e3cdaa2SKuninori Morimoto } 28135e3cdaa2SKuninori Morimoto 2814c13493a2SKuninori Morimoto codec_conf->dlc.of_node = of_node; 28155e3cdaa2SKuninori Morimoto codec_conf->name_prefix = str; 28165e3cdaa2SKuninori Morimoto } 28173b710356SKuninori Morimoto EXPORT_SYMBOL_GPL(snd_soc_of_parse_node_prefix); 28185e3cdaa2SKuninori Morimoto 28192bc644afSKuninori Morimoto int snd_soc_of_parse_audio_routing(struct snd_soc_card *card, 2820a4a54dd5SStephen Warren const char *propname) 2821a4a54dd5SStephen Warren { 28222bc644afSKuninori Morimoto struct device_node *np = card->dev->of_node; 2823e3b1e6a1SMark Brown int num_routes; 2824a4a54dd5SStephen Warren struct snd_soc_dapm_route *routes; 2825a4a54dd5SStephen Warren int i, ret; 2826a4a54dd5SStephen Warren 2827a4a54dd5SStephen Warren num_routes = of_property_count_strings(np, propname); 2828c34ce320SRichard Zhao if (num_routes < 0 || num_routes & 1) { 282910e8aa9aSMichał Mirosław dev_err(card->dev, 283010e8aa9aSMichał Mirosław "ASoC: Property '%s' does not exist or its length is not even\n", 283110e8aa9aSMichał Mirosław propname); 2832a4a54dd5SStephen Warren return -EINVAL; 2833a4a54dd5SStephen Warren } 2834a4a54dd5SStephen Warren num_routes /= 2; 2835a4a54dd5SStephen Warren if (!num_routes) { 2836f110bfc7SLiam Girdwood dev_err(card->dev, "ASoC: Property '%s's length is zero\n", 2837a4a54dd5SStephen Warren propname); 2838a4a54dd5SStephen Warren return -EINVAL; 2839a4a54dd5SStephen Warren } 2840a4a54dd5SStephen Warren 2841a86854d0SKees Cook routes = devm_kcalloc(card->dev, num_routes, sizeof(*routes), 2842a4a54dd5SStephen Warren GFP_KERNEL); 2843a4a54dd5SStephen Warren if (!routes) { 2844a4a54dd5SStephen Warren dev_err(card->dev, 2845f110bfc7SLiam Girdwood "ASoC: Could not allocate DAPM route table\n"); 2846a4a54dd5SStephen Warren return -EINVAL; 2847a4a54dd5SStephen Warren } 2848a4a54dd5SStephen Warren 2849a4a54dd5SStephen Warren for (i = 0; i < num_routes; i++) { 2850a4a54dd5SStephen Warren ret = of_property_read_string_index(np, propname, 2851e3b1e6a1SMark Brown 2 * i, &routes[i].sink); 2852a4a54dd5SStephen Warren if (ret) { 2853c871bd0bSMark Brown dev_err(card->dev, 2854c871bd0bSMark Brown "ASoC: Property '%s' index %d could not be read: %d\n", 2855c871bd0bSMark Brown propname, 2 * i, ret); 2856a4a54dd5SStephen Warren return -EINVAL; 2857a4a54dd5SStephen Warren } 2858a4a54dd5SStephen Warren ret = of_property_read_string_index(np, propname, 2859e3b1e6a1SMark Brown (2 * i) + 1, &routes[i].source); 2860a4a54dd5SStephen Warren if (ret) { 2861a4a54dd5SStephen Warren dev_err(card->dev, 2862c871bd0bSMark Brown "ASoC: Property '%s' index %d could not be read: %d\n", 2863c871bd0bSMark Brown propname, (2 * i) + 1, ret); 2864a4a54dd5SStephen Warren return -EINVAL; 2865a4a54dd5SStephen Warren } 2866a4a54dd5SStephen Warren } 2867a4a54dd5SStephen Warren 2868f23e860eSNicolin Chen card->num_of_dapm_routes = num_routes; 2869f23e860eSNicolin Chen card->of_dapm_routes = routes; 2870a4a54dd5SStephen Warren 2871a4a54dd5SStephen Warren return 0; 2872a4a54dd5SStephen Warren } 28732bc644afSKuninori Morimoto EXPORT_SYMBOL_GPL(snd_soc_of_parse_audio_routing); 2874a4a54dd5SStephen Warren 2875a7930ed4SKuninori Morimoto unsigned int snd_soc_of_parse_daifmt(struct device_node *np, 2876389cb834SJyri Sarha const char *prefix, 2877389cb834SJyri Sarha struct device_node **bitclkmaster, 2878389cb834SJyri Sarha struct device_node **framemaster) 2879a7930ed4SKuninori Morimoto { 2880a7930ed4SKuninori Morimoto int ret, i; 2881a7930ed4SKuninori Morimoto char prop[128]; 2882a7930ed4SKuninori Morimoto unsigned int format = 0; 2883a7930ed4SKuninori Morimoto int bit, frame; 2884a7930ed4SKuninori Morimoto const char *str; 2885a7930ed4SKuninori Morimoto struct { 2886a7930ed4SKuninori Morimoto char *name; 2887a7930ed4SKuninori Morimoto unsigned int val; 2888a7930ed4SKuninori Morimoto } of_fmt_table[] = { 2889a7930ed4SKuninori Morimoto { "i2s", SND_SOC_DAIFMT_I2S }, 2890a7930ed4SKuninori Morimoto { "right_j", SND_SOC_DAIFMT_RIGHT_J }, 2891a7930ed4SKuninori Morimoto { "left_j", SND_SOC_DAIFMT_LEFT_J }, 2892a7930ed4SKuninori Morimoto { "dsp_a", SND_SOC_DAIFMT_DSP_A }, 2893a7930ed4SKuninori Morimoto { "dsp_b", SND_SOC_DAIFMT_DSP_B }, 2894a7930ed4SKuninori Morimoto { "ac97", SND_SOC_DAIFMT_AC97 }, 2895a7930ed4SKuninori Morimoto { "pdm", SND_SOC_DAIFMT_PDM}, 2896a7930ed4SKuninori Morimoto { "msb", SND_SOC_DAIFMT_MSB }, 2897a7930ed4SKuninori Morimoto { "lsb", SND_SOC_DAIFMT_LSB }, 2898a7930ed4SKuninori Morimoto }; 2899a7930ed4SKuninori Morimoto 2900a7930ed4SKuninori Morimoto if (!prefix) 2901a7930ed4SKuninori Morimoto prefix = ""; 2902a7930ed4SKuninori Morimoto 2903a7930ed4SKuninori Morimoto /* 29045711c979SKuninori Morimoto * check "dai-format = xxx" 29055711c979SKuninori Morimoto * or "[prefix]format = xxx" 2906a7930ed4SKuninori Morimoto * SND_SOC_DAIFMT_FORMAT_MASK area 2907a7930ed4SKuninori Morimoto */ 29085711c979SKuninori Morimoto ret = of_property_read_string(np, "dai-format", &str); 29095711c979SKuninori Morimoto if (ret < 0) { 2910a7930ed4SKuninori Morimoto snprintf(prop, sizeof(prop), "%sformat", prefix); 2911a7930ed4SKuninori Morimoto ret = of_property_read_string(np, prop, &str); 29125711c979SKuninori Morimoto } 2913a7930ed4SKuninori Morimoto if (ret == 0) { 2914a7930ed4SKuninori Morimoto for (i = 0; i < ARRAY_SIZE(of_fmt_table); i++) { 2915a7930ed4SKuninori Morimoto if (strcmp(str, of_fmt_table[i].name) == 0) { 2916a7930ed4SKuninori Morimoto format |= of_fmt_table[i].val; 2917a7930ed4SKuninori Morimoto break; 2918a7930ed4SKuninori Morimoto } 2919a7930ed4SKuninori Morimoto } 2920a7930ed4SKuninori Morimoto } 2921a7930ed4SKuninori Morimoto 2922a7930ed4SKuninori Morimoto /* 29238c2d6a9fSKuninori Morimoto * check "[prefix]continuous-clock" 2924a7930ed4SKuninori Morimoto * SND_SOC_DAIFMT_CLOCK_MASK area 2925a7930ed4SKuninori Morimoto */ 29268c2d6a9fSKuninori Morimoto snprintf(prop, sizeof(prop), "%scontinuous-clock", prefix); 292751930295SJulia Lawall if (of_property_read_bool(np, prop)) 29288c2d6a9fSKuninori Morimoto format |= SND_SOC_DAIFMT_CONT; 29298c2d6a9fSKuninori Morimoto else 29308c2d6a9fSKuninori Morimoto format |= SND_SOC_DAIFMT_GATED; 2931a7930ed4SKuninori Morimoto 2932a7930ed4SKuninori Morimoto /* 2933a7930ed4SKuninori Morimoto * check "[prefix]bitclock-inversion" 2934a7930ed4SKuninori Morimoto * check "[prefix]frame-inversion" 2935a7930ed4SKuninori Morimoto * SND_SOC_DAIFMT_INV_MASK area 2936a7930ed4SKuninori Morimoto */ 2937a7930ed4SKuninori Morimoto snprintf(prop, sizeof(prop), "%sbitclock-inversion", prefix); 2938a7930ed4SKuninori Morimoto bit = !!of_get_property(np, prop, NULL); 2939a7930ed4SKuninori Morimoto 2940a7930ed4SKuninori Morimoto snprintf(prop, sizeof(prop), "%sframe-inversion", prefix); 2941a7930ed4SKuninori Morimoto frame = !!of_get_property(np, prop, NULL); 2942a7930ed4SKuninori Morimoto 2943a7930ed4SKuninori Morimoto switch ((bit << 4) + frame) { 2944a7930ed4SKuninori Morimoto case 0x11: 2945a7930ed4SKuninori Morimoto format |= SND_SOC_DAIFMT_IB_IF; 2946a7930ed4SKuninori Morimoto break; 2947a7930ed4SKuninori Morimoto case 0x10: 2948a7930ed4SKuninori Morimoto format |= SND_SOC_DAIFMT_IB_NF; 2949a7930ed4SKuninori Morimoto break; 2950a7930ed4SKuninori Morimoto case 0x01: 2951a7930ed4SKuninori Morimoto format |= SND_SOC_DAIFMT_NB_IF; 2952a7930ed4SKuninori Morimoto break; 2953a7930ed4SKuninori Morimoto default: 2954a7930ed4SKuninori Morimoto /* SND_SOC_DAIFMT_NB_NF is default */ 2955a7930ed4SKuninori Morimoto break; 2956a7930ed4SKuninori Morimoto } 2957a7930ed4SKuninori Morimoto 2958a7930ed4SKuninori Morimoto /* 2959a7930ed4SKuninori Morimoto * check "[prefix]bitclock-master" 2960a7930ed4SKuninori Morimoto * check "[prefix]frame-master" 2961a7930ed4SKuninori Morimoto * SND_SOC_DAIFMT_MASTER_MASK area 2962a7930ed4SKuninori Morimoto */ 2963a7930ed4SKuninori Morimoto snprintf(prop, sizeof(prop), "%sbitclock-master", prefix); 2964a7930ed4SKuninori Morimoto bit = !!of_get_property(np, prop, NULL); 2965389cb834SJyri Sarha if (bit && bitclkmaster) 2966389cb834SJyri Sarha *bitclkmaster = of_parse_phandle(np, prop, 0); 2967a7930ed4SKuninori Morimoto 2968a7930ed4SKuninori Morimoto snprintf(prop, sizeof(prop), "%sframe-master", prefix); 2969a7930ed4SKuninori Morimoto frame = !!of_get_property(np, prop, NULL); 2970389cb834SJyri Sarha if (frame && framemaster) 2971389cb834SJyri Sarha *framemaster = of_parse_phandle(np, prop, 0); 2972a7930ed4SKuninori Morimoto 2973a7930ed4SKuninori Morimoto switch ((bit << 4) + frame) { 2974a7930ed4SKuninori Morimoto case 0x11: 2975a7930ed4SKuninori Morimoto format |= SND_SOC_DAIFMT_CBM_CFM; 2976a7930ed4SKuninori Morimoto break; 2977a7930ed4SKuninori Morimoto case 0x10: 2978a7930ed4SKuninori Morimoto format |= SND_SOC_DAIFMT_CBM_CFS; 2979a7930ed4SKuninori Morimoto break; 2980a7930ed4SKuninori Morimoto case 0x01: 2981a7930ed4SKuninori Morimoto format |= SND_SOC_DAIFMT_CBS_CFM; 2982a7930ed4SKuninori Morimoto break; 2983a7930ed4SKuninori Morimoto default: 2984a7930ed4SKuninori Morimoto format |= SND_SOC_DAIFMT_CBS_CFS; 2985a7930ed4SKuninori Morimoto break; 2986a7930ed4SKuninori Morimoto } 2987a7930ed4SKuninori Morimoto 2988a7930ed4SKuninori Morimoto return format; 2989a7930ed4SKuninori Morimoto } 2990a7930ed4SKuninori Morimoto EXPORT_SYMBOL_GPL(snd_soc_of_parse_daifmt); 2991a7930ed4SKuninori Morimoto 2992a180e8b9SKuninori Morimoto int snd_soc_get_dai_id(struct device_node *ep) 2993a180e8b9SKuninori Morimoto { 299409d4cc03SKuninori Morimoto struct snd_soc_component *component; 2995c1e230f0SKuninori Morimoto struct snd_soc_dai_link_component dlc; 2996a180e8b9SKuninori Morimoto int ret; 2997a180e8b9SKuninori Morimoto 2998c1e230f0SKuninori Morimoto dlc.of_node = of_graph_get_port_parent(ep); 2999c1e230f0SKuninori Morimoto dlc.name = NULL; 3000a180e8b9SKuninori Morimoto /* 3001a180e8b9SKuninori Morimoto * For example HDMI case, HDMI has video/sound port, 3002a180e8b9SKuninori Morimoto * but ALSA SoC needs sound port number only. 3003a180e8b9SKuninori Morimoto * Thus counting HDMI DT port/endpoint doesn't work. 3004a180e8b9SKuninori Morimoto * Then, it should have .of_xlate_dai_id 3005a180e8b9SKuninori Morimoto */ 3006a180e8b9SKuninori Morimoto ret = -ENOTSUPP; 3007a180e8b9SKuninori Morimoto mutex_lock(&client_mutex); 3008c1e230f0SKuninori Morimoto component = soc_find_component(&dlc); 30092c7b1704SKuninori Morimoto if (component) 30102c7b1704SKuninori Morimoto ret = snd_soc_component_of_xlate_dai_id(component, ep); 3011a180e8b9SKuninori Morimoto mutex_unlock(&client_mutex); 3012a180e8b9SKuninori Morimoto 3013c1e230f0SKuninori Morimoto of_node_put(dlc.of_node); 3014c0a480d1STony Lindgren 3015a180e8b9SKuninori Morimoto return ret; 3016a180e8b9SKuninori Morimoto } 3017a180e8b9SKuninori Morimoto EXPORT_SYMBOL_GPL(snd_soc_get_dai_id); 3018a180e8b9SKuninori Morimoto 30191ad8ec53SKuninori Morimoto int snd_soc_get_dai_name(struct of_phandle_args *args, 3020cb470087SKuninori Morimoto const char **dai_name) 3021cb470087SKuninori Morimoto { 3022cb470087SKuninori Morimoto struct snd_soc_component *pos; 30233e0aa8d8SJyri Sarha struct device_node *component_of_node; 302493b0f3eeSJean-Francois Moine int ret = -EPROBE_DEFER; 3025cb470087SKuninori Morimoto 3026cb470087SKuninori Morimoto mutex_lock(&client_mutex); 3027368dee94SKuninori Morimoto for_each_component(pos) { 3028c0834440SKuninori Morimoto component_of_node = soc_component_to_node(pos); 30293e0aa8d8SJyri Sarha 30303e0aa8d8SJyri Sarha if (component_of_node != args->np) 3031cb470087SKuninori Morimoto continue; 3032cb470087SKuninori Morimoto 3033a2a34175SKuninori Morimoto ret = snd_soc_component_of_xlate_dai_name(pos, args, dai_name); 3034a2a34175SKuninori Morimoto if (ret == -ENOTSUPP) { 303558bf4179SKuninori Morimoto struct snd_soc_dai *dai; 30366833c452SKuninori Morimoto int id = -1; 30376833c452SKuninori Morimoto 303893b0f3eeSJean-Francois Moine switch (args->args_count) { 30396833c452SKuninori Morimoto case 0: 30406833c452SKuninori Morimoto id = 0; /* same as dai_drv[0] */ 30416833c452SKuninori Morimoto break; 30426833c452SKuninori Morimoto case 1: 304393b0f3eeSJean-Francois Moine id = args->args[0]; 30446833c452SKuninori Morimoto break; 30456833c452SKuninori Morimoto default: 30466833c452SKuninori Morimoto /* not supported */ 3047cb470087SKuninori Morimoto break; 3048cb470087SKuninori Morimoto } 3049cb470087SKuninori Morimoto 30506833c452SKuninori Morimoto if (id < 0 || id >= pos->num_dai) { 30516833c452SKuninori Morimoto ret = -EINVAL; 30523dcba280SNicolin Chen continue; 30536833c452SKuninori Morimoto } 3054e41975edSXiubo Li 3055e41975edSXiubo Li ret = 0; 3056e41975edSXiubo Li 305758bf4179SKuninori Morimoto /* find target DAI */ 305815a0c645SKuninori Morimoto for_each_component_dais(pos, dai) { 305958bf4179SKuninori Morimoto if (id == 0) 306058bf4179SKuninori Morimoto break; 306158bf4179SKuninori Morimoto id--; 306258bf4179SKuninori Morimoto } 306358bf4179SKuninori Morimoto 306458bf4179SKuninori Morimoto *dai_name = dai->driver->name; 3065e41975edSXiubo Li if (!*dai_name) 3066e41975edSXiubo Li *dai_name = pos->name; 30671dfa5a5aSJerome Brunet } else if (ret) { 30681dfa5a5aSJerome Brunet /* 30691dfa5a5aSJerome Brunet * if another error than ENOTSUPP is returned go on and 30701dfa5a5aSJerome Brunet * check if another component is provided with the same 30711dfa5a5aSJerome Brunet * node. This may happen if a device provides several 30721dfa5a5aSJerome Brunet * components 30731dfa5a5aSJerome Brunet */ 30741dfa5a5aSJerome Brunet continue; 30756833c452SKuninori Morimoto } 30766833c452SKuninori Morimoto 3077cb470087SKuninori Morimoto break; 3078cb470087SKuninori Morimoto } 3079cb470087SKuninori Morimoto mutex_unlock(&client_mutex); 308093b0f3eeSJean-Francois Moine return ret; 308193b0f3eeSJean-Francois Moine } 30821ad8ec53SKuninori Morimoto EXPORT_SYMBOL_GPL(snd_soc_get_dai_name); 308393b0f3eeSJean-Francois Moine 308493b0f3eeSJean-Francois Moine int snd_soc_of_get_dai_name(struct device_node *of_node, 308593b0f3eeSJean-Francois Moine const char **dai_name) 308693b0f3eeSJean-Francois Moine { 308793b0f3eeSJean-Francois Moine struct of_phandle_args args; 308893b0f3eeSJean-Francois Moine int ret; 308993b0f3eeSJean-Francois Moine 309093b0f3eeSJean-Francois Moine ret = of_parse_phandle_with_args(of_node, "sound-dai", 309193b0f3eeSJean-Francois Moine "#sound-dai-cells", 0, &args); 309293b0f3eeSJean-Francois Moine if (ret) 309393b0f3eeSJean-Francois Moine return ret; 309493b0f3eeSJean-Francois Moine 309593b0f3eeSJean-Francois Moine ret = snd_soc_get_dai_name(&args, dai_name); 3096cb470087SKuninori Morimoto 3097cb470087SKuninori Morimoto of_node_put(args.np); 3098cb470087SKuninori Morimoto 3099cb470087SKuninori Morimoto return ret; 3100cb470087SKuninori Morimoto } 3101cb470087SKuninori Morimoto EXPORT_SYMBOL_GPL(snd_soc_of_get_dai_name); 3102cb470087SKuninori Morimoto 310393b0f3eeSJean-Francois Moine /* 310494685763SSylwester Nawrocki * snd_soc_of_put_dai_link_codecs - Dereference device nodes in the codecs array 310594685763SSylwester Nawrocki * @dai_link: DAI link 310694685763SSylwester Nawrocki * 310794685763SSylwester Nawrocki * Dereference device nodes acquired by snd_soc_of_get_dai_link_codecs(). 310894685763SSylwester Nawrocki */ 310994685763SSylwester Nawrocki void snd_soc_of_put_dai_link_codecs(struct snd_soc_dai_link *dai_link) 311094685763SSylwester Nawrocki { 31113db769f1SKuninori Morimoto struct snd_soc_dai_link_component *component; 311294685763SSylwester Nawrocki int index; 311394685763SSylwester Nawrocki 31143db769f1SKuninori Morimoto for_each_link_codecs(dai_link, index, component) { 311594685763SSylwester Nawrocki if (!component->of_node) 311694685763SSylwester Nawrocki break; 311794685763SSylwester Nawrocki of_node_put(component->of_node); 311894685763SSylwester Nawrocki component->of_node = NULL; 311994685763SSylwester Nawrocki } 312094685763SSylwester Nawrocki } 312194685763SSylwester Nawrocki EXPORT_SYMBOL_GPL(snd_soc_of_put_dai_link_codecs); 312294685763SSylwester Nawrocki 312394685763SSylwester Nawrocki /* 312493b0f3eeSJean-Francois Moine * snd_soc_of_get_dai_link_codecs - Parse a list of CODECs in the devicetree 312593b0f3eeSJean-Francois Moine * @dev: Card device 312693b0f3eeSJean-Francois Moine * @of_node: Device node 312793b0f3eeSJean-Francois Moine * @dai_link: DAI link 312893b0f3eeSJean-Francois Moine * 312993b0f3eeSJean-Francois Moine * Builds an array of CODEC DAI components from the DAI link property 313093b0f3eeSJean-Francois Moine * 'sound-dai'. 313193b0f3eeSJean-Francois Moine * The array is set in the DAI link and the number of DAIs is set accordingly. 313294685763SSylwester Nawrocki * The device nodes in the array (of_node) must be dereferenced by calling 313394685763SSylwester Nawrocki * snd_soc_of_put_dai_link_codecs() on @dai_link. 313493b0f3eeSJean-Francois Moine * 313593b0f3eeSJean-Francois Moine * Returns 0 for success 313693b0f3eeSJean-Francois Moine */ 313793b0f3eeSJean-Francois Moine int snd_soc_of_get_dai_link_codecs(struct device *dev, 313893b0f3eeSJean-Francois Moine struct device_node *of_node, 313993b0f3eeSJean-Francois Moine struct snd_soc_dai_link *dai_link) 314093b0f3eeSJean-Francois Moine { 314193b0f3eeSJean-Francois Moine struct of_phandle_args args; 314293b0f3eeSJean-Francois Moine struct snd_soc_dai_link_component *component; 314393b0f3eeSJean-Francois Moine char *name; 314493b0f3eeSJean-Francois Moine int index, num_codecs, ret; 314593b0f3eeSJean-Francois Moine 314693b0f3eeSJean-Francois Moine /* Count the number of CODECs */ 314793b0f3eeSJean-Francois Moine name = "sound-dai"; 314893b0f3eeSJean-Francois Moine num_codecs = of_count_phandle_with_args(of_node, name, 314993b0f3eeSJean-Francois Moine "#sound-dai-cells"); 315093b0f3eeSJean-Francois Moine if (num_codecs <= 0) { 315193b0f3eeSJean-Francois Moine if (num_codecs == -ENOENT) 315293b0f3eeSJean-Francois Moine dev_err(dev, "No 'sound-dai' property\n"); 315393b0f3eeSJean-Francois Moine else 315493b0f3eeSJean-Francois Moine dev_err(dev, "Bad phandle in 'sound-dai'\n"); 315593b0f3eeSJean-Francois Moine return num_codecs; 315693b0f3eeSJean-Francois Moine } 3157a86854d0SKees Cook component = devm_kcalloc(dev, 3158a86854d0SKees Cook num_codecs, sizeof(*component), 315993b0f3eeSJean-Francois Moine GFP_KERNEL); 316093b0f3eeSJean-Francois Moine if (!component) 316193b0f3eeSJean-Francois Moine return -ENOMEM; 316293b0f3eeSJean-Francois Moine dai_link->codecs = component; 316393b0f3eeSJean-Francois Moine dai_link->num_codecs = num_codecs; 316493b0f3eeSJean-Francois Moine 316593b0f3eeSJean-Francois Moine /* Parse the list */ 31663db769f1SKuninori Morimoto for_each_link_codecs(dai_link, index, component) { 316793b0f3eeSJean-Francois Moine ret = of_parse_phandle_with_args(of_node, name, 316893b0f3eeSJean-Francois Moine "#sound-dai-cells", 316993b0f3eeSJean-Francois Moine index, &args); 317093b0f3eeSJean-Francois Moine if (ret) 317193b0f3eeSJean-Francois Moine goto err; 317293b0f3eeSJean-Francois Moine component->of_node = args.np; 317393b0f3eeSJean-Francois Moine ret = snd_soc_get_dai_name(&args, &component->dai_name); 317493b0f3eeSJean-Francois Moine if (ret < 0) 317593b0f3eeSJean-Francois Moine goto err; 317693b0f3eeSJean-Francois Moine } 317793b0f3eeSJean-Francois Moine return 0; 317893b0f3eeSJean-Francois Moine err: 317994685763SSylwester Nawrocki snd_soc_of_put_dai_link_codecs(dai_link); 318093b0f3eeSJean-Francois Moine dai_link->codecs = NULL; 318193b0f3eeSJean-Francois Moine dai_link->num_codecs = 0; 318293b0f3eeSJean-Francois Moine return ret; 318393b0f3eeSJean-Francois Moine } 318493b0f3eeSJean-Francois Moine EXPORT_SYMBOL_GPL(snd_soc_of_get_dai_link_codecs); 318593b0f3eeSJean-Francois Moine 3186c9b3a40fSTakashi Iwai static int __init snd_soc_init(void) 3187db2a4165SFrank Mandarino { 31886553bf06SLars-Peter Clausen snd_soc_debugfs_init(); 3189fb257897SMark Brown snd_soc_util_init(); 3190fb257897SMark Brown 3191db2a4165SFrank Mandarino return platform_driver_register(&soc_driver); 3192db2a4165SFrank Mandarino } 31934abe8e16SMark Brown module_init(snd_soc_init); 3194db2a4165SFrank Mandarino 31957d8c16a6SMark Brown static void __exit snd_soc_exit(void) 3196db2a4165SFrank Mandarino { 3197fb257897SMark Brown snd_soc_util_exit(); 31986553bf06SLars-Peter Clausen snd_soc_debugfs_exit(); 3199fb257897SMark Brown 3200db2a4165SFrank Mandarino platform_driver_unregister(&soc_driver); 3201db2a4165SFrank Mandarino } 3202db2a4165SFrank Mandarino module_exit(snd_soc_exit); 3203db2a4165SFrank Mandarino 3204db2a4165SFrank Mandarino /* Module information */ 3205d331124dSLiam Girdwood MODULE_AUTHOR("Liam Girdwood, lrg@slimlogic.co.uk"); 3206db2a4165SFrank Mandarino MODULE_DESCRIPTION("ALSA SoC Core"); 3207db2a4165SFrank Mandarino MODULE_LICENSE("GPL"); 32088b45a209SKay Sievers MODULE_ALIAS("platform:soc-audio"); 3209