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> 31*474828a4SMarek Vasut #include <sound/ac97_codec.h> 32db2a4165SFrank Mandarino #include <sound/core.h> 33db2a4165SFrank Mandarino #include <sound/pcm.h> 34db2a4165SFrank Mandarino #include <sound/pcm_params.h> 35db2a4165SFrank Mandarino #include <sound/soc.h> 36db2a4165SFrank Mandarino #include <sound/soc-dapm.h> 37db2a4165SFrank Mandarino #include <sound/initval.h> 38db2a4165SFrank Mandarino 39db2a4165SFrank Mandarino static DEFINE_MUTEX(pcm_mutex); 40db2a4165SFrank Mandarino static DEFINE_MUTEX(io_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 84db2a4165SFrank Mandarino #ifdef CONFIG_SND_SOC_AC97_BUS 85db2a4165SFrank Mandarino /* unregister ac97 codec */ 86db2a4165SFrank Mandarino static int soc_ac97_dev_unregister(struct snd_soc_codec *codec) 87db2a4165SFrank Mandarino { 88db2a4165SFrank Mandarino if (codec->ac97->dev.bus) 89db2a4165SFrank Mandarino device_unregister(&codec->ac97->dev); 90db2a4165SFrank Mandarino return 0; 91db2a4165SFrank Mandarino } 92db2a4165SFrank Mandarino 93db2a4165SFrank Mandarino /* stop no dev release warning */ 94db2a4165SFrank Mandarino static void soc_ac97_device_release(struct device *dev){} 95db2a4165SFrank Mandarino 96db2a4165SFrank Mandarino /* register ac97 codec to bus */ 97db2a4165SFrank Mandarino static int soc_ac97_dev_register(struct snd_soc_codec *codec) 98db2a4165SFrank Mandarino { 99db2a4165SFrank Mandarino int err; 100db2a4165SFrank Mandarino 101db2a4165SFrank Mandarino codec->ac97->dev.bus = &ac97_bus_type; 1024ac5c61fSMark Brown codec->ac97->dev.parent = codec->card->dev; 103db2a4165SFrank Mandarino codec->ac97->dev.release = soc_ac97_device_release; 104db2a4165SFrank Mandarino 105bb072bf0SKay Sievers dev_set_name(&codec->ac97->dev, "%d-%d:%s", 106db2a4165SFrank Mandarino codec->card->number, 0, codec->name); 107db2a4165SFrank Mandarino err = device_register(&codec->ac97->dev); 108db2a4165SFrank Mandarino if (err < 0) { 109db2a4165SFrank Mandarino snd_printk(KERN_ERR "Can't register ac97 bus\n"); 110db2a4165SFrank Mandarino codec->ac97->dev.bus = NULL; 111db2a4165SFrank Mandarino return err; 112db2a4165SFrank Mandarino } 113db2a4165SFrank Mandarino return 0; 114db2a4165SFrank Mandarino } 115db2a4165SFrank Mandarino #endif 116db2a4165SFrank Mandarino 11706f409d7SMark Brown static int soc_pcm_apply_symmetry(struct snd_pcm_substream *substream) 11806f409d7SMark Brown { 11906f409d7SMark Brown struct snd_soc_pcm_runtime *rtd = substream->private_data; 12006f409d7SMark Brown struct snd_soc_device *socdev = rtd->socdev; 12106f409d7SMark Brown struct snd_soc_card *card = socdev->card; 12206f409d7SMark Brown struct snd_soc_dai_link *machine = rtd->dai; 12306f409d7SMark Brown struct snd_soc_dai *cpu_dai = machine->cpu_dai; 12406f409d7SMark Brown struct snd_soc_dai *codec_dai = machine->codec_dai; 12506f409d7SMark Brown int ret; 12606f409d7SMark Brown 12706f409d7SMark Brown if (codec_dai->symmetric_rates || cpu_dai->symmetric_rates || 12806f409d7SMark Brown machine->symmetric_rates) { 12906f409d7SMark Brown dev_dbg(card->dev, "Symmetry forces %dHz rate\n", 13006f409d7SMark Brown machine->rate); 13106f409d7SMark Brown 13206f409d7SMark Brown ret = snd_pcm_hw_constraint_minmax(substream->runtime, 13306f409d7SMark Brown SNDRV_PCM_HW_PARAM_RATE, 13406f409d7SMark Brown machine->rate, 13506f409d7SMark Brown machine->rate); 13606f409d7SMark Brown if (ret < 0) { 13706f409d7SMark Brown dev_err(card->dev, 13806f409d7SMark Brown "Unable to apply rate symmetry constraint: %d\n", ret); 13906f409d7SMark Brown return ret; 14006f409d7SMark Brown } 14106f409d7SMark Brown } 14206f409d7SMark Brown 14306f409d7SMark Brown return 0; 14406f409d7SMark Brown } 14506f409d7SMark Brown 146db2a4165SFrank Mandarino /* 147db2a4165SFrank Mandarino * Called by ALSA when a PCM substream is opened, the runtime->hw record is 148db2a4165SFrank Mandarino * then initialized and any private data can be allocated. This also calls 149db2a4165SFrank Mandarino * startup for the cpu DAI, platform, machine and codec DAI. 150db2a4165SFrank Mandarino */ 151db2a4165SFrank Mandarino static int soc_pcm_open(struct snd_pcm_substream *substream) 152db2a4165SFrank Mandarino { 153db2a4165SFrank Mandarino struct snd_soc_pcm_runtime *rtd = substream->private_data; 154db2a4165SFrank Mandarino struct snd_soc_device *socdev = rtd->socdev; 15587689d56SMark Brown struct snd_soc_card *card = socdev->card; 156db2a4165SFrank Mandarino struct snd_pcm_runtime *runtime = substream->runtime; 157cb666e5bSLiam Girdwood struct snd_soc_dai_link *machine = rtd->dai; 15887689d56SMark Brown struct snd_soc_platform *platform = card->platform; 1593c4b266fSLiam Girdwood struct snd_soc_dai *cpu_dai = machine->cpu_dai; 1603c4b266fSLiam Girdwood struct snd_soc_dai *codec_dai = machine->codec_dai; 161db2a4165SFrank Mandarino int ret = 0; 162db2a4165SFrank Mandarino 163db2a4165SFrank Mandarino mutex_lock(&pcm_mutex); 164db2a4165SFrank Mandarino 165db2a4165SFrank Mandarino /* startup the audio subsystem */ 1666335d055SEric Miao if (cpu_dai->ops->startup) { 1676335d055SEric Miao ret = cpu_dai->ops->startup(substream, cpu_dai); 168db2a4165SFrank Mandarino if (ret < 0) { 169db2a4165SFrank Mandarino printk(KERN_ERR "asoc: can't open interface %s\n", 170cb666e5bSLiam Girdwood cpu_dai->name); 171db2a4165SFrank Mandarino goto out; 172db2a4165SFrank Mandarino } 173db2a4165SFrank Mandarino } 174db2a4165SFrank Mandarino 175db2a4165SFrank Mandarino if (platform->pcm_ops->open) { 176db2a4165SFrank Mandarino ret = platform->pcm_ops->open(substream); 177db2a4165SFrank Mandarino if (ret < 0) { 178db2a4165SFrank Mandarino printk(KERN_ERR "asoc: can't open platform %s\n", platform->name); 179db2a4165SFrank Mandarino goto platform_err; 180db2a4165SFrank Mandarino } 181db2a4165SFrank Mandarino } 182db2a4165SFrank Mandarino 1836335d055SEric Miao if (codec_dai->ops->startup) { 1846335d055SEric Miao ret = codec_dai->ops->startup(substream, codec_dai); 185cb666e5bSLiam Girdwood if (ret < 0) { 186cb666e5bSLiam Girdwood printk(KERN_ERR "asoc: can't open codec %s\n", 187cb666e5bSLiam Girdwood codec_dai->name); 188cb666e5bSLiam Girdwood goto codec_dai_err; 189cb666e5bSLiam Girdwood } 190cb666e5bSLiam Girdwood } 191cb666e5bSLiam Girdwood 192db2a4165SFrank Mandarino if (machine->ops && machine->ops->startup) { 193db2a4165SFrank Mandarino ret = machine->ops->startup(substream); 194db2a4165SFrank Mandarino if (ret < 0) { 195db2a4165SFrank Mandarino printk(KERN_ERR "asoc: %s startup failed\n", machine->name); 196db2a4165SFrank Mandarino goto machine_err; 197db2a4165SFrank Mandarino } 198db2a4165SFrank Mandarino } 199db2a4165SFrank Mandarino 200db2a4165SFrank Mandarino /* Check that the codec and cpu DAI's are compatible */ 201db2a4165SFrank Mandarino if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) { 202db2a4165SFrank Mandarino runtime->hw.rate_min = 2033ff3f64bSMark Brown max(codec_dai->playback.rate_min, 2043ff3f64bSMark Brown cpu_dai->playback.rate_min); 205db2a4165SFrank Mandarino runtime->hw.rate_max = 2063ff3f64bSMark Brown min(codec_dai->playback.rate_max, 2073ff3f64bSMark Brown cpu_dai->playback.rate_max); 208db2a4165SFrank Mandarino runtime->hw.channels_min = 209cb666e5bSLiam Girdwood max(codec_dai->playback.channels_min, 210cb666e5bSLiam Girdwood cpu_dai->playback.channels_min); 211db2a4165SFrank Mandarino runtime->hw.channels_max = 212cb666e5bSLiam Girdwood min(codec_dai->playback.channels_max, 213cb666e5bSLiam Girdwood cpu_dai->playback.channels_max); 214cb666e5bSLiam Girdwood runtime->hw.formats = 215cb666e5bSLiam Girdwood codec_dai->playback.formats & cpu_dai->playback.formats; 216cb666e5bSLiam Girdwood runtime->hw.rates = 217cb666e5bSLiam Girdwood codec_dai->playback.rates & cpu_dai->playback.rates; 218db2a4165SFrank Mandarino } else { 219db2a4165SFrank Mandarino runtime->hw.rate_min = 2203ff3f64bSMark Brown max(codec_dai->capture.rate_min, 2213ff3f64bSMark Brown cpu_dai->capture.rate_min); 222db2a4165SFrank Mandarino runtime->hw.rate_max = 2233ff3f64bSMark Brown min(codec_dai->capture.rate_max, 2243ff3f64bSMark Brown cpu_dai->capture.rate_max); 225db2a4165SFrank Mandarino runtime->hw.channels_min = 226cb666e5bSLiam Girdwood max(codec_dai->capture.channels_min, 227cb666e5bSLiam Girdwood cpu_dai->capture.channels_min); 228db2a4165SFrank Mandarino runtime->hw.channels_max = 229cb666e5bSLiam Girdwood min(codec_dai->capture.channels_max, 230cb666e5bSLiam Girdwood cpu_dai->capture.channels_max); 231cb666e5bSLiam Girdwood runtime->hw.formats = 232cb666e5bSLiam Girdwood codec_dai->capture.formats & cpu_dai->capture.formats; 233cb666e5bSLiam Girdwood runtime->hw.rates = 234cb666e5bSLiam Girdwood codec_dai->capture.rates & cpu_dai->capture.rates; 235db2a4165SFrank Mandarino } 236db2a4165SFrank Mandarino 237db2a4165SFrank Mandarino snd_pcm_limit_hw_rates(runtime); 238db2a4165SFrank Mandarino if (!runtime->hw.rates) { 239db2a4165SFrank Mandarino printk(KERN_ERR "asoc: %s <-> %s No matching rates\n", 240cb666e5bSLiam Girdwood codec_dai->name, cpu_dai->name); 241cb666e5bSLiam Girdwood goto machine_err; 242db2a4165SFrank Mandarino } 243db2a4165SFrank Mandarino if (!runtime->hw.formats) { 244db2a4165SFrank Mandarino printk(KERN_ERR "asoc: %s <-> %s No matching formats\n", 245cb666e5bSLiam Girdwood codec_dai->name, cpu_dai->name); 246cb666e5bSLiam Girdwood goto machine_err; 247db2a4165SFrank Mandarino } 248db2a4165SFrank Mandarino if (!runtime->hw.channels_min || !runtime->hw.channels_max) { 249db2a4165SFrank Mandarino printk(KERN_ERR "asoc: %s <-> %s No matching channels\n", 250cb666e5bSLiam Girdwood codec_dai->name, cpu_dai->name); 251cb666e5bSLiam Girdwood goto machine_err; 252db2a4165SFrank Mandarino } 253db2a4165SFrank Mandarino 25406f409d7SMark Brown /* Symmetry only applies if we've already got an active stream. */ 25506f409d7SMark Brown if (cpu_dai->active || codec_dai->active) { 25606f409d7SMark Brown ret = soc_pcm_apply_symmetry(substream); 25706f409d7SMark Brown if (ret != 0) 25806f409d7SMark Brown goto machine_err; 25906f409d7SMark Brown } 26006f409d7SMark Brown 261f24368c2SMark Brown pr_debug("asoc: %s <-> %s info:\n", codec_dai->name, cpu_dai->name); 262f24368c2SMark Brown pr_debug("asoc: rate mask 0x%x\n", runtime->hw.rates); 263f24368c2SMark Brown pr_debug("asoc: min ch %d max ch %d\n", runtime->hw.channels_min, 264b5c5fd24SLiam Girdwood runtime->hw.channels_max); 265f24368c2SMark Brown pr_debug("asoc: min rate %d max rate %d\n", runtime->hw.rate_min, 266b5c5fd24SLiam Girdwood runtime->hw.rate_max); 267db2a4165SFrank Mandarino 268db2a4165SFrank Mandarino if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) 269cb666e5bSLiam Girdwood cpu_dai->playback.active = codec_dai->playback.active = 1; 270db2a4165SFrank Mandarino else 271cb666e5bSLiam Girdwood cpu_dai->capture.active = codec_dai->capture.active = 1; 272cb666e5bSLiam Girdwood cpu_dai->active = codec_dai->active = 1; 273cb666e5bSLiam Girdwood cpu_dai->runtime = runtime; 2746627a653SMark Brown card->codec->active++; 275db2a4165SFrank Mandarino mutex_unlock(&pcm_mutex); 276db2a4165SFrank Mandarino return 0; 277db2a4165SFrank Mandarino 278cb666e5bSLiam Girdwood machine_err: 279db2a4165SFrank Mandarino if (machine->ops && machine->ops->shutdown) 280db2a4165SFrank Mandarino machine->ops->shutdown(substream); 281db2a4165SFrank Mandarino 282cb666e5bSLiam Girdwood codec_dai_err: 283db2a4165SFrank Mandarino if (platform->pcm_ops->close) 284db2a4165SFrank Mandarino platform->pcm_ops->close(substream); 285db2a4165SFrank Mandarino 286db2a4165SFrank Mandarino platform_err: 2876335d055SEric Miao if (cpu_dai->ops->shutdown) 2886335d055SEric Miao cpu_dai->ops->shutdown(substream, cpu_dai); 289db2a4165SFrank Mandarino out: 290db2a4165SFrank Mandarino mutex_unlock(&pcm_mutex); 291db2a4165SFrank Mandarino return ret; 292db2a4165SFrank Mandarino } 293db2a4165SFrank Mandarino 294db2a4165SFrank Mandarino /* 2953a4fa0a2SRobert P. J. Day * Power down the audio subsystem pmdown_time msecs after close is called. 296db2a4165SFrank Mandarino * This is to ensure there are no pops or clicks in between any music tracks 297db2a4165SFrank Mandarino * due to DAPM power cycling. 298db2a4165SFrank Mandarino */ 2994484bb2eSAndrew Morton static void close_delayed_work(struct work_struct *work) 300db2a4165SFrank Mandarino { 3016308419aSMark Brown struct snd_soc_card *card = container_of(work, struct snd_soc_card, 3026308419aSMark Brown delayed_work.work); 3036627a653SMark Brown struct snd_soc_codec *codec = card->codec; 3043c4b266fSLiam Girdwood struct snd_soc_dai *codec_dai; 305db2a4165SFrank Mandarino int i; 306db2a4165SFrank Mandarino 307db2a4165SFrank Mandarino mutex_lock(&pcm_mutex); 308db2a4165SFrank Mandarino for (i = 0; i < codec->num_dai; i++) { 309db2a4165SFrank Mandarino codec_dai = &codec->dai[i]; 310db2a4165SFrank Mandarino 311f24368c2SMark Brown pr_debug("pop wq checking: %s status: %s waiting: %s\n", 312db2a4165SFrank Mandarino codec_dai->playback.stream_name, 313db2a4165SFrank Mandarino codec_dai->playback.active ? "active" : "inactive", 314db2a4165SFrank Mandarino codec_dai->pop_wait ? "yes" : "no"); 315db2a4165SFrank Mandarino 316db2a4165SFrank Mandarino /* are we waiting on this codec DAI stream */ 317db2a4165SFrank Mandarino if (codec_dai->pop_wait == 1) { 318db2a4165SFrank Mandarino codec_dai->pop_wait = 0; 3190b4d221bSLiam Girdwood snd_soc_dapm_stream_event(codec, 3200b4d221bSLiam Girdwood codec_dai->playback.stream_name, 321db2a4165SFrank Mandarino SND_SOC_DAPM_STREAM_STOP); 322db2a4165SFrank Mandarino } 323db2a4165SFrank Mandarino } 324db2a4165SFrank Mandarino mutex_unlock(&pcm_mutex); 325db2a4165SFrank Mandarino } 326db2a4165SFrank Mandarino 327db2a4165SFrank Mandarino /* 328db2a4165SFrank Mandarino * Called by ALSA when a PCM substream is closed. Private data can be 329db2a4165SFrank Mandarino * freed here. The cpu DAI, codec DAI, machine and platform are also 330db2a4165SFrank Mandarino * shutdown. 331db2a4165SFrank Mandarino */ 332db2a4165SFrank Mandarino static int soc_codec_close(struct snd_pcm_substream *substream) 333db2a4165SFrank Mandarino { 334db2a4165SFrank Mandarino struct snd_soc_pcm_runtime *rtd = substream->private_data; 335db2a4165SFrank Mandarino struct snd_soc_device *socdev = rtd->socdev; 3366308419aSMark Brown struct snd_soc_card *card = socdev->card; 337cb666e5bSLiam Girdwood struct snd_soc_dai_link *machine = rtd->dai; 33887689d56SMark Brown struct snd_soc_platform *platform = card->platform; 3393c4b266fSLiam Girdwood struct snd_soc_dai *cpu_dai = machine->cpu_dai; 3403c4b266fSLiam Girdwood struct snd_soc_dai *codec_dai = machine->codec_dai; 3416627a653SMark Brown struct snd_soc_codec *codec = card->codec; 342db2a4165SFrank Mandarino 343db2a4165SFrank Mandarino mutex_lock(&pcm_mutex); 344db2a4165SFrank Mandarino 345db2a4165SFrank Mandarino if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) 346cb666e5bSLiam Girdwood cpu_dai->playback.active = codec_dai->playback.active = 0; 347db2a4165SFrank Mandarino else 348cb666e5bSLiam Girdwood cpu_dai->capture.active = codec_dai->capture.active = 0; 349db2a4165SFrank Mandarino 350cb666e5bSLiam Girdwood if (codec_dai->playback.active == 0 && 351cb666e5bSLiam Girdwood codec_dai->capture.active == 0) { 352cb666e5bSLiam Girdwood cpu_dai->active = codec_dai->active = 0; 353db2a4165SFrank Mandarino } 354db2a4165SFrank Mandarino codec->active--; 355db2a4165SFrank Mandarino 3566010b2daSMark Brown /* Muting the DAC suppresses artifacts caused during digital 3576010b2daSMark Brown * shutdown, for example from stopping clocks. 3586010b2daSMark Brown */ 3596010b2daSMark Brown if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) 3606010b2daSMark Brown snd_soc_dai_digital_mute(codec_dai, 1); 3616010b2daSMark Brown 3626335d055SEric Miao if (cpu_dai->ops->shutdown) 3636335d055SEric Miao cpu_dai->ops->shutdown(substream, cpu_dai); 364db2a4165SFrank Mandarino 3656335d055SEric Miao if (codec_dai->ops->shutdown) 3666335d055SEric Miao codec_dai->ops->shutdown(substream, codec_dai); 367db2a4165SFrank Mandarino 368db2a4165SFrank Mandarino if (machine->ops && machine->ops->shutdown) 369db2a4165SFrank Mandarino machine->ops->shutdown(substream); 370db2a4165SFrank Mandarino 371db2a4165SFrank Mandarino if (platform->pcm_ops->close) 372db2a4165SFrank Mandarino platform->pcm_ops->close(substream); 373cb666e5bSLiam Girdwood cpu_dai->runtime = NULL; 374db2a4165SFrank Mandarino 375db2a4165SFrank Mandarino if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) { 376db2a4165SFrank Mandarino /* start delayed pop wq here for playback streams */ 377cb666e5bSLiam Girdwood codec_dai->pop_wait = 1; 3786308419aSMark Brown schedule_delayed_work(&card->delayed_work, 379db2a4165SFrank Mandarino msecs_to_jiffies(pmdown_time)); 380db2a4165SFrank Mandarino } else { 381db2a4165SFrank Mandarino /* capture streams can be powered down now */ 382cb666e5bSLiam Girdwood snd_soc_dapm_stream_event(codec, 3830b4d221bSLiam Girdwood codec_dai->capture.stream_name, 3840b4d221bSLiam Girdwood SND_SOC_DAPM_STREAM_STOP); 385db2a4165SFrank Mandarino } 386db2a4165SFrank Mandarino 387db2a4165SFrank Mandarino mutex_unlock(&pcm_mutex); 388db2a4165SFrank Mandarino return 0; 389db2a4165SFrank Mandarino } 390db2a4165SFrank Mandarino 391db2a4165SFrank Mandarino /* 392db2a4165SFrank Mandarino * Called by ALSA when the PCM substream is prepared, can set format, sample 393db2a4165SFrank Mandarino * rate, etc. This function is non atomic and can be called multiple times, 394db2a4165SFrank Mandarino * it can refer to the runtime info. 395db2a4165SFrank Mandarino */ 396db2a4165SFrank Mandarino static int soc_pcm_prepare(struct snd_pcm_substream *substream) 397db2a4165SFrank Mandarino { 398db2a4165SFrank Mandarino struct snd_soc_pcm_runtime *rtd = substream->private_data; 399db2a4165SFrank Mandarino struct snd_soc_device *socdev = rtd->socdev; 4006308419aSMark Brown struct snd_soc_card *card = socdev->card; 401cb666e5bSLiam Girdwood struct snd_soc_dai_link *machine = rtd->dai; 40287689d56SMark Brown struct snd_soc_platform *platform = card->platform; 4033c4b266fSLiam Girdwood struct snd_soc_dai *cpu_dai = machine->cpu_dai; 4043c4b266fSLiam Girdwood struct snd_soc_dai *codec_dai = machine->codec_dai; 4056627a653SMark Brown struct snd_soc_codec *codec = card->codec; 406db2a4165SFrank Mandarino int ret = 0; 407db2a4165SFrank Mandarino 408db2a4165SFrank Mandarino mutex_lock(&pcm_mutex); 409cb666e5bSLiam Girdwood 410cb666e5bSLiam Girdwood if (machine->ops && machine->ops->prepare) { 411cb666e5bSLiam Girdwood ret = machine->ops->prepare(substream); 412cb666e5bSLiam Girdwood if (ret < 0) { 413cb666e5bSLiam Girdwood printk(KERN_ERR "asoc: machine prepare error\n"); 414cb666e5bSLiam Girdwood goto out; 415cb666e5bSLiam Girdwood } 416cb666e5bSLiam Girdwood } 417cb666e5bSLiam Girdwood 418db2a4165SFrank Mandarino if (platform->pcm_ops->prepare) { 419db2a4165SFrank Mandarino ret = platform->pcm_ops->prepare(substream); 420a71a468aSLiam Girdwood if (ret < 0) { 421a71a468aSLiam Girdwood printk(KERN_ERR "asoc: platform prepare error\n"); 422db2a4165SFrank Mandarino goto out; 423db2a4165SFrank Mandarino } 424a71a468aSLiam Girdwood } 425db2a4165SFrank Mandarino 4266335d055SEric Miao if (codec_dai->ops->prepare) { 4276335d055SEric Miao ret = codec_dai->ops->prepare(substream, codec_dai); 428a71a468aSLiam Girdwood if (ret < 0) { 429a71a468aSLiam Girdwood printk(KERN_ERR "asoc: codec DAI prepare error\n"); 430db2a4165SFrank Mandarino goto out; 431db2a4165SFrank Mandarino } 432a71a468aSLiam Girdwood } 433db2a4165SFrank Mandarino 4346335d055SEric Miao if (cpu_dai->ops->prepare) { 4356335d055SEric Miao ret = cpu_dai->ops->prepare(substream, cpu_dai); 436cb666e5bSLiam Girdwood if (ret < 0) { 437cb666e5bSLiam Girdwood printk(KERN_ERR "asoc: cpu DAI prepare error\n"); 438cb666e5bSLiam Girdwood goto out; 439cb666e5bSLiam Girdwood } 440cb666e5bSLiam Girdwood } 441db2a4165SFrank Mandarino 442d45f6219SMark Brown /* cancel any delayed stream shutdown that is pending */ 443d45f6219SMark Brown if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK && 444d45f6219SMark Brown codec_dai->pop_wait) { 445cb666e5bSLiam Girdwood codec_dai->pop_wait = 0; 4466308419aSMark Brown cancel_delayed_work(&card->delayed_work); 447db2a4165SFrank Mandarino } 448db2a4165SFrank Mandarino 449db2a4165SFrank Mandarino if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) 450db2a4165SFrank Mandarino snd_soc_dapm_stream_event(codec, 451cb666e5bSLiam Girdwood codec_dai->playback.stream_name, 452db2a4165SFrank Mandarino SND_SOC_DAPM_STREAM_START); 453db2a4165SFrank Mandarino else 454db2a4165SFrank Mandarino snd_soc_dapm_stream_event(codec, 455cb666e5bSLiam Girdwood codec_dai->capture.stream_name, 456db2a4165SFrank Mandarino SND_SOC_DAPM_STREAM_START); 4578c6529dbSLiam Girdwood 4588c6529dbSLiam Girdwood snd_soc_dai_digital_mute(codec_dai, 0); 459db2a4165SFrank Mandarino 460db2a4165SFrank Mandarino out: 461db2a4165SFrank Mandarino mutex_unlock(&pcm_mutex); 462db2a4165SFrank Mandarino return ret; 463db2a4165SFrank Mandarino } 464db2a4165SFrank Mandarino 465db2a4165SFrank Mandarino /* 466db2a4165SFrank Mandarino * Called by ALSA when the hardware params are set by application. This 467db2a4165SFrank Mandarino * function can also be called multiple times and can allocate buffers 468db2a4165SFrank Mandarino * (using snd_pcm_lib_* ). It's non-atomic. 469db2a4165SFrank Mandarino */ 470db2a4165SFrank Mandarino static int soc_pcm_hw_params(struct snd_pcm_substream *substream, 471db2a4165SFrank Mandarino struct snd_pcm_hw_params *params) 472db2a4165SFrank Mandarino { 473db2a4165SFrank Mandarino struct snd_soc_pcm_runtime *rtd = substream->private_data; 474db2a4165SFrank Mandarino struct snd_soc_device *socdev = rtd->socdev; 475cb666e5bSLiam Girdwood struct snd_soc_dai_link *machine = rtd->dai; 47687689d56SMark Brown struct snd_soc_card *card = socdev->card; 47787689d56SMark Brown struct snd_soc_platform *platform = card->platform; 4783c4b266fSLiam Girdwood struct snd_soc_dai *cpu_dai = machine->cpu_dai; 4793c4b266fSLiam Girdwood struct snd_soc_dai *codec_dai = machine->codec_dai; 480db2a4165SFrank Mandarino int ret = 0; 481db2a4165SFrank Mandarino 482db2a4165SFrank Mandarino mutex_lock(&pcm_mutex); 483db2a4165SFrank Mandarino 484cb666e5bSLiam Girdwood if (machine->ops && machine->ops->hw_params) { 485cb666e5bSLiam Girdwood ret = machine->ops->hw_params(substream, params); 486cb666e5bSLiam Girdwood if (ret < 0) { 487cb666e5bSLiam Girdwood printk(KERN_ERR "asoc: machine hw_params failed\n"); 488db2a4165SFrank Mandarino goto out; 489db2a4165SFrank Mandarino } 490cb666e5bSLiam Girdwood } 491db2a4165SFrank Mandarino 4926335d055SEric Miao if (codec_dai->ops->hw_params) { 4936335d055SEric Miao ret = codec_dai->ops->hw_params(substream, params, codec_dai); 494db2a4165SFrank Mandarino if (ret < 0) { 495db2a4165SFrank Mandarino printk(KERN_ERR "asoc: can't set codec %s hw params\n", 496cb666e5bSLiam Girdwood codec_dai->name); 497cb666e5bSLiam Girdwood goto codec_err; 498db2a4165SFrank Mandarino } 499db2a4165SFrank Mandarino } 500db2a4165SFrank Mandarino 5016335d055SEric Miao if (cpu_dai->ops->hw_params) { 5026335d055SEric Miao ret = cpu_dai->ops->hw_params(substream, params, cpu_dai); 503db2a4165SFrank Mandarino if (ret < 0) { 5043ff3f64bSMark Brown printk(KERN_ERR "asoc: interface %s hw params failed\n", 505cb666e5bSLiam Girdwood cpu_dai->name); 506db2a4165SFrank Mandarino goto interface_err; 507db2a4165SFrank Mandarino } 508db2a4165SFrank Mandarino } 509db2a4165SFrank Mandarino 510db2a4165SFrank Mandarino if (platform->pcm_ops->hw_params) { 511db2a4165SFrank Mandarino ret = platform->pcm_ops->hw_params(substream, params); 512db2a4165SFrank Mandarino if (ret < 0) { 5133ff3f64bSMark Brown printk(KERN_ERR "asoc: platform %s hw params failed\n", 514db2a4165SFrank Mandarino platform->name); 515db2a4165SFrank Mandarino goto platform_err; 516db2a4165SFrank Mandarino } 517db2a4165SFrank Mandarino } 518db2a4165SFrank Mandarino 51906f409d7SMark Brown machine->rate = params_rate(params); 52006f409d7SMark Brown 521db2a4165SFrank Mandarino out: 522db2a4165SFrank Mandarino mutex_unlock(&pcm_mutex); 523db2a4165SFrank Mandarino return ret; 524db2a4165SFrank Mandarino 525db2a4165SFrank Mandarino platform_err: 5266335d055SEric Miao if (cpu_dai->ops->hw_free) 5276335d055SEric Miao cpu_dai->ops->hw_free(substream, cpu_dai); 528db2a4165SFrank Mandarino 529db2a4165SFrank Mandarino interface_err: 5306335d055SEric Miao if (codec_dai->ops->hw_free) 5316335d055SEric Miao codec_dai->ops->hw_free(substream, codec_dai); 532cb666e5bSLiam Girdwood 533cb666e5bSLiam Girdwood codec_err: 534cb666e5bSLiam Girdwood if (machine->ops && machine->ops->hw_free) 535cb666e5bSLiam Girdwood machine->ops->hw_free(substream); 536db2a4165SFrank Mandarino 537db2a4165SFrank Mandarino mutex_unlock(&pcm_mutex); 538db2a4165SFrank Mandarino return ret; 539db2a4165SFrank Mandarino } 540db2a4165SFrank Mandarino 541db2a4165SFrank Mandarino /* 542db2a4165SFrank Mandarino * Free's resources allocated by hw_params, can be called multiple times 543db2a4165SFrank Mandarino */ 544db2a4165SFrank Mandarino static int soc_pcm_hw_free(struct snd_pcm_substream *substream) 545db2a4165SFrank Mandarino { 546db2a4165SFrank Mandarino struct snd_soc_pcm_runtime *rtd = substream->private_data; 547db2a4165SFrank Mandarino struct snd_soc_device *socdev = rtd->socdev; 548cb666e5bSLiam Girdwood struct snd_soc_dai_link *machine = rtd->dai; 54987689d56SMark Brown struct snd_soc_card *card = socdev->card; 55087689d56SMark Brown struct snd_soc_platform *platform = card->platform; 5513c4b266fSLiam Girdwood struct snd_soc_dai *cpu_dai = machine->cpu_dai; 5523c4b266fSLiam Girdwood struct snd_soc_dai *codec_dai = machine->codec_dai; 5536627a653SMark Brown struct snd_soc_codec *codec = card->codec; 554db2a4165SFrank Mandarino 555db2a4165SFrank Mandarino mutex_lock(&pcm_mutex); 556db2a4165SFrank Mandarino 557db2a4165SFrank Mandarino /* apply codec digital mute */ 5588c6529dbSLiam Girdwood if (!codec->active) 5598c6529dbSLiam Girdwood snd_soc_dai_digital_mute(codec_dai, 1); 560db2a4165SFrank Mandarino 561db2a4165SFrank Mandarino /* free any machine hw params */ 562db2a4165SFrank Mandarino if (machine->ops && machine->ops->hw_free) 563db2a4165SFrank Mandarino machine->ops->hw_free(substream); 564db2a4165SFrank Mandarino 565db2a4165SFrank Mandarino /* free any DMA resources */ 566db2a4165SFrank Mandarino if (platform->pcm_ops->hw_free) 567db2a4165SFrank Mandarino platform->pcm_ops->hw_free(substream); 568db2a4165SFrank Mandarino 569db2a4165SFrank Mandarino /* now free hw params for the DAI's */ 5706335d055SEric Miao if (codec_dai->ops->hw_free) 5716335d055SEric Miao codec_dai->ops->hw_free(substream, codec_dai); 572db2a4165SFrank Mandarino 5736335d055SEric Miao if (cpu_dai->ops->hw_free) 5746335d055SEric Miao cpu_dai->ops->hw_free(substream, cpu_dai); 575db2a4165SFrank Mandarino 576db2a4165SFrank Mandarino mutex_unlock(&pcm_mutex); 577db2a4165SFrank Mandarino return 0; 578db2a4165SFrank Mandarino } 579db2a4165SFrank Mandarino 580db2a4165SFrank Mandarino static int soc_pcm_trigger(struct snd_pcm_substream *substream, int cmd) 581db2a4165SFrank Mandarino { 582db2a4165SFrank Mandarino struct snd_soc_pcm_runtime *rtd = substream->private_data; 583db2a4165SFrank Mandarino struct snd_soc_device *socdev = rtd->socdev; 58487689d56SMark Brown struct snd_soc_card *card= socdev->card; 585cb666e5bSLiam Girdwood struct snd_soc_dai_link *machine = rtd->dai; 58687689d56SMark Brown struct snd_soc_platform *platform = card->platform; 5873c4b266fSLiam Girdwood struct snd_soc_dai *cpu_dai = machine->cpu_dai; 5883c4b266fSLiam Girdwood struct snd_soc_dai *codec_dai = machine->codec_dai; 589db2a4165SFrank Mandarino int ret; 590db2a4165SFrank Mandarino 5916335d055SEric Miao if (codec_dai->ops->trigger) { 5926335d055SEric Miao ret = codec_dai->ops->trigger(substream, cmd, codec_dai); 593db2a4165SFrank Mandarino if (ret < 0) 594db2a4165SFrank Mandarino return ret; 595db2a4165SFrank Mandarino } 596db2a4165SFrank Mandarino 597db2a4165SFrank Mandarino if (platform->pcm_ops->trigger) { 598db2a4165SFrank Mandarino ret = platform->pcm_ops->trigger(substream, cmd); 599db2a4165SFrank Mandarino if (ret < 0) 600db2a4165SFrank Mandarino return ret; 601db2a4165SFrank Mandarino } 602db2a4165SFrank Mandarino 6036335d055SEric Miao if (cpu_dai->ops->trigger) { 6046335d055SEric Miao ret = cpu_dai->ops->trigger(substream, cmd, cpu_dai); 605db2a4165SFrank Mandarino if (ret < 0) 606db2a4165SFrank Mandarino return ret; 607db2a4165SFrank Mandarino } 608db2a4165SFrank Mandarino return 0; 609db2a4165SFrank Mandarino } 610db2a4165SFrank Mandarino 611db2a4165SFrank Mandarino /* ASoC PCM operations */ 612db2a4165SFrank Mandarino static struct snd_pcm_ops soc_pcm_ops = { 613db2a4165SFrank Mandarino .open = soc_pcm_open, 614db2a4165SFrank Mandarino .close = soc_codec_close, 615db2a4165SFrank Mandarino .hw_params = soc_pcm_hw_params, 616db2a4165SFrank Mandarino .hw_free = soc_pcm_hw_free, 617db2a4165SFrank Mandarino .prepare = soc_pcm_prepare, 618db2a4165SFrank Mandarino .trigger = soc_pcm_trigger, 619db2a4165SFrank Mandarino }; 620db2a4165SFrank Mandarino 621db2a4165SFrank Mandarino #ifdef CONFIG_PM 622db2a4165SFrank Mandarino /* powers down audio subsystem for suspend */ 623416356fcSMark Brown static int soc_suspend(struct device *dev) 624db2a4165SFrank Mandarino { 625416356fcSMark Brown struct platform_device *pdev = to_platform_device(dev); 626db2a4165SFrank Mandarino struct snd_soc_device *socdev = platform_get_drvdata(pdev); 62787506549SMark Brown struct snd_soc_card *card = socdev->card; 62887689d56SMark Brown struct snd_soc_platform *platform = card->platform; 629db2a4165SFrank Mandarino struct snd_soc_codec_device *codec_dev = socdev->codec_dev; 6306627a653SMark Brown struct snd_soc_codec *codec = card->codec; 631db2a4165SFrank Mandarino int i; 632db2a4165SFrank Mandarino 633e3509ff0SDaniel Mack /* If the initialization of this soc device failed, there is no codec 634e3509ff0SDaniel Mack * associated with it. Just bail out in this case. 635e3509ff0SDaniel Mack */ 636e3509ff0SDaniel Mack if (!codec) 637e3509ff0SDaniel Mack return 0; 638e3509ff0SDaniel Mack 6396ed25978SAndy Green /* Due to the resume being scheduled into a workqueue we could 6406ed25978SAndy Green * suspend before that's finished - wait for it to complete. 6416ed25978SAndy Green */ 6426ed25978SAndy Green snd_power_lock(codec->card); 6436ed25978SAndy Green snd_power_wait(codec->card, SNDRV_CTL_POWER_D0); 6446ed25978SAndy Green snd_power_unlock(codec->card); 6456ed25978SAndy Green 6466ed25978SAndy Green /* we're going to block userspace touching us until resume completes */ 6476ed25978SAndy Green snd_power_change_state(codec->card, SNDRV_CTL_POWER_D3hot); 6486ed25978SAndy Green 649db2a4165SFrank Mandarino /* mute any active DAC's */ 650dee89c4dSMark Brown for (i = 0; i < card->num_links; i++) { 651dee89c4dSMark Brown struct snd_soc_dai *dai = card->dai_link[i].codec_dai; 6526335d055SEric Miao if (dai->ops->digital_mute && dai->playback.active) 6536335d055SEric Miao dai->ops->digital_mute(dai, 1); 654db2a4165SFrank Mandarino } 655db2a4165SFrank Mandarino 6564ccab3e7SLiam Girdwood /* suspend all pcms */ 65787506549SMark Brown for (i = 0; i < card->num_links; i++) 65887506549SMark Brown snd_pcm_suspend_all(card->dai_link[i].pcm); 6594ccab3e7SLiam Girdwood 66087506549SMark Brown if (card->suspend_pre) 661416356fcSMark Brown card->suspend_pre(pdev, PMSG_SUSPEND); 662db2a4165SFrank Mandarino 66387506549SMark Brown for (i = 0; i < card->num_links; i++) { 66487506549SMark Brown struct snd_soc_dai *cpu_dai = card->dai_link[i].cpu_dai; 6653ba9e10aSMark Brown if (cpu_dai->suspend && !cpu_dai->ac97_control) 666dc7d7b83SMark Brown cpu_dai->suspend(cpu_dai); 667db2a4165SFrank Mandarino if (platform->suspend) 66807c84d04SMark Brown platform->suspend(cpu_dai); 669db2a4165SFrank Mandarino } 670db2a4165SFrank Mandarino 671db2a4165SFrank Mandarino /* close any waiting streams and save state */ 6726308419aSMark Brown run_delayed_work(&card->delayed_work); 6730be9898aSMark Brown codec->suspend_bias_level = codec->bias_level; 674db2a4165SFrank Mandarino 675db2a4165SFrank Mandarino for (i = 0; i < codec->num_dai; i++) { 676db2a4165SFrank Mandarino char *stream = codec->dai[i].playback.stream_name; 677db2a4165SFrank Mandarino if (stream != NULL) 678db2a4165SFrank Mandarino snd_soc_dapm_stream_event(codec, stream, 679db2a4165SFrank Mandarino SND_SOC_DAPM_STREAM_SUSPEND); 680db2a4165SFrank Mandarino stream = codec->dai[i].capture.stream_name; 681db2a4165SFrank Mandarino if (stream != NULL) 682db2a4165SFrank Mandarino snd_soc_dapm_stream_event(codec, stream, 683db2a4165SFrank Mandarino SND_SOC_DAPM_STREAM_SUSPEND); 684db2a4165SFrank Mandarino } 685db2a4165SFrank Mandarino 686db2a4165SFrank Mandarino if (codec_dev->suspend) 687416356fcSMark Brown codec_dev->suspend(pdev, PMSG_SUSPEND); 688db2a4165SFrank Mandarino 68987506549SMark Brown for (i = 0; i < card->num_links; i++) { 69087506549SMark Brown struct snd_soc_dai *cpu_dai = card->dai_link[i].cpu_dai; 6913ba9e10aSMark Brown if (cpu_dai->suspend && cpu_dai->ac97_control) 692dc7d7b83SMark Brown cpu_dai->suspend(cpu_dai); 693db2a4165SFrank Mandarino } 694db2a4165SFrank Mandarino 69587506549SMark Brown if (card->suspend_post) 696416356fcSMark Brown card->suspend_post(pdev, PMSG_SUSPEND); 697db2a4165SFrank Mandarino 698db2a4165SFrank Mandarino return 0; 699db2a4165SFrank Mandarino } 700db2a4165SFrank Mandarino 7016ed25978SAndy Green /* deferred resume work, so resume can complete before we finished 7026ed25978SAndy Green * setting our codec back up, which can be very slow on I2C 7036ed25978SAndy Green */ 7046ed25978SAndy Green static void soc_resume_deferred(struct work_struct *work) 705db2a4165SFrank Mandarino { 7066308419aSMark Brown struct snd_soc_card *card = container_of(work, 7076308419aSMark Brown struct snd_soc_card, 7086ed25978SAndy Green deferred_resume_work); 7096308419aSMark Brown struct snd_soc_device *socdev = card->socdev; 71087689d56SMark Brown struct snd_soc_platform *platform = card->platform; 711db2a4165SFrank Mandarino struct snd_soc_codec_device *codec_dev = socdev->codec_dev; 7126627a653SMark Brown struct snd_soc_codec *codec = card->codec; 7136ed25978SAndy Green struct platform_device *pdev = to_platform_device(socdev->dev); 714db2a4165SFrank Mandarino int i; 715db2a4165SFrank Mandarino 7166ed25978SAndy Green /* our power state is still SNDRV_CTL_POWER_D3hot from suspend time, 7176ed25978SAndy Green * so userspace apps are blocked from touching us 7186ed25978SAndy Green */ 7196ed25978SAndy Green 720fde22f27SMark Brown dev_dbg(socdev->dev, "starting resume work\n"); 7216ed25978SAndy Green 72287506549SMark Brown if (card->resume_pre) 72387506549SMark Brown card->resume_pre(pdev); 724db2a4165SFrank Mandarino 72587506549SMark Brown for (i = 0; i < card->num_links; i++) { 72687506549SMark Brown struct snd_soc_dai *cpu_dai = card->dai_link[i].cpu_dai; 7273ba9e10aSMark Brown if (cpu_dai->resume && cpu_dai->ac97_control) 728dc7d7b83SMark Brown cpu_dai->resume(cpu_dai); 729db2a4165SFrank Mandarino } 730db2a4165SFrank Mandarino 731db2a4165SFrank Mandarino if (codec_dev->resume) 732db2a4165SFrank Mandarino codec_dev->resume(pdev); 733db2a4165SFrank Mandarino 734db2a4165SFrank Mandarino for (i = 0; i < codec->num_dai; i++) { 735db2a4165SFrank Mandarino char *stream = codec->dai[i].playback.stream_name; 736db2a4165SFrank Mandarino if (stream != NULL) 737db2a4165SFrank Mandarino snd_soc_dapm_stream_event(codec, stream, 738db2a4165SFrank Mandarino SND_SOC_DAPM_STREAM_RESUME); 739db2a4165SFrank Mandarino stream = codec->dai[i].capture.stream_name; 740db2a4165SFrank Mandarino if (stream != NULL) 741db2a4165SFrank Mandarino snd_soc_dapm_stream_event(codec, stream, 742db2a4165SFrank Mandarino SND_SOC_DAPM_STREAM_RESUME); 743db2a4165SFrank Mandarino } 744db2a4165SFrank Mandarino 7453ff3f64bSMark Brown /* unmute any active DACs */ 746dee89c4dSMark Brown for (i = 0; i < card->num_links; i++) { 747dee89c4dSMark Brown struct snd_soc_dai *dai = card->dai_link[i].codec_dai; 7486335d055SEric Miao if (dai->ops->digital_mute && dai->playback.active) 7496335d055SEric Miao dai->ops->digital_mute(dai, 0); 750db2a4165SFrank Mandarino } 751db2a4165SFrank Mandarino 75287506549SMark Brown for (i = 0; i < card->num_links; i++) { 75387506549SMark Brown struct snd_soc_dai *cpu_dai = card->dai_link[i].cpu_dai; 7543ba9e10aSMark Brown if (cpu_dai->resume && !cpu_dai->ac97_control) 755dc7d7b83SMark Brown cpu_dai->resume(cpu_dai); 756db2a4165SFrank Mandarino if (platform->resume) 75707c84d04SMark Brown platform->resume(cpu_dai); 758db2a4165SFrank Mandarino } 759db2a4165SFrank Mandarino 76087506549SMark Brown if (card->resume_post) 76187506549SMark Brown card->resume_post(pdev); 762db2a4165SFrank Mandarino 763fde22f27SMark Brown dev_dbg(socdev->dev, "resume work completed\n"); 7646ed25978SAndy Green 7656ed25978SAndy Green /* userspace can access us now we are back as we were before */ 7666ed25978SAndy Green snd_power_change_state(codec->card, SNDRV_CTL_POWER_D0); 7676ed25978SAndy Green } 7686ed25978SAndy Green 7696ed25978SAndy Green /* powers up audio subsystem after a suspend */ 770416356fcSMark Brown static int soc_resume(struct device *dev) 7716ed25978SAndy Green { 772416356fcSMark Brown struct platform_device *pdev = to_platform_device(dev); 7736ed25978SAndy Green struct snd_soc_device *socdev = platform_get_drvdata(pdev); 7746308419aSMark Brown struct snd_soc_card *card = socdev->card; 77564ab9baaSMark Brown struct snd_soc_dai *cpu_dai = card->dai_link[0].cpu_dai; 7766ed25978SAndy Green 77764ab9baaSMark Brown /* AC97 devices might have other drivers hanging off them so 77864ab9baaSMark Brown * need to resume immediately. Other drivers don't have that 77964ab9baaSMark Brown * problem and may take a substantial amount of time to resume 78064ab9baaSMark Brown * due to I/O costs and anti-pop so handle them out of line. 78164ab9baaSMark Brown */ 78264ab9baaSMark Brown if (cpu_dai->ac97_control) { 78364ab9baaSMark Brown dev_dbg(socdev->dev, "Resuming AC97 immediately\n"); 78464ab9baaSMark Brown soc_resume_deferred(&card->deferred_resume_work); 78564ab9baaSMark Brown } else { 78664ab9baaSMark Brown dev_dbg(socdev->dev, "Scheduling resume work\n"); 7876308419aSMark Brown if (!schedule_work(&card->deferred_resume_work)) 788fde22f27SMark Brown dev_err(socdev->dev, "resume work item may be lost\n"); 78964ab9baaSMark Brown } 7906ed25978SAndy Green 791db2a4165SFrank Mandarino return 0; 792db2a4165SFrank Mandarino } 793db2a4165SFrank Mandarino 794831dc0f1SMark Brown /** 795831dc0f1SMark Brown * snd_soc_suspend_device: Notify core of device suspend 796831dc0f1SMark Brown * 797831dc0f1SMark Brown * @dev: Device being suspended. 798831dc0f1SMark Brown * 799831dc0f1SMark Brown * In order to ensure that the entire audio subsystem is suspended in a 800831dc0f1SMark Brown * coordinated fashion ASoC devices should suspend themselves when 801831dc0f1SMark Brown * called by ASoC. When the standard kernel suspend process asks the 802831dc0f1SMark Brown * device to suspend it should call this function to initiate a suspend 803831dc0f1SMark Brown * of the entire ASoC card. 804831dc0f1SMark Brown * 805831dc0f1SMark Brown * \note Currently this function is stubbed out. 806831dc0f1SMark Brown */ 807831dc0f1SMark Brown int snd_soc_suspend_device(struct device *dev) 808831dc0f1SMark Brown { 809831dc0f1SMark Brown return 0; 810831dc0f1SMark Brown } 811831dc0f1SMark Brown EXPORT_SYMBOL_GPL(snd_soc_suspend_device); 812831dc0f1SMark Brown 813831dc0f1SMark Brown /** 814831dc0f1SMark Brown * snd_soc_resume_device: Notify core of device resume 815831dc0f1SMark Brown * 816831dc0f1SMark Brown * @dev: Device being resumed. 817831dc0f1SMark Brown * 818831dc0f1SMark Brown * In order to ensure that the entire audio subsystem is resumed in a 819831dc0f1SMark Brown * coordinated fashion ASoC devices should resume themselves when called 820831dc0f1SMark Brown * by ASoC. When the standard kernel resume process asks the device 821831dc0f1SMark Brown * to resume it should call this function. Once all the components of 822831dc0f1SMark Brown * the card have notified that they are ready to be resumed the card 823831dc0f1SMark Brown * will be resumed. 824831dc0f1SMark Brown * 825831dc0f1SMark Brown * \note Currently this function is stubbed out. 826831dc0f1SMark Brown */ 827831dc0f1SMark Brown int snd_soc_resume_device(struct device *dev) 828831dc0f1SMark Brown { 829831dc0f1SMark Brown return 0; 830831dc0f1SMark Brown } 831831dc0f1SMark Brown EXPORT_SYMBOL_GPL(snd_soc_resume_device); 832db2a4165SFrank Mandarino #else 833db2a4165SFrank Mandarino #define soc_suspend NULL 834db2a4165SFrank Mandarino #define soc_resume NULL 835db2a4165SFrank Mandarino #endif 836db2a4165SFrank Mandarino 837435c5e25SMark Brown static void snd_soc_instantiate_card(struct snd_soc_card *card) 838db2a4165SFrank Mandarino { 839435c5e25SMark Brown struct platform_device *pdev = container_of(card->dev, 840435c5e25SMark Brown struct platform_device, 841435c5e25SMark Brown dev); 842435c5e25SMark Brown struct snd_soc_codec_device *codec_dev = card->socdev->codec_dev; 843435c5e25SMark Brown struct snd_soc_platform *platform; 844435c5e25SMark Brown struct snd_soc_dai *dai; 8456b05eda6SMark Brown int i, found, ret, ac97; 846db2a4165SFrank Mandarino 847435c5e25SMark Brown if (card->instantiated) 848435c5e25SMark Brown return; 8496308419aSMark Brown 850435c5e25SMark Brown found = 0; 851435c5e25SMark Brown list_for_each_entry(platform, &platform_list, list) 852435c5e25SMark Brown if (card->platform == platform) { 853435c5e25SMark Brown found = 1; 854435c5e25SMark Brown break; 855435c5e25SMark Brown } 856435c5e25SMark Brown if (!found) { 857435c5e25SMark Brown dev_dbg(card->dev, "Platform %s not registered\n", 858435c5e25SMark Brown card->platform->name); 859435c5e25SMark Brown return; 860c5af3a2eSMark Brown } 861c5af3a2eSMark Brown 8626b05eda6SMark Brown ac97 = 0; 863435c5e25SMark Brown for (i = 0; i < card->num_links; i++) { 864435c5e25SMark Brown found = 0; 865435c5e25SMark Brown list_for_each_entry(dai, &dai_list, list) 866435c5e25SMark Brown if (card->dai_link[i].cpu_dai == dai) { 867435c5e25SMark Brown found = 1; 868435c5e25SMark Brown break; 869435c5e25SMark Brown } 870435c5e25SMark Brown if (!found) { 871435c5e25SMark Brown dev_dbg(card->dev, "DAI %s not registered\n", 872435c5e25SMark Brown card->dai_link[i].cpu_dai->name); 873435c5e25SMark Brown return; 874435c5e25SMark Brown } 8756b05eda6SMark Brown 8766b05eda6SMark Brown if (card->dai_link[i].cpu_dai->ac97_control) 8776b05eda6SMark Brown ac97 = 1; 8786b05eda6SMark Brown } 8796b05eda6SMark Brown 8806b05eda6SMark Brown /* If we have AC97 in the system then don't wait for the 8816b05eda6SMark Brown * codec. This will need revisiting if we have to handle 8826b05eda6SMark Brown * systems with mixed AC97 and non-AC97 parts. Only check for 8836b05eda6SMark Brown * DAIs currently; we can't do this per link since some AC97 8846b05eda6SMark Brown * codecs have non-AC97 DAIs. 8856b05eda6SMark Brown */ 8866b05eda6SMark Brown if (!ac97) 8876b05eda6SMark Brown for (i = 0; i < card->num_links; i++) { 8886b05eda6SMark Brown found = 0; 8896b05eda6SMark Brown list_for_each_entry(dai, &dai_list, list) 8906b05eda6SMark Brown if (card->dai_link[i].codec_dai == dai) { 8916b05eda6SMark Brown found = 1; 8926b05eda6SMark Brown break; 8936b05eda6SMark Brown } 8946b05eda6SMark Brown if (!found) { 8956b05eda6SMark Brown dev_dbg(card->dev, "DAI %s not registered\n", 8966b05eda6SMark Brown card->dai_link[i].codec_dai->name); 8976b05eda6SMark Brown return; 8986b05eda6SMark Brown } 899435c5e25SMark Brown } 900435c5e25SMark Brown 901435c5e25SMark Brown /* Note that we do not current check for codec components */ 902435c5e25SMark Brown 903435c5e25SMark Brown dev_dbg(card->dev, "All components present, instantiating\n"); 904435c5e25SMark Brown 905435c5e25SMark Brown /* Found everything, bring it up */ 90687506549SMark Brown if (card->probe) { 90787506549SMark Brown ret = card->probe(pdev); 908db2a4165SFrank Mandarino if (ret < 0) 909435c5e25SMark Brown return; 910db2a4165SFrank Mandarino } 911db2a4165SFrank Mandarino 91287506549SMark Brown for (i = 0; i < card->num_links; i++) { 91387506549SMark Brown struct snd_soc_dai *cpu_dai = card->dai_link[i].cpu_dai; 914db2a4165SFrank Mandarino if (cpu_dai->probe) { 915bdb92876SMark Brown ret = cpu_dai->probe(pdev, cpu_dai); 916db2a4165SFrank Mandarino if (ret < 0) 917db2a4165SFrank Mandarino goto cpu_dai_err; 918db2a4165SFrank Mandarino } 919db2a4165SFrank Mandarino } 920db2a4165SFrank Mandarino 921db2a4165SFrank Mandarino if (codec_dev->probe) { 922db2a4165SFrank Mandarino ret = codec_dev->probe(pdev); 923db2a4165SFrank Mandarino if (ret < 0) 924db2a4165SFrank Mandarino goto cpu_dai_err; 925db2a4165SFrank Mandarino } 926db2a4165SFrank Mandarino 927db2a4165SFrank Mandarino if (platform->probe) { 928db2a4165SFrank Mandarino ret = platform->probe(pdev); 929db2a4165SFrank Mandarino if (ret < 0) 930db2a4165SFrank Mandarino goto platform_err; 931db2a4165SFrank Mandarino } 932db2a4165SFrank Mandarino 933db2a4165SFrank Mandarino /* DAPM stream work */ 9346308419aSMark Brown INIT_DELAYED_WORK(&card->delayed_work, close_delayed_work); 9351301a964SRandy Dunlap #ifdef CONFIG_PM 9366ed25978SAndy Green /* deferred resume work */ 9376308419aSMark Brown INIT_WORK(&card->deferred_resume_work, soc_resume_deferred); 9381301a964SRandy Dunlap #endif 9396ed25978SAndy Green 940435c5e25SMark Brown card->instantiated = 1; 941435c5e25SMark Brown 942435c5e25SMark Brown return; 943db2a4165SFrank Mandarino 944db2a4165SFrank Mandarino platform_err: 945db2a4165SFrank Mandarino if (codec_dev->remove) 946db2a4165SFrank Mandarino codec_dev->remove(pdev); 947db2a4165SFrank Mandarino 948db2a4165SFrank Mandarino cpu_dai_err: 94918b9b3d9SLiam Girdwood for (i--; i >= 0; i--) { 95087506549SMark Brown struct snd_soc_dai *cpu_dai = card->dai_link[i].cpu_dai; 951db2a4165SFrank Mandarino if (cpu_dai->remove) 952bdb92876SMark Brown cpu_dai->remove(pdev, cpu_dai); 953db2a4165SFrank Mandarino } 954db2a4165SFrank Mandarino 95587506549SMark Brown if (card->remove) 95687506549SMark Brown card->remove(pdev); 957435c5e25SMark Brown } 958db2a4165SFrank Mandarino 959435c5e25SMark Brown /* 960435c5e25SMark Brown * Attempt to initialise any uninitalised cards. Must be called with 961435c5e25SMark Brown * client_mutex. 962435c5e25SMark Brown */ 963435c5e25SMark Brown static void snd_soc_instantiate_cards(void) 964435c5e25SMark Brown { 965435c5e25SMark Brown struct snd_soc_card *card; 966435c5e25SMark Brown list_for_each_entry(card, &card_list, list) 967435c5e25SMark Brown snd_soc_instantiate_card(card); 968435c5e25SMark Brown } 969435c5e25SMark Brown 970435c5e25SMark Brown /* probes a new socdev */ 971435c5e25SMark Brown static int soc_probe(struct platform_device *pdev) 972435c5e25SMark Brown { 973435c5e25SMark Brown int ret = 0; 974435c5e25SMark Brown struct snd_soc_device *socdev = platform_get_drvdata(pdev); 975435c5e25SMark Brown struct snd_soc_card *card = socdev->card; 976435c5e25SMark Brown 977435c5e25SMark Brown /* Bodge while we push things out of socdev */ 978435c5e25SMark Brown card->socdev = socdev; 979435c5e25SMark Brown 980435c5e25SMark Brown /* Bodge while we unpick instantiation */ 981435c5e25SMark Brown card->dev = &pdev->dev; 982435c5e25SMark Brown ret = snd_soc_register_card(card); 983435c5e25SMark Brown if (ret != 0) { 984435c5e25SMark Brown dev_err(&pdev->dev, "Failed to register card\n"); 985db2a4165SFrank Mandarino return ret; 986db2a4165SFrank Mandarino } 987db2a4165SFrank Mandarino 988435c5e25SMark Brown return 0; 989435c5e25SMark Brown } 990435c5e25SMark Brown 991db2a4165SFrank Mandarino /* removes a socdev */ 992db2a4165SFrank Mandarino static int soc_remove(struct platform_device *pdev) 993db2a4165SFrank Mandarino { 994db2a4165SFrank Mandarino int i; 995db2a4165SFrank Mandarino struct snd_soc_device *socdev = platform_get_drvdata(pdev); 99687506549SMark Brown struct snd_soc_card *card = socdev->card; 99787689d56SMark Brown struct snd_soc_platform *platform = card->platform; 998db2a4165SFrank Mandarino struct snd_soc_codec_device *codec_dev = socdev->codec_dev; 999db2a4165SFrank Mandarino 1000914dc182SMike Rapoport if (!card->instantiated) 1001914dc182SMike Rapoport return 0; 1002914dc182SMike Rapoport 10036308419aSMark Brown run_delayed_work(&card->delayed_work); 1004965ac42cSLiam Girdwood 1005db2a4165SFrank Mandarino if (platform->remove) 1006db2a4165SFrank Mandarino platform->remove(pdev); 1007db2a4165SFrank Mandarino 1008db2a4165SFrank Mandarino if (codec_dev->remove) 1009db2a4165SFrank Mandarino codec_dev->remove(pdev); 1010db2a4165SFrank Mandarino 101187506549SMark Brown for (i = 0; i < card->num_links; i++) { 101287506549SMark Brown struct snd_soc_dai *cpu_dai = card->dai_link[i].cpu_dai; 1013db2a4165SFrank Mandarino if (cpu_dai->remove) 1014bdb92876SMark Brown cpu_dai->remove(pdev, cpu_dai); 1015db2a4165SFrank Mandarino } 1016db2a4165SFrank Mandarino 101787506549SMark Brown if (card->remove) 101887506549SMark Brown card->remove(pdev); 1019db2a4165SFrank Mandarino 1020c5af3a2eSMark Brown snd_soc_unregister_card(card); 1021c5af3a2eSMark Brown 1022db2a4165SFrank Mandarino return 0; 1023db2a4165SFrank Mandarino } 1024db2a4165SFrank Mandarino 1025416356fcSMark Brown static int soc_poweroff(struct device *dev) 102651737470SMark Brown { 1027416356fcSMark Brown struct platform_device *pdev = to_platform_device(dev); 102851737470SMark Brown struct snd_soc_device *socdev = platform_get_drvdata(pdev); 102951737470SMark Brown struct snd_soc_card *card = socdev->card; 103051737470SMark Brown 103151737470SMark Brown if (!card->instantiated) 1032416356fcSMark Brown return 0; 103351737470SMark Brown 103451737470SMark Brown /* Flush out pmdown_time work - we actually do want to run it 103551737470SMark Brown * now, we're shutting down so no imminent restart. */ 103651737470SMark Brown run_delayed_work(&card->delayed_work); 103751737470SMark Brown 103851737470SMark Brown snd_soc_dapm_shutdown(socdev); 1039416356fcSMark Brown 1040416356fcSMark Brown return 0; 104151737470SMark Brown } 104251737470SMark Brown 1043416356fcSMark Brown static struct dev_pm_ops soc_pm_ops = { 1044416356fcSMark Brown .suspend = soc_suspend, 1045416356fcSMark Brown .resume = soc_resume, 1046416356fcSMark Brown .poweroff = soc_poweroff, 1047416356fcSMark Brown }; 1048416356fcSMark Brown 1049db2a4165SFrank Mandarino /* ASoC platform driver */ 1050db2a4165SFrank Mandarino static struct platform_driver soc_driver = { 1051db2a4165SFrank Mandarino .driver = { 1052db2a4165SFrank Mandarino .name = "soc-audio", 10538b45a209SKay Sievers .owner = THIS_MODULE, 1054416356fcSMark Brown .pm = &soc_pm_ops, 1055db2a4165SFrank Mandarino }, 1056db2a4165SFrank Mandarino .probe = soc_probe, 1057db2a4165SFrank Mandarino .remove = soc_remove, 1058db2a4165SFrank Mandarino }; 1059db2a4165SFrank Mandarino 1060db2a4165SFrank Mandarino /* create a new pcm */ 1061db2a4165SFrank Mandarino static int soc_new_pcm(struct snd_soc_device *socdev, 1062db2a4165SFrank Mandarino struct snd_soc_dai_link *dai_link, int num) 1063db2a4165SFrank Mandarino { 106487689d56SMark Brown struct snd_soc_card *card = socdev->card; 10656627a653SMark Brown struct snd_soc_codec *codec = card->codec; 106687689d56SMark Brown struct snd_soc_platform *platform = card->platform; 10673c4b266fSLiam Girdwood struct snd_soc_dai *codec_dai = dai_link->codec_dai; 10683c4b266fSLiam Girdwood struct snd_soc_dai *cpu_dai = dai_link->cpu_dai; 1069db2a4165SFrank Mandarino struct snd_soc_pcm_runtime *rtd; 1070db2a4165SFrank Mandarino struct snd_pcm *pcm; 1071db2a4165SFrank Mandarino char new_name[64]; 1072db2a4165SFrank Mandarino int ret = 0, playback = 0, capture = 0; 1073db2a4165SFrank Mandarino 1074db2a4165SFrank Mandarino rtd = kzalloc(sizeof(struct snd_soc_pcm_runtime), GFP_KERNEL); 1075db2a4165SFrank Mandarino if (rtd == NULL) 1076db2a4165SFrank Mandarino return -ENOMEM; 1077cb666e5bSLiam Girdwood 1078cb666e5bSLiam Girdwood rtd->dai = dai_link; 1079db2a4165SFrank Mandarino rtd->socdev = socdev; 10806627a653SMark Brown codec_dai->codec = card->codec; 1081db2a4165SFrank Mandarino 1082db2a4165SFrank Mandarino /* check client and interface hw capabilities */ 10833ba9e10aSMark Brown sprintf(new_name, "%s %s-%d", dai_link->stream_name, codec_dai->name, 10843ba9e10aSMark Brown num); 1085db2a4165SFrank Mandarino 1086db2a4165SFrank Mandarino if (codec_dai->playback.channels_min) 1087db2a4165SFrank Mandarino playback = 1; 1088db2a4165SFrank Mandarino if (codec_dai->capture.channels_min) 1089db2a4165SFrank Mandarino capture = 1; 1090db2a4165SFrank Mandarino 1091db2a4165SFrank Mandarino ret = snd_pcm_new(codec->card, new_name, codec->pcm_devs++, playback, 1092db2a4165SFrank Mandarino capture, &pcm); 1093db2a4165SFrank Mandarino if (ret < 0) { 10943ff3f64bSMark Brown printk(KERN_ERR "asoc: can't create pcm for codec %s\n", 10953ff3f64bSMark Brown codec->name); 1096db2a4165SFrank Mandarino kfree(rtd); 1097db2a4165SFrank Mandarino return ret; 1098db2a4165SFrank Mandarino } 1099db2a4165SFrank Mandarino 11004ccab3e7SLiam Girdwood dai_link->pcm = pcm; 1101db2a4165SFrank Mandarino pcm->private_data = rtd; 110287689d56SMark Brown soc_pcm_ops.mmap = platform->pcm_ops->mmap; 110387689d56SMark Brown soc_pcm_ops.pointer = platform->pcm_ops->pointer; 110487689d56SMark Brown soc_pcm_ops.ioctl = platform->pcm_ops->ioctl; 110587689d56SMark Brown soc_pcm_ops.copy = platform->pcm_ops->copy; 110687689d56SMark Brown soc_pcm_ops.silence = platform->pcm_ops->silence; 110787689d56SMark Brown soc_pcm_ops.ack = platform->pcm_ops->ack; 110887689d56SMark Brown soc_pcm_ops.page = platform->pcm_ops->page; 1109db2a4165SFrank Mandarino 1110db2a4165SFrank Mandarino if (playback) 1111db2a4165SFrank Mandarino snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &soc_pcm_ops); 1112db2a4165SFrank Mandarino 1113db2a4165SFrank Mandarino if (capture) 1114db2a4165SFrank Mandarino snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &soc_pcm_ops); 1115db2a4165SFrank Mandarino 111687689d56SMark Brown ret = platform->pcm_new(codec->card, codec_dai, pcm); 1117db2a4165SFrank Mandarino if (ret < 0) { 1118db2a4165SFrank Mandarino printk(KERN_ERR "asoc: platform pcm constructor failed\n"); 1119db2a4165SFrank Mandarino kfree(rtd); 1120db2a4165SFrank Mandarino return ret; 1121db2a4165SFrank Mandarino } 1122db2a4165SFrank Mandarino 112387689d56SMark Brown pcm->private_free = platform->pcm_free; 1124db2a4165SFrank Mandarino printk(KERN_INFO "asoc: %s <-> %s mapping ok\n", codec_dai->name, 1125db2a4165SFrank Mandarino cpu_dai->name); 1126db2a4165SFrank Mandarino return ret; 1127db2a4165SFrank Mandarino } 1128db2a4165SFrank Mandarino 1129096e49d5SMark Brown /** 1130096e49d5SMark Brown * snd_soc_codec_volatile_register: Report if a register is volatile. 1131096e49d5SMark Brown * 1132096e49d5SMark Brown * @codec: CODEC to query. 1133096e49d5SMark Brown * @reg: Register to query. 1134096e49d5SMark Brown * 1135096e49d5SMark Brown * Boolean function indiciating if a CODEC register is volatile. 1136096e49d5SMark Brown */ 1137096e49d5SMark Brown int snd_soc_codec_volatile_register(struct snd_soc_codec *codec, int reg) 1138096e49d5SMark Brown { 1139096e49d5SMark Brown if (codec->volatile_register) 1140096e49d5SMark Brown return codec->volatile_register(reg); 1141096e49d5SMark Brown else 1142096e49d5SMark Brown return 0; 1143096e49d5SMark Brown } 1144096e49d5SMark Brown EXPORT_SYMBOL_GPL(snd_soc_codec_volatile_register); 1145096e49d5SMark Brown 1146db2a4165SFrank Mandarino /* codec register dump */ 11476627a653SMark Brown static ssize_t soc_codec_reg_show(struct snd_soc_codec *codec, char *buf) 1148db2a4165SFrank Mandarino { 1149db2a4165SFrank Mandarino int i, step = 1, count = 0; 1150db2a4165SFrank Mandarino 1151db2a4165SFrank Mandarino if (!codec->reg_cache_size) 1152db2a4165SFrank Mandarino return 0; 1153db2a4165SFrank Mandarino 1154db2a4165SFrank Mandarino if (codec->reg_cache_step) 1155db2a4165SFrank Mandarino step = codec->reg_cache_step; 1156db2a4165SFrank Mandarino 1157db2a4165SFrank Mandarino count += sprintf(buf, "%s registers\n", codec->name); 115858cd33c0SMark Brown for (i = 0; i < codec->reg_cache_size; i += step) { 115958cd33c0SMark Brown count += sprintf(buf + count, "%2x: ", i); 116058cd33c0SMark Brown if (count >= PAGE_SIZE - 1) 116158cd33c0SMark Brown break; 116258cd33c0SMark Brown 116358cd33c0SMark Brown if (codec->display_register) 116458cd33c0SMark Brown count += codec->display_register(codec, buf + count, 116558cd33c0SMark Brown PAGE_SIZE - count, i); 116658cd33c0SMark Brown else 116758cd33c0SMark Brown count += snprintf(buf + count, PAGE_SIZE - count, 116858cd33c0SMark Brown "%4x", codec->read(codec, i)); 116958cd33c0SMark Brown 117058cd33c0SMark Brown if (count >= PAGE_SIZE - 1) 117158cd33c0SMark Brown break; 117258cd33c0SMark Brown 117358cd33c0SMark Brown count += snprintf(buf + count, PAGE_SIZE - count, "\n"); 117458cd33c0SMark Brown if (count >= PAGE_SIZE - 1) 117558cd33c0SMark Brown break; 117658cd33c0SMark Brown } 117758cd33c0SMark Brown 117858cd33c0SMark Brown /* Truncate count; min() would cause a warning */ 117958cd33c0SMark Brown if (count >= PAGE_SIZE) 118058cd33c0SMark Brown count = PAGE_SIZE - 1; 1181db2a4165SFrank Mandarino 1182db2a4165SFrank Mandarino return count; 1183db2a4165SFrank Mandarino } 118412ef193dSTroy Kisky static ssize_t codec_reg_show(struct device *dev, 118512ef193dSTroy Kisky struct device_attribute *attr, char *buf) 118612ef193dSTroy Kisky { 118712ef193dSTroy Kisky struct snd_soc_device *devdata = dev_get_drvdata(dev); 11886627a653SMark Brown return soc_codec_reg_show(devdata->card->codec, buf); 118912ef193dSTroy Kisky } 119012ef193dSTroy Kisky 1191db2a4165SFrank Mandarino static DEVICE_ATTR(codec_reg, 0444, codec_reg_show, NULL); 1192db2a4165SFrank Mandarino 119312ef193dSTroy Kisky #ifdef CONFIG_DEBUG_FS 119412ef193dSTroy Kisky static int codec_reg_open_file(struct inode *inode, struct file *file) 119512ef193dSTroy Kisky { 119612ef193dSTroy Kisky file->private_data = inode->i_private; 119712ef193dSTroy Kisky return 0; 119812ef193dSTroy Kisky } 119912ef193dSTroy Kisky 120012ef193dSTroy Kisky static ssize_t codec_reg_read_file(struct file *file, char __user *user_buf, 120112ef193dSTroy Kisky size_t count, loff_t *ppos) 120212ef193dSTroy Kisky { 120312ef193dSTroy Kisky ssize_t ret; 1204384c89e2SMark Brown struct snd_soc_codec *codec = file->private_data; 120512ef193dSTroy Kisky char *buf = kmalloc(PAGE_SIZE, GFP_KERNEL); 120612ef193dSTroy Kisky if (!buf) 120712ef193dSTroy Kisky return -ENOMEM; 12086627a653SMark Brown ret = soc_codec_reg_show(codec, buf); 120912ef193dSTroy Kisky if (ret >= 0) 121012ef193dSTroy Kisky ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret); 121112ef193dSTroy Kisky kfree(buf); 121212ef193dSTroy Kisky return ret; 121312ef193dSTroy Kisky } 121412ef193dSTroy Kisky 121512ef193dSTroy Kisky static ssize_t codec_reg_write_file(struct file *file, 121612ef193dSTroy Kisky const char __user *user_buf, size_t count, loff_t *ppos) 121712ef193dSTroy Kisky { 121812ef193dSTroy Kisky char buf[32]; 121912ef193dSTroy Kisky int buf_size; 122012ef193dSTroy Kisky char *start = buf; 122112ef193dSTroy Kisky unsigned long reg, value; 122212ef193dSTroy Kisky int step = 1; 1223384c89e2SMark Brown struct snd_soc_codec *codec = file->private_data; 122412ef193dSTroy Kisky 122512ef193dSTroy Kisky buf_size = min(count, (sizeof(buf)-1)); 122612ef193dSTroy Kisky if (copy_from_user(buf, user_buf, buf_size)) 122712ef193dSTroy Kisky return -EFAULT; 122812ef193dSTroy Kisky buf[buf_size] = 0; 122912ef193dSTroy Kisky 123012ef193dSTroy Kisky if (codec->reg_cache_step) 123112ef193dSTroy Kisky step = codec->reg_cache_step; 123212ef193dSTroy Kisky 123312ef193dSTroy Kisky while (*start == ' ') 123412ef193dSTroy Kisky start++; 123512ef193dSTroy Kisky reg = simple_strtoul(start, &start, 16); 123612ef193dSTroy Kisky if ((reg >= codec->reg_cache_size) || (reg % step)) 123712ef193dSTroy Kisky return -EINVAL; 123812ef193dSTroy Kisky while (*start == ' ') 123912ef193dSTroy Kisky start++; 124012ef193dSTroy Kisky if (strict_strtoul(start, 16, &value)) 124112ef193dSTroy Kisky return -EINVAL; 124212ef193dSTroy Kisky codec->write(codec, reg, value); 124312ef193dSTroy Kisky return buf_size; 124412ef193dSTroy Kisky } 124512ef193dSTroy Kisky 124612ef193dSTroy Kisky static const struct file_operations codec_reg_fops = { 124712ef193dSTroy Kisky .open = codec_reg_open_file, 124812ef193dSTroy Kisky .read = codec_reg_read_file, 124912ef193dSTroy Kisky .write = codec_reg_write_file, 125012ef193dSTroy Kisky }; 125112ef193dSTroy Kisky 1252384c89e2SMark Brown static void soc_init_codec_debugfs(struct snd_soc_codec *codec) 125312ef193dSTroy Kisky { 1254384c89e2SMark Brown codec->debugfs_reg = debugfs_create_file("codec_reg", 0644, 1255384c89e2SMark Brown debugfs_root, codec, 1256384c89e2SMark Brown &codec_reg_fops); 1257384c89e2SMark Brown if (!codec->debugfs_reg) 1258384c89e2SMark Brown printk(KERN_WARNING 1259384c89e2SMark Brown "ASoC: Failed to create codec register debugfs file\n"); 126012ef193dSTroy Kisky 1261384c89e2SMark Brown codec->debugfs_pop_time = debugfs_create_u32("dapm_pop_time", 0744, 1262384c89e2SMark Brown debugfs_root, 1263384c89e2SMark Brown &codec->pop_time); 1264384c89e2SMark Brown if (!codec->debugfs_pop_time) 1265384c89e2SMark Brown printk(KERN_WARNING 1266384c89e2SMark Brown "Failed to create pop time debugfs file\n"); 126712ef193dSTroy Kisky } 126812ef193dSTroy Kisky 1269384c89e2SMark Brown static void soc_cleanup_codec_debugfs(struct snd_soc_codec *codec) 127012ef193dSTroy Kisky { 1271384c89e2SMark Brown debugfs_remove(codec->debugfs_pop_time); 1272384c89e2SMark Brown debugfs_remove(codec->debugfs_reg); 127312ef193dSTroy Kisky } 127412ef193dSTroy Kisky 127512ef193dSTroy Kisky #else 127612ef193dSTroy Kisky 1277384c89e2SMark Brown static inline void soc_init_codec_debugfs(struct snd_soc_codec *codec) 127812ef193dSTroy Kisky { 127912ef193dSTroy Kisky } 128012ef193dSTroy Kisky 1281384c89e2SMark Brown static inline void soc_cleanup_codec_debugfs(struct snd_soc_codec *codec) 128212ef193dSTroy Kisky { 128312ef193dSTroy Kisky } 128412ef193dSTroy Kisky #endif 128512ef193dSTroy Kisky 1286db2a4165SFrank Mandarino /** 1287db2a4165SFrank Mandarino * snd_soc_new_ac97_codec - initailise AC97 device 1288db2a4165SFrank Mandarino * @codec: audio codec 1289db2a4165SFrank Mandarino * @ops: AC97 bus operations 1290db2a4165SFrank Mandarino * @num: AC97 codec number 1291db2a4165SFrank Mandarino * 1292db2a4165SFrank Mandarino * Initialises AC97 codec resources for use by ad-hoc devices only. 1293db2a4165SFrank Mandarino */ 1294db2a4165SFrank Mandarino int snd_soc_new_ac97_codec(struct snd_soc_codec *codec, 1295db2a4165SFrank Mandarino struct snd_ac97_bus_ops *ops, int num) 1296db2a4165SFrank Mandarino { 1297db2a4165SFrank Mandarino mutex_lock(&codec->mutex); 1298db2a4165SFrank Mandarino 1299db2a4165SFrank Mandarino codec->ac97 = kzalloc(sizeof(struct snd_ac97), GFP_KERNEL); 1300db2a4165SFrank Mandarino if (codec->ac97 == NULL) { 1301db2a4165SFrank Mandarino mutex_unlock(&codec->mutex); 1302db2a4165SFrank Mandarino return -ENOMEM; 1303db2a4165SFrank Mandarino } 1304db2a4165SFrank Mandarino 1305db2a4165SFrank Mandarino codec->ac97->bus = kzalloc(sizeof(struct snd_ac97_bus), GFP_KERNEL); 1306db2a4165SFrank Mandarino if (codec->ac97->bus == NULL) { 1307db2a4165SFrank Mandarino kfree(codec->ac97); 1308db2a4165SFrank Mandarino codec->ac97 = NULL; 1309db2a4165SFrank Mandarino mutex_unlock(&codec->mutex); 1310db2a4165SFrank Mandarino return -ENOMEM; 1311db2a4165SFrank Mandarino } 1312db2a4165SFrank Mandarino 1313db2a4165SFrank Mandarino codec->ac97->bus->ops = ops; 1314db2a4165SFrank Mandarino codec->ac97->num = num; 1315db2a4165SFrank Mandarino mutex_unlock(&codec->mutex); 1316db2a4165SFrank Mandarino return 0; 1317db2a4165SFrank Mandarino } 1318db2a4165SFrank Mandarino EXPORT_SYMBOL_GPL(snd_soc_new_ac97_codec); 1319db2a4165SFrank Mandarino 1320db2a4165SFrank Mandarino /** 1321db2a4165SFrank Mandarino * snd_soc_free_ac97_codec - free AC97 codec device 1322db2a4165SFrank Mandarino * @codec: audio codec 1323db2a4165SFrank Mandarino * 1324db2a4165SFrank Mandarino * Frees AC97 codec device resources. 1325db2a4165SFrank Mandarino */ 1326db2a4165SFrank Mandarino void snd_soc_free_ac97_codec(struct snd_soc_codec *codec) 1327db2a4165SFrank Mandarino { 1328db2a4165SFrank Mandarino mutex_lock(&codec->mutex); 1329db2a4165SFrank Mandarino kfree(codec->ac97->bus); 1330db2a4165SFrank Mandarino kfree(codec->ac97); 1331db2a4165SFrank Mandarino codec->ac97 = NULL; 1332db2a4165SFrank Mandarino mutex_unlock(&codec->mutex); 1333db2a4165SFrank Mandarino } 1334db2a4165SFrank Mandarino EXPORT_SYMBOL_GPL(snd_soc_free_ac97_codec); 1335db2a4165SFrank Mandarino 1336db2a4165SFrank Mandarino /** 1337db2a4165SFrank Mandarino * snd_soc_update_bits - update codec register bits 1338db2a4165SFrank Mandarino * @codec: audio codec 1339db2a4165SFrank Mandarino * @reg: codec register 1340db2a4165SFrank Mandarino * @mask: register mask 1341db2a4165SFrank Mandarino * @value: new value 1342db2a4165SFrank Mandarino * 1343db2a4165SFrank Mandarino * Writes new register value. 1344db2a4165SFrank Mandarino * 1345db2a4165SFrank Mandarino * Returns 1 for change else 0. 1346db2a4165SFrank Mandarino */ 1347db2a4165SFrank Mandarino int snd_soc_update_bits(struct snd_soc_codec *codec, unsigned short reg, 134846f5822fSDaniel Ribeiro unsigned int mask, unsigned int value) 1349db2a4165SFrank Mandarino { 1350db2a4165SFrank Mandarino int change; 135146f5822fSDaniel Ribeiro unsigned int old, new; 1352db2a4165SFrank Mandarino 1353db2a4165SFrank Mandarino mutex_lock(&io_mutex); 1354db2a4165SFrank Mandarino old = snd_soc_read(codec, reg); 1355db2a4165SFrank Mandarino new = (old & ~mask) | value; 1356db2a4165SFrank Mandarino change = old != new; 1357db2a4165SFrank Mandarino if (change) 1358db2a4165SFrank Mandarino snd_soc_write(codec, reg, new); 1359db2a4165SFrank Mandarino 1360db2a4165SFrank Mandarino mutex_unlock(&io_mutex); 1361db2a4165SFrank Mandarino return change; 1362db2a4165SFrank Mandarino } 1363db2a4165SFrank Mandarino EXPORT_SYMBOL_GPL(snd_soc_update_bits); 1364db2a4165SFrank Mandarino 1365db2a4165SFrank Mandarino /** 1366db2a4165SFrank Mandarino * snd_soc_test_bits - test register for change 1367db2a4165SFrank Mandarino * @codec: audio codec 1368db2a4165SFrank Mandarino * @reg: codec register 1369db2a4165SFrank Mandarino * @mask: register mask 1370db2a4165SFrank Mandarino * @value: new value 1371db2a4165SFrank Mandarino * 1372db2a4165SFrank Mandarino * Tests a register with a new value and checks if the new value is 1373db2a4165SFrank Mandarino * different from the old value. 1374db2a4165SFrank Mandarino * 1375db2a4165SFrank Mandarino * Returns 1 for change else 0. 1376db2a4165SFrank Mandarino */ 1377db2a4165SFrank Mandarino int snd_soc_test_bits(struct snd_soc_codec *codec, unsigned short reg, 137846f5822fSDaniel Ribeiro unsigned int mask, unsigned int value) 1379db2a4165SFrank Mandarino { 1380db2a4165SFrank Mandarino int change; 138146f5822fSDaniel Ribeiro unsigned int old, new; 1382db2a4165SFrank Mandarino 1383db2a4165SFrank Mandarino mutex_lock(&io_mutex); 1384db2a4165SFrank Mandarino old = snd_soc_read(codec, reg); 1385db2a4165SFrank Mandarino new = (old & ~mask) | value; 1386db2a4165SFrank Mandarino change = old != new; 1387db2a4165SFrank Mandarino mutex_unlock(&io_mutex); 1388db2a4165SFrank Mandarino 1389db2a4165SFrank Mandarino return change; 1390db2a4165SFrank Mandarino } 1391db2a4165SFrank Mandarino EXPORT_SYMBOL_GPL(snd_soc_test_bits); 1392db2a4165SFrank Mandarino 1393db2a4165SFrank Mandarino /** 1394db2a4165SFrank Mandarino * snd_soc_new_pcms - create new sound card and pcms 1395db2a4165SFrank Mandarino * @socdev: the SoC audio device 1396ac11a2b3SMark Brown * @idx: ALSA card index 1397ac11a2b3SMark Brown * @xid: card identification 1398db2a4165SFrank Mandarino * 1399db2a4165SFrank Mandarino * Create a new sound card based upon the codec and interface pcms. 1400db2a4165SFrank Mandarino * 1401db2a4165SFrank Mandarino * Returns 0 for success, else error. 1402db2a4165SFrank Mandarino */ 1403db2a4165SFrank Mandarino int snd_soc_new_pcms(struct snd_soc_device *socdev, int idx, const char *xid) 1404db2a4165SFrank Mandarino { 140587506549SMark Brown struct snd_soc_card *card = socdev->card; 14066627a653SMark Brown struct snd_soc_codec *codec = card->codec; 1407bd7dd77cSTakashi Iwai int ret, i; 1408db2a4165SFrank Mandarino 1409db2a4165SFrank Mandarino mutex_lock(&codec->mutex); 1410db2a4165SFrank Mandarino 1411db2a4165SFrank Mandarino /* register a sound card */ 1412bd7dd77cSTakashi Iwai ret = snd_card_create(idx, xid, codec->owner, 0, &codec->card); 1413bd7dd77cSTakashi Iwai if (ret < 0) { 1414db2a4165SFrank Mandarino printk(KERN_ERR "asoc: can't create sound card for codec %s\n", 1415db2a4165SFrank Mandarino codec->name); 1416db2a4165SFrank Mandarino mutex_unlock(&codec->mutex); 1417bd7dd77cSTakashi Iwai return ret; 1418db2a4165SFrank Mandarino } 1419db2a4165SFrank Mandarino 1420452c5eaaSMark Brown codec->socdev = socdev; 1421db2a4165SFrank Mandarino codec->card->dev = socdev->dev; 1422db2a4165SFrank Mandarino codec->card->private_data = codec; 1423db2a4165SFrank Mandarino strncpy(codec->card->driver, codec->name, sizeof(codec->card->driver)); 1424db2a4165SFrank Mandarino 1425db2a4165SFrank Mandarino /* create the pcms */ 142687506549SMark Brown for (i = 0; i < card->num_links; i++) { 142787506549SMark Brown ret = soc_new_pcm(socdev, &card->dai_link[i], i); 1428db2a4165SFrank Mandarino if (ret < 0) { 1429db2a4165SFrank Mandarino printk(KERN_ERR "asoc: can't create pcm %s\n", 143087506549SMark Brown card->dai_link[i].stream_name); 1431db2a4165SFrank Mandarino mutex_unlock(&codec->mutex); 1432db2a4165SFrank Mandarino return ret; 1433db2a4165SFrank Mandarino } 1434db2a4165SFrank Mandarino } 1435db2a4165SFrank Mandarino 1436db2a4165SFrank Mandarino mutex_unlock(&codec->mutex); 1437db2a4165SFrank Mandarino return ret; 1438db2a4165SFrank Mandarino } 1439db2a4165SFrank Mandarino EXPORT_SYMBOL_GPL(snd_soc_new_pcms); 1440db2a4165SFrank Mandarino 1441db2a4165SFrank Mandarino /** 1442968a6025SMark Brown * snd_soc_init_card - register sound card 1443db2a4165SFrank Mandarino * @socdev: the SoC audio device 1444db2a4165SFrank Mandarino * 1445db2a4165SFrank Mandarino * Register a SoC sound card. Also registers an AC97 device if the 1446db2a4165SFrank Mandarino * codec is AC97 for ad hoc devices. 1447db2a4165SFrank Mandarino * 1448db2a4165SFrank Mandarino * Returns 0 for success, else error. 1449db2a4165SFrank Mandarino */ 1450968a6025SMark Brown int snd_soc_init_card(struct snd_soc_device *socdev) 1451db2a4165SFrank Mandarino { 145287506549SMark Brown struct snd_soc_card *card = socdev->card; 14536627a653SMark Brown struct snd_soc_codec *codec = card->codec; 145412e74f7dSLiam Girdwood int ret = 0, i, ac97 = 0, err = 0; 1455db2a4165SFrank Mandarino 145687506549SMark Brown for (i = 0; i < card->num_links; i++) { 145787506549SMark Brown if (card->dai_link[i].init) { 145887506549SMark Brown err = card->dai_link[i].init(codec); 145912e74f7dSLiam Girdwood if (err < 0) { 146012e74f7dSLiam Girdwood printk(KERN_ERR "asoc: failed to init %s\n", 146187506549SMark Brown card->dai_link[i].stream_name); 146212e74f7dSLiam Girdwood continue; 146312e74f7dSLiam Girdwood } 146412e74f7dSLiam Girdwood } 1465*474828a4SMarek Vasut if (card->dai_link[i].codec_dai->ac97_control) { 1466db2a4165SFrank Mandarino ac97 = 1; 1467*474828a4SMarek Vasut snd_ac97_dev_add_pdata(codec->ac97, 1468*474828a4SMarek Vasut card->dai_link[i].cpu_dai->ac97_pdata); 1469*474828a4SMarek Vasut } 1470db2a4165SFrank Mandarino } 1471db2a4165SFrank Mandarino snprintf(codec->card->shortname, sizeof(codec->card->shortname), 147287506549SMark Brown "%s", card->name); 1473db2a4165SFrank Mandarino snprintf(codec->card->longname, sizeof(codec->card->longname), 147487506549SMark Brown "%s (%s)", card->name, codec->name); 1475db2a4165SFrank Mandarino 14766d5701b2SMark Brown /* Make sure all DAPM widgets are instantiated */ 14776d5701b2SMark Brown snd_soc_dapm_new_widgets(codec); 14786d5701b2SMark Brown 1479db2a4165SFrank Mandarino ret = snd_card_register(codec->card); 1480db2a4165SFrank Mandarino if (ret < 0) { 14813ff3f64bSMark Brown printk(KERN_ERR "asoc: failed to register soundcard for %s\n", 1482db2a4165SFrank Mandarino codec->name); 148312e74f7dSLiam Girdwood goto out; 1484db2a4165SFrank Mandarino } 1485db2a4165SFrank Mandarino 148608c8efe6SMark Brown mutex_lock(&codec->mutex); 1487db2a4165SFrank Mandarino #ifdef CONFIG_SND_SOC_AC97_BUS 148814fa43f5SMark Brown /* Only instantiate AC97 if not already done by the adaptor 148914fa43f5SMark Brown * for the generic AC97 subsystem. 149014fa43f5SMark Brown */ 149114fa43f5SMark Brown if (ac97 && strcmp(codec->name, "AC97") != 0) { 149212e74f7dSLiam Girdwood ret = soc_ac97_dev_register(codec); 149312e74f7dSLiam Girdwood if (ret < 0) { 149412e74f7dSLiam Girdwood printk(KERN_ERR "asoc: AC97 device register failed\n"); 149512e74f7dSLiam Girdwood snd_card_free(codec->card); 149608c8efe6SMark Brown mutex_unlock(&codec->mutex); 149712e74f7dSLiam Girdwood goto out; 149812e74f7dSLiam Girdwood } 149912e74f7dSLiam Girdwood } 1500db2a4165SFrank Mandarino #endif 1501db2a4165SFrank Mandarino 150212e74f7dSLiam Girdwood err = snd_soc_dapm_sys_add(socdev->dev); 150312e74f7dSLiam Girdwood if (err < 0) 150412e74f7dSLiam Girdwood printk(KERN_WARNING "asoc: failed to add dapm sysfs entries\n"); 150512e74f7dSLiam Girdwood 150612e74f7dSLiam Girdwood err = device_create_file(socdev->dev, &dev_attr_codec_reg); 150712e74f7dSLiam Girdwood if (err < 0) 15083ff3f64bSMark Brown printk(KERN_WARNING "asoc: failed to add codec sysfs files\n"); 150908c8efe6SMark Brown 15106627a653SMark Brown soc_init_codec_debugfs(codec); 1511db2a4165SFrank Mandarino mutex_unlock(&codec->mutex); 151208c8efe6SMark Brown 151308c8efe6SMark Brown out: 1514db2a4165SFrank Mandarino return ret; 1515db2a4165SFrank Mandarino } 1516968a6025SMark Brown EXPORT_SYMBOL_GPL(snd_soc_init_card); 1517db2a4165SFrank Mandarino 1518db2a4165SFrank Mandarino /** 1519db2a4165SFrank Mandarino * snd_soc_free_pcms - free sound card and pcms 1520db2a4165SFrank Mandarino * @socdev: the SoC audio device 1521db2a4165SFrank Mandarino * 1522db2a4165SFrank Mandarino * Frees sound card and pcms associated with the socdev. 1523db2a4165SFrank Mandarino * Also unregister the codec if it is an AC97 device. 1524db2a4165SFrank Mandarino */ 1525db2a4165SFrank Mandarino void snd_soc_free_pcms(struct snd_soc_device *socdev) 1526db2a4165SFrank Mandarino { 15276627a653SMark Brown struct snd_soc_codec *codec = socdev->card->codec; 1528a68660e0SLiam Girdwood #ifdef CONFIG_SND_SOC_AC97_BUS 15293c4b266fSLiam Girdwood struct snd_soc_dai *codec_dai; 1530a68660e0SLiam Girdwood int i; 1531a68660e0SLiam Girdwood #endif 1532db2a4165SFrank Mandarino 1533db2a4165SFrank Mandarino mutex_lock(&codec->mutex); 15346627a653SMark Brown soc_cleanup_codec_debugfs(codec); 1535db2a4165SFrank Mandarino #ifdef CONFIG_SND_SOC_AC97_BUS 1536a68660e0SLiam Girdwood for (i = 0; i < codec->num_dai; i++) { 1537a68660e0SLiam Girdwood codec_dai = &codec->dai[i]; 1538d2314e0eSAtsushi Nemoto if (codec_dai->ac97_control && codec->ac97 && 1539d2314e0eSAtsushi Nemoto strcmp(codec->name, "AC97") != 0) { 1540db2a4165SFrank Mandarino soc_ac97_dev_unregister(codec); 1541a68660e0SLiam Girdwood goto free_card; 1542a68660e0SLiam Girdwood } 1543a68660e0SLiam Girdwood } 1544a68660e0SLiam Girdwood free_card: 1545db2a4165SFrank Mandarino #endif 1546db2a4165SFrank Mandarino 1547db2a4165SFrank Mandarino if (codec->card) 1548db2a4165SFrank Mandarino snd_card_free(codec->card); 1549db2a4165SFrank Mandarino device_remove_file(socdev->dev, &dev_attr_codec_reg); 1550db2a4165SFrank Mandarino mutex_unlock(&codec->mutex); 1551db2a4165SFrank Mandarino } 1552db2a4165SFrank Mandarino EXPORT_SYMBOL_GPL(snd_soc_free_pcms); 1553db2a4165SFrank Mandarino 1554db2a4165SFrank Mandarino /** 1555db2a4165SFrank Mandarino * snd_soc_set_runtime_hwparams - set the runtime hardware parameters 1556db2a4165SFrank Mandarino * @substream: the pcm substream 1557db2a4165SFrank Mandarino * @hw: the hardware parameters 1558db2a4165SFrank Mandarino * 1559db2a4165SFrank Mandarino * Sets the substream runtime hardware parameters. 1560db2a4165SFrank Mandarino */ 1561db2a4165SFrank Mandarino int snd_soc_set_runtime_hwparams(struct snd_pcm_substream *substream, 1562db2a4165SFrank Mandarino const struct snd_pcm_hardware *hw) 1563db2a4165SFrank Mandarino { 1564db2a4165SFrank Mandarino struct snd_pcm_runtime *runtime = substream->runtime; 1565db2a4165SFrank Mandarino runtime->hw.info = hw->info; 1566db2a4165SFrank Mandarino runtime->hw.formats = hw->formats; 1567db2a4165SFrank Mandarino runtime->hw.period_bytes_min = hw->period_bytes_min; 1568db2a4165SFrank Mandarino runtime->hw.period_bytes_max = hw->period_bytes_max; 1569db2a4165SFrank Mandarino runtime->hw.periods_min = hw->periods_min; 1570db2a4165SFrank Mandarino runtime->hw.periods_max = hw->periods_max; 1571db2a4165SFrank Mandarino runtime->hw.buffer_bytes_max = hw->buffer_bytes_max; 1572db2a4165SFrank Mandarino runtime->hw.fifo_size = hw->fifo_size; 1573db2a4165SFrank Mandarino return 0; 1574db2a4165SFrank Mandarino } 1575db2a4165SFrank Mandarino EXPORT_SYMBOL_GPL(snd_soc_set_runtime_hwparams); 1576db2a4165SFrank Mandarino 1577db2a4165SFrank Mandarino /** 1578db2a4165SFrank Mandarino * snd_soc_cnew - create new control 1579db2a4165SFrank Mandarino * @_template: control template 1580db2a4165SFrank Mandarino * @data: control private data 1581ac11a2b3SMark Brown * @long_name: control long name 1582db2a4165SFrank Mandarino * 1583db2a4165SFrank Mandarino * Create a new mixer control from a template control. 1584db2a4165SFrank Mandarino * 1585db2a4165SFrank Mandarino * Returns 0 for success, else error. 1586db2a4165SFrank Mandarino */ 1587db2a4165SFrank Mandarino struct snd_kcontrol *snd_soc_cnew(const struct snd_kcontrol_new *_template, 1588db2a4165SFrank Mandarino void *data, char *long_name) 1589db2a4165SFrank Mandarino { 1590db2a4165SFrank Mandarino struct snd_kcontrol_new template; 1591db2a4165SFrank Mandarino 1592db2a4165SFrank Mandarino memcpy(&template, _template, sizeof(template)); 1593db2a4165SFrank Mandarino if (long_name) 1594db2a4165SFrank Mandarino template.name = long_name; 1595db2a4165SFrank Mandarino template.index = 0; 1596db2a4165SFrank Mandarino 1597db2a4165SFrank Mandarino return snd_ctl_new1(&template, data); 1598db2a4165SFrank Mandarino } 1599db2a4165SFrank Mandarino EXPORT_SYMBOL_GPL(snd_soc_cnew); 1600db2a4165SFrank Mandarino 1601db2a4165SFrank Mandarino /** 16023e8e1952SIan Molton * snd_soc_add_controls - add an array of controls to a codec. 16033e8e1952SIan Molton * Convienience function to add a list of controls. Many codecs were 16043e8e1952SIan Molton * duplicating this code. 16053e8e1952SIan Molton * 16063e8e1952SIan Molton * @codec: codec to add controls to 16073e8e1952SIan Molton * @controls: array of controls to add 16083e8e1952SIan Molton * @num_controls: number of elements in the array 16093e8e1952SIan Molton * 16103e8e1952SIan Molton * Return 0 for success, else error. 16113e8e1952SIan Molton */ 16123e8e1952SIan Molton int snd_soc_add_controls(struct snd_soc_codec *codec, 16133e8e1952SIan Molton const struct snd_kcontrol_new *controls, int num_controls) 16143e8e1952SIan Molton { 16153e8e1952SIan Molton struct snd_card *card = codec->card; 16163e8e1952SIan Molton int err, i; 16173e8e1952SIan Molton 16183e8e1952SIan Molton for (i = 0; i < num_controls; i++) { 16193e8e1952SIan Molton const struct snd_kcontrol_new *control = &controls[i]; 16203e8e1952SIan Molton err = snd_ctl_add(card, snd_soc_cnew(control, codec, NULL)); 16213e8e1952SIan Molton if (err < 0) { 16223e8e1952SIan Molton dev_err(codec->dev, "%s: Failed to add %s\n", 16233e8e1952SIan Molton codec->name, control->name); 16243e8e1952SIan Molton return err; 16253e8e1952SIan Molton } 16263e8e1952SIan Molton } 16273e8e1952SIan Molton 16283e8e1952SIan Molton return 0; 16293e8e1952SIan Molton } 16303e8e1952SIan Molton EXPORT_SYMBOL_GPL(snd_soc_add_controls); 16313e8e1952SIan Molton 16323e8e1952SIan Molton /** 1633db2a4165SFrank Mandarino * snd_soc_info_enum_double - enumerated double mixer info callback 1634db2a4165SFrank Mandarino * @kcontrol: mixer control 1635db2a4165SFrank Mandarino * @uinfo: control element information 1636db2a4165SFrank Mandarino * 1637db2a4165SFrank Mandarino * Callback to provide information about a double enumerated 1638db2a4165SFrank Mandarino * mixer control. 1639db2a4165SFrank Mandarino * 1640db2a4165SFrank Mandarino * Returns 0 for success. 1641db2a4165SFrank Mandarino */ 1642db2a4165SFrank Mandarino int snd_soc_info_enum_double(struct snd_kcontrol *kcontrol, 1643db2a4165SFrank Mandarino struct snd_ctl_elem_info *uinfo) 1644db2a4165SFrank Mandarino { 1645db2a4165SFrank Mandarino struct soc_enum *e = (struct soc_enum *)kcontrol->private_value; 1646db2a4165SFrank Mandarino 1647db2a4165SFrank Mandarino uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED; 1648db2a4165SFrank Mandarino uinfo->count = e->shift_l == e->shift_r ? 1 : 2; 1649f8ba0b7bSJon Smirl uinfo->value.enumerated.items = e->max; 1650db2a4165SFrank Mandarino 1651f8ba0b7bSJon Smirl if (uinfo->value.enumerated.item > e->max - 1) 1652f8ba0b7bSJon Smirl uinfo->value.enumerated.item = e->max - 1; 1653db2a4165SFrank Mandarino strcpy(uinfo->value.enumerated.name, 1654db2a4165SFrank Mandarino e->texts[uinfo->value.enumerated.item]); 1655db2a4165SFrank Mandarino return 0; 1656db2a4165SFrank Mandarino } 1657db2a4165SFrank Mandarino EXPORT_SYMBOL_GPL(snd_soc_info_enum_double); 1658db2a4165SFrank Mandarino 1659db2a4165SFrank Mandarino /** 1660db2a4165SFrank Mandarino * snd_soc_get_enum_double - enumerated double mixer get callback 1661db2a4165SFrank Mandarino * @kcontrol: mixer control 1662ac11a2b3SMark Brown * @ucontrol: control element information 1663db2a4165SFrank Mandarino * 1664db2a4165SFrank Mandarino * Callback to get the value of a double enumerated mixer. 1665db2a4165SFrank Mandarino * 1666db2a4165SFrank Mandarino * Returns 0 for success. 1667db2a4165SFrank Mandarino */ 1668db2a4165SFrank Mandarino int snd_soc_get_enum_double(struct snd_kcontrol *kcontrol, 1669db2a4165SFrank Mandarino struct snd_ctl_elem_value *ucontrol) 1670db2a4165SFrank Mandarino { 1671db2a4165SFrank Mandarino struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol); 1672db2a4165SFrank Mandarino struct soc_enum *e = (struct soc_enum *)kcontrol->private_value; 167346f5822fSDaniel Ribeiro unsigned int val, bitmask; 1674db2a4165SFrank Mandarino 1675f8ba0b7bSJon Smirl for (bitmask = 1; bitmask < e->max; bitmask <<= 1) 1676db2a4165SFrank Mandarino ; 1677db2a4165SFrank Mandarino val = snd_soc_read(codec, e->reg); 16783ff3f64bSMark Brown ucontrol->value.enumerated.item[0] 16793ff3f64bSMark Brown = (val >> e->shift_l) & (bitmask - 1); 1680db2a4165SFrank Mandarino if (e->shift_l != e->shift_r) 1681db2a4165SFrank Mandarino ucontrol->value.enumerated.item[1] = 1682db2a4165SFrank Mandarino (val >> e->shift_r) & (bitmask - 1); 1683db2a4165SFrank Mandarino 1684db2a4165SFrank Mandarino return 0; 1685db2a4165SFrank Mandarino } 1686db2a4165SFrank Mandarino EXPORT_SYMBOL_GPL(snd_soc_get_enum_double); 1687db2a4165SFrank Mandarino 1688db2a4165SFrank Mandarino /** 1689db2a4165SFrank Mandarino * snd_soc_put_enum_double - enumerated double mixer put callback 1690db2a4165SFrank Mandarino * @kcontrol: mixer control 1691ac11a2b3SMark Brown * @ucontrol: control element information 1692db2a4165SFrank Mandarino * 1693db2a4165SFrank Mandarino * Callback to set the value of a double enumerated mixer. 1694db2a4165SFrank Mandarino * 1695db2a4165SFrank Mandarino * Returns 0 for success. 1696db2a4165SFrank Mandarino */ 1697db2a4165SFrank Mandarino int snd_soc_put_enum_double(struct snd_kcontrol *kcontrol, 1698db2a4165SFrank Mandarino struct snd_ctl_elem_value *ucontrol) 1699db2a4165SFrank Mandarino { 1700db2a4165SFrank Mandarino struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol); 1701db2a4165SFrank Mandarino struct soc_enum *e = (struct soc_enum *)kcontrol->private_value; 170246f5822fSDaniel Ribeiro unsigned int val; 170346f5822fSDaniel Ribeiro unsigned int mask, bitmask; 1704db2a4165SFrank Mandarino 1705f8ba0b7bSJon Smirl for (bitmask = 1; bitmask < e->max; bitmask <<= 1) 1706db2a4165SFrank Mandarino ; 1707f8ba0b7bSJon Smirl if (ucontrol->value.enumerated.item[0] > e->max - 1) 1708db2a4165SFrank Mandarino return -EINVAL; 1709db2a4165SFrank Mandarino val = ucontrol->value.enumerated.item[0] << e->shift_l; 1710db2a4165SFrank Mandarino mask = (bitmask - 1) << e->shift_l; 1711db2a4165SFrank Mandarino if (e->shift_l != e->shift_r) { 1712f8ba0b7bSJon Smirl if (ucontrol->value.enumerated.item[1] > e->max - 1) 1713db2a4165SFrank Mandarino return -EINVAL; 1714db2a4165SFrank Mandarino val |= ucontrol->value.enumerated.item[1] << e->shift_r; 1715db2a4165SFrank Mandarino mask |= (bitmask - 1) << e->shift_r; 1716db2a4165SFrank Mandarino } 1717db2a4165SFrank Mandarino 1718db2a4165SFrank Mandarino return snd_soc_update_bits(codec, e->reg, mask, val); 1719db2a4165SFrank Mandarino } 1720db2a4165SFrank Mandarino EXPORT_SYMBOL_GPL(snd_soc_put_enum_double); 1721db2a4165SFrank Mandarino 1722db2a4165SFrank Mandarino /** 17232e72f8e3SPeter Ujfalusi * snd_soc_get_value_enum_double - semi enumerated double mixer get callback 17242e72f8e3SPeter Ujfalusi * @kcontrol: mixer control 17252e72f8e3SPeter Ujfalusi * @ucontrol: control element information 17262e72f8e3SPeter Ujfalusi * 17272e72f8e3SPeter Ujfalusi * Callback to get the value of a double semi enumerated mixer. 17282e72f8e3SPeter Ujfalusi * 17292e72f8e3SPeter Ujfalusi * Semi enumerated mixer: the enumerated items are referred as values. Can be 17302e72f8e3SPeter Ujfalusi * used for handling bitfield coded enumeration for example. 17312e72f8e3SPeter Ujfalusi * 17322e72f8e3SPeter Ujfalusi * Returns 0 for success. 17332e72f8e3SPeter Ujfalusi */ 17342e72f8e3SPeter Ujfalusi int snd_soc_get_value_enum_double(struct snd_kcontrol *kcontrol, 17352e72f8e3SPeter Ujfalusi struct snd_ctl_elem_value *ucontrol) 17362e72f8e3SPeter Ujfalusi { 17372e72f8e3SPeter Ujfalusi struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol); 173874155556SPeter Ujfalusi struct soc_enum *e = (struct soc_enum *)kcontrol->private_value; 173946f5822fSDaniel Ribeiro unsigned int reg_val, val, mux; 17402e72f8e3SPeter Ujfalusi 17412e72f8e3SPeter Ujfalusi reg_val = snd_soc_read(codec, e->reg); 17422e72f8e3SPeter Ujfalusi val = (reg_val >> e->shift_l) & e->mask; 17432e72f8e3SPeter Ujfalusi for (mux = 0; mux < e->max; mux++) { 17442e72f8e3SPeter Ujfalusi if (val == e->values[mux]) 17452e72f8e3SPeter Ujfalusi break; 17462e72f8e3SPeter Ujfalusi } 17472e72f8e3SPeter Ujfalusi ucontrol->value.enumerated.item[0] = mux; 17482e72f8e3SPeter Ujfalusi if (e->shift_l != e->shift_r) { 17492e72f8e3SPeter Ujfalusi val = (reg_val >> e->shift_r) & e->mask; 17502e72f8e3SPeter Ujfalusi for (mux = 0; mux < e->max; mux++) { 17512e72f8e3SPeter Ujfalusi if (val == e->values[mux]) 17522e72f8e3SPeter Ujfalusi break; 17532e72f8e3SPeter Ujfalusi } 17542e72f8e3SPeter Ujfalusi ucontrol->value.enumerated.item[1] = mux; 17552e72f8e3SPeter Ujfalusi } 17562e72f8e3SPeter Ujfalusi 17572e72f8e3SPeter Ujfalusi return 0; 17582e72f8e3SPeter Ujfalusi } 17592e72f8e3SPeter Ujfalusi EXPORT_SYMBOL_GPL(snd_soc_get_value_enum_double); 17602e72f8e3SPeter Ujfalusi 17612e72f8e3SPeter Ujfalusi /** 17622e72f8e3SPeter Ujfalusi * snd_soc_put_value_enum_double - semi enumerated double mixer put callback 17632e72f8e3SPeter Ujfalusi * @kcontrol: mixer control 17642e72f8e3SPeter Ujfalusi * @ucontrol: control element information 17652e72f8e3SPeter Ujfalusi * 17662e72f8e3SPeter Ujfalusi * Callback to set the value of a double semi enumerated mixer. 17672e72f8e3SPeter Ujfalusi * 17682e72f8e3SPeter Ujfalusi * Semi enumerated mixer: the enumerated items are referred as values. Can be 17692e72f8e3SPeter Ujfalusi * used for handling bitfield coded enumeration for example. 17702e72f8e3SPeter Ujfalusi * 17712e72f8e3SPeter Ujfalusi * Returns 0 for success. 17722e72f8e3SPeter Ujfalusi */ 17732e72f8e3SPeter Ujfalusi int snd_soc_put_value_enum_double(struct snd_kcontrol *kcontrol, 17742e72f8e3SPeter Ujfalusi struct snd_ctl_elem_value *ucontrol) 17752e72f8e3SPeter Ujfalusi { 17762e72f8e3SPeter Ujfalusi struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol); 177774155556SPeter Ujfalusi struct soc_enum *e = (struct soc_enum *)kcontrol->private_value; 177846f5822fSDaniel Ribeiro unsigned int val; 177946f5822fSDaniel Ribeiro unsigned int mask; 17802e72f8e3SPeter Ujfalusi 17812e72f8e3SPeter Ujfalusi if (ucontrol->value.enumerated.item[0] > e->max - 1) 17822e72f8e3SPeter Ujfalusi return -EINVAL; 17832e72f8e3SPeter Ujfalusi val = e->values[ucontrol->value.enumerated.item[0]] << e->shift_l; 17842e72f8e3SPeter Ujfalusi mask = e->mask << e->shift_l; 17852e72f8e3SPeter Ujfalusi if (e->shift_l != e->shift_r) { 17862e72f8e3SPeter Ujfalusi if (ucontrol->value.enumerated.item[1] > e->max - 1) 17872e72f8e3SPeter Ujfalusi return -EINVAL; 17882e72f8e3SPeter Ujfalusi val |= e->values[ucontrol->value.enumerated.item[1]] << e->shift_r; 17892e72f8e3SPeter Ujfalusi mask |= e->mask << e->shift_r; 17902e72f8e3SPeter Ujfalusi } 17912e72f8e3SPeter Ujfalusi 17922e72f8e3SPeter Ujfalusi return snd_soc_update_bits(codec, e->reg, mask, val); 17932e72f8e3SPeter Ujfalusi } 17942e72f8e3SPeter Ujfalusi EXPORT_SYMBOL_GPL(snd_soc_put_value_enum_double); 17952e72f8e3SPeter Ujfalusi 17962e72f8e3SPeter Ujfalusi /** 1797db2a4165SFrank Mandarino * snd_soc_info_enum_ext - external enumerated single mixer info callback 1798db2a4165SFrank Mandarino * @kcontrol: mixer control 1799db2a4165SFrank Mandarino * @uinfo: control element information 1800db2a4165SFrank Mandarino * 1801db2a4165SFrank Mandarino * Callback to provide information about an external enumerated 1802db2a4165SFrank Mandarino * single mixer. 1803db2a4165SFrank Mandarino * 1804db2a4165SFrank Mandarino * Returns 0 for success. 1805db2a4165SFrank Mandarino */ 1806db2a4165SFrank Mandarino int snd_soc_info_enum_ext(struct snd_kcontrol *kcontrol, 1807db2a4165SFrank Mandarino struct snd_ctl_elem_info *uinfo) 1808db2a4165SFrank Mandarino { 1809db2a4165SFrank Mandarino struct soc_enum *e = (struct soc_enum *)kcontrol->private_value; 1810db2a4165SFrank Mandarino 1811db2a4165SFrank Mandarino uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED; 1812db2a4165SFrank Mandarino uinfo->count = 1; 1813f8ba0b7bSJon Smirl uinfo->value.enumerated.items = e->max; 1814db2a4165SFrank Mandarino 1815f8ba0b7bSJon Smirl if (uinfo->value.enumerated.item > e->max - 1) 1816f8ba0b7bSJon Smirl uinfo->value.enumerated.item = e->max - 1; 1817db2a4165SFrank Mandarino strcpy(uinfo->value.enumerated.name, 1818db2a4165SFrank Mandarino e->texts[uinfo->value.enumerated.item]); 1819db2a4165SFrank Mandarino return 0; 1820db2a4165SFrank Mandarino } 1821db2a4165SFrank Mandarino EXPORT_SYMBOL_GPL(snd_soc_info_enum_ext); 1822db2a4165SFrank Mandarino 1823db2a4165SFrank Mandarino /** 1824db2a4165SFrank Mandarino * snd_soc_info_volsw_ext - external single mixer info callback 1825db2a4165SFrank Mandarino * @kcontrol: mixer control 1826db2a4165SFrank Mandarino * @uinfo: control element information 1827db2a4165SFrank Mandarino * 1828db2a4165SFrank Mandarino * Callback to provide information about a single external mixer control. 1829db2a4165SFrank Mandarino * 1830db2a4165SFrank Mandarino * Returns 0 for success. 1831db2a4165SFrank Mandarino */ 1832db2a4165SFrank Mandarino int snd_soc_info_volsw_ext(struct snd_kcontrol *kcontrol, 1833db2a4165SFrank Mandarino struct snd_ctl_elem_info *uinfo) 1834db2a4165SFrank Mandarino { 1835a7a4ac86SPhilipp Zabel int max = kcontrol->private_value; 1836db2a4165SFrank Mandarino 1837fd5dfad9SMark Brown if (max == 1 && !strstr(kcontrol->id.name, " Volume")) 1838a7a4ac86SPhilipp Zabel uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN; 1839a7a4ac86SPhilipp Zabel else 1840a7a4ac86SPhilipp Zabel uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER; 1841a7a4ac86SPhilipp Zabel 1842db2a4165SFrank Mandarino uinfo->count = 1; 1843db2a4165SFrank Mandarino uinfo->value.integer.min = 0; 1844a7a4ac86SPhilipp Zabel uinfo->value.integer.max = max; 1845db2a4165SFrank Mandarino return 0; 1846db2a4165SFrank Mandarino } 1847db2a4165SFrank Mandarino EXPORT_SYMBOL_GPL(snd_soc_info_volsw_ext); 1848db2a4165SFrank Mandarino 1849db2a4165SFrank Mandarino /** 1850db2a4165SFrank Mandarino * snd_soc_info_volsw - single mixer info callback 1851db2a4165SFrank Mandarino * @kcontrol: mixer control 1852db2a4165SFrank Mandarino * @uinfo: control element information 1853db2a4165SFrank Mandarino * 1854db2a4165SFrank Mandarino * Callback to provide information about a single mixer control. 1855db2a4165SFrank Mandarino * 1856db2a4165SFrank Mandarino * Returns 0 for success. 1857db2a4165SFrank Mandarino */ 1858db2a4165SFrank Mandarino int snd_soc_info_volsw(struct snd_kcontrol *kcontrol, 1859db2a4165SFrank Mandarino struct snd_ctl_elem_info *uinfo) 1860db2a4165SFrank Mandarino { 18614eaa9819SJon Smirl struct soc_mixer_control *mc = 18624eaa9819SJon Smirl (struct soc_mixer_control *)kcontrol->private_value; 18634eaa9819SJon Smirl int max = mc->max; 1864762b8df7SMark Brown unsigned int shift = mc->shift; 1865815ecf8dSJon Smirl unsigned int rshift = mc->rshift; 1866db2a4165SFrank Mandarino 1867fd5dfad9SMark Brown if (max == 1 && !strstr(kcontrol->id.name, " Volume")) 1868a7a4ac86SPhilipp Zabel uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN; 1869a7a4ac86SPhilipp Zabel else 1870a7a4ac86SPhilipp Zabel uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER; 1871a7a4ac86SPhilipp Zabel 1872db2a4165SFrank Mandarino uinfo->count = shift == rshift ? 1 : 2; 1873db2a4165SFrank Mandarino uinfo->value.integer.min = 0; 1874a7a4ac86SPhilipp Zabel uinfo->value.integer.max = max; 1875db2a4165SFrank Mandarino return 0; 1876db2a4165SFrank Mandarino } 1877db2a4165SFrank Mandarino EXPORT_SYMBOL_GPL(snd_soc_info_volsw); 1878db2a4165SFrank Mandarino 1879db2a4165SFrank Mandarino /** 1880db2a4165SFrank Mandarino * snd_soc_get_volsw - single mixer get callback 1881db2a4165SFrank Mandarino * @kcontrol: mixer control 1882ac11a2b3SMark Brown * @ucontrol: control element information 1883db2a4165SFrank Mandarino * 1884db2a4165SFrank Mandarino * Callback to get the value of a single mixer control. 1885db2a4165SFrank Mandarino * 1886db2a4165SFrank Mandarino * Returns 0 for success. 1887db2a4165SFrank Mandarino */ 1888db2a4165SFrank Mandarino int snd_soc_get_volsw(struct snd_kcontrol *kcontrol, 1889db2a4165SFrank Mandarino struct snd_ctl_elem_value *ucontrol) 1890db2a4165SFrank Mandarino { 18914eaa9819SJon Smirl struct soc_mixer_control *mc = 18924eaa9819SJon Smirl (struct soc_mixer_control *)kcontrol->private_value; 1893db2a4165SFrank Mandarino struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol); 1894815ecf8dSJon Smirl unsigned int reg = mc->reg; 1895815ecf8dSJon Smirl unsigned int shift = mc->shift; 1896815ecf8dSJon Smirl unsigned int rshift = mc->rshift; 18974eaa9819SJon Smirl int max = mc->max; 1898815ecf8dSJon Smirl unsigned int mask = (1 << fls(max)) - 1; 1899815ecf8dSJon Smirl unsigned int invert = mc->invert; 1900db2a4165SFrank Mandarino 1901db2a4165SFrank Mandarino ucontrol->value.integer.value[0] = 1902db2a4165SFrank Mandarino (snd_soc_read(codec, reg) >> shift) & mask; 1903db2a4165SFrank Mandarino if (shift != rshift) 1904db2a4165SFrank Mandarino ucontrol->value.integer.value[1] = 1905db2a4165SFrank Mandarino (snd_soc_read(codec, reg) >> rshift) & mask; 1906db2a4165SFrank Mandarino if (invert) { 1907db2a4165SFrank Mandarino ucontrol->value.integer.value[0] = 1908a7a4ac86SPhilipp Zabel max - ucontrol->value.integer.value[0]; 1909db2a4165SFrank Mandarino if (shift != rshift) 1910db2a4165SFrank Mandarino ucontrol->value.integer.value[1] = 1911a7a4ac86SPhilipp Zabel max - ucontrol->value.integer.value[1]; 1912db2a4165SFrank Mandarino } 1913db2a4165SFrank Mandarino 1914db2a4165SFrank Mandarino return 0; 1915db2a4165SFrank Mandarino } 1916db2a4165SFrank Mandarino EXPORT_SYMBOL_GPL(snd_soc_get_volsw); 1917db2a4165SFrank Mandarino 1918db2a4165SFrank Mandarino /** 1919db2a4165SFrank Mandarino * snd_soc_put_volsw - single mixer put callback 1920db2a4165SFrank Mandarino * @kcontrol: mixer control 1921ac11a2b3SMark Brown * @ucontrol: control element information 1922db2a4165SFrank Mandarino * 1923db2a4165SFrank Mandarino * Callback to set the value of a single mixer control. 1924db2a4165SFrank Mandarino * 1925db2a4165SFrank Mandarino * Returns 0 for success. 1926db2a4165SFrank Mandarino */ 1927db2a4165SFrank Mandarino int snd_soc_put_volsw(struct snd_kcontrol *kcontrol, 1928db2a4165SFrank Mandarino struct snd_ctl_elem_value *ucontrol) 1929db2a4165SFrank Mandarino { 19304eaa9819SJon Smirl struct soc_mixer_control *mc = 19314eaa9819SJon Smirl (struct soc_mixer_control *)kcontrol->private_value; 1932db2a4165SFrank Mandarino struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol); 1933815ecf8dSJon Smirl unsigned int reg = mc->reg; 1934815ecf8dSJon Smirl unsigned int shift = mc->shift; 1935815ecf8dSJon Smirl unsigned int rshift = mc->rshift; 19364eaa9819SJon Smirl int max = mc->max; 1937815ecf8dSJon Smirl unsigned int mask = (1 << fls(max)) - 1; 1938815ecf8dSJon Smirl unsigned int invert = mc->invert; 193946f5822fSDaniel Ribeiro unsigned int val, val2, val_mask; 1940db2a4165SFrank Mandarino 1941db2a4165SFrank Mandarino val = (ucontrol->value.integer.value[0] & mask); 1942db2a4165SFrank Mandarino if (invert) 1943a7a4ac86SPhilipp Zabel val = max - val; 1944db2a4165SFrank Mandarino val_mask = mask << shift; 1945db2a4165SFrank Mandarino val = val << shift; 1946db2a4165SFrank Mandarino if (shift != rshift) { 1947db2a4165SFrank Mandarino val2 = (ucontrol->value.integer.value[1] & mask); 1948db2a4165SFrank Mandarino if (invert) 1949a7a4ac86SPhilipp Zabel val2 = max - val2; 1950db2a4165SFrank Mandarino val_mask |= mask << rshift; 1951db2a4165SFrank Mandarino val |= val2 << rshift; 1952db2a4165SFrank Mandarino } 1953a7a4ac86SPhilipp Zabel return snd_soc_update_bits(codec, reg, val_mask, val); 1954db2a4165SFrank Mandarino } 1955db2a4165SFrank Mandarino EXPORT_SYMBOL_GPL(snd_soc_put_volsw); 1956db2a4165SFrank Mandarino 1957db2a4165SFrank Mandarino /** 1958db2a4165SFrank Mandarino * snd_soc_info_volsw_2r - double mixer info callback 1959db2a4165SFrank Mandarino * @kcontrol: mixer control 1960db2a4165SFrank Mandarino * @uinfo: control element information 1961db2a4165SFrank Mandarino * 1962db2a4165SFrank Mandarino * Callback to provide information about a double mixer control that 1963db2a4165SFrank Mandarino * spans 2 codec registers. 1964db2a4165SFrank Mandarino * 1965db2a4165SFrank Mandarino * Returns 0 for success. 1966db2a4165SFrank Mandarino */ 1967db2a4165SFrank Mandarino int snd_soc_info_volsw_2r(struct snd_kcontrol *kcontrol, 1968db2a4165SFrank Mandarino struct snd_ctl_elem_info *uinfo) 1969db2a4165SFrank Mandarino { 19704eaa9819SJon Smirl struct soc_mixer_control *mc = 19714eaa9819SJon Smirl (struct soc_mixer_control *)kcontrol->private_value; 19724eaa9819SJon Smirl int max = mc->max; 1973db2a4165SFrank Mandarino 1974fd5dfad9SMark Brown if (max == 1 && !strstr(kcontrol->id.name, " Volume")) 1975a7a4ac86SPhilipp Zabel uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN; 1976a7a4ac86SPhilipp Zabel else 1977a7a4ac86SPhilipp Zabel uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER; 1978a7a4ac86SPhilipp Zabel 1979db2a4165SFrank Mandarino uinfo->count = 2; 1980db2a4165SFrank Mandarino uinfo->value.integer.min = 0; 1981a7a4ac86SPhilipp Zabel uinfo->value.integer.max = max; 1982db2a4165SFrank Mandarino return 0; 1983db2a4165SFrank Mandarino } 1984db2a4165SFrank Mandarino EXPORT_SYMBOL_GPL(snd_soc_info_volsw_2r); 1985db2a4165SFrank Mandarino 1986db2a4165SFrank Mandarino /** 1987db2a4165SFrank Mandarino * snd_soc_get_volsw_2r - double mixer get callback 1988db2a4165SFrank Mandarino * @kcontrol: mixer control 1989ac11a2b3SMark Brown * @ucontrol: control element information 1990db2a4165SFrank Mandarino * 1991db2a4165SFrank Mandarino * Callback to get the value of a double mixer control that spans 2 registers. 1992db2a4165SFrank Mandarino * 1993db2a4165SFrank Mandarino * Returns 0 for success. 1994db2a4165SFrank Mandarino */ 1995db2a4165SFrank Mandarino int snd_soc_get_volsw_2r(struct snd_kcontrol *kcontrol, 1996db2a4165SFrank Mandarino struct snd_ctl_elem_value *ucontrol) 1997db2a4165SFrank Mandarino { 19984eaa9819SJon Smirl struct soc_mixer_control *mc = 19994eaa9819SJon Smirl (struct soc_mixer_control *)kcontrol->private_value; 2000db2a4165SFrank Mandarino struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol); 2001815ecf8dSJon Smirl unsigned int reg = mc->reg; 2002815ecf8dSJon Smirl unsigned int reg2 = mc->rreg; 2003815ecf8dSJon Smirl unsigned int shift = mc->shift; 20044eaa9819SJon Smirl int max = mc->max; 2005815ecf8dSJon Smirl unsigned int mask = (1 << fls(max)) - 1; 2006815ecf8dSJon Smirl unsigned int invert = mc->invert; 2007db2a4165SFrank Mandarino 2008db2a4165SFrank Mandarino ucontrol->value.integer.value[0] = 2009db2a4165SFrank Mandarino (snd_soc_read(codec, reg) >> shift) & mask; 2010db2a4165SFrank Mandarino ucontrol->value.integer.value[1] = 2011db2a4165SFrank Mandarino (snd_soc_read(codec, reg2) >> shift) & mask; 2012db2a4165SFrank Mandarino if (invert) { 2013db2a4165SFrank Mandarino ucontrol->value.integer.value[0] = 2014a7a4ac86SPhilipp Zabel max - ucontrol->value.integer.value[0]; 2015db2a4165SFrank Mandarino ucontrol->value.integer.value[1] = 2016a7a4ac86SPhilipp Zabel max - ucontrol->value.integer.value[1]; 2017db2a4165SFrank Mandarino } 2018db2a4165SFrank Mandarino 2019db2a4165SFrank Mandarino return 0; 2020db2a4165SFrank Mandarino } 2021db2a4165SFrank Mandarino EXPORT_SYMBOL_GPL(snd_soc_get_volsw_2r); 2022db2a4165SFrank Mandarino 2023db2a4165SFrank Mandarino /** 2024db2a4165SFrank Mandarino * snd_soc_put_volsw_2r - double mixer set callback 2025db2a4165SFrank Mandarino * @kcontrol: mixer control 2026ac11a2b3SMark Brown * @ucontrol: control element information 2027db2a4165SFrank Mandarino * 2028db2a4165SFrank Mandarino * Callback to set the value of a double mixer control that spans 2 registers. 2029db2a4165SFrank Mandarino * 2030db2a4165SFrank Mandarino * Returns 0 for success. 2031db2a4165SFrank Mandarino */ 2032db2a4165SFrank Mandarino int snd_soc_put_volsw_2r(struct snd_kcontrol *kcontrol, 2033db2a4165SFrank Mandarino struct snd_ctl_elem_value *ucontrol) 2034db2a4165SFrank Mandarino { 20354eaa9819SJon Smirl struct soc_mixer_control *mc = 20364eaa9819SJon Smirl (struct soc_mixer_control *)kcontrol->private_value; 2037db2a4165SFrank Mandarino struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol); 2038815ecf8dSJon Smirl unsigned int reg = mc->reg; 2039815ecf8dSJon Smirl unsigned int reg2 = mc->rreg; 2040815ecf8dSJon Smirl unsigned int shift = mc->shift; 20414eaa9819SJon Smirl int max = mc->max; 2042815ecf8dSJon Smirl unsigned int mask = (1 << fls(max)) - 1; 2043815ecf8dSJon Smirl unsigned int invert = mc->invert; 2044db2a4165SFrank Mandarino int err; 204546f5822fSDaniel Ribeiro unsigned int val, val2, val_mask; 2046db2a4165SFrank Mandarino 2047db2a4165SFrank Mandarino val_mask = mask << shift; 2048db2a4165SFrank Mandarino val = (ucontrol->value.integer.value[0] & mask); 2049db2a4165SFrank Mandarino val2 = (ucontrol->value.integer.value[1] & mask); 2050db2a4165SFrank Mandarino 2051db2a4165SFrank Mandarino if (invert) { 2052a7a4ac86SPhilipp Zabel val = max - val; 2053a7a4ac86SPhilipp Zabel val2 = max - val2; 2054db2a4165SFrank Mandarino } 2055db2a4165SFrank Mandarino 2056db2a4165SFrank Mandarino val = val << shift; 2057db2a4165SFrank Mandarino val2 = val2 << shift; 2058db2a4165SFrank Mandarino 20593ff3f64bSMark Brown err = snd_soc_update_bits(codec, reg, val_mask, val); 20603ff3f64bSMark Brown if (err < 0) 2061db2a4165SFrank Mandarino return err; 2062db2a4165SFrank Mandarino 2063db2a4165SFrank Mandarino err = snd_soc_update_bits(codec, reg2, val_mask, val2); 2064db2a4165SFrank Mandarino return err; 2065db2a4165SFrank Mandarino } 2066db2a4165SFrank Mandarino EXPORT_SYMBOL_GPL(snd_soc_put_volsw_2r); 2067db2a4165SFrank Mandarino 2068e13ac2e9SMark Brown /** 2069e13ac2e9SMark Brown * snd_soc_info_volsw_s8 - signed mixer info callback 2070e13ac2e9SMark Brown * @kcontrol: mixer control 2071e13ac2e9SMark Brown * @uinfo: control element information 2072e13ac2e9SMark Brown * 2073e13ac2e9SMark Brown * Callback to provide information about a signed mixer control. 2074e13ac2e9SMark Brown * 2075e13ac2e9SMark Brown * Returns 0 for success. 2076e13ac2e9SMark Brown */ 2077e13ac2e9SMark Brown int snd_soc_info_volsw_s8(struct snd_kcontrol *kcontrol, 2078e13ac2e9SMark Brown struct snd_ctl_elem_info *uinfo) 2079e13ac2e9SMark Brown { 20804eaa9819SJon Smirl struct soc_mixer_control *mc = 20814eaa9819SJon Smirl (struct soc_mixer_control *)kcontrol->private_value; 20824eaa9819SJon Smirl int max = mc->max; 20834eaa9819SJon Smirl int min = mc->min; 2084e13ac2e9SMark Brown 2085e13ac2e9SMark Brown uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER; 2086e13ac2e9SMark Brown uinfo->count = 2; 2087e13ac2e9SMark Brown uinfo->value.integer.min = 0; 2088e13ac2e9SMark Brown uinfo->value.integer.max = max-min; 2089e13ac2e9SMark Brown return 0; 2090e13ac2e9SMark Brown } 2091e13ac2e9SMark Brown EXPORT_SYMBOL_GPL(snd_soc_info_volsw_s8); 2092e13ac2e9SMark Brown 2093e13ac2e9SMark Brown /** 2094e13ac2e9SMark Brown * snd_soc_get_volsw_s8 - signed mixer get callback 2095e13ac2e9SMark Brown * @kcontrol: mixer control 2096ac11a2b3SMark Brown * @ucontrol: control element information 2097e13ac2e9SMark Brown * 2098e13ac2e9SMark Brown * Callback to get the value of a signed mixer control. 2099e13ac2e9SMark Brown * 2100e13ac2e9SMark Brown * Returns 0 for success. 2101e13ac2e9SMark Brown */ 2102e13ac2e9SMark Brown int snd_soc_get_volsw_s8(struct snd_kcontrol *kcontrol, 2103e13ac2e9SMark Brown struct snd_ctl_elem_value *ucontrol) 2104e13ac2e9SMark Brown { 21054eaa9819SJon Smirl struct soc_mixer_control *mc = 21064eaa9819SJon Smirl (struct soc_mixer_control *)kcontrol->private_value; 2107e13ac2e9SMark Brown struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol); 2108815ecf8dSJon Smirl unsigned int reg = mc->reg; 21094eaa9819SJon Smirl int min = mc->min; 2110e13ac2e9SMark Brown int val = snd_soc_read(codec, reg); 2111e13ac2e9SMark Brown 2112e13ac2e9SMark Brown ucontrol->value.integer.value[0] = 2113e13ac2e9SMark Brown ((signed char)(val & 0xff))-min; 2114e13ac2e9SMark Brown ucontrol->value.integer.value[1] = 2115e13ac2e9SMark Brown ((signed char)((val >> 8) & 0xff))-min; 2116e13ac2e9SMark Brown return 0; 2117e13ac2e9SMark Brown } 2118e13ac2e9SMark Brown EXPORT_SYMBOL_GPL(snd_soc_get_volsw_s8); 2119e13ac2e9SMark Brown 2120e13ac2e9SMark Brown /** 2121e13ac2e9SMark Brown * snd_soc_put_volsw_sgn - signed mixer put callback 2122e13ac2e9SMark Brown * @kcontrol: mixer control 2123ac11a2b3SMark Brown * @ucontrol: control element information 2124e13ac2e9SMark Brown * 2125e13ac2e9SMark Brown * Callback to set the value of a signed mixer control. 2126e13ac2e9SMark Brown * 2127e13ac2e9SMark Brown * Returns 0 for success. 2128e13ac2e9SMark Brown */ 2129e13ac2e9SMark Brown int snd_soc_put_volsw_s8(struct snd_kcontrol *kcontrol, 2130e13ac2e9SMark Brown struct snd_ctl_elem_value *ucontrol) 2131e13ac2e9SMark Brown { 21324eaa9819SJon Smirl struct soc_mixer_control *mc = 21334eaa9819SJon Smirl (struct soc_mixer_control *)kcontrol->private_value; 2134e13ac2e9SMark Brown struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol); 2135815ecf8dSJon Smirl unsigned int reg = mc->reg; 21364eaa9819SJon Smirl int min = mc->min; 213746f5822fSDaniel Ribeiro unsigned int val; 2138e13ac2e9SMark Brown 2139e13ac2e9SMark Brown val = (ucontrol->value.integer.value[0]+min) & 0xff; 2140e13ac2e9SMark Brown val |= ((ucontrol->value.integer.value[1]+min) & 0xff) << 8; 2141e13ac2e9SMark Brown 2142e13ac2e9SMark Brown return snd_soc_update_bits(codec, reg, 0xffff, val); 2143e13ac2e9SMark Brown } 2144e13ac2e9SMark Brown EXPORT_SYMBOL_GPL(snd_soc_put_volsw_s8); 2145e13ac2e9SMark Brown 21468c6529dbSLiam Girdwood /** 21478c6529dbSLiam Girdwood * snd_soc_dai_set_sysclk - configure DAI system or master clock. 21488c6529dbSLiam Girdwood * @dai: DAI 21498c6529dbSLiam Girdwood * @clk_id: DAI specific clock ID 21508c6529dbSLiam Girdwood * @freq: new clock frequency in Hz 21518c6529dbSLiam Girdwood * @dir: new clock direction - input/output. 21528c6529dbSLiam Girdwood * 21538c6529dbSLiam Girdwood * Configures the DAI master (MCLK) or system (SYSCLK) clocking. 21548c6529dbSLiam Girdwood */ 21558c6529dbSLiam Girdwood int snd_soc_dai_set_sysclk(struct snd_soc_dai *dai, int clk_id, 21568c6529dbSLiam Girdwood unsigned int freq, int dir) 21578c6529dbSLiam Girdwood { 21583f1a4d82SMark Brown if (dai->ops && dai->ops->set_sysclk) 21596335d055SEric Miao return dai->ops->set_sysclk(dai, clk_id, freq, dir); 21608c6529dbSLiam Girdwood else 21618c6529dbSLiam Girdwood return -EINVAL; 21628c6529dbSLiam Girdwood } 21638c6529dbSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_dai_set_sysclk); 21648c6529dbSLiam Girdwood 21658c6529dbSLiam Girdwood /** 21668c6529dbSLiam Girdwood * snd_soc_dai_set_clkdiv - configure DAI clock dividers. 21678c6529dbSLiam Girdwood * @dai: DAI 2168ac11a2b3SMark Brown * @div_id: DAI specific clock divider ID 21698c6529dbSLiam Girdwood * @div: new clock divisor. 21708c6529dbSLiam Girdwood * 21718c6529dbSLiam Girdwood * Configures the clock dividers. This is used to derive the best DAI bit and 21728c6529dbSLiam Girdwood * frame clocks from the system or master clock. It's best to set the DAI bit 21738c6529dbSLiam Girdwood * and frame clocks as low as possible to save system power. 21748c6529dbSLiam Girdwood */ 21758c6529dbSLiam Girdwood int snd_soc_dai_set_clkdiv(struct snd_soc_dai *dai, 21768c6529dbSLiam Girdwood int div_id, int div) 21778c6529dbSLiam Girdwood { 21783f1a4d82SMark Brown if (dai->ops && dai->ops->set_clkdiv) 21796335d055SEric Miao return dai->ops->set_clkdiv(dai, div_id, div); 21808c6529dbSLiam Girdwood else 21818c6529dbSLiam Girdwood return -EINVAL; 21828c6529dbSLiam Girdwood } 21838c6529dbSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_dai_set_clkdiv); 21848c6529dbSLiam Girdwood 21858c6529dbSLiam Girdwood /** 21868c6529dbSLiam Girdwood * snd_soc_dai_set_pll - configure DAI PLL. 21878c6529dbSLiam Girdwood * @dai: DAI 21888c6529dbSLiam Girdwood * @pll_id: DAI specific PLL ID 21898c6529dbSLiam Girdwood * @freq_in: PLL input clock frequency in Hz 21908c6529dbSLiam Girdwood * @freq_out: requested PLL output clock frequency in Hz 21918c6529dbSLiam Girdwood * 21928c6529dbSLiam Girdwood * Configures and enables PLL to generate output clock based on input clock. 21938c6529dbSLiam Girdwood */ 21948c6529dbSLiam Girdwood int snd_soc_dai_set_pll(struct snd_soc_dai *dai, 21958c6529dbSLiam Girdwood int pll_id, unsigned int freq_in, unsigned int freq_out) 21968c6529dbSLiam Girdwood { 21973f1a4d82SMark Brown if (dai->ops && dai->ops->set_pll) 21986335d055SEric Miao return dai->ops->set_pll(dai, pll_id, freq_in, freq_out); 21998c6529dbSLiam Girdwood else 22008c6529dbSLiam Girdwood return -EINVAL; 22018c6529dbSLiam Girdwood } 22028c6529dbSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_dai_set_pll); 22038c6529dbSLiam Girdwood 22048c6529dbSLiam Girdwood /** 22058c6529dbSLiam Girdwood * snd_soc_dai_set_fmt - configure DAI hardware audio format. 22068c6529dbSLiam Girdwood * @dai: DAI 22078c6529dbSLiam Girdwood * @fmt: SND_SOC_DAIFMT_ format value. 22088c6529dbSLiam Girdwood * 22098c6529dbSLiam Girdwood * Configures the DAI hardware format and clocking. 22108c6529dbSLiam Girdwood */ 22118c6529dbSLiam Girdwood int snd_soc_dai_set_fmt(struct snd_soc_dai *dai, unsigned int fmt) 22128c6529dbSLiam Girdwood { 22133f1a4d82SMark Brown if (dai->ops && dai->ops->set_fmt) 22146335d055SEric Miao return dai->ops->set_fmt(dai, fmt); 22158c6529dbSLiam Girdwood else 22168c6529dbSLiam Girdwood return -EINVAL; 22178c6529dbSLiam Girdwood } 22188c6529dbSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_dai_set_fmt); 22198c6529dbSLiam Girdwood 22208c6529dbSLiam Girdwood /** 22218c6529dbSLiam Girdwood * snd_soc_dai_set_tdm_slot - configure DAI TDM. 22228c6529dbSLiam Girdwood * @dai: DAI 22238c6529dbSLiam Girdwood * @mask: DAI specific mask representing used slots. 22248c6529dbSLiam Girdwood * @slots: Number of slots in use. 22258c6529dbSLiam Girdwood * 22268c6529dbSLiam Girdwood * Configures a DAI for TDM operation. Both mask and slots are codec and DAI 22278c6529dbSLiam Girdwood * specific. 22288c6529dbSLiam Girdwood */ 22298c6529dbSLiam Girdwood int snd_soc_dai_set_tdm_slot(struct snd_soc_dai *dai, 22308c6529dbSLiam Girdwood unsigned int mask, int slots) 22318c6529dbSLiam Girdwood { 22323f1a4d82SMark Brown if (dai->ops && dai->ops->set_tdm_slot) 22336335d055SEric Miao return dai->ops->set_tdm_slot(dai, mask, slots); 22348c6529dbSLiam Girdwood else 22358c6529dbSLiam Girdwood return -EINVAL; 22368c6529dbSLiam Girdwood } 22378c6529dbSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_dai_set_tdm_slot); 22388c6529dbSLiam Girdwood 22398c6529dbSLiam Girdwood /** 22408c6529dbSLiam Girdwood * snd_soc_dai_set_tristate - configure DAI system or master clock. 22418c6529dbSLiam Girdwood * @dai: DAI 22428c6529dbSLiam Girdwood * @tristate: tristate enable 22438c6529dbSLiam Girdwood * 22448c6529dbSLiam Girdwood * Tristates the DAI so that others can use it. 22458c6529dbSLiam Girdwood */ 22468c6529dbSLiam Girdwood int snd_soc_dai_set_tristate(struct snd_soc_dai *dai, int tristate) 22478c6529dbSLiam Girdwood { 22483f1a4d82SMark Brown if (dai->ops && dai->ops->set_tristate) 22496335d055SEric Miao return dai->ops->set_tristate(dai, tristate); 22508c6529dbSLiam Girdwood else 22518c6529dbSLiam Girdwood return -EINVAL; 22528c6529dbSLiam Girdwood } 22538c6529dbSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_dai_set_tristate); 22548c6529dbSLiam Girdwood 22558c6529dbSLiam Girdwood /** 22568c6529dbSLiam Girdwood * snd_soc_dai_digital_mute - configure DAI system or master clock. 22578c6529dbSLiam Girdwood * @dai: DAI 22588c6529dbSLiam Girdwood * @mute: mute enable 22598c6529dbSLiam Girdwood * 22608c6529dbSLiam Girdwood * Mutes the DAI DAC. 22618c6529dbSLiam Girdwood */ 22628c6529dbSLiam Girdwood int snd_soc_dai_digital_mute(struct snd_soc_dai *dai, int mute) 22638c6529dbSLiam Girdwood { 22643f1a4d82SMark Brown if (dai->ops && dai->ops->digital_mute) 22656335d055SEric Miao return dai->ops->digital_mute(dai, mute); 22668c6529dbSLiam Girdwood else 22678c6529dbSLiam Girdwood return -EINVAL; 22688c6529dbSLiam Girdwood } 22698c6529dbSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_dai_digital_mute); 22708c6529dbSLiam Girdwood 2271c5af3a2eSMark Brown /** 2272c5af3a2eSMark Brown * snd_soc_register_card - Register a card with the ASoC core 2273c5af3a2eSMark Brown * 2274ac11a2b3SMark Brown * @card: Card to register 2275c5af3a2eSMark Brown * 2276c5af3a2eSMark Brown * Note that currently this is an internal only function: it will be 2277c5af3a2eSMark Brown * exposed to machine drivers after further backporting of ASoC v2 2278c5af3a2eSMark Brown * registration APIs. 2279c5af3a2eSMark Brown */ 2280c5af3a2eSMark Brown static int snd_soc_register_card(struct snd_soc_card *card) 2281c5af3a2eSMark Brown { 2282c5af3a2eSMark Brown if (!card->name || !card->dev) 2283c5af3a2eSMark Brown return -EINVAL; 2284c5af3a2eSMark Brown 2285c5af3a2eSMark Brown INIT_LIST_HEAD(&card->list); 2286c5af3a2eSMark Brown card->instantiated = 0; 2287c5af3a2eSMark Brown 2288c5af3a2eSMark Brown mutex_lock(&client_mutex); 2289c5af3a2eSMark Brown list_add(&card->list, &card_list); 2290435c5e25SMark Brown snd_soc_instantiate_cards(); 2291c5af3a2eSMark Brown mutex_unlock(&client_mutex); 2292c5af3a2eSMark Brown 2293c5af3a2eSMark Brown dev_dbg(card->dev, "Registered card '%s'\n", card->name); 2294c5af3a2eSMark Brown 2295c5af3a2eSMark Brown return 0; 2296c5af3a2eSMark Brown } 2297c5af3a2eSMark Brown 2298c5af3a2eSMark Brown /** 2299c5af3a2eSMark Brown * snd_soc_unregister_card - Unregister a card with the ASoC core 2300c5af3a2eSMark Brown * 2301ac11a2b3SMark Brown * @card: Card to unregister 2302c5af3a2eSMark Brown * 2303c5af3a2eSMark Brown * Note that currently this is an internal only function: it will be 2304c5af3a2eSMark Brown * exposed to machine drivers after further backporting of ASoC v2 2305c5af3a2eSMark Brown * registration APIs. 2306c5af3a2eSMark Brown */ 2307c5af3a2eSMark Brown static int snd_soc_unregister_card(struct snd_soc_card *card) 2308c5af3a2eSMark Brown { 2309c5af3a2eSMark Brown mutex_lock(&client_mutex); 2310c5af3a2eSMark Brown list_del(&card->list); 2311c5af3a2eSMark Brown mutex_unlock(&client_mutex); 2312c5af3a2eSMark Brown 2313c5af3a2eSMark Brown dev_dbg(card->dev, "Unregistered card '%s'\n", card->name); 2314c5af3a2eSMark Brown 2315c5af3a2eSMark Brown return 0; 2316c5af3a2eSMark Brown } 2317c5af3a2eSMark Brown 23186335d055SEric Miao static struct snd_soc_dai_ops null_dai_ops = { 23196335d055SEric Miao }; 23206335d055SEric Miao 23219115171aSMark Brown /** 23229115171aSMark Brown * snd_soc_register_dai - Register a DAI with the ASoC core 23239115171aSMark Brown * 2324ac11a2b3SMark Brown * @dai: DAI to register 23259115171aSMark Brown */ 23269115171aSMark Brown int snd_soc_register_dai(struct snd_soc_dai *dai) 23279115171aSMark Brown { 23289115171aSMark Brown if (!dai->name) 23299115171aSMark Brown return -EINVAL; 23309115171aSMark Brown 23319115171aSMark Brown /* The device should become mandatory over time */ 23329115171aSMark Brown if (!dai->dev) 23339115171aSMark Brown printk(KERN_WARNING "No device for DAI %s\n", dai->name); 23349115171aSMark Brown 23356335d055SEric Miao if (!dai->ops) 23366335d055SEric Miao dai->ops = &null_dai_ops; 23376335d055SEric Miao 23389115171aSMark Brown INIT_LIST_HEAD(&dai->list); 23399115171aSMark Brown 23409115171aSMark Brown mutex_lock(&client_mutex); 23419115171aSMark Brown list_add(&dai->list, &dai_list); 2342435c5e25SMark Brown snd_soc_instantiate_cards(); 23439115171aSMark Brown mutex_unlock(&client_mutex); 23449115171aSMark Brown 23459115171aSMark Brown pr_debug("Registered DAI '%s'\n", dai->name); 23469115171aSMark Brown 23479115171aSMark Brown return 0; 23489115171aSMark Brown } 23499115171aSMark Brown EXPORT_SYMBOL_GPL(snd_soc_register_dai); 23509115171aSMark Brown 23519115171aSMark Brown /** 23529115171aSMark Brown * snd_soc_unregister_dai - Unregister a DAI from the ASoC core 23539115171aSMark Brown * 2354ac11a2b3SMark Brown * @dai: DAI to unregister 23559115171aSMark Brown */ 23569115171aSMark Brown void snd_soc_unregister_dai(struct snd_soc_dai *dai) 23579115171aSMark Brown { 23589115171aSMark Brown mutex_lock(&client_mutex); 23599115171aSMark Brown list_del(&dai->list); 23609115171aSMark Brown mutex_unlock(&client_mutex); 23619115171aSMark Brown 23629115171aSMark Brown pr_debug("Unregistered DAI '%s'\n", dai->name); 23639115171aSMark Brown } 23649115171aSMark Brown EXPORT_SYMBOL_GPL(snd_soc_unregister_dai); 23659115171aSMark Brown 23669115171aSMark Brown /** 23679115171aSMark Brown * snd_soc_register_dais - Register multiple DAIs with the ASoC core 23689115171aSMark Brown * 2369ac11a2b3SMark Brown * @dai: Array of DAIs to register 2370ac11a2b3SMark Brown * @count: Number of DAIs 23719115171aSMark Brown */ 23729115171aSMark Brown int snd_soc_register_dais(struct snd_soc_dai *dai, size_t count) 23739115171aSMark Brown { 23749115171aSMark Brown int i, ret; 23759115171aSMark Brown 23769115171aSMark Brown for (i = 0; i < count; i++) { 23779115171aSMark Brown ret = snd_soc_register_dai(&dai[i]); 23789115171aSMark Brown if (ret != 0) 23799115171aSMark Brown goto err; 23809115171aSMark Brown } 23819115171aSMark Brown 23829115171aSMark Brown return 0; 23839115171aSMark Brown 23849115171aSMark Brown err: 23859115171aSMark Brown for (i--; i >= 0; i--) 23869115171aSMark Brown snd_soc_unregister_dai(&dai[i]); 23879115171aSMark Brown 23889115171aSMark Brown return ret; 23899115171aSMark Brown } 23909115171aSMark Brown EXPORT_SYMBOL_GPL(snd_soc_register_dais); 23919115171aSMark Brown 23929115171aSMark Brown /** 23939115171aSMark Brown * snd_soc_unregister_dais - Unregister multiple DAIs from the ASoC core 23949115171aSMark Brown * 2395ac11a2b3SMark Brown * @dai: Array of DAIs to unregister 2396ac11a2b3SMark Brown * @count: Number of DAIs 23979115171aSMark Brown */ 23989115171aSMark Brown void snd_soc_unregister_dais(struct snd_soc_dai *dai, size_t count) 23999115171aSMark Brown { 24009115171aSMark Brown int i; 24019115171aSMark Brown 24029115171aSMark Brown for (i = 0; i < count; i++) 24039115171aSMark Brown snd_soc_unregister_dai(&dai[i]); 24049115171aSMark Brown } 24059115171aSMark Brown EXPORT_SYMBOL_GPL(snd_soc_unregister_dais); 24069115171aSMark Brown 240712a48a8cSMark Brown /** 240812a48a8cSMark Brown * snd_soc_register_platform - Register a platform with the ASoC core 240912a48a8cSMark Brown * 2410ac11a2b3SMark Brown * @platform: platform to register 241112a48a8cSMark Brown */ 241212a48a8cSMark Brown int snd_soc_register_platform(struct snd_soc_platform *platform) 241312a48a8cSMark Brown { 241412a48a8cSMark Brown if (!platform->name) 241512a48a8cSMark Brown return -EINVAL; 241612a48a8cSMark Brown 241712a48a8cSMark Brown INIT_LIST_HEAD(&platform->list); 241812a48a8cSMark Brown 241912a48a8cSMark Brown mutex_lock(&client_mutex); 242012a48a8cSMark Brown list_add(&platform->list, &platform_list); 2421435c5e25SMark Brown snd_soc_instantiate_cards(); 242212a48a8cSMark Brown mutex_unlock(&client_mutex); 242312a48a8cSMark Brown 242412a48a8cSMark Brown pr_debug("Registered platform '%s'\n", platform->name); 242512a48a8cSMark Brown 242612a48a8cSMark Brown return 0; 242712a48a8cSMark Brown } 242812a48a8cSMark Brown EXPORT_SYMBOL_GPL(snd_soc_register_platform); 242912a48a8cSMark Brown 243012a48a8cSMark Brown /** 243112a48a8cSMark Brown * snd_soc_unregister_platform - Unregister a platform from the ASoC core 243212a48a8cSMark Brown * 2433ac11a2b3SMark Brown * @platform: platform to unregister 243412a48a8cSMark Brown */ 243512a48a8cSMark Brown void snd_soc_unregister_platform(struct snd_soc_platform *platform) 243612a48a8cSMark Brown { 243712a48a8cSMark Brown mutex_lock(&client_mutex); 243812a48a8cSMark Brown list_del(&platform->list); 243912a48a8cSMark Brown mutex_unlock(&client_mutex); 244012a48a8cSMark Brown 244112a48a8cSMark Brown pr_debug("Unregistered platform '%s'\n", platform->name); 244212a48a8cSMark Brown } 244312a48a8cSMark Brown EXPORT_SYMBOL_GPL(snd_soc_unregister_platform); 244412a48a8cSMark Brown 2445151ab22cSMark Brown static u64 codec_format_map[] = { 2446151ab22cSMark Brown SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S16_BE, 2447151ab22cSMark Brown SNDRV_PCM_FMTBIT_U16_LE | SNDRV_PCM_FMTBIT_U16_BE, 2448151ab22cSMark Brown SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S24_BE, 2449151ab22cSMark Brown SNDRV_PCM_FMTBIT_U24_LE | SNDRV_PCM_FMTBIT_U24_BE, 2450151ab22cSMark Brown SNDRV_PCM_FMTBIT_S32_LE | SNDRV_PCM_FMTBIT_S32_BE, 2451151ab22cSMark Brown SNDRV_PCM_FMTBIT_U32_LE | SNDRV_PCM_FMTBIT_U32_BE, 2452151ab22cSMark Brown SNDRV_PCM_FMTBIT_S24_3LE | SNDRV_PCM_FMTBIT_U24_3BE, 2453151ab22cSMark Brown SNDRV_PCM_FMTBIT_U24_3LE | SNDRV_PCM_FMTBIT_U24_3BE, 2454151ab22cSMark Brown SNDRV_PCM_FMTBIT_S20_3LE | SNDRV_PCM_FMTBIT_S20_3BE, 2455151ab22cSMark Brown SNDRV_PCM_FMTBIT_U20_3LE | SNDRV_PCM_FMTBIT_U20_3BE, 2456151ab22cSMark Brown SNDRV_PCM_FMTBIT_S18_3LE | SNDRV_PCM_FMTBIT_S18_3BE, 2457151ab22cSMark Brown SNDRV_PCM_FMTBIT_U18_3LE | SNDRV_PCM_FMTBIT_U18_3BE, 2458151ab22cSMark Brown SNDRV_PCM_FMTBIT_FLOAT_LE | SNDRV_PCM_FMTBIT_FLOAT_BE, 2459151ab22cSMark Brown SNDRV_PCM_FMTBIT_FLOAT64_LE | SNDRV_PCM_FMTBIT_FLOAT64_BE, 2460151ab22cSMark Brown SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_LE 2461151ab22cSMark Brown | SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_BE, 2462151ab22cSMark Brown }; 2463151ab22cSMark Brown 2464151ab22cSMark Brown /* Fix up the DAI formats for endianness: codecs don't actually see 2465151ab22cSMark Brown * the endianness of the data but we're using the CPU format 2466151ab22cSMark Brown * definitions which do need to include endianness so we ensure that 2467151ab22cSMark Brown * codec DAIs always have both big and little endian variants set. 2468151ab22cSMark Brown */ 2469151ab22cSMark Brown static void fixup_codec_formats(struct snd_soc_pcm_stream *stream) 2470151ab22cSMark Brown { 2471151ab22cSMark Brown int i; 2472151ab22cSMark Brown 2473151ab22cSMark Brown for (i = 0; i < ARRAY_SIZE(codec_format_map); i++) 2474151ab22cSMark Brown if (stream->formats & codec_format_map[i]) 2475151ab22cSMark Brown stream->formats |= codec_format_map[i]; 2476151ab22cSMark Brown } 2477151ab22cSMark Brown 24780d0cf00aSMark Brown /** 24790d0cf00aSMark Brown * snd_soc_register_codec - Register a codec with the ASoC core 24800d0cf00aSMark Brown * 2481ac11a2b3SMark Brown * @codec: codec to register 24820d0cf00aSMark Brown */ 24830d0cf00aSMark Brown int snd_soc_register_codec(struct snd_soc_codec *codec) 24840d0cf00aSMark Brown { 2485151ab22cSMark Brown int i; 2486151ab22cSMark Brown 24870d0cf00aSMark Brown if (!codec->name) 24880d0cf00aSMark Brown return -EINVAL; 24890d0cf00aSMark Brown 24900d0cf00aSMark Brown /* The device should become mandatory over time */ 24910d0cf00aSMark Brown if (!codec->dev) 24920d0cf00aSMark Brown printk(KERN_WARNING "No device for codec %s\n", codec->name); 24930d0cf00aSMark Brown 24940d0cf00aSMark Brown INIT_LIST_HEAD(&codec->list); 24950d0cf00aSMark Brown 2496151ab22cSMark Brown for (i = 0; i < codec->num_dai; i++) { 2497151ab22cSMark Brown fixup_codec_formats(&codec->dai[i].playback); 2498151ab22cSMark Brown fixup_codec_formats(&codec->dai[i].capture); 2499151ab22cSMark Brown } 2500151ab22cSMark Brown 25010d0cf00aSMark Brown mutex_lock(&client_mutex); 25020d0cf00aSMark Brown list_add(&codec->list, &codec_list); 25030d0cf00aSMark Brown snd_soc_instantiate_cards(); 25040d0cf00aSMark Brown mutex_unlock(&client_mutex); 25050d0cf00aSMark Brown 25060d0cf00aSMark Brown pr_debug("Registered codec '%s'\n", codec->name); 25070d0cf00aSMark Brown 25080d0cf00aSMark Brown return 0; 25090d0cf00aSMark Brown } 25100d0cf00aSMark Brown EXPORT_SYMBOL_GPL(snd_soc_register_codec); 25110d0cf00aSMark Brown 25120d0cf00aSMark Brown /** 25130d0cf00aSMark Brown * snd_soc_unregister_codec - Unregister a codec from the ASoC core 25140d0cf00aSMark Brown * 2515ac11a2b3SMark Brown * @codec: codec to unregister 25160d0cf00aSMark Brown */ 25170d0cf00aSMark Brown void snd_soc_unregister_codec(struct snd_soc_codec *codec) 25180d0cf00aSMark Brown { 25190d0cf00aSMark Brown mutex_lock(&client_mutex); 25200d0cf00aSMark Brown list_del(&codec->list); 25210d0cf00aSMark Brown mutex_unlock(&client_mutex); 25220d0cf00aSMark Brown 25230d0cf00aSMark Brown pr_debug("Unregistered codec '%s'\n", codec->name); 25240d0cf00aSMark Brown } 25250d0cf00aSMark Brown EXPORT_SYMBOL_GPL(snd_soc_unregister_codec); 25260d0cf00aSMark Brown 2527c9b3a40fSTakashi Iwai static int __init snd_soc_init(void) 2528db2a4165SFrank Mandarino { 2529384c89e2SMark Brown #ifdef CONFIG_DEBUG_FS 2530384c89e2SMark Brown debugfs_root = debugfs_create_dir("asoc", NULL); 2531384c89e2SMark Brown if (IS_ERR(debugfs_root) || !debugfs_root) { 2532384c89e2SMark Brown printk(KERN_WARNING 2533384c89e2SMark Brown "ASoC: Failed to create debugfs directory\n"); 2534384c89e2SMark Brown debugfs_root = NULL; 2535384c89e2SMark Brown } 2536384c89e2SMark Brown #endif 2537384c89e2SMark Brown 2538db2a4165SFrank Mandarino return platform_driver_register(&soc_driver); 2539db2a4165SFrank Mandarino } 2540db2a4165SFrank Mandarino 25417d8c16a6SMark Brown static void __exit snd_soc_exit(void) 2542db2a4165SFrank Mandarino { 2543384c89e2SMark Brown #ifdef CONFIG_DEBUG_FS 2544384c89e2SMark Brown debugfs_remove_recursive(debugfs_root); 2545384c89e2SMark Brown #endif 2546db2a4165SFrank Mandarino platform_driver_unregister(&soc_driver); 2547db2a4165SFrank Mandarino } 2548db2a4165SFrank Mandarino 2549db2a4165SFrank Mandarino module_init(snd_soc_init); 2550db2a4165SFrank Mandarino module_exit(snd_soc_exit); 2551db2a4165SFrank Mandarino 2552db2a4165SFrank Mandarino /* Module information */ 2553d331124dSLiam Girdwood MODULE_AUTHOR("Liam Girdwood, lrg@slimlogic.co.uk"); 2554db2a4165SFrank Mandarino MODULE_DESCRIPTION("ALSA SoC Core"); 2555db2a4165SFrank Mandarino MODULE_LICENSE("GPL"); 25568b45a209SKay Sievers MODULE_ALIAS("platform:soc-audio"); 2557