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); 599115171aSMark Brown static LIST_HEAD(dai_list); 6012a48a8cSMark Brown static LIST_HEAD(platform_list); 610d0cf00aSMark Brown static LIST_HEAD(codec_list); 62030e79f6SKuninori Morimoto static LIST_HEAD(component_list); 63c5af3a2eSMark Brown 64db2a4165SFrank Mandarino /* 65db2a4165SFrank Mandarino * This is a timeout to do a DAPM powerdown after a stream is closed(). 66db2a4165SFrank Mandarino * It can be used to eliminate pops between different playback streams, e.g. 67db2a4165SFrank Mandarino * between two audio tracks. 68db2a4165SFrank Mandarino */ 69db2a4165SFrank Mandarino static int pmdown_time = 5000; 70db2a4165SFrank Mandarino module_param(pmdown_time, int, 0); 71db2a4165SFrank Mandarino MODULE_PARM_DESC(pmdown_time, "DAPM stream powerdown time (msecs)"); 72db2a4165SFrank Mandarino 73741a509fSMarkus Pargmann struct snd_ac97_reset_cfg { 74741a509fSMarkus Pargmann struct pinctrl *pctl; 75741a509fSMarkus Pargmann struct pinctrl_state *pstate_reset; 76741a509fSMarkus Pargmann struct pinctrl_state *pstate_warm_reset; 77741a509fSMarkus Pargmann struct pinctrl_state *pstate_run; 78741a509fSMarkus Pargmann int gpio_sdata; 79741a509fSMarkus Pargmann int gpio_sync; 80741a509fSMarkus Pargmann int gpio_reset; 81741a509fSMarkus Pargmann }; 82741a509fSMarkus Pargmann 832bc9a81eSDimitris Papastamos /* returns the minimum number of bytes needed to represent 842bc9a81eSDimitris Papastamos * a particular given value */ 852bc9a81eSDimitris Papastamos static int min_bytes_needed(unsigned long val) 862bc9a81eSDimitris Papastamos { 872bc9a81eSDimitris Papastamos int c = 0; 882bc9a81eSDimitris Papastamos int i; 892bc9a81eSDimitris Papastamos 902bc9a81eSDimitris Papastamos for (i = (sizeof val * 8) - 1; i >= 0; --i, ++c) 912bc9a81eSDimitris Papastamos if (val & (1UL << i)) 922bc9a81eSDimitris Papastamos break; 932bc9a81eSDimitris Papastamos c = (sizeof val * 8) - c; 942bc9a81eSDimitris Papastamos if (!c || (c % 8)) 952bc9a81eSDimitris Papastamos c = (c + 8) / 8; 962bc9a81eSDimitris Papastamos else 972bc9a81eSDimitris Papastamos c /= 8; 982bc9a81eSDimitris Papastamos return c; 992bc9a81eSDimitris Papastamos } 1002bc9a81eSDimitris Papastamos 10113fd179fSDimitris Papastamos /* fill buf which is 'len' bytes with a formatted 10213fd179fSDimitris Papastamos * string of the form 'reg: value\n' */ 10313fd179fSDimitris Papastamos static int format_register_str(struct snd_soc_codec *codec, 10413fd179fSDimitris Papastamos unsigned int reg, char *buf, size_t len) 1052624d5faSMark Brown { 10600b317a4SStephen Warren int wordsize = min_bytes_needed(codec->driver->reg_cache_size) * 2; 10700b317a4SStephen Warren int regsize = codec->driver->reg_word_size * 2; 10813fd179fSDimitris Papastamos int ret; 10913fd179fSDimitris Papastamos char tmpbuf[len + 1]; 11013fd179fSDimitris Papastamos char regbuf[regsize + 1]; 11113fd179fSDimitris Papastamos 11213fd179fSDimitris Papastamos /* since tmpbuf is allocated on the stack, warn the callers if they 11313fd179fSDimitris Papastamos * try to abuse this function */ 11413fd179fSDimitris Papastamos WARN_ON(len > 63); 11513fd179fSDimitris Papastamos 11613fd179fSDimitris Papastamos /* +2 for ': ' and + 1 for '\n' */ 11713fd179fSDimitris Papastamos if (wordsize + regsize + 2 + 1 != len) 11813fd179fSDimitris Papastamos return -EINVAL; 11913fd179fSDimitris Papastamos 12013fd179fSDimitris Papastamos ret = snd_soc_read(codec, reg); 12113fd179fSDimitris Papastamos if (ret < 0) { 12213fd179fSDimitris Papastamos memset(regbuf, 'X', regsize); 12313fd179fSDimitris Papastamos regbuf[regsize] = '\0'; 12413fd179fSDimitris Papastamos } else { 12513fd179fSDimitris Papastamos snprintf(regbuf, regsize + 1, "%.*x", regsize, ret); 12613fd179fSDimitris Papastamos } 12713fd179fSDimitris Papastamos 12813fd179fSDimitris Papastamos /* prepare the buffer */ 12913fd179fSDimitris Papastamos snprintf(tmpbuf, len + 1, "%.*x: %s\n", wordsize, reg, regbuf); 13013fd179fSDimitris Papastamos /* copy it back to the caller without the '\0' */ 13113fd179fSDimitris Papastamos memcpy(buf, tmpbuf, len); 13213fd179fSDimitris Papastamos 13313fd179fSDimitris Papastamos return 0; 13413fd179fSDimitris Papastamos } 13513fd179fSDimitris Papastamos 13613fd179fSDimitris Papastamos /* codec register dump */ 13713fd179fSDimitris Papastamos static ssize_t soc_codec_reg_show(struct snd_soc_codec *codec, char *buf, 13813fd179fSDimitris Papastamos size_t count, loff_t pos) 13913fd179fSDimitris Papastamos { 14013fd179fSDimitris Papastamos int i, step = 1; 1412bc9a81eSDimitris Papastamos int wordsize, regsize; 14213fd179fSDimitris Papastamos int len; 14313fd179fSDimitris Papastamos size_t total = 0; 14413fd179fSDimitris Papastamos loff_t p = 0; 1452bc9a81eSDimitris Papastamos 14600b317a4SStephen Warren wordsize = min_bytes_needed(codec->driver->reg_cache_size) * 2; 14700b317a4SStephen Warren regsize = codec->driver->reg_word_size * 2; 1482624d5faSMark Brown 14913fd179fSDimitris Papastamos len = wordsize + regsize + 2 + 1; 15013fd179fSDimitris Papastamos 151f0fba2adSLiam Girdwood if (!codec->driver->reg_cache_size) 1522624d5faSMark Brown return 0; 1532624d5faSMark Brown 154f0fba2adSLiam Girdwood if (codec->driver->reg_cache_step) 155f0fba2adSLiam Girdwood step = codec->driver->reg_cache_step; 1562624d5faSMark Brown 157f0fba2adSLiam Girdwood for (i = 0; i < codec->driver->reg_cache_size; i += step) { 158b92d150bSLars-Peter Clausen if (!snd_soc_codec_readable_register(codec, i)) 1592624d5faSMark Brown continue; 160f0fba2adSLiam Girdwood if (codec->driver->display_register) { 161f0fba2adSLiam Girdwood count += codec->driver->display_register(codec, buf + count, 1622624d5faSMark Brown PAGE_SIZE - count, i); 1635164d74dSMark Brown } else { 16413fd179fSDimitris Papastamos /* only support larger than PAGE_SIZE bytes debugfs 16513fd179fSDimitris Papastamos * entries for the default case */ 16613fd179fSDimitris Papastamos if (p >= pos) { 16713fd179fSDimitris Papastamos if (total + len >= count - 1) 1682624d5faSMark Brown break; 16913fd179fSDimitris Papastamos format_register_str(codec, i, buf + total, len); 17013fd179fSDimitris Papastamos total += len; 17113fd179fSDimitris Papastamos } 17213fd179fSDimitris Papastamos p += len; 17313fd179fSDimitris Papastamos } 1742624d5faSMark Brown } 1752624d5faSMark Brown 17613fd179fSDimitris Papastamos total = min(total, count - 1); 1772624d5faSMark Brown 17813fd179fSDimitris Papastamos return total; 1792624d5faSMark Brown } 18013fd179fSDimitris Papastamos 1812624d5faSMark Brown static ssize_t codec_reg_show(struct device *dev, 1822624d5faSMark Brown struct device_attribute *attr, char *buf) 1832624d5faSMark Brown { 18436ae1a96SMark Brown struct snd_soc_pcm_runtime *rtd = dev_get_drvdata(dev); 185f0fba2adSLiam Girdwood 18613fd179fSDimitris Papastamos return soc_codec_reg_show(rtd->codec, buf, PAGE_SIZE, 0); 1872624d5faSMark Brown } 1882624d5faSMark Brown 1892624d5faSMark Brown static DEVICE_ATTR(codec_reg, 0444, codec_reg_show, NULL); 1902624d5faSMark Brown 191dbe21408SMark Brown static ssize_t pmdown_time_show(struct device *dev, 192dbe21408SMark Brown struct device_attribute *attr, char *buf) 193dbe21408SMark Brown { 19436ae1a96SMark Brown struct snd_soc_pcm_runtime *rtd = dev_get_drvdata(dev); 195dbe21408SMark Brown 196f0fba2adSLiam Girdwood return sprintf(buf, "%ld\n", rtd->pmdown_time); 197dbe21408SMark Brown } 198dbe21408SMark Brown 199dbe21408SMark Brown static ssize_t pmdown_time_set(struct device *dev, 200dbe21408SMark Brown struct device_attribute *attr, 201dbe21408SMark Brown const char *buf, size_t count) 202dbe21408SMark Brown { 20336ae1a96SMark Brown struct snd_soc_pcm_runtime *rtd = dev_get_drvdata(dev); 204c593b520SMark Brown int ret; 205dbe21408SMark Brown 206b785a492SJingoo Han ret = kstrtol(buf, 10, &rtd->pmdown_time); 207c593b520SMark Brown if (ret) 208c593b520SMark Brown return ret; 209dbe21408SMark Brown 210dbe21408SMark Brown return count; 211dbe21408SMark Brown } 212dbe21408SMark Brown 213dbe21408SMark Brown static DEVICE_ATTR(pmdown_time, 0644, pmdown_time_show, pmdown_time_set); 214dbe21408SMark Brown 2152624d5faSMark Brown #ifdef CONFIG_DEBUG_FS 2162624d5faSMark Brown static ssize_t codec_reg_read_file(struct file *file, char __user *user_buf, 2172624d5faSMark Brown size_t count, loff_t *ppos) 2182624d5faSMark Brown { 2192624d5faSMark Brown ssize_t ret; 2202624d5faSMark Brown struct snd_soc_codec *codec = file->private_data; 22113fd179fSDimitris Papastamos char *buf; 22213fd179fSDimitris Papastamos 22313fd179fSDimitris Papastamos if (*ppos < 0 || !count) 22413fd179fSDimitris Papastamos return -EINVAL; 22513fd179fSDimitris Papastamos 22613fd179fSDimitris Papastamos buf = kmalloc(count, GFP_KERNEL); 2272624d5faSMark Brown if (!buf) 2282624d5faSMark Brown return -ENOMEM; 22913fd179fSDimitris Papastamos 23013fd179fSDimitris Papastamos ret = soc_codec_reg_show(codec, buf, count, *ppos); 23113fd179fSDimitris Papastamos if (ret >= 0) { 23213fd179fSDimitris Papastamos if (copy_to_user(user_buf, buf, ret)) { 23313fd179fSDimitris Papastamos kfree(buf); 23413fd179fSDimitris Papastamos return -EFAULT; 23513fd179fSDimitris Papastamos } 23613fd179fSDimitris Papastamos *ppos += ret; 23713fd179fSDimitris Papastamos } 23813fd179fSDimitris Papastamos 2392624d5faSMark Brown kfree(buf); 2402624d5faSMark Brown return ret; 2412624d5faSMark Brown } 2422624d5faSMark Brown 2432624d5faSMark Brown static ssize_t codec_reg_write_file(struct file *file, 2442624d5faSMark Brown const char __user *user_buf, size_t count, loff_t *ppos) 2452624d5faSMark Brown { 2462624d5faSMark Brown char buf[32]; 24734e268d8SStephen Boyd size_t buf_size; 2482624d5faSMark Brown char *start = buf; 2492624d5faSMark Brown unsigned long reg, value; 2502624d5faSMark Brown struct snd_soc_codec *codec = file->private_data; 251b785a492SJingoo Han int ret; 2522624d5faSMark Brown 2532624d5faSMark Brown buf_size = min(count, (sizeof(buf)-1)); 2542624d5faSMark Brown if (copy_from_user(buf, user_buf, buf_size)) 2552624d5faSMark Brown return -EFAULT; 2562624d5faSMark Brown buf[buf_size] = 0; 2572624d5faSMark Brown 2582624d5faSMark Brown while (*start == ' ') 2592624d5faSMark Brown start++; 2602624d5faSMark Brown reg = simple_strtoul(start, &start, 16); 2612624d5faSMark Brown while (*start == ' ') 2622624d5faSMark Brown start++; 263b785a492SJingoo Han ret = kstrtoul(start, 16, &value); 264b785a492SJingoo Han if (ret) 265b785a492SJingoo Han return ret; 2660d51a9cbSMark Brown 2670d51a9cbSMark Brown /* Userspace has been fiddling around behind the kernel's back */ 268373d4d09SRusty Russell add_taint(TAINT_USER, LOCKDEP_NOW_UNRELIABLE); 2690d51a9cbSMark Brown 270e4f078d8SDimitris Papastamos snd_soc_write(codec, reg, value); 2712624d5faSMark Brown return buf_size; 2722624d5faSMark Brown } 2732624d5faSMark Brown 2742624d5faSMark Brown static const struct file_operations codec_reg_fops = { 275234e3405SStephen Boyd .open = simple_open, 2762624d5faSMark Brown .read = codec_reg_read_file, 2772624d5faSMark Brown .write = codec_reg_write_file, 2786038f373SArnd Bergmann .llseek = default_llseek, 2792624d5faSMark Brown }; 2802624d5faSMark Brown 2812624d5faSMark Brown static void soc_init_codec_debugfs(struct snd_soc_codec *codec) 2822624d5faSMark Brown { 283d6ce4cf3SJarkko Nikula struct dentry *debugfs_card_root = codec->card->debugfs_card_root; 284d6ce4cf3SJarkko Nikula 2856ba6c9c3SMark Brown codec->debugfs_codec_root = debugfs_create_dir(codec->name, 286d6ce4cf3SJarkko Nikula debugfs_card_root); 2872624d5faSMark Brown if (!codec->debugfs_codec_root) { 28810e8aa9aSMichał Mirosław dev_warn(codec->dev, 28910e8aa9aSMichał Mirosław "ASoC: Failed to create codec debugfs directory\n"); 2902624d5faSMark Brown return; 2912624d5faSMark Brown } 2922624d5faSMark Brown 293aaee8ef1SMark Brown debugfs_create_bool("cache_sync", 0444, codec->debugfs_codec_root, 294aaee8ef1SMark Brown &codec->cache_sync); 295aaee8ef1SMark Brown debugfs_create_bool("cache_only", 0444, codec->debugfs_codec_root, 296aaee8ef1SMark Brown &codec->cache_only); 297aaee8ef1SMark Brown 2982624d5faSMark Brown codec->debugfs_reg = debugfs_create_file("codec_reg", 0644, 2992624d5faSMark Brown codec->debugfs_codec_root, 3002624d5faSMark Brown codec, &codec_reg_fops); 3012624d5faSMark Brown if (!codec->debugfs_reg) 30210e8aa9aSMichał Mirosław dev_warn(codec->dev, 30310e8aa9aSMichał Mirosław "ASoC: Failed to create codec register debugfs file\n"); 3042624d5faSMark Brown 3058eecaf62SLars-Peter Clausen snd_soc_dapm_debugfs_init(&codec->dapm, codec->debugfs_codec_root); 3062624d5faSMark Brown } 3072624d5faSMark Brown 3082624d5faSMark Brown static void soc_cleanup_codec_debugfs(struct snd_soc_codec *codec) 3092624d5faSMark Brown { 3102624d5faSMark Brown debugfs_remove_recursive(codec->debugfs_codec_root); 3112624d5faSMark Brown } 3122624d5faSMark Brown 313731f1ab2SSebastien Guiriec static void soc_init_platform_debugfs(struct snd_soc_platform *platform) 314731f1ab2SSebastien Guiriec { 315731f1ab2SSebastien Guiriec struct dentry *debugfs_card_root = platform->card->debugfs_card_root; 316731f1ab2SSebastien Guiriec 317731f1ab2SSebastien Guiriec platform->debugfs_platform_root = debugfs_create_dir(platform->name, 318731f1ab2SSebastien Guiriec debugfs_card_root); 319731f1ab2SSebastien Guiriec if (!platform->debugfs_platform_root) { 320731f1ab2SSebastien Guiriec dev_warn(platform->dev, 321f110bfc7SLiam Girdwood "ASoC: Failed to create platform debugfs directory\n"); 322731f1ab2SSebastien Guiriec return; 323731f1ab2SSebastien Guiriec } 324731f1ab2SSebastien Guiriec 325731f1ab2SSebastien Guiriec snd_soc_dapm_debugfs_init(&platform->dapm, 326731f1ab2SSebastien Guiriec platform->debugfs_platform_root); 327731f1ab2SSebastien Guiriec } 328731f1ab2SSebastien Guiriec 329731f1ab2SSebastien Guiriec static void soc_cleanup_platform_debugfs(struct snd_soc_platform *platform) 330731f1ab2SSebastien Guiriec { 331731f1ab2SSebastien Guiriec debugfs_remove_recursive(platform->debugfs_platform_root); 332731f1ab2SSebastien Guiriec } 333731f1ab2SSebastien Guiriec 334c3c5a19aSMark Brown static ssize_t codec_list_read_file(struct file *file, char __user *user_buf, 335c3c5a19aSMark Brown size_t count, loff_t *ppos) 336c3c5a19aSMark Brown { 337c3c5a19aSMark Brown char *buf = kmalloc(PAGE_SIZE, GFP_KERNEL); 3382b194f9dSMark Brown ssize_t len, ret = 0; 339c3c5a19aSMark Brown struct snd_soc_codec *codec; 340c3c5a19aSMark Brown 341c3c5a19aSMark Brown if (!buf) 342c3c5a19aSMark Brown return -ENOMEM; 343c3c5a19aSMark Brown 3442b194f9dSMark Brown list_for_each_entry(codec, &codec_list, list) { 3452b194f9dSMark Brown len = snprintf(buf + ret, PAGE_SIZE - ret, "%s\n", 346c3c5a19aSMark Brown codec->name); 3472b194f9dSMark Brown if (len >= 0) 3482b194f9dSMark Brown ret += len; 3492b194f9dSMark Brown if (ret > PAGE_SIZE) { 3502b194f9dSMark Brown ret = PAGE_SIZE; 3512b194f9dSMark Brown break; 3522b194f9dSMark Brown } 3532b194f9dSMark Brown } 354c3c5a19aSMark Brown 355c3c5a19aSMark Brown if (ret >= 0) 356c3c5a19aSMark Brown ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret); 357c3c5a19aSMark Brown 358c3c5a19aSMark Brown kfree(buf); 359c3c5a19aSMark Brown 360c3c5a19aSMark Brown return ret; 361c3c5a19aSMark Brown } 362c3c5a19aSMark Brown 363c3c5a19aSMark Brown static const struct file_operations codec_list_fops = { 364c3c5a19aSMark Brown .read = codec_list_read_file, 365c3c5a19aSMark Brown .llseek = default_llseek,/* read accesses f_pos */ 366c3c5a19aSMark Brown }; 367c3c5a19aSMark Brown 368f3208780SMark Brown static ssize_t dai_list_read_file(struct file *file, char __user *user_buf, 369f3208780SMark Brown size_t count, loff_t *ppos) 370f3208780SMark Brown { 371f3208780SMark Brown char *buf = kmalloc(PAGE_SIZE, GFP_KERNEL); 3722b194f9dSMark Brown ssize_t len, ret = 0; 373f3208780SMark Brown struct snd_soc_dai *dai; 374f3208780SMark Brown 375f3208780SMark Brown if (!buf) 376f3208780SMark Brown return -ENOMEM; 377f3208780SMark Brown 3782b194f9dSMark Brown list_for_each_entry(dai, &dai_list, list) { 3792b194f9dSMark Brown len = snprintf(buf + ret, PAGE_SIZE - ret, "%s\n", dai->name); 3802b194f9dSMark Brown if (len >= 0) 3812b194f9dSMark Brown ret += len; 3822b194f9dSMark Brown if (ret > PAGE_SIZE) { 3832b194f9dSMark Brown ret = PAGE_SIZE; 3842b194f9dSMark Brown break; 3852b194f9dSMark Brown } 3862b194f9dSMark Brown } 387f3208780SMark Brown 388f3208780SMark Brown ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret); 389f3208780SMark Brown 390f3208780SMark Brown kfree(buf); 391f3208780SMark Brown 392f3208780SMark Brown return ret; 393f3208780SMark Brown } 394f3208780SMark Brown 395f3208780SMark Brown static const struct file_operations dai_list_fops = { 396f3208780SMark Brown .read = dai_list_read_file, 397f3208780SMark Brown .llseek = default_llseek,/* read accesses f_pos */ 398f3208780SMark Brown }; 399f3208780SMark Brown 40019c7ac27SMark Brown static ssize_t platform_list_read_file(struct file *file, 40119c7ac27SMark Brown char __user *user_buf, 40219c7ac27SMark Brown size_t count, loff_t *ppos) 40319c7ac27SMark Brown { 40419c7ac27SMark Brown char *buf = kmalloc(PAGE_SIZE, GFP_KERNEL); 4052b194f9dSMark Brown ssize_t len, ret = 0; 40619c7ac27SMark Brown struct snd_soc_platform *platform; 40719c7ac27SMark Brown 40819c7ac27SMark Brown if (!buf) 40919c7ac27SMark Brown return -ENOMEM; 41019c7ac27SMark Brown 4112b194f9dSMark Brown list_for_each_entry(platform, &platform_list, list) { 4122b194f9dSMark Brown len = snprintf(buf + ret, PAGE_SIZE - ret, "%s\n", 41319c7ac27SMark Brown platform->name); 4142b194f9dSMark Brown if (len >= 0) 4152b194f9dSMark Brown ret += len; 4162b194f9dSMark Brown if (ret > PAGE_SIZE) { 4172b194f9dSMark Brown ret = PAGE_SIZE; 4182b194f9dSMark Brown break; 4192b194f9dSMark Brown } 4202b194f9dSMark Brown } 42119c7ac27SMark Brown 42219c7ac27SMark Brown ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret); 42319c7ac27SMark Brown 42419c7ac27SMark Brown kfree(buf); 42519c7ac27SMark Brown 42619c7ac27SMark Brown return ret; 42719c7ac27SMark Brown } 42819c7ac27SMark Brown 42919c7ac27SMark Brown static const struct file_operations platform_list_fops = { 43019c7ac27SMark Brown .read = platform_list_read_file, 43119c7ac27SMark Brown .llseek = default_llseek,/* read accesses f_pos */ 43219c7ac27SMark Brown }; 43319c7ac27SMark Brown 434a6052154SJarkko Nikula static void soc_init_card_debugfs(struct snd_soc_card *card) 435a6052154SJarkko Nikula { 436a6052154SJarkko Nikula card->debugfs_card_root = debugfs_create_dir(card->name, 4378a9dab1aSMark Brown snd_soc_debugfs_root); 4383a45b867SJarkko Nikula if (!card->debugfs_card_root) { 439a6052154SJarkko Nikula dev_warn(card->dev, 4407c08be84SLothar Waßmann "ASoC: Failed to create card debugfs directory\n"); 4413a45b867SJarkko Nikula return; 4423a45b867SJarkko Nikula } 4433a45b867SJarkko Nikula 4443a45b867SJarkko Nikula card->debugfs_pop_time = debugfs_create_u32("dapm_pop_time", 0644, 4453a45b867SJarkko Nikula card->debugfs_card_root, 4463a45b867SJarkko Nikula &card->pop_time); 4473a45b867SJarkko Nikula if (!card->debugfs_pop_time) 4483a45b867SJarkko Nikula dev_warn(card->dev, 449f110bfc7SLiam Girdwood "ASoC: Failed to create pop time debugfs file\n"); 450a6052154SJarkko Nikula } 451a6052154SJarkko Nikula 452a6052154SJarkko Nikula static void soc_cleanup_card_debugfs(struct snd_soc_card *card) 453a6052154SJarkko Nikula { 454a6052154SJarkko Nikula debugfs_remove_recursive(card->debugfs_card_root); 455a6052154SJarkko Nikula } 456a6052154SJarkko Nikula 4572624d5faSMark Brown #else 4582624d5faSMark Brown 4592624d5faSMark Brown static inline void soc_init_codec_debugfs(struct snd_soc_codec *codec) 4602624d5faSMark Brown { 4612624d5faSMark Brown } 4622624d5faSMark Brown 4632624d5faSMark Brown static inline void soc_cleanup_codec_debugfs(struct snd_soc_codec *codec) 4642624d5faSMark Brown { 4652624d5faSMark Brown } 466b95fccbcSAxel Lin 467731f1ab2SSebastien Guiriec static inline void soc_init_platform_debugfs(struct snd_soc_platform *platform) 468731f1ab2SSebastien Guiriec { 469731f1ab2SSebastien Guiriec } 470731f1ab2SSebastien Guiriec 471731f1ab2SSebastien Guiriec static inline void soc_cleanup_platform_debugfs(struct snd_soc_platform *platform) 472731f1ab2SSebastien Guiriec { 473731f1ab2SSebastien Guiriec } 474731f1ab2SSebastien Guiriec 475b95fccbcSAxel Lin static inline void soc_init_card_debugfs(struct snd_soc_card *card) 476b95fccbcSAxel Lin { 477b95fccbcSAxel Lin } 478b95fccbcSAxel Lin 479b95fccbcSAxel Lin static inline void soc_cleanup_card_debugfs(struct snd_soc_card *card) 480b95fccbcSAxel Lin { 481b95fccbcSAxel Lin } 4822624d5faSMark Brown #endif 4832624d5faSMark Brown 48447c88fffSLiam Girdwood struct snd_pcm_substream *snd_soc_get_dai_substream(struct snd_soc_card *card, 48547c88fffSLiam Girdwood const char *dai_link, int stream) 48647c88fffSLiam Girdwood { 48747c88fffSLiam Girdwood int i; 48847c88fffSLiam Girdwood 48947c88fffSLiam Girdwood for (i = 0; i < card->num_links; i++) { 49047c88fffSLiam Girdwood if (card->rtd[i].dai_link->no_pcm && 49147c88fffSLiam Girdwood !strcmp(card->rtd[i].dai_link->name, dai_link)) 49247c88fffSLiam Girdwood return card->rtd[i].pcm->streams[stream].substream; 49347c88fffSLiam Girdwood } 494f110bfc7SLiam Girdwood dev_dbg(card->dev, "ASoC: failed to find dai link %s\n", dai_link); 49547c88fffSLiam Girdwood return NULL; 49647c88fffSLiam Girdwood } 49747c88fffSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_get_dai_substream); 49847c88fffSLiam Girdwood 49947c88fffSLiam Girdwood struct snd_soc_pcm_runtime *snd_soc_get_pcm_runtime(struct snd_soc_card *card, 50047c88fffSLiam Girdwood const char *dai_link) 50147c88fffSLiam Girdwood { 50247c88fffSLiam Girdwood int i; 50347c88fffSLiam Girdwood 50447c88fffSLiam Girdwood for (i = 0; i < card->num_links; i++) { 50547c88fffSLiam Girdwood if (!strcmp(card->rtd[i].dai_link->name, dai_link)) 50647c88fffSLiam Girdwood return &card->rtd[i]; 50747c88fffSLiam Girdwood } 508f110bfc7SLiam Girdwood dev_dbg(card->dev, "ASoC: failed to find rtd %s\n", dai_link); 50947c88fffSLiam Girdwood return NULL; 51047c88fffSLiam Girdwood } 51147c88fffSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_get_pcm_runtime); 51247c88fffSLiam Girdwood 513db2a4165SFrank Mandarino #ifdef CONFIG_SND_SOC_AC97_BUS 514db2a4165SFrank Mandarino /* unregister ac97 codec */ 515db2a4165SFrank Mandarino static int soc_ac97_dev_unregister(struct snd_soc_codec *codec) 516db2a4165SFrank Mandarino { 517db2a4165SFrank Mandarino if (codec->ac97->dev.bus) 518db2a4165SFrank Mandarino device_unregister(&codec->ac97->dev); 519db2a4165SFrank Mandarino return 0; 520db2a4165SFrank Mandarino } 521db2a4165SFrank Mandarino 522db2a4165SFrank Mandarino /* stop no dev release warning */ 523db2a4165SFrank Mandarino static void soc_ac97_device_release(struct device *dev){} 524db2a4165SFrank Mandarino 525db2a4165SFrank Mandarino /* register ac97 codec to bus */ 526db2a4165SFrank Mandarino static int soc_ac97_dev_register(struct snd_soc_codec *codec) 527db2a4165SFrank Mandarino { 528db2a4165SFrank Mandarino int err; 529db2a4165SFrank Mandarino 530db2a4165SFrank Mandarino codec->ac97->dev.bus = &ac97_bus_type; 5314ac5c61fSMark Brown codec->ac97->dev.parent = codec->card->dev; 532db2a4165SFrank Mandarino codec->ac97->dev.release = soc_ac97_device_release; 533db2a4165SFrank Mandarino 534bb072bf0SKay Sievers dev_set_name(&codec->ac97->dev, "%d-%d:%s", 535f0fba2adSLiam Girdwood codec->card->snd_card->number, 0, codec->name); 536db2a4165SFrank Mandarino err = device_register(&codec->ac97->dev); 537db2a4165SFrank Mandarino if (err < 0) { 538f110bfc7SLiam Girdwood dev_err(codec->dev, "ASoC: Can't register ac97 bus\n"); 539db2a4165SFrank Mandarino codec->ac97->dev.bus = NULL; 540db2a4165SFrank Mandarino return err; 541db2a4165SFrank Mandarino } 542db2a4165SFrank Mandarino return 0; 543db2a4165SFrank Mandarino } 544db2a4165SFrank Mandarino #endif 545db2a4165SFrank Mandarino 5469d58a077SRichard Fitzgerald static void codec2codec_close_delayed_work(struct work_struct *work) 5479d58a077SRichard Fitzgerald { 5489d58a077SRichard Fitzgerald /* Currently nothing to do for c2c links 5499d58a077SRichard Fitzgerald * Since c2c links are internal nodes in the DAPM graph and 5509d58a077SRichard Fitzgerald * don't interface with the outside world or application layer 5519d58a077SRichard Fitzgerald * we don't have to do any special handling on close. 5529d58a077SRichard Fitzgerald */ 5539d58a077SRichard Fitzgerald } 5549d58a077SRichard Fitzgerald 5556f8ab4acSMark Brown #ifdef CONFIG_PM_SLEEP 556db2a4165SFrank Mandarino /* powers down audio subsystem for suspend */ 5576f8ab4acSMark Brown int snd_soc_suspend(struct device *dev) 558db2a4165SFrank Mandarino { 5596f8ab4acSMark Brown struct snd_soc_card *card = dev_get_drvdata(dev); 5602eea392dSJarkko Nikula struct snd_soc_codec *codec; 561db2a4165SFrank Mandarino int i; 562db2a4165SFrank Mandarino 563e3509ff0SDaniel Mack /* If the initialization of this soc device failed, there is no codec 564e3509ff0SDaniel Mack * associated with it. Just bail out in this case. 565e3509ff0SDaniel Mack */ 566f0fba2adSLiam Girdwood if (list_empty(&card->codec_dev_list)) 567e3509ff0SDaniel Mack return 0; 568e3509ff0SDaniel Mack 5696ed25978SAndy Green /* Due to the resume being scheduled into a workqueue we could 5706ed25978SAndy Green * suspend before that's finished - wait for it to complete. 5716ed25978SAndy Green */ 572f0fba2adSLiam Girdwood snd_power_lock(card->snd_card); 573f0fba2adSLiam Girdwood snd_power_wait(card->snd_card, SNDRV_CTL_POWER_D0); 574f0fba2adSLiam Girdwood snd_power_unlock(card->snd_card); 5756ed25978SAndy Green 5766ed25978SAndy Green /* we're going to block userspace touching us until resume completes */ 577f0fba2adSLiam Girdwood snd_power_change_state(card->snd_card, SNDRV_CTL_POWER_D3hot); 5786ed25978SAndy Green 579a00f90f9SMark Brown /* mute any active DACs */ 580f0fba2adSLiam Girdwood for (i = 0; i < card->num_rtd; i++) { 581f0fba2adSLiam Girdwood struct snd_soc_dai *dai = card->rtd[i].codec_dai; 582f0fba2adSLiam Girdwood struct snd_soc_dai_driver *drv = dai->driver; 5833efab7dcSMark Brown 584f0fba2adSLiam Girdwood if (card->rtd[i].dai_link->ignore_suspend) 5853efab7dcSMark Brown continue; 5863efab7dcSMark Brown 587f0fba2adSLiam Girdwood if (drv->ops->digital_mute && dai->playback_active) 588f0fba2adSLiam Girdwood drv->ops->digital_mute(dai, 1); 589db2a4165SFrank Mandarino } 590db2a4165SFrank Mandarino 5914ccab3e7SLiam Girdwood /* suspend all pcms */ 592f0fba2adSLiam Girdwood for (i = 0; i < card->num_rtd; i++) { 593f0fba2adSLiam Girdwood if (card->rtd[i].dai_link->ignore_suspend) 5943efab7dcSMark Brown continue; 5953efab7dcSMark Brown 596f0fba2adSLiam Girdwood snd_pcm_suspend_all(card->rtd[i].pcm); 5973efab7dcSMark Brown } 5984ccab3e7SLiam Girdwood 59987506549SMark Brown if (card->suspend_pre) 60070b2ac12SMark Brown card->suspend_pre(card); 601db2a4165SFrank Mandarino 602f0fba2adSLiam Girdwood for (i = 0; i < card->num_rtd; i++) { 603f0fba2adSLiam Girdwood struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai; 604f0fba2adSLiam Girdwood struct snd_soc_platform *platform = card->rtd[i].platform; 6053efab7dcSMark Brown 606f0fba2adSLiam Girdwood if (card->rtd[i].dai_link->ignore_suspend) 6073efab7dcSMark Brown continue; 6083efab7dcSMark Brown 609f0fba2adSLiam Girdwood if (cpu_dai->driver->suspend && !cpu_dai->driver->ac97_control) 610f0fba2adSLiam Girdwood cpu_dai->driver->suspend(cpu_dai); 611f0fba2adSLiam Girdwood if (platform->driver->suspend && !platform->suspended) { 612f0fba2adSLiam Girdwood platform->driver->suspend(cpu_dai); 613f0fba2adSLiam Girdwood platform->suspended = 1; 614f0fba2adSLiam Girdwood } 615db2a4165SFrank Mandarino } 616db2a4165SFrank Mandarino 617db2a4165SFrank Mandarino /* close any waiting streams and save state */ 618f0fba2adSLiam Girdwood for (i = 0; i < card->num_rtd; i++) { 61943829731STejun Heo flush_delayed_work(&card->rtd[i].delayed_work); 620ce6120ccSLiam Girdwood card->rtd[i].codec->dapm.suspend_bias_level = card->rtd[i].codec->dapm.bias_level; 621f0fba2adSLiam Girdwood } 622db2a4165SFrank Mandarino 623f0fba2adSLiam Girdwood for (i = 0; i < card->num_rtd; i++) { 6243efab7dcSMark Brown 625f0fba2adSLiam Girdwood if (card->rtd[i].dai_link->ignore_suspend) 6263efab7dcSMark Brown continue; 6273efab7dcSMark Brown 6287bd3a6f3SMark Brown snd_soc_dapm_stream_event(&card->rtd[i], 6297bd3a6f3SMark Brown SNDRV_PCM_STREAM_PLAYBACK, 630db2a4165SFrank Mandarino SND_SOC_DAPM_STREAM_SUSPEND); 631f0fba2adSLiam Girdwood 6327bd3a6f3SMark Brown snd_soc_dapm_stream_event(&card->rtd[i], 6337bd3a6f3SMark Brown SNDRV_PCM_STREAM_CAPTURE, 634db2a4165SFrank Mandarino SND_SOC_DAPM_STREAM_SUSPEND); 635db2a4165SFrank Mandarino } 636db2a4165SFrank Mandarino 637e2d32ff6SMark Brown /* Recheck all analogue paths too */ 638e2d32ff6SMark Brown dapm_mark_io_dirty(&card->dapm); 639e2d32ff6SMark Brown snd_soc_dapm_sync(&card->dapm); 640e2d32ff6SMark Brown 641f0fba2adSLiam Girdwood /* suspend all CODECs */ 6422eea392dSJarkko Nikula list_for_each_entry(codec, &card->codec_dev_list, card_list) { 6431547aba9SMark Brown /* If there are paths active then the CODEC will be held with 6441547aba9SMark Brown * bias _ON and should not be suspended. */ 645f0fba2adSLiam Girdwood if (!codec->suspended && codec->driver->suspend) { 646ce6120ccSLiam Girdwood switch (codec->dapm.bias_level) { 6471547aba9SMark Brown case SND_SOC_BIAS_STANDBY: 648125a25daSMark Brown /* 649125a25daSMark Brown * If the CODEC is capable of idle 650125a25daSMark Brown * bias off then being in STANDBY 651125a25daSMark Brown * means it's doing something, 652125a25daSMark Brown * otherwise fall through. 653125a25daSMark Brown */ 654125a25daSMark Brown if (codec->dapm.idle_bias_off) { 655125a25daSMark Brown dev_dbg(codec->dev, 65610e8aa9aSMichał Mirosław "ASoC: idle_bias_off CODEC on over suspend\n"); 657125a25daSMark Brown break; 658125a25daSMark Brown } 6591547aba9SMark Brown case SND_SOC_BIAS_OFF: 66084b315eeSLars-Peter Clausen codec->driver->suspend(codec); 661f0fba2adSLiam Girdwood codec->suspended = 1; 6627be4ba24SMark Brown codec->cache_sync = 1; 663da8b8e0fSMark Brown if (codec->using_regmap) 664da8b8e0fSMark Brown regcache_mark_dirty(codec->control_data); 6651547aba9SMark Brown break; 6661547aba9SMark Brown default: 66710e8aa9aSMichał Mirosław dev_dbg(codec->dev, 66810e8aa9aSMichał Mirosław "ASoC: CODEC is on over suspend\n"); 6691547aba9SMark Brown break; 6701547aba9SMark Brown } 6711547aba9SMark Brown } 672f0fba2adSLiam Girdwood } 673db2a4165SFrank Mandarino 674f0fba2adSLiam Girdwood for (i = 0; i < card->num_rtd; i++) { 675f0fba2adSLiam Girdwood struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai; 6763efab7dcSMark Brown 677f0fba2adSLiam Girdwood if (card->rtd[i].dai_link->ignore_suspend) 6783efab7dcSMark Brown continue; 6793efab7dcSMark Brown 680f0fba2adSLiam Girdwood if (cpu_dai->driver->suspend && cpu_dai->driver->ac97_control) 681f0fba2adSLiam Girdwood cpu_dai->driver->suspend(cpu_dai); 682db2a4165SFrank Mandarino } 683db2a4165SFrank Mandarino 68487506549SMark Brown if (card->suspend_post) 68570b2ac12SMark Brown card->suspend_post(card); 686db2a4165SFrank Mandarino 687db2a4165SFrank Mandarino return 0; 688db2a4165SFrank Mandarino } 6896f8ab4acSMark Brown EXPORT_SYMBOL_GPL(snd_soc_suspend); 690db2a4165SFrank Mandarino 6916ed25978SAndy Green /* deferred resume work, so resume can complete before we finished 6926ed25978SAndy Green * setting our codec back up, which can be very slow on I2C 6936ed25978SAndy Green */ 6946ed25978SAndy Green static void soc_resume_deferred(struct work_struct *work) 695db2a4165SFrank Mandarino { 696f0fba2adSLiam Girdwood struct snd_soc_card *card = 697f0fba2adSLiam Girdwood container_of(work, struct snd_soc_card, deferred_resume_work); 6982eea392dSJarkko Nikula struct snd_soc_codec *codec; 699db2a4165SFrank Mandarino int i; 700db2a4165SFrank Mandarino 7016ed25978SAndy Green /* our power state is still SNDRV_CTL_POWER_D3hot from suspend time, 7026ed25978SAndy Green * so userspace apps are blocked from touching us 7036ed25978SAndy Green */ 7046ed25978SAndy Green 705f110bfc7SLiam Girdwood dev_dbg(card->dev, "ASoC: starting resume work\n"); 7066ed25978SAndy Green 7079949788bSMark Brown /* Bring us up into D2 so that DAPM starts enabling things */ 708f0fba2adSLiam Girdwood snd_power_change_state(card->snd_card, SNDRV_CTL_POWER_D2); 7099949788bSMark Brown 71087506549SMark Brown if (card->resume_pre) 71170b2ac12SMark Brown card->resume_pre(card); 712db2a4165SFrank Mandarino 713f0fba2adSLiam Girdwood /* resume AC97 DAIs */ 714f0fba2adSLiam Girdwood for (i = 0; i < card->num_rtd; i++) { 715f0fba2adSLiam Girdwood struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai; 7163efab7dcSMark Brown 717f0fba2adSLiam Girdwood if (card->rtd[i].dai_link->ignore_suspend) 7183efab7dcSMark Brown continue; 7193efab7dcSMark Brown 720f0fba2adSLiam Girdwood if (cpu_dai->driver->resume && cpu_dai->driver->ac97_control) 721f0fba2adSLiam Girdwood cpu_dai->driver->resume(cpu_dai); 722db2a4165SFrank Mandarino } 723db2a4165SFrank Mandarino 7242eea392dSJarkko Nikula list_for_each_entry(codec, &card->codec_dev_list, card_list) { 7251547aba9SMark Brown /* If the CODEC was idle over suspend then it will have been 7261547aba9SMark Brown * left with bias OFF or STANDBY and suspended so we must now 7271547aba9SMark Brown * resume. Otherwise the suspend was suppressed. 7281547aba9SMark Brown */ 729f0fba2adSLiam Girdwood if (codec->driver->resume && codec->suspended) { 730ce6120ccSLiam Girdwood switch (codec->dapm.bias_level) { 7311547aba9SMark Brown case SND_SOC_BIAS_STANDBY: 7321547aba9SMark Brown case SND_SOC_BIAS_OFF: 733f0fba2adSLiam Girdwood codec->driver->resume(codec); 734f0fba2adSLiam Girdwood codec->suspended = 0; 7351547aba9SMark Brown break; 7361547aba9SMark Brown default: 73710e8aa9aSMichał Mirosław dev_dbg(codec->dev, 73810e8aa9aSMichał Mirosław "ASoC: CODEC was on over suspend\n"); 7391547aba9SMark Brown break; 7401547aba9SMark Brown } 7411547aba9SMark Brown } 742f0fba2adSLiam Girdwood } 743db2a4165SFrank Mandarino 744f0fba2adSLiam Girdwood for (i = 0; i < card->num_rtd; i++) { 7453efab7dcSMark Brown 746f0fba2adSLiam Girdwood if (card->rtd[i].dai_link->ignore_suspend) 7473efab7dcSMark Brown continue; 7483efab7dcSMark Brown 7497bd3a6f3SMark Brown snd_soc_dapm_stream_event(&card->rtd[i], 750d9b0951bSLiam Girdwood SNDRV_PCM_STREAM_PLAYBACK, 751db2a4165SFrank Mandarino SND_SOC_DAPM_STREAM_RESUME); 752f0fba2adSLiam Girdwood 7537bd3a6f3SMark Brown snd_soc_dapm_stream_event(&card->rtd[i], 754d9b0951bSLiam Girdwood SNDRV_PCM_STREAM_CAPTURE, 755db2a4165SFrank Mandarino SND_SOC_DAPM_STREAM_RESUME); 756db2a4165SFrank Mandarino } 757db2a4165SFrank Mandarino 7583ff3f64bSMark Brown /* unmute any active DACs */ 759f0fba2adSLiam Girdwood for (i = 0; i < card->num_rtd; i++) { 760f0fba2adSLiam Girdwood struct snd_soc_dai *dai = card->rtd[i].codec_dai; 761f0fba2adSLiam Girdwood struct snd_soc_dai_driver *drv = dai->driver; 7623efab7dcSMark Brown 763f0fba2adSLiam Girdwood if (card->rtd[i].dai_link->ignore_suspend) 7643efab7dcSMark Brown continue; 7653efab7dcSMark Brown 766f0fba2adSLiam Girdwood if (drv->ops->digital_mute && dai->playback_active) 767f0fba2adSLiam Girdwood drv->ops->digital_mute(dai, 0); 768db2a4165SFrank Mandarino } 769db2a4165SFrank Mandarino 770f0fba2adSLiam Girdwood for (i = 0; i < card->num_rtd; i++) { 771f0fba2adSLiam Girdwood struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai; 772f0fba2adSLiam Girdwood struct snd_soc_platform *platform = card->rtd[i].platform; 7733efab7dcSMark Brown 774f0fba2adSLiam Girdwood if (card->rtd[i].dai_link->ignore_suspend) 7753efab7dcSMark Brown continue; 7763efab7dcSMark Brown 777f0fba2adSLiam Girdwood if (cpu_dai->driver->resume && !cpu_dai->driver->ac97_control) 778f0fba2adSLiam Girdwood cpu_dai->driver->resume(cpu_dai); 779f0fba2adSLiam Girdwood if (platform->driver->resume && platform->suspended) { 780f0fba2adSLiam Girdwood platform->driver->resume(cpu_dai); 781f0fba2adSLiam Girdwood platform->suspended = 0; 782f0fba2adSLiam Girdwood } 783db2a4165SFrank Mandarino } 784db2a4165SFrank Mandarino 78587506549SMark Brown if (card->resume_post) 78670b2ac12SMark Brown card->resume_post(card); 787db2a4165SFrank Mandarino 788f110bfc7SLiam Girdwood dev_dbg(card->dev, "ASoC: resume work completed\n"); 7896ed25978SAndy Green 7906ed25978SAndy Green /* userspace can access us now we are back as we were before */ 791f0fba2adSLiam Girdwood snd_power_change_state(card->snd_card, SNDRV_CTL_POWER_D0); 792e2d32ff6SMark Brown 793e2d32ff6SMark Brown /* Recheck all analogue paths too */ 794e2d32ff6SMark Brown dapm_mark_io_dirty(&card->dapm); 795e2d32ff6SMark Brown snd_soc_dapm_sync(&card->dapm); 7966ed25978SAndy Green } 7976ed25978SAndy Green 7986ed25978SAndy Green /* powers up audio subsystem after a suspend */ 7996f8ab4acSMark Brown int snd_soc_resume(struct device *dev) 8006ed25978SAndy Green { 8016f8ab4acSMark Brown struct snd_soc_card *card = dev_get_drvdata(dev); 80282e14e8bSStephen Warren int i, ac97_control = 0; 803b9dd94a8SPeter Ujfalusi 8045ff1ddf2SEric Miao /* If the initialization of this soc device failed, there is no codec 8055ff1ddf2SEric Miao * associated with it. Just bail out in this case. 8065ff1ddf2SEric Miao */ 8075ff1ddf2SEric Miao if (list_empty(&card->codec_dev_list)) 8085ff1ddf2SEric Miao return 0; 8095ff1ddf2SEric Miao 81064ab9baaSMark Brown /* AC97 devices might have other drivers hanging off them so 81164ab9baaSMark Brown * need to resume immediately. Other drivers don't have that 81264ab9baaSMark Brown * problem and may take a substantial amount of time to resume 81364ab9baaSMark Brown * due to I/O costs and anti-pop so handle them out of line. 81464ab9baaSMark Brown */ 815f0fba2adSLiam Girdwood for (i = 0; i < card->num_rtd; i++) { 816f0fba2adSLiam Girdwood struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai; 81782e14e8bSStephen Warren ac97_control |= cpu_dai->driver->ac97_control; 81882e14e8bSStephen Warren } 81982e14e8bSStephen Warren if (ac97_control) { 820f110bfc7SLiam Girdwood dev_dbg(dev, "ASoC: Resuming AC97 immediately\n"); 82164ab9baaSMark Brown soc_resume_deferred(&card->deferred_resume_work); 82264ab9baaSMark Brown } else { 823f110bfc7SLiam Girdwood dev_dbg(dev, "ASoC: Scheduling resume work\n"); 8246308419aSMark Brown if (!schedule_work(&card->deferred_resume_work)) 825f110bfc7SLiam Girdwood dev_err(dev, "ASoC: resume work item may be lost\n"); 826f0fba2adSLiam Girdwood } 8276ed25978SAndy Green 828db2a4165SFrank Mandarino return 0; 829db2a4165SFrank Mandarino } 8306f8ab4acSMark Brown EXPORT_SYMBOL_GPL(snd_soc_resume); 831db2a4165SFrank Mandarino #else 8326f8ab4acSMark Brown #define snd_soc_suspend NULL 8336f8ab4acSMark Brown #define snd_soc_resume NULL 834db2a4165SFrank Mandarino #endif 835db2a4165SFrank Mandarino 83685e7652dSLars-Peter Clausen static const struct snd_soc_dai_ops null_dai_ops = { 83702a06d30SBarry Song }; 83802a06d30SBarry Song 839f0fba2adSLiam Girdwood static int soc_bind_dai_link(struct snd_soc_card *card, int num) 840db2a4165SFrank Mandarino { 841f0fba2adSLiam Girdwood struct snd_soc_dai_link *dai_link = &card->dai_link[num]; 842f0fba2adSLiam Girdwood struct snd_soc_pcm_runtime *rtd = &card->rtd[num]; 843fe3e78e0SMark Brown struct snd_soc_codec *codec; 844435c5e25SMark Brown struct snd_soc_platform *platform; 845f0fba2adSLiam Girdwood struct snd_soc_dai *codec_dai, *cpu_dai; 846848dd8beSMark Brown const char *platform_name; 847db2a4165SFrank Mandarino 848f110bfc7SLiam Girdwood dev_dbg(card->dev, "ASoC: binding %s at idx %d\n", dai_link->name, num); 8496308419aSMark Brown 850b19e6e7bSMark Brown /* Find CPU DAI from registered DAIs*/ 851f0fba2adSLiam Girdwood list_for_each_entry(cpu_dai, &dai_list, list) { 852bc92657aSStephen Warren if (dai_link->cpu_of_node && 853bc92657aSStephen Warren (cpu_dai->dev->of_node != dai_link->cpu_of_node)) 8545a504963SStephen Warren continue; 855bc92657aSStephen Warren if (dai_link->cpu_name && 856bc92657aSStephen Warren strcmp(dev_name(cpu_dai->dev), dai_link->cpu_name)) 8572610ab77SStephen Warren continue; 858bc92657aSStephen Warren if (dai_link->cpu_dai_name && 859bc92657aSStephen Warren strcmp(cpu_dai->name, dai_link->cpu_dai_name)) 860bc92657aSStephen Warren continue; 8612610ab77SStephen Warren 862f0fba2adSLiam Girdwood rtd->cpu_dai = cpu_dai; 863f0fba2adSLiam Girdwood } 864b19e6e7bSMark Brown 865b19e6e7bSMark Brown if (!rtd->cpu_dai) { 866f110bfc7SLiam Girdwood dev_err(card->dev, "ASoC: CPU DAI %s not registered\n", 867f0fba2adSLiam Girdwood dai_link->cpu_dai_name); 868b19e6e7bSMark Brown return -EPROBE_DEFER; 869c5af3a2eSMark Brown } 870c5af3a2eSMark Brown 871b19e6e7bSMark Brown /* Find CODEC from registered CODECs */ 872f0fba2adSLiam Girdwood list_for_each_entry(codec, &codec_list, list) { 8735a504963SStephen Warren if (dai_link->codec_of_node) { 8745a504963SStephen Warren if (codec->dev->of_node != dai_link->codec_of_node) 8755a504963SStephen Warren continue; 8765a504963SStephen Warren } else { 8772610ab77SStephen Warren if (strcmp(codec->name, dai_link->codec_name)) 8782610ab77SStephen Warren continue; 8795a504963SStephen Warren } 8802610ab77SStephen Warren 881f0fba2adSLiam Girdwood rtd->codec = codec; 882f0fba2adSLiam Girdwood 8832610ab77SStephen Warren /* 8842610ab77SStephen Warren * CODEC found, so find CODEC DAI from registered DAIs from 8852610ab77SStephen Warren * this CODEC 8862610ab77SStephen Warren */ 887f0fba2adSLiam Girdwood list_for_each_entry(codec_dai, &dai_list, list) { 888f0fba2adSLiam Girdwood if (codec->dev == codec_dai->dev && 8892610ab77SStephen Warren !strcmp(codec_dai->name, 8902610ab77SStephen Warren dai_link->codec_dai_name)) { 8912610ab77SStephen Warren 892f0fba2adSLiam Girdwood rtd->codec_dai = codec_dai; 893435c5e25SMark Brown } 894f0fba2adSLiam Girdwood } 895b19e6e7bSMark Brown 896b19e6e7bSMark Brown if (!rtd->codec_dai) { 897f110bfc7SLiam Girdwood dev_err(card->dev, "ASoC: CODEC DAI %s not registered\n", 898f0fba2adSLiam Girdwood dai_link->codec_dai_name); 899b19e6e7bSMark Brown return -EPROBE_DEFER; 900f0fba2adSLiam Girdwood } 901b19e6e7bSMark Brown } 902b19e6e7bSMark Brown 903b19e6e7bSMark Brown if (!rtd->codec) { 904f110bfc7SLiam Girdwood dev_err(card->dev, "ASoC: CODEC %s not registered\n", 905f0fba2adSLiam Girdwood dai_link->codec_name); 906b19e6e7bSMark Brown return -EPROBE_DEFER; 907b19e6e7bSMark Brown } 908848dd8beSMark Brown 909848dd8beSMark Brown /* if there's no platform we match on the empty platform */ 910848dd8beSMark Brown platform_name = dai_link->platform_name; 9115a504963SStephen Warren if (!platform_name && !dai_link->platform_of_node) 912848dd8beSMark Brown platform_name = "snd-soc-dummy"; 913848dd8beSMark Brown 914b19e6e7bSMark Brown /* find one from the set of registered platforms */ 915f0fba2adSLiam Girdwood list_for_each_entry(platform, &platform_list, list) { 9165a504963SStephen Warren if (dai_link->platform_of_node) { 9175a504963SStephen Warren if (platform->dev->of_node != 9185a504963SStephen Warren dai_link->platform_of_node) 9195a504963SStephen Warren continue; 9205a504963SStephen Warren } else { 9212610ab77SStephen Warren if (strcmp(platform->name, platform_name)) 9222610ab77SStephen Warren continue; 9235a504963SStephen Warren } 9242610ab77SStephen Warren 925f0fba2adSLiam Girdwood rtd->platform = platform; 926f0fba2adSLiam Girdwood } 927b19e6e7bSMark Brown if (!rtd->platform) { 928f110bfc7SLiam Girdwood dev_err(card->dev, "ASoC: platform %s not registered\n", 929f0fba2adSLiam Girdwood dai_link->platform_name); 930b19e6e7bSMark Brown return -EPROBE_DEFER; 931f0fba2adSLiam Girdwood } 932b19e6e7bSMark Brown 933b19e6e7bSMark Brown card->num_rtd++; 934b19e6e7bSMark Brown 935b19e6e7bSMark Brown return 0; 9366b05eda6SMark Brown } 9376b05eda6SMark Brown 938d12cd198SStephen Warren static int soc_remove_platform(struct snd_soc_platform *platform) 939d12cd198SStephen Warren { 940d12cd198SStephen Warren int ret; 941d12cd198SStephen Warren 942d12cd198SStephen Warren if (platform->driver->remove) { 943d12cd198SStephen Warren ret = platform->driver->remove(platform); 944d12cd198SStephen Warren if (ret < 0) 945f110bfc7SLiam Girdwood dev_err(platform->dev, "ASoC: failed to remove %d\n", 946f110bfc7SLiam Girdwood ret); 947d12cd198SStephen Warren } 948d12cd198SStephen Warren 949d12cd198SStephen Warren /* Make sure all DAPM widgets are freed */ 950d12cd198SStephen Warren snd_soc_dapm_free(&platform->dapm); 951d12cd198SStephen Warren 952d12cd198SStephen Warren soc_cleanup_platform_debugfs(platform); 953d12cd198SStephen Warren platform->probed = 0; 954d12cd198SStephen Warren list_del(&platform->card_list); 955d12cd198SStephen Warren module_put(platform->dev->driver->owner); 956d12cd198SStephen Warren 957d12cd198SStephen Warren return 0; 958d12cd198SStephen Warren } 959d12cd198SStephen Warren 960589c3563SJarkko Nikula static void soc_remove_codec(struct snd_soc_codec *codec) 961589c3563SJarkko Nikula { 962589c3563SJarkko Nikula int err; 963589c3563SJarkko Nikula 964589c3563SJarkko Nikula if (codec->driver->remove) { 965589c3563SJarkko Nikula err = codec->driver->remove(codec); 966589c3563SJarkko Nikula if (err < 0) 967f110bfc7SLiam Girdwood dev_err(codec->dev, "ASoC: failed to remove %d\n", err); 968589c3563SJarkko Nikula } 969589c3563SJarkko Nikula 970589c3563SJarkko Nikula /* Make sure all DAPM widgets are freed */ 971589c3563SJarkko Nikula snd_soc_dapm_free(&codec->dapm); 972589c3563SJarkko Nikula 973589c3563SJarkko Nikula soc_cleanup_codec_debugfs(codec); 974589c3563SJarkko Nikula codec->probed = 0; 975589c3563SJarkko Nikula list_del(&codec->card_list); 976589c3563SJarkko Nikula module_put(codec->dev->driver->owner); 977589c3563SJarkko Nikula } 978589c3563SJarkko Nikula 97962ae68faSStephen Warren static void soc_remove_link_dais(struct snd_soc_card *card, int num, int order) 980f0fba2adSLiam Girdwood { 981f0fba2adSLiam Girdwood struct snd_soc_pcm_runtime *rtd = &card->rtd[num]; 982f0fba2adSLiam Girdwood struct snd_soc_dai *codec_dai = rtd->codec_dai, *cpu_dai = rtd->cpu_dai; 983f0fba2adSLiam Girdwood int err; 984f0fba2adSLiam Girdwood 985f0fba2adSLiam Girdwood /* unregister the rtd device */ 986f0fba2adSLiam Girdwood if (rtd->dev_registered) { 98736ae1a96SMark Brown device_remove_file(rtd->dev, &dev_attr_pmdown_time); 98836ae1a96SMark Brown device_remove_file(rtd->dev, &dev_attr_codec_reg); 98936ae1a96SMark Brown device_unregister(rtd->dev); 990f0fba2adSLiam Girdwood rtd->dev_registered = 0; 99102a06d30SBarry Song } 99202a06d30SBarry Song 993f0fba2adSLiam Girdwood /* remove the CODEC DAI */ 9940168bf0dSLiam Girdwood if (codec_dai && codec_dai->probed && 9950168bf0dSLiam Girdwood codec_dai->driver->remove_order == order) { 996f0fba2adSLiam Girdwood if (codec_dai->driver->remove) { 997f0fba2adSLiam Girdwood err = codec_dai->driver->remove(codec_dai); 998f0fba2adSLiam Girdwood if (err < 0) 999f110bfc7SLiam Girdwood dev_err(codec_dai->dev, 1000f110bfc7SLiam Girdwood "ASoC: failed to remove %s: %d\n", 10010837fc62SFabio Estevam codec_dai->name, err); 1002f0fba2adSLiam Girdwood } 1003f0fba2adSLiam Girdwood codec_dai->probed = 0; 1004f0fba2adSLiam Girdwood list_del(&codec_dai->card_list); 1005f0fba2adSLiam Girdwood } 1006f0fba2adSLiam Girdwood 1007f0fba2adSLiam Girdwood /* remove the cpu_dai */ 10080168bf0dSLiam Girdwood if (cpu_dai && cpu_dai->probed && 10090168bf0dSLiam Girdwood cpu_dai->driver->remove_order == order) { 1010f0fba2adSLiam Girdwood if (cpu_dai->driver->remove) { 1011f0fba2adSLiam Girdwood err = cpu_dai->driver->remove(cpu_dai); 1012f0fba2adSLiam Girdwood if (err < 0) 1013f110bfc7SLiam Girdwood dev_err(cpu_dai->dev, 1014f110bfc7SLiam Girdwood "ASoC: failed to remove %s: %d\n", 10150837fc62SFabio Estevam cpu_dai->name, err); 1016f0fba2adSLiam Girdwood } 1017f0fba2adSLiam Girdwood cpu_dai->probed = 0; 1018f0fba2adSLiam Girdwood list_del(&cpu_dai->card_list); 1019a9db7dbeSStephen Warren 102018d75644SStephen Warren if (!cpu_dai->codec) { 102118d75644SStephen Warren snd_soc_dapm_free(&cpu_dai->dapm); 1022f0fba2adSLiam Girdwood module_put(cpu_dai->dev->driver->owner); 1023f0fba2adSLiam Girdwood } 1024f0fba2adSLiam Girdwood } 102518d75644SStephen Warren } 1026f0fba2adSLiam Girdwood 102762ae68faSStephen Warren static void soc_remove_link_components(struct snd_soc_card *card, int num, 102862ae68faSStephen Warren int order) 102962ae68faSStephen Warren { 103062ae68faSStephen Warren struct snd_soc_pcm_runtime *rtd = &card->rtd[num]; 103162ae68faSStephen Warren struct snd_soc_dai *cpu_dai = rtd->cpu_dai; 103262ae68faSStephen Warren struct snd_soc_dai *codec_dai = rtd->codec_dai; 103362ae68faSStephen Warren struct snd_soc_platform *platform = rtd->platform; 103462ae68faSStephen Warren struct snd_soc_codec *codec; 103562ae68faSStephen Warren 103662ae68faSStephen Warren /* remove the platform */ 103762ae68faSStephen Warren if (platform && platform->probed && 103862ae68faSStephen Warren platform->driver->remove_order == order) { 103962ae68faSStephen Warren soc_remove_platform(platform); 104062ae68faSStephen Warren } 104162ae68faSStephen Warren 104262ae68faSStephen Warren /* remove the CODEC-side CODEC */ 104362ae68faSStephen Warren if (codec_dai) { 104462ae68faSStephen Warren codec = codec_dai->codec; 104562ae68faSStephen Warren if (codec && codec->probed && 104662ae68faSStephen Warren codec->driver->remove_order == order) 104762ae68faSStephen Warren soc_remove_codec(codec); 104862ae68faSStephen Warren } 104962ae68faSStephen Warren 105062ae68faSStephen Warren /* remove any CPU-side CODEC */ 105162ae68faSStephen Warren if (cpu_dai) { 105262ae68faSStephen Warren codec = cpu_dai->codec; 105362ae68faSStephen Warren if (codec && codec->probed && 105462ae68faSStephen Warren codec->driver->remove_order == order) 105562ae68faSStephen Warren soc_remove_codec(codec); 105662ae68faSStephen Warren } 105762ae68faSStephen Warren } 105862ae68faSStephen Warren 10590671fd8eSKuninori Morimoto static void soc_remove_dai_links(struct snd_soc_card *card) 10600671fd8eSKuninori Morimoto { 10610168bf0dSLiam Girdwood int dai, order; 10620671fd8eSKuninori Morimoto 10630168bf0dSLiam Girdwood for (order = SND_SOC_COMP_ORDER_FIRST; order <= SND_SOC_COMP_ORDER_LAST; 10640168bf0dSLiam Girdwood order++) { 10650168bf0dSLiam Girdwood for (dai = 0; dai < card->num_rtd; dai++) 106662ae68faSStephen Warren soc_remove_link_dais(card, dai, order); 10670168bf0dSLiam Girdwood } 106862ae68faSStephen Warren 106962ae68faSStephen Warren for (order = SND_SOC_COMP_ORDER_FIRST; order <= SND_SOC_COMP_ORDER_LAST; 107062ae68faSStephen Warren order++) { 107162ae68faSStephen Warren for (dai = 0; dai < card->num_rtd; dai++) 107262ae68faSStephen Warren soc_remove_link_components(card, dai, order); 107362ae68faSStephen Warren } 107462ae68faSStephen Warren 10750671fd8eSKuninori Morimoto card->num_rtd = 0; 10760671fd8eSKuninori Morimoto } 10770671fd8eSKuninori Morimoto 1078ead9b919SJarkko Nikula static void soc_set_name_prefix(struct snd_soc_card *card, 1079ead9b919SJarkko Nikula struct snd_soc_codec *codec) 1080ead9b919SJarkko Nikula { 1081ead9b919SJarkko Nikula int i; 1082ead9b919SJarkko Nikula 1083ff819b83SDimitris Papastamos if (card->codec_conf == NULL) 1084ead9b919SJarkko Nikula return; 1085ead9b919SJarkko Nikula 1086ff819b83SDimitris Papastamos for (i = 0; i < card->num_configs; i++) { 1087ff819b83SDimitris Papastamos struct snd_soc_codec_conf *map = &card->codec_conf[i]; 1088ead9b919SJarkko Nikula if (map->dev_name && !strcmp(codec->name, map->dev_name)) { 1089ead9b919SJarkko Nikula codec->name_prefix = map->name_prefix; 1090ead9b919SJarkko Nikula break; 1091ead9b919SJarkko Nikula } 1092ead9b919SJarkko Nikula } 1093ead9b919SJarkko Nikula } 1094ead9b919SJarkko Nikula 1095589c3563SJarkko Nikula static int soc_probe_codec(struct snd_soc_card *card, 1096589c3563SJarkko Nikula struct snd_soc_codec *codec) 1097589c3563SJarkko Nikula { 1098589c3563SJarkko Nikula int ret = 0; 109989b95ac0SMark Brown const struct snd_soc_codec_driver *driver = codec->driver; 1100888df395SMark Brown struct snd_soc_dai *dai; 1101589c3563SJarkko Nikula 1102589c3563SJarkko Nikula codec->card = card; 1103589c3563SJarkko Nikula codec->dapm.card = card; 1104589c3563SJarkko Nikula soc_set_name_prefix(card, codec); 1105589c3563SJarkko Nikula 110670d29331SJarkko Nikula if (!try_module_get(codec->dev->driver->owner)) 110770d29331SJarkko Nikula return -ENODEV; 110870d29331SJarkko Nikula 1109d5d1e0beSLars-Peter Clausen soc_init_codec_debugfs(codec); 1110d5d1e0beSLars-Peter Clausen 111177530150SLars-Peter Clausen if (driver->dapm_widgets) 111277530150SLars-Peter Clausen snd_soc_dapm_new_controls(&codec->dapm, driver->dapm_widgets, 111377530150SLars-Peter Clausen driver->num_dapm_widgets); 111477530150SLars-Peter Clausen 1115888df395SMark Brown /* Create DAPM widgets for each DAI stream */ 1116888df395SMark Brown list_for_each_entry(dai, &dai_list, list) { 1117888df395SMark Brown if (dai->dev != codec->dev) 1118888df395SMark Brown continue; 1119888df395SMark Brown 1120888df395SMark Brown snd_soc_dapm_new_dai_widgets(&codec->dapm, dai); 1121888df395SMark Brown } 1122888df395SMark Brown 112333c5f969SMark Brown codec->dapm.idle_bias_off = driver->idle_bias_off; 112433c5f969SMark Brown 112589b95ac0SMark Brown if (driver->probe) { 112689b95ac0SMark Brown ret = driver->probe(codec); 1127589c3563SJarkko Nikula if (ret < 0) { 1128589c3563SJarkko Nikula dev_err(codec->dev, 1129f110bfc7SLiam Girdwood "ASoC: failed to probe CODEC %d\n", ret); 113070d29331SJarkko Nikula goto err_probe; 1131589c3563SJarkko Nikula } 1132ff541f4bSChuansheng Liu WARN(codec->dapm.idle_bias_off && 1133ff541f4bSChuansheng Liu codec->dapm.bias_level != SND_SOC_BIAS_OFF, 113410e8aa9aSMichał Mirosław "codec %s can not start from non-off bias with idle_bias_off==1\n", 113510e8aa9aSMichał Mirosław codec->name); 1136589c3563SJarkko Nikula } 1137589c3563SJarkko Nikula 113838cbf959SMark Brown /* If the driver didn't set I/O up try regmap */ 113998d3088eSMark Brown if (!codec->write && dev_get_regmap(codec->dev, NULL)) 114038cbf959SMark Brown snd_soc_codec_set_cache_io(codec, 0, 0, SND_SOC_REGMAP); 114138cbf959SMark Brown 1142b7af1dafSMark Brown if (driver->controls) 1143022658beSLiam Girdwood snd_soc_add_codec_controls(codec, driver->controls, 1144b7af1dafSMark Brown driver->num_controls); 114589b95ac0SMark Brown if (driver->dapm_routes) 114689b95ac0SMark Brown snd_soc_dapm_add_routes(&codec->dapm, driver->dapm_routes, 114789b95ac0SMark Brown driver->num_dapm_routes); 114889b95ac0SMark Brown 1149589c3563SJarkko Nikula /* mark codec as probed and add to card codec list */ 1150589c3563SJarkko Nikula codec->probed = 1; 1151589c3563SJarkko Nikula list_add(&codec->card_list, &card->codec_dev_list); 11527be31be8SJarkko Nikula list_add(&codec->dapm.list, &card->dapm_list); 1153589c3563SJarkko Nikula 115470d29331SJarkko Nikula return 0; 115570d29331SJarkko Nikula 115670d29331SJarkko Nikula err_probe: 1157d5d1e0beSLars-Peter Clausen soc_cleanup_codec_debugfs(codec); 115870d29331SJarkko Nikula module_put(codec->dev->driver->owner); 115970d29331SJarkko Nikula 1160589c3563SJarkko Nikula return ret; 1161589c3563SJarkko Nikula } 1162589c3563SJarkko Nikula 1163956245e9SLiam Girdwood static int soc_probe_platform(struct snd_soc_card *card, 1164956245e9SLiam Girdwood struct snd_soc_platform *platform) 1165956245e9SLiam Girdwood { 1166956245e9SLiam Girdwood int ret = 0; 1167956245e9SLiam Girdwood const struct snd_soc_platform_driver *driver = platform->driver; 1168be09ad90SLiam Girdwood struct snd_soc_dai *dai; 1169956245e9SLiam Girdwood 1170956245e9SLiam Girdwood platform->card = card; 1171b7950641SLiam Girdwood platform->dapm.card = card; 1172956245e9SLiam Girdwood 1173956245e9SLiam Girdwood if (!try_module_get(platform->dev->driver->owner)) 1174956245e9SLiam Girdwood return -ENODEV; 1175956245e9SLiam Girdwood 1176731f1ab2SSebastien Guiriec soc_init_platform_debugfs(platform); 1177731f1ab2SSebastien Guiriec 1178cb2cf612SLiam Girdwood if (driver->dapm_widgets) 1179cb2cf612SLiam Girdwood snd_soc_dapm_new_controls(&platform->dapm, 1180cb2cf612SLiam Girdwood driver->dapm_widgets, driver->num_dapm_widgets); 1181cb2cf612SLiam Girdwood 1182be09ad90SLiam Girdwood /* Create DAPM widgets for each DAI stream */ 1183be09ad90SLiam Girdwood list_for_each_entry(dai, &dai_list, list) { 1184be09ad90SLiam Girdwood if (dai->dev != platform->dev) 1185be09ad90SLiam Girdwood continue; 1186be09ad90SLiam Girdwood 1187be09ad90SLiam Girdwood snd_soc_dapm_new_dai_widgets(&platform->dapm, dai); 1188be09ad90SLiam Girdwood } 1189be09ad90SLiam Girdwood 11903fec6b6dSStephen Warren platform->dapm.idle_bias_off = 1; 11913fec6b6dSStephen Warren 1192956245e9SLiam Girdwood if (driver->probe) { 1193956245e9SLiam Girdwood ret = driver->probe(platform); 1194956245e9SLiam Girdwood if (ret < 0) { 1195956245e9SLiam Girdwood dev_err(platform->dev, 1196f110bfc7SLiam Girdwood "ASoC: failed to probe platform %d\n", ret); 1197956245e9SLiam Girdwood goto err_probe; 1198956245e9SLiam Girdwood } 1199956245e9SLiam Girdwood } 1200956245e9SLiam Girdwood 1201cb2cf612SLiam Girdwood if (driver->controls) 1202cb2cf612SLiam Girdwood snd_soc_add_platform_controls(platform, driver->controls, 1203cb2cf612SLiam Girdwood driver->num_controls); 1204cb2cf612SLiam Girdwood if (driver->dapm_routes) 1205cb2cf612SLiam Girdwood snd_soc_dapm_add_routes(&platform->dapm, driver->dapm_routes, 1206cb2cf612SLiam Girdwood driver->num_dapm_routes); 1207cb2cf612SLiam Girdwood 1208956245e9SLiam Girdwood /* mark platform as probed and add to card platform list */ 1209956245e9SLiam Girdwood platform->probed = 1; 1210956245e9SLiam Girdwood list_add(&platform->card_list, &card->platform_dev_list); 1211b7950641SLiam Girdwood list_add(&platform->dapm.list, &card->dapm_list); 1212956245e9SLiam Girdwood 1213956245e9SLiam Girdwood return 0; 1214956245e9SLiam Girdwood 1215956245e9SLiam Girdwood err_probe: 121602db1103SLiam Girdwood soc_cleanup_platform_debugfs(platform); 1217956245e9SLiam Girdwood module_put(platform->dev->driver->owner); 1218956245e9SLiam Girdwood 1219956245e9SLiam Girdwood return ret; 1220956245e9SLiam Girdwood } 1221956245e9SLiam Girdwood 122236ae1a96SMark Brown static void rtd_release(struct device *dev) 122336ae1a96SMark Brown { 122436ae1a96SMark Brown kfree(dev); 122536ae1a96SMark Brown } 1226f0fba2adSLiam Girdwood 1227589c3563SJarkko Nikula static int soc_post_component_init(struct snd_soc_card *card, 1228589c3563SJarkko Nikula struct snd_soc_codec *codec, 1229589c3563SJarkko Nikula int num, int dailess) 1230589c3563SJarkko Nikula { 1231589c3563SJarkko Nikula struct snd_soc_dai_link *dai_link = NULL; 1232589c3563SJarkko Nikula struct snd_soc_aux_dev *aux_dev = NULL; 1233589c3563SJarkko Nikula struct snd_soc_pcm_runtime *rtd; 1234589c3563SJarkko Nikula const char *temp, *name; 1235589c3563SJarkko Nikula int ret = 0; 1236589c3563SJarkko Nikula 1237589c3563SJarkko Nikula if (!dailess) { 1238589c3563SJarkko Nikula dai_link = &card->dai_link[num]; 1239589c3563SJarkko Nikula rtd = &card->rtd[num]; 1240589c3563SJarkko Nikula name = dai_link->name; 1241589c3563SJarkko Nikula } else { 1242589c3563SJarkko Nikula aux_dev = &card->aux_dev[num]; 1243589c3563SJarkko Nikula rtd = &card->rtd_aux[num]; 1244589c3563SJarkko Nikula name = aux_dev->name; 1245589c3563SJarkko Nikula } 12460962bb21SJanusz Krzysztofik rtd->card = card; 1247589c3563SJarkko Nikula 1248589c3563SJarkko Nikula /* machine controls, routes and widgets are not prefixed */ 1249589c3563SJarkko Nikula temp = codec->name_prefix; 1250589c3563SJarkko Nikula codec->name_prefix = NULL; 1251589c3563SJarkko Nikula 1252589c3563SJarkko Nikula /* do machine specific initialization */ 1253589c3563SJarkko Nikula if (!dailess && dai_link->init) 1254589c3563SJarkko Nikula ret = dai_link->init(rtd); 1255589c3563SJarkko Nikula else if (dailess && aux_dev->init) 1256589c3563SJarkko Nikula ret = aux_dev->init(&codec->dapm); 1257589c3563SJarkko Nikula if (ret < 0) { 1258f110bfc7SLiam Girdwood dev_err(card->dev, "ASoC: failed to init %s: %d\n", name, ret); 1259589c3563SJarkko Nikula return ret; 1260589c3563SJarkko Nikula } 1261589c3563SJarkko Nikula codec->name_prefix = temp; 1262589c3563SJarkko Nikula 1263589c3563SJarkko Nikula /* register the rtd device */ 1264589c3563SJarkko Nikula rtd->codec = codec; 126536ae1a96SMark Brown 126636ae1a96SMark Brown rtd->dev = kzalloc(sizeof(struct device), GFP_KERNEL); 126736ae1a96SMark Brown if (!rtd->dev) 126836ae1a96SMark Brown return -ENOMEM; 126936ae1a96SMark Brown device_initialize(rtd->dev); 127036ae1a96SMark Brown rtd->dev->parent = card->dev; 127136ae1a96SMark Brown rtd->dev->release = rtd_release; 127236ae1a96SMark Brown rtd->dev->init_name = name; 127336ae1a96SMark Brown dev_set_drvdata(rtd->dev, rtd); 1274b8c0dab9SLiam Girdwood mutex_init(&rtd->pcm_mutex); 127501d7584cSLiam Girdwood INIT_LIST_HEAD(&rtd->dpcm[SNDRV_PCM_STREAM_PLAYBACK].be_clients); 127601d7584cSLiam Girdwood INIT_LIST_HEAD(&rtd->dpcm[SNDRV_PCM_STREAM_CAPTURE].be_clients); 127701d7584cSLiam Girdwood INIT_LIST_HEAD(&rtd->dpcm[SNDRV_PCM_STREAM_PLAYBACK].fe_clients); 127801d7584cSLiam Girdwood INIT_LIST_HEAD(&rtd->dpcm[SNDRV_PCM_STREAM_CAPTURE].fe_clients); 127936ae1a96SMark Brown ret = device_add(rtd->dev); 1280589c3563SJarkko Nikula if (ret < 0) { 1281865df9cbSChuansheng Liu /* calling put_device() here to free the rtd->dev */ 1282865df9cbSChuansheng Liu put_device(rtd->dev); 1283589c3563SJarkko Nikula dev_err(card->dev, 1284f110bfc7SLiam Girdwood "ASoC: failed to register runtime device: %d\n", ret); 1285589c3563SJarkko Nikula return ret; 1286589c3563SJarkko Nikula } 1287589c3563SJarkko Nikula rtd->dev_registered = 1; 1288589c3563SJarkko Nikula 1289589c3563SJarkko Nikula /* add DAPM sysfs entries for this codec */ 129036ae1a96SMark Brown ret = snd_soc_dapm_sys_add(rtd->dev); 1291589c3563SJarkko Nikula if (ret < 0) 1292589c3563SJarkko Nikula dev_err(codec->dev, 1293f110bfc7SLiam Girdwood "ASoC: failed to add codec dapm sysfs entries: %d\n", ret); 1294589c3563SJarkko Nikula 1295589c3563SJarkko Nikula /* add codec sysfs entries */ 129636ae1a96SMark Brown ret = device_create_file(rtd->dev, &dev_attr_codec_reg); 1297589c3563SJarkko Nikula if (ret < 0) 1298589c3563SJarkko Nikula dev_err(codec->dev, 1299f110bfc7SLiam Girdwood "ASoC: failed to add codec sysfs files: %d\n", ret); 1300589c3563SJarkko Nikula 1301f86dcef8SLiam Girdwood #ifdef CONFIG_DEBUG_FS 1302f86dcef8SLiam Girdwood /* add DPCM sysfs entries */ 1303cd0f8911SLiam Girdwood if (!dailess && !dai_link->dynamic) 1304f86dcef8SLiam Girdwood goto out; 1305f86dcef8SLiam Girdwood 1306f86dcef8SLiam Girdwood ret = soc_dpcm_debugfs_add(rtd); 1307f86dcef8SLiam Girdwood if (ret < 0) 1308f110bfc7SLiam Girdwood dev_err(rtd->dev, "ASoC: failed to add dpcm sysfs entries: %d\n", ret); 1309f86dcef8SLiam Girdwood 1310f86dcef8SLiam Girdwood out: 1311f86dcef8SLiam Girdwood #endif 1312589c3563SJarkko Nikula return 0; 1313589c3563SJarkko Nikula } 1314589c3563SJarkko Nikula 131562ae68faSStephen Warren static int soc_probe_link_components(struct snd_soc_card *card, int num, 131662ae68faSStephen Warren int order) 131762ae68faSStephen Warren { 131862ae68faSStephen Warren struct snd_soc_pcm_runtime *rtd = &card->rtd[num]; 131962ae68faSStephen Warren struct snd_soc_dai *cpu_dai = rtd->cpu_dai; 132062ae68faSStephen Warren struct snd_soc_dai *codec_dai = rtd->codec_dai; 132162ae68faSStephen Warren struct snd_soc_platform *platform = rtd->platform; 132262ae68faSStephen Warren int ret; 132362ae68faSStephen Warren 132462ae68faSStephen Warren /* probe the CPU-side component, if it is a CODEC */ 132562ae68faSStephen Warren if (cpu_dai->codec && 132662ae68faSStephen Warren !cpu_dai->codec->probed && 132762ae68faSStephen Warren cpu_dai->codec->driver->probe_order == order) { 132862ae68faSStephen Warren ret = soc_probe_codec(card, cpu_dai->codec); 132962ae68faSStephen Warren if (ret < 0) 133062ae68faSStephen Warren return ret; 133162ae68faSStephen Warren } 133262ae68faSStephen Warren 133362ae68faSStephen Warren /* probe the CODEC-side component */ 133462ae68faSStephen Warren if (!codec_dai->codec->probed && 133562ae68faSStephen Warren codec_dai->codec->driver->probe_order == order) { 133662ae68faSStephen Warren ret = soc_probe_codec(card, codec_dai->codec); 133762ae68faSStephen Warren if (ret < 0) 133862ae68faSStephen Warren return ret; 133962ae68faSStephen Warren } 134062ae68faSStephen Warren 134162ae68faSStephen Warren /* probe the platform */ 134262ae68faSStephen Warren if (!platform->probed && 134362ae68faSStephen Warren platform->driver->probe_order == order) { 134462ae68faSStephen Warren ret = soc_probe_platform(card, platform); 134562ae68faSStephen Warren if (ret < 0) 134662ae68faSStephen Warren return ret; 134762ae68faSStephen Warren } 134862ae68faSStephen Warren 134962ae68faSStephen Warren return 0; 135062ae68faSStephen Warren } 135162ae68faSStephen Warren 135262ae68faSStephen Warren static int soc_probe_link_dais(struct snd_soc_card *card, int num, int order) 1353f0fba2adSLiam Girdwood { 1354f0fba2adSLiam Girdwood struct snd_soc_dai_link *dai_link = &card->dai_link[num]; 1355f0fba2adSLiam Girdwood struct snd_soc_pcm_runtime *rtd = &card->rtd[num]; 1356f0fba2adSLiam Girdwood struct snd_soc_codec *codec = rtd->codec; 1357f0fba2adSLiam Girdwood struct snd_soc_platform *platform = rtd->platform; 1358c74184edSMark Brown struct snd_soc_dai *codec_dai = rtd->codec_dai; 1359c74184edSMark Brown struct snd_soc_dai *cpu_dai = rtd->cpu_dai; 1360c74184edSMark Brown struct snd_soc_dapm_widget *play_w, *capture_w; 1361f0fba2adSLiam Girdwood int ret; 1362f0fba2adSLiam Girdwood 1363f110bfc7SLiam Girdwood dev_dbg(card->dev, "ASoC: probe %s dai link %d late %d\n", 13640168bf0dSLiam Girdwood card->name, num, order); 1365f0fba2adSLiam Girdwood 1366f0fba2adSLiam Girdwood /* config components */ 1367f0fba2adSLiam Girdwood cpu_dai->platform = platform; 1368f0fba2adSLiam Girdwood codec_dai->card = card; 1369f0fba2adSLiam Girdwood cpu_dai->card = card; 1370f0fba2adSLiam Girdwood 1371f0fba2adSLiam Girdwood /* set default power off timeout */ 1372f0fba2adSLiam Girdwood rtd->pmdown_time = pmdown_time; 1373f0fba2adSLiam Girdwood 1374f0fba2adSLiam Girdwood /* probe the cpu_dai */ 13750168bf0dSLiam Girdwood if (!cpu_dai->probed && 13760168bf0dSLiam Girdwood cpu_dai->driver->probe_order == order) { 1377a9db7dbeSStephen Warren if (!cpu_dai->codec) { 1378be09ad90SLiam Girdwood cpu_dai->dapm.card = card; 137961b61e3cSLiam Girdwood if (!try_module_get(cpu_dai->dev->driver->owner)) 138061b61e3cSLiam Girdwood return -ENODEV; 138161b61e3cSLiam Girdwood 138218d75644SStephen Warren list_add(&cpu_dai->dapm.list, &card->dapm_list); 1383be09ad90SLiam Girdwood snd_soc_dapm_new_dai_widgets(&cpu_dai->dapm, cpu_dai); 1384a9db7dbeSStephen Warren } 1385be09ad90SLiam Girdwood 1386f0fba2adSLiam Girdwood if (cpu_dai->driver->probe) { 1387f0fba2adSLiam Girdwood ret = cpu_dai->driver->probe(cpu_dai); 1388f0fba2adSLiam Girdwood if (ret < 0) { 1389f110bfc7SLiam Girdwood dev_err(cpu_dai->dev, 1390f110bfc7SLiam Girdwood "ASoC: failed to probe CPU DAI %s: %d\n", 13910837fc62SFabio Estevam cpu_dai->name, ret); 139261b61e3cSLiam Girdwood module_put(cpu_dai->dev->driver->owner); 1393f0fba2adSLiam Girdwood return ret; 1394f0fba2adSLiam Girdwood } 1395f0fba2adSLiam Girdwood } 1396f0fba2adSLiam Girdwood cpu_dai->probed = 1; 13971c8371d6SWolfram Sang /* mark cpu_dai as probed and add to card dai list */ 1398f0fba2adSLiam Girdwood list_add(&cpu_dai->card_list, &card->dai_dev_list); 1399f0fba2adSLiam Girdwood } 1400f0fba2adSLiam Girdwood 1401f0fba2adSLiam Girdwood /* probe the CODEC DAI */ 14020168bf0dSLiam Girdwood if (!codec_dai->probed && codec_dai->driver->probe_order == order) { 1403f0fba2adSLiam Girdwood if (codec_dai->driver->probe) { 1404f0fba2adSLiam Girdwood ret = codec_dai->driver->probe(codec_dai); 1405f0fba2adSLiam Girdwood if (ret < 0) { 1406f110bfc7SLiam Girdwood dev_err(codec_dai->dev, 1407f110bfc7SLiam Girdwood "ASoC: failed to probe CODEC DAI %s: %d\n", 14080837fc62SFabio Estevam codec_dai->name, ret); 1409f0fba2adSLiam Girdwood return ret; 1410f0fba2adSLiam Girdwood } 1411f0fba2adSLiam Girdwood } 1412f0fba2adSLiam Girdwood 14131c8371d6SWolfram Sang /* mark codec_dai as probed and add to card dai list */ 1414f0fba2adSLiam Girdwood codec_dai->probed = 1; 1415f0fba2adSLiam Girdwood list_add(&codec_dai->card_list, &card->dai_dev_list); 1416f0fba2adSLiam Girdwood } 1417f0fba2adSLiam Girdwood 14180168bf0dSLiam Girdwood /* complete DAI probe during last probe */ 14190168bf0dSLiam Girdwood if (order != SND_SOC_COMP_ORDER_LAST) 14200168bf0dSLiam Girdwood return 0; 14210168bf0dSLiam Girdwood 1422589c3563SJarkko Nikula ret = soc_post_component_init(card, codec, num, 0); 1423589c3563SJarkko Nikula if (ret) 1424f0fba2adSLiam Girdwood return ret; 1425f0fba2adSLiam Girdwood 142636ae1a96SMark Brown ret = device_create_file(rtd->dev, &dev_attr_pmdown_time); 1427f0fba2adSLiam Girdwood if (ret < 0) 1428f110bfc7SLiam Girdwood dev_warn(rtd->dev, "ASoC: failed to add pmdown_time sysfs: %d\n", 1429f110bfc7SLiam Girdwood ret); 1430f0fba2adSLiam Girdwood 14311245b700SNamarta Kohli if (cpu_dai->driver->compress_dai) { 14321245b700SNamarta Kohli /*create compress_device"*/ 14331245b700SNamarta Kohli ret = soc_new_compress(rtd, num); 14341245b700SNamarta Kohli if (ret < 0) { 1435f110bfc7SLiam Girdwood dev_err(card->dev, "ASoC: can't create compress %s\n", 14361245b700SNamarta Kohli dai_link->stream_name); 14371245b700SNamarta Kohli return ret; 14381245b700SNamarta Kohli } 14391245b700SNamarta Kohli } else { 14401245b700SNamarta Kohli 1441c74184edSMark Brown if (!dai_link->params) { 1442f0fba2adSLiam Girdwood /* create the pcm */ 1443f0fba2adSLiam Girdwood ret = soc_new_pcm(rtd, num); 1444f0fba2adSLiam Girdwood if (ret < 0) { 1445f110bfc7SLiam Girdwood dev_err(card->dev, "ASoC: can't create pcm %s :%d\n", 14460837fc62SFabio Estevam dai_link->stream_name, ret); 1447f0fba2adSLiam Girdwood return ret; 1448f0fba2adSLiam Girdwood } 1449c74184edSMark Brown } else { 14509d58a077SRichard Fitzgerald INIT_DELAYED_WORK(&rtd->delayed_work, 14519d58a077SRichard Fitzgerald codec2codec_close_delayed_work); 14529d58a077SRichard Fitzgerald 1453c74184edSMark Brown /* link the DAI widgets */ 1454c74184edSMark Brown play_w = codec_dai->playback_widget; 1455c74184edSMark Brown capture_w = cpu_dai->capture_widget; 1456c74184edSMark Brown if (play_w && capture_w) { 1457c74184edSMark Brown ret = snd_soc_dapm_new_pcm(card, dai_link->params, 1458c74184edSMark Brown capture_w, play_w); 1459c74184edSMark Brown if (ret != 0) { 1460f110bfc7SLiam Girdwood dev_err(card->dev, "ASoC: Can't link %s to %s: %d\n", 1461c74184edSMark Brown play_w->name, capture_w->name, ret); 1462c74184edSMark Brown return ret; 1463c74184edSMark Brown } 1464c74184edSMark Brown } 1465c74184edSMark Brown 1466c74184edSMark Brown play_w = cpu_dai->playback_widget; 1467c74184edSMark Brown capture_w = codec_dai->capture_widget; 1468c74184edSMark Brown if (play_w && capture_w) { 1469c74184edSMark Brown ret = snd_soc_dapm_new_pcm(card, dai_link->params, 1470c74184edSMark Brown capture_w, play_w); 1471c74184edSMark Brown if (ret != 0) { 1472f110bfc7SLiam Girdwood dev_err(card->dev, "ASoC: Can't link %s to %s: %d\n", 1473c74184edSMark Brown play_w->name, capture_w->name, ret); 1474c74184edSMark Brown return ret; 1475c74184edSMark Brown } 1476c74184edSMark Brown } 1477c74184edSMark Brown } 14781245b700SNamarta Kohli } 1479f0fba2adSLiam Girdwood 1480f0fba2adSLiam Girdwood /* add platform data for AC97 devices */ 1481f0fba2adSLiam Girdwood if (rtd->codec_dai->driver->ac97_control) 1482f0fba2adSLiam Girdwood snd_ac97_dev_add_pdata(codec->ac97, rtd->cpu_dai->ac97_pdata); 1483f0fba2adSLiam Girdwood 1484f0fba2adSLiam Girdwood return 0; 1485f0fba2adSLiam Girdwood } 1486f0fba2adSLiam Girdwood 1487f0fba2adSLiam Girdwood #ifdef CONFIG_SND_SOC_AC97_BUS 1488f0fba2adSLiam Girdwood static int soc_register_ac97_dai_link(struct snd_soc_pcm_runtime *rtd) 1489f0fba2adSLiam Girdwood { 1490f0fba2adSLiam Girdwood int ret; 1491f0fba2adSLiam Girdwood 1492f0fba2adSLiam Girdwood /* Only instantiate AC97 if not already done by the adaptor 1493f0fba2adSLiam Girdwood * for the generic AC97 subsystem. 14946b05eda6SMark Brown */ 1495f0fba2adSLiam Girdwood if (rtd->codec_dai->driver->ac97_control && !rtd->codec->ac97_registered) { 14960562f788SMika Westerberg /* 14970562f788SMika Westerberg * It is possible that the AC97 device is already registered to 14980562f788SMika Westerberg * the device subsystem. This happens when the device is created 14990562f788SMika Westerberg * via snd_ac97_mixer(). Currently only SoC codec that does so 15000562f788SMika Westerberg * is the generic AC97 glue but others migh emerge. 15010562f788SMika Westerberg * 15020562f788SMika Westerberg * In those cases we don't try to register the device again. 15030562f788SMika Westerberg */ 15040562f788SMika Westerberg if (!rtd->codec->ac97_created) 15050562f788SMika Westerberg return 0; 1506f0fba2adSLiam Girdwood 1507f0fba2adSLiam Girdwood ret = soc_ac97_dev_register(rtd->codec); 1508f0fba2adSLiam Girdwood if (ret < 0) { 1509f110bfc7SLiam Girdwood dev_err(rtd->codec->dev, 1510f110bfc7SLiam Girdwood "ASoC: AC97 device register failed: %d\n", ret); 1511f0fba2adSLiam Girdwood return ret; 1512435c5e25SMark Brown } 1513435c5e25SMark Brown 1514f0fba2adSLiam Girdwood rtd->codec->ac97_registered = 1; 1515f0fba2adSLiam Girdwood } 1516f0fba2adSLiam Girdwood return 0; 1517f0fba2adSLiam Girdwood } 1518435c5e25SMark Brown 1519f0fba2adSLiam Girdwood static void soc_unregister_ac97_dai_link(struct snd_soc_codec *codec) 1520f0fba2adSLiam Girdwood { 1521f0fba2adSLiam Girdwood if (codec->ac97_registered) { 1522f0fba2adSLiam Girdwood soc_ac97_dev_unregister(codec); 1523f0fba2adSLiam Girdwood codec->ac97_registered = 0; 1524f0fba2adSLiam Girdwood } 1525f0fba2adSLiam Girdwood } 1526f0fba2adSLiam Girdwood #endif 1527435c5e25SMark Brown 1528b19e6e7bSMark Brown static int soc_check_aux_dev(struct snd_soc_card *card, int num) 1529b19e6e7bSMark Brown { 1530b19e6e7bSMark Brown struct snd_soc_aux_dev *aux_dev = &card->aux_dev[num]; 1531b19e6e7bSMark Brown struct snd_soc_codec *codec; 1532b19e6e7bSMark Brown 1533b19e6e7bSMark Brown /* find CODEC from registered CODECs*/ 1534b19e6e7bSMark Brown list_for_each_entry(codec, &codec_list, list) { 1535b19e6e7bSMark Brown if (!strcmp(codec->name, aux_dev->codec_name)) 1536b19e6e7bSMark Brown return 0; 1537b19e6e7bSMark Brown } 1538b19e6e7bSMark Brown 1539f110bfc7SLiam Girdwood dev_err(card->dev, "ASoC: %s not registered\n", aux_dev->codec_name); 1540fb099cb7SMark Brown 1541b19e6e7bSMark Brown return -EPROBE_DEFER; 1542b19e6e7bSMark Brown } 1543b19e6e7bSMark Brown 15442eea392dSJarkko Nikula static int soc_probe_aux_dev(struct snd_soc_card *card, int num) 15452eea392dSJarkko Nikula { 15462eea392dSJarkko Nikula struct snd_soc_aux_dev *aux_dev = &card->aux_dev[num]; 15472eea392dSJarkko Nikula struct snd_soc_codec *codec; 1548676ad98aSJarkko Nikula int ret = -ENODEV; 15492eea392dSJarkko Nikula 15502eea392dSJarkko Nikula /* find CODEC from registered CODECs*/ 15512eea392dSJarkko Nikula list_for_each_entry(codec, &codec_list, list) { 15522eea392dSJarkko Nikula if (!strcmp(codec->name, aux_dev->codec_name)) { 15532eea392dSJarkko Nikula if (codec->probed) { 15542eea392dSJarkko Nikula dev_err(codec->dev, 1555f110bfc7SLiam Girdwood "ASoC: codec already probed"); 15562eea392dSJarkko Nikula ret = -EBUSY; 15572eea392dSJarkko Nikula goto out; 15582eea392dSJarkko Nikula } 1559676ad98aSJarkko Nikula goto found; 15602eea392dSJarkko Nikula } 15612eea392dSJarkko Nikula } 1562676ad98aSJarkko Nikula /* codec not found */ 1563f110bfc7SLiam Girdwood dev_err(card->dev, "ASoC: codec %s not found", aux_dev->codec_name); 1564b19e6e7bSMark Brown return -EPROBE_DEFER; 15652eea392dSJarkko Nikula 1566676ad98aSJarkko Nikula found: 1567589c3563SJarkko Nikula ret = soc_probe_codec(card, codec); 15682eea392dSJarkko Nikula if (ret < 0) 1569589c3563SJarkko Nikula return ret; 15702eea392dSJarkko Nikula 1571589c3563SJarkko Nikula ret = soc_post_component_init(card, codec, num, 1); 15722eea392dSJarkko Nikula 15732eea392dSJarkko Nikula out: 15742eea392dSJarkko Nikula return ret; 15752eea392dSJarkko Nikula } 15762eea392dSJarkko Nikula 15772eea392dSJarkko Nikula static void soc_remove_aux_dev(struct snd_soc_card *card, int num) 15782eea392dSJarkko Nikula { 15792eea392dSJarkko Nikula struct snd_soc_pcm_runtime *rtd = &card->rtd_aux[num]; 15802eea392dSJarkko Nikula struct snd_soc_codec *codec = rtd->codec; 15812eea392dSJarkko Nikula 15822eea392dSJarkko Nikula /* unregister the rtd device */ 15832eea392dSJarkko Nikula if (rtd->dev_registered) { 158436ae1a96SMark Brown device_remove_file(rtd->dev, &dev_attr_codec_reg); 1585d3bf1561SChuansheng Liu device_unregister(rtd->dev); 15862eea392dSJarkko Nikula rtd->dev_registered = 0; 15872eea392dSJarkko Nikula } 15882eea392dSJarkko Nikula 1589589c3563SJarkko Nikula if (codec && codec->probed) 1590589c3563SJarkko Nikula soc_remove_codec(codec); 15912eea392dSJarkko Nikula } 15922eea392dSJarkko Nikula 1593fdf0f54dSDimitris Papastamos static int snd_soc_init_codec_cache(struct snd_soc_codec *codec, 1594fdf0f54dSDimitris Papastamos enum snd_soc_compress_type compress_type) 1595fdf0f54dSDimitris Papastamos { 1596fdf0f54dSDimitris Papastamos int ret; 1597fdf0f54dSDimitris Papastamos 1598fdf0f54dSDimitris Papastamos if (codec->cache_init) 1599fdf0f54dSDimitris Papastamos return 0; 1600fdf0f54dSDimitris Papastamos 1601fdf0f54dSDimitris Papastamos /* override the compress_type if necessary */ 1602fdf0f54dSDimitris Papastamos if (compress_type && codec->compress_type != compress_type) 1603fdf0f54dSDimitris Papastamos codec->compress_type = compress_type; 1604fdf0f54dSDimitris Papastamos ret = snd_soc_cache_init(codec); 1605fdf0f54dSDimitris Papastamos if (ret < 0) { 160610e8aa9aSMichał Mirosław dev_err(codec->dev, 160710e8aa9aSMichał Mirosław "ASoC: Failed to set cache compression type: %d\n", 160810e8aa9aSMichał Mirosław ret); 1609fdf0f54dSDimitris Papastamos return ret; 1610fdf0f54dSDimitris Papastamos } 1611fdf0f54dSDimitris Papastamos codec->cache_init = 1; 1612fdf0f54dSDimitris Papastamos return 0; 1613fdf0f54dSDimitris Papastamos } 1614fdf0f54dSDimitris Papastamos 1615b19e6e7bSMark Brown static int snd_soc_instantiate_card(struct snd_soc_card *card) 1616f0fba2adSLiam Girdwood { 1617fdf0f54dSDimitris Papastamos struct snd_soc_codec *codec; 1618fdf0f54dSDimitris Papastamos struct snd_soc_codec_conf *codec_conf; 1619fdf0f54dSDimitris Papastamos enum snd_soc_compress_type compress_type; 162075d9ac46SMark Brown struct snd_soc_dai_link *dai_link; 1621f04209a7SMark Brown int ret, i, order, dai_fmt; 162296dd3622SMark Brown 162301b9d99aSLiam Girdwood mutex_lock_nested(&card->mutex, SND_SOC_CARD_CLASS_INIT); 1624f0fba2adSLiam Girdwood 1625b19e6e7bSMark Brown /* bind DAIs */ 1626b19e6e7bSMark Brown for (i = 0; i < card->num_links; i++) { 1627b19e6e7bSMark Brown ret = soc_bind_dai_link(card, i); 1628b19e6e7bSMark Brown if (ret != 0) 1629b19e6e7bSMark Brown goto base_error; 1630db2a4165SFrank Mandarino } 1631db2a4165SFrank Mandarino 1632b19e6e7bSMark Brown /* check aux_devs too */ 1633b19e6e7bSMark Brown for (i = 0; i < card->num_aux_devs; i++) { 1634b19e6e7bSMark Brown ret = soc_check_aux_dev(card, i); 1635b19e6e7bSMark Brown if (ret != 0) 1636b19e6e7bSMark Brown goto base_error; 1637db2a4165SFrank Mandarino } 1638db2a4165SFrank Mandarino 1639fdf0f54dSDimitris Papastamos /* initialize the register cache for each available codec */ 1640fdf0f54dSDimitris Papastamos list_for_each_entry(codec, &codec_list, list) { 1641fdf0f54dSDimitris Papastamos if (codec->cache_init) 1642fdf0f54dSDimitris Papastamos continue; 1643861f2fafSDimitris Papastamos /* by default we don't override the compress_type */ 1644861f2fafSDimitris Papastamos compress_type = 0; 1645fdf0f54dSDimitris Papastamos /* check to see if we need to override the compress_type */ 1646fdf0f54dSDimitris Papastamos for (i = 0; i < card->num_configs; ++i) { 1647fdf0f54dSDimitris Papastamos codec_conf = &card->codec_conf[i]; 1648fdf0f54dSDimitris Papastamos if (!strcmp(codec->name, codec_conf->dev_name)) { 1649fdf0f54dSDimitris Papastamos compress_type = codec_conf->compress_type; 1650fdf0f54dSDimitris Papastamos if (compress_type && compress_type 1651fdf0f54dSDimitris Papastamos != codec->compress_type) 1652fdf0f54dSDimitris Papastamos break; 1653fdf0f54dSDimitris Papastamos } 1654fdf0f54dSDimitris Papastamos } 1655fdf0f54dSDimitris Papastamos ret = snd_soc_init_codec_cache(codec, compress_type); 1656b19e6e7bSMark Brown if (ret < 0) 1657b19e6e7bSMark Brown goto base_error; 1658fdf0f54dSDimitris Papastamos } 1659fdf0f54dSDimitris Papastamos 1660f0fba2adSLiam Girdwood /* card bind complete so register a sound card */ 1661f0fba2adSLiam Girdwood ret = snd_card_create(SNDRV_DEFAULT_IDX1, SNDRV_DEFAULT_STR1, 1662f0fba2adSLiam Girdwood card->owner, 0, &card->snd_card); 1663f0fba2adSLiam Girdwood if (ret < 0) { 166410e8aa9aSMichał Mirosław dev_err(card->dev, 166510e8aa9aSMichał Mirosław "ASoC: can't create sound card for card %s: %d\n", 166610e8aa9aSMichał Mirosław card->name, ret); 1667b19e6e7bSMark Brown goto base_error; 1668db2a4165SFrank Mandarino } 1669f0fba2adSLiam Girdwood card->snd_card->dev = card->dev; 1670db2a4165SFrank Mandarino 1671e37a4970SMark Brown card->dapm.bias_level = SND_SOC_BIAS_OFF; 1672e37a4970SMark Brown card->dapm.dev = card->dev; 1673e37a4970SMark Brown card->dapm.card = card; 1674e37a4970SMark Brown list_add(&card->dapm.list, &card->dapm_list); 1675e37a4970SMark Brown 1676d5d1e0beSLars-Peter Clausen #ifdef CONFIG_DEBUG_FS 1677d5d1e0beSLars-Peter Clausen snd_soc_dapm_debugfs_init(&card->dapm, card->debugfs_card_root); 1678d5d1e0beSLars-Peter Clausen #endif 1679d5d1e0beSLars-Peter Clausen 168088ee1c61SMark Brown #ifdef CONFIG_PM_SLEEP 16816ed25978SAndy Green /* deferred resume work */ 16826308419aSMark Brown INIT_WORK(&card->deferred_resume_work, soc_resume_deferred); 16831301a964SRandy Dunlap #endif 16846ed25978SAndy Green 16859a841ebbSMark Brown if (card->dapm_widgets) 16869a841ebbSMark Brown snd_soc_dapm_new_controls(&card->dapm, card->dapm_widgets, 16879a841ebbSMark Brown card->num_dapm_widgets); 16889a841ebbSMark Brown 1689f0fba2adSLiam Girdwood /* initialise the sound card only once */ 1690f0fba2adSLiam Girdwood if (card->probe) { 1691e7361ec4SMark Brown ret = card->probe(card); 1692f0fba2adSLiam Girdwood if (ret < 0) 1693f0fba2adSLiam Girdwood goto card_probe_error; 1694f0fba2adSLiam Girdwood } 1695f0fba2adSLiam Girdwood 169662ae68faSStephen Warren /* probe all components used by DAI links on this card */ 16970168bf0dSLiam Girdwood for (order = SND_SOC_COMP_ORDER_FIRST; order <= SND_SOC_COMP_ORDER_LAST; 16980168bf0dSLiam Girdwood order++) { 1699fe3e78e0SMark Brown for (i = 0; i < card->num_links; i++) { 170062ae68faSStephen Warren ret = soc_probe_link_components(card, i, order); 170162ae68faSStephen Warren if (ret < 0) { 1702f110bfc7SLiam Girdwood dev_err(card->dev, 1703f110bfc7SLiam Girdwood "ASoC: failed to instantiate card %d\n", 1704f110bfc7SLiam Girdwood ret); 170562ae68faSStephen Warren goto probe_dai_err; 170662ae68faSStephen Warren } 170762ae68faSStephen Warren } 170862ae68faSStephen Warren } 170962ae68faSStephen Warren 171062ae68faSStephen Warren /* probe all DAI links on this card */ 171162ae68faSStephen Warren for (order = SND_SOC_COMP_ORDER_FIRST; order <= SND_SOC_COMP_ORDER_LAST; 171262ae68faSStephen Warren order++) { 171362ae68faSStephen Warren for (i = 0; i < card->num_links; i++) { 171462ae68faSStephen Warren ret = soc_probe_link_dais(card, i, order); 1715fe3e78e0SMark Brown if (ret < 0) { 1716f110bfc7SLiam Girdwood dev_err(card->dev, 1717f110bfc7SLiam Girdwood "ASoC: failed to instantiate card %d\n", 1718f110bfc7SLiam Girdwood ret); 1719f0fba2adSLiam Girdwood goto probe_dai_err; 1720fe3e78e0SMark Brown } 1721fe3e78e0SMark Brown } 17220168bf0dSLiam Girdwood } 1723fe3e78e0SMark Brown 17242eea392dSJarkko Nikula for (i = 0; i < card->num_aux_devs; i++) { 17252eea392dSJarkko Nikula ret = soc_probe_aux_dev(card, i); 17262eea392dSJarkko Nikula if (ret < 0) { 1727f110bfc7SLiam Girdwood dev_err(card->dev, 1728f110bfc7SLiam Girdwood "ASoC: failed to add auxiliary devices %d\n", 1729f110bfc7SLiam Girdwood ret); 17302eea392dSJarkko Nikula goto probe_aux_dev_err; 17312eea392dSJarkko Nikula } 17322eea392dSJarkko Nikula } 17332eea392dSJarkko Nikula 1734888df395SMark Brown snd_soc_dapm_link_dai_widgets(card); 1735888df395SMark Brown 1736b7af1dafSMark Brown if (card->controls) 1737022658beSLiam Girdwood snd_soc_add_card_controls(card, card->controls, card->num_controls); 1738b7af1dafSMark Brown 1739b8ad29deSMark Brown if (card->dapm_routes) 1740b8ad29deSMark Brown snd_soc_dapm_add_routes(&card->dapm, card->dapm_routes, 1741b8ad29deSMark Brown card->num_dapm_routes); 1742b8ad29deSMark Brown 174375d9ac46SMark Brown for (i = 0; i < card->num_links; i++) { 174475d9ac46SMark Brown dai_link = &card->dai_link[i]; 1745f04209a7SMark Brown dai_fmt = dai_link->dai_fmt; 174675d9ac46SMark Brown 1747f04209a7SMark Brown if (dai_fmt) { 174875d9ac46SMark Brown ret = snd_soc_dai_set_fmt(card->rtd[i].codec_dai, 1749f04209a7SMark Brown dai_fmt); 17505e4ba569SShawn Guo if (ret != 0 && ret != -ENOTSUPP) 175175d9ac46SMark Brown dev_warn(card->rtd[i].codec_dai->dev, 1752f110bfc7SLiam Girdwood "ASoC: Failed to set DAI format: %d\n", 175375d9ac46SMark Brown ret); 1754f04209a7SMark Brown } 1755f04209a7SMark Brown 1756f04209a7SMark Brown /* If this is a regular CPU link there will be a platform */ 1757fe33d4c5SStephen Warren if (dai_fmt && 1758fe33d4c5SStephen Warren (dai_link->platform_name || dai_link->platform_of_node)) { 1759f04209a7SMark Brown ret = snd_soc_dai_set_fmt(card->rtd[i].cpu_dai, 1760f04209a7SMark Brown dai_fmt); 1761f04209a7SMark Brown if (ret != 0 && ret != -ENOTSUPP) 1762f04209a7SMark Brown dev_warn(card->rtd[i].cpu_dai->dev, 1763f110bfc7SLiam Girdwood "ASoC: Failed to set DAI format: %d\n", 1764f04209a7SMark Brown ret); 1765f04209a7SMark Brown } else if (dai_fmt) { 1766f04209a7SMark Brown /* Flip the polarity for the "CPU" end */ 1767f04209a7SMark Brown dai_fmt &= ~SND_SOC_DAIFMT_MASTER_MASK; 1768f04209a7SMark Brown switch (dai_link->dai_fmt & 1769f04209a7SMark Brown SND_SOC_DAIFMT_MASTER_MASK) { 1770f04209a7SMark Brown case SND_SOC_DAIFMT_CBM_CFM: 1771f04209a7SMark Brown dai_fmt |= SND_SOC_DAIFMT_CBS_CFS; 1772f04209a7SMark Brown break; 1773f04209a7SMark Brown case SND_SOC_DAIFMT_CBM_CFS: 1774f04209a7SMark Brown dai_fmt |= SND_SOC_DAIFMT_CBS_CFM; 1775f04209a7SMark Brown break; 1776f04209a7SMark Brown case SND_SOC_DAIFMT_CBS_CFM: 1777f04209a7SMark Brown dai_fmt |= SND_SOC_DAIFMT_CBM_CFS; 1778f04209a7SMark Brown break; 1779f04209a7SMark Brown case SND_SOC_DAIFMT_CBS_CFS: 1780f04209a7SMark Brown dai_fmt |= SND_SOC_DAIFMT_CBM_CFM; 1781f04209a7SMark Brown break; 1782f04209a7SMark Brown } 178375d9ac46SMark Brown 178475d9ac46SMark Brown ret = snd_soc_dai_set_fmt(card->rtd[i].cpu_dai, 1785f04209a7SMark Brown dai_fmt); 17865e4ba569SShawn Guo if (ret != 0 && ret != -ENOTSUPP) 178775d9ac46SMark Brown dev_warn(card->rtd[i].cpu_dai->dev, 1788f110bfc7SLiam Girdwood "ASoC: Failed to set DAI format: %d\n", 178975d9ac46SMark Brown ret); 179075d9ac46SMark Brown } 179175d9ac46SMark Brown } 179275d9ac46SMark Brown 1793f0fba2adSLiam Girdwood snprintf(card->snd_card->shortname, sizeof(card->snd_card->shortname), 1794fe3e78e0SMark Brown "%s", card->name); 1795f0fba2adSLiam Girdwood snprintf(card->snd_card->longname, sizeof(card->snd_card->longname), 179622de71baSLiam Girdwood "%s", card->long_name ? card->long_name : card->name); 1797f0e8ed85SMark Brown snprintf(card->snd_card->driver, sizeof(card->snd_card->driver), 1798f0e8ed85SMark Brown "%s", card->driver_name ? card->driver_name : card->name); 1799f0e8ed85SMark Brown for (i = 0; i < ARRAY_SIZE(card->snd_card->driver); i++) { 1800f0e8ed85SMark Brown switch (card->snd_card->driver[i]) { 1801f0e8ed85SMark Brown case '_': 1802f0e8ed85SMark Brown case '-': 1803f0e8ed85SMark Brown case '\0': 1804f0e8ed85SMark Brown break; 1805f0e8ed85SMark Brown default: 1806f0e8ed85SMark Brown if (!isalnum(card->snd_card->driver[i])) 1807f0e8ed85SMark Brown card->snd_card->driver[i] = '_'; 1808f0e8ed85SMark Brown break; 1809f0e8ed85SMark Brown } 1810f0e8ed85SMark Brown } 1811fe3e78e0SMark Brown 181228e9ad92SMark Brown if (card->late_probe) { 181328e9ad92SMark Brown ret = card->late_probe(card); 181428e9ad92SMark Brown if (ret < 0) { 1815f110bfc7SLiam Girdwood dev_err(card->dev, "ASoC: %s late_probe() failed: %d\n", 181628e9ad92SMark Brown card->name, ret); 181728e9ad92SMark Brown goto probe_aux_dev_err; 181828e9ad92SMark Brown } 181928e9ad92SMark Brown } 182028e9ad92SMark Brown 18211633281bSStephen Warren if (card->fully_routed) 1822b05d8dc1SMark Brown list_for_each_entry(codec, &card->codec_dev_list, card_list) 18231633281bSStephen Warren snd_soc_dapm_auto_nc_codec_pins(codec); 18241633281bSStephen Warren 1825824ef826SLars-Peter Clausen snd_soc_dapm_new_widgets(card); 18268c193b8dSLars-Peter Clausen 1827f0fba2adSLiam Girdwood ret = snd_card_register(card->snd_card); 1828fe3e78e0SMark Brown if (ret < 0) { 1829f110bfc7SLiam Girdwood dev_err(card->dev, "ASoC: failed to register soundcard %d\n", 1830f110bfc7SLiam Girdwood ret); 18316b3ed785SAxel Lin goto probe_aux_dev_err; 1832fe3e78e0SMark Brown } 1833fe3e78e0SMark Brown 1834fe3e78e0SMark Brown #ifdef CONFIG_SND_SOC_AC97_BUS 1835f0fba2adSLiam Girdwood /* register any AC97 codecs */ 1836f0fba2adSLiam Girdwood for (i = 0; i < card->num_rtd; i++) { 1837f0fba2adSLiam Girdwood ret = soc_register_ac97_dai_link(&card->rtd[i]); 1838fe3e78e0SMark Brown if (ret < 0) { 183910e8aa9aSMichał Mirosław dev_err(card->dev, 184010e8aa9aSMichał Mirosław "ASoC: failed to register AC97: %d\n", ret); 18416b3ed785SAxel Lin while (--i >= 0) 18427d8316dfSMark Brown soc_unregister_ac97_dai_link(card->rtd[i].codec); 18436b3ed785SAxel Lin goto probe_aux_dev_err; 1844fe3e78e0SMark Brown } 1845fe3e78e0SMark Brown } 1846fe3e78e0SMark Brown #endif 1847fe3e78e0SMark Brown 1848435c5e25SMark Brown card->instantiated = 1; 18494f4c0072SMark Brown snd_soc_dapm_sync(&card->dapm); 1850f0fba2adSLiam Girdwood mutex_unlock(&card->mutex); 1851b19e6e7bSMark Brown 1852b19e6e7bSMark Brown return 0; 1853db2a4165SFrank Mandarino 18542eea392dSJarkko Nikula probe_aux_dev_err: 18552eea392dSJarkko Nikula for (i = 0; i < card->num_aux_devs; i++) 18562eea392dSJarkko Nikula soc_remove_aux_dev(card, i); 18572eea392dSJarkko Nikula 1858f0fba2adSLiam Girdwood probe_dai_err: 18590671fd8eSKuninori Morimoto soc_remove_dai_links(card); 1860fe3e78e0SMark Brown 1861f0fba2adSLiam Girdwood card_probe_error: 186287506549SMark Brown if (card->remove) 1863e7361ec4SMark Brown card->remove(card); 1864f0fba2adSLiam Girdwood 1865f0fba2adSLiam Girdwood snd_card_free(card->snd_card); 1866f0fba2adSLiam Girdwood 1867b19e6e7bSMark Brown base_error: 1868f0fba2adSLiam Girdwood mutex_unlock(&card->mutex); 1869db2a4165SFrank Mandarino 1870b19e6e7bSMark Brown return ret; 1871435c5e25SMark Brown } 1872435c5e25SMark Brown 1873435c5e25SMark Brown /* probes a new socdev */ 1874435c5e25SMark Brown static int soc_probe(struct platform_device *pdev) 1875435c5e25SMark Brown { 1876f0fba2adSLiam Girdwood struct snd_soc_card *card = platform_get_drvdata(pdev); 1877435c5e25SMark Brown 187870a7ca34SVinod Koul /* 187970a7ca34SVinod Koul * no card, so machine driver should be registering card 188070a7ca34SVinod Koul * we should not be here in that case so ret error 188170a7ca34SVinod Koul */ 188270a7ca34SVinod Koul if (!card) 188370a7ca34SVinod Koul return -EINVAL; 188470a7ca34SVinod Koul 1885fe4085e8SMark Brown dev_warn(&pdev->dev, 1886f110bfc7SLiam Girdwood "ASoC: machine %s should use snd_soc_register_card()\n", 1887fe4085e8SMark Brown card->name); 1888fe4085e8SMark Brown 1889435c5e25SMark Brown /* Bodge while we unpick instantiation */ 1890435c5e25SMark Brown card->dev = &pdev->dev; 1891f0fba2adSLiam Girdwood 189228d528c8SMark Brown return snd_soc_register_card(card); 1893435c5e25SMark Brown } 1894435c5e25SMark Brown 1895b0e26485SVinod Koul static int soc_cleanup_card_resources(struct snd_soc_card *card) 1896db2a4165SFrank Mandarino { 1897db2a4165SFrank Mandarino int i; 1898db2a4165SFrank Mandarino 1899f0fba2adSLiam Girdwood /* make sure any delayed work runs */ 1900f0fba2adSLiam Girdwood for (i = 0; i < card->num_rtd; i++) { 1901f0fba2adSLiam Girdwood struct snd_soc_pcm_runtime *rtd = &card->rtd[i]; 190243829731STejun Heo flush_delayed_work(&rtd->delayed_work); 1903db2a4165SFrank Mandarino } 1904db2a4165SFrank Mandarino 19052eea392dSJarkko Nikula /* remove auxiliary devices */ 19062eea392dSJarkko Nikula for (i = 0; i < card->num_aux_devs; i++) 19072eea392dSJarkko Nikula soc_remove_aux_dev(card, i); 19082eea392dSJarkko Nikula 1909f0fba2adSLiam Girdwood /* remove and free each DAI */ 19100671fd8eSKuninori Morimoto soc_remove_dai_links(card); 1911f0fba2adSLiam Girdwood 1912a6052154SJarkko Nikula soc_cleanup_card_debugfs(card); 1913a6052154SJarkko Nikula 1914f0fba2adSLiam Girdwood /* remove the card */ 191587506549SMark Brown if (card->remove) 1916e7361ec4SMark Brown card->remove(card); 1917f0fba2adSLiam Girdwood 19180aaae527SLars-Peter Clausen snd_soc_dapm_free(&card->dapm); 19190aaae527SLars-Peter Clausen 1920f0fba2adSLiam Girdwood snd_card_free(card->snd_card); 1921b0e26485SVinod Koul return 0; 1922b0e26485SVinod Koul 1923b2dfa62cSGuennadi Liakhovetski } 1924b0e26485SVinod Koul 1925b0e26485SVinod Koul /* removes a socdev */ 1926b0e26485SVinod Koul static int soc_remove(struct platform_device *pdev) 1927b0e26485SVinod Koul { 1928b0e26485SVinod Koul struct snd_soc_card *card = platform_get_drvdata(pdev); 1929b0e26485SVinod Koul 1930c5af3a2eSMark Brown snd_soc_unregister_card(card); 1931db2a4165SFrank Mandarino return 0; 1932db2a4165SFrank Mandarino } 1933db2a4165SFrank Mandarino 19346f8ab4acSMark Brown int snd_soc_poweroff(struct device *dev) 193551737470SMark Brown { 19366f8ab4acSMark Brown struct snd_soc_card *card = dev_get_drvdata(dev); 1937f0fba2adSLiam Girdwood int i; 193851737470SMark Brown 193951737470SMark Brown if (!card->instantiated) 1940416356fcSMark Brown return 0; 194151737470SMark Brown 194251737470SMark Brown /* Flush out pmdown_time work - we actually do want to run it 194351737470SMark Brown * now, we're shutting down so no imminent restart. */ 1944f0fba2adSLiam Girdwood for (i = 0; i < card->num_rtd; i++) { 1945f0fba2adSLiam Girdwood struct snd_soc_pcm_runtime *rtd = &card->rtd[i]; 194643829731STejun Heo flush_delayed_work(&rtd->delayed_work); 1947f0fba2adSLiam Girdwood } 194851737470SMark Brown 1949f0fba2adSLiam Girdwood snd_soc_dapm_shutdown(card); 1950416356fcSMark Brown 1951416356fcSMark Brown return 0; 195251737470SMark Brown } 19536f8ab4acSMark Brown EXPORT_SYMBOL_GPL(snd_soc_poweroff); 195451737470SMark Brown 19556f8ab4acSMark Brown const struct dev_pm_ops snd_soc_pm_ops = { 1956b1dd5897SViresh Kumar .suspend = snd_soc_suspend, 1957b1dd5897SViresh Kumar .resume = snd_soc_resume, 1958b1dd5897SViresh Kumar .freeze = snd_soc_suspend, 1959b1dd5897SViresh Kumar .thaw = snd_soc_resume, 19606f8ab4acSMark Brown .poweroff = snd_soc_poweroff, 1961b1dd5897SViresh Kumar .restore = snd_soc_resume, 1962416356fcSMark Brown }; 1963deb2607eSStephen Warren EXPORT_SYMBOL_GPL(snd_soc_pm_ops); 1964416356fcSMark Brown 1965db2a4165SFrank Mandarino /* ASoC platform driver */ 1966db2a4165SFrank Mandarino static struct platform_driver soc_driver = { 1967db2a4165SFrank Mandarino .driver = { 1968db2a4165SFrank Mandarino .name = "soc-audio", 19698b45a209SKay Sievers .owner = THIS_MODULE, 19706f8ab4acSMark Brown .pm = &snd_soc_pm_ops, 1971db2a4165SFrank Mandarino }, 1972db2a4165SFrank Mandarino .probe = soc_probe, 1973db2a4165SFrank Mandarino .remove = soc_remove, 1974db2a4165SFrank Mandarino }; 1975db2a4165SFrank Mandarino 1976096e49d5SMark Brown /** 1977096e49d5SMark Brown * snd_soc_codec_volatile_register: Report if a register is volatile. 1978096e49d5SMark Brown * 1979096e49d5SMark Brown * @codec: CODEC to query. 1980096e49d5SMark Brown * @reg: Register to query. 1981096e49d5SMark Brown * 1982096e49d5SMark Brown * Boolean function indiciating if a CODEC register is volatile. 1983096e49d5SMark Brown */ 1984181e055eSMark Brown int snd_soc_codec_volatile_register(struct snd_soc_codec *codec, 1985181e055eSMark Brown unsigned int reg) 1986096e49d5SMark Brown { 19871500b7b5SDimitris Papastamos if (codec->volatile_register) 19881500b7b5SDimitris Papastamos return codec->volatile_register(codec, reg); 1989096e49d5SMark Brown else 1990096e49d5SMark Brown return 0; 1991096e49d5SMark Brown } 1992096e49d5SMark Brown EXPORT_SYMBOL_GPL(snd_soc_codec_volatile_register); 1993096e49d5SMark Brown 1994db2a4165SFrank Mandarino /** 1995239c9706SDimitris Papastamos * snd_soc_codec_readable_register: Report if a register is readable. 1996239c9706SDimitris Papastamos * 1997239c9706SDimitris Papastamos * @codec: CODEC to query. 1998239c9706SDimitris Papastamos * @reg: Register to query. 1999239c9706SDimitris Papastamos * 2000239c9706SDimitris Papastamos * Boolean function indicating if a CODEC register is readable. 2001239c9706SDimitris Papastamos */ 2002239c9706SDimitris Papastamos int snd_soc_codec_readable_register(struct snd_soc_codec *codec, 2003239c9706SDimitris Papastamos unsigned int reg) 2004239c9706SDimitris Papastamos { 2005239c9706SDimitris Papastamos if (codec->readable_register) 2006239c9706SDimitris Papastamos return codec->readable_register(codec, reg); 2007239c9706SDimitris Papastamos else 200863fa0a28SLars-Peter Clausen return 1; 2009239c9706SDimitris Papastamos } 2010239c9706SDimitris Papastamos EXPORT_SYMBOL_GPL(snd_soc_codec_readable_register); 2011239c9706SDimitris Papastamos 2012239c9706SDimitris Papastamos /** 2013239c9706SDimitris Papastamos * snd_soc_codec_writable_register: Report if a register is writable. 2014239c9706SDimitris Papastamos * 2015239c9706SDimitris Papastamos * @codec: CODEC to query. 2016239c9706SDimitris Papastamos * @reg: Register to query. 2017239c9706SDimitris Papastamos * 2018239c9706SDimitris Papastamos * Boolean function indicating if a CODEC register is writable. 2019239c9706SDimitris Papastamos */ 2020239c9706SDimitris Papastamos int snd_soc_codec_writable_register(struct snd_soc_codec *codec, 2021239c9706SDimitris Papastamos unsigned int reg) 2022239c9706SDimitris Papastamos { 2023239c9706SDimitris Papastamos if (codec->writable_register) 2024239c9706SDimitris Papastamos return codec->writable_register(codec, reg); 2025239c9706SDimitris Papastamos else 202663fa0a28SLars-Peter Clausen return 1; 2027239c9706SDimitris Papastamos } 2028239c9706SDimitris Papastamos EXPORT_SYMBOL_GPL(snd_soc_codec_writable_register); 2029239c9706SDimitris Papastamos 2030f1442bc1SLiam Girdwood int snd_soc_platform_read(struct snd_soc_platform *platform, 2031f1442bc1SLiam Girdwood unsigned int reg) 2032f1442bc1SLiam Girdwood { 2033f1442bc1SLiam Girdwood unsigned int ret; 2034f1442bc1SLiam Girdwood 2035f1442bc1SLiam Girdwood if (!platform->driver->read) { 2036f110bfc7SLiam Girdwood dev_err(platform->dev, "ASoC: platform has no read back\n"); 2037f1442bc1SLiam Girdwood return -1; 2038f1442bc1SLiam Girdwood } 2039f1442bc1SLiam Girdwood 2040f1442bc1SLiam Girdwood ret = platform->driver->read(platform, reg); 2041f1442bc1SLiam Girdwood dev_dbg(platform->dev, "read %x => %x\n", reg, ret); 2042a82ce2aeSLiam Girdwood trace_snd_soc_preg_read(platform, reg, ret); 2043f1442bc1SLiam Girdwood 2044f1442bc1SLiam Girdwood return ret; 2045f1442bc1SLiam Girdwood } 2046f1442bc1SLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_platform_read); 2047f1442bc1SLiam Girdwood 2048f1442bc1SLiam Girdwood int snd_soc_platform_write(struct snd_soc_platform *platform, 2049f1442bc1SLiam Girdwood unsigned int reg, unsigned int val) 2050f1442bc1SLiam Girdwood { 2051f1442bc1SLiam Girdwood if (!platform->driver->write) { 2052f110bfc7SLiam Girdwood dev_err(platform->dev, "ASoC: platform has no write back\n"); 2053f1442bc1SLiam Girdwood return -1; 2054f1442bc1SLiam Girdwood } 2055f1442bc1SLiam Girdwood 2056f1442bc1SLiam Girdwood dev_dbg(platform->dev, "write %x = %x\n", reg, val); 2057a82ce2aeSLiam Girdwood trace_snd_soc_preg_write(platform, reg, val); 2058f1442bc1SLiam Girdwood return platform->driver->write(platform, reg, val); 2059f1442bc1SLiam Girdwood } 2060f1442bc1SLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_platform_write); 2061f1442bc1SLiam Girdwood 2062239c9706SDimitris Papastamos /** 2063db2a4165SFrank Mandarino * snd_soc_new_ac97_codec - initailise AC97 device 2064db2a4165SFrank Mandarino * @codec: audio codec 2065db2a4165SFrank Mandarino * @ops: AC97 bus operations 2066db2a4165SFrank Mandarino * @num: AC97 codec number 2067db2a4165SFrank Mandarino * 2068db2a4165SFrank Mandarino * Initialises AC97 codec resources for use by ad-hoc devices only. 2069db2a4165SFrank Mandarino */ 2070db2a4165SFrank Mandarino int snd_soc_new_ac97_codec(struct snd_soc_codec *codec, 2071db2a4165SFrank Mandarino struct snd_ac97_bus_ops *ops, int num) 2072db2a4165SFrank Mandarino { 2073db2a4165SFrank Mandarino mutex_lock(&codec->mutex); 2074db2a4165SFrank Mandarino 2075db2a4165SFrank Mandarino codec->ac97 = kzalloc(sizeof(struct snd_ac97), GFP_KERNEL); 2076db2a4165SFrank Mandarino if (codec->ac97 == NULL) { 2077db2a4165SFrank Mandarino mutex_unlock(&codec->mutex); 2078db2a4165SFrank Mandarino return -ENOMEM; 2079db2a4165SFrank Mandarino } 2080db2a4165SFrank Mandarino 2081db2a4165SFrank Mandarino codec->ac97->bus = kzalloc(sizeof(struct snd_ac97_bus), GFP_KERNEL); 2082db2a4165SFrank Mandarino if (codec->ac97->bus == NULL) { 2083db2a4165SFrank Mandarino kfree(codec->ac97); 2084db2a4165SFrank Mandarino codec->ac97 = NULL; 2085db2a4165SFrank Mandarino mutex_unlock(&codec->mutex); 2086db2a4165SFrank Mandarino return -ENOMEM; 2087db2a4165SFrank Mandarino } 2088db2a4165SFrank Mandarino 2089db2a4165SFrank Mandarino codec->ac97->bus->ops = ops; 2090db2a4165SFrank Mandarino codec->ac97->num = num; 20910562f788SMika Westerberg 20920562f788SMika Westerberg /* 20930562f788SMika Westerberg * Mark the AC97 device to be created by us. This way we ensure that the 20940562f788SMika Westerberg * device will be registered with the device subsystem later on. 20950562f788SMika Westerberg */ 20960562f788SMika Westerberg codec->ac97_created = 1; 20970562f788SMika Westerberg 2098db2a4165SFrank Mandarino mutex_unlock(&codec->mutex); 2099db2a4165SFrank Mandarino return 0; 2100db2a4165SFrank Mandarino } 2101db2a4165SFrank Mandarino EXPORT_SYMBOL_GPL(snd_soc_new_ac97_codec); 2102db2a4165SFrank Mandarino 2103741a509fSMarkus Pargmann static struct snd_ac97_reset_cfg snd_ac97_rst_cfg; 2104741a509fSMarkus Pargmann 2105741a509fSMarkus Pargmann static void snd_soc_ac97_warm_reset(struct snd_ac97 *ac97) 2106741a509fSMarkus Pargmann { 2107741a509fSMarkus Pargmann struct pinctrl *pctl = snd_ac97_rst_cfg.pctl; 2108741a509fSMarkus Pargmann 2109741a509fSMarkus Pargmann pinctrl_select_state(pctl, snd_ac97_rst_cfg.pstate_warm_reset); 2110741a509fSMarkus Pargmann 2111741a509fSMarkus Pargmann gpio_direction_output(snd_ac97_rst_cfg.gpio_sync, 1); 2112741a509fSMarkus Pargmann 2113741a509fSMarkus Pargmann udelay(10); 2114741a509fSMarkus Pargmann 2115741a509fSMarkus Pargmann gpio_direction_output(snd_ac97_rst_cfg.gpio_sync, 0); 2116741a509fSMarkus Pargmann 2117741a509fSMarkus Pargmann pinctrl_select_state(pctl, snd_ac97_rst_cfg.pstate_run); 2118741a509fSMarkus Pargmann msleep(2); 2119741a509fSMarkus Pargmann } 2120741a509fSMarkus Pargmann 2121741a509fSMarkus Pargmann static void snd_soc_ac97_reset(struct snd_ac97 *ac97) 2122741a509fSMarkus Pargmann { 2123741a509fSMarkus Pargmann struct pinctrl *pctl = snd_ac97_rst_cfg.pctl; 2124741a509fSMarkus Pargmann 2125741a509fSMarkus Pargmann pinctrl_select_state(pctl, snd_ac97_rst_cfg.pstate_reset); 2126741a509fSMarkus Pargmann 2127741a509fSMarkus Pargmann gpio_direction_output(snd_ac97_rst_cfg.gpio_sync, 0); 2128741a509fSMarkus Pargmann gpio_direction_output(snd_ac97_rst_cfg.gpio_sdata, 0); 2129741a509fSMarkus Pargmann gpio_direction_output(snd_ac97_rst_cfg.gpio_reset, 0); 2130741a509fSMarkus Pargmann 2131741a509fSMarkus Pargmann udelay(10); 2132741a509fSMarkus Pargmann 2133741a509fSMarkus Pargmann gpio_direction_output(snd_ac97_rst_cfg.gpio_reset, 1); 2134741a509fSMarkus Pargmann 2135741a509fSMarkus Pargmann pinctrl_select_state(pctl, snd_ac97_rst_cfg.pstate_run); 2136741a509fSMarkus Pargmann msleep(2); 2137741a509fSMarkus Pargmann } 2138741a509fSMarkus Pargmann 2139741a509fSMarkus Pargmann static int snd_soc_ac97_parse_pinctl(struct device *dev, 2140741a509fSMarkus Pargmann struct snd_ac97_reset_cfg *cfg) 2141741a509fSMarkus Pargmann { 2142741a509fSMarkus Pargmann struct pinctrl *p; 2143741a509fSMarkus Pargmann struct pinctrl_state *state; 2144741a509fSMarkus Pargmann int gpio; 2145741a509fSMarkus Pargmann int ret; 2146741a509fSMarkus Pargmann 2147741a509fSMarkus Pargmann p = devm_pinctrl_get(dev); 2148741a509fSMarkus Pargmann if (IS_ERR(p)) { 2149741a509fSMarkus Pargmann dev_err(dev, "Failed to get pinctrl\n"); 2150741a509fSMarkus Pargmann return PTR_RET(p); 2151741a509fSMarkus Pargmann } 2152741a509fSMarkus Pargmann cfg->pctl = p; 2153741a509fSMarkus Pargmann 2154741a509fSMarkus Pargmann state = pinctrl_lookup_state(p, "ac97-reset"); 2155741a509fSMarkus Pargmann if (IS_ERR(state)) { 2156741a509fSMarkus Pargmann dev_err(dev, "Can't find pinctrl state ac97-reset\n"); 2157741a509fSMarkus Pargmann return PTR_RET(state); 2158741a509fSMarkus Pargmann } 2159741a509fSMarkus Pargmann cfg->pstate_reset = state; 2160741a509fSMarkus Pargmann 2161741a509fSMarkus Pargmann state = pinctrl_lookup_state(p, "ac97-warm-reset"); 2162741a509fSMarkus Pargmann if (IS_ERR(state)) { 2163741a509fSMarkus Pargmann dev_err(dev, "Can't find pinctrl state ac97-warm-reset\n"); 2164741a509fSMarkus Pargmann return PTR_RET(state); 2165741a509fSMarkus Pargmann } 2166741a509fSMarkus Pargmann cfg->pstate_warm_reset = state; 2167741a509fSMarkus Pargmann 2168741a509fSMarkus Pargmann state = pinctrl_lookup_state(p, "ac97-running"); 2169741a509fSMarkus Pargmann if (IS_ERR(state)) { 2170741a509fSMarkus Pargmann dev_err(dev, "Can't find pinctrl state ac97-running\n"); 2171741a509fSMarkus Pargmann return PTR_RET(state); 2172741a509fSMarkus Pargmann } 2173741a509fSMarkus Pargmann cfg->pstate_run = state; 2174741a509fSMarkus Pargmann 2175741a509fSMarkus Pargmann gpio = of_get_named_gpio(dev->of_node, "ac97-gpios", 0); 2176741a509fSMarkus Pargmann if (gpio < 0) { 2177741a509fSMarkus Pargmann dev_err(dev, "Can't find ac97-sync gpio\n"); 2178741a509fSMarkus Pargmann return gpio; 2179741a509fSMarkus Pargmann } 2180741a509fSMarkus Pargmann ret = devm_gpio_request(dev, gpio, "AC97 link sync"); 2181741a509fSMarkus Pargmann if (ret) { 2182741a509fSMarkus Pargmann dev_err(dev, "Failed requesting ac97-sync gpio\n"); 2183741a509fSMarkus Pargmann return ret; 2184741a509fSMarkus Pargmann } 2185741a509fSMarkus Pargmann cfg->gpio_sync = gpio; 2186741a509fSMarkus Pargmann 2187741a509fSMarkus Pargmann gpio = of_get_named_gpio(dev->of_node, "ac97-gpios", 1); 2188741a509fSMarkus Pargmann if (gpio < 0) { 2189741a509fSMarkus Pargmann dev_err(dev, "Can't find ac97-sdata gpio %d\n", gpio); 2190741a509fSMarkus Pargmann return gpio; 2191741a509fSMarkus Pargmann } 2192741a509fSMarkus Pargmann ret = devm_gpio_request(dev, gpio, "AC97 link sdata"); 2193741a509fSMarkus Pargmann if (ret) { 2194741a509fSMarkus Pargmann dev_err(dev, "Failed requesting ac97-sdata gpio\n"); 2195741a509fSMarkus Pargmann return ret; 2196741a509fSMarkus Pargmann } 2197741a509fSMarkus Pargmann cfg->gpio_sdata = gpio; 2198741a509fSMarkus Pargmann 2199741a509fSMarkus Pargmann gpio = of_get_named_gpio(dev->of_node, "ac97-gpios", 2); 2200741a509fSMarkus Pargmann if (gpio < 0) { 2201741a509fSMarkus Pargmann dev_err(dev, "Can't find ac97-reset gpio\n"); 2202741a509fSMarkus Pargmann return gpio; 2203741a509fSMarkus Pargmann } 2204741a509fSMarkus Pargmann ret = devm_gpio_request(dev, gpio, "AC97 link reset"); 2205741a509fSMarkus Pargmann if (ret) { 2206741a509fSMarkus Pargmann dev_err(dev, "Failed requesting ac97-reset gpio\n"); 2207741a509fSMarkus Pargmann return ret; 2208741a509fSMarkus Pargmann } 2209741a509fSMarkus Pargmann cfg->gpio_reset = gpio; 2210741a509fSMarkus Pargmann 2211741a509fSMarkus Pargmann return 0; 2212741a509fSMarkus Pargmann } 2213741a509fSMarkus Pargmann 2214b047e1ccSMark Brown struct snd_ac97_bus_ops *soc_ac97_ops; 2215f74b5e25SKevin Hilman EXPORT_SYMBOL_GPL(soc_ac97_ops); 2216b047e1ccSMark Brown 2217b047e1ccSMark Brown int snd_soc_set_ac97_ops(struct snd_ac97_bus_ops *ops) 2218b047e1ccSMark Brown { 2219b047e1ccSMark Brown if (ops == soc_ac97_ops) 2220b047e1ccSMark Brown return 0; 2221b047e1ccSMark Brown 2222b047e1ccSMark Brown if (soc_ac97_ops && ops) 2223b047e1ccSMark Brown return -EBUSY; 2224b047e1ccSMark Brown 2225b047e1ccSMark Brown soc_ac97_ops = ops; 2226b047e1ccSMark Brown 2227b047e1ccSMark Brown return 0; 2228b047e1ccSMark Brown } 2229b047e1ccSMark Brown EXPORT_SYMBOL_GPL(snd_soc_set_ac97_ops); 2230b047e1ccSMark Brown 2231db2a4165SFrank Mandarino /** 2232741a509fSMarkus Pargmann * snd_soc_set_ac97_ops_of_reset - Set ac97 ops with generic ac97 reset functions 2233741a509fSMarkus Pargmann * 2234741a509fSMarkus Pargmann * This function sets the reset and warm_reset properties of ops and parses 2235741a509fSMarkus Pargmann * the device node of pdev to get pinctrl states and gpio numbers to use. 2236741a509fSMarkus Pargmann */ 2237741a509fSMarkus Pargmann int snd_soc_set_ac97_ops_of_reset(struct snd_ac97_bus_ops *ops, 2238741a509fSMarkus Pargmann struct platform_device *pdev) 2239741a509fSMarkus Pargmann { 2240741a509fSMarkus Pargmann struct device *dev = &pdev->dev; 2241741a509fSMarkus Pargmann struct snd_ac97_reset_cfg cfg; 2242741a509fSMarkus Pargmann int ret; 2243741a509fSMarkus Pargmann 2244741a509fSMarkus Pargmann ret = snd_soc_ac97_parse_pinctl(dev, &cfg); 2245741a509fSMarkus Pargmann if (ret) 2246741a509fSMarkus Pargmann return ret; 2247741a509fSMarkus Pargmann 2248741a509fSMarkus Pargmann ret = snd_soc_set_ac97_ops(ops); 2249741a509fSMarkus Pargmann if (ret) 2250741a509fSMarkus Pargmann return ret; 2251741a509fSMarkus Pargmann 2252741a509fSMarkus Pargmann ops->warm_reset = snd_soc_ac97_warm_reset; 2253741a509fSMarkus Pargmann ops->reset = snd_soc_ac97_reset; 2254741a509fSMarkus Pargmann 2255741a509fSMarkus Pargmann snd_ac97_rst_cfg = cfg; 2256741a509fSMarkus Pargmann return 0; 2257741a509fSMarkus Pargmann } 2258741a509fSMarkus Pargmann EXPORT_SYMBOL_GPL(snd_soc_set_ac97_ops_of_reset); 2259741a509fSMarkus Pargmann 2260741a509fSMarkus Pargmann /** 2261db2a4165SFrank Mandarino * snd_soc_free_ac97_codec - free AC97 codec device 2262db2a4165SFrank Mandarino * @codec: audio codec 2263db2a4165SFrank Mandarino * 2264db2a4165SFrank Mandarino * Frees AC97 codec device resources. 2265db2a4165SFrank Mandarino */ 2266db2a4165SFrank Mandarino void snd_soc_free_ac97_codec(struct snd_soc_codec *codec) 2267db2a4165SFrank Mandarino { 2268db2a4165SFrank Mandarino mutex_lock(&codec->mutex); 2269f0fba2adSLiam Girdwood #ifdef CONFIG_SND_SOC_AC97_BUS 2270f0fba2adSLiam Girdwood soc_unregister_ac97_dai_link(codec); 2271f0fba2adSLiam Girdwood #endif 2272db2a4165SFrank Mandarino kfree(codec->ac97->bus); 2273db2a4165SFrank Mandarino kfree(codec->ac97); 2274db2a4165SFrank Mandarino codec->ac97 = NULL; 22750562f788SMika Westerberg codec->ac97_created = 0; 2276db2a4165SFrank Mandarino mutex_unlock(&codec->mutex); 2277db2a4165SFrank Mandarino } 2278db2a4165SFrank Mandarino EXPORT_SYMBOL_GPL(snd_soc_free_ac97_codec); 2279db2a4165SFrank Mandarino 2280c3753707SMark Brown unsigned int snd_soc_read(struct snd_soc_codec *codec, unsigned int reg) 2281c3753707SMark Brown { 2282c3753707SMark Brown unsigned int ret; 2283c3753707SMark Brown 2284c3acec26SMark Brown ret = codec->read(codec, reg); 2285c3753707SMark Brown dev_dbg(codec->dev, "read %x => %x\n", reg, ret); 2286a8b1d34fSMark Brown trace_snd_soc_reg_read(codec, reg, ret); 2287c3753707SMark Brown 2288c3753707SMark Brown return ret; 2289c3753707SMark Brown } 2290c3753707SMark Brown EXPORT_SYMBOL_GPL(snd_soc_read); 2291c3753707SMark Brown 2292c3753707SMark Brown unsigned int snd_soc_write(struct snd_soc_codec *codec, 2293c3753707SMark Brown unsigned int reg, unsigned int val) 2294c3753707SMark Brown { 2295c3753707SMark Brown dev_dbg(codec->dev, "write %x = %x\n", reg, val); 2296a8b1d34fSMark Brown trace_snd_soc_reg_write(codec, reg, val); 2297c3acec26SMark Brown return codec->write(codec, reg, val); 2298c3753707SMark Brown } 2299c3753707SMark Brown EXPORT_SYMBOL_GPL(snd_soc_write); 2300c3753707SMark Brown 23015fb609d4SDimitris Papastamos unsigned int snd_soc_bulk_write_raw(struct snd_soc_codec *codec, 23025fb609d4SDimitris Papastamos unsigned int reg, const void *data, size_t len) 23035fb609d4SDimitris Papastamos { 23045fb609d4SDimitris Papastamos return codec->bulk_write_raw(codec, reg, data, len); 23055fb609d4SDimitris Papastamos } 23065fb609d4SDimitris Papastamos EXPORT_SYMBOL_GPL(snd_soc_bulk_write_raw); 23075fb609d4SDimitris Papastamos 2308db2a4165SFrank Mandarino /** 2309db2a4165SFrank Mandarino * snd_soc_update_bits - update codec register bits 2310db2a4165SFrank Mandarino * @codec: audio codec 2311db2a4165SFrank Mandarino * @reg: codec register 2312db2a4165SFrank Mandarino * @mask: register mask 2313db2a4165SFrank Mandarino * @value: new value 2314db2a4165SFrank Mandarino * 2315db2a4165SFrank Mandarino * Writes new register value. 2316db2a4165SFrank Mandarino * 2317180c329dSTimur Tabi * Returns 1 for change, 0 for no change, or negative error code. 2318db2a4165SFrank Mandarino */ 2319db2a4165SFrank Mandarino int snd_soc_update_bits(struct snd_soc_codec *codec, unsigned short reg, 232046f5822fSDaniel Ribeiro unsigned int mask, unsigned int value) 2321db2a4165SFrank Mandarino { 23228a713da8SMark Brown bool change; 232346f5822fSDaniel Ribeiro unsigned int old, new; 2324180c329dSTimur Tabi int ret; 2325db2a4165SFrank Mandarino 23268a713da8SMark Brown if (codec->using_regmap) { 23278a713da8SMark Brown ret = regmap_update_bits_check(codec->control_data, reg, 23288a713da8SMark Brown mask, value, &change); 23298a713da8SMark Brown } else { 2330180c329dSTimur Tabi ret = snd_soc_read(codec, reg); 2331180c329dSTimur Tabi if (ret < 0) 2332180c329dSTimur Tabi return ret; 2333180c329dSTimur Tabi 2334180c329dSTimur Tabi old = ret; 233578bf3c9aSMark Brown new = (old & ~mask) | (value & mask); 2336db2a4165SFrank Mandarino change = old != new; 23378a713da8SMark Brown if (change) 2338180c329dSTimur Tabi ret = snd_soc_write(codec, reg, new); 23398a713da8SMark Brown } 23408a713da8SMark Brown 2341180c329dSTimur Tabi if (ret < 0) 2342180c329dSTimur Tabi return ret; 2343db2a4165SFrank Mandarino 2344db2a4165SFrank Mandarino return change; 2345db2a4165SFrank Mandarino } 2346db2a4165SFrank Mandarino EXPORT_SYMBOL_GPL(snd_soc_update_bits); 2347db2a4165SFrank Mandarino 2348db2a4165SFrank Mandarino /** 23496c508c62SEero Nurkkala * snd_soc_update_bits_locked - update codec register bits 23506c508c62SEero Nurkkala * @codec: audio codec 23516c508c62SEero Nurkkala * @reg: codec register 23526c508c62SEero Nurkkala * @mask: register mask 23536c508c62SEero Nurkkala * @value: new value 23546c508c62SEero Nurkkala * 23556c508c62SEero Nurkkala * Writes new register value, and takes the codec mutex. 23566c508c62SEero Nurkkala * 23576c508c62SEero Nurkkala * Returns 1 for change else 0. 23586c508c62SEero Nurkkala */ 2359dd1b3d53SMark Brown int snd_soc_update_bits_locked(struct snd_soc_codec *codec, 23606c508c62SEero Nurkkala unsigned short reg, unsigned int mask, 23616c508c62SEero Nurkkala unsigned int value) 23626c508c62SEero Nurkkala { 23636c508c62SEero Nurkkala int change; 23646c508c62SEero Nurkkala 23656c508c62SEero Nurkkala mutex_lock(&codec->mutex); 23666c508c62SEero Nurkkala change = snd_soc_update_bits(codec, reg, mask, value); 23676c508c62SEero Nurkkala mutex_unlock(&codec->mutex); 23686c508c62SEero Nurkkala 23696c508c62SEero Nurkkala return change; 23706c508c62SEero Nurkkala } 2371dd1b3d53SMark Brown EXPORT_SYMBOL_GPL(snd_soc_update_bits_locked); 23726c508c62SEero Nurkkala 23736c508c62SEero Nurkkala /** 2374db2a4165SFrank Mandarino * snd_soc_test_bits - test register for change 2375db2a4165SFrank Mandarino * @codec: audio codec 2376db2a4165SFrank Mandarino * @reg: codec register 2377db2a4165SFrank Mandarino * @mask: register mask 2378db2a4165SFrank Mandarino * @value: new value 2379db2a4165SFrank Mandarino * 2380db2a4165SFrank Mandarino * Tests a register with a new value and checks if the new value is 2381db2a4165SFrank Mandarino * different from the old value. 2382db2a4165SFrank Mandarino * 2383db2a4165SFrank Mandarino * Returns 1 for change else 0. 2384db2a4165SFrank Mandarino */ 2385db2a4165SFrank Mandarino int snd_soc_test_bits(struct snd_soc_codec *codec, unsigned short reg, 238646f5822fSDaniel Ribeiro unsigned int mask, unsigned int value) 2387db2a4165SFrank Mandarino { 2388db2a4165SFrank Mandarino int change; 238946f5822fSDaniel Ribeiro unsigned int old, new; 2390db2a4165SFrank Mandarino 2391db2a4165SFrank Mandarino old = snd_soc_read(codec, reg); 2392db2a4165SFrank Mandarino new = (old & ~mask) | value; 2393db2a4165SFrank Mandarino change = old != new; 2394db2a4165SFrank Mandarino 2395db2a4165SFrank Mandarino return change; 2396db2a4165SFrank Mandarino } 2397db2a4165SFrank Mandarino EXPORT_SYMBOL_GPL(snd_soc_test_bits); 2398db2a4165SFrank Mandarino 2399db2a4165SFrank Mandarino /** 2400db2a4165SFrank Mandarino * snd_soc_cnew - create new control 2401db2a4165SFrank Mandarino * @_template: control template 2402db2a4165SFrank Mandarino * @data: control private data 2403ac11a2b3SMark Brown * @long_name: control long name 2404efb7ac3fSMark Brown * @prefix: control name prefix 2405db2a4165SFrank Mandarino * 2406db2a4165SFrank Mandarino * Create a new mixer control from a template control. 2407db2a4165SFrank Mandarino * 2408db2a4165SFrank Mandarino * Returns 0 for success, else error. 2409db2a4165SFrank Mandarino */ 2410db2a4165SFrank Mandarino struct snd_kcontrol *snd_soc_cnew(const struct snd_kcontrol_new *_template, 24113056557fSMark Brown void *data, const char *long_name, 2412efb7ac3fSMark Brown const char *prefix) 2413db2a4165SFrank Mandarino { 2414db2a4165SFrank Mandarino struct snd_kcontrol_new template; 2415efb7ac3fSMark Brown struct snd_kcontrol *kcontrol; 2416efb7ac3fSMark Brown char *name = NULL; 2417db2a4165SFrank Mandarino 2418db2a4165SFrank Mandarino memcpy(&template, _template, sizeof(template)); 2419db2a4165SFrank Mandarino template.index = 0; 2420db2a4165SFrank Mandarino 2421efb7ac3fSMark Brown if (!long_name) 2422efb7ac3fSMark Brown long_name = template.name; 2423efb7ac3fSMark Brown 2424efb7ac3fSMark Brown if (prefix) { 24252b581074SLars-Peter Clausen name = kasprintf(GFP_KERNEL, "%s %s", prefix, long_name); 2426efb7ac3fSMark Brown if (!name) 2427efb7ac3fSMark Brown return NULL; 2428efb7ac3fSMark Brown 2429efb7ac3fSMark Brown template.name = name; 2430efb7ac3fSMark Brown } else { 2431efb7ac3fSMark Brown template.name = long_name; 2432efb7ac3fSMark Brown } 2433efb7ac3fSMark Brown 2434efb7ac3fSMark Brown kcontrol = snd_ctl_new1(&template, data); 2435efb7ac3fSMark Brown 2436efb7ac3fSMark Brown kfree(name); 2437efb7ac3fSMark Brown 2438efb7ac3fSMark Brown return kcontrol; 2439db2a4165SFrank Mandarino } 2440db2a4165SFrank Mandarino EXPORT_SYMBOL_GPL(snd_soc_cnew); 2441db2a4165SFrank Mandarino 2442022658beSLiam Girdwood static int snd_soc_add_controls(struct snd_card *card, struct device *dev, 2443022658beSLiam Girdwood const struct snd_kcontrol_new *controls, int num_controls, 2444022658beSLiam Girdwood const char *prefix, void *data) 2445022658beSLiam Girdwood { 2446022658beSLiam Girdwood int err, i; 2447022658beSLiam Girdwood 2448022658beSLiam Girdwood for (i = 0; i < num_controls; i++) { 2449022658beSLiam Girdwood const struct snd_kcontrol_new *control = &controls[i]; 2450022658beSLiam Girdwood err = snd_ctl_add(card, snd_soc_cnew(control, data, 2451022658beSLiam Girdwood control->name, prefix)); 2452022658beSLiam Girdwood if (err < 0) { 2453f110bfc7SLiam Girdwood dev_err(dev, "ASoC: Failed to add %s: %d\n", 2454f110bfc7SLiam Girdwood control->name, err); 2455022658beSLiam Girdwood return err; 2456022658beSLiam Girdwood } 2457022658beSLiam Girdwood } 2458022658beSLiam Girdwood 2459022658beSLiam Girdwood return 0; 2460022658beSLiam Girdwood } 2461022658beSLiam Girdwood 24624fefd698SDimitris Papastamos struct snd_kcontrol *snd_soc_card_get_kcontrol(struct snd_soc_card *soc_card, 24634fefd698SDimitris Papastamos const char *name) 24644fefd698SDimitris Papastamos { 24654fefd698SDimitris Papastamos struct snd_card *card = soc_card->snd_card; 24664fefd698SDimitris Papastamos struct snd_kcontrol *kctl; 24674fefd698SDimitris Papastamos 24684fefd698SDimitris Papastamos if (unlikely(!name)) 24694fefd698SDimitris Papastamos return NULL; 24704fefd698SDimitris Papastamos 24714fefd698SDimitris Papastamos list_for_each_entry(kctl, &card->controls, list) 24724fefd698SDimitris Papastamos if (!strncmp(kctl->id.name, name, sizeof(kctl->id.name))) 24734fefd698SDimitris Papastamos return kctl; 24744fefd698SDimitris Papastamos return NULL; 24754fefd698SDimitris Papastamos } 24764fefd698SDimitris Papastamos EXPORT_SYMBOL_GPL(snd_soc_card_get_kcontrol); 24774fefd698SDimitris Papastamos 2478db2a4165SFrank Mandarino /** 2479022658beSLiam Girdwood * snd_soc_add_codec_controls - add an array of controls to a codec. 2480022658beSLiam Girdwood * Convenience function to add a list of controls. Many codecs were 24813e8e1952SIan Molton * duplicating this code. 24823e8e1952SIan Molton * 24833e8e1952SIan Molton * @codec: codec to add controls to 24843e8e1952SIan Molton * @controls: array of controls to add 24853e8e1952SIan Molton * @num_controls: number of elements in the array 24863e8e1952SIan Molton * 24873e8e1952SIan Molton * Return 0 for success, else error. 24883e8e1952SIan Molton */ 2489022658beSLiam Girdwood int snd_soc_add_codec_controls(struct snd_soc_codec *codec, 24903e8e1952SIan Molton const struct snd_kcontrol_new *controls, int num_controls) 24913e8e1952SIan Molton { 2492f0fba2adSLiam Girdwood struct snd_card *card = codec->card->snd_card; 24933e8e1952SIan Molton 2494022658beSLiam Girdwood return snd_soc_add_controls(card, codec->dev, controls, num_controls, 2495022658beSLiam Girdwood codec->name_prefix, codec); 24963e8e1952SIan Molton } 2497022658beSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_add_codec_controls); 24983e8e1952SIan Molton 24993e8e1952SIan Molton /** 2500a491a5c8SLiam Girdwood * snd_soc_add_platform_controls - add an array of controls to a platform. 2501022658beSLiam Girdwood * Convenience function to add a list of controls. 2502a491a5c8SLiam Girdwood * 2503a491a5c8SLiam Girdwood * @platform: platform to add controls to 2504a491a5c8SLiam Girdwood * @controls: array of controls to add 2505a491a5c8SLiam Girdwood * @num_controls: number of elements in the array 2506a491a5c8SLiam Girdwood * 2507a491a5c8SLiam Girdwood * Return 0 for success, else error. 2508a491a5c8SLiam Girdwood */ 2509a491a5c8SLiam Girdwood int snd_soc_add_platform_controls(struct snd_soc_platform *platform, 2510a491a5c8SLiam Girdwood const struct snd_kcontrol_new *controls, int num_controls) 2511a491a5c8SLiam Girdwood { 2512a491a5c8SLiam Girdwood struct snd_card *card = platform->card->snd_card; 2513a491a5c8SLiam Girdwood 2514022658beSLiam Girdwood return snd_soc_add_controls(card, platform->dev, controls, num_controls, 2515022658beSLiam Girdwood NULL, platform); 2516a491a5c8SLiam Girdwood } 2517a491a5c8SLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_add_platform_controls); 2518a491a5c8SLiam Girdwood 2519a491a5c8SLiam Girdwood /** 2520022658beSLiam Girdwood * snd_soc_add_card_controls - add an array of controls to a SoC card. 2521022658beSLiam Girdwood * Convenience function to add a list of controls. 2522022658beSLiam Girdwood * 2523022658beSLiam Girdwood * @soc_card: SoC card to add controls to 2524022658beSLiam Girdwood * @controls: array of controls to add 2525022658beSLiam Girdwood * @num_controls: number of elements in the array 2526022658beSLiam Girdwood * 2527022658beSLiam Girdwood * Return 0 for success, else error. 2528022658beSLiam Girdwood */ 2529022658beSLiam Girdwood int snd_soc_add_card_controls(struct snd_soc_card *soc_card, 2530022658beSLiam Girdwood const struct snd_kcontrol_new *controls, int num_controls) 2531022658beSLiam Girdwood { 2532022658beSLiam Girdwood struct snd_card *card = soc_card->snd_card; 2533022658beSLiam Girdwood 2534022658beSLiam Girdwood return snd_soc_add_controls(card, soc_card->dev, controls, num_controls, 2535022658beSLiam Girdwood NULL, soc_card); 2536022658beSLiam Girdwood } 2537022658beSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_add_card_controls); 2538022658beSLiam Girdwood 2539022658beSLiam Girdwood /** 2540022658beSLiam Girdwood * snd_soc_add_dai_controls - add an array of controls to a DAI. 2541022658beSLiam Girdwood * Convienience function to add a list of controls. 2542022658beSLiam Girdwood * 2543022658beSLiam Girdwood * @dai: DAI to add controls to 2544022658beSLiam Girdwood * @controls: array of controls to add 2545022658beSLiam Girdwood * @num_controls: number of elements in the array 2546022658beSLiam Girdwood * 2547022658beSLiam Girdwood * Return 0 for success, else error. 2548022658beSLiam Girdwood */ 2549022658beSLiam Girdwood int snd_soc_add_dai_controls(struct snd_soc_dai *dai, 2550022658beSLiam Girdwood const struct snd_kcontrol_new *controls, int num_controls) 2551022658beSLiam Girdwood { 2552022658beSLiam Girdwood struct snd_card *card = dai->card->snd_card; 2553022658beSLiam Girdwood 2554022658beSLiam Girdwood return snd_soc_add_controls(card, dai->dev, controls, num_controls, 2555022658beSLiam Girdwood NULL, dai); 2556022658beSLiam Girdwood } 2557022658beSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_add_dai_controls); 2558022658beSLiam Girdwood 2559022658beSLiam Girdwood /** 2560db2a4165SFrank Mandarino * snd_soc_info_enum_double - enumerated double mixer info callback 2561db2a4165SFrank Mandarino * @kcontrol: mixer control 2562db2a4165SFrank Mandarino * @uinfo: control element information 2563db2a4165SFrank Mandarino * 2564db2a4165SFrank Mandarino * Callback to provide information about a double enumerated 2565db2a4165SFrank Mandarino * mixer control. 2566db2a4165SFrank Mandarino * 2567db2a4165SFrank Mandarino * Returns 0 for success. 2568db2a4165SFrank Mandarino */ 2569db2a4165SFrank Mandarino int snd_soc_info_enum_double(struct snd_kcontrol *kcontrol, 2570db2a4165SFrank Mandarino struct snd_ctl_elem_info *uinfo) 2571db2a4165SFrank Mandarino { 2572db2a4165SFrank Mandarino struct soc_enum *e = (struct soc_enum *)kcontrol->private_value; 2573db2a4165SFrank Mandarino 2574db2a4165SFrank Mandarino uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED; 2575db2a4165SFrank Mandarino uinfo->count = e->shift_l == e->shift_r ? 1 : 2; 2576f8ba0b7bSJon Smirl uinfo->value.enumerated.items = e->max; 2577db2a4165SFrank Mandarino 2578f8ba0b7bSJon Smirl if (uinfo->value.enumerated.item > e->max - 1) 2579f8ba0b7bSJon Smirl uinfo->value.enumerated.item = e->max - 1; 2580db2a4165SFrank Mandarino strcpy(uinfo->value.enumerated.name, 2581db2a4165SFrank Mandarino e->texts[uinfo->value.enumerated.item]); 2582db2a4165SFrank Mandarino return 0; 2583db2a4165SFrank Mandarino } 2584db2a4165SFrank Mandarino EXPORT_SYMBOL_GPL(snd_soc_info_enum_double); 2585db2a4165SFrank Mandarino 2586db2a4165SFrank Mandarino /** 2587db2a4165SFrank Mandarino * snd_soc_get_enum_double - enumerated double mixer get callback 2588db2a4165SFrank Mandarino * @kcontrol: mixer control 2589ac11a2b3SMark Brown * @ucontrol: control element information 2590db2a4165SFrank Mandarino * 2591db2a4165SFrank Mandarino * Callback to get the value of a double enumerated mixer. 2592db2a4165SFrank Mandarino * 2593db2a4165SFrank Mandarino * Returns 0 for success. 2594db2a4165SFrank Mandarino */ 2595db2a4165SFrank Mandarino int snd_soc_get_enum_double(struct snd_kcontrol *kcontrol, 2596db2a4165SFrank Mandarino struct snd_ctl_elem_value *ucontrol) 2597db2a4165SFrank Mandarino { 2598db2a4165SFrank Mandarino struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol); 2599db2a4165SFrank Mandarino struct soc_enum *e = (struct soc_enum *)kcontrol->private_value; 260086767b7dSLars-Peter Clausen unsigned int val; 2601db2a4165SFrank Mandarino 2602db2a4165SFrank Mandarino val = snd_soc_read(codec, e->reg); 26033ff3f64bSMark Brown ucontrol->value.enumerated.item[0] 260486767b7dSLars-Peter Clausen = (val >> e->shift_l) & e->mask; 2605db2a4165SFrank Mandarino if (e->shift_l != e->shift_r) 2606db2a4165SFrank Mandarino ucontrol->value.enumerated.item[1] = 260786767b7dSLars-Peter Clausen (val >> e->shift_r) & e->mask; 2608db2a4165SFrank Mandarino 2609db2a4165SFrank Mandarino return 0; 2610db2a4165SFrank Mandarino } 2611db2a4165SFrank Mandarino EXPORT_SYMBOL_GPL(snd_soc_get_enum_double); 2612db2a4165SFrank Mandarino 2613db2a4165SFrank Mandarino /** 2614db2a4165SFrank Mandarino * snd_soc_put_enum_double - enumerated double mixer put callback 2615db2a4165SFrank Mandarino * @kcontrol: mixer control 2616ac11a2b3SMark Brown * @ucontrol: control element information 2617db2a4165SFrank Mandarino * 2618db2a4165SFrank Mandarino * Callback to set the value of a double enumerated mixer. 2619db2a4165SFrank Mandarino * 2620db2a4165SFrank Mandarino * Returns 0 for success. 2621db2a4165SFrank Mandarino */ 2622db2a4165SFrank Mandarino int snd_soc_put_enum_double(struct snd_kcontrol *kcontrol, 2623db2a4165SFrank Mandarino struct snd_ctl_elem_value *ucontrol) 2624db2a4165SFrank Mandarino { 2625db2a4165SFrank Mandarino struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol); 2626db2a4165SFrank Mandarino struct soc_enum *e = (struct soc_enum *)kcontrol->private_value; 262746f5822fSDaniel Ribeiro unsigned int val; 262886767b7dSLars-Peter Clausen unsigned int mask; 2629db2a4165SFrank Mandarino 2630f8ba0b7bSJon Smirl if (ucontrol->value.enumerated.item[0] > e->max - 1) 2631db2a4165SFrank Mandarino return -EINVAL; 2632db2a4165SFrank Mandarino val = ucontrol->value.enumerated.item[0] << e->shift_l; 263386767b7dSLars-Peter Clausen mask = e->mask << e->shift_l; 2634db2a4165SFrank Mandarino if (e->shift_l != e->shift_r) { 2635f8ba0b7bSJon Smirl if (ucontrol->value.enumerated.item[1] > e->max - 1) 2636db2a4165SFrank Mandarino return -EINVAL; 2637db2a4165SFrank Mandarino val |= ucontrol->value.enumerated.item[1] << e->shift_r; 263886767b7dSLars-Peter Clausen mask |= e->mask << e->shift_r; 2639db2a4165SFrank Mandarino } 2640db2a4165SFrank Mandarino 26416c508c62SEero Nurkkala return snd_soc_update_bits_locked(codec, e->reg, mask, val); 2642db2a4165SFrank Mandarino } 2643db2a4165SFrank Mandarino EXPORT_SYMBOL_GPL(snd_soc_put_enum_double); 2644db2a4165SFrank Mandarino 2645db2a4165SFrank Mandarino /** 26462e72f8e3SPeter Ujfalusi * snd_soc_get_value_enum_double - semi enumerated double mixer get callback 26472e72f8e3SPeter Ujfalusi * @kcontrol: mixer control 26482e72f8e3SPeter Ujfalusi * @ucontrol: control element information 26492e72f8e3SPeter Ujfalusi * 26502e72f8e3SPeter Ujfalusi * Callback to get the value of a double semi enumerated mixer. 26512e72f8e3SPeter Ujfalusi * 26522e72f8e3SPeter Ujfalusi * Semi enumerated mixer: the enumerated items are referred as values. Can be 26532e72f8e3SPeter Ujfalusi * used for handling bitfield coded enumeration for example. 26542e72f8e3SPeter Ujfalusi * 26552e72f8e3SPeter Ujfalusi * Returns 0 for success. 26562e72f8e3SPeter Ujfalusi */ 26572e72f8e3SPeter Ujfalusi int snd_soc_get_value_enum_double(struct snd_kcontrol *kcontrol, 26582e72f8e3SPeter Ujfalusi struct snd_ctl_elem_value *ucontrol) 26592e72f8e3SPeter Ujfalusi { 26602e72f8e3SPeter Ujfalusi struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol); 266174155556SPeter Ujfalusi struct soc_enum *e = (struct soc_enum *)kcontrol->private_value; 266246f5822fSDaniel Ribeiro unsigned int reg_val, val, mux; 26632e72f8e3SPeter Ujfalusi 26642e72f8e3SPeter Ujfalusi reg_val = snd_soc_read(codec, e->reg); 26652e72f8e3SPeter Ujfalusi val = (reg_val >> e->shift_l) & e->mask; 26662e72f8e3SPeter Ujfalusi for (mux = 0; mux < e->max; mux++) { 26672e72f8e3SPeter Ujfalusi if (val == e->values[mux]) 26682e72f8e3SPeter Ujfalusi break; 26692e72f8e3SPeter Ujfalusi } 26702e72f8e3SPeter Ujfalusi ucontrol->value.enumerated.item[0] = mux; 26712e72f8e3SPeter Ujfalusi if (e->shift_l != e->shift_r) { 26722e72f8e3SPeter Ujfalusi val = (reg_val >> e->shift_r) & e->mask; 26732e72f8e3SPeter Ujfalusi for (mux = 0; mux < e->max; mux++) { 26742e72f8e3SPeter Ujfalusi if (val == e->values[mux]) 26752e72f8e3SPeter Ujfalusi break; 26762e72f8e3SPeter Ujfalusi } 26772e72f8e3SPeter Ujfalusi ucontrol->value.enumerated.item[1] = mux; 26782e72f8e3SPeter Ujfalusi } 26792e72f8e3SPeter Ujfalusi 26802e72f8e3SPeter Ujfalusi return 0; 26812e72f8e3SPeter Ujfalusi } 26822e72f8e3SPeter Ujfalusi EXPORT_SYMBOL_GPL(snd_soc_get_value_enum_double); 26832e72f8e3SPeter Ujfalusi 26842e72f8e3SPeter Ujfalusi /** 26852e72f8e3SPeter Ujfalusi * snd_soc_put_value_enum_double - semi enumerated double mixer put callback 26862e72f8e3SPeter Ujfalusi * @kcontrol: mixer control 26872e72f8e3SPeter Ujfalusi * @ucontrol: control element information 26882e72f8e3SPeter Ujfalusi * 26892e72f8e3SPeter Ujfalusi * Callback to set the value of a double semi enumerated mixer. 26902e72f8e3SPeter Ujfalusi * 26912e72f8e3SPeter Ujfalusi * Semi enumerated mixer: the enumerated items are referred as values. Can be 26922e72f8e3SPeter Ujfalusi * used for handling bitfield coded enumeration for example. 26932e72f8e3SPeter Ujfalusi * 26942e72f8e3SPeter Ujfalusi * Returns 0 for success. 26952e72f8e3SPeter Ujfalusi */ 26962e72f8e3SPeter Ujfalusi int snd_soc_put_value_enum_double(struct snd_kcontrol *kcontrol, 26972e72f8e3SPeter Ujfalusi struct snd_ctl_elem_value *ucontrol) 26982e72f8e3SPeter Ujfalusi { 26992e72f8e3SPeter Ujfalusi struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol); 270074155556SPeter Ujfalusi struct soc_enum *e = (struct soc_enum *)kcontrol->private_value; 270146f5822fSDaniel Ribeiro unsigned int val; 270246f5822fSDaniel Ribeiro unsigned int mask; 27032e72f8e3SPeter Ujfalusi 27042e72f8e3SPeter Ujfalusi if (ucontrol->value.enumerated.item[0] > e->max - 1) 27052e72f8e3SPeter Ujfalusi return -EINVAL; 27062e72f8e3SPeter Ujfalusi val = e->values[ucontrol->value.enumerated.item[0]] << e->shift_l; 27072e72f8e3SPeter Ujfalusi mask = e->mask << e->shift_l; 27082e72f8e3SPeter Ujfalusi if (e->shift_l != e->shift_r) { 27092e72f8e3SPeter Ujfalusi if (ucontrol->value.enumerated.item[1] > e->max - 1) 27102e72f8e3SPeter Ujfalusi return -EINVAL; 27112e72f8e3SPeter Ujfalusi val |= e->values[ucontrol->value.enumerated.item[1]] << e->shift_r; 27122e72f8e3SPeter Ujfalusi mask |= e->mask << e->shift_r; 27132e72f8e3SPeter Ujfalusi } 27142e72f8e3SPeter Ujfalusi 27156c508c62SEero Nurkkala return snd_soc_update_bits_locked(codec, e->reg, mask, val); 27162e72f8e3SPeter Ujfalusi } 27172e72f8e3SPeter Ujfalusi EXPORT_SYMBOL_GPL(snd_soc_put_value_enum_double); 27182e72f8e3SPeter Ujfalusi 27192e72f8e3SPeter Ujfalusi /** 2720db2a4165SFrank Mandarino * snd_soc_info_volsw - single mixer info callback 2721db2a4165SFrank Mandarino * @kcontrol: mixer control 2722db2a4165SFrank Mandarino * @uinfo: control element information 2723db2a4165SFrank Mandarino * 2724e8f5a103SPeter Ujfalusi * Callback to provide information about a single mixer control, or a double 2725e8f5a103SPeter Ujfalusi * mixer control that spans 2 registers. 2726db2a4165SFrank Mandarino * 2727db2a4165SFrank Mandarino * Returns 0 for success. 2728db2a4165SFrank Mandarino */ 2729db2a4165SFrank Mandarino int snd_soc_info_volsw(struct snd_kcontrol *kcontrol, 2730db2a4165SFrank Mandarino struct snd_ctl_elem_info *uinfo) 2731db2a4165SFrank Mandarino { 27324eaa9819SJon Smirl struct soc_mixer_control *mc = 27334eaa9819SJon Smirl (struct soc_mixer_control *)kcontrol->private_value; 2734d11bb4a9SPeter Ujfalusi int platform_max; 2735db2a4165SFrank Mandarino 2736d11bb4a9SPeter Ujfalusi if (!mc->platform_max) 2737d11bb4a9SPeter Ujfalusi mc->platform_max = mc->max; 2738d11bb4a9SPeter Ujfalusi platform_max = mc->platform_max; 2739d11bb4a9SPeter Ujfalusi 2740d11bb4a9SPeter Ujfalusi if (platform_max == 1 && !strstr(kcontrol->id.name, " Volume")) 2741a7a4ac86SPhilipp Zabel uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN; 2742a7a4ac86SPhilipp Zabel else 2743a7a4ac86SPhilipp Zabel uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER; 2744a7a4ac86SPhilipp Zabel 2745e8f5a103SPeter Ujfalusi uinfo->count = snd_soc_volsw_is_stereo(mc) ? 2 : 1; 2746db2a4165SFrank Mandarino uinfo->value.integer.min = 0; 2747d11bb4a9SPeter Ujfalusi uinfo->value.integer.max = platform_max; 2748db2a4165SFrank Mandarino return 0; 2749db2a4165SFrank Mandarino } 2750db2a4165SFrank Mandarino EXPORT_SYMBOL_GPL(snd_soc_info_volsw); 2751db2a4165SFrank Mandarino 2752db2a4165SFrank Mandarino /** 2753db2a4165SFrank Mandarino * snd_soc_get_volsw - single mixer get callback 2754db2a4165SFrank Mandarino * @kcontrol: mixer control 2755ac11a2b3SMark Brown * @ucontrol: control element information 2756db2a4165SFrank Mandarino * 2757f7915d99SPeter Ujfalusi * Callback to get the value of a single mixer control, or a double mixer 2758f7915d99SPeter Ujfalusi * control that spans 2 registers. 2759db2a4165SFrank Mandarino * 2760db2a4165SFrank Mandarino * Returns 0 for success. 2761db2a4165SFrank Mandarino */ 2762db2a4165SFrank Mandarino int snd_soc_get_volsw(struct snd_kcontrol *kcontrol, 2763db2a4165SFrank Mandarino struct snd_ctl_elem_value *ucontrol) 2764db2a4165SFrank Mandarino { 27654eaa9819SJon Smirl struct soc_mixer_control *mc = 27664eaa9819SJon Smirl (struct soc_mixer_control *)kcontrol->private_value; 2767db2a4165SFrank Mandarino struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol); 2768815ecf8dSJon Smirl unsigned int reg = mc->reg; 2769f7915d99SPeter Ujfalusi unsigned int reg2 = mc->rreg; 2770815ecf8dSJon Smirl unsigned int shift = mc->shift; 2771815ecf8dSJon Smirl unsigned int rshift = mc->rshift; 27724eaa9819SJon Smirl int max = mc->max; 2773815ecf8dSJon Smirl unsigned int mask = (1 << fls(max)) - 1; 2774815ecf8dSJon Smirl unsigned int invert = mc->invert; 2775db2a4165SFrank Mandarino 2776db2a4165SFrank Mandarino ucontrol->value.integer.value[0] = 2777db2a4165SFrank Mandarino (snd_soc_read(codec, reg) >> shift) & mask; 2778f7915d99SPeter Ujfalusi if (invert) 2779db2a4165SFrank Mandarino ucontrol->value.integer.value[0] = 2780a7a4ac86SPhilipp Zabel max - ucontrol->value.integer.value[0]; 2781f7915d99SPeter Ujfalusi 2782f7915d99SPeter Ujfalusi if (snd_soc_volsw_is_stereo(mc)) { 2783f7915d99SPeter Ujfalusi if (reg == reg2) 2784f7915d99SPeter Ujfalusi ucontrol->value.integer.value[1] = 2785f7915d99SPeter Ujfalusi (snd_soc_read(codec, reg) >> rshift) & mask; 2786f7915d99SPeter Ujfalusi else 2787f7915d99SPeter Ujfalusi ucontrol->value.integer.value[1] = 2788f7915d99SPeter Ujfalusi (snd_soc_read(codec, reg2) >> shift) & mask; 2789f7915d99SPeter Ujfalusi if (invert) 2790db2a4165SFrank Mandarino ucontrol->value.integer.value[1] = 2791a7a4ac86SPhilipp Zabel max - ucontrol->value.integer.value[1]; 2792db2a4165SFrank Mandarino } 2793db2a4165SFrank Mandarino 2794db2a4165SFrank Mandarino return 0; 2795db2a4165SFrank Mandarino } 2796db2a4165SFrank Mandarino EXPORT_SYMBOL_GPL(snd_soc_get_volsw); 2797db2a4165SFrank Mandarino 2798db2a4165SFrank Mandarino /** 2799db2a4165SFrank Mandarino * snd_soc_put_volsw - single mixer put callback 2800db2a4165SFrank Mandarino * @kcontrol: mixer control 2801ac11a2b3SMark Brown * @ucontrol: control element information 2802db2a4165SFrank Mandarino * 2803974815baSPeter Ujfalusi * Callback to set the value of a single mixer control, or a double mixer 2804974815baSPeter Ujfalusi * control that spans 2 registers. 2805db2a4165SFrank Mandarino * 2806db2a4165SFrank Mandarino * Returns 0 for success. 2807db2a4165SFrank Mandarino */ 2808db2a4165SFrank Mandarino int snd_soc_put_volsw(struct snd_kcontrol *kcontrol, 2809db2a4165SFrank Mandarino struct snd_ctl_elem_value *ucontrol) 2810db2a4165SFrank Mandarino { 28114eaa9819SJon Smirl struct soc_mixer_control *mc = 28124eaa9819SJon Smirl (struct soc_mixer_control *)kcontrol->private_value; 2813db2a4165SFrank Mandarino struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol); 2814815ecf8dSJon Smirl unsigned int reg = mc->reg; 2815974815baSPeter Ujfalusi unsigned int reg2 = mc->rreg; 2816815ecf8dSJon Smirl unsigned int shift = mc->shift; 2817815ecf8dSJon Smirl unsigned int rshift = mc->rshift; 28184eaa9819SJon Smirl int max = mc->max; 2819815ecf8dSJon Smirl unsigned int mask = (1 << fls(max)) - 1; 2820815ecf8dSJon Smirl unsigned int invert = mc->invert; 2821974815baSPeter Ujfalusi int err; 2822974815baSPeter Ujfalusi bool type_2r = 0; 2823974815baSPeter Ujfalusi unsigned int val2 = 0; 2824974815baSPeter Ujfalusi unsigned int val, val_mask; 2825db2a4165SFrank Mandarino 2826db2a4165SFrank Mandarino val = (ucontrol->value.integer.value[0] & mask); 2827db2a4165SFrank Mandarino if (invert) 2828a7a4ac86SPhilipp Zabel val = max - val; 2829db2a4165SFrank Mandarino val_mask = mask << shift; 2830db2a4165SFrank Mandarino val = val << shift; 2831974815baSPeter Ujfalusi if (snd_soc_volsw_is_stereo(mc)) { 2832db2a4165SFrank Mandarino val2 = (ucontrol->value.integer.value[1] & mask); 2833db2a4165SFrank Mandarino if (invert) 2834a7a4ac86SPhilipp Zabel val2 = max - val2; 2835974815baSPeter Ujfalusi if (reg == reg2) { 2836db2a4165SFrank Mandarino val_mask |= mask << rshift; 2837db2a4165SFrank Mandarino val |= val2 << rshift; 2838974815baSPeter Ujfalusi } else { 2839db2a4165SFrank Mandarino val2 = val2 << shift; 2840974815baSPeter Ujfalusi type_2r = 1; 2841974815baSPeter Ujfalusi } 2842974815baSPeter Ujfalusi } 28436c508c62SEero Nurkkala err = snd_soc_update_bits_locked(codec, reg, val_mask, val); 28443ff3f64bSMark Brown if (err < 0) 2845db2a4165SFrank Mandarino return err; 2846db2a4165SFrank Mandarino 2847974815baSPeter Ujfalusi if (type_2r) 28486c508c62SEero Nurkkala err = snd_soc_update_bits_locked(codec, reg2, val_mask, val2); 2849974815baSPeter Ujfalusi 2850db2a4165SFrank Mandarino return err; 2851db2a4165SFrank Mandarino } 2852974815baSPeter Ujfalusi EXPORT_SYMBOL_GPL(snd_soc_put_volsw); 2853db2a4165SFrank Mandarino 2854e13ac2e9SMark Brown /** 28551d99f243SBrian Austin * snd_soc_get_volsw_sx - single mixer get callback 28561d99f243SBrian Austin * @kcontrol: mixer control 28571d99f243SBrian Austin * @ucontrol: control element information 28581d99f243SBrian Austin * 28591d99f243SBrian Austin * Callback to get the value of a single mixer control, or a double mixer 28601d99f243SBrian Austin * control that spans 2 registers. 28611d99f243SBrian Austin * 28621d99f243SBrian Austin * Returns 0 for success. 28631d99f243SBrian Austin */ 28641d99f243SBrian Austin int snd_soc_get_volsw_sx(struct snd_kcontrol *kcontrol, 28651d99f243SBrian Austin struct snd_ctl_elem_value *ucontrol) 28661d99f243SBrian Austin { 28671d99f243SBrian Austin struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol); 28681d99f243SBrian Austin struct soc_mixer_control *mc = 28691d99f243SBrian Austin (struct soc_mixer_control *)kcontrol->private_value; 28701d99f243SBrian Austin 28711d99f243SBrian Austin unsigned int reg = mc->reg; 28721d99f243SBrian Austin unsigned int reg2 = mc->rreg; 28731d99f243SBrian Austin unsigned int shift = mc->shift; 28741d99f243SBrian Austin unsigned int rshift = mc->rshift; 28751d99f243SBrian Austin int max = mc->max; 28761d99f243SBrian Austin int min = mc->min; 28771d99f243SBrian Austin int mask = (1 << (fls(min + max) - 1)) - 1; 28781d99f243SBrian Austin 28791d99f243SBrian Austin ucontrol->value.integer.value[0] = 28801d99f243SBrian Austin ((snd_soc_read(codec, reg) >> shift) - min) & mask; 28811d99f243SBrian Austin 28821d99f243SBrian Austin if (snd_soc_volsw_is_stereo(mc)) 28831d99f243SBrian Austin ucontrol->value.integer.value[1] = 28841d99f243SBrian Austin ((snd_soc_read(codec, reg2) >> rshift) - min) & mask; 28851d99f243SBrian Austin 28861d99f243SBrian Austin return 0; 28871d99f243SBrian Austin } 28881d99f243SBrian Austin EXPORT_SYMBOL_GPL(snd_soc_get_volsw_sx); 28891d99f243SBrian Austin 28901d99f243SBrian Austin /** 28911d99f243SBrian Austin * snd_soc_put_volsw_sx - double mixer set callback 28921d99f243SBrian Austin * @kcontrol: mixer control 28931d99f243SBrian Austin * @uinfo: control element information 28941d99f243SBrian Austin * 28951d99f243SBrian Austin * Callback to set the value of a double mixer control that spans 2 registers. 28961d99f243SBrian Austin * 28971d99f243SBrian Austin * Returns 0 for success. 28981d99f243SBrian Austin */ 28991d99f243SBrian Austin int snd_soc_put_volsw_sx(struct snd_kcontrol *kcontrol, 29001d99f243SBrian Austin struct snd_ctl_elem_value *ucontrol) 29011d99f243SBrian Austin { 29021d99f243SBrian Austin struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol); 29031d99f243SBrian Austin struct soc_mixer_control *mc = 29041d99f243SBrian Austin (struct soc_mixer_control *)kcontrol->private_value; 29051d99f243SBrian Austin 29061d99f243SBrian Austin unsigned int reg = mc->reg; 29071d99f243SBrian Austin unsigned int reg2 = mc->rreg; 29081d99f243SBrian Austin unsigned int shift = mc->shift; 29091d99f243SBrian Austin unsigned int rshift = mc->rshift; 29101d99f243SBrian Austin int max = mc->max; 29111d99f243SBrian Austin int min = mc->min; 29121d99f243SBrian Austin int mask = (1 << (fls(min + max) - 1)) - 1; 291327f1d759SBrian Austin int err = 0; 29141d99f243SBrian Austin unsigned short val, val_mask, val2 = 0; 29151d99f243SBrian Austin 29161d99f243SBrian Austin val_mask = mask << shift; 29171d99f243SBrian Austin val = (ucontrol->value.integer.value[0] + min) & mask; 29181d99f243SBrian Austin val = val << shift; 29191d99f243SBrian Austin 2920d055852eSMukund Navada err = snd_soc_update_bits_locked(codec, reg, val_mask, val); 2921d055852eSMukund Navada if (err < 0) 29221d99f243SBrian Austin return err; 29231d99f243SBrian Austin 29241d99f243SBrian Austin if (snd_soc_volsw_is_stereo(mc)) { 29251d99f243SBrian Austin val_mask = mask << rshift; 29261d99f243SBrian Austin val2 = (ucontrol->value.integer.value[1] + min) & mask; 29271d99f243SBrian Austin val2 = val2 << rshift; 29281d99f243SBrian Austin 29291d99f243SBrian Austin if (snd_soc_update_bits_locked(codec, reg2, val_mask, val2)) 29301d99f243SBrian Austin return err; 29311d99f243SBrian Austin } 29321d99f243SBrian Austin return 0; 29331d99f243SBrian Austin } 29341d99f243SBrian Austin EXPORT_SYMBOL_GPL(snd_soc_put_volsw_sx); 29351d99f243SBrian Austin 29361d99f243SBrian Austin /** 2937e13ac2e9SMark Brown * snd_soc_info_volsw_s8 - signed mixer info callback 2938e13ac2e9SMark Brown * @kcontrol: mixer control 2939e13ac2e9SMark Brown * @uinfo: control element information 2940e13ac2e9SMark Brown * 2941e13ac2e9SMark Brown * Callback to provide information about a signed mixer control. 2942e13ac2e9SMark Brown * 2943e13ac2e9SMark Brown * Returns 0 for success. 2944e13ac2e9SMark Brown */ 2945e13ac2e9SMark Brown int snd_soc_info_volsw_s8(struct snd_kcontrol *kcontrol, 2946e13ac2e9SMark Brown struct snd_ctl_elem_info *uinfo) 2947e13ac2e9SMark Brown { 29484eaa9819SJon Smirl struct soc_mixer_control *mc = 29494eaa9819SJon Smirl (struct soc_mixer_control *)kcontrol->private_value; 2950d11bb4a9SPeter Ujfalusi int platform_max; 29514eaa9819SJon Smirl int min = mc->min; 2952e13ac2e9SMark Brown 2953d11bb4a9SPeter Ujfalusi if (!mc->platform_max) 2954d11bb4a9SPeter Ujfalusi mc->platform_max = mc->max; 2955d11bb4a9SPeter Ujfalusi platform_max = mc->platform_max; 2956d11bb4a9SPeter Ujfalusi 2957e13ac2e9SMark Brown uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER; 2958e13ac2e9SMark Brown uinfo->count = 2; 2959e13ac2e9SMark Brown uinfo->value.integer.min = 0; 2960d11bb4a9SPeter Ujfalusi uinfo->value.integer.max = platform_max - min; 2961e13ac2e9SMark Brown return 0; 2962e13ac2e9SMark Brown } 2963e13ac2e9SMark Brown EXPORT_SYMBOL_GPL(snd_soc_info_volsw_s8); 2964e13ac2e9SMark Brown 2965e13ac2e9SMark Brown /** 2966e13ac2e9SMark Brown * snd_soc_get_volsw_s8 - signed mixer get callback 2967e13ac2e9SMark Brown * @kcontrol: mixer control 2968ac11a2b3SMark Brown * @ucontrol: control element information 2969e13ac2e9SMark Brown * 2970e13ac2e9SMark Brown * Callback to get the value of a signed mixer control. 2971e13ac2e9SMark Brown * 2972e13ac2e9SMark Brown * Returns 0 for success. 2973e13ac2e9SMark Brown */ 2974e13ac2e9SMark Brown int snd_soc_get_volsw_s8(struct snd_kcontrol *kcontrol, 2975e13ac2e9SMark Brown struct snd_ctl_elem_value *ucontrol) 2976e13ac2e9SMark Brown { 29774eaa9819SJon Smirl struct soc_mixer_control *mc = 29784eaa9819SJon Smirl (struct soc_mixer_control *)kcontrol->private_value; 2979e13ac2e9SMark Brown struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol); 2980815ecf8dSJon Smirl unsigned int reg = mc->reg; 29814eaa9819SJon Smirl int min = mc->min; 2982e13ac2e9SMark Brown int val = snd_soc_read(codec, reg); 2983e13ac2e9SMark Brown 2984e13ac2e9SMark Brown ucontrol->value.integer.value[0] = 2985e13ac2e9SMark Brown ((signed char)(val & 0xff))-min; 2986e13ac2e9SMark Brown ucontrol->value.integer.value[1] = 2987e13ac2e9SMark Brown ((signed char)((val >> 8) & 0xff))-min; 2988e13ac2e9SMark Brown return 0; 2989e13ac2e9SMark Brown } 2990e13ac2e9SMark Brown EXPORT_SYMBOL_GPL(snd_soc_get_volsw_s8); 2991e13ac2e9SMark Brown 2992e13ac2e9SMark Brown /** 2993e13ac2e9SMark Brown * snd_soc_put_volsw_sgn - signed mixer put callback 2994e13ac2e9SMark Brown * @kcontrol: mixer control 2995ac11a2b3SMark Brown * @ucontrol: control element information 2996e13ac2e9SMark Brown * 2997e13ac2e9SMark Brown * Callback to set the value of a signed mixer control. 2998e13ac2e9SMark Brown * 2999e13ac2e9SMark Brown * Returns 0 for success. 3000e13ac2e9SMark Brown */ 3001e13ac2e9SMark Brown int snd_soc_put_volsw_s8(struct snd_kcontrol *kcontrol, 3002e13ac2e9SMark Brown struct snd_ctl_elem_value *ucontrol) 3003e13ac2e9SMark Brown { 30044eaa9819SJon Smirl struct soc_mixer_control *mc = 30054eaa9819SJon Smirl (struct soc_mixer_control *)kcontrol->private_value; 3006e13ac2e9SMark Brown struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol); 3007815ecf8dSJon Smirl unsigned int reg = mc->reg; 30084eaa9819SJon Smirl int min = mc->min; 300946f5822fSDaniel Ribeiro unsigned int val; 3010e13ac2e9SMark Brown 3011e13ac2e9SMark Brown val = (ucontrol->value.integer.value[0]+min) & 0xff; 3012e13ac2e9SMark Brown val |= ((ucontrol->value.integer.value[1]+min) & 0xff) << 8; 3013e13ac2e9SMark Brown 30146c508c62SEero Nurkkala return snd_soc_update_bits_locked(codec, reg, 0xffff, val); 3015e13ac2e9SMark Brown } 3016e13ac2e9SMark Brown EXPORT_SYMBOL_GPL(snd_soc_put_volsw_s8); 3017e13ac2e9SMark Brown 30188c6529dbSLiam Girdwood /** 30196c9d8cf6SAdam Thomson * snd_soc_info_volsw_range - single mixer info callback with range. 30206c9d8cf6SAdam Thomson * @kcontrol: mixer control 30216c9d8cf6SAdam Thomson * @uinfo: control element information 30226c9d8cf6SAdam Thomson * 30236c9d8cf6SAdam Thomson * Callback to provide information, within a range, about a single 30246c9d8cf6SAdam Thomson * mixer control. 30256c9d8cf6SAdam Thomson * 30266c9d8cf6SAdam Thomson * returns 0 for success. 30276c9d8cf6SAdam Thomson */ 30286c9d8cf6SAdam Thomson int snd_soc_info_volsw_range(struct snd_kcontrol *kcontrol, 30296c9d8cf6SAdam Thomson struct snd_ctl_elem_info *uinfo) 30306c9d8cf6SAdam Thomson { 30316c9d8cf6SAdam Thomson struct soc_mixer_control *mc = 30326c9d8cf6SAdam Thomson (struct soc_mixer_control *)kcontrol->private_value; 30336c9d8cf6SAdam Thomson int platform_max; 30346c9d8cf6SAdam Thomson int min = mc->min; 30356c9d8cf6SAdam Thomson 30366c9d8cf6SAdam Thomson if (!mc->platform_max) 30376c9d8cf6SAdam Thomson mc->platform_max = mc->max; 30386c9d8cf6SAdam Thomson platform_max = mc->platform_max; 30396c9d8cf6SAdam Thomson 30406c9d8cf6SAdam Thomson uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER; 30419bde4f0bSMark Brown uinfo->count = snd_soc_volsw_is_stereo(mc) ? 2 : 1; 30426c9d8cf6SAdam Thomson uinfo->value.integer.min = 0; 30436c9d8cf6SAdam Thomson uinfo->value.integer.max = platform_max - min; 30446c9d8cf6SAdam Thomson 30456c9d8cf6SAdam Thomson return 0; 30466c9d8cf6SAdam Thomson } 30476c9d8cf6SAdam Thomson EXPORT_SYMBOL_GPL(snd_soc_info_volsw_range); 30486c9d8cf6SAdam Thomson 30496c9d8cf6SAdam Thomson /** 30506c9d8cf6SAdam Thomson * snd_soc_put_volsw_range - single mixer put value callback with range. 30516c9d8cf6SAdam Thomson * @kcontrol: mixer control 30526c9d8cf6SAdam Thomson * @ucontrol: control element information 30536c9d8cf6SAdam Thomson * 30546c9d8cf6SAdam Thomson * Callback to set the value, within a range, for a single mixer control. 30556c9d8cf6SAdam Thomson * 30566c9d8cf6SAdam Thomson * Returns 0 for success. 30576c9d8cf6SAdam Thomson */ 30586c9d8cf6SAdam Thomson int snd_soc_put_volsw_range(struct snd_kcontrol *kcontrol, 30596c9d8cf6SAdam Thomson struct snd_ctl_elem_value *ucontrol) 30606c9d8cf6SAdam Thomson { 30616c9d8cf6SAdam Thomson struct soc_mixer_control *mc = 30626c9d8cf6SAdam Thomson (struct soc_mixer_control *)kcontrol->private_value; 30636c9d8cf6SAdam Thomson struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol); 30646c9d8cf6SAdam Thomson unsigned int reg = mc->reg; 30659bde4f0bSMark Brown unsigned int rreg = mc->rreg; 30666c9d8cf6SAdam Thomson unsigned int shift = mc->shift; 30676c9d8cf6SAdam Thomson int min = mc->min; 30686c9d8cf6SAdam Thomson int max = mc->max; 30696c9d8cf6SAdam Thomson unsigned int mask = (1 << fls(max)) - 1; 30706c9d8cf6SAdam Thomson unsigned int invert = mc->invert; 30716c9d8cf6SAdam Thomson unsigned int val, val_mask; 30729bde4f0bSMark Brown int ret; 30736c9d8cf6SAdam Thomson 30746c9d8cf6SAdam Thomson val = ((ucontrol->value.integer.value[0] + min) & mask); 30756c9d8cf6SAdam Thomson if (invert) 30766c9d8cf6SAdam Thomson val = max - val; 30776c9d8cf6SAdam Thomson val_mask = mask << shift; 30786c9d8cf6SAdam Thomson val = val << shift; 30796c9d8cf6SAdam Thomson 30809bde4f0bSMark Brown ret = snd_soc_update_bits_locked(codec, reg, val_mask, val); 30810eaa6ccaSJoonyoung Shim if (ret < 0) 30829bde4f0bSMark Brown return ret; 30839bde4f0bSMark Brown 30849bde4f0bSMark Brown if (snd_soc_volsw_is_stereo(mc)) { 30859bde4f0bSMark Brown val = ((ucontrol->value.integer.value[1] + min) & mask); 30869bde4f0bSMark Brown if (invert) 30879bde4f0bSMark Brown val = max - val; 30889bde4f0bSMark Brown val_mask = mask << shift; 30899bde4f0bSMark Brown val = val << shift; 30909bde4f0bSMark Brown 30919bde4f0bSMark Brown ret = snd_soc_update_bits_locked(codec, rreg, val_mask, val); 30929bde4f0bSMark Brown } 30939bde4f0bSMark Brown 30949bde4f0bSMark Brown return ret; 30956c9d8cf6SAdam Thomson } 30966c9d8cf6SAdam Thomson EXPORT_SYMBOL_GPL(snd_soc_put_volsw_range); 30976c9d8cf6SAdam Thomson 30986c9d8cf6SAdam Thomson /** 30996c9d8cf6SAdam Thomson * snd_soc_get_volsw_range - single mixer get callback with range 31006c9d8cf6SAdam Thomson * @kcontrol: mixer control 31016c9d8cf6SAdam Thomson * @ucontrol: control element information 31026c9d8cf6SAdam Thomson * 31036c9d8cf6SAdam Thomson * Callback to get the value, within a range, of a single mixer control. 31046c9d8cf6SAdam Thomson * 31056c9d8cf6SAdam Thomson * Returns 0 for success. 31066c9d8cf6SAdam Thomson */ 31076c9d8cf6SAdam Thomson int snd_soc_get_volsw_range(struct snd_kcontrol *kcontrol, 31086c9d8cf6SAdam Thomson struct snd_ctl_elem_value *ucontrol) 31096c9d8cf6SAdam Thomson { 31106c9d8cf6SAdam Thomson struct soc_mixer_control *mc = 31116c9d8cf6SAdam Thomson (struct soc_mixer_control *)kcontrol->private_value; 31126c9d8cf6SAdam Thomson struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol); 31136c9d8cf6SAdam Thomson unsigned int reg = mc->reg; 31149bde4f0bSMark Brown unsigned int rreg = mc->rreg; 31156c9d8cf6SAdam Thomson unsigned int shift = mc->shift; 31166c9d8cf6SAdam Thomson int min = mc->min; 31176c9d8cf6SAdam Thomson int max = mc->max; 31186c9d8cf6SAdam Thomson unsigned int mask = (1 << fls(max)) - 1; 31196c9d8cf6SAdam Thomson unsigned int invert = mc->invert; 31206c9d8cf6SAdam Thomson 31216c9d8cf6SAdam Thomson ucontrol->value.integer.value[0] = 31226c9d8cf6SAdam Thomson (snd_soc_read(codec, reg) >> shift) & mask; 31236c9d8cf6SAdam Thomson if (invert) 31246c9d8cf6SAdam Thomson ucontrol->value.integer.value[0] = 31256c9d8cf6SAdam Thomson max - ucontrol->value.integer.value[0]; 31266c9d8cf6SAdam Thomson ucontrol->value.integer.value[0] = 31276c9d8cf6SAdam Thomson ucontrol->value.integer.value[0] - min; 31286c9d8cf6SAdam Thomson 31299bde4f0bSMark Brown if (snd_soc_volsw_is_stereo(mc)) { 31309bde4f0bSMark Brown ucontrol->value.integer.value[1] = 31319bde4f0bSMark Brown (snd_soc_read(codec, rreg) >> shift) & mask; 31329bde4f0bSMark Brown if (invert) 31339bde4f0bSMark Brown ucontrol->value.integer.value[1] = 31349bde4f0bSMark Brown max - ucontrol->value.integer.value[1]; 31359bde4f0bSMark Brown ucontrol->value.integer.value[1] = 31369bde4f0bSMark Brown ucontrol->value.integer.value[1] - min; 31379bde4f0bSMark Brown } 31389bde4f0bSMark Brown 31396c9d8cf6SAdam Thomson return 0; 31406c9d8cf6SAdam Thomson } 31416c9d8cf6SAdam Thomson EXPORT_SYMBOL_GPL(snd_soc_get_volsw_range); 31426c9d8cf6SAdam Thomson 31436c9d8cf6SAdam Thomson /** 3144637d3847SPeter Ujfalusi * snd_soc_limit_volume - Set new limit to an existing volume control. 3145637d3847SPeter Ujfalusi * 3146637d3847SPeter Ujfalusi * @codec: where to look for the control 3147637d3847SPeter Ujfalusi * @name: Name of the control 3148637d3847SPeter Ujfalusi * @max: new maximum limit 3149637d3847SPeter Ujfalusi * 3150637d3847SPeter Ujfalusi * Return 0 for success, else error. 3151637d3847SPeter Ujfalusi */ 3152637d3847SPeter Ujfalusi int snd_soc_limit_volume(struct snd_soc_codec *codec, 3153637d3847SPeter Ujfalusi const char *name, int max) 3154637d3847SPeter Ujfalusi { 3155f0fba2adSLiam Girdwood struct snd_card *card = codec->card->snd_card; 3156637d3847SPeter Ujfalusi struct snd_kcontrol *kctl; 3157637d3847SPeter Ujfalusi struct soc_mixer_control *mc; 3158637d3847SPeter Ujfalusi int found = 0; 3159637d3847SPeter Ujfalusi int ret = -EINVAL; 3160637d3847SPeter Ujfalusi 3161637d3847SPeter Ujfalusi /* Sanity check for name and max */ 3162637d3847SPeter Ujfalusi if (unlikely(!name || max <= 0)) 3163637d3847SPeter Ujfalusi return -EINVAL; 3164637d3847SPeter Ujfalusi 3165637d3847SPeter Ujfalusi list_for_each_entry(kctl, &card->controls, list) { 3166637d3847SPeter Ujfalusi if (!strncmp(kctl->id.name, name, sizeof(kctl->id.name))) { 3167637d3847SPeter Ujfalusi found = 1; 3168637d3847SPeter Ujfalusi break; 3169637d3847SPeter Ujfalusi } 3170637d3847SPeter Ujfalusi } 3171637d3847SPeter Ujfalusi if (found) { 3172637d3847SPeter Ujfalusi mc = (struct soc_mixer_control *)kctl->private_value; 3173637d3847SPeter Ujfalusi if (max <= mc->max) { 3174d11bb4a9SPeter Ujfalusi mc->platform_max = max; 3175637d3847SPeter Ujfalusi ret = 0; 3176637d3847SPeter Ujfalusi } 3177637d3847SPeter Ujfalusi } 3178637d3847SPeter Ujfalusi return ret; 3179637d3847SPeter Ujfalusi } 3180637d3847SPeter Ujfalusi EXPORT_SYMBOL_GPL(snd_soc_limit_volume); 3181637d3847SPeter Ujfalusi 318271d08516SMark Brown int snd_soc_bytes_info(struct snd_kcontrol *kcontrol, 318371d08516SMark Brown struct snd_ctl_elem_info *uinfo) 318471d08516SMark Brown { 318571d08516SMark Brown struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol); 318671d08516SMark Brown struct soc_bytes *params = (void *)kcontrol->private_value; 318771d08516SMark Brown 318871d08516SMark Brown uinfo->type = SNDRV_CTL_ELEM_TYPE_BYTES; 318971d08516SMark Brown uinfo->count = params->num_regs * codec->val_bytes; 319071d08516SMark Brown 319171d08516SMark Brown return 0; 319271d08516SMark Brown } 319371d08516SMark Brown EXPORT_SYMBOL_GPL(snd_soc_bytes_info); 319471d08516SMark Brown 319571d08516SMark Brown int snd_soc_bytes_get(struct snd_kcontrol *kcontrol, 319671d08516SMark Brown struct snd_ctl_elem_value *ucontrol) 319771d08516SMark Brown { 319871d08516SMark Brown struct soc_bytes *params = (void *)kcontrol->private_value; 319971d08516SMark Brown struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol); 320071d08516SMark Brown int ret; 320171d08516SMark Brown 320271d08516SMark Brown if (codec->using_regmap) 320371d08516SMark Brown ret = regmap_raw_read(codec->control_data, params->base, 320471d08516SMark Brown ucontrol->value.bytes.data, 320571d08516SMark Brown params->num_regs * codec->val_bytes); 320671d08516SMark Brown else 320771d08516SMark Brown ret = -EINVAL; 320871d08516SMark Brown 3209f831b055SMark Brown /* Hide any masked bytes to ensure consistent data reporting */ 3210f831b055SMark Brown if (ret == 0 && params->mask) { 3211f831b055SMark Brown switch (codec->val_bytes) { 3212f831b055SMark Brown case 1: 3213f831b055SMark Brown ucontrol->value.bytes.data[0] &= ~params->mask; 3214f831b055SMark Brown break; 3215f831b055SMark Brown case 2: 3216f831b055SMark Brown ((u16 *)(&ucontrol->value.bytes.data))[0] 3217f831b055SMark Brown &= ~params->mask; 3218f831b055SMark Brown break; 3219f831b055SMark Brown case 4: 3220f831b055SMark Brown ((u32 *)(&ucontrol->value.bytes.data))[0] 3221f831b055SMark Brown &= ~params->mask; 3222f831b055SMark Brown break; 3223f831b055SMark Brown default: 3224f831b055SMark Brown return -EINVAL; 3225f831b055SMark Brown } 3226f831b055SMark Brown } 3227f831b055SMark Brown 322871d08516SMark Brown return ret; 322971d08516SMark Brown } 323071d08516SMark Brown EXPORT_SYMBOL_GPL(snd_soc_bytes_get); 323171d08516SMark Brown 323271d08516SMark Brown int snd_soc_bytes_put(struct snd_kcontrol *kcontrol, 323371d08516SMark Brown struct snd_ctl_elem_value *ucontrol) 323471d08516SMark Brown { 323571d08516SMark Brown struct soc_bytes *params = (void *)kcontrol->private_value; 323671d08516SMark Brown struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol); 3237f831b055SMark Brown int ret, len; 3238f831b055SMark Brown unsigned int val; 3239f831b055SMark Brown void *data; 324071d08516SMark Brown 3241f831b055SMark Brown if (!codec->using_regmap) 3242f831b055SMark Brown return -EINVAL; 3243f831b055SMark Brown 3244f831b055SMark Brown len = params->num_regs * codec->val_bytes; 3245f831b055SMark Brown 3246b5a8fe43SMark Brown data = kmemdup(ucontrol->value.bytes.data, len, GFP_KERNEL | GFP_DMA); 3247b5a8fe43SMark Brown if (!data) 3248b5a8fe43SMark Brown return -ENOMEM; 3249b5a8fe43SMark Brown 3250f831b055SMark Brown /* 3251f831b055SMark Brown * If we've got a mask then we need to preserve the register 3252f831b055SMark Brown * bits. We shouldn't modify the incoming data so take a 3253f831b055SMark Brown * copy. 3254f831b055SMark Brown */ 3255f831b055SMark Brown if (params->mask) { 3256f831b055SMark Brown ret = regmap_read(codec->control_data, params->base, &val); 3257f831b055SMark Brown if (ret != 0) 3258e8b18addSWei Yongjun goto out; 3259f831b055SMark Brown 3260f831b055SMark Brown val &= params->mask; 3261f831b055SMark Brown 3262f831b055SMark Brown switch (codec->val_bytes) { 3263f831b055SMark Brown case 1: 3264f831b055SMark Brown ((u8 *)data)[0] &= ~params->mask; 3265f831b055SMark Brown ((u8 *)data)[0] |= val; 3266f831b055SMark Brown break; 3267f831b055SMark Brown case 2: 3268f831b055SMark Brown ((u16 *)data)[0] &= cpu_to_be16(~params->mask); 3269f831b055SMark Brown ((u16 *)data)[0] |= cpu_to_be16(val); 3270f831b055SMark Brown break; 3271f831b055SMark Brown case 4: 3272f831b055SMark Brown ((u32 *)data)[0] &= cpu_to_be32(~params->mask); 3273f831b055SMark Brown ((u32 *)data)[0] |= cpu_to_be32(val); 3274f831b055SMark Brown break; 3275f831b055SMark Brown default: 3276e8b18addSWei Yongjun ret = -EINVAL; 3277e8b18addSWei Yongjun goto out; 3278f831b055SMark Brown } 3279f831b055SMark Brown } 3280f831b055SMark Brown 328171d08516SMark Brown ret = regmap_raw_write(codec->control_data, params->base, 3282f831b055SMark Brown data, len); 3283f831b055SMark Brown 3284e8b18addSWei Yongjun out: 3285f831b055SMark Brown kfree(data); 328671d08516SMark Brown 328771d08516SMark Brown return ret; 328871d08516SMark Brown } 328971d08516SMark Brown EXPORT_SYMBOL_GPL(snd_soc_bytes_put); 329071d08516SMark Brown 3291b6f4bb38Sapatard@mandriva.com /** 32924183eed2SKristoffer KARLSSON * snd_soc_info_xr_sx - signed multi register info callback 32934183eed2SKristoffer KARLSSON * @kcontrol: mreg control 32944183eed2SKristoffer KARLSSON * @uinfo: control element information 32954183eed2SKristoffer KARLSSON * 32964183eed2SKristoffer KARLSSON * Callback to provide information of a control that can 32974183eed2SKristoffer KARLSSON * span multiple codec registers which together 32984183eed2SKristoffer KARLSSON * forms a single signed value in a MSB/LSB manner. 32994183eed2SKristoffer KARLSSON * 33004183eed2SKristoffer KARLSSON * Returns 0 for success. 33014183eed2SKristoffer KARLSSON */ 33024183eed2SKristoffer KARLSSON int snd_soc_info_xr_sx(struct snd_kcontrol *kcontrol, 33034183eed2SKristoffer KARLSSON struct snd_ctl_elem_info *uinfo) 33044183eed2SKristoffer KARLSSON { 33054183eed2SKristoffer KARLSSON struct soc_mreg_control *mc = 33064183eed2SKristoffer KARLSSON (struct soc_mreg_control *)kcontrol->private_value; 33074183eed2SKristoffer KARLSSON uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER; 33084183eed2SKristoffer KARLSSON uinfo->count = 1; 33094183eed2SKristoffer KARLSSON uinfo->value.integer.min = mc->min; 33104183eed2SKristoffer KARLSSON uinfo->value.integer.max = mc->max; 33114183eed2SKristoffer KARLSSON 33124183eed2SKristoffer KARLSSON return 0; 33134183eed2SKristoffer KARLSSON } 33144183eed2SKristoffer KARLSSON EXPORT_SYMBOL_GPL(snd_soc_info_xr_sx); 33154183eed2SKristoffer KARLSSON 33164183eed2SKristoffer KARLSSON /** 33174183eed2SKristoffer KARLSSON * snd_soc_get_xr_sx - signed multi register get callback 33184183eed2SKristoffer KARLSSON * @kcontrol: mreg control 33194183eed2SKristoffer KARLSSON * @ucontrol: control element information 33204183eed2SKristoffer KARLSSON * 33214183eed2SKristoffer KARLSSON * Callback to get the value of a control that can span 33224183eed2SKristoffer KARLSSON * multiple codec registers which together forms a single 33234183eed2SKristoffer KARLSSON * signed value in a MSB/LSB manner. The control supports 33244183eed2SKristoffer KARLSSON * specifying total no of bits used to allow for bitfields 33254183eed2SKristoffer KARLSSON * across the multiple codec registers. 33264183eed2SKristoffer KARLSSON * 33274183eed2SKristoffer KARLSSON * Returns 0 for success. 33284183eed2SKristoffer KARLSSON */ 33294183eed2SKristoffer KARLSSON int snd_soc_get_xr_sx(struct snd_kcontrol *kcontrol, 33304183eed2SKristoffer KARLSSON struct snd_ctl_elem_value *ucontrol) 33314183eed2SKristoffer KARLSSON { 33324183eed2SKristoffer KARLSSON struct soc_mreg_control *mc = 33334183eed2SKristoffer KARLSSON (struct soc_mreg_control *)kcontrol->private_value; 33344183eed2SKristoffer KARLSSON struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol); 33354183eed2SKristoffer KARLSSON unsigned int regbase = mc->regbase; 33364183eed2SKristoffer KARLSSON unsigned int regcount = mc->regcount; 33374183eed2SKristoffer KARLSSON unsigned int regwshift = codec->driver->reg_word_size * BITS_PER_BYTE; 33384183eed2SKristoffer KARLSSON unsigned int regwmask = (1<<regwshift)-1; 33394183eed2SKristoffer KARLSSON unsigned int invert = mc->invert; 33404183eed2SKristoffer KARLSSON unsigned long mask = (1UL<<mc->nbits)-1; 33414183eed2SKristoffer KARLSSON long min = mc->min; 33424183eed2SKristoffer KARLSSON long max = mc->max; 33434183eed2SKristoffer KARLSSON long val = 0; 33444183eed2SKristoffer KARLSSON unsigned long regval; 33454183eed2SKristoffer KARLSSON unsigned int i; 33464183eed2SKristoffer KARLSSON 33474183eed2SKristoffer KARLSSON for (i = 0; i < regcount; i++) { 33484183eed2SKristoffer KARLSSON regval = snd_soc_read(codec, regbase+i) & regwmask; 33494183eed2SKristoffer KARLSSON val |= regval << (regwshift*(regcount-i-1)); 33504183eed2SKristoffer KARLSSON } 33514183eed2SKristoffer KARLSSON val &= mask; 33524183eed2SKristoffer KARLSSON if (min < 0 && val > max) 33534183eed2SKristoffer KARLSSON val |= ~mask; 33544183eed2SKristoffer KARLSSON if (invert) 33554183eed2SKristoffer KARLSSON val = max - val; 33564183eed2SKristoffer KARLSSON ucontrol->value.integer.value[0] = val; 33574183eed2SKristoffer KARLSSON 33584183eed2SKristoffer KARLSSON return 0; 33594183eed2SKristoffer KARLSSON } 33604183eed2SKristoffer KARLSSON EXPORT_SYMBOL_GPL(snd_soc_get_xr_sx); 33614183eed2SKristoffer KARLSSON 33624183eed2SKristoffer KARLSSON /** 33634183eed2SKristoffer KARLSSON * snd_soc_put_xr_sx - signed multi register get callback 33644183eed2SKristoffer KARLSSON * @kcontrol: mreg control 33654183eed2SKristoffer KARLSSON * @ucontrol: control element information 33664183eed2SKristoffer KARLSSON * 33674183eed2SKristoffer KARLSSON * Callback to set the value of a control that can span 33684183eed2SKristoffer KARLSSON * multiple codec registers which together forms a single 33694183eed2SKristoffer KARLSSON * signed value in a MSB/LSB manner. The control supports 33704183eed2SKristoffer KARLSSON * specifying total no of bits used to allow for bitfields 33714183eed2SKristoffer KARLSSON * across the multiple codec registers. 33724183eed2SKristoffer KARLSSON * 33734183eed2SKristoffer KARLSSON * Returns 0 for success. 33744183eed2SKristoffer KARLSSON */ 33754183eed2SKristoffer KARLSSON int snd_soc_put_xr_sx(struct snd_kcontrol *kcontrol, 33764183eed2SKristoffer KARLSSON struct snd_ctl_elem_value *ucontrol) 33774183eed2SKristoffer KARLSSON { 33784183eed2SKristoffer KARLSSON struct soc_mreg_control *mc = 33794183eed2SKristoffer KARLSSON (struct soc_mreg_control *)kcontrol->private_value; 33804183eed2SKristoffer KARLSSON struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol); 33814183eed2SKristoffer KARLSSON unsigned int regbase = mc->regbase; 33824183eed2SKristoffer KARLSSON unsigned int regcount = mc->regcount; 33834183eed2SKristoffer KARLSSON unsigned int regwshift = codec->driver->reg_word_size * BITS_PER_BYTE; 33844183eed2SKristoffer KARLSSON unsigned int regwmask = (1<<regwshift)-1; 33854183eed2SKristoffer KARLSSON unsigned int invert = mc->invert; 33864183eed2SKristoffer KARLSSON unsigned long mask = (1UL<<mc->nbits)-1; 33874183eed2SKristoffer KARLSSON long max = mc->max; 33884183eed2SKristoffer KARLSSON long val = ucontrol->value.integer.value[0]; 33894183eed2SKristoffer KARLSSON unsigned int i, regval, regmask; 33904183eed2SKristoffer KARLSSON int err; 33914183eed2SKristoffer KARLSSON 33924183eed2SKristoffer KARLSSON if (invert) 33934183eed2SKristoffer KARLSSON val = max - val; 33944183eed2SKristoffer KARLSSON val &= mask; 33954183eed2SKristoffer KARLSSON for (i = 0; i < regcount; i++) { 33964183eed2SKristoffer KARLSSON regval = (val >> (regwshift*(regcount-i-1))) & regwmask; 33974183eed2SKristoffer KARLSSON regmask = (mask >> (regwshift*(regcount-i-1))) & regwmask; 33984183eed2SKristoffer KARLSSON err = snd_soc_update_bits_locked(codec, regbase+i, 33994183eed2SKristoffer KARLSSON regmask, regval); 34004183eed2SKristoffer KARLSSON if (err < 0) 34014183eed2SKristoffer KARLSSON return err; 34024183eed2SKristoffer KARLSSON } 34034183eed2SKristoffer KARLSSON 34044183eed2SKristoffer KARLSSON return 0; 34054183eed2SKristoffer KARLSSON } 34064183eed2SKristoffer KARLSSON EXPORT_SYMBOL_GPL(snd_soc_put_xr_sx); 34074183eed2SKristoffer KARLSSON 34084183eed2SKristoffer KARLSSON /** 3409dd7b10b3SKristoffer KARLSSON * snd_soc_get_strobe - strobe get callback 3410dd7b10b3SKristoffer KARLSSON * @kcontrol: mixer control 3411dd7b10b3SKristoffer KARLSSON * @ucontrol: control element information 3412dd7b10b3SKristoffer KARLSSON * 3413dd7b10b3SKristoffer KARLSSON * Callback get the value of a strobe mixer control. 3414dd7b10b3SKristoffer KARLSSON * 3415dd7b10b3SKristoffer KARLSSON * Returns 0 for success. 3416dd7b10b3SKristoffer KARLSSON */ 3417dd7b10b3SKristoffer KARLSSON int snd_soc_get_strobe(struct snd_kcontrol *kcontrol, 3418dd7b10b3SKristoffer KARLSSON struct snd_ctl_elem_value *ucontrol) 3419dd7b10b3SKristoffer KARLSSON { 3420dd7b10b3SKristoffer KARLSSON struct soc_mixer_control *mc = 3421dd7b10b3SKristoffer KARLSSON (struct soc_mixer_control *)kcontrol->private_value; 3422dd7b10b3SKristoffer KARLSSON struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol); 3423dd7b10b3SKristoffer KARLSSON unsigned int reg = mc->reg; 3424dd7b10b3SKristoffer KARLSSON unsigned int shift = mc->shift; 3425dd7b10b3SKristoffer KARLSSON unsigned int mask = 1 << shift; 3426dd7b10b3SKristoffer KARLSSON unsigned int invert = mc->invert != 0; 3427dd7b10b3SKristoffer KARLSSON unsigned int val = snd_soc_read(codec, reg) & mask; 3428dd7b10b3SKristoffer KARLSSON 3429dd7b10b3SKristoffer KARLSSON if (shift != 0 && val != 0) 3430dd7b10b3SKristoffer KARLSSON val = val >> shift; 3431dd7b10b3SKristoffer KARLSSON ucontrol->value.enumerated.item[0] = val ^ invert; 3432dd7b10b3SKristoffer KARLSSON 3433dd7b10b3SKristoffer KARLSSON return 0; 3434dd7b10b3SKristoffer KARLSSON } 3435dd7b10b3SKristoffer KARLSSON EXPORT_SYMBOL_GPL(snd_soc_get_strobe); 3436dd7b10b3SKristoffer KARLSSON 3437dd7b10b3SKristoffer KARLSSON /** 3438dd7b10b3SKristoffer KARLSSON * snd_soc_put_strobe - strobe put callback 3439dd7b10b3SKristoffer KARLSSON * @kcontrol: mixer control 3440dd7b10b3SKristoffer KARLSSON * @ucontrol: control element information 3441dd7b10b3SKristoffer KARLSSON * 3442dd7b10b3SKristoffer KARLSSON * Callback strobe a register bit to high then low (or the inverse) 3443dd7b10b3SKristoffer KARLSSON * in one pass of a single mixer enum control. 3444dd7b10b3SKristoffer KARLSSON * 3445dd7b10b3SKristoffer KARLSSON * Returns 1 for success. 3446dd7b10b3SKristoffer KARLSSON */ 3447dd7b10b3SKristoffer KARLSSON int snd_soc_put_strobe(struct snd_kcontrol *kcontrol, 3448dd7b10b3SKristoffer KARLSSON struct snd_ctl_elem_value *ucontrol) 3449dd7b10b3SKristoffer KARLSSON { 3450dd7b10b3SKristoffer KARLSSON struct soc_mixer_control *mc = 3451dd7b10b3SKristoffer KARLSSON (struct soc_mixer_control *)kcontrol->private_value; 3452dd7b10b3SKristoffer KARLSSON struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol); 3453dd7b10b3SKristoffer KARLSSON unsigned int reg = mc->reg; 3454dd7b10b3SKristoffer KARLSSON unsigned int shift = mc->shift; 3455dd7b10b3SKristoffer KARLSSON unsigned int mask = 1 << shift; 3456dd7b10b3SKristoffer KARLSSON unsigned int invert = mc->invert != 0; 3457dd7b10b3SKristoffer KARLSSON unsigned int strobe = ucontrol->value.enumerated.item[0] != 0; 3458dd7b10b3SKristoffer KARLSSON unsigned int val1 = (strobe ^ invert) ? mask : 0; 3459dd7b10b3SKristoffer KARLSSON unsigned int val2 = (strobe ^ invert) ? 0 : mask; 3460dd7b10b3SKristoffer KARLSSON int err; 3461dd7b10b3SKristoffer KARLSSON 3462dd7b10b3SKristoffer KARLSSON err = snd_soc_update_bits_locked(codec, reg, mask, val1); 3463dd7b10b3SKristoffer KARLSSON if (err < 0) 3464dd7b10b3SKristoffer KARLSSON return err; 3465dd7b10b3SKristoffer KARLSSON 3466dd7b10b3SKristoffer KARLSSON err = snd_soc_update_bits_locked(codec, reg, mask, val2); 3467dd7b10b3SKristoffer KARLSSON return err; 3468dd7b10b3SKristoffer KARLSSON } 3469dd7b10b3SKristoffer KARLSSON EXPORT_SYMBOL_GPL(snd_soc_put_strobe); 3470dd7b10b3SKristoffer KARLSSON 3471dd7b10b3SKristoffer KARLSSON /** 34728c6529dbSLiam Girdwood * snd_soc_dai_set_sysclk - configure DAI system or master clock. 34738c6529dbSLiam Girdwood * @dai: DAI 34748c6529dbSLiam Girdwood * @clk_id: DAI specific clock ID 34758c6529dbSLiam Girdwood * @freq: new clock frequency in Hz 34768c6529dbSLiam Girdwood * @dir: new clock direction - input/output. 34778c6529dbSLiam Girdwood * 34788c6529dbSLiam Girdwood * Configures the DAI master (MCLK) or system (SYSCLK) clocking. 34798c6529dbSLiam Girdwood */ 34808c6529dbSLiam Girdwood int snd_soc_dai_set_sysclk(struct snd_soc_dai *dai, int clk_id, 34818c6529dbSLiam Girdwood unsigned int freq, int dir) 34828c6529dbSLiam Girdwood { 3483f0fba2adSLiam Girdwood if (dai->driver && dai->driver->ops->set_sysclk) 3484f0fba2adSLiam Girdwood return dai->driver->ops->set_sysclk(dai, clk_id, freq, dir); 3485ec4ee52aSMark Brown else if (dai->codec && dai->codec->driver->set_sysclk) 3486da1c6ea6SMark Brown return dai->codec->driver->set_sysclk(dai->codec, clk_id, 0, 3487ec4ee52aSMark Brown freq, dir); 34888c6529dbSLiam Girdwood else 34898c6529dbSLiam Girdwood return -EINVAL; 34908c6529dbSLiam Girdwood } 34918c6529dbSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_dai_set_sysclk); 34928c6529dbSLiam Girdwood 34938c6529dbSLiam Girdwood /** 3494ec4ee52aSMark Brown * snd_soc_codec_set_sysclk - configure CODEC system or master clock. 3495ec4ee52aSMark Brown * @codec: CODEC 3496ec4ee52aSMark Brown * @clk_id: DAI specific clock ID 3497da1c6ea6SMark Brown * @source: Source for the clock 3498ec4ee52aSMark Brown * @freq: new clock frequency in Hz 3499ec4ee52aSMark Brown * @dir: new clock direction - input/output. 3500ec4ee52aSMark Brown * 3501ec4ee52aSMark Brown * Configures the CODEC master (MCLK) or system (SYSCLK) clocking. 3502ec4ee52aSMark Brown */ 3503ec4ee52aSMark Brown int snd_soc_codec_set_sysclk(struct snd_soc_codec *codec, int clk_id, 3504da1c6ea6SMark Brown int source, unsigned int freq, int dir) 3505ec4ee52aSMark Brown { 3506ec4ee52aSMark Brown if (codec->driver->set_sysclk) 3507da1c6ea6SMark Brown return codec->driver->set_sysclk(codec, clk_id, source, 3508da1c6ea6SMark Brown freq, dir); 3509ec4ee52aSMark Brown else 3510ec4ee52aSMark Brown return -EINVAL; 3511ec4ee52aSMark Brown } 3512ec4ee52aSMark Brown EXPORT_SYMBOL_GPL(snd_soc_codec_set_sysclk); 3513ec4ee52aSMark Brown 3514ec4ee52aSMark Brown /** 35158c6529dbSLiam Girdwood * snd_soc_dai_set_clkdiv - configure DAI clock dividers. 35168c6529dbSLiam Girdwood * @dai: DAI 3517ac11a2b3SMark Brown * @div_id: DAI specific clock divider ID 35188c6529dbSLiam Girdwood * @div: new clock divisor. 35198c6529dbSLiam Girdwood * 35208c6529dbSLiam Girdwood * Configures the clock dividers. This is used to derive the best DAI bit and 35218c6529dbSLiam Girdwood * frame clocks from the system or master clock. It's best to set the DAI bit 35228c6529dbSLiam Girdwood * and frame clocks as low as possible to save system power. 35238c6529dbSLiam Girdwood */ 35248c6529dbSLiam Girdwood int snd_soc_dai_set_clkdiv(struct snd_soc_dai *dai, 35258c6529dbSLiam Girdwood int div_id, int div) 35268c6529dbSLiam Girdwood { 3527f0fba2adSLiam Girdwood if (dai->driver && dai->driver->ops->set_clkdiv) 3528f0fba2adSLiam Girdwood return dai->driver->ops->set_clkdiv(dai, div_id, div); 35298c6529dbSLiam Girdwood else 35308c6529dbSLiam Girdwood return -EINVAL; 35318c6529dbSLiam Girdwood } 35328c6529dbSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_dai_set_clkdiv); 35338c6529dbSLiam Girdwood 35348c6529dbSLiam Girdwood /** 35358c6529dbSLiam Girdwood * snd_soc_dai_set_pll - configure DAI PLL. 35368c6529dbSLiam Girdwood * @dai: DAI 35378c6529dbSLiam Girdwood * @pll_id: DAI specific PLL ID 353885488037SMark Brown * @source: DAI specific source for the PLL 35398c6529dbSLiam Girdwood * @freq_in: PLL input clock frequency in Hz 35408c6529dbSLiam Girdwood * @freq_out: requested PLL output clock frequency in Hz 35418c6529dbSLiam Girdwood * 35428c6529dbSLiam Girdwood * Configures and enables PLL to generate output clock based on input clock. 35438c6529dbSLiam Girdwood */ 354485488037SMark Brown int snd_soc_dai_set_pll(struct snd_soc_dai *dai, int pll_id, int source, 354585488037SMark Brown unsigned int freq_in, unsigned int freq_out) 35468c6529dbSLiam Girdwood { 3547f0fba2adSLiam Girdwood if (dai->driver && dai->driver->ops->set_pll) 3548f0fba2adSLiam Girdwood return dai->driver->ops->set_pll(dai, pll_id, source, 354985488037SMark Brown freq_in, freq_out); 3550ec4ee52aSMark Brown else if (dai->codec && dai->codec->driver->set_pll) 3551ec4ee52aSMark Brown return dai->codec->driver->set_pll(dai->codec, pll_id, source, 3552ec4ee52aSMark Brown freq_in, freq_out); 35538c6529dbSLiam Girdwood else 35548c6529dbSLiam Girdwood return -EINVAL; 35558c6529dbSLiam Girdwood } 35568c6529dbSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_dai_set_pll); 35578c6529dbSLiam Girdwood 3558ec4ee52aSMark Brown /* 3559ec4ee52aSMark Brown * snd_soc_codec_set_pll - configure codec PLL. 3560ec4ee52aSMark Brown * @codec: CODEC 3561ec4ee52aSMark Brown * @pll_id: DAI specific PLL ID 3562ec4ee52aSMark Brown * @source: DAI specific source for the PLL 3563ec4ee52aSMark Brown * @freq_in: PLL input clock frequency in Hz 3564ec4ee52aSMark Brown * @freq_out: requested PLL output clock frequency in Hz 3565ec4ee52aSMark Brown * 3566ec4ee52aSMark Brown * Configures and enables PLL to generate output clock based on input clock. 3567ec4ee52aSMark Brown */ 3568ec4ee52aSMark Brown int snd_soc_codec_set_pll(struct snd_soc_codec *codec, int pll_id, int source, 3569ec4ee52aSMark Brown unsigned int freq_in, unsigned int freq_out) 3570ec4ee52aSMark Brown { 3571ec4ee52aSMark Brown if (codec->driver->set_pll) 3572ec4ee52aSMark Brown return codec->driver->set_pll(codec, pll_id, source, 3573ec4ee52aSMark Brown freq_in, freq_out); 3574ec4ee52aSMark Brown else 3575ec4ee52aSMark Brown return -EINVAL; 3576ec4ee52aSMark Brown } 3577ec4ee52aSMark Brown EXPORT_SYMBOL_GPL(snd_soc_codec_set_pll); 3578ec4ee52aSMark Brown 35798c6529dbSLiam Girdwood /** 35808c6529dbSLiam Girdwood * snd_soc_dai_set_fmt - configure DAI hardware audio format. 35818c6529dbSLiam Girdwood * @dai: DAI 35828c6529dbSLiam Girdwood * @fmt: SND_SOC_DAIFMT_ format value. 35838c6529dbSLiam Girdwood * 35848c6529dbSLiam Girdwood * Configures the DAI hardware format and clocking. 35858c6529dbSLiam Girdwood */ 35868c6529dbSLiam Girdwood int snd_soc_dai_set_fmt(struct snd_soc_dai *dai, unsigned int fmt) 35878c6529dbSLiam Girdwood { 35885e4ba569SShawn Guo if (dai->driver == NULL) 35898c6529dbSLiam Girdwood return -EINVAL; 35905e4ba569SShawn Guo if (dai->driver->ops->set_fmt == NULL) 35915e4ba569SShawn Guo return -ENOTSUPP; 35925e4ba569SShawn Guo return dai->driver->ops->set_fmt(dai, fmt); 35938c6529dbSLiam Girdwood } 35948c6529dbSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_dai_set_fmt); 35958c6529dbSLiam Girdwood 35968c6529dbSLiam Girdwood /** 35978c6529dbSLiam Girdwood * snd_soc_dai_set_tdm_slot - configure DAI TDM. 35988c6529dbSLiam Girdwood * @dai: DAI 3599a5479e38SDaniel Ribeiro * @tx_mask: bitmask representing active TX slots. 3600a5479e38SDaniel Ribeiro * @rx_mask: bitmask representing active RX slots. 36018c6529dbSLiam Girdwood * @slots: Number of slots in use. 3602a5479e38SDaniel Ribeiro * @slot_width: Width in bits for each slot. 36038c6529dbSLiam Girdwood * 36048c6529dbSLiam Girdwood * Configures a DAI for TDM operation. Both mask and slots are codec and DAI 36058c6529dbSLiam Girdwood * specific. 36068c6529dbSLiam Girdwood */ 36078c6529dbSLiam Girdwood int snd_soc_dai_set_tdm_slot(struct snd_soc_dai *dai, 3608a5479e38SDaniel Ribeiro unsigned int tx_mask, unsigned int rx_mask, int slots, int slot_width) 36098c6529dbSLiam Girdwood { 3610f0fba2adSLiam Girdwood if (dai->driver && dai->driver->ops->set_tdm_slot) 3611f0fba2adSLiam Girdwood return dai->driver->ops->set_tdm_slot(dai, tx_mask, rx_mask, 3612a5479e38SDaniel Ribeiro slots, slot_width); 36138c6529dbSLiam Girdwood else 36148c6529dbSLiam Girdwood return -EINVAL; 36158c6529dbSLiam Girdwood } 36168c6529dbSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_dai_set_tdm_slot); 36178c6529dbSLiam Girdwood 36188c6529dbSLiam Girdwood /** 3619472df3cbSBarry Song * snd_soc_dai_set_channel_map - configure DAI audio channel map 3620472df3cbSBarry Song * @dai: DAI 3621472df3cbSBarry Song * @tx_num: how many TX channels 3622472df3cbSBarry Song * @tx_slot: pointer to an array which imply the TX slot number channel 3623472df3cbSBarry Song * 0~num-1 uses 3624472df3cbSBarry Song * @rx_num: how many RX channels 3625472df3cbSBarry Song * @rx_slot: pointer to an array which imply the RX slot number channel 3626472df3cbSBarry Song * 0~num-1 uses 3627472df3cbSBarry Song * 3628472df3cbSBarry Song * configure the relationship between channel number and TDM slot number. 3629472df3cbSBarry Song */ 3630472df3cbSBarry Song int snd_soc_dai_set_channel_map(struct snd_soc_dai *dai, 3631472df3cbSBarry Song unsigned int tx_num, unsigned int *tx_slot, 3632472df3cbSBarry Song unsigned int rx_num, unsigned int *rx_slot) 3633472df3cbSBarry Song { 3634f0fba2adSLiam Girdwood if (dai->driver && dai->driver->ops->set_channel_map) 3635f0fba2adSLiam Girdwood return dai->driver->ops->set_channel_map(dai, tx_num, tx_slot, 3636472df3cbSBarry Song rx_num, rx_slot); 3637472df3cbSBarry Song else 3638472df3cbSBarry Song return -EINVAL; 3639472df3cbSBarry Song } 3640472df3cbSBarry Song EXPORT_SYMBOL_GPL(snd_soc_dai_set_channel_map); 3641472df3cbSBarry Song 3642472df3cbSBarry Song /** 36438c6529dbSLiam Girdwood * snd_soc_dai_set_tristate - configure DAI system or master clock. 36448c6529dbSLiam Girdwood * @dai: DAI 36458c6529dbSLiam Girdwood * @tristate: tristate enable 36468c6529dbSLiam Girdwood * 36478c6529dbSLiam Girdwood * Tristates the DAI so that others can use it. 36488c6529dbSLiam Girdwood */ 36498c6529dbSLiam Girdwood int snd_soc_dai_set_tristate(struct snd_soc_dai *dai, int tristate) 36508c6529dbSLiam Girdwood { 3651f0fba2adSLiam Girdwood if (dai->driver && dai->driver->ops->set_tristate) 3652f0fba2adSLiam Girdwood return dai->driver->ops->set_tristate(dai, tristate); 36538c6529dbSLiam Girdwood else 36548c6529dbSLiam Girdwood return -EINVAL; 36558c6529dbSLiam Girdwood } 36568c6529dbSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_dai_set_tristate); 36578c6529dbSLiam Girdwood 36588c6529dbSLiam Girdwood /** 36598c6529dbSLiam Girdwood * snd_soc_dai_digital_mute - configure DAI system or master clock. 36608c6529dbSLiam Girdwood * @dai: DAI 36618c6529dbSLiam Girdwood * @mute: mute enable 3662da18396fSMark Brown * @direction: stream to mute 36638c6529dbSLiam Girdwood * 36648c6529dbSLiam Girdwood * Mutes the DAI DAC. 36658c6529dbSLiam Girdwood */ 3666da18396fSMark Brown int snd_soc_dai_digital_mute(struct snd_soc_dai *dai, int mute, 3667da18396fSMark Brown int direction) 36688c6529dbSLiam Girdwood { 3669da18396fSMark Brown if (!dai->driver) 3670da18396fSMark Brown return -ENOTSUPP; 3671da18396fSMark Brown 3672da18396fSMark Brown if (dai->driver->ops->mute_stream) 3673da18396fSMark Brown return dai->driver->ops->mute_stream(dai, mute, direction); 3674da18396fSMark Brown else if (direction == SNDRV_PCM_STREAM_PLAYBACK && 3675da18396fSMark Brown dai->driver->ops->digital_mute) 3676f0fba2adSLiam Girdwood return dai->driver->ops->digital_mute(dai, mute); 36778c6529dbSLiam Girdwood else 367804570c62SMark Brown return -ENOTSUPP; 36798c6529dbSLiam Girdwood } 36808c6529dbSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_dai_digital_mute); 36818c6529dbSLiam Girdwood 3682c5af3a2eSMark Brown /** 3683c5af3a2eSMark Brown * snd_soc_register_card - Register a card with the ASoC core 3684c5af3a2eSMark Brown * 3685ac11a2b3SMark Brown * @card: Card to register 3686c5af3a2eSMark Brown * 3687c5af3a2eSMark Brown */ 368870a7ca34SVinod Koul int snd_soc_register_card(struct snd_soc_card *card) 3689c5af3a2eSMark Brown { 3690b19e6e7bSMark Brown int i, ret; 3691f0fba2adSLiam Girdwood 3692c5af3a2eSMark Brown if (!card->name || !card->dev) 3693c5af3a2eSMark Brown return -EINVAL; 3694c5af3a2eSMark Brown 36955a504963SStephen Warren for (i = 0; i < card->num_links; i++) { 36965a504963SStephen Warren struct snd_soc_dai_link *link = &card->dai_link[i]; 36975a504963SStephen Warren 36985a504963SStephen Warren /* 36995a504963SStephen Warren * Codec must be specified by 1 of name or OF node, 37005a504963SStephen Warren * not both or neither. 37015a504963SStephen Warren */ 37025a504963SStephen Warren if (!!link->codec_name == !!link->codec_of_node) { 370310e8aa9aSMichał Mirosław dev_err(card->dev, 370410e8aa9aSMichał Mirosław "ASoC: Neither/both codec name/of_node are set for %s\n", 370510e8aa9aSMichał Mirosław link->name); 37065a504963SStephen Warren return -EINVAL; 37075a504963SStephen Warren } 3708bc92657aSStephen Warren /* Codec DAI name must be specified */ 3709bc92657aSStephen Warren if (!link->codec_dai_name) { 371010e8aa9aSMichał Mirosław dev_err(card->dev, 371110e8aa9aSMichał Mirosław "ASoC: codec_dai_name not set for %s\n", 371210e8aa9aSMichał Mirosław link->name); 3713bc92657aSStephen Warren return -EINVAL; 3714bc92657aSStephen Warren } 37155a504963SStephen Warren 37165a504963SStephen Warren /* 37175a504963SStephen Warren * Platform may be specified by either name or OF node, but 37185a504963SStephen Warren * can be left unspecified, and a dummy platform will be used. 37195a504963SStephen Warren */ 37205a504963SStephen Warren if (link->platform_name && link->platform_of_node) { 372110e8aa9aSMichał Mirosław dev_err(card->dev, 372210e8aa9aSMichał Mirosław "ASoC: Both platform name/of_node are set for %s\n", 372310e8aa9aSMichał Mirosław link->name); 37245a504963SStephen Warren return -EINVAL; 37255a504963SStephen Warren } 37265a504963SStephen Warren 37275a504963SStephen Warren /* 3728bc92657aSStephen Warren * CPU device may be specified by either name or OF node, but 3729bc92657aSStephen Warren * can be left unspecified, and will be matched based on DAI 3730bc92657aSStephen Warren * name alone.. 37315a504963SStephen Warren */ 3732bc92657aSStephen Warren if (link->cpu_name && link->cpu_of_node) { 373310e8aa9aSMichał Mirosław dev_err(card->dev, 373410e8aa9aSMichał Mirosław "ASoC: Neither/both cpu name/of_node are set for %s\n", 373510e8aa9aSMichał Mirosław link->name); 3736bc92657aSStephen Warren return -EINVAL; 3737bc92657aSStephen Warren } 3738bc92657aSStephen Warren /* 3739bc92657aSStephen Warren * At least one of CPU DAI name or CPU device name/node must be 3740bc92657aSStephen Warren * specified 3741bc92657aSStephen Warren */ 3742bc92657aSStephen Warren if (!link->cpu_dai_name && 3743bc92657aSStephen Warren !(link->cpu_name || link->cpu_of_node)) { 374410e8aa9aSMichał Mirosław dev_err(card->dev, 374510e8aa9aSMichał Mirosław "ASoC: Neither cpu_dai_name nor cpu_name/of_node are set for %s\n", 374610e8aa9aSMichał Mirosław link->name); 37475a504963SStephen Warren return -EINVAL; 37485a504963SStephen Warren } 37495a504963SStephen Warren } 37505a504963SStephen Warren 3751ed77cc12SMark Brown dev_set_drvdata(card->dev, card); 3752ed77cc12SMark Brown 3753111c6419SStephen Warren snd_soc_initialize_card_lists(card); 3754111c6419SStephen Warren 3755150dd2f8SVinod Koul soc_init_card_debugfs(card); 3756150dd2f8SVinod Koul 3757181a6892SMark Brown card->rtd = devm_kzalloc(card->dev, 3758181a6892SMark Brown sizeof(struct snd_soc_pcm_runtime) * 37592eea392dSJarkko Nikula (card->num_links + card->num_aux_devs), 3760f0fba2adSLiam Girdwood GFP_KERNEL); 3761f0fba2adSLiam Girdwood if (card->rtd == NULL) 3762f0fba2adSLiam Girdwood return -ENOMEM; 3763a7dbb603SLiam Girdwood card->num_rtd = 0; 37642eea392dSJarkko Nikula card->rtd_aux = &card->rtd[card->num_links]; 3765f0fba2adSLiam Girdwood 3766f0fba2adSLiam Girdwood for (i = 0; i < card->num_links; i++) 3767f0fba2adSLiam Girdwood card->rtd[i].dai_link = &card->dai_link[i]; 3768f0fba2adSLiam Girdwood 3769c5af3a2eSMark Brown INIT_LIST_HEAD(&card->list); 3770db432b41SMark Brown INIT_LIST_HEAD(&card->dapm_dirty); 3771c5af3a2eSMark Brown card->instantiated = 0; 3772f0fba2adSLiam Girdwood mutex_init(&card->mutex); 3773a73fb2dfSLiam Girdwood mutex_init(&card->dapm_mutex); 3774c5af3a2eSMark Brown 3775b19e6e7bSMark Brown ret = snd_soc_instantiate_card(card); 3776b19e6e7bSMark Brown if (ret != 0) 3777b19e6e7bSMark Brown soc_cleanup_card_debugfs(card); 3778c5af3a2eSMark Brown 3779b19e6e7bSMark Brown return ret; 3780c5af3a2eSMark Brown } 378170a7ca34SVinod Koul EXPORT_SYMBOL_GPL(snd_soc_register_card); 3782c5af3a2eSMark Brown 3783c5af3a2eSMark Brown /** 3784c5af3a2eSMark Brown * snd_soc_unregister_card - Unregister a card with the ASoC core 3785c5af3a2eSMark Brown * 3786ac11a2b3SMark Brown * @card: Card to unregister 3787c5af3a2eSMark Brown * 3788c5af3a2eSMark Brown */ 378970a7ca34SVinod Koul int snd_soc_unregister_card(struct snd_soc_card *card) 3790c5af3a2eSMark Brown { 3791b0e26485SVinod Koul if (card->instantiated) 3792b0e26485SVinod Koul soc_cleanup_card_resources(card); 3793f110bfc7SLiam Girdwood dev_dbg(card->dev, "ASoC: Unregistered card '%s'\n", card->name); 3794c5af3a2eSMark Brown 3795c5af3a2eSMark Brown return 0; 3796c5af3a2eSMark Brown } 379770a7ca34SVinod Koul EXPORT_SYMBOL_GPL(snd_soc_unregister_card); 3798c5af3a2eSMark Brown 3799f0fba2adSLiam Girdwood /* 3800f0fba2adSLiam Girdwood * Simplify DAI link configuration by removing ".-1" from device names 3801f0fba2adSLiam Girdwood * and sanitizing names. 3802f0fba2adSLiam Girdwood */ 38030b9a214aSDimitris Papastamos static char *fmt_single_name(struct device *dev, int *id) 3804f0fba2adSLiam Girdwood { 3805f0fba2adSLiam Girdwood char *found, name[NAME_SIZE]; 3806f0fba2adSLiam Girdwood int id1, id2; 3807f0fba2adSLiam Girdwood 3808f0fba2adSLiam Girdwood if (dev_name(dev) == NULL) 3809f0fba2adSLiam Girdwood return NULL; 3810f0fba2adSLiam Girdwood 381158818a77SDimitris Papastamos strlcpy(name, dev_name(dev), NAME_SIZE); 3812f0fba2adSLiam Girdwood 3813f0fba2adSLiam Girdwood /* are we a "%s.%d" name (platform and SPI components) */ 3814f0fba2adSLiam Girdwood found = strstr(name, dev->driver->name); 3815f0fba2adSLiam Girdwood if (found) { 3816f0fba2adSLiam Girdwood /* get ID */ 3817f0fba2adSLiam Girdwood if (sscanf(&found[strlen(dev->driver->name)], ".%d", id) == 1) { 3818f0fba2adSLiam Girdwood 3819f0fba2adSLiam Girdwood /* discard ID from name if ID == -1 */ 3820f0fba2adSLiam Girdwood if (*id == -1) 3821f0fba2adSLiam Girdwood found[strlen(dev->driver->name)] = '\0'; 3822f0fba2adSLiam Girdwood } 3823f0fba2adSLiam Girdwood 3824f0fba2adSLiam Girdwood } else { 3825f0fba2adSLiam Girdwood /* I2C component devices are named "bus-addr" */ 3826f0fba2adSLiam Girdwood if (sscanf(name, "%x-%x", &id1, &id2) == 2) { 3827f0fba2adSLiam Girdwood char tmp[NAME_SIZE]; 3828f0fba2adSLiam Girdwood 3829f0fba2adSLiam Girdwood /* create unique ID number from I2C addr and bus */ 383005899446SJarkko Nikula *id = ((id1 & 0xffff) << 16) + id2; 3831f0fba2adSLiam Girdwood 3832f0fba2adSLiam Girdwood /* sanitize component name for DAI link creation */ 3833f0fba2adSLiam Girdwood snprintf(tmp, NAME_SIZE, "%s.%s", dev->driver->name, name); 383458818a77SDimitris Papastamos strlcpy(name, tmp, NAME_SIZE); 3835f0fba2adSLiam Girdwood } else 3836f0fba2adSLiam Girdwood *id = 0; 3837f0fba2adSLiam Girdwood } 3838f0fba2adSLiam Girdwood 3839f0fba2adSLiam Girdwood return kstrdup(name, GFP_KERNEL); 3840f0fba2adSLiam Girdwood } 3841f0fba2adSLiam Girdwood 3842f0fba2adSLiam Girdwood /* 3843f0fba2adSLiam Girdwood * Simplify DAI link naming for single devices with multiple DAIs by removing 3844f0fba2adSLiam Girdwood * any ".-1" and using the DAI name (instead of device name). 3845f0fba2adSLiam Girdwood */ 3846f0fba2adSLiam Girdwood static inline char *fmt_multiple_name(struct device *dev, 3847f0fba2adSLiam Girdwood struct snd_soc_dai_driver *dai_drv) 3848f0fba2adSLiam Girdwood { 3849f0fba2adSLiam Girdwood if (dai_drv->name == NULL) { 385010e8aa9aSMichał Mirosław dev_err(dev, 385110e8aa9aSMichał Mirosław "ASoC: error - multiple DAI %s registered with no name\n", 385210e8aa9aSMichał Mirosław dev_name(dev)); 3853f0fba2adSLiam Girdwood return NULL; 3854f0fba2adSLiam Girdwood } 3855f0fba2adSLiam Girdwood 3856f0fba2adSLiam Girdwood return kstrdup(dai_drv->name, GFP_KERNEL); 3857f0fba2adSLiam Girdwood } 3858f0fba2adSLiam Girdwood 38599115171aSMark Brown /** 38609115171aSMark Brown * snd_soc_register_dai - Register a DAI with the ASoC core 38619115171aSMark Brown * 3862ac11a2b3SMark Brown * @dai: DAI to register 38639115171aSMark Brown */ 3864f53179c0SKuninori Morimoto static int snd_soc_register_dai(struct device *dev, 3865f0fba2adSLiam Girdwood struct snd_soc_dai_driver *dai_drv) 38669115171aSMark Brown { 3867054880feSMark Brown struct snd_soc_codec *codec; 3868f0fba2adSLiam Girdwood struct snd_soc_dai *dai; 38699115171aSMark Brown 3870f110bfc7SLiam Girdwood dev_dbg(dev, "ASoC: dai register %s\n", dev_name(dev)); 38719115171aSMark Brown 3872f0fba2adSLiam Girdwood dai = kzalloc(sizeof(struct snd_soc_dai), GFP_KERNEL); 3873f0fba2adSLiam Girdwood if (dai == NULL) 3874f0fba2adSLiam Girdwood return -ENOMEM; 38756335d055SEric Miao 3876f0fba2adSLiam Girdwood /* create DAI component name */ 3877f0fba2adSLiam Girdwood dai->name = fmt_single_name(dev, &dai->id); 3878f0fba2adSLiam Girdwood if (dai->name == NULL) { 3879f0fba2adSLiam Girdwood kfree(dai); 3880f0fba2adSLiam Girdwood return -ENOMEM; 3881f0fba2adSLiam Girdwood } 3882f0fba2adSLiam Girdwood 3883f0fba2adSLiam Girdwood dai->dev = dev; 3884f0fba2adSLiam Girdwood dai->driver = dai_drv; 3885be09ad90SLiam Girdwood dai->dapm.dev = dev; 3886f0fba2adSLiam Girdwood if (!dai->driver->ops) 3887f0fba2adSLiam Girdwood dai->driver->ops = &null_dai_ops; 38889115171aSMark Brown 38899115171aSMark Brown mutex_lock(&client_mutex); 3890054880feSMark Brown 3891054880feSMark Brown list_for_each_entry(codec, &codec_list, list) { 3892054880feSMark Brown if (codec->dev == dev) { 3893f110bfc7SLiam Girdwood dev_dbg(dev, "ASoC: Mapped DAI %s to CODEC %s\n", 3894054880feSMark Brown dai->name, codec->name); 3895054880feSMark Brown dai->codec = codec; 3896054880feSMark Brown break; 3897054880feSMark Brown } 3898054880feSMark Brown } 3899054880feSMark Brown 39005f800080SPeter Ujfalusi if (!dai->codec) 39015f800080SPeter Ujfalusi dai->dapm.idle_bias_off = 1; 39025f800080SPeter Ujfalusi 39039115171aSMark Brown list_add(&dai->list, &dai_list); 3904054880feSMark Brown 39059115171aSMark Brown mutex_unlock(&client_mutex); 39069115171aSMark Brown 3907f110bfc7SLiam Girdwood dev_dbg(dev, "ASoC: Registered DAI '%s'\n", dai->name); 39089115171aSMark Brown 39099115171aSMark Brown return 0; 39109115171aSMark Brown } 39119115171aSMark Brown 39129115171aSMark Brown /** 39139115171aSMark Brown * snd_soc_unregister_dai - Unregister a DAI from the ASoC core 39149115171aSMark Brown * 3915ac11a2b3SMark Brown * @dai: DAI to unregister 39169115171aSMark Brown */ 3917f53179c0SKuninori Morimoto static void snd_soc_unregister_dai(struct device *dev) 39189115171aSMark Brown { 3919f0fba2adSLiam Girdwood struct snd_soc_dai *dai; 3920f0fba2adSLiam Girdwood 3921f0fba2adSLiam Girdwood list_for_each_entry(dai, &dai_list, list) { 3922f0fba2adSLiam Girdwood if (dev == dai->dev) 3923f0fba2adSLiam Girdwood goto found; 3924f0fba2adSLiam Girdwood } 3925f0fba2adSLiam Girdwood return; 3926f0fba2adSLiam Girdwood 3927f0fba2adSLiam Girdwood found: 39289115171aSMark Brown mutex_lock(&client_mutex); 39299115171aSMark Brown list_del(&dai->list); 39309115171aSMark Brown mutex_unlock(&client_mutex); 39319115171aSMark Brown 3932f110bfc7SLiam Girdwood dev_dbg(dev, "ASoC: Unregistered DAI '%s'\n", dai->name); 3933f0fba2adSLiam Girdwood kfree(dai->name); 3934f0fba2adSLiam Girdwood kfree(dai); 39359115171aSMark Brown } 39369115171aSMark Brown 39379115171aSMark Brown /** 39389115171aSMark Brown * snd_soc_register_dais - Register multiple DAIs with the ASoC core 39399115171aSMark Brown * 3940ac11a2b3SMark Brown * @dai: Array of DAIs to register 3941ac11a2b3SMark Brown * @count: Number of DAIs 39429115171aSMark Brown */ 3943f53179c0SKuninori Morimoto static int snd_soc_register_dais(struct device *dev, 3944f0fba2adSLiam Girdwood struct snd_soc_dai_driver *dai_drv, size_t count) 39459115171aSMark Brown { 3946054880feSMark Brown struct snd_soc_codec *codec; 3947f0fba2adSLiam Girdwood struct snd_soc_dai *dai; 3948f0fba2adSLiam Girdwood int i, ret = 0; 3949f0fba2adSLiam Girdwood 3950f110bfc7SLiam Girdwood dev_dbg(dev, "ASoC: dai register %s #%Zu\n", dev_name(dev), count); 39519115171aSMark Brown 39529115171aSMark Brown for (i = 0; i < count; i++) { 3953f0fba2adSLiam Girdwood 3954f0fba2adSLiam Girdwood dai = kzalloc(sizeof(struct snd_soc_dai), GFP_KERNEL); 3955c46e0079SAxel Lin if (dai == NULL) { 3956c46e0079SAxel Lin ret = -ENOMEM; 3957c46e0079SAxel Lin goto err; 3958c46e0079SAxel Lin } 3959f0fba2adSLiam Girdwood 3960f0fba2adSLiam Girdwood /* create DAI component name */ 3961f0fba2adSLiam Girdwood dai->name = fmt_multiple_name(dev, &dai_drv[i]); 3962f0fba2adSLiam Girdwood if (dai->name == NULL) { 3963f0fba2adSLiam Girdwood kfree(dai); 3964f0fba2adSLiam Girdwood ret = -EINVAL; 39659115171aSMark Brown goto err; 39669115171aSMark Brown } 39679115171aSMark Brown 3968f0fba2adSLiam Girdwood dai->dev = dev; 3969f0fba2adSLiam Girdwood dai->driver = &dai_drv[i]; 39700f9141c9SMark Brown if (dai->driver->id) 39710f9141c9SMark Brown dai->id = dai->driver->id; 39720f9141c9SMark Brown else 39730f9141c9SMark Brown dai->id = i; 3974be09ad90SLiam Girdwood dai->dapm.dev = dev; 3975f0fba2adSLiam Girdwood if (!dai->driver->ops) 3976f0fba2adSLiam Girdwood dai->driver->ops = &null_dai_ops; 3977f0fba2adSLiam Girdwood 3978f0fba2adSLiam Girdwood mutex_lock(&client_mutex); 3979054880feSMark Brown 3980054880feSMark Brown list_for_each_entry(codec, &codec_list, list) { 3981054880feSMark Brown if (codec->dev == dev) { 398210e8aa9aSMichał Mirosław dev_dbg(dev, 398310e8aa9aSMichał Mirosław "ASoC: Mapped DAI %s to CODEC %s\n", 398410e8aa9aSMichał Mirosław dai->name, codec->name); 3985054880feSMark Brown dai->codec = codec; 3986054880feSMark Brown break; 3987054880feSMark Brown } 3988054880feSMark Brown } 3989054880feSMark Brown 39905f800080SPeter Ujfalusi if (!dai->codec) 39915f800080SPeter Ujfalusi dai->dapm.idle_bias_off = 1; 39925f800080SPeter Ujfalusi 3993f0fba2adSLiam Girdwood list_add(&dai->list, &dai_list); 3994054880feSMark Brown 3995f0fba2adSLiam Girdwood mutex_unlock(&client_mutex); 3996f0fba2adSLiam Girdwood 3997f110bfc7SLiam Girdwood dev_dbg(dai->dev, "ASoC: Registered DAI '%s'\n", dai->name); 3998f0fba2adSLiam Girdwood } 3999f0fba2adSLiam Girdwood 40009115171aSMark Brown return 0; 40019115171aSMark Brown 40029115171aSMark Brown err: 40039115171aSMark Brown for (i--; i >= 0; i--) 4004f0fba2adSLiam Girdwood snd_soc_unregister_dai(dev); 40059115171aSMark Brown 40069115171aSMark Brown return ret; 40079115171aSMark Brown } 40089115171aSMark Brown 40099115171aSMark Brown /** 40109115171aSMark Brown * snd_soc_unregister_dais - Unregister multiple DAIs from the ASoC core 40119115171aSMark Brown * 4012ac11a2b3SMark Brown * @dai: Array of DAIs to unregister 4013ac11a2b3SMark Brown * @count: Number of DAIs 40149115171aSMark Brown */ 4015f53179c0SKuninori Morimoto static void snd_soc_unregister_dais(struct device *dev, size_t count) 40169115171aSMark Brown { 40179115171aSMark Brown int i; 40189115171aSMark Brown 40199115171aSMark Brown for (i = 0; i < count; i++) 4020f0fba2adSLiam Girdwood snd_soc_unregister_dai(dev); 40219115171aSMark Brown } 40229115171aSMark Brown 402312a48a8cSMark Brown /** 4024d191bd8dSKuninori Morimoto * snd_soc_register_component - Register a component with the ASoC core 4025d191bd8dSKuninori Morimoto * 4026d191bd8dSKuninori Morimoto */ 4027d191bd8dSKuninori Morimoto static int 4028d191bd8dSKuninori Morimoto __snd_soc_register_component(struct device *dev, 4029d191bd8dSKuninori Morimoto struct snd_soc_component *cmpnt, 4030d191bd8dSKuninori Morimoto const struct snd_soc_component_driver *cmpnt_drv, 4031d191bd8dSKuninori Morimoto struct snd_soc_dai_driver *dai_drv, 4032d191bd8dSKuninori Morimoto int num_dai, bool allow_single_dai) 4033d191bd8dSKuninori Morimoto { 4034d191bd8dSKuninori Morimoto int ret; 4035d191bd8dSKuninori Morimoto 4036d191bd8dSKuninori Morimoto dev_dbg(dev, "component register %s\n", dev_name(dev)); 4037d191bd8dSKuninori Morimoto 4038d191bd8dSKuninori Morimoto if (!cmpnt) { 4039d191bd8dSKuninori Morimoto dev_err(dev, "ASoC: Failed to connecting component\n"); 4040d191bd8dSKuninori Morimoto return -ENOMEM; 4041d191bd8dSKuninori Morimoto } 4042d191bd8dSKuninori Morimoto 4043d191bd8dSKuninori Morimoto cmpnt->name = fmt_single_name(dev, &cmpnt->id); 4044d191bd8dSKuninori Morimoto if (!cmpnt->name) { 4045d191bd8dSKuninori Morimoto dev_err(dev, "ASoC: Failed to simplifying name\n"); 4046d191bd8dSKuninori Morimoto return -ENOMEM; 4047d191bd8dSKuninori Morimoto } 4048d191bd8dSKuninori Morimoto 4049d191bd8dSKuninori Morimoto cmpnt->dev = dev; 4050d191bd8dSKuninori Morimoto cmpnt->driver = cmpnt_drv; 4051d191bd8dSKuninori Morimoto cmpnt->num_dai = num_dai; 4052d191bd8dSKuninori Morimoto 4053d191bd8dSKuninori Morimoto /* 4054d191bd8dSKuninori Morimoto * snd_soc_register_dai() uses fmt_single_name(), and 4055d191bd8dSKuninori Morimoto * snd_soc_register_dais() uses fmt_multiple_name() 4056d191bd8dSKuninori Morimoto * for dai->name which is used for name based matching 4057d191bd8dSKuninori Morimoto * 4058d191bd8dSKuninori Morimoto * this function is used from cpu/codec. 4059d191bd8dSKuninori Morimoto * allow_single_dai flag can ignore "codec" driver reworking 4060d191bd8dSKuninori Morimoto * since it had been used snd_soc_register_dais(), 4061d191bd8dSKuninori Morimoto */ 4062d191bd8dSKuninori Morimoto if ((1 == num_dai) && allow_single_dai) 4063d191bd8dSKuninori Morimoto ret = snd_soc_register_dai(dev, dai_drv); 4064d191bd8dSKuninori Morimoto else 4065d191bd8dSKuninori Morimoto ret = snd_soc_register_dais(dev, dai_drv, num_dai); 4066d191bd8dSKuninori Morimoto if (ret < 0) { 4067d191bd8dSKuninori Morimoto dev_err(dev, "ASoC: Failed to regster DAIs: %d\n", ret); 4068d191bd8dSKuninori Morimoto goto error_component_name; 4069d191bd8dSKuninori Morimoto } 4070d191bd8dSKuninori Morimoto 4071d191bd8dSKuninori Morimoto mutex_lock(&client_mutex); 4072d191bd8dSKuninori Morimoto list_add(&cmpnt->list, &component_list); 4073d191bd8dSKuninori Morimoto mutex_unlock(&client_mutex); 4074d191bd8dSKuninori Morimoto 4075d191bd8dSKuninori Morimoto dev_dbg(cmpnt->dev, "ASoC: Registered component '%s'\n", cmpnt->name); 4076d191bd8dSKuninori Morimoto 4077d191bd8dSKuninori Morimoto return ret; 4078d191bd8dSKuninori Morimoto 4079d191bd8dSKuninori Morimoto error_component_name: 4080d191bd8dSKuninori Morimoto kfree(cmpnt->name); 4081d191bd8dSKuninori Morimoto 4082d191bd8dSKuninori Morimoto return ret; 4083d191bd8dSKuninori Morimoto } 4084d191bd8dSKuninori Morimoto 4085d191bd8dSKuninori Morimoto int snd_soc_register_component(struct device *dev, 4086d191bd8dSKuninori Morimoto const struct snd_soc_component_driver *cmpnt_drv, 4087d191bd8dSKuninori Morimoto struct snd_soc_dai_driver *dai_drv, 4088d191bd8dSKuninori Morimoto int num_dai) 4089d191bd8dSKuninori Morimoto { 4090d191bd8dSKuninori Morimoto struct snd_soc_component *cmpnt; 4091d191bd8dSKuninori Morimoto 4092d191bd8dSKuninori Morimoto cmpnt = devm_kzalloc(dev, sizeof(*cmpnt), GFP_KERNEL); 4093d191bd8dSKuninori Morimoto if (!cmpnt) { 4094d191bd8dSKuninori Morimoto dev_err(dev, "ASoC: Failed to allocate memory\n"); 4095d191bd8dSKuninori Morimoto return -ENOMEM; 4096d191bd8dSKuninori Morimoto } 4097d191bd8dSKuninori Morimoto 4098d191bd8dSKuninori Morimoto return __snd_soc_register_component(dev, cmpnt, cmpnt_drv, 4099d191bd8dSKuninori Morimoto dai_drv, num_dai, true); 4100d191bd8dSKuninori Morimoto } 4101d191bd8dSKuninori Morimoto EXPORT_SYMBOL_GPL(snd_soc_register_component); 4102d191bd8dSKuninori Morimoto 4103d191bd8dSKuninori Morimoto /** 4104d191bd8dSKuninori Morimoto * snd_soc_unregister_component - Unregister a component from the ASoC core 4105d191bd8dSKuninori Morimoto * 4106d191bd8dSKuninori Morimoto */ 4107d191bd8dSKuninori Morimoto void snd_soc_unregister_component(struct device *dev) 4108d191bd8dSKuninori Morimoto { 4109d191bd8dSKuninori Morimoto struct snd_soc_component *cmpnt; 4110d191bd8dSKuninori Morimoto 4111d191bd8dSKuninori Morimoto list_for_each_entry(cmpnt, &component_list, list) { 4112d191bd8dSKuninori Morimoto if (dev == cmpnt->dev) 4113d191bd8dSKuninori Morimoto goto found; 4114d191bd8dSKuninori Morimoto } 4115d191bd8dSKuninori Morimoto return; 4116d191bd8dSKuninori Morimoto 4117d191bd8dSKuninori Morimoto found: 4118d191bd8dSKuninori Morimoto snd_soc_unregister_dais(dev, cmpnt->num_dai); 4119d191bd8dSKuninori Morimoto 4120d191bd8dSKuninori Morimoto mutex_lock(&client_mutex); 4121d191bd8dSKuninori Morimoto list_del(&cmpnt->list); 4122d191bd8dSKuninori Morimoto mutex_unlock(&client_mutex); 4123d191bd8dSKuninori Morimoto 4124d191bd8dSKuninori Morimoto dev_dbg(dev, "ASoC: Unregistered component '%s'\n", cmpnt->name); 4125d191bd8dSKuninori Morimoto kfree(cmpnt->name); 4126d191bd8dSKuninori Morimoto } 4127d191bd8dSKuninori Morimoto EXPORT_SYMBOL_GPL(snd_soc_unregister_component); 4128d191bd8dSKuninori Morimoto 4129d191bd8dSKuninori Morimoto /** 413071a45cdaSLars-Peter Clausen * snd_soc_add_platform - Add a platform to the ASoC core 413171a45cdaSLars-Peter Clausen * @dev: The parent device for the platform 413271a45cdaSLars-Peter Clausen * @platform: The platform to add 413371a45cdaSLars-Peter Clausen * @platform_driver: The driver for the platform 413412a48a8cSMark Brown */ 413571a45cdaSLars-Peter Clausen int snd_soc_add_platform(struct device *dev, struct snd_soc_platform *platform, 4136d79e57dbSLars-Peter Clausen const struct snd_soc_platform_driver *platform_drv) 413712a48a8cSMark Brown { 4138f0fba2adSLiam Girdwood /* create platform component name */ 4139f0fba2adSLiam Girdwood platform->name = fmt_single_name(dev, &platform->id); 4140b5c745fbSDan Carpenter if (platform->name == NULL) 4141f0fba2adSLiam Girdwood return -ENOMEM; 4142f0fba2adSLiam Girdwood 4143f0fba2adSLiam Girdwood platform->dev = dev; 4144f0fba2adSLiam Girdwood platform->driver = platform_drv; 4145b7950641SLiam Girdwood platform->dapm.dev = dev; 4146b7950641SLiam Girdwood platform->dapm.platform = platform; 414764a648c2SLiam Girdwood platform->dapm.stream_event = platform_drv->stream_event; 4148cc22d37eSLiam Girdwood mutex_init(&platform->mutex); 414912a48a8cSMark Brown 415012a48a8cSMark Brown mutex_lock(&client_mutex); 415112a48a8cSMark Brown list_add(&platform->list, &platform_list); 415212a48a8cSMark Brown mutex_unlock(&client_mutex); 415312a48a8cSMark Brown 4154f110bfc7SLiam Girdwood dev_dbg(dev, "ASoC: Registered platform '%s'\n", platform->name); 415512a48a8cSMark Brown 415612a48a8cSMark Brown return 0; 415712a48a8cSMark Brown } 415871a45cdaSLars-Peter Clausen EXPORT_SYMBOL_GPL(snd_soc_add_platform); 415971a45cdaSLars-Peter Clausen 416071a45cdaSLars-Peter Clausen /** 416171a45cdaSLars-Peter Clausen * snd_soc_register_platform - Register a platform with the ASoC core 416271a45cdaSLars-Peter Clausen * 416371a45cdaSLars-Peter Clausen * @platform: platform to register 416471a45cdaSLars-Peter Clausen */ 416571a45cdaSLars-Peter Clausen int snd_soc_register_platform(struct device *dev, 416671a45cdaSLars-Peter Clausen const struct snd_soc_platform_driver *platform_drv) 416771a45cdaSLars-Peter Clausen { 416871a45cdaSLars-Peter Clausen struct snd_soc_platform *platform; 416971a45cdaSLars-Peter Clausen int ret; 417071a45cdaSLars-Peter Clausen 417171a45cdaSLars-Peter Clausen dev_dbg(dev, "ASoC: platform register %s\n", dev_name(dev)); 417271a45cdaSLars-Peter Clausen 417371a45cdaSLars-Peter Clausen platform = kzalloc(sizeof(struct snd_soc_platform), GFP_KERNEL); 417471a45cdaSLars-Peter Clausen if (platform == NULL) 417571a45cdaSLars-Peter Clausen return -ENOMEM; 417671a45cdaSLars-Peter Clausen 417771a45cdaSLars-Peter Clausen ret = snd_soc_add_platform(dev, platform, platform_drv); 417871a45cdaSLars-Peter Clausen if (ret) 417971a45cdaSLars-Peter Clausen kfree(platform); 418071a45cdaSLars-Peter Clausen 418171a45cdaSLars-Peter Clausen return ret; 418271a45cdaSLars-Peter Clausen } 418312a48a8cSMark Brown EXPORT_SYMBOL_GPL(snd_soc_register_platform); 418412a48a8cSMark Brown 418512a48a8cSMark Brown /** 418671a45cdaSLars-Peter Clausen * snd_soc_remove_platform - Remove a platform from the ASoC core 418771a45cdaSLars-Peter Clausen * @platform: the platform to remove 418871a45cdaSLars-Peter Clausen */ 418971a45cdaSLars-Peter Clausen void snd_soc_remove_platform(struct snd_soc_platform *platform) 419071a45cdaSLars-Peter Clausen { 419171a45cdaSLars-Peter Clausen mutex_lock(&client_mutex); 419271a45cdaSLars-Peter Clausen list_del(&platform->list); 419371a45cdaSLars-Peter Clausen mutex_unlock(&client_mutex); 419471a45cdaSLars-Peter Clausen 419571a45cdaSLars-Peter Clausen dev_dbg(platform->dev, "ASoC: Unregistered platform '%s'\n", 419671a45cdaSLars-Peter Clausen platform->name); 419771a45cdaSLars-Peter Clausen kfree(platform->name); 419871a45cdaSLars-Peter Clausen } 419971a45cdaSLars-Peter Clausen EXPORT_SYMBOL_GPL(snd_soc_remove_platform); 420071a45cdaSLars-Peter Clausen 420171a45cdaSLars-Peter Clausen struct snd_soc_platform *snd_soc_lookup_platform(struct device *dev) 420271a45cdaSLars-Peter Clausen { 420371a45cdaSLars-Peter Clausen struct snd_soc_platform *platform; 420471a45cdaSLars-Peter Clausen 420571a45cdaSLars-Peter Clausen list_for_each_entry(platform, &platform_list, list) { 420671a45cdaSLars-Peter Clausen if (dev == platform->dev) 420771a45cdaSLars-Peter Clausen return platform; 420871a45cdaSLars-Peter Clausen } 420971a45cdaSLars-Peter Clausen 421071a45cdaSLars-Peter Clausen return NULL; 421171a45cdaSLars-Peter Clausen } 421271a45cdaSLars-Peter Clausen EXPORT_SYMBOL_GPL(snd_soc_lookup_platform); 421371a45cdaSLars-Peter Clausen 421471a45cdaSLars-Peter Clausen /** 421512a48a8cSMark Brown * snd_soc_unregister_platform - Unregister a platform from the ASoC core 421612a48a8cSMark Brown * 4217ac11a2b3SMark Brown * @platform: platform to unregister 421812a48a8cSMark Brown */ 4219f0fba2adSLiam Girdwood void snd_soc_unregister_platform(struct device *dev) 422012a48a8cSMark Brown { 4221f0fba2adSLiam Girdwood struct snd_soc_platform *platform; 4222f0fba2adSLiam Girdwood 422371a45cdaSLars-Peter Clausen platform = snd_soc_lookup_platform(dev); 422471a45cdaSLars-Peter Clausen if (!platform) 4225f0fba2adSLiam Girdwood return; 4226f0fba2adSLiam Girdwood 422771a45cdaSLars-Peter Clausen snd_soc_remove_platform(platform); 4228f0fba2adSLiam Girdwood kfree(platform); 422912a48a8cSMark Brown } 423012a48a8cSMark Brown EXPORT_SYMBOL_GPL(snd_soc_unregister_platform); 423112a48a8cSMark Brown 4232151ab22cSMark Brown static u64 codec_format_map[] = { 4233151ab22cSMark Brown SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S16_BE, 4234151ab22cSMark Brown SNDRV_PCM_FMTBIT_U16_LE | SNDRV_PCM_FMTBIT_U16_BE, 4235151ab22cSMark Brown SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S24_BE, 4236151ab22cSMark Brown SNDRV_PCM_FMTBIT_U24_LE | SNDRV_PCM_FMTBIT_U24_BE, 4237151ab22cSMark Brown SNDRV_PCM_FMTBIT_S32_LE | SNDRV_PCM_FMTBIT_S32_BE, 4238151ab22cSMark Brown SNDRV_PCM_FMTBIT_U32_LE | SNDRV_PCM_FMTBIT_U32_BE, 4239151ab22cSMark Brown SNDRV_PCM_FMTBIT_S24_3LE | SNDRV_PCM_FMTBIT_U24_3BE, 4240151ab22cSMark Brown SNDRV_PCM_FMTBIT_U24_3LE | SNDRV_PCM_FMTBIT_U24_3BE, 4241151ab22cSMark Brown SNDRV_PCM_FMTBIT_S20_3LE | SNDRV_PCM_FMTBIT_S20_3BE, 4242151ab22cSMark Brown SNDRV_PCM_FMTBIT_U20_3LE | SNDRV_PCM_FMTBIT_U20_3BE, 4243151ab22cSMark Brown SNDRV_PCM_FMTBIT_S18_3LE | SNDRV_PCM_FMTBIT_S18_3BE, 4244151ab22cSMark Brown SNDRV_PCM_FMTBIT_U18_3LE | SNDRV_PCM_FMTBIT_U18_3BE, 4245151ab22cSMark Brown SNDRV_PCM_FMTBIT_FLOAT_LE | SNDRV_PCM_FMTBIT_FLOAT_BE, 4246151ab22cSMark Brown SNDRV_PCM_FMTBIT_FLOAT64_LE | SNDRV_PCM_FMTBIT_FLOAT64_BE, 4247151ab22cSMark Brown SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_LE 4248151ab22cSMark Brown | SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_BE, 4249151ab22cSMark Brown }; 4250151ab22cSMark Brown 4251151ab22cSMark Brown /* Fix up the DAI formats for endianness: codecs don't actually see 4252151ab22cSMark Brown * the endianness of the data but we're using the CPU format 4253151ab22cSMark Brown * definitions which do need to include endianness so we ensure that 4254151ab22cSMark Brown * codec DAIs always have both big and little endian variants set. 4255151ab22cSMark Brown */ 4256151ab22cSMark Brown static void fixup_codec_formats(struct snd_soc_pcm_stream *stream) 4257151ab22cSMark Brown { 4258151ab22cSMark Brown int i; 4259151ab22cSMark Brown 4260151ab22cSMark Brown for (i = 0; i < ARRAY_SIZE(codec_format_map); i++) 4261151ab22cSMark Brown if (stream->formats & codec_format_map[i]) 4262151ab22cSMark Brown stream->formats |= codec_format_map[i]; 4263151ab22cSMark Brown } 4264151ab22cSMark Brown 42650d0cf00aSMark Brown /** 42660d0cf00aSMark Brown * snd_soc_register_codec - Register a codec with the ASoC core 42670d0cf00aSMark Brown * 4268ac11a2b3SMark Brown * @codec: codec to register 42690d0cf00aSMark Brown */ 4270f0fba2adSLiam Girdwood int snd_soc_register_codec(struct device *dev, 4271001ae4c0SMark Brown const struct snd_soc_codec_driver *codec_drv, 4272001ae4c0SMark Brown struct snd_soc_dai_driver *dai_drv, 4273001ae4c0SMark Brown int num_dai) 42740d0cf00aSMark Brown { 42753335ddcaSDimitris Papastamos size_t reg_size; 4276f0fba2adSLiam Girdwood struct snd_soc_codec *codec; 4277f0fba2adSLiam Girdwood int ret, i; 4278151ab22cSMark Brown 4279f0fba2adSLiam Girdwood dev_dbg(dev, "codec register %s\n", dev_name(dev)); 42800d0cf00aSMark Brown 4281f0fba2adSLiam Girdwood codec = kzalloc(sizeof(struct snd_soc_codec), GFP_KERNEL); 4282f0fba2adSLiam Girdwood if (codec == NULL) 4283f0fba2adSLiam Girdwood return -ENOMEM; 42840d0cf00aSMark Brown 4285f0fba2adSLiam Girdwood /* create CODEC component name */ 4286f0fba2adSLiam Girdwood codec->name = fmt_single_name(dev, &codec->id); 4287f0fba2adSLiam Girdwood if (codec->name == NULL) { 4288f790b94dSKuninori Morimoto ret = -ENOMEM; 4289f790b94dSKuninori Morimoto goto fail_codec; 4290151ab22cSMark Brown } 4291151ab22cSMark Brown 429223bbce34SDimitris Papastamos if (codec_drv->compress_type) 429323bbce34SDimitris Papastamos codec->compress_type = codec_drv->compress_type; 429423bbce34SDimitris Papastamos else 429523bbce34SDimitris Papastamos codec->compress_type = SND_SOC_FLAT_COMPRESSION; 429623bbce34SDimitris Papastamos 4297c3acec26SMark Brown codec->write = codec_drv->write; 4298c3acec26SMark Brown codec->read = codec_drv->read; 42991500b7b5SDimitris Papastamos codec->volatile_register = codec_drv->volatile_register; 43001500b7b5SDimitris Papastamos codec->readable_register = codec_drv->readable_register; 43018020454cSDimitris Papastamos codec->writable_register = codec_drv->writable_register; 43025124e69eSMark Brown codec->ignore_pmdown_time = codec_drv->ignore_pmdown_time; 4303ce6120ccSLiam Girdwood codec->dapm.bias_level = SND_SOC_BIAS_OFF; 4304ce6120ccSLiam Girdwood codec->dapm.dev = dev; 4305ce6120ccSLiam Girdwood codec->dapm.codec = codec; 4306474b62d6SMark Brown codec->dapm.seq_notifier = codec_drv->seq_notifier; 430764a648c2SLiam Girdwood codec->dapm.stream_event = codec_drv->stream_event; 4308f0fba2adSLiam Girdwood codec->dev = dev; 4309f0fba2adSLiam Girdwood codec->driver = codec_drv; 4310f0fba2adSLiam Girdwood codec->num_dai = num_dai; 4311f0fba2adSLiam Girdwood mutex_init(&codec->mutex); 4312f0fba2adSLiam Girdwood 43137a30a3dbSDimitris Papastamos /* allocate CODEC register cache */ 43147a30a3dbSDimitris Papastamos if (codec_drv->reg_cache_size && codec_drv->reg_word_size) { 43153335ddcaSDimitris Papastamos reg_size = codec_drv->reg_cache_size * codec_drv->reg_word_size; 4316aea170a0SDimitris Papastamos codec->reg_size = reg_size; 43173335ddcaSDimitris Papastamos /* it is necessary to make a copy of the default register cache 43183335ddcaSDimitris Papastamos * because in the case of using a compression type that requires 4319f6e65744SBill Pemberton * the default register cache to be marked as the 43203335ddcaSDimitris Papastamos * kernel might have freed the array by the time we initialize 43213335ddcaSDimitris Papastamos * the cache. 43223335ddcaSDimitris Papastamos */ 43232aa86323SDimitris Papastamos if (codec_drv->reg_cache_default) { 43243335ddcaSDimitris Papastamos codec->reg_def_copy = kmemdup(codec_drv->reg_cache_default, 43253335ddcaSDimitris Papastamos reg_size, GFP_KERNEL); 43263335ddcaSDimitris Papastamos if (!codec->reg_def_copy) { 43273335ddcaSDimitris Papastamos ret = -ENOMEM; 4328f790b94dSKuninori Morimoto goto fail_codec_name; 43297a30a3dbSDimitris Papastamos } 43307a30a3dbSDimitris Papastamos } 43312aa86323SDimitris Papastamos } 43327a30a3dbSDimitris Papastamos 43331500b7b5SDimitris Papastamos if (codec_drv->reg_access_size && codec_drv->reg_access_default) { 43341500b7b5SDimitris Papastamos if (!codec->volatile_register) 43351500b7b5SDimitris Papastamos codec->volatile_register = snd_soc_default_volatile_register; 43361500b7b5SDimitris Papastamos if (!codec->readable_register) 43371500b7b5SDimitris Papastamos codec->readable_register = snd_soc_default_readable_register; 43388020454cSDimitris Papastamos if (!codec->writable_register) 43398020454cSDimitris Papastamos codec->writable_register = snd_soc_default_writable_register; 43401500b7b5SDimitris Papastamos } 43411500b7b5SDimitris Papastamos 4342f0fba2adSLiam Girdwood for (i = 0; i < num_dai; i++) { 4343f0fba2adSLiam Girdwood fixup_codec_formats(&dai_drv[i].playback); 4344f0fba2adSLiam Girdwood fixup_codec_formats(&dai_drv[i].capture); 4345f0fba2adSLiam Girdwood } 4346f0fba2adSLiam Girdwood 4347054880feSMark Brown mutex_lock(&client_mutex); 4348054880feSMark Brown list_add(&codec->list, &codec_list); 4349054880feSMark Brown mutex_unlock(&client_mutex); 4350054880feSMark Brown 4351d191bd8dSKuninori Morimoto /* register component */ 4352d191bd8dSKuninori Morimoto ret = __snd_soc_register_component(dev, &codec->component, 4353d191bd8dSKuninori Morimoto &codec_drv->component_driver, 4354d191bd8dSKuninori Morimoto dai_drv, num_dai, false); 43555acd7dfbSKuninori Morimoto if (ret < 0) { 4356d191bd8dSKuninori Morimoto dev_err(codec->dev, "ASoC: Failed to regster component: %d\n", ret); 43575acd7dfbSKuninori Morimoto goto fail_codec_name; 435826b01ccdSMark Brown } 4359f0fba2adSLiam Girdwood 4360f110bfc7SLiam Girdwood dev_dbg(codec->dev, "ASoC: Registered codec '%s'\n", codec->name); 43610d0cf00aSMark Brown return 0; 4362f0fba2adSLiam Girdwood 4363f790b94dSKuninori Morimoto fail_codec_name: 4364e1328a83SKuninori Morimoto mutex_lock(&client_mutex); 4365e1328a83SKuninori Morimoto list_del(&codec->list); 4366e1328a83SKuninori Morimoto mutex_unlock(&client_mutex); 4367e1328a83SKuninori Morimoto 4368f0fba2adSLiam Girdwood kfree(codec->name); 4369f790b94dSKuninori Morimoto fail_codec: 4370f0fba2adSLiam Girdwood kfree(codec); 4371f0fba2adSLiam Girdwood return ret; 43720d0cf00aSMark Brown } 43730d0cf00aSMark Brown EXPORT_SYMBOL_GPL(snd_soc_register_codec); 43740d0cf00aSMark Brown 43750d0cf00aSMark Brown /** 43760d0cf00aSMark Brown * snd_soc_unregister_codec - Unregister a codec from the ASoC core 43770d0cf00aSMark Brown * 4378ac11a2b3SMark Brown * @codec: codec to unregister 43790d0cf00aSMark Brown */ 4380f0fba2adSLiam Girdwood void snd_soc_unregister_codec(struct device *dev) 43810d0cf00aSMark Brown { 4382f0fba2adSLiam Girdwood struct snd_soc_codec *codec; 4383f0fba2adSLiam Girdwood 4384f0fba2adSLiam Girdwood list_for_each_entry(codec, &codec_list, list) { 4385f0fba2adSLiam Girdwood if (dev == codec->dev) 4386f0fba2adSLiam Girdwood goto found; 4387f0fba2adSLiam Girdwood } 4388f0fba2adSLiam Girdwood return; 4389f0fba2adSLiam Girdwood 4390f0fba2adSLiam Girdwood found: 4391d191bd8dSKuninori Morimoto snd_soc_unregister_component(dev); 4392f0fba2adSLiam Girdwood 43930d0cf00aSMark Brown mutex_lock(&client_mutex); 43940d0cf00aSMark Brown list_del(&codec->list); 43950d0cf00aSMark Brown mutex_unlock(&client_mutex); 43960d0cf00aSMark Brown 4397f110bfc7SLiam Girdwood dev_dbg(codec->dev, "ASoC: Unregistered codec '%s'\n", codec->name); 4398f0fba2adSLiam Girdwood 43997a30a3dbSDimitris Papastamos snd_soc_cache_exit(codec); 44003335ddcaSDimitris Papastamos kfree(codec->reg_def_copy); 44011aafcd4dSDimitris Papastamos kfree(codec->name); 4402f0fba2adSLiam Girdwood kfree(codec); 44030d0cf00aSMark Brown } 44040d0cf00aSMark Brown EXPORT_SYMBOL_GPL(snd_soc_unregister_codec); 44050d0cf00aSMark Brown 4406bec4fa05SStephen Warren /* Retrieve a card's name from device tree */ 4407bec4fa05SStephen Warren int snd_soc_of_parse_card_name(struct snd_soc_card *card, 4408bec4fa05SStephen Warren const char *propname) 4409bec4fa05SStephen Warren { 4410bec4fa05SStephen Warren struct device_node *np = card->dev->of_node; 4411bec4fa05SStephen Warren int ret; 4412bec4fa05SStephen Warren 4413bec4fa05SStephen Warren ret = of_property_read_string_index(np, propname, 0, &card->name); 4414bec4fa05SStephen Warren /* 4415bec4fa05SStephen Warren * EINVAL means the property does not exist. This is fine providing 4416bec4fa05SStephen Warren * card->name was previously set, which is checked later in 4417bec4fa05SStephen Warren * snd_soc_register_card. 4418bec4fa05SStephen Warren */ 4419bec4fa05SStephen Warren if (ret < 0 && ret != -EINVAL) { 4420bec4fa05SStephen Warren dev_err(card->dev, 4421f110bfc7SLiam Girdwood "ASoC: Property '%s' could not be read: %d\n", 4422bec4fa05SStephen Warren propname, ret); 4423bec4fa05SStephen Warren return ret; 4424bec4fa05SStephen Warren } 4425bec4fa05SStephen Warren 4426bec4fa05SStephen Warren return 0; 4427bec4fa05SStephen Warren } 4428bec4fa05SStephen Warren EXPORT_SYMBOL_GPL(snd_soc_of_parse_card_name); 4429bec4fa05SStephen Warren 4430a4a54dd5SStephen Warren int snd_soc_of_parse_audio_routing(struct snd_soc_card *card, 4431a4a54dd5SStephen Warren const char *propname) 4432a4a54dd5SStephen Warren { 4433a4a54dd5SStephen Warren struct device_node *np = card->dev->of_node; 4434a4a54dd5SStephen Warren int num_routes; 4435a4a54dd5SStephen Warren struct snd_soc_dapm_route *routes; 4436a4a54dd5SStephen Warren int i, ret; 4437a4a54dd5SStephen Warren 4438a4a54dd5SStephen Warren num_routes = of_property_count_strings(np, propname); 4439c34ce320SRichard Zhao if (num_routes < 0 || num_routes & 1) { 444010e8aa9aSMichał Mirosław dev_err(card->dev, 444110e8aa9aSMichał Mirosław "ASoC: Property '%s' does not exist or its length is not even\n", 444210e8aa9aSMichał Mirosław propname); 4443a4a54dd5SStephen Warren return -EINVAL; 4444a4a54dd5SStephen Warren } 4445a4a54dd5SStephen Warren num_routes /= 2; 4446a4a54dd5SStephen Warren if (!num_routes) { 4447f110bfc7SLiam Girdwood dev_err(card->dev, "ASoC: Property '%s's length is zero\n", 4448a4a54dd5SStephen Warren propname); 4449a4a54dd5SStephen Warren return -EINVAL; 4450a4a54dd5SStephen Warren } 4451a4a54dd5SStephen Warren 4452a4a54dd5SStephen Warren routes = devm_kzalloc(card->dev, num_routes * sizeof(*routes), 4453a4a54dd5SStephen Warren GFP_KERNEL); 4454a4a54dd5SStephen Warren if (!routes) { 4455a4a54dd5SStephen Warren dev_err(card->dev, 4456f110bfc7SLiam Girdwood "ASoC: Could not allocate DAPM route table\n"); 4457a4a54dd5SStephen Warren return -EINVAL; 4458a4a54dd5SStephen Warren } 4459a4a54dd5SStephen Warren 4460a4a54dd5SStephen Warren for (i = 0; i < num_routes; i++) { 4461a4a54dd5SStephen Warren ret = of_property_read_string_index(np, propname, 4462a4a54dd5SStephen Warren 2 * i, &routes[i].sink); 4463a4a54dd5SStephen Warren if (ret) { 4464c871bd0bSMark Brown dev_err(card->dev, 4465c871bd0bSMark Brown "ASoC: Property '%s' index %d could not be read: %d\n", 4466c871bd0bSMark Brown propname, 2 * i, ret); 4467a4a54dd5SStephen Warren return -EINVAL; 4468a4a54dd5SStephen Warren } 4469a4a54dd5SStephen Warren ret = of_property_read_string_index(np, propname, 4470a4a54dd5SStephen Warren (2 * i) + 1, &routes[i].source); 4471a4a54dd5SStephen Warren if (ret) { 4472a4a54dd5SStephen Warren dev_err(card->dev, 4473c871bd0bSMark Brown "ASoC: Property '%s' index %d could not be read: %d\n", 4474c871bd0bSMark Brown propname, (2 * i) + 1, ret); 4475a4a54dd5SStephen Warren return -EINVAL; 4476a4a54dd5SStephen Warren } 4477a4a54dd5SStephen Warren } 4478a4a54dd5SStephen Warren 4479a4a54dd5SStephen Warren card->num_dapm_routes = num_routes; 4480a4a54dd5SStephen Warren card->dapm_routes = routes; 4481a4a54dd5SStephen Warren 4482a4a54dd5SStephen Warren return 0; 4483a4a54dd5SStephen Warren } 4484a4a54dd5SStephen Warren EXPORT_SYMBOL_GPL(snd_soc_of_parse_audio_routing); 4485a4a54dd5SStephen Warren 4486a7930ed4SKuninori Morimoto unsigned int snd_soc_of_parse_daifmt(struct device_node *np, 4487a7930ed4SKuninori Morimoto const char *prefix) 4488a7930ed4SKuninori Morimoto { 4489a7930ed4SKuninori Morimoto int ret, i; 4490a7930ed4SKuninori Morimoto char prop[128]; 4491a7930ed4SKuninori Morimoto unsigned int format = 0; 4492a7930ed4SKuninori Morimoto int bit, frame; 4493a7930ed4SKuninori Morimoto const char *str; 4494a7930ed4SKuninori Morimoto struct { 4495a7930ed4SKuninori Morimoto char *name; 4496a7930ed4SKuninori Morimoto unsigned int val; 4497a7930ed4SKuninori Morimoto } of_fmt_table[] = { 4498a7930ed4SKuninori Morimoto { "i2s", SND_SOC_DAIFMT_I2S }, 4499a7930ed4SKuninori Morimoto { "right_j", SND_SOC_DAIFMT_RIGHT_J }, 4500a7930ed4SKuninori Morimoto { "left_j", SND_SOC_DAIFMT_LEFT_J }, 4501a7930ed4SKuninori Morimoto { "dsp_a", SND_SOC_DAIFMT_DSP_A }, 4502a7930ed4SKuninori Morimoto { "dsp_b", SND_SOC_DAIFMT_DSP_B }, 4503a7930ed4SKuninori Morimoto { "ac97", SND_SOC_DAIFMT_AC97 }, 4504a7930ed4SKuninori Morimoto { "pdm", SND_SOC_DAIFMT_PDM}, 4505a7930ed4SKuninori Morimoto { "msb", SND_SOC_DAIFMT_MSB }, 4506a7930ed4SKuninori Morimoto { "lsb", SND_SOC_DAIFMT_LSB }, 4507a7930ed4SKuninori Morimoto }; 4508a7930ed4SKuninori Morimoto 4509a7930ed4SKuninori Morimoto if (!prefix) 4510a7930ed4SKuninori Morimoto prefix = ""; 4511a7930ed4SKuninori Morimoto 4512a7930ed4SKuninori Morimoto /* 4513a7930ed4SKuninori Morimoto * check "[prefix]format = xxx" 4514a7930ed4SKuninori Morimoto * SND_SOC_DAIFMT_FORMAT_MASK area 4515a7930ed4SKuninori Morimoto */ 4516a7930ed4SKuninori Morimoto snprintf(prop, sizeof(prop), "%sformat", prefix); 4517a7930ed4SKuninori Morimoto ret = of_property_read_string(np, prop, &str); 4518a7930ed4SKuninori Morimoto if (ret == 0) { 4519a7930ed4SKuninori Morimoto for (i = 0; i < ARRAY_SIZE(of_fmt_table); i++) { 4520a7930ed4SKuninori Morimoto if (strcmp(str, of_fmt_table[i].name) == 0) { 4521a7930ed4SKuninori Morimoto format |= of_fmt_table[i].val; 4522a7930ed4SKuninori Morimoto break; 4523a7930ed4SKuninori Morimoto } 4524a7930ed4SKuninori Morimoto } 4525a7930ed4SKuninori Morimoto } 4526a7930ed4SKuninori Morimoto 4527a7930ed4SKuninori Morimoto /* 45288c2d6a9fSKuninori Morimoto * check "[prefix]continuous-clock" 4529a7930ed4SKuninori Morimoto * SND_SOC_DAIFMT_CLOCK_MASK area 4530a7930ed4SKuninori Morimoto */ 45318c2d6a9fSKuninori Morimoto snprintf(prop, sizeof(prop), "%scontinuous-clock", prefix); 45328c2d6a9fSKuninori Morimoto if (of_get_property(np, prop, NULL)) 45338c2d6a9fSKuninori Morimoto format |= SND_SOC_DAIFMT_CONT; 45348c2d6a9fSKuninori Morimoto else 45358c2d6a9fSKuninori Morimoto format |= SND_SOC_DAIFMT_GATED; 4536a7930ed4SKuninori Morimoto 4537a7930ed4SKuninori Morimoto /* 4538a7930ed4SKuninori Morimoto * check "[prefix]bitclock-inversion" 4539a7930ed4SKuninori Morimoto * check "[prefix]frame-inversion" 4540a7930ed4SKuninori Morimoto * SND_SOC_DAIFMT_INV_MASK area 4541a7930ed4SKuninori Morimoto */ 4542a7930ed4SKuninori Morimoto snprintf(prop, sizeof(prop), "%sbitclock-inversion", prefix); 4543a7930ed4SKuninori Morimoto bit = !!of_get_property(np, prop, NULL); 4544a7930ed4SKuninori Morimoto 4545a7930ed4SKuninori Morimoto snprintf(prop, sizeof(prop), "%sframe-inversion", prefix); 4546a7930ed4SKuninori Morimoto frame = !!of_get_property(np, prop, NULL); 4547a7930ed4SKuninori Morimoto 4548a7930ed4SKuninori Morimoto switch ((bit << 4) + frame) { 4549a7930ed4SKuninori Morimoto case 0x11: 4550a7930ed4SKuninori Morimoto format |= SND_SOC_DAIFMT_IB_IF; 4551a7930ed4SKuninori Morimoto break; 4552a7930ed4SKuninori Morimoto case 0x10: 4553a7930ed4SKuninori Morimoto format |= SND_SOC_DAIFMT_IB_NF; 4554a7930ed4SKuninori Morimoto break; 4555a7930ed4SKuninori Morimoto case 0x01: 4556a7930ed4SKuninori Morimoto format |= SND_SOC_DAIFMT_NB_IF; 4557a7930ed4SKuninori Morimoto break; 4558a7930ed4SKuninori Morimoto default: 4559a7930ed4SKuninori Morimoto /* SND_SOC_DAIFMT_NB_NF is default */ 4560a7930ed4SKuninori Morimoto break; 4561a7930ed4SKuninori Morimoto } 4562a7930ed4SKuninori Morimoto 4563a7930ed4SKuninori Morimoto /* 4564a7930ed4SKuninori Morimoto * check "[prefix]bitclock-master" 4565a7930ed4SKuninori Morimoto * check "[prefix]frame-master" 4566a7930ed4SKuninori Morimoto * SND_SOC_DAIFMT_MASTER_MASK area 4567a7930ed4SKuninori Morimoto */ 4568a7930ed4SKuninori Morimoto snprintf(prop, sizeof(prop), "%sbitclock-master", prefix); 4569a7930ed4SKuninori Morimoto bit = !!of_get_property(np, prop, NULL); 4570a7930ed4SKuninori Morimoto 4571a7930ed4SKuninori Morimoto snprintf(prop, sizeof(prop), "%sframe-master", prefix); 4572a7930ed4SKuninori Morimoto frame = !!of_get_property(np, prop, NULL); 4573a7930ed4SKuninori Morimoto 4574a7930ed4SKuninori Morimoto switch ((bit << 4) + frame) { 4575a7930ed4SKuninori Morimoto case 0x11: 4576a7930ed4SKuninori Morimoto format |= SND_SOC_DAIFMT_CBM_CFM; 4577a7930ed4SKuninori Morimoto break; 4578a7930ed4SKuninori Morimoto case 0x10: 4579a7930ed4SKuninori Morimoto format |= SND_SOC_DAIFMT_CBM_CFS; 4580a7930ed4SKuninori Morimoto break; 4581a7930ed4SKuninori Morimoto case 0x01: 4582a7930ed4SKuninori Morimoto format |= SND_SOC_DAIFMT_CBS_CFM; 4583a7930ed4SKuninori Morimoto break; 4584a7930ed4SKuninori Morimoto default: 4585a7930ed4SKuninori Morimoto format |= SND_SOC_DAIFMT_CBS_CFS; 4586a7930ed4SKuninori Morimoto break; 4587a7930ed4SKuninori Morimoto } 4588a7930ed4SKuninori Morimoto 4589a7930ed4SKuninori Morimoto return format; 4590a7930ed4SKuninori Morimoto } 4591a7930ed4SKuninori Morimoto EXPORT_SYMBOL_GPL(snd_soc_of_parse_daifmt); 4592a7930ed4SKuninori Morimoto 4593*cb470087SKuninori Morimoto int snd_soc_of_get_dai_name(struct device_node *of_node, 4594*cb470087SKuninori Morimoto const char **dai_name) 4595*cb470087SKuninori Morimoto { 4596*cb470087SKuninori Morimoto struct snd_soc_component *pos; 4597*cb470087SKuninori Morimoto struct of_phandle_args args; 4598*cb470087SKuninori Morimoto int ret; 4599*cb470087SKuninori Morimoto 4600*cb470087SKuninori Morimoto ret = of_parse_phandle_with_args(of_node, "sound-dai", 4601*cb470087SKuninori Morimoto "#sound-dai-cells", 0, &args); 4602*cb470087SKuninori Morimoto if (ret) 4603*cb470087SKuninori Morimoto return ret; 4604*cb470087SKuninori Morimoto 4605*cb470087SKuninori Morimoto ret = -EPROBE_DEFER; 4606*cb470087SKuninori Morimoto 4607*cb470087SKuninori Morimoto mutex_lock(&client_mutex); 4608*cb470087SKuninori Morimoto list_for_each_entry(pos, &component_list, list) { 4609*cb470087SKuninori Morimoto if (pos->dev->of_node != args.np) 4610*cb470087SKuninori Morimoto continue; 4611*cb470087SKuninori Morimoto 4612*cb470087SKuninori Morimoto if (!pos->driver->of_xlate_dai_name) { 4613*cb470087SKuninori Morimoto ret = -ENOSYS; 4614*cb470087SKuninori Morimoto break; 4615*cb470087SKuninori Morimoto } 4616*cb470087SKuninori Morimoto 4617*cb470087SKuninori Morimoto ret = pos->driver->of_xlate_dai_name(pos, &args, dai_name); 4618*cb470087SKuninori Morimoto break; 4619*cb470087SKuninori Morimoto } 4620*cb470087SKuninori Morimoto mutex_unlock(&client_mutex); 4621*cb470087SKuninori Morimoto 4622*cb470087SKuninori Morimoto of_node_put(args.np); 4623*cb470087SKuninori Morimoto 4624*cb470087SKuninori Morimoto return ret; 4625*cb470087SKuninori Morimoto } 4626*cb470087SKuninori Morimoto EXPORT_SYMBOL_GPL(snd_soc_of_get_dai_name); 4627*cb470087SKuninori Morimoto 4628c9b3a40fSTakashi Iwai static int __init snd_soc_init(void) 4629db2a4165SFrank Mandarino { 4630384c89e2SMark Brown #ifdef CONFIG_DEBUG_FS 46318a9dab1aSMark Brown snd_soc_debugfs_root = debugfs_create_dir("asoc", NULL); 46328a9dab1aSMark Brown if (IS_ERR(snd_soc_debugfs_root) || !snd_soc_debugfs_root) { 46330837fc62SFabio Estevam pr_warn("ASoC: Failed to create debugfs directory\n"); 46348a9dab1aSMark Brown snd_soc_debugfs_root = NULL; 4635384c89e2SMark Brown } 4636c3c5a19aSMark Brown 46378a9dab1aSMark Brown if (!debugfs_create_file("codecs", 0444, snd_soc_debugfs_root, NULL, 4638c3c5a19aSMark Brown &codec_list_fops)) 4639c3c5a19aSMark Brown pr_warn("ASoC: Failed to create CODEC list debugfs file\n"); 4640c3c5a19aSMark Brown 46418a9dab1aSMark Brown if (!debugfs_create_file("dais", 0444, snd_soc_debugfs_root, NULL, 4642f3208780SMark Brown &dai_list_fops)) 4643f3208780SMark Brown pr_warn("ASoC: Failed to create DAI list debugfs file\n"); 464419c7ac27SMark Brown 46458a9dab1aSMark Brown if (!debugfs_create_file("platforms", 0444, snd_soc_debugfs_root, NULL, 464619c7ac27SMark Brown &platform_list_fops)) 464719c7ac27SMark Brown pr_warn("ASoC: Failed to create platform list debugfs file\n"); 4648384c89e2SMark Brown #endif 4649384c89e2SMark Brown 4650fb257897SMark Brown snd_soc_util_init(); 4651fb257897SMark Brown 4652db2a4165SFrank Mandarino return platform_driver_register(&soc_driver); 4653db2a4165SFrank Mandarino } 46544abe8e16SMark Brown module_init(snd_soc_init); 4655db2a4165SFrank Mandarino 46567d8c16a6SMark Brown static void __exit snd_soc_exit(void) 4657db2a4165SFrank Mandarino { 4658fb257897SMark Brown snd_soc_util_exit(); 4659fb257897SMark Brown 4660384c89e2SMark Brown #ifdef CONFIG_DEBUG_FS 46618a9dab1aSMark Brown debugfs_remove_recursive(snd_soc_debugfs_root); 4662384c89e2SMark Brown #endif 4663db2a4165SFrank Mandarino platform_driver_unregister(&soc_driver); 4664db2a4165SFrank Mandarino } 4665db2a4165SFrank Mandarino module_exit(snd_soc_exit); 4666db2a4165SFrank Mandarino 4667db2a4165SFrank Mandarino /* Module information */ 4668d331124dSLiam Girdwood MODULE_AUTHOR("Liam Girdwood, lrg@slimlogic.co.uk"); 4669db2a4165SFrank Mandarino MODULE_DESCRIPTION("ALSA SoC Core"); 4670db2a4165SFrank Mandarino MODULE_LICENSE("GPL"); 46718b45a209SKay Sievers MODULE_ALIAS("platform:soc-audio"); 4672