1db2a4165SFrank Mandarino /* 2db2a4165SFrank Mandarino * soc-core.c -- ALSA SoC Audio Layer 3db2a4165SFrank Mandarino * 4db2a4165SFrank Mandarino * Copyright 2005 Wolfson Microelectronics PLC. 50664d888SLiam Girdwood * Copyright 2005 Openedhand Ltd. 6f0fba2adSLiam Girdwood * Copyright (C) 2010 Slimlogic Ltd. 7f0fba2adSLiam Girdwood * Copyright (C) 2010 Texas Instruments Inc. 80664d888SLiam Girdwood * 9d331124dSLiam Girdwood * Author: Liam Girdwood <lrg@slimlogic.co.uk> 100664d888SLiam Girdwood * with code, comments and ideas from :- 110664d888SLiam Girdwood * Richard Purdie <richard@openedhand.com> 12db2a4165SFrank Mandarino * 13db2a4165SFrank Mandarino * This program is free software; you can redistribute it and/or modify it 14db2a4165SFrank Mandarino * under the terms of the GNU General Public License as published by the 15db2a4165SFrank Mandarino * Free Software Foundation; either version 2 of the License, or (at your 16db2a4165SFrank Mandarino * option) any later version. 17db2a4165SFrank Mandarino * 18db2a4165SFrank Mandarino * TODO: 19db2a4165SFrank Mandarino * o Add hw rules to enforce rates, etc. 20db2a4165SFrank Mandarino * o More testing with other codecs/machines. 21db2a4165SFrank Mandarino * o Add more codecs and platforms to ensure good API coverage. 22db2a4165SFrank Mandarino * o Support TDM on PCM and I2S 23db2a4165SFrank Mandarino */ 24db2a4165SFrank Mandarino 25db2a4165SFrank Mandarino #include <linux/module.h> 26db2a4165SFrank Mandarino #include <linux/moduleparam.h> 27db2a4165SFrank Mandarino #include <linux/init.h> 28db2a4165SFrank Mandarino #include <linux/delay.h> 29db2a4165SFrank Mandarino #include <linux/pm.h> 30db2a4165SFrank Mandarino #include <linux/bitops.h> 3112ef193dSTroy Kisky #include <linux/debugfs.h> 32db2a4165SFrank Mandarino #include <linux/platform_device.h> 335a0e3ad6STejun Heo #include <linux/slab.h> 34474828a4SMarek Vasut #include <sound/ac97_codec.h> 35db2a4165SFrank Mandarino #include <sound/core.h> 36db2a4165SFrank Mandarino #include <sound/pcm.h> 37db2a4165SFrank Mandarino #include <sound/pcm_params.h> 38db2a4165SFrank Mandarino #include <sound/soc.h> 39db2a4165SFrank Mandarino #include <sound/soc-dapm.h> 40db2a4165SFrank Mandarino #include <sound/initval.h> 41db2a4165SFrank Mandarino 42f0fba2adSLiam Girdwood #define NAME_SIZE 32 43f0fba2adSLiam Girdwood 44db2a4165SFrank Mandarino static DEFINE_MUTEX(pcm_mutex); 45db2a4165SFrank Mandarino static DECLARE_WAIT_QUEUE_HEAD(soc_pm_waitq); 46db2a4165SFrank Mandarino 47384c89e2SMark Brown #ifdef CONFIG_DEBUG_FS 48384c89e2SMark Brown static struct dentry *debugfs_root; 49384c89e2SMark Brown #endif 50384c89e2SMark Brown 51c5af3a2eSMark Brown static DEFINE_MUTEX(client_mutex); 52c5af3a2eSMark Brown static LIST_HEAD(card_list); 539115171aSMark Brown static LIST_HEAD(dai_list); 5412a48a8cSMark Brown static LIST_HEAD(platform_list); 550d0cf00aSMark Brown static LIST_HEAD(codec_list); 56c5af3a2eSMark Brown 57c5af3a2eSMark Brown static int snd_soc_register_card(struct snd_soc_card *card); 58c5af3a2eSMark Brown static int snd_soc_unregister_card(struct snd_soc_card *card); 59f0fba2adSLiam Girdwood static int soc_new_pcm(struct snd_soc_pcm_runtime *rtd, int num); 60c5af3a2eSMark Brown 61db2a4165SFrank Mandarino /* 62db2a4165SFrank Mandarino * This is a timeout to do a DAPM powerdown after a stream is closed(). 63db2a4165SFrank Mandarino * It can be used to eliminate pops between different playback streams, e.g. 64db2a4165SFrank Mandarino * between two audio tracks. 65db2a4165SFrank Mandarino */ 66db2a4165SFrank Mandarino static int pmdown_time = 5000; 67db2a4165SFrank Mandarino module_param(pmdown_time, int, 0); 68db2a4165SFrank Mandarino MODULE_PARM_DESC(pmdown_time, "DAPM stream powerdown time (msecs)"); 69db2a4165SFrank Mandarino 70965ac42cSLiam Girdwood /* 71965ac42cSLiam Girdwood * This function forces any delayed work to be queued and run. 72965ac42cSLiam Girdwood */ 73965ac42cSLiam Girdwood static int run_delayed_work(struct delayed_work *dwork) 74965ac42cSLiam Girdwood { 75965ac42cSLiam Girdwood int ret; 76965ac42cSLiam Girdwood 77965ac42cSLiam Girdwood /* cancel any work waiting to be queued. */ 78965ac42cSLiam Girdwood ret = cancel_delayed_work(dwork); 79965ac42cSLiam Girdwood 80965ac42cSLiam Girdwood /* if there was any work waiting then we run it now and 81965ac42cSLiam Girdwood * wait for it's completion */ 82965ac42cSLiam Girdwood if (ret) { 83965ac42cSLiam Girdwood schedule_delayed_work(dwork, 0); 84965ac42cSLiam Girdwood flush_scheduled_work(); 85965ac42cSLiam Girdwood } 86965ac42cSLiam Girdwood return ret; 87965ac42cSLiam Girdwood } 88965ac42cSLiam Girdwood 892624d5faSMark Brown /* codec register dump */ 902624d5faSMark Brown static ssize_t soc_codec_reg_show(struct snd_soc_codec *codec, char *buf) 912624d5faSMark Brown { 925164d74dSMark Brown int ret, i, step = 1, count = 0; 932624d5faSMark Brown 94f0fba2adSLiam Girdwood if (!codec->driver->reg_cache_size) 952624d5faSMark Brown return 0; 962624d5faSMark Brown 97f0fba2adSLiam Girdwood if (codec->driver->reg_cache_step) 98f0fba2adSLiam Girdwood step = codec->driver->reg_cache_step; 992624d5faSMark Brown 1002624d5faSMark Brown count += sprintf(buf, "%s registers\n", codec->name); 101f0fba2adSLiam Girdwood for (i = 0; i < codec->driver->reg_cache_size; i += step) { 102f0fba2adSLiam Girdwood if (codec->driver->readable_register && !codec->driver->readable_register(i)) 1032624d5faSMark Brown continue; 1042624d5faSMark Brown 1052624d5faSMark Brown count += sprintf(buf + count, "%2x: ", i); 1062624d5faSMark Brown if (count >= PAGE_SIZE - 1) 1072624d5faSMark Brown break; 1082624d5faSMark Brown 109f0fba2adSLiam Girdwood if (codec->driver->display_register) { 110f0fba2adSLiam Girdwood count += codec->driver->display_register(codec, buf + count, 1112624d5faSMark Brown PAGE_SIZE - count, i); 1125164d74dSMark Brown } else { 1135164d74dSMark Brown /* If the read fails it's almost certainly due to 1145164d74dSMark Brown * the register being volatile and the device being 1155164d74dSMark Brown * powered off. 1165164d74dSMark Brown */ 117f0fba2adSLiam Girdwood ret = codec->driver->read(codec, i); 1185164d74dSMark Brown if (ret >= 0) 1195164d74dSMark Brown count += snprintf(buf + count, 1205164d74dSMark Brown PAGE_SIZE - count, 1215164d74dSMark Brown "%4x", ret); 1222624d5faSMark Brown else 1235164d74dSMark Brown count += snprintf(buf + count, 1245164d74dSMark Brown PAGE_SIZE - count, 1255164d74dSMark Brown "<no data: %d>", ret); 1265164d74dSMark Brown } 1272624d5faSMark Brown 1282624d5faSMark Brown if (count >= PAGE_SIZE - 1) 1292624d5faSMark Brown break; 1302624d5faSMark Brown 1312624d5faSMark Brown count += snprintf(buf + count, PAGE_SIZE - count, "\n"); 1322624d5faSMark Brown if (count >= PAGE_SIZE - 1) 1332624d5faSMark Brown break; 1342624d5faSMark Brown } 1352624d5faSMark Brown 1362624d5faSMark Brown /* Truncate count; min() would cause a warning */ 1372624d5faSMark Brown if (count >= PAGE_SIZE) 1382624d5faSMark Brown count = PAGE_SIZE - 1; 1392624d5faSMark Brown 1402624d5faSMark Brown return count; 1412624d5faSMark Brown } 1422624d5faSMark Brown static ssize_t codec_reg_show(struct device *dev, 1432624d5faSMark Brown struct device_attribute *attr, char *buf) 1442624d5faSMark Brown { 145f0fba2adSLiam Girdwood struct snd_soc_pcm_runtime *rtd = 146f0fba2adSLiam Girdwood container_of(dev, struct snd_soc_pcm_runtime, dev); 147f0fba2adSLiam Girdwood 148f0fba2adSLiam Girdwood return soc_codec_reg_show(rtd->codec, buf); 1492624d5faSMark Brown } 1502624d5faSMark Brown 1512624d5faSMark Brown static DEVICE_ATTR(codec_reg, 0444, codec_reg_show, NULL); 1522624d5faSMark Brown 153dbe21408SMark Brown static ssize_t pmdown_time_show(struct device *dev, 154dbe21408SMark Brown struct device_attribute *attr, char *buf) 155dbe21408SMark Brown { 156f0fba2adSLiam Girdwood struct snd_soc_pcm_runtime *rtd = 157f0fba2adSLiam Girdwood container_of(dev, struct snd_soc_pcm_runtime, dev); 158dbe21408SMark Brown 159f0fba2adSLiam Girdwood return sprintf(buf, "%ld\n", rtd->pmdown_time); 160dbe21408SMark Brown } 161dbe21408SMark Brown 162dbe21408SMark Brown static ssize_t pmdown_time_set(struct device *dev, 163dbe21408SMark Brown struct device_attribute *attr, 164dbe21408SMark Brown const char *buf, size_t count) 165dbe21408SMark Brown { 166f0fba2adSLiam Girdwood struct snd_soc_pcm_runtime *rtd = 167f0fba2adSLiam Girdwood container_of(dev, struct snd_soc_pcm_runtime, dev); 168dbe21408SMark Brown 169f0fba2adSLiam Girdwood strict_strtol(buf, 10, &rtd->pmdown_time); 170dbe21408SMark Brown 171dbe21408SMark Brown return count; 172dbe21408SMark Brown } 173dbe21408SMark Brown 174dbe21408SMark Brown static DEVICE_ATTR(pmdown_time, 0644, pmdown_time_show, pmdown_time_set); 175dbe21408SMark Brown 1762624d5faSMark Brown #ifdef CONFIG_DEBUG_FS 1772624d5faSMark Brown static int codec_reg_open_file(struct inode *inode, struct file *file) 1782624d5faSMark Brown { 1792624d5faSMark Brown file->private_data = inode->i_private; 1802624d5faSMark Brown return 0; 1812624d5faSMark Brown } 1822624d5faSMark Brown 1832624d5faSMark Brown static ssize_t codec_reg_read_file(struct file *file, char __user *user_buf, 1842624d5faSMark Brown size_t count, loff_t *ppos) 1852624d5faSMark Brown { 1862624d5faSMark Brown ssize_t ret; 1872624d5faSMark Brown struct snd_soc_codec *codec = file->private_data; 1882624d5faSMark Brown char *buf = kmalloc(PAGE_SIZE, GFP_KERNEL); 1892624d5faSMark Brown if (!buf) 1902624d5faSMark Brown return -ENOMEM; 1912624d5faSMark Brown ret = soc_codec_reg_show(codec, buf); 1922624d5faSMark Brown if (ret >= 0) 1932624d5faSMark Brown ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret); 1942624d5faSMark Brown kfree(buf); 1952624d5faSMark Brown return ret; 1962624d5faSMark Brown } 1972624d5faSMark Brown 1982624d5faSMark Brown static ssize_t codec_reg_write_file(struct file *file, 1992624d5faSMark Brown const char __user *user_buf, size_t count, loff_t *ppos) 2002624d5faSMark Brown { 2012624d5faSMark Brown char buf[32]; 2022624d5faSMark Brown int buf_size; 2032624d5faSMark Brown char *start = buf; 2042624d5faSMark Brown unsigned long reg, value; 2052624d5faSMark Brown int step = 1; 2062624d5faSMark Brown struct snd_soc_codec *codec = file->private_data; 2072624d5faSMark Brown 2082624d5faSMark Brown buf_size = min(count, (sizeof(buf)-1)); 2092624d5faSMark Brown if (copy_from_user(buf, user_buf, buf_size)) 2102624d5faSMark Brown return -EFAULT; 2112624d5faSMark Brown buf[buf_size] = 0; 2122624d5faSMark Brown 213f0fba2adSLiam Girdwood if (codec->driver->reg_cache_step) 214f0fba2adSLiam Girdwood step = codec->driver->reg_cache_step; 2152624d5faSMark Brown 2162624d5faSMark Brown while (*start == ' ') 2172624d5faSMark Brown start++; 2182624d5faSMark Brown reg = simple_strtoul(start, &start, 16); 219f0fba2adSLiam Girdwood if ((reg >= codec->driver->reg_cache_size) || (reg % step)) 2202624d5faSMark Brown return -EINVAL; 2212624d5faSMark Brown while (*start == ' ') 2222624d5faSMark Brown start++; 2232624d5faSMark Brown if (strict_strtoul(start, 16, &value)) 2242624d5faSMark Brown return -EINVAL; 225f0fba2adSLiam Girdwood codec->driver->write(codec, reg, value); 2262624d5faSMark Brown return buf_size; 2272624d5faSMark Brown } 2282624d5faSMark Brown 2292624d5faSMark Brown static const struct file_operations codec_reg_fops = { 2302624d5faSMark Brown .open = codec_reg_open_file, 2312624d5faSMark Brown .read = codec_reg_read_file, 2322624d5faSMark Brown .write = codec_reg_write_file, 2332624d5faSMark Brown }; 2342624d5faSMark Brown 2352624d5faSMark Brown static void soc_init_codec_debugfs(struct snd_soc_codec *codec) 2362624d5faSMark Brown { 2376ba6c9c3SMark Brown codec->debugfs_codec_root = debugfs_create_dir(codec->name , 2382624d5faSMark Brown debugfs_root); 2392624d5faSMark Brown if (!codec->debugfs_codec_root) { 2402624d5faSMark Brown printk(KERN_WARNING 2412624d5faSMark Brown "ASoC: Failed to create codec debugfs directory\n"); 2422624d5faSMark Brown return; 2432624d5faSMark Brown } 2442624d5faSMark Brown 2452624d5faSMark Brown codec->debugfs_reg = debugfs_create_file("codec_reg", 0644, 2462624d5faSMark Brown codec->debugfs_codec_root, 2472624d5faSMark Brown codec, &codec_reg_fops); 2482624d5faSMark Brown if (!codec->debugfs_reg) 2492624d5faSMark Brown printk(KERN_WARNING 2502624d5faSMark Brown "ASoC: Failed to create codec register debugfs file\n"); 2512624d5faSMark Brown 252708fafb3SAxel Lin codec->debugfs_pop_time = debugfs_create_u32("dapm_pop_time", 0644, 2532624d5faSMark Brown codec->debugfs_codec_root, 2542624d5faSMark Brown &codec->pop_time); 2552624d5faSMark Brown if (!codec->debugfs_pop_time) 2562624d5faSMark Brown printk(KERN_WARNING 2572624d5faSMark Brown "Failed to create pop time debugfs file\n"); 2582624d5faSMark Brown 2592624d5faSMark Brown codec->debugfs_dapm = debugfs_create_dir("dapm", 2602624d5faSMark Brown codec->debugfs_codec_root); 2612624d5faSMark Brown if (!codec->debugfs_dapm) 2622624d5faSMark Brown printk(KERN_WARNING 2632624d5faSMark Brown "Failed to create DAPM debugfs directory\n"); 2642624d5faSMark Brown 2652624d5faSMark Brown snd_soc_dapm_debugfs_init(codec); 2662624d5faSMark Brown } 2672624d5faSMark Brown 2682624d5faSMark Brown static void soc_cleanup_codec_debugfs(struct snd_soc_codec *codec) 2692624d5faSMark Brown { 2702624d5faSMark Brown debugfs_remove_recursive(codec->debugfs_codec_root); 2712624d5faSMark Brown } 2722624d5faSMark Brown 273c3c5a19aSMark Brown static ssize_t codec_list_read_file(struct file *file, char __user *user_buf, 274c3c5a19aSMark Brown size_t count, loff_t *ppos) 275c3c5a19aSMark Brown { 276c3c5a19aSMark Brown char *buf = kmalloc(PAGE_SIZE, GFP_KERNEL); 277c3c5a19aSMark Brown ssize_t ret = 0; 278c3c5a19aSMark Brown struct snd_soc_codec *codec; 279c3c5a19aSMark Brown 280c3c5a19aSMark Brown if (!buf) 281c3c5a19aSMark Brown return -ENOMEM; 282c3c5a19aSMark Brown 283c3c5a19aSMark Brown list_for_each_entry(codec, &codec_list, list) 284c3c5a19aSMark Brown ret += snprintf(buf + ret, PAGE_SIZE - ret, "%s\n", 285c3c5a19aSMark Brown codec->name); 286c3c5a19aSMark Brown 287c3c5a19aSMark Brown if (ret >= 0) 288c3c5a19aSMark Brown ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret); 289c3c5a19aSMark Brown 290c3c5a19aSMark Brown kfree(buf); 291c3c5a19aSMark Brown 292c3c5a19aSMark Brown return ret; 293c3c5a19aSMark Brown } 294c3c5a19aSMark Brown 295c3c5a19aSMark Brown static const struct file_operations codec_list_fops = { 296c3c5a19aSMark Brown .read = codec_list_read_file, 297c3c5a19aSMark Brown .llseek = default_llseek,/* read accesses f_pos */ 298c3c5a19aSMark Brown }; 299c3c5a19aSMark Brown 300f3208780SMark Brown static ssize_t dai_list_read_file(struct file *file, char __user *user_buf, 301f3208780SMark Brown size_t count, loff_t *ppos) 302f3208780SMark Brown { 303f3208780SMark Brown char *buf = kmalloc(PAGE_SIZE, GFP_KERNEL); 304f3208780SMark Brown ssize_t ret = 0; 305f3208780SMark Brown struct snd_soc_dai *dai; 306f3208780SMark Brown 307f3208780SMark Brown if (!buf) 308f3208780SMark Brown return -ENOMEM; 309f3208780SMark Brown 310f3208780SMark Brown list_for_each_entry(dai, &dai_list, list) 311f3208780SMark Brown ret += snprintf(buf + ret, PAGE_SIZE - ret, "%s\n", dai->name); 312f3208780SMark Brown 313f3208780SMark Brown if (ret >= 0) 314f3208780SMark Brown ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret); 315f3208780SMark Brown 316f3208780SMark Brown kfree(buf); 317f3208780SMark Brown 318f3208780SMark Brown return ret; 319f3208780SMark Brown } 320f3208780SMark Brown 321f3208780SMark Brown static const struct file_operations dai_list_fops = { 322f3208780SMark Brown .read = dai_list_read_file, 323f3208780SMark Brown .llseek = default_llseek,/* read accesses f_pos */ 324f3208780SMark Brown }; 325f3208780SMark Brown 32619c7ac27SMark Brown static ssize_t platform_list_read_file(struct file *file, 32719c7ac27SMark Brown char __user *user_buf, 32819c7ac27SMark Brown size_t count, loff_t *ppos) 32919c7ac27SMark Brown { 33019c7ac27SMark Brown char *buf = kmalloc(PAGE_SIZE, GFP_KERNEL); 33119c7ac27SMark Brown ssize_t ret = 0; 33219c7ac27SMark Brown struct snd_soc_platform *platform; 33319c7ac27SMark Brown 33419c7ac27SMark Brown if (!buf) 33519c7ac27SMark Brown return -ENOMEM; 33619c7ac27SMark Brown 33719c7ac27SMark Brown list_for_each_entry(platform, &platform_list, list) 33819c7ac27SMark Brown ret += snprintf(buf + ret, PAGE_SIZE - ret, "%s\n", 33919c7ac27SMark Brown platform->name); 34019c7ac27SMark Brown 34119c7ac27SMark Brown if (ret >= 0) 34219c7ac27SMark Brown ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret); 34319c7ac27SMark Brown 34419c7ac27SMark Brown kfree(buf); 34519c7ac27SMark Brown 34619c7ac27SMark Brown return ret; 34719c7ac27SMark Brown } 34819c7ac27SMark Brown 34919c7ac27SMark Brown static const struct file_operations platform_list_fops = { 35019c7ac27SMark Brown .read = platform_list_read_file, 35119c7ac27SMark Brown .llseek = default_llseek,/* read accesses f_pos */ 35219c7ac27SMark Brown }; 35319c7ac27SMark Brown 3542624d5faSMark Brown #else 3552624d5faSMark Brown 3562624d5faSMark Brown static inline void soc_init_codec_debugfs(struct snd_soc_codec *codec) 3572624d5faSMark Brown { 3582624d5faSMark Brown } 3592624d5faSMark Brown 3602624d5faSMark Brown static inline void soc_cleanup_codec_debugfs(struct snd_soc_codec *codec) 3612624d5faSMark Brown { 3622624d5faSMark Brown } 3632624d5faSMark Brown #endif 3642624d5faSMark Brown 365db2a4165SFrank Mandarino #ifdef CONFIG_SND_SOC_AC97_BUS 366db2a4165SFrank Mandarino /* unregister ac97 codec */ 367db2a4165SFrank Mandarino static int soc_ac97_dev_unregister(struct snd_soc_codec *codec) 368db2a4165SFrank Mandarino { 369db2a4165SFrank Mandarino if (codec->ac97->dev.bus) 370db2a4165SFrank Mandarino device_unregister(&codec->ac97->dev); 371db2a4165SFrank Mandarino return 0; 372db2a4165SFrank Mandarino } 373db2a4165SFrank Mandarino 374db2a4165SFrank Mandarino /* stop no dev release warning */ 375db2a4165SFrank Mandarino static void soc_ac97_device_release(struct device *dev){} 376db2a4165SFrank Mandarino 377db2a4165SFrank Mandarino /* register ac97 codec to bus */ 378db2a4165SFrank Mandarino static int soc_ac97_dev_register(struct snd_soc_codec *codec) 379db2a4165SFrank Mandarino { 380db2a4165SFrank Mandarino int err; 381db2a4165SFrank Mandarino 382db2a4165SFrank Mandarino codec->ac97->dev.bus = &ac97_bus_type; 3834ac5c61fSMark Brown codec->ac97->dev.parent = codec->card->dev; 384db2a4165SFrank Mandarino codec->ac97->dev.release = soc_ac97_device_release; 385db2a4165SFrank Mandarino 386bb072bf0SKay Sievers dev_set_name(&codec->ac97->dev, "%d-%d:%s", 387f0fba2adSLiam Girdwood codec->card->snd_card->number, 0, codec->name); 388db2a4165SFrank Mandarino err = device_register(&codec->ac97->dev); 389db2a4165SFrank Mandarino if (err < 0) { 390db2a4165SFrank Mandarino snd_printk(KERN_ERR "Can't register ac97 bus\n"); 391db2a4165SFrank Mandarino codec->ac97->dev.bus = NULL; 392db2a4165SFrank Mandarino return err; 393db2a4165SFrank Mandarino } 394db2a4165SFrank Mandarino return 0; 395db2a4165SFrank Mandarino } 396db2a4165SFrank Mandarino #endif 397db2a4165SFrank Mandarino 39806f409d7SMark Brown static int soc_pcm_apply_symmetry(struct snd_pcm_substream *substream) 39906f409d7SMark Brown { 40006f409d7SMark Brown struct snd_soc_pcm_runtime *rtd = substream->private_data; 401f0fba2adSLiam Girdwood struct snd_soc_dai *cpu_dai = rtd->cpu_dai; 402f0fba2adSLiam Girdwood struct snd_soc_dai *codec_dai = rtd->codec_dai; 40306f409d7SMark Brown int ret; 40406f409d7SMark Brown 405f0fba2adSLiam Girdwood if (codec_dai->driver->symmetric_rates || cpu_dai->driver->symmetric_rates || 406f0fba2adSLiam Girdwood rtd->dai_link->symmetric_rates) { 407f0fba2adSLiam Girdwood dev_dbg(&rtd->dev, "Symmetry forces %dHz rate\n", 408f0fba2adSLiam Girdwood rtd->rate); 40906f409d7SMark Brown 41006f409d7SMark Brown ret = snd_pcm_hw_constraint_minmax(substream->runtime, 41106f409d7SMark Brown SNDRV_PCM_HW_PARAM_RATE, 412f0fba2adSLiam Girdwood rtd->rate, 413f0fba2adSLiam Girdwood rtd->rate); 41406f409d7SMark Brown if (ret < 0) { 415f0fba2adSLiam Girdwood dev_err(&rtd->dev, 41606f409d7SMark Brown "Unable to apply rate symmetry constraint: %d\n", ret); 41706f409d7SMark Brown return ret; 41806f409d7SMark Brown } 41906f409d7SMark Brown } 42006f409d7SMark Brown 42106f409d7SMark Brown return 0; 42206f409d7SMark Brown } 42306f409d7SMark Brown 424db2a4165SFrank Mandarino /* 425db2a4165SFrank Mandarino * Called by ALSA when a PCM substream is opened, the runtime->hw record is 426db2a4165SFrank Mandarino * then initialized and any private data can be allocated. This also calls 427db2a4165SFrank Mandarino * startup for the cpu DAI, platform, machine and codec DAI. 428db2a4165SFrank Mandarino */ 429db2a4165SFrank Mandarino static int soc_pcm_open(struct snd_pcm_substream *substream) 430db2a4165SFrank Mandarino { 431db2a4165SFrank Mandarino struct snd_soc_pcm_runtime *rtd = substream->private_data; 432db2a4165SFrank Mandarino struct snd_pcm_runtime *runtime = substream->runtime; 433f0fba2adSLiam Girdwood struct snd_soc_platform *platform = rtd->platform; 434f0fba2adSLiam Girdwood struct snd_soc_dai *cpu_dai = rtd->cpu_dai; 435f0fba2adSLiam Girdwood struct snd_soc_dai *codec_dai = rtd->codec_dai; 436f0fba2adSLiam Girdwood struct snd_soc_dai_driver *cpu_dai_drv = cpu_dai->driver; 437f0fba2adSLiam Girdwood struct snd_soc_dai_driver *codec_dai_drv = codec_dai->driver; 438db2a4165SFrank Mandarino int ret = 0; 439db2a4165SFrank Mandarino 440db2a4165SFrank Mandarino mutex_lock(&pcm_mutex); 441db2a4165SFrank Mandarino 442db2a4165SFrank Mandarino /* startup the audio subsystem */ 443f0fba2adSLiam Girdwood if (cpu_dai->driver->ops->startup) { 444f0fba2adSLiam Girdwood ret = cpu_dai->driver->ops->startup(substream, cpu_dai); 445db2a4165SFrank Mandarino if (ret < 0) { 446db2a4165SFrank Mandarino printk(KERN_ERR "asoc: can't open interface %s\n", 447cb666e5bSLiam Girdwood cpu_dai->name); 448db2a4165SFrank Mandarino goto out; 449db2a4165SFrank Mandarino } 450db2a4165SFrank Mandarino } 451db2a4165SFrank Mandarino 452f0fba2adSLiam Girdwood if (platform->driver->ops->open) { 453f0fba2adSLiam Girdwood ret = platform->driver->ops->open(substream); 454db2a4165SFrank Mandarino if (ret < 0) { 455db2a4165SFrank Mandarino printk(KERN_ERR "asoc: can't open platform %s\n", platform->name); 456db2a4165SFrank Mandarino goto platform_err; 457db2a4165SFrank Mandarino } 458db2a4165SFrank Mandarino } 459db2a4165SFrank Mandarino 460f0fba2adSLiam Girdwood if (codec_dai->driver->ops->startup) { 461f0fba2adSLiam Girdwood ret = codec_dai->driver->ops->startup(substream, codec_dai); 462cb666e5bSLiam Girdwood if (ret < 0) { 463cb666e5bSLiam Girdwood printk(KERN_ERR "asoc: can't open codec %s\n", 464cb666e5bSLiam Girdwood codec_dai->name); 465cb666e5bSLiam Girdwood goto codec_dai_err; 466cb666e5bSLiam Girdwood } 467cb666e5bSLiam Girdwood } 468cb666e5bSLiam Girdwood 469f0fba2adSLiam Girdwood if (rtd->dai_link->ops && rtd->dai_link->ops->startup) { 470f0fba2adSLiam Girdwood ret = rtd->dai_link->ops->startup(substream); 471db2a4165SFrank Mandarino if (ret < 0) { 472f0fba2adSLiam Girdwood printk(KERN_ERR "asoc: %s startup failed\n", rtd->dai_link->name); 473db2a4165SFrank Mandarino goto machine_err; 474db2a4165SFrank Mandarino } 475db2a4165SFrank Mandarino } 476db2a4165SFrank Mandarino 477db2a4165SFrank Mandarino /* Check that the codec and cpu DAI's are compatible */ 478db2a4165SFrank Mandarino if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) { 479db2a4165SFrank Mandarino runtime->hw.rate_min = 480f0fba2adSLiam Girdwood max(codec_dai_drv->playback.rate_min, 481f0fba2adSLiam Girdwood cpu_dai_drv->playback.rate_min); 482db2a4165SFrank Mandarino runtime->hw.rate_max = 483f0fba2adSLiam Girdwood min(codec_dai_drv->playback.rate_max, 484f0fba2adSLiam Girdwood cpu_dai_drv->playback.rate_max); 485db2a4165SFrank Mandarino runtime->hw.channels_min = 486f0fba2adSLiam Girdwood max(codec_dai_drv->playback.channels_min, 487f0fba2adSLiam Girdwood cpu_dai_drv->playback.channels_min); 488db2a4165SFrank Mandarino runtime->hw.channels_max = 489f0fba2adSLiam Girdwood min(codec_dai_drv->playback.channels_max, 490f0fba2adSLiam Girdwood cpu_dai_drv->playback.channels_max); 491cb666e5bSLiam Girdwood runtime->hw.formats = 492f0fba2adSLiam Girdwood codec_dai_drv->playback.formats & cpu_dai_drv->playback.formats; 493cb666e5bSLiam Girdwood runtime->hw.rates = 494f0fba2adSLiam Girdwood codec_dai_drv->playback.rates & cpu_dai_drv->playback.rates; 495f0fba2adSLiam Girdwood if (codec_dai_drv->playback.rates 496d9ad6296SJassi Brar & (SNDRV_PCM_RATE_KNOT | SNDRV_PCM_RATE_CONTINUOUS)) 497f0fba2adSLiam Girdwood runtime->hw.rates |= cpu_dai_drv->playback.rates; 498f0fba2adSLiam Girdwood if (cpu_dai_drv->playback.rates 499d9ad6296SJassi Brar & (SNDRV_PCM_RATE_KNOT | SNDRV_PCM_RATE_CONTINUOUS)) 500f0fba2adSLiam Girdwood runtime->hw.rates |= codec_dai_drv->playback.rates; 501db2a4165SFrank Mandarino } else { 502db2a4165SFrank Mandarino runtime->hw.rate_min = 503f0fba2adSLiam Girdwood max(codec_dai_drv->capture.rate_min, 504f0fba2adSLiam Girdwood cpu_dai_drv->capture.rate_min); 505db2a4165SFrank Mandarino runtime->hw.rate_max = 506f0fba2adSLiam Girdwood min(codec_dai_drv->capture.rate_max, 507f0fba2adSLiam Girdwood cpu_dai_drv->capture.rate_max); 508db2a4165SFrank Mandarino runtime->hw.channels_min = 509f0fba2adSLiam Girdwood max(codec_dai_drv->capture.channels_min, 510f0fba2adSLiam Girdwood cpu_dai_drv->capture.channels_min); 511db2a4165SFrank Mandarino runtime->hw.channels_max = 512f0fba2adSLiam Girdwood min(codec_dai_drv->capture.channels_max, 513f0fba2adSLiam Girdwood cpu_dai_drv->capture.channels_max); 514cb666e5bSLiam Girdwood runtime->hw.formats = 515f0fba2adSLiam Girdwood codec_dai_drv->capture.formats & cpu_dai_drv->capture.formats; 516cb666e5bSLiam Girdwood runtime->hw.rates = 517f0fba2adSLiam Girdwood codec_dai_drv->capture.rates & cpu_dai_drv->capture.rates; 518f0fba2adSLiam Girdwood if (codec_dai_drv->capture.rates 519d9ad6296SJassi Brar & (SNDRV_PCM_RATE_KNOT | SNDRV_PCM_RATE_CONTINUOUS)) 520f0fba2adSLiam Girdwood runtime->hw.rates |= cpu_dai_drv->capture.rates; 521f0fba2adSLiam Girdwood if (cpu_dai_drv->capture.rates 522d9ad6296SJassi Brar & (SNDRV_PCM_RATE_KNOT | SNDRV_PCM_RATE_CONTINUOUS)) 523f0fba2adSLiam Girdwood runtime->hw.rates |= codec_dai_drv->capture.rates; 524db2a4165SFrank Mandarino } 525db2a4165SFrank Mandarino 526db2a4165SFrank Mandarino snd_pcm_limit_hw_rates(runtime); 527db2a4165SFrank Mandarino if (!runtime->hw.rates) { 528db2a4165SFrank Mandarino printk(KERN_ERR "asoc: %s <-> %s No matching rates\n", 529cb666e5bSLiam Girdwood codec_dai->name, cpu_dai->name); 530bb1c0478SJassi Brar goto config_err; 531db2a4165SFrank Mandarino } 532db2a4165SFrank Mandarino if (!runtime->hw.formats) { 533db2a4165SFrank Mandarino printk(KERN_ERR "asoc: %s <-> %s No matching formats\n", 534cb666e5bSLiam Girdwood codec_dai->name, cpu_dai->name); 535bb1c0478SJassi Brar goto config_err; 536db2a4165SFrank Mandarino } 537db2a4165SFrank Mandarino if (!runtime->hw.channels_min || !runtime->hw.channels_max) { 538db2a4165SFrank Mandarino printk(KERN_ERR "asoc: %s <-> %s No matching channels\n", 539cb666e5bSLiam Girdwood codec_dai->name, cpu_dai->name); 540bb1c0478SJassi Brar goto config_err; 541db2a4165SFrank Mandarino } 542db2a4165SFrank Mandarino 54306f409d7SMark Brown /* Symmetry only applies if we've already got an active stream. */ 54406f409d7SMark Brown if (cpu_dai->active || codec_dai->active) { 54506f409d7SMark Brown ret = soc_pcm_apply_symmetry(substream); 54606f409d7SMark Brown if (ret != 0) 547bb1c0478SJassi Brar goto config_err; 54806f409d7SMark Brown } 54906f409d7SMark Brown 550f0fba2adSLiam Girdwood pr_debug("asoc: %s <-> %s info:\n", 551f0fba2adSLiam Girdwood codec_dai->name, cpu_dai->name); 552f24368c2SMark Brown pr_debug("asoc: rate mask 0x%x\n", runtime->hw.rates); 553f24368c2SMark Brown pr_debug("asoc: min ch %d max ch %d\n", runtime->hw.channels_min, 554b5c5fd24SLiam Girdwood runtime->hw.channels_max); 555f24368c2SMark Brown pr_debug("asoc: min rate %d max rate %d\n", runtime->hw.rate_min, 556b5c5fd24SLiam Girdwood runtime->hw.rate_max); 557db2a4165SFrank Mandarino 55814dc5734SJassi Brar if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) { 559f0fba2adSLiam Girdwood cpu_dai->playback_active++; 560f0fba2adSLiam Girdwood codec_dai->playback_active++; 56114dc5734SJassi Brar } else { 562f0fba2adSLiam Girdwood cpu_dai->capture_active++; 563f0fba2adSLiam Girdwood codec_dai->capture_active++; 56414dc5734SJassi Brar } 56514dc5734SJassi Brar cpu_dai->active++; 56614dc5734SJassi Brar codec_dai->active++; 567f0fba2adSLiam Girdwood rtd->codec->active++; 568db2a4165SFrank Mandarino mutex_unlock(&pcm_mutex); 569db2a4165SFrank Mandarino return 0; 570db2a4165SFrank Mandarino 571bb1c0478SJassi Brar config_err: 572f0fba2adSLiam Girdwood if (rtd->dai_link->ops && rtd->dai_link->ops->shutdown) 573f0fba2adSLiam Girdwood rtd->dai_link->ops->shutdown(substream); 574db2a4165SFrank Mandarino 575bb1c0478SJassi Brar machine_err: 576f0fba2adSLiam Girdwood if (codec_dai->driver->ops->shutdown) 577f0fba2adSLiam Girdwood codec_dai->driver->ops->shutdown(substream, codec_dai); 578bb1c0478SJassi Brar 579cb666e5bSLiam Girdwood codec_dai_err: 580f0fba2adSLiam Girdwood if (platform->driver->ops->close) 581f0fba2adSLiam Girdwood platform->driver->ops->close(substream); 582db2a4165SFrank Mandarino 583db2a4165SFrank Mandarino platform_err: 584f0fba2adSLiam Girdwood if (cpu_dai->driver->ops->shutdown) 585f0fba2adSLiam Girdwood cpu_dai->driver->ops->shutdown(substream, cpu_dai); 586db2a4165SFrank Mandarino out: 587db2a4165SFrank Mandarino mutex_unlock(&pcm_mutex); 588db2a4165SFrank Mandarino return ret; 589db2a4165SFrank Mandarino } 590db2a4165SFrank Mandarino 591db2a4165SFrank Mandarino /* 5923a4fa0a2SRobert P. J. Day * Power down the audio subsystem pmdown_time msecs after close is called. 593db2a4165SFrank Mandarino * This is to ensure there are no pops or clicks in between any music tracks 594db2a4165SFrank Mandarino * due to DAPM power cycling. 595db2a4165SFrank Mandarino */ 5964484bb2eSAndrew Morton static void close_delayed_work(struct work_struct *work) 597db2a4165SFrank Mandarino { 598f0fba2adSLiam Girdwood struct snd_soc_pcm_runtime *rtd = 599f0fba2adSLiam Girdwood container_of(work, struct snd_soc_pcm_runtime, delayed_work.work); 600f0fba2adSLiam Girdwood struct snd_soc_dai *codec_dai = rtd->codec_dai; 601db2a4165SFrank Mandarino 602db2a4165SFrank Mandarino mutex_lock(&pcm_mutex); 603db2a4165SFrank Mandarino 604f24368c2SMark Brown pr_debug("pop wq checking: %s status: %s waiting: %s\n", 605f0fba2adSLiam Girdwood codec_dai->driver->playback.stream_name, 606f0fba2adSLiam Girdwood codec_dai->playback_active ? "active" : "inactive", 607db2a4165SFrank Mandarino codec_dai->pop_wait ? "yes" : "no"); 608db2a4165SFrank Mandarino 609db2a4165SFrank Mandarino /* are we waiting on this codec DAI stream */ 610db2a4165SFrank Mandarino if (codec_dai->pop_wait == 1) { 611db2a4165SFrank Mandarino codec_dai->pop_wait = 0; 612f0fba2adSLiam Girdwood snd_soc_dapm_stream_event(rtd, 613f0fba2adSLiam Girdwood codec_dai->driver->playback.stream_name, 614db2a4165SFrank Mandarino SND_SOC_DAPM_STREAM_STOP); 615db2a4165SFrank Mandarino } 616f0fba2adSLiam Girdwood 617db2a4165SFrank Mandarino mutex_unlock(&pcm_mutex); 618db2a4165SFrank Mandarino } 619db2a4165SFrank Mandarino 620db2a4165SFrank Mandarino /* 621db2a4165SFrank Mandarino * Called by ALSA when a PCM substream is closed. Private data can be 622db2a4165SFrank Mandarino * freed here. The cpu DAI, codec DAI, machine and platform are also 623db2a4165SFrank Mandarino * shutdown. 624db2a4165SFrank Mandarino */ 625db2a4165SFrank Mandarino static int soc_codec_close(struct snd_pcm_substream *substream) 626db2a4165SFrank Mandarino { 627db2a4165SFrank Mandarino struct snd_soc_pcm_runtime *rtd = substream->private_data; 628f0fba2adSLiam Girdwood struct snd_soc_platform *platform = rtd->platform; 629f0fba2adSLiam Girdwood struct snd_soc_dai *cpu_dai = rtd->cpu_dai; 630f0fba2adSLiam Girdwood struct snd_soc_dai *codec_dai = rtd->codec_dai; 631f0fba2adSLiam Girdwood struct snd_soc_codec *codec = rtd->codec; 632db2a4165SFrank Mandarino 633db2a4165SFrank Mandarino mutex_lock(&pcm_mutex); 634db2a4165SFrank Mandarino 63514dc5734SJassi Brar if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) { 636f0fba2adSLiam Girdwood cpu_dai->playback_active--; 637f0fba2adSLiam Girdwood codec_dai->playback_active--; 63814dc5734SJassi Brar } else { 639f0fba2adSLiam Girdwood cpu_dai->capture_active--; 640f0fba2adSLiam Girdwood codec_dai->capture_active--; 641db2a4165SFrank Mandarino } 64214dc5734SJassi Brar 64314dc5734SJassi Brar cpu_dai->active--; 64414dc5734SJassi Brar codec_dai->active--; 645db2a4165SFrank Mandarino codec->active--; 646db2a4165SFrank Mandarino 6476010b2daSMark Brown /* Muting the DAC suppresses artifacts caused during digital 6486010b2daSMark Brown * shutdown, for example from stopping clocks. 6496010b2daSMark Brown */ 6506010b2daSMark Brown if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) 6516010b2daSMark Brown snd_soc_dai_digital_mute(codec_dai, 1); 6526010b2daSMark Brown 653f0fba2adSLiam Girdwood if (cpu_dai->driver->ops->shutdown) 654f0fba2adSLiam Girdwood cpu_dai->driver->ops->shutdown(substream, cpu_dai); 655db2a4165SFrank Mandarino 656f0fba2adSLiam Girdwood if (codec_dai->driver->ops->shutdown) 657f0fba2adSLiam Girdwood codec_dai->driver->ops->shutdown(substream, codec_dai); 658db2a4165SFrank Mandarino 659f0fba2adSLiam Girdwood if (rtd->dai_link->ops && rtd->dai_link->ops->shutdown) 660f0fba2adSLiam Girdwood rtd->dai_link->ops->shutdown(substream); 661db2a4165SFrank Mandarino 662f0fba2adSLiam Girdwood if (platform->driver->ops->close) 663f0fba2adSLiam Girdwood platform->driver->ops->close(substream); 664f0fba2adSLiam Girdwood cpu_dai->runtime = NULL; 665db2a4165SFrank Mandarino 666db2a4165SFrank Mandarino if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) { 667db2a4165SFrank Mandarino /* start delayed pop wq here for playback streams */ 668cb666e5bSLiam Girdwood codec_dai->pop_wait = 1; 669f0fba2adSLiam Girdwood schedule_delayed_work(&rtd->delayed_work, 670f0fba2adSLiam Girdwood msecs_to_jiffies(rtd->pmdown_time)); 671db2a4165SFrank Mandarino } else { 672db2a4165SFrank Mandarino /* capture streams can be powered down now */ 673f0fba2adSLiam Girdwood snd_soc_dapm_stream_event(rtd, 674f0fba2adSLiam Girdwood codec_dai->driver->capture.stream_name, 6750b4d221bSLiam Girdwood SND_SOC_DAPM_STREAM_STOP); 676db2a4165SFrank Mandarino } 677db2a4165SFrank Mandarino 678db2a4165SFrank Mandarino mutex_unlock(&pcm_mutex); 679db2a4165SFrank Mandarino return 0; 680db2a4165SFrank Mandarino } 681db2a4165SFrank Mandarino 682db2a4165SFrank Mandarino /* 683db2a4165SFrank Mandarino * Called by ALSA when the PCM substream is prepared, can set format, sample 684db2a4165SFrank Mandarino * rate, etc. This function is non atomic and can be called multiple times, 685db2a4165SFrank Mandarino * it can refer to the runtime info. 686db2a4165SFrank Mandarino */ 687db2a4165SFrank Mandarino static int soc_pcm_prepare(struct snd_pcm_substream *substream) 688db2a4165SFrank Mandarino { 689db2a4165SFrank Mandarino struct snd_soc_pcm_runtime *rtd = substream->private_data; 690f0fba2adSLiam Girdwood struct snd_soc_platform *platform = rtd->platform; 691f0fba2adSLiam Girdwood struct snd_soc_dai *cpu_dai = rtd->cpu_dai; 692f0fba2adSLiam Girdwood struct snd_soc_dai *codec_dai = rtd->codec_dai; 693db2a4165SFrank Mandarino int ret = 0; 694db2a4165SFrank Mandarino 695db2a4165SFrank Mandarino mutex_lock(&pcm_mutex); 696cb666e5bSLiam Girdwood 697f0fba2adSLiam Girdwood if (rtd->dai_link->ops && rtd->dai_link->ops->prepare) { 698f0fba2adSLiam Girdwood ret = rtd->dai_link->ops->prepare(substream); 699cb666e5bSLiam Girdwood if (ret < 0) { 700cb666e5bSLiam Girdwood printk(KERN_ERR "asoc: machine prepare error\n"); 701cb666e5bSLiam Girdwood goto out; 702cb666e5bSLiam Girdwood } 703cb666e5bSLiam Girdwood } 704cb666e5bSLiam Girdwood 705f0fba2adSLiam Girdwood if (platform->driver->ops->prepare) { 706f0fba2adSLiam Girdwood ret = platform->driver->ops->prepare(substream); 707a71a468aSLiam Girdwood if (ret < 0) { 708a71a468aSLiam Girdwood printk(KERN_ERR "asoc: platform prepare error\n"); 709db2a4165SFrank Mandarino goto out; 710db2a4165SFrank Mandarino } 711a71a468aSLiam Girdwood } 712db2a4165SFrank Mandarino 713f0fba2adSLiam Girdwood if (codec_dai->driver->ops->prepare) { 714f0fba2adSLiam Girdwood ret = codec_dai->driver->ops->prepare(substream, codec_dai); 715a71a468aSLiam Girdwood if (ret < 0) { 716a71a468aSLiam Girdwood printk(KERN_ERR "asoc: codec DAI prepare error\n"); 717db2a4165SFrank Mandarino goto out; 718db2a4165SFrank Mandarino } 719a71a468aSLiam Girdwood } 720db2a4165SFrank Mandarino 721f0fba2adSLiam Girdwood if (cpu_dai->driver->ops->prepare) { 722f0fba2adSLiam Girdwood ret = cpu_dai->driver->ops->prepare(substream, cpu_dai); 723cb666e5bSLiam Girdwood if (ret < 0) { 724cb666e5bSLiam Girdwood printk(KERN_ERR "asoc: cpu DAI prepare error\n"); 725cb666e5bSLiam Girdwood goto out; 726cb666e5bSLiam Girdwood } 727cb666e5bSLiam Girdwood } 728db2a4165SFrank Mandarino 729d45f6219SMark Brown /* cancel any delayed stream shutdown that is pending */ 730d45f6219SMark Brown if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK && 731d45f6219SMark Brown codec_dai->pop_wait) { 732cb666e5bSLiam Girdwood codec_dai->pop_wait = 0; 733f0fba2adSLiam Girdwood cancel_delayed_work(&rtd->delayed_work); 734db2a4165SFrank Mandarino } 735db2a4165SFrank Mandarino 736db2a4165SFrank Mandarino if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) 737f0fba2adSLiam Girdwood snd_soc_dapm_stream_event(rtd, 738f0fba2adSLiam Girdwood codec_dai->driver->playback.stream_name, 739db2a4165SFrank Mandarino SND_SOC_DAPM_STREAM_START); 740db2a4165SFrank Mandarino else 741f0fba2adSLiam Girdwood snd_soc_dapm_stream_event(rtd, 742f0fba2adSLiam Girdwood codec_dai->driver->capture.stream_name, 743db2a4165SFrank Mandarino SND_SOC_DAPM_STREAM_START); 7448c6529dbSLiam Girdwood 7458c6529dbSLiam Girdwood snd_soc_dai_digital_mute(codec_dai, 0); 746db2a4165SFrank Mandarino 747db2a4165SFrank Mandarino out: 748db2a4165SFrank Mandarino mutex_unlock(&pcm_mutex); 749db2a4165SFrank Mandarino return ret; 750db2a4165SFrank Mandarino } 751db2a4165SFrank Mandarino 752db2a4165SFrank Mandarino /* 753db2a4165SFrank Mandarino * Called by ALSA when the hardware params are set by application. This 754db2a4165SFrank Mandarino * function can also be called multiple times and can allocate buffers 755db2a4165SFrank Mandarino * (using snd_pcm_lib_* ). It's non-atomic. 756db2a4165SFrank Mandarino */ 757db2a4165SFrank Mandarino static int soc_pcm_hw_params(struct snd_pcm_substream *substream, 758db2a4165SFrank Mandarino struct snd_pcm_hw_params *params) 759db2a4165SFrank Mandarino { 760db2a4165SFrank Mandarino struct snd_soc_pcm_runtime *rtd = substream->private_data; 761f0fba2adSLiam Girdwood struct snd_soc_platform *platform = rtd->platform; 762f0fba2adSLiam Girdwood struct snd_soc_dai *cpu_dai = rtd->cpu_dai; 763f0fba2adSLiam Girdwood struct snd_soc_dai *codec_dai = rtd->codec_dai; 764db2a4165SFrank Mandarino int ret = 0; 765db2a4165SFrank Mandarino 766db2a4165SFrank Mandarino mutex_lock(&pcm_mutex); 767db2a4165SFrank Mandarino 768f0fba2adSLiam Girdwood if (rtd->dai_link->ops && rtd->dai_link->ops->hw_params) { 769f0fba2adSLiam Girdwood ret = rtd->dai_link->ops->hw_params(substream, params); 770cb666e5bSLiam Girdwood if (ret < 0) { 771cb666e5bSLiam Girdwood printk(KERN_ERR "asoc: machine hw_params failed\n"); 772db2a4165SFrank Mandarino goto out; 773db2a4165SFrank Mandarino } 774cb666e5bSLiam Girdwood } 775db2a4165SFrank Mandarino 776f0fba2adSLiam Girdwood if (codec_dai->driver->ops->hw_params) { 777f0fba2adSLiam Girdwood ret = codec_dai->driver->ops->hw_params(substream, params, codec_dai); 778db2a4165SFrank Mandarino if (ret < 0) { 779db2a4165SFrank Mandarino printk(KERN_ERR "asoc: can't set codec %s hw params\n", 780cb666e5bSLiam Girdwood codec_dai->name); 781cb666e5bSLiam Girdwood goto codec_err; 782db2a4165SFrank Mandarino } 783db2a4165SFrank Mandarino } 784db2a4165SFrank Mandarino 785f0fba2adSLiam Girdwood if (cpu_dai->driver->ops->hw_params) { 786f0fba2adSLiam Girdwood ret = cpu_dai->driver->ops->hw_params(substream, params, cpu_dai); 787db2a4165SFrank Mandarino if (ret < 0) { 7883ff3f64bSMark Brown printk(KERN_ERR "asoc: interface %s hw params failed\n", 789cb666e5bSLiam Girdwood cpu_dai->name); 790db2a4165SFrank Mandarino goto interface_err; 791db2a4165SFrank Mandarino } 792db2a4165SFrank Mandarino } 793db2a4165SFrank Mandarino 794f0fba2adSLiam Girdwood if (platform->driver->ops->hw_params) { 795f0fba2adSLiam Girdwood ret = platform->driver->ops->hw_params(substream, params); 796db2a4165SFrank Mandarino if (ret < 0) { 7973ff3f64bSMark Brown printk(KERN_ERR "asoc: platform %s hw params failed\n", 798db2a4165SFrank Mandarino platform->name); 799db2a4165SFrank Mandarino goto platform_err; 800db2a4165SFrank Mandarino } 801db2a4165SFrank Mandarino } 802db2a4165SFrank Mandarino 803f0fba2adSLiam Girdwood rtd->rate = params_rate(params); 80406f409d7SMark Brown 805db2a4165SFrank Mandarino out: 806db2a4165SFrank Mandarino mutex_unlock(&pcm_mutex); 807db2a4165SFrank Mandarino return ret; 808db2a4165SFrank Mandarino 809db2a4165SFrank Mandarino platform_err: 810f0fba2adSLiam Girdwood if (cpu_dai->driver->ops->hw_free) 811f0fba2adSLiam Girdwood cpu_dai->driver->ops->hw_free(substream, cpu_dai); 812db2a4165SFrank Mandarino 813db2a4165SFrank Mandarino interface_err: 814f0fba2adSLiam Girdwood if (codec_dai->driver->ops->hw_free) 815f0fba2adSLiam Girdwood codec_dai->driver->ops->hw_free(substream, codec_dai); 816cb666e5bSLiam Girdwood 817cb666e5bSLiam Girdwood codec_err: 818f0fba2adSLiam Girdwood if (rtd->dai_link->ops && rtd->dai_link->ops->hw_free) 819f0fba2adSLiam Girdwood rtd->dai_link->ops->hw_free(substream); 820db2a4165SFrank Mandarino 821db2a4165SFrank Mandarino mutex_unlock(&pcm_mutex); 822db2a4165SFrank Mandarino return ret; 823db2a4165SFrank Mandarino } 824db2a4165SFrank Mandarino 825db2a4165SFrank Mandarino /* 826db2a4165SFrank Mandarino * Free's resources allocated by hw_params, can be called multiple times 827db2a4165SFrank Mandarino */ 828db2a4165SFrank Mandarino static int soc_pcm_hw_free(struct snd_pcm_substream *substream) 829db2a4165SFrank Mandarino { 830db2a4165SFrank Mandarino struct snd_soc_pcm_runtime *rtd = substream->private_data; 831f0fba2adSLiam Girdwood struct snd_soc_platform *platform = rtd->platform; 832f0fba2adSLiam Girdwood struct snd_soc_dai *cpu_dai = rtd->cpu_dai; 833f0fba2adSLiam Girdwood struct snd_soc_dai *codec_dai = rtd->codec_dai; 834f0fba2adSLiam Girdwood struct snd_soc_codec *codec = rtd->codec; 835db2a4165SFrank Mandarino 836db2a4165SFrank Mandarino mutex_lock(&pcm_mutex); 837db2a4165SFrank Mandarino 838db2a4165SFrank Mandarino /* apply codec digital mute */ 8398c6529dbSLiam Girdwood if (!codec->active) 8408c6529dbSLiam Girdwood snd_soc_dai_digital_mute(codec_dai, 1); 841db2a4165SFrank Mandarino 842db2a4165SFrank Mandarino /* free any machine hw params */ 843f0fba2adSLiam Girdwood if (rtd->dai_link->ops && rtd->dai_link->ops->hw_free) 844f0fba2adSLiam Girdwood rtd->dai_link->ops->hw_free(substream); 845db2a4165SFrank Mandarino 846db2a4165SFrank Mandarino /* free any DMA resources */ 847f0fba2adSLiam Girdwood if (platform->driver->ops->hw_free) 848f0fba2adSLiam Girdwood platform->driver->ops->hw_free(substream); 849db2a4165SFrank Mandarino 850db2a4165SFrank Mandarino /* now free hw params for the DAI's */ 851f0fba2adSLiam Girdwood if (codec_dai->driver->ops->hw_free) 852f0fba2adSLiam Girdwood codec_dai->driver->ops->hw_free(substream, codec_dai); 853db2a4165SFrank Mandarino 854f0fba2adSLiam Girdwood if (cpu_dai->driver->ops->hw_free) 855f0fba2adSLiam Girdwood cpu_dai->driver->ops->hw_free(substream, cpu_dai); 856db2a4165SFrank Mandarino 857db2a4165SFrank Mandarino mutex_unlock(&pcm_mutex); 858db2a4165SFrank Mandarino return 0; 859db2a4165SFrank Mandarino } 860db2a4165SFrank Mandarino 861db2a4165SFrank Mandarino static int soc_pcm_trigger(struct snd_pcm_substream *substream, int cmd) 862db2a4165SFrank Mandarino { 863db2a4165SFrank Mandarino struct snd_soc_pcm_runtime *rtd = substream->private_data; 864f0fba2adSLiam Girdwood struct snd_soc_platform *platform = rtd->platform; 865f0fba2adSLiam Girdwood struct snd_soc_dai *cpu_dai = rtd->cpu_dai; 866f0fba2adSLiam Girdwood struct snd_soc_dai *codec_dai = rtd->codec_dai; 867db2a4165SFrank Mandarino int ret; 868db2a4165SFrank Mandarino 869f0fba2adSLiam Girdwood if (codec_dai->driver->ops->trigger) { 870f0fba2adSLiam Girdwood ret = codec_dai->driver->ops->trigger(substream, cmd, codec_dai); 871db2a4165SFrank Mandarino if (ret < 0) 872db2a4165SFrank Mandarino return ret; 873db2a4165SFrank Mandarino } 874db2a4165SFrank Mandarino 875f0fba2adSLiam Girdwood if (platform->driver->ops->trigger) { 876f0fba2adSLiam Girdwood ret = platform->driver->ops->trigger(substream, cmd); 877db2a4165SFrank Mandarino if (ret < 0) 878db2a4165SFrank Mandarino return ret; 879db2a4165SFrank Mandarino } 880db2a4165SFrank Mandarino 881f0fba2adSLiam Girdwood if (cpu_dai->driver->ops->trigger) { 882f0fba2adSLiam Girdwood ret = cpu_dai->driver->ops->trigger(substream, cmd, cpu_dai); 883db2a4165SFrank Mandarino if (ret < 0) 884db2a4165SFrank Mandarino return ret; 885db2a4165SFrank Mandarino } 886db2a4165SFrank Mandarino return 0; 887db2a4165SFrank Mandarino } 888db2a4165SFrank Mandarino 889377b6f62SPeter Ujfalusi /* 890377b6f62SPeter Ujfalusi * soc level wrapper for pointer callback 891258020d0SPeter Ujfalusi * If cpu_dai, codec_dai, platform driver has the delay callback, than 892258020d0SPeter Ujfalusi * the runtime->delay will be updated accordingly. 893377b6f62SPeter Ujfalusi */ 894377b6f62SPeter Ujfalusi static snd_pcm_uframes_t soc_pcm_pointer(struct snd_pcm_substream *substream) 895377b6f62SPeter Ujfalusi { 896377b6f62SPeter Ujfalusi struct snd_soc_pcm_runtime *rtd = substream->private_data; 897f0fba2adSLiam Girdwood struct snd_soc_platform *platform = rtd->platform; 898f0fba2adSLiam Girdwood struct snd_soc_dai *cpu_dai = rtd->cpu_dai; 899f0fba2adSLiam Girdwood struct snd_soc_dai *codec_dai = rtd->codec_dai; 900258020d0SPeter Ujfalusi struct snd_pcm_runtime *runtime = substream->runtime; 901377b6f62SPeter Ujfalusi snd_pcm_uframes_t offset = 0; 902258020d0SPeter Ujfalusi snd_pcm_sframes_t delay = 0; 903377b6f62SPeter Ujfalusi 904f0fba2adSLiam Girdwood if (platform->driver->ops->pointer) 905f0fba2adSLiam Girdwood offset = platform->driver->ops->pointer(substream); 906377b6f62SPeter Ujfalusi 907f0fba2adSLiam Girdwood if (cpu_dai->driver->ops->delay) 908f0fba2adSLiam Girdwood delay += cpu_dai->driver->ops->delay(substream, cpu_dai); 909258020d0SPeter Ujfalusi 910f0fba2adSLiam Girdwood if (codec_dai->driver->ops->delay) 911f0fba2adSLiam Girdwood delay += codec_dai->driver->ops->delay(substream, codec_dai); 912258020d0SPeter Ujfalusi 913f0fba2adSLiam Girdwood if (platform->driver->delay) 914f0fba2adSLiam Girdwood delay += platform->driver->delay(substream, codec_dai); 915258020d0SPeter Ujfalusi 916258020d0SPeter Ujfalusi runtime->delay = delay; 917258020d0SPeter Ujfalusi 918377b6f62SPeter Ujfalusi return offset; 919377b6f62SPeter Ujfalusi } 920377b6f62SPeter Ujfalusi 921db2a4165SFrank Mandarino /* ASoC PCM operations */ 922db2a4165SFrank Mandarino static struct snd_pcm_ops soc_pcm_ops = { 923db2a4165SFrank Mandarino .open = soc_pcm_open, 924db2a4165SFrank Mandarino .close = soc_codec_close, 925db2a4165SFrank Mandarino .hw_params = soc_pcm_hw_params, 926db2a4165SFrank Mandarino .hw_free = soc_pcm_hw_free, 927db2a4165SFrank Mandarino .prepare = soc_pcm_prepare, 928db2a4165SFrank Mandarino .trigger = soc_pcm_trigger, 929377b6f62SPeter Ujfalusi .pointer = soc_pcm_pointer, 930db2a4165SFrank Mandarino }; 931db2a4165SFrank Mandarino 932db2a4165SFrank Mandarino #ifdef CONFIG_PM 933db2a4165SFrank Mandarino /* powers down audio subsystem for suspend */ 934416356fcSMark Brown static int soc_suspend(struct device *dev) 935db2a4165SFrank Mandarino { 936416356fcSMark Brown struct platform_device *pdev = to_platform_device(dev); 937f0fba2adSLiam Girdwood struct snd_soc_card *card = platform_get_drvdata(pdev); 938db2a4165SFrank Mandarino int i; 939db2a4165SFrank Mandarino 940e3509ff0SDaniel Mack /* If the initialization of this soc device failed, there is no codec 941e3509ff0SDaniel Mack * associated with it. Just bail out in this case. 942e3509ff0SDaniel Mack */ 943f0fba2adSLiam Girdwood if (list_empty(&card->codec_dev_list)) 944e3509ff0SDaniel Mack return 0; 945e3509ff0SDaniel Mack 9466ed25978SAndy Green /* Due to the resume being scheduled into a workqueue we could 9476ed25978SAndy Green * suspend before that's finished - wait for it to complete. 9486ed25978SAndy Green */ 949f0fba2adSLiam Girdwood snd_power_lock(card->snd_card); 950f0fba2adSLiam Girdwood snd_power_wait(card->snd_card, SNDRV_CTL_POWER_D0); 951f0fba2adSLiam Girdwood snd_power_unlock(card->snd_card); 9526ed25978SAndy Green 9536ed25978SAndy Green /* we're going to block userspace touching us until resume completes */ 954f0fba2adSLiam Girdwood snd_power_change_state(card->snd_card, SNDRV_CTL_POWER_D3hot); 9556ed25978SAndy Green 956db2a4165SFrank Mandarino /* mute any active DAC's */ 957f0fba2adSLiam Girdwood for (i = 0; i < card->num_rtd; i++) { 958f0fba2adSLiam Girdwood struct snd_soc_dai *dai = card->rtd[i].codec_dai; 959f0fba2adSLiam Girdwood struct snd_soc_dai_driver *drv = dai->driver; 9603efab7dcSMark Brown 961f0fba2adSLiam Girdwood if (card->rtd[i].dai_link->ignore_suspend) 9623efab7dcSMark Brown continue; 9633efab7dcSMark Brown 964f0fba2adSLiam Girdwood if (drv->ops->digital_mute && dai->playback_active) 965f0fba2adSLiam Girdwood drv->ops->digital_mute(dai, 1); 966db2a4165SFrank Mandarino } 967db2a4165SFrank Mandarino 9684ccab3e7SLiam Girdwood /* suspend all pcms */ 969f0fba2adSLiam Girdwood for (i = 0; i < card->num_rtd; i++) { 970f0fba2adSLiam Girdwood if (card->rtd[i].dai_link->ignore_suspend) 9713efab7dcSMark Brown continue; 9723efab7dcSMark Brown 973f0fba2adSLiam Girdwood snd_pcm_suspend_all(card->rtd[i].pcm); 9743efab7dcSMark Brown } 9754ccab3e7SLiam Girdwood 97687506549SMark Brown if (card->suspend_pre) 977416356fcSMark Brown card->suspend_pre(pdev, PMSG_SUSPEND); 978db2a4165SFrank Mandarino 979f0fba2adSLiam Girdwood for (i = 0; i < card->num_rtd; i++) { 980f0fba2adSLiam Girdwood struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai; 981f0fba2adSLiam Girdwood struct snd_soc_platform *platform = card->rtd[i].platform; 9823efab7dcSMark Brown 983f0fba2adSLiam Girdwood if (card->rtd[i].dai_link->ignore_suspend) 9843efab7dcSMark Brown continue; 9853efab7dcSMark Brown 986f0fba2adSLiam Girdwood if (cpu_dai->driver->suspend && !cpu_dai->driver->ac97_control) 987f0fba2adSLiam Girdwood cpu_dai->driver->suspend(cpu_dai); 988f0fba2adSLiam Girdwood if (platform->driver->suspend && !platform->suspended) { 989f0fba2adSLiam Girdwood platform->driver->suspend(cpu_dai); 990f0fba2adSLiam Girdwood platform->suspended = 1; 991f0fba2adSLiam Girdwood } 992db2a4165SFrank Mandarino } 993db2a4165SFrank Mandarino 994db2a4165SFrank Mandarino /* close any waiting streams and save state */ 995f0fba2adSLiam Girdwood for (i = 0; i < card->num_rtd; i++) { 996f0fba2adSLiam Girdwood run_delayed_work(&card->rtd[i].delayed_work); 997f0fba2adSLiam Girdwood card->rtd[i].codec->suspend_bias_level = card->rtd[i].codec->bias_level; 998f0fba2adSLiam Girdwood } 999db2a4165SFrank Mandarino 1000f0fba2adSLiam Girdwood for (i = 0; i < card->num_rtd; i++) { 1001f0fba2adSLiam Girdwood struct snd_soc_dai_driver *driver = card->rtd[i].codec_dai->driver; 10023efab7dcSMark Brown 1003f0fba2adSLiam Girdwood if (card->rtd[i].dai_link->ignore_suspend) 10043efab7dcSMark Brown continue; 10053efab7dcSMark Brown 1006f0fba2adSLiam Girdwood if (driver->playback.stream_name != NULL) 1007f0fba2adSLiam Girdwood snd_soc_dapm_stream_event(&card->rtd[i], driver->playback.stream_name, 1008db2a4165SFrank Mandarino SND_SOC_DAPM_STREAM_SUSPEND); 1009f0fba2adSLiam Girdwood 1010f0fba2adSLiam Girdwood if (driver->capture.stream_name != NULL) 1011f0fba2adSLiam Girdwood snd_soc_dapm_stream_event(&card->rtd[i], driver->capture.stream_name, 1012db2a4165SFrank Mandarino SND_SOC_DAPM_STREAM_SUSPEND); 1013db2a4165SFrank Mandarino } 1014db2a4165SFrank Mandarino 1015f0fba2adSLiam Girdwood /* suspend all CODECs */ 1016f0fba2adSLiam Girdwood for (i = 0; i < card->num_rtd; i++) { 1017f0fba2adSLiam Girdwood struct snd_soc_codec *codec = card->rtd[i].codec; 10181547aba9SMark Brown /* If there are paths active then the CODEC will be held with 10191547aba9SMark Brown * bias _ON and should not be suspended. */ 1020f0fba2adSLiam Girdwood if (!codec->suspended && codec->driver->suspend) { 10211547aba9SMark Brown switch (codec->bias_level) { 10221547aba9SMark Brown case SND_SOC_BIAS_STANDBY: 10231547aba9SMark Brown case SND_SOC_BIAS_OFF: 1024f0fba2adSLiam Girdwood codec->driver->suspend(codec, PMSG_SUSPEND); 1025f0fba2adSLiam Girdwood codec->suspended = 1; 10261547aba9SMark Brown break; 10271547aba9SMark Brown default: 1028f0fba2adSLiam Girdwood dev_dbg(codec->dev, "CODEC is on over suspend\n"); 10291547aba9SMark Brown break; 10301547aba9SMark Brown } 10311547aba9SMark Brown } 1032f0fba2adSLiam Girdwood } 1033db2a4165SFrank Mandarino 1034f0fba2adSLiam Girdwood for (i = 0; i < card->num_rtd; i++) { 1035f0fba2adSLiam Girdwood struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai; 10363efab7dcSMark Brown 1037f0fba2adSLiam Girdwood if (card->rtd[i].dai_link->ignore_suspend) 10383efab7dcSMark Brown continue; 10393efab7dcSMark Brown 1040f0fba2adSLiam Girdwood if (cpu_dai->driver->suspend && cpu_dai->driver->ac97_control) 1041f0fba2adSLiam Girdwood cpu_dai->driver->suspend(cpu_dai); 1042db2a4165SFrank Mandarino } 1043db2a4165SFrank Mandarino 104487506549SMark Brown if (card->suspend_post) 1045416356fcSMark Brown card->suspend_post(pdev, PMSG_SUSPEND); 1046db2a4165SFrank Mandarino 1047db2a4165SFrank Mandarino return 0; 1048db2a4165SFrank Mandarino } 1049db2a4165SFrank Mandarino 10506ed25978SAndy Green /* deferred resume work, so resume can complete before we finished 10516ed25978SAndy Green * setting our codec back up, which can be very slow on I2C 10526ed25978SAndy Green */ 10536ed25978SAndy Green static void soc_resume_deferred(struct work_struct *work) 1054db2a4165SFrank Mandarino { 1055f0fba2adSLiam Girdwood struct snd_soc_card *card = 1056f0fba2adSLiam Girdwood container_of(work, struct snd_soc_card, deferred_resume_work); 1057f0fba2adSLiam Girdwood struct platform_device *pdev = to_platform_device(card->dev); 1058db2a4165SFrank Mandarino int i; 1059db2a4165SFrank Mandarino 10606ed25978SAndy Green /* our power state is still SNDRV_CTL_POWER_D3hot from suspend time, 10616ed25978SAndy Green * so userspace apps are blocked from touching us 10626ed25978SAndy Green */ 10636ed25978SAndy Green 1064f0fba2adSLiam Girdwood dev_dbg(card->dev, "starting resume work\n"); 10656ed25978SAndy Green 10669949788bSMark Brown /* Bring us up into D2 so that DAPM starts enabling things */ 1067f0fba2adSLiam Girdwood snd_power_change_state(card->snd_card, SNDRV_CTL_POWER_D2); 10689949788bSMark Brown 106987506549SMark Brown if (card->resume_pre) 107087506549SMark Brown card->resume_pre(pdev); 1071db2a4165SFrank Mandarino 1072f0fba2adSLiam Girdwood /* resume AC97 DAIs */ 1073f0fba2adSLiam Girdwood for (i = 0; i < card->num_rtd; i++) { 1074f0fba2adSLiam Girdwood struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai; 10753efab7dcSMark Brown 1076f0fba2adSLiam Girdwood if (card->rtd[i].dai_link->ignore_suspend) 10773efab7dcSMark Brown continue; 10783efab7dcSMark Brown 1079f0fba2adSLiam Girdwood if (cpu_dai->driver->resume && cpu_dai->driver->ac97_control) 1080f0fba2adSLiam Girdwood cpu_dai->driver->resume(cpu_dai); 1081db2a4165SFrank Mandarino } 1082db2a4165SFrank Mandarino 1083f0fba2adSLiam Girdwood for (i = 0; i < card->num_rtd; i++) { 1084f0fba2adSLiam Girdwood struct snd_soc_codec *codec = card->rtd[i].codec; 10851547aba9SMark Brown /* If the CODEC was idle over suspend then it will have been 10861547aba9SMark Brown * left with bias OFF or STANDBY and suspended so we must now 10871547aba9SMark Brown * resume. Otherwise the suspend was suppressed. 10881547aba9SMark Brown */ 1089f0fba2adSLiam Girdwood if (codec->driver->resume && codec->suspended) { 10901547aba9SMark Brown switch (codec->bias_level) { 10911547aba9SMark Brown case SND_SOC_BIAS_STANDBY: 10921547aba9SMark Brown case SND_SOC_BIAS_OFF: 1093f0fba2adSLiam Girdwood codec->driver->resume(codec); 1094f0fba2adSLiam Girdwood codec->suspended = 0; 10951547aba9SMark Brown break; 10961547aba9SMark Brown default: 1097f0fba2adSLiam Girdwood dev_dbg(codec->dev, "CODEC was on over suspend\n"); 10981547aba9SMark Brown break; 10991547aba9SMark Brown } 11001547aba9SMark Brown } 1101f0fba2adSLiam Girdwood } 1102db2a4165SFrank Mandarino 1103f0fba2adSLiam Girdwood for (i = 0; i < card->num_rtd; i++) { 1104f0fba2adSLiam Girdwood struct snd_soc_dai_driver *driver = card->rtd[i].codec_dai->driver; 11053efab7dcSMark Brown 1106f0fba2adSLiam Girdwood if (card->rtd[i].dai_link->ignore_suspend) 11073efab7dcSMark Brown continue; 11083efab7dcSMark Brown 1109f0fba2adSLiam Girdwood if (driver->playback.stream_name != NULL) 1110f0fba2adSLiam Girdwood snd_soc_dapm_stream_event(&card->rtd[i], driver->playback.stream_name, 1111db2a4165SFrank Mandarino SND_SOC_DAPM_STREAM_RESUME); 1112f0fba2adSLiam Girdwood 1113f0fba2adSLiam Girdwood if (driver->capture.stream_name != NULL) 1114f0fba2adSLiam Girdwood snd_soc_dapm_stream_event(&card->rtd[i], driver->capture.stream_name, 1115db2a4165SFrank Mandarino SND_SOC_DAPM_STREAM_RESUME); 1116db2a4165SFrank Mandarino } 1117db2a4165SFrank Mandarino 11183ff3f64bSMark Brown /* unmute any active DACs */ 1119f0fba2adSLiam Girdwood for (i = 0; i < card->num_rtd; i++) { 1120f0fba2adSLiam Girdwood struct snd_soc_dai *dai = card->rtd[i].codec_dai; 1121f0fba2adSLiam Girdwood struct snd_soc_dai_driver *drv = dai->driver; 11223efab7dcSMark Brown 1123f0fba2adSLiam Girdwood if (card->rtd[i].dai_link->ignore_suspend) 11243efab7dcSMark Brown continue; 11253efab7dcSMark Brown 1126f0fba2adSLiam Girdwood if (drv->ops->digital_mute && dai->playback_active) 1127f0fba2adSLiam Girdwood drv->ops->digital_mute(dai, 0); 1128db2a4165SFrank Mandarino } 1129db2a4165SFrank Mandarino 1130f0fba2adSLiam Girdwood for (i = 0; i < card->num_rtd; i++) { 1131f0fba2adSLiam Girdwood struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai; 1132f0fba2adSLiam Girdwood struct snd_soc_platform *platform = card->rtd[i].platform; 11333efab7dcSMark Brown 1134f0fba2adSLiam Girdwood if (card->rtd[i].dai_link->ignore_suspend) 11353efab7dcSMark Brown continue; 11363efab7dcSMark Brown 1137f0fba2adSLiam Girdwood if (cpu_dai->driver->resume && !cpu_dai->driver->ac97_control) 1138f0fba2adSLiam Girdwood cpu_dai->driver->resume(cpu_dai); 1139f0fba2adSLiam Girdwood if (platform->driver->resume && platform->suspended) { 1140f0fba2adSLiam Girdwood platform->driver->resume(cpu_dai); 1141f0fba2adSLiam Girdwood platform->suspended = 0; 1142f0fba2adSLiam Girdwood } 1143db2a4165SFrank Mandarino } 1144db2a4165SFrank Mandarino 114587506549SMark Brown if (card->resume_post) 114687506549SMark Brown card->resume_post(pdev); 1147db2a4165SFrank Mandarino 1148f0fba2adSLiam Girdwood dev_dbg(card->dev, "resume work completed\n"); 11496ed25978SAndy Green 11506ed25978SAndy Green /* userspace can access us now we are back as we were before */ 1151f0fba2adSLiam Girdwood snd_power_change_state(card->snd_card, SNDRV_CTL_POWER_D0); 11526ed25978SAndy Green } 11536ed25978SAndy Green 11546ed25978SAndy Green /* powers up audio subsystem after a suspend */ 1155416356fcSMark Brown static int soc_resume(struct device *dev) 11566ed25978SAndy Green { 1157416356fcSMark Brown struct platform_device *pdev = to_platform_device(dev); 1158f0fba2adSLiam Girdwood struct snd_soc_card *card = platform_get_drvdata(pdev); 1159f0fba2adSLiam Girdwood int i; 1160b9dd94a8SPeter Ujfalusi 116164ab9baaSMark Brown /* AC97 devices might have other drivers hanging off them so 116264ab9baaSMark Brown * need to resume immediately. Other drivers don't have that 116364ab9baaSMark Brown * problem and may take a substantial amount of time to resume 116464ab9baaSMark Brown * due to I/O costs and anti-pop so handle them out of line. 116564ab9baaSMark Brown */ 1166f0fba2adSLiam Girdwood for (i = 0; i < card->num_rtd; i++) { 1167f0fba2adSLiam Girdwood struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai; 1168f0fba2adSLiam Girdwood if (cpu_dai->driver->ac97_control) { 1169f0fba2adSLiam Girdwood dev_dbg(dev, "Resuming AC97 immediately\n"); 117064ab9baaSMark Brown soc_resume_deferred(&card->deferred_resume_work); 117164ab9baaSMark Brown } else { 1172f0fba2adSLiam Girdwood dev_dbg(dev, "Scheduling resume work\n"); 11736308419aSMark Brown if (!schedule_work(&card->deferred_resume_work)) 1174f0fba2adSLiam Girdwood dev_err(dev, "resume work item may be lost\n"); 1175f0fba2adSLiam Girdwood } 117664ab9baaSMark Brown } 11776ed25978SAndy Green 1178db2a4165SFrank Mandarino return 0; 1179db2a4165SFrank Mandarino } 1180db2a4165SFrank Mandarino #else 1181db2a4165SFrank Mandarino #define soc_suspend NULL 1182db2a4165SFrank Mandarino #define soc_resume NULL 1183db2a4165SFrank Mandarino #endif 1184db2a4165SFrank Mandarino 118502a06d30SBarry Song static struct snd_soc_dai_ops null_dai_ops = { 118602a06d30SBarry Song }; 118702a06d30SBarry Song 1188f0fba2adSLiam Girdwood static int soc_bind_dai_link(struct snd_soc_card *card, int num) 1189db2a4165SFrank Mandarino { 1190f0fba2adSLiam Girdwood struct snd_soc_dai_link *dai_link = &card->dai_link[num]; 1191f0fba2adSLiam Girdwood struct snd_soc_pcm_runtime *rtd = &card->rtd[num]; 1192fe3e78e0SMark Brown struct snd_soc_codec *codec; 1193435c5e25SMark Brown struct snd_soc_platform *platform; 1194f0fba2adSLiam Girdwood struct snd_soc_dai *codec_dai, *cpu_dai; 1195db2a4165SFrank Mandarino 1196f0fba2adSLiam Girdwood if (rtd->complete) 1197f0fba2adSLiam Girdwood return 1; 1198f0fba2adSLiam Girdwood dev_dbg(card->dev, "binding %s at idx %d\n", dai_link->name, num); 11996308419aSMark Brown 1200f0fba2adSLiam Girdwood /* do we already have the CPU DAI for this link ? */ 1201f0fba2adSLiam Girdwood if (rtd->cpu_dai) { 1202f0fba2adSLiam Girdwood goto find_codec; 1203435c5e25SMark Brown } 1204f0fba2adSLiam Girdwood /* no, then find CPU DAI from registered DAIs*/ 1205f0fba2adSLiam Girdwood list_for_each_entry(cpu_dai, &dai_list, list) { 1206f0fba2adSLiam Girdwood if (!strcmp(cpu_dai->name, dai_link->cpu_dai_name)) { 1207f0fba2adSLiam Girdwood 1208f0fba2adSLiam Girdwood if (!try_module_get(cpu_dai->dev->driver->owner)) 1209f0fba2adSLiam Girdwood return -ENODEV; 1210f0fba2adSLiam Girdwood 1211f0fba2adSLiam Girdwood rtd->cpu_dai = cpu_dai; 1212f0fba2adSLiam Girdwood goto find_codec; 1213f0fba2adSLiam Girdwood } 1214f0fba2adSLiam Girdwood } 1215f0fba2adSLiam Girdwood dev_dbg(card->dev, "CPU DAI %s not registered\n", 1216f0fba2adSLiam Girdwood dai_link->cpu_dai_name); 1217f0fba2adSLiam Girdwood 1218f0fba2adSLiam Girdwood find_codec: 1219f0fba2adSLiam Girdwood /* do we already have the CODEC for this link ? */ 1220f0fba2adSLiam Girdwood if (rtd->codec) { 1221f0fba2adSLiam Girdwood goto find_platform; 1222c5af3a2eSMark Brown } 1223c5af3a2eSMark Brown 1224f0fba2adSLiam Girdwood /* no, then find CODEC from registered CODECs*/ 1225f0fba2adSLiam Girdwood list_for_each_entry(codec, &codec_list, list) { 1226f0fba2adSLiam Girdwood if (!strcmp(codec->name, dai_link->codec_name)) { 1227f0fba2adSLiam Girdwood rtd->codec = codec; 1228f0fba2adSLiam Girdwood 1229f0fba2adSLiam Girdwood if (!try_module_get(codec->dev->driver->owner)) 1230f0fba2adSLiam Girdwood return -ENODEV; 1231f0fba2adSLiam Girdwood 1232f0fba2adSLiam Girdwood /* CODEC found, so find CODEC DAI from registered DAIs from this CODEC*/ 1233f0fba2adSLiam Girdwood list_for_each_entry(codec_dai, &dai_list, list) { 1234f0fba2adSLiam Girdwood if (codec->dev == codec_dai->dev && 1235f0fba2adSLiam Girdwood !strcmp(codec_dai->name, dai_link->codec_dai_name)) { 1236f0fba2adSLiam Girdwood rtd->codec_dai = codec_dai; 1237f0fba2adSLiam Girdwood goto find_platform; 1238435c5e25SMark Brown } 1239f0fba2adSLiam Girdwood } 1240f0fba2adSLiam Girdwood dev_dbg(card->dev, "CODEC DAI %s not registered\n", 1241f0fba2adSLiam Girdwood dai_link->codec_dai_name); 1242f0fba2adSLiam Girdwood 1243f0fba2adSLiam Girdwood goto find_platform; 1244f0fba2adSLiam Girdwood } 1245f0fba2adSLiam Girdwood } 1246f0fba2adSLiam Girdwood dev_dbg(card->dev, "CODEC %s not registered\n", 1247f0fba2adSLiam Girdwood dai_link->codec_name); 1248f0fba2adSLiam Girdwood 1249f0fba2adSLiam Girdwood find_platform: 1250f0fba2adSLiam Girdwood /* do we already have the CODEC DAI for this link ? */ 1251f0fba2adSLiam Girdwood if (rtd->platform) { 1252f0fba2adSLiam Girdwood goto out; 1253f0fba2adSLiam Girdwood } 1254f0fba2adSLiam Girdwood /* no, then find CPU DAI from registered DAIs*/ 1255f0fba2adSLiam Girdwood list_for_each_entry(platform, &platform_list, list) { 1256f0fba2adSLiam Girdwood if (!strcmp(platform->name, dai_link->platform_name)) { 1257f0fba2adSLiam Girdwood 1258f0fba2adSLiam Girdwood if (!try_module_get(platform->dev->driver->owner)) 1259f0fba2adSLiam Girdwood return -ENODEV; 1260f0fba2adSLiam Girdwood 1261f0fba2adSLiam Girdwood rtd->platform = platform; 1262f0fba2adSLiam Girdwood goto out; 1263f0fba2adSLiam Girdwood } 1264435c5e25SMark Brown } 12656b05eda6SMark Brown 1266f0fba2adSLiam Girdwood dev_dbg(card->dev, "platform %s not registered\n", 1267f0fba2adSLiam Girdwood dai_link->platform_name); 1268f0fba2adSLiam Girdwood return 0; 1269f0fba2adSLiam Girdwood 1270f0fba2adSLiam Girdwood out: 1271f0fba2adSLiam Girdwood /* mark rtd as complete if we found all 4 of our client devices */ 1272f0fba2adSLiam Girdwood if (rtd->codec && rtd->codec_dai && rtd->platform && rtd->cpu_dai) { 1273f0fba2adSLiam Girdwood rtd->complete = 1; 1274f0fba2adSLiam Girdwood card->num_rtd++; 1275f0fba2adSLiam Girdwood } 1276f0fba2adSLiam Girdwood return 1; 12776b05eda6SMark Brown } 12786b05eda6SMark Brown 1279f0fba2adSLiam Girdwood static void soc_remove_dai_link(struct snd_soc_card *card, int num) 1280f0fba2adSLiam Girdwood { 1281f0fba2adSLiam Girdwood struct snd_soc_pcm_runtime *rtd = &card->rtd[num]; 1282f0fba2adSLiam Girdwood struct snd_soc_codec *codec = rtd->codec; 1283f0fba2adSLiam Girdwood struct snd_soc_platform *platform = rtd->platform; 1284f0fba2adSLiam Girdwood struct snd_soc_dai *codec_dai = rtd->codec_dai, *cpu_dai = rtd->cpu_dai; 1285f0fba2adSLiam Girdwood int err; 1286f0fba2adSLiam Girdwood 1287f0fba2adSLiam Girdwood /* unregister the rtd device */ 1288f0fba2adSLiam Girdwood if (rtd->dev_registered) { 1289f0fba2adSLiam Girdwood device_remove_file(&rtd->dev, &dev_attr_pmdown_time); 1290f0fba2adSLiam Girdwood device_unregister(&rtd->dev); 1291f0fba2adSLiam Girdwood rtd->dev_registered = 0; 129202a06d30SBarry Song } 129302a06d30SBarry Song 1294f0fba2adSLiam Girdwood /* remove the CODEC DAI */ 1295f0fba2adSLiam Girdwood if (codec_dai && codec_dai->probed) { 1296f0fba2adSLiam Girdwood if (codec_dai->driver->remove) { 1297f0fba2adSLiam Girdwood err = codec_dai->driver->remove(codec_dai); 1298f0fba2adSLiam Girdwood if (err < 0) 1299f0fba2adSLiam Girdwood printk(KERN_ERR "asoc: failed to remove %s\n", codec_dai->name); 1300f0fba2adSLiam Girdwood } 1301f0fba2adSLiam Girdwood codec_dai->probed = 0; 1302f0fba2adSLiam Girdwood list_del(&codec_dai->card_list); 1303f0fba2adSLiam Girdwood } 1304f0fba2adSLiam Girdwood 1305f0fba2adSLiam Girdwood /* remove the platform */ 1306f0fba2adSLiam Girdwood if (platform && platform->probed) { 1307f0fba2adSLiam Girdwood if (platform->driver->remove) { 1308f0fba2adSLiam Girdwood err = platform->driver->remove(platform); 1309f0fba2adSLiam Girdwood if (err < 0) 1310f0fba2adSLiam Girdwood printk(KERN_ERR "asoc: failed to remove %s\n", platform->name); 1311f0fba2adSLiam Girdwood } 1312f0fba2adSLiam Girdwood platform->probed = 0; 1313f0fba2adSLiam Girdwood list_del(&platform->card_list); 1314f0fba2adSLiam Girdwood module_put(platform->dev->driver->owner); 1315f0fba2adSLiam Girdwood } 1316f0fba2adSLiam Girdwood 1317f0fba2adSLiam Girdwood /* remove the CODEC */ 1318f0fba2adSLiam Girdwood if (codec && codec->probed) { 1319f0fba2adSLiam Girdwood if (codec->driver->remove) { 1320f0fba2adSLiam Girdwood err = codec->driver->remove(codec); 1321f0fba2adSLiam Girdwood if (err < 0) 1322f0fba2adSLiam Girdwood printk(KERN_ERR "asoc: failed to remove %s\n", codec->name); 1323f0fba2adSLiam Girdwood } 1324f0fba2adSLiam Girdwood 1325f0fba2adSLiam Girdwood /* Make sure all DAPM widgets are freed */ 1326f0fba2adSLiam Girdwood snd_soc_dapm_free(codec); 1327f0fba2adSLiam Girdwood 1328f0fba2adSLiam Girdwood soc_cleanup_codec_debugfs(codec); 1329f0fba2adSLiam Girdwood device_remove_file(&rtd->dev, &dev_attr_codec_reg); 1330f0fba2adSLiam Girdwood codec->probed = 0; 1331f0fba2adSLiam Girdwood list_del(&codec->card_list); 1332f0fba2adSLiam Girdwood module_put(codec->dev->driver->owner); 1333f0fba2adSLiam Girdwood } 1334f0fba2adSLiam Girdwood 1335f0fba2adSLiam Girdwood /* remove the cpu_dai */ 1336f0fba2adSLiam Girdwood if (cpu_dai && cpu_dai->probed) { 1337f0fba2adSLiam Girdwood if (cpu_dai->driver->remove) { 1338f0fba2adSLiam Girdwood err = cpu_dai->driver->remove(cpu_dai); 1339f0fba2adSLiam Girdwood if (err < 0) 1340f0fba2adSLiam Girdwood printk(KERN_ERR "asoc: failed to remove %s\n", cpu_dai->name); 1341f0fba2adSLiam Girdwood } 1342f0fba2adSLiam Girdwood cpu_dai->probed = 0; 1343f0fba2adSLiam Girdwood list_del(&cpu_dai->card_list); 1344f0fba2adSLiam Girdwood module_put(cpu_dai->dev->driver->owner); 1345f0fba2adSLiam Girdwood } 1346f0fba2adSLiam Girdwood } 1347f0fba2adSLiam Girdwood 1348f0fba2adSLiam Girdwood static void rtd_release(struct device *dev) {} 1349f0fba2adSLiam Girdwood 1350f0fba2adSLiam Girdwood static int soc_probe_dai_link(struct snd_soc_card *card, int num) 1351f0fba2adSLiam Girdwood { 1352f0fba2adSLiam Girdwood struct snd_soc_dai_link *dai_link = &card->dai_link[num]; 1353f0fba2adSLiam Girdwood struct snd_soc_pcm_runtime *rtd = &card->rtd[num]; 1354f0fba2adSLiam Girdwood struct snd_soc_codec *codec = rtd->codec; 1355f0fba2adSLiam Girdwood struct snd_soc_platform *platform = rtd->platform; 1356f0fba2adSLiam Girdwood struct snd_soc_dai *codec_dai = rtd->codec_dai, *cpu_dai = rtd->cpu_dai; 1357f0fba2adSLiam Girdwood int ret; 1358f0fba2adSLiam Girdwood 1359f0fba2adSLiam Girdwood dev_dbg(card->dev, "probe %s dai link %d\n", card->name, num); 1360f0fba2adSLiam Girdwood 1361f0fba2adSLiam Girdwood /* config components */ 1362f0fba2adSLiam Girdwood codec_dai->codec = codec; 1363f0fba2adSLiam Girdwood codec->card = card; 1364f0fba2adSLiam Girdwood cpu_dai->platform = platform; 1365f0fba2adSLiam Girdwood rtd->card = card; 1366f0fba2adSLiam Girdwood rtd->dev.parent = card->dev; 1367f0fba2adSLiam Girdwood codec_dai->card = card; 1368f0fba2adSLiam Girdwood cpu_dai->card = card; 1369f0fba2adSLiam Girdwood 1370f0fba2adSLiam Girdwood /* set default power off timeout */ 1371f0fba2adSLiam Girdwood rtd->pmdown_time = pmdown_time; 1372f0fba2adSLiam Girdwood 1373f0fba2adSLiam Girdwood /* probe the cpu_dai */ 1374f0fba2adSLiam Girdwood if (!cpu_dai->probed) { 1375f0fba2adSLiam Girdwood if (cpu_dai->driver->probe) { 1376f0fba2adSLiam Girdwood ret = cpu_dai->driver->probe(cpu_dai); 1377f0fba2adSLiam Girdwood if (ret < 0) { 1378f0fba2adSLiam Girdwood printk(KERN_ERR "asoc: failed to probe CPU DAI %s\n", 1379f0fba2adSLiam Girdwood cpu_dai->name); 1380f0fba2adSLiam Girdwood return ret; 1381f0fba2adSLiam Girdwood } 1382f0fba2adSLiam Girdwood } 1383f0fba2adSLiam Girdwood cpu_dai->probed = 1; 1384f0fba2adSLiam Girdwood /* mark cpu_dai as probed and add to card cpu_dai list */ 1385f0fba2adSLiam Girdwood list_add(&cpu_dai->card_list, &card->dai_dev_list); 1386f0fba2adSLiam Girdwood } 1387f0fba2adSLiam Girdwood 1388f0fba2adSLiam Girdwood /* probe the CODEC */ 1389f0fba2adSLiam Girdwood if (!codec->probed) { 1390f0fba2adSLiam Girdwood if (codec->driver->probe) { 1391f0fba2adSLiam Girdwood ret = codec->driver->probe(codec); 1392f0fba2adSLiam Girdwood if (ret < 0) { 1393f0fba2adSLiam Girdwood printk(KERN_ERR "asoc: failed to probe CODEC %s\n", 1394f0fba2adSLiam Girdwood codec->name); 1395f0fba2adSLiam Girdwood return ret; 1396f0fba2adSLiam Girdwood } 1397f0fba2adSLiam Girdwood } 139813cb61f8SMark Brown 139913cb61f8SMark Brown soc_init_codec_debugfs(codec); 140013cb61f8SMark Brown 1401f0fba2adSLiam Girdwood /* mark codec as probed and add to card codec list */ 1402f0fba2adSLiam Girdwood codec->probed = 1; 1403f0fba2adSLiam Girdwood list_add(&codec->card_list, &card->codec_dev_list); 1404f0fba2adSLiam Girdwood } 1405f0fba2adSLiam Girdwood 1406f0fba2adSLiam Girdwood /* probe the platform */ 1407f0fba2adSLiam Girdwood if (!platform->probed) { 1408f0fba2adSLiam Girdwood if (platform->driver->probe) { 1409f0fba2adSLiam Girdwood ret = platform->driver->probe(platform); 1410f0fba2adSLiam Girdwood if (ret < 0) { 1411f0fba2adSLiam Girdwood printk(KERN_ERR "asoc: failed to probe platform %s\n", 1412f0fba2adSLiam Girdwood platform->name); 1413f0fba2adSLiam Girdwood return ret; 1414f0fba2adSLiam Girdwood } 1415f0fba2adSLiam Girdwood } 1416f0fba2adSLiam Girdwood /* mark platform as probed and add to card platform list */ 1417f0fba2adSLiam Girdwood platform->probed = 1; 1418f0fba2adSLiam Girdwood list_add(&platform->card_list, &card->platform_dev_list); 1419f0fba2adSLiam Girdwood } 1420f0fba2adSLiam Girdwood 1421f0fba2adSLiam Girdwood /* probe the CODEC DAI */ 1422f0fba2adSLiam Girdwood if (!codec_dai->probed) { 1423f0fba2adSLiam Girdwood if (codec_dai->driver->probe) { 1424f0fba2adSLiam Girdwood ret = codec_dai->driver->probe(codec_dai); 1425f0fba2adSLiam Girdwood if (ret < 0) { 1426f0fba2adSLiam Girdwood printk(KERN_ERR "asoc: failed to probe CODEC DAI %s\n", 1427f0fba2adSLiam Girdwood codec_dai->name); 1428f0fba2adSLiam Girdwood return ret; 1429f0fba2adSLiam Girdwood } 1430f0fba2adSLiam Girdwood } 1431f0fba2adSLiam Girdwood 1432f0fba2adSLiam Girdwood /* mark cpu_dai as probed and add to card cpu_dai list */ 1433f0fba2adSLiam Girdwood codec_dai->probed = 1; 1434f0fba2adSLiam Girdwood list_add(&codec_dai->card_list, &card->dai_dev_list); 1435f0fba2adSLiam Girdwood } 1436f0fba2adSLiam Girdwood 1437f0fba2adSLiam Girdwood /* DAPM dai link stream work */ 1438f0fba2adSLiam Girdwood INIT_DELAYED_WORK(&rtd->delayed_work, close_delayed_work); 1439f0fba2adSLiam Girdwood 1440f0fba2adSLiam Girdwood /* now that all clients have probed, initialise the DAI link */ 1441f0fba2adSLiam Girdwood if (dai_link->init) { 1442f0fba2adSLiam Girdwood ret = dai_link->init(rtd); 1443f0fba2adSLiam Girdwood if (ret < 0) { 1444f0fba2adSLiam Girdwood printk(KERN_ERR "asoc: failed to init %s\n", dai_link->stream_name); 1445f0fba2adSLiam Girdwood return ret; 1446f0fba2adSLiam Girdwood } 1447f0fba2adSLiam Girdwood } 1448f0fba2adSLiam Girdwood 1449f0fba2adSLiam Girdwood /* Make sure all DAPM widgets are instantiated */ 1450f0fba2adSLiam Girdwood snd_soc_dapm_new_widgets(codec); 1451f0fba2adSLiam Girdwood snd_soc_dapm_sync(codec); 1452f0fba2adSLiam Girdwood 1453f0fba2adSLiam Girdwood /* register the rtd device */ 1454f0fba2adSLiam Girdwood rtd->dev.init_name = rtd->dai_link->stream_name; 1455f0fba2adSLiam Girdwood rtd->dev.release = rtd_release; 1456f0fba2adSLiam Girdwood rtd->dev.init_name = dai_link->name; 1457f0fba2adSLiam Girdwood ret = device_register(&rtd->dev); 1458f0fba2adSLiam Girdwood if (ret < 0) { 1459f0fba2adSLiam Girdwood printk(KERN_ERR "asoc: failed to register DAI runtime device %d\n", ret); 1460f0fba2adSLiam Girdwood return ret; 1461f0fba2adSLiam Girdwood } 1462f0fba2adSLiam Girdwood 1463f0fba2adSLiam Girdwood rtd->dev_registered = 1; 1464f0fba2adSLiam Girdwood ret = device_create_file(&rtd->dev, &dev_attr_pmdown_time); 1465f0fba2adSLiam Girdwood if (ret < 0) 1466f0fba2adSLiam Girdwood printk(KERN_WARNING "asoc: failed to add pmdown_time sysfs\n"); 1467f0fba2adSLiam Girdwood 1468f0fba2adSLiam Girdwood /* add DAPM sysfs entries for this codec */ 1469f0fba2adSLiam Girdwood ret = snd_soc_dapm_sys_add(&rtd->dev); 1470f0fba2adSLiam Girdwood if (ret < 0) 1471f0fba2adSLiam Girdwood printk(KERN_WARNING "asoc: failed to add codec dapm sysfs entries\n"); 1472f0fba2adSLiam Girdwood 1473f0fba2adSLiam Girdwood /* add codec sysfs entries */ 1474f0fba2adSLiam Girdwood ret = device_create_file(&rtd->dev, &dev_attr_codec_reg); 1475f0fba2adSLiam Girdwood if (ret < 0) 1476f0fba2adSLiam Girdwood printk(KERN_WARNING "asoc: failed to add codec sysfs files\n"); 1477f0fba2adSLiam Girdwood 1478f0fba2adSLiam Girdwood /* create the pcm */ 1479f0fba2adSLiam Girdwood ret = soc_new_pcm(rtd, num); 1480f0fba2adSLiam Girdwood if (ret < 0) { 1481f0fba2adSLiam Girdwood printk(KERN_ERR "asoc: can't create pcm %s\n", dai_link->stream_name); 1482f0fba2adSLiam Girdwood return ret; 1483f0fba2adSLiam Girdwood } 1484f0fba2adSLiam Girdwood 1485f0fba2adSLiam Girdwood /* add platform data for AC97 devices */ 1486f0fba2adSLiam Girdwood if (rtd->codec_dai->driver->ac97_control) 1487f0fba2adSLiam Girdwood snd_ac97_dev_add_pdata(codec->ac97, rtd->cpu_dai->ac97_pdata); 1488f0fba2adSLiam Girdwood 1489f0fba2adSLiam Girdwood return 0; 1490f0fba2adSLiam Girdwood } 1491f0fba2adSLiam Girdwood 1492f0fba2adSLiam Girdwood #ifdef CONFIG_SND_SOC_AC97_BUS 1493f0fba2adSLiam Girdwood static int soc_register_ac97_dai_link(struct snd_soc_pcm_runtime *rtd) 1494f0fba2adSLiam Girdwood { 1495f0fba2adSLiam Girdwood int ret; 1496f0fba2adSLiam Girdwood 1497f0fba2adSLiam Girdwood /* Only instantiate AC97 if not already done by the adaptor 1498f0fba2adSLiam Girdwood * for the generic AC97 subsystem. 14996b05eda6SMark Brown */ 1500f0fba2adSLiam Girdwood if (rtd->codec_dai->driver->ac97_control && !rtd->codec->ac97_registered) { 1501f0fba2adSLiam Girdwood 1502f0fba2adSLiam Girdwood ret = soc_ac97_dev_register(rtd->codec); 1503f0fba2adSLiam Girdwood if (ret < 0) { 1504f0fba2adSLiam Girdwood printk(KERN_ERR "asoc: AC97 device register failed\n"); 1505f0fba2adSLiam Girdwood return ret; 1506435c5e25SMark Brown } 1507435c5e25SMark Brown 1508f0fba2adSLiam Girdwood rtd->codec->ac97_registered = 1; 1509f0fba2adSLiam Girdwood } 1510f0fba2adSLiam Girdwood return 0; 1511f0fba2adSLiam Girdwood } 1512435c5e25SMark Brown 1513f0fba2adSLiam Girdwood static void soc_unregister_ac97_dai_link(struct snd_soc_codec *codec) 1514f0fba2adSLiam Girdwood { 1515f0fba2adSLiam Girdwood if (codec->ac97_registered) { 1516f0fba2adSLiam Girdwood soc_ac97_dev_unregister(codec); 1517f0fba2adSLiam Girdwood codec->ac97_registered = 0; 1518f0fba2adSLiam Girdwood } 1519f0fba2adSLiam Girdwood } 1520f0fba2adSLiam Girdwood #endif 1521435c5e25SMark Brown 1522f0fba2adSLiam Girdwood static void snd_soc_instantiate_card(struct snd_soc_card *card) 1523f0fba2adSLiam Girdwood { 1524f0fba2adSLiam Girdwood struct platform_device *pdev = to_platform_device(card->dev); 1525f0fba2adSLiam Girdwood int ret, i; 152696dd3622SMark Brown 1527f0fba2adSLiam Girdwood mutex_lock(&card->mutex); 1528f0fba2adSLiam Girdwood 1529f0fba2adSLiam Girdwood if (card->instantiated) { 1530f0fba2adSLiam Girdwood mutex_unlock(&card->mutex); 1531435c5e25SMark Brown return; 1532db2a4165SFrank Mandarino } 1533db2a4165SFrank Mandarino 1534f0fba2adSLiam Girdwood /* bind DAIs */ 1535f0fba2adSLiam Girdwood for (i = 0; i < card->num_links; i++) 1536f0fba2adSLiam Girdwood soc_bind_dai_link(card, i); 1537f0fba2adSLiam Girdwood 1538f0fba2adSLiam Girdwood /* bind completed ? */ 1539f0fba2adSLiam Girdwood if (card->num_rtd != card->num_links) { 1540f0fba2adSLiam Girdwood mutex_unlock(&card->mutex); 1541f0fba2adSLiam Girdwood return; 1542db2a4165SFrank Mandarino } 1543db2a4165SFrank Mandarino 1544f0fba2adSLiam Girdwood /* card bind complete so register a sound card */ 1545f0fba2adSLiam Girdwood ret = snd_card_create(SNDRV_DEFAULT_IDX1, SNDRV_DEFAULT_STR1, 1546f0fba2adSLiam Girdwood card->owner, 0, &card->snd_card); 1547f0fba2adSLiam Girdwood if (ret < 0) { 1548f0fba2adSLiam Girdwood printk(KERN_ERR "asoc: can't create sound card for card %s\n", 1549f0fba2adSLiam Girdwood card->name); 1550f0fba2adSLiam Girdwood mutex_unlock(&card->mutex); 1551f0fba2adSLiam Girdwood return; 1552db2a4165SFrank Mandarino } 1553f0fba2adSLiam Girdwood card->snd_card->dev = card->dev; 1554db2a4165SFrank Mandarino 15551301a964SRandy Dunlap #ifdef CONFIG_PM 15566ed25978SAndy Green /* deferred resume work */ 15576308419aSMark Brown INIT_WORK(&card->deferred_resume_work, soc_resume_deferred); 15581301a964SRandy Dunlap #endif 15596ed25978SAndy Green 1560f0fba2adSLiam Girdwood /* initialise the sound card only once */ 1561f0fba2adSLiam Girdwood if (card->probe) { 1562f0fba2adSLiam Girdwood ret = card->probe(pdev); 1563f0fba2adSLiam Girdwood if (ret < 0) 1564f0fba2adSLiam Girdwood goto card_probe_error; 1565f0fba2adSLiam Girdwood } 1566f0fba2adSLiam Girdwood 1567fe3e78e0SMark Brown for (i = 0; i < card->num_links; i++) { 1568f0fba2adSLiam Girdwood ret = soc_probe_dai_link(card, i); 1569fe3e78e0SMark Brown if (ret < 0) { 1570f0fba2adSLiam Girdwood printk(KERN_ERR "asoc: failed to instanciate card %s\n", card->name); 1571f0fba2adSLiam Girdwood goto probe_dai_err; 1572fe3e78e0SMark Brown } 1573fe3e78e0SMark Brown } 1574fe3e78e0SMark Brown 1575f0fba2adSLiam Girdwood snprintf(card->snd_card->shortname, sizeof(card->snd_card->shortname), 1576fe3e78e0SMark Brown "%s", card->name); 1577f0fba2adSLiam Girdwood snprintf(card->snd_card->longname, sizeof(card->snd_card->longname), 1578f0fba2adSLiam Girdwood "%s", card->name); 1579fe3e78e0SMark Brown 1580f0fba2adSLiam Girdwood ret = snd_card_register(card->snd_card); 1581fe3e78e0SMark Brown if (ret < 0) { 1582f0fba2adSLiam Girdwood printk(KERN_ERR "asoc: failed to register soundcard for %s\n", card->name); 1583f0fba2adSLiam Girdwood goto probe_dai_err; 1584fe3e78e0SMark Brown } 1585fe3e78e0SMark Brown 1586fe3e78e0SMark Brown #ifdef CONFIG_SND_SOC_AC97_BUS 1587f0fba2adSLiam Girdwood /* register any AC97 codecs */ 1588f0fba2adSLiam Girdwood for (i = 0; i < card->num_rtd; i++) { 1589f0fba2adSLiam Girdwood ret = soc_register_ac97_dai_link(&card->rtd[i]); 1590fe3e78e0SMark Brown if (ret < 0) { 1591f0fba2adSLiam Girdwood printk(KERN_ERR "asoc: failed to register AC97 %s\n", card->name); 1592f0fba2adSLiam Girdwood goto probe_dai_err; 1593fe3e78e0SMark Brown } 1594fe3e78e0SMark Brown } 1595fe3e78e0SMark Brown #endif 1596fe3e78e0SMark Brown 1597435c5e25SMark Brown card->instantiated = 1; 1598f0fba2adSLiam Girdwood mutex_unlock(&card->mutex); 1599435c5e25SMark Brown return; 1600db2a4165SFrank Mandarino 1601f0fba2adSLiam Girdwood probe_dai_err: 1602f0fba2adSLiam Girdwood for (i = 0; i < card->num_links; i++) 1603f0fba2adSLiam Girdwood soc_remove_dai_link(card, i); 1604fe3e78e0SMark Brown 1605f0fba2adSLiam Girdwood card_probe_error: 160687506549SMark Brown if (card->remove) 160787506549SMark Brown card->remove(pdev); 1608f0fba2adSLiam Girdwood 1609f0fba2adSLiam Girdwood snd_card_free(card->snd_card); 1610f0fba2adSLiam Girdwood 1611f0fba2adSLiam Girdwood mutex_unlock(&card->mutex); 1612435c5e25SMark Brown } 1613db2a4165SFrank Mandarino 1614435c5e25SMark Brown /* 1615421f91d2SUwe Kleine-König * Attempt to initialise any uninitialised cards. Must be called with 1616435c5e25SMark Brown * client_mutex. 1617435c5e25SMark Brown */ 1618435c5e25SMark Brown static void snd_soc_instantiate_cards(void) 1619435c5e25SMark Brown { 1620435c5e25SMark Brown struct snd_soc_card *card; 1621435c5e25SMark Brown list_for_each_entry(card, &card_list, list) 1622435c5e25SMark Brown snd_soc_instantiate_card(card); 1623435c5e25SMark Brown } 1624435c5e25SMark Brown 1625435c5e25SMark Brown /* probes a new socdev */ 1626435c5e25SMark Brown static int soc_probe(struct platform_device *pdev) 1627435c5e25SMark Brown { 1628f0fba2adSLiam Girdwood struct snd_soc_card *card = platform_get_drvdata(pdev); 1629435c5e25SMark Brown int ret = 0; 1630435c5e25SMark Brown 1631435c5e25SMark Brown /* Bodge while we unpick instantiation */ 1632435c5e25SMark Brown card->dev = &pdev->dev; 1633f0fba2adSLiam Girdwood INIT_LIST_HEAD(&card->dai_dev_list); 1634f0fba2adSLiam Girdwood INIT_LIST_HEAD(&card->codec_dev_list); 1635f0fba2adSLiam Girdwood INIT_LIST_HEAD(&card->platform_dev_list); 1636f0fba2adSLiam Girdwood 1637435c5e25SMark Brown ret = snd_soc_register_card(card); 1638435c5e25SMark Brown if (ret != 0) { 1639435c5e25SMark Brown dev_err(&pdev->dev, "Failed to register card\n"); 1640db2a4165SFrank Mandarino return ret; 1641db2a4165SFrank Mandarino } 1642db2a4165SFrank Mandarino 1643435c5e25SMark Brown return 0; 1644435c5e25SMark Brown } 1645435c5e25SMark Brown 1646db2a4165SFrank Mandarino /* removes a socdev */ 1647db2a4165SFrank Mandarino static int soc_remove(struct platform_device *pdev) 1648db2a4165SFrank Mandarino { 1649f0fba2adSLiam Girdwood struct snd_soc_card *card = platform_get_drvdata(pdev); 1650db2a4165SFrank Mandarino int i; 1651db2a4165SFrank Mandarino 1652b2dfa62cSGuennadi Liakhovetski if (card->instantiated) { 1653965ac42cSLiam Girdwood 1654f0fba2adSLiam Girdwood /* make sure any delayed work runs */ 1655f0fba2adSLiam Girdwood for (i = 0; i < card->num_rtd; i++) { 1656f0fba2adSLiam Girdwood struct snd_soc_pcm_runtime *rtd = &card->rtd[i]; 1657f0fba2adSLiam Girdwood run_delayed_work(&rtd->delayed_work); 1658db2a4165SFrank Mandarino } 1659db2a4165SFrank Mandarino 1660f0fba2adSLiam Girdwood /* remove and free each DAI */ 1661f0fba2adSLiam Girdwood for (i = 0; i < card->num_rtd; i++) 1662f0fba2adSLiam Girdwood soc_remove_dai_link(card, i); 1663f0fba2adSLiam Girdwood 1664f0fba2adSLiam Girdwood /* remove the card */ 166587506549SMark Brown if (card->remove) 166687506549SMark Brown card->remove(pdev); 1667f0fba2adSLiam Girdwood 1668f0fba2adSLiam Girdwood kfree(card->rtd); 1669f0fba2adSLiam Girdwood snd_card_free(card->snd_card); 1670b2dfa62cSGuennadi Liakhovetski } 1671c5af3a2eSMark Brown snd_soc_unregister_card(card); 1672db2a4165SFrank Mandarino return 0; 1673db2a4165SFrank Mandarino } 1674db2a4165SFrank Mandarino 1675416356fcSMark Brown static int soc_poweroff(struct device *dev) 167651737470SMark Brown { 1677416356fcSMark Brown struct platform_device *pdev = to_platform_device(dev); 1678f0fba2adSLiam Girdwood struct snd_soc_card *card = platform_get_drvdata(pdev); 1679f0fba2adSLiam Girdwood int i; 168051737470SMark Brown 168151737470SMark Brown if (!card->instantiated) 1682416356fcSMark Brown return 0; 168351737470SMark Brown 168451737470SMark Brown /* Flush out pmdown_time work - we actually do want to run it 168551737470SMark Brown * now, we're shutting down so no imminent restart. */ 1686f0fba2adSLiam Girdwood for (i = 0; i < card->num_rtd; i++) { 1687f0fba2adSLiam Girdwood struct snd_soc_pcm_runtime *rtd = &card->rtd[i]; 1688f0fba2adSLiam Girdwood run_delayed_work(&rtd->delayed_work); 1689f0fba2adSLiam Girdwood } 169051737470SMark Brown 1691f0fba2adSLiam Girdwood snd_soc_dapm_shutdown(card); 1692416356fcSMark Brown 1693416356fcSMark Brown return 0; 169451737470SMark Brown } 169551737470SMark Brown 169647145210SAlexey Dobriyan static const struct dev_pm_ops soc_pm_ops = { 1697416356fcSMark Brown .suspend = soc_suspend, 1698416356fcSMark Brown .resume = soc_resume, 1699416356fcSMark Brown .poweroff = soc_poweroff, 1700416356fcSMark Brown }; 1701416356fcSMark Brown 1702db2a4165SFrank Mandarino /* ASoC platform driver */ 1703db2a4165SFrank Mandarino static struct platform_driver soc_driver = { 1704db2a4165SFrank Mandarino .driver = { 1705db2a4165SFrank Mandarino .name = "soc-audio", 17068b45a209SKay Sievers .owner = THIS_MODULE, 1707416356fcSMark Brown .pm = &soc_pm_ops, 1708db2a4165SFrank Mandarino }, 1709db2a4165SFrank Mandarino .probe = soc_probe, 1710db2a4165SFrank Mandarino .remove = soc_remove, 1711db2a4165SFrank Mandarino }; 1712db2a4165SFrank Mandarino 1713db2a4165SFrank Mandarino /* create a new pcm */ 1714f0fba2adSLiam Girdwood static int soc_new_pcm(struct snd_soc_pcm_runtime *rtd, int num) 1715db2a4165SFrank Mandarino { 1716f0fba2adSLiam Girdwood struct snd_soc_codec *codec = rtd->codec; 1717f0fba2adSLiam Girdwood struct snd_soc_platform *platform = rtd->platform; 1718f0fba2adSLiam Girdwood struct snd_soc_dai *codec_dai = rtd->codec_dai; 1719f0fba2adSLiam Girdwood struct snd_soc_dai *cpu_dai = rtd->cpu_dai; 1720db2a4165SFrank Mandarino struct snd_pcm *pcm; 1721db2a4165SFrank Mandarino char new_name[64]; 1722db2a4165SFrank Mandarino int ret = 0, playback = 0, capture = 0; 1723db2a4165SFrank Mandarino 1724db2a4165SFrank Mandarino /* check client and interface hw capabilities */ 172540ca1142SMark Brown snprintf(new_name, sizeof(new_name), "%s %s-%d", 1726f0fba2adSLiam Girdwood rtd->dai_link->stream_name, codec_dai->name, num); 1727db2a4165SFrank Mandarino 1728f0fba2adSLiam Girdwood if (codec_dai->driver->playback.channels_min) 1729db2a4165SFrank Mandarino playback = 1; 1730f0fba2adSLiam Girdwood if (codec_dai->driver->capture.channels_min) 1731db2a4165SFrank Mandarino capture = 1; 1732db2a4165SFrank Mandarino 1733f0fba2adSLiam Girdwood dev_dbg(rtd->card->dev, "registered pcm #%d %s\n",num,new_name); 1734f0fba2adSLiam Girdwood ret = snd_pcm_new(rtd->card->snd_card, new_name, 1735f0fba2adSLiam Girdwood num, playback, capture, &pcm); 1736db2a4165SFrank Mandarino if (ret < 0) { 1737f0fba2adSLiam Girdwood printk(KERN_ERR "asoc: can't create pcm for codec %s\n", codec->name); 1738db2a4165SFrank Mandarino return ret; 1739db2a4165SFrank Mandarino } 1740db2a4165SFrank Mandarino 1741f0fba2adSLiam Girdwood rtd->pcm = pcm; 1742db2a4165SFrank Mandarino pcm->private_data = rtd; 1743f0fba2adSLiam Girdwood soc_pcm_ops.mmap = platform->driver->ops->mmap; 1744f0fba2adSLiam Girdwood soc_pcm_ops.pointer = platform->driver->ops->pointer; 1745f0fba2adSLiam Girdwood soc_pcm_ops.ioctl = platform->driver->ops->ioctl; 1746f0fba2adSLiam Girdwood soc_pcm_ops.copy = platform->driver->ops->copy; 1747f0fba2adSLiam Girdwood soc_pcm_ops.silence = platform->driver->ops->silence; 1748f0fba2adSLiam Girdwood soc_pcm_ops.ack = platform->driver->ops->ack; 1749f0fba2adSLiam Girdwood soc_pcm_ops.page = platform->driver->ops->page; 1750db2a4165SFrank Mandarino 1751db2a4165SFrank Mandarino if (playback) 1752db2a4165SFrank Mandarino snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &soc_pcm_ops); 1753db2a4165SFrank Mandarino 1754db2a4165SFrank Mandarino if (capture) 1755db2a4165SFrank Mandarino snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &soc_pcm_ops); 1756db2a4165SFrank Mandarino 1757f0fba2adSLiam Girdwood ret = platform->driver->pcm_new(rtd->card->snd_card, codec_dai, pcm); 1758db2a4165SFrank Mandarino if (ret < 0) { 1759db2a4165SFrank Mandarino printk(KERN_ERR "asoc: platform pcm constructor failed\n"); 1760db2a4165SFrank Mandarino return ret; 1761db2a4165SFrank Mandarino } 1762db2a4165SFrank Mandarino 1763f0fba2adSLiam Girdwood pcm->private_free = platform->driver->pcm_free; 1764db2a4165SFrank Mandarino printk(KERN_INFO "asoc: %s <-> %s mapping ok\n", codec_dai->name, 1765db2a4165SFrank Mandarino cpu_dai->name); 1766db2a4165SFrank Mandarino return ret; 1767db2a4165SFrank Mandarino } 1768db2a4165SFrank Mandarino 1769096e49d5SMark Brown /** 1770096e49d5SMark Brown * snd_soc_codec_volatile_register: Report if a register is volatile. 1771096e49d5SMark Brown * 1772096e49d5SMark Brown * @codec: CODEC to query. 1773096e49d5SMark Brown * @reg: Register to query. 1774096e49d5SMark Brown * 1775096e49d5SMark Brown * Boolean function indiciating if a CODEC register is volatile. 1776096e49d5SMark Brown */ 1777096e49d5SMark Brown int snd_soc_codec_volatile_register(struct snd_soc_codec *codec, int reg) 1778096e49d5SMark Brown { 1779f0fba2adSLiam Girdwood if (codec->driver->volatile_register) 1780f0fba2adSLiam Girdwood return codec->driver->volatile_register(reg); 1781096e49d5SMark Brown else 1782096e49d5SMark Brown return 0; 1783096e49d5SMark Brown } 1784096e49d5SMark Brown EXPORT_SYMBOL_GPL(snd_soc_codec_volatile_register); 1785096e49d5SMark Brown 1786db2a4165SFrank Mandarino /** 1787db2a4165SFrank Mandarino * snd_soc_new_ac97_codec - initailise AC97 device 1788db2a4165SFrank Mandarino * @codec: audio codec 1789db2a4165SFrank Mandarino * @ops: AC97 bus operations 1790db2a4165SFrank Mandarino * @num: AC97 codec number 1791db2a4165SFrank Mandarino * 1792db2a4165SFrank Mandarino * Initialises AC97 codec resources for use by ad-hoc devices only. 1793db2a4165SFrank Mandarino */ 1794db2a4165SFrank Mandarino int snd_soc_new_ac97_codec(struct snd_soc_codec *codec, 1795db2a4165SFrank Mandarino struct snd_ac97_bus_ops *ops, int num) 1796db2a4165SFrank Mandarino { 1797db2a4165SFrank Mandarino mutex_lock(&codec->mutex); 1798db2a4165SFrank Mandarino 1799db2a4165SFrank Mandarino codec->ac97 = kzalloc(sizeof(struct snd_ac97), GFP_KERNEL); 1800db2a4165SFrank Mandarino if (codec->ac97 == NULL) { 1801db2a4165SFrank Mandarino mutex_unlock(&codec->mutex); 1802db2a4165SFrank Mandarino return -ENOMEM; 1803db2a4165SFrank Mandarino } 1804db2a4165SFrank Mandarino 1805db2a4165SFrank Mandarino codec->ac97->bus = kzalloc(sizeof(struct snd_ac97_bus), GFP_KERNEL); 1806db2a4165SFrank Mandarino if (codec->ac97->bus == NULL) { 1807db2a4165SFrank Mandarino kfree(codec->ac97); 1808db2a4165SFrank Mandarino codec->ac97 = NULL; 1809db2a4165SFrank Mandarino mutex_unlock(&codec->mutex); 1810db2a4165SFrank Mandarino return -ENOMEM; 1811db2a4165SFrank Mandarino } 1812db2a4165SFrank Mandarino 1813db2a4165SFrank Mandarino codec->ac97->bus->ops = ops; 1814db2a4165SFrank Mandarino codec->ac97->num = num; 1815db2a4165SFrank Mandarino mutex_unlock(&codec->mutex); 1816db2a4165SFrank Mandarino return 0; 1817db2a4165SFrank Mandarino } 1818db2a4165SFrank Mandarino EXPORT_SYMBOL_GPL(snd_soc_new_ac97_codec); 1819db2a4165SFrank Mandarino 1820db2a4165SFrank Mandarino /** 1821db2a4165SFrank Mandarino * snd_soc_free_ac97_codec - free AC97 codec device 1822db2a4165SFrank Mandarino * @codec: audio codec 1823db2a4165SFrank Mandarino * 1824db2a4165SFrank Mandarino * Frees AC97 codec device resources. 1825db2a4165SFrank Mandarino */ 1826db2a4165SFrank Mandarino void snd_soc_free_ac97_codec(struct snd_soc_codec *codec) 1827db2a4165SFrank Mandarino { 1828db2a4165SFrank Mandarino mutex_lock(&codec->mutex); 1829f0fba2adSLiam Girdwood #ifdef CONFIG_SND_SOC_AC97_BUS 1830f0fba2adSLiam Girdwood soc_unregister_ac97_dai_link(codec); 1831f0fba2adSLiam Girdwood #endif 1832db2a4165SFrank Mandarino kfree(codec->ac97->bus); 1833db2a4165SFrank Mandarino kfree(codec->ac97); 1834db2a4165SFrank Mandarino codec->ac97 = NULL; 1835db2a4165SFrank Mandarino mutex_unlock(&codec->mutex); 1836db2a4165SFrank Mandarino } 1837db2a4165SFrank Mandarino EXPORT_SYMBOL_GPL(snd_soc_free_ac97_codec); 1838db2a4165SFrank Mandarino 1839db2a4165SFrank Mandarino /** 1840db2a4165SFrank Mandarino * snd_soc_update_bits - update codec register bits 1841db2a4165SFrank Mandarino * @codec: audio codec 1842db2a4165SFrank Mandarino * @reg: codec register 1843db2a4165SFrank Mandarino * @mask: register mask 1844db2a4165SFrank Mandarino * @value: new value 1845db2a4165SFrank Mandarino * 1846db2a4165SFrank Mandarino * Writes new register value. 1847db2a4165SFrank Mandarino * 1848db2a4165SFrank Mandarino * Returns 1 for change else 0. 1849db2a4165SFrank Mandarino */ 1850db2a4165SFrank Mandarino int snd_soc_update_bits(struct snd_soc_codec *codec, unsigned short reg, 185146f5822fSDaniel Ribeiro unsigned int mask, unsigned int value) 1852db2a4165SFrank Mandarino { 1853db2a4165SFrank Mandarino int change; 185446f5822fSDaniel Ribeiro unsigned int old, new; 1855db2a4165SFrank Mandarino 1856db2a4165SFrank Mandarino old = snd_soc_read(codec, reg); 1857db2a4165SFrank Mandarino new = (old & ~mask) | value; 1858db2a4165SFrank Mandarino change = old != new; 1859db2a4165SFrank Mandarino if (change) 1860db2a4165SFrank Mandarino snd_soc_write(codec, reg, new); 1861db2a4165SFrank Mandarino 1862db2a4165SFrank Mandarino return change; 1863db2a4165SFrank Mandarino } 1864db2a4165SFrank Mandarino EXPORT_SYMBOL_GPL(snd_soc_update_bits); 1865db2a4165SFrank Mandarino 1866db2a4165SFrank Mandarino /** 18676c508c62SEero Nurkkala * snd_soc_update_bits_locked - update codec register bits 18686c508c62SEero Nurkkala * @codec: audio codec 18696c508c62SEero Nurkkala * @reg: codec register 18706c508c62SEero Nurkkala * @mask: register mask 18716c508c62SEero Nurkkala * @value: new value 18726c508c62SEero Nurkkala * 18736c508c62SEero Nurkkala * Writes new register value, and takes the codec mutex. 18746c508c62SEero Nurkkala * 18756c508c62SEero Nurkkala * Returns 1 for change else 0. 18766c508c62SEero Nurkkala */ 1877dd1b3d53SMark Brown int snd_soc_update_bits_locked(struct snd_soc_codec *codec, 18786c508c62SEero Nurkkala unsigned short reg, unsigned int mask, 18796c508c62SEero Nurkkala unsigned int value) 18806c508c62SEero Nurkkala { 18816c508c62SEero Nurkkala int change; 18826c508c62SEero Nurkkala 18836c508c62SEero Nurkkala mutex_lock(&codec->mutex); 18846c508c62SEero Nurkkala change = snd_soc_update_bits(codec, reg, mask, value); 18856c508c62SEero Nurkkala mutex_unlock(&codec->mutex); 18866c508c62SEero Nurkkala 18876c508c62SEero Nurkkala return change; 18886c508c62SEero Nurkkala } 1889dd1b3d53SMark Brown EXPORT_SYMBOL_GPL(snd_soc_update_bits_locked); 18906c508c62SEero Nurkkala 18916c508c62SEero Nurkkala /** 1892db2a4165SFrank Mandarino * snd_soc_test_bits - test register for change 1893db2a4165SFrank Mandarino * @codec: audio codec 1894db2a4165SFrank Mandarino * @reg: codec register 1895db2a4165SFrank Mandarino * @mask: register mask 1896db2a4165SFrank Mandarino * @value: new value 1897db2a4165SFrank Mandarino * 1898db2a4165SFrank Mandarino * Tests a register with a new value and checks if the new value is 1899db2a4165SFrank Mandarino * different from the old value. 1900db2a4165SFrank Mandarino * 1901db2a4165SFrank Mandarino * Returns 1 for change else 0. 1902db2a4165SFrank Mandarino */ 1903db2a4165SFrank Mandarino int snd_soc_test_bits(struct snd_soc_codec *codec, unsigned short reg, 190446f5822fSDaniel Ribeiro unsigned int mask, unsigned int value) 1905db2a4165SFrank Mandarino { 1906db2a4165SFrank Mandarino int change; 190746f5822fSDaniel Ribeiro unsigned int old, new; 1908db2a4165SFrank Mandarino 1909db2a4165SFrank Mandarino old = snd_soc_read(codec, reg); 1910db2a4165SFrank Mandarino new = (old & ~mask) | value; 1911db2a4165SFrank Mandarino change = old != new; 1912db2a4165SFrank Mandarino 1913db2a4165SFrank Mandarino return change; 1914db2a4165SFrank Mandarino } 1915db2a4165SFrank Mandarino EXPORT_SYMBOL_GPL(snd_soc_test_bits); 1916db2a4165SFrank Mandarino 1917db2a4165SFrank Mandarino /** 1918db2a4165SFrank Mandarino * snd_soc_set_runtime_hwparams - set the runtime hardware parameters 1919db2a4165SFrank Mandarino * @substream: the pcm substream 1920db2a4165SFrank Mandarino * @hw: the hardware parameters 1921db2a4165SFrank Mandarino * 1922db2a4165SFrank Mandarino * Sets the substream runtime hardware parameters. 1923db2a4165SFrank Mandarino */ 1924db2a4165SFrank Mandarino int snd_soc_set_runtime_hwparams(struct snd_pcm_substream *substream, 1925db2a4165SFrank Mandarino const struct snd_pcm_hardware *hw) 1926db2a4165SFrank Mandarino { 1927db2a4165SFrank Mandarino struct snd_pcm_runtime *runtime = substream->runtime; 1928db2a4165SFrank Mandarino runtime->hw.info = hw->info; 1929db2a4165SFrank Mandarino runtime->hw.formats = hw->formats; 1930db2a4165SFrank Mandarino runtime->hw.period_bytes_min = hw->period_bytes_min; 1931db2a4165SFrank Mandarino runtime->hw.period_bytes_max = hw->period_bytes_max; 1932db2a4165SFrank Mandarino runtime->hw.periods_min = hw->periods_min; 1933db2a4165SFrank Mandarino runtime->hw.periods_max = hw->periods_max; 1934db2a4165SFrank Mandarino runtime->hw.buffer_bytes_max = hw->buffer_bytes_max; 1935db2a4165SFrank Mandarino runtime->hw.fifo_size = hw->fifo_size; 1936db2a4165SFrank Mandarino return 0; 1937db2a4165SFrank Mandarino } 1938db2a4165SFrank Mandarino EXPORT_SYMBOL_GPL(snd_soc_set_runtime_hwparams); 1939db2a4165SFrank Mandarino 1940db2a4165SFrank Mandarino /** 1941db2a4165SFrank Mandarino * snd_soc_cnew - create new control 1942db2a4165SFrank Mandarino * @_template: control template 1943db2a4165SFrank Mandarino * @data: control private data 1944ac11a2b3SMark Brown * @long_name: control long name 1945db2a4165SFrank Mandarino * 1946db2a4165SFrank Mandarino * Create a new mixer control from a template control. 1947db2a4165SFrank Mandarino * 1948db2a4165SFrank Mandarino * Returns 0 for success, else error. 1949db2a4165SFrank Mandarino */ 1950db2a4165SFrank Mandarino struct snd_kcontrol *snd_soc_cnew(const struct snd_kcontrol_new *_template, 1951db2a4165SFrank Mandarino void *data, char *long_name) 1952db2a4165SFrank Mandarino { 1953db2a4165SFrank Mandarino struct snd_kcontrol_new template; 1954db2a4165SFrank Mandarino 1955db2a4165SFrank Mandarino memcpy(&template, _template, sizeof(template)); 1956db2a4165SFrank Mandarino if (long_name) 1957db2a4165SFrank Mandarino template.name = long_name; 1958db2a4165SFrank Mandarino template.index = 0; 1959db2a4165SFrank Mandarino 1960db2a4165SFrank Mandarino return snd_ctl_new1(&template, data); 1961db2a4165SFrank Mandarino } 1962db2a4165SFrank Mandarino EXPORT_SYMBOL_GPL(snd_soc_cnew); 1963db2a4165SFrank Mandarino 1964db2a4165SFrank Mandarino /** 19653e8e1952SIan Molton * snd_soc_add_controls - add an array of controls to a codec. 19663e8e1952SIan Molton * Convienience function to add a list of controls. Many codecs were 19673e8e1952SIan Molton * duplicating this code. 19683e8e1952SIan Molton * 19693e8e1952SIan Molton * @codec: codec to add controls to 19703e8e1952SIan Molton * @controls: array of controls to add 19713e8e1952SIan Molton * @num_controls: number of elements in the array 19723e8e1952SIan Molton * 19733e8e1952SIan Molton * Return 0 for success, else error. 19743e8e1952SIan Molton */ 19753e8e1952SIan Molton int snd_soc_add_controls(struct snd_soc_codec *codec, 19763e8e1952SIan Molton const struct snd_kcontrol_new *controls, int num_controls) 19773e8e1952SIan Molton { 1978f0fba2adSLiam Girdwood struct snd_card *card = codec->card->snd_card; 19793e8e1952SIan Molton int err, i; 19803e8e1952SIan Molton 19813e8e1952SIan Molton for (i = 0; i < num_controls; i++) { 19823e8e1952SIan Molton const struct snd_kcontrol_new *control = &controls[i]; 19833e8e1952SIan Molton err = snd_ctl_add(card, snd_soc_cnew(control, codec, NULL)); 19843e8e1952SIan Molton if (err < 0) { 1985*082100dcSMark Brown dev_err(codec->dev, "%s: Failed to add %s: %d\n", 1986*082100dcSMark Brown codec->name, control->name, err); 19873e8e1952SIan Molton return err; 19883e8e1952SIan Molton } 19893e8e1952SIan Molton } 19903e8e1952SIan Molton 19913e8e1952SIan Molton return 0; 19923e8e1952SIan Molton } 19933e8e1952SIan Molton EXPORT_SYMBOL_GPL(snd_soc_add_controls); 19943e8e1952SIan Molton 19953e8e1952SIan Molton /** 1996db2a4165SFrank Mandarino * snd_soc_info_enum_double - enumerated double mixer info callback 1997db2a4165SFrank Mandarino * @kcontrol: mixer control 1998db2a4165SFrank Mandarino * @uinfo: control element information 1999db2a4165SFrank Mandarino * 2000db2a4165SFrank Mandarino * Callback to provide information about a double enumerated 2001db2a4165SFrank Mandarino * mixer control. 2002db2a4165SFrank Mandarino * 2003db2a4165SFrank Mandarino * Returns 0 for success. 2004db2a4165SFrank Mandarino */ 2005db2a4165SFrank Mandarino int snd_soc_info_enum_double(struct snd_kcontrol *kcontrol, 2006db2a4165SFrank Mandarino struct snd_ctl_elem_info *uinfo) 2007db2a4165SFrank Mandarino { 2008db2a4165SFrank Mandarino struct soc_enum *e = (struct soc_enum *)kcontrol->private_value; 2009db2a4165SFrank Mandarino 2010db2a4165SFrank Mandarino uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED; 2011db2a4165SFrank Mandarino uinfo->count = e->shift_l == e->shift_r ? 1 : 2; 2012f8ba0b7bSJon Smirl uinfo->value.enumerated.items = e->max; 2013db2a4165SFrank Mandarino 2014f8ba0b7bSJon Smirl if (uinfo->value.enumerated.item > e->max - 1) 2015f8ba0b7bSJon Smirl uinfo->value.enumerated.item = e->max - 1; 2016db2a4165SFrank Mandarino strcpy(uinfo->value.enumerated.name, 2017db2a4165SFrank Mandarino e->texts[uinfo->value.enumerated.item]); 2018db2a4165SFrank Mandarino return 0; 2019db2a4165SFrank Mandarino } 2020db2a4165SFrank Mandarino EXPORT_SYMBOL_GPL(snd_soc_info_enum_double); 2021db2a4165SFrank Mandarino 2022db2a4165SFrank Mandarino /** 2023db2a4165SFrank Mandarino * snd_soc_get_enum_double - enumerated double mixer get callback 2024db2a4165SFrank Mandarino * @kcontrol: mixer control 2025ac11a2b3SMark Brown * @ucontrol: control element information 2026db2a4165SFrank Mandarino * 2027db2a4165SFrank Mandarino * Callback to get the value of a double enumerated mixer. 2028db2a4165SFrank Mandarino * 2029db2a4165SFrank Mandarino * Returns 0 for success. 2030db2a4165SFrank Mandarino */ 2031db2a4165SFrank Mandarino int snd_soc_get_enum_double(struct snd_kcontrol *kcontrol, 2032db2a4165SFrank Mandarino struct snd_ctl_elem_value *ucontrol) 2033db2a4165SFrank Mandarino { 2034db2a4165SFrank Mandarino struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol); 2035db2a4165SFrank Mandarino struct soc_enum *e = (struct soc_enum *)kcontrol->private_value; 203646f5822fSDaniel Ribeiro unsigned int val, bitmask; 2037db2a4165SFrank Mandarino 2038f8ba0b7bSJon Smirl for (bitmask = 1; bitmask < e->max; bitmask <<= 1) 2039db2a4165SFrank Mandarino ; 2040db2a4165SFrank Mandarino val = snd_soc_read(codec, e->reg); 20413ff3f64bSMark Brown ucontrol->value.enumerated.item[0] 20423ff3f64bSMark Brown = (val >> e->shift_l) & (bitmask - 1); 2043db2a4165SFrank Mandarino if (e->shift_l != e->shift_r) 2044db2a4165SFrank Mandarino ucontrol->value.enumerated.item[1] = 2045db2a4165SFrank Mandarino (val >> e->shift_r) & (bitmask - 1); 2046db2a4165SFrank Mandarino 2047db2a4165SFrank Mandarino return 0; 2048db2a4165SFrank Mandarino } 2049db2a4165SFrank Mandarino EXPORT_SYMBOL_GPL(snd_soc_get_enum_double); 2050db2a4165SFrank Mandarino 2051db2a4165SFrank Mandarino /** 2052db2a4165SFrank Mandarino * snd_soc_put_enum_double - enumerated double mixer put callback 2053db2a4165SFrank Mandarino * @kcontrol: mixer control 2054ac11a2b3SMark Brown * @ucontrol: control element information 2055db2a4165SFrank Mandarino * 2056db2a4165SFrank Mandarino * Callback to set the value of a double enumerated mixer. 2057db2a4165SFrank Mandarino * 2058db2a4165SFrank Mandarino * Returns 0 for success. 2059db2a4165SFrank Mandarino */ 2060db2a4165SFrank Mandarino int snd_soc_put_enum_double(struct snd_kcontrol *kcontrol, 2061db2a4165SFrank Mandarino struct snd_ctl_elem_value *ucontrol) 2062db2a4165SFrank Mandarino { 2063db2a4165SFrank Mandarino struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol); 2064db2a4165SFrank Mandarino struct soc_enum *e = (struct soc_enum *)kcontrol->private_value; 206546f5822fSDaniel Ribeiro unsigned int val; 206646f5822fSDaniel Ribeiro unsigned int mask, bitmask; 2067db2a4165SFrank Mandarino 2068f8ba0b7bSJon Smirl for (bitmask = 1; bitmask < e->max; bitmask <<= 1) 2069db2a4165SFrank Mandarino ; 2070f8ba0b7bSJon Smirl if (ucontrol->value.enumerated.item[0] > e->max - 1) 2071db2a4165SFrank Mandarino return -EINVAL; 2072db2a4165SFrank Mandarino val = ucontrol->value.enumerated.item[0] << e->shift_l; 2073db2a4165SFrank Mandarino mask = (bitmask - 1) << e->shift_l; 2074db2a4165SFrank Mandarino if (e->shift_l != e->shift_r) { 2075f8ba0b7bSJon Smirl if (ucontrol->value.enumerated.item[1] > e->max - 1) 2076db2a4165SFrank Mandarino return -EINVAL; 2077db2a4165SFrank Mandarino val |= ucontrol->value.enumerated.item[1] << e->shift_r; 2078db2a4165SFrank Mandarino mask |= (bitmask - 1) << e->shift_r; 2079db2a4165SFrank Mandarino } 2080db2a4165SFrank Mandarino 20816c508c62SEero Nurkkala return snd_soc_update_bits_locked(codec, e->reg, mask, val); 2082db2a4165SFrank Mandarino } 2083db2a4165SFrank Mandarino EXPORT_SYMBOL_GPL(snd_soc_put_enum_double); 2084db2a4165SFrank Mandarino 2085db2a4165SFrank Mandarino /** 20862e72f8e3SPeter Ujfalusi * snd_soc_get_value_enum_double - semi enumerated double mixer get callback 20872e72f8e3SPeter Ujfalusi * @kcontrol: mixer control 20882e72f8e3SPeter Ujfalusi * @ucontrol: control element information 20892e72f8e3SPeter Ujfalusi * 20902e72f8e3SPeter Ujfalusi * Callback to get the value of a double semi enumerated mixer. 20912e72f8e3SPeter Ujfalusi * 20922e72f8e3SPeter Ujfalusi * Semi enumerated mixer: the enumerated items are referred as values. Can be 20932e72f8e3SPeter Ujfalusi * used for handling bitfield coded enumeration for example. 20942e72f8e3SPeter Ujfalusi * 20952e72f8e3SPeter Ujfalusi * Returns 0 for success. 20962e72f8e3SPeter Ujfalusi */ 20972e72f8e3SPeter Ujfalusi int snd_soc_get_value_enum_double(struct snd_kcontrol *kcontrol, 20982e72f8e3SPeter Ujfalusi struct snd_ctl_elem_value *ucontrol) 20992e72f8e3SPeter Ujfalusi { 21002e72f8e3SPeter Ujfalusi struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol); 210174155556SPeter Ujfalusi struct soc_enum *e = (struct soc_enum *)kcontrol->private_value; 210246f5822fSDaniel Ribeiro unsigned int reg_val, val, mux; 21032e72f8e3SPeter Ujfalusi 21042e72f8e3SPeter Ujfalusi reg_val = snd_soc_read(codec, e->reg); 21052e72f8e3SPeter Ujfalusi val = (reg_val >> e->shift_l) & e->mask; 21062e72f8e3SPeter Ujfalusi for (mux = 0; mux < e->max; mux++) { 21072e72f8e3SPeter Ujfalusi if (val == e->values[mux]) 21082e72f8e3SPeter Ujfalusi break; 21092e72f8e3SPeter Ujfalusi } 21102e72f8e3SPeter Ujfalusi ucontrol->value.enumerated.item[0] = mux; 21112e72f8e3SPeter Ujfalusi if (e->shift_l != e->shift_r) { 21122e72f8e3SPeter Ujfalusi val = (reg_val >> e->shift_r) & e->mask; 21132e72f8e3SPeter Ujfalusi for (mux = 0; mux < e->max; mux++) { 21142e72f8e3SPeter Ujfalusi if (val == e->values[mux]) 21152e72f8e3SPeter Ujfalusi break; 21162e72f8e3SPeter Ujfalusi } 21172e72f8e3SPeter Ujfalusi ucontrol->value.enumerated.item[1] = mux; 21182e72f8e3SPeter Ujfalusi } 21192e72f8e3SPeter Ujfalusi 21202e72f8e3SPeter Ujfalusi return 0; 21212e72f8e3SPeter Ujfalusi } 21222e72f8e3SPeter Ujfalusi EXPORT_SYMBOL_GPL(snd_soc_get_value_enum_double); 21232e72f8e3SPeter Ujfalusi 21242e72f8e3SPeter Ujfalusi /** 21252e72f8e3SPeter Ujfalusi * snd_soc_put_value_enum_double - semi enumerated double mixer put callback 21262e72f8e3SPeter Ujfalusi * @kcontrol: mixer control 21272e72f8e3SPeter Ujfalusi * @ucontrol: control element information 21282e72f8e3SPeter Ujfalusi * 21292e72f8e3SPeter Ujfalusi * Callback to set the value of a double semi enumerated mixer. 21302e72f8e3SPeter Ujfalusi * 21312e72f8e3SPeter Ujfalusi * Semi enumerated mixer: the enumerated items are referred as values. Can be 21322e72f8e3SPeter Ujfalusi * used for handling bitfield coded enumeration for example. 21332e72f8e3SPeter Ujfalusi * 21342e72f8e3SPeter Ujfalusi * Returns 0 for success. 21352e72f8e3SPeter Ujfalusi */ 21362e72f8e3SPeter Ujfalusi int snd_soc_put_value_enum_double(struct snd_kcontrol *kcontrol, 21372e72f8e3SPeter Ujfalusi struct snd_ctl_elem_value *ucontrol) 21382e72f8e3SPeter Ujfalusi { 21392e72f8e3SPeter Ujfalusi struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol); 214074155556SPeter Ujfalusi struct soc_enum *e = (struct soc_enum *)kcontrol->private_value; 214146f5822fSDaniel Ribeiro unsigned int val; 214246f5822fSDaniel Ribeiro unsigned int mask; 21432e72f8e3SPeter Ujfalusi 21442e72f8e3SPeter Ujfalusi if (ucontrol->value.enumerated.item[0] > e->max - 1) 21452e72f8e3SPeter Ujfalusi return -EINVAL; 21462e72f8e3SPeter Ujfalusi val = e->values[ucontrol->value.enumerated.item[0]] << e->shift_l; 21472e72f8e3SPeter Ujfalusi mask = e->mask << e->shift_l; 21482e72f8e3SPeter Ujfalusi if (e->shift_l != e->shift_r) { 21492e72f8e3SPeter Ujfalusi if (ucontrol->value.enumerated.item[1] > e->max - 1) 21502e72f8e3SPeter Ujfalusi return -EINVAL; 21512e72f8e3SPeter Ujfalusi val |= e->values[ucontrol->value.enumerated.item[1]] << e->shift_r; 21522e72f8e3SPeter Ujfalusi mask |= e->mask << e->shift_r; 21532e72f8e3SPeter Ujfalusi } 21542e72f8e3SPeter Ujfalusi 21556c508c62SEero Nurkkala return snd_soc_update_bits_locked(codec, e->reg, mask, val); 21562e72f8e3SPeter Ujfalusi } 21572e72f8e3SPeter Ujfalusi EXPORT_SYMBOL_GPL(snd_soc_put_value_enum_double); 21582e72f8e3SPeter Ujfalusi 21592e72f8e3SPeter Ujfalusi /** 2160db2a4165SFrank Mandarino * snd_soc_info_enum_ext - external enumerated single mixer info callback 2161db2a4165SFrank Mandarino * @kcontrol: mixer control 2162db2a4165SFrank Mandarino * @uinfo: control element information 2163db2a4165SFrank Mandarino * 2164db2a4165SFrank Mandarino * Callback to provide information about an external enumerated 2165db2a4165SFrank Mandarino * single mixer. 2166db2a4165SFrank Mandarino * 2167db2a4165SFrank Mandarino * Returns 0 for success. 2168db2a4165SFrank Mandarino */ 2169db2a4165SFrank Mandarino int snd_soc_info_enum_ext(struct snd_kcontrol *kcontrol, 2170db2a4165SFrank Mandarino struct snd_ctl_elem_info *uinfo) 2171db2a4165SFrank Mandarino { 2172db2a4165SFrank Mandarino struct soc_enum *e = (struct soc_enum *)kcontrol->private_value; 2173db2a4165SFrank Mandarino 2174db2a4165SFrank Mandarino uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED; 2175db2a4165SFrank Mandarino uinfo->count = 1; 2176f8ba0b7bSJon Smirl uinfo->value.enumerated.items = e->max; 2177db2a4165SFrank Mandarino 2178f8ba0b7bSJon Smirl if (uinfo->value.enumerated.item > e->max - 1) 2179f8ba0b7bSJon Smirl uinfo->value.enumerated.item = e->max - 1; 2180db2a4165SFrank Mandarino strcpy(uinfo->value.enumerated.name, 2181db2a4165SFrank Mandarino e->texts[uinfo->value.enumerated.item]); 2182db2a4165SFrank Mandarino return 0; 2183db2a4165SFrank Mandarino } 2184db2a4165SFrank Mandarino EXPORT_SYMBOL_GPL(snd_soc_info_enum_ext); 2185db2a4165SFrank Mandarino 2186db2a4165SFrank Mandarino /** 2187db2a4165SFrank Mandarino * snd_soc_info_volsw_ext - external single mixer info callback 2188db2a4165SFrank Mandarino * @kcontrol: mixer control 2189db2a4165SFrank Mandarino * @uinfo: control element information 2190db2a4165SFrank Mandarino * 2191db2a4165SFrank Mandarino * Callback to provide information about a single external mixer control. 2192db2a4165SFrank Mandarino * 2193db2a4165SFrank Mandarino * Returns 0 for success. 2194db2a4165SFrank Mandarino */ 2195db2a4165SFrank Mandarino int snd_soc_info_volsw_ext(struct snd_kcontrol *kcontrol, 2196db2a4165SFrank Mandarino struct snd_ctl_elem_info *uinfo) 2197db2a4165SFrank Mandarino { 2198a7a4ac86SPhilipp Zabel int max = kcontrol->private_value; 2199db2a4165SFrank Mandarino 2200fd5dfad9SMark Brown if (max == 1 && !strstr(kcontrol->id.name, " Volume")) 2201a7a4ac86SPhilipp Zabel uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN; 2202a7a4ac86SPhilipp Zabel else 2203a7a4ac86SPhilipp Zabel uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER; 2204a7a4ac86SPhilipp Zabel 2205db2a4165SFrank Mandarino uinfo->count = 1; 2206db2a4165SFrank Mandarino uinfo->value.integer.min = 0; 2207a7a4ac86SPhilipp Zabel uinfo->value.integer.max = max; 2208db2a4165SFrank Mandarino return 0; 2209db2a4165SFrank Mandarino } 2210db2a4165SFrank Mandarino EXPORT_SYMBOL_GPL(snd_soc_info_volsw_ext); 2211db2a4165SFrank Mandarino 2212db2a4165SFrank Mandarino /** 2213db2a4165SFrank Mandarino * snd_soc_info_volsw - single mixer info callback 2214db2a4165SFrank Mandarino * @kcontrol: mixer control 2215db2a4165SFrank Mandarino * @uinfo: control element information 2216db2a4165SFrank Mandarino * 2217db2a4165SFrank Mandarino * Callback to provide information about a single mixer control. 2218db2a4165SFrank Mandarino * 2219db2a4165SFrank Mandarino * Returns 0 for success. 2220db2a4165SFrank Mandarino */ 2221db2a4165SFrank Mandarino int snd_soc_info_volsw(struct snd_kcontrol *kcontrol, 2222db2a4165SFrank Mandarino struct snd_ctl_elem_info *uinfo) 2223db2a4165SFrank Mandarino { 22244eaa9819SJon Smirl struct soc_mixer_control *mc = 22254eaa9819SJon Smirl (struct soc_mixer_control *)kcontrol->private_value; 2226d11bb4a9SPeter Ujfalusi int platform_max; 2227762b8df7SMark Brown unsigned int shift = mc->shift; 2228815ecf8dSJon Smirl unsigned int rshift = mc->rshift; 2229db2a4165SFrank Mandarino 2230d11bb4a9SPeter Ujfalusi if (!mc->platform_max) 2231d11bb4a9SPeter Ujfalusi mc->platform_max = mc->max; 2232d11bb4a9SPeter Ujfalusi platform_max = mc->platform_max; 2233d11bb4a9SPeter Ujfalusi 2234d11bb4a9SPeter Ujfalusi if (platform_max == 1 && !strstr(kcontrol->id.name, " Volume")) 2235a7a4ac86SPhilipp Zabel uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN; 2236a7a4ac86SPhilipp Zabel else 2237a7a4ac86SPhilipp Zabel uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER; 2238a7a4ac86SPhilipp Zabel 2239db2a4165SFrank Mandarino uinfo->count = shift == rshift ? 1 : 2; 2240db2a4165SFrank Mandarino uinfo->value.integer.min = 0; 2241d11bb4a9SPeter Ujfalusi uinfo->value.integer.max = platform_max; 2242db2a4165SFrank Mandarino return 0; 2243db2a4165SFrank Mandarino } 2244db2a4165SFrank Mandarino EXPORT_SYMBOL_GPL(snd_soc_info_volsw); 2245db2a4165SFrank Mandarino 2246db2a4165SFrank Mandarino /** 2247db2a4165SFrank Mandarino * snd_soc_get_volsw - single mixer get callback 2248db2a4165SFrank Mandarino * @kcontrol: mixer control 2249ac11a2b3SMark Brown * @ucontrol: control element information 2250db2a4165SFrank Mandarino * 2251db2a4165SFrank Mandarino * Callback to get the value of a single mixer control. 2252db2a4165SFrank Mandarino * 2253db2a4165SFrank Mandarino * Returns 0 for success. 2254db2a4165SFrank Mandarino */ 2255db2a4165SFrank Mandarino int snd_soc_get_volsw(struct snd_kcontrol *kcontrol, 2256db2a4165SFrank Mandarino struct snd_ctl_elem_value *ucontrol) 2257db2a4165SFrank Mandarino { 22584eaa9819SJon Smirl struct soc_mixer_control *mc = 22594eaa9819SJon Smirl (struct soc_mixer_control *)kcontrol->private_value; 2260db2a4165SFrank Mandarino struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol); 2261815ecf8dSJon Smirl unsigned int reg = mc->reg; 2262815ecf8dSJon Smirl unsigned int shift = mc->shift; 2263815ecf8dSJon Smirl unsigned int rshift = mc->rshift; 22644eaa9819SJon Smirl int max = mc->max; 2265815ecf8dSJon Smirl unsigned int mask = (1 << fls(max)) - 1; 2266815ecf8dSJon Smirl unsigned int invert = mc->invert; 2267db2a4165SFrank Mandarino 2268db2a4165SFrank Mandarino ucontrol->value.integer.value[0] = 2269db2a4165SFrank Mandarino (snd_soc_read(codec, reg) >> shift) & mask; 2270db2a4165SFrank Mandarino if (shift != rshift) 2271db2a4165SFrank Mandarino ucontrol->value.integer.value[1] = 2272db2a4165SFrank Mandarino (snd_soc_read(codec, reg) >> rshift) & mask; 2273db2a4165SFrank Mandarino if (invert) { 2274db2a4165SFrank Mandarino ucontrol->value.integer.value[0] = 2275a7a4ac86SPhilipp Zabel max - ucontrol->value.integer.value[0]; 2276db2a4165SFrank Mandarino if (shift != rshift) 2277db2a4165SFrank Mandarino ucontrol->value.integer.value[1] = 2278a7a4ac86SPhilipp Zabel max - ucontrol->value.integer.value[1]; 2279db2a4165SFrank Mandarino } 2280db2a4165SFrank Mandarino 2281db2a4165SFrank Mandarino return 0; 2282db2a4165SFrank Mandarino } 2283db2a4165SFrank Mandarino EXPORT_SYMBOL_GPL(snd_soc_get_volsw); 2284db2a4165SFrank Mandarino 2285db2a4165SFrank Mandarino /** 2286db2a4165SFrank Mandarino * snd_soc_put_volsw - single mixer put callback 2287db2a4165SFrank Mandarino * @kcontrol: mixer control 2288ac11a2b3SMark Brown * @ucontrol: control element information 2289db2a4165SFrank Mandarino * 2290db2a4165SFrank Mandarino * Callback to set the value of a single mixer control. 2291db2a4165SFrank Mandarino * 2292db2a4165SFrank Mandarino * Returns 0 for success. 2293db2a4165SFrank Mandarino */ 2294db2a4165SFrank Mandarino int snd_soc_put_volsw(struct snd_kcontrol *kcontrol, 2295db2a4165SFrank Mandarino struct snd_ctl_elem_value *ucontrol) 2296db2a4165SFrank Mandarino { 22974eaa9819SJon Smirl struct soc_mixer_control *mc = 22984eaa9819SJon Smirl (struct soc_mixer_control *)kcontrol->private_value; 2299db2a4165SFrank Mandarino struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol); 2300815ecf8dSJon Smirl unsigned int reg = mc->reg; 2301815ecf8dSJon Smirl unsigned int shift = mc->shift; 2302815ecf8dSJon Smirl unsigned int rshift = mc->rshift; 23034eaa9819SJon Smirl int max = mc->max; 2304815ecf8dSJon Smirl unsigned int mask = (1 << fls(max)) - 1; 2305815ecf8dSJon Smirl unsigned int invert = mc->invert; 230646f5822fSDaniel Ribeiro unsigned int val, val2, val_mask; 2307db2a4165SFrank Mandarino 2308db2a4165SFrank Mandarino val = (ucontrol->value.integer.value[0] & mask); 2309db2a4165SFrank Mandarino if (invert) 2310a7a4ac86SPhilipp Zabel val = max - val; 2311db2a4165SFrank Mandarino val_mask = mask << shift; 2312db2a4165SFrank Mandarino val = val << shift; 2313db2a4165SFrank Mandarino if (shift != rshift) { 2314db2a4165SFrank Mandarino val2 = (ucontrol->value.integer.value[1] & mask); 2315db2a4165SFrank Mandarino if (invert) 2316a7a4ac86SPhilipp Zabel val2 = max - val2; 2317db2a4165SFrank Mandarino val_mask |= mask << rshift; 2318db2a4165SFrank Mandarino val |= val2 << rshift; 2319db2a4165SFrank Mandarino } 23206c508c62SEero Nurkkala return snd_soc_update_bits_locked(codec, reg, val_mask, val); 2321db2a4165SFrank Mandarino } 2322db2a4165SFrank Mandarino EXPORT_SYMBOL_GPL(snd_soc_put_volsw); 2323db2a4165SFrank Mandarino 2324db2a4165SFrank Mandarino /** 2325db2a4165SFrank Mandarino * snd_soc_info_volsw_2r - double mixer info callback 2326db2a4165SFrank Mandarino * @kcontrol: mixer control 2327db2a4165SFrank Mandarino * @uinfo: control element information 2328db2a4165SFrank Mandarino * 2329db2a4165SFrank Mandarino * Callback to provide information about a double mixer control that 2330db2a4165SFrank Mandarino * spans 2 codec registers. 2331db2a4165SFrank Mandarino * 2332db2a4165SFrank Mandarino * Returns 0 for success. 2333db2a4165SFrank Mandarino */ 2334db2a4165SFrank Mandarino int snd_soc_info_volsw_2r(struct snd_kcontrol *kcontrol, 2335db2a4165SFrank Mandarino struct snd_ctl_elem_info *uinfo) 2336db2a4165SFrank Mandarino { 23374eaa9819SJon Smirl struct soc_mixer_control *mc = 23384eaa9819SJon Smirl (struct soc_mixer_control *)kcontrol->private_value; 2339d11bb4a9SPeter Ujfalusi int platform_max; 2340db2a4165SFrank Mandarino 2341d11bb4a9SPeter Ujfalusi if (!mc->platform_max) 2342d11bb4a9SPeter Ujfalusi mc->platform_max = mc->max; 2343d11bb4a9SPeter Ujfalusi platform_max = mc->platform_max; 2344d11bb4a9SPeter Ujfalusi 2345d11bb4a9SPeter Ujfalusi if (platform_max == 1 && !strstr(kcontrol->id.name, " Volume")) 2346a7a4ac86SPhilipp Zabel uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN; 2347a7a4ac86SPhilipp Zabel else 2348a7a4ac86SPhilipp Zabel uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER; 2349a7a4ac86SPhilipp Zabel 2350db2a4165SFrank Mandarino uinfo->count = 2; 2351db2a4165SFrank Mandarino uinfo->value.integer.min = 0; 2352d11bb4a9SPeter Ujfalusi uinfo->value.integer.max = platform_max; 2353db2a4165SFrank Mandarino return 0; 2354db2a4165SFrank Mandarino } 2355db2a4165SFrank Mandarino EXPORT_SYMBOL_GPL(snd_soc_info_volsw_2r); 2356db2a4165SFrank Mandarino 2357db2a4165SFrank Mandarino /** 2358db2a4165SFrank Mandarino * snd_soc_get_volsw_2r - double mixer get callback 2359db2a4165SFrank Mandarino * @kcontrol: mixer control 2360ac11a2b3SMark Brown * @ucontrol: control element information 2361db2a4165SFrank Mandarino * 2362db2a4165SFrank Mandarino * Callback to get the value of a double mixer control that spans 2 registers. 2363db2a4165SFrank Mandarino * 2364db2a4165SFrank Mandarino * Returns 0 for success. 2365db2a4165SFrank Mandarino */ 2366db2a4165SFrank Mandarino int snd_soc_get_volsw_2r(struct snd_kcontrol *kcontrol, 2367db2a4165SFrank Mandarino struct snd_ctl_elem_value *ucontrol) 2368db2a4165SFrank Mandarino { 23694eaa9819SJon Smirl struct soc_mixer_control *mc = 23704eaa9819SJon Smirl (struct soc_mixer_control *)kcontrol->private_value; 2371db2a4165SFrank Mandarino struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol); 2372815ecf8dSJon Smirl unsigned int reg = mc->reg; 2373815ecf8dSJon Smirl unsigned int reg2 = mc->rreg; 2374815ecf8dSJon Smirl unsigned int shift = mc->shift; 23754eaa9819SJon Smirl int max = mc->max; 2376815ecf8dSJon Smirl unsigned int mask = (1 << fls(max)) - 1; 2377815ecf8dSJon Smirl unsigned int invert = mc->invert; 2378db2a4165SFrank Mandarino 2379db2a4165SFrank Mandarino ucontrol->value.integer.value[0] = 2380db2a4165SFrank Mandarino (snd_soc_read(codec, reg) >> shift) & mask; 2381db2a4165SFrank Mandarino ucontrol->value.integer.value[1] = 2382db2a4165SFrank Mandarino (snd_soc_read(codec, reg2) >> shift) & mask; 2383db2a4165SFrank Mandarino if (invert) { 2384db2a4165SFrank Mandarino ucontrol->value.integer.value[0] = 2385a7a4ac86SPhilipp Zabel max - ucontrol->value.integer.value[0]; 2386db2a4165SFrank Mandarino ucontrol->value.integer.value[1] = 2387a7a4ac86SPhilipp Zabel max - ucontrol->value.integer.value[1]; 2388db2a4165SFrank Mandarino } 2389db2a4165SFrank Mandarino 2390db2a4165SFrank Mandarino return 0; 2391db2a4165SFrank Mandarino } 2392db2a4165SFrank Mandarino EXPORT_SYMBOL_GPL(snd_soc_get_volsw_2r); 2393db2a4165SFrank Mandarino 2394db2a4165SFrank Mandarino /** 2395db2a4165SFrank Mandarino * snd_soc_put_volsw_2r - double mixer set callback 2396db2a4165SFrank Mandarino * @kcontrol: mixer control 2397ac11a2b3SMark Brown * @ucontrol: control element information 2398db2a4165SFrank Mandarino * 2399db2a4165SFrank Mandarino * Callback to set the value of a double mixer control that spans 2 registers. 2400db2a4165SFrank Mandarino * 2401db2a4165SFrank Mandarino * Returns 0 for success. 2402db2a4165SFrank Mandarino */ 2403db2a4165SFrank Mandarino int snd_soc_put_volsw_2r(struct snd_kcontrol *kcontrol, 2404db2a4165SFrank Mandarino struct snd_ctl_elem_value *ucontrol) 2405db2a4165SFrank Mandarino { 24064eaa9819SJon Smirl struct soc_mixer_control *mc = 24074eaa9819SJon Smirl (struct soc_mixer_control *)kcontrol->private_value; 2408db2a4165SFrank Mandarino struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol); 2409815ecf8dSJon Smirl unsigned int reg = mc->reg; 2410815ecf8dSJon Smirl unsigned int reg2 = mc->rreg; 2411815ecf8dSJon Smirl unsigned int shift = mc->shift; 24124eaa9819SJon Smirl int max = mc->max; 2413815ecf8dSJon Smirl unsigned int mask = (1 << fls(max)) - 1; 2414815ecf8dSJon Smirl unsigned int invert = mc->invert; 2415db2a4165SFrank Mandarino int err; 241646f5822fSDaniel Ribeiro unsigned int val, val2, val_mask; 2417db2a4165SFrank Mandarino 2418db2a4165SFrank Mandarino val_mask = mask << shift; 2419db2a4165SFrank Mandarino val = (ucontrol->value.integer.value[0] & mask); 2420db2a4165SFrank Mandarino val2 = (ucontrol->value.integer.value[1] & mask); 2421db2a4165SFrank Mandarino 2422db2a4165SFrank Mandarino if (invert) { 2423a7a4ac86SPhilipp Zabel val = max - val; 2424a7a4ac86SPhilipp Zabel val2 = max - val2; 2425db2a4165SFrank Mandarino } 2426db2a4165SFrank Mandarino 2427db2a4165SFrank Mandarino val = val << shift; 2428db2a4165SFrank Mandarino val2 = val2 << shift; 2429db2a4165SFrank Mandarino 24306c508c62SEero Nurkkala err = snd_soc_update_bits_locked(codec, reg, val_mask, val); 24313ff3f64bSMark Brown if (err < 0) 2432db2a4165SFrank Mandarino return err; 2433db2a4165SFrank Mandarino 24346c508c62SEero Nurkkala err = snd_soc_update_bits_locked(codec, reg2, val_mask, val2); 2435db2a4165SFrank Mandarino return err; 2436db2a4165SFrank Mandarino } 2437db2a4165SFrank Mandarino EXPORT_SYMBOL_GPL(snd_soc_put_volsw_2r); 2438db2a4165SFrank Mandarino 2439e13ac2e9SMark Brown /** 2440e13ac2e9SMark Brown * snd_soc_info_volsw_s8 - signed mixer info callback 2441e13ac2e9SMark Brown * @kcontrol: mixer control 2442e13ac2e9SMark Brown * @uinfo: control element information 2443e13ac2e9SMark Brown * 2444e13ac2e9SMark Brown * Callback to provide information about a signed mixer control. 2445e13ac2e9SMark Brown * 2446e13ac2e9SMark Brown * Returns 0 for success. 2447e13ac2e9SMark Brown */ 2448e13ac2e9SMark Brown int snd_soc_info_volsw_s8(struct snd_kcontrol *kcontrol, 2449e13ac2e9SMark Brown struct snd_ctl_elem_info *uinfo) 2450e13ac2e9SMark Brown { 24514eaa9819SJon Smirl struct soc_mixer_control *mc = 24524eaa9819SJon Smirl (struct soc_mixer_control *)kcontrol->private_value; 2453d11bb4a9SPeter Ujfalusi int platform_max; 24544eaa9819SJon Smirl int min = mc->min; 2455e13ac2e9SMark Brown 2456d11bb4a9SPeter Ujfalusi if (!mc->platform_max) 2457d11bb4a9SPeter Ujfalusi mc->platform_max = mc->max; 2458d11bb4a9SPeter Ujfalusi platform_max = mc->platform_max; 2459d11bb4a9SPeter Ujfalusi 2460e13ac2e9SMark Brown uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER; 2461e13ac2e9SMark Brown uinfo->count = 2; 2462e13ac2e9SMark Brown uinfo->value.integer.min = 0; 2463d11bb4a9SPeter Ujfalusi uinfo->value.integer.max = platform_max - min; 2464e13ac2e9SMark Brown return 0; 2465e13ac2e9SMark Brown } 2466e13ac2e9SMark Brown EXPORT_SYMBOL_GPL(snd_soc_info_volsw_s8); 2467e13ac2e9SMark Brown 2468e13ac2e9SMark Brown /** 2469e13ac2e9SMark Brown * snd_soc_get_volsw_s8 - signed mixer get callback 2470e13ac2e9SMark Brown * @kcontrol: mixer control 2471ac11a2b3SMark Brown * @ucontrol: control element information 2472e13ac2e9SMark Brown * 2473e13ac2e9SMark Brown * Callback to get the value of a signed mixer control. 2474e13ac2e9SMark Brown * 2475e13ac2e9SMark Brown * Returns 0 for success. 2476e13ac2e9SMark Brown */ 2477e13ac2e9SMark Brown int snd_soc_get_volsw_s8(struct snd_kcontrol *kcontrol, 2478e13ac2e9SMark Brown struct snd_ctl_elem_value *ucontrol) 2479e13ac2e9SMark Brown { 24804eaa9819SJon Smirl struct soc_mixer_control *mc = 24814eaa9819SJon Smirl (struct soc_mixer_control *)kcontrol->private_value; 2482e13ac2e9SMark Brown struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol); 2483815ecf8dSJon Smirl unsigned int reg = mc->reg; 24844eaa9819SJon Smirl int min = mc->min; 2485e13ac2e9SMark Brown int val = snd_soc_read(codec, reg); 2486e13ac2e9SMark Brown 2487e13ac2e9SMark Brown ucontrol->value.integer.value[0] = 2488e13ac2e9SMark Brown ((signed char)(val & 0xff))-min; 2489e13ac2e9SMark Brown ucontrol->value.integer.value[1] = 2490e13ac2e9SMark Brown ((signed char)((val >> 8) & 0xff))-min; 2491e13ac2e9SMark Brown return 0; 2492e13ac2e9SMark Brown } 2493e13ac2e9SMark Brown EXPORT_SYMBOL_GPL(snd_soc_get_volsw_s8); 2494e13ac2e9SMark Brown 2495e13ac2e9SMark Brown /** 2496e13ac2e9SMark Brown * snd_soc_put_volsw_sgn - signed mixer put callback 2497e13ac2e9SMark Brown * @kcontrol: mixer control 2498ac11a2b3SMark Brown * @ucontrol: control element information 2499e13ac2e9SMark Brown * 2500e13ac2e9SMark Brown * Callback to set the value of a signed mixer control. 2501e13ac2e9SMark Brown * 2502e13ac2e9SMark Brown * Returns 0 for success. 2503e13ac2e9SMark Brown */ 2504e13ac2e9SMark Brown int snd_soc_put_volsw_s8(struct snd_kcontrol *kcontrol, 2505e13ac2e9SMark Brown struct snd_ctl_elem_value *ucontrol) 2506e13ac2e9SMark Brown { 25074eaa9819SJon Smirl struct soc_mixer_control *mc = 25084eaa9819SJon Smirl (struct soc_mixer_control *)kcontrol->private_value; 2509e13ac2e9SMark Brown struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol); 2510815ecf8dSJon Smirl unsigned int reg = mc->reg; 25114eaa9819SJon Smirl int min = mc->min; 251246f5822fSDaniel Ribeiro unsigned int val; 2513e13ac2e9SMark Brown 2514e13ac2e9SMark Brown val = (ucontrol->value.integer.value[0]+min) & 0xff; 2515e13ac2e9SMark Brown val |= ((ucontrol->value.integer.value[1]+min) & 0xff) << 8; 2516e13ac2e9SMark Brown 25176c508c62SEero Nurkkala return snd_soc_update_bits_locked(codec, reg, 0xffff, val); 2518e13ac2e9SMark Brown } 2519e13ac2e9SMark Brown EXPORT_SYMBOL_GPL(snd_soc_put_volsw_s8); 2520e13ac2e9SMark Brown 25218c6529dbSLiam Girdwood /** 2522637d3847SPeter Ujfalusi * snd_soc_limit_volume - Set new limit to an existing volume control. 2523637d3847SPeter Ujfalusi * 2524637d3847SPeter Ujfalusi * @codec: where to look for the control 2525637d3847SPeter Ujfalusi * @name: Name of the control 2526637d3847SPeter Ujfalusi * @max: new maximum limit 2527637d3847SPeter Ujfalusi * 2528637d3847SPeter Ujfalusi * Return 0 for success, else error. 2529637d3847SPeter Ujfalusi */ 2530637d3847SPeter Ujfalusi int snd_soc_limit_volume(struct snd_soc_codec *codec, 2531637d3847SPeter Ujfalusi const char *name, int max) 2532637d3847SPeter Ujfalusi { 2533f0fba2adSLiam Girdwood struct snd_card *card = codec->card->snd_card; 2534637d3847SPeter Ujfalusi struct snd_kcontrol *kctl; 2535637d3847SPeter Ujfalusi struct soc_mixer_control *mc; 2536637d3847SPeter Ujfalusi int found = 0; 2537637d3847SPeter Ujfalusi int ret = -EINVAL; 2538637d3847SPeter Ujfalusi 2539637d3847SPeter Ujfalusi /* Sanity check for name and max */ 2540637d3847SPeter Ujfalusi if (unlikely(!name || max <= 0)) 2541637d3847SPeter Ujfalusi return -EINVAL; 2542637d3847SPeter Ujfalusi 2543637d3847SPeter Ujfalusi list_for_each_entry(kctl, &card->controls, list) { 2544637d3847SPeter Ujfalusi if (!strncmp(kctl->id.name, name, sizeof(kctl->id.name))) { 2545637d3847SPeter Ujfalusi found = 1; 2546637d3847SPeter Ujfalusi break; 2547637d3847SPeter Ujfalusi } 2548637d3847SPeter Ujfalusi } 2549637d3847SPeter Ujfalusi if (found) { 2550637d3847SPeter Ujfalusi mc = (struct soc_mixer_control *)kctl->private_value; 2551637d3847SPeter Ujfalusi if (max <= mc->max) { 2552d11bb4a9SPeter Ujfalusi mc->platform_max = max; 2553637d3847SPeter Ujfalusi ret = 0; 2554637d3847SPeter Ujfalusi } 2555637d3847SPeter Ujfalusi } 2556637d3847SPeter Ujfalusi return ret; 2557637d3847SPeter Ujfalusi } 2558637d3847SPeter Ujfalusi EXPORT_SYMBOL_GPL(snd_soc_limit_volume); 2559637d3847SPeter Ujfalusi 2560637d3847SPeter Ujfalusi /** 2561b6f4bb38Sapatard@mandriva.com * snd_soc_info_volsw_2r_sx - double with tlv and variable data size 2562b6f4bb38Sapatard@mandriva.com * mixer info callback 2563b6f4bb38Sapatard@mandriva.com * @kcontrol: mixer control 2564b6f4bb38Sapatard@mandriva.com * @uinfo: control element information 2565b6f4bb38Sapatard@mandriva.com * 2566b6f4bb38Sapatard@mandriva.com * Returns 0 for success. 2567b6f4bb38Sapatard@mandriva.com */ 2568b6f4bb38Sapatard@mandriva.com int snd_soc_info_volsw_2r_sx(struct snd_kcontrol *kcontrol, 2569b6f4bb38Sapatard@mandriva.com struct snd_ctl_elem_info *uinfo) 2570b6f4bb38Sapatard@mandriva.com { 2571b6f4bb38Sapatard@mandriva.com struct soc_mixer_control *mc = 2572b6f4bb38Sapatard@mandriva.com (struct soc_mixer_control *)kcontrol->private_value; 2573b6f4bb38Sapatard@mandriva.com int max = mc->max; 2574b6f4bb38Sapatard@mandriva.com int min = mc->min; 2575b6f4bb38Sapatard@mandriva.com 2576b6f4bb38Sapatard@mandriva.com uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER; 2577b6f4bb38Sapatard@mandriva.com uinfo->count = 2; 2578b6f4bb38Sapatard@mandriva.com uinfo->value.integer.min = 0; 2579b6f4bb38Sapatard@mandriva.com uinfo->value.integer.max = max-min; 2580b6f4bb38Sapatard@mandriva.com 2581b6f4bb38Sapatard@mandriva.com return 0; 2582b6f4bb38Sapatard@mandriva.com } 2583b6f4bb38Sapatard@mandriva.com EXPORT_SYMBOL_GPL(snd_soc_info_volsw_2r_sx); 2584b6f4bb38Sapatard@mandriva.com 2585b6f4bb38Sapatard@mandriva.com /** 2586b6f4bb38Sapatard@mandriva.com * snd_soc_get_volsw_2r_sx - double with tlv and variable data size 2587b6f4bb38Sapatard@mandriva.com * mixer get callback 2588b6f4bb38Sapatard@mandriva.com * @kcontrol: mixer control 2589b6f4bb38Sapatard@mandriva.com * @uinfo: control element information 2590b6f4bb38Sapatard@mandriva.com * 2591b6f4bb38Sapatard@mandriva.com * Returns 0 for success. 2592b6f4bb38Sapatard@mandriva.com */ 2593b6f4bb38Sapatard@mandriva.com int snd_soc_get_volsw_2r_sx(struct snd_kcontrol *kcontrol, 2594b6f4bb38Sapatard@mandriva.com struct snd_ctl_elem_value *ucontrol) 2595b6f4bb38Sapatard@mandriva.com { 2596b6f4bb38Sapatard@mandriva.com struct soc_mixer_control *mc = 2597b6f4bb38Sapatard@mandriva.com (struct soc_mixer_control *)kcontrol->private_value; 2598b6f4bb38Sapatard@mandriva.com struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol); 2599b6f4bb38Sapatard@mandriva.com unsigned int mask = (1<<mc->shift)-1; 2600b6f4bb38Sapatard@mandriva.com int min = mc->min; 2601b6f4bb38Sapatard@mandriva.com int val = snd_soc_read(codec, mc->reg) & mask; 2602b6f4bb38Sapatard@mandriva.com int valr = snd_soc_read(codec, mc->rreg) & mask; 2603b6f4bb38Sapatard@mandriva.com 260420630c7fSStuart Longland ucontrol->value.integer.value[0] = ((val & 0xff)-min) & mask; 260520630c7fSStuart Longland ucontrol->value.integer.value[1] = ((valr & 0xff)-min) & mask; 2606b6f4bb38Sapatard@mandriva.com return 0; 2607b6f4bb38Sapatard@mandriva.com } 2608b6f4bb38Sapatard@mandriva.com EXPORT_SYMBOL_GPL(snd_soc_get_volsw_2r_sx); 2609b6f4bb38Sapatard@mandriva.com 2610b6f4bb38Sapatard@mandriva.com /** 2611b6f4bb38Sapatard@mandriva.com * snd_soc_put_volsw_2r_sx - double with tlv and variable data size 2612b6f4bb38Sapatard@mandriva.com * mixer put callback 2613b6f4bb38Sapatard@mandriva.com * @kcontrol: mixer control 2614b6f4bb38Sapatard@mandriva.com * @uinfo: control element information 2615b6f4bb38Sapatard@mandriva.com * 2616b6f4bb38Sapatard@mandriva.com * Returns 0 for success. 2617b6f4bb38Sapatard@mandriva.com */ 2618b6f4bb38Sapatard@mandriva.com int snd_soc_put_volsw_2r_sx(struct snd_kcontrol *kcontrol, 2619b6f4bb38Sapatard@mandriva.com struct snd_ctl_elem_value *ucontrol) 2620b6f4bb38Sapatard@mandriva.com { 2621b6f4bb38Sapatard@mandriva.com struct soc_mixer_control *mc = 2622b6f4bb38Sapatard@mandriva.com (struct soc_mixer_control *)kcontrol->private_value; 2623b6f4bb38Sapatard@mandriva.com struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol); 2624b6f4bb38Sapatard@mandriva.com unsigned int mask = (1<<mc->shift)-1; 2625b6f4bb38Sapatard@mandriva.com int min = mc->min; 2626b6f4bb38Sapatard@mandriva.com int ret; 2627b6f4bb38Sapatard@mandriva.com unsigned int val, valr, oval, ovalr; 2628b6f4bb38Sapatard@mandriva.com 2629b6f4bb38Sapatard@mandriva.com val = ((ucontrol->value.integer.value[0]+min) & 0xff); 2630b6f4bb38Sapatard@mandriva.com val &= mask; 2631b6f4bb38Sapatard@mandriva.com valr = ((ucontrol->value.integer.value[1]+min) & 0xff); 2632b6f4bb38Sapatard@mandriva.com valr &= mask; 2633b6f4bb38Sapatard@mandriva.com 2634b6f4bb38Sapatard@mandriva.com oval = snd_soc_read(codec, mc->reg) & mask; 2635b6f4bb38Sapatard@mandriva.com ovalr = snd_soc_read(codec, mc->rreg) & mask; 2636b6f4bb38Sapatard@mandriva.com 2637b6f4bb38Sapatard@mandriva.com ret = 0; 2638b6f4bb38Sapatard@mandriva.com if (oval != val) { 2639b6f4bb38Sapatard@mandriva.com ret = snd_soc_write(codec, mc->reg, val); 2640b6f4bb38Sapatard@mandriva.com if (ret < 0) 2641f1df5aecSMark Brown return ret; 2642b6f4bb38Sapatard@mandriva.com } 2643b6f4bb38Sapatard@mandriva.com if (ovalr != valr) { 2644b6f4bb38Sapatard@mandriva.com ret = snd_soc_write(codec, mc->rreg, valr); 2645b6f4bb38Sapatard@mandriva.com if (ret < 0) 2646f1df5aecSMark Brown return ret; 2647b6f4bb38Sapatard@mandriva.com } 2648b6f4bb38Sapatard@mandriva.com 2649b6f4bb38Sapatard@mandriva.com return 0; 2650b6f4bb38Sapatard@mandriva.com } 2651b6f4bb38Sapatard@mandriva.com EXPORT_SYMBOL_GPL(snd_soc_put_volsw_2r_sx); 2652b6f4bb38Sapatard@mandriva.com 2653b6f4bb38Sapatard@mandriva.com /** 26548c6529dbSLiam Girdwood * snd_soc_dai_set_sysclk - configure DAI system or master clock. 26558c6529dbSLiam Girdwood * @dai: DAI 26568c6529dbSLiam Girdwood * @clk_id: DAI specific clock ID 26578c6529dbSLiam Girdwood * @freq: new clock frequency in Hz 26588c6529dbSLiam Girdwood * @dir: new clock direction - input/output. 26598c6529dbSLiam Girdwood * 26608c6529dbSLiam Girdwood * Configures the DAI master (MCLK) or system (SYSCLK) clocking. 26618c6529dbSLiam Girdwood */ 26628c6529dbSLiam Girdwood int snd_soc_dai_set_sysclk(struct snd_soc_dai *dai, int clk_id, 26638c6529dbSLiam Girdwood unsigned int freq, int dir) 26648c6529dbSLiam Girdwood { 2665f0fba2adSLiam Girdwood if (dai->driver && dai->driver->ops->set_sysclk) 2666f0fba2adSLiam Girdwood return dai->driver->ops->set_sysclk(dai, clk_id, freq, dir); 26678c6529dbSLiam Girdwood else 26688c6529dbSLiam Girdwood return -EINVAL; 26698c6529dbSLiam Girdwood } 26708c6529dbSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_dai_set_sysclk); 26718c6529dbSLiam Girdwood 26728c6529dbSLiam Girdwood /** 26738c6529dbSLiam Girdwood * snd_soc_dai_set_clkdiv - configure DAI clock dividers. 26748c6529dbSLiam Girdwood * @dai: DAI 2675ac11a2b3SMark Brown * @div_id: DAI specific clock divider ID 26768c6529dbSLiam Girdwood * @div: new clock divisor. 26778c6529dbSLiam Girdwood * 26788c6529dbSLiam Girdwood * Configures the clock dividers. This is used to derive the best DAI bit and 26798c6529dbSLiam Girdwood * frame clocks from the system or master clock. It's best to set the DAI bit 26808c6529dbSLiam Girdwood * and frame clocks as low as possible to save system power. 26818c6529dbSLiam Girdwood */ 26828c6529dbSLiam Girdwood int snd_soc_dai_set_clkdiv(struct snd_soc_dai *dai, 26838c6529dbSLiam Girdwood int div_id, int div) 26848c6529dbSLiam Girdwood { 2685f0fba2adSLiam Girdwood if (dai->driver && dai->driver->ops->set_clkdiv) 2686f0fba2adSLiam Girdwood return dai->driver->ops->set_clkdiv(dai, div_id, div); 26878c6529dbSLiam Girdwood else 26888c6529dbSLiam Girdwood return -EINVAL; 26898c6529dbSLiam Girdwood } 26908c6529dbSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_dai_set_clkdiv); 26918c6529dbSLiam Girdwood 26928c6529dbSLiam Girdwood /** 26938c6529dbSLiam Girdwood * snd_soc_dai_set_pll - configure DAI PLL. 26948c6529dbSLiam Girdwood * @dai: DAI 26958c6529dbSLiam Girdwood * @pll_id: DAI specific PLL ID 269685488037SMark Brown * @source: DAI specific source for the PLL 26978c6529dbSLiam Girdwood * @freq_in: PLL input clock frequency in Hz 26988c6529dbSLiam Girdwood * @freq_out: requested PLL output clock frequency in Hz 26998c6529dbSLiam Girdwood * 27008c6529dbSLiam Girdwood * Configures and enables PLL to generate output clock based on input clock. 27018c6529dbSLiam Girdwood */ 270285488037SMark Brown int snd_soc_dai_set_pll(struct snd_soc_dai *dai, int pll_id, int source, 270385488037SMark Brown unsigned int freq_in, unsigned int freq_out) 27048c6529dbSLiam Girdwood { 2705f0fba2adSLiam Girdwood if (dai->driver && dai->driver->ops->set_pll) 2706f0fba2adSLiam Girdwood return dai->driver->ops->set_pll(dai, pll_id, source, 270785488037SMark Brown freq_in, freq_out); 27088c6529dbSLiam Girdwood else 27098c6529dbSLiam Girdwood return -EINVAL; 27108c6529dbSLiam Girdwood } 27118c6529dbSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_dai_set_pll); 27128c6529dbSLiam Girdwood 27138c6529dbSLiam Girdwood /** 27148c6529dbSLiam Girdwood * snd_soc_dai_set_fmt - configure DAI hardware audio format. 27158c6529dbSLiam Girdwood * @dai: DAI 27168c6529dbSLiam Girdwood * @fmt: SND_SOC_DAIFMT_ format value. 27178c6529dbSLiam Girdwood * 27188c6529dbSLiam Girdwood * Configures the DAI hardware format and clocking. 27198c6529dbSLiam Girdwood */ 27208c6529dbSLiam Girdwood int snd_soc_dai_set_fmt(struct snd_soc_dai *dai, unsigned int fmt) 27218c6529dbSLiam Girdwood { 2722f0fba2adSLiam Girdwood if (dai->driver && dai->driver->ops->set_fmt) 2723f0fba2adSLiam Girdwood return dai->driver->ops->set_fmt(dai, fmt); 27248c6529dbSLiam Girdwood else 27258c6529dbSLiam Girdwood return -EINVAL; 27268c6529dbSLiam Girdwood } 27278c6529dbSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_dai_set_fmt); 27288c6529dbSLiam Girdwood 27298c6529dbSLiam Girdwood /** 27308c6529dbSLiam Girdwood * snd_soc_dai_set_tdm_slot - configure DAI TDM. 27318c6529dbSLiam Girdwood * @dai: DAI 2732a5479e38SDaniel Ribeiro * @tx_mask: bitmask representing active TX slots. 2733a5479e38SDaniel Ribeiro * @rx_mask: bitmask representing active RX slots. 27348c6529dbSLiam Girdwood * @slots: Number of slots in use. 2735a5479e38SDaniel Ribeiro * @slot_width: Width in bits for each slot. 27368c6529dbSLiam Girdwood * 27378c6529dbSLiam Girdwood * Configures a DAI for TDM operation. Both mask and slots are codec and DAI 27388c6529dbSLiam Girdwood * specific. 27398c6529dbSLiam Girdwood */ 27408c6529dbSLiam Girdwood int snd_soc_dai_set_tdm_slot(struct snd_soc_dai *dai, 2741a5479e38SDaniel Ribeiro unsigned int tx_mask, unsigned int rx_mask, int slots, int slot_width) 27428c6529dbSLiam Girdwood { 2743f0fba2adSLiam Girdwood if (dai->driver && dai->driver->ops->set_tdm_slot) 2744f0fba2adSLiam Girdwood return dai->driver->ops->set_tdm_slot(dai, tx_mask, rx_mask, 2745a5479e38SDaniel Ribeiro slots, slot_width); 27468c6529dbSLiam Girdwood else 27478c6529dbSLiam Girdwood return -EINVAL; 27488c6529dbSLiam Girdwood } 27498c6529dbSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_dai_set_tdm_slot); 27508c6529dbSLiam Girdwood 27518c6529dbSLiam Girdwood /** 2752472df3cbSBarry Song * snd_soc_dai_set_channel_map - configure DAI audio channel map 2753472df3cbSBarry Song * @dai: DAI 2754472df3cbSBarry Song * @tx_num: how many TX channels 2755472df3cbSBarry Song * @tx_slot: pointer to an array which imply the TX slot number channel 2756472df3cbSBarry Song * 0~num-1 uses 2757472df3cbSBarry Song * @rx_num: how many RX channels 2758472df3cbSBarry Song * @rx_slot: pointer to an array which imply the RX slot number channel 2759472df3cbSBarry Song * 0~num-1 uses 2760472df3cbSBarry Song * 2761472df3cbSBarry Song * configure the relationship between channel number and TDM slot number. 2762472df3cbSBarry Song */ 2763472df3cbSBarry Song int snd_soc_dai_set_channel_map(struct snd_soc_dai *dai, 2764472df3cbSBarry Song unsigned int tx_num, unsigned int *tx_slot, 2765472df3cbSBarry Song unsigned int rx_num, unsigned int *rx_slot) 2766472df3cbSBarry Song { 2767f0fba2adSLiam Girdwood if (dai->driver && dai->driver->ops->set_channel_map) 2768f0fba2adSLiam Girdwood return dai->driver->ops->set_channel_map(dai, tx_num, tx_slot, 2769472df3cbSBarry Song rx_num, rx_slot); 2770472df3cbSBarry Song else 2771472df3cbSBarry Song return -EINVAL; 2772472df3cbSBarry Song } 2773472df3cbSBarry Song EXPORT_SYMBOL_GPL(snd_soc_dai_set_channel_map); 2774472df3cbSBarry Song 2775472df3cbSBarry Song /** 27768c6529dbSLiam Girdwood * snd_soc_dai_set_tristate - configure DAI system or master clock. 27778c6529dbSLiam Girdwood * @dai: DAI 27788c6529dbSLiam Girdwood * @tristate: tristate enable 27798c6529dbSLiam Girdwood * 27808c6529dbSLiam Girdwood * Tristates the DAI so that others can use it. 27818c6529dbSLiam Girdwood */ 27828c6529dbSLiam Girdwood int snd_soc_dai_set_tristate(struct snd_soc_dai *dai, int tristate) 27838c6529dbSLiam Girdwood { 2784f0fba2adSLiam Girdwood if (dai->driver && dai->driver->ops->set_tristate) 2785f0fba2adSLiam Girdwood return dai->driver->ops->set_tristate(dai, tristate); 27868c6529dbSLiam Girdwood else 27878c6529dbSLiam Girdwood return -EINVAL; 27888c6529dbSLiam Girdwood } 27898c6529dbSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_dai_set_tristate); 27908c6529dbSLiam Girdwood 27918c6529dbSLiam Girdwood /** 27928c6529dbSLiam Girdwood * snd_soc_dai_digital_mute - configure DAI system or master clock. 27938c6529dbSLiam Girdwood * @dai: DAI 27948c6529dbSLiam Girdwood * @mute: mute enable 27958c6529dbSLiam Girdwood * 27968c6529dbSLiam Girdwood * Mutes the DAI DAC. 27978c6529dbSLiam Girdwood */ 27988c6529dbSLiam Girdwood int snd_soc_dai_digital_mute(struct snd_soc_dai *dai, int mute) 27998c6529dbSLiam Girdwood { 2800f0fba2adSLiam Girdwood if (dai->driver && dai->driver->ops->digital_mute) 2801f0fba2adSLiam Girdwood return dai->driver->ops->digital_mute(dai, mute); 28028c6529dbSLiam Girdwood else 28038c6529dbSLiam Girdwood return -EINVAL; 28048c6529dbSLiam Girdwood } 28058c6529dbSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_dai_digital_mute); 28068c6529dbSLiam Girdwood 2807c5af3a2eSMark Brown /** 2808c5af3a2eSMark Brown * snd_soc_register_card - Register a card with the ASoC core 2809c5af3a2eSMark Brown * 2810ac11a2b3SMark Brown * @card: Card to register 2811c5af3a2eSMark Brown * 2812c5af3a2eSMark Brown * Note that currently this is an internal only function: it will be 2813c5af3a2eSMark Brown * exposed to machine drivers after further backporting of ASoC v2 2814c5af3a2eSMark Brown * registration APIs. 2815c5af3a2eSMark Brown */ 2816c5af3a2eSMark Brown static int snd_soc_register_card(struct snd_soc_card *card) 2817c5af3a2eSMark Brown { 2818f0fba2adSLiam Girdwood int i; 2819f0fba2adSLiam Girdwood 2820c5af3a2eSMark Brown if (!card->name || !card->dev) 2821c5af3a2eSMark Brown return -EINVAL; 2822c5af3a2eSMark Brown 2823f0fba2adSLiam Girdwood card->rtd = kzalloc(sizeof(struct snd_soc_pcm_runtime) * card->num_links, 2824f0fba2adSLiam Girdwood GFP_KERNEL); 2825f0fba2adSLiam Girdwood if (card->rtd == NULL) 2826f0fba2adSLiam Girdwood return -ENOMEM; 2827f0fba2adSLiam Girdwood 2828f0fba2adSLiam Girdwood for (i = 0; i < card->num_links; i++) 2829f0fba2adSLiam Girdwood card->rtd[i].dai_link = &card->dai_link[i]; 2830f0fba2adSLiam Girdwood 2831c5af3a2eSMark Brown INIT_LIST_HEAD(&card->list); 2832c5af3a2eSMark Brown card->instantiated = 0; 2833f0fba2adSLiam Girdwood mutex_init(&card->mutex); 2834c5af3a2eSMark Brown 2835c5af3a2eSMark Brown mutex_lock(&client_mutex); 2836c5af3a2eSMark Brown list_add(&card->list, &card_list); 2837435c5e25SMark Brown snd_soc_instantiate_cards(); 2838c5af3a2eSMark Brown mutex_unlock(&client_mutex); 2839c5af3a2eSMark Brown 2840c5af3a2eSMark Brown dev_dbg(card->dev, "Registered card '%s'\n", card->name); 2841c5af3a2eSMark Brown 2842c5af3a2eSMark Brown return 0; 2843c5af3a2eSMark Brown } 2844c5af3a2eSMark Brown 2845c5af3a2eSMark Brown /** 2846c5af3a2eSMark Brown * snd_soc_unregister_card - Unregister a card with the ASoC core 2847c5af3a2eSMark Brown * 2848ac11a2b3SMark Brown * @card: Card to unregister 2849c5af3a2eSMark Brown * 2850c5af3a2eSMark Brown * Note that currently this is an internal only function: it will be 2851c5af3a2eSMark Brown * exposed to machine drivers after further backporting of ASoC v2 2852c5af3a2eSMark Brown * registration APIs. 2853c5af3a2eSMark Brown */ 2854c5af3a2eSMark Brown static int snd_soc_unregister_card(struct snd_soc_card *card) 2855c5af3a2eSMark Brown { 2856c5af3a2eSMark Brown mutex_lock(&client_mutex); 2857c5af3a2eSMark Brown list_del(&card->list); 2858c5af3a2eSMark Brown mutex_unlock(&client_mutex); 2859c5af3a2eSMark Brown dev_dbg(card->dev, "Unregistered card '%s'\n", card->name); 2860c5af3a2eSMark Brown 2861c5af3a2eSMark Brown return 0; 2862c5af3a2eSMark Brown } 2863c5af3a2eSMark Brown 2864f0fba2adSLiam Girdwood /* 2865f0fba2adSLiam Girdwood * Simplify DAI link configuration by removing ".-1" from device names 2866f0fba2adSLiam Girdwood * and sanitizing names. 2867f0fba2adSLiam Girdwood */ 2868f0fba2adSLiam Girdwood static inline char *fmt_single_name(struct device *dev, int *id) 2869f0fba2adSLiam Girdwood { 2870f0fba2adSLiam Girdwood char *found, name[NAME_SIZE]; 2871f0fba2adSLiam Girdwood int id1, id2; 2872f0fba2adSLiam Girdwood 2873f0fba2adSLiam Girdwood if (dev_name(dev) == NULL) 2874f0fba2adSLiam Girdwood return NULL; 2875f0fba2adSLiam Girdwood 2876f0fba2adSLiam Girdwood strncpy(name, dev_name(dev), NAME_SIZE); 2877f0fba2adSLiam Girdwood 2878f0fba2adSLiam Girdwood /* are we a "%s.%d" name (platform and SPI components) */ 2879f0fba2adSLiam Girdwood found = strstr(name, dev->driver->name); 2880f0fba2adSLiam Girdwood if (found) { 2881f0fba2adSLiam Girdwood /* get ID */ 2882f0fba2adSLiam Girdwood if (sscanf(&found[strlen(dev->driver->name)], ".%d", id) == 1) { 2883f0fba2adSLiam Girdwood 2884f0fba2adSLiam Girdwood /* discard ID from name if ID == -1 */ 2885f0fba2adSLiam Girdwood if (*id == -1) 2886f0fba2adSLiam Girdwood found[strlen(dev->driver->name)] = '\0'; 2887f0fba2adSLiam Girdwood } 2888f0fba2adSLiam Girdwood 2889f0fba2adSLiam Girdwood } else { 2890f0fba2adSLiam Girdwood /* I2C component devices are named "bus-addr" */ 2891f0fba2adSLiam Girdwood if (sscanf(name, "%x-%x", &id1, &id2) == 2) { 2892f0fba2adSLiam Girdwood char tmp[NAME_SIZE]; 2893f0fba2adSLiam Girdwood 2894f0fba2adSLiam Girdwood /* create unique ID number from I2C addr and bus */ 2895f0fba2adSLiam Girdwood *id = ((id1 && 0xffff) << 16) + id2; 2896f0fba2adSLiam Girdwood 2897f0fba2adSLiam Girdwood /* sanitize component name for DAI link creation */ 2898f0fba2adSLiam Girdwood snprintf(tmp, NAME_SIZE, "%s.%s", dev->driver->name, name); 2899f0fba2adSLiam Girdwood strncpy(name, tmp, NAME_SIZE); 2900f0fba2adSLiam Girdwood } else 2901f0fba2adSLiam Girdwood *id = 0; 2902f0fba2adSLiam Girdwood } 2903f0fba2adSLiam Girdwood 2904f0fba2adSLiam Girdwood return kstrdup(name, GFP_KERNEL); 2905f0fba2adSLiam Girdwood } 2906f0fba2adSLiam Girdwood 2907f0fba2adSLiam Girdwood /* 2908f0fba2adSLiam Girdwood * Simplify DAI link naming for single devices with multiple DAIs by removing 2909f0fba2adSLiam Girdwood * any ".-1" and using the DAI name (instead of device name). 2910f0fba2adSLiam Girdwood */ 2911f0fba2adSLiam Girdwood static inline char *fmt_multiple_name(struct device *dev, 2912f0fba2adSLiam Girdwood struct snd_soc_dai_driver *dai_drv) 2913f0fba2adSLiam Girdwood { 2914f0fba2adSLiam Girdwood if (dai_drv->name == NULL) { 2915f0fba2adSLiam Girdwood printk(KERN_ERR "asoc: error - multiple DAI %s registered with no name\n", 2916f0fba2adSLiam Girdwood dev_name(dev)); 2917f0fba2adSLiam Girdwood return NULL; 2918f0fba2adSLiam Girdwood } 2919f0fba2adSLiam Girdwood 2920f0fba2adSLiam Girdwood return kstrdup(dai_drv->name, GFP_KERNEL); 2921f0fba2adSLiam Girdwood } 2922f0fba2adSLiam Girdwood 29239115171aSMark Brown /** 29249115171aSMark Brown * snd_soc_register_dai - Register a DAI with the ASoC core 29259115171aSMark Brown * 2926ac11a2b3SMark Brown * @dai: DAI to register 29279115171aSMark Brown */ 2928f0fba2adSLiam Girdwood int snd_soc_register_dai(struct device *dev, 2929f0fba2adSLiam Girdwood struct snd_soc_dai_driver *dai_drv) 29309115171aSMark Brown { 2931f0fba2adSLiam Girdwood struct snd_soc_dai *dai; 29329115171aSMark Brown 2933f0fba2adSLiam Girdwood dev_dbg(dev, "dai register %s\n", dev_name(dev)); 29349115171aSMark Brown 2935f0fba2adSLiam Girdwood dai = kzalloc(sizeof(struct snd_soc_dai), GFP_KERNEL); 2936f0fba2adSLiam Girdwood if (dai == NULL) 2937f0fba2adSLiam Girdwood return -ENOMEM; 29386335d055SEric Miao 2939f0fba2adSLiam Girdwood /* create DAI component name */ 2940f0fba2adSLiam Girdwood dai->name = fmt_single_name(dev, &dai->id); 2941f0fba2adSLiam Girdwood if (dai->name == NULL) { 2942f0fba2adSLiam Girdwood kfree(dai); 2943f0fba2adSLiam Girdwood return -ENOMEM; 2944f0fba2adSLiam Girdwood } 2945f0fba2adSLiam Girdwood 2946f0fba2adSLiam Girdwood dai->dev = dev; 2947f0fba2adSLiam Girdwood dai->driver = dai_drv; 2948f0fba2adSLiam Girdwood if (!dai->driver->ops) 2949f0fba2adSLiam Girdwood dai->driver->ops = &null_dai_ops; 29509115171aSMark Brown 29519115171aSMark Brown mutex_lock(&client_mutex); 29529115171aSMark Brown list_add(&dai->list, &dai_list); 2953435c5e25SMark Brown snd_soc_instantiate_cards(); 29549115171aSMark Brown mutex_unlock(&client_mutex); 29559115171aSMark Brown 29569115171aSMark Brown pr_debug("Registered DAI '%s'\n", dai->name); 29579115171aSMark Brown 29589115171aSMark Brown return 0; 29599115171aSMark Brown } 29609115171aSMark Brown EXPORT_SYMBOL_GPL(snd_soc_register_dai); 29619115171aSMark Brown 29629115171aSMark Brown /** 29639115171aSMark Brown * snd_soc_unregister_dai - Unregister a DAI from the ASoC core 29649115171aSMark Brown * 2965ac11a2b3SMark Brown * @dai: DAI to unregister 29669115171aSMark Brown */ 2967f0fba2adSLiam Girdwood void snd_soc_unregister_dai(struct device *dev) 29689115171aSMark Brown { 2969f0fba2adSLiam Girdwood struct snd_soc_dai *dai; 2970f0fba2adSLiam Girdwood 2971f0fba2adSLiam Girdwood list_for_each_entry(dai, &dai_list, list) { 2972f0fba2adSLiam Girdwood if (dev == dai->dev) 2973f0fba2adSLiam Girdwood goto found; 2974f0fba2adSLiam Girdwood } 2975f0fba2adSLiam Girdwood return; 2976f0fba2adSLiam Girdwood 2977f0fba2adSLiam Girdwood found: 29789115171aSMark Brown mutex_lock(&client_mutex); 29799115171aSMark Brown list_del(&dai->list); 29809115171aSMark Brown mutex_unlock(&client_mutex); 29819115171aSMark Brown 29829115171aSMark Brown pr_debug("Unregistered DAI '%s'\n", dai->name); 2983f0fba2adSLiam Girdwood kfree(dai->name); 2984f0fba2adSLiam Girdwood kfree(dai); 29859115171aSMark Brown } 29869115171aSMark Brown EXPORT_SYMBOL_GPL(snd_soc_unregister_dai); 29879115171aSMark Brown 29889115171aSMark Brown /** 29899115171aSMark Brown * snd_soc_register_dais - Register multiple DAIs with the ASoC core 29909115171aSMark Brown * 2991ac11a2b3SMark Brown * @dai: Array of DAIs to register 2992ac11a2b3SMark Brown * @count: Number of DAIs 29939115171aSMark Brown */ 2994f0fba2adSLiam Girdwood int snd_soc_register_dais(struct device *dev, 2995f0fba2adSLiam Girdwood struct snd_soc_dai_driver *dai_drv, size_t count) 29969115171aSMark Brown { 2997f0fba2adSLiam Girdwood struct snd_soc_dai *dai; 2998f0fba2adSLiam Girdwood int i, ret = 0; 2999f0fba2adSLiam Girdwood 3000720ffa4cSLiam Girdwood dev_dbg(dev, "dai register %s #%Zu\n", dev_name(dev), count); 30019115171aSMark Brown 30029115171aSMark Brown for (i = 0; i < count; i++) { 3003f0fba2adSLiam Girdwood 3004f0fba2adSLiam Girdwood dai = kzalloc(sizeof(struct snd_soc_dai), GFP_KERNEL); 3005f0fba2adSLiam Girdwood if (dai == NULL) 3006f0fba2adSLiam Girdwood return -ENOMEM; 3007f0fba2adSLiam Girdwood 3008f0fba2adSLiam Girdwood /* create DAI component name */ 3009f0fba2adSLiam Girdwood dai->name = fmt_multiple_name(dev, &dai_drv[i]); 3010f0fba2adSLiam Girdwood if (dai->name == NULL) { 3011f0fba2adSLiam Girdwood kfree(dai); 3012f0fba2adSLiam Girdwood ret = -EINVAL; 30139115171aSMark Brown goto err; 30149115171aSMark Brown } 30159115171aSMark Brown 3016f0fba2adSLiam Girdwood dai->dev = dev; 3017f0fba2adSLiam Girdwood dai->id = i; 3018f0fba2adSLiam Girdwood dai->driver = &dai_drv[i]; 3019f0fba2adSLiam Girdwood if (!dai->driver->ops) 3020f0fba2adSLiam Girdwood dai->driver->ops = &null_dai_ops; 3021f0fba2adSLiam Girdwood 3022f0fba2adSLiam Girdwood mutex_lock(&client_mutex); 3023f0fba2adSLiam Girdwood list_add(&dai->list, &dai_list); 3024f0fba2adSLiam Girdwood mutex_unlock(&client_mutex); 3025f0fba2adSLiam Girdwood 3026f0fba2adSLiam Girdwood pr_debug("Registered DAI '%s'\n", dai->name); 3027f0fba2adSLiam Girdwood } 3028f0fba2adSLiam Girdwood 3029f0fba2adSLiam Girdwood snd_soc_instantiate_cards(); 30309115171aSMark Brown return 0; 30319115171aSMark Brown 30329115171aSMark Brown err: 30339115171aSMark Brown for (i--; i >= 0; i--) 3034f0fba2adSLiam Girdwood snd_soc_unregister_dai(dev); 30359115171aSMark Brown 30369115171aSMark Brown return ret; 30379115171aSMark Brown } 30389115171aSMark Brown EXPORT_SYMBOL_GPL(snd_soc_register_dais); 30399115171aSMark Brown 30409115171aSMark Brown /** 30419115171aSMark Brown * snd_soc_unregister_dais - Unregister multiple DAIs from the ASoC core 30429115171aSMark Brown * 3043ac11a2b3SMark Brown * @dai: Array of DAIs to unregister 3044ac11a2b3SMark Brown * @count: Number of DAIs 30459115171aSMark Brown */ 3046f0fba2adSLiam Girdwood void snd_soc_unregister_dais(struct device *dev, size_t count) 30479115171aSMark Brown { 30489115171aSMark Brown int i; 30499115171aSMark Brown 30509115171aSMark Brown for (i = 0; i < count; i++) 3051f0fba2adSLiam Girdwood snd_soc_unregister_dai(dev); 30529115171aSMark Brown } 30539115171aSMark Brown EXPORT_SYMBOL_GPL(snd_soc_unregister_dais); 30549115171aSMark Brown 305512a48a8cSMark Brown /** 305612a48a8cSMark Brown * snd_soc_register_platform - Register a platform with the ASoC core 305712a48a8cSMark Brown * 3058ac11a2b3SMark Brown * @platform: platform to register 305912a48a8cSMark Brown */ 3060f0fba2adSLiam Girdwood int snd_soc_register_platform(struct device *dev, 3061f0fba2adSLiam Girdwood struct snd_soc_platform_driver *platform_drv) 306212a48a8cSMark Brown { 3063f0fba2adSLiam Girdwood struct snd_soc_platform *platform; 306412a48a8cSMark Brown 3065f0fba2adSLiam Girdwood dev_dbg(dev, "platform register %s\n", dev_name(dev)); 3066f0fba2adSLiam Girdwood 3067f0fba2adSLiam Girdwood platform = kzalloc(sizeof(struct snd_soc_platform), GFP_KERNEL); 3068f0fba2adSLiam Girdwood if (platform == NULL) 3069f0fba2adSLiam Girdwood return -ENOMEM; 3070f0fba2adSLiam Girdwood 3071f0fba2adSLiam Girdwood /* create platform component name */ 3072f0fba2adSLiam Girdwood platform->name = fmt_single_name(dev, &platform->id); 3073f0fba2adSLiam Girdwood if (platform->name == NULL) { 3074f0fba2adSLiam Girdwood kfree(platform); 3075f0fba2adSLiam Girdwood return -ENOMEM; 3076f0fba2adSLiam Girdwood } 3077f0fba2adSLiam Girdwood 3078f0fba2adSLiam Girdwood platform->dev = dev; 3079f0fba2adSLiam Girdwood platform->driver = platform_drv; 308012a48a8cSMark Brown 308112a48a8cSMark Brown mutex_lock(&client_mutex); 308212a48a8cSMark Brown list_add(&platform->list, &platform_list); 3083435c5e25SMark Brown snd_soc_instantiate_cards(); 308412a48a8cSMark Brown mutex_unlock(&client_mutex); 308512a48a8cSMark Brown 308612a48a8cSMark Brown pr_debug("Registered platform '%s'\n", platform->name); 308712a48a8cSMark Brown 308812a48a8cSMark Brown return 0; 308912a48a8cSMark Brown } 309012a48a8cSMark Brown EXPORT_SYMBOL_GPL(snd_soc_register_platform); 309112a48a8cSMark Brown 309212a48a8cSMark Brown /** 309312a48a8cSMark Brown * snd_soc_unregister_platform - Unregister a platform from the ASoC core 309412a48a8cSMark Brown * 3095ac11a2b3SMark Brown * @platform: platform to unregister 309612a48a8cSMark Brown */ 3097f0fba2adSLiam Girdwood void snd_soc_unregister_platform(struct device *dev) 309812a48a8cSMark Brown { 3099f0fba2adSLiam Girdwood struct snd_soc_platform *platform; 3100f0fba2adSLiam Girdwood 3101f0fba2adSLiam Girdwood list_for_each_entry(platform, &platform_list, list) { 3102f0fba2adSLiam Girdwood if (dev == platform->dev) 3103f0fba2adSLiam Girdwood goto found; 3104f0fba2adSLiam Girdwood } 3105f0fba2adSLiam Girdwood return; 3106f0fba2adSLiam Girdwood 3107f0fba2adSLiam Girdwood found: 310812a48a8cSMark Brown mutex_lock(&client_mutex); 310912a48a8cSMark Brown list_del(&platform->list); 311012a48a8cSMark Brown mutex_unlock(&client_mutex); 311112a48a8cSMark Brown 311212a48a8cSMark Brown pr_debug("Unregistered platform '%s'\n", platform->name); 3113f0fba2adSLiam Girdwood kfree(platform->name); 3114f0fba2adSLiam Girdwood kfree(platform); 311512a48a8cSMark Brown } 311612a48a8cSMark Brown EXPORT_SYMBOL_GPL(snd_soc_unregister_platform); 311712a48a8cSMark Brown 3118151ab22cSMark Brown static u64 codec_format_map[] = { 3119151ab22cSMark Brown SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S16_BE, 3120151ab22cSMark Brown SNDRV_PCM_FMTBIT_U16_LE | SNDRV_PCM_FMTBIT_U16_BE, 3121151ab22cSMark Brown SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S24_BE, 3122151ab22cSMark Brown SNDRV_PCM_FMTBIT_U24_LE | SNDRV_PCM_FMTBIT_U24_BE, 3123151ab22cSMark Brown SNDRV_PCM_FMTBIT_S32_LE | SNDRV_PCM_FMTBIT_S32_BE, 3124151ab22cSMark Brown SNDRV_PCM_FMTBIT_U32_LE | SNDRV_PCM_FMTBIT_U32_BE, 3125151ab22cSMark Brown SNDRV_PCM_FMTBIT_S24_3LE | SNDRV_PCM_FMTBIT_U24_3BE, 3126151ab22cSMark Brown SNDRV_PCM_FMTBIT_U24_3LE | SNDRV_PCM_FMTBIT_U24_3BE, 3127151ab22cSMark Brown SNDRV_PCM_FMTBIT_S20_3LE | SNDRV_PCM_FMTBIT_S20_3BE, 3128151ab22cSMark Brown SNDRV_PCM_FMTBIT_U20_3LE | SNDRV_PCM_FMTBIT_U20_3BE, 3129151ab22cSMark Brown SNDRV_PCM_FMTBIT_S18_3LE | SNDRV_PCM_FMTBIT_S18_3BE, 3130151ab22cSMark Brown SNDRV_PCM_FMTBIT_U18_3LE | SNDRV_PCM_FMTBIT_U18_3BE, 3131151ab22cSMark Brown SNDRV_PCM_FMTBIT_FLOAT_LE | SNDRV_PCM_FMTBIT_FLOAT_BE, 3132151ab22cSMark Brown SNDRV_PCM_FMTBIT_FLOAT64_LE | SNDRV_PCM_FMTBIT_FLOAT64_BE, 3133151ab22cSMark Brown SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_LE 3134151ab22cSMark Brown | SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_BE, 3135151ab22cSMark Brown }; 3136151ab22cSMark Brown 3137151ab22cSMark Brown /* Fix up the DAI formats for endianness: codecs don't actually see 3138151ab22cSMark Brown * the endianness of the data but we're using the CPU format 3139151ab22cSMark Brown * definitions which do need to include endianness so we ensure that 3140151ab22cSMark Brown * codec DAIs always have both big and little endian variants set. 3141151ab22cSMark Brown */ 3142151ab22cSMark Brown static void fixup_codec_formats(struct snd_soc_pcm_stream *stream) 3143151ab22cSMark Brown { 3144151ab22cSMark Brown int i; 3145151ab22cSMark Brown 3146151ab22cSMark Brown for (i = 0; i < ARRAY_SIZE(codec_format_map); i++) 3147151ab22cSMark Brown if (stream->formats & codec_format_map[i]) 3148151ab22cSMark Brown stream->formats |= codec_format_map[i]; 3149151ab22cSMark Brown } 3150151ab22cSMark Brown 31510d0cf00aSMark Brown /** 31520d0cf00aSMark Brown * snd_soc_register_codec - Register a codec with the ASoC core 31530d0cf00aSMark Brown * 3154ac11a2b3SMark Brown * @codec: codec to register 31550d0cf00aSMark Brown */ 3156f0fba2adSLiam Girdwood int snd_soc_register_codec(struct device *dev, 3157f0fba2adSLiam Girdwood struct snd_soc_codec_driver *codec_drv, 3158f0fba2adSLiam Girdwood struct snd_soc_dai_driver *dai_drv, int num_dai) 31590d0cf00aSMark Brown { 3160f0fba2adSLiam Girdwood struct snd_soc_codec *codec; 3161f0fba2adSLiam Girdwood int ret, i; 3162151ab22cSMark Brown 3163f0fba2adSLiam Girdwood dev_dbg(dev, "codec register %s\n", dev_name(dev)); 31640d0cf00aSMark Brown 3165f0fba2adSLiam Girdwood codec = kzalloc(sizeof(struct snd_soc_codec), GFP_KERNEL); 3166f0fba2adSLiam Girdwood if (codec == NULL) 3167f0fba2adSLiam Girdwood return -ENOMEM; 31680d0cf00aSMark Brown 3169f0fba2adSLiam Girdwood /* create CODEC component name */ 3170f0fba2adSLiam Girdwood codec->name = fmt_single_name(dev, &codec->id); 3171f0fba2adSLiam Girdwood if (codec->name == NULL) { 3172f0fba2adSLiam Girdwood kfree(codec); 3173f0fba2adSLiam Girdwood return -ENOMEM; 3174151ab22cSMark Brown } 3175151ab22cSMark Brown 3176f0fba2adSLiam Girdwood /* allocate CODEC register cache */ 3177f0fba2adSLiam Girdwood if (codec_drv->reg_cache_size && codec_drv->reg_word_size) { 3178f0fba2adSLiam Girdwood 3179f0fba2adSLiam Girdwood if (codec_drv->reg_cache_default) 3180f0fba2adSLiam Girdwood codec->reg_cache = kmemdup(codec_drv->reg_cache_default, 3181f0fba2adSLiam Girdwood codec_drv->reg_cache_size * codec_drv->reg_word_size, GFP_KERNEL); 3182f0fba2adSLiam Girdwood else 3183f0fba2adSLiam Girdwood codec->reg_cache = kzalloc(codec_drv->reg_cache_size * 3184f0fba2adSLiam Girdwood codec_drv->reg_word_size, GFP_KERNEL); 3185f0fba2adSLiam Girdwood 3186f0fba2adSLiam Girdwood if (codec->reg_cache == NULL) { 3187f0fba2adSLiam Girdwood kfree(codec->name); 3188f0fba2adSLiam Girdwood kfree(codec); 3189f0fba2adSLiam Girdwood return -ENOMEM; 3190f0fba2adSLiam Girdwood } 3191f0fba2adSLiam Girdwood } 3192f0fba2adSLiam Girdwood 3193f0fba2adSLiam Girdwood codec->dev = dev; 3194f0fba2adSLiam Girdwood codec->driver = codec_drv; 3195f0fba2adSLiam Girdwood codec->bias_level = SND_SOC_BIAS_OFF; 3196f0fba2adSLiam Girdwood codec->num_dai = num_dai; 3197f0fba2adSLiam Girdwood mutex_init(&codec->mutex); 3198f0fba2adSLiam Girdwood INIT_LIST_HEAD(&codec->dapm_widgets); 3199f0fba2adSLiam Girdwood INIT_LIST_HEAD(&codec->dapm_paths); 3200f0fba2adSLiam Girdwood 3201f0fba2adSLiam Girdwood for (i = 0; i < num_dai; i++) { 3202f0fba2adSLiam Girdwood fixup_codec_formats(&dai_drv[i].playback); 3203f0fba2adSLiam Girdwood fixup_codec_formats(&dai_drv[i].capture); 3204f0fba2adSLiam Girdwood } 3205f0fba2adSLiam Girdwood 320626b01ccdSMark Brown /* register any DAIs */ 320726b01ccdSMark Brown if (num_dai) { 3208f0fba2adSLiam Girdwood ret = snd_soc_register_dais(dev, dai_drv, num_dai); 3209f0fba2adSLiam Girdwood if (ret < 0) 3210f0fba2adSLiam Girdwood goto error; 321126b01ccdSMark Brown } 3212f0fba2adSLiam Girdwood 32130d0cf00aSMark Brown mutex_lock(&client_mutex); 32140d0cf00aSMark Brown list_add(&codec->list, &codec_list); 32150d0cf00aSMark Brown snd_soc_instantiate_cards(); 32160d0cf00aSMark Brown mutex_unlock(&client_mutex); 32170d0cf00aSMark Brown 32180d0cf00aSMark Brown pr_debug("Registered codec '%s'\n", codec->name); 32190d0cf00aSMark Brown return 0; 3220f0fba2adSLiam Girdwood 3221f0fba2adSLiam Girdwood error: 3222f0fba2adSLiam Girdwood for (i--; i >= 0; i--) 3223f0fba2adSLiam Girdwood snd_soc_unregister_dai(dev); 3224f0fba2adSLiam Girdwood 3225f0fba2adSLiam Girdwood if (codec->reg_cache) 3226f0fba2adSLiam Girdwood kfree(codec->reg_cache); 3227f0fba2adSLiam Girdwood kfree(codec->name); 3228f0fba2adSLiam Girdwood kfree(codec); 3229f0fba2adSLiam Girdwood return ret; 32300d0cf00aSMark Brown } 32310d0cf00aSMark Brown EXPORT_SYMBOL_GPL(snd_soc_register_codec); 32320d0cf00aSMark Brown 32330d0cf00aSMark Brown /** 32340d0cf00aSMark Brown * snd_soc_unregister_codec - Unregister a codec from the ASoC core 32350d0cf00aSMark Brown * 3236ac11a2b3SMark Brown * @codec: codec to unregister 32370d0cf00aSMark Brown */ 3238f0fba2adSLiam Girdwood void snd_soc_unregister_codec(struct device *dev) 32390d0cf00aSMark Brown { 3240f0fba2adSLiam Girdwood struct snd_soc_codec *codec; 3241f0fba2adSLiam Girdwood int i; 3242f0fba2adSLiam Girdwood 3243f0fba2adSLiam Girdwood list_for_each_entry(codec, &codec_list, list) { 3244f0fba2adSLiam Girdwood if (dev == codec->dev) 3245f0fba2adSLiam Girdwood goto found; 3246f0fba2adSLiam Girdwood } 3247f0fba2adSLiam Girdwood return; 3248f0fba2adSLiam Girdwood 3249f0fba2adSLiam Girdwood found: 325026b01ccdSMark Brown if (codec->num_dai) 3251f0fba2adSLiam Girdwood for (i = 0; i < codec->num_dai; i++) 3252f0fba2adSLiam Girdwood snd_soc_unregister_dai(dev); 3253f0fba2adSLiam Girdwood 32540d0cf00aSMark Brown mutex_lock(&client_mutex); 32550d0cf00aSMark Brown list_del(&codec->list); 32560d0cf00aSMark Brown mutex_unlock(&client_mutex); 32570d0cf00aSMark Brown 32580d0cf00aSMark Brown pr_debug("Unregistered codec '%s'\n", codec->name); 3259f0fba2adSLiam Girdwood 3260f0fba2adSLiam Girdwood if (codec->reg_cache) 3261f0fba2adSLiam Girdwood kfree(codec->reg_cache); 3262f0fba2adSLiam Girdwood kfree(codec); 32630d0cf00aSMark Brown } 32640d0cf00aSMark Brown EXPORT_SYMBOL_GPL(snd_soc_unregister_codec); 32650d0cf00aSMark Brown 3266c9b3a40fSTakashi Iwai static int __init snd_soc_init(void) 3267db2a4165SFrank Mandarino { 3268384c89e2SMark Brown #ifdef CONFIG_DEBUG_FS 3269384c89e2SMark Brown debugfs_root = debugfs_create_dir("asoc", NULL); 3270384c89e2SMark Brown if (IS_ERR(debugfs_root) || !debugfs_root) { 3271384c89e2SMark Brown printk(KERN_WARNING 3272384c89e2SMark Brown "ASoC: Failed to create debugfs directory\n"); 3273384c89e2SMark Brown debugfs_root = NULL; 3274384c89e2SMark Brown } 3275c3c5a19aSMark Brown 3276c3c5a19aSMark Brown if (!debugfs_create_file("codecs", 0444, debugfs_root, NULL, 3277c3c5a19aSMark Brown &codec_list_fops)) 3278c3c5a19aSMark Brown pr_warn("ASoC: Failed to create CODEC list debugfs file\n"); 3279c3c5a19aSMark Brown 3280f3208780SMark Brown if (!debugfs_create_file("dais", 0444, debugfs_root, NULL, 3281f3208780SMark Brown &dai_list_fops)) 3282f3208780SMark Brown pr_warn("ASoC: Failed to create DAI list debugfs file\n"); 328319c7ac27SMark Brown 328419c7ac27SMark Brown if (!debugfs_create_file("platforms", 0444, debugfs_root, NULL, 328519c7ac27SMark Brown &platform_list_fops)) 328619c7ac27SMark Brown pr_warn("ASoC: Failed to create platform list debugfs file\n"); 3287384c89e2SMark Brown #endif 3288384c89e2SMark Brown 3289db2a4165SFrank Mandarino return platform_driver_register(&soc_driver); 3290db2a4165SFrank Mandarino } 3291db2a4165SFrank Mandarino 32927d8c16a6SMark Brown static void __exit snd_soc_exit(void) 3293db2a4165SFrank Mandarino { 3294384c89e2SMark Brown #ifdef CONFIG_DEBUG_FS 3295384c89e2SMark Brown debugfs_remove_recursive(debugfs_root); 3296384c89e2SMark Brown #endif 3297db2a4165SFrank Mandarino platform_driver_unregister(&soc_driver); 3298db2a4165SFrank Mandarino } 3299db2a4165SFrank Mandarino 3300db2a4165SFrank Mandarino module_init(snd_soc_init); 3301db2a4165SFrank Mandarino module_exit(snd_soc_exit); 3302db2a4165SFrank Mandarino 3303db2a4165SFrank Mandarino /* Module information */ 3304d331124dSLiam Girdwood MODULE_AUTHOR("Liam Girdwood, lrg@slimlogic.co.uk"); 3305db2a4165SFrank Mandarino MODULE_DESCRIPTION("ALSA SoC Core"); 3306db2a4165SFrank Mandarino MODULE_LICENSE("GPL"); 33078b45a209SKay Sievers MODULE_ALIAS("platform:soc-audio"); 3308