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> 363028eb8cSMark Brown #include <sound/jack.h> 37db2a4165SFrank Mandarino #include <sound/pcm.h> 38db2a4165SFrank Mandarino #include <sound/pcm_params.h> 39db2a4165SFrank Mandarino #include <sound/soc.h> 40db2a4165SFrank Mandarino #include <sound/initval.h> 41db2a4165SFrank Mandarino 42a8b1d34fSMark Brown #define CREATE_TRACE_POINTS 43a8b1d34fSMark Brown #include <trace/events/asoc.h> 44a8b1d34fSMark Brown 45f0fba2adSLiam Girdwood #define NAME_SIZE 32 46f0fba2adSLiam Girdwood 47db2a4165SFrank Mandarino static DEFINE_MUTEX(pcm_mutex); 48db2a4165SFrank Mandarino static DECLARE_WAIT_QUEUE_HEAD(soc_pm_waitq); 49db2a4165SFrank Mandarino 50384c89e2SMark Brown #ifdef CONFIG_DEBUG_FS 518a9dab1aSMark Brown struct dentry *snd_soc_debugfs_root; 528a9dab1aSMark Brown EXPORT_SYMBOL_GPL(snd_soc_debugfs_root); 53384c89e2SMark Brown #endif 54384c89e2SMark Brown 55c5af3a2eSMark Brown static DEFINE_MUTEX(client_mutex); 56c5af3a2eSMark Brown static LIST_HEAD(card_list); 579115171aSMark Brown static LIST_HEAD(dai_list); 5812a48a8cSMark Brown static LIST_HEAD(platform_list); 590d0cf00aSMark Brown static LIST_HEAD(codec_list); 60c5af3a2eSMark Brown 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 722bc9a81eSDimitris Papastamos /* returns the minimum number of bytes needed to represent 732bc9a81eSDimitris Papastamos * a particular given value */ 742bc9a81eSDimitris Papastamos static int min_bytes_needed(unsigned long val) 752bc9a81eSDimitris Papastamos { 762bc9a81eSDimitris Papastamos int c = 0; 772bc9a81eSDimitris Papastamos int i; 782bc9a81eSDimitris Papastamos 792bc9a81eSDimitris Papastamos for (i = (sizeof val * 8) - 1; i >= 0; --i, ++c) 802bc9a81eSDimitris Papastamos if (val & (1UL << i)) 812bc9a81eSDimitris Papastamos break; 822bc9a81eSDimitris Papastamos c = (sizeof val * 8) - c; 832bc9a81eSDimitris Papastamos if (!c || (c % 8)) 842bc9a81eSDimitris Papastamos c = (c + 8) / 8; 852bc9a81eSDimitris Papastamos else 862bc9a81eSDimitris Papastamos c /= 8; 872bc9a81eSDimitris Papastamos return c; 882bc9a81eSDimitris Papastamos } 892bc9a81eSDimitris Papastamos 9013fd179fSDimitris Papastamos /* fill buf which is 'len' bytes with a formatted 9113fd179fSDimitris Papastamos * string of the form 'reg: value\n' */ 9213fd179fSDimitris Papastamos static int format_register_str(struct snd_soc_codec *codec, 9313fd179fSDimitris Papastamos unsigned int reg, char *buf, size_t len) 942624d5faSMark Brown { 9513fd179fSDimitris Papastamos int wordsize = codec->driver->reg_word_size * 2; 9613fd179fSDimitris Papastamos int regsize = min_bytes_needed(codec->driver->reg_cache_size) * 2; 9713fd179fSDimitris Papastamos int ret; 9813fd179fSDimitris Papastamos char tmpbuf[len + 1]; 9913fd179fSDimitris Papastamos char regbuf[regsize + 1]; 10013fd179fSDimitris Papastamos 10113fd179fSDimitris Papastamos /* since tmpbuf is allocated on the stack, warn the callers if they 10213fd179fSDimitris Papastamos * try to abuse this function */ 10313fd179fSDimitris Papastamos WARN_ON(len > 63); 10413fd179fSDimitris Papastamos 10513fd179fSDimitris Papastamos /* +2 for ': ' and + 1 for '\n' */ 10613fd179fSDimitris Papastamos if (wordsize + regsize + 2 + 1 != len) 10713fd179fSDimitris Papastamos return -EINVAL; 10813fd179fSDimitris Papastamos 10913fd179fSDimitris Papastamos ret = snd_soc_read(codec , reg); 11013fd179fSDimitris Papastamos if (ret < 0) { 11113fd179fSDimitris Papastamos memset(regbuf, 'X', regsize); 11213fd179fSDimitris Papastamos regbuf[regsize] = '\0'; 11313fd179fSDimitris Papastamos } else { 11413fd179fSDimitris Papastamos snprintf(regbuf, regsize + 1, "%.*x", regsize, ret); 11513fd179fSDimitris Papastamos } 11613fd179fSDimitris Papastamos 11713fd179fSDimitris Papastamos /* prepare the buffer */ 11813fd179fSDimitris Papastamos snprintf(tmpbuf, len + 1, "%.*x: %s\n", wordsize, reg, regbuf); 11913fd179fSDimitris Papastamos /* copy it back to the caller without the '\0' */ 12013fd179fSDimitris Papastamos memcpy(buf, tmpbuf, len); 12113fd179fSDimitris Papastamos 12213fd179fSDimitris Papastamos return 0; 12313fd179fSDimitris Papastamos } 12413fd179fSDimitris Papastamos 12513fd179fSDimitris Papastamos /* codec register dump */ 12613fd179fSDimitris Papastamos static ssize_t soc_codec_reg_show(struct snd_soc_codec *codec, char *buf, 12713fd179fSDimitris Papastamos size_t count, loff_t pos) 12813fd179fSDimitris Papastamos { 12913fd179fSDimitris Papastamos int i, step = 1; 1302bc9a81eSDimitris Papastamos int wordsize, regsize; 13113fd179fSDimitris Papastamos int len; 13213fd179fSDimitris Papastamos size_t total = 0; 13313fd179fSDimitris Papastamos loff_t p = 0; 1342bc9a81eSDimitris Papastamos 1352bc9a81eSDimitris Papastamos wordsize = codec->driver->reg_word_size * 2; 1362bc9a81eSDimitris Papastamos regsize = min_bytes_needed(codec->driver->reg_cache_size) * 2; 1372624d5faSMark Brown 13813fd179fSDimitris Papastamos len = wordsize + regsize + 2 + 1; 13913fd179fSDimitris Papastamos 140f0fba2adSLiam Girdwood if (!codec->driver->reg_cache_size) 1412624d5faSMark Brown return 0; 1422624d5faSMark Brown 143f0fba2adSLiam Girdwood if (codec->driver->reg_cache_step) 144f0fba2adSLiam Girdwood step = codec->driver->reg_cache_step; 1452624d5faSMark Brown 146f0fba2adSLiam Girdwood for (i = 0; i < codec->driver->reg_cache_size; i += step) { 1471500b7b5SDimitris Papastamos if (codec->readable_register && !codec->readable_register(codec, i)) 1482624d5faSMark Brown continue; 149f0fba2adSLiam Girdwood if (codec->driver->display_register) { 150f0fba2adSLiam Girdwood count += codec->driver->display_register(codec, buf + count, 1512624d5faSMark Brown PAGE_SIZE - count, i); 1525164d74dSMark Brown } else { 15313fd179fSDimitris Papastamos /* only support larger than PAGE_SIZE bytes debugfs 15413fd179fSDimitris Papastamos * entries for the default case */ 15513fd179fSDimitris Papastamos if (p >= pos) { 15613fd179fSDimitris Papastamos if (total + len >= count - 1) 1572624d5faSMark Brown break; 15813fd179fSDimitris Papastamos format_register_str(codec, i, buf + total, len); 15913fd179fSDimitris Papastamos total += len; 16013fd179fSDimitris Papastamos } 16113fd179fSDimitris Papastamos p += len; 16213fd179fSDimitris Papastamos } 1632624d5faSMark Brown } 1642624d5faSMark Brown 16513fd179fSDimitris Papastamos total = min(total, count - 1); 1662624d5faSMark Brown 16713fd179fSDimitris Papastamos return total; 1682624d5faSMark Brown } 16913fd179fSDimitris Papastamos 1702624d5faSMark Brown static ssize_t codec_reg_show(struct device *dev, 1712624d5faSMark Brown struct device_attribute *attr, char *buf) 1722624d5faSMark Brown { 173f0fba2adSLiam Girdwood struct snd_soc_pcm_runtime *rtd = 174f0fba2adSLiam Girdwood container_of(dev, struct snd_soc_pcm_runtime, dev); 175f0fba2adSLiam Girdwood 17613fd179fSDimitris Papastamos return soc_codec_reg_show(rtd->codec, buf, PAGE_SIZE, 0); 1772624d5faSMark Brown } 1782624d5faSMark Brown 1792624d5faSMark Brown static DEVICE_ATTR(codec_reg, 0444, codec_reg_show, NULL); 1802624d5faSMark Brown 181dbe21408SMark Brown static ssize_t pmdown_time_show(struct device *dev, 182dbe21408SMark Brown struct device_attribute *attr, char *buf) 183dbe21408SMark Brown { 184f0fba2adSLiam Girdwood struct snd_soc_pcm_runtime *rtd = 185f0fba2adSLiam Girdwood container_of(dev, struct snd_soc_pcm_runtime, dev); 186dbe21408SMark Brown 187f0fba2adSLiam Girdwood return sprintf(buf, "%ld\n", rtd->pmdown_time); 188dbe21408SMark Brown } 189dbe21408SMark Brown 190dbe21408SMark Brown static ssize_t pmdown_time_set(struct device *dev, 191dbe21408SMark Brown struct device_attribute *attr, 192dbe21408SMark Brown const char *buf, size_t count) 193dbe21408SMark Brown { 194f0fba2adSLiam Girdwood struct snd_soc_pcm_runtime *rtd = 195f0fba2adSLiam Girdwood container_of(dev, struct snd_soc_pcm_runtime, dev); 196c593b520SMark Brown int ret; 197dbe21408SMark Brown 198c593b520SMark Brown ret = strict_strtol(buf, 10, &rtd->pmdown_time); 199c593b520SMark Brown if (ret) 200c593b520SMark Brown return ret; 201dbe21408SMark Brown 202dbe21408SMark Brown return count; 203dbe21408SMark Brown } 204dbe21408SMark Brown 205dbe21408SMark Brown static DEVICE_ATTR(pmdown_time, 0644, pmdown_time_show, pmdown_time_set); 206dbe21408SMark Brown 2072624d5faSMark Brown #ifdef CONFIG_DEBUG_FS 2082624d5faSMark Brown static int codec_reg_open_file(struct inode *inode, struct file *file) 2092624d5faSMark Brown { 2102624d5faSMark Brown file->private_data = inode->i_private; 2112624d5faSMark Brown return 0; 2122624d5faSMark Brown } 2132624d5faSMark Brown 2142624d5faSMark Brown static ssize_t codec_reg_read_file(struct file *file, char __user *user_buf, 2152624d5faSMark Brown size_t count, loff_t *ppos) 2162624d5faSMark Brown { 2172624d5faSMark Brown ssize_t ret; 2182624d5faSMark Brown struct snd_soc_codec *codec = file->private_data; 21913fd179fSDimitris Papastamos char *buf; 22013fd179fSDimitris Papastamos 22113fd179fSDimitris Papastamos if (*ppos < 0 || !count) 22213fd179fSDimitris Papastamos return -EINVAL; 22313fd179fSDimitris Papastamos 22413fd179fSDimitris Papastamos buf = kmalloc(count, GFP_KERNEL); 2252624d5faSMark Brown if (!buf) 2262624d5faSMark Brown return -ENOMEM; 22713fd179fSDimitris Papastamos 22813fd179fSDimitris Papastamos ret = soc_codec_reg_show(codec, buf, count, *ppos); 22913fd179fSDimitris Papastamos if (ret >= 0) { 23013fd179fSDimitris Papastamos if (copy_to_user(user_buf, buf, ret)) { 23113fd179fSDimitris Papastamos kfree(buf); 23213fd179fSDimitris Papastamos return -EFAULT; 23313fd179fSDimitris Papastamos } 23413fd179fSDimitris Papastamos *ppos += ret; 23513fd179fSDimitris Papastamos } 23613fd179fSDimitris Papastamos 2372624d5faSMark Brown kfree(buf); 2382624d5faSMark Brown return ret; 2392624d5faSMark Brown } 2402624d5faSMark Brown 2412624d5faSMark Brown static ssize_t codec_reg_write_file(struct file *file, 2422624d5faSMark Brown const char __user *user_buf, size_t count, loff_t *ppos) 2432624d5faSMark Brown { 2442624d5faSMark Brown char buf[32]; 2452624d5faSMark Brown int buf_size; 2462624d5faSMark Brown char *start = buf; 2472624d5faSMark Brown unsigned long reg, value; 2482624d5faSMark Brown int step = 1; 2492624d5faSMark Brown struct snd_soc_codec *codec = file->private_data; 2502624d5faSMark Brown 2512624d5faSMark Brown buf_size = min(count, (sizeof(buf)-1)); 2522624d5faSMark Brown if (copy_from_user(buf, user_buf, buf_size)) 2532624d5faSMark Brown return -EFAULT; 2542624d5faSMark Brown buf[buf_size] = 0; 2552624d5faSMark Brown 256f0fba2adSLiam Girdwood if (codec->driver->reg_cache_step) 257f0fba2adSLiam Girdwood step = codec->driver->reg_cache_step; 2582624d5faSMark Brown 2592624d5faSMark Brown while (*start == ' ') 2602624d5faSMark Brown start++; 2612624d5faSMark Brown reg = simple_strtoul(start, &start, 16); 262f0fba2adSLiam Girdwood if ((reg >= codec->driver->reg_cache_size) || (reg % step)) 2632624d5faSMark Brown return -EINVAL; 2642624d5faSMark Brown while (*start == ' ') 2652624d5faSMark Brown start++; 2662624d5faSMark Brown if (strict_strtoul(start, 16, &value)) 2672624d5faSMark Brown return -EINVAL; 2680d51a9cbSMark Brown 2690d51a9cbSMark Brown /* Userspace has been fiddling around behind the kernel's back */ 2700d51a9cbSMark Brown add_taint(TAINT_USER); 2710d51a9cbSMark Brown 272e4f078d8SDimitris Papastamos snd_soc_write(codec, reg, value); 2732624d5faSMark Brown return buf_size; 2742624d5faSMark Brown } 2752624d5faSMark Brown 2762624d5faSMark Brown static const struct file_operations codec_reg_fops = { 2772624d5faSMark Brown .open = codec_reg_open_file, 2782624d5faSMark Brown .read = codec_reg_read_file, 2792624d5faSMark Brown .write = codec_reg_write_file, 2806038f373SArnd Bergmann .llseek = default_llseek, 2812624d5faSMark Brown }; 2822624d5faSMark Brown 2832624d5faSMark Brown static void soc_init_codec_debugfs(struct snd_soc_codec *codec) 2842624d5faSMark Brown { 285d6ce4cf3SJarkko Nikula struct dentry *debugfs_card_root = codec->card->debugfs_card_root; 286d6ce4cf3SJarkko Nikula 2876ba6c9c3SMark Brown codec->debugfs_codec_root = debugfs_create_dir(codec->name, 288d6ce4cf3SJarkko Nikula debugfs_card_root); 2892624d5faSMark Brown if (!codec->debugfs_codec_root) { 2902624d5faSMark Brown printk(KERN_WARNING 2912624d5faSMark Brown "ASoC: Failed to create codec debugfs directory\n"); 2922624d5faSMark Brown return; 2932624d5faSMark Brown } 2942624d5faSMark Brown 295aaee8ef1SMark Brown debugfs_create_bool("cache_sync", 0444, codec->debugfs_codec_root, 296aaee8ef1SMark Brown &codec->cache_sync); 297aaee8ef1SMark Brown debugfs_create_bool("cache_only", 0444, codec->debugfs_codec_root, 298aaee8ef1SMark Brown &codec->cache_only); 299aaee8ef1SMark Brown 3002624d5faSMark Brown codec->debugfs_reg = debugfs_create_file("codec_reg", 0644, 3012624d5faSMark Brown codec->debugfs_codec_root, 3022624d5faSMark Brown codec, &codec_reg_fops); 3032624d5faSMark Brown if (!codec->debugfs_reg) 3042624d5faSMark Brown printk(KERN_WARNING 3052624d5faSMark Brown "ASoC: Failed to create codec register debugfs file\n"); 3062624d5faSMark Brown 307ce6120ccSLiam Girdwood codec->dapm.debugfs_dapm = debugfs_create_dir("dapm", 3082624d5faSMark Brown codec->debugfs_codec_root); 309ce6120ccSLiam Girdwood if (!codec->dapm.debugfs_dapm) 3102624d5faSMark Brown printk(KERN_WARNING 3112624d5faSMark Brown "Failed to create DAPM debugfs directory\n"); 3122624d5faSMark Brown 313ce6120ccSLiam Girdwood snd_soc_dapm_debugfs_init(&codec->dapm); 3142624d5faSMark Brown } 3152624d5faSMark Brown 3162624d5faSMark Brown static void soc_cleanup_codec_debugfs(struct snd_soc_codec *codec) 3172624d5faSMark Brown { 3182624d5faSMark Brown debugfs_remove_recursive(codec->debugfs_codec_root); 3192624d5faSMark Brown } 3202624d5faSMark Brown 321c3c5a19aSMark Brown static ssize_t codec_list_read_file(struct file *file, char __user *user_buf, 322c3c5a19aSMark Brown size_t count, loff_t *ppos) 323c3c5a19aSMark Brown { 324c3c5a19aSMark Brown char *buf = kmalloc(PAGE_SIZE, GFP_KERNEL); 3252b194f9dSMark Brown ssize_t len, ret = 0; 326c3c5a19aSMark Brown struct snd_soc_codec *codec; 327c3c5a19aSMark Brown 328c3c5a19aSMark Brown if (!buf) 329c3c5a19aSMark Brown return -ENOMEM; 330c3c5a19aSMark Brown 3312b194f9dSMark Brown list_for_each_entry(codec, &codec_list, list) { 3322b194f9dSMark Brown len = snprintf(buf + ret, PAGE_SIZE - ret, "%s\n", 333c3c5a19aSMark Brown codec->name); 3342b194f9dSMark Brown if (len >= 0) 3352b194f9dSMark Brown ret += len; 3362b194f9dSMark Brown if (ret > PAGE_SIZE) { 3372b194f9dSMark Brown ret = PAGE_SIZE; 3382b194f9dSMark Brown break; 3392b194f9dSMark Brown } 3402b194f9dSMark Brown } 341c3c5a19aSMark Brown 342c3c5a19aSMark Brown if (ret >= 0) 343c3c5a19aSMark Brown ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret); 344c3c5a19aSMark Brown 345c3c5a19aSMark Brown kfree(buf); 346c3c5a19aSMark Brown 347c3c5a19aSMark Brown return ret; 348c3c5a19aSMark Brown } 349c3c5a19aSMark Brown 350c3c5a19aSMark Brown static const struct file_operations codec_list_fops = { 351c3c5a19aSMark Brown .read = codec_list_read_file, 352c3c5a19aSMark Brown .llseek = default_llseek,/* read accesses f_pos */ 353c3c5a19aSMark Brown }; 354c3c5a19aSMark Brown 355f3208780SMark Brown static ssize_t dai_list_read_file(struct file *file, char __user *user_buf, 356f3208780SMark Brown size_t count, loff_t *ppos) 357f3208780SMark Brown { 358f3208780SMark Brown char *buf = kmalloc(PAGE_SIZE, GFP_KERNEL); 3592b194f9dSMark Brown ssize_t len, ret = 0; 360f3208780SMark Brown struct snd_soc_dai *dai; 361f3208780SMark Brown 362f3208780SMark Brown if (!buf) 363f3208780SMark Brown return -ENOMEM; 364f3208780SMark Brown 3652b194f9dSMark Brown list_for_each_entry(dai, &dai_list, list) { 3662b194f9dSMark Brown len = snprintf(buf + ret, PAGE_SIZE - ret, "%s\n", dai->name); 3672b194f9dSMark Brown if (len >= 0) 3682b194f9dSMark Brown ret += len; 3692b194f9dSMark Brown if (ret > PAGE_SIZE) { 3702b194f9dSMark Brown ret = PAGE_SIZE; 3712b194f9dSMark Brown break; 3722b194f9dSMark Brown } 3732b194f9dSMark Brown } 374f3208780SMark Brown 375f3208780SMark Brown ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret); 376f3208780SMark Brown 377f3208780SMark Brown kfree(buf); 378f3208780SMark Brown 379f3208780SMark Brown return ret; 380f3208780SMark Brown } 381f3208780SMark Brown 382f3208780SMark Brown static const struct file_operations dai_list_fops = { 383f3208780SMark Brown .read = dai_list_read_file, 384f3208780SMark Brown .llseek = default_llseek,/* read accesses f_pos */ 385f3208780SMark Brown }; 386f3208780SMark Brown 38719c7ac27SMark Brown static ssize_t platform_list_read_file(struct file *file, 38819c7ac27SMark Brown char __user *user_buf, 38919c7ac27SMark Brown size_t count, loff_t *ppos) 39019c7ac27SMark Brown { 39119c7ac27SMark Brown char *buf = kmalloc(PAGE_SIZE, GFP_KERNEL); 3922b194f9dSMark Brown ssize_t len, ret = 0; 39319c7ac27SMark Brown struct snd_soc_platform *platform; 39419c7ac27SMark Brown 39519c7ac27SMark Brown if (!buf) 39619c7ac27SMark Brown return -ENOMEM; 39719c7ac27SMark Brown 3982b194f9dSMark Brown list_for_each_entry(platform, &platform_list, list) { 3992b194f9dSMark Brown len = snprintf(buf + ret, PAGE_SIZE - ret, "%s\n", 40019c7ac27SMark Brown platform->name); 4012b194f9dSMark Brown if (len >= 0) 4022b194f9dSMark Brown ret += len; 4032b194f9dSMark Brown if (ret > PAGE_SIZE) { 4042b194f9dSMark Brown ret = PAGE_SIZE; 4052b194f9dSMark Brown break; 4062b194f9dSMark Brown } 4072b194f9dSMark Brown } 40819c7ac27SMark Brown 40919c7ac27SMark Brown ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret); 41019c7ac27SMark Brown 41119c7ac27SMark Brown kfree(buf); 41219c7ac27SMark Brown 41319c7ac27SMark Brown return ret; 41419c7ac27SMark Brown } 41519c7ac27SMark Brown 41619c7ac27SMark Brown static const struct file_operations platform_list_fops = { 41719c7ac27SMark Brown .read = platform_list_read_file, 41819c7ac27SMark Brown .llseek = default_llseek,/* read accesses f_pos */ 41919c7ac27SMark Brown }; 42019c7ac27SMark Brown 421a6052154SJarkko Nikula static void soc_init_card_debugfs(struct snd_soc_card *card) 422a6052154SJarkko Nikula { 423a6052154SJarkko Nikula card->debugfs_card_root = debugfs_create_dir(card->name, 4248a9dab1aSMark Brown snd_soc_debugfs_root); 4253a45b867SJarkko Nikula if (!card->debugfs_card_root) { 426a6052154SJarkko Nikula dev_warn(card->dev, 427a6052154SJarkko Nikula "ASoC: Failed to create codec debugfs directory\n"); 4283a45b867SJarkko Nikula return; 4293a45b867SJarkko Nikula } 4303a45b867SJarkko Nikula 4313a45b867SJarkko Nikula card->debugfs_pop_time = debugfs_create_u32("dapm_pop_time", 0644, 4323a45b867SJarkko Nikula card->debugfs_card_root, 4333a45b867SJarkko Nikula &card->pop_time); 4343a45b867SJarkko Nikula if (!card->debugfs_pop_time) 4353a45b867SJarkko Nikula dev_warn(card->dev, 4363a45b867SJarkko Nikula "Failed to create pop time debugfs file\n"); 437a6052154SJarkko Nikula } 438a6052154SJarkko Nikula 439a6052154SJarkko Nikula static void soc_cleanup_card_debugfs(struct snd_soc_card *card) 440a6052154SJarkko Nikula { 441a6052154SJarkko Nikula debugfs_remove_recursive(card->debugfs_card_root); 442a6052154SJarkko Nikula } 443a6052154SJarkko Nikula 4442624d5faSMark Brown #else 4452624d5faSMark Brown 4462624d5faSMark Brown static inline void soc_init_codec_debugfs(struct snd_soc_codec *codec) 4472624d5faSMark Brown { 4482624d5faSMark Brown } 4492624d5faSMark Brown 4502624d5faSMark Brown static inline void soc_cleanup_codec_debugfs(struct snd_soc_codec *codec) 4512624d5faSMark Brown { 4522624d5faSMark Brown } 453b95fccbcSAxel Lin 454b95fccbcSAxel Lin static inline void soc_init_card_debugfs(struct snd_soc_card *card) 455b95fccbcSAxel Lin { 456b95fccbcSAxel Lin } 457b95fccbcSAxel Lin 458b95fccbcSAxel Lin static inline void soc_cleanup_card_debugfs(struct snd_soc_card *card) 459b95fccbcSAxel Lin { 460b95fccbcSAxel Lin } 4612624d5faSMark Brown #endif 4622624d5faSMark Brown 463db2a4165SFrank Mandarino #ifdef CONFIG_SND_SOC_AC97_BUS 464db2a4165SFrank Mandarino /* unregister ac97 codec */ 465db2a4165SFrank Mandarino static int soc_ac97_dev_unregister(struct snd_soc_codec *codec) 466db2a4165SFrank Mandarino { 467db2a4165SFrank Mandarino if (codec->ac97->dev.bus) 468db2a4165SFrank Mandarino device_unregister(&codec->ac97->dev); 469db2a4165SFrank Mandarino return 0; 470db2a4165SFrank Mandarino } 471db2a4165SFrank Mandarino 472db2a4165SFrank Mandarino /* stop no dev release warning */ 473db2a4165SFrank Mandarino static void soc_ac97_device_release(struct device *dev){} 474db2a4165SFrank Mandarino 475db2a4165SFrank Mandarino /* register ac97 codec to bus */ 476db2a4165SFrank Mandarino static int soc_ac97_dev_register(struct snd_soc_codec *codec) 477db2a4165SFrank Mandarino { 478db2a4165SFrank Mandarino int err; 479db2a4165SFrank Mandarino 480db2a4165SFrank Mandarino codec->ac97->dev.bus = &ac97_bus_type; 4814ac5c61fSMark Brown codec->ac97->dev.parent = codec->card->dev; 482db2a4165SFrank Mandarino codec->ac97->dev.release = soc_ac97_device_release; 483db2a4165SFrank Mandarino 484bb072bf0SKay Sievers dev_set_name(&codec->ac97->dev, "%d-%d:%s", 485f0fba2adSLiam Girdwood codec->card->snd_card->number, 0, codec->name); 486db2a4165SFrank Mandarino err = device_register(&codec->ac97->dev); 487db2a4165SFrank Mandarino if (err < 0) { 488db2a4165SFrank Mandarino snd_printk(KERN_ERR "Can't register ac97 bus\n"); 489db2a4165SFrank Mandarino codec->ac97->dev.bus = NULL; 490db2a4165SFrank Mandarino return err; 491db2a4165SFrank Mandarino } 492db2a4165SFrank Mandarino return 0; 493db2a4165SFrank Mandarino } 494db2a4165SFrank Mandarino #endif 495db2a4165SFrank Mandarino 49606f409d7SMark Brown static int soc_pcm_apply_symmetry(struct snd_pcm_substream *substream) 49706f409d7SMark Brown { 49806f409d7SMark Brown struct snd_soc_pcm_runtime *rtd = substream->private_data; 499f0fba2adSLiam Girdwood struct snd_soc_dai *cpu_dai = rtd->cpu_dai; 500f0fba2adSLiam Girdwood struct snd_soc_dai *codec_dai = rtd->codec_dai; 50106f409d7SMark Brown int ret; 50206f409d7SMark Brown 5034f333b20SMark Brown if (!codec_dai->driver->symmetric_rates && 5044f333b20SMark Brown !cpu_dai->driver->symmetric_rates && 5054f333b20SMark Brown !rtd->dai_link->symmetric_rates) 5064f333b20SMark Brown return 0; 5074f333b20SMark Brown 508c4ef8786SMark Brown /* This can happen if multiple streams are starting simultaneously - 509c4ef8786SMark Brown * the second can need to get its constraints before the first has 510c4ef8786SMark Brown * picked a rate. Complain and allow the application to carry on. 511c4ef8786SMark Brown */ 512c4ef8786SMark Brown if (!rtd->rate) { 513c4ef8786SMark Brown dev_warn(&rtd->dev, 514c4ef8786SMark Brown "Not enforcing symmetric_rates due to race\n"); 515c4ef8786SMark Brown return 0; 516c4ef8786SMark Brown } 517c4ef8786SMark Brown 5184f333b20SMark Brown dev_dbg(&rtd->dev, "Symmetry forces %dHz rate\n", rtd->rate); 51906f409d7SMark Brown 52006f409d7SMark Brown ret = snd_pcm_hw_constraint_minmax(substream->runtime, 52106f409d7SMark Brown SNDRV_PCM_HW_PARAM_RATE, 5224f333b20SMark Brown rtd->rate, rtd->rate); 52306f409d7SMark Brown if (ret < 0) { 524f0fba2adSLiam Girdwood dev_err(&rtd->dev, 52506f409d7SMark Brown "Unable to apply rate symmetry constraint: %d\n", ret); 52606f409d7SMark Brown return ret; 52706f409d7SMark Brown } 52806f409d7SMark Brown 52906f409d7SMark Brown return 0; 53006f409d7SMark Brown } 53106f409d7SMark Brown 532db2a4165SFrank Mandarino /* 533db2a4165SFrank Mandarino * Called by ALSA when a PCM substream is opened, the runtime->hw record is 534db2a4165SFrank Mandarino * then initialized and any private data can be allocated. This also calls 535db2a4165SFrank Mandarino * startup for the cpu DAI, platform, machine and codec DAI. 536db2a4165SFrank Mandarino */ 537db2a4165SFrank Mandarino static int soc_pcm_open(struct snd_pcm_substream *substream) 538db2a4165SFrank Mandarino { 539db2a4165SFrank Mandarino struct snd_soc_pcm_runtime *rtd = substream->private_data; 540db2a4165SFrank Mandarino struct snd_pcm_runtime *runtime = substream->runtime; 541f0fba2adSLiam Girdwood struct snd_soc_platform *platform = rtd->platform; 542f0fba2adSLiam Girdwood struct snd_soc_dai *cpu_dai = rtd->cpu_dai; 543f0fba2adSLiam Girdwood struct snd_soc_dai *codec_dai = rtd->codec_dai; 544f0fba2adSLiam Girdwood struct snd_soc_dai_driver *cpu_dai_drv = cpu_dai->driver; 545f0fba2adSLiam Girdwood struct snd_soc_dai_driver *codec_dai_drv = codec_dai->driver; 546db2a4165SFrank Mandarino int ret = 0; 547db2a4165SFrank Mandarino 548db2a4165SFrank Mandarino mutex_lock(&pcm_mutex); 549db2a4165SFrank Mandarino 550db2a4165SFrank Mandarino /* startup the audio subsystem */ 551f0fba2adSLiam Girdwood if (cpu_dai->driver->ops->startup) { 552f0fba2adSLiam Girdwood ret = cpu_dai->driver->ops->startup(substream, cpu_dai); 553db2a4165SFrank Mandarino if (ret < 0) { 554db2a4165SFrank Mandarino printk(KERN_ERR "asoc: can't open interface %s\n", 555cb666e5bSLiam Girdwood cpu_dai->name); 556db2a4165SFrank Mandarino goto out; 557db2a4165SFrank Mandarino } 558db2a4165SFrank Mandarino } 559db2a4165SFrank Mandarino 560f0fba2adSLiam Girdwood if (platform->driver->ops->open) { 561f0fba2adSLiam Girdwood ret = platform->driver->ops->open(substream); 562db2a4165SFrank Mandarino if (ret < 0) { 563db2a4165SFrank Mandarino printk(KERN_ERR "asoc: can't open platform %s\n", platform->name); 564db2a4165SFrank Mandarino goto platform_err; 565db2a4165SFrank Mandarino } 566db2a4165SFrank Mandarino } 567db2a4165SFrank Mandarino 568f0fba2adSLiam Girdwood if (codec_dai->driver->ops->startup) { 569f0fba2adSLiam Girdwood ret = codec_dai->driver->ops->startup(substream, codec_dai); 570cb666e5bSLiam Girdwood if (ret < 0) { 571cb666e5bSLiam Girdwood printk(KERN_ERR "asoc: can't open codec %s\n", 572cb666e5bSLiam Girdwood codec_dai->name); 573cb666e5bSLiam Girdwood goto codec_dai_err; 574cb666e5bSLiam Girdwood } 575cb666e5bSLiam Girdwood } 576cb666e5bSLiam Girdwood 577f0fba2adSLiam Girdwood if (rtd->dai_link->ops && rtd->dai_link->ops->startup) { 578f0fba2adSLiam Girdwood ret = rtd->dai_link->ops->startup(substream); 579db2a4165SFrank Mandarino if (ret < 0) { 580f0fba2adSLiam Girdwood printk(KERN_ERR "asoc: %s startup failed\n", rtd->dai_link->name); 581db2a4165SFrank Mandarino goto machine_err; 582db2a4165SFrank Mandarino } 583db2a4165SFrank Mandarino } 584db2a4165SFrank Mandarino 585a00f90f9SMark Brown /* Check that the codec and cpu DAIs are compatible */ 586db2a4165SFrank Mandarino if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) { 587db2a4165SFrank Mandarino runtime->hw.rate_min = 588f0fba2adSLiam Girdwood max(codec_dai_drv->playback.rate_min, 589f0fba2adSLiam Girdwood cpu_dai_drv->playback.rate_min); 590db2a4165SFrank Mandarino runtime->hw.rate_max = 591f0fba2adSLiam Girdwood min(codec_dai_drv->playback.rate_max, 592f0fba2adSLiam Girdwood cpu_dai_drv->playback.rate_max); 593db2a4165SFrank Mandarino runtime->hw.channels_min = 594f0fba2adSLiam Girdwood max(codec_dai_drv->playback.channels_min, 595f0fba2adSLiam Girdwood cpu_dai_drv->playback.channels_min); 596db2a4165SFrank Mandarino runtime->hw.channels_max = 597f0fba2adSLiam Girdwood min(codec_dai_drv->playback.channels_max, 598f0fba2adSLiam Girdwood cpu_dai_drv->playback.channels_max); 599cb666e5bSLiam Girdwood runtime->hw.formats = 600f0fba2adSLiam Girdwood codec_dai_drv->playback.formats & cpu_dai_drv->playback.formats; 601cb666e5bSLiam Girdwood runtime->hw.rates = 602f0fba2adSLiam Girdwood codec_dai_drv->playback.rates & cpu_dai_drv->playback.rates; 603f0fba2adSLiam Girdwood if (codec_dai_drv->playback.rates 604d9ad6296SJassi Brar & (SNDRV_PCM_RATE_KNOT | SNDRV_PCM_RATE_CONTINUOUS)) 605f0fba2adSLiam Girdwood runtime->hw.rates |= cpu_dai_drv->playback.rates; 606f0fba2adSLiam Girdwood if (cpu_dai_drv->playback.rates 607d9ad6296SJassi Brar & (SNDRV_PCM_RATE_KNOT | SNDRV_PCM_RATE_CONTINUOUS)) 608f0fba2adSLiam Girdwood runtime->hw.rates |= codec_dai_drv->playback.rates; 609db2a4165SFrank Mandarino } else { 610db2a4165SFrank Mandarino runtime->hw.rate_min = 611f0fba2adSLiam Girdwood max(codec_dai_drv->capture.rate_min, 612f0fba2adSLiam Girdwood cpu_dai_drv->capture.rate_min); 613db2a4165SFrank Mandarino runtime->hw.rate_max = 614f0fba2adSLiam Girdwood min(codec_dai_drv->capture.rate_max, 615f0fba2adSLiam Girdwood cpu_dai_drv->capture.rate_max); 616db2a4165SFrank Mandarino runtime->hw.channels_min = 617f0fba2adSLiam Girdwood max(codec_dai_drv->capture.channels_min, 618f0fba2adSLiam Girdwood cpu_dai_drv->capture.channels_min); 619db2a4165SFrank Mandarino runtime->hw.channels_max = 620f0fba2adSLiam Girdwood min(codec_dai_drv->capture.channels_max, 621f0fba2adSLiam Girdwood cpu_dai_drv->capture.channels_max); 622cb666e5bSLiam Girdwood runtime->hw.formats = 623f0fba2adSLiam Girdwood codec_dai_drv->capture.formats & cpu_dai_drv->capture.formats; 624cb666e5bSLiam Girdwood runtime->hw.rates = 625f0fba2adSLiam Girdwood codec_dai_drv->capture.rates & cpu_dai_drv->capture.rates; 626f0fba2adSLiam Girdwood if (codec_dai_drv->capture.rates 627d9ad6296SJassi Brar & (SNDRV_PCM_RATE_KNOT | SNDRV_PCM_RATE_CONTINUOUS)) 628f0fba2adSLiam Girdwood runtime->hw.rates |= cpu_dai_drv->capture.rates; 629f0fba2adSLiam Girdwood if (cpu_dai_drv->capture.rates 630d9ad6296SJassi Brar & (SNDRV_PCM_RATE_KNOT | SNDRV_PCM_RATE_CONTINUOUS)) 631f0fba2adSLiam Girdwood runtime->hw.rates |= codec_dai_drv->capture.rates; 632db2a4165SFrank Mandarino } 633db2a4165SFrank Mandarino 634db2a4165SFrank Mandarino snd_pcm_limit_hw_rates(runtime); 635db2a4165SFrank Mandarino if (!runtime->hw.rates) { 636db2a4165SFrank Mandarino printk(KERN_ERR "asoc: %s <-> %s No matching rates\n", 637cb666e5bSLiam Girdwood codec_dai->name, cpu_dai->name); 638bb1c0478SJassi Brar goto config_err; 639db2a4165SFrank Mandarino } 640db2a4165SFrank Mandarino if (!runtime->hw.formats) { 641db2a4165SFrank Mandarino printk(KERN_ERR "asoc: %s <-> %s No matching formats\n", 642cb666e5bSLiam Girdwood codec_dai->name, cpu_dai->name); 643bb1c0478SJassi Brar goto config_err; 644db2a4165SFrank Mandarino } 645db2a4165SFrank Mandarino if (!runtime->hw.channels_min || !runtime->hw.channels_max) { 646db2a4165SFrank Mandarino printk(KERN_ERR "asoc: %s <-> %s No matching channels\n", 647cb666e5bSLiam Girdwood codec_dai->name, cpu_dai->name); 648bb1c0478SJassi Brar goto config_err; 649db2a4165SFrank Mandarino } 650db2a4165SFrank Mandarino 65106f409d7SMark Brown /* Symmetry only applies if we've already got an active stream. */ 65206f409d7SMark Brown if (cpu_dai->active || codec_dai->active) { 65306f409d7SMark Brown ret = soc_pcm_apply_symmetry(substream); 65406f409d7SMark Brown if (ret != 0) 655bb1c0478SJassi Brar goto config_err; 65606f409d7SMark Brown } 65706f409d7SMark Brown 658f0fba2adSLiam Girdwood pr_debug("asoc: %s <-> %s info:\n", 659f0fba2adSLiam Girdwood codec_dai->name, cpu_dai->name); 660f24368c2SMark Brown pr_debug("asoc: rate mask 0x%x\n", runtime->hw.rates); 661f24368c2SMark Brown pr_debug("asoc: min ch %d max ch %d\n", runtime->hw.channels_min, 662b5c5fd24SLiam Girdwood runtime->hw.channels_max); 663f24368c2SMark Brown pr_debug("asoc: min rate %d max rate %d\n", runtime->hw.rate_min, 664b5c5fd24SLiam Girdwood runtime->hw.rate_max); 665db2a4165SFrank Mandarino 66614dc5734SJassi Brar if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) { 667f0fba2adSLiam Girdwood cpu_dai->playback_active++; 668f0fba2adSLiam Girdwood codec_dai->playback_active++; 66914dc5734SJassi Brar } else { 670f0fba2adSLiam Girdwood cpu_dai->capture_active++; 671f0fba2adSLiam Girdwood codec_dai->capture_active++; 67214dc5734SJassi Brar } 67314dc5734SJassi Brar cpu_dai->active++; 67414dc5734SJassi Brar codec_dai->active++; 675f0fba2adSLiam Girdwood rtd->codec->active++; 676db2a4165SFrank Mandarino mutex_unlock(&pcm_mutex); 677db2a4165SFrank Mandarino return 0; 678db2a4165SFrank Mandarino 679bb1c0478SJassi Brar config_err: 680f0fba2adSLiam Girdwood if (rtd->dai_link->ops && rtd->dai_link->ops->shutdown) 681f0fba2adSLiam Girdwood rtd->dai_link->ops->shutdown(substream); 682db2a4165SFrank Mandarino 683bb1c0478SJassi Brar machine_err: 684f0fba2adSLiam Girdwood if (codec_dai->driver->ops->shutdown) 685f0fba2adSLiam Girdwood codec_dai->driver->ops->shutdown(substream, codec_dai); 686bb1c0478SJassi Brar 687cb666e5bSLiam Girdwood codec_dai_err: 688f0fba2adSLiam Girdwood if (platform->driver->ops->close) 689f0fba2adSLiam Girdwood platform->driver->ops->close(substream); 690db2a4165SFrank Mandarino 691db2a4165SFrank Mandarino platform_err: 692f0fba2adSLiam Girdwood if (cpu_dai->driver->ops->shutdown) 693f0fba2adSLiam Girdwood cpu_dai->driver->ops->shutdown(substream, cpu_dai); 694db2a4165SFrank Mandarino out: 695db2a4165SFrank Mandarino mutex_unlock(&pcm_mutex); 696db2a4165SFrank Mandarino return ret; 697db2a4165SFrank Mandarino } 698db2a4165SFrank Mandarino 699db2a4165SFrank Mandarino /* 7003a4fa0a2SRobert P. J. Day * Power down the audio subsystem pmdown_time msecs after close is called. 701db2a4165SFrank Mandarino * This is to ensure there are no pops or clicks in between any music tracks 702db2a4165SFrank Mandarino * due to DAPM power cycling. 703db2a4165SFrank Mandarino */ 7044484bb2eSAndrew Morton static void close_delayed_work(struct work_struct *work) 705db2a4165SFrank Mandarino { 706f0fba2adSLiam Girdwood struct snd_soc_pcm_runtime *rtd = 707f0fba2adSLiam Girdwood container_of(work, struct snd_soc_pcm_runtime, delayed_work.work); 708f0fba2adSLiam Girdwood struct snd_soc_dai *codec_dai = rtd->codec_dai; 709db2a4165SFrank Mandarino 710db2a4165SFrank Mandarino mutex_lock(&pcm_mutex); 711db2a4165SFrank Mandarino 712f24368c2SMark Brown pr_debug("pop wq checking: %s status: %s waiting: %s\n", 713f0fba2adSLiam Girdwood codec_dai->driver->playback.stream_name, 714f0fba2adSLiam Girdwood codec_dai->playback_active ? "active" : "inactive", 715db2a4165SFrank Mandarino codec_dai->pop_wait ? "yes" : "no"); 716db2a4165SFrank Mandarino 717db2a4165SFrank Mandarino /* are we waiting on this codec DAI stream */ 718db2a4165SFrank Mandarino if (codec_dai->pop_wait == 1) { 719db2a4165SFrank Mandarino codec_dai->pop_wait = 0; 720f0fba2adSLiam Girdwood snd_soc_dapm_stream_event(rtd, 721f0fba2adSLiam Girdwood codec_dai->driver->playback.stream_name, 722db2a4165SFrank Mandarino SND_SOC_DAPM_STREAM_STOP); 723db2a4165SFrank Mandarino } 724f0fba2adSLiam Girdwood 725db2a4165SFrank Mandarino mutex_unlock(&pcm_mutex); 726db2a4165SFrank Mandarino } 727db2a4165SFrank Mandarino 728db2a4165SFrank Mandarino /* 729db2a4165SFrank Mandarino * Called by ALSA when a PCM substream is closed. Private data can be 730db2a4165SFrank Mandarino * freed here. The cpu DAI, codec DAI, machine and platform are also 731db2a4165SFrank Mandarino * shutdown. 732db2a4165SFrank Mandarino */ 733db2a4165SFrank Mandarino static int soc_codec_close(struct snd_pcm_substream *substream) 734db2a4165SFrank Mandarino { 735db2a4165SFrank Mandarino struct snd_soc_pcm_runtime *rtd = substream->private_data; 736f0fba2adSLiam Girdwood struct snd_soc_platform *platform = rtd->platform; 737f0fba2adSLiam Girdwood struct snd_soc_dai *cpu_dai = rtd->cpu_dai; 738f0fba2adSLiam Girdwood struct snd_soc_dai *codec_dai = rtd->codec_dai; 739f0fba2adSLiam Girdwood struct snd_soc_codec *codec = rtd->codec; 740db2a4165SFrank Mandarino 741db2a4165SFrank Mandarino mutex_lock(&pcm_mutex); 742db2a4165SFrank Mandarino 74314dc5734SJassi Brar if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) { 744f0fba2adSLiam Girdwood cpu_dai->playback_active--; 745f0fba2adSLiam Girdwood codec_dai->playback_active--; 74614dc5734SJassi Brar } else { 747f0fba2adSLiam Girdwood cpu_dai->capture_active--; 748f0fba2adSLiam Girdwood codec_dai->capture_active--; 749db2a4165SFrank Mandarino } 75014dc5734SJassi Brar 75114dc5734SJassi Brar cpu_dai->active--; 75214dc5734SJassi Brar codec_dai->active--; 753db2a4165SFrank Mandarino codec->active--; 754db2a4165SFrank Mandarino 7556010b2daSMark Brown /* Muting the DAC suppresses artifacts caused during digital 7566010b2daSMark Brown * shutdown, for example from stopping clocks. 7576010b2daSMark Brown */ 7586010b2daSMark Brown if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) 7596010b2daSMark Brown snd_soc_dai_digital_mute(codec_dai, 1); 7606010b2daSMark Brown 761f0fba2adSLiam Girdwood if (cpu_dai->driver->ops->shutdown) 762f0fba2adSLiam Girdwood cpu_dai->driver->ops->shutdown(substream, cpu_dai); 763db2a4165SFrank Mandarino 764f0fba2adSLiam Girdwood if (codec_dai->driver->ops->shutdown) 765f0fba2adSLiam Girdwood codec_dai->driver->ops->shutdown(substream, codec_dai); 766db2a4165SFrank Mandarino 767f0fba2adSLiam Girdwood if (rtd->dai_link->ops && rtd->dai_link->ops->shutdown) 768f0fba2adSLiam Girdwood rtd->dai_link->ops->shutdown(substream); 769db2a4165SFrank Mandarino 770f0fba2adSLiam Girdwood if (platform->driver->ops->close) 771f0fba2adSLiam Girdwood platform->driver->ops->close(substream); 772f0fba2adSLiam Girdwood cpu_dai->runtime = NULL; 773db2a4165SFrank Mandarino 774db2a4165SFrank Mandarino if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) { 775db2a4165SFrank Mandarino /* start delayed pop wq here for playback streams */ 776cb666e5bSLiam Girdwood codec_dai->pop_wait = 1; 777f0fba2adSLiam Girdwood schedule_delayed_work(&rtd->delayed_work, 778f0fba2adSLiam Girdwood msecs_to_jiffies(rtd->pmdown_time)); 779db2a4165SFrank Mandarino } else { 780db2a4165SFrank Mandarino /* capture streams can be powered down now */ 781f0fba2adSLiam Girdwood snd_soc_dapm_stream_event(rtd, 782f0fba2adSLiam Girdwood codec_dai->driver->capture.stream_name, 7830b4d221bSLiam Girdwood SND_SOC_DAPM_STREAM_STOP); 784db2a4165SFrank Mandarino } 785db2a4165SFrank Mandarino 786db2a4165SFrank Mandarino mutex_unlock(&pcm_mutex); 787db2a4165SFrank Mandarino return 0; 788db2a4165SFrank Mandarino } 789db2a4165SFrank Mandarino 790db2a4165SFrank Mandarino /* 791db2a4165SFrank Mandarino * Called by ALSA when the PCM substream is prepared, can set format, sample 792db2a4165SFrank Mandarino * rate, etc. This function is non atomic and can be called multiple times, 793db2a4165SFrank Mandarino * it can refer to the runtime info. 794db2a4165SFrank Mandarino */ 795db2a4165SFrank Mandarino static int soc_pcm_prepare(struct snd_pcm_substream *substream) 796db2a4165SFrank Mandarino { 797db2a4165SFrank Mandarino struct snd_soc_pcm_runtime *rtd = substream->private_data; 798f0fba2adSLiam Girdwood struct snd_soc_platform *platform = rtd->platform; 799f0fba2adSLiam Girdwood struct snd_soc_dai *cpu_dai = rtd->cpu_dai; 800f0fba2adSLiam Girdwood struct snd_soc_dai *codec_dai = rtd->codec_dai; 801db2a4165SFrank Mandarino int ret = 0; 802db2a4165SFrank Mandarino 803db2a4165SFrank Mandarino mutex_lock(&pcm_mutex); 804cb666e5bSLiam Girdwood 805f0fba2adSLiam Girdwood if (rtd->dai_link->ops && rtd->dai_link->ops->prepare) { 806f0fba2adSLiam Girdwood ret = rtd->dai_link->ops->prepare(substream); 807cb666e5bSLiam Girdwood if (ret < 0) { 808cb666e5bSLiam Girdwood printk(KERN_ERR "asoc: machine prepare error\n"); 809cb666e5bSLiam Girdwood goto out; 810cb666e5bSLiam Girdwood } 811cb666e5bSLiam Girdwood } 812cb666e5bSLiam Girdwood 813f0fba2adSLiam Girdwood if (platform->driver->ops->prepare) { 814f0fba2adSLiam Girdwood ret = platform->driver->ops->prepare(substream); 815a71a468aSLiam Girdwood if (ret < 0) { 816a71a468aSLiam Girdwood printk(KERN_ERR "asoc: platform prepare error\n"); 817db2a4165SFrank Mandarino goto out; 818db2a4165SFrank Mandarino } 819a71a468aSLiam Girdwood } 820db2a4165SFrank Mandarino 821f0fba2adSLiam Girdwood if (codec_dai->driver->ops->prepare) { 822f0fba2adSLiam Girdwood ret = codec_dai->driver->ops->prepare(substream, codec_dai); 823a71a468aSLiam Girdwood if (ret < 0) { 824a71a468aSLiam Girdwood printk(KERN_ERR "asoc: codec DAI prepare error\n"); 825db2a4165SFrank Mandarino goto out; 826db2a4165SFrank Mandarino } 827a71a468aSLiam Girdwood } 828db2a4165SFrank Mandarino 829f0fba2adSLiam Girdwood if (cpu_dai->driver->ops->prepare) { 830f0fba2adSLiam Girdwood ret = cpu_dai->driver->ops->prepare(substream, cpu_dai); 831cb666e5bSLiam Girdwood if (ret < 0) { 832cb666e5bSLiam Girdwood printk(KERN_ERR "asoc: cpu DAI prepare error\n"); 833cb666e5bSLiam Girdwood goto out; 834cb666e5bSLiam Girdwood } 835cb666e5bSLiam Girdwood } 836db2a4165SFrank Mandarino 837d45f6219SMark Brown /* cancel any delayed stream shutdown that is pending */ 838d45f6219SMark Brown if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK && 839d45f6219SMark Brown codec_dai->pop_wait) { 840cb666e5bSLiam Girdwood codec_dai->pop_wait = 0; 841f0fba2adSLiam Girdwood cancel_delayed_work(&rtd->delayed_work); 842db2a4165SFrank Mandarino } 843db2a4165SFrank Mandarino 844db2a4165SFrank Mandarino if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) 845f0fba2adSLiam Girdwood snd_soc_dapm_stream_event(rtd, 846f0fba2adSLiam Girdwood codec_dai->driver->playback.stream_name, 847db2a4165SFrank Mandarino SND_SOC_DAPM_STREAM_START); 848db2a4165SFrank Mandarino else 849f0fba2adSLiam Girdwood snd_soc_dapm_stream_event(rtd, 850f0fba2adSLiam Girdwood codec_dai->driver->capture.stream_name, 851db2a4165SFrank Mandarino SND_SOC_DAPM_STREAM_START); 8528c6529dbSLiam Girdwood 8538c6529dbSLiam Girdwood snd_soc_dai_digital_mute(codec_dai, 0); 854db2a4165SFrank Mandarino 855db2a4165SFrank Mandarino out: 856db2a4165SFrank Mandarino mutex_unlock(&pcm_mutex); 857db2a4165SFrank Mandarino return ret; 858db2a4165SFrank Mandarino } 859db2a4165SFrank Mandarino 860db2a4165SFrank Mandarino /* 861db2a4165SFrank Mandarino * Called by ALSA when the hardware params are set by application. This 862db2a4165SFrank Mandarino * function can also be called multiple times and can allocate buffers 863db2a4165SFrank Mandarino * (using snd_pcm_lib_* ). It's non-atomic. 864db2a4165SFrank Mandarino */ 865db2a4165SFrank Mandarino static int soc_pcm_hw_params(struct snd_pcm_substream *substream, 866db2a4165SFrank Mandarino struct snd_pcm_hw_params *params) 867db2a4165SFrank Mandarino { 868db2a4165SFrank Mandarino struct snd_soc_pcm_runtime *rtd = substream->private_data; 869f0fba2adSLiam Girdwood struct snd_soc_platform *platform = rtd->platform; 870f0fba2adSLiam Girdwood struct snd_soc_dai *cpu_dai = rtd->cpu_dai; 871f0fba2adSLiam Girdwood struct snd_soc_dai *codec_dai = rtd->codec_dai; 872db2a4165SFrank Mandarino int ret = 0; 873db2a4165SFrank Mandarino 874db2a4165SFrank Mandarino mutex_lock(&pcm_mutex); 875db2a4165SFrank Mandarino 876f0fba2adSLiam Girdwood if (rtd->dai_link->ops && rtd->dai_link->ops->hw_params) { 877f0fba2adSLiam Girdwood ret = rtd->dai_link->ops->hw_params(substream, params); 878cb666e5bSLiam Girdwood if (ret < 0) { 879cb666e5bSLiam Girdwood printk(KERN_ERR "asoc: machine hw_params failed\n"); 880db2a4165SFrank Mandarino goto out; 881db2a4165SFrank Mandarino } 882cb666e5bSLiam Girdwood } 883db2a4165SFrank Mandarino 884f0fba2adSLiam Girdwood if (codec_dai->driver->ops->hw_params) { 885f0fba2adSLiam Girdwood ret = codec_dai->driver->ops->hw_params(substream, params, codec_dai); 886db2a4165SFrank Mandarino if (ret < 0) { 887db2a4165SFrank Mandarino printk(KERN_ERR "asoc: can't set codec %s hw params\n", 888cb666e5bSLiam Girdwood codec_dai->name); 889cb666e5bSLiam Girdwood goto codec_err; 890db2a4165SFrank Mandarino } 891db2a4165SFrank Mandarino } 892db2a4165SFrank Mandarino 893f0fba2adSLiam Girdwood if (cpu_dai->driver->ops->hw_params) { 894f0fba2adSLiam Girdwood ret = cpu_dai->driver->ops->hw_params(substream, params, cpu_dai); 895db2a4165SFrank Mandarino if (ret < 0) { 8963ff3f64bSMark Brown printk(KERN_ERR "asoc: interface %s hw params failed\n", 897cb666e5bSLiam Girdwood cpu_dai->name); 898db2a4165SFrank Mandarino goto interface_err; 899db2a4165SFrank Mandarino } 900db2a4165SFrank Mandarino } 901db2a4165SFrank Mandarino 902f0fba2adSLiam Girdwood if (platform->driver->ops->hw_params) { 903f0fba2adSLiam Girdwood ret = platform->driver->ops->hw_params(substream, params); 904db2a4165SFrank Mandarino if (ret < 0) { 9053ff3f64bSMark Brown printk(KERN_ERR "asoc: platform %s hw params failed\n", 906db2a4165SFrank Mandarino platform->name); 907db2a4165SFrank Mandarino goto platform_err; 908db2a4165SFrank Mandarino } 909db2a4165SFrank Mandarino } 910db2a4165SFrank Mandarino 911f0fba2adSLiam Girdwood rtd->rate = params_rate(params); 91206f409d7SMark Brown 913db2a4165SFrank Mandarino out: 914db2a4165SFrank Mandarino mutex_unlock(&pcm_mutex); 915db2a4165SFrank Mandarino return ret; 916db2a4165SFrank Mandarino 917db2a4165SFrank Mandarino platform_err: 918f0fba2adSLiam Girdwood if (cpu_dai->driver->ops->hw_free) 919f0fba2adSLiam Girdwood cpu_dai->driver->ops->hw_free(substream, cpu_dai); 920db2a4165SFrank Mandarino 921db2a4165SFrank Mandarino interface_err: 922f0fba2adSLiam Girdwood if (codec_dai->driver->ops->hw_free) 923f0fba2adSLiam Girdwood codec_dai->driver->ops->hw_free(substream, codec_dai); 924cb666e5bSLiam Girdwood 925cb666e5bSLiam Girdwood codec_err: 926f0fba2adSLiam Girdwood if (rtd->dai_link->ops && rtd->dai_link->ops->hw_free) 927f0fba2adSLiam Girdwood rtd->dai_link->ops->hw_free(substream); 928db2a4165SFrank Mandarino 929db2a4165SFrank Mandarino mutex_unlock(&pcm_mutex); 930db2a4165SFrank Mandarino return ret; 931db2a4165SFrank Mandarino } 932db2a4165SFrank Mandarino 933db2a4165SFrank Mandarino /* 934a00f90f9SMark Brown * Frees resources allocated by hw_params, can be called multiple times 935db2a4165SFrank Mandarino */ 936db2a4165SFrank Mandarino static int soc_pcm_hw_free(struct snd_pcm_substream *substream) 937db2a4165SFrank Mandarino { 938db2a4165SFrank Mandarino struct snd_soc_pcm_runtime *rtd = substream->private_data; 939f0fba2adSLiam Girdwood struct snd_soc_platform *platform = rtd->platform; 940f0fba2adSLiam Girdwood struct snd_soc_dai *cpu_dai = rtd->cpu_dai; 941f0fba2adSLiam Girdwood struct snd_soc_dai *codec_dai = rtd->codec_dai; 942f0fba2adSLiam Girdwood struct snd_soc_codec *codec = rtd->codec; 943db2a4165SFrank Mandarino 944db2a4165SFrank Mandarino mutex_lock(&pcm_mutex); 945db2a4165SFrank Mandarino 946db2a4165SFrank Mandarino /* apply codec digital mute */ 9478c6529dbSLiam Girdwood if (!codec->active) 9488c6529dbSLiam Girdwood snd_soc_dai_digital_mute(codec_dai, 1); 949db2a4165SFrank Mandarino 950db2a4165SFrank Mandarino /* free any machine hw params */ 951f0fba2adSLiam Girdwood if (rtd->dai_link->ops && rtd->dai_link->ops->hw_free) 952f0fba2adSLiam Girdwood rtd->dai_link->ops->hw_free(substream); 953db2a4165SFrank Mandarino 954db2a4165SFrank Mandarino /* free any DMA resources */ 955f0fba2adSLiam Girdwood if (platform->driver->ops->hw_free) 956f0fba2adSLiam Girdwood platform->driver->ops->hw_free(substream); 957db2a4165SFrank Mandarino 958a00f90f9SMark Brown /* now free hw params for the DAIs */ 959f0fba2adSLiam Girdwood if (codec_dai->driver->ops->hw_free) 960f0fba2adSLiam Girdwood codec_dai->driver->ops->hw_free(substream, codec_dai); 961db2a4165SFrank Mandarino 962f0fba2adSLiam Girdwood if (cpu_dai->driver->ops->hw_free) 963f0fba2adSLiam Girdwood cpu_dai->driver->ops->hw_free(substream, cpu_dai); 964db2a4165SFrank Mandarino 965db2a4165SFrank Mandarino mutex_unlock(&pcm_mutex); 966db2a4165SFrank Mandarino return 0; 967db2a4165SFrank Mandarino } 968db2a4165SFrank Mandarino 969db2a4165SFrank Mandarino static int soc_pcm_trigger(struct snd_pcm_substream *substream, int cmd) 970db2a4165SFrank Mandarino { 971db2a4165SFrank Mandarino struct snd_soc_pcm_runtime *rtd = substream->private_data; 972f0fba2adSLiam Girdwood struct snd_soc_platform *platform = rtd->platform; 973f0fba2adSLiam Girdwood struct snd_soc_dai *cpu_dai = rtd->cpu_dai; 974f0fba2adSLiam Girdwood struct snd_soc_dai *codec_dai = rtd->codec_dai; 975db2a4165SFrank Mandarino int ret; 976db2a4165SFrank Mandarino 977f0fba2adSLiam Girdwood if (codec_dai->driver->ops->trigger) { 978f0fba2adSLiam Girdwood ret = codec_dai->driver->ops->trigger(substream, cmd, codec_dai); 979db2a4165SFrank Mandarino if (ret < 0) 980db2a4165SFrank Mandarino return ret; 981db2a4165SFrank Mandarino } 982db2a4165SFrank Mandarino 983f0fba2adSLiam Girdwood if (platform->driver->ops->trigger) { 984f0fba2adSLiam Girdwood ret = platform->driver->ops->trigger(substream, cmd); 985db2a4165SFrank Mandarino if (ret < 0) 986db2a4165SFrank Mandarino return ret; 987db2a4165SFrank Mandarino } 988db2a4165SFrank Mandarino 989f0fba2adSLiam Girdwood if (cpu_dai->driver->ops->trigger) { 990f0fba2adSLiam Girdwood ret = cpu_dai->driver->ops->trigger(substream, cmd, cpu_dai); 991db2a4165SFrank Mandarino if (ret < 0) 992db2a4165SFrank Mandarino return ret; 993db2a4165SFrank Mandarino } 994db2a4165SFrank Mandarino return 0; 995db2a4165SFrank Mandarino } 996db2a4165SFrank Mandarino 997377b6f62SPeter Ujfalusi /* 998377b6f62SPeter Ujfalusi * soc level wrapper for pointer callback 999258020d0SPeter Ujfalusi * If cpu_dai, codec_dai, platform driver has the delay callback, than 1000258020d0SPeter Ujfalusi * the runtime->delay will be updated accordingly. 1001377b6f62SPeter Ujfalusi */ 1002377b6f62SPeter Ujfalusi static snd_pcm_uframes_t soc_pcm_pointer(struct snd_pcm_substream *substream) 1003377b6f62SPeter Ujfalusi { 1004377b6f62SPeter Ujfalusi struct snd_soc_pcm_runtime *rtd = substream->private_data; 1005f0fba2adSLiam Girdwood struct snd_soc_platform *platform = rtd->platform; 1006f0fba2adSLiam Girdwood struct snd_soc_dai *cpu_dai = rtd->cpu_dai; 1007f0fba2adSLiam Girdwood struct snd_soc_dai *codec_dai = rtd->codec_dai; 1008258020d0SPeter Ujfalusi struct snd_pcm_runtime *runtime = substream->runtime; 1009377b6f62SPeter Ujfalusi snd_pcm_uframes_t offset = 0; 1010258020d0SPeter Ujfalusi snd_pcm_sframes_t delay = 0; 1011377b6f62SPeter Ujfalusi 1012f0fba2adSLiam Girdwood if (platform->driver->ops->pointer) 1013f0fba2adSLiam Girdwood offset = platform->driver->ops->pointer(substream); 1014377b6f62SPeter Ujfalusi 1015f0fba2adSLiam Girdwood if (cpu_dai->driver->ops->delay) 1016f0fba2adSLiam Girdwood delay += cpu_dai->driver->ops->delay(substream, cpu_dai); 1017258020d0SPeter Ujfalusi 1018f0fba2adSLiam Girdwood if (codec_dai->driver->ops->delay) 1019f0fba2adSLiam Girdwood delay += codec_dai->driver->ops->delay(substream, codec_dai); 1020258020d0SPeter Ujfalusi 1021f0fba2adSLiam Girdwood if (platform->driver->delay) 1022f0fba2adSLiam Girdwood delay += platform->driver->delay(substream, codec_dai); 1023258020d0SPeter Ujfalusi 1024258020d0SPeter Ujfalusi runtime->delay = delay; 1025258020d0SPeter Ujfalusi 1026377b6f62SPeter Ujfalusi return offset; 1027377b6f62SPeter Ujfalusi } 1028377b6f62SPeter Ujfalusi 1029db2a4165SFrank Mandarino /* ASoC PCM operations */ 1030db2a4165SFrank Mandarino static struct snd_pcm_ops soc_pcm_ops = { 1031db2a4165SFrank Mandarino .open = soc_pcm_open, 1032db2a4165SFrank Mandarino .close = soc_codec_close, 1033db2a4165SFrank Mandarino .hw_params = soc_pcm_hw_params, 1034db2a4165SFrank Mandarino .hw_free = soc_pcm_hw_free, 1035db2a4165SFrank Mandarino .prepare = soc_pcm_prepare, 1036db2a4165SFrank Mandarino .trigger = soc_pcm_trigger, 1037377b6f62SPeter Ujfalusi .pointer = soc_pcm_pointer, 1038db2a4165SFrank Mandarino }; 1039db2a4165SFrank Mandarino 10406f8ab4acSMark Brown #ifdef CONFIG_PM_SLEEP 1041db2a4165SFrank Mandarino /* powers down audio subsystem for suspend */ 10426f8ab4acSMark Brown int snd_soc_suspend(struct device *dev) 1043db2a4165SFrank Mandarino { 10446f8ab4acSMark Brown struct snd_soc_card *card = dev_get_drvdata(dev); 10452eea392dSJarkko Nikula struct snd_soc_codec *codec; 1046db2a4165SFrank Mandarino int i; 1047db2a4165SFrank Mandarino 1048e3509ff0SDaniel Mack /* If the initialization of this soc device failed, there is no codec 1049e3509ff0SDaniel Mack * associated with it. Just bail out in this case. 1050e3509ff0SDaniel Mack */ 1051f0fba2adSLiam Girdwood if (list_empty(&card->codec_dev_list)) 1052e3509ff0SDaniel Mack return 0; 1053e3509ff0SDaniel Mack 10546ed25978SAndy Green /* Due to the resume being scheduled into a workqueue we could 10556ed25978SAndy Green * suspend before that's finished - wait for it to complete. 10566ed25978SAndy Green */ 1057f0fba2adSLiam Girdwood snd_power_lock(card->snd_card); 1058f0fba2adSLiam Girdwood snd_power_wait(card->snd_card, SNDRV_CTL_POWER_D0); 1059f0fba2adSLiam Girdwood snd_power_unlock(card->snd_card); 10606ed25978SAndy Green 10616ed25978SAndy Green /* we're going to block userspace touching us until resume completes */ 1062f0fba2adSLiam Girdwood snd_power_change_state(card->snd_card, SNDRV_CTL_POWER_D3hot); 10636ed25978SAndy Green 1064a00f90f9SMark Brown /* mute any active DACs */ 1065f0fba2adSLiam Girdwood for (i = 0; i < card->num_rtd; i++) { 1066f0fba2adSLiam Girdwood struct snd_soc_dai *dai = card->rtd[i].codec_dai; 1067f0fba2adSLiam Girdwood struct snd_soc_dai_driver *drv = dai->driver; 10683efab7dcSMark Brown 1069f0fba2adSLiam Girdwood if (card->rtd[i].dai_link->ignore_suspend) 10703efab7dcSMark Brown continue; 10713efab7dcSMark Brown 1072f0fba2adSLiam Girdwood if (drv->ops->digital_mute && dai->playback_active) 1073f0fba2adSLiam Girdwood drv->ops->digital_mute(dai, 1); 1074db2a4165SFrank Mandarino } 1075db2a4165SFrank Mandarino 10764ccab3e7SLiam Girdwood /* suspend all pcms */ 1077f0fba2adSLiam Girdwood for (i = 0; i < card->num_rtd; i++) { 1078f0fba2adSLiam Girdwood if (card->rtd[i].dai_link->ignore_suspend) 10793efab7dcSMark Brown continue; 10803efab7dcSMark Brown 1081f0fba2adSLiam Girdwood snd_pcm_suspend_all(card->rtd[i].pcm); 10823efab7dcSMark Brown } 10834ccab3e7SLiam Girdwood 108487506549SMark Brown if (card->suspend_pre) 108570b2ac12SMark Brown card->suspend_pre(card); 1086db2a4165SFrank Mandarino 1087f0fba2adSLiam Girdwood for (i = 0; i < card->num_rtd; i++) { 1088f0fba2adSLiam Girdwood struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai; 1089f0fba2adSLiam Girdwood struct snd_soc_platform *platform = card->rtd[i].platform; 10903efab7dcSMark Brown 1091f0fba2adSLiam Girdwood if (card->rtd[i].dai_link->ignore_suspend) 10923efab7dcSMark Brown continue; 10933efab7dcSMark Brown 1094f0fba2adSLiam Girdwood if (cpu_dai->driver->suspend && !cpu_dai->driver->ac97_control) 1095f0fba2adSLiam Girdwood cpu_dai->driver->suspend(cpu_dai); 1096f0fba2adSLiam Girdwood if (platform->driver->suspend && !platform->suspended) { 1097f0fba2adSLiam Girdwood platform->driver->suspend(cpu_dai); 1098f0fba2adSLiam Girdwood platform->suspended = 1; 1099f0fba2adSLiam Girdwood } 1100db2a4165SFrank Mandarino } 1101db2a4165SFrank Mandarino 1102db2a4165SFrank Mandarino /* close any waiting streams and save state */ 1103f0fba2adSLiam Girdwood for (i = 0; i < card->num_rtd; i++) { 11045b84ba26STejun Heo flush_delayed_work_sync(&card->rtd[i].delayed_work); 1105ce6120ccSLiam Girdwood card->rtd[i].codec->dapm.suspend_bias_level = card->rtd[i].codec->dapm.bias_level; 1106f0fba2adSLiam Girdwood } 1107db2a4165SFrank Mandarino 1108f0fba2adSLiam Girdwood for (i = 0; i < card->num_rtd; i++) { 1109f0fba2adSLiam Girdwood struct snd_soc_dai_driver *driver = card->rtd[i].codec_dai->driver; 11103efab7dcSMark Brown 1111f0fba2adSLiam Girdwood if (card->rtd[i].dai_link->ignore_suspend) 11123efab7dcSMark Brown continue; 11133efab7dcSMark Brown 1114f0fba2adSLiam Girdwood if (driver->playback.stream_name != NULL) 1115f0fba2adSLiam Girdwood snd_soc_dapm_stream_event(&card->rtd[i], driver->playback.stream_name, 1116db2a4165SFrank Mandarino SND_SOC_DAPM_STREAM_SUSPEND); 1117f0fba2adSLiam Girdwood 1118f0fba2adSLiam Girdwood if (driver->capture.stream_name != NULL) 1119f0fba2adSLiam Girdwood snd_soc_dapm_stream_event(&card->rtd[i], driver->capture.stream_name, 1120db2a4165SFrank Mandarino SND_SOC_DAPM_STREAM_SUSPEND); 1121db2a4165SFrank Mandarino } 1122db2a4165SFrank Mandarino 1123f0fba2adSLiam Girdwood /* suspend all CODECs */ 11242eea392dSJarkko Nikula list_for_each_entry(codec, &card->codec_dev_list, card_list) { 11251547aba9SMark Brown /* If there are paths active then the CODEC will be held with 11261547aba9SMark Brown * bias _ON and should not be suspended. */ 1127f0fba2adSLiam Girdwood if (!codec->suspended && codec->driver->suspend) { 1128ce6120ccSLiam Girdwood switch (codec->dapm.bias_level) { 11291547aba9SMark Brown case SND_SOC_BIAS_STANDBY: 11301547aba9SMark Brown case SND_SOC_BIAS_OFF: 1131f0fba2adSLiam Girdwood codec->driver->suspend(codec, PMSG_SUSPEND); 1132f0fba2adSLiam Girdwood codec->suspended = 1; 11331547aba9SMark Brown break; 11341547aba9SMark Brown default: 1135f0fba2adSLiam Girdwood dev_dbg(codec->dev, "CODEC is on over suspend\n"); 11361547aba9SMark Brown break; 11371547aba9SMark Brown } 11381547aba9SMark Brown } 1139f0fba2adSLiam Girdwood } 1140db2a4165SFrank Mandarino 1141f0fba2adSLiam Girdwood for (i = 0; i < card->num_rtd; i++) { 1142f0fba2adSLiam Girdwood struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai; 11433efab7dcSMark Brown 1144f0fba2adSLiam Girdwood if (card->rtd[i].dai_link->ignore_suspend) 11453efab7dcSMark Brown continue; 11463efab7dcSMark Brown 1147f0fba2adSLiam Girdwood if (cpu_dai->driver->suspend && cpu_dai->driver->ac97_control) 1148f0fba2adSLiam Girdwood cpu_dai->driver->suspend(cpu_dai); 1149db2a4165SFrank Mandarino } 1150db2a4165SFrank Mandarino 115187506549SMark Brown if (card->suspend_post) 115270b2ac12SMark Brown card->suspend_post(card); 1153db2a4165SFrank Mandarino 1154db2a4165SFrank Mandarino return 0; 1155db2a4165SFrank Mandarino } 11566f8ab4acSMark Brown EXPORT_SYMBOL_GPL(snd_soc_suspend); 1157db2a4165SFrank Mandarino 11586ed25978SAndy Green /* deferred resume work, so resume can complete before we finished 11596ed25978SAndy Green * setting our codec back up, which can be very slow on I2C 11606ed25978SAndy Green */ 11616ed25978SAndy Green static void soc_resume_deferred(struct work_struct *work) 1162db2a4165SFrank Mandarino { 1163f0fba2adSLiam Girdwood struct snd_soc_card *card = 1164f0fba2adSLiam Girdwood container_of(work, struct snd_soc_card, deferred_resume_work); 11652eea392dSJarkko Nikula struct snd_soc_codec *codec; 1166db2a4165SFrank Mandarino int i; 1167db2a4165SFrank Mandarino 11686ed25978SAndy Green /* our power state is still SNDRV_CTL_POWER_D3hot from suspend time, 11696ed25978SAndy Green * so userspace apps are blocked from touching us 11706ed25978SAndy Green */ 11716ed25978SAndy Green 1172f0fba2adSLiam Girdwood dev_dbg(card->dev, "starting resume work\n"); 11736ed25978SAndy Green 11749949788bSMark Brown /* Bring us up into D2 so that DAPM starts enabling things */ 1175f0fba2adSLiam Girdwood snd_power_change_state(card->snd_card, SNDRV_CTL_POWER_D2); 11769949788bSMark Brown 117787506549SMark Brown if (card->resume_pre) 117870b2ac12SMark Brown card->resume_pre(card); 1179db2a4165SFrank Mandarino 1180f0fba2adSLiam Girdwood /* resume AC97 DAIs */ 1181f0fba2adSLiam Girdwood for (i = 0; i < card->num_rtd; i++) { 1182f0fba2adSLiam Girdwood struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai; 11833efab7dcSMark Brown 1184f0fba2adSLiam Girdwood if (card->rtd[i].dai_link->ignore_suspend) 11853efab7dcSMark Brown continue; 11863efab7dcSMark Brown 1187f0fba2adSLiam Girdwood if (cpu_dai->driver->resume && cpu_dai->driver->ac97_control) 1188f0fba2adSLiam Girdwood cpu_dai->driver->resume(cpu_dai); 1189db2a4165SFrank Mandarino } 1190db2a4165SFrank Mandarino 11912eea392dSJarkko Nikula list_for_each_entry(codec, &card->codec_dev_list, card_list) { 11921547aba9SMark Brown /* If the CODEC was idle over suspend then it will have been 11931547aba9SMark Brown * left with bias OFF or STANDBY and suspended so we must now 11941547aba9SMark Brown * resume. Otherwise the suspend was suppressed. 11951547aba9SMark Brown */ 1196f0fba2adSLiam Girdwood if (codec->driver->resume && codec->suspended) { 1197ce6120ccSLiam Girdwood switch (codec->dapm.bias_level) { 11981547aba9SMark Brown case SND_SOC_BIAS_STANDBY: 11991547aba9SMark Brown case SND_SOC_BIAS_OFF: 1200f0fba2adSLiam Girdwood codec->driver->resume(codec); 1201f0fba2adSLiam Girdwood codec->suspended = 0; 12021547aba9SMark Brown break; 12031547aba9SMark Brown default: 1204f0fba2adSLiam Girdwood dev_dbg(codec->dev, "CODEC was on over suspend\n"); 12051547aba9SMark Brown break; 12061547aba9SMark Brown } 12071547aba9SMark Brown } 1208f0fba2adSLiam Girdwood } 1209db2a4165SFrank Mandarino 1210f0fba2adSLiam Girdwood for (i = 0; i < card->num_rtd; i++) { 1211f0fba2adSLiam Girdwood struct snd_soc_dai_driver *driver = card->rtd[i].codec_dai->driver; 12123efab7dcSMark Brown 1213f0fba2adSLiam Girdwood if (card->rtd[i].dai_link->ignore_suspend) 12143efab7dcSMark Brown continue; 12153efab7dcSMark Brown 1216f0fba2adSLiam Girdwood if (driver->playback.stream_name != NULL) 1217f0fba2adSLiam Girdwood snd_soc_dapm_stream_event(&card->rtd[i], driver->playback.stream_name, 1218db2a4165SFrank Mandarino SND_SOC_DAPM_STREAM_RESUME); 1219f0fba2adSLiam Girdwood 1220f0fba2adSLiam Girdwood if (driver->capture.stream_name != NULL) 1221f0fba2adSLiam Girdwood snd_soc_dapm_stream_event(&card->rtd[i], driver->capture.stream_name, 1222db2a4165SFrank Mandarino SND_SOC_DAPM_STREAM_RESUME); 1223db2a4165SFrank Mandarino } 1224db2a4165SFrank Mandarino 12253ff3f64bSMark Brown /* unmute any active DACs */ 1226f0fba2adSLiam Girdwood for (i = 0; i < card->num_rtd; i++) { 1227f0fba2adSLiam Girdwood struct snd_soc_dai *dai = card->rtd[i].codec_dai; 1228f0fba2adSLiam Girdwood struct snd_soc_dai_driver *drv = dai->driver; 12293efab7dcSMark Brown 1230f0fba2adSLiam Girdwood if (card->rtd[i].dai_link->ignore_suspend) 12313efab7dcSMark Brown continue; 12323efab7dcSMark Brown 1233f0fba2adSLiam Girdwood if (drv->ops->digital_mute && dai->playback_active) 1234f0fba2adSLiam Girdwood drv->ops->digital_mute(dai, 0); 1235db2a4165SFrank Mandarino } 1236db2a4165SFrank Mandarino 1237f0fba2adSLiam Girdwood for (i = 0; i < card->num_rtd; i++) { 1238f0fba2adSLiam Girdwood struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai; 1239f0fba2adSLiam Girdwood struct snd_soc_platform *platform = card->rtd[i].platform; 12403efab7dcSMark Brown 1241f0fba2adSLiam Girdwood if (card->rtd[i].dai_link->ignore_suspend) 12423efab7dcSMark Brown continue; 12433efab7dcSMark Brown 1244f0fba2adSLiam Girdwood if (cpu_dai->driver->resume && !cpu_dai->driver->ac97_control) 1245f0fba2adSLiam Girdwood cpu_dai->driver->resume(cpu_dai); 1246f0fba2adSLiam Girdwood if (platform->driver->resume && platform->suspended) { 1247f0fba2adSLiam Girdwood platform->driver->resume(cpu_dai); 1248f0fba2adSLiam Girdwood platform->suspended = 0; 1249f0fba2adSLiam Girdwood } 1250db2a4165SFrank Mandarino } 1251db2a4165SFrank Mandarino 125287506549SMark Brown if (card->resume_post) 125370b2ac12SMark Brown card->resume_post(card); 1254db2a4165SFrank Mandarino 1255f0fba2adSLiam Girdwood dev_dbg(card->dev, "resume work completed\n"); 12566ed25978SAndy Green 12576ed25978SAndy Green /* userspace can access us now we are back as we were before */ 1258f0fba2adSLiam Girdwood snd_power_change_state(card->snd_card, SNDRV_CTL_POWER_D0); 12596ed25978SAndy Green } 12606ed25978SAndy Green 12616ed25978SAndy Green /* powers up audio subsystem after a suspend */ 12626f8ab4acSMark Brown int snd_soc_resume(struct device *dev) 12636ed25978SAndy Green { 12646f8ab4acSMark Brown struct snd_soc_card *card = dev_get_drvdata(dev); 1265f0fba2adSLiam Girdwood int i; 1266b9dd94a8SPeter Ujfalusi 126764ab9baaSMark Brown /* AC97 devices might have other drivers hanging off them so 126864ab9baaSMark Brown * need to resume immediately. Other drivers don't have that 126964ab9baaSMark Brown * problem and may take a substantial amount of time to resume 127064ab9baaSMark Brown * due to I/O costs and anti-pop so handle them out of line. 127164ab9baaSMark Brown */ 1272f0fba2adSLiam Girdwood for (i = 0; i < card->num_rtd; i++) { 1273f0fba2adSLiam Girdwood struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai; 1274f0fba2adSLiam Girdwood if (cpu_dai->driver->ac97_control) { 1275f0fba2adSLiam Girdwood dev_dbg(dev, "Resuming AC97 immediately\n"); 127664ab9baaSMark Brown soc_resume_deferred(&card->deferred_resume_work); 127764ab9baaSMark Brown } else { 1278f0fba2adSLiam Girdwood dev_dbg(dev, "Scheduling resume work\n"); 12796308419aSMark Brown if (!schedule_work(&card->deferred_resume_work)) 1280f0fba2adSLiam Girdwood dev_err(dev, "resume work item may be lost\n"); 1281f0fba2adSLiam Girdwood } 128264ab9baaSMark Brown } 12836ed25978SAndy Green 1284db2a4165SFrank Mandarino return 0; 1285db2a4165SFrank Mandarino } 12866f8ab4acSMark Brown EXPORT_SYMBOL_GPL(snd_soc_resume); 1287db2a4165SFrank Mandarino #else 12886f8ab4acSMark Brown #define snd_soc_suspend NULL 12896f8ab4acSMark Brown #define snd_soc_resume NULL 1290db2a4165SFrank Mandarino #endif 1291db2a4165SFrank Mandarino 129202a06d30SBarry Song static struct snd_soc_dai_ops null_dai_ops = { 129302a06d30SBarry Song }; 129402a06d30SBarry Song 1295f0fba2adSLiam Girdwood static int soc_bind_dai_link(struct snd_soc_card *card, int num) 1296db2a4165SFrank Mandarino { 1297f0fba2adSLiam Girdwood struct snd_soc_dai_link *dai_link = &card->dai_link[num]; 1298f0fba2adSLiam Girdwood struct snd_soc_pcm_runtime *rtd = &card->rtd[num]; 1299fe3e78e0SMark Brown struct snd_soc_codec *codec; 1300435c5e25SMark Brown struct snd_soc_platform *platform; 1301f0fba2adSLiam Girdwood struct snd_soc_dai *codec_dai, *cpu_dai; 1302db2a4165SFrank Mandarino 1303f0fba2adSLiam Girdwood if (rtd->complete) 1304f0fba2adSLiam Girdwood return 1; 1305f0fba2adSLiam Girdwood dev_dbg(card->dev, "binding %s at idx %d\n", dai_link->name, num); 13066308419aSMark Brown 1307f0fba2adSLiam Girdwood /* do we already have the CPU DAI for this link ? */ 1308f0fba2adSLiam Girdwood if (rtd->cpu_dai) { 1309f0fba2adSLiam Girdwood goto find_codec; 1310435c5e25SMark Brown } 1311f0fba2adSLiam Girdwood /* no, then find CPU DAI from registered DAIs*/ 1312f0fba2adSLiam Girdwood list_for_each_entry(cpu_dai, &dai_list, list) { 1313f0fba2adSLiam Girdwood if (!strcmp(cpu_dai->name, dai_link->cpu_dai_name)) { 1314f0fba2adSLiam Girdwood 1315f0fba2adSLiam Girdwood if (!try_module_get(cpu_dai->dev->driver->owner)) 1316f0fba2adSLiam Girdwood return -ENODEV; 1317f0fba2adSLiam Girdwood 1318f0fba2adSLiam Girdwood rtd->cpu_dai = cpu_dai; 1319f0fba2adSLiam Girdwood goto find_codec; 1320f0fba2adSLiam Girdwood } 1321f0fba2adSLiam Girdwood } 1322f0fba2adSLiam Girdwood dev_dbg(card->dev, "CPU DAI %s not registered\n", 1323f0fba2adSLiam Girdwood dai_link->cpu_dai_name); 1324f0fba2adSLiam Girdwood 1325f0fba2adSLiam Girdwood find_codec: 1326f0fba2adSLiam Girdwood /* do we already have the CODEC for this link ? */ 1327f0fba2adSLiam Girdwood if (rtd->codec) { 1328f0fba2adSLiam Girdwood goto find_platform; 1329c5af3a2eSMark Brown } 1330c5af3a2eSMark Brown 1331f0fba2adSLiam Girdwood /* no, then find CODEC from registered CODECs*/ 1332f0fba2adSLiam Girdwood list_for_each_entry(codec, &codec_list, list) { 1333f0fba2adSLiam Girdwood if (!strcmp(codec->name, dai_link->codec_name)) { 1334f0fba2adSLiam Girdwood rtd->codec = codec; 1335f0fba2adSLiam Girdwood 1336f0fba2adSLiam Girdwood /* CODEC found, so find CODEC DAI from registered DAIs from this CODEC*/ 1337f0fba2adSLiam Girdwood list_for_each_entry(codec_dai, &dai_list, list) { 1338f0fba2adSLiam Girdwood if (codec->dev == codec_dai->dev && 1339f0fba2adSLiam Girdwood !strcmp(codec_dai->name, dai_link->codec_dai_name)) { 1340f0fba2adSLiam Girdwood rtd->codec_dai = codec_dai; 1341f0fba2adSLiam Girdwood goto find_platform; 1342435c5e25SMark Brown } 1343f0fba2adSLiam Girdwood } 1344f0fba2adSLiam Girdwood dev_dbg(card->dev, "CODEC DAI %s not registered\n", 1345f0fba2adSLiam Girdwood dai_link->codec_dai_name); 1346f0fba2adSLiam Girdwood 1347f0fba2adSLiam Girdwood goto find_platform; 1348f0fba2adSLiam Girdwood } 1349f0fba2adSLiam Girdwood } 1350f0fba2adSLiam Girdwood dev_dbg(card->dev, "CODEC %s not registered\n", 1351f0fba2adSLiam Girdwood dai_link->codec_name); 1352f0fba2adSLiam Girdwood 1353f0fba2adSLiam Girdwood find_platform: 1354f0fba2adSLiam Girdwood /* do we already have the CODEC DAI for this link ? */ 1355f0fba2adSLiam Girdwood if (rtd->platform) { 1356f0fba2adSLiam Girdwood goto out; 1357f0fba2adSLiam Girdwood } 1358f0fba2adSLiam Girdwood /* no, then find CPU DAI from registered DAIs*/ 1359f0fba2adSLiam Girdwood list_for_each_entry(platform, &platform_list, list) { 1360f0fba2adSLiam Girdwood if (!strcmp(platform->name, dai_link->platform_name)) { 1361f0fba2adSLiam Girdwood rtd->platform = platform; 1362f0fba2adSLiam Girdwood goto out; 1363f0fba2adSLiam Girdwood } 1364435c5e25SMark Brown } 13656b05eda6SMark Brown 1366f0fba2adSLiam Girdwood dev_dbg(card->dev, "platform %s not registered\n", 1367f0fba2adSLiam Girdwood dai_link->platform_name); 1368f0fba2adSLiam Girdwood return 0; 1369f0fba2adSLiam Girdwood 1370f0fba2adSLiam Girdwood out: 1371f0fba2adSLiam Girdwood /* mark rtd as complete if we found all 4 of our client devices */ 1372f0fba2adSLiam Girdwood if (rtd->codec && rtd->codec_dai && rtd->platform && rtd->cpu_dai) { 1373f0fba2adSLiam Girdwood rtd->complete = 1; 1374f0fba2adSLiam Girdwood card->num_rtd++; 1375f0fba2adSLiam Girdwood } 1376f0fba2adSLiam Girdwood return 1; 13776b05eda6SMark Brown } 13786b05eda6SMark Brown 1379589c3563SJarkko Nikula static void soc_remove_codec(struct snd_soc_codec *codec) 1380589c3563SJarkko Nikula { 1381589c3563SJarkko Nikula int err; 1382589c3563SJarkko Nikula 1383589c3563SJarkko Nikula if (codec->driver->remove) { 1384589c3563SJarkko Nikula err = codec->driver->remove(codec); 1385589c3563SJarkko Nikula if (err < 0) 1386589c3563SJarkko Nikula dev_err(codec->dev, 1387589c3563SJarkko Nikula "asoc: failed to remove %s: %d\n", 1388589c3563SJarkko Nikula codec->name, err); 1389589c3563SJarkko Nikula } 1390589c3563SJarkko Nikula 1391589c3563SJarkko Nikula /* Make sure all DAPM widgets are freed */ 1392589c3563SJarkko Nikula snd_soc_dapm_free(&codec->dapm); 1393589c3563SJarkko Nikula 1394589c3563SJarkko Nikula soc_cleanup_codec_debugfs(codec); 1395589c3563SJarkko Nikula codec->probed = 0; 1396589c3563SJarkko Nikula list_del(&codec->card_list); 1397589c3563SJarkko Nikula module_put(codec->dev->driver->owner); 1398589c3563SJarkko Nikula } 1399589c3563SJarkko Nikula 1400f0fba2adSLiam Girdwood static void soc_remove_dai_link(struct snd_soc_card *card, int num) 1401f0fba2adSLiam Girdwood { 1402f0fba2adSLiam Girdwood struct snd_soc_pcm_runtime *rtd = &card->rtd[num]; 1403f0fba2adSLiam Girdwood struct snd_soc_codec *codec = rtd->codec; 1404f0fba2adSLiam Girdwood struct snd_soc_platform *platform = rtd->platform; 1405f0fba2adSLiam Girdwood struct snd_soc_dai *codec_dai = rtd->codec_dai, *cpu_dai = rtd->cpu_dai; 1406f0fba2adSLiam Girdwood int err; 1407f0fba2adSLiam Girdwood 1408f0fba2adSLiam Girdwood /* unregister the rtd device */ 1409f0fba2adSLiam Girdwood if (rtd->dev_registered) { 1410f0fba2adSLiam Girdwood device_remove_file(&rtd->dev, &dev_attr_pmdown_time); 1411589c3563SJarkko Nikula device_remove_file(&rtd->dev, &dev_attr_codec_reg); 1412f0fba2adSLiam Girdwood device_unregister(&rtd->dev); 1413f0fba2adSLiam Girdwood rtd->dev_registered = 0; 141402a06d30SBarry Song } 141502a06d30SBarry Song 1416f0fba2adSLiam Girdwood /* remove the CODEC DAI */ 1417f0fba2adSLiam Girdwood if (codec_dai && codec_dai->probed) { 1418f0fba2adSLiam Girdwood if (codec_dai->driver->remove) { 1419f0fba2adSLiam Girdwood err = codec_dai->driver->remove(codec_dai); 1420f0fba2adSLiam Girdwood if (err < 0) 1421f0fba2adSLiam Girdwood printk(KERN_ERR "asoc: failed to remove %s\n", codec_dai->name); 1422f0fba2adSLiam Girdwood } 1423f0fba2adSLiam Girdwood codec_dai->probed = 0; 1424f0fba2adSLiam Girdwood list_del(&codec_dai->card_list); 1425f0fba2adSLiam Girdwood } 1426f0fba2adSLiam Girdwood 1427f0fba2adSLiam Girdwood /* remove the platform */ 1428f0fba2adSLiam Girdwood if (platform && platform->probed) { 1429f0fba2adSLiam Girdwood if (platform->driver->remove) { 1430f0fba2adSLiam Girdwood err = platform->driver->remove(platform); 1431f0fba2adSLiam Girdwood if (err < 0) 1432f0fba2adSLiam Girdwood printk(KERN_ERR "asoc: failed to remove %s\n", platform->name); 1433f0fba2adSLiam Girdwood } 1434f0fba2adSLiam Girdwood platform->probed = 0; 1435f0fba2adSLiam Girdwood list_del(&platform->card_list); 1436f0fba2adSLiam Girdwood module_put(platform->dev->driver->owner); 1437f0fba2adSLiam Girdwood } 1438f0fba2adSLiam Girdwood 1439f0fba2adSLiam Girdwood /* remove the CODEC */ 1440589c3563SJarkko Nikula if (codec && codec->probed) 1441589c3563SJarkko Nikula soc_remove_codec(codec); 1442f0fba2adSLiam Girdwood 1443f0fba2adSLiam Girdwood /* remove the cpu_dai */ 1444f0fba2adSLiam Girdwood if (cpu_dai && cpu_dai->probed) { 1445f0fba2adSLiam Girdwood if (cpu_dai->driver->remove) { 1446f0fba2adSLiam Girdwood err = cpu_dai->driver->remove(cpu_dai); 1447f0fba2adSLiam Girdwood if (err < 0) 1448f0fba2adSLiam Girdwood printk(KERN_ERR "asoc: failed to remove %s\n", cpu_dai->name); 1449f0fba2adSLiam Girdwood } 1450f0fba2adSLiam Girdwood cpu_dai->probed = 0; 1451f0fba2adSLiam Girdwood list_del(&cpu_dai->card_list); 1452f0fba2adSLiam Girdwood module_put(cpu_dai->dev->driver->owner); 1453f0fba2adSLiam Girdwood } 1454f0fba2adSLiam Girdwood } 1455f0fba2adSLiam Girdwood 1456ead9b919SJarkko Nikula static void soc_set_name_prefix(struct snd_soc_card *card, 1457ead9b919SJarkko Nikula struct snd_soc_codec *codec) 1458ead9b919SJarkko Nikula { 1459ead9b919SJarkko Nikula int i; 1460ead9b919SJarkko Nikula 1461ff819b83SDimitris Papastamos if (card->codec_conf == NULL) 1462ead9b919SJarkko Nikula return; 1463ead9b919SJarkko Nikula 1464ff819b83SDimitris Papastamos for (i = 0; i < card->num_configs; i++) { 1465ff819b83SDimitris Papastamos struct snd_soc_codec_conf *map = &card->codec_conf[i]; 1466ead9b919SJarkko Nikula if (map->dev_name && !strcmp(codec->name, map->dev_name)) { 1467ead9b919SJarkko Nikula codec->name_prefix = map->name_prefix; 1468ead9b919SJarkko Nikula break; 1469ead9b919SJarkko Nikula } 1470ead9b919SJarkko Nikula } 1471ead9b919SJarkko Nikula } 1472ead9b919SJarkko Nikula 1473589c3563SJarkko Nikula static int soc_probe_codec(struct snd_soc_card *card, 1474589c3563SJarkko Nikula struct snd_soc_codec *codec) 1475589c3563SJarkko Nikula { 1476589c3563SJarkko Nikula int ret = 0; 147789b95ac0SMark Brown const struct snd_soc_codec_driver *driver = codec->driver; 1478589c3563SJarkko Nikula 1479589c3563SJarkko Nikula codec->card = card; 1480589c3563SJarkko Nikula codec->dapm.card = card; 1481589c3563SJarkko Nikula soc_set_name_prefix(card, codec); 1482589c3563SJarkko Nikula 148370d29331SJarkko Nikula if (!try_module_get(codec->dev->driver->owner)) 148470d29331SJarkko Nikula return -ENODEV; 148570d29331SJarkko Nikula 148689b95ac0SMark Brown if (driver->probe) { 148789b95ac0SMark Brown ret = driver->probe(codec); 1488589c3563SJarkko Nikula if (ret < 0) { 1489589c3563SJarkko Nikula dev_err(codec->dev, 1490589c3563SJarkko Nikula "asoc: failed to probe CODEC %s: %d\n", 1491589c3563SJarkko Nikula codec->name, ret); 149270d29331SJarkko Nikula goto err_probe; 1493589c3563SJarkko Nikula } 1494589c3563SJarkko Nikula } 1495589c3563SJarkko Nikula 149689b95ac0SMark Brown if (driver->dapm_widgets) 149789b95ac0SMark Brown snd_soc_dapm_new_controls(&codec->dapm, driver->dapm_widgets, 149889b95ac0SMark Brown driver->num_dapm_widgets); 149989b95ac0SMark Brown if (driver->dapm_routes) 150089b95ac0SMark Brown snd_soc_dapm_add_routes(&codec->dapm, driver->dapm_routes, 150189b95ac0SMark Brown driver->num_dapm_routes); 150289b95ac0SMark Brown 1503589c3563SJarkko Nikula soc_init_codec_debugfs(codec); 1504589c3563SJarkko Nikula 1505589c3563SJarkko Nikula /* mark codec as probed and add to card codec list */ 1506589c3563SJarkko Nikula codec->probed = 1; 1507589c3563SJarkko Nikula list_add(&codec->card_list, &card->codec_dev_list); 15087be31be8SJarkko Nikula list_add(&codec->dapm.list, &card->dapm_list); 1509589c3563SJarkko Nikula 151070d29331SJarkko Nikula return 0; 151170d29331SJarkko Nikula 151270d29331SJarkko Nikula err_probe: 151370d29331SJarkko Nikula module_put(codec->dev->driver->owner); 151470d29331SJarkko Nikula 1515589c3563SJarkko Nikula return ret; 1516589c3563SJarkko Nikula } 1517589c3563SJarkko Nikula 1518f0fba2adSLiam Girdwood static void rtd_release(struct device *dev) {} 1519f0fba2adSLiam Girdwood 1520589c3563SJarkko Nikula static int soc_post_component_init(struct snd_soc_card *card, 1521589c3563SJarkko Nikula struct snd_soc_codec *codec, 1522589c3563SJarkko Nikula int num, int dailess) 1523589c3563SJarkko Nikula { 1524589c3563SJarkko Nikula struct snd_soc_dai_link *dai_link = NULL; 1525589c3563SJarkko Nikula struct snd_soc_aux_dev *aux_dev = NULL; 1526589c3563SJarkko Nikula struct snd_soc_pcm_runtime *rtd; 1527589c3563SJarkko Nikula const char *temp, *name; 1528589c3563SJarkko Nikula int ret = 0; 1529589c3563SJarkko Nikula 1530589c3563SJarkko Nikula if (!dailess) { 1531589c3563SJarkko Nikula dai_link = &card->dai_link[num]; 1532589c3563SJarkko Nikula rtd = &card->rtd[num]; 1533589c3563SJarkko Nikula name = dai_link->name; 1534589c3563SJarkko Nikula } else { 1535589c3563SJarkko Nikula aux_dev = &card->aux_dev[num]; 1536589c3563SJarkko Nikula rtd = &card->rtd_aux[num]; 1537589c3563SJarkko Nikula name = aux_dev->name; 1538589c3563SJarkko Nikula } 15390962bb21SJanusz Krzysztofik rtd->card = card; 1540589c3563SJarkko Nikula 1541589c3563SJarkko Nikula /* machine controls, routes and widgets are not prefixed */ 1542589c3563SJarkko Nikula temp = codec->name_prefix; 1543589c3563SJarkko Nikula codec->name_prefix = NULL; 1544589c3563SJarkko Nikula 1545589c3563SJarkko Nikula /* do machine specific initialization */ 1546589c3563SJarkko Nikula if (!dailess && dai_link->init) 1547589c3563SJarkko Nikula ret = dai_link->init(rtd); 1548589c3563SJarkko Nikula else if (dailess && aux_dev->init) 1549589c3563SJarkko Nikula ret = aux_dev->init(&codec->dapm); 1550589c3563SJarkko Nikula if (ret < 0) { 1551589c3563SJarkko Nikula dev_err(card->dev, "asoc: failed to init %s: %d\n", name, ret); 1552589c3563SJarkko Nikula return ret; 1553589c3563SJarkko Nikula } 1554589c3563SJarkko Nikula codec->name_prefix = temp; 1555589c3563SJarkko Nikula 1556589c3563SJarkko Nikula /* Make sure all DAPM widgets are instantiated */ 1557589c3563SJarkko Nikula snd_soc_dapm_new_widgets(&codec->dapm); 1558589c3563SJarkko Nikula 1559589c3563SJarkko Nikula /* register the rtd device */ 1560589c3563SJarkko Nikula rtd->codec = codec; 1561589c3563SJarkko Nikula rtd->dev.parent = card->dev; 1562589c3563SJarkko Nikula rtd->dev.release = rtd_release; 1563589c3563SJarkko Nikula rtd->dev.init_name = name; 1564589c3563SJarkko Nikula ret = device_register(&rtd->dev); 1565589c3563SJarkko Nikula if (ret < 0) { 1566589c3563SJarkko Nikula dev_err(card->dev, 1567589c3563SJarkko Nikula "asoc: failed to register runtime device: %d\n", ret); 1568589c3563SJarkko Nikula return ret; 1569589c3563SJarkko Nikula } 1570589c3563SJarkko Nikula rtd->dev_registered = 1; 1571589c3563SJarkko Nikula 1572589c3563SJarkko Nikula /* add DAPM sysfs entries for this codec */ 1573589c3563SJarkko Nikula ret = snd_soc_dapm_sys_add(&rtd->dev); 1574589c3563SJarkko Nikula if (ret < 0) 1575589c3563SJarkko Nikula dev_err(codec->dev, 1576589c3563SJarkko Nikula "asoc: failed to add codec dapm sysfs entries: %d\n", 1577589c3563SJarkko Nikula ret); 1578589c3563SJarkko Nikula 1579589c3563SJarkko Nikula /* add codec sysfs entries */ 1580589c3563SJarkko Nikula ret = device_create_file(&rtd->dev, &dev_attr_codec_reg); 1581589c3563SJarkko Nikula if (ret < 0) 1582589c3563SJarkko Nikula dev_err(codec->dev, 1583589c3563SJarkko Nikula "asoc: failed to add codec sysfs files: %d\n", ret); 1584589c3563SJarkko Nikula 1585589c3563SJarkko Nikula return 0; 1586589c3563SJarkko Nikula } 1587589c3563SJarkko Nikula 1588f0fba2adSLiam Girdwood static int soc_probe_dai_link(struct snd_soc_card *card, int num) 1589f0fba2adSLiam Girdwood { 1590f0fba2adSLiam Girdwood struct snd_soc_dai_link *dai_link = &card->dai_link[num]; 1591f0fba2adSLiam Girdwood struct snd_soc_pcm_runtime *rtd = &card->rtd[num]; 1592f0fba2adSLiam Girdwood struct snd_soc_codec *codec = rtd->codec; 1593f0fba2adSLiam Girdwood struct snd_soc_platform *platform = rtd->platform; 1594f0fba2adSLiam Girdwood struct snd_soc_dai *codec_dai = rtd->codec_dai, *cpu_dai = rtd->cpu_dai; 1595f0fba2adSLiam Girdwood int ret; 1596f0fba2adSLiam Girdwood 1597f0fba2adSLiam Girdwood dev_dbg(card->dev, "probe %s dai link %d\n", card->name, num); 1598f0fba2adSLiam Girdwood 1599f0fba2adSLiam Girdwood /* config components */ 1600f0fba2adSLiam Girdwood codec_dai->codec = codec; 1601f0fba2adSLiam Girdwood cpu_dai->platform = platform; 1602f0fba2adSLiam Girdwood codec_dai->card = card; 1603f0fba2adSLiam Girdwood cpu_dai->card = card; 1604f0fba2adSLiam Girdwood 1605f0fba2adSLiam Girdwood /* set default power off timeout */ 1606f0fba2adSLiam Girdwood rtd->pmdown_time = pmdown_time; 1607f0fba2adSLiam Girdwood 1608f0fba2adSLiam Girdwood /* probe the cpu_dai */ 1609f0fba2adSLiam Girdwood if (!cpu_dai->probed) { 1610f0fba2adSLiam Girdwood if (cpu_dai->driver->probe) { 1611f0fba2adSLiam Girdwood ret = cpu_dai->driver->probe(cpu_dai); 1612f0fba2adSLiam Girdwood if (ret < 0) { 1613f0fba2adSLiam Girdwood printk(KERN_ERR "asoc: failed to probe CPU DAI %s\n", 1614f0fba2adSLiam Girdwood cpu_dai->name); 1615f0fba2adSLiam Girdwood return ret; 1616f0fba2adSLiam Girdwood } 1617f0fba2adSLiam Girdwood } 1618f0fba2adSLiam Girdwood cpu_dai->probed = 1; 1619f0fba2adSLiam Girdwood /* mark cpu_dai as probed and add to card cpu_dai list */ 1620f0fba2adSLiam Girdwood list_add(&cpu_dai->card_list, &card->dai_dev_list); 1621f0fba2adSLiam Girdwood } 1622f0fba2adSLiam Girdwood 1623f0fba2adSLiam Girdwood /* probe the CODEC */ 1624f0fba2adSLiam Girdwood if (!codec->probed) { 1625589c3563SJarkko Nikula ret = soc_probe_codec(card, codec); 1626589c3563SJarkko Nikula if (ret < 0) 1627f0fba2adSLiam Girdwood return ret; 1628f0fba2adSLiam Girdwood } 1629f0fba2adSLiam Girdwood 1630f0fba2adSLiam Girdwood /* probe the platform */ 1631f0fba2adSLiam Girdwood if (!platform->probed) { 163270d29331SJarkko Nikula if (!try_module_get(platform->dev->driver->owner)) 163370d29331SJarkko Nikula return -ENODEV; 163470d29331SJarkko Nikula 1635f0fba2adSLiam Girdwood if (platform->driver->probe) { 1636f0fba2adSLiam Girdwood ret = platform->driver->probe(platform); 1637f0fba2adSLiam Girdwood if (ret < 0) { 1638f0fba2adSLiam Girdwood printk(KERN_ERR "asoc: failed to probe platform %s\n", 1639f0fba2adSLiam Girdwood platform->name); 164070d29331SJarkko Nikula module_put(platform->dev->driver->owner); 1641f0fba2adSLiam Girdwood return ret; 1642f0fba2adSLiam Girdwood } 1643f0fba2adSLiam Girdwood } 1644f0fba2adSLiam Girdwood /* mark platform as probed and add to card platform list */ 1645f0fba2adSLiam Girdwood platform->probed = 1; 1646f0fba2adSLiam Girdwood list_add(&platform->card_list, &card->platform_dev_list); 1647f0fba2adSLiam Girdwood } 1648f0fba2adSLiam Girdwood 1649f0fba2adSLiam Girdwood /* probe the CODEC DAI */ 1650f0fba2adSLiam Girdwood if (!codec_dai->probed) { 1651f0fba2adSLiam Girdwood if (codec_dai->driver->probe) { 1652f0fba2adSLiam Girdwood ret = codec_dai->driver->probe(codec_dai); 1653f0fba2adSLiam Girdwood if (ret < 0) { 1654f0fba2adSLiam Girdwood printk(KERN_ERR "asoc: failed to probe CODEC DAI %s\n", 1655f0fba2adSLiam Girdwood codec_dai->name); 1656f0fba2adSLiam Girdwood return ret; 1657f0fba2adSLiam Girdwood } 1658f0fba2adSLiam Girdwood } 1659f0fba2adSLiam Girdwood 1660f0fba2adSLiam Girdwood /* mark cpu_dai as probed and add to card cpu_dai list */ 1661f0fba2adSLiam Girdwood codec_dai->probed = 1; 1662f0fba2adSLiam Girdwood list_add(&codec_dai->card_list, &card->dai_dev_list); 1663f0fba2adSLiam Girdwood } 1664f0fba2adSLiam Girdwood 1665f0fba2adSLiam Girdwood /* DAPM dai link stream work */ 1666f0fba2adSLiam Girdwood INIT_DELAYED_WORK(&rtd->delayed_work, close_delayed_work); 1667f0fba2adSLiam Girdwood 1668589c3563SJarkko Nikula ret = soc_post_component_init(card, codec, num, 0); 1669589c3563SJarkko Nikula if (ret) 1670f0fba2adSLiam Girdwood return ret; 1671f0fba2adSLiam Girdwood 1672f0fba2adSLiam Girdwood ret = device_create_file(&rtd->dev, &dev_attr_pmdown_time); 1673f0fba2adSLiam Girdwood if (ret < 0) 1674f0fba2adSLiam Girdwood printk(KERN_WARNING "asoc: failed to add pmdown_time sysfs\n"); 1675f0fba2adSLiam Girdwood 1676f0fba2adSLiam Girdwood /* create the pcm */ 1677f0fba2adSLiam Girdwood ret = soc_new_pcm(rtd, num); 1678f0fba2adSLiam Girdwood if (ret < 0) { 1679f0fba2adSLiam Girdwood printk(KERN_ERR "asoc: can't create pcm %s\n", dai_link->stream_name); 1680f0fba2adSLiam Girdwood return ret; 1681f0fba2adSLiam Girdwood } 1682f0fba2adSLiam Girdwood 1683f0fba2adSLiam Girdwood /* add platform data for AC97 devices */ 1684f0fba2adSLiam Girdwood if (rtd->codec_dai->driver->ac97_control) 1685f0fba2adSLiam Girdwood snd_ac97_dev_add_pdata(codec->ac97, rtd->cpu_dai->ac97_pdata); 1686f0fba2adSLiam Girdwood 1687f0fba2adSLiam Girdwood return 0; 1688f0fba2adSLiam Girdwood } 1689f0fba2adSLiam Girdwood 1690f0fba2adSLiam Girdwood #ifdef CONFIG_SND_SOC_AC97_BUS 1691f0fba2adSLiam Girdwood static int soc_register_ac97_dai_link(struct snd_soc_pcm_runtime *rtd) 1692f0fba2adSLiam Girdwood { 1693f0fba2adSLiam Girdwood int ret; 1694f0fba2adSLiam Girdwood 1695f0fba2adSLiam Girdwood /* Only instantiate AC97 if not already done by the adaptor 1696f0fba2adSLiam Girdwood * for the generic AC97 subsystem. 16976b05eda6SMark Brown */ 1698f0fba2adSLiam Girdwood if (rtd->codec_dai->driver->ac97_control && !rtd->codec->ac97_registered) { 16990562f788SMika Westerberg /* 17000562f788SMika Westerberg * It is possible that the AC97 device is already registered to 17010562f788SMika Westerberg * the device subsystem. This happens when the device is created 17020562f788SMika Westerberg * via snd_ac97_mixer(). Currently only SoC codec that does so 17030562f788SMika Westerberg * is the generic AC97 glue but others migh emerge. 17040562f788SMika Westerberg * 17050562f788SMika Westerberg * In those cases we don't try to register the device again. 17060562f788SMika Westerberg */ 17070562f788SMika Westerberg if (!rtd->codec->ac97_created) 17080562f788SMika Westerberg return 0; 1709f0fba2adSLiam Girdwood 1710f0fba2adSLiam Girdwood ret = soc_ac97_dev_register(rtd->codec); 1711f0fba2adSLiam Girdwood if (ret < 0) { 1712f0fba2adSLiam Girdwood printk(KERN_ERR "asoc: AC97 device register failed\n"); 1713f0fba2adSLiam Girdwood return ret; 1714435c5e25SMark Brown } 1715435c5e25SMark Brown 1716f0fba2adSLiam Girdwood rtd->codec->ac97_registered = 1; 1717f0fba2adSLiam Girdwood } 1718f0fba2adSLiam Girdwood return 0; 1719f0fba2adSLiam Girdwood } 1720435c5e25SMark Brown 1721f0fba2adSLiam Girdwood static void soc_unregister_ac97_dai_link(struct snd_soc_codec *codec) 1722f0fba2adSLiam Girdwood { 1723f0fba2adSLiam Girdwood if (codec->ac97_registered) { 1724f0fba2adSLiam Girdwood soc_ac97_dev_unregister(codec); 1725f0fba2adSLiam Girdwood codec->ac97_registered = 0; 1726f0fba2adSLiam Girdwood } 1727f0fba2adSLiam Girdwood } 1728f0fba2adSLiam Girdwood #endif 1729435c5e25SMark Brown 17302eea392dSJarkko Nikula static int soc_probe_aux_dev(struct snd_soc_card *card, int num) 17312eea392dSJarkko Nikula { 17322eea392dSJarkko Nikula struct snd_soc_aux_dev *aux_dev = &card->aux_dev[num]; 17332eea392dSJarkko Nikula struct snd_soc_codec *codec; 1734676ad98aSJarkko Nikula int ret = -ENODEV; 17352eea392dSJarkko Nikula 17362eea392dSJarkko Nikula /* find CODEC from registered CODECs*/ 17372eea392dSJarkko Nikula list_for_each_entry(codec, &codec_list, list) { 17382eea392dSJarkko Nikula if (!strcmp(codec->name, aux_dev->codec_name)) { 17392eea392dSJarkko Nikula if (codec->probed) { 17402eea392dSJarkko Nikula dev_err(codec->dev, 17412eea392dSJarkko Nikula "asoc: codec already probed"); 17422eea392dSJarkko Nikula ret = -EBUSY; 17432eea392dSJarkko Nikula goto out; 17442eea392dSJarkko Nikula } 1745676ad98aSJarkko Nikula goto found; 17462eea392dSJarkko Nikula } 17472eea392dSJarkko Nikula } 1748676ad98aSJarkko Nikula /* codec not found */ 1749676ad98aSJarkko Nikula dev_err(card->dev, "asoc: codec %s not found", aux_dev->codec_name); 1750676ad98aSJarkko Nikula goto out; 17512eea392dSJarkko Nikula 1752676ad98aSJarkko Nikula found: 1753589c3563SJarkko Nikula ret = soc_probe_codec(card, codec); 17542eea392dSJarkko Nikula if (ret < 0) 1755589c3563SJarkko Nikula return ret; 17562eea392dSJarkko Nikula 1757589c3563SJarkko Nikula ret = soc_post_component_init(card, codec, num, 1); 17582eea392dSJarkko Nikula 17592eea392dSJarkko Nikula out: 17602eea392dSJarkko Nikula return ret; 17612eea392dSJarkko Nikula } 17622eea392dSJarkko Nikula 17632eea392dSJarkko Nikula static void soc_remove_aux_dev(struct snd_soc_card *card, int num) 17642eea392dSJarkko Nikula { 17652eea392dSJarkko Nikula struct snd_soc_pcm_runtime *rtd = &card->rtd_aux[num]; 17662eea392dSJarkko Nikula struct snd_soc_codec *codec = rtd->codec; 17672eea392dSJarkko Nikula 17682eea392dSJarkko Nikula /* unregister the rtd device */ 17692eea392dSJarkko Nikula if (rtd->dev_registered) { 1770589c3563SJarkko Nikula device_remove_file(&rtd->dev, &dev_attr_codec_reg); 17712eea392dSJarkko Nikula device_unregister(&rtd->dev); 17722eea392dSJarkko Nikula rtd->dev_registered = 0; 17732eea392dSJarkko Nikula } 17742eea392dSJarkko Nikula 1775589c3563SJarkko Nikula if (codec && codec->probed) 1776589c3563SJarkko Nikula soc_remove_codec(codec); 17772eea392dSJarkko Nikula } 17782eea392dSJarkko Nikula 1779fdf0f54dSDimitris Papastamos static int snd_soc_init_codec_cache(struct snd_soc_codec *codec, 1780fdf0f54dSDimitris Papastamos enum snd_soc_compress_type compress_type) 1781fdf0f54dSDimitris Papastamos { 1782fdf0f54dSDimitris Papastamos int ret; 1783fdf0f54dSDimitris Papastamos 1784fdf0f54dSDimitris Papastamos if (codec->cache_init) 1785fdf0f54dSDimitris Papastamos return 0; 1786fdf0f54dSDimitris Papastamos 1787fdf0f54dSDimitris Papastamos /* override the compress_type if necessary */ 1788fdf0f54dSDimitris Papastamos if (compress_type && codec->compress_type != compress_type) 1789fdf0f54dSDimitris Papastamos codec->compress_type = compress_type; 1790fdf0f54dSDimitris Papastamos ret = snd_soc_cache_init(codec); 1791fdf0f54dSDimitris Papastamos if (ret < 0) { 1792fdf0f54dSDimitris Papastamos dev_err(codec->dev, "Failed to set cache compression type: %d\n", 1793fdf0f54dSDimitris Papastamos ret); 1794fdf0f54dSDimitris Papastamos return ret; 1795fdf0f54dSDimitris Papastamos } 1796fdf0f54dSDimitris Papastamos codec->cache_init = 1; 1797fdf0f54dSDimitris Papastamos return 0; 1798fdf0f54dSDimitris Papastamos } 1799fdf0f54dSDimitris Papastamos 1800f0fba2adSLiam Girdwood static void snd_soc_instantiate_card(struct snd_soc_card *card) 1801f0fba2adSLiam Girdwood { 1802fdf0f54dSDimitris Papastamos struct snd_soc_codec *codec; 1803fdf0f54dSDimitris Papastamos struct snd_soc_codec_conf *codec_conf; 1804fdf0f54dSDimitris Papastamos enum snd_soc_compress_type compress_type; 1805f0fba2adSLiam Girdwood int ret, i; 180696dd3622SMark Brown 1807f0fba2adSLiam Girdwood mutex_lock(&card->mutex); 1808f0fba2adSLiam Girdwood 1809f0fba2adSLiam Girdwood if (card->instantiated) { 1810f0fba2adSLiam Girdwood mutex_unlock(&card->mutex); 1811435c5e25SMark Brown return; 1812db2a4165SFrank Mandarino } 1813db2a4165SFrank Mandarino 1814f0fba2adSLiam Girdwood /* bind DAIs */ 1815f0fba2adSLiam Girdwood for (i = 0; i < card->num_links; i++) 1816f0fba2adSLiam Girdwood soc_bind_dai_link(card, i); 1817f0fba2adSLiam Girdwood 1818f0fba2adSLiam Girdwood /* bind completed ? */ 1819f0fba2adSLiam Girdwood if (card->num_rtd != card->num_links) { 1820f0fba2adSLiam Girdwood mutex_unlock(&card->mutex); 1821f0fba2adSLiam Girdwood return; 1822db2a4165SFrank Mandarino } 1823db2a4165SFrank Mandarino 1824fdf0f54dSDimitris Papastamos /* initialize the register cache for each available codec */ 1825fdf0f54dSDimitris Papastamos list_for_each_entry(codec, &codec_list, list) { 1826fdf0f54dSDimitris Papastamos if (codec->cache_init) 1827fdf0f54dSDimitris Papastamos continue; 1828861f2fafSDimitris Papastamos /* by default we don't override the compress_type */ 1829861f2fafSDimitris Papastamos compress_type = 0; 1830fdf0f54dSDimitris Papastamos /* check to see if we need to override the compress_type */ 1831fdf0f54dSDimitris Papastamos for (i = 0; i < card->num_configs; ++i) { 1832fdf0f54dSDimitris Papastamos codec_conf = &card->codec_conf[i]; 1833fdf0f54dSDimitris Papastamos if (!strcmp(codec->name, codec_conf->dev_name)) { 1834fdf0f54dSDimitris Papastamos compress_type = codec_conf->compress_type; 1835fdf0f54dSDimitris Papastamos if (compress_type && compress_type 1836fdf0f54dSDimitris Papastamos != codec->compress_type) 1837fdf0f54dSDimitris Papastamos break; 1838fdf0f54dSDimitris Papastamos } 1839fdf0f54dSDimitris Papastamos } 1840fdf0f54dSDimitris Papastamos ret = snd_soc_init_codec_cache(codec, compress_type); 1841fdf0f54dSDimitris Papastamos if (ret < 0) { 1842fdf0f54dSDimitris Papastamos mutex_unlock(&card->mutex); 1843fdf0f54dSDimitris Papastamos return; 1844fdf0f54dSDimitris Papastamos } 1845fdf0f54dSDimitris Papastamos } 1846fdf0f54dSDimitris Papastamos 1847f0fba2adSLiam Girdwood /* card bind complete so register a sound card */ 1848f0fba2adSLiam Girdwood ret = snd_card_create(SNDRV_DEFAULT_IDX1, SNDRV_DEFAULT_STR1, 1849f0fba2adSLiam Girdwood card->owner, 0, &card->snd_card); 1850f0fba2adSLiam Girdwood if (ret < 0) { 1851f0fba2adSLiam Girdwood printk(KERN_ERR "asoc: can't create sound card for card %s\n", 1852f0fba2adSLiam Girdwood card->name); 1853f0fba2adSLiam Girdwood mutex_unlock(&card->mutex); 1854f0fba2adSLiam Girdwood return; 1855db2a4165SFrank Mandarino } 1856f0fba2adSLiam Girdwood card->snd_card->dev = card->dev; 1857db2a4165SFrank Mandarino 1858e37a4970SMark Brown card->dapm.bias_level = SND_SOC_BIAS_OFF; 1859e37a4970SMark Brown card->dapm.dev = card->dev; 1860e37a4970SMark Brown card->dapm.card = card; 1861e37a4970SMark Brown list_add(&card->dapm.list, &card->dapm_list); 1862e37a4970SMark Brown 186388ee1c61SMark Brown #ifdef CONFIG_PM_SLEEP 18646ed25978SAndy Green /* deferred resume work */ 18656308419aSMark Brown INIT_WORK(&card->deferred_resume_work, soc_resume_deferred); 18661301a964SRandy Dunlap #endif 18676ed25978SAndy Green 1868f0fba2adSLiam Girdwood /* initialise the sound card only once */ 1869f0fba2adSLiam Girdwood if (card->probe) { 1870e7361ec4SMark Brown ret = card->probe(card); 1871f0fba2adSLiam Girdwood if (ret < 0) 1872f0fba2adSLiam Girdwood goto card_probe_error; 1873f0fba2adSLiam Girdwood } 1874f0fba2adSLiam Girdwood 1875fe3e78e0SMark Brown for (i = 0; i < card->num_links; i++) { 1876f0fba2adSLiam Girdwood ret = soc_probe_dai_link(card, i); 1877fe3e78e0SMark Brown if (ret < 0) { 1878321de0d0SMark Brown pr_err("asoc: failed to instantiate card %s: %d\n", 1879321de0d0SMark Brown card->name, ret); 1880f0fba2adSLiam Girdwood goto probe_dai_err; 1881fe3e78e0SMark Brown } 1882fe3e78e0SMark Brown } 1883fe3e78e0SMark Brown 18842eea392dSJarkko Nikula for (i = 0; i < card->num_aux_devs; i++) { 18852eea392dSJarkko Nikula ret = soc_probe_aux_dev(card, i); 18862eea392dSJarkko Nikula if (ret < 0) { 18872eea392dSJarkko Nikula pr_err("asoc: failed to add auxiliary devices %s: %d\n", 18882eea392dSJarkko Nikula card->name, ret); 18892eea392dSJarkko Nikula goto probe_aux_dev_err; 18902eea392dSJarkko Nikula } 18912eea392dSJarkko Nikula } 18922eea392dSJarkko Nikula 1893b8ad29deSMark Brown if (card->dapm_widgets) 1894b8ad29deSMark Brown snd_soc_dapm_new_controls(&card->dapm, card->dapm_widgets, 1895b8ad29deSMark Brown card->num_dapm_widgets); 1896b8ad29deSMark Brown if (card->dapm_routes) 1897b8ad29deSMark Brown snd_soc_dapm_add_routes(&card->dapm, card->dapm_routes, 1898b8ad29deSMark Brown card->num_dapm_routes); 1899b8ad29deSMark Brown 1900a2721fd9SMark Brown #ifdef CONFIG_DEBUG_FS 1901e37a4970SMark Brown card->dapm.debugfs_dapm = debugfs_create_dir("dapm", 1902e37a4970SMark Brown card->debugfs_card_root); 1903e37a4970SMark Brown if (!card->dapm.debugfs_dapm) 1904e37a4970SMark Brown printk(KERN_WARNING 1905e37a4970SMark Brown "Failed to create card DAPM debugfs directory\n"); 1906e37a4970SMark Brown 1907e37a4970SMark Brown snd_soc_dapm_debugfs_init(&card->dapm); 1908a2721fd9SMark Brown #endif 1909e37a4970SMark Brown 1910f0fba2adSLiam Girdwood snprintf(card->snd_card->shortname, sizeof(card->snd_card->shortname), 1911fe3e78e0SMark Brown "%s", card->name); 1912f0fba2adSLiam Girdwood snprintf(card->snd_card->longname, sizeof(card->snd_card->longname), 1913f0fba2adSLiam Girdwood "%s", card->name); 1914fe3e78e0SMark Brown 191528e9ad92SMark Brown if (card->late_probe) { 191628e9ad92SMark Brown ret = card->late_probe(card); 191728e9ad92SMark Brown if (ret < 0) { 191828e9ad92SMark Brown dev_err(card->dev, "%s late_probe() failed: %d\n", 191928e9ad92SMark Brown card->name, ret); 192028e9ad92SMark Brown goto probe_aux_dev_err; 192128e9ad92SMark Brown } 192228e9ad92SMark Brown } 192328e9ad92SMark Brown 1924f0fba2adSLiam Girdwood ret = snd_card_register(card->snd_card); 1925fe3e78e0SMark Brown if (ret < 0) { 1926f0fba2adSLiam Girdwood printk(KERN_ERR "asoc: failed to register soundcard for %s\n", card->name); 19276b3ed785SAxel Lin goto probe_aux_dev_err; 1928fe3e78e0SMark Brown } 1929fe3e78e0SMark Brown 1930fe3e78e0SMark Brown #ifdef CONFIG_SND_SOC_AC97_BUS 1931f0fba2adSLiam Girdwood /* register any AC97 codecs */ 1932f0fba2adSLiam Girdwood for (i = 0; i < card->num_rtd; i++) { 1933f0fba2adSLiam Girdwood ret = soc_register_ac97_dai_link(&card->rtd[i]); 1934fe3e78e0SMark Brown if (ret < 0) { 1935f0fba2adSLiam Girdwood printk(KERN_ERR "asoc: failed to register AC97 %s\n", card->name); 19366b3ed785SAxel Lin while (--i >= 0) 19377d8316dfSMark Brown soc_unregister_ac97_dai_link(card->rtd[i].codec); 19386b3ed785SAxel Lin goto probe_aux_dev_err; 1939fe3e78e0SMark Brown } 1940fe3e78e0SMark Brown } 1941fe3e78e0SMark Brown #endif 1942fe3e78e0SMark Brown 1943435c5e25SMark Brown card->instantiated = 1; 1944f0fba2adSLiam Girdwood mutex_unlock(&card->mutex); 1945435c5e25SMark Brown return; 1946db2a4165SFrank Mandarino 19472eea392dSJarkko Nikula probe_aux_dev_err: 19482eea392dSJarkko Nikula for (i = 0; i < card->num_aux_devs; i++) 19492eea392dSJarkko Nikula soc_remove_aux_dev(card, i); 19502eea392dSJarkko Nikula 1951f0fba2adSLiam Girdwood probe_dai_err: 1952f0fba2adSLiam Girdwood for (i = 0; i < card->num_links; i++) 1953f0fba2adSLiam Girdwood soc_remove_dai_link(card, i); 1954fe3e78e0SMark Brown 1955f0fba2adSLiam Girdwood card_probe_error: 195687506549SMark Brown if (card->remove) 1957e7361ec4SMark Brown card->remove(card); 1958f0fba2adSLiam Girdwood 1959f0fba2adSLiam Girdwood snd_card_free(card->snd_card); 1960f0fba2adSLiam Girdwood 1961f0fba2adSLiam Girdwood mutex_unlock(&card->mutex); 1962435c5e25SMark Brown } 1963db2a4165SFrank Mandarino 1964435c5e25SMark Brown /* 1965421f91d2SUwe Kleine-König * Attempt to initialise any uninitialised cards. Must be called with 1966435c5e25SMark Brown * client_mutex. 1967435c5e25SMark Brown */ 1968435c5e25SMark Brown static void snd_soc_instantiate_cards(void) 1969435c5e25SMark Brown { 1970435c5e25SMark Brown struct snd_soc_card *card; 1971435c5e25SMark Brown list_for_each_entry(card, &card_list, list) 1972435c5e25SMark Brown snd_soc_instantiate_card(card); 1973435c5e25SMark Brown } 1974435c5e25SMark Brown 1975435c5e25SMark Brown /* probes a new socdev */ 1976435c5e25SMark Brown static int soc_probe(struct platform_device *pdev) 1977435c5e25SMark Brown { 1978f0fba2adSLiam Girdwood struct snd_soc_card *card = platform_get_drvdata(pdev); 1979435c5e25SMark Brown int ret = 0; 1980435c5e25SMark Brown 198170a7ca34SVinod Koul /* 198270a7ca34SVinod Koul * no card, so machine driver should be registering card 198370a7ca34SVinod Koul * we should not be here in that case so ret error 198470a7ca34SVinod Koul */ 198570a7ca34SVinod Koul if (!card) 198670a7ca34SVinod Koul return -EINVAL; 198770a7ca34SVinod Koul 1988435c5e25SMark Brown /* Bodge while we unpick instantiation */ 1989435c5e25SMark Brown card->dev = &pdev->dev; 1990f0fba2adSLiam Girdwood 1991435c5e25SMark Brown ret = snd_soc_register_card(card); 1992435c5e25SMark Brown if (ret != 0) { 1993435c5e25SMark Brown dev_err(&pdev->dev, "Failed to register card\n"); 1994db2a4165SFrank Mandarino return ret; 1995db2a4165SFrank Mandarino } 1996db2a4165SFrank Mandarino 1997435c5e25SMark Brown return 0; 1998435c5e25SMark Brown } 1999435c5e25SMark Brown 2000b0e26485SVinod Koul static int soc_cleanup_card_resources(struct snd_soc_card *card) 2001db2a4165SFrank Mandarino { 2002db2a4165SFrank Mandarino int i; 2003db2a4165SFrank Mandarino 2004f0fba2adSLiam Girdwood /* make sure any delayed work runs */ 2005f0fba2adSLiam Girdwood for (i = 0; i < card->num_rtd; i++) { 2006f0fba2adSLiam Girdwood struct snd_soc_pcm_runtime *rtd = &card->rtd[i]; 20075b84ba26STejun Heo flush_delayed_work_sync(&rtd->delayed_work); 2008db2a4165SFrank Mandarino } 2009db2a4165SFrank Mandarino 20102eea392dSJarkko Nikula /* remove auxiliary devices */ 20112eea392dSJarkko Nikula for (i = 0; i < card->num_aux_devs; i++) 20122eea392dSJarkko Nikula soc_remove_aux_dev(card, i); 20132eea392dSJarkko Nikula 2014f0fba2adSLiam Girdwood /* remove and free each DAI */ 2015f0fba2adSLiam Girdwood for (i = 0; i < card->num_rtd; i++) 2016f0fba2adSLiam Girdwood soc_remove_dai_link(card, i); 2017f0fba2adSLiam Girdwood 2018a6052154SJarkko Nikula soc_cleanup_card_debugfs(card); 2019a6052154SJarkko Nikula 2020f0fba2adSLiam Girdwood /* remove the card */ 202187506549SMark Brown if (card->remove) 2022e7361ec4SMark Brown card->remove(card); 2023f0fba2adSLiam Girdwood 2024f0fba2adSLiam Girdwood kfree(card->rtd); 2025f0fba2adSLiam Girdwood snd_card_free(card->snd_card); 2026b0e26485SVinod Koul return 0; 2027b0e26485SVinod Koul 2028b2dfa62cSGuennadi Liakhovetski } 2029b0e26485SVinod Koul 2030b0e26485SVinod Koul /* removes a socdev */ 2031b0e26485SVinod Koul static int soc_remove(struct platform_device *pdev) 2032b0e26485SVinod Koul { 2033b0e26485SVinod Koul struct snd_soc_card *card = platform_get_drvdata(pdev); 2034b0e26485SVinod Koul 2035c5af3a2eSMark Brown snd_soc_unregister_card(card); 2036db2a4165SFrank Mandarino return 0; 2037db2a4165SFrank Mandarino } 2038db2a4165SFrank Mandarino 20396f8ab4acSMark Brown int snd_soc_poweroff(struct device *dev) 204051737470SMark Brown { 20416f8ab4acSMark Brown struct snd_soc_card *card = dev_get_drvdata(dev); 2042f0fba2adSLiam Girdwood int i; 204351737470SMark Brown 204451737470SMark Brown if (!card->instantiated) 2045416356fcSMark Brown return 0; 204651737470SMark Brown 204751737470SMark Brown /* Flush out pmdown_time work - we actually do want to run it 204851737470SMark Brown * now, we're shutting down so no imminent restart. */ 2049f0fba2adSLiam Girdwood for (i = 0; i < card->num_rtd; i++) { 2050f0fba2adSLiam Girdwood struct snd_soc_pcm_runtime *rtd = &card->rtd[i]; 20515b84ba26STejun Heo flush_delayed_work_sync(&rtd->delayed_work); 2052f0fba2adSLiam Girdwood } 205351737470SMark Brown 2054f0fba2adSLiam Girdwood snd_soc_dapm_shutdown(card); 2055416356fcSMark Brown 2056416356fcSMark Brown return 0; 205751737470SMark Brown } 20586f8ab4acSMark Brown EXPORT_SYMBOL_GPL(snd_soc_poweroff); 205951737470SMark Brown 20606f8ab4acSMark Brown const struct dev_pm_ops snd_soc_pm_ops = { 20616f8ab4acSMark Brown .suspend = snd_soc_suspend, 20626f8ab4acSMark Brown .resume = snd_soc_resume, 20636f8ab4acSMark Brown .poweroff = snd_soc_poweroff, 2064416356fcSMark Brown }; 2065416356fcSMark Brown 2066db2a4165SFrank Mandarino /* ASoC platform driver */ 2067db2a4165SFrank Mandarino static struct platform_driver soc_driver = { 2068db2a4165SFrank Mandarino .driver = { 2069db2a4165SFrank Mandarino .name = "soc-audio", 20708b45a209SKay Sievers .owner = THIS_MODULE, 20716f8ab4acSMark Brown .pm = &snd_soc_pm_ops, 2072db2a4165SFrank Mandarino }, 2073db2a4165SFrank Mandarino .probe = soc_probe, 2074db2a4165SFrank Mandarino .remove = soc_remove, 2075db2a4165SFrank Mandarino }; 2076db2a4165SFrank Mandarino 2077db2a4165SFrank Mandarino /* create a new pcm */ 2078f0fba2adSLiam Girdwood static int soc_new_pcm(struct snd_soc_pcm_runtime *rtd, int num) 2079db2a4165SFrank Mandarino { 2080f0fba2adSLiam Girdwood struct snd_soc_codec *codec = rtd->codec; 2081f0fba2adSLiam Girdwood struct snd_soc_platform *platform = rtd->platform; 2082f0fba2adSLiam Girdwood struct snd_soc_dai *codec_dai = rtd->codec_dai; 2083f0fba2adSLiam Girdwood struct snd_soc_dai *cpu_dai = rtd->cpu_dai; 2084db2a4165SFrank Mandarino struct snd_pcm *pcm; 2085db2a4165SFrank Mandarino char new_name[64]; 2086db2a4165SFrank Mandarino int ret = 0, playback = 0, capture = 0; 2087db2a4165SFrank Mandarino 2088db2a4165SFrank Mandarino /* check client and interface hw capabilities */ 208940ca1142SMark Brown snprintf(new_name, sizeof(new_name), "%s %s-%d", 2090f0fba2adSLiam Girdwood rtd->dai_link->stream_name, codec_dai->name, num); 2091db2a4165SFrank Mandarino 2092f0fba2adSLiam Girdwood if (codec_dai->driver->playback.channels_min) 2093db2a4165SFrank Mandarino playback = 1; 2094f0fba2adSLiam Girdwood if (codec_dai->driver->capture.channels_min) 2095db2a4165SFrank Mandarino capture = 1; 2096db2a4165SFrank Mandarino 2097f0fba2adSLiam Girdwood dev_dbg(rtd->card->dev, "registered pcm #%d %s\n",num,new_name); 2098f0fba2adSLiam Girdwood ret = snd_pcm_new(rtd->card->snd_card, new_name, 2099f0fba2adSLiam Girdwood num, playback, capture, &pcm); 2100db2a4165SFrank Mandarino if (ret < 0) { 2101f0fba2adSLiam Girdwood printk(KERN_ERR "asoc: can't create pcm for codec %s\n", codec->name); 2102db2a4165SFrank Mandarino return ret; 2103db2a4165SFrank Mandarino } 2104db2a4165SFrank Mandarino 2105f0fba2adSLiam Girdwood rtd->pcm = pcm; 2106db2a4165SFrank Mandarino pcm->private_data = rtd; 2107f0fba2adSLiam Girdwood soc_pcm_ops.mmap = platform->driver->ops->mmap; 2108f0fba2adSLiam Girdwood soc_pcm_ops.pointer = platform->driver->ops->pointer; 2109f0fba2adSLiam Girdwood soc_pcm_ops.ioctl = platform->driver->ops->ioctl; 2110f0fba2adSLiam Girdwood soc_pcm_ops.copy = platform->driver->ops->copy; 2111f0fba2adSLiam Girdwood soc_pcm_ops.silence = platform->driver->ops->silence; 2112f0fba2adSLiam Girdwood soc_pcm_ops.ack = platform->driver->ops->ack; 2113f0fba2adSLiam Girdwood soc_pcm_ops.page = platform->driver->ops->page; 2114db2a4165SFrank Mandarino 2115db2a4165SFrank Mandarino if (playback) 2116db2a4165SFrank Mandarino snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &soc_pcm_ops); 2117db2a4165SFrank Mandarino 2118db2a4165SFrank Mandarino if (capture) 2119db2a4165SFrank Mandarino snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &soc_pcm_ops); 2120db2a4165SFrank Mandarino 2121f0fba2adSLiam Girdwood ret = platform->driver->pcm_new(rtd->card->snd_card, codec_dai, pcm); 2122db2a4165SFrank Mandarino if (ret < 0) { 2123db2a4165SFrank Mandarino printk(KERN_ERR "asoc: platform pcm constructor failed\n"); 2124db2a4165SFrank Mandarino return ret; 2125db2a4165SFrank Mandarino } 2126db2a4165SFrank Mandarino 2127f0fba2adSLiam Girdwood pcm->private_free = platform->driver->pcm_free; 2128db2a4165SFrank Mandarino printk(KERN_INFO "asoc: %s <-> %s mapping ok\n", codec_dai->name, 2129db2a4165SFrank Mandarino cpu_dai->name); 2130db2a4165SFrank Mandarino return ret; 2131db2a4165SFrank Mandarino } 2132db2a4165SFrank Mandarino 2133096e49d5SMark Brown /** 2134096e49d5SMark Brown * snd_soc_codec_volatile_register: Report if a register is volatile. 2135096e49d5SMark Brown * 2136096e49d5SMark Brown * @codec: CODEC to query. 2137096e49d5SMark Brown * @reg: Register to query. 2138096e49d5SMark Brown * 2139096e49d5SMark Brown * Boolean function indiciating if a CODEC register is volatile. 2140096e49d5SMark Brown */ 2141181e055eSMark Brown int snd_soc_codec_volatile_register(struct snd_soc_codec *codec, 2142181e055eSMark Brown unsigned int reg) 2143096e49d5SMark Brown { 21441500b7b5SDimitris Papastamos if (codec->volatile_register) 21451500b7b5SDimitris Papastamos return codec->volatile_register(codec, reg); 2146096e49d5SMark Brown else 2147096e49d5SMark Brown return 0; 2148096e49d5SMark Brown } 2149096e49d5SMark Brown EXPORT_SYMBOL_GPL(snd_soc_codec_volatile_register); 2150096e49d5SMark Brown 2151db2a4165SFrank Mandarino /** 2152db2a4165SFrank Mandarino * snd_soc_new_ac97_codec - initailise AC97 device 2153db2a4165SFrank Mandarino * @codec: audio codec 2154db2a4165SFrank Mandarino * @ops: AC97 bus operations 2155db2a4165SFrank Mandarino * @num: AC97 codec number 2156db2a4165SFrank Mandarino * 2157db2a4165SFrank Mandarino * Initialises AC97 codec resources for use by ad-hoc devices only. 2158db2a4165SFrank Mandarino */ 2159db2a4165SFrank Mandarino int snd_soc_new_ac97_codec(struct snd_soc_codec *codec, 2160db2a4165SFrank Mandarino struct snd_ac97_bus_ops *ops, int num) 2161db2a4165SFrank Mandarino { 2162db2a4165SFrank Mandarino mutex_lock(&codec->mutex); 2163db2a4165SFrank Mandarino 2164db2a4165SFrank Mandarino codec->ac97 = kzalloc(sizeof(struct snd_ac97), GFP_KERNEL); 2165db2a4165SFrank Mandarino if (codec->ac97 == NULL) { 2166db2a4165SFrank Mandarino mutex_unlock(&codec->mutex); 2167db2a4165SFrank Mandarino return -ENOMEM; 2168db2a4165SFrank Mandarino } 2169db2a4165SFrank Mandarino 2170db2a4165SFrank Mandarino codec->ac97->bus = kzalloc(sizeof(struct snd_ac97_bus), GFP_KERNEL); 2171db2a4165SFrank Mandarino if (codec->ac97->bus == NULL) { 2172db2a4165SFrank Mandarino kfree(codec->ac97); 2173db2a4165SFrank Mandarino codec->ac97 = NULL; 2174db2a4165SFrank Mandarino mutex_unlock(&codec->mutex); 2175db2a4165SFrank Mandarino return -ENOMEM; 2176db2a4165SFrank Mandarino } 2177db2a4165SFrank Mandarino 2178db2a4165SFrank Mandarino codec->ac97->bus->ops = ops; 2179db2a4165SFrank Mandarino codec->ac97->num = num; 21800562f788SMika Westerberg 21810562f788SMika Westerberg /* 21820562f788SMika Westerberg * Mark the AC97 device to be created by us. This way we ensure that the 21830562f788SMika Westerberg * device will be registered with the device subsystem later on. 21840562f788SMika Westerberg */ 21850562f788SMika Westerberg codec->ac97_created = 1; 21860562f788SMika Westerberg 2187db2a4165SFrank Mandarino mutex_unlock(&codec->mutex); 2188db2a4165SFrank Mandarino return 0; 2189db2a4165SFrank Mandarino } 2190db2a4165SFrank Mandarino EXPORT_SYMBOL_GPL(snd_soc_new_ac97_codec); 2191db2a4165SFrank Mandarino 2192db2a4165SFrank Mandarino /** 2193db2a4165SFrank Mandarino * snd_soc_free_ac97_codec - free AC97 codec device 2194db2a4165SFrank Mandarino * @codec: audio codec 2195db2a4165SFrank Mandarino * 2196db2a4165SFrank Mandarino * Frees AC97 codec device resources. 2197db2a4165SFrank Mandarino */ 2198db2a4165SFrank Mandarino void snd_soc_free_ac97_codec(struct snd_soc_codec *codec) 2199db2a4165SFrank Mandarino { 2200db2a4165SFrank Mandarino mutex_lock(&codec->mutex); 2201f0fba2adSLiam Girdwood #ifdef CONFIG_SND_SOC_AC97_BUS 2202f0fba2adSLiam Girdwood soc_unregister_ac97_dai_link(codec); 2203f0fba2adSLiam Girdwood #endif 2204db2a4165SFrank Mandarino kfree(codec->ac97->bus); 2205db2a4165SFrank Mandarino kfree(codec->ac97); 2206db2a4165SFrank Mandarino codec->ac97 = NULL; 22070562f788SMika Westerberg codec->ac97_created = 0; 2208db2a4165SFrank Mandarino mutex_unlock(&codec->mutex); 2209db2a4165SFrank Mandarino } 2210db2a4165SFrank Mandarino EXPORT_SYMBOL_GPL(snd_soc_free_ac97_codec); 2211db2a4165SFrank Mandarino 2212c3753707SMark Brown unsigned int snd_soc_read(struct snd_soc_codec *codec, unsigned int reg) 2213c3753707SMark Brown { 2214c3753707SMark Brown unsigned int ret; 2215c3753707SMark Brown 2216c3acec26SMark Brown ret = codec->read(codec, reg); 2217c3753707SMark Brown dev_dbg(codec->dev, "read %x => %x\n", reg, ret); 2218a8b1d34fSMark Brown trace_snd_soc_reg_read(codec, reg, ret); 2219c3753707SMark Brown 2220c3753707SMark Brown return ret; 2221c3753707SMark Brown } 2222c3753707SMark Brown EXPORT_SYMBOL_GPL(snd_soc_read); 2223c3753707SMark Brown 2224c3753707SMark Brown unsigned int snd_soc_write(struct snd_soc_codec *codec, 2225c3753707SMark Brown unsigned int reg, unsigned int val) 2226c3753707SMark Brown { 2227c3753707SMark Brown dev_dbg(codec->dev, "write %x = %x\n", reg, val); 2228a8b1d34fSMark Brown trace_snd_soc_reg_write(codec, reg, val); 2229c3acec26SMark Brown return codec->write(codec, reg, val); 2230c3753707SMark Brown } 2231c3753707SMark Brown EXPORT_SYMBOL_GPL(snd_soc_write); 2232c3753707SMark Brown 2233db2a4165SFrank Mandarino /** 2234db2a4165SFrank Mandarino * snd_soc_update_bits - update codec register bits 2235db2a4165SFrank Mandarino * @codec: audio codec 2236db2a4165SFrank Mandarino * @reg: codec register 2237db2a4165SFrank Mandarino * @mask: register mask 2238db2a4165SFrank Mandarino * @value: new value 2239db2a4165SFrank Mandarino * 2240db2a4165SFrank Mandarino * Writes new register value. 2241db2a4165SFrank Mandarino * 2242180c329dSTimur Tabi * Returns 1 for change, 0 for no change, or negative error code. 2243db2a4165SFrank Mandarino */ 2244db2a4165SFrank Mandarino int snd_soc_update_bits(struct snd_soc_codec *codec, unsigned short reg, 224546f5822fSDaniel Ribeiro unsigned int mask, unsigned int value) 2246db2a4165SFrank Mandarino { 2247db2a4165SFrank Mandarino int change; 224846f5822fSDaniel Ribeiro unsigned int old, new; 2249180c329dSTimur Tabi int ret; 2250db2a4165SFrank Mandarino 2251180c329dSTimur Tabi ret = snd_soc_read(codec, reg); 2252180c329dSTimur Tabi if (ret < 0) 2253180c329dSTimur Tabi return ret; 2254180c329dSTimur Tabi 2255180c329dSTimur Tabi old = ret; 2256db2a4165SFrank Mandarino new = (old & ~mask) | value; 2257db2a4165SFrank Mandarino change = old != new; 2258180c329dSTimur Tabi if (change) { 2259180c329dSTimur Tabi ret = snd_soc_write(codec, reg, new); 2260180c329dSTimur Tabi if (ret < 0) 2261180c329dSTimur Tabi return ret; 2262180c329dSTimur Tabi } 2263db2a4165SFrank Mandarino 2264db2a4165SFrank Mandarino return change; 2265db2a4165SFrank Mandarino } 2266db2a4165SFrank Mandarino EXPORT_SYMBOL_GPL(snd_soc_update_bits); 2267db2a4165SFrank Mandarino 2268db2a4165SFrank Mandarino /** 22696c508c62SEero Nurkkala * snd_soc_update_bits_locked - update codec register bits 22706c508c62SEero Nurkkala * @codec: audio codec 22716c508c62SEero Nurkkala * @reg: codec register 22726c508c62SEero Nurkkala * @mask: register mask 22736c508c62SEero Nurkkala * @value: new value 22746c508c62SEero Nurkkala * 22756c508c62SEero Nurkkala * Writes new register value, and takes the codec mutex. 22766c508c62SEero Nurkkala * 22776c508c62SEero Nurkkala * Returns 1 for change else 0. 22786c508c62SEero Nurkkala */ 2279dd1b3d53SMark Brown int snd_soc_update_bits_locked(struct snd_soc_codec *codec, 22806c508c62SEero Nurkkala unsigned short reg, unsigned int mask, 22816c508c62SEero Nurkkala unsigned int value) 22826c508c62SEero Nurkkala { 22836c508c62SEero Nurkkala int change; 22846c508c62SEero Nurkkala 22856c508c62SEero Nurkkala mutex_lock(&codec->mutex); 22866c508c62SEero Nurkkala change = snd_soc_update_bits(codec, reg, mask, value); 22876c508c62SEero Nurkkala mutex_unlock(&codec->mutex); 22886c508c62SEero Nurkkala 22896c508c62SEero Nurkkala return change; 22906c508c62SEero Nurkkala } 2291dd1b3d53SMark Brown EXPORT_SYMBOL_GPL(snd_soc_update_bits_locked); 22926c508c62SEero Nurkkala 22936c508c62SEero Nurkkala /** 2294db2a4165SFrank Mandarino * snd_soc_test_bits - test register for change 2295db2a4165SFrank Mandarino * @codec: audio codec 2296db2a4165SFrank Mandarino * @reg: codec register 2297db2a4165SFrank Mandarino * @mask: register mask 2298db2a4165SFrank Mandarino * @value: new value 2299db2a4165SFrank Mandarino * 2300db2a4165SFrank Mandarino * Tests a register with a new value and checks if the new value is 2301db2a4165SFrank Mandarino * different from the old value. 2302db2a4165SFrank Mandarino * 2303db2a4165SFrank Mandarino * Returns 1 for change else 0. 2304db2a4165SFrank Mandarino */ 2305db2a4165SFrank Mandarino int snd_soc_test_bits(struct snd_soc_codec *codec, unsigned short reg, 230646f5822fSDaniel Ribeiro unsigned int mask, unsigned int value) 2307db2a4165SFrank Mandarino { 2308db2a4165SFrank Mandarino int change; 230946f5822fSDaniel Ribeiro unsigned int old, new; 2310db2a4165SFrank Mandarino 2311db2a4165SFrank Mandarino old = snd_soc_read(codec, reg); 2312db2a4165SFrank Mandarino new = (old & ~mask) | value; 2313db2a4165SFrank Mandarino change = old != new; 2314db2a4165SFrank Mandarino 2315db2a4165SFrank Mandarino return change; 2316db2a4165SFrank Mandarino } 2317db2a4165SFrank Mandarino EXPORT_SYMBOL_GPL(snd_soc_test_bits); 2318db2a4165SFrank Mandarino 2319db2a4165SFrank Mandarino /** 2320db2a4165SFrank Mandarino * snd_soc_set_runtime_hwparams - set the runtime hardware parameters 2321db2a4165SFrank Mandarino * @substream: the pcm substream 2322db2a4165SFrank Mandarino * @hw: the hardware parameters 2323db2a4165SFrank Mandarino * 2324db2a4165SFrank Mandarino * Sets the substream runtime hardware parameters. 2325db2a4165SFrank Mandarino */ 2326db2a4165SFrank Mandarino int snd_soc_set_runtime_hwparams(struct snd_pcm_substream *substream, 2327db2a4165SFrank Mandarino const struct snd_pcm_hardware *hw) 2328db2a4165SFrank Mandarino { 2329db2a4165SFrank Mandarino struct snd_pcm_runtime *runtime = substream->runtime; 2330db2a4165SFrank Mandarino runtime->hw.info = hw->info; 2331db2a4165SFrank Mandarino runtime->hw.formats = hw->formats; 2332db2a4165SFrank Mandarino runtime->hw.period_bytes_min = hw->period_bytes_min; 2333db2a4165SFrank Mandarino runtime->hw.period_bytes_max = hw->period_bytes_max; 2334db2a4165SFrank Mandarino runtime->hw.periods_min = hw->periods_min; 2335db2a4165SFrank Mandarino runtime->hw.periods_max = hw->periods_max; 2336db2a4165SFrank Mandarino runtime->hw.buffer_bytes_max = hw->buffer_bytes_max; 2337db2a4165SFrank Mandarino runtime->hw.fifo_size = hw->fifo_size; 2338db2a4165SFrank Mandarino return 0; 2339db2a4165SFrank Mandarino } 2340db2a4165SFrank Mandarino EXPORT_SYMBOL_GPL(snd_soc_set_runtime_hwparams); 2341db2a4165SFrank Mandarino 2342db2a4165SFrank Mandarino /** 2343db2a4165SFrank Mandarino * snd_soc_cnew - create new control 2344db2a4165SFrank Mandarino * @_template: control template 2345db2a4165SFrank Mandarino * @data: control private data 2346ac11a2b3SMark Brown * @long_name: control long name 2347*efb7ac3fSMark Brown * @prefix: control name prefix 2348db2a4165SFrank Mandarino * 2349db2a4165SFrank Mandarino * Create a new mixer control from a template control. 2350db2a4165SFrank Mandarino * 2351db2a4165SFrank Mandarino * Returns 0 for success, else error. 2352db2a4165SFrank Mandarino */ 2353db2a4165SFrank Mandarino struct snd_kcontrol *snd_soc_cnew(const struct snd_kcontrol_new *_template, 2354*efb7ac3fSMark Brown void *data, char *long_name, 2355*efb7ac3fSMark Brown const char *prefix) 2356db2a4165SFrank Mandarino { 2357db2a4165SFrank Mandarino struct snd_kcontrol_new template; 2358*efb7ac3fSMark Brown struct snd_kcontrol *kcontrol; 2359*efb7ac3fSMark Brown char *name = NULL; 2360*efb7ac3fSMark Brown int name_len; 2361db2a4165SFrank Mandarino 2362db2a4165SFrank Mandarino memcpy(&template, _template, sizeof(template)); 2363db2a4165SFrank Mandarino template.index = 0; 2364db2a4165SFrank Mandarino 2365*efb7ac3fSMark Brown if (!long_name) 2366*efb7ac3fSMark Brown long_name = template.name; 2367*efb7ac3fSMark Brown 2368*efb7ac3fSMark Brown if (prefix) { 2369*efb7ac3fSMark Brown name_len = strlen(long_name) + strlen(prefix) + 2; 2370*efb7ac3fSMark Brown name = kmalloc(name_len, GFP_ATOMIC); 2371*efb7ac3fSMark Brown if (!name) 2372*efb7ac3fSMark Brown return NULL; 2373*efb7ac3fSMark Brown 2374*efb7ac3fSMark Brown snprintf(name, name_len, "%s %s", prefix, long_name); 2375*efb7ac3fSMark Brown 2376*efb7ac3fSMark Brown template.name = name; 2377*efb7ac3fSMark Brown } else { 2378*efb7ac3fSMark Brown template.name = long_name; 2379*efb7ac3fSMark Brown } 2380*efb7ac3fSMark Brown 2381*efb7ac3fSMark Brown kcontrol = snd_ctl_new1(&template, data); 2382*efb7ac3fSMark Brown 2383*efb7ac3fSMark Brown kfree(name); 2384*efb7ac3fSMark Brown 2385*efb7ac3fSMark Brown return kcontrol; 2386db2a4165SFrank Mandarino } 2387db2a4165SFrank Mandarino EXPORT_SYMBOL_GPL(snd_soc_cnew); 2388db2a4165SFrank Mandarino 2389db2a4165SFrank Mandarino /** 23903e8e1952SIan Molton * snd_soc_add_controls - add an array of controls to a codec. 23913e8e1952SIan Molton * Convienience function to add a list of controls. Many codecs were 23923e8e1952SIan Molton * duplicating this code. 23933e8e1952SIan Molton * 23943e8e1952SIan Molton * @codec: codec to add controls to 23953e8e1952SIan Molton * @controls: array of controls to add 23963e8e1952SIan Molton * @num_controls: number of elements in the array 23973e8e1952SIan Molton * 23983e8e1952SIan Molton * Return 0 for success, else error. 23993e8e1952SIan Molton */ 24003e8e1952SIan Molton int snd_soc_add_controls(struct snd_soc_codec *codec, 24013e8e1952SIan Molton const struct snd_kcontrol_new *controls, int num_controls) 24023e8e1952SIan Molton { 2403f0fba2adSLiam Girdwood struct snd_card *card = codec->card->snd_card; 24043e8e1952SIan Molton int err, i; 24053e8e1952SIan Molton 24063e8e1952SIan Molton for (i = 0; i < num_controls; i++) { 24073e8e1952SIan Molton const struct snd_kcontrol_new *control = &controls[i]; 2408*efb7ac3fSMark Brown err = snd_ctl_add(card, snd_soc_cnew(control, codec, 2409*efb7ac3fSMark Brown control->name, 2410*efb7ac3fSMark Brown codec->name_prefix)); 24113e8e1952SIan Molton if (err < 0) { 2412082100dcSMark Brown dev_err(codec->dev, "%s: Failed to add %s: %d\n", 2413*efb7ac3fSMark Brown codec->name, control->name, err); 24143e8e1952SIan Molton return err; 24153e8e1952SIan Molton } 24163e8e1952SIan Molton } 24173e8e1952SIan Molton 24183e8e1952SIan Molton return 0; 24193e8e1952SIan Molton } 24203e8e1952SIan Molton EXPORT_SYMBOL_GPL(snd_soc_add_controls); 24213e8e1952SIan Molton 24223e8e1952SIan Molton /** 2423db2a4165SFrank Mandarino * snd_soc_info_enum_double - enumerated double mixer info callback 2424db2a4165SFrank Mandarino * @kcontrol: mixer control 2425db2a4165SFrank Mandarino * @uinfo: control element information 2426db2a4165SFrank Mandarino * 2427db2a4165SFrank Mandarino * Callback to provide information about a double enumerated 2428db2a4165SFrank Mandarino * mixer control. 2429db2a4165SFrank Mandarino * 2430db2a4165SFrank Mandarino * Returns 0 for success. 2431db2a4165SFrank Mandarino */ 2432db2a4165SFrank Mandarino int snd_soc_info_enum_double(struct snd_kcontrol *kcontrol, 2433db2a4165SFrank Mandarino struct snd_ctl_elem_info *uinfo) 2434db2a4165SFrank Mandarino { 2435db2a4165SFrank Mandarino struct soc_enum *e = (struct soc_enum *)kcontrol->private_value; 2436db2a4165SFrank Mandarino 2437db2a4165SFrank Mandarino uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED; 2438db2a4165SFrank Mandarino uinfo->count = e->shift_l == e->shift_r ? 1 : 2; 2439f8ba0b7bSJon Smirl uinfo->value.enumerated.items = e->max; 2440db2a4165SFrank Mandarino 2441f8ba0b7bSJon Smirl if (uinfo->value.enumerated.item > e->max - 1) 2442f8ba0b7bSJon Smirl uinfo->value.enumerated.item = e->max - 1; 2443db2a4165SFrank Mandarino strcpy(uinfo->value.enumerated.name, 2444db2a4165SFrank Mandarino e->texts[uinfo->value.enumerated.item]); 2445db2a4165SFrank Mandarino return 0; 2446db2a4165SFrank Mandarino } 2447db2a4165SFrank Mandarino EXPORT_SYMBOL_GPL(snd_soc_info_enum_double); 2448db2a4165SFrank Mandarino 2449db2a4165SFrank Mandarino /** 2450db2a4165SFrank Mandarino * snd_soc_get_enum_double - enumerated double mixer get callback 2451db2a4165SFrank Mandarino * @kcontrol: mixer control 2452ac11a2b3SMark Brown * @ucontrol: control element information 2453db2a4165SFrank Mandarino * 2454db2a4165SFrank Mandarino * Callback to get the value of a double enumerated mixer. 2455db2a4165SFrank Mandarino * 2456db2a4165SFrank Mandarino * Returns 0 for success. 2457db2a4165SFrank Mandarino */ 2458db2a4165SFrank Mandarino int snd_soc_get_enum_double(struct snd_kcontrol *kcontrol, 2459db2a4165SFrank Mandarino struct snd_ctl_elem_value *ucontrol) 2460db2a4165SFrank Mandarino { 2461db2a4165SFrank Mandarino struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol); 2462db2a4165SFrank Mandarino struct soc_enum *e = (struct soc_enum *)kcontrol->private_value; 246346f5822fSDaniel Ribeiro unsigned int val, bitmask; 2464db2a4165SFrank Mandarino 2465f8ba0b7bSJon Smirl for (bitmask = 1; bitmask < e->max; bitmask <<= 1) 2466db2a4165SFrank Mandarino ; 2467db2a4165SFrank Mandarino val = snd_soc_read(codec, e->reg); 24683ff3f64bSMark Brown ucontrol->value.enumerated.item[0] 24693ff3f64bSMark Brown = (val >> e->shift_l) & (bitmask - 1); 2470db2a4165SFrank Mandarino if (e->shift_l != e->shift_r) 2471db2a4165SFrank Mandarino ucontrol->value.enumerated.item[1] = 2472db2a4165SFrank Mandarino (val >> e->shift_r) & (bitmask - 1); 2473db2a4165SFrank Mandarino 2474db2a4165SFrank Mandarino return 0; 2475db2a4165SFrank Mandarino } 2476db2a4165SFrank Mandarino EXPORT_SYMBOL_GPL(snd_soc_get_enum_double); 2477db2a4165SFrank Mandarino 2478db2a4165SFrank Mandarino /** 2479db2a4165SFrank Mandarino * snd_soc_put_enum_double - enumerated double mixer put callback 2480db2a4165SFrank Mandarino * @kcontrol: mixer control 2481ac11a2b3SMark Brown * @ucontrol: control element information 2482db2a4165SFrank Mandarino * 2483db2a4165SFrank Mandarino * Callback to set the value of a double enumerated mixer. 2484db2a4165SFrank Mandarino * 2485db2a4165SFrank Mandarino * Returns 0 for success. 2486db2a4165SFrank Mandarino */ 2487db2a4165SFrank Mandarino int snd_soc_put_enum_double(struct snd_kcontrol *kcontrol, 2488db2a4165SFrank Mandarino struct snd_ctl_elem_value *ucontrol) 2489db2a4165SFrank Mandarino { 2490db2a4165SFrank Mandarino struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol); 2491db2a4165SFrank Mandarino struct soc_enum *e = (struct soc_enum *)kcontrol->private_value; 249246f5822fSDaniel Ribeiro unsigned int val; 249346f5822fSDaniel Ribeiro unsigned int mask, bitmask; 2494db2a4165SFrank Mandarino 2495f8ba0b7bSJon Smirl for (bitmask = 1; bitmask < e->max; bitmask <<= 1) 2496db2a4165SFrank Mandarino ; 2497f8ba0b7bSJon Smirl if (ucontrol->value.enumerated.item[0] > e->max - 1) 2498db2a4165SFrank Mandarino return -EINVAL; 2499db2a4165SFrank Mandarino val = ucontrol->value.enumerated.item[0] << e->shift_l; 2500db2a4165SFrank Mandarino mask = (bitmask - 1) << e->shift_l; 2501db2a4165SFrank Mandarino if (e->shift_l != e->shift_r) { 2502f8ba0b7bSJon Smirl if (ucontrol->value.enumerated.item[1] > e->max - 1) 2503db2a4165SFrank Mandarino return -EINVAL; 2504db2a4165SFrank Mandarino val |= ucontrol->value.enumerated.item[1] << e->shift_r; 2505db2a4165SFrank Mandarino mask |= (bitmask - 1) << e->shift_r; 2506db2a4165SFrank Mandarino } 2507db2a4165SFrank Mandarino 25086c508c62SEero Nurkkala return snd_soc_update_bits_locked(codec, e->reg, mask, val); 2509db2a4165SFrank Mandarino } 2510db2a4165SFrank Mandarino EXPORT_SYMBOL_GPL(snd_soc_put_enum_double); 2511db2a4165SFrank Mandarino 2512db2a4165SFrank Mandarino /** 25132e72f8e3SPeter Ujfalusi * snd_soc_get_value_enum_double - semi enumerated double mixer get callback 25142e72f8e3SPeter Ujfalusi * @kcontrol: mixer control 25152e72f8e3SPeter Ujfalusi * @ucontrol: control element information 25162e72f8e3SPeter Ujfalusi * 25172e72f8e3SPeter Ujfalusi * Callback to get the value of a double semi enumerated mixer. 25182e72f8e3SPeter Ujfalusi * 25192e72f8e3SPeter Ujfalusi * Semi enumerated mixer: the enumerated items are referred as values. Can be 25202e72f8e3SPeter Ujfalusi * used for handling bitfield coded enumeration for example. 25212e72f8e3SPeter Ujfalusi * 25222e72f8e3SPeter Ujfalusi * Returns 0 for success. 25232e72f8e3SPeter Ujfalusi */ 25242e72f8e3SPeter Ujfalusi int snd_soc_get_value_enum_double(struct snd_kcontrol *kcontrol, 25252e72f8e3SPeter Ujfalusi struct snd_ctl_elem_value *ucontrol) 25262e72f8e3SPeter Ujfalusi { 25272e72f8e3SPeter Ujfalusi struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol); 252874155556SPeter Ujfalusi struct soc_enum *e = (struct soc_enum *)kcontrol->private_value; 252946f5822fSDaniel Ribeiro unsigned int reg_val, val, mux; 25302e72f8e3SPeter Ujfalusi 25312e72f8e3SPeter Ujfalusi reg_val = snd_soc_read(codec, e->reg); 25322e72f8e3SPeter Ujfalusi val = (reg_val >> e->shift_l) & e->mask; 25332e72f8e3SPeter Ujfalusi for (mux = 0; mux < e->max; mux++) { 25342e72f8e3SPeter Ujfalusi if (val == e->values[mux]) 25352e72f8e3SPeter Ujfalusi break; 25362e72f8e3SPeter Ujfalusi } 25372e72f8e3SPeter Ujfalusi ucontrol->value.enumerated.item[0] = mux; 25382e72f8e3SPeter Ujfalusi if (e->shift_l != e->shift_r) { 25392e72f8e3SPeter Ujfalusi val = (reg_val >> e->shift_r) & e->mask; 25402e72f8e3SPeter Ujfalusi for (mux = 0; mux < e->max; mux++) { 25412e72f8e3SPeter Ujfalusi if (val == e->values[mux]) 25422e72f8e3SPeter Ujfalusi break; 25432e72f8e3SPeter Ujfalusi } 25442e72f8e3SPeter Ujfalusi ucontrol->value.enumerated.item[1] = mux; 25452e72f8e3SPeter Ujfalusi } 25462e72f8e3SPeter Ujfalusi 25472e72f8e3SPeter Ujfalusi return 0; 25482e72f8e3SPeter Ujfalusi } 25492e72f8e3SPeter Ujfalusi EXPORT_SYMBOL_GPL(snd_soc_get_value_enum_double); 25502e72f8e3SPeter Ujfalusi 25512e72f8e3SPeter Ujfalusi /** 25522e72f8e3SPeter Ujfalusi * snd_soc_put_value_enum_double - semi enumerated double mixer put callback 25532e72f8e3SPeter Ujfalusi * @kcontrol: mixer control 25542e72f8e3SPeter Ujfalusi * @ucontrol: control element information 25552e72f8e3SPeter Ujfalusi * 25562e72f8e3SPeter Ujfalusi * Callback to set the value of a double semi enumerated mixer. 25572e72f8e3SPeter Ujfalusi * 25582e72f8e3SPeter Ujfalusi * Semi enumerated mixer: the enumerated items are referred as values. Can be 25592e72f8e3SPeter Ujfalusi * used for handling bitfield coded enumeration for example. 25602e72f8e3SPeter Ujfalusi * 25612e72f8e3SPeter Ujfalusi * Returns 0 for success. 25622e72f8e3SPeter Ujfalusi */ 25632e72f8e3SPeter Ujfalusi int snd_soc_put_value_enum_double(struct snd_kcontrol *kcontrol, 25642e72f8e3SPeter Ujfalusi struct snd_ctl_elem_value *ucontrol) 25652e72f8e3SPeter Ujfalusi { 25662e72f8e3SPeter Ujfalusi struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol); 256774155556SPeter Ujfalusi struct soc_enum *e = (struct soc_enum *)kcontrol->private_value; 256846f5822fSDaniel Ribeiro unsigned int val; 256946f5822fSDaniel Ribeiro unsigned int mask; 25702e72f8e3SPeter Ujfalusi 25712e72f8e3SPeter Ujfalusi if (ucontrol->value.enumerated.item[0] > e->max - 1) 25722e72f8e3SPeter Ujfalusi return -EINVAL; 25732e72f8e3SPeter Ujfalusi val = e->values[ucontrol->value.enumerated.item[0]] << e->shift_l; 25742e72f8e3SPeter Ujfalusi mask = e->mask << e->shift_l; 25752e72f8e3SPeter Ujfalusi if (e->shift_l != e->shift_r) { 25762e72f8e3SPeter Ujfalusi if (ucontrol->value.enumerated.item[1] > e->max - 1) 25772e72f8e3SPeter Ujfalusi return -EINVAL; 25782e72f8e3SPeter Ujfalusi val |= e->values[ucontrol->value.enumerated.item[1]] << e->shift_r; 25792e72f8e3SPeter Ujfalusi mask |= e->mask << e->shift_r; 25802e72f8e3SPeter Ujfalusi } 25812e72f8e3SPeter Ujfalusi 25826c508c62SEero Nurkkala return snd_soc_update_bits_locked(codec, e->reg, mask, val); 25832e72f8e3SPeter Ujfalusi } 25842e72f8e3SPeter Ujfalusi EXPORT_SYMBOL_GPL(snd_soc_put_value_enum_double); 25852e72f8e3SPeter Ujfalusi 25862e72f8e3SPeter Ujfalusi /** 2587db2a4165SFrank Mandarino * snd_soc_info_enum_ext - external enumerated single mixer info callback 2588db2a4165SFrank Mandarino * @kcontrol: mixer control 2589db2a4165SFrank Mandarino * @uinfo: control element information 2590db2a4165SFrank Mandarino * 2591db2a4165SFrank Mandarino * Callback to provide information about an external enumerated 2592db2a4165SFrank Mandarino * single mixer. 2593db2a4165SFrank Mandarino * 2594db2a4165SFrank Mandarino * Returns 0 for success. 2595db2a4165SFrank Mandarino */ 2596db2a4165SFrank Mandarino int snd_soc_info_enum_ext(struct snd_kcontrol *kcontrol, 2597db2a4165SFrank Mandarino struct snd_ctl_elem_info *uinfo) 2598db2a4165SFrank Mandarino { 2599db2a4165SFrank Mandarino struct soc_enum *e = (struct soc_enum *)kcontrol->private_value; 2600db2a4165SFrank Mandarino 2601db2a4165SFrank Mandarino uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED; 2602db2a4165SFrank Mandarino uinfo->count = 1; 2603f8ba0b7bSJon Smirl uinfo->value.enumerated.items = e->max; 2604db2a4165SFrank Mandarino 2605f8ba0b7bSJon Smirl if (uinfo->value.enumerated.item > e->max - 1) 2606f8ba0b7bSJon Smirl uinfo->value.enumerated.item = e->max - 1; 2607db2a4165SFrank Mandarino strcpy(uinfo->value.enumerated.name, 2608db2a4165SFrank Mandarino e->texts[uinfo->value.enumerated.item]); 2609db2a4165SFrank Mandarino return 0; 2610db2a4165SFrank Mandarino } 2611db2a4165SFrank Mandarino EXPORT_SYMBOL_GPL(snd_soc_info_enum_ext); 2612db2a4165SFrank Mandarino 2613db2a4165SFrank Mandarino /** 2614db2a4165SFrank Mandarino * snd_soc_info_volsw_ext - external single mixer info callback 2615db2a4165SFrank Mandarino * @kcontrol: mixer control 2616db2a4165SFrank Mandarino * @uinfo: control element information 2617db2a4165SFrank Mandarino * 2618db2a4165SFrank Mandarino * Callback to provide information about a single external mixer control. 2619db2a4165SFrank Mandarino * 2620db2a4165SFrank Mandarino * Returns 0 for success. 2621db2a4165SFrank Mandarino */ 2622db2a4165SFrank Mandarino int snd_soc_info_volsw_ext(struct snd_kcontrol *kcontrol, 2623db2a4165SFrank Mandarino struct snd_ctl_elem_info *uinfo) 2624db2a4165SFrank Mandarino { 2625a7a4ac86SPhilipp Zabel int max = kcontrol->private_value; 2626db2a4165SFrank Mandarino 2627fd5dfad9SMark Brown if (max == 1 && !strstr(kcontrol->id.name, " Volume")) 2628a7a4ac86SPhilipp Zabel uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN; 2629a7a4ac86SPhilipp Zabel else 2630a7a4ac86SPhilipp Zabel uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER; 2631a7a4ac86SPhilipp Zabel 2632db2a4165SFrank Mandarino uinfo->count = 1; 2633db2a4165SFrank Mandarino uinfo->value.integer.min = 0; 2634a7a4ac86SPhilipp Zabel uinfo->value.integer.max = max; 2635db2a4165SFrank Mandarino return 0; 2636db2a4165SFrank Mandarino } 2637db2a4165SFrank Mandarino EXPORT_SYMBOL_GPL(snd_soc_info_volsw_ext); 2638db2a4165SFrank Mandarino 2639db2a4165SFrank Mandarino /** 2640db2a4165SFrank Mandarino * snd_soc_info_volsw - single mixer info callback 2641db2a4165SFrank Mandarino * @kcontrol: mixer control 2642db2a4165SFrank Mandarino * @uinfo: control element information 2643db2a4165SFrank Mandarino * 2644db2a4165SFrank Mandarino * Callback to provide information about a single mixer control. 2645db2a4165SFrank Mandarino * 2646db2a4165SFrank Mandarino * Returns 0 for success. 2647db2a4165SFrank Mandarino */ 2648db2a4165SFrank Mandarino int snd_soc_info_volsw(struct snd_kcontrol *kcontrol, 2649db2a4165SFrank Mandarino struct snd_ctl_elem_info *uinfo) 2650db2a4165SFrank Mandarino { 26514eaa9819SJon Smirl struct soc_mixer_control *mc = 26524eaa9819SJon Smirl (struct soc_mixer_control *)kcontrol->private_value; 2653d11bb4a9SPeter Ujfalusi int platform_max; 2654762b8df7SMark Brown unsigned int shift = mc->shift; 2655815ecf8dSJon Smirl unsigned int rshift = mc->rshift; 2656db2a4165SFrank Mandarino 2657d11bb4a9SPeter Ujfalusi if (!mc->platform_max) 2658d11bb4a9SPeter Ujfalusi mc->platform_max = mc->max; 2659d11bb4a9SPeter Ujfalusi platform_max = mc->platform_max; 2660d11bb4a9SPeter Ujfalusi 2661d11bb4a9SPeter Ujfalusi if (platform_max == 1 && !strstr(kcontrol->id.name, " Volume")) 2662a7a4ac86SPhilipp Zabel uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN; 2663a7a4ac86SPhilipp Zabel else 2664a7a4ac86SPhilipp Zabel uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER; 2665a7a4ac86SPhilipp Zabel 2666db2a4165SFrank Mandarino uinfo->count = shift == rshift ? 1 : 2; 2667db2a4165SFrank Mandarino uinfo->value.integer.min = 0; 2668d11bb4a9SPeter Ujfalusi uinfo->value.integer.max = platform_max; 2669db2a4165SFrank Mandarino return 0; 2670db2a4165SFrank Mandarino } 2671db2a4165SFrank Mandarino EXPORT_SYMBOL_GPL(snd_soc_info_volsw); 2672db2a4165SFrank Mandarino 2673db2a4165SFrank Mandarino /** 2674db2a4165SFrank Mandarino * snd_soc_get_volsw - single mixer get callback 2675db2a4165SFrank Mandarino * @kcontrol: mixer control 2676ac11a2b3SMark Brown * @ucontrol: control element information 2677db2a4165SFrank Mandarino * 2678db2a4165SFrank Mandarino * Callback to get the value of a single mixer control. 2679db2a4165SFrank Mandarino * 2680db2a4165SFrank Mandarino * Returns 0 for success. 2681db2a4165SFrank Mandarino */ 2682db2a4165SFrank Mandarino int snd_soc_get_volsw(struct snd_kcontrol *kcontrol, 2683db2a4165SFrank Mandarino struct snd_ctl_elem_value *ucontrol) 2684db2a4165SFrank Mandarino { 26854eaa9819SJon Smirl struct soc_mixer_control *mc = 26864eaa9819SJon Smirl (struct soc_mixer_control *)kcontrol->private_value; 2687db2a4165SFrank Mandarino struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol); 2688815ecf8dSJon Smirl unsigned int reg = mc->reg; 2689815ecf8dSJon Smirl unsigned int shift = mc->shift; 2690815ecf8dSJon Smirl unsigned int rshift = mc->rshift; 26914eaa9819SJon Smirl int max = mc->max; 2692815ecf8dSJon Smirl unsigned int mask = (1 << fls(max)) - 1; 2693815ecf8dSJon Smirl unsigned int invert = mc->invert; 2694db2a4165SFrank Mandarino 2695db2a4165SFrank Mandarino ucontrol->value.integer.value[0] = 2696db2a4165SFrank Mandarino (snd_soc_read(codec, reg) >> shift) & mask; 2697db2a4165SFrank Mandarino if (shift != rshift) 2698db2a4165SFrank Mandarino ucontrol->value.integer.value[1] = 2699db2a4165SFrank Mandarino (snd_soc_read(codec, reg) >> rshift) & mask; 2700db2a4165SFrank Mandarino if (invert) { 2701db2a4165SFrank Mandarino ucontrol->value.integer.value[0] = 2702a7a4ac86SPhilipp Zabel max - ucontrol->value.integer.value[0]; 2703db2a4165SFrank Mandarino if (shift != rshift) 2704db2a4165SFrank Mandarino ucontrol->value.integer.value[1] = 2705a7a4ac86SPhilipp Zabel max - ucontrol->value.integer.value[1]; 2706db2a4165SFrank Mandarino } 2707db2a4165SFrank Mandarino 2708db2a4165SFrank Mandarino return 0; 2709db2a4165SFrank Mandarino } 2710db2a4165SFrank Mandarino EXPORT_SYMBOL_GPL(snd_soc_get_volsw); 2711db2a4165SFrank Mandarino 2712db2a4165SFrank Mandarino /** 2713db2a4165SFrank Mandarino * snd_soc_put_volsw - single mixer put callback 2714db2a4165SFrank Mandarino * @kcontrol: mixer control 2715ac11a2b3SMark Brown * @ucontrol: control element information 2716db2a4165SFrank Mandarino * 2717db2a4165SFrank Mandarino * Callback to set the value of a single mixer control. 2718db2a4165SFrank Mandarino * 2719db2a4165SFrank Mandarino * Returns 0 for success. 2720db2a4165SFrank Mandarino */ 2721db2a4165SFrank Mandarino int snd_soc_put_volsw(struct snd_kcontrol *kcontrol, 2722db2a4165SFrank Mandarino struct snd_ctl_elem_value *ucontrol) 2723db2a4165SFrank Mandarino { 27244eaa9819SJon Smirl struct soc_mixer_control *mc = 27254eaa9819SJon Smirl (struct soc_mixer_control *)kcontrol->private_value; 2726db2a4165SFrank Mandarino struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol); 2727815ecf8dSJon Smirl unsigned int reg = mc->reg; 2728815ecf8dSJon Smirl unsigned int shift = mc->shift; 2729815ecf8dSJon Smirl unsigned int rshift = mc->rshift; 27304eaa9819SJon Smirl int max = mc->max; 2731815ecf8dSJon Smirl unsigned int mask = (1 << fls(max)) - 1; 2732815ecf8dSJon Smirl unsigned int invert = mc->invert; 273346f5822fSDaniel Ribeiro unsigned int val, val2, val_mask; 2734db2a4165SFrank Mandarino 2735db2a4165SFrank Mandarino val = (ucontrol->value.integer.value[0] & mask); 2736db2a4165SFrank Mandarino if (invert) 2737a7a4ac86SPhilipp Zabel val = max - val; 2738db2a4165SFrank Mandarino val_mask = mask << shift; 2739db2a4165SFrank Mandarino val = val << shift; 2740db2a4165SFrank Mandarino if (shift != rshift) { 2741db2a4165SFrank Mandarino val2 = (ucontrol->value.integer.value[1] & mask); 2742db2a4165SFrank Mandarino if (invert) 2743a7a4ac86SPhilipp Zabel val2 = max - val2; 2744db2a4165SFrank Mandarino val_mask |= mask << rshift; 2745db2a4165SFrank Mandarino val |= val2 << rshift; 2746db2a4165SFrank Mandarino } 27476c508c62SEero Nurkkala return snd_soc_update_bits_locked(codec, reg, val_mask, val); 2748db2a4165SFrank Mandarino } 2749db2a4165SFrank Mandarino EXPORT_SYMBOL_GPL(snd_soc_put_volsw); 2750db2a4165SFrank Mandarino 2751db2a4165SFrank Mandarino /** 2752db2a4165SFrank Mandarino * snd_soc_info_volsw_2r - double mixer info callback 2753db2a4165SFrank Mandarino * @kcontrol: mixer control 2754db2a4165SFrank Mandarino * @uinfo: control element information 2755db2a4165SFrank Mandarino * 2756db2a4165SFrank Mandarino * Callback to provide information about a double mixer control that 2757db2a4165SFrank Mandarino * spans 2 codec registers. 2758db2a4165SFrank Mandarino * 2759db2a4165SFrank Mandarino * Returns 0 for success. 2760db2a4165SFrank Mandarino */ 2761db2a4165SFrank Mandarino int snd_soc_info_volsw_2r(struct snd_kcontrol *kcontrol, 2762db2a4165SFrank Mandarino struct snd_ctl_elem_info *uinfo) 2763db2a4165SFrank Mandarino { 27644eaa9819SJon Smirl struct soc_mixer_control *mc = 27654eaa9819SJon Smirl (struct soc_mixer_control *)kcontrol->private_value; 2766d11bb4a9SPeter Ujfalusi int platform_max; 2767db2a4165SFrank Mandarino 2768d11bb4a9SPeter Ujfalusi if (!mc->platform_max) 2769d11bb4a9SPeter Ujfalusi mc->platform_max = mc->max; 2770d11bb4a9SPeter Ujfalusi platform_max = mc->platform_max; 2771d11bb4a9SPeter Ujfalusi 2772d11bb4a9SPeter Ujfalusi if (platform_max == 1 && !strstr(kcontrol->id.name, " Volume")) 2773a7a4ac86SPhilipp Zabel uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN; 2774a7a4ac86SPhilipp Zabel else 2775a7a4ac86SPhilipp Zabel uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER; 2776a7a4ac86SPhilipp Zabel 2777db2a4165SFrank Mandarino uinfo->count = 2; 2778db2a4165SFrank Mandarino uinfo->value.integer.min = 0; 2779d11bb4a9SPeter Ujfalusi uinfo->value.integer.max = platform_max; 2780db2a4165SFrank Mandarino return 0; 2781db2a4165SFrank Mandarino } 2782db2a4165SFrank Mandarino EXPORT_SYMBOL_GPL(snd_soc_info_volsw_2r); 2783db2a4165SFrank Mandarino 2784db2a4165SFrank Mandarino /** 2785db2a4165SFrank Mandarino * snd_soc_get_volsw_2r - double mixer get callback 2786db2a4165SFrank Mandarino * @kcontrol: mixer control 2787ac11a2b3SMark Brown * @ucontrol: control element information 2788db2a4165SFrank Mandarino * 2789db2a4165SFrank Mandarino * Callback to get the value of a double mixer control that spans 2 registers. 2790db2a4165SFrank Mandarino * 2791db2a4165SFrank Mandarino * Returns 0 for success. 2792db2a4165SFrank Mandarino */ 2793db2a4165SFrank Mandarino int snd_soc_get_volsw_2r(struct snd_kcontrol *kcontrol, 2794db2a4165SFrank Mandarino struct snd_ctl_elem_value *ucontrol) 2795db2a4165SFrank Mandarino { 27964eaa9819SJon Smirl struct soc_mixer_control *mc = 27974eaa9819SJon Smirl (struct soc_mixer_control *)kcontrol->private_value; 2798db2a4165SFrank Mandarino struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol); 2799815ecf8dSJon Smirl unsigned int reg = mc->reg; 2800815ecf8dSJon Smirl unsigned int reg2 = mc->rreg; 2801815ecf8dSJon Smirl unsigned int shift = mc->shift; 28024eaa9819SJon Smirl int max = mc->max; 2803815ecf8dSJon Smirl unsigned int mask = (1 << fls(max)) - 1; 2804815ecf8dSJon Smirl unsigned int invert = mc->invert; 2805db2a4165SFrank Mandarino 2806db2a4165SFrank Mandarino ucontrol->value.integer.value[0] = 2807db2a4165SFrank Mandarino (snd_soc_read(codec, reg) >> shift) & mask; 2808db2a4165SFrank Mandarino ucontrol->value.integer.value[1] = 2809db2a4165SFrank Mandarino (snd_soc_read(codec, reg2) >> shift) & mask; 2810db2a4165SFrank Mandarino if (invert) { 2811db2a4165SFrank Mandarino ucontrol->value.integer.value[0] = 2812a7a4ac86SPhilipp Zabel max - ucontrol->value.integer.value[0]; 2813db2a4165SFrank Mandarino ucontrol->value.integer.value[1] = 2814a7a4ac86SPhilipp Zabel max - ucontrol->value.integer.value[1]; 2815db2a4165SFrank Mandarino } 2816db2a4165SFrank Mandarino 2817db2a4165SFrank Mandarino return 0; 2818db2a4165SFrank Mandarino } 2819db2a4165SFrank Mandarino EXPORT_SYMBOL_GPL(snd_soc_get_volsw_2r); 2820db2a4165SFrank Mandarino 2821db2a4165SFrank Mandarino /** 2822db2a4165SFrank Mandarino * snd_soc_put_volsw_2r - double mixer set callback 2823db2a4165SFrank Mandarino * @kcontrol: mixer control 2824ac11a2b3SMark Brown * @ucontrol: control element information 2825db2a4165SFrank Mandarino * 2826db2a4165SFrank Mandarino * Callback to set the value of a double mixer control that spans 2 registers. 2827db2a4165SFrank Mandarino * 2828db2a4165SFrank Mandarino * Returns 0 for success. 2829db2a4165SFrank Mandarino */ 2830db2a4165SFrank Mandarino int snd_soc_put_volsw_2r(struct snd_kcontrol *kcontrol, 2831db2a4165SFrank Mandarino struct snd_ctl_elem_value *ucontrol) 2832db2a4165SFrank Mandarino { 28334eaa9819SJon Smirl struct soc_mixer_control *mc = 28344eaa9819SJon Smirl (struct soc_mixer_control *)kcontrol->private_value; 2835db2a4165SFrank Mandarino struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol); 2836815ecf8dSJon Smirl unsigned int reg = mc->reg; 2837815ecf8dSJon Smirl unsigned int reg2 = mc->rreg; 2838815ecf8dSJon Smirl unsigned int shift = mc->shift; 28394eaa9819SJon Smirl int max = mc->max; 2840815ecf8dSJon Smirl unsigned int mask = (1 << fls(max)) - 1; 2841815ecf8dSJon Smirl unsigned int invert = mc->invert; 2842db2a4165SFrank Mandarino int err; 284346f5822fSDaniel Ribeiro unsigned int val, val2, val_mask; 2844db2a4165SFrank Mandarino 2845db2a4165SFrank Mandarino val_mask = mask << shift; 2846db2a4165SFrank Mandarino val = (ucontrol->value.integer.value[0] & mask); 2847db2a4165SFrank Mandarino val2 = (ucontrol->value.integer.value[1] & mask); 2848db2a4165SFrank Mandarino 2849db2a4165SFrank Mandarino if (invert) { 2850a7a4ac86SPhilipp Zabel val = max - val; 2851a7a4ac86SPhilipp Zabel val2 = max - val2; 2852db2a4165SFrank Mandarino } 2853db2a4165SFrank Mandarino 2854db2a4165SFrank Mandarino val = val << shift; 2855db2a4165SFrank Mandarino val2 = val2 << shift; 2856db2a4165SFrank Mandarino 28576c508c62SEero Nurkkala err = snd_soc_update_bits_locked(codec, reg, val_mask, val); 28583ff3f64bSMark Brown if (err < 0) 2859db2a4165SFrank Mandarino return err; 2860db2a4165SFrank Mandarino 28616c508c62SEero Nurkkala err = snd_soc_update_bits_locked(codec, reg2, val_mask, val2); 2862db2a4165SFrank Mandarino return err; 2863db2a4165SFrank Mandarino } 2864db2a4165SFrank Mandarino EXPORT_SYMBOL_GPL(snd_soc_put_volsw_2r); 2865db2a4165SFrank Mandarino 2866e13ac2e9SMark Brown /** 2867e13ac2e9SMark Brown * snd_soc_info_volsw_s8 - signed mixer info callback 2868e13ac2e9SMark Brown * @kcontrol: mixer control 2869e13ac2e9SMark Brown * @uinfo: control element information 2870e13ac2e9SMark Brown * 2871e13ac2e9SMark Brown * Callback to provide information about a signed mixer control. 2872e13ac2e9SMark Brown * 2873e13ac2e9SMark Brown * Returns 0 for success. 2874e13ac2e9SMark Brown */ 2875e13ac2e9SMark Brown int snd_soc_info_volsw_s8(struct snd_kcontrol *kcontrol, 2876e13ac2e9SMark Brown struct snd_ctl_elem_info *uinfo) 2877e13ac2e9SMark Brown { 28784eaa9819SJon Smirl struct soc_mixer_control *mc = 28794eaa9819SJon Smirl (struct soc_mixer_control *)kcontrol->private_value; 2880d11bb4a9SPeter Ujfalusi int platform_max; 28814eaa9819SJon Smirl int min = mc->min; 2882e13ac2e9SMark Brown 2883d11bb4a9SPeter Ujfalusi if (!mc->platform_max) 2884d11bb4a9SPeter Ujfalusi mc->platform_max = mc->max; 2885d11bb4a9SPeter Ujfalusi platform_max = mc->platform_max; 2886d11bb4a9SPeter Ujfalusi 2887e13ac2e9SMark Brown uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER; 2888e13ac2e9SMark Brown uinfo->count = 2; 2889e13ac2e9SMark Brown uinfo->value.integer.min = 0; 2890d11bb4a9SPeter Ujfalusi uinfo->value.integer.max = platform_max - min; 2891e13ac2e9SMark Brown return 0; 2892e13ac2e9SMark Brown } 2893e13ac2e9SMark Brown EXPORT_SYMBOL_GPL(snd_soc_info_volsw_s8); 2894e13ac2e9SMark Brown 2895e13ac2e9SMark Brown /** 2896e13ac2e9SMark Brown * snd_soc_get_volsw_s8 - signed mixer get callback 2897e13ac2e9SMark Brown * @kcontrol: mixer control 2898ac11a2b3SMark Brown * @ucontrol: control element information 2899e13ac2e9SMark Brown * 2900e13ac2e9SMark Brown * Callback to get the value of a signed mixer control. 2901e13ac2e9SMark Brown * 2902e13ac2e9SMark Brown * Returns 0 for success. 2903e13ac2e9SMark Brown */ 2904e13ac2e9SMark Brown int snd_soc_get_volsw_s8(struct snd_kcontrol *kcontrol, 2905e13ac2e9SMark Brown struct snd_ctl_elem_value *ucontrol) 2906e13ac2e9SMark Brown { 29074eaa9819SJon Smirl struct soc_mixer_control *mc = 29084eaa9819SJon Smirl (struct soc_mixer_control *)kcontrol->private_value; 2909e13ac2e9SMark Brown struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol); 2910815ecf8dSJon Smirl unsigned int reg = mc->reg; 29114eaa9819SJon Smirl int min = mc->min; 2912e13ac2e9SMark Brown int val = snd_soc_read(codec, reg); 2913e13ac2e9SMark Brown 2914e13ac2e9SMark Brown ucontrol->value.integer.value[0] = 2915e13ac2e9SMark Brown ((signed char)(val & 0xff))-min; 2916e13ac2e9SMark Brown ucontrol->value.integer.value[1] = 2917e13ac2e9SMark Brown ((signed char)((val >> 8) & 0xff))-min; 2918e13ac2e9SMark Brown return 0; 2919e13ac2e9SMark Brown } 2920e13ac2e9SMark Brown EXPORT_SYMBOL_GPL(snd_soc_get_volsw_s8); 2921e13ac2e9SMark Brown 2922e13ac2e9SMark Brown /** 2923e13ac2e9SMark Brown * snd_soc_put_volsw_sgn - signed mixer put callback 2924e13ac2e9SMark Brown * @kcontrol: mixer control 2925ac11a2b3SMark Brown * @ucontrol: control element information 2926e13ac2e9SMark Brown * 2927e13ac2e9SMark Brown * Callback to set the value of a signed mixer control. 2928e13ac2e9SMark Brown * 2929e13ac2e9SMark Brown * Returns 0 for success. 2930e13ac2e9SMark Brown */ 2931e13ac2e9SMark Brown int snd_soc_put_volsw_s8(struct snd_kcontrol *kcontrol, 2932e13ac2e9SMark Brown struct snd_ctl_elem_value *ucontrol) 2933e13ac2e9SMark Brown { 29344eaa9819SJon Smirl struct soc_mixer_control *mc = 29354eaa9819SJon Smirl (struct soc_mixer_control *)kcontrol->private_value; 2936e13ac2e9SMark Brown struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol); 2937815ecf8dSJon Smirl unsigned int reg = mc->reg; 29384eaa9819SJon Smirl int min = mc->min; 293946f5822fSDaniel Ribeiro unsigned int val; 2940e13ac2e9SMark Brown 2941e13ac2e9SMark Brown val = (ucontrol->value.integer.value[0]+min) & 0xff; 2942e13ac2e9SMark Brown val |= ((ucontrol->value.integer.value[1]+min) & 0xff) << 8; 2943e13ac2e9SMark Brown 29446c508c62SEero Nurkkala return snd_soc_update_bits_locked(codec, reg, 0xffff, val); 2945e13ac2e9SMark Brown } 2946e13ac2e9SMark Brown EXPORT_SYMBOL_GPL(snd_soc_put_volsw_s8); 2947e13ac2e9SMark Brown 29488c6529dbSLiam Girdwood /** 2949637d3847SPeter Ujfalusi * snd_soc_limit_volume - Set new limit to an existing volume control. 2950637d3847SPeter Ujfalusi * 2951637d3847SPeter Ujfalusi * @codec: where to look for the control 2952637d3847SPeter Ujfalusi * @name: Name of the control 2953637d3847SPeter Ujfalusi * @max: new maximum limit 2954637d3847SPeter Ujfalusi * 2955637d3847SPeter Ujfalusi * Return 0 for success, else error. 2956637d3847SPeter Ujfalusi */ 2957637d3847SPeter Ujfalusi int snd_soc_limit_volume(struct snd_soc_codec *codec, 2958637d3847SPeter Ujfalusi const char *name, int max) 2959637d3847SPeter Ujfalusi { 2960f0fba2adSLiam Girdwood struct snd_card *card = codec->card->snd_card; 2961637d3847SPeter Ujfalusi struct snd_kcontrol *kctl; 2962637d3847SPeter Ujfalusi struct soc_mixer_control *mc; 2963637d3847SPeter Ujfalusi int found = 0; 2964637d3847SPeter Ujfalusi int ret = -EINVAL; 2965637d3847SPeter Ujfalusi 2966637d3847SPeter Ujfalusi /* Sanity check for name and max */ 2967637d3847SPeter Ujfalusi if (unlikely(!name || max <= 0)) 2968637d3847SPeter Ujfalusi return -EINVAL; 2969637d3847SPeter Ujfalusi 2970637d3847SPeter Ujfalusi list_for_each_entry(kctl, &card->controls, list) { 2971637d3847SPeter Ujfalusi if (!strncmp(kctl->id.name, name, sizeof(kctl->id.name))) { 2972637d3847SPeter Ujfalusi found = 1; 2973637d3847SPeter Ujfalusi break; 2974637d3847SPeter Ujfalusi } 2975637d3847SPeter Ujfalusi } 2976637d3847SPeter Ujfalusi if (found) { 2977637d3847SPeter Ujfalusi mc = (struct soc_mixer_control *)kctl->private_value; 2978637d3847SPeter Ujfalusi if (max <= mc->max) { 2979d11bb4a9SPeter Ujfalusi mc->platform_max = max; 2980637d3847SPeter Ujfalusi ret = 0; 2981637d3847SPeter Ujfalusi } 2982637d3847SPeter Ujfalusi } 2983637d3847SPeter Ujfalusi return ret; 2984637d3847SPeter Ujfalusi } 2985637d3847SPeter Ujfalusi EXPORT_SYMBOL_GPL(snd_soc_limit_volume); 2986637d3847SPeter Ujfalusi 2987637d3847SPeter Ujfalusi /** 2988b6f4bb38Sapatard@mandriva.com * snd_soc_info_volsw_2r_sx - double with tlv and variable data size 2989b6f4bb38Sapatard@mandriva.com * mixer info callback 2990b6f4bb38Sapatard@mandriva.com * @kcontrol: mixer control 2991b6f4bb38Sapatard@mandriva.com * @uinfo: control element information 2992b6f4bb38Sapatard@mandriva.com * 2993b6f4bb38Sapatard@mandriva.com * Returns 0 for success. 2994b6f4bb38Sapatard@mandriva.com */ 2995b6f4bb38Sapatard@mandriva.com int snd_soc_info_volsw_2r_sx(struct snd_kcontrol *kcontrol, 2996b6f4bb38Sapatard@mandriva.com struct snd_ctl_elem_info *uinfo) 2997b6f4bb38Sapatard@mandriva.com { 2998b6f4bb38Sapatard@mandriva.com struct soc_mixer_control *mc = 2999b6f4bb38Sapatard@mandriva.com (struct soc_mixer_control *)kcontrol->private_value; 3000b6f4bb38Sapatard@mandriva.com int max = mc->max; 3001b6f4bb38Sapatard@mandriva.com int min = mc->min; 3002b6f4bb38Sapatard@mandriva.com 3003b6f4bb38Sapatard@mandriva.com uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER; 3004b6f4bb38Sapatard@mandriva.com uinfo->count = 2; 3005b6f4bb38Sapatard@mandriva.com uinfo->value.integer.min = 0; 3006b6f4bb38Sapatard@mandriva.com uinfo->value.integer.max = max-min; 3007b6f4bb38Sapatard@mandriva.com 3008b6f4bb38Sapatard@mandriva.com return 0; 3009b6f4bb38Sapatard@mandriva.com } 3010b6f4bb38Sapatard@mandriva.com EXPORT_SYMBOL_GPL(snd_soc_info_volsw_2r_sx); 3011b6f4bb38Sapatard@mandriva.com 3012b6f4bb38Sapatard@mandriva.com /** 3013b6f4bb38Sapatard@mandriva.com * snd_soc_get_volsw_2r_sx - double with tlv and variable data size 3014b6f4bb38Sapatard@mandriva.com * mixer get callback 3015b6f4bb38Sapatard@mandriva.com * @kcontrol: mixer control 3016b6f4bb38Sapatard@mandriva.com * @uinfo: control element information 3017b6f4bb38Sapatard@mandriva.com * 3018b6f4bb38Sapatard@mandriva.com * Returns 0 for success. 3019b6f4bb38Sapatard@mandriva.com */ 3020b6f4bb38Sapatard@mandriva.com int snd_soc_get_volsw_2r_sx(struct snd_kcontrol *kcontrol, 3021b6f4bb38Sapatard@mandriva.com struct snd_ctl_elem_value *ucontrol) 3022b6f4bb38Sapatard@mandriva.com { 3023b6f4bb38Sapatard@mandriva.com struct soc_mixer_control *mc = 3024b6f4bb38Sapatard@mandriva.com (struct soc_mixer_control *)kcontrol->private_value; 3025b6f4bb38Sapatard@mandriva.com struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol); 3026b6f4bb38Sapatard@mandriva.com unsigned int mask = (1<<mc->shift)-1; 3027b6f4bb38Sapatard@mandriva.com int min = mc->min; 3028b6f4bb38Sapatard@mandriva.com int val = snd_soc_read(codec, mc->reg) & mask; 3029b6f4bb38Sapatard@mandriva.com int valr = snd_soc_read(codec, mc->rreg) & mask; 3030b6f4bb38Sapatard@mandriva.com 303120630c7fSStuart Longland ucontrol->value.integer.value[0] = ((val & 0xff)-min) & mask; 303220630c7fSStuart Longland ucontrol->value.integer.value[1] = ((valr & 0xff)-min) & mask; 3033b6f4bb38Sapatard@mandriva.com return 0; 3034b6f4bb38Sapatard@mandriva.com } 3035b6f4bb38Sapatard@mandriva.com EXPORT_SYMBOL_GPL(snd_soc_get_volsw_2r_sx); 3036b6f4bb38Sapatard@mandriva.com 3037b6f4bb38Sapatard@mandriva.com /** 3038b6f4bb38Sapatard@mandriva.com * snd_soc_put_volsw_2r_sx - double with tlv and variable data size 3039b6f4bb38Sapatard@mandriva.com * mixer put callback 3040b6f4bb38Sapatard@mandriva.com * @kcontrol: mixer control 3041b6f4bb38Sapatard@mandriva.com * @uinfo: control element information 3042b6f4bb38Sapatard@mandriva.com * 3043b6f4bb38Sapatard@mandriva.com * Returns 0 for success. 3044b6f4bb38Sapatard@mandriva.com */ 3045b6f4bb38Sapatard@mandriva.com int snd_soc_put_volsw_2r_sx(struct snd_kcontrol *kcontrol, 3046b6f4bb38Sapatard@mandriva.com struct snd_ctl_elem_value *ucontrol) 3047b6f4bb38Sapatard@mandriva.com { 3048b6f4bb38Sapatard@mandriva.com struct soc_mixer_control *mc = 3049b6f4bb38Sapatard@mandriva.com (struct soc_mixer_control *)kcontrol->private_value; 3050b6f4bb38Sapatard@mandriva.com struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol); 3051b6f4bb38Sapatard@mandriva.com unsigned int mask = (1<<mc->shift)-1; 3052b6f4bb38Sapatard@mandriva.com int min = mc->min; 3053b6f4bb38Sapatard@mandriva.com int ret; 3054b6f4bb38Sapatard@mandriva.com unsigned int val, valr, oval, ovalr; 3055b6f4bb38Sapatard@mandriva.com 3056b6f4bb38Sapatard@mandriva.com val = ((ucontrol->value.integer.value[0]+min) & 0xff); 3057b6f4bb38Sapatard@mandriva.com val &= mask; 3058b6f4bb38Sapatard@mandriva.com valr = ((ucontrol->value.integer.value[1]+min) & 0xff); 3059b6f4bb38Sapatard@mandriva.com valr &= mask; 3060b6f4bb38Sapatard@mandriva.com 3061b6f4bb38Sapatard@mandriva.com oval = snd_soc_read(codec, mc->reg) & mask; 3062b6f4bb38Sapatard@mandriva.com ovalr = snd_soc_read(codec, mc->rreg) & mask; 3063b6f4bb38Sapatard@mandriva.com 3064b6f4bb38Sapatard@mandriva.com ret = 0; 3065b6f4bb38Sapatard@mandriva.com if (oval != val) { 3066b6f4bb38Sapatard@mandriva.com ret = snd_soc_write(codec, mc->reg, val); 3067b6f4bb38Sapatard@mandriva.com if (ret < 0) 3068f1df5aecSMark Brown return ret; 3069b6f4bb38Sapatard@mandriva.com } 3070b6f4bb38Sapatard@mandriva.com if (ovalr != valr) { 3071b6f4bb38Sapatard@mandriva.com ret = snd_soc_write(codec, mc->rreg, valr); 3072b6f4bb38Sapatard@mandriva.com if (ret < 0) 3073f1df5aecSMark Brown return ret; 3074b6f4bb38Sapatard@mandriva.com } 3075b6f4bb38Sapatard@mandriva.com 3076b6f4bb38Sapatard@mandriva.com return 0; 3077b6f4bb38Sapatard@mandriva.com } 3078b6f4bb38Sapatard@mandriva.com EXPORT_SYMBOL_GPL(snd_soc_put_volsw_2r_sx); 3079b6f4bb38Sapatard@mandriva.com 3080b6f4bb38Sapatard@mandriva.com /** 30818c6529dbSLiam Girdwood * snd_soc_dai_set_sysclk - configure DAI system or master clock. 30828c6529dbSLiam Girdwood * @dai: DAI 30838c6529dbSLiam Girdwood * @clk_id: DAI specific clock ID 30848c6529dbSLiam Girdwood * @freq: new clock frequency in Hz 30858c6529dbSLiam Girdwood * @dir: new clock direction - input/output. 30868c6529dbSLiam Girdwood * 30878c6529dbSLiam Girdwood * Configures the DAI master (MCLK) or system (SYSCLK) clocking. 30888c6529dbSLiam Girdwood */ 30898c6529dbSLiam Girdwood int snd_soc_dai_set_sysclk(struct snd_soc_dai *dai, int clk_id, 30908c6529dbSLiam Girdwood unsigned int freq, int dir) 30918c6529dbSLiam Girdwood { 3092f0fba2adSLiam Girdwood if (dai->driver && dai->driver->ops->set_sysclk) 3093f0fba2adSLiam Girdwood return dai->driver->ops->set_sysclk(dai, clk_id, freq, dir); 3094ec4ee52aSMark Brown else if (dai->codec && dai->codec->driver->set_sysclk) 3095ec4ee52aSMark Brown return dai->codec->driver->set_sysclk(dai->codec, clk_id, 3096ec4ee52aSMark Brown freq, dir); 30978c6529dbSLiam Girdwood else 30988c6529dbSLiam Girdwood return -EINVAL; 30998c6529dbSLiam Girdwood } 31008c6529dbSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_dai_set_sysclk); 31018c6529dbSLiam Girdwood 31028c6529dbSLiam Girdwood /** 3103ec4ee52aSMark Brown * snd_soc_codec_set_sysclk - configure CODEC system or master clock. 3104ec4ee52aSMark Brown * @codec: CODEC 3105ec4ee52aSMark Brown * @clk_id: DAI specific clock ID 3106ec4ee52aSMark Brown * @freq: new clock frequency in Hz 3107ec4ee52aSMark Brown * @dir: new clock direction - input/output. 3108ec4ee52aSMark Brown * 3109ec4ee52aSMark Brown * Configures the CODEC master (MCLK) or system (SYSCLK) clocking. 3110ec4ee52aSMark Brown */ 3111ec4ee52aSMark Brown int snd_soc_codec_set_sysclk(struct snd_soc_codec *codec, int clk_id, 3112ec4ee52aSMark Brown unsigned int freq, int dir) 3113ec4ee52aSMark Brown { 3114ec4ee52aSMark Brown if (codec->driver->set_sysclk) 3115ec4ee52aSMark Brown return codec->driver->set_sysclk(codec, clk_id, freq, dir); 3116ec4ee52aSMark Brown else 3117ec4ee52aSMark Brown return -EINVAL; 3118ec4ee52aSMark Brown } 3119ec4ee52aSMark Brown EXPORT_SYMBOL_GPL(snd_soc_codec_set_sysclk); 3120ec4ee52aSMark Brown 3121ec4ee52aSMark Brown /** 31228c6529dbSLiam Girdwood * snd_soc_dai_set_clkdiv - configure DAI clock dividers. 31238c6529dbSLiam Girdwood * @dai: DAI 3124ac11a2b3SMark Brown * @div_id: DAI specific clock divider ID 31258c6529dbSLiam Girdwood * @div: new clock divisor. 31268c6529dbSLiam Girdwood * 31278c6529dbSLiam Girdwood * Configures the clock dividers. This is used to derive the best DAI bit and 31288c6529dbSLiam Girdwood * frame clocks from the system or master clock. It's best to set the DAI bit 31298c6529dbSLiam Girdwood * and frame clocks as low as possible to save system power. 31308c6529dbSLiam Girdwood */ 31318c6529dbSLiam Girdwood int snd_soc_dai_set_clkdiv(struct snd_soc_dai *dai, 31328c6529dbSLiam Girdwood int div_id, int div) 31338c6529dbSLiam Girdwood { 3134f0fba2adSLiam Girdwood if (dai->driver && dai->driver->ops->set_clkdiv) 3135f0fba2adSLiam Girdwood return dai->driver->ops->set_clkdiv(dai, div_id, div); 31368c6529dbSLiam Girdwood else 31378c6529dbSLiam Girdwood return -EINVAL; 31388c6529dbSLiam Girdwood } 31398c6529dbSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_dai_set_clkdiv); 31408c6529dbSLiam Girdwood 31418c6529dbSLiam Girdwood /** 31428c6529dbSLiam Girdwood * snd_soc_dai_set_pll - configure DAI PLL. 31438c6529dbSLiam Girdwood * @dai: DAI 31448c6529dbSLiam Girdwood * @pll_id: DAI specific PLL ID 314585488037SMark Brown * @source: DAI specific source for the PLL 31468c6529dbSLiam Girdwood * @freq_in: PLL input clock frequency in Hz 31478c6529dbSLiam Girdwood * @freq_out: requested PLL output clock frequency in Hz 31488c6529dbSLiam Girdwood * 31498c6529dbSLiam Girdwood * Configures and enables PLL to generate output clock based on input clock. 31508c6529dbSLiam Girdwood */ 315185488037SMark Brown int snd_soc_dai_set_pll(struct snd_soc_dai *dai, int pll_id, int source, 315285488037SMark Brown unsigned int freq_in, unsigned int freq_out) 31538c6529dbSLiam Girdwood { 3154f0fba2adSLiam Girdwood if (dai->driver && dai->driver->ops->set_pll) 3155f0fba2adSLiam Girdwood return dai->driver->ops->set_pll(dai, pll_id, source, 315685488037SMark Brown freq_in, freq_out); 3157ec4ee52aSMark Brown else if (dai->codec && dai->codec->driver->set_pll) 3158ec4ee52aSMark Brown return dai->codec->driver->set_pll(dai->codec, pll_id, source, 3159ec4ee52aSMark Brown freq_in, freq_out); 31608c6529dbSLiam Girdwood else 31618c6529dbSLiam Girdwood return -EINVAL; 31628c6529dbSLiam Girdwood } 31638c6529dbSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_dai_set_pll); 31648c6529dbSLiam Girdwood 3165ec4ee52aSMark Brown /* 3166ec4ee52aSMark Brown * snd_soc_codec_set_pll - configure codec PLL. 3167ec4ee52aSMark Brown * @codec: CODEC 3168ec4ee52aSMark Brown * @pll_id: DAI specific PLL ID 3169ec4ee52aSMark Brown * @source: DAI specific source for the PLL 3170ec4ee52aSMark Brown * @freq_in: PLL input clock frequency in Hz 3171ec4ee52aSMark Brown * @freq_out: requested PLL output clock frequency in Hz 3172ec4ee52aSMark Brown * 3173ec4ee52aSMark Brown * Configures and enables PLL to generate output clock based on input clock. 3174ec4ee52aSMark Brown */ 3175ec4ee52aSMark Brown int snd_soc_codec_set_pll(struct snd_soc_codec *codec, int pll_id, int source, 3176ec4ee52aSMark Brown unsigned int freq_in, unsigned int freq_out) 3177ec4ee52aSMark Brown { 3178ec4ee52aSMark Brown if (codec->driver->set_pll) 3179ec4ee52aSMark Brown return codec->driver->set_pll(codec, pll_id, source, 3180ec4ee52aSMark Brown freq_in, freq_out); 3181ec4ee52aSMark Brown else 3182ec4ee52aSMark Brown return -EINVAL; 3183ec4ee52aSMark Brown } 3184ec4ee52aSMark Brown EXPORT_SYMBOL_GPL(snd_soc_codec_set_pll); 3185ec4ee52aSMark Brown 31868c6529dbSLiam Girdwood /** 31878c6529dbSLiam Girdwood * snd_soc_dai_set_fmt - configure DAI hardware audio format. 31888c6529dbSLiam Girdwood * @dai: DAI 31898c6529dbSLiam Girdwood * @fmt: SND_SOC_DAIFMT_ format value. 31908c6529dbSLiam Girdwood * 31918c6529dbSLiam Girdwood * Configures the DAI hardware format and clocking. 31928c6529dbSLiam Girdwood */ 31938c6529dbSLiam Girdwood int snd_soc_dai_set_fmt(struct snd_soc_dai *dai, unsigned int fmt) 31948c6529dbSLiam Girdwood { 3195f0fba2adSLiam Girdwood if (dai->driver && dai->driver->ops->set_fmt) 3196f0fba2adSLiam Girdwood return dai->driver->ops->set_fmt(dai, fmt); 31978c6529dbSLiam Girdwood else 31988c6529dbSLiam Girdwood return -EINVAL; 31998c6529dbSLiam Girdwood } 32008c6529dbSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_dai_set_fmt); 32018c6529dbSLiam Girdwood 32028c6529dbSLiam Girdwood /** 32038c6529dbSLiam Girdwood * snd_soc_dai_set_tdm_slot - configure DAI TDM. 32048c6529dbSLiam Girdwood * @dai: DAI 3205a5479e38SDaniel Ribeiro * @tx_mask: bitmask representing active TX slots. 3206a5479e38SDaniel Ribeiro * @rx_mask: bitmask representing active RX slots. 32078c6529dbSLiam Girdwood * @slots: Number of slots in use. 3208a5479e38SDaniel Ribeiro * @slot_width: Width in bits for each slot. 32098c6529dbSLiam Girdwood * 32108c6529dbSLiam Girdwood * Configures a DAI for TDM operation. Both mask and slots are codec and DAI 32118c6529dbSLiam Girdwood * specific. 32128c6529dbSLiam Girdwood */ 32138c6529dbSLiam Girdwood int snd_soc_dai_set_tdm_slot(struct snd_soc_dai *dai, 3214a5479e38SDaniel Ribeiro unsigned int tx_mask, unsigned int rx_mask, int slots, int slot_width) 32158c6529dbSLiam Girdwood { 3216f0fba2adSLiam Girdwood if (dai->driver && dai->driver->ops->set_tdm_slot) 3217f0fba2adSLiam Girdwood return dai->driver->ops->set_tdm_slot(dai, tx_mask, rx_mask, 3218a5479e38SDaniel Ribeiro slots, slot_width); 32198c6529dbSLiam Girdwood else 32208c6529dbSLiam Girdwood return -EINVAL; 32218c6529dbSLiam Girdwood } 32228c6529dbSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_dai_set_tdm_slot); 32238c6529dbSLiam Girdwood 32248c6529dbSLiam Girdwood /** 3225472df3cbSBarry Song * snd_soc_dai_set_channel_map - configure DAI audio channel map 3226472df3cbSBarry Song * @dai: DAI 3227472df3cbSBarry Song * @tx_num: how many TX channels 3228472df3cbSBarry Song * @tx_slot: pointer to an array which imply the TX slot number channel 3229472df3cbSBarry Song * 0~num-1 uses 3230472df3cbSBarry Song * @rx_num: how many RX channels 3231472df3cbSBarry Song * @rx_slot: pointer to an array which imply the RX slot number channel 3232472df3cbSBarry Song * 0~num-1 uses 3233472df3cbSBarry Song * 3234472df3cbSBarry Song * configure the relationship between channel number and TDM slot number. 3235472df3cbSBarry Song */ 3236472df3cbSBarry Song int snd_soc_dai_set_channel_map(struct snd_soc_dai *dai, 3237472df3cbSBarry Song unsigned int tx_num, unsigned int *tx_slot, 3238472df3cbSBarry Song unsigned int rx_num, unsigned int *rx_slot) 3239472df3cbSBarry Song { 3240f0fba2adSLiam Girdwood if (dai->driver && dai->driver->ops->set_channel_map) 3241f0fba2adSLiam Girdwood return dai->driver->ops->set_channel_map(dai, tx_num, tx_slot, 3242472df3cbSBarry Song rx_num, rx_slot); 3243472df3cbSBarry Song else 3244472df3cbSBarry Song return -EINVAL; 3245472df3cbSBarry Song } 3246472df3cbSBarry Song EXPORT_SYMBOL_GPL(snd_soc_dai_set_channel_map); 3247472df3cbSBarry Song 3248472df3cbSBarry Song /** 32498c6529dbSLiam Girdwood * snd_soc_dai_set_tristate - configure DAI system or master clock. 32508c6529dbSLiam Girdwood * @dai: DAI 32518c6529dbSLiam Girdwood * @tristate: tristate enable 32528c6529dbSLiam Girdwood * 32538c6529dbSLiam Girdwood * Tristates the DAI so that others can use it. 32548c6529dbSLiam Girdwood */ 32558c6529dbSLiam Girdwood int snd_soc_dai_set_tristate(struct snd_soc_dai *dai, int tristate) 32568c6529dbSLiam Girdwood { 3257f0fba2adSLiam Girdwood if (dai->driver && dai->driver->ops->set_tristate) 3258f0fba2adSLiam Girdwood return dai->driver->ops->set_tristate(dai, tristate); 32598c6529dbSLiam Girdwood else 32608c6529dbSLiam Girdwood return -EINVAL; 32618c6529dbSLiam Girdwood } 32628c6529dbSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_dai_set_tristate); 32638c6529dbSLiam Girdwood 32648c6529dbSLiam Girdwood /** 32658c6529dbSLiam Girdwood * snd_soc_dai_digital_mute - configure DAI system or master clock. 32668c6529dbSLiam Girdwood * @dai: DAI 32678c6529dbSLiam Girdwood * @mute: mute enable 32688c6529dbSLiam Girdwood * 32698c6529dbSLiam Girdwood * Mutes the DAI DAC. 32708c6529dbSLiam Girdwood */ 32718c6529dbSLiam Girdwood int snd_soc_dai_digital_mute(struct snd_soc_dai *dai, int mute) 32728c6529dbSLiam Girdwood { 3273f0fba2adSLiam Girdwood if (dai->driver && dai->driver->ops->digital_mute) 3274f0fba2adSLiam Girdwood return dai->driver->ops->digital_mute(dai, mute); 32758c6529dbSLiam Girdwood else 32768c6529dbSLiam Girdwood return -EINVAL; 32778c6529dbSLiam Girdwood } 32788c6529dbSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_dai_digital_mute); 32798c6529dbSLiam Girdwood 3280c5af3a2eSMark Brown /** 3281c5af3a2eSMark Brown * snd_soc_register_card - Register a card with the ASoC core 3282c5af3a2eSMark Brown * 3283ac11a2b3SMark Brown * @card: Card to register 3284c5af3a2eSMark Brown * 3285c5af3a2eSMark Brown */ 328670a7ca34SVinod Koul int snd_soc_register_card(struct snd_soc_card *card) 3287c5af3a2eSMark Brown { 3288f0fba2adSLiam Girdwood int i; 3289f0fba2adSLiam Girdwood 3290c5af3a2eSMark Brown if (!card->name || !card->dev) 3291c5af3a2eSMark Brown return -EINVAL; 3292c5af3a2eSMark Brown 3293111c6419SStephen Warren snd_soc_initialize_card_lists(card); 3294111c6419SStephen Warren 3295150dd2f8SVinod Koul soc_init_card_debugfs(card); 3296150dd2f8SVinod Koul 32972eea392dSJarkko Nikula card->rtd = kzalloc(sizeof(struct snd_soc_pcm_runtime) * 32982eea392dSJarkko Nikula (card->num_links + card->num_aux_devs), 3299f0fba2adSLiam Girdwood GFP_KERNEL); 3300f0fba2adSLiam Girdwood if (card->rtd == NULL) 3301f0fba2adSLiam Girdwood return -ENOMEM; 33022eea392dSJarkko Nikula card->rtd_aux = &card->rtd[card->num_links]; 3303f0fba2adSLiam Girdwood 3304f0fba2adSLiam Girdwood for (i = 0; i < card->num_links; i++) 3305f0fba2adSLiam Girdwood card->rtd[i].dai_link = &card->dai_link[i]; 3306f0fba2adSLiam Girdwood 3307c5af3a2eSMark Brown INIT_LIST_HEAD(&card->list); 3308c5af3a2eSMark Brown card->instantiated = 0; 3309f0fba2adSLiam Girdwood mutex_init(&card->mutex); 3310c5af3a2eSMark Brown 3311c5af3a2eSMark Brown mutex_lock(&client_mutex); 3312c5af3a2eSMark Brown list_add(&card->list, &card_list); 3313435c5e25SMark Brown snd_soc_instantiate_cards(); 3314c5af3a2eSMark Brown mutex_unlock(&client_mutex); 3315c5af3a2eSMark Brown 3316c5af3a2eSMark Brown dev_dbg(card->dev, "Registered card '%s'\n", card->name); 3317c5af3a2eSMark Brown 3318c5af3a2eSMark Brown return 0; 3319c5af3a2eSMark Brown } 332070a7ca34SVinod Koul EXPORT_SYMBOL_GPL(snd_soc_register_card); 3321c5af3a2eSMark Brown 3322c5af3a2eSMark Brown /** 3323c5af3a2eSMark Brown * snd_soc_unregister_card - Unregister a card with the ASoC core 3324c5af3a2eSMark Brown * 3325ac11a2b3SMark Brown * @card: Card to unregister 3326c5af3a2eSMark Brown * 3327c5af3a2eSMark Brown */ 332870a7ca34SVinod Koul int snd_soc_unregister_card(struct snd_soc_card *card) 3329c5af3a2eSMark Brown { 3330b0e26485SVinod Koul if (card->instantiated) 3331b0e26485SVinod Koul soc_cleanup_card_resources(card); 3332c5af3a2eSMark Brown mutex_lock(&client_mutex); 3333c5af3a2eSMark Brown list_del(&card->list); 3334c5af3a2eSMark Brown mutex_unlock(&client_mutex); 3335c5af3a2eSMark Brown dev_dbg(card->dev, "Unregistered card '%s'\n", card->name); 3336c5af3a2eSMark Brown 3337c5af3a2eSMark Brown return 0; 3338c5af3a2eSMark Brown } 333970a7ca34SVinod Koul EXPORT_SYMBOL_GPL(snd_soc_unregister_card); 3340c5af3a2eSMark Brown 3341f0fba2adSLiam Girdwood /* 3342f0fba2adSLiam Girdwood * Simplify DAI link configuration by removing ".-1" from device names 3343f0fba2adSLiam Girdwood * and sanitizing names. 3344f0fba2adSLiam Girdwood */ 33450b9a214aSDimitris Papastamos static char *fmt_single_name(struct device *dev, int *id) 3346f0fba2adSLiam Girdwood { 3347f0fba2adSLiam Girdwood char *found, name[NAME_SIZE]; 3348f0fba2adSLiam Girdwood int id1, id2; 3349f0fba2adSLiam Girdwood 3350f0fba2adSLiam Girdwood if (dev_name(dev) == NULL) 3351f0fba2adSLiam Girdwood return NULL; 3352f0fba2adSLiam Girdwood 335358818a77SDimitris Papastamos strlcpy(name, dev_name(dev), NAME_SIZE); 3354f0fba2adSLiam Girdwood 3355f0fba2adSLiam Girdwood /* are we a "%s.%d" name (platform and SPI components) */ 3356f0fba2adSLiam Girdwood found = strstr(name, dev->driver->name); 3357f0fba2adSLiam Girdwood if (found) { 3358f0fba2adSLiam Girdwood /* get ID */ 3359f0fba2adSLiam Girdwood if (sscanf(&found[strlen(dev->driver->name)], ".%d", id) == 1) { 3360f0fba2adSLiam Girdwood 3361f0fba2adSLiam Girdwood /* discard ID from name if ID == -1 */ 3362f0fba2adSLiam Girdwood if (*id == -1) 3363f0fba2adSLiam Girdwood found[strlen(dev->driver->name)] = '\0'; 3364f0fba2adSLiam Girdwood } 3365f0fba2adSLiam Girdwood 3366f0fba2adSLiam Girdwood } else { 3367f0fba2adSLiam Girdwood /* I2C component devices are named "bus-addr" */ 3368f0fba2adSLiam Girdwood if (sscanf(name, "%x-%x", &id1, &id2) == 2) { 3369f0fba2adSLiam Girdwood char tmp[NAME_SIZE]; 3370f0fba2adSLiam Girdwood 3371f0fba2adSLiam Girdwood /* create unique ID number from I2C addr and bus */ 337205899446SJarkko Nikula *id = ((id1 & 0xffff) << 16) + id2; 3373f0fba2adSLiam Girdwood 3374f0fba2adSLiam Girdwood /* sanitize component name for DAI link creation */ 3375f0fba2adSLiam Girdwood snprintf(tmp, NAME_SIZE, "%s.%s", dev->driver->name, name); 337658818a77SDimitris Papastamos strlcpy(name, tmp, NAME_SIZE); 3377f0fba2adSLiam Girdwood } else 3378f0fba2adSLiam Girdwood *id = 0; 3379f0fba2adSLiam Girdwood } 3380f0fba2adSLiam Girdwood 3381f0fba2adSLiam Girdwood return kstrdup(name, GFP_KERNEL); 3382f0fba2adSLiam Girdwood } 3383f0fba2adSLiam Girdwood 3384f0fba2adSLiam Girdwood /* 3385f0fba2adSLiam Girdwood * Simplify DAI link naming for single devices with multiple DAIs by removing 3386f0fba2adSLiam Girdwood * any ".-1" and using the DAI name (instead of device name). 3387f0fba2adSLiam Girdwood */ 3388f0fba2adSLiam Girdwood static inline char *fmt_multiple_name(struct device *dev, 3389f0fba2adSLiam Girdwood struct snd_soc_dai_driver *dai_drv) 3390f0fba2adSLiam Girdwood { 3391f0fba2adSLiam Girdwood if (dai_drv->name == NULL) { 3392f0fba2adSLiam Girdwood printk(KERN_ERR "asoc: error - multiple DAI %s registered with no name\n", 3393f0fba2adSLiam Girdwood dev_name(dev)); 3394f0fba2adSLiam Girdwood return NULL; 3395f0fba2adSLiam Girdwood } 3396f0fba2adSLiam Girdwood 3397f0fba2adSLiam Girdwood return kstrdup(dai_drv->name, GFP_KERNEL); 3398f0fba2adSLiam Girdwood } 3399f0fba2adSLiam Girdwood 34009115171aSMark Brown /** 34019115171aSMark Brown * snd_soc_register_dai - Register a DAI with the ASoC core 34029115171aSMark Brown * 3403ac11a2b3SMark Brown * @dai: DAI to register 34049115171aSMark Brown */ 3405f0fba2adSLiam Girdwood int snd_soc_register_dai(struct device *dev, 3406f0fba2adSLiam Girdwood struct snd_soc_dai_driver *dai_drv) 34079115171aSMark Brown { 3408f0fba2adSLiam Girdwood struct snd_soc_dai *dai; 34099115171aSMark Brown 3410f0fba2adSLiam Girdwood dev_dbg(dev, "dai register %s\n", dev_name(dev)); 34119115171aSMark Brown 3412f0fba2adSLiam Girdwood dai = kzalloc(sizeof(struct snd_soc_dai), GFP_KERNEL); 3413f0fba2adSLiam Girdwood if (dai == NULL) 3414f0fba2adSLiam Girdwood return -ENOMEM; 34156335d055SEric Miao 3416f0fba2adSLiam Girdwood /* create DAI component name */ 3417f0fba2adSLiam Girdwood dai->name = fmt_single_name(dev, &dai->id); 3418f0fba2adSLiam Girdwood if (dai->name == NULL) { 3419f0fba2adSLiam Girdwood kfree(dai); 3420f0fba2adSLiam Girdwood return -ENOMEM; 3421f0fba2adSLiam Girdwood } 3422f0fba2adSLiam Girdwood 3423f0fba2adSLiam Girdwood dai->dev = dev; 3424f0fba2adSLiam Girdwood dai->driver = dai_drv; 3425f0fba2adSLiam Girdwood if (!dai->driver->ops) 3426f0fba2adSLiam Girdwood dai->driver->ops = &null_dai_ops; 34279115171aSMark Brown 34289115171aSMark Brown mutex_lock(&client_mutex); 34299115171aSMark Brown list_add(&dai->list, &dai_list); 3430435c5e25SMark Brown snd_soc_instantiate_cards(); 34319115171aSMark Brown mutex_unlock(&client_mutex); 34329115171aSMark Brown 34339115171aSMark Brown pr_debug("Registered DAI '%s'\n", dai->name); 34349115171aSMark Brown 34359115171aSMark Brown return 0; 34369115171aSMark Brown } 34379115171aSMark Brown EXPORT_SYMBOL_GPL(snd_soc_register_dai); 34389115171aSMark Brown 34399115171aSMark Brown /** 34409115171aSMark Brown * snd_soc_unregister_dai - Unregister a DAI from the ASoC core 34419115171aSMark Brown * 3442ac11a2b3SMark Brown * @dai: DAI to unregister 34439115171aSMark Brown */ 3444f0fba2adSLiam Girdwood void snd_soc_unregister_dai(struct device *dev) 34459115171aSMark Brown { 3446f0fba2adSLiam Girdwood struct snd_soc_dai *dai; 3447f0fba2adSLiam Girdwood 3448f0fba2adSLiam Girdwood list_for_each_entry(dai, &dai_list, list) { 3449f0fba2adSLiam Girdwood if (dev == dai->dev) 3450f0fba2adSLiam Girdwood goto found; 3451f0fba2adSLiam Girdwood } 3452f0fba2adSLiam Girdwood return; 3453f0fba2adSLiam Girdwood 3454f0fba2adSLiam Girdwood found: 34559115171aSMark Brown mutex_lock(&client_mutex); 34569115171aSMark Brown list_del(&dai->list); 34579115171aSMark Brown mutex_unlock(&client_mutex); 34589115171aSMark Brown 34599115171aSMark Brown pr_debug("Unregistered DAI '%s'\n", dai->name); 3460f0fba2adSLiam Girdwood kfree(dai->name); 3461f0fba2adSLiam Girdwood kfree(dai); 34629115171aSMark Brown } 34639115171aSMark Brown EXPORT_SYMBOL_GPL(snd_soc_unregister_dai); 34649115171aSMark Brown 34659115171aSMark Brown /** 34669115171aSMark Brown * snd_soc_register_dais - Register multiple DAIs with the ASoC core 34679115171aSMark Brown * 3468ac11a2b3SMark Brown * @dai: Array of DAIs to register 3469ac11a2b3SMark Brown * @count: Number of DAIs 34709115171aSMark Brown */ 3471f0fba2adSLiam Girdwood int snd_soc_register_dais(struct device *dev, 3472f0fba2adSLiam Girdwood struct snd_soc_dai_driver *dai_drv, size_t count) 34739115171aSMark Brown { 3474f0fba2adSLiam Girdwood struct snd_soc_dai *dai; 3475f0fba2adSLiam Girdwood int i, ret = 0; 3476f0fba2adSLiam Girdwood 3477720ffa4cSLiam Girdwood dev_dbg(dev, "dai register %s #%Zu\n", dev_name(dev), count); 34789115171aSMark Brown 34799115171aSMark Brown for (i = 0; i < count; i++) { 3480f0fba2adSLiam Girdwood 3481f0fba2adSLiam Girdwood dai = kzalloc(sizeof(struct snd_soc_dai), GFP_KERNEL); 3482c46e0079SAxel Lin if (dai == NULL) { 3483c46e0079SAxel Lin ret = -ENOMEM; 3484c46e0079SAxel Lin goto err; 3485c46e0079SAxel Lin } 3486f0fba2adSLiam Girdwood 3487f0fba2adSLiam Girdwood /* create DAI component name */ 3488f0fba2adSLiam Girdwood dai->name = fmt_multiple_name(dev, &dai_drv[i]); 3489f0fba2adSLiam Girdwood if (dai->name == NULL) { 3490f0fba2adSLiam Girdwood kfree(dai); 3491f0fba2adSLiam Girdwood ret = -EINVAL; 34929115171aSMark Brown goto err; 34939115171aSMark Brown } 34949115171aSMark Brown 3495f0fba2adSLiam Girdwood dai->dev = dev; 3496f0fba2adSLiam Girdwood dai->driver = &dai_drv[i]; 34970f9141c9SMark Brown if (dai->driver->id) 34980f9141c9SMark Brown dai->id = dai->driver->id; 34990f9141c9SMark Brown else 35000f9141c9SMark Brown dai->id = i; 3501f0fba2adSLiam Girdwood if (!dai->driver->ops) 3502f0fba2adSLiam Girdwood dai->driver->ops = &null_dai_ops; 3503f0fba2adSLiam Girdwood 3504f0fba2adSLiam Girdwood mutex_lock(&client_mutex); 3505f0fba2adSLiam Girdwood list_add(&dai->list, &dai_list); 3506f0fba2adSLiam Girdwood mutex_unlock(&client_mutex); 3507f0fba2adSLiam Girdwood 3508f0fba2adSLiam Girdwood pr_debug("Registered DAI '%s'\n", dai->name); 3509f0fba2adSLiam Girdwood } 3510f0fba2adSLiam Girdwood 35111dcb4f38SAxel Lin mutex_lock(&client_mutex); 3512f0fba2adSLiam Girdwood snd_soc_instantiate_cards(); 35131dcb4f38SAxel Lin mutex_unlock(&client_mutex); 35149115171aSMark Brown return 0; 35159115171aSMark Brown 35169115171aSMark Brown err: 35179115171aSMark Brown for (i--; i >= 0; i--) 3518f0fba2adSLiam Girdwood snd_soc_unregister_dai(dev); 35199115171aSMark Brown 35209115171aSMark Brown return ret; 35219115171aSMark Brown } 35229115171aSMark Brown EXPORT_SYMBOL_GPL(snd_soc_register_dais); 35239115171aSMark Brown 35249115171aSMark Brown /** 35259115171aSMark Brown * snd_soc_unregister_dais - Unregister multiple DAIs from the ASoC core 35269115171aSMark Brown * 3527ac11a2b3SMark Brown * @dai: Array of DAIs to unregister 3528ac11a2b3SMark Brown * @count: Number of DAIs 35299115171aSMark Brown */ 3530f0fba2adSLiam Girdwood void snd_soc_unregister_dais(struct device *dev, size_t count) 35319115171aSMark Brown { 35329115171aSMark Brown int i; 35339115171aSMark Brown 35349115171aSMark Brown for (i = 0; i < count; i++) 3535f0fba2adSLiam Girdwood snd_soc_unregister_dai(dev); 35369115171aSMark Brown } 35379115171aSMark Brown EXPORT_SYMBOL_GPL(snd_soc_unregister_dais); 35389115171aSMark Brown 353912a48a8cSMark Brown /** 354012a48a8cSMark Brown * snd_soc_register_platform - Register a platform with the ASoC core 354112a48a8cSMark Brown * 3542ac11a2b3SMark Brown * @platform: platform to register 354312a48a8cSMark Brown */ 3544f0fba2adSLiam Girdwood int snd_soc_register_platform(struct device *dev, 3545f0fba2adSLiam Girdwood struct snd_soc_platform_driver *platform_drv) 354612a48a8cSMark Brown { 3547f0fba2adSLiam Girdwood struct snd_soc_platform *platform; 354812a48a8cSMark Brown 3549f0fba2adSLiam Girdwood dev_dbg(dev, "platform register %s\n", dev_name(dev)); 3550f0fba2adSLiam Girdwood 3551f0fba2adSLiam Girdwood platform = kzalloc(sizeof(struct snd_soc_platform), GFP_KERNEL); 3552f0fba2adSLiam Girdwood if (platform == NULL) 3553f0fba2adSLiam Girdwood return -ENOMEM; 3554f0fba2adSLiam Girdwood 3555f0fba2adSLiam Girdwood /* create platform component name */ 3556f0fba2adSLiam Girdwood platform->name = fmt_single_name(dev, &platform->id); 3557f0fba2adSLiam Girdwood if (platform->name == NULL) { 3558f0fba2adSLiam Girdwood kfree(platform); 3559f0fba2adSLiam Girdwood return -ENOMEM; 3560f0fba2adSLiam Girdwood } 3561f0fba2adSLiam Girdwood 3562f0fba2adSLiam Girdwood platform->dev = dev; 3563f0fba2adSLiam Girdwood platform->driver = platform_drv; 356412a48a8cSMark Brown 356512a48a8cSMark Brown mutex_lock(&client_mutex); 356612a48a8cSMark Brown list_add(&platform->list, &platform_list); 3567435c5e25SMark Brown snd_soc_instantiate_cards(); 356812a48a8cSMark Brown mutex_unlock(&client_mutex); 356912a48a8cSMark Brown 357012a48a8cSMark Brown pr_debug("Registered platform '%s'\n", platform->name); 357112a48a8cSMark Brown 357212a48a8cSMark Brown return 0; 357312a48a8cSMark Brown } 357412a48a8cSMark Brown EXPORT_SYMBOL_GPL(snd_soc_register_platform); 357512a48a8cSMark Brown 357612a48a8cSMark Brown /** 357712a48a8cSMark Brown * snd_soc_unregister_platform - Unregister a platform from the ASoC core 357812a48a8cSMark Brown * 3579ac11a2b3SMark Brown * @platform: platform to unregister 358012a48a8cSMark Brown */ 3581f0fba2adSLiam Girdwood void snd_soc_unregister_platform(struct device *dev) 358212a48a8cSMark Brown { 3583f0fba2adSLiam Girdwood struct snd_soc_platform *platform; 3584f0fba2adSLiam Girdwood 3585f0fba2adSLiam Girdwood list_for_each_entry(platform, &platform_list, list) { 3586f0fba2adSLiam Girdwood if (dev == platform->dev) 3587f0fba2adSLiam Girdwood goto found; 3588f0fba2adSLiam Girdwood } 3589f0fba2adSLiam Girdwood return; 3590f0fba2adSLiam Girdwood 3591f0fba2adSLiam Girdwood found: 359212a48a8cSMark Brown mutex_lock(&client_mutex); 359312a48a8cSMark Brown list_del(&platform->list); 359412a48a8cSMark Brown mutex_unlock(&client_mutex); 359512a48a8cSMark Brown 359612a48a8cSMark Brown pr_debug("Unregistered platform '%s'\n", platform->name); 3597f0fba2adSLiam Girdwood kfree(platform->name); 3598f0fba2adSLiam Girdwood kfree(platform); 359912a48a8cSMark Brown } 360012a48a8cSMark Brown EXPORT_SYMBOL_GPL(snd_soc_unregister_platform); 360112a48a8cSMark Brown 3602151ab22cSMark Brown static u64 codec_format_map[] = { 3603151ab22cSMark Brown SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S16_BE, 3604151ab22cSMark Brown SNDRV_PCM_FMTBIT_U16_LE | SNDRV_PCM_FMTBIT_U16_BE, 3605151ab22cSMark Brown SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S24_BE, 3606151ab22cSMark Brown SNDRV_PCM_FMTBIT_U24_LE | SNDRV_PCM_FMTBIT_U24_BE, 3607151ab22cSMark Brown SNDRV_PCM_FMTBIT_S32_LE | SNDRV_PCM_FMTBIT_S32_BE, 3608151ab22cSMark Brown SNDRV_PCM_FMTBIT_U32_LE | SNDRV_PCM_FMTBIT_U32_BE, 3609151ab22cSMark Brown SNDRV_PCM_FMTBIT_S24_3LE | SNDRV_PCM_FMTBIT_U24_3BE, 3610151ab22cSMark Brown SNDRV_PCM_FMTBIT_U24_3LE | SNDRV_PCM_FMTBIT_U24_3BE, 3611151ab22cSMark Brown SNDRV_PCM_FMTBIT_S20_3LE | SNDRV_PCM_FMTBIT_S20_3BE, 3612151ab22cSMark Brown SNDRV_PCM_FMTBIT_U20_3LE | SNDRV_PCM_FMTBIT_U20_3BE, 3613151ab22cSMark Brown SNDRV_PCM_FMTBIT_S18_3LE | SNDRV_PCM_FMTBIT_S18_3BE, 3614151ab22cSMark Brown SNDRV_PCM_FMTBIT_U18_3LE | SNDRV_PCM_FMTBIT_U18_3BE, 3615151ab22cSMark Brown SNDRV_PCM_FMTBIT_FLOAT_LE | SNDRV_PCM_FMTBIT_FLOAT_BE, 3616151ab22cSMark Brown SNDRV_PCM_FMTBIT_FLOAT64_LE | SNDRV_PCM_FMTBIT_FLOAT64_BE, 3617151ab22cSMark Brown SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_LE 3618151ab22cSMark Brown | SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_BE, 3619151ab22cSMark Brown }; 3620151ab22cSMark Brown 3621151ab22cSMark Brown /* Fix up the DAI formats for endianness: codecs don't actually see 3622151ab22cSMark Brown * the endianness of the data but we're using the CPU format 3623151ab22cSMark Brown * definitions which do need to include endianness so we ensure that 3624151ab22cSMark Brown * codec DAIs always have both big and little endian variants set. 3625151ab22cSMark Brown */ 3626151ab22cSMark Brown static void fixup_codec_formats(struct snd_soc_pcm_stream *stream) 3627151ab22cSMark Brown { 3628151ab22cSMark Brown int i; 3629151ab22cSMark Brown 3630151ab22cSMark Brown for (i = 0; i < ARRAY_SIZE(codec_format_map); i++) 3631151ab22cSMark Brown if (stream->formats & codec_format_map[i]) 3632151ab22cSMark Brown stream->formats |= codec_format_map[i]; 3633151ab22cSMark Brown } 3634151ab22cSMark Brown 36350d0cf00aSMark Brown /** 36360d0cf00aSMark Brown * snd_soc_register_codec - Register a codec with the ASoC core 36370d0cf00aSMark Brown * 3638ac11a2b3SMark Brown * @codec: codec to register 36390d0cf00aSMark Brown */ 3640f0fba2adSLiam Girdwood int snd_soc_register_codec(struct device *dev, 3641001ae4c0SMark Brown const struct snd_soc_codec_driver *codec_drv, 3642001ae4c0SMark Brown struct snd_soc_dai_driver *dai_drv, 3643001ae4c0SMark Brown int num_dai) 36440d0cf00aSMark Brown { 36453335ddcaSDimitris Papastamos size_t reg_size; 3646f0fba2adSLiam Girdwood struct snd_soc_codec *codec; 3647f0fba2adSLiam Girdwood int ret, i; 3648151ab22cSMark Brown 3649f0fba2adSLiam Girdwood dev_dbg(dev, "codec register %s\n", dev_name(dev)); 36500d0cf00aSMark Brown 3651f0fba2adSLiam Girdwood codec = kzalloc(sizeof(struct snd_soc_codec), GFP_KERNEL); 3652f0fba2adSLiam Girdwood if (codec == NULL) 3653f0fba2adSLiam Girdwood return -ENOMEM; 36540d0cf00aSMark Brown 3655f0fba2adSLiam Girdwood /* create CODEC component name */ 3656f0fba2adSLiam Girdwood codec->name = fmt_single_name(dev, &codec->id); 3657f0fba2adSLiam Girdwood if (codec->name == NULL) { 3658f0fba2adSLiam Girdwood kfree(codec); 3659f0fba2adSLiam Girdwood return -ENOMEM; 3660151ab22cSMark Brown } 3661151ab22cSMark Brown 366223bbce34SDimitris Papastamos if (codec_drv->compress_type) 366323bbce34SDimitris Papastamos codec->compress_type = codec_drv->compress_type; 366423bbce34SDimitris Papastamos else 366523bbce34SDimitris Papastamos codec->compress_type = SND_SOC_FLAT_COMPRESSION; 366623bbce34SDimitris Papastamos 3667c3acec26SMark Brown codec->write = codec_drv->write; 3668c3acec26SMark Brown codec->read = codec_drv->read; 36691500b7b5SDimitris Papastamos codec->volatile_register = codec_drv->volatile_register; 36701500b7b5SDimitris Papastamos codec->readable_register = codec_drv->readable_register; 3671ce6120ccSLiam Girdwood codec->dapm.bias_level = SND_SOC_BIAS_OFF; 3672ce6120ccSLiam Girdwood codec->dapm.dev = dev; 3673ce6120ccSLiam Girdwood codec->dapm.codec = codec; 3674474b62d6SMark Brown codec->dapm.seq_notifier = codec_drv->seq_notifier; 3675f0fba2adSLiam Girdwood codec->dev = dev; 3676f0fba2adSLiam Girdwood codec->driver = codec_drv; 3677f0fba2adSLiam Girdwood codec->num_dai = num_dai; 3678f0fba2adSLiam Girdwood mutex_init(&codec->mutex); 3679f0fba2adSLiam Girdwood 36807a30a3dbSDimitris Papastamos /* allocate CODEC register cache */ 36817a30a3dbSDimitris Papastamos if (codec_drv->reg_cache_size && codec_drv->reg_word_size) { 36823335ddcaSDimitris Papastamos reg_size = codec_drv->reg_cache_size * codec_drv->reg_word_size; 3683aea170a0SDimitris Papastamos codec->reg_size = reg_size; 36843335ddcaSDimitris Papastamos /* it is necessary to make a copy of the default register cache 36853335ddcaSDimitris Papastamos * because in the case of using a compression type that requires 36863335ddcaSDimitris Papastamos * the default register cache to be marked as __devinitconst the 36873335ddcaSDimitris Papastamos * kernel might have freed the array by the time we initialize 36883335ddcaSDimitris Papastamos * the cache. 36893335ddcaSDimitris Papastamos */ 36902aa86323SDimitris Papastamos if (codec_drv->reg_cache_default) { 36913335ddcaSDimitris Papastamos codec->reg_def_copy = kmemdup(codec_drv->reg_cache_default, 36923335ddcaSDimitris Papastamos reg_size, GFP_KERNEL); 36933335ddcaSDimitris Papastamos if (!codec->reg_def_copy) { 36943335ddcaSDimitris Papastamos ret = -ENOMEM; 3695fdf0f54dSDimitris Papastamos goto fail; 36967a30a3dbSDimitris Papastamos } 36977a30a3dbSDimitris Papastamos } 36982aa86323SDimitris Papastamos } 36997a30a3dbSDimitris Papastamos 37001500b7b5SDimitris Papastamos if (codec_drv->reg_access_size && codec_drv->reg_access_default) { 37011500b7b5SDimitris Papastamos if (!codec->volatile_register) 37021500b7b5SDimitris Papastamos codec->volatile_register = snd_soc_default_volatile_register; 37031500b7b5SDimitris Papastamos if (!codec->readable_register) 37041500b7b5SDimitris Papastamos codec->readable_register = snd_soc_default_readable_register; 37051500b7b5SDimitris Papastamos } 37061500b7b5SDimitris Papastamos 3707f0fba2adSLiam Girdwood for (i = 0; i < num_dai; i++) { 3708f0fba2adSLiam Girdwood fixup_codec_formats(&dai_drv[i].playback); 3709f0fba2adSLiam Girdwood fixup_codec_formats(&dai_drv[i].capture); 3710f0fba2adSLiam Girdwood } 3711f0fba2adSLiam Girdwood 371226b01ccdSMark Brown /* register any DAIs */ 371326b01ccdSMark Brown if (num_dai) { 3714f0fba2adSLiam Girdwood ret = snd_soc_register_dais(dev, dai_drv, num_dai); 3715f0fba2adSLiam Girdwood if (ret < 0) 3716fdf0f54dSDimitris Papastamos goto fail; 371726b01ccdSMark Brown } 3718f0fba2adSLiam Girdwood 37190d0cf00aSMark Brown mutex_lock(&client_mutex); 37200d0cf00aSMark Brown list_add(&codec->list, &codec_list); 37210d0cf00aSMark Brown snd_soc_instantiate_cards(); 37220d0cf00aSMark Brown mutex_unlock(&client_mutex); 37230d0cf00aSMark Brown 37240d0cf00aSMark Brown pr_debug("Registered codec '%s'\n", codec->name); 37250d0cf00aSMark Brown return 0; 3726f0fba2adSLiam Girdwood 3727fdf0f54dSDimitris Papastamos fail: 37283335ddcaSDimitris Papastamos kfree(codec->reg_def_copy); 37293335ddcaSDimitris Papastamos codec->reg_def_copy = NULL; 3730f0fba2adSLiam Girdwood kfree(codec->name); 3731f0fba2adSLiam Girdwood kfree(codec); 3732f0fba2adSLiam Girdwood return ret; 37330d0cf00aSMark Brown } 37340d0cf00aSMark Brown EXPORT_SYMBOL_GPL(snd_soc_register_codec); 37350d0cf00aSMark Brown 37360d0cf00aSMark Brown /** 37370d0cf00aSMark Brown * snd_soc_unregister_codec - Unregister a codec from the ASoC core 37380d0cf00aSMark Brown * 3739ac11a2b3SMark Brown * @codec: codec to unregister 37400d0cf00aSMark Brown */ 3741f0fba2adSLiam Girdwood void snd_soc_unregister_codec(struct device *dev) 37420d0cf00aSMark Brown { 3743f0fba2adSLiam Girdwood struct snd_soc_codec *codec; 3744f0fba2adSLiam Girdwood int i; 3745f0fba2adSLiam Girdwood 3746f0fba2adSLiam Girdwood list_for_each_entry(codec, &codec_list, list) { 3747f0fba2adSLiam Girdwood if (dev == codec->dev) 3748f0fba2adSLiam Girdwood goto found; 3749f0fba2adSLiam Girdwood } 3750f0fba2adSLiam Girdwood return; 3751f0fba2adSLiam Girdwood 3752f0fba2adSLiam Girdwood found: 375326b01ccdSMark Brown if (codec->num_dai) 3754f0fba2adSLiam Girdwood for (i = 0; i < codec->num_dai; i++) 3755f0fba2adSLiam Girdwood snd_soc_unregister_dai(dev); 3756f0fba2adSLiam Girdwood 37570d0cf00aSMark Brown mutex_lock(&client_mutex); 37580d0cf00aSMark Brown list_del(&codec->list); 37590d0cf00aSMark Brown mutex_unlock(&client_mutex); 37600d0cf00aSMark Brown 37610d0cf00aSMark Brown pr_debug("Unregistered codec '%s'\n", codec->name); 3762f0fba2adSLiam Girdwood 37637a30a3dbSDimitris Papastamos snd_soc_cache_exit(codec); 37643335ddcaSDimitris Papastamos kfree(codec->reg_def_copy); 37651aafcd4dSDimitris Papastamos kfree(codec->name); 3766f0fba2adSLiam Girdwood kfree(codec); 37670d0cf00aSMark Brown } 37680d0cf00aSMark Brown EXPORT_SYMBOL_GPL(snd_soc_unregister_codec); 37690d0cf00aSMark Brown 3770c9b3a40fSTakashi Iwai static int __init snd_soc_init(void) 3771db2a4165SFrank Mandarino { 3772384c89e2SMark Brown #ifdef CONFIG_DEBUG_FS 37738a9dab1aSMark Brown snd_soc_debugfs_root = debugfs_create_dir("asoc", NULL); 37748a9dab1aSMark Brown if (IS_ERR(snd_soc_debugfs_root) || !snd_soc_debugfs_root) { 3775384c89e2SMark Brown printk(KERN_WARNING 3776384c89e2SMark Brown "ASoC: Failed to create debugfs directory\n"); 37778a9dab1aSMark Brown snd_soc_debugfs_root = NULL; 3778384c89e2SMark Brown } 3779c3c5a19aSMark Brown 37808a9dab1aSMark Brown if (!debugfs_create_file("codecs", 0444, snd_soc_debugfs_root, NULL, 3781c3c5a19aSMark Brown &codec_list_fops)) 3782c3c5a19aSMark Brown pr_warn("ASoC: Failed to create CODEC list debugfs file\n"); 3783c3c5a19aSMark Brown 37848a9dab1aSMark Brown if (!debugfs_create_file("dais", 0444, snd_soc_debugfs_root, NULL, 3785f3208780SMark Brown &dai_list_fops)) 3786f3208780SMark Brown pr_warn("ASoC: Failed to create DAI list debugfs file\n"); 378719c7ac27SMark Brown 37888a9dab1aSMark Brown if (!debugfs_create_file("platforms", 0444, snd_soc_debugfs_root, NULL, 378919c7ac27SMark Brown &platform_list_fops)) 379019c7ac27SMark Brown pr_warn("ASoC: Failed to create platform list debugfs file\n"); 3791384c89e2SMark Brown #endif 3792384c89e2SMark Brown 3793db2a4165SFrank Mandarino return platform_driver_register(&soc_driver); 3794db2a4165SFrank Mandarino } 37954abe8e16SMark Brown module_init(snd_soc_init); 3796db2a4165SFrank Mandarino 37977d8c16a6SMark Brown static void __exit snd_soc_exit(void) 3798db2a4165SFrank Mandarino { 3799384c89e2SMark Brown #ifdef CONFIG_DEBUG_FS 38008a9dab1aSMark Brown debugfs_remove_recursive(snd_soc_debugfs_root); 3801384c89e2SMark Brown #endif 3802db2a4165SFrank Mandarino platform_driver_unregister(&soc_driver); 3803db2a4165SFrank Mandarino } 3804db2a4165SFrank Mandarino module_exit(snd_soc_exit); 3805db2a4165SFrank Mandarino 3806db2a4165SFrank Mandarino /* Module information */ 3807d331124dSLiam Girdwood MODULE_AUTHOR("Liam Girdwood, lrg@slimlogic.co.uk"); 3808db2a4165SFrank Mandarino MODULE_DESCRIPTION("ALSA SoC Core"); 3809db2a4165SFrank Mandarino MODULE_LICENSE("GPL"); 38108b45a209SKay Sievers MODULE_ALIAS("platform:soc-audio"); 3811