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 { 9500b317a4SStephen Warren int wordsize = min_bytes_needed(codec->driver->reg_cache_size) * 2; 9600b317a4SStephen Warren int regsize = codec->driver->reg_word_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 13500b317a4SStephen Warren wordsize = min_bytes_needed(codec->driver->reg_cache_size) * 2; 13600b317a4SStephen Warren regsize = codec->driver->reg_word_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); 2622624d5faSMark Brown while (*start == ' ') 2632624d5faSMark Brown start++; 2642624d5faSMark Brown if (strict_strtoul(start, 16, &value)) 2652624d5faSMark Brown return -EINVAL; 2660d51a9cbSMark Brown 2670d51a9cbSMark Brown /* Userspace has been fiddling around behind the kernel's back */ 2680d51a9cbSMark Brown add_taint(TAINT_USER); 2690d51a9cbSMark Brown 270e4f078d8SDimitris Papastamos snd_soc_write(codec, reg, value); 2712624d5faSMark Brown return buf_size; 2722624d5faSMark Brown } 2732624d5faSMark Brown 2742624d5faSMark Brown static const struct file_operations codec_reg_fops = { 2752624d5faSMark Brown .open = codec_reg_open_file, 2762624d5faSMark Brown .read = codec_reg_read_file, 2772624d5faSMark Brown .write = codec_reg_write_file, 2786038f373SArnd Bergmann .llseek = default_llseek, 2792624d5faSMark Brown }; 2802624d5faSMark Brown 2812624d5faSMark Brown static void soc_init_codec_debugfs(struct snd_soc_codec *codec) 2822624d5faSMark Brown { 283d6ce4cf3SJarkko Nikula struct dentry *debugfs_card_root = codec->card->debugfs_card_root; 284d6ce4cf3SJarkko Nikula 2856ba6c9c3SMark Brown codec->debugfs_codec_root = debugfs_create_dir(codec->name, 286d6ce4cf3SJarkko Nikula debugfs_card_root); 2872624d5faSMark Brown if (!codec->debugfs_codec_root) { 2882624d5faSMark Brown printk(KERN_WARNING 2892624d5faSMark Brown "ASoC: Failed to create codec debugfs directory\n"); 2902624d5faSMark Brown return; 2912624d5faSMark Brown } 2922624d5faSMark Brown 293aaee8ef1SMark Brown debugfs_create_bool("cache_sync", 0444, codec->debugfs_codec_root, 294aaee8ef1SMark Brown &codec->cache_sync); 295aaee8ef1SMark Brown debugfs_create_bool("cache_only", 0444, codec->debugfs_codec_root, 296aaee8ef1SMark Brown &codec->cache_only); 297aaee8ef1SMark Brown 2982624d5faSMark Brown codec->debugfs_reg = debugfs_create_file("codec_reg", 0644, 2992624d5faSMark Brown codec->debugfs_codec_root, 3002624d5faSMark Brown codec, &codec_reg_fops); 3012624d5faSMark Brown if (!codec->debugfs_reg) 3022624d5faSMark Brown printk(KERN_WARNING 3032624d5faSMark Brown "ASoC: Failed to create codec register debugfs file\n"); 3042624d5faSMark Brown 3058eecaf62SLars-Peter Clausen snd_soc_dapm_debugfs_init(&codec->dapm, codec->debugfs_codec_root); 3062624d5faSMark Brown } 3072624d5faSMark Brown 3082624d5faSMark Brown static void soc_cleanup_codec_debugfs(struct snd_soc_codec *codec) 3092624d5faSMark Brown { 3102624d5faSMark Brown debugfs_remove_recursive(codec->debugfs_codec_root); 3112624d5faSMark Brown } 3122624d5faSMark Brown 313c3c5a19aSMark Brown static ssize_t codec_list_read_file(struct file *file, char __user *user_buf, 314c3c5a19aSMark Brown size_t count, loff_t *ppos) 315c3c5a19aSMark Brown { 316c3c5a19aSMark Brown char *buf = kmalloc(PAGE_SIZE, GFP_KERNEL); 3172b194f9dSMark Brown ssize_t len, ret = 0; 318c3c5a19aSMark Brown struct snd_soc_codec *codec; 319c3c5a19aSMark Brown 320c3c5a19aSMark Brown if (!buf) 321c3c5a19aSMark Brown return -ENOMEM; 322c3c5a19aSMark Brown 3232b194f9dSMark Brown list_for_each_entry(codec, &codec_list, list) { 3242b194f9dSMark Brown len = snprintf(buf + ret, PAGE_SIZE - ret, "%s\n", 325c3c5a19aSMark Brown codec->name); 3262b194f9dSMark Brown if (len >= 0) 3272b194f9dSMark Brown ret += len; 3282b194f9dSMark Brown if (ret > PAGE_SIZE) { 3292b194f9dSMark Brown ret = PAGE_SIZE; 3302b194f9dSMark Brown break; 3312b194f9dSMark Brown } 3322b194f9dSMark Brown } 333c3c5a19aSMark Brown 334c3c5a19aSMark Brown if (ret >= 0) 335c3c5a19aSMark Brown ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret); 336c3c5a19aSMark Brown 337c3c5a19aSMark Brown kfree(buf); 338c3c5a19aSMark Brown 339c3c5a19aSMark Brown return ret; 340c3c5a19aSMark Brown } 341c3c5a19aSMark Brown 342c3c5a19aSMark Brown static const struct file_operations codec_list_fops = { 343c3c5a19aSMark Brown .read = codec_list_read_file, 344c3c5a19aSMark Brown .llseek = default_llseek,/* read accesses f_pos */ 345c3c5a19aSMark Brown }; 346c3c5a19aSMark Brown 347f3208780SMark Brown static ssize_t dai_list_read_file(struct file *file, char __user *user_buf, 348f3208780SMark Brown size_t count, loff_t *ppos) 349f3208780SMark Brown { 350f3208780SMark Brown char *buf = kmalloc(PAGE_SIZE, GFP_KERNEL); 3512b194f9dSMark Brown ssize_t len, ret = 0; 352f3208780SMark Brown struct snd_soc_dai *dai; 353f3208780SMark Brown 354f3208780SMark Brown if (!buf) 355f3208780SMark Brown return -ENOMEM; 356f3208780SMark Brown 3572b194f9dSMark Brown list_for_each_entry(dai, &dai_list, list) { 3582b194f9dSMark Brown len = snprintf(buf + ret, PAGE_SIZE - ret, "%s\n", dai->name); 3592b194f9dSMark Brown if (len >= 0) 3602b194f9dSMark Brown ret += len; 3612b194f9dSMark Brown if (ret > PAGE_SIZE) { 3622b194f9dSMark Brown ret = PAGE_SIZE; 3632b194f9dSMark Brown break; 3642b194f9dSMark Brown } 3652b194f9dSMark Brown } 366f3208780SMark Brown 367f3208780SMark Brown ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret); 368f3208780SMark Brown 369f3208780SMark Brown kfree(buf); 370f3208780SMark Brown 371f3208780SMark Brown return ret; 372f3208780SMark Brown } 373f3208780SMark Brown 374f3208780SMark Brown static const struct file_operations dai_list_fops = { 375f3208780SMark Brown .read = dai_list_read_file, 376f3208780SMark Brown .llseek = default_llseek,/* read accesses f_pos */ 377f3208780SMark Brown }; 378f3208780SMark Brown 37919c7ac27SMark Brown static ssize_t platform_list_read_file(struct file *file, 38019c7ac27SMark Brown char __user *user_buf, 38119c7ac27SMark Brown size_t count, loff_t *ppos) 38219c7ac27SMark Brown { 38319c7ac27SMark Brown char *buf = kmalloc(PAGE_SIZE, GFP_KERNEL); 3842b194f9dSMark Brown ssize_t len, ret = 0; 38519c7ac27SMark Brown struct snd_soc_platform *platform; 38619c7ac27SMark Brown 38719c7ac27SMark Brown if (!buf) 38819c7ac27SMark Brown return -ENOMEM; 38919c7ac27SMark Brown 3902b194f9dSMark Brown list_for_each_entry(platform, &platform_list, list) { 3912b194f9dSMark Brown len = snprintf(buf + ret, PAGE_SIZE - ret, "%s\n", 39219c7ac27SMark Brown platform->name); 3932b194f9dSMark Brown if (len >= 0) 3942b194f9dSMark Brown ret += len; 3952b194f9dSMark Brown if (ret > PAGE_SIZE) { 3962b194f9dSMark Brown ret = PAGE_SIZE; 3972b194f9dSMark Brown break; 3982b194f9dSMark Brown } 3992b194f9dSMark Brown } 40019c7ac27SMark Brown 40119c7ac27SMark Brown ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret); 40219c7ac27SMark Brown 40319c7ac27SMark Brown kfree(buf); 40419c7ac27SMark Brown 40519c7ac27SMark Brown return ret; 40619c7ac27SMark Brown } 40719c7ac27SMark Brown 40819c7ac27SMark Brown static const struct file_operations platform_list_fops = { 40919c7ac27SMark Brown .read = platform_list_read_file, 41019c7ac27SMark Brown .llseek = default_llseek,/* read accesses f_pos */ 41119c7ac27SMark Brown }; 41219c7ac27SMark Brown 413a6052154SJarkko Nikula static void soc_init_card_debugfs(struct snd_soc_card *card) 414a6052154SJarkko Nikula { 415a6052154SJarkko Nikula card->debugfs_card_root = debugfs_create_dir(card->name, 4168a9dab1aSMark Brown snd_soc_debugfs_root); 4173a45b867SJarkko Nikula if (!card->debugfs_card_root) { 418a6052154SJarkko Nikula dev_warn(card->dev, 419a6052154SJarkko Nikula "ASoC: Failed to create codec debugfs directory\n"); 4203a45b867SJarkko Nikula return; 4213a45b867SJarkko Nikula } 4223a45b867SJarkko Nikula 4233a45b867SJarkko Nikula card->debugfs_pop_time = debugfs_create_u32("dapm_pop_time", 0644, 4243a45b867SJarkko Nikula card->debugfs_card_root, 4253a45b867SJarkko Nikula &card->pop_time); 4263a45b867SJarkko Nikula if (!card->debugfs_pop_time) 4273a45b867SJarkko Nikula dev_warn(card->dev, 4283a45b867SJarkko Nikula "Failed to create pop time debugfs file\n"); 429a6052154SJarkko Nikula } 430a6052154SJarkko Nikula 431a6052154SJarkko Nikula static void soc_cleanup_card_debugfs(struct snd_soc_card *card) 432a6052154SJarkko Nikula { 433a6052154SJarkko Nikula debugfs_remove_recursive(card->debugfs_card_root); 434a6052154SJarkko Nikula } 435a6052154SJarkko Nikula 4362624d5faSMark Brown #else 4372624d5faSMark Brown 4382624d5faSMark Brown static inline void soc_init_codec_debugfs(struct snd_soc_codec *codec) 4392624d5faSMark Brown { 4402624d5faSMark Brown } 4412624d5faSMark Brown 4422624d5faSMark Brown static inline void soc_cleanup_codec_debugfs(struct snd_soc_codec *codec) 4432624d5faSMark Brown { 4442624d5faSMark Brown } 445b95fccbcSAxel Lin 446b95fccbcSAxel Lin static inline void soc_init_card_debugfs(struct snd_soc_card *card) 447b95fccbcSAxel Lin { 448b95fccbcSAxel Lin } 449b95fccbcSAxel Lin 450b95fccbcSAxel Lin static inline void soc_cleanup_card_debugfs(struct snd_soc_card *card) 451b95fccbcSAxel Lin { 452b95fccbcSAxel Lin } 4532624d5faSMark Brown #endif 4542624d5faSMark Brown 455db2a4165SFrank Mandarino #ifdef CONFIG_SND_SOC_AC97_BUS 456db2a4165SFrank Mandarino /* unregister ac97 codec */ 457db2a4165SFrank Mandarino static int soc_ac97_dev_unregister(struct snd_soc_codec *codec) 458db2a4165SFrank Mandarino { 459db2a4165SFrank Mandarino if (codec->ac97->dev.bus) 460db2a4165SFrank Mandarino device_unregister(&codec->ac97->dev); 461db2a4165SFrank Mandarino return 0; 462db2a4165SFrank Mandarino } 463db2a4165SFrank Mandarino 464db2a4165SFrank Mandarino /* stop no dev release warning */ 465db2a4165SFrank Mandarino static void soc_ac97_device_release(struct device *dev){} 466db2a4165SFrank Mandarino 467db2a4165SFrank Mandarino /* register ac97 codec to bus */ 468db2a4165SFrank Mandarino static int soc_ac97_dev_register(struct snd_soc_codec *codec) 469db2a4165SFrank Mandarino { 470db2a4165SFrank Mandarino int err; 471db2a4165SFrank Mandarino 472db2a4165SFrank Mandarino codec->ac97->dev.bus = &ac97_bus_type; 4734ac5c61fSMark Brown codec->ac97->dev.parent = codec->card->dev; 474db2a4165SFrank Mandarino codec->ac97->dev.release = soc_ac97_device_release; 475db2a4165SFrank Mandarino 476bb072bf0SKay Sievers dev_set_name(&codec->ac97->dev, "%d-%d:%s", 477f0fba2adSLiam Girdwood codec->card->snd_card->number, 0, codec->name); 478db2a4165SFrank Mandarino err = device_register(&codec->ac97->dev); 479db2a4165SFrank Mandarino if (err < 0) { 480db2a4165SFrank Mandarino snd_printk(KERN_ERR "Can't register ac97 bus\n"); 481db2a4165SFrank Mandarino codec->ac97->dev.bus = NULL; 482db2a4165SFrank Mandarino return err; 483db2a4165SFrank Mandarino } 484db2a4165SFrank Mandarino return 0; 485db2a4165SFrank Mandarino } 486db2a4165SFrank Mandarino #endif 487db2a4165SFrank Mandarino 48806f409d7SMark Brown static int soc_pcm_apply_symmetry(struct snd_pcm_substream *substream) 48906f409d7SMark Brown { 49006f409d7SMark Brown struct snd_soc_pcm_runtime *rtd = substream->private_data; 491f0fba2adSLiam Girdwood struct snd_soc_dai *cpu_dai = rtd->cpu_dai; 492f0fba2adSLiam Girdwood struct snd_soc_dai *codec_dai = rtd->codec_dai; 49306f409d7SMark Brown int ret; 49406f409d7SMark Brown 4954f333b20SMark Brown if (!codec_dai->driver->symmetric_rates && 4964f333b20SMark Brown !cpu_dai->driver->symmetric_rates && 4974f333b20SMark Brown !rtd->dai_link->symmetric_rates) 4984f333b20SMark Brown return 0; 4994f333b20SMark Brown 500c4ef8786SMark Brown /* This can happen if multiple streams are starting simultaneously - 501c4ef8786SMark Brown * the second can need to get its constraints before the first has 502c4ef8786SMark Brown * picked a rate. Complain and allow the application to carry on. 503c4ef8786SMark Brown */ 504c4ef8786SMark Brown if (!rtd->rate) { 505c4ef8786SMark Brown dev_warn(&rtd->dev, 506c4ef8786SMark Brown "Not enforcing symmetric_rates due to race\n"); 507c4ef8786SMark Brown return 0; 508c4ef8786SMark Brown } 509c4ef8786SMark Brown 5104f333b20SMark Brown dev_dbg(&rtd->dev, "Symmetry forces %dHz rate\n", rtd->rate); 51106f409d7SMark Brown 51206f409d7SMark Brown ret = snd_pcm_hw_constraint_minmax(substream->runtime, 51306f409d7SMark Brown SNDRV_PCM_HW_PARAM_RATE, 5144f333b20SMark Brown rtd->rate, rtd->rate); 51506f409d7SMark Brown if (ret < 0) { 516f0fba2adSLiam Girdwood dev_err(&rtd->dev, 51706f409d7SMark Brown "Unable to apply rate symmetry constraint: %d\n", ret); 51806f409d7SMark Brown return ret; 51906f409d7SMark Brown } 52006f409d7SMark Brown 52106f409d7SMark Brown return 0; 52206f409d7SMark Brown } 52306f409d7SMark Brown 524db2a4165SFrank Mandarino /* 525db2a4165SFrank Mandarino * Called by ALSA when a PCM substream is opened, the runtime->hw record is 526db2a4165SFrank Mandarino * then initialized and any private data can be allocated. This also calls 527db2a4165SFrank Mandarino * startup for the cpu DAI, platform, machine and codec DAI. 528db2a4165SFrank Mandarino */ 529db2a4165SFrank Mandarino static int soc_pcm_open(struct snd_pcm_substream *substream) 530db2a4165SFrank Mandarino { 531db2a4165SFrank Mandarino struct snd_soc_pcm_runtime *rtd = substream->private_data; 532db2a4165SFrank Mandarino struct snd_pcm_runtime *runtime = substream->runtime; 533f0fba2adSLiam Girdwood struct snd_soc_platform *platform = rtd->platform; 534f0fba2adSLiam Girdwood struct snd_soc_dai *cpu_dai = rtd->cpu_dai; 535f0fba2adSLiam Girdwood struct snd_soc_dai *codec_dai = rtd->codec_dai; 536f0fba2adSLiam Girdwood struct snd_soc_dai_driver *cpu_dai_drv = cpu_dai->driver; 537f0fba2adSLiam Girdwood struct snd_soc_dai_driver *codec_dai_drv = codec_dai->driver; 538db2a4165SFrank Mandarino int ret = 0; 539db2a4165SFrank Mandarino 540db2a4165SFrank Mandarino mutex_lock(&pcm_mutex); 541db2a4165SFrank Mandarino 542db2a4165SFrank Mandarino /* startup the audio subsystem */ 543f0fba2adSLiam Girdwood if (cpu_dai->driver->ops->startup) { 544f0fba2adSLiam Girdwood ret = cpu_dai->driver->ops->startup(substream, cpu_dai); 545db2a4165SFrank Mandarino if (ret < 0) { 546db2a4165SFrank Mandarino printk(KERN_ERR "asoc: can't open interface %s\n", 547cb666e5bSLiam Girdwood cpu_dai->name); 548db2a4165SFrank Mandarino goto out; 549db2a4165SFrank Mandarino } 550db2a4165SFrank Mandarino } 551db2a4165SFrank Mandarino 5528842c72aSMark Brown if (platform->driver->ops && platform->driver->ops->open) { 553f0fba2adSLiam Girdwood ret = platform->driver->ops->open(substream); 554db2a4165SFrank Mandarino if (ret < 0) { 555db2a4165SFrank Mandarino printk(KERN_ERR "asoc: can't open platform %s\n", platform->name); 556db2a4165SFrank Mandarino goto platform_err; 557db2a4165SFrank Mandarino } 558db2a4165SFrank Mandarino } 559db2a4165SFrank Mandarino 560f0fba2adSLiam Girdwood if (codec_dai->driver->ops->startup) { 561f0fba2adSLiam Girdwood ret = codec_dai->driver->ops->startup(substream, codec_dai); 562cb666e5bSLiam Girdwood if (ret < 0) { 563cb666e5bSLiam Girdwood printk(KERN_ERR "asoc: can't open codec %s\n", 564cb666e5bSLiam Girdwood codec_dai->name); 565cb666e5bSLiam Girdwood goto codec_dai_err; 566cb666e5bSLiam Girdwood } 567cb666e5bSLiam Girdwood } 568cb666e5bSLiam Girdwood 569f0fba2adSLiam Girdwood if (rtd->dai_link->ops && rtd->dai_link->ops->startup) { 570f0fba2adSLiam Girdwood ret = rtd->dai_link->ops->startup(substream); 571db2a4165SFrank Mandarino if (ret < 0) { 572f0fba2adSLiam Girdwood printk(KERN_ERR "asoc: %s startup failed\n", rtd->dai_link->name); 573db2a4165SFrank Mandarino goto machine_err; 574db2a4165SFrank Mandarino } 575db2a4165SFrank Mandarino } 576db2a4165SFrank Mandarino 577a00f90f9SMark Brown /* Check that the codec and cpu DAIs are compatible */ 578db2a4165SFrank Mandarino if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) { 579db2a4165SFrank Mandarino runtime->hw.rate_min = 580f0fba2adSLiam Girdwood max(codec_dai_drv->playback.rate_min, 581f0fba2adSLiam Girdwood cpu_dai_drv->playback.rate_min); 582db2a4165SFrank Mandarino runtime->hw.rate_max = 583f0fba2adSLiam Girdwood min(codec_dai_drv->playback.rate_max, 584f0fba2adSLiam Girdwood cpu_dai_drv->playback.rate_max); 585db2a4165SFrank Mandarino runtime->hw.channels_min = 586f0fba2adSLiam Girdwood max(codec_dai_drv->playback.channels_min, 587f0fba2adSLiam Girdwood cpu_dai_drv->playback.channels_min); 588db2a4165SFrank Mandarino runtime->hw.channels_max = 589f0fba2adSLiam Girdwood min(codec_dai_drv->playback.channels_max, 590f0fba2adSLiam Girdwood cpu_dai_drv->playback.channels_max); 591cb666e5bSLiam Girdwood runtime->hw.formats = 592f0fba2adSLiam Girdwood codec_dai_drv->playback.formats & cpu_dai_drv->playback.formats; 593cb666e5bSLiam Girdwood runtime->hw.rates = 594f0fba2adSLiam Girdwood codec_dai_drv->playback.rates & cpu_dai_drv->playback.rates; 595f0fba2adSLiam Girdwood if (codec_dai_drv->playback.rates 596d9ad6296SJassi Brar & (SNDRV_PCM_RATE_KNOT | SNDRV_PCM_RATE_CONTINUOUS)) 597f0fba2adSLiam Girdwood runtime->hw.rates |= cpu_dai_drv->playback.rates; 598f0fba2adSLiam Girdwood if (cpu_dai_drv->playback.rates 599d9ad6296SJassi Brar & (SNDRV_PCM_RATE_KNOT | SNDRV_PCM_RATE_CONTINUOUS)) 600f0fba2adSLiam Girdwood runtime->hw.rates |= codec_dai_drv->playback.rates; 601db2a4165SFrank Mandarino } else { 602db2a4165SFrank Mandarino runtime->hw.rate_min = 603f0fba2adSLiam Girdwood max(codec_dai_drv->capture.rate_min, 604f0fba2adSLiam Girdwood cpu_dai_drv->capture.rate_min); 605db2a4165SFrank Mandarino runtime->hw.rate_max = 606f0fba2adSLiam Girdwood min(codec_dai_drv->capture.rate_max, 607f0fba2adSLiam Girdwood cpu_dai_drv->capture.rate_max); 608db2a4165SFrank Mandarino runtime->hw.channels_min = 609f0fba2adSLiam Girdwood max(codec_dai_drv->capture.channels_min, 610f0fba2adSLiam Girdwood cpu_dai_drv->capture.channels_min); 611db2a4165SFrank Mandarino runtime->hw.channels_max = 612f0fba2adSLiam Girdwood min(codec_dai_drv->capture.channels_max, 613f0fba2adSLiam Girdwood cpu_dai_drv->capture.channels_max); 614cb666e5bSLiam Girdwood runtime->hw.formats = 615f0fba2adSLiam Girdwood codec_dai_drv->capture.formats & cpu_dai_drv->capture.formats; 616cb666e5bSLiam Girdwood runtime->hw.rates = 617f0fba2adSLiam Girdwood codec_dai_drv->capture.rates & cpu_dai_drv->capture.rates; 618f0fba2adSLiam Girdwood if (codec_dai_drv->capture.rates 619d9ad6296SJassi Brar & (SNDRV_PCM_RATE_KNOT | SNDRV_PCM_RATE_CONTINUOUS)) 620f0fba2adSLiam Girdwood runtime->hw.rates |= cpu_dai_drv->capture.rates; 621f0fba2adSLiam Girdwood if (cpu_dai_drv->capture.rates 622d9ad6296SJassi Brar & (SNDRV_PCM_RATE_KNOT | SNDRV_PCM_RATE_CONTINUOUS)) 623f0fba2adSLiam Girdwood runtime->hw.rates |= codec_dai_drv->capture.rates; 624db2a4165SFrank Mandarino } 625db2a4165SFrank Mandarino 626c51def65SLu Guanqun ret = -EINVAL; 627db2a4165SFrank Mandarino snd_pcm_limit_hw_rates(runtime); 628db2a4165SFrank Mandarino if (!runtime->hw.rates) { 629db2a4165SFrank Mandarino printk(KERN_ERR "asoc: %s <-> %s No matching rates\n", 630cb666e5bSLiam Girdwood codec_dai->name, cpu_dai->name); 631bb1c0478SJassi Brar goto config_err; 632db2a4165SFrank Mandarino } 633db2a4165SFrank Mandarino if (!runtime->hw.formats) { 634db2a4165SFrank Mandarino printk(KERN_ERR "asoc: %s <-> %s No matching formats\n", 635cb666e5bSLiam Girdwood codec_dai->name, cpu_dai->name); 636bb1c0478SJassi Brar goto config_err; 637db2a4165SFrank Mandarino } 638b04cfcf7SLu Guanqun if (!runtime->hw.channels_min || !runtime->hw.channels_max || 639b04cfcf7SLu Guanqun runtime->hw.channels_min > runtime->hw.channels_max) { 640db2a4165SFrank Mandarino printk(KERN_ERR "asoc: %s <-> %s No matching channels\n", 641cb666e5bSLiam Girdwood codec_dai->name, cpu_dai->name); 642bb1c0478SJassi Brar goto config_err; 643db2a4165SFrank Mandarino } 644db2a4165SFrank Mandarino 64506f409d7SMark Brown /* Symmetry only applies if we've already got an active stream. */ 64606f409d7SMark Brown if (cpu_dai->active || codec_dai->active) { 64706f409d7SMark Brown ret = soc_pcm_apply_symmetry(substream); 64806f409d7SMark Brown if (ret != 0) 649bb1c0478SJassi Brar goto config_err; 65006f409d7SMark Brown } 65106f409d7SMark Brown 652f0fba2adSLiam Girdwood pr_debug("asoc: %s <-> %s info:\n", 653f0fba2adSLiam Girdwood codec_dai->name, cpu_dai->name); 654f24368c2SMark Brown pr_debug("asoc: rate mask 0x%x\n", runtime->hw.rates); 655f24368c2SMark Brown pr_debug("asoc: min ch %d max ch %d\n", runtime->hw.channels_min, 656b5c5fd24SLiam Girdwood runtime->hw.channels_max); 657f24368c2SMark Brown pr_debug("asoc: min rate %d max rate %d\n", runtime->hw.rate_min, 658b5c5fd24SLiam Girdwood runtime->hw.rate_max); 659db2a4165SFrank Mandarino 66014dc5734SJassi Brar if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) { 661f0fba2adSLiam Girdwood cpu_dai->playback_active++; 662f0fba2adSLiam Girdwood codec_dai->playback_active++; 66314dc5734SJassi Brar } else { 664f0fba2adSLiam Girdwood cpu_dai->capture_active++; 665f0fba2adSLiam Girdwood codec_dai->capture_active++; 66614dc5734SJassi Brar } 66714dc5734SJassi Brar cpu_dai->active++; 66814dc5734SJassi Brar codec_dai->active++; 669f0fba2adSLiam Girdwood rtd->codec->active++; 670db2a4165SFrank Mandarino mutex_unlock(&pcm_mutex); 671db2a4165SFrank Mandarino return 0; 672db2a4165SFrank Mandarino 673bb1c0478SJassi Brar config_err: 674f0fba2adSLiam Girdwood if (rtd->dai_link->ops && rtd->dai_link->ops->shutdown) 675f0fba2adSLiam Girdwood rtd->dai_link->ops->shutdown(substream); 676db2a4165SFrank Mandarino 677bb1c0478SJassi Brar machine_err: 678f0fba2adSLiam Girdwood if (codec_dai->driver->ops->shutdown) 679f0fba2adSLiam Girdwood codec_dai->driver->ops->shutdown(substream, codec_dai); 680bb1c0478SJassi Brar 681cb666e5bSLiam Girdwood codec_dai_err: 6828842c72aSMark Brown if (platform->driver->ops && platform->driver->ops->close) 683f0fba2adSLiam Girdwood platform->driver->ops->close(substream); 684db2a4165SFrank Mandarino 685db2a4165SFrank Mandarino platform_err: 686f0fba2adSLiam Girdwood if (cpu_dai->driver->ops->shutdown) 687f0fba2adSLiam Girdwood cpu_dai->driver->ops->shutdown(substream, cpu_dai); 688db2a4165SFrank Mandarino out: 689db2a4165SFrank Mandarino mutex_unlock(&pcm_mutex); 690db2a4165SFrank Mandarino return ret; 691db2a4165SFrank Mandarino } 692db2a4165SFrank Mandarino 693db2a4165SFrank Mandarino /* 6943a4fa0a2SRobert P. J. Day * Power down the audio subsystem pmdown_time msecs after close is called. 695db2a4165SFrank Mandarino * This is to ensure there are no pops or clicks in between any music tracks 696db2a4165SFrank Mandarino * due to DAPM power cycling. 697db2a4165SFrank Mandarino */ 6984484bb2eSAndrew Morton static void close_delayed_work(struct work_struct *work) 699db2a4165SFrank Mandarino { 700f0fba2adSLiam Girdwood struct snd_soc_pcm_runtime *rtd = 701f0fba2adSLiam Girdwood container_of(work, struct snd_soc_pcm_runtime, delayed_work.work); 702f0fba2adSLiam Girdwood struct snd_soc_dai *codec_dai = rtd->codec_dai; 703db2a4165SFrank Mandarino 704db2a4165SFrank Mandarino mutex_lock(&pcm_mutex); 705db2a4165SFrank Mandarino 706f24368c2SMark Brown pr_debug("pop wq checking: %s status: %s waiting: %s\n", 707f0fba2adSLiam Girdwood codec_dai->driver->playback.stream_name, 708f0fba2adSLiam Girdwood codec_dai->playback_active ? "active" : "inactive", 709db2a4165SFrank Mandarino codec_dai->pop_wait ? "yes" : "no"); 710db2a4165SFrank Mandarino 711db2a4165SFrank Mandarino /* are we waiting on this codec DAI stream */ 712db2a4165SFrank Mandarino if (codec_dai->pop_wait == 1) { 713db2a4165SFrank Mandarino codec_dai->pop_wait = 0; 714f0fba2adSLiam Girdwood snd_soc_dapm_stream_event(rtd, 715f0fba2adSLiam Girdwood codec_dai->driver->playback.stream_name, 716db2a4165SFrank Mandarino SND_SOC_DAPM_STREAM_STOP); 717db2a4165SFrank Mandarino } 718f0fba2adSLiam Girdwood 719db2a4165SFrank Mandarino mutex_unlock(&pcm_mutex); 720db2a4165SFrank Mandarino } 721db2a4165SFrank Mandarino 722db2a4165SFrank Mandarino /* 723db2a4165SFrank Mandarino * Called by ALSA when a PCM substream is closed. Private data can be 724db2a4165SFrank Mandarino * freed here. The cpu DAI, codec DAI, machine and platform are also 725db2a4165SFrank Mandarino * shutdown. 726db2a4165SFrank Mandarino */ 727db2a4165SFrank Mandarino static int soc_codec_close(struct snd_pcm_substream *substream) 728db2a4165SFrank Mandarino { 729db2a4165SFrank Mandarino struct snd_soc_pcm_runtime *rtd = substream->private_data; 730f0fba2adSLiam Girdwood struct snd_soc_platform *platform = rtd->platform; 731f0fba2adSLiam Girdwood struct snd_soc_dai *cpu_dai = rtd->cpu_dai; 732f0fba2adSLiam Girdwood struct snd_soc_dai *codec_dai = rtd->codec_dai; 733f0fba2adSLiam Girdwood struct snd_soc_codec *codec = rtd->codec; 734db2a4165SFrank Mandarino 735db2a4165SFrank Mandarino mutex_lock(&pcm_mutex); 736db2a4165SFrank Mandarino 73714dc5734SJassi Brar if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) { 738f0fba2adSLiam Girdwood cpu_dai->playback_active--; 739f0fba2adSLiam Girdwood codec_dai->playback_active--; 74014dc5734SJassi Brar } else { 741f0fba2adSLiam Girdwood cpu_dai->capture_active--; 742f0fba2adSLiam Girdwood codec_dai->capture_active--; 743db2a4165SFrank Mandarino } 74414dc5734SJassi Brar 74514dc5734SJassi Brar cpu_dai->active--; 74614dc5734SJassi Brar codec_dai->active--; 747db2a4165SFrank Mandarino codec->active--; 748db2a4165SFrank Mandarino 7496010b2daSMark Brown /* Muting the DAC suppresses artifacts caused during digital 7506010b2daSMark Brown * shutdown, for example from stopping clocks. 7516010b2daSMark Brown */ 7526010b2daSMark Brown if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) 7536010b2daSMark Brown snd_soc_dai_digital_mute(codec_dai, 1); 7546010b2daSMark Brown 755f0fba2adSLiam Girdwood if (cpu_dai->driver->ops->shutdown) 756f0fba2adSLiam Girdwood cpu_dai->driver->ops->shutdown(substream, cpu_dai); 757db2a4165SFrank Mandarino 758f0fba2adSLiam Girdwood if (codec_dai->driver->ops->shutdown) 759f0fba2adSLiam Girdwood codec_dai->driver->ops->shutdown(substream, codec_dai); 760db2a4165SFrank Mandarino 761f0fba2adSLiam Girdwood if (rtd->dai_link->ops && rtd->dai_link->ops->shutdown) 762f0fba2adSLiam Girdwood rtd->dai_link->ops->shutdown(substream); 763db2a4165SFrank Mandarino 7648842c72aSMark Brown if (platform->driver->ops && platform->driver->ops->close) 765f0fba2adSLiam Girdwood platform->driver->ops->close(substream); 766f0fba2adSLiam Girdwood cpu_dai->runtime = NULL; 767db2a4165SFrank Mandarino 768db2a4165SFrank Mandarino if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) { 769db2a4165SFrank Mandarino /* start delayed pop wq here for playback streams */ 770cb666e5bSLiam Girdwood codec_dai->pop_wait = 1; 771f0fba2adSLiam Girdwood schedule_delayed_work(&rtd->delayed_work, 772f0fba2adSLiam Girdwood msecs_to_jiffies(rtd->pmdown_time)); 773db2a4165SFrank Mandarino } else { 774db2a4165SFrank Mandarino /* capture streams can be powered down now */ 775f0fba2adSLiam Girdwood snd_soc_dapm_stream_event(rtd, 776f0fba2adSLiam Girdwood codec_dai->driver->capture.stream_name, 7770b4d221bSLiam Girdwood SND_SOC_DAPM_STREAM_STOP); 778db2a4165SFrank Mandarino } 779db2a4165SFrank Mandarino 780db2a4165SFrank Mandarino mutex_unlock(&pcm_mutex); 781db2a4165SFrank Mandarino return 0; 782db2a4165SFrank Mandarino } 783db2a4165SFrank Mandarino 784db2a4165SFrank Mandarino /* 785db2a4165SFrank Mandarino * Called by ALSA when the PCM substream is prepared, can set format, sample 786db2a4165SFrank Mandarino * rate, etc. This function is non atomic and can be called multiple times, 787db2a4165SFrank Mandarino * it can refer to the runtime info. 788db2a4165SFrank Mandarino */ 789db2a4165SFrank Mandarino static int soc_pcm_prepare(struct snd_pcm_substream *substream) 790db2a4165SFrank Mandarino { 791db2a4165SFrank Mandarino struct snd_soc_pcm_runtime *rtd = substream->private_data; 792f0fba2adSLiam Girdwood struct snd_soc_platform *platform = rtd->platform; 793f0fba2adSLiam Girdwood struct snd_soc_dai *cpu_dai = rtd->cpu_dai; 794f0fba2adSLiam Girdwood struct snd_soc_dai *codec_dai = rtd->codec_dai; 795db2a4165SFrank Mandarino int ret = 0; 796db2a4165SFrank Mandarino 797db2a4165SFrank Mandarino mutex_lock(&pcm_mutex); 798cb666e5bSLiam Girdwood 799f0fba2adSLiam Girdwood if (rtd->dai_link->ops && rtd->dai_link->ops->prepare) { 800f0fba2adSLiam Girdwood ret = rtd->dai_link->ops->prepare(substream); 801cb666e5bSLiam Girdwood if (ret < 0) { 802cb666e5bSLiam Girdwood printk(KERN_ERR "asoc: machine prepare error\n"); 803cb666e5bSLiam Girdwood goto out; 804cb666e5bSLiam Girdwood } 805cb666e5bSLiam Girdwood } 806cb666e5bSLiam Girdwood 8078842c72aSMark Brown if (platform->driver->ops && platform->driver->ops->prepare) { 808f0fba2adSLiam Girdwood ret = platform->driver->ops->prepare(substream); 809a71a468aSLiam Girdwood if (ret < 0) { 810a71a468aSLiam Girdwood printk(KERN_ERR "asoc: platform prepare error\n"); 811db2a4165SFrank Mandarino goto out; 812db2a4165SFrank Mandarino } 813a71a468aSLiam Girdwood } 814db2a4165SFrank Mandarino 815f0fba2adSLiam Girdwood if (codec_dai->driver->ops->prepare) { 816f0fba2adSLiam Girdwood ret = codec_dai->driver->ops->prepare(substream, codec_dai); 817a71a468aSLiam Girdwood if (ret < 0) { 818a71a468aSLiam Girdwood printk(KERN_ERR "asoc: codec DAI prepare error\n"); 819db2a4165SFrank Mandarino goto out; 820db2a4165SFrank Mandarino } 821a71a468aSLiam Girdwood } 822db2a4165SFrank Mandarino 823f0fba2adSLiam Girdwood if (cpu_dai->driver->ops->prepare) { 824f0fba2adSLiam Girdwood ret = cpu_dai->driver->ops->prepare(substream, cpu_dai); 825cb666e5bSLiam Girdwood if (ret < 0) { 826cb666e5bSLiam Girdwood printk(KERN_ERR "asoc: cpu DAI prepare error\n"); 827cb666e5bSLiam Girdwood goto out; 828cb666e5bSLiam Girdwood } 829cb666e5bSLiam Girdwood } 830db2a4165SFrank Mandarino 831d45f6219SMark Brown /* cancel any delayed stream shutdown that is pending */ 832d45f6219SMark Brown if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK && 833d45f6219SMark Brown codec_dai->pop_wait) { 834cb666e5bSLiam Girdwood codec_dai->pop_wait = 0; 835f0fba2adSLiam Girdwood cancel_delayed_work(&rtd->delayed_work); 836db2a4165SFrank Mandarino } 837db2a4165SFrank Mandarino 838db2a4165SFrank Mandarino if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) 839f0fba2adSLiam Girdwood snd_soc_dapm_stream_event(rtd, 840f0fba2adSLiam Girdwood codec_dai->driver->playback.stream_name, 841db2a4165SFrank Mandarino SND_SOC_DAPM_STREAM_START); 842db2a4165SFrank Mandarino else 843f0fba2adSLiam Girdwood snd_soc_dapm_stream_event(rtd, 844f0fba2adSLiam Girdwood codec_dai->driver->capture.stream_name, 845db2a4165SFrank Mandarino SND_SOC_DAPM_STREAM_START); 8468c6529dbSLiam Girdwood 8478c6529dbSLiam Girdwood snd_soc_dai_digital_mute(codec_dai, 0); 848db2a4165SFrank Mandarino 849db2a4165SFrank Mandarino out: 850db2a4165SFrank Mandarino mutex_unlock(&pcm_mutex); 851db2a4165SFrank Mandarino return ret; 852db2a4165SFrank Mandarino } 853db2a4165SFrank Mandarino 854db2a4165SFrank Mandarino /* 855db2a4165SFrank Mandarino * Called by ALSA when the hardware params are set by application. This 856db2a4165SFrank Mandarino * function can also be called multiple times and can allocate buffers 857db2a4165SFrank Mandarino * (using snd_pcm_lib_* ). It's non-atomic. 858db2a4165SFrank Mandarino */ 859db2a4165SFrank Mandarino static int soc_pcm_hw_params(struct snd_pcm_substream *substream, 860db2a4165SFrank Mandarino struct snd_pcm_hw_params *params) 861db2a4165SFrank Mandarino { 862db2a4165SFrank Mandarino struct snd_soc_pcm_runtime *rtd = substream->private_data; 863f0fba2adSLiam Girdwood struct snd_soc_platform *platform = rtd->platform; 864f0fba2adSLiam Girdwood struct snd_soc_dai *cpu_dai = rtd->cpu_dai; 865f0fba2adSLiam Girdwood struct snd_soc_dai *codec_dai = rtd->codec_dai; 866db2a4165SFrank Mandarino int ret = 0; 867db2a4165SFrank Mandarino 868db2a4165SFrank Mandarino mutex_lock(&pcm_mutex); 869db2a4165SFrank Mandarino 870f0fba2adSLiam Girdwood if (rtd->dai_link->ops && rtd->dai_link->ops->hw_params) { 871f0fba2adSLiam Girdwood ret = rtd->dai_link->ops->hw_params(substream, params); 872cb666e5bSLiam Girdwood if (ret < 0) { 873cb666e5bSLiam Girdwood printk(KERN_ERR "asoc: machine hw_params failed\n"); 874db2a4165SFrank Mandarino goto out; 875db2a4165SFrank Mandarino } 876cb666e5bSLiam Girdwood } 877db2a4165SFrank Mandarino 878f0fba2adSLiam Girdwood if (codec_dai->driver->ops->hw_params) { 879f0fba2adSLiam Girdwood ret = codec_dai->driver->ops->hw_params(substream, params, codec_dai); 880db2a4165SFrank Mandarino if (ret < 0) { 881db2a4165SFrank Mandarino printk(KERN_ERR "asoc: can't set codec %s hw params\n", 882cb666e5bSLiam Girdwood codec_dai->name); 883cb666e5bSLiam Girdwood goto codec_err; 884db2a4165SFrank Mandarino } 885db2a4165SFrank Mandarino } 886db2a4165SFrank Mandarino 887f0fba2adSLiam Girdwood if (cpu_dai->driver->ops->hw_params) { 888f0fba2adSLiam Girdwood ret = cpu_dai->driver->ops->hw_params(substream, params, cpu_dai); 889db2a4165SFrank Mandarino if (ret < 0) { 8903ff3f64bSMark Brown printk(KERN_ERR "asoc: interface %s hw params failed\n", 891cb666e5bSLiam Girdwood cpu_dai->name); 892db2a4165SFrank Mandarino goto interface_err; 893db2a4165SFrank Mandarino } 894db2a4165SFrank Mandarino } 895db2a4165SFrank Mandarino 8968842c72aSMark Brown if (platform->driver->ops && platform->driver->ops->hw_params) { 897f0fba2adSLiam Girdwood ret = platform->driver->ops->hw_params(substream, params); 898db2a4165SFrank Mandarino if (ret < 0) { 8993ff3f64bSMark Brown printk(KERN_ERR "asoc: platform %s hw params failed\n", 900db2a4165SFrank Mandarino platform->name); 901db2a4165SFrank Mandarino goto platform_err; 902db2a4165SFrank Mandarino } 903db2a4165SFrank Mandarino } 904db2a4165SFrank Mandarino 905f0fba2adSLiam Girdwood rtd->rate = params_rate(params); 90606f409d7SMark Brown 907db2a4165SFrank Mandarino out: 908db2a4165SFrank Mandarino mutex_unlock(&pcm_mutex); 909db2a4165SFrank Mandarino return ret; 910db2a4165SFrank Mandarino 911db2a4165SFrank Mandarino platform_err: 912f0fba2adSLiam Girdwood if (cpu_dai->driver->ops->hw_free) 913f0fba2adSLiam Girdwood cpu_dai->driver->ops->hw_free(substream, cpu_dai); 914db2a4165SFrank Mandarino 915db2a4165SFrank Mandarino interface_err: 916f0fba2adSLiam Girdwood if (codec_dai->driver->ops->hw_free) 917f0fba2adSLiam Girdwood codec_dai->driver->ops->hw_free(substream, codec_dai); 918cb666e5bSLiam Girdwood 919cb666e5bSLiam Girdwood codec_err: 920f0fba2adSLiam Girdwood if (rtd->dai_link->ops && rtd->dai_link->ops->hw_free) 921f0fba2adSLiam Girdwood rtd->dai_link->ops->hw_free(substream); 922db2a4165SFrank Mandarino 923db2a4165SFrank Mandarino mutex_unlock(&pcm_mutex); 924db2a4165SFrank Mandarino return ret; 925db2a4165SFrank Mandarino } 926db2a4165SFrank Mandarino 927db2a4165SFrank Mandarino /* 928a00f90f9SMark Brown * Frees resources allocated by hw_params, can be called multiple times 929db2a4165SFrank Mandarino */ 930db2a4165SFrank Mandarino static int soc_pcm_hw_free(struct snd_pcm_substream *substream) 931db2a4165SFrank Mandarino { 932db2a4165SFrank Mandarino struct snd_soc_pcm_runtime *rtd = substream->private_data; 933f0fba2adSLiam Girdwood struct snd_soc_platform *platform = rtd->platform; 934f0fba2adSLiam Girdwood struct snd_soc_dai *cpu_dai = rtd->cpu_dai; 935f0fba2adSLiam Girdwood struct snd_soc_dai *codec_dai = rtd->codec_dai; 936f0fba2adSLiam Girdwood struct snd_soc_codec *codec = rtd->codec; 937db2a4165SFrank Mandarino 938db2a4165SFrank Mandarino mutex_lock(&pcm_mutex); 939db2a4165SFrank Mandarino 940db2a4165SFrank Mandarino /* apply codec digital mute */ 9418c6529dbSLiam Girdwood if (!codec->active) 9428c6529dbSLiam Girdwood snd_soc_dai_digital_mute(codec_dai, 1); 943db2a4165SFrank Mandarino 944db2a4165SFrank Mandarino /* free any machine hw params */ 945f0fba2adSLiam Girdwood if (rtd->dai_link->ops && rtd->dai_link->ops->hw_free) 946f0fba2adSLiam Girdwood rtd->dai_link->ops->hw_free(substream); 947db2a4165SFrank Mandarino 948db2a4165SFrank Mandarino /* free any DMA resources */ 9498842c72aSMark Brown if (platform->driver->ops && platform->driver->ops->hw_free) 950f0fba2adSLiam Girdwood platform->driver->ops->hw_free(substream); 951db2a4165SFrank Mandarino 952a00f90f9SMark Brown /* now free hw params for the DAIs */ 953f0fba2adSLiam Girdwood if (codec_dai->driver->ops->hw_free) 954f0fba2adSLiam Girdwood codec_dai->driver->ops->hw_free(substream, codec_dai); 955db2a4165SFrank Mandarino 956f0fba2adSLiam Girdwood if (cpu_dai->driver->ops->hw_free) 957f0fba2adSLiam Girdwood cpu_dai->driver->ops->hw_free(substream, cpu_dai); 958db2a4165SFrank Mandarino 959db2a4165SFrank Mandarino mutex_unlock(&pcm_mutex); 960db2a4165SFrank Mandarino return 0; 961db2a4165SFrank Mandarino } 962db2a4165SFrank Mandarino 963db2a4165SFrank Mandarino static int soc_pcm_trigger(struct snd_pcm_substream *substream, int cmd) 964db2a4165SFrank Mandarino { 965db2a4165SFrank Mandarino struct snd_soc_pcm_runtime *rtd = substream->private_data; 966f0fba2adSLiam Girdwood struct snd_soc_platform *platform = rtd->platform; 967f0fba2adSLiam Girdwood struct snd_soc_dai *cpu_dai = rtd->cpu_dai; 968f0fba2adSLiam Girdwood struct snd_soc_dai *codec_dai = rtd->codec_dai; 969db2a4165SFrank Mandarino int ret; 970db2a4165SFrank Mandarino 971f0fba2adSLiam Girdwood if (codec_dai->driver->ops->trigger) { 972f0fba2adSLiam Girdwood ret = codec_dai->driver->ops->trigger(substream, cmd, codec_dai); 973db2a4165SFrank Mandarino if (ret < 0) 974db2a4165SFrank Mandarino return ret; 975db2a4165SFrank Mandarino } 976db2a4165SFrank Mandarino 9778842c72aSMark Brown if (platform->driver->ops && platform->driver->ops->trigger) { 978f0fba2adSLiam Girdwood ret = platform->driver->ops->trigger(substream, cmd); 979db2a4165SFrank Mandarino if (ret < 0) 980db2a4165SFrank Mandarino return ret; 981db2a4165SFrank Mandarino } 982db2a4165SFrank Mandarino 983f0fba2adSLiam Girdwood if (cpu_dai->driver->ops->trigger) { 984f0fba2adSLiam Girdwood ret = cpu_dai->driver->ops->trigger(substream, cmd, cpu_dai); 985db2a4165SFrank Mandarino if (ret < 0) 986db2a4165SFrank Mandarino return ret; 987db2a4165SFrank Mandarino } 988db2a4165SFrank Mandarino return 0; 989db2a4165SFrank Mandarino } 990db2a4165SFrank Mandarino 991377b6f62SPeter Ujfalusi /* 992377b6f62SPeter Ujfalusi * soc level wrapper for pointer callback 993258020d0SPeter Ujfalusi * If cpu_dai, codec_dai, platform driver has the delay callback, than 994258020d0SPeter Ujfalusi * the runtime->delay will be updated accordingly. 995377b6f62SPeter Ujfalusi */ 996377b6f62SPeter Ujfalusi static snd_pcm_uframes_t soc_pcm_pointer(struct snd_pcm_substream *substream) 997377b6f62SPeter Ujfalusi { 998377b6f62SPeter Ujfalusi struct snd_soc_pcm_runtime *rtd = substream->private_data; 999f0fba2adSLiam Girdwood struct snd_soc_platform *platform = rtd->platform; 1000f0fba2adSLiam Girdwood struct snd_soc_dai *cpu_dai = rtd->cpu_dai; 1001f0fba2adSLiam Girdwood struct snd_soc_dai *codec_dai = rtd->codec_dai; 1002258020d0SPeter Ujfalusi struct snd_pcm_runtime *runtime = substream->runtime; 1003377b6f62SPeter Ujfalusi snd_pcm_uframes_t offset = 0; 1004258020d0SPeter Ujfalusi snd_pcm_sframes_t delay = 0; 1005377b6f62SPeter Ujfalusi 10068842c72aSMark Brown if (platform->driver->ops && platform->driver->ops->pointer) 1007f0fba2adSLiam Girdwood offset = platform->driver->ops->pointer(substream); 1008377b6f62SPeter Ujfalusi 1009f0fba2adSLiam Girdwood if (cpu_dai->driver->ops->delay) 1010f0fba2adSLiam Girdwood delay += cpu_dai->driver->ops->delay(substream, cpu_dai); 1011258020d0SPeter Ujfalusi 1012f0fba2adSLiam Girdwood if (codec_dai->driver->ops->delay) 1013f0fba2adSLiam Girdwood delay += codec_dai->driver->ops->delay(substream, codec_dai); 1014258020d0SPeter Ujfalusi 1015f0fba2adSLiam Girdwood if (platform->driver->delay) 1016f0fba2adSLiam Girdwood delay += platform->driver->delay(substream, codec_dai); 1017258020d0SPeter Ujfalusi 1018258020d0SPeter Ujfalusi runtime->delay = delay; 1019258020d0SPeter Ujfalusi 1020377b6f62SPeter Ujfalusi return offset; 1021377b6f62SPeter Ujfalusi } 1022377b6f62SPeter Ujfalusi 1023db2a4165SFrank Mandarino /* ASoC PCM operations */ 1024db2a4165SFrank Mandarino static struct snd_pcm_ops soc_pcm_ops = { 1025db2a4165SFrank Mandarino .open = soc_pcm_open, 1026db2a4165SFrank Mandarino .close = soc_codec_close, 1027db2a4165SFrank Mandarino .hw_params = soc_pcm_hw_params, 1028db2a4165SFrank Mandarino .hw_free = soc_pcm_hw_free, 1029db2a4165SFrank Mandarino .prepare = soc_pcm_prepare, 1030db2a4165SFrank Mandarino .trigger = soc_pcm_trigger, 1031377b6f62SPeter Ujfalusi .pointer = soc_pcm_pointer, 1032db2a4165SFrank Mandarino }; 1033db2a4165SFrank Mandarino 10346f8ab4acSMark Brown #ifdef CONFIG_PM_SLEEP 1035db2a4165SFrank Mandarino /* powers down audio subsystem for suspend */ 10366f8ab4acSMark Brown int snd_soc_suspend(struct device *dev) 1037db2a4165SFrank Mandarino { 10386f8ab4acSMark Brown struct snd_soc_card *card = dev_get_drvdata(dev); 10392eea392dSJarkko Nikula struct snd_soc_codec *codec; 1040db2a4165SFrank Mandarino int i; 1041db2a4165SFrank Mandarino 1042e3509ff0SDaniel Mack /* If the initialization of this soc device failed, there is no codec 1043e3509ff0SDaniel Mack * associated with it. Just bail out in this case. 1044e3509ff0SDaniel Mack */ 1045f0fba2adSLiam Girdwood if (list_empty(&card->codec_dev_list)) 1046e3509ff0SDaniel Mack return 0; 1047e3509ff0SDaniel Mack 10486ed25978SAndy Green /* Due to the resume being scheduled into a workqueue we could 10496ed25978SAndy Green * suspend before that's finished - wait for it to complete. 10506ed25978SAndy Green */ 1051f0fba2adSLiam Girdwood snd_power_lock(card->snd_card); 1052f0fba2adSLiam Girdwood snd_power_wait(card->snd_card, SNDRV_CTL_POWER_D0); 1053f0fba2adSLiam Girdwood snd_power_unlock(card->snd_card); 10546ed25978SAndy Green 10556ed25978SAndy Green /* we're going to block userspace touching us until resume completes */ 1056f0fba2adSLiam Girdwood snd_power_change_state(card->snd_card, SNDRV_CTL_POWER_D3hot); 10576ed25978SAndy Green 1058a00f90f9SMark Brown /* mute any active DACs */ 1059f0fba2adSLiam Girdwood for (i = 0; i < card->num_rtd; i++) { 1060f0fba2adSLiam Girdwood struct snd_soc_dai *dai = card->rtd[i].codec_dai; 1061f0fba2adSLiam Girdwood struct snd_soc_dai_driver *drv = dai->driver; 10623efab7dcSMark Brown 1063f0fba2adSLiam Girdwood if (card->rtd[i].dai_link->ignore_suspend) 10643efab7dcSMark Brown continue; 10653efab7dcSMark Brown 1066f0fba2adSLiam Girdwood if (drv->ops->digital_mute && dai->playback_active) 1067f0fba2adSLiam Girdwood drv->ops->digital_mute(dai, 1); 1068db2a4165SFrank Mandarino } 1069db2a4165SFrank Mandarino 10704ccab3e7SLiam Girdwood /* suspend all pcms */ 1071f0fba2adSLiam Girdwood for (i = 0; i < card->num_rtd; i++) { 1072f0fba2adSLiam Girdwood if (card->rtd[i].dai_link->ignore_suspend) 10733efab7dcSMark Brown continue; 10743efab7dcSMark Brown 1075f0fba2adSLiam Girdwood snd_pcm_suspend_all(card->rtd[i].pcm); 10763efab7dcSMark Brown } 10774ccab3e7SLiam Girdwood 107887506549SMark Brown if (card->suspend_pre) 107970b2ac12SMark Brown card->suspend_pre(card); 1080db2a4165SFrank Mandarino 1081f0fba2adSLiam Girdwood for (i = 0; i < card->num_rtd; i++) { 1082f0fba2adSLiam Girdwood struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai; 1083f0fba2adSLiam Girdwood struct snd_soc_platform *platform = card->rtd[i].platform; 10843efab7dcSMark Brown 1085f0fba2adSLiam Girdwood if (card->rtd[i].dai_link->ignore_suspend) 10863efab7dcSMark Brown continue; 10873efab7dcSMark Brown 1088f0fba2adSLiam Girdwood if (cpu_dai->driver->suspend && !cpu_dai->driver->ac97_control) 1089f0fba2adSLiam Girdwood cpu_dai->driver->suspend(cpu_dai); 1090f0fba2adSLiam Girdwood if (platform->driver->suspend && !platform->suspended) { 1091f0fba2adSLiam Girdwood platform->driver->suspend(cpu_dai); 1092f0fba2adSLiam Girdwood platform->suspended = 1; 1093f0fba2adSLiam Girdwood } 1094db2a4165SFrank Mandarino } 1095db2a4165SFrank Mandarino 1096db2a4165SFrank Mandarino /* close any waiting streams and save state */ 1097f0fba2adSLiam Girdwood for (i = 0; i < card->num_rtd; i++) { 10985b84ba26STejun Heo flush_delayed_work_sync(&card->rtd[i].delayed_work); 1099ce6120ccSLiam Girdwood card->rtd[i].codec->dapm.suspend_bias_level = card->rtd[i].codec->dapm.bias_level; 1100f0fba2adSLiam Girdwood } 1101db2a4165SFrank Mandarino 1102f0fba2adSLiam Girdwood for (i = 0; i < card->num_rtd; i++) { 1103f0fba2adSLiam Girdwood struct snd_soc_dai_driver *driver = card->rtd[i].codec_dai->driver; 11043efab7dcSMark Brown 1105f0fba2adSLiam Girdwood if (card->rtd[i].dai_link->ignore_suspend) 11063efab7dcSMark Brown continue; 11073efab7dcSMark Brown 1108f0fba2adSLiam Girdwood if (driver->playback.stream_name != NULL) 1109f0fba2adSLiam Girdwood snd_soc_dapm_stream_event(&card->rtd[i], driver->playback.stream_name, 1110db2a4165SFrank Mandarino SND_SOC_DAPM_STREAM_SUSPEND); 1111f0fba2adSLiam Girdwood 1112f0fba2adSLiam Girdwood if (driver->capture.stream_name != NULL) 1113f0fba2adSLiam Girdwood snd_soc_dapm_stream_event(&card->rtd[i], driver->capture.stream_name, 1114db2a4165SFrank Mandarino SND_SOC_DAPM_STREAM_SUSPEND); 1115db2a4165SFrank Mandarino } 1116db2a4165SFrank Mandarino 1117f0fba2adSLiam Girdwood /* suspend all CODECs */ 11182eea392dSJarkko Nikula list_for_each_entry(codec, &card->codec_dev_list, card_list) { 11191547aba9SMark Brown /* If there are paths active then the CODEC will be held with 11201547aba9SMark Brown * bias _ON and should not be suspended. */ 1121f0fba2adSLiam Girdwood if (!codec->suspended && codec->driver->suspend) { 1122ce6120ccSLiam Girdwood switch (codec->dapm.bias_level) { 11231547aba9SMark Brown case SND_SOC_BIAS_STANDBY: 11241547aba9SMark Brown case SND_SOC_BIAS_OFF: 1125f0fba2adSLiam Girdwood codec->driver->suspend(codec, PMSG_SUSPEND); 1126f0fba2adSLiam Girdwood codec->suspended = 1; 11271547aba9SMark Brown break; 11281547aba9SMark Brown default: 1129f0fba2adSLiam Girdwood dev_dbg(codec->dev, "CODEC is on over suspend\n"); 11301547aba9SMark Brown break; 11311547aba9SMark Brown } 11321547aba9SMark Brown } 1133f0fba2adSLiam Girdwood } 1134db2a4165SFrank Mandarino 1135f0fba2adSLiam Girdwood for (i = 0; i < card->num_rtd; i++) { 1136f0fba2adSLiam Girdwood struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai; 11373efab7dcSMark Brown 1138f0fba2adSLiam Girdwood if (card->rtd[i].dai_link->ignore_suspend) 11393efab7dcSMark Brown continue; 11403efab7dcSMark Brown 1141f0fba2adSLiam Girdwood if (cpu_dai->driver->suspend && cpu_dai->driver->ac97_control) 1142f0fba2adSLiam Girdwood cpu_dai->driver->suspend(cpu_dai); 1143db2a4165SFrank Mandarino } 1144db2a4165SFrank Mandarino 114587506549SMark Brown if (card->suspend_post) 114670b2ac12SMark Brown card->suspend_post(card); 1147db2a4165SFrank Mandarino 1148db2a4165SFrank Mandarino return 0; 1149db2a4165SFrank Mandarino } 11506f8ab4acSMark Brown EXPORT_SYMBOL_GPL(snd_soc_suspend); 1151db2a4165SFrank Mandarino 11526ed25978SAndy Green /* deferred resume work, so resume can complete before we finished 11536ed25978SAndy Green * setting our codec back up, which can be very slow on I2C 11546ed25978SAndy Green */ 11556ed25978SAndy Green static void soc_resume_deferred(struct work_struct *work) 1156db2a4165SFrank Mandarino { 1157f0fba2adSLiam Girdwood struct snd_soc_card *card = 1158f0fba2adSLiam Girdwood container_of(work, struct snd_soc_card, deferred_resume_work); 11592eea392dSJarkko Nikula struct snd_soc_codec *codec; 1160db2a4165SFrank Mandarino int i; 1161db2a4165SFrank Mandarino 11626ed25978SAndy Green /* our power state is still SNDRV_CTL_POWER_D3hot from suspend time, 11636ed25978SAndy Green * so userspace apps are blocked from touching us 11646ed25978SAndy Green */ 11656ed25978SAndy Green 1166f0fba2adSLiam Girdwood dev_dbg(card->dev, "starting resume work\n"); 11676ed25978SAndy Green 11689949788bSMark Brown /* Bring us up into D2 so that DAPM starts enabling things */ 1169f0fba2adSLiam Girdwood snd_power_change_state(card->snd_card, SNDRV_CTL_POWER_D2); 11709949788bSMark Brown 117187506549SMark Brown if (card->resume_pre) 117270b2ac12SMark Brown card->resume_pre(card); 1173db2a4165SFrank Mandarino 1174f0fba2adSLiam Girdwood /* resume AC97 DAIs */ 1175f0fba2adSLiam Girdwood for (i = 0; i < card->num_rtd; i++) { 1176f0fba2adSLiam Girdwood struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai; 11773efab7dcSMark Brown 1178f0fba2adSLiam Girdwood if (card->rtd[i].dai_link->ignore_suspend) 11793efab7dcSMark Brown continue; 11803efab7dcSMark Brown 1181f0fba2adSLiam Girdwood if (cpu_dai->driver->resume && cpu_dai->driver->ac97_control) 1182f0fba2adSLiam Girdwood cpu_dai->driver->resume(cpu_dai); 1183db2a4165SFrank Mandarino } 1184db2a4165SFrank Mandarino 11852eea392dSJarkko Nikula list_for_each_entry(codec, &card->codec_dev_list, card_list) { 11861547aba9SMark Brown /* If the CODEC was idle over suspend then it will have been 11871547aba9SMark Brown * left with bias OFF or STANDBY and suspended so we must now 11881547aba9SMark Brown * resume. Otherwise the suspend was suppressed. 11891547aba9SMark Brown */ 1190f0fba2adSLiam Girdwood if (codec->driver->resume && codec->suspended) { 1191ce6120ccSLiam Girdwood switch (codec->dapm.bias_level) { 11921547aba9SMark Brown case SND_SOC_BIAS_STANDBY: 11931547aba9SMark Brown case SND_SOC_BIAS_OFF: 1194f0fba2adSLiam Girdwood codec->driver->resume(codec); 1195f0fba2adSLiam Girdwood codec->suspended = 0; 11961547aba9SMark Brown break; 11971547aba9SMark Brown default: 1198f0fba2adSLiam Girdwood dev_dbg(codec->dev, "CODEC was on over suspend\n"); 11991547aba9SMark Brown break; 12001547aba9SMark Brown } 12011547aba9SMark Brown } 1202f0fba2adSLiam Girdwood } 1203db2a4165SFrank Mandarino 1204f0fba2adSLiam Girdwood for (i = 0; i < card->num_rtd; i++) { 1205f0fba2adSLiam Girdwood struct snd_soc_dai_driver *driver = card->rtd[i].codec_dai->driver; 12063efab7dcSMark Brown 1207f0fba2adSLiam Girdwood if (card->rtd[i].dai_link->ignore_suspend) 12083efab7dcSMark Brown continue; 12093efab7dcSMark Brown 1210f0fba2adSLiam Girdwood if (driver->playback.stream_name != NULL) 1211f0fba2adSLiam Girdwood snd_soc_dapm_stream_event(&card->rtd[i], driver->playback.stream_name, 1212db2a4165SFrank Mandarino SND_SOC_DAPM_STREAM_RESUME); 1213f0fba2adSLiam Girdwood 1214f0fba2adSLiam Girdwood if (driver->capture.stream_name != NULL) 1215f0fba2adSLiam Girdwood snd_soc_dapm_stream_event(&card->rtd[i], driver->capture.stream_name, 1216db2a4165SFrank Mandarino SND_SOC_DAPM_STREAM_RESUME); 1217db2a4165SFrank Mandarino } 1218db2a4165SFrank Mandarino 12193ff3f64bSMark Brown /* unmute any active DACs */ 1220f0fba2adSLiam Girdwood for (i = 0; i < card->num_rtd; i++) { 1221f0fba2adSLiam Girdwood struct snd_soc_dai *dai = card->rtd[i].codec_dai; 1222f0fba2adSLiam Girdwood struct snd_soc_dai_driver *drv = dai->driver; 12233efab7dcSMark Brown 1224f0fba2adSLiam Girdwood if (card->rtd[i].dai_link->ignore_suspend) 12253efab7dcSMark Brown continue; 12263efab7dcSMark Brown 1227f0fba2adSLiam Girdwood if (drv->ops->digital_mute && dai->playback_active) 1228f0fba2adSLiam Girdwood drv->ops->digital_mute(dai, 0); 1229db2a4165SFrank Mandarino } 1230db2a4165SFrank Mandarino 1231f0fba2adSLiam Girdwood for (i = 0; i < card->num_rtd; i++) { 1232f0fba2adSLiam Girdwood struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai; 1233f0fba2adSLiam Girdwood struct snd_soc_platform *platform = card->rtd[i].platform; 12343efab7dcSMark Brown 1235f0fba2adSLiam Girdwood if (card->rtd[i].dai_link->ignore_suspend) 12363efab7dcSMark Brown continue; 12373efab7dcSMark Brown 1238f0fba2adSLiam Girdwood if (cpu_dai->driver->resume && !cpu_dai->driver->ac97_control) 1239f0fba2adSLiam Girdwood cpu_dai->driver->resume(cpu_dai); 1240f0fba2adSLiam Girdwood if (platform->driver->resume && platform->suspended) { 1241f0fba2adSLiam Girdwood platform->driver->resume(cpu_dai); 1242f0fba2adSLiam Girdwood platform->suspended = 0; 1243f0fba2adSLiam Girdwood } 1244db2a4165SFrank Mandarino } 1245db2a4165SFrank Mandarino 124687506549SMark Brown if (card->resume_post) 124770b2ac12SMark Brown card->resume_post(card); 1248db2a4165SFrank Mandarino 1249f0fba2adSLiam Girdwood dev_dbg(card->dev, "resume work completed\n"); 12506ed25978SAndy Green 12516ed25978SAndy Green /* userspace can access us now we are back as we were before */ 1252f0fba2adSLiam Girdwood snd_power_change_state(card->snd_card, SNDRV_CTL_POWER_D0); 12536ed25978SAndy Green } 12546ed25978SAndy Green 12556ed25978SAndy Green /* powers up audio subsystem after a suspend */ 12566f8ab4acSMark Brown int snd_soc_resume(struct device *dev) 12576ed25978SAndy Green { 12586f8ab4acSMark Brown struct snd_soc_card *card = dev_get_drvdata(dev); 1259f0fba2adSLiam Girdwood int i; 1260b9dd94a8SPeter Ujfalusi 126164ab9baaSMark Brown /* AC97 devices might have other drivers hanging off them so 126264ab9baaSMark Brown * need to resume immediately. Other drivers don't have that 126364ab9baaSMark Brown * problem and may take a substantial amount of time to resume 126464ab9baaSMark Brown * due to I/O costs and anti-pop so handle them out of line. 126564ab9baaSMark Brown */ 1266f0fba2adSLiam Girdwood for (i = 0; i < card->num_rtd; i++) { 1267f0fba2adSLiam Girdwood struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai; 1268f0fba2adSLiam Girdwood if (cpu_dai->driver->ac97_control) { 1269f0fba2adSLiam Girdwood dev_dbg(dev, "Resuming AC97 immediately\n"); 127064ab9baaSMark Brown soc_resume_deferred(&card->deferred_resume_work); 127164ab9baaSMark Brown } else { 1272f0fba2adSLiam Girdwood dev_dbg(dev, "Scheduling resume work\n"); 12736308419aSMark Brown if (!schedule_work(&card->deferred_resume_work)) 1274f0fba2adSLiam Girdwood dev_err(dev, "resume work item may be lost\n"); 1275f0fba2adSLiam Girdwood } 127664ab9baaSMark Brown } 12776ed25978SAndy Green 1278db2a4165SFrank Mandarino return 0; 1279db2a4165SFrank Mandarino } 12806f8ab4acSMark Brown EXPORT_SYMBOL_GPL(snd_soc_resume); 1281db2a4165SFrank Mandarino #else 12826f8ab4acSMark Brown #define snd_soc_suspend NULL 12836f8ab4acSMark Brown #define snd_soc_resume NULL 1284db2a4165SFrank Mandarino #endif 1285db2a4165SFrank Mandarino 128602a06d30SBarry Song static struct snd_soc_dai_ops null_dai_ops = { 128702a06d30SBarry Song }; 128802a06d30SBarry Song 1289f0fba2adSLiam Girdwood static int soc_bind_dai_link(struct snd_soc_card *card, int num) 1290db2a4165SFrank Mandarino { 1291f0fba2adSLiam Girdwood struct snd_soc_dai_link *dai_link = &card->dai_link[num]; 1292f0fba2adSLiam Girdwood struct snd_soc_pcm_runtime *rtd = &card->rtd[num]; 1293fe3e78e0SMark Brown struct snd_soc_codec *codec; 1294435c5e25SMark Brown struct snd_soc_platform *platform; 1295f0fba2adSLiam Girdwood struct snd_soc_dai *codec_dai, *cpu_dai; 1296848dd8beSMark Brown const char *platform_name; 1297db2a4165SFrank Mandarino 1298f0fba2adSLiam Girdwood if (rtd->complete) 1299f0fba2adSLiam Girdwood return 1; 1300f0fba2adSLiam Girdwood dev_dbg(card->dev, "binding %s at idx %d\n", dai_link->name, num); 13016308419aSMark Brown 1302f0fba2adSLiam Girdwood /* do we already have the CPU DAI for this link ? */ 1303f0fba2adSLiam Girdwood if (rtd->cpu_dai) { 1304f0fba2adSLiam Girdwood goto find_codec; 1305435c5e25SMark Brown } 1306f0fba2adSLiam Girdwood /* no, then find CPU DAI from registered DAIs*/ 1307f0fba2adSLiam Girdwood list_for_each_entry(cpu_dai, &dai_list, list) { 1308f0fba2adSLiam Girdwood if (!strcmp(cpu_dai->name, dai_link->cpu_dai_name)) { 1309f0fba2adSLiam Girdwood 1310f0fba2adSLiam Girdwood if (!try_module_get(cpu_dai->dev->driver->owner)) 1311f0fba2adSLiam Girdwood return -ENODEV; 1312f0fba2adSLiam Girdwood 1313f0fba2adSLiam Girdwood rtd->cpu_dai = cpu_dai; 1314f0fba2adSLiam Girdwood goto find_codec; 1315f0fba2adSLiam Girdwood } 1316f0fba2adSLiam Girdwood } 1317f0fba2adSLiam Girdwood dev_dbg(card->dev, "CPU DAI %s not registered\n", 1318f0fba2adSLiam Girdwood dai_link->cpu_dai_name); 1319f0fba2adSLiam Girdwood 1320f0fba2adSLiam Girdwood find_codec: 1321f0fba2adSLiam Girdwood /* do we already have the CODEC for this link ? */ 1322f0fba2adSLiam Girdwood if (rtd->codec) { 1323f0fba2adSLiam Girdwood goto find_platform; 1324c5af3a2eSMark Brown } 1325c5af3a2eSMark Brown 1326f0fba2adSLiam Girdwood /* no, then find CODEC from registered CODECs*/ 1327f0fba2adSLiam Girdwood list_for_each_entry(codec, &codec_list, list) { 1328f0fba2adSLiam Girdwood if (!strcmp(codec->name, dai_link->codec_name)) { 1329f0fba2adSLiam Girdwood rtd->codec = codec; 1330f0fba2adSLiam Girdwood 1331f0fba2adSLiam Girdwood /* CODEC found, so find CODEC DAI from registered DAIs from this CODEC*/ 1332f0fba2adSLiam Girdwood list_for_each_entry(codec_dai, &dai_list, list) { 1333f0fba2adSLiam Girdwood if (codec->dev == codec_dai->dev && 1334f0fba2adSLiam Girdwood !strcmp(codec_dai->name, dai_link->codec_dai_name)) { 1335f0fba2adSLiam Girdwood rtd->codec_dai = codec_dai; 1336f0fba2adSLiam Girdwood goto find_platform; 1337435c5e25SMark Brown } 1338f0fba2adSLiam Girdwood } 1339f0fba2adSLiam Girdwood dev_dbg(card->dev, "CODEC DAI %s not registered\n", 1340f0fba2adSLiam Girdwood dai_link->codec_dai_name); 1341f0fba2adSLiam Girdwood 1342f0fba2adSLiam Girdwood goto find_platform; 1343f0fba2adSLiam Girdwood } 1344f0fba2adSLiam Girdwood } 1345f0fba2adSLiam Girdwood dev_dbg(card->dev, "CODEC %s not registered\n", 1346f0fba2adSLiam Girdwood dai_link->codec_name); 1347f0fba2adSLiam Girdwood 1348f0fba2adSLiam Girdwood find_platform: 1349848dd8beSMark Brown /* do we need a platform? */ 1350848dd8beSMark Brown if (rtd->platform) 1351f0fba2adSLiam Girdwood goto out; 1352848dd8beSMark Brown 1353848dd8beSMark Brown /* if there's no platform we match on the empty platform */ 1354848dd8beSMark Brown platform_name = dai_link->platform_name; 1355848dd8beSMark Brown if (!platform_name) 1356848dd8beSMark Brown platform_name = "snd-soc-dummy"; 1357848dd8beSMark Brown 1358848dd8beSMark Brown /* no, then find one from the set of registered platforms */ 1359f0fba2adSLiam Girdwood list_for_each_entry(platform, &platform_list, list) { 1360848dd8beSMark Brown if (!strcmp(platform->name, 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 14560671fd8eSKuninori Morimoto static void soc_remove_dai_links(struct snd_soc_card *card) 14570671fd8eSKuninori Morimoto { 14580671fd8eSKuninori Morimoto int i; 14590671fd8eSKuninori Morimoto 14600671fd8eSKuninori Morimoto for (i = 0; i < card->num_rtd; i++) 14610671fd8eSKuninori Morimoto soc_remove_dai_link(card, i); 14620671fd8eSKuninori Morimoto 14630671fd8eSKuninori Morimoto card->num_rtd = 0; 14640671fd8eSKuninori Morimoto } 14650671fd8eSKuninori Morimoto 1466ead9b919SJarkko Nikula static void soc_set_name_prefix(struct snd_soc_card *card, 1467ead9b919SJarkko Nikula struct snd_soc_codec *codec) 1468ead9b919SJarkko Nikula { 1469ead9b919SJarkko Nikula int i; 1470ead9b919SJarkko Nikula 1471ff819b83SDimitris Papastamos if (card->codec_conf == NULL) 1472ead9b919SJarkko Nikula return; 1473ead9b919SJarkko Nikula 1474ff819b83SDimitris Papastamos for (i = 0; i < card->num_configs; i++) { 1475ff819b83SDimitris Papastamos struct snd_soc_codec_conf *map = &card->codec_conf[i]; 1476ead9b919SJarkko Nikula if (map->dev_name && !strcmp(codec->name, map->dev_name)) { 1477ead9b919SJarkko Nikula codec->name_prefix = map->name_prefix; 1478ead9b919SJarkko Nikula break; 1479ead9b919SJarkko Nikula } 1480ead9b919SJarkko Nikula } 1481ead9b919SJarkko Nikula } 1482ead9b919SJarkko Nikula 1483589c3563SJarkko Nikula static int soc_probe_codec(struct snd_soc_card *card, 1484589c3563SJarkko Nikula struct snd_soc_codec *codec) 1485589c3563SJarkko Nikula { 1486589c3563SJarkko Nikula int ret = 0; 148789b95ac0SMark Brown const struct snd_soc_codec_driver *driver = codec->driver; 1488589c3563SJarkko Nikula 1489589c3563SJarkko Nikula codec->card = card; 1490589c3563SJarkko Nikula codec->dapm.card = card; 1491589c3563SJarkko Nikula soc_set_name_prefix(card, codec); 1492589c3563SJarkko Nikula 149370d29331SJarkko Nikula if (!try_module_get(codec->dev->driver->owner)) 149470d29331SJarkko Nikula return -ENODEV; 149570d29331SJarkko Nikula 1496d5d1e0beSLars-Peter Clausen soc_init_codec_debugfs(codec); 1497d5d1e0beSLars-Peter Clausen 1498*77530150SLars-Peter Clausen if (driver->dapm_widgets) 1499*77530150SLars-Peter Clausen snd_soc_dapm_new_controls(&codec->dapm, driver->dapm_widgets, 1500*77530150SLars-Peter Clausen driver->num_dapm_widgets); 1501*77530150SLars-Peter Clausen 150289b95ac0SMark Brown if (driver->probe) { 150389b95ac0SMark Brown ret = driver->probe(codec); 1504589c3563SJarkko Nikula if (ret < 0) { 1505589c3563SJarkko Nikula dev_err(codec->dev, 1506589c3563SJarkko Nikula "asoc: failed to probe CODEC %s: %d\n", 1507589c3563SJarkko Nikula codec->name, ret); 150870d29331SJarkko Nikula goto err_probe; 1509589c3563SJarkko Nikula } 1510589c3563SJarkko Nikula } 1511589c3563SJarkko Nikula 1512b7af1dafSMark Brown if (driver->controls) 1513b7af1dafSMark Brown snd_soc_add_controls(codec, driver->controls, 1514b7af1dafSMark Brown driver->num_controls); 151589b95ac0SMark Brown if (driver->dapm_routes) 151689b95ac0SMark Brown snd_soc_dapm_add_routes(&codec->dapm, driver->dapm_routes, 151789b95ac0SMark Brown driver->num_dapm_routes); 151889b95ac0SMark Brown 1519589c3563SJarkko Nikula /* mark codec as probed and add to card codec list */ 1520589c3563SJarkko Nikula codec->probed = 1; 1521589c3563SJarkko Nikula list_add(&codec->card_list, &card->codec_dev_list); 15227be31be8SJarkko Nikula list_add(&codec->dapm.list, &card->dapm_list); 1523589c3563SJarkko Nikula 152470d29331SJarkko Nikula return 0; 152570d29331SJarkko Nikula 152670d29331SJarkko Nikula err_probe: 1527d5d1e0beSLars-Peter Clausen soc_cleanup_codec_debugfs(codec); 152870d29331SJarkko Nikula module_put(codec->dev->driver->owner); 152970d29331SJarkko Nikula 1530589c3563SJarkko Nikula return ret; 1531589c3563SJarkko Nikula } 1532589c3563SJarkko Nikula 1533f0fba2adSLiam Girdwood static void rtd_release(struct device *dev) {} 1534f0fba2adSLiam Girdwood 1535589c3563SJarkko Nikula static int soc_post_component_init(struct snd_soc_card *card, 1536589c3563SJarkko Nikula struct snd_soc_codec *codec, 1537589c3563SJarkko Nikula int num, int dailess) 1538589c3563SJarkko Nikula { 1539589c3563SJarkko Nikula struct snd_soc_dai_link *dai_link = NULL; 1540589c3563SJarkko Nikula struct snd_soc_aux_dev *aux_dev = NULL; 1541589c3563SJarkko Nikula struct snd_soc_pcm_runtime *rtd; 1542589c3563SJarkko Nikula const char *temp, *name; 1543589c3563SJarkko Nikula int ret = 0; 1544589c3563SJarkko Nikula 1545589c3563SJarkko Nikula if (!dailess) { 1546589c3563SJarkko Nikula dai_link = &card->dai_link[num]; 1547589c3563SJarkko Nikula rtd = &card->rtd[num]; 1548589c3563SJarkko Nikula name = dai_link->name; 1549589c3563SJarkko Nikula } else { 1550589c3563SJarkko Nikula aux_dev = &card->aux_dev[num]; 1551589c3563SJarkko Nikula rtd = &card->rtd_aux[num]; 1552589c3563SJarkko Nikula name = aux_dev->name; 1553589c3563SJarkko Nikula } 15540962bb21SJanusz Krzysztofik rtd->card = card; 1555589c3563SJarkko Nikula 1556589c3563SJarkko Nikula /* machine controls, routes and widgets are not prefixed */ 1557589c3563SJarkko Nikula temp = codec->name_prefix; 1558589c3563SJarkko Nikula codec->name_prefix = NULL; 1559589c3563SJarkko Nikula 1560589c3563SJarkko Nikula /* do machine specific initialization */ 1561589c3563SJarkko Nikula if (!dailess && dai_link->init) 1562589c3563SJarkko Nikula ret = dai_link->init(rtd); 1563589c3563SJarkko Nikula else if (dailess && aux_dev->init) 1564589c3563SJarkko Nikula ret = aux_dev->init(&codec->dapm); 1565589c3563SJarkko Nikula if (ret < 0) { 1566589c3563SJarkko Nikula dev_err(card->dev, "asoc: failed to init %s: %d\n", name, ret); 1567589c3563SJarkko Nikula return ret; 1568589c3563SJarkko Nikula } 1569589c3563SJarkko Nikula codec->name_prefix = temp; 1570589c3563SJarkko Nikula 1571589c3563SJarkko Nikula /* Make sure all DAPM widgets are instantiated */ 1572589c3563SJarkko Nikula snd_soc_dapm_new_widgets(&codec->dapm); 1573589c3563SJarkko Nikula 1574589c3563SJarkko Nikula /* register the rtd device */ 1575589c3563SJarkko Nikula rtd->codec = codec; 1576589c3563SJarkko Nikula rtd->dev.parent = card->dev; 1577589c3563SJarkko Nikula rtd->dev.release = rtd_release; 1578589c3563SJarkko Nikula rtd->dev.init_name = name; 1579589c3563SJarkko Nikula ret = device_register(&rtd->dev); 1580589c3563SJarkko Nikula if (ret < 0) { 1581589c3563SJarkko Nikula dev_err(card->dev, 1582589c3563SJarkko Nikula "asoc: failed to register runtime device: %d\n", ret); 1583589c3563SJarkko Nikula return ret; 1584589c3563SJarkko Nikula } 1585589c3563SJarkko Nikula rtd->dev_registered = 1; 1586589c3563SJarkko Nikula 1587589c3563SJarkko Nikula /* add DAPM sysfs entries for this codec */ 1588589c3563SJarkko Nikula ret = snd_soc_dapm_sys_add(&rtd->dev); 1589589c3563SJarkko Nikula if (ret < 0) 1590589c3563SJarkko Nikula dev_err(codec->dev, 1591589c3563SJarkko Nikula "asoc: failed to add codec dapm sysfs entries: %d\n", 1592589c3563SJarkko Nikula ret); 1593589c3563SJarkko Nikula 1594589c3563SJarkko Nikula /* add codec sysfs entries */ 1595589c3563SJarkko Nikula ret = device_create_file(&rtd->dev, &dev_attr_codec_reg); 1596589c3563SJarkko Nikula if (ret < 0) 1597589c3563SJarkko Nikula dev_err(codec->dev, 1598589c3563SJarkko Nikula "asoc: failed to add codec sysfs files: %d\n", ret); 1599589c3563SJarkko Nikula 1600589c3563SJarkko Nikula return 0; 1601589c3563SJarkko Nikula } 1602589c3563SJarkko Nikula 1603f0fba2adSLiam Girdwood static int soc_probe_dai_link(struct snd_soc_card *card, int num) 1604f0fba2adSLiam Girdwood { 1605f0fba2adSLiam Girdwood struct snd_soc_dai_link *dai_link = &card->dai_link[num]; 1606f0fba2adSLiam Girdwood struct snd_soc_pcm_runtime *rtd = &card->rtd[num]; 1607f0fba2adSLiam Girdwood struct snd_soc_codec *codec = rtd->codec; 1608f0fba2adSLiam Girdwood struct snd_soc_platform *platform = rtd->platform; 1609f0fba2adSLiam Girdwood struct snd_soc_dai *codec_dai = rtd->codec_dai, *cpu_dai = rtd->cpu_dai; 1610f0fba2adSLiam Girdwood int ret; 1611f0fba2adSLiam Girdwood 1612f0fba2adSLiam Girdwood dev_dbg(card->dev, "probe %s dai link %d\n", card->name, num); 1613f0fba2adSLiam Girdwood 1614f0fba2adSLiam Girdwood /* config components */ 1615f0fba2adSLiam Girdwood codec_dai->codec = codec; 1616f0fba2adSLiam Girdwood cpu_dai->platform = platform; 1617f0fba2adSLiam Girdwood codec_dai->card = card; 1618f0fba2adSLiam Girdwood cpu_dai->card = card; 1619f0fba2adSLiam Girdwood 1620f0fba2adSLiam Girdwood /* set default power off timeout */ 1621f0fba2adSLiam Girdwood rtd->pmdown_time = pmdown_time; 1622f0fba2adSLiam Girdwood 1623f0fba2adSLiam Girdwood /* probe the cpu_dai */ 1624f0fba2adSLiam Girdwood if (!cpu_dai->probed) { 1625f0fba2adSLiam Girdwood if (cpu_dai->driver->probe) { 1626f0fba2adSLiam Girdwood ret = cpu_dai->driver->probe(cpu_dai); 1627f0fba2adSLiam Girdwood if (ret < 0) { 1628f0fba2adSLiam Girdwood printk(KERN_ERR "asoc: failed to probe CPU DAI %s\n", 1629f0fba2adSLiam Girdwood cpu_dai->name); 1630f0fba2adSLiam Girdwood return ret; 1631f0fba2adSLiam Girdwood } 1632f0fba2adSLiam Girdwood } 1633f0fba2adSLiam Girdwood cpu_dai->probed = 1; 1634f0fba2adSLiam Girdwood /* mark cpu_dai as probed and add to card cpu_dai list */ 1635f0fba2adSLiam Girdwood list_add(&cpu_dai->card_list, &card->dai_dev_list); 1636f0fba2adSLiam Girdwood } 1637f0fba2adSLiam Girdwood 1638f0fba2adSLiam Girdwood /* probe the CODEC */ 1639f0fba2adSLiam Girdwood if (!codec->probed) { 1640589c3563SJarkko Nikula ret = soc_probe_codec(card, codec); 1641589c3563SJarkko Nikula if (ret < 0) 1642f0fba2adSLiam Girdwood return ret; 1643f0fba2adSLiam Girdwood } 1644f0fba2adSLiam Girdwood 1645f0fba2adSLiam Girdwood /* probe the platform */ 1646f0fba2adSLiam Girdwood if (!platform->probed) { 164770d29331SJarkko Nikula if (!try_module_get(platform->dev->driver->owner)) 164870d29331SJarkko Nikula return -ENODEV; 164970d29331SJarkko Nikula 1650f0fba2adSLiam Girdwood if (platform->driver->probe) { 1651f0fba2adSLiam Girdwood ret = platform->driver->probe(platform); 1652f0fba2adSLiam Girdwood if (ret < 0) { 1653f0fba2adSLiam Girdwood printk(KERN_ERR "asoc: failed to probe platform %s\n", 1654f0fba2adSLiam Girdwood platform->name); 165570d29331SJarkko Nikula module_put(platform->dev->driver->owner); 1656f0fba2adSLiam Girdwood return ret; 1657f0fba2adSLiam Girdwood } 1658f0fba2adSLiam Girdwood } 1659f0fba2adSLiam Girdwood /* mark platform as probed and add to card platform list */ 1660f0fba2adSLiam Girdwood platform->probed = 1; 1661f0fba2adSLiam Girdwood list_add(&platform->card_list, &card->platform_dev_list); 1662f0fba2adSLiam Girdwood } 1663f0fba2adSLiam Girdwood 1664f0fba2adSLiam Girdwood /* probe the CODEC DAI */ 1665f0fba2adSLiam Girdwood if (!codec_dai->probed) { 1666f0fba2adSLiam Girdwood if (codec_dai->driver->probe) { 1667f0fba2adSLiam Girdwood ret = codec_dai->driver->probe(codec_dai); 1668f0fba2adSLiam Girdwood if (ret < 0) { 1669f0fba2adSLiam Girdwood printk(KERN_ERR "asoc: failed to probe CODEC DAI %s\n", 1670f0fba2adSLiam Girdwood codec_dai->name); 1671f0fba2adSLiam Girdwood return ret; 1672f0fba2adSLiam Girdwood } 1673f0fba2adSLiam Girdwood } 1674f0fba2adSLiam Girdwood 1675f0fba2adSLiam Girdwood /* mark cpu_dai as probed and add to card cpu_dai list */ 1676f0fba2adSLiam Girdwood codec_dai->probed = 1; 1677f0fba2adSLiam Girdwood list_add(&codec_dai->card_list, &card->dai_dev_list); 1678f0fba2adSLiam Girdwood } 1679f0fba2adSLiam Girdwood 1680f0fba2adSLiam Girdwood /* DAPM dai link stream work */ 1681f0fba2adSLiam Girdwood INIT_DELAYED_WORK(&rtd->delayed_work, close_delayed_work); 1682f0fba2adSLiam Girdwood 1683589c3563SJarkko Nikula ret = soc_post_component_init(card, codec, num, 0); 1684589c3563SJarkko Nikula if (ret) 1685f0fba2adSLiam Girdwood return ret; 1686f0fba2adSLiam Girdwood 1687f0fba2adSLiam Girdwood ret = device_create_file(&rtd->dev, &dev_attr_pmdown_time); 1688f0fba2adSLiam Girdwood if (ret < 0) 1689f0fba2adSLiam Girdwood printk(KERN_WARNING "asoc: failed to add pmdown_time sysfs\n"); 1690f0fba2adSLiam Girdwood 1691f0fba2adSLiam Girdwood /* create the pcm */ 1692f0fba2adSLiam Girdwood ret = soc_new_pcm(rtd, num); 1693f0fba2adSLiam Girdwood if (ret < 0) { 1694f0fba2adSLiam Girdwood printk(KERN_ERR "asoc: can't create pcm %s\n", dai_link->stream_name); 1695f0fba2adSLiam Girdwood return ret; 1696f0fba2adSLiam Girdwood } 1697f0fba2adSLiam Girdwood 1698f0fba2adSLiam Girdwood /* add platform data for AC97 devices */ 1699f0fba2adSLiam Girdwood if (rtd->codec_dai->driver->ac97_control) 1700f0fba2adSLiam Girdwood snd_ac97_dev_add_pdata(codec->ac97, rtd->cpu_dai->ac97_pdata); 1701f0fba2adSLiam Girdwood 1702f0fba2adSLiam Girdwood return 0; 1703f0fba2adSLiam Girdwood } 1704f0fba2adSLiam Girdwood 1705f0fba2adSLiam Girdwood #ifdef CONFIG_SND_SOC_AC97_BUS 1706f0fba2adSLiam Girdwood static int soc_register_ac97_dai_link(struct snd_soc_pcm_runtime *rtd) 1707f0fba2adSLiam Girdwood { 1708f0fba2adSLiam Girdwood int ret; 1709f0fba2adSLiam Girdwood 1710f0fba2adSLiam Girdwood /* Only instantiate AC97 if not already done by the adaptor 1711f0fba2adSLiam Girdwood * for the generic AC97 subsystem. 17126b05eda6SMark Brown */ 1713f0fba2adSLiam Girdwood if (rtd->codec_dai->driver->ac97_control && !rtd->codec->ac97_registered) { 17140562f788SMika Westerberg /* 17150562f788SMika Westerberg * It is possible that the AC97 device is already registered to 17160562f788SMika Westerberg * the device subsystem. This happens when the device is created 17170562f788SMika Westerberg * via snd_ac97_mixer(). Currently only SoC codec that does so 17180562f788SMika Westerberg * is the generic AC97 glue but others migh emerge. 17190562f788SMika Westerberg * 17200562f788SMika Westerberg * In those cases we don't try to register the device again. 17210562f788SMika Westerberg */ 17220562f788SMika Westerberg if (!rtd->codec->ac97_created) 17230562f788SMika Westerberg return 0; 1724f0fba2adSLiam Girdwood 1725f0fba2adSLiam Girdwood ret = soc_ac97_dev_register(rtd->codec); 1726f0fba2adSLiam Girdwood if (ret < 0) { 1727f0fba2adSLiam Girdwood printk(KERN_ERR "asoc: AC97 device register failed\n"); 1728f0fba2adSLiam Girdwood return ret; 1729435c5e25SMark Brown } 1730435c5e25SMark Brown 1731f0fba2adSLiam Girdwood rtd->codec->ac97_registered = 1; 1732f0fba2adSLiam Girdwood } 1733f0fba2adSLiam Girdwood return 0; 1734f0fba2adSLiam Girdwood } 1735435c5e25SMark Brown 1736f0fba2adSLiam Girdwood static void soc_unregister_ac97_dai_link(struct snd_soc_codec *codec) 1737f0fba2adSLiam Girdwood { 1738f0fba2adSLiam Girdwood if (codec->ac97_registered) { 1739f0fba2adSLiam Girdwood soc_ac97_dev_unregister(codec); 1740f0fba2adSLiam Girdwood codec->ac97_registered = 0; 1741f0fba2adSLiam Girdwood } 1742f0fba2adSLiam Girdwood } 1743f0fba2adSLiam Girdwood #endif 1744435c5e25SMark Brown 17452eea392dSJarkko Nikula static int soc_probe_aux_dev(struct snd_soc_card *card, int num) 17462eea392dSJarkko Nikula { 17472eea392dSJarkko Nikula struct snd_soc_aux_dev *aux_dev = &card->aux_dev[num]; 17482eea392dSJarkko Nikula struct snd_soc_codec *codec; 1749676ad98aSJarkko Nikula int ret = -ENODEV; 17502eea392dSJarkko Nikula 17512eea392dSJarkko Nikula /* find CODEC from registered CODECs*/ 17522eea392dSJarkko Nikula list_for_each_entry(codec, &codec_list, list) { 17532eea392dSJarkko Nikula if (!strcmp(codec->name, aux_dev->codec_name)) { 17542eea392dSJarkko Nikula if (codec->probed) { 17552eea392dSJarkko Nikula dev_err(codec->dev, 17562eea392dSJarkko Nikula "asoc: codec already probed"); 17572eea392dSJarkko Nikula ret = -EBUSY; 17582eea392dSJarkko Nikula goto out; 17592eea392dSJarkko Nikula } 1760676ad98aSJarkko Nikula goto found; 17612eea392dSJarkko Nikula } 17622eea392dSJarkko Nikula } 1763676ad98aSJarkko Nikula /* codec not found */ 1764676ad98aSJarkko Nikula dev_err(card->dev, "asoc: codec %s not found", aux_dev->codec_name); 1765676ad98aSJarkko Nikula goto out; 17662eea392dSJarkko Nikula 1767676ad98aSJarkko Nikula found: 1768589c3563SJarkko Nikula ret = soc_probe_codec(card, codec); 17692eea392dSJarkko Nikula if (ret < 0) 1770589c3563SJarkko Nikula return ret; 17712eea392dSJarkko Nikula 1772589c3563SJarkko Nikula ret = soc_post_component_init(card, codec, num, 1); 17732eea392dSJarkko Nikula 17742eea392dSJarkko Nikula out: 17752eea392dSJarkko Nikula return ret; 17762eea392dSJarkko Nikula } 17772eea392dSJarkko Nikula 17782eea392dSJarkko Nikula static void soc_remove_aux_dev(struct snd_soc_card *card, int num) 17792eea392dSJarkko Nikula { 17802eea392dSJarkko Nikula struct snd_soc_pcm_runtime *rtd = &card->rtd_aux[num]; 17812eea392dSJarkko Nikula struct snd_soc_codec *codec = rtd->codec; 17822eea392dSJarkko Nikula 17832eea392dSJarkko Nikula /* unregister the rtd device */ 17842eea392dSJarkko Nikula if (rtd->dev_registered) { 1785589c3563SJarkko Nikula device_remove_file(&rtd->dev, &dev_attr_codec_reg); 17862eea392dSJarkko Nikula device_unregister(&rtd->dev); 17872eea392dSJarkko Nikula rtd->dev_registered = 0; 17882eea392dSJarkko Nikula } 17892eea392dSJarkko Nikula 1790589c3563SJarkko Nikula if (codec && codec->probed) 1791589c3563SJarkko Nikula soc_remove_codec(codec); 17922eea392dSJarkko Nikula } 17932eea392dSJarkko Nikula 1794fdf0f54dSDimitris Papastamos static int snd_soc_init_codec_cache(struct snd_soc_codec *codec, 1795fdf0f54dSDimitris Papastamos enum snd_soc_compress_type compress_type) 1796fdf0f54dSDimitris Papastamos { 1797fdf0f54dSDimitris Papastamos int ret; 1798fdf0f54dSDimitris Papastamos 1799fdf0f54dSDimitris Papastamos if (codec->cache_init) 1800fdf0f54dSDimitris Papastamos return 0; 1801fdf0f54dSDimitris Papastamos 1802fdf0f54dSDimitris Papastamos /* override the compress_type if necessary */ 1803fdf0f54dSDimitris Papastamos if (compress_type && codec->compress_type != compress_type) 1804fdf0f54dSDimitris Papastamos codec->compress_type = compress_type; 1805fdf0f54dSDimitris Papastamos ret = snd_soc_cache_init(codec); 1806fdf0f54dSDimitris Papastamos if (ret < 0) { 1807fdf0f54dSDimitris Papastamos dev_err(codec->dev, "Failed to set cache compression type: %d\n", 1808fdf0f54dSDimitris Papastamos ret); 1809fdf0f54dSDimitris Papastamos return ret; 1810fdf0f54dSDimitris Papastamos } 1811fdf0f54dSDimitris Papastamos codec->cache_init = 1; 1812fdf0f54dSDimitris Papastamos return 0; 1813fdf0f54dSDimitris Papastamos } 1814fdf0f54dSDimitris Papastamos 1815f0fba2adSLiam Girdwood static void snd_soc_instantiate_card(struct snd_soc_card *card) 1816f0fba2adSLiam Girdwood { 1817fdf0f54dSDimitris Papastamos struct snd_soc_codec *codec; 1818fdf0f54dSDimitris Papastamos struct snd_soc_codec_conf *codec_conf; 1819fdf0f54dSDimitris Papastamos enum snd_soc_compress_type compress_type; 1820f0fba2adSLiam Girdwood int ret, i; 182196dd3622SMark Brown 1822f0fba2adSLiam Girdwood mutex_lock(&card->mutex); 1823f0fba2adSLiam Girdwood 1824f0fba2adSLiam Girdwood if (card->instantiated) { 1825f0fba2adSLiam Girdwood mutex_unlock(&card->mutex); 1826435c5e25SMark Brown return; 1827db2a4165SFrank Mandarino } 1828db2a4165SFrank Mandarino 1829f0fba2adSLiam Girdwood /* bind DAIs */ 1830f0fba2adSLiam Girdwood for (i = 0; i < card->num_links; i++) 1831f0fba2adSLiam Girdwood soc_bind_dai_link(card, i); 1832f0fba2adSLiam Girdwood 1833f0fba2adSLiam Girdwood /* bind completed ? */ 1834f0fba2adSLiam Girdwood if (card->num_rtd != card->num_links) { 1835f0fba2adSLiam Girdwood mutex_unlock(&card->mutex); 1836f0fba2adSLiam Girdwood return; 1837db2a4165SFrank Mandarino } 1838db2a4165SFrank Mandarino 1839fdf0f54dSDimitris Papastamos /* initialize the register cache for each available codec */ 1840fdf0f54dSDimitris Papastamos list_for_each_entry(codec, &codec_list, list) { 1841fdf0f54dSDimitris Papastamos if (codec->cache_init) 1842fdf0f54dSDimitris Papastamos continue; 1843861f2fafSDimitris Papastamos /* by default we don't override the compress_type */ 1844861f2fafSDimitris Papastamos compress_type = 0; 1845fdf0f54dSDimitris Papastamos /* check to see if we need to override the compress_type */ 1846fdf0f54dSDimitris Papastamos for (i = 0; i < card->num_configs; ++i) { 1847fdf0f54dSDimitris Papastamos codec_conf = &card->codec_conf[i]; 1848fdf0f54dSDimitris Papastamos if (!strcmp(codec->name, codec_conf->dev_name)) { 1849fdf0f54dSDimitris Papastamos compress_type = codec_conf->compress_type; 1850fdf0f54dSDimitris Papastamos if (compress_type && compress_type 1851fdf0f54dSDimitris Papastamos != codec->compress_type) 1852fdf0f54dSDimitris Papastamos break; 1853fdf0f54dSDimitris Papastamos } 1854fdf0f54dSDimitris Papastamos } 1855fdf0f54dSDimitris Papastamos ret = snd_soc_init_codec_cache(codec, compress_type); 1856fdf0f54dSDimitris Papastamos if (ret < 0) { 1857fdf0f54dSDimitris Papastamos mutex_unlock(&card->mutex); 1858fdf0f54dSDimitris Papastamos return; 1859fdf0f54dSDimitris Papastamos } 1860fdf0f54dSDimitris Papastamos } 1861fdf0f54dSDimitris Papastamos 1862f0fba2adSLiam Girdwood /* card bind complete so register a sound card */ 1863f0fba2adSLiam Girdwood ret = snd_card_create(SNDRV_DEFAULT_IDX1, SNDRV_DEFAULT_STR1, 1864f0fba2adSLiam Girdwood card->owner, 0, &card->snd_card); 1865f0fba2adSLiam Girdwood if (ret < 0) { 1866f0fba2adSLiam Girdwood printk(KERN_ERR "asoc: can't create sound card for card %s\n", 1867f0fba2adSLiam Girdwood card->name); 1868f0fba2adSLiam Girdwood mutex_unlock(&card->mutex); 1869f0fba2adSLiam Girdwood return; 1870db2a4165SFrank Mandarino } 1871f0fba2adSLiam Girdwood card->snd_card->dev = card->dev; 1872db2a4165SFrank Mandarino 1873e37a4970SMark Brown card->dapm.bias_level = SND_SOC_BIAS_OFF; 1874e37a4970SMark Brown card->dapm.dev = card->dev; 1875e37a4970SMark Brown card->dapm.card = card; 1876e37a4970SMark Brown list_add(&card->dapm.list, &card->dapm_list); 1877e37a4970SMark Brown 1878d5d1e0beSLars-Peter Clausen #ifdef CONFIG_DEBUG_FS 1879d5d1e0beSLars-Peter Clausen snd_soc_dapm_debugfs_init(&card->dapm, card->debugfs_card_root); 1880d5d1e0beSLars-Peter Clausen #endif 1881d5d1e0beSLars-Peter Clausen 188288ee1c61SMark Brown #ifdef CONFIG_PM_SLEEP 18836ed25978SAndy Green /* deferred resume work */ 18846308419aSMark Brown INIT_WORK(&card->deferred_resume_work, soc_resume_deferred); 18851301a964SRandy Dunlap #endif 18866ed25978SAndy Green 18879a841ebbSMark Brown if (card->dapm_widgets) 18889a841ebbSMark Brown snd_soc_dapm_new_controls(&card->dapm, card->dapm_widgets, 18899a841ebbSMark Brown card->num_dapm_widgets); 18909a841ebbSMark Brown 1891f0fba2adSLiam Girdwood /* initialise the sound card only once */ 1892f0fba2adSLiam Girdwood if (card->probe) { 1893e7361ec4SMark Brown ret = card->probe(card); 1894f0fba2adSLiam Girdwood if (ret < 0) 1895f0fba2adSLiam Girdwood goto card_probe_error; 1896f0fba2adSLiam Girdwood } 1897f0fba2adSLiam Girdwood 1898fe3e78e0SMark Brown for (i = 0; i < card->num_links; i++) { 1899f0fba2adSLiam Girdwood ret = soc_probe_dai_link(card, i); 1900fe3e78e0SMark Brown if (ret < 0) { 1901321de0d0SMark Brown pr_err("asoc: failed to instantiate card %s: %d\n", 1902321de0d0SMark Brown card->name, ret); 1903f0fba2adSLiam Girdwood goto probe_dai_err; 1904fe3e78e0SMark Brown } 1905fe3e78e0SMark Brown } 1906fe3e78e0SMark Brown 19072eea392dSJarkko Nikula for (i = 0; i < card->num_aux_devs; i++) { 19082eea392dSJarkko Nikula ret = soc_probe_aux_dev(card, i); 19092eea392dSJarkko Nikula if (ret < 0) { 19102eea392dSJarkko Nikula pr_err("asoc: failed to add auxiliary devices %s: %d\n", 19112eea392dSJarkko Nikula card->name, ret); 19122eea392dSJarkko Nikula goto probe_aux_dev_err; 19132eea392dSJarkko Nikula } 19142eea392dSJarkko Nikula } 19152eea392dSJarkko Nikula 1916b7af1dafSMark Brown /* We should have a non-codec control add function but we don't */ 1917b7af1dafSMark Brown if (card->controls) 1918b7af1dafSMark Brown snd_soc_add_controls(list_first_entry(&card->codec_dev_list, 1919b7af1dafSMark Brown struct snd_soc_codec, 1920b7af1dafSMark Brown card_list), 1921b7af1dafSMark Brown card->controls, 1922b7af1dafSMark Brown card->num_controls); 1923b7af1dafSMark Brown 1924b8ad29deSMark Brown if (card->dapm_routes) 1925b8ad29deSMark Brown snd_soc_dapm_add_routes(&card->dapm, card->dapm_routes, 1926b8ad29deSMark Brown card->num_dapm_routes); 1927b8ad29deSMark Brown 1928f0fba2adSLiam Girdwood snprintf(card->snd_card->shortname, sizeof(card->snd_card->shortname), 1929fe3e78e0SMark Brown "%s", card->name); 1930f0fba2adSLiam Girdwood snprintf(card->snd_card->longname, sizeof(card->snd_card->longname), 1931f0fba2adSLiam Girdwood "%s", card->name); 1932fe3e78e0SMark Brown 193328e9ad92SMark Brown if (card->late_probe) { 193428e9ad92SMark Brown ret = card->late_probe(card); 193528e9ad92SMark Brown if (ret < 0) { 193628e9ad92SMark Brown dev_err(card->dev, "%s late_probe() failed: %d\n", 193728e9ad92SMark Brown card->name, ret); 193828e9ad92SMark Brown goto probe_aux_dev_err; 193928e9ad92SMark Brown } 194028e9ad92SMark Brown } 194128e9ad92SMark Brown 1942f0fba2adSLiam Girdwood ret = snd_card_register(card->snd_card); 1943fe3e78e0SMark Brown if (ret < 0) { 1944f0fba2adSLiam Girdwood printk(KERN_ERR "asoc: failed to register soundcard for %s\n", card->name); 19456b3ed785SAxel Lin goto probe_aux_dev_err; 1946fe3e78e0SMark Brown } 1947fe3e78e0SMark Brown 1948fe3e78e0SMark Brown #ifdef CONFIG_SND_SOC_AC97_BUS 1949f0fba2adSLiam Girdwood /* register any AC97 codecs */ 1950f0fba2adSLiam Girdwood for (i = 0; i < card->num_rtd; i++) { 1951f0fba2adSLiam Girdwood ret = soc_register_ac97_dai_link(&card->rtd[i]); 1952fe3e78e0SMark Brown if (ret < 0) { 1953f0fba2adSLiam Girdwood printk(KERN_ERR "asoc: failed to register AC97 %s\n", card->name); 19546b3ed785SAxel Lin while (--i >= 0) 19557d8316dfSMark Brown soc_unregister_ac97_dai_link(card->rtd[i].codec); 19566b3ed785SAxel Lin goto probe_aux_dev_err; 1957fe3e78e0SMark Brown } 1958fe3e78e0SMark Brown } 1959fe3e78e0SMark Brown #endif 1960fe3e78e0SMark Brown 1961435c5e25SMark Brown card->instantiated = 1; 1962f0fba2adSLiam Girdwood mutex_unlock(&card->mutex); 1963435c5e25SMark Brown return; 1964db2a4165SFrank Mandarino 19652eea392dSJarkko Nikula probe_aux_dev_err: 19662eea392dSJarkko Nikula for (i = 0; i < card->num_aux_devs; i++) 19672eea392dSJarkko Nikula soc_remove_aux_dev(card, i); 19682eea392dSJarkko Nikula 1969f0fba2adSLiam Girdwood probe_dai_err: 19700671fd8eSKuninori Morimoto soc_remove_dai_links(card); 1971fe3e78e0SMark Brown 1972f0fba2adSLiam Girdwood card_probe_error: 197387506549SMark Brown if (card->remove) 1974e7361ec4SMark Brown card->remove(card); 1975f0fba2adSLiam Girdwood 1976f0fba2adSLiam Girdwood snd_card_free(card->snd_card); 1977f0fba2adSLiam Girdwood 1978f0fba2adSLiam Girdwood mutex_unlock(&card->mutex); 1979435c5e25SMark Brown } 1980db2a4165SFrank Mandarino 1981435c5e25SMark Brown /* 1982421f91d2SUwe Kleine-König * Attempt to initialise any uninitialised cards. Must be called with 1983435c5e25SMark Brown * client_mutex. 1984435c5e25SMark Brown */ 1985435c5e25SMark Brown static void snd_soc_instantiate_cards(void) 1986435c5e25SMark Brown { 1987435c5e25SMark Brown struct snd_soc_card *card; 1988435c5e25SMark Brown list_for_each_entry(card, &card_list, list) 1989435c5e25SMark Brown snd_soc_instantiate_card(card); 1990435c5e25SMark Brown } 1991435c5e25SMark Brown 1992435c5e25SMark Brown /* probes a new socdev */ 1993435c5e25SMark Brown static int soc_probe(struct platform_device *pdev) 1994435c5e25SMark Brown { 1995f0fba2adSLiam Girdwood struct snd_soc_card *card = platform_get_drvdata(pdev); 1996435c5e25SMark Brown int ret = 0; 1997435c5e25SMark Brown 199870a7ca34SVinod Koul /* 199970a7ca34SVinod Koul * no card, so machine driver should be registering card 200070a7ca34SVinod Koul * we should not be here in that case so ret error 200170a7ca34SVinod Koul */ 200270a7ca34SVinod Koul if (!card) 200370a7ca34SVinod Koul return -EINVAL; 200470a7ca34SVinod Koul 2005435c5e25SMark Brown /* Bodge while we unpick instantiation */ 2006435c5e25SMark Brown card->dev = &pdev->dev; 2007f0fba2adSLiam Girdwood 2008435c5e25SMark Brown ret = snd_soc_register_card(card); 2009435c5e25SMark Brown if (ret != 0) { 2010435c5e25SMark Brown dev_err(&pdev->dev, "Failed to register card\n"); 2011db2a4165SFrank Mandarino return ret; 2012db2a4165SFrank Mandarino } 2013db2a4165SFrank Mandarino 2014435c5e25SMark Brown return 0; 2015435c5e25SMark Brown } 2016435c5e25SMark Brown 2017b0e26485SVinod Koul static int soc_cleanup_card_resources(struct snd_soc_card *card) 2018db2a4165SFrank Mandarino { 2019db2a4165SFrank Mandarino int i; 2020db2a4165SFrank Mandarino 2021f0fba2adSLiam Girdwood /* make sure any delayed work runs */ 2022f0fba2adSLiam Girdwood for (i = 0; i < card->num_rtd; i++) { 2023f0fba2adSLiam Girdwood struct snd_soc_pcm_runtime *rtd = &card->rtd[i]; 20245b84ba26STejun Heo flush_delayed_work_sync(&rtd->delayed_work); 2025db2a4165SFrank Mandarino } 2026db2a4165SFrank Mandarino 20272eea392dSJarkko Nikula /* remove auxiliary devices */ 20282eea392dSJarkko Nikula for (i = 0; i < card->num_aux_devs; i++) 20292eea392dSJarkko Nikula soc_remove_aux_dev(card, i); 20302eea392dSJarkko Nikula 2031f0fba2adSLiam Girdwood /* remove and free each DAI */ 20320671fd8eSKuninori Morimoto soc_remove_dai_links(card); 2033f0fba2adSLiam Girdwood 2034a6052154SJarkko Nikula soc_cleanup_card_debugfs(card); 2035a6052154SJarkko Nikula 2036f0fba2adSLiam Girdwood /* remove the card */ 203787506549SMark Brown if (card->remove) 2038e7361ec4SMark Brown card->remove(card); 2039f0fba2adSLiam Girdwood 20400aaae527SLars-Peter Clausen snd_soc_dapm_free(&card->dapm); 20410aaae527SLars-Peter Clausen 2042f0fba2adSLiam Girdwood kfree(card->rtd); 2043f0fba2adSLiam Girdwood snd_card_free(card->snd_card); 2044b0e26485SVinod Koul return 0; 2045b0e26485SVinod Koul 2046b2dfa62cSGuennadi Liakhovetski } 2047b0e26485SVinod Koul 2048b0e26485SVinod Koul /* removes a socdev */ 2049b0e26485SVinod Koul static int soc_remove(struct platform_device *pdev) 2050b0e26485SVinod Koul { 2051b0e26485SVinod Koul struct snd_soc_card *card = platform_get_drvdata(pdev); 2052b0e26485SVinod Koul 2053c5af3a2eSMark Brown snd_soc_unregister_card(card); 2054db2a4165SFrank Mandarino return 0; 2055db2a4165SFrank Mandarino } 2056db2a4165SFrank Mandarino 20576f8ab4acSMark Brown int snd_soc_poweroff(struct device *dev) 205851737470SMark Brown { 20596f8ab4acSMark Brown struct snd_soc_card *card = dev_get_drvdata(dev); 2060f0fba2adSLiam Girdwood int i; 206151737470SMark Brown 206251737470SMark Brown if (!card->instantiated) 2063416356fcSMark Brown return 0; 206451737470SMark Brown 206551737470SMark Brown /* Flush out pmdown_time work - we actually do want to run it 206651737470SMark Brown * now, we're shutting down so no imminent restart. */ 2067f0fba2adSLiam Girdwood for (i = 0; i < card->num_rtd; i++) { 2068f0fba2adSLiam Girdwood struct snd_soc_pcm_runtime *rtd = &card->rtd[i]; 20695b84ba26STejun Heo flush_delayed_work_sync(&rtd->delayed_work); 2070f0fba2adSLiam Girdwood } 207151737470SMark Brown 2072f0fba2adSLiam Girdwood snd_soc_dapm_shutdown(card); 2073416356fcSMark Brown 2074416356fcSMark Brown return 0; 207551737470SMark Brown } 20766f8ab4acSMark Brown EXPORT_SYMBOL_GPL(snd_soc_poweroff); 207751737470SMark Brown 20786f8ab4acSMark Brown const struct dev_pm_ops snd_soc_pm_ops = { 20796f8ab4acSMark Brown .suspend = snd_soc_suspend, 20806f8ab4acSMark Brown .resume = snd_soc_resume, 20816f8ab4acSMark Brown .poweroff = snd_soc_poweroff, 2082416356fcSMark Brown }; 2083deb2607eSStephen Warren EXPORT_SYMBOL_GPL(snd_soc_pm_ops); 2084416356fcSMark Brown 2085db2a4165SFrank Mandarino /* ASoC platform driver */ 2086db2a4165SFrank Mandarino static struct platform_driver soc_driver = { 2087db2a4165SFrank Mandarino .driver = { 2088db2a4165SFrank Mandarino .name = "soc-audio", 20898b45a209SKay Sievers .owner = THIS_MODULE, 20906f8ab4acSMark Brown .pm = &snd_soc_pm_ops, 2091db2a4165SFrank Mandarino }, 2092db2a4165SFrank Mandarino .probe = soc_probe, 2093db2a4165SFrank Mandarino .remove = soc_remove, 2094db2a4165SFrank Mandarino }; 2095db2a4165SFrank Mandarino 2096db2a4165SFrank Mandarino /* create a new pcm */ 2097f0fba2adSLiam Girdwood static int soc_new_pcm(struct snd_soc_pcm_runtime *rtd, int num) 2098db2a4165SFrank Mandarino { 2099f0fba2adSLiam Girdwood struct snd_soc_codec *codec = rtd->codec; 2100f0fba2adSLiam Girdwood struct snd_soc_platform *platform = rtd->platform; 2101f0fba2adSLiam Girdwood struct snd_soc_dai *codec_dai = rtd->codec_dai; 2102f0fba2adSLiam Girdwood struct snd_soc_dai *cpu_dai = rtd->cpu_dai; 2103db2a4165SFrank Mandarino struct snd_pcm *pcm; 2104db2a4165SFrank Mandarino char new_name[64]; 2105db2a4165SFrank Mandarino int ret = 0, playback = 0, capture = 0; 2106db2a4165SFrank Mandarino 2107db2a4165SFrank Mandarino /* check client and interface hw capabilities */ 210840ca1142SMark Brown snprintf(new_name, sizeof(new_name), "%s %s-%d", 2109f0fba2adSLiam Girdwood rtd->dai_link->stream_name, codec_dai->name, num); 2110db2a4165SFrank Mandarino 2111f0fba2adSLiam Girdwood if (codec_dai->driver->playback.channels_min) 2112db2a4165SFrank Mandarino playback = 1; 2113f0fba2adSLiam Girdwood if (codec_dai->driver->capture.channels_min) 2114db2a4165SFrank Mandarino capture = 1; 2115db2a4165SFrank Mandarino 2116f0fba2adSLiam Girdwood dev_dbg(rtd->card->dev, "registered pcm #%d %s\n",num,new_name); 2117f0fba2adSLiam Girdwood ret = snd_pcm_new(rtd->card->snd_card, new_name, 2118f0fba2adSLiam Girdwood num, playback, capture, &pcm); 2119db2a4165SFrank Mandarino if (ret < 0) { 2120f0fba2adSLiam Girdwood printk(KERN_ERR "asoc: can't create pcm for codec %s\n", codec->name); 2121db2a4165SFrank Mandarino return ret; 2122db2a4165SFrank Mandarino } 2123db2a4165SFrank Mandarino 2124f0fba2adSLiam Girdwood rtd->pcm = pcm; 2125db2a4165SFrank Mandarino pcm->private_data = rtd; 21268842c72aSMark Brown if (platform->driver->ops) { 2127f0fba2adSLiam Girdwood soc_pcm_ops.mmap = platform->driver->ops->mmap; 2128f0fba2adSLiam Girdwood soc_pcm_ops.pointer = platform->driver->ops->pointer; 2129f0fba2adSLiam Girdwood soc_pcm_ops.ioctl = platform->driver->ops->ioctl; 2130f0fba2adSLiam Girdwood soc_pcm_ops.copy = platform->driver->ops->copy; 2131f0fba2adSLiam Girdwood soc_pcm_ops.silence = platform->driver->ops->silence; 2132f0fba2adSLiam Girdwood soc_pcm_ops.ack = platform->driver->ops->ack; 2133f0fba2adSLiam Girdwood soc_pcm_ops.page = platform->driver->ops->page; 21348842c72aSMark Brown } 2135db2a4165SFrank Mandarino 2136db2a4165SFrank Mandarino if (playback) 2137db2a4165SFrank Mandarino snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &soc_pcm_ops); 2138db2a4165SFrank Mandarino 2139db2a4165SFrank Mandarino if (capture) 2140db2a4165SFrank Mandarino snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &soc_pcm_ops); 2141db2a4165SFrank Mandarino 21428842c72aSMark Brown if (platform->driver->pcm_new) { 21438842c72aSMark Brown ret = platform->driver->pcm_new(rtd->card->snd_card, 21448842c72aSMark Brown codec_dai, pcm); 2145db2a4165SFrank Mandarino if (ret < 0) { 21468842c72aSMark Brown pr_err("asoc: platform pcm constructor failed\n"); 2147db2a4165SFrank Mandarino return ret; 2148db2a4165SFrank Mandarino } 21498842c72aSMark Brown } 2150db2a4165SFrank Mandarino 2151f0fba2adSLiam Girdwood pcm->private_free = platform->driver->pcm_free; 2152db2a4165SFrank Mandarino printk(KERN_INFO "asoc: %s <-> %s mapping ok\n", codec_dai->name, 2153db2a4165SFrank Mandarino cpu_dai->name); 2154db2a4165SFrank Mandarino return ret; 2155db2a4165SFrank Mandarino } 2156db2a4165SFrank Mandarino 2157096e49d5SMark Brown /** 2158096e49d5SMark Brown * snd_soc_codec_volatile_register: Report if a register is volatile. 2159096e49d5SMark Brown * 2160096e49d5SMark Brown * @codec: CODEC to query. 2161096e49d5SMark Brown * @reg: Register to query. 2162096e49d5SMark Brown * 2163096e49d5SMark Brown * Boolean function indiciating if a CODEC register is volatile. 2164096e49d5SMark Brown */ 2165181e055eSMark Brown int snd_soc_codec_volatile_register(struct snd_soc_codec *codec, 2166181e055eSMark Brown unsigned int reg) 2167096e49d5SMark Brown { 21681500b7b5SDimitris Papastamos if (codec->volatile_register) 21691500b7b5SDimitris Papastamos return codec->volatile_register(codec, reg); 2170096e49d5SMark Brown else 2171096e49d5SMark Brown return 0; 2172096e49d5SMark Brown } 2173096e49d5SMark Brown EXPORT_SYMBOL_GPL(snd_soc_codec_volatile_register); 2174096e49d5SMark Brown 2175db2a4165SFrank Mandarino /** 2176239c9706SDimitris Papastamos * snd_soc_codec_readable_register: Report if a register is readable. 2177239c9706SDimitris Papastamos * 2178239c9706SDimitris Papastamos * @codec: CODEC to query. 2179239c9706SDimitris Papastamos * @reg: Register to query. 2180239c9706SDimitris Papastamos * 2181239c9706SDimitris Papastamos * Boolean function indicating if a CODEC register is readable. 2182239c9706SDimitris Papastamos */ 2183239c9706SDimitris Papastamos int snd_soc_codec_readable_register(struct snd_soc_codec *codec, 2184239c9706SDimitris Papastamos unsigned int reg) 2185239c9706SDimitris Papastamos { 2186239c9706SDimitris Papastamos if (codec->readable_register) 2187239c9706SDimitris Papastamos return codec->readable_register(codec, reg); 2188239c9706SDimitris Papastamos else 2189239c9706SDimitris Papastamos return 0; 2190239c9706SDimitris Papastamos } 2191239c9706SDimitris Papastamos EXPORT_SYMBOL_GPL(snd_soc_codec_readable_register); 2192239c9706SDimitris Papastamos 2193239c9706SDimitris Papastamos /** 2194239c9706SDimitris Papastamos * snd_soc_codec_writable_register: Report if a register is writable. 2195239c9706SDimitris Papastamos * 2196239c9706SDimitris Papastamos * @codec: CODEC to query. 2197239c9706SDimitris Papastamos * @reg: Register to query. 2198239c9706SDimitris Papastamos * 2199239c9706SDimitris Papastamos * Boolean function indicating if a CODEC register is writable. 2200239c9706SDimitris Papastamos */ 2201239c9706SDimitris Papastamos int snd_soc_codec_writable_register(struct snd_soc_codec *codec, 2202239c9706SDimitris Papastamos unsigned int reg) 2203239c9706SDimitris Papastamos { 2204239c9706SDimitris Papastamos if (codec->writable_register) 2205239c9706SDimitris Papastamos return codec->writable_register(codec, reg); 2206239c9706SDimitris Papastamos else 2207239c9706SDimitris Papastamos return 0; 2208239c9706SDimitris Papastamos } 2209239c9706SDimitris Papastamos EXPORT_SYMBOL_GPL(snd_soc_codec_writable_register); 2210239c9706SDimitris Papastamos 2211239c9706SDimitris Papastamos /** 2212db2a4165SFrank Mandarino * snd_soc_new_ac97_codec - initailise AC97 device 2213db2a4165SFrank Mandarino * @codec: audio codec 2214db2a4165SFrank Mandarino * @ops: AC97 bus operations 2215db2a4165SFrank Mandarino * @num: AC97 codec number 2216db2a4165SFrank Mandarino * 2217db2a4165SFrank Mandarino * Initialises AC97 codec resources for use by ad-hoc devices only. 2218db2a4165SFrank Mandarino */ 2219db2a4165SFrank Mandarino int snd_soc_new_ac97_codec(struct snd_soc_codec *codec, 2220db2a4165SFrank Mandarino struct snd_ac97_bus_ops *ops, int num) 2221db2a4165SFrank Mandarino { 2222db2a4165SFrank Mandarino mutex_lock(&codec->mutex); 2223db2a4165SFrank Mandarino 2224db2a4165SFrank Mandarino codec->ac97 = kzalloc(sizeof(struct snd_ac97), GFP_KERNEL); 2225db2a4165SFrank Mandarino if (codec->ac97 == NULL) { 2226db2a4165SFrank Mandarino mutex_unlock(&codec->mutex); 2227db2a4165SFrank Mandarino return -ENOMEM; 2228db2a4165SFrank Mandarino } 2229db2a4165SFrank Mandarino 2230db2a4165SFrank Mandarino codec->ac97->bus = kzalloc(sizeof(struct snd_ac97_bus), GFP_KERNEL); 2231db2a4165SFrank Mandarino if (codec->ac97->bus == NULL) { 2232db2a4165SFrank Mandarino kfree(codec->ac97); 2233db2a4165SFrank Mandarino codec->ac97 = NULL; 2234db2a4165SFrank Mandarino mutex_unlock(&codec->mutex); 2235db2a4165SFrank Mandarino return -ENOMEM; 2236db2a4165SFrank Mandarino } 2237db2a4165SFrank Mandarino 2238db2a4165SFrank Mandarino codec->ac97->bus->ops = ops; 2239db2a4165SFrank Mandarino codec->ac97->num = num; 22400562f788SMika Westerberg 22410562f788SMika Westerberg /* 22420562f788SMika Westerberg * Mark the AC97 device to be created by us. This way we ensure that the 22430562f788SMika Westerberg * device will be registered with the device subsystem later on. 22440562f788SMika Westerberg */ 22450562f788SMika Westerberg codec->ac97_created = 1; 22460562f788SMika Westerberg 2247db2a4165SFrank Mandarino mutex_unlock(&codec->mutex); 2248db2a4165SFrank Mandarino return 0; 2249db2a4165SFrank Mandarino } 2250db2a4165SFrank Mandarino EXPORT_SYMBOL_GPL(snd_soc_new_ac97_codec); 2251db2a4165SFrank Mandarino 2252db2a4165SFrank Mandarino /** 2253db2a4165SFrank Mandarino * snd_soc_free_ac97_codec - free AC97 codec device 2254db2a4165SFrank Mandarino * @codec: audio codec 2255db2a4165SFrank Mandarino * 2256db2a4165SFrank Mandarino * Frees AC97 codec device resources. 2257db2a4165SFrank Mandarino */ 2258db2a4165SFrank Mandarino void snd_soc_free_ac97_codec(struct snd_soc_codec *codec) 2259db2a4165SFrank Mandarino { 2260db2a4165SFrank Mandarino mutex_lock(&codec->mutex); 2261f0fba2adSLiam Girdwood #ifdef CONFIG_SND_SOC_AC97_BUS 2262f0fba2adSLiam Girdwood soc_unregister_ac97_dai_link(codec); 2263f0fba2adSLiam Girdwood #endif 2264db2a4165SFrank Mandarino kfree(codec->ac97->bus); 2265db2a4165SFrank Mandarino kfree(codec->ac97); 2266db2a4165SFrank Mandarino codec->ac97 = NULL; 22670562f788SMika Westerberg codec->ac97_created = 0; 2268db2a4165SFrank Mandarino mutex_unlock(&codec->mutex); 2269db2a4165SFrank Mandarino } 2270db2a4165SFrank Mandarino EXPORT_SYMBOL_GPL(snd_soc_free_ac97_codec); 2271db2a4165SFrank Mandarino 2272c3753707SMark Brown unsigned int snd_soc_read(struct snd_soc_codec *codec, unsigned int reg) 2273c3753707SMark Brown { 2274c3753707SMark Brown unsigned int ret; 2275c3753707SMark Brown 2276c3acec26SMark Brown ret = codec->read(codec, reg); 2277c3753707SMark Brown dev_dbg(codec->dev, "read %x => %x\n", reg, ret); 2278a8b1d34fSMark Brown trace_snd_soc_reg_read(codec, reg, ret); 2279c3753707SMark Brown 2280c3753707SMark Brown return ret; 2281c3753707SMark Brown } 2282c3753707SMark Brown EXPORT_SYMBOL_GPL(snd_soc_read); 2283c3753707SMark Brown 2284c3753707SMark Brown unsigned int snd_soc_write(struct snd_soc_codec *codec, 2285c3753707SMark Brown unsigned int reg, unsigned int val) 2286c3753707SMark Brown { 2287c3753707SMark Brown dev_dbg(codec->dev, "write %x = %x\n", reg, val); 2288a8b1d34fSMark Brown trace_snd_soc_reg_write(codec, reg, val); 2289c3acec26SMark Brown return codec->write(codec, reg, val); 2290c3753707SMark Brown } 2291c3753707SMark Brown EXPORT_SYMBOL_GPL(snd_soc_write); 2292c3753707SMark Brown 22935fb609d4SDimitris Papastamos unsigned int snd_soc_bulk_write_raw(struct snd_soc_codec *codec, 22945fb609d4SDimitris Papastamos unsigned int reg, const void *data, size_t len) 22955fb609d4SDimitris Papastamos { 22965fb609d4SDimitris Papastamos return codec->bulk_write_raw(codec, reg, data, len); 22975fb609d4SDimitris Papastamos } 22985fb609d4SDimitris Papastamos EXPORT_SYMBOL_GPL(snd_soc_bulk_write_raw); 22995fb609d4SDimitris Papastamos 2300db2a4165SFrank Mandarino /** 2301db2a4165SFrank Mandarino * snd_soc_update_bits - update codec register bits 2302db2a4165SFrank Mandarino * @codec: audio codec 2303db2a4165SFrank Mandarino * @reg: codec register 2304db2a4165SFrank Mandarino * @mask: register mask 2305db2a4165SFrank Mandarino * @value: new value 2306db2a4165SFrank Mandarino * 2307db2a4165SFrank Mandarino * Writes new register value. 2308db2a4165SFrank Mandarino * 2309180c329dSTimur Tabi * Returns 1 for change, 0 for no change, or negative error code. 2310db2a4165SFrank Mandarino */ 2311db2a4165SFrank Mandarino int snd_soc_update_bits(struct snd_soc_codec *codec, unsigned short reg, 231246f5822fSDaniel Ribeiro unsigned int mask, unsigned int value) 2313db2a4165SFrank Mandarino { 2314db2a4165SFrank Mandarino int change; 231546f5822fSDaniel Ribeiro unsigned int old, new; 2316180c329dSTimur Tabi int ret; 2317db2a4165SFrank Mandarino 2318180c329dSTimur Tabi ret = snd_soc_read(codec, reg); 2319180c329dSTimur Tabi if (ret < 0) 2320180c329dSTimur Tabi return ret; 2321180c329dSTimur Tabi 2322180c329dSTimur Tabi old = ret; 2323db2a4165SFrank Mandarino new = (old & ~mask) | value; 2324db2a4165SFrank Mandarino change = old != new; 2325180c329dSTimur Tabi if (change) { 2326180c329dSTimur Tabi ret = snd_soc_write(codec, reg, new); 2327180c329dSTimur Tabi if (ret < 0) 2328180c329dSTimur Tabi return ret; 2329180c329dSTimur Tabi } 2330db2a4165SFrank Mandarino 2331db2a4165SFrank Mandarino return change; 2332db2a4165SFrank Mandarino } 2333db2a4165SFrank Mandarino EXPORT_SYMBOL_GPL(snd_soc_update_bits); 2334db2a4165SFrank Mandarino 2335db2a4165SFrank Mandarino /** 23366c508c62SEero Nurkkala * snd_soc_update_bits_locked - update codec register bits 23376c508c62SEero Nurkkala * @codec: audio codec 23386c508c62SEero Nurkkala * @reg: codec register 23396c508c62SEero Nurkkala * @mask: register mask 23406c508c62SEero Nurkkala * @value: new value 23416c508c62SEero Nurkkala * 23426c508c62SEero Nurkkala * Writes new register value, and takes the codec mutex. 23436c508c62SEero Nurkkala * 23446c508c62SEero Nurkkala * Returns 1 for change else 0. 23456c508c62SEero Nurkkala */ 2346dd1b3d53SMark Brown int snd_soc_update_bits_locked(struct snd_soc_codec *codec, 23476c508c62SEero Nurkkala unsigned short reg, unsigned int mask, 23486c508c62SEero Nurkkala unsigned int value) 23496c508c62SEero Nurkkala { 23506c508c62SEero Nurkkala int change; 23516c508c62SEero Nurkkala 23526c508c62SEero Nurkkala mutex_lock(&codec->mutex); 23536c508c62SEero Nurkkala change = snd_soc_update_bits(codec, reg, mask, value); 23546c508c62SEero Nurkkala mutex_unlock(&codec->mutex); 23556c508c62SEero Nurkkala 23566c508c62SEero Nurkkala return change; 23576c508c62SEero Nurkkala } 2358dd1b3d53SMark Brown EXPORT_SYMBOL_GPL(snd_soc_update_bits_locked); 23596c508c62SEero Nurkkala 23606c508c62SEero Nurkkala /** 2361db2a4165SFrank Mandarino * snd_soc_test_bits - test register for change 2362db2a4165SFrank Mandarino * @codec: audio codec 2363db2a4165SFrank Mandarino * @reg: codec register 2364db2a4165SFrank Mandarino * @mask: register mask 2365db2a4165SFrank Mandarino * @value: new value 2366db2a4165SFrank Mandarino * 2367db2a4165SFrank Mandarino * Tests a register with a new value and checks if the new value is 2368db2a4165SFrank Mandarino * different from the old value. 2369db2a4165SFrank Mandarino * 2370db2a4165SFrank Mandarino * Returns 1 for change else 0. 2371db2a4165SFrank Mandarino */ 2372db2a4165SFrank Mandarino int snd_soc_test_bits(struct snd_soc_codec *codec, unsigned short reg, 237346f5822fSDaniel Ribeiro unsigned int mask, unsigned int value) 2374db2a4165SFrank Mandarino { 2375db2a4165SFrank Mandarino int change; 237646f5822fSDaniel Ribeiro unsigned int old, new; 2377db2a4165SFrank Mandarino 2378db2a4165SFrank Mandarino old = snd_soc_read(codec, reg); 2379db2a4165SFrank Mandarino new = (old & ~mask) | value; 2380db2a4165SFrank Mandarino change = old != new; 2381db2a4165SFrank Mandarino 2382db2a4165SFrank Mandarino return change; 2383db2a4165SFrank Mandarino } 2384db2a4165SFrank Mandarino EXPORT_SYMBOL_GPL(snd_soc_test_bits); 2385db2a4165SFrank Mandarino 2386db2a4165SFrank Mandarino /** 2387db2a4165SFrank Mandarino * snd_soc_set_runtime_hwparams - set the runtime hardware parameters 2388db2a4165SFrank Mandarino * @substream: the pcm substream 2389db2a4165SFrank Mandarino * @hw: the hardware parameters 2390db2a4165SFrank Mandarino * 2391db2a4165SFrank Mandarino * Sets the substream runtime hardware parameters. 2392db2a4165SFrank Mandarino */ 2393db2a4165SFrank Mandarino int snd_soc_set_runtime_hwparams(struct snd_pcm_substream *substream, 2394db2a4165SFrank Mandarino const struct snd_pcm_hardware *hw) 2395db2a4165SFrank Mandarino { 2396db2a4165SFrank Mandarino struct snd_pcm_runtime *runtime = substream->runtime; 2397db2a4165SFrank Mandarino runtime->hw.info = hw->info; 2398db2a4165SFrank Mandarino runtime->hw.formats = hw->formats; 2399db2a4165SFrank Mandarino runtime->hw.period_bytes_min = hw->period_bytes_min; 2400db2a4165SFrank Mandarino runtime->hw.period_bytes_max = hw->period_bytes_max; 2401db2a4165SFrank Mandarino runtime->hw.periods_min = hw->periods_min; 2402db2a4165SFrank Mandarino runtime->hw.periods_max = hw->periods_max; 2403db2a4165SFrank Mandarino runtime->hw.buffer_bytes_max = hw->buffer_bytes_max; 2404db2a4165SFrank Mandarino runtime->hw.fifo_size = hw->fifo_size; 2405db2a4165SFrank Mandarino return 0; 2406db2a4165SFrank Mandarino } 2407db2a4165SFrank Mandarino EXPORT_SYMBOL_GPL(snd_soc_set_runtime_hwparams); 2408db2a4165SFrank Mandarino 2409db2a4165SFrank Mandarino /** 2410db2a4165SFrank Mandarino * snd_soc_cnew - create new control 2411db2a4165SFrank Mandarino * @_template: control template 2412db2a4165SFrank Mandarino * @data: control private data 2413ac11a2b3SMark Brown * @long_name: control long name 2414efb7ac3fSMark Brown * @prefix: control name prefix 2415db2a4165SFrank Mandarino * 2416db2a4165SFrank Mandarino * Create a new mixer control from a template control. 2417db2a4165SFrank Mandarino * 2418db2a4165SFrank Mandarino * Returns 0 for success, else error. 2419db2a4165SFrank Mandarino */ 2420db2a4165SFrank Mandarino struct snd_kcontrol *snd_soc_cnew(const struct snd_kcontrol_new *_template, 2421efb7ac3fSMark Brown void *data, char *long_name, 2422efb7ac3fSMark Brown const char *prefix) 2423db2a4165SFrank Mandarino { 2424db2a4165SFrank Mandarino struct snd_kcontrol_new template; 2425efb7ac3fSMark Brown struct snd_kcontrol *kcontrol; 2426efb7ac3fSMark Brown char *name = NULL; 2427efb7ac3fSMark Brown int name_len; 2428db2a4165SFrank Mandarino 2429db2a4165SFrank Mandarino memcpy(&template, _template, sizeof(template)); 2430db2a4165SFrank Mandarino template.index = 0; 2431db2a4165SFrank Mandarino 2432efb7ac3fSMark Brown if (!long_name) 2433efb7ac3fSMark Brown long_name = template.name; 2434efb7ac3fSMark Brown 2435efb7ac3fSMark Brown if (prefix) { 2436efb7ac3fSMark Brown name_len = strlen(long_name) + strlen(prefix) + 2; 2437efb7ac3fSMark Brown name = kmalloc(name_len, GFP_ATOMIC); 2438efb7ac3fSMark Brown if (!name) 2439efb7ac3fSMark Brown return NULL; 2440efb7ac3fSMark Brown 2441efb7ac3fSMark Brown snprintf(name, name_len, "%s %s", prefix, long_name); 2442efb7ac3fSMark Brown 2443efb7ac3fSMark Brown template.name = name; 2444efb7ac3fSMark Brown } else { 2445efb7ac3fSMark Brown template.name = long_name; 2446efb7ac3fSMark Brown } 2447efb7ac3fSMark Brown 2448efb7ac3fSMark Brown kcontrol = snd_ctl_new1(&template, data); 2449efb7ac3fSMark Brown 2450efb7ac3fSMark Brown kfree(name); 2451efb7ac3fSMark Brown 2452efb7ac3fSMark Brown return kcontrol; 2453db2a4165SFrank Mandarino } 2454db2a4165SFrank Mandarino EXPORT_SYMBOL_GPL(snd_soc_cnew); 2455db2a4165SFrank Mandarino 2456db2a4165SFrank Mandarino /** 24573e8e1952SIan Molton * snd_soc_add_controls - add an array of controls to a codec. 24583e8e1952SIan Molton * Convienience function to add a list of controls. Many codecs were 24593e8e1952SIan Molton * duplicating this code. 24603e8e1952SIan Molton * 24613e8e1952SIan Molton * @codec: codec to add controls to 24623e8e1952SIan Molton * @controls: array of controls to add 24633e8e1952SIan Molton * @num_controls: number of elements in the array 24643e8e1952SIan Molton * 24653e8e1952SIan Molton * Return 0 for success, else error. 24663e8e1952SIan Molton */ 24673e8e1952SIan Molton int snd_soc_add_controls(struct snd_soc_codec *codec, 24683e8e1952SIan Molton const struct snd_kcontrol_new *controls, int num_controls) 24693e8e1952SIan Molton { 2470f0fba2adSLiam Girdwood struct snd_card *card = codec->card->snd_card; 24713e8e1952SIan Molton int err, i; 24723e8e1952SIan Molton 24733e8e1952SIan Molton for (i = 0; i < num_controls; i++) { 24743e8e1952SIan Molton const struct snd_kcontrol_new *control = &controls[i]; 2475efb7ac3fSMark Brown err = snd_ctl_add(card, snd_soc_cnew(control, codec, 2476efb7ac3fSMark Brown control->name, 2477efb7ac3fSMark Brown codec->name_prefix)); 24783e8e1952SIan Molton if (err < 0) { 2479082100dcSMark Brown dev_err(codec->dev, "%s: Failed to add %s: %d\n", 2480efb7ac3fSMark Brown codec->name, control->name, err); 24813e8e1952SIan Molton return err; 24823e8e1952SIan Molton } 24833e8e1952SIan Molton } 24843e8e1952SIan Molton 24853e8e1952SIan Molton return 0; 24863e8e1952SIan Molton } 24873e8e1952SIan Molton EXPORT_SYMBOL_GPL(snd_soc_add_controls); 24883e8e1952SIan Molton 24893e8e1952SIan Molton /** 2490db2a4165SFrank Mandarino * snd_soc_info_enum_double - enumerated double mixer info callback 2491db2a4165SFrank Mandarino * @kcontrol: mixer control 2492db2a4165SFrank Mandarino * @uinfo: control element information 2493db2a4165SFrank Mandarino * 2494db2a4165SFrank Mandarino * Callback to provide information about a double enumerated 2495db2a4165SFrank Mandarino * mixer control. 2496db2a4165SFrank Mandarino * 2497db2a4165SFrank Mandarino * Returns 0 for success. 2498db2a4165SFrank Mandarino */ 2499db2a4165SFrank Mandarino int snd_soc_info_enum_double(struct snd_kcontrol *kcontrol, 2500db2a4165SFrank Mandarino struct snd_ctl_elem_info *uinfo) 2501db2a4165SFrank Mandarino { 2502db2a4165SFrank Mandarino struct soc_enum *e = (struct soc_enum *)kcontrol->private_value; 2503db2a4165SFrank Mandarino 2504db2a4165SFrank Mandarino uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED; 2505db2a4165SFrank Mandarino uinfo->count = e->shift_l == e->shift_r ? 1 : 2; 2506f8ba0b7bSJon Smirl uinfo->value.enumerated.items = e->max; 2507db2a4165SFrank Mandarino 2508f8ba0b7bSJon Smirl if (uinfo->value.enumerated.item > e->max - 1) 2509f8ba0b7bSJon Smirl uinfo->value.enumerated.item = e->max - 1; 2510db2a4165SFrank Mandarino strcpy(uinfo->value.enumerated.name, 2511db2a4165SFrank Mandarino e->texts[uinfo->value.enumerated.item]); 2512db2a4165SFrank Mandarino return 0; 2513db2a4165SFrank Mandarino } 2514db2a4165SFrank Mandarino EXPORT_SYMBOL_GPL(snd_soc_info_enum_double); 2515db2a4165SFrank Mandarino 2516db2a4165SFrank Mandarino /** 2517db2a4165SFrank Mandarino * snd_soc_get_enum_double - enumerated double mixer get callback 2518db2a4165SFrank Mandarino * @kcontrol: mixer control 2519ac11a2b3SMark Brown * @ucontrol: control element information 2520db2a4165SFrank Mandarino * 2521db2a4165SFrank Mandarino * Callback to get the value of a double enumerated mixer. 2522db2a4165SFrank Mandarino * 2523db2a4165SFrank Mandarino * Returns 0 for success. 2524db2a4165SFrank Mandarino */ 2525db2a4165SFrank Mandarino int snd_soc_get_enum_double(struct snd_kcontrol *kcontrol, 2526db2a4165SFrank Mandarino struct snd_ctl_elem_value *ucontrol) 2527db2a4165SFrank Mandarino { 2528db2a4165SFrank Mandarino struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol); 2529db2a4165SFrank Mandarino struct soc_enum *e = (struct soc_enum *)kcontrol->private_value; 253046f5822fSDaniel Ribeiro unsigned int val, bitmask; 2531db2a4165SFrank Mandarino 2532f8ba0b7bSJon Smirl for (bitmask = 1; bitmask < e->max; bitmask <<= 1) 2533db2a4165SFrank Mandarino ; 2534db2a4165SFrank Mandarino val = snd_soc_read(codec, e->reg); 25353ff3f64bSMark Brown ucontrol->value.enumerated.item[0] 25363ff3f64bSMark Brown = (val >> e->shift_l) & (bitmask - 1); 2537db2a4165SFrank Mandarino if (e->shift_l != e->shift_r) 2538db2a4165SFrank Mandarino ucontrol->value.enumerated.item[1] = 2539db2a4165SFrank Mandarino (val >> e->shift_r) & (bitmask - 1); 2540db2a4165SFrank Mandarino 2541db2a4165SFrank Mandarino return 0; 2542db2a4165SFrank Mandarino } 2543db2a4165SFrank Mandarino EXPORT_SYMBOL_GPL(snd_soc_get_enum_double); 2544db2a4165SFrank Mandarino 2545db2a4165SFrank Mandarino /** 2546db2a4165SFrank Mandarino * snd_soc_put_enum_double - enumerated double mixer put callback 2547db2a4165SFrank Mandarino * @kcontrol: mixer control 2548ac11a2b3SMark Brown * @ucontrol: control element information 2549db2a4165SFrank Mandarino * 2550db2a4165SFrank Mandarino * Callback to set the value of a double enumerated mixer. 2551db2a4165SFrank Mandarino * 2552db2a4165SFrank Mandarino * Returns 0 for success. 2553db2a4165SFrank Mandarino */ 2554db2a4165SFrank Mandarino int snd_soc_put_enum_double(struct snd_kcontrol *kcontrol, 2555db2a4165SFrank Mandarino struct snd_ctl_elem_value *ucontrol) 2556db2a4165SFrank Mandarino { 2557db2a4165SFrank Mandarino struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol); 2558db2a4165SFrank Mandarino struct soc_enum *e = (struct soc_enum *)kcontrol->private_value; 255946f5822fSDaniel Ribeiro unsigned int val; 256046f5822fSDaniel Ribeiro unsigned int mask, bitmask; 2561db2a4165SFrank Mandarino 2562f8ba0b7bSJon Smirl for (bitmask = 1; bitmask < e->max; bitmask <<= 1) 2563db2a4165SFrank Mandarino ; 2564f8ba0b7bSJon Smirl if (ucontrol->value.enumerated.item[0] > e->max - 1) 2565db2a4165SFrank Mandarino return -EINVAL; 2566db2a4165SFrank Mandarino val = ucontrol->value.enumerated.item[0] << e->shift_l; 2567db2a4165SFrank Mandarino mask = (bitmask - 1) << e->shift_l; 2568db2a4165SFrank Mandarino if (e->shift_l != e->shift_r) { 2569f8ba0b7bSJon Smirl if (ucontrol->value.enumerated.item[1] > e->max - 1) 2570db2a4165SFrank Mandarino return -EINVAL; 2571db2a4165SFrank Mandarino val |= ucontrol->value.enumerated.item[1] << e->shift_r; 2572db2a4165SFrank Mandarino mask |= (bitmask - 1) << e->shift_r; 2573db2a4165SFrank Mandarino } 2574db2a4165SFrank Mandarino 25756c508c62SEero Nurkkala return snd_soc_update_bits_locked(codec, e->reg, mask, val); 2576db2a4165SFrank Mandarino } 2577db2a4165SFrank Mandarino EXPORT_SYMBOL_GPL(snd_soc_put_enum_double); 2578db2a4165SFrank Mandarino 2579db2a4165SFrank Mandarino /** 25802e72f8e3SPeter Ujfalusi * snd_soc_get_value_enum_double - semi enumerated double mixer get callback 25812e72f8e3SPeter Ujfalusi * @kcontrol: mixer control 25822e72f8e3SPeter Ujfalusi * @ucontrol: control element information 25832e72f8e3SPeter Ujfalusi * 25842e72f8e3SPeter Ujfalusi * Callback to get the value of a double semi enumerated mixer. 25852e72f8e3SPeter Ujfalusi * 25862e72f8e3SPeter Ujfalusi * Semi enumerated mixer: the enumerated items are referred as values. Can be 25872e72f8e3SPeter Ujfalusi * used for handling bitfield coded enumeration for example. 25882e72f8e3SPeter Ujfalusi * 25892e72f8e3SPeter Ujfalusi * Returns 0 for success. 25902e72f8e3SPeter Ujfalusi */ 25912e72f8e3SPeter Ujfalusi int snd_soc_get_value_enum_double(struct snd_kcontrol *kcontrol, 25922e72f8e3SPeter Ujfalusi struct snd_ctl_elem_value *ucontrol) 25932e72f8e3SPeter Ujfalusi { 25942e72f8e3SPeter Ujfalusi struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol); 259574155556SPeter Ujfalusi struct soc_enum *e = (struct soc_enum *)kcontrol->private_value; 259646f5822fSDaniel Ribeiro unsigned int reg_val, val, mux; 25972e72f8e3SPeter Ujfalusi 25982e72f8e3SPeter Ujfalusi reg_val = snd_soc_read(codec, e->reg); 25992e72f8e3SPeter Ujfalusi val = (reg_val >> e->shift_l) & e->mask; 26002e72f8e3SPeter Ujfalusi for (mux = 0; mux < e->max; mux++) { 26012e72f8e3SPeter Ujfalusi if (val == e->values[mux]) 26022e72f8e3SPeter Ujfalusi break; 26032e72f8e3SPeter Ujfalusi } 26042e72f8e3SPeter Ujfalusi ucontrol->value.enumerated.item[0] = mux; 26052e72f8e3SPeter Ujfalusi if (e->shift_l != e->shift_r) { 26062e72f8e3SPeter Ujfalusi val = (reg_val >> e->shift_r) & e->mask; 26072e72f8e3SPeter Ujfalusi for (mux = 0; mux < e->max; mux++) { 26082e72f8e3SPeter Ujfalusi if (val == e->values[mux]) 26092e72f8e3SPeter Ujfalusi break; 26102e72f8e3SPeter Ujfalusi } 26112e72f8e3SPeter Ujfalusi ucontrol->value.enumerated.item[1] = mux; 26122e72f8e3SPeter Ujfalusi } 26132e72f8e3SPeter Ujfalusi 26142e72f8e3SPeter Ujfalusi return 0; 26152e72f8e3SPeter Ujfalusi } 26162e72f8e3SPeter Ujfalusi EXPORT_SYMBOL_GPL(snd_soc_get_value_enum_double); 26172e72f8e3SPeter Ujfalusi 26182e72f8e3SPeter Ujfalusi /** 26192e72f8e3SPeter Ujfalusi * snd_soc_put_value_enum_double - semi enumerated double mixer put callback 26202e72f8e3SPeter Ujfalusi * @kcontrol: mixer control 26212e72f8e3SPeter Ujfalusi * @ucontrol: control element information 26222e72f8e3SPeter Ujfalusi * 26232e72f8e3SPeter Ujfalusi * Callback to set the value of a double semi enumerated mixer. 26242e72f8e3SPeter Ujfalusi * 26252e72f8e3SPeter Ujfalusi * Semi enumerated mixer: the enumerated items are referred as values. Can be 26262e72f8e3SPeter Ujfalusi * used for handling bitfield coded enumeration for example. 26272e72f8e3SPeter Ujfalusi * 26282e72f8e3SPeter Ujfalusi * Returns 0 for success. 26292e72f8e3SPeter Ujfalusi */ 26302e72f8e3SPeter Ujfalusi int snd_soc_put_value_enum_double(struct snd_kcontrol *kcontrol, 26312e72f8e3SPeter Ujfalusi struct snd_ctl_elem_value *ucontrol) 26322e72f8e3SPeter Ujfalusi { 26332e72f8e3SPeter Ujfalusi struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol); 263474155556SPeter Ujfalusi struct soc_enum *e = (struct soc_enum *)kcontrol->private_value; 263546f5822fSDaniel Ribeiro unsigned int val; 263646f5822fSDaniel Ribeiro unsigned int mask; 26372e72f8e3SPeter Ujfalusi 26382e72f8e3SPeter Ujfalusi if (ucontrol->value.enumerated.item[0] > e->max - 1) 26392e72f8e3SPeter Ujfalusi return -EINVAL; 26402e72f8e3SPeter Ujfalusi val = e->values[ucontrol->value.enumerated.item[0]] << e->shift_l; 26412e72f8e3SPeter Ujfalusi mask = e->mask << e->shift_l; 26422e72f8e3SPeter Ujfalusi if (e->shift_l != e->shift_r) { 26432e72f8e3SPeter Ujfalusi if (ucontrol->value.enumerated.item[1] > e->max - 1) 26442e72f8e3SPeter Ujfalusi return -EINVAL; 26452e72f8e3SPeter Ujfalusi val |= e->values[ucontrol->value.enumerated.item[1]] << e->shift_r; 26462e72f8e3SPeter Ujfalusi mask |= e->mask << e->shift_r; 26472e72f8e3SPeter Ujfalusi } 26482e72f8e3SPeter Ujfalusi 26496c508c62SEero Nurkkala return snd_soc_update_bits_locked(codec, e->reg, mask, val); 26502e72f8e3SPeter Ujfalusi } 26512e72f8e3SPeter Ujfalusi EXPORT_SYMBOL_GPL(snd_soc_put_value_enum_double); 26522e72f8e3SPeter Ujfalusi 26532e72f8e3SPeter Ujfalusi /** 2654db2a4165SFrank Mandarino * snd_soc_info_enum_ext - external enumerated single mixer info callback 2655db2a4165SFrank Mandarino * @kcontrol: mixer control 2656db2a4165SFrank Mandarino * @uinfo: control element information 2657db2a4165SFrank Mandarino * 2658db2a4165SFrank Mandarino * Callback to provide information about an external enumerated 2659db2a4165SFrank Mandarino * single mixer. 2660db2a4165SFrank Mandarino * 2661db2a4165SFrank Mandarino * Returns 0 for success. 2662db2a4165SFrank Mandarino */ 2663db2a4165SFrank Mandarino int snd_soc_info_enum_ext(struct snd_kcontrol *kcontrol, 2664db2a4165SFrank Mandarino struct snd_ctl_elem_info *uinfo) 2665db2a4165SFrank Mandarino { 2666db2a4165SFrank Mandarino struct soc_enum *e = (struct soc_enum *)kcontrol->private_value; 2667db2a4165SFrank Mandarino 2668db2a4165SFrank Mandarino uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED; 2669db2a4165SFrank Mandarino uinfo->count = 1; 2670f8ba0b7bSJon Smirl uinfo->value.enumerated.items = e->max; 2671db2a4165SFrank Mandarino 2672f8ba0b7bSJon Smirl if (uinfo->value.enumerated.item > e->max - 1) 2673f8ba0b7bSJon Smirl uinfo->value.enumerated.item = e->max - 1; 2674db2a4165SFrank Mandarino strcpy(uinfo->value.enumerated.name, 2675db2a4165SFrank Mandarino e->texts[uinfo->value.enumerated.item]); 2676db2a4165SFrank Mandarino return 0; 2677db2a4165SFrank Mandarino } 2678db2a4165SFrank Mandarino EXPORT_SYMBOL_GPL(snd_soc_info_enum_ext); 2679db2a4165SFrank Mandarino 2680db2a4165SFrank Mandarino /** 2681db2a4165SFrank Mandarino * snd_soc_info_volsw_ext - external single mixer info callback 2682db2a4165SFrank Mandarino * @kcontrol: mixer control 2683db2a4165SFrank Mandarino * @uinfo: control element information 2684db2a4165SFrank Mandarino * 2685db2a4165SFrank Mandarino * Callback to provide information about a single external mixer control. 2686db2a4165SFrank Mandarino * 2687db2a4165SFrank Mandarino * Returns 0 for success. 2688db2a4165SFrank Mandarino */ 2689db2a4165SFrank Mandarino int snd_soc_info_volsw_ext(struct snd_kcontrol *kcontrol, 2690db2a4165SFrank Mandarino struct snd_ctl_elem_info *uinfo) 2691db2a4165SFrank Mandarino { 2692a7a4ac86SPhilipp Zabel int max = kcontrol->private_value; 2693db2a4165SFrank Mandarino 2694fd5dfad9SMark Brown if (max == 1 && !strstr(kcontrol->id.name, " Volume")) 2695a7a4ac86SPhilipp Zabel uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN; 2696a7a4ac86SPhilipp Zabel else 2697a7a4ac86SPhilipp Zabel uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER; 2698a7a4ac86SPhilipp Zabel 2699db2a4165SFrank Mandarino uinfo->count = 1; 2700db2a4165SFrank Mandarino uinfo->value.integer.min = 0; 2701a7a4ac86SPhilipp Zabel uinfo->value.integer.max = max; 2702db2a4165SFrank Mandarino return 0; 2703db2a4165SFrank Mandarino } 2704db2a4165SFrank Mandarino EXPORT_SYMBOL_GPL(snd_soc_info_volsw_ext); 2705db2a4165SFrank Mandarino 2706db2a4165SFrank Mandarino /** 2707db2a4165SFrank Mandarino * snd_soc_info_volsw - single mixer info callback 2708db2a4165SFrank Mandarino * @kcontrol: mixer control 2709db2a4165SFrank Mandarino * @uinfo: control element information 2710db2a4165SFrank Mandarino * 2711db2a4165SFrank Mandarino * Callback to provide information about a single mixer control. 2712db2a4165SFrank Mandarino * 2713db2a4165SFrank Mandarino * Returns 0 for success. 2714db2a4165SFrank Mandarino */ 2715db2a4165SFrank Mandarino int snd_soc_info_volsw(struct snd_kcontrol *kcontrol, 2716db2a4165SFrank Mandarino struct snd_ctl_elem_info *uinfo) 2717db2a4165SFrank Mandarino { 27184eaa9819SJon Smirl struct soc_mixer_control *mc = 27194eaa9819SJon Smirl (struct soc_mixer_control *)kcontrol->private_value; 2720d11bb4a9SPeter Ujfalusi int platform_max; 2721762b8df7SMark Brown unsigned int shift = mc->shift; 2722815ecf8dSJon Smirl unsigned int rshift = mc->rshift; 2723db2a4165SFrank Mandarino 2724d11bb4a9SPeter Ujfalusi if (!mc->platform_max) 2725d11bb4a9SPeter Ujfalusi mc->platform_max = mc->max; 2726d11bb4a9SPeter Ujfalusi platform_max = mc->platform_max; 2727d11bb4a9SPeter Ujfalusi 2728d11bb4a9SPeter Ujfalusi if (platform_max == 1 && !strstr(kcontrol->id.name, " Volume")) 2729a7a4ac86SPhilipp Zabel uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN; 2730a7a4ac86SPhilipp Zabel else 2731a7a4ac86SPhilipp Zabel uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER; 2732a7a4ac86SPhilipp Zabel 2733db2a4165SFrank Mandarino uinfo->count = shift == rshift ? 1 : 2; 2734db2a4165SFrank Mandarino uinfo->value.integer.min = 0; 2735d11bb4a9SPeter Ujfalusi uinfo->value.integer.max = platform_max; 2736db2a4165SFrank Mandarino return 0; 2737db2a4165SFrank Mandarino } 2738db2a4165SFrank Mandarino EXPORT_SYMBOL_GPL(snd_soc_info_volsw); 2739db2a4165SFrank Mandarino 2740db2a4165SFrank Mandarino /** 2741db2a4165SFrank Mandarino * snd_soc_get_volsw - single mixer get callback 2742db2a4165SFrank Mandarino * @kcontrol: mixer control 2743ac11a2b3SMark Brown * @ucontrol: control element information 2744db2a4165SFrank Mandarino * 2745db2a4165SFrank Mandarino * Callback to get the value of a single mixer control. 2746db2a4165SFrank Mandarino * 2747db2a4165SFrank Mandarino * Returns 0 for success. 2748db2a4165SFrank Mandarino */ 2749db2a4165SFrank Mandarino int snd_soc_get_volsw(struct snd_kcontrol *kcontrol, 2750db2a4165SFrank Mandarino struct snd_ctl_elem_value *ucontrol) 2751db2a4165SFrank Mandarino { 27524eaa9819SJon Smirl struct soc_mixer_control *mc = 27534eaa9819SJon Smirl (struct soc_mixer_control *)kcontrol->private_value; 2754db2a4165SFrank Mandarino struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol); 2755815ecf8dSJon Smirl unsigned int reg = mc->reg; 2756815ecf8dSJon Smirl unsigned int shift = mc->shift; 2757815ecf8dSJon Smirl unsigned int rshift = mc->rshift; 27584eaa9819SJon Smirl int max = mc->max; 2759815ecf8dSJon Smirl unsigned int mask = (1 << fls(max)) - 1; 2760815ecf8dSJon Smirl unsigned int invert = mc->invert; 2761db2a4165SFrank Mandarino 2762db2a4165SFrank Mandarino ucontrol->value.integer.value[0] = 2763db2a4165SFrank Mandarino (snd_soc_read(codec, reg) >> shift) & mask; 2764db2a4165SFrank Mandarino if (shift != rshift) 2765db2a4165SFrank Mandarino ucontrol->value.integer.value[1] = 2766db2a4165SFrank Mandarino (snd_soc_read(codec, reg) >> rshift) & mask; 2767db2a4165SFrank Mandarino if (invert) { 2768db2a4165SFrank Mandarino ucontrol->value.integer.value[0] = 2769a7a4ac86SPhilipp Zabel max - ucontrol->value.integer.value[0]; 2770db2a4165SFrank Mandarino if (shift != rshift) 2771db2a4165SFrank Mandarino ucontrol->value.integer.value[1] = 2772a7a4ac86SPhilipp Zabel max - ucontrol->value.integer.value[1]; 2773db2a4165SFrank Mandarino } 2774db2a4165SFrank Mandarino 2775db2a4165SFrank Mandarino return 0; 2776db2a4165SFrank Mandarino } 2777db2a4165SFrank Mandarino EXPORT_SYMBOL_GPL(snd_soc_get_volsw); 2778db2a4165SFrank Mandarino 2779db2a4165SFrank Mandarino /** 2780db2a4165SFrank Mandarino * snd_soc_put_volsw - single mixer put callback 2781db2a4165SFrank Mandarino * @kcontrol: mixer control 2782ac11a2b3SMark Brown * @ucontrol: control element information 2783db2a4165SFrank Mandarino * 2784db2a4165SFrank Mandarino * Callback to set the value of a single mixer control. 2785db2a4165SFrank Mandarino * 2786db2a4165SFrank Mandarino * Returns 0 for success. 2787db2a4165SFrank Mandarino */ 2788db2a4165SFrank Mandarino int snd_soc_put_volsw(struct snd_kcontrol *kcontrol, 2789db2a4165SFrank Mandarino struct snd_ctl_elem_value *ucontrol) 2790db2a4165SFrank Mandarino { 27914eaa9819SJon Smirl struct soc_mixer_control *mc = 27924eaa9819SJon Smirl (struct soc_mixer_control *)kcontrol->private_value; 2793db2a4165SFrank Mandarino struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol); 2794815ecf8dSJon Smirl unsigned int reg = mc->reg; 2795815ecf8dSJon Smirl unsigned int shift = mc->shift; 2796815ecf8dSJon Smirl unsigned int rshift = mc->rshift; 27974eaa9819SJon Smirl int max = mc->max; 2798815ecf8dSJon Smirl unsigned int mask = (1 << fls(max)) - 1; 2799815ecf8dSJon Smirl unsigned int invert = mc->invert; 280046f5822fSDaniel Ribeiro unsigned int val, val2, val_mask; 2801db2a4165SFrank Mandarino 2802db2a4165SFrank Mandarino val = (ucontrol->value.integer.value[0] & mask); 2803db2a4165SFrank Mandarino if (invert) 2804a7a4ac86SPhilipp Zabel val = max - val; 2805db2a4165SFrank Mandarino val_mask = mask << shift; 2806db2a4165SFrank Mandarino val = val << shift; 2807db2a4165SFrank Mandarino if (shift != rshift) { 2808db2a4165SFrank Mandarino val2 = (ucontrol->value.integer.value[1] & mask); 2809db2a4165SFrank Mandarino if (invert) 2810a7a4ac86SPhilipp Zabel val2 = max - val2; 2811db2a4165SFrank Mandarino val_mask |= mask << rshift; 2812db2a4165SFrank Mandarino val |= val2 << rshift; 2813db2a4165SFrank Mandarino } 28146c508c62SEero Nurkkala return snd_soc_update_bits_locked(codec, reg, val_mask, val); 2815db2a4165SFrank Mandarino } 2816db2a4165SFrank Mandarino EXPORT_SYMBOL_GPL(snd_soc_put_volsw); 2817db2a4165SFrank Mandarino 2818db2a4165SFrank Mandarino /** 2819db2a4165SFrank Mandarino * snd_soc_info_volsw_2r - double mixer info callback 2820db2a4165SFrank Mandarino * @kcontrol: mixer control 2821db2a4165SFrank Mandarino * @uinfo: control element information 2822db2a4165SFrank Mandarino * 2823db2a4165SFrank Mandarino * Callback to provide information about a double mixer control that 2824db2a4165SFrank Mandarino * spans 2 codec registers. 2825db2a4165SFrank Mandarino * 2826db2a4165SFrank Mandarino * Returns 0 for success. 2827db2a4165SFrank Mandarino */ 2828db2a4165SFrank Mandarino int snd_soc_info_volsw_2r(struct snd_kcontrol *kcontrol, 2829db2a4165SFrank Mandarino struct snd_ctl_elem_info *uinfo) 2830db2a4165SFrank Mandarino { 28314eaa9819SJon Smirl struct soc_mixer_control *mc = 28324eaa9819SJon Smirl (struct soc_mixer_control *)kcontrol->private_value; 2833d11bb4a9SPeter Ujfalusi int platform_max; 2834db2a4165SFrank Mandarino 2835d11bb4a9SPeter Ujfalusi if (!mc->platform_max) 2836d11bb4a9SPeter Ujfalusi mc->platform_max = mc->max; 2837d11bb4a9SPeter Ujfalusi platform_max = mc->platform_max; 2838d11bb4a9SPeter Ujfalusi 2839d11bb4a9SPeter Ujfalusi if (platform_max == 1 && !strstr(kcontrol->id.name, " Volume")) 2840a7a4ac86SPhilipp Zabel uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN; 2841a7a4ac86SPhilipp Zabel else 2842a7a4ac86SPhilipp Zabel uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER; 2843a7a4ac86SPhilipp Zabel 2844db2a4165SFrank Mandarino uinfo->count = 2; 2845db2a4165SFrank Mandarino uinfo->value.integer.min = 0; 2846d11bb4a9SPeter Ujfalusi uinfo->value.integer.max = platform_max; 2847db2a4165SFrank Mandarino return 0; 2848db2a4165SFrank Mandarino } 2849db2a4165SFrank Mandarino EXPORT_SYMBOL_GPL(snd_soc_info_volsw_2r); 2850db2a4165SFrank Mandarino 2851db2a4165SFrank Mandarino /** 2852db2a4165SFrank Mandarino * snd_soc_get_volsw_2r - double mixer get callback 2853db2a4165SFrank Mandarino * @kcontrol: mixer control 2854ac11a2b3SMark Brown * @ucontrol: control element information 2855db2a4165SFrank Mandarino * 2856db2a4165SFrank Mandarino * Callback to get the value of a double mixer control that spans 2 registers. 2857db2a4165SFrank Mandarino * 2858db2a4165SFrank Mandarino * Returns 0 for success. 2859db2a4165SFrank Mandarino */ 2860db2a4165SFrank Mandarino int snd_soc_get_volsw_2r(struct snd_kcontrol *kcontrol, 2861db2a4165SFrank Mandarino struct snd_ctl_elem_value *ucontrol) 2862db2a4165SFrank Mandarino { 28634eaa9819SJon Smirl struct soc_mixer_control *mc = 28644eaa9819SJon Smirl (struct soc_mixer_control *)kcontrol->private_value; 2865db2a4165SFrank Mandarino struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol); 2866815ecf8dSJon Smirl unsigned int reg = mc->reg; 2867815ecf8dSJon Smirl unsigned int reg2 = mc->rreg; 2868815ecf8dSJon Smirl unsigned int shift = mc->shift; 28694eaa9819SJon Smirl int max = mc->max; 2870815ecf8dSJon Smirl unsigned int mask = (1 << fls(max)) - 1; 2871815ecf8dSJon Smirl unsigned int invert = mc->invert; 2872db2a4165SFrank Mandarino 2873db2a4165SFrank Mandarino ucontrol->value.integer.value[0] = 2874db2a4165SFrank Mandarino (snd_soc_read(codec, reg) >> shift) & mask; 2875db2a4165SFrank Mandarino ucontrol->value.integer.value[1] = 2876db2a4165SFrank Mandarino (snd_soc_read(codec, reg2) >> shift) & mask; 2877db2a4165SFrank Mandarino if (invert) { 2878db2a4165SFrank Mandarino ucontrol->value.integer.value[0] = 2879a7a4ac86SPhilipp Zabel max - ucontrol->value.integer.value[0]; 2880db2a4165SFrank Mandarino ucontrol->value.integer.value[1] = 2881a7a4ac86SPhilipp Zabel max - ucontrol->value.integer.value[1]; 2882db2a4165SFrank Mandarino } 2883db2a4165SFrank Mandarino 2884db2a4165SFrank Mandarino return 0; 2885db2a4165SFrank Mandarino } 2886db2a4165SFrank Mandarino EXPORT_SYMBOL_GPL(snd_soc_get_volsw_2r); 2887db2a4165SFrank Mandarino 2888db2a4165SFrank Mandarino /** 2889db2a4165SFrank Mandarino * snd_soc_put_volsw_2r - double mixer set callback 2890db2a4165SFrank Mandarino * @kcontrol: mixer control 2891ac11a2b3SMark Brown * @ucontrol: control element information 2892db2a4165SFrank Mandarino * 2893db2a4165SFrank Mandarino * Callback to set the value of a double mixer control that spans 2 registers. 2894db2a4165SFrank Mandarino * 2895db2a4165SFrank Mandarino * Returns 0 for success. 2896db2a4165SFrank Mandarino */ 2897db2a4165SFrank Mandarino int snd_soc_put_volsw_2r(struct snd_kcontrol *kcontrol, 2898db2a4165SFrank Mandarino struct snd_ctl_elem_value *ucontrol) 2899db2a4165SFrank Mandarino { 29004eaa9819SJon Smirl struct soc_mixer_control *mc = 29014eaa9819SJon Smirl (struct soc_mixer_control *)kcontrol->private_value; 2902db2a4165SFrank Mandarino struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol); 2903815ecf8dSJon Smirl unsigned int reg = mc->reg; 2904815ecf8dSJon Smirl unsigned int reg2 = mc->rreg; 2905815ecf8dSJon Smirl unsigned int shift = mc->shift; 29064eaa9819SJon Smirl int max = mc->max; 2907815ecf8dSJon Smirl unsigned int mask = (1 << fls(max)) - 1; 2908815ecf8dSJon Smirl unsigned int invert = mc->invert; 2909db2a4165SFrank Mandarino int err; 291046f5822fSDaniel Ribeiro unsigned int val, val2, val_mask; 2911db2a4165SFrank Mandarino 2912db2a4165SFrank Mandarino val_mask = mask << shift; 2913db2a4165SFrank Mandarino val = (ucontrol->value.integer.value[0] & mask); 2914db2a4165SFrank Mandarino val2 = (ucontrol->value.integer.value[1] & mask); 2915db2a4165SFrank Mandarino 2916db2a4165SFrank Mandarino if (invert) { 2917a7a4ac86SPhilipp Zabel val = max - val; 2918a7a4ac86SPhilipp Zabel val2 = max - val2; 2919db2a4165SFrank Mandarino } 2920db2a4165SFrank Mandarino 2921db2a4165SFrank Mandarino val = val << shift; 2922db2a4165SFrank Mandarino val2 = val2 << shift; 2923db2a4165SFrank Mandarino 29246c508c62SEero Nurkkala err = snd_soc_update_bits_locked(codec, reg, val_mask, val); 29253ff3f64bSMark Brown if (err < 0) 2926db2a4165SFrank Mandarino return err; 2927db2a4165SFrank Mandarino 29286c508c62SEero Nurkkala err = snd_soc_update_bits_locked(codec, reg2, val_mask, val2); 2929db2a4165SFrank Mandarino return err; 2930db2a4165SFrank Mandarino } 2931db2a4165SFrank Mandarino EXPORT_SYMBOL_GPL(snd_soc_put_volsw_2r); 2932db2a4165SFrank Mandarino 2933e13ac2e9SMark Brown /** 2934e13ac2e9SMark Brown * snd_soc_info_volsw_s8 - signed mixer info callback 2935e13ac2e9SMark Brown * @kcontrol: mixer control 2936e13ac2e9SMark Brown * @uinfo: control element information 2937e13ac2e9SMark Brown * 2938e13ac2e9SMark Brown * Callback to provide information about a signed mixer control. 2939e13ac2e9SMark Brown * 2940e13ac2e9SMark Brown * Returns 0 for success. 2941e13ac2e9SMark Brown */ 2942e13ac2e9SMark Brown int snd_soc_info_volsw_s8(struct snd_kcontrol *kcontrol, 2943e13ac2e9SMark Brown struct snd_ctl_elem_info *uinfo) 2944e13ac2e9SMark Brown { 29454eaa9819SJon Smirl struct soc_mixer_control *mc = 29464eaa9819SJon Smirl (struct soc_mixer_control *)kcontrol->private_value; 2947d11bb4a9SPeter Ujfalusi int platform_max; 29484eaa9819SJon Smirl int min = mc->min; 2949e13ac2e9SMark Brown 2950d11bb4a9SPeter Ujfalusi if (!mc->platform_max) 2951d11bb4a9SPeter Ujfalusi mc->platform_max = mc->max; 2952d11bb4a9SPeter Ujfalusi platform_max = mc->platform_max; 2953d11bb4a9SPeter Ujfalusi 2954e13ac2e9SMark Brown uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER; 2955e13ac2e9SMark Brown uinfo->count = 2; 2956e13ac2e9SMark Brown uinfo->value.integer.min = 0; 2957d11bb4a9SPeter Ujfalusi uinfo->value.integer.max = platform_max - min; 2958e13ac2e9SMark Brown return 0; 2959e13ac2e9SMark Brown } 2960e13ac2e9SMark Brown EXPORT_SYMBOL_GPL(snd_soc_info_volsw_s8); 2961e13ac2e9SMark Brown 2962e13ac2e9SMark Brown /** 2963e13ac2e9SMark Brown * snd_soc_get_volsw_s8 - signed mixer get callback 2964e13ac2e9SMark Brown * @kcontrol: mixer control 2965ac11a2b3SMark Brown * @ucontrol: control element information 2966e13ac2e9SMark Brown * 2967e13ac2e9SMark Brown * Callback to get the value of a signed mixer control. 2968e13ac2e9SMark Brown * 2969e13ac2e9SMark Brown * Returns 0 for success. 2970e13ac2e9SMark Brown */ 2971e13ac2e9SMark Brown int snd_soc_get_volsw_s8(struct snd_kcontrol *kcontrol, 2972e13ac2e9SMark Brown struct snd_ctl_elem_value *ucontrol) 2973e13ac2e9SMark Brown { 29744eaa9819SJon Smirl struct soc_mixer_control *mc = 29754eaa9819SJon Smirl (struct soc_mixer_control *)kcontrol->private_value; 2976e13ac2e9SMark Brown struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol); 2977815ecf8dSJon Smirl unsigned int reg = mc->reg; 29784eaa9819SJon Smirl int min = mc->min; 2979e13ac2e9SMark Brown int val = snd_soc_read(codec, reg); 2980e13ac2e9SMark Brown 2981e13ac2e9SMark Brown ucontrol->value.integer.value[0] = 2982e13ac2e9SMark Brown ((signed char)(val & 0xff))-min; 2983e13ac2e9SMark Brown ucontrol->value.integer.value[1] = 2984e13ac2e9SMark Brown ((signed char)((val >> 8) & 0xff))-min; 2985e13ac2e9SMark Brown return 0; 2986e13ac2e9SMark Brown } 2987e13ac2e9SMark Brown EXPORT_SYMBOL_GPL(snd_soc_get_volsw_s8); 2988e13ac2e9SMark Brown 2989e13ac2e9SMark Brown /** 2990e13ac2e9SMark Brown * snd_soc_put_volsw_sgn - signed mixer put callback 2991e13ac2e9SMark Brown * @kcontrol: mixer control 2992ac11a2b3SMark Brown * @ucontrol: control element information 2993e13ac2e9SMark Brown * 2994e13ac2e9SMark Brown * Callback to set the value of a signed mixer control. 2995e13ac2e9SMark Brown * 2996e13ac2e9SMark Brown * Returns 0 for success. 2997e13ac2e9SMark Brown */ 2998e13ac2e9SMark Brown int snd_soc_put_volsw_s8(struct snd_kcontrol *kcontrol, 2999e13ac2e9SMark Brown struct snd_ctl_elem_value *ucontrol) 3000e13ac2e9SMark Brown { 30014eaa9819SJon Smirl struct soc_mixer_control *mc = 30024eaa9819SJon Smirl (struct soc_mixer_control *)kcontrol->private_value; 3003e13ac2e9SMark Brown struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol); 3004815ecf8dSJon Smirl unsigned int reg = mc->reg; 30054eaa9819SJon Smirl int min = mc->min; 300646f5822fSDaniel Ribeiro unsigned int val; 3007e13ac2e9SMark Brown 3008e13ac2e9SMark Brown val = (ucontrol->value.integer.value[0]+min) & 0xff; 3009e13ac2e9SMark Brown val |= ((ucontrol->value.integer.value[1]+min) & 0xff) << 8; 3010e13ac2e9SMark Brown 30116c508c62SEero Nurkkala return snd_soc_update_bits_locked(codec, reg, 0xffff, val); 3012e13ac2e9SMark Brown } 3013e13ac2e9SMark Brown EXPORT_SYMBOL_GPL(snd_soc_put_volsw_s8); 3014e13ac2e9SMark Brown 30158c6529dbSLiam Girdwood /** 3016637d3847SPeter Ujfalusi * snd_soc_limit_volume - Set new limit to an existing volume control. 3017637d3847SPeter Ujfalusi * 3018637d3847SPeter Ujfalusi * @codec: where to look for the control 3019637d3847SPeter Ujfalusi * @name: Name of the control 3020637d3847SPeter Ujfalusi * @max: new maximum limit 3021637d3847SPeter Ujfalusi * 3022637d3847SPeter Ujfalusi * Return 0 for success, else error. 3023637d3847SPeter Ujfalusi */ 3024637d3847SPeter Ujfalusi int snd_soc_limit_volume(struct snd_soc_codec *codec, 3025637d3847SPeter Ujfalusi const char *name, int max) 3026637d3847SPeter Ujfalusi { 3027f0fba2adSLiam Girdwood struct snd_card *card = codec->card->snd_card; 3028637d3847SPeter Ujfalusi struct snd_kcontrol *kctl; 3029637d3847SPeter Ujfalusi struct soc_mixer_control *mc; 3030637d3847SPeter Ujfalusi int found = 0; 3031637d3847SPeter Ujfalusi int ret = -EINVAL; 3032637d3847SPeter Ujfalusi 3033637d3847SPeter Ujfalusi /* Sanity check for name and max */ 3034637d3847SPeter Ujfalusi if (unlikely(!name || max <= 0)) 3035637d3847SPeter Ujfalusi return -EINVAL; 3036637d3847SPeter Ujfalusi 3037637d3847SPeter Ujfalusi list_for_each_entry(kctl, &card->controls, list) { 3038637d3847SPeter Ujfalusi if (!strncmp(kctl->id.name, name, sizeof(kctl->id.name))) { 3039637d3847SPeter Ujfalusi found = 1; 3040637d3847SPeter Ujfalusi break; 3041637d3847SPeter Ujfalusi } 3042637d3847SPeter Ujfalusi } 3043637d3847SPeter Ujfalusi if (found) { 3044637d3847SPeter Ujfalusi mc = (struct soc_mixer_control *)kctl->private_value; 3045637d3847SPeter Ujfalusi if (max <= mc->max) { 3046d11bb4a9SPeter Ujfalusi mc->platform_max = max; 3047637d3847SPeter Ujfalusi ret = 0; 3048637d3847SPeter Ujfalusi } 3049637d3847SPeter Ujfalusi } 3050637d3847SPeter Ujfalusi return ret; 3051637d3847SPeter Ujfalusi } 3052637d3847SPeter Ujfalusi EXPORT_SYMBOL_GPL(snd_soc_limit_volume); 3053637d3847SPeter Ujfalusi 3054637d3847SPeter Ujfalusi /** 3055b6f4bb38Sapatard@mandriva.com * snd_soc_info_volsw_2r_sx - double with tlv and variable data size 3056b6f4bb38Sapatard@mandriva.com * mixer info callback 3057b6f4bb38Sapatard@mandriva.com * @kcontrol: mixer control 3058b6f4bb38Sapatard@mandriva.com * @uinfo: control element information 3059b6f4bb38Sapatard@mandriva.com * 3060b6f4bb38Sapatard@mandriva.com * Returns 0 for success. 3061b6f4bb38Sapatard@mandriva.com */ 3062b6f4bb38Sapatard@mandriva.com int snd_soc_info_volsw_2r_sx(struct snd_kcontrol *kcontrol, 3063b6f4bb38Sapatard@mandriva.com struct snd_ctl_elem_info *uinfo) 3064b6f4bb38Sapatard@mandriva.com { 3065b6f4bb38Sapatard@mandriva.com struct soc_mixer_control *mc = 3066b6f4bb38Sapatard@mandriva.com (struct soc_mixer_control *)kcontrol->private_value; 3067b6f4bb38Sapatard@mandriva.com int max = mc->max; 3068b6f4bb38Sapatard@mandriva.com int min = mc->min; 3069b6f4bb38Sapatard@mandriva.com 3070b6f4bb38Sapatard@mandriva.com uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER; 3071b6f4bb38Sapatard@mandriva.com uinfo->count = 2; 3072b6f4bb38Sapatard@mandriva.com uinfo->value.integer.min = 0; 3073b6f4bb38Sapatard@mandriva.com uinfo->value.integer.max = max-min; 3074b6f4bb38Sapatard@mandriva.com 3075b6f4bb38Sapatard@mandriva.com return 0; 3076b6f4bb38Sapatard@mandriva.com } 3077b6f4bb38Sapatard@mandriva.com EXPORT_SYMBOL_GPL(snd_soc_info_volsw_2r_sx); 3078b6f4bb38Sapatard@mandriva.com 3079b6f4bb38Sapatard@mandriva.com /** 3080b6f4bb38Sapatard@mandriva.com * snd_soc_get_volsw_2r_sx - double with tlv and variable data size 3081b6f4bb38Sapatard@mandriva.com * mixer get callback 3082b6f4bb38Sapatard@mandriva.com * @kcontrol: mixer control 3083b6f4bb38Sapatard@mandriva.com * @uinfo: control element information 3084b6f4bb38Sapatard@mandriva.com * 3085b6f4bb38Sapatard@mandriva.com * Returns 0 for success. 3086b6f4bb38Sapatard@mandriva.com */ 3087b6f4bb38Sapatard@mandriva.com int snd_soc_get_volsw_2r_sx(struct snd_kcontrol *kcontrol, 3088b6f4bb38Sapatard@mandriva.com struct snd_ctl_elem_value *ucontrol) 3089b6f4bb38Sapatard@mandriva.com { 3090b6f4bb38Sapatard@mandriva.com struct soc_mixer_control *mc = 3091b6f4bb38Sapatard@mandriva.com (struct soc_mixer_control *)kcontrol->private_value; 3092b6f4bb38Sapatard@mandriva.com struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol); 3093b6f4bb38Sapatard@mandriva.com unsigned int mask = (1<<mc->shift)-1; 3094b6f4bb38Sapatard@mandriva.com int min = mc->min; 3095b6f4bb38Sapatard@mandriva.com int val = snd_soc_read(codec, mc->reg) & mask; 3096b6f4bb38Sapatard@mandriva.com int valr = snd_soc_read(codec, mc->rreg) & mask; 3097b6f4bb38Sapatard@mandriva.com 309820630c7fSStuart Longland ucontrol->value.integer.value[0] = ((val & 0xff)-min) & mask; 309920630c7fSStuart Longland ucontrol->value.integer.value[1] = ((valr & 0xff)-min) & mask; 3100b6f4bb38Sapatard@mandriva.com return 0; 3101b6f4bb38Sapatard@mandriva.com } 3102b6f4bb38Sapatard@mandriva.com EXPORT_SYMBOL_GPL(snd_soc_get_volsw_2r_sx); 3103b6f4bb38Sapatard@mandriva.com 3104b6f4bb38Sapatard@mandriva.com /** 3105b6f4bb38Sapatard@mandriva.com * snd_soc_put_volsw_2r_sx - double with tlv and variable data size 3106b6f4bb38Sapatard@mandriva.com * mixer put callback 3107b6f4bb38Sapatard@mandriva.com * @kcontrol: mixer control 3108b6f4bb38Sapatard@mandriva.com * @uinfo: control element information 3109b6f4bb38Sapatard@mandriva.com * 3110b6f4bb38Sapatard@mandriva.com * Returns 0 for success. 3111b6f4bb38Sapatard@mandriva.com */ 3112b6f4bb38Sapatard@mandriva.com int snd_soc_put_volsw_2r_sx(struct snd_kcontrol *kcontrol, 3113b6f4bb38Sapatard@mandriva.com struct snd_ctl_elem_value *ucontrol) 3114b6f4bb38Sapatard@mandriva.com { 3115b6f4bb38Sapatard@mandriva.com struct soc_mixer_control *mc = 3116b6f4bb38Sapatard@mandriva.com (struct soc_mixer_control *)kcontrol->private_value; 3117b6f4bb38Sapatard@mandriva.com struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol); 3118b6f4bb38Sapatard@mandriva.com unsigned int mask = (1<<mc->shift)-1; 3119b6f4bb38Sapatard@mandriva.com int min = mc->min; 3120b6f4bb38Sapatard@mandriva.com int ret; 3121b6f4bb38Sapatard@mandriva.com unsigned int val, valr, oval, ovalr; 3122b6f4bb38Sapatard@mandriva.com 3123b6f4bb38Sapatard@mandriva.com val = ((ucontrol->value.integer.value[0]+min) & 0xff); 3124b6f4bb38Sapatard@mandriva.com val &= mask; 3125b6f4bb38Sapatard@mandriva.com valr = ((ucontrol->value.integer.value[1]+min) & 0xff); 3126b6f4bb38Sapatard@mandriva.com valr &= mask; 3127b6f4bb38Sapatard@mandriva.com 3128b6f4bb38Sapatard@mandriva.com oval = snd_soc_read(codec, mc->reg) & mask; 3129b6f4bb38Sapatard@mandriva.com ovalr = snd_soc_read(codec, mc->rreg) & mask; 3130b6f4bb38Sapatard@mandriva.com 3131b6f4bb38Sapatard@mandriva.com ret = 0; 3132b6f4bb38Sapatard@mandriva.com if (oval != val) { 3133b6f4bb38Sapatard@mandriva.com ret = snd_soc_write(codec, mc->reg, val); 3134b6f4bb38Sapatard@mandriva.com if (ret < 0) 3135f1df5aecSMark Brown return ret; 3136b6f4bb38Sapatard@mandriva.com } 3137b6f4bb38Sapatard@mandriva.com if (ovalr != valr) { 3138b6f4bb38Sapatard@mandriva.com ret = snd_soc_write(codec, mc->rreg, valr); 3139b6f4bb38Sapatard@mandriva.com if (ret < 0) 3140f1df5aecSMark Brown return ret; 3141b6f4bb38Sapatard@mandriva.com } 3142b6f4bb38Sapatard@mandriva.com 3143b6f4bb38Sapatard@mandriva.com return 0; 3144b6f4bb38Sapatard@mandriva.com } 3145b6f4bb38Sapatard@mandriva.com EXPORT_SYMBOL_GPL(snd_soc_put_volsw_2r_sx); 3146b6f4bb38Sapatard@mandriva.com 3147b6f4bb38Sapatard@mandriva.com /** 31488c6529dbSLiam Girdwood * snd_soc_dai_set_sysclk - configure DAI system or master clock. 31498c6529dbSLiam Girdwood * @dai: DAI 31508c6529dbSLiam Girdwood * @clk_id: DAI specific clock ID 31518c6529dbSLiam Girdwood * @freq: new clock frequency in Hz 31528c6529dbSLiam Girdwood * @dir: new clock direction - input/output. 31538c6529dbSLiam Girdwood * 31548c6529dbSLiam Girdwood * Configures the DAI master (MCLK) or system (SYSCLK) clocking. 31558c6529dbSLiam Girdwood */ 31568c6529dbSLiam Girdwood int snd_soc_dai_set_sysclk(struct snd_soc_dai *dai, int clk_id, 31578c6529dbSLiam Girdwood unsigned int freq, int dir) 31588c6529dbSLiam Girdwood { 3159f0fba2adSLiam Girdwood if (dai->driver && dai->driver->ops->set_sysclk) 3160f0fba2adSLiam Girdwood return dai->driver->ops->set_sysclk(dai, clk_id, freq, dir); 3161ec4ee52aSMark Brown else if (dai->codec && dai->codec->driver->set_sysclk) 3162ec4ee52aSMark Brown return dai->codec->driver->set_sysclk(dai->codec, clk_id, 3163ec4ee52aSMark Brown freq, dir); 31648c6529dbSLiam Girdwood else 31658c6529dbSLiam Girdwood return -EINVAL; 31668c6529dbSLiam Girdwood } 31678c6529dbSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_dai_set_sysclk); 31688c6529dbSLiam Girdwood 31698c6529dbSLiam Girdwood /** 3170ec4ee52aSMark Brown * snd_soc_codec_set_sysclk - configure CODEC system or master clock. 3171ec4ee52aSMark Brown * @codec: CODEC 3172ec4ee52aSMark Brown * @clk_id: DAI specific clock ID 3173ec4ee52aSMark Brown * @freq: new clock frequency in Hz 3174ec4ee52aSMark Brown * @dir: new clock direction - input/output. 3175ec4ee52aSMark Brown * 3176ec4ee52aSMark Brown * Configures the CODEC master (MCLK) or system (SYSCLK) clocking. 3177ec4ee52aSMark Brown */ 3178ec4ee52aSMark Brown int snd_soc_codec_set_sysclk(struct snd_soc_codec *codec, int clk_id, 3179ec4ee52aSMark Brown unsigned int freq, int dir) 3180ec4ee52aSMark Brown { 3181ec4ee52aSMark Brown if (codec->driver->set_sysclk) 3182ec4ee52aSMark Brown return codec->driver->set_sysclk(codec, clk_id, freq, dir); 3183ec4ee52aSMark Brown else 3184ec4ee52aSMark Brown return -EINVAL; 3185ec4ee52aSMark Brown } 3186ec4ee52aSMark Brown EXPORT_SYMBOL_GPL(snd_soc_codec_set_sysclk); 3187ec4ee52aSMark Brown 3188ec4ee52aSMark Brown /** 31898c6529dbSLiam Girdwood * snd_soc_dai_set_clkdiv - configure DAI clock dividers. 31908c6529dbSLiam Girdwood * @dai: DAI 3191ac11a2b3SMark Brown * @div_id: DAI specific clock divider ID 31928c6529dbSLiam Girdwood * @div: new clock divisor. 31938c6529dbSLiam Girdwood * 31948c6529dbSLiam Girdwood * Configures the clock dividers. This is used to derive the best DAI bit and 31958c6529dbSLiam Girdwood * frame clocks from the system or master clock. It's best to set the DAI bit 31968c6529dbSLiam Girdwood * and frame clocks as low as possible to save system power. 31978c6529dbSLiam Girdwood */ 31988c6529dbSLiam Girdwood int snd_soc_dai_set_clkdiv(struct snd_soc_dai *dai, 31998c6529dbSLiam Girdwood int div_id, int div) 32008c6529dbSLiam Girdwood { 3201f0fba2adSLiam Girdwood if (dai->driver && dai->driver->ops->set_clkdiv) 3202f0fba2adSLiam Girdwood return dai->driver->ops->set_clkdiv(dai, div_id, div); 32038c6529dbSLiam Girdwood else 32048c6529dbSLiam Girdwood return -EINVAL; 32058c6529dbSLiam Girdwood } 32068c6529dbSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_dai_set_clkdiv); 32078c6529dbSLiam Girdwood 32088c6529dbSLiam Girdwood /** 32098c6529dbSLiam Girdwood * snd_soc_dai_set_pll - configure DAI PLL. 32108c6529dbSLiam Girdwood * @dai: DAI 32118c6529dbSLiam Girdwood * @pll_id: DAI specific PLL ID 321285488037SMark Brown * @source: DAI specific source for the PLL 32138c6529dbSLiam Girdwood * @freq_in: PLL input clock frequency in Hz 32148c6529dbSLiam Girdwood * @freq_out: requested PLL output clock frequency in Hz 32158c6529dbSLiam Girdwood * 32168c6529dbSLiam Girdwood * Configures and enables PLL to generate output clock based on input clock. 32178c6529dbSLiam Girdwood */ 321885488037SMark Brown int snd_soc_dai_set_pll(struct snd_soc_dai *dai, int pll_id, int source, 321985488037SMark Brown unsigned int freq_in, unsigned int freq_out) 32208c6529dbSLiam Girdwood { 3221f0fba2adSLiam Girdwood if (dai->driver && dai->driver->ops->set_pll) 3222f0fba2adSLiam Girdwood return dai->driver->ops->set_pll(dai, pll_id, source, 322385488037SMark Brown freq_in, freq_out); 3224ec4ee52aSMark Brown else if (dai->codec && dai->codec->driver->set_pll) 3225ec4ee52aSMark Brown return dai->codec->driver->set_pll(dai->codec, pll_id, source, 3226ec4ee52aSMark Brown freq_in, freq_out); 32278c6529dbSLiam Girdwood else 32288c6529dbSLiam Girdwood return -EINVAL; 32298c6529dbSLiam Girdwood } 32308c6529dbSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_dai_set_pll); 32318c6529dbSLiam Girdwood 3232ec4ee52aSMark Brown /* 3233ec4ee52aSMark Brown * snd_soc_codec_set_pll - configure codec PLL. 3234ec4ee52aSMark Brown * @codec: CODEC 3235ec4ee52aSMark Brown * @pll_id: DAI specific PLL ID 3236ec4ee52aSMark Brown * @source: DAI specific source for the PLL 3237ec4ee52aSMark Brown * @freq_in: PLL input clock frequency in Hz 3238ec4ee52aSMark Brown * @freq_out: requested PLL output clock frequency in Hz 3239ec4ee52aSMark Brown * 3240ec4ee52aSMark Brown * Configures and enables PLL to generate output clock based on input clock. 3241ec4ee52aSMark Brown */ 3242ec4ee52aSMark Brown int snd_soc_codec_set_pll(struct snd_soc_codec *codec, int pll_id, int source, 3243ec4ee52aSMark Brown unsigned int freq_in, unsigned int freq_out) 3244ec4ee52aSMark Brown { 3245ec4ee52aSMark Brown if (codec->driver->set_pll) 3246ec4ee52aSMark Brown return codec->driver->set_pll(codec, pll_id, source, 3247ec4ee52aSMark Brown freq_in, freq_out); 3248ec4ee52aSMark Brown else 3249ec4ee52aSMark Brown return -EINVAL; 3250ec4ee52aSMark Brown } 3251ec4ee52aSMark Brown EXPORT_SYMBOL_GPL(snd_soc_codec_set_pll); 3252ec4ee52aSMark Brown 32538c6529dbSLiam Girdwood /** 32548c6529dbSLiam Girdwood * snd_soc_dai_set_fmt - configure DAI hardware audio format. 32558c6529dbSLiam Girdwood * @dai: DAI 32568c6529dbSLiam Girdwood * @fmt: SND_SOC_DAIFMT_ format value. 32578c6529dbSLiam Girdwood * 32588c6529dbSLiam Girdwood * Configures the DAI hardware format and clocking. 32598c6529dbSLiam Girdwood */ 32608c6529dbSLiam Girdwood int snd_soc_dai_set_fmt(struct snd_soc_dai *dai, unsigned int fmt) 32618c6529dbSLiam Girdwood { 3262f0fba2adSLiam Girdwood if (dai->driver && dai->driver->ops->set_fmt) 3263f0fba2adSLiam Girdwood return dai->driver->ops->set_fmt(dai, fmt); 32648c6529dbSLiam Girdwood else 32658c6529dbSLiam Girdwood return -EINVAL; 32668c6529dbSLiam Girdwood } 32678c6529dbSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_dai_set_fmt); 32688c6529dbSLiam Girdwood 32698c6529dbSLiam Girdwood /** 32708c6529dbSLiam Girdwood * snd_soc_dai_set_tdm_slot - configure DAI TDM. 32718c6529dbSLiam Girdwood * @dai: DAI 3272a5479e38SDaniel Ribeiro * @tx_mask: bitmask representing active TX slots. 3273a5479e38SDaniel Ribeiro * @rx_mask: bitmask representing active RX slots. 32748c6529dbSLiam Girdwood * @slots: Number of slots in use. 3275a5479e38SDaniel Ribeiro * @slot_width: Width in bits for each slot. 32768c6529dbSLiam Girdwood * 32778c6529dbSLiam Girdwood * Configures a DAI for TDM operation. Both mask and slots are codec and DAI 32788c6529dbSLiam Girdwood * specific. 32798c6529dbSLiam Girdwood */ 32808c6529dbSLiam Girdwood int snd_soc_dai_set_tdm_slot(struct snd_soc_dai *dai, 3281a5479e38SDaniel Ribeiro unsigned int tx_mask, unsigned int rx_mask, int slots, int slot_width) 32828c6529dbSLiam Girdwood { 3283f0fba2adSLiam Girdwood if (dai->driver && dai->driver->ops->set_tdm_slot) 3284f0fba2adSLiam Girdwood return dai->driver->ops->set_tdm_slot(dai, tx_mask, rx_mask, 3285a5479e38SDaniel Ribeiro slots, slot_width); 32868c6529dbSLiam Girdwood else 32878c6529dbSLiam Girdwood return -EINVAL; 32888c6529dbSLiam Girdwood } 32898c6529dbSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_dai_set_tdm_slot); 32908c6529dbSLiam Girdwood 32918c6529dbSLiam Girdwood /** 3292472df3cbSBarry Song * snd_soc_dai_set_channel_map - configure DAI audio channel map 3293472df3cbSBarry Song * @dai: DAI 3294472df3cbSBarry Song * @tx_num: how many TX channels 3295472df3cbSBarry Song * @tx_slot: pointer to an array which imply the TX slot number channel 3296472df3cbSBarry Song * 0~num-1 uses 3297472df3cbSBarry Song * @rx_num: how many RX channels 3298472df3cbSBarry Song * @rx_slot: pointer to an array which imply the RX slot number channel 3299472df3cbSBarry Song * 0~num-1 uses 3300472df3cbSBarry Song * 3301472df3cbSBarry Song * configure the relationship between channel number and TDM slot number. 3302472df3cbSBarry Song */ 3303472df3cbSBarry Song int snd_soc_dai_set_channel_map(struct snd_soc_dai *dai, 3304472df3cbSBarry Song unsigned int tx_num, unsigned int *tx_slot, 3305472df3cbSBarry Song unsigned int rx_num, unsigned int *rx_slot) 3306472df3cbSBarry Song { 3307f0fba2adSLiam Girdwood if (dai->driver && dai->driver->ops->set_channel_map) 3308f0fba2adSLiam Girdwood return dai->driver->ops->set_channel_map(dai, tx_num, tx_slot, 3309472df3cbSBarry Song rx_num, rx_slot); 3310472df3cbSBarry Song else 3311472df3cbSBarry Song return -EINVAL; 3312472df3cbSBarry Song } 3313472df3cbSBarry Song EXPORT_SYMBOL_GPL(snd_soc_dai_set_channel_map); 3314472df3cbSBarry Song 3315472df3cbSBarry Song /** 33168c6529dbSLiam Girdwood * snd_soc_dai_set_tristate - configure DAI system or master clock. 33178c6529dbSLiam Girdwood * @dai: DAI 33188c6529dbSLiam Girdwood * @tristate: tristate enable 33198c6529dbSLiam Girdwood * 33208c6529dbSLiam Girdwood * Tristates the DAI so that others can use it. 33218c6529dbSLiam Girdwood */ 33228c6529dbSLiam Girdwood int snd_soc_dai_set_tristate(struct snd_soc_dai *dai, int tristate) 33238c6529dbSLiam Girdwood { 3324f0fba2adSLiam Girdwood if (dai->driver && dai->driver->ops->set_tristate) 3325f0fba2adSLiam Girdwood return dai->driver->ops->set_tristate(dai, tristate); 33268c6529dbSLiam Girdwood else 33278c6529dbSLiam Girdwood return -EINVAL; 33288c6529dbSLiam Girdwood } 33298c6529dbSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_dai_set_tristate); 33308c6529dbSLiam Girdwood 33318c6529dbSLiam Girdwood /** 33328c6529dbSLiam Girdwood * snd_soc_dai_digital_mute - configure DAI system or master clock. 33338c6529dbSLiam Girdwood * @dai: DAI 33348c6529dbSLiam Girdwood * @mute: mute enable 33358c6529dbSLiam Girdwood * 33368c6529dbSLiam Girdwood * Mutes the DAI DAC. 33378c6529dbSLiam Girdwood */ 33388c6529dbSLiam Girdwood int snd_soc_dai_digital_mute(struct snd_soc_dai *dai, int mute) 33398c6529dbSLiam Girdwood { 3340f0fba2adSLiam Girdwood if (dai->driver && dai->driver->ops->digital_mute) 3341f0fba2adSLiam Girdwood return dai->driver->ops->digital_mute(dai, mute); 33428c6529dbSLiam Girdwood else 33438c6529dbSLiam Girdwood return -EINVAL; 33448c6529dbSLiam Girdwood } 33458c6529dbSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_dai_digital_mute); 33468c6529dbSLiam Girdwood 3347c5af3a2eSMark Brown /** 3348c5af3a2eSMark Brown * snd_soc_register_card - Register a card with the ASoC core 3349c5af3a2eSMark Brown * 3350ac11a2b3SMark Brown * @card: Card to register 3351c5af3a2eSMark Brown * 3352c5af3a2eSMark Brown */ 335370a7ca34SVinod Koul int snd_soc_register_card(struct snd_soc_card *card) 3354c5af3a2eSMark Brown { 3355f0fba2adSLiam Girdwood int i; 3356f0fba2adSLiam Girdwood 3357c5af3a2eSMark Brown if (!card->name || !card->dev) 3358c5af3a2eSMark Brown return -EINVAL; 3359c5af3a2eSMark Brown 3360ed77cc12SMark Brown dev_set_drvdata(card->dev, card); 3361ed77cc12SMark Brown 3362111c6419SStephen Warren snd_soc_initialize_card_lists(card); 3363111c6419SStephen Warren 3364150dd2f8SVinod Koul soc_init_card_debugfs(card); 3365150dd2f8SVinod Koul 33662eea392dSJarkko Nikula card->rtd = kzalloc(sizeof(struct snd_soc_pcm_runtime) * 33672eea392dSJarkko Nikula (card->num_links + card->num_aux_devs), 3368f0fba2adSLiam Girdwood GFP_KERNEL); 3369f0fba2adSLiam Girdwood if (card->rtd == NULL) 3370f0fba2adSLiam Girdwood return -ENOMEM; 33712eea392dSJarkko Nikula card->rtd_aux = &card->rtd[card->num_links]; 3372f0fba2adSLiam Girdwood 3373f0fba2adSLiam Girdwood for (i = 0; i < card->num_links; i++) 3374f0fba2adSLiam Girdwood card->rtd[i].dai_link = &card->dai_link[i]; 3375f0fba2adSLiam Girdwood 3376c5af3a2eSMark Brown INIT_LIST_HEAD(&card->list); 3377c5af3a2eSMark Brown card->instantiated = 0; 3378f0fba2adSLiam Girdwood mutex_init(&card->mutex); 3379c5af3a2eSMark Brown 3380c5af3a2eSMark Brown mutex_lock(&client_mutex); 3381c5af3a2eSMark Brown list_add(&card->list, &card_list); 3382435c5e25SMark Brown snd_soc_instantiate_cards(); 3383c5af3a2eSMark Brown mutex_unlock(&client_mutex); 3384c5af3a2eSMark Brown 3385c5af3a2eSMark Brown dev_dbg(card->dev, "Registered card '%s'\n", card->name); 3386c5af3a2eSMark Brown 3387c5af3a2eSMark Brown return 0; 3388c5af3a2eSMark Brown } 338970a7ca34SVinod Koul EXPORT_SYMBOL_GPL(snd_soc_register_card); 3390c5af3a2eSMark Brown 3391c5af3a2eSMark Brown /** 3392c5af3a2eSMark Brown * snd_soc_unregister_card - Unregister a card with the ASoC core 3393c5af3a2eSMark Brown * 3394ac11a2b3SMark Brown * @card: Card to unregister 3395c5af3a2eSMark Brown * 3396c5af3a2eSMark Brown */ 339770a7ca34SVinod Koul int snd_soc_unregister_card(struct snd_soc_card *card) 3398c5af3a2eSMark Brown { 3399b0e26485SVinod Koul if (card->instantiated) 3400b0e26485SVinod Koul soc_cleanup_card_resources(card); 3401c5af3a2eSMark Brown mutex_lock(&client_mutex); 3402c5af3a2eSMark Brown list_del(&card->list); 3403c5af3a2eSMark Brown mutex_unlock(&client_mutex); 3404c5af3a2eSMark Brown dev_dbg(card->dev, "Unregistered card '%s'\n", card->name); 3405c5af3a2eSMark Brown 3406c5af3a2eSMark Brown return 0; 3407c5af3a2eSMark Brown } 340870a7ca34SVinod Koul EXPORT_SYMBOL_GPL(snd_soc_unregister_card); 3409c5af3a2eSMark Brown 3410f0fba2adSLiam Girdwood /* 3411f0fba2adSLiam Girdwood * Simplify DAI link configuration by removing ".-1" from device names 3412f0fba2adSLiam Girdwood * and sanitizing names. 3413f0fba2adSLiam Girdwood */ 34140b9a214aSDimitris Papastamos static char *fmt_single_name(struct device *dev, int *id) 3415f0fba2adSLiam Girdwood { 3416f0fba2adSLiam Girdwood char *found, name[NAME_SIZE]; 3417f0fba2adSLiam Girdwood int id1, id2; 3418f0fba2adSLiam Girdwood 3419f0fba2adSLiam Girdwood if (dev_name(dev) == NULL) 3420f0fba2adSLiam Girdwood return NULL; 3421f0fba2adSLiam Girdwood 342258818a77SDimitris Papastamos strlcpy(name, dev_name(dev), NAME_SIZE); 3423f0fba2adSLiam Girdwood 3424f0fba2adSLiam Girdwood /* are we a "%s.%d" name (platform and SPI components) */ 3425f0fba2adSLiam Girdwood found = strstr(name, dev->driver->name); 3426f0fba2adSLiam Girdwood if (found) { 3427f0fba2adSLiam Girdwood /* get ID */ 3428f0fba2adSLiam Girdwood if (sscanf(&found[strlen(dev->driver->name)], ".%d", id) == 1) { 3429f0fba2adSLiam Girdwood 3430f0fba2adSLiam Girdwood /* discard ID from name if ID == -1 */ 3431f0fba2adSLiam Girdwood if (*id == -1) 3432f0fba2adSLiam Girdwood found[strlen(dev->driver->name)] = '\0'; 3433f0fba2adSLiam Girdwood } 3434f0fba2adSLiam Girdwood 3435f0fba2adSLiam Girdwood } else { 3436f0fba2adSLiam Girdwood /* I2C component devices are named "bus-addr" */ 3437f0fba2adSLiam Girdwood if (sscanf(name, "%x-%x", &id1, &id2) == 2) { 3438f0fba2adSLiam Girdwood char tmp[NAME_SIZE]; 3439f0fba2adSLiam Girdwood 3440f0fba2adSLiam Girdwood /* create unique ID number from I2C addr and bus */ 344105899446SJarkko Nikula *id = ((id1 & 0xffff) << 16) + id2; 3442f0fba2adSLiam Girdwood 3443f0fba2adSLiam Girdwood /* sanitize component name for DAI link creation */ 3444f0fba2adSLiam Girdwood snprintf(tmp, NAME_SIZE, "%s.%s", dev->driver->name, name); 344558818a77SDimitris Papastamos strlcpy(name, tmp, NAME_SIZE); 3446f0fba2adSLiam Girdwood } else 3447f0fba2adSLiam Girdwood *id = 0; 3448f0fba2adSLiam Girdwood } 3449f0fba2adSLiam Girdwood 3450f0fba2adSLiam Girdwood return kstrdup(name, GFP_KERNEL); 3451f0fba2adSLiam Girdwood } 3452f0fba2adSLiam Girdwood 3453f0fba2adSLiam Girdwood /* 3454f0fba2adSLiam Girdwood * Simplify DAI link naming for single devices with multiple DAIs by removing 3455f0fba2adSLiam Girdwood * any ".-1" and using the DAI name (instead of device name). 3456f0fba2adSLiam Girdwood */ 3457f0fba2adSLiam Girdwood static inline char *fmt_multiple_name(struct device *dev, 3458f0fba2adSLiam Girdwood struct snd_soc_dai_driver *dai_drv) 3459f0fba2adSLiam Girdwood { 3460f0fba2adSLiam Girdwood if (dai_drv->name == NULL) { 3461f0fba2adSLiam Girdwood printk(KERN_ERR "asoc: error - multiple DAI %s registered with no name\n", 3462f0fba2adSLiam Girdwood dev_name(dev)); 3463f0fba2adSLiam Girdwood return NULL; 3464f0fba2adSLiam Girdwood } 3465f0fba2adSLiam Girdwood 3466f0fba2adSLiam Girdwood return kstrdup(dai_drv->name, GFP_KERNEL); 3467f0fba2adSLiam Girdwood } 3468f0fba2adSLiam Girdwood 34699115171aSMark Brown /** 34709115171aSMark Brown * snd_soc_register_dai - Register a DAI with the ASoC core 34719115171aSMark Brown * 3472ac11a2b3SMark Brown * @dai: DAI to register 34739115171aSMark Brown */ 3474f0fba2adSLiam Girdwood int snd_soc_register_dai(struct device *dev, 3475f0fba2adSLiam Girdwood struct snd_soc_dai_driver *dai_drv) 34769115171aSMark Brown { 3477f0fba2adSLiam Girdwood struct snd_soc_dai *dai; 34789115171aSMark Brown 3479f0fba2adSLiam Girdwood dev_dbg(dev, "dai register %s\n", dev_name(dev)); 34809115171aSMark Brown 3481f0fba2adSLiam Girdwood dai = kzalloc(sizeof(struct snd_soc_dai), GFP_KERNEL); 3482f0fba2adSLiam Girdwood if (dai == NULL) 3483f0fba2adSLiam Girdwood return -ENOMEM; 34846335d055SEric Miao 3485f0fba2adSLiam Girdwood /* create DAI component name */ 3486f0fba2adSLiam Girdwood dai->name = fmt_single_name(dev, &dai->id); 3487f0fba2adSLiam Girdwood if (dai->name == NULL) { 3488f0fba2adSLiam Girdwood kfree(dai); 3489f0fba2adSLiam Girdwood return -ENOMEM; 3490f0fba2adSLiam Girdwood } 3491f0fba2adSLiam Girdwood 3492f0fba2adSLiam Girdwood dai->dev = dev; 3493f0fba2adSLiam Girdwood dai->driver = dai_drv; 3494f0fba2adSLiam Girdwood if (!dai->driver->ops) 3495f0fba2adSLiam Girdwood dai->driver->ops = &null_dai_ops; 34969115171aSMark Brown 34979115171aSMark Brown mutex_lock(&client_mutex); 34989115171aSMark Brown list_add(&dai->list, &dai_list); 3499435c5e25SMark Brown snd_soc_instantiate_cards(); 35009115171aSMark Brown mutex_unlock(&client_mutex); 35019115171aSMark Brown 35029115171aSMark Brown pr_debug("Registered DAI '%s'\n", dai->name); 35039115171aSMark Brown 35049115171aSMark Brown return 0; 35059115171aSMark Brown } 35069115171aSMark Brown EXPORT_SYMBOL_GPL(snd_soc_register_dai); 35079115171aSMark Brown 35089115171aSMark Brown /** 35099115171aSMark Brown * snd_soc_unregister_dai - Unregister a DAI from the ASoC core 35109115171aSMark Brown * 3511ac11a2b3SMark Brown * @dai: DAI to unregister 35129115171aSMark Brown */ 3513f0fba2adSLiam Girdwood void snd_soc_unregister_dai(struct device *dev) 35149115171aSMark Brown { 3515f0fba2adSLiam Girdwood struct snd_soc_dai *dai; 3516f0fba2adSLiam Girdwood 3517f0fba2adSLiam Girdwood list_for_each_entry(dai, &dai_list, list) { 3518f0fba2adSLiam Girdwood if (dev == dai->dev) 3519f0fba2adSLiam Girdwood goto found; 3520f0fba2adSLiam Girdwood } 3521f0fba2adSLiam Girdwood return; 3522f0fba2adSLiam Girdwood 3523f0fba2adSLiam Girdwood found: 35249115171aSMark Brown mutex_lock(&client_mutex); 35259115171aSMark Brown list_del(&dai->list); 35269115171aSMark Brown mutex_unlock(&client_mutex); 35279115171aSMark Brown 35289115171aSMark Brown pr_debug("Unregistered DAI '%s'\n", dai->name); 3529f0fba2adSLiam Girdwood kfree(dai->name); 3530f0fba2adSLiam Girdwood kfree(dai); 35319115171aSMark Brown } 35329115171aSMark Brown EXPORT_SYMBOL_GPL(snd_soc_unregister_dai); 35339115171aSMark Brown 35349115171aSMark Brown /** 35359115171aSMark Brown * snd_soc_register_dais - Register multiple DAIs with the ASoC core 35369115171aSMark Brown * 3537ac11a2b3SMark Brown * @dai: Array of DAIs to register 3538ac11a2b3SMark Brown * @count: Number of DAIs 35399115171aSMark Brown */ 3540f0fba2adSLiam Girdwood int snd_soc_register_dais(struct device *dev, 3541f0fba2adSLiam Girdwood struct snd_soc_dai_driver *dai_drv, size_t count) 35429115171aSMark Brown { 3543f0fba2adSLiam Girdwood struct snd_soc_dai *dai; 3544f0fba2adSLiam Girdwood int i, ret = 0; 3545f0fba2adSLiam Girdwood 3546720ffa4cSLiam Girdwood dev_dbg(dev, "dai register %s #%Zu\n", dev_name(dev), count); 35479115171aSMark Brown 35489115171aSMark Brown for (i = 0; i < count; i++) { 3549f0fba2adSLiam Girdwood 3550f0fba2adSLiam Girdwood dai = kzalloc(sizeof(struct snd_soc_dai), GFP_KERNEL); 3551c46e0079SAxel Lin if (dai == NULL) { 3552c46e0079SAxel Lin ret = -ENOMEM; 3553c46e0079SAxel Lin goto err; 3554c46e0079SAxel Lin } 3555f0fba2adSLiam Girdwood 3556f0fba2adSLiam Girdwood /* create DAI component name */ 3557f0fba2adSLiam Girdwood dai->name = fmt_multiple_name(dev, &dai_drv[i]); 3558f0fba2adSLiam Girdwood if (dai->name == NULL) { 3559f0fba2adSLiam Girdwood kfree(dai); 3560f0fba2adSLiam Girdwood ret = -EINVAL; 35619115171aSMark Brown goto err; 35629115171aSMark Brown } 35639115171aSMark Brown 3564f0fba2adSLiam Girdwood dai->dev = dev; 3565f0fba2adSLiam Girdwood dai->driver = &dai_drv[i]; 35660f9141c9SMark Brown if (dai->driver->id) 35670f9141c9SMark Brown dai->id = dai->driver->id; 35680f9141c9SMark Brown else 35690f9141c9SMark Brown dai->id = i; 3570f0fba2adSLiam Girdwood if (!dai->driver->ops) 3571f0fba2adSLiam Girdwood dai->driver->ops = &null_dai_ops; 3572f0fba2adSLiam Girdwood 3573f0fba2adSLiam Girdwood mutex_lock(&client_mutex); 3574f0fba2adSLiam Girdwood list_add(&dai->list, &dai_list); 3575f0fba2adSLiam Girdwood mutex_unlock(&client_mutex); 3576f0fba2adSLiam Girdwood 3577f0fba2adSLiam Girdwood pr_debug("Registered DAI '%s'\n", dai->name); 3578f0fba2adSLiam Girdwood } 3579f0fba2adSLiam Girdwood 35801dcb4f38SAxel Lin mutex_lock(&client_mutex); 3581f0fba2adSLiam Girdwood snd_soc_instantiate_cards(); 35821dcb4f38SAxel Lin mutex_unlock(&client_mutex); 35839115171aSMark Brown return 0; 35849115171aSMark Brown 35859115171aSMark Brown err: 35869115171aSMark Brown for (i--; i >= 0; i--) 3587f0fba2adSLiam Girdwood snd_soc_unregister_dai(dev); 35889115171aSMark Brown 35899115171aSMark Brown return ret; 35909115171aSMark Brown } 35919115171aSMark Brown EXPORT_SYMBOL_GPL(snd_soc_register_dais); 35929115171aSMark Brown 35939115171aSMark Brown /** 35949115171aSMark Brown * snd_soc_unregister_dais - Unregister multiple DAIs from the ASoC core 35959115171aSMark Brown * 3596ac11a2b3SMark Brown * @dai: Array of DAIs to unregister 3597ac11a2b3SMark Brown * @count: Number of DAIs 35989115171aSMark Brown */ 3599f0fba2adSLiam Girdwood void snd_soc_unregister_dais(struct device *dev, size_t count) 36009115171aSMark Brown { 36019115171aSMark Brown int i; 36029115171aSMark Brown 36039115171aSMark Brown for (i = 0; i < count; i++) 3604f0fba2adSLiam Girdwood snd_soc_unregister_dai(dev); 36059115171aSMark Brown } 36069115171aSMark Brown EXPORT_SYMBOL_GPL(snd_soc_unregister_dais); 36079115171aSMark Brown 360812a48a8cSMark Brown /** 360912a48a8cSMark Brown * snd_soc_register_platform - Register a platform with the ASoC core 361012a48a8cSMark Brown * 3611ac11a2b3SMark Brown * @platform: platform to register 361212a48a8cSMark Brown */ 3613f0fba2adSLiam Girdwood int snd_soc_register_platform(struct device *dev, 3614f0fba2adSLiam Girdwood struct snd_soc_platform_driver *platform_drv) 361512a48a8cSMark Brown { 3616f0fba2adSLiam Girdwood struct snd_soc_platform *platform; 361712a48a8cSMark Brown 3618f0fba2adSLiam Girdwood dev_dbg(dev, "platform register %s\n", dev_name(dev)); 3619f0fba2adSLiam Girdwood 3620f0fba2adSLiam Girdwood platform = kzalloc(sizeof(struct snd_soc_platform), GFP_KERNEL); 3621f0fba2adSLiam Girdwood if (platform == NULL) 3622f0fba2adSLiam Girdwood return -ENOMEM; 3623f0fba2adSLiam Girdwood 3624f0fba2adSLiam Girdwood /* create platform component name */ 3625f0fba2adSLiam Girdwood platform->name = fmt_single_name(dev, &platform->id); 3626f0fba2adSLiam Girdwood if (platform->name == NULL) { 3627f0fba2adSLiam Girdwood kfree(platform); 3628f0fba2adSLiam Girdwood return -ENOMEM; 3629f0fba2adSLiam Girdwood } 3630f0fba2adSLiam Girdwood 3631f0fba2adSLiam Girdwood platform->dev = dev; 3632f0fba2adSLiam Girdwood platform->driver = platform_drv; 363312a48a8cSMark Brown 363412a48a8cSMark Brown mutex_lock(&client_mutex); 363512a48a8cSMark Brown list_add(&platform->list, &platform_list); 3636435c5e25SMark Brown snd_soc_instantiate_cards(); 363712a48a8cSMark Brown mutex_unlock(&client_mutex); 363812a48a8cSMark Brown 363912a48a8cSMark Brown pr_debug("Registered platform '%s'\n", platform->name); 364012a48a8cSMark Brown 364112a48a8cSMark Brown return 0; 364212a48a8cSMark Brown } 364312a48a8cSMark Brown EXPORT_SYMBOL_GPL(snd_soc_register_platform); 364412a48a8cSMark Brown 364512a48a8cSMark Brown /** 364612a48a8cSMark Brown * snd_soc_unregister_platform - Unregister a platform from the ASoC core 364712a48a8cSMark Brown * 3648ac11a2b3SMark Brown * @platform: platform to unregister 364912a48a8cSMark Brown */ 3650f0fba2adSLiam Girdwood void snd_soc_unregister_platform(struct device *dev) 365112a48a8cSMark Brown { 3652f0fba2adSLiam Girdwood struct snd_soc_platform *platform; 3653f0fba2adSLiam Girdwood 3654f0fba2adSLiam Girdwood list_for_each_entry(platform, &platform_list, list) { 3655f0fba2adSLiam Girdwood if (dev == platform->dev) 3656f0fba2adSLiam Girdwood goto found; 3657f0fba2adSLiam Girdwood } 3658f0fba2adSLiam Girdwood return; 3659f0fba2adSLiam Girdwood 3660f0fba2adSLiam Girdwood found: 366112a48a8cSMark Brown mutex_lock(&client_mutex); 366212a48a8cSMark Brown list_del(&platform->list); 366312a48a8cSMark Brown mutex_unlock(&client_mutex); 366412a48a8cSMark Brown 366512a48a8cSMark Brown pr_debug("Unregistered platform '%s'\n", platform->name); 3666f0fba2adSLiam Girdwood kfree(platform->name); 3667f0fba2adSLiam Girdwood kfree(platform); 366812a48a8cSMark Brown } 366912a48a8cSMark Brown EXPORT_SYMBOL_GPL(snd_soc_unregister_platform); 367012a48a8cSMark Brown 3671151ab22cSMark Brown static u64 codec_format_map[] = { 3672151ab22cSMark Brown SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S16_BE, 3673151ab22cSMark Brown SNDRV_PCM_FMTBIT_U16_LE | SNDRV_PCM_FMTBIT_U16_BE, 3674151ab22cSMark Brown SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S24_BE, 3675151ab22cSMark Brown SNDRV_PCM_FMTBIT_U24_LE | SNDRV_PCM_FMTBIT_U24_BE, 3676151ab22cSMark Brown SNDRV_PCM_FMTBIT_S32_LE | SNDRV_PCM_FMTBIT_S32_BE, 3677151ab22cSMark Brown SNDRV_PCM_FMTBIT_U32_LE | SNDRV_PCM_FMTBIT_U32_BE, 3678151ab22cSMark Brown SNDRV_PCM_FMTBIT_S24_3LE | SNDRV_PCM_FMTBIT_U24_3BE, 3679151ab22cSMark Brown SNDRV_PCM_FMTBIT_U24_3LE | SNDRV_PCM_FMTBIT_U24_3BE, 3680151ab22cSMark Brown SNDRV_PCM_FMTBIT_S20_3LE | SNDRV_PCM_FMTBIT_S20_3BE, 3681151ab22cSMark Brown SNDRV_PCM_FMTBIT_U20_3LE | SNDRV_PCM_FMTBIT_U20_3BE, 3682151ab22cSMark Brown SNDRV_PCM_FMTBIT_S18_3LE | SNDRV_PCM_FMTBIT_S18_3BE, 3683151ab22cSMark Brown SNDRV_PCM_FMTBIT_U18_3LE | SNDRV_PCM_FMTBIT_U18_3BE, 3684151ab22cSMark Brown SNDRV_PCM_FMTBIT_FLOAT_LE | SNDRV_PCM_FMTBIT_FLOAT_BE, 3685151ab22cSMark Brown SNDRV_PCM_FMTBIT_FLOAT64_LE | SNDRV_PCM_FMTBIT_FLOAT64_BE, 3686151ab22cSMark Brown SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_LE 3687151ab22cSMark Brown | SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_BE, 3688151ab22cSMark Brown }; 3689151ab22cSMark Brown 3690151ab22cSMark Brown /* Fix up the DAI formats for endianness: codecs don't actually see 3691151ab22cSMark Brown * the endianness of the data but we're using the CPU format 3692151ab22cSMark Brown * definitions which do need to include endianness so we ensure that 3693151ab22cSMark Brown * codec DAIs always have both big and little endian variants set. 3694151ab22cSMark Brown */ 3695151ab22cSMark Brown static void fixup_codec_formats(struct snd_soc_pcm_stream *stream) 3696151ab22cSMark Brown { 3697151ab22cSMark Brown int i; 3698151ab22cSMark Brown 3699151ab22cSMark Brown for (i = 0; i < ARRAY_SIZE(codec_format_map); i++) 3700151ab22cSMark Brown if (stream->formats & codec_format_map[i]) 3701151ab22cSMark Brown stream->formats |= codec_format_map[i]; 3702151ab22cSMark Brown } 3703151ab22cSMark Brown 37040d0cf00aSMark Brown /** 37050d0cf00aSMark Brown * snd_soc_register_codec - Register a codec with the ASoC core 37060d0cf00aSMark Brown * 3707ac11a2b3SMark Brown * @codec: codec to register 37080d0cf00aSMark Brown */ 3709f0fba2adSLiam Girdwood int snd_soc_register_codec(struct device *dev, 3710001ae4c0SMark Brown const struct snd_soc_codec_driver *codec_drv, 3711001ae4c0SMark Brown struct snd_soc_dai_driver *dai_drv, 3712001ae4c0SMark Brown int num_dai) 37130d0cf00aSMark Brown { 37143335ddcaSDimitris Papastamos size_t reg_size; 3715f0fba2adSLiam Girdwood struct snd_soc_codec *codec; 3716f0fba2adSLiam Girdwood int ret, i; 3717151ab22cSMark Brown 3718f0fba2adSLiam Girdwood dev_dbg(dev, "codec register %s\n", dev_name(dev)); 37190d0cf00aSMark Brown 3720f0fba2adSLiam Girdwood codec = kzalloc(sizeof(struct snd_soc_codec), GFP_KERNEL); 3721f0fba2adSLiam Girdwood if (codec == NULL) 3722f0fba2adSLiam Girdwood return -ENOMEM; 37230d0cf00aSMark Brown 3724f0fba2adSLiam Girdwood /* create CODEC component name */ 3725f0fba2adSLiam Girdwood codec->name = fmt_single_name(dev, &codec->id); 3726f0fba2adSLiam Girdwood if (codec->name == NULL) { 3727f0fba2adSLiam Girdwood kfree(codec); 3728f0fba2adSLiam Girdwood return -ENOMEM; 3729151ab22cSMark Brown } 3730151ab22cSMark Brown 373123bbce34SDimitris Papastamos if (codec_drv->compress_type) 373223bbce34SDimitris Papastamos codec->compress_type = codec_drv->compress_type; 373323bbce34SDimitris Papastamos else 373423bbce34SDimitris Papastamos codec->compress_type = SND_SOC_FLAT_COMPRESSION; 373523bbce34SDimitris Papastamos 3736c3acec26SMark Brown codec->write = codec_drv->write; 3737c3acec26SMark Brown codec->read = codec_drv->read; 37381500b7b5SDimitris Papastamos codec->volatile_register = codec_drv->volatile_register; 37391500b7b5SDimitris Papastamos codec->readable_register = codec_drv->readable_register; 37408020454cSDimitris Papastamos codec->writable_register = codec_drv->writable_register; 3741ce6120ccSLiam Girdwood codec->dapm.bias_level = SND_SOC_BIAS_OFF; 3742ce6120ccSLiam Girdwood codec->dapm.dev = dev; 3743ce6120ccSLiam Girdwood codec->dapm.codec = codec; 3744474b62d6SMark Brown codec->dapm.seq_notifier = codec_drv->seq_notifier; 3745f0fba2adSLiam Girdwood codec->dev = dev; 3746f0fba2adSLiam Girdwood codec->driver = codec_drv; 3747f0fba2adSLiam Girdwood codec->num_dai = num_dai; 3748f0fba2adSLiam Girdwood mutex_init(&codec->mutex); 3749f0fba2adSLiam Girdwood 37507a30a3dbSDimitris Papastamos /* allocate CODEC register cache */ 37517a30a3dbSDimitris Papastamos if (codec_drv->reg_cache_size && codec_drv->reg_word_size) { 37523335ddcaSDimitris Papastamos reg_size = codec_drv->reg_cache_size * codec_drv->reg_word_size; 3753aea170a0SDimitris Papastamos codec->reg_size = reg_size; 37543335ddcaSDimitris Papastamos /* it is necessary to make a copy of the default register cache 37553335ddcaSDimitris Papastamos * because in the case of using a compression type that requires 37563335ddcaSDimitris Papastamos * the default register cache to be marked as __devinitconst the 37573335ddcaSDimitris Papastamos * kernel might have freed the array by the time we initialize 37583335ddcaSDimitris Papastamos * the cache. 37593335ddcaSDimitris Papastamos */ 37602aa86323SDimitris Papastamos if (codec_drv->reg_cache_default) { 37613335ddcaSDimitris Papastamos codec->reg_def_copy = kmemdup(codec_drv->reg_cache_default, 37623335ddcaSDimitris Papastamos reg_size, GFP_KERNEL); 37633335ddcaSDimitris Papastamos if (!codec->reg_def_copy) { 37643335ddcaSDimitris Papastamos ret = -ENOMEM; 3765fdf0f54dSDimitris Papastamos goto fail; 37667a30a3dbSDimitris Papastamos } 37677a30a3dbSDimitris Papastamos } 37682aa86323SDimitris Papastamos } 37697a30a3dbSDimitris Papastamos 37701500b7b5SDimitris Papastamos if (codec_drv->reg_access_size && codec_drv->reg_access_default) { 37711500b7b5SDimitris Papastamos if (!codec->volatile_register) 37721500b7b5SDimitris Papastamos codec->volatile_register = snd_soc_default_volatile_register; 37731500b7b5SDimitris Papastamos if (!codec->readable_register) 37741500b7b5SDimitris Papastamos codec->readable_register = snd_soc_default_readable_register; 37758020454cSDimitris Papastamos if (!codec->writable_register) 37768020454cSDimitris Papastamos codec->writable_register = snd_soc_default_writable_register; 37771500b7b5SDimitris Papastamos } 37781500b7b5SDimitris Papastamos 3779f0fba2adSLiam Girdwood for (i = 0; i < num_dai; i++) { 3780f0fba2adSLiam Girdwood fixup_codec_formats(&dai_drv[i].playback); 3781f0fba2adSLiam Girdwood fixup_codec_formats(&dai_drv[i].capture); 3782f0fba2adSLiam Girdwood } 3783f0fba2adSLiam Girdwood 378426b01ccdSMark Brown /* register any DAIs */ 378526b01ccdSMark Brown if (num_dai) { 3786f0fba2adSLiam Girdwood ret = snd_soc_register_dais(dev, dai_drv, num_dai); 3787f0fba2adSLiam Girdwood if (ret < 0) 3788fdf0f54dSDimitris Papastamos goto fail; 378926b01ccdSMark Brown } 3790f0fba2adSLiam Girdwood 37910d0cf00aSMark Brown mutex_lock(&client_mutex); 37920d0cf00aSMark Brown list_add(&codec->list, &codec_list); 37930d0cf00aSMark Brown snd_soc_instantiate_cards(); 37940d0cf00aSMark Brown mutex_unlock(&client_mutex); 37950d0cf00aSMark Brown 37960d0cf00aSMark Brown pr_debug("Registered codec '%s'\n", codec->name); 37970d0cf00aSMark Brown return 0; 3798f0fba2adSLiam Girdwood 3799fdf0f54dSDimitris Papastamos fail: 38003335ddcaSDimitris Papastamos kfree(codec->reg_def_copy); 38013335ddcaSDimitris Papastamos codec->reg_def_copy = NULL; 3802f0fba2adSLiam Girdwood kfree(codec->name); 3803f0fba2adSLiam Girdwood kfree(codec); 3804f0fba2adSLiam Girdwood return ret; 38050d0cf00aSMark Brown } 38060d0cf00aSMark Brown EXPORT_SYMBOL_GPL(snd_soc_register_codec); 38070d0cf00aSMark Brown 38080d0cf00aSMark Brown /** 38090d0cf00aSMark Brown * snd_soc_unregister_codec - Unregister a codec from the ASoC core 38100d0cf00aSMark Brown * 3811ac11a2b3SMark Brown * @codec: codec to unregister 38120d0cf00aSMark Brown */ 3813f0fba2adSLiam Girdwood void snd_soc_unregister_codec(struct device *dev) 38140d0cf00aSMark Brown { 3815f0fba2adSLiam Girdwood struct snd_soc_codec *codec; 3816f0fba2adSLiam Girdwood int i; 3817f0fba2adSLiam Girdwood 3818f0fba2adSLiam Girdwood list_for_each_entry(codec, &codec_list, list) { 3819f0fba2adSLiam Girdwood if (dev == codec->dev) 3820f0fba2adSLiam Girdwood goto found; 3821f0fba2adSLiam Girdwood } 3822f0fba2adSLiam Girdwood return; 3823f0fba2adSLiam Girdwood 3824f0fba2adSLiam Girdwood found: 382526b01ccdSMark Brown if (codec->num_dai) 3826f0fba2adSLiam Girdwood for (i = 0; i < codec->num_dai; i++) 3827f0fba2adSLiam Girdwood snd_soc_unregister_dai(dev); 3828f0fba2adSLiam Girdwood 38290d0cf00aSMark Brown mutex_lock(&client_mutex); 38300d0cf00aSMark Brown list_del(&codec->list); 38310d0cf00aSMark Brown mutex_unlock(&client_mutex); 38320d0cf00aSMark Brown 38330d0cf00aSMark Brown pr_debug("Unregistered codec '%s'\n", codec->name); 3834f0fba2adSLiam Girdwood 38357a30a3dbSDimitris Papastamos snd_soc_cache_exit(codec); 38363335ddcaSDimitris Papastamos kfree(codec->reg_def_copy); 38371aafcd4dSDimitris Papastamos kfree(codec->name); 3838f0fba2adSLiam Girdwood kfree(codec); 38390d0cf00aSMark Brown } 38400d0cf00aSMark Brown EXPORT_SYMBOL_GPL(snd_soc_unregister_codec); 38410d0cf00aSMark Brown 3842c9b3a40fSTakashi Iwai static int __init snd_soc_init(void) 3843db2a4165SFrank Mandarino { 3844384c89e2SMark Brown #ifdef CONFIG_DEBUG_FS 38458a9dab1aSMark Brown snd_soc_debugfs_root = debugfs_create_dir("asoc", NULL); 38468a9dab1aSMark Brown if (IS_ERR(snd_soc_debugfs_root) || !snd_soc_debugfs_root) { 3847384c89e2SMark Brown printk(KERN_WARNING 3848384c89e2SMark Brown "ASoC: Failed to create debugfs directory\n"); 38498a9dab1aSMark Brown snd_soc_debugfs_root = NULL; 3850384c89e2SMark Brown } 3851c3c5a19aSMark Brown 38528a9dab1aSMark Brown if (!debugfs_create_file("codecs", 0444, snd_soc_debugfs_root, NULL, 3853c3c5a19aSMark Brown &codec_list_fops)) 3854c3c5a19aSMark Brown pr_warn("ASoC: Failed to create CODEC list debugfs file\n"); 3855c3c5a19aSMark Brown 38568a9dab1aSMark Brown if (!debugfs_create_file("dais", 0444, snd_soc_debugfs_root, NULL, 3857f3208780SMark Brown &dai_list_fops)) 3858f3208780SMark Brown pr_warn("ASoC: Failed to create DAI list debugfs file\n"); 385919c7ac27SMark Brown 38608a9dab1aSMark Brown if (!debugfs_create_file("platforms", 0444, snd_soc_debugfs_root, NULL, 386119c7ac27SMark Brown &platform_list_fops)) 386219c7ac27SMark Brown pr_warn("ASoC: Failed to create platform list debugfs file\n"); 3863384c89e2SMark Brown #endif 3864384c89e2SMark Brown 3865fb257897SMark Brown snd_soc_util_init(); 3866fb257897SMark Brown 3867db2a4165SFrank Mandarino return platform_driver_register(&soc_driver); 3868db2a4165SFrank Mandarino } 38694abe8e16SMark Brown module_init(snd_soc_init); 3870db2a4165SFrank Mandarino 38717d8c16a6SMark Brown static void __exit snd_soc_exit(void) 3872db2a4165SFrank Mandarino { 3873fb257897SMark Brown snd_soc_util_exit(); 3874fb257897SMark Brown 3875384c89e2SMark Brown #ifdef CONFIG_DEBUG_FS 38768a9dab1aSMark Brown debugfs_remove_recursive(snd_soc_debugfs_root); 3877384c89e2SMark Brown #endif 3878db2a4165SFrank Mandarino platform_driver_unregister(&soc_driver); 3879db2a4165SFrank Mandarino } 3880db2a4165SFrank Mandarino module_exit(snd_soc_exit); 3881db2a4165SFrank Mandarino 3882db2a4165SFrank Mandarino /* Module information */ 3883d331124dSLiam Girdwood MODULE_AUTHOR("Liam Girdwood, lrg@slimlogic.co.uk"); 3884db2a4165SFrank Mandarino MODULE_DESCRIPTION("ALSA SoC Core"); 3885db2a4165SFrank Mandarino MODULE_LICENSE("GPL"); 38868b45a209SKay Sievers MODULE_ALIAS("platform:soc-audio"); 3887