1db2a4165SFrank Mandarino /* 2db2a4165SFrank Mandarino * soc-core.c -- ALSA SoC Audio Layer 3db2a4165SFrank Mandarino * 4db2a4165SFrank Mandarino * Copyright 2005 Wolfson Microelectronics PLC. 50664d888SLiam Girdwood * Copyright 2005 Openedhand Ltd. 6f0fba2adSLiam Girdwood * Copyright (C) 2010 Slimlogic Ltd. 7f0fba2adSLiam Girdwood * Copyright (C) 2010 Texas Instruments Inc. 80664d888SLiam Girdwood * 9d331124dSLiam Girdwood * Author: Liam Girdwood <lrg@slimlogic.co.uk> 100664d888SLiam Girdwood * with code, comments and ideas from :- 110664d888SLiam Girdwood * Richard Purdie <richard@openedhand.com> 12db2a4165SFrank Mandarino * 13db2a4165SFrank Mandarino * This program is free software; you can redistribute it and/or modify it 14db2a4165SFrank Mandarino * under the terms of the GNU General Public License as published by the 15db2a4165SFrank Mandarino * Free Software Foundation; either version 2 of the License, or (at your 16db2a4165SFrank Mandarino * option) any later version. 17db2a4165SFrank Mandarino * 18db2a4165SFrank Mandarino * TODO: 19db2a4165SFrank Mandarino * o Add hw rules to enforce rates, etc. 20db2a4165SFrank Mandarino * o More testing with other codecs/machines. 21db2a4165SFrank Mandarino * o Add more codecs and platforms to ensure good API coverage. 22db2a4165SFrank Mandarino * o Support TDM on PCM and I2S 23db2a4165SFrank Mandarino */ 24db2a4165SFrank Mandarino 25db2a4165SFrank Mandarino #include <linux/module.h> 26db2a4165SFrank Mandarino #include <linux/moduleparam.h> 27db2a4165SFrank Mandarino #include <linux/init.h> 28db2a4165SFrank Mandarino #include <linux/delay.h> 29db2a4165SFrank Mandarino #include <linux/pm.h> 30db2a4165SFrank Mandarino #include <linux/bitops.h> 3112ef193dSTroy Kisky #include <linux/debugfs.h> 32db2a4165SFrank Mandarino #include <linux/platform_device.h> 33741a509fSMarkus Pargmann #include <linux/pinctrl/consumer.h> 34f0e8ed85SMark Brown #include <linux/ctype.h> 355a0e3ad6STejun Heo #include <linux/slab.h> 36bec4fa05SStephen Warren #include <linux/of.h> 37db2a4165SFrank Mandarino #include <sound/core.h> 383028eb8cSMark Brown #include <sound/jack.h> 39db2a4165SFrank Mandarino #include <sound/pcm.h> 40db2a4165SFrank Mandarino #include <sound/pcm_params.h> 41db2a4165SFrank Mandarino #include <sound/soc.h> 4201d7584cSLiam Girdwood #include <sound/soc-dpcm.h> 43db2a4165SFrank Mandarino #include <sound/initval.h> 44db2a4165SFrank Mandarino 45a8b1d34fSMark Brown #define CREATE_TRACE_POINTS 46a8b1d34fSMark Brown #include <trace/events/asoc.h> 47a8b1d34fSMark Brown 48f0fba2adSLiam Girdwood #define NAME_SIZE 32 49f0fba2adSLiam Girdwood 50384c89e2SMark Brown #ifdef CONFIG_DEBUG_FS 518a9dab1aSMark Brown struct dentry *snd_soc_debugfs_root; 528a9dab1aSMark Brown EXPORT_SYMBOL_GPL(snd_soc_debugfs_root); 53384c89e2SMark Brown #endif 54384c89e2SMark Brown 55c5af3a2eSMark Brown static DEFINE_MUTEX(client_mutex); 5612a48a8cSMark Brown static LIST_HEAD(platform_list); 570d0cf00aSMark Brown static LIST_HEAD(codec_list); 58030e79f6SKuninori Morimoto static LIST_HEAD(component_list); 59c5af3a2eSMark Brown 60db2a4165SFrank Mandarino /* 61db2a4165SFrank Mandarino * This is a timeout to do a DAPM powerdown after a stream is closed(). 62db2a4165SFrank Mandarino * It can be used to eliminate pops between different playback streams, e.g. 63db2a4165SFrank Mandarino * between two audio tracks. 64db2a4165SFrank Mandarino */ 65db2a4165SFrank Mandarino static int pmdown_time = 5000; 66db2a4165SFrank Mandarino module_param(pmdown_time, int, 0); 67db2a4165SFrank Mandarino MODULE_PARM_DESC(pmdown_time, "DAPM stream powerdown time (msecs)"); 68db2a4165SFrank Mandarino 692bc9a81eSDimitris Papastamos /* returns the minimum number of bytes needed to represent 702bc9a81eSDimitris Papastamos * a particular given value */ 712bc9a81eSDimitris Papastamos static int min_bytes_needed(unsigned long val) 722bc9a81eSDimitris Papastamos { 732bc9a81eSDimitris Papastamos int c = 0; 742bc9a81eSDimitris Papastamos int i; 752bc9a81eSDimitris Papastamos 762bc9a81eSDimitris Papastamos for (i = (sizeof val * 8) - 1; i >= 0; --i, ++c) 772bc9a81eSDimitris Papastamos if (val & (1UL << i)) 782bc9a81eSDimitris Papastamos break; 792bc9a81eSDimitris Papastamos c = (sizeof val * 8) - c; 802bc9a81eSDimitris Papastamos if (!c || (c % 8)) 812bc9a81eSDimitris Papastamos c = (c + 8) / 8; 822bc9a81eSDimitris Papastamos else 832bc9a81eSDimitris Papastamos c /= 8; 842bc9a81eSDimitris Papastamos return c; 852bc9a81eSDimitris Papastamos } 862bc9a81eSDimitris Papastamos 8713fd179fSDimitris Papastamos /* fill buf which is 'len' bytes with a formatted 8813fd179fSDimitris Papastamos * string of the form 'reg: value\n' */ 8913fd179fSDimitris Papastamos static int format_register_str(struct snd_soc_codec *codec, 9013fd179fSDimitris Papastamos unsigned int reg, char *buf, size_t len) 912624d5faSMark Brown { 9200b317a4SStephen Warren int wordsize = min_bytes_needed(codec->driver->reg_cache_size) * 2; 9300b317a4SStephen Warren int regsize = codec->driver->reg_word_size * 2; 9413fd179fSDimitris Papastamos int ret; 9513fd179fSDimitris Papastamos char tmpbuf[len + 1]; 9613fd179fSDimitris Papastamos char regbuf[regsize + 1]; 9713fd179fSDimitris Papastamos 9813fd179fSDimitris Papastamos /* since tmpbuf is allocated on the stack, warn the callers if they 9913fd179fSDimitris Papastamos * try to abuse this function */ 10013fd179fSDimitris Papastamos WARN_ON(len > 63); 10113fd179fSDimitris Papastamos 10213fd179fSDimitris Papastamos /* +2 for ': ' and + 1 for '\n' */ 10313fd179fSDimitris Papastamos if (wordsize + regsize + 2 + 1 != len) 10413fd179fSDimitris Papastamos return -EINVAL; 10513fd179fSDimitris Papastamos 10613fd179fSDimitris Papastamos ret = snd_soc_read(codec, reg); 10713fd179fSDimitris Papastamos if (ret < 0) { 10813fd179fSDimitris Papastamos memset(regbuf, 'X', regsize); 10913fd179fSDimitris Papastamos regbuf[regsize] = '\0'; 11013fd179fSDimitris Papastamos } else { 11113fd179fSDimitris Papastamos snprintf(regbuf, regsize + 1, "%.*x", regsize, ret); 11213fd179fSDimitris Papastamos } 11313fd179fSDimitris Papastamos 11413fd179fSDimitris Papastamos /* prepare the buffer */ 11513fd179fSDimitris Papastamos snprintf(tmpbuf, len + 1, "%.*x: %s\n", wordsize, reg, regbuf); 11613fd179fSDimitris Papastamos /* copy it back to the caller without the '\0' */ 11713fd179fSDimitris Papastamos memcpy(buf, tmpbuf, len); 11813fd179fSDimitris Papastamos 11913fd179fSDimitris Papastamos return 0; 12013fd179fSDimitris Papastamos } 12113fd179fSDimitris Papastamos 12213fd179fSDimitris Papastamos /* codec register dump */ 12313fd179fSDimitris Papastamos static ssize_t soc_codec_reg_show(struct snd_soc_codec *codec, char *buf, 12413fd179fSDimitris Papastamos size_t count, loff_t pos) 12513fd179fSDimitris Papastamos { 12613fd179fSDimitris Papastamos int i, step = 1; 1272bc9a81eSDimitris Papastamos int wordsize, regsize; 12813fd179fSDimitris Papastamos int len; 12913fd179fSDimitris Papastamos size_t total = 0; 13013fd179fSDimitris Papastamos loff_t p = 0; 1312bc9a81eSDimitris Papastamos 13200b317a4SStephen Warren wordsize = min_bytes_needed(codec->driver->reg_cache_size) * 2; 13300b317a4SStephen Warren regsize = codec->driver->reg_word_size * 2; 1342624d5faSMark Brown 13513fd179fSDimitris Papastamos len = wordsize + regsize + 2 + 1; 13613fd179fSDimitris Papastamos 137f0fba2adSLiam Girdwood if (!codec->driver->reg_cache_size) 1382624d5faSMark Brown return 0; 1392624d5faSMark Brown 140f0fba2adSLiam Girdwood if (codec->driver->reg_cache_step) 141f0fba2adSLiam Girdwood step = codec->driver->reg_cache_step; 1422624d5faSMark Brown 143f0fba2adSLiam Girdwood for (i = 0; i < codec->driver->reg_cache_size; i += step) { 14413fd179fSDimitris Papastamos /* only support larger than PAGE_SIZE bytes debugfs 14513fd179fSDimitris Papastamos * entries for the default case */ 14613fd179fSDimitris Papastamos if (p >= pos) { 14713fd179fSDimitris Papastamos if (total + len >= count - 1) 1482624d5faSMark Brown break; 14913fd179fSDimitris Papastamos format_register_str(codec, i, buf + total, len); 15013fd179fSDimitris Papastamos total += len; 15113fd179fSDimitris Papastamos } 15213fd179fSDimitris Papastamos p += len; 15313fd179fSDimitris Papastamos } 1542624d5faSMark Brown 15513fd179fSDimitris Papastamos total = min(total, count - 1); 1562624d5faSMark Brown 15713fd179fSDimitris Papastamos return total; 1582624d5faSMark Brown } 15913fd179fSDimitris Papastamos 1602624d5faSMark Brown static ssize_t codec_reg_show(struct device *dev, 1612624d5faSMark Brown struct device_attribute *attr, char *buf) 1622624d5faSMark Brown { 16336ae1a96SMark Brown struct snd_soc_pcm_runtime *rtd = dev_get_drvdata(dev); 164f0fba2adSLiam Girdwood 16513fd179fSDimitris Papastamos return soc_codec_reg_show(rtd->codec, buf, PAGE_SIZE, 0); 1662624d5faSMark Brown } 1672624d5faSMark Brown 1682624d5faSMark Brown static DEVICE_ATTR(codec_reg, 0444, codec_reg_show, NULL); 1692624d5faSMark Brown 170dbe21408SMark Brown static ssize_t pmdown_time_show(struct device *dev, 171dbe21408SMark Brown struct device_attribute *attr, char *buf) 172dbe21408SMark Brown { 17336ae1a96SMark Brown struct snd_soc_pcm_runtime *rtd = dev_get_drvdata(dev); 174dbe21408SMark Brown 175f0fba2adSLiam Girdwood return sprintf(buf, "%ld\n", rtd->pmdown_time); 176dbe21408SMark Brown } 177dbe21408SMark Brown 178dbe21408SMark Brown static ssize_t pmdown_time_set(struct device *dev, 179dbe21408SMark Brown struct device_attribute *attr, 180dbe21408SMark Brown const char *buf, size_t count) 181dbe21408SMark Brown { 18236ae1a96SMark Brown struct snd_soc_pcm_runtime *rtd = dev_get_drvdata(dev); 183c593b520SMark Brown int ret; 184dbe21408SMark Brown 185b785a492SJingoo Han ret = kstrtol(buf, 10, &rtd->pmdown_time); 186c593b520SMark Brown if (ret) 187c593b520SMark Brown return ret; 188dbe21408SMark Brown 189dbe21408SMark Brown return count; 190dbe21408SMark Brown } 191dbe21408SMark Brown 192dbe21408SMark Brown static DEVICE_ATTR(pmdown_time, 0644, pmdown_time_show, pmdown_time_set); 193dbe21408SMark Brown 194d29697dcSTakashi Iwai static struct attribute *soc_dev_attrs[] = { 195d29697dcSTakashi Iwai &dev_attr_codec_reg.attr, 196d29697dcSTakashi Iwai &dev_attr_pmdown_time.attr, 197d29697dcSTakashi Iwai NULL 198d29697dcSTakashi Iwai }; 199d29697dcSTakashi Iwai 200d29697dcSTakashi Iwai static umode_t soc_dev_attr_is_visible(struct kobject *kobj, 201d29697dcSTakashi Iwai struct attribute *attr, int idx) 202d29697dcSTakashi Iwai { 203d29697dcSTakashi Iwai struct device *dev = kobj_to_dev(kobj); 204d29697dcSTakashi Iwai struct snd_soc_pcm_runtime *rtd = dev_get_drvdata(dev); 205d29697dcSTakashi Iwai 206d29697dcSTakashi Iwai if (attr == &dev_attr_pmdown_time.attr) 207d29697dcSTakashi Iwai return attr->mode; /* always visible */ 208d29697dcSTakashi Iwai return rtd->codec ? attr->mode : 0; /* enabled only with codec */ 209d29697dcSTakashi Iwai } 210d29697dcSTakashi Iwai 211d29697dcSTakashi Iwai static const struct attribute_group soc_dapm_dev_group = { 212d29697dcSTakashi Iwai .attrs = soc_dapm_dev_attrs, 213d29697dcSTakashi Iwai .is_visible = soc_dev_attr_is_visible, 214d29697dcSTakashi Iwai }; 215d29697dcSTakashi Iwai 216d29697dcSTakashi Iwai static const struct attribute_group soc_dev_roup = { 217d29697dcSTakashi Iwai .attrs = soc_dev_attrs, 218d29697dcSTakashi Iwai .is_visible = soc_dev_attr_is_visible, 219d29697dcSTakashi Iwai }; 220d29697dcSTakashi Iwai 221d29697dcSTakashi Iwai static const struct attribute_group *soc_dev_attr_groups[] = { 222d29697dcSTakashi Iwai &soc_dapm_dev_group, 223d29697dcSTakashi Iwai &soc_dev_roup, 224d29697dcSTakashi Iwai NULL 225d29697dcSTakashi Iwai }; 226d29697dcSTakashi Iwai 2272624d5faSMark Brown #ifdef CONFIG_DEBUG_FS 2282624d5faSMark Brown static ssize_t codec_reg_read_file(struct file *file, char __user *user_buf, 2292624d5faSMark Brown size_t count, loff_t *ppos) 2302624d5faSMark Brown { 2312624d5faSMark Brown ssize_t ret; 2322624d5faSMark Brown struct snd_soc_codec *codec = file->private_data; 23313fd179fSDimitris Papastamos char *buf; 23413fd179fSDimitris Papastamos 23513fd179fSDimitris Papastamos if (*ppos < 0 || !count) 23613fd179fSDimitris Papastamos return -EINVAL; 23713fd179fSDimitris Papastamos 23813fd179fSDimitris Papastamos buf = kmalloc(count, GFP_KERNEL); 2392624d5faSMark Brown if (!buf) 2402624d5faSMark Brown return -ENOMEM; 24113fd179fSDimitris Papastamos 24213fd179fSDimitris Papastamos ret = soc_codec_reg_show(codec, buf, count, *ppos); 24313fd179fSDimitris Papastamos if (ret >= 0) { 24413fd179fSDimitris Papastamos if (copy_to_user(user_buf, buf, ret)) { 24513fd179fSDimitris Papastamos kfree(buf); 24613fd179fSDimitris Papastamos return -EFAULT; 24713fd179fSDimitris Papastamos } 24813fd179fSDimitris Papastamos *ppos += ret; 24913fd179fSDimitris Papastamos } 25013fd179fSDimitris Papastamos 2512624d5faSMark Brown kfree(buf); 2522624d5faSMark Brown return ret; 2532624d5faSMark Brown } 2542624d5faSMark Brown 2552624d5faSMark Brown static ssize_t codec_reg_write_file(struct file *file, 2562624d5faSMark Brown const char __user *user_buf, size_t count, loff_t *ppos) 2572624d5faSMark Brown { 2582624d5faSMark Brown char buf[32]; 25934e268d8SStephen Boyd size_t buf_size; 2602624d5faSMark Brown char *start = buf; 2612624d5faSMark Brown unsigned long reg, value; 2622624d5faSMark Brown struct snd_soc_codec *codec = file->private_data; 263b785a492SJingoo Han int ret; 2642624d5faSMark Brown 2652624d5faSMark Brown buf_size = min(count, (sizeof(buf)-1)); 2662624d5faSMark Brown if (copy_from_user(buf, user_buf, buf_size)) 2672624d5faSMark Brown return -EFAULT; 2682624d5faSMark Brown buf[buf_size] = 0; 2692624d5faSMark Brown 2702624d5faSMark Brown while (*start == ' ') 2712624d5faSMark Brown start++; 2722624d5faSMark Brown reg = simple_strtoul(start, &start, 16); 2732624d5faSMark Brown while (*start == ' ') 2742624d5faSMark Brown start++; 275b785a492SJingoo Han ret = kstrtoul(start, 16, &value); 276b785a492SJingoo Han if (ret) 277b785a492SJingoo Han return ret; 2780d51a9cbSMark Brown 2790d51a9cbSMark Brown /* Userspace has been fiddling around behind the kernel's back */ 280373d4d09SRusty Russell add_taint(TAINT_USER, LOCKDEP_NOW_UNRELIABLE); 2810d51a9cbSMark Brown 282e4f078d8SDimitris Papastamos snd_soc_write(codec, reg, value); 2832624d5faSMark Brown return buf_size; 2842624d5faSMark Brown } 2852624d5faSMark Brown 2862624d5faSMark Brown static const struct file_operations codec_reg_fops = { 287234e3405SStephen Boyd .open = simple_open, 2882624d5faSMark Brown .read = codec_reg_read_file, 2892624d5faSMark Brown .write = codec_reg_write_file, 2906038f373SArnd Bergmann .llseek = default_llseek, 2912624d5faSMark Brown }; 2922624d5faSMark Brown 29381c7cfd1SLars-Peter Clausen static void soc_init_component_debugfs(struct snd_soc_component *component) 294e73f3de5SRussell King { 29581c7cfd1SLars-Peter Clausen if (component->debugfs_prefix) { 29681c7cfd1SLars-Peter Clausen char *name; 297e73f3de5SRussell King 29881c7cfd1SLars-Peter Clausen name = kasprintf(GFP_KERNEL, "%s:%s", 29981c7cfd1SLars-Peter Clausen component->debugfs_prefix, component->name); 30081c7cfd1SLars-Peter Clausen if (name) { 30181c7cfd1SLars-Peter Clausen component->debugfs_root = debugfs_create_dir(name, 30281c7cfd1SLars-Peter Clausen component->card->debugfs_card_root); 30381c7cfd1SLars-Peter Clausen kfree(name); 30481c7cfd1SLars-Peter Clausen } 30581c7cfd1SLars-Peter Clausen } else { 30681c7cfd1SLars-Peter Clausen component->debugfs_root = debugfs_create_dir(component->name, 30781c7cfd1SLars-Peter Clausen component->card->debugfs_card_root); 308e73f3de5SRussell King } 309e73f3de5SRussell King 31081c7cfd1SLars-Peter Clausen if (!component->debugfs_root) { 31181c7cfd1SLars-Peter Clausen dev_warn(component->dev, 31281c7cfd1SLars-Peter Clausen "ASoC: Failed to create component debugfs directory\n"); 3132624d5faSMark Brown return; 3142624d5faSMark Brown } 3152624d5faSMark Brown 31681c7cfd1SLars-Peter Clausen snd_soc_dapm_debugfs_init(snd_soc_component_get_dapm(component), 31781c7cfd1SLars-Peter Clausen component->debugfs_root); 31881c7cfd1SLars-Peter Clausen 31981c7cfd1SLars-Peter Clausen if (component->init_debugfs) 32081c7cfd1SLars-Peter Clausen component->init_debugfs(component); 32181c7cfd1SLars-Peter Clausen } 32281c7cfd1SLars-Peter Clausen 32381c7cfd1SLars-Peter Clausen static void soc_cleanup_component_debugfs(struct snd_soc_component *component) 32481c7cfd1SLars-Peter Clausen { 32581c7cfd1SLars-Peter Clausen debugfs_remove_recursive(component->debugfs_root); 32681c7cfd1SLars-Peter Clausen } 32781c7cfd1SLars-Peter Clausen 32881c7cfd1SLars-Peter Clausen static void soc_init_codec_debugfs(struct snd_soc_component *component) 32981c7cfd1SLars-Peter Clausen { 33081c7cfd1SLars-Peter Clausen struct snd_soc_codec *codec = snd_soc_component_to_codec(component); 33181c7cfd1SLars-Peter Clausen 3322624d5faSMark Brown codec->debugfs_reg = debugfs_create_file("codec_reg", 0644, 33381c7cfd1SLars-Peter Clausen codec->component.debugfs_root, 3342624d5faSMark Brown codec, &codec_reg_fops); 3352624d5faSMark Brown if (!codec->debugfs_reg) 33610e8aa9aSMichał Mirosław dev_warn(codec->dev, 33710e8aa9aSMichał Mirosław "ASoC: Failed to create codec register debugfs file\n"); 338731f1ab2SSebastien Guiriec } 339731f1ab2SSebastien Guiriec 340c3c5a19aSMark Brown static ssize_t codec_list_read_file(struct file *file, char __user *user_buf, 341c3c5a19aSMark Brown size_t count, loff_t *ppos) 342c3c5a19aSMark Brown { 343c3c5a19aSMark Brown char *buf = kmalloc(PAGE_SIZE, GFP_KERNEL); 3442b194f9dSMark Brown ssize_t len, ret = 0; 345c3c5a19aSMark Brown struct snd_soc_codec *codec; 346c3c5a19aSMark Brown 347c3c5a19aSMark Brown if (!buf) 348c3c5a19aSMark Brown return -ENOMEM; 349c3c5a19aSMark Brown 3502b194f9dSMark Brown list_for_each_entry(codec, &codec_list, list) { 3512b194f9dSMark Brown len = snprintf(buf + ret, PAGE_SIZE - ret, "%s\n", 352f4333203SLars-Peter Clausen codec->component.name); 3532b194f9dSMark Brown if (len >= 0) 3542b194f9dSMark Brown ret += len; 3552b194f9dSMark Brown if (ret > PAGE_SIZE) { 3562b194f9dSMark Brown ret = PAGE_SIZE; 3572b194f9dSMark Brown break; 3582b194f9dSMark Brown } 3592b194f9dSMark Brown } 360c3c5a19aSMark Brown 361c3c5a19aSMark Brown if (ret >= 0) 362c3c5a19aSMark Brown ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret); 363c3c5a19aSMark Brown 364c3c5a19aSMark Brown kfree(buf); 365c3c5a19aSMark Brown 366c3c5a19aSMark Brown return ret; 367c3c5a19aSMark Brown } 368c3c5a19aSMark Brown 369c3c5a19aSMark Brown static const struct file_operations codec_list_fops = { 370c3c5a19aSMark Brown .read = codec_list_read_file, 371c3c5a19aSMark Brown .llseek = default_llseek,/* read accesses f_pos */ 372c3c5a19aSMark Brown }; 373c3c5a19aSMark Brown 374f3208780SMark Brown static ssize_t dai_list_read_file(struct file *file, char __user *user_buf, 375f3208780SMark Brown size_t count, loff_t *ppos) 376f3208780SMark Brown { 377f3208780SMark Brown char *buf = kmalloc(PAGE_SIZE, GFP_KERNEL); 3782b194f9dSMark Brown ssize_t len, ret = 0; 3791438c2f6SLars-Peter Clausen struct snd_soc_component *component; 380f3208780SMark Brown struct snd_soc_dai *dai; 381f3208780SMark Brown 382f3208780SMark Brown if (!buf) 383f3208780SMark Brown return -ENOMEM; 384f3208780SMark Brown 3851438c2f6SLars-Peter Clausen list_for_each_entry(component, &component_list, list) { 3861438c2f6SLars-Peter Clausen list_for_each_entry(dai, &component->dai_list, list) { 3871438c2f6SLars-Peter Clausen len = snprintf(buf + ret, PAGE_SIZE - ret, "%s\n", 3881438c2f6SLars-Peter Clausen dai->name); 3892b194f9dSMark Brown if (len >= 0) 3902b194f9dSMark Brown ret += len; 3912b194f9dSMark Brown if (ret > PAGE_SIZE) { 3922b194f9dSMark Brown ret = PAGE_SIZE; 3932b194f9dSMark Brown break; 3942b194f9dSMark Brown } 3952b194f9dSMark Brown } 3961438c2f6SLars-Peter Clausen } 397f3208780SMark Brown 398f3208780SMark Brown ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret); 399f3208780SMark Brown 400f3208780SMark Brown kfree(buf); 401f3208780SMark Brown 402f3208780SMark Brown return ret; 403f3208780SMark Brown } 404f3208780SMark Brown 405f3208780SMark Brown static const struct file_operations dai_list_fops = { 406f3208780SMark Brown .read = dai_list_read_file, 407f3208780SMark Brown .llseek = default_llseek,/* read accesses f_pos */ 408f3208780SMark Brown }; 409f3208780SMark Brown 41019c7ac27SMark Brown static ssize_t platform_list_read_file(struct file *file, 41119c7ac27SMark Brown char __user *user_buf, 41219c7ac27SMark Brown size_t count, loff_t *ppos) 41319c7ac27SMark Brown { 41419c7ac27SMark Brown char *buf = kmalloc(PAGE_SIZE, GFP_KERNEL); 4152b194f9dSMark Brown ssize_t len, ret = 0; 41619c7ac27SMark Brown struct snd_soc_platform *platform; 41719c7ac27SMark Brown 41819c7ac27SMark Brown if (!buf) 41919c7ac27SMark Brown return -ENOMEM; 42019c7ac27SMark Brown 4212b194f9dSMark Brown list_for_each_entry(platform, &platform_list, list) { 4222b194f9dSMark Brown len = snprintf(buf + ret, PAGE_SIZE - ret, "%s\n", 423f4333203SLars-Peter Clausen platform->component.name); 4242b194f9dSMark Brown if (len >= 0) 4252b194f9dSMark Brown ret += len; 4262b194f9dSMark Brown if (ret > PAGE_SIZE) { 4272b194f9dSMark Brown ret = PAGE_SIZE; 4282b194f9dSMark Brown break; 4292b194f9dSMark Brown } 4302b194f9dSMark Brown } 43119c7ac27SMark Brown 43219c7ac27SMark Brown ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret); 43319c7ac27SMark Brown 43419c7ac27SMark Brown kfree(buf); 43519c7ac27SMark Brown 43619c7ac27SMark Brown return ret; 43719c7ac27SMark Brown } 43819c7ac27SMark Brown 43919c7ac27SMark Brown static const struct file_operations platform_list_fops = { 44019c7ac27SMark Brown .read = platform_list_read_file, 44119c7ac27SMark Brown .llseek = default_llseek,/* read accesses f_pos */ 44219c7ac27SMark Brown }; 44319c7ac27SMark Brown 444a6052154SJarkko Nikula static void soc_init_card_debugfs(struct snd_soc_card *card) 445a6052154SJarkko Nikula { 446a6052154SJarkko Nikula card->debugfs_card_root = debugfs_create_dir(card->name, 4478a9dab1aSMark Brown snd_soc_debugfs_root); 4483a45b867SJarkko Nikula if (!card->debugfs_card_root) { 449a6052154SJarkko Nikula dev_warn(card->dev, 4507c08be84SLothar Waßmann "ASoC: Failed to create card debugfs directory\n"); 4513a45b867SJarkko Nikula return; 4523a45b867SJarkko Nikula } 4533a45b867SJarkko Nikula 4543a45b867SJarkko Nikula card->debugfs_pop_time = debugfs_create_u32("dapm_pop_time", 0644, 4553a45b867SJarkko Nikula card->debugfs_card_root, 4563a45b867SJarkko Nikula &card->pop_time); 4573a45b867SJarkko Nikula if (!card->debugfs_pop_time) 4583a45b867SJarkko Nikula dev_warn(card->dev, 459f110bfc7SLiam Girdwood "ASoC: Failed to create pop time debugfs file\n"); 460a6052154SJarkko Nikula } 461a6052154SJarkko Nikula 462a6052154SJarkko Nikula static void soc_cleanup_card_debugfs(struct snd_soc_card *card) 463a6052154SJarkko Nikula { 464a6052154SJarkko Nikula debugfs_remove_recursive(card->debugfs_card_root); 465a6052154SJarkko Nikula } 466a6052154SJarkko Nikula 4672624d5faSMark Brown #else 4682624d5faSMark Brown 46981c7cfd1SLars-Peter Clausen #define soc_init_codec_debugfs NULL 47081c7cfd1SLars-Peter Clausen 47181c7cfd1SLars-Peter Clausen static inline void soc_init_component_debugfs( 47281c7cfd1SLars-Peter Clausen struct snd_soc_component *component) 4732624d5faSMark Brown { 4742624d5faSMark Brown } 4752624d5faSMark Brown 47681c7cfd1SLars-Peter Clausen static inline void soc_cleanup_component_debugfs( 47781c7cfd1SLars-Peter Clausen struct snd_soc_component *component) 478731f1ab2SSebastien Guiriec { 479731f1ab2SSebastien Guiriec } 480731f1ab2SSebastien Guiriec 481b95fccbcSAxel Lin static inline void soc_init_card_debugfs(struct snd_soc_card *card) 482b95fccbcSAxel Lin { 483b95fccbcSAxel Lin } 484b95fccbcSAxel Lin 485b95fccbcSAxel Lin static inline void soc_cleanup_card_debugfs(struct snd_soc_card *card) 486b95fccbcSAxel Lin { 487b95fccbcSAxel Lin } 4882624d5faSMark Brown #endif 4892624d5faSMark Brown 49047c88fffSLiam Girdwood struct snd_pcm_substream *snd_soc_get_dai_substream(struct snd_soc_card *card, 49147c88fffSLiam Girdwood const char *dai_link, int stream) 49247c88fffSLiam Girdwood { 49347c88fffSLiam Girdwood int i; 49447c88fffSLiam Girdwood 49547c88fffSLiam Girdwood for (i = 0; i < card->num_links; i++) { 49647c88fffSLiam Girdwood if (card->rtd[i].dai_link->no_pcm && 49747c88fffSLiam Girdwood !strcmp(card->rtd[i].dai_link->name, dai_link)) 49847c88fffSLiam Girdwood return card->rtd[i].pcm->streams[stream].substream; 49947c88fffSLiam Girdwood } 500f110bfc7SLiam Girdwood dev_dbg(card->dev, "ASoC: failed to find dai link %s\n", dai_link); 50147c88fffSLiam Girdwood return NULL; 50247c88fffSLiam Girdwood } 50347c88fffSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_get_dai_substream); 50447c88fffSLiam Girdwood 50547c88fffSLiam Girdwood struct snd_soc_pcm_runtime *snd_soc_get_pcm_runtime(struct snd_soc_card *card, 50647c88fffSLiam Girdwood const char *dai_link) 50747c88fffSLiam Girdwood { 50847c88fffSLiam Girdwood int i; 50947c88fffSLiam Girdwood 51047c88fffSLiam Girdwood for (i = 0; i < card->num_links; i++) { 51147c88fffSLiam Girdwood if (!strcmp(card->rtd[i].dai_link->name, dai_link)) 51247c88fffSLiam Girdwood return &card->rtd[i]; 51347c88fffSLiam Girdwood } 514f110bfc7SLiam Girdwood dev_dbg(card->dev, "ASoC: failed to find rtd %s\n", dai_link); 51547c88fffSLiam Girdwood return NULL; 51647c88fffSLiam Girdwood } 51747c88fffSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_get_pcm_runtime); 51847c88fffSLiam Girdwood 5199d58a077SRichard Fitzgerald static void codec2codec_close_delayed_work(struct work_struct *work) 5209d58a077SRichard Fitzgerald { 5219d58a077SRichard Fitzgerald /* Currently nothing to do for c2c links 5229d58a077SRichard Fitzgerald * Since c2c links are internal nodes in the DAPM graph and 5239d58a077SRichard Fitzgerald * don't interface with the outside world or application layer 5249d58a077SRichard Fitzgerald * we don't have to do any special handling on close. 5259d58a077SRichard Fitzgerald */ 5269d58a077SRichard Fitzgerald } 5279d58a077SRichard Fitzgerald 5286f8ab4acSMark Brown #ifdef CONFIG_PM_SLEEP 529db2a4165SFrank Mandarino /* powers down audio subsystem for suspend */ 5306f8ab4acSMark Brown int snd_soc_suspend(struct device *dev) 531db2a4165SFrank Mandarino { 5326f8ab4acSMark Brown struct snd_soc_card *card = dev_get_drvdata(dev); 5332eea392dSJarkko Nikula struct snd_soc_codec *codec; 53488bd870fSBenoit Cousson int i, j; 535db2a4165SFrank Mandarino 536c5599b87SLars-Peter Clausen /* If the card is not initialized yet there is nothing to do */ 537c5599b87SLars-Peter Clausen if (!card->instantiated) 538e3509ff0SDaniel Mack return 0; 539e3509ff0SDaniel Mack 5406ed25978SAndy Green /* Due to the resume being scheduled into a workqueue we could 5416ed25978SAndy Green * suspend before that's finished - wait for it to complete. 5426ed25978SAndy Green */ 543f0fba2adSLiam Girdwood snd_power_lock(card->snd_card); 544f0fba2adSLiam Girdwood snd_power_wait(card->snd_card, SNDRV_CTL_POWER_D0); 545f0fba2adSLiam Girdwood snd_power_unlock(card->snd_card); 5466ed25978SAndy Green 5476ed25978SAndy Green /* we're going to block userspace touching us until resume completes */ 548f0fba2adSLiam Girdwood snd_power_change_state(card->snd_card, SNDRV_CTL_POWER_D3hot); 5496ed25978SAndy Green 550a00f90f9SMark Brown /* mute any active DACs */ 551f0fba2adSLiam Girdwood for (i = 0; i < card->num_rtd; i++) { 5523efab7dcSMark Brown 553f0fba2adSLiam Girdwood if (card->rtd[i].dai_link->ignore_suspend) 5543efab7dcSMark Brown continue; 5553efab7dcSMark Brown 55688bd870fSBenoit Cousson for (j = 0; j < card->rtd[i].num_codecs; j++) { 55788bd870fSBenoit Cousson struct snd_soc_dai *dai = card->rtd[i].codec_dais[j]; 55888bd870fSBenoit Cousson struct snd_soc_dai_driver *drv = dai->driver; 55988bd870fSBenoit Cousson 560f0fba2adSLiam Girdwood if (drv->ops->digital_mute && dai->playback_active) 561f0fba2adSLiam Girdwood drv->ops->digital_mute(dai, 1); 562db2a4165SFrank Mandarino } 56388bd870fSBenoit Cousson } 564db2a4165SFrank Mandarino 5654ccab3e7SLiam Girdwood /* suspend all pcms */ 566f0fba2adSLiam Girdwood for (i = 0; i < card->num_rtd; i++) { 567f0fba2adSLiam Girdwood if (card->rtd[i].dai_link->ignore_suspend) 5683efab7dcSMark Brown continue; 5693efab7dcSMark Brown 570f0fba2adSLiam Girdwood snd_pcm_suspend_all(card->rtd[i].pcm); 5713efab7dcSMark Brown } 5724ccab3e7SLiam Girdwood 57387506549SMark Brown if (card->suspend_pre) 57470b2ac12SMark Brown card->suspend_pre(card); 575db2a4165SFrank Mandarino 576f0fba2adSLiam Girdwood for (i = 0; i < card->num_rtd; i++) { 577f0fba2adSLiam Girdwood struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai; 5783efab7dcSMark Brown 579f0fba2adSLiam Girdwood if (card->rtd[i].dai_link->ignore_suspend) 5803efab7dcSMark Brown continue; 5813efab7dcSMark Brown 582bc263214SLars-Peter Clausen if (cpu_dai->driver->suspend && !cpu_dai->driver->bus_control) 583f0fba2adSLiam Girdwood cpu_dai->driver->suspend(cpu_dai); 584db2a4165SFrank Mandarino } 585db2a4165SFrank Mandarino 586db2a4165SFrank Mandarino /* close any waiting streams and save state */ 587f0fba2adSLiam Girdwood for (i = 0; i < card->num_rtd; i++) { 58888bd870fSBenoit Cousson struct snd_soc_dai **codec_dais = card->rtd[i].codec_dais; 58943829731STejun Heo flush_delayed_work(&card->rtd[i].delayed_work); 59088bd870fSBenoit Cousson for (j = 0; j < card->rtd[i].num_codecs; j++) { 59188bd870fSBenoit Cousson codec_dais[j]->codec->dapm.suspend_bias_level = 59288bd870fSBenoit Cousson codec_dais[j]->codec->dapm.bias_level; 59388bd870fSBenoit Cousson } 594f0fba2adSLiam Girdwood } 595db2a4165SFrank Mandarino 596f0fba2adSLiam Girdwood for (i = 0; i < card->num_rtd; i++) { 5973efab7dcSMark Brown 598f0fba2adSLiam Girdwood if (card->rtd[i].dai_link->ignore_suspend) 5993efab7dcSMark Brown continue; 6003efab7dcSMark Brown 6017bd3a6f3SMark Brown snd_soc_dapm_stream_event(&card->rtd[i], 6027bd3a6f3SMark Brown SNDRV_PCM_STREAM_PLAYBACK, 603db2a4165SFrank Mandarino SND_SOC_DAPM_STREAM_SUSPEND); 604f0fba2adSLiam Girdwood 6057bd3a6f3SMark Brown snd_soc_dapm_stream_event(&card->rtd[i], 6067bd3a6f3SMark Brown SNDRV_PCM_STREAM_CAPTURE, 607db2a4165SFrank Mandarino SND_SOC_DAPM_STREAM_SUSPEND); 608db2a4165SFrank Mandarino } 609db2a4165SFrank Mandarino 6108be4da29SLars-Peter Clausen /* Recheck all endpoints too, their state is affected by suspend */ 6118be4da29SLars-Peter Clausen dapm_mark_endpoints_dirty(card); 612e2d32ff6SMark Brown snd_soc_dapm_sync(&card->dapm); 613e2d32ff6SMark Brown 614f0fba2adSLiam Girdwood /* suspend all CODECs */ 6152eea392dSJarkko Nikula list_for_each_entry(codec, &card->codec_dev_list, card_list) { 6161547aba9SMark Brown /* If there are paths active then the CODEC will be held with 6171547aba9SMark Brown * bias _ON and should not be suspended. */ 618a8093297SLars-Peter Clausen if (!codec->suspended) { 619ce6120ccSLiam Girdwood switch (codec->dapm.bias_level) { 6201547aba9SMark Brown case SND_SOC_BIAS_STANDBY: 621125a25daSMark Brown /* 622125a25daSMark Brown * If the CODEC is capable of idle 623125a25daSMark Brown * bias off then being in STANDBY 624125a25daSMark Brown * means it's doing something, 625125a25daSMark Brown * otherwise fall through. 626125a25daSMark Brown */ 627125a25daSMark Brown if (codec->dapm.idle_bias_off) { 628125a25daSMark Brown dev_dbg(codec->dev, 62910e8aa9aSMichał Mirosław "ASoC: idle_bias_off CODEC on over suspend\n"); 630125a25daSMark Brown break; 631125a25daSMark Brown } 632a8093297SLars-Peter Clausen 6331547aba9SMark Brown case SND_SOC_BIAS_OFF: 634a8093297SLars-Peter Clausen if (codec->driver->suspend) 63584b315eeSLars-Peter Clausen codec->driver->suspend(codec); 636f0fba2adSLiam Girdwood codec->suspended = 1; 637e2c330b9SLars-Peter Clausen if (codec->component.regmap) 638e2c330b9SLars-Peter Clausen regcache_mark_dirty(codec->component.regmap); 639988e8cc4SNicolin Chen /* deactivate pins to sleep state */ 640988e8cc4SNicolin Chen pinctrl_pm_select_sleep_state(codec->dev); 6411547aba9SMark Brown break; 6421547aba9SMark Brown default: 64310e8aa9aSMichał Mirosław dev_dbg(codec->dev, 64410e8aa9aSMichał Mirosław "ASoC: CODEC is on over suspend\n"); 6451547aba9SMark Brown break; 6461547aba9SMark Brown } 6471547aba9SMark Brown } 648f0fba2adSLiam Girdwood } 649db2a4165SFrank Mandarino 650f0fba2adSLiam Girdwood for (i = 0; i < card->num_rtd; i++) { 651f0fba2adSLiam Girdwood struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai; 6523efab7dcSMark Brown 653f0fba2adSLiam Girdwood if (card->rtd[i].dai_link->ignore_suspend) 6543efab7dcSMark Brown continue; 6553efab7dcSMark Brown 656bc263214SLars-Peter Clausen if (cpu_dai->driver->suspend && cpu_dai->driver->bus_control) 657f0fba2adSLiam Girdwood cpu_dai->driver->suspend(cpu_dai); 658988e8cc4SNicolin Chen 659988e8cc4SNicolin Chen /* deactivate pins to sleep state */ 660988e8cc4SNicolin Chen pinctrl_pm_select_sleep_state(cpu_dai->dev); 661db2a4165SFrank Mandarino } 662db2a4165SFrank Mandarino 66387506549SMark Brown if (card->suspend_post) 66470b2ac12SMark Brown card->suspend_post(card); 665db2a4165SFrank Mandarino 666db2a4165SFrank Mandarino return 0; 667db2a4165SFrank Mandarino } 6686f8ab4acSMark Brown EXPORT_SYMBOL_GPL(snd_soc_suspend); 669db2a4165SFrank Mandarino 6706ed25978SAndy Green /* deferred resume work, so resume can complete before we finished 6716ed25978SAndy Green * setting our codec back up, which can be very slow on I2C 6726ed25978SAndy Green */ 6736ed25978SAndy Green static void soc_resume_deferred(struct work_struct *work) 674db2a4165SFrank Mandarino { 675f0fba2adSLiam Girdwood struct snd_soc_card *card = 676f0fba2adSLiam Girdwood container_of(work, struct snd_soc_card, deferred_resume_work); 6772eea392dSJarkko Nikula struct snd_soc_codec *codec; 67888bd870fSBenoit Cousson int i, j; 679db2a4165SFrank Mandarino 6806ed25978SAndy Green /* our power state is still SNDRV_CTL_POWER_D3hot from suspend time, 6816ed25978SAndy Green * so userspace apps are blocked from touching us 6826ed25978SAndy Green */ 6836ed25978SAndy Green 684f110bfc7SLiam Girdwood dev_dbg(card->dev, "ASoC: starting resume work\n"); 6856ed25978SAndy Green 6869949788bSMark Brown /* Bring us up into D2 so that DAPM starts enabling things */ 687f0fba2adSLiam Girdwood snd_power_change_state(card->snd_card, SNDRV_CTL_POWER_D2); 6889949788bSMark Brown 68987506549SMark Brown if (card->resume_pre) 69070b2ac12SMark Brown card->resume_pre(card); 691db2a4165SFrank Mandarino 692bc263214SLars-Peter Clausen /* resume control bus DAIs */ 693f0fba2adSLiam Girdwood for (i = 0; i < card->num_rtd; i++) { 694f0fba2adSLiam Girdwood struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai; 6953efab7dcSMark Brown 696f0fba2adSLiam Girdwood if (card->rtd[i].dai_link->ignore_suspend) 6973efab7dcSMark Brown continue; 6983efab7dcSMark Brown 699bc263214SLars-Peter Clausen if (cpu_dai->driver->resume && cpu_dai->driver->bus_control) 700f0fba2adSLiam Girdwood cpu_dai->driver->resume(cpu_dai); 701db2a4165SFrank Mandarino } 702db2a4165SFrank Mandarino 7032eea392dSJarkko Nikula list_for_each_entry(codec, &card->codec_dev_list, card_list) { 7041547aba9SMark Brown /* If the CODEC was idle over suspend then it will have been 7051547aba9SMark Brown * left with bias OFF or STANDBY and suspended so we must now 7061547aba9SMark Brown * resume. Otherwise the suspend was suppressed. 7071547aba9SMark Brown */ 708a8093297SLars-Peter Clausen if (codec->suspended) { 709ce6120ccSLiam Girdwood switch (codec->dapm.bias_level) { 7101547aba9SMark Brown case SND_SOC_BIAS_STANDBY: 7111547aba9SMark Brown case SND_SOC_BIAS_OFF: 712a8093297SLars-Peter Clausen if (codec->driver->resume) 713f0fba2adSLiam Girdwood codec->driver->resume(codec); 714f0fba2adSLiam Girdwood codec->suspended = 0; 7151547aba9SMark Brown break; 7161547aba9SMark Brown default: 71710e8aa9aSMichał Mirosław dev_dbg(codec->dev, 71810e8aa9aSMichał Mirosław "ASoC: CODEC was on over suspend\n"); 7191547aba9SMark Brown break; 7201547aba9SMark Brown } 7211547aba9SMark Brown } 722f0fba2adSLiam Girdwood } 723db2a4165SFrank Mandarino 724f0fba2adSLiam Girdwood for (i = 0; i < card->num_rtd; i++) { 7253efab7dcSMark Brown 726f0fba2adSLiam Girdwood if (card->rtd[i].dai_link->ignore_suspend) 7273efab7dcSMark Brown continue; 7283efab7dcSMark Brown 7297bd3a6f3SMark Brown snd_soc_dapm_stream_event(&card->rtd[i], 730d9b0951bSLiam Girdwood SNDRV_PCM_STREAM_PLAYBACK, 731db2a4165SFrank Mandarino SND_SOC_DAPM_STREAM_RESUME); 732f0fba2adSLiam Girdwood 7337bd3a6f3SMark Brown snd_soc_dapm_stream_event(&card->rtd[i], 734d9b0951bSLiam Girdwood SNDRV_PCM_STREAM_CAPTURE, 735db2a4165SFrank Mandarino SND_SOC_DAPM_STREAM_RESUME); 736db2a4165SFrank Mandarino } 737db2a4165SFrank Mandarino 7383ff3f64bSMark Brown /* unmute any active DACs */ 739f0fba2adSLiam Girdwood for (i = 0; i < card->num_rtd; i++) { 7403efab7dcSMark Brown 741f0fba2adSLiam Girdwood if (card->rtd[i].dai_link->ignore_suspend) 7423efab7dcSMark Brown continue; 7433efab7dcSMark Brown 74488bd870fSBenoit Cousson for (j = 0; j < card->rtd[i].num_codecs; j++) { 74588bd870fSBenoit Cousson struct snd_soc_dai *dai = card->rtd[i].codec_dais[j]; 74688bd870fSBenoit Cousson struct snd_soc_dai_driver *drv = dai->driver; 74788bd870fSBenoit Cousson 748f0fba2adSLiam Girdwood if (drv->ops->digital_mute && dai->playback_active) 749f0fba2adSLiam Girdwood drv->ops->digital_mute(dai, 0); 750db2a4165SFrank Mandarino } 75188bd870fSBenoit Cousson } 752db2a4165SFrank Mandarino 753f0fba2adSLiam Girdwood for (i = 0; i < card->num_rtd; i++) { 754f0fba2adSLiam Girdwood struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai; 7553efab7dcSMark Brown 756f0fba2adSLiam Girdwood if (card->rtd[i].dai_link->ignore_suspend) 7573efab7dcSMark Brown continue; 7583efab7dcSMark Brown 759bc263214SLars-Peter Clausen if (cpu_dai->driver->resume && !cpu_dai->driver->bus_control) 760f0fba2adSLiam Girdwood cpu_dai->driver->resume(cpu_dai); 761db2a4165SFrank Mandarino } 762db2a4165SFrank Mandarino 76387506549SMark Brown if (card->resume_post) 76470b2ac12SMark Brown card->resume_post(card); 765db2a4165SFrank Mandarino 766f110bfc7SLiam Girdwood dev_dbg(card->dev, "ASoC: resume work completed\n"); 7676ed25978SAndy Green 7686ed25978SAndy Green /* userspace can access us now we are back as we were before */ 769f0fba2adSLiam Girdwood snd_power_change_state(card->snd_card, SNDRV_CTL_POWER_D0); 770e2d32ff6SMark Brown 7718be4da29SLars-Peter Clausen /* Recheck all endpoints too, their state is affected by suspend */ 7728be4da29SLars-Peter Clausen dapm_mark_endpoints_dirty(card); 773e2d32ff6SMark Brown snd_soc_dapm_sync(&card->dapm); 7746ed25978SAndy Green } 7756ed25978SAndy Green 7766ed25978SAndy Green /* powers up audio subsystem after a suspend */ 7776f8ab4acSMark Brown int snd_soc_resume(struct device *dev) 7786ed25978SAndy Green { 7796f8ab4acSMark Brown struct snd_soc_card *card = dev_get_drvdata(dev); 780bc263214SLars-Peter Clausen bool bus_control = false; 781bc263214SLars-Peter Clausen int i; 782b9dd94a8SPeter Ujfalusi 783c5599b87SLars-Peter Clausen /* If the card is not initialized yet there is nothing to do */ 784c5599b87SLars-Peter Clausen if (!card->instantiated) 7855ff1ddf2SEric Miao return 0; 7865ff1ddf2SEric Miao 787988e8cc4SNicolin Chen /* activate pins from sleep state */ 788988e8cc4SNicolin Chen for (i = 0; i < card->num_rtd; i++) { 78988bd870fSBenoit Cousson struct snd_soc_pcm_runtime *rtd = &card->rtd[i]; 79088bd870fSBenoit Cousson struct snd_soc_dai **codec_dais = rtd->codec_dais; 79188bd870fSBenoit Cousson struct snd_soc_dai *cpu_dai = rtd->cpu_dai; 79288bd870fSBenoit Cousson int j; 79388bd870fSBenoit Cousson 794988e8cc4SNicolin Chen if (cpu_dai->active) 795988e8cc4SNicolin Chen pinctrl_pm_select_default_state(cpu_dai->dev); 79688bd870fSBenoit Cousson 79788bd870fSBenoit Cousson for (j = 0; j < rtd->num_codecs; j++) { 79888bd870fSBenoit Cousson struct snd_soc_dai *codec_dai = codec_dais[j]; 799988e8cc4SNicolin Chen if (codec_dai->active) 800988e8cc4SNicolin Chen pinctrl_pm_select_default_state(codec_dai->dev); 801988e8cc4SNicolin Chen } 80288bd870fSBenoit Cousson } 803988e8cc4SNicolin Chen 804bc263214SLars-Peter Clausen /* 805bc263214SLars-Peter Clausen * DAIs that also act as the control bus master might have other drivers 806bc263214SLars-Peter Clausen * hanging off them so need to resume immediately. Other drivers don't 807bc263214SLars-Peter Clausen * have that problem and may take a substantial amount of time to resume 80864ab9baaSMark Brown * due to I/O costs and anti-pop so handle them out of line. 80964ab9baaSMark Brown */ 810f0fba2adSLiam Girdwood for (i = 0; i < card->num_rtd; i++) { 811f0fba2adSLiam Girdwood struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai; 812bc263214SLars-Peter Clausen bus_control |= cpu_dai->driver->bus_control; 81382e14e8bSStephen Warren } 814bc263214SLars-Peter Clausen if (bus_control) { 815bc263214SLars-Peter Clausen dev_dbg(dev, "ASoC: Resuming control bus master immediately\n"); 81664ab9baaSMark Brown soc_resume_deferred(&card->deferred_resume_work); 81764ab9baaSMark Brown } else { 818f110bfc7SLiam Girdwood dev_dbg(dev, "ASoC: Scheduling resume work\n"); 8196308419aSMark Brown if (!schedule_work(&card->deferred_resume_work)) 820f110bfc7SLiam Girdwood dev_err(dev, "ASoC: resume work item may be lost\n"); 821f0fba2adSLiam Girdwood } 8226ed25978SAndy Green 823db2a4165SFrank Mandarino return 0; 824db2a4165SFrank Mandarino } 8256f8ab4acSMark Brown EXPORT_SYMBOL_GPL(snd_soc_resume); 826db2a4165SFrank Mandarino #else 8276f8ab4acSMark Brown #define snd_soc_suspend NULL 8286f8ab4acSMark Brown #define snd_soc_resume NULL 829db2a4165SFrank Mandarino #endif 830db2a4165SFrank Mandarino 83185e7652dSLars-Peter Clausen static const struct snd_soc_dai_ops null_dai_ops = { 83202a06d30SBarry Song }; 83302a06d30SBarry Song 83465d9361fSLars-Peter Clausen static struct snd_soc_component *soc_find_component( 83565d9361fSLars-Peter Clausen const struct device_node *of_node, const char *name) 83612023a9aSMisael Lopez Cruz { 83765d9361fSLars-Peter Clausen struct snd_soc_component *component; 83812023a9aSMisael Lopez Cruz 83965d9361fSLars-Peter Clausen list_for_each_entry(component, &component_list, list) { 84065d9361fSLars-Peter Clausen if (of_node) { 84165d9361fSLars-Peter Clausen if (component->dev->of_node == of_node) 84265d9361fSLars-Peter Clausen return component; 84365d9361fSLars-Peter Clausen } else if (strcmp(component->name, name) == 0) { 84465d9361fSLars-Peter Clausen return component; 84512023a9aSMisael Lopez Cruz } 84612023a9aSMisael Lopez Cruz } 84712023a9aSMisael Lopez Cruz 84812023a9aSMisael Lopez Cruz return NULL; 84912023a9aSMisael Lopez Cruz } 85012023a9aSMisael Lopez Cruz 85114621c7eSLars-Peter Clausen static struct snd_soc_dai *snd_soc_find_dai( 85214621c7eSLars-Peter Clausen const struct snd_soc_dai_link_component *dlc) 85312023a9aSMisael Lopez Cruz { 85414621c7eSLars-Peter Clausen struct snd_soc_component *component; 85514621c7eSLars-Peter Clausen struct snd_soc_dai *dai; 85612023a9aSMisael Lopez Cruz 85714621c7eSLars-Peter Clausen /* Find CPU DAI from registered DAIs*/ 85814621c7eSLars-Peter Clausen list_for_each_entry(component, &component_list, list) { 85914621c7eSLars-Peter Clausen if (dlc->of_node && component->dev->of_node != dlc->of_node) 86012023a9aSMisael Lopez Cruz continue; 8611ffae361SLars-Peter Clausen if (dlc->name && strcmp(component->name, dlc->name)) 86212023a9aSMisael Lopez Cruz continue; 86314621c7eSLars-Peter Clausen list_for_each_entry(dai, &component->dai_list, list) { 86414621c7eSLars-Peter Clausen if (dlc->dai_name && strcmp(dai->name, dlc->dai_name)) 86514621c7eSLars-Peter Clausen continue; 86612023a9aSMisael Lopez Cruz 86714621c7eSLars-Peter Clausen return dai; 86812023a9aSMisael Lopez Cruz } 86912023a9aSMisael Lopez Cruz } 87012023a9aSMisael Lopez Cruz 87112023a9aSMisael Lopez Cruz return NULL; 87212023a9aSMisael Lopez Cruz } 87312023a9aSMisael Lopez Cruz 874f0fba2adSLiam Girdwood static int soc_bind_dai_link(struct snd_soc_card *card, int num) 875db2a4165SFrank Mandarino { 876f0fba2adSLiam Girdwood struct snd_soc_dai_link *dai_link = &card->dai_link[num]; 877f0fba2adSLiam Girdwood struct snd_soc_pcm_runtime *rtd = &card->rtd[num]; 87888bd870fSBenoit Cousson struct snd_soc_dai_link_component *codecs = dai_link->codecs; 87914621c7eSLars-Peter Clausen struct snd_soc_dai_link_component cpu_dai_component; 88088bd870fSBenoit Cousson struct snd_soc_dai **codec_dais = rtd->codec_dais; 881435c5e25SMark Brown struct snd_soc_platform *platform; 882848dd8beSMark Brown const char *platform_name; 88388bd870fSBenoit Cousson int i; 884db2a4165SFrank Mandarino 885f110bfc7SLiam Girdwood dev_dbg(card->dev, "ASoC: binding %s at idx %d\n", dai_link->name, num); 8866308419aSMark Brown 88714621c7eSLars-Peter Clausen cpu_dai_component.name = dai_link->cpu_name; 88814621c7eSLars-Peter Clausen cpu_dai_component.of_node = dai_link->cpu_of_node; 88914621c7eSLars-Peter Clausen cpu_dai_component.dai_name = dai_link->cpu_dai_name; 89014621c7eSLars-Peter Clausen rtd->cpu_dai = snd_soc_find_dai(&cpu_dai_component); 891b19e6e7bSMark Brown if (!rtd->cpu_dai) { 892f110bfc7SLiam Girdwood dev_err(card->dev, "ASoC: CPU DAI %s not registered\n", 893f0fba2adSLiam Girdwood dai_link->cpu_dai_name); 894b19e6e7bSMark Brown return -EPROBE_DEFER; 895c5af3a2eSMark Brown } 896c5af3a2eSMark Brown 89788bd870fSBenoit Cousson rtd->num_codecs = dai_link->num_codecs; 89888bd870fSBenoit Cousson 89988bd870fSBenoit Cousson /* Find CODEC from registered CODECs */ 90088bd870fSBenoit Cousson for (i = 0; i < rtd->num_codecs; i++) { 90114621c7eSLars-Peter Clausen codec_dais[i] = snd_soc_find_dai(&codecs[i]); 90288bd870fSBenoit Cousson if (!codec_dais[i]) { 90312023a9aSMisael Lopez Cruz dev_err(card->dev, "ASoC: CODEC DAI %s not registered\n", 90488bd870fSBenoit Cousson codecs[i].dai_name); 90512023a9aSMisael Lopez Cruz return -EPROBE_DEFER; 90612023a9aSMisael Lopez Cruz } 90788bd870fSBenoit Cousson } 90888bd870fSBenoit Cousson 90988bd870fSBenoit Cousson /* Single codec links expect codec and codec_dai in runtime data */ 91088bd870fSBenoit Cousson rtd->codec_dai = codec_dais[0]; 91188bd870fSBenoit Cousson rtd->codec = rtd->codec_dai->codec; 91212023a9aSMisael Lopez Cruz 913848dd8beSMark Brown /* if there's no platform we match on the empty platform */ 914848dd8beSMark Brown platform_name = dai_link->platform_name; 9155a504963SStephen Warren if (!platform_name && !dai_link->platform_of_node) 916848dd8beSMark Brown platform_name = "snd-soc-dummy"; 917848dd8beSMark Brown 918b19e6e7bSMark Brown /* find one from the set of registered platforms */ 919f0fba2adSLiam Girdwood list_for_each_entry(platform, &platform_list, list) { 9205a504963SStephen Warren if (dai_link->platform_of_node) { 9215a504963SStephen Warren if (platform->dev->of_node != 9225a504963SStephen Warren dai_link->platform_of_node) 9235a504963SStephen Warren continue; 9245a504963SStephen Warren } else { 925f4333203SLars-Peter Clausen if (strcmp(platform->component.name, platform_name)) 9262610ab77SStephen Warren continue; 9275a504963SStephen Warren } 9282610ab77SStephen Warren 929f0fba2adSLiam Girdwood rtd->platform = platform; 930f0fba2adSLiam Girdwood } 931b19e6e7bSMark Brown if (!rtd->platform) { 932f110bfc7SLiam Girdwood dev_err(card->dev, "ASoC: platform %s not registered\n", 933f0fba2adSLiam Girdwood dai_link->platform_name); 934b19e6e7bSMark Brown return -EPROBE_DEFER; 935f0fba2adSLiam Girdwood } 936b19e6e7bSMark Brown 937b19e6e7bSMark Brown card->num_rtd++; 938b19e6e7bSMark Brown 939b19e6e7bSMark Brown return 0; 9406b05eda6SMark Brown } 9416b05eda6SMark Brown 942f1d45cc3SLars-Peter Clausen static void soc_remove_component(struct snd_soc_component *component) 943d12cd198SStephen Warren { 94470090bbbSLars-Peter Clausen if (!component->probed) 94570090bbbSLars-Peter Clausen return; 946d12cd198SStephen Warren 947f1d45cc3SLars-Peter Clausen /* This is a HACK and will be removed soon */ 948f1d45cc3SLars-Peter Clausen if (component->codec) 949f1d45cc3SLars-Peter Clausen list_del(&component->codec->card_list); 950d12cd198SStephen Warren 951f1d45cc3SLars-Peter Clausen if (component->remove) 952f1d45cc3SLars-Peter Clausen component->remove(component); 953d12cd198SStephen Warren 954f1d45cc3SLars-Peter Clausen snd_soc_dapm_free(snd_soc_component_get_dapm(component)); 955d12cd198SStephen Warren 956f1d45cc3SLars-Peter Clausen soc_cleanup_component_debugfs(component); 957f1d45cc3SLars-Peter Clausen component->probed = 0; 958f1d45cc3SLars-Peter Clausen module_put(component->dev->driver->owner); 959d12cd198SStephen Warren } 960d12cd198SStephen Warren 961e60cd14fSLars-Peter Clausen static void soc_remove_dai(struct snd_soc_dai *dai, int order) 962589c3563SJarkko Nikula { 963589c3563SJarkko Nikula int err; 964589c3563SJarkko Nikula 965e60cd14fSLars-Peter Clausen if (dai && dai->probed && 966e60cd14fSLars-Peter Clausen dai->driver->remove_order == order) { 967e60cd14fSLars-Peter Clausen if (dai->driver->remove) { 968e60cd14fSLars-Peter Clausen err = dai->driver->remove(dai); 969589c3563SJarkko Nikula if (err < 0) 970e60cd14fSLars-Peter Clausen dev_err(dai->dev, 971b0aa88afSMisael Lopez Cruz "ASoC: failed to remove %s: %d\n", 972e60cd14fSLars-Peter Clausen dai->name, err); 973b0aa88afSMisael Lopez Cruz } 974e60cd14fSLars-Peter Clausen dai->probed = 0; 975b0aa88afSMisael Lopez Cruz } 976b0aa88afSMisael Lopez Cruz } 977b0aa88afSMisael Lopez Cruz 97862ae68faSStephen Warren static void soc_remove_link_dais(struct snd_soc_card *card, int num, int order) 979f0fba2adSLiam Girdwood { 980f0fba2adSLiam Girdwood struct snd_soc_pcm_runtime *rtd = &card->rtd[num]; 981e60cd14fSLars-Peter Clausen int i; 982f0fba2adSLiam Girdwood 983f0fba2adSLiam Girdwood /* unregister the rtd device */ 984f0fba2adSLiam Girdwood if (rtd->dev_registered) { 98536ae1a96SMark Brown device_unregister(rtd->dev); 986f0fba2adSLiam Girdwood rtd->dev_registered = 0; 98702a06d30SBarry Song } 98802a06d30SBarry Song 989f0fba2adSLiam Girdwood /* remove the CODEC DAI */ 99088bd870fSBenoit Cousson for (i = 0; i < rtd->num_codecs; i++) 991e60cd14fSLars-Peter Clausen soc_remove_dai(rtd->codec_dais[i], order); 992f0fba2adSLiam Girdwood 993e60cd14fSLars-Peter Clausen soc_remove_dai(rtd->cpu_dai, order); 994f0fba2adSLiam Girdwood } 995f0fba2adSLiam Girdwood 99662ae68faSStephen Warren static void soc_remove_link_components(struct snd_soc_card *card, int num, 99762ae68faSStephen Warren int order) 99862ae68faSStephen Warren { 99962ae68faSStephen Warren struct snd_soc_pcm_runtime *rtd = &card->rtd[num]; 100062ae68faSStephen Warren struct snd_soc_dai *cpu_dai = rtd->cpu_dai; 100162ae68faSStephen Warren struct snd_soc_platform *platform = rtd->platform; 100261aca564SLars-Peter Clausen struct snd_soc_component *component; 100388bd870fSBenoit Cousson int i; 100462ae68faSStephen Warren 100562ae68faSStephen Warren /* remove the platform */ 100670090bbbSLars-Peter Clausen if (platform && platform->component.driver->remove_order == order) 1007f1d45cc3SLars-Peter Clausen soc_remove_component(&platform->component); 100862ae68faSStephen Warren 100962ae68faSStephen Warren /* remove the CODEC-side CODEC */ 101088bd870fSBenoit Cousson for (i = 0; i < rtd->num_codecs; i++) { 101161aca564SLars-Peter Clausen component = rtd->codec_dais[i]->component; 101270090bbbSLars-Peter Clausen if (component->driver->remove_order == order) 101361aca564SLars-Peter Clausen soc_remove_component(component); 101462ae68faSStephen Warren } 101562ae68faSStephen Warren 101662ae68faSStephen Warren /* remove any CPU-side CODEC */ 101762ae68faSStephen Warren if (cpu_dai) { 101870090bbbSLars-Peter Clausen if (cpu_dai->component->driver->remove_order == order) 101961aca564SLars-Peter Clausen soc_remove_component(cpu_dai->component); 102062ae68faSStephen Warren } 102162ae68faSStephen Warren } 102262ae68faSStephen Warren 10230671fd8eSKuninori Morimoto static void soc_remove_dai_links(struct snd_soc_card *card) 10240671fd8eSKuninori Morimoto { 10250168bf0dSLiam Girdwood int dai, order; 10260671fd8eSKuninori Morimoto 10270168bf0dSLiam Girdwood for (order = SND_SOC_COMP_ORDER_FIRST; order <= SND_SOC_COMP_ORDER_LAST; 10280168bf0dSLiam Girdwood order++) { 10290168bf0dSLiam Girdwood for (dai = 0; dai < card->num_rtd; dai++) 103062ae68faSStephen Warren soc_remove_link_dais(card, dai, order); 10310168bf0dSLiam Girdwood } 103262ae68faSStephen Warren 103362ae68faSStephen Warren for (order = SND_SOC_COMP_ORDER_FIRST; order <= SND_SOC_COMP_ORDER_LAST; 103462ae68faSStephen Warren order++) { 103562ae68faSStephen Warren for (dai = 0; dai < card->num_rtd; dai++) 103662ae68faSStephen Warren soc_remove_link_components(card, dai, order); 103762ae68faSStephen Warren } 103862ae68faSStephen Warren 10390671fd8eSKuninori Morimoto card->num_rtd = 0; 10400671fd8eSKuninori Morimoto } 10410671fd8eSKuninori Morimoto 1042ead9b919SJarkko Nikula static void soc_set_name_prefix(struct snd_soc_card *card, 104394f99c87SLars-Peter Clausen struct snd_soc_component *component) 1044ead9b919SJarkko Nikula { 1045ead9b919SJarkko Nikula int i; 1046ead9b919SJarkko Nikula 1047ff819b83SDimitris Papastamos if (card->codec_conf == NULL) 1048ead9b919SJarkko Nikula return; 1049ead9b919SJarkko Nikula 1050ff819b83SDimitris Papastamos for (i = 0; i < card->num_configs; i++) { 1051ff819b83SDimitris Papastamos struct snd_soc_codec_conf *map = &card->codec_conf[i]; 105294f99c87SLars-Peter Clausen if (map->of_node && component->dev->of_node != map->of_node) 10533ca041edSSebastian Reichel continue; 105494f99c87SLars-Peter Clausen if (map->dev_name && strcmp(component->name, map->dev_name)) 10553ca041edSSebastian Reichel continue; 105694f99c87SLars-Peter Clausen component->name_prefix = map->name_prefix; 1057ead9b919SJarkko Nikula break; 1058ead9b919SJarkko Nikula } 1059ead9b919SJarkko Nikula } 1060ead9b919SJarkko Nikula 1061f1d45cc3SLars-Peter Clausen static int soc_probe_component(struct snd_soc_card *card, 1062f1d45cc3SLars-Peter Clausen struct snd_soc_component *component) 1063589c3563SJarkko Nikula { 1064f1d45cc3SLars-Peter Clausen struct snd_soc_dapm_context *dapm = snd_soc_component_get_dapm(component); 1065888df395SMark Brown struct snd_soc_dai *dai; 1066f1d45cc3SLars-Peter Clausen int ret; 1067589c3563SJarkko Nikula 106870090bbbSLars-Peter Clausen if (component->probed) 106970090bbbSLars-Peter Clausen return 0; 1070589c3563SJarkko Nikula 1071f1d45cc3SLars-Peter Clausen component->card = card; 1072f1d45cc3SLars-Peter Clausen dapm->card = card; 1073f1d45cc3SLars-Peter Clausen soc_set_name_prefix(card, component); 1074589c3563SJarkko Nikula 1075f1d45cc3SLars-Peter Clausen if (!try_module_get(component->dev->driver->owner)) 107670d29331SJarkko Nikula return -ENODEV; 107770d29331SJarkko Nikula 1078f1d45cc3SLars-Peter Clausen soc_init_component_debugfs(component); 1079d5d1e0beSLars-Peter Clausen 1080f1d45cc3SLars-Peter Clausen if (component->dapm_widgets) { 1081f1d45cc3SLars-Peter Clausen ret = snd_soc_dapm_new_controls(dapm, component->dapm_widgets, 1082f1d45cc3SLars-Peter Clausen component->num_dapm_widgets); 108377530150SLars-Peter Clausen 1084b318ad50SNariman Poushin if (ret != 0) { 1085f1d45cc3SLars-Peter Clausen dev_err(component->dev, 1086b318ad50SNariman Poushin "Failed to create new controls %d\n", ret); 1087b318ad50SNariman Poushin goto err_probe; 1088b318ad50SNariman Poushin } 1089b318ad50SNariman Poushin } 1090b318ad50SNariman Poushin 10910634814fSLars-Peter Clausen list_for_each_entry(dai, &component->dai_list, list) { 10920634814fSLars-Peter Clausen ret = snd_soc_dapm_new_dai_widgets(dapm, dai); 1093261edc70SNariman Poushin if (ret != 0) { 1094f1d45cc3SLars-Peter Clausen dev_err(component->dev, 1095261edc70SNariman Poushin "Failed to create DAI widgets %d\n", ret); 1096261edc70SNariman Poushin goto err_probe; 1097261edc70SNariman Poushin } 1098261edc70SNariman Poushin } 1099888df395SMark Brown 1100f1d45cc3SLars-Peter Clausen if (component->probe) { 1101f1d45cc3SLars-Peter Clausen ret = component->probe(component); 1102589c3563SJarkko Nikula if (ret < 0) { 1103f1d45cc3SLars-Peter Clausen dev_err(component->dev, 1104f1d45cc3SLars-Peter Clausen "ASoC: failed to probe component %d\n", ret); 110570d29331SJarkko Nikula goto err_probe; 1106589c3563SJarkko Nikula } 1107cb2cf612SLiam Girdwood 1108f1d45cc3SLars-Peter Clausen WARN(dapm->idle_bias_off && 1109f1d45cc3SLars-Peter Clausen dapm->bias_level != SND_SOC_BIAS_OFF, 1110956245e9SLiam Girdwood "codec %s can not start from non-off bias with idle_bias_off==1\n", 1111f1d45cc3SLars-Peter Clausen component->name); 1112956245e9SLiam Girdwood } 1113956245e9SLiam Girdwood 1114f1d45cc3SLars-Peter Clausen if (component->controls) 1115f1d45cc3SLars-Peter Clausen snd_soc_add_component_controls(component, component->controls, 1116f1d45cc3SLars-Peter Clausen component->num_controls); 1117f1d45cc3SLars-Peter Clausen if (component->dapm_routes) 1118f1d45cc3SLars-Peter Clausen snd_soc_dapm_add_routes(dapm, component->dapm_routes, 1119f1d45cc3SLars-Peter Clausen component->num_dapm_routes); 1120956245e9SLiam Girdwood 1121f1d45cc3SLars-Peter Clausen component->probed = 1; 1122f1d45cc3SLars-Peter Clausen list_add(&dapm->list, &card->dapm_list); 1123f1d45cc3SLars-Peter Clausen 1124f1d45cc3SLars-Peter Clausen /* This is a HACK and will be removed soon */ 1125f1d45cc3SLars-Peter Clausen if (component->codec) 1126f1d45cc3SLars-Peter Clausen list_add(&component->codec->card_list, &card->codec_dev_list); 1127956245e9SLiam Girdwood 1128956245e9SLiam Girdwood return 0; 1129956245e9SLiam Girdwood 1130956245e9SLiam Girdwood err_probe: 1131f1d45cc3SLars-Peter Clausen soc_cleanup_component_debugfs(component); 1132f1d45cc3SLars-Peter Clausen module_put(component->dev->driver->owner); 1133956245e9SLiam Girdwood 1134956245e9SLiam Girdwood return ret; 1135956245e9SLiam Girdwood } 1136956245e9SLiam Girdwood 113736ae1a96SMark Brown static void rtd_release(struct device *dev) 113836ae1a96SMark Brown { 113936ae1a96SMark Brown kfree(dev); 114036ae1a96SMark Brown } 1141f0fba2adSLiam Girdwood 11425f3484acSLars-Peter Clausen static int soc_post_component_init(struct snd_soc_pcm_runtime *rtd, 11435f3484acSLars-Peter Clausen const char *name) 1144503ae5e0SMisael Lopez Cruz { 1145589c3563SJarkko Nikula int ret = 0; 1146589c3563SJarkko Nikula 1147589c3563SJarkko Nikula /* register the rtd device */ 114836ae1a96SMark Brown rtd->dev = kzalloc(sizeof(struct device), GFP_KERNEL); 114936ae1a96SMark Brown if (!rtd->dev) 115036ae1a96SMark Brown return -ENOMEM; 115136ae1a96SMark Brown device_initialize(rtd->dev); 11525f3484acSLars-Peter Clausen rtd->dev->parent = rtd->card->dev; 115336ae1a96SMark Brown rtd->dev->release = rtd_release; 1154d29697dcSTakashi Iwai rtd->dev->groups = soc_dev_attr_groups; 1155f294afedSLars-Peter Clausen dev_set_name(rtd->dev, "%s", name); 115636ae1a96SMark Brown dev_set_drvdata(rtd->dev, rtd); 1157b8c0dab9SLiam Girdwood mutex_init(&rtd->pcm_mutex); 115801d7584cSLiam Girdwood INIT_LIST_HEAD(&rtd->dpcm[SNDRV_PCM_STREAM_PLAYBACK].be_clients); 115901d7584cSLiam Girdwood INIT_LIST_HEAD(&rtd->dpcm[SNDRV_PCM_STREAM_CAPTURE].be_clients); 116001d7584cSLiam Girdwood INIT_LIST_HEAD(&rtd->dpcm[SNDRV_PCM_STREAM_PLAYBACK].fe_clients); 116101d7584cSLiam Girdwood INIT_LIST_HEAD(&rtd->dpcm[SNDRV_PCM_STREAM_CAPTURE].fe_clients); 116236ae1a96SMark Brown ret = device_add(rtd->dev); 1163589c3563SJarkko Nikula if (ret < 0) { 1164865df9cbSChuansheng Liu /* calling put_device() here to free the rtd->dev */ 1165865df9cbSChuansheng Liu put_device(rtd->dev); 11665f3484acSLars-Peter Clausen dev_err(rtd->card->dev, 1167f110bfc7SLiam Girdwood "ASoC: failed to register runtime device: %d\n", ret); 1168589c3563SJarkko Nikula return ret; 1169589c3563SJarkko Nikula } 1170589c3563SJarkko Nikula rtd->dev_registered = 1; 1171589c3563SJarkko Nikula return 0; 1172589c3563SJarkko Nikula } 1173589c3563SJarkko Nikula 117462ae68faSStephen Warren static int soc_probe_link_components(struct snd_soc_card *card, int num, 117562ae68faSStephen Warren int order) 117662ae68faSStephen Warren { 117762ae68faSStephen Warren struct snd_soc_pcm_runtime *rtd = &card->rtd[num]; 117862ae68faSStephen Warren struct snd_soc_platform *platform = rtd->platform; 1179f1d45cc3SLars-Peter Clausen struct snd_soc_component *component; 118088bd870fSBenoit Cousson int i, ret; 118162ae68faSStephen Warren 118262ae68faSStephen Warren /* probe the CPU-side component, if it is a CODEC */ 118361aca564SLars-Peter Clausen component = rtd->cpu_dai->component; 118470090bbbSLars-Peter Clausen if (component->driver->probe_order == order) { 1185f1d45cc3SLars-Peter Clausen ret = soc_probe_component(card, component); 118662ae68faSStephen Warren if (ret < 0) 118762ae68faSStephen Warren return ret; 118862ae68faSStephen Warren } 118962ae68faSStephen Warren 119088bd870fSBenoit Cousson /* probe the CODEC-side components */ 119188bd870fSBenoit Cousson for (i = 0; i < rtd->num_codecs; i++) { 119261aca564SLars-Peter Clausen component = rtd->codec_dais[i]->component; 119370090bbbSLars-Peter Clausen if (component->driver->probe_order == order) { 1194f1d45cc3SLars-Peter Clausen ret = soc_probe_component(card, component); 119562ae68faSStephen Warren if (ret < 0) 119662ae68faSStephen Warren return ret; 119762ae68faSStephen Warren } 119888bd870fSBenoit Cousson } 119962ae68faSStephen Warren 120062ae68faSStephen Warren /* probe the platform */ 120170090bbbSLars-Peter Clausen if (platform->component.driver->probe_order == order) { 1202f1d45cc3SLars-Peter Clausen ret = soc_probe_component(card, &platform->component); 120362ae68faSStephen Warren if (ret < 0) 120462ae68faSStephen Warren return ret; 120562ae68faSStephen Warren } 120662ae68faSStephen Warren 120762ae68faSStephen Warren return 0; 120862ae68faSStephen Warren } 120962ae68faSStephen Warren 12108e2be562SLars-Peter Clausen static int soc_probe_dai(struct snd_soc_dai *dai, int order) 1211b0aa88afSMisael Lopez Cruz { 1212b0aa88afSMisael Lopez Cruz int ret; 1213b0aa88afSMisael Lopez Cruz 12148e2be562SLars-Peter Clausen if (!dai->probed && dai->driver->probe_order == order) { 12158e2be562SLars-Peter Clausen if (dai->driver->probe) { 12168e2be562SLars-Peter Clausen ret = dai->driver->probe(dai); 1217b0aa88afSMisael Lopez Cruz if (ret < 0) { 12188e2be562SLars-Peter Clausen dev_err(dai->dev, 12198e2be562SLars-Peter Clausen "ASoC: failed to probe DAI %s: %d\n", 12208e2be562SLars-Peter Clausen dai->name, ret); 1221b0aa88afSMisael Lopez Cruz return ret; 1222b0aa88afSMisael Lopez Cruz } 1223b0aa88afSMisael Lopez Cruz } 1224b0aa88afSMisael Lopez Cruz 12258e2be562SLars-Peter Clausen dai->probed = 1; 1226b0aa88afSMisael Lopez Cruz } 1227b0aa88afSMisael Lopez Cruz 1228b0aa88afSMisael Lopez Cruz return 0; 1229b0aa88afSMisael Lopez Cruz } 1230b0aa88afSMisael Lopez Cruz 12312436a723SMisael Lopez Cruz static int soc_link_dai_widgets(struct snd_soc_card *card, 12322436a723SMisael Lopez Cruz struct snd_soc_dai_link *dai_link, 12333f901a02SBenoit Cousson struct snd_soc_pcm_runtime *rtd) 12342436a723SMisael Lopez Cruz { 12353f901a02SBenoit Cousson struct snd_soc_dai *cpu_dai = rtd->cpu_dai; 12363f901a02SBenoit Cousson struct snd_soc_dai *codec_dai = rtd->codec_dai; 12372436a723SMisael Lopez Cruz struct snd_soc_dapm_widget *play_w, *capture_w; 12382436a723SMisael Lopez Cruz int ret; 12392436a723SMisael Lopez Cruz 124088bd870fSBenoit Cousson if (rtd->num_codecs > 1) 124188bd870fSBenoit Cousson dev_warn(card->dev, "ASoC: Multiple codecs not supported yet\n"); 124288bd870fSBenoit Cousson 12432436a723SMisael Lopez Cruz /* link the DAI widgets */ 12442436a723SMisael Lopez Cruz play_w = codec_dai->playback_widget; 12452436a723SMisael Lopez Cruz capture_w = cpu_dai->capture_widget; 12462436a723SMisael Lopez Cruz if (play_w && capture_w) { 12472436a723SMisael Lopez Cruz ret = snd_soc_dapm_new_pcm(card, dai_link->params, 12482436a723SMisael Lopez Cruz capture_w, play_w); 12492436a723SMisael Lopez Cruz if (ret != 0) { 12502436a723SMisael Lopez Cruz dev_err(card->dev, "ASoC: Can't link %s to %s: %d\n", 12512436a723SMisael Lopez Cruz play_w->name, capture_w->name, ret); 12522436a723SMisael Lopez Cruz return ret; 12532436a723SMisael Lopez Cruz } 12542436a723SMisael Lopez Cruz } 12552436a723SMisael Lopez Cruz 12562436a723SMisael Lopez Cruz play_w = cpu_dai->playback_widget; 12572436a723SMisael Lopez Cruz capture_w = codec_dai->capture_widget; 12582436a723SMisael Lopez Cruz if (play_w && capture_w) { 12592436a723SMisael Lopez Cruz ret = snd_soc_dapm_new_pcm(card, dai_link->params, 12602436a723SMisael Lopez Cruz capture_w, play_w); 12612436a723SMisael Lopez Cruz if (ret != 0) { 12622436a723SMisael Lopez Cruz dev_err(card->dev, "ASoC: Can't link %s to %s: %d\n", 12632436a723SMisael Lopez Cruz play_w->name, capture_w->name, ret); 12642436a723SMisael Lopez Cruz return ret; 12652436a723SMisael Lopez Cruz } 12662436a723SMisael Lopez Cruz } 12672436a723SMisael Lopez Cruz 12682436a723SMisael Lopez Cruz return 0; 12692436a723SMisael Lopez Cruz } 12702436a723SMisael Lopez Cruz 127162ae68faSStephen Warren static int soc_probe_link_dais(struct snd_soc_card *card, int num, int order) 1272f0fba2adSLiam Girdwood { 1273f0fba2adSLiam Girdwood struct snd_soc_dai_link *dai_link = &card->dai_link[num]; 1274f0fba2adSLiam Girdwood struct snd_soc_pcm_runtime *rtd = &card->rtd[num]; 1275c74184edSMark Brown struct snd_soc_dai *cpu_dai = rtd->cpu_dai; 127688bd870fSBenoit Cousson int i, ret; 1277f0fba2adSLiam Girdwood 1278f110bfc7SLiam Girdwood dev_dbg(card->dev, "ASoC: probe %s dai link %d late %d\n", 12790168bf0dSLiam Girdwood card->name, num, order); 1280f0fba2adSLiam Girdwood 1281f0fba2adSLiam Girdwood /* set default power off timeout */ 1282f0fba2adSLiam Girdwood rtd->pmdown_time = pmdown_time; 1283f0fba2adSLiam Girdwood 12848e2be562SLars-Peter Clausen ret = soc_probe_dai(cpu_dai, order); 12858e2be562SLars-Peter Clausen if (ret) 1286f0fba2adSLiam Girdwood return ret; 1287f0fba2adSLiam Girdwood 1288f0fba2adSLiam Girdwood /* probe the CODEC DAI */ 128988bd870fSBenoit Cousson for (i = 0; i < rtd->num_codecs; i++) { 12908e2be562SLars-Peter Clausen ret = soc_probe_dai(rtd->codec_dais[i], order); 1291b0aa88afSMisael Lopez Cruz if (ret) 1292f0fba2adSLiam Girdwood return ret; 129388bd870fSBenoit Cousson } 1294f0fba2adSLiam Girdwood 12950168bf0dSLiam Girdwood /* complete DAI probe during last probe */ 12960168bf0dSLiam Girdwood if (order != SND_SOC_COMP_ORDER_LAST) 12970168bf0dSLiam Girdwood return 0; 12980168bf0dSLiam Girdwood 12995f3484acSLars-Peter Clausen /* do machine specific initialization */ 13005f3484acSLars-Peter Clausen if (dai_link->init) { 13015f3484acSLars-Peter Clausen ret = dai_link->init(rtd); 13025f3484acSLars-Peter Clausen if (ret < 0) { 13035f3484acSLars-Peter Clausen dev_err(card->dev, "ASoC: failed to init %s: %d\n", 13045f3484acSLars-Peter Clausen dai_link->name, ret); 13055f3484acSLars-Peter Clausen return ret; 13065f3484acSLars-Peter Clausen } 13075f3484acSLars-Peter Clausen } 13085f3484acSLars-Peter Clausen 13095f3484acSLars-Peter Clausen ret = soc_post_component_init(rtd, dai_link->name); 1310589c3563SJarkko Nikula if (ret) 1311f0fba2adSLiam Girdwood return ret; 1312f0fba2adSLiam Girdwood 13135f3484acSLars-Peter Clausen #ifdef CONFIG_DEBUG_FS 13145f3484acSLars-Peter Clausen /* add DPCM sysfs entries */ 13155f3484acSLars-Peter Clausen if (dai_link->dynamic) { 13165f3484acSLars-Peter Clausen ret = soc_dpcm_debugfs_add(rtd); 13175f3484acSLars-Peter Clausen if (ret < 0) { 13185f3484acSLars-Peter Clausen dev_err(rtd->dev, 13195f3484acSLars-Peter Clausen "ASoC: failed to add dpcm sysfs entries: %d\n", 13205f3484acSLars-Peter Clausen ret); 13215f3484acSLars-Peter Clausen return ret; 13225f3484acSLars-Peter Clausen } 13235f3484acSLars-Peter Clausen } 13245f3484acSLars-Peter Clausen #endif 13255f3484acSLars-Peter Clausen 13261245b700SNamarta Kohli if (cpu_dai->driver->compress_dai) { 13271245b700SNamarta Kohli /*create compress_device"*/ 13281245b700SNamarta Kohli ret = soc_new_compress(rtd, num); 13291245b700SNamarta Kohli if (ret < 0) { 1330f110bfc7SLiam Girdwood dev_err(card->dev, "ASoC: can't create compress %s\n", 13311245b700SNamarta Kohli dai_link->stream_name); 13321245b700SNamarta Kohli return ret; 13331245b700SNamarta Kohli } 13341245b700SNamarta Kohli } else { 13351245b700SNamarta Kohli 1336c74184edSMark Brown if (!dai_link->params) { 1337f0fba2adSLiam Girdwood /* create the pcm */ 1338f0fba2adSLiam Girdwood ret = soc_new_pcm(rtd, num); 1339f0fba2adSLiam Girdwood if (ret < 0) { 1340f110bfc7SLiam Girdwood dev_err(card->dev, "ASoC: can't create pcm %s :%d\n", 13410837fc62SFabio Estevam dai_link->stream_name, ret); 1342f0fba2adSLiam Girdwood return ret; 1343f0fba2adSLiam Girdwood } 1344c74184edSMark Brown } else { 13459d58a077SRichard Fitzgerald INIT_DELAYED_WORK(&rtd->delayed_work, 13469d58a077SRichard Fitzgerald codec2codec_close_delayed_work); 13479d58a077SRichard Fitzgerald 1348c74184edSMark Brown /* link the DAI widgets */ 13493f901a02SBenoit Cousson ret = soc_link_dai_widgets(card, dai_link, rtd); 13502436a723SMisael Lopez Cruz if (ret) 1351c74184edSMark Brown return ret; 1352c74184edSMark Brown } 1353c74184edSMark Brown } 1354c74184edSMark Brown 1355f0fba2adSLiam Girdwood return 0; 1356f0fba2adSLiam Girdwood } 1357f0fba2adSLiam Girdwood 135844c69bb1SLars-Peter Clausen static int soc_bind_aux_dev(struct snd_soc_card *card, int num) 1359b19e6e7bSMark Brown { 136044c69bb1SLars-Peter Clausen struct snd_soc_pcm_runtime *rtd = &card->rtd_aux[num]; 13613ca041edSSebastian Reichel struct snd_soc_aux_dev *aux_dev = &card->aux_dev[num]; 136265d9361fSLars-Peter Clausen const char *name = aux_dev->codec_name; 13633ca041edSSebastian Reichel 136465d9361fSLars-Peter Clausen rtd->component = soc_find_component(aux_dev->codec_of_node, name); 136565d9361fSLars-Peter Clausen if (!rtd->component) { 13663ca041edSSebastian Reichel if (aux_dev->codec_of_node) 136765d9361fSLars-Peter Clausen name = of_node_full_name(aux_dev->codec_of_node); 13683ca041edSSebastian Reichel 136965d9361fSLars-Peter Clausen dev_err(card->dev, "ASoC: %s not registered\n", name); 1370b19e6e7bSMark Brown return -EPROBE_DEFER; 1371b19e6e7bSMark Brown } 1372b19e6e7bSMark Brown 137365d9361fSLars-Peter Clausen /* 137465d9361fSLars-Peter Clausen * Some places still reference rtd->codec, so we have to keep that 137565d9361fSLars-Peter Clausen * initialized if the component is a CODEC. Once all those references 137665d9361fSLars-Peter Clausen * have been removed, this code can be removed as well. 137765d9361fSLars-Peter Clausen */ 137865d9361fSLars-Peter Clausen rtd->codec = rtd->component->codec; 137965d9361fSLars-Peter Clausen 138044c69bb1SLars-Peter Clausen return 0; 13813ca041edSSebastian Reichel } 13822eea392dSJarkko Nikula 138344c69bb1SLars-Peter Clausen static int soc_probe_aux_dev(struct snd_soc_card *card, int num) 138444c69bb1SLars-Peter Clausen { 138544c69bb1SLars-Peter Clausen struct snd_soc_pcm_runtime *rtd = &card->rtd_aux[num]; 13865f3484acSLars-Peter Clausen struct snd_soc_aux_dev *aux_dev = &card->aux_dev[num]; 138744c69bb1SLars-Peter Clausen int ret; 138844c69bb1SLars-Peter Clausen 138965d9361fSLars-Peter Clausen ret = soc_probe_component(card, rtd->component); 13902eea392dSJarkko Nikula if (ret < 0) 1391589c3563SJarkko Nikula return ret; 13922eea392dSJarkko Nikula 13935f3484acSLars-Peter Clausen /* do machine specific initialization */ 13945f3484acSLars-Peter Clausen if (aux_dev->init) { 139557bf7726SLars-Peter Clausen ret = aux_dev->init(rtd->component); 13965f3484acSLars-Peter Clausen if (ret < 0) { 13975f3484acSLars-Peter Clausen dev_err(card->dev, "ASoC: failed to init %s: %d\n", 13985f3484acSLars-Peter Clausen aux_dev->name, ret); 13992eea392dSJarkko Nikula return ret; 14002eea392dSJarkko Nikula } 14015f3484acSLars-Peter Clausen } 14025f3484acSLars-Peter Clausen 14035f3484acSLars-Peter Clausen return soc_post_component_init(rtd, aux_dev->name); 14042eea392dSJarkko Nikula } 14052eea392dSJarkko Nikula 14062eea392dSJarkko Nikula static void soc_remove_aux_dev(struct snd_soc_card *card, int num) 14072eea392dSJarkko Nikula { 14082eea392dSJarkko Nikula struct snd_soc_pcm_runtime *rtd = &card->rtd_aux[num]; 140965d9361fSLars-Peter Clausen struct snd_soc_component *component = rtd->component; 14102eea392dSJarkko Nikula 14112eea392dSJarkko Nikula /* unregister the rtd device */ 14122eea392dSJarkko Nikula if (rtd->dev_registered) { 141336ae1a96SMark Brown device_remove_file(rtd->dev, &dev_attr_codec_reg); 1414d3bf1561SChuansheng Liu device_unregister(rtd->dev); 14152eea392dSJarkko Nikula rtd->dev_registered = 0; 14162eea392dSJarkko Nikula } 14172eea392dSJarkko Nikula 141865d9361fSLars-Peter Clausen if (component && component->probed) 141965d9361fSLars-Peter Clausen soc_remove_component(component); 14202eea392dSJarkko Nikula } 14212eea392dSJarkko Nikula 1422f90fb3f7SLars-Peter Clausen static int snd_soc_init_codec_cache(struct snd_soc_codec *codec) 1423fdf0f54dSDimitris Papastamos { 1424fdf0f54dSDimitris Papastamos int ret; 1425fdf0f54dSDimitris Papastamos 1426fdf0f54dSDimitris Papastamos if (codec->cache_init) 1427fdf0f54dSDimitris Papastamos return 0; 1428fdf0f54dSDimitris Papastamos 1429fdf0f54dSDimitris Papastamos ret = snd_soc_cache_init(codec); 1430fdf0f54dSDimitris Papastamos if (ret < 0) { 143110e8aa9aSMichał Mirosław dev_err(codec->dev, 143210e8aa9aSMichał Mirosław "ASoC: Failed to set cache compression type: %d\n", 143310e8aa9aSMichał Mirosław ret); 1434fdf0f54dSDimitris Papastamos return ret; 1435fdf0f54dSDimitris Papastamos } 1436fdf0f54dSDimitris Papastamos codec->cache_init = 1; 1437fdf0f54dSDimitris Papastamos return 0; 1438fdf0f54dSDimitris Papastamos } 1439fdf0f54dSDimitris Papastamos 1440b19e6e7bSMark Brown static int snd_soc_instantiate_card(struct snd_soc_card *card) 1441f0fba2adSLiam Girdwood { 1442fdf0f54dSDimitris Papastamos struct snd_soc_codec *codec; 144375d9ac46SMark Brown struct snd_soc_dai_link *dai_link; 1444f04209a7SMark Brown int ret, i, order, dai_fmt; 144596dd3622SMark Brown 144601b9d99aSLiam Girdwood mutex_lock_nested(&card->mutex, SND_SOC_CARD_CLASS_INIT); 1447f0fba2adSLiam Girdwood 1448b19e6e7bSMark Brown /* bind DAIs */ 1449b19e6e7bSMark Brown for (i = 0; i < card->num_links; i++) { 1450b19e6e7bSMark Brown ret = soc_bind_dai_link(card, i); 1451b19e6e7bSMark Brown if (ret != 0) 1452b19e6e7bSMark Brown goto base_error; 1453db2a4165SFrank Mandarino } 1454db2a4165SFrank Mandarino 145544c69bb1SLars-Peter Clausen /* bind aux_devs too */ 1456b19e6e7bSMark Brown for (i = 0; i < card->num_aux_devs; i++) { 145744c69bb1SLars-Peter Clausen ret = soc_bind_aux_dev(card, i); 1458b19e6e7bSMark Brown if (ret != 0) 1459b19e6e7bSMark Brown goto base_error; 1460db2a4165SFrank Mandarino } 1461db2a4165SFrank Mandarino 1462fdf0f54dSDimitris Papastamos /* initialize the register cache for each available codec */ 1463fdf0f54dSDimitris Papastamos list_for_each_entry(codec, &codec_list, list) { 1464fdf0f54dSDimitris Papastamos if (codec->cache_init) 1465fdf0f54dSDimitris Papastamos continue; 1466f90fb3f7SLars-Peter Clausen ret = snd_soc_init_codec_cache(codec); 1467b19e6e7bSMark Brown if (ret < 0) 1468b19e6e7bSMark Brown goto base_error; 1469fdf0f54dSDimitris Papastamos } 1470fdf0f54dSDimitris Papastamos 1471f0fba2adSLiam Girdwood /* card bind complete so register a sound card */ 1472102b5a8dSTakashi Iwai ret = snd_card_new(card->dev, SNDRV_DEFAULT_IDX1, SNDRV_DEFAULT_STR1, 1473f0fba2adSLiam Girdwood card->owner, 0, &card->snd_card); 1474f0fba2adSLiam Girdwood if (ret < 0) { 147510e8aa9aSMichał Mirosław dev_err(card->dev, 147610e8aa9aSMichał Mirosław "ASoC: can't create sound card for card %s: %d\n", 147710e8aa9aSMichał Mirosław card->name, ret); 1478b19e6e7bSMark Brown goto base_error; 1479db2a4165SFrank Mandarino } 1480db2a4165SFrank Mandarino 1481e37a4970SMark Brown card->dapm.bias_level = SND_SOC_BIAS_OFF; 1482e37a4970SMark Brown card->dapm.dev = card->dev; 1483e37a4970SMark Brown card->dapm.card = card; 1484e37a4970SMark Brown list_add(&card->dapm.list, &card->dapm_list); 1485e37a4970SMark Brown 1486d5d1e0beSLars-Peter Clausen #ifdef CONFIG_DEBUG_FS 1487d5d1e0beSLars-Peter Clausen snd_soc_dapm_debugfs_init(&card->dapm, card->debugfs_card_root); 1488d5d1e0beSLars-Peter Clausen #endif 1489d5d1e0beSLars-Peter Clausen 149088ee1c61SMark Brown #ifdef CONFIG_PM_SLEEP 14916ed25978SAndy Green /* deferred resume work */ 14926308419aSMark Brown INIT_WORK(&card->deferred_resume_work, soc_resume_deferred); 14931301a964SRandy Dunlap #endif 14946ed25978SAndy Green 14959a841ebbSMark Brown if (card->dapm_widgets) 14969a841ebbSMark Brown snd_soc_dapm_new_controls(&card->dapm, card->dapm_widgets, 14979a841ebbSMark Brown card->num_dapm_widgets); 14989a841ebbSMark Brown 1499f0fba2adSLiam Girdwood /* initialise the sound card only once */ 1500f0fba2adSLiam Girdwood if (card->probe) { 1501e7361ec4SMark Brown ret = card->probe(card); 1502f0fba2adSLiam Girdwood if (ret < 0) 1503f0fba2adSLiam Girdwood goto card_probe_error; 1504f0fba2adSLiam Girdwood } 1505f0fba2adSLiam Girdwood 150662ae68faSStephen Warren /* probe all components used by DAI links on this card */ 15070168bf0dSLiam Girdwood for (order = SND_SOC_COMP_ORDER_FIRST; order <= SND_SOC_COMP_ORDER_LAST; 15080168bf0dSLiam Girdwood order++) { 1509fe3e78e0SMark Brown for (i = 0; i < card->num_links; i++) { 151062ae68faSStephen Warren ret = soc_probe_link_components(card, i, order); 151162ae68faSStephen Warren if (ret < 0) { 1512f110bfc7SLiam Girdwood dev_err(card->dev, 1513f110bfc7SLiam Girdwood "ASoC: failed to instantiate card %d\n", 1514f110bfc7SLiam Girdwood ret); 151562ae68faSStephen Warren goto probe_dai_err; 151662ae68faSStephen Warren } 151762ae68faSStephen Warren } 151862ae68faSStephen Warren } 151962ae68faSStephen Warren 152062ae68faSStephen Warren /* probe all DAI links on this card */ 152162ae68faSStephen Warren for (order = SND_SOC_COMP_ORDER_FIRST; order <= SND_SOC_COMP_ORDER_LAST; 152262ae68faSStephen Warren order++) { 152362ae68faSStephen Warren for (i = 0; i < card->num_links; i++) { 152462ae68faSStephen Warren ret = soc_probe_link_dais(card, i, order); 1525fe3e78e0SMark Brown if (ret < 0) { 1526f110bfc7SLiam Girdwood dev_err(card->dev, 1527f110bfc7SLiam Girdwood "ASoC: failed to instantiate card %d\n", 1528f110bfc7SLiam Girdwood ret); 1529f0fba2adSLiam Girdwood goto probe_dai_err; 1530fe3e78e0SMark Brown } 1531fe3e78e0SMark Brown } 15320168bf0dSLiam Girdwood } 1533fe3e78e0SMark Brown 15342eea392dSJarkko Nikula for (i = 0; i < card->num_aux_devs; i++) { 15352eea392dSJarkko Nikula ret = soc_probe_aux_dev(card, i); 15362eea392dSJarkko Nikula if (ret < 0) { 1537f110bfc7SLiam Girdwood dev_err(card->dev, 1538f110bfc7SLiam Girdwood "ASoC: failed to add auxiliary devices %d\n", 1539f110bfc7SLiam Girdwood ret); 15402eea392dSJarkko Nikula goto probe_aux_dev_err; 15412eea392dSJarkko Nikula } 15422eea392dSJarkko Nikula } 15432eea392dSJarkko Nikula 1544888df395SMark Brown snd_soc_dapm_link_dai_widgets(card); 1545b893ea5fSLiam Girdwood snd_soc_dapm_connect_dai_link_widgets(card); 1546888df395SMark Brown 1547b7af1dafSMark Brown if (card->controls) 1548022658beSLiam Girdwood snd_soc_add_card_controls(card, card->controls, card->num_controls); 1549b7af1dafSMark Brown 1550b8ad29deSMark Brown if (card->dapm_routes) 1551b8ad29deSMark Brown snd_soc_dapm_add_routes(&card->dapm, card->dapm_routes, 1552b8ad29deSMark Brown card->num_dapm_routes); 1553b8ad29deSMark Brown 155475d9ac46SMark Brown for (i = 0; i < card->num_links; i++) { 155588bd870fSBenoit Cousson struct snd_soc_pcm_runtime *rtd = &card->rtd[i]; 155675d9ac46SMark Brown dai_link = &card->dai_link[i]; 1557f04209a7SMark Brown dai_fmt = dai_link->dai_fmt; 155875d9ac46SMark Brown 1559f04209a7SMark Brown if (dai_fmt) { 156088bd870fSBenoit Cousson struct snd_soc_dai **codec_dais = rtd->codec_dais; 156188bd870fSBenoit Cousson int j; 156288bd870fSBenoit Cousson 156388bd870fSBenoit Cousson for (j = 0; j < rtd->num_codecs; j++) { 156488bd870fSBenoit Cousson struct snd_soc_dai *codec_dai = codec_dais[j]; 156588bd870fSBenoit Cousson 156688bd870fSBenoit Cousson ret = snd_soc_dai_set_fmt(codec_dai, dai_fmt); 15675e4ba569SShawn Guo if (ret != 0 && ret != -ENOTSUPP) 156888bd870fSBenoit Cousson dev_warn(codec_dai->dev, 1569f110bfc7SLiam Girdwood "ASoC: Failed to set DAI format: %d\n", 157075d9ac46SMark Brown ret); 1571f04209a7SMark Brown } 157288bd870fSBenoit Cousson } 1573f04209a7SMark Brown 1574f04209a7SMark Brown /* If this is a regular CPU link there will be a platform */ 1575fe33d4c5SStephen Warren if (dai_fmt && 1576fe33d4c5SStephen Warren (dai_link->platform_name || dai_link->platform_of_node)) { 1577f04209a7SMark Brown ret = snd_soc_dai_set_fmt(card->rtd[i].cpu_dai, 1578f04209a7SMark Brown dai_fmt); 1579f04209a7SMark Brown if (ret != 0 && ret != -ENOTSUPP) 1580f04209a7SMark Brown dev_warn(card->rtd[i].cpu_dai->dev, 1581f110bfc7SLiam Girdwood "ASoC: Failed to set DAI format: %d\n", 1582f04209a7SMark Brown ret); 1583f04209a7SMark Brown } else if (dai_fmt) { 1584f04209a7SMark Brown /* Flip the polarity for the "CPU" end */ 1585f04209a7SMark Brown dai_fmt &= ~SND_SOC_DAIFMT_MASTER_MASK; 1586f04209a7SMark Brown switch (dai_link->dai_fmt & 1587f04209a7SMark Brown SND_SOC_DAIFMT_MASTER_MASK) { 1588f04209a7SMark Brown case SND_SOC_DAIFMT_CBM_CFM: 1589f04209a7SMark Brown dai_fmt |= SND_SOC_DAIFMT_CBS_CFS; 1590f04209a7SMark Brown break; 1591f04209a7SMark Brown case SND_SOC_DAIFMT_CBM_CFS: 1592f04209a7SMark Brown dai_fmt |= SND_SOC_DAIFMT_CBS_CFM; 1593f04209a7SMark Brown break; 1594f04209a7SMark Brown case SND_SOC_DAIFMT_CBS_CFM: 1595f04209a7SMark Brown dai_fmt |= SND_SOC_DAIFMT_CBM_CFS; 1596f04209a7SMark Brown break; 1597f04209a7SMark Brown case SND_SOC_DAIFMT_CBS_CFS: 1598f04209a7SMark Brown dai_fmt |= SND_SOC_DAIFMT_CBM_CFM; 1599f04209a7SMark Brown break; 1600f04209a7SMark Brown } 160175d9ac46SMark Brown 160275d9ac46SMark Brown ret = snd_soc_dai_set_fmt(card->rtd[i].cpu_dai, 1603f04209a7SMark Brown dai_fmt); 16045e4ba569SShawn Guo if (ret != 0 && ret != -ENOTSUPP) 160575d9ac46SMark Brown dev_warn(card->rtd[i].cpu_dai->dev, 1606f110bfc7SLiam Girdwood "ASoC: Failed to set DAI format: %d\n", 160775d9ac46SMark Brown ret); 160875d9ac46SMark Brown } 160975d9ac46SMark Brown } 161075d9ac46SMark Brown 1611f0fba2adSLiam Girdwood snprintf(card->snd_card->shortname, sizeof(card->snd_card->shortname), 1612fe3e78e0SMark Brown "%s", card->name); 1613f0fba2adSLiam Girdwood snprintf(card->snd_card->longname, sizeof(card->snd_card->longname), 161422de71baSLiam Girdwood "%s", card->long_name ? card->long_name : card->name); 1615f0e8ed85SMark Brown snprintf(card->snd_card->driver, sizeof(card->snd_card->driver), 1616f0e8ed85SMark Brown "%s", card->driver_name ? card->driver_name : card->name); 1617f0e8ed85SMark Brown for (i = 0; i < ARRAY_SIZE(card->snd_card->driver); i++) { 1618f0e8ed85SMark Brown switch (card->snd_card->driver[i]) { 1619f0e8ed85SMark Brown case '_': 1620f0e8ed85SMark Brown case '-': 1621f0e8ed85SMark Brown case '\0': 1622f0e8ed85SMark Brown break; 1623f0e8ed85SMark Brown default: 1624f0e8ed85SMark Brown if (!isalnum(card->snd_card->driver[i])) 1625f0e8ed85SMark Brown card->snd_card->driver[i] = '_'; 1626f0e8ed85SMark Brown break; 1627f0e8ed85SMark Brown } 1628f0e8ed85SMark Brown } 1629fe3e78e0SMark Brown 163028e9ad92SMark Brown if (card->late_probe) { 163128e9ad92SMark Brown ret = card->late_probe(card); 163228e9ad92SMark Brown if (ret < 0) { 1633f110bfc7SLiam Girdwood dev_err(card->dev, "ASoC: %s late_probe() failed: %d\n", 163428e9ad92SMark Brown card->name, ret); 163528e9ad92SMark Brown goto probe_aux_dev_err; 163628e9ad92SMark Brown } 163728e9ad92SMark Brown } 163828e9ad92SMark Brown 16391633281bSStephen Warren if (card->fully_routed) 16407df37884SLars-Peter Clausen snd_soc_dapm_auto_nc_pins(card); 16411633281bSStephen Warren 1642824ef826SLars-Peter Clausen snd_soc_dapm_new_widgets(card); 16438c193b8dSLars-Peter Clausen 1644f0fba2adSLiam Girdwood ret = snd_card_register(card->snd_card); 1645fe3e78e0SMark Brown if (ret < 0) { 1646f110bfc7SLiam Girdwood dev_err(card->dev, "ASoC: failed to register soundcard %d\n", 1647f110bfc7SLiam Girdwood ret); 16486b3ed785SAxel Lin goto probe_aux_dev_err; 1649fe3e78e0SMark Brown } 1650fe3e78e0SMark Brown 1651435c5e25SMark Brown card->instantiated = 1; 16524f4c0072SMark Brown snd_soc_dapm_sync(&card->dapm); 1653f0fba2adSLiam Girdwood mutex_unlock(&card->mutex); 1654b19e6e7bSMark Brown 1655b19e6e7bSMark Brown return 0; 1656db2a4165SFrank Mandarino 16572eea392dSJarkko Nikula probe_aux_dev_err: 16582eea392dSJarkko Nikula for (i = 0; i < card->num_aux_devs; i++) 16592eea392dSJarkko Nikula soc_remove_aux_dev(card, i); 16602eea392dSJarkko Nikula 1661f0fba2adSLiam Girdwood probe_dai_err: 16620671fd8eSKuninori Morimoto soc_remove_dai_links(card); 1663fe3e78e0SMark Brown 1664f0fba2adSLiam Girdwood card_probe_error: 166587506549SMark Brown if (card->remove) 1666e7361ec4SMark Brown card->remove(card); 1667f0fba2adSLiam Girdwood 1668f0fba2adSLiam Girdwood snd_card_free(card->snd_card); 1669f0fba2adSLiam Girdwood 1670b19e6e7bSMark Brown base_error: 1671f0fba2adSLiam Girdwood mutex_unlock(&card->mutex); 1672db2a4165SFrank Mandarino 1673b19e6e7bSMark Brown return ret; 1674435c5e25SMark Brown } 1675435c5e25SMark Brown 1676435c5e25SMark Brown /* probes a new socdev */ 1677435c5e25SMark Brown static int soc_probe(struct platform_device *pdev) 1678435c5e25SMark Brown { 1679f0fba2adSLiam Girdwood struct snd_soc_card *card = platform_get_drvdata(pdev); 1680435c5e25SMark Brown 168170a7ca34SVinod Koul /* 168270a7ca34SVinod Koul * no card, so machine driver should be registering card 168370a7ca34SVinod Koul * we should not be here in that case so ret error 168470a7ca34SVinod Koul */ 168570a7ca34SVinod Koul if (!card) 168670a7ca34SVinod Koul return -EINVAL; 168770a7ca34SVinod Koul 1688fe4085e8SMark Brown dev_warn(&pdev->dev, 1689f110bfc7SLiam Girdwood "ASoC: machine %s should use snd_soc_register_card()\n", 1690fe4085e8SMark Brown card->name); 1691fe4085e8SMark Brown 1692435c5e25SMark Brown /* Bodge while we unpick instantiation */ 1693435c5e25SMark Brown card->dev = &pdev->dev; 1694f0fba2adSLiam Girdwood 169528d528c8SMark Brown return snd_soc_register_card(card); 1696435c5e25SMark Brown } 1697435c5e25SMark Brown 1698b0e26485SVinod Koul static int soc_cleanup_card_resources(struct snd_soc_card *card) 1699db2a4165SFrank Mandarino { 1700db2a4165SFrank Mandarino int i; 1701db2a4165SFrank Mandarino 1702f0fba2adSLiam Girdwood /* make sure any delayed work runs */ 1703f0fba2adSLiam Girdwood for (i = 0; i < card->num_rtd; i++) { 1704f0fba2adSLiam Girdwood struct snd_soc_pcm_runtime *rtd = &card->rtd[i]; 170543829731STejun Heo flush_delayed_work(&rtd->delayed_work); 1706db2a4165SFrank Mandarino } 1707db2a4165SFrank Mandarino 17082eea392dSJarkko Nikula /* remove auxiliary devices */ 17092eea392dSJarkko Nikula for (i = 0; i < card->num_aux_devs; i++) 17102eea392dSJarkko Nikula soc_remove_aux_dev(card, i); 17112eea392dSJarkko Nikula 1712f0fba2adSLiam Girdwood /* remove and free each DAI */ 17130671fd8eSKuninori Morimoto soc_remove_dai_links(card); 1714f0fba2adSLiam Girdwood 1715a6052154SJarkko Nikula soc_cleanup_card_debugfs(card); 1716a6052154SJarkko Nikula 1717f0fba2adSLiam Girdwood /* remove the card */ 171887506549SMark Brown if (card->remove) 1719e7361ec4SMark Brown card->remove(card); 1720f0fba2adSLiam Girdwood 17210aaae527SLars-Peter Clausen snd_soc_dapm_free(&card->dapm); 17220aaae527SLars-Peter Clausen 1723f0fba2adSLiam Girdwood snd_card_free(card->snd_card); 1724b0e26485SVinod Koul return 0; 1725b0e26485SVinod Koul 1726b2dfa62cSGuennadi Liakhovetski } 1727b0e26485SVinod Koul 1728b0e26485SVinod Koul /* removes a socdev */ 1729b0e26485SVinod Koul static int soc_remove(struct platform_device *pdev) 1730b0e26485SVinod Koul { 1731b0e26485SVinod Koul struct snd_soc_card *card = platform_get_drvdata(pdev); 1732b0e26485SVinod Koul 1733c5af3a2eSMark Brown snd_soc_unregister_card(card); 1734db2a4165SFrank Mandarino return 0; 1735db2a4165SFrank Mandarino } 1736db2a4165SFrank Mandarino 17376f8ab4acSMark Brown int snd_soc_poweroff(struct device *dev) 173851737470SMark Brown { 17396f8ab4acSMark Brown struct snd_soc_card *card = dev_get_drvdata(dev); 1740f0fba2adSLiam Girdwood int i; 174151737470SMark Brown 174251737470SMark Brown if (!card->instantiated) 1743416356fcSMark Brown return 0; 174451737470SMark Brown 174551737470SMark Brown /* Flush out pmdown_time work - we actually do want to run it 174651737470SMark Brown * now, we're shutting down so no imminent restart. */ 1747f0fba2adSLiam Girdwood for (i = 0; i < card->num_rtd; i++) { 1748f0fba2adSLiam Girdwood struct snd_soc_pcm_runtime *rtd = &card->rtd[i]; 174943829731STejun Heo flush_delayed_work(&rtd->delayed_work); 1750f0fba2adSLiam Girdwood } 175151737470SMark Brown 1752f0fba2adSLiam Girdwood snd_soc_dapm_shutdown(card); 1753416356fcSMark Brown 1754988e8cc4SNicolin Chen /* deactivate pins to sleep state */ 1755988e8cc4SNicolin Chen for (i = 0; i < card->num_rtd; i++) { 175688bd870fSBenoit Cousson struct snd_soc_pcm_runtime *rtd = &card->rtd[i]; 175788bd870fSBenoit Cousson struct snd_soc_dai *cpu_dai = rtd->cpu_dai; 175888bd870fSBenoit Cousson int j; 175988bd870fSBenoit Cousson 1760988e8cc4SNicolin Chen pinctrl_pm_select_sleep_state(cpu_dai->dev); 176188bd870fSBenoit Cousson for (j = 0; j < rtd->num_codecs; j++) { 176288bd870fSBenoit Cousson struct snd_soc_dai *codec_dai = rtd->codec_dais[j]; 176388bd870fSBenoit Cousson pinctrl_pm_select_sleep_state(codec_dai->dev); 176488bd870fSBenoit Cousson } 1765988e8cc4SNicolin Chen } 1766988e8cc4SNicolin Chen 1767416356fcSMark Brown return 0; 176851737470SMark Brown } 17696f8ab4acSMark Brown EXPORT_SYMBOL_GPL(snd_soc_poweroff); 177051737470SMark Brown 17716f8ab4acSMark Brown const struct dev_pm_ops snd_soc_pm_ops = { 1772b1dd5897SViresh Kumar .suspend = snd_soc_suspend, 1773b1dd5897SViresh Kumar .resume = snd_soc_resume, 1774b1dd5897SViresh Kumar .freeze = snd_soc_suspend, 1775b1dd5897SViresh Kumar .thaw = snd_soc_resume, 17766f8ab4acSMark Brown .poweroff = snd_soc_poweroff, 1777b1dd5897SViresh Kumar .restore = snd_soc_resume, 1778416356fcSMark Brown }; 1779deb2607eSStephen Warren EXPORT_SYMBOL_GPL(snd_soc_pm_ops); 1780416356fcSMark Brown 1781db2a4165SFrank Mandarino /* ASoC platform driver */ 1782db2a4165SFrank Mandarino static struct platform_driver soc_driver = { 1783db2a4165SFrank Mandarino .driver = { 1784db2a4165SFrank Mandarino .name = "soc-audio", 17856f8ab4acSMark Brown .pm = &snd_soc_pm_ops, 1786db2a4165SFrank Mandarino }, 1787db2a4165SFrank Mandarino .probe = soc_probe, 1788db2a4165SFrank Mandarino .remove = soc_remove, 1789db2a4165SFrank Mandarino }; 1790db2a4165SFrank Mandarino 1791096e49d5SMark Brown /** 1792db2a4165SFrank Mandarino * snd_soc_cnew - create new control 1793db2a4165SFrank Mandarino * @_template: control template 1794db2a4165SFrank Mandarino * @data: control private data 1795ac11a2b3SMark Brown * @long_name: control long name 1796efb7ac3fSMark Brown * @prefix: control name prefix 1797db2a4165SFrank Mandarino * 1798db2a4165SFrank Mandarino * Create a new mixer control from a template control. 1799db2a4165SFrank Mandarino * 1800db2a4165SFrank Mandarino * Returns 0 for success, else error. 1801db2a4165SFrank Mandarino */ 1802db2a4165SFrank Mandarino struct snd_kcontrol *snd_soc_cnew(const struct snd_kcontrol_new *_template, 18033056557fSMark Brown void *data, const char *long_name, 1804efb7ac3fSMark Brown const char *prefix) 1805db2a4165SFrank Mandarino { 1806db2a4165SFrank Mandarino struct snd_kcontrol_new template; 1807efb7ac3fSMark Brown struct snd_kcontrol *kcontrol; 1808efb7ac3fSMark Brown char *name = NULL; 1809db2a4165SFrank Mandarino 1810db2a4165SFrank Mandarino memcpy(&template, _template, sizeof(template)); 1811db2a4165SFrank Mandarino template.index = 0; 1812db2a4165SFrank Mandarino 1813efb7ac3fSMark Brown if (!long_name) 1814efb7ac3fSMark Brown long_name = template.name; 1815efb7ac3fSMark Brown 1816efb7ac3fSMark Brown if (prefix) { 18172b581074SLars-Peter Clausen name = kasprintf(GFP_KERNEL, "%s %s", prefix, long_name); 1818efb7ac3fSMark Brown if (!name) 1819efb7ac3fSMark Brown return NULL; 1820efb7ac3fSMark Brown 1821efb7ac3fSMark Brown template.name = name; 1822efb7ac3fSMark Brown } else { 1823efb7ac3fSMark Brown template.name = long_name; 1824efb7ac3fSMark Brown } 1825efb7ac3fSMark Brown 1826efb7ac3fSMark Brown kcontrol = snd_ctl_new1(&template, data); 1827efb7ac3fSMark Brown 1828efb7ac3fSMark Brown kfree(name); 1829efb7ac3fSMark Brown 1830efb7ac3fSMark Brown return kcontrol; 1831db2a4165SFrank Mandarino } 1832db2a4165SFrank Mandarino EXPORT_SYMBOL_GPL(snd_soc_cnew); 1833db2a4165SFrank Mandarino 1834022658beSLiam Girdwood static int snd_soc_add_controls(struct snd_card *card, struct device *dev, 1835022658beSLiam Girdwood const struct snd_kcontrol_new *controls, int num_controls, 1836022658beSLiam Girdwood const char *prefix, void *data) 1837022658beSLiam Girdwood { 1838022658beSLiam Girdwood int err, i; 1839022658beSLiam Girdwood 1840022658beSLiam Girdwood for (i = 0; i < num_controls; i++) { 1841022658beSLiam Girdwood const struct snd_kcontrol_new *control = &controls[i]; 1842022658beSLiam Girdwood err = snd_ctl_add(card, snd_soc_cnew(control, data, 1843022658beSLiam Girdwood control->name, prefix)); 1844022658beSLiam Girdwood if (err < 0) { 1845f110bfc7SLiam Girdwood dev_err(dev, "ASoC: Failed to add %s: %d\n", 1846f110bfc7SLiam Girdwood control->name, err); 1847022658beSLiam Girdwood return err; 1848022658beSLiam Girdwood } 1849022658beSLiam Girdwood } 1850022658beSLiam Girdwood 1851022658beSLiam Girdwood return 0; 1852022658beSLiam Girdwood } 1853022658beSLiam Girdwood 18544fefd698SDimitris Papastamos struct snd_kcontrol *snd_soc_card_get_kcontrol(struct snd_soc_card *soc_card, 18554fefd698SDimitris Papastamos const char *name) 18564fefd698SDimitris Papastamos { 18574fefd698SDimitris Papastamos struct snd_card *card = soc_card->snd_card; 18584fefd698SDimitris Papastamos struct snd_kcontrol *kctl; 18594fefd698SDimitris Papastamos 18604fefd698SDimitris Papastamos if (unlikely(!name)) 18614fefd698SDimitris Papastamos return NULL; 18624fefd698SDimitris Papastamos 18634fefd698SDimitris Papastamos list_for_each_entry(kctl, &card->controls, list) 18644fefd698SDimitris Papastamos if (!strncmp(kctl->id.name, name, sizeof(kctl->id.name))) 18654fefd698SDimitris Papastamos return kctl; 18664fefd698SDimitris Papastamos return NULL; 18674fefd698SDimitris Papastamos } 18684fefd698SDimitris Papastamos EXPORT_SYMBOL_GPL(snd_soc_card_get_kcontrol); 18694fefd698SDimitris Papastamos 1870db2a4165SFrank Mandarino /** 18710f2780adSLars-Peter Clausen * snd_soc_add_component_controls - Add an array of controls to a component. 18720f2780adSLars-Peter Clausen * 18730f2780adSLars-Peter Clausen * @component: Component to add controls to 18740f2780adSLars-Peter Clausen * @controls: Array of controls to add 18750f2780adSLars-Peter Clausen * @num_controls: Number of elements in the array 18760f2780adSLars-Peter Clausen * 18770f2780adSLars-Peter Clausen * Return: 0 for success, else error. 18780f2780adSLars-Peter Clausen */ 18790f2780adSLars-Peter Clausen int snd_soc_add_component_controls(struct snd_soc_component *component, 18800f2780adSLars-Peter Clausen const struct snd_kcontrol_new *controls, unsigned int num_controls) 18810f2780adSLars-Peter Clausen { 18820f2780adSLars-Peter Clausen struct snd_card *card = component->card->snd_card; 18830f2780adSLars-Peter Clausen 18840f2780adSLars-Peter Clausen return snd_soc_add_controls(card, component->dev, controls, 18850f2780adSLars-Peter Clausen num_controls, component->name_prefix, component); 18860f2780adSLars-Peter Clausen } 18870f2780adSLars-Peter Clausen EXPORT_SYMBOL_GPL(snd_soc_add_component_controls); 18880f2780adSLars-Peter Clausen 18890f2780adSLars-Peter Clausen /** 1890022658beSLiam Girdwood * snd_soc_add_codec_controls - add an array of controls to a codec. 1891022658beSLiam Girdwood * Convenience function to add a list of controls. Many codecs were 18923e8e1952SIan Molton * duplicating this code. 18933e8e1952SIan Molton * 18943e8e1952SIan Molton * @codec: codec to add controls to 18953e8e1952SIan Molton * @controls: array of controls to add 18963e8e1952SIan Molton * @num_controls: number of elements in the array 18973e8e1952SIan Molton * 18983e8e1952SIan Molton * Return 0 for success, else error. 18993e8e1952SIan Molton */ 1900022658beSLiam Girdwood int snd_soc_add_codec_controls(struct snd_soc_codec *codec, 19010f2780adSLars-Peter Clausen const struct snd_kcontrol_new *controls, unsigned int num_controls) 19023e8e1952SIan Molton { 19030f2780adSLars-Peter Clausen return snd_soc_add_component_controls(&codec->component, controls, 19040f2780adSLars-Peter Clausen num_controls); 19053e8e1952SIan Molton } 1906022658beSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_add_codec_controls); 19073e8e1952SIan Molton 19083e8e1952SIan Molton /** 1909a491a5c8SLiam Girdwood * snd_soc_add_platform_controls - add an array of controls to a platform. 1910022658beSLiam Girdwood * Convenience function to add a list of controls. 1911a491a5c8SLiam Girdwood * 1912a491a5c8SLiam Girdwood * @platform: platform to add controls to 1913a491a5c8SLiam Girdwood * @controls: array of controls to add 1914a491a5c8SLiam Girdwood * @num_controls: number of elements in the array 1915a491a5c8SLiam Girdwood * 1916a491a5c8SLiam Girdwood * Return 0 for success, else error. 1917a491a5c8SLiam Girdwood */ 1918a491a5c8SLiam Girdwood int snd_soc_add_platform_controls(struct snd_soc_platform *platform, 19190f2780adSLars-Peter Clausen const struct snd_kcontrol_new *controls, unsigned int num_controls) 1920a491a5c8SLiam Girdwood { 19210f2780adSLars-Peter Clausen return snd_soc_add_component_controls(&platform->component, controls, 19220f2780adSLars-Peter Clausen num_controls); 1923a491a5c8SLiam Girdwood } 1924a491a5c8SLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_add_platform_controls); 1925a491a5c8SLiam Girdwood 1926a491a5c8SLiam Girdwood /** 1927022658beSLiam Girdwood * snd_soc_add_card_controls - add an array of controls to a SoC card. 1928022658beSLiam Girdwood * Convenience function to add a list of controls. 1929022658beSLiam Girdwood * 1930022658beSLiam Girdwood * @soc_card: SoC card to add controls to 1931022658beSLiam Girdwood * @controls: array of controls to add 1932022658beSLiam Girdwood * @num_controls: number of elements in the array 1933022658beSLiam Girdwood * 1934022658beSLiam Girdwood * Return 0 for success, else error. 1935022658beSLiam Girdwood */ 1936022658beSLiam Girdwood int snd_soc_add_card_controls(struct snd_soc_card *soc_card, 1937022658beSLiam Girdwood const struct snd_kcontrol_new *controls, int num_controls) 1938022658beSLiam Girdwood { 1939022658beSLiam Girdwood struct snd_card *card = soc_card->snd_card; 1940022658beSLiam Girdwood 1941022658beSLiam Girdwood return snd_soc_add_controls(card, soc_card->dev, controls, num_controls, 1942022658beSLiam Girdwood NULL, soc_card); 1943022658beSLiam Girdwood } 1944022658beSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_add_card_controls); 1945022658beSLiam Girdwood 1946022658beSLiam Girdwood /** 1947022658beSLiam Girdwood * snd_soc_add_dai_controls - add an array of controls to a DAI. 1948022658beSLiam Girdwood * Convienience function to add a list of controls. 1949022658beSLiam Girdwood * 1950022658beSLiam Girdwood * @dai: DAI to add controls to 1951022658beSLiam Girdwood * @controls: array of controls to add 1952022658beSLiam Girdwood * @num_controls: number of elements in the array 1953022658beSLiam Girdwood * 1954022658beSLiam Girdwood * Return 0 for success, else error. 1955022658beSLiam Girdwood */ 1956022658beSLiam Girdwood int snd_soc_add_dai_controls(struct snd_soc_dai *dai, 1957022658beSLiam Girdwood const struct snd_kcontrol_new *controls, int num_controls) 1958022658beSLiam Girdwood { 1959313665b9SLars-Peter Clausen struct snd_card *card = dai->component->card->snd_card; 1960022658beSLiam Girdwood 1961022658beSLiam Girdwood return snd_soc_add_controls(card, dai->dev, controls, num_controls, 1962022658beSLiam Girdwood NULL, dai); 1963022658beSLiam Girdwood } 1964022658beSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_add_dai_controls); 1965022658beSLiam Girdwood 1966022658beSLiam Girdwood /** 19678c6529dbSLiam Girdwood * snd_soc_dai_set_sysclk - configure DAI system or master clock. 19688c6529dbSLiam Girdwood * @dai: DAI 19698c6529dbSLiam Girdwood * @clk_id: DAI specific clock ID 19708c6529dbSLiam Girdwood * @freq: new clock frequency in Hz 19718c6529dbSLiam Girdwood * @dir: new clock direction - input/output. 19728c6529dbSLiam Girdwood * 19738c6529dbSLiam Girdwood * Configures the DAI master (MCLK) or system (SYSCLK) clocking. 19748c6529dbSLiam Girdwood */ 19758c6529dbSLiam Girdwood int snd_soc_dai_set_sysclk(struct snd_soc_dai *dai, int clk_id, 19768c6529dbSLiam Girdwood unsigned int freq, int dir) 19778c6529dbSLiam Girdwood { 1978f0fba2adSLiam Girdwood if (dai->driver && dai->driver->ops->set_sysclk) 1979f0fba2adSLiam Girdwood return dai->driver->ops->set_sysclk(dai, clk_id, freq, dir); 1980ec4ee52aSMark Brown else if (dai->codec && dai->codec->driver->set_sysclk) 1981da1c6ea6SMark Brown return dai->codec->driver->set_sysclk(dai->codec, clk_id, 0, 1982ec4ee52aSMark Brown freq, dir); 19838c6529dbSLiam Girdwood else 19841104a9c8SMark Brown return -ENOTSUPP; 19858c6529dbSLiam Girdwood } 19868c6529dbSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_dai_set_sysclk); 19878c6529dbSLiam Girdwood 19888c6529dbSLiam Girdwood /** 1989ec4ee52aSMark Brown * snd_soc_codec_set_sysclk - configure CODEC system or master clock. 1990ec4ee52aSMark Brown * @codec: CODEC 1991ec4ee52aSMark Brown * @clk_id: DAI specific clock ID 1992da1c6ea6SMark Brown * @source: Source for the clock 1993ec4ee52aSMark Brown * @freq: new clock frequency in Hz 1994ec4ee52aSMark Brown * @dir: new clock direction - input/output. 1995ec4ee52aSMark Brown * 1996ec4ee52aSMark Brown * Configures the CODEC master (MCLK) or system (SYSCLK) clocking. 1997ec4ee52aSMark Brown */ 1998ec4ee52aSMark Brown int snd_soc_codec_set_sysclk(struct snd_soc_codec *codec, int clk_id, 1999da1c6ea6SMark Brown int source, unsigned int freq, int dir) 2000ec4ee52aSMark Brown { 2001ec4ee52aSMark Brown if (codec->driver->set_sysclk) 2002da1c6ea6SMark Brown return codec->driver->set_sysclk(codec, clk_id, source, 2003da1c6ea6SMark Brown freq, dir); 2004ec4ee52aSMark Brown else 20051104a9c8SMark Brown return -ENOTSUPP; 2006ec4ee52aSMark Brown } 2007ec4ee52aSMark Brown EXPORT_SYMBOL_GPL(snd_soc_codec_set_sysclk); 2008ec4ee52aSMark Brown 2009ec4ee52aSMark Brown /** 20108c6529dbSLiam Girdwood * snd_soc_dai_set_clkdiv - configure DAI clock dividers. 20118c6529dbSLiam Girdwood * @dai: DAI 2012ac11a2b3SMark Brown * @div_id: DAI specific clock divider ID 20138c6529dbSLiam Girdwood * @div: new clock divisor. 20148c6529dbSLiam Girdwood * 20158c6529dbSLiam Girdwood * Configures the clock dividers. This is used to derive the best DAI bit and 20168c6529dbSLiam Girdwood * frame clocks from the system or master clock. It's best to set the DAI bit 20178c6529dbSLiam Girdwood * and frame clocks as low as possible to save system power. 20188c6529dbSLiam Girdwood */ 20198c6529dbSLiam Girdwood int snd_soc_dai_set_clkdiv(struct snd_soc_dai *dai, 20208c6529dbSLiam Girdwood int div_id, int div) 20218c6529dbSLiam Girdwood { 2022f0fba2adSLiam Girdwood if (dai->driver && dai->driver->ops->set_clkdiv) 2023f0fba2adSLiam Girdwood return dai->driver->ops->set_clkdiv(dai, div_id, div); 20248c6529dbSLiam Girdwood else 20258c6529dbSLiam Girdwood return -EINVAL; 20268c6529dbSLiam Girdwood } 20278c6529dbSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_dai_set_clkdiv); 20288c6529dbSLiam Girdwood 20298c6529dbSLiam Girdwood /** 20308c6529dbSLiam Girdwood * snd_soc_dai_set_pll - configure DAI PLL. 20318c6529dbSLiam Girdwood * @dai: DAI 20328c6529dbSLiam Girdwood * @pll_id: DAI specific PLL ID 203385488037SMark Brown * @source: DAI specific source for the PLL 20348c6529dbSLiam Girdwood * @freq_in: PLL input clock frequency in Hz 20358c6529dbSLiam Girdwood * @freq_out: requested PLL output clock frequency in Hz 20368c6529dbSLiam Girdwood * 20378c6529dbSLiam Girdwood * Configures and enables PLL to generate output clock based on input clock. 20388c6529dbSLiam Girdwood */ 203985488037SMark Brown int snd_soc_dai_set_pll(struct snd_soc_dai *dai, int pll_id, int source, 204085488037SMark Brown unsigned int freq_in, unsigned int freq_out) 20418c6529dbSLiam Girdwood { 2042f0fba2adSLiam Girdwood if (dai->driver && dai->driver->ops->set_pll) 2043f0fba2adSLiam Girdwood return dai->driver->ops->set_pll(dai, pll_id, source, 204485488037SMark Brown freq_in, freq_out); 2045ec4ee52aSMark Brown else if (dai->codec && dai->codec->driver->set_pll) 2046ec4ee52aSMark Brown return dai->codec->driver->set_pll(dai->codec, pll_id, source, 2047ec4ee52aSMark Brown freq_in, freq_out); 20488c6529dbSLiam Girdwood else 20498c6529dbSLiam Girdwood return -EINVAL; 20508c6529dbSLiam Girdwood } 20518c6529dbSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_dai_set_pll); 20528c6529dbSLiam Girdwood 2053ec4ee52aSMark Brown /* 2054ec4ee52aSMark Brown * snd_soc_codec_set_pll - configure codec PLL. 2055ec4ee52aSMark Brown * @codec: CODEC 2056ec4ee52aSMark Brown * @pll_id: DAI specific PLL ID 2057ec4ee52aSMark Brown * @source: DAI specific source for the PLL 2058ec4ee52aSMark Brown * @freq_in: PLL input clock frequency in Hz 2059ec4ee52aSMark Brown * @freq_out: requested PLL output clock frequency in Hz 2060ec4ee52aSMark Brown * 2061ec4ee52aSMark Brown * Configures and enables PLL to generate output clock based on input clock. 2062ec4ee52aSMark Brown */ 2063ec4ee52aSMark Brown int snd_soc_codec_set_pll(struct snd_soc_codec *codec, int pll_id, int source, 2064ec4ee52aSMark Brown unsigned int freq_in, unsigned int freq_out) 2065ec4ee52aSMark Brown { 2066ec4ee52aSMark Brown if (codec->driver->set_pll) 2067ec4ee52aSMark Brown return codec->driver->set_pll(codec, pll_id, source, 2068ec4ee52aSMark Brown freq_in, freq_out); 2069ec4ee52aSMark Brown else 2070ec4ee52aSMark Brown return -EINVAL; 2071ec4ee52aSMark Brown } 2072ec4ee52aSMark Brown EXPORT_SYMBOL_GPL(snd_soc_codec_set_pll); 2073ec4ee52aSMark Brown 20748c6529dbSLiam Girdwood /** 2075e54cf76bSLiam Girdwood * snd_soc_dai_set_bclk_ratio - configure BCLK to sample rate ratio. 2076e54cf76bSLiam Girdwood * @dai: DAI 2077e54cf76bSLiam Girdwood * @ratio Ratio of BCLK to Sample rate. 2078e54cf76bSLiam Girdwood * 2079e54cf76bSLiam Girdwood * Configures the DAI for a preset BCLK to sample rate ratio. 2080e54cf76bSLiam Girdwood */ 2081e54cf76bSLiam Girdwood int snd_soc_dai_set_bclk_ratio(struct snd_soc_dai *dai, unsigned int ratio) 2082e54cf76bSLiam Girdwood { 2083e54cf76bSLiam Girdwood if (dai->driver && dai->driver->ops->set_bclk_ratio) 2084e54cf76bSLiam Girdwood return dai->driver->ops->set_bclk_ratio(dai, ratio); 2085e54cf76bSLiam Girdwood else 2086e54cf76bSLiam Girdwood return -EINVAL; 2087e54cf76bSLiam Girdwood } 2088e54cf76bSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_dai_set_bclk_ratio); 2089e54cf76bSLiam Girdwood 2090e54cf76bSLiam Girdwood /** 20918c6529dbSLiam Girdwood * snd_soc_dai_set_fmt - configure DAI hardware audio format. 20928c6529dbSLiam Girdwood * @dai: DAI 20938c6529dbSLiam Girdwood * @fmt: SND_SOC_DAIFMT_ format value. 20948c6529dbSLiam Girdwood * 20958c6529dbSLiam Girdwood * Configures the DAI hardware format and clocking. 20968c6529dbSLiam Girdwood */ 20978c6529dbSLiam Girdwood int snd_soc_dai_set_fmt(struct snd_soc_dai *dai, unsigned int fmt) 20988c6529dbSLiam Girdwood { 20995e4ba569SShawn Guo if (dai->driver == NULL) 21008c6529dbSLiam Girdwood return -EINVAL; 21015e4ba569SShawn Guo if (dai->driver->ops->set_fmt == NULL) 21025e4ba569SShawn Guo return -ENOTSUPP; 21035e4ba569SShawn Guo return dai->driver->ops->set_fmt(dai, fmt); 21048c6529dbSLiam Girdwood } 21058c6529dbSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_dai_set_fmt); 21068c6529dbSLiam Girdwood 21078c6529dbSLiam Girdwood /** 2108e5c21514SXiubo Li * snd_soc_xlate_tdm_slot - generate tx/rx slot mask. 210989c67857SXiubo Li * @slots: Number of slots in use. 211089c67857SXiubo Li * @tx_mask: bitmask representing active TX slots. 211189c67857SXiubo Li * @rx_mask: bitmask representing active RX slots. 211289c67857SXiubo Li * 211389c67857SXiubo Li * Generates the TDM tx and rx slot default masks for DAI. 211489c67857SXiubo Li */ 2115e5c21514SXiubo Li static int snd_soc_xlate_tdm_slot_mask(unsigned int slots, 211689c67857SXiubo Li unsigned int *tx_mask, 211789c67857SXiubo Li unsigned int *rx_mask) 211889c67857SXiubo Li { 211989c67857SXiubo Li if (*tx_mask || *rx_mask) 212089c67857SXiubo Li return 0; 212189c67857SXiubo Li 212289c67857SXiubo Li if (!slots) 212389c67857SXiubo Li return -EINVAL; 212489c67857SXiubo Li 212589c67857SXiubo Li *tx_mask = (1 << slots) - 1; 212689c67857SXiubo Li *rx_mask = (1 << slots) - 1; 212789c67857SXiubo Li 212889c67857SXiubo Li return 0; 212989c67857SXiubo Li } 213089c67857SXiubo Li 213189c67857SXiubo Li /** 21328c6529dbSLiam Girdwood * snd_soc_dai_set_tdm_slot - configure DAI TDM. 21338c6529dbSLiam Girdwood * @dai: DAI 2134a5479e38SDaniel Ribeiro * @tx_mask: bitmask representing active TX slots. 2135a5479e38SDaniel Ribeiro * @rx_mask: bitmask representing active RX slots. 21368c6529dbSLiam Girdwood * @slots: Number of slots in use. 2137a5479e38SDaniel Ribeiro * @slot_width: Width in bits for each slot. 21388c6529dbSLiam Girdwood * 21398c6529dbSLiam Girdwood * Configures a DAI for TDM operation. Both mask and slots are codec and DAI 21408c6529dbSLiam Girdwood * specific. 21418c6529dbSLiam Girdwood */ 21428c6529dbSLiam Girdwood int snd_soc_dai_set_tdm_slot(struct snd_soc_dai *dai, 2143a5479e38SDaniel Ribeiro unsigned int tx_mask, unsigned int rx_mask, int slots, int slot_width) 21448c6529dbSLiam Girdwood { 2145e5c21514SXiubo Li if (dai->driver && dai->driver->ops->xlate_tdm_slot_mask) 2146e5c21514SXiubo Li dai->driver->ops->xlate_tdm_slot_mask(slots, 214789c67857SXiubo Li &tx_mask, &rx_mask); 214889c67857SXiubo Li else 2149e5c21514SXiubo Li snd_soc_xlate_tdm_slot_mask(slots, &tx_mask, &rx_mask); 215089c67857SXiubo Li 215188bd870fSBenoit Cousson dai->tx_mask = tx_mask; 215288bd870fSBenoit Cousson dai->rx_mask = rx_mask; 215388bd870fSBenoit Cousson 2154f0fba2adSLiam Girdwood if (dai->driver && dai->driver->ops->set_tdm_slot) 2155f0fba2adSLiam Girdwood return dai->driver->ops->set_tdm_slot(dai, tx_mask, rx_mask, 2156a5479e38SDaniel Ribeiro slots, slot_width); 21578c6529dbSLiam Girdwood else 2158b2cbb6e1SXiubo Li return -ENOTSUPP; 21598c6529dbSLiam Girdwood } 21608c6529dbSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_dai_set_tdm_slot); 21618c6529dbSLiam Girdwood 21628c6529dbSLiam Girdwood /** 2163472df3cbSBarry Song * snd_soc_dai_set_channel_map - configure DAI audio channel map 2164472df3cbSBarry Song * @dai: DAI 2165472df3cbSBarry Song * @tx_num: how many TX channels 2166472df3cbSBarry Song * @tx_slot: pointer to an array which imply the TX slot number channel 2167472df3cbSBarry Song * 0~num-1 uses 2168472df3cbSBarry Song * @rx_num: how many RX channels 2169472df3cbSBarry Song * @rx_slot: pointer to an array which imply the RX slot number channel 2170472df3cbSBarry Song * 0~num-1 uses 2171472df3cbSBarry Song * 2172472df3cbSBarry Song * configure the relationship between channel number and TDM slot number. 2173472df3cbSBarry Song */ 2174472df3cbSBarry Song int snd_soc_dai_set_channel_map(struct snd_soc_dai *dai, 2175472df3cbSBarry Song unsigned int tx_num, unsigned int *tx_slot, 2176472df3cbSBarry Song unsigned int rx_num, unsigned int *rx_slot) 2177472df3cbSBarry Song { 2178f0fba2adSLiam Girdwood if (dai->driver && dai->driver->ops->set_channel_map) 2179f0fba2adSLiam Girdwood return dai->driver->ops->set_channel_map(dai, tx_num, tx_slot, 2180472df3cbSBarry Song rx_num, rx_slot); 2181472df3cbSBarry Song else 2182472df3cbSBarry Song return -EINVAL; 2183472df3cbSBarry Song } 2184472df3cbSBarry Song EXPORT_SYMBOL_GPL(snd_soc_dai_set_channel_map); 2185472df3cbSBarry Song 2186472df3cbSBarry Song /** 21878c6529dbSLiam Girdwood * snd_soc_dai_set_tristate - configure DAI system or master clock. 21888c6529dbSLiam Girdwood * @dai: DAI 21898c6529dbSLiam Girdwood * @tristate: tristate enable 21908c6529dbSLiam Girdwood * 21918c6529dbSLiam Girdwood * Tristates the DAI so that others can use it. 21928c6529dbSLiam Girdwood */ 21938c6529dbSLiam Girdwood int snd_soc_dai_set_tristate(struct snd_soc_dai *dai, int tristate) 21948c6529dbSLiam Girdwood { 2195f0fba2adSLiam Girdwood if (dai->driver && dai->driver->ops->set_tristate) 2196f0fba2adSLiam Girdwood return dai->driver->ops->set_tristate(dai, tristate); 21978c6529dbSLiam Girdwood else 21988c6529dbSLiam Girdwood return -EINVAL; 21998c6529dbSLiam Girdwood } 22008c6529dbSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_dai_set_tristate); 22018c6529dbSLiam Girdwood 22028c6529dbSLiam Girdwood /** 22038c6529dbSLiam Girdwood * snd_soc_dai_digital_mute - configure DAI system or master clock. 22048c6529dbSLiam Girdwood * @dai: DAI 22058c6529dbSLiam Girdwood * @mute: mute enable 2206da18396fSMark Brown * @direction: stream to mute 22078c6529dbSLiam Girdwood * 22088c6529dbSLiam Girdwood * Mutes the DAI DAC. 22098c6529dbSLiam Girdwood */ 2210da18396fSMark Brown int snd_soc_dai_digital_mute(struct snd_soc_dai *dai, int mute, 2211da18396fSMark Brown int direction) 22128c6529dbSLiam Girdwood { 2213da18396fSMark Brown if (!dai->driver) 2214da18396fSMark Brown return -ENOTSUPP; 2215da18396fSMark Brown 2216da18396fSMark Brown if (dai->driver->ops->mute_stream) 2217da18396fSMark Brown return dai->driver->ops->mute_stream(dai, mute, direction); 2218da18396fSMark Brown else if (direction == SNDRV_PCM_STREAM_PLAYBACK && 2219da18396fSMark Brown dai->driver->ops->digital_mute) 2220f0fba2adSLiam Girdwood return dai->driver->ops->digital_mute(dai, mute); 22218c6529dbSLiam Girdwood else 222204570c62SMark Brown return -ENOTSUPP; 22238c6529dbSLiam Girdwood } 22248c6529dbSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_dai_digital_mute); 22258c6529dbSLiam Girdwood 222688bd870fSBenoit Cousson static int snd_soc_init_multicodec(struct snd_soc_card *card, 222788bd870fSBenoit Cousson struct snd_soc_dai_link *dai_link) 222888bd870fSBenoit Cousson { 222988bd870fSBenoit Cousson /* Legacy codec/codec_dai link is a single entry in multicodec */ 223088bd870fSBenoit Cousson if (dai_link->codec_name || dai_link->codec_of_node || 223188bd870fSBenoit Cousson dai_link->codec_dai_name) { 223288bd870fSBenoit Cousson dai_link->num_codecs = 1; 223388bd870fSBenoit Cousson 223488bd870fSBenoit Cousson dai_link->codecs = devm_kzalloc(card->dev, 223588bd870fSBenoit Cousson sizeof(struct snd_soc_dai_link_component), 223688bd870fSBenoit Cousson GFP_KERNEL); 223788bd870fSBenoit Cousson if (!dai_link->codecs) 223888bd870fSBenoit Cousson return -ENOMEM; 223988bd870fSBenoit Cousson 224088bd870fSBenoit Cousson dai_link->codecs[0].name = dai_link->codec_name; 224188bd870fSBenoit Cousson dai_link->codecs[0].of_node = dai_link->codec_of_node; 224288bd870fSBenoit Cousson dai_link->codecs[0].dai_name = dai_link->codec_dai_name; 224388bd870fSBenoit Cousson } 224488bd870fSBenoit Cousson 224588bd870fSBenoit Cousson if (!dai_link->codecs) { 224688bd870fSBenoit Cousson dev_err(card->dev, "ASoC: DAI link has no CODECs\n"); 224788bd870fSBenoit Cousson return -EINVAL; 224888bd870fSBenoit Cousson } 224988bd870fSBenoit Cousson 225088bd870fSBenoit Cousson return 0; 225188bd870fSBenoit Cousson } 225288bd870fSBenoit Cousson 2253c5af3a2eSMark Brown /** 2254c5af3a2eSMark Brown * snd_soc_register_card - Register a card with the ASoC core 2255c5af3a2eSMark Brown * 2256ac11a2b3SMark Brown * @card: Card to register 2257c5af3a2eSMark Brown * 2258c5af3a2eSMark Brown */ 225970a7ca34SVinod Koul int snd_soc_register_card(struct snd_soc_card *card) 2260c5af3a2eSMark Brown { 226188bd870fSBenoit Cousson int i, j, ret; 2262f0fba2adSLiam Girdwood 2263c5af3a2eSMark Brown if (!card->name || !card->dev) 2264c5af3a2eSMark Brown return -EINVAL; 2265c5af3a2eSMark Brown 22665a504963SStephen Warren for (i = 0; i < card->num_links; i++) { 22675a504963SStephen Warren struct snd_soc_dai_link *link = &card->dai_link[i]; 22685a504963SStephen Warren 226988bd870fSBenoit Cousson ret = snd_soc_init_multicodec(card, link); 227088bd870fSBenoit Cousson if (ret) { 227188bd870fSBenoit Cousson dev_err(card->dev, "ASoC: failed to init multicodec\n"); 227288bd870fSBenoit Cousson return ret; 227388bd870fSBenoit Cousson } 227488bd870fSBenoit Cousson 227588bd870fSBenoit Cousson for (j = 0; j < link->num_codecs; j++) { 22765a504963SStephen Warren /* 22775a504963SStephen Warren * Codec must be specified by 1 of name or OF node, 22785a504963SStephen Warren * not both or neither. 22795a504963SStephen Warren */ 228088bd870fSBenoit Cousson if (!!link->codecs[j].name == 228188bd870fSBenoit Cousson !!link->codecs[j].of_node) { 228288bd870fSBenoit Cousson dev_err(card->dev, "ASoC: Neither/both codec name/of_node are set for %s\n", 228310e8aa9aSMichał Mirosław link->name); 22845a504963SStephen Warren return -EINVAL; 22855a504963SStephen Warren } 2286bc92657aSStephen Warren /* Codec DAI name must be specified */ 228788bd870fSBenoit Cousson if (!link->codecs[j].dai_name) { 228888bd870fSBenoit Cousson dev_err(card->dev, "ASoC: codec_dai_name not set for %s\n", 228910e8aa9aSMichał Mirosław link->name); 2290bc92657aSStephen Warren return -EINVAL; 2291bc92657aSStephen Warren } 229288bd870fSBenoit Cousson } 22935a504963SStephen Warren 22945a504963SStephen Warren /* 22955a504963SStephen Warren * Platform may be specified by either name or OF node, but 22965a504963SStephen Warren * can be left unspecified, and a dummy platform will be used. 22975a504963SStephen Warren */ 22985a504963SStephen Warren if (link->platform_name && link->platform_of_node) { 229910e8aa9aSMichał Mirosław dev_err(card->dev, 230010e8aa9aSMichał Mirosław "ASoC: Both platform name/of_node are set for %s\n", 230110e8aa9aSMichał Mirosław link->name); 23025a504963SStephen Warren return -EINVAL; 23035a504963SStephen Warren } 23045a504963SStephen Warren 23055a504963SStephen Warren /* 2306bc92657aSStephen Warren * CPU device may be specified by either name or OF node, but 2307bc92657aSStephen Warren * can be left unspecified, and will be matched based on DAI 2308bc92657aSStephen Warren * name alone.. 23095a504963SStephen Warren */ 2310bc92657aSStephen Warren if (link->cpu_name && link->cpu_of_node) { 231110e8aa9aSMichał Mirosław dev_err(card->dev, 231210e8aa9aSMichał Mirosław "ASoC: Neither/both cpu name/of_node are set for %s\n", 231310e8aa9aSMichał Mirosław link->name); 2314bc92657aSStephen Warren return -EINVAL; 2315bc92657aSStephen Warren } 2316bc92657aSStephen Warren /* 2317bc92657aSStephen Warren * At least one of CPU DAI name or CPU device name/node must be 2318bc92657aSStephen Warren * specified 2319bc92657aSStephen Warren */ 2320bc92657aSStephen Warren if (!link->cpu_dai_name && 2321bc92657aSStephen Warren !(link->cpu_name || link->cpu_of_node)) { 232210e8aa9aSMichał Mirosław dev_err(card->dev, 232310e8aa9aSMichał Mirosław "ASoC: Neither cpu_dai_name nor cpu_name/of_node are set for %s\n", 232410e8aa9aSMichał Mirosław link->name); 23255a504963SStephen Warren return -EINVAL; 23265a504963SStephen Warren } 23275a504963SStephen Warren } 23285a504963SStephen Warren 2329ed77cc12SMark Brown dev_set_drvdata(card->dev, card); 2330ed77cc12SMark Brown 2331111c6419SStephen Warren snd_soc_initialize_card_lists(card); 2332111c6419SStephen Warren 2333150dd2f8SVinod Koul soc_init_card_debugfs(card); 2334150dd2f8SVinod Koul 2335181a6892SMark Brown card->rtd = devm_kzalloc(card->dev, 2336181a6892SMark Brown sizeof(struct snd_soc_pcm_runtime) * 23372eea392dSJarkko Nikula (card->num_links + card->num_aux_devs), 2338f0fba2adSLiam Girdwood GFP_KERNEL); 2339f0fba2adSLiam Girdwood if (card->rtd == NULL) 2340f0fba2adSLiam Girdwood return -ENOMEM; 2341a7dbb603SLiam Girdwood card->num_rtd = 0; 23422eea392dSJarkko Nikula card->rtd_aux = &card->rtd[card->num_links]; 2343f0fba2adSLiam Girdwood 23445f3484acSLars-Peter Clausen for (i = 0; i < card->num_links; i++) { 23455f3484acSLars-Peter Clausen card->rtd[i].card = card; 2346f0fba2adSLiam Girdwood card->rtd[i].dai_link = &card->dai_link[i]; 234788bd870fSBenoit Cousson card->rtd[i].codec_dais = devm_kzalloc(card->dev, 234888bd870fSBenoit Cousson sizeof(struct snd_soc_dai *) * 234988bd870fSBenoit Cousson (card->rtd[i].dai_link->num_codecs), 235088bd870fSBenoit Cousson GFP_KERNEL); 235188bd870fSBenoit Cousson if (card->rtd[i].codec_dais == NULL) 235288bd870fSBenoit Cousson return -ENOMEM; 23535f3484acSLars-Peter Clausen } 23545f3484acSLars-Peter Clausen 23555f3484acSLars-Peter Clausen for (i = 0; i < card->num_aux_devs; i++) 23565f3484acSLars-Peter Clausen card->rtd_aux[i].card = card; 2357f0fba2adSLiam Girdwood 2358db432b41SMark Brown INIT_LIST_HEAD(&card->dapm_dirty); 2359c5af3a2eSMark Brown card->instantiated = 0; 2360f0fba2adSLiam Girdwood mutex_init(&card->mutex); 2361a73fb2dfSLiam Girdwood mutex_init(&card->dapm_mutex); 2362c5af3a2eSMark Brown 2363b19e6e7bSMark Brown ret = snd_soc_instantiate_card(card); 2364b19e6e7bSMark Brown if (ret != 0) 2365b19e6e7bSMark Brown soc_cleanup_card_debugfs(card); 2366c5af3a2eSMark Brown 2367988e8cc4SNicolin Chen /* deactivate pins to sleep state */ 2368988e8cc4SNicolin Chen for (i = 0; i < card->num_rtd; i++) { 236988bd870fSBenoit Cousson struct snd_soc_pcm_runtime *rtd = &card->rtd[i]; 237088bd870fSBenoit Cousson struct snd_soc_dai *cpu_dai = rtd->cpu_dai; 237188bd870fSBenoit Cousson int j; 237288bd870fSBenoit Cousson 237388bd870fSBenoit Cousson for (j = 0; j < rtd->num_codecs; j++) { 237488bd870fSBenoit Cousson struct snd_soc_dai *codec_dai = rtd->codec_dais[j]; 2375988e8cc4SNicolin Chen if (!codec_dai->active) 2376988e8cc4SNicolin Chen pinctrl_pm_select_sleep_state(codec_dai->dev); 237788bd870fSBenoit Cousson } 237888bd870fSBenoit Cousson 2379988e8cc4SNicolin Chen if (!cpu_dai->active) 2380988e8cc4SNicolin Chen pinctrl_pm_select_sleep_state(cpu_dai->dev); 2381988e8cc4SNicolin Chen } 2382988e8cc4SNicolin Chen 2383b19e6e7bSMark Brown return ret; 2384c5af3a2eSMark Brown } 238570a7ca34SVinod Koul EXPORT_SYMBOL_GPL(snd_soc_register_card); 2386c5af3a2eSMark Brown 2387c5af3a2eSMark Brown /** 2388c5af3a2eSMark Brown * snd_soc_unregister_card - Unregister a card with the ASoC core 2389c5af3a2eSMark Brown * 2390ac11a2b3SMark Brown * @card: Card to unregister 2391c5af3a2eSMark Brown * 2392c5af3a2eSMark Brown */ 239370a7ca34SVinod Koul int snd_soc_unregister_card(struct snd_soc_card *card) 2394c5af3a2eSMark Brown { 239501e0df66SLars-Peter Clausen if (card->instantiated) { 239601e0df66SLars-Peter Clausen card->instantiated = false; 23971c325f77SLars-Peter Clausen snd_soc_dapm_shutdown(card); 2398b0e26485SVinod Koul soc_cleanup_card_resources(card); 2399f110bfc7SLiam Girdwood dev_dbg(card->dev, "ASoC: Unregistered card '%s'\n", card->name); 2400*8f6f9b29SKuninori Morimoto } 2401c5af3a2eSMark Brown 2402c5af3a2eSMark Brown return 0; 2403c5af3a2eSMark Brown } 240470a7ca34SVinod Koul EXPORT_SYMBOL_GPL(snd_soc_unregister_card); 2405c5af3a2eSMark Brown 2406f0fba2adSLiam Girdwood /* 2407f0fba2adSLiam Girdwood * Simplify DAI link configuration by removing ".-1" from device names 2408f0fba2adSLiam Girdwood * and sanitizing names. 2409f0fba2adSLiam Girdwood */ 24100b9a214aSDimitris Papastamos static char *fmt_single_name(struct device *dev, int *id) 2411f0fba2adSLiam Girdwood { 2412f0fba2adSLiam Girdwood char *found, name[NAME_SIZE]; 2413f0fba2adSLiam Girdwood int id1, id2; 2414f0fba2adSLiam Girdwood 2415f0fba2adSLiam Girdwood if (dev_name(dev) == NULL) 2416f0fba2adSLiam Girdwood return NULL; 2417f0fba2adSLiam Girdwood 241858818a77SDimitris Papastamos strlcpy(name, dev_name(dev), NAME_SIZE); 2419f0fba2adSLiam Girdwood 2420f0fba2adSLiam Girdwood /* are we a "%s.%d" name (platform and SPI components) */ 2421f0fba2adSLiam Girdwood found = strstr(name, dev->driver->name); 2422f0fba2adSLiam Girdwood if (found) { 2423f0fba2adSLiam Girdwood /* get ID */ 2424f0fba2adSLiam Girdwood if (sscanf(&found[strlen(dev->driver->name)], ".%d", id) == 1) { 2425f0fba2adSLiam Girdwood 2426f0fba2adSLiam Girdwood /* discard ID from name if ID == -1 */ 2427f0fba2adSLiam Girdwood if (*id == -1) 2428f0fba2adSLiam Girdwood found[strlen(dev->driver->name)] = '\0'; 2429f0fba2adSLiam Girdwood } 2430f0fba2adSLiam Girdwood 2431f0fba2adSLiam Girdwood } else { 2432f0fba2adSLiam Girdwood /* I2C component devices are named "bus-addr" */ 2433f0fba2adSLiam Girdwood if (sscanf(name, "%x-%x", &id1, &id2) == 2) { 2434f0fba2adSLiam Girdwood char tmp[NAME_SIZE]; 2435f0fba2adSLiam Girdwood 2436f0fba2adSLiam Girdwood /* create unique ID number from I2C addr and bus */ 243705899446SJarkko Nikula *id = ((id1 & 0xffff) << 16) + id2; 2438f0fba2adSLiam Girdwood 2439f0fba2adSLiam Girdwood /* sanitize component name for DAI link creation */ 2440f0fba2adSLiam Girdwood snprintf(tmp, NAME_SIZE, "%s.%s", dev->driver->name, name); 244158818a77SDimitris Papastamos strlcpy(name, tmp, NAME_SIZE); 2442f0fba2adSLiam Girdwood } else 2443f0fba2adSLiam Girdwood *id = 0; 2444f0fba2adSLiam Girdwood } 2445f0fba2adSLiam Girdwood 2446f0fba2adSLiam Girdwood return kstrdup(name, GFP_KERNEL); 2447f0fba2adSLiam Girdwood } 2448f0fba2adSLiam Girdwood 2449f0fba2adSLiam Girdwood /* 2450f0fba2adSLiam Girdwood * Simplify DAI link naming for single devices with multiple DAIs by removing 2451f0fba2adSLiam Girdwood * any ".-1" and using the DAI name (instead of device name). 2452f0fba2adSLiam Girdwood */ 2453f0fba2adSLiam Girdwood static inline char *fmt_multiple_name(struct device *dev, 2454f0fba2adSLiam Girdwood struct snd_soc_dai_driver *dai_drv) 2455f0fba2adSLiam Girdwood { 2456f0fba2adSLiam Girdwood if (dai_drv->name == NULL) { 245710e8aa9aSMichał Mirosław dev_err(dev, 245810e8aa9aSMichał Mirosław "ASoC: error - multiple DAI %s registered with no name\n", 245910e8aa9aSMichał Mirosław dev_name(dev)); 2460f0fba2adSLiam Girdwood return NULL; 2461f0fba2adSLiam Girdwood } 2462f0fba2adSLiam Girdwood 2463f0fba2adSLiam Girdwood return kstrdup(dai_drv->name, GFP_KERNEL); 2464f0fba2adSLiam Girdwood } 2465f0fba2adSLiam Girdwood 24669115171aSMark Brown /** 246732c9ba54SLars-Peter Clausen * snd_soc_unregister_dai - Unregister DAIs from the ASoC core 24689115171aSMark Brown * 246932c9ba54SLars-Peter Clausen * @component: The component for which the DAIs should be unregistered 24709115171aSMark Brown */ 247132c9ba54SLars-Peter Clausen static void snd_soc_unregister_dais(struct snd_soc_component *component) 24729115171aSMark Brown { 24735c1d5f09SLars-Peter Clausen struct snd_soc_dai *dai, *_dai; 24749115171aSMark Brown 24755c1d5f09SLars-Peter Clausen list_for_each_entry_safe(dai, _dai, &component->dai_list, list) { 247632c9ba54SLars-Peter Clausen dev_dbg(component->dev, "ASoC: Unregistered DAI '%s'\n", 247732c9ba54SLars-Peter Clausen dai->name); 24789115171aSMark Brown list_del(&dai->list); 2479f0fba2adSLiam Girdwood kfree(dai->name); 2480f0fba2adSLiam Girdwood kfree(dai); 24819115171aSMark Brown } 248232c9ba54SLars-Peter Clausen } 24839115171aSMark Brown 24849115171aSMark Brown /** 248532c9ba54SLars-Peter Clausen * snd_soc_register_dais - Register a DAI with the ASoC core 24869115171aSMark Brown * 24876106d129SLars-Peter Clausen * @component: The component the DAIs are registered for 24886106d129SLars-Peter Clausen * @dai_drv: DAI driver to use for the DAIs 2489ac11a2b3SMark Brown * @count: Number of DAIs 249032c9ba54SLars-Peter Clausen * @legacy_dai_naming: Use the legacy naming scheme and let the DAI inherit the 249132c9ba54SLars-Peter Clausen * parent's name. 24929115171aSMark Brown */ 249332c9ba54SLars-Peter Clausen static int snd_soc_register_dais(struct snd_soc_component *component, 2494bb13109dSLars-Peter Clausen struct snd_soc_dai_driver *dai_drv, size_t count, 2495bb13109dSLars-Peter Clausen bool legacy_dai_naming) 24969115171aSMark Brown { 24976106d129SLars-Peter Clausen struct device *dev = component->dev; 2498f0fba2adSLiam Girdwood struct snd_soc_dai *dai; 249932c9ba54SLars-Peter Clausen unsigned int i; 250032c9ba54SLars-Peter Clausen int ret; 2501f0fba2adSLiam Girdwood 2502f110bfc7SLiam Girdwood dev_dbg(dev, "ASoC: dai register %s #%Zu\n", dev_name(dev), count); 25039115171aSMark Brown 2504bb13109dSLars-Peter Clausen component->dai_drv = dai_drv; 2505bb13109dSLars-Peter Clausen component->num_dai = count; 2506bb13109dSLars-Peter Clausen 25079115171aSMark Brown for (i = 0; i < count; i++) { 2508f0fba2adSLiam Girdwood 2509f0fba2adSLiam Girdwood dai = kzalloc(sizeof(struct snd_soc_dai), GFP_KERNEL); 2510c46e0079SAxel Lin if (dai == NULL) { 2511c46e0079SAxel Lin ret = -ENOMEM; 2512c46e0079SAxel Lin goto err; 2513c46e0079SAxel Lin } 2514f0fba2adSLiam Girdwood 251532c9ba54SLars-Peter Clausen /* 251632c9ba54SLars-Peter Clausen * Back in the old days when we still had component-less DAIs, 251732c9ba54SLars-Peter Clausen * instead of having a static name, component-less DAIs would 251832c9ba54SLars-Peter Clausen * inherit the name of the parent device so it is possible to 251932c9ba54SLars-Peter Clausen * register multiple instances of the DAI. We still need to keep 252032c9ba54SLars-Peter Clausen * the same naming style even though those DAIs are not 252132c9ba54SLars-Peter Clausen * component-less anymore. 252232c9ba54SLars-Peter Clausen */ 252332c9ba54SLars-Peter Clausen if (count == 1 && legacy_dai_naming) { 2524db2a4165SFrank Mandarino dai->name = fmt_single_name(dev, &dai->id); 252532c9ba54SLars-Peter Clausen } else { 2526f0fba2adSLiam Girdwood dai->name = fmt_multiple_name(dev, &dai_drv[i]); 252732c9ba54SLars-Peter Clausen if (dai_drv[i].id) 252832c9ba54SLars-Peter Clausen dai->id = dai_drv[i].id; 252932c9ba54SLars-Peter Clausen else 253032c9ba54SLars-Peter Clausen dai->id = i; 253132c9ba54SLars-Peter Clausen } 2532f0fba2adSLiam Girdwood if (dai->name == NULL) { 2533f0fba2adSLiam Girdwood kfree(dai); 253432c9ba54SLars-Peter Clausen ret = -ENOMEM; 25359115171aSMark Brown goto err; 25369115171aSMark Brown } 25379115171aSMark Brown 25386106d129SLars-Peter Clausen dai->component = component; 2539f0fba2adSLiam Girdwood dai->dev = dev; 2540f0fba2adSLiam Girdwood dai->driver = &dai_drv[i]; 2541f0fba2adSLiam Girdwood if (!dai->driver->ops) 2542f0fba2adSLiam Girdwood dai->driver->ops = &null_dai_ops; 2543f0fba2adSLiam Girdwood 25441438c2f6SLars-Peter Clausen list_add(&dai->list, &component->dai_list); 2545054880feSMark Brown 25469115171aSMark Brown dev_dbg(dev, "ASoC: Registered DAI '%s'\n", dai->name); 2547f0fba2adSLiam Girdwood } 2548f0fba2adSLiam Girdwood 25499115171aSMark Brown return 0; 25509115171aSMark Brown 25519115171aSMark Brown err: 255232c9ba54SLars-Peter Clausen snd_soc_unregister_dais(component); 25539115171aSMark Brown 25549115171aSMark Brown return ret; 25559115171aSMark Brown } 25569115171aSMark Brown 255714e8bdebSLars-Peter Clausen static void snd_soc_component_seq_notifier(struct snd_soc_dapm_context *dapm, 255814e8bdebSLars-Peter Clausen enum snd_soc_dapm_type type, int subseq) 2559d191bd8dSKuninori Morimoto { 256014e8bdebSLars-Peter Clausen struct snd_soc_component *component = dapm->component; 2561d191bd8dSKuninori Morimoto 256214e8bdebSLars-Peter Clausen component->driver->seq_notifier(component, type, subseq); 256314e8bdebSLars-Peter Clausen } 2564d191bd8dSKuninori Morimoto 256514e8bdebSLars-Peter Clausen static int snd_soc_component_stream_event(struct snd_soc_dapm_context *dapm, 256614e8bdebSLars-Peter Clausen int event) 256714e8bdebSLars-Peter Clausen { 256814e8bdebSLars-Peter Clausen struct snd_soc_component *component = dapm->component; 256914e8bdebSLars-Peter Clausen 257014e8bdebSLars-Peter Clausen return component->driver->stream_event(component, event); 257114e8bdebSLars-Peter Clausen } 257214e8bdebSLars-Peter Clausen 2573bb13109dSLars-Peter Clausen static int snd_soc_component_initialize(struct snd_soc_component *component, 2574bb13109dSLars-Peter Clausen const struct snd_soc_component_driver *driver, struct device *dev) 2575d191bd8dSKuninori Morimoto { 2576ce0fc93aSLars-Peter Clausen struct snd_soc_dapm_context *dapm; 2577ce0fc93aSLars-Peter Clausen 2578bb13109dSLars-Peter Clausen component->name = fmt_single_name(dev, &component->id); 2579bb13109dSLars-Peter Clausen if (!component->name) { 2580bb13109dSLars-Peter Clausen dev_err(dev, "ASoC: Failed to allocate name\n"); 2581d191bd8dSKuninori Morimoto return -ENOMEM; 2582d191bd8dSKuninori Morimoto } 2583d191bd8dSKuninori Morimoto 2584bb13109dSLars-Peter Clausen component->dev = dev; 2585bb13109dSLars-Peter Clausen component->driver = driver; 258661aca564SLars-Peter Clausen component->probe = component->driver->probe; 258761aca564SLars-Peter Clausen component->remove = component->driver->remove; 2588e2c330b9SLars-Peter Clausen 2589ce0fc93aSLars-Peter Clausen if (!component->dapm_ptr) 2590ce0fc93aSLars-Peter Clausen component->dapm_ptr = &component->dapm; 2591ce0fc93aSLars-Peter Clausen 2592ce0fc93aSLars-Peter Clausen dapm = component->dapm_ptr; 2593ce0fc93aSLars-Peter Clausen dapm->dev = dev; 2594ce0fc93aSLars-Peter Clausen dapm->component = component; 2595ce0fc93aSLars-Peter Clausen dapm->bias_level = SND_SOC_BIAS_OFF; 25965819c2faSLars-Peter Clausen dapm->idle_bias_off = true; 259714e8bdebSLars-Peter Clausen if (driver->seq_notifier) 259814e8bdebSLars-Peter Clausen dapm->seq_notifier = snd_soc_component_seq_notifier; 259914e8bdebSLars-Peter Clausen if (driver->stream_event) 260014e8bdebSLars-Peter Clausen dapm->stream_event = snd_soc_component_stream_event; 2601ce0fc93aSLars-Peter Clausen 260261aca564SLars-Peter Clausen component->controls = driver->controls; 260361aca564SLars-Peter Clausen component->num_controls = driver->num_controls; 260461aca564SLars-Peter Clausen component->dapm_widgets = driver->dapm_widgets; 260561aca564SLars-Peter Clausen component->num_dapm_widgets = driver->num_dapm_widgets; 260661aca564SLars-Peter Clausen component->dapm_routes = driver->dapm_routes; 260761aca564SLars-Peter Clausen component->num_dapm_routes = driver->num_dapm_routes; 260861aca564SLars-Peter Clausen 2609bb13109dSLars-Peter Clausen INIT_LIST_HEAD(&component->dai_list); 2610bb13109dSLars-Peter Clausen mutex_init(&component->io_mutex); 2611bb13109dSLars-Peter Clausen 2612bb13109dSLars-Peter Clausen return 0; 2613d191bd8dSKuninori Morimoto } 2614d191bd8dSKuninori Morimoto 261520feb881SLars-Peter Clausen static void snd_soc_component_setup_regmap(struct snd_soc_component *component) 2616886f5692SLars-Peter Clausen { 2617886f5692SLars-Peter Clausen int val_bytes = regmap_get_val_bytes(component->regmap); 261820feb881SLars-Peter Clausen 2619886f5692SLars-Peter Clausen /* Errors are legitimate for non-integer byte multiples */ 2620886f5692SLars-Peter Clausen if (val_bytes > 0) 2621886f5692SLars-Peter Clausen component->val_bytes = val_bytes; 2622886f5692SLars-Peter Clausen } 262320feb881SLars-Peter Clausen 2624e874bf5fSLars-Peter Clausen #ifdef CONFIG_REGMAP 2625e874bf5fSLars-Peter Clausen 262620feb881SLars-Peter Clausen /** 262720feb881SLars-Peter Clausen * snd_soc_component_init_regmap() - Initialize regmap instance for the component 262820feb881SLars-Peter Clausen * @component: The component for which to initialize the regmap instance 262920feb881SLars-Peter Clausen * @regmap: The regmap instance that should be used by the component 263020feb881SLars-Peter Clausen * 263120feb881SLars-Peter Clausen * This function allows deferred assignment of the regmap instance that is 263220feb881SLars-Peter Clausen * associated with the component. Only use this if the regmap instance is not 263320feb881SLars-Peter Clausen * yet ready when the component is registered. The function must also be called 263420feb881SLars-Peter Clausen * before the first IO attempt of the component. 263520feb881SLars-Peter Clausen */ 263620feb881SLars-Peter Clausen void snd_soc_component_init_regmap(struct snd_soc_component *component, 263720feb881SLars-Peter Clausen struct regmap *regmap) 263820feb881SLars-Peter Clausen { 263920feb881SLars-Peter Clausen component->regmap = regmap; 264020feb881SLars-Peter Clausen snd_soc_component_setup_regmap(component); 2641886f5692SLars-Peter Clausen } 264220feb881SLars-Peter Clausen EXPORT_SYMBOL_GPL(snd_soc_component_init_regmap); 264320feb881SLars-Peter Clausen 264420feb881SLars-Peter Clausen /** 264520feb881SLars-Peter Clausen * snd_soc_component_exit_regmap() - De-initialize regmap instance for the component 264620feb881SLars-Peter Clausen * @component: The component for which to de-initialize the regmap instance 264720feb881SLars-Peter Clausen * 264820feb881SLars-Peter Clausen * Calls regmap_exit() on the regmap instance associated to the component and 264920feb881SLars-Peter Clausen * removes the regmap instance from the component. 265020feb881SLars-Peter Clausen * 265120feb881SLars-Peter Clausen * This function should only be used if snd_soc_component_init_regmap() was used 265220feb881SLars-Peter Clausen * to initialize the regmap instance. 265320feb881SLars-Peter Clausen */ 265420feb881SLars-Peter Clausen void snd_soc_component_exit_regmap(struct snd_soc_component *component) 265520feb881SLars-Peter Clausen { 265620feb881SLars-Peter Clausen regmap_exit(component->regmap); 265720feb881SLars-Peter Clausen component->regmap = NULL; 265820feb881SLars-Peter Clausen } 265920feb881SLars-Peter Clausen EXPORT_SYMBOL_GPL(snd_soc_component_exit_regmap); 2660886f5692SLars-Peter Clausen 2661e874bf5fSLars-Peter Clausen #endif 2662d191bd8dSKuninori Morimoto 2663bb13109dSLars-Peter Clausen static void snd_soc_component_add_unlocked(struct snd_soc_component *component) 2664bb13109dSLars-Peter Clausen { 266520feb881SLars-Peter Clausen if (!component->write && !component->read) { 266620feb881SLars-Peter Clausen if (!component->regmap) 266720feb881SLars-Peter Clausen component->regmap = dev_get_regmap(component->dev, NULL); 266820feb881SLars-Peter Clausen if (component->regmap) 266920feb881SLars-Peter Clausen snd_soc_component_setup_regmap(component); 267020feb881SLars-Peter Clausen } 2671886f5692SLars-Peter Clausen 2672bb13109dSLars-Peter Clausen list_add(&component->list, &component_list); 2673d191bd8dSKuninori Morimoto } 2674d191bd8dSKuninori Morimoto 2675bb13109dSLars-Peter Clausen static void snd_soc_component_add(struct snd_soc_component *component) 2676bb13109dSLars-Peter Clausen { 2677d191bd8dSKuninori Morimoto mutex_lock(&client_mutex); 2678bb13109dSLars-Peter Clausen snd_soc_component_add_unlocked(component); 2679d191bd8dSKuninori Morimoto mutex_unlock(&client_mutex); 2680bb13109dSLars-Peter Clausen } 2681d191bd8dSKuninori Morimoto 2682bb13109dSLars-Peter Clausen static void snd_soc_component_cleanup(struct snd_soc_component *component) 2683bb13109dSLars-Peter Clausen { 2684bb13109dSLars-Peter Clausen snd_soc_unregister_dais(component); 2685bb13109dSLars-Peter Clausen kfree(component->name); 2686bb13109dSLars-Peter Clausen } 2687d191bd8dSKuninori Morimoto 2688bb13109dSLars-Peter Clausen static void snd_soc_component_del_unlocked(struct snd_soc_component *component) 2689bb13109dSLars-Peter Clausen { 2690bb13109dSLars-Peter Clausen list_del(&component->list); 2691bb13109dSLars-Peter Clausen } 2692d191bd8dSKuninori Morimoto 2693bb13109dSLars-Peter Clausen static void snd_soc_component_del(struct snd_soc_component *component) 2694bb13109dSLars-Peter Clausen { 2695bb13109dSLars-Peter Clausen mutex_lock(&client_mutex); 2696bb13109dSLars-Peter Clausen snd_soc_component_del_unlocked(component); 2697bb13109dSLars-Peter Clausen mutex_unlock(&client_mutex); 2698d191bd8dSKuninori Morimoto } 2699d191bd8dSKuninori Morimoto 2700d191bd8dSKuninori Morimoto int snd_soc_register_component(struct device *dev, 2701d191bd8dSKuninori Morimoto const struct snd_soc_component_driver *cmpnt_drv, 2702d191bd8dSKuninori Morimoto struct snd_soc_dai_driver *dai_drv, 2703d191bd8dSKuninori Morimoto int num_dai) 2704d191bd8dSKuninori Morimoto { 2705d191bd8dSKuninori Morimoto struct snd_soc_component *cmpnt; 2706bb13109dSLars-Peter Clausen int ret; 2707d191bd8dSKuninori Morimoto 2708bb13109dSLars-Peter Clausen cmpnt = kzalloc(sizeof(*cmpnt), GFP_KERNEL); 2709d191bd8dSKuninori Morimoto if (!cmpnt) { 2710d191bd8dSKuninori Morimoto dev_err(dev, "ASoC: Failed to allocate memory\n"); 2711d191bd8dSKuninori Morimoto return -ENOMEM; 2712d191bd8dSKuninori Morimoto } 2713d191bd8dSKuninori Morimoto 2714bb13109dSLars-Peter Clausen ret = snd_soc_component_initialize(cmpnt, cmpnt_drv, dev); 2715bb13109dSLars-Peter Clausen if (ret) 2716bb13109dSLars-Peter Clausen goto err_free; 2717bb13109dSLars-Peter Clausen 27183d59400fSLars-Peter Clausen cmpnt->ignore_pmdown_time = true; 271998e639fbSLars-Peter Clausen cmpnt->registered_as_component = true; 27203d59400fSLars-Peter Clausen 2721bb13109dSLars-Peter Clausen ret = snd_soc_register_dais(cmpnt, dai_drv, num_dai, true); 2722bb13109dSLars-Peter Clausen if (ret < 0) { 2723bb13109dSLars-Peter Clausen dev_err(dev, "ASoC: Failed to regster DAIs: %d\n", ret); 2724bb13109dSLars-Peter Clausen goto err_cleanup; 2725bb13109dSLars-Peter Clausen } 2726bb13109dSLars-Peter Clausen 2727bb13109dSLars-Peter Clausen snd_soc_component_add(cmpnt); 2728bb13109dSLars-Peter Clausen 2729bb13109dSLars-Peter Clausen return 0; 2730bb13109dSLars-Peter Clausen 2731bb13109dSLars-Peter Clausen err_cleanup: 2732bb13109dSLars-Peter Clausen snd_soc_component_cleanup(cmpnt); 2733bb13109dSLars-Peter Clausen err_free: 2734bb13109dSLars-Peter Clausen kfree(cmpnt); 2735bb13109dSLars-Peter Clausen return ret; 2736d191bd8dSKuninori Morimoto } 2737d191bd8dSKuninori Morimoto EXPORT_SYMBOL_GPL(snd_soc_register_component); 2738d191bd8dSKuninori Morimoto 2739d191bd8dSKuninori Morimoto /** 2740d191bd8dSKuninori Morimoto * snd_soc_unregister_component - Unregister a component from the ASoC core 2741d191bd8dSKuninori Morimoto * 2742d191bd8dSKuninori Morimoto */ 2743d191bd8dSKuninori Morimoto void snd_soc_unregister_component(struct device *dev) 2744d191bd8dSKuninori Morimoto { 2745d191bd8dSKuninori Morimoto struct snd_soc_component *cmpnt; 2746d191bd8dSKuninori Morimoto 2747d191bd8dSKuninori Morimoto list_for_each_entry(cmpnt, &component_list, list) { 274898e639fbSLars-Peter Clausen if (dev == cmpnt->dev && cmpnt->registered_as_component) 2749d191bd8dSKuninori Morimoto goto found; 2750d191bd8dSKuninori Morimoto } 2751d191bd8dSKuninori Morimoto return; 2752d191bd8dSKuninori Morimoto 2753d191bd8dSKuninori Morimoto found: 2754bb13109dSLars-Peter Clausen snd_soc_component_del(cmpnt); 2755bb13109dSLars-Peter Clausen snd_soc_component_cleanup(cmpnt); 2756bb13109dSLars-Peter Clausen kfree(cmpnt); 2757d191bd8dSKuninori Morimoto } 2758d191bd8dSKuninori Morimoto EXPORT_SYMBOL_GPL(snd_soc_unregister_component); 2759d191bd8dSKuninori Morimoto 2760f1d45cc3SLars-Peter Clausen static int snd_soc_platform_drv_probe(struct snd_soc_component *component) 2761e2c330b9SLars-Peter Clausen { 2762e2c330b9SLars-Peter Clausen struct snd_soc_platform *platform = snd_soc_component_to_platform(component); 2763e2c330b9SLars-Peter Clausen 2764f1d45cc3SLars-Peter Clausen return platform->driver->probe(platform); 2765e2c330b9SLars-Peter Clausen } 2766e2c330b9SLars-Peter Clausen 2767f1d45cc3SLars-Peter Clausen static void snd_soc_platform_drv_remove(struct snd_soc_component *component) 2768e2c330b9SLars-Peter Clausen { 2769e2c330b9SLars-Peter Clausen struct snd_soc_platform *platform = snd_soc_component_to_platform(component); 2770e2c330b9SLars-Peter Clausen 2771f1d45cc3SLars-Peter Clausen platform->driver->remove(platform); 2772e2c330b9SLars-Peter Clausen } 2773e2c330b9SLars-Peter Clausen 2774d191bd8dSKuninori Morimoto /** 277571a45cdaSLars-Peter Clausen * snd_soc_add_platform - Add a platform to the ASoC core 277671a45cdaSLars-Peter Clausen * @dev: The parent device for the platform 277771a45cdaSLars-Peter Clausen * @platform: The platform to add 277871a45cdaSLars-Peter Clausen * @platform_driver: The driver for the platform 277912a48a8cSMark Brown */ 278071a45cdaSLars-Peter Clausen int snd_soc_add_platform(struct device *dev, struct snd_soc_platform *platform, 2781d79e57dbSLars-Peter Clausen const struct snd_soc_platform_driver *platform_drv) 278212a48a8cSMark Brown { 2783b37f1d12SLars-Peter Clausen int ret; 2784b37f1d12SLars-Peter Clausen 2785bb13109dSLars-Peter Clausen ret = snd_soc_component_initialize(&platform->component, 2786bb13109dSLars-Peter Clausen &platform_drv->component_driver, dev); 2787bb13109dSLars-Peter Clausen if (ret) 2788bb13109dSLars-Peter Clausen return ret; 2789f0fba2adSLiam Girdwood 2790f0fba2adSLiam Girdwood platform->dev = dev; 2791f0fba2adSLiam Girdwood platform->driver = platform_drv; 2792f1d45cc3SLars-Peter Clausen 2793f1d45cc3SLars-Peter Clausen if (platform_drv->probe) 2794f1d45cc3SLars-Peter Clausen platform->component.probe = snd_soc_platform_drv_probe; 2795f1d45cc3SLars-Peter Clausen if (platform_drv->remove) 2796f1d45cc3SLars-Peter Clausen platform->component.remove = snd_soc_platform_drv_remove; 279712a48a8cSMark Brown 279881c7cfd1SLars-Peter Clausen #ifdef CONFIG_DEBUG_FS 279981c7cfd1SLars-Peter Clausen platform->component.debugfs_prefix = "platform"; 280081c7cfd1SLars-Peter Clausen #endif 280112a48a8cSMark Brown 280212a48a8cSMark Brown mutex_lock(&client_mutex); 2803bb13109dSLars-Peter Clausen snd_soc_component_add_unlocked(&platform->component); 280412a48a8cSMark Brown list_add(&platform->list, &platform_list); 280512a48a8cSMark Brown mutex_unlock(&client_mutex); 280612a48a8cSMark Brown 2807f4333203SLars-Peter Clausen dev_dbg(dev, "ASoC: Registered platform '%s'\n", 2808f4333203SLars-Peter Clausen platform->component.name); 280912a48a8cSMark Brown 281012a48a8cSMark Brown return 0; 281112a48a8cSMark Brown } 281271a45cdaSLars-Peter Clausen EXPORT_SYMBOL_GPL(snd_soc_add_platform); 281371a45cdaSLars-Peter Clausen 281471a45cdaSLars-Peter Clausen /** 281571a45cdaSLars-Peter Clausen * snd_soc_register_platform - Register a platform with the ASoC core 281671a45cdaSLars-Peter Clausen * 281771a45cdaSLars-Peter Clausen * @platform: platform to register 281871a45cdaSLars-Peter Clausen */ 281971a45cdaSLars-Peter Clausen int snd_soc_register_platform(struct device *dev, 282071a45cdaSLars-Peter Clausen const struct snd_soc_platform_driver *platform_drv) 282171a45cdaSLars-Peter Clausen { 282271a45cdaSLars-Peter Clausen struct snd_soc_platform *platform; 282371a45cdaSLars-Peter Clausen int ret; 282471a45cdaSLars-Peter Clausen 282571a45cdaSLars-Peter Clausen dev_dbg(dev, "ASoC: platform register %s\n", dev_name(dev)); 282671a45cdaSLars-Peter Clausen 282771a45cdaSLars-Peter Clausen platform = kzalloc(sizeof(struct snd_soc_platform), GFP_KERNEL); 282871a45cdaSLars-Peter Clausen if (platform == NULL) 282971a45cdaSLars-Peter Clausen return -ENOMEM; 283071a45cdaSLars-Peter Clausen 283171a45cdaSLars-Peter Clausen ret = snd_soc_add_platform(dev, platform, platform_drv); 283271a45cdaSLars-Peter Clausen if (ret) 283371a45cdaSLars-Peter Clausen kfree(platform); 283471a45cdaSLars-Peter Clausen 283571a45cdaSLars-Peter Clausen return ret; 283671a45cdaSLars-Peter Clausen } 283712a48a8cSMark Brown EXPORT_SYMBOL_GPL(snd_soc_register_platform); 283812a48a8cSMark Brown 283912a48a8cSMark Brown /** 284071a45cdaSLars-Peter Clausen * snd_soc_remove_platform - Remove a platform from the ASoC core 284171a45cdaSLars-Peter Clausen * @platform: the platform to remove 284271a45cdaSLars-Peter Clausen */ 284371a45cdaSLars-Peter Clausen void snd_soc_remove_platform(struct snd_soc_platform *platform) 284471a45cdaSLars-Peter Clausen { 2845b37f1d12SLars-Peter Clausen 284671a45cdaSLars-Peter Clausen mutex_lock(&client_mutex); 284771a45cdaSLars-Peter Clausen list_del(&platform->list); 2848bb13109dSLars-Peter Clausen snd_soc_component_del_unlocked(&platform->component); 284971a45cdaSLars-Peter Clausen mutex_unlock(&client_mutex); 285071a45cdaSLars-Peter Clausen 285171a45cdaSLars-Peter Clausen dev_dbg(platform->dev, "ASoC: Unregistered platform '%s'\n", 2852f4333203SLars-Peter Clausen platform->component.name); 2853decc27b0SDaniel Mack 2854decc27b0SDaniel Mack snd_soc_component_cleanup(&platform->component); 285571a45cdaSLars-Peter Clausen } 285671a45cdaSLars-Peter Clausen EXPORT_SYMBOL_GPL(snd_soc_remove_platform); 285771a45cdaSLars-Peter Clausen 285871a45cdaSLars-Peter Clausen struct snd_soc_platform *snd_soc_lookup_platform(struct device *dev) 285971a45cdaSLars-Peter Clausen { 286071a45cdaSLars-Peter Clausen struct snd_soc_platform *platform; 286171a45cdaSLars-Peter Clausen 286271a45cdaSLars-Peter Clausen list_for_each_entry(platform, &platform_list, list) { 286371a45cdaSLars-Peter Clausen if (dev == platform->dev) 286471a45cdaSLars-Peter Clausen return platform; 286571a45cdaSLars-Peter Clausen } 286671a45cdaSLars-Peter Clausen 286771a45cdaSLars-Peter Clausen return NULL; 286871a45cdaSLars-Peter Clausen } 286971a45cdaSLars-Peter Clausen EXPORT_SYMBOL_GPL(snd_soc_lookup_platform); 287071a45cdaSLars-Peter Clausen 287171a45cdaSLars-Peter Clausen /** 287212a48a8cSMark Brown * snd_soc_unregister_platform - Unregister a platform from the ASoC core 287312a48a8cSMark Brown * 2874ac11a2b3SMark Brown * @platform: platform to unregister 287512a48a8cSMark Brown */ 2876f0fba2adSLiam Girdwood void snd_soc_unregister_platform(struct device *dev) 287712a48a8cSMark Brown { 2878f0fba2adSLiam Girdwood struct snd_soc_platform *platform; 2879f0fba2adSLiam Girdwood 288071a45cdaSLars-Peter Clausen platform = snd_soc_lookup_platform(dev); 288171a45cdaSLars-Peter Clausen if (!platform) 2882f0fba2adSLiam Girdwood return; 2883f0fba2adSLiam Girdwood 288471a45cdaSLars-Peter Clausen snd_soc_remove_platform(platform); 2885f0fba2adSLiam Girdwood kfree(platform); 288612a48a8cSMark Brown } 288712a48a8cSMark Brown EXPORT_SYMBOL_GPL(snd_soc_unregister_platform); 288812a48a8cSMark Brown 2889151ab22cSMark Brown static u64 codec_format_map[] = { 2890151ab22cSMark Brown SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S16_BE, 2891151ab22cSMark Brown SNDRV_PCM_FMTBIT_U16_LE | SNDRV_PCM_FMTBIT_U16_BE, 2892151ab22cSMark Brown SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S24_BE, 2893151ab22cSMark Brown SNDRV_PCM_FMTBIT_U24_LE | SNDRV_PCM_FMTBIT_U24_BE, 2894151ab22cSMark Brown SNDRV_PCM_FMTBIT_S32_LE | SNDRV_PCM_FMTBIT_S32_BE, 2895151ab22cSMark Brown SNDRV_PCM_FMTBIT_U32_LE | SNDRV_PCM_FMTBIT_U32_BE, 2896151ab22cSMark Brown SNDRV_PCM_FMTBIT_S24_3LE | SNDRV_PCM_FMTBIT_U24_3BE, 2897151ab22cSMark Brown SNDRV_PCM_FMTBIT_U24_3LE | SNDRV_PCM_FMTBIT_U24_3BE, 2898151ab22cSMark Brown SNDRV_PCM_FMTBIT_S20_3LE | SNDRV_PCM_FMTBIT_S20_3BE, 2899151ab22cSMark Brown SNDRV_PCM_FMTBIT_U20_3LE | SNDRV_PCM_FMTBIT_U20_3BE, 2900151ab22cSMark Brown SNDRV_PCM_FMTBIT_S18_3LE | SNDRV_PCM_FMTBIT_S18_3BE, 2901151ab22cSMark Brown SNDRV_PCM_FMTBIT_U18_3LE | SNDRV_PCM_FMTBIT_U18_3BE, 2902151ab22cSMark Brown SNDRV_PCM_FMTBIT_FLOAT_LE | SNDRV_PCM_FMTBIT_FLOAT_BE, 2903151ab22cSMark Brown SNDRV_PCM_FMTBIT_FLOAT64_LE | SNDRV_PCM_FMTBIT_FLOAT64_BE, 2904151ab22cSMark Brown SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_LE 2905151ab22cSMark Brown | SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_BE, 2906151ab22cSMark Brown }; 2907151ab22cSMark Brown 2908151ab22cSMark Brown /* Fix up the DAI formats for endianness: codecs don't actually see 2909151ab22cSMark Brown * the endianness of the data but we're using the CPU format 2910151ab22cSMark Brown * definitions which do need to include endianness so we ensure that 2911151ab22cSMark Brown * codec DAIs always have both big and little endian variants set. 2912151ab22cSMark Brown */ 2913151ab22cSMark Brown static void fixup_codec_formats(struct snd_soc_pcm_stream *stream) 2914151ab22cSMark Brown { 2915151ab22cSMark Brown int i; 2916151ab22cSMark Brown 2917151ab22cSMark Brown for (i = 0; i < ARRAY_SIZE(codec_format_map); i++) 2918151ab22cSMark Brown if (stream->formats & codec_format_map[i]) 2919151ab22cSMark Brown stream->formats |= codec_format_map[i]; 2920151ab22cSMark Brown } 2921151ab22cSMark Brown 2922f1d45cc3SLars-Peter Clausen static int snd_soc_codec_drv_probe(struct snd_soc_component *component) 2923f1d45cc3SLars-Peter Clausen { 2924f1d45cc3SLars-Peter Clausen struct snd_soc_codec *codec = snd_soc_component_to_codec(component); 2925f1d45cc3SLars-Peter Clausen 2926f1d45cc3SLars-Peter Clausen return codec->driver->probe(codec); 2927f1d45cc3SLars-Peter Clausen } 2928f1d45cc3SLars-Peter Clausen 2929f1d45cc3SLars-Peter Clausen static void snd_soc_codec_drv_remove(struct snd_soc_component *component) 2930f1d45cc3SLars-Peter Clausen { 2931f1d45cc3SLars-Peter Clausen struct snd_soc_codec *codec = snd_soc_component_to_codec(component); 2932f1d45cc3SLars-Peter Clausen 2933f1d45cc3SLars-Peter Clausen codec->driver->remove(codec); 2934f1d45cc3SLars-Peter Clausen } 2935f1d45cc3SLars-Peter Clausen 2936e2c330b9SLars-Peter Clausen static int snd_soc_codec_drv_write(struct snd_soc_component *component, 2937e2c330b9SLars-Peter Clausen unsigned int reg, unsigned int val) 2938e2c330b9SLars-Peter Clausen { 2939e2c330b9SLars-Peter Clausen struct snd_soc_codec *codec = snd_soc_component_to_codec(component); 2940e2c330b9SLars-Peter Clausen 2941e2c330b9SLars-Peter Clausen return codec->driver->write(codec, reg, val); 2942e2c330b9SLars-Peter Clausen } 2943e2c330b9SLars-Peter Clausen 2944e2c330b9SLars-Peter Clausen static int snd_soc_codec_drv_read(struct snd_soc_component *component, 2945e2c330b9SLars-Peter Clausen unsigned int reg, unsigned int *val) 2946e2c330b9SLars-Peter Clausen { 2947e2c330b9SLars-Peter Clausen struct snd_soc_codec *codec = snd_soc_component_to_codec(component); 2948e2c330b9SLars-Peter Clausen 2949e2c330b9SLars-Peter Clausen *val = codec->driver->read(codec, reg); 2950e2c330b9SLars-Peter Clausen 2951e2c330b9SLars-Peter Clausen return 0; 2952e2c330b9SLars-Peter Clausen } 2953e2c330b9SLars-Peter Clausen 295468f831c2SLars-Peter Clausen static int snd_soc_codec_set_bias_level(struct snd_soc_dapm_context *dapm, 295568f831c2SLars-Peter Clausen enum snd_soc_bias_level level) 295668f831c2SLars-Peter Clausen { 295768f831c2SLars-Peter Clausen struct snd_soc_codec *codec = snd_soc_dapm_to_codec(dapm); 295868f831c2SLars-Peter Clausen 295968f831c2SLars-Peter Clausen return codec->driver->set_bias_level(codec, level); 296068f831c2SLars-Peter Clausen } 296168f831c2SLars-Peter Clausen 29620d0cf00aSMark Brown /** 29630d0cf00aSMark Brown * snd_soc_register_codec - Register a codec with the ASoC core 29640d0cf00aSMark Brown * 2965ac11a2b3SMark Brown * @codec: codec to register 29660d0cf00aSMark Brown */ 2967f0fba2adSLiam Girdwood int snd_soc_register_codec(struct device *dev, 2968001ae4c0SMark Brown const struct snd_soc_codec_driver *codec_drv, 2969001ae4c0SMark Brown struct snd_soc_dai_driver *dai_drv, 2970001ae4c0SMark Brown int num_dai) 29710d0cf00aSMark Brown { 2972f0fba2adSLiam Girdwood struct snd_soc_codec *codec; 2973bb13109dSLars-Peter Clausen struct snd_soc_dai *dai; 2974f0fba2adSLiam Girdwood int ret, i; 2975151ab22cSMark Brown 2976f0fba2adSLiam Girdwood dev_dbg(dev, "codec register %s\n", dev_name(dev)); 29770d0cf00aSMark Brown 2978f0fba2adSLiam Girdwood codec = kzalloc(sizeof(struct snd_soc_codec), GFP_KERNEL); 2979f0fba2adSLiam Girdwood if (codec == NULL) 2980f0fba2adSLiam Girdwood return -ENOMEM; 29810d0cf00aSMark Brown 2982ce0fc93aSLars-Peter Clausen codec->component.dapm_ptr = &codec->dapm; 2983f1d45cc3SLars-Peter Clausen codec->component.codec = codec; 2984ce0fc93aSLars-Peter Clausen 2985bb13109dSLars-Peter Clausen ret = snd_soc_component_initialize(&codec->component, 2986bb13109dSLars-Peter Clausen &codec_drv->component_driver, dev); 2987bb13109dSLars-Peter Clausen if (ret) 2988bb13109dSLars-Peter Clausen goto err_free; 2989151ab22cSMark Brown 2990f1d45cc3SLars-Peter Clausen if (codec_drv->controls) { 2991f1d45cc3SLars-Peter Clausen codec->component.controls = codec_drv->controls; 2992f1d45cc3SLars-Peter Clausen codec->component.num_controls = codec_drv->num_controls; 2993f1d45cc3SLars-Peter Clausen } 2994f1d45cc3SLars-Peter Clausen if (codec_drv->dapm_widgets) { 2995f1d45cc3SLars-Peter Clausen codec->component.dapm_widgets = codec_drv->dapm_widgets; 2996f1d45cc3SLars-Peter Clausen codec->component.num_dapm_widgets = codec_drv->num_dapm_widgets; 2997f1d45cc3SLars-Peter Clausen } 2998f1d45cc3SLars-Peter Clausen if (codec_drv->dapm_routes) { 2999f1d45cc3SLars-Peter Clausen codec->component.dapm_routes = codec_drv->dapm_routes; 3000f1d45cc3SLars-Peter Clausen codec->component.num_dapm_routes = codec_drv->num_dapm_routes; 3001f1d45cc3SLars-Peter Clausen } 3002f1d45cc3SLars-Peter Clausen 3003f1d45cc3SLars-Peter Clausen if (codec_drv->probe) 3004f1d45cc3SLars-Peter Clausen codec->component.probe = snd_soc_codec_drv_probe; 3005f1d45cc3SLars-Peter Clausen if (codec_drv->remove) 3006f1d45cc3SLars-Peter Clausen codec->component.remove = snd_soc_codec_drv_remove; 3007e2c330b9SLars-Peter Clausen if (codec_drv->write) 3008e2c330b9SLars-Peter Clausen codec->component.write = snd_soc_codec_drv_write; 3009e2c330b9SLars-Peter Clausen if (codec_drv->read) 3010e2c330b9SLars-Peter Clausen codec->component.read = snd_soc_codec_drv_read; 30113d59400fSLars-Peter Clausen codec->component.ignore_pmdown_time = codec_drv->ignore_pmdown_time; 30125819c2faSLars-Peter Clausen codec->dapm.idle_bias_off = codec_drv->idle_bias_off; 301386dbf2acSLars-Peter Clausen codec->dapm.suspend_bias_off = codec_drv->suspend_bias_off; 301414e8bdebSLars-Peter Clausen if (codec_drv->seq_notifier) 3015474b62d6SMark Brown codec->dapm.seq_notifier = codec_drv->seq_notifier; 301668f831c2SLars-Peter Clausen if (codec_drv->set_bias_level) 301768f831c2SLars-Peter Clausen codec->dapm.set_bias_level = snd_soc_codec_set_bias_level; 3018f0fba2adSLiam Girdwood codec->dev = dev; 3019f0fba2adSLiam Girdwood codec->driver = codec_drv; 3020e2c330b9SLars-Peter Clausen codec->component.val_bytes = codec_drv->reg_word_size; 3021f0fba2adSLiam Girdwood 302281c7cfd1SLars-Peter Clausen #ifdef CONFIG_DEBUG_FS 302381c7cfd1SLars-Peter Clausen codec->component.init_debugfs = soc_init_codec_debugfs; 302481c7cfd1SLars-Peter Clausen codec->component.debugfs_prefix = "codec"; 302581c7cfd1SLars-Peter Clausen #endif 3026a39f75f7SXiubo Li 3027a39f75f7SXiubo Li if (codec_drv->get_regmap) 3028886f5692SLars-Peter Clausen codec->component.regmap = codec_drv->get_regmap(dev); 3029a39f75f7SXiubo Li 3030f0fba2adSLiam Girdwood for (i = 0; i < num_dai; i++) { 3031f0fba2adSLiam Girdwood fixup_codec_formats(&dai_drv[i].playback); 3032f0fba2adSLiam Girdwood fixup_codec_formats(&dai_drv[i].capture); 3033f0fba2adSLiam Girdwood } 3034f0fba2adSLiam Girdwood 3035bb13109dSLars-Peter Clausen ret = snd_soc_register_dais(&codec->component, dai_drv, num_dai, false); 3036bb13109dSLars-Peter Clausen if (ret < 0) { 3037bb13109dSLars-Peter Clausen dev_err(dev, "ASoC: Failed to regster DAIs: %d\n", ret); 3038bb13109dSLars-Peter Clausen goto err_cleanup; 3039bb13109dSLars-Peter Clausen } 3040bb13109dSLars-Peter Clausen 3041bb13109dSLars-Peter Clausen list_for_each_entry(dai, &codec->component.dai_list, list) 3042bb13109dSLars-Peter Clausen dai->codec = codec; 3043bb13109dSLars-Peter Clausen 3044054880feSMark Brown mutex_lock(&client_mutex); 3045bb13109dSLars-Peter Clausen snd_soc_component_add_unlocked(&codec->component); 3046054880feSMark Brown list_add(&codec->list, &codec_list); 3047054880feSMark Brown mutex_unlock(&client_mutex); 3048054880feSMark Brown 3049f4333203SLars-Peter Clausen dev_dbg(codec->dev, "ASoC: Registered codec '%s'\n", 3050f4333203SLars-Peter Clausen codec->component.name); 30510d0cf00aSMark Brown return 0; 3052f0fba2adSLiam Girdwood 3053bb13109dSLars-Peter Clausen err_cleanup: 3054bb13109dSLars-Peter Clausen snd_soc_component_cleanup(&codec->component); 3055bb13109dSLars-Peter Clausen err_free: 3056f0fba2adSLiam Girdwood kfree(codec); 3057f0fba2adSLiam Girdwood return ret; 30580d0cf00aSMark Brown } 30590d0cf00aSMark Brown EXPORT_SYMBOL_GPL(snd_soc_register_codec); 30600d0cf00aSMark Brown 30610d0cf00aSMark Brown /** 30620d0cf00aSMark Brown * snd_soc_unregister_codec - Unregister a codec from the ASoC core 30630d0cf00aSMark Brown * 3064ac11a2b3SMark Brown * @codec: codec to unregister 30650d0cf00aSMark Brown */ 3066f0fba2adSLiam Girdwood void snd_soc_unregister_codec(struct device *dev) 30670d0cf00aSMark Brown { 3068f0fba2adSLiam Girdwood struct snd_soc_codec *codec; 3069f0fba2adSLiam Girdwood 3070f0fba2adSLiam Girdwood list_for_each_entry(codec, &codec_list, list) { 3071f0fba2adSLiam Girdwood if (dev == codec->dev) 3072f0fba2adSLiam Girdwood goto found; 3073f0fba2adSLiam Girdwood } 3074f0fba2adSLiam Girdwood return; 3075f0fba2adSLiam Girdwood 3076f0fba2adSLiam Girdwood found: 3077f0fba2adSLiam Girdwood 30780d0cf00aSMark Brown mutex_lock(&client_mutex); 30790d0cf00aSMark Brown list_del(&codec->list); 3080bb13109dSLars-Peter Clausen snd_soc_component_del_unlocked(&codec->component); 30810d0cf00aSMark Brown mutex_unlock(&client_mutex); 30820d0cf00aSMark Brown 3083f4333203SLars-Peter Clausen dev_dbg(codec->dev, "ASoC: Unregistered codec '%s'\n", 3084f4333203SLars-Peter Clausen codec->component.name); 3085f0fba2adSLiam Girdwood 3086bb13109dSLars-Peter Clausen snd_soc_component_cleanup(&codec->component); 30877a30a3dbSDimitris Papastamos snd_soc_cache_exit(codec); 3088f0fba2adSLiam Girdwood kfree(codec); 30890d0cf00aSMark Brown } 30900d0cf00aSMark Brown EXPORT_SYMBOL_GPL(snd_soc_unregister_codec); 30910d0cf00aSMark Brown 3092bec4fa05SStephen Warren /* Retrieve a card's name from device tree */ 3093bec4fa05SStephen Warren int snd_soc_of_parse_card_name(struct snd_soc_card *card, 3094bec4fa05SStephen Warren const char *propname) 3095bec4fa05SStephen Warren { 30967e07e7c0STushar Behera struct device_node *np; 3097bec4fa05SStephen Warren int ret; 3098bec4fa05SStephen Warren 30997e07e7c0STushar Behera if (!card->dev) { 31007e07e7c0STushar Behera pr_err("card->dev is not set before calling %s\n", __func__); 31017e07e7c0STushar Behera return -EINVAL; 31027e07e7c0STushar Behera } 31037e07e7c0STushar Behera 31047e07e7c0STushar Behera np = card->dev->of_node; 31057e07e7c0STushar Behera 3106bec4fa05SStephen Warren ret = of_property_read_string_index(np, propname, 0, &card->name); 3107bec4fa05SStephen Warren /* 3108bec4fa05SStephen Warren * EINVAL means the property does not exist. This is fine providing 3109bec4fa05SStephen Warren * card->name was previously set, which is checked later in 3110bec4fa05SStephen Warren * snd_soc_register_card. 3111bec4fa05SStephen Warren */ 3112bec4fa05SStephen Warren if (ret < 0 && ret != -EINVAL) { 3113bec4fa05SStephen Warren dev_err(card->dev, 3114f110bfc7SLiam Girdwood "ASoC: Property '%s' could not be read: %d\n", 3115bec4fa05SStephen Warren propname, ret); 3116bec4fa05SStephen Warren return ret; 3117bec4fa05SStephen Warren } 3118bec4fa05SStephen Warren 3119bec4fa05SStephen Warren return 0; 3120bec4fa05SStephen Warren } 3121bec4fa05SStephen Warren EXPORT_SYMBOL_GPL(snd_soc_of_parse_card_name); 3122bec4fa05SStephen Warren 31239a6d4860SXiubo Li static const struct snd_soc_dapm_widget simple_widgets[] = { 31249a6d4860SXiubo Li SND_SOC_DAPM_MIC("Microphone", NULL), 31259a6d4860SXiubo Li SND_SOC_DAPM_LINE("Line", NULL), 31269a6d4860SXiubo Li SND_SOC_DAPM_HP("Headphone", NULL), 31279a6d4860SXiubo Li SND_SOC_DAPM_SPK("Speaker", NULL), 31289a6d4860SXiubo Li }; 31299a6d4860SXiubo Li 31309a6d4860SXiubo Li int snd_soc_of_parse_audio_simple_widgets(struct snd_soc_card *card, 31319a6d4860SXiubo Li const char *propname) 31329a6d4860SXiubo Li { 31339a6d4860SXiubo Li struct device_node *np = card->dev->of_node; 31349a6d4860SXiubo Li struct snd_soc_dapm_widget *widgets; 31359a6d4860SXiubo Li const char *template, *wname; 31369a6d4860SXiubo Li int i, j, num_widgets, ret; 31379a6d4860SXiubo Li 31389a6d4860SXiubo Li num_widgets = of_property_count_strings(np, propname); 31399a6d4860SXiubo Li if (num_widgets < 0) { 31409a6d4860SXiubo Li dev_err(card->dev, 31419a6d4860SXiubo Li "ASoC: Property '%s' does not exist\n", propname); 31429a6d4860SXiubo Li return -EINVAL; 31439a6d4860SXiubo Li } 31449a6d4860SXiubo Li if (num_widgets & 1) { 31459a6d4860SXiubo Li dev_err(card->dev, 31469a6d4860SXiubo Li "ASoC: Property '%s' length is not even\n", propname); 31479a6d4860SXiubo Li return -EINVAL; 31489a6d4860SXiubo Li } 31499a6d4860SXiubo Li 31509a6d4860SXiubo Li num_widgets /= 2; 31519a6d4860SXiubo Li if (!num_widgets) { 31529a6d4860SXiubo Li dev_err(card->dev, "ASoC: Property '%s's length is zero\n", 31539a6d4860SXiubo Li propname); 31549a6d4860SXiubo Li return -EINVAL; 31559a6d4860SXiubo Li } 31569a6d4860SXiubo Li 31579a6d4860SXiubo Li widgets = devm_kcalloc(card->dev, num_widgets, sizeof(*widgets), 31589a6d4860SXiubo Li GFP_KERNEL); 31599a6d4860SXiubo Li if (!widgets) { 31609a6d4860SXiubo Li dev_err(card->dev, 31619a6d4860SXiubo Li "ASoC: Could not allocate memory for widgets\n"); 31629a6d4860SXiubo Li return -ENOMEM; 31639a6d4860SXiubo Li } 31649a6d4860SXiubo Li 31659a6d4860SXiubo Li for (i = 0; i < num_widgets; i++) { 31669a6d4860SXiubo Li ret = of_property_read_string_index(np, propname, 31679a6d4860SXiubo Li 2 * i, &template); 31689a6d4860SXiubo Li if (ret) { 31699a6d4860SXiubo Li dev_err(card->dev, 31709a6d4860SXiubo Li "ASoC: Property '%s' index %d read error:%d\n", 31719a6d4860SXiubo Li propname, 2 * i, ret); 31729a6d4860SXiubo Li return -EINVAL; 31739a6d4860SXiubo Li } 31749a6d4860SXiubo Li 31759a6d4860SXiubo Li for (j = 0; j < ARRAY_SIZE(simple_widgets); j++) { 31769a6d4860SXiubo Li if (!strncmp(template, simple_widgets[j].name, 31779a6d4860SXiubo Li strlen(simple_widgets[j].name))) { 31789a6d4860SXiubo Li widgets[i] = simple_widgets[j]; 31799a6d4860SXiubo Li break; 31809a6d4860SXiubo Li } 31819a6d4860SXiubo Li } 31829a6d4860SXiubo Li 31839a6d4860SXiubo Li if (j >= ARRAY_SIZE(simple_widgets)) { 31849a6d4860SXiubo Li dev_err(card->dev, 31859a6d4860SXiubo Li "ASoC: DAPM widget '%s' is not supported\n", 31869a6d4860SXiubo Li template); 31879a6d4860SXiubo Li return -EINVAL; 31889a6d4860SXiubo Li } 31899a6d4860SXiubo Li 31909a6d4860SXiubo Li ret = of_property_read_string_index(np, propname, 31919a6d4860SXiubo Li (2 * i) + 1, 31929a6d4860SXiubo Li &wname); 31939a6d4860SXiubo Li if (ret) { 31949a6d4860SXiubo Li dev_err(card->dev, 31959a6d4860SXiubo Li "ASoC: Property '%s' index %d read error:%d\n", 31969a6d4860SXiubo Li propname, (2 * i) + 1, ret); 31979a6d4860SXiubo Li return -EINVAL; 31989a6d4860SXiubo Li } 31999a6d4860SXiubo Li 32009a6d4860SXiubo Li widgets[i].name = wname; 32019a6d4860SXiubo Li } 32029a6d4860SXiubo Li 32039a6d4860SXiubo Li card->dapm_widgets = widgets; 32049a6d4860SXiubo Li card->num_dapm_widgets = num_widgets; 32059a6d4860SXiubo Li 32069a6d4860SXiubo Li return 0; 32079a6d4860SXiubo Li } 32089a6d4860SXiubo Li EXPORT_SYMBOL_GPL(snd_soc_of_parse_audio_simple_widgets); 32099a6d4860SXiubo Li 321089c67857SXiubo Li int snd_soc_of_parse_tdm_slot(struct device_node *np, 321189c67857SXiubo Li unsigned int *slots, 321289c67857SXiubo Li unsigned int *slot_width) 321389c67857SXiubo Li { 321489c67857SXiubo Li u32 val; 321589c67857SXiubo Li int ret; 321689c67857SXiubo Li 321789c67857SXiubo Li if (of_property_read_bool(np, "dai-tdm-slot-num")) { 321889c67857SXiubo Li ret = of_property_read_u32(np, "dai-tdm-slot-num", &val); 321989c67857SXiubo Li if (ret) 322089c67857SXiubo Li return ret; 322189c67857SXiubo Li 322289c67857SXiubo Li if (slots) 322389c67857SXiubo Li *slots = val; 322489c67857SXiubo Li } 322589c67857SXiubo Li 322689c67857SXiubo Li if (of_property_read_bool(np, "dai-tdm-slot-width")) { 322789c67857SXiubo Li ret = of_property_read_u32(np, "dai-tdm-slot-width", &val); 322889c67857SXiubo Li if (ret) 322989c67857SXiubo Li return ret; 323089c67857SXiubo Li 323189c67857SXiubo Li if (slot_width) 323289c67857SXiubo Li *slot_width = val; 323389c67857SXiubo Li } 323489c67857SXiubo Li 323589c67857SXiubo Li return 0; 323689c67857SXiubo Li } 323789c67857SXiubo Li EXPORT_SYMBOL_GPL(snd_soc_of_parse_tdm_slot); 323889c67857SXiubo Li 3239a4a54dd5SStephen Warren int snd_soc_of_parse_audio_routing(struct snd_soc_card *card, 3240a4a54dd5SStephen Warren const char *propname) 3241a4a54dd5SStephen Warren { 3242a4a54dd5SStephen Warren struct device_node *np = card->dev->of_node; 3243f8781db8SPeter Rosin int num_routes, old_routes; 3244a4a54dd5SStephen Warren struct snd_soc_dapm_route *routes; 3245a4a54dd5SStephen Warren int i, ret; 3246a4a54dd5SStephen Warren 3247a4a54dd5SStephen Warren num_routes = of_property_count_strings(np, propname); 3248c34ce320SRichard Zhao if (num_routes < 0 || num_routes & 1) { 324910e8aa9aSMichał Mirosław dev_err(card->dev, 325010e8aa9aSMichał Mirosław "ASoC: Property '%s' does not exist or its length is not even\n", 325110e8aa9aSMichał Mirosław propname); 3252a4a54dd5SStephen Warren return -EINVAL; 3253a4a54dd5SStephen Warren } 3254a4a54dd5SStephen Warren num_routes /= 2; 3255a4a54dd5SStephen Warren if (!num_routes) { 3256f110bfc7SLiam Girdwood dev_err(card->dev, "ASoC: Property '%s's length is zero\n", 3257a4a54dd5SStephen Warren propname); 3258a4a54dd5SStephen Warren return -EINVAL; 3259a4a54dd5SStephen Warren } 3260a4a54dd5SStephen Warren 3261f8781db8SPeter Rosin old_routes = card->num_dapm_routes; 3262f8781db8SPeter Rosin routes = devm_kzalloc(card->dev, 3263f8781db8SPeter Rosin (old_routes + num_routes) * sizeof(*routes), 3264a4a54dd5SStephen Warren GFP_KERNEL); 3265a4a54dd5SStephen Warren if (!routes) { 3266a4a54dd5SStephen Warren dev_err(card->dev, 3267f110bfc7SLiam Girdwood "ASoC: Could not allocate DAPM route table\n"); 3268a4a54dd5SStephen Warren return -EINVAL; 3269a4a54dd5SStephen Warren } 3270a4a54dd5SStephen Warren 3271f8781db8SPeter Rosin memcpy(routes, card->dapm_routes, old_routes * sizeof(*routes)); 3272f8781db8SPeter Rosin 3273a4a54dd5SStephen Warren for (i = 0; i < num_routes; i++) { 3274a4a54dd5SStephen Warren ret = of_property_read_string_index(np, propname, 3275f8781db8SPeter Rosin 2 * i, &routes[old_routes + i].sink); 3276a4a54dd5SStephen Warren if (ret) { 3277c871bd0bSMark Brown dev_err(card->dev, 3278c871bd0bSMark Brown "ASoC: Property '%s' index %d could not be read: %d\n", 3279c871bd0bSMark Brown propname, 2 * i, ret); 3280a4a54dd5SStephen Warren return -EINVAL; 3281a4a54dd5SStephen Warren } 3282a4a54dd5SStephen Warren ret = of_property_read_string_index(np, propname, 3283f8781db8SPeter Rosin (2 * i) + 1, &routes[old_routes + i].source); 3284a4a54dd5SStephen Warren if (ret) { 3285a4a54dd5SStephen Warren dev_err(card->dev, 3286c871bd0bSMark Brown "ASoC: Property '%s' index %d could not be read: %d\n", 3287c871bd0bSMark Brown propname, (2 * i) + 1, ret); 3288a4a54dd5SStephen Warren return -EINVAL; 3289a4a54dd5SStephen Warren } 3290a4a54dd5SStephen Warren } 3291a4a54dd5SStephen Warren 3292f8781db8SPeter Rosin card->num_dapm_routes += num_routes; 3293a4a54dd5SStephen Warren card->dapm_routes = routes; 3294a4a54dd5SStephen Warren 3295a4a54dd5SStephen Warren return 0; 3296a4a54dd5SStephen Warren } 3297a4a54dd5SStephen Warren EXPORT_SYMBOL_GPL(snd_soc_of_parse_audio_routing); 3298a4a54dd5SStephen Warren 3299a7930ed4SKuninori Morimoto unsigned int snd_soc_of_parse_daifmt(struct device_node *np, 3300389cb834SJyri Sarha const char *prefix, 3301389cb834SJyri Sarha struct device_node **bitclkmaster, 3302389cb834SJyri Sarha struct device_node **framemaster) 3303a7930ed4SKuninori Morimoto { 3304a7930ed4SKuninori Morimoto int ret, i; 3305a7930ed4SKuninori Morimoto char prop[128]; 3306a7930ed4SKuninori Morimoto unsigned int format = 0; 3307a7930ed4SKuninori Morimoto int bit, frame; 3308a7930ed4SKuninori Morimoto const char *str; 3309a7930ed4SKuninori Morimoto struct { 3310a7930ed4SKuninori Morimoto char *name; 3311a7930ed4SKuninori Morimoto unsigned int val; 3312a7930ed4SKuninori Morimoto } of_fmt_table[] = { 3313a7930ed4SKuninori Morimoto { "i2s", SND_SOC_DAIFMT_I2S }, 3314a7930ed4SKuninori Morimoto { "right_j", SND_SOC_DAIFMT_RIGHT_J }, 3315a7930ed4SKuninori Morimoto { "left_j", SND_SOC_DAIFMT_LEFT_J }, 3316a7930ed4SKuninori Morimoto { "dsp_a", SND_SOC_DAIFMT_DSP_A }, 3317a7930ed4SKuninori Morimoto { "dsp_b", SND_SOC_DAIFMT_DSP_B }, 3318a7930ed4SKuninori Morimoto { "ac97", SND_SOC_DAIFMT_AC97 }, 3319a7930ed4SKuninori Morimoto { "pdm", SND_SOC_DAIFMT_PDM}, 3320a7930ed4SKuninori Morimoto { "msb", SND_SOC_DAIFMT_MSB }, 3321a7930ed4SKuninori Morimoto { "lsb", SND_SOC_DAIFMT_LSB }, 3322a7930ed4SKuninori Morimoto }; 3323a7930ed4SKuninori Morimoto 3324a7930ed4SKuninori Morimoto if (!prefix) 3325a7930ed4SKuninori Morimoto prefix = ""; 3326a7930ed4SKuninori Morimoto 3327a7930ed4SKuninori Morimoto /* 3328a7930ed4SKuninori Morimoto * check "[prefix]format = xxx" 3329a7930ed4SKuninori Morimoto * SND_SOC_DAIFMT_FORMAT_MASK area 3330a7930ed4SKuninori Morimoto */ 3331a7930ed4SKuninori Morimoto snprintf(prop, sizeof(prop), "%sformat", prefix); 3332a7930ed4SKuninori Morimoto ret = of_property_read_string(np, prop, &str); 3333a7930ed4SKuninori Morimoto if (ret == 0) { 3334a7930ed4SKuninori Morimoto for (i = 0; i < ARRAY_SIZE(of_fmt_table); i++) { 3335a7930ed4SKuninori Morimoto if (strcmp(str, of_fmt_table[i].name) == 0) { 3336a7930ed4SKuninori Morimoto format |= of_fmt_table[i].val; 3337a7930ed4SKuninori Morimoto break; 3338a7930ed4SKuninori Morimoto } 3339a7930ed4SKuninori Morimoto } 3340a7930ed4SKuninori Morimoto } 3341a7930ed4SKuninori Morimoto 3342a7930ed4SKuninori Morimoto /* 33438c2d6a9fSKuninori Morimoto * check "[prefix]continuous-clock" 3344a7930ed4SKuninori Morimoto * SND_SOC_DAIFMT_CLOCK_MASK area 3345a7930ed4SKuninori Morimoto */ 33468c2d6a9fSKuninori Morimoto snprintf(prop, sizeof(prop), "%scontinuous-clock", prefix); 33478c2d6a9fSKuninori Morimoto if (of_get_property(np, prop, NULL)) 33488c2d6a9fSKuninori Morimoto format |= SND_SOC_DAIFMT_CONT; 33498c2d6a9fSKuninori Morimoto else 33508c2d6a9fSKuninori Morimoto format |= SND_SOC_DAIFMT_GATED; 3351a7930ed4SKuninori Morimoto 3352a7930ed4SKuninori Morimoto /* 3353a7930ed4SKuninori Morimoto * check "[prefix]bitclock-inversion" 3354a7930ed4SKuninori Morimoto * check "[prefix]frame-inversion" 3355a7930ed4SKuninori Morimoto * SND_SOC_DAIFMT_INV_MASK area 3356a7930ed4SKuninori Morimoto */ 3357a7930ed4SKuninori Morimoto snprintf(prop, sizeof(prop), "%sbitclock-inversion", prefix); 3358a7930ed4SKuninori Morimoto bit = !!of_get_property(np, prop, NULL); 3359a7930ed4SKuninori Morimoto 3360a7930ed4SKuninori Morimoto snprintf(prop, sizeof(prop), "%sframe-inversion", prefix); 3361a7930ed4SKuninori Morimoto frame = !!of_get_property(np, prop, NULL); 3362a7930ed4SKuninori Morimoto 3363a7930ed4SKuninori Morimoto switch ((bit << 4) + frame) { 3364a7930ed4SKuninori Morimoto case 0x11: 3365a7930ed4SKuninori Morimoto format |= SND_SOC_DAIFMT_IB_IF; 3366a7930ed4SKuninori Morimoto break; 3367a7930ed4SKuninori Morimoto case 0x10: 3368a7930ed4SKuninori Morimoto format |= SND_SOC_DAIFMT_IB_NF; 3369a7930ed4SKuninori Morimoto break; 3370a7930ed4SKuninori Morimoto case 0x01: 3371a7930ed4SKuninori Morimoto format |= SND_SOC_DAIFMT_NB_IF; 3372a7930ed4SKuninori Morimoto break; 3373a7930ed4SKuninori Morimoto default: 3374a7930ed4SKuninori Morimoto /* SND_SOC_DAIFMT_NB_NF is default */ 3375a7930ed4SKuninori Morimoto break; 3376a7930ed4SKuninori Morimoto } 3377a7930ed4SKuninori Morimoto 3378a7930ed4SKuninori Morimoto /* 3379a7930ed4SKuninori Morimoto * check "[prefix]bitclock-master" 3380a7930ed4SKuninori Morimoto * check "[prefix]frame-master" 3381a7930ed4SKuninori Morimoto * SND_SOC_DAIFMT_MASTER_MASK area 3382a7930ed4SKuninori Morimoto */ 3383a7930ed4SKuninori Morimoto snprintf(prop, sizeof(prop), "%sbitclock-master", prefix); 3384a7930ed4SKuninori Morimoto bit = !!of_get_property(np, prop, NULL); 3385389cb834SJyri Sarha if (bit && bitclkmaster) 3386389cb834SJyri Sarha *bitclkmaster = of_parse_phandle(np, prop, 0); 3387a7930ed4SKuninori Morimoto 3388a7930ed4SKuninori Morimoto snprintf(prop, sizeof(prop), "%sframe-master", prefix); 3389a7930ed4SKuninori Morimoto frame = !!of_get_property(np, prop, NULL); 3390389cb834SJyri Sarha if (frame && framemaster) 3391389cb834SJyri Sarha *framemaster = of_parse_phandle(np, prop, 0); 3392a7930ed4SKuninori Morimoto 3393a7930ed4SKuninori Morimoto switch ((bit << 4) + frame) { 3394a7930ed4SKuninori Morimoto case 0x11: 3395a7930ed4SKuninori Morimoto format |= SND_SOC_DAIFMT_CBM_CFM; 3396a7930ed4SKuninori Morimoto break; 3397a7930ed4SKuninori Morimoto case 0x10: 3398a7930ed4SKuninori Morimoto format |= SND_SOC_DAIFMT_CBM_CFS; 3399a7930ed4SKuninori Morimoto break; 3400a7930ed4SKuninori Morimoto case 0x01: 3401a7930ed4SKuninori Morimoto format |= SND_SOC_DAIFMT_CBS_CFM; 3402a7930ed4SKuninori Morimoto break; 3403a7930ed4SKuninori Morimoto default: 3404a7930ed4SKuninori Morimoto format |= SND_SOC_DAIFMT_CBS_CFS; 3405a7930ed4SKuninori Morimoto break; 3406a7930ed4SKuninori Morimoto } 3407a7930ed4SKuninori Morimoto 3408a7930ed4SKuninori Morimoto return format; 3409a7930ed4SKuninori Morimoto } 3410a7930ed4SKuninori Morimoto EXPORT_SYMBOL_GPL(snd_soc_of_parse_daifmt); 3411a7930ed4SKuninori Morimoto 341293b0f3eeSJean-Francois Moine static int snd_soc_get_dai_name(struct of_phandle_args *args, 3413cb470087SKuninori Morimoto const char **dai_name) 3414cb470087SKuninori Morimoto { 3415cb470087SKuninori Morimoto struct snd_soc_component *pos; 341693b0f3eeSJean-Francois Moine int ret = -EPROBE_DEFER; 3417cb470087SKuninori Morimoto 3418cb470087SKuninori Morimoto mutex_lock(&client_mutex); 3419cb470087SKuninori Morimoto list_for_each_entry(pos, &component_list, list) { 342093b0f3eeSJean-Francois Moine if (pos->dev->of_node != args->np) 3421cb470087SKuninori Morimoto continue; 3422cb470087SKuninori Morimoto 34236833c452SKuninori Morimoto if (pos->driver->of_xlate_dai_name) { 342493b0f3eeSJean-Francois Moine ret = pos->driver->of_xlate_dai_name(pos, 342593b0f3eeSJean-Francois Moine args, 342693b0f3eeSJean-Francois Moine dai_name); 34276833c452SKuninori Morimoto } else { 34286833c452SKuninori Morimoto int id = -1; 34296833c452SKuninori Morimoto 343093b0f3eeSJean-Francois Moine switch (args->args_count) { 34316833c452SKuninori Morimoto case 0: 34326833c452SKuninori Morimoto id = 0; /* same as dai_drv[0] */ 34336833c452SKuninori Morimoto break; 34346833c452SKuninori Morimoto case 1: 343593b0f3eeSJean-Francois Moine id = args->args[0]; 34366833c452SKuninori Morimoto break; 34376833c452SKuninori Morimoto default: 34386833c452SKuninori Morimoto /* not supported */ 3439cb470087SKuninori Morimoto break; 3440cb470087SKuninori Morimoto } 3441cb470087SKuninori Morimoto 34426833c452SKuninori Morimoto if (id < 0 || id >= pos->num_dai) { 34436833c452SKuninori Morimoto ret = -EINVAL; 34443dcba280SNicolin Chen continue; 34456833c452SKuninori Morimoto } 3446e41975edSXiubo Li 3447e41975edSXiubo Li ret = 0; 3448e41975edSXiubo Li 3449e41975edSXiubo Li *dai_name = pos->dai_drv[id].name; 3450e41975edSXiubo Li if (!*dai_name) 3451e41975edSXiubo Li *dai_name = pos->name; 34526833c452SKuninori Morimoto } 34536833c452SKuninori Morimoto 3454cb470087SKuninori Morimoto break; 3455cb470087SKuninori Morimoto } 3456cb470087SKuninori Morimoto mutex_unlock(&client_mutex); 345793b0f3eeSJean-Francois Moine return ret; 345893b0f3eeSJean-Francois Moine } 345993b0f3eeSJean-Francois Moine 346093b0f3eeSJean-Francois Moine int snd_soc_of_get_dai_name(struct device_node *of_node, 346193b0f3eeSJean-Francois Moine const char **dai_name) 346293b0f3eeSJean-Francois Moine { 346393b0f3eeSJean-Francois Moine struct of_phandle_args args; 346493b0f3eeSJean-Francois Moine int ret; 346593b0f3eeSJean-Francois Moine 346693b0f3eeSJean-Francois Moine ret = of_parse_phandle_with_args(of_node, "sound-dai", 346793b0f3eeSJean-Francois Moine "#sound-dai-cells", 0, &args); 346893b0f3eeSJean-Francois Moine if (ret) 346993b0f3eeSJean-Francois Moine return ret; 347093b0f3eeSJean-Francois Moine 347193b0f3eeSJean-Francois Moine ret = snd_soc_get_dai_name(&args, dai_name); 3472cb470087SKuninori Morimoto 3473cb470087SKuninori Morimoto of_node_put(args.np); 3474cb470087SKuninori Morimoto 3475cb470087SKuninori Morimoto return ret; 3476cb470087SKuninori Morimoto } 3477cb470087SKuninori Morimoto EXPORT_SYMBOL_GPL(snd_soc_of_get_dai_name); 3478cb470087SKuninori Morimoto 347993b0f3eeSJean-Francois Moine /* 348093b0f3eeSJean-Francois Moine * snd_soc_of_get_dai_link_codecs - Parse a list of CODECs in the devicetree 348193b0f3eeSJean-Francois Moine * @dev: Card device 348293b0f3eeSJean-Francois Moine * @of_node: Device node 348393b0f3eeSJean-Francois Moine * @dai_link: DAI link 348493b0f3eeSJean-Francois Moine * 348593b0f3eeSJean-Francois Moine * Builds an array of CODEC DAI components from the DAI link property 348693b0f3eeSJean-Francois Moine * 'sound-dai'. 348793b0f3eeSJean-Francois Moine * The array is set in the DAI link and the number of DAIs is set accordingly. 348893b0f3eeSJean-Francois Moine * The device nodes in the array (of_node) must be dereferenced by the caller. 348993b0f3eeSJean-Francois Moine * 349093b0f3eeSJean-Francois Moine * Returns 0 for success 349193b0f3eeSJean-Francois Moine */ 349293b0f3eeSJean-Francois Moine int snd_soc_of_get_dai_link_codecs(struct device *dev, 349393b0f3eeSJean-Francois Moine struct device_node *of_node, 349493b0f3eeSJean-Francois Moine struct snd_soc_dai_link *dai_link) 349593b0f3eeSJean-Francois Moine { 349693b0f3eeSJean-Francois Moine struct of_phandle_args args; 349793b0f3eeSJean-Francois Moine struct snd_soc_dai_link_component *component; 349893b0f3eeSJean-Francois Moine char *name; 349993b0f3eeSJean-Francois Moine int index, num_codecs, ret; 350093b0f3eeSJean-Francois Moine 350193b0f3eeSJean-Francois Moine /* Count the number of CODECs */ 350293b0f3eeSJean-Francois Moine name = "sound-dai"; 350393b0f3eeSJean-Francois Moine num_codecs = of_count_phandle_with_args(of_node, name, 350493b0f3eeSJean-Francois Moine "#sound-dai-cells"); 350593b0f3eeSJean-Francois Moine if (num_codecs <= 0) { 350693b0f3eeSJean-Francois Moine if (num_codecs == -ENOENT) 350793b0f3eeSJean-Francois Moine dev_err(dev, "No 'sound-dai' property\n"); 350893b0f3eeSJean-Francois Moine else 350993b0f3eeSJean-Francois Moine dev_err(dev, "Bad phandle in 'sound-dai'\n"); 351093b0f3eeSJean-Francois Moine return num_codecs; 351193b0f3eeSJean-Francois Moine } 351293b0f3eeSJean-Francois Moine component = devm_kzalloc(dev, 351393b0f3eeSJean-Francois Moine sizeof *component * num_codecs, 351493b0f3eeSJean-Francois Moine GFP_KERNEL); 351593b0f3eeSJean-Francois Moine if (!component) 351693b0f3eeSJean-Francois Moine return -ENOMEM; 351793b0f3eeSJean-Francois Moine dai_link->codecs = component; 351893b0f3eeSJean-Francois Moine dai_link->num_codecs = num_codecs; 351993b0f3eeSJean-Francois Moine 352093b0f3eeSJean-Francois Moine /* Parse the list */ 352193b0f3eeSJean-Francois Moine for (index = 0, component = dai_link->codecs; 352293b0f3eeSJean-Francois Moine index < dai_link->num_codecs; 352393b0f3eeSJean-Francois Moine index++, component++) { 352493b0f3eeSJean-Francois Moine ret = of_parse_phandle_with_args(of_node, name, 352593b0f3eeSJean-Francois Moine "#sound-dai-cells", 352693b0f3eeSJean-Francois Moine index, &args); 352793b0f3eeSJean-Francois Moine if (ret) 352893b0f3eeSJean-Francois Moine goto err; 352993b0f3eeSJean-Francois Moine component->of_node = args.np; 353093b0f3eeSJean-Francois Moine ret = snd_soc_get_dai_name(&args, &component->dai_name); 353193b0f3eeSJean-Francois Moine if (ret < 0) 353293b0f3eeSJean-Francois Moine goto err; 353393b0f3eeSJean-Francois Moine } 353493b0f3eeSJean-Francois Moine return 0; 353593b0f3eeSJean-Francois Moine err: 353693b0f3eeSJean-Francois Moine for (index = 0, component = dai_link->codecs; 353793b0f3eeSJean-Francois Moine index < dai_link->num_codecs; 353893b0f3eeSJean-Francois Moine index++, component++) { 353993b0f3eeSJean-Francois Moine if (!component->of_node) 354093b0f3eeSJean-Francois Moine break; 354193b0f3eeSJean-Francois Moine of_node_put(component->of_node); 354293b0f3eeSJean-Francois Moine component->of_node = NULL; 354393b0f3eeSJean-Francois Moine } 354493b0f3eeSJean-Francois Moine dai_link->codecs = NULL; 354593b0f3eeSJean-Francois Moine dai_link->num_codecs = 0; 354693b0f3eeSJean-Francois Moine return ret; 354793b0f3eeSJean-Francois Moine } 354893b0f3eeSJean-Francois Moine EXPORT_SYMBOL_GPL(snd_soc_of_get_dai_link_codecs); 354993b0f3eeSJean-Francois Moine 3550c9b3a40fSTakashi Iwai static int __init snd_soc_init(void) 3551db2a4165SFrank Mandarino { 3552db2a4165SFrank Mandarino #ifdef CONFIG_DEBUG_FS 35538a9dab1aSMark Brown snd_soc_debugfs_root = debugfs_create_dir("asoc", NULL); 35548a9dab1aSMark Brown if (IS_ERR(snd_soc_debugfs_root) || !snd_soc_debugfs_root) { 35550837fc62SFabio Estevam pr_warn("ASoC: Failed to create debugfs directory\n"); 35568a9dab1aSMark Brown snd_soc_debugfs_root = NULL; 3557db2a4165SFrank Mandarino } 3558c3c5a19aSMark Brown 35598a9dab1aSMark Brown if (!debugfs_create_file("codecs", 0444, snd_soc_debugfs_root, NULL, 3560c3c5a19aSMark Brown &codec_list_fops)) 3561c3c5a19aSMark Brown pr_warn("ASoC: Failed to create CODEC list debugfs file\n"); 3562c3c5a19aSMark Brown 35638a9dab1aSMark Brown if (!debugfs_create_file("dais", 0444, snd_soc_debugfs_root, NULL, 3564f3208780SMark Brown &dai_list_fops)) 3565f3208780SMark Brown pr_warn("ASoC: Failed to create DAI list debugfs file\n"); 356619c7ac27SMark Brown 35678a9dab1aSMark Brown if (!debugfs_create_file("platforms", 0444, snd_soc_debugfs_root, NULL, 356819c7ac27SMark Brown &platform_list_fops)) 356919c7ac27SMark Brown pr_warn("ASoC: Failed to create platform list debugfs file\n"); 3570db2a4165SFrank Mandarino #endif 3571db2a4165SFrank Mandarino 3572fb257897SMark Brown snd_soc_util_init(); 3573fb257897SMark Brown 3574db2a4165SFrank Mandarino return platform_driver_register(&soc_driver); 3575db2a4165SFrank Mandarino } 35764abe8e16SMark Brown module_init(snd_soc_init); 3577db2a4165SFrank Mandarino 3578db2a4165SFrank Mandarino static void __exit snd_soc_exit(void) 3579db2a4165SFrank Mandarino { 3580fb257897SMark Brown snd_soc_util_exit(); 3581fb257897SMark Brown 3582db2a4165SFrank Mandarino #ifdef CONFIG_DEBUG_FS 35838a9dab1aSMark Brown debugfs_remove_recursive(snd_soc_debugfs_root); 3584db2a4165SFrank Mandarino #endif 3585db2a4165SFrank Mandarino platform_driver_unregister(&soc_driver); 3586db2a4165SFrank Mandarino } 3587db2a4165SFrank Mandarino module_exit(snd_soc_exit); 3588db2a4165SFrank Mandarino 3589db2a4165SFrank Mandarino /* Module information */ 3590db2a4165SFrank Mandarino MODULE_AUTHOR("Liam Girdwood, lrg@slimlogic.co.uk"); 3591db2a4165SFrank Mandarino MODULE_DESCRIPTION("ALSA SoC Core"); 3592db2a4165SFrank Mandarino MODULE_LICENSE("GPL"); 3593db2a4165SFrank Mandarino MODULE_ALIAS("platform:soc-audio"); 3594