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> 335a0e3ad6STejun Heo #include <linux/slab.h> 34474828a4SMarek Vasut #include <sound/ac97_codec.h> 35db2a4165SFrank Mandarino #include <sound/core.h> 36db2a4165SFrank Mandarino #include <sound/pcm.h> 37db2a4165SFrank Mandarino #include <sound/pcm_params.h> 38db2a4165SFrank Mandarino #include <sound/soc.h> 39db2a4165SFrank Mandarino #include <sound/initval.h> 40db2a4165SFrank Mandarino 41a8b1d34fSMark Brown #define CREATE_TRACE_POINTS 42a8b1d34fSMark Brown #include <trace/events/asoc.h> 43a8b1d34fSMark Brown 44f0fba2adSLiam Girdwood #define NAME_SIZE 32 45f0fba2adSLiam Girdwood 46db2a4165SFrank Mandarino static DEFINE_MUTEX(pcm_mutex); 47db2a4165SFrank Mandarino static DECLARE_WAIT_QUEUE_HEAD(soc_pm_waitq); 48db2a4165SFrank Mandarino 49384c89e2SMark Brown #ifdef CONFIG_DEBUG_FS 50384c89e2SMark Brown static struct dentry *debugfs_root; 51384c89e2SMark Brown #endif 52384c89e2SMark Brown 53c5af3a2eSMark Brown static DEFINE_MUTEX(client_mutex); 54c5af3a2eSMark Brown static LIST_HEAD(card_list); 559115171aSMark Brown static LIST_HEAD(dai_list); 5612a48a8cSMark Brown static LIST_HEAD(platform_list); 570d0cf00aSMark Brown static LIST_HEAD(codec_list); 58c5af3a2eSMark Brown 59c5af3a2eSMark Brown static int snd_soc_register_card(struct snd_soc_card *card); 60c5af3a2eSMark Brown static int snd_soc_unregister_card(struct snd_soc_card *card); 61f0fba2adSLiam Girdwood static int soc_new_pcm(struct snd_soc_pcm_runtime *rtd, int num); 62c5af3a2eSMark Brown 63db2a4165SFrank Mandarino /* 64db2a4165SFrank Mandarino * This is a timeout to do a DAPM powerdown after a stream is closed(). 65db2a4165SFrank Mandarino * It can be used to eliminate pops between different playback streams, e.g. 66db2a4165SFrank Mandarino * between two audio tracks. 67db2a4165SFrank Mandarino */ 68db2a4165SFrank Mandarino static int pmdown_time = 5000; 69db2a4165SFrank Mandarino module_param(pmdown_time, int, 0); 70db2a4165SFrank Mandarino MODULE_PARM_DESC(pmdown_time, "DAPM stream powerdown time (msecs)"); 71db2a4165SFrank Mandarino 72965ac42cSLiam Girdwood /* 73965ac42cSLiam Girdwood * This function forces any delayed work to be queued and run. 74965ac42cSLiam Girdwood */ 75965ac42cSLiam Girdwood static int run_delayed_work(struct delayed_work *dwork) 76965ac42cSLiam Girdwood { 77965ac42cSLiam Girdwood int ret; 78965ac42cSLiam Girdwood 79965ac42cSLiam Girdwood /* cancel any work waiting to be queued. */ 80965ac42cSLiam Girdwood ret = cancel_delayed_work(dwork); 81965ac42cSLiam Girdwood 82965ac42cSLiam Girdwood /* if there was any work waiting then we run it now and 83965ac42cSLiam Girdwood * wait for it's completion */ 84965ac42cSLiam Girdwood if (ret) { 85965ac42cSLiam Girdwood schedule_delayed_work(dwork, 0); 86965ac42cSLiam Girdwood flush_scheduled_work(); 87965ac42cSLiam Girdwood } 88965ac42cSLiam Girdwood return ret; 89965ac42cSLiam Girdwood } 90965ac42cSLiam Girdwood 912624d5faSMark Brown /* codec register dump */ 922624d5faSMark Brown static ssize_t soc_codec_reg_show(struct snd_soc_codec *codec, char *buf) 932624d5faSMark Brown { 945164d74dSMark Brown int ret, i, step = 1, count = 0; 952624d5faSMark Brown 96f0fba2adSLiam Girdwood if (!codec->driver->reg_cache_size) 972624d5faSMark Brown return 0; 982624d5faSMark Brown 99f0fba2adSLiam Girdwood if (codec->driver->reg_cache_step) 100f0fba2adSLiam Girdwood step = codec->driver->reg_cache_step; 1012624d5faSMark Brown 1022624d5faSMark Brown count += sprintf(buf, "%s registers\n", codec->name); 103f0fba2adSLiam Girdwood for (i = 0; i < codec->driver->reg_cache_size; i += step) { 104f0fba2adSLiam Girdwood if (codec->driver->readable_register && !codec->driver->readable_register(i)) 1052624d5faSMark Brown continue; 1062624d5faSMark Brown 1072624d5faSMark Brown count += sprintf(buf + count, "%2x: ", i); 1082624d5faSMark Brown if (count >= PAGE_SIZE - 1) 1092624d5faSMark Brown break; 1102624d5faSMark Brown 111f0fba2adSLiam Girdwood if (codec->driver->display_register) { 112f0fba2adSLiam Girdwood count += codec->driver->display_register(codec, buf + count, 1132624d5faSMark Brown PAGE_SIZE - count, i); 1145164d74dSMark Brown } else { 1155164d74dSMark Brown /* If the read fails it's almost certainly due to 1165164d74dSMark Brown * the register being volatile and the device being 1175164d74dSMark Brown * powered off. 1185164d74dSMark Brown */ 119f0fba2adSLiam Girdwood ret = codec->driver->read(codec, i); 1205164d74dSMark Brown if (ret >= 0) 1215164d74dSMark Brown count += snprintf(buf + count, 1225164d74dSMark Brown PAGE_SIZE - count, 1235164d74dSMark Brown "%4x", ret); 1242624d5faSMark Brown else 1255164d74dSMark Brown count += snprintf(buf + count, 1265164d74dSMark Brown PAGE_SIZE - count, 1275164d74dSMark Brown "<no data: %d>", ret); 1285164d74dSMark Brown } 1292624d5faSMark Brown 1302624d5faSMark Brown if (count >= PAGE_SIZE - 1) 1312624d5faSMark Brown break; 1322624d5faSMark Brown 1332624d5faSMark Brown count += snprintf(buf + count, PAGE_SIZE - count, "\n"); 1342624d5faSMark Brown if (count >= PAGE_SIZE - 1) 1352624d5faSMark Brown break; 1362624d5faSMark Brown } 1372624d5faSMark Brown 1382624d5faSMark Brown /* Truncate count; min() would cause a warning */ 1392624d5faSMark Brown if (count >= PAGE_SIZE) 1402624d5faSMark Brown count = PAGE_SIZE - 1; 1412624d5faSMark Brown 1422624d5faSMark Brown return count; 1432624d5faSMark Brown } 1442624d5faSMark Brown static ssize_t codec_reg_show(struct device *dev, 1452624d5faSMark Brown struct device_attribute *attr, char *buf) 1462624d5faSMark Brown { 147f0fba2adSLiam Girdwood struct snd_soc_pcm_runtime *rtd = 148f0fba2adSLiam Girdwood container_of(dev, struct snd_soc_pcm_runtime, dev); 149f0fba2adSLiam Girdwood 150f0fba2adSLiam Girdwood return soc_codec_reg_show(rtd->codec, buf); 1512624d5faSMark Brown } 1522624d5faSMark Brown 1532624d5faSMark Brown static DEVICE_ATTR(codec_reg, 0444, codec_reg_show, NULL); 1542624d5faSMark Brown 155dbe21408SMark Brown static ssize_t pmdown_time_show(struct device *dev, 156dbe21408SMark Brown struct device_attribute *attr, char *buf) 157dbe21408SMark Brown { 158f0fba2adSLiam Girdwood struct snd_soc_pcm_runtime *rtd = 159f0fba2adSLiam Girdwood container_of(dev, struct snd_soc_pcm_runtime, dev); 160dbe21408SMark Brown 161f0fba2adSLiam Girdwood return sprintf(buf, "%ld\n", rtd->pmdown_time); 162dbe21408SMark Brown } 163dbe21408SMark Brown 164dbe21408SMark Brown static ssize_t pmdown_time_set(struct device *dev, 165dbe21408SMark Brown struct device_attribute *attr, 166dbe21408SMark Brown const char *buf, size_t count) 167dbe21408SMark Brown { 168f0fba2adSLiam Girdwood struct snd_soc_pcm_runtime *rtd = 169f0fba2adSLiam Girdwood container_of(dev, struct snd_soc_pcm_runtime, dev); 170c593b520SMark Brown int ret; 171dbe21408SMark Brown 172c593b520SMark Brown ret = strict_strtol(buf, 10, &rtd->pmdown_time); 173c593b520SMark Brown if (ret) 174c593b520SMark Brown return ret; 175dbe21408SMark Brown 176dbe21408SMark Brown return count; 177dbe21408SMark Brown } 178dbe21408SMark Brown 179dbe21408SMark Brown static DEVICE_ATTR(pmdown_time, 0644, pmdown_time_show, pmdown_time_set); 180dbe21408SMark Brown 1812624d5faSMark Brown #ifdef CONFIG_DEBUG_FS 1822624d5faSMark Brown static int codec_reg_open_file(struct inode *inode, struct file *file) 1832624d5faSMark Brown { 1842624d5faSMark Brown file->private_data = inode->i_private; 1852624d5faSMark Brown return 0; 1862624d5faSMark Brown } 1872624d5faSMark Brown 1882624d5faSMark Brown static ssize_t codec_reg_read_file(struct file *file, char __user *user_buf, 1892624d5faSMark Brown size_t count, loff_t *ppos) 1902624d5faSMark Brown { 1912624d5faSMark Brown ssize_t ret; 1922624d5faSMark Brown struct snd_soc_codec *codec = file->private_data; 1932624d5faSMark Brown char *buf = kmalloc(PAGE_SIZE, GFP_KERNEL); 1942624d5faSMark Brown if (!buf) 1952624d5faSMark Brown return -ENOMEM; 1962624d5faSMark Brown ret = soc_codec_reg_show(codec, buf); 1972624d5faSMark Brown if (ret >= 0) 1982624d5faSMark Brown ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret); 1992624d5faSMark Brown kfree(buf); 2002624d5faSMark Brown return ret; 2012624d5faSMark Brown } 2022624d5faSMark Brown 2032624d5faSMark Brown static ssize_t codec_reg_write_file(struct file *file, 2042624d5faSMark Brown const char __user *user_buf, size_t count, loff_t *ppos) 2052624d5faSMark Brown { 2062624d5faSMark Brown char buf[32]; 2072624d5faSMark Brown int buf_size; 2082624d5faSMark Brown char *start = buf; 2092624d5faSMark Brown unsigned long reg, value; 2102624d5faSMark Brown int step = 1; 2112624d5faSMark Brown struct snd_soc_codec *codec = file->private_data; 2122624d5faSMark Brown 2132624d5faSMark Brown buf_size = min(count, (sizeof(buf)-1)); 2142624d5faSMark Brown if (copy_from_user(buf, user_buf, buf_size)) 2152624d5faSMark Brown return -EFAULT; 2162624d5faSMark Brown buf[buf_size] = 0; 2172624d5faSMark Brown 218f0fba2adSLiam Girdwood if (codec->driver->reg_cache_step) 219f0fba2adSLiam Girdwood step = codec->driver->reg_cache_step; 2202624d5faSMark Brown 2212624d5faSMark Brown while (*start == ' ') 2222624d5faSMark Brown start++; 2232624d5faSMark Brown reg = simple_strtoul(start, &start, 16); 224f0fba2adSLiam Girdwood if ((reg >= codec->driver->reg_cache_size) || (reg % step)) 2252624d5faSMark Brown return -EINVAL; 2262624d5faSMark Brown while (*start == ' ') 2272624d5faSMark Brown start++; 2282624d5faSMark Brown if (strict_strtoul(start, 16, &value)) 2292624d5faSMark Brown return -EINVAL; 230f0fba2adSLiam Girdwood codec->driver->write(codec, reg, value); 2312624d5faSMark Brown return buf_size; 2322624d5faSMark Brown } 2332624d5faSMark Brown 2342624d5faSMark Brown static const struct file_operations codec_reg_fops = { 2352624d5faSMark Brown .open = codec_reg_open_file, 2362624d5faSMark Brown .read = codec_reg_read_file, 2372624d5faSMark Brown .write = codec_reg_write_file, 2386038f373SArnd Bergmann .llseek = default_llseek, 2392624d5faSMark Brown }; 2402624d5faSMark Brown 2412624d5faSMark Brown static void soc_init_codec_debugfs(struct snd_soc_codec *codec) 2422624d5faSMark Brown { 243d6ce4cf3SJarkko Nikula struct dentry *debugfs_card_root = codec->card->debugfs_card_root; 244d6ce4cf3SJarkko Nikula 2456ba6c9c3SMark Brown codec->debugfs_codec_root = debugfs_create_dir(codec->name, 246d6ce4cf3SJarkko Nikula debugfs_card_root); 2472624d5faSMark Brown if (!codec->debugfs_codec_root) { 2482624d5faSMark Brown printk(KERN_WARNING 2492624d5faSMark Brown "ASoC: Failed to create codec debugfs directory\n"); 2502624d5faSMark Brown return; 2512624d5faSMark Brown } 2522624d5faSMark Brown 2532624d5faSMark Brown codec->debugfs_reg = debugfs_create_file("codec_reg", 0644, 2542624d5faSMark Brown codec->debugfs_codec_root, 2552624d5faSMark Brown codec, &codec_reg_fops); 2562624d5faSMark Brown if (!codec->debugfs_reg) 2572624d5faSMark Brown printk(KERN_WARNING 2582624d5faSMark Brown "ASoC: Failed to create codec register debugfs file\n"); 2592624d5faSMark Brown 260ce6120ccSLiam Girdwood codec->dapm.debugfs_dapm = debugfs_create_dir("dapm", 2612624d5faSMark Brown codec->debugfs_codec_root); 262ce6120ccSLiam Girdwood if (!codec->dapm.debugfs_dapm) 2632624d5faSMark Brown printk(KERN_WARNING 2642624d5faSMark Brown "Failed to create DAPM debugfs directory\n"); 2652624d5faSMark Brown 266ce6120ccSLiam Girdwood snd_soc_dapm_debugfs_init(&codec->dapm); 2672624d5faSMark Brown } 2682624d5faSMark Brown 2692624d5faSMark Brown static void soc_cleanup_codec_debugfs(struct snd_soc_codec *codec) 2702624d5faSMark Brown { 2712624d5faSMark Brown debugfs_remove_recursive(codec->debugfs_codec_root); 2722624d5faSMark Brown } 2732624d5faSMark Brown 274c3c5a19aSMark Brown static ssize_t codec_list_read_file(struct file *file, char __user *user_buf, 275c3c5a19aSMark Brown size_t count, loff_t *ppos) 276c3c5a19aSMark Brown { 277c3c5a19aSMark Brown char *buf = kmalloc(PAGE_SIZE, GFP_KERNEL); 2782b194f9dSMark Brown ssize_t len, ret = 0; 279c3c5a19aSMark Brown struct snd_soc_codec *codec; 280c3c5a19aSMark Brown 281c3c5a19aSMark Brown if (!buf) 282c3c5a19aSMark Brown return -ENOMEM; 283c3c5a19aSMark Brown 2842b194f9dSMark Brown list_for_each_entry(codec, &codec_list, list) { 2852b194f9dSMark Brown len = snprintf(buf + ret, PAGE_SIZE - ret, "%s\n", 286c3c5a19aSMark Brown codec->name); 2872b194f9dSMark Brown if (len >= 0) 2882b194f9dSMark Brown ret += len; 2892b194f9dSMark Brown if (ret > PAGE_SIZE) { 2902b194f9dSMark Brown ret = PAGE_SIZE; 2912b194f9dSMark Brown break; 2922b194f9dSMark Brown } 2932b194f9dSMark Brown } 294c3c5a19aSMark Brown 295c3c5a19aSMark Brown if (ret >= 0) 296c3c5a19aSMark Brown ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret); 297c3c5a19aSMark Brown 298c3c5a19aSMark Brown kfree(buf); 299c3c5a19aSMark Brown 300c3c5a19aSMark Brown return ret; 301c3c5a19aSMark Brown } 302c3c5a19aSMark Brown 303c3c5a19aSMark Brown static const struct file_operations codec_list_fops = { 304c3c5a19aSMark Brown .read = codec_list_read_file, 305c3c5a19aSMark Brown .llseek = default_llseek,/* read accesses f_pos */ 306c3c5a19aSMark Brown }; 307c3c5a19aSMark Brown 308f3208780SMark Brown static ssize_t dai_list_read_file(struct file *file, char __user *user_buf, 309f3208780SMark Brown size_t count, loff_t *ppos) 310f3208780SMark Brown { 311f3208780SMark Brown char *buf = kmalloc(PAGE_SIZE, GFP_KERNEL); 3122b194f9dSMark Brown ssize_t len, ret = 0; 313f3208780SMark Brown struct snd_soc_dai *dai; 314f3208780SMark Brown 315f3208780SMark Brown if (!buf) 316f3208780SMark Brown return -ENOMEM; 317f3208780SMark Brown 3182b194f9dSMark Brown list_for_each_entry(dai, &dai_list, list) { 3192b194f9dSMark Brown len = snprintf(buf + ret, PAGE_SIZE - ret, "%s\n", dai->name); 3202b194f9dSMark Brown if (len >= 0) 3212b194f9dSMark Brown ret += len; 3222b194f9dSMark Brown if (ret > PAGE_SIZE) { 3232b194f9dSMark Brown ret = PAGE_SIZE; 3242b194f9dSMark Brown break; 3252b194f9dSMark Brown } 3262b194f9dSMark Brown } 327f3208780SMark Brown 328f3208780SMark Brown ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret); 329f3208780SMark Brown 330f3208780SMark Brown kfree(buf); 331f3208780SMark Brown 332f3208780SMark Brown return ret; 333f3208780SMark Brown } 334f3208780SMark Brown 335f3208780SMark Brown static const struct file_operations dai_list_fops = { 336f3208780SMark Brown .read = dai_list_read_file, 337f3208780SMark Brown .llseek = default_llseek,/* read accesses f_pos */ 338f3208780SMark Brown }; 339f3208780SMark Brown 34019c7ac27SMark Brown static ssize_t platform_list_read_file(struct file *file, 34119c7ac27SMark Brown char __user *user_buf, 34219c7ac27SMark Brown size_t count, loff_t *ppos) 34319c7ac27SMark Brown { 34419c7ac27SMark Brown char *buf = kmalloc(PAGE_SIZE, GFP_KERNEL); 3452b194f9dSMark Brown ssize_t len, ret = 0; 34619c7ac27SMark Brown struct snd_soc_platform *platform; 34719c7ac27SMark Brown 34819c7ac27SMark Brown if (!buf) 34919c7ac27SMark Brown return -ENOMEM; 35019c7ac27SMark Brown 3512b194f9dSMark Brown list_for_each_entry(platform, &platform_list, list) { 3522b194f9dSMark Brown len = snprintf(buf + ret, PAGE_SIZE - ret, "%s\n", 35319c7ac27SMark Brown platform->name); 3542b194f9dSMark Brown if (len >= 0) 3552b194f9dSMark Brown ret += len; 3562b194f9dSMark Brown if (ret > PAGE_SIZE) { 3572b194f9dSMark Brown ret = PAGE_SIZE; 3582b194f9dSMark Brown break; 3592b194f9dSMark Brown } 3602b194f9dSMark Brown } 36119c7ac27SMark Brown 36219c7ac27SMark Brown ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret); 36319c7ac27SMark Brown 36419c7ac27SMark Brown kfree(buf); 36519c7ac27SMark Brown 36619c7ac27SMark Brown return ret; 36719c7ac27SMark Brown } 36819c7ac27SMark Brown 36919c7ac27SMark Brown static const struct file_operations platform_list_fops = { 37019c7ac27SMark Brown .read = platform_list_read_file, 37119c7ac27SMark Brown .llseek = default_llseek,/* read accesses f_pos */ 37219c7ac27SMark Brown }; 37319c7ac27SMark Brown 374a6052154SJarkko Nikula static void soc_init_card_debugfs(struct snd_soc_card *card) 375a6052154SJarkko Nikula { 376a6052154SJarkko Nikula card->debugfs_card_root = debugfs_create_dir(card->name, 377a6052154SJarkko Nikula debugfs_root); 3783a45b867SJarkko Nikula if (!card->debugfs_card_root) { 379a6052154SJarkko Nikula dev_warn(card->dev, 380a6052154SJarkko Nikula "ASoC: Failed to create codec debugfs directory\n"); 3813a45b867SJarkko Nikula return; 3823a45b867SJarkko Nikula } 3833a45b867SJarkko Nikula 3843a45b867SJarkko Nikula card->debugfs_pop_time = debugfs_create_u32("dapm_pop_time", 0644, 3853a45b867SJarkko Nikula card->debugfs_card_root, 3863a45b867SJarkko Nikula &card->pop_time); 3873a45b867SJarkko Nikula if (!card->debugfs_pop_time) 3883a45b867SJarkko Nikula dev_warn(card->dev, 3893a45b867SJarkko Nikula "Failed to create pop time debugfs file\n"); 390a6052154SJarkko Nikula } 391a6052154SJarkko Nikula 392a6052154SJarkko Nikula static void soc_cleanup_card_debugfs(struct snd_soc_card *card) 393a6052154SJarkko Nikula { 394a6052154SJarkko Nikula debugfs_remove_recursive(card->debugfs_card_root); 395a6052154SJarkko Nikula } 396a6052154SJarkko Nikula 3972624d5faSMark Brown #else 3982624d5faSMark Brown 3992624d5faSMark Brown static inline void soc_init_codec_debugfs(struct snd_soc_codec *codec) 4002624d5faSMark Brown { 4012624d5faSMark Brown } 4022624d5faSMark Brown 4032624d5faSMark Brown static inline void soc_cleanup_codec_debugfs(struct snd_soc_codec *codec) 4042624d5faSMark Brown { 4052624d5faSMark Brown } 406b95fccbcSAxel Lin 407b95fccbcSAxel Lin static inline void soc_init_card_debugfs(struct snd_soc_card *card) 408b95fccbcSAxel Lin { 409b95fccbcSAxel Lin } 410b95fccbcSAxel Lin 411b95fccbcSAxel Lin static inline void soc_cleanup_card_debugfs(struct snd_soc_card *card) 412b95fccbcSAxel Lin { 413b95fccbcSAxel Lin } 4142624d5faSMark Brown #endif 4152624d5faSMark Brown 416db2a4165SFrank Mandarino #ifdef CONFIG_SND_SOC_AC97_BUS 417db2a4165SFrank Mandarino /* unregister ac97 codec */ 418db2a4165SFrank Mandarino static int soc_ac97_dev_unregister(struct snd_soc_codec *codec) 419db2a4165SFrank Mandarino { 420db2a4165SFrank Mandarino if (codec->ac97->dev.bus) 421db2a4165SFrank Mandarino device_unregister(&codec->ac97->dev); 422db2a4165SFrank Mandarino return 0; 423db2a4165SFrank Mandarino } 424db2a4165SFrank Mandarino 425db2a4165SFrank Mandarino /* stop no dev release warning */ 426db2a4165SFrank Mandarino static void soc_ac97_device_release(struct device *dev){} 427db2a4165SFrank Mandarino 428db2a4165SFrank Mandarino /* register ac97 codec to bus */ 429db2a4165SFrank Mandarino static int soc_ac97_dev_register(struct snd_soc_codec *codec) 430db2a4165SFrank Mandarino { 431db2a4165SFrank Mandarino int err; 432db2a4165SFrank Mandarino 433db2a4165SFrank Mandarino codec->ac97->dev.bus = &ac97_bus_type; 4344ac5c61fSMark Brown codec->ac97->dev.parent = codec->card->dev; 435db2a4165SFrank Mandarino codec->ac97->dev.release = soc_ac97_device_release; 436db2a4165SFrank Mandarino 437bb072bf0SKay Sievers dev_set_name(&codec->ac97->dev, "%d-%d:%s", 438f0fba2adSLiam Girdwood codec->card->snd_card->number, 0, codec->name); 439db2a4165SFrank Mandarino err = device_register(&codec->ac97->dev); 440db2a4165SFrank Mandarino if (err < 0) { 441db2a4165SFrank Mandarino snd_printk(KERN_ERR "Can't register ac97 bus\n"); 442db2a4165SFrank Mandarino codec->ac97->dev.bus = NULL; 443db2a4165SFrank Mandarino return err; 444db2a4165SFrank Mandarino } 445db2a4165SFrank Mandarino return 0; 446db2a4165SFrank Mandarino } 447db2a4165SFrank Mandarino #endif 448db2a4165SFrank Mandarino 44906f409d7SMark Brown static int soc_pcm_apply_symmetry(struct snd_pcm_substream *substream) 45006f409d7SMark Brown { 45106f409d7SMark Brown struct snd_soc_pcm_runtime *rtd = substream->private_data; 452f0fba2adSLiam Girdwood struct snd_soc_dai *cpu_dai = rtd->cpu_dai; 453f0fba2adSLiam Girdwood struct snd_soc_dai *codec_dai = rtd->codec_dai; 45406f409d7SMark Brown int ret; 45506f409d7SMark Brown 456f0fba2adSLiam Girdwood if (codec_dai->driver->symmetric_rates || cpu_dai->driver->symmetric_rates || 457f0fba2adSLiam Girdwood rtd->dai_link->symmetric_rates) { 458f0fba2adSLiam Girdwood dev_dbg(&rtd->dev, "Symmetry forces %dHz rate\n", 459f0fba2adSLiam Girdwood rtd->rate); 46006f409d7SMark Brown 46106f409d7SMark Brown ret = snd_pcm_hw_constraint_minmax(substream->runtime, 46206f409d7SMark Brown SNDRV_PCM_HW_PARAM_RATE, 463f0fba2adSLiam Girdwood rtd->rate, 464f0fba2adSLiam Girdwood rtd->rate); 46506f409d7SMark Brown if (ret < 0) { 466f0fba2adSLiam Girdwood dev_err(&rtd->dev, 46706f409d7SMark Brown "Unable to apply rate symmetry constraint: %d\n", ret); 46806f409d7SMark Brown return ret; 46906f409d7SMark Brown } 47006f409d7SMark Brown } 47106f409d7SMark Brown 47206f409d7SMark Brown return 0; 47306f409d7SMark Brown } 47406f409d7SMark Brown 475db2a4165SFrank Mandarino /* 476db2a4165SFrank Mandarino * Called by ALSA when a PCM substream is opened, the runtime->hw record is 477db2a4165SFrank Mandarino * then initialized and any private data can be allocated. This also calls 478db2a4165SFrank Mandarino * startup for the cpu DAI, platform, machine and codec DAI. 479db2a4165SFrank Mandarino */ 480db2a4165SFrank Mandarino static int soc_pcm_open(struct snd_pcm_substream *substream) 481db2a4165SFrank Mandarino { 482db2a4165SFrank Mandarino struct snd_soc_pcm_runtime *rtd = substream->private_data; 483db2a4165SFrank Mandarino struct snd_pcm_runtime *runtime = substream->runtime; 484f0fba2adSLiam Girdwood struct snd_soc_platform *platform = rtd->platform; 485f0fba2adSLiam Girdwood struct snd_soc_dai *cpu_dai = rtd->cpu_dai; 486f0fba2adSLiam Girdwood struct snd_soc_dai *codec_dai = rtd->codec_dai; 487f0fba2adSLiam Girdwood struct snd_soc_dai_driver *cpu_dai_drv = cpu_dai->driver; 488f0fba2adSLiam Girdwood struct snd_soc_dai_driver *codec_dai_drv = codec_dai->driver; 489db2a4165SFrank Mandarino int ret = 0; 490db2a4165SFrank Mandarino 491db2a4165SFrank Mandarino mutex_lock(&pcm_mutex); 492db2a4165SFrank Mandarino 493db2a4165SFrank Mandarino /* startup the audio subsystem */ 494f0fba2adSLiam Girdwood if (cpu_dai->driver->ops->startup) { 495f0fba2adSLiam Girdwood ret = cpu_dai->driver->ops->startup(substream, cpu_dai); 496db2a4165SFrank Mandarino if (ret < 0) { 497db2a4165SFrank Mandarino printk(KERN_ERR "asoc: can't open interface %s\n", 498cb666e5bSLiam Girdwood cpu_dai->name); 499db2a4165SFrank Mandarino goto out; 500db2a4165SFrank Mandarino } 501db2a4165SFrank Mandarino } 502db2a4165SFrank Mandarino 503f0fba2adSLiam Girdwood if (platform->driver->ops->open) { 504f0fba2adSLiam Girdwood ret = platform->driver->ops->open(substream); 505db2a4165SFrank Mandarino if (ret < 0) { 506db2a4165SFrank Mandarino printk(KERN_ERR "asoc: can't open platform %s\n", platform->name); 507db2a4165SFrank Mandarino goto platform_err; 508db2a4165SFrank Mandarino } 509db2a4165SFrank Mandarino } 510db2a4165SFrank Mandarino 511f0fba2adSLiam Girdwood if (codec_dai->driver->ops->startup) { 512f0fba2adSLiam Girdwood ret = codec_dai->driver->ops->startup(substream, codec_dai); 513cb666e5bSLiam Girdwood if (ret < 0) { 514cb666e5bSLiam Girdwood printk(KERN_ERR "asoc: can't open codec %s\n", 515cb666e5bSLiam Girdwood codec_dai->name); 516cb666e5bSLiam Girdwood goto codec_dai_err; 517cb666e5bSLiam Girdwood } 518cb666e5bSLiam Girdwood } 519cb666e5bSLiam Girdwood 520f0fba2adSLiam Girdwood if (rtd->dai_link->ops && rtd->dai_link->ops->startup) { 521f0fba2adSLiam Girdwood ret = rtd->dai_link->ops->startup(substream); 522db2a4165SFrank Mandarino if (ret < 0) { 523f0fba2adSLiam Girdwood printk(KERN_ERR "asoc: %s startup failed\n", rtd->dai_link->name); 524db2a4165SFrank Mandarino goto machine_err; 525db2a4165SFrank Mandarino } 526db2a4165SFrank Mandarino } 527db2a4165SFrank Mandarino 528a00f90f9SMark Brown /* Check that the codec and cpu DAIs are compatible */ 529db2a4165SFrank Mandarino if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) { 530db2a4165SFrank Mandarino runtime->hw.rate_min = 531f0fba2adSLiam Girdwood max(codec_dai_drv->playback.rate_min, 532f0fba2adSLiam Girdwood cpu_dai_drv->playback.rate_min); 533db2a4165SFrank Mandarino runtime->hw.rate_max = 534f0fba2adSLiam Girdwood min(codec_dai_drv->playback.rate_max, 535f0fba2adSLiam Girdwood cpu_dai_drv->playback.rate_max); 536db2a4165SFrank Mandarino runtime->hw.channels_min = 537f0fba2adSLiam Girdwood max(codec_dai_drv->playback.channels_min, 538f0fba2adSLiam Girdwood cpu_dai_drv->playback.channels_min); 539db2a4165SFrank Mandarino runtime->hw.channels_max = 540f0fba2adSLiam Girdwood min(codec_dai_drv->playback.channels_max, 541f0fba2adSLiam Girdwood cpu_dai_drv->playback.channels_max); 542cb666e5bSLiam Girdwood runtime->hw.formats = 543f0fba2adSLiam Girdwood codec_dai_drv->playback.formats & cpu_dai_drv->playback.formats; 544cb666e5bSLiam Girdwood runtime->hw.rates = 545f0fba2adSLiam Girdwood codec_dai_drv->playback.rates & cpu_dai_drv->playback.rates; 546f0fba2adSLiam Girdwood if (codec_dai_drv->playback.rates 547d9ad6296SJassi Brar & (SNDRV_PCM_RATE_KNOT | SNDRV_PCM_RATE_CONTINUOUS)) 548f0fba2adSLiam Girdwood runtime->hw.rates |= cpu_dai_drv->playback.rates; 549f0fba2adSLiam Girdwood if (cpu_dai_drv->playback.rates 550d9ad6296SJassi Brar & (SNDRV_PCM_RATE_KNOT | SNDRV_PCM_RATE_CONTINUOUS)) 551f0fba2adSLiam Girdwood runtime->hw.rates |= codec_dai_drv->playback.rates; 552db2a4165SFrank Mandarino } else { 553db2a4165SFrank Mandarino runtime->hw.rate_min = 554f0fba2adSLiam Girdwood max(codec_dai_drv->capture.rate_min, 555f0fba2adSLiam Girdwood cpu_dai_drv->capture.rate_min); 556db2a4165SFrank Mandarino runtime->hw.rate_max = 557f0fba2adSLiam Girdwood min(codec_dai_drv->capture.rate_max, 558f0fba2adSLiam Girdwood cpu_dai_drv->capture.rate_max); 559db2a4165SFrank Mandarino runtime->hw.channels_min = 560f0fba2adSLiam Girdwood max(codec_dai_drv->capture.channels_min, 561f0fba2adSLiam Girdwood cpu_dai_drv->capture.channels_min); 562db2a4165SFrank Mandarino runtime->hw.channels_max = 563f0fba2adSLiam Girdwood min(codec_dai_drv->capture.channels_max, 564f0fba2adSLiam Girdwood cpu_dai_drv->capture.channels_max); 565cb666e5bSLiam Girdwood runtime->hw.formats = 566f0fba2adSLiam Girdwood codec_dai_drv->capture.formats & cpu_dai_drv->capture.formats; 567cb666e5bSLiam Girdwood runtime->hw.rates = 568f0fba2adSLiam Girdwood codec_dai_drv->capture.rates & cpu_dai_drv->capture.rates; 569f0fba2adSLiam Girdwood if (codec_dai_drv->capture.rates 570d9ad6296SJassi Brar & (SNDRV_PCM_RATE_KNOT | SNDRV_PCM_RATE_CONTINUOUS)) 571f0fba2adSLiam Girdwood runtime->hw.rates |= cpu_dai_drv->capture.rates; 572f0fba2adSLiam Girdwood if (cpu_dai_drv->capture.rates 573d9ad6296SJassi Brar & (SNDRV_PCM_RATE_KNOT | SNDRV_PCM_RATE_CONTINUOUS)) 574f0fba2adSLiam Girdwood runtime->hw.rates |= codec_dai_drv->capture.rates; 575db2a4165SFrank Mandarino } 576db2a4165SFrank Mandarino 577db2a4165SFrank Mandarino snd_pcm_limit_hw_rates(runtime); 578db2a4165SFrank Mandarino if (!runtime->hw.rates) { 579db2a4165SFrank Mandarino printk(KERN_ERR "asoc: %s <-> %s No matching rates\n", 580cb666e5bSLiam Girdwood codec_dai->name, cpu_dai->name); 581bb1c0478SJassi Brar goto config_err; 582db2a4165SFrank Mandarino } 583db2a4165SFrank Mandarino if (!runtime->hw.formats) { 584db2a4165SFrank Mandarino printk(KERN_ERR "asoc: %s <-> %s No matching formats\n", 585cb666e5bSLiam Girdwood codec_dai->name, cpu_dai->name); 586bb1c0478SJassi Brar goto config_err; 587db2a4165SFrank Mandarino } 588db2a4165SFrank Mandarino if (!runtime->hw.channels_min || !runtime->hw.channels_max) { 589db2a4165SFrank Mandarino printk(KERN_ERR "asoc: %s <-> %s No matching channels\n", 590cb666e5bSLiam Girdwood codec_dai->name, cpu_dai->name); 591bb1c0478SJassi Brar goto config_err; 592db2a4165SFrank Mandarino } 593db2a4165SFrank Mandarino 59406f409d7SMark Brown /* Symmetry only applies if we've already got an active stream. */ 59506f409d7SMark Brown if (cpu_dai->active || codec_dai->active) { 59606f409d7SMark Brown ret = soc_pcm_apply_symmetry(substream); 59706f409d7SMark Brown if (ret != 0) 598bb1c0478SJassi Brar goto config_err; 59906f409d7SMark Brown } 60006f409d7SMark Brown 601f0fba2adSLiam Girdwood pr_debug("asoc: %s <-> %s info:\n", 602f0fba2adSLiam Girdwood codec_dai->name, cpu_dai->name); 603f24368c2SMark Brown pr_debug("asoc: rate mask 0x%x\n", runtime->hw.rates); 604f24368c2SMark Brown pr_debug("asoc: min ch %d max ch %d\n", runtime->hw.channels_min, 605b5c5fd24SLiam Girdwood runtime->hw.channels_max); 606f24368c2SMark Brown pr_debug("asoc: min rate %d max rate %d\n", runtime->hw.rate_min, 607b5c5fd24SLiam Girdwood runtime->hw.rate_max); 608db2a4165SFrank Mandarino 60914dc5734SJassi Brar if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) { 610f0fba2adSLiam Girdwood cpu_dai->playback_active++; 611f0fba2adSLiam Girdwood codec_dai->playback_active++; 61214dc5734SJassi Brar } else { 613f0fba2adSLiam Girdwood cpu_dai->capture_active++; 614f0fba2adSLiam Girdwood codec_dai->capture_active++; 61514dc5734SJassi Brar } 61614dc5734SJassi Brar cpu_dai->active++; 61714dc5734SJassi Brar codec_dai->active++; 618f0fba2adSLiam Girdwood rtd->codec->active++; 619db2a4165SFrank Mandarino mutex_unlock(&pcm_mutex); 620db2a4165SFrank Mandarino return 0; 621db2a4165SFrank Mandarino 622bb1c0478SJassi Brar config_err: 623f0fba2adSLiam Girdwood if (rtd->dai_link->ops && rtd->dai_link->ops->shutdown) 624f0fba2adSLiam Girdwood rtd->dai_link->ops->shutdown(substream); 625db2a4165SFrank Mandarino 626bb1c0478SJassi Brar machine_err: 627f0fba2adSLiam Girdwood if (codec_dai->driver->ops->shutdown) 628f0fba2adSLiam Girdwood codec_dai->driver->ops->shutdown(substream, codec_dai); 629bb1c0478SJassi Brar 630cb666e5bSLiam Girdwood codec_dai_err: 631f0fba2adSLiam Girdwood if (platform->driver->ops->close) 632f0fba2adSLiam Girdwood platform->driver->ops->close(substream); 633db2a4165SFrank Mandarino 634db2a4165SFrank Mandarino platform_err: 635f0fba2adSLiam Girdwood if (cpu_dai->driver->ops->shutdown) 636f0fba2adSLiam Girdwood cpu_dai->driver->ops->shutdown(substream, cpu_dai); 637db2a4165SFrank Mandarino out: 638db2a4165SFrank Mandarino mutex_unlock(&pcm_mutex); 639db2a4165SFrank Mandarino return ret; 640db2a4165SFrank Mandarino } 641db2a4165SFrank Mandarino 642db2a4165SFrank Mandarino /* 6433a4fa0a2SRobert P. J. Day * Power down the audio subsystem pmdown_time msecs after close is called. 644db2a4165SFrank Mandarino * This is to ensure there are no pops or clicks in between any music tracks 645db2a4165SFrank Mandarino * due to DAPM power cycling. 646db2a4165SFrank Mandarino */ 6474484bb2eSAndrew Morton static void close_delayed_work(struct work_struct *work) 648db2a4165SFrank Mandarino { 649f0fba2adSLiam Girdwood struct snd_soc_pcm_runtime *rtd = 650f0fba2adSLiam Girdwood container_of(work, struct snd_soc_pcm_runtime, delayed_work.work); 651f0fba2adSLiam Girdwood struct snd_soc_dai *codec_dai = rtd->codec_dai; 652db2a4165SFrank Mandarino 653db2a4165SFrank Mandarino mutex_lock(&pcm_mutex); 654db2a4165SFrank Mandarino 655f24368c2SMark Brown pr_debug("pop wq checking: %s status: %s waiting: %s\n", 656f0fba2adSLiam Girdwood codec_dai->driver->playback.stream_name, 657f0fba2adSLiam Girdwood codec_dai->playback_active ? "active" : "inactive", 658db2a4165SFrank Mandarino codec_dai->pop_wait ? "yes" : "no"); 659db2a4165SFrank Mandarino 660db2a4165SFrank Mandarino /* are we waiting on this codec DAI stream */ 661db2a4165SFrank Mandarino if (codec_dai->pop_wait == 1) { 662db2a4165SFrank Mandarino codec_dai->pop_wait = 0; 663f0fba2adSLiam Girdwood snd_soc_dapm_stream_event(rtd, 664f0fba2adSLiam Girdwood codec_dai->driver->playback.stream_name, 665db2a4165SFrank Mandarino SND_SOC_DAPM_STREAM_STOP); 666db2a4165SFrank Mandarino } 667f0fba2adSLiam Girdwood 668db2a4165SFrank Mandarino mutex_unlock(&pcm_mutex); 669db2a4165SFrank Mandarino } 670db2a4165SFrank Mandarino 671db2a4165SFrank Mandarino /* 672db2a4165SFrank Mandarino * Called by ALSA when a PCM substream is closed. Private data can be 673db2a4165SFrank Mandarino * freed here. The cpu DAI, codec DAI, machine and platform are also 674db2a4165SFrank Mandarino * shutdown. 675db2a4165SFrank Mandarino */ 676db2a4165SFrank Mandarino static int soc_codec_close(struct snd_pcm_substream *substream) 677db2a4165SFrank Mandarino { 678db2a4165SFrank Mandarino struct snd_soc_pcm_runtime *rtd = substream->private_data; 679f0fba2adSLiam Girdwood struct snd_soc_platform *platform = rtd->platform; 680f0fba2adSLiam Girdwood struct snd_soc_dai *cpu_dai = rtd->cpu_dai; 681f0fba2adSLiam Girdwood struct snd_soc_dai *codec_dai = rtd->codec_dai; 682f0fba2adSLiam Girdwood struct snd_soc_codec *codec = rtd->codec; 683db2a4165SFrank Mandarino 684db2a4165SFrank Mandarino mutex_lock(&pcm_mutex); 685db2a4165SFrank Mandarino 68614dc5734SJassi Brar if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) { 687f0fba2adSLiam Girdwood cpu_dai->playback_active--; 688f0fba2adSLiam Girdwood codec_dai->playback_active--; 68914dc5734SJassi Brar } else { 690f0fba2adSLiam Girdwood cpu_dai->capture_active--; 691f0fba2adSLiam Girdwood codec_dai->capture_active--; 692db2a4165SFrank Mandarino } 69314dc5734SJassi Brar 69414dc5734SJassi Brar cpu_dai->active--; 69514dc5734SJassi Brar codec_dai->active--; 696db2a4165SFrank Mandarino codec->active--; 697db2a4165SFrank Mandarino 6986010b2daSMark Brown /* Muting the DAC suppresses artifacts caused during digital 6996010b2daSMark Brown * shutdown, for example from stopping clocks. 7006010b2daSMark Brown */ 7016010b2daSMark Brown if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) 7026010b2daSMark Brown snd_soc_dai_digital_mute(codec_dai, 1); 7036010b2daSMark Brown 704f0fba2adSLiam Girdwood if (cpu_dai->driver->ops->shutdown) 705f0fba2adSLiam Girdwood cpu_dai->driver->ops->shutdown(substream, cpu_dai); 706db2a4165SFrank Mandarino 707f0fba2adSLiam Girdwood if (codec_dai->driver->ops->shutdown) 708f0fba2adSLiam Girdwood codec_dai->driver->ops->shutdown(substream, codec_dai); 709db2a4165SFrank Mandarino 710f0fba2adSLiam Girdwood if (rtd->dai_link->ops && rtd->dai_link->ops->shutdown) 711f0fba2adSLiam Girdwood rtd->dai_link->ops->shutdown(substream); 712db2a4165SFrank Mandarino 713f0fba2adSLiam Girdwood if (platform->driver->ops->close) 714f0fba2adSLiam Girdwood platform->driver->ops->close(substream); 715f0fba2adSLiam Girdwood cpu_dai->runtime = NULL; 716db2a4165SFrank Mandarino 717db2a4165SFrank Mandarino if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) { 718db2a4165SFrank Mandarino /* start delayed pop wq here for playback streams */ 719cb666e5bSLiam Girdwood codec_dai->pop_wait = 1; 720f0fba2adSLiam Girdwood schedule_delayed_work(&rtd->delayed_work, 721f0fba2adSLiam Girdwood msecs_to_jiffies(rtd->pmdown_time)); 722db2a4165SFrank Mandarino } else { 723db2a4165SFrank Mandarino /* capture streams can be powered down now */ 724f0fba2adSLiam Girdwood snd_soc_dapm_stream_event(rtd, 725f0fba2adSLiam Girdwood codec_dai->driver->capture.stream_name, 7260b4d221bSLiam Girdwood SND_SOC_DAPM_STREAM_STOP); 727db2a4165SFrank Mandarino } 728db2a4165SFrank Mandarino 729db2a4165SFrank Mandarino mutex_unlock(&pcm_mutex); 730db2a4165SFrank Mandarino return 0; 731db2a4165SFrank Mandarino } 732db2a4165SFrank Mandarino 733db2a4165SFrank Mandarino /* 734db2a4165SFrank Mandarino * Called by ALSA when the PCM substream is prepared, can set format, sample 735db2a4165SFrank Mandarino * rate, etc. This function is non atomic and can be called multiple times, 736db2a4165SFrank Mandarino * it can refer to the runtime info. 737db2a4165SFrank Mandarino */ 738db2a4165SFrank Mandarino static int soc_pcm_prepare(struct snd_pcm_substream *substream) 739db2a4165SFrank Mandarino { 740db2a4165SFrank Mandarino struct snd_soc_pcm_runtime *rtd = substream->private_data; 741f0fba2adSLiam Girdwood struct snd_soc_platform *platform = rtd->platform; 742f0fba2adSLiam Girdwood struct snd_soc_dai *cpu_dai = rtd->cpu_dai; 743f0fba2adSLiam Girdwood struct snd_soc_dai *codec_dai = rtd->codec_dai; 744db2a4165SFrank Mandarino int ret = 0; 745db2a4165SFrank Mandarino 746db2a4165SFrank Mandarino mutex_lock(&pcm_mutex); 747cb666e5bSLiam Girdwood 748f0fba2adSLiam Girdwood if (rtd->dai_link->ops && rtd->dai_link->ops->prepare) { 749f0fba2adSLiam Girdwood ret = rtd->dai_link->ops->prepare(substream); 750cb666e5bSLiam Girdwood if (ret < 0) { 751cb666e5bSLiam Girdwood printk(KERN_ERR "asoc: machine prepare error\n"); 752cb666e5bSLiam Girdwood goto out; 753cb666e5bSLiam Girdwood } 754cb666e5bSLiam Girdwood } 755cb666e5bSLiam Girdwood 756f0fba2adSLiam Girdwood if (platform->driver->ops->prepare) { 757f0fba2adSLiam Girdwood ret = platform->driver->ops->prepare(substream); 758a71a468aSLiam Girdwood if (ret < 0) { 759a71a468aSLiam Girdwood printk(KERN_ERR "asoc: platform prepare error\n"); 760db2a4165SFrank Mandarino goto out; 761db2a4165SFrank Mandarino } 762a71a468aSLiam Girdwood } 763db2a4165SFrank Mandarino 764f0fba2adSLiam Girdwood if (codec_dai->driver->ops->prepare) { 765f0fba2adSLiam Girdwood ret = codec_dai->driver->ops->prepare(substream, codec_dai); 766a71a468aSLiam Girdwood if (ret < 0) { 767a71a468aSLiam Girdwood printk(KERN_ERR "asoc: codec DAI prepare error\n"); 768db2a4165SFrank Mandarino goto out; 769db2a4165SFrank Mandarino } 770a71a468aSLiam Girdwood } 771db2a4165SFrank Mandarino 772f0fba2adSLiam Girdwood if (cpu_dai->driver->ops->prepare) { 773f0fba2adSLiam Girdwood ret = cpu_dai->driver->ops->prepare(substream, cpu_dai); 774cb666e5bSLiam Girdwood if (ret < 0) { 775cb666e5bSLiam Girdwood printk(KERN_ERR "asoc: cpu DAI prepare error\n"); 776cb666e5bSLiam Girdwood goto out; 777cb666e5bSLiam Girdwood } 778cb666e5bSLiam Girdwood } 779db2a4165SFrank Mandarino 780d45f6219SMark Brown /* cancel any delayed stream shutdown that is pending */ 781d45f6219SMark Brown if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK && 782d45f6219SMark Brown codec_dai->pop_wait) { 783cb666e5bSLiam Girdwood codec_dai->pop_wait = 0; 784f0fba2adSLiam Girdwood cancel_delayed_work(&rtd->delayed_work); 785db2a4165SFrank Mandarino } 786db2a4165SFrank Mandarino 787db2a4165SFrank Mandarino if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) 788f0fba2adSLiam Girdwood snd_soc_dapm_stream_event(rtd, 789f0fba2adSLiam Girdwood codec_dai->driver->playback.stream_name, 790db2a4165SFrank Mandarino SND_SOC_DAPM_STREAM_START); 791db2a4165SFrank Mandarino else 792f0fba2adSLiam Girdwood snd_soc_dapm_stream_event(rtd, 793f0fba2adSLiam Girdwood codec_dai->driver->capture.stream_name, 794db2a4165SFrank Mandarino SND_SOC_DAPM_STREAM_START); 7958c6529dbSLiam Girdwood 7968c6529dbSLiam Girdwood snd_soc_dai_digital_mute(codec_dai, 0); 797db2a4165SFrank Mandarino 798db2a4165SFrank Mandarino out: 799db2a4165SFrank Mandarino mutex_unlock(&pcm_mutex); 800db2a4165SFrank Mandarino return ret; 801db2a4165SFrank Mandarino } 802db2a4165SFrank Mandarino 803db2a4165SFrank Mandarino /* 804db2a4165SFrank Mandarino * Called by ALSA when the hardware params are set by application. This 805db2a4165SFrank Mandarino * function can also be called multiple times and can allocate buffers 806db2a4165SFrank Mandarino * (using snd_pcm_lib_* ). It's non-atomic. 807db2a4165SFrank Mandarino */ 808db2a4165SFrank Mandarino static int soc_pcm_hw_params(struct snd_pcm_substream *substream, 809db2a4165SFrank Mandarino struct snd_pcm_hw_params *params) 810db2a4165SFrank Mandarino { 811db2a4165SFrank Mandarino struct snd_soc_pcm_runtime *rtd = substream->private_data; 812f0fba2adSLiam Girdwood struct snd_soc_platform *platform = rtd->platform; 813f0fba2adSLiam Girdwood struct snd_soc_dai *cpu_dai = rtd->cpu_dai; 814f0fba2adSLiam Girdwood struct snd_soc_dai *codec_dai = rtd->codec_dai; 815db2a4165SFrank Mandarino int ret = 0; 816db2a4165SFrank Mandarino 817db2a4165SFrank Mandarino mutex_lock(&pcm_mutex); 818db2a4165SFrank Mandarino 819f0fba2adSLiam Girdwood if (rtd->dai_link->ops && rtd->dai_link->ops->hw_params) { 820f0fba2adSLiam Girdwood ret = rtd->dai_link->ops->hw_params(substream, params); 821cb666e5bSLiam Girdwood if (ret < 0) { 822cb666e5bSLiam Girdwood printk(KERN_ERR "asoc: machine hw_params failed\n"); 823db2a4165SFrank Mandarino goto out; 824db2a4165SFrank Mandarino } 825cb666e5bSLiam Girdwood } 826db2a4165SFrank Mandarino 827f0fba2adSLiam Girdwood if (codec_dai->driver->ops->hw_params) { 828f0fba2adSLiam Girdwood ret = codec_dai->driver->ops->hw_params(substream, params, codec_dai); 829db2a4165SFrank Mandarino if (ret < 0) { 830db2a4165SFrank Mandarino printk(KERN_ERR "asoc: can't set codec %s hw params\n", 831cb666e5bSLiam Girdwood codec_dai->name); 832cb666e5bSLiam Girdwood goto codec_err; 833db2a4165SFrank Mandarino } 834db2a4165SFrank Mandarino } 835db2a4165SFrank Mandarino 836f0fba2adSLiam Girdwood if (cpu_dai->driver->ops->hw_params) { 837f0fba2adSLiam Girdwood ret = cpu_dai->driver->ops->hw_params(substream, params, cpu_dai); 838db2a4165SFrank Mandarino if (ret < 0) { 8393ff3f64bSMark Brown printk(KERN_ERR "asoc: interface %s hw params failed\n", 840cb666e5bSLiam Girdwood cpu_dai->name); 841db2a4165SFrank Mandarino goto interface_err; 842db2a4165SFrank Mandarino } 843db2a4165SFrank Mandarino } 844db2a4165SFrank Mandarino 845f0fba2adSLiam Girdwood if (platform->driver->ops->hw_params) { 846f0fba2adSLiam Girdwood ret = platform->driver->ops->hw_params(substream, params); 847db2a4165SFrank Mandarino if (ret < 0) { 8483ff3f64bSMark Brown printk(KERN_ERR "asoc: platform %s hw params failed\n", 849db2a4165SFrank Mandarino platform->name); 850db2a4165SFrank Mandarino goto platform_err; 851db2a4165SFrank Mandarino } 852db2a4165SFrank Mandarino } 853db2a4165SFrank Mandarino 854f0fba2adSLiam Girdwood rtd->rate = params_rate(params); 85506f409d7SMark Brown 856db2a4165SFrank Mandarino out: 857db2a4165SFrank Mandarino mutex_unlock(&pcm_mutex); 858db2a4165SFrank Mandarino return ret; 859db2a4165SFrank Mandarino 860db2a4165SFrank Mandarino platform_err: 861f0fba2adSLiam Girdwood if (cpu_dai->driver->ops->hw_free) 862f0fba2adSLiam Girdwood cpu_dai->driver->ops->hw_free(substream, cpu_dai); 863db2a4165SFrank Mandarino 864db2a4165SFrank Mandarino interface_err: 865f0fba2adSLiam Girdwood if (codec_dai->driver->ops->hw_free) 866f0fba2adSLiam Girdwood codec_dai->driver->ops->hw_free(substream, codec_dai); 867cb666e5bSLiam Girdwood 868cb666e5bSLiam Girdwood codec_err: 869f0fba2adSLiam Girdwood if (rtd->dai_link->ops && rtd->dai_link->ops->hw_free) 870f0fba2adSLiam Girdwood rtd->dai_link->ops->hw_free(substream); 871db2a4165SFrank Mandarino 872db2a4165SFrank Mandarino mutex_unlock(&pcm_mutex); 873db2a4165SFrank Mandarino return ret; 874db2a4165SFrank Mandarino } 875db2a4165SFrank Mandarino 876db2a4165SFrank Mandarino /* 877a00f90f9SMark Brown * Frees resources allocated by hw_params, can be called multiple times 878db2a4165SFrank Mandarino */ 879db2a4165SFrank Mandarino static int soc_pcm_hw_free(struct snd_pcm_substream *substream) 880db2a4165SFrank Mandarino { 881db2a4165SFrank Mandarino struct snd_soc_pcm_runtime *rtd = substream->private_data; 882f0fba2adSLiam Girdwood struct snd_soc_platform *platform = rtd->platform; 883f0fba2adSLiam Girdwood struct snd_soc_dai *cpu_dai = rtd->cpu_dai; 884f0fba2adSLiam Girdwood struct snd_soc_dai *codec_dai = rtd->codec_dai; 885f0fba2adSLiam Girdwood struct snd_soc_codec *codec = rtd->codec; 886db2a4165SFrank Mandarino 887db2a4165SFrank Mandarino mutex_lock(&pcm_mutex); 888db2a4165SFrank Mandarino 889db2a4165SFrank Mandarino /* apply codec digital mute */ 8908c6529dbSLiam Girdwood if (!codec->active) 8918c6529dbSLiam Girdwood snd_soc_dai_digital_mute(codec_dai, 1); 892db2a4165SFrank Mandarino 893db2a4165SFrank Mandarino /* free any machine hw params */ 894f0fba2adSLiam Girdwood if (rtd->dai_link->ops && rtd->dai_link->ops->hw_free) 895f0fba2adSLiam Girdwood rtd->dai_link->ops->hw_free(substream); 896db2a4165SFrank Mandarino 897db2a4165SFrank Mandarino /* free any DMA resources */ 898f0fba2adSLiam Girdwood if (platform->driver->ops->hw_free) 899f0fba2adSLiam Girdwood platform->driver->ops->hw_free(substream); 900db2a4165SFrank Mandarino 901a00f90f9SMark Brown /* now free hw params for the DAIs */ 902f0fba2adSLiam Girdwood if (codec_dai->driver->ops->hw_free) 903f0fba2adSLiam Girdwood codec_dai->driver->ops->hw_free(substream, codec_dai); 904db2a4165SFrank Mandarino 905f0fba2adSLiam Girdwood if (cpu_dai->driver->ops->hw_free) 906f0fba2adSLiam Girdwood cpu_dai->driver->ops->hw_free(substream, cpu_dai); 907db2a4165SFrank Mandarino 908db2a4165SFrank Mandarino mutex_unlock(&pcm_mutex); 909db2a4165SFrank Mandarino return 0; 910db2a4165SFrank Mandarino } 911db2a4165SFrank Mandarino 912db2a4165SFrank Mandarino static int soc_pcm_trigger(struct snd_pcm_substream *substream, int cmd) 913db2a4165SFrank Mandarino { 914db2a4165SFrank Mandarino struct snd_soc_pcm_runtime *rtd = substream->private_data; 915f0fba2adSLiam Girdwood struct snd_soc_platform *platform = rtd->platform; 916f0fba2adSLiam Girdwood struct snd_soc_dai *cpu_dai = rtd->cpu_dai; 917f0fba2adSLiam Girdwood struct snd_soc_dai *codec_dai = rtd->codec_dai; 918db2a4165SFrank Mandarino int ret; 919db2a4165SFrank Mandarino 920f0fba2adSLiam Girdwood if (codec_dai->driver->ops->trigger) { 921f0fba2adSLiam Girdwood ret = codec_dai->driver->ops->trigger(substream, cmd, codec_dai); 922db2a4165SFrank Mandarino if (ret < 0) 923db2a4165SFrank Mandarino return ret; 924db2a4165SFrank Mandarino } 925db2a4165SFrank Mandarino 926f0fba2adSLiam Girdwood if (platform->driver->ops->trigger) { 927f0fba2adSLiam Girdwood ret = platform->driver->ops->trigger(substream, cmd); 928db2a4165SFrank Mandarino if (ret < 0) 929db2a4165SFrank Mandarino return ret; 930db2a4165SFrank Mandarino } 931db2a4165SFrank Mandarino 932f0fba2adSLiam Girdwood if (cpu_dai->driver->ops->trigger) { 933f0fba2adSLiam Girdwood ret = cpu_dai->driver->ops->trigger(substream, cmd, cpu_dai); 934db2a4165SFrank Mandarino if (ret < 0) 935db2a4165SFrank Mandarino return ret; 936db2a4165SFrank Mandarino } 937db2a4165SFrank Mandarino return 0; 938db2a4165SFrank Mandarino } 939db2a4165SFrank Mandarino 940377b6f62SPeter Ujfalusi /* 941377b6f62SPeter Ujfalusi * soc level wrapper for pointer callback 942258020d0SPeter Ujfalusi * If cpu_dai, codec_dai, platform driver has the delay callback, than 943258020d0SPeter Ujfalusi * the runtime->delay will be updated accordingly. 944377b6f62SPeter Ujfalusi */ 945377b6f62SPeter Ujfalusi static snd_pcm_uframes_t soc_pcm_pointer(struct snd_pcm_substream *substream) 946377b6f62SPeter Ujfalusi { 947377b6f62SPeter Ujfalusi struct snd_soc_pcm_runtime *rtd = substream->private_data; 948f0fba2adSLiam Girdwood struct snd_soc_platform *platform = rtd->platform; 949f0fba2adSLiam Girdwood struct snd_soc_dai *cpu_dai = rtd->cpu_dai; 950f0fba2adSLiam Girdwood struct snd_soc_dai *codec_dai = rtd->codec_dai; 951258020d0SPeter Ujfalusi struct snd_pcm_runtime *runtime = substream->runtime; 952377b6f62SPeter Ujfalusi snd_pcm_uframes_t offset = 0; 953258020d0SPeter Ujfalusi snd_pcm_sframes_t delay = 0; 954377b6f62SPeter Ujfalusi 955f0fba2adSLiam Girdwood if (platform->driver->ops->pointer) 956f0fba2adSLiam Girdwood offset = platform->driver->ops->pointer(substream); 957377b6f62SPeter Ujfalusi 958f0fba2adSLiam Girdwood if (cpu_dai->driver->ops->delay) 959f0fba2adSLiam Girdwood delay += cpu_dai->driver->ops->delay(substream, cpu_dai); 960258020d0SPeter Ujfalusi 961f0fba2adSLiam Girdwood if (codec_dai->driver->ops->delay) 962f0fba2adSLiam Girdwood delay += codec_dai->driver->ops->delay(substream, codec_dai); 963258020d0SPeter Ujfalusi 964f0fba2adSLiam Girdwood if (platform->driver->delay) 965f0fba2adSLiam Girdwood delay += platform->driver->delay(substream, codec_dai); 966258020d0SPeter Ujfalusi 967258020d0SPeter Ujfalusi runtime->delay = delay; 968258020d0SPeter Ujfalusi 969377b6f62SPeter Ujfalusi return offset; 970377b6f62SPeter Ujfalusi } 971377b6f62SPeter Ujfalusi 972db2a4165SFrank Mandarino /* ASoC PCM operations */ 973db2a4165SFrank Mandarino static struct snd_pcm_ops soc_pcm_ops = { 974db2a4165SFrank Mandarino .open = soc_pcm_open, 975db2a4165SFrank Mandarino .close = soc_codec_close, 976db2a4165SFrank Mandarino .hw_params = soc_pcm_hw_params, 977db2a4165SFrank Mandarino .hw_free = soc_pcm_hw_free, 978db2a4165SFrank Mandarino .prepare = soc_pcm_prepare, 979db2a4165SFrank Mandarino .trigger = soc_pcm_trigger, 980377b6f62SPeter Ujfalusi .pointer = soc_pcm_pointer, 981db2a4165SFrank Mandarino }; 982db2a4165SFrank Mandarino 983db2a4165SFrank Mandarino #ifdef CONFIG_PM 984db2a4165SFrank Mandarino /* powers down audio subsystem for suspend */ 985416356fcSMark Brown static int soc_suspend(struct device *dev) 986db2a4165SFrank Mandarino { 987416356fcSMark Brown struct platform_device *pdev = to_platform_device(dev); 988f0fba2adSLiam Girdwood struct snd_soc_card *card = platform_get_drvdata(pdev); 9892eea392dSJarkko Nikula struct snd_soc_codec *codec; 990db2a4165SFrank Mandarino int i; 991db2a4165SFrank Mandarino 992e3509ff0SDaniel Mack /* If the initialization of this soc device failed, there is no codec 993e3509ff0SDaniel Mack * associated with it. Just bail out in this case. 994e3509ff0SDaniel Mack */ 995f0fba2adSLiam Girdwood if (list_empty(&card->codec_dev_list)) 996e3509ff0SDaniel Mack return 0; 997e3509ff0SDaniel Mack 9986ed25978SAndy Green /* Due to the resume being scheduled into a workqueue we could 9996ed25978SAndy Green * suspend before that's finished - wait for it to complete. 10006ed25978SAndy Green */ 1001f0fba2adSLiam Girdwood snd_power_lock(card->snd_card); 1002f0fba2adSLiam Girdwood snd_power_wait(card->snd_card, SNDRV_CTL_POWER_D0); 1003f0fba2adSLiam Girdwood snd_power_unlock(card->snd_card); 10046ed25978SAndy Green 10056ed25978SAndy Green /* we're going to block userspace touching us until resume completes */ 1006f0fba2adSLiam Girdwood snd_power_change_state(card->snd_card, SNDRV_CTL_POWER_D3hot); 10076ed25978SAndy Green 1008a00f90f9SMark Brown /* mute any active DACs */ 1009f0fba2adSLiam Girdwood for (i = 0; i < card->num_rtd; i++) { 1010f0fba2adSLiam Girdwood struct snd_soc_dai *dai = card->rtd[i].codec_dai; 1011f0fba2adSLiam Girdwood struct snd_soc_dai_driver *drv = dai->driver; 10123efab7dcSMark Brown 1013f0fba2adSLiam Girdwood if (card->rtd[i].dai_link->ignore_suspend) 10143efab7dcSMark Brown continue; 10153efab7dcSMark Brown 1016f0fba2adSLiam Girdwood if (drv->ops->digital_mute && dai->playback_active) 1017f0fba2adSLiam Girdwood drv->ops->digital_mute(dai, 1); 1018db2a4165SFrank Mandarino } 1019db2a4165SFrank Mandarino 10204ccab3e7SLiam Girdwood /* suspend all pcms */ 1021f0fba2adSLiam Girdwood for (i = 0; i < card->num_rtd; i++) { 1022f0fba2adSLiam Girdwood if (card->rtd[i].dai_link->ignore_suspend) 10233efab7dcSMark Brown continue; 10243efab7dcSMark Brown 1025f0fba2adSLiam Girdwood snd_pcm_suspend_all(card->rtd[i].pcm); 10263efab7dcSMark Brown } 10274ccab3e7SLiam Girdwood 102887506549SMark Brown if (card->suspend_pre) 1029416356fcSMark Brown card->suspend_pre(pdev, PMSG_SUSPEND); 1030db2a4165SFrank Mandarino 1031f0fba2adSLiam Girdwood for (i = 0; i < card->num_rtd; i++) { 1032f0fba2adSLiam Girdwood struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai; 1033f0fba2adSLiam Girdwood struct snd_soc_platform *platform = card->rtd[i].platform; 10343efab7dcSMark Brown 1035f0fba2adSLiam Girdwood if (card->rtd[i].dai_link->ignore_suspend) 10363efab7dcSMark Brown continue; 10373efab7dcSMark Brown 1038f0fba2adSLiam Girdwood if (cpu_dai->driver->suspend && !cpu_dai->driver->ac97_control) 1039f0fba2adSLiam Girdwood cpu_dai->driver->suspend(cpu_dai); 1040f0fba2adSLiam Girdwood if (platform->driver->suspend && !platform->suspended) { 1041f0fba2adSLiam Girdwood platform->driver->suspend(cpu_dai); 1042f0fba2adSLiam Girdwood platform->suspended = 1; 1043f0fba2adSLiam Girdwood } 1044db2a4165SFrank Mandarino } 1045db2a4165SFrank Mandarino 1046db2a4165SFrank Mandarino /* close any waiting streams and save state */ 1047f0fba2adSLiam Girdwood for (i = 0; i < card->num_rtd; i++) { 1048f0fba2adSLiam Girdwood run_delayed_work(&card->rtd[i].delayed_work); 1049ce6120ccSLiam Girdwood card->rtd[i].codec->dapm.suspend_bias_level = card->rtd[i].codec->dapm.bias_level; 1050f0fba2adSLiam Girdwood } 1051db2a4165SFrank Mandarino 1052f0fba2adSLiam Girdwood for (i = 0; i < card->num_rtd; i++) { 1053f0fba2adSLiam Girdwood struct snd_soc_dai_driver *driver = card->rtd[i].codec_dai->driver; 10543efab7dcSMark Brown 1055f0fba2adSLiam Girdwood if (card->rtd[i].dai_link->ignore_suspend) 10563efab7dcSMark Brown continue; 10573efab7dcSMark Brown 1058f0fba2adSLiam Girdwood if (driver->playback.stream_name != NULL) 1059f0fba2adSLiam Girdwood snd_soc_dapm_stream_event(&card->rtd[i], driver->playback.stream_name, 1060db2a4165SFrank Mandarino SND_SOC_DAPM_STREAM_SUSPEND); 1061f0fba2adSLiam Girdwood 1062f0fba2adSLiam Girdwood if (driver->capture.stream_name != NULL) 1063f0fba2adSLiam Girdwood snd_soc_dapm_stream_event(&card->rtd[i], driver->capture.stream_name, 1064db2a4165SFrank Mandarino SND_SOC_DAPM_STREAM_SUSPEND); 1065db2a4165SFrank Mandarino } 1066db2a4165SFrank Mandarino 1067f0fba2adSLiam Girdwood /* suspend all CODECs */ 10682eea392dSJarkko Nikula list_for_each_entry(codec, &card->codec_dev_list, card_list) { 10691547aba9SMark Brown /* If there are paths active then the CODEC will be held with 10701547aba9SMark Brown * bias _ON and should not be suspended. */ 1071f0fba2adSLiam Girdwood if (!codec->suspended && codec->driver->suspend) { 1072ce6120ccSLiam Girdwood switch (codec->dapm.bias_level) { 10731547aba9SMark Brown case SND_SOC_BIAS_STANDBY: 10741547aba9SMark Brown case SND_SOC_BIAS_OFF: 1075f0fba2adSLiam Girdwood codec->driver->suspend(codec, PMSG_SUSPEND); 1076f0fba2adSLiam Girdwood codec->suspended = 1; 10771547aba9SMark Brown break; 10781547aba9SMark Brown default: 1079f0fba2adSLiam Girdwood dev_dbg(codec->dev, "CODEC is on over suspend\n"); 10801547aba9SMark Brown break; 10811547aba9SMark Brown } 10821547aba9SMark Brown } 1083f0fba2adSLiam Girdwood } 1084db2a4165SFrank Mandarino 1085f0fba2adSLiam Girdwood for (i = 0; i < card->num_rtd; i++) { 1086f0fba2adSLiam Girdwood struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai; 10873efab7dcSMark Brown 1088f0fba2adSLiam Girdwood if (card->rtd[i].dai_link->ignore_suspend) 10893efab7dcSMark Brown continue; 10903efab7dcSMark Brown 1091f0fba2adSLiam Girdwood if (cpu_dai->driver->suspend && cpu_dai->driver->ac97_control) 1092f0fba2adSLiam Girdwood cpu_dai->driver->suspend(cpu_dai); 1093db2a4165SFrank Mandarino } 1094db2a4165SFrank Mandarino 109587506549SMark Brown if (card->suspend_post) 1096416356fcSMark Brown card->suspend_post(pdev, PMSG_SUSPEND); 1097db2a4165SFrank Mandarino 1098db2a4165SFrank Mandarino return 0; 1099db2a4165SFrank Mandarino } 1100db2a4165SFrank Mandarino 11016ed25978SAndy Green /* deferred resume work, so resume can complete before we finished 11026ed25978SAndy Green * setting our codec back up, which can be very slow on I2C 11036ed25978SAndy Green */ 11046ed25978SAndy Green static void soc_resume_deferred(struct work_struct *work) 1105db2a4165SFrank Mandarino { 1106f0fba2adSLiam Girdwood struct snd_soc_card *card = 1107f0fba2adSLiam Girdwood container_of(work, struct snd_soc_card, deferred_resume_work); 1108f0fba2adSLiam Girdwood struct platform_device *pdev = to_platform_device(card->dev); 11092eea392dSJarkko Nikula struct snd_soc_codec *codec; 1110db2a4165SFrank Mandarino int i; 1111db2a4165SFrank Mandarino 11126ed25978SAndy Green /* our power state is still SNDRV_CTL_POWER_D3hot from suspend time, 11136ed25978SAndy Green * so userspace apps are blocked from touching us 11146ed25978SAndy Green */ 11156ed25978SAndy Green 1116f0fba2adSLiam Girdwood dev_dbg(card->dev, "starting resume work\n"); 11176ed25978SAndy Green 11189949788bSMark Brown /* Bring us up into D2 so that DAPM starts enabling things */ 1119f0fba2adSLiam Girdwood snd_power_change_state(card->snd_card, SNDRV_CTL_POWER_D2); 11209949788bSMark Brown 112187506549SMark Brown if (card->resume_pre) 112287506549SMark Brown card->resume_pre(pdev); 1123db2a4165SFrank Mandarino 1124f0fba2adSLiam Girdwood /* resume AC97 DAIs */ 1125f0fba2adSLiam Girdwood for (i = 0; i < card->num_rtd; i++) { 1126f0fba2adSLiam Girdwood struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai; 11273efab7dcSMark Brown 1128f0fba2adSLiam Girdwood if (card->rtd[i].dai_link->ignore_suspend) 11293efab7dcSMark Brown continue; 11303efab7dcSMark Brown 1131f0fba2adSLiam Girdwood if (cpu_dai->driver->resume && cpu_dai->driver->ac97_control) 1132f0fba2adSLiam Girdwood cpu_dai->driver->resume(cpu_dai); 1133db2a4165SFrank Mandarino } 1134db2a4165SFrank Mandarino 11352eea392dSJarkko Nikula list_for_each_entry(codec, &card->codec_dev_list, card_list) { 11361547aba9SMark Brown /* If the CODEC was idle over suspend then it will have been 11371547aba9SMark Brown * left with bias OFF or STANDBY and suspended so we must now 11381547aba9SMark Brown * resume. Otherwise the suspend was suppressed. 11391547aba9SMark Brown */ 1140f0fba2adSLiam Girdwood if (codec->driver->resume && codec->suspended) { 1141ce6120ccSLiam Girdwood switch (codec->dapm.bias_level) { 11421547aba9SMark Brown case SND_SOC_BIAS_STANDBY: 11431547aba9SMark Brown case SND_SOC_BIAS_OFF: 1144f0fba2adSLiam Girdwood codec->driver->resume(codec); 1145f0fba2adSLiam Girdwood codec->suspended = 0; 11461547aba9SMark Brown break; 11471547aba9SMark Brown default: 1148f0fba2adSLiam Girdwood dev_dbg(codec->dev, "CODEC was on over suspend\n"); 11491547aba9SMark Brown break; 11501547aba9SMark Brown } 11511547aba9SMark Brown } 1152f0fba2adSLiam Girdwood } 1153db2a4165SFrank Mandarino 1154f0fba2adSLiam Girdwood for (i = 0; i < card->num_rtd; i++) { 1155f0fba2adSLiam Girdwood struct snd_soc_dai_driver *driver = card->rtd[i].codec_dai->driver; 11563efab7dcSMark Brown 1157f0fba2adSLiam Girdwood if (card->rtd[i].dai_link->ignore_suspend) 11583efab7dcSMark Brown continue; 11593efab7dcSMark Brown 1160f0fba2adSLiam Girdwood if (driver->playback.stream_name != NULL) 1161f0fba2adSLiam Girdwood snd_soc_dapm_stream_event(&card->rtd[i], driver->playback.stream_name, 1162db2a4165SFrank Mandarino SND_SOC_DAPM_STREAM_RESUME); 1163f0fba2adSLiam Girdwood 1164f0fba2adSLiam Girdwood if (driver->capture.stream_name != NULL) 1165f0fba2adSLiam Girdwood snd_soc_dapm_stream_event(&card->rtd[i], driver->capture.stream_name, 1166db2a4165SFrank Mandarino SND_SOC_DAPM_STREAM_RESUME); 1167db2a4165SFrank Mandarino } 1168db2a4165SFrank Mandarino 11693ff3f64bSMark Brown /* unmute any active DACs */ 1170f0fba2adSLiam Girdwood for (i = 0; i < card->num_rtd; i++) { 1171f0fba2adSLiam Girdwood struct snd_soc_dai *dai = card->rtd[i].codec_dai; 1172f0fba2adSLiam Girdwood struct snd_soc_dai_driver *drv = dai->driver; 11733efab7dcSMark Brown 1174f0fba2adSLiam Girdwood if (card->rtd[i].dai_link->ignore_suspend) 11753efab7dcSMark Brown continue; 11763efab7dcSMark Brown 1177f0fba2adSLiam Girdwood if (drv->ops->digital_mute && dai->playback_active) 1178f0fba2adSLiam Girdwood drv->ops->digital_mute(dai, 0); 1179db2a4165SFrank Mandarino } 1180db2a4165SFrank Mandarino 1181f0fba2adSLiam Girdwood for (i = 0; i < card->num_rtd; i++) { 1182f0fba2adSLiam Girdwood struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai; 1183f0fba2adSLiam Girdwood struct snd_soc_platform *platform = card->rtd[i].platform; 11843efab7dcSMark Brown 1185f0fba2adSLiam Girdwood if (card->rtd[i].dai_link->ignore_suspend) 11863efab7dcSMark Brown continue; 11873efab7dcSMark Brown 1188f0fba2adSLiam Girdwood if (cpu_dai->driver->resume && !cpu_dai->driver->ac97_control) 1189f0fba2adSLiam Girdwood cpu_dai->driver->resume(cpu_dai); 1190f0fba2adSLiam Girdwood if (platform->driver->resume && platform->suspended) { 1191f0fba2adSLiam Girdwood platform->driver->resume(cpu_dai); 1192f0fba2adSLiam Girdwood platform->suspended = 0; 1193f0fba2adSLiam Girdwood } 1194db2a4165SFrank Mandarino } 1195db2a4165SFrank Mandarino 119687506549SMark Brown if (card->resume_post) 119787506549SMark Brown card->resume_post(pdev); 1198db2a4165SFrank Mandarino 1199f0fba2adSLiam Girdwood dev_dbg(card->dev, "resume work completed\n"); 12006ed25978SAndy Green 12016ed25978SAndy Green /* userspace can access us now we are back as we were before */ 1202f0fba2adSLiam Girdwood snd_power_change_state(card->snd_card, SNDRV_CTL_POWER_D0); 12036ed25978SAndy Green } 12046ed25978SAndy Green 12056ed25978SAndy Green /* powers up audio subsystem after a suspend */ 1206416356fcSMark Brown static int soc_resume(struct device *dev) 12076ed25978SAndy Green { 1208416356fcSMark Brown struct platform_device *pdev = to_platform_device(dev); 1209f0fba2adSLiam Girdwood struct snd_soc_card *card = platform_get_drvdata(pdev); 1210f0fba2adSLiam Girdwood int i; 1211b9dd94a8SPeter Ujfalusi 121264ab9baaSMark Brown /* AC97 devices might have other drivers hanging off them so 121364ab9baaSMark Brown * need to resume immediately. Other drivers don't have that 121464ab9baaSMark Brown * problem and may take a substantial amount of time to resume 121564ab9baaSMark Brown * due to I/O costs and anti-pop so handle them out of line. 121664ab9baaSMark Brown */ 1217f0fba2adSLiam Girdwood for (i = 0; i < card->num_rtd; i++) { 1218f0fba2adSLiam Girdwood struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai; 1219f0fba2adSLiam Girdwood if (cpu_dai->driver->ac97_control) { 1220f0fba2adSLiam Girdwood dev_dbg(dev, "Resuming AC97 immediately\n"); 122164ab9baaSMark Brown soc_resume_deferred(&card->deferred_resume_work); 122264ab9baaSMark Brown } else { 1223f0fba2adSLiam Girdwood dev_dbg(dev, "Scheduling resume work\n"); 12246308419aSMark Brown if (!schedule_work(&card->deferred_resume_work)) 1225f0fba2adSLiam Girdwood dev_err(dev, "resume work item may be lost\n"); 1226f0fba2adSLiam Girdwood } 122764ab9baaSMark Brown } 12286ed25978SAndy Green 1229db2a4165SFrank Mandarino return 0; 1230db2a4165SFrank Mandarino } 1231db2a4165SFrank Mandarino #else 1232db2a4165SFrank Mandarino #define soc_suspend NULL 1233db2a4165SFrank Mandarino #define soc_resume NULL 1234db2a4165SFrank Mandarino #endif 1235db2a4165SFrank Mandarino 123602a06d30SBarry Song static struct snd_soc_dai_ops null_dai_ops = { 123702a06d30SBarry Song }; 123802a06d30SBarry Song 1239f0fba2adSLiam Girdwood static int soc_bind_dai_link(struct snd_soc_card *card, int num) 1240db2a4165SFrank Mandarino { 1241f0fba2adSLiam Girdwood struct snd_soc_dai_link *dai_link = &card->dai_link[num]; 1242f0fba2adSLiam Girdwood struct snd_soc_pcm_runtime *rtd = &card->rtd[num]; 1243fe3e78e0SMark Brown struct snd_soc_codec *codec; 1244435c5e25SMark Brown struct snd_soc_platform *platform; 1245f0fba2adSLiam Girdwood struct snd_soc_dai *codec_dai, *cpu_dai; 1246db2a4165SFrank Mandarino 1247f0fba2adSLiam Girdwood if (rtd->complete) 1248f0fba2adSLiam Girdwood return 1; 1249f0fba2adSLiam Girdwood dev_dbg(card->dev, "binding %s at idx %d\n", dai_link->name, num); 12506308419aSMark Brown 1251f0fba2adSLiam Girdwood /* do we already have the CPU DAI for this link ? */ 1252f0fba2adSLiam Girdwood if (rtd->cpu_dai) { 1253f0fba2adSLiam Girdwood goto find_codec; 1254435c5e25SMark Brown } 1255f0fba2adSLiam Girdwood /* no, then find CPU DAI from registered DAIs*/ 1256f0fba2adSLiam Girdwood list_for_each_entry(cpu_dai, &dai_list, list) { 1257f0fba2adSLiam Girdwood if (!strcmp(cpu_dai->name, dai_link->cpu_dai_name)) { 1258f0fba2adSLiam Girdwood 1259f0fba2adSLiam Girdwood if (!try_module_get(cpu_dai->dev->driver->owner)) 1260f0fba2adSLiam Girdwood return -ENODEV; 1261f0fba2adSLiam Girdwood 1262f0fba2adSLiam Girdwood rtd->cpu_dai = cpu_dai; 1263f0fba2adSLiam Girdwood goto find_codec; 1264f0fba2adSLiam Girdwood } 1265f0fba2adSLiam Girdwood } 1266f0fba2adSLiam Girdwood dev_dbg(card->dev, "CPU DAI %s not registered\n", 1267f0fba2adSLiam Girdwood dai_link->cpu_dai_name); 1268f0fba2adSLiam Girdwood 1269f0fba2adSLiam Girdwood find_codec: 1270f0fba2adSLiam Girdwood /* do we already have the CODEC for this link ? */ 1271f0fba2adSLiam Girdwood if (rtd->codec) { 1272f0fba2adSLiam Girdwood goto find_platform; 1273c5af3a2eSMark Brown } 1274c5af3a2eSMark Brown 1275f0fba2adSLiam Girdwood /* no, then find CODEC from registered CODECs*/ 1276f0fba2adSLiam Girdwood list_for_each_entry(codec, &codec_list, list) { 1277f0fba2adSLiam Girdwood if (!strcmp(codec->name, dai_link->codec_name)) { 1278f0fba2adSLiam Girdwood rtd->codec = codec; 1279f0fba2adSLiam Girdwood 1280f0fba2adSLiam Girdwood if (!try_module_get(codec->dev->driver->owner)) 1281f0fba2adSLiam Girdwood return -ENODEV; 1282f0fba2adSLiam Girdwood 1283f0fba2adSLiam Girdwood /* CODEC found, so find CODEC DAI from registered DAIs from this CODEC*/ 1284f0fba2adSLiam Girdwood list_for_each_entry(codec_dai, &dai_list, list) { 1285f0fba2adSLiam Girdwood if (codec->dev == codec_dai->dev && 1286f0fba2adSLiam Girdwood !strcmp(codec_dai->name, dai_link->codec_dai_name)) { 1287f0fba2adSLiam Girdwood rtd->codec_dai = codec_dai; 1288f0fba2adSLiam Girdwood goto find_platform; 1289435c5e25SMark Brown } 1290f0fba2adSLiam Girdwood } 1291f0fba2adSLiam Girdwood dev_dbg(card->dev, "CODEC DAI %s not registered\n", 1292f0fba2adSLiam Girdwood dai_link->codec_dai_name); 1293f0fba2adSLiam Girdwood 1294f0fba2adSLiam Girdwood goto find_platform; 1295f0fba2adSLiam Girdwood } 1296f0fba2adSLiam Girdwood } 1297f0fba2adSLiam Girdwood dev_dbg(card->dev, "CODEC %s not registered\n", 1298f0fba2adSLiam Girdwood dai_link->codec_name); 1299f0fba2adSLiam Girdwood 1300f0fba2adSLiam Girdwood find_platform: 1301f0fba2adSLiam Girdwood /* do we already have the CODEC DAI for this link ? */ 1302f0fba2adSLiam Girdwood if (rtd->platform) { 1303f0fba2adSLiam Girdwood goto out; 1304f0fba2adSLiam Girdwood } 1305f0fba2adSLiam Girdwood /* no, then find CPU DAI from registered DAIs*/ 1306f0fba2adSLiam Girdwood list_for_each_entry(platform, &platform_list, list) { 1307f0fba2adSLiam Girdwood if (!strcmp(platform->name, dai_link->platform_name)) { 1308f0fba2adSLiam Girdwood 1309f0fba2adSLiam Girdwood if (!try_module_get(platform->dev->driver->owner)) 1310f0fba2adSLiam Girdwood return -ENODEV; 1311f0fba2adSLiam Girdwood 1312f0fba2adSLiam Girdwood rtd->platform = platform; 1313f0fba2adSLiam Girdwood goto out; 1314f0fba2adSLiam Girdwood } 1315435c5e25SMark Brown } 13166b05eda6SMark Brown 1317f0fba2adSLiam Girdwood dev_dbg(card->dev, "platform %s not registered\n", 1318f0fba2adSLiam Girdwood dai_link->platform_name); 1319f0fba2adSLiam Girdwood return 0; 1320f0fba2adSLiam Girdwood 1321f0fba2adSLiam Girdwood out: 1322f0fba2adSLiam Girdwood /* mark rtd as complete if we found all 4 of our client devices */ 1323f0fba2adSLiam Girdwood if (rtd->codec && rtd->codec_dai && rtd->platform && rtd->cpu_dai) { 1324f0fba2adSLiam Girdwood rtd->complete = 1; 1325f0fba2adSLiam Girdwood card->num_rtd++; 1326f0fba2adSLiam Girdwood } 1327f0fba2adSLiam Girdwood return 1; 13286b05eda6SMark Brown } 13296b05eda6SMark Brown 1330f0fba2adSLiam Girdwood static void soc_remove_dai_link(struct snd_soc_card *card, int num) 1331f0fba2adSLiam Girdwood { 1332f0fba2adSLiam Girdwood struct snd_soc_pcm_runtime *rtd = &card->rtd[num]; 1333f0fba2adSLiam Girdwood struct snd_soc_codec *codec = rtd->codec; 1334f0fba2adSLiam Girdwood struct snd_soc_platform *platform = rtd->platform; 1335f0fba2adSLiam Girdwood struct snd_soc_dai *codec_dai = rtd->codec_dai, *cpu_dai = rtd->cpu_dai; 1336f0fba2adSLiam Girdwood int err; 1337f0fba2adSLiam Girdwood 1338f0fba2adSLiam Girdwood /* unregister the rtd device */ 1339f0fba2adSLiam Girdwood if (rtd->dev_registered) { 1340f0fba2adSLiam Girdwood device_remove_file(&rtd->dev, &dev_attr_pmdown_time); 1341f0fba2adSLiam Girdwood device_unregister(&rtd->dev); 1342f0fba2adSLiam Girdwood rtd->dev_registered = 0; 134302a06d30SBarry Song } 134402a06d30SBarry Song 1345f0fba2adSLiam Girdwood /* remove the CODEC DAI */ 1346f0fba2adSLiam Girdwood if (codec_dai && codec_dai->probed) { 1347f0fba2adSLiam Girdwood if (codec_dai->driver->remove) { 1348f0fba2adSLiam Girdwood err = codec_dai->driver->remove(codec_dai); 1349f0fba2adSLiam Girdwood if (err < 0) 1350f0fba2adSLiam Girdwood printk(KERN_ERR "asoc: failed to remove %s\n", codec_dai->name); 1351f0fba2adSLiam Girdwood } 1352f0fba2adSLiam Girdwood codec_dai->probed = 0; 1353f0fba2adSLiam Girdwood list_del(&codec_dai->card_list); 1354f0fba2adSLiam Girdwood } 1355f0fba2adSLiam Girdwood 1356f0fba2adSLiam Girdwood /* remove the platform */ 1357f0fba2adSLiam Girdwood if (platform && platform->probed) { 1358f0fba2adSLiam Girdwood if (platform->driver->remove) { 1359f0fba2adSLiam Girdwood err = platform->driver->remove(platform); 1360f0fba2adSLiam Girdwood if (err < 0) 1361f0fba2adSLiam Girdwood printk(KERN_ERR "asoc: failed to remove %s\n", platform->name); 1362f0fba2adSLiam Girdwood } 1363f0fba2adSLiam Girdwood platform->probed = 0; 1364f0fba2adSLiam Girdwood list_del(&platform->card_list); 1365f0fba2adSLiam Girdwood module_put(platform->dev->driver->owner); 1366f0fba2adSLiam Girdwood } 1367f0fba2adSLiam Girdwood 1368f0fba2adSLiam Girdwood /* remove the CODEC */ 1369f0fba2adSLiam Girdwood if (codec && codec->probed) { 1370f0fba2adSLiam Girdwood if (codec->driver->remove) { 1371f0fba2adSLiam Girdwood err = codec->driver->remove(codec); 1372f0fba2adSLiam Girdwood if (err < 0) 1373f0fba2adSLiam Girdwood printk(KERN_ERR "asoc: failed to remove %s\n", codec->name); 1374f0fba2adSLiam Girdwood } 1375f0fba2adSLiam Girdwood 1376f0fba2adSLiam Girdwood /* Make sure all DAPM widgets are freed */ 1377ce6120ccSLiam Girdwood snd_soc_dapm_free(&codec->dapm); 1378f0fba2adSLiam Girdwood 1379f0fba2adSLiam Girdwood soc_cleanup_codec_debugfs(codec); 1380f0fba2adSLiam Girdwood device_remove_file(&rtd->dev, &dev_attr_codec_reg); 1381f0fba2adSLiam Girdwood codec->probed = 0; 1382f0fba2adSLiam Girdwood list_del(&codec->card_list); 1383f0fba2adSLiam Girdwood module_put(codec->dev->driver->owner); 1384f0fba2adSLiam Girdwood } 1385f0fba2adSLiam Girdwood 1386f0fba2adSLiam Girdwood /* remove the cpu_dai */ 1387f0fba2adSLiam Girdwood if (cpu_dai && cpu_dai->probed) { 1388f0fba2adSLiam Girdwood if (cpu_dai->driver->remove) { 1389f0fba2adSLiam Girdwood err = cpu_dai->driver->remove(cpu_dai); 1390f0fba2adSLiam Girdwood if (err < 0) 1391f0fba2adSLiam Girdwood printk(KERN_ERR "asoc: failed to remove %s\n", cpu_dai->name); 1392f0fba2adSLiam Girdwood } 1393f0fba2adSLiam Girdwood cpu_dai->probed = 0; 1394f0fba2adSLiam Girdwood list_del(&cpu_dai->card_list); 1395f0fba2adSLiam Girdwood module_put(cpu_dai->dev->driver->owner); 1396f0fba2adSLiam Girdwood } 1397f0fba2adSLiam Girdwood } 1398f0fba2adSLiam Girdwood 1399ead9b919SJarkko Nikula static void soc_set_name_prefix(struct snd_soc_card *card, 1400ead9b919SJarkko Nikula struct snd_soc_codec *codec) 1401ead9b919SJarkko Nikula { 1402ead9b919SJarkko Nikula int i; 1403ead9b919SJarkko Nikula 1404ead9b919SJarkko Nikula if (card->prefix_map == NULL) 1405ead9b919SJarkko Nikula return; 1406ead9b919SJarkko Nikula 1407ead9b919SJarkko Nikula for (i = 0; i < card->num_prefixes; i++) { 1408ead9b919SJarkko Nikula struct snd_soc_prefix_map *map = &card->prefix_map[i]; 1409ead9b919SJarkko Nikula if (map->dev_name && !strcmp(codec->name, map->dev_name)) { 1410ead9b919SJarkko Nikula codec->name_prefix = map->name_prefix; 1411ead9b919SJarkko Nikula break; 1412ead9b919SJarkko Nikula } 1413ead9b919SJarkko Nikula } 1414ead9b919SJarkko Nikula } 1415ead9b919SJarkko Nikula 1416f0fba2adSLiam Girdwood static void rtd_release(struct device *dev) {} 1417f0fba2adSLiam Girdwood 1418f0fba2adSLiam Girdwood static int soc_probe_dai_link(struct snd_soc_card *card, int num) 1419f0fba2adSLiam Girdwood { 1420f0fba2adSLiam Girdwood struct snd_soc_dai_link *dai_link = &card->dai_link[num]; 1421f0fba2adSLiam Girdwood struct snd_soc_pcm_runtime *rtd = &card->rtd[num]; 1422f0fba2adSLiam Girdwood struct snd_soc_codec *codec = rtd->codec; 1423f0fba2adSLiam Girdwood struct snd_soc_platform *platform = rtd->platform; 1424f0fba2adSLiam Girdwood struct snd_soc_dai *codec_dai = rtd->codec_dai, *cpu_dai = rtd->cpu_dai; 1425ead9b919SJarkko Nikula const char *temp; 1426f0fba2adSLiam Girdwood int ret; 1427f0fba2adSLiam Girdwood 1428f0fba2adSLiam Girdwood dev_dbg(card->dev, "probe %s dai link %d\n", card->name, num); 1429f0fba2adSLiam Girdwood 1430f0fba2adSLiam Girdwood /* config components */ 1431f0fba2adSLiam Girdwood codec_dai->codec = codec; 1432f0fba2adSLiam Girdwood codec->card = card; 1433f0fba2adSLiam Girdwood cpu_dai->platform = platform; 1434f0fba2adSLiam Girdwood rtd->card = card; 1435f0fba2adSLiam Girdwood rtd->dev.parent = card->dev; 1436f0fba2adSLiam Girdwood codec_dai->card = card; 1437f0fba2adSLiam Girdwood cpu_dai->card = card; 1438f0fba2adSLiam Girdwood 1439f0fba2adSLiam Girdwood /* set default power off timeout */ 1440f0fba2adSLiam Girdwood rtd->pmdown_time = pmdown_time; 1441f0fba2adSLiam Girdwood 1442f0fba2adSLiam Girdwood /* probe the cpu_dai */ 1443f0fba2adSLiam Girdwood if (!cpu_dai->probed) { 1444f0fba2adSLiam Girdwood if (cpu_dai->driver->probe) { 1445f0fba2adSLiam Girdwood ret = cpu_dai->driver->probe(cpu_dai); 1446f0fba2adSLiam Girdwood if (ret < 0) { 1447f0fba2adSLiam Girdwood printk(KERN_ERR "asoc: failed to probe CPU DAI %s\n", 1448f0fba2adSLiam Girdwood cpu_dai->name); 1449f0fba2adSLiam Girdwood return ret; 1450f0fba2adSLiam Girdwood } 1451f0fba2adSLiam Girdwood } 1452f0fba2adSLiam Girdwood cpu_dai->probed = 1; 1453f0fba2adSLiam Girdwood /* mark cpu_dai as probed and add to card cpu_dai list */ 1454f0fba2adSLiam Girdwood list_add(&cpu_dai->card_list, &card->dai_dev_list); 1455f0fba2adSLiam Girdwood } 1456f0fba2adSLiam Girdwood 1457f0fba2adSLiam Girdwood /* probe the CODEC */ 1458f0fba2adSLiam Girdwood if (!codec->probed) { 14593a45b867SJarkko Nikula codec->dapm.card = card; 1460ead9b919SJarkko Nikula soc_set_name_prefix(card, codec); 1461f0fba2adSLiam Girdwood if (codec->driver->probe) { 1462f0fba2adSLiam Girdwood ret = codec->driver->probe(codec); 1463f0fba2adSLiam Girdwood if (ret < 0) { 1464f0fba2adSLiam Girdwood printk(KERN_ERR "asoc: failed to probe CODEC %s\n", 1465f0fba2adSLiam Girdwood codec->name); 1466f0fba2adSLiam Girdwood return ret; 1467f0fba2adSLiam Girdwood } 1468f0fba2adSLiam Girdwood } 146913cb61f8SMark Brown 147013cb61f8SMark Brown soc_init_codec_debugfs(codec); 147113cb61f8SMark Brown 1472f0fba2adSLiam Girdwood /* mark codec as probed and add to card codec list */ 1473f0fba2adSLiam Girdwood codec->probed = 1; 1474f0fba2adSLiam Girdwood list_add(&codec->card_list, &card->codec_dev_list); 1475f0fba2adSLiam Girdwood } 1476f0fba2adSLiam Girdwood 1477f0fba2adSLiam Girdwood /* probe the platform */ 1478f0fba2adSLiam Girdwood if (!platform->probed) { 1479f0fba2adSLiam Girdwood if (platform->driver->probe) { 1480f0fba2adSLiam Girdwood ret = platform->driver->probe(platform); 1481f0fba2adSLiam Girdwood if (ret < 0) { 1482f0fba2adSLiam Girdwood printk(KERN_ERR "asoc: failed to probe platform %s\n", 1483f0fba2adSLiam Girdwood platform->name); 1484f0fba2adSLiam Girdwood return ret; 1485f0fba2adSLiam Girdwood } 1486f0fba2adSLiam Girdwood } 1487f0fba2adSLiam Girdwood /* mark platform as probed and add to card platform list */ 1488f0fba2adSLiam Girdwood platform->probed = 1; 1489f0fba2adSLiam Girdwood list_add(&platform->card_list, &card->platform_dev_list); 1490f0fba2adSLiam Girdwood } 1491f0fba2adSLiam Girdwood 1492f0fba2adSLiam Girdwood /* probe the CODEC DAI */ 1493f0fba2adSLiam Girdwood if (!codec_dai->probed) { 1494f0fba2adSLiam Girdwood if (codec_dai->driver->probe) { 1495f0fba2adSLiam Girdwood ret = codec_dai->driver->probe(codec_dai); 1496f0fba2adSLiam Girdwood if (ret < 0) { 1497f0fba2adSLiam Girdwood printk(KERN_ERR "asoc: failed to probe CODEC DAI %s\n", 1498f0fba2adSLiam Girdwood codec_dai->name); 1499f0fba2adSLiam Girdwood return ret; 1500f0fba2adSLiam Girdwood } 1501f0fba2adSLiam Girdwood } 1502f0fba2adSLiam Girdwood 1503f0fba2adSLiam Girdwood /* mark cpu_dai as probed and add to card cpu_dai list */ 1504f0fba2adSLiam Girdwood codec_dai->probed = 1; 1505f0fba2adSLiam Girdwood list_add(&codec_dai->card_list, &card->dai_dev_list); 1506f0fba2adSLiam Girdwood } 1507f0fba2adSLiam Girdwood 1508f0fba2adSLiam Girdwood /* DAPM dai link stream work */ 1509f0fba2adSLiam Girdwood INIT_DELAYED_WORK(&rtd->delayed_work, close_delayed_work); 1510f0fba2adSLiam Girdwood 1511f0fba2adSLiam Girdwood /* now that all clients have probed, initialise the DAI link */ 1512f0fba2adSLiam Girdwood if (dai_link->init) { 1513ead9b919SJarkko Nikula /* machine controls, routes and widgets are not prefixed */ 1514ead9b919SJarkko Nikula temp = rtd->codec->name_prefix; 1515ead9b919SJarkko Nikula rtd->codec->name_prefix = NULL; 1516f0fba2adSLiam Girdwood ret = dai_link->init(rtd); 1517f0fba2adSLiam Girdwood if (ret < 0) { 1518f0fba2adSLiam Girdwood printk(KERN_ERR "asoc: failed to init %s\n", dai_link->stream_name); 1519f0fba2adSLiam Girdwood return ret; 1520f0fba2adSLiam Girdwood } 1521ead9b919SJarkko Nikula rtd->codec->name_prefix = temp; 1522f0fba2adSLiam Girdwood } 1523f0fba2adSLiam Girdwood 1524f0fba2adSLiam Girdwood /* Make sure all DAPM widgets are instantiated */ 1525ce6120ccSLiam Girdwood snd_soc_dapm_new_widgets(&codec->dapm); 1526ce6120ccSLiam Girdwood snd_soc_dapm_sync(&codec->dapm); 1527f0fba2adSLiam Girdwood 1528f0fba2adSLiam Girdwood /* register the rtd device */ 1529f0fba2adSLiam Girdwood rtd->dev.release = rtd_release; 1530f0fba2adSLiam Girdwood rtd->dev.init_name = dai_link->name; 1531f0fba2adSLiam Girdwood ret = device_register(&rtd->dev); 1532f0fba2adSLiam Girdwood if (ret < 0) { 1533f0fba2adSLiam Girdwood printk(KERN_ERR "asoc: failed to register DAI runtime device %d\n", ret); 1534f0fba2adSLiam Girdwood return ret; 1535f0fba2adSLiam Girdwood } 1536f0fba2adSLiam Girdwood 1537f0fba2adSLiam Girdwood rtd->dev_registered = 1; 1538f0fba2adSLiam Girdwood ret = device_create_file(&rtd->dev, &dev_attr_pmdown_time); 1539f0fba2adSLiam Girdwood if (ret < 0) 1540f0fba2adSLiam Girdwood printk(KERN_WARNING "asoc: failed to add pmdown_time sysfs\n"); 1541f0fba2adSLiam Girdwood 1542f0fba2adSLiam Girdwood /* add DAPM sysfs entries for this codec */ 1543f0fba2adSLiam Girdwood ret = snd_soc_dapm_sys_add(&rtd->dev); 1544f0fba2adSLiam Girdwood if (ret < 0) 1545f0fba2adSLiam Girdwood printk(KERN_WARNING "asoc: failed to add codec dapm sysfs entries\n"); 1546f0fba2adSLiam Girdwood 1547f0fba2adSLiam Girdwood /* add codec sysfs entries */ 1548f0fba2adSLiam Girdwood ret = device_create_file(&rtd->dev, &dev_attr_codec_reg); 1549f0fba2adSLiam Girdwood if (ret < 0) 1550f0fba2adSLiam Girdwood printk(KERN_WARNING "asoc: failed to add codec sysfs files\n"); 1551f0fba2adSLiam Girdwood 1552f0fba2adSLiam Girdwood /* create the pcm */ 1553f0fba2adSLiam Girdwood ret = soc_new_pcm(rtd, num); 1554f0fba2adSLiam Girdwood if (ret < 0) { 1555f0fba2adSLiam Girdwood printk(KERN_ERR "asoc: can't create pcm %s\n", dai_link->stream_name); 1556f0fba2adSLiam Girdwood return ret; 1557f0fba2adSLiam Girdwood } 1558f0fba2adSLiam Girdwood 1559f0fba2adSLiam Girdwood /* add platform data for AC97 devices */ 1560f0fba2adSLiam Girdwood if (rtd->codec_dai->driver->ac97_control) 1561f0fba2adSLiam Girdwood snd_ac97_dev_add_pdata(codec->ac97, rtd->cpu_dai->ac97_pdata); 1562f0fba2adSLiam Girdwood 1563f0fba2adSLiam Girdwood return 0; 1564f0fba2adSLiam Girdwood } 1565f0fba2adSLiam Girdwood 1566f0fba2adSLiam Girdwood #ifdef CONFIG_SND_SOC_AC97_BUS 1567f0fba2adSLiam Girdwood static int soc_register_ac97_dai_link(struct snd_soc_pcm_runtime *rtd) 1568f0fba2adSLiam Girdwood { 1569f0fba2adSLiam Girdwood int ret; 1570f0fba2adSLiam Girdwood 1571f0fba2adSLiam Girdwood /* Only instantiate AC97 if not already done by the adaptor 1572f0fba2adSLiam Girdwood * for the generic AC97 subsystem. 15736b05eda6SMark Brown */ 1574f0fba2adSLiam Girdwood if (rtd->codec_dai->driver->ac97_control && !rtd->codec->ac97_registered) { 15750562f788SMika Westerberg /* 15760562f788SMika Westerberg * It is possible that the AC97 device is already registered to 15770562f788SMika Westerberg * the device subsystem. This happens when the device is created 15780562f788SMika Westerberg * via snd_ac97_mixer(). Currently only SoC codec that does so 15790562f788SMika Westerberg * is the generic AC97 glue but others migh emerge. 15800562f788SMika Westerberg * 15810562f788SMika Westerberg * In those cases we don't try to register the device again. 15820562f788SMika Westerberg */ 15830562f788SMika Westerberg if (!rtd->codec->ac97_created) 15840562f788SMika Westerberg return 0; 1585f0fba2adSLiam Girdwood 1586f0fba2adSLiam Girdwood ret = soc_ac97_dev_register(rtd->codec); 1587f0fba2adSLiam Girdwood if (ret < 0) { 1588f0fba2adSLiam Girdwood printk(KERN_ERR "asoc: AC97 device register failed\n"); 1589f0fba2adSLiam Girdwood return ret; 1590435c5e25SMark Brown } 1591435c5e25SMark Brown 1592f0fba2adSLiam Girdwood rtd->codec->ac97_registered = 1; 1593f0fba2adSLiam Girdwood } 1594f0fba2adSLiam Girdwood return 0; 1595f0fba2adSLiam Girdwood } 1596435c5e25SMark Brown 1597f0fba2adSLiam Girdwood static void soc_unregister_ac97_dai_link(struct snd_soc_codec *codec) 1598f0fba2adSLiam Girdwood { 1599f0fba2adSLiam Girdwood if (codec->ac97_registered) { 1600f0fba2adSLiam Girdwood soc_ac97_dev_unregister(codec); 1601f0fba2adSLiam Girdwood codec->ac97_registered = 0; 1602f0fba2adSLiam Girdwood } 1603f0fba2adSLiam Girdwood } 1604f0fba2adSLiam Girdwood #endif 1605435c5e25SMark Brown 16062eea392dSJarkko Nikula static int soc_probe_aux_dev(struct snd_soc_card *card, int num) 16072eea392dSJarkko Nikula { 16082eea392dSJarkko Nikula struct snd_soc_aux_dev *aux_dev = &card->aux_dev[num]; 16092eea392dSJarkko Nikula struct snd_soc_pcm_runtime *rtd = &card->rtd_aux[num]; 16102eea392dSJarkko Nikula struct snd_soc_codec *codec; 16112eea392dSJarkko Nikula const char *temp; 1612676ad98aSJarkko Nikula int ret = -ENODEV; 16132eea392dSJarkko Nikula 16142eea392dSJarkko Nikula /* find CODEC from registered CODECs*/ 16152eea392dSJarkko Nikula list_for_each_entry(codec, &codec_list, list) { 16162eea392dSJarkko Nikula if (!strcmp(codec->name, aux_dev->codec_name)) { 16172eea392dSJarkko Nikula if (codec->probed) { 16182eea392dSJarkko Nikula dev_err(codec->dev, 16192eea392dSJarkko Nikula "asoc: codec already probed"); 16202eea392dSJarkko Nikula ret = -EBUSY; 16212eea392dSJarkko Nikula goto out; 16222eea392dSJarkko Nikula } 1623676ad98aSJarkko Nikula goto found; 16242eea392dSJarkko Nikula } 16252eea392dSJarkko Nikula } 1626676ad98aSJarkko Nikula /* codec not found */ 1627676ad98aSJarkko Nikula dev_err(card->dev, "asoc: codec %s not found", aux_dev->codec_name); 1628676ad98aSJarkko Nikula goto out; 16292eea392dSJarkko Nikula 1630676ad98aSJarkko Nikula found: 16312eea392dSJarkko Nikula if (!try_module_get(codec->dev->driver->owner)) 16322eea392dSJarkko Nikula return -ENODEV; 16332eea392dSJarkko Nikula 16342eea392dSJarkko Nikula codec->card = card; 16352eea392dSJarkko Nikula codec->dapm.card = card; 16362eea392dSJarkko Nikula 16372eea392dSJarkko Nikula soc_set_name_prefix(card, codec); 16382eea392dSJarkko Nikula if (codec->driver->probe) { 16392eea392dSJarkko Nikula ret = codec->driver->probe(codec); 16402eea392dSJarkko Nikula if (ret < 0) { 16412eea392dSJarkko Nikula dev_err(codec->dev, "asoc: failed to probe CODEC"); 16422eea392dSJarkko Nikula return ret; 16432eea392dSJarkko Nikula } 16442eea392dSJarkko Nikula } 16452eea392dSJarkko Nikula 16462eea392dSJarkko Nikula soc_init_codec_debugfs(codec); 16472eea392dSJarkko Nikula 16482eea392dSJarkko Nikula /* mark codec as probed and add to card codec list */ 16492eea392dSJarkko Nikula codec->probed = 1; 16502eea392dSJarkko Nikula list_add(&codec->card_list, &card->codec_dev_list); 16512eea392dSJarkko Nikula 16522eea392dSJarkko Nikula /* now that all clients have probed, initialise the DAI link */ 16532eea392dSJarkko Nikula if (aux_dev->init) { 16542eea392dSJarkko Nikula /* machine controls, routes and widgets are not prefixed */ 16552eea392dSJarkko Nikula temp = codec->name_prefix; 16562eea392dSJarkko Nikula codec->name_prefix = NULL; 16572eea392dSJarkko Nikula ret = aux_dev->init(&codec->dapm); 16582eea392dSJarkko Nikula if (ret < 0) { 16592eea392dSJarkko Nikula dev_err(codec->dev, 16602eea392dSJarkko Nikula "asoc: failed to init %s\n", aux_dev->name); 16612eea392dSJarkko Nikula return ret; 16622eea392dSJarkko Nikula } 16632eea392dSJarkko Nikula codec->name_prefix = temp; 16642eea392dSJarkko Nikula } 16652eea392dSJarkko Nikula 16662eea392dSJarkko Nikula /* Make sure all DAPM widgets are instantiated */ 16672eea392dSJarkko Nikula snd_soc_dapm_new_widgets(&codec->dapm); 16682eea392dSJarkko Nikula snd_soc_dapm_sync(&codec->dapm); 16692eea392dSJarkko Nikula 16702eea392dSJarkko Nikula /* register the rtd device */ 16712eea392dSJarkko Nikula rtd->codec = codec; 16722eea392dSJarkko Nikula rtd->card = card; 16732eea392dSJarkko Nikula rtd->dev.parent = card->dev; 16742eea392dSJarkko Nikula rtd->dev.release = rtd_release; 16752eea392dSJarkko Nikula rtd->dev.init_name = aux_dev->name; 16762eea392dSJarkko Nikula ret = device_register(&rtd->dev); 16772eea392dSJarkko Nikula if (ret < 0) { 16782eea392dSJarkko Nikula dev_err(codec->dev, 16792eea392dSJarkko Nikula "asoc: failed to register aux runtime device %d\n", 16802eea392dSJarkko Nikula ret); 16812eea392dSJarkko Nikula return ret; 16822eea392dSJarkko Nikula } 16832eea392dSJarkko Nikula rtd->dev_registered = 1; 16842eea392dSJarkko Nikula 16852eea392dSJarkko Nikula /* add DAPM sysfs entries for this codec */ 16862eea392dSJarkko Nikula ret = snd_soc_dapm_sys_add(&rtd->dev); 16872eea392dSJarkko Nikula if (ret < 0) 16882eea392dSJarkko Nikula dev_err(codec->dev, 16892eea392dSJarkko Nikula "asoc: failed to add codec dapm sysfs entries\n"); 16902eea392dSJarkko Nikula 16912eea392dSJarkko Nikula /* add codec sysfs entries */ 16922eea392dSJarkko Nikula ret = device_create_file(&rtd->dev, &dev_attr_codec_reg); 16932eea392dSJarkko Nikula if (ret < 0) 16942eea392dSJarkko Nikula dev_err(codec->dev, "asoc: failed to add codec sysfs files\n"); 16952eea392dSJarkko Nikula 16962eea392dSJarkko Nikula out: 16972eea392dSJarkko Nikula return ret; 16982eea392dSJarkko Nikula } 16992eea392dSJarkko Nikula 17002eea392dSJarkko Nikula static void soc_remove_aux_dev(struct snd_soc_card *card, int num) 17012eea392dSJarkko Nikula { 17022eea392dSJarkko Nikula struct snd_soc_pcm_runtime *rtd = &card->rtd_aux[num]; 17032eea392dSJarkko Nikula struct snd_soc_codec *codec = rtd->codec; 17042eea392dSJarkko Nikula int err; 17052eea392dSJarkko Nikula 17062eea392dSJarkko Nikula /* unregister the rtd device */ 17072eea392dSJarkko Nikula if (rtd->dev_registered) { 17082eea392dSJarkko Nikula device_unregister(&rtd->dev); 17092eea392dSJarkko Nikula rtd->dev_registered = 0; 17102eea392dSJarkko Nikula } 17112eea392dSJarkko Nikula 17122eea392dSJarkko Nikula /* remove the CODEC */ 17132eea392dSJarkko Nikula if (codec && codec->probed) { 17142eea392dSJarkko Nikula if (codec->driver->remove) { 17152eea392dSJarkko Nikula err = codec->driver->remove(codec); 17162eea392dSJarkko Nikula if (err < 0) 17172eea392dSJarkko Nikula dev_err(codec->dev, 17182eea392dSJarkko Nikula "asoc: failed to remove %s\n", 17192eea392dSJarkko Nikula codec->name); 17202eea392dSJarkko Nikula } 17212eea392dSJarkko Nikula 17222eea392dSJarkko Nikula /* Make sure all DAPM widgets are freed */ 17232eea392dSJarkko Nikula snd_soc_dapm_free(&codec->dapm); 17242eea392dSJarkko Nikula 17252eea392dSJarkko Nikula soc_cleanup_codec_debugfs(codec); 17262eea392dSJarkko Nikula device_remove_file(&rtd->dev, &dev_attr_codec_reg); 17272eea392dSJarkko Nikula codec->probed = 0; 17282eea392dSJarkko Nikula list_del(&codec->card_list); 17292eea392dSJarkko Nikula module_put(codec->dev->driver->owner); 17302eea392dSJarkko Nikula } 17312eea392dSJarkko Nikula } 17322eea392dSJarkko Nikula 1733f0fba2adSLiam Girdwood static void snd_soc_instantiate_card(struct snd_soc_card *card) 1734f0fba2adSLiam Girdwood { 1735f0fba2adSLiam Girdwood struct platform_device *pdev = to_platform_device(card->dev); 1736f0fba2adSLiam Girdwood int ret, i; 173796dd3622SMark Brown 1738f0fba2adSLiam Girdwood mutex_lock(&card->mutex); 1739f0fba2adSLiam Girdwood 1740f0fba2adSLiam Girdwood if (card->instantiated) { 1741f0fba2adSLiam Girdwood mutex_unlock(&card->mutex); 1742435c5e25SMark Brown return; 1743db2a4165SFrank Mandarino } 1744db2a4165SFrank Mandarino 1745f0fba2adSLiam Girdwood /* bind DAIs */ 1746f0fba2adSLiam Girdwood for (i = 0; i < card->num_links; i++) 1747f0fba2adSLiam Girdwood soc_bind_dai_link(card, i); 1748f0fba2adSLiam Girdwood 1749f0fba2adSLiam Girdwood /* bind completed ? */ 1750f0fba2adSLiam Girdwood if (card->num_rtd != card->num_links) { 1751f0fba2adSLiam Girdwood mutex_unlock(&card->mutex); 1752f0fba2adSLiam Girdwood return; 1753db2a4165SFrank Mandarino } 1754db2a4165SFrank Mandarino 1755f0fba2adSLiam Girdwood /* card bind complete so register a sound card */ 1756f0fba2adSLiam Girdwood ret = snd_card_create(SNDRV_DEFAULT_IDX1, SNDRV_DEFAULT_STR1, 1757f0fba2adSLiam Girdwood card->owner, 0, &card->snd_card); 1758f0fba2adSLiam Girdwood if (ret < 0) { 1759f0fba2adSLiam Girdwood printk(KERN_ERR "asoc: can't create sound card for card %s\n", 1760f0fba2adSLiam Girdwood card->name); 1761f0fba2adSLiam Girdwood mutex_unlock(&card->mutex); 1762f0fba2adSLiam Girdwood return; 1763db2a4165SFrank Mandarino } 1764f0fba2adSLiam Girdwood card->snd_card->dev = card->dev; 1765db2a4165SFrank Mandarino 17661301a964SRandy Dunlap #ifdef CONFIG_PM 17676ed25978SAndy Green /* deferred resume work */ 17686308419aSMark Brown INIT_WORK(&card->deferred_resume_work, soc_resume_deferred); 17691301a964SRandy Dunlap #endif 17706ed25978SAndy Green 1771f0fba2adSLiam Girdwood /* initialise the sound card only once */ 1772f0fba2adSLiam Girdwood if (card->probe) { 1773f0fba2adSLiam Girdwood ret = card->probe(pdev); 1774f0fba2adSLiam Girdwood if (ret < 0) 1775f0fba2adSLiam Girdwood goto card_probe_error; 1776f0fba2adSLiam Girdwood } 1777f0fba2adSLiam Girdwood 1778fe3e78e0SMark Brown for (i = 0; i < card->num_links; i++) { 1779f0fba2adSLiam Girdwood ret = soc_probe_dai_link(card, i); 1780fe3e78e0SMark Brown if (ret < 0) { 1781321de0d0SMark Brown pr_err("asoc: failed to instantiate card %s: %d\n", 1782321de0d0SMark Brown card->name, ret); 1783f0fba2adSLiam Girdwood goto probe_dai_err; 1784fe3e78e0SMark Brown } 1785fe3e78e0SMark Brown } 1786fe3e78e0SMark Brown 17872eea392dSJarkko Nikula for (i = 0; i < card->num_aux_devs; i++) { 17882eea392dSJarkko Nikula ret = soc_probe_aux_dev(card, i); 17892eea392dSJarkko Nikula if (ret < 0) { 17902eea392dSJarkko Nikula pr_err("asoc: failed to add auxiliary devices %s: %d\n", 17912eea392dSJarkko Nikula card->name, ret); 17922eea392dSJarkko Nikula goto probe_aux_dev_err; 17932eea392dSJarkko Nikula } 17942eea392dSJarkko Nikula } 17952eea392dSJarkko Nikula 1796f0fba2adSLiam Girdwood snprintf(card->snd_card->shortname, sizeof(card->snd_card->shortname), 1797fe3e78e0SMark Brown "%s", card->name); 1798f0fba2adSLiam Girdwood snprintf(card->snd_card->longname, sizeof(card->snd_card->longname), 1799f0fba2adSLiam Girdwood "%s", card->name); 1800fe3e78e0SMark Brown 1801f0fba2adSLiam Girdwood ret = snd_card_register(card->snd_card); 1802fe3e78e0SMark Brown if (ret < 0) { 1803f0fba2adSLiam Girdwood printk(KERN_ERR "asoc: failed to register soundcard for %s\n", card->name); 1804f0fba2adSLiam Girdwood goto probe_dai_err; 1805fe3e78e0SMark Brown } 1806fe3e78e0SMark Brown 1807fe3e78e0SMark Brown #ifdef CONFIG_SND_SOC_AC97_BUS 1808f0fba2adSLiam Girdwood /* register any AC97 codecs */ 1809f0fba2adSLiam Girdwood for (i = 0; i < card->num_rtd; i++) { 1810f0fba2adSLiam Girdwood ret = soc_register_ac97_dai_link(&card->rtd[i]); 1811fe3e78e0SMark Brown if (ret < 0) { 1812f0fba2adSLiam Girdwood printk(KERN_ERR "asoc: failed to register AC97 %s\n", card->name); 1813f0fba2adSLiam Girdwood goto probe_dai_err; 1814fe3e78e0SMark Brown } 1815fe3e78e0SMark Brown } 1816fe3e78e0SMark Brown #endif 1817fe3e78e0SMark Brown 1818435c5e25SMark Brown card->instantiated = 1; 1819f0fba2adSLiam Girdwood mutex_unlock(&card->mutex); 1820435c5e25SMark Brown return; 1821db2a4165SFrank Mandarino 18222eea392dSJarkko Nikula probe_aux_dev_err: 18232eea392dSJarkko Nikula for (i = 0; i < card->num_aux_devs; i++) 18242eea392dSJarkko Nikula soc_remove_aux_dev(card, i); 18252eea392dSJarkko Nikula 1826f0fba2adSLiam Girdwood probe_dai_err: 1827f0fba2adSLiam Girdwood for (i = 0; i < card->num_links; i++) 1828f0fba2adSLiam Girdwood soc_remove_dai_link(card, i); 1829fe3e78e0SMark Brown 1830f0fba2adSLiam Girdwood card_probe_error: 183187506549SMark Brown if (card->remove) 183287506549SMark Brown card->remove(pdev); 1833f0fba2adSLiam Girdwood 1834f0fba2adSLiam Girdwood snd_card_free(card->snd_card); 1835f0fba2adSLiam Girdwood 1836f0fba2adSLiam Girdwood mutex_unlock(&card->mutex); 1837435c5e25SMark Brown } 1838db2a4165SFrank Mandarino 1839435c5e25SMark Brown /* 1840421f91d2SUwe Kleine-König * Attempt to initialise any uninitialised cards. Must be called with 1841435c5e25SMark Brown * client_mutex. 1842435c5e25SMark Brown */ 1843435c5e25SMark Brown static void snd_soc_instantiate_cards(void) 1844435c5e25SMark Brown { 1845435c5e25SMark Brown struct snd_soc_card *card; 1846435c5e25SMark Brown list_for_each_entry(card, &card_list, list) 1847435c5e25SMark Brown snd_soc_instantiate_card(card); 1848435c5e25SMark Brown } 1849435c5e25SMark Brown 1850435c5e25SMark Brown /* probes a new socdev */ 1851435c5e25SMark Brown static int soc_probe(struct platform_device *pdev) 1852435c5e25SMark Brown { 1853f0fba2adSLiam Girdwood struct snd_soc_card *card = platform_get_drvdata(pdev); 1854435c5e25SMark Brown int ret = 0; 1855435c5e25SMark Brown 1856435c5e25SMark Brown /* Bodge while we unpick instantiation */ 1857435c5e25SMark Brown card->dev = &pdev->dev; 1858f0fba2adSLiam Girdwood INIT_LIST_HEAD(&card->dai_dev_list); 1859f0fba2adSLiam Girdwood INIT_LIST_HEAD(&card->codec_dev_list); 1860f0fba2adSLiam Girdwood INIT_LIST_HEAD(&card->platform_dev_list); 1861f0fba2adSLiam Girdwood 1862a6052154SJarkko Nikula soc_init_card_debugfs(card); 1863a6052154SJarkko Nikula 1864435c5e25SMark Brown ret = snd_soc_register_card(card); 1865435c5e25SMark Brown if (ret != 0) { 1866435c5e25SMark Brown dev_err(&pdev->dev, "Failed to register card\n"); 1867db2a4165SFrank Mandarino return ret; 1868db2a4165SFrank Mandarino } 1869db2a4165SFrank Mandarino 1870435c5e25SMark Brown return 0; 1871435c5e25SMark Brown } 1872435c5e25SMark Brown 1873db2a4165SFrank Mandarino /* removes a socdev */ 1874db2a4165SFrank Mandarino static int soc_remove(struct platform_device *pdev) 1875db2a4165SFrank Mandarino { 1876f0fba2adSLiam Girdwood struct snd_soc_card *card = platform_get_drvdata(pdev); 1877db2a4165SFrank Mandarino int i; 1878db2a4165SFrank Mandarino 1879b2dfa62cSGuennadi Liakhovetski if (card->instantiated) { 1880965ac42cSLiam Girdwood 1881f0fba2adSLiam Girdwood /* make sure any delayed work runs */ 1882f0fba2adSLiam Girdwood for (i = 0; i < card->num_rtd; i++) { 1883f0fba2adSLiam Girdwood struct snd_soc_pcm_runtime *rtd = &card->rtd[i]; 1884f0fba2adSLiam Girdwood run_delayed_work(&rtd->delayed_work); 1885db2a4165SFrank Mandarino } 1886db2a4165SFrank Mandarino 18872eea392dSJarkko Nikula /* remove auxiliary devices */ 18882eea392dSJarkko Nikula for (i = 0; i < card->num_aux_devs; i++) 18892eea392dSJarkko Nikula soc_remove_aux_dev(card, i); 18902eea392dSJarkko Nikula 1891f0fba2adSLiam Girdwood /* remove and free each DAI */ 1892f0fba2adSLiam Girdwood for (i = 0; i < card->num_rtd; i++) 1893f0fba2adSLiam Girdwood soc_remove_dai_link(card, i); 1894f0fba2adSLiam Girdwood 1895a6052154SJarkko Nikula soc_cleanup_card_debugfs(card); 1896a6052154SJarkko Nikula 1897f0fba2adSLiam Girdwood /* remove the card */ 189887506549SMark Brown if (card->remove) 189987506549SMark Brown card->remove(pdev); 1900f0fba2adSLiam Girdwood 1901f0fba2adSLiam Girdwood kfree(card->rtd); 1902f0fba2adSLiam Girdwood snd_card_free(card->snd_card); 1903b2dfa62cSGuennadi Liakhovetski } 1904c5af3a2eSMark Brown snd_soc_unregister_card(card); 1905db2a4165SFrank Mandarino return 0; 1906db2a4165SFrank Mandarino } 1907db2a4165SFrank Mandarino 1908416356fcSMark Brown static int soc_poweroff(struct device *dev) 190951737470SMark Brown { 1910416356fcSMark Brown struct platform_device *pdev = to_platform_device(dev); 1911f0fba2adSLiam Girdwood struct snd_soc_card *card = platform_get_drvdata(pdev); 1912f0fba2adSLiam Girdwood int i; 191351737470SMark Brown 191451737470SMark Brown if (!card->instantiated) 1915416356fcSMark Brown return 0; 191651737470SMark Brown 191751737470SMark Brown /* Flush out pmdown_time work - we actually do want to run it 191851737470SMark Brown * now, we're shutting down so no imminent restart. */ 1919f0fba2adSLiam Girdwood for (i = 0; i < card->num_rtd; i++) { 1920f0fba2adSLiam Girdwood struct snd_soc_pcm_runtime *rtd = &card->rtd[i]; 1921f0fba2adSLiam Girdwood run_delayed_work(&rtd->delayed_work); 1922f0fba2adSLiam Girdwood } 192351737470SMark Brown 1924f0fba2adSLiam Girdwood snd_soc_dapm_shutdown(card); 1925416356fcSMark Brown 1926416356fcSMark Brown return 0; 192751737470SMark Brown } 192851737470SMark Brown 192947145210SAlexey Dobriyan static const struct dev_pm_ops soc_pm_ops = { 1930416356fcSMark Brown .suspend = soc_suspend, 1931416356fcSMark Brown .resume = soc_resume, 1932416356fcSMark Brown .poweroff = soc_poweroff, 1933416356fcSMark Brown }; 1934416356fcSMark Brown 1935db2a4165SFrank Mandarino /* ASoC platform driver */ 1936db2a4165SFrank Mandarino static struct platform_driver soc_driver = { 1937db2a4165SFrank Mandarino .driver = { 1938db2a4165SFrank Mandarino .name = "soc-audio", 19398b45a209SKay Sievers .owner = THIS_MODULE, 1940416356fcSMark Brown .pm = &soc_pm_ops, 1941db2a4165SFrank Mandarino }, 1942db2a4165SFrank Mandarino .probe = soc_probe, 1943db2a4165SFrank Mandarino .remove = soc_remove, 1944db2a4165SFrank Mandarino }; 1945db2a4165SFrank Mandarino 1946db2a4165SFrank Mandarino /* create a new pcm */ 1947f0fba2adSLiam Girdwood static int soc_new_pcm(struct snd_soc_pcm_runtime *rtd, int num) 1948db2a4165SFrank Mandarino { 1949f0fba2adSLiam Girdwood struct snd_soc_codec *codec = rtd->codec; 1950f0fba2adSLiam Girdwood struct snd_soc_platform *platform = rtd->platform; 1951f0fba2adSLiam Girdwood struct snd_soc_dai *codec_dai = rtd->codec_dai; 1952f0fba2adSLiam Girdwood struct snd_soc_dai *cpu_dai = rtd->cpu_dai; 1953db2a4165SFrank Mandarino struct snd_pcm *pcm; 1954db2a4165SFrank Mandarino char new_name[64]; 1955db2a4165SFrank Mandarino int ret = 0, playback = 0, capture = 0; 1956db2a4165SFrank Mandarino 1957db2a4165SFrank Mandarino /* check client and interface hw capabilities */ 195840ca1142SMark Brown snprintf(new_name, sizeof(new_name), "%s %s-%d", 1959f0fba2adSLiam Girdwood rtd->dai_link->stream_name, codec_dai->name, num); 1960db2a4165SFrank Mandarino 1961f0fba2adSLiam Girdwood if (codec_dai->driver->playback.channels_min) 1962db2a4165SFrank Mandarino playback = 1; 1963f0fba2adSLiam Girdwood if (codec_dai->driver->capture.channels_min) 1964db2a4165SFrank Mandarino capture = 1; 1965db2a4165SFrank Mandarino 1966f0fba2adSLiam Girdwood dev_dbg(rtd->card->dev, "registered pcm #%d %s\n",num,new_name); 1967f0fba2adSLiam Girdwood ret = snd_pcm_new(rtd->card->snd_card, new_name, 1968f0fba2adSLiam Girdwood num, playback, capture, &pcm); 1969db2a4165SFrank Mandarino if (ret < 0) { 1970f0fba2adSLiam Girdwood printk(KERN_ERR "asoc: can't create pcm for codec %s\n", codec->name); 1971db2a4165SFrank Mandarino return ret; 1972db2a4165SFrank Mandarino } 1973db2a4165SFrank Mandarino 1974f0fba2adSLiam Girdwood rtd->pcm = pcm; 1975db2a4165SFrank Mandarino pcm->private_data = rtd; 1976f0fba2adSLiam Girdwood soc_pcm_ops.mmap = platform->driver->ops->mmap; 1977f0fba2adSLiam Girdwood soc_pcm_ops.pointer = platform->driver->ops->pointer; 1978f0fba2adSLiam Girdwood soc_pcm_ops.ioctl = platform->driver->ops->ioctl; 1979f0fba2adSLiam Girdwood soc_pcm_ops.copy = platform->driver->ops->copy; 1980f0fba2adSLiam Girdwood soc_pcm_ops.silence = platform->driver->ops->silence; 1981f0fba2adSLiam Girdwood soc_pcm_ops.ack = platform->driver->ops->ack; 1982f0fba2adSLiam Girdwood soc_pcm_ops.page = platform->driver->ops->page; 1983db2a4165SFrank Mandarino 1984db2a4165SFrank Mandarino if (playback) 1985db2a4165SFrank Mandarino snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &soc_pcm_ops); 1986db2a4165SFrank Mandarino 1987db2a4165SFrank Mandarino if (capture) 1988db2a4165SFrank Mandarino snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &soc_pcm_ops); 1989db2a4165SFrank Mandarino 1990f0fba2adSLiam Girdwood ret = platform->driver->pcm_new(rtd->card->snd_card, codec_dai, pcm); 1991db2a4165SFrank Mandarino if (ret < 0) { 1992db2a4165SFrank Mandarino printk(KERN_ERR "asoc: platform pcm constructor failed\n"); 1993db2a4165SFrank Mandarino return ret; 1994db2a4165SFrank Mandarino } 1995db2a4165SFrank Mandarino 1996f0fba2adSLiam Girdwood pcm->private_free = platform->driver->pcm_free; 1997db2a4165SFrank Mandarino printk(KERN_INFO "asoc: %s <-> %s mapping ok\n", codec_dai->name, 1998db2a4165SFrank Mandarino cpu_dai->name); 1999db2a4165SFrank Mandarino return ret; 2000db2a4165SFrank Mandarino } 2001db2a4165SFrank Mandarino 2002096e49d5SMark Brown /** 2003096e49d5SMark Brown * snd_soc_codec_volatile_register: Report if a register is volatile. 2004096e49d5SMark Brown * 2005096e49d5SMark Brown * @codec: CODEC to query. 2006096e49d5SMark Brown * @reg: Register to query. 2007096e49d5SMark Brown * 2008096e49d5SMark Brown * Boolean function indiciating if a CODEC register is volatile. 2009096e49d5SMark Brown */ 2010096e49d5SMark Brown int snd_soc_codec_volatile_register(struct snd_soc_codec *codec, int reg) 2011096e49d5SMark Brown { 2012f0fba2adSLiam Girdwood if (codec->driver->volatile_register) 2013f0fba2adSLiam Girdwood return codec->driver->volatile_register(reg); 2014096e49d5SMark Brown else 2015096e49d5SMark Brown return 0; 2016096e49d5SMark Brown } 2017096e49d5SMark Brown EXPORT_SYMBOL_GPL(snd_soc_codec_volatile_register); 2018096e49d5SMark Brown 2019db2a4165SFrank Mandarino /** 2020db2a4165SFrank Mandarino * snd_soc_new_ac97_codec - initailise AC97 device 2021db2a4165SFrank Mandarino * @codec: audio codec 2022db2a4165SFrank Mandarino * @ops: AC97 bus operations 2023db2a4165SFrank Mandarino * @num: AC97 codec number 2024db2a4165SFrank Mandarino * 2025db2a4165SFrank Mandarino * Initialises AC97 codec resources for use by ad-hoc devices only. 2026db2a4165SFrank Mandarino */ 2027db2a4165SFrank Mandarino int snd_soc_new_ac97_codec(struct snd_soc_codec *codec, 2028db2a4165SFrank Mandarino struct snd_ac97_bus_ops *ops, int num) 2029db2a4165SFrank Mandarino { 2030db2a4165SFrank Mandarino mutex_lock(&codec->mutex); 2031db2a4165SFrank Mandarino 2032db2a4165SFrank Mandarino codec->ac97 = kzalloc(sizeof(struct snd_ac97), GFP_KERNEL); 2033db2a4165SFrank Mandarino if (codec->ac97 == NULL) { 2034db2a4165SFrank Mandarino mutex_unlock(&codec->mutex); 2035db2a4165SFrank Mandarino return -ENOMEM; 2036db2a4165SFrank Mandarino } 2037db2a4165SFrank Mandarino 2038db2a4165SFrank Mandarino codec->ac97->bus = kzalloc(sizeof(struct snd_ac97_bus), GFP_KERNEL); 2039db2a4165SFrank Mandarino if (codec->ac97->bus == NULL) { 2040db2a4165SFrank Mandarino kfree(codec->ac97); 2041db2a4165SFrank Mandarino codec->ac97 = NULL; 2042db2a4165SFrank Mandarino mutex_unlock(&codec->mutex); 2043db2a4165SFrank Mandarino return -ENOMEM; 2044db2a4165SFrank Mandarino } 2045db2a4165SFrank Mandarino 2046db2a4165SFrank Mandarino codec->ac97->bus->ops = ops; 2047db2a4165SFrank Mandarino codec->ac97->num = num; 20480562f788SMika Westerberg 20490562f788SMika Westerberg /* 20500562f788SMika Westerberg * Mark the AC97 device to be created by us. This way we ensure that the 20510562f788SMika Westerberg * device will be registered with the device subsystem later on. 20520562f788SMika Westerberg */ 20530562f788SMika Westerberg codec->ac97_created = 1; 20540562f788SMika Westerberg 2055db2a4165SFrank Mandarino mutex_unlock(&codec->mutex); 2056db2a4165SFrank Mandarino return 0; 2057db2a4165SFrank Mandarino } 2058db2a4165SFrank Mandarino EXPORT_SYMBOL_GPL(snd_soc_new_ac97_codec); 2059db2a4165SFrank Mandarino 2060db2a4165SFrank Mandarino /** 2061db2a4165SFrank Mandarino * snd_soc_free_ac97_codec - free AC97 codec device 2062db2a4165SFrank Mandarino * @codec: audio codec 2063db2a4165SFrank Mandarino * 2064db2a4165SFrank Mandarino * Frees AC97 codec device resources. 2065db2a4165SFrank Mandarino */ 2066db2a4165SFrank Mandarino void snd_soc_free_ac97_codec(struct snd_soc_codec *codec) 2067db2a4165SFrank Mandarino { 2068db2a4165SFrank Mandarino mutex_lock(&codec->mutex); 2069f0fba2adSLiam Girdwood #ifdef CONFIG_SND_SOC_AC97_BUS 2070f0fba2adSLiam Girdwood soc_unregister_ac97_dai_link(codec); 2071f0fba2adSLiam Girdwood #endif 2072db2a4165SFrank Mandarino kfree(codec->ac97->bus); 2073db2a4165SFrank Mandarino kfree(codec->ac97); 2074db2a4165SFrank Mandarino codec->ac97 = NULL; 20750562f788SMika Westerberg codec->ac97_created = 0; 2076db2a4165SFrank Mandarino mutex_unlock(&codec->mutex); 2077db2a4165SFrank Mandarino } 2078db2a4165SFrank Mandarino EXPORT_SYMBOL_GPL(snd_soc_free_ac97_codec); 2079db2a4165SFrank Mandarino 2080c3753707SMark Brown unsigned int snd_soc_read(struct snd_soc_codec *codec, unsigned int reg) 2081c3753707SMark Brown { 2082c3753707SMark Brown unsigned int ret; 2083c3753707SMark Brown 2084c3acec26SMark Brown ret = codec->read(codec, reg); 2085c3753707SMark Brown dev_dbg(codec->dev, "read %x => %x\n", reg, ret); 2086a8b1d34fSMark Brown trace_snd_soc_reg_read(codec, reg, ret); 2087c3753707SMark Brown 2088c3753707SMark Brown return ret; 2089c3753707SMark Brown } 2090c3753707SMark Brown EXPORT_SYMBOL_GPL(snd_soc_read); 2091c3753707SMark Brown 2092c3753707SMark Brown unsigned int snd_soc_write(struct snd_soc_codec *codec, 2093c3753707SMark Brown unsigned int reg, unsigned int val) 2094c3753707SMark Brown { 2095c3753707SMark Brown dev_dbg(codec->dev, "write %x = %x\n", reg, val); 2096a8b1d34fSMark Brown trace_snd_soc_reg_write(codec, reg, val); 2097c3acec26SMark Brown return codec->write(codec, reg, val); 2098c3753707SMark Brown } 2099c3753707SMark Brown EXPORT_SYMBOL_GPL(snd_soc_write); 2100c3753707SMark Brown 2101db2a4165SFrank Mandarino /** 2102db2a4165SFrank Mandarino * snd_soc_update_bits - update codec register bits 2103db2a4165SFrank Mandarino * @codec: audio codec 2104db2a4165SFrank Mandarino * @reg: codec register 2105db2a4165SFrank Mandarino * @mask: register mask 2106db2a4165SFrank Mandarino * @value: new value 2107db2a4165SFrank Mandarino * 2108db2a4165SFrank Mandarino * Writes new register value. 2109db2a4165SFrank Mandarino * 2110db2a4165SFrank Mandarino * Returns 1 for change else 0. 2111db2a4165SFrank Mandarino */ 2112db2a4165SFrank Mandarino int snd_soc_update_bits(struct snd_soc_codec *codec, unsigned short reg, 211346f5822fSDaniel Ribeiro unsigned int mask, unsigned int value) 2114db2a4165SFrank Mandarino { 2115db2a4165SFrank Mandarino int change; 211646f5822fSDaniel Ribeiro unsigned int old, new; 2117db2a4165SFrank Mandarino 2118db2a4165SFrank Mandarino old = snd_soc_read(codec, reg); 2119db2a4165SFrank Mandarino new = (old & ~mask) | value; 2120db2a4165SFrank Mandarino change = old != new; 2121db2a4165SFrank Mandarino if (change) 2122db2a4165SFrank Mandarino snd_soc_write(codec, reg, new); 2123db2a4165SFrank Mandarino 2124db2a4165SFrank Mandarino return change; 2125db2a4165SFrank Mandarino } 2126db2a4165SFrank Mandarino EXPORT_SYMBOL_GPL(snd_soc_update_bits); 2127db2a4165SFrank Mandarino 2128db2a4165SFrank Mandarino /** 21296c508c62SEero Nurkkala * snd_soc_update_bits_locked - update codec register bits 21306c508c62SEero Nurkkala * @codec: audio codec 21316c508c62SEero Nurkkala * @reg: codec register 21326c508c62SEero Nurkkala * @mask: register mask 21336c508c62SEero Nurkkala * @value: new value 21346c508c62SEero Nurkkala * 21356c508c62SEero Nurkkala * Writes new register value, and takes the codec mutex. 21366c508c62SEero Nurkkala * 21376c508c62SEero Nurkkala * Returns 1 for change else 0. 21386c508c62SEero Nurkkala */ 2139dd1b3d53SMark Brown int snd_soc_update_bits_locked(struct snd_soc_codec *codec, 21406c508c62SEero Nurkkala unsigned short reg, unsigned int mask, 21416c508c62SEero Nurkkala unsigned int value) 21426c508c62SEero Nurkkala { 21436c508c62SEero Nurkkala int change; 21446c508c62SEero Nurkkala 21456c508c62SEero Nurkkala mutex_lock(&codec->mutex); 21466c508c62SEero Nurkkala change = snd_soc_update_bits(codec, reg, mask, value); 21476c508c62SEero Nurkkala mutex_unlock(&codec->mutex); 21486c508c62SEero Nurkkala 21496c508c62SEero Nurkkala return change; 21506c508c62SEero Nurkkala } 2151dd1b3d53SMark Brown EXPORT_SYMBOL_GPL(snd_soc_update_bits_locked); 21526c508c62SEero Nurkkala 21536c508c62SEero Nurkkala /** 2154db2a4165SFrank Mandarino * snd_soc_test_bits - test register for change 2155db2a4165SFrank Mandarino * @codec: audio codec 2156db2a4165SFrank Mandarino * @reg: codec register 2157db2a4165SFrank Mandarino * @mask: register mask 2158db2a4165SFrank Mandarino * @value: new value 2159db2a4165SFrank Mandarino * 2160db2a4165SFrank Mandarino * Tests a register with a new value and checks if the new value is 2161db2a4165SFrank Mandarino * different from the old value. 2162db2a4165SFrank Mandarino * 2163db2a4165SFrank Mandarino * Returns 1 for change else 0. 2164db2a4165SFrank Mandarino */ 2165db2a4165SFrank Mandarino int snd_soc_test_bits(struct snd_soc_codec *codec, unsigned short reg, 216646f5822fSDaniel Ribeiro unsigned int mask, unsigned int value) 2167db2a4165SFrank Mandarino { 2168db2a4165SFrank Mandarino int change; 216946f5822fSDaniel Ribeiro unsigned int old, new; 2170db2a4165SFrank Mandarino 2171db2a4165SFrank Mandarino old = snd_soc_read(codec, reg); 2172db2a4165SFrank Mandarino new = (old & ~mask) | value; 2173db2a4165SFrank Mandarino change = old != new; 2174db2a4165SFrank Mandarino 2175db2a4165SFrank Mandarino return change; 2176db2a4165SFrank Mandarino } 2177db2a4165SFrank Mandarino EXPORT_SYMBOL_GPL(snd_soc_test_bits); 2178db2a4165SFrank Mandarino 2179db2a4165SFrank Mandarino /** 2180db2a4165SFrank Mandarino * snd_soc_set_runtime_hwparams - set the runtime hardware parameters 2181db2a4165SFrank Mandarino * @substream: the pcm substream 2182db2a4165SFrank Mandarino * @hw: the hardware parameters 2183db2a4165SFrank Mandarino * 2184db2a4165SFrank Mandarino * Sets the substream runtime hardware parameters. 2185db2a4165SFrank Mandarino */ 2186db2a4165SFrank Mandarino int snd_soc_set_runtime_hwparams(struct snd_pcm_substream *substream, 2187db2a4165SFrank Mandarino const struct snd_pcm_hardware *hw) 2188db2a4165SFrank Mandarino { 2189db2a4165SFrank Mandarino struct snd_pcm_runtime *runtime = substream->runtime; 2190db2a4165SFrank Mandarino runtime->hw.info = hw->info; 2191db2a4165SFrank Mandarino runtime->hw.formats = hw->formats; 2192db2a4165SFrank Mandarino runtime->hw.period_bytes_min = hw->period_bytes_min; 2193db2a4165SFrank Mandarino runtime->hw.period_bytes_max = hw->period_bytes_max; 2194db2a4165SFrank Mandarino runtime->hw.periods_min = hw->periods_min; 2195db2a4165SFrank Mandarino runtime->hw.periods_max = hw->periods_max; 2196db2a4165SFrank Mandarino runtime->hw.buffer_bytes_max = hw->buffer_bytes_max; 2197db2a4165SFrank Mandarino runtime->hw.fifo_size = hw->fifo_size; 2198db2a4165SFrank Mandarino return 0; 2199db2a4165SFrank Mandarino } 2200db2a4165SFrank Mandarino EXPORT_SYMBOL_GPL(snd_soc_set_runtime_hwparams); 2201db2a4165SFrank Mandarino 2202db2a4165SFrank Mandarino /** 2203db2a4165SFrank Mandarino * snd_soc_cnew - create new control 2204db2a4165SFrank Mandarino * @_template: control template 2205db2a4165SFrank Mandarino * @data: control private data 2206ac11a2b3SMark Brown * @long_name: control long name 2207db2a4165SFrank Mandarino * 2208db2a4165SFrank Mandarino * Create a new mixer control from a template control. 2209db2a4165SFrank Mandarino * 2210db2a4165SFrank Mandarino * Returns 0 for success, else error. 2211db2a4165SFrank Mandarino */ 2212db2a4165SFrank Mandarino struct snd_kcontrol *snd_soc_cnew(const struct snd_kcontrol_new *_template, 2213db2a4165SFrank Mandarino void *data, char *long_name) 2214db2a4165SFrank Mandarino { 2215db2a4165SFrank Mandarino struct snd_kcontrol_new template; 2216db2a4165SFrank Mandarino 2217db2a4165SFrank Mandarino memcpy(&template, _template, sizeof(template)); 2218db2a4165SFrank Mandarino if (long_name) 2219db2a4165SFrank Mandarino template.name = long_name; 2220db2a4165SFrank Mandarino template.index = 0; 2221db2a4165SFrank Mandarino 2222db2a4165SFrank Mandarino return snd_ctl_new1(&template, data); 2223db2a4165SFrank Mandarino } 2224db2a4165SFrank Mandarino EXPORT_SYMBOL_GPL(snd_soc_cnew); 2225db2a4165SFrank Mandarino 2226db2a4165SFrank Mandarino /** 22273e8e1952SIan Molton * snd_soc_add_controls - add an array of controls to a codec. 22283e8e1952SIan Molton * Convienience function to add a list of controls. Many codecs were 22293e8e1952SIan Molton * duplicating this code. 22303e8e1952SIan Molton * 22313e8e1952SIan Molton * @codec: codec to add controls to 22323e8e1952SIan Molton * @controls: array of controls to add 22333e8e1952SIan Molton * @num_controls: number of elements in the array 22343e8e1952SIan Molton * 22353e8e1952SIan Molton * Return 0 for success, else error. 22363e8e1952SIan Molton */ 22373e8e1952SIan Molton int snd_soc_add_controls(struct snd_soc_codec *codec, 22383e8e1952SIan Molton const struct snd_kcontrol_new *controls, int num_controls) 22393e8e1952SIan Molton { 2240f0fba2adSLiam Girdwood struct snd_card *card = codec->card->snd_card; 2241ead9b919SJarkko Nikula char prefixed_name[44], *name; 22423e8e1952SIan Molton int err, i; 22433e8e1952SIan Molton 22443e8e1952SIan Molton for (i = 0; i < num_controls; i++) { 22453e8e1952SIan Molton const struct snd_kcontrol_new *control = &controls[i]; 2246ead9b919SJarkko Nikula if (codec->name_prefix) { 2247ead9b919SJarkko Nikula snprintf(prefixed_name, sizeof(prefixed_name), "%s %s", 2248ead9b919SJarkko Nikula codec->name_prefix, control->name); 2249ead9b919SJarkko Nikula name = prefixed_name; 2250ead9b919SJarkko Nikula } else { 2251ead9b919SJarkko Nikula name = control->name; 2252ead9b919SJarkko Nikula } 2253ead9b919SJarkko Nikula err = snd_ctl_add(card, snd_soc_cnew(control, codec, name)); 22543e8e1952SIan Molton if (err < 0) { 2255082100dcSMark Brown dev_err(codec->dev, "%s: Failed to add %s: %d\n", 2256ead9b919SJarkko Nikula codec->name, name, err); 22573e8e1952SIan Molton return err; 22583e8e1952SIan Molton } 22593e8e1952SIan Molton } 22603e8e1952SIan Molton 22613e8e1952SIan Molton return 0; 22623e8e1952SIan Molton } 22633e8e1952SIan Molton EXPORT_SYMBOL_GPL(snd_soc_add_controls); 22643e8e1952SIan Molton 22653e8e1952SIan Molton /** 2266db2a4165SFrank Mandarino * snd_soc_info_enum_double - enumerated double mixer info callback 2267db2a4165SFrank Mandarino * @kcontrol: mixer control 2268db2a4165SFrank Mandarino * @uinfo: control element information 2269db2a4165SFrank Mandarino * 2270db2a4165SFrank Mandarino * Callback to provide information about a double enumerated 2271db2a4165SFrank Mandarino * mixer control. 2272db2a4165SFrank Mandarino * 2273db2a4165SFrank Mandarino * Returns 0 for success. 2274db2a4165SFrank Mandarino */ 2275db2a4165SFrank Mandarino int snd_soc_info_enum_double(struct snd_kcontrol *kcontrol, 2276db2a4165SFrank Mandarino struct snd_ctl_elem_info *uinfo) 2277db2a4165SFrank Mandarino { 2278db2a4165SFrank Mandarino struct soc_enum *e = (struct soc_enum *)kcontrol->private_value; 2279db2a4165SFrank Mandarino 2280db2a4165SFrank Mandarino uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED; 2281db2a4165SFrank Mandarino uinfo->count = e->shift_l == e->shift_r ? 1 : 2; 2282f8ba0b7bSJon Smirl uinfo->value.enumerated.items = e->max; 2283db2a4165SFrank Mandarino 2284f8ba0b7bSJon Smirl if (uinfo->value.enumerated.item > e->max - 1) 2285f8ba0b7bSJon Smirl uinfo->value.enumerated.item = e->max - 1; 2286db2a4165SFrank Mandarino strcpy(uinfo->value.enumerated.name, 2287db2a4165SFrank Mandarino e->texts[uinfo->value.enumerated.item]); 2288db2a4165SFrank Mandarino return 0; 2289db2a4165SFrank Mandarino } 2290db2a4165SFrank Mandarino EXPORT_SYMBOL_GPL(snd_soc_info_enum_double); 2291db2a4165SFrank Mandarino 2292db2a4165SFrank Mandarino /** 2293db2a4165SFrank Mandarino * snd_soc_get_enum_double - enumerated double mixer get callback 2294db2a4165SFrank Mandarino * @kcontrol: mixer control 2295ac11a2b3SMark Brown * @ucontrol: control element information 2296db2a4165SFrank Mandarino * 2297db2a4165SFrank Mandarino * Callback to get the value of a double enumerated mixer. 2298db2a4165SFrank Mandarino * 2299db2a4165SFrank Mandarino * Returns 0 for success. 2300db2a4165SFrank Mandarino */ 2301db2a4165SFrank Mandarino int snd_soc_get_enum_double(struct snd_kcontrol *kcontrol, 2302db2a4165SFrank Mandarino struct snd_ctl_elem_value *ucontrol) 2303db2a4165SFrank Mandarino { 2304db2a4165SFrank Mandarino struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol); 2305db2a4165SFrank Mandarino struct soc_enum *e = (struct soc_enum *)kcontrol->private_value; 230646f5822fSDaniel Ribeiro unsigned int val, bitmask; 2307db2a4165SFrank Mandarino 2308f8ba0b7bSJon Smirl for (bitmask = 1; bitmask < e->max; bitmask <<= 1) 2309db2a4165SFrank Mandarino ; 2310db2a4165SFrank Mandarino val = snd_soc_read(codec, e->reg); 23113ff3f64bSMark Brown ucontrol->value.enumerated.item[0] 23123ff3f64bSMark Brown = (val >> e->shift_l) & (bitmask - 1); 2313db2a4165SFrank Mandarino if (e->shift_l != e->shift_r) 2314db2a4165SFrank Mandarino ucontrol->value.enumerated.item[1] = 2315db2a4165SFrank Mandarino (val >> e->shift_r) & (bitmask - 1); 2316db2a4165SFrank Mandarino 2317db2a4165SFrank Mandarino return 0; 2318db2a4165SFrank Mandarino } 2319db2a4165SFrank Mandarino EXPORT_SYMBOL_GPL(snd_soc_get_enum_double); 2320db2a4165SFrank Mandarino 2321db2a4165SFrank Mandarino /** 2322db2a4165SFrank Mandarino * snd_soc_put_enum_double - enumerated double mixer put callback 2323db2a4165SFrank Mandarino * @kcontrol: mixer control 2324ac11a2b3SMark Brown * @ucontrol: control element information 2325db2a4165SFrank Mandarino * 2326db2a4165SFrank Mandarino * Callback to set the value of a double enumerated mixer. 2327db2a4165SFrank Mandarino * 2328db2a4165SFrank Mandarino * Returns 0 for success. 2329db2a4165SFrank Mandarino */ 2330db2a4165SFrank Mandarino int snd_soc_put_enum_double(struct snd_kcontrol *kcontrol, 2331db2a4165SFrank Mandarino struct snd_ctl_elem_value *ucontrol) 2332db2a4165SFrank Mandarino { 2333db2a4165SFrank Mandarino struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol); 2334db2a4165SFrank Mandarino struct soc_enum *e = (struct soc_enum *)kcontrol->private_value; 233546f5822fSDaniel Ribeiro unsigned int val; 233646f5822fSDaniel Ribeiro unsigned int mask, bitmask; 2337db2a4165SFrank Mandarino 2338f8ba0b7bSJon Smirl for (bitmask = 1; bitmask < e->max; bitmask <<= 1) 2339db2a4165SFrank Mandarino ; 2340f8ba0b7bSJon Smirl if (ucontrol->value.enumerated.item[0] > e->max - 1) 2341db2a4165SFrank Mandarino return -EINVAL; 2342db2a4165SFrank Mandarino val = ucontrol->value.enumerated.item[0] << e->shift_l; 2343db2a4165SFrank Mandarino mask = (bitmask - 1) << e->shift_l; 2344db2a4165SFrank Mandarino if (e->shift_l != e->shift_r) { 2345f8ba0b7bSJon Smirl if (ucontrol->value.enumerated.item[1] > e->max - 1) 2346db2a4165SFrank Mandarino return -EINVAL; 2347db2a4165SFrank Mandarino val |= ucontrol->value.enumerated.item[1] << e->shift_r; 2348db2a4165SFrank Mandarino mask |= (bitmask - 1) << e->shift_r; 2349db2a4165SFrank Mandarino } 2350db2a4165SFrank Mandarino 23516c508c62SEero Nurkkala return snd_soc_update_bits_locked(codec, e->reg, mask, val); 2352db2a4165SFrank Mandarino } 2353db2a4165SFrank Mandarino EXPORT_SYMBOL_GPL(snd_soc_put_enum_double); 2354db2a4165SFrank Mandarino 2355db2a4165SFrank Mandarino /** 23562e72f8e3SPeter Ujfalusi * snd_soc_get_value_enum_double - semi enumerated double mixer get callback 23572e72f8e3SPeter Ujfalusi * @kcontrol: mixer control 23582e72f8e3SPeter Ujfalusi * @ucontrol: control element information 23592e72f8e3SPeter Ujfalusi * 23602e72f8e3SPeter Ujfalusi * Callback to get the value of a double semi enumerated mixer. 23612e72f8e3SPeter Ujfalusi * 23622e72f8e3SPeter Ujfalusi * Semi enumerated mixer: the enumerated items are referred as values. Can be 23632e72f8e3SPeter Ujfalusi * used for handling bitfield coded enumeration for example. 23642e72f8e3SPeter Ujfalusi * 23652e72f8e3SPeter Ujfalusi * Returns 0 for success. 23662e72f8e3SPeter Ujfalusi */ 23672e72f8e3SPeter Ujfalusi int snd_soc_get_value_enum_double(struct snd_kcontrol *kcontrol, 23682e72f8e3SPeter Ujfalusi struct snd_ctl_elem_value *ucontrol) 23692e72f8e3SPeter Ujfalusi { 23702e72f8e3SPeter Ujfalusi struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol); 237174155556SPeter Ujfalusi struct soc_enum *e = (struct soc_enum *)kcontrol->private_value; 237246f5822fSDaniel Ribeiro unsigned int reg_val, val, mux; 23732e72f8e3SPeter Ujfalusi 23742e72f8e3SPeter Ujfalusi reg_val = snd_soc_read(codec, e->reg); 23752e72f8e3SPeter Ujfalusi val = (reg_val >> e->shift_l) & e->mask; 23762e72f8e3SPeter Ujfalusi for (mux = 0; mux < e->max; mux++) { 23772e72f8e3SPeter Ujfalusi if (val == e->values[mux]) 23782e72f8e3SPeter Ujfalusi break; 23792e72f8e3SPeter Ujfalusi } 23802e72f8e3SPeter Ujfalusi ucontrol->value.enumerated.item[0] = mux; 23812e72f8e3SPeter Ujfalusi if (e->shift_l != e->shift_r) { 23822e72f8e3SPeter Ujfalusi val = (reg_val >> e->shift_r) & e->mask; 23832e72f8e3SPeter Ujfalusi for (mux = 0; mux < e->max; mux++) { 23842e72f8e3SPeter Ujfalusi if (val == e->values[mux]) 23852e72f8e3SPeter Ujfalusi break; 23862e72f8e3SPeter Ujfalusi } 23872e72f8e3SPeter Ujfalusi ucontrol->value.enumerated.item[1] = mux; 23882e72f8e3SPeter Ujfalusi } 23892e72f8e3SPeter Ujfalusi 23902e72f8e3SPeter Ujfalusi return 0; 23912e72f8e3SPeter Ujfalusi } 23922e72f8e3SPeter Ujfalusi EXPORT_SYMBOL_GPL(snd_soc_get_value_enum_double); 23932e72f8e3SPeter Ujfalusi 23942e72f8e3SPeter Ujfalusi /** 23952e72f8e3SPeter Ujfalusi * snd_soc_put_value_enum_double - semi enumerated double mixer put callback 23962e72f8e3SPeter Ujfalusi * @kcontrol: mixer control 23972e72f8e3SPeter Ujfalusi * @ucontrol: control element information 23982e72f8e3SPeter Ujfalusi * 23992e72f8e3SPeter Ujfalusi * Callback to set the value of a double semi enumerated mixer. 24002e72f8e3SPeter Ujfalusi * 24012e72f8e3SPeter Ujfalusi * Semi enumerated mixer: the enumerated items are referred as values. Can be 24022e72f8e3SPeter Ujfalusi * used for handling bitfield coded enumeration for example. 24032e72f8e3SPeter Ujfalusi * 24042e72f8e3SPeter Ujfalusi * Returns 0 for success. 24052e72f8e3SPeter Ujfalusi */ 24062e72f8e3SPeter Ujfalusi int snd_soc_put_value_enum_double(struct snd_kcontrol *kcontrol, 24072e72f8e3SPeter Ujfalusi struct snd_ctl_elem_value *ucontrol) 24082e72f8e3SPeter Ujfalusi { 24092e72f8e3SPeter Ujfalusi struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol); 241074155556SPeter Ujfalusi struct soc_enum *e = (struct soc_enum *)kcontrol->private_value; 241146f5822fSDaniel Ribeiro unsigned int val; 241246f5822fSDaniel Ribeiro unsigned int mask; 24132e72f8e3SPeter Ujfalusi 24142e72f8e3SPeter Ujfalusi if (ucontrol->value.enumerated.item[0] > e->max - 1) 24152e72f8e3SPeter Ujfalusi return -EINVAL; 24162e72f8e3SPeter Ujfalusi val = e->values[ucontrol->value.enumerated.item[0]] << e->shift_l; 24172e72f8e3SPeter Ujfalusi mask = e->mask << e->shift_l; 24182e72f8e3SPeter Ujfalusi if (e->shift_l != e->shift_r) { 24192e72f8e3SPeter Ujfalusi if (ucontrol->value.enumerated.item[1] > e->max - 1) 24202e72f8e3SPeter Ujfalusi return -EINVAL; 24212e72f8e3SPeter Ujfalusi val |= e->values[ucontrol->value.enumerated.item[1]] << e->shift_r; 24222e72f8e3SPeter Ujfalusi mask |= e->mask << e->shift_r; 24232e72f8e3SPeter Ujfalusi } 24242e72f8e3SPeter Ujfalusi 24256c508c62SEero Nurkkala return snd_soc_update_bits_locked(codec, e->reg, mask, val); 24262e72f8e3SPeter Ujfalusi } 24272e72f8e3SPeter Ujfalusi EXPORT_SYMBOL_GPL(snd_soc_put_value_enum_double); 24282e72f8e3SPeter Ujfalusi 24292e72f8e3SPeter Ujfalusi /** 2430db2a4165SFrank Mandarino * snd_soc_info_enum_ext - external enumerated single mixer info callback 2431db2a4165SFrank Mandarino * @kcontrol: mixer control 2432db2a4165SFrank Mandarino * @uinfo: control element information 2433db2a4165SFrank Mandarino * 2434db2a4165SFrank Mandarino * Callback to provide information about an external enumerated 2435db2a4165SFrank Mandarino * single mixer. 2436db2a4165SFrank Mandarino * 2437db2a4165SFrank Mandarino * Returns 0 for success. 2438db2a4165SFrank Mandarino */ 2439db2a4165SFrank Mandarino int snd_soc_info_enum_ext(struct snd_kcontrol *kcontrol, 2440db2a4165SFrank Mandarino struct snd_ctl_elem_info *uinfo) 2441db2a4165SFrank Mandarino { 2442db2a4165SFrank Mandarino struct soc_enum *e = (struct soc_enum *)kcontrol->private_value; 2443db2a4165SFrank Mandarino 2444db2a4165SFrank Mandarino uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED; 2445db2a4165SFrank Mandarino uinfo->count = 1; 2446f8ba0b7bSJon Smirl uinfo->value.enumerated.items = e->max; 2447db2a4165SFrank Mandarino 2448f8ba0b7bSJon Smirl if (uinfo->value.enumerated.item > e->max - 1) 2449f8ba0b7bSJon Smirl uinfo->value.enumerated.item = e->max - 1; 2450db2a4165SFrank Mandarino strcpy(uinfo->value.enumerated.name, 2451db2a4165SFrank Mandarino e->texts[uinfo->value.enumerated.item]); 2452db2a4165SFrank Mandarino return 0; 2453db2a4165SFrank Mandarino } 2454db2a4165SFrank Mandarino EXPORT_SYMBOL_GPL(snd_soc_info_enum_ext); 2455db2a4165SFrank Mandarino 2456db2a4165SFrank Mandarino /** 2457db2a4165SFrank Mandarino * snd_soc_info_volsw_ext - external single mixer info callback 2458db2a4165SFrank Mandarino * @kcontrol: mixer control 2459db2a4165SFrank Mandarino * @uinfo: control element information 2460db2a4165SFrank Mandarino * 2461db2a4165SFrank Mandarino * Callback to provide information about a single external mixer control. 2462db2a4165SFrank Mandarino * 2463db2a4165SFrank Mandarino * Returns 0 for success. 2464db2a4165SFrank Mandarino */ 2465db2a4165SFrank Mandarino int snd_soc_info_volsw_ext(struct snd_kcontrol *kcontrol, 2466db2a4165SFrank Mandarino struct snd_ctl_elem_info *uinfo) 2467db2a4165SFrank Mandarino { 2468a7a4ac86SPhilipp Zabel int max = kcontrol->private_value; 2469db2a4165SFrank Mandarino 2470fd5dfad9SMark Brown if (max == 1 && !strstr(kcontrol->id.name, " Volume")) 2471a7a4ac86SPhilipp Zabel uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN; 2472a7a4ac86SPhilipp Zabel else 2473a7a4ac86SPhilipp Zabel uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER; 2474a7a4ac86SPhilipp Zabel 2475db2a4165SFrank Mandarino uinfo->count = 1; 2476db2a4165SFrank Mandarino uinfo->value.integer.min = 0; 2477a7a4ac86SPhilipp Zabel uinfo->value.integer.max = max; 2478db2a4165SFrank Mandarino return 0; 2479db2a4165SFrank Mandarino } 2480db2a4165SFrank Mandarino EXPORT_SYMBOL_GPL(snd_soc_info_volsw_ext); 2481db2a4165SFrank Mandarino 2482db2a4165SFrank Mandarino /** 2483db2a4165SFrank Mandarino * snd_soc_info_volsw - single mixer info callback 2484db2a4165SFrank Mandarino * @kcontrol: mixer control 2485db2a4165SFrank Mandarino * @uinfo: control element information 2486db2a4165SFrank Mandarino * 2487db2a4165SFrank Mandarino * Callback to provide information about a single mixer control. 2488db2a4165SFrank Mandarino * 2489db2a4165SFrank Mandarino * Returns 0 for success. 2490db2a4165SFrank Mandarino */ 2491db2a4165SFrank Mandarino int snd_soc_info_volsw(struct snd_kcontrol *kcontrol, 2492db2a4165SFrank Mandarino struct snd_ctl_elem_info *uinfo) 2493db2a4165SFrank Mandarino { 24944eaa9819SJon Smirl struct soc_mixer_control *mc = 24954eaa9819SJon Smirl (struct soc_mixer_control *)kcontrol->private_value; 2496d11bb4a9SPeter Ujfalusi int platform_max; 2497762b8df7SMark Brown unsigned int shift = mc->shift; 2498815ecf8dSJon Smirl unsigned int rshift = mc->rshift; 2499db2a4165SFrank Mandarino 2500d11bb4a9SPeter Ujfalusi if (!mc->platform_max) 2501d11bb4a9SPeter Ujfalusi mc->platform_max = mc->max; 2502d11bb4a9SPeter Ujfalusi platform_max = mc->platform_max; 2503d11bb4a9SPeter Ujfalusi 2504d11bb4a9SPeter Ujfalusi if (platform_max == 1 && !strstr(kcontrol->id.name, " Volume")) 2505a7a4ac86SPhilipp Zabel uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN; 2506a7a4ac86SPhilipp Zabel else 2507a7a4ac86SPhilipp Zabel uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER; 2508a7a4ac86SPhilipp Zabel 2509db2a4165SFrank Mandarino uinfo->count = shift == rshift ? 1 : 2; 2510db2a4165SFrank Mandarino uinfo->value.integer.min = 0; 2511d11bb4a9SPeter Ujfalusi uinfo->value.integer.max = platform_max; 2512db2a4165SFrank Mandarino return 0; 2513db2a4165SFrank Mandarino } 2514db2a4165SFrank Mandarino EXPORT_SYMBOL_GPL(snd_soc_info_volsw); 2515db2a4165SFrank Mandarino 2516db2a4165SFrank Mandarino /** 2517db2a4165SFrank Mandarino * snd_soc_get_volsw - single mixer get callback 2518db2a4165SFrank Mandarino * @kcontrol: mixer control 2519ac11a2b3SMark Brown * @ucontrol: control element information 2520db2a4165SFrank Mandarino * 2521db2a4165SFrank Mandarino * Callback to get the value of a single mixer control. 2522db2a4165SFrank Mandarino * 2523db2a4165SFrank Mandarino * Returns 0 for success. 2524db2a4165SFrank Mandarino */ 2525db2a4165SFrank Mandarino int snd_soc_get_volsw(struct snd_kcontrol *kcontrol, 2526db2a4165SFrank Mandarino struct snd_ctl_elem_value *ucontrol) 2527db2a4165SFrank Mandarino { 25284eaa9819SJon Smirl struct soc_mixer_control *mc = 25294eaa9819SJon Smirl (struct soc_mixer_control *)kcontrol->private_value; 2530db2a4165SFrank Mandarino struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol); 2531815ecf8dSJon Smirl unsigned int reg = mc->reg; 2532815ecf8dSJon Smirl unsigned int shift = mc->shift; 2533815ecf8dSJon Smirl unsigned int rshift = mc->rshift; 25344eaa9819SJon Smirl int max = mc->max; 2535815ecf8dSJon Smirl unsigned int mask = (1 << fls(max)) - 1; 2536815ecf8dSJon Smirl unsigned int invert = mc->invert; 2537db2a4165SFrank Mandarino 2538db2a4165SFrank Mandarino ucontrol->value.integer.value[0] = 2539db2a4165SFrank Mandarino (snd_soc_read(codec, reg) >> shift) & mask; 2540db2a4165SFrank Mandarino if (shift != rshift) 2541db2a4165SFrank Mandarino ucontrol->value.integer.value[1] = 2542db2a4165SFrank Mandarino (snd_soc_read(codec, reg) >> rshift) & mask; 2543db2a4165SFrank Mandarino if (invert) { 2544db2a4165SFrank Mandarino ucontrol->value.integer.value[0] = 2545a7a4ac86SPhilipp Zabel max - ucontrol->value.integer.value[0]; 2546db2a4165SFrank Mandarino if (shift != rshift) 2547db2a4165SFrank Mandarino ucontrol->value.integer.value[1] = 2548a7a4ac86SPhilipp Zabel max - ucontrol->value.integer.value[1]; 2549db2a4165SFrank Mandarino } 2550db2a4165SFrank Mandarino 2551db2a4165SFrank Mandarino return 0; 2552db2a4165SFrank Mandarino } 2553db2a4165SFrank Mandarino EXPORT_SYMBOL_GPL(snd_soc_get_volsw); 2554db2a4165SFrank Mandarino 2555db2a4165SFrank Mandarino /** 2556db2a4165SFrank Mandarino * snd_soc_put_volsw - single mixer put callback 2557db2a4165SFrank Mandarino * @kcontrol: mixer control 2558ac11a2b3SMark Brown * @ucontrol: control element information 2559db2a4165SFrank Mandarino * 2560db2a4165SFrank Mandarino * Callback to set the value of a single mixer control. 2561db2a4165SFrank Mandarino * 2562db2a4165SFrank Mandarino * Returns 0 for success. 2563db2a4165SFrank Mandarino */ 2564db2a4165SFrank Mandarino int snd_soc_put_volsw(struct snd_kcontrol *kcontrol, 2565db2a4165SFrank Mandarino struct snd_ctl_elem_value *ucontrol) 2566db2a4165SFrank Mandarino { 25674eaa9819SJon Smirl struct soc_mixer_control *mc = 25684eaa9819SJon Smirl (struct soc_mixer_control *)kcontrol->private_value; 2569db2a4165SFrank Mandarino struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol); 2570815ecf8dSJon Smirl unsigned int reg = mc->reg; 2571815ecf8dSJon Smirl unsigned int shift = mc->shift; 2572815ecf8dSJon Smirl unsigned int rshift = mc->rshift; 25734eaa9819SJon Smirl int max = mc->max; 2574815ecf8dSJon Smirl unsigned int mask = (1 << fls(max)) - 1; 2575815ecf8dSJon Smirl unsigned int invert = mc->invert; 257646f5822fSDaniel Ribeiro unsigned int val, val2, val_mask; 2577db2a4165SFrank Mandarino 2578db2a4165SFrank Mandarino val = (ucontrol->value.integer.value[0] & mask); 2579db2a4165SFrank Mandarino if (invert) 2580a7a4ac86SPhilipp Zabel val = max - val; 2581db2a4165SFrank Mandarino val_mask = mask << shift; 2582db2a4165SFrank Mandarino val = val << shift; 2583db2a4165SFrank Mandarino if (shift != rshift) { 2584db2a4165SFrank Mandarino val2 = (ucontrol->value.integer.value[1] & mask); 2585db2a4165SFrank Mandarino if (invert) 2586a7a4ac86SPhilipp Zabel val2 = max - val2; 2587db2a4165SFrank Mandarino val_mask |= mask << rshift; 2588db2a4165SFrank Mandarino val |= val2 << rshift; 2589db2a4165SFrank Mandarino } 25906c508c62SEero Nurkkala return snd_soc_update_bits_locked(codec, reg, val_mask, val); 2591db2a4165SFrank Mandarino } 2592db2a4165SFrank Mandarino EXPORT_SYMBOL_GPL(snd_soc_put_volsw); 2593db2a4165SFrank Mandarino 2594db2a4165SFrank Mandarino /** 2595db2a4165SFrank Mandarino * snd_soc_info_volsw_2r - double mixer info callback 2596db2a4165SFrank Mandarino * @kcontrol: mixer control 2597db2a4165SFrank Mandarino * @uinfo: control element information 2598db2a4165SFrank Mandarino * 2599db2a4165SFrank Mandarino * Callback to provide information about a double mixer control that 2600db2a4165SFrank Mandarino * spans 2 codec registers. 2601db2a4165SFrank Mandarino * 2602db2a4165SFrank Mandarino * Returns 0 for success. 2603db2a4165SFrank Mandarino */ 2604db2a4165SFrank Mandarino int snd_soc_info_volsw_2r(struct snd_kcontrol *kcontrol, 2605db2a4165SFrank Mandarino struct snd_ctl_elem_info *uinfo) 2606db2a4165SFrank Mandarino { 26074eaa9819SJon Smirl struct soc_mixer_control *mc = 26084eaa9819SJon Smirl (struct soc_mixer_control *)kcontrol->private_value; 2609d11bb4a9SPeter Ujfalusi int platform_max; 2610db2a4165SFrank Mandarino 2611d11bb4a9SPeter Ujfalusi if (!mc->platform_max) 2612d11bb4a9SPeter Ujfalusi mc->platform_max = mc->max; 2613d11bb4a9SPeter Ujfalusi platform_max = mc->platform_max; 2614d11bb4a9SPeter Ujfalusi 2615d11bb4a9SPeter Ujfalusi if (platform_max == 1 && !strstr(kcontrol->id.name, " Volume")) 2616a7a4ac86SPhilipp Zabel uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN; 2617a7a4ac86SPhilipp Zabel else 2618a7a4ac86SPhilipp Zabel uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER; 2619a7a4ac86SPhilipp Zabel 2620db2a4165SFrank Mandarino uinfo->count = 2; 2621db2a4165SFrank Mandarino uinfo->value.integer.min = 0; 2622d11bb4a9SPeter Ujfalusi uinfo->value.integer.max = platform_max; 2623db2a4165SFrank Mandarino return 0; 2624db2a4165SFrank Mandarino } 2625db2a4165SFrank Mandarino EXPORT_SYMBOL_GPL(snd_soc_info_volsw_2r); 2626db2a4165SFrank Mandarino 2627db2a4165SFrank Mandarino /** 2628db2a4165SFrank Mandarino * snd_soc_get_volsw_2r - double mixer get callback 2629db2a4165SFrank Mandarino * @kcontrol: mixer control 2630ac11a2b3SMark Brown * @ucontrol: control element information 2631db2a4165SFrank Mandarino * 2632db2a4165SFrank Mandarino * Callback to get the value of a double mixer control that spans 2 registers. 2633db2a4165SFrank Mandarino * 2634db2a4165SFrank Mandarino * Returns 0 for success. 2635db2a4165SFrank Mandarino */ 2636db2a4165SFrank Mandarino int snd_soc_get_volsw_2r(struct snd_kcontrol *kcontrol, 2637db2a4165SFrank Mandarino struct snd_ctl_elem_value *ucontrol) 2638db2a4165SFrank Mandarino { 26394eaa9819SJon Smirl struct soc_mixer_control *mc = 26404eaa9819SJon Smirl (struct soc_mixer_control *)kcontrol->private_value; 2641db2a4165SFrank Mandarino struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol); 2642815ecf8dSJon Smirl unsigned int reg = mc->reg; 2643815ecf8dSJon Smirl unsigned int reg2 = mc->rreg; 2644815ecf8dSJon Smirl unsigned int shift = mc->shift; 26454eaa9819SJon Smirl int max = mc->max; 2646815ecf8dSJon Smirl unsigned int mask = (1 << fls(max)) - 1; 2647815ecf8dSJon Smirl unsigned int invert = mc->invert; 2648db2a4165SFrank Mandarino 2649db2a4165SFrank Mandarino ucontrol->value.integer.value[0] = 2650db2a4165SFrank Mandarino (snd_soc_read(codec, reg) >> shift) & mask; 2651db2a4165SFrank Mandarino ucontrol->value.integer.value[1] = 2652db2a4165SFrank Mandarino (snd_soc_read(codec, reg2) >> shift) & mask; 2653db2a4165SFrank Mandarino if (invert) { 2654db2a4165SFrank Mandarino ucontrol->value.integer.value[0] = 2655a7a4ac86SPhilipp Zabel max - ucontrol->value.integer.value[0]; 2656db2a4165SFrank Mandarino ucontrol->value.integer.value[1] = 2657a7a4ac86SPhilipp Zabel max - ucontrol->value.integer.value[1]; 2658db2a4165SFrank Mandarino } 2659db2a4165SFrank Mandarino 2660db2a4165SFrank Mandarino return 0; 2661db2a4165SFrank Mandarino } 2662db2a4165SFrank Mandarino EXPORT_SYMBOL_GPL(snd_soc_get_volsw_2r); 2663db2a4165SFrank Mandarino 2664db2a4165SFrank Mandarino /** 2665db2a4165SFrank Mandarino * snd_soc_put_volsw_2r - double mixer set callback 2666db2a4165SFrank Mandarino * @kcontrol: mixer control 2667ac11a2b3SMark Brown * @ucontrol: control element information 2668db2a4165SFrank Mandarino * 2669db2a4165SFrank Mandarino * Callback to set the value of a double mixer control that spans 2 registers. 2670db2a4165SFrank Mandarino * 2671db2a4165SFrank Mandarino * Returns 0 for success. 2672db2a4165SFrank Mandarino */ 2673db2a4165SFrank Mandarino int snd_soc_put_volsw_2r(struct snd_kcontrol *kcontrol, 2674db2a4165SFrank Mandarino struct snd_ctl_elem_value *ucontrol) 2675db2a4165SFrank Mandarino { 26764eaa9819SJon Smirl struct soc_mixer_control *mc = 26774eaa9819SJon Smirl (struct soc_mixer_control *)kcontrol->private_value; 2678db2a4165SFrank Mandarino struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol); 2679815ecf8dSJon Smirl unsigned int reg = mc->reg; 2680815ecf8dSJon Smirl unsigned int reg2 = mc->rreg; 2681815ecf8dSJon Smirl unsigned int shift = mc->shift; 26824eaa9819SJon Smirl int max = mc->max; 2683815ecf8dSJon Smirl unsigned int mask = (1 << fls(max)) - 1; 2684815ecf8dSJon Smirl unsigned int invert = mc->invert; 2685db2a4165SFrank Mandarino int err; 268646f5822fSDaniel Ribeiro unsigned int val, val2, val_mask; 2687db2a4165SFrank Mandarino 2688db2a4165SFrank Mandarino val_mask = mask << shift; 2689db2a4165SFrank Mandarino val = (ucontrol->value.integer.value[0] & mask); 2690db2a4165SFrank Mandarino val2 = (ucontrol->value.integer.value[1] & mask); 2691db2a4165SFrank Mandarino 2692db2a4165SFrank Mandarino if (invert) { 2693a7a4ac86SPhilipp Zabel val = max - val; 2694a7a4ac86SPhilipp Zabel val2 = max - val2; 2695db2a4165SFrank Mandarino } 2696db2a4165SFrank Mandarino 2697db2a4165SFrank Mandarino val = val << shift; 2698db2a4165SFrank Mandarino val2 = val2 << shift; 2699db2a4165SFrank Mandarino 27006c508c62SEero Nurkkala err = snd_soc_update_bits_locked(codec, reg, val_mask, val); 27013ff3f64bSMark Brown if (err < 0) 2702db2a4165SFrank Mandarino return err; 2703db2a4165SFrank Mandarino 27046c508c62SEero Nurkkala err = snd_soc_update_bits_locked(codec, reg2, val_mask, val2); 2705db2a4165SFrank Mandarino return err; 2706db2a4165SFrank Mandarino } 2707db2a4165SFrank Mandarino EXPORT_SYMBOL_GPL(snd_soc_put_volsw_2r); 2708db2a4165SFrank Mandarino 2709e13ac2e9SMark Brown /** 2710e13ac2e9SMark Brown * snd_soc_info_volsw_s8 - signed mixer info callback 2711e13ac2e9SMark Brown * @kcontrol: mixer control 2712e13ac2e9SMark Brown * @uinfo: control element information 2713e13ac2e9SMark Brown * 2714e13ac2e9SMark Brown * Callback to provide information about a signed mixer control. 2715e13ac2e9SMark Brown * 2716e13ac2e9SMark Brown * Returns 0 for success. 2717e13ac2e9SMark Brown */ 2718e13ac2e9SMark Brown int snd_soc_info_volsw_s8(struct snd_kcontrol *kcontrol, 2719e13ac2e9SMark Brown struct snd_ctl_elem_info *uinfo) 2720e13ac2e9SMark Brown { 27214eaa9819SJon Smirl struct soc_mixer_control *mc = 27224eaa9819SJon Smirl (struct soc_mixer_control *)kcontrol->private_value; 2723d11bb4a9SPeter Ujfalusi int platform_max; 27244eaa9819SJon Smirl int min = mc->min; 2725e13ac2e9SMark Brown 2726d11bb4a9SPeter Ujfalusi if (!mc->platform_max) 2727d11bb4a9SPeter Ujfalusi mc->platform_max = mc->max; 2728d11bb4a9SPeter Ujfalusi platform_max = mc->platform_max; 2729d11bb4a9SPeter Ujfalusi 2730e13ac2e9SMark Brown uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER; 2731e13ac2e9SMark Brown uinfo->count = 2; 2732e13ac2e9SMark Brown uinfo->value.integer.min = 0; 2733d11bb4a9SPeter Ujfalusi uinfo->value.integer.max = platform_max - min; 2734e13ac2e9SMark Brown return 0; 2735e13ac2e9SMark Brown } 2736e13ac2e9SMark Brown EXPORT_SYMBOL_GPL(snd_soc_info_volsw_s8); 2737e13ac2e9SMark Brown 2738e13ac2e9SMark Brown /** 2739e13ac2e9SMark Brown * snd_soc_get_volsw_s8 - signed mixer get callback 2740e13ac2e9SMark Brown * @kcontrol: mixer control 2741ac11a2b3SMark Brown * @ucontrol: control element information 2742e13ac2e9SMark Brown * 2743e13ac2e9SMark Brown * Callback to get the value of a signed mixer control. 2744e13ac2e9SMark Brown * 2745e13ac2e9SMark Brown * Returns 0 for success. 2746e13ac2e9SMark Brown */ 2747e13ac2e9SMark Brown int snd_soc_get_volsw_s8(struct snd_kcontrol *kcontrol, 2748e13ac2e9SMark Brown struct snd_ctl_elem_value *ucontrol) 2749e13ac2e9SMark Brown { 27504eaa9819SJon Smirl struct soc_mixer_control *mc = 27514eaa9819SJon Smirl (struct soc_mixer_control *)kcontrol->private_value; 2752e13ac2e9SMark Brown struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol); 2753815ecf8dSJon Smirl unsigned int reg = mc->reg; 27544eaa9819SJon Smirl int min = mc->min; 2755e13ac2e9SMark Brown int val = snd_soc_read(codec, reg); 2756e13ac2e9SMark Brown 2757e13ac2e9SMark Brown ucontrol->value.integer.value[0] = 2758e13ac2e9SMark Brown ((signed char)(val & 0xff))-min; 2759e13ac2e9SMark Brown ucontrol->value.integer.value[1] = 2760e13ac2e9SMark Brown ((signed char)((val >> 8) & 0xff))-min; 2761e13ac2e9SMark Brown return 0; 2762e13ac2e9SMark Brown } 2763e13ac2e9SMark Brown EXPORT_SYMBOL_GPL(snd_soc_get_volsw_s8); 2764e13ac2e9SMark Brown 2765e13ac2e9SMark Brown /** 2766e13ac2e9SMark Brown * snd_soc_put_volsw_sgn - signed mixer put callback 2767e13ac2e9SMark Brown * @kcontrol: mixer control 2768ac11a2b3SMark Brown * @ucontrol: control element information 2769e13ac2e9SMark Brown * 2770e13ac2e9SMark Brown * Callback to set the value of a signed mixer control. 2771e13ac2e9SMark Brown * 2772e13ac2e9SMark Brown * Returns 0 for success. 2773e13ac2e9SMark Brown */ 2774e13ac2e9SMark Brown int snd_soc_put_volsw_s8(struct snd_kcontrol *kcontrol, 2775e13ac2e9SMark Brown struct snd_ctl_elem_value *ucontrol) 2776e13ac2e9SMark Brown { 27774eaa9819SJon Smirl struct soc_mixer_control *mc = 27784eaa9819SJon Smirl (struct soc_mixer_control *)kcontrol->private_value; 2779e13ac2e9SMark Brown struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol); 2780815ecf8dSJon Smirl unsigned int reg = mc->reg; 27814eaa9819SJon Smirl int min = mc->min; 278246f5822fSDaniel Ribeiro unsigned int val; 2783e13ac2e9SMark Brown 2784e13ac2e9SMark Brown val = (ucontrol->value.integer.value[0]+min) & 0xff; 2785e13ac2e9SMark Brown val |= ((ucontrol->value.integer.value[1]+min) & 0xff) << 8; 2786e13ac2e9SMark Brown 27876c508c62SEero Nurkkala return snd_soc_update_bits_locked(codec, reg, 0xffff, val); 2788e13ac2e9SMark Brown } 2789e13ac2e9SMark Brown EXPORT_SYMBOL_GPL(snd_soc_put_volsw_s8); 2790e13ac2e9SMark Brown 27918c6529dbSLiam Girdwood /** 2792637d3847SPeter Ujfalusi * snd_soc_limit_volume - Set new limit to an existing volume control. 2793637d3847SPeter Ujfalusi * 2794637d3847SPeter Ujfalusi * @codec: where to look for the control 2795637d3847SPeter Ujfalusi * @name: Name of the control 2796637d3847SPeter Ujfalusi * @max: new maximum limit 2797637d3847SPeter Ujfalusi * 2798637d3847SPeter Ujfalusi * Return 0 for success, else error. 2799637d3847SPeter Ujfalusi */ 2800637d3847SPeter Ujfalusi int snd_soc_limit_volume(struct snd_soc_codec *codec, 2801637d3847SPeter Ujfalusi const char *name, int max) 2802637d3847SPeter Ujfalusi { 2803f0fba2adSLiam Girdwood struct snd_card *card = codec->card->snd_card; 2804637d3847SPeter Ujfalusi struct snd_kcontrol *kctl; 2805637d3847SPeter Ujfalusi struct soc_mixer_control *mc; 2806637d3847SPeter Ujfalusi int found = 0; 2807637d3847SPeter Ujfalusi int ret = -EINVAL; 2808637d3847SPeter Ujfalusi 2809637d3847SPeter Ujfalusi /* Sanity check for name and max */ 2810637d3847SPeter Ujfalusi if (unlikely(!name || max <= 0)) 2811637d3847SPeter Ujfalusi return -EINVAL; 2812637d3847SPeter Ujfalusi 2813637d3847SPeter Ujfalusi list_for_each_entry(kctl, &card->controls, list) { 2814637d3847SPeter Ujfalusi if (!strncmp(kctl->id.name, name, sizeof(kctl->id.name))) { 2815637d3847SPeter Ujfalusi found = 1; 2816637d3847SPeter Ujfalusi break; 2817637d3847SPeter Ujfalusi } 2818637d3847SPeter Ujfalusi } 2819637d3847SPeter Ujfalusi if (found) { 2820637d3847SPeter Ujfalusi mc = (struct soc_mixer_control *)kctl->private_value; 2821637d3847SPeter Ujfalusi if (max <= mc->max) { 2822d11bb4a9SPeter Ujfalusi mc->platform_max = max; 2823637d3847SPeter Ujfalusi ret = 0; 2824637d3847SPeter Ujfalusi } 2825637d3847SPeter Ujfalusi } 2826637d3847SPeter Ujfalusi return ret; 2827637d3847SPeter Ujfalusi } 2828637d3847SPeter Ujfalusi EXPORT_SYMBOL_GPL(snd_soc_limit_volume); 2829637d3847SPeter Ujfalusi 2830637d3847SPeter Ujfalusi /** 2831b6f4bb38Sapatard@mandriva.com * snd_soc_info_volsw_2r_sx - double with tlv and variable data size 2832b6f4bb38Sapatard@mandriva.com * mixer info callback 2833b6f4bb38Sapatard@mandriva.com * @kcontrol: mixer control 2834b6f4bb38Sapatard@mandriva.com * @uinfo: control element information 2835b6f4bb38Sapatard@mandriva.com * 2836b6f4bb38Sapatard@mandriva.com * Returns 0 for success. 2837b6f4bb38Sapatard@mandriva.com */ 2838b6f4bb38Sapatard@mandriva.com int snd_soc_info_volsw_2r_sx(struct snd_kcontrol *kcontrol, 2839b6f4bb38Sapatard@mandriva.com struct snd_ctl_elem_info *uinfo) 2840b6f4bb38Sapatard@mandriva.com { 2841b6f4bb38Sapatard@mandriva.com struct soc_mixer_control *mc = 2842b6f4bb38Sapatard@mandriva.com (struct soc_mixer_control *)kcontrol->private_value; 2843b6f4bb38Sapatard@mandriva.com int max = mc->max; 2844b6f4bb38Sapatard@mandriva.com int min = mc->min; 2845b6f4bb38Sapatard@mandriva.com 2846b6f4bb38Sapatard@mandriva.com uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER; 2847b6f4bb38Sapatard@mandriva.com uinfo->count = 2; 2848b6f4bb38Sapatard@mandriva.com uinfo->value.integer.min = 0; 2849b6f4bb38Sapatard@mandriva.com uinfo->value.integer.max = max-min; 2850b6f4bb38Sapatard@mandriva.com 2851b6f4bb38Sapatard@mandriva.com return 0; 2852b6f4bb38Sapatard@mandriva.com } 2853b6f4bb38Sapatard@mandriva.com EXPORT_SYMBOL_GPL(snd_soc_info_volsw_2r_sx); 2854b6f4bb38Sapatard@mandriva.com 2855b6f4bb38Sapatard@mandriva.com /** 2856b6f4bb38Sapatard@mandriva.com * snd_soc_get_volsw_2r_sx - double with tlv and variable data size 2857b6f4bb38Sapatard@mandriva.com * mixer get callback 2858b6f4bb38Sapatard@mandriva.com * @kcontrol: mixer control 2859b6f4bb38Sapatard@mandriva.com * @uinfo: control element information 2860b6f4bb38Sapatard@mandriva.com * 2861b6f4bb38Sapatard@mandriva.com * Returns 0 for success. 2862b6f4bb38Sapatard@mandriva.com */ 2863b6f4bb38Sapatard@mandriva.com int snd_soc_get_volsw_2r_sx(struct snd_kcontrol *kcontrol, 2864b6f4bb38Sapatard@mandriva.com struct snd_ctl_elem_value *ucontrol) 2865b6f4bb38Sapatard@mandriva.com { 2866b6f4bb38Sapatard@mandriva.com struct soc_mixer_control *mc = 2867b6f4bb38Sapatard@mandriva.com (struct soc_mixer_control *)kcontrol->private_value; 2868b6f4bb38Sapatard@mandriva.com struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol); 2869b6f4bb38Sapatard@mandriva.com unsigned int mask = (1<<mc->shift)-1; 2870b6f4bb38Sapatard@mandriva.com int min = mc->min; 2871b6f4bb38Sapatard@mandriva.com int val = snd_soc_read(codec, mc->reg) & mask; 2872b6f4bb38Sapatard@mandriva.com int valr = snd_soc_read(codec, mc->rreg) & mask; 2873b6f4bb38Sapatard@mandriva.com 287420630c7fSStuart Longland ucontrol->value.integer.value[0] = ((val & 0xff)-min) & mask; 287520630c7fSStuart Longland ucontrol->value.integer.value[1] = ((valr & 0xff)-min) & mask; 2876b6f4bb38Sapatard@mandriva.com return 0; 2877b6f4bb38Sapatard@mandriva.com } 2878b6f4bb38Sapatard@mandriva.com EXPORT_SYMBOL_GPL(snd_soc_get_volsw_2r_sx); 2879b6f4bb38Sapatard@mandriva.com 2880b6f4bb38Sapatard@mandriva.com /** 2881b6f4bb38Sapatard@mandriva.com * snd_soc_put_volsw_2r_sx - double with tlv and variable data size 2882b6f4bb38Sapatard@mandriva.com * mixer put callback 2883b6f4bb38Sapatard@mandriva.com * @kcontrol: mixer control 2884b6f4bb38Sapatard@mandriva.com * @uinfo: control element information 2885b6f4bb38Sapatard@mandriva.com * 2886b6f4bb38Sapatard@mandriva.com * Returns 0 for success. 2887b6f4bb38Sapatard@mandriva.com */ 2888b6f4bb38Sapatard@mandriva.com int snd_soc_put_volsw_2r_sx(struct snd_kcontrol *kcontrol, 2889b6f4bb38Sapatard@mandriva.com struct snd_ctl_elem_value *ucontrol) 2890b6f4bb38Sapatard@mandriva.com { 2891b6f4bb38Sapatard@mandriva.com struct soc_mixer_control *mc = 2892b6f4bb38Sapatard@mandriva.com (struct soc_mixer_control *)kcontrol->private_value; 2893b6f4bb38Sapatard@mandriva.com struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol); 2894b6f4bb38Sapatard@mandriva.com unsigned int mask = (1<<mc->shift)-1; 2895b6f4bb38Sapatard@mandriva.com int min = mc->min; 2896b6f4bb38Sapatard@mandriva.com int ret; 2897b6f4bb38Sapatard@mandriva.com unsigned int val, valr, oval, ovalr; 2898b6f4bb38Sapatard@mandriva.com 2899b6f4bb38Sapatard@mandriva.com val = ((ucontrol->value.integer.value[0]+min) & 0xff); 2900b6f4bb38Sapatard@mandriva.com val &= mask; 2901b6f4bb38Sapatard@mandriva.com valr = ((ucontrol->value.integer.value[1]+min) & 0xff); 2902b6f4bb38Sapatard@mandriva.com valr &= mask; 2903b6f4bb38Sapatard@mandriva.com 2904b6f4bb38Sapatard@mandriva.com oval = snd_soc_read(codec, mc->reg) & mask; 2905b6f4bb38Sapatard@mandriva.com ovalr = snd_soc_read(codec, mc->rreg) & mask; 2906b6f4bb38Sapatard@mandriva.com 2907b6f4bb38Sapatard@mandriva.com ret = 0; 2908b6f4bb38Sapatard@mandriva.com if (oval != val) { 2909b6f4bb38Sapatard@mandriva.com ret = snd_soc_write(codec, mc->reg, val); 2910b6f4bb38Sapatard@mandriva.com if (ret < 0) 2911f1df5aecSMark Brown return ret; 2912b6f4bb38Sapatard@mandriva.com } 2913b6f4bb38Sapatard@mandriva.com if (ovalr != valr) { 2914b6f4bb38Sapatard@mandriva.com ret = snd_soc_write(codec, mc->rreg, valr); 2915b6f4bb38Sapatard@mandriva.com if (ret < 0) 2916f1df5aecSMark Brown return ret; 2917b6f4bb38Sapatard@mandriva.com } 2918b6f4bb38Sapatard@mandriva.com 2919b6f4bb38Sapatard@mandriva.com return 0; 2920b6f4bb38Sapatard@mandriva.com } 2921b6f4bb38Sapatard@mandriva.com EXPORT_SYMBOL_GPL(snd_soc_put_volsw_2r_sx); 2922b6f4bb38Sapatard@mandriva.com 2923b6f4bb38Sapatard@mandriva.com /** 29248c6529dbSLiam Girdwood * snd_soc_dai_set_sysclk - configure DAI system or master clock. 29258c6529dbSLiam Girdwood * @dai: DAI 29268c6529dbSLiam Girdwood * @clk_id: DAI specific clock ID 29278c6529dbSLiam Girdwood * @freq: new clock frequency in Hz 29288c6529dbSLiam Girdwood * @dir: new clock direction - input/output. 29298c6529dbSLiam Girdwood * 29308c6529dbSLiam Girdwood * Configures the DAI master (MCLK) or system (SYSCLK) clocking. 29318c6529dbSLiam Girdwood */ 29328c6529dbSLiam Girdwood int snd_soc_dai_set_sysclk(struct snd_soc_dai *dai, int clk_id, 29338c6529dbSLiam Girdwood unsigned int freq, int dir) 29348c6529dbSLiam Girdwood { 2935f0fba2adSLiam Girdwood if (dai->driver && dai->driver->ops->set_sysclk) 2936f0fba2adSLiam Girdwood return dai->driver->ops->set_sysclk(dai, clk_id, freq, dir); 29378c6529dbSLiam Girdwood else 29388c6529dbSLiam Girdwood return -EINVAL; 29398c6529dbSLiam Girdwood } 29408c6529dbSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_dai_set_sysclk); 29418c6529dbSLiam Girdwood 29428c6529dbSLiam Girdwood /** 29438c6529dbSLiam Girdwood * snd_soc_dai_set_clkdiv - configure DAI clock dividers. 29448c6529dbSLiam Girdwood * @dai: DAI 2945ac11a2b3SMark Brown * @div_id: DAI specific clock divider ID 29468c6529dbSLiam Girdwood * @div: new clock divisor. 29478c6529dbSLiam Girdwood * 29488c6529dbSLiam Girdwood * Configures the clock dividers. This is used to derive the best DAI bit and 29498c6529dbSLiam Girdwood * frame clocks from the system or master clock. It's best to set the DAI bit 29508c6529dbSLiam Girdwood * and frame clocks as low as possible to save system power. 29518c6529dbSLiam Girdwood */ 29528c6529dbSLiam Girdwood int snd_soc_dai_set_clkdiv(struct snd_soc_dai *dai, 29538c6529dbSLiam Girdwood int div_id, int div) 29548c6529dbSLiam Girdwood { 2955f0fba2adSLiam Girdwood if (dai->driver && dai->driver->ops->set_clkdiv) 2956f0fba2adSLiam Girdwood return dai->driver->ops->set_clkdiv(dai, div_id, div); 29578c6529dbSLiam Girdwood else 29588c6529dbSLiam Girdwood return -EINVAL; 29598c6529dbSLiam Girdwood } 29608c6529dbSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_dai_set_clkdiv); 29618c6529dbSLiam Girdwood 29628c6529dbSLiam Girdwood /** 29638c6529dbSLiam Girdwood * snd_soc_dai_set_pll - configure DAI PLL. 29648c6529dbSLiam Girdwood * @dai: DAI 29658c6529dbSLiam Girdwood * @pll_id: DAI specific PLL ID 296685488037SMark Brown * @source: DAI specific source for the PLL 29678c6529dbSLiam Girdwood * @freq_in: PLL input clock frequency in Hz 29688c6529dbSLiam Girdwood * @freq_out: requested PLL output clock frequency in Hz 29698c6529dbSLiam Girdwood * 29708c6529dbSLiam Girdwood * Configures and enables PLL to generate output clock based on input clock. 29718c6529dbSLiam Girdwood */ 297285488037SMark Brown int snd_soc_dai_set_pll(struct snd_soc_dai *dai, int pll_id, int source, 297385488037SMark Brown unsigned int freq_in, unsigned int freq_out) 29748c6529dbSLiam Girdwood { 2975f0fba2adSLiam Girdwood if (dai->driver && dai->driver->ops->set_pll) 2976f0fba2adSLiam Girdwood return dai->driver->ops->set_pll(dai, pll_id, source, 297785488037SMark Brown freq_in, freq_out); 29788c6529dbSLiam Girdwood else 29798c6529dbSLiam Girdwood return -EINVAL; 29808c6529dbSLiam Girdwood } 29818c6529dbSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_dai_set_pll); 29828c6529dbSLiam Girdwood 29838c6529dbSLiam Girdwood /** 29848c6529dbSLiam Girdwood * snd_soc_dai_set_fmt - configure DAI hardware audio format. 29858c6529dbSLiam Girdwood * @dai: DAI 29868c6529dbSLiam Girdwood * @fmt: SND_SOC_DAIFMT_ format value. 29878c6529dbSLiam Girdwood * 29888c6529dbSLiam Girdwood * Configures the DAI hardware format and clocking. 29898c6529dbSLiam Girdwood */ 29908c6529dbSLiam Girdwood int snd_soc_dai_set_fmt(struct snd_soc_dai *dai, unsigned int fmt) 29918c6529dbSLiam Girdwood { 2992f0fba2adSLiam Girdwood if (dai->driver && dai->driver->ops->set_fmt) 2993f0fba2adSLiam Girdwood return dai->driver->ops->set_fmt(dai, fmt); 29948c6529dbSLiam Girdwood else 29958c6529dbSLiam Girdwood return -EINVAL; 29968c6529dbSLiam Girdwood } 29978c6529dbSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_dai_set_fmt); 29988c6529dbSLiam Girdwood 29998c6529dbSLiam Girdwood /** 30008c6529dbSLiam Girdwood * snd_soc_dai_set_tdm_slot - configure DAI TDM. 30018c6529dbSLiam Girdwood * @dai: DAI 3002a5479e38SDaniel Ribeiro * @tx_mask: bitmask representing active TX slots. 3003a5479e38SDaniel Ribeiro * @rx_mask: bitmask representing active RX slots. 30048c6529dbSLiam Girdwood * @slots: Number of slots in use. 3005a5479e38SDaniel Ribeiro * @slot_width: Width in bits for each slot. 30068c6529dbSLiam Girdwood * 30078c6529dbSLiam Girdwood * Configures a DAI for TDM operation. Both mask and slots are codec and DAI 30088c6529dbSLiam Girdwood * specific. 30098c6529dbSLiam Girdwood */ 30108c6529dbSLiam Girdwood int snd_soc_dai_set_tdm_slot(struct snd_soc_dai *dai, 3011a5479e38SDaniel Ribeiro unsigned int tx_mask, unsigned int rx_mask, int slots, int slot_width) 30128c6529dbSLiam Girdwood { 3013f0fba2adSLiam Girdwood if (dai->driver && dai->driver->ops->set_tdm_slot) 3014f0fba2adSLiam Girdwood return dai->driver->ops->set_tdm_slot(dai, tx_mask, rx_mask, 3015a5479e38SDaniel Ribeiro slots, slot_width); 30168c6529dbSLiam Girdwood else 30178c6529dbSLiam Girdwood return -EINVAL; 30188c6529dbSLiam Girdwood } 30198c6529dbSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_dai_set_tdm_slot); 30208c6529dbSLiam Girdwood 30218c6529dbSLiam Girdwood /** 3022472df3cbSBarry Song * snd_soc_dai_set_channel_map - configure DAI audio channel map 3023472df3cbSBarry Song * @dai: DAI 3024472df3cbSBarry Song * @tx_num: how many TX channels 3025472df3cbSBarry Song * @tx_slot: pointer to an array which imply the TX slot number channel 3026472df3cbSBarry Song * 0~num-1 uses 3027472df3cbSBarry Song * @rx_num: how many RX channels 3028472df3cbSBarry Song * @rx_slot: pointer to an array which imply the RX slot number channel 3029472df3cbSBarry Song * 0~num-1 uses 3030472df3cbSBarry Song * 3031472df3cbSBarry Song * configure the relationship between channel number and TDM slot number. 3032472df3cbSBarry Song */ 3033472df3cbSBarry Song int snd_soc_dai_set_channel_map(struct snd_soc_dai *dai, 3034472df3cbSBarry Song unsigned int tx_num, unsigned int *tx_slot, 3035472df3cbSBarry Song unsigned int rx_num, unsigned int *rx_slot) 3036472df3cbSBarry Song { 3037f0fba2adSLiam Girdwood if (dai->driver && dai->driver->ops->set_channel_map) 3038f0fba2adSLiam Girdwood return dai->driver->ops->set_channel_map(dai, tx_num, tx_slot, 3039472df3cbSBarry Song rx_num, rx_slot); 3040472df3cbSBarry Song else 3041472df3cbSBarry Song return -EINVAL; 3042472df3cbSBarry Song } 3043472df3cbSBarry Song EXPORT_SYMBOL_GPL(snd_soc_dai_set_channel_map); 3044472df3cbSBarry Song 3045472df3cbSBarry Song /** 30468c6529dbSLiam Girdwood * snd_soc_dai_set_tristate - configure DAI system or master clock. 30478c6529dbSLiam Girdwood * @dai: DAI 30488c6529dbSLiam Girdwood * @tristate: tristate enable 30498c6529dbSLiam Girdwood * 30508c6529dbSLiam Girdwood * Tristates the DAI so that others can use it. 30518c6529dbSLiam Girdwood */ 30528c6529dbSLiam Girdwood int snd_soc_dai_set_tristate(struct snd_soc_dai *dai, int tristate) 30538c6529dbSLiam Girdwood { 3054f0fba2adSLiam Girdwood if (dai->driver && dai->driver->ops->set_tristate) 3055f0fba2adSLiam Girdwood return dai->driver->ops->set_tristate(dai, tristate); 30568c6529dbSLiam Girdwood else 30578c6529dbSLiam Girdwood return -EINVAL; 30588c6529dbSLiam Girdwood } 30598c6529dbSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_dai_set_tristate); 30608c6529dbSLiam Girdwood 30618c6529dbSLiam Girdwood /** 30628c6529dbSLiam Girdwood * snd_soc_dai_digital_mute - configure DAI system or master clock. 30638c6529dbSLiam Girdwood * @dai: DAI 30648c6529dbSLiam Girdwood * @mute: mute enable 30658c6529dbSLiam Girdwood * 30668c6529dbSLiam Girdwood * Mutes the DAI DAC. 30678c6529dbSLiam Girdwood */ 30688c6529dbSLiam Girdwood int snd_soc_dai_digital_mute(struct snd_soc_dai *dai, int mute) 30698c6529dbSLiam Girdwood { 3070f0fba2adSLiam Girdwood if (dai->driver && dai->driver->ops->digital_mute) 3071f0fba2adSLiam Girdwood return dai->driver->ops->digital_mute(dai, mute); 30728c6529dbSLiam Girdwood else 30738c6529dbSLiam Girdwood return -EINVAL; 30748c6529dbSLiam Girdwood } 30758c6529dbSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_dai_digital_mute); 30768c6529dbSLiam Girdwood 3077c5af3a2eSMark Brown /** 3078c5af3a2eSMark Brown * snd_soc_register_card - Register a card with the ASoC core 3079c5af3a2eSMark Brown * 3080ac11a2b3SMark Brown * @card: Card to register 3081c5af3a2eSMark Brown * 3082c5af3a2eSMark Brown * Note that currently this is an internal only function: it will be 3083c5af3a2eSMark Brown * exposed to machine drivers after further backporting of ASoC v2 3084c5af3a2eSMark Brown * registration APIs. 3085c5af3a2eSMark Brown */ 3086c5af3a2eSMark Brown static int snd_soc_register_card(struct snd_soc_card *card) 3087c5af3a2eSMark Brown { 3088f0fba2adSLiam Girdwood int i; 3089f0fba2adSLiam Girdwood 3090c5af3a2eSMark Brown if (!card->name || !card->dev) 3091c5af3a2eSMark Brown return -EINVAL; 3092c5af3a2eSMark Brown 30932eea392dSJarkko Nikula card->rtd = kzalloc(sizeof(struct snd_soc_pcm_runtime) * 30942eea392dSJarkko Nikula (card->num_links + card->num_aux_devs), 3095f0fba2adSLiam Girdwood GFP_KERNEL); 3096f0fba2adSLiam Girdwood if (card->rtd == NULL) 3097f0fba2adSLiam Girdwood return -ENOMEM; 30982eea392dSJarkko Nikula card->rtd_aux = &card->rtd[card->num_links]; 3099f0fba2adSLiam Girdwood 3100f0fba2adSLiam Girdwood for (i = 0; i < card->num_links; i++) 3101f0fba2adSLiam Girdwood card->rtd[i].dai_link = &card->dai_link[i]; 3102f0fba2adSLiam Girdwood 3103c5af3a2eSMark Brown INIT_LIST_HEAD(&card->list); 3104c5af3a2eSMark Brown card->instantiated = 0; 3105f0fba2adSLiam Girdwood mutex_init(&card->mutex); 3106c5af3a2eSMark Brown 3107c5af3a2eSMark Brown mutex_lock(&client_mutex); 3108c5af3a2eSMark Brown list_add(&card->list, &card_list); 3109435c5e25SMark Brown snd_soc_instantiate_cards(); 3110c5af3a2eSMark Brown mutex_unlock(&client_mutex); 3111c5af3a2eSMark Brown 3112c5af3a2eSMark Brown dev_dbg(card->dev, "Registered card '%s'\n", card->name); 3113c5af3a2eSMark Brown 3114c5af3a2eSMark Brown return 0; 3115c5af3a2eSMark Brown } 3116c5af3a2eSMark Brown 3117c5af3a2eSMark Brown /** 3118c5af3a2eSMark Brown * snd_soc_unregister_card - Unregister a card with the ASoC core 3119c5af3a2eSMark Brown * 3120ac11a2b3SMark Brown * @card: Card to unregister 3121c5af3a2eSMark Brown * 3122c5af3a2eSMark Brown * Note that currently this is an internal only function: it will be 3123c5af3a2eSMark Brown * exposed to machine drivers after further backporting of ASoC v2 3124c5af3a2eSMark Brown * registration APIs. 3125c5af3a2eSMark Brown */ 3126c5af3a2eSMark Brown static int snd_soc_unregister_card(struct snd_soc_card *card) 3127c5af3a2eSMark Brown { 3128c5af3a2eSMark Brown mutex_lock(&client_mutex); 3129c5af3a2eSMark Brown list_del(&card->list); 3130c5af3a2eSMark Brown mutex_unlock(&client_mutex); 3131c5af3a2eSMark Brown dev_dbg(card->dev, "Unregistered card '%s'\n", card->name); 3132c5af3a2eSMark Brown 3133c5af3a2eSMark Brown return 0; 3134c5af3a2eSMark Brown } 3135c5af3a2eSMark Brown 3136f0fba2adSLiam Girdwood /* 3137f0fba2adSLiam Girdwood * Simplify DAI link configuration by removing ".-1" from device names 3138f0fba2adSLiam Girdwood * and sanitizing names. 3139f0fba2adSLiam Girdwood */ 3140f0fba2adSLiam Girdwood static inline char *fmt_single_name(struct device *dev, int *id) 3141f0fba2adSLiam Girdwood { 3142f0fba2adSLiam Girdwood char *found, name[NAME_SIZE]; 3143f0fba2adSLiam Girdwood int id1, id2; 3144f0fba2adSLiam Girdwood 3145f0fba2adSLiam Girdwood if (dev_name(dev) == NULL) 3146f0fba2adSLiam Girdwood return NULL; 3147f0fba2adSLiam Girdwood 3148f0fba2adSLiam Girdwood strncpy(name, dev_name(dev), NAME_SIZE); 3149f0fba2adSLiam Girdwood 3150f0fba2adSLiam Girdwood /* are we a "%s.%d" name (platform and SPI components) */ 3151f0fba2adSLiam Girdwood found = strstr(name, dev->driver->name); 3152f0fba2adSLiam Girdwood if (found) { 3153f0fba2adSLiam Girdwood /* get ID */ 3154f0fba2adSLiam Girdwood if (sscanf(&found[strlen(dev->driver->name)], ".%d", id) == 1) { 3155f0fba2adSLiam Girdwood 3156f0fba2adSLiam Girdwood /* discard ID from name if ID == -1 */ 3157f0fba2adSLiam Girdwood if (*id == -1) 3158f0fba2adSLiam Girdwood found[strlen(dev->driver->name)] = '\0'; 3159f0fba2adSLiam Girdwood } 3160f0fba2adSLiam Girdwood 3161f0fba2adSLiam Girdwood } else { 3162f0fba2adSLiam Girdwood /* I2C component devices are named "bus-addr" */ 3163f0fba2adSLiam Girdwood if (sscanf(name, "%x-%x", &id1, &id2) == 2) { 3164f0fba2adSLiam Girdwood char tmp[NAME_SIZE]; 3165f0fba2adSLiam Girdwood 3166f0fba2adSLiam Girdwood /* create unique ID number from I2C addr and bus */ 316705899446SJarkko Nikula *id = ((id1 & 0xffff) << 16) + id2; 3168f0fba2adSLiam Girdwood 3169f0fba2adSLiam Girdwood /* sanitize component name for DAI link creation */ 3170f0fba2adSLiam Girdwood snprintf(tmp, NAME_SIZE, "%s.%s", dev->driver->name, name); 3171f0fba2adSLiam Girdwood strncpy(name, tmp, NAME_SIZE); 3172f0fba2adSLiam Girdwood } else 3173f0fba2adSLiam Girdwood *id = 0; 3174f0fba2adSLiam Girdwood } 3175f0fba2adSLiam Girdwood 3176f0fba2adSLiam Girdwood return kstrdup(name, GFP_KERNEL); 3177f0fba2adSLiam Girdwood } 3178f0fba2adSLiam Girdwood 3179f0fba2adSLiam Girdwood /* 3180f0fba2adSLiam Girdwood * Simplify DAI link naming for single devices with multiple DAIs by removing 3181f0fba2adSLiam Girdwood * any ".-1" and using the DAI name (instead of device name). 3182f0fba2adSLiam Girdwood */ 3183f0fba2adSLiam Girdwood static inline char *fmt_multiple_name(struct device *dev, 3184f0fba2adSLiam Girdwood struct snd_soc_dai_driver *dai_drv) 3185f0fba2adSLiam Girdwood { 3186f0fba2adSLiam Girdwood if (dai_drv->name == NULL) { 3187f0fba2adSLiam Girdwood printk(KERN_ERR "asoc: error - multiple DAI %s registered with no name\n", 3188f0fba2adSLiam Girdwood dev_name(dev)); 3189f0fba2adSLiam Girdwood return NULL; 3190f0fba2adSLiam Girdwood } 3191f0fba2adSLiam Girdwood 3192f0fba2adSLiam Girdwood return kstrdup(dai_drv->name, GFP_KERNEL); 3193f0fba2adSLiam Girdwood } 3194f0fba2adSLiam Girdwood 31959115171aSMark Brown /** 31969115171aSMark Brown * snd_soc_register_dai - Register a DAI with the ASoC core 31979115171aSMark Brown * 3198ac11a2b3SMark Brown * @dai: DAI to register 31999115171aSMark Brown */ 3200f0fba2adSLiam Girdwood int snd_soc_register_dai(struct device *dev, 3201f0fba2adSLiam Girdwood struct snd_soc_dai_driver *dai_drv) 32029115171aSMark Brown { 3203f0fba2adSLiam Girdwood struct snd_soc_dai *dai; 32049115171aSMark Brown 3205f0fba2adSLiam Girdwood dev_dbg(dev, "dai register %s\n", dev_name(dev)); 32069115171aSMark Brown 3207f0fba2adSLiam Girdwood dai = kzalloc(sizeof(struct snd_soc_dai), GFP_KERNEL); 3208f0fba2adSLiam Girdwood if (dai == NULL) 3209f0fba2adSLiam Girdwood return -ENOMEM; 32106335d055SEric Miao 3211f0fba2adSLiam Girdwood /* create DAI component name */ 3212f0fba2adSLiam Girdwood dai->name = fmt_single_name(dev, &dai->id); 3213f0fba2adSLiam Girdwood if (dai->name == NULL) { 3214f0fba2adSLiam Girdwood kfree(dai); 3215f0fba2adSLiam Girdwood return -ENOMEM; 3216f0fba2adSLiam Girdwood } 3217f0fba2adSLiam Girdwood 3218f0fba2adSLiam Girdwood dai->dev = dev; 3219f0fba2adSLiam Girdwood dai->driver = dai_drv; 3220f0fba2adSLiam Girdwood if (!dai->driver->ops) 3221f0fba2adSLiam Girdwood dai->driver->ops = &null_dai_ops; 32229115171aSMark Brown 32239115171aSMark Brown mutex_lock(&client_mutex); 32249115171aSMark Brown list_add(&dai->list, &dai_list); 3225435c5e25SMark Brown snd_soc_instantiate_cards(); 32269115171aSMark Brown mutex_unlock(&client_mutex); 32279115171aSMark Brown 32289115171aSMark Brown pr_debug("Registered DAI '%s'\n", dai->name); 32299115171aSMark Brown 32309115171aSMark Brown return 0; 32319115171aSMark Brown } 32329115171aSMark Brown EXPORT_SYMBOL_GPL(snd_soc_register_dai); 32339115171aSMark Brown 32349115171aSMark Brown /** 32359115171aSMark Brown * snd_soc_unregister_dai - Unregister a DAI from the ASoC core 32369115171aSMark Brown * 3237ac11a2b3SMark Brown * @dai: DAI to unregister 32389115171aSMark Brown */ 3239f0fba2adSLiam Girdwood void snd_soc_unregister_dai(struct device *dev) 32409115171aSMark Brown { 3241f0fba2adSLiam Girdwood struct snd_soc_dai *dai; 3242f0fba2adSLiam Girdwood 3243f0fba2adSLiam Girdwood list_for_each_entry(dai, &dai_list, list) { 3244f0fba2adSLiam Girdwood if (dev == dai->dev) 3245f0fba2adSLiam Girdwood goto found; 3246f0fba2adSLiam Girdwood } 3247f0fba2adSLiam Girdwood return; 3248f0fba2adSLiam Girdwood 3249f0fba2adSLiam Girdwood found: 32509115171aSMark Brown mutex_lock(&client_mutex); 32519115171aSMark Brown list_del(&dai->list); 32529115171aSMark Brown mutex_unlock(&client_mutex); 32539115171aSMark Brown 32549115171aSMark Brown pr_debug("Unregistered DAI '%s'\n", dai->name); 3255f0fba2adSLiam Girdwood kfree(dai->name); 3256f0fba2adSLiam Girdwood kfree(dai); 32579115171aSMark Brown } 32589115171aSMark Brown EXPORT_SYMBOL_GPL(snd_soc_unregister_dai); 32599115171aSMark Brown 32609115171aSMark Brown /** 32619115171aSMark Brown * snd_soc_register_dais - Register multiple DAIs with the ASoC core 32629115171aSMark Brown * 3263ac11a2b3SMark Brown * @dai: Array of DAIs to register 3264ac11a2b3SMark Brown * @count: Number of DAIs 32659115171aSMark Brown */ 3266f0fba2adSLiam Girdwood int snd_soc_register_dais(struct device *dev, 3267f0fba2adSLiam Girdwood struct snd_soc_dai_driver *dai_drv, size_t count) 32689115171aSMark Brown { 3269f0fba2adSLiam Girdwood struct snd_soc_dai *dai; 3270f0fba2adSLiam Girdwood int i, ret = 0; 3271f0fba2adSLiam Girdwood 3272720ffa4cSLiam Girdwood dev_dbg(dev, "dai register %s #%Zu\n", dev_name(dev), count); 32739115171aSMark Brown 32749115171aSMark Brown for (i = 0; i < count; i++) { 3275f0fba2adSLiam Girdwood 3276f0fba2adSLiam Girdwood dai = kzalloc(sizeof(struct snd_soc_dai), GFP_KERNEL); 3277c46e0079SAxel Lin if (dai == NULL) { 3278c46e0079SAxel Lin ret = -ENOMEM; 3279c46e0079SAxel Lin goto err; 3280c46e0079SAxel Lin } 3281f0fba2adSLiam Girdwood 3282f0fba2adSLiam Girdwood /* create DAI component name */ 3283f0fba2adSLiam Girdwood dai->name = fmt_multiple_name(dev, &dai_drv[i]); 3284f0fba2adSLiam Girdwood if (dai->name == NULL) { 3285f0fba2adSLiam Girdwood kfree(dai); 3286f0fba2adSLiam Girdwood ret = -EINVAL; 32879115171aSMark Brown goto err; 32889115171aSMark Brown } 32899115171aSMark Brown 3290f0fba2adSLiam Girdwood dai->dev = dev; 3291f0fba2adSLiam Girdwood dai->driver = &dai_drv[i]; 32920f9141c9SMark Brown if (dai->driver->id) 32930f9141c9SMark Brown dai->id = dai->driver->id; 32940f9141c9SMark Brown else 32950f9141c9SMark Brown dai->id = i; 3296f0fba2adSLiam Girdwood if (!dai->driver->ops) 3297f0fba2adSLiam Girdwood dai->driver->ops = &null_dai_ops; 3298f0fba2adSLiam Girdwood 3299f0fba2adSLiam Girdwood mutex_lock(&client_mutex); 3300f0fba2adSLiam Girdwood list_add(&dai->list, &dai_list); 3301f0fba2adSLiam Girdwood mutex_unlock(&client_mutex); 3302f0fba2adSLiam Girdwood 3303f0fba2adSLiam Girdwood pr_debug("Registered DAI '%s'\n", dai->name); 3304f0fba2adSLiam Girdwood } 3305f0fba2adSLiam Girdwood 3306f0fba2adSLiam Girdwood snd_soc_instantiate_cards(); 33079115171aSMark Brown return 0; 33089115171aSMark Brown 33099115171aSMark Brown err: 33109115171aSMark Brown for (i--; i >= 0; i--) 3311f0fba2adSLiam Girdwood snd_soc_unregister_dai(dev); 33129115171aSMark Brown 33139115171aSMark Brown return ret; 33149115171aSMark Brown } 33159115171aSMark Brown EXPORT_SYMBOL_GPL(snd_soc_register_dais); 33169115171aSMark Brown 33179115171aSMark Brown /** 33189115171aSMark Brown * snd_soc_unregister_dais - Unregister multiple DAIs from the ASoC core 33199115171aSMark Brown * 3320ac11a2b3SMark Brown * @dai: Array of DAIs to unregister 3321ac11a2b3SMark Brown * @count: Number of DAIs 33229115171aSMark Brown */ 3323f0fba2adSLiam Girdwood void snd_soc_unregister_dais(struct device *dev, size_t count) 33249115171aSMark Brown { 33259115171aSMark Brown int i; 33269115171aSMark Brown 33279115171aSMark Brown for (i = 0; i < count; i++) 3328f0fba2adSLiam Girdwood snd_soc_unregister_dai(dev); 33299115171aSMark Brown } 33309115171aSMark Brown EXPORT_SYMBOL_GPL(snd_soc_unregister_dais); 33319115171aSMark Brown 333212a48a8cSMark Brown /** 333312a48a8cSMark Brown * snd_soc_register_platform - Register a platform with the ASoC core 333412a48a8cSMark Brown * 3335ac11a2b3SMark Brown * @platform: platform to register 333612a48a8cSMark Brown */ 3337f0fba2adSLiam Girdwood int snd_soc_register_platform(struct device *dev, 3338f0fba2adSLiam Girdwood struct snd_soc_platform_driver *platform_drv) 333912a48a8cSMark Brown { 3340f0fba2adSLiam Girdwood struct snd_soc_platform *platform; 334112a48a8cSMark Brown 3342f0fba2adSLiam Girdwood dev_dbg(dev, "platform register %s\n", dev_name(dev)); 3343f0fba2adSLiam Girdwood 3344f0fba2adSLiam Girdwood platform = kzalloc(sizeof(struct snd_soc_platform), GFP_KERNEL); 3345f0fba2adSLiam Girdwood if (platform == NULL) 3346f0fba2adSLiam Girdwood return -ENOMEM; 3347f0fba2adSLiam Girdwood 3348f0fba2adSLiam Girdwood /* create platform component name */ 3349f0fba2adSLiam Girdwood platform->name = fmt_single_name(dev, &platform->id); 3350f0fba2adSLiam Girdwood if (platform->name == NULL) { 3351f0fba2adSLiam Girdwood kfree(platform); 3352f0fba2adSLiam Girdwood return -ENOMEM; 3353f0fba2adSLiam Girdwood } 3354f0fba2adSLiam Girdwood 3355f0fba2adSLiam Girdwood platform->dev = dev; 3356f0fba2adSLiam Girdwood platform->driver = platform_drv; 335712a48a8cSMark Brown 335812a48a8cSMark Brown mutex_lock(&client_mutex); 335912a48a8cSMark Brown list_add(&platform->list, &platform_list); 3360435c5e25SMark Brown snd_soc_instantiate_cards(); 336112a48a8cSMark Brown mutex_unlock(&client_mutex); 336212a48a8cSMark Brown 336312a48a8cSMark Brown pr_debug("Registered platform '%s'\n", platform->name); 336412a48a8cSMark Brown 336512a48a8cSMark Brown return 0; 336612a48a8cSMark Brown } 336712a48a8cSMark Brown EXPORT_SYMBOL_GPL(snd_soc_register_platform); 336812a48a8cSMark Brown 336912a48a8cSMark Brown /** 337012a48a8cSMark Brown * snd_soc_unregister_platform - Unregister a platform from the ASoC core 337112a48a8cSMark Brown * 3372ac11a2b3SMark Brown * @platform: platform to unregister 337312a48a8cSMark Brown */ 3374f0fba2adSLiam Girdwood void snd_soc_unregister_platform(struct device *dev) 337512a48a8cSMark Brown { 3376f0fba2adSLiam Girdwood struct snd_soc_platform *platform; 3377f0fba2adSLiam Girdwood 3378f0fba2adSLiam Girdwood list_for_each_entry(platform, &platform_list, list) { 3379f0fba2adSLiam Girdwood if (dev == platform->dev) 3380f0fba2adSLiam Girdwood goto found; 3381f0fba2adSLiam Girdwood } 3382f0fba2adSLiam Girdwood return; 3383f0fba2adSLiam Girdwood 3384f0fba2adSLiam Girdwood found: 338512a48a8cSMark Brown mutex_lock(&client_mutex); 338612a48a8cSMark Brown list_del(&platform->list); 338712a48a8cSMark Brown mutex_unlock(&client_mutex); 338812a48a8cSMark Brown 338912a48a8cSMark Brown pr_debug("Unregistered platform '%s'\n", platform->name); 3390f0fba2adSLiam Girdwood kfree(platform->name); 3391f0fba2adSLiam Girdwood kfree(platform); 339212a48a8cSMark Brown } 339312a48a8cSMark Brown EXPORT_SYMBOL_GPL(snd_soc_unregister_platform); 339412a48a8cSMark Brown 3395151ab22cSMark Brown static u64 codec_format_map[] = { 3396151ab22cSMark Brown SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S16_BE, 3397151ab22cSMark Brown SNDRV_PCM_FMTBIT_U16_LE | SNDRV_PCM_FMTBIT_U16_BE, 3398151ab22cSMark Brown SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S24_BE, 3399151ab22cSMark Brown SNDRV_PCM_FMTBIT_U24_LE | SNDRV_PCM_FMTBIT_U24_BE, 3400151ab22cSMark Brown SNDRV_PCM_FMTBIT_S32_LE | SNDRV_PCM_FMTBIT_S32_BE, 3401151ab22cSMark Brown SNDRV_PCM_FMTBIT_U32_LE | SNDRV_PCM_FMTBIT_U32_BE, 3402151ab22cSMark Brown SNDRV_PCM_FMTBIT_S24_3LE | SNDRV_PCM_FMTBIT_U24_3BE, 3403151ab22cSMark Brown SNDRV_PCM_FMTBIT_U24_3LE | SNDRV_PCM_FMTBIT_U24_3BE, 3404151ab22cSMark Brown SNDRV_PCM_FMTBIT_S20_3LE | SNDRV_PCM_FMTBIT_S20_3BE, 3405151ab22cSMark Brown SNDRV_PCM_FMTBIT_U20_3LE | SNDRV_PCM_FMTBIT_U20_3BE, 3406151ab22cSMark Brown SNDRV_PCM_FMTBIT_S18_3LE | SNDRV_PCM_FMTBIT_S18_3BE, 3407151ab22cSMark Brown SNDRV_PCM_FMTBIT_U18_3LE | SNDRV_PCM_FMTBIT_U18_3BE, 3408151ab22cSMark Brown SNDRV_PCM_FMTBIT_FLOAT_LE | SNDRV_PCM_FMTBIT_FLOAT_BE, 3409151ab22cSMark Brown SNDRV_PCM_FMTBIT_FLOAT64_LE | SNDRV_PCM_FMTBIT_FLOAT64_BE, 3410151ab22cSMark Brown SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_LE 3411151ab22cSMark Brown | SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_BE, 3412151ab22cSMark Brown }; 3413151ab22cSMark Brown 3414151ab22cSMark Brown /* Fix up the DAI formats for endianness: codecs don't actually see 3415151ab22cSMark Brown * the endianness of the data but we're using the CPU format 3416151ab22cSMark Brown * definitions which do need to include endianness so we ensure that 3417151ab22cSMark Brown * codec DAIs always have both big and little endian variants set. 3418151ab22cSMark Brown */ 3419151ab22cSMark Brown static void fixup_codec_formats(struct snd_soc_pcm_stream *stream) 3420151ab22cSMark Brown { 3421151ab22cSMark Brown int i; 3422151ab22cSMark Brown 3423151ab22cSMark Brown for (i = 0; i < ARRAY_SIZE(codec_format_map); i++) 3424151ab22cSMark Brown if (stream->formats & codec_format_map[i]) 3425151ab22cSMark Brown stream->formats |= codec_format_map[i]; 3426151ab22cSMark Brown } 3427151ab22cSMark Brown 34280d0cf00aSMark Brown /** 34290d0cf00aSMark Brown * snd_soc_register_codec - Register a codec with the ASoC core 34300d0cf00aSMark Brown * 3431ac11a2b3SMark Brown * @codec: codec to register 34320d0cf00aSMark Brown */ 3433f0fba2adSLiam Girdwood int snd_soc_register_codec(struct device *dev, 3434f0fba2adSLiam Girdwood struct snd_soc_codec_driver *codec_drv, 3435f0fba2adSLiam Girdwood struct snd_soc_dai_driver *dai_drv, int num_dai) 34360d0cf00aSMark Brown { 3437f0fba2adSLiam Girdwood struct snd_soc_codec *codec; 3438f0fba2adSLiam Girdwood int ret, i; 3439151ab22cSMark Brown 3440f0fba2adSLiam Girdwood dev_dbg(dev, "codec register %s\n", dev_name(dev)); 34410d0cf00aSMark Brown 3442f0fba2adSLiam Girdwood codec = kzalloc(sizeof(struct snd_soc_codec), GFP_KERNEL); 3443f0fba2adSLiam Girdwood if (codec == NULL) 3444f0fba2adSLiam Girdwood return -ENOMEM; 34450d0cf00aSMark Brown 3446f0fba2adSLiam Girdwood /* create CODEC component name */ 3447f0fba2adSLiam Girdwood codec->name = fmt_single_name(dev, &codec->id); 3448f0fba2adSLiam Girdwood if (codec->name == NULL) { 3449f0fba2adSLiam Girdwood kfree(codec); 3450f0fba2adSLiam Girdwood return -ENOMEM; 3451151ab22cSMark Brown } 3452151ab22cSMark Brown 3453*23bbce34SDimitris Papastamos if (codec_drv->compress_type) 3454*23bbce34SDimitris Papastamos codec->compress_type = codec_drv->compress_type; 3455*23bbce34SDimitris Papastamos else 3456*23bbce34SDimitris Papastamos codec->compress_type = SND_SOC_FLAT_COMPRESSION; 3457*23bbce34SDimitris Papastamos 3458ce6120ccSLiam Girdwood INIT_LIST_HEAD(&codec->dapm.widgets); 3459ce6120ccSLiam Girdwood INIT_LIST_HEAD(&codec->dapm.paths); 3460c3acec26SMark Brown codec->write = codec_drv->write; 3461c3acec26SMark Brown codec->read = codec_drv->read; 3462ce6120ccSLiam Girdwood codec->dapm.bias_level = SND_SOC_BIAS_OFF; 3463ce6120ccSLiam Girdwood codec->dapm.dev = dev; 3464ce6120ccSLiam Girdwood codec->dapm.codec = codec; 3465f0fba2adSLiam Girdwood codec->dev = dev; 3466f0fba2adSLiam Girdwood codec->driver = codec_drv; 3467f0fba2adSLiam Girdwood codec->num_dai = num_dai; 3468f0fba2adSLiam Girdwood mutex_init(&codec->mutex); 3469f0fba2adSLiam Girdwood 34707a30a3dbSDimitris Papastamos /* allocate CODEC register cache */ 34717a30a3dbSDimitris Papastamos if (codec_drv->reg_cache_size && codec_drv->reg_word_size) { 34727a30a3dbSDimitris Papastamos ret = snd_soc_cache_init(codec); 34737a30a3dbSDimitris Papastamos if (ret < 0) { 34747a30a3dbSDimitris Papastamos dev_err(codec->dev, "Failed to set cache compression type: %d\n", 34757a30a3dbSDimitris Papastamos ret); 34767a30a3dbSDimitris Papastamos goto error_cache; 34777a30a3dbSDimitris Papastamos } 34787a30a3dbSDimitris Papastamos } 34797a30a3dbSDimitris Papastamos 3480f0fba2adSLiam Girdwood for (i = 0; i < num_dai; i++) { 3481f0fba2adSLiam Girdwood fixup_codec_formats(&dai_drv[i].playback); 3482f0fba2adSLiam Girdwood fixup_codec_formats(&dai_drv[i].capture); 3483f0fba2adSLiam Girdwood } 3484f0fba2adSLiam Girdwood 348526b01ccdSMark Brown /* register any DAIs */ 348626b01ccdSMark Brown if (num_dai) { 3487f0fba2adSLiam Girdwood ret = snd_soc_register_dais(dev, dai_drv, num_dai); 3488f0fba2adSLiam Girdwood if (ret < 0) 34897a30a3dbSDimitris Papastamos goto error_dais; 349026b01ccdSMark Brown } 3491f0fba2adSLiam Girdwood 34920d0cf00aSMark Brown mutex_lock(&client_mutex); 34930d0cf00aSMark Brown list_add(&codec->list, &codec_list); 34940d0cf00aSMark Brown snd_soc_instantiate_cards(); 34950d0cf00aSMark Brown mutex_unlock(&client_mutex); 34960d0cf00aSMark Brown 34970d0cf00aSMark Brown pr_debug("Registered codec '%s'\n", codec->name); 34980d0cf00aSMark Brown return 0; 3499f0fba2adSLiam Girdwood 35007a30a3dbSDimitris Papastamos error_dais: 35017a30a3dbSDimitris Papastamos snd_soc_cache_exit(codec); 35027a30a3dbSDimitris Papastamos error_cache: 3503f0fba2adSLiam Girdwood kfree(codec->name); 3504f0fba2adSLiam Girdwood kfree(codec); 3505f0fba2adSLiam Girdwood return ret; 35060d0cf00aSMark Brown } 35070d0cf00aSMark Brown EXPORT_SYMBOL_GPL(snd_soc_register_codec); 35080d0cf00aSMark Brown 35090d0cf00aSMark Brown /** 35100d0cf00aSMark Brown * snd_soc_unregister_codec - Unregister a codec from the ASoC core 35110d0cf00aSMark Brown * 3512ac11a2b3SMark Brown * @codec: codec to unregister 35130d0cf00aSMark Brown */ 3514f0fba2adSLiam Girdwood void snd_soc_unregister_codec(struct device *dev) 35150d0cf00aSMark Brown { 3516f0fba2adSLiam Girdwood struct snd_soc_codec *codec; 3517f0fba2adSLiam Girdwood int i; 3518f0fba2adSLiam Girdwood 3519f0fba2adSLiam Girdwood list_for_each_entry(codec, &codec_list, list) { 3520f0fba2adSLiam Girdwood if (dev == codec->dev) 3521f0fba2adSLiam Girdwood goto found; 3522f0fba2adSLiam Girdwood } 3523f0fba2adSLiam Girdwood return; 3524f0fba2adSLiam Girdwood 3525f0fba2adSLiam Girdwood found: 352626b01ccdSMark Brown if (codec->num_dai) 3527f0fba2adSLiam Girdwood for (i = 0; i < codec->num_dai; i++) 3528f0fba2adSLiam Girdwood snd_soc_unregister_dai(dev); 3529f0fba2adSLiam Girdwood 35300d0cf00aSMark Brown mutex_lock(&client_mutex); 35310d0cf00aSMark Brown list_del(&codec->list); 35320d0cf00aSMark Brown mutex_unlock(&client_mutex); 35330d0cf00aSMark Brown 35340d0cf00aSMark Brown pr_debug("Unregistered codec '%s'\n", codec->name); 3535f0fba2adSLiam Girdwood 35367a30a3dbSDimitris Papastamos snd_soc_cache_exit(codec); 35371aafcd4dSDimitris Papastamos kfree(codec->name); 3538f0fba2adSLiam Girdwood kfree(codec); 35390d0cf00aSMark Brown } 35400d0cf00aSMark Brown EXPORT_SYMBOL_GPL(snd_soc_unregister_codec); 35410d0cf00aSMark Brown 3542c9b3a40fSTakashi Iwai static int __init snd_soc_init(void) 3543db2a4165SFrank Mandarino { 3544384c89e2SMark Brown #ifdef CONFIG_DEBUG_FS 3545384c89e2SMark Brown debugfs_root = debugfs_create_dir("asoc", NULL); 3546384c89e2SMark Brown if (IS_ERR(debugfs_root) || !debugfs_root) { 3547384c89e2SMark Brown printk(KERN_WARNING 3548384c89e2SMark Brown "ASoC: Failed to create debugfs directory\n"); 3549384c89e2SMark Brown debugfs_root = NULL; 3550384c89e2SMark Brown } 3551c3c5a19aSMark Brown 3552c3c5a19aSMark Brown if (!debugfs_create_file("codecs", 0444, debugfs_root, NULL, 3553c3c5a19aSMark Brown &codec_list_fops)) 3554c3c5a19aSMark Brown pr_warn("ASoC: Failed to create CODEC list debugfs file\n"); 3555c3c5a19aSMark Brown 3556f3208780SMark Brown if (!debugfs_create_file("dais", 0444, debugfs_root, NULL, 3557f3208780SMark Brown &dai_list_fops)) 3558f3208780SMark Brown pr_warn("ASoC: Failed to create DAI list debugfs file\n"); 355919c7ac27SMark Brown 356019c7ac27SMark Brown if (!debugfs_create_file("platforms", 0444, debugfs_root, NULL, 356119c7ac27SMark Brown &platform_list_fops)) 356219c7ac27SMark Brown pr_warn("ASoC: Failed to create platform list debugfs file\n"); 3563384c89e2SMark Brown #endif 3564384c89e2SMark Brown 3565db2a4165SFrank Mandarino return platform_driver_register(&soc_driver); 3566db2a4165SFrank Mandarino } 35674abe8e16SMark Brown module_init(snd_soc_init); 3568db2a4165SFrank Mandarino 35697d8c16a6SMark Brown static void __exit snd_soc_exit(void) 3570db2a4165SFrank Mandarino { 3571384c89e2SMark Brown #ifdef CONFIG_DEBUG_FS 3572384c89e2SMark Brown debugfs_remove_recursive(debugfs_root); 3573384c89e2SMark Brown #endif 3574db2a4165SFrank Mandarino platform_driver_unregister(&soc_driver); 3575db2a4165SFrank Mandarino } 3576db2a4165SFrank Mandarino module_exit(snd_soc_exit); 3577db2a4165SFrank Mandarino 3578db2a4165SFrank Mandarino /* Module information */ 3579d331124dSLiam Girdwood MODULE_AUTHOR("Liam Girdwood, lrg@slimlogic.co.uk"); 3580db2a4165SFrank Mandarino MODULE_DESCRIPTION("ALSA SoC Core"); 3581db2a4165SFrank Mandarino MODULE_LICENSE("GPL"); 35828b45a209SKay Sievers MODULE_ALIAS("platform:soc-audio"); 3583