1873486edSKuninori Morimoto // SPDX-License-Identifier: GPL-2.0+ 2873486edSKuninori Morimoto // 3873486edSKuninori Morimoto // soc-core.c -- ALSA SoC Audio Layer 4873486edSKuninori Morimoto // 5873486edSKuninori Morimoto // Copyright 2005 Wolfson Microelectronics PLC. 6873486edSKuninori Morimoto // Copyright 2005 Openedhand Ltd. 7873486edSKuninori Morimoto // Copyright (C) 2010 Slimlogic Ltd. 8873486edSKuninori Morimoto // Copyright (C) 2010 Texas Instruments Inc. 9873486edSKuninori Morimoto // 10873486edSKuninori Morimoto // Author: Liam Girdwood <lrg@slimlogic.co.uk> 11873486edSKuninori Morimoto // with code, comments and ideas from :- 12873486edSKuninori Morimoto // Richard Purdie <richard@openedhand.com> 13873486edSKuninori Morimoto // 14873486edSKuninori Morimoto // TODO: 15873486edSKuninori Morimoto // o Add hw rules to enforce rates, etc. 16873486edSKuninori Morimoto // o More testing with other codecs/machines. 17873486edSKuninori Morimoto // o Add more codecs and platforms to ensure good API coverage. 18873486edSKuninori Morimoto // o Support TDM on PCM and I2S 19db2a4165SFrank Mandarino 20db2a4165SFrank Mandarino #include <linux/module.h> 21db2a4165SFrank Mandarino #include <linux/moduleparam.h> 22db2a4165SFrank Mandarino #include <linux/init.h> 23db2a4165SFrank Mandarino #include <linux/delay.h> 24db2a4165SFrank Mandarino #include <linux/pm.h> 25db2a4165SFrank Mandarino #include <linux/bitops.h> 2612ef193dSTroy Kisky #include <linux/debugfs.h> 27db2a4165SFrank Mandarino #include <linux/platform_device.h> 28741a509fSMarkus Pargmann #include <linux/pinctrl/consumer.h> 29f0e8ed85SMark Brown #include <linux/ctype.h> 305a0e3ad6STejun Heo #include <linux/slab.h> 31bec4fa05SStephen Warren #include <linux/of.h> 32a180e8b9SKuninori Morimoto #include <linux/of_graph.h> 33345233d7SLiam Girdwood #include <linux/dmi.h> 34db2a4165SFrank Mandarino #include <sound/core.h> 353028eb8cSMark Brown #include <sound/jack.h> 36db2a4165SFrank Mandarino #include <sound/pcm.h> 37db2a4165SFrank Mandarino #include <sound/pcm_params.h> 38db2a4165SFrank Mandarino #include <sound/soc.h> 3901d7584cSLiam Girdwood #include <sound/soc-dpcm.h> 408a978234SLiam Girdwood #include <sound/soc-topology.h> 41db2a4165SFrank Mandarino #include <sound/initval.h> 42db2a4165SFrank Mandarino 43a8b1d34fSMark Brown #define CREATE_TRACE_POINTS 44a8b1d34fSMark Brown #include <trace/events/asoc.h> 45a8b1d34fSMark Brown 46f0fba2adSLiam Girdwood #define NAME_SIZE 32 47f0fba2adSLiam Girdwood 48384c89e2SMark Brown #ifdef CONFIG_DEBUG_FS 498a9dab1aSMark Brown struct dentry *snd_soc_debugfs_root; 508a9dab1aSMark Brown EXPORT_SYMBOL_GPL(snd_soc_debugfs_root); 51384c89e2SMark Brown #endif 52384c89e2SMark Brown 53c5af3a2eSMark Brown static DEFINE_MUTEX(client_mutex); 54030e79f6SKuninori Morimoto static LIST_HEAD(component_list); 55*e894efefSSrinivas Kandagatla static LIST_HEAD(unbind_card_list); 56c5af3a2eSMark Brown 57db2a4165SFrank Mandarino /* 58db2a4165SFrank Mandarino * This is a timeout to do a DAPM powerdown after a stream is closed(). 59db2a4165SFrank Mandarino * It can be used to eliminate pops between different playback streams, e.g. 60db2a4165SFrank Mandarino * between two audio tracks. 61db2a4165SFrank Mandarino */ 62db2a4165SFrank Mandarino static int pmdown_time = 5000; 63db2a4165SFrank Mandarino module_param(pmdown_time, int, 0); 64db2a4165SFrank Mandarino MODULE_PARM_DESC(pmdown_time, "DAPM stream powerdown time (msecs)"); 65db2a4165SFrank Mandarino 6698faf436SMengdong Lin /* If a DMI filed contain strings in this blacklist (e.g. 6798faf436SMengdong Lin * "Type2 - Board Manufacturer" or "Type1 - TBD by OEM"), it will be taken 6898faf436SMengdong Lin * as invalid and dropped when setting the card long name from DMI info. 6998faf436SMengdong Lin */ 7098faf436SMengdong Lin static const char * const dmi_blacklist[] = { 7198faf436SMengdong Lin "To be filled by OEM", 7298faf436SMengdong Lin "TBD by OEM", 7398faf436SMengdong Lin "Default String", 7498faf436SMengdong Lin "Board Manufacturer", 7598faf436SMengdong Lin "Board Vendor Name", 7698faf436SMengdong Lin "Board Product Name", 7798faf436SMengdong Lin NULL, /* terminator */ 7898faf436SMengdong Lin }; 7998faf436SMengdong Lin 80dbe21408SMark Brown static ssize_t pmdown_time_show(struct device *dev, 81dbe21408SMark Brown struct device_attribute *attr, char *buf) 82dbe21408SMark Brown { 8336ae1a96SMark Brown struct snd_soc_pcm_runtime *rtd = dev_get_drvdata(dev); 84dbe21408SMark Brown 85f0fba2adSLiam Girdwood return sprintf(buf, "%ld\n", rtd->pmdown_time); 86dbe21408SMark Brown } 87dbe21408SMark Brown 88dbe21408SMark Brown static ssize_t pmdown_time_set(struct device *dev, 89dbe21408SMark Brown struct device_attribute *attr, 90dbe21408SMark Brown const char *buf, size_t count) 91dbe21408SMark Brown { 9236ae1a96SMark Brown struct snd_soc_pcm_runtime *rtd = dev_get_drvdata(dev); 93c593b520SMark Brown int ret; 94dbe21408SMark Brown 95b785a492SJingoo Han ret = kstrtol(buf, 10, &rtd->pmdown_time); 96c593b520SMark Brown if (ret) 97c593b520SMark Brown return ret; 98dbe21408SMark Brown 99dbe21408SMark Brown return count; 100dbe21408SMark Brown } 101dbe21408SMark Brown 102dbe21408SMark Brown static DEVICE_ATTR(pmdown_time, 0644, pmdown_time_show, pmdown_time_set); 103dbe21408SMark Brown 104d29697dcSTakashi Iwai static struct attribute *soc_dev_attrs[] = { 105d29697dcSTakashi Iwai &dev_attr_pmdown_time.attr, 106d29697dcSTakashi Iwai NULL 107d29697dcSTakashi Iwai }; 108d29697dcSTakashi Iwai 109d29697dcSTakashi Iwai static umode_t soc_dev_attr_is_visible(struct kobject *kobj, 110d29697dcSTakashi Iwai struct attribute *attr, int idx) 111d29697dcSTakashi Iwai { 112d29697dcSTakashi Iwai struct device *dev = kobj_to_dev(kobj); 113d29697dcSTakashi Iwai struct snd_soc_pcm_runtime *rtd = dev_get_drvdata(dev); 114d29697dcSTakashi Iwai 115d29697dcSTakashi Iwai if (attr == &dev_attr_pmdown_time.attr) 116d29697dcSTakashi Iwai return attr->mode; /* always visible */ 1173b6eed8dSKuninori Morimoto return rtd->num_codecs ? attr->mode : 0; /* enabled only with codec */ 118d29697dcSTakashi Iwai } 119d29697dcSTakashi Iwai 120d29697dcSTakashi Iwai static const struct attribute_group soc_dapm_dev_group = { 121d29697dcSTakashi Iwai .attrs = soc_dapm_dev_attrs, 122d29697dcSTakashi Iwai .is_visible = soc_dev_attr_is_visible, 123d29697dcSTakashi Iwai }; 124d29697dcSTakashi Iwai 125f7e73b26SMark Brown static const struct attribute_group soc_dev_group = { 126d29697dcSTakashi Iwai .attrs = soc_dev_attrs, 127d29697dcSTakashi Iwai .is_visible = soc_dev_attr_is_visible, 128d29697dcSTakashi Iwai }; 129d29697dcSTakashi Iwai 130d29697dcSTakashi Iwai static const struct attribute_group *soc_dev_attr_groups[] = { 131d29697dcSTakashi Iwai &soc_dapm_dev_group, 132f7e73b26SMark Brown &soc_dev_group, 133d29697dcSTakashi Iwai NULL 134d29697dcSTakashi Iwai }; 135d29697dcSTakashi Iwai 1362624d5faSMark Brown #ifdef CONFIG_DEBUG_FS 13781c7cfd1SLars-Peter Clausen static void soc_init_component_debugfs(struct snd_soc_component *component) 138e73f3de5SRussell King { 1396553bf06SLars-Peter Clausen if (!component->card->debugfs_card_root) 1406553bf06SLars-Peter Clausen return; 1416553bf06SLars-Peter Clausen 14281c7cfd1SLars-Peter Clausen if (component->debugfs_prefix) { 14381c7cfd1SLars-Peter Clausen char *name; 144e73f3de5SRussell King 14581c7cfd1SLars-Peter Clausen name = kasprintf(GFP_KERNEL, "%s:%s", 14681c7cfd1SLars-Peter Clausen component->debugfs_prefix, component->name); 14781c7cfd1SLars-Peter Clausen if (name) { 14881c7cfd1SLars-Peter Clausen component->debugfs_root = debugfs_create_dir(name, 14981c7cfd1SLars-Peter Clausen component->card->debugfs_card_root); 15081c7cfd1SLars-Peter Clausen kfree(name); 15181c7cfd1SLars-Peter Clausen } 15281c7cfd1SLars-Peter Clausen } else { 15381c7cfd1SLars-Peter Clausen component->debugfs_root = debugfs_create_dir(component->name, 15481c7cfd1SLars-Peter Clausen component->card->debugfs_card_root); 155e73f3de5SRussell King } 156e73f3de5SRussell King 15781c7cfd1SLars-Peter Clausen if (!component->debugfs_root) { 15881c7cfd1SLars-Peter Clausen dev_warn(component->dev, 15981c7cfd1SLars-Peter Clausen "ASoC: Failed to create component debugfs directory\n"); 1602624d5faSMark Brown return; 1612624d5faSMark Brown } 1622624d5faSMark Brown 16381c7cfd1SLars-Peter Clausen snd_soc_dapm_debugfs_init(snd_soc_component_get_dapm(component), 16481c7cfd1SLars-Peter Clausen component->debugfs_root); 16581c7cfd1SLars-Peter Clausen } 16681c7cfd1SLars-Peter Clausen 16781c7cfd1SLars-Peter Clausen static void soc_cleanup_component_debugfs(struct snd_soc_component *component) 16881c7cfd1SLars-Peter Clausen { 16981c7cfd1SLars-Peter Clausen debugfs_remove_recursive(component->debugfs_root); 17081c7cfd1SLars-Peter Clausen } 17181c7cfd1SLars-Peter Clausen 172c15b2a1dSPeng Donglin static int dai_list_show(struct seq_file *m, void *v) 173f3208780SMark Brown { 1741438c2f6SLars-Peter Clausen struct snd_soc_component *component; 175f3208780SMark Brown struct snd_soc_dai *dai; 176f3208780SMark Brown 17734e81ab4SLars-Peter Clausen mutex_lock(&client_mutex); 17834e81ab4SLars-Peter Clausen 179700c17caSDonglin Peng list_for_each_entry(component, &component_list, list) 180700c17caSDonglin Peng list_for_each_entry(dai, &component->dai_list, list) 181700c17caSDonglin Peng seq_printf(m, "%s\n", dai->name); 182f3208780SMark Brown 18334e81ab4SLars-Peter Clausen mutex_unlock(&client_mutex); 18434e81ab4SLars-Peter Clausen 185700c17caSDonglin Peng return 0; 186700c17caSDonglin Peng } 187c15b2a1dSPeng Donglin DEFINE_SHOW_ATTRIBUTE(dai_list); 188f3208780SMark Brown 189db795f9bSKuninori Morimoto static int component_list_show(struct seq_file *m, void *v) 190db795f9bSKuninori Morimoto { 191db795f9bSKuninori Morimoto struct snd_soc_component *component; 192db795f9bSKuninori Morimoto 193db795f9bSKuninori Morimoto mutex_lock(&client_mutex); 194db795f9bSKuninori Morimoto 195db795f9bSKuninori Morimoto list_for_each_entry(component, &component_list, list) 196db795f9bSKuninori Morimoto seq_printf(m, "%s\n", component->name); 197db795f9bSKuninori Morimoto 198db795f9bSKuninori Morimoto mutex_unlock(&client_mutex); 199db795f9bSKuninori Morimoto 200db795f9bSKuninori Morimoto return 0; 201db795f9bSKuninori Morimoto } 202db795f9bSKuninori Morimoto DEFINE_SHOW_ATTRIBUTE(component_list); 203db795f9bSKuninori Morimoto 204a6052154SJarkko Nikula static void soc_init_card_debugfs(struct snd_soc_card *card) 205a6052154SJarkko Nikula { 2066553bf06SLars-Peter Clausen if (!snd_soc_debugfs_root) 2076553bf06SLars-Peter Clausen return; 2086553bf06SLars-Peter Clausen 209a6052154SJarkko Nikula card->debugfs_card_root = debugfs_create_dir(card->name, 2108a9dab1aSMark Brown snd_soc_debugfs_root); 2113a45b867SJarkko Nikula if (!card->debugfs_card_root) { 212a6052154SJarkko Nikula dev_warn(card->dev, 2137c08be84SLothar Waßmann "ASoC: Failed to create card debugfs directory\n"); 2143a45b867SJarkko Nikula return; 2153a45b867SJarkko Nikula } 2163a45b867SJarkko Nikula 2173a45b867SJarkko Nikula card->debugfs_pop_time = debugfs_create_u32("dapm_pop_time", 0644, 2183a45b867SJarkko Nikula card->debugfs_card_root, 2193a45b867SJarkko Nikula &card->pop_time); 2203a45b867SJarkko Nikula if (!card->debugfs_pop_time) 2213a45b867SJarkko Nikula dev_warn(card->dev, 222f110bfc7SLiam Girdwood "ASoC: Failed to create pop time debugfs file\n"); 223a6052154SJarkko Nikula } 224a6052154SJarkko Nikula 225a6052154SJarkko Nikula static void soc_cleanup_card_debugfs(struct snd_soc_card *card) 226a6052154SJarkko Nikula { 227a6052154SJarkko Nikula debugfs_remove_recursive(card->debugfs_card_root); 228a6052154SJarkko Nikula } 229a6052154SJarkko Nikula 2306553bf06SLars-Peter Clausen static void snd_soc_debugfs_init(void) 2316553bf06SLars-Peter Clausen { 2326553bf06SLars-Peter Clausen snd_soc_debugfs_root = debugfs_create_dir("asoc", NULL); 233d9a02c55SFabio Estevam if (IS_ERR_OR_NULL(snd_soc_debugfs_root)) { 2346553bf06SLars-Peter Clausen pr_warn("ASoC: Failed to create debugfs directory\n"); 2356553bf06SLars-Peter Clausen snd_soc_debugfs_root = NULL; 2366553bf06SLars-Peter Clausen return; 2376553bf06SLars-Peter Clausen } 2386553bf06SLars-Peter Clausen 2396553bf06SLars-Peter Clausen if (!debugfs_create_file("dais", 0444, snd_soc_debugfs_root, NULL, 2406553bf06SLars-Peter Clausen &dai_list_fops)) 2416553bf06SLars-Peter Clausen pr_warn("ASoC: Failed to create DAI list debugfs file\n"); 242db795f9bSKuninori Morimoto 243db795f9bSKuninori Morimoto if (!debugfs_create_file("components", 0444, snd_soc_debugfs_root, NULL, 244db795f9bSKuninori Morimoto &component_list_fops)) 245db795f9bSKuninori Morimoto pr_warn("ASoC: Failed to create component list debugfs file\n"); 2466553bf06SLars-Peter Clausen } 2476553bf06SLars-Peter Clausen 2486553bf06SLars-Peter Clausen static void snd_soc_debugfs_exit(void) 2496553bf06SLars-Peter Clausen { 2506553bf06SLars-Peter Clausen debugfs_remove_recursive(snd_soc_debugfs_root); 2516553bf06SLars-Peter Clausen } 2526553bf06SLars-Peter Clausen 2532624d5faSMark Brown #else 2542624d5faSMark Brown 25581c7cfd1SLars-Peter Clausen static inline void soc_init_component_debugfs( 25681c7cfd1SLars-Peter Clausen struct snd_soc_component *component) 2572624d5faSMark Brown { 2582624d5faSMark Brown } 2592624d5faSMark Brown 26081c7cfd1SLars-Peter Clausen static inline void soc_cleanup_component_debugfs( 26181c7cfd1SLars-Peter Clausen struct snd_soc_component *component) 262731f1ab2SSebastien Guiriec { 263731f1ab2SSebastien Guiriec } 264731f1ab2SSebastien Guiriec 265b95fccbcSAxel Lin static inline void soc_init_card_debugfs(struct snd_soc_card *card) 266b95fccbcSAxel Lin { 267b95fccbcSAxel Lin } 268b95fccbcSAxel Lin 269b95fccbcSAxel Lin static inline void soc_cleanup_card_debugfs(struct snd_soc_card *card) 270b95fccbcSAxel Lin { 271b95fccbcSAxel Lin } 2726553bf06SLars-Peter Clausen 2736553bf06SLars-Peter Clausen static inline void snd_soc_debugfs_init(void) 2746553bf06SLars-Peter Clausen { 2756553bf06SLars-Peter Clausen } 2766553bf06SLars-Peter Clausen 2776553bf06SLars-Peter Clausen static inline void snd_soc_debugfs_exit(void) 2786553bf06SLars-Peter Clausen { 2796553bf06SLars-Peter Clausen } 2806553bf06SLars-Peter Clausen 2812624d5faSMark Brown #endif 2822624d5faSMark Brown 283a0ac4411SKuninori Morimoto static int snd_soc_rtdcom_add(struct snd_soc_pcm_runtime *rtd, 284a0ac4411SKuninori Morimoto struct snd_soc_component *component) 285a0ac4411SKuninori Morimoto { 286a0ac4411SKuninori Morimoto struct snd_soc_rtdcom_list *rtdcom; 287a0ac4411SKuninori Morimoto struct snd_soc_rtdcom_list *new_rtdcom; 288a0ac4411SKuninori Morimoto 289a0ac4411SKuninori Morimoto for_each_rtdcom(rtd, rtdcom) { 290a0ac4411SKuninori Morimoto /* already connected */ 291a0ac4411SKuninori Morimoto if (rtdcom->component == component) 292a0ac4411SKuninori Morimoto return 0; 293a0ac4411SKuninori Morimoto } 294a0ac4411SKuninori Morimoto 295a0ac4411SKuninori Morimoto new_rtdcom = kmalloc(sizeof(*new_rtdcom), GFP_KERNEL); 296a0ac4411SKuninori Morimoto if (!new_rtdcom) 297a0ac4411SKuninori Morimoto return -ENOMEM; 298a0ac4411SKuninori Morimoto 299a0ac4411SKuninori Morimoto new_rtdcom->component = component; 300a0ac4411SKuninori Morimoto INIT_LIST_HEAD(&new_rtdcom->list); 301a0ac4411SKuninori Morimoto 302a0ac4411SKuninori Morimoto list_add_tail(&new_rtdcom->list, &rtd->component_list); 303a0ac4411SKuninori Morimoto 304a0ac4411SKuninori Morimoto return 0; 305a0ac4411SKuninori Morimoto } 306a0ac4411SKuninori Morimoto 307a0ac4411SKuninori Morimoto static void snd_soc_rtdcom_del_all(struct snd_soc_pcm_runtime *rtd) 308a0ac4411SKuninori Morimoto { 309a0ac4411SKuninori Morimoto struct snd_soc_rtdcom_list *rtdcom1, *rtdcom2; 310a0ac4411SKuninori Morimoto 311a0ac4411SKuninori Morimoto for_each_rtdcom_safe(rtd, rtdcom1, rtdcom2) 312a0ac4411SKuninori Morimoto kfree(rtdcom1); 313a0ac4411SKuninori Morimoto 314a0ac4411SKuninori Morimoto INIT_LIST_HEAD(&rtd->component_list); 315a0ac4411SKuninori Morimoto } 316a0ac4411SKuninori Morimoto 317a0ac4411SKuninori Morimoto struct snd_soc_component *snd_soc_rtdcom_lookup(struct snd_soc_pcm_runtime *rtd, 318a0ac4411SKuninori Morimoto const char *driver_name) 319a0ac4411SKuninori Morimoto { 320a0ac4411SKuninori Morimoto struct snd_soc_rtdcom_list *rtdcom; 321a0ac4411SKuninori Morimoto 322971da24cSKuninori Morimoto if (!driver_name) 323971da24cSKuninori Morimoto return NULL; 324971da24cSKuninori Morimoto 325a0ac4411SKuninori Morimoto for_each_rtdcom(rtd, rtdcom) { 326971da24cSKuninori Morimoto const char *component_name = rtdcom->component->driver->name; 327971da24cSKuninori Morimoto 328971da24cSKuninori Morimoto if (!component_name) 329971da24cSKuninori Morimoto continue; 330971da24cSKuninori Morimoto 331971da24cSKuninori Morimoto if ((component_name == driver_name) || 332971da24cSKuninori Morimoto strcmp(component_name, driver_name) == 0) 333a0ac4411SKuninori Morimoto return rtdcom->component; 334a0ac4411SKuninori Morimoto } 335a0ac4411SKuninori Morimoto 336a0ac4411SKuninori Morimoto return NULL; 337a0ac4411SKuninori Morimoto } 338031734b7SKuninori Morimoto EXPORT_SYMBOL_GPL(snd_soc_rtdcom_lookup); 339a0ac4411SKuninori Morimoto 34047c88fffSLiam Girdwood struct snd_pcm_substream *snd_soc_get_dai_substream(struct snd_soc_card *card, 34147c88fffSLiam Girdwood const char *dai_link, int stream) 34247c88fffSLiam Girdwood { 3431a497983SMengdong Lin struct snd_soc_pcm_runtime *rtd; 34447c88fffSLiam Girdwood 3451a497983SMengdong Lin list_for_each_entry(rtd, &card->rtd_list, list) { 3461a497983SMengdong Lin if (rtd->dai_link->no_pcm && 3471a497983SMengdong Lin !strcmp(rtd->dai_link->name, dai_link)) 3481a497983SMengdong Lin return rtd->pcm->streams[stream].substream; 34947c88fffSLiam Girdwood } 350f110bfc7SLiam Girdwood dev_dbg(card->dev, "ASoC: failed to find dai link %s\n", dai_link); 35147c88fffSLiam Girdwood return NULL; 35247c88fffSLiam Girdwood } 35347c88fffSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_get_dai_substream); 35447c88fffSLiam Girdwood 35575ab9eb6SKuninori Morimoto static const struct snd_soc_ops null_snd_soc_ops; 35675ab9eb6SKuninori Morimoto 3571a497983SMengdong Lin static struct snd_soc_pcm_runtime *soc_new_pcm_runtime( 3581a497983SMengdong Lin struct snd_soc_card *card, struct snd_soc_dai_link *dai_link) 3591a497983SMengdong Lin { 3601a497983SMengdong Lin struct snd_soc_pcm_runtime *rtd; 3611a497983SMengdong Lin 3621a497983SMengdong Lin rtd = kzalloc(sizeof(struct snd_soc_pcm_runtime), GFP_KERNEL); 3631a497983SMengdong Lin if (!rtd) 3641a497983SMengdong Lin return NULL; 3651a497983SMengdong Lin 366a0ac4411SKuninori Morimoto INIT_LIST_HEAD(&rtd->component_list); 3671a497983SMengdong Lin rtd->card = card; 3681a497983SMengdong Lin rtd->dai_link = dai_link; 36975ab9eb6SKuninori Morimoto if (!rtd->dai_link->ops) 37075ab9eb6SKuninori Morimoto rtd->dai_link->ops = &null_snd_soc_ops; 37175ab9eb6SKuninori Morimoto 3726396bb22SKees Cook rtd->codec_dais = kcalloc(dai_link->num_codecs, 3736396bb22SKees Cook sizeof(struct snd_soc_dai *), 3741a497983SMengdong Lin GFP_KERNEL); 3751a497983SMengdong Lin if (!rtd->codec_dais) { 3761a497983SMengdong Lin kfree(rtd); 3771a497983SMengdong Lin return NULL; 3781a497983SMengdong Lin } 3791a497983SMengdong Lin 3801a497983SMengdong Lin return rtd; 3811a497983SMengdong Lin } 3821a497983SMengdong Lin 3831a497983SMengdong Lin static void soc_free_pcm_runtime(struct snd_soc_pcm_runtime *rtd) 3841a497983SMengdong Lin { 3851a497983SMengdong Lin kfree(rtd->codec_dais); 386a0ac4411SKuninori Morimoto snd_soc_rtdcom_del_all(rtd); 3871a497983SMengdong Lin kfree(rtd); 3881a497983SMengdong Lin } 3891a497983SMengdong Lin 3901a497983SMengdong Lin static void soc_add_pcm_runtime(struct snd_soc_card *card, 3911a497983SMengdong Lin struct snd_soc_pcm_runtime *rtd) 3921a497983SMengdong Lin { 3931a497983SMengdong Lin list_add_tail(&rtd->list, &card->rtd_list); 3941a497983SMengdong Lin rtd->num = card->num_rtd; 3951a497983SMengdong Lin card->num_rtd++; 3961a497983SMengdong Lin } 3971a497983SMengdong Lin 3981a497983SMengdong Lin static void soc_remove_pcm_runtimes(struct snd_soc_card *card) 3991a497983SMengdong Lin { 4001a497983SMengdong Lin struct snd_soc_pcm_runtime *rtd, *_rtd; 4011a497983SMengdong Lin 4021a497983SMengdong Lin list_for_each_entry_safe(rtd, _rtd, &card->rtd_list, list) { 4031a497983SMengdong Lin list_del(&rtd->list); 4041a497983SMengdong Lin soc_free_pcm_runtime(rtd); 4051a497983SMengdong Lin } 4061a497983SMengdong Lin 4071a497983SMengdong Lin card->num_rtd = 0; 4081a497983SMengdong Lin } 4091a497983SMengdong Lin 41047c88fffSLiam Girdwood struct snd_soc_pcm_runtime *snd_soc_get_pcm_runtime(struct snd_soc_card *card, 41147c88fffSLiam Girdwood const char *dai_link) 41247c88fffSLiam Girdwood { 4131a497983SMengdong Lin struct snd_soc_pcm_runtime *rtd; 41447c88fffSLiam Girdwood 4151a497983SMengdong Lin list_for_each_entry(rtd, &card->rtd_list, list) { 4161a497983SMengdong Lin if (!strcmp(rtd->dai_link->name, dai_link)) 4171a497983SMengdong Lin return rtd; 41847c88fffSLiam Girdwood } 419f110bfc7SLiam Girdwood dev_dbg(card->dev, "ASoC: failed to find rtd %s\n", dai_link); 42047c88fffSLiam Girdwood return NULL; 42147c88fffSLiam Girdwood } 42247c88fffSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_get_pcm_runtime); 42347c88fffSLiam Girdwood 4249d58a077SRichard Fitzgerald static void codec2codec_close_delayed_work(struct work_struct *work) 4259d58a077SRichard Fitzgerald { 4269d58a077SRichard Fitzgerald /* Currently nothing to do for c2c links 4279d58a077SRichard Fitzgerald * Since c2c links are internal nodes in the DAPM graph and 4289d58a077SRichard Fitzgerald * don't interface with the outside world or application layer 4299d58a077SRichard Fitzgerald * we don't have to do any special handling on close. 4309d58a077SRichard Fitzgerald */ 4319d58a077SRichard Fitzgerald } 4329d58a077SRichard Fitzgerald 4336f8ab4acSMark Brown #ifdef CONFIG_PM_SLEEP 434db2a4165SFrank Mandarino /* powers down audio subsystem for suspend */ 4356f8ab4acSMark Brown int snd_soc_suspend(struct device *dev) 436db2a4165SFrank Mandarino { 4376f8ab4acSMark Brown struct snd_soc_card *card = dev_get_drvdata(dev); 438d9fc4063SKuninori Morimoto struct snd_soc_component *component; 4391a497983SMengdong Lin struct snd_soc_pcm_runtime *rtd; 4401a497983SMengdong Lin int i; 441db2a4165SFrank Mandarino 442c5599b87SLars-Peter Clausen /* If the card is not initialized yet there is nothing to do */ 443c5599b87SLars-Peter Clausen if (!card->instantiated) 444e3509ff0SDaniel Mack return 0; 445e3509ff0SDaniel Mack 4466ed25978SAndy Green /* Due to the resume being scheduled into a workqueue we could 4476ed25978SAndy Green * suspend before that's finished - wait for it to complete. 4486ed25978SAndy Green */ 449f0fba2adSLiam Girdwood snd_power_wait(card->snd_card, SNDRV_CTL_POWER_D0); 4506ed25978SAndy Green 4516ed25978SAndy Green /* we're going to block userspace touching us until resume completes */ 452f0fba2adSLiam Girdwood snd_power_change_state(card->snd_card, SNDRV_CTL_POWER_D3hot); 4536ed25978SAndy Green 454a00f90f9SMark Brown /* mute any active DACs */ 4551a497983SMengdong Lin list_for_each_entry(rtd, &card->rtd_list, list) { 4560b7990e3SKuninori Morimoto struct snd_soc_dai *dai; 4573efab7dcSMark Brown 4581a497983SMengdong Lin if (rtd->dai_link->ignore_suspend) 4593efab7dcSMark Brown continue; 4603efab7dcSMark Brown 4610b7990e3SKuninori Morimoto for_each_rtd_codec_dai(rtd, i, dai) { 46288bd870fSBenoit Cousson struct snd_soc_dai_driver *drv = dai->driver; 46388bd870fSBenoit Cousson 464f0fba2adSLiam Girdwood if (drv->ops->digital_mute && dai->playback_active) 465f0fba2adSLiam Girdwood drv->ops->digital_mute(dai, 1); 466db2a4165SFrank Mandarino } 46788bd870fSBenoit Cousson } 468db2a4165SFrank Mandarino 4694ccab3e7SLiam Girdwood /* suspend all pcms */ 4701a497983SMengdong Lin list_for_each_entry(rtd, &card->rtd_list, list) { 4711a497983SMengdong Lin if (rtd->dai_link->ignore_suspend) 4723efab7dcSMark Brown continue; 4733efab7dcSMark Brown 4741a497983SMengdong Lin snd_pcm_suspend_all(rtd->pcm); 4753efab7dcSMark Brown } 4764ccab3e7SLiam Girdwood 47787506549SMark Brown if (card->suspend_pre) 47870b2ac12SMark Brown card->suspend_pre(card); 479db2a4165SFrank Mandarino 4801a497983SMengdong Lin list_for_each_entry(rtd, &card->rtd_list, list) { 4811a497983SMengdong Lin struct snd_soc_dai *cpu_dai = rtd->cpu_dai; 4823efab7dcSMark Brown 4831a497983SMengdong Lin if (rtd->dai_link->ignore_suspend) 4843efab7dcSMark Brown continue; 4853efab7dcSMark Brown 486bc263214SLars-Peter Clausen if (cpu_dai->driver->suspend && !cpu_dai->driver->bus_control) 487f0fba2adSLiam Girdwood cpu_dai->driver->suspend(cpu_dai); 488db2a4165SFrank Mandarino } 489db2a4165SFrank Mandarino 49037660b6dSLars-Peter Clausen /* close any waiting streams */ 4911a497983SMengdong Lin list_for_each_entry(rtd, &card->rtd_list, list) 4921a497983SMengdong Lin flush_delayed_work(&rtd->delayed_work); 493db2a4165SFrank Mandarino 4941a497983SMengdong Lin list_for_each_entry(rtd, &card->rtd_list, list) { 4953efab7dcSMark Brown 4961a497983SMengdong Lin if (rtd->dai_link->ignore_suspend) 4973efab7dcSMark Brown continue; 4983efab7dcSMark Brown 4991a497983SMengdong Lin snd_soc_dapm_stream_event(rtd, 5007bd3a6f3SMark Brown SNDRV_PCM_STREAM_PLAYBACK, 501db2a4165SFrank Mandarino SND_SOC_DAPM_STREAM_SUSPEND); 502f0fba2adSLiam Girdwood 5031a497983SMengdong Lin snd_soc_dapm_stream_event(rtd, 5047bd3a6f3SMark Brown SNDRV_PCM_STREAM_CAPTURE, 505db2a4165SFrank Mandarino SND_SOC_DAPM_STREAM_SUSPEND); 506db2a4165SFrank Mandarino } 507db2a4165SFrank Mandarino 5088be4da29SLars-Peter Clausen /* Recheck all endpoints too, their state is affected by suspend */ 5098be4da29SLars-Peter Clausen dapm_mark_endpoints_dirty(card); 510e2d32ff6SMark Brown snd_soc_dapm_sync(&card->dapm); 511e2d32ff6SMark Brown 5129178feb4SKuninori Morimoto /* suspend all COMPONENTs */ 513d9fc4063SKuninori Morimoto list_for_each_entry(component, &card->component_dev_list, card_list) { 514d9fc4063SKuninori Morimoto struct snd_soc_dapm_context *dapm = snd_soc_component_get_dapm(component); 515d9fc4063SKuninori Morimoto 5169178feb4SKuninori Morimoto /* If there are paths active then the COMPONENT will be held with 5171547aba9SMark Brown * bias _ON and should not be suspended. */ 5189178feb4SKuninori Morimoto if (!component->suspended) { 5194890140fSLars-Peter Clausen switch (snd_soc_dapm_get_bias_level(dapm)) { 5201547aba9SMark Brown case SND_SOC_BIAS_STANDBY: 521125a25daSMark Brown /* 5229178feb4SKuninori Morimoto * If the COMPONENT is capable of idle 523125a25daSMark Brown * bias off then being in STANDBY 524125a25daSMark Brown * means it's doing something, 525125a25daSMark Brown * otherwise fall through. 526125a25daSMark Brown */ 5274890140fSLars-Peter Clausen if (dapm->idle_bias_off) { 5289178feb4SKuninori Morimoto dev_dbg(component->dev, 52910e8aa9aSMichał Mirosław "ASoC: idle_bias_off CODEC on over suspend\n"); 530125a25daSMark Brown break; 531125a25daSMark Brown } 5321a12d5dcSGustavo A. R. Silva /* fall through */ 533a8093297SLars-Peter Clausen 5341547aba9SMark Brown case SND_SOC_BIAS_OFF: 535999f7f5aSKuninori Morimoto if (component->driver->suspend) 536999f7f5aSKuninori Morimoto component->driver->suspend(component); 5379178feb4SKuninori Morimoto component->suspended = 1; 5389178feb4SKuninori Morimoto if (component->regmap) 5399178feb4SKuninori Morimoto regcache_mark_dirty(component->regmap); 540988e8cc4SNicolin Chen /* deactivate pins to sleep state */ 5419178feb4SKuninori Morimoto pinctrl_pm_select_sleep_state(component->dev); 5421547aba9SMark Brown break; 5431547aba9SMark Brown default: 5449178feb4SKuninori Morimoto dev_dbg(component->dev, 5459178feb4SKuninori Morimoto "ASoC: COMPONENT is on over suspend\n"); 5461547aba9SMark Brown break; 5471547aba9SMark Brown } 5481547aba9SMark Brown } 549f0fba2adSLiam Girdwood } 550db2a4165SFrank Mandarino 5511a497983SMengdong Lin list_for_each_entry(rtd, &card->rtd_list, list) { 5521a497983SMengdong Lin struct snd_soc_dai *cpu_dai = rtd->cpu_dai; 5533efab7dcSMark Brown 5541a497983SMengdong Lin if (rtd->dai_link->ignore_suspend) 5553efab7dcSMark Brown continue; 5563efab7dcSMark Brown 557bc263214SLars-Peter Clausen if (cpu_dai->driver->suspend && cpu_dai->driver->bus_control) 558f0fba2adSLiam Girdwood cpu_dai->driver->suspend(cpu_dai); 559988e8cc4SNicolin Chen 560988e8cc4SNicolin Chen /* deactivate pins to sleep state */ 561988e8cc4SNicolin Chen pinctrl_pm_select_sleep_state(cpu_dai->dev); 562db2a4165SFrank Mandarino } 563db2a4165SFrank Mandarino 56487506549SMark Brown if (card->suspend_post) 56570b2ac12SMark Brown card->suspend_post(card); 566db2a4165SFrank Mandarino 567db2a4165SFrank Mandarino return 0; 568db2a4165SFrank Mandarino } 5696f8ab4acSMark Brown EXPORT_SYMBOL_GPL(snd_soc_suspend); 570db2a4165SFrank Mandarino 5716ed25978SAndy Green /* deferred resume work, so resume can complete before we finished 5726ed25978SAndy Green * setting our codec back up, which can be very slow on I2C 5736ed25978SAndy Green */ 5746ed25978SAndy Green static void soc_resume_deferred(struct work_struct *work) 575db2a4165SFrank Mandarino { 576f0fba2adSLiam Girdwood struct snd_soc_card *card = 577f0fba2adSLiam Girdwood container_of(work, struct snd_soc_card, deferred_resume_work); 5781a497983SMengdong Lin struct snd_soc_pcm_runtime *rtd; 579d9fc4063SKuninori Morimoto struct snd_soc_component *component; 5801a497983SMengdong Lin int i; 581db2a4165SFrank Mandarino 5826ed25978SAndy Green /* our power state is still SNDRV_CTL_POWER_D3hot from suspend time, 5836ed25978SAndy Green * so userspace apps are blocked from touching us 5846ed25978SAndy Green */ 5856ed25978SAndy Green 586f110bfc7SLiam Girdwood dev_dbg(card->dev, "ASoC: starting resume work\n"); 5876ed25978SAndy Green 5889949788bSMark Brown /* Bring us up into D2 so that DAPM starts enabling things */ 589f0fba2adSLiam Girdwood snd_power_change_state(card->snd_card, SNDRV_CTL_POWER_D2); 5909949788bSMark Brown 59187506549SMark Brown if (card->resume_pre) 59270b2ac12SMark Brown card->resume_pre(card); 593db2a4165SFrank Mandarino 594bc263214SLars-Peter Clausen /* resume control bus DAIs */ 5951a497983SMengdong Lin list_for_each_entry(rtd, &card->rtd_list, list) { 5961a497983SMengdong Lin struct snd_soc_dai *cpu_dai = rtd->cpu_dai; 5973efab7dcSMark Brown 5981a497983SMengdong Lin if (rtd->dai_link->ignore_suspend) 5993efab7dcSMark Brown continue; 6003efab7dcSMark Brown 601bc263214SLars-Peter Clausen if (cpu_dai->driver->resume && cpu_dai->driver->bus_control) 602f0fba2adSLiam Girdwood cpu_dai->driver->resume(cpu_dai); 603db2a4165SFrank Mandarino } 604db2a4165SFrank Mandarino 605d9fc4063SKuninori Morimoto list_for_each_entry(component, &card->component_dev_list, card_list) { 6069178feb4SKuninori Morimoto if (component->suspended) { 607999f7f5aSKuninori Morimoto if (component->driver->resume) 608999f7f5aSKuninori Morimoto component->driver->resume(component); 6099178feb4SKuninori Morimoto component->suspended = 0; 6101547aba9SMark Brown } 611f0fba2adSLiam Girdwood } 612db2a4165SFrank Mandarino 6131a497983SMengdong Lin list_for_each_entry(rtd, &card->rtd_list, list) { 6143efab7dcSMark Brown 6151a497983SMengdong Lin if (rtd->dai_link->ignore_suspend) 6163efab7dcSMark Brown continue; 6173efab7dcSMark Brown 6181a497983SMengdong Lin snd_soc_dapm_stream_event(rtd, 619d9b0951bSLiam Girdwood SNDRV_PCM_STREAM_PLAYBACK, 620db2a4165SFrank Mandarino SND_SOC_DAPM_STREAM_RESUME); 621f0fba2adSLiam Girdwood 6221a497983SMengdong Lin snd_soc_dapm_stream_event(rtd, 623d9b0951bSLiam Girdwood SNDRV_PCM_STREAM_CAPTURE, 624db2a4165SFrank Mandarino SND_SOC_DAPM_STREAM_RESUME); 625db2a4165SFrank Mandarino } 626db2a4165SFrank Mandarino 6273ff3f64bSMark Brown /* unmute any active DACs */ 6281a497983SMengdong Lin list_for_each_entry(rtd, &card->rtd_list, list) { 6290b7990e3SKuninori Morimoto struct snd_soc_dai *dai; 6303efab7dcSMark Brown 6311a497983SMengdong Lin if (rtd->dai_link->ignore_suspend) 6323efab7dcSMark Brown continue; 6333efab7dcSMark Brown 6340b7990e3SKuninori Morimoto for_each_rtd_codec_dai(rtd, i, dai) { 63588bd870fSBenoit Cousson struct snd_soc_dai_driver *drv = dai->driver; 63688bd870fSBenoit Cousson 637f0fba2adSLiam Girdwood if (drv->ops->digital_mute && dai->playback_active) 638f0fba2adSLiam Girdwood drv->ops->digital_mute(dai, 0); 639db2a4165SFrank Mandarino } 64088bd870fSBenoit Cousson } 641db2a4165SFrank Mandarino 6421a497983SMengdong Lin list_for_each_entry(rtd, &card->rtd_list, list) { 6431a497983SMengdong Lin struct snd_soc_dai *cpu_dai = rtd->cpu_dai; 6443efab7dcSMark Brown 6451a497983SMengdong Lin if (rtd->dai_link->ignore_suspend) 6463efab7dcSMark Brown continue; 6473efab7dcSMark Brown 648bc263214SLars-Peter Clausen if (cpu_dai->driver->resume && !cpu_dai->driver->bus_control) 649f0fba2adSLiam Girdwood cpu_dai->driver->resume(cpu_dai); 650db2a4165SFrank Mandarino } 651db2a4165SFrank Mandarino 65287506549SMark Brown if (card->resume_post) 65370b2ac12SMark Brown card->resume_post(card); 654db2a4165SFrank Mandarino 655f110bfc7SLiam Girdwood dev_dbg(card->dev, "ASoC: resume work completed\n"); 6566ed25978SAndy Green 6578be4da29SLars-Peter Clausen /* Recheck all endpoints too, their state is affected by suspend */ 6588be4da29SLars-Peter Clausen dapm_mark_endpoints_dirty(card); 659e2d32ff6SMark Brown snd_soc_dapm_sync(&card->dapm); 6601a7aaa58SJeeja KP 6611a7aaa58SJeeja KP /* userspace can access us now we are back as we were before */ 6621a7aaa58SJeeja KP snd_power_change_state(card->snd_card, SNDRV_CTL_POWER_D0); 6636ed25978SAndy Green } 6646ed25978SAndy Green 6656ed25978SAndy Green /* powers up audio subsystem after a suspend */ 6666f8ab4acSMark Brown int snd_soc_resume(struct device *dev) 6676ed25978SAndy Green { 6686f8ab4acSMark Brown struct snd_soc_card *card = dev_get_drvdata(dev); 669bc263214SLars-Peter Clausen bool bus_control = false; 6701a497983SMengdong Lin struct snd_soc_pcm_runtime *rtd; 671b9dd94a8SPeter Ujfalusi 672c5599b87SLars-Peter Clausen /* If the card is not initialized yet there is nothing to do */ 673c5599b87SLars-Peter Clausen if (!card->instantiated) 6745ff1ddf2SEric Miao return 0; 6755ff1ddf2SEric Miao 676988e8cc4SNicolin Chen /* activate pins from sleep state */ 6771a497983SMengdong Lin list_for_each_entry(rtd, &card->rtd_list, list) { 6780b7990e3SKuninori Morimoto struct snd_soc_dai *codec_dai; 67988bd870fSBenoit Cousson struct snd_soc_dai *cpu_dai = rtd->cpu_dai; 68088bd870fSBenoit Cousson int j; 68188bd870fSBenoit Cousson 682988e8cc4SNicolin Chen if (cpu_dai->active) 683988e8cc4SNicolin Chen pinctrl_pm_select_default_state(cpu_dai->dev); 68488bd870fSBenoit Cousson 6850b7990e3SKuninori Morimoto for_each_rtd_codec_dai(rtd, j, codec_dai) { 686988e8cc4SNicolin Chen if (codec_dai->active) 687988e8cc4SNicolin Chen pinctrl_pm_select_default_state(codec_dai->dev); 688988e8cc4SNicolin Chen } 68988bd870fSBenoit Cousson } 690988e8cc4SNicolin Chen 691bc263214SLars-Peter Clausen /* 692bc263214SLars-Peter Clausen * DAIs that also act as the control bus master might have other drivers 693bc263214SLars-Peter Clausen * hanging off them so need to resume immediately. Other drivers don't 694bc263214SLars-Peter Clausen * have that problem and may take a substantial amount of time to resume 69564ab9baaSMark Brown * due to I/O costs and anti-pop so handle them out of line. 69664ab9baaSMark Brown */ 6971a497983SMengdong Lin list_for_each_entry(rtd, &card->rtd_list, list) { 6981a497983SMengdong Lin struct snd_soc_dai *cpu_dai = rtd->cpu_dai; 699bc263214SLars-Peter Clausen bus_control |= cpu_dai->driver->bus_control; 70082e14e8bSStephen Warren } 701bc263214SLars-Peter Clausen if (bus_control) { 702bc263214SLars-Peter Clausen dev_dbg(dev, "ASoC: Resuming control bus master immediately\n"); 70364ab9baaSMark Brown soc_resume_deferred(&card->deferred_resume_work); 70464ab9baaSMark Brown } else { 705f110bfc7SLiam Girdwood dev_dbg(dev, "ASoC: Scheduling resume work\n"); 7066308419aSMark Brown if (!schedule_work(&card->deferred_resume_work)) 707f110bfc7SLiam Girdwood dev_err(dev, "ASoC: resume work item may be lost\n"); 708f0fba2adSLiam Girdwood } 7096ed25978SAndy Green 710db2a4165SFrank Mandarino return 0; 711db2a4165SFrank Mandarino } 7126f8ab4acSMark Brown EXPORT_SYMBOL_GPL(snd_soc_resume); 713db2a4165SFrank Mandarino #else 7146f8ab4acSMark Brown #define snd_soc_suspend NULL 7156f8ab4acSMark Brown #define snd_soc_resume NULL 716db2a4165SFrank Mandarino #endif 717db2a4165SFrank Mandarino 71885e7652dSLars-Peter Clausen static const struct snd_soc_dai_ops null_dai_ops = { 71902a06d30SBarry Song }; 72002a06d30SBarry Song 72165d9361fSLars-Peter Clausen static struct snd_soc_component *soc_find_component( 72265d9361fSLars-Peter Clausen const struct device_node *of_node, const char *name) 72312023a9aSMisael Lopez Cruz { 72465d9361fSLars-Peter Clausen struct snd_soc_component *component; 72512023a9aSMisael Lopez Cruz 72634e81ab4SLars-Peter Clausen lockdep_assert_held(&client_mutex); 72734e81ab4SLars-Peter Clausen 72865d9361fSLars-Peter Clausen list_for_each_entry(component, &component_list, list) { 72965d9361fSLars-Peter Clausen if (of_node) { 73065d9361fSLars-Peter Clausen if (component->dev->of_node == of_node) 73165d9361fSLars-Peter Clausen return component; 73265d9361fSLars-Peter Clausen } else if (strcmp(component->name, name) == 0) { 73365d9361fSLars-Peter Clausen return component; 73412023a9aSMisael Lopez Cruz } 73512023a9aSMisael Lopez Cruz } 73612023a9aSMisael Lopez Cruz 73712023a9aSMisael Lopez Cruz return NULL; 73812023a9aSMisael Lopez Cruz } 73912023a9aSMisael Lopez Cruz 740fbb88b5cSMengdong Lin /** 741fbb88b5cSMengdong Lin * snd_soc_find_dai - Find a registered DAI 742fbb88b5cSMengdong Lin * 7434958471bSJeffy Chen * @dlc: name of the DAI or the DAI driver and optional component info to match 744fbb88b5cSMengdong Lin * 745ad61dd30SStephen Boyd * This function will search all registered components and their DAIs to 746fbb88b5cSMengdong Lin * find the DAI of the same name. The component's of_node and name 747fbb88b5cSMengdong Lin * should also match if being specified. 748fbb88b5cSMengdong Lin * 749fbb88b5cSMengdong Lin * Return: pointer of DAI, or NULL if not found. 750fbb88b5cSMengdong Lin */ 751305e9020SMengdong Lin struct snd_soc_dai *snd_soc_find_dai( 75214621c7eSLars-Peter Clausen const struct snd_soc_dai_link_component *dlc) 75312023a9aSMisael Lopez Cruz { 75414621c7eSLars-Peter Clausen struct snd_soc_component *component; 75514621c7eSLars-Peter Clausen struct snd_soc_dai *dai; 7563e0aa8d8SJyri Sarha struct device_node *component_of_node; 75712023a9aSMisael Lopez Cruz 75834e81ab4SLars-Peter Clausen lockdep_assert_held(&client_mutex); 75934e81ab4SLars-Peter Clausen 76014621c7eSLars-Peter Clausen /* Find CPU DAI from registered DAIs*/ 76114621c7eSLars-Peter Clausen list_for_each_entry(component, &component_list, list) { 7623e0aa8d8SJyri Sarha component_of_node = component->dev->of_node; 7633e0aa8d8SJyri Sarha if (!component_of_node && component->dev->parent) 7643e0aa8d8SJyri Sarha component_of_node = component->dev->parent->of_node; 7653e0aa8d8SJyri Sarha 7663e0aa8d8SJyri Sarha if (dlc->of_node && component_of_node != dlc->of_node) 76712023a9aSMisael Lopez Cruz continue; 7681ffae361SLars-Peter Clausen if (dlc->name && strcmp(component->name, dlc->name)) 76912023a9aSMisael Lopez Cruz continue; 77014621c7eSLars-Peter Clausen list_for_each_entry(dai, &component->dai_list, list) { 7714958471bSJeffy Chen if (dlc->dai_name && strcmp(dai->name, dlc->dai_name) 7726a6dafdaSJeffy Chen && (!dai->driver->name 7736a6dafdaSJeffy Chen || strcmp(dai->driver->name, dlc->dai_name))) 77414621c7eSLars-Peter Clausen continue; 77512023a9aSMisael Lopez Cruz 77614621c7eSLars-Peter Clausen return dai; 77712023a9aSMisael Lopez Cruz } 77812023a9aSMisael Lopez Cruz } 77912023a9aSMisael Lopez Cruz 78012023a9aSMisael Lopez Cruz return NULL; 78112023a9aSMisael Lopez Cruz } 782305e9020SMengdong Lin EXPORT_SYMBOL_GPL(snd_soc_find_dai); 78312023a9aSMisael Lopez Cruz 78417fb1755SMengdong Lin 78517fb1755SMengdong Lin /** 78617fb1755SMengdong Lin * snd_soc_find_dai_link - Find a DAI link 78717fb1755SMengdong Lin * 78817fb1755SMengdong Lin * @card: soc card 78917fb1755SMengdong Lin * @id: DAI link ID to match 79017fb1755SMengdong Lin * @name: DAI link name to match, optional 7918abab35fSCharles Keepax * @stream_name: DAI link stream name to match, optional 79217fb1755SMengdong Lin * 79317fb1755SMengdong Lin * This function will search all existing DAI links of the soc card to 79417fb1755SMengdong Lin * find the link of the same ID. Since DAI links may not have their 79517fb1755SMengdong Lin * unique ID, so name and stream name should also match if being 79617fb1755SMengdong Lin * specified. 79717fb1755SMengdong Lin * 79817fb1755SMengdong Lin * Return: pointer of DAI link, or NULL if not found. 79917fb1755SMengdong Lin */ 80017fb1755SMengdong Lin struct snd_soc_dai_link *snd_soc_find_dai_link(struct snd_soc_card *card, 80117fb1755SMengdong Lin int id, const char *name, 80217fb1755SMengdong Lin const char *stream_name) 80317fb1755SMengdong Lin { 80417fb1755SMengdong Lin struct snd_soc_dai_link *link, *_link; 80517fb1755SMengdong Lin 80617fb1755SMengdong Lin lockdep_assert_held(&client_mutex); 80717fb1755SMengdong Lin 80817fb1755SMengdong Lin list_for_each_entry_safe(link, _link, &card->dai_link_list, list) { 80917fb1755SMengdong Lin if (link->id != id) 81017fb1755SMengdong Lin continue; 81117fb1755SMengdong Lin 81217fb1755SMengdong Lin if (name && (!link->name || strcmp(name, link->name))) 81317fb1755SMengdong Lin continue; 81417fb1755SMengdong Lin 81517fb1755SMengdong Lin if (stream_name && (!link->stream_name 81617fb1755SMengdong Lin || strcmp(stream_name, link->stream_name))) 81717fb1755SMengdong Lin continue; 81817fb1755SMengdong Lin 81917fb1755SMengdong Lin return link; 82017fb1755SMengdong Lin } 82117fb1755SMengdong Lin 82217fb1755SMengdong Lin return NULL; 82317fb1755SMengdong Lin } 82417fb1755SMengdong Lin EXPORT_SYMBOL_GPL(snd_soc_find_dai_link); 82517fb1755SMengdong Lin 82649a5ba1cSMengdong Lin static bool soc_is_dai_link_bound(struct snd_soc_card *card, 82749a5ba1cSMengdong Lin struct snd_soc_dai_link *dai_link) 828db2a4165SFrank Mandarino { 82949a5ba1cSMengdong Lin struct snd_soc_pcm_runtime *rtd; 83049a5ba1cSMengdong Lin 83149a5ba1cSMengdong Lin list_for_each_entry(rtd, &card->rtd_list, list) { 83249a5ba1cSMengdong Lin if (rtd->dai_link == dai_link) 83349a5ba1cSMengdong Lin return true; 83449a5ba1cSMengdong Lin } 83549a5ba1cSMengdong Lin 83649a5ba1cSMengdong Lin return false; 83749a5ba1cSMengdong Lin } 83849a5ba1cSMengdong Lin 8396f2f1ff0SMengdong Lin static int soc_bind_dai_link(struct snd_soc_card *card, 8406f2f1ff0SMengdong Lin struct snd_soc_dai_link *dai_link) 841db2a4165SFrank Mandarino { 8421a497983SMengdong Lin struct snd_soc_pcm_runtime *rtd; 84388bd870fSBenoit Cousson struct snd_soc_dai_link_component *codecs = dai_link->codecs; 84414621c7eSLars-Peter Clausen struct snd_soc_dai_link_component cpu_dai_component; 84590be711eSKuninori Morimoto struct snd_soc_component *component; 8461a497983SMengdong Lin struct snd_soc_dai **codec_dais; 84706859fcaSCharles Keepax struct device_node *platform_of_node; 848848dd8beSMark Brown const char *platform_name; 84988bd870fSBenoit Cousson int i; 850db2a4165SFrank Mandarino 851a655de80SLiam Girdwood if (dai_link->ignore) 852a655de80SLiam Girdwood return 0; 853a655de80SLiam Girdwood 8546f2f1ff0SMengdong Lin dev_dbg(card->dev, "ASoC: binding %s\n", dai_link->name); 8556308419aSMark Brown 85649a5ba1cSMengdong Lin if (soc_is_dai_link_bound(card, dai_link)) { 85749a5ba1cSMengdong Lin dev_dbg(card->dev, "ASoC: dai link %s already bound\n", 85849a5ba1cSMengdong Lin dai_link->name); 85949a5ba1cSMengdong Lin return 0; 86049a5ba1cSMengdong Lin } 861db2a4165SFrank Mandarino 862513cb311SSudip Mukherjee rtd = soc_new_pcm_runtime(card, dai_link); 863513cb311SSudip Mukherjee if (!rtd) 864513cb311SSudip Mukherjee return -ENOMEM; 865513cb311SSudip Mukherjee 86614621c7eSLars-Peter Clausen cpu_dai_component.name = dai_link->cpu_name; 86714621c7eSLars-Peter Clausen cpu_dai_component.of_node = dai_link->cpu_of_node; 86814621c7eSLars-Peter Clausen cpu_dai_component.dai_name = dai_link->cpu_dai_name; 86914621c7eSLars-Peter Clausen rtd->cpu_dai = snd_soc_find_dai(&cpu_dai_component); 870b19e6e7bSMark Brown if (!rtd->cpu_dai) { 8716b490879SMartin Hundebøll dev_info(card->dev, "ASoC: CPU DAI %s not registered\n", 872f0fba2adSLiam Girdwood dai_link->cpu_dai_name); 8731a497983SMengdong Lin goto _err_defer; 874c5af3a2eSMark Brown } 87590be711eSKuninori Morimoto snd_soc_rtdcom_add(rtd, rtd->cpu_dai->component); 876c5af3a2eSMark Brown 87788bd870fSBenoit Cousson rtd->num_codecs = dai_link->num_codecs; 87888bd870fSBenoit Cousson 87988bd870fSBenoit Cousson /* Find CODEC from registered CODECs */ 8800b7990e3SKuninori Morimoto /* we can use for_each_rtd_codec_dai() after this */ 8811a497983SMengdong Lin codec_dais = rtd->codec_dais; 88288bd870fSBenoit Cousson for (i = 0; i < rtd->num_codecs; i++) { 88314621c7eSLars-Peter Clausen codec_dais[i] = snd_soc_find_dai(&codecs[i]); 88488bd870fSBenoit Cousson if (!codec_dais[i]) { 88512023a9aSMisael Lopez Cruz dev_err(card->dev, "ASoC: CODEC DAI %s not registered\n", 88688bd870fSBenoit Cousson codecs[i].dai_name); 8871a497983SMengdong Lin goto _err_defer; 88812023a9aSMisael Lopez Cruz } 88990be711eSKuninori Morimoto snd_soc_rtdcom_add(rtd, codec_dais[i]->component); 89088bd870fSBenoit Cousson } 89188bd870fSBenoit Cousson 89288bd870fSBenoit Cousson /* Single codec links expect codec and codec_dai in runtime data */ 89388bd870fSBenoit Cousson rtd->codec_dai = codec_dais[0]; 89412023a9aSMisael Lopez Cruz 895848dd8beSMark Brown /* if there's no platform we match on the empty platform */ 896daecf46eSKuninori Morimoto platform_name = dai_link->platform->name; 897daecf46eSKuninori Morimoto if (!platform_name && !dai_link->platform->of_node) 898848dd8beSMark Brown platform_name = "snd-soc-dummy"; 899848dd8beSMark Brown 900b19e6e7bSMark Brown /* find one from the set of registered platforms */ 90190be711eSKuninori Morimoto list_for_each_entry(component, &component_list, list) { 90290be711eSKuninori Morimoto platform_of_node = component->dev->of_node; 90390be711eSKuninori Morimoto if (!platform_of_node && component->dev->parent->of_node) 90490be711eSKuninori Morimoto platform_of_node = component->dev->parent->of_node; 90590be711eSKuninori Morimoto 906daecf46eSKuninori Morimoto if (dai_link->platform->of_node) { 907daecf46eSKuninori Morimoto if (platform_of_node != dai_link->platform->of_node) 90890be711eSKuninori Morimoto continue; 90990be711eSKuninori Morimoto } else { 91090be711eSKuninori Morimoto if (strcmp(component->name, platform_name)) 91190be711eSKuninori Morimoto continue; 91290be711eSKuninori Morimoto } 91390be711eSKuninori Morimoto 91490be711eSKuninori Morimoto snd_soc_rtdcom_add(rtd, component); 91590be711eSKuninori Morimoto } 91690be711eSKuninori Morimoto 9171a497983SMengdong Lin soc_add_pcm_runtime(card, rtd); 918b19e6e7bSMark Brown return 0; 9191a497983SMengdong Lin 9201a497983SMengdong Lin _err_defer: 9211a497983SMengdong Lin soc_free_pcm_runtime(rtd); 9221a497983SMengdong Lin return -EPROBE_DEFER; 9236b05eda6SMark Brown } 9246b05eda6SMark Brown 925f1d45cc3SLars-Peter Clausen static void soc_remove_component(struct snd_soc_component *component) 926d12cd198SStephen Warren { 927abd31b32SLars-Peter Clausen if (!component->card) 92870090bbbSLars-Peter Clausen return; 929d12cd198SStephen Warren 930d9fc4063SKuninori Morimoto list_del(&component->card_list); 931d12cd198SStephen Warren 932999f7f5aSKuninori Morimoto if (component->driver->remove) 933999f7f5aSKuninori Morimoto component->driver->remove(component); 934d12cd198SStephen Warren 935f1d45cc3SLars-Peter Clausen snd_soc_dapm_free(snd_soc_component_get_dapm(component)); 936d12cd198SStephen Warren 937f1d45cc3SLars-Peter Clausen soc_cleanup_component_debugfs(component); 938abd31b32SLars-Peter Clausen component->card = NULL; 939f1d45cc3SLars-Peter Clausen module_put(component->dev->driver->owner); 940d12cd198SStephen Warren } 941d12cd198SStephen Warren 942e60cd14fSLars-Peter Clausen static void soc_remove_dai(struct snd_soc_dai *dai, int order) 943589c3563SJarkko Nikula { 944589c3563SJarkko Nikula int err; 945589c3563SJarkko Nikula 9462eda3cb1SKuninori Morimoto if (!dai || !dai->probed || 9472eda3cb1SKuninori Morimoto dai->driver->remove_order != order) 9482eda3cb1SKuninori Morimoto return; 9492eda3cb1SKuninori Morimoto 950e60cd14fSLars-Peter Clausen if (dai->driver->remove) { 951e60cd14fSLars-Peter Clausen err = dai->driver->remove(dai); 952589c3563SJarkko Nikula if (err < 0) 953e60cd14fSLars-Peter Clausen dev_err(dai->dev, 954b0aa88afSMisael Lopez Cruz "ASoC: failed to remove %s: %d\n", 955e60cd14fSLars-Peter Clausen dai->name, err); 956b0aa88afSMisael Lopez Cruz } 957e60cd14fSLars-Peter Clausen dai->probed = 0; 958b0aa88afSMisael Lopez Cruz } 959b0aa88afSMisael Lopez Cruz 9601a497983SMengdong Lin static void soc_remove_link_dais(struct snd_soc_card *card, 9611a497983SMengdong Lin struct snd_soc_pcm_runtime *rtd, int order) 962f0fba2adSLiam Girdwood { 963e60cd14fSLars-Peter Clausen int i; 9640b7990e3SKuninori Morimoto struct snd_soc_dai *codec_dai; 965f0fba2adSLiam Girdwood 966f0fba2adSLiam Girdwood /* unregister the rtd device */ 967f0fba2adSLiam Girdwood if (rtd->dev_registered) { 96836ae1a96SMark Brown device_unregister(rtd->dev); 969f0fba2adSLiam Girdwood rtd->dev_registered = 0; 97002a06d30SBarry Song } 97102a06d30SBarry Song 972f0fba2adSLiam Girdwood /* remove the CODEC DAI */ 9730b7990e3SKuninori Morimoto for_each_rtd_codec_dai(rtd, i, codec_dai) 9740b7990e3SKuninori Morimoto soc_remove_dai(codec_dai, order); 975f0fba2adSLiam Girdwood 976e60cd14fSLars-Peter Clausen soc_remove_dai(rtd->cpu_dai, order); 977f0fba2adSLiam Girdwood } 978f0fba2adSLiam Girdwood 9791a497983SMengdong Lin static void soc_remove_link_components(struct snd_soc_card *card, 9801a497983SMengdong Lin struct snd_soc_pcm_runtime *rtd, int order) 98162ae68faSStephen Warren { 98261aca564SLars-Peter Clausen struct snd_soc_component *component; 98390be711eSKuninori Morimoto struct snd_soc_rtdcom_list *rtdcom; 98462ae68faSStephen Warren 98590be711eSKuninori Morimoto for_each_rtdcom(rtd, rtdcom) { 98690be711eSKuninori Morimoto component = rtdcom->component; 98762ae68faSStephen Warren 98870090bbbSLars-Peter Clausen if (component->driver->remove_order == order) 98961aca564SLars-Peter Clausen soc_remove_component(component); 99062ae68faSStephen Warren } 99162ae68faSStephen Warren } 99262ae68faSStephen Warren 9930671fd8eSKuninori Morimoto static void soc_remove_dai_links(struct snd_soc_card *card) 9940671fd8eSKuninori Morimoto { 9951a497983SMengdong Lin int order; 9961a497983SMengdong Lin struct snd_soc_pcm_runtime *rtd; 997f8f80361SMengdong Lin struct snd_soc_dai_link *link, *_link; 9980671fd8eSKuninori Morimoto 9990168bf0dSLiam Girdwood for (order = SND_SOC_COMP_ORDER_FIRST; order <= SND_SOC_COMP_ORDER_LAST; 10000168bf0dSLiam Girdwood order++) { 10011a497983SMengdong Lin list_for_each_entry(rtd, &card->rtd_list, list) 10021a497983SMengdong Lin soc_remove_link_dais(card, rtd, order); 10030168bf0dSLiam Girdwood } 100462ae68faSStephen Warren 100562ae68faSStephen Warren for (order = SND_SOC_COMP_ORDER_FIRST; order <= SND_SOC_COMP_ORDER_LAST; 100662ae68faSStephen Warren order++) { 10071a497983SMengdong Lin list_for_each_entry(rtd, &card->rtd_list, list) 10081a497983SMengdong Lin soc_remove_link_components(card, rtd, order); 100962ae68faSStephen Warren } 101062ae68faSStephen Warren 1011f8f80361SMengdong Lin list_for_each_entry_safe(link, _link, &card->dai_link_list, list) { 1012f8f80361SMengdong Lin if (link->dobj.type == SND_SOC_DOBJ_DAI_LINK) 1013f8f80361SMengdong Lin dev_warn(card->dev, "Topology forgot to remove link %s?\n", 1014f8f80361SMengdong Lin link->name); 1015f8f80361SMengdong Lin 1016f8f80361SMengdong Lin list_del(&link->list); 10170671fd8eSKuninori Morimoto } 10180671fd8eSKuninori Morimoto } 10190671fd8eSKuninori Morimoto 1020daecf46eSKuninori Morimoto static int snd_soc_init_platform(struct snd_soc_card *card, 1021daecf46eSKuninori Morimoto struct snd_soc_dai_link *dai_link) 1022daecf46eSKuninori Morimoto { 1023daecf46eSKuninori Morimoto /* 1024daecf46eSKuninori Morimoto * FIXME 1025daecf46eSKuninori Morimoto * 1026daecf46eSKuninori Morimoto * this function should be removed in the future 1027daecf46eSKuninori Morimoto */ 1028daecf46eSKuninori Morimoto /* convert Legacy platform link */ 1029daecf46eSKuninori Morimoto if (dai_link->platform) 1030daecf46eSKuninori Morimoto return 0; 1031daecf46eSKuninori Morimoto 1032daecf46eSKuninori Morimoto dai_link->platform = devm_kzalloc(card->dev, 1033daecf46eSKuninori Morimoto sizeof(struct snd_soc_dai_link_component), 1034daecf46eSKuninori Morimoto GFP_KERNEL); 1035daecf46eSKuninori Morimoto if (!dai_link->platform) 1036daecf46eSKuninori Morimoto return -ENOMEM; 1037daecf46eSKuninori Morimoto 1038daecf46eSKuninori Morimoto dai_link->platform->name = dai_link->platform_name; 1039daecf46eSKuninori Morimoto dai_link->platform->of_node = dai_link->platform_of_node; 1040daecf46eSKuninori Morimoto dai_link->platform->dai_name = NULL; 1041daecf46eSKuninori Morimoto 1042daecf46eSKuninori Morimoto return 0; 1043daecf46eSKuninori Morimoto } 1044daecf46eSKuninori Morimoto 1045923c5e61SMengdong Lin static int snd_soc_init_multicodec(struct snd_soc_card *card, 1046923c5e61SMengdong Lin struct snd_soc_dai_link *dai_link) 1047923c5e61SMengdong Lin { 1048923c5e61SMengdong Lin /* Legacy codec/codec_dai link is a single entry in multicodec */ 1049923c5e61SMengdong Lin if (dai_link->codec_name || dai_link->codec_of_node || 1050923c5e61SMengdong Lin dai_link->codec_dai_name) { 1051923c5e61SMengdong Lin dai_link->num_codecs = 1; 1052923c5e61SMengdong Lin 1053923c5e61SMengdong Lin dai_link->codecs = devm_kzalloc(card->dev, 1054923c5e61SMengdong Lin sizeof(struct snd_soc_dai_link_component), 1055923c5e61SMengdong Lin GFP_KERNEL); 1056923c5e61SMengdong Lin if (!dai_link->codecs) 1057923c5e61SMengdong Lin return -ENOMEM; 1058923c5e61SMengdong Lin 1059923c5e61SMengdong Lin dai_link->codecs[0].name = dai_link->codec_name; 1060923c5e61SMengdong Lin dai_link->codecs[0].of_node = dai_link->codec_of_node; 1061923c5e61SMengdong Lin dai_link->codecs[0].dai_name = dai_link->codec_dai_name; 1062923c5e61SMengdong Lin } 1063923c5e61SMengdong Lin 1064923c5e61SMengdong Lin if (!dai_link->codecs) { 1065923c5e61SMengdong Lin dev_err(card->dev, "ASoC: DAI link has no CODECs\n"); 1066923c5e61SMengdong Lin return -EINVAL; 1067923c5e61SMengdong Lin } 1068923c5e61SMengdong Lin 1069923c5e61SMengdong Lin return 0; 1070923c5e61SMengdong Lin } 1071923c5e61SMengdong Lin 1072923c5e61SMengdong Lin static int soc_init_dai_link(struct snd_soc_card *card, 1073923c5e61SMengdong Lin struct snd_soc_dai_link *link) 1074923c5e61SMengdong Lin { 1075923c5e61SMengdong Lin int i, ret; 10763db769f1SKuninori Morimoto struct snd_soc_dai_link_component *codec; 1077923c5e61SMengdong Lin 1078daecf46eSKuninori Morimoto ret = snd_soc_init_platform(card, link); 1079daecf46eSKuninori Morimoto if (ret) { 1080daecf46eSKuninori Morimoto dev_err(card->dev, "ASoC: failed to init multiplatform\n"); 1081daecf46eSKuninori Morimoto return ret; 1082daecf46eSKuninori Morimoto } 1083daecf46eSKuninori Morimoto 1084923c5e61SMengdong Lin ret = snd_soc_init_multicodec(card, link); 1085923c5e61SMengdong Lin if (ret) { 1086923c5e61SMengdong Lin dev_err(card->dev, "ASoC: failed to init multicodec\n"); 1087923c5e61SMengdong Lin return ret; 1088923c5e61SMengdong Lin } 1089923c5e61SMengdong Lin 10903db769f1SKuninori Morimoto for_each_link_codecs(link, i, codec) { 1091923c5e61SMengdong Lin /* 1092923c5e61SMengdong Lin * Codec must be specified by 1 of name or OF node, 1093923c5e61SMengdong Lin * not both or neither. 1094923c5e61SMengdong Lin */ 10953db769f1SKuninori Morimoto if (!!codec->name == 10963db769f1SKuninori Morimoto !!codec->of_node) { 1097923c5e61SMengdong Lin dev_err(card->dev, "ASoC: Neither/both codec name/of_node are set for %s\n", 1098923c5e61SMengdong Lin link->name); 1099923c5e61SMengdong Lin return -EINVAL; 1100923c5e61SMengdong Lin } 1101923c5e61SMengdong Lin /* Codec DAI name must be specified */ 11023db769f1SKuninori Morimoto if (!codec->dai_name) { 1103923c5e61SMengdong Lin dev_err(card->dev, "ASoC: codec_dai_name not set for %s\n", 1104923c5e61SMengdong Lin link->name); 1105923c5e61SMengdong Lin return -EINVAL; 1106923c5e61SMengdong Lin } 1107923c5e61SMengdong Lin } 1108923c5e61SMengdong Lin 1109923c5e61SMengdong Lin /* 1110923c5e61SMengdong Lin * Platform may be specified by either name or OF node, but 1111923c5e61SMengdong Lin * can be left unspecified, and a dummy platform will be used. 1112923c5e61SMengdong Lin */ 1113daecf46eSKuninori Morimoto if (link->platform->name && link->platform->of_node) { 1114923c5e61SMengdong Lin dev_err(card->dev, 1115923c5e61SMengdong Lin "ASoC: Both platform name/of_node are set for %s\n", 1116923c5e61SMengdong Lin link->name); 1117923c5e61SMengdong Lin return -EINVAL; 1118923c5e61SMengdong Lin } 1119923c5e61SMengdong Lin /* 1120923c5e61SMengdong Lin * CPU device may be specified by either name or OF node, but 1121923c5e61SMengdong Lin * can be left unspecified, and will be matched based on DAI 1122923c5e61SMengdong Lin * name alone.. 1123923c5e61SMengdong Lin */ 1124923c5e61SMengdong Lin if (link->cpu_name && link->cpu_of_node) { 1125923c5e61SMengdong Lin dev_err(card->dev, 1126923c5e61SMengdong Lin "ASoC: Neither/both cpu name/of_node are set for %s\n", 1127923c5e61SMengdong Lin link->name); 1128923c5e61SMengdong Lin return -EINVAL; 1129923c5e61SMengdong Lin } 1130923c5e61SMengdong Lin /* 1131923c5e61SMengdong Lin * At least one of CPU DAI name or CPU device name/node must be 1132923c5e61SMengdong Lin * specified 1133923c5e61SMengdong Lin */ 1134923c5e61SMengdong Lin if (!link->cpu_dai_name && 1135923c5e61SMengdong Lin !(link->cpu_name || link->cpu_of_node)) { 1136923c5e61SMengdong Lin dev_err(card->dev, 1137923c5e61SMengdong Lin "ASoC: Neither cpu_dai_name nor cpu_name/of_node are set for %s\n", 1138923c5e61SMengdong Lin link->name); 1139923c5e61SMengdong Lin return -EINVAL; 1140923c5e61SMengdong Lin } 1141923c5e61SMengdong Lin 1142923c5e61SMengdong Lin return 0; 1143923c5e61SMengdong Lin } 1144923c5e61SMengdong Lin 1145ef2e8175SKuninori Morimoto void snd_soc_disconnect_sync(struct device *dev) 1146ef2e8175SKuninori Morimoto { 1147ef2e8175SKuninori Morimoto struct snd_soc_component *component = snd_soc_lookup_component(dev, NULL); 1148ef2e8175SKuninori Morimoto 1149ef2e8175SKuninori Morimoto if (!component || !component->card) 1150ef2e8175SKuninori Morimoto return; 1151ef2e8175SKuninori Morimoto 1152ef2e8175SKuninori Morimoto snd_card_disconnect_sync(component->card->snd_card); 1153ef2e8175SKuninori Morimoto } 1154df532185SKuninori Morimoto EXPORT_SYMBOL_GPL(snd_soc_disconnect_sync); 1155ef2e8175SKuninori Morimoto 1156f8f80361SMengdong Lin /** 1157f8f80361SMengdong Lin * snd_soc_add_dai_link - Add a DAI link dynamically 1158f8f80361SMengdong Lin * @card: The ASoC card to which the DAI link is added 1159f8f80361SMengdong Lin * @dai_link: The new DAI link to add 1160f8f80361SMengdong Lin * 1161f8f80361SMengdong Lin * This function adds a DAI link to the ASoC card's link list. 1162f8f80361SMengdong Lin * 1163f8f80361SMengdong Lin * Note: Topology can use this API to add DAI links when probing the 1164f8f80361SMengdong Lin * topology component. And machine drivers can still define static 1165f8f80361SMengdong Lin * DAI links in dai_link array. 1166f8f80361SMengdong Lin */ 1167f8f80361SMengdong Lin int snd_soc_add_dai_link(struct snd_soc_card *card, 1168f8f80361SMengdong Lin struct snd_soc_dai_link *dai_link) 1169f8f80361SMengdong Lin { 1170f8f80361SMengdong Lin if (dai_link->dobj.type 1171f8f80361SMengdong Lin && dai_link->dobj.type != SND_SOC_DOBJ_DAI_LINK) { 1172f8f80361SMengdong Lin dev_err(card->dev, "Invalid dai link type %d\n", 1173f8f80361SMengdong Lin dai_link->dobj.type); 1174f8f80361SMengdong Lin return -EINVAL; 1175f8f80361SMengdong Lin } 1176f8f80361SMengdong Lin 1177f8f80361SMengdong Lin lockdep_assert_held(&client_mutex); 1178d6f220eaSMengdong Lin /* Notify the machine driver for extra initialization 1179d6f220eaSMengdong Lin * on the link created by topology. 1180d6f220eaSMengdong Lin */ 1181d6f220eaSMengdong Lin if (dai_link->dobj.type && card->add_dai_link) 1182d6f220eaSMengdong Lin card->add_dai_link(card, dai_link); 1183d6f220eaSMengdong Lin 1184f8f80361SMengdong Lin list_add_tail(&dai_link->list, &card->dai_link_list); 1185f8f80361SMengdong Lin 1186f8f80361SMengdong Lin return 0; 1187f8f80361SMengdong Lin } 1188f8f80361SMengdong Lin EXPORT_SYMBOL_GPL(snd_soc_add_dai_link); 1189f8f80361SMengdong Lin 1190f8f80361SMengdong Lin /** 1191f8f80361SMengdong Lin * snd_soc_remove_dai_link - Remove a DAI link from the list 1192f8f80361SMengdong Lin * @card: The ASoC card that owns the link 1193f8f80361SMengdong Lin * @dai_link: The DAI link to remove 1194f8f80361SMengdong Lin * 1195f8f80361SMengdong Lin * This function removes a DAI link from the ASoC card's link list. 1196f8f80361SMengdong Lin * 1197f8f80361SMengdong Lin * For DAI links previously added by topology, topology should 1198f8f80361SMengdong Lin * remove them by using the dobj embedded in the link. 1199f8f80361SMengdong Lin */ 1200f8f80361SMengdong Lin void snd_soc_remove_dai_link(struct snd_soc_card *card, 1201f8f80361SMengdong Lin struct snd_soc_dai_link *dai_link) 1202f8f80361SMengdong Lin { 1203f8f80361SMengdong Lin struct snd_soc_dai_link *link, *_link; 1204f8f80361SMengdong Lin 1205f8f80361SMengdong Lin if (dai_link->dobj.type 1206f8f80361SMengdong Lin && dai_link->dobj.type != SND_SOC_DOBJ_DAI_LINK) { 1207f8f80361SMengdong Lin dev_err(card->dev, "Invalid dai link type %d\n", 1208f8f80361SMengdong Lin dai_link->dobj.type); 1209f8f80361SMengdong Lin return; 1210f8f80361SMengdong Lin } 1211f8f80361SMengdong Lin 1212f8f80361SMengdong Lin lockdep_assert_held(&client_mutex); 1213d6f220eaSMengdong Lin /* Notify the machine driver for extra destruction 1214d6f220eaSMengdong Lin * on the link created by topology. 1215d6f220eaSMengdong Lin */ 1216d6f220eaSMengdong Lin if (dai_link->dobj.type && card->remove_dai_link) 1217d6f220eaSMengdong Lin card->remove_dai_link(card, dai_link); 1218d6f220eaSMengdong Lin 1219f8f80361SMengdong Lin list_for_each_entry_safe(link, _link, &card->dai_link_list, list) { 1220f8f80361SMengdong Lin if (link == dai_link) { 1221f8f80361SMengdong Lin list_del(&link->list); 1222f8f80361SMengdong Lin return; 1223f8f80361SMengdong Lin } 1224f8f80361SMengdong Lin } 1225f8f80361SMengdong Lin } 1226f8f80361SMengdong Lin EXPORT_SYMBOL_GPL(snd_soc_remove_dai_link); 1227f0fba2adSLiam Girdwood 1228aefba455SJerome Brunet static void soc_set_of_name_prefix(struct snd_soc_component *component) 1229aefba455SJerome Brunet { 1230aefba455SJerome Brunet struct device_node *component_of_node = component->dev->of_node; 1231aefba455SJerome Brunet const char *str; 1232aefba455SJerome Brunet int ret; 1233aefba455SJerome Brunet 1234aefba455SJerome Brunet if (!component_of_node && component->dev->parent) 1235aefba455SJerome Brunet component_of_node = component->dev->parent->of_node; 1236aefba455SJerome Brunet 1237aefba455SJerome Brunet ret = of_property_read_string(component_of_node, "sound-name-prefix", 1238aefba455SJerome Brunet &str); 1239aefba455SJerome Brunet if (!ret) 1240aefba455SJerome Brunet component->name_prefix = str; 1241aefba455SJerome Brunet } 1242aefba455SJerome Brunet 1243ead9b919SJarkko Nikula static void soc_set_name_prefix(struct snd_soc_card *card, 124494f99c87SLars-Peter Clausen struct snd_soc_component *component) 1245ead9b919SJarkko Nikula { 1246ead9b919SJarkko Nikula int i; 1247ead9b919SJarkko Nikula 1248aefba455SJerome Brunet for (i = 0; i < card->num_configs && card->codec_conf; i++) { 1249ff819b83SDimitris Papastamos struct snd_soc_codec_conf *map = &card->codec_conf[i]; 1250b24c539bSCharles Keepax struct device_node *component_of_node = component->dev->of_node; 1251b24c539bSCharles Keepax 1252b24c539bSCharles Keepax if (!component_of_node && component->dev->parent) 1253b24c539bSCharles Keepax component_of_node = component->dev->parent->of_node; 1254b24c539bSCharles Keepax 1255b24c539bSCharles Keepax if (map->of_node && component_of_node != map->of_node) 12563ca041edSSebastian Reichel continue; 125794f99c87SLars-Peter Clausen if (map->dev_name && strcmp(component->name, map->dev_name)) 12583ca041edSSebastian Reichel continue; 125994f99c87SLars-Peter Clausen component->name_prefix = map->name_prefix; 1260aefba455SJerome Brunet return; 1261ead9b919SJarkko Nikula } 1262aefba455SJerome Brunet 1263aefba455SJerome Brunet /* 1264aefba455SJerome Brunet * If there is no configuration table or no match in the table, 1265aefba455SJerome Brunet * check if a prefix is provided in the node 1266aefba455SJerome Brunet */ 1267aefba455SJerome Brunet soc_set_of_name_prefix(component); 1268ead9b919SJarkko Nikula } 1269ead9b919SJarkko Nikula 1270f1d45cc3SLars-Peter Clausen static int soc_probe_component(struct snd_soc_card *card, 1271f1d45cc3SLars-Peter Clausen struct snd_soc_component *component) 1272589c3563SJarkko Nikula { 1273f1d45cc3SLars-Peter Clausen struct snd_soc_dapm_context *dapm = snd_soc_component_get_dapm(component); 1274888df395SMark Brown struct snd_soc_dai *dai; 1275f1d45cc3SLars-Peter Clausen int ret; 1276589c3563SJarkko Nikula 12771b7c1231SLars-Peter Clausen if (!strcmp(component->name, "snd-soc-dummy")) 127870090bbbSLars-Peter Clausen return 0; 1279589c3563SJarkko Nikula 1280abd31b32SLars-Peter Clausen if (component->card) { 12811b7c1231SLars-Peter Clausen if (component->card != card) { 12821b7c1231SLars-Peter Clausen dev_err(component->dev, 12831b7c1231SLars-Peter Clausen "Trying to bind component to card \"%s\" but is already bound to card \"%s\"\n", 12841b7c1231SLars-Peter Clausen card->name, component->card->name); 12851b7c1231SLars-Peter Clausen return -ENODEV; 12861b7c1231SLars-Peter Clausen } 12871b7c1231SLars-Peter Clausen return 0; 12881b7c1231SLars-Peter Clausen } 12891b7c1231SLars-Peter Clausen 1290abd31b32SLars-Peter Clausen if (!try_module_get(component->dev->driver->owner)) 1291abd31b32SLars-Peter Clausen return -ENODEV; 1292abd31b32SLars-Peter Clausen 1293f1d45cc3SLars-Peter Clausen component->card = card; 1294f1d45cc3SLars-Peter Clausen dapm->card = card; 1295f1d45cc3SLars-Peter Clausen soc_set_name_prefix(card, component); 1296589c3563SJarkko Nikula 1297f1d45cc3SLars-Peter Clausen soc_init_component_debugfs(component); 1298d5d1e0beSLars-Peter Clausen 1299688d0ebfSKuninori Morimoto if (component->driver->dapm_widgets) { 1300688d0ebfSKuninori Morimoto ret = snd_soc_dapm_new_controls(dapm, 1301688d0ebfSKuninori Morimoto component->driver->dapm_widgets, 1302688d0ebfSKuninori Morimoto component->driver->num_dapm_widgets); 130377530150SLars-Peter Clausen 1304b318ad50SNariman Poushin if (ret != 0) { 1305f1d45cc3SLars-Peter Clausen dev_err(component->dev, 1306b318ad50SNariman Poushin "Failed to create new controls %d\n", ret); 1307b318ad50SNariman Poushin goto err_probe; 1308b318ad50SNariman Poushin } 1309b318ad50SNariman Poushin } 1310b318ad50SNariman Poushin 13110634814fSLars-Peter Clausen list_for_each_entry(dai, &component->dai_list, list) { 13120634814fSLars-Peter Clausen ret = snd_soc_dapm_new_dai_widgets(dapm, dai); 1313261edc70SNariman Poushin if (ret != 0) { 1314f1d45cc3SLars-Peter Clausen dev_err(component->dev, 1315261edc70SNariman Poushin "Failed to create DAI widgets %d\n", ret); 1316261edc70SNariman Poushin goto err_probe; 1317261edc70SNariman Poushin } 1318261edc70SNariman Poushin } 1319888df395SMark Brown 1320999f7f5aSKuninori Morimoto if (component->driver->probe) { 1321999f7f5aSKuninori Morimoto ret = component->driver->probe(component); 1322589c3563SJarkko Nikula if (ret < 0) { 1323f1d45cc3SLars-Peter Clausen dev_err(component->dev, 1324f1d45cc3SLars-Peter Clausen "ASoC: failed to probe component %d\n", ret); 132570d29331SJarkko Nikula goto err_probe; 1326589c3563SJarkko Nikula } 1327cb2cf612SLiam Girdwood 1328f1d45cc3SLars-Peter Clausen WARN(dapm->idle_bias_off && 1329f1d45cc3SLars-Peter Clausen dapm->bias_level != SND_SOC_BIAS_OFF, 1330956245e9SLiam Girdwood "codec %s can not start from non-off bias with idle_bias_off==1\n", 1331f1d45cc3SLars-Peter Clausen component->name); 1332956245e9SLiam Girdwood } 1333956245e9SLiam Girdwood 1334f2ed6b07SMengdong Lin /* machine specific init */ 1335f2ed6b07SMengdong Lin if (component->init) { 1336f2ed6b07SMengdong Lin ret = component->init(component); 1337f2ed6b07SMengdong Lin if (ret < 0) { 1338f2ed6b07SMengdong Lin dev_err(component->dev, 1339f2ed6b07SMengdong Lin "Failed to do machine specific init %d\n", ret); 1340f2ed6b07SMengdong Lin goto err_probe; 1341f2ed6b07SMengdong Lin } 1342f2ed6b07SMengdong Lin } 1343f2ed6b07SMengdong Lin 1344b8972bf0SKuninori Morimoto if (component->driver->controls) 1345b8972bf0SKuninori Morimoto snd_soc_add_component_controls(component, 1346b8972bf0SKuninori Morimoto component->driver->controls, 1347b8972bf0SKuninori Morimoto component->driver->num_controls); 13486969b2baSKuninori Morimoto if (component->driver->dapm_routes) 13496969b2baSKuninori Morimoto snd_soc_dapm_add_routes(dapm, 13506969b2baSKuninori Morimoto component->driver->dapm_routes, 13516969b2baSKuninori Morimoto component->driver->num_dapm_routes); 1352956245e9SLiam Girdwood 1353f1d45cc3SLars-Peter Clausen list_add(&dapm->list, &card->dapm_list); 1354d9fc4063SKuninori Morimoto list_add(&component->card_list, &card->component_dev_list); 1355956245e9SLiam Girdwood 1356956245e9SLiam Girdwood return 0; 1357956245e9SLiam Girdwood 1358956245e9SLiam Girdwood err_probe: 1359f1d45cc3SLars-Peter Clausen soc_cleanup_component_debugfs(component); 1360abd31b32SLars-Peter Clausen component->card = NULL; 1361f1d45cc3SLars-Peter Clausen module_put(component->dev->driver->owner); 1362956245e9SLiam Girdwood 1363956245e9SLiam Girdwood return ret; 1364956245e9SLiam Girdwood } 1365956245e9SLiam Girdwood 136636ae1a96SMark Brown static void rtd_release(struct device *dev) 136736ae1a96SMark Brown { 136836ae1a96SMark Brown kfree(dev); 136936ae1a96SMark Brown } 1370f0fba2adSLiam Girdwood 13715f3484acSLars-Peter Clausen static int soc_post_component_init(struct snd_soc_pcm_runtime *rtd, 13725f3484acSLars-Peter Clausen const char *name) 1373503ae5e0SMisael Lopez Cruz { 1374589c3563SJarkko Nikula int ret = 0; 1375589c3563SJarkko Nikula 1376589c3563SJarkko Nikula /* register the rtd device */ 137736ae1a96SMark Brown rtd->dev = kzalloc(sizeof(struct device), GFP_KERNEL); 137836ae1a96SMark Brown if (!rtd->dev) 137936ae1a96SMark Brown return -ENOMEM; 138036ae1a96SMark Brown device_initialize(rtd->dev); 13815f3484acSLars-Peter Clausen rtd->dev->parent = rtd->card->dev; 138236ae1a96SMark Brown rtd->dev->release = rtd_release; 1383d29697dcSTakashi Iwai rtd->dev->groups = soc_dev_attr_groups; 1384f294afedSLars-Peter Clausen dev_set_name(rtd->dev, "%s", name); 138536ae1a96SMark Brown dev_set_drvdata(rtd->dev, rtd); 1386b8c0dab9SLiam Girdwood mutex_init(&rtd->pcm_mutex); 138701d7584cSLiam Girdwood INIT_LIST_HEAD(&rtd->dpcm[SNDRV_PCM_STREAM_PLAYBACK].be_clients); 138801d7584cSLiam Girdwood INIT_LIST_HEAD(&rtd->dpcm[SNDRV_PCM_STREAM_CAPTURE].be_clients); 138901d7584cSLiam Girdwood INIT_LIST_HEAD(&rtd->dpcm[SNDRV_PCM_STREAM_PLAYBACK].fe_clients); 139001d7584cSLiam Girdwood INIT_LIST_HEAD(&rtd->dpcm[SNDRV_PCM_STREAM_CAPTURE].fe_clients); 139136ae1a96SMark Brown ret = device_add(rtd->dev); 1392589c3563SJarkko Nikula if (ret < 0) { 1393865df9cbSChuansheng Liu /* calling put_device() here to free the rtd->dev */ 1394865df9cbSChuansheng Liu put_device(rtd->dev); 13955f3484acSLars-Peter Clausen dev_err(rtd->card->dev, 1396f110bfc7SLiam Girdwood "ASoC: failed to register runtime device: %d\n", ret); 1397589c3563SJarkko Nikula return ret; 1398589c3563SJarkko Nikula } 1399589c3563SJarkko Nikula rtd->dev_registered = 1; 1400589c3563SJarkko Nikula return 0; 1401589c3563SJarkko Nikula } 1402589c3563SJarkko Nikula 14031a497983SMengdong Lin static int soc_probe_link_components(struct snd_soc_card *card, 14041a497983SMengdong Lin struct snd_soc_pcm_runtime *rtd, 140562ae68faSStephen Warren int order) 140662ae68faSStephen Warren { 1407f1d45cc3SLars-Peter Clausen struct snd_soc_component *component; 140890be711eSKuninori Morimoto struct snd_soc_rtdcom_list *rtdcom; 140990be711eSKuninori Morimoto int ret; 141062ae68faSStephen Warren 141190be711eSKuninori Morimoto for_each_rtdcom(rtd, rtdcom) { 141290be711eSKuninori Morimoto component = rtdcom->component; 141390be711eSKuninori Morimoto 141470090bbbSLars-Peter Clausen if (component->driver->probe_order == order) { 1415f1d45cc3SLars-Peter Clausen ret = soc_probe_component(card, component); 141662ae68faSStephen Warren if (ret < 0) 141762ae68faSStephen Warren return ret; 141862ae68faSStephen Warren } 141962ae68faSStephen Warren } 142062ae68faSStephen Warren 142162ae68faSStephen Warren return 0; 142262ae68faSStephen Warren } 142362ae68faSStephen Warren 14248e2be562SLars-Peter Clausen static int soc_probe_dai(struct snd_soc_dai *dai, int order) 1425b0aa88afSMisael Lopez Cruz { 14267a2ccad5SKuninori Morimoto if (dai->probed || 14277a2ccad5SKuninori Morimoto dai->driver->probe_order != order) 14287a2ccad5SKuninori Morimoto return 0; 1429b0aa88afSMisael Lopez Cruz 14308e2be562SLars-Peter Clausen if (dai->driver->probe) { 14317a2ccad5SKuninori Morimoto int ret = dai->driver->probe(dai); 1432b0aa88afSMisael Lopez Cruz if (ret < 0) { 14337a2ccad5SKuninori Morimoto dev_err(dai->dev, "ASoC: failed to probe DAI %s: %d\n", 14348e2be562SLars-Peter Clausen dai->name, ret); 1435b0aa88afSMisael Lopez Cruz return ret; 1436b0aa88afSMisael Lopez Cruz } 1437b0aa88afSMisael Lopez Cruz } 1438b0aa88afSMisael Lopez Cruz 14398e2be562SLars-Peter Clausen dai->probed = 1; 1440b0aa88afSMisael Lopez Cruz 1441b0aa88afSMisael Lopez Cruz return 0; 1442b0aa88afSMisael Lopez Cruz } 1443b0aa88afSMisael Lopez Cruz 144425f7b701SArnaud Pouliquen static int soc_link_dai_pcm_new(struct snd_soc_dai **dais, int num_dais, 144525f7b701SArnaud Pouliquen struct snd_soc_pcm_runtime *rtd) 144625f7b701SArnaud Pouliquen { 144725f7b701SArnaud Pouliquen int i, ret = 0; 144825f7b701SArnaud Pouliquen 144925f7b701SArnaud Pouliquen for (i = 0; i < num_dais; ++i) { 145025f7b701SArnaud Pouliquen struct snd_soc_dai_driver *drv = dais[i]->driver; 145125f7b701SArnaud Pouliquen 145225f7b701SArnaud Pouliquen if (!rtd->dai_link->no_pcm && drv->pcm_new) 145325f7b701SArnaud Pouliquen ret = drv->pcm_new(rtd, dais[i]); 145425f7b701SArnaud Pouliquen if (ret < 0) { 145525f7b701SArnaud Pouliquen dev_err(dais[i]->dev, 145625f7b701SArnaud Pouliquen "ASoC: Failed to bind %s with pcm device\n", 145725f7b701SArnaud Pouliquen dais[i]->name); 145825f7b701SArnaud Pouliquen return ret; 145925f7b701SArnaud Pouliquen } 146025f7b701SArnaud Pouliquen } 146125f7b701SArnaud Pouliquen 146225f7b701SArnaud Pouliquen return 0; 146325f7b701SArnaud Pouliquen } 146425f7b701SArnaud Pouliquen 14651a497983SMengdong Lin static int soc_probe_link_dais(struct snd_soc_card *card, 14661a497983SMengdong Lin struct snd_soc_pcm_runtime *rtd, int order) 1467f0fba2adSLiam Girdwood { 14681a497983SMengdong Lin struct snd_soc_dai_link *dai_link = rtd->dai_link; 1469c74184edSMark Brown struct snd_soc_dai *cpu_dai = rtd->cpu_dai; 1470a655de80SLiam Girdwood struct snd_soc_rtdcom_list *rtdcom; 1471a655de80SLiam Girdwood struct snd_soc_component *component; 14720b7990e3SKuninori Morimoto struct snd_soc_dai *codec_dai; 1473a655de80SLiam Girdwood int i, ret, num; 1474f0fba2adSLiam Girdwood 1475f110bfc7SLiam Girdwood dev_dbg(card->dev, "ASoC: probe %s dai link %d late %d\n", 14761a497983SMengdong Lin card->name, rtd->num, order); 1477f0fba2adSLiam Girdwood 1478f0fba2adSLiam Girdwood /* set default power off timeout */ 1479f0fba2adSLiam Girdwood rtd->pmdown_time = pmdown_time; 1480f0fba2adSLiam Girdwood 14818e2be562SLars-Peter Clausen ret = soc_probe_dai(cpu_dai, order); 14828e2be562SLars-Peter Clausen if (ret) 1483f0fba2adSLiam Girdwood return ret; 1484f0fba2adSLiam Girdwood 1485f0fba2adSLiam Girdwood /* probe the CODEC DAI */ 14860b7990e3SKuninori Morimoto for_each_rtd_codec_dai(rtd, i, codec_dai) { 14870b7990e3SKuninori Morimoto ret = soc_probe_dai(codec_dai, order); 1488b0aa88afSMisael Lopez Cruz if (ret) 1489f0fba2adSLiam Girdwood return ret; 149088bd870fSBenoit Cousson } 1491f0fba2adSLiam Girdwood 14920168bf0dSLiam Girdwood /* complete DAI probe during last probe */ 14930168bf0dSLiam Girdwood if (order != SND_SOC_COMP_ORDER_LAST) 14940168bf0dSLiam Girdwood return 0; 14950168bf0dSLiam Girdwood 14965f3484acSLars-Peter Clausen /* do machine specific initialization */ 14975f3484acSLars-Peter Clausen if (dai_link->init) { 14985f3484acSLars-Peter Clausen ret = dai_link->init(rtd); 14995f3484acSLars-Peter Clausen if (ret < 0) { 15005f3484acSLars-Peter Clausen dev_err(card->dev, "ASoC: failed to init %s: %d\n", 15015f3484acSLars-Peter Clausen dai_link->name, ret); 15025f3484acSLars-Peter Clausen return ret; 15035f3484acSLars-Peter Clausen } 15045f3484acSLars-Peter Clausen } 15055f3484acSLars-Peter Clausen 1506a5053a8eSKuninori Morimoto if (dai_link->dai_fmt) 1507a5053a8eSKuninori Morimoto snd_soc_runtime_set_dai_fmt(rtd, dai_link->dai_fmt); 1508a5053a8eSKuninori Morimoto 15095f3484acSLars-Peter Clausen ret = soc_post_component_init(rtd, dai_link->name); 1510589c3563SJarkko Nikula if (ret) 1511f0fba2adSLiam Girdwood return ret; 1512f0fba2adSLiam Girdwood 15135f3484acSLars-Peter Clausen #ifdef CONFIG_DEBUG_FS 15145f3484acSLars-Peter Clausen /* add DPCM sysfs entries */ 15152e55b90aSLars-Peter Clausen if (dai_link->dynamic) 15162e55b90aSLars-Peter Clausen soc_dpcm_debugfs_add(rtd); 15175f3484acSLars-Peter Clausen #endif 15185f3484acSLars-Peter Clausen 1519a655de80SLiam Girdwood num = rtd->num; 1520a655de80SLiam Girdwood 1521a655de80SLiam Girdwood /* 1522a655de80SLiam Girdwood * most drivers will register their PCMs using DAI link ordering but 1523a655de80SLiam Girdwood * topology based drivers can use the DAI link id field to set PCM 1524a655de80SLiam Girdwood * device number and then use rtd + a base offset of the BEs. 1525a655de80SLiam Girdwood */ 1526a655de80SLiam Girdwood for_each_rtdcom(rtd, rtdcom) { 1527a655de80SLiam Girdwood component = rtdcom->component; 1528a655de80SLiam Girdwood 1529a655de80SLiam Girdwood if (!component->driver->use_dai_pcm_id) 1530a655de80SLiam Girdwood continue; 1531a655de80SLiam Girdwood 1532a655de80SLiam Girdwood if (rtd->dai_link->no_pcm) 1533a655de80SLiam Girdwood num += component->driver->be_pcm_base; 1534a655de80SLiam Girdwood else 1535a655de80SLiam Girdwood num = rtd->dai_link->id; 1536a655de80SLiam Girdwood } 1537a655de80SLiam Girdwood 15386f0c4226SJie Yang if (cpu_dai->driver->compress_new) { 15391245b700SNamarta Kohli /*create compress_device"*/ 1540a655de80SLiam Girdwood ret = cpu_dai->driver->compress_new(rtd, num); 15411245b700SNamarta Kohli if (ret < 0) { 1542f110bfc7SLiam Girdwood dev_err(card->dev, "ASoC: can't create compress %s\n", 15431245b700SNamarta Kohli dai_link->stream_name); 15441245b700SNamarta Kohli return ret; 15451245b700SNamarta Kohli } 15461245b700SNamarta Kohli } else { 15471245b700SNamarta Kohli 1548c74184edSMark Brown if (!dai_link->params) { 1549f0fba2adSLiam Girdwood /* create the pcm */ 1550a655de80SLiam Girdwood ret = soc_new_pcm(rtd, num); 1551f0fba2adSLiam Girdwood if (ret < 0) { 1552f110bfc7SLiam Girdwood dev_err(card->dev, "ASoC: can't create pcm %s :%d\n", 15530837fc62SFabio Estevam dai_link->stream_name, ret); 1554f0fba2adSLiam Girdwood return ret; 1555f0fba2adSLiam Girdwood } 155625f7b701SArnaud Pouliquen ret = soc_link_dai_pcm_new(&cpu_dai, 1, rtd); 155725f7b701SArnaud Pouliquen if (ret < 0) 155825f7b701SArnaud Pouliquen return ret; 155925f7b701SArnaud Pouliquen ret = soc_link_dai_pcm_new(rtd->codec_dais, 156025f7b701SArnaud Pouliquen rtd->num_codecs, rtd); 156125f7b701SArnaud Pouliquen if (ret < 0) 156225f7b701SArnaud Pouliquen return ret; 1563c74184edSMark Brown } else { 15649d58a077SRichard Fitzgerald INIT_DELAYED_WORK(&rtd->delayed_work, 15659d58a077SRichard Fitzgerald codec2codec_close_delayed_work); 1566c74184edSMark Brown } 1567c74184edSMark Brown } 1568c74184edSMark Brown 1569f0fba2adSLiam Girdwood return 0; 1570f0fba2adSLiam Girdwood } 1571f0fba2adSLiam Girdwood 157244c69bb1SLars-Peter Clausen static int soc_bind_aux_dev(struct snd_soc_card *card, int num) 1573b19e6e7bSMark Brown { 15743ca041edSSebastian Reichel struct snd_soc_aux_dev *aux_dev = &card->aux_dev[num]; 1575f2ed6b07SMengdong Lin struct snd_soc_component *component; 1576f2ed6b07SMengdong Lin const char *name; 1577f2ed6b07SMengdong Lin struct device_node *codec_of_node; 15783ca041edSSebastian Reichel 1579f2ed6b07SMengdong Lin if (aux_dev->codec_of_node || aux_dev->codec_name) { 1580f2ed6b07SMengdong Lin /* codecs, usually analog devices */ 1581f2ed6b07SMengdong Lin name = aux_dev->codec_name; 1582f2ed6b07SMengdong Lin codec_of_node = aux_dev->codec_of_node; 1583f2ed6b07SMengdong Lin component = soc_find_component(codec_of_node, name); 1584f2ed6b07SMengdong Lin if (!component) { 1585f2ed6b07SMengdong Lin if (codec_of_node) 1586f2ed6b07SMengdong Lin name = of_node_full_name(codec_of_node); 1587f2ed6b07SMengdong Lin goto err_defer; 1588f2ed6b07SMengdong Lin } 1589f2ed6b07SMengdong Lin } else if (aux_dev->name) { 1590f2ed6b07SMengdong Lin /* generic components */ 1591f2ed6b07SMengdong Lin name = aux_dev->name; 1592f2ed6b07SMengdong Lin component = soc_find_component(NULL, name); 1593f2ed6b07SMengdong Lin if (!component) 1594f2ed6b07SMengdong Lin goto err_defer; 1595f2ed6b07SMengdong Lin } else { 1596f2ed6b07SMengdong Lin dev_err(card->dev, "ASoC: Invalid auxiliary device\n"); 1597f2ed6b07SMengdong Lin return -EINVAL; 1598f2ed6b07SMengdong Lin } 15993ca041edSSebastian Reichel 1600f2ed6b07SMengdong Lin component->init = aux_dev->init; 1601d2e3a135SSylwester Nawrocki list_add(&component->card_aux_list, &card->aux_comp_list); 16021a653aa4SKuninori Morimoto 1603f2ed6b07SMengdong Lin return 0; 1604f2ed6b07SMengdong Lin 1605f2ed6b07SMengdong Lin err_defer: 160665d9361fSLars-Peter Clausen dev_err(card->dev, "ASoC: %s not registered\n", name); 1607b19e6e7bSMark Brown return -EPROBE_DEFER; 1608b19e6e7bSMark Brown } 1609b19e6e7bSMark Brown 1610f2ed6b07SMengdong Lin static int soc_probe_aux_devices(struct snd_soc_card *card) 1611f2ed6b07SMengdong Lin { 1612991454e1SKuninori Morimoto struct snd_soc_component *comp; 1613f2ed6b07SMengdong Lin int order; 1614f2ed6b07SMengdong Lin int ret; 1615f2ed6b07SMengdong Lin 1616f2ed6b07SMengdong Lin for (order = SND_SOC_COMP_ORDER_FIRST; order <= SND_SOC_COMP_ORDER_LAST; 1617f2ed6b07SMengdong Lin order++) { 1618991454e1SKuninori Morimoto list_for_each_entry(comp, &card->aux_comp_list, card_aux_list) { 1619f2ed6b07SMengdong Lin if (comp->driver->probe_order == order) { 1620f2ed6b07SMengdong Lin ret = soc_probe_component(card, comp); 1621f2ed6b07SMengdong Lin if (ret < 0) { 1622f2ed6b07SMengdong Lin dev_err(card->dev, 1623f2ed6b07SMengdong Lin "ASoC: failed to probe aux component %s %d\n", 1624f2ed6b07SMengdong Lin comp->name, ret); 1625f2ed6b07SMengdong Lin return ret; 1626f2ed6b07SMengdong Lin } 1627f2ed6b07SMengdong Lin } 1628f2ed6b07SMengdong Lin } 1629f2ed6b07SMengdong Lin } 163065d9361fSLars-Peter Clausen 163144c69bb1SLars-Peter Clausen return 0; 16323ca041edSSebastian Reichel } 16332eea392dSJarkko Nikula 1634f2ed6b07SMengdong Lin static void soc_remove_aux_devices(struct snd_soc_card *card) 163544c69bb1SLars-Peter Clausen { 1636f2ed6b07SMengdong Lin struct snd_soc_component *comp, *_comp; 1637f2ed6b07SMengdong Lin int order; 163844c69bb1SLars-Peter Clausen 1639f2ed6b07SMengdong Lin for (order = SND_SOC_COMP_ORDER_FIRST; order <= SND_SOC_COMP_ORDER_LAST; 1640f2ed6b07SMengdong Lin order++) { 1641f2ed6b07SMengdong Lin list_for_each_entry_safe(comp, _comp, 1642991454e1SKuninori Morimoto &card->aux_comp_list, card_aux_list) { 16431a653aa4SKuninori Morimoto 1644f2ed6b07SMengdong Lin if (comp->driver->remove_order == order) { 1645f2ed6b07SMengdong Lin soc_remove_component(comp); 1646991454e1SKuninori Morimoto /* remove it from the card's aux_comp_list */ 1647991454e1SKuninori Morimoto list_del(&comp->card_aux_list); 16482eea392dSJarkko Nikula } 16495f3484acSLars-Peter Clausen } 16502eea392dSJarkko Nikula } 16512eea392dSJarkko Nikula } 16522eea392dSJarkko Nikula 1653ce64c8b9SLars-Peter Clausen /** 1654ce64c8b9SLars-Peter Clausen * snd_soc_runtime_set_dai_fmt() - Change DAI link format for a ASoC runtime 1655ce64c8b9SLars-Peter Clausen * @rtd: The runtime for which the DAI link format should be changed 1656ce64c8b9SLars-Peter Clausen * @dai_fmt: The new DAI link format 1657ce64c8b9SLars-Peter Clausen * 1658ce64c8b9SLars-Peter Clausen * This function updates the DAI link format for all DAIs connected to the DAI 1659ce64c8b9SLars-Peter Clausen * link for the specified runtime. 1660ce64c8b9SLars-Peter Clausen * 1661ce64c8b9SLars-Peter Clausen * Note: For setups with a static format set the dai_fmt field in the 1662ce64c8b9SLars-Peter Clausen * corresponding snd_dai_link struct instead of using this function. 1663ce64c8b9SLars-Peter Clausen * 1664ce64c8b9SLars-Peter Clausen * Returns 0 on success, otherwise a negative error code. 1665ce64c8b9SLars-Peter Clausen */ 1666ce64c8b9SLars-Peter Clausen int snd_soc_runtime_set_dai_fmt(struct snd_soc_pcm_runtime *rtd, 1667ce64c8b9SLars-Peter Clausen unsigned int dai_fmt) 1668ce64c8b9SLars-Peter Clausen { 1669ce64c8b9SLars-Peter Clausen struct snd_soc_dai *cpu_dai = rtd->cpu_dai; 16700b7990e3SKuninori Morimoto struct snd_soc_dai *codec_dai; 1671ce64c8b9SLars-Peter Clausen unsigned int i; 1672ce64c8b9SLars-Peter Clausen int ret; 1673ce64c8b9SLars-Peter Clausen 16740b7990e3SKuninori Morimoto for_each_rtd_codec_dai(rtd, i, codec_dai) { 1675ce64c8b9SLars-Peter Clausen ret = snd_soc_dai_set_fmt(codec_dai, dai_fmt); 1676ce64c8b9SLars-Peter Clausen if (ret != 0 && ret != -ENOTSUPP) { 1677ce64c8b9SLars-Peter Clausen dev_warn(codec_dai->dev, 1678ce64c8b9SLars-Peter Clausen "ASoC: Failed to set DAI format: %d\n", ret); 1679ce64c8b9SLars-Peter Clausen return ret; 1680ce64c8b9SLars-Peter Clausen } 1681ce64c8b9SLars-Peter Clausen } 1682ce64c8b9SLars-Peter Clausen 1683ce64c8b9SLars-Peter Clausen /* Flip the polarity for the "CPU" end of a CODEC<->CODEC link */ 1684cb2cf0deSKuninori Morimoto /* the component which has non_legacy_dai_naming is Codec */ 1685999f7f5aSKuninori Morimoto if (cpu_dai->component->driver->non_legacy_dai_naming) { 1686ce64c8b9SLars-Peter Clausen unsigned int inv_dai_fmt; 1687ce64c8b9SLars-Peter Clausen 1688ce64c8b9SLars-Peter Clausen inv_dai_fmt = dai_fmt & ~SND_SOC_DAIFMT_MASTER_MASK; 1689ce64c8b9SLars-Peter Clausen switch (dai_fmt & SND_SOC_DAIFMT_MASTER_MASK) { 1690ce64c8b9SLars-Peter Clausen case SND_SOC_DAIFMT_CBM_CFM: 1691ce64c8b9SLars-Peter Clausen inv_dai_fmt |= SND_SOC_DAIFMT_CBS_CFS; 1692ce64c8b9SLars-Peter Clausen break; 1693ce64c8b9SLars-Peter Clausen case SND_SOC_DAIFMT_CBM_CFS: 1694ce64c8b9SLars-Peter Clausen inv_dai_fmt |= SND_SOC_DAIFMT_CBS_CFM; 1695ce64c8b9SLars-Peter Clausen break; 1696ce64c8b9SLars-Peter Clausen case SND_SOC_DAIFMT_CBS_CFM: 1697ce64c8b9SLars-Peter Clausen inv_dai_fmt |= SND_SOC_DAIFMT_CBM_CFS; 1698ce64c8b9SLars-Peter Clausen break; 1699ce64c8b9SLars-Peter Clausen case SND_SOC_DAIFMT_CBS_CFS: 1700ce64c8b9SLars-Peter Clausen inv_dai_fmt |= SND_SOC_DAIFMT_CBM_CFM; 1701ce64c8b9SLars-Peter Clausen break; 1702ce64c8b9SLars-Peter Clausen } 1703ce64c8b9SLars-Peter Clausen 1704ce64c8b9SLars-Peter Clausen dai_fmt = inv_dai_fmt; 1705ce64c8b9SLars-Peter Clausen } 1706ce64c8b9SLars-Peter Clausen 1707ce64c8b9SLars-Peter Clausen ret = snd_soc_dai_set_fmt(cpu_dai, dai_fmt); 1708ce64c8b9SLars-Peter Clausen if (ret != 0 && ret != -ENOTSUPP) { 1709ce64c8b9SLars-Peter Clausen dev_warn(cpu_dai->dev, 1710ce64c8b9SLars-Peter Clausen "ASoC: Failed to set DAI format: %d\n", ret); 1711ce64c8b9SLars-Peter Clausen return ret; 1712ce64c8b9SLars-Peter Clausen } 1713ce64c8b9SLars-Peter Clausen 1714ce64c8b9SLars-Peter Clausen return 0; 1715ce64c8b9SLars-Peter Clausen } 1716ddaca25aSLars-Peter Clausen EXPORT_SYMBOL_GPL(snd_soc_runtime_set_dai_fmt); 1717ce64c8b9SLars-Peter Clausen 1718345233d7SLiam Girdwood 17191f5a4535STakashi Iwai #ifdef CONFIG_DMI 1720345233d7SLiam Girdwood /* Trim special characters, and replace '-' with '_' since '-' is used to 1721345233d7SLiam Girdwood * separate different DMI fields in the card long name. Only number and 1722345233d7SLiam Girdwood * alphabet characters and a few separator characters are kept. 1723345233d7SLiam Girdwood */ 1724345233d7SLiam Girdwood static void cleanup_dmi_name(char *name) 1725345233d7SLiam Girdwood { 1726345233d7SLiam Girdwood int i, j = 0; 1727345233d7SLiam Girdwood 1728345233d7SLiam Girdwood for (i = 0; name[i]; i++) { 1729345233d7SLiam Girdwood if (isalnum(name[i]) || (name[i] == '.') 1730345233d7SLiam Girdwood || (name[i] == '_')) 1731345233d7SLiam Girdwood name[j++] = name[i]; 1732345233d7SLiam Girdwood else if (name[i] == '-') 1733345233d7SLiam Girdwood name[j++] = '_'; 1734345233d7SLiam Girdwood } 1735345233d7SLiam Girdwood 1736345233d7SLiam Girdwood name[j] = '\0'; 1737345233d7SLiam Girdwood } 1738345233d7SLiam Girdwood 173998faf436SMengdong Lin /* Check if a DMI field is valid, i.e. not containing any string 174098faf436SMengdong Lin * in the black list. 174198faf436SMengdong Lin */ 174298faf436SMengdong Lin static int is_dmi_valid(const char *field) 174398faf436SMengdong Lin { 174498faf436SMengdong Lin int i = 0; 174598faf436SMengdong Lin 174698faf436SMengdong Lin while (dmi_blacklist[i]) { 174798faf436SMengdong Lin if (strstr(field, dmi_blacklist[i])) 174898faf436SMengdong Lin return 0; 174998faf436SMengdong Lin i++; 175046b5a4d2SWu Fengguang } 175198faf436SMengdong Lin 175298faf436SMengdong Lin return 1; 175398faf436SMengdong Lin } 175498faf436SMengdong Lin 1755345233d7SLiam Girdwood /** 1756345233d7SLiam Girdwood * snd_soc_set_dmi_name() - Register DMI names to card 1757345233d7SLiam Girdwood * @card: The card to register DMI names 1758345233d7SLiam Girdwood * @flavour: The flavour "differentiator" for the card amongst its peers. 1759345233d7SLiam Girdwood * 1760345233d7SLiam Girdwood * An Intel machine driver may be used by many different devices but are 1761345233d7SLiam Girdwood * difficult for userspace to differentiate, since machine drivers ususally 1762345233d7SLiam Girdwood * use their own name as the card short name and leave the card long name 1763345233d7SLiam Girdwood * blank. To differentiate such devices and fix bugs due to lack of 1764345233d7SLiam Girdwood * device-specific configurations, this function allows DMI info to be used 1765345233d7SLiam Girdwood * as the sound card long name, in the format of 1766345233d7SLiam Girdwood * "vendor-product-version-board" 1767345233d7SLiam Girdwood * (Character '-' is used to separate different DMI fields here). 1768345233d7SLiam Girdwood * This will help the user space to load the device-specific Use Case Manager 1769345233d7SLiam Girdwood * (UCM) configurations for the card. 1770345233d7SLiam Girdwood * 1771345233d7SLiam Girdwood * Possible card long names may be: 1772345233d7SLiam Girdwood * DellInc.-XPS139343-01-0310JH 1773345233d7SLiam Girdwood * ASUSTeKCOMPUTERINC.-T100TA-1.0-T100TA 1774345233d7SLiam Girdwood * Circuitco-MinnowboardMaxD0PLATFORM-D0-MinnowBoardMAX 1775345233d7SLiam Girdwood * 1776345233d7SLiam Girdwood * This function also supports flavoring the card longname to provide 1777345233d7SLiam Girdwood * the extra differentiation, like "vendor-product-version-board-flavor". 1778345233d7SLiam Girdwood * 1779345233d7SLiam Girdwood * We only keep number and alphabet characters and a few separator characters 1780345233d7SLiam Girdwood * in the card long name since UCM in the user space uses the card long names 1781345233d7SLiam Girdwood * as card configuration directory names and AudoConf cannot support special 1782345233d7SLiam Girdwood * charactors like SPACE. 1783345233d7SLiam Girdwood * 1784345233d7SLiam Girdwood * Returns 0 on success, otherwise a negative error code. 1785345233d7SLiam Girdwood */ 1786345233d7SLiam Girdwood int snd_soc_set_dmi_name(struct snd_soc_card *card, const char *flavour) 1787345233d7SLiam Girdwood { 1788345233d7SLiam Girdwood const char *vendor, *product, *product_version, *board; 1789345233d7SLiam Girdwood size_t longname_buf_size = sizeof(card->snd_card->longname); 1790345233d7SLiam Girdwood size_t len; 1791345233d7SLiam Girdwood 1792345233d7SLiam Girdwood if (card->long_name) 1793345233d7SLiam Girdwood return 0; /* long name already set by driver or from DMI */ 1794345233d7SLiam Girdwood 1795345233d7SLiam Girdwood /* make up dmi long name as: vendor.product.version.board */ 1796345233d7SLiam Girdwood vendor = dmi_get_system_info(DMI_BOARD_VENDOR); 179798faf436SMengdong Lin if (!vendor || !is_dmi_valid(vendor)) { 1798345233d7SLiam Girdwood dev_warn(card->dev, "ASoC: no DMI vendor name!\n"); 1799345233d7SLiam Girdwood return 0; 1800345233d7SLiam Girdwood } 1801345233d7SLiam Girdwood 180298faf436SMengdong Lin 1803345233d7SLiam Girdwood snprintf(card->dmi_longname, sizeof(card->snd_card->longname), 1804345233d7SLiam Girdwood "%s", vendor); 1805345233d7SLiam Girdwood cleanup_dmi_name(card->dmi_longname); 1806345233d7SLiam Girdwood 1807345233d7SLiam Girdwood product = dmi_get_system_info(DMI_PRODUCT_NAME); 180898faf436SMengdong Lin if (product && is_dmi_valid(product)) { 1809345233d7SLiam Girdwood len = strlen(card->dmi_longname); 1810345233d7SLiam Girdwood snprintf(card->dmi_longname + len, 1811345233d7SLiam Girdwood longname_buf_size - len, 1812345233d7SLiam Girdwood "-%s", product); 1813345233d7SLiam Girdwood 1814345233d7SLiam Girdwood len++; /* skip the separator "-" */ 1815345233d7SLiam Girdwood if (len < longname_buf_size) 1816345233d7SLiam Girdwood cleanup_dmi_name(card->dmi_longname + len); 1817345233d7SLiam Girdwood 1818345233d7SLiam Girdwood /* some vendors like Lenovo may only put a self-explanatory 1819345233d7SLiam Girdwood * name in the product version field 1820345233d7SLiam Girdwood */ 1821345233d7SLiam Girdwood product_version = dmi_get_system_info(DMI_PRODUCT_VERSION); 182298faf436SMengdong Lin if (product_version && is_dmi_valid(product_version)) { 1823345233d7SLiam Girdwood len = strlen(card->dmi_longname); 1824345233d7SLiam Girdwood snprintf(card->dmi_longname + len, 1825345233d7SLiam Girdwood longname_buf_size - len, 1826345233d7SLiam Girdwood "-%s", product_version); 1827345233d7SLiam Girdwood 1828345233d7SLiam Girdwood len++; 1829345233d7SLiam Girdwood if (len < longname_buf_size) 1830345233d7SLiam Girdwood cleanup_dmi_name(card->dmi_longname + len); 1831345233d7SLiam Girdwood } 1832345233d7SLiam Girdwood } 1833345233d7SLiam Girdwood 1834345233d7SLiam Girdwood board = dmi_get_system_info(DMI_BOARD_NAME); 183598faf436SMengdong Lin if (board && is_dmi_valid(board)) { 1836345233d7SLiam Girdwood len = strlen(card->dmi_longname); 1837345233d7SLiam Girdwood snprintf(card->dmi_longname + len, 1838345233d7SLiam Girdwood longname_buf_size - len, 1839345233d7SLiam Girdwood "-%s", board); 1840345233d7SLiam Girdwood 1841345233d7SLiam Girdwood len++; 1842345233d7SLiam Girdwood if (len < longname_buf_size) 1843345233d7SLiam Girdwood cleanup_dmi_name(card->dmi_longname + len); 1844345233d7SLiam Girdwood } else if (!product) { 1845345233d7SLiam Girdwood /* fall back to using legacy name */ 1846345233d7SLiam Girdwood dev_warn(card->dev, "ASoC: no DMI board/product name!\n"); 1847345233d7SLiam Girdwood return 0; 1848345233d7SLiam Girdwood } 1849345233d7SLiam Girdwood 1850345233d7SLiam Girdwood /* Add flavour to dmi long name */ 1851345233d7SLiam Girdwood if (flavour) { 1852345233d7SLiam Girdwood len = strlen(card->dmi_longname); 1853345233d7SLiam Girdwood snprintf(card->dmi_longname + len, 1854345233d7SLiam Girdwood longname_buf_size - len, 1855345233d7SLiam Girdwood "-%s", flavour); 1856345233d7SLiam Girdwood 1857345233d7SLiam Girdwood len++; 1858345233d7SLiam Girdwood if (len < longname_buf_size) 1859345233d7SLiam Girdwood cleanup_dmi_name(card->dmi_longname + len); 1860345233d7SLiam Girdwood } 1861345233d7SLiam Girdwood 1862345233d7SLiam Girdwood /* set the card long name */ 1863345233d7SLiam Girdwood card->long_name = card->dmi_longname; 1864345233d7SLiam Girdwood 1865345233d7SLiam Girdwood return 0; 1866345233d7SLiam Girdwood } 1867345233d7SLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_set_dmi_name); 18681f5a4535STakashi Iwai #endif /* CONFIG_DMI */ 1869345233d7SLiam Girdwood 1870a655de80SLiam Girdwood static void soc_check_tplg_fes(struct snd_soc_card *card) 1871a655de80SLiam Girdwood { 1872a655de80SLiam Girdwood struct snd_soc_component *component; 1873a655de80SLiam Girdwood const struct snd_soc_component_driver *comp_drv; 1874a655de80SLiam Girdwood struct snd_soc_dai_link *dai_link; 1875a655de80SLiam Girdwood int i; 1876a655de80SLiam Girdwood 1877a655de80SLiam Girdwood list_for_each_entry(component, &component_list, list) { 1878a655de80SLiam Girdwood 1879a655de80SLiam Girdwood /* does this component override FEs ? */ 1880a655de80SLiam Girdwood if (!component->driver->ignore_machine) 1881a655de80SLiam Girdwood continue; 1882a655de80SLiam Girdwood 1883a655de80SLiam Girdwood /* for this machine ? */ 1884a655de80SLiam Girdwood if (strcmp(component->driver->ignore_machine, 1885a655de80SLiam Girdwood card->dev->driver->name)) 1886a655de80SLiam Girdwood continue; 1887a655de80SLiam Girdwood 1888a655de80SLiam Girdwood /* machine matches, so override the rtd data */ 1889a655de80SLiam Girdwood for (i = 0; i < card->num_links; i++) { 1890a655de80SLiam Girdwood 1891a655de80SLiam Girdwood dai_link = &card->dai_link[i]; 1892a655de80SLiam Girdwood 1893a655de80SLiam Girdwood /* ignore this FE */ 1894a655de80SLiam Girdwood if (dai_link->dynamic) { 1895a655de80SLiam Girdwood dai_link->ignore = true; 1896a655de80SLiam Girdwood continue; 1897a655de80SLiam Girdwood } 1898a655de80SLiam Girdwood 1899a655de80SLiam Girdwood dev_info(card->dev, "info: override FE DAI link %s\n", 1900a655de80SLiam Girdwood card->dai_link[i].name); 1901a655de80SLiam Girdwood 1902a655de80SLiam Girdwood /* override platform component */ 1903daecf46eSKuninori Morimoto if (snd_soc_init_platform(card, dai_link) < 0) { 1904daecf46eSKuninori Morimoto dev_err(card->dev, "init platform error"); 1905daecf46eSKuninori Morimoto continue; 1906daecf46eSKuninori Morimoto } 1907daecf46eSKuninori Morimoto dai_link->platform->name = component->name; 1908a655de80SLiam Girdwood 1909a655de80SLiam Girdwood /* convert non BE into BE */ 1910a655de80SLiam Girdwood dai_link->no_pcm = 1; 1911a655de80SLiam Girdwood 1912a655de80SLiam Girdwood /* override any BE fixups */ 1913a655de80SLiam Girdwood dai_link->be_hw_params_fixup = 1914a655de80SLiam Girdwood component->driver->be_hw_params_fixup; 1915a655de80SLiam Girdwood 1916a655de80SLiam Girdwood /* most BE links don't set stream name, so set it to 1917a655de80SLiam Girdwood * dai link name if it's NULL to help bind widgets. 1918a655de80SLiam Girdwood */ 1919a655de80SLiam Girdwood if (!dai_link->stream_name) 1920a655de80SLiam Girdwood dai_link->stream_name = dai_link->name; 1921a655de80SLiam Girdwood } 1922a655de80SLiam Girdwood 1923a655de80SLiam Girdwood /* Inform userspace we are using alternate topology */ 1924a655de80SLiam Girdwood if (component->driver->topology_name_prefix) { 1925a655de80SLiam Girdwood 1926a655de80SLiam Girdwood /* topology shortname created ? */ 1927a655de80SLiam Girdwood if (!card->topology_shortname_created) { 1928a655de80SLiam Girdwood comp_drv = component->driver; 1929a655de80SLiam Girdwood 1930a655de80SLiam Girdwood snprintf(card->topology_shortname, 32, "%s-%s", 1931a655de80SLiam Girdwood comp_drv->topology_name_prefix, 1932a655de80SLiam Girdwood card->name); 1933a655de80SLiam Girdwood card->topology_shortname_created = true; 1934a655de80SLiam Girdwood } 1935a655de80SLiam Girdwood 1936a655de80SLiam Girdwood /* use topology shortname */ 1937a655de80SLiam Girdwood card->name = card->topology_shortname; 1938a655de80SLiam Girdwood } 1939a655de80SLiam Girdwood } 1940a655de80SLiam Girdwood } 1941a655de80SLiam Girdwood 1942b19e6e7bSMark Brown static int snd_soc_instantiate_card(struct snd_soc_card *card) 1943f0fba2adSLiam Girdwood { 19441a497983SMengdong Lin struct snd_soc_pcm_runtime *rtd; 194561b0088bSMengdong Lin struct snd_soc_dai_link *dai_link; 1946ce64c8b9SLars-Peter Clausen int ret, i, order; 194796dd3622SMark Brown 194834e81ab4SLars-Peter Clausen mutex_lock(&client_mutex); 194901b9d99aSLiam Girdwood mutex_lock_nested(&card->mutex, SND_SOC_CARD_CLASS_INIT); 1950f0fba2adSLiam Girdwood 1951a655de80SLiam Girdwood /* check whether any platform is ignore machine FE and using topology */ 1952a655de80SLiam Girdwood soc_check_tplg_fes(card); 1953a655de80SLiam Girdwood 1954b19e6e7bSMark Brown /* bind DAIs */ 1955b19e6e7bSMark Brown for (i = 0; i < card->num_links; i++) { 19566f2f1ff0SMengdong Lin ret = soc_bind_dai_link(card, &card->dai_link[i]); 1957b19e6e7bSMark Brown if (ret != 0) 1958b19e6e7bSMark Brown goto base_error; 1959db2a4165SFrank Mandarino } 1960db2a4165SFrank Mandarino 196144c69bb1SLars-Peter Clausen /* bind aux_devs too */ 1962b19e6e7bSMark Brown for (i = 0; i < card->num_aux_devs; i++) { 196344c69bb1SLars-Peter Clausen ret = soc_bind_aux_dev(card, i); 1964b19e6e7bSMark Brown if (ret != 0) 1965b19e6e7bSMark Brown goto base_error; 1966db2a4165SFrank Mandarino } 1967db2a4165SFrank Mandarino 1968f8f80361SMengdong Lin /* add predefined DAI links to the list */ 1969f8f80361SMengdong Lin for (i = 0; i < card->num_links; i++) 1970f8f80361SMengdong Lin snd_soc_add_dai_link(card, card->dai_link+i); 1971f8f80361SMengdong Lin 1972f0fba2adSLiam Girdwood /* card bind complete so register a sound card */ 1973102b5a8dSTakashi Iwai ret = snd_card_new(card->dev, SNDRV_DEFAULT_IDX1, SNDRV_DEFAULT_STR1, 1974f0fba2adSLiam Girdwood card->owner, 0, &card->snd_card); 1975f0fba2adSLiam Girdwood if (ret < 0) { 197610e8aa9aSMichał Mirosław dev_err(card->dev, 197710e8aa9aSMichał Mirosław "ASoC: can't create sound card for card %s: %d\n", 197810e8aa9aSMichał Mirosław card->name, ret); 1979b19e6e7bSMark Brown goto base_error; 1980db2a4165SFrank Mandarino } 1981db2a4165SFrank Mandarino 19820757d834SLars-Peter Clausen soc_init_card_debugfs(card); 19830757d834SLars-Peter Clausen 1984e37a4970SMark Brown card->dapm.bias_level = SND_SOC_BIAS_OFF; 1985e37a4970SMark Brown card->dapm.dev = card->dev; 1986e37a4970SMark Brown card->dapm.card = card; 1987e37a4970SMark Brown list_add(&card->dapm.list, &card->dapm_list); 1988e37a4970SMark Brown 1989d5d1e0beSLars-Peter Clausen #ifdef CONFIG_DEBUG_FS 1990d5d1e0beSLars-Peter Clausen snd_soc_dapm_debugfs_init(&card->dapm, card->debugfs_card_root); 1991d5d1e0beSLars-Peter Clausen #endif 1992d5d1e0beSLars-Peter Clausen 199388ee1c61SMark Brown #ifdef CONFIG_PM_SLEEP 19946ed25978SAndy Green /* deferred resume work */ 19956308419aSMark Brown INIT_WORK(&card->deferred_resume_work, soc_resume_deferred); 19961301a964SRandy Dunlap #endif 19976ed25978SAndy Green 19989a841ebbSMark Brown if (card->dapm_widgets) 19999a841ebbSMark Brown snd_soc_dapm_new_controls(&card->dapm, card->dapm_widgets, 20009a841ebbSMark Brown card->num_dapm_widgets); 20019a841ebbSMark Brown 2002f23e860eSNicolin Chen if (card->of_dapm_widgets) 2003f23e860eSNicolin Chen snd_soc_dapm_new_controls(&card->dapm, card->of_dapm_widgets, 2004f23e860eSNicolin Chen card->num_of_dapm_widgets); 2005f23e860eSNicolin Chen 2006f0fba2adSLiam Girdwood /* initialise the sound card only once */ 2007f0fba2adSLiam Girdwood if (card->probe) { 2008e7361ec4SMark Brown ret = card->probe(card); 2009f0fba2adSLiam Girdwood if (ret < 0) 2010f0fba2adSLiam Girdwood goto card_probe_error; 2011f0fba2adSLiam Girdwood } 2012f0fba2adSLiam Girdwood 201362ae68faSStephen Warren /* probe all components used by DAI links on this card */ 20140168bf0dSLiam Girdwood for (order = SND_SOC_COMP_ORDER_FIRST; order <= SND_SOC_COMP_ORDER_LAST; 20150168bf0dSLiam Girdwood order++) { 20161a497983SMengdong Lin list_for_each_entry(rtd, &card->rtd_list, list) { 20171a497983SMengdong Lin ret = soc_probe_link_components(card, rtd, order); 201862ae68faSStephen Warren if (ret < 0) { 2019f110bfc7SLiam Girdwood dev_err(card->dev, 2020f110bfc7SLiam Girdwood "ASoC: failed to instantiate card %d\n", 2021f110bfc7SLiam Girdwood ret); 202262ae68faSStephen Warren goto probe_dai_err; 202362ae68faSStephen Warren } 202462ae68faSStephen Warren } 202562ae68faSStephen Warren } 202662ae68faSStephen Warren 2027f2ed6b07SMengdong Lin /* probe auxiliary components */ 2028f2ed6b07SMengdong Lin ret = soc_probe_aux_devices(card); 2029f2ed6b07SMengdong Lin if (ret < 0) 2030f2ed6b07SMengdong Lin goto probe_dai_err; 2031f2ed6b07SMengdong Lin 203261b0088bSMengdong Lin /* Find new DAI links added during probing components and bind them. 203361b0088bSMengdong Lin * Components with topology may bring new DAIs and DAI links. 203461b0088bSMengdong Lin */ 203561b0088bSMengdong Lin list_for_each_entry(dai_link, &card->dai_link_list, list) { 203661b0088bSMengdong Lin if (soc_is_dai_link_bound(card, dai_link)) 203761b0088bSMengdong Lin continue; 203861b0088bSMengdong Lin 203961b0088bSMengdong Lin ret = soc_init_dai_link(card, dai_link); 204061b0088bSMengdong Lin if (ret) 204161b0088bSMengdong Lin goto probe_dai_err; 204261b0088bSMengdong Lin ret = soc_bind_dai_link(card, dai_link); 204361b0088bSMengdong Lin if (ret) 204461b0088bSMengdong Lin goto probe_dai_err; 204561b0088bSMengdong Lin } 204661b0088bSMengdong Lin 204762ae68faSStephen Warren /* probe all DAI links on this card */ 204862ae68faSStephen Warren for (order = SND_SOC_COMP_ORDER_FIRST; order <= SND_SOC_COMP_ORDER_LAST; 204962ae68faSStephen Warren order++) { 20501a497983SMengdong Lin list_for_each_entry(rtd, &card->rtd_list, list) { 20511a497983SMengdong Lin ret = soc_probe_link_dais(card, rtd, order); 2052fe3e78e0SMark Brown if (ret < 0) { 2053f110bfc7SLiam Girdwood dev_err(card->dev, 2054f110bfc7SLiam Girdwood "ASoC: failed to instantiate card %d\n", 2055f110bfc7SLiam Girdwood ret); 2056f0fba2adSLiam Girdwood goto probe_dai_err; 2057fe3e78e0SMark Brown } 2058fe3e78e0SMark Brown } 20590168bf0dSLiam Girdwood } 2060fe3e78e0SMark Brown 2061888df395SMark Brown snd_soc_dapm_link_dai_widgets(card); 2062b893ea5fSLiam Girdwood snd_soc_dapm_connect_dai_link_widgets(card); 2063888df395SMark Brown 2064b7af1dafSMark Brown if (card->controls) 2065022658beSLiam Girdwood snd_soc_add_card_controls(card, card->controls, card->num_controls); 2066b7af1dafSMark Brown 2067b8ad29deSMark Brown if (card->dapm_routes) 2068b8ad29deSMark Brown snd_soc_dapm_add_routes(&card->dapm, card->dapm_routes, 2069b8ad29deSMark Brown card->num_dapm_routes); 2070b8ad29deSMark Brown 2071f23e860eSNicolin Chen if (card->of_dapm_routes) 2072f23e860eSNicolin Chen snd_soc_dapm_add_routes(&card->dapm, card->of_dapm_routes, 2073f23e860eSNicolin Chen card->num_of_dapm_routes); 207475d9ac46SMark Brown 2075861886d3STakashi Iwai /* try to set some sane longname if DMI is available */ 2076861886d3STakashi Iwai snd_soc_set_dmi_name(card, NULL); 2077861886d3STakashi Iwai 2078f0fba2adSLiam Girdwood snprintf(card->snd_card->shortname, sizeof(card->snd_card->shortname), 2079fe3e78e0SMark Brown "%s", card->name); 2080f0fba2adSLiam Girdwood snprintf(card->snd_card->longname, sizeof(card->snd_card->longname), 208122de71baSLiam Girdwood "%s", card->long_name ? card->long_name : card->name); 2082f0e8ed85SMark Brown snprintf(card->snd_card->driver, sizeof(card->snd_card->driver), 2083f0e8ed85SMark Brown "%s", card->driver_name ? card->driver_name : card->name); 2084f0e8ed85SMark Brown for (i = 0; i < ARRAY_SIZE(card->snd_card->driver); i++) { 2085f0e8ed85SMark Brown switch (card->snd_card->driver[i]) { 2086f0e8ed85SMark Brown case '_': 2087f0e8ed85SMark Brown case '-': 2088f0e8ed85SMark Brown case '\0': 2089f0e8ed85SMark Brown break; 2090f0e8ed85SMark Brown default: 2091f0e8ed85SMark Brown if (!isalnum(card->snd_card->driver[i])) 2092f0e8ed85SMark Brown card->snd_card->driver[i] = '_'; 2093f0e8ed85SMark Brown break; 2094f0e8ed85SMark Brown } 2095f0e8ed85SMark Brown } 2096fe3e78e0SMark Brown 209728e9ad92SMark Brown if (card->late_probe) { 209828e9ad92SMark Brown ret = card->late_probe(card); 209928e9ad92SMark Brown if (ret < 0) { 2100f110bfc7SLiam Girdwood dev_err(card->dev, "ASoC: %s late_probe() failed: %d\n", 210128e9ad92SMark Brown card->name, ret); 210228e9ad92SMark Brown goto probe_aux_dev_err; 210328e9ad92SMark Brown } 210428e9ad92SMark Brown } 210528e9ad92SMark Brown 2106824ef826SLars-Peter Clausen snd_soc_dapm_new_widgets(card); 21078c193b8dSLars-Peter Clausen 2108f0fba2adSLiam Girdwood ret = snd_card_register(card->snd_card); 2109fe3e78e0SMark Brown if (ret < 0) { 2110f110bfc7SLiam Girdwood dev_err(card->dev, "ASoC: failed to register soundcard %d\n", 2111f110bfc7SLiam Girdwood ret); 21126b3ed785SAxel Lin goto probe_aux_dev_err; 2113fe3e78e0SMark Brown } 2114fe3e78e0SMark Brown 2115435c5e25SMark Brown card->instantiated = 1; 21164f4c0072SMark Brown snd_soc_dapm_sync(&card->dapm); 2117f0fba2adSLiam Girdwood mutex_unlock(&card->mutex); 211834e81ab4SLars-Peter Clausen mutex_unlock(&client_mutex); 2119b19e6e7bSMark Brown 2120b19e6e7bSMark Brown return 0; 2121db2a4165SFrank Mandarino 21222eea392dSJarkko Nikula probe_aux_dev_err: 2123f2ed6b07SMengdong Lin soc_remove_aux_devices(card); 21242eea392dSJarkko Nikula 2125f0fba2adSLiam Girdwood probe_dai_err: 21260671fd8eSKuninori Morimoto soc_remove_dai_links(card); 2127fe3e78e0SMark Brown 2128f0fba2adSLiam Girdwood card_probe_error: 212987506549SMark Brown if (card->remove) 2130e7361ec4SMark Brown card->remove(card); 2131f0fba2adSLiam Girdwood 21322210438bSLars-Peter Clausen snd_soc_dapm_free(&card->dapm); 21330757d834SLars-Peter Clausen soc_cleanup_card_debugfs(card); 2134f0fba2adSLiam Girdwood snd_card_free(card->snd_card); 2135f0fba2adSLiam Girdwood 2136b19e6e7bSMark Brown base_error: 21371a497983SMengdong Lin soc_remove_pcm_runtimes(card); 2138f0fba2adSLiam Girdwood mutex_unlock(&card->mutex); 213934e81ab4SLars-Peter Clausen mutex_unlock(&client_mutex); 2140db2a4165SFrank Mandarino 2141b19e6e7bSMark Brown return ret; 2142435c5e25SMark Brown } 2143435c5e25SMark Brown 2144435c5e25SMark Brown /* probes a new socdev */ 2145435c5e25SMark Brown static int soc_probe(struct platform_device *pdev) 2146435c5e25SMark Brown { 2147f0fba2adSLiam Girdwood struct snd_soc_card *card = platform_get_drvdata(pdev); 2148435c5e25SMark Brown 214970a7ca34SVinod Koul /* 215070a7ca34SVinod Koul * no card, so machine driver should be registering card 215170a7ca34SVinod Koul * we should not be here in that case so ret error 215270a7ca34SVinod Koul */ 215370a7ca34SVinod Koul if (!card) 215470a7ca34SVinod Koul return -EINVAL; 215570a7ca34SVinod Koul 2156fe4085e8SMark Brown dev_warn(&pdev->dev, 2157f110bfc7SLiam Girdwood "ASoC: machine %s should use snd_soc_register_card()\n", 2158fe4085e8SMark Brown card->name); 2159fe4085e8SMark Brown 2160435c5e25SMark Brown /* Bodge while we unpick instantiation */ 2161435c5e25SMark Brown card->dev = &pdev->dev; 2162f0fba2adSLiam Girdwood 216328d528c8SMark Brown return snd_soc_register_card(card); 2164435c5e25SMark Brown } 2165435c5e25SMark Brown 2166b0e26485SVinod Koul static int soc_cleanup_card_resources(struct snd_soc_card *card) 2167db2a4165SFrank Mandarino { 21681a497983SMengdong Lin struct snd_soc_pcm_runtime *rtd; 2169db2a4165SFrank Mandarino 2170f0fba2adSLiam Girdwood /* make sure any delayed work runs */ 21711a497983SMengdong Lin list_for_each_entry(rtd, &card->rtd_list, list) 217243829731STejun Heo flush_delayed_work(&rtd->delayed_work); 2173db2a4165SFrank Mandarino 21744efda5f2STakashi Iwai /* free the ALSA card at first; this syncs with pending operations */ 21754efda5f2STakashi Iwai snd_card_free(card->snd_card); 21764efda5f2STakashi Iwai 2177f0fba2adSLiam Girdwood /* remove and free each DAI */ 21780671fd8eSKuninori Morimoto soc_remove_dai_links(card); 21791a497983SMengdong Lin soc_remove_pcm_runtimes(card); 2180f0fba2adSLiam Girdwood 2181f2ed6b07SMengdong Lin /* remove auxiliary devices */ 2182f2ed6b07SMengdong Lin soc_remove_aux_devices(card); 2183f2ed6b07SMengdong Lin 2184d1e81428SMark Brown snd_soc_dapm_free(&card->dapm); 2185a6052154SJarkko Nikula soc_cleanup_card_debugfs(card); 2186a6052154SJarkko Nikula 2187f0fba2adSLiam Girdwood /* remove the card */ 218887506549SMark Brown if (card->remove) 2189e7361ec4SMark Brown card->remove(card); 2190f0fba2adSLiam Girdwood 2191b0e26485SVinod Koul return 0; 2192b2dfa62cSGuennadi Liakhovetski } 2193b0e26485SVinod Koul 2194b0e26485SVinod Koul /* removes a socdev */ 2195b0e26485SVinod Koul static int soc_remove(struct platform_device *pdev) 2196b0e26485SVinod Koul { 2197b0e26485SVinod Koul struct snd_soc_card *card = platform_get_drvdata(pdev); 2198b0e26485SVinod Koul 2199c5af3a2eSMark Brown snd_soc_unregister_card(card); 2200db2a4165SFrank Mandarino return 0; 2201db2a4165SFrank Mandarino } 2202db2a4165SFrank Mandarino 22036f8ab4acSMark Brown int snd_soc_poweroff(struct device *dev) 220451737470SMark Brown { 22056f8ab4acSMark Brown struct snd_soc_card *card = dev_get_drvdata(dev); 22061a497983SMengdong Lin struct snd_soc_pcm_runtime *rtd; 220751737470SMark Brown 220851737470SMark Brown if (!card->instantiated) 2209416356fcSMark Brown return 0; 221051737470SMark Brown 221151737470SMark Brown /* Flush out pmdown_time work - we actually do want to run it 221251737470SMark Brown * now, we're shutting down so no imminent restart. */ 22131a497983SMengdong Lin list_for_each_entry(rtd, &card->rtd_list, list) 221443829731STejun Heo flush_delayed_work(&rtd->delayed_work); 221551737470SMark Brown 2216f0fba2adSLiam Girdwood snd_soc_dapm_shutdown(card); 2217416356fcSMark Brown 2218988e8cc4SNicolin Chen /* deactivate pins to sleep state */ 22191a497983SMengdong Lin list_for_each_entry(rtd, &card->rtd_list, list) { 222088bd870fSBenoit Cousson struct snd_soc_dai *cpu_dai = rtd->cpu_dai; 22210b7990e3SKuninori Morimoto struct snd_soc_dai *codec_dai; 22221a497983SMengdong Lin int i; 222388bd870fSBenoit Cousson 2224988e8cc4SNicolin Chen pinctrl_pm_select_sleep_state(cpu_dai->dev); 22250b7990e3SKuninori Morimoto for_each_rtd_codec_dai(rtd, i, codec_dai) { 222688bd870fSBenoit Cousson pinctrl_pm_select_sleep_state(codec_dai->dev); 222788bd870fSBenoit Cousson } 2228988e8cc4SNicolin Chen } 2229988e8cc4SNicolin Chen 2230416356fcSMark Brown return 0; 223151737470SMark Brown } 22326f8ab4acSMark Brown EXPORT_SYMBOL_GPL(snd_soc_poweroff); 223351737470SMark Brown 22346f8ab4acSMark Brown const struct dev_pm_ops snd_soc_pm_ops = { 2235b1dd5897SViresh Kumar .suspend = snd_soc_suspend, 2236b1dd5897SViresh Kumar .resume = snd_soc_resume, 2237b1dd5897SViresh Kumar .freeze = snd_soc_suspend, 2238b1dd5897SViresh Kumar .thaw = snd_soc_resume, 22396f8ab4acSMark Brown .poweroff = snd_soc_poweroff, 2240b1dd5897SViresh Kumar .restore = snd_soc_resume, 2241416356fcSMark Brown }; 2242deb2607eSStephen Warren EXPORT_SYMBOL_GPL(snd_soc_pm_ops); 2243416356fcSMark Brown 2244db2a4165SFrank Mandarino /* ASoC platform driver */ 2245db2a4165SFrank Mandarino static struct platform_driver soc_driver = { 2246db2a4165SFrank Mandarino .driver = { 2247db2a4165SFrank Mandarino .name = "soc-audio", 22486f8ab4acSMark Brown .pm = &snd_soc_pm_ops, 2249db2a4165SFrank Mandarino }, 2250db2a4165SFrank Mandarino .probe = soc_probe, 2251db2a4165SFrank Mandarino .remove = soc_remove, 2252db2a4165SFrank Mandarino }; 2253db2a4165SFrank Mandarino 2254096e49d5SMark Brown /** 2255db2a4165SFrank Mandarino * snd_soc_cnew - create new control 2256db2a4165SFrank Mandarino * @_template: control template 2257db2a4165SFrank Mandarino * @data: control private data 2258ac11a2b3SMark Brown * @long_name: control long name 2259efb7ac3fSMark Brown * @prefix: control name prefix 2260db2a4165SFrank Mandarino * 2261db2a4165SFrank Mandarino * Create a new mixer control from a template control. 2262db2a4165SFrank Mandarino * 2263db2a4165SFrank Mandarino * Returns 0 for success, else error. 2264db2a4165SFrank Mandarino */ 2265db2a4165SFrank Mandarino struct snd_kcontrol *snd_soc_cnew(const struct snd_kcontrol_new *_template, 22663056557fSMark Brown void *data, const char *long_name, 2267efb7ac3fSMark Brown const char *prefix) 2268db2a4165SFrank Mandarino { 2269db2a4165SFrank Mandarino struct snd_kcontrol_new template; 2270efb7ac3fSMark Brown struct snd_kcontrol *kcontrol; 2271efb7ac3fSMark Brown char *name = NULL; 2272db2a4165SFrank Mandarino 2273db2a4165SFrank Mandarino memcpy(&template, _template, sizeof(template)); 2274db2a4165SFrank Mandarino template.index = 0; 2275db2a4165SFrank Mandarino 2276efb7ac3fSMark Brown if (!long_name) 2277efb7ac3fSMark Brown long_name = template.name; 2278efb7ac3fSMark Brown 2279efb7ac3fSMark Brown if (prefix) { 22802b581074SLars-Peter Clausen name = kasprintf(GFP_KERNEL, "%s %s", prefix, long_name); 2281efb7ac3fSMark Brown if (!name) 2282efb7ac3fSMark Brown return NULL; 2283efb7ac3fSMark Brown 2284efb7ac3fSMark Brown template.name = name; 2285efb7ac3fSMark Brown } else { 2286efb7ac3fSMark Brown template.name = long_name; 2287efb7ac3fSMark Brown } 2288efb7ac3fSMark Brown 2289efb7ac3fSMark Brown kcontrol = snd_ctl_new1(&template, data); 2290efb7ac3fSMark Brown 2291efb7ac3fSMark Brown kfree(name); 2292efb7ac3fSMark Brown 2293efb7ac3fSMark Brown return kcontrol; 2294db2a4165SFrank Mandarino } 2295db2a4165SFrank Mandarino EXPORT_SYMBOL_GPL(snd_soc_cnew); 2296db2a4165SFrank Mandarino 2297022658beSLiam Girdwood static int snd_soc_add_controls(struct snd_card *card, struct device *dev, 2298022658beSLiam Girdwood const struct snd_kcontrol_new *controls, int num_controls, 2299022658beSLiam Girdwood const char *prefix, void *data) 2300022658beSLiam Girdwood { 2301022658beSLiam Girdwood int err, i; 2302022658beSLiam Girdwood 2303022658beSLiam Girdwood for (i = 0; i < num_controls; i++) { 2304022658beSLiam Girdwood const struct snd_kcontrol_new *control = &controls[i]; 2305022658beSLiam Girdwood err = snd_ctl_add(card, snd_soc_cnew(control, data, 2306022658beSLiam Girdwood control->name, prefix)); 2307022658beSLiam Girdwood if (err < 0) { 2308f110bfc7SLiam Girdwood dev_err(dev, "ASoC: Failed to add %s: %d\n", 2309f110bfc7SLiam Girdwood control->name, err); 2310022658beSLiam Girdwood return err; 2311022658beSLiam Girdwood } 2312022658beSLiam Girdwood } 2313022658beSLiam Girdwood 2314022658beSLiam Girdwood return 0; 2315022658beSLiam Girdwood } 2316022658beSLiam Girdwood 23174fefd698SDimitris Papastamos struct snd_kcontrol *snd_soc_card_get_kcontrol(struct snd_soc_card *soc_card, 23184fefd698SDimitris Papastamos const char *name) 23194fefd698SDimitris Papastamos { 23204fefd698SDimitris Papastamos struct snd_card *card = soc_card->snd_card; 23214fefd698SDimitris Papastamos struct snd_kcontrol *kctl; 23224fefd698SDimitris Papastamos 23234fefd698SDimitris Papastamos if (unlikely(!name)) 23244fefd698SDimitris Papastamos return NULL; 23254fefd698SDimitris Papastamos 23264fefd698SDimitris Papastamos list_for_each_entry(kctl, &card->controls, list) 23274fefd698SDimitris Papastamos if (!strncmp(kctl->id.name, name, sizeof(kctl->id.name))) 23284fefd698SDimitris Papastamos return kctl; 23294fefd698SDimitris Papastamos return NULL; 23304fefd698SDimitris Papastamos } 23314fefd698SDimitris Papastamos EXPORT_SYMBOL_GPL(snd_soc_card_get_kcontrol); 23324fefd698SDimitris Papastamos 2333db2a4165SFrank Mandarino /** 23340f2780adSLars-Peter Clausen * snd_soc_add_component_controls - Add an array of controls to a component. 23350f2780adSLars-Peter Clausen * 23360f2780adSLars-Peter Clausen * @component: Component to add controls to 23370f2780adSLars-Peter Clausen * @controls: Array of controls to add 23380f2780adSLars-Peter Clausen * @num_controls: Number of elements in the array 23390f2780adSLars-Peter Clausen * 23400f2780adSLars-Peter Clausen * Return: 0 for success, else error. 23410f2780adSLars-Peter Clausen */ 23420f2780adSLars-Peter Clausen int snd_soc_add_component_controls(struct snd_soc_component *component, 23430f2780adSLars-Peter Clausen const struct snd_kcontrol_new *controls, unsigned int num_controls) 23440f2780adSLars-Peter Clausen { 23450f2780adSLars-Peter Clausen struct snd_card *card = component->card->snd_card; 23460f2780adSLars-Peter Clausen 23470f2780adSLars-Peter Clausen return snd_soc_add_controls(card, component->dev, controls, 23480f2780adSLars-Peter Clausen num_controls, component->name_prefix, component); 23490f2780adSLars-Peter Clausen } 23500f2780adSLars-Peter Clausen EXPORT_SYMBOL_GPL(snd_soc_add_component_controls); 23510f2780adSLars-Peter Clausen 23520f2780adSLars-Peter Clausen /** 2353022658beSLiam Girdwood * snd_soc_add_card_controls - add an array of controls to a SoC card. 2354022658beSLiam Girdwood * Convenience function to add a list of controls. 2355022658beSLiam Girdwood * 2356022658beSLiam Girdwood * @soc_card: SoC card to add controls to 2357022658beSLiam Girdwood * @controls: array of controls to add 2358022658beSLiam Girdwood * @num_controls: number of elements in the array 2359022658beSLiam Girdwood * 2360022658beSLiam Girdwood * Return 0 for success, else error. 2361022658beSLiam Girdwood */ 2362022658beSLiam Girdwood int snd_soc_add_card_controls(struct snd_soc_card *soc_card, 2363022658beSLiam Girdwood const struct snd_kcontrol_new *controls, int num_controls) 2364022658beSLiam Girdwood { 2365022658beSLiam Girdwood struct snd_card *card = soc_card->snd_card; 2366022658beSLiam Girdwood 2367022658beSLiam Girdwood return snd_soc_add_controls(card, soc_card->dev, controls, num_controls, 2368022658beSLiam Girdwood NULL, soc_card); 2369022658beSLiam Girdwood } 2370022658beSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_add_card_controls); 2371022658beSLiam Girdwood 2372022658beSLiam Girdwood /** 2373022658beSLiam Girdwood * snd_soc_add_dai_controls - add an array of controls to a DAI. 2374022658beSLiam Girdwood * Convienience function to add a list of controls. 2375022658beSLiam Girdwood * 2376022658beSLiam Girdwood * @dai: DAI to add controls to 2377022658beSLiam Girdwood * @controls: array of controls to add 2378022658beSLiam Girdwood * @num_controls: number of elements in the array 2379022658beSLiam Girdwood * 2380022658beSLiam Girdwood * Return 0 for success, else error. 2381022658beSLiam Girdwood */ 2382022658beSLiam Girdwood int snd_soc_add_dai_controls(struct snd_soc_dai *dai, 2383022658beSLiam Girdwood const struct snd_kcontrol_new *controls, int num_controls) 2384022658beSLiam Girdwood { 2385313665b9SLars-Peter Clausen struct snd_card *card = dai->component->card->snd_card; 2386022658beSLiam Girdwood 2387022658beSLiam Girdwood return snd_soc_add_controls(card, dai->dev, controls, num_controls, 2388022658beSLiam Girdwood NULL, dai); 2389022658beSLiam Girdwood } 2390022658beSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_add_dai_controls); 2391022658beSLiam Girdwood 2392022658beSLiam Girdwood /** 23938c6529dbSLiam Girdwood * snd_soc_dai_set_sysclk - configure DAI system or master clock. 23948c6529dbSLiam Girdwood * @dai: DAI 23958c6529dbSLiam Girdwood * @clk_id: DAI specific clock ID 23968c6529dbSLiam Girdwood * @freq: new clock frequency in Hz 23978c6529dbSLiam Girdwood * @dir: new clock direction - input/output. 23988c6529dbSLiam Girdwood * 23998c6529dbSLiam Girdwood * Configures the DAI master (MCLK) or system (SYSCLK) clocking. 24008c6529dbSLiam Girdwood */ 24018c6529dbSLiam Girdwood int snd_soc_dai_set_sysclk(struct snd_soc_dai *dai, int clk_id, 24028c6529dbSLiam Girdwood unsigned int freq, int dir) 24038c6529dbSLiam Girdwood { 240446471925SKuninori Morimoto if (dai->driver->ops->set_sysclk) 2405f0fba2adSLiam Girdwood return dai->driver->ops->set_sysclk(dai, clk_id, freq, dir); 240671ccef0dSKuninori Morimoto 240771ccef0dSKuninori Morimoto return snd_soc_component_set_sysclk(dai->component, clk_id, 0, 2408ec4ee52aSMark Brown freq, dir); 24098c6529dbSLiam Girdwood } 24108c6529dbSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_dai_set_sysclk); 24118c6529dbSLiam Girdwood 24128c6529dbSLiam Girdwood /** 241371ccef0dSKuninori Morimoto * snd_soc_component_set_sysclk - configure COMPONENT system or master clock. 241471ccef0dSKuninori Morimoto * @component: COMPONENT 241571ccef0dSKuninori Morimoto * @clk_id: DAI specific clock ID 241671ccef0dSKuninori Morimoto * @source: Source for the clock 241771ccef0dSKuninori Morimoto * @freq: new clock frequency in Hz 241871ccef0dSKuninori Morimoto * @dir: new clock direction - input/output. 241971ccef0dSKuninori Morimoto * 242071ccef0dSKuninori Morimoto * Configures the CODEC master (MCLK) or system (SYSCLK) clocking. 242171ccef0dSKuninori Morimoto */ 242271ccef0dSKuninori Morimoto int snd_soc_component_set_sysclk(struct snd_soc_component *component, int clk_id, 242371ccef0dSKuninori Morimoto int source, unsigned int freq, int dir) 242471ccef0dSKuninori Morimoto { 242571ccef0dSKuninori Morimoto if (component->driver->set_sysclk) 242671ccef0dSKuninori Morimoto return component->driver->set_sysclk(component, clk_id, source, 242771ccef0dSKuninori Morimoto freq, dir); 242871ccef0dSKuninori Morimoto 242971ccef0dSKuninori Morimoto return -ENOTSUPP; 243071ccef0dSKuninori Morimoto } 243171ccef0dSKuninori Morimoto EXPORT_SYMBOL_GPL(snd_soc_component_set_sysclk); 243271ccef0dSKuninori Morimoto 243371ccef0dSKuninori Morimoto /** 24348c6529dbSLiam Girdwood * snd_soc_dai_set_clkdiv - configure DAI clock dividers. 24358c6529dbSLiam Girdwood * @dai: DAI 2436ac11a2b3SMark Brown * @div_id: DAI specific clock divider ID 24378c6529dbSLiam Girdwood * @div: new clock divisor. 24388c6529dbSLiam Girdwood * 24398c6529dbSLiam Girdwood * Configures the clock dividers. This is used to derive the best DAI bit and 24408c6529dbSLiam Girdwood * frame clocks from the system or master clock. It's best to set the DAI bit 24418c6529dbSLiam Girdwood * and frame clocks as low as possible to save system power. 24428c6529dbSLiam Girdwood */ 24438c6529dbSLiam Girdwood int snd_soc_dai_set_clkdiv(struct snd_soc_dai *dai, 24448c6529dbSLiam Girdwood int div_id, int div) 24458c6529dbSLiam Girdwood { 244646471925SKuninori Morimoto if (dai->driver->ops->set_clkdiv) 2447f0fba2adSLiam Girdwood return dai->driver->ops->set_clkdiv(dai, div_id, div); 24488c6529dbSLiam Girdwood else 24498c6529dbSLiam Girdwood return -EINVAL; 24508c6529dbSLiam Girdwood } 24518c6529dbSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_dai_set_clkdiv); 24528c6529dbSLiam Girdwood 24538c6529dbSLiam Girdwood /** 24548c6529dbSLiam Girdwood * snd_soc_dai_set_pll - configure DAI PLL. 24558c6529dbSLiam Girdwood * @dai: DAI 24568c6529dbSLiam Girdwood * @pll_id: DAI specific PLL ID 245785488037SMark Brown * @source: DAI specific source for the PLL 24588c6529dbSLiam Girdwood * @freq_in: PLL input clock frequency in Hz 24598c6529dbSLiam Girdwood * @freq_out: requested PLL output clock frequency in Hz 24608c6529dbSLiam Girdwood * 24618c6529dbSLiam Girdwood * Configures and enables PLL to generate output clock based on input clock. 24628c6529dbSLiam Girdwood */ 246385488037SMark Brown int snd_soc_dai_set_pll(struct snd_soc_dai *dai, int pll_id, int source, 246485488037SMark Brown unsigned int freq_in, unsigned int freq_out) 24658c6529dbSLiam Girdwood { 246646471925SKuninori Morimoto if (dai->driver->ops->set_pll) 2467f0fba2adSLiam Girdwood return dai->driver->ops->set_pll(dai, pll_id, source, 246885488037SMark Brown freq_in, freq_out); 2469ef641e5dSKuninori Morimoto 2470ef641e5dSKuninori Morimoto return snd_soc_component_set_pll(dai->component, pll_id, source, 2471ec4ee52aSMark Brown freq_in, freq_out); 24728c6529dbSLiam Girdwood } 24738c6529dbSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_dai_set_pll); 24748c6529dbSLiam Girdwood 2475ec4ee52aSMark Brown /* 2476ef641e5dSKuninori Morimoto * snd_soc_component_set_pll - configure component PLL. 2477ef641e5dSKuninori Morimoto * @component: COMPONENT 2478ef641e5dSKuninori Morimoto * @pll_id: DAI specific PLL ID 2479ef641e5dSKuninori Morimoto * @source: DAI specific source for the PLL 2480ef641e5dSKuninori Morimoto * @freq_in: PLL input clock frequency in Hz 2481ef641e5dSKuninori Morimoto * @freq_out: requested PLL output clock frequency in Hz 2482ef641e5dSKuninori Morimoto * 2483ef641e5dSKuninori Morimoto * Configures and enables PLL to generate output clock based on input clock. 2484ef641e5dSKuninori Morimoto */ 2485ef641e5dSKuninori Morimoto int snd_soc_component_set_pll(struct snd_soc_component *component, int pll_id, 2486ef641e5dSKuninori Morimoto int source, unsigned int freq_in, 2487ef641e5dSKuninori Morimoto unsigned int freq_out) 2488ef641e5dSKuninori Morimoto { 2489ef641e5dSKuninori Morimoto if (component->driver->set_pll) 2490ef641e5dSKuninori Morimoto return component->driver->set_pll(component, pll_id, source, 2491ef641e5dSKuninori Morimoto freq_in, freq_out); 2492ef641e5dSKuninori Morimoto 2493ef641e5dSKuninori Morimoto return -EINVAL; 2494ef641e5dSKuninori Morimoto } 2495ef641e5dSKuninori Morimoto EXPORT_SYMBOL_GPL(snd_soc_component_set_pll); 2496ef641e5dSKuninori Morimoto 24978c6529dbSLiam Girdwood /** 2498e54cf76bSLiam Girdwood * snd_soc_dai_set_bclk_ratio - configure BCLK to sample rate ratio. 2499e54cf76bSLiam Girdwood * @dai: DAI 2500231b86b1SMasanari Iida * @ratio: Ratio of BCLK to Sample rate. 2501e54cf76bSLiam Girdwood * 2502e54cf76bSLiam Girdwood * Configures the DAI for a preset BCLK to sample rate ratio. 2503e54cf76bSLiam Girdwood */ 2504e54cf76bSLiam Girdwood int snd_soc_dai_set_bclk_ratio(struct snd_soc_dai *dai, unsigned int ratio) 2505e54cf76bSLiam Girdwood { 250646471925SKuninori Morimoto if (dai->driver->ops->set_bclk_ratio) 2507e54cf76bSLiam Girdwood return dai->driver->ops->set_bclk_ratio(dai, ratio); 2508e54cf76bSLiam Girdwood else 2509e54cf76bSLiam Girdwood return -EINVAL; 2510e54cf76bSLiam Girdwood } 2511e54cf76bSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_dai_set_bclk_ratio); 2512e54cf76bSLiam Girdwood 2513e54cf76bSLiam Girdwood /** 25148c6529dbSLiam Girdwood * snd_soc_dai_set_fmt - configure DAI hardware audio format. 25158c6529dbSLiam Girdwood * @dai: DAI 2516bb19ba2aSRandy Dunlap * @fmt: SND_SOC_DAIFMT_* format value. 25178c6529dbSLiam Girdwood * 25188c6529dbSLiam Girdwood * Configures the DAI hardware format and clocking. 25198c6529dbSLiam Girdwood */ 25208c6529dbSLiam Girdwood int snd_soc_dai_set_fmt(struct snd_soc_dai *dai, unsigned int fmt) 25218c6529dbSLiam Girdwood { 25225e4ba569SShawn Guo if (dai->driver == NULL) 25238c6529dbSLiam Girdwood return -EINVAL; 25245e4ba569SShawn Guo if (dai->driver->ops->set_fmt == NULL) 25255e4ba569SShawn Guo return -ENOTSUPP; 25265e4ba569SShawn Guo return dai->driver->ops->set_fmt(dai, fmt); 25278c6529dbSLiam Girdwood } 25288c6529dbSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_dai_set_fmt); 25298c6529dbSLiam Girdwood 25308c6529dbSLiam Girdwood /** 2531e5c21514SXiubo Li * snd_soc_xlate_tdm_slot - generate tx/rx slot mask. 253289c67857SXiubo Li * @slots: Number of slots in use. 253389c67857SXiubo Li * @tx_mask: bitmask representing active TX slots. 253489c67857SXiubo Li * @rx_mask: bitmask representing active RX slots. 253589c67857SXiubo Li * 253689c67857SXiubo Li * Generates the TDM tx and rx slot default masks for DAI. 253789c67857SXiubo Li */ 2538e5c21514SXiubo Li static int snd_soc_xlate_tdm_slot_mask(unsigned int slots, 253989c67857SXiubo Li unsigned int *tx_mask, 254089c67857SXiubo Li unsigned int *rx_mask) 254189c67857SXiubo Li { 254289c67857SXiubo Li if (*tx_mask || *rx_mask) 254389c67857SXiubo Li return 0; 254489c67857SXiubo Li 254589c67857SXiubo Li if (!slots) 254689c67857SXiubo Li return -EINVAL; 254789c67857SXiubo Li 254889c67857SXiubo Li *tx_mask = (1 << slots) - 1; 254989c67857SXiubo Li *rx_mask = (1 << slots) - 1; 255089c67857SXiubo Li 255189c67857SXiubo Li return 0; 255289c67857SXiubo Li } 255389c67857SXiubo Li 255489c67857SXiubo Li /** 2555e46c9366SLars-Peter Clausen * snd_soc_dai_set_tdm_slot() - Configures a DAI for TDM operation 2556e46c9366SLars-Peter Clausen * @dai: The DAI to configure 2557a5479e38SDaniel Ribeiro * @tx_mask: bitmask representing active TX slots. 2558a5479e38SDaniel Ribeiro * @rx_mask: bitmask representing active RX slots. 25598c6529dbSLiam Girdwood * @slots: Number of slots in use. 2560a5479e38SDaniel Ribeiro * @slot_width: Width in bits for each slot. 25618c6529dbSLiam Girdwood * 2562e46c9366SLars-Peter Clausen * This function configures the specified DAI for TDM operation. @slot contains 2563e46c9366SLars-Peter Clausen * the total number of slots of the TDM stream and @slot_with the width of each 2564e46c9366SLars-Peter Clausen * slot in bit clock cycles. @tx_mask and @rx_mask are bitmasks specifying the 2565e46c9366SLars-Peter Clausen * active slots of the TDM stream for the specified DAI, i.e. which slots the 2566e46c9366SLars-Peter Clausen * DAI should write to or read from. If a bit is set the corresponding slot is 2567e46c9366SLars-Peter Clausen * active, if a bit is cleared the corresponding slot is inactive. Bit 0 maps to 2568e46c9366SLars-Peter Clausen * the first slot, bit 1 to the second slot and so on. The first active slot 2569e46c9366SLars-Peter Clausen * maps to the first channel of the DAI, the second active slot to the second 2570e46c9366SLars-Peter Clausen * channel and so on. 2571e46c9366SLars-Peter Clausen * 2572e46c9366SLars-Peter Clausen * TDM mode can be disabled by passing 0 for @slots. In this case @tx_mask, 2573e46c9366SLars-Peter Clausen * @rx_mask and @slot_width will be ignored. 2574e46c9366SLars-Peter Clausen * 2575e46c9366SLars-Peter Clausen * Returns 0 on success, a negative error code otherwise. 25768c6529dbSLiam Girdwood */ 25778c6529dbSLiam Girdwood int snd_soc_dai_set_tdm_slot(struct snd_soc_dai *dai, 2578a5479e38SDaniel Ribeiro unsigned int tx_mask, unsigned int rx_mask, int slots, int slot_width) 25798c6529dbSLiam Girdwood { 258046471925SKuninori Morimoto if (dai->driver->ops->xlate_tdm_slot_mask) 2581e5c21514SXiubo Li dai->driver->ops->xlate_tdm_slot_mask(slots, 258289c67857SXiubo Li &tx_mask, &rx_mask); 258389c67857SXiubo Li else 2584e5c21514SXiubo Li snd_soc_xlate_tdm_slot_mask(slots, &tx_mask, &rx_mask); 258589c67857SXiubo Li 258688bd870fSBenoit Cousson dai->tx_mask = tx_mask; 258788bd870fSBenoit Cousson dai->rx_mask = rx_mask; 258888bd870fSBenoit Cousson 258946471925SKuninori Morimoto if (dai->driver->ops->set_tdm_slot) 2590f0fba2adSLiam Girdwood return dai->driver->ops->set_tdm_slot(dai, tx_mask, rx_mask, 2591a5479e38SDaniel Ribeiro slots, slot_width); 25928c6529dbSLiam Girdwood else 2593b2cbb6e1SXiubo Li return -ENOTSUPP; 25948c6529dbSLiam Girdwood } 25958c6529dbSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_dai_set_tdm_slot); 25968c6529dbSLiam Girdwood 25978c6529dbSLiam Girdwood /** 2598472df3cbSBarry Song * snd_soc_dai_set_channel_map - configure DAI audio channel map 2599472df3cbSBarry Song * @dai: DAI 2600472df3cbSBarry Song * @tx_num: how many TX channels 2601472df3cbSBarry Song * @tx_slot: pointer to an array which imply the TX slot number channel 2602472df3cbSBarry Song * 0~num-1 uses 2603472df3cbSBarry Song * @rx_num: how many RX channels 2604472df3cbSBarry Song * @rx_slot: pointer to an array which imply the RX slot number channel 2605472df3cbSBarry Song * 0~num-1 uses 2606472df3cbSBarry Song * 2607472df3cbSBarry Song * configure the relationship between channel number and TDM slot number. 2608472df3cbSBarry Song */ 2609472df3cbSBarry Song int snd_soc_dai_set_channel_map(struct snd_soc_dai *dai, 2610472df3cbSBarry Song unsigned int tx_num, unsigned int *tx_slot, 2611472df3cbSBarry Song unsigned int rx_num, unsigned int *rx_slot) 2612472df3cbSBarry Song { 261346471925SKuninori Morimoto if (dai->driver->ops->set_channel_map) 2614f0fba2adSLiam Girdwood return dai->driver->ops->set_channel_map(dai, tx_num, tx_slot, 2615472df3cbSBarry Song rx_num, rx_slot); 2616472df3cbSBarry Song else 2617472df3cbSBarry Song return -EINVAL; 2618472df3cbSBarry Song } 2619472df3cbSBarry Song EXPORT_SYMBOL_GPL(snd_soc_dai_set_channel_map); 2620472df3cbSBarry Song 2621472df3cbSBarry Song /** 2622467b061fSSrinivas Kandagatla * snd_soc_dai_get_channel_map - Get DAI audio channel map 2623467b061fSSrinivas Kandagatla * @dai: DAI 2624467b061fSSrinivas Kandagatla * @tx_num: how many TX channels 2625467b061fSSrinivas Kandagatla * @tx_slot: pointer to an array which imply the TX slot number channel 2626467b061fSSrinivas Kandagatla * 0~num-1 uses 2627467b061fSSrinivas Kandagatla * @rx_num: how many RX channels 2628467b061fSSrinivas Kandagatla * @rx_slot: pointer to an array which imply the RX slot number channel 2629467b061fSSrinivas Kandagatla * 0~num-1 uses 2630467b061fSSrinivas Kandagatla */ 2631467b061fSSrinivas Kandagatla int snd_soc_dai_get_channel_map(struct snd_soc_dai *dai, 2632467b061fSSrinivas Kandagatla unsigned int *tx_num, unsigned int *tx_slot, 2633467b061fSSrinivas Kandagatla unsigned int *rx_num, unsigned int *rx_slot) 2634467b061fSSrinivas Kandagatla { 2635467b061fSSrinivas Kandagatla if (dai->driver->ops->get_channel_map) 2636467b061fSSrinivas Kandagatla return dai->driver->ops->get_channel_map(dai, tx_num, tx_slot, 2637467b061fSSrinivas Kandagatla rx_num, rx_slot); 2638467b061fSSrinivas Kandagatla else 2639467b061fSSrinivas Kandagatla return -ENOTSUPP; 2640467b061fSSrinivas Kandagatla } 2641467b061fSSrinivas Kandagatla EXPORT_SYMBOL_GPL(snd_soc_dai_get_channel_map); 2642467b061fSSrinivas Kandagatla 2643467b061fSSrinivas Kandagatla /** 26448c6529dbSLiam Girdwood * snd_soc_dai_set_tristate - configure DAI system or master clock. 26458c6529dbSLiam Girdwood * @dai: DAI 26468c6529dbSLiam Girdwood * @tristate: tristate enable 26478c6529dbSLiam Girdwood * 26488c6529dbSLiam Girdwood * Tristates the DAI so that others can use it. 26498c6529dbSLiam Girdwood */ 26508c6529dbSLiam Girdwood int snd_soc_dai_set_tristate(struct snd_soc_dai *dai, int tristate) 26518c6529dbSLiam Girdwood { 265246471925SKuninori Morimoto if (dai->driver->ops->set_tristate) 2653f0fba2adSLiam Girdwood return dai->driver->ops->set_tristate(dai, tristate); 26548c6529dbSLiam Girdwood else 26558c6529dbSLiam Girdwood return -EINVAL; 26568c6529dbSLiam Girdwood } 26578c6529dbSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_dai_set_tristate); 26588c6529dbSLiam Girdwood 26598c6529dbSLiam Girdwood /** 26608c6529dbSLiam Girdwood * snd_soc_dai_digital_mute - configure DAI system or master clock. 26618c6529dbSLiam Girdwood * @dai: DAI 26628c6529dbSLiam Girdwood * @mute: mute enable 2663da18396fSMark Brown * @direction: stream to mute 26648c6529dbSLiam Girdwood * 26658c6529dbSLiam Girdwood * Mutes the DAI DAC. 26668c6529dbSLiam Girdwood */ 2667da18396fSMark Brown int snd_soc_dai_digital_mute(struct snd_soc_dai *dai, int mute, 2668da18396fSMark Brown int direction) 26698c6529dbSLiam Girdwood { 2670da18396fSMark Brown if (!dai->driver) 2671da18396fSMark Brown return -ENOTSUPP; 2672da18396fSMark Brown 2673da18396fSMark Brown if (dai->driver->ops->mute_stream) 2674da18396fSMark Brown return dai->driver->ops->mute_stream(dai, mute, direction); 2675da18396fSMark Brown else if (direction == SNDRV_PCM_STREAM_PLAYBACK && 2676da18396fSMark Brown dai->driver->ops->digital_mute) 2677f0fba2adSLiam Girdwood return dai->driver->ops->digital_mute(dai, mute); 26788c6529dbSLiam Girdwood else 267904570c62SMark Brown return -ENOTSUPP; 26808c6529dbSLiam Girdwood } 26818c6529dbSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_dai_digital_mute); 26828c6529dbSLiam Girdwood 2683*e894efefSSrinivas Kandagatla static int snd_soc_bind_card(struct snd_soc_card *card) 2684*e894efefSSrinivas Kandagatla { 2685*e894efefSSrinivas Kandagatla struct snd_soc_pcm_runtime *rtd; 2686*e894efefSSrinivas Kandagatla int ret; 2687*e894efefSSrinivas Kandagatla 2688*e894efefSSrinivas Kandagatla ret = snd_soc_instantiate_card(card); 2689*e894efefSSrinivas Kandagatla if (ret != 0) 2690*e894efefSSrinivas Kandagatla return ret; 2691*e894efefSSrinivas Kandagatla 2692*e894efefSSrinivas Kandagatla /* deactivate pins to sleep state */ 2693*e894efefSSrinivas Kandagatla list_for_each_entry(rtd, &card->rtd_list, list) { 2694*e894efefSSrinivas Kandagatla struct snd_soc_dai *cpu_dai = rtd->cpu_dai; 2695*e894efefSSrinivas Kandagatla struct snd_soc_dai *codec_dai; 2696*e894efefSSrinivas Kandagatla int j; 2697*e894efefSSrinivas Kandagatla 2698*e894efefSSrinivas Kandagatla for_each_rtd_codec_dai(rtd, j, codec_dai) { 2699*e894efefSSrinivas Kandagatla if (!codec_dai->active) 2700*e894efefSSrinivas Kandagatla pinctrl_pm_select_sleep_state(codec_dai->dev); 2701*e894efefSSrinivas Kandagatla } 2702*e894efefSSrinivas Kandagatla 2703*e894efefSSrinivas Kandagatla if (!cpu_dai->active) 2704*e894efefSSrinivas Kandagatla pinctrl_pm_select_sleep_state(cpu_dai->dev); 2705*e894efefSSrinivas Kandagatla } 2706*e894efefSSrinivas Kandagatla 2707*e894efefSSrinivas Kandagatla return ret; 2708*e894efefSSrinivas Kandagatla } 2709*e894efefSSrinivas Kandagatla 2710c5af3a2eSMark Brown /** 2711c5af3a2eSMark Brown * snd_soc_register_card - Register a card with the ASoC core 2712c5af3a2eSMark Brown * 2713ac11a2b3SMark Brown * @card: Card to register 2714c5af3a2eSMark Brown * 2715c5af3a2eSMark Brown */ 271670a7ca34SVinod Koul int snd_soc_register_card(struct snd_soc_card *card) 2717c5af3a2eSMark Brown { 2718923c5e61SMengdong Lin int i, ret; 2719f0fba2adSLiam Girdwood 2720c5af3a2eSMark Brown if (!card->name || !card->dev) 2721c5af3a2eSMark Brown return -EINVAL; 2722c5af3a2eSMark Brown 27235a504963SStephen Warren for (i = 0; i < card->num_links; i++) { 27245a504963SStephen Warren struct snd_soc_dai_link *link = &card->dai_link[i]; 27255a504963SStephen Warren 2726923c5e61SMengdong Lin ret = soc_init_dai_link(card, link); 272788bd870fSBenoit Cousson if (ret) { 2728923c5e61SMengdong Lin dev_err(card->dev, "ASoC: failed to init link %s\n", 2729923c5e61SMengdong Lin link->name); 273088bd870fSBenoit Cousson return ret; 273188bd870fSBenoit Cousson } 27325a504963SStephen Warren } 27335a504963SStephen Warren 2734ed77cc12SMark Brown dev_set_drvdata(card->dev, card); 2735ed77cc12SMark Brown 2736111c6419SStephen Warren snd_soc_initialize_card_lists(card); 2737111c6419SStephen Warren 2738f8f80361SMengdong Lin INIT_LIST_HEAD(&card->dai_link_list); 2739f0fba2adSLiam Girdwood 27401a497983SMengdong Lin INIT_LIST_HEAD(&card->rtd_list); 27419115171aSMark Brown card->num_rtd = 0; 2742db2a4165SFrank Mandarino 2743db432b41SMark Brown INIT_LIST_HEAD(&card->dapm_dirty); 27448a978234SLiam Girdwood INIT_LIST_HEAD(&card->dobj_list); 2745db2a4165SFrank Mandarino card->instantiated = 0; 2746db2a4165SFrank Mandarino mutex_init(&card->mutex); 2747a73fb2dfSLiam Girdwood mutex_init(&card->dapm_mutex); 2748db2a4165SFrank Mandarino 2749*e894efefSSrinivas Kandagatla return snd_soc_bind_card(card); 2750db2a4165SFrank Mandarino } 275170a7ca34SVinod Koul EXPORT_SYMBOL_GPL(snd_soc_register_card); 2752db2a4165SFrank Mandarino 2753*e894efefSSrinivas Kandagatla static void snd_soc_unbind_card(struct snd_soc_card *card, bool unregister) 2754*e894efefSSrinivas Kandagatla { 2755*e894efefSSrinivas Kandagatla if (card->instantiated) { 2756*e894efefSSrinivas Kandagatla card->instantiated = false; 2757*e894efefSSrinivas Kandagatla snd_soc_dapm_shutdown(card); 2758*e894efefSSrinivas Kandagatla soc_cleanup_card_resources(card); 2759*e894efefSSrinivas Kandagatla if (!unregister) 2760*e894efefSSrinivas Kandagatla list_add(&card->list, &unbind_card_list); 2761*e894efefSSrinivas Kandagatla } else { 2762*e894efefSSrinivas Kandagatla if (unregister) 2763*e894efefSSrinivas Kandagatla list_del(&card->list); 2764*e894efefSSrinivas Kandagatla } 2765*e894efefSSrinivas Kandagatla } 2766*e894efefSSrinivas Kandagatla 2767db2a4165SFrank Mandarino /** 2768db2a4165SFrank Mandarino * snd_soc_unregister_card - Unregister a card with the ASoC core 2769db2a4165SFrank Mandarino * 2770db2a4165SFrank Mandarino * @card: Card to unregister 2771db2a4165SFrank Mandarino * 2772db2a4165SFrank Mandarino */ 277370a7ca34SVinod Koul int snd_soc_unregister_card(struct snd_soc_card *card) 2774db2a4165SFrank Mandarino { 2775*e894efefSSrinivas Kandagatla snd_soc_unbind_card(card, true); 2776f110bfc7SLiam Girdwood dev_dbg(card->dev, "ASoC: Unregistered card '%s'\n", card->name); 2777db2a4165SFrank Mandarino 2778db2a4165SFrank Mandarino return 0; 2779db2a4165SFrank Mandarino } 278070a7ca34SVinod Koul EXPORT_SYMBOL_GPL(snd_soc_unregister_card); 2781db2a4165SFrank Mandarino 2782db2a4165SFrank Mandarino /* 2783db2a4165SFrank Mandarino * Simplify DAI link configuration by removing ".-1" from device names 2784db2a4165SFrank Mandarino * and sanitizing names. 2785db2a4165SFrank Mandarino */ 27860b9a214aSDimitris Papastamos static char *fmt_single_name(struct device *dev, int *id) 2787db2a4165SFrank Mandarino { 2788db2a4165SFrank Mandarino char *found, name[NAME_SIZE]; 2789db2a4165SFrank Mandarino int id1, id2; 2790db2a4165SFrank Mandarino 2791db2a4165SFrank Mandarino if (dev_name(dev) == NULL) 2792db2a4165SFrank Mandarino return NULL; 2793db2a4165SFrank Mandarino 279458818a77SDimitris Papastamos strlcpy(name, dev_name(dev), NAME_SIZE); 2795db2a4165SFrank Mandarino 2796db2a4165SFrank Mandarino /* are we a "%s.%d" name (platform and SPI components) */ 2797c5af3a2eSMark Brown found = strstr(name, dev->driver->name); 2798c5af3a2eSMark Brown if (found) { 2799c5af3a2eSMark Brown /* get ID */ 2800c5af3a2eSMark Brown if (sscanf(&found[strlen(dev->driver->name)], ".%d", id) == 1) { 2801c5af3a2eSMark Brown 2802c5af3a2eSMark Brown /* discard ID from name if ID == -1 */ 2803c5af3a2eSMark Brown if (*id == -1) 2804c5af3a2eSMark Brown found[strlen(dev->driver->name)] = '\0'; 2805c5af3a2eSMark Brown } 2806c5af3a2eSMark Brown 2807c5af3a2eSMark Brown } else { 2808c5af3a2eSMark Brown /* I2C component devices are named "bus-addr" */ 2809c5af3a2eSMark Brown if (sscanf(name, "%x-%x", &id1, &id2) == 2) { 2810c5af3a2eSMark Brown char tmp[NAME_SIZE]; 2811c5af3a2eSMark Brown 2812c5af3a2eSMark Brown /* create unique ID number from I2C addr and bus */ 2813c5af3a2eSMark Brown *id = ((id1 & 0xffff) << 16) + id2; 2814c5af3a2eSMark Brown 2815c5af3a2eSMark Brown /* sanitize component name for DAI link creation */ 2816c5af3a2eSMark Brown snprintf(tmp, NAME_SIZE, "%s.%s", dev->driver->name, name); 281758818a77SDimitris Papastamos strlcpy(name, tmp, NAME_SIZE); 2818c5af3a2eSMark Brown } else 2819c5af3a2eSMark Brown *id = 0; 2820c5af3a2eSMark Brown } 2821c5af3a2eSMark Brown 2822c5af3a2eSMark Brown return kstrdup(name, GFP_KERNEL); 2823c5af3a2eSMark Brown } 2824c5af3a2eSMark Brown 2825c5af3a2eSMark Brown /* 2826c5af3a2eSMark Brown * Simplify DAI link naming for single devices with multiple DAIs by removing 2827c5af3a2eSMark Brown * any ".-1" and using the DAI name (instead of device name). 2828c5af3a2eSMark Brown */ 2829c5af3a2eSMark Brown static inline char *fmt_multiple_name(struct device *dev, 2830c5af3a2eSMark Brown struct snd_soc_dai_driver *dai_drv) 2831c5af3a2eSMark Brown { 2832c5af3a2eSMark Brown if (dai_drv->name == NULL) { 283310e8aa9aSMichał Mirosław dev_err(dev, 283410e8aa9aSMichał Mirosław "ASoC: error - multiple DAI %s registered with no name\n", 283510e8aa9aSMichał Mirosław dev_name(dev)); 2836c5af3a2eSMark Brown return NULL; 2837c5af3a2eSMark Brown } 2838c5af3a2eSMark Brown 2839c5af3a2eSMark Brown return kstrdup(dai_drv->name, GFP_KERNEL); 2840c5af3a2eSMark Brown } 2841c5af3a2eSMark Brown 2842c5af3a2eSMark Brown /** 284332c9ba54SLars-Peter Clausen * snd_soc_unregister_dai - Unregister DAIs from the ASoC core 28449115171aSMark Brown * 284532c9ba54SLars-Peter Clausen * @component: The component for which the DAIs should be unregistered 28469115171aSMark Brown */ 284732c9ba54SLars-Peter Clausen static void snd_soc_unregister_dais(struct snd_soc_component *component) 28489115171aSMark Brown { 28495c1d5f09SLars-Peter Clausen struct snd_soc_dai *dai, *_dai; 28509115171aSMark Brown 28515c1d5f09SLars-Peter Clausen list_for_each_entry_safe(dai, _dai, &component->dai_list, list) { 285232c9ba54SLars-Peter Clausen dev_dbg(component->dev, "ASoC: Unregistered DAI '%s'\n", 285332c9ba54SLars-Peter Clausen dai->name); 28549115171aSMark Brown list_del(&dai->list); 2855f0fba2adSLiam Girdwood kfree(dai->name); 2856f0fba2adSLiam Girdwood kfree(dai); 28579115171aSMark Brown } 285832c9ba54SLars-Peter Clausen } 28599115171aSMark Brown 28605e4fb372SMengdong Lin /* Create a DAI and add it to the component's DAI list */ 28615e4fb372SMengdong Lin static struct snd_soc_dai *soc_add_dai(struct snd_soc_component *component, 28625e4fb372SMengdong Lin struct snd_soc_dai_driver *dai_drv, 28635e4fb372SMengdong Lin bool legacy_dai_naming) 28645e4fb372SMengdong Lin { 28655e4fb372SMengdong Lin struct device *dev = component->dev; 28665e4fb372SMengdong Lin struct snd_soc_dai *dai; 28675e4fb372SMengdong Lin 28685e4fb372SMengdong Lin dev_dbg(dev, "ASoC: dynamically register DAI %s\n", dev_name(dev)); 28695e4fb372SMengdong Lin 28705e4fb372SMengdong Lin dai = kzalloc(sizeof(struct snd_soc_dai), GFP_KERNEL); 28715e4fb372SMengdong Lin if (dai == NULL) 28725e4fb372SMengdong Lin return NULL; 28735e4fb372SMengdong Lin 28745e4fb372SMengdong Lin /* 28755e4fb372SMengdong Lin * Back in the old days when we still had component-less DAIs, 28765e4fb372SMengdong Lin * instead of having a static name, component-less DAIs would 28775e4fb372SMengdong Lin * inherit the name of the parent device so it is possible to 28785e4fb372SMengdong Lin * register multiple instances of the DAI. We still need to keep 28795e4fb372SMengdong Lin * the same naming style even though those DAIs are not 28805e4fb372SMengdong Lin * component-less anymore. 28815e4fb372SMengdong Lin */ 28825e4fb372SMengdong Lin if (legacy_dai_naming && 28835e4fb372SMengdong Lin (dai_drv->id == 0 || dai_drv->name == NULL)) { 28845e4fb372SMengdong Lin dai->name = fmt_single_name(dev, &dai->id); 28855e4fb372SMengdong Lin } else { 28865e4fb372SMengdong Lin dai->name = fmt_multiple_name(dev, dai_drv); 28875e4fb372SMengdong Lin if (dai_drv->id) 28885e4fb372SMengdong Lin dai->id = dai_drv->id; 28895e4fb372SMengdong Lin else 28905e4fb372SMengdong Lin dai->id = component->num_dai; 28915e4fb372SMengdong Lin } 28925e4fb372SMengdong Lin if (dai->name == NULL) { 28935e4fb372SMengdong Lin kfree(dai); 28945e4fb372SMengdong Lin return NULL; 28955e4fb372SMengdong Lin } 28965e4fb372SMengdong Lin 28975e4fb372SMengdong Lin dai->component = component; 28985e4fb372SMengdong Lin dai->dev = dev; 28995e4fb372SMengdong Lin dai->driver = dai_drv; 29005e4fb372SMengdong Lin if (!dai->driver->ops) 29015e4fb372SMengdong Lin dai->driver->ops = &null_dai_ops; 29025e4fb372SMengdong Lin 290358bf4179SKuninori Morimoto list_add_tail(&dai->list, &component->dai_list); 29045e4fb372SMengdong Lin component->num_dai++; 29055e4fb372SMengdong Lin 29065e4fb372SMengdong Lin dev_dbg(dev, "ASoC: Registered DAI '%s'\n", dai->name); 29075e4fb372SMengdong Lin return dai; 29085e4fb372SMengdong Lin } 29095e4fb372SMengdong Lin 29109115171aSMark Brown /** 291132c9ba54SLars-Peter Clausen * snd_soc_register_dais - Register a DAI with the ASoC core 29129115171aSMark Brown * 29136106d129SLars-Peter Clausen * @component: The component the DAIs are registered for 29146106d129SLars-Peter Clausen * @dai_drv: DAI driver to use for the DAIs 2915ac11a2b3SMark Brown * @count: Number of DAIs 29169115171aSMark Brown */ 291732c9ba54SLars-Peter Clausen static int snd_soc_register_dais(struct snd_soc_component *component, 29180e7b25c6SKuninori Morimoto struct snd_soc_dai_driver *dai_drv, size_t count) 29199115171aSMark Brown { 29206106d129SLars-Peter Clausen struct device *dev = component->dev; 2921f0fba2adSLiam Girdwood struct snd_soc_dai *dai; 292232c9ba54SLars-Peter Clausen unsigned int i; 292332c9ba54SLars-Peter Clausen int ret; 2924f0fba2adSLiam Girdwood 29255b5e0928SAlexey Dobriyan dev_dbg(dev, "ASoC: dai register %s #%zu\n", dev_name(dev), count); 29269115171aSMark Brown 29279115171aSMark Brown for (i = 0; i < count; i++) { 2928f0fba2adSLiam Girdwood 29295e4fb372SMengdong Lin dai = soc_add_dai(component, dai_drv + i, 29300e7b25c6SKuninori Morimoto count == 1 && !component->driver->non_legacy_dai_naming); 2931c46e0079SAxel Lin if (dai == NULL) { 2932c46e0079SAxel Lin ret = -ENOMEM; 2933c46e0079SAxel Lin goto err; 2934c46e0079SAxel Lin } 2935f0fba2adSLiam Girdwood } 2936f0fba2adSLiam Girdwood 29379115171aSMark Brown return 0; 29389115171aSMark Brown 29399115171aSMark Brown err: 294032c9ba54SLars-Peter Clausen snd_soc_unregister_dais(component); 29419115171aSMark Brown 29429115171aSMark Brown return ret; 29439115171aSMark Brown } 29449115171aSMark Brown 294568003e6cSMengdong Lin /** 294668003e6cSMengdong Lin * snd_soc_register_dai - Register a DAI dynamically & create its widgets 294768003e6cSMengdong Lin * 294868003e6cSMengdong Lin * @component: The component the DAIs are registered for 294968003e6cSMengdong Lin * @dai_drv: DAI driver to use for the DAI 295068003e6cSMengdong Lin * 295168003e6cSMengdong Lin * Topology can use this API to register DAIs when probing a component. 295268003e6cSMengdong Lin * These DAIs's widgets will be freed in the card cleanup and the DAIs 295368003e6cSMengdong Lin * will be freed in the component cleanup. 295468003e6cSMengdong Lin */ 295568003e6cSMengdong Lin int snd_soc_register_dai(struct snd_soc_component *component, 295668003e6cSMengdong Lin struct snd_soc_dai_driver *dai_drv) 295768003e6cSMengdong Lin { 295868003e6cSMengdong Lin struct snd_soc_dapm_context *dapm = 295968003e6cSMengdong Lin snd_soc_component_get_dapm(component); 296068003e6cSMengdong Lin struct snd_soc_dai *dai; 296168003e6cSMengdong Lin int ret; 296268003e6cSMengdong Lin 296368003e6cSMengdong Lin if (dai_drv->dobj.type != SND_SOC_DOBJ_PCM) { 296468003e6cSMengdong Lin dev_err(component->dev, "Invalid dai type %d\n", 296568003e6cSMengdong Lin dai_drv->dobj.type); 296668003e6cSMengdong Lin return -EINVAL; 296768003e6cSMengdong Lin } 296868003e6cSMengdong Lin 296968003e6cSMengdong Lin lockdep_assert_held(&client_mutex); 297068003e6cSMengdong Lin dai = soc_add_dai(component, dai_drv, false); 297168003e6cSMengdong Lin if (!dai) 297268003e6cSMengdong Lin return -ENOMEM; 297368003e6cSMengdong Lin 297468003e6cSMengdong Lin /* Create the DAI widgets here. After adding DAIs, topology may 297568003e6cSMengdong Lin * also add routes that need these widgets as source or sink. 297668003e6cSMengdong Lin */ 297768003e6cSMengdong Lin ret = snd_soc_dapm_new_dai_widgets(dapm, dai); 297868003e6cSMengdong Lin if (ret != 0) { 297968003e6cSMengdong Lin dev_err(component->dev, 298068003e6cSMengdong Lin "Failed to create DAI widgets %d\n", ret); 298168003e6cSMengdong Lin } 298268003e6cSMengdong Lin 298368003e6cSMengdong Lin return ret; 298468003e6cSMengdong Lin } 298568003e6cSMengdong Lin EXPORT_SYMBOL_GPL(snd_soc_register_dai); 298668003e6cSMengdong Lin 298714e8bdebSLars-Peter Clausen static void snd_soc_component_seq_notifier(struct snd_soc_dapm_context *dapm, 298814e8bdebSLars-Peter Clausen enum snd_soc_dapm_type type, int subseq) 2989d191bd8dSKuninori Morimoto { 299014e8bdebSLars-Peter Clausen struct snd_soc_component *component = dapm->component; 2991d191bd8dSKuninori Morimoto 299214e8bdebSLars-Peter Clausen component->driver->seq_notifier(component, type, subseq); 299314e8bdebSLars-Peter Clausen } 2994d191bd8dSKuninori Morimoto 299514e8bdebSLars-Peter Clausen static int snd_soc_component_stream_event(struct snd_soc_dapm_context *dapm, 299614e8bdebSLars-Peter Clausen int event) 299714e8bdebSLars-Peter Clausen { 299814e8bdebSLars-Peter Clausen struct snd_soc_component *component = dapm->component; 299914e8bdebSLars-Peter Clausen 300014e8bdebSLars-Peter Clausen return component->driver->stream_event(component, event); 300114e8bdebSLars-Peter Clausen } 300214e8bdebSLars-Peter Clausen 30037ba236ceSKuninori Morimoto static int snd_soc_component_set_bias_level(struct snd_soc_dapm_context *dapm, 30047ba236ceSKuninori Morimoto enum snd_soc_bias_level level) 30057ba236ceSKuninori Morimoto { 30067ba236ceSKuninori Morimoto struct snd_soc_component *component = dapm->component; 30077ba236ceSKuninori Morimoto 30087ba236ceSKuninori Morimoto return component->driver->set_bias_level(component, level); 30097ba236ceSKuninori Morimoto } 30107ba236ceSKuninori Morimoto 3011bb13109dSLars-Peter Clausen static int snd_soc_component_initialize(struct snd_soc_component *component, 3012bb13109dSLars-Peter Clausen const struct snd_soc_component_driver *driver, struct device *dev) 3013d191bd8dSKuninori Morimoto { 3014ce0fc93aSLars-Peter Clausen struct snd_soc_dapm_context *dapm; 3015ce0fc93aSLars-Peter Clausen 3016bb13109dSLars-Peter Clausen component->name = fmt_single_name(dev, &component->id); 3017bb13109dSLars-Peter Clausen if (!component->name) { 3018bb13109dSLars-Peter Clausen dev_err(dev, "ASoC: Failed to allocate name\n"); 3019d191bd8dSKuninori Morimoto return -ENOMEM; 3020d191bd8dSKuninori Morimoto } 3021d191bd8dSKuninori Morimoto 3022bb13109dSLars-Peter Clausen component->dev = dev; 3023bb13109dSLars-Peter Clausen component->driver = driver; 3024e2c330b9SLars-Peter Clausen 302588c27465SKuninori Morimoto dapm = snd_soc_component_get_dapm(component); 3026ce0fc93aSLars-Peter Clausen dapm->dev = dev; 3027ce0fc93aSLars-Peter Clausen dapm->component = component; 3028ce0fc93aSLars-Peter Clausen dapm->bias_level = SND_SOC_BIAS_OFF; 30297ba236ceSKuninori Morimoto dapm->idle_bias_off = !driver->idle_bias_on; 30307ba236ceSKuninori Morimoto dapm->suspend_bias_off = driver->suspend_bias_off; 303114e8bdebSLars-Peter Clausen if (driver->seq_notifier) 303214e8bdebSLars-Peter Clausen dapm->seq_notifier = snd_soc_component_seq_notifier; 303314e8bdebSLars-Peter Clausen if (driver->stream_event) 303414e8bdebSLars-Peter Clausen dapm->stream_event = snd_soc_component_stream_event; 30357ba236ceSKuninori Morimoto if (driver->set_bias_level) 30367ba236ceSKuninori Morimoto dapm->set_bias_level = snd_soc_component_set_bias_level; 3037ce0fc93aSLars-Peter Clausen 3038bb13109dSLars-Peter Clausen INIT_LIST_HEAD(&component->dai_list); 3039bb13109dSLars-Peter Clausen mutex_init(&component->io_mutex); 3040bb13109dSLars-Peter Clausen 3041bb13109dSLars-Peter Clausen return 0; 3042d191bd8dSKuninori Morimoto } 3043d191bd8dSKuninori Morimoto 304420feb881SLars-Peter Clausen static void snd_soc_component_setup_regmap(struct snd_soc_component *component) 3045886f5692SLars-Peter Clausen { 3046886f5692SLars-Peter Clausen int val_bytes = regmap_get_val_bytes(component->regmap); 304720feb881SLars-Peter Clausen 3048886f5692SLars-Peter Clausen /* Errors are legitimate for non-integer byte multiples */ 3049886f5692SLars-Peter Clausen if (val_bytes > 0) 3050886f5692SLars-Peter Clausen component->val_bytes = val_bytes; 3051886f5692SLars-Peter Clausen } 305220feb881SLars-Peter Clausen 3053e874bf5fSLars-Peter Clausen #ifdef CONFIG_REGMAP 3054e874bf5fSLars-Peter Clausen 305520feb881SLars-Peter Clausen /** 305620feb881SLars-Peter Clausen * snd_soc_component_init_regmap() - Initialize regmap instance for the component 305720feb881SLars-Peter Clausen * @component: The component for which to initialize the regmap instance 305820feb881SLars-Peter Clausen * @regmap: The regmap instance that should be used by the component 305920feb881SLars-Peter Clausen * 306020feb881SLars-Peter Clausen * This function allows deferred assignment of the regmap instance that is 306120feb881SLars-Peter Clausen * associated with the component. Only use this if the regmap instance is not 306220feb881SLars-Peter Clausen * yet ready when the component is registered. The function must also be called 306320feb881SLars-Peter Clausen * before the first IO attempt of the component. 306420feb881SLars-Peter Clausen */ 306520feb881SLars-Peter Clausen void snd_soc_component_init_regmap(struct snd_soc_component *component, 306620feb881SLars-Peter Clausen struct regmap *regmap) 306720feb881SLars-Peter Clausen { 306820feb881SLars-Peter Clausen component->regmap = regmap; 306920feb881SLars-Peter Clausen snd_soc_component_setup_regmap(component); 3070886f5692SLars-Peter Clausen } 307120feb881SLars-Peter Clausen EXPORT_SYMBOL_GPL(snd_soc_component_init_regmap); 307220feb881SLars-Peter Clausen 307320feb881SLars-Peter Clausen /** 307420feb881SLars-Peter Clausen * snd_soc_component_exit_regmap() - De-initialize regmap instance for the component 307520feb881SLars-Peter Clausen * @component: The component for which to de-initialize the regmap instance 307620feb881SLars-Peter Clausen * 307720feb881SLars-Peter Clausen * Calls regmap_exit() on the regmap instance associated to the component and 307820feb881SLars-Peter Clausen * removes the regmap instance from the component. 307920feb881SLars-Peter Clausen * 308020feb881SLars-Peter Clausen * This function should only be used if snd_soc_component_init_regmap() was used 308120feb881SLars-Peter Clausen * to initialize the regmap instance. 308220feb881SLars-Peter Clausen */ 308320feb881SLars-Peter Clausen void snd_soc_component_exit_regmap(struct snd_soc_component *component) 308420feb881SLars-Peter Clausen { 308520feb881SLars-Peter Clausen regmap_exit(component->regmap); 308620feb881SLars-Peter Clausen component->regmap = NULL; 308720feb881SLars-Peter Clausen } 308820feb881SLars-Peter Clausen EXPORT_SYMBOL_GPL(snd_soc_component_exit_regmap); 3089886f5692SLars-Peter Clausen 3090e874bf5fSLars-Peter Clausen #endif 3091d191bd8dSKuninori Morimoto 3092359c71eeSKuninori Morimoto static void snd_soc_component_add(struct snd_soc_component *component) 3093bb13109dSLars-Peter Clausen { 3094359c71eeSKuninori Morimoto mutex_lock(&client_mutex); 3095359c71eeSKuninori Morimoto 3096999f7f5aSKuninori Morimoto if (!component->driver->write && !component->driver->read) { 309720feb881SLars-Peter Clausen if (!component->regmap) 309820feb881SLars-Peter Clausen component->regmap = dev_get_regmap(component->dev, NULL); 309920feb881SLars-Peter Clausen if (component->regmap) 310020feb881SLars-Peter Clausen snd_soc_component_setup_regmap(component); 310120feb881SLars-Peter Clausen } 3102886f5692SLars-Peter Clausen 3103bb13109dSLars-Peter Clausen list_add(&component->list, &component_list); 31048a978234SLiam Girdwood INIT_LIST_HEAD(&component->dobj_list); 3105d191bd8dSKuninori Morimoto 3106d191bd8dSKuninori Morimoto mutex_unlock(&client_mutex); 3107bb13109dSLars-Peter Clausen } 3108d191bd8dSKuninori Morimoto 3109bb13109dSLars-Peter Clausen static void snd_soc_component_cleanup(struct snd_soc_component *component) 3110bb13109dSLars-Peter Clausen { 3111bb13109dSLars-Peter Clausen snd_soc_unregister_dais(component); 3112bb13109dSLars-Peter Clausen kfree(component->name); 3113bb13109dSLars-Peter Clausen } 3114d191bd8dSKuninori Morimoto 3115bb13109dSLars-Peter Clausen static void snd_soc_component_del_unlocked(struct snd_soc_component *component) 3116bb13109dSLars-Peter Clausen { 3117c12c1aadSKuninori Morimoto struct snd_soc_card *card = component->card; 3118c12c1aadSKuninori Morimoto 3119c12c1aadSKuninori Morimoto if (card) 3120*e894efefSSrinivas Kandagatla snd_soc_unbind_card(card, false); 3121c12c1aadSKuninori Morimoto 3122bb13109dSLars-Peter Clausen list_del(&component->list); 3123bb13109dSLars-Peter Clausen } 3124d191bd8dSKuninori Morimoto 3125273d778eSKuninori Morimoto #define ENDIANNESS_MAP(name) \ 3126273d778eSKuninori Morimoto (SNDRV_PCM_FMTBIT_##name##LE | SNDRV_PCM_FMTBIT_##name##BE) 3127273d778eSKuninori Morimoto static u64 endianness_format_map[] = { 3128273d778eSKuninori Morimoto ENDIANNESS_MAP(S16_), 3129273d778eSKuninori Morimoto ENDIANNESS_MAP(U16_), 3130273d778eSKuninori Morimoto ENDIANNESS_MAP(S24_), 3131273d778eSKuninori Morimoto ENDIANNESS_MAP(U24_), 3132273d778eSKuninori Morimoto ENDIANNESS_MAP(S32_), 3133273d778eSKuninori Morimoto ENDIANNESS_MAP(U32_), 3134273d778eSKuninori Morimoto ENDIANNESS_MAP(S24_3), 3135273d778eSKuninori Morimoto ENDIANNESS_MAP(U24_3), 3136273d778eSKuninori Morimoto ENDIANNESS_MAP(S20_3), 3137273d778eSKuninori Morimoto ENDIANNESS_MAP(U20_3), 3138273d778eSKuninori Morimoto ENDIANNESS_MAP(S18_3), 3139273d778eSKuninori Morimoto ENDIANNESS_MAP(U18_3), 3140273d778eSKuninori Morimoto ENDIANNESS_MAP(FLOAT_), 3141273d778eSKuninori Morimoto ENDIANNESS_MAP(FLOAT64_), 3142273d778eSKuninori Morimoto ENDIANNESS_MAP(IEC958_SUBFRAME_), 3143273d778eSKuninori Morimoto }; 3144273d778eSKuninori Morimoto 3145273d778eSKuninori Morimoto /* 3146273d778eSKuninori Morimoto * Fix up the DAI formats for endianness: codecs don't actually see 3147273d778eSKuninori Morimoto * the endianness of the data but we're using the CPU format 3148273d778eSKuninori Morimoto * definitions which do need to include endianness so we ensure that 3149273d778eSKuninori Morimoto * codec DAIs always have both big and little endian variants set. 3150273d778eSKuninori Morimoto */ 3151273d778eSKuninori Morimoto static void convert_endianness_formats(struct snd_soc_pcm_stream *stream) 3152273d778eSKuninori Morimoto { 3153273d778eSKuninori Morimoto int i; 3154273d778eSKuninori Morimoto 3155273d778eSKuninori Morimoto for (i = 0; i < ARRAY_SIZE(endianness_format_map); i++) 3156273d778eSKuninori Morimoto if (stream->formats & endianness_format_map[i]) 3157273d778eSKuninori Morimoto stream->formats |= endianness_format_map[i]; 3158273d778eSKuninori Morimoto } 3159273d778eSKuninori Morimoto 3160*e894efefSSrinivas Kandagatla static void snd_soc_try_rebind_card(void) 3161*e894efefSSrinivas Kandagatla { 3162*e894efefSSrinivas Kandagatla struct snd_soc_card *card, *c; 3163*e894efefSSrinivas Kandagatla 3164*e894efefSSrinivas Kandagatla if (!list_empty(&unbind_card_list)) { 3165*e894efefSSrinivas Kandagatla list_for_each_entry_safe(card, c, &unbind_card_list, list) { 3166*e894efefSSrinivas Kandagatla if (!snd_soc_bind_card(card)) 3167*e894efefSSrinivas Kandagatla list_del(&card->list); 3168*e894efefSSrinivas Kandagatla } 3169*e894efefSSrinivas Kandagatla } 3170*e894efefSSrinivas Kandagatla } 3171*e894efefSSrinivas Kandagatla 3172e0dac41bSKuninori Morimoto int snd_soc_add_component(struct device *dev, 3173e0dac41bSKuninori Morimoto struct snd_soc_component *component, 3174cf9e829eSKuninori Morimoto const struct snd_soc_component_driver *component_driver, 3175d191bd8dSKuninori Morimoto struct snd_soc_dai_driver *dai_drv, 3176d191bd8dSKuninori Morimoto int num_dai) 3177d191bd8dSKuninori Morimoto { 3178bb13109dSLars-Peter Clausen int ret; 3179273d778eSKuninori Morimoto int i; 3180d191bd8dSKuninori Morimoto 3181cf9e829eSKuninori Morimoto ret = snd_soc_component_initialize(component, component_driver, dev); 3182bb13109dSLars-Peter Clausen if (ret) 3183bb13109dSLars-Peter Clausen goto err_free; 3184bb13109dSLars-Peter Clausen 3185273d778eSKuninori Morimoto if (component_driver->endianness) { 3186273d778eSKuninori Morimoto for (i = 0; i < num_dai; i++) { 3187273d778eSKuninori Morimoto convert_endianness_formats(&dai_drv[i].playback); 3188273d778eSKuninori Morimoto convert_endianness_formats(&dai_drv[i].capture); 3189273d778eSKuninori Morimoto } 3190273d778eSKuninori Morimoto } 3191273d778eSKuninori Morimoto 31920e7b25c6SKuninori Morimoto ret = snd_soc_register_dais(component, dai_drv, num_dai); 3193bb13109dSLars-Peter Clausen if (ret < 0) { 3194f42cf8d6SMasanari Iida dev_err(dev, "ASoC: Failed to register DAIs: %d\n", ret); 3195bb13109dSLars-Peter Clausen goto err_cleanup; 3196bb13109dSLars-Peter Clausen } 3197bb13109dSLars-Peter Clausen 3198cf9e829eSKuninori Morimoto snd_soc_component_add(component); 3199*e894efefSSrinivas Kandagatla snd_soc_try_rebind_card(); 3200bb13109dSLars-Peter Clausen 3201bb13109dSLars-Peter Clausen return 0; 3202bb13109dSLars-Peter Clausen 3203bb13109dSLars-Peter Clausen err_cleanup: 3204cf9e829eSKuninori Morimoto snd_soc_component_cleanup(component); 3205bb13109dSLars-Peter Clausen err_free: 3206bb13109dSLars-Peter Clausen return ret; 3207d191bd8dSKuninori Morimoto } 3208e0dac41bSKuninori Morimoto EXPORT_SYMBOL_GPL(snd_soc_add_component); 3209e0dac41bSKuninori Morimoto 3210e0dac41bSKuninori Morimoto int snd_soc_register_component(struct device *dev, 3211e0dac41bSKuninori Morimoto const struct snd_soc_component_driver *component_driver, 3212e0dac41bSKuninori Morimoto struct snd_soc_dai_driver *dai_drv, 3213e0dac41bSKuninori Morimoto int num_dai) 3214e0dac41bSKuninori Morimoto { 3215e0dac41bSKuninori Morimoto struct snd_soc_component *component; 3216e0dac41bSKuninori Morimoto 32177ecbd6a9SKuninori Morimoto component = devm_kzalloc(dev, sizeof(*component), GFP_KERNEL); 321808e61d03SKuninori Morimoto if (!component) 3219e0dac41bSKuninori Morimoto return -ENOMEM; 3220e0dac41bSKuninori Morimoto 3221e0dac41bSKuninori Morimoto return snd_soc_add_component(dev, component, component_driver, 3222e0dac41bSKuninori Morimoto dai_drv, num_dai); 3223e0dac41bSKuninori Morimoto } 3224d191bd8dSKuninori Morimoto EXPORT_SYMBOL_GPL(snd_soc_register_component); 3225d191bd8dSKuninori Morimoto 3226d191bd8dSKuninori Morimoto /** 32272eccea8cSKuninori Morimoto * snd_soc_unregister_component - Unregister all related component 32282eccea8cSKuninori Morimoto * from the ASoC core 3229d191bd8dSKuninori Morimoto * 3230628536eaSJonathan Corbet * @dev: The device to unregister 3231d191bd8dSKuninori Morimoto */ 32322eccea8cSKuninori Morimoto static int __snd_soc_unregister_component(struct device *dev) 3233d191bd8dSKuninori Morimoto { 3234cf9e829eSKuninori Morimoto struct snd_soc_component *component; 323521a03528SKuninori Morimoto int found = 0; 3236d191bd8dSKuninori Morimoto 323734e81ab4SLars-Peter Clausen mutex_lock(&client_mutex); 3238cf9e829eSKuninori Morimoto list_for_each_entry(component, &component_list, list) { 3239999f7f5aSKuninori Morimoto if (dev != component->dev) 324021a03528SKuninori Morimoto continue; 3241d191bd8dSKuninori Morimoto 3242cf9e829eSKuninori Morimoto snd_soc_tplg_component_remove(component, SND_SOC_TPLG_INDEX_ALL); 3243cf9e829eSKuninori Morimoto snd_soc_component_del_unlocked(component); 324421a03528SKuninori Morimoto found = 1; 324521a03528SKuninori Morimoto break; 3246d191bd8dSKuninori Morimoto } 3247d191bd8dSKuninori Morimoto mutex_unlock(&client_mutex); 3248d191bd8dSKuninori Morimoto 324921a03528SKuninori Morimoto if (found) { 3250cf9e829eSKuninori Morimoto snd_soc_component_cleanup(component); 3251d191bd8dSKuninori Morimoto } 32522eccea8cSKuninori Morimoto 32532eccea8cSKuninori Morimoto return found; 32542eccea8cSKuninori Morimoto } 32552eccea8cSKuninori Morimoto 32562eccea8cSKuninori Morimoto void snd_soc_unregister_component(struct device *dev) 32572eccea8cSKuninori Morimoto { 32582eccea8cSKuninori Morimoto while (__snd_soc_unregister_component(dev)); 3259d191bd8dSKuninori Morimoto } 3260d191bd8dSKuninori Morimoto EXPORT_SYMBOL_GPL(snd_soc_unregister_component); 3261d191bd8dSKuninori Morimoto 32627dd5d0d9SKuninori Morimoto struct snd_soc_component *snd_soc_lookup_component(struct device *dev, 32637dd5d0d9SKuninori Morimoto const char *driver_name) 32647dd5d0d9SKuninori Morimoto { 32657dd5d0d9SKuninori Morimoto struct snd_soc_component *component; 32667dd5d0d9SKuninori Morimoto struct snd_soc_component *ret; 32677dd5d0d9SKuninori Morimoto 32687dd5d0d9SKuninori Morimoto ret = NULL; 32697dd5d0d9SKuninori Morimoto mutex_lock(&client_mutex); 32707dd5d0d9SKuninori Morimoto list_for_each_entry(component, &component_list, list) { 32717dd5d0d9SKuninori Morimoto if (dev != component->dev) 32727dd5d0d9SKuninori Morimoto continue; 32737dd5d0d9SKuninori Morimoto 32747dd5d0d9SKuninori Morimoto if (driver_name && 32757dd5d0d9SKuninori Morimoto (driver_name != component->driver->name) && 32767dd5d0d9SKuninori Morimoto (strcmp(component->driver->name, driver_name) != 0)) 32777dd5d0d9SKuninori Morimoto continue; 32787dd5d0d9SKuninori Morimoto 32797dd5d0d9SKuninori Morimoto ret = component; 32807dd5d0d9SKuninori Morimoto break; 32817dd5d0d9SKuninori Morimoto } 32827dd5d0d9SKuninori Morimoto mutex_unlock(&client_mutex); 32837dd5d0d9SKuninori Morimoto 32847dd5d0d9SKuninori Morimoto return ret; 32857dd5d0d9SKuninori Morimoto } 32867dd5d0d9SKuninori Morimoto EXPORT_SYMBOL_GPL(snd_soc_lookup_component); 32877dd5d0d9SKuninori Morimoto 3288bec4fa05SStephen Warren /* Retrieve a card's name from device tree */ 3289b07609ceSKuninori Morimoto int snd_soc_of_parse_card_name(struct snd_soc_card *card, 3290bec4fa05SStephen Warren const char *propname) 3291bec4fa05SStephen Warren { 3292b07609ceSKuninori Morimoto struct device_node *np; 3293bec4fa05SStephen Warren int ret; 3294bec4fa05SStephen Warren 32957e07e7c0STushar Behera if (!card->dev) { 32967e07e7c0STushar Behera pr_err("card->dev is not set before calling %s\n", __func__); 32977e07e7c0STushar Behera return -EINVAL; 32987e07e7c0STushar Behera } 32997e07e7c0STushar Behera 33007e07e7c0STushar Behera np = card->dev->of_node; 33017e07e7c0STushar Behera 3302bec4fa05SStephen Warren ret = of_property_read_string_index(np, propname, 0, &card->name); 3303bec4fa05SStephen Warren /* 3304bec4fa05SStephen Warren * EINVAL means the property does not exist. This is fine providing 3305bec4fa05SStephen Warren * card->name was previously set, which is checked later in 3306bec4fa05SStephen Warren * snd_soc_register_card. 3307bec4fa05SStephen Warren */ 3308bec4fa05SStephen Warren if (ret < 0 && ret != -EINVAL) { 3309bec4fa05SStephen Warren dev_err(card->dev, 3310f110bfc7SLiam Girdwood "ASoC: Property '%s' could not be read: %d\n", 3311bec4fa05SStephen Warren propname, ret); 3312bec4fa05SStephen Warren return ret; 3313bec4fa05SStephen Warren } 3314bec4fa05SStephen Warren 3315bec4fa05SStephen Warren return 0; 3316bec4fa05SStephen Warren } 3317b07609ceSKuninori Morimoto EXPORT_SYMBOL_GPL(snd_soc_of_parse_card_name); 3318bec4fa05SStephen Warren 33199a6d4860SXiubo Li static const struct snd_soc_dapm_widget simple_widgets[] = { 33209a6d4860SXiubo Li SND_SOC_DAPM_MIC("Microphone", NULL), 33219a6d4860SXiubo Li SND_SOC_DAPM_LINE("Line", NULL), 33229a6d4860SXiubo Li SND_SOC_DAPM_HP("Headphone", NULL), 33239a6d4860SXiubo Li SND_SOC_DAPM_SPK("Speaker", NULL), 33249a6d4860SXiubo Li }; 33259a6d4860SXiubo Li 332621efde50SKuninori Morimoto int snd_soc_of_parse_audio_simple_widgets(struct snd_soc_card *card, 33279a6d4860SXiubo Li const char *propname) 33289a6d4860SXiubo Li { 332921efde50SKuninori Morimoto struct device_node *np = card->dev->of_node; 33309a6d4860SXiubo Li struct snd_soc_dapm_widget *widgets; 33319a6d4860SXiubo Li const char *template, *wname; 33329a6d4860SXiubo Li int i, j, num_widgets, ret; 33339a6d4860SXiubo Li 33349a6d4860SXiubo Li num_widgets = of_property_count_strings(np, propname); 33359a6d4860SXiubo Li if (num_widgets < 0) { 33369a6d4860SXiubo Li dev_err(card->dev, 33379a6d4860SXiubo Li "ASoC: Property '%s' does not exist\n", propname); 33389a6d4860SXiubo Li return -EINVAL; 33399a6d4860SXiubo Li } 33409a6d4860SXiubo Li if (num_widgets & 1) { 33419a6d4860SXiubo Li dev_err(card->dev, 33429a6d4860SXiubo Li "ASoC: Property '%s' length is not even\n", propname); 33439a6d4860SXiubo Li return -EINVAL; 33449a6d4860SXiubo Li } 33459a6d4860SXiubo Li 33469a6d4860SXiubo Li num_widgets /= 2; 33479a6d4860SXiubo Li if (!num_widgets) { 33489a6d4860SXiubo Li dev_err(card->dev, "ASoC: Property '%s's length is zero\n", 33499a6d4860SXiubo Li propname); 33509a6d4860SXiubo Li return -EINVAL; 33519a6d4860SXiubo Li } 33529a6d4860SXiubo Li 33539a6d4860SXiubo Li widgets = devm_kcalloc(card->dev, num_widgets, sizeof(*widgets), 33549a6d4860SXiubo Li GFP_KERNEL); 33559a6d4860SXiubo Li if (!widgets) { 33569a6d4860SXiubo Li dev_err(card->dev, 33579a6d4860SXiubo Li "ASoC: Could not allocate memory for widgets\n"); 33589a6d4860SXiubo Li return -ENOMEM; 33599a6d4860SXiubo Li } 33609a6d4860SXiubo Li 33619a6d4860SXiubo Li for (i = 0; i < num_widgets; i++) { 33629a6d4860SXiubo Li ret = of_property_read_string_index(np, propname, 33639a6d4860SXiubo Li 2 * i, &template); 33649a6d4860SXiubo Li if (ret) { 33659a6d4860SXiubo Li dev_err(card->dev, 33669a6d4860SXiubo Li "ASoC: Property '%s' index %d read error:%d\n", 33679a6d4860SXiubo Li propname, 2 * i, ret); 33689a6d4860SXiubo Li return -EINVAL; 33699a6d4860SXiubo Li } 33709a6d4860SXiubo Li 33719a6d4860SXiubo Li for (j = 0; j < ARRAY_SIZE(simple_widgets); j++) { 33729a6d4860SXiubo Li if (!strncmp(template, simple_widgets[j].name, 33739a6d4860SXiubo Li strlen(simple_widgets[j].name))) { 33749a6d4860SXiubo Li widgets[i] = simple_widgets[j]; 33759a6d4860SXiubo Li break; 33769a6d4860SXiubo Li } 33779a6d4860SXiubo Li } 33789a6d4860SXiubo Li 33799a6d4860SXiubo Li if (j >= ARRAY_SIZE(simple_widgets)) { 33809a6d4860SXiubo Li dev_err(card->dev, 33819a6d4860SXiubo Li "ASoC: DAPM widget '%s' is not supported\n", 33829a6d4860SXiubo Li template); 33839a6d4860SXiubo Li return -EINVAL; 33849a6d4860SXiubo Li } 33859a6d4860SXiubo Li 33869a6d4860SXiubo Li ret = of_property_read_string_index(np, propname, 33879a6d4860SXiubo Li (2 * i) + 1, 33889a6d4860SXiubo Li &wname); 33899a6d4860SXiubo Li if (ret) { 33909a6d4860SXiubo Li dev_err(card->dev, 33919a6d4860SXiubo Li "ASoC: Property '%s' index %d read error:%d\n", 33929a6d4860SXiubo Li propname, (2 * i) + 1, ret); 33939a6d4860SXiubo Li return -EINVAL; 33949a6d4860SXiubo Li } 33959a6d4860SXiubo Li 33969a6d4860SXiubo Li widgets[i].name = wname; 33979a6d4860SXiubo Li } 33989a6d4860SXiubo Li 3399f23e860eSNicolin Chen card->of_dapm_widgets = widgets; 3400f23e860eSNicolin Chen card->num_of_dapm_widgets = num_widgets; 34019a6d4860SXiubo Li 34029a6d4860SXiubo Li return 0; 34039a6d4860SXiubo Li } 340421efde50SKuninori Morimoto EXPORT_SYMBOL_GPL(snd_soc_of_parse_audio_simple_widgets); 34059a6d4860SXiubo Li 3406cbdfab3bSJerome Brunet int snd_soc_of_get_slot_mask(struct device_node *np, 34076131084aSJyri Sarha const char *prop_name, 34086131084aSJyri Sarha unsigned int *mask) 34096131084aSJyri Sarha { 34106131084aSJyri Sarha u32 val; 34116c84e591SJyri Sarha const __be32 *of_slot_mask = of_get_property(np, prop_name, &val); 34126131084aSJyri Sarha int i; 34136131084aSJyri Sarha 34146131084aSJyri Sarha if (!of_slot_mask) 34156131084aSJyri Sarha return 0; 34166131084aSJyri Sarha val /= sizeof(u32); 34176131084aSJyri Sarha for (i = 0; i < val; i++) 34186131084aSJyri Sarha if (be32_to_cpup(&of_slot_mask[i])) 34196131084aSJyri Sarha *mask |= (1 << i); 34206131084aSJyri Sarha 34216131084aSJyri Sarha return val; 34226131084aSJyri Sarha } 3423cbdfab3bSJerome Brunet EXPORT_SYMBOL_GPL(snd_soc_of_get_slot_mask); 34246131084aSJyri Sarha 342589c67857SXiubo Li int snd_soc_of_parse_tdm_slot(struct device_node *np, 34266131084aSJyri Sarha unsigned int *tx_mask, 34276131084aSJyri Sarha unsigned int *rx_mask, 342889c67857SXiubo Li unsigned int *slots, 342989c67857SXiubo Li unsigned int *slot_width) 343089c67857SXiubo Li { 343189c67857SXiubo Li u32 val; 343289c67857SXiubo Li int ret; 343389c67857SXiubo Li 34346131084aSJyri Sarha if (tx_mask) 34356131084aSJyri Sarha snd_soc_of_get_slot_mask(np, "dai-tdm-slot-tx-mask", tx_mask); 34366131084aSJyri Sarha if (rx_mask) 34376131084aSJyri Sarha snd_soc_of_get_slot_mask(np, "dai-tdm-slot-rx-mask", rx_mask); 34386131084aSJyri Sarha 343989c67857SXiubo Li if (of_property_read_bool(np, "dai-tdm-slot-num")) { 344089c67857SXiubo Li ret = of_property_read_u32(np, "dai-tdm-slot-num", &val); 344189c67857SXiubo Li if (ret) 344289c67857SXiubo Li return ret; 344389c67857SXiubo Li 344489c67857SXiubo Li if (slots) 344589c67857SXiubo Li *slots = val; 344689c67857SXiubo Li } 344789c67857SXiubo Li 344889c67857SXiubo Li if (of_property_read_bool(np, "dai-tdm-slot-width")) { 344989c67857SXiubo Li ret = of_property_read_u32(np, "dai-tdm-slot-width", &val); 345089c67857SXiubo Li if (ret) 345189c67857SXiubo Li return ret; 345289c67857SXiubo Li 345389c67857SXiubo Li if (slot_width) 345489c67857SXiubo Li *slot_width = val; 345589c67857SXiubo Li } 345689c67857SXiubo Li 345789c67857SXiubo Li return 0; 345889c67857SXiubo Li } 345989c67857SXiubo Li EXPORT_SYMBOL_GPL(snd_soc_of_parse_tdm_slot); 346089c67857SXiubo Li 3461440a3006SKuninori Morimoto void snd_soc_of_parse_audio_prefix(struct snd_soc_card *card, 34625e3cdaa2SKuninori Morimoto struct snd_soc_codec_conf *codec_conf, 34635e3cdaa2SKuninori Morimoto struct device_node *of_node, 34645e3cdaa2SKuninori Morimoto const char *propname) 34655e3cdaa2SKuninori Morimoto { 3466440a3006SKuninori Morimoto struct device_node *np = card->dev->of_node; 34675e3cdaa2SKuninori Morimoto const char *str; 34685e3cdaa2SKuninori Morimoto int ret; 34695e3cdaa2SKuninori Morimoto 34705e3cdaa2SKuninori Morimoto ret = of_property_read_string(np, propname, &str); 34715e3cdaa2SKuninori Morimoto if (ret < 0) { 34725e3cdaa2SKuninori Morimoto /* no prefix is not error */ 34735e3cdaa2SKuninori Morimoto return; 34745e3cdaa2SKuninori Morimoto } 34755e3cdaa2SKuninori Morimoto 34765e3cdaa2SKuninori Morimoto codec_conf->of_node = of_node; 34775e3cdaa2SKuninori Morimoto codec_conf->name_prefix = str; 34785e3cdaa2SKuninori Morimoto } 3479440a3006SKuninori Morimoto EXPORT_SYMBOL_GPL(snd_soc_of_parse_audio_prefix); 34805e3cdaa2SKuninori Morimoto 34812bc644afSKuninori Morimoto int snd_soc_of_parse_audio_routing(struct snd_soc_card *card, 3482a4a54dd5SStephen Warren const char *propname) 3483a4a54dd5SStephen Warren { 34842bc644afSKuninori Morimoto struct device_node *np = card->dev->of_node; 3485e3b1e6a1SMark Brown int num_routes; 3486a4a54dd5SStephen Warren struct snd_soc_dapm_route *routes; 3487a4a54dd5SStephen Warren int i, ret; 3488a4a54dd5SStephen Warren 3489a4a54dd5SStephen Warren num_routes = of_property_count_strings(np, propname); 3490c34ce320SRichard Zhao if (num_routes < 0 || num_routes & 1) { 349110e8aa9aSMichał Mirosław dev_err(card->dev, 349210e8aa9aSMichał Mirosław "ASoC: Property '%s' does not exist or its length is not even\n", 349310e8aa9aSMichał Mirosław propname); 3494a4a54dd5SStephen Warren return -EINVAL; 3495a4a54dd5SStephen Warren } 3496a4a54dd5SStephen Warren num_routes /= 2; 3497a4a54dd5SStephen Warren if (!num_routes) { 3498f110bfc7SLiam Girdwood dev_err(card->dev, "ASoC: Property '%s's length is zero\n", 3499a4a54dd5SStephen Warren propname); 3500a4a54dd5SStephen Warren return -EINVAL; 3501a4a54dd5SStephen Warren } 3502a4a54dd5SStephen Warren 3503a86854d0SKees Cook routes = devm_kcalloc(card->dev, num_routes, sizeof(*routes), 3504a4a54dd5SStephen Warren GFP_KERNEL); 3505a4a54dd5SStephen Warren if (!routes) { 3506a4a54dd5SStephen Warren dev_err(card->dev, 3507f110bfc7SLiam Girdwood "ASoC: Could not allocate DAPM route table\n"); 3508a4a54dd5SStephen Warren return -EINVAL; 3509a4a54dd5SStephen Warren } 3510a4a54dd5SStephen Warren 3511a4a54dd5SStephen Warren for (i = 0; i < num_routes; i++) { 3512a4a54dd5SStephen Warren ret = of_property_read_string_index(np, propname, 3513e3b1e6a1SMark Brown 2 * i, &routes[i].sink); 3514a4a54dd5SStephen Warren if (ret) { 3515c871bd0bSMark Brown dev_err(card->dev, 3516c871bd0bSMark Brown "ASoC: Property '%s' index %d could not be read: %d\n", 3517c871bd0bSMark Brown propname, 2 * i, ret); 3518a4a54dd5SStephen Warren return -EINVAL; 3519a4a54dd5SStephen Warren } 3520a4a54dd5SStephen Warren ret = of_property_read_string_index(np, propname, 3521e3b1e6a1SMark Brown (2 * i) + 1, &routes[i].source); 3522a4a54dd5SStephen Warren if (ret) { 3523a4a54dd5SStephen Warren dev_err(card->dev, 3524c871bd0bSMark Brown "ASoC: Property '%s' index %d could not be read: %d\n", 3525c871bd0bSMark Brown propname, (2 * i) + 1, ret); 3526a4a54dd5SStephen Warren return -EINVAL; 3527a4a54dd5SStephen Warren } 3528a4a54dd5SStephen Warren } 3529a4a54dd5SStephen Warren 3530f23e860eSNicolin Chen card->num_of_dapm_routes = num_routes; 3531f23e860eSNicolin Chen card->of_dapm_routes = routes; 3532a4a54dd5SStephen Warren 3533a4a54dd5SStephen Warren return 0; 3534a4a54dd5SStephen Warren } 35352bc644afSKuninori Morimoto EXPORT_SYMBOL_GPL(snd_soc_of_parse_audio_routing); 3536a4a54dd5SStephen Warren 3537a7930ed4SKuninori Morimoto unsigned int snd_soc_of_parse_daifmt(struct device_node *np, 3538389cb834SJyri Sarha const char *prefix, 3539389cb834SJyri Sarha struct device_node **bitclkmaster, 3540389cb834SJyri Sarha struct device_node **framemaster) 3541a7930ed4SKuninori Morimoto { 3542a7930ed4SKuninori Morimoto int ret, i; 3543a7930ed4SKuninori Morimoto char prop[128]; 3544a7930ed4SKuninori Morimoto unsigned int format = 0; 3545a7930ed4SKuninori Morimoto int bit, frame; 3546a7930ed4SKuninori Morimoto const char *str; 3547a7930ed4SKuninori Morimoto struct { 3548a7930ed4SKuninori Morimoto char *name; 3549a7930ed4SKuninori Morimoto unsigned int val; 3550a7930ed4SKuninori Morimoto } of_fmt_table[] = { 3551a7930ed4SKuninori Morimoto { "i2s", SND_SOC_DAIFMT_I2S }, 3552a7930ed4SKuninori Morimoto { "right_j", SND_SOC_DAIFMT_RIGHT_J }, 3553a7930ed4SKuninori Morimoto { "left_j", SND_SOC_DAIFMT_LEFT_J }, 3554a7930ed4SKuninori Morimoto { "dsp_a", SND_SOC_DAIFMT_DSP_A }, 3555a7930ed4SKuninori Morimoto { "dsp_b", SND_SOC_DAIFMT_DSP_B }, 3556a7930ed4SKuninori Morimoto { "ac97", SND_SOC_DAIFMT_AC97 }, 3557a7930ed4SKuninori Morimoto { "pdm", SND_SOC_DAIFMT_PDM}, 3558a7930ed4SKuninori Morimoto { "msb", SND_SOC_DAIFMT_MSB }, 3559a7930ed4SKuninori Morimoto { "lsb", SND_SOC_DAIFMT_LSB }, 3560a7930ed4SKuninori Morimoto }; 3561a7930ed4SKuninori Morimoto 3562a7930ed4SKuninori Morimoto if (!prefix) 3563a7930ed4SKuninori Morimoto prefix = ""; 3564a7930ed4SKuninori Morimoto 3565a7930ed4SKuninori Morimoto /* 35665711c979SKuninori Morimoto * check "dai-format = xxx" 35675711c979SKuninori Morimoto * or "[prefix]format = xxx" 3568a7930ed4SKuninori Morimoto * SND_SOC_DAIFMT_FORMAT_MASK area 3569a7930ed4SKuninori Morimoto */ 35705711c979SKuninori Morimoto ret = of_property_read_string(np, "dai-format", &str); 35715711c979SKuninori Morimoto if (ret < 0) { 3572a7930ed4SKuninori Morimoto snprintf(prop, sizeof(prop), "%sformat", prefix); 3573a7930ed4SKuninori Morimoto ret = of_property_read_string(np, prop, &str); 35745711c979SKuninori Morimoto } 3575a7930ed4SKuninori Morimoto if (ret == 0) { 3576a7930ed4SKuninori Morimoto for (i = 0; i < ARRAY_SIZE(of_fmt_table); i++) { 3577a7930ed4SKuninori Morimoto if (strcmp(str, of_fmt_table[i].name) == 0) { 3578a7930ed4SKuninori Morimoto format |= of_fmt_table[i].val; 3579a7930ed4SKuninori Morimoto break; 3580a7930ed4SKuninori Morimoto } 3581a7930ed4SKuninori Morimoto } 3582a7930ed4SKuninori Morimoto } 3583a7930ed4SKuninori Morimoto 3584a7930ed4SKuninori Morimoto /* 35858c2d6a9fSKuninori Morimoto * check "[prefix]continuous-clock" 3586a7930ed4SKuninori Morimoto * SND_SOC_DAIFMT_CLOCK_MASK area 3587a7930ed4SKuninori Morimoto */ 35888c2d6a9fSKuninori Morimoto snprintf(prop, sizeof(prop), "%scontinuous-clock", prefix); 358951930295SJulia Lawall if (of_property_read_bool(np, prop)) 35908c2d6a9fSKuninori Morimoto format |= SND_SOC_DAIFMT_CONT; 35918c2d6a9fSKuninori Morimoto else 35928c2d6a9fSKuninori Morimoto format |= SND_SOC_DAIFMT_GATED; 3593a7930ed4SKuninori Morimoto 3594a7930ed4SKuninori Morimoto /* 3595a7930ed4SKuninori Morimoto * check "[prefix]bitclock-inversion" 3596a7930ed4SKuninori Morimoto * check "[prefix]frame-inversion" 3597a7930ed4SKuninori Morimoto * SND_SOC_DAIFMT_INV_MASK area 3598a7930ed4SKuninori Morimoto */ 3599a7930ed4SKuninori Morimoto snprintf(prop, sizeof(prop), "%sbitclock-inversion", prefix); 3600a7930ed4SKuninori Morimoto bit = !!of_get_property(np, prop, NULL); 3601a7930ed4SKuninori Morimoto 3602a7930ed4SKuninori Morimoto snprintf(prop, sizeof(prop), "%sframe-inversion", prefix); 3603a7930ed4SKuninori Morimoto frame = !!of_get_property(np, prop, NULL); 3604a7930ed4SKuninori Morimoto 3605a7930ed4SKuninori Morimoto switch ((bit << 4) + frame) { 3606a7930ed4SKuninori Morimoto case 0x11: 3607a7930ed4SKuninori Morimoto format |= SND_SOC_DAIFMT_IB_IF; 3608a7930ed4SKuninori Morimoto break; 3609a7930ed4SKuninori Morimoto case 0x10: 3610a7930ed4SKuninori Morimoto format |= SND_SOC_DAIFMT_IB_NF; 3611a7930ed4SKuninori Morimoto break; 3612a7930ed4SKuninori Morimoto case 0x01: 3613a7930ed4SKuninori Morimoto format |= SND_SOC_DAIFMT_NB_IF; 3614a7930ed4SKuninori Morimoto break; 3615a7930ed4SKuninori Morimoto default: 3616a7930ed4SKuninori Morimoto /* SND_SOC_DAIFMT_NB_NF is default */ 3617a7930ed4SKuninori Morimoto break; 3618a7930ed4SKuninori Morimoto } 3619a7930ed4SKuninori Morimoto 3620a7930ed4SKuninori Morimoto /* 3621a7930ed4SKuninori Morimoto * check "[prefix]bitclock-master" 3622a7930ed4SKuninori Morimoto * check "[prefix]frame-master" 3623a7930ed4SKuninori Morimoto * SND_SOC_DAIFMT_MASTER_MASK area 3624a7930ed4SKuninori Morimoto */ 3625a7930ed4SKuninori Morimoto snprintf(prop, sizeof(prop), "%sbitclock-master", prefix); 3626a7930ed4SKuninori Morimoto bit = !!of_get_property(np, prop, NULL); 3627389cb834SJyri Sarha if (bit && bitclkmaster) 3628389cb834SJyri Sarha *bitclkmaster = of_parse_phandle(np, prop, 0); 3629a7930ed4SKuninori Morimoto 3630a7930ed4SKuninori Morimoto snprintf(prop, sizeof(prop), "%sframe-master", prefix); 3631a7930ed4SKuninori Morimoto frame = !!of_get_property(np, prop, NULL); 3632389cb834SJyri Sarha if (frame && framemaster) 3633389cb834SJyri Sarha *framemaster = of_parse_phandle(np, prop, 0); 3634a7930ed4SKuninori Morimoto 3635a7930ed4SKuninori Morimoto switch ((bit << 4) + frame) { 3636a7930ed4SKuninori Morimoto case 0x11: 3637a7930ed4SKuninori Morimoto format |= SND_SOC_DAIFMT_CBM_CFM; 3638a7930ed4SKuninori Morimoto break; 3639a7930ed4SKuninori Morimoto case 0x10: 3640a7930ed4SKuninori Morimoto format |= SND_SOC_DAIFMT_CBM_CFS; 3641a7930ed4SKuninori Morimoto break; 3642a7930ed4SKuninori Morimoto case 0x01: 3643a7930ed4SKuninori Morimoto format |= SND_SOC_DAIFMT_CBS_CFM; 3644a7930ed4SKuninori Morimoto break; 3645a7930ed4SKuninori Morimoto default: 3646a7930ed4SKuninori Morimoto format |= SND_SOC_DAIFMT_CBS_CFS; 3647a7930ed4SKuninori Morimoto break; 3648a7930ed4SKuninori Morimoto } 3649a7930ed4SKuninori Morimoto 3650a7930ed4SKuninori Morimoto return format; 3651a7930ed4SKuninori Morimoto } 3652a7930ed4SKuninori Morimoto EXPORT_SYMBOL_GPL(snd_soc_of_parse_daifmt); 3653a7930ed4SKuninori Morimoto 3654a180e8b9SKuninori Morimoto int snd_soc_get_dai_id(struct device_node *ep) 3655a180e8b9SKuninori Morimoto { 3656a180e8b9SKuninori Morimoto struct snd_soc_component *pos; 3657a180e8b9SKuninori Morimoto struct device_node *node; 3658a180e8b9SKuninori Morimoto int ret; 3659a180e8b9SKuninori Morimoto 3660a180e8b9SKuninori Morimoto node = of_graph_get_port_parent(ep); 3661a180e8b9SKuninori Morimoto 3662a180e8b9SKuninori Morimoto /* 3663a180e8b9SKuninori Morimoto * For example HDMI case, HDMI has video/sound port, 3664a180e8b9SKuninori Morimoto * but ALSA SoC needs sound port number only. 3665a180e8b9SKuninori Morimoto * Thus counting HDMI DT port/endpoint doesn't work. 3666a180e8b9SKuninori Morimoto * Then, it should have .of_xlate_dai_id 3667a180e8b9SKuninori Morimoto */ 3668a180e8b9SKuninori Morimoto ret = -ENOTSUPP; 3669a180e8b9SKuninori Morimoto mutex_lock(&client_mutex); 3670a180e8b9SKuninori Morimoto list_for_each_entry(pos, &component_list, list) { 3671a180e8b9SKuninori Morimoto struct device_node *component_of_node = pos->dev->of_node; 3672a180e8b9SKuninori Morimoto 3673a180e8b9SKuninori Morimoto if (!component_of_node && pos->dev->parent) 3674a180e8b9SKuninori Morimoto component_of_node = pos->dev->parent->of_node; 3675a180e8b9SKuninori Morimoto 3676a180e8b9SKuninori Morimoto if (component_of_node != node) 3677a180e8b9SKuninori Morimoto continue; 3678a180e8b9SKuninori Morimoto 3679a180e8b9SKuninori Morimoto if (pos->driver->of_xlate_dai_id) 3680a180e8b9SKuninori Morimoto ret = pos->driver->of_xlate_dai_id(pos, ep); 3681a180e8b9SKuninori Morimoto 3682a180e8b9SKuninori Morimoto break; 3683a180e8b9SKuninori Morimoto } 3684a180e8b9SKuninori Morimoto mutex_unlock(&client_mutex); 3685a180e8b9SKuninori Morimoto 3686c0a480d1STony Lindgren of_node_put(node); 3687c0a480d1STony Lindgren 3688a180e8b9SKuninori Morimoto return ret; 3689a180e8b9SKuninori Morimoto } 3690a180e8b9SKuninori Morimoto EXPORT_SYMBOL_GPL(snd_soc_get_dai_id); 3691a180e8b9SKuninori Morimoto 36921ad8ec53SKuninori Morimoto int snd_soc_get_dai_name(struct of_phandle_args *args, 3693cb470087SKuninori Morimoto const char **dai_name) 3694cb470087SKuninori Morimoto { 3695cb470087SKuninori Morimoto struct snd_soc_component *pos; 36963e0aa8d8SJyri Sarha struct device_node *component_of_node; 369793b0f3eeSJean-Francois Moine int ret = -EPROBE_DEFER; 3698cb470087SKuninori Morimoto 3699cb470087SKuninori Morimoto mutex_lock(&client_mutex); 3700cb470087SKuninori Morimoto list_for_each_entry(pos, &component_list, list) { 37013e0aa8d8SJyri Sarha component_of_node = pos->dev->of_node; 37023e0aa8d8SJyri Sarha if (!component_of_node && pos->dev->parent) 37033e0aa8d8SJyri Sarha component_of_node = pos->dev->parent->of_node; 37043e0aa8d8SJyri Sarha 37053e0aa8d8SJyri Sarha if (component_of_node != args->np) 3706cb470087SKuninori Morimoto continue; 3707cb470087SKuninori Morimoto 37086833c452SKuninori Morimoto if (pos->driver->of_xlate_dai_name) { 370993b0f3eeSJean-Francois Moine ret = pos->driver->of_xlate_dai_name(pos, 371093b0f3eeSJean-Francois Moine args, 371193b0f3eeSJean-Francois Moine dai_name); 37126833c452SKuninori Morimoto } else { 371358bf4179SKuninori Morimoto struct snd_soc_dai *dai; 37146833c452SKuninori Morimoto int id = -1; 37156833c452SKuninori Morimoto 371693b0f3eeSJean-Francois Moine switch (args->args_count) { 37176833c452SKuninori Morimoto case 0: 37186833c452SKuninori Morimoto id = 0; /* same as dai_drv[0] */ 37196833c452SKuninori Morimoto break; 37206833c452SKuninori Morimoto case 1: 372193b0f3eeSJean-Francois Moine id = args->args[0]; 37226833c452SKuninori Morimoto break; 37236833c452SKuninori Morimoto default: 37246833c452SKuninori Morimoto /* not supported */ 3725cb470087SKuninori Morimoto break; 3726cb470087SKuninori Morimoto } 3727cb470087SKuninori Morimoto 37286833c452SKuninori Morimoto if (id < 0 || id >= pos->num_dai) { 37296833c452SKuninori Morimoto ret = -EINVAL; 37303dcba280SNicolin Chen continue; 37316833c452SKuninori Morimoto } 3732e41975edSXiubo Li 3733e41975edSXiubo Li ret = 0; 3734e41975edSXiubo Li 373558bf4179SKuninori Morimoto /* find target DAI */ 373658bf4179SKuninori Morimoto list_for_each_entry(dai, &pos->dai_list, list) { 373758bf4179SKuninori Morimoto if (id == 0) 373858bf4179SKuninori Morimoto break; 373958bf4179SKuninori Morimoto id--; 374058bf4179SKuninori Morimoto } 374158bf4179SKuninori Morimoto 374258bf4179SKuninori Morimoto *dai_name = dai->driver->name; 3743e41975edSXiubo Li if (!*dai_name) 3744e41975edSXiubo Li *dai_name = pos->name; 37456833c452SKuninori Morimoto } 37466833c452SKuninori Morimoto 3747cb470087SKuninori Morimoto break; 3748cb470087SKuninori Morimoto } 3749cb470087SKuninori Morimoto mutex_unlock(&client_mutex); 375093b0f3eeSJean-Francois Moine return ret; 375193b0f3eeSJean-Francois Moine } 37521ad8ec53SKuninori Morimoto EXPORT_SYMBOL_GPL(snd_soc_get_dai_name); 375393b0f3eeSJean-Francois Moine 375493b0f3eeSJean-Francois Moine int snd_soc_of_get_dai_name(struct device_node *of_node, 375593b0f3eeSJean-Francois Moine const char **dai_name) 375693b0f3eeSJean-Francois Moine { 375793b0f3eeSJean-Francois Moine struct of_phandle_args args; 375893b0f3eeSJean-Francois Moine int ret; 375993b0f3eeSJean-Francois Moine 376093b0f3eeSJean-Francois Moine ret = of_parse_phandle_with_args(of_node, "sound-dai", 376193b0f3eeSJean-Francois Moine "#sound-dai-cells", 0, &args); 376293b0f3eeSJean-Francois Moine if (ret) 376393b0f3eeSJean-Francois Moine return ret; 376493b0f3eeSJean-Francois Moine 376593b0f3eeSJean-Francois Moine ret = snd_soc_get_dai_name(&args, dai_name); 3766cb470087SKuninori Morimoto 3767cb470087SKuninori Morimoto of_node_put(args.np); 3768cb470087SKuninori Morimoto 3769cb470087SKuninori Morimoto return ret; 3770cb470087SKuninori Morimoto } 3771cb470087SKuninori Morimoto EXPORT_SYMBOL_GPL(snd_soc_of_get_dai_name); 3772cb470087SKuninori Morimoto 377393b0f3eeSJean-Francois Moine /* 377494685763SSylwester Nawrocki * snd_soc_of_put_dai_link_codecs - Dereference device nodes in the codecs array 377594685763SSylwester Nawrocki * @dai_link: DAI link 377694685763SSylwester Nawrocki * 377794685763SSylwester Nawrocki * Dereference device nodes acquired by snd_soc_of_get_dai_link_codecs(). 377894685763SSylwester Nawrocki */ 377994685763SSylwester Nawrocki void snd_soc_of_put_dai_link_codecs(struct snd_soc_dai_link *dai_link) 378094685763SSylwester Nawrocki { 37813db769f1SKuninori Morimoto struct snd_soc_dai_link_component *component; 378294685763SSylwester Nawrocki int index; 378394685763SSylwester Nawrocki 37843db769f1SKuninori Morimoto for_each_link_codecs(dai_link, index, component) { 378594685763SSylwester Nawrocki if (!component->of_node) 378694685763SSylwester Nawrocki break; 378794685763SSylwester Nawrocki of_node_put(component->of_node); 378894685763SSylwester Nawrocki component->of_node = NULL; 378994685763SSylwester Nawrocki } 379094685763SSylwester Nawrocki } 379194685763SSylwester Nawrocki EXPORT_SYMBOL_GPL(snd_soc_of_put_dai_link_codecs); 379294685763SSylwester Nawrocki 379394685763SSylwester Nawrocki /* 379493b0f3eeSJean-Francois Moine * snd_soc_of_get_dai_link_codecs - Parse a list of CODECs in the devicetree 379593b0f3eeSJean-Francois Moine * @dev: Card device 379693b0f3eeSJean-Francois Moine * @of_node: Device node 379793b0f3eeSJean-Francois Moine * @dai_link: DAI link 379893b0f3eeSJean-Francois Moine * 379993b0f3eeSJean-Francois Moine * Builds an array of CODEC DAI components from the DAI link property 380093b0f3eeSJean-Francois Moine * 'sound-dai'. 380193b0f3eeSJean-Francois Moine * The array is set in the DAI link and the number of DAIs is set accordingly. 380294685763SSylwester Nawrocki * The device nodes in the array (of_node) must be dereferenced by calling 380394685763SSylwester Nawrocki * snd_soc_of_put_dai_link_codecs() on @dai_link. 380493b0f3eeSJean-Francois Moine * 380593b0f3eeSJean-Francois Moine * Returns 0 for success 380693b0f3eeSJean-Francois Moine */ 380793b0f3eeSJean-Francois Moine int snd_soc_of_get_dai_link_codecs(struct device *dev, 380893b0f3eeSJean-Francois Moine struct device_node *of_node, 380993b0f3eeSJean-Francois Moine struct snd_soc_dai_link *dai_link) 381093b0f3eeSJean-Francois Moine { 381193b0f3eeSJean-Francois Moine struct of_phandle_args args; 381293b0f3eeSJean-Francois Moine struct snd_soc_dai_link_component *component; 381393b0f3eeSJean-Francois Moine char *name; 381493b0f3eeSJean-Francois Moine int index, num_codecs, ret; 381593b0f3eeSJean-Francois Moine 381693b0f3eeSJean-Francois Moine /* Count the number of CODECs */ 381793b0f3eeSJean-Francois Moine name = "sound-dai"; 381893b0f3eeSJean-Francois Moine num_codecs = of_count_phandle_with_args(of_node, name, 381993b0f3eeSJean-Francois Moine "#sound-dai-cells"); 382093b0f3eeSJean-Francois Moine if (num_codecs <= 0) { 382193b0f3eeSJean-Francois Moine if (num_codecs == -ENOENT) 382293b0f3eeSJean-Francois Moine dev_err(dev, "No 'sound-dai' property\n"); 382393b0f3eeSJean-Francois Moine else 382493b0f3eeSJean-Francois Moine dev_err(dev, "Bad phandle in 'sound-dai'\n"); 382593b0f3eeSJean-Francois Moine return num_codecs; 382693b0f3eeSJean-Francois Moine } 3827a86854d0SKees Cook component = devm_kcalloc(dev, 3828a86854d0SKees Cook num_codecs, sizeof(*component), 382993b0f3eeSJean-Francois Moine GFP_KERNEL); 383093b0f3eeSJean-Francois Moine if (!component) 383193b0f3eeSJean-Francois Moine return -ENOMEM; 383293b0f3eeSJean-Francois Moine dai_link->codecs = component; 383393b0f3eeSJean-Francois Moine dai_link->num_codecs = num_codecs; 383493b0f3eeSJean-Francois Moine 383593b0f3eeSJean-Francois Moine /* Parse the list */ 38363db769f1SKuninori Morimoto for_each_link_codecs(dai_link, index, component) { 383793b0f3eeSJean-Francois Moine ret = of_parse_phandle_with_args(of_node, name, 383893b0f3eeSJean-Francois Moine "#sound-dai-cells", 383993b0f3eeSJean-Francois Moine index, &args); 384093b0f3eeSJean-Francois Moine if (ret) 384193b0f3eeSJean-Francois Moine goto err; 384293b0f3eeSJean-Francois Moine component->of_node = args.np; 384393b0f3eeSJean-Francois Moine ret = snd_soc_get_dai_name(&args, &component->dai_name); 384493b0f3eeSJean-Francois Moine if (ret < 0) 384593b0f3eeSJean-Francois Moine goto err; 384693b0f3eeSJean-Francois Moine } 384793b0f3eeSJean-Francois Moine return 0; 384893b0f3eeSJean-Francois Moine err: 384994685763SSylwester Nawrocki snd_soc_of_put_dai_link_codecs(dai_link); 385093b0f3eeSJean-Francois Moine dai_link->codecs = NULL; 385193b0f3eeSJean-Francois Moine dai_link->num_codecs = 0; 385293b0f3eeSJean-Francois Moine return ret; 385393b0f3eeSJean-Francois Moine } 385493b0f3eeSJean-Francois Moine EXPORT_SYMBOL_GPL(snd_soc_of_get_dai_link_codecs); 385593b0f3eeSJean-Francois Moine 3856c9b3a40fSTakashi Iwai static int __init snd_soc_init(void) 3857db2a4165SFrank Mandarino { 38586553bf06SLars-Peter Clausen snd_soc_debugfs_init(); 3859fb257897SMark Brown snd_soc_util_init(); 3860fb257897SMark Brown 3861db2a4165SFrank Mandarino return platform_driver_register(&soc_driver); 3862db2a4165SFrank Mandarino } 38634abe8e16SMark Brown module_init(snd_soc_init); 3864db2a4165SFrank Mandarino 38657d8c16a6SMark Brown static void __exit snd_soc_exit(void) 3866db2a4165SFrank Mandarino { 3867fb257897SMark Brown snd_soc_util_exit(); 38686553bf06SLars-Peter Clausen snd_soc_debugfs_exit(); 3869fb257897SMark Brown 3870db2a4165SFrank Mandarino platform_driver_unregister(&soc_driver); 3871db2a4165SFrank Mandarino } 3872db2a4165SFrank Mandarino module_exit(snd_soc_exit); 3873db2a4165SFrank Mandarino 3874db2a4165SFrank Mandarino /* Module information */ 3875d331124dSLiam Girdwood MODULE_AUTHOR("Liam Girdwood, lrg@slimlogic.co.uk"); 3876db2a4165SFrank Mandarino MODULE_DESCRIPTION("ALSA SoC Core"); 3877db2a4165SFrank Mandarino MODULE_LICENSE("GPL"); 3878db2a4165SFrank Mandarino MODULE_ALIAS("platform:soc-audio"); 3879