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> 37741a509fSMarkus Pargmann #include <linux/gpio.h> 38741a509fSMarkus Pargmann #include <linux/of_gpio.h> 39474828a4SMarek Vasut #include <sound/ac97_codec.h> 40db2a4165SFrank Mandarino #include <sound/core.h> 413028eb8cSMark Brown #include <sound/jack.h> 42db2a4165SFrank Mandarino #include <sound/pcm.h> 43db2a4165SFrank Mandarino #include <sound/pcm_params.h> 44db2a4165SFrank Mandarino #include <sound/soc.h> 4501d7584cSLiam Girdwood #include <sound/soc-dpcm.h> 46db2a4165SFrank Mandarino #include <sound/initval.h> 47db2a4165SFrank Mandarino 48a8b1d34fSMark Brown #define CREATE_TRACE_POINTS 49a8b1d34fSMark Brown #include <trace/events/asoc.h> 50a8b1d34fSMark Brown 51f0fba2adSLiam Girdwood #define NAME_SIZE 32 52f0fba2adSLiam Girdwood 53384c89e2SMark Brown #ifdef CONFIG_DEBUG_FS 548a9dab1aSMark Brown struct dentry *snd_soc_debugfs_root; 558a9dab1aSMark Brown EXPORT_SYMBOL_GPL(snd_soc_debugfs_root); 56384c89e2SMark Brown #endif 57384c89e2SMark Brown 58c5af3a2eSMark Brown static DEFINE_MUTEX(client_mutex); 5912a48a8cSMark Brown static LIST_HEAD(platform_list); 600d0cf00aSMark Brown static LIST_HEAD(codec_list); 61030e79f6SKuninori Morimoto static LIST_HEAD(component_list); 62c5af3a2eSMark Brown 63db2a4165SFrank Mandarino /* 64db2a4165SFrank Mandarino * This is a timeout to do a DAPM powerdown after a stream is closed(). 65db2a4165SFrank Mandarino * It can be used to eliminate pops between different playback streams, e.g. 66db2a4165SFrank Mandarino * between two audio tracks. 67db2a4165SFrank Mandarino */ 68db2a4165SFrank Mandarino static int pmdown_time = 5000; 69db2a4165SFrank Mandarino module_param(pmdown_time, int, 0); 70db2a4165SFrank Mandarino MODULE_PARM_DESC(pmdown_time, "DAPM stream powerdown time (msecs)"); 71db2a4165SFrank Mandarino 72741a509fSMarkus Pargmann struct snd_ac97_reset_cfg { 73741a509fSMarkus Pargmann struct pinctrl *pctl; 74741a509fSMarkus Pargmann struct pinctrl_state *pstate_reset; 75741a509fSMarkus Pargmann struct pinctrl_state *pstate_warm_reset; 76741a509fSMarkus Pargmann struct pinctrl_state *pstate_run; 77741a509fSMarkus Pargmann int gpio_sdata; 78741a509fSMarkus Pargmann int gpio_sync; 79741a509fSMarkus Pargmann int gpio_reset; 80741a509fSMarkus Pargmann }; 81741a509fSMarkus Pargmann 822bc9a81eSDimitris Papastamos /* returns the minimum number of bytes needed to represent 832bc9a81eSDimitris Papastamos * a particular given value */ 842bc9a81eSDimitris Papastamos static int min_bytes_needed(unsigned long val) 852bc9a81eSDimitris Papastamos { 862bc9a81eSDimitris Papastamos int c = 0; 872bc9a81eSDimitris Papastamos int i; 882bc9a81eSDimitris Papastamos 892bc9a81eSDimitris Papastamos for (i = (sizeof val * 8) - 1; i >= 0; --i, ++c) 902bc9a81eSDimitris Papastamos if (val & (1UL << i)) 912bc9a81eSDimitris Papastamos break; 922bc9a81eSDimitris Papastamos c = (sizeof val * 8) - c; 932bc9a81eSDimitris Papastamos if (!c || (c % 8)) 942bc9a81eSDimitris Papastamos c = (c + 8) / 8; 952bc9a81eSDimitris Papastamos else 962bc9a81eSDimitris Papastamos c /= 8; 972bc9a81eSDimitris Papastamos return c; 982bc9a81eSDimitris Papastamos } 992bc9a81eSDimitris Papastamos 10013fd179fSDimitris Papastamos /* fill buf which is 'len' bytes with a formatted 10113fd179fSDimitris Papastamos * string of the form 'reg: value\n' */ 10213fd179fSDimitris Papastamos static int format_register_str(struct snd_soc_codec *codec, 10313fd179fSDimitris Papastamos unsigned int reg, char *buf, size_t len) 1042624d5faSMark Brown { 10500b317a4SStephen Warren int wordsize = min_bytes_needed(codec->driver->reg_cache_size) * 2; 10600b317a4SStephen Warren int regsize = codec->driver->reg_word_size * 2; 10713fd179fSDimitris Papastamos int ret; 10813fd179fSDimitris Papastamos char tmpbuf[len + 1]; 10913fd179fSDimitris Papastamos char regbuf[regsize + 1]; 11013fd179fSDimitris Papastamos 11113fd179fSDimitris Papastamos /* since tmpbuf is allocated on the stack, warn the callers if they 11213fd179fSDimitris Papastamos * try to abuse this function */ 11313fd179fSDimitris Papastamos WARN_ON(len > 63); 11413fd179fSDimitris Papastamos 11513fd179fSDimitris Papastamos /* +2 for ': ' and + 1 for '\n' */ 11613fd179fSDimitris Papastamos if (wordsize + regsize + 2 + 1 != len) 11713fd179fSDimitris Papastamos return -EINVAL; 11813fd179fSDimitris Papastamos 11913fd179fSDimitris Papastamos ret = snd_soc_read(codec, reg); 12013fd179fSDimitris Papastamos if (ret < 0) { 12113fd179fSDimitris Papastamos memset(regbuf, 'X', regsize); 12213fd179fSDimitris Papastamos regbuf[regsize] = '\0'; 12313fd179fSDimitris Papastamos } else { 12413fd179fSDimitris Papastamos snprintf(regbuf, regsize + 1, "%.*x", regsize, ret); 12513fd179fSDimitris Papastamos } 12613fd179fSDimitris Papastamos 12713fd179fSDimitris Papastamos /* prepare the buffer */ 12813fd179fSDimitris Papastamos snprintf(tmpbuf, len + 1, "%.*x: %s\n", wordsize, reg, regbuf); 12913fd179fSDimitris Papastamos /* copy it back to the caller without the '\0' */ 13013fd179fSDimitris Papastamos memcpy(buf, tmpbuf, len); 13113fd179fSDimitris Papastamos 13213fd179fSDimitris Papastamos return 0; 13313fd179fSDimitris Papastamos } 13413fd179fSDimitris Papastamos 13513fd179fSDimitris Papastamos /* codec register dump */ 13613fd179fSDimitris Papastamos static ssize_t soc_codec_reg_show(struct snd_soc_codec *codec, char *buf, 13713fd179fSDimitris Papastamos size_t count, loff_t pos) 13813fd179fSDimitris Papastamos { 13913fd179fSDimitris Papastamos int i, step = 1; 1402bc9a81eSDimitris Papastamos int wordsize, regsize; 14113fd179fSDimitris Papastamos int len; 14213fd179fSDimitris Papastamos size_t total = 0; 14313fd179fSDimitris Papastamos loff_t p = 0; 1442bc9a81eSDimitris Papastamos 14500b317a4SStephen Warren wordsize = min_bytes_needed(codec->driver->reg_cache_size) * 2; 14600b317a4SStephen Warren regsize = codec->driver->reg_word_size * 2; 1472624d5faSMark Brown 14813fd179fSDimitris Papastamos len = wordsize + regsize + 2 + 1; 14913fd179fSDimitris Papastamos 150f0fba2adSLiam Girdwood if (!codec->driver->reg_cache_size) 1512624d5faSMark Brown return 0; 1522624d5faSMark Brown 153f0fba2adSLiam Girdwood if (codec->driver->reg_cache_step) 154f0fba2adSLiam Girdwood step = codec->driver->reg_cache_step; 1552624d5faSMark Brown 156f0fba2adSLiam Girdwood for (i = 0; i < codec->driver->reg_cache_size; i += step) { 15713fd179fSDimitris Papastamos /* only support larger than PAGE_SIZE bytes debugfs 15813fd179fSDimitris Papastamos * entries for the default case */ 15913fd179fSDimitris Papastamos if (p >= pos) { 16013fd179fSDimitris Papastamos if (total + len >= count - 1) 1612624d5faSMark Brown break; 16213fd179fSDimitris Papastamos format_register_str(codec, i, buf + total, len); 16313fd179fSDimitris Papastamos total += len; 16413fd179fSDimitris Papastamos } 16513fd179fSDimitris Papastamos p += len; 16613fd179fSDimitris Papastamos } 1672624d5faSMark Brown 16813fd179fSDimitris Papastamos total = min(total, count - 1); 1692624d5faSMark Brown 17013fd179fSDimitris Papastamos return total; 1712624d5faSMark Brown } 17213fd179fSDimitris Papastamos 1732624d5faSMark Brown static ssize_t codec_reg_show(struct device *dev, 1742624d5faSMark Brown struct device_attribute *attr, char *buf) 1752624d5faSMark Brown { 17636ae1a96SMark Brown struct snd_soc_pcm_runtime *rtd = dev_get_drvdata(dev); 177f0fba2adSLiam Girdwood 17813fd179fSDimitris Papastamos return soc_codec_reg_show(rtd->codec, buf, PAGE_SIZE, 0); 1792624d5faSMark Brown } 1802624d5faSMark Brown 1812624d5faSMark Brown static DEVICE_ATTR(codec_reg, 0444, codec_reg_show, NULL); 1822624d5faSMark Brown 183dbe21408SMark Brown static ssize_t pmdown_time_show(struct device *dev, 184dbe21408SMark Brown struct device_attribute *attr, char *buf) 185dbe21408SMark Brown { 18636ae1a96SMark Brown struct snd_soc_pcm_runtime *rtd = dev_get_drvdata(dev); 187dbe21408SMark Brown 188f0fba2adSLiam Girdwood return sprintf(buf, "%ld\n", rtd->pmdown_time); 189dbe21408SMark Brown } 190dbe21408SMark Brown 191dbe21408SMark Brown static ssize_t pmdown_time_set(struct device *dev, 192dbe21408SMark Brown struct device_attribute *attr, 193dbe21408SMark Brown const char *buf, size_t count) 194dbe21408SMark Brown { 19536ae1a96SMark Brown struct snd_soc_pcm_runtime *rtd = dev_get_drvdata(dev); 196c593b520SMark Brown int ret; 197dbe21408SMark Brown 198b785a492SJingoo Han ret = kstrtol(buf, 10, &rtd->pmdown_time); 199c593b520SMark Brown if (ret) 200c593b520SMark Brown return ret; 201dbe21408SMark Brown 202dbe21408SMark Brown return count; 203dbe21408SMark Brown } 204dbe21408SMark Brown 205dbe21408SMark Brown static DEVICE_ATTR(pmdown_time, 0644, pmdown_time_show, pmdown_time_set); 206dbe21408SMark Brown 2072624d5faSMark Brown #ifdef CONFIG_DEBUG_FS 2082624d5faSMark Brown static ssize_t codec_reg_read_file(struct file *file, char __user *user_buf, 2092624d5faSMark Brown size_t count, loff_t *ppos) 2102624d5faSMark Brown { 2112624d5faSMark Brown ssize_t ret; 2122624d5faSMark Brown struct snd_soc_codec *codec = file->private_data; 21313fd179fSDimitris Papastamos char *buf; 21413fd179fSDimitris Papastamos 21513fd179fSDimitris Papastamos if (*ppos < 0 || !count) 21613fd179fSDimitris Papastamos return -EINVAL; 21713fd179fSDimitris Papastamos 21813fd179fSDimitris Papastamos buf = kmalloc(count, GFP_KERNEL); 2192624d5faSMark Brown if (!buf) 2202624d5faSMark Brown return -ENOMEM; 22113fd179fSDimitris Papastamos 22213fd179fSDimitris Papastamos ret = soc_codec_reg_show(codec, buf, count, *ppos); 22313fd179fSDimitris Papastamos if (ret >= 0) { 22413fd179fSDimitris Papastamos if (copy_to_user(user_buf, buf, ret)) { 22513fd179fSDimitris Papastamos kfree(buf); 22613fd179fSDimitris Papastamos return -EFAULT; 22713fd179fSDimitris Papastamos } 22813fd179fSDimitris Papastamos *ppos += ret; 22913fd179fSDimitris Papastamos } 23013fd179fSDimitris Papastamos 2312624d5faSMark Brown kfree(buf); 2322624d5faSMark Brown return ret; 2332624d5faSMark Brown } 2342624d5faSMark Brown 2352624d5faSMark Brown static ssize_t codec_reg_write_file(struct file *file, 2362624d5faSMark Brown const char __user *user_buf, size_t count, loff_t *ppos) 2372624d5faSMark Brown { 2382624d5faSMark Brown char buf[32]; 23934e268d8SStephen Boyd size_t buf_size; 2402624d5faSMark Brown char *start = buf; 2412624d5faSMark Brown unsigned long reg, value; 2422624d5faSMark Brown struct snd_soc_codec *codec = file->private_data; 243b785a492SJingoo Han int ret; 2442624d5faSMark Brown 2452624d5faSMark Brown buf_size = min(count, (sizeof(buf)-1)); 2462624d5faSMark Brown if (copy_from_user(buf, user_buf, buf_size)) 2472624d5faSMark Brown return -EFAULT; 2482624d5faSMark Brown buf[buf_size] = 0; 2492624d5faSMark Brown 2502624d5faSMark Brown while (*start == ' ') 2512624d5faSMark Brown start++; 2522624d5faSMark Brown reg = simple_strtoul(start, &start, 16); 2532624d5faSMark Brown while (*start == ' ') 2542624d5faSMark Brown start++; 255b785a492SJingoo Han ret = kstrtoul(start, 16, &value); 256b785a492SJingoo Han if (ret) 257b785a492SJingoo Han return ret; 2580d51a9cbSMark Brown 2590d51a9cbSMark Brown /* Userspace has been fiddling around behind the kernel's back */ 260373d4d09SRusty Russell add_taint(TAINT_USER, LOCKDEP_NOW_UNRELIABLE); 2610d51a9cbSMark Brown 262e4f078d8SDimitris Papastamos snd_soc_write(codec, reg, value); 2632624d5faSMark Brown return buf_size; 2642624d5faSMark Brown } 2652624d5faSMark Brown 2662624d5faSMark Brown static const struct file_operations codec_reg_fops = { 267234e3405SStephen Boyd .open = simple_open, 2682624d5faSMark Brown .read = codec_reg_read_file, 2692624d5faSMark Brown .write = codec_reg_write_file, 2706038f373SArnd Bergmann .llseek = default_llseek, 2712624d5faSMark Brown }; 2722624d5faSMark Brown 27381c7cfd1SLars-Peter Clausen static void soc_init_component_debugfs(struct snd_soc_component *component) 274e73f3de5SRussell King { 27581c7cfd1SLars-Peter Clausen if (component->debugfs_prefix) { 27681c7cfd1SLars-Peter Clausen char *name; 277e73f3de5SRussell King 27881c7cfd1SLars-Peter Clausen name = kasprintf(GFP_KERNEL, "%s:%s", 27981c7cfd1SLars-Peter Clausen component->debugfs_prefix, component->name); 28081c7cfd1SLars-Peter Clausen if (name) { 28181c7cfd1SLars-Peter Clausen component->debugfs_root = debugfs_create_dir(name, 28281c7cfd1SLars-Peter Clausen component->card->debugfs_card_root); 28381c7cfd1SLars-Peter Clausen kfree(name); 28481c7cfd1SLars-Peter Clausen } 28581c7cfd1SLars-Peter Clausen } else { 28681c7cfd1SLars-Peter Clausen component->debugfs_root = debugfs_create_dir(component->name, 28781c7cfd1SLars-Peter Clausen component->card->debugfs_card_root); 288e73f3de5SRussell King } 289e73f3de5SRussell King 29081c7cfd1SLars-Peter Clausen if (!component->debugfs_root) { 29181c7cfd1SLars-Peter Clausen dev_warn(component->dev, 29281c7cfd1SLars-Peter Clausen "ASoC: Failed to create component debugfs directory\n"); 2932624d5faSMark Brown return; 2942624d5faSMark Brown } 2952624d5faSMark Brown 29681c7cfd1SLars-Peter Clausen snd_soc_dapm_debugfs_init(snd_soc_component_get_dapm(component), 29781c7cfd1SLars-Peter Clausen component->debugfs_root); 29881c7cfd1SLars-Peter Clausen 29981c7cfd1SLars-Peter Clausen if (component->init_debugfs) 30081c7cfd1SLars-Peter Clausen component->init_debugfs(component); 30181c7cfd1SLars-Peter Clausen } 30281c7cfd1SLars-Peter Clausen 30381c7cfd1SLars-Peter Clausen static void soc_cleanup_component_debugfs(struct snd_soc_component *component) 30481c7cfd1SLars-Peter Clausen { 30581c7cfd1SLars-Peter Clausen debugfs_remove_recursive(component->debugfs_root); 30681c7cfd1SLars-Peter Clausen } 30781c7cfd1SLars-Peter Clausen 30881c7cfd1SLars-Peter Clausen static void soc_init_codec_debugfs(struct snd_soc_component *component) 30981c7cfd1SLars-Peter Clausen { 31081c7cfd1SLars-Peter Clausen struct snd_soc_codec *codec = snd_soc_component_to_codec(component); 31181c7cfd1SLars-Peter Clausen 31281c7cfd1SLars-Peter Clausen debugfs_create_bool("cache_sync", 0444, codec->component.debugfs_root, 313aaee8ef1SMark Brown &codec->cache_sync); 314aaee8ef1SMark Brown 3152624d5faSMark Brown codec->debugfs_reg = debugfs_create_file("codec_reg", 0644, 31681c7cfd1SLars-Peter Clausen codec->component.debugfs_root, 3172624d5faSMark Brown codec, &codec_reg_fops); 3182624d5faSMark Brown if (!codec->debugfs_reg) 31910e8aa9aSMichał Mirosław dev_warn(codec->dev, 32010e8aa9aSMichał Mirosław "ASoC: Failed to create codec register debugfs file\n"); 321731f1ab2SSebastien Guiriec } 322731f1ab2SSebastien Guiriec 323c3c5a19aSMark Brown static ssize_t codec_list_read_file(struct file *file, char __user *user_buf, 324c3c5a19aSMark Brown size_t count, loff_t *ppos) 325c3c5a19aSMark Brown { 326c3c5a19aSMark Brown char *buf = kmalloc(PAGE_SIZE, GFP_KERNEL); 3272b194f9dSMark Brown ssize_t len, ret = 0; 328c3c5a19aSMark Brown struct snd_soc_codec *codec; 329c3c5a19aSMark Brown 330c3c5a19aSMark Brown if (!buf) 331c3c5a19aSMark Brown return -ENOMEM; 332c3c5a19aSMark Brown 3332b194f9dSMark Brown list_for_each_entry(codec, &codec_list, list) { 3342b194f9dSMark Brown len = snprintf(buf + ret, PAGE_SIZE - ret, "%s\n", 335f4333203SLars-Peter Clausen codec->component.name); 3362b194f9dSMark Brown if (len >= 0) 3372b194f9dSMark Brown ret += len; 3382b194f9dSMark Brown if (ret > PAGE_SIZE) { 3392b194f9dSMark Brown ret = PAGE_SIZE; 3402b194f9dSMark Brown break; 3412b194f9dSMark Brown } 3422b194f9dSMark Brown } 343c3c5a19aSMark Brown 344c3c5a19aSMark Brown if (ret >= 0) 345c3c5a19aSMark Brown ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret); 346c3c5a19aSMark Brown 347c3c5a19aSMark Brown kfree(buf); 348c3c5a19aSMark Brown 349c3c5a19aSMark Brown return ret; 350c3c5a19aSMark Brown } 351c3c5a19aSMark Brown 352c3c5a19aSMark Brown static const struct file_operations codec_list_fops = { 353c3c5a19aSMark Brown .read = codec_list_read_file, 354c3c5a19aSMark Brown .llseek = default_llseek,/* read accesses f_pos */ 355c3c5a19aSMark Brown }; 356c3c5a19aSMark Brown 357f3208780SMark Brown static ssize_t dai_list_read_file(struct file *file, char __user *user_buf, 358f3208780SMark Brown size_t count, loff_t *ppos) 359f3208780SMark Brown { 360f3208780SMark Brown char *buf = kmalloc(PAGE_SIZE, GFP_KERNEL); 3612b194f9dSMark Brown ssize_t len, ret = 0; 3621438c2f6SLars-Peter Clausen struct snd_soc_component *component; 363f3208780SMark Brown struct snd_soc_dai *dai; 364f3208780SMark Brown 365f3208780SMark Brown if (!buf) 366f3208780SMark Brown return -ENOMEM; 367f3208780SMark Brown 3681438c2f6SLars-Peter Clausen list_for_each_entry(component, &component_list, list) { 3691438c2f6SLars-Peter Clausen list_for_each_entry(dai, &component->dai_list, list) { 3701438c2f6SLars-Peter Clausen len = snprintf(buf + ret, PAGE_SIZE - ret, "%s\n", 3711438c2f6SLars-Peter Clausen dai->name); 3722b194f9dSMark Brown if (len >= 0) 3732b194f9dSMark Brown ret += len; 3742b194f9dSMark Brown if (ret > PAGE_SIZE) { 3752b194f9dSMark Brown ret = PAGE_SIZE; 3762b194f9dSMark Brown break; 3772b194f9dSMark Brown } 3782b194f9dSMark Brown } 3791438c2f6SLars-Peter Clausen } 380f3208780SMark Brown 381f3208780SMark Brown ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret); 382f3208780SMark Brown 383f3208780SMark Brown kfree(buf); 384f3208780SMark Brown 385f3208780SMark Brown return ret; 386f3208780SMark Brown } 387f3208780SMark Brown 388f3208780SMark Brown static const struct file_operations dai_list_fops = { 389f3208780SMark Brown .read = dai_list_read_file, 390f3208780SMark Brown .llseek = default_llseek,/* read accesses f_pos */ 391f3208780SMark Brown }; 392f3208780SMark Brown 39319c7ac27SMark Brown static ssize_t platform_list_read_file(struct file *file, 39419c7ac27SMark Brown char __user *user_buf, 39519c7ac27SMark Brown size_t count, loff_t *ppos) 39619c7ac27SMark Brown { 39719c7ac27SMark Brown char *buf = kmalloc(PAGE_SIZE, GFP_KERNEL); 3982b194f9dSMark Brown ssize_t len, ret = 0; 39919c7ac27SMark Brown struct snd_soc_platform *platform; 40019c7ac27SMark Brown 40119c7ac27SMark Brown if (!buf) 40219c7ac27SMark Brown return -ENOMEM; 40319c7ac27SMark Brown 4042b194f9dSMark Brown list_for_each_entry(platform, &platform_list, list) { 4052b194f9dSMark Brown len = snprintf(buf + ret, PAGE_SIZE - ret, "%s\n", 406f4333203SLars-Peter Clausen platform->component.name); 4072b194f9dSMark Brown if (len >= 0) 4082b194f9dSMark Brown ret += len; 4092b194f9dSMark Brown if (ret > PAGE_SIZE) { 4102b194f9dSMark Brown ret = PAGE_SIZE; 4112b194f9dSMark Brown break; 4122b194f9dSMark Brown } 4132b194f9dSMark Brown } 41419c7ac27SMark Brown 41519c7ac27SMark Brown ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret); 41619c7ac27SMark Brown 41719c7ac27SMark Brown kfree(buf); 41819c7ac27SMark Brown 41919c7ac27SMark Brown return ret; 42019c7ac27SMark Brown } 42119c7ac27SMark Brown 42219c7ac27SMark Brown static const struct file_operations platform_list_fops = { 42319c7ac27SMark Brown .read = platform_list_read_file, 42419c7ac27SMark Brown .llseek = default_llseek,/* read accesses f_pos */ 42519c7ac27SMark Brown }; 42619c7ac27SMark Brown 427a6052154SJarkko Nikula static void soc_init_card_debugfs(struct snd_soc_card *card) 428a6052154SJarkko Nikula { 429a6052154SJarkko Nikula card->debugfs_card_root = debugfs_create_dir(card->name, 4308a9dab1aSMark Brown snd_soc_debugfs_root); 4313a45b867SJarkko Nikula if (!card->debugfs_card_root) { 432a6052154SJarkko Nikula dev_warn(card->dev, 4337c08be84SLothar Waßmann "ASoC: Failed to create card debugfs directory\n"); 4343a45b867SJarkko Nikula return; 4353a45b867SJarkko Nikula } 4363a45b867SJarkko Nikula 4373a45b867SJarkko Nikula card->debugfs_pop_time = debugfs_create_u32("dapm_pop_time", 0644, 4383a45b867SJarkko Nikula card->debugfs_card_root, 4393a45b867SJarkko Nikula &card->pop_time); 4403a45b867SJarkko Nikula if (!card->debugfs_pop_time) 4413a45b867SJarkko Nikula dev_warn(card->dev, 442f110bfc7SLiam Girdwood "ASoC: Failed to create pop time debugfs file\n"); 443a6052154SJarkko Nikula } 444a6052154SJarkko Nikula 445a6052154SJarkko Nikula static void soc_cleanup_card_debugfs(struct snd_soc_card *card) 446a6052154SJarkko Nikula { 447a6052154SJarkko Nikula debugfs_remove_recursive(card->debugfs_card_root); 448a6052154SJarkko Nikula } 449a6052154SJarkko Nikula 4502624d5faSMark Brown #else 4512624d5faSMark Brown 45281c7cfd1SLars-Peter Clausen #define soc_init_codec_debugfs NULL 45381c7cfd1SLars-Peter Clausen 45481c7cfd1SLars-Peter Clausen static inline void soc_init_component_debugfs( 45581c7cfd1SLars-Peter Clausen struct snd_soc_component *component) 4562624d5faSMark Brown { 4572624d5faSMark Brown } 4582624d5faSMark Brown 45981c7cfd1SLars-Peter Clausen static inline void soc_cleanup_component_debugfs( 46081c7cfd1SLars-Peter Clausen struct snd_soc_component *component) 461731f1ab2SSebastien Guiriec { 462731f1ab2SSebastien Guiriec } 463731f1ab2SSebastien Guiriec 464b95fccbcSAxel Lin static inline void soc_init_card_debugfs(struct snd_soc_card *card) 465b95fccbcSAxel Lin { 466b95fccbcSAxel Lin } 467b95fccbcSAxel Lin 468b95fccbcSAxel Lin static inline void soc_cleanup_card_debugfs(struct snd_soc_card *card) 469b95fccbcSAxel Lin { 470b95fccbcSAxel Lin } 4712624d5faSMark Brown #endif 4722624d5faSMark Brown 47347c88fffSLiam Girdwood struct snd_pcm_substream *snd_soc_get_dai_substream(struct snd_soc_card *card, 47447c88fffSLiam Girdwood const char *dai_link, int stream) 47547c88fffSLiam Girdwood { 47647c88fffSLiam Girdwood int i; 47747c88fffSLiam Girdwood 47847c88fffSLiam Girdwood for (i = 0; i < card->num_links; i++) { 47947c88fffSLiam Girdwood if (card->rtd[i].dai_link->no_pcm && 48047c88fffSLiam Girdwood !strcmp(card->rtd[i].dai_link->name, dai_link)) 48147c88fffSLiam Girdwood return card->rtd[i].pcm->streams[stream].substream; 48247c88fffSLiam Girdwood } 483f110bfc7SLiam Girdwood dev_dbg(card->dev, "ASoC: failed to find dai link %s\n", dai_link); 48447c88fffSLiam Girdwood return NULL; 48547c88fffSLiam Girdwood } 48647c88fffSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_get_dai_substream); 48747c88fffSLiam Girdwood 48847c88fffSLiam Girdwood struct snd_soc_pcm_runtime *snd_soc_get_pcm_runtime(struct snd_soc_card *card, 48947c88fffSLiam Girdwood const char *dai_link) 49047c88fffSLiam Girdwood { 49147c88fffSLiam Girdwood int i; 49247c88fffSLiam Girdwood 49347c88fffSLiam Girdwood for (i = 0; i < card->num_links; i++) { 49447c88fffSLiam Girdwood if (!strcmp(card->rtd[i].dai_link->name, dai_link)) 49547c88fffSLiam Girdwood return &card->rtd[i]; 49647c88fffSLiam Girdwood } 497f110bfc7SLiam Girdwood dev_dbg(card->dev, "ASoC: failed to find rtd %s\n", dai_link); 49847c88fffSLiam Girdwood return NULL; 49947c88fffSLiam Girdwood } 50047c88fffSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_get_pcm_runtime); 50147c88fffSLiam Girdwood 502db2a4165SFrank Mandarino #ifdef CONFIG_SND_SOC_AC97_BUS 503db2a4165SFrank Mandarino /* unregister ac97 codec */ 504db2a4165SFrank Mandarino static int soc_ac97_dev_unregister(struct snd_soc_codec *codec) 505db2a4165SFrank Mandarino { 506db2a4165SFrank Mandarino if (codec->ac97->dev.bus) 507db2a4165SFrank Mandarino device_unregister(&codec->ac97->dev); 508db2a4165SFrank Mandarino return 0; 509db2a4165SFrank Mandarino } 510db2a4165SFrank Mandarino 511db2a4165SFrank Mandarino /* stop no dev release warning */ 512db2a4165SFrank Mandarino static void soc_ac97_device_release(struct device *dev){} 513db2a4165SFrank Mandarino 514db2a4165SFrank Mandarino /* register ac97 codec to bus */ 515db2a4165SFrank Mandarino static int soc_ac97_dev_register(struct snd_soc_codec *codec) 516db2a4165SFrank Mandarino { 517db2a4165SFrank Mandarino int err; 518db2a4165SFrank Mandarino 519db2a4165SFrank Mandarino codec->ac97->dev.bus = &ac97_bus_type; 52000200107SLars-Peter Clausen codec->ac97->dev.parent = codec->component.card->dev; 521db2a4165SFrank Mandarino codec->ac97->dev.release = soc_ac97_device_release; 522db2a4165SFrank Mandarino 523bb072bf0SKay Sievers dev_set_name(&codec->ac97->dev, "%d-%d:%s", 52400200107SLars-Peter Clausen codec->component.card->snd_card->number, 0, 52500200107SLars-Peter Clausen codec->component.name); 526db2a4165SFrank Mandarino err = device_register(&codec->ac97->dev); 527db2a4165SFrank Mandarino if (err < 0) { 528f110bfc7SLiam Girdwood dev_err(codec->dev, "ASoC: Can't register ac97 bus\n"); 529db2a4165SFrank Mandarino codec->ac97->dev.bus = NULL; 530db2a4165SFrank Mandarino return err; 531db2a4165SFrank Mandarino } 532db2a4165SFrank Mandarino return 0; 533db2a4165SFrank Mandarino } 534db2a4165SFrank Mandarino #endif 535db2a4165SFrank Mandarino 5369d58a077SRichard Fitzgerald static void codec2codec_close_delayed_work(struct work_struct *work) 5379d58a077SRichard Fitzgerald { 5389d58a077SRichard Fitzgerald /* Currently nothing to do for c2c links 5399d58a077SRichard Fitzgerald * Since c2c links are internal nodes in the DAPM graph and 5409d58a077SRichard Fitzgerald * don't interface with the outside world or application layer 5419d58a077SRichard Fitzgerald * we don't have to do any special handling on close. 5429d58a077SRichard Fitzgerald */ 5439d58a077SRichard Fitzgerald } 5449d58a077SRichard Fitzgerald 5456f8ab4acSMark Brown #ifdef CONFIG_PM_SLEEP 546db2a4165SFrank Mandarino /* powers down audio subsystem for suspend */ 5476f8ab4acSMark Brown int snd_soc_suspend(struct device *dev) 548db2a4165SFrank Mandarino { 5496f8ab4acSMark Brown struct snd_soc_card *card = dev_get_drvdata(dev); 5502eea392dSJarkko Nikula struct snd_soc_codec *codec; 55188bd870fSBenoit Cousson int i, j; 552db2a4165SFrank Mandarino 553c5599b87SLars-Peter Clausen /* If the card is not initialized yet there is nothing to do */ 554c5599b87SLars-Peter Clausen if (!card->instantiated) 555e3509ff0SDaniel Mack return 0; 556e3509ff0SDaniel Mack 5576ed25978SAndy Green /* Due to the resume being scheduled into a workqueue we could 5586ed25978SAndy Green * suspend before that's finished - wait for it to complete. 5596ed25978SAndy Green */ 560f0fba2adSLiam Girdwood snd_power_lock(card->snd_card); 561f0fba2adSLiam Girdwood snd_power_wait(card->snd_card, SNDRV_CTL_POWER_D0); 562f0fba2adSLiam Girdwood snd_power_unlock(card->snd_card); 5636ed25978SAndy Green 5646ed25978SAndy Green /* we're going to block userspace touching us until resume completes */ 565f0fba2adSLiam Girdwood snd_power_change_state(card->snd_card, SNDRV_CTL_POWER_D3hot); 5666ed25978SAndy Green 567a00f90f9SMark Brown /* mute any active DACs */ 568f0fba2adSLiam Girdwood for (i = 0; i < card->num_rtd; i++) { 5693efab7dcSMark Brown 570f0fba2adSLiam Girdwood if (card->rtd[i].dai_link->ignore_suspend) 5713efab7dcSMark Brown continue; 5723efab7dcSMark Brown 57388bd870fSBenoit Cousson for (j = 0; j < card->rtd[i].num_codecs; j++) { 57488bd870fSBenoit Cousson struct snd_soc_dai *dai = card->rtd[i].codec_dais[j]; 57588bd870fSBenoit Cousson struct snd_soc_dai_driver *drv = dai->driver; 57688bd870fSBenoit Cousson 577f0fba2adSLiam Girdwood if (drv->ops->digital_mute && dai->playback_active) 578f0fba2adSLiam Girdwood drv->ops->digital_mute(dai, 1); 579db2a4165SFrank Mandarino } 58088bd870fSBenoit Cousson } 581db2a4165SFrank Mandarino 5824ccab3e7SLiam Girdwood /* suspend all pcms */ 583f0fba2adSLiam Girdwood for (i = 0; i < card->num_rtd; i++) { 584f0fba2adSLiam Girdwood if (card->rtd[i].dai_link->ignore_suspend) 5853efab7dcSMark Brown continue; 5863efab7dcSMark Brown 587f0fba2adSLiam Girdwood snd_pcm_suspend_all(card->rtd[i].pcm); 5883efab7dcSMark Brown } 5894ccab3e7SLiam Girdwood 59087506549SMark Brown if (card->suspend_pre) 59170b2ac12SMark Brown card->suspend_pre(card); 592db2a4165SFrank Mandarino 593f0fba2adSLiam Girdwood for (i = 0; i < card->num_rtd; i++) { 594f0fba2adSLiam Girdwood struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai; 595f0fba2adSLiam Girdwood struct snd_soc_platform *platform = card->rtd[i].platform; 5963efab7dcSMark Brown 597f0fba2adSLiam Girdwood if (card->rtd[i].dai_link->ignore_suspend) 5983efab7dcSMark Brown continue; 5993efab7dcSMark Brown 600f0fba2adSLiam Girdwood if (cpu_dai->driver->suspend && !cpu_dai->driver->ac97_control) 601f0fba2adSLiam Girdwood cpu_dai->driver->suspend(cpu_dai); 602f0fba2adSLiam Girdwood if (platform->driver->suspend && !platform->suspended) { 603f0fba2adSLiam Girdwood platform->driver->suspend(cpu_dai); 604f0fba2adSLiam Girdwood platform->suspended = 1; 605f0fba2adSLiam Girdwood } 606db2a4165SFrank Mandarino } 607db2a4165SFrank Mandarino 608db2a4165SFrank Mandarino /* close any waiting streams and save state */ 609f0fba2adSLiam Girdwood for (i = 0; i < card->num_rtd; i++) { 61088bd870fSBenoit Cousson struct snd_soc_dai **codec_dais = card->rtd[i].codec_dais; 61143829731STejun Heo flush_delayed_work(&card->rtd[i].delayed_work); 61288bd870fSBenoit Cousson for (j = 0; j < card->rtd[i].num_codecs; j++) { 61388bd870fSBenoit Cousson codec_dais[j]->codec->dapm.suspend_bias_level = 61488bd870fSBenoit Cousson codec_dais[j]->codec->dapm.bias_level; 61588bd870fSBenoit Cousson } 616f0fba2adSLiam Girdwood } 617db2a4165SFrank Mandarino 618f0fba2adSLiam Girdwood for (i = 0; i < card->num_rtd; i++) { 6193efab7dcSMark Brown 620f0fba2adSLiam Girdwood if (card->rtd[i].dai_link->ignore_suspend) 6213efab7dcSMark Brown continue; 6223efab7dcSMark Brown 6237bd3a6f3SMark Brown snd_soc_dapm_stream_event(&card->rtd[i], 6247bd3a6f3SMark Brown SNDRV_PCM_STREAM_PLAYBACK, 625db2a4165SFrank Mandarino SND_SOC_DAPM_STREAM_SUSPEND); 626f0fba2adSLiam Girdwood 6277bd3a6f3SMark Brown snd_soc_dapm_stream_event(&card->rtd[i], 6287bd3a6f3SMark Brown SNDRV_PCM_STREAM_CAPTURE, 629db2a4165SFrank Mandarino SND_SOC_DAPM_STREAM_SUSPEND); 630db2a4165SFrank Mandarino } 631db2a4165SFrank Mandarino 632e2d32ff6SMark Brown /* Recheck all analogue paths too */ 633e2d32ff6SMark Brown dapm_mark_io_dirty(&card->dapm); 634e2d32ff6SMark Brown snd_soc_dapm_sync(&card->dapm); 635e2d32ff6SMark Brown 636f0fba2adSLiam Girdwood /* suspend all CODECs */ 6372eea392dSJarkko Nikula list_for_each_entry(codec, &card->codec_dev_list, card_list) { 6381547aba9SMark Brown /* If there are paths active then the CODEC will be held with 6391547aba9SMark Brown * bias _ON and should not be suspended. */ 640a8093297SLars-Peter Clausen if (!codec->suspended) { 641ce6120ccSLiam Girdwood switch (codec->dapm.bias_level) { 6421547aba9SMark Brown case SND_SOC_BIAS_STANDBY: 643125a25daSMark Brown /* 644125a25daSMark Brown * If the CODEC is capable of idle 645125a25daSMark Brown * bias off then being in STANDBY 646125a25daSMark Brown * means it's doing something, 647125a25daSMark Brown * otherwise fall through. 648125a25daSMark Brown */ 649125a25daSMark Brown if (codec->dapm.idle_bias_off) { 650125a25daSMark Brown dev_dbg(codec->dev, 65110e8aa9aSMichał Mirosław "ASoC: idle_bias_off CODEC on over suspend\n"); 652125a25daSMark Brown break; 653125a25daSMark Brown } 654a8093297SLars-Peter Clausen 6551547aba9SMark Brown case SND_SOC_BIAS_OFF: 656a8093297SLars-Peter Clausen if (codec->driver->suspend) 65784b315eeSLars-Peter Clausen codec->driver->suspend(codec); 658f0fba2adSLiam Girdwood codec->suspended = 1; 6597be4ba24SMark Brown codec->cache_sync = 1; 660e2c330b9SLars-Peter Clausen if (codec->component.regmap) 661e2c330b9SLars-Peter Clausen regcache_mark_dirty(codec->component.regmap); 662988e8cc4SNicolin Chen /* deactivate pins to sleep state */ 663988e8cc4SNicolin Chen pinctrl_pm_select_sleep_state(codec->dev); 6641547aba9SMark Brown break; 6651547aba9SMark Brown default: 66610e8aa9aSMichał Mirosław dev_dbg(codec->dev, 66710e8aa9aSMichał Mirosław "ASoC: CODEC is on over suspend\n"); 6681547aba9SMark Brown break; 6691547aba9SMark Brown } 6701547aba9SMark Brown } 671f0fba2adSLiam Girdwood } 672db2a4165SFrank Mandarino 673f0fba2adSLiam Girdwood for (i = 0; i < card->num_rtd; i++) { 674f0fba2adSLiam Girdwood struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai; 6753efab7dcSMark Brown 676f0fba2adSLiam Girdwood if (card->rtd[i].dai_link->ignore_suspend) 6773efab7dcSMark Brown continue; 6783efab7dcSMark Brown 679f0fba2adSLiam Girdwood if (cpu_dai->driver->suspend && cpu_dai->driver->ac97_control) 680f0fba2adSLiam Girdwood cpu_dai->driver->suspend(cpu_dai); 681988e8cc4SNicolin Chen 682988e8cc4SNicolin Chen /* deactivate pins to sleep state */ 683988e8cc4SNicolin Chen pinctrl_pm_select_sleep_state(cpu_dai->dev); 684db2a4165SFrank Mandarino } 685db2a4165SFrank Mandarino 68687506549SMark Brown if (card->suspend_post) 68770b2ac12SMark Brown card->suspend_post(card); 688db2a4165SFrank Mandarino 689db2a4165SFrank Mandarino return 0; 690db2a4165SFrank Mandarino } 6916f8ab4acSMark Brown EXPORT_SYMBOL_GPL(snd_soc_suspend); 692db2a4165SFrank Mandarino 6936ed25978SAndy Green /* deferred resume work, so resume can complete before we finished 6946ed25978SAndy Green * setting our codec back up, which can be very slow on I2C 6956ed25978SAndy Green */ 6966ed25978SAndy Green static void soc_resume_deferred(struct work_struct *work) 697db2a4165SFrank Mandarino { 698f0fba2adSLiam Girdwood struct snd_soc_card *card = 699f0fba2adSLiam Girdwood container_of(work, struct snd_soc_card, deferred_resume_work); 7002eea392dSJarkko Nikula struct snd_soc_codec *codec; 70188bd870fSBenoit Cousson int i, j; 702db2a4165SFrank Mandarino 7036ed25978SAndy Green /* our power state is still SNDRV_CTL_POWER_D3hot from suspend time, 7046ed25978SAndy Green * so userspace apps are blocked from touching us 7056ed25978SAndy Green */ 7066ed25978SAndy Green 707f110bfc7SLiam Girdwood dev_dbg(card->dev, "ASoC: starting resume work\n"); 7086ed25978SAndy Green 7099949788bSMark Brown /* Bring us up into D2 so that DAPM starts enabling things */ 710f0fba2adSLiam Girdwood snd_power_change_state(card->snd_card, SNDRV_CTL_POWER_D2); 7119949788bSMark Brown 71287506549SMark Brown if (card->resume_pre) 71370b2ac12SMark Brown card->resume_pre(card); 714db2a4165SFrank Mandarino 715f0fba2adSLiam Girdwood /* resume AC97 DAIs */ 716f0fba2adSLiam Girdwood for (i = 0; i < card->num_rtd; i++) { 717f0fba2adSLiam Girdwood struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai; 7183efab7dcSMark Brown 719f0fba2adSLiam Girdwood if (card->rtd[i].dai_link->ignore_suspend) 7203efab7dcSMark Brown continue; 7213efab7dcSMark Brown 722f0fba2adSLiam Girdwood if (cpu_dai->driver->resume && cpu_dai->driver->ac97_control) 723f0fba2adSLiam Girdwood cpu_dai->driver->resume(cpu_dai); 724db2a4165SFrank Mandarino } 725db2a4165SFrank Mandarino 7262eea392dSJarkko Nikula list_for_each_entry(codec, &card->codec_dev_list, card_list) { 7271547aba9SMark Brown /* If the CODEC was idle over suspend then it will have been 7281547aba9SMark Brown * left with bias OFF or STANDBY and suspended so we must now 7291547aba9SMark Brown * resume. Otherwise the suspend was suppressed. 7301547aba9SMark Brown */ 731a8093297SLars-Peter Clausen if (codec->suspended) { 732ce6120ccSLiam Girdwood switch (codec->dapm.bias_level) { 7331547aba9SMark Brown case SND_SOC_BIAS_STANDBY: 7341547aba9SMark Brown case SND_SOC_BIAS_OFF: 735a8093297SLars-Peter Clausen if (codec->driver->resume) 736f0fba2adSLiam Girdwood codec->driver->resume(codec); 737f0fba2adSLiam Girdwood codec->suspended = 0; 7381547aba9SMark Brown break; 7391547aba9SMark Brown default: 74010e8aa9aSMichał Mirosław dev_dbg(codec->dev, 74110e8aa9aSMichał Mirosław "ASoC: CODEC was on over suspend\n"); 7421547aba9SMark Brown break; 7431547aba9SMark Brown } 7441547aba9SMark Brown } 745f0fba2adSLiam Girdwood } 746db2a4165SFrank Mandarino 747f0fba2adSLiam Girdwood for (i = 0; i < card->num_rtd; i++) { 7483efab7dcSMark Brown 749f0fba2adSLiam Girdwood if (card->rtd[i].dai_link->ignore_suspend) 7503efab7dcSMark Brown continue; 7513efab7dcSMark Brown 7527bd3a6f3SMark Brown snd_soc_dapm_stream_event(&card->rtd[i], 753d9b0951bSLiam Girdwood SNDRV_PCM_STREAM_PLAYBACK, 754db2a4165SFrank Mandarino SND_SOC_DAPM_STREAM_RESUME); 755f0fba2adSLiam Girdwood 7567bd3a6f3SMark Brown snd_soc_dapm_stream_event(&card->rtd[i], 757d9b0951bSLiam Girdwood SNDRV_PCM_STREAM_CAPTURE, 758db2a4165SFrank Mandarino SND_SOC_DAPM_STREAM_RESUME); 759db2a4165SFrank Mandarino } 760db2a4165SFrank Mandarino 7613ff3f64bSMark Brown /* unmute any active DACs */ 762f0fba2adSLiam Girdwood for (i = 0; i < card->num_rtd; i++) { 7633efab7dcSMark Brown 764f0fba2adSLiam Girdwood if (card->rtd[i].dai_link->ignore_suspend) 7653efab7dcSMark Brown continue; 7663efab7dcSMark Brown 76788bd870fSBenoit Cousson for (j = 0; j < card->rtd[i].num_codecs; j++) { 76888bd870fSBenoit Cousson struct snd_soc_dai *dai = card->rtd[i].codec_dais[j]; 76988bd870fSBenoit Cousson struct snd_soc_dai_driver *drv = dai->driver; 77088bd870fSBenoit Cousson 771f0fba2adSLiam Girdwood if (drv->ops->digital_mute && dai->playback_active) 772f0fba2adSLiam Girdwood drv->ops->digital_mute(dai, 0); 773db2a4165SFrank Mandarino } 77488bd870fSBenoit Cousson } 775db2a4165SFrank Mandarino 776f0fba2adSLiam Girdwood for (i = 0; i < card->num_rtd; i++) { 777f0fba2adSLiam Girdwood struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai; 778f0fba2adSLiam Girdwood struct snd_soc_platform *platform = card->rtd[i].platform; 7793efab7dcSMark Brown 780f0fba2adSLiam Girdwood if (card->rtd[i].dai_link->ignore_suspend) 7813efab7dcSMark Brown continue; 7823efab7dcSMark Brown 783f0fba2adSLiam Girdwood if (cpu_dai->driver->resume && !cpu_dai->driver->ac97_control) 784f0fba2adSLiam Girdwood cpu_dai->driver->resume(cpu_dai); 785f0fba2adSLiam Girdwood if (platform->driver->resume && platform->suspended) { 786f0fba2adSLiam Girdwood platform->driver->resume(cpu_dai); 787f0fba2adSLiam Girdwood platform->suspended = 0; 788f0fba2adSLiam Girdwood } 789db2a4165SFrank Mandarino } 790db2a4165SFrank Mandarino 79187506549SMark Brown if (card->resume_post) 79270b2ac12SMark Brown card->resume_post(card); 793db2a4165SFrank Mandarino 794f110bfc7SLiam Girdwood dev_dbg(card->dev, "ASoC: resume work completed\n"); 7956ed25978SAndy Green 7966ed25978SAndy Green /* userspace can access us now we are back as we were before */ 797f0fba2adSLiam Girdwood snd_power_change_state(card->snd_card, SNDRV_CTL_POWER_D0); 798e2d32ff6SMark Brown 799e2d32ff6SMark Brown /* Recheck all analogue paths too */ 800e2d32ff6SMark Brown dapm_mark_io_dirty(&card->dapm); 801e2d32ff6SMark Brown snd_soc_dapm_sync(&card->dapm); 8026ed25978SAndy Green } 8036ed25978SAndy Green 8046ed25978SAndy Green /* powers up audio subsystem after a suspend */ 8056f8ab4acSMark Brown int snd_soc_resume(struct device *dev) 8066ed25978SAndy Green { 8076f8ab4acSMark Brown struct snd_soc_card *card = dev_get_drvdata(dev); 80882e14e8bSStephen Warren int i, ac97_control = 0; 809b9dd94a8SPeter Ujfalusi 810c5599b87SLars-Peter Clausen /* If the card is not initialized yet there is nothing to do */ 811c5599b87SLars-Peter Clausen if (!card->instantiated) 8125ff1ddf2SEric Miao return 0; 8135ff1ddf2SEric Miao 814988e8cc4SNicolin Chen /* activate pins from sleep state */ 815988e8cc4SNicolin Chen for (i = 0; i < card->num_rtd; i++) { 81688bd870fSBenoit Cousson struct snd_soc_pcm_runtime *rtd = &card->rtd[i]; 81788bd870fSBenoit Cousson struct snd_soc_dai **codec_dais = rtd->codec_dais; 81888bd870fSBenoit Cousson struct snd_soc_dai *cpu_dai = rtd->cpu_dai; 81988bd870fSBenoit Cousson int j; 82088bd870fSBenoit Cousson 821988e8cc4SNicolin Chen if (cpu_dai->active) 822988e8cc4SNicolin Chen pinctrl_pm_select_default_state(cpu_dai->dev); 82388bd870fSBenoit Cousson 82488bd870fSBenoit Cousson for (j = 0; j < rtd->num_codecs; j++) { 82588bd870fSBenoit Cousson struct snd_soc_dai *codec_dai = codec_dais[j]; 826988e8cc4SNicolin Chen if (codec_dai->active) 827988e8cc4SNicolin Chen pinctrl_pm_select_default_state(codec_dai->dev); 828988e8cc4SNicolin Chen } 82988bd870fSBenoit Cousson } 830988e8cc4SNicolin Chen 83164ab9baaSMark Brown /* AC97 devices might have other drivers hanging off them so 83264ab9baaSMark Brown * need to resume immediately. Other drivers don't have that 83364ab9baaSMark Brown * problem and may take a substantial amount of time to resume 83464ab9baaSMark Brown * due to I/O costs and anti-pop so handle them out of line. 83564ab9baaSMark Brown */ 836f0fba2adSLiam Girdwood for (i = 0; i < card->num_rtd; i++) { 837f0fba2adSLiam Girdwood struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai; 83882e14e8bSStephen Warren ac97_control |= cpu_dai->driver->ac97_control; 83982e14e8bSStephen Warren } 84082e14e8bSStephen Warren if (ac97_control) { 841f110bfc7SLiam Girdwood dev_dbg(dev, "ASoC: Resuming AC97 immediately\n"); 84264ab9baaSMark Brown soc_resume_deferred(&card->deferred_resume_work); 84364ab9baaSMark Brown } else { 844f110bfc7SLiam Girdwood dev_dbg(dev, "ASoC: Scheduling resume work\n"); 8456308419aSMark Brown if (!schedule_work(&card->deferred_resume_work)) 846f110bfc7SLiam Girdwood dev_err(dev, "ASoC: resume work item may be lost\n"); 847f0fba2adSLiam Girdwood } 8486ed25978SAndy Green 849db2a4165SFrank Mandarino return 0; 850db2a4165SFrank Mandarino } 8516f8ab4acSMark Brown EXPORT_SYMBOL_GPL(snd_soc_resume); 852db2a4165SFrank Mandarino #else 8536f8ab4acSMark Brown #define snd_soc_suspend NULL 8546f8ab4acSMark Brown #define snd_soc_resume NULL 855db2a4165SFrank Mandarino #endif 856db2a4165SFrank Mandarino 85785e7652dSLars-Peter Clausen static const struct snd_soc_dai_ops null_dai_ops = { 85802a06d30SBarry Song }; 85902a06d30SBarry Song 86065d9361fSLars-Peter Clausen static struct snd_soc_component *soc_find_component( 86165d9361fSLars-Peter Clausen const struct device_node *of_node, const char *name) 86212023a9aSMisael Lopez Cruz { 86365d9361fSLars-Peter Clausen struct snd_soc_component *component; 86412023a9aSMisael Lopez Cruz 86565d9361fSLars-Peter Clausen list_for_each_entry(component, &component_list, list) { 86665d9361fSLars-Peter Clausen if (of_node) { 86765d9361fSLars-Peter Clausen if (component->dev->of_node == of_node) 86865d9361fSLars-Peter Clausen return component; 86965d9361fSLars-Peter Clausen } else if (strcmp(component->name, name) == 0) { 87065d9361fSLars-Peter Clausen return component; 87112023a9aSMisael Lopez Cruz } 87212023a9aSMisael Lopez Cruz } 87312023a9aSMisael Lopez Cruz 87412023a9aSMisael Lopez Cruz return NULL; 87512023a9aSMisael Lopez Cruz } 87612023a9aSMisael Lopez Cruz 87714621c7eSLars-Peter Clausen static struct snd_soc_dai *snd_soc_find_dai( 87814621c7eSLars-Peter Clausen const struct snd_soc_dai_link_component *dlc) 87912023a9aSMisael Lopez Cruz { 88014621c7eSLars-Peter Clausen struct snd_soc_component *component; 88114621c7eSLars-Peter Clausen struct snd_soc_dai *dai; 88212023a9aSMisael Lopez Cruz 88314621c7eSLars-Peter Clausen /* Find CPU DAI from registered DAIs*/ 88414621c7eSLars-Peter Clausen list_for_each_entry(component, &component_list, list) { 88514621c7eSLars-Peter Clausen if (dlc->of_node && component->dev->of_node != dlc->of_node) 88612023a9aSMisael Lopez Cruz continue; 887*1ffae361SLars-Peter Clausen if (dlc->name && strcmp(component->name, dlc->name)) 88812023a9aSMisael Lopez Cruz continue; 88914621c7eSLars-Peter Clausen list_for_each_entry(dai, &component->dai_list, list) { 89014621c7eSLars-Peter Clausen if (dlc->dai_name && strcmp(dai->name, dlc->dai_name)) 89114621c7eSLars-Peter Clausen continue; 89212023a9aSMisael Lopez Cruz 89314621c7eSLars-Peter Clausen return dai; 89412023a9aSMisael Lopez Cruz } 89512023a9aSMisael Lopez Cruz } 89612023a9aSMisael Lopez Cruz 89712023a9aSMisael Lopez Cruz return NULL; 89812023a9aSMisael Lopez Cruz } 89912023a9aSMisael Lopez Cruz 900f0fba2adSLiam Girdwood static int soc_bind_dai_link(struct snd_soc_card *card, int num) 901db2a4165SFrank Mandarino { 902f0fba2adSLiam Girdwood struct snd_soc_dai_link *dai_link = &card->dai_link[num]; 903f0fba2adSLiam Girdwood struct snd_soc_pcm_runtime *rtd = &card->rtd[num]; 90488bd870fSBenoit Cousson struct snd_soc_dai_link_component *codecs = dai_link->codecs; 90514621c7eSLars-Peter Clausen struct snd_soc_dai_link_component cpu_dai_component; 90688bd870fSBenoit Cousson struct snd_soc_dai **codec_dais = rtd->codec_dais; 907435c5e25SMark Brown struct snd_soc_platform *platform; 908848dd8beSMark Brown const char *platform_name; 90988bd870fSBenoit Cousson int i; 910db2a4165SFrank Mandarino 911f110bfc7SLiam Girdwood dev_dbg(card->dev, "ASoC: binding %s at idx %d\n", dai_link->name, num); 9126308419aSMark Brown 91314621c7eSLars-Peter Clausen cpu_dai_component.name = dai_link->cpu_name; 91414621c7eSLars-Peter Clausen cpu_dai_component.of_node = dai_link->cpu_of_node; 91514621c7eSLars-Peter Clausen cpu_dai_component.dai_name = dai_link->cpu_dai_name; 91614621c7eSLars-Peter Clausen rtd->cpu_dai = snd_soc_find_dai(&cpu_dai_component); 917b19e6e7bSMark Brown if (!rtd->cpu_dai) { 918f110bfc7SLiam Girdwood dev_err(card->dev, "ASoC: CPU DAI %s not registered\n", 919f0fba2adSLiam Girdwood dai_link->cpu_dai_name); 920b19e6e7bSMark Brown return -EPROBE_DEFER; 921c5af3a2eSMark Brown } 922c5af3a2eSMark Brown 92388bd870fSBenoit Cousson rtd->num_codecs = dai_link->num_codecs; 92488bd870fSBenoit Cousson 92588bd870fSBenoit Cousson /* Find CODEC from registered CODECs */ 92688bd870fSBenoit Cousson for (i = 0; i < rtd->num_codecs; i++) { 92714621c7eSLars-Peter Clausen codec_dais[i] = snd_soc_find_dai(&codecs[i]); 92888bd870fSBenoit Cousson if (!codec_dais[i]) { 92912023a9aSMisael Lopez Cruz dev_err(card->dev, "ASoC: CODEC DAI %s not registered\n", 93088bd870fSBenoit Cousson codecs[i].dai_name); 93112023a9aSMisael Lopez Cruz return -EPROBE_DEFER; 93212023a9aSMisael Lopez Cruz } 93388bd870fSBenoit Cousson } 93488bd870fSBenoit Cousson 93588bd870fSBenoit Cousson /* Single codec links expect codec and codec_dai in runtime data */ 93688bd870fSBenoit Cousson rtd->codec_dai = codec_dais[0]; 93788bd870fSBenoit Cousson rtd->codec = rtd->codec_dai->codec; 93812023a9aSMisael Lopez Cruz 939848dd8beSMark Brown /* if there's no platform we match on the empty platform */ 940848dd8beSMark Brown platform_name = dai_link->platform_name; 9415a504963SStephen Warren if (!platform_name && !dai_link->platform_of_node) 942848dd8beSMark Brown platform_name = "snd-soc-dummy"; 943848dd8beSMark Brown 944b19e6e7bSMark Brown /* find one from the set of registered platforms */ 945f0fba2adSLiam Girdwood list_for_each_entry(platform, &platform_list, list) { 9465a504963SStephen Warren if (dai_link->platform_of_node) { 9475a504963SStephen Warren if (platform->dev->of_node != 9485a504963SStephen Warren dai_link->platform_of_node) 9495a504963SStephen Warren continue; 9505a504963SStephen Warren } else { 951f4333203SLars-Peter Clausen if (strcmp(platform->component.name, platform_name)) 9522610ab77SStephen Warren continue; 9535a504963SStephen Warren } 9542610ab77SStephen Warren 955f0fba2adSLiam Girdwood rtd->platform = platform; 956f0fba2adSLiam Girdwood } 957b19e6e7bSMark Brown if (!rtd->platform) { 958f110bfc7SLiam Girdwood dev_err(card->dev, "ASoC: platform %s not registered\n", 959f0fba2adSLiam Girdwood dai_link->platform_name); 960b19e6e7bSMark Brown return -EPROBE_DEFER; 961f0fba2adSLiam Girdwood } 962b19e6e7bSMark Brown 963b19e6e7bSMark Brown card->num_rtd++; 964b19e6e7bSMark Brown 965b19e6e7bSMark Brown return 0; 9666b05eda6SMark Brown } 9676b05eda6SMark Brown 968f1d45cc3SLars-Peter Clausen static void soc_remove_component(struct snd_soc_component *component) 969d12cd198SStephen Warren { 97070090bbbSLars-Peter Clausen if (!component->probed) 97170090bbbSLars-Peter Clausen return; 972d12cd198SStephen Warren 973f1d45cc3SLars-Peter Clausen /* This is a HACK and will be removed soon */ 974f1d45cc3SLars-Peter Clausen if (component->codec) 975f1d45cc3SLars-Peter Clausen list_del(&component->codec->card_list); 976d12cd198SStephen Warren 977f1d45cc3SLars-Peter Clausen if (component->remove) 978f1d45cc3SLars-Peter Clausen component->remove(component); 979d12cd198SStephen Warren 980f1d45cc3SLars-Peter Clausen snd_soc_dapm_free(snd_soc_component_get_dapm(component)); 981d12cd198SStephen Warren 982f1d45cc3SLars-Peter Clausen soc_cleanup_component_debugfs(component); 983f1d45cc3SLars-Peter Clausen component->probed = 0; 984f1d45cc3SLars-Peter Clausen module_put(component->dev->driver->owner); 985d12cd198SStephen Warren } 986d12cd198SStephen Warren 987e60cd14fSLars-Peter Clausen static void soc_remove_dai(struct snd_soc_dai *dai, int order) 988589c3563SJarkko Nikula { 989589c3563SJarkko Nikula int err; 990589c3563SJarkko Nikula 991e60cd14fSLars-Peter Clausen if (dai && dai->probed && 992e60cd14fSLars-Peter Clausen dai->driver->remove_order == order) { 993e60cd14fSLars-Peter Clausen if (dai->driver->remove) { 994e60cd14fSLars-Peter Clausen err = dai->driver->remove(dai); 995589c3563SJarkko Nikula if (err < 0) 996e60cd14fSLars-Peter Clausen dev_err(dai->dev, 997b0aa88afSMisael Lopez Cruz "ASoC: failed to remove %s: %d\n", 998e60cd14fSLars-Peter Clausen dai->name, err); 999b0aa88afSMisael Lopez Cruz } 1000e60cd14fSLars-Peter Clausen dai->probed = 0; 1001b0aa88afSMisael Lopez Cruz } 1002b0aa88afSMisael Lopez Cruz } 1003b0aa88afSMisael Lopez Cruz 100462ae68faSStephen Warren static void soc_remove_link_dais(struct snd_soc_card *card, int num, int order) 1005f0fba2adSLiam Girdwood { 1006f0fba2adSLiam Girdwood struct snd_soc_pcm_runtime *rtd = &card->rtd[num]; 1007e60cd14fSLars-Peter Clausen int i; 1008f0fba2adSLiam Girdwood 1009f0fba2adSLiam Girdwood /* unregister the rtd device */ 1010f0fba2adSLiam Girdwood if (rtd->dev_registered) { 101136ae1a96SMark Brown device_remove_file(rtd->dev, &dev_attr_pmdown_time); 101236ae1a96SMark Brown device_remove_file(rtd->dev, &dev_attr_codec_reg); 101336ae1a96SMark Brown device_unregister(rtd->dev); 1014f0fba2adSLiam Girdwood rtd->dev_registered = 0; 101502a06d30SBarry Song } 101602a06d30SBarry Song 1017f0fba2adSLiam Girdwood /* remove the CODEC DAI */ 101888bd870fSBenoit Cousson for (i = 0; i < rtd->num_codecs; i++) 1019e60cd14fSLars-Peter Clausen soc_remove_dai(rtd->codec_dais[i], order); 1020f0fba2adSLiam Girdwood 1021e60cd14fSLars-Peter Clausen soc_remove_dai(rtd->cpu_dai, order); 1022f0fba2adSLiam Girdwood } 1023f0fba2adSLiam Girdwood 102462ae68faSStephen Warren static void soc_remove_link_components(struct snd_soc_card *card, int num, 102562ae68faSStephen Warren int order) 102662ae68faSStephen Warren { 102762ae68faSStephen Warren struct snd_soc_pcm_runtime *rtd = &card->rtd[num]; 102862ae68faSStephen Warren struct snd_soc_dai *cpu_dai = rtd->cpu_dai; 102962ae68faSStephen Warren struct snd_soc_platform *platform = rtd->platform; 103061aca564SLars-Peter Clausen struct snd_soc_component *component; 103188bd870fSBenoit Cousson int i; 103262ae68faSStephen Warren 103362ae68faSStephen Warren /* remove the platform */ 103470090bbbSLars-Peter Clausen if (platform && platform->component.driver->remove_order == order) 1035f1d45cc3SLars-Peter Clausen soc_remove_component(&platform->component); 103662ae68faSStephen Warren 103762ae68faSStephen Warren /* remove the CODEC-side CODEC */ 103888bd870fSBenoit Cousson for (i = 0; i < rtd->num_codecs; i++) { 103961aca564SLars-Peter Clausen component = rtd->codec_dais[i]->component; 104070090bbbSLars-Peter Clausen if (component->driver->remove_order == order) 104161aca564SLars-Peter Clausen soc_remove_component(component); 104262ae68faSStephen Warren } 104362ae68faSStephen Warren 104462ae68faSStephen Warren /* remove any CPU-side CODEC */ 104562ae68faSStephen Warren if (cpu_dai) { 104670090bbbSLars-Peter Clausen if (cpu_dai->component->driver->remove_order == order) 104761aca564SLars-Peter Clausen soc_remove_component(cpu_dai->component); 104862ae68faSStephen Warren } 104962ae68faSStephen Warren } 105062ae68faSStephen Warren 10510671fd8eSKuninori Morimoto static void soc_remove_dai_links(struct snd_soc_card *card) 10520671fd8eSKuninori Morimoto { 10530168bf0dSLiam Girdwood int dai, order; 10540671fd8eSKuninori Morimoto 10550168bf0dSLiam Girdwood for (order = SND_SOC_COMP_ORDER_FIRST; order <= SND_SOC_COMP_ORDER_LAST; 10560168bf0dSLiam Girdwood order++) { 10570168bf0dSLiam Girdwood for (dai = 0; dai < card->num_rtd; dai++) 105862ae68faSStephen Warren soc_remove_link_dais(card, dai, order); 10590168bf0dSLiam Girdwood } 106062ae68faSStephen Warren 106162ae68faSStephen Warren for (order = SND_SOC_COMP_ORDER_FIRST; order <= SND_SOC_COMP_ORDER_LAST; 106262ae68faSStephen Warren order++) { 106362ae68faSStephen Warren for (dai = 0; dai < card->num_rtd; dai++) 106462ae68faSStephen Warren soc_remove_link_components(card, dai, order); 106562ae68faSStephen Warren } 106662ae68faSStephen Warren 10670671fd8eSKuninori Morimoto card->num_rtd = 0; 10680671fd8eSKuninori Morimoto } 10690671fd8eSKuninori Morimoto 1070ead9b919SJarkko Nikula static void soc_set_name_prefix(struct snd_soc_card *card, 107194f99c87SLars-Peter Clausen struct snd_soc_component *component) 1072ead9b919SJarkko Nikula { 1073ead9b919SJarkko Nikula int i; 1074ead9b919SJarkko Nikula 1075ff819b83SDimitris Papastamos if (card->codec_conf == NULL) 1076ead9b919SJarkko Nikula return; 1077ead9b919SJarkko Nikula 1078ff819b83SDimitris Papastamos for (i = 0; i < card->num_configs; i++) { 1079ff819b83SDimitris Papastamos struct snd_soc_codec_conf *map = &card->codec_conf[i]; 108094f99c87SLars-Peter Clausen if (map->of_node && component->dev->of_node != map->of_node) 10813ca041edSSebastian Reichel continue; 108294f99c87SLars-Peter Clausen if (map->dev_name && strcmp(component->name, map->dev_name)) 10833ca041edSSebastian Reichel continue; 108494f99c87SLars-Peter Clausen component->name_prefix = map->name_prefix; 1085ead9b919SJarkko Nikula break; 1086ead9b919SJarkko Nikula } 1087ead9b919SJarkko Nikula } 1088ead9b919SJarkko Nikula 1089f1d45cc3SLars-Peter Clausen static int soc_probe_component(struct snd_soc_card *card, 1090f1d45cc3SLars-Peter Clausen struct snd_soc_component *component) 1091589c3563SJarkko Nikula { 1092f1d45cc3SLars-Peter Clausen struct snd_soc_dapm_context *dapm = snd_soc_component_get_dapm(component); 1093888df395SMark Brown struct snd_soc_dai *dai; 1094f1d45cc3SLars-Peter Clausen int ret; 1095589c3563SJarkko Nikula 109670090bbbSLars-Peter Clausen if (component->probed) 109770090bbbSLars-Peter Clausen return 0; 1098589c3563SJarkko Nikula 1099f1d45cc3SLars-Peter Clausen component->card = card; 1100f1d45cc3SLars-Peter Clausen dapm->card = card; 1101f1d45cc3SLars-Peter Clausen soc_set_name_prefix(card, component); 1102589c3563SJarkko Nikula 1103f1d45cc3SLars-Peter Clausen if (!try_module_get(component->dev->driver->owner)) 110470d29331SJarkko Nikula return -ENODEV; 110570d29331SJarkko Nikula 1106f1d45cc3SLars-Peter Clausen soc_init_component_debugfs(component); 1107d5d1e0beSLars-Peter Clausen 1108f1d45cc3SLars-Peter Clausen if (component->dapm_widgets) { 1109f1d45cc3SLars-Peter Clausen ret = snd_soc_dapm_new_controls(dapm, component->dapm_widgets, 1110f1d45cc3SLars-Peter Clausen component->num_dapm_widgets); 111177530150SLars-Peter Clausen 1112b318ad50SNariman Poushin if (ret != 0) { 1113f1d45cc3SLars-Peter Clausen dev_err(component->dev, 1114b318ad50SNariman Poushin "Failed to create new controls %d\n", ret); 1115b318ad50SNariman Poushin goto err_probe; 1116b318ad50SNariman Poushin } 1117b318ad50SNariman Poushin } 1118b318ad50SNariman Poushin 11190634814fSLars-Peter Clausen list_for_each_entry(dai, &component->dai_list, list) { 11200634814fSLars-Peter Clausen ret = snd_soc_dapm_new_dai_widgets(dapm, dai); 1121261edc70SNariman Poushin if (ret != 0) { 1122f1d45cc3SLars-Peter Clausen dev_err(component->dev, 1123261edc70SNariman Poushin "Failed to create DAI widgets %d\n", ret); 1124261edc70SNariman Poushin goto err_probe; 1125261edc70SNariman Poushin } 1126261edc70SNariman Poushin } 1127888df395SMark Brown 1128f1d45cc3SLars-Peter Clausen if (component->probe) { 1129f1d45cc3SLars-Peter Clausen ret = component->probe(component); 1130589c3563SJarkko Nikula if (ret < 0) { 1131f1d45cc3SLars-Peter Clausen dev_err(component->dev, 1132f1d45cc3SLars-Peter Clausen "ASoC: failed to probe component %d\n", ret); 113370d29331SJarkko Nikula goto err_probe; 1134589c3563SJarkko Nikula } 1135cb2cf612SLiam Girdwood 1136f1d45cc3SLars-Peter Clausen WARN(dapm->idle_bias_off && 1137f1d45cc3SLars-Peter Clausen dapm->bias_level != SND_SOC_BIAS_OFF, 1138956245e9SLiam Girdwood "codec %s can not start from non-off bias with idle_bias_off==1\n", 1139f1d45cc3SLars-Peter Clausen component->name); 1140956245e9SLiam Girdwood } 1141956245e9SLiam Girdwood 1142f1d45cc3SLars-Peter Clausen if (component->controls) 1143f1d45cc3SLars-Peter Clausen snd_soc_add_component_controls(component, component->controls, 1144f1d45cc3SLars-Peter Clausen component->num_controls); 1145f1d45cc3SLars-Peter Clausen if (component->dapm_routes) 1146f1d45cc3SLars-Peter Clausen snd_soc_dapm_add_routes(dapm, component->dapm_routes, 1147f1d45cc3SLars-Peter Clausen component->num_dapm_routes); 1148956245e9SLiam Girdwood 1149f1d45cc3SLars-Peter Clausen component->probed = 1; 1150f1d45cc3SLars-Peter Clausen list_add(&dapm->list, &card->dapm_list); 1151f1d45cc3SLars-Peter Clausen 1152f1d45cc3SLars-Peter Clausen /* This is a HACK and will be removed soon */ 1153f1d45cc3SLars-Peter Clausen if (component->codec) 1154f1d45cc3SLars-Peter Clausen list_add(&component->codec->card_list, &card->codec_dev_list); 1155956245e9SLiam Girdwood 1156956245e9SLiam Girdwood return 0; 1157956245e9SLiam Girdwood 1158956245e9SLiam Girdwood err_probe: 1159f1d45cc3SLars-Peter Clausen soc_cleanup_component_debugfs(component); 1160f1d45cc3SLars-Peter Clausen module_put(component->dev->driver->owner); 1161956245e9SLiam Girdwood 1162956245e9SLiam Girdwood return ret; 1163956245e9SLiam Girdwood } 1164956245e9SLiam Girdwood 116536ae1a96SMark Brown static void rtd_release(struct device *dev) 116636ae1a96SMark Brown { 116736ae1a96SMark Brown kfree(dev); 116836ae1a96SMark Brown } 1169f0fba2adSLiam Girdwood 11705f3484acSLars-Peter Clausen static int soc_post_component_init(struct snd_soc_pcm_runtime *rtd, 11715f3484acSLars-Peter Clausen const char *name) 1172503ae5e0SMisael Lopez Cruz { 1173589c3563SJarkko Nikula int ret = 0; 1174589c3563SJarkko Nikula 1175589c3563SJarkko Nikula /* register the rtd device */ 117636ae1a96SMark Brown rtd->dev = kzalloc(sizeof(struct device), GFP_KERNEL); 117736ae1a96SMark Brown if (!rtd->dev) 117836ae1a96SMark Brown return -ENOMEM; 117936ae1a96SMark Brown device_initialize(rtd->dev); 11805f3484acSLars-Peter Clausen rtd->dev->parent = rtd->card->dev; 118136ae1a96SMark Brown rtd->dev->release = rtd_release; 1182f294afedSLars-Peter Clausen dev_set_name(rtd->dev, "%s", name); 118336ae1a96SMark Brown dev_set_drvdata(rtd->dev, rtd); 1184b8c0dab9SLiam Girdwood mutex_init(&rtd->pcm_mutex); 118501d7584cSLiam Girdwood INIT_LIST_HEAD(&rtd->dpcm[SNDRV_PCM_STREAM_PLAYBACK].be_clients); 118601d7584cSLiam Girdwood INIT_LIST_HEAD(&rtd->dpcm[SNDRV_PCM_STREAM_CAPTURE].be_clients); 118701d7584cSLiam Girdwood INIT_LIST_HEAD(&rtd->dpcm[SNDRV_PCM_STREAM_PLAYBACK].fe_clients); 118801d7584cSLiam Girdwood INIT_LIST_HEAD(&rtd->dpcm[SNDRV_PCM_STREAM_CAPTURE].fe_clients); 118936ae1a96SMark Brown ret = device_add(rtd->dev); 1190589c3563SJarkko Nikula if (ret < 0) { 1191865df9cbSChuansheng Liu /* calling put_device() here to free the rtd->dev */ 1192865df9cbSChuansheng Liu put_device(rtd->dev); 11935f3484acSLars-Peter Clausen dev_err(rtd->card->dev, 1194f110bfc7SLiam Girdwood "ASoC: failed to register runtime device: %d\n", ret); 1195589c3563SJarkko Nikula return ret; 1196589c3563SJarkko Nikula } 1197589c3563SJarkko Nikula rtd->dev_registered = 1; 1198589c3563SJarkko Nikula 119993c3ce76SLars-Peter Clausen if (rtd->codec) { 1200589c3563SJarkko Nikula /* add DAPM sysfs entries for this codec */ 120136ae1a96SMark Brown ret = snd_soc_dapm_sys_add(rtd->dev); 1202589c3563SJarkko Nikula if (ret < 0) 120344c69bb1SLars-Peter Clausen dev_err(rtd->dev, 120493c3ce76SLars-Peter Clausen "ASoC: failed to add codec dapm sysfs entries: %d\n", 120593c3ce76SLars-Peter Clausen ret); 1206589c3563SJarkko Nikula 1207589c3563SJarkko Nikula /* add codec sysfs entries */ 120836ae1a96SMark Brown ret = device_create_file(rtd->dev, &dev_attr_codec_reg); 1209589c3563SJarkko Nikula if (ret < 0) 121044c69bb1SLars-Peter Clausen dev_err(rtd->dev, 121193c3ce76SLars-Peter Clausen "ASoC: failed to add codec sysfs files: %d\n", 121293c3ce76SLars-Peter Clausen ret); 121393c3ce76SLars-Peter Clausen } 1214589c3563SJarkko Nikula 1215589c3563SJarkko Nikula return 0; 1216589c3563SJarkko Nikula } 1217589c3563SJarkko Nikula 121862ae68faSStephen Warren static int soc_probe_link_components(struct snd_soc_card *card, int num, 121962ae68faSStephen Warren int order) 122062ae68faSStephen Warren { 122162ae68faSStephen Warren struct snd_soc_pcm_runtime *rtd = &card->rtd[num]; 122262ae68faSStephen Warren struct snd_soc_platform *platform = rtd->platform; 1223f1d45cc3SLars-Peter Clausen struct snd_soc_component *component; 122488bd870fSBenoit Cousson int i, ret; 122562ae68faSStephen Warren 122662ae68faSStephen Warren /* probe the CPU-side component, if it is a CODEC */ 122761aca564SLars-Peter Clausen component = rtd->cpu_dai->component; 122870090bbbSLars-Peter Clausen if (component->driver->probe_order == order) { 1229f1d45cc3SLars-Peter Clausen ret = soc_probe_component(card, component); 123062ae68faSStephen Warren if (ret < 0) 123162ae68faSStephen Warren return ret; 123262ae68faSStephen Warren } 123362ae68faSStephen Warren 123488bd870fSBenoit Cousson /* probe the CODEC-side components */ 123588bd870fSBenoit Cousson for (i = 0; i < rtd->num_codecs; i++) { 123661aca564SLars-Peter Clausen component = rtd->codec_dais[i]->component; 123770090bbbSLars-Peter Clausen if (component->driver->probe_order == order) { 1238f1d45cc3SLars-Peter Clausen ret = soc_probe_component(card, component); 123962ae68faSStephen Warren if (ret < 0) 124062ae68faSStephen Warren return ret; 124162ae68faSStephen Warren } 124288bd870fSBenoit Cousson } 124362ae68faSStephen Warren 124462ae68faSStephen Warren /* probe the platform */ 124570090bbbSLars-Peter Clausen if (platform->component.driver->probe_order == order) { 1246f1d45cc3SLars-Peter Clausen ret = soc_probe_component(card, &platform->component); 124762ae68faSStephen Warren if (ret < 0) 124862ae68faSStephen Warren return ret; 124962ae68faSStephen Warren } 125062ae68faSStephen Warren 125162ae68faSStephen Warren return 0; 125262ae68faSStephen Warren } 125362ae68faSStephen Warren 1254b0aa88afSMisael Lopez Cruz static int soc_probe_codec_dai(struct snd_soc_card *card, 1255b0aa88afSMisael Lopez Cruz struct snd_soc_dai *codec_dai, 1256b0aa88afSMisael Lopez Cruz int order) 1257b0aa88afSMisael Lopez Cruz { 1258b0aa88afSMisael Lopez Cruz int ret; 1259b0aa88afSMisael Lopez Cruz 1260b0aa88afSMisael Lopez Cruz if (!codec_dai->probed && codec_dai->driver->probe_order == order) { 1261b0aa88afSMisael Lopez Cruz if (codec_dai->driver->probe) { 1262b0aa88afSMisael Lopez Cruz ret = codec_dai->driver->probe(codec_dai); 1263b0aa88afSMisael Lopez Cruz if (ret < 0) { 1264b0aa88afSMisael Lopez Cruz dev_err(codec_dai->dev, 1265b0aa88afSMisael Lopez Cruz "ASoC: failed to probe CODEC DAI %s: %d\n", 1266b0aa88afSMisael Lopez Cruz codec_dai->name, ret); 1267b0aa88afSMisael Lopez Cruz return ret; 1268b0aa88afSMisael Lopez Cruz } 1269b0aa88afSMisael Lopez Cruz } 1270b0aa88afSMisael Lopez Cruz 1271b0aa88afSMisael Lopez Cruz /* mark codec_dai as probed and add to card dai list */ 1272b0aa88afSMisael Lopez Cruz codec_dai->probed = 1; 1273b0aa88afSMisael Lopez Cruz } 1274b0aa88afSMisael Lopez Cruz 1275b0aa88afSMisael Lopez Cruz return 0; 1276b0aa88afSMisael Lopez Cruz } 1277b0aa88afSMisael Lopez Cruz 12782436a723SMisael Lopez Cruz static int soc_link_dai_widgets(struct snd_soc_card *card, 12792436a723SMisael Lopez Cruz struct snd_soc_dai_link *dai_link, 12803f901a02SBenoit Cousson struct snd_soc_pcm_runtime *rtd) 12812436a723SMisael Lopez Cruz { 12823f901a02SBenoit Cousson struct snd_soc_dai *cpu_dai = rtd->cpu_dai; 12833f901a02SBenoit Cousson struct snd_soc_dai *codec_dai = rtd->codec_dai; 12842436a723SMisael Lopez Cruz struct snd_soc_dapm_widget *play_w, *capture_w; 12852436a723SMisael Lopez Cruz int ret; 12862436a723SMisael Lopez Cruz 128788bd870fSBenoit Cousson if (rtd->num_codecs > 1) 128888bd870fSBenoit Cousson dev_warn(card->dev, "ASoC: Multiple codecs not supported yet\n"); 128988bd870fSBenoit Cousson 12902436a723SMisael Lopez Cruz /* link the DAI widgets */ 12912436a723SMisael Lopez Cruz play_w = codec_dai->playback_widget; 12922436a723SMisael Lopez Cruz capture_w = cpu_dai->capture_widget; 12932436a723SMisael Lopez Cruz if (play_w && capture_w) { 12942436a723SMisael Lopez Cruz ret = snd_soc_dapm_new_pcm(card, dai_link->params, 12952436a723SMisael Lopez Cruz capture_w, play_w); 12962436a723SMisael Lopez Cruz if (ret != 0) { 12972436a723SMisael Lopez Cruz dev_err(card->dev, "ASoC: Can't link %s to %s: %d\n", 12982436a723SMisael Lopez Cruz play_w->name, capture_w->name, ret); 12992436a723SMisael Lopez Cruz return ret; 13002436a723SMisael Lopez Cruz } 13012436a723SMisael Lopez Cruz } 13022436a723SMisael Lopez Cruz 13032436a723SMisael Lopez Cruz play_w = cpu_dai->playback_widget; 13042436a723SMisael Lopez Cruz capture_w = codec_dai->capture_widget; 13052436a723SMisael Lopez Cruz if (play_w && capture_w) { 13062436a723SMisael Lopez Cruz ret = snd_soc_dapm_new_pcm(card, dai_link->params, 13072436a723SMisael Lopez Cruz capture_w, play_w); 13082436a723SMisael Lopez Cruz if (ret != 0) { 13092436a723SMisael Lopez Cruz dev_err(card->dev, "ASoC: Can't link %s to %s: %d\n", 13102436a723SMisael Lopez Cruz play_w->name, capture_w->name, ret); 13112436a723SMisael Lopez Cruz return ret; 13122436a723SMisael Lopez Cruz } 13132436a723SMisael Lopez Cruz } 13142436a723SMisael Lopez Cruz 13152436a723SMisael Lopez Cruz return 0; 13162436a723SMisael Lopez Cruz } 13172436a723SMisael Lopez Cruz 131862ae68faSStephen Warren static int soc_probe_link_dais(struct snd_soc_card *card, int num, int order) 1319f0fba2adSLiam Girdwood { 1320f0fba2adSLiam Girdwood struct snd_soc_dai_link *dai_link = &card->dai_link[num]; 1321f0fba2adSLiam Girdwood struct snd_soc_pcm_runtime *rtd = &card->rtd[num]; 1322f0fba2adSLiam Girdwood struct snd_soc_platform *platform = rtd->platform; 1323c74184edSMark Brown struct snd_soc_dai *cpu_dai = rtd->cpu_dai; 132488bd870fSBenoit Cousson int i, ret; 1325f0fba2adSLiam Girdwood 1326f110bfc7SLiam Girdwood dev_dbg(card->dev, "ASoC: probe %s dai link %d late %d\n", 13270168bf0dSLiam Girdwood card->name, num, order); 1328f0fba2adSLiam Girdwood 1329f0fba2adSLiam Girdwood /* config components */ 1330f0fba2adSLiam Girdwood cpu_dai->platform = platform; 1331f0fba2adSLiam Girdwood cpu_dai->card = card; 133288bd870fSBenoit Cousson for (i = 0; i < rtd->num_codecs; i++) 133388bd870fSBenoit Cousson rtd->codec_dais[i]->card = card; 1334f0fba2adSLiam Girdwood 1335f0fba2adSLiam Girdwood /* set default power off timeout */ 1336f0fba2adSLiam Girdwood rtd->pmdown_time = pmdown_time; 1337f0fba2adSLiam Girdwood 1338f0fba2adSLiam Girdwood /* probe the cpu_dai */ 13390168bf0dSLiam Girdwood if (!cpu_dai->probed && 13400168bf0dSLiam Girdwood cpu_dai->driver->probe_order == order) { 1341f0fba2adSLiam Girdwood if (cpu_dai->driver->probe) { 1342f0fba2adSLiam Girdwood ret = cpu_dai->driver->probe(cpu_dai); 1343f0fba2adSLiam Girdwood if (ret < 0) { 1344f110bfc7SLiam Girdwood dev_err(cpu_dai->dev, 1345f110bfc7SLiam Girdwood "ASoC: failed to probe CPU DAI %s: %d\n", 13460837fc62SFabio Estevam cpu_dai->name, ret); 1347f0fba2adSLiam Girdwood return ret; 1348f0fba2adSLiam Girdwood } 1349f0fba2adSLiam Girdwood } 1350f0fba2adSLiam Girdwood cpu_dai->probed = 1; 1351f0fba2adSLiam Girdwood } 1352f0fba2adSLiam Girdwood 1353f0fba2adSLiam Girdwood /* probe the CODEC DAI */ 135488bd870fSBenoit Cousson for (i = 0; i < rtd->num_codecs; i++) { 135588bd870fSBenoit Cousson ret = soc_probe_codec_dai(card, rtd->codec_dais[i], order); 1356b0aa88afSMisael Lopez Cruz if (ret) 1357f0fba2adSLiam Girdwood return ret; 135888bd870fSBenoit Cousson } 1359f0fba2adSLiam Girdwood 13600168bf0dSLiam Girdwood /* complete DAI probe during last probe */ 13610168bf0dSLiam Girdwood if (order != SND_SOC_COMP_ORDER_LAST) 13620168bf0dSLiam Girdwood return 0; 13630168bf0dSLiam Girdwood 13645f3484acSLars-Peter Clausen /* do machine specific initialization */ 13655f3484acSLars-Peter Clausen if (dai_link->init) { 13665f3484acSLars-Peter Clausen ret = dai_link->init(rtd); 13675f3484acSLars-Peter Clausen if (ret < 0) { 13685f3484acSLars-Peter Clausen dev_err(card->dev, "ASoC: failed to init %s: %d\n", 13695f3484acSLars-Peter Clausen dai_link->name, ret); 13705f3484acSLars-Peter Clausen return ret; 13715f3484acSLars-Peter Clausen } 13725f3484acSLars-Peter Clausen } 13735f3484acSLars-Peter Clausen 13745f3484acSLars-Peter Clausen ret = soc_post_component_init(rtd, dai_link->name); 1375589c3563SJarkko Nikula if (ret) 1376f0fba2adSLiam Girdwood return ret; 1377f0fba2adSLiam Girdwood 13785f3484acSLars-Peter Clausen #ifdef CONFIG_DEBUG_FS 13795f3484acSLars-Peter Clausen /* add DPCM sysfs entries */ 13805f3484acSLars-Peter Clausen if (dai_link->dynamic) { 13815f3484acSLars-Peter Clausen ret = soc_dpcm_debugfs_add(rtd); 13825f3484acSLars-Peter Clausen if (ret < 0) { 13835f3484acSLars-Peter Clausen dev_err(rtd->dev, 13845f3484acSLars-Peter Clausen "ASoC: failed to add dpcm sysfs entries: %d\n", 13855f3484acSLars-Peter Clausen ret); 13865f3484acSLars-Peter Clausen return ret; 13875f3484acSLars-Peter Clausen } 13885f3484acSLars-Peter Clausen } 13895f3484acSLars-Peter Clausen #endif 13905f3484acSLars-Peter Clausen 139136ae1a96SMark Brown ret = device_create_file(rtd->dev, &dev_attr_pmdown_time); 1392f0fba2adSLiam Girdwood if (ret < 0) 1393f110bfc7SLiam Girdwood dev_warn(rtd->dev, "ASoC: failed to add pmdown_time sysfs: %d\n", 1394f110bfc7SLiam Girdwood ret); 1395f0fba2adSLiam Girdwood 13961245b700SNamarta Kohli if (cpu_dai->driver->compress_dai) { 13971245b700SNamarta Kohli /*create compress_device"*/ 13981245b700SNamarta Kohli ret = soc_new_compress(rtd, num); 13991245b700SNamarta Kohli if (ret < 0) { 1400f110bfc7SLiam Girdwood dev_err(card->dev, "ASoC: can't create compress %s\n", 14011245b700SNamarta Kohli dai_link->stream_name); 14021245b700SNamarta Kohli return ret; 14031245b700SNamarta Kohli } 14041245b700SNamarta Kohli } else { 14051245b700SNamarta Kohli 1406c74184edSMark Brown if (!dai_link->params) { 1407f0fba2adSLiam Girdwood /* create the pcm */ 1408f0fba2adSLiam Girdwood ret = soc_new_pcm(rtd, num); 1409f0fba2adSLiam Girdwood if (ret < 0) { 1410f110bfc7SLiam Girdwood dev_err(card->dev, "ASoC: can't create pcm %s :%d\n", 14110837fc62SFabio Estevam dai_link->stream_name, ret); 1412f0fba2adSLiam Girdwood return ret; 1413f0fba2adSLiam Girdwood } 1414c74184edSMark Brown } else { 14159d58a077SRichard Fitzgerald INIT_DELAYED_WORK(&rtd->delayed_work, 14169d58a077SRichard Fitzgerald codec2codec_close_delayed_work); 14179d58a077SRichard Fitzgerald 1418c74184edSMark Brown /* link the DAI widgets */ 14193f901a02SBenoit Cousson ret = soc_link_dai_widgets(card, dai_link, rtd); 14202436a723SMisael Lopez Cruz if (ret) 1421c74184edSMark Brown return ret; 1422c74184edSMark Brown } 1423c74184edSMark Brown } 1424c74184edSMark Brown 1425f0fba2adSLiam Girdwood /* add platform data for AC97 devices */ 142688bd870fSBenoit Cousson for (i = 0; i < rtd->num_codecs; i++) { 142788bd870fSBenoit Cousson if (rtd->codec_dais[i]->driver->ac97_control) 142888bd870fSBenoit Cousson snd_ac97_dev_add_pdata(rtd->codec_dais[i]->codec->ac97, 142988bd870fSBenoit Cousson rtd->cpu_dai->ac97_pdata); 143088bd870fSBenoit Cousson } 1431f0fba2adSLiam Girdwood 1432f0fba2adSLiam Girdwood return 0; 1433f0fba2adSLiam Girdwood } 1434f0fba2adSLiam Girdwood 1435f0fba2adSLiam Girdwood #ifdef CONFIG_SND_SOC_AC97_BUS 143602c9c7b9SMisael Lopez Cruz static int soc_register_ac97_codec(struct snd_soc_codec *codec, 143702c9c7b9SMisael Lopez Cruz struct snd_soc_dai *codec_dai) 1438f0fba2adSLiam Girdwood { 1439f0fba2adSLiam Girdwood int ret; 1440f0fba2adSLiam Girdwood 1441f0fba2adSLiam Girdwood /* Only instantiate AC97 if not already done by the adaptor 1442f0fba2adSLiam Girdwood * for the generic AC97 subsystem. 14436b05eda6SMark Brown */ 144402c9c7b9SMisael Lopez Cruz if (codec_dai->driver->ac97_control && !codec->ac97_registered) { 14450562f788SMika Westerberg /* 14460562f788SMika Westerberg * It is possible that the AC97 device is already registered to 14470562f788SMika Westerberg * the device subsystem. This happens when the device is created 14480562f788SMika Westerberg * via snd_ac97_mixer(). Currently only SoC codec that does so 14490562f788SMika Westerberg * is the generic AC97 glue but others migh emerge. 14500562f788SMika Westerberg * 14510562f788SMika Westerberg * In those cases we don't try to register the device again. 14520562f788SMika Westerberg */ 145302c9c7b9SMisael Lopez Cruz if (!codec->ac97_created) 14540562f788SMika Westerberg return 0; 1455f0fba2adSLiam Girdwood 145602c9c7b9SMisael Lopez Cruz ret = soc_ac97_dev_register(codec); 1457f0fba2adSLiam Girdwood if (ret < 0) { 145802c9c7b9SMisael Lopez Cruz dev_err(codec->dev, 1459f110bfc7SLiam Girdwood "ASoC: AC97 device register failed: %d\n", ret); 1460f0fba2adSLiam Girdwood return ret; 1461435c5e25SMark Brown } 1462435c5e25SMark Brown 146302c9c7b9SMisael Lopez Cruz codec->ac97_registered = 1; 1464f0fba2adSLiam Girdwood } 1465f0fba2adSLiam Girdwood return 0; 1466f0fba2adSLiam Girdwood } 1467435c5e25SMark Brown 146802c9c7b9SMisael Lopez Cruz static void soc_unregister_ac97_codec(struct snd_soc_codec *codec) 1469f0fba2adSLiam Girdwood { 1470f0fba2adSLiam Girdwood if (codec->ac97_registered) { 1471f0fba2adSLiam Girdwood soc_ac97_dev_unregister(codec); 1472f0fba2adSLiam Girdwood codec->ac97_registered = 0; 1473f0fba2adSLiam Girdwood } 1474f0fba2adSLiam Girdwood } 147502c9c7b9SMisael Lopez Cruz 147688bd870fSBenoit Cousson static int soc_register_ac97_dai_link(struct snd_soc_pcm_runtime *rtd) 147788bd870fSBenoit Cousson { 147888bd870fSBenoit Cousson int i, ret; 147988bd870fSBenoit Cousson 148088bd870fSBenoit Cousson for (i = 0; i < rtd->num_codecs; i++) { 148188bd870fSBenoit Cousson struct snd_soc_dai *codec_dai = rtd->codec_dais[i]; 148288bd870fSBenoit Cousson 148388bd870fSBenoit Cousson ret = soc_register_ac97_codec(codec_dai->codec, codec_dai); 148488bd870fSBenoit Cousson if (ret) { 148588bd870fSBenoit Cousson while (--i >= 0) 148688bd870fSBenoit Cousson soc_unregister_ac97_codec(codec_dai->codec); 148788bd870fSBenoit Cousson return ret; 148888bd870fSBenoit Cousson } 148988bd870fSBenoit Cousson } 149088bd870fSBenoit Cousson 149188bd870fSBenoit Cousson return 0; 149288bd870fSBenoit Cousson } 149388bd870fSBenoit Cousson 149402c9c7b9SMisael Lopez Cruz static void soc_unregister_ac97_dai_link(struct snd_soc_pcm_runtime *rtd) 149502c9c7b9SMisael Lopez Cruz { 149688bd870fSBenoit Cousson int i; 149788bd870fSBenoit Cousson 149888bd870fSBenoit Cousson for (i = 0; i < rtd->num_codecs; i++) 149988bd870fSBenoit Cousson soc_unregister_ac97_codec(rtd->codec_dais[i]->codec); 150002c9c7b9SMisael Lopez Cruz } 1501f0fba2adSLiam Girdwood #endif 1502435c5e25SMark Brown 150344c69bb1SLars-Peter Clausen static int soc_bind_aux_dev(struct snd_soc_card *card, int num) 1504b19e6e7bSMark Brown { 150544c69bb1SLars-Peter Clausen struct snd_soc_pcm_runtime *rtd = &card->rtd_aux[num]; 15063ca041edSSebastian Reichel struct snd_soc_aux_dev *aux_dev = &card->aux_dev[num]; 150765d9361fSLars-Peter Clausen const char *name = aux_dev->codec_name; 15083ca041edSSebastian Reichel 150965d9361fSLars-Peter Clausen rtd->component = soc_find_component(aux_dev->codec_of_node, name); 151065d9361fSLars-Peter Clausen if (!rtd->component) { 15113ca041edSSebastian Reichel if (aux_dev->codec_of_node) 151265d9361fSLars-Peter Clausen name = of_node_full_name(aux_dev->codec_of_node); 15133ca041edSSebastian Reichel 151465d9361fSLars-Peter Clausen dev_err(card->dev, "ASoC: %s not registered\n", name); 1515b19e6e7bSMark Brown return -EPROBE_DEFER; 1516b19e6e7bSMark Brown } 1517b19e6e7bSMark Brown 151865d9361fSLars-Peter Clausen /* 151965d9361fSLars-Peter Clausen * Some places still reference rtd->codec, so we have to keep that 152065d9361fSLars-Peter Clausen * initialized if the component is a CODEC. Once all those references 152165d9361fSLars-Peter Clausen * have been removed, this code can be removed as well. 152265d9361fSLars-Peter Clausen */ 152365d9361fSLars-Peter Clausen rtd->codec = rtd->component->codec; 152465d9361fSLars-Peter Clausen 152544c69bb1SLars-Peter Clausen return 0; 15263ca041edSSebastian Reichel } 15272eea392dSJarkko Nikula 152844c69bb1SLars-Peter Clausen static int soc_probe_aux_dev(struct snd_soc_card *card, int num) 152944c69bb1SLars-Peter Clausen { 153044c69bb1SLars-Peter Clausen struct snd_soc_pcm_runtime *rtd = &card->rtd_aux[num]; 15315f3484acSLars-Peter Clausen struct snd_soc_aux_dev *aux_dev = &card->aux_dev[num]; 153244c69bb1SLars-Peter Clausen int ret; 153344c69bb1SLars-Peter Clausen 153465d9361fSLars-Peter Clausen ret = soc_probe_component(card, rtd->component); 15352eea392dSJarkko Nikula if (ret < 0) 1536589c3563SJarkko Nikula return ret; 15372eea392dSJarkko Nikula 15385f3484acSLars-Peter Clausen /* do machine specific initialization */ 15395f3484acSLars-Peter Clausen if (aux_dev->init) { 154057bf7726SLars-Peter Clausen ret = aux_dev->init(rtd->component); 15415f3484acSLars-Peter Clausen if (ret < 0) { 15425f3484acSLars-Peter Clausen dev_err(card->dev, "ASoC: failed to init %s: %d\n", 15435f3484acSLars-Peter Clausen aux_dev->name, ret); 15442eea392dSJarkko Nikula return ret; 15452eea392dSJarkko Nikula } 15465f3484acSLars-Peter Clausen } 15475f3484acSLars-Peter Clausen 15485f3484acSLars-Peter Clausen return soc_post_component_init(rtd, aux_dev->name); 15492eea392dSJarkko Nikula } 15502eea392dSJarkko Nikula 15512eea392dSJarkko Nikula static void soc_remove_aux_dev(struct snd_soc_card *card, int num) 15522eea392dSJarkko Nikula { 15532eea392dSJarkko Nikula struct snd_soc_pcm_runtime *rtd = &card->rtd_aux[num]; 155465d9361fSLars-Peter Clausen struct snd_soc_component *component = rtd->component; 15552eea392dSJarkko Nikula 15562eea392dSJarkko Nikula /* unregister the rtd device */ 15572eea392dSJarkko Nikula if (rtd->dev_registered) { 155836ae1a96SMark Brown device_remove_file(rtd->dev, &dev_attr_codec_reg); 1559d3bf1561SChuansheng Liu device_unregister(rtd->dev); 15602eea392dSJarkko Nikula rtd->dev_registered = 0; 15612eea392dSJarkko Nikula } 15622eea392dSJarkko Nikula 156365d9361fSLars-Peter Clausen if (component && component->probed) 156465d9361fSLars-Peter Clausen soc_remove_component(component); 15652eea392dSJarkko Nikula } 15662eea392dSJarkko Nikula 1567f90fb3f7SLars-Peter Clausen static int snd_soc_init_codec_cache(struct snd_soc_codec *codec) 1568fdf0f54dSDimitris Papastamos { 1569fdf0f54dSDimitris Papastamos int ret; 1570fdf0f54dSDimitris Papastamos 1571fdf0f54dSDimitris Papastamos if (codec->cache_init) 1572fdf0f54dSDimitris Papastamos return 0; 1573fdf0f54dSDimitris Papastamos 1574fdf0f54dSDimitris Papastamos ret = snd_soc_cache_init(codec); 1575fdf0f54dSDimitris Papastamos if (ret < 0) { 157610e8aa9aSMichał Mirosław dev_err(codec->dev, 157710e8aa9aSMichał Mirosław "ASoC: Failed to set cache compression type: %d\n", 157810e8aa9aSMichał Mirosław ret); 1579fdf0f54dSDimitris Papastamos return ret; 1580fdf0f54dSDimitris Papastamos } 1581fdf0f54dSDimitris Papastamos codec->cache_init = 1; 1582fdf0f54dSDimitris Papastamos return 0; 1583fdf0f54dSDimitris Papastamos } 1584fdf0f54dSDimitris Papastamos 1585b19e6e7bSMark Brown static int snd_soc_instantiate_card(struct snd_soc_card *card) 1586f0fba2adSLiam Girdwood { 1587fdf0f54dSDimitris Papastamos struct snd_soc_codec *codec; 158875d9ac46SMark Brown struct snd_soc_dai_link *dai_link; 1589f04209a7SMark Brown int ret, i, order, dai_fmt; 159096dd3622SMark Brown 159101b9d99aSLiam Girdwood mutex_lock_nested(&card->mutex, SND_SOC_CARD_CLASS_INIT); 1592f0fba2adSLiam Girdwood 1593b19e6e7bSMark Brown /* bind DAIs */ 1594b19e6e7bSMark Brown for (i = 0; i < card->num_links; i++) { 1595b19e6e7bSMark Brown ret = soc_bind_dai_link(card, i); 1596b19e6e7bSMark Brown if (ret != 0) 1597b19e6e7bSMark Brown goto base_error; 1598db2a4165SFrank Mandarino } 1599db2a4165SFrank Mandarino 160044c69bb1SLars-Peter Clausen /* bind aux_devs too */ 1601b19e6e7bSMark Brown for (i = 0; i < card->num_aux_devs; i++) { 160244c69bb1SLars-Peter Clausen ret = soc_bind_aux_dev(card, i); 1603b19e6e7bSMark Brown if (ret != 0) 1604b19e6e7bSMark Brown goto base_error; 1605db2a4165SFrank Mandarino } 1606db2a4165SFrank Mandarino 1607fdf0f54dSDimitris Papastamos /* initialize the register cache for each available codec */ 1608fdf0f54dSDimitris Papastamos list_for_each_entry(codec, &codec_list, list) { 1609fdf0f54dSDimitris Papastamos if (codec->cache_init) 1610fdf0f54dSDimitris Papastamos continue; 1611f90fb3f7SLars-Peter Clausen ret = snd_soc_init_codec_cache(codec); 1612b19e6e7bSMark Brown if (ret < 0) 1613b19e6e7bSMark Brown goto base_error; 1614fdf0f54dSDimitris Papastamos } 1615fdf0f54dSDimitris Papastamos 1616f0fba2adSLiam Girdwood /* card bind complete so register a sound card */ 1617102b5a8dSTakashi Iwai ret = snd_card_new(card->dev, SNDRV_DEFAULT_IDX1, SNDRV_DEFAULT_STR1, 1618f0fba2adSLiam Girdwood card->owner, 0, &card->snd_card); 1619f0fba2adSLiam Girdwood if (ret < 0) { 162010e8aa9aSMichał Mirosław dev_err(card->dev, 162110e8aa9aSMichał Mirosław "ASoC: can't create sound card for card %s: %d\n", 162210e8aa9aSMichał Mirosław card->name, ret); 1623b19e6e7bSMark Brown goto base_error; 1624db2a4165SFrank Mandarino } 1625db2a4165SFrank Mandarino 1626e37a4970SMark Brown card->dapm.bias_level = SND_SOC_BIAS_OFF; 1627e37a4970SMark Brown card->dapm.dev = card->dev; 1628e37a4970SMark Brown card->dapm.card = card; 1629e37a4970SMark Brown list_add(&card->dapm.list, &card->dapm_list); 1630e37a4970SMark Brown 1631d5d1e0beSLars-Peter Clausen #ifdef CONFIG_DEBUG_FS 1632d5d1e0beSLars-Peter Clausen snd_soc_dapm_debugfs_init(&card->dapm, card->debugfs_card_root); 1633d5d1e0beSLars-Peter Clausen #endif 1634d5d1e0beSLars-Peter Clausen 163588ee1c61SMark Brown #ifdef CONFIG_PM_SLEEP 16366ed25978SAndy Green /* deferred resume work */ 16376308419aSMark Brown INIT_WORK(&card->deferred_resume_work, soc_resume_deferred); 16381301a964SRandy Dunlap #endif 16396ed25978SAndy Green 16409a841ebbSMark Brown if (card->dapm_widgets) 16419a841ebbSMark Brown snd_soc_dapm_new_controls(&card->dapm, card->dapm_widgets, 16429a841ebbSMark Brown card->num_dapm_widgets); 16439a841ebbSMark Brown 1644f0fba2adSLiam Girdwood /* initialise the sound card only once */ 1645f0fba2adSLiam Girdwood if (card->probe) { 1646e7361ec4SMark Brown ret = card->probe(card); 1647f0fba2adSLiam Girdwood if (ret < 0) 1648f0fba2adSLiam Girdwood goto card_probe_error; 1649f0fba2adSLiam Girdwood } 1650f0fba2adSLiam Girdwood 165162ae68faSStephen Warren /* probe all components used by DAI links on this card */ 16520168bf0dSLiam Girdwood for (order = SND_SOC_COMP_ORDER_FIRST; order <= SND_SOC_COMP_ORDER_LAST; 16530168bf0dSLiam Girdwood order++) { 1654fe3e78e0SMark Brown for (i = 0; i < card->num_links; i++) { 165562ae68faSStephen Warren ret = soc_probe_link_components(card, i, order); 165662ae68faSStephen Warren if (ret < 0) { 1657f110bfc7SLiam Girdwood dev_err(card->dev, 1658f110bfc7SLiam Girdwood "ASoC: failed to instantiate card %d\n", 1659f110bfc7SLiam Girdwood ret); 166062ae68faSStephen Warren goto probe_dai_err; 166162ae68faSStephen Warren } 166262ae68faSStephen Warren } 166362ae68faSStephen Warren } 166462ae68faSStephen Warren 166562ae68faSStephen Warren /* probe all DAI links on this card */ 166662ae68faSStephen Warren for (order = SND_SOC_COMP_ORDER_FIRST; order <= SND_SOC_COMP_ORDER_LAST; 166762ae68faSStephen Warren order++) { 166862ae68faSStephen Warren for (i = 0; i < card->num_links; i++) { 166962ae68faSStephen Warren ret = soc_probe_link_dais(card, i, order); 1670fe3e78e0SMark Brown if (ret < 0) { 1671f110bfc7SLiam Girdwood dev_err(card->dev, 1672f110bfc7SLiam Girdwood "ASoC: failed to instantiate card %d\n", 1673f110bfc7SLiam Girdwood ret); 1674f0fba2adSLiam Girdwood goto probe_dai_err; 1675fe3e78e0SMark Brown } 1676fe3e78e0SMark Brown } 16770168bf0dSLiam Girdwood } 1678fe3e78e0SMark Brown 16792eea392dSJarkko Nikula for (i = 0; i < card->num_aux_devs; i++) { 16802eea392dSJarkko Nikula ret = soc_probe_aux_dev(card, i); 16812eea392dSJarkko Nikula if (ret < 0) { 1682f110bfc7SLiam Girdwood dev_err(card->dev, 1683f110bfc7SLiam Girdwood "ASoC: failed to add auxiliary devices %d\n", 1684f110bfc7SLiam Girdwood ret); 16852eea392dSJarkko Nikula goto probe_aux_dev_err; 16862eea392dSJarkko Nikula } 16872eea392dSJarkko Nikula } 16882eea392dSJarkko Nikula 1689888df395SMark Brown snd_soc_dapm_link_dai_widgets(card); 1690b893ea5fSLiam Girdwood snd_soc_dapm_connect_dai_link_widgets(card); 1691888df395SMark Brown 1692b7af1dafSMark Brown if (card->controls) 1693022658beSLiam Girdwood snd_soc_add_card_controls(card, card->controls, card->num_controls); 1694b7af1dafSMark Brown 1695b8ad29deSMark Brown if (card->dapm_routes) 1696b8ad29deSMark Brown snd_soc_dapm_add_routes(&card->dapm, card->dapm_routes, 1697b8ad29deSMark Brown card->num_dapm_routes); 1698b8ad29deSMark Brown 169975d9ac46SMark Brown for (i = 0; i < card->num_links; i++) { 170088bd870fSBenoit Cousson struct snd_soc_pcm_runtime *rtd = &card->rtd[i]; 170175d9ac46SMark Brown dai_link = &card->dai_link[i]; 1702f04209a7SMark Brown dai_fmt = dai_link->dai_fmt; 170375d9ac46SMark Brown 1704f04209a7SMark Brown if (dai_fmt) { 170588bd870fSBenoit Cousson struct snd_soc_dai **codec_dais = rtd->codec_dais; 170688bd870fSBenoit Cousson int j; 170788bd870fSBenoit Cousson 170888bd870fSBenoit Cousson for (j = 0; j < rtd->num_codecs; j++) { 170988bd870fSBenoit Cousson struct snd_soc_dai *codec_dai = codec_dais[j]; 171088bd870fSBenoit Cousson 171188bd870fSBenoit Cousson ret = snd_soc_dai_set_fmt(codec_dai, dai_fmt); 17125e4ba569SShawn Guo if (ret != 0 && ret != -ENOTSUPP) 171388bd870fSBenoit Cousson dev_warn(codec_dai->dev, 1714f110bfc7SLiam Girdwood "ASoC: Failed to set DAI format: %d\n", 171575d9ac46SMark Brown ret); 1716f04209a7SMark Brown } 171788bd870fSBenoit Cousson } 1718f04209a7SMark Brown 1719f04209a7SMark Brown /* If this is a regular CPU link there will be a platform */ 1720fe33d4c5SStephen Warren if (dai_fmt && 1721fe33d4c5SStephen Warren (dai_link->platform_name || dai_link->platform_of_node)) { 1722f04209a7SMark Brown ret = snd_soc_dai_set_fmt(card->rtd[i].cpu_dai, 1723f04209a7SMark Brown dai_fmt); 1724f04209a7SMark Brown if (ret != 0 && ret != -ENOTSUPP) 1725f04209a7SMark Brown dev_warn(card->rtd[i].cpu_dai->dev, 1726f110bfc7SLiam Girdwood "ASoC: Failed to set DAI format: %d\n", 1727f04209a7SMark Brown ret); 1728f04209a7SMark Brown } else if (dai_fmt) { 1729f04209a7SMark Brown /* Flip the polarity for the "CPU" end */ 1730f04209a7SMark Brown dai_fmt &= ~SND_SOC_DAIFMT_MASTER_MASK; 1731f04209a7SMark Brown switch (dai_link->dai_fmt & 1732f04209a7SMark Brown SND_SOC_DAIFMT_MASTER_MASK) { 1733f04209a7SMark Brown case SND_SOC_DAIFMT_CBM_CFM: 1734f04209a7SMark Brown dai_fmt |= SND_SOC_DAIFMT_CBS_CFS; 1735f04209a7SMark Brown break; 1736f04209a7SMark Brown case SND_SOC_DAIFMT_CBM_CFS: 1737f04209a7SMark Brown dai_fmt |= SND_SOC_DAIFMT_CBS_CFM; 1738f04209a7SMark Brown break; 1739f04209a7SMark Brown case SND_SOC_DAIFMT_CBS_CFM: 1740f04209a7SMark Brown dai_fmt |= SND_SOC_DAIFMT_CBM_CFS; 1741f04209a7SMark Brown break; 1742f04209a7SMark Brown case SND_SOC_DAIFMT_CBS_CFS: 1743f04209a7SMark Brown dai_fmt |= SND_SOC_DAIFMT_CBM_CFM; 1744f04209a7SMark Brown break; 1745f04209a7SMark Brown } 174675d9ac46SMark Brown 174775d9ac46SMark Brown ret = snd_soc_dai_set_fmt(card->rtd[i].cpu_dai, 1748f04209a7SMark Brown dai_fmt); 17495e4ba569SShawn Guo if (ret != 0 && ret != -ENOTSUPP) 175075d9ac46SMark Brown dev_warn(card->rtd[i].cpu_dai->dev, 1751f110bfc7SLiam Girdwood "ASoC: Failed to set DAI format: %d\n", 175275d9ac46SMark Brown ret); 175375d9ac46SMark Brown } 175475d9ac46SMark Brown } 175575d9ac46SMark Brown 1756f0fba2adSLiam Girdwood snprintf(card->snd_card->shortname, sizeof(card->snd_card->shortname), 1757fe3e78e0SMark Brown "%s", card->name); 1758f0fba2adSLiam Girdwood snprintf(card->snd_card->longname, sizeof(card->snd_card->longname), 175922de71baSLiam Girdwood "%s", card->long_name ? card->long_name : card->name); 1760f0e8ed85SMark Brown snprintf(card->snd_card->driver, sizeof(card->snd_card->driver), 1761f0e8ed85SMark Brown "%s", card->driver_name ? card->driver_name : card->name); 1762f0e8ed85SMark Brown for (i = 0; i < ARRAY_SIZE(card->snd_card->driver); i++) { 1763f0e8ed85SMark Brown switch (card->snd_card->driver[i]) { 1764f0e8ed85SMark Brown case '_': 1765f0e8ed85SMark Brown case '-': 1766f0e8ed85SMark Brown case '\0': 1767f0e8ed85SMark Brown break; 1768f0e8ed85SMark Brown default: 1769f0e8ed85SMark Brown if (!isalnum(card->snd_card->driver[i])) 1770f0e8ed85SMark Brown card->snd_card->driver[i] = '_'; 1771f0e8ed85SMark Brown break; 1772f0e8ed85SMark Brown } 1773f0e8ed85SMark Brown } 1774fe3e78e0SMark Brown 177528e9ad92SMark Brown if (card->late_probe) { 177628e9ad92SMark Brown ret = card->late_probe(card); 177728e9ad92SMark Brown if (ret < 0) { 1778f110bfc7SLiam Girdwood dev_err(card->dev, "ASoC: %s late_probe() failed: %d\n", 177928e9ad92SMark Brown card->name, ret); 178028e9ad92SMark Brown goto probe_aux_dev_err; 178128e9ad92SMark Brown } 178228e9ad92SMark Brown } 178328e9ad92SMark Brown 17841633281bSStephen Warren if (card->fully_routed) 17857df37884SLars-Peter Clausen snd_soc_dapm_auto_nc_pins(card); 17861633281bSStephen Warren 1787824ef826SLars-Peter Clausen snd_soc_dapm_new_widgets(card); 17888c193b8dSLars-Peter Clausen 1789f0fba2adSLiam Girdwood ret = snd_card_register(card->snd_card); 1790fe3e78e0SMark Brown if (ret < 0) { 1791f110bfc7SLiam Girdwood dev_err(card->dev, "ASoC: failed to register soundcard %d\n", 1792f110bfc7SLiam Girdwood ret); 17936b3ed785SAxel Lin goto probe_aux_dev_err; 1794fe3e78e0SMark Brown } 1795fe3e78e0SMark Brown 1796fe3e78e0SMark Brown #ifdef CONFIG_SND_SOC_AC97_BUS 1797f0fba2adSLiam Girdwood /* register any AC97 codecs */ 1798f0fba2adSLiam Girdwood for (i = 0; i < card->num_rtd; i++) { 1799f0fba2adSLiam Girdwood ret = soc_register_ac97_dai_link(&card->rtd[i]); 1800fe3e78e0SMark Brown if (ret < 0) { 180110e8aa9aSMichał Mirosław dev_err(card->dev, 180210e8aa9aSMichał Mirosław "ASoC: failed to register AC97: %d\n", ret); 18036b3ed785SAxel Lin while (--i >= 0) 180402c9c7b9SMisael Lopez Cruz soc_unregister_ac97_dai_link(&card->rtd[i]); 18056b3ed785SAxel Lin goto probe_aux_dev_err; 1806fe3e78e0SMark Brown } 1807fe3e78e0SMark Brown } 1808fe3e78e0SMark Brown #endif 1809fe3e78e0SMark Brown 1810435c5e25SMark Brown card->instantiated = 1; 18114f4c0072SMark Brown snd_soc_dapm_sync(&card->dapm); 1812f0fba2adSLiam Girdwood mutex_unlock(&card->mutex); 1813b19e6e7bSMark Brown 1814b19e6e7bSMark Brown return 0; 1815db2a4165SFrank Mandarino 18162eea392dSJarkko Nikula probe_aux_dev_err: 18172eea392dSJarkko Nikula for (i = 0; i < card->num_aux_devs; i++) 18182eea392dSJarkko Nikula soc_remove_aux_dev(card, i); 18192eea392dSJarkko Nikula 1820f0fba2adSLiam Girdwood probe_dai_err: 18210671fd8eSKuninori Morimoto soc_remove_dai_links(card); 1822fe3e78e0SMark Brown 1823f0fba2adSLiam Girdwood card_probe_error: 182487506549SMark Brown if (card->remove) 1825e7361ec4SMark Brown card->remove(card); 1826f0fba2adSLiam Girdwood 1827f0fba2adSLiam Girdwood snd_card_free(card->snd_card); 1828f0fba2adSLiam Girdwood 1829b19e6e7bSMark Brown base_error: 1830f0fba2adSLiam Girdwood mutex_unlock(&card->mutex); 1831db2a4165SFrank Mandarino 1832b19e6e7bSMark Brown return ret; 1833435c5e25SMark Brown } 1834435c5e25SMark Brown 1835435c5e25SMark Brown /* probes a new socdev */ 1836435c5e25SMark Brown static int soc_probe(struct platform_device *pdev) 1837435c5e25SMark Brown { 1838f0fba2adSLiam Girdwood struct snd_soc_card *card = platform_get_drvdata(pdev); 1839435c5e25SMark Brown 184070a7ca34SVinod Koul /* 184170a7ca34SVinod Koul * no card, so machine driver should be registering card 184270a7ca34SVinod Koul * we should not be here in that case so ret error 184370a7ca34SVinod Koul */ 184470a7ca34SVinod Koul if (!card) 184570a7ca34SVinod Koul return -EINVAL; 184670a7ca34SVinod Koul 1847fe4085e8SMark Brown dev_warn(&pdev->dev, 1848f110bfc7SLiam Girdwood "ASoC: machine %s should use snd_soc_register_card()\n", 1849fe4085e8SMark Brown card->name); 1850fe4085e8SMark Brown 1851435c5e25SMark Brown /* Bodge while we unpick instantiation */ 1852435c5e25SMark Brown card->dev = &pdev->dev; 1853f0fba2adSLiam Girdwood 185428d528c8SMark Brown return snd_soc_register_card(card); 1855435c5e25SMark Brown } 1856435c5e25SMark Brown 1857b0e26485SVinod Koul static int soc_cleanup_card_resources(struct snd_soc_card *card) 1858db2a4165SFrank Mandarino { 1859db2a4165SFrank Mandarino int i; 1860db2a4165SFrank Mandarino 1861f0fba2adSLiam Girdwood /* make sure any delayed work runs */ 1862f0fba2adSLiam Girdwood for (i = 0; i < card->num_rtd; i++) { 1863f0fba2adSLiam Girdwood struct snd_soc_pcm_runtime *rtd = &card->rtd[i]; 186443829731STejun Heo flush_delayed_work(&rtd->delayed_work); 1865db2a4165SFrank Mandarino } 1866db2a4165SFrank Mandarino 18672eea392dSJarkko Nikula /* remove auxiliary devices */ 18682eea392dSJarkko Nikula for (i = 0; i < card->num_aux_devs; i++) 18692eea392dSJarkko Nikula soc_remove_aux_dev(card, i); 18702eea392dSJarkko Nikula 1871f0fba2adSLiam Girdwood /* remove and free each DAI */ 18720671fd8eSKuninori Morimoto soc_remove_dai_links(card); 1873f0fba2adSLiam Girdwood 1874a6052154SJarkko Nikula soc_cleanup_card_debugfs(card); 1875a6052154SJarkko Nikula 1876f0fba2adSLiam Girdwood /* remove the card */ 187787506549SMark Brown if (card->remove) 1878e7361ec4SMark Brown card->remove(card); 1879f0fba2adSLiam Girdwood 18800aaae527SLars-Peter Clausen snd_soc_dapm_free(&card->dapm); 18810aaae527SLars-Peter Clausen 1882f0fba2adSLiam Girdwood snd_card_free(card->snd_card); 1883b0e26485SVinod Koul return 0; 1884b0e26485SVinod Koul 1885b2dfa62cSGuennadi Liakhovetski } 1886b0e26485SVinod Koul 1887b0e26485SVinod Koul /* removes a socdev */ 1888b0e26485SVinod Koul static int soc_remove(struct platform_device *pdev) 1889b0e26485SVinod Koul { 1890b0e26485SVinod Koul struct snd_soc_card *card = platform_get_drvdata(pdev); 1891b0e26485SVinod Koul 1892c5af3a2eSMark Brown snd_soc_unregister_card(card); 1893db2a4165SFrank Mandarino return 0; 1894db2a4165SFrank Mandarino } 1895db2a4165SFrank Mandarino 18966f8ab4acSMark Brown int snd_soc_poweroff(struct device *dev) 189751737470SMark Brown { 18986f8ab4acSMark Brown struct snd_soc_card *card = dev_get_drvdata(dev); 1899f0fba2adSLiam Girdwood int i; 190051737470SMark Brown 190151737470SMark Brown if (!card->instantiated) 1902416356fcSMark Brown return 0; 190351737470SMark Brown 190451737470SMark Brown /* Flush out pmdown_time work - we actually do want to run it 190551737470SMark Brown * now, we're shutting down so no imminent restart. */ 1906f0fba2adSLiam Girdwood for (i = 0; i < card->num_rtd; i++) { 1907f0fba2adSLiam Girdwood struct snd_soc_pcm_runtime *rtd = &card->rtd[i]; 190843829731STejun Heo flush_delayed_work(&rtd->delayed_work); 1909f0fba2adSLiam Girdwood } 191051737470SMark Brown 1911f0fba2adSLiam Girdwood snd_soc_dapm_shutdown(card); 1912416356fcSMark Brown 1913988e8cc4SNicolin Chen /* deactivate pins to sleep state */ 1914988e8cc4SNicolin Chen for (i = 0; i < card->num_rtd; i++) { 191588bd870fSBenoit Cousson struct snd_soc_pcm_runtime *rtd = &card->rtd[i]; 191688bd870fSBenoit Cousson struct snd_soc_dai *cpu_dai = rtd->cpu_dai; 191788bd870fSBenoit Cousson int j; 191888bd870fSBenoit Cousson 1919988e8cc4SNicolin Chen pinctrl_pm_select_sleep_state(cpu_dai->dev); 192088bd870fSBenoit Cousson for (j = 0; j < rtd->num_codecs; j++) { 192188bd870fSBenoit Cousson struct snd_soc_dai *codec_dai = rtd->codec_dais[j]; 192288bd870fSBenoit Cousson pinctrl_pm_select_sleep_state(codec_dai->dev); 192388bd870fSBenoit Cousson } 1924988e8cc4SNicolin Chen } 1925988e8cc4SNicolin Chen 1926416356fcSMark Brown return 0; 192751737470SMark Brown } 19286f8ab4acSMark Brown EXPORT_SYMBOL_GPL(snd_soc_poweroff); 192951737470SMark Brown 19306f8ab4acSMark Brown const struct dev_pm_ops snd_soc_pm_ops = { 1931b1dd5897SViresh Kumar .suspend = snd_soc_suspend, 1932b1dd5897SViresh Kumar .resume = snd_soc_resume, 1933b1dd5897SViresh Kumar .freeze = snd_soc_suspend, 1934b1dd5897SViresh Kumar .thaw = snd_soc_resume, 19356f8ab4acSMark Brown .poweroff = snd_soc_poweroff, 1936b1dd5897SViresh Kumar .restore = snd_soc_resume, 1937416356fcSMark Brown }; 1938deb2607eSStephen Warren EXPORT_SYMBOL_GPL(snd_soc_pm_ops); 1939416356fcSMark Brown 1940db2a4165SFrank Mandarino /* ASoC platform driver */ 1941db2a4165SFrank Mandarino static struct platform_driver soc_driver = { 1942db2a4165SFrank Mandarino .driver = { 1943db2a4165SFrank Mandarino .name = "soc-audio", 19448b45a209SKay Sievers .owner = THIS_MODULE, 19456f8ab4acSMark Brown .pm = &snd_soc_pm_ops, 1946db2a4165SFrank Mandarino }, 1947db2a4165SFrank Mandarino .probe = soc_probe, 1948db2a4165SFrank Mandarino .remove = soc_remove, 1949db2a4165SFrank Mandarino }; 1950db2a4165SFrank Mandarino 1951096e49d5SMark Brown /** 1952db2a4165SFrank Mandarino * snd_soc_new_ac97_codec - initailise AC97 device 1953db2a4165SFrank Mandarino * @codec: audio codec 1954db2a4165SFrank Mandarino * @ops: AC97 bus operations 1955db2a4165SFrank Mandarino * @num: AC97 codec number 1956db2a4165SFrank Mandarino * 1957db2a4165SFrank Mandarino * Initialises AC97 codec resources for use by ad-hoc devices only. 1958db2a4165SFrank Mandarino */ 1959db2a4165SFrank Mandarino int snd_soc_new_ac97_codec(struct snd_soc_codec *codec, 1960db2a4165SFrank Mandarino struct snd_ac97_bus_ops *ops, int num) 1961db2a4165SFrank Mandarino { 1962db2a4165SFrank Mandarino codec->ac97 = kzalloc(sizeof(struct snd_ac97), GFP_KERNEL); 1963e3f205a7SLars-Peter Clausen if (codec->ac97 == NULL) 1964db2a4165SFrank Mandarino return -ENOMEM; 1965db2a4165SFrank Mandarino 1966db2a4165SFrank Mandarino codec->ac97->bus = kzalloc(sizeof(struct snd_ac97_bus), GFP_KERNEL); 1967db2a4165SFrank Mandarino if (codec->ac97->bus == NULL) { 1968db2a4165SFrank Mandarino kfree(codec->ac97); 1969db2a4165SFrank Mandarino codec->ac97 = NULL; 1970db2a4165SFrank Mandarino return -ENOMEM; 1971db2a4165SFrank Mandarino } 1972db2a4165SFrank Mandarino 1973db2a4165SFrank Mandarino codec->ac97->bus->ops = ops; 1974db2a4165SFrank Mandarino codec->ac97->num = num; 19750562f788SMika Westerberg 19760562f788SMika Westerberg /* 19770562f788SMika Westerberg * Mark the AC97 device to be created by us. This way we ensure that the 19780562f788SMika Westerberg * device will be registered with the device subsystem later on. 19790562f788SMika Westerberg */ 19800562f788SMika Westerberg codec->ac97_created = 1; 19810562f788SMika Westerberg 1982db2a4165SFrank Mandarino return 0; 1983db2a4165SFrank Mandarino } 1984db2a4165SFrank Mandarino EXPORT_SYMBOL_GPL(snd_soc_new_ac97_codec); 1985db2a4165SFrank Mandarino 1986741a509fSMarkus Pargmann static struct snd_ac97_reset_cfg snd_ac97_rst_cfg; 1987741a509fSMarkus Pargmann 1988741a509fSMarkus Pargmann static void snd_soc_ac97_warm_reset(struct snd_ac97 *ac97) 1989741a509fSMarkus Pargmann { 1990741a509fSMarkus Pargmann struct pinctrl *pctl = snd_ac97_rst_cfg.pctl; 1991741a509fSMarkus Pargmann 1992741a509fSMarkus Pargmann pinctrl_select_state(pctl, snd_ac97_rst_cfg.pstate_warm_reset); 1993741a509fSMarkus Pargmann 1994741a509fSMarkus Pargmann gpio_direction_output(snd_ac97_rst_cfg.gpio_sync, 1); 1995741a509fSMarkus Pargmann 1996741a509fSMarkus Pargmann udelay(10); 1997741a509fSMarkus Pargmann 1998741a509fSMarkus Pargmann gpio_direction_output(snd_ac97_rst_cfg.gpio_sync, 0); 1999741a509fSMarkus Pargmann 2000741a509fSMarkus Pargmann pinctrl_select_state(pctl, snd_ac97_rst_cfg.pstate_run); 2001741a509fSMarkus Pargmann msleep(2); 2002741a509fSMarkus Pargmann } 2003741a509fSMarkus Pargmann 2004741a509fSMarkus Pargmann static void snd_soc_ac97_reset(struct snd_ac97 *ac97) 2005741a509fSMarkus Pargmann { 2006741a509fSMarkus Pargmann struct pinctrl *pctl = snd_ac97_rst_cfg.pctl; 2007741a509fSMarkus Pargmann 2008741a509fSMarkus Pargmann pinctrl_select_state(pctl, snd_ac97_rst_cfg.pstate_reset); 2009741a509fSMarkus Pargmann 2010741a509fSMarkus Pargmann gpio_direction_output(snd_ac97_rst_cfg.gpio_sync, 0); 2011741a509fSMarkus Pargmann gpio_direction_output(snd_ac97_rst_cfg.gpio_sdata, 0); 2012741a509fSMarkus Pargmann gpio_direction_output(snd_ac97_rst_cfg.gpio_reset, 0); 2013741a509fSMarkus Pargmann 2014741a509fSMarkus Pargmann udelay(10); 2015741a509fSMarkus Pargmann 2016741a509fSMarkus Pargmann gpio_direction_output(snd_ac97_rst_cfg.gpio_reset, 1); 2017741a509fSMarkus Pargmann 2018741a509fSMarkus Pargmann pinctrl_select_state(pctl, snd_ac97_rst_cfg.pstate_run); 2019741a509fSMarkus Pargmann msleep(2); 2020741a509fSMarkus Pargmann } 2021741a509fSMarkus Pargmann 2022741a509fSMarkus Pargmann static int snd_soc_ac97_parse_pinctl(struct device *dev, 2023741a509fSMarkus Pargmann struct snd_ac97_reset_cfg *cfg) 2024741a509fSMarkus Pargmann { 2025741a509fSMarkus Pargmann struct pinctrl *p; 2026741a509fSMarkus Pargmann struct pinctrl_state *state; 2027741a509fSMarkus Pargmann int gpio; 2028741a509fSMarkus Pargmann int ret; 2029741a509fSMarkus Pargmann 2030741a509fSMarkus Pargmann p = devm_pinctrl_get(dev); 2031741a509fSMarkus Pargmann if (IS_ERR(p)) { 2032741a509fSMarkus Pargmann dev_err(dev, "Failed to get pinctrl\n"); 2033b7580cdeSChristoph Jaeger return PTR_ERR(p); 2034741a509fSMarkus Pargmann } 2035741a509fSMarkus Pargmann cfg->pctl = p; 2036741a509fSMarkus Pargmann 2037741a509fSMarkus Pargmann state = pinctrl_lookup_state(p, "ac97-reset"); 2038741a509fSMarkus Pargmann if (IS_ERR(state)) { 2039741a509fSMarkus Pargmann dev_err(dev, "Can't find pinctrl state ac97-reset\n"); 2040b7580cdeSChristoph Jaeger return PTR_ERR(state); 2041741a509fSMarkus Pargmann } 2042741a509fSMarkus Pargmann cfg->pstate_reset = state; 2043741a509fSMarkus Pargmann 2044741a509fSMarkus Pargmann state = pinctrl_lookup_state(p, "ac97-warm-reset"); 2045741a509fSMarkus Pargmann if (IS_ERR(state)) { 2046741a509fSMarkus Pargmann dev_err(dev, "Can't find pinctrl state ac97-warm-reset\n"); 2047b7580cdeSChristoph Jaeger return PTR_ERR(state); 2048741a509fSMarkus Pargmann } 2049741a509fSMarkus Pargmann cfg->pstate_warm_reset = state; 2050741a509fSMarkus Pargmann 2051741a509fSMarkus Pargmann state = pinctrl_lookup_state(p, "ac97-running"); 2052741a509fSMarkus Pargmann if (IS_ERR(state)) { 2053741a509fSMarkus Pargmann dev_err(dev, "Can't find pinctrl state ac97-running\n"); 2054b7580cdeSChristoph Jaeger return PTR_ERR(state); 2055741a509fSMarkus Pargmann } 2056741a509fSMarkus Pargmann cfg->pstate_run = state; 2057741a509fSMarkus Pargmann 2058741a509fSMarkus Pargmann gpio = of_get_named_gpio(dev->of_node, "ac97-gpios", 0); 2059741a509fSMarkus Pargmann if (gpio < 0) { 2060741a509fSMarkus Pargmann dev_err(dev, "Can't find ac97-sync gpio\n"); 2061741a509fSMarkus Pargmann return gpio; 2062741a509fSMarkus Pargmann } 2063741a509fSMarkus Pargmann ret = devm_gpio_request(dev, gpio, "AC97 link sync"); 2064741a509fSMarkus Pargmann if (ret) { 2065741a509fSMarkus Pargmann dev_err(dev, "Failed requesting ac97-sync gpio\n"); 2066741a509fSMarkus Pargmann return ret; 2067741a509fSMarkus Pargmann } 2068741a509fSMarkus Pargmann cfg->gpio_sync = gpio; 2069741a509fSMarkus Pargmann 2070741a509fSMarkus Pargmann gpio = of_get_named_gpio(dev->of_node, "ac97-gpios", 1); 2071741a509fSMarkus Pargmann if (gpio < 0) { 2072741a509fSMarkus Pargmann dev_err(dev, "Can't find ac97-sdata gpio %d\n", gpio); 2073741a509fSMarkus Pargmann return gpio; 2074741a509fSMarkus Pargmann } 2075741a509fSMarkus Pargmann ret = devm_gpio_request(dev, gpio, "AC97 link sdata"); 2076741a509fSMarkus Pargmann if (ret) { 2077741a509fSMarkus Pargmann dev_err(dev, "Failed requesting ac97-sdata gpio\n"); 2078741a509fSMarkus Pargmann return ret; 2079741a509fSMarkus Pargmann } 2080741a509fSMarkus Pargmann cfg->gpio_sdata = gpio; 2081741a509fSMarkus Pargmann 2082741a509fSMarkus Pargmann gpio = of_get_named_gpio(dev->of_node, "ac97-gpios", 2); 2083741a509fSMarkus Pargmann if (gpio < 0) { 2084741a509fSMarkus Pargmann dev_err(dev, "Can't find ac97-reset gpio\n"); 2085741a509fSMarkus Pargmann return gpio; 2086741a509fSMarkus Pargmann } 2087741a509fSMarkus Pargmann ret = devm_gpio_request(dev, gpio, "AC97 link reset"); 2088741a509fSMarkus Pargmann if (ret) { 2089741a509fSMarkus Pargmann dev_err(dev, "Failed requesting ac97-reset gpio\n"); 2090741a509fSMarkus Pargmann return ret; 2091741a509fSMarkus Pargmann } 2092741a509fSMarkus Pargmann cfg->gpio_reset = gpio; 2093741a509fSMarkus Pargmann 2094741a509fSMarkus Pargmann return 0; 2095741a509fSMarkus Pargmann } 2096741a509fSMarkus Pargmann 2097b047e1ccSMark Brown struct snd_ac97_bus_ops *soc_ac97_ops; 2098f74b5e25SKevin Hilman EXPORT_SYMBOL_GPL(soc_ac97_ops); 2099b047e1ccSMark Brown 2100b047e1ccSMark Brown int snd_soc_set_ac97_ops(struct snd_ac97_bus_ops *ops) 2101b047e1ccSMark Brown { 2102b047e1ccSMark Brown if (ops == soc_ac97_ops) 2103b047e1ccSMark Brown return 0; 2104b047e1ccSMark Brown 2105b047e1ccSMark Brown if (soc_ac97_ops && ops) 2106b047e1ccSMark Brown return -EBUSY; 2107b047e1ccSMark Brown 2108b047e1ccSMark Brown soc_ac97_ops = ops; 2109b047e1ccSMark Brown 2110b047e1ccSMark Brown return 0; 2111b047e1ccSMark Brown } 2112b047e1ccSMark Brown EXPORT_SYMBOL_GPL(snd_soc_set_ac97_ops); 2113b047e1ccSMark Brown 2114db2a4165SFrank Mandarino /** 2115741a509fSMarkus Pargmann * snd_soc_set_ac97_ops_of_reset - Set ac97 ops with generic ac97 reset functions 2116741a509fSMarkus Pargmann * 2117741a509fSMarkus Pargmann * This function sets the reset and warm_reset properties of ops and parses 2118741a509fSMarkus Pargmann * the device node of pdev to get pinctrl states and gpio numbers to use. 2119741a509fSMarkus Pargmann */ 2120741a509fSMarkus Pargmann int snd_soc_set_ac97_ops_of_reset(struct snd_ac97_bus_ops *ops, 2121741a509fSMarkus Pargmann struct platform_device *pdev) 2122741a509fSMarkus Pargmann { 2123741a509fSMarkus Pargmann struct device *dev = &pdev->dev; 2124741a509fSMarkus Pargmann struct snd_ac97_reset_cfg cfg; 2125741a509fSMarkus Pargmann int ret; 2126741a509fSMarkus Pargmann 2127741a509fSMarkus Pargmann ret = snd_soc_ac97_parse_pinctl(dev, &cfg); 2128741a509fSMarkus Pargmann if (ret) 2129741a509fSMarkus Pargmann return ret; 2130741a509fSMarkus Pargmann 2131741a509fSMarkus Pargmann ret = snd_soc_set_ac97_ops(ops); 2132741a509fSMarkus Pargmann if (ret) 2133741a509fSMarkus Pargmann return ret; 2134741a509fSMarkus Pargmann 2135741a509fSMarkus Pargmann ops->warm_reset = snd_soc_ac97_warm_reset; 2136741a509fSMarkus Pargmann ops->reset = snd_soc_ac97_reset; 2137741a509fSMarkus Pargmann 2138741a509fSMarkus Pargmann snd_ac97_rst_cfg = cfg; 2139741a509fSMarkus Pargmann return 0; 2140741a509fSMarkus Pargmann } 2141741a509fSMarkus Pargmann EXPORT_SYMBOL_GPL(snd_soc_set_ac97_ops_of_reset); 2142741a509fSMarkus Pargmann 2143741a509fSMarkus Pargmann /** 2144db2a4165SFrank Mandarino * snd_soc_free_ac97_codec - free AC97 codec device 2145db2a4165SFrank Mandarino * @codec: audio codec 2146db2a4165SFrank Mandarino * 2147db2a4165SFrank Mandarino * Frees AC97 codec device resources. 2148db2a4165SFrank Mandarino */ 2149db2a4165SFrank Mandarino void snd_soc_free_ac97_codec(struct snd_soc_codec *codec) 2150db2a4165SFrank Mandarino { 2151f0fba2adSLiam Girdwood #ifdef CONFIG_SND_SOC_AC97_BUS 215202c9c7b9SMisael Lopez Cruz soc_unregister_ac97_codec(codec); 2153f0fba2adSLiam Girdwood #endif 2154db2a4165SFrank Mandarino kfree(codec->ac97->bus); 2155db2a4165SFrank Mandarino kfree(codec->ac97); 2156db2a4165SFrank Mandarino codec->ac97 = NULL; 21570562f788SMika Westerberg codec->ac97_created = 0; 2158db2a4165SFrank Mandarino } 2159db2a4165SFrank Mandarino EXPORT_SYMBOL_GPL(snd_soc_free_ac97_codec); 2160db2a4165SFrank Mandarino 2161db2a4165SFrank Mandarino /** 2162db2a4165SFrank Mandarino * snd_soc_cnew - create new control 2163db2a4165SFrank Mandarino * @_template: control template 2164db2a4165SFrank Mandarino * @data: control private data 2165ac11a2b3SMark Brown * @long_name: control long name 2166efb7ac3fSMark Brown * @prefix: control name prefix 2167db2a4165SFrank Mandarino * 2168db2a4165SFrank Mandarino * Create a new mixer control from a template control. 2169db2a4165SFrank Mandarino * 2170db2a4165SFrank Mandarino * Returns 0 for success, else error. 2171db2a4165SFrank Mandarino */ 2172db2a4165SFrank Mandarino struct snd_kcontrol *snd_soc_cnew(const struct snd_kcontrol_new *_template, 21733056557fSMark Brown void *data, const char *long_name, 2174efb7ac3fSMark Brown const char *prefix) 2175db2a4165SFrank Mandarino { 2176db2a4165SFrank Mandarino struct snd_kcontrol_new template; 2177efb7ac3fSMark Brown struct snd_kcontrol *kcontrol; 2178efb7ac3fSMark Brown char *name = NULL; 2179db2a4165SFrank Mandarino 2180db2a4165SFrank Mandarino memcpy(&template, _template, sizeof(template)); 2181db2a4165SFrank Mandarino template.index = 0; 2182db2a4165SFrank Mandarino 2183efb7ac3fSMark Brown if (!long_name) 2184efb7ac3fSMark Brown long_name = template.name; 2185efb7ac3fSMark Brown 2186efb7ac3fSMark Brown if (prefix) { 21872b581074SLars-Peter Clausen name = kasprintf(GFP_KERNEL, "%s %s", prefix, long_name); 2188efb7ac3fSMark Brown if (!name) 2189efb7ac3fSMark Brown return NULL; 2190efb7ac3fSMark Brown 2191efb7ac3fSMark Brown template.name = name; 2192efb7ac3fSMark Brown } else { 2193efb7ac3fSMark Brown template.name = long_name; 2194efb7ac3fSMark Brown } 2195efb7ac3fSMark Brown 2196efb7ac3fSMark Brown kcontrol = snd_ctl_new1(&template, data); 2197efb7ac3fSMark Brown 2198efb7ac3fSMark Brown kfree(name); 2199efb7ac3fSMark Brown 2200efb7ac3fSMark Brown return kcontrol; 2201db2a4165SFrank Mandarino } 2202db2a4165SFrank Mandarino EXPORT_SYMBOL_GPL(snd_soc_cnew); 2203db2a4165SFrank Mandarino 2204022658beSLiam Girdwood static int snd_soc_add_controls(struct snd_card *card, struct device *dev, 2205022658beSLiam Girdwood const struct snd_kcontrol_new *controls, int num_controls, 2206022658beSLiam Girdwood const char *prefix, void *data) 2207022658beSLiam Girdwood { 2208022658beSLiam Girdwood int err, i; 2209022658beSLiam Girdwood 2210022658beSLiam Girdwood for (i = 0; i < num_controls; i++) { 2211022658beSLiam Girdwood const struct snd_kcontrol_new *control = &controls[i]; 2212022658beSLiam Girdwood err = snd_ctl_add(card, snd_soc_cnew(control, data, 2213022658beSLiam Girdwood control->name, prefix)); 2214022658beSLiam Girdwood if (err < 0) { 2215f110bfc7SLiam Girdwood dev_err(dev, "ASoC: Failed to add %s: %d\n", 2216f110bfc7SLiam Girdwood control->name, err); 2217022658beSLiam Girdwood return err; 2218022658beSLiam Girdwood } 2219022658beSLiam Girdwood } 2220022658beSLiam Girdwood 2221022658beSLiam Girdwood return 0; 2222022658beSLiam Girdwood } 2223022658beSLiam Girdwood 22244fefd698SDimitris Papastamos struct snd_kcontrol *snd_soc_card_get_kcontrol(struct snd_soc_card *soc_card, 22254fefd698SDimitris Papastamos const char *name) 22264fefd698SDimitris Papastamos { 22274fefd698SDimitris Papastamos struct snd_card *card = soc_card->snd_card; 22284fefd698SDimitris Papastamos struct snd_kcontrol *kctl; 22294fefd698SDimitris Papastamos 22304fefd698SDimitris Papastamos if (unlikely(!name)) 22314fefd698SDimitris Papastamos return NULL; 22324fefd698SDimitris Papastamos 22334fefd698SDimitris Papastamos list_for_each_entry(kctl, &card->controls, list) 22344fefd698SDimitris Papastamos if (!strncmp(kctl->id.name, name, sizeof(kctl->id.name))) 22354fefd698SDimitris Papastamos return kctl; 22364fefd698SDimitris Papastamos return NULL; 22374fefd698SDimitris Papastamos } 22384fefd698SDimitris Papastamos EXPORT_SYMBOL_GPL(snd_soc_card_get_kcontrol); 22394fefd698SDimitris Papastamos 2240db2a4165SFrank Mandarino /** 22410f2780adSLars-Peter Clausen * snd_soc_add_component_controls - Add an array of controls to a component. 22420f2780adSLars-Peter Clausen * 22430f2780adSLars-Peter Clausen * @component: Component to add controls to 22440f2780adSLars-Peter Clausen * @controls: Array of controls to add 22450f2780adSLars-Peter Clausen * @num_controls: Number of elements in the array 22460f2780adSLars-Peter Clausen * 22470f2780adSLars-Peter Clausen * Return: 0 for success, else error. 22480f2780adSLars-Peter Clausen */ 22490f2780adSLars-Peter Clausen int snd_soc_add_component_controls(struct snd_soc_component *component, 22500f2780adSLars-Peter Clausen const struct snd_kcontrol_new *controls, unsigned int num_controls) 22510f2780adSLars-Peter Clausen { 22520f2780adSLars-Peter Clausen struct snd_card *card = component->card->snd_card; 22530f2780adSLars-Peter Clausen 22540f2780adSLars-Peter Clausen return snd_soc_add_controls(card, component->dev, controls, 22550f2780adSLars-Peter Clausen num_controls, component->name_prefix, component); 22560f2780adSLars-Peter Clausen } 22570f2780adSLars-Peter Clausen EXPORT_SYMBOL_GPL(snd_soc_add_component_controls); 22580f2780adSLars-Peter Clausen 22590f2780adSLars-Peter Clausen /** 2260022658beSLiam Girdwood * snd_soc_add_codec_controls - add an array of controls to a codec. 2261022658beSLiam Girdwood * Convenience function to add a list of controls. Many codecs were 22623e8e1952SIan Molton * duplicating this code. 22633e8e1952SIan Molton * 22643e8e1952SIan Molton * @codec: codec to add controls to 22653e8e1952SIan Molton * @controls: array of controls to add 22663e8e1952SIan Molton * @num_controls: number of elements in the array 22673e8e1952SIan Molton * 22683e8e1952SIan Molton * Return 0 for success, else error. 22693e8e1952SIan Molton */ 2270022658beSLiam Girdwood int snd_soc_add_codec_controls(struct snd_soc_codec *codec, 22710f2780adSLars-Peter Clausen const struct snd_kcontrol_new *controls, unsigned int num_controls) 22723e8e1952SIan Molton { 22730f2780adSLars-Peter Clausen return snd_soc_add_component_controls(&codec->component, controls, 22740f2780adSLars-Peter Clausen num_controls); 22753e8e1952SIan Molton } 2276022658beSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_add_codec_controls); 22773e8e1952SIan Molton 22783e8e1952SIan Molton /** 2279a491a5c8SLiam Girdwood * snd_soc_add_platform_controls - add an array of controls to a platform. 2280022658beSLiam Girdwood * Convenience function to add a list of controls. 2281a491a5c8SLiam Girdwood * 2282a491a5c8SLiam Girdwood * @platform: platform to add controls to 2283a491a5c8SLiam Girdwood * @controls: array of controls to add 2284a491a5c8SLiam Girdwood * @num_controls: number of elements in the array 2285a491a5c8SLiam Girdwood * 2286a491a5c8SLiam Girdwood * Return 0 for success, else error. 2287a491a5c8SLiam Girdwood */ 2288a491a5c8SLiam Girdwood int snd_soc_add_platform_controls(struct snd_soc_platform *platform, 22890f2780adSLars-Peter Clausen const struct snd_kcontrol_new *controls, unsigned int num_controls) 2290a491a5c8SLiam Girdwood { 22910f2780adSLars-Peter Clausen return snd_soc_add_component_controls(&platform->component, controls, 22920f2780adSLars-Peter Clausen num_controls); 2293a491a5c8SLiam Girdwood } 2294a491a5c8SLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_add_platform_controls); 2295a491a5c8SLiam Girdwood 2296a491a5c8SLiam Girdwood /** 2297022658beSLiam Girdwood * snd_soc_add_card_controls - add an array of controls to a SoC card. 2298022658beSLiam Girdwood * Convenience function to add a list of controls. 2299022658beSLiam Girdwood * 2300022658beSLiam Girdwood * @soc_card: SoC card to add controls to 2301022658beSLiam Girdwood * @controls: array of controls to add 2302022658beSLiam Girdwood * @num_controls: number of elements in the array 2303022658beSLiam Girdwood * 2304022658beSLiam Girdwood * Return 0 for success, else error. 2305022658beSLiam Girdwood */ 2306022658beSLiam Girdwood int snd_soc_add_card_controls(struct snd_soc_card *soc_card, 2307022658beSLiam Girdwood const struct snd_kcontrol_new *controls, int num_controls) 2308022658beSLiam Girdwood { 2309022658beSLiam Girdwood struct snd_card *card = soc_card->snd_card; 2310022658beSLiam Girdwood 2311022658beSLiam Girdwood return snd_soc_add_controls(card, soc_card->dev, controls, num_controls, 2312022658beSLiam Girdwood NULL, soc_card); 2313022658beSLiam Girdwood } 2314022658beSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_add_card_controls); 2315022658beSLiam Girdwood 2316022658beSLiam Girdwood /** 2317022658beSLiam Girdwood * snd_soc_add_dai_controls - add an array of controls to a DAI. 2318022658beSLiam Girdwood * Convienience function to add a list of controls. 2319022658beSLiam Girdwood * 2320022658beSLiam Girdwood * @dai: DAI to add controls to 2321022658beSLiam Girdwood * @controls: array of controls to add 2322022658beSLiam Girdwood * @num_controls: number of elements in the array 2323022658beSLiam Girdwood * 2324022658beSLiam Girdwood * Return 0 for success, else error. 2325022658beSLiam Girdwood */ 2326022658beSLiam Girdwood int snd_soc_add_dai_controls(struct snd_soc_dai *dai, 2327022658beSLiam Girdwood const struct snd_kcontrol_new *controls, int num_controls) 2328022658beSLiam Girdwood { 2329022658beSLiam Girdwood struct snd_card *card = dai->card->snd_card; 2330022658beSLiam Girdwood 2331022658beSLiam Girdwood return snd_soc_add_controls(card, dai->dev, controls, num_controls, 2332022658beSLiam Girdwood NULL, dai); 2333022658beSLiam Girdwood } 2334022658beSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_add_dai_controls); 2335022658beSLiam Girdwood 2336022658beSLiam Girdwood /** 2337db2a4165SFrank Mandarino * snd_soc_info_enum_double - enumerated double mixer info callback 2338db2a4165SFrank Mandarino * @kcontrol: mixer control 2339db2a4165SFrank Mandarino * @uinfo: control element information 2340db2a4165SFrank Mandarino * 2341db2a4165SFrank Mandarino * Callback to provide information about a double enumerated 2342db2a4165SFrank Mandarino * mixer control. 2343db2a4165SFrank Mandarino * 2344db2a4165SFrank Mandarino * Returns 0 for success. 2345db2a4165SFrank Mandarino */ 2346db2a4165SFrank Mandarino int snd_soc_info_enum_double(struct snd_kcontrol *kcontrol, 2347db2a4165SFrank Mandarino struct snd_ctl_elem_info *uinfo) 2348db2a4165SFrank Mandarino { 2349db2a4165SFrank Mandarino struct soc_enum *e = (struct soc_enum *)kcontrol->private_value; 2350db2a4165SFrank Mandarino 2351db2a4165SFrank Mandarino uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED; 2352db2a4165SFrank Mandarino uinfo->count = e->shift_l == e->shift_r ? 1 : 2; 23539a8d38dbSTakashi Iwai uinfo->value.enumerated.items = e->items; 2354db2a4165SFrank Mandarino 23559a8d38dbSTakashi Iwai if (uinfo->value.enumerated.item >= e->items) 23569a8d38dbSTakashi Iwai uinfo->value.enumerated.item = e->items - 1; 2357a19685cbSTakashi Iwai strlcpy(uinfo->value.enumerated.name, 2358a19685cbSTakashi Iwai e->texts[uinfo->value.enumerated.item], 2359a19685cbSTakashi Iwai sizeof(uinfo->value.enumerated.name)); 2360db2a4165SFrank Mandarino return 0; 2361db2a4165SFrank Mandarino } 2362db2a4165SFrank Mandarino EXPORT_SYMBOL_GPL(snd_soc_info_enum_double); 2363db2a4165SFrank Mandarino 2364db2a4165SFrank Mandarino /** 2365db2a4165SFrank Mandarino * snd_soc_get_enum_double - enumerated double mixer get callback 2366db2a4165SFrank Mandarino * @kcontrol: mixer control 2367ac11a2b3SMark Brown * @ucontrol: control element information 2368db2a4165SFrank Mandarino * 2369db2a4165SFrank Mandarino * Callback to get the value of a double enumerated mixer. 2370db2a4165SFrank Mandarino * 2371db2a4165SFrank Mandarino * Returns 0 for success. 2372db2a4165SFrank Mandarino */ 2373db2a4165SFrank Mandarino int snd_soc_get_enum_double(struct snd_kcontrol *kcontrol, 2374db2a4165SFrank Mandarino struct snd_ctl_elem_value *ucontrol) 2375db2a4165SFrank Mandarino { 2376907fe36aSLars-Peter Clausen struct snd_soc_component *component = snd_kcontrol_chip(kcontrol); 2377db2a4165SFrank Mandarino struct soc_enum *e = (struct soc_enum *)kcontrol->private_value; 237829ae2fa5SLars-Peter Clausen unsigned int val, item; 237929ae2fa5SLars-Peter Clausen unsigned int reg_val; 2380907fe36aSLars-Peter Clausen int ret; 2381db2a4165SFrank Mandarino 2382907fe36aSLars-Peter Clausen ret = snd_soc_component_read(component, e->reg, ®_val); 2383907fe36aSLars-Peter Clausen if (ret) 2384907fe36aSLars-Peter Clausen return ret; 238529ae2fa5SLars-Peter Clausen val = (reg_val >> e->shift_l) & e->mask; 238629ae2fa5SLars-Peter Clausen item = snd_soc_enum_val_to_item(e, val); 238729ae2fa5SLars-Peter Clausen ucontrol->value.enumerated.item[0] = item; 238829ae2fa5SLars-Peter Clausen if (e->shift_l != e->shift_r) { 238929ae2fa5SLars-Peter Clausen val = (reg_val >> e->shift_l) & e->mask; 239029ae2fa5SLars-Peter Clausen item = snd_soc_enum_val_to_item(e, val); 239129ae2fa5SLars-Peter Clausen ucontrol->value.enumerated.item[1] = item; 239229ae2fa5SLars-Peter Clausen } 2393db2a4165SFrank Mandarino 2394db2a4165SFrank Mandarino return 0; 2395db2a4165SFrank Mandarino } 2396db2a4165SFrank Mandarino EXPORT_SYMBOL_GPL(snd_soc_get_enum_double); 2397db2a4165SFrank Mandarino 2398db2a4165SFrank Mandarino /** 2399db2a4165SFrank Mandarino * snd_soc_put_enum_double - enumerated double mixer put callback 2400db2a4165SFrank Mandarino * @kcontrol: mixer control 2401ac11a2b3SMark Brown * @ucontrol: control element information 2402db2a4165SFrank Mandarino * 2403db2a4165SFrank Mandarino * Callback to set the value of a double enumerated mixer. 2404db2a4165SFrank Mandarino * 2405db2a4165SFrank Mandarino * Returns 0 for success. 2406db2a4165SFrank Mandarino */ 2407db2a4165SFrank Mandarino int snd_soc_put_enum_double(struct snd_kcontrol *kcontrol, 2408db2a4165SFrank Mandarino struct snd_ctl_elem_value *ucontrol) 2409db2a4165SFrank Mandarino { 2410907fe36aSLars-Peter Clausen struct snd_soc_component *component = snd_kcontrol_chip(kcontrol); 2411db2a4165SFrank Mandarino struct soc_enum *e = (struct soc_enum *)kcontrol->private_value; 241229ae2fa5SLars-Peter Clausen unsigned int *item = ucontrol->value.enumerated.item; 241346f5822fSDaniel Ribeiro unsigned int val; 241486767b7dSLars-Peter Clausen unsigned int mask; 2415db2a4165SFrank Mandarino 241629ae2fa5SLars-Peter Clausen if (item[0] >= e->items) 2417db2a4165SFrank Mandarino return -EINVAL; 241829ae2fa5SLars-Peter Clausen val = snd_soc_enum_item_to_val(e, item[0]) << e->shift_l; 241986767b7dSLars-Peter Clausen mask = e->mask << e->shift_l; 2420db2a4165SFrank Mandarino if (e->shift_l != e->shift_r) { 242129ae2fa5SLars-Peter Clausen if (item[1] >= e->items) 2422db2a4165SFrank Mandarino return -EINVAL; 242329ae2fa5SLars-Peter Clausen val |= snd_soc_enum_item_to_val(e, item[1]) << e->shift_r; 242486767b7dSLars-Peter Clausen mask |= e->mask << e->shift_r; 2425db2a4165SFrank Mandarino } 2426db2a4165SFrank Mandarino 2427907fe36aSLars-Peter Clausen return snd_soc_component_update_bits(component, e->reg, mask, val); 2428db2a4165SFrank Mandarino } 2429db2a4165SFrank Mandarino EXPORT_SYMBOL_GPL(snd_soc_put_enum_double); 2430db2a4165SFrank Mandarino 2431db2a4165SFrank Mandarino /** 2432f227b88fSMarkus Pargmann * snd_soc_read_signed - Read a codec register and interprete as signed value 2433907fe36aSLars-Peter Clausen * @component: component 2434f227b88fSMarkus Pargmann * @reg: Register to read 2435f227b88fSMarkus Pargmann * @mask: Mask to use after shifting the register value 2436f227b88fSMarkus Pargmann * @shift: Right shift of register value 2437f227b88fSMarkus Pargmann * @sign_bit: Bit that describes if a number is negative or not. 2438907fe36aSLars-Peter Clausen * @signed_val: Pointer to where the read value should be stored 24392e72f8e3SPeter Ujfalusi * 2440f227b88fSMarkus Pargmann * This functions reads a codec register. The register value is shifted right 2441f227b88fSMarkus Pargmann * by 'shift' bits and masked with the given 'mask'. Afterwards it translates 2442f227b88fSMarkus Pargmann * the given registervalue into a signed integer if sign_bit is non-zero. 24432e72f8e3SPeter Ujfalusi * 2444907fe36aSLars-Peter Clausen * Returns 0 on sucess, otherwise an error value 24452e72f8e3SPeter Ujfalusi */ 2446907fe36aSLars-Peter Clausen static int snd_soc_read_signed(struct snd_soc_component *component, 2447907fe36aSLars-Peter Clausen unsigned int reg, unsigned int mask, unsigned int shift, 2448907fe36aSLars-Peter Clausen unsigned int sign_bit, int *signed_val) 24492e72f8e3SPeter Ujfalusi { 2450f227b88fSMarkus Pargmann int ret; 245146f5822fSDaniel Ribeiro unsigned int val; 24522e72f8e3SPeter Ujfalusi 2453907fe36aSLars-Peter Clausen ret = snd_soc_component_read(component, reg, &val); 2454907fe36aSLars-Peter Clausen if (ret < 0) 2455907fe36aSLars-Peter Clausen return ret; 24562e72f8e3SPeter Ujfalusi 2457907fe36aSLars-Peter Clausen val = (val >> shift) & mask; 2458907fe36aSLars-Peter Clausen 2459907fe36aSLars-Peter Clausen if (!sign_bit) { 2460907fe36aSLars-Peter Clausen *signed_val = val; 2461907fe36aSLars-Peter Clausen return 0; 2462907fe36aSLars-Peter Clausen } 2463f227b88fSMarkus Pargmann 2464f227b88fSMarkus Pargmann /* non-negative number */ 2465907fe36aSLars-Peter Clausen if (!(val & BIT(sign_bit))) { 2466907fe36aSLars-Peter Clausen *signed_val = val; 2467907fe36aSLars-Peter Clausen return 0; 2468907fe36aSLars-Peter Clausen } 2469f227b88fSMarkus Pargmann 2470f227b88fSMarkus Pargmann ret = val; 2471f227b88fSMarkus Pargmann 2472f227b88fSMarkus Pargmann /* 2473f227b88fSMarkus Pargmann * The register most probably does not contain a full-sized int. 2474f227b88fSMarkus Pargmann * Instead we have an arbitrary number of bits in a signed 2475f227b88fSMarkus Pargmann * representation which has to be translated into a full-sized int. 2476f227b88fSMarkus Pargmann * This is done by filling up all bits above the sign-bit. 2477f227b88fSMarkus Pargmann */ 2478f227b88fSMarkus Pargmann ret |= ~((int)(BIT(sign_bit) - 1)); 2479f227b88fSMarkus Pargmann 2480907fe36aSLars-Peter Clausen *signed_val = ret; 2481907fe36aSLars-Peter Clausen 2482907fe36aSLars-Peter Clausen return 0; 24832e72f8e3SPeter Ujfalusi } 24842e72f8e3SPeter Ujfalusi 24852e72f8e3SPeter Ujfalusi /** 2486db2a4165SFrank Mandarino * snd_soc_info_volsw - single mixer info callback 2487db2a4165SFrank Mandarino * @kcontrol: mixer control 2488db2a4165SFrank Mandarino * @uinfo: control element information 2489db2a4165SFrank Mandarino * 2490e8f5a103SPeter Ujfalusi * Callback to provide information about a single mixer control, or a double 2491e8f5a103SPeter Ujfalusi * mixer control that spans 2 registers. 2492db2a4165SFrank Mandarino * 2493db2a4165SFrank Mandarino * Returns 0 for success. 2494db2a4165SFrank Mandarino */ 2495db2a4165SFrank Mandarino int snd_soc_info_volsw(struct snd_kcontrol *kcontrol, 2496db2a4165SFrank Mandarino struct snd_ctl_elem_info *uinfo) 2497db2a4165SFrank Mandarino { 24984eaa9819SJon Smirl struct soc_mixer_control *mc = 24994eaa9819SJon Smirl (struct soc_mixer_control *)kcontrol->private_value; 2500d11bb4a9SPeter Ujfalusi int platform_max; 2501db2a4165SFrank Mandarino 2502d11bb4a9SPeter Ujfalusi if (!mc->platform_max) 2503d11bb4a9SPeter Ujfalusi mc->platform_max = mc->max; 2504d11bb4a9SPeter Ujfalusi platform_max = mc->platform_max; 2505d11bb4a9SPeter Ujfalusi 2506d11bb4a9SPeter Ujfalusi if (platform_max == 1 && !strstr(kcontrol->id.name, " Volume")) 2507a7a4ac86SPhilipp Zabel uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN; 2508a7a4ac86SPhilipp Zabel else 2509a7a4ac86SPhilipp Zabel uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER; 2510a7a4ac86SPhilipp Zabel 2511e8f5a103SPeter Ujfalusi uinfo->count = snd_soc_volsw_is_stereo(mc) ? 2 : 1; 2512db2a4165SFrank Mandarino uinfo->value.integer.min = 0; 2513f227b88fSMarkus Pargmann uinfo->value.integer.max = platform_max - mc->min; 2514db2a4165SFrank Mandarino return 0; 2515db2a4165SFrank Mandarino } 2516db2a4165SFrank Mandarino EXPORT_SYMBOL_GPL(snd_soc_info_volsw); 2517db2a4165SFrank Mandarino 2518db2a4165SFrank Mandarino /** 2519db2a4165SFrank Mandarino * snd_soc_get_volsw - single mixer get callback 2520db2a4165SFrank Mandarino * @kcontrol: mixer control 2521ac11a2b3SMark Brown * @ucontrol: control element information 2522db2a4165SFrank Mandarino * 2523f7915d99SPeter Ujfalusi * Callback to get the value of a single mixer control, or a double mixer 2524f7915d99SPeter Ujfalusi * control that spans 2 registers. 2525db2a4165SFrank Mandarino * 2526db2a4165SFrank Mandarino * Returns 0 for success. 2527db2a4165SFrank Mandarino */ 2528db2a4165SFrank Mandarino int snd_soc_get_volsw(struct snd_kcontrol *kcontrol, 2529db2a4165SFrank Mandarino struct snd_ctl_elem_value *ucontrol) 2530db2a4165SFrank Mandarino { 2531907fe36aSLars-Peter Clausen struct snd_soc_component *component = snd_kcontrol_chip(kcontrol); 25324eaa9819SJon Smirl struct soc_mixer_control *mc = 25334eaa9819SJon Smirl (struct soc_mixer_control *)kcontrol->private_value; 2534815ecf8dSJon Smirl unsigned int reg = mc->reg; 2535f7915d99SPeter Ujfalusi unsigned int reg2 = mc->rreg; 2536815ecf8dSJon Smirl unsigned int shift = mc->shift; 2537815ecf8dSJon Smirl unsigned int rshift = mc->rshift; 25384eaa9819SJon Smirl int max = mc->max; 2539f227b88fSMarkus Pargmann int min = mc->min; 2540f227b88fSMarkus Pargmann int sign_bit = mc->sign_bit; 2541815ecf8dSJon Smirl unsigned int mask = (1 << fls(max)) - 1; 2542815ecf8dSJon Smirl unsigned int invert = mc->invert; 2543907fe36aSLars-Peter Clausen int val; 2544907fe36aSLars-Peter Clausen int ret; 2545db2a4165SFrank Mandarino 2546f227b88fSMarkus Pargmann if (sign_bit) 2547f227b88fSMarkus Pargmann mask = BIT(sign_bit + 1) - 1; 2548f227b88fSMarkus Pargmann 2549907fe36aSLars-Peter Clausen ret = snd_soc_read_signed(component, reg, mask, shift, sign_bit, &val); 2550907fe36aSLars-Peter Clausen if (ret) 2551907fe36aSLars-Peter Clausen return ret; 2552907fe36aSLars-Peter Clausen 2553907fe36aSLars-Peter Clausen ucontrol->value.integer.value[0] = val - min; 2554f7915d99SPeter Ujfalusi if (invert) 2555db2a4165SFrank Mandarino ucontrol->value.integer.value[0] = 2556a7a4ac86SPhilipp Zabel max - ucontrol->value.integer.value[0]; 2557f7915d99SPeter Ujfalusi 2558f7915d99SPeter Ujfalusi if (snd_soc_volsw_is_stereo(mc)) { 2559f7915d99SPeter Ujfalusi if (reg == reg2) 2560907fe36aSLars-Peter Clausen ret = snd_soc_read_signed(component, reg, mask, rshift, 2561907fe36aSLars-Peter Clausen sign_bit, &val); 2562f7915d99SPeter Ujfalusi else 2563907fe36aSLars-Peter Clausen ret = snd_soc_read_signed(component, reg2, mask, shift, 2564907fe36aSLars-Peter Clausen sign_bit, &val); 2565907fe36aSLars-Peter Clausen if (ret) 2566907fe36aSLars-Peter Clausen return ret; 2567907fe36aSLars-Peter Clausen 2568907fe36aSLars-Peter Clausen ucontrol->value.integer.value[1] = val - min; 2569f7915d99SPeter Ujfalusi if (invert) 2570db2a4165SFrank Mandarino ucontrol->value.integer.value[1] = 2571a7a4ac86SPhilipp Zabel max - ucontrol->value.integer.value[1]; 2572db2a4165SFrank Mandarino } 2573db2a4165SFrank Mandarino 2574db2a4165SFrank Mandarino return 0; 2575db2a4165SFrank Mandarino } 2576db2a4165SFrank Mandarino EXPORT_SYMBOL_GPL(snd_soc_get_volsw); 2577db2a4165SFrank Mandarino 2578db2a4165SFrank Mandarino /** 2579db2a4165SFrank Mandarino * snd_soc_put_volsw - single mixer put callback 2580db2a4165SFrank Mandarino * @kcontrol: mixer control 2581ac11a2b3SMark Brown * @ucontrol: control element information 2582db2a4165SFrank Mandarino * 2583974815baSPeter Ujfalusi * Callback to set the value of a single mixer control, or a double mixer 2584974815baSPeter Ujfalusi * control that spans 2 registers. 2585db2a4165SFrank Mandarino * 2586db2a4165SFrank Mandarino * Returns 0 for success. 2587db2a4165SFrank Mandarino */ 2588db2a4165SFrank Mandarino int snd_soc_put_volsw(struct snd_kcontrol *kcontrol, 2589db2a4165SFrank Mandarino struct snd_ctl_elem_value *ucontrol) 2590db2a4165SFrank Mandarino { 2591907fe36aSLars-Peter Clausen struct snd_soc_component *component = snd_kcontrol_chip(kcontrol); 25924eaa9819SJon Smirl struct soc_mixer_control *mc = 25934eaa9819SJon Smirl (struct soc_mixer_control *)kcontrol->private_value; 2594815ecf8dSJon Smirl unsigned int reg = mc->reg; 2595974815baSPeter Ujfalusi unsigned int reg2 = mc->rreg; 2596815ecf8dSJon Smirl unsigned int shift = mc->shift; 2597815ecf8dSJon Smirl unsigned int rshift = mc->rshift; 25984eaa9819SJon Smirl int max = mc->max; 2599f227b88fSMarkus Pargmann int min = mc->min; 2600f227b88fSMarkus Pargmann unsigned int sign_bit = mc->sign_bit; 2601815ecf8dSJon Smirl unsigned int mask = (1 << fls(max)) - 1; 2602815ecf8dSJon Smirl unsigned int invert = mc->invert; 2603974815baSPeter Ujfalusi int err; 2604939d9f16SNenghua Cao bool type_2r = false; 2605974815baSPeter Ujfalusi unsigned int val2 = 0; 2606974815baSPeter Ujfalusi unsigned int val, val_mask; 2607db2a4165SFrank Mandarino 2608f227b88fSMarkus Pargmann if (sign_bit) 2609f227b88fSMarkus Pargmann mask = BIT(sign_bit + 1) - 1; 2610f227b88fSMarkus Pargmann 2611f227b88fSMarkus Pargmann val = ((ucontrol->value.integer.value[0] + min) & mask); 2612db2a4165SFrank Mandarino if (invert) 2613a7a4ac86SPhilipp Zabel val = max - val; 2614db2a4165SFrank Mandarino val_mask = mask << shift; 2615db2a4165SFrank Mandarino val = val << shift; 2616974815baSPeter Ujfalusi if (snd_soc_volsw_is_stereo(mc)) { 2617f227b88fSMarkus Pargmann val2 = ((ucontrol->value.integer.value[1] + min) & mask); 2618db2a4165SFrank Mandarino if (invert) 2619a7a4ac86SPhilipp Zabel val2 = max - val2; 2620974815baSPeter Ujfalusi if (reg == reg2) { 2621db2a4165SFrank Mandarino val_mask |= mask << rshift; 2622db2a4165SFrank Mandarino val |= val2 << rshift; 2623974815baSPeter Ujfalusi } else { 2624db2a4165SFrank Mandarino val2 = val2 << shift; 2625939d9f16SNenghua Cao type_2r = true; 2626974815baSPeter Ujfalusi } 2627974815baSPeter Ujfalusi } 2628907fe36aSLars-Peter Clausen err = snd_soc_component_update_bits(component, reg, val_mask, val); 26293ff3f64bSMark Brown if (err < 0) 2630db2a4165SFrank Mandarino return err; 2631db2a4165SFrank Mandarino 2632974815baSPeter Ujfalusi if (type_2r) 2633907fe36aSLars-Peter Clausen err = snd_soc_component_update_bits(component, reg2, val_mask, 2634907fe36aSLars-Peter Clausen val2); 2635974815baSPeter Ujfalusi 2636db2a4165SFrank Mandarino return err; 2637db2a4165SFrank Mandarino } 2638974815baSPeter Ujfalusi EXPORT_SYMBOL_GPL(snd_soc_put_volsw); 2639db2a4165SFrank Mandarino 2640e13ac2e9SMark Brown /** 26411d99f243SBrian Austin * snd_soc_get_volsw_sx - single mixer get callback 26421d99f243SBrian Austin * @kcontrol: mixer control 26431d99f243SBrian Austin * @ucontrol: control element information 26441d99f243SBrian Austin * 26451d99f243SBrian Austin * Callback to get the value of a single mixer control, or a double mixer 26461d99f243SBrian Austin * control that spans 2 registers. 26471d99f243SBrian Austin * 26481d99f243SBrian Austin * Returns 0 for success. 26491d99f243SBrian Austin */ 26501d99f243SBrian Austin int snd_soc_get_volsw_sx(struct snd_kcontrol *kcontrol, 26511d99f243SBrian Austin struct snd_ctl_elem_value *ucontrol) 26521d99f243SBrian Austin { 2653907fe36aSLars-Peter Clausen struct snd_soc_component *component = snd_kcontrol_chip(kcontrol); 26541d99f243SBrian Austin struct soc_mixer_control *mc = 26551d99f243SBrian Austin (struct soc_mixer_control *)kcontrol->private_value; 26561d99f243SBrian Austin unsigned int reg = mc->reg; 26571d99f243SBrian Austin unsigned int reg2 = mc->rreg; 26581d99f243SBrian Austin unsigned int shift = mc->shift; 26591d99f243SBrian Austin unsigned int rshift = mc->rshift; 26601d99f243SBrian Austin int max = mc->max; 26611d99f243SBrian Austin int min = mc->min; 26621d99f243SBrian Austin int mask = (1 << (fls(min + max) - 1)) - 1; 2663907fe36aSLars-Peter Clausen unsigned int val; 2664907fe36aSLars-Peter Clausen int ret; 26651d99f243SBrian Austin 2666907fe36aSLars-Peter Clausen ret = snd_soc_component_read(component, reg, &val); 2667907fe36aSLars-Peter Clausen if (ret < 0) 2668907fe36aSLars-Peter Clausen return ret; 26691d99f243SBrian Austin 2670907fe36aSLars-Peter Clausen ucontrol->value.integer.value[0] = ((val >> shift) - min) & mask; 2671907fe36aSLars-Peter Clausen 2672907fe36aSLars-Peter Clausen if (snd_soc_volsw_is_stereo(mc)) { 2673907fe36aSLars-Peter Clausen ret = snd_soc_component_read(component, reg2, &val); 2674907fe36aSLars-Peter Clausen if (ret < 0) 2675907fe36aSLars-Peter Clausen return ret; 2676907fe36aSLars-Peter Clausen 2677907fe36aSLars-Peter Clausen val = ((val >> rshift) - min) & mask; 2678907fe36aSLars-Peter Clausen ucontrol->value.integer.value[1] = val; 2679907fe36aSLars-Peter Clausen } 26801d99f243SBrian Austin 26811d99f243SBrian Austin return 0; 26821d99f243SBrian Austin } 26831d99f243SBrian Austin EXPORT_SYMBOL_GPL(snd_soc_get_volsw_sx); 26841d99f243SBrian Austin 26851d99f243SBrian Austin /** 26861d99f243SBrian Austin * snd_soc_put_volsw_sx - double mixer set callback 26871d99f243SBrian Austin * @kcontrol: mixer control 26881d99f243SBrian Austin * @uinfo: control element information 26891d99f243SBrian Austin * 26901d99f243SBrian Austin * Callback to set the value of a double mixer control that spans 2 registers. 26911d99f243SBrian Austin * 26921d99f243SBrian Austin * Returns 0 for success. 26931d99f243SBrian Austin */ 26941d99f243SBrian Austin int snd_soc_put_volsw_sx(struct snd_kcontrol *kcontrol, 26951d99f243SBrian Austin struct snd_ctl_elem_value *ucontrol) 26961d99f243SBrian Austin { 2697907fe36aSLars-Peter Clausen struct snd_soc_component *component = snd_kcontrol_chip(kcontrol); 26981d99f243SBrian Austin struct soc_mixer_control *mc = 26991d99f243SBrian Austin (struct soc_mixer_control *)kcontrol->private_value; 27001d99f243SBrian Austin 27011d99f243SBrian Austin unsigned int reg = mc->reg; 27021d99f243SBrian Austin unsigned int reg2 = mc->rreg; 27031d99f243SBrian Austin unsigned int shift = mc->shift; 27041d99f243SBrian Austin unsigned int rshift = mc->rshift; 27051d99f243SBrian Austin int max = mc->max; 27061d99f243SBrian Austin int min = mc->min; 27071d99f243SBrian Austin int mask = (1 << (fls(min + max) - 1)) - 1; 270827f1d759SBrian Austin int err = 0; 27091a39019eSBard Liao unsigned int val, val_mask, val2 = 0; 27101d99f243SBrian Austin 27111d99f243SBrian Austin val_mask = mask << shift; 27121d99f243SBrian Austin val = (ucontrol->value.integer.value[0] + min) & mask; 27131d99f243SBrian Austin val = val << shift; 27141d99f243SBrian Austin 2715907fe36aSLars-Peter Clausen err = snd_soc_component_update_bits(component, reg, val_mask, val); 2716d055852eSMukund Navada if (err < 0) 27171d99f243SBrian Austin return err; 27181d99f243SBrian Austin 27191d99f243SBrian Austin if (snd_soc_volsw_is_stereo(mc)) { 27201d99f243SBrian Austin val_mask = mask << rshift; 27211d99f243SBrian Austin val2 = (ucontrol->value.integer.value[1] + min) & mask; 27221d99f243SBrian Austin val2 = val2 << rshift; 27231d99f243SBrian Austin 2724907fe36aSLars-Peter Clausen err = snd_soc_component_update_bits(component, reg2, val_mask, 2725907fe36aSLars-Peter Clausen val2); 27261d99f243SBrian Austin } 2727907fe36aSLars-Peter Clausen return err; 27281d99f243SBrian Austin } 27291d99f243SBrian Austin EXPORT_SYMBOL_GPL(snd_soc_put_volsw_sx); 27301d99f243SBrian Austin 27311d99f243SBrian Austin /** 2732e13ac2e9SMark Brown * snd_soc_info_volsw_s8 - signed mixer info callback 2733e13ac2e9SMark Brown * @kcontrol: mixer control 2734e13ac2e9SMark Brown * @uinfo: control element information 2735e13ac2e9SMark Brown * 2736e13ac2e9SMark Brown * Callback to provide information about a signed mixer control. 2737e13ac2e9SMark Brown * 2738e13ac2e9SMark Brown * Returns 0 for success. 2739e13ac2e9SMark Brown */ 2740e13ac2e9SMark Brown int snd_soc_info_volsw_s8(struct snd_kcontrol *kcontrol, 2741e13ac2e9SMark Brown struct snd_ctl_elem_info *uinfo) 2742e13ac2e9SMark Brown { 27434eaa9819SJon Smirl struct soc_mixer_control *mc = 27444eaa9819SJon Smirl (struct soc_mixer_control *)kcontrol->private_value; 2745d11bb4a9SPeter Ujfalusi int platform_max; 27464eaa9819SJon Smirl int min = mc->min; 2747e13ac2e9SMark Brown 2748d11bb4a9SPeter Ujfalusi if (!mc->platform_max) 2749d11bb4a9SPeter Ujfalusi mc->platform_max = mc->max; 2750d11bb4a9SPeter Ujfalusi platform_max = mc->platform_max; 2751d11bb4a9SPeter Ujfalusi 2752e13ac2e9SMark Brown uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER; 2753e13ac2e9SMark Brown uinfo->count = 2; 2754e13ac2e9SMark Brown uinfo->value.integer.min = 0; 2755d11bb4a9SPeter Ujfalusi uinfo->value.integer.max = platform_max - min; 2756e13ac2e9SMark Brown return 0; 2757e13ac2e9SMark Brown } 2758e13ac2e9SMark Brown EXPORT_SYMBOL_GPL(snd_soc_info_volsw_s8); 2759e13ac2e9SMark Brown 2760e13ac2e9SMark Brown /** 2761e13ac2e9SMark Brown * snd_soc_get_volsw_s8 - signed mixer get callback 2762e13ac2e9SMark Brown * @kcontrol: mixer control 2763ac11a2b3SMark Brown * @ucontrol: control element information 2764e13ac2e9SMark Brown * 2765e13ac2e9SMark Brown * Callback to get the value of a signed mixer control. 2766e13ac2e9SMark Brown * 2767e13ac2e9SMark Brown * Returns 0 for success. 2768e13ac2e9SMark Brown */ 2769e13ac2e9SMark Brown int snd_soc_get_volsw_s8(struct snd_kcontrol *kcontrol, 2770e13ac2e9SMark Brown struct snd_ctl_elem_value *ucontrol) 2771e13ac2e9SMark Brown { 27724eaa9819SJon Smirl struct soc_mixer_control *mc = 27734eaa9819SJon Smirl (struct soc_mixer_control *)kcontrol->private_value; 2774907fe36aSLars-Peter Clausen struct snd_soc_component *component = snd_kcontrol_chip(kcontrol); 2775815ecf8dSJon Smirl unsigned int reg = mc->reg; 2776907fe36aSLars-Peter Clausen unsigned int val; 27774eaa9819SJon Smirl int min = mc->min; 2778907fe36aSLars-Peter Clausen int ret; 2779907fe36aSLars-Peter Clausen 2780907fe36aSLars-Peter Clausen ret = snd_soc_component_read(component, reg, &val); 2781907fe36aSLars-Peter Clausen if (ret) 2782907fe36aSLars-Peter Clausen return ret; 2783e13ac2e9SMark Brown 2784e13ac2e9SMark Brown ucontrol->value.integer.value[0] = 2785e13ac2e9SMark Brown ((signed char)(val & 0xff))-min; 2786e13ac2e9SMark Brown ucontrol->value.integer.value[1] = 2787e13ac2e9SMark Brown ((signed char)((val >> 8) & 0xff))-min; 2788e13ac2e9SMark Brown return 0; 2789e13ac2e9SMark Brown } 2790e13ac2e9SMark Brown EXPORT_SYMBOL_GPL(snd_soc_get_volsw_s8); 2791e13ac2e9SMark Brown 2792e13ac2e9SMark Brown /** 2793e13ac2e9SMark Brown * snd_soc_put_volsw_sgn - signed mixer put callback 2794e13ac2e9SMark Brown * @kcontrol: mixer control 2795ac11a2b3SMark Brown * @ucontrol: control element information 2796e13ac2e9SMark Brown * 2797e13ac2e9SMark Brown * Callback to set the value of a signed mixer control. 2798e13ac2e9SMark Brown * 2799e13ac2e9SMark Brown * Returns 0 for success. 2800e13ac2e9SMark Brown */ 2801e13ac2e9SMark Brown int snd_soc_put_volsw_s8(struct snd_kcontrol *kcontrol, 2802e13ac2e9SMark Brown struct snd_ctl_elem_value *ucontrol) 2803e13ac2e9SMark Brown { 28044eaa9819SJon Smirl struct soc_mixer_control *mc = 28054eaa9819SJon Smirl (struct soc_mixer_control *)kcontrol->private_value; 2806907fe36aSLars-Peter Clausen struct snd_soc_component *component = snd_kcontrol_chip(kcontrol); 2807815ecf8dSJon Smirl unsigned int reg = mc->reg; 28084eaa9819SJon Smirl int min = mc->min; 280946f5822fSDaniel Ribeiro unsigned int val; 2810e13ac2e9SMark Brown 2811e13ac2e9SMark Brown val = (ucontrol->value.integer.value[0]+min) & 0xff; 2812e13ac2e9SMark Brown val |= ((ucontrol->value.integer.value[1]+min) & 0xff) << 8; 2813e13ac2e9SMark Brown 2814907fe36aSLars-Peter Clausen return snd_soc_component_update_bits(component, reg, 0xffff, val); 2815e13ac2e9SMark Brown } 2816e13ac2e9SMark Brown EXPORT_SYMBOL_GPL(snd_soc_put_volsw_s8); 2817e13ac2e9SMark Brown 28188c6529dbSLiam Girdwood /** 28196c9d8cf6SAdam Thomson * snd_soc_info_volsw_range - single mixer info callback with range. 28206c9d8cf6SAdam Thomson * @kcontrol: mixer control 28216c9d8cf6SAdam Thomson * @uinfo: control element information 28226c9d8cf6SAdam Thomson * 28236c9d8cf6SAdam Thomson * Callback to provide information, within a range, about a single 28246c9d8cf6SAdam Thomson * mixer control. 28256c9d8cf6SAdam Thomson * 28266c9d8cf6SAdam Thomson * returns 0 for success. 28276c9d8cf6SAdam Thomson */ 28286c9d8cf6SAdam Thomson int snd_soc_info_volsw_range(struct snd_kcontrol *kcontrol, 28296c9d8cf6SAdam Thomson struct snd_ctl_elem_info *uinfo) 28306c9d8cf6SAdam Thomson { 28316c9d8cf6SAdam Thomson struct soc_mixer_control *mc = 28326c9d8cf6SAdam Thomson (struct soc_mixer_control *)kcontrol->private_value; 28336c9d8cf6SAdam Thomson int platform_max; 28346c9d8cf6SAdam Thomson int min = mc->min; 28356c9d8cf6SAdam Thomson 28366c9d8cf6SAdam Thomson if (!mc->platform_max) 28376c9d8cf6SAdam Thomson mc->platform_max = mc->max; 28386c9d8cf6SAdam Thomson platform_max = mc->platform_max; 28396c9d8cf6SAdam Thomson 28406c9d8cf6SAdam Thomson uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER; 28419bde4f0bSMark Brown uinfo->count = snd_soc_volsw_is_stereo(mc) ? 2 : 1; 28426c9d8cf6SAdam Thomson uinfo->value.integer.min = 0; 28436c9d8cf6SAdam Thomson uinfo->value.integer.max = platform_max - min; 28446c9d8cf6SAdam Thomson 28456c9d8cf6SAdam Thomson return 0; 28466c9d8cf6SAdam Thomson } 28476c9d8cf6SAdam Thomson EXPORT_SYMBOL_GPL(snd_soc_info_volsw_range); 28486c9d8cf6SAdam Thomson 28496c9d8cf6SAdam Thomson /** 28506c9d8cf6SAdam Thomson * snd_soc_put_volsw_range - single mixer put value callback with range. 28516c9d8cf6SAdam Thomson * @kcontrol: mixer control 28526c9d8cf6SAdam Thomson * @ucontrol: control element information 28536c9d8cf6SAdam Thomson * 28546c9d8cf6SAdam Thomson * Callback to set the value, within a range, for a single mixer control. 28556c9d8cf6SAdam Thomson * 28566c9d8cf6SAdam Thomson * Returns 0 for success. 28576c9d8cf6SAdam Thomson */ 28586c9d8cf6SAdam Thomson int snd_soc_put_volsw_range(struct snd_kcontrol *kcontrol, 28596c9d8cf6SAdam Thomson struct snd_ctl_elem_value *ucontrol) 28606c9d8cf6SAdam Thomson { 28616c9d8cf6SAdam Thomson struct soc_mixer_control *mc = 28626c9d8cf6SAdam Thomson (struct soc_mixer_control *)kcontrol->private_value; 2863907fe36aSLars-Peter Clausen struct snd_soc_component *component = snd_kcontrol_chip(kcontrol); 28646c9d8cf6SAdam Thomson unsigned int reg = mc->reg; 28659bde4f0bSMark Brown unsigned int rreg = mc->rreg; 28666c9d8cf6SAdam Thomson unsigned int shift = mc->shift; 28676c9d8cf6SAdam Thomson int min = mc->min; 28686c9d8cf6SAdam Thomson int max = mc->max; 28696c9d8cf6SAdam Thomson unsigned int mask = (1 << fls(max)) - 1; 28706c9d8cf6SAdam Thomson unsigned int invert = mc->invert; 28716c9d8cf6SAdam Thomson unsigned int val, val_mask; 28729bde4f0bSMark Brown int ret; 28736c9d8cf6SAdam Thomson 28746c9d8cf6SAdam Thomson if (invert) 28755c7c343aSHoward Mitchell val = (max - ucontrol->value.integer.value[0]) & mask; 28765c7c343aSHoward Mitchell else 28775c7c343aSHoward Mitchell val = ((ucontrol->value.integer.value[0] + min) & mask); 28786c9d8cf6SAdam Thomson val_mask = mask << shift; 28796c9d8cf6SAdam Thomson val = val << shift; 28806c9d8cf6SAdam Thomson 2881907fe36aSLars-Peter Clausen ret = snd_soc_component_update_bits(component, reg, val_mask, val); 28820eaa6ccaSJoonyoung Shim if (ret < 0) 28839bde4f0bSMark Brown return ret; 28849bde4f0bSMark Brown 28859bde4f0bSMark Brown if (snd_soc_volsw_is_stereo(mc)) { 28869bde4f0bSMark Brown if (invert) 28875c7c343aSHoward Mitchell val = (max - ucontrol->value.integer.value[1]) & mask; 28885c7c343aSHoward Mitchell else 28895c7c343aSHoward Mitchell val = ((ucontrol->value.integer.value[1] + min) & mask); 28909bde4f0bSMark Brown val_mask = mask << shift; 28919bde4f0bSMark Brown val = val << shift; 28929bde4f0bSMark Brown 2893907fe36aSLars-Peter Clausen ret = snd_soc_component_update_bits(component, rreg, val_mask, 2894907fe36aSLars-Peter Clausen val); 28959bde4f0bSMark Brown } 28969bde4f0bSMark Brown 28979bde4f0bSMark Brown return ret; 28986c9d8cf6SAdam Thomson } 28996c9d8cf6SAdam Thomson EXPORT_SYMBOL_GPL(snd_soc_put_volsw_range); 29006c9d8cf6SAdam Thomson 29016c9d8cf6SAdam Thomson /** 29026c9d8cf6SAdam Thomson * snd_soc_get_volsw_range - single mixer get callback with range 29036c9d8cf6SAdam Thomson * @kcontrol: mixer control 29046c9d8cf6SAdam Thomson * @ucontrol: control element information 29056c9d8cf6SAdam Thomson * 29066c9d8cf6SAdam Thomson * Callback to get the value, within a range, of a single mixer control. 29076c9d8cf6SAdam Thomson * 29086c9d8cf6SAdam Thomson * Returns 0 for success. 29096c9d8cf6SAdam Thomson */ 29106c9d8cf6SAdam Thomson int snd_soc_get_volsw_range(struct snd_kcontrol *kcontrol, 29116c9d8cf6SAdam Thomson struct snd_ctl_elem_value *ucontrol) 29126c9d8cf6SAdam Thomson { 2913907fe36aSLars-Peter Clausen struct snd_soc_component *component = snd_kcontrol_chip(kcontrol); 29146c9d8cf6SAdam Thomson struct soc_mixer_control *mc = 29156c9d8cf6SAdam Thomson (struct soc_mixer_control *)kcontrol->private_value; 29166c9d8cf6SAdam Thomson unsigned int reg = mc->reg; 29179bde4f0bSMark Brown unsigned int rreg = mc->rreg; 29186c9d8cf6SAdam Thomson unsigned int shift = mc->shift; 29196c9d8cf6SAdam Thomson int min = mc->min; 29206c9d8cf6SAdam Thomson int max = mc->max; 29216c9d8cf6SAdam Thomson unsigned int mask = (1 << fls(max)) - 1; 29226c9d8cf6SAdam Thomson unsigned int invert = mc->invert; 2923907fe36aSLars-Peter Clausen unsigned int val; 2924907fe36aSLars-Peter Clausen int ret; 29256c9d8cf6SAdam Thomson 2926907fe36aSLars-Peter Clausen ret = snd_soc_component_read(component, reg, &val); 2927907fe36aSLars-Peter Clausen if (ret) 2928907fe36aSLars-Peter Clausen return ret; 2929907fe36aSLars-Peter Clausen 2930907fe36aSLars-Peter Clausen ucontrol->value.integer.value[0] = (val >> shift) & mask; 29316c9d8cf6SAdam Thomson if (invert) 29326c9d8cf6SAdam Thomson ucontrol->value.integer.value[0] = 29336c9d8cf6SAdam Thomson max - ucontrol->value.integer.value[0]; 29345c7c343aSHoward Mitchell else 29356c9d8cf6SAdam Thomson ucontrol->value.integer.value[0] = 29366c9d8cf6SAdam Thomson ucontrol->value.integer.value[0] - min; 29376c9d8cf6SAdam Thomson 29389bde4f0bSMark Brown if (snd_soc_volsw_is_stereo(mc)) { 2939907fe36aSLars-Peter Clausen ret = snd_soc_component_read(component, rreg, &val); 2940907fe36aSLars-Peter Clausen if (ret) 2941907fe36aSLars-Peter Clausen return ret; 2942907fe36aSLars-Peter Clausen 2943907fe36aSLars-Peter Clausen ucontrol->value.integer.value[1] = (val >> shift) & mask; 29449bde4f0bSMark Brown if (invert) 29459bde4f0bSMark Brown ucontrol->value.integer.value[1] = 29469bde4f0bSMark Brown max - ucontrol->value.integer.value[1]; 29475c7c343aSHoward Mitchell else 29489bde4f0bSMark Brown ucontrol->value.integer.value[1] = 29499bde4f0bSMark Brown ucontrol->value.integer.value[1] - min; 29509bde4f0bSMark Brown } 29519bde4f0bSMark Brown 29526c9d8cf6SAdam Thomson return 0; 29536c9d8cf6SAdam Thomson } 29546c9d8cf6SAdam Thomson EXPORT_SYMBOL_GPL(snd_soc_get_volsw_range); 29556c9d8cf6SAdam Thomson 29566c9d8cf6SAdam Thomson /** 2957637d3847SPeter Ujfalusi * snd_soc_limit_volume - Set new limit to an existing volume control. 2958637d3847SPeter Ujfalusi * 2959637d3847SPeter Ujfalusi * @codec: where to look for the control 2960637d3847SPeter Ujfalusi * @name: Name of the control 2961637d3847SPeter Ujfalusi * @max: new maximum limit 2962637d3847SPeter Ujfalusi * 2963637d3847SPeter Ujfalusi * Return 0 for success, else error. 2964637d3847SPeter Ujfalusi */ 2965637d3847SPeter Ujfalusi int snd_soc_limit_volume(struct snd_soc_codec *codec, 2966637d3847SPeter Ujfalusi const char *name, int max) 2967637d3847SPeter Ujfalusi { 296800200107SLars-Peter Clausen struct snd_card *card = codec->component.card->snd_card; 2969637d3847SPeter Ujfalusi struct snd_kcontrol *kctl; 2970637d3847SPeter Ujfalusi struct soc_mixer_control *mc; 2971637d3847SPeter Ujfalusi int found = 0; 2972637d3847SPeter Ujfalusi int ret = -EINVAL; 2973637d3847SPeter Ujfalusi 2974637d3847SPeter Ujfalusi /* Sanity check for name and max */ 2975637d3847SPeter Ujfalusi if (unlikely(!name || max <= 0)) 2976637d3847SPeter Ujfalusi return -EINVAL; 2977637d3847SPeter Ujfalusi 2978637d3847SPeter Ujfalusi list_for_each_entry(kctl, &card->controls, list) { 2979637d3847SPeter Ujfalusi if (!strncmp(kctl->id.name, name, sizeof(kctl->id.name))) { 2980637d3847SPeter Ujfalusi found = 1; 2981637d3847SPeter Ujfalusi break; 2982637d3847SPeter Ujfalusi } 2983637d3847SPeter Ujfalusi } 2984637d3847SPeter Ujfalusi if (found) { 2985637d3847SPeter Ujfalusi mc = (struct soc_mixer_control *)kctl->private_value; 2986637d3847SPeter Ujfalusi if (max <= mc->max) { 2987d11bb4a9SPeter Ujfalusi mc->platform_max = max; 2988637d3847SPeter Ujfalusi ret = 0; 2989637d3847SPeter Ujfalusi } 2990637d3847SPeter Ujfalusi } 2991637d3847SPeter Ujfalusi return ret; 2992637d3847SPeter Ujfalusi } 2993637d3847SPeter Ujfalusi EXPORT_SYMBOL_GPL(snd_soc_limit_volume); 2994637d3847SPeter Ujfalusi 299571d08516SMark Brown int snd_soc_bytes_info(struct snd_kcontrol *kcontrol, 299671d08516SMark Brown struct snd_ctl_elem_info *uinfo) 299771d08516SMark Brown { 2998907fe36aSLars-Peter Clausen struct snd_soc_component *component = snd_kcontrol_chip(kcontrol); 299971d08516SMark Brown struct soc_bytes *params = (void *)kcontrol->private_value; 300071d08516SMark Brown 300171d08516SMark Brown uinfo->type = SNDRV_CTL_ELEM_TYPE_BYTES; 3002907fe36aSLars-Peter Clausen uinfo->count = params->num_regs * component->val_bytes; 300371d08516SMark Brown 300471d08516SMark Brown return 0; 300571d08516SMark Brown } 300671d08516SMark Brown EXPORT_SYMBOL_GPL(snd_soc_bytes_info); 300771d08516SMark Brown 300871d08516SMark Brown int snd_soc_bytes_get(struct snd_kcontrol *kcontrol, 300971d08516SMark Brown struct snd_ctl_elem_value *ucontrol) 301071d08516SMark Brown { 3011907fe36aSLars-Peter Clausen struct snd_soc_component *component = snd_kcontrol_chip(kcontrol); 301271d08516SMark Brown struct soc_bytes *params = (void *)kcontrol->private_value; 301371d08516SMark Brown int ret; 301471d08516SMark Brown 3015907fe36aSLars-Peter Clausen if (component->regmap) 3016907fe36aSLars-Peter Clausen ret = regmap_raw_read(component->regmap, params->base, 301771d08516SMark Brown ucontrol->value.bytes.data, 3018907fe36aSLars-Peter Clausen params->num_regs * component->val_bytes); 301971d08516SMark Brown else 302071d08516SMark Brown ret = -EINVAL; 302171d08516SMark Brown 3022f831b055SMark Brown /* Hide any masked bytes to ensure consistent data reporting */ 3023f831b055SMark Brown if (ret == 0 && params->mask) { 3024907fe36aSLars-Peter Clausen switch (component->val_bytes) { 3025f831b055SMark Brown case 1: 3026f831b055SMark Brown ucontrol->value.bytes.data[0] &= ~params->mask; 3027f831b055SMark Brown break; 3028f831b055SMark Brown case 2: 3029f831b055SMark Brown ((u16 *)(&ucontrol->value.bytes.data))[0] 30308f1ec93aSCharles Keepax &= cpu_to_be16(~params->mask); 3031f831b055SMark Brown break; 3032f831b055SMark Brown case 4: 3033f831b055SMark Brown ((u32 *)(&ucontrol->value.bytes.data))[0] 30348f1ec93aSCharles Keepax &= cpu_to_be32(~params->mask); 3035f831b055SMark Brown break; 3036f831b055SMark Brown default: 3037f831b055SMark Brown return -EINVAL; 3038f831b055SMark Brown } 3039f831b055SMark Brown } 3040f831b055SMark Brown 304171d08516SMark Brown return ret; 304271d08516SMark Brown } 304371d08516SMark Brown EXPORT_SYMBOL_GPL(snd_soc_bytes_get); 304471d08516SMark Brown 304571d08516SMark Brown int snd_soc_bytes_put(struct snd_kcontrol *kcontrol, 304671d08516SMark Brown struct snd_ctl_elem_value *ucontrol) 304771d08516SMark Brown { 3048907fe36aSLars-Peter Clausen struct snd_soc_component *component = snd_kcontrol_chip(kcontrol); 304971d08516SMark Brown struct soc_bytes *params = (void *)kcontrol->private_value; 3050f831b055SMark Brown int ret, len; 3051a1a564edSNenghua Cao unsigned int val, mask; 3052f831b055SMark Brown void *data; 305371d08516SMark Brown 30546596aa04SXiubo Li if (!component->regmap || !params->num_regs) 3055f831b055SMark Brown return -EINVAL; 3056f831b055SMark Brown 3057907fe36aSLars-Peter Clausen len = params->num_regs * component->val_bytes; 3058f831b055SMark Brown 3059b5a8fe43SMark Brown data = kmemdup(ucontrol->value.bytes.data, len, GFP_KERNEL | GFP_DMA); 3060b5a8fe43SMark Brown if (!data) 3061b5a8fe43SMark Brown return -ENOMEM; 3062b5a8fe43SMark Brown 3063f831b055SMark Brown /* 3064f831b055SMark Brown * If we've got a mask then we need to preserve the register 3065f831b055SMark Brown * bits. We shouldn't modify the incoming data so take a 3066f831b055SMark Brown * copy. 3067f831b055SMark Brown */ 3068f831b055SMark Brown if (params->mask) { 3069907fe36aSLars-Peter Clausen ret = regmap_read(component->regmap, params->base, &val); 3070f831b055SMark Brown if (ret != 0) 3071e8b18addSWei Yongjun goto out; 3072f831b055SMark Brown 3073f831b055SMark Brown val &= params->mask; 3074f831b055SMark Brown 3075907fe36aSLars-Peter Clausen switch (component->val_bytes) { 3076f831b055SMark Brown case 1: 3077f831b055SMark Brown ((u8 *)data)[0] &= ~params->mask; 3078f831b055SMark Brown ((u8 *)data)[0] |= val; 3079f831b055SMark Brown break; 3080f831b055SMark Brown case 2: 3081a1a564edSNenghua Cao mask = ~params->mask; 3082907fe36aSLars-Peter Clausen ret = regmap_parse_val(component->regmap, 3083a1a564edSNenghua Cao &mask, &mask); 3084a1a564edSNenghua Cao if (ret != 0) 3085a1a564edSNenghua Cao goto out; 3086a1a564edSNenghua Cao 3087a1a564edSNenghua Cao ((u16 *)data)[0] &= mask; 3088a1a564edSNenghua Cao 3089907fe36aSLars-Peter Clausen ret = regmap_parse_val(component->regmap, 3090a1a564edSNenghua Cao &val, &val); 3091a1a564edSNenghua Cao if (ret != 0) 3092a1a564edSNenghua Cao goto out; 3093a1a564edSNenghua Cao 3094a1a564edSNenghua Cao ((u16 *)data)[0] |= val; 3095f831b055SMark Brown break; 3096f831b055SMark Brown case 4: 3097a1a564edSNenghua Cao mask = ~params->mask; 3098907fe36aSLars-Peter Clausen ret = regmap_parse_val(component->regmap, 3099a1a564edSNenghua Cao &mask, &mask); 3100a1a564edSNenghua Cao if (ret != 0) 3101a1a564edSNenghua Cao goto out; 3102a1a564edSNenghua Cao 3103a1a564edSNenghua Cao ((u32 *)data)[0] &= mask; 3104a1a564edSNenghua Cao 3105907fe36aSLars-Peter Clausen ret = regmap_parse_val(component->regmap, 3106a1a564edSNenghua Cao &val, &val); 3107a1a564edSNenghua Cao if (ret != 0) 3108a1a564edSNenghua Cao goto out; 3109a1a564edSNenghua Cao 3110a1a564edSNenghua Cao ((u32 *)data)[0] |= val; 3111f831b055SMark Brown break; 3112f831b055SMark Brown default: 3113e8b18addSWei Yongjun ret = -EINVAL; 3114e8b18addSWei Yongjun goto out; 3115f831b055SMark Brown } 3116f831b055SMark Brown } 3117f831b055SMark Brown 3118907fe36aSLars-Peter Clausen ret = regmap_raw_write(component->regmap, params->base, 3119f831b055SMark Brown data, len); 3120f831b055SMark Brown 3121e8b18addSWei Yongjun out: 3122f831b055SMark Brown kfree(data); 312371d08516SMark Brown 312471d08516SMark Brown return ret; 312571d08516SMark Brown } 312671d08516SMark Brown EXPORT_SYMBOL_GPL(snd_soc_bytes_put); 312771d08516SMark Brown 3128d9881208SVinod Koul int snd_soc_bytes_info_ext(struct snd_kcontrol *kcontrol, 3129d9881208SVinod Koul struct snd_ctl_elem_info *ucontrol) 3130d9881208SVinod Koul { 3131d9881208SVinod Koul struct soc_bytes_ext *params = (void *)kcontrol->private_value; 3132d9881208SVinod Koul 3133d9881208SVinod Koul ucontrol->type = SNDRV_CTL_ELEM_TYPE_BYTES; 3134d9881208SVinod Koul ucontrol->count = params->max; 3135d9881208SVinod Koul 3136d9881208SVinod Koul return 0; 3137d9881208SVinod Koul } 3138d9881208SVinod Koul EXPORT_SYMBOL_GPL(snd_soc_bytes_info_ext); 3139d9881208SVinod Koul 31407523a271SOmair Mohammed Abdullah int snd_soc_bytes_tlv_callback(struct snd_kcontrol *kcontrol, int op_flag, 31417523a271SOmair Mohammed Abdullah unsigned int size, unsigned int __user *tlv) 31427523a271SOmair Mohammed Abdullah { 31437523a271SOmair Mohammed Abdullah struct soc_bytes_ext *params = (void *)kcontrol->private_value; 31447523a271SOmair Mohammed Abdullah unsigned int count = size < params->max ? size : params->max; 31457523a271SOmair Mohammed Abdullah int ret = -ENXIO; 31467523a271SOmair Mohammed Abdullah 31477523a271SOmair Mohammed Abdullah switch (op_flag) { 31487523a271SOmair Mohammed Abdullah case SNDRV_CTL_TLV_OP_READ: 31497523a271SOmair Mohammed Abdullah if (params->get) 31507523a271SOmair Mohammed Abdullah ret = params->get(tlv, count); 31517523a271SOmair Mohammed Abdullah break; 31527523a271SOmair Mohammed Abdullah case SNDRV_CTL_TLV_OP_WRITE: 31537523a271SOmair Mohammed Abdullah if (params->put) 31547523a271SOmair Mohammed Abdullah ret = params->put(tlv, count); 31557523a271SOmair Mohammed Abdullah break; 31567523a271SOmair Mohammed Abdullah } 31577523a271SOmair Mohammed Abdullah return ret; 31587523a271SOmair Mohammed Abdullah } 31597523a271SOmair Mohammed Abdullah EXPORT_SYMBOL_GPL(snd_soc_bytes_tlv_callback); 31607523a271SOmair Mohammed Abdullah 3161b6f4bb38Sapatard@mandriva.com /** 31624183eed2SKristoffer KARLSSON * snd_soc_info_xr_sx - signed multi register info callback 31634183eed2SKristoffer KARLSSON * @kcontrol: mreg control 31644183eed2SKristoffer KARLSSON * @uinfo: control element information 31654183eed2SKristoffer KARLSSON * 31664183eed2SKristoffer KARLSSON * Callback to provide information of a control that can 31674183eed2SKristoffer KARLSSON * span multiple codec registers which together 31684183eed2SKristoffer KARLSSON * forms a single signed value in a MSB/LSB manner. 31694183eed2SKristoffer KARLSSON * 31704183eed2SKristoffer KARLSSON * Returns 0 for success. 31714183eed2SKristoffer KARLSSON */ 31724183eed2SKristoffer KARLSSON int snd_soc_info_xr_sx(struct snd_kcontrol *kcontrol, 31734183eed2SKristoffer KARLSSON struct snd_ctl_elem_info *uinfo) 31744183eed2SKristoffer KARLSSON { 31754183eed2SKristoffer KARLSSON struct soc_mreg_control *mc = 31764183eed2SKristoffer KARLSSON (struct soc_mreg_control *)kcontrol->private_value; 31774183eed2SKristoffer KARLSSON uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER; 31784183eed2SKristoffer KARLSSON uinfo->count = 1; 31794183eed2SKristoffer KARLSSON uinfo->value.integer.min = mc->min; 31804183eed2SKristoffer KARLSSON uinfo->value.integer.max = mc->max; 31814183eed2SKristoffer KARLSSON 31824183eed2SKristoffer KARLSSON return 0; 31834183eed2SKristoffer KARLSSON } 31844183eed2SKristoffer KARLSSON EXPORT_SYMBOL_GPL(snd_soc_info_xr_sx); 31854183eed2SKristoffer KARLSSON 31864183eed2SKristoffer KARLSSON /** 31874183eed2SKristoffer KARLSSON * snd_soc_get_xr_sx - signed multi register get callback 31884183eed2SKristoffer KARLSSON * @kcontrol: mreg control 31894183eed2SKristoffer KARLSSON * @ucontrol: control element information 31904183eed2SKristoffer KARLSSON * 31914183eed2SKristoffer KARLSSON * Callback to get the value of a control that can span 31924183eed2SKristoffer KARLSSON * multiple codec registers which together forms a single 31934183eed2SKristoffer KARLSSON * signed value in a MSB/LSB manner. The control supports 31944183eed2SKristoffer KARLSSON * specifying total no of bits used to allow for bitfields 31954183eed2SKristoffer KARLSSON * across the multiple codec registers. 31964183eed2SKristoffer KARLSSON * 31974183eed2SKristoffer KARLSSON * Returns 0 for success. 31984183eed2SKristoffer KARLSSON */ 31994183eed2SKristoffer KARLSSON int snd_soc_get_xr_sx(struct snd_kcontrol *kcontrol, 32004183eed2SKristoffer KARLSSON struct snd_ctl_elem_value *ucontrol) 32014183eed2SKristoffer KARLSSON { 3202907fe36aSLars-Peter Clausen struct snd_soc_component *component = snd_kcontrol_chip(kcontrol); 32034183eed2SKristoffer KARLSSON struct soc_mreg_control *mc = 32044183eed2SKristoffer KARLSSON (struct soc_mreg_control *)kcontrol->private_value; 32054183eed2SKristoffer KARLSSON unsigned int regbase = mc->regbase; 32064183eed2SKristoffer KARLSSON unsigned int regcount = mc->regcount; 3207907fe36aSLars-Peter Clausen unsigned int regwshift = component->val_bytes * BITS_PER_BYTE; 32084183eed2SKristoffer KARLSSON unsigned int regwmask = (1<<regwshift)-1; 32094183eed2SKristoffer KARLSSON unsigned int invert = mc->invert; 32104183eed2SKristoffer KARLSSON unsigned long mask = (1UL<<mc->nbits)-1; 32114183eed2SKristoffer KARLSSON long min = mc->min; 32124183eed2SKristoffer KARLSSON long max = mc->max; 32134183eed2SKristoffer KARLSSON long val = 0; 3214907fe36aSLars-Peter Clausen unsigned int regval; 32154183eed2SKristoffer KARLSSON unsigned int i; 3216907fe36aSLars-Peter Clausen int ret; 32174183eed2SKristoffer KARLSSON 32184183eed2SKristoffer KARLSSON for (i = 0; i < regcount; i++) { 3219907fe36aSLars-Peter Clausen ret = snd_soc_component_read(component, regbase+i, ®val); 3220907fe36aSLars-Peter Clausen if (ret) 3221907fe36aSLars-Peter Clausen return ret; 3222907fe36aSLars-Peter Clausen val |= (regval & regwmask) << (regwshift*(regcount-i-1)); 32234183eed2SKristoffer KARLSSON } 32244183eed2SKristoffer KARLSSON val &= mask; 32254183eed2SKristoffer KARLSSON if (min < 0 && val > max) 32264183eed2SKristoffer KARLSSON val |= ~mask; 32274183eed2SKristoffer KARLSSON if (invert) 32284183eed2SKristoffer KARLSSON val = max - val; 32294183eed2SKristoffer KARLSSON ucontrol->value.integer.value[0] = val; 32304183eed2SKristoffer KARLSSON 32314183eed2SKristoffer KARLSSON return 0; 32324183eed2SKristoffer KARLSSON } 32334183eed2SKristoffer KARLSSON EXPORT_SYMBOL_GPL(snd_soc_get_xr_sx); 32344183eed2SKristoffer KARLSSON 32354183eed2SKristoffer KARLSSON /** 32364183eed2SKristoffer KARLSSON * snd_soc_put_xr_sx - signed multi register get callback 32374183eed2SKristoffer KARLSSON * @kcontrol: mreg control 32384183eed2SKristoffer KARLSSON * @ucontrol: control element information 32394183eed2SKristoffer KARLSSON * 32404183eed2SKristoffer KARLSSON * Callback to set the value of a control that can span 32414183eed2SKristoffer KARLSSON * multiple codec registers which together forms a single 32424183eed2SKristoffer KARLSSON * signed value in a MSB/LSB manner. The control supports 32434183eed2SKristoffer KARLSSON * specifying total no of bits used to allow for bitfields 32444183eed2SKristoffer KARLSSON * across the multiple codec registers. 32454183eed2SKristoffer KARLSSON * 32464183eed2SKristoffer KARLSSON * Returns 0 for success. 32474183eed2SKristoffer KARLSSON */ 32484183eed2SKristoffer KARLSSON int snd_soc_put_xr_sx(struct snd_kcontrol *kcontrol, 32494183eed2SKristoffer KARLSSON struct snd_ctl_elem_value *ucontrol) 32504183eed2SKristoffer KARLSSON { 3251907fe36aSLars-Peter Clausen struct snd_soc_component *component = snd_kcontrol_chip(kcontrol); 32524183eed2SKristoffer KARLSSON struct soc_mreg_control *mc = 32534183eed2SKristoffer KARLSSON (struct soc_mreg_control *)kcontrol->private_value; 32544183eed2SKristoffer KARLSSON unsigned int regbase = mc->regbase; 32554183eed2SKristoffer KARLSSON unsigned int regcount = mc->regcount; 3256907fe36aSLars-Peter Clausen unsigned int regwshift = component->val_bytes * BITS_PER_BYTE; 32574183eed2SKristoffer KARLSSON unsigned int regwmask = (1<<regwshift)-1; 32584183eed2SKristoffer KARLSSON unsigned int invert = mc->invert; 32594183eed2SKristoffer KARLSSON unsigned long mask = (1UL<<mc->nbits)-1; 32604183eed2SKristoffer KARLSSON long max = mc->max; 32614183eed2SKristoffer KARLSSON long val = ucontrol->value.integer.value[0]; 32624183eed2SKristoffer KARLSSON unsigned int i, regval, regmask; 32634183eed2SKristoffer KARLSSON int err; 32644183eed2SKristoffer KARLSSON 32654183eed2SKristoffer KARLSSON if (invert) 32664183eed2SKristoffer KARLSSON val = max - val; 32674183eed2SKristoffer KARLSSON val &= mask; 32684183eed2SKristoffer KARLSSON for (i = 0; i < regcount; i++) { 32694183eed2SKristoffer KARLSSON regval = (val >> (regwshift*(regcount-i-1))) & regwmask; 32704183eed2SKristoffer KARLSSON regmask = (mask >> (regwshift*(regcount-i-1))) & regwmask; 3271907fe36aSLars-Peter Clausen err = snd_soc_component_update_bits(component, regbase+i, 32724183eed2SKristoffer KARLSSON regmask, regval); 32734183eed2SKristoffer KARLSSON if (err < 0) 32744183eed2SKristoffer KARLSSON return err; 32754183eed2SKristoffer KARLSSON } 32764183eed2SKristoffer KARLSSON 32774183eed2SKristoffer KARLSSON return 0; 32784183eed2SKristoffer KARLSSON } 32794183eed2SKristoffer KARLSSON EXPORT_SYMBOL_GPL(snd_soc_put_xr_sx); 32804183eed2SKristoffer KARLSSON 32814183eed2SKristoffer KARLSSON /** 3282dd7b10b3SKristoffer KARLSSON * snd_soc_get_strobe - strobe get callback 3283dd7b10b3SKristoffer KARLSSON * @kcontrol: mixer control 3284dd7b10b3SKristoffer KARLSSON * @ucontrol: control element information 3285dd7b10b3SKristoffer KARLSSON * 3286dd7b10b3SKristoffer KARLSSON * Callback get the value of a strobe mixer control. 3287dd7b10b3SKristoffer KARLSSON * 3288dd7b10b3SKristoffer KARLSSON * Returns 0 for success. 3289dd7b10b3SKristoffer KARLSSON */ 3290dd7b10b3SKristoffer KARLSSON int snd_soc_get_strobe(struct snd_kcontrol *kcontrol, 3291dd7b10b3SKristoffer KARLSSON struct snd_ctl_elem_value *ucontrol) 3292dd7b10b3SKristoffer KARLSSON { 3293907fe36aSLars-Peter Clausen struct snd_soc_component *component = snd_kcontrol_chip(kcontrol); 3294dd7b10b3SKristoffer KARLSSON struct soc_mixer_control *mc = 3295dd7b10b3SKristoffer KARLSSON (struct soc_mixer_control *)kcontrol->private_value; 3296dd7b10b3SKristoffer KARLSSON unsigned int reg = mc->reg; 3297dd7b10b3SKristoffer KARLSSON unsigned int shift = mc->shift; 3298dd7b10b3SKristoffer KARLSSON unsigned int mask = 1 << shift; 3299dd7b10b3SKristoffer KARLSSON unsigned int invert = mc->invert != 0; 3300907fe36aSLars-Peter Clausen unsigned int val; 3301907fe36aSLars-Peter Clausen int ret; 3302907fe36aSLars-Peter Clausen 3303907fe36aSLars-Peter Clausen ret = snd_soc_component_read(component, reg, &val); 3304907fe36aSLars-Peter Clausen if (ret) 3305907fe36aSLars-Peter Clausen return ret; 3306907fe36aSLars-Peter Clausen 3307907fe36aSLars-Peter Clausen val &= mask; 3308dd7b10b3SKristoffer KARLSSON 3309dd7b10b3SKristoffer KARLSSON if (shift != 0 && val != 0) 3310dd7b10b3SKristoffer KARLSSON val = val >> shift; 3311dd7b10b3SKristoffer KARLSSON ucontrol->value.enumerated.item[0] = val ^ invert; 3312dd7b10b3SKristoffer KARLSSON 3313dd7b10b3SKristoffer KARLSSON return 0; 3314dd7b10b3SKristoffer KARLSSON } 3315dd7b10b3SKristoffer KARLSSON EXPORT_SYMBOL_GPL(snd_soc_get_strobe); 3316dd7b10b3SKristoffer KARLSSON 3317dd7b10b3SKristoffer KARLSSON /** 3318dd7b10b3SKristoffer KARLSSON * snd_soc_put_strobe - strobe put callback 3319dd7b10b3SKristoffer KARLSSON * @kcontrol: mixer control 3320dd7b10b3SKristoffer KARLSSON * @ucontrol: control element information 3321dd7b10b3SKristoffer KARLSSON * 3322dd7b10b3SKristoffer KARLSSON * Callback strobe a register bit to high then low (or the inverse) 3323dd7b10b3SKristoffer KARLSSON * in one pass of a single mixer enum control. 3324dd7b10b3SKristoffer KARLSSON * 3325dd7b10b3SKristoffer KARLSSON * Returns 1 for success. 3326dd7b10b3SKristoffer KARLSSON */ 3327dd7b10b3SKristoffer KARLSSON int snd_soc_put_strobe(struct snd_kcontrol *kcontrol, 3328dd7b10b3SKristoffer KARLSSON struct snd_ctl_elem_value *ucontrol) 3329dd7b10b3SKristoffer KARLSSON { 3330907fe36aSLars-Peter Clausen struct snd_soc_component *component = snd_kcontrol_chip(kcontrol); 3331dd7b10b3SKristoffer KARLSSON struct soc_mixer_control *mc = 3332dd7b10b3SKristoffer KARLSSON (struct soc_mixer_control *)kcontrol->private_value; 3333dd7b10b3SKristoffer KARLSSON unsigned int reg = mc->reg; 3334dd7b10b3SKristoffer KARLSSON unsigned int shift = mc->shift; 3335dd7b10b3SKristoffer KARLSSON unsigned int mask = 1 << shift; 3336dd7b10b3SKristoffer KARLSSON unsigned int invert = mc->invert != 0; 3337dd7b10b3SKristoffer KARLSSON unsigned int strobe = ucontrol->value.enumerated.item[0] != 0; 3338dd7b10b3SKristoffer KARLSSON unsigned int val1 = (strobe ^ invert) ? mask : 0; 3339dd7b10b3SKristoffer KARLSSON unsigned int val2 = (strobe ^ invert) ? 0 : mask; 3340dd7b10b3SKristoffer KARLSSON int err; 3341dd7b10b3SKristoffer KARLSSON 3342907fe36aSLars-Peter Clausen err = snd_soc_component_update_bits(component, reg, mask, val1); 3343dd7b10b3SKristoffer KARLSSON if (err < 0) 3344dd7b10b3SKristoffer KARLSSON return err; 3345dd7b10b3SKristoffer KARLSSON 3346907fe36aSLars-Peter Clausen return snd_soc_component_update_bits(component, reg, mask, val2); 3347dd7b10b3SKristoffer KARLSSON } 3348dd7b10b3SKristoffer KARLSSON EXPORT_SYMBOL_GPL(snd_soc_put_strobe); 3349dd7b10b3SKristoffer KARLSSON 3350dd7b10b3SKristoffer KARLSSON /** 33518c6529dbSLiam Girdwood * snd_soc_dai_set_sysclk - configure DAI system or master clock. 33528c6529dbSLiam Girdwood * @dai: DAI 33538c6529dbSLiam Girdwood * @clk_id: DAI specific clock ID 33548c6529dbSLiam Girdwood * @freq: new clock frequency in Hz 33558c6529dbSLiam Girdwood * @dir: new clock direction - input/output. 33568c6529dbSLiam Girdwood * 33578c6529dbSLiam Girdwood * Configures the DAI master (MCLK) or system (SYSCLK) clocking. 33588c6529dbSLiam Girdwood */ 33598c6529dbSLiam Girdwood int snd_soc_dai_set_sysclk(struct snd_soc_dai *dai, int clk_id, 33608c6529dbSLiam Girdwood unsigned int freq, int dir) 33618c6529dbSLiam Girdwood { 3362f0fba2adSLiam Girdwood if (dai->driver && dai->driver->ops->set_sysclk) 3363f0fba2adSLiam Girdwood return dai->driver->ops->set_sysclk(dai, clk_id, freq, dir); 3364ec4ee52aSMark Brown else if (dai->codec && dai->codec->driver->set_sysclk) 3365da1c6ea6SMark Brown return dai->codec->driver->set_sysclk(dai->codec, clk_id, 0, 3366ec4ee52aSMark Brown freq, dir); 33678c6529dbSLiam Girdwood else 33681104a9c8SMark Brown return -ENOTSUPP; 33698c6529dbSLiam Girdwood } 33708c6529dbSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_dai_set_sysclk); 33718c6529dbSLiam Girdwood 33728c6529dbSLiam Girdwood /** 3373ec4ee52aSMark Brown * snd_soc_codec_set_sysclk - configure CODEC system or master clock. 3374ec4ee52aSMark Brown * @codec: CODEC 3375ec4ee52aSMark Brown * @clk_id: DAI specific clock ID 3376da1c6ea6SMark Brown * @source: Source for the clock 3377ec4ee52aSMark Brown * @freq: new clock frequency in Hz 3378ec4ee52aSMark Brown * @dir: new clock direction - input/output. 3379ec4ee52aSMark Brown * 3380ec4ee52aSMark Brown * Configures the CODEC master (MCLK) or system (SYSCLK) clocking. 3381ec4ee52aSMark Brown */ 3382ec4ee52aSMark Brown int snd_soc_codec_set_sysclk(struct snd_soc_codec *codec, int clk_id, 3383da1c6ea6SMark Brown int source, unsigned int freq, int dir) 3384ec4ee52aSMark Brown { 3385ec4ee52aSMark Brown if (codec->driver->set_sysclk) 3386da1c6ea6SMark Brown return codec->driver->set_sysclk(codec, clk_id, source, 3387da1c6ea6SMark Brown freq, dir); 3388ec4ee52aSMark Brown else 33891104a9c8SMark Brown return -ENOTSUPP; 3390ec4ee52aSMark Brown } 3391ec4ee52aSMark Brown EXPORT_SYMBOL_GPL(snd_soc_codec_set_sysclk); 3392ec4ee52aSMark Brown 3393ec4ee52aSMark Brown /** 33948c6529dbSLiam Girdwood * snd_soc_dai_set_clkdiv - configure DAI clock dividers. 33958c6529dbSLiam Girdwood * @dai: DAI 3396ac11a2b3SMark Brown * @div_id: DAI specific clock divider ID 33978c6529dbSLiam Girdwood * @div: new clock divisor. 33988c6529dbSLiam Girdwood * 33998c6529dbSLiam Girdwood * Configures the clock dividers. This is used to derive the best DAI bit and 34008c6529dbSLiam Girdwood * frame clocks from the system or master clock. It's best to set the DAI bit 34018c6529dbSLiam Girdwood * and frame clocks as low as possible to save system power. 34028c6529dbSLiam Girdwood */ 34038c6529dbSLiam Girdwood int snd_soc_dai_set_clkdiv(struct snd_soc_dai *dai, 34048c6529dbSLiam Girdwood int div_id, int div) 34058c6529dbSLiam Girdwood { 3406f0fba2adSLiam Girdwood if (dai->driver && dai->driver->ops->set_clkdiv) 3407f0fba2adSLiam Girdwood return dai->driver->ops->set_clkdiv(dai, div_id, div); 34088c6529dbSLiam Girdwood else 34098c6529dbSLiam Girdwood return -EINVAL; 34108c6529dbSLiam Girdwood } 34118c6529dbSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_dai_set_clkdiv); 34128c6529dbSLiam Girdwood 34138c6529dbSLiam Girdwood /** 34148c6529dbSLiam Girdwood * snd_soc_dai_set_pll - configure DAI PLL. 34158c6529dbSLiam Girdwood * @dai: DAI 34168c6529dbSLiam Girdwood * @pll_id: DAI specific PLL ID 341785488037SMark Brown * @source: DAI specific source for the PLL 34188c6529dbSLiam Girdwood * @freq_in: PLL input clock frequency in Hz 34198c6529dbSLiam Girdwood * @freq_out: requested PLL output clock frequency in Hz 34208c6529dbSLiam Girdwood * 34218c6529dbSLiam Girdwood * Configures and enables PLL to generate output clock based on input clock. 34228c6529dbSLiam Girdwood */ 342385488037SMark Brown int snd_soc_dai_set_pll(struct snd_soc_dai *dai, int pll_id, int source, 342485488037SMark Brown unsigned int freq_in, unsigned int freq_out) 34258c6529dbSLiam Girdwood { 3426f0fba2adSLiam Girdwood if (dai->driver && dai->driver->ops->set_pll) 3427f0fba2adSLiam Girdwood return dai->driver->ops->set_pll(dai, pll_id, source, 342885488037SMark Brown freq_in, freq_out); 3429ec4ee52aSMark Brown else if (dai->codec && dai->codec->driver->set_pll) 3430ec4ee52aSMark Brown return dai->codec->driver->set_pll(dai->codec, pll_id, source, 3431ec4ee52aSMark Brown freq_in, freq_out); 34328c6529dbSLiam Girdwood else 34338c6529dbSLiam Girdwood return -EINVAL; 34348c6529dbSLiam Girdwood } 34358c6529dbSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_dai_set_pll); 34368c6529dbSLiam Girdwood 3437ec4ee52aSMark Brown /* 3438ec4ee52aSMark Brown * snd_soc_codec_set_pll - configure codec PLL. 3439ec4ee52aSMark Brown * @codec: CODEC 3440ec4ee52aSMark Brown * @pll_id: DAI specific PLL ID 3441ec4ee52aSMark Brown * @source: DAI specific source for the PLL 3442ec4ee52aSMark Brown * @freq_in: PLL input clock frequency in Hz 3443ec4ee52aSMark Brown * @freq_out: requested PLL output clock frequency in Hz 3444ec4ee52aSMark Brown * 3445ec4ee52aSMark Brown * Configures and enables PLL to generate output clock based on input clock. 3446ec4ee52aSMark Brown */ 3447ec4ee52aSMark Brown int snd_soc_codec_set_pll(struct snd_soc_codec *codec, int pll_id, int source, 3448ec4ee52aSMark Brown unsigned int freq_in, unsigned int freq_out) 3449ec4ee52aSMark Brown { 3450ec4ee52aSMark Brown if (codec->driver->set_pll) 3451ec4ee52aSMark Brown return codec->driver->set_pll(codec, pll_id, source, 3452ec4ee52aSMark Brown freq_in, freq_out); 3453ec4ee52aSMark Brown else 3454ec4ee52aSMark Brown return -EINVAL; 3455ec4ee52aSMark Brown } 3456ec4ee52aSMark Brown EXPORT_SYMBOL_GPL(snd_soc_codec_set_pll); 3457ec4ee52aSMark Brown 34588c6529dbSLiam Girdwood /** 3459e54cf76bSLiam Girdwood * snd_soc_dai_set_bclk_ratio - configure BCLK to sample rate ratio. 3460e54cf76bSLiam Girdwood * @dai: DAI 3461e54cf76bSLiam Girdwood * @ratio Ratio of BCLK to Sample rate. 3462e54cf76bSLiam Girdwood * 3463e54cf76bSLiam Girdwood * Configures the DAI for a preset BCLK to sample rate ratio. 3464e54cf76bSLiam Girdwood */ 3465e54cf76bSLiam Girdwood int snd_soc_dai_set_bclk_ratio(struct snd_soc_dai *dai, unsigned int ratio) 3466e54cf76bSLiam Girdwood { 3467e54cf76bSLiam Girdwood if (dai->driver && dai->driver->ops->set_bclk_ratio) 3468e54cf76bSLiam Girdwood return dai->driver->ops->set_bclk_ratio(dai, ratio); 3469e54cf76bSLiam Girdwood else 3470e54cf76bSLiam Girdwood return -EINVAL; 3471e54cf76bSLiam Girdwood } 3472e54cf76bSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_dai_set_bclk_ratio); 3473e54cf76bSLiam Girdwood 3474e54cf76bSLiam Girdwood /** 34758c6529dbSLiam Girdwood * snd_soc_dai_set_fmt - configure DAI hardware audio format. 34768c6529dbSLiam Girdwood * @dai: DAI 34778c6529dbSLiam Girdwood * @fmt: SND_SOC_DAIFMT_ format value. 34788c6529dbSLiam Girdwood * 34798c6529dbSLiam Girdwood * Configures the DAI hardware format and clocking. 34808c6529dbSLiam Girdwood */ 34818c6529dbSLiam Girdwood int snd_soc_dai_set_fmt(struct snd_soc_dai *dai, unsigned int fmt) 34828c6529dbSLiam Girdwood { 34835e4ba569SShawn Guo if (dai->driver == NULL) 34848c6529dbSLiam Girdwood return -EINVAL; 34855e4ba569SShawn Guo if (dai->driver->ops->set_fmt == NULL) 34865e4ba569SShawn Guo return -ENOTSUPP; 34875e4ba569SShawn Guo return dai->driver->ops->set_fmt(dai, fmt); 34888c6529dbSLiam Girdwood } 34898c6529dbSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_dai_set_fmt); 34908c6529dbSLiam Girdwood 34918c6529dbSLiam Girdwood /** 3492e5c21514SXiubo Li * snd_soc_xlate_tdm_slot - generate tx/rx slot mask. 349389c67857SXiubo Li * @slots: Number of slots in use. 349489c67857SXiubo Li * @tx_mask: bitmask representing active TX slots. 349589c67857SXiubo Li * @rx_mask: bitmask representing active RX slots. 349689c67857SXiubo Li * 349789c67857SXiubo Li * Generates the TDM tx and rx slot default masks for DAI. 349889c67857SXiubo Li */ 3499e5c21514SXiubo Li static int snd_soc_xlate_tdm_slot_mask(unsigned int slots, 350089c67857SXiubo Li unsigned int *tx_mask, 350189c67857SXiubo Li unsigned int *rx_mask) 350289c67857SXiubo Li { 350389c67857SXiubo Li if (*tx_mask || *rx_mask) 350489c67857SXiubo Li return 0; 350589c67857SXiubo Li 350689c67857SXiubo Li if (!slots) 350789c67857SXiubo Li return -EINVAL; 350889c67857SXiubo Li 350989c67857SXiubo Li *tx_mask = (1 << slots) - 1; 351089c67857SXiubo Li *rx_mask = (1 << slots) - 1; 351189c67857SXiubo Li 351289c67857SXiubo Li return 0; 351389c67857SXiubo Li } 351489c67857SXiubo Li 351589c67857SXiubo Li /** 35168c6529dbSLiam Girdwood * snd_soc_dai_set_tdm_slot - configure DAI TDM. 35178c6529dbSLiam Girdwood * @dai: DAI 3518a5479e38SDaniel Ribeiro * @tx_mask: bitmask representing active TX slots. 3519a5479e38SDaniel Ribeiro * @rx_mask: bitmask representing active RX slots. 35208c6529dbSLiam Girdwood * @slots: Number of slots in use. 3521a5479e38SDaniel Ribeiro * @slot_width: Width in bits for each slot. 35228c6529dbSLiam Girdwood * 35238c6529dbSLiam Girdwood * Configures a DAI for TDM operation. Both mask and slots are codec and DAI 35248c6529dbSLiam Girdwood * specific. 35258c6529dbSLiam Girdwood */ 35268c6529dbSLiam Girdwood int snd_soc_dai_set_tdm_slot(struct snd_soc_dai *dai, 3527a5479e38SDaniel Ribeiro unsigned int tx_mask, unsigned int rx_mask, int slots, int slot_width) 35288c6529dbSLiam Girdwood { 3529e5c21514SXiubo Li if (dai->driver && dai->driver->ops->xlate_tdm_slot_mask) 3530e5c21514SXiubo Li dai->driver->ops->xlate_tdm_slot_mask(slots, 353189c67857SXiubo Li &tx_mask, &rx_mask); 353289c67857SXiubo Li else 3533e5c21514SXiubo Li snd_soc_xlate_tdm_slot_mask(slots, &tx_mask, &rx_mask); 353489c67857SXiubo Li 353588bd870fSBenoit Cousson dai->tx_mask = tx_mask; 353688bd870fSBenoit Cousson dai->rx_mask = rx_mask; 353788bd870fSBenoit Cousson 3538f0fba2adSLiam Girdwood if (dai->driver && dai->driver->ops->set_tdm_slot) 3539f0fba2adSLiam Girdwood return dai->driver->ops->set_tdm_slot(dai, tx_mask, rx_mask, 3540a5479e38SDaniel Ribeiro slots, slot_width); 35418c6529dbSLiam Girdwood else 3542b2cbb6e1SXiubo Li return -ENOTSUPP; 35438c6529dbSLiam Girdwood } 35448c6529dbSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_dai_set_tdm_slot); 35458c6529dbSLiam Girdwood 35468c6529dbSLiam Girdwood /** 3547472df3cbSBarry Song * snd_soc_dai_set_channel_map - configure DAI audio channel map 3548472df3cbSBarry Song * @dai: DAI 3549472df3cbSBarry Song * @tx_num: how many TX channels 3550472df3cbSBarry Song * @tx_slot: pointer to an array which imply the TX slot number channel 3551472df3cbSBarry Song * 0~num-1 uses 3552472df3cbSBarry Song * @rx_num: how many RX channels 3553472df3cbSBarry Song * @rx_slot: pointer to an array which imply the RX slot number channel 3554472df3cbSBarry Song * 0~num-1 uses 3555472df3cbSBarry Song * 3556472df3cbSBarry Song * configure the relationship between channel number and TDM slot number. 3557472df3cbSBarry Song */ 3558472df3cbSBarry Song int snd_soc_dai_set_channel_map(struct snd_soc_dai *dai, 3559472df3cbSBarry Song unsigned int tx_num, unsigned int *tx_slot, 3560472df3cbSBarry Song unsigned int rx_num, unsigned int *rx_slot) 3561472df3cbSBarry Song { 3562f0fba2adSLiam Girdwood if (dai->driver && dai->driver->ops->set_channel_map) 3563f0fba2adSLiam Girdwood return dai->driver->ops->set_channel_map(dai, tx_num, tx_slot, 3564472df3cbSBarry Song rx_num, rx_slot); 3565472df3cbSBarry Song else 3566472df3cbSBarry Song return -EINVAL; 3567472df3cbSBarry Song } 3568472df3cbSBarry Song EXPORT_SYMBOL_GPL(snd_soc_dai_set_channel_map); 3569472df3cbSBarry Song 3570472df3cbSBarry Song /** 35718c6529dbSLiam Girdwood * snd_soc_dai_set_tristate - configure DAI system or master clock. 35728c6529dbSLiam Girdwood * @dai: DAI 35738c6529dbSLiam Girdwood * @tristate: tristate enable 35748c6529dbSLiam Girdwood * 35758c6529dbSLiam Girdwood * Tristates the DAI so that others can use it. 35768c6529dbSLiam Girdwood */ 35778c6529dbSLiam Girdwood int snd_soc_dai_set_tristate(struct snd_soc_dai *dai, int tristate) 35788c6529dbSLiam Girdwood { 3579f0fba2adSLiam Girdwood if (dai->driver && dai->driver->ops->set_tristate) 3580f0fba2adSLiam Girdwood return dai->driver->ops->set_tristate(dai, tristate); 35818c6529dbSLiam Girdwood else 35828c6529dbSLiam Girdwood return -EINVAL; 35838c6529dbSLiam Girdwood } 35848c6529dbSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_dai_set_tristate); 35858c6529dbSLiam Girdwood 35868c6529dbSLiam Girdwood /** 35878c6529dbSLiam Girdwood * snd_soc_dai_digital_mute - configure DAI system or master clock. 35888c6529dbSLiam Girdwood * @dai: DAI 35898c6529dbSLiam Girdwood * @mute: mute enable 3590da18396fSMark Brown * @direction: stream to mute 35918c6529dbSLiam Girdwood * 35928c6529dbSLiam Girdwood * Mutes the DAI DAC. 35938c6529dbSLiam Girdwood */ 3594da18396fSMark Brown int snd_soc_dai_digital_mute(struct snd_soc_dai *dai, int mute, 3595da18396fSMark Brown int direction) 35968c6529dbSLiam Girdwood { 3597da18396fSMark Brown if (!dai->driver) 3598da18396fSMark Brown return -ENOTSUPP; 3599da18396fSMark Brown 3600da18396fSMark Brown if (dai->driver->ops->mute_stream) 3601da18396fSMark Brown return dai->driver->ops->mute_stream(dai, mute, direction); 3602da18396fSMark Brown else if (direction == SNDRV_PCM_STREAM_PLAYBACK && 3603da18396fSMark Brown dai->driver->ops->digital_mute) 3604f0fba2adSLiam Girdwood return dai->driver->ops->digital_mute(dai, mute); 36058c6529dbSLiam Girdwood else 360604570c62SMark Brown return -ENOTSUPP; 36078c6529dbSLiam Girdwood } 36088c6529dbSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_dai_digital_mute); 36098c6529dbSLiam Girdwood 361088bd870fSBenoit Cousson static int snd_soc_init_multicodec(struct snd_soc_card *card, 361188bd870fSBenoit Cousson struct snd_soc_dai_link *dai_link) 361288bd870fSBenoit Cousson { 361388bd870fSBenoit Cousson /* Legacy codec/codec_dai link is a single entry in multicodec */ 361488bd870fSBenoit Cousson if (dai_link->codec_name || dai_link->codec_of_node || 361588bd870fSBenoit Cousson dai_link->codec_dai_name) { 361688bd870fSBenoit Cousson dai_link->num_codecs = 1; 361788bd870fSBenoit Cousson 361888bd870fSBenoit Cousson dai_link->codecs = devm_kzalloc(card->dev, 361988bd870fSBenoit Cousson sizeof(struct snd_soc_dai_link_component), 362088bd870fSBenoit Cousson GFP_KERNEL); 362188bd870fSBenoit Cousson if (!dai_link->codecs) 362288bd870fSBenoit Cousson return -ENOMEM; 362388bd870fSBenoit Cousson 362488bd870fSBenoit Cousson dai_link->codecs[0].name = dai_link->codec_name; 362588bd870fSBenoit Cousson dai_link->codecs[0].of_node = dai_link->codec_of_node; 362688bd870fSBenoit Cousson dai_link->codecs[0].dai_name = dai_link->codec_dai_name; 362788bd870fSBenoit Cousson } 362888bd870fSBenoit Cousson 362988bd870fSBenoit Cousson if (!dai_link->codecs) { 363088bd870fSBenoit Cousson dev_err(card->dev, "ASoC: DAI link has no CODECs\n"); 363188bd870fSBenoit Cousson return -EINVAL; 363288bd870fSBenoit Cousson } 363388bd870fSBenoit Cousson 363488bd870fSBenoit Cousson return 0; 363588bd870fSBenoit Cousson } 363688bd870fSBenoit Cousson 3637c5af3a2eSMark Brown /** 3638c5af3a2eSMark Brown * snd_soc_register_card - Register a card with the ASoC core 3639c5af3a2eSMark Brown * 3640ac11a2b3SMark Brown * @card: Card to register 3641c5af3a2eSMark Brown * 3642c5af3a2eSMark Brown */ 364370a7ca34SVinod Koul int snd_soc_register_card(struct snd_soc_card *card) 3644c5af3a2eSMark Brown { 364588bd870fSBenoit Cousson int i, j, ret; 3646f0fba2adSLiam Girdwood 3647c5af3a2eSMark Brown if (!card->name || !card->dev) 3648c5af3a2eSMark Brown return -EINVAL; 3649c5af3a2eSMark Brown 36505a504963SStephen Warren for (i = 0; i < card->num_links; i++) { 36515a504963SStephen Warren struct snd_soc_dai_link *link = &card->dai_link[i]; 36525a504963SStephen Warren 365388bd870fSBenoit Cousson ret = snd_soc_init_multicodec(card, link); 365488bd870fSBenoit Cousson if (ret) { 365588bd870fSBenoit Cousson dev_err(card->dev, "ASoC: failed to init multicodec\n"); 365688bd870fSBenoit Cousson return ret; 365788bd870fSBenoit Cousson } 365888bd870fSBenoit Cousson 365988bd870fSBenoit Cousson for (j = 0; j < link->num_codecs; j++) { 36605a504963SStephen Warren /* 36615a504963SStephen Warren * Codec must be specified by 1 of name or OF node, 36625a504963SStephen Warren * not both or neither. 36635a504963SStephen Warren */ 366488bd870fSBenoit Cousson if (!!link->codecs[j].name == 366588bd870fSBenoit Cousson !!link->codecs[j].of_node) { 366688bd870fSBenoit Cousson dev_err(card->dev, "ASoC: Neither/both codec name/of_node are set for %s\n", 366710e8aa9aSMichał Mirosław link->name); 36685a504963SStephen Warren return -EINVAL; 36695a504963SStephen Warren } 3670bc92657aSStephen Warren /* Codec DAI name must be specified */ 367188bd870fSBenoit Cousson if (!link->codecs[j].dai_name) { 367288bd870fSBenoit Cousson dev_err(card->dev, "ASoC: codec_dai_name not set for %s\n", 367310e8aa9aSMichał Mirosław link->name); 3674bc92657aSStephen Warren return -EINVAL; 3675bc92657aSStephen Warren } 367688bd870fSBenoit Cousson } 36775a504963SStephen Warren 36785a504963SStephen Warren /* 36795a504963SStephen Warren * Platform may be specified by either name or OF node, but 36805a504963SStephen Warren * can be left unspecified, and a dummy platform will be used. 36815a504963SStephen Warren */ 36825a504963SStephen Warren if (link->platform_name && link->platform_of_node) { 368310e8aa9aSMichał Mirosław dev_err(card->dev, 368410e8aa9aSMichał Mirosław "ASoC: Both platform name/of_node are set for %s\n", 368510e8aa9aSMichał Mirosław link->name); 36865a504963SStephen Warren return -EINVAL; 36875a504963SStephen Warren } 36885a504963SStephen Warren 36895a504963SStephen Warren /* 3690bc92657aSStephen Warren * CPU device may be specified by either name or OF node, but 3691bc92657aSStephen Warren * can be left unspecified, and will be matched based on DAI 3692bc92657aSStephen Warren * name alone.. 36935a504963SStephen Warren */ 3694bc92657aSStephen Warren if (link->cpu_name && link->cpu_of_node) { 369510e8aa9aSMichał Mirosław dev_err(card->dev, 369610e8aa9aSMichał Mirosław "ASoC: Neither/both cpu name/of_node are set for %s\n", 369710e8aa9aSMichał Mirosław link->name); 3698bc92657aSStephen Warren return -EINVAL; 3699bc92657aSStephen Warren } 3700bc92657aSStephen Warren /* 3701bc92657aSStephen Warren * At least one of CPU DAI name or CPU device name/node must be 3702bc92657aSStephen Warren * specified 3703bc92657aSStephen Warren */ 3704bc92657aSStephen Warren if (!link->cpu_dai_name && 3705bc92657aSStephen Warren !(link->cpu_name || link->cpu_of_node)) { 370610e8aa9aSMichał Mirosław dev_err(card->dev, 370710e8aa9aSMichał Mirosław "ASoC: Neither cpu_dai_name nor cpu_name/of_node are set for %s\n", 370810e8aa9aSMichał Mirosław link->name); 37095a504963SStephen Warren return -EINVAL; 37105a504963SStephen Warren } 37115a504963SStephen Warren } 37125a504963SStephen Warren 3713ed77cc12SMark Brown dev_set_drvdata(card->dev, card); 3714ed77cc12SMark Brown 3715111c6419SStephen Warren snd_soc_initialize_card_lists(card); 3716111c6419SStephen Warren 3717150dd2f8SVinod Koul soc_init_card_debugfs(card); 3718150dd2f8SVinod Koul 3719181a6892SMark Brown card->rtd = devm_kzalloc(card->dev, 3720181a6892SMark Brown sizeof(struct snd_soc_pcm_runtime) * 37212eea392dSJarkko Nikula (card->num_links + card->num_aux_devs), 3722f0fba2adSLiam Girdwood GFP_KERNEL); 3723f0fba2adSLiam Girdwood if (card->rtd == NULL) 3724f0fba2adSLiam Girdwood return -ENOMEM; 3725a7dbb603SLiam Girdwood card->num_rtd = 0; 37262eea392dSJarkko Nikula card->rtd_aux = &card->rtd[card->num_links]; 3727f0fba2adSLiam Girdwood 37285f3484acSLars-Peter Clausen for (i = 0; i < card->num_links; i++) { 37295f3484acSLars-Peter Clausen card->rtd[i].card = card; 3730f0fba2adSLiam Girdwood card->rtd[i].dai_link = &card->dai_link[i]; 373188bd870fSBenoit Cousson card->rtd[i].codec_dais = devm_kzalloc(card->dev, 373288bd870fSBenoit Cousson sizeof(struct snd_soc_dai *) * 373388bd870fSBenoit Cousson (card->rtd[i].dai_link->num_codecs), 373488bd870fSBenoit Cousson GFP_KERNEL); 373588bd870fSBenoit Cousson if (card->rtd[i].codec_dais == NULL) 373688bd870fSBenoit Cousson return -ENOMEM; 37375f3484acSLars-Peter Clausen } 37385f3484acSLars-Peter Clausen 37395f3484acSLars-Peter Clausen for (i = 0; i < card->num_aux_devs; i++) 37405f3484acSLars-Peter Clausen card->rtd_aux[i].card = card; 3741f0fba2adSLiam Girdwood 3742db432b41SMark Brown INIT_LIST_HEAD(&card->dapm_dirty); 3743c5af3a2eSMark Brown card->instantiated = 0; 3744f0fba2adSLiam Girdwood mutex_init(&card->mutex); 3745a73fb2dfSLiam Girdwood mutex_init(&card->dapm_mutex); 3746c5af3a2eSMark Brown 3747b19e6e7bSMark Brown ret = snd_soc_instantiate_card(card); 3748b19e6e7bSMark Brown if (ret != 0) 3749b19e6e7bSMark Brown soc_cleanup_card_debugfs(card); 3750c5af3a2eSMark Brown 3751988e8cc4SNicolin Chen /* deactivate pins to sleep state */ 3752988e8cc4SNicolin Chen for (i = 0; i < card->num_rtd; i++) { 375388bd870fSBenoit Cousson struct snd_soc_pcm_runtime *rtd = &card->rtd[i]; 375488bd870fSBenoit Cousson struct snd_soc_dai *cpu_dai = rtd->cpu_dai; 375588bd870fSBenoit Cousson int j; 375688bd870fSBenoit Cousson 375788bd870fSBenoit Cousson for (j = 0; j < rtd->num_codecs; j++) { 375888bd870fSBenoit Cousson struct snd_soc_dai *codec_dai = rtd->codec_dais[j]; 3759988e8cc4SNicolin Chen if (!codec_dai->active) 3760988e8cc4SNicolin Chen pinctrl_pm_select_sleep_state(codec_dai->dev); 376188bd870fSBenoit Cousson } 376288bd870fSBenoit Cousson 3763988e8cc4SNicolin Chen if (!cpu_dai->active) 3764988e8cc4SNicolin Chen pinctrl_pm_select_sleep_state(cpu_dai->dev); 3765988e8cc4SNicolin Chen } 3766988e8cc4SNicolin Chen 3767b19e6e7bSMark Brown return ret; 3768c5af3a2eSMark Brown } 376970a7ca34SVinod Koul EXPORT_SYMBOL_GPL(snd_soc_register_card); 3770c5af3a2eSMark Brown 3771c5af3a2eSMark Brown /** 3772c5af3a2eSMark Brown * snd_soc_unregister_card - Unregister a card with the ASoC core 3773c5af3a2eSMark Brown * 3774ac11a2b3SMark Brown * @card: Card to unregister 3775c5af3a2eSMark Brown * 3776c5af3a2eSMark Brown */ 377770a7ca34SVinod Koul int snd_soc_unregister_card(struct snd_soc_card *card) 3778c5af3a2eSMark Brown { 377901e0df66SLars-Peter Clausen if (card->instantiated) { 378001e0df66SLars-Peter Clausen card->instantiated = false; 37811c325f77SLars-Peter Clausen snd_soc_dapm_shutdown(card); 3782b0e26485SVinod Koul soc_cleanup_card_resources(card); 378301e0df66SLars-Peter Clausen } 3784f110bfc7SLiam Girdwood dev_dbg(card->dev, "ASoC: Unregistered card '%s'\n", card->name); 3785c5af3a2eSMark Brown 3786c5af3a2eSMark Brown return 0; 3787c5af3a2eSMark Brown } 378870a7ca34SVinod Koul EXPORT_SYMBOL_GPL(snd_soc_unregister_card); 3789c5af3a2eSMark Brown 3790f0fba2adSLiam Girdwood /* 3791f0fba2adSLiam Girdwood * Simplify DAI link configuration by removing ".-1" from device names 3792f0fba2adSLiam Girdwood * and sanitizing names. 3793f0fba2adSLiam Girdwood */ 37940b9a214aSDimitris Papastamos static char *fmt_single_name(struct device *dev, int *id) 3795f0fba2adSLiam Girdwood { 3796f0fba2adSLiam Girdwood char *found, name[NAME_SIZE]; 3797f0fba2adSLiam Girdwood int id1, id2; 3798f0fba2adSLiam Girdwood 3799f0fba2adSLiam Girdwood if (dev_name(dev) == NULL) 3800f0fba2adSLiam Girdwood return NULL; 3801f0fba2adSLiam Girdwood 380258818a77SDimitris Papastamos strlcpy(name, dev_name(dev), NAME_SIZE); 3803f0fba2adSLiam Girdwood 3804f0fba2adSLiam Girdwood /* are we a "%s.%d" name (platform and SPI components) */ 3805f0fba2adSLiam Girdwood found = strstr(name, dev->driver->name); 3806f0fba2adSLiam Girdwood if (found) { 3807f0fba2adSLiam Girdwood /* get ID */ 3808f0fba2adSLiam Girdwood if (sscanf(&found[strlen(dev->driver->name)], ".%d", id) == 1) { 3809f0fba2adSLiam Girdwood 3810f0fba2adSLiam Girdwood /* discard ID from name if ID == -1 */ 3811f0fba2adSLiam Girdwood if (*id == -1) 3812f0fba2adSLiam Girdwood found[strlen(dev->driver->name)] = '\0'; 3813f0fba2adSLiam Girdwood } 3814f0fba2adSLiam Girdwood 3815f0fba2adSLiam Girdwood } else { 3816f0fba2adSLiam Girdwood /* I2C component devices are named "bus-addr" */ 3817f0fba2adSLiam Girdwood if (sscanf(name, "%x-%x", &id1, &id2) == 2) { 3818f0fba2adSLiam Girdwood char tmp[NAME_SIZE]; 3819f0fba2adSLiam Girdwood 3820f0fba2adSLiam Girdwood /* create unique ID number from I2C addr and bus */ 382105899446SJarkko Nikula *id = ((id1 & 0xffff) << 16) + id2; 3822f0fba2adSLiam Girdwood 3823f0fba2adSLiam Girdwood /* sanitize component name for DAI link creation */ 3824f0fba2adSLiam Girdwood snprintf(tmp, NAME_SIZE, "%s.%s", dev->driver->name, name); 382558818a77SDimitris Papastamos strlcpy(name, tmp, NAME_SIZE); 3826f0fba2adSLiam Girdwood } else 3827f0fba2adSLiam Girdwood *id = 0; 3828f0fba2adSLiam Girdwood } 3829f0fba2adSLiam Girdwood 3830f0fba2adSLiam Girdwood return kstrdup(name, GFP_KERNEL); 3831f0fba2adSLiam Girdwood } 3832f0fba2adSLiam Girdwood 3833f0fba2adSLiam Girdwood /* 3834f0fba2adSLiam Girdwood * Simplify DAI link naming for single devices with multiple DAIs by removing 3835f0fba2adSLiam Girdwood * any ".-1" and using the DAI name (instead of device name). 3836f0fba2adSLiam Girdwood */ 3837f0fba2adSLiam Girdwood static inline char *fmt_multiple_name(struct device *dev, 3838f0fba2adSLiam Girdwood struct snd_soc_dai_driver *dai_drv) 3839f0fba2adSLiam Girdwood { 3840f0fba2adSLiam Girdwood if (dai_drv->name == NULL) { 384110e8aa9aSMichał Mirosław dev_err(dev, 384210e8aa9aSMichał Mirosław "ASoC: error - multiple DAI %s registered with no name\n", 384310e8aa9aSMichał Mirosław dev_name(dev)); 3844f0fba2adSLiam Girdwood return NULL; 3845f0fba2adSLiam Girdwood } 3846f0fba2adSLiam Girdwood 3847f0fba2adSLiam Girdwood return kstrdup(dai_drv->name, GFP_KERNEL); 3848f0fba2adSLiam Girdwood } 3849f0fba2adSLiam Girdwood 38509115171aSMark Brown /** 385132c9ba54SLars-Peter Clausen * snd_soc_unregister_dai - Unregister DAIs from the ASoC core 38529115171aSMark Brown * 385332c9ba54SLars-Peter Clausen * @component: The component for which the DAIs should be unregistered 38549115171aSMark Brown */ 385532c9ba54SLars-Peter Clausen static void snd_soc_unregister_dais(struct snd_soc_component *component) 38569115171aSMark Brown { 38575c1d5f09SLars-Peter Clausen struct snd_soc_dai *dai, *_dai; 38589115171aSMark Brown 38595c1d5f09SLars-Peter Clausen list_for_each_entry_safe(dai, _dai, &component->dai_list, list) { 386032c9ba54SLars-Peter Clausen dev_dbg(component->dev, "ASoC: Unregistered DAI '%s'\n", 386132c9ba54SLars-Peter Clausen dai->name); 38629115171aSMark Brown list_del(&dai->list); 3863f0fba2adSLiam Girdwood kfree(dai->name); 3864f0fba2adSLiam Girdwood kfree(dai); 38659115171aSMark Brown } 386632c9ba54SLars-Peter Clausen } 38679115171aSMark Brown 38689115171aSMark Brown /** 386932c9ba54SLars-Peter Clausen * snd_soc_register_dais - Register a DAI with the ASoC core 38709115171aSMark Brown * 38716106d129SLars-Peter Clausen * @component: The component the DAIs are registered for 38726106d129SLars-Peter Clausen * @dai_drv: DAI driver to use for the DAIs 3873ac11a2b3SMark Brown * @count: Number of DAIs 387432c9ba54SLars-Peter Clausen * @legacy_dai_naming: Use the legacy naming scheme and let the DAI inherit the 387532c9ba54SLars-Peter Clausen * parent's name. 38769115171aSMark Brown */ 387732c9ba54SLars-Peter Clausen static int snd_soc_register_dais(struct snd_soc_component *component, 3878bb13109dSLars-Peter Clausen struct snd_soc_dai_driver *dai_drv, size_t count, 3879bb13109dSLars-Peter Clausen bool legacy_dai_naming) 38809115171aSMark Brown { 38816106d129SLars-Peter Clausen struct device *dev = component->dev; 3882f0fba2adSLiam Girdwood struct snd_soc_dai *dai; 388332c9ba54SLars-Peter Clausen unsigned int i; 388432c9ba54SLars-Peter Clausen int ret; 3885f0fba2adSLiam Girdwood 3886f110bfc7SLiam Girdwood dev_dbg(dev, "ASoC: dai register %s #%Zu\n", dev_name(dev), count); 38879115171aSMark Brown 3888bb13109dSLars-Peter Clausen component->dai_drv = dai_drv; 3889bb13109dSLars-Peter Clausen component->num_dai = count; 3890bb13109dSLars-Peter Clausen 38919115171aSMark Brown for (i = 0; i < count; i++) { 3892f0fba2adSLiam Girdwood 3893f0fba2adSLiam Girdwood dai = kzalloc(sizeof(struct snd_soc_dai), GFP_KERNEL); 3894c46e0079SAxel Lin if (dai == NULL) { 3895c46e0079SAxel Lin ret = -ENOMEM; 3896c46e0079SAxel Lin goto err; 3897c46e0079SAxel Lin } 3898f0fba2adSLiam Girdwood 389932c9ba54SLars-Peter Clausen /* 390032c9ba54SLars-Peter Clausen * Back in the old days when we still had component-less DAIs, 390132c9ba54SLars-Peter Clausen * instead of having a static name, component-less DAIs would 390232c9ba54SLars-Peter Clausen * inherit the name of the parent device so it is possible to 390332c9ba54SLars-Peter Clausen * register multiple instances of the DAI. We still need to keep 390432c9ba54SLars-Peter Clausen * the same naming style even though those DAIs are not 390532c9ba54SLars-Peter Clausen * component-less anymore. 390632c9ba54SLars-Peter Clausen */ 390732c9ba54SLars-Peter Clausen if (count == 1 && legacy_dai_naming) { 3908db2a4165SFrank Mandarino dai->name = fmt_single_name(dev, &dai->id); 390932c9ba54SLars-Peter Clausen } else { 3910f0fba2adSLiam Girdwood dai->name = fmt_multiple_name(dev, &dai_drv[i]); 391132c9ba54SLars-Peter Clausen if (dai_drv[i].id) 391232c9ba54SLars-Peter Clausen dai->id = dai_drv[i].id; 391332c9ba54SLars-Peter Clausen else 391432c9ba54SLars-Peter Clausen dai->id = i; 391532c9ba54SLars-Peter Clausen } 3916f0fba2adSLiam Girdwood if (dai->name == NULL) { 3917f0fba2adSLiam Girdwood kfree(dai); 391832c9ba54SLars-Peter Clausen ret = -ENOMEM; 39199115171aSMark Brown goto err; 39209115171aSMark Brown } 39219115171aSMark Brown 39226106d129SLars-Peter Clausen dai->component = component; 3923f0fba2adSLiam Girdwood dai->dev = dev; 3924f0fba2adSLiam Girdwood dai->driver = &dai_drv[i]; 3925f0fba2adSLiam Girdwood if (!dai->driver->ops) 3926f0fba2adSLiam Girdwood dai->driver->ops = &null_dai_ops; 3927f0fba2adSLiam Girdwood 39281438c2f6SLars-Peter Clausen list_add(&dai->list, &component->dai_list); 3929054880feSMark Brown 39309115171aSMark Brown dev_dbg(dev, "ASoC: Registered DAI '%s'\n", dai->name); 3931f0fba2adSLiam Girdwood } 3932f0fba2adSLiam Girdwood 39339115171aSMark Brown return 0; 39349115171aSMark Brown 39359115171aSMark Brown err: 393632c9ba54SLars-Peter Clausen snd_soc_unregister_dais(component); 39379115171aSMark Brown 39389115171aSMark Brown return ret; 39399115171aSMark Brown } 39409115171aSMark Brown 394114e8bdebSLars-Peter Clausen static void snd_soc_component_seq_notifier(struct snd_soc_dapm_context *dapm, 394214e8bdebSLars-Peter Clausen enum snd_soc_dapm_type type, int subseq) 3943d191bd8dSKuninori Morimoto { 394414e8bdebSLars-Peter Clausen struct snd_soc_component *component = dapm->component; 3945d191bd8dSKuninori Morimoto 394614e8bdebSLars-Peter Clausen component->driver->seq_notifier(component, type, subseq); 394714e8bdebSLars-Peter Clausen } 3948d191bd8dSKuninori Morimoto 394914e8bdebSLars-Peter Clausen static int snd_soc_component_stream_event(struct snd_soc_dapm_context *dapm, 395014e8bdebSLars-Peter Clausen int event) 395114e8bdebSLars-Peter Clausen { 395214e8bdebSLars-Peter Clausen struct snd_soc_component *component = dapm->component; 395314e8bdebSLars-Peter Clausen 395414e8bdebSLars-Peter Clausen return component->driver->stream_event(component, event); 395514e8bdebSLars-Peter Clausen } 395614e8bdebSLars-Peter Clausen 3957bb13109dSLars-Peter Clausen static int snd_soc_component_initialize(struct snd_soc_component *component, 3958bb13109dSLars-Peter Clausen const struct snd_soc_component_driver *driver, struct device *dev) 3959d191bd8dSKuninori Morimoto { 3960ce0fc93aSLars-Peter Clausen struct snd_soc_dapm_context *dapm; 3961ce0fc93aSLars-Peter Clausen 3962bb13109dSLars-Peter Clausen component->name = fmt_single_name(dev, &component->id); 3963bb13109dSLars-Peter Clausen if (!component->name) { 3964bb13109dSLars-Peter Clausen dev_err(dev, "ASoC: Failed to allocate name\n"); 3965d191bd8dSKuninori Morimoto return -ENOMEM; 3966d191bd8dSKuninori Morimoto } 3967d191bd8dSKuninori Morimoto 3968bb13109dSLars-Peter Clausen component->dev = dev; 3969bb13109dSLars-Peter Clausen component->driver = driver; 397061aca564SLars-Peter Clausen component->probe = component->driver->probe; 397161aca564SLars-Peter Clausen component->remove = component->driver->remove; 3972e2c330b9SLars-Peter Clausen 3973ce0fc93aSLars-Peter Clausen if (!component->dapm_ptr) 3974ce0fc93aSLars-Peter Clausen component->dapm_ptr = &component->dapm; 3975ce0fc93aSLars-Peter Clausen 3976ce0fc93aSLars-Peter Clausen dapm = component->dapm_ptr; 3977ce0fc93aSLars-Peter Clausen dapm->dev = dev; 3978ce0fc93aSLars-Peter Clausen dapm->component = component; 3979ce0fc93aSLars-Peter Clausen dapm->bias_level = SND_SOC_BIAS_OFF; 39805819c2faSLars-Peter Clausen dapm->idle_bias_off = true; 398114e8bdebSLars-Peter Clausen if (driver->seq_notifier) 398214e8bdebSLars-Peter Clausen dapm->seq_notifier = snd_soc_component_seq_notifier; 398314e8bdebSLars-Peter Clausen if (driver->stream_event) 398414e8bdebSLars-Peter Clausen dapm->stream_event = snd_soc_component_stream_event; 3985ce0fc93aSLars-Peter Clausen 398661aca564SLars-Peter Clausen component->controls = driver->controls; 398761aca564SLars-Peter Clausen component->num_controls = driver->num_controls; 398861aca564SLars-Peter Clausen component->dapm_widgets = driver->dapm_widgets; 398961aca564SLars-Peter Clausen component->num_dapm_widgets = driver->num_dapm_widgets; 399061aca564SLars-Peter Clausen component->dapm_routes = driver->dapm_routes; 399161aca564SLars-Peter Clausen component->num_dapm_routes = driver->num_dapm_routes; 399261aca564SLars-Peter Clausen 3993bb13109dSLars-Peter Clausen INIT_LIST_HEAD(&component->dai_list); 3994bb13109dSLars-Peter Clausen mutex_init(&component->io_mutex); 3995bb13109dSLars-Peter Clausen 3996bb13109dSLars-Peter Clausen return 0; 3997d191bd8dSKuninori Morimoto } 3998d191bd8dSKuninori Morimoto 3999886f5692SLars-Peter Clausen static void snd_soc_component_init_regmap(struct snd_soc_component *component) 4000886f5692SLars-Peter Clausen { 4001886f5692SLars-Peter Clausen if (!component->regmap) 4002886f5692SLars-Peter Clausen component->regmap = dev_get_regmap(component->dev, NULL); 4003886f5692SLars-Peter Clausen if (component->regmap) { 4004886f5692SLars-Peter Clausen int val_bytes = regmap_get_val_bytes(component->regmap); 4005886f5692SLars-Peter Clausen /* Errors are legitimate for non-integer byte multiples */ 4006886f5692SLars-Peter Clausen if (val_bytes > 0) 4007886f5692SLars-Peter Clausen component->val_bytes = val_bytes; 4008886f5692SLars-Peter Clausen } 4009886f5692SLars-Peter Clausen } 4010886f5692SLars-Peter Clausen 4011bb13109dSLars-Peter Clausen static void snd_soc_component_add_unlocked(struct snd_soc_component *component) 4012bb13109dSLars-Peter Clausen { 4013886f5692SLars-Peter Clausen if (!component->write && !component->read) 4014886f5692SLars-Peter Clausen snd_soc_component_init_regmap(component); 4015886f5692SLars-Peter Clausen 4016bb13109dSLars-Peter Clausen list_add(&component->list, &component_list); 4017d191bd8dSKuninori Morimoto } 4018d191bd8dSKuninori Morimoto 4019bb13109dSLars-Peter Clausen static void snd_soc_component_add(struct snd_soc_component *component) 4020bb13109dSLars-Peter Clausen { 4021d191bd8dSKuninori Morimoto mutex_lock(&client_mutex); 4022bb13109dSLars-Peter Clausen snd_soc_component_add_unlocked(component); 4023d191bd8dSKuninori Morimoto mutex_unlock(&client_mutex); 4024bb13109dSLars-Peter Clausen } 4025d191bd8dSKuninori Morimoto 4026bb13109dSLars-Peter Clausen static void snd_soc_component_cleanup(struct snd_soc_component *component) 4027bb13109dSLars-Peter Clausen { 4028bb13109dSLars-Peter Clausen snd_soc_unregister_dais(component); 4029bb13109dSLars-Peter Clausen kfree(component->name); 4030bb13109dSLars-Peter Clausen } 4031d191bd8dSKuninori Morimoto 4032bb13109dSLars-Peter Clausen static void snd_soc_component_del_unlocked(struct snd_soc_component *component) 4033bb13109dSLars-Peter Clausen { 4034bb13109dSLars-Peter Clausen list_del(&component->list); 4035bb13109dSLars-Peter Clausen } 4036d191bd8dSKuninori Morimoto 4037bb13109dSLars-Peter Clausen static void snd_soc_component_del(struct snd_soc_component *component) 4038bb13109dSLars-Peter Clausen { 4039bb13109dSLars-Peter Clausen mutex_lock(&client_mutex); 4040bb13109dSLars-Peter Clausen snd_soc_component_del_unlocked(component); 4041bb13109dSLars-Peter Clausen mutex_unlock(&client_mutex); 4042d191bd8dSKuninori Morimoto } 4043d191bd8dSKuninori Morimoto 4044d191bd8dSKuninori Morimoto int snd_soc_register_component(struct device *dev, 4045d191bd8dSKuninori Morimoto const struct snd_soc_component_driver *cmpnt_drv, 4046d191bd8dSKuninori Morimoto struct snd_soc_dai_driver *dai_drv, 4047d191bd8dSKuninori Morimoto int num_dai) 4048d191bd8dSKuninori Morimoto { 4049d191bd8dSKuninori Morimoto struct snd_soc_component *cmpnt; 4050bb13109dSLars-Peter Clausen int ret; 4051d191bd8dSKuninori Morimoto 4052bb13109dSLars-Peter Clausen cmpnt = kzalloc(sizeof(*cmpnt), GFP_KERNEL); 4053d191bd8dSKuninori Morimoto if (!cmpnt) { 4054d191bd8dSKuninori Morimoto dev_err(dev, "ASoC: Failed to allocate memory\n"); 4055d191bd8dSKuninori Morimoto return -ENOMEM; 4056d191bd8dSKuninori Morimoto } 4057d191bd8dSKuninori Morimoto 4058bb13109dSLars-Peter Clausen ret = snd_soc_component_initialize(cmpnt, cmpnt_drv, dev); 4059bb13109dSLars-Peter Clausen if (ret) 4060bb13109dSLars-Peter Clausen goto err_free; 4061bb13109dSLars-Peter Clausen 40623d59400fSLars-Peter Clausen cmpnt->ignore_pmdown_time = true; 406398e639fbSLars-Peter Clausen cmpnt->registered_as_component = true; 40643d59400fSLars-Peter Clausen 4065bb13109dSLars-Peter Clausen ret = snd_soc_register_dais(cmpnt, dai_drv, num_dai, true); 4066bb13109dSLars-Peter Clausen if (ret < 0) { 4067bb13109dSLars-Peter Clausen dev_err(dev, "ASoC: Failed to regster DAIs: %d\n", ret); 4068bb13109dSLars-Peter Clausen goto err_cleanup; 4069bb13109dSLars-Peter Clausen } 4070bb13109dSLars-Peter Clausen 4071bb13109dSLars-Peter Clausen snd_soc_component_add(cmpnt); 4072bb13109dSLars-Peter Clausen 4073bb13109dSLars-Peter Clausen return 0; 4074bb13109dSLars-Peter Clausen 4075bb13109dSLars-Peter Clausen err_cleanup: 4076bb13109dSLars-Peter Clausen snd_soc_component_cleanup(cmpnt); 4077bb13109dSLars-Peter Clausen err_free: 4078bb13109dSLars-Peter Clausen kfree(cmpnt); 4079bb13109dSLars-Peter Clausen return ret; 4080d191bd8dSKuninori Morimoto } 4081d191bd8dSKuninori Morimoto EXPORT_SYMBOL_GPL(snd_soc_register_component); 4082d191bd8dSKuninori Morimoto 4083d191bd8dSKuninori Morimoto /** 4084d191bd8dSKuninori Morimoto * snd_soc_unregister_component - Unregister a component from the ASoC core 4085d191bd8dSKuninori Morimoto * 4086d191bd8dSKuninori Morimoto */ 4087d191bd8dSKuninori Morimoto void snd_soc_unregister_component(struct device *dev) 4088d191bd8dSKuninori Morimoto { 4089d191bd8dSKuninori Morimoto struct snd_soc_component *cmpnt; 4090d191bd8dSKuninori Morimoto 4091d191bd8dSKuninori Morimoto list_for_each_entry(cmpnt, &component_list, list) { 409298e639fbSLars-Peter Clausen if (dev == cmpnt->dev && cmpnt->registered_as_component) 4093d191bd8dSKuninori Morimoto goto found; 4094d191bd8dSKuninori Morimoto } 4095d191bd8dSKuninori Morimoto return; 4096d191bd8dSKuninori Morimoto 4097d191bd8dSKuninori Morimoto found: 4098bb13109dSLars-Peter Clausen snd_soc_component_del(cmpnt); 4099bb13109dSLars-Peter Clausen snd_soc_component_cleanup(cmpnt); 4100bb13109dSLars-Peter Clausen kfree(cmpnt); 4101d191bd8dSKuninori Morimoto } 4102d191bd8dSKuninori Morimoto EXPORT_SYMBOL_GPL(snd_soc_unregister_component); 4103d191bd8dSKuninori Morimoto 4104f1d45cc3SLars-Peter Clausen static int snd_soc_platform_drv_probe(struct snd_soc_component *component) 4105e2c330b9SLars-Peter Clausen { 4106e2c330b9SLars-Peter Clausen struct snd_soc_platform *platform = snd_soc_component_to_platform(component); 4107e2c330b9SLars-Peter Clausen 4108f1d45cc3SLars-Peter Clausen return platform->driver->probe(platform); 4109e2c330b9SLars-Peter Clausen } 4110e2c330b9SLars-Peter Clausen 4111f1d45cc3SLars-Peter Clausen static void snd_soc_platform_drv_remove(struct snd_soc_component *component) 4112e2c330b9SLars-Peter Clausen { 4113e2c330b9SLars-Peter Clausen struct snd_soc_platform *platform = snd_soc_component_to_platform(component); 4114e2c330b9SLars-Peter Clausen 4115f1d45cc3SLars-Peter Clausen platform->driver->remove(platform); 4116e2c330b9SLars-Peter Clausen } 4117e2c330b9SLars-Peter Clausen 4118d191bd8dSKuninori Morimoto /** 411971a45cdaSLars-Peter Clausen * snd_soc_add_platform - Add a platform to the ASoC core 412071a45cdaSLars-Peter Clausen * @dev: The parent device for the platform 412171a45cdaSLars-Peter Clausen * @platform: The platform to add 412271a45cdaSLars-Peter Clausen * @platform_driver: The driver for the platform 412312a48a8cSMark Brown */ 412471a45cdaSLars-Peter Clausen int snd_soc_add_platform(struct device *dev, struct snd_soc_platform *platform, 4125d79e57dbSLars-Peter Clausen const struct snd_soc_platform_driver *platform_drv) 412612a48a8cSMark Brown { 4127b37f1d12SLars-Peter Clausen int ret; 4128b37f1d12SLars-Peter Clausen 4129bb13109dSLars-Peter Clausen ret = snd_soc_component_initialize(&platform->component, 4130bb13109dSLars-Peter Clausen &platform_drv->component_driver, dev); 4131bb13109dSLars-Peter Clausen if (ret) 4132bb13109dSLars-Peter Clausen return ret; 4133f0fba2adSLiam Girdwood 4134f0fba2adSLiam Girdwood platform->dev = dev; 4135f0fba2adSLiam Girdwood platform->driver = platform_drv; 4136f1d45cc3SLars-Peter Clausen 4137f1d45cc3SLars-Peter Clausen if (platform_drv->probe) 4138f1d45cc3SLars-Peter Clausen platform->component.probe = snd_soc_platform_drv_probe; 4139f1d45cc3SLars-Peter Clausen if (platform_drv->remove) 4140f1d45cc3SLars-Peter Clausen platform->component.remove = snd_soc_platform_drv_remove; 414112a48a8cSMark Brown 414281c7cfd1SLars-Peter Clausen #ifdef CONFIG_DEBUG_FS 414381c7cfd1SLars-Peter Clausen platform->component.debugfs_prefix = "platform"; 414481c7cfd1SLars-Peter Clausen #endif 414512a48a8cSMark Brown 414612a48a8cSMark Brown mutex_lock(&client_mutex); 4147bb13109dSLars-Peter Clausen snd_soc_component_add_unlocked(&platform->component); 414812a48a8cSMark Brown list_add(&platform->list, &platform_list); 414912a48a8cSMark Brown mutex_unlock(&client_mutex); 415012a48a8cSMark Brown 4151f4333203SLars-Peter Clausen dev_dbg(dev, "ASoC: Registered platform '%s'\n", 4152f4333203SLars-Peter Clausen platform->component.name); 415312a48a8cSMark Brown 415412a48a8cSMark Brown return 0; 415512a48a8cSMark Brown } 415671a45cdaSLars-Peter Clausen EXPORT_SYMBOL_GPL(snd_soc_add_platform); 415771a45cdaSLars-Peter Clausen 415871a45cdaSLars-Peter Clausen /** 415971a45cdaSLars-Peter Clausen * snd_soc_register_platform - Register a platform with the ASoC core 416071a45cdaSLars-Peter Clausen * 416171a45cdaSLars-Peter Clausen * @platform: platform to register 416271a45cdaSLars-Peter Clausen */ 416371a45cdaSLars-Peter Clausen int snd_soc_register_platform(struct device *dev, 416471a45cdaSLars-Peter Clausen const struct snd_soc_platform_driver *platform_drv) 416571a45cdaSLars-Peter Clausen { 416671a45cdaSLars-Peter Clausen struct snd_soc_platform *platform; 416771a45cdaSLars-Peter Clausen int ret; 416871a45cdaSLars-Peter Clausen 416971a45cdaSLars-Peter Clausen dev_dbg(dev, "ASoC: platform register %s\n", dev_name(dev)); 417071a45cdaSLars-Peter Clausen 417171a45cdaSLars-Peter Clausen platform = kzalloc(sizeof(struct snd_soc_platform), GFP_KERNEL); 417271a45cdaSLars-Peter Clausen if (platform == NULL) 417371a45cdaSLars-Peter Clausen return -ENOMEM; 417471a45cdaSLars-Peter Clausen 417571a45cdaSLars-Peter Clausen ret = snd_soc_add_platform(dev, platform, platform_drv); 417671a45cdaSLars-Peter Clausen if (ret) 417771a45cdaSLars-Peter Clausen kfree(platform); 417871a45cdaSLars-Peter Clausen 417971a45cdaSLars-Peter Clausen return ret; 418071a45cdaSLars-Peter Clausen } 418112a48a8cSMark Brown EXPORT_SYMBOL_GPL(snd_soc_register_platform); 418212a48a8cSMark Brown 418312a48a8cSMark Brown /** 418471a45cdaSLars-Peter Clausen * snd_soc_remove_platform - Remove a platform from the ASoC core 418571a45cdaSLars-Peter Clausen * @platform: the platform to remove 418671a45cdaSLars-Peter Clausen */ 418771a45cdaSLars-Peter Clausen void snd_soc_remove_platform(struct snd_soc_platform *platform) 418871a45cdaSLars-Peter Clausen { 4189b37f1d12SLars-Peter Clausen 419071a45cdaSLars-Peter Clausen mutex_lock(&client_mutex); 419171a45cdaSLars-Peter Clausen list_del(&platform->list); 4192bb13109dSLars-Peter Clausen snd_soc_component_del_unlocked(&platform->component); 419371a45cdaSLars-Peter Clausen mutex_unlock(&client_mutex); 419471a45cdaSLars-Peter Clausen 419571a45cdaSLars-Peter Clausen dev_dbg(platform->dev, "ASoC: Unregistered platform '%s'\n", 4196f4333203SLars-Peter Clausen platform->component.name); 4197decc27b0SDaniel Mack 4198decc27b0SDaniel Mack snd_soc_component_cleanup(&platform->component); 419971a45cdaSLars-Peter Clausen } 420071a45cdaSLars-Peter Clausen EXPORT_SYMBOL_GPL(snd_soc_remove_platform); 420171a45cdaSLars-Peter Clausen 420271a45cdaSLars-Peter Clausen struct snd_soc_platform *snd_soc_lookup_platform(struct device *dev) 420371a45cdaSLars-Peter Clausen { 420471a45cdaSLars-Peter Clausen struct snd_soc_platform *platform; 420571a45cdaSLars-Peter Clausen 420671a45cdaSLars-Peter Clausen list_for_each_entry(platform, &platform_list, list) { 420771a45cdaSLars-Peter Clausen if (dev == platform->dev) 420871a45cdaSLars-Peter Clausen return platform; 420971a45cdaSLars-Peter Clausen } 421071a45cdaSLars-Peter Clausen 421171a45cdaSLars-Peter Clausen return NULL; 421271a45cdaSLars-Peter Clausen } 421371a45cdaSLars-Peter Clausen EXPORT_SYMBOL_GPL(snd_soc_lookup_platform); 421471a45cdaSLars-Peter Clausen 421571a45cdaSLars-Peter Clausen /** 421612a48a8cSMark Brown * snd_soc_unregister_platform - Unregister a platform from the ASoC core 421712a48a8cSMark Brown * 4218ac11a2b3SMark Brown * @platform: platform to unregister 421912a48a8cSMark Brown */ 4220f0fba2adSLiam Girdwood void snd_soc_unregister_platform(struct device *dev) 422112a48a8cSMark Brown { 4222f0fba2adSLiam Girdwood struct snd_soc_platform *platform; 4223f0fba2adSLiam Girdwood 422471a45cdaSLars-Peter Clausen platform = snd_soc_lookup_platform(dev); 422571a45cdaSLars-Peter Clausen if (!platform) 4226f0fba2adSLiam Girdwood return; 4227f0fba2adSLiam Girdwood 422871a45cdaSLars-Peter Clausen snd_soc_remove_platform(platform); 4229f0fba2adSLiam Girdwood kfree(platform); 423012a48a8cSMark Brown } 423112a48a8cSMark Brown EXPORT_SYMBOL_GPL(snd_soc_unregister_platform); 423212a48a8cSMark Brown 4233151ab22cSMark Brown static u64 codec_format_map[] = { 4234151ab22cSMark Brown SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S16_BE, 4235151ab22cSMark Brown SNDRV_PCM_FMTBIT_U16_LE | SNDRV_PCM_FMTBIT_U16_BE, 4236151ab22cSMark Brown SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S24_BE, 4237151ab22cSMark Brown SNDRV_PCM_FMTBIT_U24_LE | SNDRV_PCM_FMTBIT_U24_BE, 4238151ab22cSMark Brown SNDRV_PCM_FMTBIT_S32_LE | SNDRV_PCM_FMTBIT_S32_BE, 4239151ab22cSMark Brown SNDRV_PCM_FMTBIT_U32_LE | SNDRV_PCM_FMTBIT_U32_BE, 4240151ab22cSMark Brown SNDRV_PCM_FMTBIT_S24_3LE | SNDRV_PCM_FMTBIT_U24_3BE, 4241151ab22cSMark Brown SNDRV_PCM_FMTBIT_U24_3LE | SNDRV_PCM_FMTBIT_U24_3BE, 4242151ab22cSMark Brown SNDRV_PCM_FMTBIT_S20_3LE | SNDRV_PCM_FMTBIT_S20_3BE, 4243151ab22cSMark Brown SNDRV_PCM_FMTBIT_U20_3LE | SNDRV_PCM_FMTBIT_U20_3BE, 4244151ab22cSMark Brown SNDRV_PCM_FMTBIT_S18_3LE | SNDRV_PCM_FMTBIT_S18_3BE, 4245151ab22cSMark Brown SNDRV_PCM_FMTBIT_U18_3LE | SNDRV_PCM_FMTBIT_U18_3BE, 4246151ab22cSMark Brown SNDRV_PCM_FMTBIT_FLOAT_LE | SNDRV_PCM_FMTBIT_FLOAT_BE, 4247151ab22cSMark Brown SNDRV_PCM_FMTBIT_FLOAT64_LE | SNDRV_PCM_FMTBIT_FLOAT64_BE, 4248151ab22cSMark Brown SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_LE 4249151ab22cSMark Brown | SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_BE, 4250151ab22cSMark Brown }; 4251151ab22cSMark Brown 4252151ab22cSMark Brown /* Fix up the DAI formats for endianness: codecs don't actually see 4253151ab22cSMark Brown * the endianness of the data but we're using the CPU format 4254151ab22cSMark Brown * definitions which do need to include endianness so we ensure that 4255151ab22cSMark Brown * codec DAIs always have both big and little endian variants set. 4256151ab22cSMark Brown */ 4257151ab22cSMark Brown static void fixup_codec_formats(struct snd_soc_pcm_stream *stream) 4258151ab22cSMark Brown { 4259151ab22cSMark Brown int i; 4260151ab22cSMark Brown 4261151ab22cSMark Brown for (i = 0; i < ARRAY_SIZE(codec_format_map); i++) 4262151ab22cSMark Brown if (stream->formats & codec_format_map[i]) 4263151ab22cSMark Brown stream->formats |= codec_format_map[i]; 4264151ab22cSMark Brown } 4265151ab22cSMark Brown 4266f1d45cc3SLars-Peter Clausen static int snd_soc_codec_drv_probe(struct snd_soc_component *component) 4267f1d45cc3SLars-Peter Clausen { 4268f1d45cc3SLars-Peter Clausen struct snd_soc_codec *codec = snd_soc_component_to_codec(component); 4269f1d45cc3SLars-Peter Clausen 4270f1d45cc3SLars-Peter Clausen return codec->driver->probe(codec); 4271f1d45cc3SLars-Peter Clausen } 4272f1d45cc3SLars-Peter Clausen 4273f1d45cc3SLars-Peter Clausen static void snd_soc_codec_drv_remove(struct snd_soc_component *component) 4274f1d45cc3SLars-Peter Clausen { 4275f1d45cc3SLars-Peter Clausen struct snd_soc_codec *codec = snd_soc_component_to_codec(component); 4276f1d45cc3SLars-Peter Clausen 4277f1d45cc3SLars-Peter Clausen codec->driver->remove(codec); 4278f1d45cc3SLars-Peter Clausen } 4279f1d45cc3SLars-Peter Clausen 4280e2c330b9SLars-Peter Clausen static int snd_soc_codec_drv_write(struct snd_soc_component *component, 4281e2c330b9SLars-Peter Clausen unsigned int reg, unsigned int val) 4282e2c330b9SLars-Peter Clausen { 4283e2c330b9SLars-Peter Clausen struct snd_soc_codec *codec = snd_soc_component_to_codec(component); 4284e2c330b9SLars-Peter Clausen 4285e2c330b9SLars-Peter Clausen return codec->driver->write(codec, reg, val); 4286e2c330b9SLars-Peter Clausen } 4287e2c330b9SLars-Peter Clausen 4288e2c330b9SLars-Peter Clausen static int snd_soc_codec_drv_read(struct snd_soc_component *component, 4289e2c330b9SLars-Peter Clausen unsigned int reg, unsigned int *val) 4290e2c330b9SLars-Peter Clausen { 4291e2c330b9SLars-Peter Clausen struct snd_soc_codec *codec = snd_soc_component_to_codec(component); 4292e2c330b9SLars-Peter Clausen 4293e2c330b9SLars-Peter Clausen *val = codec->driver->read(codec, reg); 4294e2c330b9SLars-Peter Clausen 4295e2c330b9SLars-Peter Clausen return 0; 4296e2c330b9SLars-Peter Clausen } 4297e2c330b9SLars-Peter Clausen 429868f831c2SLars-Peter Clausen static int snd_soc_codec_set_bias_level(struct snd_soc_dapm_context *dapm, 429968f831c2SLars-Peter Clausen enum snd_soc_bias_level level) 430068f831c2SLars-Peter Clausen { 430168f831c2SLars-Peter Clausen struct snd_soc_codec *codec = snd_soc_dapm_to_codec(dapm); 430268f831c2SLars-Peter Clausen 430368f831c2SLars-Peter Clausen return codec->driver->set_bias_level(codec, level); 430468f831c2SLars-Peter Clausen } 430568f831c2SLars-Peter Clausen 43060d0cf00aSMark Brown /** 43070d0cf00aSMark Brown * snd_soc_register_codec - Register a codec with the ASoC core 43080d0cf00aSMark Brown * 4309ac11a2b3SMark Brown * @codec: codec to register 43100d0cf00aSMark Brown */ 4311f0fba2adSLiam Girdwood int snd_soc_register_codec(struct device *dev, 4312001ae4c0SMark Brown const struct snd_soc_codec_driver *codec_drv, 4313001ae4c0SMark Brown struct snd_soc_dai_driver *dai_drv, 4314001ae4c0SMark Brown int num_dai) 43150d0cf00aSMark Brown { 4316f0fba2adSLiam Girdwood struct snd_soc_codec *codec; 4317bb13109dSLars-Peter Clausen struct snd_soc_dai *dai; 4318f0fba2adSLiam Girdwood int ret, i; 4319151ab22cSMark Brown 4320f0fba2adSLiam Girdwood dev_dbg(dev, "codec register %s\n", dev_name(dev)); 43210d0cf00aSMark Brown 4322f0fba2adSLiam Girdwood codec = kzalloc(sizeof(struct snd_soc_codec), GFP_KERNEL); 4323f0fba2adSLiam Girdwood if (codec == NULL) 4324f0fba2adSLiam Girdwood return -ENOMEM; 43250d0cf00aSMark Brown 4326ce0fc93aSLars-Peter Clausen codec->component.dapm_ptr = &codec->dapm; 4327f1d45cc3SLars-Peter Clausen codec->component.codec = codec; 4328ce0fc93aSLars-Peter Clausen 4329bb13109dSLars-Peter Clausen ret = snd_soc_component_initialize(&codec->component, 4330bb13109dSLars-Peter Clausen &codec_drv->component_driver, dev); 4331bb13109dSLars-Peter Clausen if (ret) 4332bb13109dSLars-Peter Clausen goto err_free; 4333151ab22cSMark Brown 4334f1d45cc3SLars-Peter Clausen if (codec_drv->controls) { 4335f1d45cc3SLars-Peter Clausen codec->component.controls = codec_drv->controls; 4336f1d45cc3SLars-Peter Clausen codec->component.num_controls = codec_drv->num_controls; 4337f1d45cc3SLars-Peter Clausen } 4338f1d45cc3SLars-Peter Clausen if (codec_drv->dapm_widgets) { 4339f1d45cc3SLars-Peter Clausen codec->component.dapm_widgets = codec_drv->dapm_widgets; 4340f1d45cc3SLars-Peter Clausen codec->component.num_dapm_widgets = codec_drv->num_dapm_widgets; 4341f1d45cc3SLars-Peter Clausen } 4342f1d45cc3SLars-Peter Clausen if (codec_drv->dapm_routes) { 4343f1d45cc3SLars-Peter Clausen codec->component.dapm_routes = codec_drv->dapm_routes; 4344f1d45cc3SLars-Peter Clausen codec->component.num_dapm_routes = codec_drv->num_dapm_routes; 4345f1d45cc3SLars-Peter Clausen } 4346f1d45cc3SLars-Peter Clausen 4347f1d45cc3SLars-Peter Clausen if (codec_drv->probe) 4348f1d45cc3SLars-Peter Clausen codec->component.probe = snd_soc_codec_drv_probe; 4349f1d45cc3SLars-Peter Clausen if (codec_drv->remove) 4350f1d45cc3SLars-Peter Clausen codec->component.remove = snd_soc_codec_drv_remove; 4351e2c330b9SLars-Peter Clausen if (codec_drv->write) 4352e2c330b9SLars-Peter Clausen codec->component.write = snd_soc_codec_drv_write; 4353e2c330b9SLars-Peter Clausen if (codec_drv->read) 4354e2c330b9SLars-Peter Clausen codec->component.read = snd_soc_codec_drv_read; 43553d59400fSLars-Peter Clausen codec->component.ignore_pmdown_time = codec_drv->ignore_pmdown_time; 43565819c2faSLars-Peter Clausen codec->dapm.idle_bias_off = codec_drv->idle_bias_off; 435786dbf2acSLars-Peter Clausen codec->dapm.suspend_bias_off = codec_drv->suspend_bias_off; 435814e8bdebSLars-Peter Clausen if (codec_drv->seq_notifier) 4359474b62d6SMark Brown codec->dapm.seq_notifier = codec_drv->seq_notifier; 436068f831c2SLars-Peter Clausen if (codec_drv->set_bias_level) 436168f831c2SLars-Peter Clausen codec->dapm.set_bias_level = snd_soc_codec_set_bias_level; 4362f0fba2adSLiam Girdwood codec->dev = dev; 4363f0fba2adSLiam Girdwood codec->driver = codec_drv; 4364e2c330b9SLars-Peter Clausen codec->component.val_bytes = codec_drv->reg_word_size; 4365f0fba2adSLiam Girdwood mutex_init(&codec->mutex); 4366f0fba2adSLiam Girdwood 436781c7cfd1SLars-Peter Clausen #ifdef CONFIG_DEBUG_FS 436881c7cfd1SLars-Peter Clausen codec->component.init_debugfs = soc_init_codec_debugfs; 436981c7cfd1SLars-Peter Clausen codec->component.debugfs_prefix = "codec"; 437081c7cfd1SLars-Peter Clausen #endif 4371a39f75f7SXiubo Li 4372a39f75f7SXiubo Li if (codec_drv->get_regmap) 4373886f5692SLars-Peter Clausen codec->component.regmap = codec_drv->get_regmap(dev); 4374a39f75f7SXiubo Li 4375f0fba2adSLiam Girdwood for (i = 0; i < num_dai; i++) { 4376f0fba2adSLiam Girdwood fixup_codec_formats(&dai_drv[i].playback); 4377f0fba2adSLiam Girdwood fixup_codec_formats(&dai_drv[i].capture); 4378f0fba2adSLiam Girdwood } 4379f0fba2adSLiam Girdwood 4380bb13109dSLars-Peter Clausen ret = snd_soc_register_dais(&codec->component, dai_drv, num_dai, false); 4381bb13109dSLars-Peter Clausen if (ret < 0) { 4382bb13109dSLars-Peter Clausen dev_err(dev, "ASoC: Failed to regster DAIs: %d\n", ret); 4383bb13109dSLars-Peter Clausen goto err_cleanup; 4384bb13109dSLars-Peter Clausen } 4385bb13109dSLars-Peter Clausen 4386bb13109dSLars-Peter Clausen list_for_each_entry(dai, &codec->component.dai_list, list) 4387bb13109dSLars-Peter Clausen dai->codec = codec; 4388bb13109dSLars-Peter Clausen 4389054880feSMark Brown mutex_lock(&client_mutex); 4390bb13109dSLars-Peter Clausen snd_soc_component_add_unlocked(&codec->component); 4391054880feSMark Brown list_add(&codec->list, &codec_list); 4392054880feSMark Brown mutex_unlock(&client_mutex); 4393054880feSMark Brown 4394f4333203SLars-Peter Clausen dev_dbg(codec->dev, "ASoC: Registered codec '%s'\n", 4395f4333203SLars-Peter Clausen codec->component.name); 43960d0cf00aSMark Brown return 0; 4397f0fba2adSLiam Girdwood 4398bb13109dSLars-Peter Clausen err_cleanup: 4399bb13109dSLars-Peter Clausen snd_soc_component_cleanup(&codec->component); 4400bb13109dSLars-Peter Clausen err_free: 4401f0fba2adSLiam Girdwood kfree(codec); 4402f0fba2adSLiam Girdwood return ret; 44030d0cf00aSMark Brown } 44040d0cf00aSMark Brown EXPORT_SYMBOL_GPL(snd_soc_register_codec); 44050d0cf00aSMark Brown 44060d0cf00aSMark Brown /** 44070d0cf00aSMark Brown * snd_soc_unregister_codec - Unregister a codec from the ASoC core 44080d0cf00aSMark Brown * 4409ac11a2b3SMark Brown * @codec: codec to unregister 44100d0cf00aSMark Brown */ 4411f0fba2adSLiam Girdwood void snd_soc_unregister_codec(struct device *dev) 44120d0cf00aSMark Brown { 4413f0fba2adSLiam Girdwood struct snd_soc_codec *codec; 4414f0fba2adSLiam Girdwood 4415f0fba2adSLiam Girdwood list_for_each_entry(codec, &codec_list, list) { 4416f0fba2adSLiam Girdwood if (dev == codec->dev) 4417f0fba2adSLiam Girdwood goto found; 4418f0fba2adSLiam Girdwood } 4419f0fba2adSLiam Girdwood return; 4420f0fba2adSLiam Girdwood 4421f0fba2adSLiam Girdwood found: 4422f0fba2adSLiam Girdwood 44230d0cf00aSMark Brown mutex_lock(&client_mutex); 44240d0cf00aSMark Brown list_del(&codec->list); 4425bb13109dSLars-Peter Clausen snd_soc_component_del_unlocked(&codec->component); 44260d0cf00aSMark Brown mutex_unlock(&client_mutex); 44270d0cf00aSMark Brown 4428f4333203SLars-Peter Clausen dev_dbg(codec->dev, "ASoC: Unregistered codec '%s'\n", 4429f4333203SLars-Peter Clausen codec->component.name); 4430f0fba2adSLiam Girdwood 4431bb13109dSLars-Peter Clausen snd_soc_component_cleanup(&codec->component); 44327a30a3dbSDimitris Papastamos snd_soc_cache_exit(codec); 4433f0fba2adSLiam Girdwood kfree(codec); 44340d0cf00aSMark Brown } 44350d0cf00aSMark Brown EXPORT_SYMBOL_GPL(snd_soc_unregister_codec); 44360d0cf00aSMark Brown 4437bec4fa05SStephen Warren /* Retrieve a card's name from device tree */ 4438bec4fa05SStephen Warren int snd_soc_of_parse_card_name(struct snd_soc_card *card, 4439bec4fa05SStephen Warren const char *propname) 4440bec4fa05SStephen Warren { 44417e07e7c0STushar Behera struct device_node *np; 4442bec4fa05SStephen Warren int ret; 4443bec4fa05SStephen Warren 44447e07e7c0STushar Behera if (!card->dev) { 44457e07e7c0STushar Behera pr_err("card->dev is not set before calling %s\n", __func__); 44467e07e7c0STushar Behera return -EINVAL; 44477e07e7c0STushar Behera } 44487e07e7c0STushar Behera 44497e07e7c0STushar Behera np = card->dev->of_node; 44507e07e7c0STushar Behera 4451bec4fa05SStephen Warren ret = of_property_read_string_index(np, propname, 0, &card->name); 4452bec4fa05SStephen Warren /* 4453bec4fa05SStephen Warren * EINVAL means the property does not exist. This is fine providing 4454bec4fa05SStephen Warren * card->name was previously set, which is checked later in 4455bec4fa05SStephen Warren * snd_soc_register_card. 4456bec4fa05SStephen Warren */ 4457bec4fa05SStephen Warren if (ret < 0 && ret != -EINVAL) { 4458bec4fa05SStephen Warren dev_err(card->dev, 4459f110bfc7SLiam Girdwood "ASoC: Property '%s' could not be read: %d\n", 4460bec4fa05SStephen Warren propname, ret); 4461bec4fa05SStephen Warren return ret; 4462bec4fa05SStephen Warren } 4463bec4fa05SStephen Warren 4464bec4fa05SStephen Warren return 0; 4465bec4fa05SStephen Warren } 4466bec4fa05SStephen Warren EXPORT_SYMBOL_GPL(snd_soc_of_parse_card_name); 4467bec4fa05SStephen Warren 44689a6d4860SXiubo Li static const struct snd_soc_dapm_widget simple_widgets[] = { 44699a6d4860SXiubo Li SND_SOC_DAPM_MIC("Microphone", NULL), 44709a6d4860SXiubo Li SND_SOC_DAPM_LINE("Line", NULL), 44719a6d4860SXiubo Li SND_SOC_DAPM_HP("Headphone", NULL), 44729a6d4860SXiubo Li SND_SOC_DAPM_SPK("Speaker", NULL), 44739a6d4860SXiubo Li }; 44749a6d4860SXiubo Li 44759a6d4860SXiubo Li int snd_soc_of_parse_audio_simple_widgets(struct snd_soc_card *card, 44769a6d4860SXiubo Li const char *propname) 44779a6d4860SXiubo Li { 44789a6d4860SXiubo Li struct device_node *np = card->dev->of_node; 44799a6d4860SXiubo Li struct snd_soc_dapm_widget *widgets; 44809a6d4860SXiubo Li const char *template, *wname; 44819a6d4860SXiubo Li int i, j, num_widgets, ret; 44829a6d4860SXiubo Li 44839a6d4860SXiubo Li num_widgets = of_property_count_strings(np, propname); 44849a6d4860SXiubo Li if (num_widgets < 0) { 44859a6d4860SXiubo Li dev_err(card->dev, 44869a6d4860SXiubo Li "ASoC: Property '%s' does not exist\n", propname); 44879a6d4860SXiubo Li return -EINVAL; 44889a6d4860SXiubo Li } 44899a6d4860SXiubo Li if (num_widgets & 1) { 44909a6d4860SXiubo Li dev_err(card->dev, 44919a6d4860SXiubo Li "ASoC: Property '%s' length is not even\n", propname); 44929a6d4860SXiubo Li return -EINVAL; 44939a6d4860SXiubo Li } 44949a6d4860SXiubo Li 44959a6d4860SXiubo Li num_widgets /= 2; 44969a6d4860SXiubo Li if (!num_widgets) { 44979a6d4860SXiubo Li dev_err(card->dev, "ASoC: Property '%s's length is zero\n", 44989a6d4860SXiubo Li propname); 44999a6d4860SXiubo Li return -EINVAL; 45009a6d4860SXiubo Li } 45019a6d4860SXiubo Li 45029a6d4860SXiubo Li widgets = devm_kcalloc(card->dev, num_widgets, sizeof(*widgets), 45039a6d4860SXiubo Li GFP_KERNEL); 45049a6d4860SXiubo Li if (!widgets) { 45059a6d4860SXiubo Li dev_err(card->dev, 45069a6d4860SXiubo Li "ASoC: Could not allocate memory for widgets\n"); 45079a6d4860SXiubo Li return -ENOMEM; 45089a6d4860SXiubo Li } 45099a6d4860SXiubo Li 45109a6d4860SXiubo Li for (i = 0; i < num_widgets; i++) { 45119a6d4860SXiubo Li ret = of_property_read_string_index(np, propname, 45129a6d4860SXiubo Li 2 * i, &template); 45139a6d4860SXiubo Li if (ret) { 45149a6d4860SXiubo Li dev_err(card->dev, 45159a6d4860SXiubo Li "ASoC: Property '%s' index %d read error:%d\n", 45169a6d4860SXiubo Li propname, 2 * i, ret); 45179a6d4860SXiubo Li return -EINVAL; 45189a6d4860SXiubo Li } 45199a6d4860SXiubo Li 45209a6d4860SXiubo Li for (j = 0; j < ARRAY_SIZE(simple_widgets); j++) { 45219a6d4860SXiubo Li if (!strncmp(template, simple_widgets[j].name, 45229a6d4860SXiubo Li strlen(simple_widgets[j].name))) { 45239a6d4860SXiubo Li widgets[i] = simple_widgets[j]; 45249a6d4860SXiubo Li break; 45259a6d4860SXiubo Li } 45269a6d4860SXiubo Li } 45279a6d4860SXiubo Li 45289a6d4860SXiubo Li if (j >= ARRAY_SIZE(simple_widgets)) { 45299a6d4860SXiubo Li dev_err(card->dev, 45309a6d4860SXiubo Li "ASoC: DAPM widget '%s' is not supported\n", 45319a6d4860SXiubo Li template); 45329a6d4860SXiubo Li return -EINVAL; 45339a6d4860SXiubo Li } 45349a6d4860SXiubo Li 45359a6d4860SXiubo Li ret = of_property_read_string_index(np, propname, 45369a6d4860SXiubo Li (2 * i) + 1, 45379a6d4860SXiubo Li &wname); 45389a6d4860SXiubo Li if (ret) { 45399a6d4860SXiubo Li dev_err(card->dev, 45409a6d4860SXiubo Li "ASoC: Property '%s' index %d read error:%d\n", 45419a6d4860SXiubo Li propname, (2 * i) + 1, ret); 45429a6d4860SXiubo Li return -EINVAL; 45439a6d4860SXiubo Li } 45449a6d4860SXiubo Li 45459a6d4860SXiubo Li widgets[i].name = wname; 45469a6d4860SXiubo Li } 45479a6d4860SXiubo Li 45489a6d4860SXiubo Li card->dapm_widgets = widgets; 45499a6d4860SXiubo Li card->num_dapm_widgets = num_widgets; 45509a6d4860SXiubo Li 45519a6d4860SXiubo Li return 0; 45529a6d4860SXiubo Li } 45539a6d4860SXiubo Li EXPORT_SYMBOL_GPL(snd_soc_of_parse_audio_simple_widgets); 45549a6d4860SXiubo Li 455589c67857SXiubo Li int snd_soc_of_parse_tdm_slot(struct device_node *np, 455689c67857SXiubo Li unsigned int *slots, 455789c67857SXiubo Li unsigned int *slot_width) 455889c67857SXiubo Li { 455989c67857SXiubo Li u32 val; 456089c67857SXiubo Li int ret; 456189c67857SXiubo Li 456289c67857SXiubo Li if (of_property_read_bool(np, "dai-tdm-slot-num")) { 456389c67857SXiubo Li ret = of_property_read_u32(np, "dai-tdm-slot-num", &val); 456489c67857SXiubo Li if (ret) 456589c67857SXiubo Li return ret; 456689c67857SXiubo Li 456789c67857SXiubo Li if (slots) 456889c67857SXiubo Li *slots = val; 456989c67857SXiubo Li } 457089c67857SXiubo Li 457189c67857SXiubo Li if (of_property_read_bool(np, "dai-tdm-slot-width")) { 457289c67857SXiubo Li ret = of_property_read_u32(np, "dai-tdm-slot-width", &val); 457389c67857SXiubo Li if (ret) 457489c67857SXiubo Li return ret; 457589c67857SXiubo Li 457689c67857SXiubo Li if (slot_width) 457789c67857SXiubo Li *slot_width = val; 457889c67857SXiubo Li } 457989c67857SXiubo Li 458089c67857SXiubo Li return 0; 458189c67857SXiubo Li } 458289c67857SXiubo Li EXPORT_SYMBOL_GPL(snd_soc_of_parse_tdm_slot); 458389c67857SXiubo Li 4584a4a54dd5SStephen Warren int snd_soc_of_parse_audio_routing(struct snd_soc_card *card, 4585a4a54dd5SStephen Warren const char *propname) 4586a4a54dd5SStephen Warren { 4587a4a54dd5SStephen Warren struct device_node *np = card->dev->of_node; 4588a4a54dd5SStephen Warren int num_routes; 4589a4a54dd5SStephen Warren struct snd_soc_dapm_route *routes; 4590a4a54dd5SStephen Warren int i, ret; 4591a4a54dd5SStephen Warren 4592a4a54dd5SStephen Warren num_routes = of_property_count_strings(np, propname); 4593c34ce320SRichard Zhao if (num_routes < 0 || num_routes & 1) { 459410e8aa9aSMichał Mirosław dev_err(card->dev, 459510e8aa9aSMichał Mirosław "ASoC: Property '%s' does not exist or its length is not even\n", 459610e8aa9aSMichał Mirosław propname); 4597a4a54dd5SStephen Warren return -EINVAL; 4598a4a54dd5SStephen Warren } 4599a4a54dd5SStephen Warren num_routes /= 2; 4600a4a54dd5SStephen Warren if (!num_routes) { 4601f110bfc7SLiam Girdwood dev_err(card->dev, "ASoC: Property '%s's length is zero\n", 4602a4a54dd5SStephen Warren propname); 4603a4a54dd5SStephen Warren return -EINVAL; 4604a4a54dd5SStephen Warren } 4605a4a54dd5SStephen Warren 4606a4a54dd5SStephen Warren routes = devm_kzalloc(card->dev, num_routes * sizeof(*routes), 4607a4a54dd5SStephen Warren GFP_KERNEL); 4608a4a54dd5SStephen Warren if (!routes) { 4609a4a54dd5SStephen Warren dev_err(card->dev, 4610f110bfc7SLiam Girdwood "ASoC: Could not allocate DAPM route table\n"); 4611a4a54dd5SStephen Warren return -EINVAL; 4612a4a54dd5SStephen Warren } 4613a4a54dd5SStephen Warren 4614a4a54dd5SStephen Warren for (i = 0; i < num_routes; i++) { 4615a4a54dd5SStephen Warren ret = of_property_read_string_index(np, propname, 4616a4a54dd5SStephen Warren 2 * i, &routes[i].sink); 4617a4a54dd5SStephen Warren if (ret) { 4618c871bd0bSMark Brown dev_err(card->dev, 4619c871bd0bSMark Brown "ASoC: Property '%s' index %d could not be read: %d\n", 4620c871bd0bSMark Brown propname, 2 * i, ret); 4621a4a54dd5SStephen Warren return -EINVAL; 4622a4a54dd5SStephen Warren } 4623a4a54dd5SStephen Warren ret = of_property_read_string_index(np, propname, 4624a4a54dd5SStephen Warren (2 * i) + 1, &routes[i].source); 4625a4a54dd5SStephen Warren if (ret) { 4626a4a54dd5SStephen Warren dev_err(card->dev, 4627c871bd0bSMark Brown "ASoC: Property '%s' index %d could not be read: %d\n", 4628c871bd0bSMark Brown propname, (2 * i) + 1, ret); 4629a4a54dd5SStephen Warren return -EINVAL; 4630a4a54dd5SStephen Warren } 4631a4a54dd5SStephen Warren } 4632a4a54dd5SStephen Warren 4633a4a54dd5SStephen Warren card->num_dapm_routes = num_routes; 4634a4a54dd5SStephen Warren card->dapm_routes = routes; 4635a4a54dd5SStephen Warren 4636a4a54dd5SStephen Warren return 0; 4637a4a54dd5SStephen Warren } 4638a4a54dd5SStephen Warren EXPORT_SYMBOL_GPL(snd_soc_of_parse_audio_routing); 4639a4a54dd5SStephen Warren 4640a7930ed4SKuninori Morimoto unsigned int snd_soc_of_parse_daifmt(struct device_node *np, 4641389cb834SJyri Sarha const char *prefix, 4642389cb834SJyri Sarha struct device_node **bitclkmaster, 4643389cb834SJyri Sarha struct device_node **framemaster) 4644a7930ed4SKuninori Morimoto { 4645a7930ed4SKuninori Morimoto int ret, i; 4646a7930ed4SKuninori Morimoto char prop[128]; 4647a7930ed4SKuninori Morimoto unsigned int format = 0; 4648a7930ed4SKuninori Morimoto int bit, frame; 4649a7930ed4SKuninori Morimoto const char *str; 4650a7930ed4SKuninori Morimoto struct { 4651a7930ed4SKuninori Morimoto char *name; 4652a7930ed4SKuninori Morimoto unsigned int val; 4653a7930ed4SKuninori Morimoto } of_fmt_table[] = { 4654a7930ed4SKuninori Morimoto { "i2s", SND_SOC_DAIFMT_I2S }, 4655a7930ed4SKuninori Morimoto { "right_j", SND_SOC_DAIFMT_RIGHT_J }, 4656a7930ed4SKuninori Morimoto { "left_j", SND_SOC_DAIFMT_LEFT_J }, 4657a7930ed4SKuninori Morimoto { "dsp_a", SND_SOC_DAIFMT_DSP_A }, 4658a7930ed4SKuninori Morimoto { "dsp_b", SND_SOC_DAIFMT_DSP_B }, 4659a7930ed4SKuninori Morimoto { "ac97", SND_SOC_DAIFMT_AC97 }, 4660a7930ed4SKuninori Morimoto { "pdm", SND_SOC_DAIFMT_PDM}, 4661a7930ed4SKuninori Morimoto { "msb", SND_SOC_DAIFMT_MSB }, 4662a7930ed4SKuninori Morimoto { "lsb", SND_SOC_DAIFMT_LSB }, 4663a7930ed4SKuninori Morimoto }; 4664a7930ed4SKuninori Morimoto 4665a7930ed4SKuninori Morimoto if (!prefix) 4666a7930ed4SKuninori Morimoto prefix = ""; 4667a7930ed4SKuninori Morimoto 4668a7930ed4SKuninori Morimoto /* 4669a7930ed4SKuninori Morimoto * check "[prefix]format = xxx" 4670a7930ed4SKuninori Morimoto * SND_SOC_DAIFMT_FORMAT_MASK area 4671a7930ed4SKuninori Morimoto */ 4672a7930ed4SKuninori Morimoto snprintf(prop, sizeof(prop), "%sformat", prefix); 4673a7930ed4SKuninori Morimoto ret = of_property_read_string(np, prop, &str); 4674a7930ed4SKuninori Morimoto if (ret == 0) { 4675a7930ed4SKuninori Morimoto for (i = 0; i < ARRAY_SIZE(of_fmt_table); i++) { 4676a7930ed4SKuninori Morimoto if (strcmp(str, of_fmt_table[i].name) == 0) { 4677a7930ed4SKuninori Morimoto format |= of_fmt_table[i].val; 4678a7930ed4SKuninori Morimoto break; 4679a7930ed4SKuninori Morimoto } 4680a7930ed4SKuninori Morimoto } 4681a7930ed4SKuninori Morimoto } 4682a7930ed4SKuninori Morimoto 4683a7930ed4SKuninori Morimoto /* 46848c2d6a9fSKuninori Morimoto * check "[prefix]continuous-clock" 4685a7930ed4SKuninori Morimoto * SND_SOC_DAIFMT_CLOCK_MASK area 4686a7930ed4SKuninori Morimoto */ 46878c2d6a9fSKuninori Morimoto snprintf(prop, sizeof(prop), "%scontinuous-clock", prefix); 46888c2d6a9fSKuninori Morimoto if (of_get_property(np, prop, NULL)) 46898c2d6a9fSKuninori Morimoto format |= SND_SOC_DAIFMT_CONT; 46908c2d6a9fSKuninori Morimoto else 46918c2d6a9fSKuninori Morimoto format |= SND_SOC_DAIFMT_GATED; 4692a7930ed4SKuninori Morimoto 4693a7930ed4SKuninori Morimoto /* 4694a7930ed4SKuninori Morimoto * check "[prefix]bitclock-inversion" 4695a7930ed4SKuninori Morimoto * check "[prefix]frame-inversion" 4696a7930ed4SKuninori Morimoto * SND_SOC_DAIFMT_INV_MASK area 4697a7930ed4SKuninori Morimoto */ 4698a7930ed4SKuninori Morimoto snprintf(prop, sizeof(prop), "%sbitclock-inversion", prefix); 4699a7930ed4SKuninori Morimoto bit = !!of_get_property(np, prop, NULL); 4700a7930ed4SKuninori Morimoto 4701a7930ed4SKuninori Morimoto snprintf(prop, sizeof(prop), "%sframe-inversion", prefix); 4702a7930ed4SKuninori Morimoto frame = !!of_get_property(np, prop, NULL); 4703a7930ed4SKuninori Morimoto 4704a7930ed4SKuninori Morimoto switch ((bit << 4) + frame) { 4705a7930ed4SKuninori Morimoto case 0x11: 4706a7930ed4SKuninori Morimoto format |= SND_SOC_DAIFMT_IB_IF; 4707a7930ed4SKuninori Morimoto break; 4708a7930ed4SKuninori Morimoto case 0x10: 4709a7930ed4SKuninori Morimoto format |= SND_SOC_DAIFMT_IB_NF; 4710a7930ed4SKuninori Morimoto break; 4711a7930ed4SKuninori Morimoto case 0x01: 4712a7930ed4SKuninori Morimoto format |= SND_SOC_DAIFMT_NB_IF; 4713a7930ed4SKuninori Morimoto break; 4714a7930ed4SKuninori Morimoto default: 4715a7930ed4SKuninori Morimoto /* SND_SOC_DAIFMT_NB_NF is default */ 4716a7930ed4SKuninori Morimoto break; 4717a7930ed4SKuninori Morimoto } 4718a7930ed4SKuninori Morimoto 4719a7930ed4SKuninori Morimoto /* 4720a7930ed4SKuninori Morimoto * check "[prefix]bitclock-master" 4721a7930ed4SKuninori Morimoto * check "[prefix]frame-master" 4722a7930ed4SKuninori Morimoto * SND_SOC_DAIFMT_MASTER_MASK area 4723a7930ed4SKuninori Morimoto */ 4724a7930ed4SKuninori Morimoto snprintf(prop, sizeof(prop), "%sbitclock-master", prefix); 4725a7930ed4SKuninori Morimoto bit = !!of_get_property(np, prop, NULL); 4726389cb834SJyri Sarha if (bit && bitclkmaster) 4727389cb834SJyri Sarha *bitclkmaster = of_parse_phandle(np, prop, 0); 4728a7930ed4SKuninori Morimoto 4729a7930ed4SKuninori Morimoto snprintf(prop, sizeof(prop), "%sframe-master", prefix); 4730a7930ed4SKuninori Morimoto frame = !!of_get_property(np, prop, NULL); 4731389cb834SJyri Sarha if (frame && framemaster) 4732389cb834SJyri Sarha *framemaster = of_parse_phandle(np, prop, 0); 4733a7930ed4SKuninori Morimoto 4734a7930ed4SKuninori Morimoto switch ((bit << 4) + frame) { 4735a7930ed4SKuninori Morimoto case 0x11: 4736a7930ed4SKuninori Morimoto format |= SND_SOC_DAIFMT_CBM_CFM; 4737a7930ed4SKuninori Morimoto break; 4738a7930ed4SKuninori Morimoto case 0x10: 4739a7930ed4SKuninori Morimoto format |= SND_SOC_DAIFMT_CBM_CFS; 4740a7930ed4SKuninori Morimoto break; 4741a7930ed4SKuninori Morimoto case 0x01: 4742a7930ed4SKuninori Morimoto format |= SND_SOC_DAIFMT_CBS_CFM; 4743a7930ed4SKuninori Morimoto break; 4744a7930ed4SKuninori Morimoto default: 4745a7930ed4SKuninori Morimoto format |= SND_SOC_DAIFMT_CBS_CFS; 4746a7930ed4SKuninori Morimoto break; 4747a7930ed4SKuninori Morimoto } 4748a7930ed4SKuninori Morimoto 4749a7930ed4SKuninori Morimoto return format; 4750a7930ed4SKuninori Morimoto } 4751a7930ed4SKuninori Morimoto EXPORT_SYMBOL_GPL(snd_soc_of_parse_daifmt); 4752a7930ed4SKuninori Morimoto 4753cb470087SKuninori Morimoto int snd_soc_of_get_dai_name(struct device_node *of_node, 4754cb470087SKuninori Morimoto const char **dai_name) 4755cb470087SKuninori Morimoto { 4756cb470087SKuninori Morimoto struct snd_soc_component *pos; 4757cb470087SKuninori Morimoto struct of_phandle_args args; 4758cb470087SKuninori Morimoto int ret; 4759cb470087SKuninori Morimoto 4760cb470087SKuninori Morimoto ret = of_parse_phandle_with_args(of_node, "sound-dai", 4761cb470087SKuninori Morimoto "#sound-dai-cells", 0, &args); 4762cb470087SKuninori Morimoto if (ret) 4763cb470087SKuninori Morimoto return ret; 4764cb470087SKuninori Morimoto 4765cb470087SKuninori Morimoto ret = -EPROBE_DEFER; 4766cb470087SKuninori Morimoto 4767cb470087SKuninori Morimoto mutex_lock(&client_mutex); 4768cb470087SKuninori Morimoto list_for_each_entry(pos, &component_list, list) { 4769cb470087SKuninori Morimoto if (pos->dev->of_node != args.np) 4770cb470087SKuninori Morimoto continue; 4771cb470087SKuninori Morimoto 47726833c452SKuninori Morimoto if (pos->driver->of_xlate_dai_name) { 47736833c452SKuninori Morimoto ret = pos->driver->of_xlate_dai_name(pos, &args, dai_name); 47746833c452SKuninori Morimoto } else { 47756833c452SKuninori Morimoto int id = -1; 47766833c452SKuninori Morimoto 47776833c452SKuninori Morimoto switch (args.args_count) { 47786833c452SKuninori Morimoto case 0: 47796833c452SKuninori Morimoto id = 0; /* same as dai_drv[0] */ 47806833c452SKuninori Morimoto break; 47816833c452SKuninori Morimoto case 1: 47826833c452SKuninori Morimoto id = args.args[0]; 47836833c452SKuninori Morimoto break; 47846833c452SKuninori Morimoto default: 47856833c452SKuninori Morimoto /* not supported */ 4786cb470087SKuninori Morimoto break; 4787cb470087SKuninori Morimoto } 4788cb470087SKuninori Morimoto 47896833c452SKuninori Morimoto if (id < 0 || id >= pos->num_dai) { 47906833c452SKuninori Morimoto ret = -EINVAL; 47913dcba280SNicolin Chen continue; 47926833c452SKuninori Morimoto } 4793e41975edSXiubo Li 4794e41975edSXiubo Li ret = 0; 4795e41975edSXiubo Li 4796e41975edSXiubo Li *dai_name = pos->dai_drv[id].name; 4797e41975edSXiubo Li if (!*dai_name) 4798e41975edSXiubo Li *dai_name = pos->name; 47996833c452SKuninori Morimoto } 48006833c452SKuninori Morimoto 4801cb470087SKuninori Morimoto break; 4802cb470087SKuninori Morimoto } 4803cb470087SKuninori Morimoto mutex_unlock(&client_mutex); 4804cb470087SKuninori Morimoto 4805cb470087SKuninori Morimoto of_node_put(args.np); 4806cb470087SKuninori Morimoto 4807cb470087SKuninori Morimoto return ret; 4808cb470087SKuninori Morimoto } 4809cb470087SKuninori Morimoto EXPORT_SYMBOL_GPL(snd_soc_of_get_dai_name); 4810cb470087SKuninori Morimoto 4811c9b3a40fSTakashi Iwai static int __init snd_soc_init(void) 4812db2a4165SFrank Mandarino { 4813db2a4165SFrank Mandarino #ifdef CONFIG_DEBUG_FS 48148a9dab1aSMark Brown snd_soc_debugfs_root = debugfs_create_dir("asoc", NULL); 48158a9dab1aSMark Brown if (IS_ERR(snd_soc_debugfs_root) || !snd_soc_debugfs_root) { 48160837fc62SFabio Estevam pr_warn("ASoC: Failed to create debugfs directory\n"); 48178a9dab1aSMark Brown snd_soc_debugfs_root = NULL; 4818db2a4165SFrank Mandarino } 4819c3c5a19aSMark Brown 48208a9dab1aSMark Brown if (!debugfs_create_file("codecs", 0444, snd_soc_debugfs_root, NULL, 4821c3c5a19aSMark Brown &codec_list_fops)) 4822c3c5a19aSMark Brown pr_warn("ASoC: Failed to create CODEC list debugfs file\n"); 4823c3c5a19aSMark Brown 48248a9dab1aSMark Brown if (!debugfs_create_file("dais", 0444, snd_soc_debugfs_root, NULL, 4825f3208780SMark Brown &dai_list_fops)) 4826f3208780SMark Brown pr_warn("ASoC: Failed to create DAI list debugfs file\n"); 482719c7ac27SMark Brown 48288a9dab1aSMark Brown if (!debugfs_create_file("platforms", 0444, snd_soc_debugfs_root, NULL, 482919c7ac27SMark Brown &platform_list_fops)) 483019c7ac27SMark Brown pr_warn("ASoC: Failed to create platform list debugfs file\n"); 4831db2a4165SFrank Mandarino #endif 4832db2a4165SFrank Mandarino 4833fb257897SMark Brown snd_soc_util_init(); 4834fb257897SMark Brown 4835db2a4165SFrank Mandarino return platform_driver_register(&soc_driver); 4836db2a4165SFrank Mandarino } 48374abe8e16SMark Brown module_init(snd_soc_init); 4838db2a4165SFrank Mandarino 4839db2a4165SFrank Mandarino static void __exit snd_soc_exit(void) 4840db2a4165SFrank Mandarino { 4841fb257897SMark Brown snd_soc_util_exit(); 4842fb257897SMark Brown 4843db2a4165SFrank Mandarino #ifdef CONFIG_DEBUG_FS 48448a9dab1aSMark Brown debugfs_remove_recursive(snd_soc_debugfs_root); 4845db2a4165SFrank Mandarino #endif 4846db2a4165SFrank Mandarino platform_driver_unregister(&soc_driver); 4847db2a4165SFrank Mandarino } 4848db2a4165SFrank Mandarino module_exit(snd_soc_exit); 4849db2a4165SFrank Mandarino 4850db2a4165SFrank Mandarino /* Module information */ 4851db2a4165SFrank Mandarino MODULE_AUTHOR("Liam Girdwood, lrg@slimlogic.co.uk"); 4852db2a4165SFrank Mandarino MODULE_DESCRIPTION("ALSA SoC Core"); 4853db2a4165SFrank Mandarino MODULE_LICENSE("GPL"); 4854db2a4165SFrank Mandarino MODULE_ALIAS("platform:soc-audio"); 4855