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. 60664d888SLiam Girdwood * 7d331124dSLiam Girdwood * Author: Liam Girdwood <lrg@slimlogic.co.uk> 80664d888SLiam Girdwood * with code, comments and ideas from :- 90664d888SLiam Girdwood * Richard Purdie <richard@openedhand.com> 10db2a4165SFrank Mandarino * 11db2a4165SFrank Mandarino * This program is free software; you can redistribute it and/or modify it 12db2a4165SFrank Mandarino * under the terms of the GNU General Public License as published by the 13db2a4165SFrank Mandarino * Free Software Foundation; either version 2 of the License, or (at your 14db2a4165SFrank Mandarino * option) any later version. 15db2a4165SFrank Mandarino * 16db2a4165SFrank Mandarino * TODO: 17db2a4165SFrank Mandarino * o Add hw rules to enforce rates, etc. 18db2a4165SFrank Mandarino * o More testing with other codecs/machines. 19db2a4165SFrank Mandarino * o Add more codecs and platforms to ensure good API coverage. 20db2a4165SFrank Mandarino * o Support TDM on PCM and I2S 21db2a4165SFrank Mandarino */ 22db2a4165SFrank Mandarino 23db2a4165SFrank Mandarino #include <linux/module.h> 24db2a4165SFrank Mandarino #include <linux/moduleparam.h> 25db2a4165SFrank Mandarino #include <linux/init.h> 26db2a4165SFrank Mandarino #include <linux/delay.h> 27db2a4165SFrank Mandarino #include <linux/pm.h> 28db2a4165SFrank Mandarino #include <linux/bitops.h> 2912ef193dSTroy Kisky #include <linux/debugfs.h> 30db2a4165SFrank Mandarino #include <linux/platform_device.h> 315a0e3ad6STejun Heo #include <linux/slab.h> 32474828a4SMarek Vasut #include <sound/ac97_codec.h> 33db2a4165SFrank Mandarino #include <sound/core.h> 34db2a4165SFrank Mandarino #include <sound/pcm.h> 35db2a4165SFrank Mandarino #include <sound/pcm_params.h> 36db2a4165SFrank Mandarino #include <sound/soc.h> 37db2a4165SFrank Mandarino #include <sound/soc-dapm.h> 38db2a4165SFrank Mandarino #include <sound/initval.h> 39db2a4165SFrank Mandarino 40db2a4165SFrank Mandarino static DEFINE_MUTEX(pcm_mutex); 41db2a4165SFrank Mandarino static DECLARE_WAIT_QUEUE_HEAD(soc_pm_waitq); 42db2a4165SFrank Mandarino 43384c89e2SMark Brown #ifdef CONFIG_DEBUG_FS 44384c89e2SMark Brown static struct dentry *debugfs_root; 45384c89e2SMark Brown #endif 46384c89e2SMark Brown 47c5af3a2eSMark Brown static DEFINE_MUTEX(client_mutex); 48c5af3a2eSMark Brown static LIST_HEAD(card_list); 499115171aSMark Brown static LIST_HEAD(dai_list); 5012a48a8cSMark Brown static LIST_HEAD(platform_list); 510d0cf00aSMark Brown static LIST_HEAD(codec_list); 52c5af3a2eSMark Brown 53c5af3a2eSMark Brown static int snd_soc_register_card(struct snd_soc_card *card); 54c5af3a2eSMark Brown static int snd_soc_unregister_card(struct snd_soc_card *card); 55c5af3a2eSMark Brown 56db2a4165SFrank Mandarino /* 57db2a4165SFrank Mandarino * This is a timeout to do a DAPM powerdown after a stream is closed(). 58db2a4165SFrank Mandarino * It can be used to eliminate pops between different playback streams, e.g. 59db2a4165SFrank Mandarino * between two audio tracks. 60db2a4165SFrank Mandarino */ 61db2a4165SFrank Mandarino static int pmdown_time = 5000; 62db2a4165SFrank Mandarino module_param(pmdown_time, int, 0); 63db2a4165SFrank Mandarino MODULE_PARM_DESC(pmdown_time, "DAPM stream powerdown time (msecs)"); 64db2a4165SFrank Mandarino 65965ac42cSLiam Girdwood /* 66965ac42cSLiam Girdwood * This function forces any delayed work to be queued and run. 67965ac42cSLiam Girdwood */ 68965ac42cSLiam Girdwood static int run_delayed_work(struct delayed_work *dwork) 69965ac42cSLiam Girdwood { 70965ac42cSLiam Girdwood int ret; 71965ac42cSLiam Girdwood 72965ac42cSLiam Girdwood /* cancel any work waiting to be queued. */ 73965ac42cSLiam Girdwood ret = cancel_delayed_work(dwork); 74965ac42cSLiam Girdwood 75965ac42cSLiam Girdwood /* if there was any work waiting then we run it now and 76965ac42cSLiam Girdwood * wait for it's completion */ 77965ac42cSLiam Girdwood if (ret) { 78965ac42cSLiam Girdwood schedule_delayed_work(dwork, 0); 79965ac42cSLiam Girdwood flush_scheduled_work(); 80965ac42cSLiam Girdwood } 81965ac42cSLiam Girdwood return ret; 82965ac42cSLiam Girdwood } 83965ac42cSLiam Girdwood 842624d5faSMark Brown /* codec register dump */ 852624d5faSMark Brown static ssize_t soc_codec_reg_show(struct snd_soc_codec *codec, char *buf) 862624d5faSMark Brown { 872624d5faSMark Brown int i, step = 1, count = 0; 882624d5faSMark Brown 892624d5faSMark Brown if (!codec->reg_cache_size) 902624d5faSMark Brown return 0; 912624d5faSMark Brown 922624d5faSMark Brown if (codec->reg_cache_step) 932624d5faSMark Brown step = codec->reg_cache_step; 942624d5faSMark Brown 952624d5faSMark Brown count += sprintf(buf, "%s registers\n", codec->name); 962624d5faSMark Brown for (i = 0; i < codec->reg_cache_size; i += step) { 972624d5faSMark Brown if (codec->readable_register && !codec->readable_register(i)) 982624d5faSMark Brown continue; 992624d5faSMark Brown 1002624d5faSMark Brown count += sprintf(buf + count, "%2x: ", i); 1012624d5faSMark Brown if (count >= PAGE_SIZE - 1) 1022624d5faSMark Brown break; 1032624d5faSMark Brown 1042624d5faSMark Brown if (codec->display_register) 1052624d5faSMark Brown count += codec->display_register(codec, buf + count, 1062624d5faSMark Brown PAGE_SIZE - count, i); 1072624d5faSMark Brown else 1082624d5faSMark Brown count += snprintf(buf + count, PAGE_SIZE - count, 1092624d5faSMark Brown "%4x", codec->read(codec, i)); 1102624d5faSMark Brown 1112624d5faSMark Brown if (count >= PAGE_SIZE - 1) 1122624d5faSMark Brown break; 1132624d5faSMark Brown 1142624d5faSMark Brown count += snprintf(buf + count, PAGE_SIZE - count, "\n"); 1152624d5faSMark Brown if (count >= PAGE_SIZE - 1) 1162624d5faSMark Brown break; 1172624d5faSMark Brown } 1182624d5faSMark Brown 1192624d5faSMark Brown /* Truncate count; min() would cause a warning */ 1202624d5faSMark Brown if (count >= PAGE_SIZE) 1212624d5faSMark Brown count = PAGE_SIZE - 1; 1222624d5faSMark Brown 1232624d5faSMark Brown return count; 1242624d5faSMark Brown } 1252624d5faSMark Brown static ssize_t codec_reg_show(struct device *dev, 1262624d5faSMark Brown struct device_attribute *attr, char *buf) 1272624d5faSMark Brown { 1282624d5faSMark Brown struct snd_soc_device *devdata = dev_get_drvdata(dev); 1292624d5faSMark Brown return soc_codec_reg_show(devdata->card->codec, buf); 1302624d5faSMark Brown } 1312624d5faSMark Brown 1322624d5faSMark Brown static DEVICE_ATTR(codec_reg, 0444, codec_reg_show, NULL); 1332624d5faSMark Brown 134dbe21408SMark Brown static ssize_t pmdown_time_show(struct device *dev, 135dbe21408SMark Brown struct device_attribute *attr, char *buf) 136dbe21408SMark Brown { 137dbe21408SMark Brown struct snd_soc_device *socdev = dev_get_drvdata(dev); 138dbe21408SMark Brown struct snd_soc_card *card = socdev->card; 139dbe21408SMark Brown 1406c5f1fedSMark Brown return sprintf(buf, "%ld\n", card->pmdown_time); 141dbe21408SMark Brown } 142dbe21408SMark Brown 143dbe21408SMark Brown static ssize_t pmdown_time_set(struct device *dev, 144dbe21408SMark Brown struct device_attribute *attr, 145dbe21408SMark Brown const char *buf, size_t count) 146dbe21408SMark Brown { 147dbe21408SMark Brown struct snd_soc_device *socdev = dev_get_drvdata(dev); 148dbe21408SMark Brown struct snd_soc_card *card = socdev->card; 149dbe21408SMark Brown 150dbe21408SMark Brown strict_strtol(buf, 10, &card->pmdown_time); 151dbe21408SMark Brown 152dbe21408SMark Brown return count; 153dbe21408SMark Brown } 154dbe21408SMark Brown 155dbe21408SMark Brown static DEVICE_ATTR(pmdown_time, 0644, pmdown_time_show, pmdown_time_set); 156dbe21408SMark Brown 1572624d5faSMark Brown #ifdef CONFIG_DEBUG_FS 1582624d5faSMark Brown static int codec_reg_open_file(struct inode *inode, struct file *file) 1592624d5faSMark Brown { 1602624d5faSMark Brown file->private_data = inode->i_private; 1612624d5faSMark Brown return 0; 1622624d5faSMark Brown } 1632624d5faSMark Brown 1642624d5faSMark Brown static ssize_t codec_reg_read_file(struct file *file, char __user *user_buf, 1652624d5faSMark Brown size_t count, loff_t *ppos) 1662624d5faSMark Brown { 1672624d5faSMark Brown ssize_t ret; 1682624d5faSMark Brown struct snd_soc_codec *codec = file->private_data; 1692624d5faSMark Brown char *buf = kmalloc(PAGE_SIZE, GFP_KERNEL); 1702624d5faSMark Brown if (!buf) 1712624d5faSMark Brown return -ENOMEM; 1722624d5faSMark Brown ret = soc_codec_reg_show(codec, buf); 1732624d5faSMark Brown if (ret >= 0) 1742624d5faSMark Brown ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret); 1752624d5faSMark Brown kfree(buf); 1762624d5faSMark Brown return ret; 1772624d5faSMark Brown } 1782624d5faSMark Brown 1792624d5faSMark Brown static ssize_t codec_reg_write_file(struct file *file, 1802624d5faSMark Brown const char __user *user_buf, size_t count, loff_t *ppos) 1812624d5faSMark Brown { 1822624d5faSMark Brown char buf[32]; 1832624d5faSMark Brown int buf_size; 1842624d5faSMark Brown char *start = buf; 1852624d5faSMark Brown unsigned long reg, value; 1862624d5faSMark Brown int step = 1; 1872624d5faSMark Brown struct snd_soc_codec *codec = file->private_data; 1882624d5faSMark Brown 1892624d5faSMark Brown buf_size = min(count, (sizeof(buf)-1)); 1902624d5faSMark Brown if (copy_from_user(buf, user_buf, buf_size)) 1912624d5faSMark Brown return -EFAULT; 1922624d5faSMark Brown buf[buf_size] = 0; 1932624d5faSMark Brown 1942624d5faSMark Brown if (codec->reg_cache_step) 1952624d5faSMark Brown step = codec->reg_cache_step; 1962624d5faSMark Brown 1972624d5faSMark Brown while (*start == ' ') 1982624d5faSMark Brown start++; 1992624d5faSMark Brown reg = simple_strtoul(start, &start, 16); 2002624d5faSMark Brown if ((reg >= codec->reg_cache_size) || (reg % step)) 2012624d5faSMark Brown return -EINVAL; 2022624d5faSMark Brown while (*start == ' ') 2032624d5faSMark Brown start++; 2042624d5faSMark Brown if (strict_strtoul(start, 16, &value)) 2052624d5faSMark Brown return -EINVAL; 2062624d5faSMark Brown codec->write(codec, reg, value); 2072624d5faSMark Brown return buf_size; 2082624d5faSMark Brown } 2092624d5faSMark Brown 2102624d5faSMark Brown static const struct file_operations codec_reg_fops = { 2112624d5faSMark Brown .open = codec_reg_open_file, 2122624d5faSMark Brown .read = codec_reg_read_file, 2132624d5faSMark Brown .write = codec_reg_write_file, 2142624d5faSMark Brown }; 2152624d5faSMark Brown 2162624d5faSMark Brown static void soc_init_codec_debugfs(struct snd_soc_codec *codec) 2172624d5faSMark Brown { 2182624d5faSMark Brown char codec_root[128]; 2192624d5faSMark Brown 2202624d5faSMark Brown if (codec->dev) 2212624d5faSMark Brown snprintf(codec_root, sizeof(codec_root), 2222624d5faSMark Brown "%s.%s", codec->name, dev_name(codec->dev)); 2232624d5faSMark Brown else 2242624d5faSMark Brown snprintf(codec_root, sizeof(codec_root), 2252624d5faSMark Brown "%s", codec->name); 2262624d5faSMark Brown 2272624d5faSMark Brown codec->debugfs_codec_root = debugfs_create_dir(codec_root, 2282624d5faSMark Brown debugfs_root); 2292624d5faSMark Brown if (!codec->debugfs_codec_root) { 2302624d5faSMark Brown printk(KERN_WARNING 2312624d5faSMark Brown "ASoC: Failed to create codec debugfs directory\n"); 2322624d5faSMark Brown return; 2332624d5faSMark Brown } 2342624d5faSMark Brown 2352624d5faSMark Brown codec->debugfs_reg = debugfs_create_file("codec_reg", 0644, 2362624d5faSMark Brown codec->debugfs_codec_root, 2372624d5faSMark Brown codec, &codec_reg_fops); 2382624d5faSMark Brown if (!codec->debugfs_reg) 2392624d5faSMark Brown printk(KERN_WARNING 2402624d5faSMark Brown "ASoC: Failed to create codec register debugfs file\n"); 2412624d5faSMark Brown 2422624d5faSMark Brown codec->debugfs_pop_time = debugfs_create_u32("dapm_pop_time", 0744, 2432624d5faSMark Brown codec->debugfs_codec_root, 2442624d5faSMark Brown &codec->pop_time); 2452624d5faSMark Brown if (!codec->debugfs_pop_time) 2462624d5faSMark Brown printk(KERN_WARNING 2472624d5faSMark Brown "Failed to create pop time debugfs file\n"); 2482624d5faSMark Brown 2492624d5faSMark Brown codec->debugfs_dapm = debugfs_create_dir("dapm", 2502624d5faSMark Brown codec->debugfs_codec_root); 2512624d5faSMark Brown if (!codec->debugfs_dapm) 2522624d5faSMark Brown printk(KERN_WARNING 2532624d5faSMark Brown "Failed to create DAPM debugfs directory\n"); 2542624d5faSMark Brown 2552624d5faSMark Brown snd_soc_dapm_debugfs_init(codec); 2562624d5faSMark Brown } 2572624d5faSMark Brown 2582624d5faSMark Brown static void soc_cleanup_codec_debugfs(struct snd_soc_codec *codec) 2592624d5faSMark Brown { 2602624d5faSMark Brown debugfs_remove_recursive(codec->debugfs_codec_root); 2612624d5faSMark Brown } 2622624d5faSMark Brown 2632624d5faSMark Brown #else 2642624d5faSMark Brown 2652624d5faSMark Brown static inline void soc_init_codec_debugfs(struct snd_soc_codec *codec) 2662624d5faSMark Brown { 2672624d5faSMark Brown } 2682624d5faSMark Brown 2692624d5faSMark Brown static inline void soc_cleanup_codec_debugfs(struct snd_soc_codec *codec) 2702624d5faSMark Brown { 2712624d5faSMark Brown } 2722624d5faSMark Brown #endif 2732624d5faSMark Brown 274db2a4165SFrank Mandarino #ifdef CONFIG_SND_SOC_AC97_BUS 275db2a4165SFrank Mandarino /* unregister ac97 codec */ 276db2a4165SFrank Mandarino static int soc_ac97_dev_unregister(struct snd_soc_codec *codec) 277db2a4165SFrank Mandarino { 278db2a4165SFrank Mandarino if (codec->ac97->dev.bus) 279db2a4165SFrank Mandarino device_unregister(&codec->ac97->dev); 280db2a4165SFrank Mandarino return 0; 281db2a4165SFrank Mandarino } 282db2a4165SFrank Mandarino 283db2a4165SFrank Mandarino /* stop no dev release warning */ 284db2a4165SFrank Mandarino static void soc_ac97_device_release(struct device *dev){} 285db2a4165SFrank Mandarino 286db2a4165SFrank Mandarino /* register ac97 codec to bus */ 287db2a4165SFrank Mandarino static int soc_ac97_dev_register(struct snd_soc_codec *codec) 288db2a4165SFrank Mandarino { 289db2a4165SFrank Mandarino int err; 290db2a4165SFrank Mandarino 291db2a4165SFrank Mandarino codec->ac97->dev.bus = &ac97_bus_type; 2924ac5c61fSMark Brown codec->ac97->dev.parent = codec->card->dev; 293db2a4165SFrank Mandarino codec->ac97->dev.release = soc_ac97_device_release; 294db2a4165SFrank Mandarino 295bb072bf0SKay Sievers dev_set_name(&codec->ac97->dev, "%d-%d:%s", 296db2a4165SFrank Mandarino codec->card->number, 0, codec->name); 297db2a4165SFrank Mandarino err = device_register(&codec->ac97->dev); 298db2a4165SFrank Mandarino if (err < 0) { 299db2a4165SFrank Mandarino snd_printk(KERN_ERR "Can't register ac97 bus\n"); 300db2a4165SFrank Mandarino codec->ac97->dev.bus = NULL; 301db2a4165SFrank Mandarino return err; 302db2a4165SFrank Mandarino } 303db2a4165SFrank Mandarino return 0; 304db2a4165SFrank Mandarino } 305db2a4165SFrank Mandarino #endif 306db2a4165SFrank Mandarino 30706f409d7SMark Brown static int soc_pcm_apply_symmetry(struct snd_pcm_substream *substream) 30806f409d7SMark Brown { 30906f409d7SMark Brown struct snd_soc_pcm_runtime *rtd = substream->private_data; 31006f409d7SMark Brown struct snd_soc_device *socdev = rtd->socdev; 31106f409d7SMark Brown struct snd_soc_card *card = socdev->card; 31206f409d7SMark Brown struct snd_soc_dai_link *machine = rtd->dai; 31306f409d7SMark Brown struct snd_soc_dai *cpu_dai = machine->cpu_dai; 31406f409d7SMark Brown struct snd_soc_dai *codec_dai = machine->codec_dai; 31506f409d7SMark Brown int ret; 31606f409d7SMark Brown 31706f409d7SMark Brown if (codec_dai->symmetric_rates || cpu_dai->symmetric_rates || 31806f409d7SMark Brown machine->symmetric_rates) { 31906f409d7SMark Brown dev_dbg(card->dev, "Symmetry forces %dHz rate\n", 32006f409d7SMark Brown machine->rate); 32106f409d7SMark Brown 32206f409d7SMark Brown ret = snd_pcm_hw_constraint_minmax(substream->runtime, 32306f409d7SMark Brown SNDRV_PCM_HW_PARAM_RATE, 32406f409d7SMark Brown machine->rate, 32506f409d7SMark Brown machine->rate); 32606f409d7SMark Brown if (ret < 0) { 32706f409d7SMark Brown dev_err(card->dev, 32806f409d7SMark Brown "Unable to apply rate symmetry constraint: %d\n", ret); 32906f409d7SMark Brown return ret; 33006f409d7SMark Brown } 33106f409d7SMark Brown } 33206f409d7SMark Brown 33306f409d7SMark Brown return 0; 33406f409d7SMark Brown } 33506f409d7SMark Brown 336db2a4165SFrank Mandarino /* 337db2a4165SFrank Mandarino * Called by ALSA when a PCM substream is opened, the runtime->hw record is 338db2a4165SFrank Mandarino * then initialized and any private data can be allocated. This also calls 339db2a4165SFrank Mandarino * startup for the cpu DAI, platform, machine and codec DAI. 340db2a4165SFrank Mandarino */ 341db2a4165SFrank Mandarino static int soc_pcm_open(struct snd_pcm_substream *substream) 342db2a4165SFrank Mandarino { 343db2a4165SFrank Mandarino struct snd_soc_pcm_runtime *rtd = substream->private_data; 344db2a4165SFrank Mandarino struct snd_soc_device *socdev = rtd->socdev; 34587689d56SMark Brown struct snd_soc_card *card = socdev->card; 346db2a4165SFrank Mandarino struct snd_pcm_runtime *runtime = substream->runtime; 347cb666e5bSLiam Girdwood struct snd_soc_dai_link *machine = rtd->dai; 34887689d56SMark Brown struct snd_soc_platform *platform = card->platform; 3493c4b266fSLiam Girdwood struct snd_soc_dai *cpu_dai = machine->cpu_dai; 3503c4b266fSLiam Girdwood struct snd_soc_dai *codec_dai = machine->codec_dai; 351db2a4165SFrank Mandarino int ret = 0; 352db2a4165SFrank Mandarino 353db2a4165SFrank Mandarino mutex_lock(&pcm_mutex); 354db2a4165SFrank Mandarino 355db2a4165SFrank Mandarino /* startup the audio subsystem */ 3566335d055SEric Miao if (cpu_dai->ops->startup) { 3576335d055SEric Miao ret = cpu_dai->ops->startup(substream, cpu_dai); 358db2a4165SFrank Mandarino if (ret < 0) { 359db2a4165SFrank Mandarino printk(KERN_ERR "asoc: can't open interface %s\n", 360cb666e5bSLiam Girdwood cpu_dai->name); 361db2a4165SFrank Mandarino goto out; 362db2a4165SFrank Mandarino } 363db2a4165SFrank Mandarino } 364db2a4165SFrank Mandarino 365db2a4165SFrank Mandarino if (platform->pcm_ops->open) { 366db2a4165SFrank Mandarino ret = platform->pcm_ops->open(substream); 367db2a4165SFrank Mandarino if (ret < 0) { 368db2a4165SFrank Mandarino printk(KERN_ERR "asoc: can't open platform %s\n", platform->name); 369db2a4165SFrank Mandarino goto platform_err; 370db2a4165SFrank Mandarino } 371db2a4165SFrank Mandarino } 372db2a4165SFrank Mandarino 3736335d055SEric Miao if (codec_dai->ops->startup) { 3746335d055SEric Miao ret = codec_dai->ops->startup(substream, codec_dai); 375cb666e5bSLiam Girdwood if (ret < 0) { 376cb666e5bSLiam Girdwood printk(KERN_ERR "asoc: can't open codec %s\n", 377cb666e5bSLiam Girdwood codec_dai->name); 378cb666e5bSLiam Girdwood goto codec_dai_err; 379cb666e5bSLiam Girdwood } 380cb666e5bSLiam Girdwood } 381cb666e5bSLiam Girdwood 382db2a4165SFrank Mandarino if (machine->ops && machine->ops->startup) { 383db2a4165SFrank Mandarino ret = machine->ops->startup(substream); 384db2a4165SFrank Mandarino if (ret < 0) { 385db2a4165SFrank Mandarino printk(KERN_ERR "asoc: %s startup failed\n", machine->name); 386db2a4165SFrank Mandarino goto machine_err; 387db2a4165SFrank Mandarino } 388db2a4165SFrank Mandarino } 389db2a4165SFrank Mandarino 390db2a4165SFrank Mandarino /* Check that the codec and cpu DAI's are compatible */ 391db2a4165SFrank Mandarino if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) { 392db2a4165SFrank Mandarino runtime->hw.rate_min = 3933ff3f64bSMark Brown max(codec_dai->playback.rate_min, 3943ff3f64bSMark Brown cpu_dai->playback.rate_min); 395db2a4165SFrank Mandarino runtime->hw.rate_max = 3963ff3f64bSMark Brown min(codec_dai->playback.rate_max, 3973ff3f64bSMark Brown cpu_dai->playback.rate_max); 398db2a4165SFrank Mandarino runtime->hw.channels_min = 399cb666e5bSLiam Girdwood max(codec_dai->playback.channels_min, 400cb666e5bSLiam Girdwood cpu_dai->playback.channels_min); 401db2a4165SFrank Mandarino runtime->hw.channels_max = 402cb666e5bSLiam Girdwood min(codec_dai->playback.channels_max, 403cb666e5bSLiam Girdwood cpu_dai->playback.channels_max); 404cb666e5bSLiam Girdwood runtime->hw.formats = 405cb666e5bSLiam Girdwood codec_dai->playback.formats & cpu_dai->playback.formats; 406cb666e5bSLiam Girdwood runtime->hw.rates = 407cb666e5bSLiam Girdwood codec_dai->playback.rates & cpu_dai->playback.rates; 408d9ad6296SJassi Brar if (codec_dai->playback.rates 409d9ad6296SJassi Brar & (SNDRV_PCM_RATE_KNOT | SNDRV_PCM_RATE_CONTINUOUS)) 410d9ad6296SJassi Brar runtime->hw.rates |= cpu_dai->playback.rates; 411d9ad6296SJassi Brar if (cpu_dai->playback.rates 412d9ad6296SJassi Brar & (SNDRV_PCM_RATE_KNOT | SNDRV_PCM_RATE_CONTINUOUS)) 413d9ad6296SJassi Brar runtime->hw.rates |= codec_dai->playback.rates; 414db2a4165SFrank Mandarino } else { 415db2a4165SFrank Mandarino runtime->hw.rate_min = 4163ff3f64bSMark Brown max(codec_dai->capture.rate_min, 4173ff3f64bSMark Brown cpu_dai->capture.rate_min); 418db2a4165SFrank Mandarino runtime->hw.rate_max = 4193ff3f64bSMark Brown min(codec_dai->capture.rate_max, 4203ff3f64bSMark Brown cpu_dai->capture.rate_max); 421db2a4165SFrank Mandarino runtime->hw.channels_min = 422cb666e5bSLiam Girdwood max(codec_dai->capture.channels_min, 423cb666e5bSLiam Girdwood cpu_dai->capture.channels_min); 424db2a4165SFrank Mandarino runtime->hw.channels_max = 425cb666e5bSLiam Girdwood min(codec_dai->capture.channels_max, 426cb666e5bSLiam Girdwood cpu_dai->capture.channels_max); 427cb666e5bSLiam Girdwood runtime->hw.formats = 428cb666e5bSLiam Girdwood codec_dai->capture.formats & cpu_dai->capture.formats; 429cb666e5bSLiam Girdwood runtime->hw.rates = 430cb666e5bSLiam Girdwood codec_dai->capture.rates & cpu_dai->capture.rates; 431d9ad6296SJassi Brar if (codec_dai->capture.rates 432d9ad6296SJassi Brar & (SNDRV_PCM_RATE_KNOT | SNDRV_PCM_RATE_CONTINUOUS)) 433d9ad6296SJassi Brar runtime->hw.rates |= cpu_dai->capture.rates; 434d9ad6296SJassi Brar if (cpu_dai->capture.rates 435d9ad6296SJassi Brar & (SNDRV_PCM_RATE_KNOT | SNDRV_PCM_RATE_CONTINUOUS)) 436d9ad6296SJassi Brar runtime->hw.rates |= codec_dai->capture.rates; 437db2a4165SFrank Mandarino } 438db2a4165SFrank Mandarino 439db2a4165SFrank Mandarino snd_pcm_limit_hw_rates(runtime); 440db2a4165SFrank Mandarino if (!runtime->hw.rates) { 441db2a4165SFrank Mandarino printk(KERN_ERR "asoc: %s <-> %s No matching rates\n", 442cb666e5bSLiam Girdwood codec_dai->name, cpu_dai->name); 443bb1c0478SJassi Brar goto config_err; 444db2a4165SFrank Mandarino } 445db2a4165SFrank Mandarino if (!runtime->hw.formats) { 446db2a4165SFrank Mandarino printk(KERN_ERR "asoc: %s <-> %s No matching formats\n", 447cb666e5bSLiam Girdwood codec_dai->name, cpu_dai->name); 448bb1c0478SJassi Brar goto config_err; 449db2a4165SFrank Mandarino } 450db2a4165SFrank Mandarino if (!runtime->hw.channels_min || !runtime->hw.channels_max) { 451db2a4165SFrank Mandarino printk(KERN_ERR "asoc: %s <-> %s No matching channels\n", 452cb666e5bSLiam Girdwood codec_dai->name, cpu_dai->name); 453bb1c0478SJassi Brar goto config_err; 454db2a4165SFrank Mandarino } 455db2a4165SFrank Mandarino 45606f409d7SMark Brown /* Symmetry only applies if we've already got an active stream. */ 45706f409d7SMark Brown if (cpu_dai->active || codec_dai->active) { 45806f409d7SMark Brown ret = soc_pcm_apply_symmetry(substream); 45906f409d7SMark Brown if (ret != 0) 460bb1c0478SJassi Brar goto config_err; 46106f409d7SMark Brown } 46206f409d7SMark Brown 463f24368c2SMark Brown pr_debug("asoc: %s <-> %s info:\n", codec_dai->name, cpu_dai->name); 464f24368c2SMark Brown pr_debug("asoc: rate mask 0x%x\n", runtime->hw.rates); 465f24368c2SMark Brown pr_debug("asoc: min ch %d max ch %d\n", runtime->hw.channels_min, 466b5c5fd24SLiam Girdwood runtime->hw.channels_max); 467f24368c2SMark Brown pr_debug("asoc: min rate %d max rate %d\n", runtime->hw.rate_min, 468b5c5fd24SLiam Girdwood runtime->hw.rate_max); 469db2a4165SFrank Mandarino 47014dc5734SJassi Brar if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) { 47114dc5734SJassi Brar cpu_dai->playback.active++; 47214dc5734SJassi Brar codec_dai->playback.active++; 47314dc5734SJassi Brar } else { 47414dc5734SJassi Brar cpu_dai->capture.active++; 47514dc5734SJassi Brar codec_dai->capture.active++; 47614dc5734SJassi Brar } 47714dc5734SJassi Brar cpu_dai->active++; 47814dc5734SJassi Brar codec_dai->active++; 4796627a653SMark Brown card->codec->active++; 480db2a4165SFrank Mandarino mutex_unlock(&pcm_mutex); 481db2a4165SFrank Mandarino return 0; 482db2a4165SFrank Mandarino 483bb1c0478SJassi Brar config_err: 484db2a4165SFrank Mandarino if (machine->ops && machine->ops->shutdown) 485db2a4165SFrank Mandarino machine->ops->shutdown(substream); 486db2a4165SFrank Mandarino 487bb1c0478SJassi Brar machine_err: 488bb1c0478SJassi Brar if (codec_dai->ops->shutdown) 489bb1c0478SJassi Brar codec_dai->ops->shutdown(substream, codec_dai); 490bb1c0478SJassi Brar 491cb666e5bSLiam Girdwood codec_dai_err: 492db2a4165SFrank Mandarino if (platform->pcm_ops->close) 493db2a4165SFrank Mandarino platform->pcm_ops->close(substream); 494db2a4165SFrank Mandarino 495db2a4165SFrank Mandarino platform_err: 4966335d055SEric Miao if (cpu_dai->ops->shutdown) 4976335d055SEric Miao cpu_dai->ops->shutdown(substream, cpu_dai); 498db2a4165SFrank Mandarino out: 499db2a4165SFrank Mandarino mutex_unlock(&pcm_mutex); 500db2a4165SFrank Mandarino return ret; 501db2a4165SFrank Mandarino } 502db2a4165SFrank Mandarino 503db2a4165SFrank Mandarino /* 5043a4fa0a2SRobert P. J. Day * Power down the audio subsystem pmdown_time msecs after close is called. 505db2a4165SFrank Mandarino * This is to ensure there are no pops or clicks in between any music tracks 506db2a4165SFrank Mandarino * due to DAPM power cycling. 507db2a4165SFrank Mandarino */ 5084484bb2eSAndrew Morton static void close_delayed_work(struct work_struct *work) 509db2a4165SFrank Mandarino { 5106308419aSMark Brown struct snd_soc_card *card = container_of(work, struct snd_soc_card, 5116308419aSMark Brown delayed_work.work); 5126627a653SMark Brown struct snd_soc_codec *codec = card->codec; 5133c4b266fSLiam Girdwood struct snd_soc_dai *codec_dai; 514db2a4165SFrank Mandarino int i; 515db2a4165SFrank Mandarino 516db2a4165SFrank Mandarino mutex_lock(&pcm_mutex); 517db2a4165SFrank Mandarino for (i = 0; i < codec->num_dai; i++) { 518db2a4165SFrank Mandarino codec_dai = &codec->dai[i]; 519db2a4165SFrank Mandarino 520f24368c2SMark Brown pr_debug("pop wq checking: %s status: %s waiting: %s\n", 521db2a4165SFrank Mandarino codec_dai->playback.stream_name, 522db2a4165SFrank Mandarino codec_dai->playback.active ? "active" : "inactive", 523db2a4165SFrank Mandarino codec_dai->pop_wait ? "yes" : "no"); 524db2a4165SFrank Mandarino 525db2a4165SFrank Mandarino /* are we waiting on this codec DAI stream */ 526db2a4165SFrank Mandarino if (codec_dai->pop_wait == 1) { 527db2a4165SFrank Mandarino codec_dai->pop_wait = 0; 5280b4d221bSLiam Girdwood snd_soc_dapm_stream_event(codec, 5290b4d221bSLiam Girdwood codec_dai->playback.stream_name, 530db2a4165SFrank Mandarino SND_SOC_DAPM_STREAM_STOP); 531db2a4165SFrank Mandarino } 532db2a4165SFrank Mandarino } 533db2a4165SFrank Mandarino mutex_unlock(&pcm_mutex); 534db2a4165SFrank Mandarino } 535db2a4165SFrank Mandarino 536db2a4165SFrank Mandarino /* 537db2a4165SFrank Mandarino * Called by ALSA when a PCM substream is closed. Private data can be 538db2a4165SFrank Mandarino * freed here. The cpu DAI, codec DAI, machine and platform are also 539db2a4165SFrank Mandarino * shutdown. 540db2a4165SFrank Mandarino */ 541db2a4165SFrank Mandarino static int soc_codec_close(struct snd_pcm_substream *substream) 542db2a4165SFrank Mandarino { 543db2a4165SFrank Mandarino struct snd_soc_pcm_runtime *rtd = substream->private_data; 544db2a4165SFrank Mandarino struct snd_soc_device *socdev = rtd->socdev; 5456308419aSMark Brown struct snd_soc_card *card = socdev->card; 546cb666e5bSLiam Girdwood struct snd_soc_dai_link *machine = rtd->dai; 54787689d56SMark Brown struct snd_soc_platform *platform = card->platform; 5483c4b266fSLiam Girdwood struct snd_soc_dai *cpu_dai = machine->cpu_dai; 5493c4b266fSLiam Girdwood struct snd_soc_dai *codec_dai = machine->codec_dai; 5506627a653SMark Brown struct snd_soc_codec *codec = card->codec; 551db2a4165SFrank Mandarino 552db2a4165SFrank Mandarino mutex_lock(&pcm_mutex); 553db2a4165SFrank Mandarino 55414dc5734SJassi Brar if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) { 55514dc5734SJassi Brar cpu_dai->playback.active--; 55614dc5734SJassi Brar codec_dai->playback.active--; 55714dc5734SJassi Brar } else { 55814dc5734SJassi Brar cpu_dai->capture.active--; 55914dc5734SJassi Brar codec_dai->capture.active--; 560db2a4165SFrank Mandarino } 56114dc5734SJassi Brar 56214dc5734SJassi Brar cpu_dai->active--; 56314dc5734SJassi Brar codec_dai->active--; 564db2a4165SFrank Mandarino codec->active--; 565db2a4165SFrank Mandarino 5666010b2daSMark Brown /* Muting the DAC suppresses artifacts caused during digital 5676010b2daSMark Brown * shutdown, for example from stopping clocks. 5686010b2daSMark Brown */ 5696010b2daSMark Brown if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) 5706010b2daSMark Brown snd_soc_dai_digital_mute(codec_dai, 1); 5716010b2daSMark Brown 5726335d055SEric Miao if (cpu_dai->ops->shutdown) 5736335d055SEric Miao cpu_dai->ops->shutdown(substream, cpu_dai); 574db2a4165SFrank Mandarino 5756335d055SEric Miao if (codec_dai->ops->shutdown) 5766335d055SEric Miao codec_dai->ops->shutdown(substream, codec_dai); 577db2a4165SFrank Mandarino 578db2a4165SFrank Mandarino if (machine->ops && machine->ops->shutdown) 579db2a4165SFrank Mandarino machine->ops->shutdown(substream); 580db2a4165SFrank Mandarino 581db2a4165SFrank Mandarino if (platform->pcm_ops->close) 582db2a4165SFrank Mandarino platform->pcm_ops->close(substream); 583db2a4165SFrank Mandarino 584db2a4165SFrank Mandarino if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) { 585db2a4165SFrank Mandarino /* start delayed pop wq here for playback streams */ 586cb666e5bSLiam Girdwood codec_dai->pop_wait = 1; 5876308419aSMark Brown schedule_delayed_work(&card->delayed_work, 58896dd3622SMark Brown msecs_to_jiffies(card->pmdown_time)); 589db2a4165SFrank Mandarino } else { 590db2a4165SFrank Mandarino /* capture streams can be powered down now */ 591cb666e5bSLiam Girdwood snd_soc_dapm_stream_event(codec, 5920b4d221bSLiam Girdwood codec_dai->capture.stream_name, 5930b4d221bSLiam Girdwood SND_SOC_DAPM_STREAM_STOP); 594db2a4165SFrank Mandarino } 595db2a4165SFrank Mandarino 596db2a4165SFrank Mandarino mutex_unlock(&pcm_mutex); 597db2a4165SFrank Mandarino return 0; 598db2a4165SFrank Mandarino } 599db2a4165SFrank Mandarino 600db2a4165SFrank Mandarino /* 601db2a4165SFrank Mandarino * Called by ALSA when the PCM substream is prepared, can set format, sample 602db2a4165SFrank Mandarino * rate, etc. This function is non atomic and can be called multiple times, 603db2a4165SFrank Mandarino * it can refer to the runtime info. 604db2a4165SFrank Mandarino */ 605db2a4165SFrank Mandarino static int soc_pcm_prepare(struct snd_pcm_substream *substream) 606db2a4165SFrank Mandarino { 607db2a4165SFrank Mandarino struct snd_soc_pcm_runtime *rtd = substream->private_data; 608db2a4165SFrank Mandarino struct snd_soc_device *socdev = rtd->socdev; 6096308419aSMark Brown struct snd_soc_card *card = socdev->card; 610cb666e5bSLiam Girdwood struct snd_soc_dai_link *machine = rtd->dai; 61187689d56SMark Brown struct snd_soc_platform *platform = card->platform; 6123c4b266fSLiam Girdwood struct snd_soc_dai *cpu_dai = machine->cpu_dai; 6133c4b266fSLiam Girdwood struct snd_soc_dai *codec_dai = machine->codec_dai; 6146627a653SMark Brown struct snd_soc_codec *codec = card->codec; 615db2a4165SFrank Mandarino int ret = 0; 616db2a4165SFrank Mandarino 617db2a4165SFrank Mandarino mutex_lock(&pcm_mutex); 618cb666e5bSLiam Girdwood 619cb666e5bSLiam Girdwood if (machine->ops && machine->ops->prepare) { 620cb666e5bSLiam Girdwood ret = machine->ops->prepare(substream); 621cb666e5bSLiam Girdwood if (ret < 0) { 622cb666e5bSLiam Girdwood printk(KERN_ERR "asoc: machine prepare error\n"); 623cb666e5bSLiam Girdwood goto out; 624cb666e5bSLiam Girdwood } 625cb666e5bSLiam Girdwood } 626cb666e5bSLiam Girdwood 627db2a4165SFrank Mandarino if (platform->pcm_ops->prepare) { 628db2a4165SFrank Mandarino ret = platform->pcm_ops->prepare(substream); 629a71a468aSLiam Girdwood if (ret < 0) { 630a71a468aSLiam Girdwood printk(KERN_ERR "asoc: platform prepare error\n"); 631db2a4165SFrank Mandarino goto out; 632db2a4165SFrank Mandarino } 633a71a468aSLiam Girdwood } 634db2a4165SFrank Mandarino 6356335d055SEric Miao if (codec_dai->ops->prepare) { 6366335d055SEric Miao ret = codec_dai->ops->prepare(substream, codec_dai); 637a71a468aSLiam Girdwood if (ret < 0) { 638a71a468aSLiam Girdwood printk(KERN_ERR "asoc: codec DAI prepare error\n"); 639db2a4165SFrank Mandarino goto out; 640db2a4165SFrank Mandarino } 641a71a468aSLiam Girdwood } 642db2a4165SFrank Mandarino 6436335d055SEric Miao if (cpu_dai->ops->prepare) { 6446335d055SEric Miao ret = cpu_dai->ops->prepare(substream, cpu_dai); 645cb666e5bSLiam Girdwood if (ret < 0) { 646cb666e5bSLiam Girdwood printk(KERN_ERR "asoc: cpu DAI prepare error\n"); 647cb666e5bSLiam Girdwood goto out; 648cb666e5bSLiam Girdwood } 649cb666e5bSLiam Girdwood } 650db2a4165SFrank Mandarino 651d45f6219SMark Brown /* cancel any delayed stream shutdown that is pending */ 652d45f6219SMark Brown if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK && 653d45f6219SMark Brown codec_dai->pop_wait) { 654cb666e5bSLiam Girdwood codec_dai->pop_wait = 0; 6556308419aSMark Brown cancel_delayed_work(&card->delayed_work); 656db2a4165SFrank Mandarino } 657db2a4165SFrank Mandarino 658db2a4165SFrank Mandarino if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) 659db2a4165SFrank Mandarino snd_soc_dapm_stream_event(codec, 660cb666e5bSLiam Girdwood codec_dai->playback.stream_name, 661db2a4165SFrank Mandarino SND_SOC_DAPM_STREAM_START); 662db2a4165SFrank Mandarino else 663db2a4165SFrank Mandarino snd_soc_dapm_stream_event(codec, 664cb666e5bSLiam Girdwood codec_dai->capture.stream_name, 665db2a4165SFrank Mandarino SND_SOC_DAPM_STREAM_START); 6668c6529dbSLiam Girdwood 6678c6529dbSLiam Girdwood snd_soc_dai_digital_mute(codec_dai, 0); 668db2a4165SFrank Mandarino 669db2a4165SFrank Mandarino out: 670db2a4165SFrank Mandarino mutex_unlock(&pcm_mutex); 671db2a4165SFrank Mandarino return ret; 672db2a4165SFrank Mandarino } 673db2a4165SFrank Mandarino 674db2a4165SFrank Mandarino /* 675db2a4165SFrank Mandarino * Called by ALSA when the hardware params are set by application. This 676db2a4165SFrank Mandarino * function can also be called multiple times and can allocate buffers 677db2a4165SFrank Mandarino * (using snd_pcm_lib_* ). It's non-atomic. 678db2a4165SFrank Mandarino */ 679db2a4165SFrank Mandarino static int soc_pcm_hw_params(struct snd_pcm_substream *substream, 680db2a4165SFrank Mandarino struct snd_pcm_hw_params *params) 681db2a4165SFrank Mandarino { 682db2a4165SFrank Mandarino struct snd_soc_pcm_runtime *rtd = substream->private_data; 683db2a4165SFrank Mandarino struct snd_soc_device *socdev = rtd->socdev; 684cb666e5bSLiam Girdwood struct snd_soc_dai_link *machine = rtd->dai; 68587689d56SMark Brown struct snd_soc_card *card = socdev->card; 68687689d56SMark Brown struct snd_soc_platform *platform = card->platform; 6873c4b266fSLiam Girdwood struct snd_soc_dai *cpu_dai = machine->cpu_dai; 6883c4b266fSLiam Girdwood struct snd_soc_dai *codec_dai = machine->codec_dai; 689db2a4165SFrank Mandarino int ret = 0; 690db2a4165SFrank Mandarino 691db2a4165SFrank Mandarino mutex_lock(&pcm_mutex); 692db2a4165SFrank Mandarino 693cb666e5bSLiam Girdwood if (machine->ops && machine->ops->hw_params) { 694cb666e5bSLiam Girdwood ret = machine->ops->hw_params(substream, params); 695cb666e5bSLiam Girdwood if (ret < 0) { 696cb666e5bSLiam Girdwood printk(KERN_ERR "asoc: machine hw_params failed\n"); 697db2a4165SFrank Mandarino goto out; 698db2a4165SFrank Mandarino } 699cb666e5bSLiam Girdwood } 700db2a4165SFrank Mandarino 7016335d055SEric Miao if (codec_dai->ops->hw_params) { 7026335d055SEric Miao ret = codec_dai->ops->hw_params(substream, params, codec_dai); 703db2a4165SFrank Mandarino if (ret < 0) { 704db2a4165SFrank Mandarino printk(KERN_ERR "asoc: can't set codec %s hw params\n", 705cb666e5bSLiam Girdwood codec_dai->name); 706cb666e5bSLiam Girdwood goto codec_err; 707db2a4165SFrank Mandarino } 708db2a4165SFrank Mandarino } 709db2a4165SFrank Mandarino 7106335d055SEric Miao if (cpu_dai->ops->hw_params) { 7116335d055SEric Miao ret = cpu_dai->ops->hw_params(substream, params, cpu_dai); 712db2a4165SFrank Mandarino if (ret < 0) { 7133ff3f64bSMark Brown printk(KERN_ERR "asoc: interface %s hw params failed\n", 714cb666e5bSLiam Girdwood cpu_dai->name); 715db2a4165SFrank Mandarino goto interface_err; 716db2a4165SFrank Mandarino } 717db2a4165SFrank Mandarino } 718db2a4165SFrank Mandarino 719db2a4165SFrank Mandarino if (platform->pcm_ops->hw_params) { 720db2a4165SFrank Mandarino ret = platform->pcm_ops->hw_params(substream, params); 721db2a4165SFrank Mandarino if (ret < 0) { 7223ff3f64bSMark Brown printk(KERN_ERR "asoc: platform %s hw params failed\n", 723db2a4165SFrank Mandarino platform->name); 724db2a4165SFrank Mandarino goto platform_err; 725db2a4165SFrank Mandarino } 726db2a4165SFrank Mandarino } 727db2a4165SFrank Mandarino 72806f409d7SMark Brown machine->rate = params_rate(params); 72906f409d7SMark Brown 730db2a4165SFrank Mandarino out: 731db2a4165SFrank Mandarino mutex_unlock(&pcm_mutex); 732db2a4165SFrank Mandarino return ret; 733db2a4165SFrank Mandarino 734db2a4165SFrank Mandarino platform_err: 7356335d055SEric Miao if (cpu_dai->ops->hw_free) 7366335d055SEric Miao cpu_dai->ops->hw_free(substream, cpu_dai); 737db2a4165SFrank Mandarino 738db2a4165SFrank Mandarino interface_err: 7396335d055SEric Miao if (codec_dai->ops->hw_free) 7406335d055SEric Miao codec_dai->ops->hw_free(substream, codec_dai); 741cb666e5bSLiam Girdwood 742cb666e5bSLiam Girdwood codec_err: 743cb666e5bSLiam Girdwood if (machine->ops && machine->ops->hw_free) 744cb666e5bSLiam Girdwood machine->ops->hw_free(substream); 745db2a4165SFrank Mandarino 746db2a4165SFrank Mandarino mutex_unlock(&pcm_mutex); 747db2a4165SFrank Mandarino return ret; 748db2a4165SFrank Mandarino } 749db2a4165SFrank Mandarino 750db2a4165SFrank Mandarino /* 751db2a4165SFrank Mandarino * Free's resources allocated by hw_params, can be called multiple times 752db2a4165SFrank Mandarino */ 753db2a4165SFrank Mandarino static int soc_pcm_hw_free(struct snd_pcm_substream *substream) 754db2a4165SFrank Mandarino { 755db2a4165SFrank Mandarino struct snd_soc_pcm_runtime *rtd = substream->private_data; 756db2a4165SFrank Mandarino struct snd_soc_device *socdev = rtd->socdev; 757cb666e5bSLiam Girdwood struct snd_soc_dai_link *machine = rtd->dai; 75887689d56SMark Brown struct snd_soc_card *card = socdev->card; 75987689d56SMark Brown struct snd_soc_platform *platform = card->platform; 7603c4b266fSLiam Girdwood struct snd_soc_dai *cpu_dai = machine->cpu_dai; 7613c4b266fSLiam Girdwood struct snd_soc_dai *codec_dai = machine->codec_dai; 7626627a653SMark Brown struct snd_soc_codec *codec = card->codec; 763db2a4165SFrank Mandarino 764db2a4165SFrank Mandarino mutex_lock(&pcm_mutex); 765db2a4165SFrank Mandarino 766db2a4165SFrank Mandarino /* apply codec digital mute */ 7678c6529dbSLiam Girdwood if (!codec->active) 7688c6529dbSLiam Girdwood snd_soc_dai_digital_mute(codec_dai, 1); 769db2a4165SFrank Mandarino 770db2a4165SFrank Mandarino /* free any machine hw params */ 771db2a4165SFrank Mandarino if (machine->ops && machine->ops->hw_free) 772db2a4165SFrank Mandarino machine->ops->hw_free(substream); 773db2a4165SFrank Mandarino 774db2a4165SFrank Mandarino /* free any DMA resources */ 775db2a4165SFrank Mandarino if (platform->pcm_ops->hw_free) 776db2a4165SFrank Mandarino platform->pcm_ops->hw_free(substream); 777db2a4165SFrank Mandarino 778db2a4165SFrank Mandarino /* now free hw params for the DAI's */ 7796335d055SEric Miao if (codec_dai->ops->hw_free) 7806335d055SEric Miao codec_dai->ops->hw_free(substream, codec_dai); 781db2a4165SFrank Mandarino 7826335d055SEric Miao if (cpu_dai->ops->hw_free) 7836335d055SEric Miao cpu_dai->ops->hw_free(substream, cpu_dai); 784db2a4165SFrank Mandarino 785db2a4165SFrank Mandarino mutex_unlock(&pcm_mutex); 786db2a4165SFrank Mandarino return 0; 787db2a4165SFrank Mandarino } 788db2a4165SFrank Mandarino 789db2a4165SFrank Mandarino static int soc_pcm_trigger(struct snd_pcm_substream *substream, int cmd) 790db2a4165SFrank Mandarino { 791db2a4165SFrank Mandarino struct snd_soc_pcm_runtime *rtd = substream->private_data; 792db2a4165SFrank Mandarino struct snd_soc_device *socdev = rtd->socdev; 79387689d56SMark Brown struct snd_soc_card *card= socdev->card; 794cb666e5bSLiam Girdwood struct snd_soc_dai_link *machine = rtd->dai; 79587689d56SMark Brown struct snd_soc_platform *platform = card->platform; 7963c4b266fSLiam Girdwood struct snd_soc_dai *cpu_dai = machine->cpu_dai; 7973c4b266fSLiam Girdwood struct snd_soc_dai *codec_dai = machine->codec_dai; 798db2a4165SFrank Mandarino int ret; 799db2a4165SFrank Mandarino 8006335d055SEric Miao if (codec_dai->ops->trigger) { 8016335d055SEric Miao ret = codec_dai->ops->trigger(substream, cmd, codec_dai); 802db2a4165SFrank Mandarino if (ret < 0) 803db2a4165SFrank Mandarino return ret; 804db2a4165SFrank Mandarino } 805db2a4165SFrank Mandarino 806db2a4165SFrank Mandarino if (platform->pcm_ops->trigger) { 807db2a4165SFrank Mandarino ret = platform->pcm_ops->trigger(substream, cmd); 808db2a4165SFrank Mandarino if (ret < 0) 809db2a4165SFrank Mandarino return ret; 810db2a4165SFrank Mandarino } 811db2a4165SFrank Mandarino 8126335d055SEric Miao if (cpu_dai->ops->trigger) { 8136335d055SEric Miao ret = cpu_dai->ops->trigger(substream, cmd, cpu_dai); 814db2a4165SFrank Mandarino if (ret < 0) 815db2a4165SFrank Mandarino return ret; 816db2a4165SFrank Mandarino } 817db2a4165SFrank Mandarino return 0; 818db2a4165SFrank Mandarino } 819db2a4165SFrank Mandarino 820377b6f62SPeter Ujfalusi /* 821377b6f62SPeter Ujfalusi * soc level wrapper for pointer callback 822258020d0SPeter Ujfalusi * If cpu_dai, codec_dai, platform driver has the delay callback, than 823258020d0SPeter Ujfalusi * the runtime->delay will be updated accordingly. 824377b6f62SPeter Ujfalusi */ 825377b6f62SPeter Ujfalusi static snd_pcm_uframes_t soc_pcm_pointer(struct snd_pcm_substream *substream) 826377b6f62SPeter Ujfalusi { 827377b6f62SPeter Ujfalusi struct snd_soc_pcm_runtime *rtd = substream->private_data; 828377b6f62SPeter Ujfalusi struct snd_soc_device *socdev = rtd->socdev; 829377b6f62SPeter Ujfalusi struct snd_soc_card *card = socdev->card; 830377b6f62SPeter Ujfalusi struct snd_soc_platform *platform = card->platform; 831258020d0SPeter Ujfalusi struct snd_soc_dai_link *machine = rtd->dai; 832258020d0SPeter Ujfalusi struct snd_soc_dai *cpu_dai = machine->cpu_dai; 833258020d0SPeter Ujfalusi struct snd_soc_dai *codec_dai = machine->codec_dai; 834258020d0SPeter Ujfalusi struct snd_pcm_runtime *runtime = substream->runtime; 835377b6f62SPeter Ujfalusi snd_pcm_uframes_t offset = 0; 836258020d0SPeter Ujfalusi snd_pcm_sframes_t delay = 0; 837377b6f62SPeter Ujfalusi 838377b6f62SPeter Ujfalusi if (platform->pcm_ops->pointer) 839377b6f62SPeter Ujfalusi offset = platform->pcm_ops->pointer(substream); 840377b6f62SPeter Ujfalusi 841258020d0SPeter Ujfalusi if (cpu_dai->ops->delay) 842258020d0SPeter Ujfalusi delay += cpu_dai->ops->delay(substream, cpu_dai); 843258020d0SPeter Ujfalusi 844258020d0SPeter Ujfalusi if (codec_dai->ops->delay) 845258020d0SPeter Ujfalusi delay += codec_dai->ops->delay(substream, codec_dai); 846258020d0SPeter Ujfalusi 847258020d0SPeter Ujfalusi if (platform->delay) 848258020d0SPeter Ujfalusi delay += platform->delay(substream, codec_dai); 849258020d0SPeter Ujfalusi 850258020d0SPeter Ujfalusi runtime->delay = delay; 851258020d0SPeter Ujfalusi 852377b6f62SPeter Ujfalusi return offset; 853377b6f62SPeter Ujfalusi } 854377b6f62SPeter Ujfalusi 855db2a4165SFrank Mandarino /* ASoC PCM operations */ 856db2a4165SFrank Mandarino static struct snd_pcm_ops soc_pcm_ops = { 857db2a4165SFrank Mandarino .open = soc_pcm_open, 858db2a4165SFrank Mandarino .close = soc_codec_close, 859db2a4165SFrank Mandarino .hw_params = soc_pcm_hw_params, 860db2a4165SFrank Mandarino .hw_free = soc_pcm_hw_free, 861db2a4165SFrank Mandarino .prepare = soc_pcm_prepare, 862db2a4165SFrank Mandarino .trigger = soc_pcm_trigger, 863377b6f62SPeter Ujfalusi .pointer = soc_pcm_pointer, 864db2a4165SFrank Mandarino }; 865db2a4165SFrank Mandarino 866db2a4165SFrank Mandarino #ifdef CONFIG_PM 867db2a4165SFrank Mandarino /* powers down audio subsystem for suspend */ 868416356fcSMark Brown static int soc_suspend(struct device *dev) 869db2a4165SFrank Mandarino { 870416356fcSMark Brown struct platform_device *pdev = to_platform_device(dev); 871db2a4165SFrank Mandarino struct snd_soc_device *socdev = platform_get_drvdata(pdev); 87287506549SMark Brown struct snd_soc_card *card = socdev->card; 87387689d56SMark Brown struct snd_soc_platform *platform = card->platform; 874db2a4165SFrank Mandarino struct snd_soc_codec_device *codec_dev = socdev->codec_dev; 8756627a653SMark Brown struct snd_soc_codec *codec = card->codec; 876db2a4165SFrank Mandarino int i; 877db2a4165SFrank Mandarino 878e3509ff0SDaniel Mack /* If the initialization of this soc device failed, there is no codec 879e3509ff0SDaniel Mack * associated with it. Just bail out in this case. 880e3509ff0SDaniel Mack */ 881e3509ff0SDaniel Mack if (!codec) 882e3509ff0SDaniel Mack return 0; 883e3509ff0SDaniel Mack 8846ed25978SAndy Green /* Due to the resume being scheduled into a workqueue we could 8856ed25978SAndy Green * suspend before that's finished - wait for it to complete. 8866ed25978SAndy Green */ 8876ed25978SAndy Green snd_power_lock(codec->card); 8886ed25978SAndy Green snd_power_wait(codec->card, SNDRV_CTL_POWER_D0); 8896ed25978SAndy Green snd_power_unlock(codec->card); 8906ed25978SAndy Green 8916ed25978SAndy Green /* we're going to block userspace touching us until resume completes */ 8926ed25978SAndy Green snd_power_change_state(codec->card, SNDRV_CTL_POWER_D3hot); 8936ed25978SAndy Green 894db2a4165SFrank Mandarino /* mute any active DAC's */ 895dee89c4dSMark Brown for (i = 0; i < card->num_links; i++) { 896dee89c4dSMark Brown struct snd_soc_dai *dai = card->dai_link[i].codec_dai; 8973efab7dcSMark Brown 8983efab7dcSMark Brown if (card->dai_link[i].ignore_suspend) 8993efab7dcSMark Brown continue; 9003efab7dcSMark Brown 9016335d055SEric Miao if (dai->ops->digital_mute && dai->playback.active) 9026335d055SEric Miao dai->ops->digital_mute(dai, 1); 903db2a4165SFrank Mandarino } 904db2a4165SFrank Mandarino 9054ccab3e7SLiam Girdwood /* suspend all pcms */ 9063efab7dcSMark Brown for (i = 0; i < card->num_links; i++) { 9073efab7dcSMark Brown if (card->dai_link[i].ignore_suspend) 9083efab7dcSMark Brown continue; 9093efab7dcSMark Brown 91087506549SMark Brown snd_pcm_suspend_all(card->dai_link[i].pcm); 9113efab7dcSMark Brown } 9124ccab3e7SLiam Girdwood 91387506549SMark Brown if (card->suspend_pre) 914416356fcSMark Brown card->suspend_pre(pdev, PMSG_SUSPEND); 915db2a4165SFrank Mandarino 91687506549SMark Brown for (i = 0; i < card->num_links; i++) { 91787506549SMark Brown struct snd_soc_dai *cpu_dai = card->dai_link[i].cpu_dai; 9183efab7dcSMark Brown 9193efab7dcSMark Brown if (card->dai_link[i].ignore_suspend) 9203efab7dcSMark Brown continue; 9213efab7dcSMark Brown 9223ba9e10aSMark Brown if (cpu_dai->suspend && !cpu_dai->ac97_control) 923dc7d7b83SMark Brown cpu_dai->suspend(cpu_dai); 924db2a4165SFrank Mandarino if (platform->suspend) 925d273ebe7Sjassi brar platform->suspend(&card->dai_link[i]); 926db2a4165SFrank Mandarino } 927db2a4165SFrank Mandarino 928db2a4165SFrank Mandarino /* close any waiting streams and save state */ 9296308419aSMark Brown run_delayed_work(&card->delayed_work); 9300be9898aSMark Brown codec->suspend_bias_level = codec->bias_level; 931db2a4165SFrank Mandarino 932db2a4165SFrank Mandarino for (i = 0; i < codec->num_dai; i++) { 933db2a4165SFrank Mandarino char *stream = codec->dai[i].playback.stream_name; 9343efab7dcSMark Brown 9353efab7dcSMark Brown if (card->dai_link[i].ignore_suspend) 9363efab7dcSMark Brown continue; 9373efab7dcSMark Brown 938db2a4165SFrank Mandarino if (stream != NULL) 939db2a4165SFrank Mandarino snd_soc_dapm_stream_event(codec, stream, 940db2a4165SFrank Mandarino SND_SOC_DAPM_STREAM_SUSPEND); 941db2a4165SFrank Mandarino stream = codec->dai[i].capture.stream_name; 942db2a4165SFrank Mandarino if (stream != NULL) 943db2a4165SFrank Mandarino snd_soc_dapm_stream_event(codec, stream, 944db2a4165SFrank Mandarino SND_SOC_DAPM_STREAM_SUSPEND); 945db2a4165SFrank Mandarino } 946db2a4165SFrank Mandarino 9471547aba9SMark Brown /* If there are paths active then the CODEC will be held with 9481547aba9SMark Brown * bias _ON and should not be suspended. */ 9491547aba9SMark Brown if (codec_dev->suspend) { 9501547aba9SMark Brown switch (codec->bias_level) { 9511547aba9SMark Brown case SND_SOC_BIAS_STANDBY: 9521547aba9SMark Brown case SND_SOC_BIAS_OFF: 953416356fcSMark Brown codec_dev->suspend(pdev, PMSG_SUSPEND); 9541547aba9SMark Brown break; 9551547aba9SMark Brown default: 9561547aba9SMark Brown dev_dbg(socdev->dev, "CODEC is on over suspend\n"); 9571547aba9SMark Brown break; 9581547aba9SMark Brown } 9591547aba9SMark Brown } 960db2a4165SFrank Mandarino 96187506549SMark Brown for (i = 0; i < card->num_links; i++) { 96287506549SMark Brown struct snd_soc_dai *cpu_dai = card->dai_link[i].cpu_dai; 9633efab7dcSMark Brown 9643efab7dcSMark Brown if (card->dai_link[i].ignore_suspend) 9653efab7dcSMark Brown continue; 9663efab7dcSMark Brown 9673ba9e10aSMark Brown if (cpu_dai->suspend && cpu_dai->ac97_control) 968dc7d7b83SMark Brown cpu_dai->suspend(cpu_dai); 969db2a4165SFrank Mandarino } 970db2a4165SFrank Mandarino 97187506549SMark Brown if (card->suspend_post) 972416356fcSMark Brown card->suspend_post(pdev, PMSG_SUSPEND); 973db2a4165SFrank Mandarino 974db2a4165SFrank Mandarino return 0; 975db2a4165SFrank Mandarino } 976db2a4165SFrank Mandarino 9776ed25978SAndy Green /* deferred resume work, so resume can complete before we finished 9786ed25978SAndy Green * setting our codec back up, which can be very slow on I2C 9796ed25978SAndy Green */ 9806ed25978SAndy Green static void soc_resume_deferred(struct work_struct *work) 981db2a4165SFrank Mandarino { 9826308419aSMark Brown struct snd_soc_card *card = container_of(work, 9836308419aSMark Brown struct snd_soc_card, 9846ed25978SAndy Green deferred_resume_work); 9856308419aSMark Brown struct snd_soc_device *socdev = card->socdev; 98687689d56SMark Brown struct snd_soc_platform *platform = card->platform; 987db2a4165SFrank Mandarino struct snd_soc_codec_device *codec_dev = socdev->codec_dev; 9886627a653SMark Brown struct snd_soc_codec *codec = card->codec; 9896ed25978SAndy Green struct platform_device *pdev = to_platform_device(socdev->dev); 990db2a4165SFrank Mandarino int i; 991db2a4165SFrank Mandarino 9926ed25978SAndy Green /* our power state is still SNDRV_CTL_POWER_D3hot from suspend time, 9936ed25978SAndy Green * so userspace apps are blocked from touching us 9946ed25978SAndy Green */ 9956ed25978SAndy Green 996fde22f27SMark Brown dev_dbg(socdev->dev, "starting resume work\n"); 9976ed25978SAndy Green 9989949788bSMark Brown /* Bring us up into D2 so that DAPM starts enabling things */ 9999949788bSMark Brown snd_power_change_state(codec->card, SNDRV_CTL_POWER_D2); 10009949788bSMark Brown 100187506549SMark Brown if (card->resume_pre) 100287506549SMark Brown card->resume_pre(pdev); 1003db2a4165SFrank Mandarino 100487506549SMark Brown for (i = 0; i < card->num_links; i++) { 100587506549SMark Brown struct snd_soc_dai *cpu_dai = card->dai_link[i].cpu_dai; 10063efab7dcSMark Brown 10073efab7dcSMark Brown if (card->dai_link[i].ignore_suspend) 10083efab7dcSMark Brown continue; 10093efab7dcSMark Brown 10103ba9e10aSMark Brown if (cpu_dai->resume && cpu_dai->ac97_control) 1011dc7d7b83SMark Brown cpu_dai->resume(cpu_dai); 1012db2a4165SFrank Mandarino } 1013db2a4165SFrank Mandarino 10141547aba9SMark Brown /* If the CODEC was idle over suspend then it will have been 10151547aba9SMark Brown * left with bias OFF or STANDBY and suspended so we must now 10161547aba9SMark Brown * resume. Otherwise the suspend was suppressed. 10171547aba9SMark Brown */ 10181547aba9SMark Brown if (codec_dev->resume) { 10191547aba9SMark Brown switch (codec->bias_level) { 10201547aba9SMark Brown case SND_SOC_BIAS_STANDBY: 10211547aba9SMark Brown case SND_SOC_BIAS_OFF: 1022db2a4165SFrank Mandarino codec_dev->resume(pdev); 10231547aba9SMark Brown break; 10241547aba9SMark Brown default: 10251547aba9SMark Brown dev_dbg(socdev->dev, "CODEC was on over suspend\n"); 10261547aba9SMark Brown break; 10271547aba9SMark Brown } 10281547aba9SMark Brown } 1029db2a4165SFrank Mandarino 1030db2a4165SFrank Mandarino for (i = 0; i < codec->num_dai; i++) { 1031db2a4165SFrank Mandarino char *stream = codec->dai[i].playback.stream_name; 10323efab7dcSMark Brown 10333efab7dcSMark Brown if (card->dai_link[i].ignore_suspend) 10343efab7dcSMark Brown continue; 10353efab7dcSMark Brown 1036db2a4165SFrank Mandarino if (stream != NULL) 1037db2a4165SFrank Mandarino snd_soc_dapm_stream_event(codec, stream, 1038db2a4165SFrank Mandarino SND_SOC_DAPM_STREAM_RESUME); 1039db2a4165SFrank Mandarino stream = codec->dai[i].capture.stream_name; 1040db2a4165SFrank Mandarino if (stream != NULL) 1041db2a4165SFrank Mandarino snd_soc_dapm_stream_event(codec, stream, 1042db2a4165SFrank Mandarino SND_SOC_DAPM_STREAM_RESUME); 1043db2a4165SFrank Mandarino } 1044db2a4165SFrank Mandarino 10453ff3f64bSMark Brown /* unmute any active DACs */ 1046dee89c4dSMark Brown for (i = 0; i < card->num_links; i++) { 1047dee89c4dSMark Brown struct snd_soc_dai *dai = card->dai_link[i].codec_dai; 10483efab7dcSMark Brown 10493efab7dcSMark Brown if (card->dai_link[i].ignore_suspend) 10503efab7dcSMark Brown continue; 10513efab7dcSMark Brown 10526335d055SEric Miao if (dai->ops->digital_mute && dai->playback.active) 10536335d055SEric Miao dai->ops->digital_mute(dai, 0); 1054db2a4165SFrank Mandarino } 1055db2a4165SFrank Mandarino 105687506549SMark Brown for (i = 0; i < card->num_links; i++) { 105787506549SMark Brown struct snd_soc_dai *cpu_dai = card->dai_link[i].cpu_dai; 10583efab7dcSMark Brown 10593efab7dcSMark Brown if (card->dai_link[i].ignore_suspend) 10603efab7dcSMark Brown continue; 10613efab7dcSMark Brown 10623ba9e10aSMark Brown if (cpu_dai->resume && !cpu_dai->ac97_control) 1063dc7d7b83SMark Brown cpu_dai->resume(cpu_dai); 1064db2a4165SFrank Mandarino if (platform->resume) 1065d273ebe7Sjassi brar platform->resume(&card->dai_link[i]); 1066db2a4165SFrank Mandarino } 1067db2a4165SFrank Mandarino 106887506549SMark Brown if (card->resume_post) 106987506549SMark Brown card->resume_post(pdev); 1070db2a4165SFrank Mandarino 1071fde22f27SMark Brown dev_dbg(socdev->dev, "resume work completed\n"); 10726ed25978SAndy Green 10736ed25978SAndy Green /* userspace can access us now we are back as we were before */ 10746ed25978SAndy Green snd_power_change_state(codec->card, SNDRV_CTL_POWER_D0); 10756ed25978SAndy Green } 10766ed25978SAndy Green 10776ed25978SAndy Green /* powers up audio subsystem after a suspend */ 1078416356fcSMark Brown static int soc_resume(struct device *dev) 10796ed25978SAndy Green { 1080416356fcSMark Brown struct platform_device *pdev = to_platform_device(dev); 10816ed25978SAndy Green struct snd_soc_device *socdev = platform_get_drvdata(pdev); 10826308419aSMark Brown struct snd_soc_card *card = socdev->card; 108364ab9baaSMark Brown struct snd_soc_dai *cpu_dai = card->dai_link[0].cpu_dai; 10846ed25978SAndy Green 1085b9dd94a8SPeter Ujfalusi /* If the initialization of this soc device failed, there is no codec 1086b9dd94a8SPeter Ujfalusi * associated with it. Just bail out in this case. 1087b9dd94a8SPeter Ujfalusi */ 1088b9dd94a8SPeter Ujfalusi if (!card->codec) 1089b9dd94a8SPeter Ujfalusi return 0; 1090b9dd94a8SPeter Ujfalusi 109164ab9baaSMark Brown /* AC97 devices might have other drivers hanging off them so 109264ab9baaSMark Brown * need to resume immediately. Other drivers don't have that 109364ab9baaSMark Brown * problem and may take a substantial amount of time to resume 109464ab9baaSMark Brown * due to I/O costs and anti-pop so handle them out of line. 109564ab9baaSMark Brown */ 109664ab9baaSMark Brown if (cpu_dai->ac97_control) { 109764ab9baaSMark Brown dev_dbg(socdev->dev, "Resuming AC97 immediately\n"); 109864ab9baaSMark Brown soc_resume_deferred(&card->deferred_resume_work); 109964ab9baaSMark Brown } else { 110064ab9baaSMark Brown dev_dbg(socdev->dev, "Scheduling resume work\n"); 11016308419aSMark Brown if (!schedule_work(&card->deferred_resume_work)) 1102fde22f27SMark Brown dev_err(socdev->dev, "resume work item may be lost\n"); 110364ab9baaSMark Brown } 11046ed25978SAndy Green 1105db2a4165SFrank Mandarino return 0; 1106db2a4165SFrank Mandarino } 1107db2a4165SFrank Mandarino #else 1108db2a4165SFrank Mandarino #define soc_suspend NULL 1109db2a4165SFrank Mandarino #define soc_resume NULL 1110db2a4165SFrank Mandarino #endif 1111db2a4165SFrank Mandarino 111202a06d30SBarry Song static struct snd_soc_dai_ops null_dai_ops = { 111302a06d30SBarry Song }; 111402a06d30SBarry Song 1115435c5e25SMark Brown static void snd_soc_instantiate_card(struct snd_soc_card *card) 1116db2a4165SFrank Mandarino { 1117435c5e25SMark Brown struct platform_device *pdev = container_of(card->dev, 1118435c5e25SMark Brown struct platform_device, 1119435c5e25SMark Brown dev); 1120435c5e25SMark Brown struct snd_soc_codec_device *codec_dev = card->socdev->codec_dev; 1121fe3e78e0SMark Brown struct snd_soc_codec *codec; 1122435c5e25SMark Brown struct snd_soc_platform *platform; 1123435c5e25SMark Brown struct snd_soc_dai *dai; 11246b05eda6SMark Brown int i, found, ret, ac97; 1125db2a4165SFrank Mandarino 1126435c5e25SMark Brown if (card->instantiated) 1127435c5e25SMark Brown return; 11286308419aSMark Brown 1129435c5e25SMark Brown found = 0; 1130435c5e25SMark Brown list_for_each_entry(platform, &platform_list, list) 1131435c5e25SMark Brown if (card->platform == platform) { 1132435c5e25SMark Brown found = 1; 1133435c5e25SMark Brown break; 1134435c5e25SMark Brown } 1135435c5e25SMark Brown if (!found) { 1136435c5e25SMark Brown dev_dbg(card->dev, "Platform %s not registered\n", 1137435c5e25SMark Brown card->platform->name); 1138435c5e25SMark Brown return; 1139c5af3a2eSMark Brown } 1140c5af3a2eSMark Brown 11416b05eda6SMark Brown ac97 = 0; 1142435c5e25SMark Brown for (i = 0; i < card->num_links; i++) { 1143435c5e25SMark Brown found = 0; 1144435c5e25SMark Brown list_for_each_entry(dai, &dai_list, list) 1145435c5e25SMark Brown if (card->dai_link[i].cpu_dai == dai) { 1146435c5e25SMark Brown found = 1; 1147435c5e25SMark Brown break; 1148435c5e25SMark Brown } 1149435c5e25SMark Brown if (!found) { 1150435c5e25SMark Brown dev_dbg(card->dev, "DAI %s not registered\n", 1151435c5e25SMark Brown card->dai_link[i].cpu_dai->name); 1152435c5e25SMark Brown return; 1153435c5e25SMark Brown } 11546b05eda6SMark Brown 11556b05eda6SMark Brown if (card->dai_link[i].cpu_dai->ac97_control) 11566b05eda6SMark Brown ac97 = 1; 11576b05eda6SMark Brown } 11586b05eda6SMark Brown 115902a06d30SBarry Song for (i = 0; i < card->num_links; i++) { 116002a06d30SBarry Song if (!card->dai_link[i].codec_dai->ops) 116102a06d30SBarry Song card->dai_link[i].codec_dai->ops = &null_dai_ops; 116202a06d30SBarry Song } 116302a06d30SBarry Song 11646b05eda6SMark Brown /* If we have AC97 in the system then don't wait for the 11656b05eda6SMark Brown * codec. This will need revisiting if we have to handle 11666b05eda6SMark Brown * systems with mixed AC97 and non-AC97 parts. Only check for 11676b05eda6SMark Brown * DAIs currently; we can't do this per link since some AC97 11686b05eda6SMark Brown * codecs have non-AC97 DAIs. 11696b05eda6SMark Brown */ 11706b05eda6SMark Brown if (!ac97) 11716b05eda6SMark Brown for (i = 0; i < card->num_links; i++) { 11726b05eda6SMark Brown found = 0; 11736b05eda6SMark Brown list_for_each_entry(dai, &dai_list, list) 11746b05eda6SMark Brown if (card->dai_link[i].codec_dai == dai) { 11756b05eda6SMark Brown found = 1; 11766b05eda6SMark Brown break; 11776b05eda6SMark Brown } 11786b05eda6SMark Brown if (!found) { 11796b05eda6SMark Brown dev_dbg(card->dev, "DAI %s not registered\n", 11806b05eda6SMark Brown card->dai_link[i].codec_dai->name); 11816b05eda6SMark Brown return; 11826b05eda6SMark Brown } 1183435c5e25SMark Brown } 1184435c5e25SMark Brown 1185435c5e25SMark Brown /* Note that we do not current check for codec components */ 1186435c5e25SMark Brown 1187435c5e25SMark Brown dev_dbg(card->dev, "All components present, instantiating\n"); 1188435c5e25SMark Brown 1189435c5e25SMark Brown /* Found everything, bring it up */ 119096dd3622SMark Brown card->pmdown_time = pmdown_time; 119196dd3622SMark Brown 119287506549SMark Brown if (card->probe) { 119387506549SMark Brown ret = card->probe(pdev); 1194db2a4165SFrank Mandarino if (ret < 0) 1195435c5e25SMark Brown return; 1196db2a4165SFrank Mandarino } 1197db2a4165SFrank Mandarino 119887506549SMark Brown for (i = 0; i < card->num_links; i++) { 119987506549SMark Brown struct snd_soc_dai *cpu_dai = card->dai_link[i].cpu_dai; 1200db2a4165SFrank Mandarino if (cpu_dai->probe) { 1201bdb92876SMark Brown ret = cpu_dai->probe(pdev, cpu_dai); 1202db2a4165SFrank Mandarino if (ret < 0) 1203db2a4165SFrank Mandarino goto cpu_dai_err; 1204db2a4165SFrank Mandarino } 1205db2a4165SFrank Mandarino } 1206db2a4165SFrank Mandarino 1207db2a4165SFrank Mandarino if (codec_dev->probe) { 1208db2a4165SFrank Mandarino ret = codec_dev->probe(pdev); 1209db2a4165SFrank Mandarino if (ret < 0) 1210db2a4165SFrank Mandarino goto cpu_dai_err; 1211db2a4165SFrank Mandarino } 1212fe3e78e0SMark Brown codec = card->codec; 1213db2a4165SFrank Mandarino 1214db2a4165SFrank Mandarino if (platform->probe) { 1215db2a4165SFrank Mandarino ret = platform->probe(pdev); 1216db2a4165SFrank Mandarino if (ret < 0) 1217db2a4165SFrank Mandarino goto platform_err; 1218db2a4165SFrank Mandarino } 1219db2a4165SFrank Mandarino 1220db2a4165SFrank Mandarino /* DAPM stream work */ 12216308419aSMark Brown INIT_DELAYED_WORK(&card->delayed_work, close_delayed_work); 12221301a964SRandy Dunlap #ifdef CONFIG_PM 12236ed25978SAndy Green /* deferred resume work */ 12246308419aSMark Brown INIT_WORK(&card->deferred_resume_work, soc_resume_deferred); 12251301a964SRandy Dunlap #endif 12266ed25978SAndy Green 1227fe3e78e0SMark Brown for (i = 0; i < card->num_links; i++) { 1228fe3e78e0SMark Brown if (card->dai_link[i].init) { 1229fe3e78e0SMark Brown ret = card->dai_link[i].init(codec); 1230fe3e78e0SMark Brown if (ret < 0) { 1231fe3e78e0SMark Brown printk(KERN_ERR "asoc: failed to init %s\n", 1232fe3e78e0SMark Brown card->dai_link[i].stream_name); 1233fe3e78e0SMark Brown continue; 1234fe3e78e0SMark Brown } 1235fe3e78e0SMark Brown } 1236f7732053SBarry Song if (card->dai_link[i].codec_dai->ac97_control) 1237fe3e78e0SMark Brown ac97 = 1; 1238fe3e78e0SMark Brown } 1239fe3e78e0SMark Brown 1240fe3e78e0SMark Brown snprintf(codec->card->shortname, sizeof(codec->card->shortname), 1241fe3e78e0SMark Brown "%s", card->name); 1242fe3e78e0SMark Brown snprintf(codec->card->longname, sizeof(codec->card->longname), 1243fe3e78e0SMark Brown "%s (%s)", card->name, codec->name); 1244fe3e78e0SMark Brown 1245fe3e78e0SMark Brown /* Make sure all DAPM widgets are instantiated */ 1246fe3e78e0SMark Brown snd_soc_dapm_new_widgets(codec); 1247fe3e78e0SMark Brown 1248fe3e78e0SMark Brown ret = snd_card_register(codec->card); 1249fe3e78e0SMark Brown if (ret < 0) { 1250fe3e78e0SMark Brown printk(KERN_ERR "asoc: failed to register soundcard for %s\n", 1251fe3e78e0SMark Brown codec->name); 1252fe3e78e0SMark Brown goto card_err; 1253fe3e78e0SMark Brown } 1254fe3e78e0SMark Brown 1255fe3e78e0SMark Brown mutex_lock(&codec->mutex); 1256fe3e78e0SMark Brown #ifdef CONFIG_SND_SOC_AC97_BUS 1257fe3e78e0SMark Brown /* Only instantiate AC97 if not already done by the adaptor 1258fe3e78e0SMark Brown * for the generic AC97 subsystem. 1259fe3e78e0SMark Brown */ 1260fe3e78e0SMark Brown if (ac97 && strcmp(codec->name, "AC97") != 0) { 1261fe3e78e0SMark Brown ret = soc_ac97_dev_register(codec); 1262fe3e78e0SMark Brown if (ret < 0) { 1263fe3e78e0SMark Brown printk(KERN_ERR "asoc: AC97 device register failed\n"); 1264fe3e78e0SMark Brown snd_card_free(codec->card); 1265fe3e78e0SMark Brown mutex_unlock(&codec->mutex); 1266fe3e78e0SMark Brown goto card_err; 1267fe3e78e0SMark Brown } 1268fe3e78e0SMark Brown } 1269fe3e78e0SMark Brown #endif 1270fe3e78e0SMark Brown 1271fe3e78e0SMark Brown ret = snd_soc_dapm_sys_add(card->socdev->dev); 1272fe3e78e0SMark Brown if (ret < 0) 1273fe3e78e0SMark Brown printk(KERN_WARNING "asoc: failed to add dapm sysfs entries\n"); 1274fe3e78e0SMark Brown 1275dbe21408SMark Brown ret = device_create_file(card->socdev->dev, &dev_attr_pmdown_time); 1276dbe21408SMark Brown if (ret < 0) 1277dbe21408SMark Brown printk(KERN_WARNING "asoc: failed to add pmdown_time sysfs\n"); 1278dbe21408SMark Brown 1279fe3e78e0SMark Brown ret = device_create_file(card->socdev->dev, &dev_attr_codec_reg); 1280fe3e78e0SMark Brown if (ret < 0) 1281fe3e78e0SMark Brown printk(KERN_WARNING "asoc: failed to add codec sysfs files\n"); 1282fe3e78e0SMark Brown 1283fe3e78e0SMark Brown soc_init_codec_debugfs(codec); 1284fe3e78e0SMark Brown mutex_unlock(&codec->mutex); 1285fe3e78e0SMark Brown 1286435c5e25SMark Brown card->instantiated = 1; 1287435c5e25SMark Brown 1288435c5e25SMark Brown return; 1289db2a4165SFrank Mandarino 1290fe3e78e0SMark Brown card_err: 1291fe3e78e0SMark Brown if (platform->remove) 1292fe3e78e0SMark Brown platform->remove(pdev); 1293fe3e78e0SMark Brown 1294db2a4165SFrank Mandarino platform_err: 1295db2a4165SFrank Mandarino if (codec_dev->remove) 1296db2a4165SFrank Mandarino codec_dev->remove(pdev); 1297db2a4165SFrank Mandarino 1298db2a4165SFrank Mandarino cpu_dai_err: 129918b9b3d9SLiam Girdwood for (i--; i >= 0; i--) { 130087506549SMark Brown struct snd_soc_dai *cpu_dai = card->dai_link[i].cpu_dai; 1301db2a4165SFrank Mandarino if (cpu_dai->remove) 1302bdb92876SMark Brown cpu_dai->remove(pdev, cpu_dai); 1303db2a4165SFrank Mandarino } 1304db2a4165SFrank Mandarino 130587506549SMark Brown if (card->remove) 130687506549SMark Brown card->remove(pdev); 1307435c5e25SMark Brown } 1308db2a4165SFrank Mandarino 1309435c5e25SMark Brown /* 1310435c5e25SMark Brown * Attempt to initialise any uninitalised cards. Must be called with 1311435c5e25SMark Brown * client_mutex. 1312435c5e25SMark Brown */ 1313435c5e25SMark Brown static void snd_soc_instantiate_cards(void) 1314435c5e25SMark Brown { 1315435c5e25SMark Brown struct snd_soc_card *card; 1316435c5e25SMark Brown list_for_each_entry(card, &card_list, list) 1317435c5e25SMark Brown snd_soc_instantiate_card(card); 1318435c5e25SMark Brown } 1319435c5e25SMark Brown 1320435c5e25SMark Brown /* probes a new socdev */ 1321435c5e25SMark Brown static int soc_probe(struct platform_device *pdev) 1322435c5e25SMark Brown { 1323435c5e25SMark Brown int ret = 0; 1324435c5e25SMark Brown struct snd_soc_device *socdev = platform_get_drvdata(pdev); 1325435c5e25SMark Brown struct snd_soc_card *card = socdev->card; 1326435c5e25SMark Brown 1327435c5e25SMark Brown /* Bodge while we push things out of socdev */ 1328435c5e25SMark Brown card->socdev = socdev; 1329435c5e25SMark Brown 1330435c5e25SMark Brown /* Bodge while we unpick instantiation */ 1331435c5e25SMark Brown card->dev = &pdev->dev; 1332435c5e25SMark Brown ret = snd_soc_register_card(card); 1333435c5e25SMark Brown if (ret != 0) { 1334435c5e25SMark Brown dev_err(&pdev->dev, "Failed to register card\n"); 1335db2a4165SFrank Mandarino return ret; 1336db2a4165SFrank Mandarino } 1337db2a4165SFrank Mandarino 1338435c5e25SMark Brown return 0; 1339435c5e25SMark Brown } 1340435c5e25SMark Brown 1341db2a4165SFrank Mandarino /* removes a socdev */ 1342db2a4165SFrank Mandarino static int soc_remove(struct platform_device *pdev) 1343db2a4165SFrank Mandarino { 1344db2a4165SFrank Mandarino int i; 1345db2a4165SFrank Mandarino struct snd_soc_device *socdev = platform_get_drvdata(pdev); 134687506549SMark Brown struct snd_soc_card *card = socdev->card; 134787689d56SMark Brown struct snd_soc_platform *platform = card->platform; 1348db2a4165SFrank Mandarino struct snd_soc_codec_device *codec_dev = socdev->codec_dev; 1349db2a4165SFrank Mandarino 1350b2dfa62cSGuennadi Liakhovetski if (card->instantiated) { 13516308419aSMark Brown run_delayed_work(&card->delayed_work); 1352965ac42cSLiam Girdwood 1353db2a4165SFrank Mandarino if (platform->remove) 1354db2a4165SFrank Mandarino platform->remove(pdev); 1355db2a4165SFrank Mandarino 1356db2a4165SFrank Mandarino if (codec_dev->remove) 1357db2a4165SFrank Mandarino codec_dev->remove(pdev); 1358db2a4165SFrank Mandarino 135987506549SMark Brown for (i = 0; i < card->num_links; i++) { 136087506549SMark Brown struct snd_soc_dai *cpu_dai = card->dai_link[i].cpu_dai; 1361db2a4165SFrank Mandarino if (cpu_dai->remove) 1362bdb92876SMark Brown cpu_dai->remove(pdev, cpu_dai); 1363db2a4165SFrank Mandarino } 1364db2a4165SFrank Mandarino 136587506549SMark Brown if (card->remove) 136687506549SMark Brown card->remove(pdev); 1367b2dfa62cSGuennadi Liakhovetski } 1368db2a4165SFrank Mandarino 1369c5af3a2eSMark Brown snd_soc_unregister_card(card); 1370c5af3a2eSMark Brown 1371db2a4165SFrank Mandarino return 0; 1372db2a4165SFrank Mandarino } 1373db2a4165SFrank Mandarino 1374416356fcSMark Brown static int soc_poweroff(struct device *dev) 137551737470SMark Brown { 1376416356fcSMark Brown struct platform_device *pdev = to_platform_device(dev); 137751737470SMark Brown struct snd_soc_device *socdev = platform_get_drvdata(pdev); 137851737470SMark Brown struct snd_soc_card *card = socdev->card; 137951737470SMark Brown 138051737470SMark Brown if (!card->instantiated) 1381416356fcSMark Brown return 0; 138251737470SMark Brown 138351737470SMark Brown /* Flush out pmdown_time work - we actually do want to run it 138451737470SMark Brown * now, we're shutting down so no imminent restart. */ 138551737470SMark Brown run_delayed_work(&card->delayed_work); 138651737470SMark Brown 138751737470SMark Brown snd_soc_dapm_shutdown(socdev); 1388416356fcSMark Brown 1389416356fcSMark Brown return 0; 139051737470SMark Brown } 139151737470SMark Brown 139247145210SAlexey Dobriyan static const struct dev_pm_ops soc_pm_ops = { 1393416356fcSMark Brown .suspend = soc_suspend, 1394416356fcSMark Brown .resume = soc_resume, 1395416356fcSMark Brown .poweroff = soc_poweroff, 1396416356fcSMark Brown }; 1397416356fcSMark Brown 1398db2a4165SFrank Mandarino /* ASoC platform driver */ 1399db2a4165SFrank Mandarino static struct platform_driver soc_driver = { 1400db2a4165SFrank Mandarino .driver = { 1401db2a4165SFrank Mandarino .name = "soc-audio", 14028b45a209SKay Sievers .owner = THIS_MODULE, 1403416356fcSMark Brown .pm = &soc_pm_ops, 1404db2a4165SFrank Mandarino }, 1405db2a4165SFrank Mandarino .probe = soc_probe, 1406db2a4165SFrank Mandarino .remove = soc_remove, 1407db2a4165SFrank Mandarino }; 1408db2a4165SFrank Mandarino 1409db2a4165SFrank Mandarino /* create a new pcm */ 1410db2a4165SFrank Mandarino static int soc_new_pcm(struct snd_soc_device *socdev, 1411db2a4165SFrank Mandarino struct snd_soc_dai_link *dai_link, int num) 1412db2a4165SFrank Mandarino { 141387689d56SMark Brown struct snd_soc_card *card = socdev->card; 14146627a653SMark Brown struct snd_soc_codec *codec = card->codec; 141587689d56SMark Brown struct snd_soc_platform *platform = card->platform; 14163c4b266fSLiam Girdwood struct snd_soc_dai *codec_dai = dai_link->codec_dai; 14173c4b266fSLiam Girdwood struct snd_soc_dai *cpu_dai = dai_link->cpu_dai; 1418db2a4165SFrank Mandarino struct snd_soc_pcm_runtime *rtd; 1419db2a4165SFrank Mandarino struct snd_pcm *pcm; 1420db2a4165SFrank Mandarino char new_name[64]; 1421db2a4165SFrank Mandarino int ret = 0, playback = 0, capture = 0; 1422db2a4165SFrank Mandarino 1423db2a4165SFrank Mandarino rtd = kzalloc(sizeof(struct snd_soc_pcm_runtime), GFP_KERNEL); 1424db2a4165SFrank Mandarino if (rtd == NULL) 1425db2a4165SFrank Mandarino return -ENOMEM; 1426cb666e5bSLiam Girdwood 1427cb666e5bSLiam Girdwood rtd->dai = dai_link; 1428db2a4165SFrank Mandarino rtd->socdev = socdev; 14296627a653SMark Brown codec_dai->codec = card->codec; 1430db2a4165SFrank Mandarino 1431db2a4165SFrank Mandarino /* check client and interface hw capabilities */ 143240ca1142SMark Brown snprintf(new_name, sizeof(new_name), "%s %s-%d", 143340ca1142SMark Brown dai_link->stream_name, codec_dai->name, num); 1434db2a4165SFrank Mandarino 1435db2a4165SFrank Mandarino if (codec_dai->playback.channels_min) 1436db2a4165SFrank Mandarino playback = 1; 1437db2a4165SFrank Mandarino if (codec_dai->capture.channels_min) 1438db2a4165SFrank Mandarino capture = 1; 1439db2a4165SFrank Mandarino 1440db2a4165SFrank Mandarino ret = snd_pcm_new(codec->card, new_name, codec->pcm_devs++, playback, 1441db2a4165SFrank Mandarino capture, &pcm); 1442db2a4165SFrank Mandarino if (ret < 0) { 14433ff3f64bSMark Brown printk(KERN_ERR "asoc: can't create pcm for codec %s\n", 14443ff3f64bSMark Brown codec->name); 1445db2a4165SFrank Mandarino kfree(rtd); 1446db2a4165SFrank Mandarino return ret; 1447db2a4165SFrank Mandarino } 1448db2a4165SFrank Mandarino 14494ccab3e7SLiam Girdwood dai_link->pcm = pcm; 1450db2a4165SFrank Mandarino pcm->private_data = rtd; 145187689d56SMark Brown soc_pcm_ops.mmap = platform->pcm_ops->mmap; 145287689d56SMark Brown soc_pcm_ops.ioctl = platform->pcm_ops->ioctl; 145387689d56SMark Brown soc_pcm_ops.copy = platform->pcm_ops->copy; 145487689d56SMark Brown soc_pcm_ops.silence = platform->pcm_ops->silence; 145587689d56SMark Brown soc_pcm_ops.ack = platform->pcm_ops->ack; 145687689d56SMark Brown soc_pcm_ops.page = platform->pcm_ops->page; 1457db2a4165SFrank Mandarino 1458db2a4165SFrank Mandarino if (playback) 1459db2a4165SFrank Mandarino snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &soc_pcm_ops); 1460db2a4165SFrank Mandarino 1461db2a4165SFrank Mandarino if (capture) 1462db2a4165SFrank Mandarino snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &soc_pcm_ops); 1463db2a4165SFrank Mandarino 146487689d56SMark Brown ret = platform->pcm_new(codec->card, codec_dai, pcm); 1465db2a4165SFrank Mandarino if (ret < 0) { 1466db2a4165SFrank Mandarino printk(KERN_ERR "asoc: platform pcm constructor failed\n"); 1467db2a4165SFrank Mandarino kfree(rtd); 1468db2a4165SFrank Mandarino return ret; 1469db2a4165SFrank Mandarino } 1470db2a4165SFrank Mandarino 147187689d56SMark Brown pcm->private_free = platform->pcm_free; 1472db2a4165SFrank Mandarino printk(KERN_INFO "asoc: %s <-> %s mapping ok\n", codec_dai->name, 1473db2a4165SFrank Mandarino cpu_dai->name); 1474db2a4165SFrank Mandarino return ret; 1475db2a4165SFrank Mandarino } 1476db2a4165SFrank Mandarino 1477096e49d5SMark Brown /** 1478096e49d5SMark Brown * snd_soc_codec_volatile_register: Report if a register is volatile. 1479096e49d5SMark Brown * 1480096e49d5SMark Brown * @codec: CODEC to query. 1481096e49d5SMark Brown * @reg: Register to query. 1482096e49d5SMark Brown * 1483096e49d5SMark Brown * Boolean function indiciating if a CODEC register is volatile. 1484096e49d5SMark Brown */ 1485096e49d5SMark Brown int snd_soc_codec_volatile_register(struct snd_soc_codec *codec, int reg) 1486096e49d5SMark Brown { 1487096e49d5SMark Brown if (codec->volatile_register) 1488096e49d5SMark Brown return codec->volatile_register(reg); 1489096e49d5SMark Brown else 1490096e49d5SMark Brown return 0; 1491096e49d5SMark Brown } 1492096e49d5SMark Brown EXPORT_SYMBOL_GPL(snd_soc_codec_volatile_register); 1493096e49d5SMark Brown 1494db2a4165SFrank Mandarino /** 1495db2a4165SFrank Mandarino * snd_soc_new_ac97_codec - initailise AC97 device 1496db2a4165SFrank Mandarino * @codec: audio codec 1497db2a4165SFrank Mandarino * @ops: AC97 bus operations 1498db2a4165SFrank Mandarino * @num: AC97 codec number 1499db2a4165SFrank Mandarino * 1500db2a4165SFrank Mandarino * Initialises AC97 codec resources for use by ad-hoc devices only. 1501db2a4165SFrank Mandarino */ 1502db2a4165SFrank Mandarino int snd_soc_new_ac97_codec(struct snd_soc_codec *codec, 1503db2a4165SFrank Mandarino struct snd_ac97_bus_ops *ops, int num) 1504db2a4165SFrank Mandarino { 1505db2a4165SFrank Mandarino mutex_lock(&codec->mutex); 1506db2a4165SFrank Mandarino 1507db2a4165SFrank Mandarino codec->ac97 = kzalloc(sizeof(struct snd_ac97), GFP_KERNEL); 1508db2a4165SFrank Mandarino if (codec->ac97 == NULL) { 1509db2a4165SFrank Mandarino mutex_unlock(&codec->mutex); 1510db2a4165SFrank Mandarino return -ENOMEM; 1511db2a4165SFrank Mandarino } 1512db2a4165SFrank Mandarino 1513db2a4165SFrank Mandarino codec->ac97->bus = kzalloc(sizeof(struct snd_ac97_bus), GFP_KERNEL); 1514db2a4165SFrank Mandarino if (codec->ac97->bus == NULL) { 1515db2a4165SFrank Mandarino kfree(codec->ac97); 1516db2a4165SFrank Mandarino codec->ac97 = NULL; 1517db2a4165SFrank Mandarino mutex_unlock(&codec->mutex); 1518db2a4165SFrank Mandarino return -ENOMEM; 1519db2a4165SFrank Mandarino } 1520db2a4165SFrank Mandarino 1521db2a4165SFrank Mandarino codec->ac97->bus->ops = ops; 1522db2a4165SFrank Mandarino codec->ac97->num = num; 15232718625fSMark Brown codec->dev = &codec->ac97->dev; 1524db2a4165SFrank Mandarino mutex_unlock(&codec->mutex); 1525db2a4165SFrank Mandarino return 0; 1526db2a4165SFrank Mandarino } 1527db2a4165SFrank Mandarino EXPORT_SYMBOL_GPL(snd_soc_new_ac97_codec); 1528db2a4165SFrank Mandarino 1529db2a4165SFrank Mandarino /** 1530db2a4165SFrank Mandarino * snd_soc_free_ac97_codec - free AC97 codec device 1531db2a4165SFrank Mandarino * @codec: audio codec 1532db2a4165SFrank Mandarino * 1533db2a4165SFrank Mandarino * Frees AC97 codec device resources. 1534db2a4165SFrank Mandarino */ 1535db2a4165SFrank Mandarino void snd_soc_free_ac97_codec(struct snd_soc_codec *codec) 1536db2a4165SFrank Mandarino { 1537db2a4165SFrank Mandarino mutex_lock(&codec->mutex); 1538db2a4165SFrank Mandarino kfree(codec->ac97->bus); 1539db2a4165SFrank Mandarino kfree(codec->ac97); 1540db2a4165SFrank Mandarino codec->ac97 = NULL; 1541db2a4165SFrank Mandarino mutex_unlock(&codec->mutex); 1542db2a4165SFrank Mandarino } 1543db2a4165SFrank Mandarino EXPORT_SYMBOL_GPL(snd_soc_free_ac97_codec); 1544db2a4165SFrank Mandarino 1545db2a4165SFrank Mandarino /** 1546db2a4165SFrank Mandarino * snd_soc_update_bits - update codec register bits 1547db2a4165SFrank Mandarino * @codec: audio codec 1548db2a4165SFrank Mandarino * @reg: codec register 1549db2a4165SFrank Mandarino * @mask: register mask 1550db2a4165SFrank Mandarino * @value: new value 1551db2a4165SFrank Mandarino * 1552db2a4165SFrank Mandarino * Writes new register value. 1553db2a4165SFrank Mandarino * 1554db2a4165SFrank Mandarino * Returns 1 for change else 0. 1555db2a4165SFrank Mandarino */ 1556db2a4165SFrank Mandarino int snd_soc_update_bits(struct snd_soc_codec *codec, unsigned short reg, 155746f5822fSDaniel Ribeiro unsigned int mask, unsigned int value) 1558db2a4165SFrank Mandarino { 1559db2a4165SFrank Mandarino int change; 156046f5822fSDaniel Ribeiro unsigned int old, new; 1561db2a4165SFrank Mandarino 1562db2a4165SFrank Mandarino old = snd_soc_read(codec, reg); 1563db2a4165SFrank Mandarino new = (old & ~mask) | value; 1564db2a4165SFrank Mandarino change = old != new; 1565db2a4165SFrank Mandarino if (change) 1566db2a4165SFrank Mandarino snd_soc_write(codec, reg, new); 1567db2a4165SFrank Mandarino 1568db2a4165SFrank Mandarino return change; 1569db2a4165SFrank Mandarino } 1570db2a4165SFrank Mandarino EXPORT_SYMBOL_GPL(snd_soc_update_bits); 1571db2a4165SFrank Mandarino 1572db2a4165SFrank Mandarino /** 15736c508c62SEero Nurkkala * snd_soc_update_bits_locked - update codec register bits 15746c508c62SEero Nurkkala * @codec: audio codec 15756c508c62SEero Nurkkala * @reg: codec register 15766c508c62SEero Nurkkala * @mask: register mask 15776c508c62SEero Nurkkala * @value: new value 15786c508c62SEero Nurkkala * 15796c508c62SEero Nurkkala * Writes new register value, and takes the codec mutex. 15806c508c62SEero Nurkkala * 15816c508c62SEero Nurkkala * Returns 1 for change else 0. 15826c508c62SEero Nurkkala */ 1583dd1b3d53SMark Brown int snd_soc_update_bits_locked(struct snd_soc_codec *codec, 15846c508c62SEero Nurkkala unsigned short reg, unsigned int mask, 15856c508c62SEero Nurkkala unsigned int value) 15866c508c62SEero Nurkkala { 15876c508c62SEero Nurkkala int change; 15886c508c62SEero Nurkkala 15896c508c62SEero Nurkkala mutex_lock(&codec->mutex); 15906c508c62SEero Nurkkala change = snd_soc_update_bits(codec, reg, mask, value); 15916c508c62SEero Nurkkala mutex_unlock(&codec->mutex); 15926c508c62SEero Nurkkala 15936c508c62SEero Nurkkala return change; 15946c508c62SEero Nurkkala } 1595dd1b3d53SMark Brown EXPORT_SYMBOL_GPL(snd_soc_update_bits_locked); 15966c508c62SEero Nurkkala 15976c508c62SEero Nurkkala /** 1598db2a4165SFrank Mandarino * snd_soc_test_bits - test register for change 1599db2a4165SFrank Mandarino * @codec: audio codec 1600db2a4165SFrank Mandarino * @reg: codec register 1601db2a4165SFrank Mandarino * @mask: register mask 1602db2a4165SFrank Mandarino * @value: new value 1603db2a4165SFrank Mandarino * 1604db2a4165SFrank Mandarino * Tests a register with a new value and checks if the new value is 1605db2a4165SFrank Mandarino * different from the old value. 1606db2a4165SFrank Mandarino * 1607db2a4165SFrank Mandarino * Returns 1 for change else 0. 1608db2a4165SFrank Mandarino */ 1609db2a4165SFrank Mandarino int snd_soc_test_bits(struct snd_soc_codec *codec, unsigned short reg, 161046f5822fSDaniel Ribeiro unsigned int mask, unsigned int value) 1611db2a4165SFrank Mandarino { 1612db2a4165SFrank Mandarino int change; 161346f5822fSDaniel Ribeiro unsigned int old, new; 1614db2a4165SFrank Mandarino 1615db2a4165SFrank Mandarino old = snd_soc_read(codec, reg); 1616db2a4165SFrank Mandarino new = (old & ~mask) | value; 1617db2a4165SFrank Mandarino change = old != new; 1618db2a4165SFrank Mandarino 1619db2a4165SFrank Mandarino return change; 1620db2a4165SFrank Mandarino } 1621db2a4165SFrank Mandarino EXPORT_SYMBOL_GPL(snd_soc_test_bits); 1622db2a4165SFrank Mandarino 1623db2a4165SFrank Mandarino /** 1624db2a4165SFrank Mandarino * snd_soc_new_pcms - create new sound card and pcms 1625db2a4165SFrank Mandarino * @socdev: the SoC audio device 1626ac11a2b3SMark Brown * @idx: ALSA card index 1627ac11a2b3SMark Brown * @xid: card identification 1628db2a4165SFrank Mandarino * 1629db2a4165SFrank Mandarino * Create a new sound card based upon the codec and interface pcms. 1630db2a4165SFrank Mandarino * 1631db2a4165SFrank Mandarino * Returns 0 for success, else error. 1632db2a4165SFrank Mandarino */ 1633db2a4165SFrank Mandarino int snd_soc_new_pcms(struct snd_soc_device *socdev, int idx, const char *xid) 1634db2a4165SFrank Mandarino { 163587506549SMark Brown struct snd_soc_card *card = socdev->card; 16366627a653SMark Brown struct snd_soc_codec *codec = card->codec; 1637bd7dd77cSTakashi Iwai int ret, i; 1638db2a4165SFrank Mandarino 1639db2a4165SFrank Mandarino mutex_lock(&codec->mutex); 1640db2a4165SFrank Mandarino 1641db2a4165SFrank Mandarino /* register a sound card */ 1642bd7dd77cSTakashi Iwai ret = snd_card_create(idx, xid, codec->owner, 0, &codec->card); 1643bd7dd77cSTakashi Iwai if (ret < 0) { 1644db2a4165SFrank Mandarino printk(KERN_ERR "asoc: can't create sound card for codec %s\n", 1645db2a4165SFrank Mandarino codec->name); 1646db2a4165SFrank Mandarino mutex_unlock(&codec->mutex); 1647bd7dd77cSTakashi Iwai return ret; 1648db2a4165SFrank Mandarino } 1649db2a4165SFrank Mandarino 1650452c5eaaSMark Brown codec->socdev = socdev; 1651db2a4165SFrank Mandarino codec->card->dev = socdev->dev; 1652db2a4165SFrank Mandarino codec->card->private_data = codec; 1653db2a4165SFrank Mandarino strncpy(codec->card->driver, codec->name, sizeof(codec->card->driver)); 1654db2a4165SFrank Mandarino 1655db2a4165SFrank Mandarino /* create the pcms */ 165687506549SMark Brown for (i = 0; i < card->num_links; i++) { 165787506549SMark Brown ret = soc_new_pcm(socdev, &card->dai_link[i], i); 1658db2a4165SFrank Mandarino if (ret < 0) { 1659db2a4165SFrank Mandarino printk(KERN_ERR "asoc: can't create pcm %s\n", 166087506549SMark Brown card->dai_link[i].stream_name); 1661db2a4165SFrank Mandarino mutex_unlock(&codec->mutex); 1662db2a4165SFrank Mandarino return ret; 1663db2a4165SFrank Mandarino } 1664fb48e3c6SGraham Gower /* Check for codec->ac97 to handle the ac97.c fun */ 1665fb48e3c6SGraham Gower if (card->dai_link[i].codec_dai->ac97_control && codec->ac97) { 1666f7732053SBarry Song snd_ac97_dev_add_pdata(codec->ac97, 1667f7732053SBarry Song card->dai_link[i].cpu_dai->ac97_pdata); 1668f7732053SBarry Song } 1669db2a4165SFrank Mandarino } 1670db2a4165SFrank Mandarino 1671db2a4165SFrank Mandarino mutex_unlock(&codec->mutex); 1672db2a4165SFrank Mandarino return ret; 1673db2a4165SFrank Mandarino } 1674db2a4165SFrank Mandarino EXPORT_SYMBOL_GPL(snd_soc_new_pcms); 1675db2a4165SFrank Mandarino 1676db2a4165SFrank Mandarino /** 1677db2a4165SFrank Mandarino * snd_soc_free_pcms - free sound card and pcms 1678db2a4165SFrank Mandarino * @socdev: the SoC audio device 1679db2a4165SFrank Mandarino * 1680db2a4165SFrank Mandarino * Frees sound card and pcms associated with the socdev. 1681db2a4165SFrank Mandarino * Also unregister the codec if it is an AC97 device. 1682db2a4165SFrank Mandarino */ 1683db2a4165SFrank Mandarino void snd_soc_free_pcms(struct snd_soc_device *socdev) 1684db2a4165SFrank Mandarino { 16856627a653SMark Brown struct snd_soc_codec *codec = socdev->card->codec; 1686a68660e0SLiam Girdwood #ifdef CONFIG_SND_SOC_AC97_BUS 16873c4b266fSLiam Girdwood struct snd_soc_dai *codec_dai; 1688a68660e0SLiam Girdwood int i; 1689a68660e0SLiam Girdwood #endif 1690db2a4165SFrank Mandarino 1691db2a4165SFrank Mandarino mutex_lock(&codec->mutex); 16926627a653SMark Brown soc_cleanup_codec_debugfs(codec); 1693db2a4165SFrank Mandarino #ifdef CONFIG_SND_SOC_AC97_BUS 1694a68660e0SLiam Girdwood for (i = 0; i < codec->num_dai; i++) { 1695a68660e0SLiam Girdwood codec_dai = &codec->dai[i]; 1696d2314e0eSAtsushi Nemoto if (codec_dai->ac97_control && codec->ac97 && 1697d2314e0eSAtsushi Nemoto strcmp(codec->name, "AC97") != 0) { 1698db2a4165SFrank Mandarino soc_ac97_dev_unregister(codec); 1699a68660e0SLiam Girdwood goto free_card; 1700a68660e0SLiam Girdwood } 1701a68660e0SLiam Girdwood } 1702a68660e0SLiam Girdwood free_card: 1703db2a4165SFrank Mandarino #endif 1704db2a4165SFrank Mandarino 1705db2a4165SFrank Mandarino if (codec->card) 1706db2a4165SFrank Mandarino snd_card_free(codec->card); 1707db2a4165SFrank Mandarino device_remove_file(socdev->dev, &dev_attr_codec_reg); 1708db2a4165SFrank Mandarino mutex_unlock(&codec->mutex); 1709db2a4165SFrank Mandarino } 1710db2a4165SFrank Mandarino EXPORT_SYMBOL_GPL(snd_soc_free_pcms); 1711db2a4165SFrank Mandarino 1712db2a4165SFrank Mandarino /** 1713db2a4165SFrank Mandarino * snd_soc_set_runtime_hwparams - set the runtime hardware parameters 1714db2a4165SFrank Mandarino * @substream: the pcm substream 1715db2a4165SFrank Mandarino * @hw: the hardware parameters 1716db2a4165SFrank Mandarino * 1717db2a4165SFrank Mandarino * Sets the substream runtime hardware parameters. 1718db2a4165SFrank Mandarino */ 1719db2a4165SFrank Mandarino int snd_soc_set_runtime_hwparams(struct snd_pcm_substream *substream, 1720db2a4165SFrank Mandarino const struct snd_pcm_hardware *hw) 1721db2a4165SFrank Mandarino { 1722db2a4165SFrank Mandarino struct snd_pcm_runtime *runtime = substream->runtime; 1723db2a4165SFrank Mandarino runtime->hw.info = hw->info; 1724db2a4165SFrank Mandarino runtime->hw.formats = hw->formats; 1725db2a4165SFrank Mandarino runtime->hw.period_bytes_min = hw->period_bytes_min; 1726db2a4165SFrank Mandarino runtime->hw.period_bytes_max = hw->period_bytes_max; 1727db2a4165SFrank Mandarino runtime->hw.periods_min = hw->periods_min; 1728db2a4165SFrank Mandarino runtime->hw.periods_max = hw->periods_max; 1729db2a4165SFrank Mandarino runtime->hw.buffer_bytes_max = hw->buffer_bytes_max; 1730db2a4165SFrank Mandarino runtime->hw.fifo_size = hw->fifo_size; 1731db2a4165SFrank Mandarino return 0; 1732db2a4165SFrank Mandarino } 1733db2a4165SFrank Mandarino EXPORT_SYMBOL_GPL(snd_soc_set_runtime_hwparams); 1734db2a4165SFrank Mandarino 1735db2a4165SFrank Mandarino /** 1736db2a4165SFrank Mandarino * snd_soc_cnew - create new control 1737db2a4165SFrank Mandarino * @_template: control template 1738db2a4165SFrank Mandarino * @data: control private data 1739ac11a2b3SMark Brown * @long_name: control long name 1740db2a4165SFrank Mandarino * 1741db2a4165SFrank Mandarino * Create a new mixer control from a template control. 1742db2a4165SFrank Mandarino * 1743db2a4165SFrank Mandarino * Returns 0 for success, else error. 1744db2a4165SFrank Mandarino */ 1745db2a4165SFrank Mandarino struct snd_kcontrol *snd_soc_cnew(const struct snd_kcontrol_new *_template, 1746db2a4165SFrank Mandarino void *data, char *long_name) 1747db2a4165SFrank Mandarino { 1748db2a4165SFrank Mandarino struct snd_kcontrol_new template; 1749db2a4165SFrank Mandarino 1750db2a4165SFrank Mandarino memcpy(&template, _template, sizeof(template)); 1751db2a4165SFrank Mandarino if (long_name) 1752db2a4165SFrank Mandarino template.name = long_name; 1753db2a4165SFrank Mandarino template.index = 0; 1754db2a4165SFrank Mandarino 1755db2a4165SFrank Mandarino return snd_ctl_new1(&template, data); 1756db2a4165SFrank Mandarino } 1757db2a4165SFrank Mandarino EXPORT_SYMBOL_GPL(snd_soc_cnew); 1758db2a4165SFrank Mandarino 1759db2a4165SFrank Mandarino /** 17603e8e1952SIan Molton * snd_soc_add_controls - add an array of controls to a codec. 17613e8e1952SIan Molton * Convienience function to add a list of controls. Many codecs were 17623e8e1952SIan Molton * duplicating this code. 17633e8e1952SIan Molton * 17643e8e1952SIan Molton * @codec: codec to add controls to 17653e8e1952SIan Molton * @controls: array of controls to add 17663e8e1952SIan Molton * @num_controls: number of elements in the array 17673e8e1952SIan Molton * 17683e8e1952SIan Molton * Return 0 for success, else error. 17693e8e1952SIan Molton */ 17703e8e1952SIan Molton int snd_soc_add_controls(struct snd_soc_codec *codec, 17713e8e1952SIan Molton const struct snd_kcontrol_new *controls, int num_controls) 17723e8e1952SIan Molton { 17733e8e1952SIan Molton struct snd_card *card = codec->card; 17743e8e1952SIan Molton int err, i; 17753e8e1952SIan Molton 17763e8e1952SIan Molton for (i = 0; i < num_controls; i++) { 17773e8e1952SIan Molton const struct snd_kcontrol_new *control = &controls[i]; 17783e8e1952SIan Molton err = snd_ctl_add(card, snd_soc_cnew(control, codec, NULL)); 17793e8e1952SIan Molton if (err < 0) { 17803e8e1952SIan Molton dev_err(codec->dev, "%s: Failed to add %s\n", 17813e8e1952SIan Molton codec->name, control->name); 17823e8e1952SIan Molton return err; 17833e8e1952SIan Molton } 17843e8e1952SIan Molton } 17853e8e1952SIan Molton 17863e8e1952SIan Molton return 0; 17873e8e1952SIan Molton } 17883e8e1952SIan Molton EXPORT_SYMBOL_GPL(snd_soc_add_controls); 17893e8e1952SIan Molton 17903e8e1952SIan Molton /** 1791db2a4165SFrank Mandarino * snd_soc_info_enum_double - enumerated double mixer info callback 1792db2a4165SFrank Mandarino * @kcontrol: mixer control 1793db2a4165SFrank Mandarino * @uinfo: control element information 1794db2a4165SFrank Mandarino * 1795db2a4165SFrank Mandarino * Callback to provide information about a double enumerated 1796db2a4165SFrank Mandarino * mixer control. 1797db2a4165SFrank Mandarino * 1798db2a4165SFrank Mandarino * Returns 0 for success. 1799db2a4165SFrank Mandarino */ 1800db2a4165SFrank Mandarino int snd_soc_info_enum_double(struct snd_kcontrol *kcontrol, 1801db2a4165SFrank Mandarino struct snd_ctl_elem_info *uinfo) 1802db2a4165SFrank Mandarino { 1803db2a4165SFrank Mandarino struct soc_enum *e = (struct soc_enum *)kcontrol->private_value; 1804db2a4165SFrank Mandarino 1805db2a4165SFrank Mandarino uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED; 1806db2a4165SFrank Mandarino uinfo->count = e->shift_l == e->shift_r ? 1 : 2; 1807f8ba0b7bSJon Smirl uinfo->value.enumerated.items = e->max; 1808db2a4165SFrank Mandarino 1809f8ba0b7bSJon Smirl if (uinfo->value.enumerated.item > e->max - 1) 1810f8ba0b7bSJon Smirl uinfo->value.enumerated.item = e->max - 1; 1811db2a4165SFrank Mandarino strcpy(uinfo->value.enumerated.name, 1812db2a4165SFrank Mandarino e->texts[uinfo->value.enumerated.item]); 1813db2a4165SFrank Mandarino return 0; 1814db2a4165SFrank Mandarino } 1815db2a4165SFrank Mandarino EXPORT_SYMBOL_GPL(snd_soc_info_enum_double); 1816db2a4165SFrank Mandarino 1817db2a4165SFrank Mandarino /** 1818db2a4165SFrank Mandarino * snd_soc_get_enum_double - enumerated double mixer get callback 1819db2a4165SFrank Mandarino * @kcontrol: mixer control 1820ac11a2b3SMark Brown * @ucontrol: control element information 1821db2a4165SFrank Mandarino * 1822db2a4165SFrank Mandarino * Callback to get the value of a double enumerated mixer. 1823db2a4165SFrank Mandarino * 1824db2a4165SFrank Mandarino * Returns 0 for success. 1825db2a4165SFrank Mandarino */ 1826db2a4165SFrank Mandarino int snd_soc_get_enum_double(struct snd_kcontrol *kcontrol, 1827db2a4165SFrank Mandarino struct snd_ctl_elem_value *ucontrol) 1828db2a4165SFrank Mandarino { 1829db2a4165SFrank Mandarino struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol); 1830db2a4165SFrank Mandarino struct soc_enum *e = (struct soc_enum *)kcontrol->private_value; 183146f5822fSDaniel Ribeiro unsigned int val, bitmask; 1832db2a4165SFrank Mandarino 1833f8ba0b7bSJon Smirl for (bitmask = 1; bitmask < e->max; bitmask <<= 1) 1834db2a4165SFrank Mandarino ; 1835db2a4165SFrank Mandarino val = snd_soc_read(codec, e->reg); 18363ff3f64bSMark Brown ucontrol->value.enumerated.item[0] 18373ff3f64bSMark Brown = (val >> e->shift_l) & (bitmask - 1); 1838db2a4165SFrank Mandarino if (e->shift_l != e->shift_r) 1839db2a4165SFrank Mandarino ucontrol->value.enumerated.item[1] = 1840db2a4165SFrank Mandarino (val >> e->shift_r) & (bitmask - 1); 1841db2a4165SFrank Mandarino 1842db2a4165SFrank Mandarino return 0; 1843db2a4165SFrank Mandarino } 1844db2a4165SFrank Mandarino EXPORT_SYMBOL_GPL(snd_soc_get_enum_double); 1845db2a4165SFrank Mandarino 1846db2a4165SFrank Mandarino /** 1847db2a4165SFrank Mandarino * snd_soc_put_enum_double - enumerated double mixer put callback 1848db2a4165SFrank Mandarino * @kcontrol: mixer control 1849ac11a2b3SMark Brown * @ucontrol: control element information 1850db2a4165SFrank Mandarino * 1851db2a4165SFrank Mandarino * Callback to set the value of a double enumerated mixer. 1852db2a4165SFrank Mandarino * 1853db2a4165SFrank Mandarino * Returns 0 for success. 1854db2a4165SFrank Mandarino */ 1855db2a4165SFrank Mandarino int snd_soc_put_enum_double(struct snd_kcontrol *kcontrol, 1856db2a4165SFrank Mandarino struct snd_ctl_elem_value *ucontrol) 1857db2a4165SFrank Mandarino { 1858db2a4165SFrank Mandarino struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol); 1859db2a4165SFrank Mandarino struct soc_enum *e = (struct soc_enum *)kcontrol->private_value; 186046f5822fSDaniel Ribeiro unsigned int val; 186146f5822fSDaniel Ribeiro unsigned int mask, bitmask; 1862db2a4165SFrank Mandarino 1863f8ba0b7bSJon Smirl for (bitmask = 1; bitmask < e->max; bitmask <<= 1) 1864db2a4165SFrank Mandarino ; 1865f8ba0b7bSJon Smirl if (ucontrol->value.enumerated.item[0] > e->max - 1) 1866db2a4165SFrank Mandarino return -EINVAL; 1867db2a4165SFrank Mandarino val = ucontrol->value.enumerated.item[0] << e->shift_l; 1868db2a4165SFrank Mandarino mask = (bitmask - 1) << e->shift_l; 1869db2a4165SFrank Mandarino if (e->shift_l != e->shift_r) { 1870f8ba0b7bSJon Smirl if (ucontrol->value.enumerated.item[1] > e->max - 1) 1871db2a4165SFrank Mandarino return -EINVAL; 1872db2a4165SFrank Mandarino val |= ucontrol->value.enumerated.item[1] << e->shift_r; 1873db2a4165SFrank Mandarino mask |= (bitmask - 1) << e->shift_r; 1874db2a4165SFrank Mandarino } 1875db2a4165SFrank Mandarino 18766c508c62SEero Nurkkala return snd_soc_update_bits_locked(codec, e->reg, mask, val); 1877db2a4165SFrank Mandarino } 1878db2a4165SFrank Mandarino EXPORT_SYMBOL_GPL(snd_soc_put_enum_double); 1879db2a4165SFrank Mandarino 1880db2a4165SFrank Mandarino /** 18812e72f8e3SPeter Ujfalusi * snd_soc_get_value_enum_double - semi enumerated double mixer get callback 18822e72f8e3SPeter Ujfalusi * @kcontrol: mixer control 18832e72f8e3SPeter Ujfalusi * @ucontrol: control element information 18842e72f8e3SPeter Ujfalusi * 18852e72f8e3SPeter Ujfalusi * Callback to get the value of a double semi enumerated mixer. 18862e72f8e3SPeter Ujfalusi * 18872e72f8e3SPeter Ujfalusi * Semi enumerated mixer: the enumerated items are referred as values. Can be 18882e72f8e3SPeter Ujfalusi * used for handling bitfield coded enumeration for example. 18892e72f8e3SPeter Ujfalusi * 18902e72f8e3SPeter Ujfalusi * Returns 0 for success. 18912e72f8e3SPeter Ujfalusi */ 18922e72f8e3SPeter Ujfalusi int snd_soc_get_value_enum_double(struct snd_kcontrol *kcontrol, 18932e72f8e3SPeter Ujfalusi struct snd_ctl_elem_value *ucontrol) 18942e72f8e3SPeter Ujfalusi { 18952e72f8e3SPeter Ujfalusi struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol); 189674155556SPeter Ujfalusi struct soc_enum *e = (struct soc_enum *)kcontrol->private_value; 189746f5822fSDaniel Ribeiro unsigned int reg_val, val, mux; 18982e72f8e3SPeter Ujfalusi 18992e72f8e3SPeter Ujfalusi reg_val = snd_soc_read(codec, e->reg); 19002e72f8e3SPeter Ujfalusi val = (reg_val >> e->shift_l) & e->mask; 19012e72f8e3SPeter Ujfalusi for (mux = 0; mux < e->max; mux++) { 19022e72f8e3SPeter Ujfalusi if (val == e->values[mux]) 19032e72f8e3SPeter Ujfalusi break; 19042e72f8e3SPeter Ujfalusi } 19052e72f8e3SPeter Ujfalusi ucontrol->value.enumerated.item[0] = mux; 19062e72f8e3SPeter Ujfalusi if (e->shift_l != e->shift_r) { 19072e72f8e3SPeter Ujfalusi val = (reg_val >> e->shift_r) & e->mask; 19082e72f8e3SPeter Ujfalusi for (mux = 0; mux < e->max; mux++) { 19092e72f8e3SPeter Ujfalusi if (val == e->values[mux]) 19102e72f8e3SPeter Ujfalusi break; 19112e72f8e3SPeter Ujfalusi } 19122e72f8e3SPeter Ujfalusi ucontrol->value.enumerated.item[1] = mux; 19132e72f8e3SPeter Ujfalusi } 19142e72f8e3SPeter Ujfalusi 19152e72f8e3SPeter Ujfalusi return 0; 19162e72f8e3SPeter Ujfalusi } 19172e72f8e3SPeter Ujfalusi EXPORT_SYMBOL_GPL(snd_soc_get_value_enum_double); 19182e72f8e3SPeter Ujfalusi 19192e72f8e3SPeter Ujfalusi /** 19202e72f8e3SPeter Ujfalusi * snd_soc_put_value_enum_double - semi enumerated double mixer put callback 19212e72f8e3SPeter Ujfalusi * @kcontrol: mixer control 19222e72f8e3SPeter Ujfalusi * @ucontrol: control element information 19232e72f8e3SPeter Ujfalusi * 19242e72f8e3SPeter Ujfalusi * Callback to set the value of a double semi enumerated mixer. 19252e72f8e3SPeter Ujfalusi * 19262e72f8e3SPeter Ujfalusi * Semi enumerated mixer: the enumerated items are referred as values. Can be 19272e72f8e3SPeter Ujfalusi * used for handling bitfield coded enumeration for example. 19282e72f8e3SPeter Ujfalusi * 19292e72f8e3SPeter Ujfalusi * Returns 0 for success. 19302e72f8e3SPeter Ujfalusi */ 19312e72f8e3SPeter Ujfalusi int snd_soc_put_value_enum_double(struct snd_kcontrol *kcontrol, 19322e72f8e3SPeter Ujfalusi struct snd_ctl_elem_value *ucontrol) 19332e72f8e3SPeter Ujfalusi { 19342e72f8e3SPeter Ujfalusi struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol); 193574155556SPeter Ujfalusi struct soc_enum *e = (struct soc_enum *)kcontrol->private_value; 193646f5822fSDaniel Ribeiro unsigned int val; 193746f5822fSDaniel Ribeiro unsigned int mask; 19382e72f8e3SPeter Ujfalusi 19392e72f8e3SPeter Ujfalusi if (ucontrol->value.enumerated.item[0] > e->max - 1) 19402e72f8e3SPeter Ujfalusi return -EINVAL; 19412e72f8e3SPeter Ujfalusi val = e->values[ucontrol->value.enumerated.item[0]] << e->shift_l; 19422e72f8e3SPeter Ujfalusi mask = e->mask << e->shift_l; 19432e72f8e3SPeter Ujfalusi if (e->shift_l != e->shift_r) { 19442e72f8e3SPeter Ujfalusi if (ucontrol->value.enumerated.item[1] > e->max - 1) 19452e72f8e3SPeter Ujfalusi return -EINVAL; 19462e72f8e3SPeter Ujfalusi val |= e->values[ucontrol->value.enumerated.item[1]] << e->shift_r; 19472e72f8e3SPeter Ujfalusi mask |= e->mask << e->shift_r; 19482e72f8e3SPeter Ujfalusi } 19492e72f8e3SPeter Ujfalusi 19506c508c62SEero Nurkkala return snd_soc_update_bits_locked(codec, e->reg, mask, val); 19512e72f8e3SPeter Ujfalusi } 19522e72f8e3SPeter Ujfalusi EXPORT_SYMBOL_GPL(snd_soc_put_value_enum_double); 19532e72f8e3SPeter Ujfalusi 19542e72f8e3SPeter Ujfalusi /** 1955db2a4165SFrank Mandarino * snd_soc_info_enum_ext - external enumerated single mixer info callback 1956db2a4165SFrank Mandarino * @kcontrol: mixer control 1957db2a4165SFrank Mandarino * @uinfo: control element information 1958db2a4165SFrank Mandarino * 1959db2a4165SFrank Mandarino * Callback to provide information about an external enumerated 1960db2a4165SFrank Mandarino * single mixer. 1961db2a4165SFrank Mandarino * 1962db2a4165SFrank Mandarino * Returns 0 for success. 1963db2a4165SFrank Mandarino */ 1964db2a4165SFrank Mandarino int snd_soc_info_enum_ext(struct snd_kcontrol *kcontrol, 1965db2a4165SFrank Mandarino struct snd_ctl_elem_info *uinfo) 1966db2a4165SFrank Mandarino { 1967db2a4165SFrank Mandarino struct soc_enum *e = (struct soc_enum *)kcontrol->private_value; 1968db2a4165SFrank Mandarino 1969db2a4165SFrank Mandarino uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED; 1970db2a4165SFrank Mandarino uinfo->count = 1; 1971f8ba0b7bSJon Smirl uinfo->value.enumerated.items = e->max; 1972db2a4165SFrank Mandarino 1973f8ba0b7bSJon Smirl if (uinfo->value.enumerated.item > e->max - 1) 1974f8ba0b7bSJon Smirl uinfo->value.enumerated.item = e->max - 1; 1975db2a4165SFrank Mandarino strcpy(uinfo->value.enumerated.name, 1976db2a4165SFrank Mandarino e->texts[uinfo->value.enumerated.item]); 1977db2a4165SFrank Mandarino return 0; 1978db2a4165SFrank Mandarino } 1979db2a4165SFrank Mandarino EXPORT_SYMBOL_GPL(snd_soc_info_enum_ext); 1980db2a4165SFrank Mandarino 1981db2a4165SFrank Mandarino /** 1982db2a4165SFrank Mandarino * snd_soc_info_volsw_ext - external single mixer info callback 1983db2a4165SFrank Mandarino * @kcontrol: mixer control 1984db2a4165SFrank Mandarino * @uinfo: control element information 1985db2a4165SFrank Mandarino * 1986db2a4165SFrank Mandarino * Callback to provide information about a single external mixer control. 1987db2a4165SFrank Mandarino * 1988db2a4165SFrank Mandarino * Returns 0 for success. 1989db2a4165SFrank Mandarino */ 1990db2a4165SFrank Mandarino int snd_soc_info_volsw_ext(struct snd_kcontrol *kcontrol, 1991db2a4165SFrank Mandarino struct snd_ctl_elem_info *uinfo) 1992db2a4165SFrank Mandarino { 1993a7a4ac86SPhilipp Zabel int max = kcontrol->private_value; 1994db2a4165SFrank Mandarino 1995fd5dfad9SMark Brown if (max == 1 && !strstr(kcontrol->id.name, " Volume")) 1996a7a4ac86SPhilipp Zabel uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN; 1997a7a4ac86SPhilipp Zabel else 1998a7a4ac86SPhilipp Zabel uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER; 1999a7a4ac86SPhilipp Zabel 2000db2a4165SFrank Mandarino uinfo->count = 1; 2001db2a4165SFrank Mandarino uinfo->value.integer.min = 0; 2002a7a4ac86SPhilipp Zabel uinfo->value.integer.max = max; 2003db2a4165SFrank Mandarino return 0; 2004db2a4165SFrank Mandarino } 2005db2a4165SFrank Mandarino EXPORT_SYMBOL_GPL(snd_soc_info_volsw_ext); 2006db2a4165SFrank Mandarino 2007db2a4165SFrank Mandarino /** 2008db2a4165SFrank Mandarino * snd_soc_info_volsw - single mixer info callback 2009db2a4165SFrank Mandarino * @kcontrol: mixer control 2010db2a4165SFrank Mandarino * @uinfo: control element information 2011db2a4165SFrank Mandarino * 2012db2a4165SFrank Mandarino * Callback to provide information about a single mixer control. 2013db2a4165SFrank Mandarino * 2014db2a4165SFrank Mandarino * Returns 0 for success. 2015db2a4165SFrank Mandarino */ 2016db2a4165SFrank Mandarino int snd_soc_info_volsw(struct snd_kcontrol *kcontrol, 2017db2a4165SFrank Mandarino struct snd_ctl_elem_info *uinfo) 2018db2a4165SFrank Mandarino { 20194eaa9819SJon Smirl struct soc_mixer_control *mc = 20204eaa9819SJon Smirl (struct soc_mixer_control *)kcontrol->private_value; 2021d11bb4a9SPeter Ujfalusi int platform_max; 2022762b8df7SMark Brown unsigned int shift = mc->shift; 2023815ecf8dSJon Smirl unsigned int rshift = mc->rshift; 2024db2a4165SFrank Mandarino 2025d11bb4a9SPeter Ujfalusi if (!mc->platform_max) 2026d11bb4a9SPeter Ujfalusi mc->platform_max = mc->max; 2027d11bb4a9SPeter Ujfalusi platform_max = mc->platform_max; 2028d11bb4a9SPeter Ujfalusi 2029d11bb4a9SPeter Ujfalusi if (platform_max == 1 && !strstr(kcontrol->id.name, " Volume")) 2030a7a4ac86SPhilipp Zabel uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN; 2031a7a4ac86SPhilipp Zabel else 2032a7a4ac86SPhilipp Zabel uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER; 2033a7a4ac86SPhilipp Zabel 2034db2a4165SFrank Mandarino uinfo->count = shift == rshift ? 1 : 2; 2035db2a4165SFrank Mandarino uinfo->value.integer.min = 0; 2036d11bb4a9SPeter Ujfalusi uinfo->value.integer.max = platform_max; 2037db2a4165SFrank Mandarino return 0; 2038db2a4165SFrank Mandarino } 2039db2a4165SFrank Mandarino EXPORT_SYMBOL_GPL(snd_soc_info_volsw); 2040db2a4165SFrank Mandarino 2041db2a4165SFrank Mandarino /** 2042db2a4165SFrank Mandarino * snd_soc_get_volsw - single mixer get callback 2043db2a4165SFrank Mandarino * @kcontrol: mixer control 2044ac11a2b3SMark Brown * @ucontrol: control element information 2045db2a4165SFrank Mandarino * 2046db2a4165SFrank Mandarino * Callback to get the value of a single mixer control. 2047db2a4165SFrank Mandarino * 2048db2a4165SFrank Mandarino * Returns 0 for success. 2049db2a4165SFrank Mandarino */ 2050db2a4165SFrank Mandarino int snd_soc_get_volsw(struct snd_kcontrol *kcontrol, 2051db2a4165SFrank Mandarino struct snd_ctl_elem_value *ucontrol) 2052db2a4165SFrank Mandarino { 20534eaa9819SJon Smirl struct soc_mixer_control *mc = 20544eaa9819SJon Smirl (struct soc_mixer_control *)kcontrol->private_value; 2055db2a4165SFrank Mandarino struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol); 2056815ecf8dSJon Smirl unsigned int reg = mc->reg; 2057815ecf8dSJon Smirl unsigned int shift = mc->shift; 2058815ecf8dSJon Smirl unsigned int rshift = mc->rshift; 20594eaa9819SJon Smirl int max = mc->max; 2060815ecf8dSJon Smirl unsigned int mask = (1 << fls(max)) - 1; 2061815ecf8dSJon Smirl unsigned int invert = mc->invert; 2062db2a4165SFrank Mandarino 2063db2a4165SFrank Mandarino ucontrol->value.integer.value[0] = 2064db2a4165SFrank Mandarino (snd_soc_read(codec, reg) >> shift) & mask; 2065db2a4165SFrank Mandarino if (shift != rshift) 2066db2a4165SFrank Mandarino ucontrol->value.integer.value[1] = 2067db2a4165SFrank Mandarino (snd_soc_read(codec, reg) >> rshift) & mask; 2068db2a4165SFrank Mandarino if (invert) { 2069db2a4165SFrank Mandarino ucontrol->value.integer.value[0] = 2070a7a4ac86SPhilipp Zabel max - ucontrol->value.integer.value[0]; 2071db2a4165SFrank Mandarino if (shift != rshift) 2072db2a4165SFrank Mandarino ucontrol->value.integer.value[1] = 2073a7a4ac86SPhilipp Zabel max - ucontrol->value.integer.value[1]; 2074db2a4165SFrank Mandarino } 2075db2a4165SFrank Mandarino 2076db2a4165SFrank Mandarino return 0; 2077db2a4165SFrank Mandarino } 2078db2a4165SFrank Mandarino EXPORT_SYMBOL_GPL(snd_soc_get_volsw); 2079db2a4165SFrank Mandarino 2080db2a4165SFrank Mandarino /** 2081db2a4165SFrank Mandarino * snd_soc_put_volsw - single mixer put callback 2082db2a4165SFrank Mandarino * @kcontrol: mixer control 2083ac11a2b3SMark Brown * @ucontrol: control element information 2084db2a4165SFrank Mandarino * 2085db2a4165SFrank Mandarino * Callback to set the value of a single mixer control. 2086db2a4165SFrank Mandarino * 2087db2a4165SFrank Mandarino * Returns 0 for success. 2088db2a4165SFrank Mandarino */ 2089db2a4165SFrank Mandarino int snd_soc_put_volsw(struct snd_kcontrol *kcontrol, 2090db2a4165SFrank Mandarino struct snd_ctl_elem_value *ucontrol) 2091db2a4165SFrank Mandarino { 20924eaa9819SJon Smirl struct soc_mixer_control *mc = 20934eaa9819SJon Smirl (struct soc_mixer_control *)kcontrol->private_value; 2094db2a4165SFrank Mandarino struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol); 2095815ecf8dSJon Smirl unsigned int reg = mc->reg; 2096815ecf8dSJon Smirl unsigned int shift = mc->shift; 2097815ecf8dSJon Smirl unsigned int rshift = mc->rshift; 20984eaa9819SJon Smirl int max = mc->max; 2099815ecf8dSJon Smirl unsigned int mask = (1 << fls(max)) - 1; 2100815ecf8dSJon Smirl unsigned int invert = mc->invert; 210146f5822fSDaniel Ribeiro unsigned int val, val2, val_mask; 2102db2a4165SFrank Mandarino 2103db2a4165SFrank Mandarino val = (ucontrol->value.integer.value[0] & mask); 2104db2a4165SFrank Mandarino if (invert) 2105a7a4ac86SPhilipp Zabel val = max - val; 2106db2a4165SFrank Mandarino val_mask = mask << shift; 2107db2a4165SFrank Mandarino val = val << shift; 2108db2a4165SFrank Mandarino if (shift != rshift) { 2109db2a4165SFrank Mandarino val2 = (ucontrol->value.integer.value[1] & mask); 2110db2a4165SFrank Mandarino if (invert) 2111a7a4ac86SPhilipp Zabel val2 = max - val2; 2112db2a4165SFrank Mandarino val_mask |= mask << rshift; 2113db2a4165SFrank Mandarino val |= val2 << rshift; 2114db2a4165SFrank Mandarino } 21156c508c62SEero Nurkkala return snd_soc_update_bits_locked(codec, reg, val_mask, val); 2116db2a4165SFrank Mandarino } 2117db2a4165SFrank Mandarino EXPORT_SYMBOL_GPL(snd_soc_put_volsw); 2118db2a4165SFrank Mandarino 2119db2a4165SFrank Mandarino /** 2120db2a4165SFrank Mandarino * snd_soc_info_volsw_2r - double mixer info callback 2121db2a4165SFrank Mandarino * @kcontrol: mixer control 2122db2a4165SFrank Mandarino * @uinfo: control element information 2123db2a4165SFrank Mandarino * 2124db2a4165SFrank Mandarino * Callback to provide information about a double mixer control that 2125db2a4165SFrank Mandarino * spans 2 codec registers. 2126db2a4165SFrank Mandarino * 2127db2a4165SFrank Mandarino * Returns 0 for success. 2128db2a4165SFrank Mandarino */ 2129db2a4165SFrank Mandarino int snd_soc_info_volsw_2r(struct snd_kcontrol *kcontrol, 2130db2a4165SFrank Mandarino struct snd_ctl_elem_info *uinfo) 2131db2a4165SFrank Mandarino { 21324eaa9819SJon Smirl struct soc_mixer_control *mc = 21334eaa9819SJon Smirl (struct soc_mixer_control *)kcontrol->private_value; 2134d11bb4a9SPeter Ujfalusi int platform_max; 2135db2a4165SFrank Mandarino 2136d11bb4a9SPeter Ujfalusi if (!mc->platform_max) 2137d11bb4a9SPeter Ujfalusi mc->platform_max = mc->max; 2138d11bb4a9SPeter Ujfalusi platform_max = mc->platform_max; 2139d11bb4a9SPeter Ujfalusi 2140d11bb4a9SPeter Ujfalusi if (platform_max == 1 && !strstr(kcontrol->id.name, " Volume")) 2141a7a4ac86SPhilipp Zabel uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN; 2142a7a4ac86SPhilipp Zabel else 2143a7a4ac86SPhilipp Zabel uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER; 2144a7a4ac86SPhilipp Zabel 2145db2a4165SFrank Mandarino uinfo->count = 2; 2146db2a4165SFrank Mandarino uinfo->value.integer.min = 0; 2147d11bb4a9SPeter Ujfalusi uinfo->value.integer.max = platform_max; 2148db2a4165SFrank Mandarino return 0; 2149db2a4165SFrank Mandarino } 2150db2a4165SFrank Mandarino EXPORT_SYMBOL_GPL(snd_soc_info_volsw_2r); 2151db2a4165SFrank Mandarino 2152db2a4165SFrank Mandarino /** 2153db2a4165SFrank Mandarino * snd_soc_get_volsw_2r - double mixer get callback 2154db2a4165SFrank Mandarino * @kcontrol: mixer control 2155ac11a2b3SMark Brown * @ucontrol: control element information 2156db2a4165SFrank Mandarino * 2157db2a4165SFrank Mandarino * Callback to get the value of a double mixer control that spans 2 registers. 2158db2a4165SFrank Mandarino * 2159db2a4165SFrank Mandarino * Returns 0 for success. 2160db2a4165SFrank Mandarino */ 2161db2a4165SFrank Mandarino int snd_soc_get_volsw_2r(struct snd_kcontrol *kcontrol, 2162db2a4165SFrank Mandarino struct snd_ctl_elem_value *ucontrol) 2163db2a4165SFrank Mandarino { 21644eaa9819SJon Smirl struct soc_mixer_control *mc = 21654eaa9819SJon Smirl (struct soc_mixer_control *)kcontrol->private_value; 2166db2a4165SFrank Mandarino struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol); 2167815ecf8dSJon Smirl unsigned int reg = mc->reg; 2168815ecf8dSJon Smirl unsigned int reg2 = mc->rreg; 2169815ecf8dSJon Smirl unsigned int shift = mc->shift; 21704eaa9819SJon Smirl int max = mc->max; 2171815ecf8dSJon Smirl unsigned int mask = (1 << fls(max)) - 1; 2172815ecf8dSJon Smirl unsigned int invert = mc->invert; 2173db2a4165SFrank Mandarino 2174db2a4165SFrank Mandarino ucontrol->value.integer.value[0] = 2175db2a4165SFrank Mandarino (snd_soc_read(codec, reg) >> shift) & mask; 2176db2a4165SFrank Mandarino ucontrol->value.integer.value[1] = 2177db2a4165SFrank Mandarino (snd_soc_read(codec, reg2) >> shift) & mask; 2178db2a4165SFrank Mandarino if (invert) { 2179db2a4165SFrank Mandarino ucontrol->value.integer.value[0] = 2180a7a4ac86SPhilipp Zabel max - ucontrol->value.integer.value[0]; 2181db2a4165SFrank Mandarino ucontrol->value.integer.value[1] = 2182a7a4ac86SPhilipp Zabel max - ucontrol->value.integer.value[1]; 2183db2a4165SFrank Mandarino } 2184db2a4165SFrank Mandarino 2185db2a4165SFrank Mandarino return 0; 2186db2a4165SFrank Mandarino } 2187db2a4165SFrank Mandarino EXPORT_SYMBOL_GPL(snd_soc_get_volsw_2r); 2188db2a4165SFrank Mandarino 2189db2a4165SFrank Mandarino /** 2190db2a4165SFrank Mandarino * snd_soc_put_volsw_2r - double mixer set callback 2191db2a4165SFrank Mandarino * @kcontrol: mixer control 2192ac11a2b3SMark Brown * @ucontrol: control element information 2193db2a4165SFrank Mandarino * 2194db2a4165SFrank Mandarino * Callback to set the value of a double mixer control that spans 2 registers. 2195db2a4165SFrank Mandarino * 2196db2a4165SFrank Mandarino * Returns 0 for success. 2197db2a4165SFrank Mandarino */ 2198db2a4165SFrank Mandarino int snd_soc_put_volsw_2r(struct snd_kcontrol *kcontrol, 2199db2a4165SFrank Mandarino struct snd_ctl_elem_value *ucontrol) 2200db2a4165SFrank Mandarino { 22014eaa9819SJon Smirl struct soc_mixer_control *mc = 22024eaa9819SJon Smirl (struct soc_mixer_control *)kcontrol->private_value; 2203db2a4165SFrank Mandarino struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol); 2204815ecf8dSJon Smirl unsigned int reg = mc->reg; 2205815ecf8dSJon Smirl unsigned int reg2 = mc->rreg; 2206815ecf8dSJon Smirl unsigned int shift = mc->shift; 22074eaa9819SJon Smirl int max = mc->max; 2208815ecf8dSJon Smirl unsigned int mask = (1 << fls(max)) - 1; 2209815ecf8dSJon Smirl unsigned int invert = mc->invert; 2210db2a4165SFrank Mandarino int err; 221146f5822fSDaniel Ribeiro unsigned int val, val2, val_mask; 2212db2a4165SFrank Mandarino 2213db2a4165SFrank Mandarino val_mask = mask << shift; 2214db2a4165SFrank Mandarino val = (ucontrol->value.integer.value[0] & mask); 2215db2a4165SFrank Mandarino val2 = (ucontrol->value.integer.value[1] & mask); 2216db2a4165SFrank Mandarino 2217db2a4165SFrank Mandarino if (invert) { 2218a7a4ac86SPhilipp Zabel val = max - val; 2219a7a4ac86SPhilipp Zabel val2 = max - val2; 2220db2a4165SFrank Mandarino } 2221db2a4165SFrank Mandarino 2222db2a4165SFrank Mandarino val = val << shift; 2223db2a4165SFrank Mandarino val2 = val2 << shift; 2224db2a4165SFrank Mandarino 22256c508c62SEero Nurkkala err = snd_soc_update_bits_locked(codec, reg, val_mask, val); 22263ff3f64bSMark Brown if (err < 0) 2227db2a4165SFrank Mandarino return err; 2228db2a4165SFrank Mandarino 22296c508c62SEero Nurkkala err = snd_soc_update_bits_locked(codec, reg2, val_mask, val2); 2230db2a4165SFrank Mandarino return err; 2231db2a4165SFrank Mandarino } 2232db2a4165SFrank Mandarino EXPORT_SYMBOL_GPL(snd_soc_put_volsw_2r); 2233db2a4165SFrank Mandarino 2234e13ac2e9SMark Brown /** 2235e13ac2e9SMark Brown * snd_soc_info_volsw_s8 - signed mixer info callback 2236e13ac2e9SMark Brown * @kcontrol: mixer control 2237e13ac2e9SMark Brown * @uinfo: control element information 2238e13ac2e9SMark Brown * 2239e13ac2e9SMark Brown * Callback to provide information about a signed mixer control. 2240e13ac2e9SMark Brown * 2241e13ac2e9SMark Brown * Returns 0 for success. 2242e13ac2e9SMark Brown */ 2243e13ac2e9SMark Brown int snd_soc_info_volsw_s8(struct snd_kcontrol *kcontrol, 2244e13ac2e9SMark Brown struct snd_ctl_elem_info *uinfo) 2245e13ac2e9SMark Brown { 22464eaa9819SJon Smirl struct soc_mixer_control *mc = 22474eaa9819SJon Smirl (struct soc_mixer_control *)kcontrol->private_value; 2248d11bb4a9SPeter Ujfalusi int platform_max; 22494eaa9819SJon Smirl int min = mc->min; 2250e13ac2e9SMark Brown 2251d11bb4a9SPeter Ujfalusi if (!mc->platform_max) 2252d11bb4a9SPeter Ujfalusi mc->platform_max = mc->max; 2253d11bb4a9SPeter Ujfalusi platform_max = mc->platform_max; 2254d11bb4a9SPeter Ujfalusi 2255e13ac2e9SMark Brown uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER; 2256e13ac2e9SMark Brown uinfo->count = 2; 2257e13ac2e9SMark Brown uinfo->value.integer.min = 0; 2258d11bb4a9SPeter Ujfalusi uinfo->value.integer.max = platform_max - min; 2259e13ac2e9SMark Brown return 0; 2260e13ac2e9SMark Brown } 2261e13ac2e9SMark Brown EXPORT_SYMBOL_GPL(snd_soc_info_volsw_s8); 2262e13ac2e9SMark Brown 2263e13ac2e9SMark Brown /** 2264e13ac2e9SMark Brown * snd_soc_get_volsw_s8 - signed mixer get callback 2265e13ac2e9SMark Brown * @kcontrol: mixer control 2266ac11a2b3SMark Brown * @ucontrol: control element information 2267e13ac2e9SMark Brown * 2268e13ac2e9SMark Brown * Callback to get the value of a signed mixer control. 2269e13ac2e9SMark Brown * 2270e13ac2e9SMark Brown * Returns 0 for success. 2271e13ac2e9SMark Brown */ 2272e13ac2e9SMark Brown int snd_soc_get_volsw_s8(struct snd_kcontrol *kcontrol, 2273e13ac2e9SMark Brown struct snd_ctl_elem_value *ucontrol) 2274e13ac2e9SMark Brown { 22754eaa9819SJon Smirl struct soc_mixer_control *mc = 22764eaa9819SJon Smirl (struct soc_mixer_control *)kcontrol->private_value; 2277e13ac2e9SMark Brown struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol); 2278815ecf8dSJon Smirl unsigned int reg = mc->reg; 22794eaa9819SJon Smirl int min = mc->min; 2280e13ac2e9SMark Brown int val = snd_soc_read(codec, reg); 2281e13ac2e9SMark Brown 2282e13ac2e9SMark Brown ucontrol->value.integer.value[0] = 2283e13ac2e9SMark Brown ((signed char)(val & 0xff))-min; 2284e13ac2e9SMark Brown ucontrol->value.integer.value[1] = 2285e13ac2e9SMark Brown ((signed char)((val >> 8) & 0xff))-min; 2286e13ac2e9SMark Brown return 0; 2287e13ac2e9SMark Brown } 2288e13ac2e9SMark Brown EXPORT_SYMBOL_GPL(snd_soc_get_volsw_s8); 2289e13ac2e9SMark Brown 2290e13ac2e9SMark Brown /** 2291e13ac2e9SMark Brown * snd_soc_put_volsw_sgn - signed mixer put callback 2292e13ac2e9SMark Brown * @kcontrol: mixer control 2293ac11a2b3SMark Brown * @ucontrol: control element information 2294e13ac2e9SMark Brown * 2295e13ac2e9SMark Brown * Callback to set the value of a signed mixer control. 2296e13ac2e9SMark Brown * 2297e13ac2e9SMark Brown * Returns 0 for success. 2298e13ac2e9SMark Brown */ 2299e13ac2e9SMark Brown int snd_soc_put_volsw_s8(struct snd_kcontrol *kcontrol, 2300e13ac2e9SMark Brown struct snd_ctl_elem_value *ucontrol) 2301e13ac2e9SMark Brown { 23024eaa9819SJon Smirl struct soc_mixer_control *mc = 23034eaa9819SJon Smirl (struct soc_mixer_control *)kcontrol->private_value; 2304e13ac2e9SMark Brown struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol); 2305815ecf8dSJon Smirl unsigned int reg = mc->reg; 23064eaa9819SJon Smirl int min = mc->min; 230746f5822fSDaniel Ribeiro unsigned int val; 2308e13ac2e9SMark Brown 2309e13ac2e9SMark Brown val = (ucontrol->value.integer.value[0]+min) & 0xff; 2310e13ac2e9SMark Brown val |= ((ucontrol->value.integer.value[1]+min) & 0xff) << 8; 2311e13ac2e9SMark Brown 23126c508c62SEero Nurkkala return snd_soc_update_bits_locked(codec, reg, 0xffff, val); 2313e13ac2e9SMark Brown } 2314e13ac2e9SMark Brown EXPORT_SYMBOL_GPL(snd_soc_put_volsw_s8); 2315e13ac2e9SMark Brown 23168c6529dbSLiam Girdwood /** 2317637d3847SPeter Ujfalusi * snd_soc_limit_volume - Set new limit to an existing volume control. 2318637d3847SPeter Ujfalusi * 2319637d3847SPeter Ujfalusi * @codec: where to look for the control 2320637d3847SPeter Ujfalusi * @name: Name of the control 2321637d3847SPeter Ujfalusi * @max: new maximum limit 2322637d3847SPeter Ujfalusi * 2323637d3847SPeter Ujfalusi * Return 0 for success, else error. 2324637d3847SPeter Ujfalusi */ 2325637d3847SPeter Ujfalusi int snd_soc_limit_volume(struct snd_soc_codec *codec, 2326637d3847SPeter Ujfalusi const char *name, int max) 2327637d3847SPeter Ujfalusi { 2328637d3847SPeter Ujfalusi struct snd_card *card = codec->card; 2329637d3847SPeter Ujfalusi struct snd_kcontrol *kctl; 2330637d3847SPeter Ujfalusi struct soc_mixer_control *mc; 2331637d3847SPeter Ujfalusi int found = 0; 2332637d3847SPeter Ujfalusi int ret = -EINVAL; 2333637d3847SPeter Ujfalusi 2334637d3847SPeter Ujfalusi /* Sanity check for name and max */ 2335637d3847SPeter Ujfalusi if (unlikely(!name || max <= 0)) 2336637d3847SPeter Ujfalusi return -EINVAL; 2337637d3847SPeter Ujfalusi 2338637d3847SPeter Ujfalusi list_for_each_entry(kctl, &card->controls, list) { 2339637d3847SPeter Ujfalusi if (!strncmp(kctl->id.name, name, sizeof(kctl->id.name))) { 2340637d3847SPeter Ujfalusi found = 1; 2341637d3847SPeter Ujfalusi break; 2342637d3847SPeter Ujfalusi } 2343637d3847SPeter Ujfalusi } 2344637d3847SPeter Ujfalusi if (found) { 2345637d3847SPeter Ujfalusi mc = (struct soc_mixer_control *)kctl->private_value; 2346637d3847SPeter Ujfalusi if (max <= mc->max) { 2347d11bb4a9SPeter Ujfalusi mc->platform_max = max; 2348637d3847SPeter Ujfalusi ret = 0; 2349637d3847SPeter Ujfalusi } 2350637d3847SPeter Ujfalusi } 2351637d3847SPeter Ujfalusi return ret; 2352637d3847SPeter Ujfalusi } 2353637d3847SPeter Ujfalusi EXPORT_SYMBOL_GPL(snd_soc_limit_volume); 2354637d3847SPeter Ujfalusi 2355637d3847SPeter Ujfalusi /** 2356b6f4bb38Sapatard@mandriva.com * snd_soc_info_volsw_2r_sx - double with tlv and variable data size 2357b6f4bb38Sapatard@mandriva.com * mixer info callback 2358b6f4bb38Sapatard@mandriva.com * @kcontrol: mixer control 2359b6f4bb38Sapatard@mandriva.com * @uinfo: control element information 2360b6f4bb38Sapatard@mandriva.com * 2361b6f4bb38Sapatard@mandriva.com * Returns 0 for success. 2362b6f4bb38Sapatard@mandriva.com */ 2363b6f4bb38Sapatard@mandriva.com int snd_soc_info_volsw_2r_sx(struct snd_kcontrol *kcontrol, 2364b6f4bb38Sapatard@mandriva.com struct snd_ctl_elem_info *uinfo) 2365b6f4bb38Sapatard@mandriva.com { 2366b6f4bb38Sapatard@mandriva.com struct soc_mixer_control *mc = 2367b6f4bb38Sapatard@mandriva.com (struct soc_mixer_control *)kcontrol->private_value; 2368b6f4bb38Sapatard@mandriva.com int max = mc->max; 2369b6f4bb38Sapatard@mandriva.com int min = mc->min; 2370b6f4bb38Sapatard@mandriva.com 2371b6f4bb38Sapatard@mandriva.com uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER; 2372b6f4bb38Sapatard@mandriva.com uinfo->count = 2; 2373b6f4bb38Sapatard@mandriva.com uinfo->value.integer.min = 0; 2374b6f4bb38Sapatard@mandriva.com uinfo->value.integer.max = max-min; 2375b6f4bb38Sapatard@mandriva.com 2376b6f4bb38Sapatard@mandriva.com return 0; 2377b6f4bb38Sapatard@mandriva.com } 2378b6f4bb38Sapatard@mandriva.com EXPORT_SYMBOL_GPL(snd_soc_info_volsw_2r_sx); 2379b6f4bb38Sapatard@mandriva.com 2380b6f4bb38Sapatard@mandriva.com /** 2381b6f4bb38Sapatard@mandriva.com * snd_soc_get_volsw_2r_sx - double with tlv and variable data size 2382b6f4bb38Sapatard@mandriva.com * mixer get callback 2383b6f4bb38Sapatard@mandriva.com * @kcontrol: mixer control 2384b6f4bb38Sapatard@mandriva.com * @uinfo: control element information 2385b6f4bb38Sapatard@mandriva.com * 2386b6f4bb38Sapatard@mandriva.com * Returns 0 for success. 2387b6f4bb38Sapatard@mandriva.com */ 2388b6f4bb38Sapatard@mandriva.com int snd_soc_get_volsw_2r_sx(struct snd_kcontrol *kcontrol, 2389b6f4bb38Sapatard@mandriva.com struct snd_ctl_elem_value *ucontrol) 2390b6f4bb38Sapatard@mandriva.com { 2391b6f4bb38Sapatard@mandriva.com struct soc_mixer_control *mc = 2392b6f4bb38Sapatard@mandriva.com (struct soc_mixer_control *)kcontrol->private_value; 2393b6f4bb38Sapatard@mandriva.com struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol); 2394b6f4bb38Sapatard@mandriva.com unsigned int mask = (1<<mc->shift)-1; 2395b6f4bb38Sapatard@mandriva.com int min = mc->min; 2396b6f4bb38Sapatard@mandriva.com int val = snd_soc_read(codec, mc->reg) & mask; 2397b6f4bb38Sapatard@mandriva.com int valr = snd_soc_read(codec, mc->rreg) & mask; 2398b6f4bb38Sapatard@mandriva.com 2399b6f4bb38Sapatard@mandriva.com ucontrol->value.integer.value[0] = ((val & 0xff)-min); 2400b6f4bb38Sapatard@mandriva.com ucontrol->value.integer.value[1] = ((valr & 0xff)-min); 2401b6f4bb38Sapatard@mandriva.com return 0; 2402b6f4bb38Sapatard@mandriva.com } 2403b6f4bb38Sapatard@mandriva.com EXPORT_SYMBOL_GPL(snd_soc_get_volsw_2r_sx); 2404b6f4bb38Sapatard@mandriva.com 2405b6f4bb38Sapatard@mandriva.com /** 2406b6f4bb38Sapatard@mandriva.com * snd_soc_put_volsw_2r_sx - double with tlv and variable data size 2407b6f4bb38Sapatard@mandriva.com * mixer put callback 2408b6f4bb38Sapatard@mandriva.com * @kcontrol: mixer control 2409b6f4bb38Sapatard@mandriva.com * @uinfo: control element information 2410b6f4bb38Sapatard@mandriva.com * 2411b6f4bb38Sapatard@mandriva.com * Returns 0 for success. 2412b6f4bb38Sapatard@mandriva.com */ 2413b6f4bb38Sapatard@mandriva.com int snd_soc_put_volsw_2r_sx(struct snd_kcontrol *kcontrol, 2414b6f4bb38Sapatard@mandriva.com struct snd_ctl_elem_value *ucontrol) 2415b6f4bb38Sapatard@mandriva.com { 2416b6f4bb38Sapatard@mandriva.com struct soc_mixer_control *mc = 2417b6f4bb38Sapatard@mandriva.com (struct soc_mixer_control *)kcontrol->private_value; 2418b6f4bb38Sapatard@mandriva.com struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol); 2419b6f4bb38Sapatard@mandriva.com unsigned int mask = (1<<mc->shift)-1; 2420b6f4bb38Sapatard@mandriva.com int min = mc->min; 2421b6f4bb38Sapatard@mandriva.com int ret; 2422b6f4bb38Sapatard@mandriva.com unsigned int val, valr, oval, ovalr; 2423b6f4bb38Sapatard@mandriva.com 2424b6f4bb38Sapatard@mandriva.com val = ((ucontrol->value.integer.value[0]+min) & 0xff); 2425b6f4bb38Sapatard@mandriva.com val &= mask; 2426b6f4bb38Sapatard@mandriva.com valr = ((ucontrol->value.integer.value[1]+min) & 0xff); 2427b6f4bb38Sapatard@mandriva.com valr &= mask; 2428b6f4bb38Sapatard@mandriva.com 2429b6f4bb38Sapatard@mandriva.com oval = snd_soc_read(codec, mc->reg) & mask; 2430b6f4bb38Sapatard@mandriva.com ovalr = snd_soc_read(codec, mc->rreg) & mask; 2431b6f4bb38Sapatard@mandriva.com 2432b6f4bb38Sapatard@mandriva.com ret = 0; 2433b6f4bb38Sapatard@mandriva.com if (oval != val) { 2434b6f4bb38Sapatard@mandriva.com ret = snd_soc_write(codec, mc->reg, val); 2435b6f4bb38Sapatard@mandriva.com if (ret < 0) 2436*f1df5aecSMark Brown return ret; 2437b6f4bb38Sapatard@mandriva.com } 2438b6f4bb38Sapatard@mandriva.com if (ovalr != valr) { 2439b6f4bb38Sapatard@mandriva.com ret = snd_soc_write(codec, mc->rreg, valr); 2440b6f4bb38Sapatard@mandriva.com if (ret < 0) 2441*f1df5aecSMark Brown return ret; 2442b6f4bb38Sapatard@mandriva.com } 2443b6f4bb38Sapatard@mandriva.com 2444b6f4bb38Sapatard@mandriva.com return 0; 2445b6f4bb38Sapatard@mandriva.com } 2446b6f4bb38Sapatard@mandriva.com EXPORT_SYMBOL_GPL(snd_soc_put_volsw_2r_sx); 2447b6f4bb38Sapatard@mandriva.com 2448b6f4bb38Sapatard@mandriva.com /** 24498c6529dbSLiam Girdwood * snd_soc_dai_set_sysclk - configure DAI system or master clock. 24508c6529dbSLiam Girdwood * @dai: DAI 24518c6529dbSLiam Girdwood * @clk_id: DAI specific clock ID 24528c6529dbSLiam Girdwood * @freq: new clock frequency in Hz 24538c6529dbSLiam Girdwood * @dir: new clock direction - input/output. 24548c6529dbSLiam Girdwood * 24558c6529dbSLiam Girdwood * Configures the DAI master (MCLK) or system (SYSCLK) clocking. 24568c6529dbSLiam Girdwood */ 24578c6529dbSLiam Girdwood int snd_soc_dai_set_sysclk(struct snd_soc_dai *dai, int clk_id, 24588c6529dbSLiam Girdwood unsigned int freq, int dir) 24598c6529dbSLiam Girdwood { 24603f1a4d82SMark Brown if (dai->ops && dai->ops->set_sysclk) 24616335d055SEric Miao return dai->ops->set_sysclk(dai, clk_id, freq, dir); 24628c6529dbSLiam Girdwood else 24638c6529dbSLiam Girdwood return -EINVAL; 24648c6529dbSLiam Girdwood } 24658c6529dbSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_dai_set_sysclk); 24668c6529dbSLiam Girdwood 24678c6529dbSLiam Girdwood /** 24688c6529dbSLiam Girdwood * snd_soc_dai_set_clkdiv - configure DAI clock dividers. 24698c6529dbSLiam Girdwood * @dai: DAI 2470ac11a2b3SMark Brown * @div_id: DAI specific clock divider ID 24718c6529dbSLiam Girdwood * @div: new clock divisor. 24728c6529dbSLiam Girdwood * 24738c6529dbSLiam Girdwood * Configures the clock dividers. This is used to derive the best DAI bit and 24748c6529dbSLiam Girdwood * frame clocks from the system or master clock. It's best to set the DAI bit 24758c6529dbSLiam Girdwood * and frame clocks as low as possible to save system power. 24768c6529dbSLiam Girdwood */ 24778c6529dbSLiam Girdwood int snd_soc_dai_set_clkdiv(struct snd_soc_dai *dai, 24788c6529dbSLiam Girdwood int div_id, int div) 24798c6529dbSLiam Girdwood { 24803f1a4d82SMark Brown if (dai->ops && dai->ops->set_clkdiv) 24816335d055SEric Miao return dai->ops->set_clkdiv(dai, div_id, div); 24828c6529dbSLiam Girdwood else 24838c6529dbSLiam Girdwood return -EINVAL; 24848c6529dbSLiam Girdwood } 24858c6529dbSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_dai_set_clkdiv); 24868c6529dbSLiam Girdwood 24878c6529dbSLiam Girdwood /** 24888c6529dbSLiam Girdwood * snd_soc_dai_set_pll - configure DAI PLL. 24898c6529dbSLiam Girdwood * @dai: DAI 24908c6529dbSLiam Girdwood * @pll_id: DAI specific PLL ID 249185488037SMark Brown * @source: DAI specific source for the PLL 24928c6529dbSLiam Girdwood * @freq_in: PLL input clock frequency in Hz 24938c6529dbSLiam Girdwood * @freq_out: requested PLL output clock frequency in Hz 24948c6529dbSLiam Girdwood * 24958c6529dbSLiam Girdwood * Configures and enables PLL to generate output clock based on input clock. 24968c6529dbSLiam Girdwood */ 249785488037SMark Brown int snd_soc_dai_set_pll(struct snd_soc_dai *dai, int pll_id, int source, 249885488037SMark Brown unsigned int freq_in, unsigned int freq_out) 24998c6529dbSLiam Girdwood { 25003f1a4d82SMark Brown if (dai->ops && dai->ops->set_pll) 250185488037SMark Brown return dai->ops->set_pll(dai, pll_id, source, 250285488037SMark Brown freq_in, freq_out); 25038c6529dbSLiam Girdwood else 25048c6529dbSLiam Girdwood return -EINVAL; 25058c6529dbSLiam Girdwood } 25068c6529dbSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_dai_set_pll); 25078c6529dbSLiam Girdwood 25088c6529dbSLiam Girdwood /** 25098c6529dbSLiam Girdwood * snd_soc_dai_set_fmt - configure DAI hardware audio format. 25108c6529dbSLiam Girdwood * @dai: DAI 25118c6529dbSLiam Girdwood * @fmt: SND_SOC_DAIFMT_ format value. 25128c6529dbSLiam Girdwood * 25138c6529dbSLiam Girdwood * Configures the DAI hardware format and clocking. 25148c6529dbSLiam Girdwood */ 25158c6529dbSLiam Girdwood int snd_soc_dai_set_fmt(struct snd_soc_dai *dai, unsigned int fmt) 25168c6529dbSLiam Girdwood { 25173f1a4d82SMark Brown if (dai->ops && dai->ops->set_fmt) 25186335d055SEric Miao return dai->ops->set_fmt(dai, fmt); 25198c6529dbSLiam Girdwood else 25208c6529dbSLiam Girdwood return -EINVAL; 25218c6529dbSLiam Girdwood } 25228c6529dbSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_dai_set_fmt); 25238c6529dbSLiam Girdwood 25248c6529dbSLiam Girdwood /** 25258c6529dbSLiam Girdwood * snd_soc_dai_set_tdm_slot - configure DAI TDM. 25268c6529dbSLiam Girdwood * @dai: DAI 2527a5479e38SDaniel Ribeiro * @tx_mask: bitmask representing active TX slots. 2528a5479e38SDaniel Ribeiro * @rx_mask: bitmask representing active RX slots. 25298c6529dbSLiam Girdwood * @slots: Number of slots in use. 2530a5479e38SDaniel Ribeiro * @slot_width: Width in bits for each slot. 25318c6529dbSLiam Girdwood * 25328c6529dbSLiam Girdwood * Configures a DAI for TDM operation. Both mask and slots are codec and DAI 25338c6529dbSLiam Girdwood * specific. 25348c6529dbSLiam Girdwood */ 25358c6529dbSLiam Girdwood int snd_soc_dai_set_tdm_slot(struct snd_soc_dai *dai, 2536a5479e38SDaniel Ribeiro unsigned int tx_mask, unsigned int rx_mask, int slots, int slot_width) 25378c6529dbSLiam Girdwood { 25383f1a4d82SMark Brown if (dai->ops && dai->ops->set_tdm_slot) 2539a5479e38SDaniel Ribeiro return dai->ops->set_tdm_slot(dai, tx_mask, rx_mask, 2540a5479e38SDaniel Ribeiro slots, slot_width); 25418c6529dbSLiam Girdwood else 25428c6529dbSLiam Girdwood return -EINVAL; 25438c6529dbSLiam Girdwood } 25448c6529dbSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_dai_set_tdm_slot); 25458c6529dbSLiam Girdwood 25468c6529dbSLiam Girdwood /** 2547472df3cbSBarry Song * snd_soc_dai_set_channel_map - configure DAI audio channel map 2548472df3cbSBarry Song * @dai: DAI 2549472df3cbSBarry Song * @tx_num: how many TX channels 2550472df3cbSBarry Song * @tx_slot: pointer to an array which imply the TX slot number channel 2551472df3cbSBarry Song * 0~num-1 uses 2552472df3cbSBarry Song * @rx_num: how many RX channels 2553472df3cbSBarry Song * @rx_slot: pointer to an array which imply the RX slot number channel 2554472df3cbSBarry Song * 0~num-1 uses 2555472df3cbSBarry Song * 2556472df3cbSBarry Song * configure the relationship between channel number and TDM slot number. 2557472df3cbSBarry Song */ 2558472df3cbSBarry Song int snd_soc_dai_set_channel_map(struct snd_soc_dai *dai, 2559472df3cbSBarry Song unsigned int tx_num, unsigned int *tx_slot, 2560472df3cbSBarry Song unsigned int rx_num, unsigned int *rx_slot) 2561472df3cbSBarry Song { 2562472df3cbSBarry Song if (dai->ops && dai->ops->set_channel_map) 2563472df3cbSBarry Song return dai->ops->set_channel_map(dai, tx_num, tx_slot, 2564472df3cbSBarry Song rx_num, rx_slot); 2565472df3cbSBarry Song else 2566472df3cbSBarry Song return -EINVAL; 2567472df3cbSBarry Song } 2568472df3cbSBarry Song EXPORT_SYMBOL_GPL(snd_soc_dai_set_channel_map); 2569472df3cbSBarry Song 2570472df3cbSBarry Song /** 25718c6529dbSLiam Girdwood * snd_soc_dai_set_tristate - configure DAI system or master clock. 25728c6529dbSLiam Girdwood * @dai: DAI 25738c6529dbSLiam Girdwood * @tristate: tristate enable 25748c6529dbSLiam Girdwood * 25758c6529dbSLiam Girdwood * Tristates the DAI so that others can use it. 25768c6529dbSLiam Girdwood */ 25778c6529dbSLiam Girdwood int snd_soc_dai_set_tristate(struct snd_soc_dai *dai, int tristate) 25788c6529dbSLiam Girdwood { 25793f1a4d82SMark Brown if (dai->ops && dai->ops->set_tristate) 25806335d055SEric Miao return dai->ops->set_tristate(dai, tristate); 25818c6529dbSLiam Girdwood else 25828c6529dbSLiam Girdwood return -EINVAL; 25838c6529dbSLiam Girdwood } 25848c6529dbSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_dai_set_tristate); 25858c6529dbSLiam Girdwood 25868c6529dbSLiam Girdwood /** 25878c6529dbSLiam Girdwood * snd_soc_dai_digital_mute - configure DAI system or master clock. 25888c6529dbSLiam Girdwood * @dai: DAI 25898c6529dbSLiam Girdwood * @mute: mute enable 25908c6529dbSLiam Girdwood * 25918c6529dbSLiam Girdwood * Mutes the DAI DAC. 25928c6529dbSLiam Girdwood */ 25938c6529dbSLiam Girdwood int snd_soc_dai_digital_mute(struct snd_soc_dai *dai, int mute) 25948c6529dbSLiam Girdwood { 25953f1a4d82SMark Brown if (dai->ops && dai->ops->digital_mute) 25966335d055SEric Miao return dai->ops->digital_mute(dai, mute); 25978c6529dbSLiam Girdwood else 25988c6529dbSLiam Girdwood return -EINVAL; 25998c6529dbSLiam Girdwood } 26008c6529dbSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_dai_digital_mute); 26018c6529dbSLiam Girdwood 2602c5af3a2eSMark Brown /** 2603c5af3a2eSMark Brown * snd_soc_register_card - Register a card with the ASoC core 2604c5af3a2eSMark Brown * 2605ac11a2b3SMark Brown * @card: Card to register 2606c5af3a2eSMark Brown * 2607c5af3a2eSMark Brown * Note that currently this is an internal only function: it will be 2608c5af3a2eSMark Brown * exposed to machine drivers after further backporting of ASoC v2 2609c5af3a2eSMark Brown * registration APIs. 2610c5af3a2eSMark Brown */ 2611c5af3a2eSMark Brown static int snd_soc_register_card(struct snd_soc_card *card) 2612c5af3a2eSMark Brown { 2613c5af3a2eSMark Brown if (!card->name || !card->dev) 2614c5af3a2eSMark Brown return -EINVAL; 2615c5af3a2eSMark Brown 2616c5af3a2eSMark Brown INIT_LIST_HEAD(&card->list); 2617c5af3a2eSMark Brown card->instantiated = 0; 2618c5af3a2eSMark Brown 2619c5af3a2eSMark Brown mutex_lock(&client_mutex); 2620c5af3a2eSMark Brown list_add(&card->list, &card_list); 2621435c5e25SMark Brown snd_soc_instantiate_cards(); 2622c5af3a2eSMark Brown mutex_unlock(&client_mutex); 2623c5af3a2eSMark Brown 2624c5af3a2eSMark Brown dev_dbg(card->dev, "Registered card '%s'\n", card->name); 2625c5af3a2eSMark Brown 2626c5af3a2eSMark Brown return 0; 2627c5af3a2eSMark Brown } 2628c5af3a2eSMark Brown 2629c5af3a2eSMark Brown /** 2630c5af3a2eSMark Brown * snd_soc_unregister_card - Unregister a card with the ASoC core 2631c5af3a2eSMark Brown * 2632ac11a2b3SMark Brown * @card: Card to unregister 2633c5af3a2eSMark Brown * 2634c5af3a2eSMark Brown * Note that currently this is an internal only function: it will be 2635c5af3a2eSMark Brown * exposed to machine drivers after further backporting of ASoC v2 2636c5af3a2eSMark Brown * registration APIs. 2637c5af3a2eSMark Brown */ 2638c5af3a2eSMark Brown static int snd_soc_unregister_card(struct snd_soc_card *card) 2639c5af3a2eSMark Brown { 2640c5af3a2eSMark Brown mutex_lock(&client_mutex); 2641c5af3a2eSMark Brown list_del(&card->list); 2642c5af3a2eSMark Brown mutex_unlock(&client_mutex); 2643c5af3a2eSMark Brown 2644c5af3a2eSMark Brown dev_dbg(card->dev, "Unregistered card '%s'\n", card->name); 2645c5af3a2eSMark Brown 2646c5af3a2eSMark Brown return 0; 2647c5af3a2eSMark Brown } 2648c5af3a2eSMark Brown 26499115171aSMark Brown /** 26509115171aSMark Brown * snd_soc_register_dai - Register a DAI with the ASoC core 26519115171aSMark Brown * 2652ac11a2b3SMark Brown * @dai: DAI to register 26539115171aSMark Brown */ 26549115171aSMark Brown int snd_soc_register_dai(struct snd_soc_dai *dai) 26559115171aSMark Brown { 26569115171aSMark Brown if (!dai->name) 26579115171aSMark Brown return -EINVAL; 26589115171aSMark Brown 26599115171aSMark Brown /* The device should become mandatory over time */ 26609115171aSMark Brown if (!dai->dev) 26619115171aSMark Brown printk(KERN_WARNING "No device for DAI %s\n", dai->name); 26629115171aSMark Brown 26636335d055SEric Miao if (!dai->ops) 26646335d055SEric Miao dai->ops = &null_dai_ops; 26656335d055SEric Miao 26669115171aSMark Brown INIT_LIST_HEAD(&dai->list); 26679115171aSMark Brown 26689115171aSMark Brown mutex_lock(&client_mutex); 26699115171aSMark Brown list_add(&dai->list, &dai_list); 2670435c5e25SMark Brown snd_soc_instantiate_cards(); 26719115171aSMark Brown mutex_unlock(&client_mutex); 26729115171aSMark Brown 26739115171aSMark Brown pr_debug("Registered DAI '%s'\n", dai->name); 26749115171aSMark Brown 26759115171aSMark Brown return 0; 26769115171aSMark Brown } 26779115171aSMark Brown EXPORT_SYMBOL_GPL(snd_soc_register_dai); 26789115171aSMark Brown 26799115171aSMark Brown /** 26809115171aSMark Brown * snd_soc_unregister_dai - Unregister a DAI from the ASoC core 26819115171aSMark Brown * 2682ac11a2b3SMark Brown * @dai: DAI to unregister 26839115171aSMark Brown */ 26849115171aSMark Brown void snd_soc_unregister_dai(struct snd_soc_dai *dai) 26859115171aSMark Brown { 26869115171aSMark Brown mutex_lock(&client_mutex); 26879115171aSMark Brown list_del(&dai->list); 26889115171aSMark Brown mutex_unlock(&client_mutex); 26899115171aSMark Brown 26909115171aSMark Brown pr_debug("Unregistered DAI '%s'\n", dai->name); 26919115171aSMark Brown } 26929115171aSMark Brown EXPORT_SYMBOL_GPL(snd_soc_unregister_dai); 26939115171aSMark Brown 26949115171aSMark Brown /** 26959115171aSMark Brown * snd_soc_register_dais - Register multiple DAIs with the ASoC core 26969115171aSMark Brown * 2697ac11a2b3SMark Brown * @dai: Array of DAIs to register 2698ac11a2b3SMark Brown * @count: Number of DAIs 26999115171aSMark Brown */ 27009115171aSMark Brown int snd_soc_register_dais(struct snd_soc_dai *dai, size_t count) 27019115171aSMark Brown { 27029115171aSMark Brown int i, ret; 27039115171aSMark Brown 27049115171aSMark Brown for (i = 0; i < count; i++) { 27059115171aSMark Brown ret = snd_soc_register_dai(&dai[i]); 27069115171aSMark Brown if (ret != 0) 27079115171aSMark Brown goto err; 27089115171aSMark Brown } 27099115171aSMark Brown 27109115171aSMark Brown return 0; 27119115171aSMark Brown 27129115171aSMark Brown err: 27139115171aSMark Brown for (i--; i >= 0; i--) 27149115171aSMark Brown snd_soc_unregister_dai(&dai[i]); 27159115171aSMark Brown 27169115171aSMark Brown return ret; 27179115171aSMark Brown } 27189115171aSMark Brown EXPORT_SYMBOL_GPL(snd_soc_register_dais); 27199115171aSMark Brown 27209115171aSMark Brown /** 27219115171aSMark Brown * snd_soc_unregister_dais - Unregister multiple DAIs from the ASoC core 27229115171aSMark Brown * 2723ac11a2b3SMark Brown * @dai: Array of DAIs to unregister 2724ac11a2b3SMark Brown * @count: Number of DAIs 27259115171aSMark Brown */ 27269115171aSMark Brown void snd_soc_unregister_dais(struct snd_soc_dai *dai, size_t count) 27279115171aSMark Brown { 27289115171aSMark Brown int i; 27299115171aSMark Brown 27309115171aSMark Brown for (i = 0; i < count; i++) 27319115171aSMark Brown snd_soc_unregister_dai(&dai[i]); 27329115171aSMark Brown } 27339115171aSMark Brown EXPORT_SYMBOL_GPL(snd_soc_unregister_dais); 27349115171aSMark Brown 273512a48a8cSMark Brown /** 273612a48a8cSMark Brown * snd_soc_register_platform - Register a platform with the ASoC core 273712a48a8cSMark Brown * 2738ac11a2b3SMark Brown * @platform: platform to register 273912a48a8cSMark Brown */ 274012a48a8cSMark Brown int snd_soc_register_platform(struct snd_soc_platform *platform) 274112a48a8cSMark Brown { 274212a48a8cSMark Brown if (!platform->name) 274312a48a8cSMark Brown return -EINVAL; 274412a48a8cSMark Brown 274512a48a8cSMark Brown INIT_LIST_HEAD(&platform->list); 274612a48a8cSMark Brown 274712a48a8cSMark Brown mutex_lock(&client_mutex); 274812a48a8cSMark Brown list_add(&platform->list, &platform_list); 2749435c5e25SMark Brown snd_soc_instantiate_cards(); 275012a48a8cSMark Brown mutex_unlock(&client_mutex); 275112a48a8cSMark Brown 275212a48a8cSMark Brown pr_debug("Registered platform '%s'\n", platform->name); 275312a48a8cSMark Brown 275412a48a8cSMark Brown return 0; 275512a48a8cSMark Brown } 275612a48a8cSMark Brown EXPORT_SYMBOL_GPL(snd_soc_register_platform); 275712a48a8cSMark Brown 275812a48a8cSMark Brown /** 275912a48a8cSMark Brown * snd_soc_unregister_platform - Unregister a platform from the ASoC core 276012a48a8cSMark Brown * 2761ac11a2b3SMark Brown * @platform: platform to unregister 276212a48a8cSMark Brown */ 276312a48a8cSMark Brown void snd_soc_unregister_platform(struct snd_soc_platform *platform) 276412a48a8cSMark Brown { 276512a48a8cSMark Brown mutex_lock(&client_mutex); 276612a48a8cSMark Brown list_del(&platform->list); 276712a48a8cSMark Brown mutex_unlock(&client_mutex); 276812a48a8cSMark Brown 276912a48a8cSMark Brown pr_debug("Unregistered platform '%s'\n", platform->name); 277012a48a8cSMark Brown } 277112a48a8cSMark Brown EXPORT_SYMBOL_GPL(snd_soc_unregister_platform); 277212a48a8cSMark Brown 2773151ab22cSMark Brown static u64 codec_format_map[] = { 2774151ab22cSMark Brown SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S16_BE, 2775151ab22cSMark Brown SNDRV_PCM_FMTBIT_U16_LE | SNDRV_PCM_FMTBIT_U16_BE, 2776151ab22cSMark Brown SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S24_BE, 2777151ab22cSMark Brown SNDRV_PCM_FMTBIT_U24_LE | SNDRV_PCM_FMTBIT_U24_BE, 2778151ab22cSMark Brown SNDRV_PCM_FMTBIT_S32_LE | SNDRV_PCM_FMTBIT_S32_BE, 2779151ab22cSMark Brown SNDRV_PCM_FMTBIT_U32_LE | SNDRV_PCM_FMTBIT_U32_BE, 2780151ab22cSMark Brown SNDRV_PCM_FMTBIT_S24_3LE | SNDRV_PCM_FMTBIT_U24_3BE, 2781151ab22cSMark Brown SNDRV_PCM_FMTBIT_U24_3LE | SNDRV_PCM_FMTBIT_U24_3BE, 2782151ab22cSMark Brown SNDRV_PCM_FMTBIT_S20_3LE | SNDRV_PCM_FMTBIT_S20_3BE, 2783151ab22cSMark Brown SNDRV_PCM_FMTBIT_U20_3LE | SNDRV_PCM_FMTBIT_U20_3BE, 2784151ab22cSMark Brown SNDRV_PCM_FMTBIT_S18_3LE | SNDRV_PCM_FMTBIT_S18_3BE, 2785151ab22cSMark Brown SNDRV_PCM_FMTBIT_U18_3LE | SNDRV_PCM_FMTBIT_U18_3BE, 2786151ab22cSMark Brown SNDRV_PCM_FMTBIT_FLOAT_LE | SNDRV_PCM_FMTBIT_FLOAT_BE, 2787151ab22cSMark Brown SNDRV_PCM_FMTBIT_FLOAT64_LE | SNDRV_PCM_FMTBIT_FLOAT64_BE, 2788151ab22cSMark Brown SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_LE 2789151ab22cSMark Brown | SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_BE, 2790151ab22cSMark Brown }; 2791151ab22cSMark Brown 2792151ab22cSMark Brown /* Fix up the DAI formats for endianness: codecs don't actually see 2793151ab22cSMark Brown * the endianness of the data but we're using the CPU format 2794151ab22cSMark Brown * definitions which do need to include endianness so we ensure that 2795151ab22cSMark Brown * codec DAIs always have both big and little endian variants set. 2796151ab22cSMark Brown */ 2797151ab22cSMark Brown static void fixup_codec_formats(struct snd_soc_pcm_stream *stream) 2798151ab22cSMark Brown { 2799151ab22cSMark Brown int i; 2800151ab22cSMark Brown 2801151ab22cSMark Brown for (i = 0; i < ARRAY_SIZE(codec_format_map); i++) 2802151ab22cSMark Brown if (stream->formats & codec_format_map[i]) 2803151ab22cSMark Brown stream->formats |= codec_format_map[i]; 2804151ab22cSMark Brown } 2805151ab22cSMark Brown 28060d0cf00aSMark Brown /** 28070d0cf00aSMark Brown * snd_soc_register_codec - Register a codec with the ASoC core 28080d0cf00aSMark Brown * 2809ac11a2b3SMark Brown * @codec: codec to register 28100d0cf00aSMark Brown */ 28110d0cf00aSMark Brown int snd_soc_register_codec(struct snd_soc_codec *codec) 28120d0cf00aSMark Brown { 2813151ab22cSMark Brown int i; 2814151ab22cSMark Brown 28150d0cf00aSMark Brown if (!codec->name) 28160d0cf00aSMark Brown return -EINVAL; 28170d0cf00aSMark Brown 28180d0cf00aSMark Brown /* The device should become mandatory over time */ 28190d0cf00aSMark Brown if (!codec->dev) 28200d0cf00aSMark Brown printk(KERN_WARNING "No device for codec %s\n", codec->name); 28210d0cf00aSMark Brown 28220d0cf00aSMark Brown INIT_LIST_HEAD(&codec->list); 28230d0cf00aSMark Brown 2824151ab22cSMark Brown for (i = 0; i < codec->num_dai; i++) { 2825151ab22cSMark Brown fixup_codec_formats(&codec->dai[i].playback); 2826151ab22cSMark Brown fixup_codec_formats(&codec->dai[i].capture); 2827151ab22cSMark Brown } 2828151ab22cSMark Brown 28290d0cf00aSMark Brown mutex_lock(&client_mutex); 28300d0cf00aSMark Brown list_add(&codec->list, &codec_list); 28310d0cf00aSMark Brown snd_soc_instantiate_cards(); 28320d0cf00aSMark Brown mutex_unlock(&client_mutex); 28330d0cf00aSMark Brown 28340d0cf00aSMark Brown pr_debug("Registered codec '%s'\n", codec->name); 28350d0cf00aSMark Brown 28360d0cf00aSMark Brown return 0; 28370d0cf00aSMark Brown } 28380d0cf00aSMark Brown EXPORT_SYMBOL_GPL(snd_soc_register_codec); 28390d0cf00aSMark Brown 28400d0cf00aSMark Brown /** 28410d0cf00aSMark Brown * snd_soc_unregister_codec - Unregister a codec from the ASoC core 28420d0cf00aSMark Brown * 2843ac11a2b3SMark Brown * @codec: codec to unregister 28440d0cf00aSMark Brown */ 28450d0cf00aSMark Brown void snd_soc_unregister_codec(struct snd_soc_codec *codec) 28460d0cf00aSMark Brown { 28470d0cf00aSMark Brown mutex_lock(&client_mutex); 28480d0cf00aSMark Brown list_del(&codec->list); 28490d0cf00aSMark Brown mutex_unlock(&client_mutex); 28500d0cf00aSMark Brown 28510d0cf00aSMark Brown pr_debug("Unregistered codec '%s'\n", codec->name); 28520d0cf00aSMark Brown } 28530d0cf00aSMark Brown EXPORT_SYMBOL_GPL(snd_soc_unregister_codec); 28540d0cf00aSMark Brown 2855c9b3a40fSTakashi Iwai static int __init snd_soc_init(void) 2856db2a4165SFrank Mandarino { 2857384c89e2SMark Brown #ifdef CONFIG_DEBUG_FS 2858384c89e2SMark Brown debugfs_root = debugfs_create_dir("asoc", NULL); 2859384c89e2SMark Brown if (IS_ERR(debugfs_root) || !debugfs_root) { 2860384c89e2SMark Brown printk(KERN_WARNING 2861384c89e2SMark Brown "ASoC: Failed to create debugfs directory\n"); 2862384c89e2SMark Brown debugfs_root = NULL; 2863384c89e2SMark Brown } 2864384c89e2SMark Brown #endif 2865384c89e2SMark Brown 2866db2a4165SFrank Mandarino return platform_driver_register(&soc_driver); 2867db2a4165SFrank Mandarino } 2868db2a4165SFrank Mandarino 28697d8c16a6SMark Brown static void __exit snd_soc_exit(void) 2870db2a4165SFrank Mandarino { 2871384c89e2SMark Brown #ifdef CONFIG_DEBUG_FS 2872384c89e2SMark Brown debugfs_remove_recursive(debugfs_root); 2873384c89e2SMark Brown #endif 2874db2a4165SFrank Mandarino platform_driver_unregister(&soc_driver); 2875db2a4165SFrank Mandarino } 2876db2a4165SFrank Mandarino 2877db2a4165SFrank Mandarino module_init(snd_soc_init); 2878db2a4165SFrank Mandarino module_exit(snd_soc_exit); 2879db2a4165SFrank Mandarino 2880db2a4165SFrank Mandarino /* Module information */ 2881d331124dSLiam Girdwood MODULE_AUTHOR("Liam Girdwood, lrg@slimlogic.co.uk"); 2882db2a4165SFrank Mandarino MODULE_DESCRIPTION("ALSA SoC Core"); 2883db2a4165SFrank Mandarino MODULE_LICENSE("GPL"); 28848b45a209SKay Sievers MODULE_ALIAS("platform:soc-audio"); 2885